cs4270.c 23 KB

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