tlv320aic31xx.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /*
  2. * ALSA SoC TLV320AIC31XX codec driver
  3. *
  4. * Copyright (C) 2014 Texas Instruments, Inc.
  5. *
  6. * Author: Jyri Sarha <jsarha@ti.com>
  7. *
  8. * Based on ground work by: Ajit Kulkarni <x0175765@ti.com>
  9. *
  10. * This package is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * THIS PACKAGE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * The TLV320AIC31xx series of audio codec is a low-power, highly integrated
  19. * high performance codec which provides a stereo DAC, a mono ADC,
  20. * and mono/stereo Class-D speaker driver.
  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/gpio.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/of.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/slab.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/soc.h>
  37. #include <sound/initval.h>
  38. #include <sound/tlv.h>
  39. #include <dt-bindings/sound/tlv320aic31xx-micbias.h>
  40. #include "tlv320aic31xx.h"
  41. static const struct reg_default aic31xx_reg_defaults[] = {
  42. { AIC31XX_CLKMUX, 0x00 },
  43. { AIC31XX_PLLPR, 0x11 },
  44. { AIC31XX_PLLJ, 0x04 },
  45. { AIC31XX_PLLDMSB, 0x00 },
  46. { AIC31XX_PLLDLSB, 0x00 },
  47. { AIC31XX_NDAC, 0x01 },
  48. { AIC31XX_MDAC, 0x01 },
  49. { AIC31XX_DOSRMSB, 0x00 },
  50. { AIC31XX_DOSRLSB, 0x80 },
  51. { AIC31XX_NADC, 0x01 },
  52. { AIC31XX_MADC, 0x01 },
  53. { AIC31XX_AOSR, 0x80 },
  54. { AIC31XX_IFACE1, 0x00 },
  55. { AIC31XX_DATA_OFFSET, 0x00 },
  56. { AIC31XX_IFACE2, 0x00 },
  57. { AIC31XX_BCLKN, 0x01 },
  58. { AIC31XX_DACSETUP, 0x14 },
  59. { AIC31XX_DACMUTE, 0x0c },
  60. { AIC31XX_LDACVOL, 0x00 },
  61. { AIC31XX_RDACVOL, 0x00 },
  62. { AIC31XX_ADCSETUP, 0x00 },
  63. { AIC31XX_ADCFGA, 0x80 },
  64. { AIC31XX_ADCVOL, 0x00 },
  65. { AIC31XX_HPDRIVER, 0x04 },
  66. { AIC31XX_SPKAMP, 0x06 },
  67. { AIC31XX_DACMIXERROUTE, 0x00 },
  68. { AIC31XX_LANALOGHPL, 0x7f },
  69. { AIC31XX_RANALOGHPR, 0x7f },
  70. { AIC31XX_LANALOGSPL, 0x7f },
  71. { AIC31XX_RANALOGSPR, 0x7f },
  72. { AIC31XX_HPLGAIN, 0x02 },
  73. { AIC31XX_HPRGAIN, 0x02 },
  74. { AIC31XX_SPLGAIN, 0x00 },
  75. { AIC31XX_SPRGAIN, 0x00 },
  76. { AIC31XX_MICBIAS, 0x00 },
  77. { AIC31XX_MICPGA, 0x80 },
  78. { AIC31XX_MICPGAPI, 0x00 },
  79. { AIC31XX_MICPGAMI, 0x00 },
  80. };
  81. static bool aic31xx_volatile(struct device *dev, unsigned int reg)
  82. {
  83. switch (reg) {
  84. case AIC31XX_PAGECTL: /* regmap implementation requires this */
  85. case AIC31XX_RESET: /* always clears after write */
  86. case AIC31XX_OT_FLAG:
  87. case AIC31XX_ADCFLAG:
  88. case AIC31XX_DACFLAG1:
  89. case AIC31XX_DACFLAG2:
  90. case AIC31XX_OFFLAG: /* Sticky interrupt flags */
  91. case AIC31XX_INTRDACFLAG: /* Sticky interrupt flags */
  92. case AIC31XX_INTRADCFLAG: /* Sticky interrupt flags */
  93. case AIC31XX_INTRDACFLAG2:
  94. case AIC31XX_INTRADCFLAG2:
  95. return true;
  96. }
  97. return false;
  98. }
  99. static bool aic31xx_writeable(struct device *dev, unsigned int reg)
  100. {
  101. switch (reg) {
  102. case AIC31XX_OT_FLAG:
  103. case AIC31XX_ADCFLAG:
  104. case AIC31XX_DACFLAG1:
  105. case AIC31XX_DACFLAG2:
  106. case AIC31XX_OFFLAG: /* Sticky interrupt flags */
  107. case AIC31XX_INTRDACFLAG: /* Sticky interrupt flags */
  108. case AIC31XX_INTRADCFLAG: /* Sticky interrupt flags */
  109. case AIC31XX_INTRDACFLAG2:
  110. case AIC31XX_INTRADCFLAG2:
  111. return false;
  112. }
  113. return true;
  114. }
  115. static const struct regmap_range_cfg aic31xx_ranges[] = {
  116. {
  117. .range_min = 0,
  118. .range_max = 12 * 128,
  119. .selector_reg = AIC31XX_PAGECTL,
  120. .selector_mask = 0xff,
  121. .selector_shift = 0,
  122. .window_start = 0,
  123. .window_len = 128,
  124. },
  125. };
  126. static const struct regmap_config aic31xx_i2c_regmap = {
  127. .reg_bits = 8,
  128. .val_bits = 8,
  129. .writeable_reg = aic31xx_writeable,
  130. .volatile_reg = aic31xx_volatile,
  131. .reg_defaults = aic31xx_reg_defaults,
  132. .num_reg_defaults = ARRAY_SIZE(aic31xx_reg_defaults),
  133. .cache_type = REGCACHE_RBTREE,
  134. .ranges = aic31xx_ranges,
  135. .num_ranges = ARRAY_SIZE(aic31xx_ranges),
  136. .max_register = 12 * 128,
  137. };
  138. #define AIC31XX_NUM_SUPPLIES 6
  139. static const char * const aic31xx_supply_names[AIC31XX_NUM_SUPPLIES] = {
  140. "HPVDD",
  141. "SPRVDD",
  142. "SPLVDD",
  143. "AVDD",
  144. "IOVDD",
  145. "DVDD",
  146. };
  147. struct aic31xx_disable_nb {
  148. struct notifier_block nb;
  149. struct aic31xx_priv *aic31xx;
  150. };
  151. struct aic31xx_priv {
  152. struct snd_soc_codec *codec;
  153. u8 i2c_regs_status;
  154. struct device *dev;
  155. struct regmap *regmap;
  156. struct aic31xx_pdata pdata;
  157. struct regulator_bulk_data supplies[AIC31XX_NUM_SUPPLIES];
  158. struct aic31xx_disable_nb disable_nb[AIC31XX_NUM_SUPPLIES];
  159. unsigned int sysclk;
  160. u8 p_div;
  161. int rate_div_line;
  162. };
  163. struct aic31xx_rate_divs {
  164. u32 mclk_p;
  165. u32 rate;
  166. u8 pll_j;
  167. u16 pll_d;
  168. u16 dosr;
  169. u8 ndac;
  170. u8 mdac;
  171. u8 aosr;
  172. u8 nadc;
  173. u8 madc;
  174. };
  175. /* ADC dividers can be disabled by cofiguring them to 0 */
  176. static const struct aic31xx_rate_divs aic31xx_divs[] = {
  177. /* mclk/p rate pll: j d dosr ndac mdac aors nadc madc */
  178. /* 8k rate */
  179. {12000000, 8000, 8, 1920, 128, 48, 2, 128, 48, 2},
  180. {12000000, 8000, 8, 1920, 128, 32, 3, 128, 32, 3},
  181. {12500000, 8000, 7, 8643, 128, 48, 2, 128, 48, 2},
  182. /* 11.025k rate */
  183. {12000000, 11025, 7, 5264, 128, 32, 2, 128, 32, 2},
  184. {12000000, 11025, 8, 4672, 128, 24, 3, 128, 24, 3},
  185. {12500000, 11025, 7, 2253, 128, 32, 2, 128, 32, 2},
  186. /* 16k rate */
  187. {12000000, 16000, 8, 1920, 128, 24, 2, 128, 24, 2},
  188. {12000000, 16000, 8, 1920, 128, 16, 3, 128, 16, 3},
  189. {12500000, 16000, 7, 8643, 128, 24, 2, 128, 24, 2},
  190. /* 22.05k rate */
  191. {12000000, 22050, 7, 5264, 128, 16, 2, 128, 16, 2},
  192. {12000000, 22050, 8, 4672, 128, 12, 3, 128, 12, 3},
  193. {12500000, 22050, 7, 2253, 128, 16, 2, 128, 16, 2},
  194. /* 32k rate */
  195. {12000000, 32000, 8, 1920, 128, 12, 2, 128, 12, 2},
  196. {12000000, 32000, 8, 1920, 128, 8, 3, 128, 8, 3},
  197. {12500000, 32000, 7, 8643, 128, 12, 2, 128, 12, 2},
  198. /* 44.1k rate */
  199. {12000000, 44100, 7, 5264, 128, 8, 2, 128, 8, 2},
  200. {12000000, 44100, 8, 4672, 128, 6, 3, 128, 6, 3},
  201. {12500000, 44100, 7, 2253, 128, 8, 2, 128, 8, 2},
  202. /* 48k rate */
  203. {12000000, 48000, 8, 1920, 128, 8, 2, 128, 8, 2},
  204. {12000000, 48000, 7, 6800, 96, 5, 4, 96, 5, 4},
  205. {12500000, 48000, 7, 8643, 128, 8, 2, 128, 8, 2},
  206. /* 88.2k rate */
  207. {12000000, 88200, 7, 5264, 64, 8, 2, 64, 8, 2},
  208. {12000000, 88200, 8, 4672, 64, 6, 3, 64, 6, 3},
  209. {12500000, 88200, 7, 2253, 64, 8, 2, 64, 8, 2},
  210. /* 96k rate */
  211. {12000000, 96000, 8, 1920, 64, 8, 2, 64, 8, 2},
  212. {12000000, 96000, 7, 6800, 48, 5, 4, 48, 5, 4},
  213. {12500000, 96000, 7, 8643, 64, 8, 2, 64, 8, 2},
  214. /* 176.4k rate */
  215. {12000000, 176400, 7, 5264, 32, 8, 2, 32, 8, 2},
  216. {12000000, 176400, 8, 4672, 32, 6, 3, 32, 6, 3},
  217. {12500000, 176400, 7, 2253, 32, 8, 2, 32, 8, 2},
  218. /* 192k rate */
  219. {12000000, 192000, 8, 1920, 32, 8, 2, 32, 8, 2},
  220. {12000000, 192000, 7, 6800, 24, 5, 4, 24, 5, 4},
  221. {12500000, 192000, 7, 8643, 32, 8, 2, 32, 8, 2},
  222. };
  223. static const char * const ldac_in_text[] = {
  224. "Off", "Left Data", "Right Data", "Mono"
  225. };
  226. static const char * const rdac_in_text[] = {
  227. "Off", "Right Data", "Left Data", "Mono"
  228. };
  229. static SOC_ENUM_SINGLE_DECL(ldac_in_enum, AIC31XX_DACSETUP, 4, ldac_in_text);
  230. static SOC_ENUM_SINGLE_DECL(rdac_in_enum, AIC31XX_DACSETUP, 2, rdac_in_text);
  231. static const char * const mic_select_text[] = {
  232. "Off", "FFR 10 Ohm", "FFR 20 Ohm", "FFR 40 Ohm"
  233. };
  234. static SOC_ENUM_SINGLE_DECL(mic1lp_p_enum, AIC31XX_MICPGAPI, 6,
  235. mic_select_text);
  236. static SOC_ENUM_SINGLE_DECL(mic1rp_p_enum, AIC31XX_MICPGAPI, 4,
  237. mic_select_text);
  238. static SOC_ENUM_SINGLE_DECL(mic1lm_p_enum, AIC31XX_MICPGAPI, 2,
  239. mic_select_text);
  240. static SOC_ENUM_SINGLE_DECL(cm_m_enum, AIC31XX_MICPGAMI, 6, mic_select_text);
  241. static SOC_ENUM_SINGLE_DECL(mic1lm_m_enum, AIC31XX_MICPGAMI, 4,
  242. mic_select_text);
  243. static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -6350, 50, 0);
  244. static const DECLARE_TLV_DB_SCALE(adc_fgain_tlv, 0, 10, 0);
  245. static const DECLARE_TLV_DB_SCALE(adc_cgain_tlv, -2000, 50, 0);
  246. static const DECLARE_TLV_DB_SCALE(mic_pga_tlv, 0, 50, 0);
  247. static const DECLARE_TLV_DB_SCALE(hp_drv_tlv, 0, 100, 0);
  248. static const DECLARE_TLV_DB_SCALE(class_D_drv_tlv, 600, 600, 0);
  249. static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -6350, 50, 0);
  250. static const DECLARE_TLV_DB_SCALE(sp_vol_tlv, -6350, 50, 0);
  251. /*
  252. * controls to be exported to the user space
  253. */
  254. static const struct snd_kcontrol_new aic31xx_snd_controls[] = {
  255. SOC_DOUBLE_R_S_TLV("DAC Playback Volume", AIC31XX_LDACVOL,
  256. AIC31XX_RDACVOL, 0, -127, 48, 7, 0, dac_vol_tlv),
  257. SOC_SINGLE_TLV("ADC Fine Capture Volume", AIC31XX_ADCFGA, 4, 4, 1,
  258. adc_fgain_tlv),
  259. SOC_SINGLE("ADC Capture Switch", AIC31XX_ADCFGA, 7, 1, 1),
  260. SOC_DOUBLE_R_S_TLV("ADC Capture Volume", AIC31XX_ADCVOL, AIC31XX_ADCVOL,
  261. 0, -24, 40, 6, 0, adc_cgain_tlv),
  262. SOC_SINGLE_TLV("Mic PGA Capture Volume", AIC31XX_MICPGA, 0,
  263. 119, 0, mic_pga_tlv),
  264. SOC_DOUBLE_R("HP Driver Playback Switch", AIC31XX_HPLGAIN,
  265. AIC31XX_HPRGAIN, 2, 1, 0),
  266. SOC_DOUBLE_R_TLV("HP Driver Playback Volume", AIC31XX_HPLGAIN,
  267. AIC31XX_HPRGAIN, 3, 0x09, 0, hp_drv_tlv),
  268. SOC_DOUBLE_R_TLV("HP Analog Playback Volume", AIC31XX_LANALOGHPL,
  269. AIC31XX_RANALOGHPR, 0, 0x7F, 1, hp_vol_tlv),
  270. };
  271. static const struct snd_kcontrol_new aic311x_snd_controls[] = {
  272. SOC_DOUBLE_R("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
  273. AIC31XX_SPRGAIN, 2, 1, 0),
  274. SOC_DOUBLE_R_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
  275. AIC31XX_SPRGAIN, 3, 3, 0, class_D_drv_tlv),
  276. SOC_DOUBLE_R_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
  277. AIC31XX_RANALOGSPR, 0, 0x7F, 1, sp_vol_tlv),
  278. };
  279. static const struct snd_kcontrol_new aic310x_snd_controls[] = {
  280. SOC_SINGLE("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
  281. 2, 1, 0),
  282. SOC_SINGLE_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
  283. 3, 3, 0, class_D_drv_tlv),
  284. SOC_SINGLE_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
  285. 0, 0x7F, 1, sp_vol_tlv),
  286. };
  287. static const struct snd_kcontrol_new ldac_in_control =
  288. SOC_DAPM_ENUM("DAC Left Input", ldac_in_enum);
  289. static const struct snd_kcontrol_new rdac_in_control =
  290. SOC_DAPM_ENUM("DAC Right Input", rdac_in_enum);
  291. static int aic31xx_wait_bits(struct aic31xx_priv *aic31xx, unsigned int reg,
  292. unsigned int mask, unsigned int wbits, int sleep,
  293. int count)
  294. {
  295. unsigned int bits;
  296. int counter = count;
  297. int ret = regmap_read(aic31xx->regmap, reg, &bits);
  298. while ((bits & mask) != wbits && counter && !ret) {
  299. usleep_range(sleep, sleep * 2);
  300. ret = regmap_read(aic31xx->regmap, reg, &bits);
  301. counter--;
  302. }
  303. if ((bits & mask) != wbits) {
  304. dev_err(aic31xx->dev,
  305. "%s: Failed! 0x%x was 0x%x expected 0x%x (%d, 0x%x, %d us)\n",
  306. __func__, reg, bits, wbits, ret, mask,
  307. (count - counter) * sleep);
  308. ret = -1;
  309. }
  310. return ret;
  311. }
  312. #define WIDGET_BIT(reg, shift) (((shift) << 8) | (reg))
  313. static int aic31xx_dapm_power_event(struct snd_soc_dapm_widget *w,
  314. struct snd_kcontrol *kcontrol, int event)
  315. {
  316. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(w->codec);
  317. unsigned int reg = AIC31XX_DACFLAG1;
  318. unsigned int mask;
  319. switch (WIDGET_BIT(w->reg, w->shift)) {
  320. case WIDGET_BIT(AIC31XX_DACSETUP, 7):
  321. mask = AIC31XX_LDACPWRSTATUS_MASK;
  322. break;
  323. case WIDGET_BIT(AIC31XX_DACSETUP, 6):
  324. mask = AIC31XX_RDACPWRSTATUS_MASK;
  325. break;
  326. case WIDGET_BIT(AIC31XX_HPDRIVER, 7):
  327. mask = AIC31XX_HPLDRVPWRSTATUS_MASK;
  328. break;
  329. case WIDGET_BIT(AIC31XX_HPDRIVER, 6):
  330. mask = AIC31XX_HPRDRVPWRSTATUS_MASK;
  331. break;
  332. case WIDGET_BIT(AIC31XX_SPKAMP, 7):
  333. mask = AIC31XX_SPLDRVPWRSTATUS_MASK;
  334. break;
  335. case WIDGET_BIT(AIC31XX_SPKAMP, 6):
  336. mask = AIC31XX_SPRDRVPWRSTATUS_MASK;
  337. break;
  338. case WIDGET_BIT(AIC31XX_ADCSETUP, 7):
  339. mask = AIC31XX_ADCPWRSTATUS_MASK;
  340. reg = AIC31XX_ADCFLAG;
  341. break;
  342. default:
  343. dev_err(w->codec->dev, "Unknown widget '%s' calling %s\n",
  344. w->name, __func__);
  345. return -EINVAL;
  346. }
  347. switch (event) {
  348. case SND_SOC_DAPM_POST_PMU:
  349. return aic31xx_wait_bits(aic31xx, reg, mask, mask, 5000, 100);
  350. case SND_SOC_DAPM_POST_PMD:
  351. return aic31xx_wait_bits(aic31xx, reg, mask, 0, 5000, 100);
  352. default:
  353. dev_dbg(w->codec->dev,
  354. "Unhandled dapm widget event %d from %s\n",
  355. event, w->name);
  356. }
  357. return 0;
  358. }
  359. static const struct snd_kcontrol_new left_output_switches[] = {
  360. SOC_DAPM_SINGLE("From Left DAC", AIC31XX_DACMIXERROUTE, 6, 1, 0),
  361. SOC_DAPM_SINGLE("From MIC1LP", AIC31XX_DACMIXERROUTE, 5, 1, 0),
  362. SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 4, 1, 0),
  363. };
  364. static const struct snd_kcontrol_new right_output_switches[] = {
  365. SOC_DAPM_SINGLE("From Right DAC", AIC31XX_DACMIXERROUTE, 2, 1, 0),
  366. SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 1, 1, 0),
  367. };
  368. static const struct snd_kcontrol_new p_term_mic1lp =
  369. SOC_DAPM_ENUM("MIC1LP P-Terminal", mic1lp_p_enum);
  370. static const struct snd_kcontrol_new p_term_mic1rp =
  371. SOC_DAPM_ENUM("MIC1RP P-Terminal", mic1rp_p_enum);
  372. static const struct snd_kcontrol_new p_term_mic1lm =
  373. SOC_DAPM_ENUM("MIC1LM P-Terminal", mic1lm_p_enum);
  374. static const struct snd_kcontrol_new m_term_mic1lm =
  375. SOC_DAPM_ENUM("MIC1LM M-Terminal", mic1lm_m_enum);
  376. static const struct snd_kcontrol_new aic31xx_dapm_hpl_switch =
  377. SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGHPL, 7, 1, 0);
  378. static const struct snd_kcontrol_new aic31xx_dapm_hpr_switch =
  379. SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGHPR, 7, 1, 0);
  380. static const struct snd_kcontrol_new aic31xx_dapm_spl_switch =
  381. SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGSPL, 7, 1, 0);
  382. static const struct snd_kcontrol_new aic31xx_dapm_spr_switch =
  383. SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGSPR, 7, 1, 0);
  384. static int mic_bias_event(struct snd_soc_dapm_widget *w,
  385. struct snd_kcontrol *kcontrol, int event)
  386. {
  387. struct snd_soc_codec *codec = w->codec;
  388. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  389. switch (event) {
  390. case SND_SOC_DAPM_POST_PMU:
  391. /* change mic bias voltage to user defined */
  392. snd_soc_update_bits(codec, AIC31XX_MICBIAS,
  393. AIC31XX_MICBIAS_MASK,
  394. aic31xx->pdata.micbias_vg <<
  395. AIC31XX_MICBIAS_SHIFT);
  396. dev_dbg(codec->dev, "%s: turned on\n", __func__);
  397. break;
  398. case SND_SOC_DAPM_PRE_PMD:
  399. /* turn mic bias off */
  400. snd_soc_update_bits(codec, AIC31XX_MICBIAS,
  401. AIC31XX_MICBIAS_MASK, 0);
  402. dev_dbg(codec->dev, "%s: turned off\n", __func__);
  403. break;
  404. }
  405. return 0;
  406. }
  407. static const struct snd_soc_dapm_widget aic31xx_dapm_widgets[] = {
  408. SND_SOC_DAPM_AIF_IN("DAC IN", "DAC Playback", 0, SND_SOC_NOPM, 0, 0),
  409. SND_SOC_DAPM_MUX("DAC Left Input",
  410. SND_SOC_NOPM, 0, 0, &ldac_in_control),
  411. SND_SOC_DAPM_MUX("DAC Right Input",
  412. SND_SOC_NOPM, 0, 0, &rdac_in_control),
  413. /* DACs */
  414. SND_SOC_DAPM_DAC_E("DAC Left", "Left Playback",
  415. AIC31XX_DACSETUP, 7, 0, aic31xx_dapm_power_event,
  416. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  417. SND_SOC_DAPM_DAC_E("DAC Right", "Right Playback",
  418. AIC31XX_DACSETUP, 6, 0, aic31xx_dapm_power_event,
  419. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  420. /* Output Mixers */
  421. SND_SOC_DAPM_MIXER("Output Left", SND_SOC_NOPM, 0, 0,
  422. left_output_switches,
  423. ARRAY_SIZE(left_output_switches)),
  424. SND_SOC_DAPM_MIXER("Output Right", SND_SOC_NOPM, 0, 0,
  425. right_output_switches,
  426. ARRAY_SIZE(right_output_switches)),
  427. SND_SOC_DAPM_SWITCH("HP Left", SND_SOC_NOPM, 0, 0,
  428. &aic31xx_dapm_hpl_switch),
  429. SND_SOC_DAPM_SWITCH("HP Right", SND_SOC_NOPM, 0, 0,
  430. &aic31xx_dapm_hpr_switch),
  431. /* Output drivers */
  432. SND_SOC_DAPM_OUT_DRV_E("HPL Driver", AIC31XX_HPDRIVER, 7, 0,
  433. NULL, 0, aic31xx_dapm_power_event,
  434. SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
  435. SND_SOC_DAPM_OUT_DRV_E("HPR Driver", AIC31XX_HPDRIVER, 6, 0,
  436. NULL, 0, aic31xx_dapm_power_event,
  437. SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
  438. /* ADC */
  439. SND_SOC_DAPM_ADC_E("ADC", "Capture", AIC31XX_ADCSETUP, 7, 0,
  440. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  441. SND_SOC_DAPM_POST_PMD),
  442. /* Input Selection to MIC_PGA */
  443. SND_SOC_DAPM_MUX("MIC1LP P-Terminal", SND_SOC_NOPM, 0, 0,
  444. &p_term_mic1lp),
  445. SND_SOC_DAPM_MUX("MIC1RP P-Terminal", SND_SOC_NOPM, 0, 0,
  446. &p_term_mic1rp),
  447. SND_SOC_DAPM_MUX("MIC1LM P-Terminal", SND_SOC_NOPM, 0, 0,
  448. &p_term_mic1lm),
  449. SND_SOC_DAPM_MUX("MIC1LM M-Terminal", SND_SOC_NOPM, 0, 0,
  450. &m_term_mic1lm),
  451. /* Enabling & Disabling MIC Gain Ctl */
  452. SND_SOC_DAPM_PGA("MIC_GAIN_CTL", AIC31XX_MICPGA,
  453. 7, 1, NULL, 0),
  454. /* Mic Bias */
  455. SND_SOC_DAPM_SUPPLY("MICBIAS", SND_SOC_NOPM, 0, 0, mic_bias_event,
  456. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  457. /* Outputs */
  458. SND_SOC_DAPM_OUTPUT("HPL"),
  459. SND_SOC_DAPM_OUTPUT("HPR"),
  460. /* Inputs */
  461. SND_SOC_DAPM_INPUT("MIC1LP"),
  462. SND_SOC_DAPM_INPUT("MIC1RP"),
  463. SND_SOC_DAPM_INPUT("MIC1LM"),
  464. };
  465. static const struct snd_soc_dapm_widget aic311x_dapm_widgets[] = {
  466. /* AIC3111 and AIC3110 have stereo class-D amplifier */
  467. SND_SOC_DAPM_OUT_DRV_E("SPL ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
  468. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  469. SND_SOC_DAPM_POST_PMD),
  470. SND_SOC_DAPM_OUT_DRV_E("SPR ClassD", AIC31XX_SPKAMP, 6, 0, NULL, 0,
  471. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  472. SND_SOC_DAPM_POST_PMD),
  473. SND_SOC_DAPM_SWITCH("Speaker Left", SND_SOC_NOPM, 0, 0,
  474. &aic31xx_dapm_spl_switch),
  475. SND_SOC_DAPM_SWITCH("Speaker Right", SND_SOC_NOPM, 0, 0,
  476. &aic31xx_dapm_spr_switch),
  477. SND_SOC_DAPM_OUTPUT("SPL"),
  478. SND_SOC_DAPM_OUTPUT("SPR"),
  479. };
  480. /* AIC3100 and AIC3120 have only mono class-D amplifier */
  481. static const struct snd_soc_dapm_widget aic310x_dapm_widgets[] = {
  482. SND_SOC_DAPM_OUT_DRV_E("SPK ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
  483. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  484. SND_SOC_DAPM_POST_PMD),
  485. SND_SOC_DAPM_SWITCH("Speaker", SND_SOC_NOPM, 0, 0,
  486. &aic31xx_dapm_spl_switch),
  487. SND_SOC_DAPM_OUTPUT("SPK"),
  488. };
  489. static const struct snd_soc_dapm_route
  490. aic31xx_audio_map[] = {
  491. /* DAC Input Routing */
  492. {"DAC Left Input", "Left Data", "DAC IN"},
  493. {"DAC Left Input", "Right Data", "DAC IN"},
  494. {"DAC Left Input", "Mono", "DAC IN"},
  495. {"DAC Right Input", "Left Data", "DAC IN"},
  496. {"DAC Right Input", "Right Data", "DAC IN"},
  497. {"DAC Right Input", "Mono", "DAC IN"},
  498. {"DAC Left", NULL, "DAC Left Input"},
  499. {"DAC Right", NULL, "DAC Right Input"},
  500. /* Mic input */
  501. {"MIC1LP P-Terminal", "FFR 10 Ohm", "MIC1LP"},
  502. {"MIC1LP P-Terminal", "FFR 20 Ohm", "MIC1LP"},
  503. {"MIC1LP P-Terminal", "FFR 40 Ohm", "MIC1LP"},
  504. {"MIC1RP P-Terminal", "FFR 10 Ohm", "MIC1RP"},
  505. {"MIC1RP P-Terminal", "FFR 20 Ohm", "MIC1RP"},
  506. {"MIC1RP P-Terminal", "FFR 40 Ohm", "MIC1RP"},
  507. {"MIC1LM P-Terminal", "FFR 10 Ohm", "MIC1LM"},
  508. {"MIC1LM P-Terminal", "FFR 20 Ohm", "MIC1LM"},
  509. {"MIC1LM P-Terminal", "FFR 40 Ohm", "MIC1LM"},
  510. {"MIC1LM M-Terminal", "FFR 10 Ohm", "MIC1LM"},
  511. {"MIC1LM M-Terminal", "FFR 20 Ohm", "MIC1LM"},
  512. {"MIC1LM M-Terminal", "FFR 40 Ohm", "MIC1LM"},
  513. {"MIC_GAIN_CTL", NULL, "MIC1LP P-Terminal"},
  514. {"MIC_GAIN_CTL", NULL, "MIC1RP P-Terminal"},
  515. {"MIC_GAIN_CTL", NULL, "MIC1LM P-Terminal"},
  516. {"MIC_GAIN_CTL", NULL, "MIC1LM M-Terminal"},
  517. {"ADC", NULL, "MIC_GAIN_CTL"},
  518. /* Left Output */
  519. {"Output Left", "From Left DAC", "DAC Left"},
  520. {"Output Left", "From MIC1LP", "MIC1LP"},
  521. {"Output Left", "From MIC1RP", "MIC1RP"},
  522. /* Right Output */
  523. {"Output Right", "From Right DAC", "DAC Right"},
  524. {"Output Right", "From MIC1RP", "MIC1RP"},
  525. /* HPL path */
  526. {"HP Left", "Switch", "Output Left"},
  527. {"HPL Driver", NULL, "HP Left"},
  528. {"HPL", NULL, "HPL Driver"},
  529. /* HPR path */
  530. {"HP Right", "Switch", "Output Right"},
  531. {"HPR Driver", NULL, "HP Right"},
  532. {"HPR", NULL, "HPR Driver"},
  533. };
  534. static const struct snd_soc_dapm_route
  535. aic311x_audio_map[] = {
  536. /* SP L path */
  537. {"Speaker Left", "Switch", "Output Left"},
  538. {"SPL ClassD", NULL, "Speaker Left"},
  539. {"SPL", NULL, "SPL ClassD"},
  540. /* SP R path */
  541. {"Speaker Right", "Switch", "Output Right"},
  542. {"SPR ClassD", NULL, "Speaker Right"},
  543. {"SPR", NULL, "SPR ClassD"},
  544. };
  545. static const struct snd_soc_dapm_route
  546. aic310x_audio_map[] = {
  547. /* SP L path */
  548. {"Speaker", "Switch", "Output Left"},
  549. {"SPK ClassD", NULL, "Speaker"},
  550. {"SPK", NULL, "SPK ClassD"},
  551. };
  552. static int aic31xx_add_controls(struct snd_soc_codec *codec)
  553. {
  554. int ret = 0;
  555. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  556. if (aic31xx->pdata.codec_type & AIC31XX_STEREO_CLASS_D_BIT)
  557. ret = snd_soc_add_codec_controls(
  558. codec, aic311x_snd_controls,
  559. ARRAY_SIZE(aic311x_snd_controls));
  560. else
  561. ret = snd_soc_add_codec_controls(
  562. codec, aic310x_snd_controls,
  563. ARRAY_SIZE(aic310x_snd_controls));
  564. return ret;
  565. }
  566. static int aic31xx_add_widgets(struct snd_soc_codec *codec)
  567. {
  568. struct snd_soc_dapm_context *dapm = &codec->dapm;
  569. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  570. int ret = 0;
  571. if (aic31xx->pdata.codec_type & AIC31XX_STEREO_CLASS_D_BIT) {
  572. ret = snd_soc_dapm_new_controls(
  573. dapm, aic311x_dapm_widgets,
  574. ARRAY_SIZE(aic311x_dapm_widgets));
  575. if (ret)
  576. return ret;
  577. ret = snd_soc_dapm_add_routes(dapm, aic311x_audio_map,
  578. ARRAY_SIZE(aic311x_audio_map));
  579. if (ret)
  580. return ret;
  581. } else {
  582. ret = snd_soc_dapm_new_controls(
  583. dapm, aic310x_dapm_widgets,
  584. ARRAY_SIZE(aic310x_dapm_widgets));
  585. if (ret)
  586. return ret;
  587. ret = snd_soc_dapm_add_routes(dapm, aic310x_audio_map,
  588. ARRAY_SIZE(aic310x_audio_map));
  589. if (ret)
  590. return ret;
  591. }
  592. return 0;
  593. }
  594. static int aic31xx_setup_pll(struct snd_soc_codec *codec,
  595. struct snd_pcm_hw_params *params)
  596. {
  597. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  598. int bclk_score = snd_soc_params_to_frame_size(params);
  599. int mclk_p = aic31xx->sysclk / aic31xx->p_div;
  600. int bclk_n = 0;
  601. int match = -1;
  602. int i;
  603. /* Use PLL as CODEC_CLKIN and DAC_CLK as BDIV_CLKIN */
  604. snd_soc_update_bits(codec, AIC31XX_CLKMUX,
  605. AIC31XX_CODEC_CLKIN_MASK, AIC31XX_CODEC_CLKIN_PLL);
  606. snd_soc_update_bits(codec, AIC31XX_IFACE2,
  607. AIC31XX_BDIVCLK_MASK, AIC31XX_DAC2BCLK);
  608. for (i = 0; i < ARRAY_SIZE(aic31xx_divs); i++) {
  609. if (aic31xx_divs[i].rate == params_rate(params) &&
  610. aic31xx_divs[i].mclk_p == mclk_p) {
  611. int s = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) %
  612. snd_soc_params_to_frame_size(params);
  613. int bn = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) /
  614. snd_soc_params_to_frame_size(params);
  615. if (s < bclk_score && bn > 0) {
  616. match = i;
  617. bclk_n = bn;
  618. bclk_score = s;
  619. }
  620. }
  621. }
  622. if (match == -1) {
  623. dev_err(codec->dev,
  624. "%s: Sample rate (%u) and format not supported\n",
  625. __func__, params_rate(params));
  626. /* See bellow for details how fix this. */
  627. return -EINVAL;
  628. }
  629. if (bclk_score != 0) {
  630. dev_warn(codec->dev, "Can not produce exact bitclock");
  631. /* This is fine if using dsp format, but if using i2s
  632. there may be trouble. To fix the issue edit the
  633. aic31xx_divs table for your mclk and sample
  634. rate. Details can be found from:
  635. http://www.ti.com/lit/ds/symlink/tlv320aic3100.pdf
  636. Section: 5.6 CLOCK Generation and PLL
  637. */
  638. }
  639. i = match;
  640. /* PLL configuration */
  641. snd_soc_update_bits(codec, AIC31XX_PLLPR, AIC31XX_PLL_MASK,
  642. (aic31xx->p_div << 4) | 0x01);
  643. snd_soc_write(codec, AIC31XX_PLLJ, aic31xx_divs[i].pll_j);
  644. snd_soc_write(codec, AIC31XX_PLLDMSB,
  645. aic31xx_divs[i].pll_d >> 8);
  646. snd_soc_write(codec, AIC31XX_PLLDLSB,
  647. aic31xx_divs[i].pll_d & 0xff);
  648. /* DAC dividers configuration */
  649. snd_soc_update_bits(codec, AIC31XX_NDAC, AIC31XX_PLL_MASK,
  650. aic31xx_divs[i].ndac);
  651. snd_soc_update_bits(codec, AIC31XX_MDAC, AIC31XX_PLL_MASK,
  652. aic31xx_divs[i].mdac);
  653. snd_soc_write(codec, AIC31XX_DOSRMSB, aic31xx_divs[i].dosr >> 8);
  654. snd_soc_write(codec, AIC31XX_DOSRLSB, aic31xx_divs[i].dosr & 0xff);
  655. /* ADC dividers configuration. Write reset value 1 if not used. */
  656. snd_soc_update_bits(codec, AIC31XX_NADC, AIC31XX_PLL_MASK,
  657. aic31xx_divs[i].nadc ? aic31xx_divs[i].nadc : 1);
  658. snd_soc_update_bits(codec, AIC31XX_MADC, AIC31XX_PLL_MASK,
  659. aic31xx_divs[i].madc ? aic31xx_divs[i].madc : 1);
  660. snd_soc_write(codec, AIC31XX_AOSR, aic31xx_divs[i].aosr);
  661. /* Bit clock divider configuration. */
  662. snd_soc_update_bits(codec, AIC31XX_BCLKN,
  663. AIC31XX_PLL_MASK, bclk_n);
  664. aic31xx->rate_div_line = i;
  665. dev_dbg(codec->dev,
  666. "pll %d.%04d/%d dosr %d n %d m %d aosr %d n %d m %d bclk_n %d\n",
  667. aic31xx_divs[i].pll_j, aic31xx_divs[i].pll_d,
  668. aic31xx->p_div, aic31xx_divs[i].dosr,
  669. aic31xx_divs[i].ndac, aic31xx_divs[i].mdac,
  670. aic31xx_divs[i].aosr, aic31xx_divs[i].nadc,
  671. aic31xx_divs[i].madc, bclk_n);
  672. return 0;
  673. }
  674. static int aic31xx_hw_params(struct snd_pcm_substream *substream,
  675. struct snd_pcm_hw_params *params,
  676. struct snd_soc_dai *dai)
  677. {
  678. struct snd_soc_codec *codec = dai->codec;
  679. u8 data = 0;
  680. dev_dbg(codec->dev, "## %s: width %d rate %d\n",
  681. __func__, params_width(params),
  682. params_rate(params));
  683. switch (params_width(params)) {
  684. case 16:
  685. break;
  686. case 20:
  687. data = (AIC31XX_WORD_LEN_20BITS <<
  688. AIC31XX_IFACE1_DATALEN_SHIFT);
  689. break;
  690. case 24:
  691. data = (AIC31XX_WORD_LEN_24BITS <<
  692. AIC31XX_IFACE1_DATALEN_SHIFT);
  693. break;
  694. case 32:
  695. data = (AIC31XX_WORD_LEN_32BITS <<
  696. AIC31XX_IFACE1_DATALEN_SHIFT);
  697. break;
  698. default:
  699. dev_err(codec->dev, "%s: Unsupported width %d\n",
  700. __func__, params_width(params));
  701. return -EINVAL;
  702. }
  703. snd_soc_update_bits(codec, AIC31XX_IFACE1,
  704. AIC31XX_IFACE1_DATALEN_MASK,
  705. data);
  706. return aic31xx_setup_pll(codec, params);
  707. }
  708. static int aic31xx_dac_mute(struct snd_soc_dai *codec_dai, int mute)
  709. {
  710. struct snd_soc_codec *codec = codec_dai->codec;
  711. if (mute) {
  712. snd_soc_update_bits(codec, AIC31XX_DACMUTE,
  713. AIC31XX_DACMUTE_MASK,
  714. AIC31XX_DACMUTE_MASK);
  715. } else {
  716. snd_soc_update_bits(codec, AIC31XX_DACMUTE,
  717. AIC31XX_DACMUTE_MASK, 0x0);
  718. }
  719. return 0;
  720. }
  721. static int aic31xx_set_dai_fmt(struct snd_soc_dai *codec_dai,
  722. unsigned int fmt)
  723. {
  724. struct snd_soc_codec *codec = codec_dai->codec;
  725. u8 iface_reg1 = 0;
  726. u8 iface_reg2 = 0;
  727. u8 dsp_a_val = 0;
  728. dev_dbg(codec->dev, "## %s: fmt = 0x%x\n", __func__, fmt);
  729. /* set master/slave audio interface */
  730. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  731. case SND_SOC_DAIFMT_CBM_CFM:
  732. iface_reg1 |= AIC31XX_BCLK_MASTER | AIC31XX_WCLK_MASTER;
  733. break;
  734. default:
  735. dev_alert(codec->dev, "Invalid DAI master/slave interface\n");
  736. return -EINVAL;
  737. }
  738. /* interface format */
  739. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  740. case SND_SOC_DAIFMT_I2S:
  741. break;
  742. case SND_SOC_DAIFMT_DSP_A:
  743. dsp_a_val = 0x1;
  744. case SND_SOC_DAIFMT_DSP_B:
  745. /* NOTE: BCLKINV bit value 1 equas NB and 0 equals IB */
  746. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  747. case SND_SOC_DAIFMT_NB_NF:
  748. iface_reg2 |= AIC31XX_BCLKINV_MASK;
  749. break;
  750. case SND_SOC_DAIFMT_IB_NF:
  751. break;
  752. default:
  753. return -EINVAL;
  754. }
  755. iface_reg1 |= (AIC31XX_DSP_MODE <<
  756. AIC31XX_IFACE1_DATATYPE_SHIFT);
  757. break;
  758. case SND_SOC_DAIFMT_RIGHT_J:
  759. iface_reg1 |= (AIC31XX_RIGHT_JUSTIFIED_MODE <<
  760. AIC31XX_IFACE1_DATATYPE_SHIFT);
  761. break;
  762. case SND_SOC_DAIFMT_LEFT_J:
  763. iface_reg1 |= (AIC31XX_LEFT_JUSTIFIED_MODE <<
  764. AIC31XX_IFACE1_DATATYPE_SHIFT);
  765. break;
  766. default:
  767. dev_err(codec->dev, "Invalid DAI interface format\n");
  768. return -EINVAL;
  769. }
  770. snd_soc_update_bits(codec, AIC31XX_IFACE1,
  771. AIC31XX_IFACE1_DATATYPE_MASK |
  772. AIC31XX_IFACE1_MASTER_MASK,
  773. iface_reg1);
  774. snd_soc_update_bits(codec, AIC31XX_DATA_OFFSET,
  775. AIC31XX_DATA_OFFSET_MASK,
  776. dsp_a_val);
  777. snd_soc_update_bits(codec, AIC31XX_IFACE2,
  778. AIC31XX_BCLKINV_MASK,
  779. iface_reg2);
  780. return 0;
  781. }
  782. static int aic31xx_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  783. int clk_id, unsigned int freq, int dir)
  784. {
  785. struct snd_soc_codec *codec = codec_dai->codec;
  786. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  787. int i;
  788. dev_dbg(codec->dev, "## %s: clk_id = %d, freq = %d, dir = %d\n",
  789. __func__, clk_id, freq, dir);
  790. for (i = 1; freq/i > 20000000 && i < 8; i++)
  791. ;
  792. if (freq/i > 20000000) {
  793. dev_err(aic31xx->dev, "%s: Too high mclk frequency %u\n",
  794. __func__, freq);
  795. return -EINVAL;
  796. }
  797. aic31xx->p_div = i;
  798. for (i = 0; i < ARRAY_SIZE(aic31xx_divs) &&
  799. aic31xx_divs[i].mclk_p != freq/aic31xx->p_div; i++)
  800. ;
  801. if (i == ARRAY_SIZE(aic31xx_divs)) {
  802. dev_err(aic31xx->dev, "%s: Unsupported frequency %d\n",
  803. __func__, freq);
  804. return -EINVAL;
  805. }
  806. /* set clock on MCLK, BCLK, or GPIO1 as PLL input */
  807. snd_soc_update_bits(codec, AIC31XX_CLKMUX, AIC31XX_PLL_CLKIN_MASK,
  808. clk_id << AIC31XX_PLL_CLKIN_SHIFT);
  809. aic31xx->sysclk = freq;
  810. return 0;
  811. }
  812. static int aic31xx_regulator_event(struct notifier_block *nb,
  813. unsigned long event, void *data)
  814. {
  815. struct aic31xx_disable_nb *disable_nb =
  816. container_of(nb, struct aic31xx_disable_nb, nb);
  817. struct aic31xx_priv *aic31xx = disable_nb->aic31xx;
  818. if (event & REGULATOR_EVENT_DISABLE) {
  819. /*
  820. * Put codec to reset and as at least one of the
  821. * supplies was disabled.
  822. */
  823. if (gpio_is_valid(aic31xx->pdata.gpio_reset))
  824. gpio_set_value(aic31xx->pdata.gpio_reset, 0);
  825. regcache_mark_dirty(aic31xx->regmap);
  826. dev_dbg(aic31xx->dev, "## %s: DISABLE received\n", __func__);
  827. }
  828. return 0;
  829. }
  830. static void aic31xx_clk_on(struct snd_soc_codec *codec)
  831. {
  832. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  833. u8 mask = AIC31XX_PM_MASK;
  834. u8 on = AIC31XX_PM_MASK;
  835. dev_dbg(codec->dev, "codec clock -> on (rate %d)\n",
  836. aic31xx_divs[aic31xx->rate_div_line].rate);
  837. snd_soc_update_bits(codec, AIC31XX_PLLPR, mask, on);
  838. mdelay(10);
  839. snd_soc_update_bits(codec, AIC31XX_NDAC, mask, on);
  840. snd_soc_update_bits(codec, AIC31XX_MDAC, mask, on);
  841. if (aic31xx_divs[aic31xx->rate_div_line].nadc)
  842. snd_soc_update_bits(codec, AIC31XX_NADC, mask, on);
  843. if (aic31xx_divs[aic31xx->rate_div_line].madc)
  844. snd_soc_update_bits(codec, AIC31XX_MADC, mask, on);
  845. snd_soc_update_bits(codec, AIC31XX_BCLKN, mask, on);
  846. }
  847. static void aic31xx_clk_off(struct snd_soc_codec *codec)
  848. {
  849. u8 mask = AIC31XX_PM_MASK;
  850. u8 off = 0;
  851. dev_dbg(codec->dev, "codec clock -> off\n");
  852. snd_soc_update_bits(codec, AIC31XX_BCLKN, mask, off);
  853. snd_soc_update_bits(codec, AIC31XX_MADC, mask, off);
  854. snd_soc_update_bits(codec, AIC31XX_NADC, mask, off);
  855. snd_soc_update_bits(codec, AIC31XX_MDAC, mask, off);
  856. snd_soc_update_bits(codec, AIC31XX_NDAC, mask, off);
  857. snd_soc_update_bits(codec, AIC31XX_PLLPR, mask, off);
  858. }
  859. static int aic31xx_power_on(struct snd_soc_codec *codec)
  860. {
  861. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  862. int ret = 0;
  863. ret = regulator_bulk_enable(ARRAY_SIZE(aic31xx->supplies),
  864. aic31xx->supplies);
  865. if (ret)
  866. return ret;
  867. if (gpio_is_valid(aic31xx->pdata.gpio_reset)) {
  868. gpio_set_value(aic31xx->pdata.gpio_reset, 1);
  869. udelay(100);
  870. }
  871. regcache_cache_only(aic31xx->regmap, false);
  872. ret = regcache_sync(aic31xx->regmap);
  873. if (ret != 0) {
  874. dev_err(codec->dev,
  875. "Failed to restore cache: %d\n", ret);
  876. regcache_cache_only(aic31xx->regmap, true);
  877. regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
  878. aic31xx->supplies);
  879. return ret;
  880. }
  881. return 0;
  882. }
  883. static int aic31xx_power_off(struct snd_soc_codec *codec)
  884. {
  885. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  886. int ret = 0;
  887. regcache_cache_only(aic31xx->regmap, true);
  888. ret = regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
  889. aic31xx->supplies);
  890. return ret;
  891. }
  892. static int aic31xx_set_bias_level(struct snd_soc_codec *codec,
  893. enum snd_soc_bias_level level)
  894. {
  895. dev_dbg(codec->dev, "## %s: %d -> %d\n", __func__,
  896. codec->dapm.bias_level, level);
  897. switch (level) {
  898. case SND_SOC_BIAS_ON:
  899. break;
  900. case SND_SOC_BIAS_PREPARE:
  901. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
  902. aic31xx_clk_on(codec);
  903. break;
  904. case SND_SOC_BIAS_STANDBY:
  905. switch (codec->dapm.bias_level) {
  906. case SND_SOC_BIAS_OFF:
  907. aic31xx_power_on(codec);
  908. break;
  909. case SND_SOC_BIAS_PREPARE:
  910. aic31xx_clk_off(codec);
  911. break;
  912. default:
  913. BUG();
  914. }
  915. break;
  916. case SND_SOC_BIAS_OFF:
  917. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
  918. aic31xx_power_off(codec);
  919. break;
  920. }
  921. codec->dapm.bias_level = level;
  922. return 0;
  923. }
  924. static int aic31xx_suspend(struct snd_soc_codec *codec)
  925. {
  926. aic31xx_set_bias_level(codec, SND_SOC_BIAS_OFF);
  927. return 0;
  928. }
  929. static int aic31xx_resume(struct snd_soc_codec *codec)
  930. {
  931. aic31xx_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  932. return 0;
  933. }
  934. static int aic31xx_codec_probe(struct snd_soc_codec *codec)
  935. {
  936. int ret = 0;
  937. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  938. int i;
  939. dev_dbg(aic31xx->dev, "## %s\n", __func__);
  940. aic31xx = snd_soc_codec_get_drvdata(codec);
  941. aic31xx->codec = codec;
  942. for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++) {
  943. aic31xx->disable_nb[i].nb.notifier_call =
  944. aic31xx_regulator_event;
  945. aic31xx->disable_nb[i].aic31xx = aic31xx;
  946. ret = regulator_register_notifier(aic31xx->supplies[i].consumer,
  947. &aic31xx->disable_nb[i].nb);
  948. if (ret) {
  949. dev_err(codec->dev,
  950. "Failed to request regulator notifier: %d\n",
  951. ret);
  952. return ret;
  953. }
  954. }
  955. regcache_cache_only(aic31xx->regmap, true);
  956. regcache_mark_dirty(aic31xx->regmap);
  957. ret = aic31xx_add_controls(codec);
  958. if (ret)
  959. return ret;
  960. ret = aic31xx_add_widgets(codec);
  961. return ret;
  962. }
  963. static int aic31xx_codec_remove(struct snd_soc_codec *codec)
  964. {
  965. struct aic31xx_priv *aic31xx = snd_soc_codec_get_drvdata(codec);
  966. int i;
  967. /* power down chip */
  968. aic31xx_set_bias_level(codec, SND_SOC_BIAS_OFF);
  969. for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
  970. regulator_unregister_notifier(aic31xx->supplies[i].consumer,
  971. &aic31xx->disable_nb[i].nb);
  972. return 0;
  973. }
  974. static struct snd_soc_codec_driver soc_codec_driver_aic31xx = {
  975. .probe = aic31xx_codec_probe,
  976. .remove = aic31xx_codec_remove,
  977. .suspend = aic31xx_suspend,
  978. .resume = aic31xx_resume,
  979. .set_bias_level = aic31xx_set_bias_level,
  980. .controls = aic31xx_snd_controls,
  981. .num_controls = ARRAY_SIZE(aic31xx_snd_controls),
  982. .dapm_widgets = aic31xx_dapm_widgets,
  983. .num_dapm_widgets = ARRAY_SIZE(aic31xx_dapm_widgets),
  984. .dapm_routes = aic31xx_audio_map,
  985. .num_dapm_routes = ARRAY_SIZE(aic31xx_audio_map),
  986. };
  987. static struct snd_soc_dai_ops aic31xx_dai_ops = {
  988. .hw_params = aic31xx_hw_params,
  989. .set_sysclk = aic31xx_set_dai_sysclk,
  990. .set_fmt = aic31xx_set_dai_fmt,
  991. .digital_mute = aic31xx_dac_mute,
  992. };
  993. static struct snd_soc_dai_driver aic31xx_dai_driver[] = {
  994. {
  995. .name = "tlv320aic31xx-hifi",
  996. .playback = {
  997. .stream_name = "Playback",
  998. .channels_min = 1,
  999. .channels_max = 2,
  1000. .rates = AIC31XX_RATES,
  1001. .formats = AIC31XX_FORMATS,
  1002. },
  1003. .capture = {
  1004. .stream_name = "Capture",
  1005. .channels_min = 1,
  1006. .channels_max = 2,
  1007. .rates = AIC31XX_RATES,
  1008. .formats = AIC31XX_FORMATS,
  1009. },
  1010. .ops = &aic31xx_dai_ops,
  1011. .symmetric_rates = 1,
  1012. }
  1013. };
  1014. #if defined(CONFIG_OF)
  1015. static const struct of_device_id tlv320aic31xx_of_match[] = {
  1016. { .compatible = "ti,tlv320aic310x" },
  1017. { .compatible = "ti,tlv320aic311x" },
  1018. { .compatible = "ti,tlv320aic3100" },
  1019. { .compatible = "ti,tlv320aic3110" },
  1020. { .compatible = "ti,tlv320aic3120" },
  1021. { .compatible = "ti,tlv320aic3111" },
  1022. {},
  1023. };
  1024. MODULE_DEVICE_TABLE(of, tlv320aic31xx_of_match);
  1025. static void aic31xx_pdata_from_of(struct aic31xx_priv *aic31xx)
  1026. {
  1027. struct device_node *np = aic31xx->dev->of_node;
  1028. unsigned int value = MICBIAS_2_0V;
  1029. int ret;
  1030. of_property_read_u32(np, "ai31xx-micbias-vg", &value);
  1031. switch (value) {
  1032. case MICBIAS_2_0V:
  1033. case MICBIAS_2_5V:
  1034. case MICBIAS_AVDDV:
  1035. aic31xx->pdata.micbias_vg = value;
  1036. break;
  1037. default:
  1038. dev_err(aic31xx->dev,
  1039. "Bad ai31xx-micbias-vg value %d DT\n",
  1040. value);
  1041. aic31xx->pdata.micbias_vg = MICBIAS_2_0V;
  1042. }
  1043. ret = of_get_named_gpio(np, "gpio-reset", 0);
  1044. if (ret > 0)
  1045. aic31xx->pdata.gpio_reset = ret;
  1046. }
  1047. #else /* CONFIG_OF */
  1048. static void aic31xx_pdata_from_of(struct aic31xx_priv *aic31xx)
  1049. {
  1050. }
  1051. #endif /* CONFIG_OF */
  1052. static int aic31xx_device_init(struct aic31xx_priv *aic31xx)
  1053. {
  1054. int ret, i;
  1055. dev_set_drvdata(aic31xx->dev, aic31xx);
  1056. if (dev_get_platdata(aic31xx->dev))
  1057. memcpy(&aic31xx->pdata, dev_get_platdata(aic31xx->dev),
  1058. sizeof(aic31xx->pdata));
  1059. else if (aic31xx->dev->of_node)
  1060. aic31xx_pdata_from_of(aic31xx);
  1061. if (aic31xx->pdata.gpio_reset) {
  1062. ret = devm_gpio_request_one(aic31xx->dev,
  1063. aic31xx->pdata.gpio_reset,
  1064. GPIOF_OUT_INIT_HIGH,
  1065. "aic31xx-reset-pin");
  1066. if (ret < 0) {
  1067. dev_err(aic31xx->dev, "not able to acquire gpio\n");
  1068. return ret;
  1069. }
  1070. }
  1071. for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
  1072. aic31xx->supplies[i].supply = aic31xx_supply_names[i];
  1073. ret = devm_regulator_bulk_get(aic31xx->dev,
  1074. ARRAY_SIZE(aic31xx->supplies),
  1075. aic31xx->supplies);
  1076. if (ret != 0)
  1077. dev_err(aic31xx->dev, "Failed to request supplies: %d\n", ret);
  1078. return ret;
  1079. }
  1080. static int aic31xx_i2c_probe(struct i2c_client *i2c,
  1081. const struct i2c_device_id *id)
  1082. {
  1083. struct aic31xx_priv *aic31xx;
  1084. int ret;
  1085. const struct regmap_config *regmap_config;
  1086. dev_dbg(&i2c->dev, "## %s: %s codec_type = %d\n", __func__,
  1087. id->name, (int) id->driver_data);
  1088. regmap_config = &aic31xx_i2c_regmap;
  1089. aic31xx = devm_kzalloc(&i2c->dev, sizeof(*aic31xx), GFP_KERNEL);
  1090. if (aic31xx == NULL)
  1091. return -ENOMEM;
  1092. aic31xx->regmap = devm_regmap_init_i2c(i2c, regmap_config);
  1093. if (IS_ERR(aic31xx->regmap)) {
  1094. ret = PTR_ERR(aic31xx->regmap);
  1095. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1096. ret);
  1097. return ret;
  1098. }
  1099. aic31xx->dev = &i2c->dev;
  1100. aic31xx->pdata.codec_type = id->driver_data;
  1101. ret = aic31xx_device_init(aic31xx);
  1102. if (ret)
  1103. return ret;
  1104. return snd_soc_register_codec(&i2c->dev, &soc_codec_driver_aic31xx,
  1105. aic31xx_dai_driver,
  1106. ARRAY_SIZE(aic31xx_dai_driver));
  1107. }
  1108. static int aic31xx_i2c_remove(struct i2c_client *i2c)
  1109. {
  1110. snd_soc_unregister_codec(&i2c->dev);
  1111. return 0;
  1112. }
  1113. static const struct i2c_device_id aic31xx_i2c_id[] = {
  1114. { "tlv320aic310x", AIC3100 },
  1115. { "tlv320aic311x", AIC3110 },
  1116. { "tlv320aic3100", AIC3100 },
  1117. { "tlv320aic3110", AIC3110 },
  1118. { "tlv320aic3120", AIC3120 },
  1119. { "tlv320aic3111", AIC3111 },
  1120. { }
  1121. };
  1122. MODULE_DEVICE_TABLE(i2c, aic31xx_i2c_id);
  1123. static struct i2c_driver aic31xx_i2c_driver = {
  1124. .driver = {
  1125. .name = "tlv320aic31xx-codec",
  1126. .owner = THIS_MODULE,
  1127. .of_match_table = of_match_ptr(tlv320aic31xx_of_match),
  1128. },
  1129. .probe = aic31xx_i2c_probe,
  1130. .remove = aic31xx_i2c_remove,
  1131. .id_table = aic31xx_i2c_id,
  1132. };
  1133. module_i2c_driver(aic31xx_i2c_driver);
  1134. MODULE_DESCRIPTION("ASoC TLV320AIC3111 codec driver");
  1135. MODULE_AUTHOR("Jyri Sarha");
  1136. MODULE_LICENSE("GPL");