cs4270.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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/platform_device.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 "cs4270.h"
  31. /*
  32. * The codec isn't really big-endian or little-endian, since the I2S
  33. * interface requires data to be sent serially with the MSbit first.
  34. * However, to support BE and LE I2S devices, we specify both here. That
  35. * way, ALSA will always match the bit patterns.
  36. */
  37. #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  38. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  39. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  40. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  41. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  42. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  43. /* CS4270 registers addresses */
  44. #define CS4270_CHIPID 0x01 /* Chip ID */
  45. #define CS4270_PWRCTL 0x02 /* Power Control */
  46. #define CS4270_MODE 0x03 /* Mode Control */
  47. #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
  48. #define CS4270_TRANS 0x05 /* Transition Control */
  49. #define CS4270_MUTE 0x06 /* Mute Control */
  50. #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
  51. #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
  52. #define CS4270_FIRSTREG 0x01
  53. #define CS4270_LASTREG 0x08
  54. #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
  55. #define CS4270_I2C_INCR 0x80
  56. /* Bit masks for the CS4270 registers */
  57. #define CS4270_CHIPID_ID 0xF0
  58. #define CS4270_CHIPID_REV 0x0F
  59. #define CS4270_PWRCTL_FREEZE 0x80
  60. #define CS4270_PWRCTL_PDN_ADC 0x20
  61. #define CS4270_PWRCTL_PDN_DAC 0x02
  62. #define CS4270_PWRCTL_PDN 0x01
  63. #define CS4270_PWRCTL_PDN_ALL \
  64. (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
  65. #define CS4270_MODE_SPEED_MASK 0x30
  66. #define CS4270_MODE_1X 0x00
  67. #define CS4270_MODE_2X 0x10
  68. #define CS4270_MODE_4X 0x20
  69. #define CS4270_MODE_SLAVE 0x30
  70. #define CS4270_MODE_DIV_MASK 0x0E
  71. #define CS4270_MODE_DIV1 0x00
  72. #define CS4270_MODE_DIV15 0x02
  73. #define CS4270_MODE_DIV2 0x04
  74. #define CS4270_MODE_DIV3 0x06
  75. #define CS4270_MODE_DIV4 0x08
  76. #define CS4270_MODE_POPGUARD 0x01
  77. #define CS4270_FORMAT_FREEZE_A 0x80
  78. #define CS4270_FORMAT_FREEZE_B 0x40
  79. #define CS4270_FORMAT_LOOPBACK 0x20
  80. #define CS4270_FORMAT_DAC_MASK 0x18
  81. #define CS4270_FORMAT_DAC_LJ 0x00
  82. #define CS4270_FORMAT_DAC_I2S 0x08
  83. #define CS4270_FORMAT_DAC_RJ16 0x18
  84. #define CS4270_FORMAT_DAC_RJ24 0x10
  85. #define CS4270_FORMAT_ADC_MASK 0x01
  86. #define CS4270_FORMAT_ADC_LJ 0x00
  87. #define CS4270_FORMAT_ADC_I2S 0x01
  88. #define CS4270_TRANS_ONE_VOL 0x80
  89. #define CS4270_TRANS_SOFT 0x40
  90. #define CS4270_TRANS_ZERO 0x20
  91. #define CS4270_TRANS_INV_ADC_A 0x08
  92. #define CS4270_TRANS_INV_ADC_B 0x10
  93. #define CS4270_TRANS_INV_DAC_A 0x02
  94. #define CS4270_TRANS_INV_DAC_B 0x04
  95. #define CS4270_TRANS_DEEMPH 0x01
  96. #define CS4270_MUTE_AUTO 0x20
  97. #define CS4270_MUTE_ADC_A 0x08
  98. #define CS4270_MUTE_ADC_B 0x10
  99. #define CS4270_MUTE_POLARITY 0x04
  100. #define CS4270_MUTE_DAC_A 0x01
  101. #define CS4270_MUTE_DAC_B 0x02
  102. /* Private data for the CS4270 */
  103. struct cs4270_private {
  104. struct snd_soc_codec codec;
  105. u8 reg_cache[CS4270_NUMREGS];
  106. unsigned int mclk; /* Input frequency of the MCLK pin */
  107. unsigned int mode; /* The mode (I2S or left-justified) */
  108. unsigned int slave_mode;
  109. unsigned int manual_mute;
  110. };
  111. /**
  112. * struct cs4270_mode_ratios - clock ratio tables
  113. * @ratio: the ratio of MCLK to the sample rate
  114. * @speed_mode: the Speed Mode bits to set in the Mode Control register for
  115. * this ratio
  116. * @mclk: the Ratio Select bits to set in the Mode Control register for this
  117. * ratio
  118. *
  119. * The data for this chart is taken from Table 5 of the CS4270 reference
  120. * manual.
  121. *
  122. * This table is used to determine how to program the Mode Control register.
  123. * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
  124. * rates the CS4270 currently supports.
  125. *
  126. * @speed_mode is the corresponding bit pattern to be written to the
  127. * MODE bits of the Mode Control Register
  128. *
  129. * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
  130. * the Mode Control Register.
  131. *
  132. * In situations where a single ratio is represented by multiple speed
  133. * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
  134. * double-speed instead of quad-speed. However, the CS4270 errata states
  135. * that divide-By-1.5 can cause failures, so we avoid that mode where
  136. * possible.
  137. *
  138. * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
  139. * work if Vd is 3.3V. If this effects you, select the
  140. * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
  141. * never select any sample rates that require divide-by-1.5.
  142. */
  143. struct cs4270_mode_ratios {
  144. unsigned int ratio;
  145. u8 speed_mode;
  146. u8 mclk;
  147. };
  148. static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
  149. {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
  150. #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
  151. {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
  152. #endif
  153. {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
  154. {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
  155. {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
  156. {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
  157. {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
  158. {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
  159. {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
  160. };
  161. /* The number of MCLK/LRCK ratios supported by the CS4270 */
  162. #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
  163. /**
  164. * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
  165. * @codec_dai: the codec DAI
  166. * @clk_id: the clock ID (ignored)
  167. * @freq: the MCLK input frequency
  168. * @dir: the clock direction (ignored)
  169. *
  170. * This function is used to tell the codec driver what the input MCLK
  171. * frequency is.
  172. *
  173. * The value of MCLK is used to determine which sample rates are supported
  174. * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
  175. * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
  176. *
  177. * This function calculates the nine ratios and determines which ones match
  178. * a standard sample rate. If there's a match, then it is added to the list
  179. * of supported sample rates.
  180. *
  181. * This function must be called by the machine driver's 'startup' function,
  182. * otherwise the list of supported sample rates will not be available in
  183. * time for ALSA.
  184. */
  185. static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  186. int clk_id, unsigned int freq, int dir)
  187. {
  188. struct snd_soc_codec *codec = codec_dai->codec;
  189. struct cs4270_private *cs4270 = codec->private_data;
  190. unsigned int rates = 0;
  191. unsigned int rate_min = -1;
  192. unsigned int rate_max = 0;
  193. unsigned int i;
  194. cs4270->mclk = freq;
  195. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  196. unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
  197. rates |= snd_pcm_rate_to_rate_bit(rate);
  198. if (rate < rate_min)
  199. rate_min = rate;
  200. if (rate > rate_max)
  201. rate_max = rate;
  202. }
  203. /* FIXME: soc should support a rate list */
  204. rates &= ~SNDRV_PCM_RATE_KNOT;
  205. if (!rates) {
  206. dev_err(codec->dev, "could not find a valid sample rate\n");
  207. return -EINVAL;
  208. }
  209. codec_dai->playback.rates = rates;
  210. codec_dai->playback.rate_min = rate_min;
  211. codec_dai->playback.rate_max = rate_max;
  212. codec_dai->capture.rates = rates;
  213. codec_dai->capture.rate_min = rate_min;
  214. codec_dai->capture.rate_max = rate_max;
  215. return 0;
  216. }
  217. /**
  218. * cs4270_set_dai_fmt - configure the codec for the selected audio format
  219. * @codec_dai: the codec DAI
  220. * @format: a SND_SOC_DAIFMT_x value indicating the data format
  221. *
  222. * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
  223. * codec accordingly.
  224. *
  225. * Currently, this function only supports SND_SOC_DAIFMT_I2S and
  226. * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
  227. * data for playback only, but ASoC currently does not support different
  228. * formats for playback vs. record.
  229. */
  230. static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
  231. unsigned int format)
  232. {
  233. struct snd_soc_codec *codec = codec_dai->codec;
  234. struct cs4270_private *cs4270 = codec->private_data;
  235. int ret = 0;
  236. /* set DAI format */
  237. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  238. case SND_SOC_DAIFMT_I2S:
  239. case SND_SOC_DAIFMT_LEFT_J:
  240. cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  241. break;
  242. default:
  243. dev_err(codec->dev, "invalid dai format\n");
  244. ret = -EINVAL;
  245. }
  246. /* set master/slave audio interface */
  247. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  248. case SND_SOC_DAIFMT_CBS_CFS:
  249. cs4270->slave_mode = 1;
  250. break;
  251. case SND_SOC_DAIFMT_CBM_CFM:
  252. cs4270->slave_mode = 0;
  253. break;
  254. default:
  255. /* all other modes are unsupported by the hardware */
  256. ret = -EINVAL;
  257. }
  258. return ret;
  259. }
  260. /**
  261. * cs4270_fill_cache - pre-fill the CS4270 register cache.
  262. * @codec: the codec for this CS4270
  263. *
  264. * This function fills in the CS4270 register cache by reading the register
  265. * values from the hardware.
  266. *
  267. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  268. * After the initial read to pre-fill the cache, the CS4270 never updates
  269. * the register values, so we won't have a cache coherency problem.
  270. *
  271. * We use the auto-increment feature of the CS4270 to read all registers in
  272. * one shot.
  273. */
  274. static int cs4270_fill_cache(struct snd_soc_codec *codec)
  275. {
  276. u8 *cache = codec->reg_cache;
  277. struct i2c_client *i2c_client = codec->control_data;
  278. s32 length;
  279. length = i2c_smbus_read_i2c_block_data(i2c_client,
  280. CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache);
  281. if (length != CS4270_NUMREGS) {
  282. dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
  283. i2c_client->addr);
  284. return -EIO;
  285. }
  286. return 0;
  287. }
  288. /**
  289. * cs4270_read_reg_cache - read from the CS4270 register cache.
  290. * @codec: the codec for this CS4270
  291. * @reg: the register to read
  292. *
  293. * This function returns the value for a given register. It reads only from
  294. * the register cache, not the hardware itself.
  295. *
  296. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  297. * After the initial read to pre-fill the cache, the CS4270 never updates
  298. * the register values, so we won't have a cache coherency problem.
  299. */
  300. static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
  301. unsigned int reg)
  302. {
  303. u8 *cache = codec->reg_cache;
  304. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  305. return -EIO;
  306. return cache[reg - CS4270_FIRSTREG];
  307. }
  308. /**
  309. * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
  310. * @codec: the codec for this CS4270
  311. * @reg: the register to write
  312. * @value: the value to write to the register
  313. *
  314. * This function writes the given value to the given CS4270 register, and
  315. * also updates the register cache.
  316. *
  317. * Note that we don't use the hw_write function pointer of snd_soc_codec.
  318. * That's because it's too clunky: the hw_write_t prototype does not match
  319. * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
  320. */
  321. static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
  322. unsigned int value)
  323. {
  324. u8 *cache = codec->reg_cache;
  325. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  326. return -EIO;
  327. /* Only perform an I2C operation if the new value is different */
  328. if (cache[reg - CS4270_FIRSTREG] != value) {
  329. struct i2c_client *client = codec->control_data;
  330. if (i2c_smbus_write_byte_data(client, reg, value)) {
  331. dev_err(codec->dev, "i2c write failed\n");
  332. return -EIO;
  333. }
  334. /* We've written to the hardware, so update the cache */
  335. cache[reg - CS4270_FIRSTREG] = value;
  336. }
  337. return 0;
  338. }
  339. /**
  340. * cs4270_hw_params - program the CS4270 with the given hardware parameters.
  341. * @substream: the audio stream
  342. * @params: the hardware parameters to set
  343. * @dai: the SOC DAI (ignored)
  344. *
  345. * This function programs the hardware with the values provided.
  346. * Specifically, the sample rate and the data format.
  347. *
  348. * The .ops functions are used to provide board-specific data, like input
  349. * frequencies, to this driver. This function takes that information,
  350. * combines it with the hardware parameters provided, and programs the
  351. * hardware accordingly.
  352. */
  353. static int cs4270_hw_params(struct snd_pcm_substream *substream,
  354. struct snd_pcm_hw_params *params,
  355. struct snd_soc_dai *dai)
  356. {
  357. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  358. struct snd_soc_device *socdev = rtd->socdev;
  359. struct snd_soc_codec *codec = socdev->card->codec;
  360. struct cs4270_private *cs4270 = codec->private_data;
  361. int ret;
  362. unsigned int i;
  363. unsigned int rate;
  364. unsigned int ratio;
  365. int reg;
  366. /* Figure out which MCLK/LRCK ratio to use */
  367. rate = params_rate(params); /* Sampling rate, in Hz */
  368. ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
  369. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  370. if (cs4270_mode_ratios[i].ratio == ratio)
  371. break;
  372. }
  373. if (i == NUM_MCLK_RATIOS) {
  374. /* We did not find a matching ratio */
  375. dev_err(codec->dev, "could not find matching ratio\n");
  376. return -EINVAL;
  377. }
  378. /* Set the sample rate */
  379. reg = snd_soc_read(codec, CS4270_MODE);
  380. reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
  381. reg |= cs4270_mode_ratios[i].mclk;
  382. if (cs4270->slave_mode)
  383. reg |= CS4270_MODE_SLAVE;
  384. else
  385. reg |= cs4270_mode_ratios[i].speed_mode;
  386. ret = snd_soc_write(codec, CS4270_MODE, reg);
  387. if (ret < 0) {
  388. dev_err(codec->dev, "i2c write failed\n");
  389. return ret;
  390. }
  391. /* Set the DAI format */
  392. reg = snd_soc_read(codec, CS4270_FORMAT);
  393. reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
  394. switch (cs4270->mode) {
  395. case SND_SOC_DAIFMT_I2S:
  396. reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
  397. break;
  398. case SND_SOC_DAIFMT_LEFT_J:
  399. reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
  400. break;
  401. default:
  402. dev_err(codec->dev, "unknown dai format\n");
  403. return -EINVAL;
  404. }
  405. ret = snd_soc_write(codec, CS4270_FORMAT, reg);
  406. if (ret < 0) {
  407. dev_err(codec->dev, "i2c write failed\n");
  408. return ret;
  409. }
  410. return ret;
  411. }
  412. /**
  413. * cs4270_dai_mute - enable/disable the CS4270 external mute
  414. * @dai: the SOC DAI
  415. * @mute: 0 = disable mute, 1 = enable mute
  416. *
  417. * This function toggles the mute bits in the MUTE register. The CS4270's
  418. * mute capability is intended for external muting circuitry, so if the
  419. * board does not have the MUTEA or MUTEB pins connected to such circuitry,
  420. * then this function will do nothing.
  421. */
  422. static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
  423. {
  424. struct snd_soc_codec *codec = dai->codec;
  425. struct cs4270_private *cs4270 = codec->private_data;
  426. int reg6;
  427. reg6 = snd_soc_read(codec, CS4270_MUTE);
  428. if (mute)
  429. reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
  430. else {
  431. reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
  432. reg6 |= cs4270->manual_mute;
  433. }
  434. return snd_soc_write(codec, CS4270_MUTE, reg6);
  435. }
  436. /**
  437. * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
  438. * alsa control.
  439. * @kcontrol: mixer control
  440. * @ucontrol: control element information
  441. *
  442. * This function basically passes the arguments on to the generic
  443. * snd_soc_put_volsw() function and saves the mute information in
  444. * our private data structure. This is because we want to prevent
  445. * cs4270_dai_mute() neglecting the user's decision to manually
  446. * mute the codec's output.
  447. *
  448. * Returns 0 for success.
  449. */
  450. static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
  451. struct snd_ctl_elem_value *ucontrol)
  452. {
  453. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  454. struct cs4270_private *cs4270 = codec->private_data;
  455. int left = !ucontrol->value.integer.value[0];
  456. int right = !ucontrol->value.integer.value[1];
  457. cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
  458. (right ? CS4270_MUTE_DAC_B : 0);
  459. return snd_soc_put_volsw(kcontrol, ucontrol);
  460. }
  461. /* A list of non-DAPM controls that the CS4270 supports */
  462. static const struct snd_kcontrol_new cs4270_snd_controls[] = {
  463. SOC_DOUBLE_R("Master Playback Volume",
  464. CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
  465. SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
  466. SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
  467. SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
  468. SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
  469. SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
  470. SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
  471. SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
  472. snd_soc_get_volsw, cs4270_soc_put_mute),
  473. };
  474. /*
  475. * cs4270_codec - global variable to store codec for the ASoC probe function
  476. *
  477. * If struct i2c_driver had a private_data field, we wouldn't need to use
  478. * cs4270_codec. This is the only way to pass the codec structure from
  479. * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
  480. * way to synchronize these two functions. cs4270_i2c_probe() can be called
  481. * multiple times before cs4270_probe() is called even once. So for now, we
  482. * also only allow cs4270_i2c_probe() to be run once. That means that we do
  483. * not support more than one cs4270 device in the system, at least for now.
  484. */
  485. static struct snd_soc_codec *cs4270_codec;
  486. static struct snd_soc_dai_ops cs4270_dai_ops = {
  487. .hw_params = cs4270_hw_params,
  488. .set_sysclk = cs4270_set_dai_sysclk,
  489. .set_fmt = cs4270_set_dai_fmt,
  490. .digital_mute = cs4270_dai_mute,
  491. };
  492. struct snd_soc_dai cs4270_dai = {
  493. .name = "cs4270",
  494. .playback = {
  495. .stream_name = "Playback",
  496. .channels_min = 1,
  497. .channels_max = 2,
  498. .rates = 0,
  499. .formats = CS4270_FORMATS,
  500. },
  501. .capture = {
  502. .stream_name = "Capture",
  503. .channels_min = 1,
  504. .channels_max = 2,
  505. .rates = 0,
  506. .formats = CS4270_FORMATS,
  507. },
  508. .ops = &cs4270_dai_ops,
  509. };
  510. EXPORT_SYMBOL_GPL(cs4270_dai);
  511. /**
  512. * cs4270_probe - ASoC probe function
  513. * @pdev: platform device
  514. *
  515. * This function is called when ASoC has all the pieces it needs to
  516. * instantiate a sound driver.
  517. */
  518. static int cs4270_probe(struct platform_device *pdev)
  519. {
  520. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  521. struct snd_soc_codec *codec = cs4270_codec;
  522. int ret;
  523. /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
  524. socdev->card->codec = codec;
  525. /* Register PCMs */
  526. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  527. if (ret < 0) {
  528. dev_err(codec->dev, "failed to create pcms\n");
  529. return ret;
  530. }
  531. /* Add the non-DAPM controls */
  532. ret = snd_soc_add_controls(codec, cs4270_snd_controls,
  533. ARRAY_SIZE(cs4270_snd_controls));
  534. if (ret < 0) {
  535. dev_err(codec->dev, "failed to add controls\n");
  536. goto error_free_pcms;
  537. }
  538. /* And finally, register the socdev */
  539. ret = snd_soc_init_card(socdev);
  540. if (ret < 0) {
  541. dev_err(codec->dev, "failed to register card\n");
  542. goto error_free_pcms;
  543. }
  544. return 0;
  545. error_free_pcms:
  546. snd_soc_free_pcms(socdev);
  547. return ret;
  548. }
  549. /**
  550. * cs4270_remove - ASoC remove function
  551. * @pdev: platform device
  552. *
  553. * This function is the counterpart to cs4270_probe().
  554. */
  555. static int cs4270_remove(struct platform_device *pdev)
  556. {
  557. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  558. snd_soc_free_pcms(socdev);
  559. return 0;
  560. };
  561. /**
  562. * cs4270_i2c_probe - initialize the I2C interface of the CS4270
  563. * @i2c_client: the I2C client object
  564. * @id: the I2C device ID (ignored)
  565. *
  566. * This function is called whenever the I2C subsystem finds a device that
  567. * matches the device ID given via a prior call to i2c_add_driver().
  568. */
  569. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  570. const struct i2c_device_id *id)
  571. {
  572. struct snd_soc_codec *codec;
  573. struct cs4270_private *cs4270;
  574. unsigned int reg;
  575. int ret;
  576. /* For now, we only support one cs4270 device in the system. See the
  577. * comment for cs4270_codec.
  578. */
  579. if (cs4270_codec) {
  580. dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
  581. i2c_client->addr);
  582. dev_err(&i2c_client->dev, "only one per board allowed\n");
  583. /* Should we return something other than ENODEV here? */
  584. return -ENODEV;
  585. }
  586. /* Verify that we have a CS4270 */
  587. ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
  588. if (ret < 0) {
  589. dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
  590. i2c_client->addr);
  591. return ret;
  592. }
  593. /* The top four bits of the chip ID should be 1100. */
  594. if ((ret & 0xF0) != 0xC0) {
  595. dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
  596. i2c_client->addr);
  597. return -ENODEV;
  598. }
  599. dev_info(&i2c_client->dev, "found device at i2c address %X\n",
  600. i2c_client->addr);
  601. dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
  602. /* Allocate enough space for the snd_soc_codec structure
  603. and our private data together. */
  604. cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
  605. if (!cs4270) {
  606. dev_err(&i2c_client->dev, "could not allocate codec\n");
  607. return -ENOMEM;
  608. }
  609. codec = &cs4270->codec;
  610. mutex_init(&codec->mutex);
  611. INIT_LIST_HEAD(&codec->dapm_widgets);
  612. INIT_LIST_HEAD(&codec->dapm_paths);
  613. codec->dev = &i2c_client->dev;
  614. codec->name = "CS4270";
  615. codec->owner = THIS_MODULE;
  616. codec->dai = &cs4270_dai;
  617. codec->num_dai = 1;
  618. codec->private_data = cs4270;
  619. codec->control_data = i2c_client;
  620. codec->read = cs4270_read_reg_cache;
  621. codec->write = cs4270_i2c_write;
  622. codec->reg_cache = cs4270->reg_cache;
  623. codec->reg_cache_size = CS4270_NUMREGS;
  624. /* The I2C interface is set up, so pre-fill our register cache */
  625. ret = cs4270_fill_cache(codec);
  626. if (ret < 0) {
  627. dev_err(&i2c_client->dev, "failed to fill register cache\n");
  628. goto error_free_codec;
  629. }
  630. /* Disable auto-mute. This feature appears to be buggy. In some
  631. * situations, auto-mute will not deactivate when it should, so we want
  632. * this feature disabled by default. An application (e.g. alsactl) can
  633. * re-enabled it by using the controls.
  634. */
  635. reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
  636. reg &= ~CS4270_MUTE_AUTO;
  637. ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
  638. if (ret < 0) {
  639. dev_err(&i2c_client->dev, "i2c write failed\n");
  640. return ret;
  641. }
  642. /* Disable automatic volume control. The hardware enables, and it
  643. * causes volume change commands to be delayed, sometimes until after
  644. * playback has started. An application (e.g. alsactl) can
  645. * re-enabled it by using the controls.
  646. */
  647. reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
  648. reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
  649. ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
  650. if (ret < 0) {
  651. dev_err(&i2c_client->dev, "i2c write failed\n");
  652. return ret;
  653. }
  654. /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
  655. * structure for each CS4270 device, but the machine driver needs to
  656. * have a pointer to the DAI structure, so for now it must be a global
  657. * variable.
  658. */
  659. cs4270_dai.dev = &i2c_client->dev;
  660. /* Register the DAI. If all the other ASoC driver have already
  661. * registered, then this will call our probe function, so
  662. * cs4270_codec needs to be ready.
  663. */
  664. cs4270_codec = codec;
  665. ret = snd_soc_register_dai(&cs4270_dai);
  666. if (ret < 0) {
  667. dev_err(&i2c_client->dev, "failed to register DAIe\n");
  668. goto error_free_codec;
  669. }
  670. i2c_set_clientdata(i2c_client, cs4270);
  671. return 0;
  672. error_free_codec:
  673. kfree(cs4270);
  674. cs4270_codec = NULL;
  675. cs4270_dai.dev = NULL;
  676. return ret;
  677. }
  678. /**
  679. * cs4270_i2c_remove - remove an I2C device
  680. * @i2c_client: the I2C client object
  681. *
  682. * This function is the counterpart to cs4270_i2c_probe().
  683. */
  684. static int cs4270_i2c_remove(struct i2c_client *i2c_client)
  685. {
  686. struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
  687. kfree(cs4270);
  688. cs4270_codec = NULL;
  689. cs4270_dai.dev = NULL;
  690. return 0;
  691. }
  692. /*
  693. * cs4270_id - I2C device IDs supported by this driver
  694. */
  695. static struct i2c_device_id cs4270_id[] = {
  696. {"cs4270", 0},
  697. {}
  698. };
  699. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  700. #ifdef CONFIG_PM
  701. /* This suspend/resume implementation can handle both - a simple standby
  702. * where the codec remains powered, and a full suspend, where the voltage
  703. * domain the codec is connected to is teared down and/or any other hardware
  704. * reset condition is asserted.
  705. *
  706. * The codec's own power saving features are enabled in the suspend callback,
  707. * and all registers are written back to the hardware when resuming.
  708. */
  709. static int cs4270_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
  710. {
  711. struct cs4270_private *cs4270 = i2c_get_clientdata(client);
  712. struct snd_soc_codec *codec = &cs4270->codec;
  713. return snd_soc_suspend_device(codec->dev);
  714. }
  715. static int cs4270_i2c_resume(struct i2c_client *client)
  716. {
  717. struct cs4270_private *cs4270 = i2c_get_clientdata(client);
  718. struct snd_soc_codec *codec = &cs4270->codec;
  719. return snd_soc_resume_device(codec->dev);
  720. }
  721. static int cs4270_soc_suspend(struct platform_device *pdev, pm_message_t mesg)
  722. {
  723. struct snd_soc_codec *codec = cs4270_codec;
  724. int reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
  725. return snd_soc_write(codec, CS4270_PWRCTL, reg);
  726. }
  727. static int cs4270_soc_resume(struct platform_device *pdev)
  728. {
  729. struct snd_soc_codec *codec = cs4270_codec;
  730. struct i2c_client *i2c_client = codec->control_data;
  731. int reg;
  732. /* In case the device was put to hard reset during sleep, we need to
  733. * wait 500ns here before any I2C communication. */
  734. ndelay(500);
  735. /* first restore the entire register cache ... */
  736. for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
  737. u8 val = snd_soc_read(codec, reg);
  738. if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
  739. dev_err(codec->dev, "i2c write failed\n");
  740. return -EIO;
  741. }
  742. }
  743. /* ... then disable the power-down bits */
  744. reg = snd_soc_read(codec, CS4270_PWRCTL);
  745. reg &= ~CS4270_PWRCTL_PDN_ALL;
  746. return snd_soc_write(codec, CS4270_PWRCTL, reg);
  747. }
  748. #else
  749. #define cs4270_i2c_suspend NULL
  750. #define cs4270_i2c_resume NULL
  751. #define cs4270_soc_suspend NULL
  752. #define cs4270_soc_resume NULL
  753. #endif /* CONFIG_PM */
  754. /*
  755. * cs4270_i2c_driver - I2C device identification
  756. *
  757. * This structure tells the I2C subsystem how to identify and support a
  758. * given I2C device type.
  759. */
  760. static struct i2c_driver cs4270_i2c_driver = {
  761. .driver = {
  762. .name = "cs4270",
  763. .owner = THIS_MODULE,
  764. },
  765. .id_table = cs4270_id,
  766. .probe = cs4270_i2c_probe,
  767. .remove = cs4270_i2c_remove,
  768. .suspend = cs4270_i2c_suspend,
  769. .resume = cs4270_i2c_resume,
  770. };
  771. /*
  772. * ASoC codec device structure
  773. *
  774. * Assign this variable to the codec_dev field of the machine driver's
  775. * snd_soc_device structure.
  776. */
  777. struct snd_soc_codec_device soc_codec_device_cs4270 = {
  778. .probe = cs4270_probe,
  779. .remove = cs4270_remove,
  780. .suspend = cs4270_soc_suspend,
  781. .resume = cs4270_soc_resume,
  782. };
  783. EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
  784. static int __init cs4270_init(void)
  785. {
  786. pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
  787. return i2c_add_driver(&cs4270_i2c_driver);
  788. }
  789. module_init(cs4270_init);
  790. static void __exit cs4270_exit(void)
  791. {
  792. i2c_del_driver(&cs4270_i2c_driver);
  793. }
  794. module_exit(cs4270_exit);
  795. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  796. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  797. MODULE_LICENSE("GPL");