Commit | Line | Data |
---|---|---|
cc17557e SS |
1 | /* |
2 | * ALSA SoC TWL4030 codec driver | |
3 | * | |
4 | * Author: Steve Sakoman, <steve@sakoman.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * version 2 as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/moduleparam.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/delay.h> | |
26 | #include <linux/pm.h> | |
27 | #include <linux/i2c.h> | |
28 | #include <linux/platform_device.h> | |
29 | #include <linux/i2c/twl4030.h> | |
30 | #include <sound/core.h> | |
31 | #include <sound/pcm.h> | |
32 | #include <sound/pcm_params.h> | |
33 | #include <sound/soc.h> | |
34 | #include <sound/soc-dapm.h> | |
35 | #include <sound/initval.h> | |
36 | ||
37 | #include "twl4030.h" | |
38 | ||
39 | /* | |
40 | * twl4030 register cache & default register settings | |
41 | */ | |
42 | static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = { | |
43 | 0x00, /* this register not used */ | |
44 | 0x93, /* REG_CODEC_MODE (0x1) */ | |
45 | 0xc3, /* REG_OPTION (0x2) */ | |
46 | 0x00, /* REG_UNKNOWN (0x3) */ | |
47 | 0x00, /* REG_MICBIAS_CTL (0x4) */ | |
48 | 0x24, /* REG_ANAMICL (0x5) */ | |
49 | 0x04, /* REG_ANAMICR (0x6) */ | |
50 | 0x0a, /* REG_AVADC_CTL (0x7) */ | |
51 | 0x00, /* REG_ADCMICSEL (0x8) */ | |
52 | 0x00, /* REG_DIGMIXING (0x9) */ | |
53 | 0x0c, /* REG_ATXL1PGA (0xA) */ | |
54 | 0x0c, /* REG_ATXR1PGA (0xB) */ | |
55 | 0x00, /* REG_AVTXL2PGA (0xC) */ | |
56 | 0x00, /* REG_AVTXR2PGA (0xD) */ | |
57 | 0x01, /* REG_AUDIO_IF (0xE) */ | |
58 | 0x00, /* REG_VOICE_IF (0xF) */ | |
59 | 0x00, /* REG_ARXR1PGA (0x10) */ | |
60 | 0x00, /* REG_ARXL1PGA (0x11) */ | |
61 | 0x6c, /* REG_ARXR2PGA (0x12) */ | |
62 | 0x6c, /* REG_ARXL2PGA (0x13) */ | |
63 | 0x00, /* REG_VRXPGA (0x14) */ | |
64 | 0x00, /* REG_VSTPGA (0x15) */ | |
65 | 0x00, /* REG_VRX2ARXPGA (0x16) */ | |
66 | 0x0c, /* REG_AVDAC_CTL (0x17) */ | |
67 | 0x00, /* REG_ARX2VTXPGA (0x18) */ | |
68 | 0x00, /* REG_ARXL1_APGA_CTL (0x19) */ | |
69 | 0x00, /* REG_ARXR1_APGA_CTL (0x1A) */ | |
70 | 0x4b, /* REG_ARXL2_APGA_CTL (0x1B) */ | |
71 | 0x4b, /* REG_ARXR2_APGA_CTL (0x1C) */ | |
72 | 0x00, /* REG_ATX2ARXPGA (0x1D) */ | |
73 | 0x00, /* REG_BT_IF (0x1E) */ | |
74 | 0x00, /* REG_BTPGA (0x1F) */ | |
75 | 0x00, /* REG_BTSTPGA (0x20) */ | |
76 | 0x00, /* REG_EAR_CTL (0x21) */ | |
77 | 0x24, /* REG_HS_SEL (0x22) */ | |
78 | 0x0a, /* REG_HS_GAIN_SET (0x23) */ | |
79 | 0x00, /* REG_HS_POPN_SET (0x24) */ | |
80 | 0x00, /* REG_PREDL_CTL (0x25) */ | |
81 | 0x00, /* REG_PREDR_CTL (0x26) */ | |
82 | 0x00, /* REG_PRECKL_CTL (0x27) */ | |
83 | 0x00, /* REG_PRECKR_CTL (0x28) */ | |
84 | 0x00, /* REG_HFL_CTL (0x29) */ | |
85 | 0x00, /* REG_HFR_CTL (0x2A) */ | |
86 | 0x00, /* REG_ALC_CTL (0x2B) */ | |
87 | 0x00, /* REG_ALC_SET1 (0x2C) */ | |
88 | 0x00, /* REG_ALC_SET2 (0x2D) */ | |
89 | 0x00, /* REG_BOOST_CTL (0x2E) */ | |
f8d05bdb | 90 | 0x00, /* REG_SOFTVOL_CTL (0x2F) */ |
cc17557e SS |
91 | 0x00, /* REG_DTMF_FREQSEL (0x30) */ |
92 | 0x00, /* REG_DTMF_TONEXT1H (0x31) */ | |
93 | 0x00, /* REG_DTMF_TONEXT1L (0x32) */ | |
94 | 0x00, /* REG_DTMF_TONEXT2H (0x33) */ | |
95 | 0x00, /* REG_DTMF_TONEXT2L (0x34) */ | |
96 | 0x00, /* REG_DTMF_TONOFF (0x35) */ | |
97 | 0x00, /* REG_DTMF_WANONOFF (0x36) */ | |
98 | 0x00, /* REG_I2S_RX_SCRAMBLE_H (0x37) */ | |
99 | 0x00, /* REG_I2S_RX_SCRAMBLE_M (0x38) */ | |
100 | 0x00, /* REG_I2S_RX_SCRAMBLE_L (0x39) */ | |
101 | 0x16, /* REG_APLL_CTL (0x3A) */ | |
102 | 0x00, /* REG_DTMF_CTL (0x3B) */ | |
103 | 0x00, /* REG_DTMF_PGA_CTL2 (0x3C) */ | |
104 | 0x00, /* REG_DTMF_PGA_CTL1 (0x3D) */ | |
105 | 0x00, /* REG_MISC_SET_1 (0x3E) */ | |
106 | 0x00, /* REG_PCMBTMUX (0x3F) */ | |
107 | 0x00, /* not used (0x40) */ | |
108 | 0x00, /* not used (0x41) */ | |
109 | 0x00, /* not used (0x42) */ | |
110 | 0x00, /* REG_RX_PATH_SEL (0x43) */ | |
111 | 0x00, /* REG_VDL_APGA_CTL (0x44) */ | |
112 | 0x00, /* REG_VIBRA_CTL (0x45) */ | |
113 | 0x00, /* REG_VIBRA_SET (0x46) */ | |
114 | 0x00, /* REG_VIBRA_PWM_SET (0x47) */ | |
115 | 0x00, /* REG_ANAMIC_GAIN (0x48) */ | |
116 | 0x00, /* REG_MISC_SET_2 (0x49) */ | |
117 | }; | |
118 | ||
119 | /* | |
120 | * read twl4030 register cache | |
121 | */ | |
122 | static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec, | |
123 | unsigned int reg) | |
124 | { | |
125 | u8 *cache = codec->reg_cache; | |
126 | ||
127 | return cache[reg]; | |
128 | } | |
129 | ||
130 | /* | |
131 | * write twl4030 register cache | |
132 | */ | |
133 | static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec, | |
134 | u8 reg, u8 value) | |
135 | { | |
136 | u8 *cache = codec->reg_cache; | |
137 | ||
138 | if (reg >= TWL4030_CACHEREGNUM) | |
139 | return; | |
140 | cache[reg] = value; | |
141 | } | |
142 | ||
143 | /* | |
144 | * write to the twl4030 register space | |
145 | */ | |
146 | static int twl4030_write(struct snd_soc_codec *codec, | |
147 | unsigned int reg, unsigned int value) | |
148 | { | |
149 | twl4030_write_reg_cache(codec, reg, value); | |
150 | return twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value, reg); | |
151 | } | |
152 | ||
153 | static void twl4030_clear_codecpdz(struct snd_soc_codec *codec) | |
154 | { | |
155 | u8 mode; | |
156 | ||
157 | mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE); | |
158 | twl4030_write(codec, TWL4030_REG_CODEC_MODE, | |
159 | mode & ~TWL4030_CODECPDZ); | |
160 | ||
161 | /* REVISIT: this delay is present in TI sample drivers */ | |
162 | /* but there seems to be no TRM requirement for it */ | |
163 | udelay(10); | |
164 | } | |
165 | ||
166 | static void twl4030_set_codecpdz(struct snd_soc_codec *codec) | |
167 | { | |
168 | u8 mode; | |
169 | ||
170 | mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE); | |
171 | twl4030_write(codec, TWL4030_REG_CODEC_MODE, | |
172 | mode | TWL4030_CODECPDZ); | |
173 | ||
174 | /* REVISIT: this delay is present in TI sample drivers */ | |
175 | /* but there seems to be no TRM requirement for it */ | |
176 | udelay(10); | |
177 | } | |
178 | ||
179 | static void twl4030_init_chip(struct snd_soc_codec *codec) | |
180 | { | |
181 | int i; | |
182 | ||
183 | /* clear CODECPDZ prior to setting register defaults */ | |
184 | twl4030_clear_codecpdz(codec); | |
185 | ||
186 | /* set all audio section registers to reasonable defaults */ | |
187 | for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++) | |
188 | twl4030_write(codec, i, twl4030_reg[i]); | |
189 | ||
190 | } | |
191 | ||
192 | static const struct snd_kcontrol_new twl4030_snd_controls[] = { | |
193 | SOC_DOUBLE_R("Master Playback Volume", | |
194 | TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA, | |
6e5d9db2 | 195 | 0, 0x3f, 0), |
cc17557e SS |
196 | SOC_DOUBLE_R("Capture Volume", |
197 | TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA, | |
6e5d9db2 | 198 | 0, 0x1f, 0), |
cc17557e SS |
199 | }; |
200 | ||
201 | /* add non dapm controls */ | |
202 | static int twl4030_add_controls(struct snd_soc_codec *codec) | |
203 | { | |
204 | int err, i; | |
205 | ||
206 | for (i = 0; i < ARRAY_SIZE(twl4030_snd_controls); i++) { | |
207 | err = snd_ctl_add(codec->card, | |
208 | snd_soc_cnew(&twl4030_snd_controls[i], | |
209 | codec, NULL)); | |
210 | if (err < 0) | |
211 | return err; | |
212 | } | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = { | |
218 | SND_SOC_DAPM_INPUT("INL"), | |
219 | SND_SOC_DAPM_INPUT("INR"), | |
220 | ||
221 | SND_SOC_DAPM_OUTPUT("OUTL"), | |
222 | SND_SOC_DAPM_OUTPUT("OUTR"), | |
223 | ||
224 | SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM, 0, 0), | |
225 | SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM, 0, 0), | |
226 | ||
227 | SND_SOC_DAPM_ADC("ADCL", "Left Capture", SND_SOC_NOPM, 0, 0), | |
228 | SND_SOC_DAPM_ADC("ADCR", "Right Capture", SND_SOC_NOPM, 0, 0), | |
229 | }; | |
230 | ||
231 | static const struct snd_soc_dapm_route intercon[] = { | |
232 | /* outputs */ | |
233 | {"OUTL", NULL, "DACL"}, | |
234 | {"OUTR", NULL, "DACR"}, | |
235 | ||
236 | /* inputs */ | |
237 | {"ADCL", NULL, "INL"}, | |
238 | {"ADCR", NULL, "INR"}, | |
239 | }; | |
240 | ||
241 | static int twl4030_add_widgets(struct snd_soc_codec *codec) | |
242 | { | |
243 | snd_soc_dapm_new_controls(codec, twl4030_dapm_widgets, | |
244 | ARRAY_SIZE(twl4030_dapm_widgets)); | |
245 | ||
246 | snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); | |
247 | ||
248 | snd_soc_dapm_new_widgets(codec); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | static void twl4030_power_up(struct snd_soc_codec *codec) | |
253 | { | |
254 | u8 anamicl, regmisc1, byte, popn, hsgain; | |
255 | int i = 0; | |
256 | ||
257 | /* set CODECPDZ to turn on codec */ | |
258 | twl4030_set_codecpdz(codec); | |
259 | ||
260 | /* initiate offset cancellation */ | |
261 | anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL); | |
262 | twl4030_write(codec, TWL4030_REG_ANAMICL, | |
263 | anamicl | TWL4030_CNCL_OFFSET_START); | |
264 | ||
265 | /* wait for offset cancellation to complete */ | |
266 | do { | |
267 | /* this takes a little while, so don't slam i2c */ | |
268 | udelay(2000); | |
269 | twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte, | |
270 | TWL4030_REG_ANAMICL); | |
271 | } while ((i++ < 100) && | |
272 | ((byte & TWL4030_CNCL_OFFSET_START) == | |
273 | TWL4030_CNCL_OFFSET_START)); | |
274 | ||
275 | /* anti-pop when changing analog gain */ | |
276 | regmisc1 = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1); | |
277 | twl4030_write(codec, TWL4030_REG_MISC_SET_1, | |
278 | regmisc1 | TWL4030_SMOOTH_ANAVOL_EN); | |
279 | ||
280 | /* toggle CODECPDZ as per TRM */ | |
281 | twl4030_clear_codecpdz(codec); | |
282 | twl4030_set_codecpdz(codec); | |
283 | ||
284 | /* program anti-pop with bias ramp delay */ | |
285 | popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET); | |
286 | popn &= TWL4030_RAMP_DELAY; | |
287 | popn |= TWL4030_RAMP_DELAY_645MS; | |
288 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); | |
289 | popn |= TWL4030_VMID_EN; | |
290 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); | |
291 | ||
292 | /* enable output stage and gain setting */ | |
293 | hsgain = TWL4030_HSR_GAIN_0DB | TWL4030_HSL_GAIN_0DB; | |
294 | twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain); | |
295 | ||
296 | /* enable anti-pop ramp */ | |
297 | popn |= TWL4030_RAMP_EN; | |
298 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); | |
299 | } | |
300 | ||
301 | static void twl4030_power_down(struct snd_soc_codec *codec) | |
302 | { | |
303 | u8 popn, hsgain; | |
304 | ||
305 | /* disable anti-pop ramp */ | |
306 | popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET); | |
307 | popn &= ~TWL4030_RAMP_EN; | |
308 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); | |
309 | ||
310 | /* disable output stage and gain setting */ | |
311 | hsgain = TWL4030_HSR_GAIN_PWR_DOWN | TWL4030_HSL_GAIN_PWR_DOWN; | |
312 | twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain); | |
313 | ||
314 | /* disable bias out */ | |
315 | popn &= ~TWL4030_VMID_EN; | |
316 | twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn); | |
317 | ||
318 | /* power down */ | |
319 | twl4030_clear_codecpdz(codec); | |
320 | } | |
321 | ||
322 | static int twl4030_set_bias_level(struct snd_soc_codec *codec, | |
323 | enum snd_soc_bias_level level) | |
324 | { | |
325 | switch (level) { | |
326 | case SND_SOC_BIAS_ON: | |
327 | twl4030_power_up(codec); | |
328 | break; | |
329 | case SND_SOC_BIAS_PREPARE: | |
330 | /* TODO: develop a twl4030_prepare function */ | |
331 | break; | |
332 | case SND_SOC_BIAS_STANDBY: | |
333 | /* TODO: develop a twl4030_standby function */ | |
334 | twl4030_power_down(codec); | |
335 | break; | |
336 | case SND_SOC_BIAS_OFF: | |
337 | twl4030_power_down(codec); | |
338 | break; | |
339 | } | |
340 | codec->bias_level = level; | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | static int twl4030_hw_params(struct snd_pcm_substream *substream, | |
dee89c4d MB |
346 | struct snd_pcm_hw_params *params, |
347 | struct snd_soc_dai *dai) | |
cc17557e SS |
348 | { |
349 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
350 | struct snd_soc_device *socdev = rtd->socdev; | |
351 | struct snd_soc_codec *codec = socdev->codec; | |
352 | u8 mode, old_mode, format, old_format; | |
353 | ||
354 | ||
355 | /* bit rate */ | |
356 | old_mode = twl4030_read_reg_cache(codec, | |
357 | TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ; | |
358 | mode = old_mode & ~TWL4030_APLL_RATE; | |
359 | ||
360 | switch (params_rate(params)) { | |
361 | case 8000: | |
362 | mode |= TWL4030_APLL_RATE_8000; | |
363 | break; | |
364 | case 11025: | |
365 | mode |= TWL4030_APLL_RATE_11025; | |
366 | break; | |
367 | case 12000: | |
368 | mode |= TWL4030_APLL_RATE_12000; | |
369 | break; | |
370 | case 16000: | |
371 | mode |= TWL4030_APLL_RATE_16000; | |
372 | break; | |
373 | case 22050: | |
374 | mode |= TWL4030_APLL_RATE_22050; | |
375 | break; | |
376 | case 24000: | |
377 | mode |= TWL4030_APLL_RATE_24000; | |
378 | break; | |
379 | case 32000: | |
380 | mode |= TWL4030_APLL_RATE_32000; | |
381 | break; | |
382 | case 44100: | |
383 | mode |= TWL4030_APLL_RATE_44100; | |
384 | break; | |
385 | case 48000: | |
386 | mode |= TWL4030_APLL_RATE_48000; | |
387 | break; | |
388 | default: | |
389 | printk(KERN_ERR "TWL4030 hw params: unknown rate %d\n", | |
390 | params_rate(params)); | |
391 | return -EINVAL; | |
392 | } | |
393 | ||
394 | if (mode != old_mode) { | |
395 | /* change rate and set CODECPDZ */ | |
396 | twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode); | |
397 | twl4030_set_codecpdz(codec); | |
398 | } | |
399 | ||
400 | /* sample size */ | |
401 | old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF); | |
402 | format = old_format; | |
403 | format &= ~TWL4030_DATA_WIDTH; | |
404 | switch (params_format(params)) { | |
405 | case SNDRV_PCM_FORMAT_S16_LE: | |
406 | format |= TWL4030_DATA_WIDTH_16S_16W; | |
407 | break; | |
408 | case SNDRV_PCM_FORMAT_S24_LE: | |
409 | format |= TWL4030_DATA_WIDTH_32S_24W; | |
410 | break; | |
411 | default: | |
412 | printk(KERN_ERR "TWL4030 hw params: unknown format %d\n", | |
413 | params_format(params)); | |
414 | return -EINVAL; | |
415 | } | |
416 | ||
417 | if (format != old_format) { | |
418 | ||
419 | /* clear CODECPDZ before changing format (codec requirement) */ | |
420 | twl4030_clear_codecpdz(codec); | |
421 | ||
422 | /* change format */ | |
423 | twl4030_write(codec, TWL4030_REG_AUDIO_IF, format); | |
424 | ||
425 | /* set CODECPDZ afterwards */ | |
426 | twl4030_set_codecpdz(codec); | |
427 | } | |
428 | return 0; | |
429 | } | |
430 | ||
431 | static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai, | |
432 | int clk_id, unsigned int freq, int dir) | |
433 | { | |
434 | struct snd_soc_codec *codec = codec_dai->codec; | |
435 | u8 infreq; | |
436 | ||
437 | switch (freq) { | |
438 | case 19200000: | |
439 | infreq = TWL4030_APLL_INFREQ_19200KHZ; | |
440 | break; | |
441 | case 26000000: | |
442 | infreq = TWL4030_APLL_INFREQ_26000KHZ; | |
443 | break; | |
444 | case 38400000: | |
445 | infreq = TWL4030_APLL_INFREQ_38400KHZ; | |
446 | break; | |
447 | default: | |
448 | printk(KERN_ERR "TWL4030 set sysclk: unknown rate %d\n", | |
449 | freq); | |
450 | return -EINVAL; | |
451 | } | |
452 | ||
453 | infreq |= TWL4030_APLL_EN; | |
454 | twl4030_write(codec, TWL4030_REG_APLL_CTL, infreq); | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
459 | static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
460 | unsigned int fmt) | |
461 | { | |
462 | struct snd_soc_codec *codec = codec_dai->codec; | |
463 | u8 old_format, format; | |
464 | ||
465 | /* get format */ | |
466 | old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF); | |
467 | format = old_format; | |
468 | ||
469 | /* set master/slave audio interface */ | |
470 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
471 | case SND_SOC_DAIFMT_CBM_CFM: | |
472 | format &= ~(TWL4030_AIF_SLAVE_EN); | |
e18c94d2 | 473 | format &= ~(TWL4030_CLK256FS_EN); |
cc17557e SS |
474 | break; |
475 | case SND_SOC_DAIFMT_CBS_CFS: | |
cc17557e | 476 | format |= TWL4030_AIF_SLAVE_EN; |
e18c94d2 | 477 | format |= TWL4030_CLK256FS_EN; |
cc17557e SS |
478 | break; |
479 | default: | |
480 | return -EINVAL; | |
481 | } | |
482 | ||
483 | /* interface format */ | |
484 | format &= ~TWL4030_AIF_FORMAT; | |
485 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
486 | case SND_SOC_DAIFMT_I2S: | |
487 | format |= TWL4030_AIF_FORMAT_CODEC; | |
488 | break; | |
489 | default: | |
490 | return -EINVAL; | |
491 | } | |
492 | ||
493 | if (format != old_format) { | |
494 | ||
495 | /* clear CODECPDZ before changing format (codec requirement) */ | |
496 | twl4030_clear_codecpdz(codec); | |
497 | ||
498 | /* change format */ | |
499 | twl4030_write(codec, TWL4030_REG_AUDIO_IF, format); | |
500 | ||
501 | /* set CODECPDZ afterwards */ | |
502 | twl4030_set_codecpdz(codec); | |
503 | } | |
504 | ||
505 | return 0; | |
506 | } | |
507 | ||
bbba9444 | 508 | #define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000) |
cc17557e SS |
509 | #define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE) |
510 | ||
511 | struct snd_soc_dai twl4030_dai = { | |
512 | .name = "twl4030", | |
513 | .playback = { | |
514 | .stream_name = "Playback", | |
515 | .channels_min = 2, | |
516 | .channels_max = 2, | |
517 | .rates = TWL4030_RATES, | |
518 | .formats = TWL4030_FORMATS,}, | |
519 | .capture = { | |
520 | .stream_name = "Capture", | |
521 | .channels_min = 2, | |
522 | .channels_max = 2, | |
523 | .rates = TWL4030_RATES, | |
524 | .formats = TWL4030_FORMATS,}, | |
525 | .ops = { | |
526 | .hw_params = twl4030_hw_params, | |
cc17557e SS |
527 | .set_sysclk = twl4030_set_dai_sysclk, |
528 | .set_fmt = twl4030_set_dai_fmt, | |
529 | } | |
530 | }; | |
531 | EXPORT_SYMBOL_GPL(twl4030_dai); | |
532 | ||
533 | static int twl4030_suspend(struct platform_device *pdev, pm_message_t state) | |
534 | { | |
535 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
536 | struct snd_soc_codec *codec = socdev->codec; | |
537 | ||
538 | twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF); | |
539 | ||
540 | return 0; | |
541 | } | |
542 | ||
543 | static int twl4030_resume(struct platform_device *pdev) | |
544 | { | |
545 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
546 | struct snd_soc_codec *codec = socdev->codec; | |
547 | ||
548 | twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY); | |
549 | twl4030_set_bias_level(codec, codec->suspend_bias_level); | |
550 | return 0; | |
551 | } | |
552 | ||
553 | /* | |
554 | * initialize the driver | |
555 | * register the mixer and dsp interfaces with the kernel | |
556 | */ | |
557 | ||
558 | static int twl4030_init(struct snd_soc_device *socdev) | |
559 | { | |
560 | struct snd_soc_codec *codec = socdev->codec; | |
561 | int ret = 0; | |
562 | ||
563 | printk(KERN_INFO "TWL4030 Audio Codec init \n"); | |
564 | ||
565 | codec->name = "twl4030"; | |
566 | codec->owner = THIS_MODULE; | |
567 | codec->read = twl4030_read_reg_cache; | |
568 | codec->write = twl4030_write; | |
569 | codec->set_bias_level = twl4030_set_bias_level; | |
570 | codec->dai = &twl4030_dai; | |
571 | codec->num_dai = 1; | |
572 | codec->reg_cache_size = sizeof(twl4030_reg); | |
573 | codec->reg_cache = kmemdup(twl4030_reg, sizeof(twl4030_reg), | |
574 | GFP_KERNEL); | |
575 | if (codec->reg_cache == NULL) | |
576 | return -ENOMEM; | |
577 | ||
578 | /* register pcms */ | |
579 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); | |
580 | if (ret < 0) { | |
581 | printk(KERN_ERR "twl4030: failed to create pcms\n"); | |
582 | goto pcm_err; | |
583 | } | |
584 | ||
585 | twl4030_init_chip(codec); | |
586 | ||
587 | /* power on device */ | |
588 | twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY); | |
589 | ||
590 | twl4030_add_controls(codec); | |
591 | twl4030_add_widgets(codec); | |
592 | ||
593 | ret = snd_soc_register_card(socdev); | |
594 | if (ret < 0) { | |
595 | printk(KERN_ERR "twl4030: failed to register card\n"); | |
596 | goto card_err; | |
597 | } | |
598 | ||
599 | return ret; | |
600 | ||
601 | card_err: | |
602 | snd_soc_free_pcms(socdev); | |
603 | snd_soc_dapm_free(socdev); | |
604 | pcm_err: | |
605 | kfree(codec->reg_cache); | |
606 | return ret; | |
607 | } | |
608 | ||
609 | static struct snd_soc_device *twl4030_socdev; | |
610 | ||
611 | static int twl4030_probe(struct platform_device *pdev) | |
612 | { | |
613 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
614 | struct snd_soc_codec *codec; | |
615 | ||
616 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); | |
617 | if (codec == NULL) | |
618 | return -ENOMEM; | |
619 | ||
620 | socdev->codec = codec; | |
621 | mutex_init(&codec->mutex); | |
622 | INIT_LIST_HEAD(&codec->dapm_widgets); | |
623 | INIT_LIST_HEAD(&codec->dapm_paths); | |
624 | ||
625 | twl4030_socdev = socdev; | |
626 | twl4030_init(socdev); | |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
631 | static int twl4030_remove(struct platform_device *pdev) | |
632 | { | |
633 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
634 | struct snd_soc_codec *codec = socdev->codec; | |
635 | ||
636 | printk(KERN_INFO "TWL4030 Audio Codec remove\n"); | |
637 | kfree(codec); | |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
642 | struct snd_soc_codec_device soc_codec_dev_twl4030 = { | |
643 | .probe = twl4030_probe, | |
644 | .remove = twl4030_remove, | |
645 | .suspend = twl4030_suspend, | |
646 | .resume = twl4030_resume, | |
647 | }; | |
648 | EXPORT_SYMBOL_GPL(soc_codec_dev_twl4030); | |
649 | ||
650 | MODULE_DESCRIPTION("ASoC TWL4030 codec driver"); | |
651 | MODULE_AUTHOR("Steve Sakoman"); | |
652 | MODULE_LICENSE("GPL"); |