tlv320aic3x.c 52 KB

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