tlv320aic3x.c 40 KB

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