cs4270.c 28 KB

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