cs4270.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * CS4270 ALSA SoC (ASoC) codec driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed
  7. * under the terms of the GNU General Public License version 2. This
  8. * program is licensed "as is" without any warranty of any kind, whether
  9. * express or implied.
  10. *
  11. * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
  12. *
  13. * Current features/limitations:
  14. *
  15. * - Software mode is supported. Stand-alone mode is not supported.
  16. * - Only I2C is supported, not SPI
  17. * - Support for master and slave mode
  18. * - The machine driver's 'startup' function must call
  19. * cs4270_set_dai_sysclk() with the value of MCLK.
  20. * - Only I2S and left-justified modes are supported
  21. * - Power management is not supported
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <sound/core.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include <linux/i2c.h>
  29. #include "cs4270.h"
  30. /*
  31. * The codec isn't really big-endian or little-endian, since the I2S
  32. * interface requires data to be sent serially with the MSbit first.
  33. * However, to support BE and LE I2S devices, we specify both here. That
  34. * way, ALSA will always match the bit patterns.
  35. */
  36. #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  37. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  38. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  39. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  40. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  41. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  42. /* CS4270 registers addresses */
  43. #define CS4270_CHIPID 0x01 /* Chip ID */
  44. #define CS4270_PWRCTL 0x02 /* Power Control */
  45. #define CS4270_MODE 0x03 /* Mode Control */
  46. #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
  47. #define CS4270_TRANS 0x05 /* Transition Control */
  48. #define CS4270_MUTE 0x06 /* Mute Control */
  49. #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
  50. #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
  51. #define CS4270_FIRSTREG 0x01
  52. #define CS4270_LASTREG 0x08
  53. #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
  54. /* Bit masks for the CS4270 registers */
  55. #define CS4270_CHIPID_ID 0xF0
  56. #define CS4270_CHIPID_REV 0x0F
  57. #define CS4270_PWRCTL_FREEZE 0x80
  58. #define CS4270_PWRCTL_PDN_ADC 0x20
  59. #define CS4270_PWRCTL_PDN_DAC 0x02
  60. #define CS4270_PWRCTL_PDN 0x01
  61. #define CS4270_MODE_SPEED_MASK 0x30
  62. #define CS4270_MODE_1X 0x00
  63. #define CS4270_MODE_2X 0x10
  64. #define CS4270_MODE_4X 0x20
  65. #define CS4270_MODE_SLAVE 0x30
  66. #define CS4270_MODE_DIV_MASK 0x0E
  67. #define CS4270_MODE_DIV1 0x00
  68. #define CS4270_MODE_DIV15 0x02
  69. #define CS4270_MODE_DIV2 0x04
  70. #define CS4270_MODE_DIV3 0x06
  71. #define CS4270_MODE_DIV4 0x08
  72. #define CS4270_MODE_POPGUARD 0x01
  73. #define CS4270_FORMAT_FREEZE_A 0x80
  74. #define CS4270_FORMAT_FREEZE_B 0x40
  75. #define CS4270_FORMAT_LOOPBACK 0x20
  76. #define CS4270_FORMAT_DAC_MASK 0x18
  77. #define CS4270_FORMAT_DAC_LJ 0x00
  78. #define CS4270_FORMAT_DAC_I2S 0x08
  79. #define CS4270_FORMAT_DAC_RJ16 0x18
  80. #define CS4270_FORMAT_DAC_RJ24 0x10
  81. #define CS4270_FORMAT_ADC_MASK 0x01
  82. #define CS4270_FORMAT_ADC_LJ 0x00
  83. #define CS4270_FORMAT_ADC_I2S 0x01
  84. #define CS4270_TRANS_ONE_VOL 0x80
  85. #define CS4270_TRANS_SOFT 0x40
  86. #define CS4270_TRANS_ZERO 0x20
  87. #define CS4270_TRANS_INV_ADC_A 0x08
  88. #define CS4270_TRANS_INV_ADC_B 0x10
  89. #define CS4270_TRANS_INV_DAC_A 0x02
  90. #define CS4270_TRANS_INV_DAC_B 0x04
  91. #define CS4270_TRANS_DEEMPH 0x01
  92. #define CS4270_MUTE_AUTO 0x20
  93. #define CS4270_MUTE_ADC_A 0x08
  94. #define CS4270_MUTE_ADC_B 0x10
  95. #define CS4270_MUTE_POLARITY 0x04
  96. #define CS4270_MUTE_DAC_A 0x01
  97. #define CS4270_MUTE_DAC_B 0x02
  98. /* Private data for the CS4270 */
  99. struct cs4270_private {
  100. struct snd_soc_codec codec;
  101. u8 reg_cache[CS4270_NUMREGS];
  102. unsigned int mclk; /* Input frequency of the MCLK pin */
  103. unsigned int mode; /* The mode (I2S or left-justified) */
  104. unsigned int slave_mode;
  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 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. dev_err(codec->dev, "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. /* set DAI format */
  232. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  233. case SND_SOC_DAIFMT_I2S:
  234. case SND_SOC_DAIFMT_LEFT_J:
  235. cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  236. break;
  237. default:
  238. dev_err(codec->dev, "invalid dai format\n");
  239. ret = -EINVAL;
  240. }
  241. /* set master/slave audio interface */
  242. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  243. case SND_SOC_DAIFMT_CBS_CFS:
  244. cs4270->slave_mode = 1;
  245. break;
  246. case SND_SOC_DAIFMT_CBM_CFM:
  247. cs4270->slave_mode = 0;
  248. break;
  249. default:
  250. /* all other modes are unsupported by the hardware */
  251. ret = -EINVAL;
  252. }
  253. return ret;
  254. }
  255. /**
  256. * cs4270_fill_cache - pre-fill the CS4270 register cache.
  257. * @codec: the codec for this CS4270
  258. *
  259. * This function fills in the CS4270 register cache by reading the register
  260. * values from the hardware.
  261. *
  262. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  263. * After the initial read to pre-fill the cache, the CS4270 never updates
  264. * the register values, so we won't have a cache coherency problem.
  265. *
  266. * We use the auto-increment feature of the CS4270 to read all registers in
  267. * one shot.
  268. */
  269. static int cs4270_fill_cache(struct snd_soc_codec *codec)
  270. {
  271. u8 *cache = codec->reg_cache;
  272. struct i2c_client *i2c_client = codec->control_data;
  273. s32 length;
  274. length = i2c_smbus_read_i2c_block_data(i2c_client,
  275. CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
  276. if (length != CS4270_NUMREGS) {
  277. dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
  278. i2c_client->addr);
  279. return -EIO;
  280. }
  281. return 0;
  282. }
  283. /**
  284. * cs4270_read_reg_cache - read from the CS4270 register cache.
  285. * @codec: the codec for this CS4270
  286. * @reg: the register to read
  287. *
  288. * This function returns the value for a given register. It reads only from
  289. * the register cache, not the hardware itself.
  290. *
  291. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  292. * After the initial read to pre-fill the cache, the CS4270 never updates
  293. * the register values, so we won't have a cache coherency problem.
  294. */
  295. static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
  296. unsigned int reg)
  297. {
  298. u8 *cache = codec->reg_cache;
  299. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  300. return -EIO;
  301. return cache[reg - CS4270_FIRSTREG];
  302. }
  303. /**
  304. * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
  305. * @codec: the codec for this CS4270
  306. * @reg: the register to write
  307. * @value: the value to write to the register
  308. *
  309. * This function writes the given value to the given CS4270 register, and
  310. * also updates the register cache.
  311. *
  312. * Note that we don't use the hw_write function pointer of snd_soc_codec.
  313. * That's because it's too clunky: the hw_write_t prototype does not match
  314. * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
  315. */
  316. static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
  317. unsigned int value)
  318. {
  319. u8 *cache = codec->reg_cache;
  320. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  321. return -EIO;
  322. /* Only perform an I2C operation if the new value is different */
  323. if (cache[reg - CS4270_FIRSTREG] != value) {
  324. struct i2c_client *client = codec->control_data;
  325. if (i2c_smbus_write_byte_data(client, reg, value)) {
  326. dev_err(codec->dev, "i2c write failed\n");
  327. return -EIO;
  328. }
  329. /* We've written to the hardware, so update the cache */
  330. cache[reg - CS4270_FIRSTREG] = value;
  331. }
  332. return 0;
  333. }
  334. /**
  335. * cs4270_hw_params - program the CS4270 with the given hardware parameters.
  336. * @substream: the audio stream
  337. * @params: the hardware parameters to set
  338. * @dai: the SOC DAI (ignored)
  339. *
  340. * This function programs the hardware with the values provided.
  341. * Specifically, the sample rate and the data format.
  342. *
  343. * The .ops functions are used to provide board-specific data, like input
  344. * frequencies, to this driver. This function takes that information,
  345. * combines it with the hardware parameters provided, and programs the
  346. * hardware accordingly.
  347. */
  348. static int cs4270_hw_params(struct snd_pcm_substream *substream,
  349. struct snd_pcm_hw_params *params,
  350. struct snd_soc_dai *dai)
  351. {
  352. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  353. struct snd_soc_device *socdev = rtd->socdev;
  354. struct snd_soc_codec *codec = socdev->card->codec;
  355. struct cs4270_private *cs4270 = codec->private_data;
  356. int ret;
  357. unsigned int i;
  358. unsigned int rate;
  359. unsigned int ratio;
  360. int reg;
  361. /* Figure out which MCLK/LRCK ratio to use */
  362. rate = params_rate(params); /* Sampling rate, in Hz */
  363. ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
  364. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  365. if (cs4270_mode_ratios[i].ratio == ratio)
  366. break;
  367. }
  368. if (i == NUM_MCLK_RATIOS) {
  369. /* We did not find a matching ratio */
  370. dev_err(codec->dev, "could not find matching ratio\n");
  371. return -EINVAL;
  372. }
  373. /* Set the sample rate */
  374. reg = snd_soc_read(codec, CS4270_MODE);
  375. reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
  376. reg |= cs4270_mode_ratios[i].mclk;
  377. if (cs4270->slave_mode)
  378. reg |= CS4270_MODE_SLAVE;
  379. else
  380. reg |= cs4270_mode_ratios[i].speed_mode;
  381. ret = snd_soc_write(codec, CS4270_MODE, reg);
  382. if (ret < 0) {
  383. dev_err(codec->dev, "i2c write failed\n");
  384. return ret;
  385. }
  386. /* Set the DAI format */
  387. reg = snd_soc_read(codec, CS4270_FORMAT);
  388. reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
  389. switch (cs4270->mode) {
  390. case SND_SOC_DAIFMT_I2S:
  391. reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
  392. break;
  393. case SND_SOC_DAIFMT_LEFT_J:
  394. reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
  395. break;
  396. default:
  397. dev_err(codec->dev, "unknown dai format\n");
  398. return -EINVAL;
  399. }
  400. ret = snd_soc_write(codec, CS4270_FORMAT, reg);
  401. if (ret < 0) {
  402. dev_err(codec->dev, "i2c write failed\n");
  403. return ret;
  404. }
  405. return ret;
  406. }
  407. /**
  408. * cs4270_mute - enable/disable the CS4270 external mute
  409. * @dai: the SOC DAI
  410. * @mute: 0 = disable mute, 1 = enable mute
  411. *
  412. * This function toggles the mute bits in the MUTE register. The CS4270's
  413. * mute capability is intended for external muting circuitry, so if the
  414. * board does not have the MUTEA or MUTEB pins connected to such circuitry,
  415. * then this function will do nothing.
  416. */
  417. static int cs4270_mute(struct snd_soc_dai *dai, int mute)
  418. {
  419. struct snd_soc_codec *codec = dai->codec;
  420. int reg6;
  421. reg6 = snd_soc_read(codec, CS4270_MUTE);
  422. if (mute)
  423. reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
  424. else
  425. reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
  426. return snd_soc_write(codec, CS4270_MUTE, reg6);
  427. }
  428. /* A list of non-DAPM controls that the CS4270 supports */
  429. static const struct snd_kcontrol_new cs4270_snd_controls[] = {
  430. SOC_DOUBLE_R("Master Playback Volume",
  431. CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
  432. SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
  433. SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
  434. SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
  435. SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
  436. SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
  437. SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 0)
  438. };
  439. /*
  440. * cs4270_codec - global variable to store codec for the ASoC probe function
  441. *
  442. * If struct i2c_driver had a private_data field, we wouldn't need to use
  443. * cs4270_codec. This is the only way to pass the codec structure from
  444. * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
  445. * way to synchronize these two functions. cs4270_i2c_probe() can be called
  446. * multiple times before cs4270_probe() is called even once. So for now, we
  447. * also only allow cs4270_i2c_probe() to be run once. That means that we do
  448. * not support more than one cs4270 device in the system, at least for now.
  449. */
  450. static struct snd_soc_codec *cs4270_codec;
  451. static struct snd_soc_dai_ops cs4270_dai_ops = {
  452. .hw_params = cs4270_hw_params,
  453. .set_sysclk = cs4270_set_dai_sysclk,
  454. .set_fmt = cs4270_set_dai_fmt,
  455. .digital_mute = cs4270_mute,
  456. };
  457. struct snd_soc_dai cs4270_dai = {
  458. .name = "cs4270",
  459. .playback = {
  460. .stream_name = "Playback",
  461. .channels_min = 1,
  462. .channels_max = 2,
  463. .rates = 0,
  464. .formats = CS4270_FORMATS,
  465. },
  466. .capture = {
  467. .stream_name = "Capture",
  468. .channels_min = 1,
  469. .channels_max = 2,
  470. .rates = 0,
  471. .formats = CS4270_FORMATS,
  472. },
  473. .ops = &cs4270_dai_ops,
  474. };
  475. EXPORT_SYMBOL_GPL(cs4270_dai);
  476. /**
  477. * cs4270_probe - ASoC probe function
  478. * @pdev: platform device
  479. *
  480. * This function is called when ASoC has all the pieces it needs to
  481. * instantiate a sound driver.
  482. */
  483. static int cs4270_probe(struct platform_device *pdev)
  484. {
  485. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  486. struct snd_soc_codec *codec = cs4270_codec;
  487. int ret;
  488. /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
  489. socdev->card->codec = codec;
  490. /* Register PCMs */
  491. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  492. if (ret < 0) {
  493. dev_err(codec->dev, "failed to create pcms\n");
  494. return ret;
  495. }
  496. /* Add the non-DAPM controls */
  497. ret = snd_soc_add_controls(codec, cs4270_snd_controls,
  498. ARRAY_SIZE(cs4270_snd_controls));
  499. if (ret < 0) {
  500. dev_err(codec->dev, "failed to add controls\n");
  501. goto error_free_pcms;
  502. }
  503. /* And finally, register the socdev */
  504. ret = snd_soc_init_card(socdev);
  505. if (ret < 0) {
  506. dev_err(codec->dev, "failed to register card\n");
  507. goto error_free_pcms;
  508. }
  509. return 0;
  510. error_free_pcms:
  511. snd_soc_free_pcms(socdev);
  512. return ret;
  513. }
  514. /**
  515. * cs4270_remove - ASoC remove function
  516. * @pdev: platform device
  517. *
  518. * This function is the counterpart to cs4270_probe().
  519. */
  520. static int cs4270_remove(struct platform_device *pdev)
  521. {
  522. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  523. snd_soc_free_pcms(socdev);
  524. return 0;
  525. };
  526. /**
  527. * cs4270_i2c_probe - initialize the I2C interface of the CS4270
  528. * @i2c_client: the I2C client object
  529. * @id: the I2C device ID (ignored)
  530. *
  531. * This function is called whenever the I2C subsystem finds a device that
  532. * matches the device ID given via a prior call to i2c_add_driver().
  533. */
  534. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  535. const struct i2c_device_id *id)
  536. {
  537. struct snd_soc_codec *codec;
  538. struct cs4270_private *cs4270;
  539. unsigned int reg;
  540. int ret;
  541. /* For now, we only support one cs4270 device in the system. See the
  542. * comment for cs4270_codec.
  543. */
  544. if (cs4270_codec) {
  545. dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
  546. i2c_client->addr);
  547. dev_err(&i2c_client->dev, "only one per board allowed\n");
  548. /* Should we return something other than ENODEV here? */
  549. return -ENODEV;
  550. }
  551. /* Verify that we have a CS4270 */
  552. ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
  553. if (ret < 0) {
  554. dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
  555. i2c_client->addr);
  556. return ret;
  557. }
  558. /* The top four bits of the chip ID should be 1100. */
  559. if ((ret & 0xF0) != 0xC0) {
  560. dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
  561. i2c_client->addr);
  562. return -ENODEV;
  563. }
  564. dev_info(&i2c_client->dev, "found device at i2c address %X\n",
  565. i2c_client->addr);
  566. dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
  567. /* Allocate enough space for the snd_soc_codec structure
  568. and our private data together. */
  569. cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
  570. if (!cs4270) {
  571. dev_err(&i2c_client->dev, "could not allocate codec\n");
  572. return -ENOMEM;
  573. }
  574. codec = &cs4270->codec;
  575. mutex_init(&codec->mutex);
  576. INIT_LIST_HEAD(&codec->dapm_widgets);
  577. INIT_LIST_HEAD(&codec->dapm_paths);
  578. codec->dev = &i2c_client->dev;
  579. codec->name = "CS4270";
  580. codec->owner = THIS_MODULE;
  581. codec->dai = &cs4270_dai;
  582. codec->num_dai = 1;
  583. codec->private_data = cs4270;
  584. codec->control_data = i2c_client;
  585. codec->read = cs4270_read_reg_cache;
  586. codec->write = cs4270_i2c_write;
  587. codec->reg_cache = cs4270->reg_cache;
  588. codec->reg_cache_size = CS4270_NUMREGS;
  589. /* The I2C interface is set up, so pre-fill our register cache */
  590. ret = cs4270_fill_cache(codec);
  591. if (ret < 0) {
  592. dev_err(&i2c_client->dev, "failed to fill register cache\n");
  593. goto error_free_codec;
  594. }
  595. /* Disable auto-mute. This feature appears to be buggy. In some
  596. * situations, auto-mute will not deactivate when it should, so we want
  597. * this feature disabled by default. An application (e.g. alsactl) can
  598. * re-enabled it by using the controls.
  599. */
  600. reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
  601. reg &= ~CS4270_MUTE_AUTO;
  602. ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
  603. if (ret < 0) {
  604. dev_err(&i2c_client->dev, "i2c write failed\n");
  605. return ret;
  606. }
  607. /* Disable automatic volume control. The hardware enables, and it
  608. * causes volume change commands to be delayed, sometimes until after
  609. * playback has started. An application (e.g. alsactl) can
  610. * re-enabled it by using the controls.
  611. */
  612. reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
  613. reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
  614. ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
  615. if (ret < 0) {
  616. dev_err(&i2c_client->dev, "i2c write failed\n");
  617. return ret;
  618. }
  619. /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
  620. * structure for each CS4270 device, but the machine driver needs to
  621. * have a pointer to the DAI structure, so for now it must be a global
  622. * variable.
  623. */
  624. cs4270_dai.dev = &i2c_client->dev;
  625. /* Register the DAI. If all the other ASoC driver have already
  626. * registered, then this will call our probe function, so
  627. * cs4270_codec needs to be ready.
  628. */
  629. cs4270_codec = codec;
  630. ret = snd_soc_register_dai(&cs4270_dai);
  631. if (ret < 0) {
  632. dev_err(&i2c_client->dev, "failed to register DAIe\n");
  633. goto error_free_codec;
  634. }
  635. i2c_set_clientdata(i2c_client, cs4270);
  636. return 0;
  637. error_free_codec:
  638. kfree(cs4270);
  639. cs4270_codec = NULL;
  640. cs4270_dai.dev = NULL;
  641. return ret;
  642. }
  643. /**
  644. * cs4270_i2c_remove - remove an I2C device
  645. * @i2c_client: the I2C client object
  646. *
  647. * This function is the counterpart to cs4270_i2c_probe().
  648. */
  649. static int cs4270_i2c_remove(struct i2c_client *i2c_client)
  650. {
  651. struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
  652. kfree(cs4270);
  653. cs4270_codec = NULL;
  654. cs4270_dai.dev = NULL;
  655. return 0;
  656. }
  657. /*
  658. * cs4270_id - I2C device IDs supported by this driver
  659. */
  660. static struct i2c_device_id cs4270_id[] = {
  661. {"cs4270", 0},
  662. {}
  663. };
  664. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  665. /*
  666. * cs4270_i2c_driver - I2C device identification
  667. *
  668. * This structure tells the I2C subsystem how to identify and support a
  669. * given I2C device type.
  670. */
  671. static struct i2c_driver cs4270_i2c_driver = {
  672. .driver = {
  673. .name = "cs4270",
  674. .owner = THIS_MODULE,
  675. },
  676. .id_table = cs4270_id,
  677. .probe = cs4270_i2c_probe,
  678. .remove = cs4270_i2c_remove,
  679. };
  680. /*
  681. * ASoC codec device structure
  682. *
  683. * Assign this variable to the codec_dev field of the machine driver's
  684. * snd_soc_device structure.
  685. */
  686. struct snd_soc_codec_device soc_codec_device_cs4270 = {
  687. .probe = cs4270_probe,
  688. .remove = cs4270_remove
  689. };
  690. EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
  691. static int __init cs4270_init(void)
  692. {
  693. pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
  694. return i2c_add_driver(&cs4270_i2c_driver);
  695. }
  696. module_init(cs4270_init);
  697. static void __exit cs4270_exit(void)
  698. {
  699. i2c_del_driver(&cs4270_i2c_driver);
  700. }
  701. module_exit(cs4270_exit);
  702. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  703. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  704. MODULE_LICENSE("GPL");