tlv320aic32x4.c 23 KB

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