cs4270.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 automatically
  16. * selected if I2C is disabled or if a CS4270 is not found on the I2C
  17. * bus. However, stand-alone mode is only partially implemented because
  18. * there is no mechanism yet for this driver and the machine driver to
  19. * communicate the values of the M0, M1, MCLK1, and MCLK2 pins.
  20. * 2) Only I2C is supported, not SPI
  21. * 3) Only Master mode is supported, not Slave.
  22. * 4) The machine driver's 'startup' function must call
  23. * cs4270_set_dai_sysclk() with the value of MCLK.
  24. * 5) Only I2S and left-justified modes are supported
  25. * 6) Power management is not supported
  26. * 7) The only supported control is volume and hardware mute (if enabled)
  27. */
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <sound/core.h>
  31. #include <sound/soc.h>
  32. #include <sound/initval.h>
  33. #include <linux/i2c.h>
  34. #include "cs4270.h"
  35. /* If I2C is defined, then we support software mode. However, if we're
  36. not compiled as module but I2C is, then we can't use I2C calls. */
  37. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  38. #define USE_I2C
  39. #endif
  40. /* Private data for the CS4270 */
  41. struct cs4270_private {
  42. unsigned int mclk; /* Input frequency of the MCLK pin */
  43. unsigned int mode; /* The mode (I2S or left-justified) */
  44. };
  45. /*
  46. * The codec isn't really big-endian or little-endian, since the I2S
  47. * interface requires data to be sent serially with the MSbit first.
  48. * However, to support BE and LE I2S devices, we specify both here. That
  49. * way, ALSA will always match the bit patterns.
  50. */
  51. #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  52. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  53. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  54. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  55. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  56. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  57. #ifdef USE_I2C
  58. /* CS4270 registers addresses */
  59. #define CS4270_CHIPID 0x01 /* Chip ID */
  60. #define CS4270_PWRCTL 0x02 /* Power Control */
  61. #define CS4270_MODE 0x03 /* Mode Control */
  62. #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
  63. #define CS4270_TRANS 0x05 /* Transition Control */
  64. #define CS4270_MUTE 0x06 /* Mute Control */
  65. #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
  66. #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
  67. #define CS4270_FIRSTREG 0x01
  68. #define CS4270_LASTREG 0x08
  69. #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
  70. /* Bit masks for the CS4270 registers */
  71. #define CS4270_CHIPID_ID 0xF0
  72. #define CS4270_CHIPID_REV 0x0F
  73. #define CS4270_PWRCTL_FREEZE 0x80
  74. #define CS4270_PWRCTL_PDN_ADC 0x20
  75. #define CS4270_PWRCTL_PDN_DAC 0x02
  76. #define CS4270_PWRCTL_PDN 0x01
  77. #define CS4270_MODE_SPEED_MASK 0x30
  78. #define CS4270_MODE_1X 0x00
  79. #define CS4270_MODE_2X 0x10
  80. #define CS4270_MODE_4X 0x20
  81. #define CS4270_MODE_SLAVE 0x30
  82. #define CS4270_MODE_DIV_MASK 0x0E
  83. #define CS4270_MODE_DIV1 0x00
  84. #define CS4270_MODE_DIV15 0x02
  85. #define CS4270_MODE_DIV2 0x04
  86. #define CS4270_MODE_DIV3 0x06
  87. #define CS4270_MODE_DIV4 0x08
  88. #define CS4270_MODE_POPGUARD 0x01
  89. #define CS4270_FORMAT_FREEZE_A 0x80
  90. #define CS4270_FORMAT_FREEZE_B 0x40
  91. #define CS4270_FORMAT_LOOPBACK 0x20
  92. #define CS4270_FORMAT_DAC_MASK 0x18
  93. #define CS4270_FORMAT_DAC_LJ 0x00
  94. #define CS4270_FORMAT_DAC_I2S 0x08
  95. #define CS4270_FORMAT_DAC_RJ16 0x18
  96. #define CS4270_FORMAT_DAC_RJ24 0x10
  97. #define CS4270_FORMAT_ADC_MASK 0x01
  98. #define CS4270_FORMAT_ADC_LJ 0x00
  99. #define CS4270_FORMAT_ADC_I2S 0x01
  100. #define CS4270_TRANS_ONE_VOL 0x80
  101. #define CS4270_TRANS_SOFT 0x40
  102. #define CS4270_TRANS_ZERO 0x20
  103. #define CS4270_TRANS_INV_ADC_A 0x08
  104. #define CS4270_TRANS_INV_ADC_B 0x10
  105. #define CS4270_TRANS_INV_DAC_A 0x02
  106. #define CS4270_TRANS_INV_DAC_B 0x04
  107. #define CS4270_TRANS_DEEMPH 0x01
  108. #define CS4270_MUTE_AUTO 0x20
  109. #define CS4270_MUTE_ADC_A 0x08
  110. #define CS4270_MUTE_ADC_B 0x10
  111. #define CS4270_MUTE_POLARITY 0x04
  112. #define CS4270_MUTE_DAC_A 0x01
  113. #define CS4270_MUTE_DAC_B 0x02
  114. /*
  115. * Clock Ratio Selection for Master Mode with I2C enabled
  116. *
  117. * The data for this chart is taken from Table 5 of the CS4270 reference
  118. * manual.
  119. *
  120. * This table is used to determine how to program the Mode Control register.
  121. * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
  122. * rates the CS4270 currently supports.
  123. *
  124. * Each element in this array corresponds to the ratios in mclk_ratios[].
  125. * These two arrays need to be in sync.
  126. *
  127. * 'speed_mode' is the corresponding bit pattern to be written to the
  128. * MODE bits of the Mode Control Register
  129. *
  130. * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
  131. * the Mode Control Register.
  132. *
  133. * In situations where a single ratio is represented by multiple speed
  134. * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
  135. * double-speed instead of quad-speed. However, the CS4270 errata states
  136. * that Divide-By-1.5 can cause failures, so we avoid that mode where
  137. * possible.
  138. *
  139. * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
  140. * work if VD = 3.3V. If this effects you, select the
  141. * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
  142. * never select any sample rates that require divide-by-1.5.
  143. */
  144. static struct {
  145. unsigned int ratio;
  146. u8 speed_mode;
  147. u8 mclk;
  148. } 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. * Determine the CS4270 samples rates.
  165. *
  166. * 'freq' is the input frequency to MCLK. The other parameters are ignored.
  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. * support 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 support 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. * Note that in stand-alone mode, the sample rate is determined by input
  181. * pins M0, M1, MDIV1, and MDIV2. Also in stand-alone mode, divide-by-3
  182. * is not a programmable option. However, divide-by-3 is not an available
  183. * option in stand-alone mode. This cases two problems: a ratio of 768 is
  184. * not available (it requires divide-by-3) and B) ratios 192 and 384 can
  185. * only be selected with divide-by-1.5, but there is an errate that make
  186. * this selection difficult.
  187. *
  188. * In addition, there is no mechanism for communicating with the machine
  189. * driver what the input settings can be. This would need to be implemented
  190. * for stand-alone mode to work.
  191. */
  192. static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  193. int clk_id, unsigned int freq, int dir)
  194. {
  195. struct snd_soc_codec *codec = codec_dai->codec;
  196. struct cs4270_private *cs4270 = codec->private_data;
  197. unsigned int rates = 0;
  198. unsigned int rate_min = -1;
  199. unsigned int rate_max = 0;
  200. unsigned int i;
  201. cs4270->mclk = freq;
  202. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  203. unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
  204. rates |= snd_pcm_rate_to_rate_bit(rate);
  205. if (rate < rate_min)
  206. rate_min = rate;
  207. if (rate > rate_max)
  208. rate_max = rate;
  209. }
  210. /* FIXME: soc should support a rate list */
  211. rates &= ~SNDRV_PCM_RATE_KNOT;
  212. if (!rates) {
  213. printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
  214. return -EINVAL;
  215. }
  216. codec_dai->playback.rates = rates;
  217. codec_dai->playback.rate_min = rate_min;
  218. codec_dai->playback.rate_max = rate_max;
  219. codec_dai->capture.rates = rates;
  220. codec_dai->capture.rate_min = rate_min;
  221. codec_dai->capture.rate_max = rate_max;
  222. return 0;
  223. }
  224. /*
  225. * Configure the codec for the selected audio format
  226. *
  227. * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
  228. * codec accordingly.
  229. *
  230. * Currently, this function only supports SND_SOC_DAIFMT_I2S and
  231. * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
  232. * data for playback only, but ASoC currently does not support different
  233. * formats for playback vs. record.
  234. */
  235. static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
  236. unsigned int format)
  237. {
  238. struct snd_soc_codec *codec = codec_dai->codec;
  239. struct cs4270_private *cs4270 = codec->private_data;
  240. int ret = 0;
  241. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  242. case SND_SOC_DAIFMT_I2S:
  243. case SND_SOC_DAIFMT_LEFT_J:
  244. cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  245. break;
  246. default:
  247. printk(KERN_ERR "cs4270: invalid DAI format\n");
  248. ret = -EINVAL;
  249. }
  250. return ret;
  251. }
  252. /*
  253. * A list of addresses on which this CS4270 could use. I2C addresses are
  254. * 7 bits. For the CS4270, the upper four bits are always 1001, and the
  255. * lower three bits are determined via the AD2, AD1, and AD0 pins
  256. * (respectively).
  257. */
  258. static const unsigned short normal_i2c[] = {
  259. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END
  260. };
  261. I2C_CLIENT_INSMOD;
  262. /*
  263. * Pre-fill the CS4270 register cache.
  264. *
  265. * We use the auto-increment feature of the CS4270 to read all registers in
  266. * one shot.
  267. */
  268. static int cs4270_fill_cache(struct snd_soc_codec *codec)
  269. {
  270. u8 *cache = codec->reg_cache;
  271. struct i2c_client *i2c_client = codec->control_data;
  272. s32 length;
  273. length = i2c_smbus_read_i2c_block_data(i2c_client,
  274. CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
  275. if (length != CS4270_NUMREGS) {
  276. printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
  277. i2c_client->addr);
  278. return -EIO;
  279. }
  280. return 0;
  281. }
  282. /*
  283. * Read from the CS4270 register cache.
  284. *
  285. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  286. * After the initial read to pre-fill the cache, the CS4270 never updates
  287. * the register values, so we won't have a cache coherncy problem.
  288. */
  289. static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
  290. unsigned int reg)
  291. {
  292. u8 *cache = codec->reg_cache;
  293. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  294. return -EIO;
  295. return cache[reg - CS4270_FIRSTREG];
  296. }
  297. /*
  298. * Write to a CS4270 register via the I2C bus.
  299. *
  300. * This function writes the given value to the given CS4270 register, and
  301. * also updates the register cache.
  302. *
  303. * Note that we don't use the hw_write function pointer of snd_soc_codec.
  304. * That's because it's too clunky: the hw_write_t prototype does not match
  305. * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
  306. */
  307. static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
  308. unsigned int value)
  309. {
  310. u8 *cache = codec->reg_cache;
  311. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  312. return -EIO;
  313. /* Only perform an I2C operation if the new value is different */
  314. if (cache[reg - CS4270_FIRSTREG] != value) {
  315. struct i2c_client *client = codec->control_data;
  316. if (i2c_smbus_write_byte_data(client, reg, value)) {
  317. printk(KERN_ERR "cs4270: I2C write failed\n");
  318. return -EIO;
  319. }
  320. /* We've written to the hardware, so update the cache */
  321. cache[reg - CS4270_FIRSTREG] = value;
  322. }
  323. return 0;
  324. }
  325. /*
  326. * Program the CS4270 with the given hardware parameters.
  327. *
  328. * The .ops functions are used to provide board-specific data, like
  329. * input frequencies, to this driver. This function takes that information,
  330. * combines it with the hardware parameters provided, and programs the
  331. * hardware accordingly.
  332. */
  333. static int cs4270_hw_params(struct snd_pcm_substream *substream,
  334. struct snd_pcm_hw_params *params,
  335. struct snd_soc_dai *dai)
  336. {
  337. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  338. struct snd_soc_device *socdev = rtd->socdev;
  339. struct snd_soc_codec *codec = socdev->codec;
  340. struct cs4270_private *cs4270 = codec->private_data;
  341. int ret;
  342. unsigned int i;
  343. unsigned int rate;
  344. unsigned int ratio;
  345. int reg;
  346. /* Figure out which MCLK/LRCK ratio to use */
  347. rate = params_rate(params); /* Sampling rate, in Hz */
  348. ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
  349. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  350. if (cs4270_mode_ratios[i].ratio == ratio)
  351. break;
  352. }
  353. if (i == NUM_MCLK_RATIOS) {
  354. /* We did not find a matching ratio */
  355. printk(KERN_ERR "cs4270: could not find matching ratio\n");
  356. return -EINVAL;
  357. }
  358. /* Freeze and power-down the codec */
  359. ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
  360. CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
  361. CS4270_PWRCTL_PDN);
  362. if (ret < 0) {
  363. printk(KERN_ERR "cs4270: I2C write failed\n");
  364. return ret;
  365. }
  366. /* Program the mode control register */
  367. reg = snd_soc_read(codec, CS4270_MODE);
  368. reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
  369. reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
  370. ret = snd_soc_write(codec, CS4270_MODE, reg);
  371. if (ret < 0) {
  372. printk(KERN_ERR "cs4270: I2C write failed\n");
  373. return ret;
  374. }
  375. /* Program the format register */
  376. reg = snd_soc_read(codec, CS4270_FORMAT);
  377. reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
  378. switch (cs4270->mode) {
  379. case SND_SOC_DAIFMT_I2S:
  380. reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
  381. break;
  382. case SND_SOC_DAIFMT_LEFT_J:
  383. reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
  384. break;
  385. default:
  386. printk(KERN_ERR "cs4270: unknown format\n");
  387. return -EINVAL;
  388. }
  389. ret = snd_soc_write(codec, CS4270_FORMAT, reg);
  390. if (ret < 0) {
  391. printk(KERN_ERR "cs4270: I2C write failed\n");
  392. return ret;
  393. }
  394. /* Disable auto-mute. This feature appears to be buggy, because in
  395. some situations, auto-mute will not deactivate when it should. */
  396. reg = snd_soc_read(codec, CS4270_MUTE);
  397. reg &= ~CS4270_MUTE_AUTO;
  398. ret = snd_soc_write(codec, CS4270_MUTE, reg);
  399. if (ret < 0) {
  400. printk(KERN_ERR "cs4270: I2C write failed\n");
  401. return ret;
  402. }
  403. /* Disable automatic volume control. It's enabled by default, and
  404. * it causes volume change commands to be delayed, sometimes until
  405. * after playback has started.
  406. */
  407. reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
  408. reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
  409. ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
  410. if (ret < 0) {
  411. printk(KERN_ERR "I2C write failed\n");
  412. return ret;
  413. }
  414. /* Thaw and power-up the codec */
  415. ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
  416. if (ret < 0) {
  417. printk(KERN_ERR "cs4270: I2C write failed\n");
  418. return ret;
  419. }
  420. return ret;
  421. }
  422. #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
  423. /*
  424. * Set the CS4270 external mute
  425. *
  426. * This function toggles the mute bits in the MUTE register. The CS4270's
  427. * mute capability is intended for external muting circuitry, so if the
  428. * board does not have the MUTEA or MUTEB pins connected to such circuitry,
  429. * then this function will do nothing.
  430. */
  431. static int cs4270_mute(struct snd_soc_dai *dai, int mute)
  432. {
  433. struct snd_soc_codec *codec = dai->codec;
  434. int reg6;
  435. reg6 = snd_soc_read(codec, CS4270_MUTE);
  436. if (mute)
  437. reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
  438. CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
  439. else
  440. reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
  441. CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
  442. return snd_soc_write(codec, CS4270_MUTE, reg6);
  443. }
  444. #endif
  445. static int cs4270_i2c_probe(struct i2c_client *, const struct i2c_device_id *);
  446. /* A list of non-DAPM controls that the CS4270 supports */
  447. static const struct snd_kcontrol_new cs4270_snd_controls[] = {
  448. SOC_DOUBLE_R("Master Playback Volume",
  449. CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
  450. };
  451. static const struct i2c_device_id cs4270_id[] = {
  452. {"cs4270", 0},
  453. {}
  454. };
  455. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  456. static struct i2c_driver cs4270_i2c_driver = {
  457. .driver = {
  458. .name = "CS4270 I2C",
  459. .owner = THIS_MODULE,
  460. },
  461. .id_table = cs4270_id,
  462. .probe = cs4270_i2c_probe,
  463. };
  464. /*
  465. * Global variable to store socdev for i2c probe function.
  466. *
  467. * If struct i2c_driver had a private_data field, we wouldn't need to use
  468. * cs4270_socdec. This is the only way to pass the socdev structure to
  469. * cs4270_i2c_probe().
  470. *
  471. * The real solution to cs4270_socdev is to create a mechanism
  472. * that maps I2C addresses to snd_soc_device structures. Perhaps the
  473. * creation of the snd_soc_device object should be moved out of
  474. * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
  475. * driver dependent on I2C. The CS4270 supports "stand-alone" mode, whereby
  476. * the chip is *not* connected to the I2C bus, but is instead configured via
  477. * input pins.
  478. */
  479. static struct snd_soc_device *cs4270_socdev;
  480. /*
  481. * Initialize the I2C interface of the CS4270
  482. *
  483. * This function is called for whenever the I2C subsystem finds a device
  484. * at a particular address.
  485. *
  486. * Note: snd_soc_new_pcms() must be called before this function can be called,
  487. * because of snd_ctl_add().
  488. */
  489. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  490. const struct i2c_device_id *id)
  491. {
  492. struct snd_soc_device *socdev = cs4270_socdev;
  493. struct snd_soc_codec *codec = socdev->codec;
  494. int i;
  495. int ret = 0;
  496. /* Probing all possible addresses has one drawback: if there are
  497. multiple CS4270s on the bus, then you cannot specify which
  498. socdev is matched with which CS4270. For now, we just reject
  499. this I2C device if the socdev already has one attached. */
  500. if (codec->control_data)
  501. return -ENODEV;
  502. /* Note: codec_dai->codec is NULL here */
  503. codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
  504. if (!codec->reg_cache) {
  505. printk(KERN_ERR "cs4270: could not allocate register cache\n");
  506. ret = -ENOMEM;
  507. goto error;
  508. }
  509. /* Verify that we have a CS4270 */
  510. ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
  511. if (ret < 0) {
  512. printk(KERN_ERR "cs4270: failed to read I2C\n");
  513. goto error;
  514. }
  515. /* The top four bits of the chip ID should be 1100. */
  516. if ((ret & 0xF0) != 0xC0) {
  517. /* The device at this address is not a CS4270 codec */
  518. ret = -ENODEV;
  519. goto error;
  520. }
  521. printk(KERN_INFO "cs4270: found device at I2C address %X\n",
  522. i2c_client->addr);
  523. printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
  524. codec->control_data = i2c_client;
  525. codec->read = cs4270_read_reg_cache;
  526. codec->write = cs4270_i2c_write;
  527. codec->reg_cache_size = CS4270_NUMREGS;
  528. /* The I2C interface is set up, so pre-fill our register cache */
  529. ret = cs4270_fill_cache(codec);
  530. if (ret < 0) {
  531. printk(KERN_ERR "cs4270: failed to fill register cache\n");
  532. goto error;
  533. }
  534. /* Add the non-DAPM controls */
  535. for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
  536. struct snd_kcontrol *kctrl =
  537. snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
  538. ret = snd_ctl_add(codec->card, kctrl);
  539. if (ret < 0)
  540. goto error;
  541. }
  542. i2c_set_clientdata(i2c_client, codec);
  543. return 0;
  544. error:
  545. codec->control_data = NULL;
  546. kfree(codec->reg_cache);
  547. codec->reg_cache = NULL;
  548. codec->reg_cache_size = 0;
  549. return ret;
  550. }
  551. #endif /* USE_I2C*/
  552. struct snd_soc_dai cs4270_dai = {
  553. .name = "CS4270",
  554. .playback = {
  555. .stream_name = "Playback",
  556. .channels_min = 1,
  557. .channels_max = 2,
  558. .rates = 0,
  559. .formats = CS4270_FORMATS,
  560. },
  561. .capture = {
  562. .stream_name = "Capture",
  563. .channels_min = 1,
  564. .channels_max = 2,
  565. .rates = 0,
  566. .formats = CS4270_FORMATS,
  567. },
  568. };
  569. EXPORT_SYMBOL_GPL(cs4270_dai);
  570. /*
  571. * ASoC probe function
  572. *
  573. * This function is called when the machine driver calls
  574. * platform_device_add().
  575. */
  576. static int cs4270_probe(struct platform_device *pdev)
  577. {
  578. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  579. struct snd_soc_codec *codec;
  580. int ret = 0;
  581. printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
  582. /* Allocate enough space for the snd_soc_codec structure
  583. and our private data together. */
  584. codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
  585. sizeof(struct cs4270_private), GFP_KERNEL);
  586. if (!codec) {
  587. printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
  588. return -ENOMEM;
  589. }
  590. mutex_init(&codec->mutex);
  591. INIT_LIST_HEAD(&codec->dapm_widgets);
  592. INIT_LIST_HEAD(&codec->dapm_paths);
  593. codec->name = "CS4270";
  594. codec->owner = THIS_MODULE;
  595. codec->dai = &cs4270_dai;
  596. codec->num_dai = 1;
  597. codec->private_data = (void *) codec +
  598. ALIGN(sizeof(struct snd_soc_codec), 4);
  599. socdev->codec = codec;
  600. /* Register PCMs */
  601. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  602. if (ret < 0) {
  603. printk(KERN_ERR "cs4270: failed to create PCMs\n");
  604. goto error_free_codec;
  605. }
  606. #ifdef USE_I2C
  607. cs4270_socdev = socdev;
  608. ret = i2c_add_driver(&cs4270_i2c_driver);
  609. if (ret) {
  610. printk(KERN_ERR "cs4270: failed to attach driver");
  611. goto error_free_pcms;
  612. }
  613. /* Did we find a CS4270 on the I2C bus? */
  614. if (codec->control_data) {
  615. /* Initialize codec ops */
  616. cs4270_dai.ops.hw_params = cs4270_hw_params;
  617. cs4270_dai.ops.set_sysclk = cs4270_set_dai_sysclk;
  618. cs4270_dai.ops.set_fmt = cs4270_set_dai_fmt;
  619. #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
  620. cs4270_dai.ops.digital_mute = cs4270_mute;
  621. #endif
  622. } else
  623. printk(KERN_INFO "cs4270: no I2C device found, "
  624. "using stand-alone mode\n");
  625. #else
  626. printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
  627. #endif
  628. ret = snd_soc_init_card(socdev);
  629. if (ret < 0) {
  630. printk(KERN_ERR "cs4270: failed to register card\n");
  631. goto error_del_driver;
  632. }
  633. return 0;
  634. error_del_driver:
  635. #ifdef USE_I2C
  636. i2c_del_driver(&cs4270_i2c_driver);
  637. error_free_pcms:
  638. #endif
  639. snd_soc_free_pcms(socdev);
  640. error_free_codec:
  641. kfree(socdev->codec);
  642. socdev->codec = NULL;
  643. return ret;
  644. }
  645. static int cs4270_remove(struct platform_device *pdev)
  646. {
  647. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  648. snd_soc_free_pcms(socdev);
  649. #ifdef USE_I2C
  650. i2c_del_driver(&cs4270_i2c_driver);
  651. #endif
  652. kfree(socdev->codec);
  653. socdev->codec = NULL;
  654. return 0;
  655. }
  656. /*
  657. * ASoC codec device structure
  658. *
  659. * Assign this variable to the codec_dev field of the machine driver's
  660. * snd_soc_device structure.
  661. */
  662. struct snd_soc_codec_device soc_codec_device_cs4270 = {
  663. .probe = cs4270_probe,
  664. .remove = cs4270_remove
  665. };
  666. EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
  667. static int __init cs4270_init(void)
  668. {
  669. return snd_soc_register_dai(&cs4270_dai);
  670. }
  671. module_init(cs4270_init);
  672. static void __exit cs4270_exit(void)
  673. {
  674. snd_soc_unregister_dai(&cs4270_dai);
  675. }
  676. module_exit(cs4270_exit);
  677. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  678. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  679. MODULE_LICENSE("GPL");