cs4270.c 23 KB

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