cs4270.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
  469. SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
  470. SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
  471. SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
  472. SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
  473. snd_soc_get_volsw, cs4270_soc_put_mute),
  474. };
  475. /*
  476. * cs4270_codec - global variable to store codec for the ASoC probe function
  477. *
  478. * If struct i2c_driver had a private_data field, we wouldn't need to use
  479. * cs4270_codec. This is the only way to pass the codec structure from
  480. * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
  481. * way to synchronize these two functions. cs4270_i2c_probe() can be called
  482. * multiple times before cs4270_probe() is called even once. So for now, we
  483. * also only allow cs4270_i2c_probe() to be run once. That means that we do
  484. * not support more than one cs4270 device in the system, at least for now.
  485. */
  486. static struct snd_soc_codec *cs4270_codec;
  487. static struct snd_soc_dai_ops cs4270_dai_ops = {
  488. .hw_params = cs4270_hw_params,
  489. .set_sysclk = cs4270_set_dai_sysclk,
  490. .set_fmt = cs4270_set_dai_fmt,
  491. .digital_mute = cs4270_dai_mute,
  492. };
  493. struct snd_soc_dai cs4270_dai = {
  494. .name = "cs4270",
  495. .playback = {
  496. .stream_name = "Playback",
  497. .channels_min = 1,
  498. .channels_max = 2,
  499. .rates = 0,
  500. .formats = CS4270_FORMATS,
  501. },
  502. .capture = {
  503. .stream_name = "Capture",
  504. .channels_min = 1,
  505. .channels_max = 2,
  506. .rates = 0,
  507. .formats = CS4270_FORMATS,
  508. },
  509. .ops = &cs4270_dai_ops,
  510. };
  511. EXPORT_SYMBOL_GPL(cs4270_dai);
  512. /**
  513. * cs4270_probe - ASoC probe function
  514. * @pdev: platform device
  515. *
  516. * This function is called when ASoC has all the pieces it needs to
  517. * instantiate a sound driver.
  518. */
  519. static int cs4270_probe(struct platform_device *pdev)
  520. {
  521. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  522. struct snd_soc_codec *codec = cs4270_codec;
  523. int ret;
  524. /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
  525. socdev->card->codec = codec;
  526. /* Register PCMs */
  527. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  528. if (ret < 0) {
  529. dev_err(codec->dev, "failed to create pcms\n");
  530. return ret;
  531. }
  532. /* Add the non-DAPM controls */
  533. ret = snd_soc_add_controls(codec, cs4270_snd_controls,
  534. ARRAY_SIZE(cs4270_snd_controls));
  535. if (ret < 0) {
  536. dev_err(codec->dev, "failed to add controls\n");
  537. goto error_free_pcms;
  538. }
  539. return 0;
  540. error_free_pcms:
  541. snd_soc_free_pcms(socdev);
  542. return ret;
  543. }
  544. /**
  545. * cs4270_remove - ASoC remove function
  546. * @pdev: platform device
  547. *
  548. * This function is the counterpart to cs4270_probe().
  549. */
  550. static int cs4270_remove(struct platform_device *pdev)
  551. {
  552. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  553. snd_soc_free_pcms(socdev);
  554. return 0;
  555. };
  556. /**
  557. * cs4270_i2c_probe - initialize the I2C interface of the CS4270
  558. * @i2c_client: the I2C client object
  559. * @id: the I2C device ID (ignored)
  560. *
  561. * This function is called whenever the I2C subsystem finds a device that
  562. * matches the device ID given via a prior call to i2c_add_driver().
  563. */
  564. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  565. const struct i2c_device_id *id)
  566. {
  567. struct snd_soc_codec *codec;
  568. struct cs4270_private *cs4270;
  569. unsigned int reg;
  570. int ret;
  571. /* For now, we only support one cs4270 device in the system. See the
  572. * comment for cs4270_codec.
  573. */
  574. if (cs4270_codec) {
  575. dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
  576. i2c_client->addr);
  577. dev_err(&i2c_client->dev, "only one per board allowed\n");
  578. /* Should we return something other than ENODEV here? */
  579. return -ENODEV;
  580. }
  581. /* Verify that we have a CS4270 */
  582. ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
  583. if (ret < 0) {
  584. dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
  585. i2c_client->addr);
  586. return ret;
  587. }
  588. /* The top four bits of the chip ID should be 1100. */
  589. if ((ret & 0xF0) != 0xC0) {
  590. dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
  591. i2c_client->addr);
  592. return -ENODEV;
  593. }
  594. dev_info(&i2c_client->dev, "found device at i2c address %X\n",
  595. i2c_client->addr);
  596. dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
  597. /* Allocate enough space for the snd_soc_codec structure
  598. and our private data together. */
  599. cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
  600. if (!cs4270) {
  601. dev_err(&i2c_client->dev, "could not allocate codec\n");
  602. return -ENOMEM;
  603. }
  604. codec = &cs4270->codec;
  605. mutex_init(&codec->mutex);
  606. INIT_LIST_HEAD(&codec->dapm_widgets);
  607. INIT_LIST_HEAD(&codec->dapm_paths);
  608. codec->dev = &i2c_client->dev;
  609. codec->name = "CS4270";
  610. codec->owner = THIS_MODULE;
  611. codec->dai = &cs4270_dai;
  612. codec->num_dai = 1;
  613. codec->private_data = cs4270;
  614. codec->control_data = i2c_client;
  615. codec->read = cs4270_read_reg_cache;
  616. codec->write = cs4270_i2c_write;
  617. codec->reg_cache = cs4270->reg_cache;
  618. codec->reg_cache_size = CS4270_NUMREGS;
  619. /* The I2C interface is set up, so pre-fill our register cache */
  620. ret = cs4270_fill_cache(codec);
  621. if (ret < 0) {
  622. dev_err(&i2c_client->dev, "failed to fill register cache\n");
  623. goto error_free_codec;
  624. }
  625. /* Disable auto-mute. This feature appears to be buggy. In some
  626. * situations, auto-mute will not deactivate when it should, so we want
  627. * this feature disabled by default. An application (e.g. alsactl) can
  628. * re-enabled it by using the controls.
  629. */
  630. reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
  631. reg &= ~CS4270_MUTE_AUTO;
  632. ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
  633. if (ret < 0) {
  634. dev_err(&i2c_client->dev, "i2c write failed\n");
  635. return ret;
  636. }
  637. /* Disable automatic volume control. The hardware enables, and it
  638. * causes volume change commands to be delayed, sometimes until after
  639. * playback has started. An application (e.g. alsactl) can
  640. * re-enabled it by using the controls.
  641. */
  642. reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
  643. reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
  644. ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
  645. if (ret < 0) {
  646. dev_err(&i2c_client->dev, "i2c write failed\n");
  647. return ret;
  648. }
  649. /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
  650. * structure for each CS4270 device, but the machine driver needs to
  651. * have a pointer to the DAI structure, so for now it must be a global
  652. * variable.
  653. */
  654. cs4270_dai.dev = &i2c_client->dev;
  655. /* Register the DAI. If all the other ASoC driver have already
  656. * registered, then this will call our probe function, so
  657. * cs4270_codec needs to be ready.
  658. */
  659. cs4270_codec = codec;
  660. ret = snd_soc_register_dai(&cs4270_dai);
  661. if (ret < 0) {
  662. dev_err(&i2c_client->dev, "failed to register DAIe\n");
  663. goto error_free_codec;
  664. }
  665. i2c_set_clientdata(i2c_client, cs4270);
  666. return 0;
  667. error_free_codec:
  668. kfree(cs4270);
  669. cs4270_codec = NULL;
  670. cs4270_dai.dev = NULL;
  671. return ret;
  672. }
  673. /**
  674. * cs4270_i2c_remove - remove an I2C device
  675. * @i2c_client: the I2C client object
  676. *
  677. * This function is the counterpart to cs4270_i2c_probe().
  678. */
  679. static int cs4270_i2c_remove(struct i2c_client *i2c_client)
  680. {
  681. struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
  682. kfree(cs4270);
  683. cs4270_codec = NULL;
  684. cs4270_dai.dev = NULL;
  685. return 0;
  686. }
  687. /*
  688. * cs4270_id - I2C device IDs supported by this driver
  689. */
  690. static struct i2c_device_id cs4270_id[] = {
  691. {"cs4270", 0},
  692. {}
  693. };
  694. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  695. #ifdef CONFIG_PM
  696. /* This suspend/resume implementation can handle both - a simple standby
  697. * where the codec remains powered, and a full suspend, where the voltage
  698. * domain the codec is connected to is teared down and/or any other hardware
  699. * reset condition is asserted.
  700. *
  701. * The codec's own power saving features are enabled in the suspend callback,
  702. * and all registers are written back to the hardware when resuming.
  703. */
  704. static int cs4270_soc_suspend(struct platform_device *pdev, pm_message_t mesg)
  705. {
  706. struct snd_soc_codec *codec = cs4270_codec;
  707. int reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
  708. return snd_soc_write(codec, CS4270_PWRCTL, reg);
  709. }
  710. static int cs4270_soc_resume(struct platform_device *pdev)
  711. {
  712. struct snd_soc_codec *codec = cs4270_codec;
  713. struct i2c_client *i2c_client = codec->control_data;
  714. int reg;
  715. /* In case the device was put to hard reset during sleep, we need to
  716. * wait 500ns here before any I2C communication. */
  717. ndelay(500);
  718. /* first restore the entire register cache ... */
  719. for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
  720. u8 val = snd_soc_read(codec, reg);
  721. if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
  722. dev_err(codec->dev, "i2c write failed\n");
  723. return -EIO;
  724. }
  725. }
  726. /* ... then disable the power-down bits */
  727. reg = snd_soc_read(codec, CS4270_PWRCTL);
  728. reg &= ~CS4270_PWRCTL_PDN_ALL;
  729. return snd_soc_write(codec, CS4270_PWRCTL, reg);
  730. }
  731. #else
  732. #define cs4270_soc_suspend NULL
  733. #define cs4270_soc_resume NULL
  734. #endif /* CONFIG_PM */
  735. /*
  736. * cs4270_i2c_driver - I2C device identification
  737. *
  738. * This structure tells the I2C subsystem how to identify and support a
  739. * given I2C device type.
  740. */
  741. static struct i2c_driver cs4270_i2c_driver = {
  742. .driver = {
  743. .name = "cs4270",
  744. .owner = THIS_MODULE,
  745. },
  746. .id_table = cs4270_id,
  747. .probe = cs4270_i2c_probe,
  748. .remove = cs4270_i2c_remove,
  749. };
  750. /*
  751. * ASoC codec device structure
  752. *
  753. * Assign this variable to the codec_dev field of the machine driver's
  754. * snd_soc_device structure.
  755. */
  756. struct snd_soc_codec_device soc_codec_device_cs4270 = {
  757. .probe = cs4270_probe,
  758. .remove = cs4270_remove,
  759. .suspend = cs4270_soc_suspend,
  760. .resume = cs4270_soc_resume,
  761. };
  762. EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
  763. static int __init cs4270_init(void)
  764. {
  765. pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
  766. return i2c_add_driver(&cs4270_i2c_driver);
  767. }
  768. module_init(cs4270_init);
  769. static void __exit cs4270_exit(void)
  770. {
  771. i2c_del_driver(&cs4270_i2c_driver);
  772. }
  773. module_exit(cs4270_exit);
  774. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  775. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  776. MODULE_LICENSE("GPL");