cs4271.c 18 KB

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