aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/twl6040.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/twl6040.c')
-rw-r--r--sound/soc/codecs/twl6040.c227
1 files changed, 66 insertions, 161 deletions
diff --git a/sound/soc/codecs/twl6040.c b/sound/soc/codecs/twl6040.c
index f2f4bcb2ff71..0afe8bef6765 100644
--- a/sound/soc/codecs/twl6040.c
+++ b/sound/soc/codecs/twl6040.c
@@ -72,6 +72,7 @@ struct twl6040_data {
72 int hs_power_mode_locked; 72 int hs_power_mode_locked;
73 bool dl1_unmuted; 73 bool dl1_unmuted;
74 bool dl2_unmuted; 74 bool dl2_unmuted;
75 u8 dl12_cache[TWL6040_REG_HFRCTL - TWL6040_REG_HSLCTL + 1];
75 unsigned int clk_in; 76 unsigned int clk_in;
76 unsigned int sysclk; 77 unsigned int sysclk;
77 struct twl6040_jack_data hs_jack; 78 struct twl6040_jack_data hs_jack;
@@ -79,75 +80,6 @@ struct twl6040_data {
79 struct mutex mutex; 80 struct mutex mutex;
80}; 81};
81 82
82/*
83 * twl6040 register cache & default register settings
84 */
85static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
86 0x00, /* not used 0x00 */
87 0x4B, /* REG_ASICID 0x01 (ro) */
88 0x00, /* REG_ASICREV 0x02 (ro) */
89 0x00, /* REG_INTID 0x03 */
90 0x00, /* REG_INTMR 0x04 */
91 0x00, /* REG_NCPCTRL 0x05 */
92 0x00, /* REG_LDOCTL 0x06 */
93 0x60, /* REG_HPPLLCTL 0x07 */
94 0x00, /* REG_LPPLLCTL 0x08 */
95 0x4A, /* REG_LPPLLDIV 0x09 */
96 0x00, /* REG_AMICBCTL 0x0A */
97 0x00, /* REG_DMICBCTL 0x0B */
98 0x00, /* REG_MICLCTL 0x0C */
99 0x00, /* REG_MICRCTL 0x0D */
100 0x00, /* REG_MICGAIN 0x0E */
101 0x1B, /* REG_LINEGAIN 0x0F */
102 0x00, /* REG_HSLCTL 0x10 */
103 0x00, /* REG_HSRCTL 0x11 */
104 0x00, /* REG_HSGAIN 0x12 */
105 0x00, /* REG_EARCTL 0x13 */
106 0x00, /* REG_HFLCTL 0x14 */
107 0x00, /* REG_HFLGAIN 0x15 */
108 0x00, /* REG_HFRCTL 0x16 */
109 0x00, /* REG_HFRGAIN 0x17 */
110 0x00, /* REG_VIBCTLL 0x18 */
111 0x00, /* REG_VIBDATL 0x19 */
112 0x00, /* REG_VIBCTLR 0x1A */
113 0x00, /* REG_VIBDATR 0x1B */
114 0x00, /* REG_HKCTL1 0x1C */
115 0x00, /* REG_HKCTL2 0x1D */
116 0x00, /* REG_GPOCTL 0x1E */
117 0x00, /* REG_ALB 0x1F */
118 0x00, /* REG_DLB 0x20 */
119 0x00, /* not used 0x21 */
120 0x00, /* not used 0x22 */
121 0x00, /* not used 0x23 */
122 0x00, /* not used 0x24 */
123 0x00, /* not used 0x25 */
124 0x00, /* not used 0x26 */
125 0x00, /* not used 0x27 */
126 0x00, /* REG_TRIM1 0x28 */
127 0x00, /* REG_TRIM2 0x29 */
128 0x00, /* REG_TRIM3 0x2A */
129 0x00, /* REG_HSOTRIM 0x2B */
130 0x00, /* REG_HFOTRIM 0x2C */
131 0x09, /* REG_ACCCTL 0x2D */
132 0x00, /* REG_STATUS 0x2E (ro) */
133};
134
135/* List of registers to be restored after power up */
136static const int twl6040_restore_list[] = {
137 TWL6040_REG_MICLCTL,
138 TWL6040_REG_MICRCTL,
139 TWL6040_REG_MICGAIN,
140 TWL6040_REG_LINEGAIN,
141 TWL6040_REG_HSLCTL,
142 TWL6040_REG_HSRCTL,
143 TWL6040_REG_HSGAIN,
144 TWL6040_REG_EARCTL,
145 TWL6040_REG_HFLCTL,
146 TWL6040_REG_HFLGAIN,
147 TWL6040_REG_HFRCTL,
148 TWL6040_REG_HFRGAIN,
149};
150
151/* set of rates for each pll: low-power and high-performance */ 83/* set of rates for each pll: low-power and high-performance */
152static unsigned int lp_rates[] = { 84static unsigned int lp_rates[] = {
153 8000, 85 8000,
@@ -174,53 +106,33 @@ static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
174 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, }, 106 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
175}; 107};
176 108
177/* 109static unsigned int twl6040_read(struct snd_soc_codec *codec, unsigned int reg)
178 * read twl6040 register cache
179 */
180static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
181 unsigned int reg)
182{
183 u8 *cache = codec->reg_cache;
184
185 if (reg >= TWL6040_CACHEREGNUM)
186 return -EIO;
187
188 return cache[reg];
189}
190
191/*
192 * write twl6040 register cache
193 */
194static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
195 u8 reg, u8 value)
196{
197 u8 *cache = codec->reg_cache;
198
199 if (reg >= TWL6040_CACHEREGNUM)
200 return;
201 cache[reg] = value;
202}
203
204/*
205 * read from twl6040 hardware register
206 */
207static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
208 unsigned int reg)
209{ 110{
111 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
210 struct twl6040 *twl6040 = codec->control_data; 112 struct twl6040 *twl6040 = codec->control_data;
211 u8 value; 113 u8 value;
212 114
213 if (reg >= TWL6040_CACHEREGNUM) 115 if (reg >= TWL6040_CACHEREGNUM)
214 return -EIO; 116 return -EIO;
215 117
216 value = twl6040_reg_read(twl6040, reg); 118 switch (reg) {
217 twl6040_write_reg_cache(codec, reg, value); 119 case TWL6040_REG_HSLCTL:
120 case TWL6040_REG_HSRCTL:
121 case TWL6040_REG_EARCTL:
122 case TWL6040_REG_HFLCTL:
123 case TWL6040_REG_HFRCTL:
124 value = priv->dl12_cache[reg - TWL6040_REG_HSLCTL];
125 break;
126 default:
127 value = twl6040_reg_read(twl6040, reg);
128 break;
129 }
218 130
219 return value; 131 return value;
220} 132}
221 133
222static bool twl6040_is_path_unmuted(struct snd_soc_codec *codec, 134static bool twl6040_can_write_to_chip(struct snd_soc_codec *codec,
223 unsigned int reg) 135 unsigned int reg)
224{ 136{
225 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 137 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
226 138
@@ -238,9 +150,24 @@ static bool twl6040_is_path_unmuted(struct snd_soc_codec *codec,
238 } 150 }
239} 151}
240 152
241/* 153static inline void twl6040_update_dl12_cache(struct snd_soc_codec *codec,
242 * write to the twl6040 register space 154 u8 reg, u8 value)
243 */ 155{
156 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
157
158 switch (reg) {
159 case TWL6040_REG_HSLCTL:
160 case TWL6040_REG_HSRCTL:
161 case TWL6040_REG_EARCTL:
162 case TWL6040_REG_HFLCTL:
163 case TWL6040_REG_HFRCTL:
164 priv->dl12_cache[reg - TWL6040_REG_HSLCTL] = value;
165 break;
166 default:
167 break;
168 }
169}
170
244static int twl6040_write(struct snd_soc_codec *codec, 171static int twl6040_write(struct snd_soc_codec *codec,
245 unsigned int reg, unsigned int value) 172 unsigned int reg, unsigned int value)
246{ 173{
@@ -249,8 +176,8 @@ static int twl6040_write(struct snd_soc_codec *codec,
249 if (reg >= TWL6040_CACHEREGNUM) 176 if (reg >= TWL6040_CACHEREGNUM)
250 return -EIO; 177 return -EIO;
251 178
252 twl6040_write_reg_cache(codec, reg, value); 179 twl6040_update_dl12_cache(codec, reg, value);
253 if (twl6040_is_path_unmuted(codec, reg)) 180 if (twl6040_can_write_to_chip(codec, reg))
254 return twl6040_reg_write(twl6040, reg, value); 181 return twl6040_reg_write(twl6040, reg, value);
255 else 182 else
256 return 0; 183 return 0;
@@ -258,45 +185,27 @@ static int twl6040_write(struct snd_soc_codec *codec,
258 185
259static void twl6040_init_chip(struct snd_soc_codec *codec) 186static void twl6040_init_chip(struct snd_soc_codec *codec)
260{ 187{
261 struct twl6040 *twl6040 = codec->control_data; 188 twl6040_read(codec, TWL6040_REG_TRIM1);
262 u8 val; 189 twl6040_read(codec, TWL6040_REG_TRIM2);
263 190 twl6040_read(codec, TWL6040_REG_TRIM3);
264 /* Update reg_cache: ASICREV, and TRIM values */ 191 twl6040_read(codec, TWL6040_REG_HSOTRIM);
265 val = twl6040_get_revid(twl6040); 192 twl6040_read(codec, TWL6040_REG_HFOTRIM);
266 twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
267
268 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM1);
269 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM2);
270 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM3);
271 twl6040_read_reg_volatile(codec, TWL6040_REG_HSOTRIM);
272 twl6040_read_reg_volatile(codec, TWL6040_REG_HFOTRIM);
273 193
274 /* Change chip defaults */ 194 /* Change chip defaults */
275 /* No imput selected for microphone amplifiers */ 195 /* No imput selected for microphone amplifiers */
276 twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18); 196 twl6040_write(codec, TWL6040_REG_MICLCTL, 0x18);
277 twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18); 197 twl6040_write(codec, TWL6040_REG_MICRCTL, 0x18);
278 198
279 /* 199 /*
280 * We need to lower the default gain values, so the ramp code 200 * We need to lower the default gain values, so the ramp code
281 * can work correctly for the first playback. 201 * can work correctly for the first playback.
282 * This reduces the pop noise heard at the first playback. 202 * This reduces the pop noise heard at the first playback.
283 */ 203 */
284 twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff); 204 twl6040_write(codec, TWL6040_REG_HSGAIN, 0xff);
285 twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e); 205 twl6040_write(codec, TWL6040_REG_EARCTL, 0x1e);
286 twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d); 206 twl6040_write(codec, TWL6040_REG_HFLGAIN, 0x1d);
287 twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d); 207 twl6040_write(codec, TWL6040_REG_HFRGAIN, 0x1d);
288 twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0); 208 twl6040_write(codec, TWL6040_REG_LINEGAIN, 0);
289}
290
291static void twl6040_restore_regs(struct snd_soc_codec *codec)
292{
293 u8 *cache = codec->reg_cache;
294 int reg, i;
295
296 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
297 reg = twl6040_restore_list[i];
298 twl6040_write(codec, reg, cache[reg]);
299 }
300} 209}
301 210
302/* set headset dac and driver power mode */ 211/* set headset dac and driver power mode */
@@ -305,8 +214,8 @@ static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
305 int hslctl, hsrctl; 214 int hslctl, hsrctl;
306 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE; 215 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
307 216
308 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); 217 hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
309 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); 218 hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
310 219
311 if (high_perf) { 220 if (high_perf) {
312 hslctl &= ~mask; 221 hslctl &= ~mask;
@@ -333,8 +242,8 @@ static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
333 * Both HS DAC need to be turned on (before the HS driver) and off at 242 * Both HS DAC need to be turned on (before the HS driver) and off at
334 * the same time. 243 * the same time.
335 */ 244 */
336 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); 245 hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
337 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); 246 hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
338 if (SND_SOC_DAPM_EVENT_ON(event)) { 247 if (SND_SOC_DAPM_EVENT_ON(event)) {
339 hslctl |= TWL6040_HSDACENA; 248 hslctl |= TWL6040_HSDACENA;
340 hsrctl |= TWL6040_HSDACENA; 249 hsrctl |= TWL6040_HSDACENA;
@@ -379,7 +288,7 @@ static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
379 mutex_lock(&priv->mutex); 288 mutex_lock(&priv->mutex);
380 289
381 /* Sync status */ 290 /* Sync status */
382 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS); 291 status = twl6040_read(codec, TWL6040_REG_STATUS);
383 if (status & TWL6040_PLUGCOMP) 292 if (status & TWL6040_PLUGCOMP)
384 snd_soc_jack_report(jack, report, report); 293 snd_soc_jack_report(jack, report, report);
385 else 294 else
@@ -431,7 +340,7 @@ static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
431 unsigned int val; 340 unsigned int val;
432 341
433 /* Do not allow changes while Input/FF efect is running */ 342 /* Do not allow changes while Input/FF efect is running */
434 val = twl6040_read_reg_volatile(codec, e->reg); 343 val = twl6040_read(codec, e->reg);
435 if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL)) 344 if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
436 return -EBUSY; 345 return -EBUSY;
437 346
@@ -656,7 +565,7 @@ int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
656 if (unlikely(trim >= TWL6040_TRIM_INVAL)) 565 if (unlikely(trim >= TWL6040_TRIM_INVAL))
657 return -EINVAL; 566 return -EINVAL;
658 567
659 return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim); 568 return twl6040_read(codec, TWL6040_REG_TRIM1 + trim);
660} 569}
661EXPORT_SYMBOL_GPL(twl6040_get_trim_value); 570EXPORT_SYMBOL_GPL(twl6040_get_trim_value);
662 571
@@ -931,8 +840,6 @@ static int twl6040_set_bias_level(struct snd_soc_codec *codec,
931 840
932 priv->codec_powered = 1; 841 priv->codec_powered = 1;
933 842
934 twl6040_restore_regs(codec);
935
936 /* Set external boost GPO */ 843 /* Set external boost GPO */
937 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02); 844 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
938 break; 845 break;
@@ -1053,9 +960,9 @@ static void twl6040_mute_path(struct snd_soc_codec *codec, enum twl6040_dai_id i
1053 960
1054 switch (id) { 961 switch (id) {
1055 case TWL6040_DAI_DL1: 962 case TWL6040_DAI_DL1:
1056 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); 963 hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
1057 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); 964 hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
1058 earctl = twl6040_read_reg_cache(codec, TWL6040_REG_EARCTL); 965 earctl = twl6040_read(codec, TWL6040_REG_EARCTL);
1059 966
1060 if (mute) { 967 if (mute) {
1061 /* Power down drivers and DACs */ 968 /* Power down drivers and DACs */
@@ -1071,8 +978,8 @@ static void twl6040_mute_path(struct snd_soc_codec *codec, enum twl6040_dai_id i
1071 priv->dl1_unmuted = !mute; 978 priv->dl1_unmuted = !mute;
1072 break; 979 break;
1073 case TWL6040_DAI_DL2: 980 case TWL6040_DAI_DL2:
1074 hflctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFLCTL); 981 hflctl = twl6040_read(codec, TWL6040_REG_HFLCTL);
1075 hfrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFRCTL); 982 hfrctl = twl6040_read(codec, TWL6040_REG_HFRCTL);
1076 983
1077 if (mute) { 984 if (mute) {
1078 /* Power down drivers and DACs */ 985 /* Power down drivers and DACs */
@@ -1209,6 +1116,7 @@ static int twl6040_resume(struct snd_soc_codec *codec)
1209static int twl6040_probe(struct snd_soc_codec *codec) 1116static int twl6040_probe(struct snd_soc_codec *codec)
1210{ 1117{
1211 struct twl6040_data *priv; 1118 struct twl6040_data *priv;
1119 struct twl6040 *twl6040 = dev_get_drvdata(codec->dev->parent);
1212 struct platform_device *pdev = container_of(codec->dev, 1120 struct platform_device *pdev = container_of(codec->dev,
1213 struct platform_device, dev); 1121 struct platform_device, dev);
1214 int ret = 0; 1122 int ret = 0;
@@ -1220,7 +1128,7 @@ static int twl6040_probe(struct snd_soc_codec *codec)
1220 snd_soc_codec_set_drvdata(codec, priv); 1128 snd_soc_codec_set_drvdata(codec, priv);
1221 1129
1222 priv->codec = codec; 1130 priv->codec = codec;
1223 codec->control_data = dev_get_drvdata(codec->dev->parent); 1131 codec->control_data = twl6040;
1224 1132
1225 priv->plug_irq = platform_get_irq(pdev, 0); 1133 priv->plug_irq = platform_get_irq(pdev, 0);
1226 if (priv->plug_irq < 0) { 1134 if (priv->plug_irq < 0) {
@@ -1240,10 +1148,10 @@ static int twl6040_probe(struct snd_soc_codec *codec)
1240 return ret; 1148 return ret;
1241 } 1149 }
1242 1150
1151 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1243 twl6040_init_chip(codec); 1152 twl6040_init_chip(codec);
1244 1153
1245 /* power on device */ 1154 return 0;
1246 return twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1247} 1155}
1248 1156
1249static int twl6040_remove(struct snd_soc_codec *codec) 1157static int twl6040_remove(struct snd_soc_codec *codec)
@@ -1261,12 +1169,9 @@ static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1261 .remove = twl6040_remove, 1169 .remove = twl6040_remove,
1262 .suspend = twl6040_suspend, 1170 .suspend = twl6040_suspend,
1263 .resume = twl6040_resume, 1171 .resume = twl6040_resume,
1264 .read = twl6040_read_reg_cache, 1172 .read = twl6040_read,
1265 .write = twl6040_write, 1173 .write = twl6040_write,
1266 .set_bias_level = twl6040_set_bias_level, 1174 .set_bias_level = twl6040_set_bias_level,
1267 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1268 .reg_word_size = sizeof(u8),
1269 .reg_cache_default = twl6040_reg,
1270 .ignore_pmdown_time = true, 1175 .ignore_pmdown_time = true,
1271 1176
1272 .controls = twl6040_snd_controls, 1177 .controls = twl6040_snd_controls,