tlv320aic3x.c 47 KB

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