twl4030.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/pm.h>
  26. #include <linux/i2c.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/i2c/twl4030.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include <sound/soc-dapm.h>
  34. #include <sound/initval.h>
  35. #include <sound/tlv.h>
  36. #include "twl4030.h"
  37. /*
  38. * twl4030 register cache & default register settings
  39. */
  40. static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
  41. 0x00, /* this register not used */
  42. 0x93, /* REG_CODEC_MODE (0x1) */
  43. 0xc3, /* REG_OPTION (0x2) */
  44. 0x00, /* REG_UNKNOWN (0x3) */
  45. 0x00, /* REG_MICBIAS_CTL (0x4) */
  46. 0x20, /* REG_ANAMICL (0x5) */
  47. 0x00, /* REG_ANAMICR (0x6) */
  48. 0x00, /* REG_AVADC_CTL (0x7) */
  49. 0x00, /* REG_ADCMICSEL (0x8) */
  50. 0x00, /* REG_DIGMIXING (0x9) */
  51. 0x0c, /* REG_ATXL1PGA (0xA) */
  52. 0x0c, /* REG_ATXR1PGA (0xB) */
  53. 0x00, /* REG_AVTXL2PGA (0xC) */
  54. 0x00, /* REG_AVTXR2PGA (0xD) */
  55. 0x01, /* REG_AUDIO_IF (0xE) */
  56. 0x00, /* REG_VOICE_IF (0xF) */
  57. 0x00, /* REG_ARXR1PGA (0x10) */
  58. 0x00, /* REG_ARXL1PGA (0x11) */
  59. 0x6c, /* REG_ARXR2PGA (0x12) */
  60. 0x6c, /* REG_ARXL2PGA (0x13) */
  61. 0x00, /* REG_VRXPGA (0x14) */
  62. 0x00, /* REG_VSTPGA (0x15) */
  63. 0x00, /* REG_VRX2ARXPGA (0x16) */
  64. 0x0c, /* REG_AVDAC_CTL (0x17) */
  65. 0x00, /* REG_ARX2VTXPGA (0x18) */
  66. 0x00, /* REG_ARXL1_APGA_CTL (0x19) */
  67. 0x00, /* REG_ARXR1_APGA_CTL (0x1A) */
  68. 0x4b, /* REG_ARXL2_APGA_CTL (0x1B) */
  69. 0x4b, /* REG_ARXR2_APGA_CTL (0x1C) */
  70. 0x00, /* REG_ATX2ARXPGA (0x1D) */
  71. 0x00, /* REG_BT_IF (0x1E) */
  72. 0x00, /* REG_BTPGA (0x1F) */
  73. 0x00, /* REG_BTSTPGA (0x20) */
  74. 0x00, /* REG_EAR_CTL (0x21) */
  75. 0x24, /* REG_HS_SEL (0x22) */
  76. 0x0a, /* REG_HS_GAIN_SET (0x23) */
  77. 0x00, /* REG_HS_POPN_SET (0x24) */
  78. 0x00, /* REG_PREDL_CTL (0x25) */
  79. 0x00, /* REG_PREDR_CTL (0x26) */
  80. 0x00, /* REG_PRECKL_CTL (0x27) */
  81. 0x00, /* REG_PRECKR_CTL (0x28) */
  82. 0x00, /* REG_HFL_CTL (0x29) */
  83. 0x00, /* REG_HFR_CTL (0x2A) */
  84. 0x00, /* REG_ALC_CTL (0x2B) */
  85. 0x00, /* REG_ALC_SET1 (0x2C) */
  86. 0x00, /* REG_ALC_SET2 (0x2D) */
  87. 0x00, /* REG_BOOST_CTL (0x2E) */
  88. 0x00, /* REG_SOFTVOL_CTL (0x2F) */
  89. 0x00, /* REG_DTMF_FREQSEL (0x30) */
  90. 0x00, /* REG_DTMF_TONEXT1H (0x31) */
  91. 0x00, /* REG_DTMF_TONEXT1L (0x32) */
  92. 0x00, /* REG_DTMF_TONEXT2H (0x33) */
  93. 0x00, /* REG_DTMF_TONEXT2L (0x34) */
  94. 0x00, /* REG_DTMF_TONOFF (0x35) */
  95. 0x00, /* REG_DTMF_WANONOFF (0x36) */
  96. 0x00, /* REG_I2S_RX_SCRAMBLE_H (0x37) */
  97. 0x00, /* REG_I2S_RX_SCRAMBLE_M (0x38) */
  98. 0x00, /* REG_I2S_RX_SCRAMBLE_L (0x39) */
  99. 0x16, /* REG_APLL_CTL (0x3A) */
  100. 0x00, /* REG_DTMF_CTL (0x3B) */
  101. 0x00, /* REG_DTMF_PGA_CTL2 (0x3C) */
  102. 0x00, /* REG_DTMF_PGA_CTL1 (0x3D) */
  103. 0x00, /* REG_MISC_SET_1 (0x3E) */
  104. 0x00, /* REG_PCMBTMUX (0x3F) */
  105. 0x00, /* not used (0x40) */
  106. 0x00, /* not used (0x41) */
  107. 0x00, /* not used (0x42) */
  108. 0x00, /* REG_RX_PATH_SEL (0x43) */
  109. 0x00, /* REG_VDL_APGA_CTL (0x44) */
  110. 0x00, /* REG_VIBRA_CTL (0x45) */
  111. 0x00, /* REG_VIBRA_SET (0x46) */
  112. 0x00, /* REG_VIBRA_PWM_SET (0x47) */
  113. 0x00, /* REG_ANAMIC_GAIN (0x48) */
  114. 0x00, /* REG_MISC_SET_2 (0x49) */
  115. };
  116. /*
  117. * read twl4030 register cache
  118. */
  119. static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec,
  120. unsigned int reg)
  121. {
  122. u8 *cache = codec->reg_cache;
  123. return cache[reg];
  124. }
  125. /*
  126. * write twl4030 register cache
  127. */
  128. static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec,
  129. u8 reg, u8 value)
  130. {
  131. u8 *cache = codec->reg_cache;
  132. if (reg >= TWL4030_CACHEREGNUM)
  133. return;
  134. cache[reg] = value;
  135. }
  136. /*
  137. * write to the twl4030 register space
  138. */
  139. static int twl4030_write(struct snd_soc_codec *codec,
  140. unsigned int reg, unsigned int value)
  141. {
  142. twl4030_write_reg_cache(codec, reg, value);
  143. return twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value, reg);
  144. }
  145. static void twl4030_clear_codecpdz(struct snd_soc_codec *codec)
  146. {
  147. u8 mode;
  148. mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
  149. twl4030_write(codec, TWL4030_REG_CODEC_MODE,
  150. mode & ~TWL4030_CODECPDZ);
  151. /* REVISIT: this delay is present in TI sample drivers */
  152. /* but there seems to be no TRM requirement for it */
  153. udelay(10);
  154. }
  155. static void twl4030_set_codecpdz(struct snd_soc_codec *codec)
  156. {
  157. u8 mode;
  158. mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
  159. twl4030_write(codec, TWL4030_REG_CODEC_MODE,
  160. mode | TWL4030_CODECPDZ);
  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. static void twl4030_init_chip(struct snd_soc_codec *codec)
  166. {
  167. int i;
  168. /* clear CODECPDZ prior to setting register defaults */
  169. twl4030_clear_codecpdz(codec);
  170. /* set all audio section registers to reasonable defaults */
  171. for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++)
  172. twl4030_write(codec, i, twl4030_reg[i]);
  173. }
  174. /*
  175. * Some of the gain controls in TWL (mostly those which are associated with
  176. * the outputs) are implemented in an interesting way:
  177. * 0x0 : Power down (mute)
  178. * 0x1 : 6dB
  179. * 0x2 : 0 dB
  180. * 0x3 : -6 dB
  181. * Inverting not going to help with these.
  182. * Custom volsw and volsw_2r get/put functions to handle these gain bits.
  183. */
  184. #define SOC_DOUBLE_TLV_TWL4030(xname, xreg, shift_left, shift_right, xmax,\
  185. xinvert, tlv_array) \
  186. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
  187. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  188. SNDRV_CTL_ELEM_ACCESS_READWRITE,\
  189. .tlv.p = (tlv_array), \
  190. .info = snd_soc_info_volsw, \
  191. .get = snd_soc_get_volsw_twl4030, \
  192. .put = snd_soc_put_volsw_twl4030, \
  193. .private_value = (unsigned long)&(struct soc_mixer_control) \
  194. {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
  195. .max = xmax, .invert = xinvert} }
  196. #define SOC_DOUBLE_R_TLV_TWL4030(xname, reg_left, reg_right, xshift, xmax,\
  197. xinvert, tlv_array) \
  198. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
  199. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  200. SNDRV_CTL_ELEM_ACCESS_READWRITE,\
  201. .tlv.p = (tlv_array), \
  202. .info = snd_soc_info_volsw_2r, \
  203. .get = snd_soc_get_volsw_r2_twl4030,\
  204. .put = snd_soc_put_volsw_r2_twl4030, \
  205. .private_value = (unsigned long)&(struct soc_mixer_control) \
  206. {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
  207. .max = xmax, .invert = xinvert} }
  208. #define SOC_SINGLE_TLV_TWL4030(xname, xreg, xshift, xmax, xinvert, tlv_array) \
  209. SOC_DOUBLE_TLV_TWL4030(xname, xreg, xshift, xshift, xmax, \
  210. xinvert, tlv_array)
  211. static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
  212. struct snd_ctl_elem_value *ucontrol)
  213. {
  214. struct soc_mixer_control *mc =
  215. (struct soc_mixer_control *)kcontrol->private_value;
  216. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  217. unsigned int reg = mc->reg;
  218. unsigned int shift = mc->shift;
  219. unsigned int rshift = mc->rshift;
  220. int max = mc->max;
  221. int mask = (1 << fls(max)) - 1;
  222. ucontrol->value.integer.value[0] =
  223. (snd_soc_read(codec, reg) >> shift) & mask;
  224. if (ucontrol->value.integer.value[0])
  225. ucontrol->value.integer.value[0] =
  226. max + 1 - ucontrol->value.integer.value[0];
  227. if (shift != rshift) {
  228. ucontrol->value.integer.value[1] =
  229. (snd_soc_read(codec, reg) >> rshift) & mask;
  230. if (ucontrol->value.integer.value[1])
  231. ucontrol->value.integer.value[1] =
  232. max + 1 - ucontrol->value.integer.value[1];
  233. }
  234. return 0;
  235. }
  236. static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
  237. struct snd_ctl_elem_value *ucontrol)
  238. {
  239. struct soc_mixer_control *mc =
  240. (struct soc_mixer_control *)kcontrol->private_value;
  241. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  242. unsigned int reg = mc->reg;
  243. unsigned int shift = mc->shift;
  244. unsigned int rshift = mc->rshift;
  245. int max = mc->max;
  246. int mask = (1 << fls(max)) - 1;
  247. unsigned short val, val2, val_mask;
  248. val = (ucontrol->value.integer.value[0] & mask);
  249. val_mask = mask << shift;
  250. if (val)
  251. val = max + 1 - val;
  252. val = val << shift;
  253. if (shift != rshift) {
  254. val2 = (ucontrol->value.integer.value[1] & mask);
  255. val_mask |= mask << rshift;
  256. if (val2)
  257. val2 = max + 1 - val2;
  258. val |= val2 << rshift;
  259. }
  260. return snd_soc_update_bits(codec, reg, val_mask, val);
  261. }
  262. static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
  263. struct snd_ctl_elem_value *ucontrol)
  264. {
  265. struct soc_mixer_control *mc =
  266. (struct soc_mixer_control *)kcontrol->private_value;
  267. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  268. unsigned int reg = mc->reg;
  269. unsigned int reg2 = mc->rreg;
  270. unsigned int shift = mc->shift;
  271. int max = mc->max;
  272. int mask = (1<<fls(max))-1;
  273. ucontrol->value.integer.value[0] =
  274. (snd_soc_read(codec, reg) >> shift) & mask;
  275. ucontrol->value.integer.value[1] =
  276. (snd_soc_read(codec, reg2) >> shift) & mask;
  277. if (ucontrol->value.integer.value[0])
  278. ucontrol->value.integer.value[0] =
  279. max + 1 - ucontrol->value.integer.value[0];
  280. if (ucontrol->value.integer.value[1])
  281. ucontrol->value.integer.value[1] =
  282. max + 1 - ucontrol->value.integer.value[1];
  283. return 0;
  284. }
  285. static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
  286. struct snd_ctl_elem_value *ucontrol)
  287. {
  288. struct soc_mixer_control *mc =
  289. (struct soc_mixer_control *)kcontrol->private_value;
  290. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  291. unsigned int reg = mc->reg;
  292. unsigned int reg2 = mc->rreg;
  293. unsigned int shift = mc->shift;
  294. int max = mc->max;
  295. int mask = (1 << fls(max)) - 1;
  296. int err;
  297. unsigned short val, val2, val_mask;
  298. val_mask = mask << shift;
  299. val = (ucontrol->value.integer.value[0] & mask);
  300. val2 = (ucontrol->value.integer.value[1] & mask);
  301. if (val)
  302. val = max + 1 - val;
  303. if (val2)
  304. val2 = max + 1 - val2;
  305. val = val << shift;
  306. val2 = val2 << shift;
  307. err = snd_soc_update_bits(codec, reg, val_mask, val);
  308. if (err < 0)
  309. return err;
  310. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  311. return err;
  312. }
  313. static int twl4030_get_left_input(struct snd_kcontrol *kcontrol,
  314. struct snd_ctl_elem_value *ucontrol)
  315. {
  316. struct snd_soc_codec *codec = kcontrol->private_data;
  317. u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
  318. int result = 0;
  319. /* one bit must be set a time */
  320. reg &= TWL4030_CKMIC_EN | TWL4030_AUXL_EN | TWL4030_HSMIC_EN
  321. | TWL4030_MAINMIC_EN;
  322. if (reg != 0) {
  323. result++;
  324. while ((reg & 1) == 0) {
  325. result++;
  326. reg >>= 1;
  327. }
  328. }
  329. ucontrol->value.integer.value[0] = result;
  330. return 0;
  331. }
  332. static int twl4030_put_left_input(struct snd_kcontrol *kcontrol,
  333. struct snd_ctl_elem_value *ucontrol)
  334. {
  335. struct snd_soc_codec *codec = kcontrol->private_data;
  336. int value = ucontrol->value.integer.value[0];
  337. u8 anamicl, micbias, avadc_ctl;
  338. anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
  339. anamicl &= ~(TWL4030_CKMIC_EN | TWL4030_AUXL_EN | TWL4030_HSMIC_EN
  340. | TWL4030_MAINMIC_EN);
  341. micbias = twl4030_read_reg_cache(codec, TWL4030_REG_MICBIAS_CTL);
  342. micbias &= ~(TWL4030_HSMICBIAS_EN | TWL4030_MICBIAS1_EN);
  343. avadc_ctl = twl4030_read_reg_cache(codec, TWL4030_REG_AVADC_CTL);
  344. switch (value) {
  345. case 1:
  346. anamicl |= TWL4030_MAINMIC_EN;
  347. micbias |= TWL4030_MICBIAS1_EN;
  348. break;
  349. case 2:
  350. anamicl |= TWL4030_HSMIC_EN;
  351. micbias |= TWL4030_HSMICBIAS_EN;
  352. break;
  353. case 3:
  354. anamicl |= TWL4030_AUXL_EN;
  355. break;
  356. case 4:
  357. anamicl |= TWL4030_CKMIC_EN;
  358. break;
  359. default:
  360. break;
  361. }
  362. /* If some input is selected, enable amp and ADC */
  363. if (value != 0) {
  364. anamicl |= TWL4030_MICAMPL_EN;
  365. avadc_ctl |= TWL4030_ADCL_EN;
  366. } else {
  367. anamicl &= ~TWL4030_MICAMPL_EN;
  368. avadc_ctl &= ~TWL4030_ADCL_EN;
  369. }
  370. twl4030_write(codec, TWL4030_REG_ANAMICL, anamicl);
  371. twl4030_write(codec, TWL4030_REG_MICBIAS_CTL, micbias);
  372. twl4030_write(codec, TWL4030_REG_AVADC_CTL, avadc_ctl);
  373. return 1;
  374. }
  375. static int twl4030_get_right_input(struct snd_kcontrol *kcontrol,
  376. struct snd_ctl_elem_value *ucontrol)
  377. {
  378. struct snd_soc_codec *codec = kcontrol->private_data;
  379. u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICR);
  380. int value = 0;
  381. reg &= TWL4030_SUBMIC_EN|TWL4030_AUXR_EN;
  382. switch (reg) {
  383. case TWL4030_SUBMIC_EN:
  384. value = 1;
  385. break;
  386. case TWL4030_AUXR_EN:
  387. value = 2;
  388. break;
  389. default:
  390. break;
  391. }
  392. ucontrol->value.integer.value[0] = value;
  393. return 0;
  394. }
  395. static int twl4030_put_right_input(struct snd_kcontrol *kcontrol,
  396. struct snd_ctl_elem_value *ucontrol)
  397. {
  398. struct snd_soc_codec *codec = kcontrol->private_data;
  399. int value = ucontrol->value.integer.value[0];
  400. u8 anamicr, micbias, avadc_ctl;
  401. anamicr = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICR);
  402. anamicr &= ~(TWL4030_SUBMIC_EN|TWL4030_AUXR_EN);
  403. micbias = twl4030_read_reg_cache(codec, TWL4030_REG_MICBIAS_CTL);
  404. micbias &= ~TWL4030_MICBIAS2_EN;
  405. avadc_ctl = twl4030_read_reg_cache(codec, TWL4030_REG_AVADC_CTL);
  406. switch (value) {
  407. case 1:
  408. anamicr |= TWL4030_SUBMIC_EN;
  409. micbias |= TWL4030_MICBIAS2_EN;
  410. break;
  411. case 2:
  412. anamicr |= TWL4030_AUXR_EN;
  413. break;
  414. default:
  415. break;
  416. }
  417. if (value != 0) {
  418. anamicr |= TWL4030_MICAMPR_EN;
  419. avadc_ctl |= TWL4030_ADCR_EN;
  420. } else {
  421. anamicr &= ~TWL4030_MICAMPR_EN;
  422. avadc_ctl &= ~TWL4030_ADCR_EN;
  423. }
  424. twl4030_write(codec, TWL4030_REG_ANAMICR, anamicr);
  425. twl4030_write(codec, TWL4030_REG_MICBIAS_CTL, micbias);
  426. twl4030_write(codec, TWL4030_REG_AVADC_CTL, avadc_ctl);
  427. return 1;
  428. }
  429. static const char *twl4030_left_in_sel[] = {
  430. "None",
  431. "Main Mic",
  432. "Headset Mic",
  433. "Line In",
  434. "Carkit Mic",
  435. };
  436. static const char *twl4030_right_in_sel[] = {
  437. "None",
  438. "Sub Mic",
  439. "Line In",
  440. };
  441. static const struct soc_enum twl4030_left_input_mux =
  442. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl4030_left_in_sel),
  443. twl4030_left_in_sel);
  444. static const struct soc_enum twl4030_right_input_mux =
  445. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl4030_right_in_sel),
  446. twl4030_right_in_sel);
  447. /*
  448. * FGAIN volume control:
  449. * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
  450. */
  451. static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
  452. /*
  453. * CGAIN volume control:
  454. * 0 dB to 12 dB in 6 dB steps
  455. * value 2 and 3 means 12 dB
  456. */
  457. static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
  458. /*
  459. * Analog playback gain
  460. * -24 dB to 12 dB in 2 dB steps
  461. */
  462. static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
  463. /*
  464. * Gain controls tied to outputs
  465. * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
  466. */
  467. static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
  468. /*
  469. * Capture gain after the ADCs
  470. * from 0 dB to 31 dB in 1 dB steps
  471. */
  472. static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
  473. /*
  474. * Gain control for input amplifiers
  475. * 0 dB to 30 dB in 6 dB steps
  476. */
  477. static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
  478. static const struct snd_kcontrol_new twl4030_snd_controls[] = {
  479. /* Common playback gain controls */
  480. SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
  481. TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
  482. 0, 0x3f, 0, digital_fine_tlv),
  483. SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
  484. TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
  485. 0, 0x3f, 0, digital_fine_tlv),
  486. SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
  487. TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
  488. 6, 0x2, 0, digital_coarse_tlv),
  489. SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
  490. TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
  491. 6, 0x2, 0, digital_coarse_tlv),
  492. SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
  493. TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
  494. 3, 0x12, 1, analog_tlv),
  495. SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
  496. TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
  497. 3, 0x12, 1, analog_tlv),
  498. SOC_DOUBLE_R("DAC1 Analog Playback Switch",
  499. TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
  500. 1, 1, 0),
  501. SOC_DOUBLE_R("DAC2 Analog Playback Switch",
  502. TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
  503. 1, 1, 0),
  504. /* Separate output gain controls */
  505. SOC_DOUBLE_R_TLV_TWL4030("PreDriv Playback Volume",
  506. TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
  507. 4, 3, 0, output_tvl),
  508. SOC_DOUBLE_TLV_TWL4030("Headset Playback Volume",
  509. TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, output_tvl),
  510. SOC_DOUBLE_R_TLV_TWL4030("Carkit Playback Volume",
  511. TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
  512. 4, 3, 0, output_tvl),
  513. SOC_SINGLE_TLV_TWL4030("Earpiece Playback Volume",
  514. TWL4030_REG_EAR_CTL, 4, 3, 0, output_tvl),
  515. /* Common capture gain controls */
  516. SOC_DOUBLE_R_TLV("Capture Volume",
  517. TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
  518. 0, 0x1f, 0, digital_capture_tlv),
  519. SOC_DOUBLE_TLV("Input Boost Volume", TWL4030_REG_ANAMIC_GAIN,
  520. 0, 3, 5, 0, input_gain_tlv),
  521. /* Input source controls */
  522. SOC_ENUM_EXT("Left Input Source", twl4030_left_input_mux,
  523. twl4030_get_left_input, twl4030_put_left_input),
  524. SOC_ENUM_EXT("Right Input Source", twl4030_right_input_mux,
  525. twl4030_get_right_input, twl4030_put_right_input),
  526. };
  527. /* add non dapm controls */
  528. static int twl4030_add_controls(struct snd_soc_codec *codec)
  529. {
  530. int err, i;
  531. for (i = 0; i < ARRAY_SIZE(twl4030_snd_controls); i++) {
  532. err = snd_ctl_add(codec->card,
  533. snd_soc_cnew(&twl4030_snd_controls[i],
  534. codec, NULL));
  535. if (err < 0)
  536. return err;
  537. }
  538. return 0;
  539. }
  540. static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
  541. SND_SOC_DAPM_INPUT("INL"),
  542. SND_SOC_DAPM_INPUT("INR"),
  543. SND_SOC_DAPM_OUTPUT("OUTL"),
  544. SND_SOC_DAPM_OUTPUT("OUTR"),
  545. /* DACs */
  546. SND_SOC_DAPM_DAC("DACR1", "Right Front Playback",
  547. TWL4030_REG_AVDAC_CTL, 0, 0),
  548. SND_SOC_DAPM_DAC("DACL1", "Left Front Playback",
  549. TWL4030_REG_AVDAC_CTL, 1, 0),
  550. SND_SOC_DAPM_DAC("DACR2", "Right Rear Playback",
  551. TWL4030_REG_AVDAC_CTL, 2, 0),
  552. SND_SOC_DAPM_DAC("DACL2", "Left Rear Playback",
  553. TWL4030_REG_AVDAC_CTL, 3, 0),
  554. /* Analog PGAs */
  555. SND_SOC_DAPM_PGA("ARXR1_APGA", TWL4030_REG_ARXR1_APGA_CTL,
  556. 0, 0, NULL, 0),
  557. SND_SOC_DAPM_PGA("ARXL1_APGA", TWL4030_REG_ARXL1_APGA_CTL,
  558. 0, 0, NULL, 0),
  559. SND_SOC_DAPM_PGA("ARXR2_APGA", TWL4030_REG_ARXR2_APGA_CTL,
  560. 0, 0, NULL, 0),
  561. SND_SOC_DAPM_PGA("ARXL2_APGA", TWL4030_REG_ARXL2_APGA_CTL,
  562. 0, 0, NULL, 0),
  563. SND_SOC_DAPM_ADC("ADCL", "Left Capture", SND_SOC_NOPM, 0, 0),
  564. SND_SOC_DAPM_ADC("ADCR", "Right Capture", SND_SOC_NOPM, 0, 0),
  565. };
  566. static const struct snd_soc_dapm_route intercon[] = {
  567. {"ARXL1_APGA", NULL, "DACL1"},
  568. {"ARXR1_APGA", NULL, "DACR1"},
  569. {"ARXL2_APGA", NULL, "DACL2"},
  570. {"ARXR2_APGA", NULL, "DACR2"},
  571. /* outputs */
  572. {"OUTL", NULL, "ARXL2_APGA"},
  573. {"OUTR", NULL, "ARXR2_APGA"},
  574. /* inputs */
  575. {"ADCL", NULL, "INL"},
  576. {"ADCR", NULL, "INR"},
  577. };
  578. static int twl4030_add_widgets(struct snd_soc_codec *codec)
  579. {
  580. snd_soc_dapm_new_controls(codec, twl4030_dapm_widgets,
  581. ARRAY_SIZE(twl4030_dapm_widgets));
  582. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  583. snd_soc_dapm_new_widgets(codec);
  584. return 0;
  585. }
  586. static void twl4030_power_up(struct snd_soc_codec *codec)
  587. {
  588. u8 anamicl, regmisc1, byte, popn, hsgain;
  589. int i = 0;
  590. /* set CODECPDZ to turn on codec */
  591. twl4030_set_codecpdz(codec);
  592. /* initiate offset cancellation */
  593. anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
  594. twl4030_write(codec, TWL4030_REG_ANAMICL,
  595. anamicl | TWL4030_CNCL_OFFSET_START);
  596. /* wait for offset cancellation to complete */
  597. do {
  598. /* this takes a little while, so don't slam i2c */
  599. udelay(2000);
  600. twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
  601. TWL4030_REG_ANAMICL);
  602. } while ((i++ < 100) &&
  603. ((byte & TWL4030_CNCL_OFFSET_START) ==
  604. TWL4030_CNCL_OFFSET_START));
  605. /* anti-pop when changing analog gain */
  606. regmisc1 = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1);
  607. twl4030_write(codec, TWL4030_REG_MISC_SET_1,
  608. regmisc1 | TWL4030_SMOOTH_ANAVOL_EN);
  609. /* toggle CODECPDZ as per TRM */
  610. twl4030_clear_codecpdz(codec);
  611. twl4030_set_codecpdz(codec);
  612. /* program anti-pop with bias ramp delay */
  613. popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
  614. popn &= TWL4030_RAMP_DELAY;
  615. popn |= TWL4030_RAMP_DELAY_645MS;
  616. twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
  617. popn |= TWL4030_VMID_EN;
  618. twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
  619. /* enable output stage and gain setting */
  620. hsgain = TWL4030_HSR_GAIN_0DB | TWL4030_HSL_GAIN_0DB;
  621. twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain);
  622. /* enable anti-pop ramp */
  623. popn |= TWL4030_RAMP_EN;
  624. twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
  625. }
  626. static void twl4030_power_down(struct snd_soc_codec *codec)
  627. {
  628. u8 popn, hsgain;
  629. /* disable anti-pop ramp */
  630. popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
  631. popn &= ~TWL4030_RAMP_EN;
  632. twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
  633. /* disable output stage and gain setting */
  634. hsgain = TWL4030_HSR_GAIN_PWR_DOWN | TWL4030_HSL_GAIN_PWR_DOWN;
  635. twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain);
  636. /* disable bias out */
  637. popn &= ~TWL4030_VMID_EN;
  638. twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
  639. /* power down */
  640. twl4030_clear_codecpdz(codec);
  641. }
  642. static int twl4030_set_bias_level(struct snd_soc_codec *codec,
  643. enum snd_soc_bias_level level)
  644. {
  645. switch (level) {
  646. case SND_SOC_BIAS_ON:
  647. twl4030_power_up(codec);
  648. break;
  649. case SND_SOC_BIAS_PREPARE:
  650. /* TODO: develop a twl4030_prepare function */
  651. break;
  652. case SND_SOC_BIAS_STANDBY:
  653. /* TODO: develop a twl4030_standby function */
  654. twl4030_power_down(codec);
  655. break;
  656. case SND_SOC_BIAS_OFF:
  657. twl4030_power_down(codec);
  658. break;
  659. }
  660. codec->bias_level = level;
  661. return 0;
  662. }
  663. static int twl4030_hw_params(struct snd_pcm_substream *substream,
  664. struct snd_pcm_hw_params *params,
  665. struct snd_soc_dai *dai)
  666. {
  667. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  668. struct snd_soc_device *socdev = rtd->socdev;
  669. struct snd_soc_codec *codec = socdev->codec;
  670. u8 mode, old_mode, format, old_format;
  671. /* bit rate */
  672. old_mode = twl4030_read_reg_cache(codec,
  673. TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
  674. mode = old_mode & ~TWL4030_APLL_RATE;
  675. switch (params_rate(params)) {
  676. case 8000:
  677. mode |= TWL4030_APLL_RATE_8000;
  678. break;
  679. case 11025:
  680. mode |= TWL4030_APLL_RATE_11025;
  681. break;
  682. case 12000:
  683. mode |= TWL4030_APLL_RATE_12000;
  684. break;
  685. case 16000:
  686. mode |= TWL4030_APLL_RATE_16000;
  687. break;
  688. case 22050:
  689. mode |= TWL4030_APLL_RATE_22050;
  690. break;
  691. case 24000:
  692. mode |= TWL4030_APLL_RATE_24000;
  693. break;
  694. case 32000:
  695. mode |= TWL4030_APLL_RATE_32000;
  696. break;
  697. case 44100:
  698. mode |= TWL4030_APLL_RATE_44100;
  699. break;
  700. case 48000:
  701. mode |= TWL4030_APLL_RATE_48000;
  702. break;
  703. default:
  704. printk(KERN_ERR "TWL4030 hw params: unknown rate %d\n",
  705. params_rate(params));
  706. return -EINVAL;
  707. }
  708. if (mode != old_mode) {
  709. /* change rate and set CODECPDZ */
  710. twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
  711. twl4030_set_codecpdz(codec);
  712. }
  713. /* sample size */
  714. old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
  715. format = old_format;
  716. format &= ~TWL4030_DATA_WIDTH;
  717. switch (params_format(params)) {
  718. case SNDRV_PCM_FORMAT_S16_LE:
  719. format |= TWL4030_DATA_WIDTH_16S_16W;
  720. break;
  721. case SNDRV_PCM_FORMAT_S24_LE:
  722. format |= TWL4030_DATA_WIDTH_32S_24W;
  723. break;
  724. default:
  725. printk(KERN_ERR "TWL4030 hw params: unknown format %d\n",
  726. params_format(params));
  727. return -EINVAL;
  728. }
  729. if (format != old_format) {
  730. /* clear CODECPDZ before changing format (codec requirement) */
  731. twl4030_clear_codecpdz(codec);
  732. /* change format */
  733. twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
  734. /* set CODECPDZ afterwards */
  735. twl4030_set_codecpdz(codec);
  736. }
  737. return 0;
  738. }
  739. static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  740. int clk_id, unsigned int freq, int dir)
  741. {
  742. struct snd_soc_codec *codec = codec_dai->codec;
  743. u8 infreq;
  744. switch (freq) {
  745. case 19200000:
  746. infreq = TWL4030_APLL_INFREQ_19200KHZ;
  747. break;
  748. case 26000000:
  749. infreq = TWL4030_APLL_INFREQ_26000KHZ;
  750. break;
  751. case 38400000:
  752. infreq = TWL4030_APLL_INFREQ_38400KHZ;
  753. break;
  754. default:
  755. printk(KERN_ERR "TWL4030 set sysclk: unknown rate %d\n",
  756. freq);
  757. return -EINVAL;
  758. }
  759. infreq |= TWL4030_APLL_EN;
  760. twl4030_write(codec, TWL4030_REG_APLL_CTL, infreq);
  761. return 0;
  762. }
  763. static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai,
  764. unsigned int fmt)
  765. {
  766. struct snd_soc_codec *codec = codec_dai->codec;
  767. u8 old_format, format;
  768. /* get format */
  769. old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
  770. format = old_format;
  771. /* set master/slave audio interface */
  772. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  773. case SND_SOC_DAIFMT_CBM_CFM:
  774. format &= ~(TWL4030_AIF_SLAVE_EN);
  775. format &= ~(TWL4030_CLK256FS_EN);
  776. break;
  777. case SND_SOC_DAIFMT_CBS_CFS:
  778. format |= TWL4030_AIF_SLAVE_EN;
  779. format |= TWL4030_CLK256FS_EN;
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. /* interface format */
  785. format &= ~TWL4030_AIF_FORMAT;
  786. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  787. case SND_SOC_DAIFMT_I2S:
  788. format |= TWL4030_AIF_FORMAT_CODEC;
  789. break;
  790. default:
  791. return -EINVAL;
  792. }
  793. if (format != old_format) {
  794. /* clear CODECPDZ before changing format (codec requirement) */
  795. twl4030_clear_codecpdz(codec);
  796. /* change format */
  797. twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
  798. /* set CODECPDZ afterwards */
  799. twl4030_set_codecpdz(codec);
  800. }
  801. return 0;
  802. }
  803. #define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000)
  804. #define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE)
  805. struct snd_soc_dai twl4030_dai = {
  806. .name = "twl4030",
  807. .playback = {
  808. .stream_name = "Playback",
  809. .channels_min = 2,
  810. .channels_max = 2,
  811. .rates = TWL4030_RATES,
  812. .formats = TWL4030_FORMATS,},
  813. .capture = {
  814. .stream_name = "Capture",
  815. .channels_min = 2,
  816. .channels_max = 2,
  817. .rates = TWL4030_RATES,
  818. .formats = TWL4030_FORMATS,},
  819. .ops = {
  820. .hw_params = twl4030_hw_params,
  821. .set_sysclk = twl4030_set_dai_sysclk,
  822. .set_fmt = twl4030_set_dai_fmt,
  823. }
  824. };
  825. EXPORT_SYMBOL_GPL(twl4030_dai);
  826. static int twl4030_suspend(struct platform_device *pdev, pm_message_t state)
  827. {
  828. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  829. struct snd_soc_codec *codec = socdev->codec;
  830. twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
  831. return 0;
  832. }
  833. static int twl4030_resume(struct platform_device *pdev)
  834. {
  835. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  836. struct snd_soc_codec *codec = socdev->codec;
  837. twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  838. twl4030_set_bias_level(codec, codec->suspend_bias_level);
  839. return 0;
  840. }
  841. /*
  842. * initialize the driver
  843. * register the mixer and dsp interfaces with the kernel
  844. */
  845. static int twl4030_init(struct snd_soc_device *socdev)
  846. {
  847. struct snd_soc_codec *codec = socdev->codec;
  848. int ret = 0;
  849. printk(KERN_INFO "TWL4030 Audio Codec init \n");
  850. codec->name = "twl4030";
  851. codec->owner = THIS_MODULE;
  852. codec->read = twl4030_read_reg_cache;
  853. codec->write = twl4030_write;
  854. codec->set_bias_level = twl4030_set_bias_level;
  855. codec->dai = &twl4030_dai;
  856. codec->num_dai = 1;
  857. codec->reg_cache_size = sizeof(twl4030_reg);
  858. codec->reg_cache = kmemdup(twl4030_reg, sizeof(twl4030_reg),
  859. GFP_KERNEL);
  860. if (codec->reg_cache == NULL)
  861. return -ENOMEM;
  862. /* register pcms */
  863. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  864. if (ret < 0) {
  865. printk(KERN_ERR "twl4030: failed to create pcms\n");
  866. goto pcm_err;
  867. }
  868. twl4030_init_chip(codec);
  869. /* power on device */
  870. twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  871. twl4030_add_controls(codec);
  872. twl4030_add_widgets(codec);
  873. ret = snd_soc_init_card(socdev);
  874. if (ret < 0) {
  875. printk(KERN_ERR "twl4030: failed to register card\n");
  876. goto card_err;
  877. }
  878. return ret;
  879. card_err:
  880. snd_soc_free_pcms(socdev);
  881. snd_soc_dapm_free(socdev);
  882. pcm_err:
  883. kfree(codec->reg_cache);
  884. return ret;
  885. }
  886. static struct snd_soc_device *twl4030_socdev;
  887. static int twl4030_probe(struct platform_device *pdev)
  888. {
  889. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  890. struct snd_soc_codec *codec;
  891. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  892. if (codec == NULL)
  893. return -ENOMEM;
  894. socdev->codec = codec;
  895. mutex_init(&codec->mutex);
  896. INIT_LIST_HEAD(&codec->dapm_widgets);
  897. INIT_LIST_HEAD(&codec->dapm_paths);
  898. twl4030_socdev = socdev;
  899. twl4030_init(socdev);
  900. return 0;
  901. }
  902. static int twl4030_remove(struct platform_device *pdev)
  903. {
  904. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  905. struct snd_soc_codec *codec = socdev->codec;
  906. printk(KERN_INFO "TWL4030 Audio Codec remove\n");
  907. kfree(codec);
  908. return 0;
  909. }
  910. struct snd_soc_codec_device soc_codec_dev_twl4030 = {
  911. .probe = twl4030_probe,
  912. .remove = twl4030_remove,
  913. .suspend = twl4030_suspend,
  914. .resume = twl4030_resume,
  915. };
  916. EXPORT_SYMBOL_GPL(soc_codec_dev_twl4030);
  917. MODULE_DESCRIPTION("ASoC TWL4030 codec driver");
  918. MODULE_AUTHOR("Steve Sakoman");
  919. MODULE_LICENSE("GPL");