tlv320aic32x4.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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/of_gpio.h>
  32. #include <linux/i2c.h>
  33. #include <linux/cdev.h>
  34. #include <linux/slab.h>
  35. #include <linux/clk.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <sound/tlv320aic32x4.h>
  38. #include <sound/core.h>
  39. #include <sound/pcm.h>
  40. #include <sound/pcm_params.h>
  41. #include <sound/soc.h>
  42. #include <sound/soc-dapm.h>
  43. #include <sound/initval.h>
  44. #include <sound/tlv.h>
  45. #include "tlv320aic32x4.h"
  46. struct aic32x4_rate_divs {
  47. u32 mclk;
  48. u32 rate;
  49. u8 p_val;
  50. u8 pll_j;
  51. u16 pll_d;
  52. u16 dosr;
  53. u8 ndac;
  54. u8 mdac;
  55. u8 aosr;
  56. u8 nadc;
  57. u8 madc;
  58. u8 blck_N;
  59. };
  60. struct aic32x4_priv {
  61. struct regmap *regmap;
  62. u32 sysclk;
  63. u32 power_cfg;
  64. u32 micpga_routing;
  65. bool swapdacs;
  66. int rstn_gpio;
  67. struct clk *mclk;
  68. struct regulator *supply_ldo;
  69. struct regulator *supply_iov;
  70. struct regulator *supply_dv;
  71. struct regulator *supply_av;
  72. };
  73. /* 0dB min, 0.5dB steps */
  74. static DECLARE_TLV_DB_SCALE(tlv_step_0_5, 0, 50, 0);
  75. /* -63.5dB min, 0.5dB steps */
  76. static DECLARE_TLV_DB_SCALE(tlv_pcm, -6350, 50, 0);
  77. /* -6dB min, 1dB steps */
  78. static DECLARE_TLV_DB_SCALE(tlv_driver_gain, -600, 100, 0);
  79. /* -12dB min, 0.5dB steps */
  80. static DECLARE_TLV_DB_SCALE(tlv_adc_vol, -1200, 50, 0);
  81. static const struct snd_kcontrol_new aic32x4_snd_controls[] = {
  82. SOC_DOUBLE_R_S_TLV("PCM Playback Volume", AIC32X4_LDACVOL,
  83. AIC32X4_RDACVOL, 0, -0x7f, 0x30, 7, 0, tlv_pcm),
  84. SOC_DOUBLE_R_S_TLV("HP Driver Gain Volume", AIC32X4_HPLGAIN,
  85. AIC32X4_HPRGAIN, 0, -0x6, 0x1d, 5, 0,
  86. tlv_driver_gain),
  87. SOC_DOUBLE_R_S_TLV("LO Driver Gain Volume", AIC32X4_LOLGAIN,
  88. AIC32X4_LORGAIN, 0, -0x6, 0x1d, 5, 0,
  89. tlv_driver_gain),
  90. SOC_DOUBLE_R("HP DAC Playback Switch", AIC32X4_HPLGAIN,
  91. AIC32X4_HPRGAIN, 6, 0x01, 1),
  92. SOC_DOUBLE_R("LO DAC Playback Switch", AIC32X4_LOLGAIN,
  93. AIC32X4_LORGAIN, 6, 0x01, 1),
  94. SOC_DOUBLE_R("Mic PGA Switch", AIC32X4_LMICPGAVOL,
  95. AIC32X4_RMICPGAVOL, 7, 0x01, 1),
  96. SOC_SINGLE("ADCFGA Left Mute Switch", AIC32X4_ADCFGA, 7, 1, 0),
  97. SOC_SINGLE("ADCFGA Right Mute Switch", AIC32X4_ADCFGA, 3, 1, 0),
  98. SOC_DOUBLE_R_S_TLV("ADC Level Volume", AIC32X4_LADCVOL,
  99. AIC32X4_RADCVOL, 0, -0x18, 0x28, 6, 0, tlv_adc_vol),
  100. SOC_DOUBLE_R_TLV("PGA Level Volume", AIC32X4_LMICPGAVOL,
  101. AIC32X4_RMICPGAVOL, 0, 0x5f, 0, tlv_step_0_5),
  102. SOC_SINGLE("Auto-mute Switch", AIC32X4_DACMUTE, 4, 7, 0),
  103. SOC_SINGLE("AGC Left Switch", AIC32X4_LAGC1, 7, 1, 0),
  104. SOC_SINGLE("AGC Right Switch", AIC32X4_RAGC1, 7, 1, 0),
  105. SOC_DOUBLE_R("AGC Target Level", AIC32X4_LAGC1, AIC32X4_RAGC1,
  106. 4, 0x07, 0),
  107. SOC_DOUBLE_R("AGC Gain Hysteresis", AIC32X4_LAGC1, AIC32X4_RAGC1,
  108. 0, 0x03, 0),
  109. SOC_DOUBLE_R("AGC Hysteresis", AIC32X4_LAGC2, AIC32X4_RAGC2,
  110. 6, 0x03, 0),
  111. SOC_DOUBLE_R("AGC Noise Threshold", AIC32X4_LAGC2, AIC32X4_RAGC2,
  112. 1, 0x1F, 0),
  113. SOC_DOUBLE_R("AGC Max PGA", AIC32X4_LAGC3, AIC32X4_RAGC3,
  114. 0, 0x7F, 0),
  115. SOC_DOUBLE_R("AGC Attack Time", AIC32X4_LAGC4, AIC32X4_RAGC4,
  116. 3, 0x1F, 0),
  117. SOC_DOUBLE_R("AGC Decay Time", AIC32X4_LAGC5, AIC32X4_RAGC5,
  118. 3, 0x1F, 0),
  119. SOC_DOUBLE_R("AGC Noise Debounce", AIC32X4_LAGC6, AIC32X4_RAGC6,
  120. 0, 0x1F, 0),
  121. SOC_DOUBLE_R("AGC Signal Debounce", AIC32X4_LAGC7, AIC32X4_RAGC7,
  122. 0, 0x0F, 0),
  123. };
  124. static const struct aic32x4_rate_divs aic32x4_divs[] = {
  125. /* 8k rate */
  126. {AIC32X4_FREQ_12000000, 8000, 1, 7, 6800, 768, 5, 3, 128, 5, 18, 24},
  127. {AIC32X4_FREQ_24000000, 8000, 2, 7, 6800, 768, 15, 1, 64, 45, 4, 24},
  128. {AIC32X4_FREQ_25000000, 8000, 2, 7, 3728, 768, 15, 1, 64, 45, 4, 24},
  129. /* 11.025k rate */
  130. {AIC32X4_FREQ_12000000, 11025, 1, 7, 5264, 512, 8, 2, 128, 8, 8, 16},
  131. {AIC32X4_FREQ_24000000, 11025, 2, 7, 5264, 512, 16, 1, 64, 32, 4, 16},
  132. /* 16k rate */
  133. {AIC32X4_FREQ_12000000, 16000, 1, 7, 6800, 384, 5, 3, 128, 5, 9, 12},
  134. {AIC32X4_FREQ_24000000, 16000, 2, 7, 6800, 384, 15, 1, 64, 18, 5, 12},
  135. {AIC32X4_FREQ_25000000, 16000, 2, 7, 3728, 384, 15, 1, 64, 18, 5, 12},
  136. /* 22.05k rate */
  137. {AIC32X4_FREQ_12000000, 22050, 1, 7, 5264, 256, 4, 4, 128, 4, 8, 8},
  138. {AIC32X4_FREQ_24000000, 22050, 2, 7, 5264, 256, 16, 1, 64, 16, 4, 8},
  139. {AIC32X4_FREQ_25000000, 22050, 2, 7, 2253, 256, 16, 1, 64, 16, 4, 8},
  140. /* 32k rate */
  141. {AIC32X4_FREQ_12000000, 32000, 1, 7, 1680, 192, 2, 7, 64, 2, 21, 6},
  142. {AIC32X4_FREQ_24000000, 32000, 2, 7, 1680, 192, 7, 2, 64, 7, 6, 6},
  143. /* 44.1k rate */
  144. {AIC32X4_FREQ_12000000, 44100, 1, 7, 5264, 128, 2, 8, 128, 2, 8, 4},
  145. {AIC32X4_FREQ_24000000, 44100, 2, 7, 5264, 128, 8, 2, 64, 8, 4, 4},
  146. {AIC32X4_FREQ_25000000, 44100, 2, 7, 2253, 128, 8, 2, 64, 8, 4, 4},
  147. /* 48k rate */
  148. {AIC32X4_FREQ_12000000, 48000, 1, 8, 1920, 128, 2, 8, 128, 2, 8, 4},
  149. {AIC32X4_FREQ_24000000, 48000, 2, 8, 1920, 128, 8, 2, 64, 8, 4, 4},
  150. {AIC32X4_FREQ_25000000, 48000, 2, 7, 8643, 128, 8, 2, 64, 8, 4, 4}
  151. };
  152. static const struct snd_kcontrol_new hpl_output_mixer_controls[] = {
  153. SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_HPLROUTE, 3, 1, 0),
  154. SOC_DAPM_SINGLE("IN1_L Switch", AIC32X4_HPLROUTE, 2, 1, 0),
  155. };
  156. static const struct snd_kcontrol_new hpr_output_mixer_controls[] = {
  157. SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_HPRROUTE, 3, 1, 0),
  158. SOC_DAPM_SINGLE("IN1_R Switch", AIC32X4_HPRROUTE, 2, 1, 0),
  159. };
  160. static const struct snd_kcontrol_new lol_output_mixer_controls[] = {
  161. SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_LOLROUTE, 3, 1, 0),
  162. };
  163. static const struct snd_kcontrol_new lor_output_mixer_controls[] = {
  164. SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_LORROUTE, 3, 1, 0),
  165. };
  166. static const struct snd_kcontrol_new left_input_mixer_controls[] = {
  167. SOC_DAPM_SINGLE("IN1_L P Switch", AIC32X4_LMICPGAPIN, 6, 1, 0),
  168. SOC_DAPM_SINGLE("IN2_L P Switch", AIC32X4_LMICPGAPIN, 4, 1, 0),
  169. SOC_DAPM_SINGLE("IN3_L P Switch", AIC32X4_LMICPGAPIN, 2, 1, 0),
  170. };
  171. static const struct snd_kcontrol_new right_input_mixer_controls[] = {
  172. SOC_DAPM_SINGLE("IN1_R P Switch", AIC32X4_RMICPGAPIN, 6, 1, 0),
  173. SOC_DAPM_SINGLE("IN2_R P Switch", AIC32X4_RMICPGAPIN, 4, 1, 0),
  174. SOC_DAPM_SINGLE("IN3_R P Switch", AIC32X4_RMICPGAPIN, 2, 1, 0),
  175. };
  176. static const struct snd_soc_dapm_widget aic32x4_dapm_widgets[] = {
  177. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", AIC32X4_DACSETUP, 7, 0),
  178. SND_SOC_DAPM_MIXER("HPL Output Mixer", SND_SOC_NOPM, 0, 0,
  179. &hpl_output_mixer_controls[0],
  180. ARRAY_SIZE(hpl_output_mixer_controls)),
  181. SND_SOC_DAPM_PGA("HPL Power", AIC32X4_OUTPWRCTL, 5, 0, NULL, 0),
  182. SND_SOC_DAPM_MIXER("LOL Output Mixer", SND_SOC_NOPM, 0, 0,
  183. &lol_output_mixer_controls[0],
  184. ARRAY_SIZE(lol_output_mixer_controls)),
  185. SND_SOC_DAPM_PGA("LOL Power", AIC32X4_OUTPWRCTL, 3, 0, NULL, 0),
  186. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", AIC32X4_DACSETUP, 6, 0),
  187. SND_SOC_DAPM_MIXER("HPR Output Mixer", SND_SOC_NOPM, 0, 0,
  188. &hpr_output_mixer_controls[0],
  189. ARRAY_SIZE(hpr_output_mixer_controls)),
  190. SND_SOC_DAPM_PGA("HPR Power", AIC32X4_OUTPWRCTL, 4, 0, NULL, 0),
  191. SND_SOC_DAPM_MIXER("LOR Output Mixer", SND_SOC_NOPM, 0, 0,
  192. &lor_output_mixer_controls[0],
  193. ARRAY_SIZE(lor_output_mixer_controls)),
  194. SND_SOC_DAPM_PGA("LOR Power", AIC32X4_OUTPWRCTL, 2, 0, NULL, 0),
  195. SND_SOC_DAPM_MIXER("Left Input Mixer", SND_SOC_NOPM, 0, 0,
  196. &left_input_mixer_controls[0],
  197. ARRAY_SIZE(left_input_mixer_controls)),
  198. SND_SOC_DAPM_MIXER("Right Input Mixer", SND_SOC_NOPM, 0, 0,
  199. &right_input_mixer_controls[0],
  200. ARRAY_SIZE(right_input_mixer_controls)),
  201. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", AIC32X4_ADCSETUP, 7, 0),
  202. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", AIC32X4_ADCSETUP, 6, 0),
  203. SND_SOC_DAPM_MICBIAS("Mic Bias", AIC32X4_MICBIAS, 6, 0),
  204. SND_SOC_DAPM_OUTPUT("HPL"),
  205. SND_SOC_DAPM_OUTPUT("HPR"),
  206. SND_SOC_DAPM_OUTPUT("LOL"),
  207. SND_SOC_DAPM_OUTPUT("LOR"),
  208. SND_SOC_DAPM_INPUT("IN1_L"),
  209. SND_SOC_DAPM_INPUT("IN1_R"),
  210. SND_SOC_DAPM_INPUT("IN2_L"),
  211. SND_SOC_DAPM_INPUT("IN2_R"),
  212. SND_SOC_DAPM_INPUT("IN3_L"),
  213. SND_SOC_DAPM_INPUT("IN3_R"),
  214. };
  215. static const struct snd_soc_dapm_route aic32x4_dapm_routes[] = {
  216. /* Left Output */
  217. {"HPL Output Mixer", "L_DAC Switch", "Left DAC"},
  218. {"HPL Output Mixer", "IN1_L Switch", "IN1_L"},
  219. {"HPL Power", NULL, "HPL Output Mixer"},
  220. {"HPL", NULL, "HPL Power"},
  221. {"LOL Output Mixer", "L_DAC Switch", "Left DAC"},
  222. {"LOL Power", NULL, "LOL Output Mixer"},
  223. {"LOL", NULL, "LOL Power"},
  224. /* Right Output */
  225. {"HPR Output Mixer", "R_DAC Switch", "Right DAC"},
  226. {"HPR Output Mixer", "IN1_R Switch", "IN1_R"},
  227. {"HPR Power", NULL, "HPR Output Mixer"},
  228. {"HPR", NULL, "HPR Power"},
  229. {"LOR Output Mixer", "R_DAC Switch", "Right DAC"},
  230. {"LOR Power", NULL, "LOR Output Mixer"},
  231. {"LOR", NULL, "LOR Power"},
  232. /* Left input */
  233. {"Left Input Mixer", "IN1_L P Switch", "IN1_L"},
  234. {"Left Input Mixer", "IN2_L P Switch", "IN2_L"},
  235. {"Left Input Mixer", "IN3_L P Switch", "IN3_L"},
  236. {"Left ADC", NULL, "Left Input Mixer"},
  237. /* Right Input */
  238. {"Right Input Mixer", "IN1_R P Switch", "IN1_R"},
  239. {"Right Input Mixer", "IN2_R P Switch", "IN2_R"},
  240. {"Right Input Mixer", "IN3_R P Switch", "IN3_R"},
  241. {"Right ADC", NULL, "Right Input Mixer"},
  242. };
  243. static const struct regmap_range_cfg aic32x4_regmap_pages[] = {
  244. {
  245. .selector_reg = 0,
  246. .selector_mask = 0xff,
  247. .window_start = 0,
  248. .window_len = 128,
  249. .range_min = 0,
  250. .range_max = AIC32X4_RMICPGAVOL,
  251. },
  252. };
  253. static const struct regmap_config aic32x4_regmap = {
  254. .reg_bits = 8,
  255. .val_bits = 8,
  256. .max_register = AIC32X4_RMICPGAVOL,
  257. .ranges = aic32x4_regmap_pages,
  258. .num_ranges = ARRAY_SIZE(aic32x4_regmap_pages),
  259. };
  260. static inline int aic32x4_get_divs(int mclk, int rate)
  261. {
  262. int i;
  263. for (i = 0; i < ARRAY_SIZE(aic32x4_divs); i++) {
  264. if ((aic32x4_divs[i].rate == rate)
  265. && (aic32x4_divs[i].mclk == mclk)) {
  266. return i;
  267. }
  268. }
  269. printk(KERN_ERR "aic32x4: master clock and sample rate is not supported\n");
  270. return -EINVAL;
  271. }
  272. static int aic32x4_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  273. int clk_id, unsigned int freq, int dir)
  274. {
  275. struct snd_soc_codec *codec = codec_dai->codec;
  276. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  277. switch (freq) {
  278. case AIC32X4_FREQ_12000000:
  279. case AIC32X4_FREQ_24000000:
  280. case AIC32X4_FREQ_25000000:
  281. aic32x4->sysclk = freq;
  282. return 0;
  283. }
  284. printk(KERN_ERR "aic32x4: invalid frequency to set DAI system clock\n");
  285. return -EINVAL;
  286. }
  287. static int aic32x4_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  288. {
  289. struct snd_soc_codec *codec = codec_dai->codec;
  290. u8 iface_reg_1;
  291. u8 iface_reg_2;
  292. u8 iface_reg_3;
  293. iface_reg_1 = snd_soc_read(codec, AIC32X4_IFACE1);
  294. iface_reg_1 = iface_reg_1 & ~(3 << 6 | 3 << 2);
  295. iface_reg_2 = snd_soc_read(codec, AIC32X4_IFACE2);
  296. iface_reg_2 = 0;
  297. iface_reg_3 = snd_soc_read(codec, AIC32X4_IFACE3);
  298. iface_reg_3 = iface_reg_3 & ~(1 << 3);
  299. /* set master/slave audio interface */
  300. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  301. case SND_SOC_DAIFMT_CBM_CFM:
  302. iface_reg_1 |= AIC32X4_BCLKMASTER | AIC32X4_WCLKMASTER;
  303. break;
  304. case SND_SOC_DAIFMT_CBS_CFS:
  305. break;
  306. default:
  307. printk(KERN_ERR "aic32x4: invalid DAI master/slave interface\n");
  308. return -EINVAL;
  309. }
  310. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  311. case SND_SOC_DAIFMT_I2S:
  312. break;
  313. case SND_SOC_DAIFMT_DSP_A:
  314. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  315. iface_reg_3 |= (1 << 3); /* invert bit clock */
  316. iface_reg_2 = 0x01; /* add offset 1 */
  317. break;
  318. case SND_SOC_DAIFMT_DSP_B:
  319. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  320. iface_reg_3 |= (1 << 3); /* invert bit clock */
  321. break;
  322. case SND_SOC_DAIFMT_RIGHT_J:
  323. iface_reg_1 |=
  324. (AIC32X4_RIGHT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  325. break;
  326. case SND_SOC_DAIFMT_LEFT_J:
  327. iface_reg_1 |=
  328. (AIC32X4_LEFT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  329. break;
  330. default:
  331. printk(KERN_ERR "aic32x4: invalid DAI interface format\n");
  332. return -EINVAL;
  333. }
  334. snd_soc_write(codec, AIC32X4_IFACE1, iface_reg_1);
  335. snd_soc_write(codec, AIC32X4_IFACE2, iface_reg_2);
  336. snd_soc_write(codec, AIC32X4_IFACE3, iface_reg_3);
  337. return 0;
  338. }
  339. static int aic32x4_hw_params(struct snd_pcm_substream *substream,
  340. struct snd_pcm_hw_params *params,
  341. struct snd_soc_dai *dai)
  342. {
  343. struct snd_soc_codec *codec = dai->codec;
  344. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  345. u8 data;
  346. int i;
  347. i = aic32x4_get_divs(aic32x4->sysclk, params_rate(params));
  348. if (i < 0) {
  349. printk(KERN_ERR "aic32x4: sampling rate not supported\n");
  350. return i;
  351. }
  352. /* Use PLL as CODEC_CLKIN and DAC_MOD_CLK as BDIV_CLKIN */
  353. snd_soc_write(codec, AIC32X4_CLKMUX, AIC32X4_PLLCLKIN);
  354. snd_soc_write(codec, AIC32X4_IFACE3, AIC32X4_DACMOD2BCLK);
  355. /* We will fix R value to 1 and will make P & J=K.D as varialble */
  356. data = snd_soc_read(codec, AIC32X4_PLLPR);
  357. data &= ~(7 << 4);
  358. snd_soc_write(codec, AIC32X4_PLLPR,
  359. (data | (aic32x4_divs[i].p_val << 4) | 0x01));
  360. snd_soc_write(codec, AIC32X4_PLLJ, aic32x4_divs[i].pll_j);
  361. snd_soc_write(codec, AIC32X4_PLLDMSB, (aic32x4_divs[i].pll_d >> 8));
  362. snd_soc_write(codec, AIC32X4_PLLDLSB,
  363. (aic32x4_divs[i].pll_d & 0xff));
  364. /* NDAC divider value */
  365. data = snd_soc_read(codec, AIC32X4_NDAC);
  366. data &= ~(0x7f);
  367. snd_soc_write(codec, AIC32X4_NDAC, data | aic32x4_divs[i].ndac);
  368. /* MDAC divider value */
  369. data = snd_soc_read(codec, AIC32X4_MDAC);
  370. data &= ~(0x7f);
  371. snd_soc_write(codec, AIC32X4_MDAC, data | aic32x4_divs[i].mdac);
  372. /* DOSR MSB & LSB values */
  373. snd_soc_write(codec, AIC32X4_DOSRMSB, aic32x4_divs[i].dosr >> 8);
  374. snd_soc_write(codec, AIC32X4_DOSRLSB,
  375. (aic32x4_divs[i].dosr & 0xff));
  376. /* NADC divider value */
  377. data = snd_soc_read(codec, AIC32X4_NADC);
  378. data &= ~(0x7f);
  379. snd_soc_write(codec, AIC32X4_NADC, data | aic32x4_divs[i].nadc);
  380. /* MADC divider value */
  381. data = snd_soc_read(codec, AIC32X4_MADC);
  382. data &= ~(0x7f);
  383. snd_soc_write(codec, AIC32X4_MADC, data | aic32x4_divs[i].madc);
  384. /* AOSR value */
  385. snd_soc_write(codec, AIC32X4_AOSR, aic32x4_divs[i].aosr);
  386. /* BCLK N divider */
  387. data = snd_soc_read(codec, AIC32X4_BCLKN);
  388. data &= ~(0x7f);
  389. snd_soc_write(codec, AIC32X4_BCLKN, data | aic32x4_divs[i].blck_N);
  390. data = snd_soc_read(codec, AIC32X4_IFACE1);
  391. data = data & ~(3 << 4);
  392. switch (params_width(params)) {
  393. case 16:
  394. break;
  395. case 20:
  396. data |= (AIC32X4_WORD_LEN_20BITS << AIC32X4_DOSRMSB_SHIFT);
  397. break;
  398. case 24:
  399. data |= (AIC32X4_WORD_LEN_24BITS << AIC32X4_DOSRMSB_SHIFT);
  400. break;
  401. case 32:
  402. data |= (AIC32X4_WORD_LEN_32BITS << AIC32X4_DOSRMSB_SHIFT);
  403. break;
  404. }
  405. snd_soc_write(codec, AIC32X4_IFACE1, data);
  406. if (params_channels(params) == 1) {
  407. data = AIC32X4_RDAC2LCHN | AIC32X4_LDAC2LCHN;
  408. } else {
  409. if (aic32x4->swapdacs)
  410. data = AIC32X4_RDAC2LCHN | AIC32X4_LDAC2RCHN;
  411. else
  412. data = AIC32X4_LDAC2LCHN | AIC32X4_RDAC2RCHN;
  413. }
  414. snd_soc_update_bits(codec, AIC32X4_DACSETUP, AIC32X4_DAC_CHAN_MASK,
  415. data);
  416. return 0;
  417. }
  418. static int aic32x4_mute(struct snd_soc_dai *dai, int mute)
  419. {
  420. struct snd_soc_codec *codec = dai->codec;
  421. u8 dac_reg;
  422. dac_reg = snd_soc_read(codec, AIC32X4_DACMUTE) & ~AIC32X4_MUTEON;
  423. if (mute)
  424. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg | AIC32X4_MUTEON);
  425. else
  426. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg);
  427. return 0;
  428. }
  429. static int aic32x4_set_bias_level(struct snd_soc_codec *codec,
  430. enum snd_soc_bias_level level)
  431. {
  432. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  433. int ret;
  434. switch (level) {
  435. case SND_SOC_BIAS_ON:
  436. /* Switch on master clock */
  437. ret = clk_prepare_enable(aic32x4->mclk);
  438. if (ret) {
  439. dev_err(codec->dev, "Failed to enable master clock\n");
  440. return ret;
  441. }
  442. /* Switch on PLL */
  443. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  444. AIC32X4_PLLEN, AIC32X4_PLLEN);
  445. /* Switch on NDAC Divider */
  446. snd_soc_update_bits(codec, AIC32X4_NDAC,
  447. AIC32X4_NDACEN, AIC32X4_NDACEN);
  448. /* Switch on MDAC Divider */
  449. snd_soc_update_bits(codec, AIC32X4_MDAC,
  450. AIC32X4_MDACEN, AIC32X4_MDACEN);
  451. /* Switch on NADC Divider */
  452. snd_soc_update_bits(codec, AIC32X4_NADC,
  453. AIC32X4_NADCEN, AIC32X4_NADCEN);
  454. /* Switch on MADC Divider */
  455. snd_soc_update_bits(codec, AIC32X4_MADC,
  456. AIC32X4_MADCEN, AIC32X4_MADCEN);
  457. /* Switch on BCLK_N Divider */
  458. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  459. AIC32X4_BCLKEN, AIC32X4_BCLKEN);
  460. break;
  461. case SND_SOC_BIAS_PREPARE:
  462. break;
  463. case SND_SOC_BIAS_STANDBY:
  464. /* Switch off BCLK_N Divider */
  465. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  466. AIC32X4_BCLKEN, 0);
  467. /* Switch off MADC Divider */
  468. snd_soc_update_bits(codec, AIC32X4_MADC,
  469. AIC32X4_MADCEN, 0);
  470. /* Switch off NADC Divider */
  471. snd_soc_update_bits(codec, AIC32X4_NADC,
  472. AIC32X4_NADCEN, 0);
  473. /* Switch off MDAC Divider */
  474. snd_soc_update_bits(codec, AIC32X4_MDAC,
  475. AIC32X4_MDACEN, 0);
  476. /* Switch off NDAC Divider */
  477. snd_soc_update_bits(codec, AIC32X4_NDAC,
  478. AIC32X4_NDACEN, 0);
  479. /* Switch off PLL */
  480. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  481. AIC32X4_PLLEN, 0);
  482. /* Switch off master clock */
  483. clk_disable_unprepare(aic32x4->mclk);
  484. break;
  485. case SND_SOC_BIAS_OFF:
  486. break;
  487. }
  488. codec->dapm.bias_level = level;
  489. return 0;
  490. }
  491. #define AIC32X4_RATES SNDRV_PCM_RATE_8000_48000
  492. #define AIC32X4_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  493. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  494. static const struct snd_soc_dai_ops aic32x4_ops = {
  495. .hw_params = aic32x4_hw_params,
  496. .digital_mute = aic32x4_mute,
  497. .set_fmt = aic32x4_set_dai_fmt,
  498. .set_sysclk = aic32x4_set_dai_sysclk,
  499. };
  500. static struct snd_soc_dai_driver aic32x4_dai = {
  501. .name = "tlv320aic32x4-hifi",
  502. .playback = {
  503. .stream_name = "Playback",
  504. .channels_min = 1,
  505. .channels_max = 2,
  506. .rates = AIC32X4_RATES,
  507. .formats = AIC32X4_FORMATS,},
  508. .capture = {
  509. .stream_name = "Capture",
  510. .channels_min = 1,
  511. .channels_max = 2,
  512. .rates = AIC32X4_RATES,
  513. .formats = AIC32X4_FORMATS,},
  514. .ops = &aic32x4_ops,
  515. .symmetric_rates = 1,
  516. };
  517. static int aic32x4_suspend(struct snd_soc_codec *codec)
  518. {
  519. aic32x4_set_bias_level(codec, SND_SOC_BIAS_OFF);
  520. return 0;
  521. }
  522. static int aic32x4_resume(struct snd_soc_codec *codec)
  523. {
  524. aic32x4_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  525. return 0;
  526. }
  527. static int aic32x4_probe(struct snd_soc_codec *codec)
  528. {
  529. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  530. u32 tmp_reg;
  531. if (gpio_is_valid(aic32x4->rstn_gpio)) {
  532. ndelay(10);
  533. gpio_set_value(aic32x4->rstn_gpio, 1);
  534. }
  535. snd_soc_write(codec, AIC32X4_RESET, 0x01);
  536. /* Power platform configuration */
  537. if (aic32x4->power_cfg & AIC32X4_PWR_MICBIAS_2075_LDOIN) {
  538. snd_soc_write(codec, AIC32X4_MICBIAS, AIC32X4_MICBIAS_LDOIN |
  539. AIC32X4_MICBIAS_2075V);
  540. }
  541. if (aic32x4->power_cfg & AIC32X4_PWR_AVDD_DVDD_WEAK_DISABLE)
  542. snd_soc_write(codec, AIC32X4_PWRCFG, AIC32X4_AVDDWEAKDISABLE);
  543. tmp_reg = (aic32x4->power_cfg & AIC32X4_PWR_AIC32X4_LDO_ENABLE) ?
  544. AIC32X4_LDOCTLEN : 0;
  545. snd_soc_write(codec, AIC32X4_LDOCTL, tmp_reg);
  546. tmp_reg = snd_soc_read(codec, AIC32X4_CMMODE);
  547. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_LDOIN_RANGE_18_36)
  548. tmp_reg |= AIC32X4_LDOIN_18_36;
  549. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_HP_LDOIN_POWERED)
  550. tmp_reg |= AIC32X4_LDOIN2HP;
  551. snd_soc_write(codec, AIC32X4_CMMODE, tmp_reg);
  552. /* Mic PGA routing */
  553. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_LMIC_IN2R_10K)
  554. snd_soc_write(codec, AIC32X4_LMICPGANIN,
  555. AIC32X4_LMICPGANIN_IN2R_10K);
  556. else
  557. snd_soc_write(codec, AIC32X4_LMICPGANIN,
  558. AIC32X4_LMICPGANIN_CM1L_10K);
  559. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_RMIC_IN1L_10K)
  560. snd_soc_write(codec, AIC32X4_RMICPGANIN,
  561. AIC32X4_RMICPGANIN_IN1L_10K);
  562. else
  563. snd_soc_write(codec, AIC32X4_RMICPGANIN,
  564. AIC32X4_RMICPGANIN_CM1R_10K);
  565. aic32x4_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  566. /*
  567. * Workaround: for an unknown reason, the ADC needs to be powered up
  568. * and down for the first capture to work properly. It seems related to
  569. * a HW BUG or some kind of behavior not documented in the datasheet.
  570. */
  571. tmp_reg = snd_soc_read(codec, AIC32X4_ADCSETUP);
  572. snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg |
  573. AIC32X4_LADC_EN | AIC32X4_RADC_EN);
  574. snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg);
  575. return 0;
  576. }
  577. static int aic32x4_remove(struct snd_soc_codec *codec)
  578. {
  579. aic32x4_set_bias_level(codec, SND_SOC_BIAS_OFF);
  580. return 0;
  581. }
  582. static struct snd_soc_codec_driver soc_codec_dev_aic32x4 = {
  583. .probe = aic32x4_probe,
  584. .remove = aic32x4_remove,
  585. .suspend = aic32x4_suspend,
  586. .resume = aic32x4_resume,
  587. .set_bias_level = aic32x4_set_bias_level,
  588. .controls = aic32x4_snd_controls,
  589. .num_controls = ARRAY_SIZE(aic32x4_snd_controls),
  590. .dapm_widgets = aic32x4_dapm_widgets,
  591. .num_dapm_widgets = ARRAY_SIZE(aic32x4_dapm_widgets),
  592. .dapm_routes = aic32x4_dapm_routes,
  593. .num_dapm_routes = ARRAY_SIZE(aic32x4_dapm_routes),
  594. };
  595. static int aic32x4_parse_dt(struct aic32x4_priv *aic32x4,
  596. struct device_node *np)
  597. {
  598. aic32x4->swapdacs = false;
  599. aic32x4->micpga_routing = 0;
  600. aic32x4->rstn_gpio = of_get_named_gpio(np, "reset-gpios", 0);
  601. return 0;
  602. }
  603. static void aic32x4_disable_regulators(struct aic32x4_priv *aic32x4)
  604. {
  605. regulator_disable(aic32x4->supply_iov);
  606. if (!IS_ERR(aic32x4->supply_ldo))
  607. regulator_disable(aic32x4->supply_ldo);
  608. if (!IS_ERR(aic32x4->supply_dv))
  609. regulator_disable(aic32x4->supply_dv);
  610. if (!IS_ERR(aic32x4->supply_av))
  611. regulator_disable(aic32x4->supply_av);
  612. }
  613. static int aic32x4_setup_regulators(struct device *dev,
  614. struct aic32x4_priv *aic32x4)
  615. {
  616. int ret = 0;
  617. aic32x4->supply_ldo = devm_regulator_get_optional(dev, "ldoin");
  618. aic32x4->supply_iov = devm_regulator_get(dev, "iov");
  619. aic32x4->supply_dv = devm_regulator_get_optional(dev, "dv");
  620. aic32x4->supply_av = devm_regulator_get_optional(dev, "av");
  621. /* Check if the regulator requirements are fulfilled */
  622. if (IS_ERR(aic32x4->supply_iov)) {
  623. dev_err(dev, "Missing supply 'iov'\n");
  624. return PTR_ERR(aic32x4->supply_iov);
  625. }
  626. if (IS_ERR(aic32x4->supply_ldo)) {
  627. if (PTR_ERR(aic32x4->supply_ldo) == -EPROBE_DEFER)
  628. return -EPROBE_DEFER;
  629. if (IS_ERR(aic32x4->supply_dv)) {
  630. dev_err(dev, "Missing supply 'dv' or 'ldoin'\n");
  631. return PTR_ERR(aic32x4->supply_dv);
  632. }
  633. if (IS_ERR(aic32x4->supply_av)) {
  634. dev_err(dev, "Missing supply 'av' or 'ldoin'\n");
  635. return PTR_ERR(aic32x4->supply_av);
  636. }
  637. } else {
  638. if (IS_ERR(aic32x4->supply_dv) &&
  639. PTR_ERR(aic32x4->supply_dv) == -EPROBE_DEFER)
  640. return -EPROBE_DEFER;
  641. if (IS_ERR(aic32x4->supply_av) &&
  642. PTR_ERR(aic32x4->supply_av) == -EPROBE_DEFER)
  643. return -EPROBE_DEFER;
  644. }
  645. ret = regulator_enable(aic32x4->supply_iov);
  646. if (ret) {
  647. dev_err(dev, "Failed to enable regulator iov\n");
  648. return ret;
  649. }
  650. if (!IS_ERR(aic32x4->supply_ldo)) {
  651. ret = regulator_enable(aic32x4->supply_ldo);
  652. if (ret) {
  653. dev_err(dev, "Failed to enable regulator ldo\n");
  654. goto error_ldo;
  655. }
  656. }
  657. if (!IS_ERR(aic32x4->supply_dv)) {
  658. ret = regulator_enable(aic32x4->supply_dv);
  659. if (ret) {
  660. dev_err(dev, "Failed to enable regulator dv\n");
  661. goto error_dv;
  662. }
  663. }
  664. if (!IS_ERR(aic32x4->supply_av)) {
  665. ret = regulator_enable(aic32x4->supply_av);
  666. if (ret) {
  667. dev_err(dev, "Failed to enable regulator av\n");
  668. goto error_av;
  669. }
  670. }
  671. if (!IS_ERR(aic32x4->supply_ldo) && IS_ERR(aic32x4->supply_av))
  672. aic32x4->power_cfg |= AIC32X4_PWR_AIC32X4_LDO_ENABLE;
  673. return 0;
  674. error_av:
  675. if (!IS_ERR(aic32x4->supply_dv))
  676. regulator_disable(aic32x4->supply_dv);
  677. error_dv:
  678. if (!IS_ERR(aic32x4->supply_ldo))
  679. regulator_disable(aic32x4->supply_ldo);
  680. error_ldo:
  681. regulator_disable(aic32x4->supply_iov);
  682. return ret;
  683. }
  684. static int aic32x4_i2c_probe(struct i2c_client *i2c,
  685. const struct i2c_device_id *id)
  686. {
  687. struct aic32x4_pdata *pdata = i2c->dev.platform_data;
  688. struct aic32x4_priv *aic32x4;
  689. struct device_node *np = i2c->dev.of_node;
  690. int ret;
  691. aic32x4 = devm_kzalloc(&i2c->dev, sizeof(struct aic32x4_priv),
  692. GFP_KERNEL);
  693. if (aic32x4 == NULL)
  694. return -ENOMEM;
  695. aic32x4->regmap = devm_regmap_init_i2c(i2c, &aic32x4_regmap);
  696. if (IS_ERR(aic32x4->regmap))
  697. return PTR_ERR(aic32x4->regmap);
  698. i2c_set_clientdata(i2c, aic32x4);
  699. if (pdata) {
  700. aic32x4->power_cfg = pdata->power_cfg;
  701. aic32x4->swapdacs = pdata->swapdacs;
  702. aic32x4->micpga_routing = pdata->micpga_routing;
  703. aic32x4->rstn_gpio = pdata->rstn_gpio;
  704. } else if (np) {
  705. ret = aic32x4_parse_dt(aic32x4, np);
  706. if (ret) {
  707. dev_err(&i2c->dev, "Failed to parse DT node\n");
  708. return ret;
  709. }
  710. } else {
  711. aic32x4->power_cfg = 0;
  712. aic32x4->swapdacs = false;
  713. aic32x4->micpga_routing = 0;
  714. aic32x4->rstn_gpio = -1;
  715. }
  716. aic32x4->mclk = devm_clk_get(&i2c->dev, "mclk");
  717. if (IS_ERR(aic32x4->mclk)) {
  718. dev_err(&i2c->dev, "Failed getting the mclk. The current implementation does not support the usage of this codec without mclk\n");
  719. return PTR_ERR(aic32x4->mclk);
  720. }
  721. if (gpio_is_valid(aic32x4->rstn_gpio)) {
  722. ret = devm_gpio_request_one(&i2c->dev, aic32x4->rstn_gpio,
  723. GPIOF_OUT_INIT_LOW, "tlv320aic32x4 rstn");
  724. if (ret != 0)
  725. return ret;
  726. }
  727. ret = aic32x4_setup_regulators(&i2c->dev, aic32x4);
  728. if (ret) {
  729. dev_err(&i2c->dev, "Failed to setup regulators\n");
  730. return ret;
  731. }
  732. ret = snd_soc_register_codec(&i2c->dev,
  733. &soc_codec_dev_aic32x4, &aic32x4_dai, 1);
  734. if (ret) {
  735. dev_err(&i2c->dev, "Failed to register codec\n");
  736. aic32x4_disable_regulators(aic32x4);
  737. return ret;
  738. }
  739. i2c_set_clientdata(i2c, aic32x4);
  740. return 0;
  741. }
  742. static int aic32x4_i2c_remove(struct i2c_client *client)
  743. {
  744. struct aic32x4_priv *aic32x4 = i2c_get_clientdata(client);
  745. aic32x4_disable_regulators(aic32x4);
  746. snd_soc_unregister_codec(&client->dev);
  747. return 0;
  748. }
  749. static const struct i2c_device_id aic32x4_i2c_id[] = {
  750. { "tlv320aic32x4", 0 },
  751. { }
  752. };
  753. MODULE_DEVICE_TABLE(i2c, aic32x4_i2c_id);
  754. static const struct of_device_id aic32x4_of_id[] = {
  755. { .compatible = "ti,tlv320aic32x4", },
  756. { /* senitel */ }
  757. };
  758. MODULE_DEVICE_TABLE(of, aic32x4_of_id);
  759. static struct i2c_driver aic32x4_i2c_driver = {
  760. .driver = {
  761. .name = "tlv320aic32x4",
  762. .owner = THIS_MODULE,
  763. .of_match_table = aic32x4_of_id,
  764. },
  765. .probe = aic32x4_i2c_probe,
  766. .remove = aic32x4_i2c_remove,
  767. .id_table = aic32x4_i2c_id,
  768. };
  769. module_i2c_driver(aic32x4_i2c_driver);
  770. MODULE_DESCRIPTION("ASoC tlv320aic32x4 codec driver");
  771. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  772. MODULE_LICENSE("GPL");