tlv320aic32x4.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * linux/sound/soc/codecs/tlv320aic32x4.c
  3. *
  4. * Copyright 2011 Vista Silicon S.L.
  5. *
  6. * Author: Javier Martin <javier.martin@vista-silicon.com>
  7. *
  8. * Based on sound/soc/codecs/wm8974 and TI driver for kernel 2.6.27.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. * MA 02110-1301, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/pm.h>
  30. #include <linux/i2c.h>
  31. #include <linux/cdev.h>
  32. #include <linux/slab.h>
  33. #include <sound/tlv320aic32x4.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include <sound/soc-dapm.h>
  39. #include <sound/initval.h>
  40. #include <sound/tlv.h>
  41. #include "tlv320aic32x4.h"
  42. struct aic32x4_rate_divs {
  43. u32 mclk;
  44. u32 rate;
  45. u8 p_val;
  46. u8 pll_j;
  47. u16 pll_d;
  48. u16 dosr;
  49. u8 ndac;
  50. u8 mdac;
  51. u8 aosr;
  52. u8 nadc;
  53. u8 madc;
  54. u8 blck_N;
  55. };
  56. struct aic32x4_priv {
  57. u32 sysclk;
  58. u8 page_no;
  59. void *control_data;
  60. u32 power_cfg;
  61. u32 micpga_routing;
  62. bool swapdacs;
  63. };
  64. /* 0dB min, 1dB steps */
  65. static DECLARE_TLV_DB_SCALE(tlv_step_1, 0, 100, 0);
  66. /* 0dB min, 0.5dB steps */
  67. static DECLARE_TLV_DB_SCALE(tlv_step_0_5, 0, 50, 0);
  68. static const struct snd_kcontrol_new aic32x4_snd_controls[] = {
  69. SOC_DOUBLE_R_TLV("PCM Playback Volume", AIC32X4_LDACVOL,
  70. AIC32X4_RDACVOL, 0, 0x30, 0, tlv_step_0_5),
  71. SOC_DOUBLE_R_TLV("HP Driver Gain Volume", AIC32X4_HPLGAIN,
  72. AIC32X4_HPRGAIN, 0, 0x1D, 0, tlv_step_1),
  73. SOC_DOUBLE_R_TLV("LO Driver Gain Volume", AIC32X4_LOLGAIN,
  74. AIC32X4_LORGAIN, 0, 0x1D, 0, tlv_step_1),
  75. SOC_DOUBLE_R("HP DAC Playback Switch", AIC32X4_HPLGAIN,
  76. AIC32X4_HPRGAIN, 6, 0x01, 1),
  77. SOC_DOUBLE_R("LO DAC Playback Switch", AIC32X4_LOLGAIN,
  78. AIC32X4_LORGAIN, 6, 0x01, 1),
  79. SOC_DOUBLE_R("Mic PGA Switch", AIC32X4_LMICPGAVOL,
  80. AIC32X4_RMICPGAVOL, 7, 0x01, 1),
  81. SOC_SINGLE("ADCFGA Left Mute Switch", AIC32X4_ADCFGA, 7, 1, 0),
  82. SOC_SINGLE("ADCFGA Right Mute Switch", AIC32X4_ADCFGA, 3, 1, 0),
  83. SOC_DOUBLE_R_TLV("ADC Level Volume", AIC32X4_LADCVOL,
  84. AIC32X4_RADCVOL, 0, 0x28, 0, tlv_step_0_5),
  85. SOC_DOUBLE_R_TLV("PGA Level Volume", AIC32X4_LMICPGAVOL,
  86. AIC32X4_RMICPGAVOL, 0, 0x5f, 0, tlv_step_0_5),
  87. SOC_SINGLE("Auto-mute Switch", AIC32X4_DACMUTE, 4, 7, 0),
  88. SOC_SINGLE("AGC Left Switch", AIC32X4_LAGC1, 7, 1, 0),
  89. SOC_SINGLE("AGC Right Switch", AIC32X4_RAGC1, 7, 1, 0),
  90. SOC_DOUBLE_R("AGC Target Level", AIC32X4_LAGC1, AIC32X4_RAGC1,
  91. 4, 0x07, 0),
  92. SOC_DOUBLE_R("AGC Gain Hysteresis", AIC32X4_LAGC1, AIC32X4_RAGC1,
  93. 0, 0x03, 0),
  94. SOC_DOUBLE_R("AGC Hysteresis", AIC32X4_LAGC2, AIC32X4_RAGC2,
  95. 6, 0x03, 0),
  96. SOC_DOUBLE_R("AGC Noise Threshold", AIC32X4_LAGC2, AIC32X4_RAGC2,
  97. 1, 0x1F, 0),
  98. SOC_DOUBLE_R("AGC Max PGA", AIC32X4_LAGC3, AIC32X4_RAGC3,
  99. 0, 0x7F, 0),
  100. SOC_DOUBLE_R("AGC Attack Time", AIC32X4_LAGC4, AIC32X4_RAGC4,
  101. 3, 0x1F, 0),
  102. SOC_DOUBLE_R("AGC Decay Time", AIC32X4_LAGC5, AIC32X4_RAGC5,
  103. 3, 0x1F, 0),
  104. SOC_DOUBLE_R("AGC Noise Debounce", AIC32X4_LAGC6, AIC32X4_RAGC6,
  105. 0, 0x1F, 0),
  106. SOC_DOUBLE_R("AGC Signal Debounce", AIC32X4_LAGC7, AIC32X4_RAGC7,
  107. 0, 0x0F, 0),
  108. };
  109. static const struct aic32x4_rate_divs aic32x4_divs[] = {
  110. /* 8k rate */
  111. {AIC32X4_FREQ_12000000, 8000, 1, 7, 6800, 768, 5, 3, 128, 5, 18, 24},
  112. {AIC32X4_FREQ_24000000, 8000, 2, 7, 6800, 768, 15, 1, 64, 45, 4, 24},
  113. {AIC32X4_FREQ_25000000, 8000, 2, 7, 3728, 768, 15, 1, 64, 45, 4, 24},
  114. /* 11.025k rate */
  115. {AIC32X4_FREQ_12000000, 11025, 1, 7, 5264, 512, 8, 2, 128, 8, 8, 16},
  116. {AIC32X4_FREQ_24000000, 11025, 2, 7, 5264, 512, 16, 1, 64, 32, 4, 16},
  117. /* 16k rate */
  118. {AIC32X4_FREQ_12000000, 16000, 1, 7, 6800, 384, 5, 3, 128, 5, 9, 12},
  119. {AIC32X4_FREQ_24000000, 16000, 2, 7, 6800, 384, 15, 1, 64, 18, 5, 12},
  120. {AIC32X4_FREQ_25000000, 16000, 2, 7, 3728, 384, 15, 1, 64, 18, 5, 12},
  121. /* 22.05k rate */
  122. {AIC32X4_FREQ_12000000, 22050, 1, 7, 5264, 256, 4, 4, 128, 4, 8, 8},
  123. {AIC32X4_FREQ_24000000, 22050, 2, 7, 5264, 256, 16, 1, 64, 16, 4, 8},
  124. {AIC32X4_FREQ_25000000, 22050, 2, 7, 2253, 256, 16, 1, 64, 16, 4, 8},
  125. /* 32k rate */
  126. {AIC32X4_FREQ_12000000, 32000, 1, 7, 1680, 192, 2, 7, 64, 2, 21, 6},
  127. {AIC32X4_FREQ_24000000, 32000, 2, 7, 1680, 192, 7, 2, 64, 7, 6, 6},
  128. /* 44.1k rate */
  129. {AIC32X4_FREQ_12000000, 44100, 1, 7, 5264, 128, 2, 8, 128, 2, 8, 4},
  130. {AIC32X4_FREQ_24000000, 44100, 2, 7, 5264, 128, 8, 2, 64, 8, 4, 4},
  131. {AIC32X4_FREQ_25000000, 44100, 2, 7, 2253, 128, 8, 2, 64, 8, 4, 4},
  132. /* 48k rate */
  133. {AIC32X4_FREQ_12000000, 48000, 1, 8, 1920, 128, 2, 8, 128, 2, 8, 4},
  134. {AIC32X4_FREQ_24000000, 48000, 2, 8, 1920, 128, 8, 2, 64, 8, 4, 4},
  135. {AIC32X4_FREQ_25000000, 48000, 2, 7, 8643, 128, 8, 2, 64, 8, 4, 4}
  136. };
  137. static const struct snd_kcontrol_new hpl_output_mixer_controls[] = {
  138. SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_HPLROUTE, 3, 1, 0),
  139. SOC_DAPM_SINGLE("IN1_L Switch", AIC32X4_HPLROUTE, 2, 1, 0),
  140. };
  141. static const struct snd_kcontrol_new hpr_output_mixer_controls[] = {
  142. SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_HPRROUTE, 3, 1, 0),
  143. SOC_DAPM_SINGLE("IN1_R Switch", AIC32X4_HPRROUTE, 2, 1, 0),
  144. };
  145. static const struct snd_kcontrol_new lol_output_mixer_controls[] = {
  146. SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_LOLROUTE, 3, 1, 0),
  147. };
  148. static const struct snd_kcontrol_new lor_output_mixer_controls[] = {
  149. SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_LORROUTE, 3, 1, 0),
  150. };
  151. static const struct snd_kcontrol_new left_input_mixer_controls[] = {
  152. SOC_DAPM_SINGLE("IN1_L P Switch", AIC32X4_LMICPGAPIN, 6, 1, 0),
  153. SOC_DAPM_SINGLE("IN2_L P Switch", AIC32X4_LMICPGAPIN, 4, 1, 0),
  154. SOC_DAPM_SINGLE("IN3_L P Switch", AIC32X4_LMICPGAPIN, 2, 1, 0),
  155. };
  156. static const struct snd_kcontrol_new right_input_mixer_controls[] = {
  157. SOC_DAPM_SINGLE("IN1_R P Switch", AIC32X4_RMICPGAPIN, 6, 1, 0),
  158. SOC_DAPM_SINGLE("IN2_R P Switch", AIC32X4_RMICPGAPIN, 4, 1, 0),
  159. SOC_DAPM_SINGLE("IN3_R P Switch", AIC32X4_RMICPGAPIN, 2, 1, 0),
  160. };
  161. static const struct snd_soc_dapm_widget aic32x4_dapm_widgets[] = {
  162. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", AIC32X4_DACSETUP, 7, 0),
  163. SND_SOC_DAPM_MIXER("HPL Output Mixer", SND_SOC_NOPM, 0, 0,
  164. &hpl_output_mixer_controls[0],
  165. ARRAY_SIZE(hpl_output_mixer_controls)),
  166. SND_SOC_DAPM_PGA("HPL Power", AIC32X4_OUTPWRCTL, 5, 0, NULL, 0),
  167. SND_SOC_DAPM_MIXER("LOL Output Mixer", SND_SOC_NOPM, 0, 0,
  168. &lol_output_mixer_controls[0],
  169. ARRAY_SIZE(lol_output_mixer_controls)),
  170. SND_SOC_DAPM_PGA("LOL Power", AIC32X4_OUTPWRCTL, 3, 0, NULL, 0),
  171. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", AIC32X4_DACSETUP, 6, 0),
  172. SND_SOC_DAPM_MIXER("HPR Output Mixer", SND_SOC_NOPM, 0, 0,
  173. &hpr_output_mixer_controls[0],
  174. ARRAY_SIZE(hpr_output_mixer_controls)),
  175. SND_SOC_DAPM_PGA("HPR Power", AIC32X4_OUTPWRCTL, 4, 0, NULL, 0),
  176. SND_SOC_DAPM_MIXER("LOR Output Mixer", SND_SOC_NOPM, 0, 0,
  177. &lor_output_mixer_controls[0],
  178. ARRAY_SIZE(lor_output_mixer_controls)),
  179. SND_SOC_DAPM_PGA("LOR Power", AIC32X4_OUTPWRCTL, 2, 0, NULL, 0),
  180. SND_SOC_DAPM_MIXER("Left Input Mixer", SND_SOC_NOPM, 0, 0,
  181. &left_input_mixer_controls[0],
  182. ARRAY_SIZE(left_input_mixer_controls)),
  183. SND_SOC_DAPM_MIXER("Right Input Mixer", SND_SOC_NOPM, 0, 0,
  184. &right_input_mixer_controls[0],
  185. ARRAY_SIZE(right_input_mixer_controls)),
  186. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", AIC32X4_ADCSETUP, 7, 0),
  187. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", AIC32X4_ADCSETUP, 6, 0),
  188. SND_SOC_DAPM_MICBIAS("Mic Bias", AIC32X4_MICBIAS, 6, 0),
  189. SND_SOC_DAPM_OUTPUT("HPL"),
  190. SND_SOC_DAPM_OUTPUT("HPR"),
  191. SND_SOC_DAPM_OUTPUT("LOL"),
  192. SND_SOC_DAPM_OUTPUT("LOR"),
  193. SND_SOC_DAPM_INPUT("IN1_L"),
  194. SND_SOC_DAPM_INPUT("IN1_R"),
  195. SND_SOC_DAPM_INPUT("IN2_L"),
  196. SND_SOC_DAPM_INPUT("IN2_R"),
  197. SND_SOC_DAPM_INPUT("IN3_L"),
  198. SND_SOC_DAPM_INPUT("IN3_R"),
  199. };
  200. static const struct snd_soc_dapm_route aic32x4_dapm_routes[] = {
  201. /* Left Output */
  202. {"HPL Output Mixer", "L_DAC Switch", "Left DAC"},
  203. {"HPL Output Mixer", "IN1_L Switch", "IN1_L"},
  204. {"HPL Power", NULL, "HPL Output Mixer"},
  205. {"HPL", NULL, "HPL Power"},
  206. {"LOL Output Mixer", "L_DAC Switch", "Left DAC"},
  207. {"LOL Power", NULL, "LOL Output Mixer"},
  208. {"LOL", NULL, "LOL Power"},
  209. /* Right Output */
  210. {"HPR Output Mixer", "R_DAC Switch", "Right DAC"},
  211. {"HPR Output Mixer", "IN1_R Switch", "IN1_R"},
  212. {"HPR Power", NULL, "HPR Output Mixer"},
  213. {"HPR", NULL, "HPR Power"},
  214. {"LOR Output Mixer", "R_DAC Switch", "Right DAC"},
  215. {"LOR Power", NULL, "LOR Output Mixer"},
  216. {"LOR", NULL, "LOR Power"},
  217. /* Left input */
  218. {"Left Input Mixer", "IN1_L P Switch", "IN1_L"},
  219. {"Left Input Mixer", "IN2_L P Switch", "IN2_L"},
  220. {"Left Input Mixer", "IN3_L P Switch", "IN3_L"},
  221. {"Left ADC", NULL, "Left Input Mixer"},
  222. /* Right Input */
  223. {"Right Input Mixer", "IN1_R P Switch", "IN1_R"},
  224. {"Right Input Mixer", "IN2_R P Switch", "IN2_R"},
  225. {"Right Input Mixer", "IN3_R P Switch", "IN3_R"},
  226. {"Right ADC", NULL, "Right Input Mixer"},
  227. };
  228. static inline int aic32x4_change_page(struct snd_soc_codec *codec,
  229. unsigned int new_page)
  230. {
  231. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  232. u8 data[2];
  233. int ret;
  234. data[0] = 0x00;
  235. data[1] = new_page & 0xff;
  236. ret = codec->hw_write(codec->control_data, data, 2);
  237. if (ret == 2) {
  238. aic32x4->page_no = new_page;
  239. return 0;
  240. } else {
  241. return ret;
  242. }
  243. }
  244. static int aic32x4_write(struct snd_soc_codec *codec, unsigned int reg,
  245. unsigned int val)
  246. {
  247. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  248. unsigned int page = reg / 128;
  249. unsigned int fixed_reg = reg % 128;
  250. u8 data[2];
  251. int ret;
  252. /* A write to AIC32X4_PSEL is really a non-explicit page change */
  253. if (reg == AIC32X4_PSEL)
  254. return aic32x4_change_page(codec, val);
  255. if (aic32x4->page_no != page) {
  256. ret = aic32x4_change_page(codec, page);
  257. if (ret != 0)
  258. return ret;
  259. }
  260. data[0] = fixed_reg & 0xff;
  261. data[1] = val & 0xff;
  262. if (codec->hw_write(codec->control_data, data, 2) == 2)
  263. return 0;
  264. else
  265. return -EIO;
  266. }
  267. static unsigned int aic32x4_read(struct snd_soc_codec *codec, unsigned int reg)
  268. {
  269. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  270. unsigned int page = reg / 128;
  271. unsigned int fixed_reg = reg % 128;
  272. int ret;
  273. if (aic32x4->page_no != page) {
  274. ret = aic32x4_change_page(codec, page);
  275. if (ret != 0)
  276. return ret;
  277. }
  278. return i2c_smbus_read_byte_data(codec->control_data, fixed_reg & 0xff);
  279. }
  280. static inline int aic32x4_get_divs(int mclk, int rate)
  281. {
  282. int i;
  283. for (i = 0; i < ARRAY_SIZE(aic32x4_divs); i++) {
  284. if ((aic32x4_divs[i].rate == rate)
  285. && (aic32x4_divs[i].mclk == mclk)) {
  286. return i;
  287. }
  288. }
  289. printk(KERN_ERR "aic32x4: master clock and sample rate is not supported\n");
  290. return -EINVAL;
  291. }
  292. static int aic32x4_add_widgets(struct snd_soc_codec *codec)
  293. {
  294. snd_soc_dapm_new_controls(&codec->dapm, aic32x4_dapm_widgets,
  295. ARRAY_SIZE(aic32x4_dapm_widgets));
  296. snd_soc_dapm_add_routes(&codec->dapm, aic32x4_dapm_routes,
  297. ARRAY_SIZE(aic32x4_dapm_routes));
  298. snd_soc_dapm_new_widgets(&codec->dapm);
  299. return 0;
  300. }
  301. static int aic32x4_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  302. int clk_id, unsigned int freq, int dir)
  303. {
  304. struct snd_soc_codec *codec = codec_dai->codec;
  305. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  306. switch (freq) {
  307. case AIC32X4_FREQ_12000000:
  308. case AIC32X4_FREQ_24000000:
  309. case AIC32X4_FREQ_25000000:
  310. aic32x4->sysclk = freq;
  311. return 0;
  312. }
  313. printk(KERN_ERR "aic32x4: invalid frequency to set DAI system clock\n");
  314. return -EINVAL;
  315. }
  316. static int aic32x4_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  317. {
  318. struct snd_soc_codec *codec = codec_dai->codec;
  319. u8 iface_reg_1;
  320. u8 iface_reg_2;
  321. u8 iface_reg_3;
  322. iface_reg_1 = snd_soc_read(codec, AIC32X4_IFACE1);
  323. iface_reg_1 = iface_reg_1 & ~(3 << 6 | 3 << 2);
  324. iface_reg_2 = snd_soc_read(codec, AIC32X4_IFACE2);
  325. iface_reg_2 = 0;
  326. iface_reg_3 = snd_soc_read(codec, AIC32X4_IFACE3);
  327. iface_reg_3 = iface_reg_3 & ~(1 << 3);
  328. /* set master/slave audio interface */
  329. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  330. case SND_SOC_DAIFMT_CBM_CFM:
  331. iface_reg_1 |= AIC32X4_BCLKMASTER | AIC32X4_WCLKMASTER;
  332. break;
  333. case SND_SOC_DAIFMT_CBS_CFS:
  334. break;
  335. default:
  336. printk(KERN_ERR "aic32x4: invalid DAI master/slave interface\n");
  337. return -EINVAL;
  338. }
  339. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  340. case SND_SOC_DAIFMT_I2S:
  341. break;
  342. case SND_SOC_DAIFMT_DSP_A:
  343. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  344. iface_reg_3 |= (1 << 3); /* invert bit clock */
  345. iface_reg_2 = 0x01; /* add offset 1 */
  346. break;
  347. case SND_SOC_DAIFMT_DSP_B:
  348. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  349. iface_reg_3 |= (1 << 3); /* invert bit clock */
  350. break;
  351. case SND_SOC_DAIFMT_RIGHT_J:
  352. iface_reg_1 |=
  353. (AIC32X4_RIGHT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  354. break;
  355. case SND_SOC_DAIFMT_LEFT_J:
  356. iface_reg_1 |=
  357. (AIC32X4_LEFT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  358. break;
  359. default:
  360. printk(KERN_ERR "aic32x4: invalid DAI interface format\n");
  361. return -EINVAL;
  362. }
  363. snd_soc_write(codec, AIC32X4_IFACE1, iface_reg_1);
  364. snd_soc_write(codec, AIC32X4_IFACE2, iface_reg_2);
  365. snd_soc_write(codec, AIC32X4_IFACE3, iface_reg_3);
  366. return 0;
  367. }
  368. static int aic32x4_hw_params(struct snd_pcm_substream *substream,
  369. struct snd_pcm_hw_params *params,
  370. struct snd_soc_dai *dai)
  371. {
  372. struct snd_soc_codec *codec = dai->codec;
  373. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  374. u8 data;
  375. int i;
  376. i = aic32x4_get_divs(aic32x4->sysclk, params_rate(params));
  377. if (i < 0) {
  378. printk(KERN_ERR "aic32x4: sampling rate not supported\n");
  379. return i;
  380. }
  381. /* Use PLL as CODEC_CLKIN and DAC_MOD_CLK as BDIV_CLKIN */
  382. snd_soc_write(codec, AIC32X4_CLKMUX, AIC32X4_PLLCLKIN);
  383. snd_soc_write(codec, AIC32X4_IFACE3, AIC32X4_DACMOD2BCLK);
  384. /* We will fix R value to 1 and will make P & J=K.D as varialble */
  385. data = snd_soc_read(codec, AIC32X4_PLLPR);
  386. data &= ~(7 << 4);
  387. snd_soc_write(codec, AIC32X4_PLLPR,
  388. (data | (aic32x4_divs[i].p_val << 4) | 0x01));
  389. snd_soc_write(codec, AIC32X4_PLLJ, aic32x4_divs[i].pll_j);
  390. snd_soc_write(codec, AIC32X4_PLLDMSB, (aic32x4_divs[i].pll_d >> 8));
  391. snd_soc_write(codec, AIC32X4_PLLDLSB,
  392. (aic32x4_divs[i].pll_d & 0xff));
  393. /* NDAC divider value */
  394. data = snd_soc_read(codec, AIC32X4_NDAC);
  395. data &= ~(0x7f);
  396. snd_soc_write(codec, AIC32X4_NDAC, data | aic32x4_divs[i].ndac);
  397. /* MDAC divider value */
  398. data = snd_soc_read(codec, AIC32X4_MDAC);
  399. data &= ~(0x7f);
  400. snd_soc_write(codec, AIC32X4_MDAC, data | aic32x4_divs[i].mdac);
  401. /* DOSR MSB & LSB values */
  402. snd_soc_write(codec, AIC32X4_DOSRMSB, aic32x4_divs[i].dosr >> 8);
  403. snd_soc_write(codec, AIC32X4_DOSRLSB,
  404. (aic32x4_divs[i].dosr & 0xff));
  405. /* NADC divider value */
  406. data = snd_soc_read(codec, AIC32X4_NADC);
  407. data &= ~(0x7f);
  408. snd_soc_write(codec, AIC32X4_NADC, data | aic32x4_divs[i].nadc);
  409. /* MADC divider value */
  410. data = snd_soc_read(codec, AIC32X4_MADC);
  411. data &= ~(0x7f);
  412. snd_soc_write(codec, AIC32X4_MADC, data | aic32x4_divs[i].madc);
  413. /* AOSR value */
  414. snd_soc_write(codec, AIC32X4_AOSR, aic32x4_divs[i].aosr);
  415. /* BCLK N divider */
  416. data = snd_soc_read(codec, AIC32X4_BCLKN);
  417. data &= ~(0x7f);
  418. snd_soc_write(codec, AIC32X4_BCLKN, data | aic32x4_divs[i].blck_N);
  419. data = snd_soc_read(codec, AIC32X4_IFACE1);
  420. data = data & ~(3 << 4);
  421. switch (params_format(params)) {
  422. case SNDRV_PCM_FORMAT_S16_LE:
  423. break;
  424. case SNDRV_PCM_FORMAT_S20_3LE:
  425. data |= (AIC32X4_WORD_LEN_20BITS << AIC32X4_DOSRMSB_SHIFT);
  426. break;
  427. case SNDRV_PCM_FORMAT_S24_LE:
  428. data |= (AIC32X4_WORD_LEN_24BITS << AIC32X4_DOSRMSB_SHIFT);
  429. break;
  430. case SNDRV_PCM_FORMAT_S32_LE:
  431. data |= (AIC32X4_WORD_LEN_32BITS << AIC32X4_DOSRMSB_SHIFT);
  432. break;
  433. }
  434. snd_soc_write(codec, AIC32X4_IFACE1, data);
  435. return 0;
  436. }
  437. static int aic32x4_mute(struct snd_soc_dai *dai, int mute)
  438. {
  439. struct snd_soc_codec *codec = dai->codec;
  440. u8 dac_reg;
  441. dac_reg = snd_soc_read(codec, AIC32X4_DACMUTE) & ~AIC32X4_MUTEON;
  442. if (mute)
  443. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg | AIC32X4_MUTEON);
  444. else
  445. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg);
  446. return 0;
  447. }
  448. static int aic32x4_set_bias_level(struct snd_soc_codec *codec,
  449. enum snd_soc_bias_level level)
  450. {
  451. switch (level) {
  452. case SND_SOC_BIAS_ON:
  453. /* Switch on PLL */
  454. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  455. AIC32X4_PLLEN, AIC32X4_PLLEN);
  456. /* Switch on NDAC Divider */
  457. snd_soc_update_bits(codec, AIC32X4_NDAC,
  458. AIC32X4_NDACEN, AIC32X4_NDACEN);
  459. /* Switch on MDAC Divider */
  460. snd_soc_update_bits(codec, AIC32X4_MDAC,
  461. AIC32X4_MDACEN, AIC32X4_MDACEN);
  462. /* Switch on NADC Divider */
  463. snd_soc_update_bits(codec, AIC32X4_NADC,
  464. AIC32X4_NADCEN, AIC32X4_NADCEN);
  465. /* Switch on MADC Divider */
  466. snd_soc_update_bits(codec, AIC32X4_MADC,
  467. AIC32X4_MADCEN, AIC32X4_MADCEN);
  468. /* Switch on BCLK_N Divider */
  469. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  470. AIC32X4_BCLKEN, AIC32X4_BCLKEN);
  471. break;
  472. case SND_SOC_BIAS_PREPARE:
  473. break;
  474. case SND_SOC_BIAS_STANDBY:
  475. /* Switch off PLL */
  476. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  477. AIC32X4_PLLEN, 0);
  478. /* Switch off NDAC Divider */
  479. snd_soc_update_bits(codec, AIC32X4_NDAC,
  480. AIC32X4_NDACEN, 0);
  481. /* Switch off MDAC Divider */
  482. snd_soc_update_bits(codec, AIC32X4_MDAC,
  483. AIC32X4_MDACEN, 0);
  484. /* Switch off NADC Divider */
  485. snd_soc_update_bits(codec, AIC32X4_NADC,
  486. AIC32X4_NADCEN, 0);
  487. /* Switch off MADC Divider */
  488. snd_soc_update_bits(codec, AIC32X4_MADC,
  489. AIC32X4_MADCEN, 0);
  490. /* Switch off BCLK_N Divider */
  491. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  492. AIC32X4_BCLKEN, 0);
  493. break;
  494. case SND_SOC_BIAS_OFF:
  495. break;
  496. }
  497. codec->dapm.bias_level = level;
  498. return 0;
  499. }
  500. #define AIC32X4_RATES SNDRV_PCM_RATE_8000_48000
  501. #define AIC32X4_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  502. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  503. static const struct snd_soc_dai_ops aic32x4_ops = {
  504. .hw_params = aic32x4_hw_params,
  505. .digital_mute = aic32x4_mute,
  506. .set_fmt = aic32x4_set_dai_fmt,
  507. .set_sysclk = aic32x4_set_dai_sysclk,
  508. };
  509. static struct snd_soc_dai_driver aic32x4_dai = {
  510. .name = "tlv320aic32x4-hifi",
  511. .playback = {
  512. .stream_name = "Playback",
  513. .channels_min = 1,
  514. .channels_max = 2,
  515. .rates = AIC32X4_RATES,
  516. .formats = AIC32X4_FORMATS,},
  517. .capture = {
  518. .stream_name = "Capture",
  519. .channels_min = 1,
  520. .channels_max = 2,
  521. .rates = AIC32X4_RATES,
  522. .formats = AIC32X4_FORMATS,},
  523. .ops = &aic32x4_ops,
  524. .symmetric_rates = 1,
  525. };
  526. static int aic32x4_suspend(struct snd_soc_codec *codec)
  527. {
  528. aic32x4_set_bias_level(codec, SND_SOC_BIAS_OFF);
  529. return 0;
  530. }
  531. static int aic32x4_resume(struct snd_soc_codec *codec)
  532. {
  533. aic32x4_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  534. return 0;
  535. }
  536. static int aic32x4_probe(struct snd_soc_codec *codec)
  537. {
  538. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  539. u32 tmp_reg;
  540. codec->hw_write = (hw_write_t) i2c_master_send;
  541. codec->control_data = aic32x4->control_data;
  542. snd_soc_write(codec, AIC32X4_RESET, 0x01);
  543. /* Power platform configuration */
  544. if (aic32x4->power_cfg & AIC32X4_PWR_MICBIAS_2075_LDOIN) {
  545. snd_soc_write(codec, AIC32X4_MICBIAS, AIC32X4_MICBIAS_LDOIN |
  546. AIC32X4_MICBIAS_2075V);
  547. }
  548. if (aic32x4->power_cfg & AIC32X4_PWR_AVDD_DVDD_WEAK_DISABLE) {
  549. snd_soc_write(codec, AIC32X4_PWRCFG, AIC32X4_AVDDWEAKDISABLE);
  550. }
  551. tmp_reg = (aic32x4->power_cfg & AIC32X4_PWR_AIC32X4_LDO_ENABLE) ?
  552. AIC32X4_LDOCTLEN : 0;
  553. snd_soc_write(codec, AIC32X4_LDOCTL, tmp_reg);
  554. tmp_reg = snd_soc_read(codec, AIC32X4_CMMODE);
  555. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_LDOIN_RANGE_18_36) {
  556. tmp_reg |= AIC32X4_LDOIN_18_36;
  557. }
  558. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_HP_LDOIN_POWERED) {
  559. tmp_reg |= AIC32X4_LDOIN2HP;
  560. }
  561. snd_soc_write(codec, AIC32X4_CMMODE, tmp_reg);
  562. /* Do DACs need to be swapped? */
  563. if (aic32x4->swapdacs) {
  564. snd_soc_write(codec, AIC32X4_DACSETUP, AIC32X4_LDAC2RCHN | AIC32X4_RDAC2LCHN);
  565. } else {
  566. snd_soc_write(codec, AIC32X4_DACSETUP, AIC32X4_LDAC2LCHN | AIC32X4_RDAC2RCHN);
  567. }
  568. /* Mic PGA routing */
  569. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_LMIC_IN2R_10K) {
  570. snd_soc_write(codec, AIC32X4_LMICPGANIN, AIC32X4_LMICPGANIN_IN2R_10K);
  571. }
  572. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_RMIC_IN1L_10K) {
  573. snd_soc_write(codec, AIC32X4_RMICPGANIN, AIC32X4_RMICPGANIN_IN1L_10K);
  574. }
  575. aic32x4_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  576. snd_soc_add_codec_controls(codec, aic32x4_snd_controls,
  577. ARRAY_SIZE(aic32x4_snd_controls));
  578. aic32x4_add_widgets(codec);
  579. return 0;
  580. }
  581. static int aic32x4_remove(struct snd_soc_codec *codec)
  582. {
  583. aic32x4_set_bias_level(codec, SND_SOC_BIAS_OFF);
  584. return 0;
  585. }
  586. static struct snd_soc_codec_driver soc_codec_dev_aic32x4 = {
  587. .read = aic32x4_read,
  588. .write = aic32x4_write,
  589. .probe = aic32x4_probe,
  590. .remove = aic32x4_remove,
  591. .suspend = aic32x4_suspend,
  592. .resume = aic32x4_resume,
  593. .set_bias_level = aic32x4_set_bias_level,
  594. };
  595. static __devinit int aic32x4_i2c_probe(struct i2c_client *i2c,
  596. const struct i2c_device_id *id)
  597. {
  598. struct aic32x4_pdata *pdata = i2c->dev.platform_data;
  599. struct aic32x4_priv *aic32x4;
  600. int ret;
  601. aic32x4 = devm_kzalloc(&i2c->dev, sizeof(struct aic32x4_priv),
  602. GFP_KERNEL);
  603. if (aic32x4 == NULL)
  604. return -ENOMEM;
  605. aic32x4->control_data = i2c;
  606. i2c_set_clientdata(i2c, aic32x4);
  607. if (pdata) {
  608. aic32x4->power_cfg = pdata->power_cfg;
  609. aic32x4->swapdacs = pdata->swapdacs;
  610. aic32x4->micpga_routing = pdata->micpga_routing;
  611. } else {
  612. aic32x4->power_cfg = 0;
  613. aic32x4->swapdacs = false;
  614. aic32x4->micpga_routing = 0;
  615. }
  616. ret = snd_soc_register_codec(&i2c->dev,
  617. &soc_codec_dev_aic32x4, &aic32x4_dai, 1);
  618. return ret;
  619. }
  620. static __devexit int aic32x4_i2c_remove(struct i2c_client *client)
  621. {
  622. snd_soc_unregister_codec(&client->dev);
  623. return 0;
  624. }
  625. static const struct i2c_device_id aic32x4_i2c_id[] = {
  626. { "tlv320aic32x4", 0 },
  627. { }
  628. };
  629. MODULE_DEVICE_TABLE(i2c, aic32x4_i2c_id);
  630. static struct i2c_driver aic32x4_i2c_driver = {
  631. .driver = {
  632. .name = "tlv320aic32x4",
  633. .owner = THIS_MODULE,
  634. },
  635. .probe = aic32x4_i2c_probe,
  636. .remove = __devexit_p(aic32x4_i2c_remove),
  637. .id_table = aic32x4_i2c_id,
  638. };
  639. static int __init aic32x4_modinit(void)
  640. {
  641. int ret = 0;
  642. ret = i2c_add_driver(&aic32x4_i2c_driver);
  643. if (ret != 0) {
  644. printk(KERN_ERR "Failed to register aic32x4 I2C driver: %d\n",
  645. ret);
  646. }
  647. return ret;
  648. }
  649. module_init(aic32x4_modinit);
  650. static void __exit aic32x4_exit(void)
  651. {
  652. i2c_del_driver(&aic32x4_i2c_driver);
  653. }
  654. module_exit(aic32x4_exit);
  655. MODULE_DESCRIPTION("ASoC tlv320aic32x4 codec driver");
  656. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  657. MODULE_LICENSE("GPL");