cs4270.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. static const struct reg_default cs4270_reg_defaults[] = {
  111. { 2, 0x00 },
  112. { 3, 0x30 },
  113. { 4, 0x00 },
  114. { 5, 0x60 },
  115. { 6, 0x20 },
  116. { 7, 0x00 },
  117. { 8, 0x00 },
  118. };
  119. static const char *supply_names[] = {
  120. "va", "vd", "vlc"
  121. };
  122. /* Private data for the CS4270 */
  123. struct cs4270_private {
  124. struct regmap *regmap;
  125. unsigned int mclk; /* Input frequency of the MCLK pin */
  126. unsigned int mode; /* The mode (I2S or left-justified) */
  127. unsigned int slave_mode;
  128. unsigned int manual_mute;
  129. /* power domain regulators */
  130. struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
  131. };
  132. static const struct snd_soc_dapm_widget cs4270_dapm_widgets[] = {
  133. SND_SOC_DAPM_INPUT("AINL"),
  134. SND_SOC_DAPM_INPUT("AINR"),
  135. SND_SOC_DAPM_OUTPUT("AOUTL"),
  136. SND_SOC_DAPM_OUTPUT("AOUTR"),
  137. };
  138. static const struct snd_soc_dapm_route cs4270_dapm_routes[] = {
  139. { "Capture", NULL, "AINL" },
  140. { "Capture", NULL, "AINR" },
  141. { "AOUTL", NULL, "Playback" },
  142. { "AOUTR", NULL, "Playback" },
  143. };
  144. /**
  145. * struct cs4270_mode_ratios - clock ratio tables
  146. * @ratio: the ratio of MCLK to the sample rate
  147. * @speed_mode: the Speed Mode bits to set in the Mode Control register for
  148. * this ratio
  149. * @mclk: the Ratio Select bits to set in the Mode Control register for this
  150. * ratio
  151. *
  152. * The data for this chart is taken from Table 5 of the CS4270 reference
  153. * manual.
  154. *
  155. * This table is used to determine how to program the Mode Control register.
  156. * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
  157. * rates the CS4270 currently supports.
  158. *
  159. * @speed_mode is the corresponding bit pattern to be written to the
  160. * MODE bits of the Mode Control Register
  161. *
  162. * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
  163. * the Mode Control Register.
  164. *
  165. * In situations where a single ratio is represented by multiple speed
  166. * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
  167. * double-speed instead of quad-speed. However, the CS4270 errata states
  168. * that divide-By-1.5 can cause failures, so we avoid that mode where
  169. * possible.
  170. *
  171. * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
  172. * work if Vd is 3.3V. If this effects you, select the
  173. * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
  174. * never select any sample rates that require divide-by-1.5.
  175. */
  176. struct cs4270_mode_ratios {
  177. unsigned int ratio;
  178. u8 speed_mode;
  179. u8 mclk;
  180. };
  181. static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
  182. {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
  183. #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
  184. {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
  185. #endif
  186. {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
  187. {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
  188. {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
  189. {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
  190. {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
  191. {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
  192. {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
  193. };
  194. /* The number of MCLK/LRCK ratios supported by the CS4270 */
  195. #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
  196. static bool cs4270_reg_is_readable(struct device *dev, unsigned int reg)
  197. {
  198. return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
  199. }
  200. static bool cs4270_reg_is_volatile(struct device *dev, unsigned int reg)
  201. {
  202. /* Unreadable registers are considered volatile */
  203. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  204. return 1;
  205. return reg == CS4270_CHIPID;
  206. }
  207. /**
  208. * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
  209. * @codec_dai: the codec DAI
  210. * @clk_id: the clock ID (ignored)
  211. * @freq: the MCLK input frequency
  212. * @dir: the clock direction (ignored)
  213. *
  214. * This function is used to tell the codec driver what the input MCLK
  215. * frequency is.
  216. *
  217. * The value of MCLK is used to determine which sample rates are supported
  218. * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
  219. * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
  220. *
  221. * This function calculates the nine ratios and determines which ones match
  222. * a standard sample rate. If there's a match, then it is added to the list
  223. * of supported sample rates.
  224. *
  225. * This function must be called by the machine driver's 'startup' function,
  226. * otherwise the list of supported sample rates will not be available in
  227. * time for ALSA.
  228. *
  229. * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
  230. * theoretically possible sample rates to be enabled. Call it again with a
  231. * proper value set one the external clock is set (most probably you would do
  232. * that from a machine's driver 'hw_param' hook.
  233. */
  234. static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  235. int clk_id, unsigned int freq, int dir)
  236. {
  237. struct snd_soc_codec *codec = codec_dai->codec;
  238. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  239. cs4270->mclk = freq;
  240. return 0;
  241. }
  242. /**
  243. * cs4270_set_dai_fmt - configure the codec for the selected audio format
  244. * @codec_dai: the codec DAI
  245. * @format: a SND_SOC_DAIFMT_x value indicating the data format
  246. *
  247. * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
  248. * codec accordingly.
  249. *
  250. * Currently, this function only supports SND_SOC_DAIFMT_I2S and
  251. * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
  252. * data for playback only, but ASoC currently does not support different
  253. * formats for playback vs. record.
  254. */
  255. static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
  256. unsigned int format)
  257. {
  258. struct snd_soc_codec *codec = codec_dai->codec;
  259. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  260. /* set DAI format */
  261. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  262. case SND_SOC_DAIFMT_I2S:
  263. case SND_SOC_DAIFMT_LEFT_J:
  264. cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  265. break;
  266. default:
  267. dev_err(codec->dev, "invalid dai format\n");
  268. return -EINVAL;
  269. }
  270. /* set master/slave audio interface */
  271. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  272. case SND_SOC_DAIFMT_CBS_CFS:
  273. cs4270->slave_mode = 1;
  274. break;
  275. case SND_SOC_DAIFMT_CBM_CFM:
  276. cs4270->slave_mode = 0;
  277. break;
  278. default:
  279. /* all other modes are unsupported by the hardware */
  280. dev_err(codec->dev, "Unknown master/slave configuration\n");
  281. return -EINVAL;
  282. }
  283. return 0;
  284. }
  285. /**
  286. * cs4270_hw_params - program the CS4270 with the given hardware parameters.
  287. * @substream: the audio stream
  288. * @params: the hardware parameters to set
  289. * @dai: the SOC DAI (ignored)
  290. *
  291. * This function programs the hardware with the values provided.
  292. * Specifically, the sample rate and the data format.
  293. *
  294. * The .ops functions are used to provide board-specific data, like input
  295. * frequencies, to this driver. This function takes that information,
  296. * combines it with the hardware parameters provided, and programs the
  297. * hardware accordingly.
  298. */
  299. static int cs4270_hw_params(struct snd_pcm_substream *substream,
  300. struct snd_pcm_hw_params *params,
  301. struct snd_soc_dai *dai)
  302. {
  303. struct snd_soc_codec *codec = dai->codec;
  304. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  305. int ret;
  306. unsigned int i;
  307. unsigned int rate;
  308. unsigned int ratio;
  309. int reg;
  310. /* Figure out which MCLK/LRCK ratio to use */
  311. rate = params_rate(params); /* Sampling rate, in Hz */
  312. ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
  313. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  314. if (cs4270_mode_ratios[i].ratio == ratio)
  315. break;
  316. }
  317. if (i == NUM_MCLK_RATIOS) {
  318. /* We did not find a matching ratio */
  319. dev_err(codec->dev, "could not find matching ratio\n");
  320. return -EINVAL;
  321. }
  322. /* Set the sample rate */
  323. reg = snd_soc_read(codec, CS4270_MODE);
  324. reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
  325. reg |= cs4270_mode_ratios[i].mclk;
  326. if (cs4270->slave_mode)
  327. reg |= CS4270_MODE_SLAVE;
  328. else
  329. reg |= cs4270_mode_ratios[i].speed_mode;
  330. ret = snd_soc_write(codec, CS4270_MODE, reg);
  331. if (ret < 0) {
  332. dev_err(codec->dev, "i2c write failed\n");
  333. return ret;
  334. }
  335. /* Set the DAI format */
  336. reg = snd_soc_read(codec, CS4270_FORMAT);
  337. reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
  338. switch (cs4270->mode) {
  339. case SND_SOC_DAIFMT_I2S:
  340. reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
  341. break;
  342. case SND_SOC_DAIFMT_LEFT_J:
  343. reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
  344. break;
  345. default:
  346. dev_err(codec->dev, "unknown dai format\n");
  347. return -EINVAL;
  348. }
  349. ret = snd_soc_write(codec, CS4270_FORMAT, reg);
  350. if (ret < 0) {
  351. dev_err(codec->dev, "i2c write failed\n");
  352. return ret;
  353. }
  354. return ret;
  355. }
  356. /**
  357. * cs4270_dai_mute - enable/disable the CS4270 external mute
  358. * @dai: the SOC DAI
  359. * @mute: 0 = disable mute, 1 = enable mute
  360. *
  361. * This function toggles the mute bits in the MUTE register. The CS4270's
  362. * mute capability is intended for external muting circuitry, so if the
  363. * board does not have the MUTEA or MUTEB pins connected to such circuitry,
  364. * then this function will do nothing.
  365. */
  366. static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
  367. {
  368. struct snd_soc_codec *codec = dai->codec;
  369. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  370. int reg6;
  371. reg6 = snd_soc_read(codec, CS4270_MUTE);
  372. if (mute)
  373. reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
  374. else {
  375. reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
  376. reg6 |= cs4270->manual_mute;
  377. }
  378. return snd_soc_write(codec, CS4270_MUTE, reg6);
  379. }
  380. /**
  381. * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
  382. * alsa control.
  383. * @kcontrol: mixer control
  384. * @ucontrol: control element information
  385. *
  386. * This function basically passes the arguments on to the generic
  387. * snd_soc_put_volsw() function and saves the mute information in
  388. * our private data structure. This is because we want to prevent
  389. * cs4270_dai_mute() neglecting the user's decision to manually
  390. * mute the codec's output.
  391. *
  392. * Returns 0 for success.
  393. */
  394. static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_value *ucontrol)
  396. {
  397. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  398. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  399. int left = !ucontrol->value.integer.value[0];
  400. int right = !ucontrol->value.integer.value[1];
  401. cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
  402. (right ? CS4270_MUTE_DAC_B : 0);
  403. return snd_soc_put_volsw(kcontrol, ucontrol);
  404. }
  405. /* A list of non-DAPM controls that the CS4270 supports */
  406. static const struct snd_kcontrol_new cs4270_snd_controls[] = {
  407. SOC_DOUBLE_R("Master Playback Volume",
  408. CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
  409. SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
  410. SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
  411. SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
  412. SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
  413. SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
  414. SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
  415. SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
  416. SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
  417. snd_soc_get_volsw, cs4270_soc_put_mute),
  418. };
  419. static const struct snd_soc_dai_ops cs4270_dai_ops = {
  420. .hw_params = cs4270_hw_params,
  421. .set_sysclk = cs4270_set_dai_sysclk,
  422. .set_fmt = cs4270_set_dai_fmt,
  423. .digital_mute = cs4270_dai_mute,
  424. };
  425. static struct snd_soc_dai_driver cs4270_dai = {
  426. .name = "cs4270-hifi",
  427. .playback = {
  428. .stream_name = "Playback",
  429. .channels_min = 2,
  430. .channels_max = 2,
  431. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  432. .rate_min = 4000,
  433. .rate_max = 216000,
  434. .formats = CS4270_FORMATS,
  435. },
  436. .capture = {
  437. .stream_name = "Capture",
  438. .channels_min = 2,
  439. .channels_max = 2,
  440. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  441. .rate_min = 4000,
  442. .rate_max = 216000,
  443. .formats = CS4270_FORMATS,
  444. },
  445. .ops = &cs4270_dai_ops,
  446. };
  447. /**
  448. * cs4270_probe - ASoC probe function
  449. * @pdev: platform device
  450. *
  451. * This function is called when ASoC has all the pieces it needs to
  452. * instantiate a sound driver.
  453. */
  454. static int cs4270_probe(struct snd_soc_codec *codec)
  455. {
  456. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  457. int ret;
  458. /* Disable auto-mute. This feature appears to be buggy. In some
  459. * situations, auto-mute will not deactivate when it should, so we want
  460. * this feature disabled by default. An application (e.g. alsactl) can
  461. * re-enabled it by using the controls.
  462. */
  463. ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
  464. if (ret < 0) {
  465. dev_err(codec->dev, "i2c write failed\n");
  466. return ret;
  467. }
  468. /* Disable automatic volume control. The hardware enables, and it
  469. * causes volume change commands to be delayed, sometimes until after
  470. * playback has started. An application (e.g. alsactl) can
  471. * re-enabled it by using the controls.
  472. */
  473. ret = snd_soc_update_bits(codec, CS4270_TRANS,
  474. CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
  475. if (ret < 0) {
  476. dev_err(codec->dev, "i2c write failed\n");
  477. return ret;
  478. }
  479. ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
  480. cs4270->supplies);
  481. return ret;
  482. }
  483. /**
  484. * cs4270_remove - ASoC remove function
  485. * @pdev: platform device
  486. *
  487. * This function is the counterpart to cs4270_probe().
  488. */
  489. static int cs4270_remove(struct snd_soc_codec *codec)
  490. {
  491. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  492. regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
  493. return 0;
  494. };
  495. #ifdef CONFIG_PM
  496. /* This suspend/resume implementation can handle both - a simple standby
  497. * where the codec remains powered, and a full suspend, where the voltage
  498. * domain the codec is connected to is teared down and/or any other hardware
  499. * reset condition is asserted.
  500. *
  501. * The codec's own power saving features are enabled in the suspend callback,
  502. * and all registers are written back to the hardware when resuming.
  503. */
  504. static int cs4270_soc_suspend(struct snd_soc_codec *codec)
  505. {
  506. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  507. int reg, ret;
  508. reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
  509. if (reg < 0)
  510. return reg;
  511. ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
  512. if (ret < 0)
  513. return ret;
  514. regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
  515. cs4270->supplies);
  516. return 0;
  517. }
  518. static int cs4270_soc_resume(struct snd_soc_codec *codec)
  519. {
  520. struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
  521. int reg, ret;
  522. ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
  523. cs4270->supplies);
  524. if (ret != 0)
  525. return ret;
  526. /* In case the device was put to hard reset during sleep, we need to
  527. * wait 500ns here before any I2C communication. */
  528. ndelay(500);
  529. /* first restore the entire register cache ... */
  530. regcache_sync(cs4270->regmap);
  531. /* ... then disable the power-down bits */
  532. reg = snd_soc_read(codec, CS4270_PWRCTL);
  533. reg &= ~CS4270_PWRCTL_PDN_ALL;
  534. return snd_soc_write(codec, CS4270_PWRCTL, reg);
  535. }
  536. #else
  537. #define cs4270_soc_suspend NULL
  538. #define cs4270_soc_resume NULL
  539. #endif /* CONFIG_PM */
  540. /*
  541. * ASoC codec driver structure
  542. */
  543. static const struct snd_soc_codec_driver soc_codec_device_cs4270 = {
  544. .probe = cs4270_probe,
  545. .remove = cs4270_remove,
  546. .suspend = cs4270_soc_suspend,
  547. .resume = cs4270_soc_resume,
  548. .component_driver = {
  549. .controls = cs4270_snd_controls,
  550. .num_controls = ARRAY_SIZE(cs4270_snd_controls),
  551. .dapm_widgets = cs4270_dapm_widgets,
  552. .num_dapm_widgets = ARRAY_SIZE(cs4270_dapm_widgets),
  553. .dapm_routes = cs4270_dapm_routes,
  554. .num_dapm_routes = ARRAY_SIZE(cs4270_dapm_routes),
  555. },
  556. };
  557. /*
  558. * cs4270_of_match - the device tree bindings
  559. */
  560. static const struct of_device_id cs4270_of_match[] = {
  561. { .compatible = "cirrus,cs4270", },
  562. { }
  563. };
  564. MODULE_DEVICE_TABLE(of, cs4270_of_match);
  565. static const struct regmap_config cs4270_regmap = {
  566. .reg_bits = 8,
  567. .val_bits = 8,
  568. .max_register = CS4270_LASTREG,
  569. .reg_defaults = cs4270_reg_defaults,
  570. .num_reg_defaults = ARRAY_SIZE(cs4270_reg_defaults),
  571. .cache_type = REGCACHE_RBTREE,
  572. .readable_reg = cs4270_reg_is_readable,
  573. .volatile_reg = cs4270_reg_is_volatile,
  574. };
  575. /**
  576. * cs4270_i2c_probe - initialize the I2C interface of the CS4270
  577. * @i2c_client: the I2C client object
  578. * @id: the I2C device ID (ignored)
  579. *
  580. * This function is called whenever the I2C subsystem finds a device that
  581. * matches the device ID given via a prior call to i2c_add_driver().
  582. */
  583. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  584. const struct i2c_device_id *id)
  585. {
  586. struct device_node *np = i2c_client->dev.of_node;
  587. struct cs4270_private *cs4270;
  588. unsigned int val;
  589. int ret, i;
  590. cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
  591. GFP_KERNEL);
  592. if (!cs4270)
  593. return -ENOMEM;
  594. /* get the power supply regulators */
  595. for (i = 0; i < ARRAY_SIZE(supply_names); i++)
  596. cs4270->supplies[i].supply = supply_names[i];
  597. ret = devm_regulator_bulk_get(&i2c_client->dev,
  598. ARRAY_SIZE(cs4270->supplies),
  599. cs4270->supplies);
  600. if (ret < 0)
  601. return ret;
  602. /* See if we have a way to bring the codec out of reset */
  603. if (np) {
  604. enum of_gpio_flags flags;
  605. int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
  606. if (gpio_is_valid(gpio)) {
  607. ret = devm_gpio_request_one(&i2c_client->dev, gpio,
  608. flags & OF_GPIO_ACTIVE_LOW ?
  609. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
  610. "cs4270 reset");
  611. if (ret < 0)
  612. return ret;
  613. }
  614. }
  615. cs4270->regmap = devm_regmap_init_i2c(i2c_client, &cs4270_regmap);
  616. if (IS_ERR(cs4270->regmap))
  617. return PTR_ERR(cs4270->regmap);
  618. /* Verify that we have a CS4270 */
  619. ret = regmap_read(cs4270->regmap, CS4270_CHIPID, &val);
  620. if (ret < 0) {
  621. dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
  622. i2c_client->addr);
  623. return ret;
  624. }
  625. /* The top four bits of the chip ID should be 1100. */
  626. if ((val & 0xF0) != 0xC0) {
  627. dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
  628. i2c_client->addr);
  629. return -ENODEV;
  630. }
  631. dev_info(&i2c_client->dev, "found device at i2c address %X\n",
  632. i2c_client->addr);
  633. dev_info(&i2c_client->dev, "hardware revision %X\n", val & 0xF);
  634. i2c_set_clientdata(i2c_client, cs4270);
  635. ret = snd_soc_register_codec(&i2c_client->dev,
  636. &soc_codec_device_cs4270, &cs4270_dai, 1);
  637. return ret;
  638. }
  639. /**
  640. * cs4270_i2c_remove - remove an I2C device
  641. * @i2c_client: the I2C client object
  642. *
  643. * This function is the counterpart to cs4270_i2c_probe().
  644. */
  645. static int cs4270_i2c_remove(struct i2c_client *i2c_client)
  646. {
  647. snd_soc_unregister_codec(&i2c_client->dev);
  648. return 0;
  649. }
  650. /*
  651. * cs4270_id - I2C device IDs supported by this driver
  652. */
  653. static const struct i2c_device_id cs4270_id[] = {
  654. {"cs4270", 0},
  655. {}
  656. };
  657. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  658. /*
  659. * cs4270_i2c_driver - I2C device identification
  660. *
  661. * This structure tells the I2C subsystem how to identify and support a
  662. * given I2C device type.
  663. */
  664. static struct i2c_driver cs4270_i2c_driver = {
  665. .driver = {
  666. .name = "cs4270",
  667. .of_match_table = cs4270_of_match,
  668. },
  669. .id_table = cs4270_id,
  670. .probe = cs4270_i2c_probe,
  671. .remove = cs4270_i2c_remove,
  672. };
  673. module_i2c_driver(cs4270_i2c_driver);
  674. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  675. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  676. MODULE_LICENSE("GPL");