xref: /linux/sound/soc/codecs/cs4270.c (revision 80ab8817bf9b740df1f0778c41875e93151409bf)
1b0c813ceSTimur Tabi /*
2b0c813ceSTimur Tabi  * CS4270 ALSA SoC (ASoC) codec driver
3b0c813ceSTimur Tabi  *
4b0c813ceSTimur Tabi  * Author: Timur Tabi <timur@freescale.com>
5b0c813ceSTimur Tabi  *
6ff7bf02fSTimur Tabi  * Copyright 2007-2009 Freescale Semiconductor, Inc.  This file is licensed
7ff7bf02fSTimur Tabi  * under the terms of the GNU General Public License version 2.  This
8ff7bf02fSTimur Tabi  * program is licensed "as is" without any warranty of any kind, whether
9ff7bf02fSTimur Tabi  * express or implied.
10b0c813ceSTimur Tabi  *
11b0c813ceSTimur Tabi  * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
12b0c813ceSTimur Tabi  *
13b0c813ceSTimur Tabi  * Current features/limitations:
14b0c813ceSTimur Tabi  *
15b191f63cSDaniel Mack  * - Software mode is supported.  Stand-alone mode is not supported.
16b191f63cSDaniel Mack  * - Only I2C is supported, not SPI
17b191f63cSDaniel Mack  * - Support for master and slave mode
18b191f63cSDaniel Mack  * - The machine driver's 'startup' function must call
19b0c813ceSTimur Tabi  *   cs4270_set_dai_sysclk() with the value of MCLK.
20b191f63cSDaniel Mack  * - Only I2S and left-justified modes are supported
21b191f63cSDaniel Mack  * - Power management is not supported
22b0c813ceSTimur Tabi  */
23b0c813ceSTimur Tabi 
24b0c813ceSTimur Tabi #include <linux/module.h>
25b0c813ceSTimur Tabi #include <linux/platform_device.h>
26b0c813ceSTimur Tabi #include <sound/core.h>
27b0c813ceSTimur Tabi #include <sound/soc.h>
28b0c813ceSTimur Tabi #include <sound/initval.h>
29b0c813ceSTimur Tabi #include <linux/i2c.h>
30b0c813ceSTimur Tabi 
3101e097d6SMark Brown #include "cs4270.h"
3201e097d6SMark Brown 
33b0c813ceSTimur Tabi /*
34b0c813ceSTimur Tabi  * The codec isn't really big-endian or little-endian, since the I2S
35b0c813ceSTimur Tabi  * interface requires data to be sent serially with the MSbit first.
36b0c813ceSTimur Tabi  * However, to support BE and LE I2S devices, we specify both here.  That
37b0c813ceSTimur Tabi  * way, ALSA will always match the bit patterns.
38b0c813ceSTimur Tabi  */
39b0c813ceSTimur Tabi #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | \
40b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
41b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
42b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
43b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
44b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
45b0c813ceSTimur Tabi 
46b0c813ceSTimur Tabi /* CS4270 registers addresses */
47b0c813ceSTimur Tabi #define CS4270_CHIPID	0x01	/* Chip ID */
48b0c813ceSTimur Tabi #define CS4270_PWRCTL	0x02	/* Power Control */
49b0c813ceSTimur Tabi #define CS4270_MODE	0x03	/* Mode Control */
50b0c813ceSTimur Tabi #define CS4270_FORMAT	0x04	/* Serial Format, ADC/DAC Control */
51b0c813ceSTimur Tabi #define CS4270_TRANS	0x05	/* Transition Control */
52b0c813ceSTimur Tabi #define CS4270_MUTE	0x06	/* Mute Control */
53b0c813ceSTimur Tabi #define CS4270_VOLA	0x07	/* DAC Channel A Volume Control */
54b0c813ceSTimur Tabi #define CS4270_VOLB	0x08	/* DAC Channel B Volume Control */
55b0c813ceSTimur Tabi 
56b0c813ceSTimur Tabi #define CS4270_FIRSTREG	0x01
57b0c813ceSTimur Tabi #define CS4270_LASTREG	0x08
58b0c813ceSTimur Tabi #define CS4270_NUMREGS	(CS4270_LASTREG - CS4270_FIRSTREG + 1)
59*80ab8817SDaniel Mack #define CS4270_I2C_INCR	0x80
60b0c813ceSTimur Tabi 
61b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */
62b0c813ceSTimur Tabi #define CS4270_CHIPID_ID	0xF0
63b0c813ceSTimur Tabi #define CS4270_CHIPID_REV	0x0F
64b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE	0x80
65b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC	0x20
66b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC	0x02
67b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN	0x01
68b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK	0x30
69b0c813ceSTimur Tabi #define CS4270_MODE_1X		0x00
70b0c813ceSTimur Tabi #define CS4270_MODE_2X		0x10
71b0c813ceSTimur Tabi #define CS4270_MODE_4X		0x20
72b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE	0x30
73b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK	0x0E
74b0c813ceSTimur Tabi #define CS4270_MODE_DIV1	0x00
75b0c813ceSTimur Tabi #define CS4270_MODE_DIV15	0x02
76b0c813ceSTimur Tabi #define CS4270_MODE_DIV2	0x04
77b0c813ceSTimur Tabi #define CS4270_MODE_DIV3	0x06
78b0c813ceSTimur Tabi #define CS4270_MODE_DIV4	0x08
79b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD	0x01
80b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A	0x80
81b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B	0x40
82b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK	0x20
83b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK	0x18
84b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ	0x00
85b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S	0x08
86b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16	0x18
87b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24	0x10
88b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK	0x01
89b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ	0x00
90b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S	0x01
91b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL	0x80
92b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT	0x40
93b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO	0x20
94b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A	0x08
95b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B	0x10
96b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A	0x02
97b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B	0x04
98b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH	0x01
99b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO	0x20
100b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A	0x08
101b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B	0x10
102b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY	0x04
103b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A	0x01
104b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B	0x02
105b0c813ceSTimur Tabi 
1060db4d070STimur Tabi /* Private data for the CS4270 */
1070db4d070STimur Tabi struct cs4270_private {
1080db4d070STimur Tabi 	struct snd_soc_codec codec;
1090db4d070STimur Tabi 	u8 reg_cache[CS4270_NUMREGS];
1100db4d070STimur Tabi 	unsigned int mclk; /* Input frequency of the MCLK pin */
1110db4d070STimur Tabi 	unsigned int mode; /* The mode (I2S or left-justified) */
1124eae080dSDaniel Mack 	unsigned int slave_mode;
1131a4ba05eSDaniel Mack 	unsigned int manual_mute;
1140db4d070STimur Tabi };
1150db4d070STimur Tabi 
116ff7bf02fSTimur Tabi /**
117ff7bf02fSTimur Tabi  * struct cs4270_mode_ratios - clock ratio tables
118ff7bf02fSTimur Tabi  * @ratio: the ratio of MCLK to the sample rate
119ff7bf02fSTimur Tabi  * @speed_mode: the Speed Mode bits to set in the Mode Control register for
120ff7bf02fSTimur Tabi  *              this ratio
121ff7bf02fSTimur Tabi  * @mclk: the Ratio Select bits to set in the Mode Control register for this
122ff7bf02fSTimur Tabi  *        ratio
1238432395fSTimur Tabi  *
1248432395fSTimur Tabi  * The data for this chart is taken from Table 5 of the CS4270 reference
1258432395fSTimur Tabi  * manual.
1268432395fSTimur Tabi  *
1278432395fSTimur Tabi  * This table is used to determine how to program the Mode Control register.
1288432395fSTimur Tabi  * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
1298432395fSTimur Tabi  * rates the CS4270 currently supports.
1308432395fSTimur Tabi  *
131ff7bf02fSTimur Tabi  * @speed_mode is the corresponding bit pattern to be written to the
1328432395fSTimur Tabi  * MODE bits of the Mode Control Register
1338432395fSTimur Tabi  *
134ff7bf02fSTimur Tabi  * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
1358432395fSTimur Tabi  * the Mode Control Register.
1368432395fSTimur Tabi  *
1378432395fSTimur Tabi  * In situations where a single ratio is represented by multiple speed
1388432395fSTimur Tabi  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
1398432395fSTimur Tabi  * double-speed instead of quad-speed.  However, the CS4270 errata states
140ff7bf02fSTimur Tabi  * that divide-By-1.5 can cause failures, so we avoid that mode where
1418432395fSTimur Tabi  * possible.
1428432395fSTimur Tabi  *
143ff7bf02fSTimur Tabi  * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
144ff7bf02fSTimur Tabi  * work if Vd is 3.3V.  If this effects you, select the
1458432395fSTimur Tabi  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
1468432395fSTimur Tabi  * never select any sample rates that require divide-by-1.5.
1478432395fSTimur Tabi  */
148ff7bf02fSTimur Tabi struct cs4270_mode_ratios {
1498432395fSTimur Tabi 	unsigned int ratio;
1508432395fSTimur Tabi 	u8 speed_mode;
1518432395fSTimur Tabi 	u8 mclk;
152ff7bf02fSTimur Tabi };
153ff7bf02fSTimur Tabi 
154d9fb7fbdSTimur Tabi static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
1558432395fSTimur Tabi 	{64, CS4270_MODE_4X, CS4270_MODE_DIV1},
1568432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
1578432395fSTimur Tabi 	{96, CS4270_MODE_4X, CS4270_MODE_DIV15},
1588432395fSTimur Tabi #endif
1598432395fSTimur Tabi 	{128, CS4270_MODE_2X, CS4270_MODE_DIV1},
1608432395fSTimur Tabi 	{192, CS4270_MODE_4X, CS4270_MODE_DIV3},
1618432395fSTimur Tabi 	{256, CS4270_MODE_1X, CS4270_MODE_DIV1},
1628432395fSTimur Tabi 	{384, CS4270_MODE_2X, CS4270_MODE_DIV3},
1638432395fSTimur Tabi 	{512, CS4270_MODE_1X, CS4270_MODE_DIV2},
1648432395fSTimur Tabi 	{768, CS4270_MODE_1X, CS4270_MODE_DIV3},
1658432395fSTimur Tabi 	{1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
1668432395fSTimur Tabi };
1678432395fSTimur Tabi 
1688432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */
1698432395fSTimur Tabi #define NUM_MCLK_RATIOS		ARRAY_SIZE(cs4270_mode_ratios)
1708432395fSTimur Tabi 
171ff7bf02fSTimur Tabi /**
172ff7bf02fSTimur Tabi  * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
173ff7bf02fSTimur Tabi  * @codec_dai: the codec DAI
174ff7bf02fSTimur Tabi  * @clk_id: the clock ID (ignored)
175ff7bf02fSTimur Tabi  * @freq: the MCLK input frequency
176ff7bf02fSTimur Tabi  * @dir: the clock direction (ignored)
1778432395fSTimur Tabi  *
178ff7bf02fSTimur Tabi  * This function is used to tell the codec driver what the input MCLK
179ff7bf02fSTimur Tabi  * frequency is.
1808432395fSTimur Tabi  *
1818432395fSTimur Tabi  * The value of MCLK is used to determine which sample rates are supported
1828432395fSTimur Tabi  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
183ff7bf02fSTimur Tabi  * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
1848432395fSTimur Tabi  *
1858432395fSTimur Tabi  * This function calculates the nine ratios and determines which ones match
1868432395fSTimur Tabi  * a standard sample rate.  If there's a match, then it is added to the list
187ff7bf02fSTimur Tabi  * of supported sample rates.
1888432395fSTimur Tabi  *
1898432395fSTimur Tabi  * This function must be called by the machine driver's 'startup' function,
1908432395fSTimur Tabi  * otherwise the list of supported sample rates will not be available in
1918432395fSTimur Tabi  * time for ALSA.
1928432395fSTimur Tabi  */
193e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1948432395fSTimur Tabi 				 int clk_id, unsigned int freq, int dir)
1958432395fSTimur Tabi {
1968432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
1978432395fSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
1988432395fSTimur Tabi 	unsigned int rates = 0;
1998432395fSTimur Tabi 	unsigned int rate_min = -1;
2008432395fSTimur Tabi 	unsigned int rate_max = 0;
2018432395fSTimur Tabi 	unsigned int i;
2028432395fSTimur Tabi 
2038432395fSTimur Tabi 	cs4270->mclk = freq;
2048432395fSTimur Tabi 
2058432395fSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
2068432395fSTimur Tabi 		unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
2078432395fSTimur Tabi 		rates |= snd_pcm_rate_to_rate_bit(rate);
2088432395fSTimur Tabi 		if (rate < rate_min)
2098432395fSTimur Tabi 			rate_min = rate;
2108432395fSTimur Tabi 		if (rate > rate_max)
2118432395fSTimur Tabi 			rate_max = rate;
2128432395fSTimur Tabi 	}
2138432395fSTimur Tabi 	/* FIXME: soc should support a rate list */
2148432395fSTimur Tabi 	rates &= ~SNDRV_PCM_RATE_KNOT;
2158432395fSTimur Tabi 
2168432395fSTimur Tabi 	if (!rates) {
217a6c255e0STimur Tabi 		dev_err(codec->dev, "could not find a valid sample rate\n");
2188432395fSTimur Tabi 		return -EINVAL;
2198432395fSTimur Tabi 	}
2208432395fSTimur Tabi 
2218432395fSTimur Tabi 	codec_dai->playback.rates = rates;
2228432395fSTimur Tabi 	codec_dai->playback.rate_min = rate_min;
2238432395fSTimur Tabi 	codec_dai->playback.rate_max = rate_max;
2248432395fSTimur Tabi 
2258432395fSTimur Tabi 	codec_dai->capture.rates = rates;
2268432395fSTimur Tabi 	codec_dai->capture.rate_min = rate_min;
2278432395fSTimur Tabi 	codec_dai->capture.rate_max = rate_max;
2288432395fSTimur Tabi 
2298432395fSTimur Tabi 	return 0;
2308432395fSTimur Tabi }
2318432395fSTimur Tabi 
232ff7bf02fSTimur Tabi /**
233ff7bf02fSTimur Tabi  * cs4270_set_dai_fmt - configure the codec for the selected audio format
234ff7bf02fSTimur Tabi  * @codec_dai: the codec DAI
235ff7bf02fSTimur Tabi  * @format: a SND_SOC_DAIFMT_x value indicating the data format
2368432395fSTimur Tabi  *
2378432395fSTimur Tabi  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
2388432395fSTimur Tabi  * codec accordingly.
2398432395fSTimur Tabi  *
2408432395fSTimur Tabi  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
2418432395fSTimur Tabi  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
2428432395fSTimur Tabi  * data for playback only, but ASoC currently does not support different
2438432395fSTimur Tabi  * formats for playback vs. record.
2448432395fSTimur Tabi  */
245e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
2468432395fSTimur Tabi 			      unsigned int format)
2478432395fSTimur Tabi {
2488432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
2498432395fSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
2508432395fSTimur Tabi 	int ret = 0;
2518432395fSTimur Tabi 
2524eae080dSDaniel Mack 	/* set DAI format */
2538432395fSTimur Tabi 	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
2548432395fSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
2558432395fSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
2568432395fSTimur Tabi 		cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
2578432395fSTimur Tabi 		break;
2588432395fSTimur Tabi 	default:
259a6c255e0STimur Tabi 		dev_err(codec->dev, "invalid dai format\n");
2608432395fSTimur Tabi 		ret = -EINVAL;
2618432395fSTimur Tabi 	}
2628432395fSTimur Tabi 
2634eae080dSDaniel Mack 	/* set master/slave audio interface */
2644eae080dSDaniel Mack 	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
2654eae080dSDaniel Mack 	case SND_SOC_DAIFMT_CBS_CFS:
2664eae080dSDaniel Mack 		cs4270->slave_mode = 1;
2674eae080dSDaniel Mack 		break;
2684eae080dSDaniel Mack 	case SND_SOC_DAIFMT_CBM_CFM:
2694eae080dSDaniel Mack 		cs4270->slave_mode = 0;
2704eae080dSDaniel Mack 		break;
2714eae080dSDaniel Mack 	default:
272ff09d49aSDaniel Mack 		/* all other modes are unsupported by the hardware */
2734eae080dSDaniel Mack 		ret = -EINVAL;
2744eae080dSDaniel Mack 	}
2754eae080dSDaniel Mack 
2768432395fSTimur Tabi 	return ret;
2778432395fSTimur Tabi }
2788432395fSTimur Tabi 
279ff7bf02fSTimur Tabi /**
280ff7bf02fSTimur Tabi  * cs4270_fill_cache - pre-fill the CS4270 register cache.
281ff7bf02fSTimur Tabi  * @codec: the codec for this CS4270
282ff7bf02fSTimur Tabi  *
283ff7bf02fSTimur Tabi  * This function fills in the CS4270 register cache by reading the register
284ff7bf02fSTimur Tabi  * values from the hardware.
285ff7bf02fSTimur Tabi  *
286ff7bf02fSTimur Tabi  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
287ff7bf02fSTimur Tabi  * After the initial read to pre-fill the cache, the CS4270 never updates
288ff7bf02fSTimur Tabi  * the register values, so we won't have a cache coherency problem.
289b0c813ceSTimur Tabi  *
290b0c813ceSTimur Tabi  * We use the auto-increment feature of the CS4270 to read all registers in
291b0c813ceSTimur Tabi  * one shot.
292b0c813ceSTimur Tabi  */
293b0c813ceSTimur Tabi static int cs4270_fill_cache(struct snd_soc_codec *codec)
294b0c813ceSTimur Tabi {
295b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
296b0c813ceSTimur Tabi 	struct i2c_client *i2c_client = codec->control_data;
297b0c813ceSTimur Tabi 	s32 length;
298b0c813ceSTimur Tabi 
299b0c813ceSTimur Tabi 	length = i2c_smbus_read_i2c_block_data(i2c_client,
300*80ab8817SDaniel Mack 		CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache);
301b0c813ceSTimur Tabi 
302b0c813ceSTimur Tabi 	if (length != CS4270_NUMREGS) {
303a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
304b0c813ceSTimur Tabi 		       i2c_client->addr);
305b0c813ceSTimur Tabi 		return -EIO;
306b0c813ceSTimur Tabi 	}
307b0c813ceSTimur Tabi 
308b0c813ceSTimur Tabi 	return 0;
309b0c813ceSTimur Tabi }
310b0c813ceSTimur Tabi 
311ff7bf02fSTimur Tabi /**
312ff7bf02fSTimur Tabi  * cs4270_read_reg_cache - read from the CS4270 register cache.
313ff7bf02fSTimur Tabi  * @codec: the codec for this CS4270
314ff7bf02fSTimur Tabi  * @reg: the register to read
315ff7bf02fSTimur Tabi  *
316ff7bf02fSTimur Tabi  * This function returns the value for a given register.  It reads only from
317ff7bf02fSTimur Tabi  * the register cache, not the hardware itself.
318b0c813ceSTimur Tabi  *
319b0c813ceSTimur Tabi  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
320b0c813ceSTimur Tabi  * After the initial read to pre-fill the cache, the CS4270 never updates
321ff7bf02fSTimur Tabi  * the register values, so we won't have a cache coherency problem.
322b0c813ceSTimur Tabi  */
323b0c813ceSTimur Tabi static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
324b0c813ceSTimur Tabi 	unsigned int reg)
325b0c813ceSTimur Tabi {
326b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
327b0c813ceSTimur Tabi 
328b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
329b0c813ceSTimur Tabi 		return -EIO;
330b0c813ceSTimur Tabi 
331b0c813ceSTimur Tabi 	return cache[reg - CS4270_FIRSTREG];
332b0c813ceSTimur Tabi }
333b0c813ceSTimur Tabi 
334ff7bf02fSTimur Tabi /**
335ff7bf02fSTimur Tabi  * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
336ff7bf02fSTimur Tabi  * @codec: the codec for this CS4270
337ff7bf02fSTimur Tabi  * @reg: the register to write
338ff7bf02fSTimur Tabi  * @value: the value to write to the register
339b0c813ceSTimur Tabi  *
340b0c813ceSTimur Tabi  * This function writes the given value to the given CS4270 register, and
341b0c813ceSTimur Tabi  * also updates the register cache.
342b0c813ceSTimur Tabi  *
343b0c813ceSTimur Tabi  * Note that we don't use the hw_write function pointer of snd_soc_codec.
344b0c813ceSTimur Tabi  * That's because it's too clunky: the hw_write_t prototype does not match
345b0c813ceSTimur Tabi  * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
346b0c813ceSTimur Tabi  */
347b0c813ceSTimur Tabi static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
348b0c813ceSTimur Tabi 			    unsigned int value)
349b0c813ceSTimur Tabi {
350bfc4e861STimur Tabi 	u8 *cache = codec->reg_cache;
351bfc4e861STimur Tabi 
352b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
353b0c813ceSTimur Tabi 		return -EIO;
354b0c813ceSTimur Tabi 
355bfc4e861STimur Tabi 	/* Only perform an I2C operation if the new value is different */
356bfc4e861STimur Tabi 	if (cache[reg - CS4270_FIRSTREG] != value) {
357bfc4e861STimur Tabi 		struct i2c_client *client = codec->control_data;
358bfc4e861STimur Tabi 		if (i2c_smbus_write_byte_data(client, reg, value)) {
359a6c255e0STimur Tabi 			dev_err(codec->dev, "i2c write failed\n");
360b0c813ceSTimur Tabi 			return -EIO;
361b0c813ceSTimur Tabi 		}
362bfc4e861STimur Tabi 
363bfc4e861STimur Tabi 		/* We've written to the hardware, so update the cache */
364bfc4e861STimur Tabi 		cache[reg - CS4270_FIRSTREG] = value;
365bfc4e861STimur Tabi 	}
366bfc4e861STimur Tabi 
367bfc4e861STimur Tabi 	return 0;
368b0c813ceSTimur Tabi }
369b0c813ceSTimur Tabi 
370ff7bf02fSTimur Tabi /**
371ff7bf02fSTimur Tabi  * cs4270_hw_params - program the CS4270 with the given hardware parameters.
372ff7bf02fSTimur Tabi  * @substream: the audio stream
373ff7bf02fSTimur Tabi  * @params: the hardware parameters to set
374ff7bf02fSTimur Tabi  * @dai: the SOC DAI (ignored)
375b0c813ceSTimur Tabi  *
376ff7bf02fSTimur Tabi  * This function programs the hardware with the values provided.
377ff7bf02fSTimur Tabi  * Specifically, the sample rate and the data format.
378ff7bf02fSTimur Tabi  *
379ff7bf02fSTimur Tabi  * The .ops functions are used to provide board-specific data, like input
380ff7bf02fSTimur Tabi  * frequencies, to this driver.  This function takes that information,
381b0c813ceSTimur Tabi  * combines it with the hardware parameters provided, and programs the
382b0c813ceSTimur Tabi  * hardware accordingly.
383b0c813ceSTimur Tabi  */
384b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream,
385dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
386dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
387b0c813ceSTimur Tabi {
388b0c813ceSTimur Tabi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
389b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = rtd->socdev;
3906627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
391b0c813ceSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
392e34ba212SRoel Kluin 	int ret;
393b0c813ceSTimur Tabi 	unsigned int i;
394b0c813ceSTimur Tabi 	unsigned int rate;
395b0c813ceSTimur Tabi 	unsigned int ratio;
396b0c813ceSTimur Tabi 	int reg;
397b0c813ceSTimur Tabi 
398b0c813ceSTimur Tabi 	/* Figure out which MCLK/LRCK ratio to use */
399b0c813ceSTimur Tabi 
400b0c813ceSTimur Tabi 	rate = params_rate(params);	/* Sampling rate, in Hz */
401b0c813ceSTimur Tabi 	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */
402b0c813ceSTimur Tabi 
4039dbd627bSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
4048432395fSTimur Tabi 		if (cs4270_mode_ratios[i].ratio == ratio)
405b0c813ceSTimur Tabi 			break;
406b0c813ceSTimur Tabi 	}
407b0c813ceSTimur Tabi 
4089dbd627bSTimur Tabi 	if (i == NUM_MCLK_RATIOS) {
409b0c813ceSTimur Tabi 		/* We did not find a matching ratio */
410a6c255e0STimur Tabi 		dev_err(codec->dev, "could not find matching ratio\n");
411b0c813ceSTimur Tabi 		return -EINVAL;
412b0c813ceSTimur Tabi 	}
413b0c813ceSTimur Tabi 
414d5e9ba1dSTimur Tabi 	/* Set the sample rate */
415b0c813ceSTimur Tabi 
416b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MODE);
417b0c813ceSTimur Tabi 	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
4184eae080dSDaniel Mack 	reg |= cs4270_mode_ratios[i].mclk;
4194eae080dSDaniel Mack 
4204eae080dSDaniel Mack 	if (cs4270->slave_mode)
4214eae080dSDaniel Mack 		reg |= CS4270_MODE_SLAVE;
4224eae080dSDaniel Mack 	else
4234eae080dSDaniel Mack 		reg |= cs4270_mode_ratios[i].speed_mode;
424b0c813ceSTimur Tabi 
425b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MODE, reg);
426b0c813ceSTimur Tabi 	if (ret < 0) {
427a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c write failed\n");
428b0c813ceSTimur Tabi 		return ret;
429b0c813ceSTimur Tabi 	}
430b0c813ceSTimur Tabi 
431d5e9ba1dSTimur Tabi 	/* Set the DAI format */
432b0c813ceSTimur Tabi 
433b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_FORMAT);
434b0c813ceSTimur Tabi 	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
435b0c813ceSTimur Tabi 
436b0c813ceSTimur Tabi 	switch (cs4270->mode) {
437b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
438b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
439b0c813ceSTimur Tabi 		break;
440b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
441b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
442b0c813ceSTimur Tabi 		break;
443b0c813ceSTimur Tabi 	default:
444a6c255e0STimur Tabi 		dev_err(codec->dev, "unknown dai format\n");
445b0c813ceSTimur Tabi 		return -EINVAL;
446b0c813ceSTimur Tabi 	}
447b0c813ceSTimur Tabi 
448b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_FORMAT, reg);
449b0c813ceSTimur Tabi 	if (ret < 0) {
450a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c write failed\n");
451b0c813ceSTimur Tabi 		return ret;
452b0c813ceSTimur Tabi 	}
453b0c813ceSTimur Tabi 
454b0c813ceSTimur Tabi 	return ret;
455b0c813ceSTimur Tabi }
456b0c813ceSTimur Tabi 
457ff7bf02fSTimur Tabi /**
4581a4ba05eSDaniel Mack  * cs4270_dai_mute - enable/disable the CS4270 external mute
459ff7bf02fSTimur Tabi  * @dai: the SOC DAI
460ff7bf02fSTimur Tabi  * @mute: 0 = disable mute, 1 = enable mute
461b0c813ceSTimur Tabi  *
462b0c813ceSTimur Tabi  * This function toggles the mute bits in the MUTE register.  The CS4270's
463b0c813ceSTimur Tabi  * mute capability is intended for external muting circuitry, so if the
464b0c813ceSTimur Tabi  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
465b0c813ceSTimur Tabi  * then this function will do nothing.
466b0c813ceSTimur Tabi  */
4671a4ba05eSDaniel Mack static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
468b0c813ceSTimur Tabi {
469b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = dai->codec;
4701a4ba05eSDaniel Mack 	struct cs4270_private *cs4270 = codec->private_data;
471b0c813ceSTimur Tabi 	int reg6;
472b0c813ceSTimur Tabi 
473b0c813ceSTimur Tabi 	reg6 = snd_soc_read(codec, CS4270_MUTE);
474b0c813ceSTimur Tabi 
475b0c813ceSTimur Tabi 	if (mute)
476d5e9ba1dSTimur Tabi 		reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
4771a4ba05eSDaniel Mack 	else {
478d5e9ba1dSTimur Tabi 		reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
4791a4ba05eSDaniel Mack 		reg6 |= cs4270->manual_mute;
4801a4ba05eSDaniel Mack 	}
481b0c813ceSTimur Tabi 
482b0c813ceSTimur Tabi 	return snd_soc_write(codec, CS4270_MUTE, reg6);
483b0c813ceSTimur Tabi }
484b0c813ceSTimur Tabi 
4851a4ba05eSDaniel Mack /**
4861a4ba05eSDaniel Mack  * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
4871a4ba05eSDaniel Mack  * 			 alsa control.
4881a4ba05eSDaniel Mack  * @kcontrol: mixer control
4891a4ba05eSDaniel Mack  * @ucontrol: control element information
4901a4ba05eSDaniel Mack  *
4911a4ba05eSDaniel Mack  * This function basically passes the arguments on to the generic
4921a4ba05eSDaniel Mack  * snd_soc_put_volsw() function and saves the mute information in
4931a4ba05eSDaniel Mack  * our private data structure. This is because we want to prevent
4941a4ba05eSDaniel Mack  * cs4270_dai_mute() neglecting the user's decision to manually
4951a4ba05eSDaniel Mack  * mute the codec's output.
4961a4ba05eSDaniel Mack  *
4971a4ba05eSDaniel Mack  * Returns 0 for success.
4981a4ba05eSDaniel Mack  */
4991a4ba05eSDaniel Mack static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
5001a4ba05eSDaniel Mack 				struct snd_ctl_elem_value *ucontrol)
5011a4ba05eSDaniel Mack {
5021a4ba05eSDaniel Mack 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
5031a4ba05eSDaniel Mack 	struct cs4270_private *cs4270 = codec->private_data;
5041a4ba05eSDaniel Mack 	int left = !ucontrol->value.integer.value[0];
5051a4ba05eSDaniel Mack 	int right = !ucontrol->value.integer.value[1];
5061a4ba05eSDaniel Mack 
5071a4ba05eSDaniel Mack 	cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
5081a4ba05eSDaniel Mack 			      (right ? CS4270_MUTE_DAC_B : 0);
5091a4ba05eSDaniel Mack 
5101a4ba05eSDaniel Mack 	return snd_soc_put_volsw(kcontrol, ucontrol);
5111a4ba05eSDaniel Mack }
5121a4ba05eSDaniel Mack 
513b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */
514b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = {
515b0c813ceSTimur Tabi 	SOC_DOUBLE_R("Master Playback Volume",
516d5e9ba1dSTimur Tabi 		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
517d5e9ba1dSTimur Tabi 	SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
518d5e9ba1dSTimur Tabi 	SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
519d5e9ba1dSTimur Tabi 	SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
520d5e9ba1dSTimur Tabi 	SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
521d5e9ba1dSTimur Tabi 	SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
5221a4ba05eSDaniel Mack 	SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
5231a4ba05eSDaniel Mack 	SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
5241a4ba05eSDaniel Mack 		snd_soc_get_volsw, cs4270_soc_put_mute),
525b0c813ceSTimur Tabi };
526b0c813ceSTimur Tabi 
527b0c813ceSTimur Tabi /*
528ff7bf02fSTimur Tabi  * cs4270_codec - global variable to store codec for the ASoC probe function
529b0c813ceSTimur Tabi  *
530b0c813ceSTimur Tabi  * If struct i2c_driver had a private_data field, we wouldn't need to use
53104eb093cSTimur Tabi  * cs4270_codec.  This is the only way to pass the codec structure from
53204eb093cSTimur Tabi  * cs4270_i2c_probe() to cs4270_probe().  Unfortunately, there is no good
53304eb093cSTimur Tabi  * way to synchronize these two functions.  cs4270_i2c_probe() can be called
53404eb093cSTimur Tabi  * multiple times before cs4270_probe() is called even once.  So for now, we
53504eb093cSTimur Tabi  * also only allow cs4270_i2c_probe() to be run once.  That means that we do
53604eb093cSTimur Tabi  * not support more than one cs4270 device in the system, at least for now.
537b0c813ceSTimur Tabi  */
53804eb093cSTimur Tabi static struct snd_soc_codec *cs4270_codec;
539b0c813ceSTimur Tabi 
5406335d055SEric Miao static struct snd_soc_dai_ops cs4270_dai_ops = {
5416335d055SEric Miao 	.hw_params	= cs4270_hw_params,
5426335d055SEric Miao 	.set_sysclk	= cs4270_set_dai_sysclk,
5436335d055SEric Miao 	.set_fmt	= cs4270_set_dai_fmt,
5441a4ba05eSDaniel Mack 	.digital_mute	= cs4270_dai_mute,
5456335d055SEric Miao };
5466335d055SEric Miao 
547e550e17fSLiam Girdwood struct snd_soc_dai cs4270_dai = {
5480db4d070STimur Tabi 	.name = "cs4270",
549b0c813ceSTimur Tabi 	.playback = {
550b0c813ceSTimur Tabi 		.stream_name = "Playback",
551b0c813ceSTimur Tabi 		.channels_min = 1,
552b0c813ceSTimur Tabi 		.channels_max = 2,
553b0c813ceSTimur Tabi 		.rates = 0,
554b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
555b0c813ceSTimur Tabi 	},
556b0c813ceSTimur Tabi 	.capture = {
557b0c813ceSTimur Tabi 		.stream_name = "Capture",
558b0c813ceSTimur Tabi 		.channels_min = 1,
559b0c813ceSTimur Tabi 		.channels_max = 2,
560b0c813ceSTimur Tabi 		.rates = 0,
561b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
562b0c813ceSTimur Tabi 	},
5636335d055SEric Miao 	.ops = &cs4270_dai_ops,
564b0c813ceSTimur Tabi };
565b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(cs4270_dai);
566b0c813ceSTimur Tabi 
567ff7bf02fSTimur Tabi /**
568ff7bf02fSTimur Tabi  * cs4270_probe - ASoC probe function
569ff7bf02fSTimur Tabi  * @pdev: platform device
570ff7bf02fSTimur Tabi  *
571ff7bf02fSTimur Tabi  * This function is called when ASoC has all the pieces it needs to
572ff7bf02fSTimur Tabi  * instantiate a sound driver.
57304eb093cSTimur Tabi  */
57404eb093cSTimur Tabi static int cs4270_probe(struct platform_device *pdev)
57504eb093cSTimur Tabi {
57604eb093cSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
57704eb093cSTimur Tabi 	struct snd_soc_codec *codec = cs4270_codec;
57804eb093cSTimur Tabi 	int ret;
57904eb093cSTimur Tabi 
58004eb093cSTimur Tabi 	/* Connect the codec to the socdev.  snd_soc_new_pcms() needs this. */
58104eb093cSTimur Tabi 	socdev->card->codec = codec;
58204eb093cSTimur Tabi 
58304eb093cSTimur Tabi 	/* Register PCMs */
58404eb093cSTimur Tabi 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
58504eb093cSTimur Tabi 	if (ret < 0) {
586a6c255e0STimur Tabi 		dev_err(codec->dev, "failed to create pcms\n");
58704eb093cSTimur Tabi 		return ret;
58804eb093cSTimur Tabi 	}
58904eb093cSTimur Tabi 
59004eb093cSTimur Tabi 	/* Add the non-DAPM controls */
591eb5f6d75SPhilipp Zabel 	ret = snd_soc_add_controls(codec, cs4270_snd_controls,
592eb5f6d75SPhilipp Zabel 				ARRAY_SIZE(cs4270_snd_controls));
59304eb093cSTimur Tabi 	if (ret < 0) {
594eb5f6d75SPhilipp Zabel 		dev_err(codec->dev, "failed to add controls\n");
59504eb093cSTimur Tabi 		goto error_free_pcms;
59604eb093cSTimur Tabi 	}
59704eb093cSTimur Tabi 
59804eb093cSTimur Tabi 	/* And finally, register the socdev */
59904eb093cSTimur Tabi 	ret = snd_soc_init_card(socdev);
60004eb093cSTimur Tabi 	if (ret < 0) {
601a6c255e0STimur Tabi 		dev_err(codec->dev, "failed to register card\n");
60204eb093cSTimur Tabi 		goto error_free_pcms;
60304eb093cSTimur Tabi 	}
60404eb093cSTimur Tabi 
60504eb093cSTimur Tabi 	return 0;
60604eb093cSTimur Tabi 
60704eb093cSTimur Tabi error_free_pcms:
60804eb093cSTimur Tabi 	snd_soc_free_pcms(socdev);
60904eb093cSTimur Tabi 
61004eb093cSTimur Tabi 	return ret;
61104eb093cSTimur Tabi }
61204eb093cSTimur Tabi 
613ff7bf02fSTimur Tabi /**
614ff7bf02fSTimur Tabi  * cs4270_remove - ASoC remove function
615ff7bf02fSTimur Tabi  * @pdev: platform device
616ff7bf02fSTimur Tabi  *
617ff7bf02fSTimur Tabi  * This function is the counterpart to cs4270_probe().
618ff7bf02fSTimur Tabi  */
61904eb093cSTimur Tabi static int cs4270_remove(struct platform_device *pdev)
62004eb093cSTimur Tabi {
62104eb093cSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
62204eb093cSTimur Tabi 
62304eb093cSTimur Tabi 	snd_soc_free_pcms(socdev);
62404eb093cSTimur Tabi 
62504eb093cSTimur Tabi 	return 0;
62604eb093cSTimur Tabi };
62704eb093cSTimur Tabi 
628ff7bf02fSTimur Tabi /**
629ff7bf02fSTimur Tabi  * cs4270_i2c_probe - initialize the I2C interface of the CS4270
630ff7bf02fSTimur Tabi  * @i2c_client: the I2C client object
631ff7bf02fSTimur Tabi  * @id: the I2C device ID (ignored)
632b0c813ceSTimur Tabi  *
633ff7bf02fSTimur Tabi  * This function is called whenever the I2C subsystem finds a device that
634ff7bf02fSTimur Tabi  * matches the device ID given via a prior call to i2c_add_driver().
635b0c813ceSTimur Tabi  */
6360db4d070STimur Tabi static int cs4270_i2c_probe(struct i2c_client *i2c_client,
6370db4d070STimur Tabi 	const struct i2c_device_id *id)
638b0c813ceSTimur Tabi {
639b0c813ceSTimur Tabi 	struct snd_soc_codec *codec;
6400db4d070STimur Tabi 	struct cs4270_private *cs4270;
641d5e9ba1dSTimur Tabi 	unsigned int reg;
64204eb093cSTimur Tabi 	int ret;
64304eb093cSTimur Tabi 
64404eb093cSTimur Tabi 	/* For now, we only support one cs4270 device in the system.  See the
64504eb093cSTimur Tabi 	 * comment for cs4270_codec.
64604eb093cSTimur Tabi 	 */
64704eb093cSTimur Tabi 	if (cs4270_codec) {
648a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
64904eb093cSTimur Tabi 		       i2c_client->addr);
650a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "only one per board allowed\n");
65104eb093cSTimur Tabi 		/* Should we return something other than ENODEV here? */
65204eb093cSTimur Tabi 		return -ENODEV;
65304eb093cSTimur Tabi 	}
654b0c813ceSTimur Tabi 
6550db4d070STimur Tabi 	/* Verify that we have a CS4270 */
6560db4d070STimur Tabi 
6570db4d070STimur Tabi 	ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
6580db4d070STimur Tabi 	if (ret < 0) {
659a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
66004eb093cSTimur Tabi 		       i2c_client->addr);
6610db4d070STimur Tabi 		return ret;
6620db4d070STimur Tabi 	}
6630db4d070STimur Tabi 	/* The top four bits of the chip ID should be 1100. */
6640db4d070STimur Tabi 	if ((ret & 0xF0) != 0xC0) {
665a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
6660db4d070STimur Tabi 		       i2c_client->addr);
6670db4d070STimur Tabi 		return -ENODEV;
6680db4d070STimur Tabi 	}
6690db4d070STimur Tabi 
670a6c255e0STimur Tabi 	dev_info(&i2c_client->dev, "found device at i2c address %X\n",
6710db4d070STimur Tabi 		i2c_client->addr);
672a6c255e0STimur Tabi 	dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
673b0c813ceSTimur Tabi 
674b0c813ceSTimur Tabi 	/* Allocate enough space for the snd_soc_codec structure
675b0c813ceSTimur Tabi 	   and our private data together. */
6760db4d070STimur Tabi 	cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
6770db4d070STimur Tabi 	if (!cs4270) {
678a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "could not allocate codec\n");
679b0c813ceSTimur Tabi 		return -ENOMEM;
680b0c813ceSTimur Tabi 	}
6810db4d070STimur Tabi 	codec = &cs4270->codec;
682b0c813ceSTimur Tabi 
683b0c813ceSTimur Tabi 	mutex_init(&codec->mutex);
684b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_widgets);
685b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_paths);
686b0c813ceSTimur Tabi 
687a6c255e0STimur Tabi 	codec->dev = &i2c_client->dev;
688b0c813ceSTimur Tabi 	codec->name = "CS4270";
689b0c813ceSTimur Tabi 	codec->owner = THIS_MODULE;
690b0c813ceSTimur Tabi 	codec->dai = &cs4270_dai;
691b0c813ceSTimur Tabi 	codec->num_dai = 1;
6920db4d070STimur Tabi 	codec->private_data = cs4270;
6930db4d070STimur Tabi 	codec->control_data = i2c_client;
6940db4d070STimur Tabi 	codec->read = cs4270_read_reg_cache;
6950db4d070STimur Tabi 	codec->write = cs4270_i2c_write;
6960db4d070STimur Tabi 	codec->reg_cache = cs4270->reg_cache;
6970db4d070STimur Tabi 	codec->reg_cache_size = CS4270_NUMREGS;
698b0c813ceSTimur Tabi 
6990db4d070STimur Tabi 	/* The I2C interface is set up, so pre-fill our register cache */
7000db4d070STimur Tabi 
7010db4d070STimur Tabi 	ret = cs4270_fill_cache(codec);
7020db4d070STimur Tabi 	if (ret < 0) {
703a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "failed to fill register cache\n");
7040db4d070STimur Tabi 		goto error_free_codec;
7050db4d070STimur Tabi 	}
706b0c813ceSTimur Tabi 
707d5e9ba1dSTimur Tabi 	/* Disable auto-mute.  This feature appears to be buggy.  In some
708d5e9ba1dSTimur Tabi 	 * situations, auto-mute will not deactivate when it should, so we want
709d5e9ba1dSTimur Tabi 	 * this feature disabled by default.  An application (e.g. alsactl) can
710d5e9ba1dSTimur Tabi 	 * re-enabled it by using the controls.
711d5e9ba1dSTimur Tabi 	 */
712d5e9ba1dSTimur Tabi 
713d5e9ba1dSTimur Tabi 	reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
714d5e9ba1dSTimur Tabi 	reg &= ~CS4270_MUTE_AUTO;
715d5e9ba1dSTimur Tabi 	ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
716d5e9ba1dSTimur Tabi 	if (ret < 0) {
717d5e9ba1dSTimur Tabi 		dev_err(&i2c_client->dev, "i2c write failed\n");
718d5e9ba1dSTimur Tabi 		return ret;
719d5e9ba1dSTimur Tabi 	}
720d5e9ba1dSTimur Tabi 
721d5e9ba1dSTimur Tabi 	/* Disable automatic volume control.  The hardware enables, and it
722d5e9ba1dSTimur Tabi 	 * causes volume change commands to be delayed, sometimes until after
723d5e9ba1dSTimur Tabi 	 * playback has started.  An application (e.g. alsactl) can
724d5e9ba1dSTimur Tabi 	 * re-enabled it by using the controls.
725d5e9ba1dSTimur Tabi 	 */
726d5e9ba1dSTimur Tabi 
727d5e9ba1dSTimur Tabi 	reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
728d5e9ba1dSTimur Tabi 	reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
729d5e9ba1dSTimur Tabi 	ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
730d5e9ba1dSTimur Tabi 	if (ret < 0) {
731d5e9ba1dSTimur Tabi 		dev_err(&i2c_client->dev, "i2c write failed\n");
732d5e9ba1dSTimur Tabi 		return ret;
733d5e9ba1dSTimur Tabi 	}
734d5e9ba1dSTimur Tabi 
735a6c255e0STimur Tabi 	/* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
736a6c255e0STimur Tabi 	 * structure for each CS4270 device, but the machine driver needs to
737a6c255e0STimur Tabi 	 * have a pointer to the DAI structure, so for now it must be a global
738a6c255e0STimur Tabi 	 * variable.
739a6c255e0STimur Tabi 	 */
740a6c255e0STimur Tabi 	cs4270_dai.dev = &i2c_client->dev;
741a6c255e0STimur Tabi 
74204eb093cSTimur Tabi 	/* Register the DAI.  If all the other ASoC driver have already
74304eb093cSTimur Tabi 	 * registered, then this will call our probe function, so
74404eb093cSTimur Tabi 	 * cs4270_codec needs to be ready.
74504eb093cSTimur Tabi 	 */
746a6c255e0STimur Tabi 	cs4270_codec = codec;
74704eb093cSTimur Tabi 	ret = snd_soc_register_dai(&cs4270_dai);
748b0c813ceSTimur Tabi 	if (ret < 0) {
749a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "failed to register DAIe\n");
750e3145dfbSJean Delvare 		goto error_free_codec;
751b0c813ceSTimur Tabi 	}
752b0c813ceSTimur Tabi 
75304eb093cSTimur Tabi 	i2c_set_clientdata(i2c_client, cs4270);
754e3145dfbSJean Delvare 
7550db4d070STimur Tabi 	return 0;
756e3145dfbSJean Delvare 
757e3145dfbSJean Delvare error_free_codec:
7580db4d070STimur Tabi 	kfree(cs4270);
759a6c255e0STimur Tabi 	cs4270_codec = NULL;
760a6c255e0STimur Tabi 	cs4270_dai.dev = NULL;
761e3145dfbSJean Delvare 
762b0c813ceSTimur Tabi 	return ret;
763b0c813ceSTimur Tabi }
764b0c813ceSTimur Tabi 
765ff7bf02fSTimur Tabi /**
766ff7bf02fSTimur Tabi  * cs4270_i2c_remove - remove an I2C device
767ff7bf02fSTimur Tabi  * @i2c_client: the I2C client object
768ff7bf02fSTimur Tabi  *
769ff7bf02fSTimur Tabi  * This function is the counterpart to cs4270_i2c_probe().
770ff7bf02fSTimur Tabi  */
7710db4d070STimur Tabi static int cs4270_i2c_remove(struct i2c_client *i2c_client)
772b0c813ceSTimur Tabi {
77304eb093cSTimur Tabi 	struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
774b0c813ceSTimur Tabi 
7750db4d070STimur Tabi 	kfree(cs4270);
776a6c255e0STimur Tabi 	cs4270_codec = NULL;
777a6c255e0STimur Tabi 	cs4270_dai.dev = NULL;
778b0c813ceSTimur Tabi 
7790db4d070STimur Tabi 	return 0;
7800db4d070STimur Tabi }
7810db4d070STimur Tabi 
782ff7bf02fSTimur Tabi /*
783ff7bf02fSTimur Tabi  * cs4270_id - I2C device IDs supported by this driver
784ff7bf02fSTimur Tabi  */
7850db4d070STimur Tabi static struct i2c_device_id cs4270_id[] = {
7860db4d070STimur Tabi 	{"cs4270", 0},
7870db4d070STimur Tabi 	{}
7880db4d070STimur Tabi };
7890db4d070STimur Tabi MODULE_DEVICE_TABLE(i2c, cs4270_id);
7900db4d070STimur Tabi 
791ff7bf02fSTimur Tabi /*
792ff7bf02fSTimur Tabi  * cs4270_i2c_driver - I2C device identification
793ff7bf02fSTimur Tabi  *
794ff7bf02fSTimur Tabi  * This structure tells the I2C subsystem how to identify and support a
795ff7bf02fSTimur Tabi  * given I2C device type.
796ff7bf02fSTimur Tabi  */
7970db4d070STimur Tabi static struct i2c_driver cs4270_i2c_driver = {
7980db4d070STimur Tabi 	.driver = {
7990db4d070STimur Tabi 		.name = "cs4270",
8000db4d070STimur Tabi 		.owner = THIS_MODULE,
8010db4d070STimur Tabi 	},
8020db4d070STimur Tabi 	.id_table = cs4270_id,
8030db4d070STimur Tabi 	.probe = cs4270_i2c_probe,
8040db4d070STimur Tabi 	.remove = cs4270_i2c_remove,
8050db4d070STimur Tabi };
8060db4d070STimur Tabi 
8070db4d070STimur Tabi /*
808b0c813ceSTimur Tabi  * ASoC codec device structure
809b0c813ceSTimur Tabi  *
810b0c813ceSTimur Tabi  * Assign this variable to the codec_dev field of the machine driver's
811b0c813ceSTimur Tabi  * snd_soc_device structure.
812b0c813ceSTimur Tabi  */
813b0c813ceSTimur Tabi struct snd_soc_codec_device soc_codec_device_cs4270 = {
814b0c813ceSTimur Tabi 	.probe = 	cs4270_probe,
815b0c813ceSTimur Tabi 	.remove = 	cs4270_remove
816b0c813ceSTimur Tabi };
817b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
818b0c813ceSTimur Tabi 
819c9b3a40fSTakashi Iwai static int __init cs4270_init(void)
82064089b84SMark Brown {
821a6c255e0STimur Tabi 	pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
8220db4d070STimur Tabi 
82304eb093cSTimur Tabi 	return i2c_add_driver(&cs4270_i2c_driver);
82464089b84SMark Brown }
82564089b84SMark Brown module_init(cs4270_init);
82664089b84SMark Brown 
82764089b84SMark Brown static void __exit cs4270_exit(void)
82864089b84SMark Brown {
82904eb093cSTimur Tabi 	i2c_del_driver(&cs4270_i2c_driver);
83064089b84SMark Brown }
83164089b84SMark Brown module_exit(cs4270_exit);
83264089b84SMark Brown 
833b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
834b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
835b0c813ceSTimur Tabi MODULE_LICENSE("GPL");
836