MoroSound: Add speaker and earpiece boost directly to sound driver

We are setting our gains on every speaker and earpiece executing with static defs
This commit is contained in:
xxmustafacooTR 2021-04-03 21:17:01 -04:00
parent f810ad12f6
commit c58f32f9c9
No known key found for this signature in database
GPG key ID: 520B6FE385CBF5C9
5 changed files with 299 additions and 22 deletions

View file

@ -1987,6 +1987,8 @@ static int cs47l92_codec_probe(struct snd_soc_codec *codec)
#ifdef CONFIG_MORO_SOUND
moro_sound_hook_madera_pcm_probe(madera->regmap);
cs47l92->core.madera->dapm = snd_soc_codec_get_dapm(codec);
#endif
ret = madera_init_outputs(codec, CS47L92_MONO_OUTPUTS);

View file

@ -16,6 +16,9 @@
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/tlv.h>
#ifdef CONFIG_MORO_SOUND
#include "moro_sound.h"
#endif
#include <linux/mfd/madera/core.h>
#include <linux/mfd/madera/registers.h>
@ -1376,6 +1379,10 @@ int madera_init_aif(struct snd_soc_codec *codec)
struct madera *madera = priv->madera;
int ret;
#ifdef CONFIG_MORO_SOUND
moro_sound_hook_madera_pcm_probe(madera->regmap);
#endif
/* Update Sample Rate 1 to 48kHz for cases when no AIF1 hw_params */
ret = regmap_update_bits(madera->regmap, MADERA_SAMPLE_RATE_1,
MADERA_SAMPLE_RATE_1_MASK, 0x03);

View file

@ -42,6 +42,13 @@
#define msg_maxim(format, args...)
#endif /* DEBUG_MAX98512 */
#ifdef CONFIG_MORO_SOUND
#include "moro_sound.h"
/* This is default value but we will bypass this value */
int moro_speaker_value = 12;
int moro_earpiece_value = 12;
#endif
struct max98512_priv *g_max98512;
static int max98512_remap_reg(int reg, int revID)
@ -1232,6 +1239,105 @@ static int max98512_adc_config(struct max98512_priv *max98512)
return 0;
}
#ifdef CONFIG_MORO_SOUND
int get_speaker_gain(void)
{
return moro_speaker_value; //used math for I cant set kernel app to 0-7 values (that is for 0-18)
}
int set_speaker_gain_value(int gain)
{
moro_speaker_value = gain;
return moro_speaker_value; //used math for I cant set kernel app to 0-7 values (that is for 0-18)
}
static int set_speaker_gain(struct max98512_priv *max98512)
{
unsigned int gain = 12;
gain = moro_speaker_value;
if(12==gain)
return gain;
if(gain<16)
goto spkgain;
// MAX98512_R0035_AMP_VOL_CTRL
max98512_wrapper_update(max98512, MAX98512R,
MAX98512_R0035_AMP_VOL_CTRL,
MAX98512_AMP_VOL_MASK,
(gain+2)*4);
max98512->digital_gain = (gain+2)*4;
spkgain:
// MAX98512_R003A_SPK_GAIN
gain = (gain/3)+1;
max98512_wrapper_update(max98512, MAX98512R,
MAX98512_R003A_SPK_GAIN,
MAX98512_SPK_PCM_GAIN_MASK,
gain);
max98512->spk_gain_right = gain;
max98512->spk_gain = gain;
return max98512->spk_gain_right;
}
int get_earpiece_gain(void)
{
return moro_earpiece_value; //used math for I cant set kernel app to 0-7 values (that is for 0-18)
}
int set_earpiece_gain_value(int gain)
{
moro_earpiece_value = gain;
return moro_speaker_value; //used math for I cant set kernel app to 0-7 values (that is for 0-18)
}
static int set_earpiece_gain(struct max98512_priv *max98512)
{
unsigned int gain = 12;
gain = moro_earpiece_value;
if(12==gain)
return gain;
if(gain<16)
goto eargain;
// MAX98512_R0035_AMP_VOL_CTRL
max98512_wrapper_update(max98512, MAX98512L,
MAX98512_R0035_AMP_VOL_CTRL,
MAX98512_AMP_VOL_MASK,
(gain+2)*4);
max98512->digital_gain_rcv = (gain+2)*4;
eargain:
// MAX98512_R003A_SPK_GAIN
gain = (gain/3)+1;
max98512_wrapper_update(max98512, MAX98512L,
MAX98512_R003A_SPK_GAIN,
MAX98512_SPK_PCM_GAIN_MASK,
gain);
max98512->spk_gain_left = gain;
max98512->spk_gain = gain;
return max98512->spk_gain_left;
}
#endif
static int __max98512_spk_enable(struct max98512_priv *max98512)
{
struct max98512_pdata *pdata = max98512->pdata;
@ -1332,8 +1438,8 @@ static int __max98512_spk_enable(struct max98512_priv *max98512)
battery_temp = maxdsm_cal_get_temp_from_power_supply();
if (battery_temp > 50) {
msg_maxim("battery_temp[%d] over 50", battery_temp);
if (battery_temp > 55) {
msg_maxim("battery_temp[%d] over 55", battery_temp);
max98512_wrapper_write(max98512, MAX98512B,
MAX98512_R0059_BROWNOUT_LVL2_THRESH,
0x30);
@ -1341,7 +1447,7 @@ static int __max98512_spk_enable(struct max98512_priv *max98512)
MAX98512_R005A_BROWNOUT_LVL3_THRESH,
0x10);
} else {
msg_maxim("battery_temp[%d] under 50", battery_temp);
msg_maxim("battery_temp[%d] under 55", battery_temp);
max98512_wrapper_write(max98512, MAX98512B,
MAX98512_R0059_BROWNOUT_LVL2_THRESH,
0x40);
@ -1405,6 +1511,10 @@ static int __max98512_spk_enable(struct max98512_priv *max98512)
MAX98512_R0400_GLOBAL_SHDN,
MAX98512_GLOBAL_EN_MASK, enable_r);
}
#ifdef CONFIG_MORO_SOUND
set_speaker_gain(max98512);
set_earpiece_gain(max98512);
#endif
return 0;
}
@ -1448,6 +1558,9 @@ static void max98512_spk_enable_l(struct max98512_priv *max98512, int enable)
MAX98512_R003A_SPK_GAIN,
MAX98512_SPK_PCM_GAIN_MASK,
max98512->spk_gain_left);
#ifdef CONFIG_MORO_SOUND
set_earpiece_gain(max98512);
#endif
} else {
max98512_wrapper_update(max98512, MAX98512L,
MAX98512_R003A_SPK_GAIN,
@ -1895,6 +2008,9 @@ static int max98512_analog_gain_l_put(struct snd_kcontrol *kcontrol,
sel);
max98512->spk_gain_left = sel;
#ifdef CONFIG_MORO_SOUND
set_earpiece_gain(max98512);
#endif
}
return 0;
@ -1929,6 +2045,9 @@ static int max98512_analog_gain_r_put(struct snd_kcontrol *kcontrol,
sel);
max98512->spk_gain_right = sel;
#ifdef CONFIG_MORO_SOUND
set_speaker_gain(max98512);
#endif
}
return 0;
@ -2224,6 +2343,10 @@ static int max98512_probe(struct snd_soc_codec *codec)
max98512_wrapper_write(max98512, MAX98512B,
MAX98512_R003A_SPK_GAIN,
0x05);
#ifdef CONFIG_MORO_SOUND
set_speaker_gain(max98512);
set_earpiece_gain(max98512);
#endif
/* Enable DC blocker */
max98512_wrapper_write(max98512, MAX98512B,
MAX98512_R0036_AMP_DSP_CFG,
@ -2709,6 +2832,10 @@ static int max98512_i2c_probe(struct i2c_client *i2c,
vstep->boost_step[MAX98512_VSTEP_15] = 0x10; /* 8.5V */
max98512->spk_gain = 0x05; /* +15db for PCM */
max98512->digital_gain = 0x40; /* 0db */
#ifdef CONFIG_MORO_SOUND
set_speaker_gain(max98512);
set_earpiece_gain(max98512);
#endif
max98512->mono_stereo = 0;
max98512->interleave_mode = 0;
vstep->adc_thres = MAX98512_VSTEP_8;
@ -2938,3 +3065,4 @@ module_i2c_driver(max98512_i2c_driver)
MODULE_DESCRIPTION("ALSA SoC MAX98512 driver");
MODULE_AUTHOR("Ryan Lee <ryans.lee@maximintegrated.com>");
MODULE_LICENSE("GPL");

View file

@ -4,6 +4,7 @@
* Author : @morogoku https://github.com/morogoku
*
* Date : March 2019 - v2.0
* : April 2019 - v2.1
*
*
* Based on the Boeffla Sound 1.6 for Galaxy S3
@ -11,12 +12,13 @@
* Credits: andip71, author of Boeffla Sound
* Supercurio, Yank555 and Gokhanmoral.
*
* AndreiLux, for his Arizona control sound mod
* AndreiLux, for his Madera control sound mod
*
* Flar2, for his speaker gain mod
*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include "moro_sound.h"
@ -37,6 +39,9 @@ static int debug; // debug switch
static int headphone_gain_l; // headphone volume left
static int headphone_gain_r; // headphone volume right
static int earpiece_gain; // earpiece volume
static int speaker_gain; // speaker volume
static int out2l_mix_source; // out2 mix source left
static int out2r_mix_source; // out2 mix source right
static int eq1_mix_source; // eq1 mix source left
@ -180,12 +185,12 @@ static void set_eq(void)
_write(MADERA_EQ2_1, val);
// Set mixers
eq1_mix_source = 32; // EQ1 -> AIF1 RX1 left
eq1_mix_source = 33; // EQ1 -> AIF1 RX1 left
eq2_mix_source = 33; // EQ2 -> AIF1 RX2 right
set_eq1_mix_source(eq1_mix_source);
set_eq2_mix_source(eq2_mix_source);
out2l_mix_source = 80; // OUT2L -> EQ1 left
out2l_mix_source = 81; // OUT2L -> EQ1 left
out2r_mix_source = 81; // OUT2R -> EQ2 right
set_out2l_mix_source(out2l_mix_source);
set_out2r_mix_source(out2r_mix_source);
@ -308,6 +313,14 @@ unsigned int moro_sound_write_hook(unsigned int reg, unsigned int val)
break;
}
// earpiece
case MADERA_DAC_DIGITAL_VOLUME_3L:
{
val &= ~MADERA_OUT3L_VOL_MASK;
val |= (earpiece_gain << MADERA_OUT3L_VOL_SHIFT);
break;
}
if (eq){
// hpout2 l
case MADERA_OUT2LMIX_INPUT_1_SOURCE:
@ -338,6 +351,9 @@ static void reset_moro_sound(void)
{
// set all moro sound config settings to defaults
earpiece_gain = EARPIECE_DEFAULT;
speaker_gain = SPEAKER_DEFAULT;
headphone_gain_l = HEADPHONE_DEFAULT;
headphone_gain_r = HEADPHONE_DEFAULT;
@ -361,6 +377,9 @@ static void reset_audio_hub(void)
set_headphone_gain_l(HEADPHONE_DEFAULT);
set_headphone_gain_r(HEADPHONE_DEFAULT);
set_earpiece_gain_value(EARPIECE_DEFAULT);
set_speaker_gain_value(SPEAKER_DEFAULT);
set_out2l_mix_source(OUT2L_MIX_DEFAULT);
set_out2r_mix_source(OUT2R_MIX_DEFAULT);
@ -370,7 +389,7 @@ static void reset_audio_hub(void)
set_eq();
if (debug)
printk("Moro-sound: moon audio hub reset done\n");
printk("Moro-sound: madera audio hub reset done\n");
}
@ -378,6 +397,9 @@ static void update_audio_hub(void)
{
// reset all audio hub registers back to defaults
set_earpiece_gain_value(earpiece_gain);
set_speaker_gain_value(speaker_gain);
set_headphone_gain_l(headphone_gain_l);
set_headphone_gain_r(headphone_gain_r);
@ -390,7 +412,7 @@ static void update_audio_hub(void)
set_eq();
if (debug)
printk("Moro-sound: moon audio hub updated done\n");
printk("Moro-sound: madera audio hub updated done\n");
}
@ -435,11 +457,8 @@ static ssize_t moro_sound_store(struct device *dev, struct device_attribute *att
first = 0;
}
if(val == 1) {
update_audio_hub();
} else {
reset_audio_hub();
}
if(val == 1) update_audio_hub();
if(val == 0) reset_audio_hub();
}
// print debug info
@ -477,6 +496,15 @@ static ssize_t headphone_gain_store(struct device *dev, struct device_attribute
if (ret != 2)
return -EINVAL;
if (val_l < HEADPHONE_MIN)
val_l = HEADPHONE_MIN;
if (val_l > HEADPHONE_MAX)
val_l = HEADPHONE_MAX;
if (val_r < HEADPHONE_MIN)
val_r = HEADPHONE_MIN;
if (val_r > HEADPHONE_MAX)
val_r = HEADPHONE_MAX;
// store new values
headphone_gain_l = val_l;
headphone_gain_r = val_r;
@ -491,6 +519,80 @@ static ssize_t headphone_gain_store(struct device *dev, struct device_attribute
return count;
}
static ssize_t headphone_limits_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// return version information
return sprintf(buf, "Min:%u Max:%u Def:%u\n", HEADPHONE_MIN, HEADPHONE_MAX, HEADPHONE_DEFAULT);
}
// Earpiece Volume
static ssize_t earpiece_gain_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// print current values
return sprintf(buf, "%d\n", earpiece_gain);
}
static ssize_t earpiece_gain_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned int ret = -EINVAL;
int val;
// Terminate if moro sound is not enabled
if (!moro_sound)
return count;
// read values from input buffer
ret = sscanf(buf, "%d", &val);
if (ret != 1)
return -EINVAL;
if (val < EARPIECE_MIN)
val = EARPIECE_MIN;
if (val > EARPIECE_MAX)
val = EARPIECE_MAX;
// store new values
earpiece_gain = val;
// set new values
set_earpiece_gain_value(earpiece_gain);
// print debug info
if (debug)
printk("Moro-sound: earpiece volume: %d\n", earpiece_gain);
return count;
}
static ssize_t earpiece_limits_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// return version information
return sprintf(buf, "Min:%u Max:%u Def:%u\n", EARPIECE_MIN, EARPIECE_MAX, EARPIECE_DEFAULT);
}
// Speaker Volume
static ssize_t speaker_gain_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// print current values
return sprintf(buf, "%d\n", speaker_gain);
}
static ssize_t speaker_gain_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned int ret = -EINVAL;
int val;
// read values from input buffer
ret = sscanf(buf, "%d", &val);
if (ret != 1)
return -EINVAL;
if (val < SPEAKER_MIN)
val = SPEAKER_MIN;
if (val > SPEAKER_MAX)
val = SPEAKER_MAX;
// store new values
speaker_gain = val;
// set new values
set_speaker_gain_value(speaker_gain);
// print debug info
if (debug)
printk("Moro-sound: speaker volume: %d\n", speaker_gain);
return count;
}
static ssize_t speaker_limits_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// return version information
return sprintf(buf, "Min:%u Max:%u Def:%u\n", SPEAKER_MIN, SPEAKER_MAX, SPEAKER_DEFAULT);
}
// EQ
@ -842,6 +944,8 @@ static ssize_t reg_dump_show(struct device *dev, struct device_attribute *attr,
headphone_gain_l: reg: %d, variable: %d\n\
headphone_gain_r: reg: %d, variable: %d\n\
first enable: %d\n\
earpiece_gain: %d\n\
speaker_gain: %d\n\
HPOUT2 Enabled: %d\n\
HPOUT2L Source: %d\n\
HPOUT2R Source: %d\n\
@ -860,6 +964,8 @@ headphone_gain_l,
get_headphone_gain_r(),
headphone_gain_r,
first,
get_earpiece_gain(),
get_speaker_gain(),
out2_ena,
out2l_mix,
out2r_mix,
@ -891,6 +997,11 @@ static ssize_t version_show(struct device *dev, struct device_attribute *attr, c
// define objects
static DEVICE_ATTR(moro_sound, 0664, moro_sound_show, moro_sound_store);
static DEVICE_ATTR(headphone_gain, 0664, headphone_gain_show, headphone_gain_store);
static DEVICE_ATTR(headphone_limits, 0664, headphone_limits_show, NULL);
static DEVICE_ATTR(earpiece_gain, 0664, earpiece_gain_show, earpiece_gain_store);
static DEVICE_ATTR(earpiece_limits, 0664, earpiece_limits_show, NULL);
static DEVICE_ATTR(speaker_gain, 0664, speaker_gain_show, speaker_gain_store);
static DEVICE_ATTR(speaker_limits, 0664, speaker_limits_show, NULL);
static DEVICE_ATTR(eq, 0664, eq_show, eq_store);
static DEVICE_ATTR(eq_gains, 0664, eq_gains_show, eq_gains_store);
static DEVICE_ATTR(eq_b1_gain, 0664, eq_b1_gain_show, eq_b1_gain_store);
@ -906,6 +1017,11 @@ static DEVICE_ATTR(reg_dump, 0664, reg_dump_show, NULL);
static struct attribute *moro_sound_attributes[] = {
&dev_attr_moro_sound.attr,
&dev_attr_headphone_gain.attr,
&dev_attr_headphone_limits.attr,
&dev_attr_earpiece_gain.attr,
&dev_attr_earpiece_limits.attr,
&dev_attr_speaker_gain.attr,
&dev_attr_speaker_limits.attr,
&dev_attr_eq.attr,
&dev_attr_eq_gains.attr,
&dev_attr_eq_b1_gain.attr,
@ -936,14 +1052,23 @@ static struct miscdevice moro_sound_control_device = {
// Driver init and exit functions
/*****************************************/
static int moro_sound_init(void)
static int __init moro_sound_init(void)
{
int err = 0;
// register moro sound control device
misc_register(&moro_sound_control_device);
if (sysfs_create_group(&moro_sound_control_device.this_device->kobj,
&moro_sound_control_group) < 0) {
printk("Moro-sound: failed to create sys fs object.\n");
return 0;
err = misc_register(&moro_sound_control_device);
if (err) {
pr_err("failed register the device.\n");
return err;
}
err = sysfs_create_group(&moro_sound_control_device.this_device->kobj,
&moro_sound_control_group);
if (err) {
pr_err("failed to create sys fs object.\n");
misc_deregister(&moro_sound_control_device);
return err;
}
// Initialize moro sound master switch with OFF per default (will be set to correct
@ -961,12 +1086,14 @@ static int moro_sound_init(void)
}
static void moro_sound_exit(void)
static void __exit moro_sound_exit(void)
{
// remove moro sound control device
sysfs_remove_group(&moro_sound_control_device.this_device->kobj,
&moro_sound_control_group);
misc_deregister(&moro_sound_control_device);
// Print debug info
printk("Moro-sound: engine stopped\n");
}
@ -977,4 +1104,3 @@ static void moro_sound_exit(void)
module_init(moro_sound_init);
module_exit(moro_sound_exit);

View file

@ -23,6 +23,10 @@
void moro_sound_hook_madera_pcm_probe(struct regmap *pmap);
int _regmap_write_nohook(struct regmap *map, unsigned int reg, unsigned int val);
int set_speaker_gain_value(int gain);
int get_speaker_gain(void);
int set_earpiece_gain_value(int gain);
int get_earpiece_gain(void);
/*****************************************/
@ -39,8 +43,18 @@ int _regmap_write_nohook(struct regmap *map, unsigned int reg, unsigned int val)
#define HEADPHONE_MIN 60
#define HEADPHONE_MAX 190
/* Earpiece levels */
#define EARPIECE_DEFAULT 12
#define EARPIECE_MIN 0
#define EARPIECE_MAX 18
/* Speaker levels */
#define SPEAKER_DEFAULT 12
#define SPEAKER_MIN 0
#define SPEAKER_MAX 18
// Mixers sources
#define OUT2L_MIX_DEFAULT 32
#define OUT2L_MIX_DEFAULT 33
#define OUT2R_MIX_DEFAULT 33
#define EQ1_MIX_DEFAULT 0
#define EQ2_MIX_DEFAULT 0