tlv320aic3x.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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.
  16. *
  17. * It supports full aic33 codec functionality.
  18. * The compatibility with aic32, aic31 is as follows:
  19. * aic32 | 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/platform_device.h>
  43. #include <sound/core.h>
  44. #include <sound/pcm.h>
  45. #include <sound/pcm_params.h>
  46. #include <sound/soc.h>
  47. #include <sound/soc-dapm.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. /* codec private data */
  60. struct aic3x_priv {
  61. struct snd_soc_codec codec;
  62. struct regulator_bulk_data supplies[AIC3X_NUM_SUPPLIES];
  63. unsigned int sysclk;
  64. int master;
  65. int gpio_reset;
  66. };
  67. /*
  68. * AIC3X register cache
  69. * We can't read the AIC3X register space when we are
  70. * using 2 wire for device control, so we cache them instead.
  71. * There is no point in caching the reset register
  72. */
  73. static const u8 aic3x_reg[AIC3X_CACHEREGNUM] = {
  74. 0x00, 0x00, 0x00, 0x10, /* 0 */
  75. 0x04, 0x00, 0x00, 0x00, /* 4 */
  76. 0x00, 0x00, 0x00, 0x01, /* 8 */
  77. 0x00, 0x00, 0x00, 0x80, /* 12 */
  78. 0x80, 0xff, 0xff, 0x78, /* 16 */
  79. 0x78, 0x78, 0x78, 0x78, /* 20 */
  80. 0x78, 0x00, 0x00, 0xfe, /* 24 */
  81. 0x00, 0x00, 0xfe, 0x00, /* 28 */
  82. 0x18, 0x18, 0x00, 0x00, /* 32 */
  83. 0x00, 0x00, 0x00, 0x00, /* 36 */
  84. 0x00, 0x00, 0x00, 0x80, /* 40 */
  85. 0x80, 0x00, 0x00, 0x00, /* 44 */
  86. 0x00, 0x00, 0x00, 0x04, /* 48 */
  87. 0x00, 0x00, 0x00, 0x00, /* 52 */
  88. 0x00, 0x00, 0x04, 0x00, /* 56 */
  89. 0x00, 0x00, 0x00, 0x00, /* 60 */
  90. 0x00, 0x04, 0x00, 0x00, /* 64 */
  91. 0x00, 0x00, 0x00, 0x00, /* 68 */
  92. 0x04, 0x00, 0x00, 0x00, /* 72 */
  93. 0x00, 0x00, 0x00, 0x00, /* 76 */
  94. 0x00, 0x00, 0x00, 0x00, /* 80 */
  95. 0x00, 0x00, 0x00, 0x00, /* 84 */
  96. 0x00, 0x00, 0x00, 0x00, /* 88 */
  97. 0x00, 0x00, 0x00, 0x00, /* 92 */
  98. 0x00, 0x00, 0x00, 0x00, /* 96 */
  99. 0x00, 0x00, 0x02, /* 100 */
  100. };
  101. /*
  102. * read aic3x register cache
  103. */
  104. static inline unsigned int aic3x_read_reg_cache(struct snd_soc_codec *codec,
  105. unsigned int reg)
  106. {
  107. u8 *cache = codec->reg_cache;
  108. if (reg >= AIC3X_CACHEREGNUM)
  109. return -1;
  110. return cache[reg];
  111. }
  112. /*
  113. * write aic3x register cache
  114. */
  115. static inline void aic3x_write_reg_cache(struct snd_soc_codec *codec,
  116. u8 reg, u8 value)
  117. {
  118. u8 *cache = codec->reg_cache;
  119. if (reg >= AIC3X_CACHEREGNUM)
  120. return;
  121. cache[reg] = value;
  122. }
  123. /*
  124. * write to the aic3x register space
  125. */
  126. static int aic3x_write(struct snd_soc_codec *codec, unsigned int reg,
  127. unsigned int value)
  128. {
  129. u8 data[2];
  130. /* data is
  131. * D15..D8 aic3x register offset
  132. * D7...D0 register data
  133. */
  134. data[0] = reg & 0xff;
  135. data[1] = value & 0xff;
  136. aic3x_write_reg_cache(codec, data[0], data[1]);
  137. if (codec->hw_write(codec->control_data, data, 2) == 2)
  138. return 0;
  139. else
  140. return -EIO;
  141. }
  142. /*
  143. * read from the aic3x register space
  144. */
  145. static int aic3x_read(struct snd_soc_codec *codec, unsigned int reg,
  146. u8 *value)
  147. {
  148. *value = reg & 0xff;
  149. value[0] = i2c_smbus_read_byte_data(codec->control_data, value[0]);
  150. aic3x_write_reg_cache(codec, reg, *value);
  151. return 0;
  152. }
  153. #define SOC_DAPM_SINGLE_AIC3X(xname, reg, shift, mask, invert) \
  154. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  155. .info = snd_soc_info_volsw, \
  156. .get = snd_soc_dapm_get_volsw, .put = snd_soc_dapm_put_volsw_aic3x, \
  157. .private_value = SOC_SINGLE_VALUE(reg, shift, mask, invert) }
  158. /*
  159. * All input lines are connected when !0xf and disconnected with 0xf bit field,
  160. * so we have to use specific dapm_put call for input mixer
  161. */
  162. static int snd_soc_dapm_put_volsw_aic3x(struct snd_kcontrol *kcontrol,
  163. struct snd_ctl_elem_value *ucontrol)
  164. {
  165. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  166. struct soc_mixer_control *mc =
  167. (struct soc_mixer_control *)kcontrol->private_value;
  168. unsigned int reg = mc->reg;
  169. unsigned int shift = mc->shift;
  170. int max = mc->max;
  171. unsigned int mask = (1 << fls(max)) - 1;
  172. unsigned int invert = mc->invert;
  173. unsigned short val, val_mask;
  174. int ret;
  175. struct snd_soc_dapm_path *path;
  176. int found = 0;
  177. val = (ucontrol->value.integer.value[0] & mask);
  178. mask = 0xf;
  179. if (val)
  180. val = mask;
  181. if (invert)
  182. val = mask - val;
  183. val_mask = mask << shift;
  184. val = val << shift;
  185. mutex_lock(&widget->codec->mutex);
  186. if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
  187. /* find dapm widget path assoc with kcontrol */
  188. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  189. if (path->kcontrol != kcontrol)
  190. continue;
  191. /* found, now check type */
  192. found = 1;
  193. if (val)
  194. /* new connection */
  195. path->connect = invert ? 0 : 1;
  196. else
  197. /* old connection must be powered down */
  198. path->connect = invert ? 1 : 0;
  199. break;
  200. }
  201. if (found)
  202. snd_soc_dapm_sync(widget->codec);
  203. }
  204. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  205. mutex_unlock(&widget->codec->mutex);
  206. return ret;
  207. }
  208. static const char *aic3x_left_dac_mux[] = { "DAC_L1", "DAC_L3", "DAC_L2" };
  209. static const char *aic3x_right_dac_mux[] = { "DAC_R1", "DAC_R3", "DAC_R2" };
  210. static const char *aic3x_left_hpcom_mux[] =
  211. { "differential of HPLOUT", "constant VCM", "single-ended" };
  212. static const char *aic3x_right_hpcom_mux[] =
  213. { "differential of HPROUT", "constant VCM", "single-ended",
  214. "differential of HPLCOM", "external feedback" };
  215. static const char *aic3x_linein_mode_mux[] = { "single-ended", "differential" };
  216. static const char *aic3x_adc_hpf[] =
  217. { "Disabled", "0.0045xFs", "0.0125xFs", "0.025xFs" };
  218. #define LDAC_ENUM 0
  219. #define RDAC_ENUM 1
  220. #define LHPCOM_ENUM 2
  221. #define RHPCOM_ENUM 3
  222. #define LINE1L_ENUM 4
  223. #define LINE1R_ENUM 5
  224. #define LINE2L_ENUM 6
  225. #define LINE2R_ENUM 7
  226. #define ADC_HPF_ENUM 8
  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(LINE1R_2_RADC_CTRL, 7, 2, aic3x_linein_mode_mux),
  234. SOC_ENUM_SINGLE(LINE2L_2_LADC_CTRL, 7, 2, aic3x_linein_mode_mux),
  235. SOC_ENUM_SINGLE(LINE2R_2_RADC_CTRL, 7, 2, aic3x_linein_mode_mux),
  236. SOC_ENUM_DOUBLE(AIC3X_CODEC_DFILT_CTRL, 6, 4, 4, aic3x_adc_hpf),
  237. };
  238. /*
  239. * DAC digital volumes. From -63.5 to 0 dB in 0.5 dB steps
  240. */
  241. static DECLARE_TLV_DB_SCALE(dac_tlv, -6350, 50, 0);
  242. /* ADC PGA gain volumes. From 0 to 59.5 dB in 0.5 dB steps */
  243. static DECLARE_TLV_DB_SCALE(adc_tlv, 0, 50, 0);
  244. /*
  245. * Output stage volumes. From -78.3 to 0 dB. Muted below -78.3 dB.
  246. * Step size is approximately 0.5 dB over most of the scale but increasing
  247. * near the very low levels.
  248. * Define dB scale so that it is mostly correct for range about -55 to 0 dB
  249. * but having increasing dB difference below that (and where it doesn't count
  250. * so much). This setting shows -50 dB (actual is -50.3 dB) for register
  251. * value 100 and -58.5 dB (actual is -78.3 dB) for register value 117.
  252. */
  253. static DECLARE_TLV_DB_SCALE(output_stage_tlv, -5900, 50, 1);
  254. static const struct snd_kcontrol_new aic3x_snd_controls[] = {
  255. /* Output */
  256. SOC_DOUBLE_R_TLV("PCM Playback Volume",
  257. LDAC_VOL, RDAC_VOL, 0, 0x7f, 1, dac_tlv),
  258. SOC_DOUBLE_R_TLV("Line DAC Playback Volume",
  259. DACL1_2_LLOPM_VOL, DACR1_2_RLOPM_VOL,
  260. 0, 118, 1, output_stage_tlv),
  261. SOC_SINGLE("LineL Playback Switch", LLOPM_CTRL, 3, 0x01, 0),
  262. SOC_SINGLE("LineR Playback Switch", RLOPM_CTRL, 3, 0x01, 0),
  263. SOC_DOUBLE_R_TLV("LineL DAC Playback Volume",
  264. DACL1_2_LLOPM_VOL, DACR1_2_LLOPM_VOL,
  265. 0, 118, 1, output_stage_tlv),
  266. SOC_SINGLE_TLV("LineL Left PGA Bypass Playback Volume",
  267. PGAL_2_LLOPM_VOL, 0, 118, 1, output_stage_tlv),
  268. SOC_SINGLE_TLV("LineR Right PGA Bypass Playback Volume",
  269. PGAR_2_RLOPM_VOL, 0, 118, 1, output_stage_tlv),
  270. SOC_DOUBLE_R_TLV("LineL Line2 Bypass Playback Volume",
  271. LINE2L_2_LLOPM_VOL, LINE2R_2_LLOPM_VOL,
  272. 0, 118, 1, output_stage_tlv),
  273. SOC_DOUBLE_R_TLV("LineR Line2 Bypass Playback Volume",
  274. LINE2L_2_RLOPM_VOL, LINE2R_2_RLOPM_VOL,
  275. 0, 118, 1, output_stage_tlv),
  276. SOC_DOUBLE_R_TLV("Mono DAC Playback Volume",
  277. DACL1_2_MONOLOPM_VOL, DACR1_2_MONOLOPM_VOL,
  278. 0, 118, 1, output_stage_tlv),
  279. SOC_SINGLE("Mono DAC Playback Switch", MONOLOPM_CTRL, 3, 0x01, 0),
  280. SOC_DOUBLE_R_TLV("Mono PGA Bypass Playback Volume",
  281. PGAL_2_MONOLOPM_VOL, PGAR_2_MONOLOPM_VOL,
  282. 0, 118, 1, output_stage_tlv),
  283. SOC_DOUBLE_R_TLV("Mono Line2 Bypass Playback Volume",
  284. LINE2L_2_MONOLOPM_VOL, LINE2R_2_MONOLOPM_VOL,
  285. 0, 118, 1, output_stage_tlv),
  286. SOC_DOUBLE_R_TLV("HP DAC Playback Volume",
  287. DACL1_2_HPLOUT_VOL, DACR1_2_HPROUT_VOL,
  288. 0, 118, 1, output_stage_tlv),
  289. SOC_DOUBLE_R("HP DAC Playback Switch", HPLOUT_CTRL, HPROUT_CTRL, 3,
  290. 0x01, 0),
  291. SOC_DOUBLE_R_TLV("HP Right PGA Bypass Playback Volume",
  292. PGAR_2_HPLOUT_VOL, PGAR_2_HPROUT_VOL,
  293. 0, 118, 1, output_stage_tlv),
  294. SOC_SINGLE_TLV("HPL PGA Bypass Playback Volume",
  295. PGAL_2_HPLOUT_VOL, 0, 118, 1, output_stage_tlv),
  296. SOC_SINGLE_TLV("HPR PGA Bypass Playback Volume",
  297. PGAL_2_HPROUT_VOL, 0, 118, 1, output_stage_tlv),
  298. SOC_DOUBLE_R_TLV("HP Line2 Bypass Playback Volume",
  299. LINE2L_2_HPLOUT_VOL, LINE2R_2_HPROUT_VOL,
  300. 0, 118, 1, output_stage_tlv),
  301. SOC_DOUBLE_R_TLV("HPCOM DAC Playback Volume",
  302. DACL1_2_HPLCOM_VOL, DACR1_2_HPRCOM_VOL,
  303. 0, 118, 1, output_stage_tlv),
  304. SOC_DOUBLE_R("HPCOM DAC Playback Switch", HPLCOM_CTRL, HPRCOM_CTRL, 3,
  305. 0x01, 0),
  306. SOC_SINGLE_TLV("HPLCOM PGA Bypass Playback Volume",
  307. PGAL_2_HPLCOM_VOL, 0, 118, 1, output_stage_tlv),
  308. SOC_SINGLE_TLV("HPRCOM PGA Bypass Playback Volume",
  309. PGAL_2_HPRCOM_VOL, 0, 118, 1, output_stage_tlv),
  310. SOC_DOUBLE_R_TLV("HPCOM Line2 Bypass Playback Volume",
  311. LINE2L_2_HPLCOM_VOL, LINE2R_2_HPRCOM_VOL,
  312. 0, 118, 1, output_stage_tlv),
  313. /*
  314. * Note: enable Automatic input Gain Controller with care. It can
  315. * adjust PGA to max value when ADC is on and will never go back.
  316. */
  317. SOC_DOUBLE_R("AGC Switch", LAGC_CTRL_A, RAGC_CTRL_A, 7, 0x01, 0),
  318. /* Input */
  319. SOC_DOUBLE_R_TLV("PGA Capture Volume", LADC_VOL, RADC_VOL,
  320. 0, 119, 0, adc_tlv),
  321. SOC_DOUBLE_R("PGA Capture Switch", LADC_VOL, RADC_VOL, 7, 0x01, 1),
  322. SOC_ENUM("ADC HPF Cut-off", aic3x_enum[ADC_HPF_ENUM]),
  323. };
  324. /* Left DAC Mux */
  325. static const struct snd_kcontrol_new aic3x_left_dac_mux_controls =
  326. SOC_DAPM_ENUM("Route", aic3x_enum[LDAC_ENUM]);
  327. /* Right DAC Mux */
  328. static const struct snd_kcontrol_new aic3x_right_dac_mux_controls =
  329. SOC_DAPM_ENUM("Route", aic3x_enum[RDAC_ENUM]);
  330. /* Left HPCOM Mux */
  331. static const struct snd_kcontrol_new aic3x_left_hpcom_mux_controls =
  332. SOC_DAPM_ENUM("Route", aic3x_enum[LHPCOM_ENUM]);
  333. /* Right HPCOM Mux */
  334. static const struct snd_kcontrol_new aic3x_right_hpcom_mux_controls =
  335. SOC_DAPM_ENUM("Route", aic3x_enum[RHPCOM_ENUM]);
  336. /* Left DAC_L1 Mixer */
  337. static const struct snd_kcontrol_new aic3x_left_dac_mixer_controls[] = {
  338. SOC_DAPM_SINGLE("LineL Switch", DACL1_2_LLOPM_VOL, 7, 1, 0),
  339. SOC_DAPM_SINGLE("LineR Switch", DACL1_2_RLOPM_VOL, 7, 1, 0),
  340. SOC_DAPM_SINGLE("Mono Switch", DACL1_2_MONOLOPM_VOL, 7, 1, 0),
  341. SOC_DAPM_SINGLE("HP Switch", DACL1_2_HPLOUT_VOL, 7, 1, 0),
  342. SOC_DAPM_SINGLE("HPCOM Switch", DACL1_2_HPLCOM_VOL, 7, 1, 0),
  343. };
  344. /* Right DAC_R1 Mixer */
  345. static const struct snd_kcontrol_new aic3x_right_dac_mixer_controls[] = {
  346. SOC_DAPM_SINGLE("LineL Switch", DACR1_2_LLOPM_VOL, 7, 1, 0),
  347. SOC_DAPM_SINGLE("LineR Switch", DACR1_2_RLOPM_VOL, 7, 1, 0),
  348. SOC_DAPM_SINGLE("Mono Switch", DACR1_2_MONOLOPM_VOL, 7, 1, 0),
  349. SOC_DAPM_SINGLE("HP Switch", DACR1_2_HPROUT_VOL, 7, 1, 0),
  350. SOC_DAPM_SINGLE("HPCOM Switch", DACR1_2_HPRCOM_VOL, 7, 1, 0),
  351. };
  352. /* Left PGA Mixer */
  353. static const struct snd_kcontrol_new aic3x_left_pga_mixer_controls[] = {
  354. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_LADC_CTRL, 3, 1, 1),
  355. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_LADC_CTRL, 3, 1, 1),
  356. SOC_DAPM_SINGLE_AIC3X("Line2L Switch", LINE2L_2_LADC_CTRL, 3, 1, 1),
  357. SOC_DAPM_SINGLE_AIC3X("Mic3L Switch", MIC3LR_2_LADC_CTRL, 4, 1, 1),
  358. SOC_DAPM_SINGLE_AIC3X("Mic3R Switch", MIC3LR_2_LADC_CTRL, 0, 1, 1),
  359. };
  360. /* Right PGA Mixer */
  361. static const struct snd_kcontrol_new aic3x_right_pga_mixer_controls[] = {
  362. SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_RADC_CTRL, 3, 1, 1),
  363. SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_RADC_CTRL, 3, 1, 1),
  364. SOC_DAPM_SINGLE_AIC3X("Line2R Switch", LINE2R_2_RADC_CTRL, 3, 1, 1),
  365. SOC_DAPM_SINGLE_AIC3X("Mic3L Switch", MIC3LR_2_RADC_CTRL, 4, 1, 1),
  366. SOC_DAPM_SINGLE_AIC3X("Mic3R Switch", MIC3LR_2_RADC_CTRL, 0, 1, 1),
  367. };
  368. /* Left Line1 Mux */
  369. static const struct snd_kcontrol_new aic3x_left_line1_mux_controls =
  370. SOC_DAPM_ENUM("Route", aic3x_enum[LINE1L_ENUM]);
  371. /* Right Line1 Mux */
  372. static const struct snd_kcontrol_new aic3x_right_line1_mux_controls =
  373. SOC_DAPM_ENUM("Route", aic3x_enum[LINE1R_ENUM]);
  374. /* Left Line2 Mux */
  375. static const struct snd_kcontrol_new aic3x_left_line2_mux_controls =
  376. SOC_DAPM_ENUM("Route", aic3x_enum[LINE2L_ENUM]);
  377. /* Right Line2 Mux */
  378. static const struct snd_kcontrol_new aic3x_right_line2_mux_controls =
  379. SOC_DAPM_ENUM("Route", aic3x_enum[LINE2R_ENUM]);
  380. /* Left PGA Bypass Mixer */
  381. static const struct snd_kcontrol_new aic3x_left_pga_bp_mixer_controls[] = {
  382. SOC_DAPM_SINGLE("LineL Switch", PGAL_2_LLOPM_VOL, 7, 1, 0),
  383. SOC_DAPM_SINGLE("LineR Switch", PGAL_2_RLOPM_VOL, 7, 1, 0),
  384. SOC_DAPM_SINGLE("Mono Switch", PGAL_2_MONOLOPM_VOL, 7, 1, 0),
  385. SOC_DAPM_SINGLE("HPL Switch", PGAL_2_HPLOUT_VOL, 7, 1, 0),
  386. SOC_DAPM_SINGLE("HPR Switch", PGAL_2_HPROUT_VOL, 7, 1, 0),
  387. SOC_DAPM_SINGLE("HPLCOM Switch", PGAL_2_HPLCOM_VOL, 7, 1, 0),
  388. SOC_DAPM_SINGLE("HPRCOM Switch", PGAL_2_HPRCOM_VOL, 7, 1, 0),
  389. };
  390. /* Right PGA Bypass Mixer */
  391. static const struct snd_kcontrol_new aic3x_right_pga_bp_mixer_controls[] = {
  392. SOC_DAPM_SINGLE("LineL Switch", PGAR_2_LLOPM_VOL, 7, 1, 0),
  393. SOC_DAPM_SINGLE("LineR Switch", PGAR_2_RLOPM_VOL, 7, 1, 0),
  394. SOC_DAPM_SINGLE("Mono Switch", PGAR_2_MONOLOPM_VOL, 7, 1, 0),
  395. SOC_DAPM_SINGLE("HPL Switch", PGAR_2_HPLOUT_VOL, 7, 1, 0),
  396. SOC_DAPM_SINGLE("HPR Switch", PGAR_2_HPROUT_VOL, 7, 1, 0),
  397. SOC_DAPM_SINGLE("HPLCOM Switch", PGAR_2_HPLCOM_VOL, 7, 1, 0),
  398. SOC_DAPM_SINGLE("HPRCOM Switch", PGAR_2_HPRCOM_VOL, 7, 1, 0),
  399. };
  400. /* Left Line2 Bypass Mixer */
  401. static const struct snd_kcontrol_new aic3x_left_line2_bp_mixer_controls[] = {
  402. SOC_DAPM_SINGLE("LineL Switch", LINE2L_2_LLOPM_VOL, 7, 1, 0),
  403. SOC_DAPM_SINGLE("LineR Switch", LINE2L_2_RLOPM_VOL, 7, 1, 0),
  404. SOC_DAPM_SINGLE("Mono Switch", LINE2L_2_MONOLOPM_VOL, 7, 1, 0),
  405. SOC_DAPM_SINGLE("HP Switch", LINE2L_2_HPLOUT_VOL, 7, 1, 0),
  406. SOC_DAPM_SINGLE("HPLCOM Switch", LINE2L_2_HPLCOM_VOL, 7, 1, 0),
  407. };
  408. /* Right Line2 Bypass Mixer */
  409. static const struct snd_kcontrol_new aic3x_right_line2_bp_mixer_controls[] = {
  410. SOC_DAPM_SINGLE("LineL Switch", LINE2R_2_LLOPM_VOL, 7, 1, 0),
  411. SOC_DAPM_SINGLE("LineR Switch", LINE2R_2_RLOPM_VOL, 7, 1, 0),
  412. SOC_DAPM_SINGLE("Mono Switch", LINE2R_2_MONOLOPM_VOL, 7, 1, 0),
  413. SOC_DAPM_SINGLE("HP Switch", LINE2R_2_HPROUT_VOL, 7, 1, 0),
  414. SOC_DAPM_SINGLE("HPRCOM Switch", LINE2R_2_HPRCOM_VOL, 7, 1, 0),
  415. };
  416. static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
  417. /* Left DAC to Left Outputs */
  418. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", DAC_PWR, 7, 0),
  419. SND_SOC_DAPM_MUX("Left DAC Mux", SND_SOC_NOPM, 0, 0,
  420. &aic3x_left_dac_mux_controls),
  421. SND_SOC_DAPM_MIXER("Left DAC_L1 Mixer", SND_SOC_NOPM, 0, 0,
  422. &aic3x_left_dac_mixer_controls[0],
  423. ARRAY_SIZE(aic3x_left_dac_mixer_controls)),
  424. SND_SOC_DAPM_MUX("Left HPCOM Mux", SND_SOC_NOPM, 0, 0,
  425. &aic3x_left_hpcom_mux_controls),
  426. SND_SOC_DAPM_PGA("Left Line Out", LLOPM_CTRL, 0, 0, NULL, 0),
  427. SND_SOC_DAPM_PGA("Left HP Out", HPLOUT_CTRL, 0, 0, NULL, 0),
  428. SND_SOC_DAPM_PGA("Left HP Com", HPLCOM_CTRL, 0, 0, NULL, 0),
  429. /* Right DAC to Right Outputs */
  430. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", DAC_PWR, 6, 0),
  431. SND_SOC_DAPM_MUX("Right DAC Mux", SND_SOC_NOPM, 0, 0,
  432. &aic3x_right_dac_mux_controls),
  433. SND_SOC_DAPM_MIXER("Right DAC_R1 Mixer", SND_SOC_NOPM, 0, 0,
  434. &aic3x_right_dac_mixer_controls[0],
  435. ARRAY_SIZE(aic3x_right_dac_mixer_controls)),
  436. SND_SOC_DAPM_MUX("Right HPCOM Mux", SND_SOC_NOPM, 0, 0,
  437. &aic3x_right_hpcom_mux_controls),
  438. SND_SOC_DAPM_PGA("Right Line Out", RLOPM_CTRL, 0, 0, NULL, 0),
  439. SND_SOC_DAPM_PGA("Right HP Out", HPROUT_CTRL, 0, 0, NULL, 0),
  440. SND_SOC_DAPM_PGA("Right HP Com", HPRCOM_CTRL, 0, 0, NULL, 0),
  441. /* Mono Output */
  442. SND_SOC_DAPM_PGA("Mono Out", MONOLOPM_CTRL, 0, 0, NULL, 0),
  443. /* Inputs to Left ADC */
  444. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", LINE1L_2_LADC_CTRL, 2, 0),
  445. SND_SOC_DAPM_MIXER("Left PGA Mixer", SND_SOC_NOPM, 0, 0,
  446. &aic3x_left_pga_mixer_controls[0],
  447. ARRAY_SIZE(aic3x_left_pga_mixer_controls)),
  448. SND_SOC_DAPM_MUX("Left Line1L Mux", SND_SOC_NOPM, 0, 0,
  449. &aic3x_left_line1_mux_controls),
  450. SND_SOC_DAPM_MUX("Left Line1R Mux", SND_SOC_NOPM, 0, 0,
  451. &aic3x_left_line1_mux_controls),
  452. SND_SOC_DAPM_MUX("Left Line2L Mux", SND_SOC_NOPM, 0, 0,
  453. &aic3x_left_line2_mux_controls),
  454. /* Inputs to Right ADC */
  455. SND_SOC_DAPM_ADC("Right ADC", "Right Capture",
  456. LINE1R_2_RADC_CTRL, 2, 0),
  457. SND_SOC_DAPM_MIXER("Right PGA Mixer", SND_SOC_NOPM, 0, 0,
  458. &aic3x_right_pga_mixer_controls[0],
  459. ARRAY_SIZE(aic3x_right_pga_mixer_controls)),
  460. SND_SOC_DAPM_MUX("Right Line1L Mux", SND_SOC_NOPM, 0, 0,
  461. &aic3x_right_line1_mux_controls),
  462. SND_SOC_DAPM_MUX("Right Line1R Mux", SND_SOC_NOPM, 0, 0,
  463. &aic3x_right_line1_mux_controls),
  464. SND_SOC_DAPM_MUX("Right Line2R Mux", SND_SOC_NOPM, 0, 0,
  465. &aic3x_right_line2_mux_controls),
  466. /*
  467. * Not a real mic bias widget but similar function. This is for dynamic
  468. * control of GPIO1 digital mic modulator clock output function when
  469. * using digital mic.
  470. */
  471. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "GPIO1 dmic modclk",
  472. AIC3X_GPIO1_REG, 4, 0xf,
  473. AIC3X_GPIO1_FUNC_DIGITAL_MIC_MODCLK,
  474. AIC3X_GPIO1_FUNC_DISABLED),
  475. /*
  476. * Also similar function like mic bias. Selects digital mic with
  477. * configurable oversampling rate instead of ADC converter.
  478. */
  479. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 128",
  480. AIC3X_ASD_INTF_CTRLA, 0, 3, 1, 0),
  481. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 64",
  482. AIC3X_ASD_INTF_CTRLA, 0, 3, 2, 0),
  483. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 32",
  484. AIC3X_ASD_INTF_CTRLA, 0, 3, 3, 0),
  485. /* Mic Bias */
  486. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias 2V",
  487. MICBIAS_CTRL, 6, 3, 1, 0),
  488. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias 2.5V",
  489. MICBIAS_CTRL, 6, 3, 2, 0),
  490. SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias AVDD",
  491. MICBIAS_CTRL, 6, 3, 3, 0),
  492. /* Left PGA to Left Output bypass */
  493. SND_SOC_DAPM_MIXER("Left PGA Bypass Mixer", SND_SOC_NOPM, 0, 0,
  494. &aic3x_left_pga_bp_mixer_controls[0],
  495. ARRAY_SIZE(aic3x_left_pga_bp_mixer_controls)),
  496. /* Right PGA to Right Output bypass */
  497. SND_SOC_DAPM_MIXER("Right PGA Bypass Mixer", SND_SOC_NOPM, 0, 0,
  498. &aic3x_right_pga_bp_mixer_controls[0],
  499. ARRAY_SIZE(aic3x_right_pga_bp_mixer_controls)),
  500. /* Left Line2 to Left Output bypass */
  501. SND_SOC_DAPM_MIXER("Left Line2 Bypass Mixer", SND_SOC_NOPM, 0, 0,
  502. &aic3x_left_line2_bp_mixer_controls[0],
  503. ARRAY_SIZE(aic3x_left_line2_bp_mixer_controls)),
  504. /* Right Line2 to Right Output bypass */
  505. SND_SOC_DAPM_MIXER("Right Line2 Bypass Mixer", SND_SOC_NOPM, 0, 0,
  506. &aic3x_right_line2_bp_mixer_controls[0],
  507. ARRAY_SIZE(aic3x_right_line2_bp_mixer_controls)),
  508. SND_SOC_DAPM_OUTPUT("LLOUT"),
  509. SND_SOC_DAPM_OUTPUT("RLOUT"),
  510. SND_SOC_DAPM_OUTPUT("MONO_LOUT"),
  511. SND_SOC_DAPM_OUTPUT("HPLOUT"),
  512. SND_SOC_DAPM_OUTPUT("HPROUT"),
  513. SND_SOC_DAPM_OUTPUT("HPLCOM"),
  514. SND_SOC_DAPM_OUTPUT("HPRCOM"),
  515. SND_SOC_DAPM_INPUT("MIC3L"),
  516. SND_SOC_DAPM_INPUT("MIC3R"),
  517. SND_SOC_DAPM_INPUT("LINE1L"),
  518. SND_SOC_DAPM_INPUT("LINE1R"),
  519. SND_SOC_DAPM_INPUT("LINE2L"),
  520. SND_SOC_DAPM_INPUT("LINE2R"),
  521. };
  522. static const struct snd_soc_dapm_route intercon[] = {
  523. /* Left Output */
  524. {"Left DAC Mux", "DAC_L1", "Left DAC"},
  525. {"Left DAC Mux", "DAC_L2", "Left DAC"},
  526. {"Left DAC Mux", "DAC_L3", "Left DAC"},
  527. {"Left DAC_L1 Mixer", "LineL Switch", "Left DAC Mux"},
  528. {"Left DAC_L1 Mixer", "LineR Switch", "Left DAC Mux"},
  529. {"Left DAC_L1 Mixer", "Mono Switch", "Left DAC Mux"},
  530. {"Left DAC_L1 Mixer", "HP Switch", "Left DAC Mux"},
  531. {"Left DAC_L1 Mixer", "HPCOM Switch", "Left DAC Mux"},
  532. {"Left Line Out", NULL, "Left DAC Mux"},
  533. {"Left HP Out", NULL, "Left DAC Mux"},
  534. {"Left HPCOM Mux", "differential of HPLOUT", "Left DAC_L1 Mixer"},
  535. {"Left HPCOM Mux", "constant VCM", "Left DAC_L1 Mixer"},
  536. {"Left HPCOM Mux", "single-ended", "Left DAC_L1 Mixer"},
  537. {"Left Line Out", NULL, "Left DAC_L1 Mixer"},
  538. {"Mono Out", NULL, "Left DAC_L1 Mixer"},
  539. {"Left HP Out", NULL, "Left DAC_L1 Mixer"},
  540. {"Left HP Com", NULL, "Left HPCOM Mux"},
  541. {"LLOUT", NULL, "Left Line Out"},
  542. {"LLOUT", NULL, "Left Line Out"},
  543. {"HPLOUT", NULL, "Left HP Out"},
  544. {"HPLCOM", NULL, "Left HP Com"},
  545. /* Right Output */
  546. {"Right DAC Mux", "DAC_R1", "Right DAC"},
  547. {"Right DAC Mux", "DAC_R2", "Right DAC"},
  548. {"Right DAC Mux", "DAC_R3", "Right DAC"},
  549. {"Right DAC_R1 Mixer", "LineL Switch", "Right DAC Mux"},
  550. {"Right DAC_R1 Mixer", "LineR Switch", "Right DAC Mux"},
  551. {"Right DAC_R1 Mixer", "Mono Switch", "Right DAC Mux"},
  552. {"Right DAC_R1 Mixer", "HP Switch", "Right DAC Mux"},
  553. {"Right DAC_R1 Mixer", "HPCOM Switch", "Right DAC Mux"},
  554. {"Right Line Out", NULL, "Right DAC Mux"},
  555. {"Right HP Out", NULL, "Right DAC Mux"},
  556. {"Right HPCOM Mux", "differential of HPROUT", "Right DAC_R1 Mixer"},
  557. {"Right HPCOM Mux", "constant VCM", "Right DAC_R1 Mixer"},
  558. {"Right HPCOM Mux", "single-ended", "Right DAC_R1 Mixer"},
  559. {"Right HPCOM Mux", "differential of HPLCOM", "Right DAC_R1 Mixer"},
  560. {"Right HPCOM Mux", "external feedback", "Right DAC_R1 Mixer"},
  561. {"Right Line Out", NULL, "Right DAC_R1 Mixer"},
  562. {"Mono Out", NULL, "Right DAC_R1 Mixer"},
  563. {"Right HP Out", NULL, "Right DAC_R1 Mixer"},
  564. {"Right HP Com", NULL, "Right HPCOM Mux"},
  565. {"RLOUT", NULL, "Right Line Out"},
  566. {"RLOUT", NULL, "Right Line Out"},
  567. {"HPROUT", NULL, "Right HP Out"},
  568. {"HPRCOM", NULL, "Right HP Com"},
  569. /* Mono Output */
  570. {"MONO_LOUT", NULL, "Mono Out"},
  571. {"MONO_LOUT", NULL, "Mono Out"},
  572. /* Left Input */
  573. {"Left Line1L Mux", "single-ended", "LINE1L"},
  574. {"Left Line1L Mux", "differential", "LINE1L"},
  575. {"Left Line2L Mux", "single-ended", "LINE2L"},
  576. {"Left Line2L Mux", "differential", "LINE2L"},
  577. {"Left PGA Mixer", "Line1L Switch", "Left Line1L Mux"},
  578. {"Left PGA Mixer", "Line1R Switch", "Left Line1R Mux"},
  579. {"Left PGA Mixer", "Line2L Switch", "Left Line2L Mux"},
  580. {"Left PGA Mixer", "Mic3L Switch", "MIC3L"},
  581. {"Left PGA Mixer", "Mic3R Switch", "MIC3R"},
  582. {"Left ADC", NULL, "Left PGA Mixer"},
  583. {"Left ADC", NULL, "GPIO1 dmic modclk"},
  584. /* Right Input */
  585. {"Right Line1R Mux", "single-ended", "LINE1R"},
  586. {"Right Line1R Mux", "differential", "LINE1R"},
  587. {"Right Line2R Mux", "single-ended", "LINE2R"},
  588. {"Right Line2R Mux", "differential", "LINE2R"},
  589. {"Right PGA Mixer", "Line1L Switch", "Right Line1L Mux"},
  590. {"Right PGA Mixer", "Line1R Switch", "Right Line1R Mux"},
  591. {"Right PGA Mixer", "Line2R Switch", "Right Line2R Mux"},
  592. {"Right PGA Mixer", "Mic3L Switch", "MIC3L"},
  593. {"Right PGA Mixer", "Mic3R Switch", "MIC3R"},
  594. {"Right ADC", NULL, "Right PGA Mixer"},
  595. {"Right ADC", NULL, "GPIO1 dmic modclk"},
  596. /* Left PGA Bypass */
  597. {"Left PGA Bypass Mixer", "LineL Switch", "Left PGA Mixer"},
  598. {"Left PGA Bypass Mixer", "LineR Switch", "Left PGA Mixer"},
  599. {"Left PGA Bypass Mixer", "Mono Switch", "Left PGA Mixer"},
  600. {"Left PGA Bypass Mixer", "HPL Switch", "Left PGA Mixer"},
  601. {"Left PGA Bypass Mixer", "HPR Switch", "Left PGA Mixer"},
  602. {"Left PGA Bypass Mixer", "HPLCOM Switch", "Left PGA Mixer"},
  603. {"Left PGA Bypass Mixer", "HPRCOM Switch", "Left PGA Mixer"},
  604. {"Left HPCOM Mux", "differential of HPLOUT", "Left PGA Bypass Mixer"},
  605. {"Left HPCOM Mux", "constant VCM", "Left PGA Bypass Mixer"},
  606. {"Left HPCOM Mux", "single-ended", "Left PGA Bypass Mixer"},
  607. {"Left Line Out", NULL, "Left PGA Bypass Mixer"},
  608. {"Mono Out", NULL, "Left PGA Bypass Mixer"},
  609. {"Left HP Out", NULL, "Left PGA Bypass Mixer"},
  610. /* Right PGA Bypass */
  611. {"Right PGA Bypass Mixer", "LineL Switch", "Right PGA Mixer"},
  612. {"Right PGA Bypass Mixer", "LineR Switch", "Right PGA Mixer"},
  613. {"Right PGA Bypass Mixer", "Mono Switch", "Right PGA Mixer"},
  614. {"Right PGA Bypass Mixer", "HPL Switch", "Right PGA Mixer"},
  615. {"Right PGA Bypass Mixer", "HPR Switch", "Right PGA Mixer"},
  616. {"Right PGA Bypass Mixer", "HPLCOM Switch", "Right PGA Mixer"},
  617. {"Right PGA Bypass Mixer", "HPRCOM Switch", "Right PGA Mixer"},
  618. {"Right HPCOM Mux", "differential of HPROUT", "Right PGA Bypass Mixer"},
  619. {"Right HPCOM Mux", "constant VCM", "Right PGA Bypass Mixer"},
  620. {"Right HPCOM Mux", "single-ended", "Right PGA Bypass Mixer"},
  621. {"Right HPCOM Mux", "differential of HPLCOM", "Right PGA Bypass Mixer"},
  622. {"Right HPCOM Mux", "external feedback", "Right PGA Bypass Mixer"},
  623. {"Right Line Out", NULL, "Right PGA Bypass Mixer"},
  624. {"Mono Out", NULL, "Right PGA Bypass Mixer"},
  625. {"Right HP Out", NULL, "Right PGA Bypass Mixer"},
  626. /* Left Line2 Bypass */
  627. {"Left Line2 Bypass Mixer", "LineL Switch", "Left Line2L Mux"},
  628. {"Left Line2 Bypass Mixer", "LineR Switch", "Left Line2L Mux"},
  629. {"Left Line2 Bypass Mixer", "Mono Switch", "Left Line2L Mux"},
  630. {"Left Line2 Bypass Mixer", "HP Switch", "Left Line2L Mux"},
  631. {"Left Line2 Bypass Mixer", "HPLCOM Switch", "Left Line2L Mux"},
  632. {"Left HPCOM Mux", "differential of HPLOUT", "Left Line2 Bypass Mixer"},
  633. {"Left HPCOM Mux", "constant VCM", "Left Line2 Bypass Mixer"},
  634. {"Left HPCOM Mux", "single-ended", "Left Line2 Bypass Mixer"},
  635. {"Left Line Out", NULL, "Left Line2 Bypass Mixer"},
  636. {"Mono Out", NULL, "Left Line2 Bypass Mixer"},
  637. {"Left HP Out", NULL, "Left Line2 Bypass Mixer"},
  638. /* Right Line2 Bypass */
  639. {"Right Line2 Bypass Mixer", "LineL Switch", "Right Line2R Mux"},
  640. {"Right Line2 Bypass Mixer", "LineR Switch", "Right Line2R Mux"},
  641. {"Right Line2 Bypass Mixer", "Mono Switch", "Right Line2R Mux"},
  642. {"Right Line2 Bypass Mixer", "HP Switch", "Right Line2R Mux"},
  643. {"Right Line2 Bypass Mixer", "HPRCOM Switch", "Right Line2R Mux"},
  644. {"Right HPCOM Mux", "differential of HPROUT", "Right Line2 Bypass Mixer"},
  645. {"Right HPCOM Mux", "constant VCM", "Right Line2 Bypass Mixer"},
  646. {"Right HPCOM Mux", "single-ended", "Right Line2 Bypass Mixer"},
  647. {"Right HPCOM Mux", "differential of HPLCOM", "Right Line2 Bypass Mixer"},
  648. {"Right HPCOM Mux", "external feedback", "Right Line2 Bypass Mixer"},
  649. {"Right Line Out", NULL, "Right Line2 Bypass Mixer"},
  650. {"Mono Out", NULL, "Right Line2 Bypass Mixer"},
  651. {"Right HP Out", NULL, "Right Line2 Bypass Mixer"},
  652. /*
  653. * Logical path between digital mic enable and GPIO1 modulator clock
  654. * output function
  655. */
  656. {"GPIO1 dmic modclk", NULL, "DMic Rate 128"},
  657. {"GPIO1 dmic modclk", NULL, "DMic Rate 64"},
  658. {"GPIO1 dmic modclk", NULL, "DMic Rate 32"},
  659. };
  660. static int aic3x_add_widgets(struct snd_soc_codec *codec)
  661. {
  662. snd_soc_dapm_new_controls(codec, aic3x_dapm_widgets,
  663. ARRAY_SIZE(aic3x_dapm_widgets));
  664. /* set up audio path interconnects */
  665. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  666. return 0;
  667. }
  668. static int aic3x_hw_params(struct snd_pcm_substream *substream,
  669. struct snd_pcm_hw_params *params,
  670. struct snd_soc_dai *dai)
  671. {
  672. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  673. struct snd_soc_device *socdev = rtd->socdev;
  674. struct snd_soc_codec *codec = socdev->card->codec;
  675. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  676. int codec_clk = 0, bypass_pll = 0, fsref, last_clk = 0;
  677. u8 data, j, r, p, pll_q, pll_p = 1, pll_r = 1, pll_j = 1;
  678. u16 d, pll_d = 1;
  679. u8 reg;
  680. int clk;
  681. /* select data word length */
  682. data =
  683. aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLB) & (~(0x3 << 4));
  684. switch (params_format(params)) {
  685. case SNDRV_PCM_FORMAT_S16_LE:
  686. break;
  687. case SNDRV_PCM_FORMAT_S20_3LE:
  688. data |= (0x01 << 4);
  689. break;
  690. case SNDRV_PCM_FORMAT_S24_LE:
  691. data |= (0x02 << 4);
  692. break;
  693. case SNDRV_PCM_FORMAT_S32_LE:
  694. data |= (0x03 << 4);
  695. break;
  696. }
  697. aic3x_write(codec, AIC3X_ASD_INTF_CTRLB, data);
  698. /* Fsref can be 44100 or 48000 */
  699. fsref = (params_rate(params) % 11025 == 0) ? 44100 : 48000;
  700. /* Try to find a value for Q which allows us to bypass the PLL and
  701. * generate CODEC_CLK directly. */
  702. for (pll_q = 2; pll_q < 18; pll_q++)
  703. if (aic3x->sysclk / (128 * pll_q) == fsref) {
  704. bypass_pll = 1;
  705. break;
  706. }
  707. if (bypass_pll) {
  708. pll_q &= 0xf;
  709. aic3x_write(codec, AIC3X_PLL_PROGA_REG, pll_q << PLLQ_SHIFT);
  710. aic3x_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_CLKDIV);
  711. /* disable PLL if it is bypassed */
  712. reg = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
  713. aic3x_write(codec, AIC3X_PLL_PROGA_REG, reg & ~PLL_ENABLE);
  714. } else {
  715. aic3x_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_PLLDIV);
  716. /* enable PLL when it is used */
  717. reg = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
  718. aic3x_write(codec, AIC3X_PLL_PROGA_REG, reg | PLL_ENABLE);
  719. }
  720. /* Route Left DAC to left channel input and
  721. * right DAC to right channel input */
  722. data = (LDAC2LCH | RDAC2RCH);
  723. data |= (fsref == 44100) ? FSREF_44100 : FSREF_48000;
  724. if (params_rate(params) >= 64000)
  725. data |= DUAL_RATE_MODE;
  726. aic3x_write(codec, AIC3X_CODEC_DATAPATH_REG, data);
  727. /* codec sample rate select */
  728. data = (fsref * 20) / params_rate(params);
  729. if (params_rate(params) < 64000)
  730. data /= 2;
  731. data /= 5;
  732. data -= 2;
  733. data |= (data << 4);
  734. aic3x_write(codec, AIC3X_SAMPLE_RATE_SEL_REG, data);
  735. if (bypass_pll)
  736. return 0;
  737. /* Use PLL, compute apropriate setup for j, d, r and p, the closest
  738. * one wins the game. Try with d==0 first, next with d!=0.
  739. * Constraints for j are according to the datasheet.
  740. * The sysclk is divided by 1000 to prevent integer overflows.
  741. */
  742. codec_clk = (2048 * fsref) / (aic3x->sysclk / 1000);
  743. for (r = 1; r <= 16; r++)
  744. for (p = 1; p <= 8; p++) {
  745. for (j = 4; j <= 55; j++) {
  746. /* This is actually 1000*((j+(d/10000))*r)/p
  747. * The term had to be converted to get
  748. * rid of the division by 10000; d = 0 here
  749. */
  750. int tmp_clk = (1000 * j * r) / p;
  751. /* Check whether this values get closer than
  752. * the best ones we had before
  753. */
  754. if (abs(codec_clk - tmp_clk) <
  755. abs(codec_clk - last_clk)) {
  756. pll_j = j; pll_d = 0;
  757. pll_r = r; pll_p = p;
  758. last_clk = tmp_clk;
  759. }
  760. /* Early exit for exact matches */
  761. if (tmp_clk == codec_clk)
  762. goto found;
  763. }
  764. }
  765. /* try with d != 0 */
  766. for (p = 1; p <= 8; p++) {
  767. j = codec_clk * p / 1000;
  768. if (j < 4 || j > 11)
  769. continue;
  770. /* do not use codec_clk here since we'd loose precision */
  771. d = ((2048 * p * fsref) - j * aic3x->sysclk)
  772. * 100 / (aic3x->sysclk/100);
  773. clk = (10000 * j + d) / (10 * p);
  774. /* check whether this values get closer than the best
  775. * ones we had before */
  776. if (abs(codec_clk - clk) < abs(codec_clk - last_clk)) {
  777. pll_j = j; pll_d = d; pll_r = 1; pll_p = p;
  778. last_clk = clk;
  779. }
  780. /* Early exit for exact matches */
  781. if (clk == codec_clk)
  782. goto found;
  783. }
  784. if (last_clk == 0) {
  785. printk(KERN_ERR "%s(): unable to setup PLL\n", __func__);
  786. return -EINVAL;
  787. }
  788. found:
  789. data = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
  790. aic3x_write(codec, AIC3X_PLL_PROGA_REG, data | (pll_p << PLLP_SHIFT));
  791. aic3x_write(codec, AIC3X_OVRF_STATUS_AND_PLLR_REG, pll_r << PLLR_SHIFT);
  792. aic3x_write(codec, AIC3X_PLL_PROGB_REG, pll_j << PLLJ_SHIFT);
  793. aic3x_write(codec, AIC3X_PLL_PROGC_REG, (pll_d >> 6) << PLLD_MSB_SHIFT);
  794. aic3x_write(codec, AIC3X_PLL_PROGD_REG,
  795. (pll_d & 0x3F) << PLLD_LSB_SHIFT);
  796. return 0;
  797. }
  798. static int aic3x_mute(struct snd_soc_dai *dai, int mute)
  799. {
  800. struct snd_soc_codec *codec = dai->codec;
  801. u8 ldac_reg = aic3x_read_reg_cache(codec, LDAC_VOL) & ~MUTE_ON;
  802. u8 rdac_reg = aic3x_read_reg_cache(codec, RDAC_VOL) & ~MUTE_ON;
  803. if (mute) {
  804. aic3x_write(codec, LDAC_VOL, ldac_reg | MUTE_ON);
  805. aic3x_write(codec, RDAC_VOL, rdac_reg | MUTE_ON);
  806. } else {
  807. aic3x_write(codec, LDAC_VOL, ldac_reg);
  808. aic3x_write(codec, RDAC_VOL, rdac_reg);
  809. }
  810. return 0;
  811. }
  812. static int aic3x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  813. int clk_id, unsigned int freq, int dir)
  814. {
  815. struct snd_soc_codec *codec = codec_dai->codec;
  816. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  817. aic3x->sysclk = freq;
  818. return 0;
  819. }
  820. static int aic3x_set_dai_fmt(struct snd_soc_dai *codec_dai,
  821. unsigned int fmt)
  822. {
  823. struct snd_soc_codec *codec = codec_dai->codec;
  824. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  825. u8 iface_areg, iface_breg;
  826. int delay = 0;
  827. iface_areg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLA) & 0x3f;
  828. iface_breg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLB) & 0x3f;
  829. /* set master/slave audio interface */
  830. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  831. case SND_SOC_DAIFMT_CBM_CFM:
  832. aic3x->master = 1;
  833. iface_areg |= BIT_CLK_MASTER | WORD_CLK_MASTER;
  834. break;
  835. case SND_SOC_DAIFMT_CBS_CFS:
  836. aic3x->master = 0;
  837. break;
  838. default:
  839. return -EINVAL;
  840. }
  841. /*
  842. * match both interface format and signal polarities since they
  843. * are fixed
  844. */
  845. switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
  846. SND_SOC_DAIFMT_INV_MASK)) {
  847. case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF):
  848. break;
  849. case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF):
  850. delay = 1;
  851. case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF):
  852. iface_breg |= (0x01 << 6);
  853. break;
  854. case (SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF):
  855. iface_breg |= (0x02 << 6);
  856. break;
  857. case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF):
  858. iface_breg |= (0x03 << 6);
  859. break;
  860. default:
  861. return -EINVAL;
  862. }
  863. /* set iface */
  864. aic3x_write(codec, AIC3X_ASD_INTF_CTRLA, iface_areg);
  865. aic3x_write(codec, AIC3X_ASD_INTF_CTRLB, iface_breg);
  866. aic3x_write(codec, AIC3X_ASD_INTF_CTRLC, delay);
  867. return 0;
  868. }
  869. static int aic3x_set_bias_level(struct snd_soc_codec *codec,
  870. enum snd_soc_bias_level level)
  871. {
  872. struct aic3x_priv *aic3x = snd_soc_codec_get_drvdata(codec);
  873. u8 reg;
  874. switch (level) {
  875. case SND_SOC_BIAS_ON:
  876. break;
  877. case SND_SOC_BIAS_PREPARE:
  878. if (aic3x->master) {
  879. /* enable pll */
  880. reg = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
  881. aic3x_write(codec, AIC3X_PLL_PROGA_REG,
  882. reg | PLL_ENABLE);
  883. }
  884. break;
  885. case SND_SOC_BIAS_STANDBY:
  886. /* fall through and disable pll */
  887. case SND_SOC_BIAS_OFF:
  888. if (aic3x->master) {
  889. /* disable pll */
  890. reg = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
  891. aic3x_write(codec, AIC3X_PLL_PROGA_REG,
  892. reg & ~PLL_ENABLE);
  893. }
  894. break;
  895. }
  896. codec->bias_level = level;
  897. return 0;
  898. }
  899. void aic3x_set_gpio(struct snd_soc_codec *codec, int gpio, int state)
  900. {
  901. u8 reg = gpio ? AIC3X_GPIO2_REG : AIC3X_GPIO1_REG;
  902. u8 bit = gpio ? 3: 0;
  903. u8 val = aic3x_read_reg_cache(codec, reg) & ~(1 << bit);
  904. aic3x_write(codec, reg, val | (!!state << bit));
  905. }
  906. EXPORT_SYMBOL_GPL(aic3x_set_gpio);
  907. int aic3x_get_gpio(struct snd_soc_codec *codec, int gpio)
  908. {
  909. u8 reg = gpio ? AIC3X_GPIO2_REG : AIC3X_GPIO1_REG;
  910. u8 val, bit = gpio ? 2: 1;
  911. aic3x_read(codec, reg, &val);
  912. return (val >> bit) & 1;
  913. }
  914. EXPORT_SYMBOL_GPL(aic3x_get_gpio);
  915. void aic3x_set_headset_detection(struct snd_soc_codec *codec, int detect,
  916. int headset_debounce, int button_debounce)
  917. {
  918. u8 val;
  919. val = ((detect & AIC3X_HEADSET_DETECT_MASK)
  920. << AIC3X_HEADSET_DETECT_SHIFT) |
  921. ((headset_debounce & AIC3X_HEADSET_DEBOUNCE_MASK)
  922. << AIC3X_HEADSET_DEBOUNCE_SHIFT) |
  923. ((button_debounce & AIC3X_BUTTON_DEBOUNCE_MASK)
  924. << AIC3X_BUTTON_DEBOUNCE_SHIFT);
  925. if (detect & AIC3X_HEADSET_DETECT_MASK)
  926. val |= AIC3X_HEADSET_DETECT_ENABLED;
  927. aic3x_write(codec, AIC3X_HEADSET_DETECT_CTRL_A, val);
  928. }
  929. EXPORT_SYMBOL_GPL(aic3x_set_headset_detection);
  930. int aic3x_headset_detected(struct snd_soc_codec *codec)
  931. {
  932. u8 val;
  933. aic3x_read(codec, AIC3X_HEADSET_DETECT_CTRL_B, &val);
  934. return (val >> 4) & 1;
  935. }
  936. EXPORT_SYMBOL_GPL(aic3x_headset_detected);
  937. int aic3x_button_pressed(struct snd_soc_codec *codec)
  938. {
  939. u8 val;
  940. aic3x_read(codec, AIC3X_HEADSET_DETECT_CTRL_B, &val);
  941. return (val >> 5) & 1;
  942. }
  943. EXPORT_SYMBOL_GPL(aic3x_button_pressed);
  944. #define AIC3X_RATES SNDRV_PCM_RATE_8000_96000
  945. #define AIC3X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  946. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  947. static struct snd_soc_dai_ops aic3x_dai_ops = {
  948. .hw_params = aic3x_hw_params,
  949. .digital_mute = aic3x_mute,
  950. .set_sysclk = aic3x_set_dai_sysclk,
  951. .set_fmt = aic3x_set_dai_fmt,
  952. };
  953. struct snd_soc_dai aic3x_dai = {
  954. .name = "tlv320aic3x",
  955. .playback = {
  956. .stream_name = "Playback",
  957. .channels_min = 1,
  958. .channels_max = 2,
  959. .rates = AIC3X_RATES,
  960. .formats = AIC3X_FORMATS,},
  961. .capture = {
  962. .stream_name = "Capture",
  963. .channels_min = 1,
  964. .channels_max = 2,
  965. .rates = AIC3X_RATES,
  966. .formats = AIC3X_FORMATS,},
  967. .ops = &aic3x_dai_ops,
  968. };
  969. EXPORT_SYMBOL_GPL(aic3x_dai);
  970. static int aic3x_suspend(struct platform_device *pdev, pm_message_t state)
  971. {
  972. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  973. struct snd_soc_codec *codec = socdev->card->codec;
  974. aic3x_set_bias_level(codec, SND_SOC_BIAS_OFF);
  975. return 0;
  976. }
  977. static int aic3x_resume(struct platform_device *pdev)
  978. {
  979. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  980. struct snd_soc_codec *codec = socdev->card->codec;
  981. int i;
  982. u8 data[2];
  983. u8 *cache = codec->reg_cache;
  984. /* Sync reg_cache with the hardware */
  985. for (i = 0; i < ARRAY_SIZE(aic3x_reg); i++) {
  986. data[0] = i;
  987. data[1] = cache[i];
  988. codec->hw_write(codec->control_data, data, 2);
  989. }
  990. aic3x_set_bias_level(codec, codec->suspend_bias_level);
  991. return 0;
  992. }
  993. /*
  994. * initialise the AIC3X driver
  995. * register the mixer and dsp interfaces with the kernel
  996. */
  997. static int aic3x_init(struct snd_soc_codec *codec)
  998. {
  999. int reg;
  1000. mutex_init(&codec->mutex);
  1001. INIT_LIST_HEAD(&codec->dapm_widgets);
  1002. INIT_LIST_HEAD(&codec->dapm_paths);
  1003. codec->name = "tlv320aic3x";
  1004. codec->owner = THIS_MODULE;
  1005. codec->read = aic3x_read_reg_cache;
  1006. codec->write = aic3x_write;
  1007. codec->set_bias_level = aic3x_set_bias_level;
  1008. codec->dai = &aic3x_dai;
  1009. codec->num_dai = 1;
  1010. codec->reg_cache_size = ARRAY_SIZE(aic3x_reg);
  1011. codec->reg_cache = kmemdup(aic3x_reg, sizeof(aic3x_reg), GFP_KERNEL);
  1012. if (codec->reg_cache == NULL)
  1013. return -ENOMEM;
  1014. aic3x_write(codec, AIC3X_PAGE_SELECT, PAGE0_SELECT);
  1015. aic3x_write(codec, AIC3X_RESET, SOFT_RESET);
  1016. /* DAC default volume and mute */
  1017. aic3x_write(codec, LDAC_VOL, DEFAULT_VOL | MUTE_ON);
  1018. aic3x_write(codec, RDAC_VOL, DEFAULT_VOL | MUTE_ON);
  1019. /* DAC to HP default volume and route to Output mixer */
  1020. aic3x_write(codec, DACL1_2_HPLOUT_VOL, DEFAULT_VOL | ROUTE_ON);
  1021. aic3x_write(codec, DACR1_2_HPROUT_VOL, DEFAULT_VOL | ROUTE_ON);
  1022. aic3x_write(codec, DACL1_2_HPLCOM_VOL, DEFAULT_VOL | ROUTE_ON);
  1023. aic3x_write(codec, DACR1_2_HPRCOM_VOL, DEFAULT_VOL | ROUTE_ON);
  1024. /* DAC to Line Out default volume and route to Output mixer */
  1025. aic3x_write(codec, DACL1_2_LLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1026. aic3x_write(codec, DACR1_2_RLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1027. /* DAC to Mono Line Out default volume and route to Output mixer */
  1028. aic3x_write(codec, DACL1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1029. aic3x_write(codec, DACR1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
  1030. /* unmute all outputs */
  1031. reg = aic3x_read_reg_cache(codec, LLOPM_CTRL);
  1032. aic3x_write(codec, LLOPM_CTRL, reg | UNMUTE);
  1033. reg = aic3x_read_reg_cache(codec, RLOPM_CTRL);
  1034. aic3x_write(codec, RLOPM_CTRL, reg | UNMUTE);
  1035. reg = aic3x_read_reg_cache(codec, MONOLOPM_CTRL);
  1036. aic3x_write(codec, MONOLOPM_CTRL, reg | UNMUTE);
  1037. reg = aic3x_read_reg_cache(codec, HPLOUT_CTRL);
  1038. aic3x_write(codec, HPLOUT_CTRL, reg | UNMUTE);
  1039. reg = aic3x_read_reg_cache(codec, HPROUT_CTRL);
  1040. aic3x_write(codec, HPROUT_CTRL, reg | UNMUTE);
  1041. reg = aic3x_read_reg_cache(codec, HPLCOM_CTRL);
  1042. aic3x_write(codec, HPLCOM_CTRL, reg | UNMUTE);
  1043. reg = aic3x_read_reg_cache(codec, HPRCOM_CTRL);
  1044. aic3x_write(codec, HPRCOM_CTRL, reg | UNMUTE);
  1045. /* ADC default volume and unmute */
  1046. aic3x_write(codec, LADC_VOL, DEFAULT_GAIN);
  1047. aic3x_write(codec, RADC_VOL, DEFAULT_GAIN);
  1048. /* By default route Line1 to ADC PGA mixer */
  1049. aic3x_write(codec, LINE1L_2_LADC_CTRL, 0x0);
  1050. aic3x_write(codec, LINE1R_2_RADC_CTRL, 0x0);
  1051. /* PGA to HP Bypass default volume, disconnect from Output Mixer */
  1052. aic3x_write(codec, PGAL_2_HPLOUT_VOL, DEFAULT_VOL);
  1053. aic3x_write(codec, PGAR_2_HPROUT_VOL, DEFAULT_VOL);
  1054. aic3x_write(codec, PGAL_2_HPLCOM_VOL, DEFAULT_VOL);
  1055. aic3x_write(codec, PGAR_2_HPRCOM_VOL, DEFAULT_VOL);
  1056. /* PGA to Line Out default volume, disconnect from Output Mixer */
  1057. aic3x_write(codec, PGAL_2_LLOPM_VOL, DEFAULT_VOL);
  1058. aic3x_write(codec, PGAR_2_RLOPM_VOL, DEFAULT_VOL);
  1059. /* PGA to Mono Line Out default volume, disconnect from Output Mixer */
  1060. aic3x_write(codec, PGAL_2_MONOLOPM_VOL, DEFAULT_VOL);
  1061. aic3x_write(codec, PGAR_2_MONOLOPM_VOL, DEFAULT_VOL);
  1062. /* Line2 to HP Bypass default volume, disconnect from Output Mixer */
  1063. aic3x_write(codec, LINE2L_2_HPLOUT_VOL, DEFAULT_VOL);
  1064. aic3x_write(codec, LINE2R_2_HPROUT_VOL, DEFAULT_VOL);
  1065. aic3x_write(codec, LINE2L_2_HPLCOM_VOL, DEFAULT_VOL);
  1066. aic3x_write(codec, LINE2R_2_HPRCOM_VOL, DEFAULT_VOL);
  1067. /* Line2 Line Out default volume, disconnect from Output Mixer */
  1068. aic3x_write(codec, LINE2L_2_LLOPM_VOL, DEFAULT_VOL);
  1069. aic3x_write(codec, LINE2R_2_RLOPM_VOL, DEFAULT_VOL);
  1070. /* Line2 to Mono Out default volume, disconnect from Output Mixer */
  1071. aic3x_write(codec, LINE2L_2_MONOLOPM_VOL, DEFAULT_VOL);
  1072. aic3x_write(codec, LINE2R_2_MONOLOPM_VOL, DEFAULT_VOL);
  1073. /* off, with power on */
  1074. aic3x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1075. return 0;
  1076. }
  1077. static struct snd_soc_codec *aic3x_codec;
  1078. static int aic3x_register(struct snd_soc_codec *codec)
  1079. {
  1080. int ret;
  1081. ret = aic3x_init(codec);
  1082. if (ret < 0) {
  1083. dev_err(codec->dev, "Failed to initialise device\n");
  1084. return ret;
  1085. }
  1086. aic3x_codec = codec;
  1087. ret = snd_soc_register_codec(codec);
  1088. if (ret) {
  1089. dev_err(codec->dev, "Failed to register codec\n");
  1090. return ret;
  1091. }
  1092. ret = snd_soc_register_dai(&aic3x_dai);
  1093. if (ret) {
  1094. dev_err(codec->dev, "Failed to register dai\n");
  1095. snd_soc_unregister_codec(codec);
  1096. return ret;
  1097. }
  1098. return 0;
  1099. }
  1100. static int aic3x_unregister(struct aic3x_priv *aic3x)
  1101. {
  1102. aic3x_set_bias_level(&aic3x->codec, SND_SOC_BIAS_OFF);
  1103. snd_soc_unregister_dai(&aic3x_dai);
  1104. snd_soc_unregister_codec(&aic3x->codec);
  1105. if (aic3x->gpio_reset >= 0) {
  1106. gpio_set_value(aic3x->gpio_reset, 0);
  1107. gpio_free(aic3x->gpio_reset);
  1108. }
  1109. regulator_bulk_disable(ARRAY_SIZE(aic3x->supplies), aic3x->supplies);
  1110. regulator_bulk_free(ARRAY_SIZE(aic3x->supplies), aic3x->supplies);
  1111. kfree(aic3x);
  1112. aic3x_codec = NULL;
  1113. return 0;
  1114. }
  1115. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1116. /*
  1117. * AIC3X 2 wire address can be up to 4 devices with device addresses
  1118. * 0x18, 0x19, 0x1A, 0x1B
  1119. */
  1120. /*
  1121. * If the i2c layer weren't so broken, we could pass this kind of data
  1122. * around
  1123. */
  1124. static int aic3x_i2c_probe(struct i2c_client *i2c,
  1125. const struct i2c_device_id *id)
  1126. {
  1127. struct snd_soc_codec *codec;
  1128. struct aic3x_priv *aic3x;
  1129. struct aic3x_pdata *pdata = i2c->dev.platform_data;
  1130. int ret, i;
  1131. aic3x = kzalloc(sizeof(struct aic3x_priv), GFP_KERNEL);
  1132. if (aic3x == NULL) {
  1133. dev_err(&i2c->dev, "failed to create private data\n");
  1134. return -ENOMEM;
  1135. }
  1136. codec = &aic3x->codec;
  1137. codec->dev = &i2c->dev;
  1138. snd_soc_codec_set_drvdata(codec, aic3x);
  1139. codec->control_data = i2c;
  1140. codec->hw_write = (hw_write_t) i2c_master_send;
  1141. i2c_set_clientdata(i2c, aic3x);
  1142. aic3x->gpio_reset = -1;
  1143. if (pdata && pdata->gpio_reset >= 0) {
  1144. ret = gpio_request(pdata->gpio_reset, "tlv320aic3x reset");
  1145. if (ret != 0)
  1146. goto err_gpio;
  1147. aic3x->gpio_reset = pdata->gpio_reset;
  1148. gpio_direction_output(aic3x->gpio_reset, 0);
  1149. }
  1150. for (i = 0; i < ARRAY_SIZE(aic3x->supplies); i++)
  1151. aic3x->supplies[i].supply = aic3x_supply_names[i];
  1152. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(aic3x->supplies),
  1153. aic3x->supplies);
  1154. if (ret != 0) {
  1155. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1156. goto err_get;
  1157. }
  1158. ret = regulator_bulk_enable(ARRAY_SIZE(aic3x->supplies),
  1159. aic3x->supplies);
  1160. if (ret != 0) {
  1161. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1162. goto err_enable;
  1163. }
  1164. if (aic3x->gpio_reset >= 0) {
  1165. udelay(1);
  1166. gpio_set_value(aic3x->gpio_reset, 1);
  1167. }
  1168. return aic3x_register(codec);
  1169. err_enable:
  1170. regulator_bulk_free(ARRAY_SIZE(aic3x->supplies), aic3x->supplies);
  1171. err_get:
  1172. if (aic3x->gpio_reset >= 0)
  1173. gpio_free(aic3x->gpio_reset);
  1174. err_gpio:
  1175. kfree(aic3x);
  1176. return ret;
  1177. }
  1178. static int aic3x_i2c_remove(struct i2c_client *client)
  1179. {
  1180. struct aic3x_priv *aic3x = i2c_get_clientdata(client);
  1181. return aic3x_unregister(aic3x);
  1182. }
  1183. static const struct i2c_device_id aic3x_i2c_id[] = {
  1184. { "tlv320aic3x", 0 },
  1185. { "tlv320aic33", 0 },
  1186. { }
  1187. };
  1188. MODULE_DEVICE_TABLE(i2c, aic3x_i2c_id);
  1189. /* machine i2c codec control layer */
  1190. static struct i2c_driver aic3x_i2c_driver = {
  1191. .driver = {
  1192. .name = "aic3x I2C Codec",
  1193. .owner = THIS_MODULE,
  1194. },
  1195. .probe = aic3x_i2c_probe,
  1196. .remove = aic3x_i2c_remove,
  1197. .id_table = aic3x_i2c_id,
  1198. };
  1199. static inline void aic3x_i2c_init(void)
  1200. {
  1201. int ret;
  1202. ret = i2c_add_driver(&aic3x_i2c_driver);
  1203. if (ret)
  1204. printk(KERN_ERR "%s: error regsitering i2c driver, %d\n",
  1205. __func__, ret);
  1206. }
  1207. static inline void aic3x_i2c_exit(void)
  1208. {
  1209. i2c_del_driver(&aic3x_i2c_driver);
  1210. }
  1211. #else
  1212. static inline void aic3x_i2c_init(void) { }
  1213. static inline void aic3x_i2c_exit(void) { }
  1214. #endif
  1215. static int aic3x_probe(struct platform_device *pdev)
  1216. {
  1217. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1218. struct aic3x_setup_data *setup;
  1219. struct snd_soc_codec *codec;
  1220. int ret = 0;
  1221. codec = aic3x_codec;
  1222. if (!codec) {
  1223. dev_err(&pdev->dev, "Codec not registered\n");
  1224. return -ENODEV;
  1225. }
  1226. socdev->card->codec = codec;
  1227. setup = socdev->codec_data;
  1228. if (setup) {
  1229. /* setup GPIO functions */
  1230. aic3x_write(codec, AIC3X_GPIO1_REG,
  1231. (setup->gpio_func[0] & 0xf) << 4);
  1232. aic3x_write(codec, AIC3X_GPIO2_REG,
  1233. (setup->gpio_func[1] & 0xf) << 4);
  1234. }
  1235. /* register pcms */
  1236. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  1237. if (ret < 0) {
  1238. printk(KERN_ERR "aic3x: failed to create pcms\n");
  1239. goto pcm_err;
  1240. }
  1241. snd_soc_add_controls(codec, aic3x_snd_controls,
  1242. ARRAY_SIZE(aic3x_snd_controls));
  1243. aic3x_add_widgets(codec);
  1244. return ret;
  1245. pcm_err:
  1246. kfree(codec->reg_cache);
  1247. return ret;
  1248. }
  1249. static int aic3x_remove(struct platform_device *pdev)
  1250. {
  1251. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1252. struct snd_soc_codec *codec = socdev->card->codec;
  1253. /* power down chip */
  1254. if (codec->control_data)
  1255. aic3x_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1256. snd_soc_free_pcms(socdev);
  1257. snd_soc_dapm_free(socdev);
  1258. kfree(codec->reg_cache);
  1259. return 0;
  1260. }
  1261. struct snd_soc_codec_device soc_codec_dev_aic3x = {
  1262. .probe = aic3x_probe,
  1263. .remove = aic3x_remove,
  1264. .suspend = aic3x_suspend,
  1265. .resume = aic3x_resume,
  1266. };
  1267. EXPORT_SYMBOL_GPL(soc_codec_dev_aic3x);
  1268. static int __init aic3x_modinit(void)
  1269. {
  1270. aic3x_i2c_init();
  1271. return 0;
  1272. }
  1273. module_init(aic3x_modinit);
  1274. static void __exit aic3x_exit(void)
  1275. {
  1276. aic3x_i2c_exit();
  1277. }
  1278. module_exit(aic3x_exit);
  1279. MODULE_DESCRIPTION("ASoC TLV320AIC3X codec driver");
  1280. MODULE_AUTHOR("Vladimir Barinov");
  1281. MODULE_LICENSE("GPL");