tlv320aic3x.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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, reg, mask, val);
  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("HP Line2 Bypass Volume",
  314. LINE2L_2_HPLOUT_VOL, LINE2R_2_HPROUT_VOL,
  315. 0, 118, 1, output_stage_tlv),
  316. SOC_DOUBLE_R_TLV("HP PGA Bypass Volume",
  317. PGAL_2_HPLOUT_VOL, PGAR_2_HPROUT_VOL,
  318. 0, 118, 1, output_stage_tlv),
  319. SOC_DOUBLE_R_TLV("HP DAC Playback Volume",
  320. DACL1_2_HPLOUT_VOL, DACR1_2_HPROUT_VOL,
  321. 0, 118, 1, output_stage_tlv),
  322. SOC_DOUBLE_R_TLV("HPCOM Line2 Bypass Volume",
  323. LINE2L_2_HPLCOM_VOL, LINE2R_2_HPRCOM_VOL,
  324. 0, 118, 1, output_stage_tlv),
  325. SOC_DOUBLE_R_TLV("HPCOM PGA Bypass Volume",
  326. PGAL_2_HPLCOM_VOL, PGAR_2_HPRCOM_VOL,
  327. 0, 118, 1, output_stage_tlv),
  328. SOC_DOUBLE_R_TLV("HPCOM DAC Playback Volume",
  329. DACL1_2_HPLCOM_VOL, DACR1_2_HPRCOM_VOL,
  330. 0, 118, 1, output_stage_tlv),
  331. /* Output pin mute controls */
  332. SOC_DOUBLE_R("Line Playback Switch", LLOPM_CTRL, RLOPM_CTRL, 3,
  333. 0x01, 0),
  334. SOC_DOUBLE_R("HP Playback Switch", HPLOUT_CTRL, HPROUT_CTRL, 3,
  335. 0x01, 0),
  336. SOC_DOUBLE_R("HPCOM Playback Switch", HPLCOM_CTRL, HPRCOM_CTRL, 3,
  337. 0x01, 0),
  338. /*
  339. * Note: enable Automatic input Gain Controller with care. It can
  340. * adjust PGA to max value when ADC is on and will never go back.
  341. */
  342. SOC_DOUBLE_R("AGC Switch", LAGC_CTRL_A, RAGC_CTRL_A, 7, 0x01, 0),
  343. SOC_ENUM("Left AGC Target level", aic3x_agc_level_enum[0]),
  344. SOC_ENUM("Right AGC Target level", aic3x_agc_level_enum[1]),
  345. SOC_ENUM("Left AGC Attack time", aic3x_agc_attack_enum[0]),
  346. SOC_ENUM("Right AGC Attack time", aic3x_agc_attack_enum[1]),
  347. SOC_ENUM("Left AGC Decay time", aic3x_agc_decay_enum[0]),
  348. SOC_ENUM("Right AGC Decay time", aic3x_agc_decay_enum[1]),
  349. /* De-emphasis */
  350. SOC_DOUBLE("De-emphasis Switch", AIC3X_CODEC_DFILT_CTRL, 2, 0, 0x01, 0),
  351. /* Input */
  352. SOC_DOUBLE_R_TLV("PGA Capture Volume", LADC_VOL, RADC_VOL,
  353. 0, 119, 0, adc_tlv),
  354. SOC_DOUBLE_R("PGA Capture Switch", LADC_VOL, RADC_VOL, 7, 0x01, 1),
  355. SOC_ENUM("ADC HPF Cut-off", aic3x_enum[ADC_HPF_ENUM]),
  356. };
  357. static const struct snd_kcontrol_new aic3x_mono_controls[] = {
  358. SOC_DOUBLE_R_TLV("Mono Line2 Bypass Volume",
  359. LINE2L_2_MONOLOPM_VOL, LINE2R_2_MONOLOPM_VOL,
  360. 0, 118, 1, output_stage_tlv),
  361. SOC_DOUBLE_R_TLV("Mono PGA Bypass Volume",
  362. PGAL_2_MONOLOPM_VOL, PGAR_2_MONOLOPM_VOL,
  363. 0, 118, 1, output_stage_tlv),
  364. SOC_DOUBLE_R_TLV("Mono DAC Playback Volume",
  365. DACL1_2_MONOLOPM_VOL, DACR1_2_MONOLOPM_VOL,
  366. 0, 118, 1, output_stage_tlv),
  367. SOC_SINGLE("Mono Playback Switch", MONOLOPM_CTRL, 3, 0x01, 0),
  368. };
  369. /*
  370. * Class-D amplifier gain. From 0 to 18 dB in 6 dB steps
  371. */
  372. static DECLARE_TLV_DB_SCALE(classd_amp_tlv, 0, 600, 0);
  373. static const struct snd_kcontrol_new aic3x_classd_amp_gain_ctrl =
  374. SOC_DOUBLE_TLV("Class-D Playback Volume", CLASSD_CTRL, 6, 4, 3, 0, classd_amp_tlv);
  375. /* Left DAC Mux */
  376. static const struct snd_kcontrol_new aic3x_left_dac_mux_controls =
  377. SOC_DAPM_ENUM("Route", aic3x_enum[LDAC_ENUM]);
  378. /* Right DAC Mux */
  379. static const struct snd_kcontrol_new aic3x_right_dac_mux_controls =
  380. SOC_DAPM_ENUM("Route", aic3x_enum[RDAC_ENUM]);
  381. /* Left HPCOM Mux */
  382. static const struct snd_kcontrol_new aic3x_left_hpcom_mux_controls =
  383. SOC_DAPM_ENUM("Route", aic3x_enum[LHPCOM_ENUM]);
  384. /* Right HPCOM Mux */
  385. static const struct snd_kcontrol_new aic3x_right_hpcom_mux_controls =
  386. SOC_DAPM_ENUM("Route", aic3x_enum[RHPCOM_ENUM]);
  387. /* Left Line Mixer */
  388. static const struct snd_kcontrol_new aic3x_left_line_mixer_controls[] = {
  389. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_LLOPM_VOL, 7, 1, 0),
  390. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_LLOPM_VOL, 7, 1, 0),
  391. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_LLOPM_VOL, 7, 1, 0),
  392. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_LLOPM_VOL, 7, 1, 0),
  393. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_LLOPM_VOL, 7, 1, 0),
  394. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_LLOPM_VOL, 7, 1, 0),
  395. };
  396. /* Right Line Mixer */
  397. static const struct snd_kcontrol_new aic3x_right_line_mixer_controls[] = {
  398. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_RLOPM_VOL, 7, 1, 0),
  399. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_RLOPM_VOL, 7, 1, 0),
  400. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_RLOPM_VOL, 7, 1, 0),
  401. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_RLOPM_VOL, 7, 1, 0),
  402. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_RLOPM_VOL, 7, 1, 0),
  403. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_RLOPM_VOL, 7, 1, 0),
  404. };
  405. /* Mono Mixer */
  406. static const struct snd_kcontrol_new aic3x_mono_mixer_controls[] = {
  407. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_MONOLOPM_VOL, 7, 1, 0),
  408. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_MONOLOPM_VOL, 7, 1, 0),
  409. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_MONOLOPM_VOL, 7, 1, 0),
  410. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_MONOLOPM_VOL, 7, 1, 0),
  411. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_MONOLOPM_VOL, 7, 1, 0),
  412. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_MONOLOPM_VOL, 7, 1, 0),
  413. };
  414. /* Left HP Mixer */
  415. static const struct snd_kcontrol_new aic3x_left_hp_mixer_controls[] = {
  416. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPLOUT_VOL, 7, 1, 0),
  417. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPLOUT_VOL, 7, 1, 0),
  418. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPLOUT_VOL, 7, 1, 0),
  419. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPLOUT_VOL, 7, 1, 0),
  420. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPLOUT_VOL, 7, 1, 0),
  421. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPLOUT_VOL, 7, 1, 0),
  422. };
  423. /* Right HP Mixer */
  424. static const struct snd_kcontrol_new aic3x_right_hp_mixer_controls[] = {
  425. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPROUT_VOL, 7, 1, 0),
  426. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPROUT_VOL, 7, 1, 0),
  427. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPROUT_VOL, 7, 1, 0),
  428. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPROUT_VOL, 7, 1, 0),
  429. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPROUT_VOL, 7, 1, 0),
  430. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPROUT_VOL, 7, 1, 0),
  431. };
  432. /* Left HPCOM Mixer */
  433. static const struct snd_kcontrol_new aic3x_left_hpcom_mixer_controls[] = {
  434. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPLCOM_VOL, 7, 1, 0),
  435. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPLCOM_VOL, 7, 1, 0),
  436. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPLCOM_VOL, 7, 1, 0),
  437. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPLCOM_VOL, 7, 1, 0),
  438. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPLCOM_VOL, 7, 1, 0),
  439. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPLCOM_VOL, 7, 1, 0),
  440. };
  441. /* Right HPCOM Mixer */
  442. static const struct snd_kcontrol_new aic3x_right_hpcom_mixer_controls[] = {
  443. SOC_DAPM_SINGLE("Line2L Bypass Switch", LINE2L_2_HPRCOM_VOL, 7, 1, 0),
  444. SOC_DAPM_SINGLE("PGAL Bypass Switch", PGAL_2_HPRCOM_VOL, 7, 1, 0),
  445. SOC_DAPM_SINGLE("DACL1 Switch", DACL1_2_HPRCOM_VOL, 7, 1, 0),
  446. SOC_DAPM_SINGLE("Line2R Bypass Switch", LINE2R_2_HPRCOM_VOL, 7, 1, 0),
  447. SOC_DAPM_SINGLE("PGAR Bypass Switch", PGAR_2_HPRCOM_VOL, 7, 1, 0),
  448. SOC_DAPM_SINGLE("DACR1 Switch", DACR1_2_HPRCOM_VOL, 7, 1, 0),
  449. };
  450. /* Left PGA Mixer */
  451. static const struct snd_kcontrol_new aic3x_left_pga_mixer_controls[] = {
  452. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_LADC_CTRL, 3, 1, 1),
  453. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_LADC_CTRL, 3, 1, 1),
  454. SOC_DAPM_SINGLE_AIC3X("Line2L Switch", LINE2L_2_LADC_CTRL, 3, 1, 1),
  455. SOC_DAPM_SINGLE_AIC3X("Mic3L Switch", MIC3LR_2_LADC_CTRL, 4, 1, 1),
  456. SOC_DAPM_SINGLE_AIC3X("Mic3R Switch", MIC3LR_2_LADC_CTRL, 0, 1, 1),
  457. };
  458. /* Right PGA Mixer */
  459. static const struct snd_kcontrol_new aic3x_right_pga_mixer_controls[] = {
  460. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_RADC_CTRL, 3, 1, 1),
  461. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_RADC_CTRL, 3, 1, 1),
  462. SOC_DAPM_SINGLE_AIC3X("Line2R Switch", LINE2R_2_RADC_CTRL, 3, 1, 1),
  463. SOC_DAPM_SINGLE_AIC3X("Mic3L Switch", MIC3LR_2_RADC_CTRL, 4, 1, 1),
  464. SOC_DAPM_SINGLE_AIC3X("Mic3R Switch", MIC3LR_2_RADC_CTRL, 0, 1, 1),
  465. };
  466. /* Left Line1 Mux */
  467. static const struct snd_kcontrol_new aic3x_left_line1l_mux_controls =
  468. SOC_DAPM_ENUM("Route", aic3x_enum[LINE1L_2_L_ENUM]);
  469. static const struct snd_kcontrol_new aic3x_right_line1l_mux_controls =
  470. SOC_DAPM_ENUM("Route", aic3x_enum[LINE1L_2_R_ENUM]);
  471. /* Right Line1 Mux */
  472. static const struct snd_kcontrol_new aic3x_right_line1r_mux_controls =
  473. SOC_DAPM_ENUM("Route", aic3x_enum[LINE1R_2_R_ENUM]);
  474. static const struct snd_kcontrol_new aic3x_left_line1r_mux_controls =
  475. SOC_DAPM_ENUM("Route", aic3x_enum[LINE1R_2_L_ENUM]);
  476. /* Left Line2 Mux */
  477. static const struct snd_kcontrol_new aic3x_left_line2_mux_controls =
  478. SOC_DAPM_ENUM("Route", aic3x_enum[LINE2L_ENUM]);
  479. /* Right Line2 Mux */
  480. static const struct snd_kcontrol_new aic3x_right_line2_mux_controls =
  481. SOC_DAPM_ENUM("Route", aic3x_enum[LINE2R_ENUM]);
  482. static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
  483. /* Left DAC to Left Outputs */
  484. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", DAC_PWR, 7, 0),
  485. SND_SOC_DAPM_MUX("Left DAC Mux", SND_SOC_NOPM, 0, 0,
  486. &aic3x_left_dac_mux_controls),
  487. SND_SOC_DAPM_MUX("Left HPCOM Mux", SND_SOC_NOPM, 0, 0,
  488. &aic3x_left_hpcom_mux_controls),
  489. SND_SOC_DAPM_PGA("Left Line Out", LLOPM_CTRL, 0, 0, NULL, 0),
  490. SND_SOC_DAPM_PGA("Left HP Out", HPLOUT_CTRL, 0, 0, NULL, 0),
  491. SND_SOC_DAPM_PGA("Left HP Com", HPLCOM_CTRL, 0, 0, NULL, 0),
  492. /* Right DAC to Right Outputs */
  493. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", DAC_PWR, 6, 0),
  494. SND_SOC_DAPM_MUX("Right DAC Mux", SND_SOC_NOPM, 0, 0,
  495. &aic3x_right_dac_mux_controls),
  496. SND_SOC_DAPM_MUX("Right HPCOM Mux", SND_SOC_NOPM, 0, 0,
  497. &aic3x_right_hpcom_mux_controls),
  498. SND_SOC_DAPM_PGA("Right Line Out", RLOPM_CTRL, 0, 0, NULL, 0),
  499. SND_SOC_DAPM_PGA("Right HP Out", HPROUT_CTRL, 0, 0, NULL, 0),
  500. SND_SOC_DAPM_PGA("Right HP Com", HPRCOM_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("Left HP Mixer", SND_SOC_NOPM, 0, 0,
  555. &aic3x_left_hp_mixer_controls[0],
  556. ARRAY_SIZE(aic3x_left_hp_mixer_controls)),
  557. SND_SOC_DAPM_MIXER("Right HP Mixer", SND_SOC_NOPM, 0, 0,
  558. &aic3x_right_hp_mixer_controls[0],
  559. ARRAY_SIZE(aic3x_right_hp_mixer_controls)),
  560. SND_SOC_DAPM_MIXER("Left HPCOM Mixer", SND_SOC_NOPM, 0, 0,
  561. &aic3x_left_hpcom_mixer_controls[0],
  562. ARRAY_SIZE(aic3x_left_hpcom_mixer_controls)),
  563. SND_SOC_DAPM_MIXER("Right HPCOM Mixer", SND_SOC_NOPM, 0, 0,
  564. &aic3x_right_hpcom_mixer_controls[0],
  565. ARRAY_SIZE(aic3x_right_hpcom_mixer_controls)),
  566. SND_SOC_DAPM_OUTPUT("LLOUT"),
  567. SND_SOC_DAPM_OUTPUT("RLOUT"),
  568. SND_SOC_DAPM_OUTPUT("HPLOUT"),
  569. SND_SOC_DAPM_OUTPUT("HPROUT"),
  570. SND_SOC_DAPM_OUTPUT("HPLCOM"),
  571. SND_SOC_DAPM_OUTPUT("HPRCOM"),
  572. SND_SOC_DAPM_INPUT("MIC3L"),
  573. SND_SOC_DAPM_INPUT("MIC3R"),
  574. SND_SOC_DAPM_INPUT("LINE1L"),
  575. SND_SOC_DAPM_INPUT("LINE1R"),
  576. SND_SOC_DAPM_INPUT("LINE2L"),
  577. SND_SOC_DAPM_INPUT("LINE2R"),
  578. /*
  579. * Virtual output pin to detection block inside codec. This can be
  580. * used to keep codec bias on if gpio or detection features are needed.
  581. * Force pin on or construct a path with an input jack and mic bias
  582. * widgets.
  583. */
  584. SND_SOC_DAPM_OUTPUT("Detection"),
  585. };
  586. static const struct snd_soc_dapm_widget aic3x_dapm_mono_widgets[] = {
  587. /* Mono Output */
  588. SND_SOC_DAPM_PGA("Mono Out", MONOLOPM_CTRL, 0, 0, NULL, 0),
  589. SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0,
  590. &aic3x_mono_mixer_controls[0],
  591. ARRAY_SIZE(aic3x_mono_mixer_controls)),
  592. SND_SOC_DAPM_OUTPUT("MONO_LOUT"),
  593. };
  594. static const struct snd_soc_dapm_widget aic3007_dapm_widgets[] = {
  595. /* Class-D outputs */
  596. SND_SOC_DAPM_PGA("Left Class-D Out", CLASSD_CTRL, 3, 0, NULL, 0),
  597. SND_SOC_DAPM_PGA("Right Class-D Out", CLASSD_CTRL, 2, 0, NULL, 0),
  598. SND_SOC_DAPM_OUTPUT("SPOP"),
  599. SND_SOC_DAPM_OUTPUT("SPOM"),
  600. };
  601. static const struct snd_soc_dapm_route intercon[] = {
  602. /* Left Input */
  603. {"Left Line1L Mux", "single-ended", "LINE1L"},
  604. {"Left Line1L Mux", "differential", "LINE1L"},
  605. {"Left Line1R Mux", "single-ended", "LINE1R"},
  606. {"Left Line1R Mux", "differential", "LINE1R"},
  607. {"Left Line2L Mux", "single-ended", "LINE2L"},
  608. {"Left Line2L Mux", "differential", "LINE2L"},
  609. {"Left PGA Mixer", "Line1L Switch", "Left Line1L Mux"},
  610. {"Left PGA Mixer", "Line1R Switch", "Left Line1R Mux"},
  611. {"Left PGA Mixer", "Line2L Switch", "Left Line2L Mux"},
  612. {"Left PGA Mixer", "Mic3L Switch", "MIC3L"},
  613. {"Left PGA Mixer", "Mic3R Switch", "MIC3R"},
  614. {"Left ADC", NULL, "Left PGA Mixer"},
  615. {"Left ADC", NULL, "GPIO1 dmic modclk"},
  616. /* Right Input */
  617. {"Right Line1R Mux", "single-ended", "LINE1R"},
  618. {"Right Line1R Mux", "differential", "LINE1R"},
  619. {"Right Line1L Mux", "single-ended", "LINE1L"},
  620. {"Right Line1L Mux", "differential", "LINE1L"},
  621. {"Right Line2R Mux", "single-ended", "LINE2R"},
  622. {"Right Line2R Mux", "differential", "LINE2R"},
  623. {"Right PGA Mixer", "Line1L Switch", "Right Line1L Mux"},
  624. {"Right PGA Mixer", "Line1R Switch", "Right Line1R Mux"},
  625. {"Right PGA Mixer", "Line2R Switch", "Right Line2R Mux"},
  626. {"Right PGA Mixer", "Mic3L Switch", "MIC3L"},
  627. {"Right PGA Mixer", "Mic3R Switch", "MIC3R"},
  628. {"Right ADC", NULL, "Right PGA Mixer"},
  629. {"Right ADC", NULL, "GPIO1 dmic modclk"},
  630. /*
  631. * Logical path between digital mic enable and GPIO1 modulator clock
  632. * output function
  633. */
  634. {"GPIO1 dmic modclk", NULL, "DMic Rate 128"},
  635. {"GPIO1 dmic modclk", NULL, "DMic Rate 64"},
  636. {"GPIO1 dmic modclk", NULL, "DMic Rate 32"},
  637. /* Left DAC Output */
  638. {"Left DAC Mux", "DAC_L1", "Left DAC"},
  639. {"Left DAC Mux", "DAC_L2", "Left DAC"},
  640. {"Left DAC Mux", "DAC_L3", "Left DAC"},
  641. /* Right DAC Output */
  642. {"Right DAC Mux", "DAC_R1", "Right DAC"},
  643. {"Right DAC Mux", "DAC_R2", "Right DAC"},
  644. {"Right DAC Mux", "DAC_R3", "Right DAC"},
  645. /* Left Line Output */
  646. {"Left Line Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  647. {"Left Line Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  648. {"Left Line Mixer", "DACL1 Switch", "Left DAC Mux"},
  649. {"Left Line Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  650. {"Left Line Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  651. {"Left Line Mixer", "DACR1 Switch", "Right DAC Mux"},
  652. {"Left Line Out", NULL, "Left Line Mixer"},
  653. {"Left Line Out", NULL, "Left DAC Mux"},
  654. {"LLOUT", NULL, "Left Line Out"},
  655. /* Right Line Output */
  656. {"Right Line Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  657. {"Right Line Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  658. {"Right Line Mixer", "DACL1 Switch", "Left DAC Mux"},
  659. {"Right Line Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  660. {"Right Line Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  661. {"Right Line Mixer", "DACR1 Switch", "Right DAC Mux"},
  662. {"Right Line Out", NULL, "Right Line Mixer"},
  663. {"Right Line Out", NULL, "Right DAC Mux"},
  664. {"RLOUT", NULL, "Right Line Out"},
  665. /* Left HP Output */
  666. {"Left HP Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  667. {"Left HP Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  668. {"Left HP Mixer", "DACL1 Switch", "Left DAC Mux"},
  669. {"Left HP Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  670. {"Left HP Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  671. {"Left HP Mixer", "DACR1 Switch", "Right DAC Mux"},
  672. {"Left HP Out", NULL, "Left HP Mixer"},
  673. {"Left HP Out", NULL, "Left DAC Mux"},
  674. {"HPLOUT", NULL, "Left HP Out"},
  675. /* Right HP Output */
  676. {"Right HP Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  677. {"Right HP Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  678. {"Right HP Mixer", "DACL1 Switch", "Left DAC Mux"},
  679. {"Right HP Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  680. {"Right HP Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  681. {"Right HP Mixer", "DACR1 Switch", "Right DAC Mux"},
  682. {"Right HP Out", NULL, "Right HP Mixer"},
  683. {"Right HP Out", NULL, "Right DAC Mux"},
  684. {"HPROUT", NULL, "Right HP Out"},
  685. /* Left HPCOM Output */
  686. {"Left HPCOM Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  687. {"Left HPCOM Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  688. {"Left HPCOM Mixer", "DACL1 Switch", "Left DAC Mux"},
  689. {"Left HPCOM Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  690. {"Left HPCOM Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  691. {"Left HPCOM Mixer", "DACR1 Switch", "Right DAC Mux"},
  692. {"Left HPCOM Mux", "differential of HPLOUT", "Left HP Mixer"},
  693. {"Left HPCOM Mux", "constant VCM", "Left HPCOM Mixer"},
  694. {"Left HPCOM Mux", "single-ended", "Left HPCOM Mixer"},
  695. {"Left HP Com", NULL, "Left HPCOM Mux"},
  696. {"HPLCOM", NULL, "Left HP Com"},
  697. /* Right HPCOM Output */
  698. {"Right HPCOM Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  699. {"Right HPCOM Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  700. {"Right HPCOM Mixer", "DACL1 Switch", "Left DAC Mux"},
  701. {"Right HPCOM Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  702. {"Right HPCOM Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  703. {"Right HPCOM Mixer", "DACR1 Switch", "Right DAC Mux"},
  704. {"Right HPCOM Mux", "differential of HPROUT", "Right HP Mixer"},
  705. {"Right HPCOM Mux", "constant VCM", "Right HPCOM Mixer"},
  706. {"Right HPCOM Mux", "single-ended", "Right HPCOM Mixer"},
  707. {"Right HPCOM Mux", "differential of HPLCOM", "Left HPCOM Mixer"},
  708. {"Right HPCOM Mux", "external feedback", "Right HPCOM Mixer"},
  709. {"Right HP Com", NULL, "Right HPCOM Mux"},
  710. {"HPRCOM", NULL, "Right HP Com"},
  711. };
  712. static const struct snd_soc_dapm_route intercon_mono[] = {
  713. /* Mono Output */
  714. {"Mono Mixer", "Line2L Bypass Switch", "Left Line2L Mux"},
  715. {"Mono Mixer", "PGAL Bypass Switch", "Left PGA Mixer"},
  716. {"Mono Mixer", "DACL1 Switch", "Left DAC Mux"},
  717. {"Mono Mixer", "Line2R Bypass Switch", "Right Line2R Mux"},
  718. {"Mono Mixer", "PGAR Bypass Switch", "Right PGA Mixer"},
  719. {"Mono Mixer", "DACR1 Switch", "Right DAC Mux"},
  720. {"Mono Out", NULL, "Mono Mixer"},
  721. {"MONO_LOUT", NULL, "Mono Out"},
  722. };
  723. static const struct snd_soc_dapm_route intercon_3007[] = {
  724. /* Class-D outputs */
  725. {"Left Class-D Out", NULL, "Left Line Out"},
  726. {"Right Class-D Out", NULL, "Left Line Out"},
  727. {"SPOP", NULL, "Left Class-D Out"},
  728. {"SPOM", NULL, "Right Class-D Out"},
  729. };
  730. static int aic3x_add_widgets(struct snd_soc_codec *codec)
  731. {
  732. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  733. struct snd_soc_dapm_context *dapm = &codec->dapm;
  734. switch (aic3x->model) {
  735. case AIC3X_MODEL_3X:
  736. case AIC3X_MODEL_33:
  737. snd_soc_dapm_new_controls(dapm, aic3x_dapm_mono_widgets,
  738. ARRAY_SIZE(aic3x_dapm_mono_widgets));
  739. snd_soc_dapm_add_routes(dapm, intercon_mono,
  740. ARRAY_SIZE(intercon_mono));
  741. break;
  742. case 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. break;
  748. }
  749. return 0;
  750. }
  751. static int aic3x_hw_params(struct snd_pcm_substream *substream,
  752. struct snd_pcm_hw_params *params,
  753. struct snd_soc_dai *dai)
  754. {
  755. struct snd_soc_codec *codec = dai->codec;
  756. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  757. int codec_clk = 0, bypass_pll = 0, fsref, last_clk = 0;
  758. u8 data, j, r, p, pll_q, pll_p = 1, pll_r = 1, pll_j = 1;
  759. u16 d, pll_d = 1;
  760. int clk;
  761. /* select data word length */
  762. data = snd_soc_read(codec, AIC3X_ASD_INTF_CTRLB) & (~(0x3 << 4));
  763. switch (params_width(params)) {
  764. case 16:
  765. break;
  766. case 20:
  767. data |= (0x01 << 4);
  768. break;
  769. case 24:
  770. data |= (0x02 << 4);
  771. break;
  772. case 32:
  773. data |= (0x03 << 4);
  774. break;
  775. }
  776. snd_soc_write(codec, AIC3X_ASD_INTF_CTRLB, data);
  777. /* Fsref can be 44100 or 48000 */
  778. fsref = (params_rate(params) % 11025 == 0) ? 44100 : 48000;
  779. /* Try to find a value for Q which allows us to bypass the PLL and
  780. * generate CODEC_CLK directly. */
  781. for (pll_q = 2; pll_q < 18; pll_q++)
  782. if (aic3x->sysclk / (128 * pll_q) == fsref) {
  783. bypass_pll = 1;
  784. break;
  785. }
  786. if (bypass_pll) {
  787. pll_q &= 0xf;
  788. snd_soc_write(codec, AIC3X_PLL_PROGA_REG, pll_q << PLLQ_SHIFT);
  789. snd_soc_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_CLKDIV);
  790. /* disable PLL if it is bypassed */
  791. snd_soc_update_bits(codec, AIC3X_PLL_PROGA_REG, PLL_ENABLE, 0);
  792. } else {
  793. snd_soc_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_PLLDIV);
  794. /* enable PLL when it is used */
  795. snd_soc_update_bits(codec, AIC3X_PLL_PROGA_REG,
  796. PLL_ENABLE, PLL_ENABLE);
  797. }
  798. /* Route Left DAC to left channel input and
  799. * right DAC to right channel input */
  800. data = (LDAC2LCH | RDAC2RCH);
  801. data |= (fsref == 44100) ? FSREF_44100 : FSREF_48000;
  802. if (params_rate(params) >= 64000)
  803. data |= DUAL_RATE_MODE;
  804. snd_soc_write(codec, AIC3X_CODEC_DATAPATH_REG, data);
  805. /* codec sample rate select */
  806. data = (fsref * 20) / params_rate(params);
  807. if (params_rate(params) < 64000)
  808. data /= 2;
  809. data /= 5;
  810. data -= 2;
  811. data |= (data << 4);
  812. snd_soc_write(codec, AIC3X_SAMPLE_RATE_SEL_REG, data);
  813. if (bypass_pll)
  814. return 0;
  815. /* Use PLL, compute appropriate setup for j, d, r and p, the closest
  816. * one wins the game. Try with d==0 first, next with d!=0.
  817. * Constraints for j are according to the datasheet.
  818. * The sysclk is divided by 1000 to prevent integer overflows.
  819. */
  820. codec_clk = (2048 * fsref) / (aic3x->sysclk / 1000);
  821. for (r = 1; r <= 16; r++)
  822. for (p = 1; p <= 8; p++) {
  823. for (j = 4; j <= 55; j++) {
  824. /* This is actually 1000*((j+(d/10000))*r)/p
  825. * The term had to be converted to get
  826. * rid of the division by 10000; d = 0 here
  827. */
  828. int tmp_clk = (1000 * j * r) / p;
  829. /* Check whether this values get closer than
  830. * the best ones we had before
  831. */
  832. if (abs(codec_clk - tmp_clk) <
  833. abs(codec_clk - last_clk)) {
  834. pll_j = j; pll_d = 0;
  835. pll_r = r; pll_p = p;
  836. last_clk = tmp_clk;
  837. }
  838. /* Early exit for exact matches */
  839. if (tmp_clk == codec_clk)
  840. goto found;
  841. }
  842. }
  843. /* try with d != 0 */
  844. for (p = 1; p <= 8; p++) {
  845. j = codec_clk * p / 1000;
  846. if (j < 4 || j > 11)
  847. continue;
  848. /* do not use codec_clk here since we'd loose precision */
  849. d = ((2048 * p * fsref) - j * aic3x->sysclk)
  850. * 100 / (aic3x->sysclk/100);
  851. clk = (10000 * j + d) / (10 * p);
  852. /* check whether this values get closer than the best
  853. * ones we had before */
  854. if (abs(codec_clk - clk) < abs(codec_clk - last_clk)) {
  855. pll_j = j; pll_d = d; pll_r = 1; pll_p = p;
  856. last_clk = clk;
  857. }
  858. /* Early exit for exact matches */
  859. if (clk == codec_clk)
  860. goto found;
  861. }
  862. if (last_clk == 0) {
  863. printk(KERN_ERR "%s(): unable to setup PLL\n", __func__);
  864. return -EINVAL;
  865. }
  866. found:
  867. snd_soc_update_bits(codec, AIC3X_PLL_PROGA_REG, PLLP_MASK, pll_p);
  868. snd_soc_write(codec, AIC3X_OVRF_STATUS_AND_PLLR_REG,
  869. pll_r << PLLR_SHIFT);
  870. snd_soc_write(codec, AIC3X_PLL_PROGB_REG, pll_j << PLLJ_SHIFT);
  871. snd_soc_write(codec, AIC3X_PLL_PROGC_REG,
  872. (pll_d >> 6) << PLLD_MSB_SHIFT);
  873. snd_soc_write(codec, AIC3X_PLL_PROGD_REG,
  874. (pll_d & 0x3F) << PLLD_LSB_SHIFT);
  875. return 0;
  876. }
  877. static int aic3x_mute(struct snd_soc_dai *dai, int mute)
  878. {
  879. struct snd_soc_codec *codec = dai->codec;
  880. u8 ldac_reg = snd_soc_read(codec, LDAC_VOL) & ~MUTE_ON;
  881. u8 rdac_reg = snd_soc_read(codec, RDAC_VOL) & ~MUTE_ON;
  882. if (mute) {
  883. snd_soc_write(codec, LDAC_VOL, ldac_reg | MUTE_ON);
  884. snd_soc_write(codec, RDAC_VOL, rdac_reg | MUTE_ON);
  885. } else {
  886. snd_soc_write(codec, LDAC_VOL, ldac_reg);
  887. snd_soc_write(codec, RDAC_VOL, rdac_reg);
  888. }
  889. return 0;
  890. }
  891. static int aic3x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  892. int clk_id, unsigned int freq, int dir)
  893. {
  894. struct snd_soc_codec *codec = codec_dai->codec;
  895. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  896. /* set clock on MCLK or GPIO2 or BCLK */
  897. snd_soc_update_bits(codec, AIC3X_CLKGEN_CTRL_REG, PLLCLK_IN_MASK,
  898. clk_id << PLLCLK_IN_SHIFT);
  899. snd_soc_update_bits(codec, AIC3X_CLKGEN_CTRL_REG, CLKDIV_IN_MASK,
  900. clk_id << CLKDIV_IN_SHIFT);
  901. aic3x->sysclk = freq;
  902. return 0;
  903. }
  904. static int aic3x_set_dai_fmt(struct snd_soc_dai *codec_dai,
  905. unsigned int fmt)
  906. {
  907. struct snd_soc_codec *codec = codec_dai->codec;
  908. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  909. u8 iface_areg, iface_breg;
  910. int delay = 0;
  911. iface_areg = snd_soc_read(codec, AIC3X_ASD_INTF_CTRLA) & 0x3f;
  912. iface_breg = snd_soc_read(codec, AIC3X_ASD_INTF_CTRLB) & 0x3f;
  913. /* set master/slave audio interface */
  914. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  915. case SND_SOC_DAIFMT_CBM_CFM:
  916. aic3x->master = 1;
  917. iface_areg |= BIT_CLK_MASTER | WORD_CLK_MASTER;
  918. break;
  919. case SND_SOC_DAIFMT_CBS_CFS:
  920. aic3x->master = 0;
  921. iface_areg &= ~(BIT_CLK_MASTER | WORD_CLK_MASTER);
  922. break;
  923. default:
  924. return -EINVAL;
  925. }
  926. /*
  927. * match both interface format and signal polarities since they
  928. * are fixed
  929. */
  930. switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
  931. SND_SOC_DAIFMT_INV_MASK)) {
  932. case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF):
  933. break;
  934. case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF):
  935. delay = 1;
  936. case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF):
  937. iface_breg |= (0x01 << 6);
  938. break;
  939. case (SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF):
  940. iface_breg |= (0x02 << 6);
  941. break;
  942. case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF):
  943. iface_breg |= (0x03 << 6);
  944. break;
  945. default:
  946. return -EINVAL;
  947. }
  948. /* set iface */
  949. snd_soc_write(codec, AIC3X_ASD_INTF_CTRLA, iface_areg);
  950. snd_soc_write(codec, AIC3X_ASD_INTF_CTRLB, iface_breg);
  951. snd_soc_write(codec, AIC3X_ASD_INTF_CTRLC, delay);
  952. return 0;
  953. }
  954. static int aic3x_regulator_event(struct notifier_block *nb,
  955. unsigned long event, void *data)
  956. {
  957. struct aic3x_disable_nb *disable_nb =
  958. container_of(nb, struct aic3x_disable_nb, nb);
  959. struct aic3x_priv *aic3x = disable_nb->aic3x;
  960. if (event & REGULATOR_EVENT_DISABLE) {
  961. /*
  962. * Put codec to reset and require cache sync as at least one
  963. * of the supplies was disabled
  964. */
  965. if (gpio_is_valid(aic3x->gpio_reset))
  966. gpio_set_value(aic3x->gpio_reset, 0);
  967. regcache_mark_dirty(aic3x->regmap);
  968. }
  969. return 0;
  970. }
  971. static int aic3x_set_power(struct snd_soc_codec *codec, int power)
  972. {
  973. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  974. unsigned int pll_c, pll_d;
  975. int ret;
  976. if (power) {
  977. ret = regulator_bulk_enable(ARRAY_SIZE(aic3x->supplies),
  978. aic3x->supplies);
  979. if (ret)
  980. goto out;
  981. aic3x->power = 1;
  982. if (gpio_is_valid(aic3x->gpio_reset)) {
  983. udelay(1);
  984. gpio_set_value(aic3x->gpio_reset, 1);
  985. }
  986. /* Sync reg_cache with the hardware */
  987. regcache_cache_only(aic3x->regmap, false);
  988. regcache_sync(aic3x->regmap);
  989. /* Rewrite paired PLL D registers in case cached sync skipped
  990. * writing one of them and thus caused other one also not
  991. * being written
  992. */
  993. pll_c = snd_soc_read(codec, AIC3X_PLL_PROGC_REG);
  994. pll_d = snd_soc_read(codec, AIC3X_PLL_PROGD_REG);
  995. if (pll_c == aic3x_reg[AIC3X_PLL_PROGC_REG].def ||
  996. pll_d == aic3x_reg[AIC3X_PLL_PROGD_REG].def) {
  997. snd_soc_write(codec, AIC3X_PLL_PROGC_REG, pll_c);
  998. snd_soc_write(codec, AIC3X_PLL_PROGD_REG, pll_d);
  999. }
  1000. } else {
  1001. /*
  1002. * Do soft reset to this codec instance in order to clear
  1003. * possible VDD leakage currents in case the supply regulators
  1004. * remain on
  1005. */
  1006. snd_soc_write(codec, AIC3X_RESET, SOFT_RESET);
  1007. regcache_mark_dirty(aic3x->regmap);
  1008. aic3x->power = 0;
  1009. /* HW writes are needless when bias is off */
  1010. regcache_cache_only(aic3x->regmap, true);
  1011. ret = regulator_bulk_disable(ARRAY_SIZE(aic3x->supplies),
  1012. aic3x->supplies);
  1013. }
  1014. out:
  1015. return ret;
  1016. }
  1017. static int aic3x_set_bias_level(struct snd_soc_codec *codec,
  1018. enum snd_soc_bias_level level)
  1019. {
  1020. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  1021. switch (level) {
  1022. case SND_SOC_BIAS_ON:
  1023. break;
  1024. case SND_SOC_BIAS_PREPARE:
  1025. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY &&
  1026. aic3x->master) {
  1027. /* enable pll */
  1028. snd_soc_update_bits(codec, AIC3X_PLL_PROGA_REG,
  1029. PLL_ENABLE, PLL_ENABLE);
  1030. }
  1031. break;
  1032. case SND_SOC_BIAS_STANDBY:
  1033. if (!aic3x->power)
  1034. aic3x_set_power(codec, 1);
  1035. if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE &&
  1036. aic3x->master) {
  1037. /* disable pll */
  1038. snd_soc_update_bits(codec, AIC3X_PLL_PROGA_REG,
  1039. PLL_ENABLE, 0);
  1040. }
  1041. break;
  1042. case SND_SOC_BIAS_OFF:
  1043. if (aic3x->power)
  1044. aic3x_set_power(codec, 0);
  1045. break;
  1046. }
  1047. codec->dapm.bias_level = level;
  1048. return 0;
  1049. }
  1050. #define AIC3X_RATES SNDRV_PCM_RATE_8000_96000
  1051. #define AIC3X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  1052. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \
  1053. SNDRV_PCM_FMTBIT_S32_LE)
  1054. static const struct snd_soc_dai_ops aic3x_dai_ops = {
  1055. .hw_params = aic3x_hw_params,
  1056. .digital_mute = aic3x_mute,
  1057. .set_sysclk = aic3x_set_dai_sysclk,
  1058. .set_fmt = aic3x_set_dai_fmt,
  1059. };
  1060. static struct snd_soc_dai_driver aic3x_dai = {
  1061. .name = "tlv320aic3x-hifi",
  1062. .playback = {
  1063. .stream_name = "Playback",
  1064. .channels_min = 2,
  1065. .channels_max = 2,
  1066. .rates = AIC3X_RATES,
  1067. .formats = AIC3X_FORMATS,},
  1068. .capture = {
  1069. .stream_name = "Capture",
  1070. .channels_min = 2,
  1071. .channels_max = 2,
  1072. .rates = AIC3X_RATES,
  1073. .formats = AIC3X_FORMATS,},
  1074. .ops = &aic3x_dai_ops,
  1075. .symmetric_rates = 1,
  1076. };
  1077. static void aic3x_mono_init(struct snd_soc_codec *codec)
  1078. {
  1079. /* DAC to Mono Line Out default volume and route to Output mixer */
  1080. snd_soc_write(codec, DACL1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1081. snd_soc_write(codec, DACR1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1082. /* unmute all outputs */
  1083. snd_soc_update_bits(codec, MONOLOPM_CTRL, UNMUTE, UNMUTE);
  1084. /* PGA to Mono Line Out default volume, disconnect from Output Mixer */
  1085. snd_soc_write(codec, PGAL_2_MONOLOPM_VOL, DEFAULT_VOL);
  1086. snd_soc_write(codec, PGAR_2_MONOLOPM_VOL, DEFAULT_VOL);
  1087. /* Line2 to Mono Out default volume, disconnect from Output Mixer */
  1088. snd_soc_write(codec, LINE2L_2_MONOLOPM_VOL, DEFAULT_VOL);
  1089. snd_soc_write(codec, LINE2R_2_MONOLOPM_VOL, DEFAULT_VOL);
  1090. }
  1091. /*
  1092. * initialise the AIC3X driver
  1093. * register the mixer and dsp interfaces with the kernel
  1094. */
  1095. static int aic3x_init(struct snd_soc_codec *codec)
  1096. {
  1097. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  1098. snd_soc_write(codec, AIC3X_PAGE_SELECT, PAGE0_SELECT);
  1099. snd_soc_write(codec, AIC3X_RESET, SOFT_RESET);
  1100. /* DAC default volume and mute */
  1101. snd_soc_write(codec, LDAC_VOL, DEFAULT_VOL | MUTE_ON);
  1102. snd_soc_write(codec, RDAC_VOL, DEFAULT_VOL | MUTE_ON);
  1103. /* DAC to HP default volume and route to Output mixer */
  1104. snd_soc_write(codec, DACL1_2_HPLOUT_VOL, DEFAULT_VOL | ROUTE_ON);
  1105. snd_soc_write(codec, DACR1_2_HPROUT_VOL, DEFAULT_VOL | ROUTE_ON);
  1106. snd_soc_write(codec, DACL1_2_HPLCOM_VOL, DEFAULT_VOL | ROUTE_ON);
  1107. snd_soc_write(codec, DACR1_2_HPRCOM_VOL, DEFAULT_VOL | ROUTE_ON);
  1108. /* DAC to Line Out default volume and route to Output mixer */
  1109. snd_soc_write(codec, DACL1_2_LLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1110. snd_soc_write(codec, DACR1_2_RLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1111. /* unmute all outputs */
  1112. snd_soc_update_bits(codec, LLOPM_CTRL, UNMUTE, UNMUTE);
  1113. snd_soc_update_bits(codec, RLOPM_CTRL, UNMUTE, UNMUTE);
  1114. snd_soc_update_bits(codec, HPLOUT_CTRL, UNMUTE, UNMUTE);
  1115. snd_soc_update_bits(codec, HPROUT_CTRL, UNMUTE, UNMUTE);
  1116. snd_soc_update_bits(codec, HPLCOM_CTRL, UNMUTE, UNMUTE);
  1117. snd_soc_update_bits(codec, HPRCOM_CTRL, UNMUTE, UNMUTE);
  1118. /* ADC default volume and unmute */
  1119. snd_soc_write(codec, LADC_VOL, DEFAULT_GAIN);
  1120. snd_soc_write(codec, RADC_VOL, DEFAULT_GAIN);
  1121. /* By default route Line1 to ADC PGA mixer */
  1122. snd_soc_write(codec, LINE1L_2_LADC_CTRL, 0x0);
  1123. snd_soc_write(codec, LINE1R_2_RADC_CTRL, 0x0);
  1124. /* PGA to HP Bypass default volume, disconnect from Output Mixer */
  1125. snd_soc_write(codec, PGAL_2_HPLOUT_VOL, DEFAULT_VOL);
  1126. snd_soc_write(codec, PGAR_2_HPROUT_VOL, DEFAULT_VOL);
  1127. snd_soc_write(codec, PGAL_2_HPLCOM_VOL, DEFAULT_VOL);
  1128. snd_soc_write(codec, PGAR_2_HPRCOM_VOL, DEFAULT_VOL);
  1129. /* PGA to Line Out default volume, disconnect from Output Mixer */
  1130. snd_soc_write(codec, PGAL_2_LLOPM_VOL, DEFAULT_VOL);
  1131. snd_soc_write(codec, PGAR_2_RLOPM_VOL, DEFAULT_VOL);
  1132. /* Line2 to HP Bypass default volume, disconnect from Output Mixer */
  1133. snd_soc_write(codec, LINE2L_2_HPLOUT_VOL, DEFAULT_VOL);
  1134. snd_soc_write(codec, LINE2R_2_HPROUT_VOL, DEFAULT_VOL);
  1135. snd_soc_write(codec, LINE2L_2_HPLCOM_VOL, DEFAULT_VOL);
  1136. snd_soc_write(codec, LINE2R_2_HPRCOM_VOL, DEFAULT_VOL);
  1137. /* Line2 Line Out default volume, disconnect from Output Mixer */
  1138. snd_soc_write(codec, LINE2L_2_LLOPM_VOL, DEFAULT_VOL);
  1139. snd_soc_write(codec, LINE2R_2_RLOPM_VOL, DEFAULT_VOL);
  1140. switch (aic3x->model) {
  1141. case AIC3X_MODEL_3X:
  1142. case AIC3X_MODEL_33:
  1143. aic3x_mono_init(codec);
  1144. break;
  1145. case AIC3X_MODEL_3007:
  1146. snd_soc_write(codec, CLASSD_CTRL, 0);
  1147. break;
  1148. }
  1149. return 0;
  1150. }
  1151. static bool aic3x_is_shared_reset(struct aic3x_priv *aic3x)
  1152. {
  1153. struct aic3x_priv *a;
  1154. list_for_each_entry(a, &reset_list, list) {
  1155. if (gpio_is_valid(aic3x->gpio_reset) &&
  1156. aic3x->gpio_reset == a->gpio_reset)
  1157. return true;
  1158. }
  1159. return false;
  1160. }
  1161. static int aic3x_probe(struct snd_soc_codec *codec)
  1162. {
  1163. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  1164. int ret, i;
  1165. INIT_LIST_HEAD(&aic3x->list);
  1166. aic3x->codec = codec;
  1167. for (i = 0; i < ARRAY_SIZE(aic3x->supplies); i++) {
  1168. aic3x->disable_nb[i].nb.notifier_call = aic3x_regulator_event;
  1169. aic3x->disable_nb[i].aic3x = aic3x;
  1170. ret = regulator_register_notifier(aic3x->supplies[i].consumer,
  1171. &aic3x->disable_nb[i].nb);
  1172. if (ret) {
  1173. dev_err(codec->dev,
  1174. "Failed to request regulator notifier: %d\n",
  1175. ret);
  1176. goto err_notif;
  1177. }
  1178. }
  1179. regcache_mark_dirty(aic3x->regmap);
  1180. aic3x_init(codec);
  1181. if (aic3x->setup) {
  1182. /* setup GPIO functions */
  1183. snd_soc_write(codec, AIC3X_GPIO1_REG,
  1184. (aic3x->setup->gpio_func[0] & 0xf) << 4);
  1185. snd_soc_write(codec, AIC3X_GPIO2_REG,
  1186. (aic3x->setup->gpio_func[1] & 0xf) << 4);
  1187. }
  1188. switch (aic3x->model) {
  1189. case AIC3X_MODEL_3X:
  1190. case AIC3X_MODEL_33:
  1191. snd_soc_add_codec_controls(codec, aic3x_mono_controls,
  1192. ARRAY_SIZE(aic3x_mono_controls));
  1193. break;
  1194. case AIC3X_MODEL_3007:
  1195. snd_soc_add_codec_controls(codec,
  1196. &aic3x_classd_amp_gain_ctrl, 1);
  1197. break;
  1198. }
  1199. /* set mic bias voltage */
  1200. switch (aic3x->micbias_vg) {
  1201. case AIC3X_MICBIAS_2_0V:
  1202. case AIC3X_MICBIAS_2_5V:
  1203. case AIC3X_MICBIAS_AVDDV:
  1204. snd_soc_update_bits(codec, MICBIAS_CTRL,
  1205. MICBIAS_LEVEL_MASK,
  1206. (aic3x->micbias_vg) << MICBIAS_LEVEL_SHIFT);
  1207. break;
  1208. case AIC3X_MICBIAS_OFF:
  1209. /*
  1210. * noting to do. target won't enter here. This is just to avoid
  1211. * compile time warning "warning: enumeration value
  1212. * 'AIC3X_MICBIAS_OFF' not handled in switch"
  1213. */
  1214. break;
  1215. }
  1216. aic3x_add_widgets(codec);
  1217. return 0;
  1218. err_notif:
  1219. while (i--)
  1220. regulator_unregister_notifier(aic3x->supplies[i].consumer,
  1221. &aic3x->disable_nb[i].nb);
  1222. return ret;
  1223. }
  1224. static int aic3x_remove(struct snd_soc_codec *codec)
  1225. {
  1226. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  1227. int i;
  1228. aic3x_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1229. list_del(&aic3x->list);
  1230. for (i = 0; i < ARRAY_SIZE(aic3x->supplies); i++)
  1231. regulator_unregister_notifier(aic3x->supplies[i].consumer,
  1232. &aic3x->disable_nb[i].nb);
  1233. return 0;
  1234. }
  1235. static struct snd_soc_codec_driver soc_codec_dev_aic3x = {
  1236. .set_bias_level = aic3x_set_bias_level,
  1237. .idle_bias_off = true,
  1238. .probe = aic3x_probe,
  1239. .remove = aic3x_remove,
  1240. .controls = aic3x_snd_controls,
  1241. .num_controls = ARRAY_SIZE(aic3x_snd_controls),
  1242. .dapm_widgets = aic3x_dapm_widgets,
  1243. .num_dapm_widgets = ARRAY_SIZE(aic3x_dapm_widgets),
  1244. .dapm_routes = intercon,
  1245. .num_dapm_routes = ARRAY_SIZE(intercon),
  1246. };
  1247. /*
  1248. * AIC3X 2 wire address can be up to 4 devices with device addresses
  1249. * 0x18, 0x19, 0x1A, 0x1B
  1250. */
  1251. static const struct i2c_device_id aic3x_i2c_id[] = {
  1252. { "tlv320aic3x", AIC3X_MODEL_3X },
  1253. { "tlv320aic33", AIC3X_MODEL_33 },
  1254. { "tlv320aic3007", AIC3X_MODEL_3007 },
  1255. { "tlv320aic3106", AIC3X_MODEL_3X },
  1256. { }
  1257. };
  1258. MODULE_DEVICE_TABLE(i2c, aic3x_i2c_id);
  1259. static const struct reg_default aic3007_class_d[] = {
  1260. /* Class-D speaker driver init; datasheet p. 46 */
  1261. { AIC3X_PAGE_SELECT, 0x0D },
  1262. { 0xD, 0x0D },
  1263. { 0x8, 0x5C },
  1264. { 0x8, 0x5D },
  1265. { 0x8, 0x5C },
  1266. { AIC3X_PAGE_SELECT, 0x00 },
  1267. };
  1268. /*
  1269. * If the i2c layer weren't so broken, we could pass this kind of data
  1270. * around
  1271. */
  1272. static int aic3x_i2c_probe(struct i2c_client *i2c,
  1273. const struct i2c_device_id *id)
  1274. {
  1275. struct aic3x_pdata *pdata = i2c->dev.platform_data;
  1276. struct aic3x_priv *aic3x;
  1277. struct aic3x_setup_data *ai3x_setup;
  1278. struct device_node *np = i2c->dev.of_node;
  1279. int ret, i;
  1280. u32 value;
  1281. aic3x = devm_kzalloc(&i2c->dev, sizeof(struct aic3x_priv), GFP_KERNEL);
  1282. if (!aic3x)
  1283. return -ENOMEM;
  1284. aic3x->regmap = devm_regmap_init_i2c(i2c, &aic3x_regmap);
  1285. if (IS_ERR(aic3x->regmap)) {
  1286. ret = PTR_ERR(aic3x->regmap);
  1287. return ret;
  1288. }
  1289. regcache_cache_only(aic3x->regmap, true);
  1290. i2c_set_clientdata(i2c, aic3x);
  1291. if (pdata) {
  1292. aic3x->gpio_reset = pdata->gpio_reset;
  1293. aic3x->setup = pdata->setup;
  1294. aic3x->micbias_vg = pdata->micbias_vg;
  1295. } else if (np) {
  1296. ai3x_setup = devm_kzalloc(&i2c->dev, sizeof(*ai3x_setup),
  1297. GFP_KERNEL);
  1298. if (!ai3x_setup)
  1299. return -ENOMEM;
  1300. ret = of_get_named_gpio(np, "gpio-reset", 0);
  1301. if (ret >= 0)
  1302. aic3x->gpio_reset = ret;
  1303. else
  1304. aic3x->gpio_reset = -1;
  1305. if (of_property_read_u32_array(np, "ai3x-gpio-func",
  1306. ai3x_setup->gpio_func, 2) >= 0) {
  1307. aic3x->setup = ai3x_setup;
  1308. }
  1309. if (!of_property_read_u32(np, "ai3x-micbias-vg", &value)) {
  1310. switch (value) {
  1311. case 1 :
  1312. aic3x->micbias_vg = AIC3X_MICBIAS_2_0V;
  1313. break;
  1314. case 2 :
  1315. aic3x->micbias_vg = AIC3X_MICBIAS_2_5V;
  1316. break;
  1317. case 3 :
  1318. aic3x->micbias_vg = AIC3X_MICBIAS_AVDDV;
  1319. break;
  1320. default :
  1321. aic3x->micbias_vg = AIC3X_MICBIAS_OFF;
  1322. dev_err(&i2c->dev, "Unsuitable MicBias voltage "
  1323. "found in DT\n");
  1324. }
  1325. } else {
  1326. aic3x->micbias_vg = AIC3X_MICBIAS_OFF;
  1327. }
  1328. } else {
  1329. aic3x->gpio_reset = -1;
  1330. }
  1331. aic3x->model = id->driver_data;
  1332. if (gpio_is_valid(aic3x->gpio_reset) &&
  1333. !aic3x_is_shared_reset(aic3x)) {
  1334. ret = gpio_request(aic3x->gpio_reset, "tlv320aic3x reset");
  1335. if (ret != 0)
  1336. goto err;
  1337. gpio_direction_output(aic3x->gpio_reset, 0);
  1338. }
  1339. for (i = 0; i < ARRAY_SIZE(aic3x->supplies); i++)
  1340. aic3x->supplies[i].supply = aic3x_supply_names[i];
  1341. ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(aic3x->supplies),
  1342. aic3x->supplies);
  1343. if (ret != 0) {
  1344. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  1345. goto err_gpio;
  1346. }
  1347. if (aic3x->model == AIC3X_MODEL_3007) {
  1348. ret = regmap_register_patch(aic3x->regmap, aic3007_class_d,
  1349. ARRAY_SIZE(aic3007_class_d));
  1350. if (ret != 0)
  1351. dev_err(&i2c->dev, "Failed to init class D: %d\n",
  1352. ret);
  1353. }
  1354. ret = snd_soc_register_codec(&i2c->dev,
  1355. &soc_codec_dev_aic3x, &aic3x_dai, 1);
  1356. if (ret != 0)
  1357. goto err_gpio;
  1358. list_add(&aic3x->list, &reset_list);
  1359. return 0;
  1360. err_gpio:
  1361. if (gpio_is_valid(aic3x->gpio_reset) &&
  1362. !aic3x_is_shared_reset(aic3x))
  1363. gpio_free(aic3x->gpio_reset);
  1364. err:
  1365. return ret;
  1366. }
  1367. static int aic3x_i2c_remove(struct i2c_client *client)
  1368. {
  1369. struct aic3x_priv *aic3x = i2c_get_clientdata(client);
  1370. snd_soc_unregister_codec(&client->dev);
  1371. if (gpio_is_valid(aic3x->gpio_reset) &&
  1372. !aic3x_is_shared_reset(aic3x)) {
  1373. gpio_set_value(aic3x->gpio_reset, 0);
  1374. gpio_free(aic3x->gpio_reset);
  1375. }
  1376. return 0;
  1377. }
  1378. #if defined(CONFIG_OF)
  1379. static const struct of_device_id tlv320aic3x_of_match[] = {
  1380. { .compatible = "ti,tlv320aic3x", },
  1381. { .compatible = "ti,tlv320aic33" },
  1382. { .compatible = "ti,tlv320aic3007" },
  1383. { .compatible = "ti,tlv320aic3106" },
  1384. {},
  1385. };
  1386. MODULE_DEVICE_TABLE(of, tlv320aic3x_of_match);
  1387. #endif
  1388. /* machine i2c codec control layer */
  1389. static struct i2c_driver aic3x_i2c_driver = {
  1390. .driver = {
  1391. .name = "tlv320aic3x-codec",
  1392. .owner = THIS_MODULE,
  1393. .of_match_table = of_match_ptr(tlv320aic3x_of_match),
  1394. },
  1395. .probe = aic3x_i2c_probe,
  1396. .remove = aic3x_i2c_remove,
  1397. .id_table = aic3x_i2c_id,
  1398. };
  1399. module_i2c_driver(aic3x_i2c_driver);
  1400. MODULE_DESCRIPTION("ASoC TLV320AIC3X codec driver");
  1401. MODULE_AUTHOR("Vladimir Barinov");
  1402. MODULE_LICENSE("GPL");