tlv320aic3x.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* ALSA SoC TLV320AIC3X codec driver
  3. *
  4. * Author: Vladimir Barinov, <[email protected]>
  5. * Copyright: (C) 2007 MontaVista Software, Inc., <[email protected]>
  6. *
  7. * Based on sound/soc/codecs/wm8753.c by Liam Girdwood
  8. *
  9. * Notes:
  10. * The AIC3X is a driver for a low power stereo audio
  11. * codecs aic31, aic32, aic33, aic3007.
  12. *
  13. * It supports full aic33 codec functionality.
  14. * The compatibility with aic32, aic31 and aic3007 is as follows:
  15. * aic32/aic3007 | aic31
  16. * ---------------------------------------
  17. * MONO_LOUT -> N/A | MONO_LOUT -> N/A
  18. * | IN1L -> LINE1L
  19. * | IN1R -> LINE1R
  20. * | IN2L -> LINE2L
  21. * | IN2R -> LINE2R
  22. * | MIC3L/R -> N/A
  23. * truncated internal functionality in
  24. * accordance with documentation
  25. * ---------------------------------------
  26. *
  27. * Hence the machine layer should disable unsupported inputs/outputs by
  28. * snd_soc_dapm_disable_pin(codec, "MONO_LOUT"), etc.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/init.h>
  33. #include <linux/delay.h>
  34. #include <linux/pm.h>
  35. #include <linux/i2c.h>
  36. #include <linux/gpio.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <linux/of.h>
  39. #include <linux/of_gpio.h>
  40. #include <linux/slab.h>
  41. #include <sound/core.h>
  42. #include <sound/pcm.h>
  43. #include <sound/pcm_params.h>
  44. #include <sound/soc.h>
  45. #include <sound/initval.h>
  46. #include <sound/tlv.h>
  47. #include <sound/tlv320aic3x.h>
  48. #include "tlv320aic3x.h"
  49. #define AIC3X_NUM_SUPPLIES 4
  50. static const char *aic3x_supply_names[AIC3X_NUM_SUPPLIES] = {
  51. "IOVDD", /* I/O Voltage */
  52. "DVDD", /* Digital Core Voltage */
  53. "AVDD", /* Analog DAC Voltage */
  54. "DRVDD", /* ADC Analog and Output Driver Voltage */
  55. };
  56. static LIST_HEAD(reset_list);
  57. struct aic3x_priv;
  58. struct aic3x_disable_nb {
  59. struct notifier_block nb;
  60. struct aic3x_priv *aic3x;
  61. };
  62. /* codec private data */
  63. struct aic3x_priv {
  64. struct snd_soc_component *component;
  65. struct regmap *regmap;
  66. struct regulator_bulk_data supplies[AIC3X_NUM_SUPPLIES];
  67. struct aic3x_disable_nb disable_nb[AIC3X_NUM_SUPPLIES];
  68. struct aic3x_setup_data *setup;
  69. unsigned int sysclk;
  70. unsigned int dai_fmt;
  71. unsigned int tdm_delay;
  72. unsigned int slot_width;
  73. struct list_head list;
  74. int master;
  75. int gpio_reset;
  76. int power;
  77. u16 model;
  78. /* Selects the micbias voltage */
  79. enum aic3x_micbias_voltage micbias_vg;
  80. /* Output Common-Mode Voltage */
  81. u8 ocmv;
  82. };
  83. static const struct reg_default aic3x_reg[] = {
  84. { 0, 0x00 }, { 1, 0x00 }, { 2, 0x00 }, { 3, 0x10 },
  85. { 4, 0x04 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
  86. { 8, 0x00 }, { 9, 0x00 }, { 10, 0x00 }, { 11, 0x01 },
  87. { 12, 0x00 }, { 13, 0x00 }, { 14, 0x00 }, { 15, 0x80 },
  88. { 16, 0x80 }, { 17, 0xff }, { 18, 0xff }, { 19, 0x78 },
  89. { 20, 0x78 }, { 21, 0x78 }, { 22, 0x78 }, { 23, 0x78 },
  90. { 24, 0x78 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0xfe },
  91. { 28, 0x00 }, { 29, 0x00 }, { 30, 0xfe }, { 31, 0x00 },
  92. { 32, 0x18 }, { 33, 0x18 }, { 34, 0x00 }, { 35, 0x00 },
  93. { 36, 0x00 }, { 37, 0x00 }, { 38, 0x00 }, { 39, 0x00 },
  94. { 40, 0x00 }, { 41, 0x00 }, { 42, 0x00 }, { 43, 0x80 },
  95. { 44, 0x80 }, { 45, 0x00 }, { 46, 0x00 }, { 47, 0x00 },
  96. { 48, 0x00 }, { 49, 0x00 }, { 50, 0x00 }, { 51, 0x04 },
  97. { 52, 0x00 }, { 53, 0x00 }, { 54, 0x00 }, { 55, 0x00 },
  98. { 56, 0x00 }, { 57, 0x00 }, { 58, 0x04 }, { 59, 0x00 },
  99. { 60, 0x00 }, { 61, 0x00 }, { 62, 0x00 }, { 63, 0x00 },
  100. { 64, 0x00 }, { 65, 0x04 }, { 66, 0x00 }, { 67, 0x00 },
  101. { 68, 0x00 }, { 69, 0x00 }, { 70, 0x00 }, { 71, 0x00 },
  102. { 72, 0x04 }, { 73, 0x00 }, { 74, 0x00 }, { 75, 0x00 },
  103. { 76, 0x00 }, { 77, 0x00 }, { 78, 0x00 }, { 79, 0x00 },
  104. { 80, 0x00 }, { 81, 0x00 }, { 82, 0x00 }, { 83, 0x00 },
  105. { 84, 0x00 }, { 85, 0x00 }, { 86, 0x00 }, { 87, 0x00 },
  106. { 88, 0x00 }, { 89, 0x00 }, { 90, 0x00 }, { 91, 0x00 },
  107. { 92, 0x00 }, { 93, 0x00 }, { 94, 0x00 }, { 95, 0x00 },
  108. { 96, 0x00 }, { 97, 0x00 }, { 98, 0x00 }, { 99, 0x00 },
  109. { 100, 0x00 }, { 101, 0x00 }, { 102, 0x02 }, { 103, 0x00 },
  110. { 104, 0x00 }, { 105, 0x00 }, { 106, 0x00 }, { 107, 0x00 },
  111. { 108, 0x00 }, { 109, 0x00 },
  112. };
  113. static bool aic3x_volatile_reg(struct device *dev, unsigned int reg)
  114. {
  115. switch (reg) {
  116. case AIC3X_RESET:
  117. return true;
  118. default:
  119. return false;
  120. }
  121. }
  122. const struct regmap_config aic3x_regmap = {
  123. .max_register = DAC_ICC_ADJ,
  124. .reg_defaults = aic3x_reg,
  125. .num_reg_defaults = ARRAY_SIZE(aic3x_reg),
  126. .volatile_reg = aic3x_volatile_reg,
  127. .cache_type = REGCACHE_RBTREE,
  128. };
  129. EXPORT_SYMBOL_GPL(aic3x_regmap);
  130. #define SOC_DAPM_SINGLE_AIC3X(xname, reg, shift, mask, invert) \
  131. SOC_SINGLE_EXT(xname, reg, shift, mask, invert, \
  132. snd_soc_dapm_get_volsw, snd_soc_dapm_put_volsw_aic3x)
  133. /*
  134. * All input lines are connected when !0xf and disconnected with 0xf bit field,
  135. * so we have to use specific dapm_put call for input mixer
  136. */
  137. static int snd_soc_dapm_put_volsw_aic3x(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
  141. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  142. struct soc_mixer_control *mc =
  143. (struct soc_mixer_control *)kcontrol->private_value;
  144. unsigned int reg = mc->reg;
  145. unsigned int shift = mc->shift;
  146. int max = mc->max;
  147. unsigned int mask = (1 << fls(max)) - 1;
  148. unsigned int invert = mc->invert;
  149. unsigned short val;
  150. struct snd_soc_dapm_update update = {};
  151. int connect, change;
  152. val = (ucontrol->value.integer.value[0] & mask);
  153. mask = 0xf;
  154. if (val)
  155. val = mask;
  156. connect = !!val;
  157. if (invert)
  158. val = mask - val;
  159. mask <<= shift;
  160. val <<= shift;
  161. change = snd_soc_component_test_bits(component, reg, mask, val);
  162. if (change) {
  163. update.kcontrol = kcontrol;
  164. update.reg = reg;
  165. update.mask = mask;
  166. update.val = val;
  167. snd_soc_dapm_mixer_update_power(dapm, kcontrol, connect,
  168. &update);
  169. }
  170. return change;
  171. }
  172. /*
  173. * mic bias power on/off share the same register bits with
  174. * output voltage of mic bias. when power on mic bias, we
  175. * need reclaim it to voltage value.
  176. * 0x0 = Powered off
  177. * 0x1 = MICBIAS output is powered to 2.0V,
  178. * 0x2 = MICBIAS output is powered to 2.5V
  179. * 0x3 = MICBIAS output is connected to AVDD
  180. */
  181. static int mic_bias_event(struct snd_soc_dapm_widget *w,
  182. struct snd_kcontrol *kcontrol, int event)
  183. {
  184. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  185. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  186. switch (event) {
  187. case SND_SOC_DAPM_POST_PMU:
  188. /* change mic bias voltage to user defined */
  189. snd_soc_component_update_bits(component, MICBIAS_CTRL,
  190. MICBIAS_LEVEL_MASK,
  191. aic3x->micbias_vg << MICBIAS_LEVEL_SHIFT);
  192. break;
  193. case SND_SOC_DAPM_PRE_PMD:
  194. snd_soc_component_update_bits(component, MICBIAS_CTRL,
  195. MICBIAS_LEVEL_MASK, 0);
  196. break;
  197. }
  198. return 0;
  199. }
  200. static const char * const aic3x_left_dac_mux[] = {
  201. "DAC_L1", "DAC_L3", "DAC_L2" };
  202. static SOC_ENUM_SINGLE_DECL(aic3x_left_dac_enum, DAC_LINE_MUX, 6,
  203. aic3x_left_dac_mux);
  204. static const char * const aic3x_right_dac_mux[] = {
  205. "DAC_R1", "DAC_R3", "DAC_R2" };
  206. static SOC_ENUM_SINGLE_DECL(aic3x_right_dac_enum, DAC_LINE_MUX, 4,
  207. aic3x_right_dac_mux);
  208. static const char * const aic3x_left_hpcom_mux[] = {
  209. "differential of HPLOUT", "constant VCM", "single-ended" };
  210. static SOC_ENUM_SINGLE_DECL(aic3x_left_hpcom_enum, HPLCOM_CFG, 4,
  211. aic3x_left_hpcom_mux);
  212. static const char * const aic3x_right_hpcom_mux[] = {
  213. "differential of HPROUT", "constant VCM", "single-ended",
  214. "differential of HPLCOM", "external feedback" };
  215. static SOC_ENUM_SINGLE_DECL(aic3x_right_hpcom_enum, HPRCOM_CFG, 3,
  216. aic3x_right_hpcom_mux);
  217. static const char * const aic3x_linein_mode_mux[] = {
  218. "single-ended", "differential" };
  219. static SOC_ENUM_SINGLE_DECL(aic3x_line1l_2_l_enum, LINE1L_2_LADC_CTRL, 7,
  220. aic3x_linein_mode_mux);
  221. static SOC_ENUM_SINGLE_DECL(aic3x_line1l_2_r_enum, LINE1L_2_RADC_CTRL, 7,
  222. aic3x_linein_mode_mux);
  223. static SOC_ENUM_SINGLE_DECL(aic3x_line1r_2_l_enum, LINE1R_2_LADC_CTRL, 7,
  224. aic3x_linein_mode_mux);
  225. static SOC_ENUM_SINGLE_DECL(aic3x_line1r_2_r_enum, LINE1R_2_RADC_CTRL, 7,
  226. aic3x_linein_mode_mux);
  227. static SOC_ENUM_SINGLE_DECL(aic3x_line2l_2_ldac_enum, LINE2L_2_LADC_CTRL, 7,
  228. aic3x_linein_mode_mux);
  229. static SOC_ENUM_SINGLE_DECL(aic3x_line2r_2_rdac_enum, LINE2R_2_RADC_CTRL, 7,
  230. aic3x_linein_mode_mux);
  231. static const char * const aic3x_adc_hpf[] = {
  232. "Disabled", "0.0045xFs", "0.0125xFs", "0.025xFs" };
  233. static SOC_ENUM_DOUBLE_DECL(aic3x_adc_hpf_enum, AIC3X_CODEC_DFILT_CTRL, 6, 4,
  234. aic3x_adc_hpf);
  235. static const char * const aic3x_agc_level[] = {
  236. "-5.5dB", "-8dB", "-10dB", "-12dB",
  237. "-14dB", "-17dB", "-20dB", "-24dB" };
  238. static SOC_ENUM_SINGLE_DECL(aic3x_lagc_level_enum, LAGC_CTRL_A, 4,
  239. aic3x_agc_level);
  240. static SOC_ENUM_SINGLE_DECL(aic3x_ragc_level_enum, RAGC_CTRL_A, 4,
  241. aic3x_agc_level);
  242. static const char * const aic3x_agc_attack[] = {
  243. "8ms", "11ms", "16ms", "20ms" };
  244. static SOC_ENUM_SINGLE_DECL(aic3x_lagc_attack_enum, LAGC_CTRL_A, 2,
  245. aic3x_agc_attack);
  246. static SOC_ENUM_SINGLE_DECL(aic3x_ragc_attack_enum, RAGC_CTRL_A, 2,
  247. aic3x_agc_attack);
  248. static const char * const aic3x_agc_decay[] = {
  249. "100ms", "200ms", "400ms", "500ms" };
  250. static SOC_ENUM_SINGLE_DECL(aic3x_lagc_decay_enum, LAGC_CTRL_A, 0,
  251. aic3x_agc_decay);
  252. static SOC_ENUM_SINGLE_DECL(aic3x_ragc_decay_enum, RAGC_CTRL_A, 0,
  253. aic3x_agc_decay);
  254. static const char * const aic3x_poweron_time[] = {
  255. "0us", "10us", "100us", "1ms", "10ms", "50ms",
  256. "100ms", "200ms", "400ms", "800ms", "2s", "4s" };
  257. static SOC_ENUM_SINGLE_DECL(aic3x_poweron_time_enum, HPOUT_POP_REDUCTION, 4,
  258. aic3x_poweron_time);
  259. static const char * const aic3x_rampup_step[] = { "0ms", "1ms", "2ms", "4ms" };
  260. static SOC_ENUM_SINGLE_DECL(aic3x_rampup_step_enum, HPOUT_POP_REDUCTION, 2,
  261. aic3x_rampup_step);
  262. /*
  263. * DAC digital volumes. From -63.5 to 0 dB in 0.5 dB steps
  264. */
  265. static DECLARE_TLV_DB_SCALE(dac_tlv, -6350, 50, 0);
  266. /* ADC PGA gain volumes. From 0 to 59.5 dB in 0.5 dB steps */
  267. static DECLARE_TLV_DB_SCALE(adc_tlv, 0, 50, 0);
  268. /*
  269. * Output stage volumes. From -78.3 to 0 dB. Muted below -78.3 dB.
  270. * Step size is approximately 0.5 dB over most of the scale but increasing
  271. * near the very low levels.
  272. * Define dB scale so that it is mostly correct for range about -55 to 0 dB
  273. * but having increasing dB difference below that (and where it doesn't count
  274. * so much). This setting shows -50 dB (actual is -50.3 dB) for register
  275. * value 100 and -58.5 dB (actual is -78.3 dB) for register value 117.
  276. */
  277. static DECLARE_TLV_DB_SCALE(output_stage_tlv, -5900, 50, 1);
  278. /* Output volumes. From 0 to 9 dB in 1 dB steps */
  279. static const DECLARE_TLV_DB_SCALE(out_tlv, 0, 100, 0);
  280. static const struct snd_kcontrol_new aic3x_snd_controls[] = {
  281. /* Output */
  282. SOC_DOUBLE_R_TLV("PCM Playback Volume",
  283. LDAC_VOL, RDAC_VOL, 0, 0x7f, 1, dac_tlv),
  284. /*
  285. * Output controls that map to output mixer switches. Note these are
  286. * only for swapped L-to-R and R-to-L routes. See below stereo controls
  287. * for direct L-to-L and R-to-R routes.
  288. */
  289. SOC_SINGLE_TLV("Left Line Mixer PGAR Bypass Volume",
  290. PGAR_2_LLOPM_VOL, 0, 118, 1, output_stage_tlv),
  291. SOC_SINGLE_TLV("Left Line Mixer DACR1 Playback Volume",
  292. DACR1_2_LLOPM_VOL, 0, 118, 1, output_stage_tlv),
  293. SOC_SINGLE_TLV("Right Line Mixer PGAL Bypass Volume",
  294. PGAL_2_RLOPM_VOL, 0, 118, 1, output_stage_tlv),
  295. SOC_SINGLE_TLV("Right Line Mixer DACL1 Playback Volume",
  296. DACL1_2_RLOPM_VOL, 0, 118, 1, output_stage_tlv),
  297. SOC_SINGLE_TLV("Left HP Mixer PGAR Bypass Volume",
  298. PGAR_2_HPLOUT_VOL, 0, 118, 1, output_stage_tlv),
  299. SOC_SINGLE_TLV("Left HP Mixer DACR1 Playback Volume",
  300. DACR1_2_HPLOUT_VOL, 0, 118, 1, output_stage_tlv),
  301. SOC_SINGLE_TLV("Right HP Mixer PGAL Bypass Volume",
  302. PGAL_2_HPROUT_VOL, 0, 118, 1, output_stage_tlv),
  303. SOC_SINGLE_TLV("Right HP Mixer DACL1 Playback Volume",
  304. DACL1_2_HPROUT_VOL, 0, 118, 1, output_stage_tlv),
  305. SOC_SINGLE_TLV("Left HPCOM Mixer PGAR Bypass Volume",
  306. PGAR_2_HPLCOM_VOL, 0, 118, 1, output_stage_tlv),
  307. SOC_SINGLE_TLV("Left HPCOM Mixer DACR1 Playback Volume",
  308. DACR1_2_HPLCOM_VOL, 0, 118, 1, output_stage_tlv),
  309. SOC_SINGLE_TLV("Right HPCOM Mixer PGAL Bypass Volume",
  310. PGAL_2_HPRCOM_VOL, 0, 118, 1, output_stage_tlv),
  311. SOC_SINGLE_TLV("Right HPCOM Mixer DACL1 Playback Volume",
  312. DACL1_2_HPRCOM_VOL, 0, 118, 1, output_stage_tlv),
  313. /* Stereo output controls for direct L-to-L and R-to-R routes */
  314. SOC_DOUBLE_R_TLV("Line PGA Bypass Volume",
  315. PGAL_2_LLOPM_VOL, PGAR_2_RLOPM_VOL,
  316. 0, 118, 1, output_stage_tlv),
  317. SOC_DOUBLE_R_TLV("Line DAC Playback Volume",
  318. DACL1_2_LLOPM_VOL, DACR1_2_RLOPM_VOL,
  319. 0, 118, 1, output_stage_tlv),
  320. SOC_DOUBLE_R_TLV("HP PGA Bypass Volume",
  321. PGAL_2_HPLOUT_VOL, PGAR_2_HPROUT_VOL,
  322. 0, 118, 1, output_stage_tlv),
  323. SOC_DOUBLE_R_TLV("HP DAC Playback Volume",
  324. DACL1_2_HPLOUT_VOL, DACR1_2_HPROUT_VOL,
  325. 0, 118, 1, output_stage_tlv),
  326. SOC_DOUBLE_R_TLV("HPCOM PGA Bypass Volume",
  327. PGAL_2_HPLCOM_VOL, PGAR_2_HPRCOM_VOL,
  328. 0, 118, 1, output_stage_tlv),
  329. SOC_DOUBLE_R_TLV("HPCOM DAC Playback Volume",
  330. DACL1_2_HPLCOM_VOL, DACR1_2_HPRCOM_VOL,
  331. 0, 118, 1, output_stage_tlv),
  332. /* Output pin controls */
  333. SOC_DOUBLE_R_TLV("Line Playback Volume", LLOPM_CTRL, RLOPM_CTRL, 4,
  334. 9, 0, out_tlv),
  335. SOC_DOUBLE_R("Line Playback Switch", LLOPM_CTRL, RLOPM_CTRL, 3,
  336. 0x01, 0),
  337. SOC_DOUBLE_R_TLV("HP Playback Volume", HPLOUT_CTRL, HPROUT_CTRL, 4,
  338. 9, 0, out_tlv),
  339. SOC_DOUBLE_R("HP Playback Switch", HPLOUT_CTRL, HPROUT_CTRL, 3,
  340. 0x01, 0),
  341. SOC_DOUBLE_R_TLV("HPCOM Playback Volume", HPLCOM_CTRL, HPRCOM_CTRL,
  342. 4, 9, 0, out_tlv),
  343. SOC_DOUBLE_R("HPCOM Playback Switch", HPLCOM_CTRL, HPRCOM_CTRL, 3,
  344. 0x01, 0),
  345. /*
  346. * Note: enable Automatic input Gain Controller with care. It can
  347. * adjust PGA to max value when ADC is on and will never go back.
  348. */
  349. SOC_DOUBLE_R("AGC Switch", LAGC_CTRL_A, RAGC_CTRL_A, 7, 0x01, 0),
  350. SOC_ENUM("Left AGC Target level", aic3x_lagc_level_enum),
  351. SOC_ENUM("Right AGC Target level", aic3x_ragc_level_enum),
  352. SOC_ENUM("Left AGC Attack time", aic3x_lagc_attack_enum),
  353. SOC_ENUM("Right AGC Attack time", aic3x_ragc_attack_enum),
  354. SOC_ENUM("Left AGC Decay time", aic3x_lagc_decay_enum),
  355. SOC_ENUM("Right AGC Decay time", aic3x_ragc_decay_enum),
  356. /* De-emphasis */
  357. SOC_DOUBLE("De-emphasis Switch", AIC3X_CODEC_DFILT_CTRL, 2, 0, 0x01, 0),
  358. /* Input */
  359. SOC_DOUBLE_R_TLV("PGA Capture Volume", LADC_VOL, RADC_VOL,
  360. 0, 119, 0, adc_tlv),
  361. SOC_DOUBLE_R("PGA Capture Switch", LADC_VOL, RADC_VOL, 7, 0x01, 1),
  362. SOC_ENUM("ADC HPF Cut-off", aic3x_adc_hpf_enum),
  363. /* Pop reduction */
  364. SOC_ENUM("Output Driver Power-On time", aic3x_poweron_time_enum),
  365. SOC_ENUM("Output Driver Ramp-up step", aic3x_rampup_step_enum),
  366. };
  367. /* For other than tlv320aic3104 */
  368. static const struct snd_kcontrol_new aic3x_extra_snd_controls[] = {
  369. /*
  370. * Output controls that map to output mixer switches. Note these are
  371. * only for swapped L-to-R and R-to-L routes. See below stereo controls
  372. * for direct L-to-L and R-to-R routes.
  373. */
  374. SOC_SINGLE_TLV("Left Line Mixer Line2R Bypass Volume",
  375. LINE2R_2_LLOPM_VOL, 0, 118, 1, output_stage_tlv),
  376. SOC_SINGLE_TLV("Right Line Mixer Line2L Bypass Volume",
  377. LINE2L_2_RLOPM_VOL, 0, 118, 1, output_stage_tlv),
  378. SOC_SINGLE_TLV("Left HP Mixer Line2R Bypass Volume",
  379. LINE2R_2_HPLOUT_VOL, 0, 118, 1, output_stage_tlv),
  380. SOC_SINGLE_TLV("Right HP Mixer Line2L Bypass Volume",
  381. LINE2L_2_HPROUT_VOL, 0, 118, 1, output_stage_tlv),
  382. SOC_SINGLE_TLV("Left HPCOM Mixer Line2R Bypass Volume",
  383. LINE2R_2_HPLCOM_VOL, 0, 118, 1, output_stage_tlv),
  384. SOC_SINGLE_TLV("Right HPCOM Mixer Line2L Bypass Volume",
  385. LINE2L_2_HPRCOM_VOL, 0, 118, 1, output_stage_tlv),
  386. /* Stereo output controls for direct L-to-L and R-to-R routes */
  387. SOC_DOUBLE_R_TLV("Line Line2 Bypass Volume",
  388. LINE2L_2_LLOPM_VOL, LINE2R_2_RLOPM_VOL,
  389. 0, 118, 1, output_stage_tlv),
  390. SOC_DOUBLE_R_TLV("HP Line2 Bypass Volume",
  391. LINE2L_2_HPLOUT_VOL, LINE2R_2_HPROUT_VOL,
  392. 0, 118, 1, output_stage_tlv),
  393. SOC_DOUBLE_R_TLV("HPCOM Line2 Bypass Volume",
  394. LINE2L_2_HPLCOM_VOL, LINE2R_2_HPRCOM_VOL,
  395. 0, 118, 1, output_stage_tlv),
  396. };
  397. static const struct snd_kcontrol_new aic3x_mono_controls[] = {
  398. SOC_DOUBLE_R_TLV("Mono Line2 Bypass Volume",
  399. LINE2L_2_MONOLOPM_VOL, LINE2R_2_MONOLOPM_VOL,
  400. 0, 118, 1, output_stage_tlv),
  401. SOC_DOUBLE_R_TLV("Mono PGA Bypass Volume",
  402. PGAL_2_MONOLOPM_VOL, PGAR_2_MONOLOPM_VOL,
  403. 0, 118, 1, output_stage_tlv),
  404. SOC_DOUBLE_R_TLV("Mono DAC Playback Volume",
  405. DACL1_2_MONOLOPM_VOL, DACR1_2_MONOLOPM_VOL,
  406. 0, 118, 1, output_stage_tlv),
  407. SOC_SINGLE("Mono Playback Switch", MONOLOPM_CTRL, 3, 0x01, 0),
  408. SOC_SINGLE_TLV("Mono Playback Volume", MONOLOPM_CTRL, 4, 9, 0,
  409. out_tlv),
  410. };
  411. /*
  412. * Class-D amplifier gain. From 0 to 18 dB in 6 dB steps
  413. */
  414. static DECLARE_TLV_DB_SCALE(classd_amp_tlv, 0, 600, 0);
  415. static const struct snd_kcontrol_new aic3x_classd_amp_gain_ctrl =
  416. SOC_DOUBLE_TLV("Class-D Playback Volume", CLASSD_CTRL, 6, 4, 3, 0, classd_amp_tlv);
  417. /* Left DAC Mux */
  418. static const struct snd_kcontrol_new aic3x_left_dac_mux_controls =
  419. SOC_DAPM_ENUM("Route", aic3x_left_dac_enum);
  420. /* Right DAC Mux */
  421. static const struct snd_kcontrol_new aic3x_right_dac_mux_controls =
  422. SOC_DAPM_ENUM("Route", aic3x_right_dac_enum);
  423. /* Left HPCOM Mux */
  424. static const struct snd_kcontrol_new aic3x_left_hpcom_mux_controls =
  425. SOC_DAPM_ENUM("Route", aic3x_left_hpcom_enum);
  426. /* Right HPCOM Mux */
  427. static const struct snd_kcontrol_new aic3x_right_hpcom_mux_controls =
  428. SOC_DAPM_ENUM("Route", aic3x_right_hpcom_enum);
  429. /* Left Line Mixer */
  430. static const struct snd_kcontrol_new aic3x_left_line_mixer_controls[] = {
  431. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_LLOPM_VOL, 7, 1, 0),
  432. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_LLOPM_VOL, 7, 1, 0),
  433. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_LLOPM_VOL, 7, 1, 0),
  434. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_LLOPM_VOL, 7, 1, 0),
  435. /* Not on tlv320aic3104 */
  436. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_LLOPM_VOL, 7, 1, 0),
  437. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_LLOPM_VOL, 7, 1, 0),
  438. };
  439. /* Right Line Mixer */
  440. static const struct snd_kcontrol_new aic3x_right_line_mixer_controls[] = {
  441. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_RLOPM_VOL, 7, 1, 0),
  442. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_RLOPM_VOL, 7, 1, 0),
  443. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_RLOPM_VOL, 7, 1, 0),
  444. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_RLOPM_VOL, 7, 1, 0),
  445. /* Not on tlv320aic3104 */
  446. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_RLOPM_VOL, 7, 1, 0),
  447. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_RLOPM_VOL, 7, 1, 0),
  448. };
  449. /* Mono Mixer */
  450. static const struct snd_kcontrol_new aic3x_mono_mixer_controls[] = {
  451. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_MONOLOPM_VOL, 7, 1, 0),
  452. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_MONOLOPM_VOL, 7, 1, 0),
  453. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_MONOLOPM_VOL, 7, 1, 0),
  454. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_MONOLOPM_VOL, 7, 1, 0),
  455. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_MONOLOPM_VOL, 7, 1, 0),
  456. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_MONOLOPM_VOL, 7, 1, 0),
  457. };
  458. /* Left HP Mixer */
  459. static const struct snd_kcontrol_new aic3x_left_hp_mixer_controls[] = {
  460. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPLOUT_VOL, 7, 1, 0),
  461. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPLOUT_VOL, 7, 1, 0),
  462. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPLOUT_VOL, 7, 1, 0),
  463. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPLOUT_VOL, 7, 1, 0),
  464. /* Not on tlv320aic3104 */
  465. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPLOUT_VOL, 7, 1, 0),
  466. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPLOUT_VOL, 7, 1, 0),
  467. };
  468. /* Right HP Mixer */
  469. static const struct snd_kcontrol_new aic3x_right_hp_mixer_controls[] = {
  470. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPROUT_VOL, 7, 1, 0),
  471. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPROUT_VOL, 7, 1, 0),
  472. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPROUT_VOL, 7, 1, 0),
  473. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPROUT_VOL, 7, 1, 0),
  474. /* Not on tlv320aic3104 */
  475. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPROUT_VOL, 7, 1, 0),
  476. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPROUT_VOL, 7, 1, 0),
  477. };
  478. /* Left HPCOM Mixer */
  479. static const struct snd_kcontrol_new aic3x_left_hpcom_mixer_controls[] = {
  480. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPLCOM_VOL, 7, 1, 0),
  481. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPLCOM_VOL, 7, 1, 0),
  482. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPLCOM_VOL, 7, 1, 0),
  483. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPLCOM_VOL, 7, 1, 0),
  484. /* Not on tlv320aic3104 */
  485. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPLCOM_VOL, 7, 1, 0),
  486. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPLCOM_VOL, 7, 1, 0),
  487. };
  488. /* Right HPCOM Mixer */
  489. static const struct snd_kcontrol_new aic3x_right_hpcom_mixer_controls[] = {
  490. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPRCOM_VOL, 7, 1, 0),
  491. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPRCOM_VOL, 7, 1, 0),
  492. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPRCOM_VOL, 7, 1, 0),
  493. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPRCOM_VOL, 7, 1, 0),
  494. /* Not on tlv320aic3104 */
  495. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPRCOM_VOL, 7, 1, 0),
  496. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPRCOM_VOL, 7, 1, 0),
  497. };
  498. /* Left PGA Mixer */
  499. static const struct snd_kcontrol_new aic3x_left_pga_mixer_controls[] = {
  500. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_LADC_CTRL, 3, 1, 1),
  501. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_LADC_CTRL, 3, 1, 1),
  502. SOC_DAPM_SINGLE_AIC3X("Line2L Switch", LINE2L_2_LADC_CTRL, 3, 1, 1),
  503. SOC_DAPM_SINGLE_AIC3X("Mic3L Switch", MIC3LR_2_LADC_CTRL, 4, 1, 1),
  504. SOC_DAPM_SINGLE_AIC3X("Mic3R Switch", MIC3LR_2_LADC_CTRL, 0, 1, 1),
  505. };
  506. /* Right PGA Mixer */
  507. static const struct snd_kcontrol_new aic3x_right_pga_mixer_controls[] = {
  508. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_RADC_CTRL, 3, 1, 1),
  509. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_RADC_CTRL, 3, 1, 1),
  510. SOC_DAPM_SINGLE_AIC3X("Line2R Switch", LINE2R_2_RADC_CTRL, 3, 1, 1),
  511. SOC_DAPM_SINGLE_AIC3X("Mic3L Switch", MIC3LR_2_RADC_CTRL, 4, 1, 1),
  512. SOC_DAPM_SINGLE_AIC3X("Mic3R Switch", MIC3LR_2_RADC_CTRL, 0, 1, 1),
  513. };
  514. /* Left PGA Mixer for tlv320aic3104 */
  515. static const struct snd_kcontrol_new aic3104_left_pga_mixer_controls[] = {
  516. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_LADC_CTRL, 3, 1, 1),
  517. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_LADC_CTRL, 3, 1, 1),
  518. SOC_DAPM_SINGLE_AIC3X("Mic2L Switch", MIC3LR_2_LADC_CTRL, 4, 1, 1),
  519. SOC_DAPM_SINGLE_AIC3X("Mic2R Switch", MIC3LR_2_LADC_CTRL, 0, 1, 1),
  520. };
  521. /* Right PGA Mixer for tlv320aic3104 */
  522. static const struct snd_kcontrol_new aic3104_right_pga_mixer_controls[] = {
  523. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_RADC_CTRL, 3, 1, 1),
  524. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_RADC_CTRL, 3, 1, 1),
  525. SOC_DAPM_SINGLE_AIC3X("Mic2L Switch", MIC3LR_2_RADC_CTRL, 4, 1, 1),
  526. SOC_DAPM_SINGLE_AIC3X("Mic2R Switch", MIC3LR_2_RADC_CTRL, 0, 1, 1),
  527. };
  528. /* Left Line1 Mux */
  529. static const struct snd_kcontrol_new aic3x_left_line1l_mux_controls =
  530. SOC_DAPM_ENUM("Route", aic3x_line1l_2_l_enum);
  531. static const struct snd_kcontrol_new aic3x_right_line1l_mux_controls =
  532. SOC_DAPM_ENUM("Route", aic3x_line1l_2_r_enum);
  533. /* Right Line1 Mux */
  534. static const struct snd_kcontrol_new aic3x_right_line1r_mux_controls =
  535. SOC_DAPM_ENUM("Route", aic3x_line1r_2_r_enum);
  536. static const struct snd_kcontrol_new aic3x_left_line1r_mux_controls =
  537. SOC_DAPM_ENUM("Route", aic3x_line1r_2_l_enum);
  538. /* Left Line2 Mux */
  539. static const struct snd_kcontrol_new aic3x_left_line2_mux_controls =
  540. SOC_DAPM_ENUM("Route", aic3x_line2l_2_ldac_enum);
  541. /* Right Line2 Mux */
  542. static const struct snd_kcontrol_new aic3x_right_line2_mux_controls =
  543. SOC_DAPM_ENUM("Route", aic3x_line2r_2_rdac_enum);
  544. static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
  545. /* Left DAC to Left Outputs */
  546. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", DAC_PWR, 7, 0),
  547. SND_SOC_DAPM_MUX("Left DAC Mux", SND_SOC_NOPM, 0, 0,
  548. &aic3x_left_dac_mux_controls),
  549. SND_SOC_DAPM_MUX("Left HPCOM Mux", SND_SOC_NOPM, 0, 0,
  550. &aic3x_left_hpcom_mux_controls),
  551. SND_SOC_DAPM_PGA("Left Line Out", LLOPM_CTRL, 0, 0, NULL, 0),
  552. SND_SOC_DAPM_PGA("Left HP Out", HPLOUT_CTRL, 0, 0, NULL, 0),
  553. SND_SOC_DAPM_PGA("Left HP Com", HPLCOM_CTRL, 0, 0, NULL, 0),
  554. /* Right DAC to Right Outputs */
  555. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", DAC_PWR, 6, 0),
  556. SND_SOC_DAPM_MUX("Right DAC Mux", SND_SOC_NOPM, 0, 0,
  557. &aic3x_right_dac_mux_controls),
  558. SND_SOC_DAPM_MUX("Right HPCOM Mux", SND_SOC_NOPM, 0, 0,
  559. &aic3x_right_hpcom_mux_controls),
  560. SND_SOC_DAPM_PGA("Right Line Out", RLOPM_CTRL, 0, 0, NULL, 0),
  561. SND_SOC_DAPM_PGA("Right HP Out", HPROUT_CTRL, 0, 0, NULL, 0),
  562. SND_SOC_DAPM_PGA("Right HP Com", HPRCOM_CTRL, 0, 0, NULL, 0),
  563. /* Inputs to Left ADC */
  564. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", LINE1L_2_LADC_CTRL, 2, 0),
  565. SND_SOC_DAPM_MUX("Left Line1L Mux", SND_SOC_NOPM, 0, 0,
  566. &aic3x_left_line1l_mux_controls),
  567. SND_SOC_DAPM_MUX("Left Line1R Mux", SND_SOC_NOPM, 0, 0,
  568. &aic3x_left_line1r_mux_controls),
  569. /* Inputs to Right ADC */
  570. SND_SOC_DAPM_ADC("Right ADC", "Right Capture",
  571. LINE1R_2_RADC_CTRL, 2, 0),
  572. SND_SOC_DAPM_MUX("Right Line1L Mux", SND_SOC_NOPM, 0, 0,
  573. &aic3x_right_line1l_mux_controls),
  574. SND_SOC_DAPM_MUX("Right Line1R Mux", SND_SOC_NOPM, 0, 0,
  575. &aic3x_right_line1r_mux_controls),
  576. /* Mic Bias */
  577. SND_SOC_DAPM_SUPPLY("Mic Bias", MICBIAS_CTRL, 6, 0,
  578. mic_bias_event,
  579. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  580. SND_SOC_DAPM_OUTPUT("LLOUT"),
  581. SND_SOC_DAPM_OUTPUT("RLOUT"),
  582. SND_SOC_DAPM_OUTPUT("HPLOUT"),
  583. SND_SOC_DAPM_OUTPUT("HPROUT"),
  584. SND_SOC_DAPM_OUTPUT("HPLCOM"),
  585. SND_SOC_DAPM_OUTPUT("HPRCOM"),
  586. SND_SOC_DAPM_INPUT("LINE1L"),
  587. SND_SOC_DAPM_INPUT("LINE1R"),
  588. /*
  589. * Virtual output pin to detection block inside codec. This can be
  590. * used to keep codec bias on if gpio or detection features are needed.
  591. * Force pin on or construct a path with an input jack and mic bias
  592. * widgets.
  593. */
  594. SND_SOC_DAPM_OUTPUT("Detection"),
  595. };
  596. /* For other than tlv320aic3104 */
  597. static const struct snd_soc_dapm_widget aic3x_extra_dapm_widgets[] = {
  598. /* Inputs to Left ADC */
  599. SND_SOC_DAPM_MIXER("Left PGA Mixer", SND_SOC_NOPM, 0, 0,
  600. &aic3x_left_pga_mixer_controls[0],
  601. ARRAY_SIZE(aic3x_left_pga_mixer_controls)),
  602. SND_SOC_DAPM_MUX("Left Line2L Mux", SND_SOC_NOPM, 0, 0,
  603. &aic3x_left_line2_mux_controls),
  604. /* Inputs to Right ADC */
  605. SND_SOC_DAPM_MIXER("Right PGA Mixer", SND_SOC_NOPM, 0, 0,
  606. &aic3x_right_pga_mixer_controls[0],
  607. ARRAY_SIZE(aic3x_right_pga_mixer_controls)),
  608. SND_SOC_DAPM_MUX("Right Line2R Mux", SND_SOC_NOPM, 0, 0,
  609. &aic3x_right_line2_mux_controls),
  610. /*
  611. * Not a real mic bias widget but similar function. This is for dynamic
  612. * control of GPIO1 digital mic modulator clock output function when
  613. * using digital mic.
  614. */
  615. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "GPIO1 dmic modclk",
  616. AIC3X_GPIO1_REG, 4, 0xf,
  617. AIC3X_GPIO1_FUNC_DIGITAL_MIC_MODCLK,
  618. AIC3X_GPIO1_FUNC_DISABLED),
  619. /*
  620. * Also similar function like mic bias. Selects digital mic with
  621. * configurable oversampling rate instead of ADC converter.
  622. */
  623. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 128",
  624. AIC3X_ASD_INTF_CTRLA, 0, 3, 1, 0),
  625. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 64",
  626. AIC3X_ASD_INTF_CTRLA, 0, 3, 2, 0),
  627. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 32",
  628. AIC3X_ASD_INTF_CTRLA, 0, 3, 3, 0),
  629. /* Output mixers */
  630. SND_SOC_DAPM_MIXER("Left Line Mixer", SND_SOC_NOPM, 0, 0,
  631. &aic3x_left_line_mixer_controls[0],
  632. ARRAY_SIZE(aic3x_left_line_mixer_controls)),
  633. SND_SOC_DAPM_MIXER("Right Line Mixer", SND_SOC_NOPM, 0, 0,
  634. &aic3x_right_line_mixer_controls[0],
  635. ARRAY_SIZE(aic3x_right_line_mixer_controls)),
  636. SND_SOC_DAPM_MIXER("Left HP Mixer", SND_SOC_NOPM, 0, 0,
  637. &aic3x_left_hp_mixer_controls[0],
  638. ARRAY_SIZE(aic3x_left_hp_mixer_controls)),
  639. SND_SOC_DAPM_MIXER("Right HP Mixer", SND_SOC_NOPM, 0, 0,
  640. &aic3x_right_hp_mixer_controls[0],
  641. ARRAY_SIZE(aic3x_right_hp_mixer_controls)),
  642. SND_SOC_DAPM_MIXER("Left HPCOM Mixer", SND_SOC_NOPM, 0, 0,
  643. &aic3x_left_hpcom_mixer_controls[0],
  644. ARRAY_SIZE(aic3x_left_hpcom_mixer_controls)),
  645. SND_SOC_DAPM_MIXER("Right HPCOM Mixer", SND_SOC_NOPM, 0, 0,
  646. &aic3x_right_hpcom_mixer_controls[0],
  647. ARRAY_SIZE(aic3x_right_hpcom_mixer_controls)),
  648. SND_SOC_DAPM_INPUT("MIC3L"),
  649. SND_SOC_DAPM_INPUT("MIC3R"),
  650. SND_SOC_DAPM_INPUT("LINE2L"),
  651. SND_SOC_DAPM_INPUT("LINE2R"),
  652. };
  653. /* For tlv320aic3104 */
  654. static const struct snd_soc_dapm_widget aic3104_extra_dapm_widgets[] = {
  655. /* Inputs to Left ADC */
  656. SND_SOC_DAPM_MIXER("Left PGA Mixer", SND_SOC_NOPM, 0, 0,
  657. &aic3104_left_pga_mixer_controls[0],
  658. ARRAY_SIZE(aic3104_left_pga_mixer_controls)),
  659. /* Inputs to Right ADC */
  660. SND_SOC_DAPM_MIXER("Right PGA Mixer", SND_SOC_NOPM, 0, 0,
  661. &aic3104_right_pga_mixer_controls[0],
  662. ARRAY_SIZE(aic3104_right_pga_mixer_controls)),
  663. /* Output mixers */
  664. SND_SOC_DAPM_MIXER("Left Line Mixer", SND_SOC_NOPM, 0, 0,
  665. &aic3x_left_line_mixer_controls[0],
  666. ARRAY_SIZE(aic3x_left_line_mixer_controls) - 2),
  667. SND_SOC_DAPM_MIXER("Right Line Mixer", SND_SOC_NOPM, 0, 0,
  668. &aic3x_right_line_mixer_controls[0],
  669. ARRAY_SIZE(aic3x_right_line_mixer_controls) - 2),
  670. SND_SOC_DAPM_MIXER("Left HP Mixer", SND_SOC_NOPM, 0, 0,
  671. &aic3x_left_hp_mixer_controls[0],
  672. ARRAY_SIZE(aic3x_left_hp_mixer_controls) - 2),
  673. SND_SOC_DAPM_MIXER("Right HP Mixer", SND_SOC_NOPM, 0, 0,
  674. &aic3x_right_hp_mixer_controls[0],
  675. ARRAY_SIZE(aic3x_right_hp_mixer_controls) - 2),
  676. SND_SOC_DAPM_MIXER("Left HPCOM Mixer", SND_SOC_NOPM, 0, 0,
  677. &aic3x_left_hpcom_mixer_controls[0],
  678. ARRAY_SIZE(aic3x_left_hpcom_mixer_controls) - 2),
  679. SND_SOC_DAPM_MIXER("Right HPCOM Mixer", SND_SOC_NOPM, 0, 0,
  680. &aic3x_right_hpcom_mixer_controls[0],
  681. ARRAY_SIZE(aic3x_right_hpcom_mixer_controls) - 2),
  682. SND_SOC_DAPM_INPUT("MIC2L"),
  683. SND_SOC_DAPM_INPUT("MIC2R"),
  684. };
  685. static const struct snd_soc_dapm_widget aic3x_dapm_mono_widgets[] = {
  686. /* Mono Output */
  687. SND_SOC_DAPM_PGA("Mono Out", MONOLOPM_CTRL, 0, 0, NULL, 0),
  688. SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0,
  689. &aic3x_mono_mixer_controls[0],
  690. ARRAY_SIZE(aic3x_mono_mixer_controls)),
  691. SND_SOC_DAPM_OUTPUT("MONO_LOUT"),
  692. };
  693. static const struct snd_soc_dapm_widget aic3007_dapm_widgets[] = {
  694. /* Class-D outputs */
  695. SND_SOC_DAPM_PGA("Left Class-D Out", CLASSD_CTRL, 3, 0, NULL, 0),
  696. SND_SOC_DAPM_PGA("Right Class-D Out", CLASSD_CTRL, 2, 0, NULL, 0),
  697. SND_SOC_DAPM_OUTPUT("SPOP"),
  698. SND_SOC_DAPM_OUTPUT("SPOM"),
  699. };
  700. static const struct snd_soc_dapm_route intercon[] = {
  701. /* Left Input */
  702. {"Left Line1L Mux", "single-ended", "LINE1L"},
  703. {"Left Line1L Mux", "differential", "LINE1L"},
  704. {"Left Line1R Mux", "single-ended", "LINE1R"},
  705. {"Left Line1R Mux", "differential", "LINE1R"},
  706. {"Left PGA Mixer", "Line1L Switch", "Left Line1L Mux"},
  707. {"Left PGA Mixer", "Line1R Switch", "Left Line1R Mux"},
  708. {"Left ADC", NULL, "Left PGA Mixer"},
  709. /* Right Input */
  710. {"Right Line1R Mux", "single-ended", "LINE1R"},
  711. {"Right Line1R Mux", "differential", "LINE1R"},
  712. {"Right Line1L Mux", "single-ended", "LINE1L"},
  713. {"Right Line1L Mux", "differential", "LINE1L"},
  714. {"Right PGA Mixer", "Line1L Switch", "Right Line1L Mux"},
  715. {"Right PGA Mixer", "Line1R Switch", "Right Line1R Mux"},
  716. {"Right ADC", NULL, "Right PGA Mixer"},
  717. /* Left DAC Output */
  718. {"Left DAC Mux", "DAC_L1", "Left DAC"},
  719. {"Left DAC Mux", "DAC_L2", "Left DAC"},
  720. {"Left DAC Mux", "DAC_L3", "Left DAC"},
  721. /* Right DAC Output */
  722. {"Right DAC Mux", "DAC_R1", "Right DAC"},
  723. {"Right DAC Mux", "DAC_R2", "Right DAC"},
  724. {"Right DAC Mux", "DAC_R3", "Right DAC"},
  725. /* Left Line Output */
  726. {"Left Line Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  727. {"Left Line Mixer", "DACL1 Switch", "Left DAC Mux"},
  728. {"Left Line Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  729. {"Left Line Mixer", "DACR1 Switch", "Right DAC Mux"},
  730. {"Left Line Out", NULL, "Left Line Mixer"},
  731. {"Left Line Out", NULL, "Left DAC Mux"},
  732. {"LLOUT", NULL, "Left Line Out"},
  733. /* Right Line Output */
  734. {"Right Line Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  735. {"Right Line Mixer", "DACL1 Switch", "Left DAC Mux"},
  736. {"Right Line Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  737. {"Right Line Mixer", "DACR1 Switch", "Right DAC Mux"},
  738. {"Right Line Out", NULL, "Right Line Mixer"},
  739. {"Right Line Out", NULL, "Right DAC Mux"},
  740. {"RLOUT", NULL, "Right Line Out"},
  741. /* Left HP Output */
  742. {"Left HP Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  743. {"Left HP Mixer", "DACL1 Switch", "Left DAC Mux"},
  744. {"Left HP Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  745. {"Left HP Mixer", "DACR1 Switch", "Right DAC Mux"},
  746. {"Left HP Out", NULL, "Left HP Mixer"},
  747. {"Left HP Out", NULL, "Left DAC Mux"},
  748. {"HPLOUT", NULL, "Left HP Out"},
  749. /* Right HP Output */
  750. {"Right HP Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  751. {"Right HP Mixer", "DACL1 Switch", "Left DAC Mux"},
  752. {"Right HP Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  753. {"Right HP Mixer", "DACR1 Switch", "Right DAC Mux"},
  754. {"Right HP Out", NULL, "Right HP Mixer"},
  755. {"Right HP Out", NULL, "Right DAC Mux"},
  756. {"HPROUT", NULL, "Right HP Out"},
  757. /* Left HPCOM Output */
  758. {"Left HPCOM Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  759. {"Left HPCOM Mixer", "DACL1 Switch", "Left DAC Mux"},
  760. {"Left HPCOM Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  761. {"Left HPCOM Mixer", "DACR1 Switch", "Right DAC Mux"},
  762. {"Left HPCOM Mux", "differential of HPLOUT", "Left HP Mixer"},
  763. {"Left HPCOM Mux", "constant VCM", "Left HPCOM Mixer"},
  764. {"Left HPCOM Mux", "single-ended", "Left HPCOM Mixer"},
  765. {"Left HP Com", NULL, "Left HPCOM Mux"},
  766. {"HPLCOM", NULL, "Left HP Com"},
  767. /* Right HPCOM Output */
  768. {"Right HPCOM Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  769. {"Right HPCOM Mixer", "DACL1 Switch", "Left DAC Mux"},
  770. {"Right HPCOM Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  771. {"Right HPCOM Mixer", "DACR1 Switch", "Right DAC Mux"},
  772. {"Right HPCOM Mux", "differential of HPROUT", "Right HP Mixer"},
  773. {"Right HPCOM Mux", "constant VCM", "Right HPCOM Mixer"},
  774. {"Right HPCOM Mux", "single-ended", "Right HPCOM Mixer"},
  775. {"Right HPCOM Mux", "differential of HPLCOM", "Left HPCOM Mixer"},
  776. {"Right HPCOM Mux", "external feedback", "Right HPCOM Mixer"},
  777. {"Right HP Com", NULL, "Right HPCOM Mux"},
  778. {"HPRCOM", NULL, "Right HP Com"},
  779. };
  780. /* For other than tlv320aic3104 */
  781. static const struct snd_soc_dapm_route intercon_extra[] = {
  782. /* Left Input */
  783. {"Left Line2L Mux", "single-ended", "LINE2L"},
  784. {"Left Line2L Mux", "differential", "LINE2L"},
  785. {"Left PGA Mixer", "Line2L Switch", "Left Line2L Mux"},
  786. {"Left PGA Mixer", "Mic3L Switch", "MIC3L"},
  787. {"Left PGA Mixer", "Mic3R Switch", "MIC3R"},
  788. {"Left ADC", NULL, "GPIO1 dmic modclk"},
  789. /* Right Input */
  790. {"Right Line2R Mux", "single-ended", "LINE2R"},
  791. {"Right Line2R Mux", "differential", "LINE2R"},
  792. {"Right PGA Mixer", "Line2R Switch", "Right Line2R Mux"},
  793. {"Right PGA Mixer", "Mic3L Switch", "MIC3L"},
  794. {"Right PGA Mixer", "Mic3R Switch", "MIC3R"},
  795. {"Right ADC", NULL, "GPIO1 dmic modclk"},
  796. /*
  797. * Logical path between digital mic enable and GPIO1 modulator clock
  798. * output function
  799. */
  800. {"GPIO1 dmic modclk", NULL, "DMic Rate 128"},
  801. {"GPIO1 dmic modclk", NULL, "DMic Rate 64"},
  802. {"GPIO1 dmic modclk", NULL, "DMic Rate 32"},
  803. /* Left Line Output */
  804. {"Left Line Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  805. {"Left Line Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  806. /* Right Line Output */
  807. {"Right Line Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  808. {"Right Line Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  809. /* Left HP Output */
  810. {"Left HP Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  811. {"Left HP Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  812. /* Right HP Output */
  813. {"Right HP Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  814. {"Right HP Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  815. /* Left HPCOM Output */
  816. {"Left HPCOM Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  817. {"Left HPCOM Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  818. /* Right HPCOM Output */
  819. {"Right HPCOM Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  820. {"Right HPCOM Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  821. };
  822. /* For tlv320aic3104 */
  823. static const struct snd_soc_dapm_route intercon_extra_3104[] = {
  824. /* Left Input */
  825. {"Left PGA Mixer", "Mic2L Switch", "MIC2L"},
  826. {"Left PGA Mixer", "Mic2R Switch", "MIC2R"},
  827. /* Right Input */
  828. {"Right PGA Mixer", "Mic2L Switch", "MIC2L"},
  829. {"Right PGA Mixer", "Mic2R Switch", "MIC2R"},
  830. };
  831. static const struct snd_soc_dapm_route intercon_mono[] = {
  832. /* Mono Output */
  833. {"Mono Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  834. {"Mono Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  835. {"Mono Mixer", "DACL1 Switch", "Left DAC Mux"},
  836. {"Mono Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  837. {"Mono Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  838. {"Mono Mixer", "DACR1 Switch", "Right DAC Mux"},
  839. {"Mono Out", NULL, "Mono Mixer"},
  840. {"MONO_LOUT", NULL, "Mono Out"},
  841. };
  842. static const struct snd_soc_dapm_route intercon_3007[] = {
  843. /* Class-D outputs */
  844. {"Left Class-D Out", NULL, "Left Line Out"},
  845. {"Right Class-D Out", NULL, "Left Line Out"},
  846. {"SPOP", NULL, "Left Class-D Out"},
  847. {"SPOM", NULL, "Right Class-D Out"},
  848. };
  849. static int aic3x_add_widgets(struct snd_soc_component *component)
  850. {
  851. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  852. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  853. switch (aic3x->model) {
  854. case AIC3X_MODEL_3X:
  855. case AIC3X_MODEL_33:
  856. case AIC3X_MODEL_3106:
  857. snd_soc_dapm_new_controls(dapm, aic3x_extra_dapm_widgets,
  858. ARRAY_SIZE(aic3x_extra_dapm_widgets));
  859. snd_soc_dapm_add_routes(dapm, intercon_extra,
  860. ARRAY_SIZE(intercon_extra));
  861. snd_soc_dapm_new_controls(dapm, aic3x_dapm_mono_widgets,
  862. ARRAY_SIZE(aic3x_dapm_mono_widgets));
  863. snd_soc_dapm_add_routes(dapm, intercon_mono,
  864. ARRAY_SIZE(intercon_mono));
  865. break;
  866. case AIC3X_MODEL_3007:
  867. snd_soc_dapm_new_controls(dapm, aic3x_extra_dapm_widgets,
  868. ARRAY_SIZE(aic3x_extra_dapm_widgets));
  869. snd_soc_dapm_add_routes(dapm, intercon_extra,
  870. ARRAY_SIZE(intercon_extra));
  871. snd_soc_dapm_new_controls(dapm, aic3007_dapm_widgets,
  872. ARRAY_SIZE(aic3007_dapm_widgets));
  873. snd_soc_dapm_add_routes(dapm, intercon_3007,
  874. ARRAY_SIZE(intercon_3007));
  875. break;
  876. case AIC3X_MODEL_3104:
  877. snd_soc_dapm_new_controls(dapm, aic3104_extra_dapm_widgets,
  878. ARRAY_SIZE(aic3104_extra_dapm_widgets));
  879. snd_soc_dapm_add_routes(dapm, intercon_extra_3104,
  880. ARRAY_SIZE(intercon_extra_3104));
  881. break;
  882. }
  883. return 0;
  884. }
  885. static int aic3x_hw_params(struct snd_pcm_substream *substream,
  886. struct snd_pcm_hw_params *params,
  887. struct snd_soc_dai *dai)
  888. {
  889. struct snd_soc_component *component = dai->component;
  890. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  891. int codec_clk = 0, bypass_pll = 0, fsref, last_clk = 0;
  892. u8 data, j, r, p, pll_q, pll_p = 1, pll_r = 1, pll_j = 1;
  893. u16 d, pll_d = 1;
  894. int clk;
  895. int width = aic3x->slot_width;
  896. if (!width)
  897. width = params_width(params);
  898. /* select data word length */
  899. data = snd_soc_component_read(component, AIC3X_ASD_INTF_CTRLB) & (~(0x3 << 4));
  900. switch (width) {
  901. case 16:
  902. break;
  903. case 20:
  904. data |= (0x01 << 4);
  905. break;
  906. case 24:
  907. data |= (0x02 << 4);
  908. break;
  909. case 32:
  910. data |= (0x03 << 4);
  911. break;
  912. }
  913. snd_soc_component_write(component, AIC3X_ASD_INTF_CTRLB, data);
  914. /* Fsref can be 44100 or 48000 */
  915. fsref = (params_rate(params) % 11025 == 0) ? 44100 : 48000;
  916. /* Try to find a value for Q which allows us to bypass the PLL and
  917. * generate CODEC_CLK directly. */
  918. for (pll_q = 2; pll_q < 18; pll_q++)
  919. if (aic3x->sysclk / (128 * pll_q) == fsref) {
  920. bypass_pll = 1;
  921. break;
  922. }
  923. if (bypass_pll) {
  924. pll_q &= 0xf;
  925. snd_soc_component_write(component, AIC3X_PLL_PROGA_REG, pll_q << PLLQ_SHIFT);
  926. snd_soc_component_write(component, AIC3X_GPIOB_REG, CODEC_CLKIN_CLKDIV);
  927. /* disable PLL if it is bypassed */
  928. snd_soc_component_update_bits(component, AIC3X_PLL_PROGA_REG, PLL_ENABLE, 0);
  929. } else {
  930. snd_soc_component_write(component, AIC3X_GPIOB_REG, CODEC_CLKIN_PLLDIV);
  931. /* enable PLL when it is used */
  932. snd_soc_component_update_bits(component, AIC3X_PLL_PROGA_REG,
  933. PLL_ENABLE, PLL_ENABLE);
  934. }
  935. /* Route Left DAC to left channel input and
  936. * right DAC to right channel input */
  937. data = (LDAC2LCH | RDAC2RCH);
  938. data |= (fsref == 44100) ? FSREF_44100 : FSREF_48000;
  939. if (params_rate(params) >= 64000)
  940. data |= DUAL_RATE_MODE;
  941. snd_soc_component_write(component, AIC3X_CODEC_DATAPATH_REG, data);
  942. /* codec sample rate select */
  943. data = (fsref * 20) / params_rate(params);
  944. if (params_rate(params) < 64000)
  945. data /= 2;
  946. data /= 5;
  947. data -= 2;
  948. data |= (data << 4);
  949. snd_soc_component_write(component, AIC3X_SAMPLE_RATE_SEL_REG, data);
  950. if (bypass_pll)
  951. return 0;
  952. /* Use PLL, compute appropriate setup for j, d, r and p, the closest
  953. * one wins the game. Try with d==0 first, next with d!=0.
  954. * Constraints for j are according to the datasheet.
  955. * The sysclk is divided by 1000 to prevent integer overflows.
  956. */
  957. codec_clk = (2048 * fsref) / (aic3x->sysclk / 1000);
  958. for (r = 1; r <= 16; r++)
  959. for (p = 1; p <= 8; p++) {
  960. for (j = 4; j <= 55; j++) {
  961. /* This is actually 1000*((j+(d/10000))*r)/p
  962. * The term had to be converted to get
  963. * rid of the division by 10000; d = 0 here
  964. */
  965. int tmp_clk = (1000 * j * r) / p;
  966. /* Check whether this values get closer than
  967. * the best ones we had before
  968. */
  969. if (abs(codec_clk - tmp_clk) <
  970. abs(codec_clk - last_clk)) {
  971. pll_j = j; pll_d = 0;
  972. pll_r = r; pll_p = p;
  973. last_clk = tmp_clk;
  974. }
  975. /* Early exit for exact matches */
  976. if (tmp_clk == codec_clk)
  977. goto found;
  978. }
  979. }
  980. /* try with d != 0 */
  981. for (p = 1; p <= 8; p++) {
  982. j = codec_clk * p / 1000;
  983. if (j < 4 || j > 11)
  984. continue;
  985. /* do not use codec_clk here since we'd loose precision */
  986. d = ((2048 * p * fsref) - j * aic3x->sysclk)
  987. * 100 / (aic3x->sysclk/100);
  988. clk = (10000 * j + d) / (10 * p);
  989. /* check whether this values get closer than the best
  990. * ones we had before */
  991. if (abs(codec_clk - clk) < abs(codec_clk - last_clk)) {
  992. pll_j = j; pll_d = d; pll_r = 1; pll_p = p;
  993. last_clk = clk;
  994. }
  995. /* Early exit for exact matches */
  996. if (clk == codec_clk)
  997. goto found;
  998. }
  999. if (last_clk == 0) {
  1000. printk(KERN_ERR "%s(): unable to setup PLL\n", __func__);
  1001. return -EINVAL;
  1002. }
  1003. found:
  1004. snd_soc_component_update_bits(component, AIC3X_PLL_PROGA_REG, PLLP_MASK, pll_p);
  1005. snd_soc_component_write(component, AIC3X_OVRF_STATUS_AND_PLLR_REG,
  1006. pll_r << PLLR_SHIFT);
  1007. snd_soc_component_write(component, AIC3X_PLL_PROGB_REG, pll_j << PLLJ_SHIFT);
  1008. snd_soc_component_write(component, AIC3X_PLL_PROGC_REG,
  1009. (pll_d >> 6) << PLLD_MSB_SHIFT);
  1010. snd_soc_component_write(component, AIC3X_PLL_PROGD_REG,
  1011. (pll_d & 0x3F) << PLLD_LSB_SHIFT);
  1012. return 0;
  1013. }
  1014. static int aic3x_prepare(struct snd_pcm_substream *substream,
  1015. struct snd_soc_dai *dai)
  1016. {
  1017. struct snd_soc_component *component = dai->component;
  1018. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1019. int delay = 0;
  1020. int width = aic3x->slot_width;
  1021. if (!width)
  1022. width = substream->runtime->sample_bits;
  1023. /* TDM slot selection only valid in DSP_A/_B mode */
  1024. if (aic3x->dai_fmt == SND_SOC_DAIFMT_DSP_A)
  1025. delay += (aic3x->tdm_delay*width + 1);
  1026. else if (aic3x->dai_fmt == SND_SOC_DAIFMT_DSP_B)
  1027. delay += aic3x->tdm_delay*width;
  1028. /* Configure data delay */
  1029. snd_soc_component_write(component, AIC3X_ASD_INTF_CTRLC, delay);
  1030. return 0;
  1031. }
  1032. static int aic3x_mute(struct snd_soc_dai *dai, int mute, int direction)
  1033. {
  1034. struct snd_soc_component *component = dai->component;
  1035. u8 ldac_reg = snd_soc_component_read(component, LDAC_VOL) & ~MUTE_ON;
  1036. u8 rdac_reg = snd_soc_component_read(component, RDAC_VOL) & ~MUTE_ON;
  1037. if (mute) {
  1038. snd_soc_component_write(component, LDAC_VOL, ldac_reg | MUTE_ON);
  1039. snd_soc_component_write(component, RDAC_VOL, rdac_reg | MUTE_ON);
  1040. } else {
  1041. snd_soc_component_write(component, LDAC_VOL, ldac_reg);
  1042. snd_soc_component_write(component, RDAC_VOL, rdac_reg);
  1043. }
  1044. return 0;
  1045. }
  1046. static int aic3x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1047. int clk_id, unsigned int freq, int dir)
  1048. {
  1049. struct snd_soc_component *component = codec_dai->component;
  1050. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1051. /* set clock on MCLK or GPIO2 or BCLK */
  1052. snd_soc_component_update_bits(component, AIC3X_CLKGEN_CTRL_REG, PLLCLK_IN_MASK,
  1053. clk_id << PLLCLK_IN_SHIFT);
  1054. snd_soc_component_update_bits(component, AIC3X_CLKGEN_CTRL_REG, CLKDIV_IN_MASK,
  1055. clk_id << CLKDIV_IN_SHIFT);
  1056. aic3x->sysclk = freq;
  1057. return 0;
  1058. }
  1059. static int aic3x_set_dai_fmt(struct snd_soc_dai *codec_dai,
  1060. unsigned int fmt)
  1061. {
  1062. struct snd_soc_component *component = codec_dai->component;
  1063. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1064. u8 iface_areg, iface_breg;
  1065. iface_areg = snd_soc_component_read(component, AIC3X_ASD_INTF_CTRLA) & 0x3f;
  1066. iface_breg = snd_soc_component_read(component, AIC3X_ASD_INTF_CTRLB) & 0x3f;
  1067. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  1068. case SND_SOC_DAIFMT_CBP_CFP:
  1069. aic3x->master = 1;
  1070. iface_areg |= BIT_CLK_MASTER | WORD_CLK_MASTER;
  1071. break;
  1072. case SND_SOC_DAIFMT_CBC_CFC:
  1073. aic3x->master = 0;
  1074. iface_areg &= ~(BIT_CLK_MASTER | WORD_CLK_MASTER);
  1075. break;
  1076. case SND_SOC_DAIFMT_CBP_CFC:
  1077. aic3x->master = 1;
  1078. iface_areg |= BIT_CLK_MASTER;
  1079. iface_areg &= ~WORD_CLK_MASTER;
  1080. break;
  1081. case SND_SOC_DAIFMT_CBC_CFP:
  1082. aic3x->master = 1;
  1083. iface_areg |= WORD_CLK_MASTER;
  1084. iface_areg &= ~BIT_CLK_MASTER;
  1085. break;
  1086. default:
  1087. return -EINVAL;
  1088. }
  1089. /*
  1090. * match both interface format and signal polarities since they
  1091. * are fixed
  1092. */
  1093. switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
  1094. SND_SOC_DAIFMT_INV_MASK)) {
  1095. case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF):
  1096. break;
  1097. case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF):
  1098. case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF):
  1099. iface_breg |= (0x01 << 6);
  1100. break;
  1101. case (SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF):
  1102. iface_breg |= (0x02 << 6);
  1103. break;
  1104. case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF):
  1105. iface_breg |= (0x03 << 6);
  1106. break;
  1107. default:
  1108. return -EINVAL;
  1109. }
  1110. aic3x->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  1111. /* set iface */
  1112. snd_soc_component_write(component, AIC3X_ASD_INTF_CTRLA, iface_areg);
  1113. snd_soc_component_write(component, AIC3X_ASD_INTF_CTRLB, iface_breg);
  1114. return 0;
  1115. }
  1116. static int aic3x_set_dai_tdm_slot(struct snd_soc_dai *codec_dai,
  1117. unsigned int tx_mask, unsigned int rx_mask,
  1118. int slots, int slot_width)
  1119. {
  1120. struct snd_soc_component *component = codec_dai->component;
  1121. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1122. unsigned int lsb;
  1123. if (tx_mask != rx_mask) {
  1124. dev_err(component->dev, "tx and rx masks must be symmetric\n");
  1125. return -EINVAL;
  1126. }
  1127. if (unlikely(!tx_mask)) {
  1128. dev_err(component->dev, "tx and rx masks need to be non 0\n");
  1129. return -EINVAL;
  1130. }
  1131. /* TDM based on DSP mode requires slots to be adjacent */
  1132. lsb = __ffs(tx_mask);
  1133. if ((lsb + 1) != __fls(tx_mask)) {
  1134. dev_err(component->dev, "Invalid mask, slots must be adjacent\n");
  1135. return -EINVAL;
  1136. }
  1137. switch (slot_width) {
  1138. case 16:
  1139. case 20:
  1140. case 24:
  1141. case 32:
  1142. break;
  1143. default:
  1144. dev_err(component->dev, "Unsupported slot width %d\n", slot_width);
  1145. return -EINVAL;
  1146. }
  1147. aic3x->tdm_delay = lsb;
  1148. aic3x->slot_width = slot_width;
  1149. /* DOUT in high-impedance on inactive bit clocks */
  1150. snd_soc_component_update_bits(component, AIC3X_ASD_INTF_CTRLA,
  1151. DOUT_TRISTATE, DOUT_TRISTATE);
  1152. return 0;
  1153. }
  1154. static int aic3x_regulator_event(struct notifier_block *nb,
  1155. unsigned long event, void *data)
  1156. {
  1157. struct aic3x_disable_nb *disable_nb =
  1158. container_of(nb, struct aic3x_disable_nb, nb);
  1159. struct aic3x_priv *aic3x = disable_nb->aic3x;
  1160. if (event & REGULATOR_EVENT_DISABLE) {
  1161. /*
  1162. * Put codec to reset and require cache sync as at least one
  1163. * of the supplies was disabled
  1164. */
  1165. if (gpio_is_valid(aic3x->gpio_reset))
  1166. gpio_set_value(aic3x->gpio_reset, 0);
  1167. regcache_mark_dirty(aic3x->regmap);
  1168. }
  1169. return 0;
  1170. }
  1171. static int aic3x_set_power(struct snd_soc_component *component, int power)
  1172. {
  1173. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1174. unsigned int pll_c, pll_d;
  1175. int ret;
  1176. if (power) {
  1177. ret = regulator_bulk_enable(ARRAY_SIZE(aic3x->supplies),
  1178. aic3x->supplies);
  1179. if (ret)
  1180. goto out;
  1181. aic3x->power = 1;
  1182. if (gpio_is_valid(aic3x->gpio_reset)) {
  1183. udelay(1);
  1184. gpio_set_value(aic3x->gpio_reset, 1);
  1185. }
  1186. /* Sync reg_cache with the hardware */
  1187. regcache_cache_only(aic3x->regmap, false);
  1188. regcache_sync(aic3x->regmap);
  1189. /* Rewrite paired PLL D registers in case cached sync skipped
  1190. * writing one of them and thus caused other one also not
  1191. * being written
  1192. */
  1193. pll_c = snd_soc_component_read(component, AIC3X_PLL_PROGC_REG);
  1194. pll_d = snd_soc_component_read(component, AIC3X_PLL_PROGD_REG);
  1195. if (pll_c == aic3x_reg[AIC3X_PLL_PROGC_REG].def ||
  1196. pll_d == aic3x_reg[AIC3X_PLL_PROGD_REG].def) {
  1197. snd_soc_component_write(component, AIC3X_PLL_PROGC_REG, pll_c);
  1198. snd_soc_component_write(component, AIC3X_PLL_PROGD_REG, pll_d);
  1199. }
  1200. /*
  1201. * Delay is needed to reduce pop-noise after syncing back the
  1202. * registers
  1203. */
  1204. mdelay(50);
  1205. } else {
  1206. /*
  1207. * Do soft reset to this codec instance in order to clear
  1208. * possible VDD leakage currents in case the supply regulators
  1209. * remain on
  1210. */
  1211. snd_soc_component_write(component, AIC3X_RESET, SOFT_RESET);
  1212. regcache_mark_dirty(aic3x->regmap);
  1213. aic3x->power = 0;
  1214. /* HW writes are needless when bias is off */
  1215. regcache_cache_only(aic3x->regmap, true);
  1216. ret = regulator_bulk_disable(ARRAY_SIZE(aic3x->supplies),
  1217. aic3x->supplies);
  1218. }
  1219. out:
  1220. return ret;
  1221. }
  1222. static int aic3x_set_bias_level(struct snd_soc_component *component,
  1223. enum snd_soc_bias_level level)
  1224. {
  1225. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1226. switch (level) {
  1227. case SND_SOC_BIAS_ON:
  1228. break;
  1229. case SND_SOC_BIAS_PREPARE:
  1230. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY &&
  1231. aic3x->master) {
  1232. /* enable pll */
  1233. snd_soc_component_update_bits(component, AIC3X_PLL_PROGA_REG,
  1234. PLL_ENABLE, PLL_ENABLE);
  1235. }
  1236. break;
  1237. case SND_SOC_BIAS_STANDBY:
  1238. if (!aic3x->power)
  1239. aic3x_set_power(component, 1);
  1240. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE &&
  1241. aic3x->master) {
  1242. /* disable pll */
  1243. snd_soc_component_update_bits(component, AIC3X_PLL_PROGA_REG,
  1244. PLL_ENABLE, 0);
  1245. }
  1246. break;
  1247. case SND_SOC_BIAS_OFF:
  1248. if (aic3x->power)
  1249. aic3x_set_power(component, 0);
  1250. break;
  1251. }
  1252. return 0;
  1253. }
  1254. #define AIC3X_RATES SNDRV_PCM_RATE_8000_96000
  1255. #define AIC3X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  1256. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \
  1257. SNDRV_PCM_FMTBIT_S32_LE)
  1258. static const struct snd_soc_dai_ops aic3x_dai_ops = {
  1259. .hw_params = aic3x_hw_params,
  1260. .prepare = aic3x_prepare,
  1261. .mute_stream = aic3x_mute,
  1262. .set_sysclk = aic3x_set_dai_sysclk,
  1263. .set_fmt = aic3x_set_dai_fmt,
  1264. .set_tdm_slot = aic3x_set_dai_tdm_slot,
  1265. .no_capture_mute = 1,
  1266. };
  1267. static struct snd_soc_dai_driver aic3x_dai = {
  1268. .name = "tlv320aic3x-hifi",
  1269. .playback = {
  1270. .stream_name = "Playback",
  1271. .channels_min = 2,
  1272. .channels_max = 2,
  1273. .rates = AIC3X_RATES,
  1274. .formats = AIC3X_FORMATS,},
  1275. .capture = {
  1276. .stream_name = "Capture",
  1277. .channels_min = 2,
  1278. .channels_max = 2,
  1279. .rates = AIC3X_RATES,
  1280. .formats = AIC3X_FORMATS,},
  1281. .ops = &aic3x_dai_ops,
  1282. .symmetric_rate = 1,
  1283. };
  1284. static void aic3x_mono_init(struct snd_soc_component *component)
  1285. {
  1286. /* DAC to Mono Line Out default volume and route to Output mixer */
  1287. snd_soc_component_write(component, DACL1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1288. snd_soc_component_write(component, DACR1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1289. /* unmute all outputs */
  1290. snd_soc_component_update_bits(component, MONOLOPM_CTRL, UNMUTE, UNMUTE);
  1291. /* PGA to Mono Line Out default volume, disconnect from Output Mixer */
  1292. snd_soc_component_write(component, PGAL_2_MONOLOPM_VOL, DEFAULT_VOL);
  1293. snd_soc_component_write(component, PGAR_2_MONOLOPM_VOL, DEFAULT_VOL);
  1294. /* Line2 to Mono Out default volume, disconnect from Output Mixer */
  1295. snd_soc_component_write(component, LINE2L_2_MONOLOPM_VOL, DEFAULT_VOL);
  1296. snd_soc_component_write(component, LINE2R_2_MONOLOPM_VOL, DEFAULT_VOL);
  1297. }
  1298. /*
  1299. * initialise the AIC3X driver
  1300. * register the mixer and dsp interfaces with the kernel
  1301. */
  1302. static int aic3x_init(struct snd_soc_component *component)
  1303. {
  1304. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1305. snd_soc_component_write(component, AIC3X_PAGE_SELECT, PAGE0_SELECT);
  1306. snd_soc_component_write(component, AIC3X_RESET, SOFT_RESET);
  1307. /* DAC default volume and mute */
  1308. snd_soc_component_write(component, LDAC_VOL, DEFAULT_VOL | MUTE_ON);
  1309. snd_soc_component_write(component, RDAC_VOL, DEFAULT_VOL | MUTE_ON);
  1310. /* DAC to HP default volume and route to Output mixer */
  1311. snd_soc_component_write(component, DACL1_2_HPLOUT_VOL, DEFAULT_VOL | ROUTE_ON);
  1312. snd_soc_component_write(component, DACR1_2_HPROUT_VOL, DEFAULT_VOL | ROUTE_ON);
  1313. snd_soc_component_write(component, DACL1_2_HPLCOM_VOL, DEFAULT_VOL | ROUTE_ON);
  1314. snd_soc_component_write(component, DACR1_2_HPRCOM_VOL, DEFAULT_VOL | ROUTE_ON);
  1315. /* DAC to Line Out default volume and route to Output mixer */
  1316. snd_soc_component_write(component, DACL1_2_LLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1317. snd_soc_component_write(component, DACR1_2_RLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1318. /* unmute all outputs */
  1319. snd_soc_component_update_bits(component, LLOPM_CTRL, UNMUTE, UNMUTE);
  1320. snd_soc_component_update_bits(component, RLOPM_CTRL, UNMUTE, UNMUTE);
  1321. snd_soc_component_update_bits(component, HPLOUT_CTRL, UNMUTE, UNMUTE);
  1322. snd_soc_component_update_bits(component, HPROUT_CTRL, UNMUTE, UNMUTE);
  1323. snd_soc_component_update_bits(component, HPLCOM_CTRL, UNMUTE, UNMUTE);
  1324. snd_soc_component_update_bits(component, HPRCOM_CTRL, UNMUTE, UNMUTE);
  1325. /* ADC default volume and unmute */
  1326. snd_soc_component_write(component, LADC_VOL, DEFAULT_GAIN);
  1327. snd_soc_component_write(component, RADC_VOL, DEFAULT_GAIN);
  1328. /* By default route Line1 to ADC PGA mixer */
  1329. snd_soc_component_write(component, LINE1L_2_LADC_CTRL, 0x0);
  1330. snd_soc_component_write(component, LINE1R_2_RADC_CTRL, 0x0);
  1331. /* PGA to HP Bypass default volume, disconnect from Output Mixer */
  1332. snd_soc_component_write(component, PGAL_2_HPLOUT_VOL, DEFAULT_VOL);
  1333. snd_soc_component_write(component, PGAR_2_HPROUT_VOL, DEFAULT_VOL);
  1334. snd_soc_component_write(component, PGAL_2_HPLCOM_VOL, DEFAULT_VOL);
  1335. snd_soc_component_write(component, PGAR_2_HPRCOM_VOL, DEFAULT_VOL);
  1336. /* PGA to Line Out default volume, disconnect from Output Mixer */
  1337. snd_soc_component_write(component, PGAL_2_LLOPM_VOL, DEFAULT_VOL);
  1338. snd_soc_component_write(component, PGAR_2_RLOPM_VOL, DEFAULT_VOL);
  1339. /* On tlv320aic3104, these registers are reserved and must not be written */
  1340. if (aic3x->model != AIC3X_MODEL_3104) {
  1341. /* Line2 to HP Bypass default volume, disconnect from Output Mixer */
  1342. snd_soc_component_write(component, LINE2L_2_HPLOUT_VOL, DEFAULT_VOL);
  1343. snd_soc_component_write(component, LINE2R_2_HPROUT_VOL, DEFAULT_VOL);
  1344. snd_soc_component_write(component, LINE2L_2_HPLCOM_VOL, DEFAULT_VOL);
  1345. snd_soc_component_write(component, LINE2R_2_HPRCOM_VOL, DEFAULT_VOL);
  1346. /* Line2 Line Out default volume, disconnect from Output Mixer */
  1347. snd_soc_component_write(component, LINE2L_2_LLOPM_VOL, DEFAULT_VOL);
  1348. snd_soc_component_write(component, LINE2R_2_RLOPM_VOL, DEFAULT_VOL);
  1349. }
  1350. switch (aic3x->model) {
  1351. case AIC3X_MODEL_3X:
  1352. case AIC3X_MODEL_33:
  1353. case AIC3X_MODEL_3106:
  1354. aic3x_mono_init(component);
  1355. break;
  1356. case AIC3X_MODEL_3007:
  1357. snd_soc_component_write(component, CLASSD_CTRL, 0);
  1358. break;
  1359. }
  1360. /* Output common-mode voltage = 1.5 V */
  1361. snd_soc_component_update_bits(component, HPOUT_SC, HPOUT_SC_OCMV_MASK,
  1362. aic3x->ocmv << HPOUT_SC_OCMV_SHIFT);
  1363. return 0;
  1364. }
  1365. static bool aic3x_is_shared_reset(struct aic3x_priv *aic3x)
  1366. {
  1367. struct aic3x_priv *a;
  1368. list_for_each_entry(a, &reset_list, list) {
  1369. if (gpio_is_valid(aic3x->gpio_reset) &&
  1370. aic3x->gpio_reset == a->gpio_reset)
  1371. return true;
  1372. }
  1373. return false;
  1374. }
  1375. static int aic3x_component_probe(struct snd_soc_component *component)
  1376. {
  1377. struct aic3x_priv *aic3x = snd_soc_component_get_drvdata(component);
  1378. int ret, i;
  1379. aic3x->component = component;
  1380. for (i = 0; i < ARRAY_SIZE(aic3x->supplies); i++) {
  1381. aic3x->disable_nb[i].nb.notifier_call = aic3x_regulator_event;
  1382. aic3x->disable_nb[i].aic3x = aic3x;
  1383. ret = devm_regulator_register_notifier(
  1384. aic3x->supplies[i].consumer,
  1385. &aic3x->disable_nb[i].nb);
  1386. if (ret) {
  1387. dev_err(component->dev,
  1388. "Failed to request regulator notifier: %d\n",
  1389. ret);
  1390. return ret;
  1391. }
  1392. }
  1393. regcache_mark_dirty(aic3x->regmap);
  1394. aic3x_init(component);
  1395. if (aic3x->setup) {
  1396. if (aic3x->model != AIC3X_MODEL_3104) {
  1397. /* setup GPIO functions */
  1398. snd_soc_component_write(component, AIC3X_GPIO1_REG,
  1399. (aic3x->setup->gpio_func[0] & 0xf) << 4);
  1400. snd_soc_component_write(component, AIC3X_GPIO2_REG,
  1401. (aic3x->setup->gpio_func[1] & 0xf) << 4);
  1402. } else {
  1403. dev_warn(component->dev, "GPIO functionality is not supported on tlv320aic3104\n");
  1404. }
  1405. }
  1406. switch (aic3x->model) {
  1407. case AIC3X_MODEL_3X:
  1408. case AIC3X_MODEL_33:
  1409. case AIC3X_MODEL_3106:
  1410. snd_soc_add_component_controls(component, aic3x_extra_snd_controls,
  1411. ARRAY_SIZE(aic3x_extra_snd_controls));
  1412. snd_soc_add_component_controls(component, aic3x_mono_controls,
  1413. ARRAY_SIZE(aic3x_mono_controls));
  1414. break;
  1415. case AIC3X_MODEL_3007:
  1416. snd_soc_add_component_controls(component, aic3x_extra_snd_controls,
  1417. ARRAY_SIZE(aic3x_extra_snd_controls));
  1418. snd_soc_add_component_controls(component,
  1419. &aic3x_classd_amp_gain_ctrl, 1);
  1420. break;
  1421. case AIC3X_MODEL_3104:
  1422. break;
  1423. }
  1424. /* set mic bias voltage */
  1425. switch (aic3x->micbias_vg) {
  1426. case AIC3X_MICBIAS_2_0V:
  1427. case AIC3X_MICBIAS_2_5V:
  1428. case AIC3X_MICBIAS_AVDDV:
  1429. snd_soc_component_update_bits(component, MICBIAS_CTRL,
  1430. MICBIAS_LEVEL_MASK,
  1431. (aic3x->micbias_vg) << MICBIAS_LEVEL_SHIFT);
  1432. break;
  1433. case AIC3X_MICBIAS_OFF:
  1434. /*
  1435. * noting to do. target won't enter here. This is just to avoid
  1436. * compile time warning "warning: enumeration value
  1437. * 'AIC3X_MICBIAS_OFF' not handled in switch"
  1438. */
  1439. break;
  1440. }
  1441. aic3x_add_widgets(component);
  1442. return 0;
  1443. }
  1444. static const struct snd_soc_component_driver soc_component_dev_aic3x = {
  1445. .set_bias_level = aic3x_set_bias_level,
  1446. .probe = aic3x_component_probe,
  1447. .controls = aic3x_snd_controls,
  1448. .num_controls = ARRAY_SIZE(aic3x_snd_controls),
  1449. .dapm_widgets = aic3x_dapm_widgets,
  1450. .num_dapm_widgets = ARRAY_SIZE(aic3x_dapm_widgets),
  1451. .dapm_routes = intercon,
  1452. .num_dapm_routes = ARRAY_SIZE(intercon),
  1453. .use_pmdown_time = 1,
  1454. .endianness = 1,
  1455. };
  1456. static void aic3x_configure_ocmv(struct device *dev, struct aic3x_priv *aic3x)
  1457. {
  1458. struct device_node *np = dev->of_node;
  1459. u32 value;
  1460. int dvdd, avdd;
  1461. if (np && !of_property_read_u32(np, "ai3x-ocmv", &value)) {
  1462. /* OCMV setting is forced by DT */
  1463. if (value <= 3) {
  1464. aic3x->ocmv = value;
  1465. return;
  1466. }
  1467. }
  1468. dvdd = regulator_get_voltage(aic3x->supplies[1].consumer);
  1469. avdd = regulator_get_voltage(aic3x->supplies[2].consumer);
  1470. if (avdd > 3600000 || dvdd > 1950000) {
  1471. dev_warn(dev,
  1472. "Too high supply voltage(s) AVDD: %d, DVDD: %d\n",
  1473. avdd, dvdd);
  1474. } else if (avdd == 3600000 && dvdd == 1950000) {
  1475. aic3x->ocmv = HPOUT_SC_OCMV_1_8V;
  1476. } else if (avdd > 3300000 && dvdd > 1800000) {
  1477. aic3x->ocmv = HPOUT_SC_OCMV_1_65V;
  1478. } else if (avdd > 3000000 && dvdd > 1650000) {
  1479. aic3x->ocmv = HPOUT_SC_OCMV_1_5V;
  1480. } else if (avdd >= 2700000 && dvdd >= 1525000) {
  1481. aic3x->ocmv = HPOUT_SC_OCMV_1_35V;
  1482. } else {
  1483. dev_warn(dev,
  1484. "Invalid supply voltage(s) AVDD: %d, DVDD: %d\n",
  1485. avdd, dvdd);
  1486. }
  1487. }
  1488. static const struct reg_sequence aic3007_class_d[] = {
  1489. /* Class-D speaker driver init; datasheet p. 46 */
  1490. { AIC3X_PAGE_SELECT, 0x0D },
  1491. { 0xD, 0x0D },
  1492. { 0x8, 0x5C },
  1493. { 0x8, 0x5D },
  1494. { 0x8, 0x5C },
  1495. { AIC3X_PAGE_SELECT, 0x00 },
  1496. };
  1497. int aic3x_probe(struct device *dev, struct regmap *regmap, kernel_ulong_t driver_data)
  1498. {
  1499. struct aic3x_pdata *pdata = dev->platform_data;
  1500. struct aic3x_priv *aic3x;
  1501. struct aic3x_setup_data *ai3x_setup;
  1502. struct device_node *np = dev->of_node;
  1503. int ret, i;
  1504. u32 value;
  1505. aic3x = devm_kzalloc(dev, sizeof(struct aic3x_priv), GFP_KERNEL);
  1506. if (!aic3x)
  1507. return -ENOMEM;
  1508. aic3x->regmap = regmap;
  1509. if (IS_ERR(aic3x->regmap)) {
  1510. ret = PTR_ERR(aic3x->regmap);
  1511. return ret;
  1512. }
  1513. regcache_cache_only(aic3x->regmap, true);
  1514. dev_set_drvdata(dev, aic3x);
  1515. if (pdata) {
  1516. aic3x->gpio_reset = pdata->gpio_reset;
  1517. aic3x->setup = pdata->setup;
  1518. aic3x->micbias_vg = pdata->micbias_vg;
  1519. } else if (np) {
  1520. ai3x_setup = devm_kzalloc(dev, sizeof(*ai3x_setup), GFP_KERNEL);
  1521. if (!ai3x_setup)
  1522. return -ENOMEM;
  1523. ret = of_get_named_gpio(np, "reset-gpios", 0);
  1524. if (ret >= 0) {
  1525. aic3x->gpio_reset = ret;
  1526. } else {
  1527. ret = of_get_named_gpio(np, "gpio-reset", 0);
  1528. if (ret > 0) {
  1529. dev_warn(dev, "Using deprecated property \"gpio-reset\", please update your DT");
  1530. aic3x->gpio_reset = ret;
  1531. } else {
  1532. aic3x->gpio_reset = -1;
  1533. }
  1534. }
  1535. if (of_property_read_u32_array(np, "ai3x-gpio-func",
  1536. ai3x_setup->gpio_func, 2) >= 0) {
  1537. aic3x->setup = ai3x_setup;
  1538. }
  1539. if (!of_property_read_u32(np, "ai3x-micbias-vg", &value)) {
  1540. switch (value) {
  1541. case 1 :
  1542. aic3x->micbias_vg = AIC3X_MICBIAS_2_0V;
  1543. break;
  1544. case 2 :
  1545. aic3x->micbias_vg = AIC3X_MICBIAS_2_5V;
  1546. break;
  1547. case 3 :
  1548. aic3x->micbias_vg = AIC3X_MICBIAS_AVDDV;
  1549. break;
  1550. default :
  1551. aic3x->micbias_vg = AIC3X_MICBIAS_OFF;
  1552. dev_err(dev, "Unsuitable MicBias voltage "
  1553. "found in DT\n");
  1554. }
  1555. } else {
  1556. aic3x->micbias_vg = AIC3X_MICBIAS_OFF;
  1557. }
  1558. } else {
  1559. aic3x->gpio_reset = -1;
  1560. }
  1561. aic3x->model = driver_data;
  1562. if (gpio_is_valid(aic3x->gpio_reset) &&
  1563. !aic3x_is_shared_reset(aic3x)) {
  1564. ret = gpio_request(aic3x->gpio_reset, "tlv320aic3x reset");
  1565. if (ret != 0)
  1566. goto err;
  1567. gpio_direction_output(aic3x->gpio_reset, 0);
  1568. }
  1569. for (i = 0; i < ARRAY_SIZE(aic3x->supplies); i++)
  1570. aic3x->supplies[i].supply = aic3x_supply_names[i];
  1571. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(aic3x->supplies),
  1572. aic3x->supplies);
  1573. if (ret != 0) {
  1574. dev_err(dev, "Failed to request supplies: %d\n", ret);
  1575. goto err_gpio;
  1576. }
  1577. aic3x_configure_ocmv(dev, aic3x);
  1578. if (aic3x->model == AIC3X_MODEL_3007) {
  1579. ret = regmap_register_patch(aic3x->regmap, aic3007_class_d,
  1580. ARRAY_SIZE(aic3007_class_d));
  1581. if (ret != 0)
  1582. dev_err(dev, "Failed to init class D: %d\n",
  1583. ret);
  1584. }
  1585. ret = devm_snd_soc_register_component(dev, &soc_component_dev_aic3x, &aic3x_dai, 1);
  1586. if (ret != 0)
  1587. goto err_gpio;
  1588. INIT_LIST_HEAD(&aic3x->list);
  1589. list_add(&aic3x->list, &reset_list);
  1590. return 0;
  1591. err_gpio:
  1592. if (gpio_is_valid(aic3x->gpio_reset) &&
  1593. !aic3x_is_shared_reset(aic3x))
  1594. gpio_free(aic3x->gpio_reset);
  1595. err:
  1596. return ret;
  1597. }
  1598. EXPORT_SYMBOL(aic3x_probe);
  1599. void aic3x_remove(struct device *dev)
  1600. {
  1601. struct aic3x_priv *aic3x = dev_get_drvdata(dev);
  1602. list_del(&aic3x->list);
  1603. if (gpio_is_valid(aic3x->gpio_reset) &&
  1604. !aic3x_is_shared_reset(aic3x)) {
  1605. gpio_set_value(aic3x->gpio_reset, 0);
  1606. gpio_free(aic3x->gpio_reset);
  1607. }
  1608. }
  1609. EXPORT_SYMBOL(aic3x_remove);
  1610. MODULE_DESCRIPTION("ASoC TLV320AIC3X codec driver");
  1611. MODULE_AUTHOR("Vladimir Barinov");
  1612. MODULE_LICENSE("GPL");