tlv320aic31xx.c 40 KB

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