cs4271.c 18 KB

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