cs4270.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. * 1) Software mode is supported. Stand-alone mode is not supported.
  16. * 2) Only I2C is supported, not SPI
  17. * 3) Only Master mode is supported, not Slave.
  18. * 4) The machine driver's 'startup' function must call
  19. * cs4270_set_dai_sysclk() with the value of MCLK.
  20. * 5) Only I2S and left-justified modes are supported
  21. * 6) Power management is not supported
  22. * 7) The only supported control is volume and hardware mute (if enabled)
  23. */
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <sound/core.h>
  27. #include <sound/soc.h>
  28. #include <sound/initval.h>
  29. #include <linux/i2c.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. /* 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. };
  106. /**
  107. * struct cs4270_mode_ratios - clock ratio tables
  108. * @ratio: the ratio of MCLK to the sample rate
  109. * @speed_mode: the Speed Mode bits to set in the Mode Control register for
  110. * this ratio
  111. * @mclk: the Ratio Select bits to set in the Mode Control register for this
  112. * ratio
  113. *
  114. * The data for this chart is taken from Table 5 of the CS4270 reference
  115. * manual.
  116. *
  117. * This table is used to determine how to program the Mode Control register.
  118. * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
  119. * rates the CS4270 currently supports.
  120. *
  121. * @speed_mode is the corresponding bit pattern to be written to the
  122. * MODE bits of the Mode Control Register
  123. *
  124. * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
  125. * the Mode Control Register.
  126. *
  127. * In situations where a single ratio is represented by multiple speed
  128. * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
  129. * double-speed instead of quad-speed. However, the CS4270 errata states
  130. * that divide-By-1.5 can cause failures, so we avoid that mode where
  131. * possible.
  132. *
  133. * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
  134. * work if Vd is 3.3V. If this effects you, select the
  135. * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
  136. * never select any sample rates that require divide-by-1.5.
  137. */
  138. struct cs4270_mode_ratios {
  139. unsigned int ratio;
  140. u8 speed_mode;
  141. u8 mclk;
  142. };
  143. static struct cs4270_mode_ratios[] = {
  144. {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
  145. #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
  146. {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
  147. #endif
  148. {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
  149. {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
  150. {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
  151. {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
  152. {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
  153. {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
  154. {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
  155. };
  156. /* The number of MCLK/LRCK ratios supported by the CS4270 */
  157. #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
  158. /**
  159. * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
  160. * @codec_dai: the codec DAI
  161. * @clk_id: the clock ID (ignored)
  162. * @freq: the MCLK input frequency
  163. * @dir: the clock direction (ignored)
  164. *
  165. * This function is used to tell the codec driver what the input MCLK
  166. * frequency is.
  167. *
  168. * The value of MCLK is used to determine which sample rates are supported
  169. * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
  170. * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
  171. *
  172. * This function calculates the nine ratios and determines which ones match
  173. * a standard sample rate. If there's a match, then it is added to the list
  174. * of supported sample rates.
  175. *
  176. * This function must be called by the machine driver's 'startup' function,
  177. * otherwise the list of supported sample rates will not be available in
  178. * time for ALSA.
  179. */
  180. static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  181. int clk_id, unsigned int freq, int dir)
  182. {
  183. struct snd_soc_codec *codec = codec_dai->codec;
  184. struct cs4270_private *cs4270 = codec->private_data;
  185. unsigned int rates = 0;
  186. unsigned int rate_min = -1;
  187. unsigned int rate_max = 0;
  188. unsigned int i;
  189. cs4270->mclk = freq;
  190. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  191. unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
  192. rates |= snd_pcm_rate_to_rate_bit(rate);
  193. if (rate < rate_min)
  194. rate_min = rate;
  195. if (rate > rate_max)
  196. rate_max = rate;
  197. }
  198. /* FIXME: soc should support a rate list */
  199. rates &= ~SNDRV_PCM_RATE_KNOT;
  200. if (!rates) {
  201. printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
  202. return -EINVAL;
  203. }
  204. codec_dai->playback.rates = rates;
  205. codec_dai->playback.rate_min = rate_min;
  206. codec_dai->playback.rate_max = rate_max;
  207. codec_dai->capture.rates = rates;
  208. codec_dai->capture.rate_min = rate_min;
  209. codec_dai->capture.rate_max = rate_max;
  210. return 0;
  211. }
  212. /**
  213. * cs4270_set_dai_fmt - configure the codec for the selected audio format
  214. * @codec_dai: the codec DAI
  215. * @format: a SND_SOC_DAIFMT_x value indicating the data format
  216. *
  217. * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
  218. * codec accordingly.
  219. *
  220. * Currently, this function only supports SND_SOC_DAIFMT_I2S and
  221. * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
  222. * data for playback only, but ASoC currently does not support different
  223. * formats for playback vs. record.
  224. */
  225. static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
  226. unsigned int format)
  227. {
  228. struct snd_soc_codec *codec = codec_dai->codec;
  229. struct cs4270_private *cs4270 = codec->private_data;
  230. int ret = 0;
  231. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  232. case SND_SOC_DAIFMT_I2S:
  233. case SND_SOC_DAIFMT_LEFT_J:
  234. cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  235. break;
  236. default:
  237. printk(KERN_ERR "cs4270: invalid DAI format\n");
  238. ret = -EINVAL;
  239. }
  240. return ret;
  241. }
  242. /**
  243. * cs4270_fill_cache - pre-fill the CS4270 register cache.
  244. * @codec: the codec for this CS4270
  245. *
  246. * This function fills in the CS4270 register cache by reading the register
  247. * values from the hardware.
  248. *
  249. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  250. * After the initial read to pre-fill the cache, the CS4270 never updates
  251. * the register values, so we won't have a cache coherency problem.
  252. *
  253. * We use the auto-increment feature of the CS4270 to read all registers in
  254. * one shot.
  255. */
  256. static int cs4270_fill_cache(struct snd_soc_codec *codec)
  257. {
  258. u8 *cache = codec->reg_cache;
  259. struct i2c_client *i2c_client = codec->control_data;
  260. s32 length;
  261. length = i2c_smbus_read_i2c_block_data(i2c_client,
  262. CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
  263. if (length != CS4270_NUMREGS) {
  264. printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
  265. i2c_client->addr);
  266. return -EIO;
  267. }
  268. return 0;
  269. }
  270. /**
  271. * cs4270_read_reg_cache - read from the CS4270 register cache.
  272. * @codec: the codec for this CS4270
  273. * @reg: the register to read
  274. *
  275. * This function returns the value for a given register. It reads only from
  276. * the register cache, not the hardware itself.
  277. *
  278. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  279. * After the initial read to pre-fill the cache, the CS4270 never updates
  280. * the register values, so we won't have a cache coherency problem.
  281. */
  282. static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
  283. unsigned int reg)
  284. {
  285. u8 *cache = codec->reg_cache;
  286. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  287. return -EIO;
  288. return cache[reg - CS4270_FIRSTREG];
  289. }
  290. /**
  291. * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
  292. * @codec: the codec for this CS4270
  293. * @reg: the register to write
  294. * @value: the value to write to the register
  295. *
  296. * This function writes the given value to the given CS4270 register, and
  297. * also updates the register cache.
  298. *
  299. * Note that we don't use the hw_write function pointer of snd_soc_codec.
  300. * That's because it's too clunky: the hw_write_t prototype does not match
  301. * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
  302. */
  303. static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
  304. unsigned int value)
  305. {
  306. u8 *cache = codec->reg_cache;
  307. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  308. return -EIO;
  309. /* Only perform an I2C operation if the new value is different */
  310. if (cache[reg - CS4270_FIRSTREG] != value) {
  311. struct i2c_client *client = codec->control_data;
  312. if (i2c_smbus_write_byte_data(client, reg, value)) {
  313. printk(KERN_ERR "cs4270: I2C write failed\n");
  314. return -EIO;
  315. }
  316. /* We've written to the hardware, so update the cache */
  317. cache[reg - CS4270_FIRSTREG] = value;
  318. }
  319. return 0;
  320. }
  321. /**
  322. * cs4270_hw_params - program the CS4270 with the given hardware parameters.
  323. * @substream: the audio stream
  324. * @params: the hardware parameters to set
  325. * @dai: the SOC DAI (ignored)
  326. *
  327. * This function programs the hardware with the values provided.
  328. * Specifically, the sample rate and the data format.
  329. *
  330. * The .ops functions are used to provide board-specific data, like input
  331. * frequencies, to this driver. This function takes that information,
  332. * combines it with the hardware parameters provided, and programs the
  333. * hardware accordingly.
  334. */
  335. static int cs4270_hw_params(struct snd_pcm_substream *substream,
  336. struct snd_pcm_hw_params *params,
  337. struct snd_soc_dai *dai)
  338. {
  339. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  340. struct snd_soc_device *socdev = rtd->socdev;
  341. struct snd_soc_codec *codec = socdev->card->codec;
  342. struct cs4270_private *cs4270 = codec->private_data;
  343. int ret;
  344. unsigned int i;
  345. unsigned int rate;
  346. unsigned int ratio;
  347. int reg;
  348. /* Figure out which MCLK/LRCK ratio to use */
  349. rate = params_rate(params); /* Sampling rate, in Hz */
  350. ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
  351. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  352. if (cs4270_mode_ratios[i].ratio == ratio)
  353. break;
  354. }
  355. if (i == NUM_MCLK_RATIOS) {
  356. /* We did not find a matching ratio */
  357. printk(KERN_ERR "cs4270: could not find matching ratio\n");
  358. return -EINVAL;
  359. }
  360. /* Freeze and power-down the codec */
  361. ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
  362. CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
  363. CS4270_PWRCTL_PDN);
  364. if (ret < 0) {
  365. printk(KERN_ERR "cs4270: I2C write failed\n");
  366. return ret;
  367. }
  368. /* Program the mode control register */
  369. reg = snd_soc_read(codec, CS4270_MODE);
  370. reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
  371. reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
  372. ret = snd_soc_write(codec, CS4270_MODE, reg);
  373. if (ret < 0) {
  374. printk(KERN_ERR "cs4270: I2C write failed\n");
  375. return ret;
  376. }
  377. /* Program the format register */
  378. reg = snd_soc_read(codec, CS4270_FORMAT);
  379. reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
  380. switch (cs4270->mode) {
  381. case SND_SOC_DAIFMT_I2S:
  382. reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
  383. break;
  384. case SND_SOC_DAIFMT_LEFT_J:
  385. reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
  386. break;
  387. default:
  388. printk(KERN_ERR "cs4270: unknown format\n");
  389. return -EINVAL;
  390. }
  391. ret = snd_soc_write(codec, CS4270_FORMAT, reg);
  392. if (ret < 0) {
  393. printk(KERN_ERR "cs4270: I2C write failed\n");
  394. return ret;
  395. }
  396. /* Disable auto-mute. This feature appears to be buggy, because in
  397. some situations, auto-mute will not deactivate when it should. */
  398. reg = snd_soc_read(codec, CS4270_MUTE);
  399. reg &= ~CS4270_MUTE_AUTO;
  400. ret = snd_soc_write(codec, CS4270_MUTE, reg);
  401. if (ret < 0) {
  402. printk(KERN_ERR "cs4270: I2C write failed\n");
  403. return ret;
  404. }
  405. /* Disable automatic volume control. It's enabled by default, and
  406. * it causes volume change commands to be delayed, sometimes until
  407. * after playback has started.
  408. */
  409. reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
  410. reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
  411. ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
  412. if (ret < 0) {
  413. printk(KERN_ERR "I2C write failed\n");
  414. return ret;
  415. }
  416. /* Thaw and power-up the codec */
  417. ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
  418. if (ret < 0) {
  419. printk(KERN_ERR "cs4270: I2C write failed\n");
  420. return ret;
  421. }
  422. return ret;
  423. }
  424. #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
  425. /**
  426. * cs4270_mute - enable/disable the CS4270 external mute
  427. * @dai: the SOC DAI
  428. * @mute: 0 = disable mute, 1 = enable mute
  429. *
  430. * This function toggles the mute bits in the MUTE register. The CS4270's
  431. * mute capability is intended for external muting circuitry, so if the
  432. * board does not have the MUTEA or MUTEB pins connected to such circuitry,
  433. * then this function will do nothing.
  434. */
  435. static int cs4270_mute(struct snd_soc_dai *dai, int mute)
  436. {
  437. struct snd_soc_codec *codec = dai->codec;
  438. int reg6;
  439. reg6 = snd_soc_read(codec, CS4270_MUTE);
  440. if (mute)
  441. reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
  442. CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
  443. else
  444. reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
  445. CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
  446. return snd_soc_write(codec, CS4270_MUTE, reg6);
  447. }
  448. #else
  449. #define cs4270_mute NULL
  450. #endif
  451. /* A list of non-DAPM controls that the CS4270 supports */
  452. static const struct snd_kcontrol_new cs4270_snd_controls[] = {
  453. SOC_DOUBLE_R("Master Playback Volume",
  454. CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
  455. };
  456. /*
  457. * cs4270_codec - global variable to store codec for the ASoC probe function
  458. *
  459. * If struct i2c_driver had a private_data field, we wouldn't need to use
  460. * cs4270_codec. This is the only way to pass the codec structure from
  461. * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
  462. * way to synchronize these two functions. cs4270_i2c_probe() can be called
  463. * multiple times before cs4270_probe() is called even once. So for now, we
  464. * also only allow cs4270_i2c_probe() to be run once. That means that we do
  465. * not support more than one cs4270 device in the system, at least for now.
  466. */
  467. static struct snd_soc_codec *cs4270_codec;
  468. struct snd_soc_dai cs4270_dai = {
  469. .name = "cs4270",
  470. .playback = {
  471. .stream_name = "Playback",
  472. .channels_min = 1,
  473. .channels_max = 2,
  474. .rates = 0,
  475. .formats = CS4270_FORMATS,
  476. },
  477. .capture = {
  478. .stream_name = "Capture",
  479. .channels_min = 1,
  480. .channels_max = 2,
  481. .rates = 0,
  482. .formats = CS4270_FORMATS,
  483. },
  484. .ops = {
  485. .hw_params = cs4270_hw_params,
  486. .set_sysclk = cs4270_set_dai_sysclk,
  487. .set_fmt = cs4270_set_dai_fmt,
  488. .digital_mute = cs4270_mute,
  489. },
  490. };
  491. EXPORT_SYMBOL_GPL(cs4270_dai);
  492. /**
  493. * cs4270_probe - ASoC probe function
  494. * @pdev: platform device
  495. *
  496. * This function is called when ASoC has all the pieces it needs to
  497. * instantiate a sound driver.
  498. */
  499. static int cs4270_probe(struct platform_device *pdev)
  500. {
  501. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  502. struct snd_soc_codec *codec = cs4270_codec;
  503. unsigned int i;
  504. int ret;
  505. /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
  506. socdev->card->codec = codec;
  507. /* Register PCMs */
  508. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  509. if (ret < 0) {
  510. printk(KERN_ERR "cs4270: failed to create PCMs\n");
  511. return ret;
  512. }
  513. /* Add the non-DAPM controls */
  514. for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
  515. struct snd_kcontrol *kctrl;
  516. kctrl = snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
  517. if (!kctrl) {
  518. printk(KERN_ERR "cs4270: error creating control '%s'\n",
  519. cs4270_snd_controls[i].name);
  520. ret = -ENOMEM;
  521. goto error_free_pcms;
  522. }
  523. ret = snd_ctl_add(codec->card, kctrl);
  524. if (ret < 0) {
  525. printk(KERN_ERR "cs4270: error adding control '%s'\n",
  526. cs4270_snd_controls[i].name);
  527. goto error_free_pcms;
  528. }
  529. }
  530. /* And finally, register the socdev */
  531. ret = snd_soc_init_card(socdev);
  532. if (ret < 0) {
  533. printk(KERN_ERR "cs4270: failed to register card\n");
  534. goto error_free_pcms;
  535. }
  536. return 0;
  537. error_free_pcms:
  538. snd_soc_free_pcms(socdev);
  539. return ret;
  540. }
  541. /**
  542. * cs4270_remove - ASoC remove function
  543. * @pdev: platform device
  544. *
  545. * This function is the counterpart to cs4270_probe().
  546. */
  547. static int cs4270_remove(struct platform_device *pdev)
  548. {
  549. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  550. snd_soc_free_pcms(socdev);
  551. return 0;
  552. };
  553. /**
  554. * cs4270_i2c_probe - initialize the I2C interface of the CS4270
  555. * @i2c_client: the I2C client object
  556. * @id: the I2C device ID (ignored)
  557. *
  558. * This function is called whenever the I2C subsystem finds a device that
  559. * matches the device ID given via a prior call to i2c_add_driver().
  560. */
  561. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  562. const struct i2c_device_id *id)
  563. {
  564. struct snd_soc_codec *codec;
  565. struct cs4270_private *cs4270;
  566. int ret;
  567. /* For now, we only support one cs4270 device in the system. See the
  568. * comment for cs4270_codec.
  569. */
  570. if (cs4270_codec) {
  571. printk(KERN_ERR "cs4270: ignoring CS4270 at addr %X\n",
  572. i2c_client->addr);
  573. printk(KERN_ERR "cs4270: only one CS4270 per board allowed\n");
  574. /* Should we return something other than ENODEV here? */
  575. return -ENODEV;
  576. }
  577. /* Verify that we have a CS4270 */
  578. ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
  579. if (ret < 0) {
  580. printk(KERN_ERR "cs4270: failed to read I2C at addr %X\n",
  581. i2c_client->addr);
  582. return ret;
  583. }
  584. /* The top four bits of the chip ID should be 1100. */
  585. if ((ret & 0xF0) != 0xC0) {
  586. printk(KERN_ERR "cs4270: device at addr %X is not a CS4270\n",
  587. i2c_client->addr);
  588. return -ENODEV;
  589. }
  590. printk(KERN_INFO "cs4270: found device at I2C address %X\n",
  591. i2c_client->addr);
  592. printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
  593. /* Allocate enough space for the snd_soc_codec structure
  594. and our private data together. */
  595. cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
  596. if (!cs4270) {
  597. printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
  598. return -ENOMEM;
  599. }
  600. codec = &cs4270->codec;
  601. cs4270_codec = codec;
  602. mutex_init(&codec->mutex);
  603. INIT_LIST_HEAD(&codec->dapm_widgets);
  604. INIT_LIST_HEAD(&codec->dapm_paths);
  605. codec->name = "CS4270";
  606. codec->owner = THIS_MODULE;
  607. codec->dai = &cs4270_dai;
  608. codec->num_dai = 1;
  609. codec->private_data = cs4270;
  610. codec->control_data = i2c_client;
  611. codec->read = cs4270_read_reg_cache;
  612. codec->write = cs4270_i2c_write;
  613. codec->reg_cache = cs4270->reg_cache;
  614. codec->reg_cache_size = CS4270_NUMREGS;
  615. /* The I2C interface is set up, so pre-fill our register cache */
  616. ret = cs4270_fill_cache(codec);
  617. if (ret < 0) {
  618. printk(KERN_ERR "cs4270: failed to fill register cache\n");
  619. goto error_free_codec;
  620. }
  621. /* Register the DAI. If all the other ASoC driver have already
  622. * registered, then this will call our probe function, so
  623. * cs4270_codec needs to be ready.
  624. */
  625. ret = snd_soc_register_dai(&cs4270_dai);
  626. if (ret < 0) {
  627. printk(KERN_ERR "cs4270: failed to register DAIe\n");
  628. goto error_free_codec;
  629. }
  630. i2c_set_clientdata(i2c_client, cs4270);
  631. return 0;
  632. error_free_codec:
  633. kfree(cs4270);
  634. return ret;
  635. }
  636. /**
  637. * cs4270_i2c_remove - remove an I2C device
  638. * @i2c_client: the I2C client object
  639. *
  640. * This function is the counterpart to cs4270_i2c_probe().
  641. */
  642. static int cs4270_i2c_remove(struct i2c_client *i2c_client)
  643. {
  644. struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
  645. kfree(cs4270);
  646. return 0;
  647. }
  648. /*
  649. * cs4270_id - I2C device IDs supported by this driver
  650. */
  651. static struct i2c_device_id cs4270_id[] = {
  652. {"cs4270", 0},
  653. {}
  654. };
  655. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  656. /*
  657. * cs4270_i2c_driver - I2C device identification
  658. *
  659. * This structure tells the I2C subsystem how to identify and support a
  660. * given I2C device type.
  661. */
  662. static struct i2c_driver cs4270_i2c_driver = {
  663. .driver = {
  664. .name = "cs4270",
  665. .owner = THIS_MODULE,
  666. },
  667. .id_table = cs4270_id,
  668. .probe = cs4270_i2c_probe,
  669. .remove = cs4270_i2c_remove,
  670. };
  671. /*
  672. * ASoC codec device structure
  673. *
  674. * Assign this variable to the codec_dev field of the machine driver's
  675. * snd_soc_device structure.
  676. */
  677. struct snd_soc_codec_device soc_codec_device_cs4270 = {
  678. .probe = cs4270_probe,
  679. .remove = cs4270_remove
  680. };
  681. EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
  682. static int __init cs4270_init(void)
  683. {
  684. printk(KERN_INFO "Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
  685. return i2c_add_driver(&cs4270_i2c_driver);
  686. }
  687. module_init(cs4270_init);
  688. static void __exit cs4270_exit(void)
  689. {
  690. i2c_del_driver(&cs4270_i2c_driver);
  691. }
  692. module_exit(cs4270_exit);
  693. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  694. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  695. MODULE_LICENSE("GPL");