tlv320aic3x.c 42 KB

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