tlv320aic3x.c 41 KB

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