cs4271.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. };
  150. /*
  151. * @freq is the desired MCLK rate
  152. * MCLK rate should (c) be the sample rate, multiplied by one of the
  153. * ratios listed in cs4271_mclk_fs_ratios table
  154. */
  155. static int cs4271_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  156. int clk_id, unsigned int freq, int dir)
  157. {
  158. struct snd_soc_codec *codec = codec_dai->codec;
  159. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  160. cs4271->mclk = freq;
  161. return 0;
  162. }
  163. static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai,
  164. unsigned int format)
  165. {
  166. struct snd_soc_codec *codec = codec_dai->codec;
  167. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  168. unsigned int val = 0;
  169. int ret;
  170. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  171. case SND_SOC_DAIFMT_CBS_CFS:
  172. cs4271->master = 0;
  173. break;
  174. case SND_SOC_DAIFMT_CBM_CFM:
  175. cs4271->master = 1;
  176. val |= CS4271_MODE1_MASTER;
  177. break;
  178. default:
  179. dev_err(codec->dev, "Invalid DAI format\n");
  180. return -EINVAL;
  181. }
  182. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  183. case SND_SOC_DAIFMT_LEFT_J:
  184. val |= CS4271_MODE1_DAC_DIF_LJ;
  185. ret = snd_soc_update_bits(codec, CS4271_ADCCTL,
  186. CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_LJ);
  187. if (ret < 0)
  188. return ret;
  189. break;
  190. case SND_SOC_DAIFMT_I2S:
  191. val |= CS4271_MODE1_DAC_DIF_I2S;
  192. ret = snd_soc_update_bits(codec, CS4271_ADCCTL,
  193. CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_I2S);
  194. if (ret < 0)
  195. return ret;
  196. break;
  197. default:
  198. dev_err(codec->dev, "Invalid DAI format\n");
  199. return -EINVAL;
  200. }
  201. ret = snd_soc_update_bits(codec, CS4271_MODE1,
  202. CS4271_MODE1_DAC_DIF_MASK | CS4271_MODE1_MASTER, val);
  203. if (ret < 0)
  204. return ret;
  205. return 0;
  206. }
  207. static int cs4271_deemph[] = {0, 44100, 48000, 32000};
  208. static int cs4271_set_deemph(struct snd_soc_codec *codec)
  209. {
  210. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  211. int i, ret;
  212. int val = CS4271_DACCTL_DEM_DIS;
  213. if (cs4271->deemph) {
  214. /* Find closest de-emphasis freq */
  215. val = 1;
  216. for (i = 2; i < ARRAY_SIZE(cs4271_deemph); i++)
  217. if (abs(cs4271_deemph[i] - cs4271->rate) <
  218. abs(cs4271_deemph[val] - cs4271->rate))
  219. val = i;
  220. val <<= 4;
  221. }
  222. ret = snd_soc_update_bits(codec, CS4271_DACCTL,
  223. CS4271_DACCTL_DEM_MASK, val);
  224. if (ret < 0)
  225. return ret;
  226. return 0;
  227. }
  228. static int cs4271_get_deemph(struct snd_kcontrol *kcontrol,
  229. struct snd_ctl_elem_value *ucontrol)
  230. {
  231. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  232. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  233. ucontrol->value.enumerated.item[0] = cs4271->deemph;
  234. return 0;
  235. }
  236. static int cs4271_put_deemph(struct snd_kcontrol *kcontrol,
  237. struct snd_ctl_elem_value *ucontrol)
  238. {
  239. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  240. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  241. cs4271->deemph = ucontrol->value.enumerated.item[0];
  242. return cs4271_set_deemph(codec);
  243. }
  244. struct cs4271_clk_cfg {
  245. bool master; /* codec mode */
  246. u8 speed_mode; /* codec speed mode: 1x, 2x, 4x */
  247. unsigned short ratio; /* MCLK / sample rate */
  248. u8 ratio_mask; /* ratio bit mask for Master mode */
  249. };
  250. static struct cs4271_clk_cfg cs4271_clk_tab[] = {
  251. {1, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
  252. {1, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_15},
  253. {1, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_2},
  254. {1, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_3},
  255. {1, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
  256. {1, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_15},
  257. {1, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_2},
  258. {1, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_3},
  259. {1, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
  260. {1, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_15},
  261. {1, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_2},
  262. {1, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_3},
  263. {0, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
  264. {0, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_1},
  265. {0, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_1},
  266. {0, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_2},
  267. {0, CS4271_MODE1_MODE_1X, 1024, CS4271_MODE1_DIV_2},
  268. {0, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
  269. {0, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_1},
  270. {0, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_1},
  271. {0, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_2},
  272. {0, CS4271_MODE1_MODE_2X, 512, CS4271_MODE1_DIV_2},
  273. {0, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
  274. {0, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_1},
  275. {0, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_1},
  276. {0, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_2},
  277. {0, CS4271_MODE1_MODE_4X, 256, CS4271_MODE1_DIV_2},
  278. };
  279. #define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab)
  280. static int cs4271_hw_params(struct snd_pcm_substream *substream,
  281. struct snd_pcm_hw_params *params,
  282. struct snd_soc_dai *dai)
  283. {
  284. struct snd_soc_codec *codec = dai->codec;
  285. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  286. int i, ret;
  287. unsigned int ratio, val;
  288. cs4271->rate = params_rate(params);
  289. /* Configure DAC */
  290. if (cs4271->rate < 50000)
  291. val = CS4271_MODE1_MODE_1X;
  292. else if (cs4271->rate < 100000)
  293. val = CS4271_MODE1_MODE_2X;
  294. else
  295. val = CS4271_MODE1_MODE_4X;
  296. ratio = cs4271->mclk / cs4271->rate;
  297. for (i = 0; i < CS4171_NR_RATIOS; i++)
  298. if ((cs4271_clk_tab[i].master == cs4271->master) &&
  299. (cs4271_clk_tab[i].speed_mode == val) &&
  300. (cs4271_clk_tab[i].ratio == ratio))
  301. break;
  302. if (i == CS4171_NR_RATIOS) {
  303. dev_err(codec->dev, "Invalid sample rate\n");
  304. return -EINVAL;
  305. }
  306. val |= cs4271_clk_tab[i].ratio_mask;
  307. ret = snd_soc_update_bits(codec, CS4271_MODE1,
  308. CS4271_MODE1_MODE_MASK | CS4271_MODE1_DIV_MASK, val);
  309. if (ret < 0)
  310. return ret;
  311. return cs4271_set_deemph(codec);
  312. }
  313. static int cs4271_digital_mute(struct snd_soc_dai *dai, int mute)
  314. {
  315. struct snd_soc_codec *codec = dai->codec;
  316. int ret;
  317. int val_a = 0;
  318. int val_b = 0;
  319. if (mute) {
  320. val_a = CS4271_VOLA_MUTE;
  321. val_b = CS4271_VOLB_MUTE;
  322. }
  323. ret = snd_soc_update_bits(codec, CS4271_VOLA, CS4271_VOLA_MUTE, val_a);
  324. if (ret < 0)
  325. return ret;
  326. ret = snd_soc_update_bits(codec, CS4271_VOLB, CS4271_VOLB_MUTE, val_b);
  327. if (ret < 0)
  328. return ret;
  329. return 0;
  330. }
  331. /* CS4271 controls */
  332. static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0);
  333. static const struct snd_kcontrol_new cs4271_snd_controls[] = {
  334. SOC_DOUBLE_R_TLV("Master Playback Volume", CS4271_VOLA, CS4271_VOLB,
  335. 0, 0x7F, 1, cs4271_dac_tlv),
  336. SOC_SINGLE("Digital Loopback Switch", CS4271_MODE2, 4, 1, 0),
  337. SOC_SINGLE("Soft Ramp Switch", CS4271_DACVOL, 5, 1, 0),
  338. SOC_SINGLE("Zero Cross Switch", CS4271_DACVOL, 4, 1, 0),
  339. SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
  340. cs4271_get_deemph, cs4271_put_deemph),
  341. SOC_SINGLE("Auto-Mute Switch", CS4271_DACCTL, 7, 1, 0),
  342. SOC_SINGLE("Slow Roll Off Filter Switch", CS4271_DACCTL, 6, 1, 0),
  343. SOC_SINGLE("Soft Volume Ramp-Up Switch", CS4271_DACCTL, 3, 1, 0),
  344. SOC_SINGLE("Soft Ramp-Down Switch", CS4271_DACCTL, 2, 1, 0),
  345. SOC_SINGLE("Left Channel Inversion Switch", CS4271_DACCTL, 1, 1, 0),
  346. SOC_SINGLE("Right Channel Inversion Switch", CS4271_DACCTL, 0, 1, 0),
  347. SOC_DOUBLE("Master Capture Switch", CS4271_ADCCTL, 3, 2, 1, 1),
  348. SOC_SINGLE("Dither 16-Bit Data Switch", CS4271_ADCCTL, 5, 1, 0),
  349. SOC_DOUBLE("High Pass Filter Switch", CS4271_ADCCTL, 1, 0, 1, 1),
  350. SOC_DOUBLE_R("Master Playback Switch", CS4271_VOLA, CS4271_VOLB,
  351. 7, 1, 1),
  352. };
  353. static const struct snd_soc_dai_ops cs4271_dai_ops = {
  354. .hw_params = cs4271_hw_params,
  355. .set_sysclk = cs4271_set_dai_sysclk,
  356. .set_fmt = cs4271_set_dai_fmt,
  357. .digital_mute = cs4271_digital_mute,
  358. };
  359. static struct snd_soc_dai_driver cs4271_dai = {
  360. .name = "cs4271-hifi",
  361. .playback = {
  362. .stream_name = "Playback",
  363. .channels_min = 2,
  364. .channels_max = 2,
  365. .rates = CS4271_PCM_RATES,
  366. .formats = CS4271_PCM_FORMATS,
  367. },
  368. .capture = {
  369. .stream_name = "Capture",
  370. .channels_min = 2,
  371. .channels_max = 2,
  372. .rates = CS4271_PCM_RATES,
  373. .formats = CS4271_PCM_FORMATS,
  374. },
  375. .ops = &cs4271_dai_ops,
  376. .symmetric_rates = 1,
  377. };
  378. #ifdef CONFIG_PM
  379. static int cs4271_soc_suspend(struct snd_soc_codec *codec)
  380. {
  381. int ret;
  382. /* Set power-down bit */
  383. ret = snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN,
  384. CS4271_MODE2_PDN);
  385. if (ret < 0)
  386. return ret;
  387. return 0;
  388. }
  389. static int cs4271_soc_resume(struct snd_soc_codec *codec)
  390. {
  391. int ret;
  392. /* Restore codec state */
  393. ret = snd_soc_cache_sync(codec);
  394. if (ret < 0)
  395. return ret;
  396. /* then disable the power-down bit */
  397. ret = snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN, 0);
  398. if (ret < 0)
  399. return ret;
  400. return 0;
  401. }
  402. #else
  403. #define cs4271_soc_suspend NULL
  404. #define cs4271_soc_resume NULL
  405. #endif /* CONFIG_PM */
  406. #ifdef CONFIG_OF
  407. static const struct of_device_id cs4271_dt_ids[] = {
  408. { .compatible = "cirrus,cs4271", },
  409. { }
  410. };
  411. MODULE_DEVICE_TABLE(of, cs4271_dt_ids);
  412. #endif
  413. static int cs4271_probe(struct snd_soc_codec *codec)
  414. {
  415. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  416. struct cs4271_platform_data *cs4271plat = codec->dev->platform_data;
  417. int ret;
  418. int gpio_nreset = -EINVAL;
  419. #ifdef CONFIG_OF
  420. if (of_match_device(cs4271_dt_ids, codec->dev))
  421. gpio_nreset = of_get_named_gpio(codec->dev->of_node,
  422. "reset-gpio", 0);
  423. #endif
  424. if (cs4271plat && gpio_is_valid(cs4271plat->gpio_nreset))
  425. gpio_nreset = cs4271plat->gpio_nreset;
  426. if (gpio_nreset >= 0)
  427. if (gpio_request(gpio_nreset, "CS4271 Reset"))
  428. gpio_nreset = -EINVAL;
  429. if (gpio_nreset >= 0) {
  430. /* Reset codec */
  431. gpio_direction_output(gpio_nreset, 0);
  432. udelay(1);
  433. gpio_set_value(gpio_nreset, 1);
  434. /* Give the codec time to wake up */
  435. udelay(1);
  436. }
  437. cs4271->gpio_nreset = gpio_nreset;
  438. /*
  439. * In case of I2C, chip address specified in board data.
  440. * So cache IO operations use 8 bit codec register address.
  441. * In case of SPI, chip address and register address
  442. * passed together as 16 bit value.
  443. * Anyway, register address is masked with 0xFF inside
  444. * soc-cache code.
  445. */
  446. if (cs4271->bus_type == SND_SOC_SPI)
  447. ret = snd_soc_codec_set_cache_io(codec, 16, 8,
  448. cs4271->bus_type);
  449. else
  450. ret = snd_soc_codec_set_cache_io(codec, 8, 8,
  451. cs4271->bus_type);
  452. if (ret) {
  453. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  454. return ret;
  455. }
  456. ret = snd_soc_update_bits(codec, CS4271_MODE2,
  457. CS4271_MODE2_PDN | CS4271_MODE2_CPEN,
  458. CS4271_MODE2_PDN | CS4271_MODE2_CPEN);
  459. if (ret < 0)
  460. return ret;
  461. ret = snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN, 0);
  462. if (ret < 0)
  463. return ret;
  464. /* Power-up sequence requires 85 uS */
  465. udelay(85);
  466. return snd_soc_add_codec_controls(codec, cs4271_snd_controls,
  467. ARRAY_SIZE(cs4271_snd_controls));
  468. }
  469. static int cs4271_remove(struct snd_soc_codec *codec)
  470. {
  471. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  472. int gpio_nreset;
  473. gpio_nreset = cs4271->gpio_nreset;
  474. if (gpio_is_valid(gpio_nreset)) {
  475. /* Set codec to the reset state */
  476. gpio_set_value(gpio_nreset, 0);
  477. gpio_free(gpio_nreset);
  478. }
  479. return 0;
  480. };
  481. static struct snd_soc_codec_driver soc_codec_dev_cs4271 = {
  482. .probe = cs4271_probe,
  483. .remove = cs4271_remove,
  484. .suspend = cs4271_soc_suspend,
  485. .resume = cs4271_soc_resume,
  486. .reg_cache_default = cs4271_dflt_reg,
  487. .reg_cache_size = ARRAY_SIZE(cs4271_dflt_reg),
  488. .reg_word_size = sizeof(cs4271_dflt_reg[0]),
  489. .compress_type = SND_SOC_FLAT_COMPRESSION,
  490. };
  491. #if defined(CONFIG_SPI_MASTER)
  492. static int __devinit cs4271_spi_probe(struct spi_device *spi)
  493. {
  494. struct cs4271_private *cs4271;
  495. cs4271 = devm_kzalloc(&spi->dev, sizeof(*cs4271), GFP_KERNEL);
  496. if (!cs4271)
  497. return -ENOMEM;
  498. spi_set_drvdata(spi, cs4271);
  499. cs4271->bus_type = SND_SOC_SPI;
  500. return snd_soc_register_codec(&spi->dev, &soc_codec_dev_cs4271,
  501. &cs4271_dai, 1);
  502. }
  503. static int __devexit cs4271_spi_remove(struct spi_device *spi)
  504. {
  505. snd_soc_unregister_codec(&spi->dev);
  506. return 0;
  507. }
  508. static struct spi_driver cs4271_spi_driver = {
  509. .driver = {
  510. .name = "cs4271",
  511. .owner = THIS_MODULE,
  512. .of_match_table = of_match_ptr(cs4271_dt_ids),
  513. },
  514. .probe = cs4271_spi_probe,
  515. .remove = __devexit_p(cs4271_spi_remove),
  516. };
  517. #endif /* defined(CONFIG_SPI_MASTER) */
  518. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  519. static const struct i2c_device_id cs4271_i2c_id[] = {
  520. {"cs4271", 0},
  521. {}
  522. };
  523. MODULE_DEVICE_TABLE(i2c, cs4271_i2c_id);
  524. static int __devinit cs4271_i2c_probe(struct i2c_client *client,
  525. const struct i2c_device_id *id)
  526. {
  527. struct cs4271_private *cs4271;
  528. cs4271 = devm_kzalloc(&client->dev, sizeof(*cs4271), GFP_KERNEL);
  529. if (!cs4271)
  530. return -ENOMEM;
  531. i2c_set_clientdata(client, cs4271);
  532. cs4271->bus_type = SND_SOC_I2C;
  533. return snd_soc_register_codec(&client->dev, &soc_codec_dev_cs4271,
  534. &cs4271_dai, 1);
  535. }
  536. static int __devexit cs4271_i2c_remove(struct i2c_client *client)
  537. {
  538. snd_soc_unregister_codec(&client->dev);
  539. return 0;
  540. }
  541. static struct i2c_driver cs4271_i2c_driver = {
  542. .driver = {
  543. .name = "cs4271",
  544. .owner = THIS_MODULE,
  545. .of_match_table = of_match_ptr(cs4271_dt_ids),
  546. },
  547. .id_table = cs4271_i2c_id,
  548. .probe = cs4271_i2c_probe,
  549. .remove = __devexit_p(cs4271_i2c_remove),
  550. };
  551. #endif /* defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) */
  552. /*
  553. * We only register our serial bus driver here without
  554. * assignment to particular chip. So if any of the below
  555. * fails, there is some problem with I2C or SPI subsystem.
  556. * In most cases this module will be compiled with support
  557. * of only one serial bus.
  558. */
  559. static int __init cs4271_modinit(void)
  560. {
  561. int ret;
  562. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  563. ret = i2c_add_driver(&cs4271_i2c_driver);
  564. if (ret) {
  565. pr_err("Failed to register CS4271 I2C driver: %d\n", ret);
  566. return ret;
  567. }
  568. #endif
  569. #if defined(CONFIG_SPI_MASTER)
  570. ret = spi_register_driver(&cs4271_spi_driver);
  571. if (ret) {
  572. pr_err("Failed to register CS4271 SPI driver: %d\n", ret);
  573. return ret;
  574. }
  575. #endif
  576. return 0;
  577. }
  578. module_init(cs4271_modinit);
  579. static void __exit cs4271_modexit(void)
  580. {
  581. #if defined(CONFIG_SPI_MASTER)
  582. spi_unregister_driver(&cs4271_spi_driver);
  583. #endif
  584. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  585. i2c_del_driver(&cs4271_i2c_driver);
  586. #endif
  587. }
  588. module_exit(cs4271_modexit);
  589. MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>");
  590. MODULE_DESCRIPTION("Cirrus Logic CS4271 ALSA SoC Codec Driver");
  591. MODULE_LICENSE("GPL");