cs4271.c 20 KB

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