tlv320aic32x4.c 23 KB

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