tlv320aic3x.c 46 KB

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