tlv320aic31xx.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ALSA SoC TLV320AIC31xx CODEC Driver
  4. *
  5. * Copyright (C) 2014-2017 Texas Instruments Incorporated - https://www.ti.com/
  6. * Jyri Sarha <[email protected]>
  7. *
  8. * Based on ground work by: Ajit Kulkarni <[email protected]>
  9. *
  10. * The TLV320AIC31xx series of audio codecs are low-power, highly integrated
  11. * high performance codecs which provides a stereo DAC, a mono ADC,
  12. * and mono/stereo Class-D speaker driver.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/pm.h>
  20. #include <linux/i2c.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/acpi.h>
  24. #include <linux/of.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/slab.h>
  27. #include <sound/core.h>
  28. #include <sound/jack.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/soc.h>
  32. #include <sound/initval.h>
  33. #include <sound/tlv.h>
  34. #include <dt-bindings/sound/tlv320aic31xx.h>
  35. #include "tlv320aic31xx.h"
  36. static int aic31xx_set_jack(struct snd_soc_component *component,
  37. struct snd_soc_jack *jack, void *data);
  38. static const struct reg_default aic31xx_reg_defaults[] = {
  39. { AIC31XX_CLKMUX, 0x00 },
  40. { AIC31XX_PLLPR, 0x11 },
  41. { AIC31XX_PLLJ, 0x04 },
  42. { AIC31XX_PLLDMSB, 0x00 },
  43. { AIC31XX_PLLDLSB, 0x00 },
  44. { AIC31XX_NDAC, 0x01 },
  45. { AIC31XX_MDAC, 0x01 },
  46. { AIC31XX_DOSRMSB, 0x00 },
  47. { AIC31XX_DOSRLSB, 0x80 },
  48. { AIC31XX_NADC, 0x01 },
  49. { AIC31XX_MADC, 0x01 },
  50. { AIC31XX_AOSR, 0x80 },
  51. { AIC31XX_IFACE1, 0x00 },
  52. { AIC31XX_DATA_OFFSET, 0x00 },
  53. { AIC31XX_IFACE2, 0x00 },
  54. { AIC31XX_BCLKN, 0x01 },
  55. { AIC31XX_DACSETUP, 0x14 },
  56. { AIC31XX_DACMUTE, 0x0c },
  57. { AIC31XX_LDACVOL, 0x00 },
  58. { AIC31XX_RDACVOL, 0x00 },
  59. { AIC31XX_ADCSETUP, 0x00 },
  60. { AIC31XX_ADCFGA, 0x80 },
  61. { AIC31XX_ADCVOL, 0x00 },
  62. { AIC31XX_HPDRIVER, 0x04 },
  63. { AIC31XX_SPKAMP, 0x06 },
  64. { AIC31XX_DACMIXERROUTE, 0x00 },
  65. { AIC31XX_LANALOGHPL, 0x7f },
  66. { AIC31XX_RANALOGHPR, 0x7f },
  67. { AIC31XX_LANALOGSPL, 0x7f },
  68. { AIC31XX_RANALOGSPR, 0x7f },
  69. { AIC31XX_HPLGAIN, 0x02 },
  70. { AIC31XX_HPRGAIN, 0x02 },
  71. { AIC31XX_SPLGAIN, 0x00 },
  72. { AIC31XX_SPRGAIN, 0x00 },
  73. { AIC31XX_MICBIAS, 0x00 },
  74. { AIC31XX_MICPGA, 0x80 },
  75. { AIC31XX_MICPGAPI, 0x00 },
  76. { AIC31XX_MICPGAMI, 0x00 },
  77. };
  78. static bool aic31xx_volatile(struct device *dev, unsigned int reg)
  79. {
  80. switch (reg) {
  81. case AIC31XX_PAGECTL: /* regmap implementation requires this */
  82. case AIC31XX_RESET: /* always clears after write */
  83. case AIC31XX_OT_FLAG:
  84. case AIC31XX_ADCFLAG:
  85. case AIC31XX_DACFLAG1:
  86. case AIC31XX_DACFLAG2:
  87. case AIC31XX_OFFLAG: /* Sticky interrupt flags */
  88. case AIC31XX_INTRDACFLAG: /* Sticky interrupt flags */
  89. case AIC31XX_INTRADCFLAG: /* Sticky interrupt flags */
  90. case AIC31XX_INTRDACFLAG2:
  91. case AIC31XX_INTRADCFLAG2:
  92. case AIC31XX_HSDETECT:
  93. return true;
  94. }
  95. return false;
  96. }
  97. static bool aic31xx_writeable(struct device *dev, unsigned int reg)
  98. {
  99. switch (reg) {
  100. case AIC31XX_OT_FLAG:
  101. case AIC31XX_ADCFLAG:
  102. case AIC31XX_DACFLAG1:
  103. case AIC31XX_DACFLAG2:
  104. case AIC31XX_OFFLAG: /* Sticky interrupt flags */
  105. case AIC31XX_INTRDACFLAG: /* Sticky interrupt flags */
  106. case AIC31XX_INTRADCFLAG: /* Sticky interrupt flags */
  107. case AIC31XX_INTRDACFLAG2:
  108. case AIC31XX_INTRADCFLAG2:
  109. return false;
  110. }
  111. return true;
  112. }
  113. static const struct regmap_range_cfg aic31xx_ranges[] = {
  114. {
  115. .range_min = 0,
  116. .range_max = 12 * 128,
  117. .selector_reg = AIC31XX_PAGECTL,
  118. .selector_mask = 0xff,
  119. .selector_shift = 0,
  120. .window_start = 0,
  121. .window_len = 128,
  122. },
  123. };
  124. static const struct regmap_config aic31xx_i2c_regmap = {
  125. .reg_bits = 8,
  126. .val_bits = 8,
  127. .writeable_reg = aic31xx_writeable,
  128. .volatile_reg = aic31xx_volatile,
  129. .reg_defaults = aic31xx_reg_defaults,
  130. .num_reg_defaults = ARRAY_SIZE(aic31xx_reg_defaults),
  131. .cache_type = REGCACHE_RBTREE,
  132. .ranges = aic31xx_ranges,
  133. .num_ranges = ARRAY_SIZE(aic31xx_ranges),
  134. .max_register = 12 * 128,
  135. };
  136. static const char * const aic31xx_supply_names[] = {
  137. "HPVDD",
  138. "SPRVDD",
  139. "SPLVDD",
  140. "AVDD",
  141. "IOVDD",
  142. "DVDD",
  143. };
  144. #define AIC31XX_NUM_SUPPLIES ARRAY_SIZE(aic31xx_supply_names)
  145. struct aic31xx_disable_nb {
  146. struct notifier_block nb;
  147. struct aic31xx_priv *aic31xx;
  148. };
  149. struct aic31xx_priv {
  150. struct snd_soc_component *component;
  151. u8 i2c_regs_status;
  152. struct device *dev;
  153. struct regmap *regmap;
  154. enum aic31xx_type codec_type;
  155. struct gpio_desc *gpio_reset;
  156. int micbias_vg;
  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. struct snd_soc_jack *jack;
  161. u32 sysclk_id;
  162. unsigned int sysclk;
  163. u8 p_div;
  164. int rate_div_line;
  165. bool master_dapm_route_applied;
  166. int irq;
  167. u8 ocmv; /* output common-mode voltage */
  168. };
  169. struct aic31xx_rate_divs {
  170. u32 mclk_p;
  171. u32 rate;
  172. u8 pll_r;
  173. u8 pll_j;
  174. u16 pll_d;
  175. u16 dosr;
  176. u8 ndac;
  177. u8 mdac;
  178. u8 aosr;
  179. u8 nadc;
  180. u8 madc;
  181. };
  182. /* ADC dividers can be disabled by configuring them to 0 */
  183. static const struct aic31xx_rate_divs aic31xx_divs[] = {
  184. /* mclk/p rate pll: r j d dosr ndac mdac aors nadc madc */
  185. /* 8k rate */
  186. { 512000, 8000, 4, 48, 0, 128, 48, 2, 128, 48, 2},
  187. {12000000, 8000, 1, 8, 1920, 128, 48, 2, 128, 48, 2},
  188. {12000000, 8000, 1, 8, 1920, 128, 32, 3, 128, 32, 3},
  189. {12500000, 8000, 1, 7, 8643, 128, 48, 2, 128, 48, 2},
  190. /* 11.025k rate */
  191. { 705600, 11025, 3, 48, 0, 128, 24, 3, 128, 24, 3},
  192. {12000000, 11025, 1, 7, 5264, 128, 32, 2, 128, 32, 2},
  193. {12000000, 11025, 1, 8, 4672, 128, 24, 3, 128, 24, 3},
  194. {12500000, 11025, 1, 7, 2253, 128, 32, 2, 128, 32, 2},
  195. /* 16k rate */
  196. { 512000, 16000, 4, 48, 0, 128, 16, 3, 128, 16, 3},
  197. { 1024000, 16000, 2, 48, 0, 128, 16, 3, 128, 16, 3},
  198. {12000000, 16000, 1, 8, 1920, 128, 24, 2, 128, 24, 2},
  199. {12000000, 16000, 1, 8, 1920, 128, 16, 3, 128, 16, 3},
  200. {12500000, 16000, 1, 7, 8643, 128, 24, 2, 128, 24, 2},
  201. /* 22.05k rate */
  202. { 705600, 22050, 4, 36, 0, 128, 12, 3, 128, 12, 3},
  203. { 1411200, 22050, 2, 36, 0, 128, 12, 3, 128, 12, 3},
  204. {12000000, 22050, 1, 7, 5264, 128, 16, 2, 128, 16, 2},
  205. {12000000, 22050, 1, 8, 4672, 128, 12, 3, 128, 12, 3},
  206. {12500000, 22050, 1, 7, 2253, 128, 16, 2, 128, 16, 2},
  207. /* 32k rate */
  208. { 1024000, 32000, 2, 48, 0, 128, 12, 2, 128, 12, 2},
  209. { 2048000, 32000, 1, 48, 0, 128, 12, 2, 128, 12, 2},
  210. {12000000, 32000, 1, 8, 1920, 128, 12, 2, 128, 12, 2},
  211. {12000000, 32000, 1, 8, 1920, 128, 8, 3, 128, 8, 3},
  212. {12500000, 32000, 1, 7, 8643, 128, 12, 2, 128, 12, 2},
  213. /* 44.1k rate */
  214. { 1411200, 44100, 2, 32, 0, 128, 8, 2, 128, 8, 2},
  215. { 2822400, 44100, 1, 32, 0, 128, 8, 2, 128, 8, 2},
  216. {12000000, 44100, 1, 7, 5264, 128, 8, 2, 128, 8, 2},
  217. {12000000, 44100, 1, 8, 4672, 128, 6, 3, 128, 6, 3},
  218. {12500000, 44100, 1, 7, 2253, 128, 8, 2, 128, 8, 2},
  219. /* 48k rate */
  220. { 1536000, 48000, 2, 32, 0, 128, 8, 2, 128, 8, 2},
  221. { 3072000, 48000, 1, 32, 0, 128, 8, 2, 128, 8, 2},
  222. {12000000, 48000, 1, 8, 1920, 128, 8, 2, 128, 8, 2},
  223. {12000000, 48000, 1, 7, 6800, 96, 5, 4, 96, 5, 4},
  224. {12500000, 48000, 1, 7, 8643, 128, 8, 2, 128, 8, 2},
  225. /* 88.2k rate */
  226. { 2822400, 88200, 2, 16, 0, 64, 8, 2, 64, 8, 2},
  227. { 5644800, 88200, 1, 16, 0, 64, 8, 2, 64, 8, 2},
  228. {12000000, 88200, 1, 7, 5264, 64, 8, 2, 64, 8, 2},
  229. {12000000, 88200, 1, 8, 4672, 64, 6, 3, 64, 6, 3},
  230. {12500000, 88200, 1, 7, 2253, 64, 8, 2, 64, 8, 2},
  231. /* 96k rate */
  232. { 3072000, 96000, 2, 16, 0, 64, 8, 2, 64, 8, 2},
  233. { 6144000, 96000, 1, 16, 0, 64, 8, 2, 64, 8, 2},
  234. {12000000, 96000, 1, 8, 1920, 64, 8, 2, 64, 8, 2},
  235. {12000000, 96000, 1, 7, 6800, 48, 5, 4, 48, 5, 4},
  236. {12500000, 96000, 1, 7, 8643, 64, 8, 2, 64, 8, 2},
  237. /* 176.4k rate */
  238. { 5644800, 176400, 2, 8, 0, 32, 8, 2, 32, 8, 2},
  239. {11289600, 176400, 1, 8, 0, 32, 8, 2, 32, 8, 2},
  240. {12000000, 176400, 1, 7, 5264, 32, 8, 2, 32, 8, 2},
  241. {12000000, 176400, 1, 8, 4672, 32, 6, 3, 32, 6, 3},
  242. {12500000, 176400, 1, 7, 2253, 32, 8, 2, 32, 8, 2},
  243. /* 192k rate */
  244. { 6144000, 192000, 2, 8, 0, 32, 8, 2, 32, 8, 2},
  245. {12288000, 192000, 1, 8, 0, 32, 8, 2, 32, 8, 2},
  246. {12000000, 192000, 1, 8, 1920, 32, 8, 2, 32, 8, 2},
  247. {12000000, 192000, 1, 7, 6800, 24, 5, 4, 24, 5, 4},
  248. {12500000, 192000, 1, 7, 8643, 32, 8, 2, 32, 8, 2},
  249. };
  250. static const char * const ldac_in_text[] = {
  251. "Off", "Left Data", "Right Data", "Mono"
  252. };
  253. static const char * const rdac_in_text[] = {
  254. "Off", "Right Data", "Left Data", "Mono"
  255. };
  256. static SOC_ENUM_SINGLE_DECL(ldac_in_enum, AIC31XX_DACSETUP, 4, ldac_in_text);
  257. static SOC_ENUM_SINGLE_DECL(rdac_in_enum, AIC31XX_DACSETUP, 2, rdac_in_text);
  258. static const char * const mic_select_text[] = {
  259. "Off", "FFR 10 Ohm", "FFR 20 Ohm", "FFR 40 Ohm"
  260. };
  261. static SOC_ENUM_SINGLE_DECL(mic1lp_p_enum, AIC31XX_MICPGAPI, 6,
  262. mic_select_text);
  263. static SOC_ENUM_SINGLE_DECL(mic1rp_p_enum, AIC31XX_MICPGAPI, 4,
  264. mic_select_text);
  265. static SOC_ENUM_SINGLE_DECL(mic1lm_p_enum, AIC31XX_MICPGAPI, 2,
  266. mic_select_text);
  267. static SOC_ENUM_SINGLE_DECL(mic1lm_m_enum, AIC31XX_MICPGAMI, 4,
  268. mic_select_text);
  269. static const char * const hp_poweron_time_text[] = {
  270. "0us", "15.3us", "153us", "1.53ms", "15.3ms", "76.2ms",
  271. "153ms", "304ms", "610ms", "1.22s", "3.04s", "6.1s" };
  272. static SOC_ENUM_SINGLE_DECL(hp_poweron_time_enum, AIC31XX_HPPOP, 3,
  273. hp_poweron_time_text);
  274. static const char * const hp_rampup_step_text[] = {
  275. "0ms", "0.98ms", "1.95ms", "3.9ms" };
  276. static SOC_ENUM_SINGLE_DECL(hp_rampup_step_enum, AIC31XX_HPPOP, 1,
  277. hp_rampup_step_text);
  278. static const char * const vol_soft_step_mode_text[] = {
  279. "fast", "slow", "disabled" };
  280. static SOC_ENUM_SINGLE_DECL(vol_soft_step_mode_enum, AIC31XX_DACSETUP, 0,
  281. vol_soft_step_mode_text);
  282. static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -6350, 50, 0);
  283. static const DECLARE_TLV_DB_SCALE(adc_fgain_tlv, 0, 10, 0);
  284. static const DECLARE_TLV_DB_SCALE(adc_cgain_tlv, -2000, 50, 0);
  285. static const DECLARE_TLV_DB_SCALE(mic_pga_tlv, 0, 50, 0);
  286. static const DECLARE_TLV_DB_SCALE(hp_drv_tlv, 0, 100, 0);
  287. static const DECLARE_TLV_DB_SCALE(class_D_drv_tlv, 600, 600, 0);
  288. static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -6350, 50, 0);
  289. static const DECLARE_TLV_DB_SCALE(sp_vol_tlv, -6350, 50, 0);
  290. /*
  291. * controls to be exported to the user space
  292. */
  293. static const struct snd_kcontrol_new common31xx_snd_controls[] = {
  294. SOC_DOUBLE_R_S_TLV("DAC Playback Volume", AIC31XX_LDACVOL,
  295. AIC31XX_RDACVOL, 0, -127, 48, 7, 0, dac_vol_tlv),
  296. SOC_DOUBLE_R("HP Driver Playback Switch", AIC31XX_HPLGAIN,
  297. AIC31XX_HPRGAIN, 2, 1, 0),
  298. SOC_DOUBLE_R_TLV("HP Driver Playback Volume", AIC31XX_HPLGAIN,
  299. AIC31XX_HPRGAIN, 3, 0x09, 0, hp_drv_tlv),
  300. SOC_DOUBLE_R_TLV("HP Analog Playback Volume", AIC31XX_LANALOGHPL,
  301. AIC31XX_RANALOGHPR, 0, 0x7F, 1, hp_vol_tlv),
  302. /* HP de-pop control: apply power not immediately but via ramp
  303. * function with these psarameters. Note that power up sequence
  304. * has to wait for this to complete; this is implemented by
  305. * polling HP driver status in aic31xx_dapm_power_event()
  306. */
  307. SOC_ENUM("HP Output Driver Power-On time", hp_poweron_time_enum),
  308. SOC_ENUM("HP Output Driver Ramp-up step", hp_rampup_step_enum),
  309. SOC_ENUM("Volume Soft Stepping", vol_soft_step_mode_enum),
  310. };
  311. static const struct snd_kcontrol_new aic31xx_snd_controls[] = {
  312. SOC_SINGLE_TLV("ADC Fine Capture Volume", AIC31XX_ADCFGA, 4, 4, 1,
  313. adc_fgain_tlv),
  314. SOC_SINGLE("ADC Capture Switch", AIC31XX_ADCFGA, 7, 1, 1),
  315. SOC_DOUBLE_R_S_TLV("ADC Capture Volume", AIC31XX_ADCVOL, AIC31XX_ADCVOL,
  316. 0, -24, 40, 6, 0, adc_cgain_tlv),
  317. SOC_SINGLE_TLV("Mic PGA Capture Volume", AIC31XX_MICPGA, 0,
  318. 119, 0, mic_pga_tlv),
  319. };
  320. static const struct snd_kcontrol_new aic311x_snd_controls[] = {
  321. SOC_DOUBLE_R("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
  322. AIC31XX_SPRGAIN, 2, 1, 0),
  323. SOC_DOUBLE_R_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
  324. AIC31XX_SPRGAIN, 3, 3, 0, class_D_drv_tlv),
  325. SOC_DOUBLE_R_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
  326. AIC31XX_RANALOGSPR, 0, 0x7F, 1, sp_vol_tlv),
  327. };
  328. static const struct snd_kcontrol_new aic310x_snd_controls[] = {
  329. SOC_SINGLE("Speaker Driver Playback Switch", AIC31XX_SPLGAIN,
  330. 2, 1, 0),
  331. SOC_SINGLE_TLV("Speaker Driver Playback Volume", AIC31XX_SPLGAIN,
  332. 3, 3, 0, class_D_drv_tlv),
  333. SOC_SINGLE_TLV("Speaker Analog Playback Volume", AIC31XX_LANALOGSPL,
  334. 0, 0x7F, 1, sp_vol_tlv),
  335. };
  336. static const struct snd_kcontrol_new ldac_in_control =
  337. SOC_DAPM_ENUM("DAC Left Input", ldac_in_enum);
  338. static const struct snd_kcontrol_new rdac_in_control =
  339. SOC_DAPM_ENUM("DAC Right Input", rdac_in_enum);
  340. static int aic31xx_wait_bits(struct aic31xx_priv *aic31xx, unsigned int reg,
  341. unsigned int mask, unsigned int wbits, int sleep,
  342. int count)
  343. {
  344. unsigned int bits;
  345. int counter = count;
  346. int ret = regmap_read(aic31xx->regmap, reg, &bits);
  347. while ((bits & mask) != wbits && counter && !ret) {
  348. usleep_range(sleep, sleep * 2);
  349. ret = regmap_read(aic31xx->regmap, reg, &bits);
  350. counter--;
  351. }
  352. if ((bits & mask) != wbits) {
  353. dev_err(aic31xx->dev,
  354. "%s: Failed! 0x%x was 0x%x expected 0x%x (%d, 0x%x, %d us)\n",
  355. __func__, reg, bits, wbits, ret, mask,
  356. (count - counter) * sleep);
  357. ret = -1;
  358. }
  359. return ret;
  360. }
  361. #define WIDGET_BIT(reg, shift) (((shift) << 8) | (reg))
  362. static int aic31xx_dapm_power_event(struct snd_soc_dapm_widget *w,
  363. struct snd_kcontrol *kcontrol, int event)
  364. {
  365. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  366. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  367. unsigned int reg = AIC31XX_DACFLAG1;
  368. unsigned int mask;
  369. unsigned int timeout = 500 * USEC_PER_MSEC;
  370. switch (WIDGET_BIT(w->reg, w->shift)) {
  371. case WIDGET_BIT(AIC31XX_DACSETUP, 7):
  372. mask = AIC31XX_LDACPWRSTATUS_MASK;
  373. break;
  374. case WIDGET_BIT(AIC31XX_DACSETUP, 6):
  375. mask = AIC31XX_RDACPWRSTATUS_MASK;
  376. break;
  377. case WIDGET_BIT(AIC31XX_HPDRIVER, 7):
  378. mask = AIC31XX_HPLDRVPWRSTATUS_MASK;
  379. if (event == SND_SOC_DAPM_POST_PMU)
  380. timeout = 7 * USEC_PER_SEC;
  381. break;
  382. case WIDGET_BIT(AIC31XX_HPDRIVER, 6):
  383. mask = AIC31XX_HPRDRVPWRSTATUS_MASK;
  384. if (event == SND_SOC_DAPM_POST_PMU)
  385. timeout = 7 * USEC_PER_SEC;
  386. break;
  387. case WIDGET_BIT(AIC31XX_SPKAMP, 7):
  388. mask = AIC31XX_SPLDRVPWRSTATUS_MASK;
  389. break;
  390. case WIDGET_BIT(AIC31XX_SPKAMP, 6):
  391. mask = AIC31XX_SPRDRVPWRSTATUS_MASK;
  392. break;
  393. case WIDGET_BIT(AIC31XX_ADCSETUP, 7):
  394. mask = AIC31XX_ADCPWRSTATUS_MASK;
  395. reg = AIC31XX_ADCFLAG;
  396. break;
  397. default:
  398. dev_err(component->dev, "Unknown widget '%s' calling %s\n",
  399. w->name, __func__);
  400. return -EINVAL;
  401. }
  402. switch (event) {
  403. case SND_SOC_DAPM_POST_PMU:
  404. return aic31xx_wait_bits(aic31xx, reg, mask, mask,
  405. 5000, timeout / 5000);
  406. case SND_SOC_DAPM_POST_PMD:
  407. return aic31xx_wait_bits(aic31xx, reg, mask, 0,
  408. 5000, timeout / 5000);
  409. default:
  410. dev_dbg(component->dev,
  411. "Unhandled dapm widget event %d from %s\n",
  412. event, w->name);
  413. }
  414. return 0;
  415. }
  416. static const struct snd_kcontrol_new aic31xx_left_output_switches[] = {
  417. SOC_DAPM_SINGLE("From Left DAC", AIC31XX_DACMIXERROUTE, 6, 1, 0),
  418. SOC_DAPM_SINGLE("From MIC1LP", AIC31XX_DACMIXERROUTE, 5, 1, 0),
  419. SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 4, 1, 0),
  420. };
  421. static const struct snd_kcontrol_new aic31xx_right_output_switches[] = {
  422. SOC_DAPM_SINGLE("From Right DAC", AIC31XX_DACMIXERROUTE, 2, 1, 0),
  423. SOC_DAPM_SINGLE("From MIC1RP", AIC31XX_DACMIXERROUTE, 1, 1, 0),
  424. };
  425. static const struct snd_kcontrol_new dac31xx_left_output_switches[] = {
  426. SOC_DAPM_SINGLE("From Left DAC", AIC31XX_DACMIXERROUTE, 6, 1, 0),
  427. SOC_DAPM_SINGLE("From AIN1", AIC31XX_DACMIXERROUTE, 5, 1, 0),
  428. SOC_DAPM_SINGLE("From AIN2", AIC31XX_DACMIXERROUTE, 4, 1, 0),
  429. };
  430. static const struct snd_kcontrol_new dac31xx_right_output_switches[] = {
  431. SOC_DAPM_SINGLE("From Right DAC", AIC31XX_DACMIXERROUTE, 2, 1, 0),
  432. SOC_DAPM_SINGLE("From AIN2", AIC31XX_DACMIXERROUTE, 1, 1, 0),
  433. };
  434. static const struct snd_kcontrol_new p_term_mic1lp =
  435. SOC_DAPM_ENUM("MIC1LP P-Terminal", mic1lp_p_enum);
  436. static const struct snd_kcontrol_new p_term_mic1rp =
  437. SOC_DAPM_ENUM("MIC1RP P-Terminal", mic1rp_p_enum);
  438. static const struct snd_kcontrol_new p_term_mic1lm =
  439. SOC_DAPM_ENUM("MIC1LM P-Terminal", mic1lm_p_enum);
  440. static const struct snd_kcontrol_new m_term_mic1lm =
  441. SOC_DAPM_ENUM("MIC1LM M-Terminal", mic1lm_m_enum);
  442. static const struct snd_kcontrol_new aic31xx_dapm_hpl_switch =
  443. SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGHPL, 7, 1, 0);
  444. static const struct snd_kcontrol_new aic31xx_dapm_hpr_switch =
  445. SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGHPR, 7, 1, 0);
  446. static const struct snd_kcontrol_new aic31xx_dapm_spl_switch =
  447. SOC_DAPM_SINGLE("Switch", AIC31XX_LANALOGSPL, 7, 1, 0);
  448. static const struct snd_kcontrol_new aic31xx_dapm_spr_switch =
  449. SOC_DAPM_SINGLE("Switch", AIC31XX_RANALOGSPR, 7, 1, 0);
  450. static int mic_bias_event(struct snd_soc_dapm_widget *w,
  451. struct snd_kcontrol *kcontrol, int event)
  452. {
  453. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  454. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  455. switch (event) {
  456. case SND_SOC_DAPM_POST_PMU:
  457. /* change mic bias voltage to user defined */
  458. snd_soc_component_update_bits(component, AIC31XX_MICBIAS,
  459. AIC31XX_MICBIAS_MASK,
  460. aic31xx->micbias_vg <<
  461. AIC31XX_MICBIAS_SHIFT);
  462. dev_dbg(component->dev, "%s: turned on\n", __func__);
  463. break;
  464. case SND_SOC_DAPM_PRE_PMD:
  465. /* turn mic bias off */
  466. snd_soc_component_update_bits(component, AIC31XX_MICBIAS,
  467. AIC31XX_MICBIAS_MASK, 0);
  468. dev_dbg(component->dev, "%s: turned off\n", __func__);
  469. break;
  470. }
  471. return 0;
  472. }
  473. static const struct snd_soc_dapm_widget common31xx_dapm_widgets[] = {
  474. SND_SOC_DAPM_AIF_IN("AIF IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
  475. SND_SOC_DAPM_MUX("DAC Left Input",
  476. SND_SOC_NOPM, 0, 0, &ldac_in_control),
  477. SND_SOC_DAPM_MUX("DAC Right Input",
  478. SND_SOC_NOPM, 0, 0, &rdac_in_control),
  479. /* DACs */
  480. SND_SOC_DAPM_DAC_E("DAC Left", "Left Playback",
  481. AIC31XX_DACSETUP, 7, 0, aic31xx_dapm_power_event,
  482. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  483. SND_SOC_DAPM_DAC_E("DAC Right", "Right Playback",
  484. AIC31XX_DACSETUP, 6, 0, aic31xx_dapm_power_event,
  485. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  486. /* HP */
  487. SND_SOC_DAPM_SWITCH("HP Left", SND_SOC_NOPM, 0, 0,
  488. &aic31xx_dapm_hpl_switch),
  489. SND_SOC_DAPM_SWITCH("HP Right", SND_SOC_NOPM, 0, 0,
  490. &aic31xx_dapm_hpr_switch),
  491. /* Output drivers */
  492. SND_SOC_DAPM_OUT_DRV_E("HPL Driver", AIC31XX_HPDRIVER, 7, 0,
  493. NULL, 0, aic31xx_dapm_power_event,
  494. SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
  495. SND_SOC_DAPM_OUT_DRV_E("HPR Driver", AIC31XX_HPDRIVER, 6, 0,
  496. NULL, 0, aic31xx_dapm_power_event,
  497. SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
  498. /* Mic Bias */
  499. SND_SOC_DAPM_SUPPLY("MICBIAS", SND_SOC_NOPM, 0, 0, mic_bias_event,
  500. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  501. /* Keep BCLK/WCLK enabled even if DAC/ADC is powered down */
  502. SND_SOC_DAPM_SUPPLY("Activate I2S clocks", AIC31XX_IFACE2, 2, 0,
  503. NULL, 0),
  504. /* Outputs */
  505. SND_SOC_DAPM_OUTPUT("HPL"),
  506. SND_SOC_DAPM_OUTPUT("HPR"),
  507. };
  508. static const struct snd_soc_dapm_widget dac31xx_dapm_widgets[] = {
  509. /* Inputs */
  510. SND_SOC_DAPM_INPUT("AIN1"),
  511. SND_SOC_DAPM_INPUT("AIN2"),
  512. /* Output Mixers */
  513. SND_SOC_DAPM_MIXER("Output Left", SND_SOC_NOPM, 0, 0,
  514. dac31xx_left_output_switches,
  515. ARRAY_SIZE(dac31xx_left_output_switches)),
  516. SND_SOC_DAPM_MIXER("Output Right", SND_SOC_NOPM, 0, 0,
  517. dac31xx_right_output_switches,
  518. ARRAY_SIZE(dac31xx_right_output_switches)),
  519. };
  520. static const struct snd_soc_dapm_widget aic31xx_dapm_widgets[] = {
  521. /* Inputs */
  522. SND_SOC_DAPM_INPUT("MIC1LP"),
  523. SND_SOC_DAPM_INPUT("MIC1RP"),
  524. SND_SOC_DAPM_INPUT("MIC1LM"),
  525. /* Input Selection to MIC_PGA */
  526. SND_SOC_DAPM_MUX("MIC1LP P-Terminal", SND_SOC_NOPM, 0, 0,
  527. &p_term_mic1lp),
  528. SND_SOC_DAPM_MUX("MIC1RP P-Terminal", SND_SOC_NOPM, 0, 0,
  529. &p_term_mic1rp),
  530. SND_SOC_DAPM_MUX("MIC1LM P-Terminal", SND_SOC_NOPM, 0, 0,
  531. &p_term_mic1lm),
  532. /* ADC */
  533. SND_SOC_DAPM_ADC_E("ADC", "Capture", AIC31XX_ADCSETUP, 7, 0,
  534. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  535. SND_SOC_DAPM_POST_PMD),
  536. SND_SOC_DAPM_MUX("MIC1LM M-Terminal", SND_SOC_NOPM, 0, 0,
  537. &m_term_mic1lm),
  538. /* Enabling & Disabling MIC Gain Ctl */
  539. SND_SOC_DAPM_PGA("MIC_GAIN_CTL", AIC31XX_MICPGA,
  540. 7, 1, NULL, 0),
  541. /* Output Mixers */
  542. SND_SOC_DAPM_MIXER("Output Left", SND_SOC_NOPM, 0, 0,
  543. aic31xx_left_output_switches,
  544. ARRAY_SIZE(aic31xx_left_output_switches)),
  545. SND_SOC_DAPM_MIXER("Output Right", SND_SOC_NOPM, 0, 0,
  546. aic31xx_right_output_switches,
  547. ARRAY_SIZE(aic31xx_right_output_switches)),
  548. SND_SOC_DAPM_AIF_OUT("AIF OUT", "Capture", 0, SND_SOC_NOPM, 0, 0),
  549. };
  550. static const struct snd_soc_dapm_widget aic311x_dapm_widgets[] = {
  551. /* AIC3111 and AIC3110 have stereo class-D amplifier */
  552. SND_SOC_DAPM_OUT_DRV_E("SPL ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
  553. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  554. SND_SOC_DAPM_POST_PMD),
  555. SND_SOC_DAPM_OUT_DRV_E("SPR ClassD", AIC31XX_SPKAMP, 6, 0, NULL, 0,
  556. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  557. SND_SOC_DAPM_POST_PMD),
  558. SND_SOC_DAPM_SWITCH("Speaker Left", SND_SOC_NOPM, 0, 0,
  559. &aic31xx_dapm_spl_switch),
  560. SND_SOC_DAPM_SWITCH("Speaker Right", SND_SOC_NOPM, 0, 0,
  561. &aic31xx_dapm_spr_switch),
  562. SND_SOC_DAPM_OUTPUT("SPL"),
  563. SND_SOC_DAPM_OUTPUT("SPR"),
  564. };
  565. /* AIC3100 and AIC3120 have only mono class-D amplifier */
  566. static const struct snd_soc_dapm_widget aic310x_dapm_widgets[] = {
  567. SND_SOC_DAPM_OUT_DRV_E("SPK ClassD", AIC31XX_SPKAMP, 7, 0, NULL, 0,
  568. aic31xx_dapm_power_event, SND_SOC_DAPM_POST_PMU |
  569. SND_SOC_DAPM_POST_PMD),
  570. SND_SOC_DAPM_SWITCH("Speaker", SND_SOC_NOPM, 0, 0,
  571. &aic31xx_dapm_spl_switch),
  572. SND_SOC_DAPM_OUTPUT("SPK"),
  573. };
  574. static const struct snd_soc_dapm_route
  575. common31xx_audio_map[] = {
  576. /* DAC Input Routing */
  577. {"DAC Left Input", "Left Data", "AIF IN"},
  578. {"DAC Left Input", "Right Data", "AIF IN"},
  579. {"DAC Left Input", "Mono", "AIF IN"},
  580. {"DAC Right Input", "Left Data", "AIF IN"},
  581. {"DAC Right Input", "Right Data", "AIF IN"},
  582. {"DAC Right Input", "Mono", "AIF IN"},
  583. {"DAC Left", NULL, "DAC Left Input"},
  584. {"DAC Right", NULL, "DAC Right Input"},
  585. /* HPL path */
  586. {"HP Left", "Switch", "Output Left"},
  587. {"HPL Driver", NULL, "HP Left"},
  588. {"HPL", NULL, "HPL Driver"},
  589. /* HPR path */
  590. {"HP Right", "Switch", "Output Right"},
  591. {"HPR Driver", NULL, "HP Right"},
  592. {"HPR", NULL, "HPR Driver"},
  593. };
  594. static const struct snd_soc_dapm_route
  595. dac31xx_audio_map[] = {
  596. /* Left Output */
  597. {"Output Left", "From Left DAC", "DAC Left"},
  598. {"Output Left", "From AIN1", "AIN1"},
  599. {"Output Left", "From AIN2", "AIN2"},
  600. /* Right Output */
  601. {"Output Right", "From Right DAC", "DAC Right"},
  602. {"Output Right", "From AIN2", "AIN2"},
  603. };
  604. static const struct snd_soc_dapm_route
  605. aic31xx_audio_map[] = {
  606. /* Mic input */
  607. {"MIC1LP P-Terminal", "FFR 10 Ohm", "MIC1LP"},
  608. {"MIC1LP P-Terminal", "FFR 20 Ohm", "MIC1LP"},
  609. {"MIC1LP P-Terminal", "FFR 40 Ohm", "MIC1LP"},
  610. {"MIC1RP P-Terminal", "FFR 10 Ohm", "MIC1RP"},
  611. {"MIC1RP P-Terminal", "FFR 20 Ohm", "MIC1RP"},
  612. {"MIC1RP P-Terminal", "FFR 40 Ohm", "MIC1RP"},
  613. {"MIC1LM P-Terminal", "FFR 10 Ohm", "MIC1LM"},
  614. {"MIC1LM P-Terminal", "FFR 20 Ohm", "MIC1LM"},
  615. {"MIC1LM P-Terminal", "FFR 40 Ohm", "MIC1LM"},
  616. {"MIC1LM M-Terminal", "FFR 10 Ohm", "MIC1LM"},
  617. {"MIC1LM M-Terminal", "FFR 20 Ohm", "MIC1LM"},
  618. {"MIC1LM M-Terminal", "FFR 40 Ohm", "MIC1LM"},
  619. {"MIC_GAIN_CTL", NULL, "MIC1LP P-Terminal"},
  620. {"MIC_GAIN_CTL", NULL, "MIC1RP P-Terminal"},
  621. {"MIC_GAIN_CTL", NULL, "MIC1LM P-Terminal"},
  622. {"MIC_GAIN_CTL", NULL, "MIC1LM M-Terminal"},
  623. {"ADC", NULL, "MIC_GAIN_CTL"},
  624. {"AIF OUT", NULL, "ADC"},
  625. /* Left Output */
  626. {"Output Left", "From Left DAC", "DAC Left"},
  627. {"Output Left", "From MIC1LP", "MIC1LP"},
  628. {"Output Left", "From MIC1RP", "MIC1RP"},
  629. /* Right Output */
  630. {"Output Right", "From Right DAC", "DAC Right"},
  631. {"Output Right", "From MIC1RP", "MIC1RP"},
  632. };
  633. static const struct snd_soc_dapm_route
  634. aic311x_audio_map[] = {
  635. /* SP L path */
  636. {"Speaker Left", "Switch", "Output Left"},
  637. {"SPL ClassD", NULL, "Speaker Left"},
  638. {"SPL", NULL, "SPL ClassD"},
  639. /* SP R path */
  640. {"Speaker Right", "Switch", "Output Right"},
  641. {"SPR ClassD", NULL, "Speaker Right"},
  642. {"SPR", NULL, "SPR ClassD"},
  643. };
  644. static const struct snd_soc_dapm_route
  645. aic310x_audio_map[] = {
  646. /* SP L path */
  647. {"Speaker", "Switch", "Output Left"},
  648. {"SPK ClassD", NULL, "Speaker"},
  649. {"SPK", NULL, "SPK ClassD"},
  650. };
  651. /*
  652. * Always connected DAPM routes for codec clock master modes.
  653. * If the codec is the master on the I2S bus, we need to power up components
  654. * to have valid DAC_CLK.
  655. *
  656. * In order to have the I2S clocks on the bus either the DACs/ADC need to be
  657. * enabled, or the P0/R29/D2 (Keep bclk/wclk in power down) need to be set.
  658. *
  659. * Otherwise the codec will not generate clocks on the bus.
  660. */
  661. static const struct snd_soc_dapm_route
  662. common31xx_cm_audio_map[] = {
  663. {"HPL", NULL, "AIF IN"},
  664. {"HPR", NULL, "AIF IN"},
  665. {"AIF IN", NULL, "Activate I2S clocks"},
  666. };
  667. static const struct snd_soc_dapm_route
  668. aic31xx_cm_audio_map[] = {
  669. {"AIF OUT", NULL, "MIC1LP"},
  670. {"AIF OUT", NULL, "MIC1RP"},
  671. {"AIF OUT", NULL, "MIC1LM"},
  672. {"AIF OUT", NULL, "Activate I2S clocks"},
  673. };
  674. static int aic31xx_add_controls(struct snd_soc_component *component)
  675. {
  676. int ret = 0;
  677. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  678. if (!(aic31xx->codec_type & DAC31XX_BIT))
  679. ret = snd_soc_add_component_controls(
  680. component, aic31xx_snd_controls,
  681. ARRAY_SIZE(aic31xx_snd_controls));
  682. if (ret)
  683. return ret;
  684. if (aic31xx->codec_type & AIC31XX_STEREO_CLASS_D_BIT)
  685. ret = snd_soc_add_component_controls(
  686. component, aic311x_snd_controls,
  687. ARRAY_SIZE(aic311x_snd_controls));
  688. else
  689. ret = snd_soc_add_component_controls(
  690. component, aic310x_snd_controls,
  691. ARRAY_SIZE(aic310x_snd_controls));
  692. return ret;
  693. }
  694. static int aic31xx_add_widgets(struct snd_soc_component *component)
  695. {
  696. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  697. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  698. int ret = 0;
  699. if (aic31xx->codec_type & DAC31XX_BIT) {
  700. ret = snd_soc_dapm_new_controls(
  701. dapm, dac31xx_dapm_widgets,
  702. ARRAY_SIZE(dac31xx_dapm_widgets));
  703. if (ret)
  704. return ret;
  705. ret = snd_soc_dapm_add_routes(dapm, dac31xx_audio_map,
  706. ARRAY_SIZE(dac31xx_audio_map));
  707. if (ret)
  708. return ret;
  709. } else {
  710. ret = snd_soc_dapm_new_controls(
  711. dapm, aic31xx_dapm_widgets,
  712. ARRAY_SIZE(aic31xx_dapm_widgets));
  713. if (ret)
  714. return ret;
  715. ret = snd_soc_dapm_add_routes(dapm, aic31xx_audio_map,
  716. ARRAY_SIZE(aic31xx_audio_map));
  717. if (ret)
  718. return ret;
  719. }
  720. if (aic31xx->codec_type & AIC31XX_STEREO_CLASS_D_BIT) {
  721. ret = snd_soc_dapm_new_controls(
  722. dapm, aic311x_dapm_widgets,
  723. ARRAY_SIZE(aic311x_dapm_widgets));
  724. if (ret)
  725. return ret;
  726. ret = snd_soc_dapm_add_routes(dapm, aic311x_audio_map,
  727. ARRAY_SIZE(aic311x_audio_map));
  728. if (ret)
  729. return ret;
  730. } else {
  731. ret = snd_soc_dapm_new_controls(
  732. dapm, aic310x_dapm_widgets,
  733. ARRAY_SIZE(aic310x_dapm_widgets));
  734. if (ret)
  735. return ret;
  736. ret = snd_soc_dapm_add_routes(dapm, aic310x_audio_map,
  737. ARRAY_SIZE(aic310x_audio_map));
  738. if (ret)
  739. return ret;
  740. }
  741. return 0;
  742. }
  743. static int aic31xx_setup_pll(struct snd_soc_component *component,
  744. struct snd_pcm_hw_params *params)
  745. {
  746. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  747. int bclk_score = snd_soc_params_to_frame_size(params);
  748. int mclk_p;
  749. int bclk_n = 0;
  750. int match = -1;
  751. int i;
  752. if (!aic31xx->sysclk || !aic31xx->p_div) {
  753. dev_err(component->dev, "Master clock not supplied\n");
  754. return -EINVAL;
  755. }
  756. mclk_p = aic31xx->sysclk / aic31xx->p_div;
  757. /* Use PLL as CODEC_CLKIN and DAC_CLK as BDIV_CLKIN */
  758. snd_soc_component_update_bits(component, AIC31XX_CLKMUX,
  759. AIC31XX_CODEC_CLKIN_MASK, AIC31XX_CODEC_CLKIN_PLL);
  760. snd_soc_component_update_bits(component, AIC31XX_IFACE2,
  761. AIC31XX_BDIVCLK_MASK, AIC31XX_DAC2BCLK);
  762. for (i = 0; i < ARRAY_SIZE(aic31xx_divs); i++) {
  763. if (aic31xx_divs[i].rate == params_rate(params) &&
  764. aic31xx_divs[i].mclk_p == mclk_p) {
  765. int s = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) %
  766. snd_soc_params_to_frame_size(params);
  767. int bn = (aic31xx_divs[i].dosr * aic31xx_divs[i].mdac) /
  768. snd_soc_params_to_frame_size(params);
  769. if (s < bclk_score && bn > 0) {
  770. match = i;
  771. bclk_n = bn;
  772. bclk_score = s;
  773. }
  774. }
  775. }
  776. if (match == -1) {
  777. dev_err(component->dev,
  778. "%s: Sample rate (%u) and format not supported\n",
  779. __func__, params_rate(params));
  780. /* See bellow for details how fix this. */
  781. return -EINVAL;
  782. }
  783. if (bclk_score != 0) {
  784. dev_warn(component->dev, "Can not produce exact bitclock");
  785. /* This is fine if using dsp format, but if using i2s
  786. there may be trouble. To fix the issue edit the
  787. aic31xx_divs table for your mclk and sample
  788. rate. Details can be found from:
  789. https://www.ti.com/lit/ds/symlink/tlv320aic3100.pdf
  790. Section: 5.6 CLOCK Generation and PLL
  791. */
  792. }
  793. i = match;
  794. /* PLL configuration */
  795. snd_soc_component_update_bits(component, AIC31XX_PLLPR, AIC31XX_PLL_MASK,
  796. (aic31xx->p_div << 4) | aic31xx_divs[i].pll_r);
  797. snd_soc_component_write(component, AIC31XX_PLLJ, aic31xx_divs[i].pll_j);
  798. snd_soc_component_write(component, AIC31XX_PLLDMSB,
  799. aic31xx_divs[i].pll_d >> 8);
  800. snd_soc_component_write(component, AIC31XX_PLLDLSB,
  801. aic31xx_divs[i].pll_d & 0xff);
  802. /* DAC dividers configuration */
  803. snd_soc_component_update_bits(component, AIC31XX_NDAC, AIC31XX_PLL_MASK,
  804. aic31xx_divs[i].ndac);
  805. snd_soc_component_update_bits(component, AIC31XX_MDAC, AIC31XX_PLL_MASK,
  806. aic31xx_divs[i].mdac);
  807. snd_soc_component_write(component, AIC31XX_DOSRMSB, aic31xx_divs[i].dosr >> 8);
  808. snd_soc_component_write(component, AIC31XX_DOSRLSB, aic31xx_divs[i].dosr & 0xff);
  809. /* ADC dividers configuration. Write reset value 1 if not used. */
  810. snd_soc_component_update_bits(component, AIC31XX_NADC, AIC31XX_PLL_MASK,
  811. aic31xx_divs[i].nadc ? aic31xx_divs[i].nadc : 1);
  812. snd_soc_component_update_bits(component, AIC31XX_MADC, AIC31XX_PLL_MASK,
  813. aic31xx_divs[i].madc ? aic31xx_divs[i].madc : 1);
  814. snd_soc_component_write(component, AIC31XX_AOSR, aic31xx_divs[i].aosr);
  815. /* Bit clock divider configuration. */
  816. snd_soc_component_update_bits(component, AIC31XX_BCLKN,
  817. AIC31XX_PLL_MASK, bclk_n);
  818. aic31xx->rate_div_line = i;
  819. dev_dbg(component->dev,
  820. "pll %d.%04d/%d dosr %d n %d m %d aosr %d n %d m %d bclk_n %d\n",
  821. aic31xx_divs[i].pll_j,
  822. aic31xx_divs[i].pll_d,
  823. aic31xx->p_div,
  824. aic31xx_divs[i].dosr,
  825. aic31xx_divs[i].ndac,
  826. aic31xx_divs[i].mdac,
  827. aic31xx_divs[i].aosr,
  828. aic31xx_divs[i].nadc,
  829. aic31xx_divs[i].madc,
  830. bclk_n
  831. );
  832. return 0;
  833. }
  834. static int aic31xx_hw_params(struct snd_pcm_substream *substream,
  835. struct snd_pcm_hw_params *params,
  836. struct snd_soc_dai *dai)
  837. {
  838. struct snd_soc_component *component = dai->component;
  839. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  840. u8 data = 0;
  841. dev_dbg(component->dev, "## %s: width %d rate %d\n",
  842. __func__, params_width(params),
  843. params_rate(params));
  844. switch (params_width(params)) {
  845. case 16:
  846. break;
  847. case 20:
  848. data = (AIC31XX_WORD_LEN_20BITS <<
  849. AIC31XX_IFACE1_DATALEN_SHIFT);
  850. break;
  851. case 24:
  852. data = (AIC31XX_WORD_LEN_24BITS <<
  853. AIC31XX_IFACE1_DATALEN_SHIFT);
  854. break;
  855. case 32:
  856. data = (AIC31XX_WORD_LEN_32BITS <<
  857. AIC31XX_IFACE1_DATALEN_SHIFT);
  858. break;
  859. default:
  860. dev_err(component->dev, "%s: Unsupported width %d\n",
  861. __func__, params_width(params));
  862. return -EINVAL;
  863. }
  864. snd_soc_component_update_bits(component, AIC31XX_IFACE1,
  865. AIC31XX_IFACE1_DATALEN_MASK,
  866. data);
  867. /*
  868. * If BCLK is used as PLL input, the sysclk is determined by the hw
  869. * params. So it must be updated here to match the input frequency.
  870. */
  871. if (aic31xx->sysclk_id == AIC31XX_PLL_CLKIN_BCLK) {
  872. aic31xx->sysclk = params_rate(params) * params_width(params) *
  873. params_channels(params);
  874. aic31xx->p_div = 1;
  875. }
  876. return aic31xx_setup_pll(component, params);
  877. }
  878. static int aic31xx_dac_mute(struct snd_soc_dai *codec_dai, int mute,
  879. int direction)
  880. {
  881. struct snd_soc_component *component = codec_dai->component;
  882. if (mute) {
  883. snd_soc_component_update_bits(component, AIC31XX_DACMUTE,
  884. AIC31XX_DACMUTE_MASK,
  885. AIC31XX_DACMUTE_MASK);
  886. } else {
  887. snd_soc_component_update_bits(component, AIC31XX_DACMUTE,
  888. AIC31XX_DACMUTE_MASK, 0x0);
  889. }
  890. return 0;
  891. }
  892. static int aic31xx_clock_master_routes(struct snd_soc_component *component,
  893. unsigned int fmt)
  894. {
  895. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  896. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  897. int ret;
  898. fmt &= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
  899. if (fmt == SND_SOC_DAIFMT_CBC_CFC &&
  900. aic31xx->master_dapm_route_applied) {
  901. /*
  902. * Remove the DAPM route(s) for codec clock master modes,
  903. * if applied
  904. */
  905. ret = snd_soc_dapm_del_routes(dapm, common31xx_cm_audio_map,
  906. ARRAY_SIZE(common31xx_cm_audio_map));
  907. if (!ret && !(aic31xx->codec_type & DAC31XX_BIT))
  908. ret = snd_soc_dapm_del_routes(dapm,
  909. aic31xx_cm_audio_map,
  910. ARRAY_SIZE(aic31xx_cm_audio_map));
  911. if (ret)
  912. return ret;
  913. aic31xx->master_dapm_route_applied = false;
  914. } else if (fmt != SND_SOC_DAIFMT_CBC_CFC &&
  915. !aic31xx->master_dapm_route_applied) {
  916. /*
  917. * Add the needed DAPM route(s) for codec clock master modes,
  918. * if it is not done already
  919. */
  920. ret = snd_soc_dapm_add_routes(dapm, common31xx_cm_audio_map,
  921. ARRAY_SIZE(common31xx_cm_audio_map));
  922. if (!ret && !(aic31xx->codec_type & DAC31XX_BIT))
  923. ret = snd_soc_dapm_add_routes(dapm,
  924. aic31xx_cm_audio_map,
  925. ARRAY_SIZE(aic31xx_cm_audio_map));
  926. if (ret)
  927. return ret;
  928. aic31xx->master_dapm_route_applied = true;
  929. }
  930. return 0;
  931. }
  932. static int aic31xx_set_dai_fmt(struct snd_soc_dai *codec_dai,
  933. unsigned int fmt)
  934. {
  935. struct snd_soc_component *component = codec_dai->component;
  936. u8 iface_reg1 = 0;
  937. u8 iface_reg2 = 0;
  938. u8 dsp_a_val = 0;
  939. dev_dbg(component->dev, "## %s: fmt = 0x%x\n", __func__, fmt);
  940. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  941. case SND_SOC_DAIFMT_CBP_CFP:
  942. iface_reg1 |= AIC31XX_BCLK_MASTER | AIC31XX_WCLK_MASTER;
  943. break;
  944. case SND_SOC_DAIFMT_CBC_CFP:
  945. iface_reg1 |= AIC31XX_WCLK_MASTER;
  946. break;
  947. case SND_SOC_DAIFMT_CBP_CFC:
  948. iface_reg1 |= AIC31XX_BCLK_MASTER;
  949. break;
  950. case SND_SOC_DAIFMT_CBC_CFC:
  951. break;
  952. default:
  953. dev_err(component->dev, "Invalid DAI clock provider\n");
  954. return -EINVAL;
  955. }
  956. /* signal polarity */
  957. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  958. case SND_SOC_DAIFMT_NB_NF:
  959. break;
  960. case SND_SOC_DAIFMT_IB_NF:
  961. iface_reg2 |= AIC31XX_BCLKINV_MASK;
  962. break;
  963. default:
  964. dev_err(component->dev, "Invalid DAI clock signal polarity\n");
  965. return -EINVAL;
  966. }
  967. /* interface format */
  968. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  969. case SND_SOC_DAIFMT_I2S:
  970. break;
  971. case SND_SOC_DAIFMT_DSP_A:
  972. dsp_a_val = 0x1;
  973. fallthrough;
  974. case SND_SOC_DAIFMT_DSP_B:
  975. /*
  976. * NOTE: This CODEC samples on the falling edge of BCLK in
  977. * DSP mode, this is inverted compared to what most DAIs
  978. * expect, so we invert for this mode
  979. */
  980. iface_reg2 ^= AIC31XX_BCLKINV_MASK;
  981. iface_reg1 |= (AIC31XX_DSP_MODE <<
  982. AIC31XX_IFACE1_DATATYPE_SHIFT);
  983. break;
  984. case SND_SOC_DAIFMT_RIGHT_J:
  985. iface_reg1 |= (AIC31XX_RIGHT_JUSTIFIED_MODE <<
  986. AIC31XX_IFACE1_DATATYPE_SHIFT);
  987. break;
  988. case SND_SOC_DAIFMT_LEFT_J:
  989. iface_reg1 |= (AIC31XX_LEFT_JUSTIFIED_MODE <<
  990. AIC31XX_IFACE1_DATATYPE_SHIFT);
  991. break;
  992. default:
  993. dev_err(component->dev, "Invalid DAI interface format\n");
  994. return -EINVAL;
  995. }
  996. snd_soc_component_update_bits(component, AIC31XX_IFACE1,
  997. AIC31XX_IFACE1_DATATYPE_MASK |
  998. AIC31XX_IFACE1_MASTER_MASK,
  999. iface_reg1);
  1000. snd_soc_component_update_bits(component, AIC31XX_DATA_OFFSET,
  1001. AIC31XX_DATA_OFFSET_MASK,
  1002. dsp_a_val);
  1003. snd_soc_component_update_bits(component, AIC31XX_IFACE2,
  1004. AIC31XX_BCLKINV_MASK,
  1005. iface_reg2);
  1006. return aic31xx_clock_master_routes(component, fmt);
  1007. }
  1008. static int aic31xx_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1009. int clk_id, unsigned int freq, int dir)
  1010. {
  1011. struct snd_soc_component *component = codec_dai->component;
  1012. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  1013. int i;
  1014. dev_dbg(component->dev, "## %s: clk_id = %d, freq = %d, dir = %d\n",
  1015. __func__, clk_id, freq, dir);
  1016. for (i = 1; i < 8; i++)
  1017. if (freq / i <= 20000000)
  1018. break;
  1019. if (freq/i > 20000000) {
  1020. dev_err(aic31xx->dev, "%s: Too high mclk frequency %u\n",
  1021. __func__, freq);
  1022. return -EINVAL;
  1023. }
  1024. aic31xx->p_div = i;
  1025. for (i = 0; i < ARRAY_SIZE(aic31xx_divs); i++)
  1026. if (aic31xx_divs[i].mclk_p == freq / aic31xx->p_div)
  1027. break;
  1028. if (i == ARRAY_SIZE(aic31xx_divs)) {
  1029. dev_err(aic31xx->dev, "%s: Unsupported frequency %d\n",
  1030. __func__, freq);
  1031. return -EINVAL;
  1032. }
  1033. /* set clock on MCLK, BCLK, or GPIO1 as PLL input */
  1034. snd_soc_component_update_bits(component, AIC31XX_CLKMUX, AIC31XX_PLL_CLKIN_MASK,
  1035. clk_id << AIC31XX_PLL_CLKIN_SHIFT);
  1036. aic31xx->sysclk_id = clk_id;
  1037. aic31xx->sysclk = freq;
  1038. return 0;
  1039. }
  1040. static int aic31xx_regulator_event(struct notifier_block *nb,
  1041. unsigned long event, void *data)
  1042. {
  1043. struct aic31xx_disable_nb *disable_nb =
  1044. container_of(nb, struct aic31xx_disable_nb, nb);
  1045. struct aic31xx_priv *aic31xx = disable_nb->aic31xx;
  1046. if (event & REGULATOR_EVENT_DISABLE) {
  1047. /*
  1048. * Put codec to reset and as at least one of the
  1049. * supplies was disabled.
  1050. */
  1051. if (aic31xx->gpio_reset)
  1052. gpiod_set_value(aic31xx->gpio_reset, 1);
  1053. regcache_mark_dirty(aic31xx->regmap);
  1054. dev_dbg(aic31xx->dev, "## %s: DISABLE received\n", __func__);
  1055. }
  1056. return 0;
  1057. }
  1058. static int aic31xx_reset(struct aic31xx_priv *aic31xx)
  1059. {
  1060. int ret = 0;
  1061. if (aic31xx->gpio_reset) {
  1062. gpiod_set_value(aic31xx->gpio_reset, 1);
  1063. ndelay(10); /* At least 10ns */
  1064. gpiod_set_value(aic31xx->gpio_reset, 0);
  1065. } else {
  1066. ret = regmap_write(aic31xx->regmap, AIC31XX_RESET, 1);
  1067. }
  1068. mdelay(1); /* At least 1ms */
  1069. return ret;
  1070. }
  1071. static void aic31xx_clk_on(struct snd_soc_component *component)
  1072. {
  1073. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  1074. u8 mask = AIC31XX_PM_MASK;
  1075. u8 on = AIC31XX_PM_MASK;
  1076. dev_dbg(component->dev, "codec clock -> on (rate %d)\n",
  1077. aic31xx_divs[aic31xx->rate_div_line].rate);
  1078. snd_soc_component_update_bits(component, AIC31XX_PLLPR, mask, on);
  1079. mdelay(10);
  1080. snd_soc_component_update_bits(component, AIC31XX_NDAC, mask, on);
  1081. snd_soc_component_update_bits(component, AIC31XX_MDAC, mask, on);
  1082. if (aic31xx_divs[aic31xx->rate_div_line].nadc)
  1083. snd_soc_component_update_bits(component, AIC31XX_NADC, mask, on);
  1084. if (aic31xx_divs[aic31xx->rate_div_line].madc)
  1085. snd_soc_component_update_bits(component, AIC31XX_MADC, mask, on);
  1086. snd_soc_component_update_bits(component, AIC31XX_BCLKN, mask, on);
  1087. }
  1088. static void aic31xx_clk_off(struct snd_soc_component *component)
  1089. {
  1090. u8 mask = AIC31XX_PM_MASK;
  1091. u8 off = 0;
  1092. dev_dbg(component->dev, "codec clock -> off\n");
  1093. snd_soc_component_update_bits(component, AIC31XX_BCLKN, mask, off);
  1094. snd_soc_component_update_bits(component, AIC31XX_MADC, mask, off);
  1095. snd_soc_component_update_bits(component, AIC31XX_NADC, mask, off);
  1096. snd_soc_component_update_bits(component, AIC31XX_MDAC, mask, off);
  1097. snd_soc_component_update_bits(component, AIC31XX_NDAC, mask, off);
  1098. snd_soc_component_update_bits(component, AIC31XX_PLLPR, mask, off);
  1099. }
  1100. static int aic31xx_power_on(struct snd_soc_component *component)
  1101. {
  1102. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  1103. int ret;
  1104. ret = regulator_bulk_enable(ARRAY_SIZE(aic31xx->supplies),
  1105. aic31xx->supplies);
  1106. if (ret)
  1107. return ret;
  1108. regcache_cache_only(aic31xx->regmap, false);
  1109. /* Reset device registers for a consistent power-on like state */
  1110. ret = aic31xx_reset(aic31xx);
  1111. if (ret < 0)
  1112. dev_err(aic31xx->dev, "Could not reset device: %d\n", ret);
  1113. ret = regcache_sync(aic31xx->regmap);
  1114. if (ret) {
  1115. dev_err(component->dev,
  1116. "Failed to restore cache: %d\n", ret);
  1117. regcache_cache_only(aic31xx->regmap, true);
  1118. regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
  1119. aic31xx->supplies);
  1120. return ret;
  1121. }
  1122. /*
  1123. * The jack detection configuration is in the same register
  1124. * that is used to report jack detect status so is volatile
  1125. * and not covered by the cache sync, restore it separately.
  1126. */
  1127. aic31xx_set_jack(component, aic31xx->jack, NULL);
  1128. return 0;
  1129. }
  1130. static void aic31xx_power_off(struct snd_soc_component *component)
  1131. {
  1132. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  1133. regcache_cache_only(aic31xx->regmap, true);
  1134. regulator_bulk_disable(ARRAY_SIZE(aic31xx->supplies),
  1135. aic31xx->supplies);
  1136. }
  1137. static int aic31xx_set_bias_level(struct snd_soc_component *component,
  1138. enum snd_soc_bias_level level)
  1139. {
  1140. dev_dbg(component->dev, "## %s: %d -> %d\n", __func__,
  1141. snd_soc_component_get_bias_level(component), level);
  1142. switch (level) {
  1143. case SND_SOC_BIAS_ON:
  1144. break;
  1145. case SND_SOC_BIAS_PREPARE:
  1146. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY)
  1147. aic31xx_clk_on(component);
  1148. break;
  1149. case SND_SOC_BIAS_STANDBY:
  1150. switch (snd_soc_component_get_bias_level(component)) {
  1151. case SND_SOC_BIAS_OFF:
  1152. aic31xx_power_on(component);
  1153. break;
  1154. case SND_SOC_BIAS_PREPARE:
  1155. aic31xx_clk_off(component);
  1156. break;
  1157. default:
  1158. BUG();
  1159. }
  1160. break;
  1161. case SND_SOC_BIAS_OFF:
  1162. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY)
  1163. aic31xx_power_off(component);
  1164. break;
  1165. }
  1166. return 0;
  1167. }
  1168. static int aic31xx_set_jack(struct snd_soc_component *component,
  1169. struct snd_soc_jack *jack, void *data)
  1170. {
  1171. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  1172. aic31xx->jack = jack;
  1173. /* Enable/Disable jack detection */
  1174. regmap_write(aic31xx->regmap, AIC31XX_HSDETECT,
  1175. jack ? AIC31XX_HSD_ENABLE : 0);
  1176. return 0;
  1177. }
  1178. static int aic31xx_codec_probe(struct snd_soc_component *component)
  1179. {
  1180. struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
  1181. int i, ret;
  1182. dev_dbg(aic31xx->dev, "## %s\n", __func__);
  1183. aic31xx->component = component;
  1184. for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++) {
  1185. aic31xx->disable_nb[i].nb.notifier_call =
  1186. aic31xx_regulator_event;
  1187. aic31xx->disable_nb[i].aic31xx = aic31xx;
  1188. ret = devm_regulator_register_notifier(
  1189. aic31xx->supplies[i].consumer,
  1190. &aic31xx->disable_nb[i].nb);
  1191. if (ret) {
  1192. dev_err(component->dev,
  1193. "Failed to request regulator notifier: %d\n",
  1194. ret);
  1195. return ret;
  1196. }
  1197. }
  1198. regcache_cache_only(aic31xx->regmap, true);
  1199. regcache_mark_dirty(aic31xx->regmap);
  1200. ret = aic31xx_add_controls(component);
  1201. if (ret)
  1202. return ret;
  1203. ret = aic31xx_add_widgets(component);
  1204. if (ret)
  1205. return ret;
  1206. /* set output common-mode voltage */
  1207. snd_soc_component_update_bits(component, AIC31XX_HPDRIVER,
  1208. AIC31XX_HPD_OCMV_MASK,
  1209. aic31xx->ocmv << AIC31XX_HPD_OCMV_SHIFT);
  1210. return 0;
  1211. }
  1212. static const struct snd_soc_component_driver soc_codec_driver_aic31xx = {
  1213. .probe = aic31xx_codec_probe,
  1214. .set_jack = aic31xx_set_jack,
  1215. .set_bias_level = aic31xx_set_bias_level,
  1216. .controls = common31xx_snd_controls,
  1217. .num_controls = ARRAY_SIZE(common31xx_snd_controls),
  1218. .dapm_widgets = common31xx_dapm_widgets,
  1219. .num_dapm_widgets = ARRAY_SIZE(common31xx_dapm_widgets),
  1220. .dapm_routes = common31xx_audio_map,
  1221. .num_dapm_routes = ARRAY_SIZE(common31xx_audio_map),
  1222. .suspend_bias_off = 1,
  1223. .idle_bias_on = 1,
  1224. .use_pmdown_time = 1,
  1225. .endianness = 1,
  1226. };
  1227. static const struct snd_soc_dai_ops aic31xx_dai_ops = {
  1228. .hw_params = aic31xx_hw_params,
  1229. .set_sysclk = aic31xx_set_dai_sysclk,
  1230. .set_fmt = aic31xx_set_dai_fmt,
  1231. .mute_stream = aic31xx_dac_mute,
  1232. .no_capture_mute = 1,
  1233. };
  1234. static struct snd_soc_dai_driver dac31xx_dai_driver[] = {
  1235. {
  1236. .name = "tlv320dac31xx-hifi",
  1237. .playback = {
  1238. .stream_name = "Playback",
  1239. .channels_min = 2,
  1240. .channels_max = 2,
  1241. .rates = AIC31XX_RATES,
  1242. .formats = AIC31XX_FORMATS,
  1243. },
  1244. .ops = &aic31xx_dai_ops,
  1245. .symmetric_rate = 1,
  1246. }
  1247. };
  1248. static struct snd_soc_dai_driver aic31xx_dai_driver[] = {
  1249. {
  1250. .name = "tlv320aic31xx-hifi",
  1251. .playback = {
  1252. .stream_name = "Playback",
  1253. .channels_min = 2,
  1254. .channels_max = 2,
  1255. .rates = AIC31XX_RATES,
  1256. .formats = AIC31XX_FORMATS,
  1257. },
  1258. .capture = {
  1259. .stream_name = "Capture",
  1260. .channels_min = 2,
  1261. .channels_max = 2,
  1262. .rates = AIC31XX_RATES,
  1263. .formats = AIC31XX_FORMATS,
  1264. },
  1265. .ops = &aic31xx_dai_ops,
  1266. .symmetric_rate = 1,
  1267. }
  1268. };
  1269. #if defined(CONFIG_OF)
  1270. static const struct of_device_id tlv320aic31xx_of_match[] = {
  1271. { .compatible = "ti,tlv320aic310x" },
  1272. { .compatible = "ti,tlv320aic311x" },
  1273. { .compatible = "ti,tlv320aic3100" },
  1274. { .compatible = "ti,tlv320aic3110" },
  1275. { .compatible = "ti,tlv320aic3120" },
  1276. { .compatible = "ti,tlv320aic3111" },
  1277. { .compatible = "ti,tlv320dac3100" },
  1278. { .compatible = "ti,tlv320dac3101" },
  1279. {},
  1280. };
  1281. MODULE_DEVICE_TABLE(of, tlv320aic31xx_of_match);
  1282. #endif /* CONFIG_OF */
  1283. #ifdef CONFIG_ACPI
  1284. static const struct acpi_device_id aic31xx_acpi_match[] = {
  1285. { "10TI3100", 0 },
  1286. { }
  1287. };
  1288. MODULE_DEVICE_TABLE(acpi, aic31xx_acpi_match);
  1289. #endif
  1290. static irqreturn_t aic31xx_irq(int irq, void *data)
  1291. {
  1292. struct aic31xx_priv *aic31xx = data;
  1293. struct device *dev = aic31xx->dev;
  1294. unsigned int value;
  1295. bool handled = false;
  1296. int ret;
  1297. ret = regmap_read(aic31xx->regmap, AIC31XX_INTRDACFLAG, &value);
  1298. if (ret) {
  1299. dev_err(dev, "Failed to read interrupt mask: %d\n", ret);
  1300. goto exit;
  1301. }
  1302. if (value)
  1303. handled = true;
  1304. else
  1305. goto read_overflow;
  1306. if (value & AIC31XX_HPLSCDETECT)
  1307. dev_err(dev, "Short circuit on Left output is detected\n");
  1308. if (value & AIC31XX_HPRSCDETECT)
  1309. dev_err(dev, "Short circuit on Right output is detected\n");
  1310. if (value & (AIC31XX_HSPLUG | AIC31XX_BUTTONPRESS)) {
  1311. unsigned int val;
  1312. int status = 0;
  1313. ret = regmap_read(aic31xx->regmap, AIC31XX_INTRDACFLAG2,
  1314. &val);
  1315. if (ret) {
  1316. dev_err(dev, "Failed to read interrupt mask: %d\n",
  1317. ret);
  1318. goto exit;
  1319. }
  1320. if (val & AIC31XX_BUTTONPRESS)
  1321. status |= SND_JACK_BTN_0;
  1322. ret = regmap_read(aic31xx->regmap, AIC31XX_HSDETECT, &val);
  1323. if (ret) {
  1324. dev_err(dev, "Failed to read headset type: %d\n", ret);
  1325. goto exit;
  1326. }
  1327. switch ((val & AIC31XX_HSD_TYPE_MASK) >>
  1328. AIC31XX_HSD_TYPE_SHIFT) {
  1329. case AIC31XX_HSD_HP:
  1330. status |= SND_JACK_HEADPHONE;
  1331. break;
  1332. case AIC31XX_HSD_HS:
  1333. status |= SND_JACK_HEADSET;
  1334. break;
  1335. default:
  1336. break;
  1337. }
  1338. if (aic31xx->jack)
  1339. snd_soc_jack_report(aic31xx->jack, status,
  1340. AIC31XX_JACK_MASK);
  1341. }
  1342. if (value & ~(AIC31XX_HPLSCDETECT |
  1343. AIC31XX_HPRSCDETECT |
  1344. AIC31XX_HSPLUG |
  1345. AIC31XX_BUTTONPRESS))
  1346. dev_err(dev, "Unknown DAC interrupt flags: 0x%08x\n", value);
  1347. read_overflow:
  1348. ret = regmap_read(aic31xx->regmap, AIC31XX_OFFLAG, &value);
  1349. if (ret) {
  1350. dev_err(dev, "Failed to read overflow flag: %d\n", ret);
  1351. goto exit;
  1352. }
  1353. if (value)
  1354. handled = true;
  1355. else
  1356. goto exit;
  1357. if (value & AIC31XX_DAC_OF_LEFT)
  1358. dev_warn(dev, "Left-channel DAC overflow has occurred\n");
  1359. if (value & AIC31XX_DAC_OF_RIGHT)
  1360. dev_warn(dev, "Right-channel DAC overflow has occurred\n");
  1361. if (value & AIC31XX_DAC_OF_SHIFTER)
  1362. dev_warn(dev, "DAC barrel shifter overflow has occurred\n");
  1363. if (value & AIC31XX_ADC_OF)
  1364. dev_warn(dev, "ADC overflow has occurred\n");
  1365. if (value & AIC31XX_ADC_OF_SHIFTER)
  1366. dev_warn(dev, "ADC barrel shifter overflow has occurred\n");
  1367. if (value & ~(AIC31XX_DAC_OF_LEFT |
  1368. AIC31XX_DAC_OF_RIGHT |
  1369. AIC31XX_DAC_OF_SHIFTER |
  1370. AIC31XX_ADC_OF |
  1371. AIC31XX_ADC_OF_SHIFTER))
  1372. dev_warn(dev, "Unknown overflow interrupt flags: 0x%08x\n", value);
  1373. exit:
  1374. if (handled)
  1375. return IRQ_HANDLED;
  1376. else
  1377. return IRQ_NONE;
  1378. }
  1379. static void aic31xx_configure_ocmv(struct aic31xx_priv *priv)
  1380. {
  1381. struct device *dev = priv->dev;
  1382. int dvdd, avdd;
  1383. u32 value;
  1384. if (dev->fwnode &&
  1385. fwnode_property_read_u32(dev->fwnode, "ai31xx-ocmv", &value)) {
  1386. /* OCMV setting is forced by DT */
  1387. if (value <= 3) {
  1388. priv->ocmv = value;
  1389. return;
  1390. }
  1391. }
  1392. avdd = regulator_get_voltage(priv->supplies[3].consumer);
  1393. dvdd = regulator_get_voltage(priv->supplies[5].consumer);
  1394. if (avdd > 3600000 || dvdd > 1950000) {
  1395. dev_warn(dev,
  1396. "Too high supply voltage(s) AVDD: %d, DVDD: %d\n",
  1397. avdd, dvdd);
  1398. } else if (avdd == 3600000 && dvdd == 1950000) {
  1399. priv->ocmv = AIC31XX_HPD_OCMV_1_8V;
  1400. } else if (avdd >= 3300000 && dvdd >= 1800000) {
  1401. priv->ocmv = AIC31XX_HPD_OCMV_1_65V;
  1402. } else if (avdd >= 3000000 && dvdd >= 1650000) {
  1403. priv->ocmv = AIC31XX_HPD_OCMV_1_5V;
  1404. } else if (avdd >= 2700000 && dvdd >= 1525000) {
  1405. priv->ocmv = AIC31XX_HPD_OCMV_1_35V;
  1406. } else {
  1407. dev_warn(dev,
  1408. "Invalid supply voltage(s) AVDD: %d, DVDD: %d\n",
  1409. avdd, dvdd);
  1410. }
  1411. }
  1412. static const struct i2c_device_id aic31xx_i2c_id[] = {
  1413. { "tlv320aic310x", AIC3100 },
  1414. { "tlv320aic311x", AIC3110 },
  1415. { "tlv320aic3100", AIC3100 },
  1416. { "tlv320aic3110", AIC3110 },
  1417. { "tlv320aic3120", AIC3120 },
  1418. { "tlv320aic3111", AIC3111 },
  1419. { "tlv320dac3100", DAC3100 },
  1420. { "tlv320dac3101", DAC3101 },
  1421. { }
  1422. };
  1423. MODULE_DEVICE_TABLE(i2c, aic31xx_i2c_id);
  1424. static int aic31xx_i2c_probe(struct i2c_client *i2c)
  1425. {
  1426. struct aic31xx_priv *aic31xx;
  1427. unsigned int micbias_value = MICBIAS_2_0V;
  1428. const struct i2c_device_id *id = i2c_match_id(aic31xx_i2c_id, i2c);
  1429. int i, ret;
  1430. dev_dbg(&i2c->dev, "## %s: %s codec_type = %d\n", __func__,
  1431. id->name, (int)id->driver_data);
  1432. aic31xx = devm_kzalloc(&i2c->dev, sizeof(*aic31xx), GFP_KERNEL);
  1433. if (!aic31xx)
  1434. return -ENOMEM;
  1435. aic31xx->regmap = devm_regmap_init_i2c(i2c, &aic31xx_i2c_regmap);
  1436. if (IS_ERR(aic31xx->regmap)) {
  1437. ret = PTR_ERR(aic31xx->regmap);
  1438. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1439. ret);
  1440. return ret;
  1441. }
  1442. regcache_cache_only(aic31xx->regmap, true);
  1443. aic31xx->dev = &i2c->dev;
  1444. aic31xx->irq = i2c->irq;
  1445. aic31xx->codec_type = id->driver_data;
  1446. dev_set_drvdata(aic31xx->dev, aic31xx);
  1447. fwnode_property_read_u32(aic31xx->dev->fwnode, "ai31xx-micbias-vg",
  1448. &micbias_value);
  1449. switch (micbias_value) {
  1450. case MICBIAS_2_0V:
  1451. case MICBIAS_2_5V:
  1452. case MICBIAS_AVDDV:
  1453. aic31xx->micbias_vg = micbias_value;
  1454. break;
  1455. default:
  1456. dev_err(aic31xx->dev, "Bad ai31xx-micbias-vg value %d\n",
  1457. micbias_value);
  1458. aic31xx->micbias_vg = MICBIAS_2_0V;
  1459. }
  1460. if (dev_get_platdata(aic31xx->dev)) {
  1461. memcpy(&aic31xx->pdata, dev_get_platdata(aic31xx->dev), sizeof(aic31xx->pdata));
  1462. aic31xx->codec_type = aic31xx->pdata.codec_type;
  1463. aic31xx->micbias_vg = aic31xx->pdata.micbias_vg;
  1464. }
  1465. aic31xx->gpio_reset = devm_gpiod_get_optional(aic31xx->dev, "reset",
  1466. GPIOD_OUT_LOW);
  1467. if (IS_ERR(aic31xx->gpio_reset))
  1468. return dev_err_probe(aic31xx->dev, PTR_ERR(aic31xx->gpio_reset),
  1469. "not able to acquire gpio\n");
  1470. for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
  1471. aic31xx->supplies[i].supply = aic31xx_supply_names[i];
  1472. ret = devm_regulator_bulk_get(aic31xx->dev,
  1473. ARRAY_SIZE(aic31xx->supplies),
  1474. aic31xx->supplies);
  1475. if (ret)
  1476. return dev_err_probe(aic31xx->dev, ret, "Failed to request supplies\n");
  1477. aic31xx_configure_ocmv(aic31xx);
  1478. if (aic31xx->irq > 0) {
  1479. regmap_update_bits(aic31xx->regmap, AIC31XX_GPIO1,
  1480. AIC31XX_GPIO1_FUNC_MASK,
  1481. AIC31XX_GPIO1_INT1 <<
  1482. AIC31XX_GPIO1_FUNC_SHIFT);
  1483. regmap_write(aic31xx->regmap, AIC31XX_INT1CTRL,
  1484. AIC31XX_HSPLUGDET |
  1485. AIC31XX_BUTTONPRESSDET |
  1486. AIC31XX_SC |
  1487. AIC31XX_ENGINE);
  1488. ret = devm_request_threaded_irq(aic31xx->dev, aic31xx->irq,
  1489. NULL, aic31xx_irq,
  1490. IRQF_ONESHOT, "aic31xx-irq",
  1491. aic31xx);
  1492. if (ret) {
  1493. dev_err(aic31xx->dev, "Unable to request IRQ\n");
  1494. return ret;
  1495. }
  1496. }
  1497. if (aic31xx->codec_type & DAC31XX_BIT)
  1498. return devm_snd_soc_register_component(&i2c->dev,
  1499. &soc_codec_driver_aic31xx,
  1500. dac31xx_dai_driver,
  1501. ARRAY_SIZE(dac31xx_dai_driver));
  1502. else
  1503. return devm_snd_soc_register_component(&i2c->dev,
  1504. &soc_codec_driver_aic31xx,
  1505. aic31xx_dai_driver,
  1506. ARRAY_SIZE(aic31xx_dai_driver));
  1507. }
  1508. static struct i2c_driver aic31xx_i2c_driver = {
  1509. .driver = {
  1510. .name = "tlv320aic31xx-codec",
  1511. .of_match_table = of_match_ptr(tlv320aic31xx_of_match),
  1512. .acpi_match_table = ACPI_PTR(aic31xx_acpi_match),
  1513. },
  1514. .probe_new = aic31xx_i2c_probe,
  1515. .id_table = aic31xx_i2c_id,
  1516. };
  1517. module_i2c_driver(aic31xx_i2c_driver);
  1518. MODULE_AUTHOR("Jyri Sarha <[email protected]>");
  1519. MODULE_DESCRIPTION("ASoC TLV320AIC31xx CODEC Driver");
  1520. MODULE_LICENSE("GPL v2");