xref: /linux/sound/soc/codecs/cs4270.c (revision 7e1aa1dcd0d886df72586e3a94b1a7382952f21f)
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
215e7c0344SDaniel Mack  * - Power management is 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>
305e7c0344SDaniel Mack #include <linux/delay.h>
31b0c813ceSTimur Tabi 
3201e097d6SMark Brown #include "cs4270.h"
3301e097d6SMark Brown 
34b0c813ceSTimur Tabi /*
35b0c813ceSTimur Tabi  * The codec isn't really big-endian or little-endian, since the I2S
36b0c813ceSTimur Tabi  * interface requires data to be sent serially with the MSbit first.
37b0c813ceSTimur Tabi  * However, to support BE and LE I2S devices, we specify both here.  That
38b0c813ceSTimur Tabi  * way, ALSA will always match the bit patterns.
39b0c813ceSTimur Tabi  */
40b0c813ceSTimur Tabi #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | \
41b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
42b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
43b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
44b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
45b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
46b0c813ceSTimur Tabi 
47b0c813ceSTimur Tabi /* CS4270 registers addresses */
48b0c813ceSTimur Tabi #define CS4270_CHIPID	0x01	/* Chip ID */
49b0c813ceSTimur Tabi #define CS4270_PWRCTL	0x02	/* Power Control */
50b0c813ceSTimur Tabi #define CS4270_MODE	0x03	/* Mode Control */
51b0c813ceSTimur Tabi #define CS4270_FORMAT	0x04	/* Serial Format, ADC/DAC Control */
52b0c813ceSTimur Tabi #define CS4270_TRANS	0x05	/* Transition Control */
53b0c813ceSTimur Tabi #define CS4270_MUTE	0x06	/* Mute Control */
54b0c813ceSTimur Tabi #define CS4270_VOLA	0x07	/* DAC Channel A Volume Control */
55b0c813ceSTimur Tabi #define CS4270_VOLB	0x08	/* DAC Channel B Volume Control */
56b0c813ceSTimur Tabi 
57b0c813ceSTimur Tabi #define CS4270_FIRSTREG	0x01
58b0c813ceSTimur Tabi #define CS4270_LASTREG	0x08
59b0c813ceSTimur Tabi #define CS4270_NUMREGS	(CS4270_LASTREG - CS4270_FIRSTREG + 1)
6080ab8817SDaniel Mack #define CS4270_I2C_INCR	0x80
61b0c813ceSTimur Tabi 
62b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */
63b0c813ceSTimur Tabi #define CS4270_CHIPID_ID	0xF0
64b0c813ceSTimur Tabi #define CS4270_CHIPID_REV	0x0F
65b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE	0x80
66b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC	0x20
67b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC	0x02
68b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN	0x01
695e7c0344SDaniel Mack #define CS4270_PWRCTL_PDN_ALL	\
705e7c0344SDaniel Mack 	(CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
71b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK	0x30
72b0c813ceSTimur Tabi #define CS4270_MODE_1X		0x00
73b0c813ceSTimur Tabi #define CS4270_MODE_2X		0x10
74b0c813ceSTimur Tabi #define CS4270_MODE_4X		0x20
75b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE	0x30
76b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK	0x0E
77b0c813ceSTimur Tabi #define CS4270_MODE_DIV1	0x00
78b0c813ceSTimur Tabi #define CS4270_MODE_DIV15	0x02
79b0c813ceSTimur Tabi #define CS4270_MODE_DIV2	0x04
80b0c813ceSTimur Tabi #define CS4270_MODE_DIV3	0x06
81b0c813ceSTimur Tabi #define CS4270_MODE_DIV4	0x08
82b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD	0x01
83b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A	0x80
84b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B	0x40
85b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK	0x20
86b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK	0x18
87b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ	0x00
88b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S	0x08
89b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16	0x18
90b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24	0x10
91b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK	0x01
92b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ	0x00
93b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S	0x01
94b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL	0x80
95b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT	0x40
96b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO	0x20
97b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A	0x08
98b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B	0x10
99b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A	0x02
100b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B	0x04
101b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH	0x01
102b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO	0x20
103b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A	0x08
104b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B	0x10
105b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY	0x04
106b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A	0x01
107b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B	0x02
108b0c813ceSTimur Tabi 
1090db4d070STimur Tabi /* Private data for the CS4270 */
1100db4d070STimur Tabi struct cs4270_private {
1110db4d070STimur Tabi 	struct snd_soc_codec codec;
1120db4d070STimur Tabi 	u8 reg_cache[CS4270_NUMREGS];
1130db4d070STimur Tabi 	unsigned int mclk; /* Input frequency of the MCLK pin */
1140db4d070STimur Tabi 	unsigned int mode; /* The mode (I2S or left-justified) */
1154eae080dSDaniel Mack 	unsigned int slave_mode;
1161a4ba05eSDaniel Mack 	unsigned int manual_mute;
1170db4d070STimur Tabi };
1180db4d070STimur Tabi 
119ff7bf02fSTimur Tabi /**
120ff7bf02fSTimur Tabi  * struct cs4270_mode_ratios - clock ratio tables
121ff7bf02fSTimur Tabi  * @ratio: the ratio of MCLK to the sample rate
122ff7bf02fSTimur Tabi  * @speed_mode: the Speed Mode bits to set in the Mode Control register for
123ff7bf02fSTimur Tabi  *              this ratio
124ff7bf02fSTimur Tabi  * @mclk: the Ratio Select bits to set in the Mode Control register for this
125ff7bf02fSTimur Tabi  *        ratio
1268432395fSTimur Tabi  *
1278432395fSTimur Tabi  * The data for this chart is taken from Table 5 of the CS4270 reference
1288432395fSTimur Tabi  * manual.
1298432395fSTimur Tabi  *
1308432395fSTimur Tabi  * This table is used to determine how to program the Mode Control register.
1318432395fSTimur Tabi  * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
1328432395fSTimur Tabi  * rates the CS4270 currently supports.
1338432395fSTimur Tabi  *
134ff7bf02fSTimur Tabi  * @speed_mode is the corresponding bit pattern to be written to the
1358432395fSTimur Tabi  * MODE bits of the Mode Control Register
1368432395fSTimur Tabi  *
137ff7bf02fSTimur Tabi  * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
1388432395fSTimur Tabi  * the Mode Control Register.
1398432395fSTimur Tabi  *
1408432395fSTimur Tabi  * In situations where a single ratio is represented by multiple speed
1418432395fSTimur Tabi  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
1428432395fSTimur Tabi  * double-speed instead of quad-speed.  However, the CS4270 errata states
143ff7bf02fSTimur Tabi  * that divide-By-1.5 can cause failures, so we avoid that mode where
1448432395fSTimur Tabi  * possible.
1458432395fSTimur Tabi  *
146ff7bf02fSTimur Tabi  * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
147ff7bf02fSTimur Tabi  * work if Vd is 3.3V.  If this effects you, select the
1488432395fSTimur Tabi  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
1498432395fSTimur Tabi  * never select any sample rates that require divide-by-1.5.
1508432395fSTimur Tabi  */
151ff7bf02fSTimur Tabi struct cs4270_mode_ratios {
1528432395fSTimur Tabi 	unsigned int ratio;
1538432395fSTimur Tabi 	u8 speed_mode;
1548432395fSTimur Tabi 	u8 mclk;
155ff7bf02fSTimur Tabi };
156ff7bf02fSTimur Tabi 
157d9fb7fbdSTimur Tabi static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
1588432395fSTimur Tabi 	{64, CS4270_MODE_4X, CS4270_MODE_DIV1},
1598432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
1608432395fSTimur Tabi 	{96, CS4270_MODE_4X, CS4270_MODE_DIV15},
1618432395fSTimur Tabi #endif
1628432395fSTimur Tabi 	{128, CS4270_MODE_2X, CS4270_MODE_DIV1},
1638432395fSTimur Tabi 	{192, CS4270_MODE_4X, CS4270_MODE_DIV3},
1648432395fSTimur Tabi 	{256, CS4270_MODE_1X, CS4270_MODE_DIV1},
1658432395fSTimur Tabi 	{384, CS4270_MODE_2X, CS4270_MODE_DIV3},
1668432395fSTimur Tabi 	{512, CS4270_MODE_1X, CS4270_MODE_DIV2},
1678432395fSTimur Tabi 	{768, CS4270_MODE_1X, CS4270_MODE_DIV3},
1688432395fSTimur Tabi 	{1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
1698432395fSTimur Tabi };
1708432395fSTimur Tabi 
1718432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */
1728432395fSTimur Tabi #define NUM_MCLK_RATIOS		ARRAY_SIZE(cs4270_mode_ratios)
1738432395fSTimur Tabi 
174ff7bf02fSTimur Tabi /**
175ff7bf02fSTimur Tabi  * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
176ff7bf02fSTimur Tabi  * @codec_dai: the codec DAI
177ff7bf02fSTimur Tabi  * @clk_id: the clock ID (ignored)
178ff7bf02fSTimur Tabi  * @freq: the MCLK input frequency
179ff7bf02fSTimur Tabi  * @dir: the clock direction (ignored)
1808432395fSTimur Tabi  *
181ff7bf02fSTimur Tabi  * This function is used to tell the codec driver what the input MCLK
182ff7bf02fSTimur Tabi  * frequency is.
1838432395fSTimur Tabi  *
1848432395fSTimur Tabi  * The value of MCLK is used to determine which sample rates are supported
1858432395fSTimur Tabi  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
186ff7bf02fSTimur Tabi  * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
1878432395fSTimur Tabi  *
1888432395fSTimur Tabi  * This function calculates the nine ratios and determines which ones match
1898432395fSTimur Tabi  * a standard sample rate.  If there's a match, then it is added to the list
190ff7bf02fSTimur Tabi  * of supported sample rates.
1918432395fSTimur Tabi  *
1928432395fSTimur Tabi  * This function must be called by the machine driver's 'startup' function,
1938432395fSTimur Tabi  * otherwise the list of supported sample rates will not be available in
1948432395fSTimur Tabi  * time for ALSA.
1958432395fSTimur Tabi  */
196e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1978432395fSTimur Tabi 				 int clk_id, unsigned int freq, int dir)
1988432395fSTimur Tabi {
1998432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
2008432395fSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
2018432395fSTimur Tabi 	unsigned int rates = 0;
2028432395fSTimur Tabi 	unsigned int rate_min = -1;
2038432395fSTimur Tabi 	unsigned int rate_max = 0;
2048432395fSTimur Tabi 	unsigned int i;
2058432395fSTimur Tabi 
2068432395fSTimur Tabi 	cs4270->mclk = freq;
2078432395fSTimur Tabi 
2088432395fSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
2098432395fSTimur Tabi 		unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
2108432395fSTimur Tabi 		rates |= snd_pcm_rate_to_rate_bit(rate);
2118432395fSTimur Tabi 		if (rate < rate_min)
2128432395fSTimur Tabi 			rate_min = rate;
2138432395fSTimur Tabi 		if (rate > rate_max)
2148432395fSTimur Tabi 			rate_max = rate;
2158432395fSTimur Tabi 	}
2168432395fSTimur Tabi 	/* FIXME: soc should support a rate list */
2178432395fSTimur Tabi 	rates &= ~SNDRV_PCM_RATE_KNOT;
2188432395fSTimur Tabi 
2198432395fSTimur Tabi 	if (!rates) {
220a6c255e0STimur Tabi 		dev_err(codec->dev, "could not find a valid sample rate\n");
2218432395fSTimur Tabi 		return -EINVAL;
2228432395fSTimur Tabi 	}
2238432395fSTimur Tabi 
2248432395fSTimur Tabi 	codec_dai->playback.rates = rates;
2258432395fSTimur Tabi 	codec_dai->playback.rate_min = rate_min;
2268432395fSTimur Tabi 	codec_dai->playback.rate_max = rate_max;
2278432395fSTimur Tabi 
2288432395fSTimur Tabi 	codec_dai->capture.rates = rates;
2298432395fSTimur Tabi 	codec_dai->capture.rate_min = rate_min;
2308432395fSTimur Tabi 	codec_dai->capture.rate_max = rate_max;
2318432395fSTimur Tabi 
2328432395fSTimur Tabi 	return 0;
2338432395fSTimur Tabi }
2348432395fSTimur Tabi 
235ff7bf02fSTimur Tabi /**
236ff7bf02fSTimur Tabi  * cs4270_set_dai_fmt - configure the codec for the selected audio format
237ff7bf02fSTimur Tabi  * @codec_dai: the codec DAI
238ff7bf02fSTimur Tabi  * @format: a SND_SOC_DAIFMT_x value indicating the data format
2398432395fSTimur Tabi  *
2408432395fSTimur Tabi  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
2418432395fSTimur Tabi  * codec accordingly.
2428432395fSTimur Tabi  *
2438432395fSTimur Tabi  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
2448432395fSTimur Tabi  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
2458432395fSTimur Tabi  * data for playback only, but ASoC currently does not support different
2468432395fSTimur Tabi  * formats for playback vs. record.
2478432395fSTimur Tabi  */
248e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
2498432395fSTimur Tabi 			      unsigned int format)
2508432395fSTimur Tabi {
2518432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
2528432395fSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
2538432395fSTimur Tabi 	int ret = 0;
2548432395fSTimur Tabi 
2554eae080dSDaniel Mack 	/* set DAI format */
2568432395fSTimur Tabi 	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
2578432395fSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
2588432395fSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
2598432395fSTimur Tabi 		cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
2608432395fSTimur Tabi 		break;
2618432395fSTimur Tabi 	default:
262a6c255e0STimur Tabi 		dev_err(codec->dev, "invalid dai format\n");
2638432395fSTimur Tabi 		ret = -EINVAL;
2648432395fSTimur Tabi 	}
2658432395fSTimur Tabi 
2664eae080dSDaniel Mack 	/* set master/slave audio interface */
2674eae080dSDaniel Mack 	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
2684eae080dSDaniel Mack 	case SND_SOC_DAIFMT_CBS_CFS:
2694eae080dSDaniel Mack 		cs4270->slave_mode = 1;
2704eae080dSDaniel Mack 		break;
2714eae080dSDaniel Mack 	case SND_SOC_DAIFMT_CBM_CFM:
2724eae080dSDaniel Mack 		cs4270->slave_mode = 0;
2734eae080dSDaniel Mack 		break;
2744eae080dSDaniel Mack 	default:
275ff09d49aSDaniel Mack 		/* all other modes are unsupported by the hardware */
2764eae080dSDaniel Mack 		ret = -EINVAL;
2774eae080dSDaniel Mack 	}
2784eae080dSDaniel Mack 
2798432395fSTimur Tabi 	return ret;
2808432395fSTimur Tabi }
2818432395fSTimur Tabi 
282ff7bf02fSTimur Tabi /**
283ff7bf02fSTimur Tabi  * cs4270_fill_cache - pre-fill the CS4270 register cache.
284ff7bf02fSTimur Tabi  * @codec: the codec for this CS4270
285ff7bf02fSTimur Tabi  *
286ff7bf02fSTimur Tabi  * This function fills in the CS4270 register cache by reading the register
287ff7bf02fSTimur Tabi  * values from the hardware.
288ff7bf02fSTimur Tabi  *
289ff7bf02fSTimur Tabi  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
290ff7bf02fSTimur Tabi  * After the initial read to pre-fill the cache, the CS4270 never updates
291ff7bf02fSTimur Tabi  * the register values, so we won't have a cache coherency problem.
292b0c813ceSTimur Tabi  *
293b0c813ceSTimur Tabi  * We use the auto-increment feature of the CS4270 to read all registers in
294b0c813ceSTimur Tabi  * one shot.
295b0c813ceSTimur Tabi  */
296b0c813ceSTimur Tabi static int cs4270_fill_cache(struct snd_soc_codec *codec)
297b0c813ceSTimur Tabi {
298b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
299b0c813ceSTimur Tabi 	struct i2c_client *i2c_client = codec->control_data;
300b0c813ceSTimur Tabi 	s32 length;
301b0c813ceSTimur Tabi 
302b0c813ceSTimur Tabi 	length = i2c_smbus_read_i2c_block_data(i2c_client,
30380ab8817SDaniel Mack 		CS4270_FIRSTREG | CS4270_I2C_INCR, CS4270_NUMREGS, cache);
304b0c813ceSTimur Tabi 
305b0c813ceSTimur Tabi 	if (length != CS4270_NUMREGS) {
306a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
307b0c813ceSTimur Tabi 		       i2c_client->addr);
308b0c813ceSTimur Tabi 		return -EIO;
309b0c813ceSTimur Tabi 	}
310b0c813ceSTimur Tabi 
311b0c813ceSTimur Tabi 	return 0;
312b0c813ceSTimur Tabi }
313b0c813ceSTimur Tabi 
314ff7bf02fSTimur Tabi /**
315ff7bf02fSTimur Tabi  * cs4270_read_reg_cache - read from the CS4270 register cache.
316ff7bf02fSTimur Tabi  * @codec: the codec for this CS4270
317ff7bf02fSTimur Tabi  * @reg: the register to read
318ff7bf02fSTimur Tabi  *
319ff7bf02fSTimur Tabi  * This function returns the value for a given register.  It reads only from
320ff7bf02fSTimur Tabi  * the register cache, not the hardware itself.
321b0c813ceSTimur Tabi  *
322b0c813ceSTimur Tabi  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
323b0c813ceSTimur Tabi  * After the initial read to pre-fill the cache, the CS4270 never updates
324ff7bf02fSTimur Tabi  * the register values, so we won't have a cache coherency problem.
325b0c813ceSTimur Tabi  */
326b0c813ceSTimur Tabi static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
327b0c813ceSTimur Tabi 	unsigned int reg)
328b0c813ceSTimur Tabi {
329b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
330b0c813ceSTimur Tabi 
331b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
332b0c813ceSTimur Tabi 		return -EIO;
333b0c813ceSTimur Tabi 
334b0c813ceSTimur Tabi 	return cache[reg - CS4270_FIRSTREG];
335b0c813ceSTimur Tabi }
336b0c813ceSTimur Tabi 
337ff7bf02fSTimur Tabi /**
338ff7bf02fSTimur Tabi  * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
339ff7bf02fSTimur Tabi  * @codec: the codec for this CS4270
340ff7bf02fSTimur Tabi  * @reg: the register to write
341ff7bf02fSTimur Tabi  * @value: the value to write to the register
342b0c813ceSTimur Tabi  *
343b0c813ceSTimur Tabi  * This function writes the given value to the given CS4270 register, and
344b0c813ceSTimur Tabi  * also updates the register cache.
345b0c813ceSTimur Tabi  *
346b0c813ceSTimur Tabi  * Note that we don't use the hw_write function pointer of snd_soc_codec.
347b0c813ceSTimur Tabi  * That's because it's too clunky: the hw_write_t prototype does not match
348b0c813ceSTimur Tabi  * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
349b0c813ceSTimur Tabi  */
350b0c813ceSTimur Tabi static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
351b0c813ceSTimur Tabi 			    unsigned int value)
352b0c813ceSTimur Tabi {
353bfc4e861STimur Tabi 	u8 *cache = codec->reg_cache;
354bfc4e861STimur Tabi 
355b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
356b0c813ceSTimur Tabi 		return -EIO;
357b0c813ceSTimur Tabi 
358bfc4e861STimur Tabi 	/* Only perform an I2C operation if the new value is different */
359bfc4e861STimur Tabi 	if (cache[reg - CS4270_FIRSTREG] != value) {
360bfc4e861STimur Tabi 		struct i2c_client *client = codec->control_data;
361bfc4e861STimur Tabi 		if (i2c_smbus_write_byte_data(client, reg, value)) {
362a6c255e0STimur Tabi 			dev_err(codec->dev, "i2c write failed\n");
363b0c813ceSTimur Tabi 			return -EIO;
364b0c813ceSTimur Tabi 		}
365bfc4e861STimur Tabi 
366bfc4e861STimur Tabi 		/* We've written to the hardware, so update the cache */
367bfc4e861STimur Tabi 		cache[reg - CS4270_FIRSTREG] = value;
368bfc4e861STimur Tabi 	}
369bfc4e861STimur Tabi 
370bfc4e861STimur Tabi 	return 0;
371b0c813ceSTimur Tabi }
372b0c813ceSTimur Tabi 
373ff7bf02fSTimur Tabi /**
374ff7bf02fSTimur Tabi  * cs4270_hw_params - program the CS4270 with the given hardware parameters.
375ff7bf02fSTimur Tabi  * @substream: the audio stream
376ff7bf02fSTimur Tabi  * @params: the hardware parameters to set
377ff7bf02fSTimur Tabi  * @dai: the SOC DAI (ignored)
378b0c813ceSTimur Tabi  *
379ff7bf02fSTimur Tabi  * This function programs the hardware with the values provided.
380ff7bf02fSTimur Tabi  * Specifically, the sample rate and the data format.
381ff7bf02fSTimur Tabi  *
382ff7bf02fSTimur Tabi  * The .ops functions are used to provide board-specific data, like input
383ff7bf02fSTimur Tabi  * frequencies, to this driver.  This function takes that information,
384b0c813ceSTimur Tabi  * combines it with the hardware parameters provided, and programs the
385b0c813ceSTimur Tabi  * hardware accordingly.
386b0c813ceSTimur Tabi  */
387b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream,
388dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
389dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
390b0c813ceSTimur Tabi {
391b0c813ceSTimur Tabi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
392b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = rtd->socdev;
3936627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
394b0c813ceSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
395e34ba212SRoel Kluin 	int ret;
396b0c813ceSTimur Tabi 	unsigned int i;
397b0c813ceSTimur Tabi 	unsigned int rate;
398b0c813ceSTimur Tabi 	unsigned int ratio;
399b0c813ceSTimur Tabi 	int reg;
400b0c813ceSTimur Tabi 
401b0c813ceSTimur Tabi 	/* Figure out which MCLK/LRCK ratio to use */
402b0c813ceSTimur Tabi 
403b0c813ceSTimur Tabi 	rate = params_rate(params);	/* Sampling rate, in Hz */
404b0c813ceSTimur Tabi 	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */
405b0c813ceSTimur Tabi 
4069dbd627bSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
4078432395fSTimur Tabi 		if (cs4270_mode_ratios[i].ratio == ratio)
408b0c813ceSTimur Tabi 			break;
409b0c813ceSTimur Tabi 	}
410b0c813ceSTimur Tabi 
4119dbd627bSTimur Tabi 	if (i == NUM_MCLK_RATIOS) {
412b0c813ceSTimur Tabi 		/* We did not find a matching ratio */
413a6c255e0STimur Tabi 		dev_err(codec->dev, "could not find matching ratio\n");
414b0c813ceSTimur Tabi 		return -EINVAL;
415b0c813ceSTimur Tabi 	}
416b0c813ceSTimur Tabi 
417d5e9ba1dSTimur Tabi 	/* Set the sample rate */
418b0c813ceSTimur Tabi 
419b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MODE);
420b0c813ceSTimur Tabi 	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
4214eae080dSDaniel Mack 	reg |= cs4270_mode_ratios[i].mclk;
4224eae080dSDaniel Mack 
4234eae080dSDaniel Mack 	if (cs4270->slave_mode)
4244eae080dSDaniel Mack 		reg |= CS4270_MODE_SLAVE;
4254eae080dSDaniel Mack 	else
4264eae080dSDaniel Mack 		reg |= cs4270_mode_ratios[i].speed_mode;
427b0c813ceSTimur Tabi 
428b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MODE, reg);
429b0c813ceSTimur Tabi 	if (ret < 0) {
430a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c write failed\n");
431b0c813ceSTimur Tabi 		return ret;
432b0c813ceSTimur Tabi 	}
433b0c813ceSTimur Tabi 
434d5e9ba1dSTimur Tabi 	/* Set the DAI format */
435b0c813ceSTimur Tabi 
436b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_FORMAT);
437b0c813ceSTimur Tabi 	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
438b0c813ceSTimur Tabi 
439b0c813ceSTimur Tabi 	switch (cs4270->mode) {
440b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
441b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
442b0c813ceSTimur Tabi 		break;
443b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
444b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
445b0c813ceSTimur Tabi 		break;
446b0c813ceSTimur Tabi 	default:
447a6c255e0STimur Tabi 		dev_err(codec->dev, "unknown dai format\n");
448b0c813ceSTimur Tabi 		return -EINVAL;
449b0c813ceSTimur Tabi 	}
450b0c813ceSTimur Tabi 
451b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_FORMAT, reg);
452b0c813ceSTimur Tabi 	if (ret < 0) {
453a6c255e0STimur Tabi 		dev_err(codec->dev, "i2c write failed\n");
454b0c813ceSTimur Tabi 		return ret;
455b0c813ceSTimur Tabi 	}
456b0c813ceSTimur Tabi 
457b0c813ceSTimur Tabi 	return ret;
458b0c813ceSTimur Tabi }
459b0c813ceSTimur Tabi 
460ff7bf02fSTimur Tabi /**
4611a4ba05eSDaniel Mack  * cs4270_dai_mute - enable/disable the CS4270 external mute
462ff7bf02fSTimur Tabi  * @dai: the SOC DAI
463ff7bf02fSTimur Tabi  * @mute: 0 = disable mute, 1 = enable mute
464b0c813ceSTimur Tabi  *
465b0c813ceSTimur Tabi  * This function toggles the mute bits in the MUTE register.  The CS4270's
466b0c813ceSTimur Tabi  * mute capability is intended for external muting circuitry, so if the
467b0c813ceSTimur Tabi  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
468b0c813ceSTimur Tabi  * then this function will do nothing.
469b0c813ceSTimur Tabi  */
4701a4ba05eSDaniel Mack static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
471b0c813ceSTimur Tabi {
472b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = dai->codec;
4731a4ba05eSDaniel Mack 	struct cs4270_private *cs4270 = codec->private_data;
474b0c813ceSTimur Tabi 	int reg6;
475b0c813ceSTimur Tabi 
476b0c813ceSTimur Tabi 	reg6 = snd_soc_read(codec, CS4270_MUTE);
477b0c813ceSTimur Tabi 
478b0c813ceSTimur Tabi 	if (mute)
479d5e9ba1dSTimur Tabi 		reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
4801a4ba05eSDaniel Mack 	else {
481d5e9ba1dSTimur Tabi 		reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
4821a4ba05eSDaniel Mack 		reg6 |= cs4270->manual_mute;
4831a4ba05eSDaniel Mack 	}
484b0c813ceSTimur Tabi 
485b0c813ceSTimur Tabi 	return snd_soc_write(codec, CS4270_MUTE, reg6);
486b0c813ceSTimur Tabi }
487b0c813ceSTimur Tabi 
4881a4ba05eSDaniel Mack /**
4891a4ba05eSDaniel Mack  * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
4901a4ba05eSDaniel Mack  * 			 alsa control.
4911a4ba05eSDaniel Mack  * @kcontrol: mixer control
4921a4ba05eSDaniel Mack  * @ucontrol: control element information
4931a4ba05eSDaniel Mack  *
4941a4ba05eSDaniel Mack  * This function basically passes the arguments on to the generic
4951a4ba05eSDaniel Mack  * snd_soc_put_volsw() function and saves the mute information in
4961a4ba05eSDaniel Mack  * our private data structure. This is because we want to prevent
4971a4ba05eSDaniel Mack  * cs4270_dai_mute() neglecting the user's decision to manually
4981a4ba05eSDaniel Mack  * mute the codec's output.
4991a4ba05eSDaniel Mack  *
5001a4ba05eSDaniel Mack  * Returns 0 for success.
5011a4ba05eSDaniel Mack  */
5021a4ba05eSDaniel Mack static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
5031a4ba05eSDaniel Mack 				struct snd_ctl_elem_value *ucontrol)
5041a4ba05eSDaniel Mack {
5051a4ba05eSDaniel Mack 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
5061a4ba05eSDaniel Mack 	struct cs4270_private *cs4270 = codec->private_data;
5071a4ba05eSDaniel Mack 	int left = !ucontrol->value.integer.value[0];
5081a4ba05eSDaniel Mack 	int right = !ucontrol->value.integer.value[1];
5091a4ba05eSDaniel Mack 
5101a4ba05eSDaniel Mack 	cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
5111a4ba05eSDaniel Mack 			      (right ? CS4270_MUTE_DAC_B : 0);
5121a4ba05eSDaniel Mack 
5131a4ba05eSDaniel Mack 	return snd_soc_put_volsw(kcontrol, ucontrol);
5141a4ba05eSDaniel Mack }
5151a4ba05eSDaniel Mack 
516b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */
517b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = {
518b0c813ceSTimur Tabi 	SOC_DOUBLE_R("Master Playback Volume",
519d5e9ba1dSTimur Tabi 		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
520d5e9ba1dSTimur Tabi 	SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
521d5e9ba1dSTimur Tabi 	SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
522d5e9ba1dSTimur Tabi 	SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
523*7e1aa1dcSDaniel Mack 	SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
524d5e9ba1dSTimur Tabi 	SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
525d5e9ba1dSTimur Tabi 	SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
5261a4ba05eSDaniel Mack 	SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
5271a4ba05eSDaniel Mack 	SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
5281a4ba05eSDaniel Mack 		snd_soc_get_volsw, cs4270_soc_put_mute),
529b0c813ceSTimur Tabi };
530b0c813ceSTimur Tabi 
531b0c813ceSTimur Tabi /*
532ff7bf02fSTimur Tabi  * cs4270_codec - global variable to store codec for the ASoC probe function
533b0c813ceSTimur Tabi  *
534b0c813ceSTimur Tabi  * If struct i2c_driver had a private_data field, we wouldn't need to use
53504eb093cSTimur Tabi  * cs4270_codec.  This is the only way to pass the codec structure from
53604eb093cSTimur Tabi  * cs4270_i2c_probe() to cs4270_probe().  Unfortunately, there is no good
53704eb093cSTimur Tabi  * way to synchronize these two functions.  cs4270_i2c_probe() can be called
53804eb093cSTimur Tabi  * multiple times before cs4270_probe() is called even once.  So for now, we
53904eb093cSTimur Tabi  * also only allow cs4270_i2c_probe() to be run once.  That means that we do
54004eb093cSTimur Tabi  * not support more than one cs4270 device in the system, at least for now.
541b0c813ceSTimur Tabi  */
54204eb093cSTimur Tabi static struct snd_soc_codec *cs4270_codec;
543b0c813ceSTimur Tabi 
5446335d055SEric Miao static struct snd_soc_dai_ops cs4270_dai_ops = {
5456335d055SEric Miao 	.hw_params	= cs4270_hw_params,
5466335d055SEric Miao 	.set_sysclk	= cs4270_set_dai_sysclk,
5476335d055SEric Miao 	.set_fmt	= cs4270_set_dai_fmt,
5481a4ba05eSDaniel Mack 	.digital_mute	= cs4270_dai_mute,
5496335d055SEric Miao };
5506335d055SEric Miao 
551e550e17fSLiam Girdwood struct snd_soc_dai cs4270_dai = {
5520db4d070STimur Tabi 	.name = "cs4270",
553b0c813ceSTimur Tabi 	.playback = {
554b0c813ceSTimur Tabi 		.stream_name = "Playback",
555b0c813ceSTimur Tabi 		.channels_min = 1,
556b0c813ceSTimur Tabi 		.channels_max = 2,
557b0c813ceSTimur Tabi 		.rates = 0,
558b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
559b0c813ceSTimur Tabi 	},
560b0c813ceSTimur Tabi 	.capture = {
561b0c813ceSTimur Tabi 		.stream_name = "Capture",
562b0c813ceSTimur Tabi 		.channels_min = 1,
563b0c813ceSTimur Tabi 		.channels_max = 2,
564b0c813ceSTimur Tabi 		.rates = 0,
565b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
566b0c813ceSTimur Tabi 	},
5676335d055SEric Miao 	.ops = &cs4270_dai_ops,
568b0c813ceSTimur Tabi };
569b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(cs4270_dai);
570b0c813ceSTimur Tabi 
571ff7bf02fSTimur Tabi /**
572ff7bf02fSTimur Tabi  * cs4270_probe - ASoC probe function
573ff7bf02fSTimur Tabi  * @pdev: platform device
574ff7bf02fSTimur Tabi  *
575ff7bf02fSTimur Tabi  * This function is called when ASoC has all the pieces it needs to
576ff7bf02fSTimur Tabi  * instantiate a sound driver.
57704eb093cSTimur Tabi  */
57804eb093cSTimur Tabi static int cs4270_probe(struct platform_device *pdev)
57904eb093cSTimur Tabi {
58004eb093cSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
58104eb093cSTimur Tabi 	struct snd_soc_codec *codec = cs4270_codec;
58204eb093cSTimur Tabi 	int ret;
58304eb093cSTimur Tabi 
58404eb093cSTimur Tabi 	/* Connect the codec to the socdev.  snd_soc_new_pcms() needs this. */
58504eb093cSTimur Tabi 	socdev->card->codec = codec;
58604eb093cSTimur Tabi 
58704eb093cSTimur Tabi 	/* Register PCMs */
58804eb093cSTimur Tabi 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
58904eb093cSTimur Tabi 	if (ret < 0) {
590a6c255e0STimur Tabi 		dev_err(codec->dev, "failed to create pcms\n");
59104eb093cSTimur Tabi 		return ret;
59204eb093cSTimur Tabi 	}
59304eb093cSTimur Tabi 
59404eb093cSTimur Tabi 	/* Add the non-DAPM controls */
595eb5f6d75SPhilipp Zabel 	ret = snd_soc_add_controls(codec, cs4270_snd_controls,
596eb5f6d75SPhilipp Zabel 				ARRAY_SIZE(cs4270_snd_controls));
59704eb093cSTimur Tabi 	if (ret < 0) {
598eb5f6d75SPhilipp Zabel 		dev_err(codec->dev, "failed to add controls\n");
59904eb093cSTimur Tabi 		goto error_free_pcms;
60004eb093cSTimur Tabi 	}
60104eb093cSTimur Tabi 
60204eb093cSTimur Tabi 	/* And finally, register the socdev */
60304eb093cSTimur Tabi 	ret = snd_soc_init_card(socdev);
60404eb093cSTimur Tabi 	if (ret < 0) {
605a6c255e0STimur Tabi 		dev_err(codec->dev, "failed to register card\n");
60604eb093cSTimur Tabi 		goto error_free_pcms;
60704eb093cSTimur Tabi 	}
60804eb093cSTimur Tabi 
60904eb093cSTimur Tabi 	return 0;
61004eb093cSTimur Tabi 
61104eb093cSTimur Tabi error_free_pcms:
61204eb093cSTimur Tabi 	snd_soc_free_pcms(socdev);
61304eb093cSTimur Tabi 
61404eb093cSTimur Tabi 	return ret;
61504eb093cSTimur Tabi }
61604eb093cSTimur Tabi 
617ff7bf02fSTimur Tabi /**
618ff7bf02fSTimur Tabi  * cs4270_remove - ASoC remove function
619ff7bf02fSTimur Tabi  * @pdev: platform device
620ff7bf02fSTimur Tabi  *
621ff7bf02fSTimur Tabi  * This function is the counterpart to cs4270_probe().
622ff7bf02fSTimur Tabi  */
62304eb093cSTimur Tabi static int cs4270_remove(struct platform_device *pdev)
62404eb093cSTimur Tabi {
62504eb093cSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
62604eb093cSTimur Tabi 
62704eb093cSTimur Tabi 	snd_soc_free_pcms(socdev);
62804eb093cSTimur Tabi 
62904eb093cSTimur Tabi 	return 0;
63004eb093cSTimur Tabi };
63104eb093cSTimur Tabi 
632ff7bf02fSTimur Tabi /**
633ff7bf02fSTimur Tabi  * cs4270_i2c_probe - initialize the I2C interface of the CS4270
634ff7bf02fSTimur Tabi  * @i2c_client: the I2C client object
635ff7bf02fSTimur Tabi  * @id: the I2C device ID (ignored)
636b0c813ceSTimur Tabi  *
637ff7bf02fSTimur Tabi  * This function is called whenever the I2C subsystem finds a device that
638ff7bf02fSTimur Tabi  * matches the device ID given via a prior call to i2c_add_driver().
639b0c813ceSTimur Tabi  */
6400db4d070STimur Tabi static int cs4270_i2c_probe(struct i2c_client *i2c_client,
6410db4d070STimur Tabi 	const struct i2c_device_id *id)
642b0c813ceSTimur Tabi {
643b0c813ceSTimur Tabi 	struct snd_soc_codec *codec;
6440db4d070STimur Tabi 	struct cs4270_private *cs4270;
645d5e9ba1dSTimur Tabi 	unsigned int reg;
64604eb093cSTimur Tabi 	int ret;
64704eb093cSTimur Tabi 
64804eb093cSTimur Tabi 	/* For now, we only support one cs4270 device in the system.  See the
64904eb093cSTimur Tabi 	 * comment for cs4270_codec.
65004eb093cSTimur Tabi 	 */
65104eb093cSTimur Tabi 	if (cs4270_codec) {
652a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
65304eb093cSTimur Tabi 		       i2c_client->addr);
654a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "only one per board allowed\n");
65504eb093cSTimur Tabi 		/* Should we return something other than ENODEV here? */
65604eb093cSTimur Tabi 		return -ENODEV;
65704eb093cSTimur Tabi 	}
658b0c813ceSTimur Tabi 
6590db4d070STimur Tabi 	/* Verify that we have a CS4270 */
6600db4d070STimur Tabi 
6610db4d070STimur Tabi 	ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
6620db4d070STimur Tabi 	if (ret < 0) {
663a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
66404eb093cSTimur Tabi 		       i2c_client->addr);
6650db4d070STimur Tabi 		return ret;
6660db4d070STimur Tabi 	}
6670db4d070STimur Tabi 	/* The top four bits of the chip ID should be 1100. */
6680db4d070STimur Tabi 	if ((ret & 0xF0) != 0xC0) {
669a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
6700db4d070STimur Tabi 		       i2c_client->addr);
6710db4d070STimur Tabi 		return -ENODEV;
6720db4d070STimur Tabi 	}
6730db4d070STimur Tabi 
674a6c255e0STimur Tabi 	dev_info(&i2c_client->dev, "found device at i2c address %X\n",
6750db4d070STimur Tabi 		i2c_client->addr);
676a6c255e0STimur Tabi 	dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
677b0c813ceSTimur Tabi 
678b0c813ceSTimur Tabi 	/* Allocate enough space for the snd_soc_codec structure
679b0c813ceSTimur Tabi 	   and our private data together. */
6800db4d070STimur Tabi 	cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
6810db4d070STimur Tabi 	if (!cs4270) {
682a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "could not allocate codec\n");
683b0c813ceSTimur Tabi 		return -ENOMEM;
684b0c813ceSTimur Tabi 	}
6850db4d070STimur Tabi 	codec = &cs4270->codec;
686b0c813ceSTimur Tabi 
687b0c813ceSTimur Tabi 	mutex_init(&codec->mutex);
688b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_widgets);
689b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_paths);
690b0c813ceSTimur Tabi 
691a6c255e0STimur Tabi 	codec->dev = &i2c_client->dev;
692b0c813ceSTimur Tabi 	codec->name = "CS4270";
693b0c813ceSTimur Tabi 	codec->owner = THIS_MODULE;
694b0c813ceSTimur Tabi 	codec->dai = &cs4270_dai;
695b0c813ceSTimur Tabi 	codec->num_dai = 1;
6960db4d070STimur Tabi 	codec->private_data = cs4270;
6970db4d070STimur Tabi 	codec->control_data = i2c_client;
6980db4d070STimur Tabi 	codec->read = cs4270_read_reg_cache;
6990db4d070STimur Tabi 	codec->write = cs4270_i2c_write;
7000db4d070STimur Tabi 	codec->reg_cache = cs4270->reg_cache;
7010db4d070STimur Tabi 	codec->reg_cache_size = CS4270_NUMREGS;
702b0c813ceSTimur Tabi 
7030db4d070STimur Tabi 	/* The I2C interface is set up, so pre-fill our register cache */
7040db4d070STimur Tabi 
7050db4d070STimur Tabi 	ret = cs4270_fill_cache(codec);
7060db4d070STimur Tabi 	if (ret < 0) {
707a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "failed to fill register cache\n");
7080db4d070STimur Tabi 		goto error_free_codec;
7090db4d070STimur Tabi 	}
710b0c813ceSTimur Tabi 
711d5e9ba1dSTimur Tabi 	/* Disable auto-mute.  This feature appears to be buggy.  In some
712d5e9ba1dSTimur Tabi 	 * situations, auto-mute will not deactivate when it should, so we want
713d5e9ba1dSTimur Tabi 	 * this feature disabled by default.  An application (e.g. alsactl) can
714d5e9ba1dSTimur Tabi 	 * re-enabled it by using the controls.
715d5e9ba1dSTimur Tabi 	 */
716d5e9ba1dSTimur Tabi 
717d5e9ba1dSTimur Tabi 	reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
718d5e9ba1dSTimur Tabi 	reg &= ~CS4270_MUTE_AUTO;
719d5e9ba1dSTimur Tabi 	ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
720d5e9ba1dSTimur Tabi 	if (ret < 0) {
721d5e9ba1dSTimur Tabi 		dev_err(&i2c_client->dev, "i2c write failed\n");
722d5e9ba1dSTimur Tabi 		return ret;
723d5e9ba1dSTimur Tabi 	}
724d5e9ba1dSTimur Tabi 
725d5e9ba1dSTimur Tabi 	/* Disable automatic volume control.  The hardware enables, and it
726d5e9ba1dSTimur Tabi 	 * causes volume change commands to be delayed, sometimes until after
727d5e9ba1dSTimur Tabi 	 * playback has started.  An application (e.g. alsactl) can
728d5e9ba1dSTimur Tabi 	 * re-enabled it by using the controls.
729d5e9ba1dSTimur Tabi 	 */
730d5e9ba1dSTimur Tabi 
731d5e9ba1dSTimur Tabi 	reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
732d5e9ba1dSTimur Tabi 	reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
733d5e9ba1dSTimur Tabi 	ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
734d5e9ba1dSTimur Tabi 	if (ret < 0) {
735d5e9ba1dSTimur Tabi 		dev_err(&i2c_client->dev, "i2c write failed\n");
736d5e9ba1dSTimur Tabi 		return ret;
737d5e9ba1dSTimur Tabi 	}
738d5e9ba1dSTimur Tabi 
739a6c255e0STimur Tabi 	/* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
740a6c255e0STimur Tabi 	 * structure for each CS4270 device, but the machine driver needs to
741a6c255e0STimur Tabi 	 * have a pointer to the DAI structure, so for now it must be a global
742a6c255e0STimur Tabi 	 * variable.
743a6c255e0STimur Tabi 	 */
744a6c255e0STimur Tabi 	cs4270_dai.dev = &i2c_client->dev;
745a6c255e0STimur Tabi 
74604eb093cSTimur Tabi 	/* Register the DAI.  If all the other ASoC driver have already
74704eb093cSTimur Tabi 	 * registered, then this will call our probe function, so
74804eb093cSTimur Tabi 	 * cs4270_codec needs to be ready.
74904eb093cSTimur Tabi 	 */
750a6c255e0STimur Tabi 	cs4270_codec = codec;
75104eb093cSTimur Tabi 	ret = snd_soc_register_dai(&cs4270_dai);
752b0c813ceSTimur Tabi 	if (ret < 0) {
753a6c255e0STimur Tabi 		dev_err(&i2c_client->dev, "failed to register DAIe\n");
754e3145dfbSJean Delvare 		goto error_free_codec;
755b0c813ceSTimur Tabi 	}
756b0c813ceSTimur Tabi 
75704eb093cSTimur Tabi 	i2c_set_clientdata(i2c_client, cs4270);
758e3145dfbSJean Delvare 
7590db4d070STimur Tabi 	return 0;
760e3145dfbSJean Delvare 
761e3145dfbSJean Delvare error_free_codec:
7620db4d070STimur Tabi 	kfree(cs4270);
763a6c255e0STimur Tabi 	cs4270_codec = NULL;
764a6c255e0STimur Tabi 	cs4270_dai.dev = NULL;
765e3145dfbSJean Delvare 
766b0c813ceSTimur Tabi 	return ret;
767b0c813ceSTimur Tabi }
768b0c813ceSTimur Tabi 
769ff7bf02fSTimur Tabi /**
770ff7bf02fSTimur Tabi  * cs4270_i2c_remove - remove an I2C device
771ff7bf02fSTimur Tabi  * @i2c_client: the I2C client object
772ff7bf02fSTimur Tabi  *
773ff7bf02fSTimur Tabi  * This function is the counterpart to cs4270_i2c_probe().
774ff7bf02fSTimur Tabi  */
7750db4d070STimur Tabi static int cs4270_i2c_remove(struct i2c_client *i2c_client)
776b0c813ceSTimur Tabi {
77704eb093cSTimur Tabi 	struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
778b0c813ceSTimur Tabi 
7790db4d070STimur Tabi 	kfree(cs4270);
780a6c255e0STimur Tabi 	cs4270_codec = NULL;
781a6c255e0STimur Tabi 	cs4270_dai.dev = NULL;
782b0c813ceSTimur Tabi 
7830db4d070STimur Tabi 	return 0;
7840db4d070STimur Tabi }
7850db4d070STimur Tabi 
786ff7bf02fSTimur Tabi /*
787ff7bf02fSTimur Tabi  * cs4270_id - I2C device IDs supported by this driver
788ff7bf02fSTimur Tabi  */
7890db4d070STimur Tabi static struct i2c_device_id cs4270_id[] = {
7900db4d070STimur Tabi 	{"cs4270", 0},
7910db4d070STimur Tabi 	{}
7920db4d070STimur Tabi };
7930db4d070STimur Tabi MODULE_DEVICE_TABLE(i2c, cs4270_id);
7940db4d070STimur Tabi 
7955e7c0344SDaniel Mack #ifdef CONFIG_PM
7965e7c0344SDaniel Mack 
7975e7c0344SDaniel Mack /* This suspend/resume implementation can handle both - a simple standby
7985e7c0344SDaniel Mack  * where the codec remains powered, and a full suspend, where the voltage
7995e7c0344SDaniel Mack  * domain the codec is connected to is teared down and/or any other hardware
8005e7c0344SDaniel Mack  * reset condition is asserted.
8015e7c0344SDaniel Mack  *
8025e7c0344SDaniel Mack  * The codec's own power saving features are enabled in the suspend callback,
8035e7c0344SDaniel Mack  * and all registers are written back to the hardware when resuming.
8045e7c0344SDaniel Mack  */
8055e7c0344SDaniel Mack 
80615b5bdaeSDaniel Mack static int cs4270_soc_suspend(struct platform_device *pdev, pm_message_t mesg)
80715b5bdaeSDaniel Mack {
80815b5bdaeSDaniel Mack 	struct snd_soc_codec *codec = cs4270_codec;
80915b5bdaeSDaniel Mack 	int reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
81015b5bdaeSDaniel Mack 
81115b5bdaeSDaniel Mack 	return snd_soc_write(codec, CS4270_PWRCTL, reg);
81215b5bdaeSDaniel Mack }
81315b5bdaeSDaniel Mack 
81415b5bdaeSDaniel Mack static int cs4270_soc_resume(struct platform_device *pdev)
81515b5bdaeSDaniel Mack {
81615b5bdaeSDaniel Mack 	struct snd_soc_codec *codec = cs4270_codec;
81715b5bdaeSDaniel Mack 	struct i2c_client *i2c_client = codec->control_data;
8185e7c0344SDaniel Mack 	int reg;
8195e7c0344SDaniel Mack 
8205e7c0344SDaniel Mack 	/* In case the device was put to hard reset during sleep, we need to
8215e7c0344SDaniel Mack 	 * wait 500ns here before any I2C communication. */
8225e7c0344SDaniel Mack 	ndelay(500);
8235e7c0344SDaniel Mack 
8245e7c0344SDaniel Mack 	/* first restore the entire register cache ... */
8255e7c0344SDaniel Mack 	for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
8265e7c0344SDaniel Mack 		u8 val = snd_soc_read(codec, reg);
8275e7c0344SDaniel Mack 
82815b5bdaeSDaniel Mack 		if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
8295e7c0344SDaniel Mack 			dev_err(codec->dev, "i2c write failed\n");
8305e7c0344SDaniel Mack 			return -EIO;
8315e7c0344SDaniel Mack 		}
8325e7c0344SDaniel Mack 	}
8335e7c0344SDaniel Mack 
8345e7c0344SDaniel Mack 	/* ... then disable the power-down bits */
8355e7c0344SDaniel Mack 	reg = snd_soc_read(codec, CS4270_PWRCTL);
8365e7c0344SDaniel Mack 	reg &= ~CS4270_PWRCTL_PDN_ALL;
8375e7c0344SDaniel Mack 
8385e7c0344SDaniel Mack 	return snd_soc_write(codec, CS4270_PWRCTL, reg);
8395e7c0344SDaniel Mack }
8405e7c0344SDaniel Mack #else
84115b5bdaeSDaniel Mack #define cs4270_soc_suspend	NULL
84215b5bdaeSDaniel Mack #define cs4270_soc_resume	NULL
8435e7c0344SDaniel Mack #endif /* CONFIG_PM */
8445e7c0344SDaniel Mack 
845ff7bf02fSTimur Tabi /*
846ff7bf02fSTimur Tabi  * cs4270_i2c_driver - I2C device identification
847ff7bf02fSTimur Tabi  *
848ff7bf02fSTimur Tabi  * This structure tells the I2C subsystem how to identify and support a
849ff7bf02fSTimur Tabi  * given I2C device type.
850ff7bf02fSTimur Tabi  */
8510db4d070STimur Tabi static struct i2c_driver cs4270_i2c_driver = {
8520db4d070STimur Tabi 	.driver = {
8530db4d070STimur Tabi 		.name = "cs4270",
8540db4d070STimur Tabi 		.owner = THIS_MODULE,
8550db4d070STimur Tabi 	},
8560db4d070STimur Tabi 	.id_table = cs4270_id,
8570db4d070STimur Tabi 	.probe = cs4270_i2c_probe,
8580db4d070STimur Tabi 	.remove = cs4270_i2c_remove,
8590db4d070STimur Tabi };
8600db4d070STimur Tabi 
8610db4d070STimur Tabi /*
862b0c813ceSTimur Tabi  * ASoC codec device structure
863b0c813ceSTimur Tabi  *
864b0c813ceSTimur Tabi  * Assign this variable to the codec_dev field of the machine driver's
865b0c813ceSTimur Tabi  * snd_soc_device structure.
866b0c813ceSTimur Tabi  */
867b0c813ceSTimur Tabi struct snd_soc_codec_device soc_codec_device_cs4270 = {
868b0c813ceSTimur Tabi 	.probe = 	cs4270_probe,
86915b5bdaeSDaniel Mack 	.remove = 	cs4270_remove,
87015b5bdaeSDaniel Mack 	.suspend =	cs4270_soc_suspend,
87115b5bdaeSDaniel Mack 	.resume =	cs4270_soc_resume,
872b0c813ceSTimur Tabi };
873b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
874b0c813ceSTimur Tabi 
875c9b3a40fSTakashi Iwai static int __init cs4270_init(void)
87664089b84SMark Brown {
877a6c255e0STimur Tabi 	pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
8780db4d070STimur Tabi 
87904eb093cSTimur Tabi 	return i2c_add_driver(&cs4270_i2c_driver);
88064089b84SMark Brown }
88164089b84SMark Brown module_init(cs4270_init);
88264089b84SMark Brown 
88364089b84SMark Brown static void __exit cs4270_exit(void)
88464089b84SMark Brown {
88504eb093cSTimur Tabi 	i2c_del_driver(&cs4270_i2c_driver);
88664089b84SMark Brown }
88764089b84SMark Brown module_exit(cs4270_exit);
88864089b84SMark Brown 
889b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
890b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
891b0c813ceSTimur Tabi MODULE_LICENSE("GPL");
892