cs4271.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * CS4271 ASoC codec driver
  3. *
  4. * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * This driver support CS4271 codec being master or slave, working
  17. * in control port mode, connected either via SPI or I2C.
  18. * The data format accepted is I2S or left-justified.
  19. * DAPM support not implemented.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/gpio.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <sound/pcm.h>
  30. #include <sound/soc.h>
  31. #include <sound/tlv.h>
  32. #include <sound/cs4271.h>
  33. #include "cs4271.h"
  34. #define CS4271_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  35. SNDRV_PCM_FMTBIT_S24_LE | \
  36. SNDRV_PCM_FMTBIT_S32_LE)
  37. #define CS4271_PCM_RATES SNDRV_PCM_RATE_8000_192000
  38. /*
  39. * CS4271 registers
  40. */
  41. #define CS4271_MODE1 0x01 /* Mode Control 1 */
  42. #define CS4271_DACCTL 0x02 /* DAC Control */
  43. #define CS4271_DACVOL 0x03 /* DAC Volume & Mixing Control */
  44. #define CS4271_VOLA 0x04 /* DAC Channel A Volume Control */
  45. #define CS4271_VOLB 0x05 /* DAC Channel B Volume Control */
  46. #define CS4271_ADCCTL 0x06 /* ADC Control */
  47. #define CS4271_MODE2 0x07 /* Mode Control 2 */
  48. #define CS4271_CHIPID 0x08 /* Chip ID */
  49. #define CS4271_FIRSTREG CS4271_MODE1
  50. #define CS4271_LASTREG CS4271_MODE2
  51. #define CS4271_NR_REGS ((CS4271_LASTREG & 0xFF) + 1)
  52. /* Bit masks for the CS4271 registers */
  53. #define CS4271_MODE1_MODE_MASK 0xC0
  54. #define CS4271_MODE1_MODE_1X 0x00
  55. #define CS4271_MODE1_MODE_2X 0x80
  56. #define CS4271_MODE1_MODE_4X 0xC0
  57. #define CS4271_MODE1_DIV_MASK 0x30
  58. #define CS4271_MODE1_DIV_1 0x00
  59. #define CS4271_MODE1_DIV_15 0x10
  60. #define CS4271_MODE1_DIV_2 0x20
  61. #define CS4271_MODE1_DIV_3 0x30
  62. #define CS4271_MODE1_MASTER 0x08
  63. #define CS4271_MODE1_DAC_DIF_MASK 0x07
  64. #define CS4271_MODE1_DAC_DIF_LJ 0x00
  65. #define CS4271_MODE1_DAC_DIF_I2S 0x01
  66. #define CS4271_MODE1_DAC_DIF_RJ16 0x02
  67. #define CS4271_MODE1_DAC_DIF_RJ24 0x03
  68. #define CS4271_MODE1_DAC_DIF_RJ20 0x04
  69. #define CS4271_MODE1_DAC_DIF_RJ18 0x05
  70. #define CS4271_DACCTL_AMUTE 0x80
  71. #define CS4271_DACCTL_IF_SLOW 0x40
  72. #define CS4271_DACCTL_DEM_MASK 0x30
  73. #define CS4271_DACCTL_DEM_DIS 0x00
  74. #define CS4271_DACCTL_DEM_441 0x10
  75. #define CS4271_DACCTL_DEM_48 0x20
  76. #define CS4271_DACCTL_DEM_32 0x30
  77. #define CS4271_DACCTL_SVRU 0x08
  78. #define CS4271_DACCTL_SRD 0x04
  79. #define CS4271_DACCTL_INVA 0x02
  80. #define CS4271_DACCTL_INVB 0x01
  81. #define CS4271_DACVOL_BEQUA 0x40
  82. #define CS4271_DACVOL_SOFT 0x20
  83. #define CS4271_DACVOL_ZEROC 0x10
  84. #define CS4271_DACVOL_ATAPI_MASK 0x0F
  85. #define CS4271_DACVOL_ATAPI_M_M 0x00
  86. #define CS4271_DACVOL_ATAPI_M_BR 0x01
  87. #define CS4271_DACVOL_ATAPI_M_BL 0x02
  88. #define CS4271_DACVOL_ATAPI_M_BLR2 0x03
  89. #define CS4271_DACVOL_ATAPI_AR_M 0x04
  90. #define CS4271_DACVOL_ATAPI_AR_BR 0x05
  91. #define CS4271_DACVOL_ATAPI_AR_BL 0x06
  92. #define CS4271_DACVOL_ATAPI_AR_BLR2 0x07
  93. #define CS4271_DACVOL_ATAPI_AL_M 0x08
  94. #define CS4271_DACVOL_ATAPI_AL_BR 0x09
  95. #define CS4271_DACVOL_ATAPI_AL_BL 0x0A
  96. #define CS4271_DACVOL_ATAPI_AL_BLR2 0x0B
  97. #define CS4271_DACVOL_ATAPI_ALR2_M 0x0C
  98. #define CS4271_DACVOL_ATAPI_ALR2_BR 0x0D
  99. #define CS4271_DACVOL_ATAPI_ALR2_BL 0x0E
  100. #define CS4271_DACVOL_ATAPI_ALR2_BLR2 0x0F
  101. #define CS4271_VOLA_MUTE 0x80
  102. #define CS4271_VOLA_VOL_MASK 0x7F
  103. #define CS4271_VOLB_MUTE 0x80
  104. #define CS4271_VOLB_VOL_MASK 0x7F
  105. #define CS4271_ADCCTL_DITHER16 0x20
  106. #define CS4271_ADCCTL_ADC_DIF_MASK 0x10
  107. #define CS4271_ADCCTL_ADC_DIF_LJ 0x00
  108. #define CS4271_ADCCTL_ADC_DIF_I2S 0x10
  109. #define CS4271_ADCCTL_MUTEA 0x08
  110. #define CS4271_ADCCTL_MUTEB 0x04
  111. #define CS4271_ADCCTL_HPFDA 0x02
  112. #define CS4271_ADCCTL_HPFDB 0x01
  113. #define CS4271_MODE2_LOOP 0x10
  114. #define CS4271_MODE2_MUTECAEQUB 0x08
  115. #define CS4271_MODE2_FREEZE 0x04
  116. #define CS4271_MODE2_CPEN 0x02
  117. #define CS4271_MODE2_PDN 0x01
  118. #define CS4271_CHIPID_PART_MASK 0xF0
  119. #define CS4271_CHIPID_REV_MASK 0x0F
  120. /*
  121. * Default CS4271 power-up configuration
  122. * Array contains non-existing in hw register at address 0
  123. * Array do not include Chip ID, as codec driver does not use
  124. * registers read operations at all
  125. */
  126. static const struct reg_default cs4271_reg_defaults[] = {
  127. { CS4271_MODE1, 0, },
  128. { CS4271_DACCTL, CS4271_DACCTL_AMUTE, },
  129. { CS4271_DACVOL, CS4271_DACVOL_SOFT | CS4271_DACVOL_ATAPI_AL_BR, },
  130. { CS4271_VOLA, 0, },
  131. { CS4271_VOLB, 0, },
  132. { CS4271_ADCCTL, 0, },
  133. { CS4271_MODE2, 0, },
  134. };
  135. static bool cs4271_volatile_reg(struct device *dev, unsigned int reg)
  136. {
  137. return reg == CS4271_CHIPID;
  138. }
  139. static const char * const supply_names[] = {
  140. "vd", "vl", "va"
  141. };
  142. struct cs4271_private {
  143. unsigned int mclk;
  144. bool master;
  145. bool deemph;
  146. struct regmap *regmap;
  147. /* Current sample rate for de-emphasis control */
  148. int rate;
  149. /* GPIO driving Reset pin, if any */
  150. int gpio_nreset;
  151. /* GPIO that disable serial bus, if any */
  152. int gpio_disable;
  153. /* enable soft reset workaround */
  154. bool enable_soft_reset;
  155. struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
  156. };
  157. static const struct snd_soc_dapm_widget cs4271_dapm_widgets[] = {
  158. SND_SOC_DAPM_INPUT("AINA"),
  159. SND_SOC_DAPM_INPUT("AINB"),
  160. SND_SOC_DAPM_OUTPUT("AOUTA+"),
  161. SND_SOC_DAPM_OUTPUT("AOUTA-"),
  162. SND_SOC_DAPM_OUTPUT("AOUTB+"),
  163. SND_SOC_DAPM_OUTPUT("AOUTB-"),
  164. };
  165. static const struct snd_soc_dapm_route cs4271_dapm_routes[] = {
  166. { "Capture", NULL, "AINA" },
  167. { "Capture", NULL, "AINB" },
  168. { "AOUTA+", NULL, "Playback" },
  169. { "AOUTA-", NULL, "Playback" },
  170. { "AOUTB+", NULL, "Playback" },
  171. { "AOUTB-", NULL, "Playback" },
  172. };
  173. /*
  174. * @freq is the desired MCLK rate
  175. * MCLK rate should (c) be the sample rate, multiplied by one of the
  176. * ratios listed in cs4271_mclk_fs_ratios table
  177. */
  178. static int cs4271_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  179. int clk_id, unsigned int freq, int dir)
  180. {
  181. struct snd_soc_codec *codec = codec_dai->codec;
  182. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  183. cs4271->mclk = freq;
  184. return 0;
  185. }
  186. static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai,
  187. unsigned int format)
  188. {
  189. struct snd_soc_codec *codec = codec_dai->codec;
  190. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  191. unsigned int val = 0;
  192. int ret;
  193. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  194. case SND_SOC_DAIFMT_CBS_CFS:
  195. cs4271->master = 0;
  196. break;
  197. case SND_SOC_DAIFMT_CBM_CFM:
  198. cs4271->master = 1;
  199. val |= CS4271_MODE1_MASTER;
  200. break;
  201. default:
  202. dev_err(codec->dev, "Invalid DAI format\n");
  203. return -EINVAL;
  204. }
  205. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  206. case SND_SOC_DAIFMT_LEFT_J:
  207. val |= CS4271_MODE1_DAC_DIF_LJ;
  208. ret = regmap_update_bits(cs4271->regmap, CS4271_ADCCTL,
  209. CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_LJ);
  210. if (ret < 0)
  211. return ret;
  212. break;
  213. case SND_SOC_DAIFMT_I2S:
  214. val |= CS4271_MODE1_DAC_DIF_I2S;
  215. ret = regmap_update_bits(cs4271->regmap, CS4271_ADCCTL,
  216. CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_I2S);
  217. if (ret < 0)
  218. return ret;
  219. break;
  220. default:
  221. dev_err(codec->dev, "Invalid DAI format\n");
  222. return -EINVAL;
  223. }
  224. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE1,
  225. CS4271_MODE1_DAC_DIF_MASK | CS4271_MODE1_MASTER, val);
  226. if (ret < 0)
  227. return ret;
  228. return 0;
  229. }
  230. static int cs4271_deemph[] = {0, 44100, 48000, 32000};
  231. static int cs4271_set_deemph(struct snd_soc_codec *codec)
  232. {
  233. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  234. int i, ret;
  235. int val = CS4271_DACCTL_DEM_DIS;
  236. if (cs4271->deemph) {
  237. /* Find closest de-emphasis freq */
  238. val = 1;
  239. for (i = 2; i < ARRAY_SIZE(cs4271_deemph); i++)
  240. if (abs(cs4271_deemph[i] - cs4271->rate) <
  241. abs(cs4271_deemph[val] - cs4271->rate))
  242. val = i;
  243. val <<= 4;
  244. }
  245. ret = regmap_update_bits(cs4271->regmap, CS4271_DACCTL,
  246. CS4271_DACCTL_DEM_MASK, val);
  247. if (ret < 0)
  248. return ret;
  249. return 0;
  250. }
  251. static int cs4271_get_deemph(struct snd_kcontrol *kcontrol,
  252. struct snd_ctl_elem_value *ucontrol)
  253. {
  254. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  255. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  256. ucontrol->value.integer.value[0] = cs4271->deemph;
  257. return 0;
  258. }
  259. static int cs4271_put_deemph(struct snd_kcontrol *kcontrol,
  260. struct snd_ctl_elem_value *ucontrol)
  261. {
  262. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  263. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  264. cs4271->deemph = ucontrol->value.integer.value[0];
  265. return cs4271_set_deemph(codec);
  266. }
  267. struct cs4271_clk_cfg {
  268. bool master; /* codec mode */
  269. u8 speed_mode; /* codec speed mode: 1x, 2x, 4x */
  270. unsigned short ratio; /* MCLK / sample rate */
  271. u8 ratio_mask; /* ratio bit mask for Master mode */
  272. };
  273. static struct cs4271_clk_cfg cs4271_clk_tab[] = {
  274. {1, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
  275. {1, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_15},
  276. {1, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_2},
  277. {1, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_3},
  278. {1, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
  279. {1, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_15},
  280. {1, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_2},
  281. {1, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_3},
  282. {1, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
  283. {1, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_15},
  284. {1, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_2},
  285. {1, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_3},
  286. {0, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
  287. {0, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_1},
  288. {0, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_1},
  289. {0, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_2},
  290. {0, CS4271_MODE1_MODE_1X, 1024, CS4271_MODE1_DIV_2},
  291. {0, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
  292. {0, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_1},
  293. {0, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_1},
  294. {0, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_2},
  295. {0, CS4271_MODE1_MODE_2X, 512, CS4271_MODE1_DIV_2},
  296. {0, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
  297. {0, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_1},
  298. {0, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_1},
  299. {0, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_2},
  300. {0, CS4271_MODE1_MODE_4X, 256, CS4271_MODE1_DIV_2},
  301. };
  302. #define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab)
  303. static int cs4271_hw_params(struct snd_pcm_substream *substream,
  304. struct snd_pcm_hw_params *params,
  305. struct snd_soc_dai *dai)
  306. {
  307. struct snd_soc_codec *codec = dai->codec;
  308. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  309. int i, ret;
  310. unsigned int ratio, val;
  311. if (cs4271->enable_soft_reset) {
  312. /*
  313. * Put the codec in soft reset and back again in case it's not
  314. * currently streaming data. This way of bringing the codec in
  315. * sync to the current clocks is not explicitly documented in
  316. * the data sheet, but it seems to work fine, and in contrast
  317. * to a read hardware reset, we don't have to sync back all
  318. * registers every time.
  319. */
  320. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  321. !dai->capture_active) ||
  322. (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
  323. !dai->playback_active)) {
  324. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
  325. CS4271_MODE2_PDN,
  326. CS4271_MODE2_PDN);
  327. if (ret < 0)
  328. return ret;
  329. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
  330. CS4271_MODE2_PDN, 0);
  331. if (ret < 0)
  332. return ret;
  333. }
  334. }
  335. cs4271->rate = params_rate(params);
  336. /* Configure DAC */
  337. if (cs4271->rate < 50000)
  338. val = CS4271_MODE1_MODE_1X;
  339. else if (cs4271->rate < 100000)
  340. val = CS4271_MODE1_MODE_2X;
  341. else
  342. val = CS4271_MODE1_MODE_4X;
  343. ratio = cs4271->mclk / cs4271->rate;
  344. for (i = 0; i < CS4171_NR_RATIOS; i++)
  345. if ((cs4271_clk_tab[i].master == cs4271->master) &&
  346. (cs4271_clk_tab[i].speed_mode == val) &&
  347. (cs4271_clk_tab[i].ratio == ratio))
  348. break;
  349. if (i == CS4171_NR_RATIOS) {
  350. dev_err(codec->dev, "Invalid sample rate\n");
  351. return -EINVAL;
  352. }
  353. val |= cs4271_clk_tab[i].ratio_mask;
  354. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE1,
  355. CS4271_MODE1_MODE_MASK | CS4271_MODE1_DIV_MASK, val);
  356. if (ret < 0)
  357. return ret;
  358. return cs4271_set_deemph(codec);
  359. }
  360. static int cs4271_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
  361. {
  362. struct snd_soc_codec *codec = dai->codec;
  363. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  364. int ret;
  365. int val_a = 0;
  366. int val_b = 0;
  367. if (stream != SNDRV_PCM_STREAM_PLAYBACK)
  368. return 0;
  369. if (mute) {
  370. val_a = CS4271_VOLA_MUTE;
  371. val_b = CS4271_VOLB_MUTE;
  372. }
  373. ret = regmap_update_bits(cs4271->regmap, CS4271_VOLA,
  374. CS4271_VOLA_MUTE, val_a);
  375. if (ret < 0)
  376. return ret;
  377. ret = regmap_update_bits(cs4271->regmap, CS4271_VOLB,
  378. CS4271_VOLB_MUTE, val_b);
  379. if (ret < 0)
  380. return ret;
  381. return 0;
  382. }
  383. /* CS4271 controls */
  384. static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0);
  385. static const struct snd_kcontrol_new cs4271_snd_controls[] = {
  386. SOC_DOUBLE_R_TLV("Master Playback Volume", CS4271_VOLA, CS4271_VOLB,
  387. 0, 0x7F, 1, cs4271_dac_tlv),
  388. SOC_SINGLE("Digital Loopback Switch", CS4271_MODE2, 4, 1, 0),
  389. SOC_SINGLE("Soft Ramp Switch", CS4271_DACVOL, 5, 1, 0),
  390. SOC_SINGLE("Zero Cross Switch", CS4271_DACVOL, 4, 1, 0),
  391. SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
  392. cs4271_get_deemph, cs4271_put_deemph),
  393. SOC_SINGLE("Auto-Mute Switch", CS4271_DACCTL, 7, 1, 0),
  394. SOC_SINGLE("Slow Roll Off Filter Switch", CS4271_DACCTL, 6, 1, 0),
  395. SOC_SINGLE("Soft Volume Ramp-Up Switch", CS4271_DACCTL, 3, 1, 0),
  396. SOC_SINGLE("Soft Ramp-Down Switch", CS4271_DACCTL, 2, 1, 0),
  397. SOC_SINGLE("Left Channel Inversion Switch", CS4271_DACCTL, 1, 1, 0),
  398. SOC_SINGLE("Right Channel Inversion Switch", CS4271_DACCTL, 0, 1, 0),
  399. SOC_DOUBLE("Master Capture Switch", CS4271_ADCCTL, 3, 2, 1, 1),
  400. SOC_SINGLE("Dither 16-Bit Data Switch", CS4271_ADCCTL, 5, 1, 0),
  401. SOC_DOUBLE("High Pass Filter Switch", CS4271_ADCCTL, 1, 0, 1, 1),
  402. SOC_DOUBLE_R("Master Playback Switch", CS4271_VOLA, CS4271_VOLB,
  403. 7, 1, 1),
  404. };
  405. static const struct snd_soc_dai_ops cs4271_dai_ops = {
  406. .hw_params = cs4271_hw_params,
  407. .set_sysclk = cs4271_set_dai_sysclk,
  408. .set_fmt = cs4271_set_dai_fmt,
  409. .mute_stream = cs4271_mute_stream,
  410. };
  411. static struct snd_soc_dai_driver cs4271_dai = {
  412. .name = "cs4271-hifi",
  413. .playback = {
  414. .stream_name = "Playback",
  415. .channels_min = 2,
  416. .channels_max = 2,
  417. .rates = CS4271_PCM_RATES,
  418. .formats = CS4271_PCM_FORMATS,
  419. },
  420. .capture = {
  421. .stream_name = "Capture",
  422. .channels_min = 2,
  423. .channels_max = 2,
  424. .rates = CS4271_PCM_RATES,
  425. .formats = CS4271_PCM_FORMATS,
  426. },
  427. .ops = &cs4271_dai_ops,
  428. .symmetric_rates = 1,
  429. };
  430. static int cs4271_reset(struct snd_soc_codec *codec)
  431. {
  432. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  433. if (gpio_is_valid(cs4271->gpio_nreset)) {
  434. gpio_direction_output(cs4271->gpio_nreset, 0);
  435. mdelay(1);
  436. gpio_set_value(cs4271->gpio_nreset, 1);
  437. mdelay(1);
  438. }
  439. return 0;
  440. }
  441. #ifdef CONFIG_PM
  442. static int cs4271_soc_suspend(struct snd_soc_codec *codec)
  443. {
  444. int ret;
  445. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  446. /* Set power-down bit */
  447. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
  448. CS4271_MODE2_PDN, CS4271_MODE2_PDN);
  449. if (ret < 0)
  450. return ret;
  451. regcache_mark_dirty(cs4271->regmap);
  452. regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies);
  453. return 0;
  454. }
  455. static int cs4271_soc_resume(struct snd_soc_codec *codec)
  456. {
  457. int ret;
  458. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  459. ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies),
  460. cs4271->supplies);
  461. if (ret < 0) {
  462. dev_err(codec->dev, "Failed to enable regulators: %d\n", ret);
  463. return ret;
  464. }
  465. /* Do a proper reset after power up */
  466. cs4271_reset(codec);
  467. /* Restore codec state */
  468. ret = regcache_sync(cs4271->regmap);
  469. if (ret < 0)
  470. return ret;
  471. /* then disable the power-down bit */
  472. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
  473. CS4271_MODE2_PDN, 0);
  474. if (ret < 0)
  475. return ret;
  476. return 0;
  477. }
  478. #else
  479. #define cs4271_soc_suspend NULL
  480. #define cs4271_soc_resume NULL
  481. #endif /* CONFIG_PM */
  482. #ifdef CONFIG_OF
  483. const struct of_device_id cs4271_dt_ids[] = {
  484. { .compatible = "cirrus,cs4271", },
  485. { }
  486. };
  487. MODULE_DEVICE_TABLE(of, cs4271_dt_ids);
  488. EXPORT_SYMBOL_GPL(cs4271_dt_ids);
  489. #endif
  490. static int cs4271_codec_probe(struct snd_soc_codec *codec)
  491. {
  492. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  493. struct cs4271_platform_data *cs4271plat = codec->dev->platform_data;
  494. int ret;
  495. bool amutec_eq_bmutec = false;
  496. #ifdef CONFIG_OF
  497. if (of_match_device(cs4271_dt_ids, codec->dev)) {
  498. if (of_get_property(codec->dev->of_node,
  499. "cirrus,amutec-eq-bmutec", NULL))
  500. amutec_eq_bmutec = true;
  501. if (of_get_property(codec->dev->of_node,
  502. "cirrus,enable-soft-reset", NULL))
  503. cs4271->enable_soft_reset = true;
  504. }
  505. #endif
  506. ret = regulator_bulk_enable(ARRAY_SIZE(cs4271->supplies),
  507. cs4271->supplies);
  508. if (ret < 0) {
  509. dev_err(codec->dev, "Failed to enable regulators: %d\n", ret);
  510. return ret;
  511. }
  512. if (cs4271plat) {
  513. amutec_eq_bmutec = cs4271plat->amutec_eq_bmutec;
  514. cs4271->enable_soft_reset = cs4271plat->enable_soft_reset;
  515. }
  516. /* Reset codec */
  517. cs4271_reset(codec);
  518. ret = regcache_sync(cs4271->regmap);
  519. if (ret < 0)
  520. return ret;
  521. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
  522. CS4271_MODE2_PDN | CS4271_MODE2_CPEN,
  523. CS4271_MODE2_PDN | CS4271_MODE2_CPEN);
  524. if (ret < 0)
  525. return ret;
  526. ret = regmap_update_bits(cs4271->regmap, CS4271_MODE2,
  527. CS4271_MODE2_PDN, 0);
  528. if (ret < 0)
  529. return ret;
  530. /* Power-up sequence requires 85 uS */
  531. udelay(85);
  532. if (amutec_eq_bmutec)
  533. regmap_update_bits(cs4271->regmap, CS4271_MODE2,
  534. CS4271_MODE2_MUTECAEQUB,
  535. CS4271_MODE2_MUTECAEQUB);
  536. return 0;
  537. }
  538. static int cs4271_codec_remove(struct snd_soc_codec *codec)
  539. {
  540. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  541. if (gpio_is_valid(cs4271->gpio_nreset))
  542. /* Set codec to the reset state */
  543. gpio_set_value(cs4271->gpio_nreset, 0);
  544. regcache_mark_dirty(cs4271->regmap);
  545. regulator_bulk_disable(ARRAY_SIZE(cs4271->supplies), cs4271->supplies);
  546. return 0;
  547. };
  548. static struct snd_soc_codec_driver soc_codec_dev_cs4271 = {
  549. .probe = cs4271_codec_probe,
  550. .remove = cs4271_codec_remove,
  551. .suspend = cs4271_soc_suspend,
  552. .resume = cs4271_soc_resume,
  553. .component_driver = {
  554. .controls = cs4271_snd_controls,
  555. .num_controls = ARRAY_SIZE(cs4271_snd_controls),
  556. .dapm_widgets = cs4271_dapm_widgets,
  557. .num_dapm_widgets = ARRAY_SIZE(cs4271_dapm_widgets),
  558. .dapm_routes = cs4271_dapm_routes,
  559. .num_dapm_routes = ARRAY_SIZE(cs4271_dapm_routes),
  560. },
  561. };
  562. static int cs4271_common_probe(struct device *dev,
  563. struct cs4271_private **c)
  564. {
  565. struct cs4271_platform_data *cs4271plat = dev->platform_data;
  566. struct cs4271_private *cs4271;
  567. int i, ret;
  568. cs4271 = devm_kzalloc(dev, sizeof(*cs4271), GFP_KERNEL);
  569. if (!cs4271)
  570. return -ENOMEM;
  571. if (of_match_device(cs4271_dt_ids, dev))
  572. cs4271->gpio_nreset =
  573. of_get_named_gpio(dev->of_node, "reset-gpio", 0);
  574. if (cs4271plat)
  575. cs4271->gpio_nreset = cs4271plat->gpio_nreset;
  576. if (gpio_is_valid(cs4271->gpio_nreset)) {
  577. int ret;
  578. ret = devm_gpio_request(dev, cs4271->gpio_nreset,
  579. "CS4271 Reset");
  580. if (ret < 0)
  581. return ret;
  582. }
  583. for (i = 0; i < ARRAY_SIZE(supply_names); i++)
  584. cs4271->supplies[i].supply = supply_names[i];
  585. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(cs4271->supplies),
  586. cs4271->supplies);
  587. if (ret < 0) {
  588. dev_err(dev, "Failed to get regulators: %d\n", ret);
  589. return ret;
  590. }
  591. *c = cs4271;
  592. return 0;
  593. }
  594. const struct regmap_config cs4271_regmap_config = {
  595. .max_register = CS4271_LASTREG,
  596. .reg_defaults = cs4271_reg_defaults,
  597. .num_reg_defaults = ARRAY_SIZE(cs4271_reg_defaults),
  598. .cache_type = REGCACHE_RBTREE,
  599. .volatile_reg = cs4271_volatile_reg,
  600. };
  601. EXPORT_SYMBOL_GPL(cs4271_regmap_config);
  602. int cs4271_probe(struct device *dev, struct regmap *regmap)
  603. {
  604. struct cs4271_private *cs4271;
  605. int ret;
  606. if (IS_ERR(regmap))
  607. return PTR_ERR(regmap);
  608. ret = cs4271_common_probe(dev, &cs4271);
  609. if (ret < 0)
  610. return ret;
  611. dev_set_drvdata(dev, cs4271);
  612. cs4271->regmap = regmap;
  613. return snd_soc_register_codec(dev, &soc_codec_dev_cs4271, &cs4271_dai,
  614. 1);
  615. }
  616. EXPORT_SYMBOL_GPL(cs4271_probe);
  617. MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>");
  618. MODULE_DESCRIPTION("Cirrus Logic CS4271 ALSA SoC Codec Driver");
  619. MODULE_LICENSE("GPL");