cs4270.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * CS4270 ALSA SoC (ASoC) codec driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed
  7. * under the terms of the GNU General Public License version 2. This
  8. * program is licensed "as is" without any warranty of any kind, whether
  9. * express or implied.
  10. *
  11. * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
  12. *
  13. * Current features/limitations:
  14. *
  15. * - Software mode is supported. Stand-alone mode is not supported.
  16. * - Only I2C is supported, not SPI
  17. * - Support for master and slave mode
  18. * - The machine driver's 'startup' function must call
  19. * cs4270_set_dai_sysclk() with the value of MCLK.
  20. * - Only I2S and left-justified modes are supported
  21. * - Power management is supported
  22. */
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <sound/core.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include <linux/i2c.h>
  29. #include <linux/delay.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_gpio.h>
  33. /*
  34. * The codec isn't really big-endian or little-endian, since the I2S
  35. * interface requires data to be sent serially with the MSbit first.
  36. * However, to support BE and LE I2S devices, we specify both here. That
  37. * way, ALSA will always match the bit patterns.
  38. */
  39. #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  40. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  41. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  42. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  43. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  44. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  45. /* CS4270 registers addresses */
  46. #define CS4270_CHIPID 0x01 /* Chip ID */
  47. #define CS4270_PWRCTL 0x02 /* Power Control */
  48. #define CS4270_MODE 0x03 /* Mode Control */
  49. #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
  50. #define CS4270_TRANS 0x05 /* Transition Control */
  51. #define CS4270_MUTE 0x06 /* Mute Control */
  52. #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
  53. #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
  54. #define CS4270_FIRSTREG 0x01
  55. #define CS4270_LASTREG 0x08
  56. #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
  57. #define CS4270_I2C_INCR 0x80
  58. /* Bit masks for the CS4270 registers */
  59. #define CS4270_CHIPID_ID 0xF0
  60. #define CS4270_CHIPID_REV 0x0F
  61. #define CS4270_PWRCTL_FREEZE 0x80
  62. #define CS4270_PWRCTL_PDN_ADC 0x20
  63. #define CS4270_PWRCTL_PDN_DAC 0x02
  64. #define CS4270_PWRCTL_PDN 0x01
  65. #define CS4270_PWRCTL_PDN_ALL \
  66. (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
  67. #define CS4270_MODE_SPEED_MASK 0x30
  68. #define CS4270_MODE_1X 0x00
  69. #define CS4270_MODE_2X 0x10
  70. #define CS4270_MODE_4X 0x20
  71. #define CS4270_MODE_SLAVE 0x30
  72. #define CS4270_MODE_DIV_MASK 0x0E
  73. #define CS4270_MODE_DIV1 0x00
  74. #define CS4270_MODE_DIV15 0x02
  75. #define CS4270_MODE_DIV2 0x04
  76. #define CS4270_MODE_DIV3 0x06
  77. #define CS4270_MODE_DIV4 0x08
  78. #define CS4270_MODE_POPGUARD 0x01
  79. #define CS4270_FORMAT_FREEZE_A 0x80
  80. #define CS4270_FORMAT_FREEZE_B 0x40
  81. #define CS4270_FORMAT_LOOPBACK 0x20
  82. #define CS4270_FORMAT_DAC_MASK 0x18
  83. #define CS4270_FORMAT_DAC_LJ 0x00
  84. #define CS4270_FORMAT_DAC_I2S 0x08
  85. #define CS4270_FORMAT_DAC_RJ16 0x18
  86. #define CS4270_FORMAT_DAC_RJ24 0x10
  87. #define CS4270_FORMAT_ADC_MASK 0x01
  88. #define CS4270_FORMAT_ADC_LJ 0x00
  89. #define CS4270_FORMAT_ADC_I2S 0x01
  90. #define CS4270_TRANS_ONE_VOL 0x80
  91. #define CS4270_TRANS_SOFT 0x40
  92. #define CS4270_TRANS_ZERO 0x20
  93. #define CS4270_TRANS_INV_ADC_A 0x08
  94. #define CS4270_TRANS_INV_ADC_B 0x10
  95. #define CS4270_TRANS_INV_DAC_A 0x02
  96. #define CS4270_TRANS_INV_DAC_B 0x04
  97. #define CS4270_TRANS_DEEMPH 0x01
  98. #define CS4270_MUTE_AUTO 0x20
  99. #define CS4270_MUTE_ADC_A 0x08
  100. #define CS4270_MUTE_ADC_B 0x10
  101. #define CS4270_MUTE_POLARITY 0x04
  102. #define CS4270_MUTE_DAC_A 0x01
  103. #define CS4270_MUTE_DAC_B 0x02
  104. /* Power-on default values for the registers
  105. *
  106. * This array contains the power-on default values of the registers, with the
  107. * exception of the "CHIPID" register (01h). The lower four bits of that
  108. * register contain the hardware revision, so it is treated as volatile.
  109. *
  110. * Also note that on the CS4270, the first readable register is 1, but ASoC
  111. * assumes the first register is 0. Therfore, the array must have an entry for
  112. * register 0, but we use cs4270_reg_is_readable() to tell ASoC that it can't
  113. * be read.
  114. */
  115. static const u8 cs4270_default_reg_cache[CS4270_LASTREG + 1] = {
  116. 0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x20, 0x00, 0x00
  117. };
  118. static const char *supply_names[] = {
  119. "va", "vd", "vlc"
  120. };
  121. /* Private data for the CS4270 */
  122. struct cs4270_private {
  123. enum snd_soc_control_type control_type;
  124. unsigned int mclk; /* Input frequency of the MCLK pin */
  125. unsigned int mode; /* The mode (I2S or left-justified) */
  126. unsigned int slave_mode;
  127. unsigned int manual_mute;
  128. /* power domain regulators */
  129. struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
  130. };
  131. /**
  132. * struct cs4270_mode_ratios - clock ratio tables
  133. * @ratio: the ratio of MCLK to the sample rate
  134. * @speed_mode: the Speed Mode bits to set in the Mode Control register for
  135. * this ratio
  136. * @mclk: the Ratio Select bits to set in the Mode Control register for this
  137. * ratio
  138. *
  139. * The data for this chart is taken from Table 5 of the CS4270 reference
  140. * manual.
  141. *
  142. * This table is used to determine how to program the Mode Control register.
  143. * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
  144. * rates the CS4270 currently supports.
  145. *
  146. * @speed_mode is the corresponding bit pattern to be written to the
  147. * MODE bits of the Mode Control Register
  148. *
  149. * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
  150. * the Mode Control Register.
  151. *
  152. * In situations where a single ratio is represented by multiple speed
  153. * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
  154. * double-speed instead of quad-speed. However, the CS4270 errata states
  155. * that divide-By-1.5 can cause failures, so we avoid that mode where
  156. * possible.
  157. *
  158. * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
  159. * work if Vd is 3.3V. If this effects you, select the
  160. * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
  161. * never select any sample rates that require divide-by-1.5.
  162. */
  163. struct cs4270_mode_ratios {
  164. unsigned int ratio;
  165. u8 speed_mode;
  166. u8 mclk;
  167. };
  168. static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
  169. {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
  170. #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
  171. {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
  172. #endif
  173. {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
  174. {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
  175. {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
  176. {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
  177. {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
  178. {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
  179. {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
  180. };
  181. /* The number of MCLK/LRCK ratios supported by the CS4270 */
  182. #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
  183. static int cs4270_reg_is_readable(struct snd_soc_codec *codec, unsigned int reg)
  184. {
  185. return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
  186. }
  187. static int cs4270_reg_is_volatile(struct snd_soc_codec *codec, unsigned int reg)
  188. {
  189. /* Unreadable registers are considered volatile */
  190. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  191. return 1;
  192. return reg == CS4270_CHIPID;
  193. }
  194. /**
  195. * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
  196. * @codec_dai: the codec DAI
  197. * @clk_id: the clock ID (ignored)
  198. * @freq: the MCLK input frequency
  199. * @dir: the clock direction (ignored)
  200. *
  201. * This function is used to tell the codec driver what the input MCLK
  202. * frequency is.
  203. *
  204. * The value of MCLK is used to determine which sample rates are supported
  205. * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
  206. * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
  207. *
  208. * This function calculates the nine ratios and determines which ones match
  209. * a standard sample rate. If there's a match, then it is added to the list
  210. * of supported sample rates.
  211. *
  212. * This function must be called by the machine driver's 'startup' function,
  213. * otherwise the list of supported sample rates will not be available in
  214. * time for ALSA.
  215. *
  216. * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
  217. * theoretically possible sample rates to be enabled. Call it again with a
  218. * proper value set one the external clock is set (most probably you would do
  219. * that from a machine's driver 'hw_param' hook.
  220. */
  221. static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  222. int clk_id, unsigned int freq, int dir)
  223. {
  224. struct snd_soc_codec *codec = codec_dai->codec;
  225. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  226. cs4270->mclk = freq;
  227. return 0;
  228. }
  229. /**
  230. * cs4270_set_dai_fmt - configure the codec for the selected audio format
  231. * @codec_dai: the codec DAI
  232. * @format: a SND_SOC_DAIFMT_x value indicating the data format
  233. *
  234. * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
  235. * codec accordingly.
  236. *
  237. * Currently, this function only supports SND_SOC_DAIFMT_I2S and
  238. * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
  239. * data for playback only, but ASoC currently does not support different
  240. * formats for playback vs. record.
  241. */
  242. static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
  243. unsigned int format)
  244. {
  245. struct snd_soc_codec *codec = codec_dai->codec;
  246. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  247. /* set DAI format */
  248. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  249. case SND_SOC_DAIFMT_I2S:
  250. case SND_SOC_DAIFMT_LEFT_J:
  251. cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  252. break;
  253. default:
  254. dev_err(codec->dev, "invalid dai format\n");
  255. return -EINVAL;
  256. }
  257. /* set master/slave audio interface */
  258. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  259. case SND_SOC_DAIFMT_CBS_CFS:
  260. cs4270->slave_mode = 1;
  261. break;
  262. case SND_SOC_DAIFMT_CBM_CFM:
  263. cs4270->slave_mode = 0;
  264. break;
  265. default:
  266. /* all other modes are unsupported by the hardware */
  267. dev_err(codec->dev, "Unknown master/slave configuration\n");
  268. return -EINVAL;
  269. }
  270. return 0;
  271. }
  272. /**
  273. * cs4270_hw_params - program the CS4270 with the given hardware parameters.
  274. * @substream: the audio stream
  275. * @params: the hardware parameters to set
  276. * @dai: the SOC DAI (ignored)
  277. *
  278. * This function programs the hardware with the values provided.
  279. * Specifically, the sample rate and the data format.
  280. *
  281. * The .ops functions are used to provide board-specific data, like input
  282. * frequencies, to this driver. This function takes that information,
  283. * combines it with the hardware parameters provided, and programs the
  284. * hardware accordingly.
  285. */
  286. static int cs4270_hw_params(struct snd_pcm_substream *substream,
  287. struct snd_pcm_hw_params *params,
  288. struct snd_soc_dai *dai)
  289. {
  290. struct snd_soc_codec *codec = dai->codec;
  291. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  292. int ret;
  293. unsigned int i;
  294. unsigned int rate;
  295. unsigned int ratio;
  296. int reg;
  297. /* Figure out which MCLK/LRCK ratio to use */
  298. rate = params_rate(params); /* Sampling rate, in Hz */
  299. ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
  300. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  301. if (cs4270_mode_ratios[i].ratio == ratio)
  302. break;
  303. }
  304. if (i == NUM_MCLK_RATIOS) {
  305. /* We did not find a matching ratio */
  306. dev_err(codec->dev, "could not find matching ratio\n");
  307. return -EINVAL;
  308. }
  309. /* Set the sample rate */
  310. reg = snd_soc_read(codec, CS4270_MODE);
  311. reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
  312. reg |= cs4270_mode_ratios[i].mclk;
  313. if (cs4270->slave_mode)
  314. reg |= CS4270_MODE_SLAVE;
  315. else
  316. reg |= cs4270_mode_ratios[i].speed_mode;
  317. ret = snd_soc_write(codec, CS4270_MODE, reg);
  318. if (ret < 0) {
  319. dev_err(codec->dev, "i2c write failed\n");
  320. return ret;
  321. }
  322. /* Set the DAI format */
  323. reg = snd_soc_read(codec, CS4270_FORMAT);
  324. reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
  325. switch (cs4270->mode) {
  326. case SND_SOC_DAIFMT_I2S:
  327. reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
  328. break;
  329. case SND_SOC_DAIFMT_LEFT_J:
  330. reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
  331. break;
  332. default:
  333. dev_err(codec->dev, "unknown dai format\n");
  334. return -EINVAL;
  335. }
  336. ret = snd_soc_write(codec, CS4270_FORMAT, reg);
  337. if (ret < 0) {
  338. dev_err(codec->dev, "i2c write failed\n");
  339. return ret;
  340. }
  341. return ret;
  342. }
  343. /**
  344. * cs4270_dai_mute - enable/disable the CS4270 external mute
  345. * @dai: the SOC DAI
  346. * @mute: 0 = disable mute, 1 = enable mute
  347. *
  348. * This function toggles the mute bits in the MUTE register. The CS4270's
  349. * mute capability is intended for external muting circuitry, so if the
  350. * board does not have the MUTEA or MUTEB pins connected to such circuitry,
  351. * then this function will do nothing.
  352. */
  353. static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
  354. {
  355. struct snd_soc_codec *codec = dai->codec;
  356. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  357. int reg6;
  358. reg6 = snd_soc_read(codec, CS4270_MUTE);
  359. if (mute)
  360. reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
  361. else {
  362. reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
  363. reg6 |= cs4270->manual_mute;
  364. }
  365. return snd_soc_write(codec, CS4270_MUTE, reg6);
  366. }
  367. /**
  368. * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
  369. * alsa control.
  370. * @kcontrol: mixer control
  371. * @ucontrol: control element information
  372. *
  373. * This function basically passes the arguments on to the generic
  374. * snd_soc_put_volsw() function and saves the mute information in
  375. * our private data structure. This is because we want to prevent
  376. * cs4270_dai_mute() neglecting the user's decision to manually
  377. * mute the codec's output.
  378. *
  379. * Returns 0 for success.
  380. */
  381. static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
  382. struct snd_ctl_elem_value *ucontrol)
  383. {
  384. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  385. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  386. int left = !ucontrol->value.integer.value[0];
  387. int right = !ucontrol->value.integer.value[1];
  388. cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
  389. (right ? CS4270_MUTE_DAC_B : 0);
  390. return snd_soc_put_volsw(kcontrol, ucontrol);
  391. }
  392. /* A list of non-DAPM controls that the CS4270 supports */
  393. static const struct snd_kcontrol_new cs4270_snd_controls[] = {
  394. SOC_DOUBLE_R("Master Playback Volume",
  395. CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
  396. SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
  397. SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
  398. SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
  399. SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
  400. SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
  401. SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
  402. SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
  403. SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
  404. snd_soc_get_volsw, cs4270_soc_put_mute),
  405. };
  406. static const struct snd_soc_dai_ops cs4270_dai_ops = {
  407. .hw_params = cs4270_hw_params,
  408. .set_sysclk = cs4270_set_dai_sysclk,
  409. .set_fmt = cs4270_set_dai_fmt,
  410. .digital_mute = cs4270_dai_mute,
  411. };
  412. static struct snd_soc_dai_driver cs4270_dai = {
  413. .name = "cs4270-hifi",
  414. .playback = {
  415. .stream_name = "Playback",
  416. .channels_min = 1,
  417. .channels_max = 2,
  418. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  419. .rate_min = 4000,
  420. .rate_max = 216000,
  421. .formats = CS4270_FORMATS,
  422. },
  423. .capture = {
  424. .stream_name = "Capture",
  425. .channels_min = 1,
  426. .channels_max = 2,
  427. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  428. .rate_min = 4000,
  429. .rate_max = 216000,
  430. .formats = CS4270_FORMATS,
  431. },
  432. .ops = &cs4270_dai_ops,
  433. };
  434. /**
  435. * cs4270_probe - ASoC probe function
  436. * @pdev: platform device
  437. *
  438. * This function is called when ASoC has all the pieces it needs to
  439. * instantiate a sound driver.
  440. */
  441. static int cs4270_probe(struct snd_soc_codec *codec)
  442. {
  443. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  444. int i, ret;
  445. /* Tell ASoC what kind of I/O to use to read the registers. ASoC will
  446. * then do the I2C transactions itself.
  447. */
  448. ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs4270->control_type);
  449. if (ret < 0) {
  450. dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret);
  451. return ret;
  452. }
  453. /* Disable auto-mute. This feature appears to be buggy. In some
  454. * situations, auto-mute will not deactivate when it should, so we want
  455. * this feature disabled by default. An application (e.g. alsactl) can
  456. * re-enabled it by using the controls.
  457. */
  458. ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
  459. if (ret < 0) {
  460. dev_err(codec->dev, "i2c write failed\n");
  461. return ret;
  462. }
  463. /* Disable automatic volume control. The hardware enables, and it
  464. * causes volume change commands to be delayed, sometimes until after
  465. * playback has started. An application (e.g. alsactl) can
  466. * re-enabled it by using the controls.
  467. */
  468. ret = snd_soc_update_bits(codec, CS4270_TRANS,
  469. CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
  470. if (ret < 0) {
  471. dev_err(codec->dev, "i2c write failed\n");
  472. return ret;
  473. }
  474. /* Add the non-DAPM controls */
  475. ret = snd_soc_add_codec_controls(codec, cs4270_snd_controls,
  476. ARRAY_SIZE(cs4270_snd_controls));
  477. if (ret < 0) {
  478. dev_err(codec->dev, "failed to add controls\n");
  479. return ret;
  480. }
  481. /* get the power supply regulators */
  482. for (i = 0; i < ARRAY_SIZE(supply_names); i++)
  483. cs4270->supplies[i].supply = supply_names[i];
  484. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
  485. cs4270->supplies);
  486. if (ret < 0)
  487. return ret;
  488. ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
  489. cs4270->supplies);
  490. if (ret < 0)
  491. goto error_free_regulators;
  492. return 0;
  493. error_free_regulators:
  494. regulator_bulk_free(ARRAY_SIZE(cs4270->supplies),
  495. cs4270->supplies);
  496. return ret;
  497. }
  498. /**
  499. * cs4270_remove - ASoC remove function
  500. * @pdev: platform device
  501. *
  502. * This function is the counterpart to cs4270_probe().
  503. */
  504. static int cs4270_remove(struct snd_soc_codec *codec)
  505. {
  506. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  507. regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
  508. regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
  509. return 0;
  510. };
  511. #ifdef CONFIG_PM
  512. /* This suspend/resume implementation can handle both - a simple standby
  513. * where the codec remains powered, and a full suspend, where the voltage
  514. * domain the codec is connected to is teared down and/or any other hardware
  515. * reset condition is asserted.
  516. *
  517. * The codec's own power saving features are enabled in the suspend callback,
  518. * and all registers are written back to the hardware when resuming.
  519. */
  520. static int cs4270_soc_suspend(struct snd_soc_codec *codec)
  521. {
  522. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  523. int reg, ret;
  524. reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
  525. if (reg < 0)
  526. return reg;
  527. ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
  528. if (ret < 0)
  529. return ret;
  530. regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
  531. cs4270->supplies);
  532. return 0;
  533. }
  534. static int cs4270_soc_resume(struct snd_soc_codec *codec)
  535. {
  536. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  537. int reg, ret;
  538. ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
  539. cs4270->supplies);
  540. if (ret != 0)
  541. return ret;
  542. /* In case the device was put to hard reset during sleep, we need to
  543. * wait 500ns here before any I2C communication. */
  544. ndelay(500);
  545. /* first restore the entire register cache ... */
  546. snd_soc_cache_sync(codec);
  547. /* ... then disable the power-down bits */
  548. reg = snd_soc_read(codec, CS4270_PWRCTL);
  549. reg &= ~CS4270_PWRCTL_PDN_ALL;
  550. return snd_soc_write(codec, CS4270_PWRCTL, reg);
  551. }
  552. #else
  553. #define cs4270_soc_suspend NULL
  554. #define cs4270_soc_resume NULL
  555. #endif /* CONFIG_PM */
  556. /*
  557. * ASoC codec driver structure
  558. */
  559. static const struct snd_soc_codec_driver soc_codec_device_cs4270 = {
  560. .probe = cs4270_probe,
  561. .remove = cs4270_remove,
  562. .suspend = cs4270_soc_suspend,
  563. .resume = cs4270_soc_resume,
  564. .volatile_register = cs4270_reg_is_volatile,
  565. .readable_register = cs4270_reg_is_readable,
  566. .reg_cache_size = CS4270_LASTREG + 1,
  567. .reg_word_size = sizeof(u8),
  568. .reg_cache_default = cs4270_default_reg_cache,
  569. };
  570. /*
  571. * cs4270_of_match - the device tree bindings
  572. */
  573. static const struct of_device_id cs4270_of_match[] = {
  574. { .compatible = "cirrus,cs4270", },
  575. { }
  576. };
  577. MODULE_DEVICE_TABLE(of, cs4270_of_match);
  578. /**
  579. * cs4270_i2c_probe - initialize the I2C interface of the CS4270
  580. * @i2c_client: the I2C client object
  581. * @id: the I2C device ID (ignored)
  582. *
  583. * This function is called whenever the I2C subsystem finds a device that
  584. * matches the device ID given via a prior call to i2c_add_driver().
  585. */
  586. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  587. const struct i2c_device_id *id)
  588. {
  589. struct device_node *np = i2c_client->dev.of_node;
  590. struct cs4270_private *cs4270;
  591. int ret;
  592. /* See if we have a way to bring the codec out of reset */
  593. if (np) {
  594. enum of_gpio_flags flags;
  595. int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
  596. if (gpio_is_valid(gpio)) {
  597. ret = devm_gpio_request_one(&i2c_client->dev, gpio,
  598. flags & OF_GPIO_ACTIVE_LOW ?
  599. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
  600. "cs4270 reset");
  601. if (ret < 0)
  602. return ret;
  603. }
  604. }
  605. /* Verify that we have a CS4270 */
  606. ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
  607. if (ret < 0) {
  608. dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
  609. i2c_client->addr);
  610. return ret;
  611. }
  612. /* The top four bits of the chip ID should be 1100. */
  613. if ((ret & 0xF0) != 0xC0) {
  614. dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
  615. i2c_client->addr);
  616. return -ENODEV;
  617. }
  618. dev_info(&i2c_client->dev, "found device at i2c address %X\n",
  619. i2c_client->addr);
  620. dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
  621. cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
  622. GFP_KERNEL);
  623. if (!cs4270) {
  624. dev_err(&i2c_client->dev, "could not allocate codec\n");
  625. return -ENOMEM;
  626. }
  627. i2c_set_clientdata(i2c_client, cs4270);
  628. cs4270->control_type = SND_SOC_I2C;
  629. ret = snd_soc_register_codec(&i2c_client->dev,
  630. &soc_codec_device_cs4270, &cs4270_dai, 1);
  631. return ret;
  632. }
  633. /**
  634. * cs4270_i2c_remove - remove an I2C device
  635. * @i2c_client: the I2C client object
  636. *
  637. * This function is the counterpart to cs4270_i2c_probe().
  638. */
  639. static int cs4270_i2c_remove(struct i2c_client *i2c_client)
  640. {
  641. snd_soc_unregister_codec(&i2c_client->dev);
  642. return 0;
  643. }
  644. /*
  645. * cs4270_id - I2C device IDs supported by this driver
  646. */
  647. static const struct i2c_device_id cs4270_id[] = {
  648. {"cs4270", 0},
  649. {}
  650. };
  651. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  652. /*
  653. * cs4270_i2c_driver - I2C device identification
  654. *
  655. * This structure tells the I2C subsystem how to identify and support a
  656. * given I2C device type.
  657. */
  658. static struct i2c_driver cs4270_i2c_driver = {
  659. .driver = {
  660. .name = "cs4270",
  661. .owner = THIS_MODULE,
  662. .of_match_table = cs4270_of_match,
  663. },
  664. .id_table = cs4270_id,
  665. .probe = cs4270_i2c_probe,
  666. .remove = cs4270_i2c_remove,
  667. };
  668. static int __init cs4270_init(void)
  669. {
  670. return i2c_add_driver(&cs4270_i2c_driver);
  671. }
  672. module_init(cs4270_init);
  673. static void __exit cs4270_exit(void)
  674. {
  675. i2c_del_driver(&cs4270_i2c_driver);
  676. }
  677. module_exit(cs4270_exit);
  678. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  679. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  680. MODULE_LICENSE("GPL");