tlv320aic3x.c 40 KB

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