tlv320aic32x4.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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/gpio.h>
  31. #include <linux/i2c.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. struct regmap *regmap;
  59. u32 sysclk;
  60. u32 power_cfg;
  61. u32 micpga_routing;
  62. bool swapdacs;
  63. int rstn_gpio;
  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 const struct regmap_range_cfg aic32x4_regmap_pages[] = {
  230. {
  231. .selector_reg = 0,
  232. .selector_mask = 0xff,
  233. .window_start = 0,
  234. .window_len = 128,
  235. .range_min = AIC32X4_PAGE1,
  236. .range_max = AIC32X4_PAGE1 + 127,
  237. },
  238. };
  239. static const struct regmap_config aic32x4_regmap = {
  240. .reg_bits = 8,
  241. .val_bits = 8,
  242. .max_register = AIC32X4_RMICPGAVOL,
  243. .ranges = aic32x4_regmap_pages,
  244. .num_ranges = ARRAY_SIZE(aic32x4_regmap_pages),
  245. };
  246. static inline int aic32x4_get_divs(int mclk, int rate)
  247. {
  248. int i;
  249. for (i = 0; i < ARRAY_SIZE(aic32x4_divs); i++) {
  250. if ((aic32x4_divs[i].rate == rate)
  251. && (aic32x4_divs[i].mclk == mclk)) {
  252. return i;
  253. }
  254. }
  255. printk(KERN_ERR "aic32x4: master clock and sample rate is not supported\n");
  256. return -EINVAL;
  257. }
  258. static int aic32x4_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  259. int clk_id, unsigned int freq, int dir)
  260. {
  261. struct snd_soc_codec *codec = codec_dai->codec;
  262. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  263. switch (freq) {
  264. case AIC32X4_FREQ_12000000:
  265. case AIC32X4_FREQ_24000000:
  266. case AIC32X4_FREQ_25000000:
  267. aic32x4->sysclk = freq;
  268. return 0;
  269. }
  270. printk(KERN_ERR "aic32x4: invalid frequency to set DAI system clock\n");
  271. return -EINVAL;
  272. }
  273. static int aic32x4_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  274. {
  275. struct snd_soc_codec *codec = codec_dai->codec;
  276. u8 iface_reg_1;
  277. u8 iface_reg_2;
  278. u8 iface_reg_3;
  279. iface_reg_1 = snd_soc_read(codec, AIC32X4_IFACE1);
  280. iface_reg_1 = iface_reg_1 & ~(3 << 6 | 3 << 2);
  281. iface_reg_2 = snd_soc_read(codec, AIC32X4_IFACE2);
  282. iface_reg_2 = 0;
  283. iface_reg_3 = snd_soc_read(codec, AIC32X4_IFACE3);
  284. iface_reg_3 = iface_reg_3 & ~(1 << 3);
  285. /* set master/slave audio interface */
  286. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  287. case SND_SOC_DAIFMT_CBM_CFM:
  288. iface_reg_1 |= AIC32X4_BCLKMASTER | AIC32X4_WCLKMASTER;
  289. break;
  290. case SND_SOC_DAIFMT_CBS_CFS:
  291. break;
  292. default:
  293. printk(KERN_ERR "aic32x4: invalid DAI master/slave interface\n");
  294. return -EINVAL;
  295. }
  296. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  297. case SND_SOC_DAIFMT_I2S:
  298. break;
  299. case SND_SOC_DAIFMT_DSP_A:
  300. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  301. iface_reg_3 |= (1 << 3); /* invert bit clock */
  302. iface_reg_2 = 0x01; /* add offset 1 */
  303. break;
  304. case SND_SOC_DAIFMT_DSP_B:
  305. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  306. iface_reg_3 |= (1 << 3); /* invert bit clock */
  307. break;
  308. case SND_SOC_DAIFMT_RIGHT_J:
  309. iface_reg_1 |=
  310. (AIC32X4_RIGHT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  311. break;
  312. case SND_SOC_DAIFMT_LEFT_J:
  313. iface_reg_1 |=
  314. (AIC32X4_LEFT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  315. break;
  316. default:
  317. printk(KERN_ERR "aic32x4: invalid DAI interface format\n");
  318. return -EINVAL;
  319. }
  320. snd_soc_write(codec, AIC32X4_IFACE1, iface_reg_1);
  321. snd_soc_write(codec, AIC32X4_IFACE2, iface_reg_2);
  322. snd_soc_write(codec, AIC32X4_IFACE3, iface_reg_3);
  323. return 0;
  324. }
  325. static int aic32x4_hw_params(struct snd_pcm_substream *substream,
  326. struct snd_pcm_hw_params *params,
  327. struct snd_soc_dai *dai)
  328. {
  329. struct snd_soc_codec *codec = dai->codec;
  330. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  331. u8 data;
  332. int i;
  333. i = aic32x4_get_divs(aic32x4->sysclk, params_rate(params));
  334. if (i < 0) {
  335. printk(KERN_ERR "aic32x4: sampling rate not supported\n");
  336. return i;
  337. }
  338. /* Use PLL as CODEC_CLKIN and DAC_MOD_CLK as BDIV_CLKIN */
  339. snd_soc_write(codec, AIC32X4_CLKMUX, AIC32X4_PLLCLKIN);
  340. snd_soc_write(codec, AIC32X4_IFACE3, AIC32X4_DACMOD2BCLK);
  341. /* We will fix R value to 1 and will make P & J=K.D as varialble */
  342. data = snd_soc_read(codec, AIC32X4_PLLPR);
  343. data &= ~(7 << 4);
  344. snd_soc_write(codec, AIC32X4_PLLPR,
  345. (data | (aic32x4_divs[i].p_val << 4) | 0x01));
  346. snd_soc_write(codec, AIC32X4_PLLJ, aic32x4_divs[i].pll_j);
  347. snd_soc_write(codec, AIC32X4_PLLDMSB, (aic32x4_divs[i].pll_d >> 8));
  348. snd_soc_write(codec, AIC32X4_PLLDLSB,
  349. (aic32x4_divs[i].pll_d & 0xff));
  350. /* NDAC divider value */
  351. data = snd_soc_read(codec, AIC32X4_NDAC);
  352. data &= ~(0x7f);
  353. snd_soc_write(codec, AIC32X4_NDAC, data | aic32x4_divs[i].ndac);
  354. /* MDAC divider value */
  355. data = snd_soc_read(codec, AIC32X4_MDAC);
  356. data &= ~(0x7f);
  357. snd_soc_write(codec, AIC32X4_MDAC, data | aic32x4_divs[i].mdac);
  358. /* DOSR MSB & LSB values */
  359. snd_soc_write(codec, AIC32X4_DOSRMSB, aic32x4_divs[i].dosr >> 8);
  360. snd_soc_write(codec, AIC32X4_DOSRLSB,
  361. (aic32x4_divs[i].dosr & 0xff));
  362. /* NADC divider value */
  363. data = snd_soc_read(codec, AIC32X4_NADC);
  364. data &= ~(0x7f);
  365. snd_soc_write(codec, AIC32X4_NADC, data | aic32x4_divs[i].nadc);
  366. /* MADC divider value */
  367. data = snd_soc_read(codec, AIC32X4_MADC);
  368. data &= ~(0x7f);
  369. snd_soc_write(codec, AIC32X4_MADC, data | aic32x4_divs[i].madc);
  370. /* AOSR value */
  371. snd_soc_write(codec, AIC32X4_AOSR, aic32x4_divs[i].aosr);
  372. /* BCLK N divider */
  373. data = snd_soc_read(codec, AIC32X4_BCLKN);
  374. data &= ~(0x7f);
  375. snd_soc_write(codec, AIC32X4_BCLKN, data | aic32x4_divs[i].blck_N);
  376. data = snd_soc_read(codec, AIC32X4_IFACE1);
  377. data = data & ~(3 << 4);
  378. switch (params_format(params)) {
  379. case SNDRV_PCM_FORMAT_S16_LE:
  380. break;
  381. case SNDRV_PCM_FORMAT_S20_3LE:
  382. data |= (AIC32X4_WORD_LEN_20BITS << AIC32X4_DOSRMSB_SHIFT);
  383. break;
  384. case SNDRV_PCM_FORMAT_S24_LE:
  385. data |= (AIC32X4_WORD_LEN_24BITS << AIC32X4_DOSRMSB_SHIFT);
  386. break;
  387. case SNDRV_PCM_FORMAT_S32_LE:
  388. data |= (AIC32X4_WORD_LEN_32BITS << AIC32X4_DOSRMSB_SHIFT);
  389. break;
  390. }
  391. snd_soc_write(codec, AIC32X4_IFACE1, data);
  392. return 0;
  393. }
  394. static int aic32x4_mute(struct snd_soc_dai *dai, int mute)
  395. {
  396. struct snd_soc_codec *codec = dai->codec;
  397. u8 dac_reg;
  398. dac_reg = snd_soc_read(codec, AIC32X4_DACMUTE) & ~AIC32X4_MUTEON;
  399. if (mute)
  400. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg | AIC32X4_MUTEON);
  401. else
  402. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg);
  403. return 0;
  404. }
  405. static int aic32x4_set_bias_level(struct snd_soc_codec *codec,
  406. enum snd_soc_bias_level level)
  407. {
  408. switch (level) {
  409. case SND_SOC_BIAS_ON:
  410. /* Switch on PLL */
  411. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  412. AIC32X4_PLLEN, AIC32X4_PLLEN);
  413. /* Switch on NDAC Divider */
  414. snd_soc_update_bits(codec, AIC32X4_NDAC,
  415. AIC32X4_NDACEN, AIC32X4_NDACEN);
  416. /* Switch on MDAC Divider */
  417. snd_soc_update_bits(codec, AIC32X4_MDAC,
  418. AIC32X4_MDACEN, AIC32X4_MDACEN);
  419. /* Switch on NADC Divider */
  420. snd_soc_update_bits(codec, AIC32X4_NADC,
  421. AIC32X4_NADCEN, AIC32X4_NADCEN);
  422. /* Switch on MADC Divider */
  423. snd_soc_update_bits(codec, AIC32X4_MADC,
  424. AIC32X4_MADCEN, AIC32X4_MADCEN);
  425. /* Switch on BCLK_N Divider */
  426. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  427. AIC32X4_BCLKEN, AIC32X4_BCLKEN);
  428. break;
  429. case SND_SOC_BIAS_PREPARE:
  430. break;
  431. case SND_SOC_BIAS_STANDBY:
  432. /* Switch off PLL */
  433. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  434. AIC32X4_PLLEN, 0);
  435. /* Switch off NDAC Divider */
  436. snd_soc_update_bits(codec, AIC32X4_NDAC,
  437. AIC32X4_NDACEN, 0);
  438. /* Switch off MDAC Divider */
  439. snd_soc_update_bits(codec, AIC32X4_MDAC,
  440. AIC32X4_MDACEN, 0);
  441. /* Switch off NADC Divider */
  442. snd_soc_update_bits(codec, AIC32X4_NADC,
  443. AIC32X4_NADCEN, 0);
  444. /* Switch off MADC Divider */
  445. snd_soc_update_bits(codec, AIC32X4_MADC,
  446. AIC32X4_MADCEN, 0);
  447. /* Switch off BCLK_N Divider */
  448. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  449. AIC32X4_BCLKEN, 0);
  450. break;
  451. case SND_SOC_BIAS_OFF:
  452. break;
  453. }
  454. codec->dapm.bias_level = level;
  455. return 0;
  456. }
  457. #define AIC32X4_RATES SNDRV_PCM_RATE_8000_48000
  458. #define AIC32X4_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  459. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  460. static const struct snd_soc_dai_ops aic32x4_ops = {
  461. .hw_params = aic32x4_hw_params,
  462. .digital_mute = aic32x4_mute,
  463. .set_fmt = aic32x4_set_dai_fmt,
  464. .set_sysclk = aic32x4_set_dai_sysclk,
  465. };
  466. static struct snd_soc_dai_driver aic32x4_dai = {
  467. .name = "tlv320aic32x4-hifi",
  468. .playback = {
  469. .stream_name = "Playback",
  470. .channels_min = 1,
  471. .channels_max = 2,
  472. .rates = AIC32X4_RATES,
  473. .formats = AIC32X4_FORMATS,},
  474. .capture = {
  475. .stream_name = "Capture",
  476. .channels_min = 1,
  477. .channels_max = 2,
  478. .rates = AIC32X4_RATES,
  479. .formats = AIC32X4_FORMATS,},
  480. .ops = &aic32x4_ops,
  481. .symmetric_rates = 1,
  482. };
  483. static int aic32x4_suspend(struct snd_soc_codec *codec)
  484. {
  485. aic32x4_set_bias_level(codec, SND_SOC_BIAS_OFF);
  486. return 0;
  487. }
  488. static int aic32x4_resume(struct snd_soc_codec *codec)
  489. {
  490. aic32x4_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  491. return 0;
  492. }
  493. static int aic32x4_probe(struct snd_soc_codec *codec)
  494. {
  495. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  496. u32 tmp_reg;
  497. snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
  498. if (aic32x4->rstn_gpio >= 0) {
  499. ndelay(10);
  500. gpio_set_value(aic32x4->rstn_gpio, 1);
  501. }
  502. snd_soc_write(codec, AIC32X4_RESET, 0x01);
  503. /* Power platform configuration */
  504. if (aic32x4->power_cfg & AIC32X4_PWR_MICBIAS_2075_LDOIN) {
  505. snd_soc_write(codec, AIC32X4_MICBIAS, AIC32X4_MICBIAS_LDOIN |
  506. AIC32X4_MICBIAS_2075V);
  507. }
  508. if (aic32x4->power_cfg & AIC32X4_PWR_AVDD_DVDD_WEAK_DISABLE) {
  509. snd_soc_write(codec, AIC32X4_PWRCFG, AIC32X4_AVDDWEAKDISABLE);
  510. }
  511. tmp_reg = (aic32x4->power_cfg & AIC32X4_PWR_AIC32X4_LDO_ENABLE) ?
  512. AIC32X4_LDOCTLEN : 0;
  513. snd_soc_write(codec, AIC32X4_LDOCTL, tmp_reg);
  514. tmp_reg = snd_soc_read(codec, AIC32X4_CMMODE);
  515. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_LDOIN_RANGE_18_36) {
  516. tmp_reg |= AIC32X4_LDOIN_18_36;
  517. }
  518. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_HP_LDOIN_POWERED) {
  519. tmp_reg |= AIC32X4_LDOIN2HP;
  520. }
  521. snd_soc_write(codec, AIC32X4_CMMODE, tmp_reg);
  522. /* Do DACs need to be swapped? */
  523. if (aic32x4->swapdacs) {
  524. snd_soc_write(codec, AIC32X4_DACSETUP, AIC32X4_LDAC2RCHN | AIC32X4_RDAC2LCHN);
  525. } else {
  526. snd_soc_write(codec, AIC32X4_DACSETUP, AIC32X4_LDAC2LCHN | AIC32X4_RDAC2RCHN);
  527. }
  528. /* Mic PGA routing */
  529. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_LMIC_IN2R_10K) {
  530. snd_soc_write(codec, AIC32X4_LMICPGANIN, AIC32X4_LMICPGANIN_IN2R_10K);
  531. }
  532. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_RMIC_IN1L_10K) {
  533. snd_soc_write(codec, AIC32X4_RMICPGANIN, AIC32X4_RMICPGANIN_IN1L_10K);
  534. }
  535. aic32x4_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  536. /*
  537. * Workaround: for an unknown reason, the ADC needs to be powered up
  538. * and down for the first capture to work properly. It seems related to
  539. * a HW BUG or some kind of behavior not documented in the datasheet.
  540. */
  541. tmp_reg = snd_soc_read(codec, AIC32X4_ADCSETUP);
  542. snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg |
  543. AIC32X4_LADC_EN | AIC32X4_RADC_EN);
  544. snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg);
  545. return 0;
  546. }
  547. static int aic32x4_remove(struct snd_soc_codec *codec)
  548. {
  549. aic32x4_set_bias_level(codec, SND_SOC_BIAS_OFF);
  550. return 0;
  551. }
  552. static struct snd_soc_codec_driver soc_codec_dev_aic32x4 = {
  553. .probe = aic32x4_probe,
  554. .remove = aic32x4_remove,
  555. .suspend = aic32x4_suspend,
  556. .resume = aic32x4_resume,
  557. .set_bias_level = aic32x4_set_bias_level,
  558. .controls = aic32x4_snd_controls,
  559. .num_controls = ARRAY_SIZE(aic32x4_snd_controls),
  560. .dapm_widgets = aic32x4_dapm_widgets,
  561. .num_dapm_widgets = ARRAY_SIZE(aic32x4_dapm_widgets),
  562. .dapm_routes = aic32x4_dapm_routes,
  563. .num_dapm_routes = ARRAY_SIZE(aic32x4_dapm_routes),
  564. };
  565. static int aic32x4_i2c_probe(struct i2c_client *i2c,
  566. const struct i2c_device_id *id)
  567. {
  568. struct aic32x4_pdata *pdata = i2c->dev.platform_data;
  569. struct aic32x4_priv *aic32x4;
  570. int ret;
  571. aic32x4 = devm_kzalloc(&i2c->dev, sizeof(struct aic32x4_priv),
  572. GFP_KERNEL);
  573. if (aic32x4 == NULL)
  574. return -ENOMEM;
  575. aic32x4->regmap = devm_regmap_init_i2c(i2c, &aic32x4_regmap);
  576. if (IS_ERR(aic32x4->regmap))
  577. return PTR_ERR(aic32x4->regmap);
  578. i2c_set_clientdata(i2c, aic32x4);
  579. if (pdata) {
  580. aic32x4->power_cfg = pdata->power_cfg;
  581. aic32x4->swapdacs = pdata->swapdacs;
  582. aic32x4->micpga_routing = pdata->micpga_routing;
  583. aic32x4->rstn_gpio = pdata->rstn_gpio;
  584. } else {
  585. aic32x4->power_cfg = 0;
  586. aic32x4->swapdacs = false;
  587. aic32x4->micpga_routing = 0;
  588. aic32x4->rstn_gpio = -1;
  589. }
  590. if (aic32x4->rstn_gpio >= 0) {
  591. ret = devm_gpio_request_one(&i2c->dev, aic32x4->rstn_gpio,
  592. GPIOF_OUT_INIT_LOW, "tlv320aic32x4 rstn");
  593. if (ret != 0)
  594. return ret;
  595. }
  596. ret = snd_soc_register_codec(&i2c->dev,
  597. &soc_codec_dev_aic32x4, &aic32x4_dai, 1);
  598. return ret;
  599. }
  600. static int aic32x4_i2c_remove(struct i2c_client *client)
  601. {
  602. snd_soc_unregister_codec(&client->dev);
  603. return 0;
  604. }
  605. static const struct i2c_device_id aic32x4_i2c_id[] = {
  606. { "tlv320aic32x4", 0 },
  607. { }
  608. };
  609. MODULE_DEVICE_TABLE(i2c, aic32x4_i2c_id);
  610. static struct i2c_driver aic32x4_i2c_driver = {
  611. .driver = {
  612. .name = "tlv320aic32x4",
  613. .owner = THIS_MODULE,
  614. },
  615. .probe = aic32x4_i2c_probe,
  616. .remove = aic32x4_i2c_remove,
  617. .id_table = aic32x4_i2c_id,
  618. };
  619. module_i2c_driver(aic32x4_i2c_driver);
  620. MODULE_DESCRIPTION("ASoC tlv320aic32x4 codec driver");
  621. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  622. MODULE_LICENSE("GPL");