cs4270.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 supported
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include <sound/soc.h>
  28. #include <sound/initval.h>
  29. #include <linux/i2c.h>
  30. #include <linux/delay.h>
  31. #include <linux/regulator/consumer.h>
  32. #include "cs4270.h"
  33. /*
  34. * The codec isn't really big-endian or little-endian, since the I2S
  35. * interface requires data to be sent serially with the MSbit first.
  36. * However, to support BE and LE I2S devices, we specify both here. That
  37. * way, ALSA will always match the bit patterns.
  38. */
  39. #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  40. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  41. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  42. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  43. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  44. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  45. /* CS4270 registers addresses */
  46. #define CS4270_CHIPID 0x01 /* Chip ID */
  47. #define CS4270_PWRCTL 0x02 /* Power Control */
  48. #define CS4270_MODE 0x03 /* Mode Control */
  49. #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
  50. #define CS4270_TRANS 0x05 /* Transition Control */
  51. #define CS4270_MUTE 0x06 /* Mute Control */
  52. #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
  53. #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
  54. #define CS4270_FIRSTREG 0x01
  55. #define CS4270_LASTREG 0x08
  56. #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
  57. #define CS4270_I2C_INCR 0x80
  58. /* Bit masks for the CS4270 registers */
  59. #define CS4270_CHIPID_ID 0xF0
  60. #define CS4270_CHIPID_REV 0x0F
  61. #define CS4270_PWRCTL_FREEZE 0x80
  62. #define CS4270_PWRCTL_PDN_ADC 0x20
  63. #define CS4270_PWRCTL_PDN_DAC 0x02
  64. #define CS4270_PWRCTL_PDN 0x01
  65. #define CS4270_PWRCTL_PDN_ALL \
  66. (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
  67. #define CS4270_MODE_SPEED_MASK 0x30
  68. #define CS4270_MODE_1X 0x00
  69. #define CS4270_MODE_2X 0x10
  70. #define CS4270_MODE_4X 0x20
  71. #define CS4270_MODE_SLAVE 0x30
  72. #define CS4270_MODE_DIV_MASK 0x0E
  73. #define CS4270_MODE_DIV1 0x00
  74. #define CS4270_MODE_DIV15 0x02
  75. #define CS4270_MODE_DIV2 0x04
  76. #define CS4270_MODE_DIV3 0x06
  77. #define CS4270_MODE_DIV4 0x08
  78. #define CS4270_MODE_POPGUARD 0x01
  79. #define CS4270_FORMAT_FREEZE_A 0x80
  80. #define CS4270_FORMAT_FREEZE_B 0x40
  81. #define CS4270_FORMAT_LOOPBACK 0x20
  82. #define CS4270_FORMAT_DAC_MASK 0x18
  83. #define CS4270_FORMAT_DAC_LJ 0x00
  84. #define CS4270_FORMAT_DAC_I2S 0x08
  85. #define CS4270_FORMAT_DAC_RJ16 0x18
  86. #define CS4270_FORMAT_DAC_RJ24 0x10
  87. #define CS4270_FORMAT_ADC_MASK 0x01
  88. #define CS4270_FORMAT_ADC_LJ 0x00
  89. #define CS4270_FORMAT_ADC_I2S 0x01
  90. #define CS4270_TRANS_ONE_VOL 0x80
  91. #define CS4270_TRANS_SOFT 0x40
  92. #define CS4270_TRANS_ZERO 0x20
  93. #define CS4270_TRANS_INV_ADC_A 0x08
  94. #define CS4270_TRANS_INV_ADC_B 0x10
  95. #define CS4270_TRANS_INV_DAC_A 0x02
  96. #define CS4270_TRANS_INV_DAC_B 0x04
  97. #define CS4270_TRANS_DEEMPH 0x01
  98. #define CS4270_MUTE_AUTO 0x20
  99. #define CS4270_MUTE_ADC_A 0x08
  100. #define CS4270_MUTE_ADC_B 0x10
  101. #define CS4270_MUTE_POLARITY 0x04
  102. #define CS4270_MUTE_DAC_A 0x01
  103. #define CS4270_MUTE_DAC_B 0x02
  104. static const char *supply_names[] = {
  105. "va", "vd", "vlc"
  106. };
  107. /* Private data for the CS4270 */
  108. struct cs4270_private {
  109. struct snd_soc_codec codec;
  110. u8 reg_cache[CS4270_NUMREGS];
  111. unsigned int mclk; /* Input frequency of the MCLK pin */
  112. unsigned int mode; /* The mode (I2S or left-justified) */
  113. unsigned int slave_mode;
  114. unsigned int manual_mute;
  115. /* power domain regulators */
  116. struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
  117. };
  118. /**
  119. * struct cs4270_mode_ratios - clock ratio tables
  120. * @ratio: the ratio of MCLK to the sample rate
  121. * @speed_mode: the Speed Mode bits to set in the Mode Control register for
  122. * this ratio
  123. * @mclk: the Ratio Select bits to set in the Mode Control register for this
  124. * ratio
  125. *
  126. * The data for this chart is taken from Table 5 of the CS4270 reference
  127. * manual.
  128. *
  129. * This table is used to determine how to program the Mode Control register.
  130. * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
  131. * rates the CS4270 currently supports.
  132. *
  133. * @speed_mode is the corresponding bit pattern to be written to the
  134. * MODE bits of the Mode Control Register
  135. *
  136. * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
  137. * the Mode Control Register.
  138. *
  139. * In situations where a single ratio is represented by multiple speed
  140. * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
  141. * double-speed instead of quad-speed. However, the CS4270 errata states
  142. * that divide-By-1.5 can cause failures, so we avoid that mode where
  143. * possible.
  144. *
  145. * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
  146. * work if Vd is 3.3V. If this effects you, select the
  147. * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
  148. * never select any sample rates that require divide-by-1.5.
  149. */
  150. struct cs4270_mode_ratios {
  151. unsigned int ratio;
  152. u8 speed_mode;
  153. u8 mclk;
  154. };
  155. static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
  156. {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
  157. #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
  158. {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
  159. #endif
  160. {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
  161. {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
  162. {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
  163. {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
  164. {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
  165. {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
  166. {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
  167. };
  168. /* The number of MCLK/LRCK ratios supported by the CS4270 */
  169. #define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
  170. /**
  171. * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
  172. * @codec_dai: the codec DAI
  173. * @clk_id: the clock ID (ignored)
  174. * @freq: the MCLK input frequency
  175. * @dir: the clock direction (ignored)
  176. *
  177. * This function is used to tell the codec driver what the input MCLK
  178. * frequency is.
  179. *
  180. * The value of MCLK is used to determine which sample rates are supported
  181. * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
  182. * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
  183. *
  184. * This function calculates the nine ratios and determines which ones match
  185. * a standard sample rate. If there's a match, then it is added to the list
  186. * of supported sample rates.
  187. *
  188. * This function must be called by the machine driver's 'startup' function,
  189. * otherwise the list of supported sample rates will not be available in
  190. * time for ALSA.
  191. *
  192. * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
  193. * theoretically possible sample rates to be enabled. Call it again with a
  194. * proper value set one the external clock is set (most probably you would do
  195. * that from a machine's driver 'hw_param' hook.
  196. */
  197. static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  198. int clk_id, unsigned int freq, int dir)
  199. {
  200. struct snd_soc_codec *codec = codec_dai->codec;
  201. struct cs4270_private *cs4270 = codec->private_data;
  202. unsigned int rates = 0;
  203. unsigned int rate_min = -1;
  204. unsigned int rate_max = 0;
  205. unsigned int i;
  206. cs4270->mclk = freq;
  207. if (cs4270->mclk) {
  208. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  209. unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
  210. rates |= snd_pcm_rate_to_rate_bit(rate);
  211. if (rate < rate_min)
  212. rate_min = rate;
  213. if (rate > rate_max)
  214. rate_max = rate;
  215. }
  216. /* FIXME: soc should support a rate list */
  217. rates &= ~SNDRV_PCM_RATE_KNOT;
  218. if (!rates) {
  219. dev_err(codec->dev, "could not find a valid sample rate\n");
  220. return -EINVAL;
  221. }
  222. } else {
  223. /* enable all possible rates */
  224. rates = SNDRV_PCM_RATE_8000_192000;
  225. rate_min = 8000;
  226. rate_max = 192000;
  227. }
  228. codec_dai->playback.rates = rates;
  229. codec_dai->playback.rate_min = rate_min;
  230. codec_dai->playback.rate_max = rate_max;
  231. codec_dai->capture.rates = rates;
  232. codec_dai->capture.rate_min = rate_min;
  233. codec_dai->capture.rate_max = rate_max;
  234. return 0;
  235. }
  236. /**
  237. * cs4270_set_dai_fmt - configure the codec for the selected audio format
  238. * @codec_dai: the codec DAI
  239. * @format: a SND_SOC_DAIFMT_x value indicating the data format
  240. *
  241. * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
  242. * codec accordingly.
  243. *
  244. * Currently, this function only supports SND_SOC_DAIFMT_I2S and
  245. * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
  246. * data for playback only, but ASoC currently does not support different
  247. * formats for playback vs. record.
  248. */
  249. static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
  250. unsigned int format)
  251. {
  252. struct snd_soc_codec *codec = codec_dai->codec;
  253. struct cs4270_private *cs4270 = codec->private_data;
  254. int ret = 0;
  255. /* set DAI format */
  256. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  257. case SND_SOC_DAIFMT_I2S:
  258. case SND_SOC_DAIFMT_LEFT_J:
  259. cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  260. break;
  261. default:
  262. dev_err(codec->dev, "invalid dai format\n");
  263. ret = -EINVAL;
  264. }
  265. /* set master/slave audio interface */
  266. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  267. case SND_SOC_DAIFMT_CBS_CFS:
  268. cs4270->slave_mode = 1;
  269. break;
  270. case SND_SOC_DAIFMT_CBM_CFM:
  271. cs4270->slave_mode = 0;
  272. break;
  273. default:
  274. /* all other modes are unsupported by the hardware */
  275. ret = -EINVAL;
  276. }
  277. return ret;
  278. }
  279. /**
  280. * cs4270_fill_cache - pre-fill the CS4270 register cache.
  281. * @codec: the codec for this CS4270
  282. *
  283. * This function fills in the CS4270 register cache by reading the register
  284. * values from the hardware.
  285. *
  286. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  287. * After the initial read to pre-fill the cache, the CS4270 never updates
  288. * the register values, so we won't have a cache coherency problem.
  289. *
  290. * We use the auto-increment feature of the CS4270 to read all registers in
  291. * one shot.
  292. */
  293. static int cs4270_fill_cache(struct snd_soc_codec *codec)
  294. {
  295. u8 *cache = codec->reg_cache;
  296. struct i2c_client *i2c_client = codec->control_data;
  297. s32 length;
  298. length = i2c_smbus_read_i2c_block_data(i2c_client,
  299. CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache);
  300. if (length != CS4270_NUMREGS) {
  301. dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
  302. i2c_client->addr);
  303. return -EIO;
  304. }
  305. return 0;
  306. }
  307. /**
  308. * cs4270_read_reg_cache - read from the CS4270 register cache.
  309. * @codec: the codec for this CS4270
  310. * @reg: the register to read
  311. *
  312. * This function returns the value for a given register. It reads only from
  313. * the register cache, not the hardware itself.
  314. *
  315. * This CS4270 registers are cached to avoid excessive I2C I/O operations.
  316. * After the initial read to pre-fill the cache, the CS4270 never updates
  317. * the register values, so we won't have a cache coherency problem.
  318. */
  319. static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
  320. unsigned int reg)
  321. {
  322. u8 *cache = codec->reg_cache;
  323. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  324. return -EIO;
  325. return cache[reg - CS4270_FIRSTREG];
  326. }
  327. /**
  328. * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
  329. * @codec: the codec for this CS4270
  330. * @reg: the register to write
  331. * @value: the value to write to the register
  332. *
  333. * This function writes the given value to the given CS4270 register, and
  334. * also updates the register cache.
  335. *
  336. * Note that we don't use the hw_write function pointer of snd_soc_codec.
  337. * That's because it's too clunky: the hw_write_t prototype does not match
  338. * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
  339. */
  340. static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
  341. unsigned int value)
  342. {
  343. u8 *cache = codec->reg_cache;
  344. if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
  345. return -EIO;
  346. /* Only perform an I2C operation if the new value is different */
  347. if (cache[reg - CS4270_FIRSTREG] != value) {
  348. struct i2c_client *client = codec->control_data;
  349. if (i2c_smbus_write_byte_data(client, reg, value)) {
  350. dev_err(codec->dev, "i2c write failed\n");
  351. return -EIO;
  352. }
  353. /* We've written to the hardware, so update the cache */
  354. cache[reg - CS4270_FIRSTREG] = value;
  355. }
  356. return 0;
  357. }
  358. /**
  359. * cs4270_hw_params - program the CS4270 with the given hardware parameters.
  360. * @substream: the audio stream
  361. * @params: the hardware parameters to set
  362. * @dai: the SOC DAI (ignored)
  363. *
  364. * This function programs the hardware with the values provided.
  365. * Specifically, the sample rate and the data format.
  366. *
  367. * The .ops functions are used to provide board-specific data, like input
  368. * frequencies, to this driver. This function takes that information,
  369. * combines it with the hardware parameters provided, and programs the
  370. * hardware accordingly.
  371. */
  372. static int cs4270_hw_params(struct snd_pcm_substream *substream,
  373. struct snd_pcm_hw_params *params,
  374. struct snd_soc_dai *dai)
  375. {
  376. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  377. struct snd_soc_device *socdev = rtd->socdev;
  378. struct snd_soc_codec *codec = socdev->card->codec;
  379. struct cs4270_private *cs4270 = codec->private_data;
  380. int ret;
  381. unsigned int i;
  382. unsigned int rate;
  383. unsigned int ratio;
  384. int reg;
  385. /* Figure out which MCLK/LRCK ratio to use */
  386. rate = params_rate(params); /* Sampling rate, in Hz */
  387. ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
  388. for (i = 0; i < NUM_MCLK_RATIOS; i++) {
  389. if (cs4270_mode_ratios[i].ratio == ratio)
  390. break;
  391. }
  392. if (i == NUM_MCLK_RATIOS) {
  393. /* We did not find a matching ratio */
  394. dev_err(codec->dev, "could not find matching ratio\n");
  395. return -EINVAL;
  396. }
  397. /* Set the sample rate */
  398. reg = snd_soc_read(codec, CS4270_MODE);
  399. reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
  400. reg |= cs4270_mode_ratios[i].mclk;
  401. if (cs4270->slave_mode)
  402. reg |= CS4270_MODE_SLAVE;
  403. else
  404. reg |= cs4270_mode_ratios[i].speed_mode;
  405. ret = snd_soc_write(codec, CS4270_MODE, reg);
  406. if (ret < 0) {
  407. dev_err(codec->dev, "i2c write failed\n");
  408. return ret;
  409. }
  410. /* Set the DAI format */
  411. reg = snd_soc_read(codec, CS4270_FORMAT);
  412. reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
  413. switch (cs4270->mode) {
  414. case SND_SOC_DAIFMT_I2S:
  415. reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
  416. break;
  417. case SND_SOC_DAIFMT_LEFT_J:
  418. reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
  419. break;
  420. default:
  421. dev_err(codec->dev, "unknown dai format\n");
  422. return -EINVAL;
  423. }
  424. ret = snd_soc_write(codec, CS4270_FORMAT, reg);
  425. if (ret < 0) {
  426. dev_err(codec->dev, "i2c write failed\n");
  427. return ret;
  428. }
  429. return ret;
  430. }
  431. /**
  432. * cs4270_dai_mute - enable/disable the CS4270 external mute
  433. * @dai: the SOC DAI
  434. * @mute: 0 = disable mute, 1 = enable mute
  435. *
  436. * This function toggles the mute bits in the MUTE register. The CS4270's
  437. * mute capability is intended for external muting circuitry, so if the
  438. * board does not have the MUTEA or MUTEB pins connected to such circuitry,
  439. * then this function will do nothing.
  440. */
  441. static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
  442. {
  443. struct snd_soc_codec *codec = dai->codec;
  444. struct cs4270_private *cs4270 = codec->private_data;
  445. int reg6;
  446. reg6 = snd_soc_read(codec, CS4270_MUTE);
  447. if (mute)
  448. reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
  449. else {
  450. reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
  451. reg6 |= cs4270->manual_mute;
  452. }
  453. return snd_soc_write(codec, CS4270_MUTE, reg6);
  454. }
  455. /**
  456. * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
  457. * alsa control.
  458. * @kcontrol: mixer control
  459. * @ucontrol: control element information
  460. *
  461. * This function basically passes the arguments on to the generic
  462. * snd_soc_put_volsw() function and saves the mute information in
  463. * our private data structure. This is because we want to prevent
  464. * cs4270_dai_mute() neglecting the user's decision to manually
  465. * mute the codec's output.
  466. *
  467. * Returns 0 for success.
  468. */
  469. static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
  470. struct snd_ctl_elem_value *ucontrol)
  471. {
  472. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  473. struct cs4270_private *cs4270 = codec->private_data;
  474. int left = !ucontrol->value.integer.value[0];
  475. int right = !ucontrol->value.integer.value[1];
  476. cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
  477. (right ? CS4270_MUTE_DAC_B : 0);
  478. return snd_soc_put_volsw(kcontrol, ucontrol);
  479. }
  480. /* A list of non-DAPM controls that the CS4270 supports */
  481. static const struct snd_kcontrol_new cs4270_snd_controls[] = {
  482. SOC_DOUBLE_R("Master Playback Volume",
  483. CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
  484. SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
  485. SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
  486. SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
  487. SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
  488. SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
  489. SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
  490. SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
  491. SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
  492. snd_soc_get_volsw, cs4270_soc_put_mute),
  493. };
  494. /*
  495. * cs4270_codec - global variable to store codec for the ASoC probe function
  496. *
  497. * If struct i2c_driver had a private_data field, we wouldn't need to use
  498. * cs4270_codec. This is the only way to pass the codec structure from
  499. * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
  500. * way to synchronize these two functions. cs4270_i2c_probe() can be called
  501. * multiple times before cs4270_probe() is called even once. So for now, we
  502. * also only allow cs4270_i2c_probe() to be run once. That means that we do
  503. * not support more than one cs4270 device in the system, at least for now.
  504. */
  505. static struct snd_soc_codec *cs4270_codec;
  506. static struct snd_soc_dai_ops cs4270_dai_ops = {
  507. .hw_params = cs4270_hw_params,
  508. .set_sysclk = cs4270_set_dai_sysclk,
  509. .set_fmt = cs4270_set_dai_fmt,
  510. .digital_mute = cs4270_dai_mute,
  511. };
  512. struct snd_soc_dai cs4270_dai = {
  513. .name = "cs4270",
  514. .playback = {
  515. .stream_name = "Playback",
  516. .channels_min = 1,
  517. .channels_max = 2,
  518. .rates = 0,
  519. .formats = CS4270_FORMATS,
  520. },
  521. .capture = {
  522. .stream_name = "Capture",
  523. .channels_min = 1,
  524. .channels_max = 2,
  525. .rates = 0,
  526. .formats = CS4270_FORMATS,
  527. },
  528. .ops = &cs4270_dai_ops,
  529. };
  530. EXPORT_SYMBOL_GPL(cs4270_dai);
  531. /**
  532. * cs4270_probe - ASoC probe function
  533. * @pdev: platform device
  534. *
  535. * This function is called when ASoC has all the pieces it needs to
  536. * instantiate a sound driver.
  537. */
  538. static int cs4270_probe(struct platform_device *pdev)
  539. {
  540. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  541. struct snd_soc_codec *codec = cs4270_codec;
  542. struct cs4270_private *cs4270 = codec->private_data;
  543. int i, ret;
  544. /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
  545. socdev->card->codec = codec;
  546. /* Register PCMs */
  547. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  548. if (ret < 0) {
  549. dev_err(codec->dev, "failed to create pcms\n");
  550. return ret;
  551. }
  552. /* Add the non-DAPM controls */
  553. ret = snd_soc_add_controls(codec, cs4270_snd_controls,
  554. ARRAY_SIZE(cs4270_snd_controls));
  555. if (ret < 0) {
  556. dev_err(codec->dev, "failed to add controls\n");
  557. goto error_free_pcms;
  558. }
  559. /* get the power supply regulators */
  560. for (i = 0; i < ARRAY_SIZE(supply_names); i++)
  561. cs4270->supplies[i].supply = supply_names[i];
  562. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
  563. cs4270->supplies);
  564. if (ret < 0)
  565. goto error_free_pcms;
  566. ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
  567. cs4270->supplies);
  568. if (ret < 0)
  569. goto error_free_regulators;
  570. return 0;
  571. error_free_regulators:
  572. regulator_bulk_free(ARRAY_SIZE(cs4270->supplies),
  573. cs4270->supplies);
  574. error_free_pcms:
  575. snd_soc_free_pcms(socdev);
  576. return ret;
  577. }
  578. /**
  579. * cs4270_remove - ASoC remove function
  580. * @pdev: platform device
  581. *
  582. * This function is the counterpart to cs4270_probe().
  583. */
  584. static int cs4270_remove(struct platform_device *pdev)
  585. {
  586. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  587. struct snd_soc_codec *codec = cs4270_codec;
  588. struct cs4270_private *cs4270 = codec->private_data;
  589. snd_soc_free_pcms(socdev);
  590. regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
  591. regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
  592. return 0;
  593. };
  594. /**
  595. * cs4270_i2c_probe - initialize the I2C interface of the CS4270
  596. * @i2c_client: the I2C client object
  597. * @id: the I2C device ID (ignored)
  598. *
  599. * This function is called whenever the I2C subsystem finds a device that
  600. * matches the device ID given via a prior call to i2c_add_driver().
  601. */
  602. static int cs4270_i2c_probe(struct i2c_client *i2c_client,
  603. const struct i2c_device_id *id)
  604. {
  605. struct snd_soc_codec *codec;
  606. struct cs4270_private *cs4270;
  607. unsigned int reg;
  608. int ret;
  609. /* For now, we only support one cs4270 device in the system. See the
  610. * comment for cs4270_codec.
  611. */
  612. if (cs4270_codec) {
  613. dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
  614. i2c_client->addr);
  615. dev_err(&i2c_client->dev, "only one per board allowed\n");
  616. /* Should we return something other than ENODEV here? */
  617. return -ENODEV;
  618. }
  619. /* Verify that we have a CS4270 */
  620. ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
  621. if (ret < 0) {
  622. dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
  623. i2c_client->addr);
  624. return ret;
  625. }
  626. /* The top four bits of the chip ID should be 1100. */
  627. if ((ret & 0xF0) != 0xC0) {
  628. dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
  629. i2c_client->addr);
  630. return -ENODEV;
  631. }
  632. dev_info(&i2c_client->dev, "found device at i2c address %X\n",
  633. i2c_client->addr);
  634. dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
  635. /* Allocate enough space for the snd_soc_codec structure
  636. and our private data together. */
  637. cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
  638. if (!cs4270) {
  639. dev_err(&i2c_client->dev, "could not allocate codec\n");
  640. return -ENOMEM;
  641. }
  642. codec = &cs4270->codec;
  643. mutex_init(&codec->mutex);
  644. INIT_LIST_HEAD(&codec->dapm_widgets);
  645. INIT_LIST_HEAD(&codec->dapm_paths);
  646. codec->dev = &i2c_client->dev;
  647. codec->name = "CS4270";
  648. codec->owner = THIS_MODULE;
  649. codec->dai = &cs4270_dai;
  650. codec->num_dai = 1;
  651. codec->private_data = cs4270;
  652. codec->control_data = i2c_client;
  653. codec->read = cs4270_read_reg_cache;
  654. codec->write = cs4270_i2c_write;
  655. codec->reg_cache = cs4270->reg_cache;
  656. codec->reg_cache_size = CS4270_NUMREGS;
  657. /* The I2C interface is set up, so pre-fill our register cache */
  658. ret = cs4270_fill_cache(codec);
  659. if (ret < 0) {
  660. dev_err(&i2c_client->dev, "failed to fill register cache\n");
  661. goto error_free_codec;
  662. }
  663. /* Disable auto-mute. This feature appears to be buggy. In some
  664. * situations, auto-mute will not deactivate when it should, so we want
  665. * this feature disabled by default. An application (e.g. alsactl) can
  666. * re-enabled it by using the controls.
  667. */
  668. reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
  669. reg &= ~CS4270_MUTE_AUTO;
  670. ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
  671. if (ret < 0) {
  672. dev_err(&i2c_client->dev, "i2c write failed\n");
  673. return ret;
  674. }
  675. /* Disable automatic volume control. The hardware enables, and it
  676. * causes volume change commands to be delayed, sometimes until after
  677. * playback has started. An application (e.g. alsactl) can
  678. * re-enabled it by using the controls.
  679. */
  680. reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
  681. reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
  682. ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
  683. if (ret < 0) {
  684. dev_err(&i2c_client->dev, "i2c write failed\n");
  685. return ret;
  686. }
  687. /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
  688. * structure for each CS4270 device, but the machine driver needs to
  689. * have a pointer to the DAI structure, so for now it must be a global
  690. * variable.
  691. */
  692. cs4270_dai.dev = &i2c_client->dev;
  693. /* Register the DAI. If all the other ASoC driver have already
  694. * registered, then this will call our probe function, so
  695. * cs4270_codec needs to be ready.
  696. */
  697. cs4270_codec = codec;
  698. ret = snd_soc_register_dai(&cs4270_dai);
  699. if (ret < 0) {
  700. dev_err(&i2c_client->dev, "failed to register DAIe\n");
  701. goto error_free_codec;
  702. }
  703. i2c_set_clientdata(i2c_client, cs4270);
  704. return 0;
  705. error_free_codec:
  706. kfree(cs4270);
  707. cs4270_codec = NULL;
  708. cs4270_dai.dev = NULL;
  709. return ret;
  710. }
  711. /**
  712. * cs4270_i2c_remove - remove an I2C device
  713. * @i2c_client: the I2C client object
  714. *
  715. * This function is the counterpart to cs4270_i2c_probe().
  716. */
  717. static int cs4270_i2c_remove(struct i2c_client *i2c_client)
  718. {
  719. struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
  720. kfree(cs4270);
  721. cs4270_codec = NULL;
  722. cs4270_dai.dev = NULL;
  723. return 0;
  724. }
  725. /*
  726. * cs4270_id - I2C device IDs supported by this driver
  727. */
  728. static struct i2c_device_id cs4270_id[] = {
  729. {"cs4270", 0},
  730. {}
  731. };
  732. MODULE_DEVICE_TABLE(i2c, cs4270_id);
  733. #ifdef CONFIG_PM
  734. /* This suspend/resume implementation can handle both - a simple standby
  735. * where the codec remains powered, and a full suspend, where the voltage
  736. * domain the codec is connected to is teared down and/or any other hardware
  737. * reset condition is asserted.
  738. *
  739. * The codec's own power saving features are enabled in the suspend callback,
  740. * and all registers are written back to the hardware when resuming.
  741. */
  742. static int cs4270_soc_suspend(struct platform_device *pdev, pm_message_t mesg)
  743. {
  744. struct snd_soc_codec *codec = cs4270_codec;
  745. struct cs4270_private *cs4270 = codec->private_data;
  746. int reg, ret;
  747. reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
  748. if (reg < 0)
  749. return reg;
  750. ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
  751. if (ret < 0)
  752. return ret;
  753. regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
  754. cs4270->supplies);
  755. return 0;
  756. }
  757. static int cs4270_soc_resume(struct platform_device *pdev)
  758. {
  759. struct snd_soc_codec *codec = cs4270_codec;
  760. struct cs4270_private *cs4270 = codec->private_data;
  761. struct i2c_client *i2c_client = codec->control_data;
  762. int reg;
  763. regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
  764. cs4270->supplies);
  765. /* In case the device was put to hard reset during sleep, we need to
  766. * wait 500ns here before any I2C communication. */
  767. ndelay(500);
  768. /* first restore the entire register cache ... */
  769. for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
  770. u8 val = snd_soc_read(codec, reg);
  771. if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
  772. dev_err(codec->dev, "i2c write failed\n");
  773. return -EIO;
  774. }
  775. }
  776. /* ... then disable the power-down bits */
  777. reg = snd_soc_read(codec, CS4270_PWRCTL);
  778. reg &= ~CS4270_PWRCTL_PDN_ALL;
  779. return snd_soc_write(codec, CS4270_PWRCTL, reg);
  780. }
  781. #else
  782. #define cs4270_soc_suspend NULL
  783. #define cs4270_soc_resume NULL
  784. #endif /* CONFIG_PM */
  785. /*
  786. * cs4270_i2c_driver - I2C device identification
  787. *
  788. * This structure tells the I2C subsystem how to identify and support a
  789. * given I2C device type.
  790. */
  791. static struct i2c_driver cs4270_i2c_driver = {
  792. .driver = {
  793. .name = "cs4270",
  794. .owner = THIS_MODULE,
  795. },
  796. .id_table = cs4270_id,
  797. .probe = cs4270_i2c_probe,
  798. .remove = cs4270_i2c_remove,
  799. };
  800. /*
  801. * ASoC codec device structure
  802. *
  803. * Assign this variable to the codec_dev field of the machine driver's
  804. * snd_soc_device structure.
  805. */
  806. struct snd_soc_codec_device soc_codec_device_cs4270 = {
  807. .probe = cs4270_probe,
  808. .remove = cs4270_remove,
  809. .suspend = cs4270_soc_suspend,
  810. .resume = cs4270_soc_resume,
  811. };
  812. EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
  813. static int __init cs4270_init(void)
  814. {
  815. pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
  816. return i2c_add_driver(&cs4270_i2c_driver);
  817. }
  818. module_init(cs4270_init);
  819. static void __exit cs4270_exit(void)
  820. {
  821. i2c_del_driver(&cs4270_i2c_driver);
  822. }
  823. module_exit(cs4270_exit);
  824. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  825. MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
  826. MODULE_LICENSE("GPL");