cs4270.c 24 KB

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