tlv320aic3x.c 41 KB

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