tlv320aic3x.c 41 KB

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