cs4270.c 21 KB

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