cs4271.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. void *control_data;
  139. unsigned int mclk;
  140. bool master;
  141. bool deemph;
  142. /* Current sample rate for de-emphasis control */
  143. int rate;
  144. /* GPIO driving Reset pin, if any */
  145. int gpio_nreset;
  146. /* GPIO that disable serial bus, if any */
  147. int gpio_disable;
  148. };
  149. /*
  150. * @freq is the desired MCLK rate
  151. * MCLK rate should (c) be the sample rate, multiplied by one of the
  152. * ratios listed in cs4271_mclk_fs_ratios table
  153. */
  154. static int cs4271_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  155. int clk_id, unsigned int freq, int dir)
  156. {
  157. struct snd_soc_codec *codec = codec_dai->codec;
  158. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  159. cs4271->mclk = freq;
  160. return 0;
  161. }
  162. static int cs4271_set_dai_fmt(struct snd_soc_dai *codec_dai,
  163. unsigned int format)
  164. {
  165. struct snd_soc_codec *codec = codec_dai->codec;
  166. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  167. unsigned int val = 0;
  168. int ret;
  169. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  170. case SND_SOC_DAIFMT_CBS_CFS:
  171. cs4271->master = 0;
  172. break;
  173. case SND_SOC_DAIFMT_CBM_CFM:
  174. cs4271->master = 1;
  175. val |= CS4271_MODE1_MASTER;
  176. break;
  177. default:
  178. dev_err(codec->dev, "Invalid DAI format\n");
  179. return -EINVAL;
  180. }
  181. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  182. case SND_SOC_DAIFMT_LEFT_J:
  183. val |= CS4271_MODE1_DAC_DIF_LJ;
  184. ret = snd_soc_update_bits(codec, CS4271_ADCCTL,
  185. CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_LJ);
  186. if (ret < 0)
  187. return ret;
  188. break;
  189. case SND_SOC_DAIFMT_I2S:
  190. val |= CS4271_MODE1_DAC_DIF_I2S;
  191. ret = snd_soc_update_bits(codec, CS4271_ADCCTL,
  192. CS4271_ADCCTL_ADC_DIF_MASK, CS4271_ADCCTL_ADC_DIF_I2S);
  193. if (ret < 0)
  194. return ret;
  195. break;
  196. default:
  197. dev_err(codec->dev, "Invalid DAI format\n");
  198. return -EINVAL;
  199. }
  200. ret = snd_soc_update_bits(codec, CS4271_MODE1,
  201. CS4271_MODE1_DAC_DIF_MASK | CS4271_MODE1_MASTER, val);
  202. if (ret < 0)
  203. return ret;
  204. return 0;
  205. }
  206. static int cs4271_deemph[] = {0, 44100, 48000, 32000};
  207. static int cs4271_set_deemph(struct snd_soc_codec *codec)
  208. {
  209. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  210. int i, ret;
  211. int val = CS4271_DACCTL_DEM_DIS;
  212. if (cs4271->deemph) {
  213. /* Find closest de-emphasis freq */
  214. val = 1;
  215. for (i = 2; i < ARRAY_SIZE(cs4271_deemph); i++)
  216. if (abs(cs4271_deemph[i] - cs4271->rate) <
  217. abs(cs4271_deemph[val] - cs4271->rate))
  218. val = i;
  219. val <<= 4;
  220. }
  221. ret = snd_soc_update_bits(codec, CS4271_DACCTL,
  222. CS4271_DACCTL_DEM_MASK, val);
  223. if (ret < 0)
  224. return ret;
  225. return 0;
  226. }
  227. static int cs4271_get_deemph(struct snd_kcontrol *kcontrol,
  228. struct snd_ctl_elem_value *ucontrol)
  229. {
  230. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  231. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  232. ucontrol->value.enumerated.item[0] = cs4271->deemph;
  233. return 0;
  234. }
  235. static int cs4271_put_deemph(struct snd_kcontrol *kcontrol,
  236. struct snd_ctl_elem_value *ucontrol)
  237. {
  238. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  239. struct cs4271_private *cs4271 = snd_soc_codec_get_drvdata(codec);
  240. cs4271->deemph = ucontrol->value.enumerated.item[0];
  241. return cs4271_set_deemph(codec);
  242. }
  243. struct cs4271_clk_cfg {
  244. bool master; /* codec mode */
  245. u8 speed_mode; /* codec speed mode: 1x, 2x, 4x */
  246. unsigned short ratio; /* MCLK / sample rate */
  247. u8 ratio_mask; /* ratio bit mask for Master mode */
  248. };
  249. static struct cs4271_clk_cfg cs4271_clk_tab[] = {
  250. {1, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
  251. {1, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_15},
  252. {1, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_2},
  253. {1, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_3},
  254. {1, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
  255. {1, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_15},
  256. {1, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_2},
  257. {1, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_3},
  258. {1, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
  259. {1, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_15},
  260. {1, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_2},
  261. {1, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_3},
  262. {0, CS4271_MODE1_MODE_1X, 256, CS4271_MODE1_DIV_1},
  263. {0, CS4271_MODE1_MODE_1X, 384, CS4271_MODE1_DIV_1},
  264. {0, CS4271_MODE1_MODE_1X, 512, CS4271_MODE1_DIV_1},
  265. {0, CS4271_MODE1_MODE_1X, 768, CS4271_MODE1_DIV_2},
  266. {0, CS4271_MODE1_MODE_1X, 1024, CS4271_MODE1_DIV_2},
  267. {0, CS4271_MODE1_MODE_2X, 128, CS4271_MODE1_DIV_1},
  268. {0, CS4271_MODE1_MODE_2X, 192, CS4271_MODE1_DIV_1},
  269. {0, CS4271_MODE1_MODE_2X, 256, CS4271_MODE1_DIV_1},
  270. {0, CS4271_MODE1_MODE_2X, 384, CS4271_MODE1_DIV_2},
  271. {0, CS4271_MODE1_MODE_2X, 512, CS4271_MODE1_DIV_2},
  272. {0, CS4271_MODE1_MODE_4X, 64, CS4271_MODE1_DIV_1},
  273. {0, CS4271_MODE1_MODE_4X, 96, CS4271_MODE1_DIV_1},
  274. {0, CS4271_MODE1_MODE_4X, 128, CS4271_MODE1_DIV_1},
  275. {0, CS4271_MODE1_MODE_4X, 192, CS4271_MODE1_DIV_2},
  276. {0, CS4271_MODE1_MODE_4X, 256, CS4271_MODE1_DIV_2},
  277. };
  278. #define CS4171_NR_RATIOS ARRAY_SIZE(cs4271_clk_tab)
  279. static int cs4271_hw_params(struct snd_pcm_substream *substream,
  280. struct snd_pcm_hw_params *params,
  281. struct snd_soc_dai *dai)
  282. {
  283. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  284. struct snd_soc_codec *codec = rtd->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 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, pm_message_t mesg)
  380. {
  381. int ret;
  382. /* Set power-down bit */
  383. ret = snd_soc_update_bits(codec, CS4271_MODE2, 0, 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. codec->control_data = cs4271->control_data;
  412. if (cs4271plat && gpio_is_valid(cs4271plat->gpio_nreset))
  413. gpio_nreset = cs4271plat->gpio_nreset;
  414. if (gpio_nreset >= 0)
  415. if (gpio_request(gpio_nreset, "CS4271 Reset"))
  416. gpio_nreset = -EINVAL;
  417. if (gpio_nreset >= 0) {
  418. /* Reset codec */
  419. gpio_direction_output(gpio_nreset, 0);
  420. udelay(1);
  421. gpio_set_value(gpio_nreset, 1);
  422. /* Give the codec time to wake up */
  423. udelay(1);
  424. }
  425. cs4271->gpio_nreset = gpio_nreset;
  426. /*
  427. * In case of I2C, chip address specified in board data.
  428. * So cache IO operations use 8 bit codec register address.
  429. * In case of SPI, chip address and register address
  430. * passed together as 16 bit value.
  431. * Anyway, register address is masked with 0xFF inside
  432. * soc-cache code.
  433. */
  434. if (cs4271->bus_type == SND_SOC_SPI)
  435. ret = snd_soc_codec_set_cache_io(codec, 16, 8,
  436. cs4271->bus_type);
  437. else
  438. ret = snd_soc_codec_set_cache_io(codec, 8, 8,
  439. cs4271->bus_type);
  440. if (ret) {
  441. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  442. return ret;
  443. }
  444. ret = snd_soc_update_bits(codec, CS4271_MODE2, 0,
  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_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->control_data = spi;
  487. cs4271->bus_type = SND_SOC_SPI;
  488. return snd_soc_register_codec(&spi->dev, &soc_codec_dev_cs4271,
  489. &cs4271_dai, 1);
  490. }
  491. static int __devexit cs4271_spi_remove(struct spi_device *spi)
  492. {
  493. snd_soc_unregister_codec(&spi->dev);
  494. return 0;
  495. }
  496. static struct spi_driver cs4271_spi_driver = {
  497. .driver = {
  498. .name = "cs4271",
  499. .owner = THIS_MODULE,
  500. },
  501. .probe = cs4271_spi_probe,
  502. .remove = __devexit_p(cs4271_spi_remove),
  503. };
  504. #endif /* defined(CONFIG_SPI_MASTER) */
  505. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  506. static const struct i2c_device_id cs4271_i2c_id[] = {
  507. {"cs4271", 0},
  508. {}
  509. };
  510. MODULE_DEVICE_TABLE(i2c, cs4271_i2c_id);
  511. static int __devinit cs4271_i2c_probe(struct i2c_client *client,
  512. const struct i2c_device_id *id)
  513. {
  514. struct cs4271_private *cs4271;
  515. cs4271 = devm_kzalloc(&client->dev, sizeof(*cs4271), GFP_KERNEL);
  516. if (!cs4271)
  517. return -ENOMEM;
  518. i2c_set_clientdata(client, cs4271);
  519. cs4271->control_data = client;
  520. cs4271->bus_type = SND_SOC_I2C;
  521. return snd_soc_register_codec(&client->dev, &soc_codec_dev_cs4271,
  522. &cs4271_dai, 1);
  523. }
  524. static int __devexit cs4271_i2c_remove(struct i2c_client *client)
  525. {
  526. snd_soc_unregister_codec(&client->dev);
  527. return 0;
  528. }
  529. static struct i2c_driver cs4271_i2c_driver = {
  530. .driver = {
  531. .name = "cs4271",
  532. .owner = THIS_MODULE,
  533. },
  534. .id_table = cs4271_i2c_id,
  535. .probe = cs4271_i2c_probe,
  536. .remove = __devexit_p(cs4271_i2c_remove),
  537. };
  538. #endif /* defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) */
  539. /*
  540. * We only register our serial bus driver here without
  541. * assignment to particular chip. So if any of the below
  542. * fails, there is some problem with I2C or SPI subsystem.
  543. * In most cases this module will be compiled with support
  544. * of only one serial bus.
  545. */
  546. static int __init cs4271_modinit(void)
  547. {
  548. int ret;
  549. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  550. ret = i2c_add_driver(&cs4271_i2c_driver);
  551. if (ret) {
  552. pr_err("Failed to register CS4271 I2C driver: %d\n", ret);
  553. return ret;
  554. }
  555. #endif
  556. #if defined(CONFIG_SPI_MASTER)
  557. ret = spi_register_driver(&cs4271_spi_driver);
  558. if (ret) {
  559. pr_err("Failed to register CS4271 SPI driver: %d\n", ret);
  560. return ret;
  561. }
  562. #endif
  563. return 0;
  564. }
  565. module_init(cs4271_modinit);
  566. static void __exit cs4271_modexit(void)
  567. {
  568. #if defined(CONFIG_SPI_MASTER)
  569. spi_unregister_driver(&cs4271_spi_driver);
  570. #endif
  571. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  572. i2c_del_driver(&cs4271_i2c_driver);
  573. #endif
  574. }
  575. module_exit(cs4271_modexit);
  576. MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>");
  577. MODULE_DESCRIPTION("Cirrus Logic CS4271 ALSA SoC Codec Driver");
  578. MODULE_LICENSE("GPL");