tlv320aic3x.c 52 KB

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