xref: /linux/sound/soc/codecs/cs4270.c (revision dee89c4d94433520e4e3977ae203d4cfbfe385fb)
1b0c813ceSTimur Tabi /*
2b0c813ceSTimur Tabi  * CS4270 ALSA SoC (ASoC) codec driver
3b0c813ceSTimur Tabi  *
4b0c813ceSTimur Tabi  * Author: Timur Tabi <timur@freescale.com>
5b0c813ceSTimur Tabi  *
6b0c813ceSTimur Tabi  * Copyright 2007 Freescale Semiconductor, Inc.  This file is licensed under
7b0c813ceSTimur Tabi  * the terms of the GNU General Public License version 2.  This program
8b0c813ceSTimur Tabi  * is licensed "as is" without any warranty of any kind, whether express
9b0c813ceSTimur Tabi  * 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  *
159dbd627bSTimur Tabi  * 1) Software mode is supported.  Stand-alone mode is automatically
169dbd627bSTimur Tabi  *    selected if I2C is disabled or if a CS4270 is not found on the I2C
179dbd627bSTimur Tabi  *    bus.  However, stand-alone mode is only partially implemented because
189dbd627bSTimur Tabi  *    there is no mechanism yet for this driver and the machine driver to
199dbd627bSTimur Tabi  *    communicate the values of the M0, M1, MCLK1, and MCLK2 pins.
20b0c813ceSTimur Tabi  * 2) Only I2C is supported, not SPI
21b0c813ceSTimur Tabi  * 3) Only Master mode is supported, not Slave.
22b0c813ceSTimur Tabi  * 4) The machine driver's 'startup' function must call
23b0c813ceSTimur Tabi  *    cs4270_set_dai_sysclk() with the value of MCLK.
24b0c813ceSTimur Tabi  * 5) Only I2S and left-justified modes are supported
25b0c813ceSTimur Tabi  * 6) Power management is not supported
26b0c813ceSTimur Tabi  * 7) The only supported control is volume and hardware mute (if enabled)
27b0c813ceSTimur Tabi  */
28b0c813ceSTimur Tabi 
29b0c813ceSTimur Tabi #include <linux/module.h>
30b0c813ceSTimur Tabi #include <linux/platform_device.h>
31b0c813ceSTimur Tabi #include <sound/core.h>
32b0c813ceSTimur Tabi #include <sound/soc.h>
33b0c813ceSTimur Tabi #include <sound/initval.h>
34b0c813ceSTimur Tabi #include <linux/i2c.h>
35b0c813ceSTimur Tabi 
36b0c813ceSTimur Tabi #include "cs4270.h"
37b0c813ceSTimur Tabi 
389dbd627bSTimur Tabi /* If I2C is defined, then we support software mode.  However, if we're
399dbd627bSTimur Tabi    not compiled as module but I2C is, then we can't use I2C calls. */
409dbd627bSTimur Tabi #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
419dbd627bSTimur Tabi #define USE_I2C
429dbd627bSTimur Tabi #endif
439dbd627bSTimur Tabi 
44b0c813ceSTimur Tabi /* Private data for the CS4270 */
45b0c813ceSTimur Tabi struct cs4270_private {
46b0c813ceSTimur Tabi 	unsigned int mclk; /* Input frequency of the MCLK pin */
47b0c813ceSTimur Tabi 	unsigned int mode; /* The mode (I2S or left-justified) */
48b0c813ceSTimur Tabi };
49b0c813ceSTimur Tabi 
50b0c813ceSTimur Tabi /*
51b0c813ceSTimur Tabi  * The codec isn't really big-endian or little-endian, since the I2S
52b0c813ceSTimur Tabi  * interface requires data to be sent serially with the MSbit first.
53b0c813ceSTimur Tabi  * However, to support BE and LE I2S devices, we specify both here.  That
54b0c813ceSTimur Tabi  * way, ALSA will always match the bit patterns.
55b0c813ceSTimur Tabi  */
56b0c813ceSTimur Tabi #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | \
57b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
58b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
59b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
60b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
61b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
62b0c813ceSTimur Tabi 
639dbd627bSTimur Tabi #ifdef USE_I2C
64b0c813ceSTimur Tabi 
65b0c813ceSTimur Tabi /* CS4270 registers addresses */
66b0c813ceSTimur Tabi #define CS4270_CHIPID	0x01	/* Chip ID */
67b0c813ceSTimur Tabi #define CS4270_PWRCTL	0x02	/* Power Control */
68b0c813ceSTimur Tabi #define CS4270_MODE	0x03	/* Mode Control */
69b0c813ceSTimur Tabi #define CS4270_FORMAT	0x04	/* Serial Format, ADC/DAC Control */
70b0c813ceSTimur Tabi #define CS4270_TRANS	0x05	/* Transition Control */
71b0c813ceSTimur Tabi #define CS4270_MUTE	0x06	/* Mute Control */
72b0c813ceSTimur Tabi #define CS4270_VOLA	0x07	/* DAC Channel A Volume Control */
73b0c813ceSTimur Tabi #define CS4270_VOLB	0x08	/* DAC Channel B Volume Control */
74b0c813ceSTimur Tabi 
75b0c813ceSTimur Tabi #define CS4270_FIRSTREG	0x01
76b0c813ceSTimur Tabi #define CS4270_LASTREG	0x08
77b0c813ceSTimur Tabi #define CS4270_NUMREGS	(CS4270_LASTREG - CS4270_FIRSTREG + 1)
78b0c813ceSTimur Tabi 
79b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */
80b0c813ceSTimur Tabi #define CS4270_CHIPID_ID	0xF0
81b0c813ceSTimur Tabi #define CS4270_CHIPID_REV	0x0F
82b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE	0x80
83b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC	0x20
84b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC	0x02
85b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN	0x01
86b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK	0x30
87b0c813ceSTimur Tabi #define CS4270_MODE_1X		0x00
88b0c813ceSTimur Tabi #define CS4270_MODE_2X		0x10
89b0c813ceSTimur Tabi #define CS4270_MODE_4X		0x20
90b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE	0x30
91b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK	0x0E
92b0c813ceSTimur Tabi #define CS4270_MODE_DIV1	0x00
93b0c813ceSTimur Tabi #define CS4270_MODE_DIV15	0x02
94b0c813ceSTimur Tabi #define CS4270_MODE_DIV2	0x04
95b0c813ceSTimur Tabi #define CS4270_MODE_DIV3	0x06
96b0c813ceSTimur Tabi #define CS4270_MODE_DIV4	0x08
97b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD	0x01
98b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A	0x80
99b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B	0x40
100b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK	0x20
101b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK	0x18
102b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ	0x00
103b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S	0x08
104b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16	0x18
105b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24	0x10
106b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK	0x01
107b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ	0x00
108b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S	0x01
109b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL	0x80
110b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT	0x40
111b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO	0x20
112b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A	0x08
113b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B	0x10
114b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A	0x02
115b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B	0x04
116b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH	0x01
117b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO	0x20
118b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A	0x08
119b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B	0x10
120b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY	0x04
121b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A	0x01
122b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B	0x02
123b0c813ceSTimur Tabi 
124b0c813ceSTimur Tabi /*
1258432395fSTimur Tabi  * Clock Ratio Selection for Master Mode with I2C enabled
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  *
1348432395fSTimur Tabi  * Each element in this array corresponds to the ratios in mclk_ratios[].
1358432395fSTimur Tabi  * These two arrays need to be in sync.
1368432395fSTimur Tabi  *
1378432395fSTimur Tabi  * 'speed_mode' is the corresponding bit pattern to be written to the
1388432395fSTimur Tabi  * MODE bits of the Mode Control Register
1398432395fSTimur Tabi  *
1408432395fSTimur Tabi  * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
1418432395fSTimur Tabi  * the Mode Control Register.
1428432395fSTimur Tabi  *
1438432395fSTimur Tabi  * In situations where a single ratio is represented by multiple speed
1448432395fSTimur Tabi  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
1458432395fSTimur Tabi  * double-speed instead of quad-speed.  However, the CS4270 errata states
1468432395fSTimur Tabi  * that Divide-By-1.5 can cause failures, so we avoid that mode where
1478432395fSTimur Tabi  * possible.
1488432395fSTimur Tabi  *
1498432395fSTimur Tabi  * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
1508432395fSTimur Tabi  * work if VD = 3.3V.  If this effects you, select the
1518432395fSTimur Tabi  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
1528432395fSTimur Tabi  * never select any sample rates that require divide-by-1.5.
1538432395fSTimur Tabi  */
1548432395fSTimur Tabi static struct {
1558432395fSTimur Tabi 	unsigned int ratio;
1568432395fSTimur Tabi 	u8 speed_mode;
1578432395fSTimur Tabi 	u8 mclk;
1588432395fSTimur Tabi } cs4270_mode_ratios[] = {
1598432395fSTimur Tabi 	{64, CS4270_MODE_4X, CS4270_MODE_DIV1},
1608432395fSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
1618432395fSTimur Tabi 	{96, CS4270_MODE_4X, CS4270_MODE_DIV15},
1628432395fSTimur Tabi #endif
1638432395fSTimur Tabi 	{128, CS4270_MODE_2X, CS4270_MODE_DIV1},
1648432395fSTimur Tabi 	{192, CS4270_MODE_4X, CS4270_MODE_DIV3},
1658432395fSTimur Tabi 	{256, CS4270_MODE_1X, CS4270_MODE_DIV1},
1668432395fSTimur Tabi 	{384, CS4270_MODE_2X, CS4270_MODE_DIV3},
1678432395fSTimur Tabi 	{512, CS4270_MODE_1X, CS4270_MODE_DIV2},
1688432395fSTimur Tabi 	{768, CS4270_MODE_1X, CS4270_MODE_DIV3},
1698432395fSTimur Tabi 	{1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
1708432395fSTimur Tabi };
1718432395fSTimur Tabi 
1728432395fSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */
1738432395fSTimur Tabi #define NUM_MCLK_RATIOS		ARRAY_SIZE(cs4270_mode_ratios)
1748432395fSTimur Tabi 
1758432395fSTimur Tabi /*
1768432395fSTimur Tabi  * Determine the CS4270 samples rates.
1778432395fSTimur Tabi  *
1788432395fSTimur Tabi  * 'freq' is the input frequency to MCLK.  The other parameters are ignored.
1798432395fSTimur Tabi  *
1808432395fSTimur Tabi  * The value of MCLK is used to determine which sample rates are supported
1818432395fSTimur Tabi  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
1828432395fSTimur Tabi  * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
1838432395fSTimur Tabi  *
1848432395fSTimur Tabi  * This function calculates the nine ratios and determines which ones match
1858432395fSTimur Tabi  * a standard sample rate.  If there's a match, then it is added to the list
1868432395fSTimur Tabi  * of support sample rates.
1878432395fSTimur Tabi  *
1888432395fSTimur Tabi  * This function must be called by the machine driver's 'startup' function,
1898432395fSTimur Tabi  * otherwise the list of supported sample rates will not be available in
1908432395fSTimur Tabi  * time for ALSA.
1918432395fSTimur Tabi  *
1928432395fSTimur Tabi  * Note that in stand-alone mode, the sample rate is determined by input
1938432395fSTimur Tabi  * pins M0, M1, MDIV1, and MDIV2.  Also in stand-alone mode, divide-by-3
1948432395fSTimur Tabi  * is not a programmable option.  However, divide-by-3 is not an available
1958432395fSTimur Tabi  * option in stand-alone mode.  This cases two problems: a ratio of 768 is
1968432395fSTimur Tabi  * not available (it requires divide-by-3) and B) ratios 192 and 384 can
1978432395fSTimur Tabi  * only be selected with divide-by-1.5, but there is an errate that make
1988432395fSTimur Tabi  * this selection difficult.
1998432395fSTimur Tabi  *
2008432395fSTimur Tabi  * In addition, there is no mechanism for communicating with the machine
2018432395fSTimur Tabi  * driver what the input settings can be.  This would need to be implemented
2028432395fSTimur Tabi  * for stand-alone mode to work.
2038432395fSTimur Tabi  */
204e550e17fSLiam Girdwood static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2058432395fSTimur Tabi 				 int clk_id, unsigned int freq, int dir)
2068432395fSTimur Tabi {
2078432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
2088432395fSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
2098432395fSTimur Tabi 	unsigned int rates = 0;
2108432395fSTimur Tabi 	unsigned int rate_min = -1;
2118432395fSTimur Tabi 	unsigned int rate_max = 0;
2128432395fSTimur Tabi 	unsigned int i;
2138432395fSTimur Tabi 
2148432395fSTimur Tabi 	cs4270->mclk = freq;
2158432395fSTimur Tabi 
2168432395fSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
2178432395fSTimur Tabi 		unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
2188432395fSTimur Tabi 		rates |= snd_pcm_rate_to_rate_bit(rate);
2198432395fSTimur Tabi 		if (rate < rate_min)
2208432395fSTimur Tabi 			rate_min = rate;
2218432395fSTimur Tabi 		if (rate > rate_max)
2228432395fSTimur Tabi 			rate_max = rate;
2238432395fSTimur Tabi 	}
2248432395fSTimur Tabi 	/* FIXME: soc should support a rate list */
2258432395fSTimur Tabi 	rates &= ~SNDRV_PCM_RATE_KNOT;
2268432395fSTimur Tabi 
2278432395fSTimur Tabi 	if (!rates) {
2288432395fSTimur Tabi 		printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
2298432395fSTimur Tabi 		return -EINVAL;
2308432395fSTimur Tabi 	}
2318432395fSTimur Tabi 
2328432395fSTimur Tabi 	codec_dai->playback.rates = rates;
2338432395fSTimur Tabi 	codec_dai->playback.rate_min = rate_min;
2348432395fSTimur Tabi 	codec_dai->playback.rate_max = rate_max;
2358432395fSTimur Tabi 
2368432395fSTimur Tabi 	codec_dai->capture.rates = rates;
2378432395fSTimur Tabi 	codec_dai->capture.rate_min = rate_min;
2388432395fSTimur Tabi 	codec_dai->capture.rate_max = rate_max;
2398432395fSTimur Tabi 
2408432395fSTimur Tabi 	return 0;
2418432395fSTimur Tabi }
2428432395fSTimur Tabi 
2438432395fSTimur Tabi /*
2448432395fSTimur Tabi  * Configure the codec for the selected audio format
2458432395fSTimur Tabi  *
2468432395fSTimur Tabi  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
2478432395fSTimur Tabi  * codec accordingly.
2488432395fSTimur Tabi  *
2498432395fSTimur Tabi  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
2508432395fSTimur Tabi  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
2518432395fSTimur Tabi  * data for playback only, but ASoC currently does not support different
2528432395fSTimur Tabi  * formats for playback vs. record.
2538432395fSTimur Tabi  */
254e550e17fSLiam Girdwood static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
2558432395fSTimur Tabi 			      unsigned int format)
2568432395fSTimur Tabi {
2578432395fSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
2588432395fSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
2598432395fSTimur Tabi 	int ret = 0;
2608432395fSTimur Tabi 
2618432395fSTimur Tabi 	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
2628432395fSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
2638432395fSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
2648432395fSTimur Tabi 		cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
2658432395fSTimur Tabi 		break;
2668432395fSTimur Tabi 	default:
2678432395fSTimur Tabi 		printk(KERN_ERR "cs4270: invalid DAI format\n");
2688432395fSTimur Tabi 		ret = -EINVAL;
2698432395fSTimur Tabi 	}
2708432395fSTimur Tabi 
2718432395fSTimur Tabi 	return ret;
2728432395fSTimur Tabi }
2738432395fSTimur Tabi 
2748432395fSTimur Tabi /*
275b0c813ceSTimur Tabi  * A list of addresses on which this CS4270 could use.  I2C addresses are
276b0c813ceSTimur Tabi  * 7 bits.  For the CS4270, the upper four bits are always 1001, and the
277b0c813ceSTimur Tabi  * lower three bits are determined via the AD2, AD1, and AD0 pins
278b0c813ceSTimur Tabi  * (respectively).
279b0c813ceSTimur Tabi  */
2802cdddeb8SJean Delvare static const unsigned short normal_i2c[] = {
281b0c813ceSTimur Tabi 	0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END
282b0c813ceSTimur Tabi };
283b0c813ceSTimur Tabi I2C_CLIENT_INSMOD;
284b0c813ceSTimur Tabi 
285b0c813ceSTimur Tabi /*
286b0c813ceSTimur Tabi  * Pre-fill the CS4270 register cache.
287b0c813ceSTimur Tabi  *
288b0c813ceSTimur Tabi  * We use the auto-increment feature of the CS4270 to read all registers in
289b0c813ceSTimur Tabi  * one shot.
290b0c813ceSTimur Tabi  */
291b0c813ceSTimur Tabi static int cs4270_fill_cache(struct snd_soc_codec *codec)
292b0c813ceSTimur Tabi {
293b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
294b0c813ceSTimur Tabi 	struct i2c_client *i2c_client = codec->control_data;
295b0c813ceSTimur Tabi 	s32 length;
296b0c813ceSTimur Tabi 
297b0c813ceSTimur Tabi 	length = i2c_smbus_read_i2c_block_data(i2c_client,
298b0c813ceSTimur Tabi 		CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
299b0c813ceSTimur Tabi 
300b0c813ceSTimur Tabi 	if (length != CS4270_NUMREGS) {
3019dbd627bSTimur Tabi 		printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
302b0c813ceSTimur Tabi 		       i2c_client->addr);
303b0c813ceSTimur Tabi 		return -EIO;
304b0c813ceSTimur Tabi 	}
305b0c813ceSTimur Tabi 
306b0c813ceSTimur Tabi 	return 0;
307b0c813ceSTimur Tabi }
308b0c813ceSTimur Tabi 
309b0c813ceSTimur Tabi /*
310b0c813ceSTimur Tabi  * Read from the CS4270 register cache.
311b0c813ceSTimur Tabi  *
312b0c813ceSTimur Tabi  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
313b0c813ceSTimur Tabi  * After the initial read to pre-fill the cache, the CS4270 never updates
314b0c813ceSTimur Tabi  * the register values, so we won't have a cache coherncy problem.
315b0c813ceSTimur Tabi  */
316b0c813ceSTimur Tabi static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
317b0c813ceSTimur Tabi 	unsigned int reg)
318b0c813ceSTimur Tabi {
319b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
320b0c813ceSTimur Tabi 
321b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
322b0c813ceSTimur Tabi 		return -EIO;
323b0c813ceSTimur Tabi 
324b0c813ceSTimur Tabi 	return cache[reg - CS4270_FIRSTREG];
325b0c813ceSTimur Tabi }
326b0c813ceSTimur Tabi 
327b0c813ceSTimur Tabi /*
328b0c813ceSTimur Tabi  * Write to a CS4270 register via the I2C bus.
329b0c813ceSTimur Tabi  *
330b0c813ceSTimur Tabi  * This function writes the given value to the given CS4270 register, and
331b0c813ceSTimur Tabi  * also updates the register cache.
332b0c813ceSTimur Tabi  *
333b0c813ceSTimur Tabi  * Note that we don't use the hw_write function pointer of snd_soc_codec.
334b0c813ceSTimur Tabi  * That's because it's too clunky: the hw_write_t prototype does not match
335b0c813ceSTimur Tabi  * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
336b0c813ceSTimur Tabi  */
337b0c813ceSTimur Tabi static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
338b0c813ceSTimur Tabi 			    unsigned int value)
339b0c813ceSTimur Tabi {
340bfc4e861STimur Tabi 	u8 *cache = codec->reg_cache;
341bfc4e861STimur Tabi 
342b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
343b0c813ceSTimur Tabi 		return -EIO;
344b0c813ceSTimur Tabi 
345bfc4e861STimur Tabi 	/* Only perform an I2C operation if the new value is different */
346bfc4e861STimur Tabi 	if (cache[reg - CS4270_FIRSTREG] != value) {
347bfc4e861STimur Tabi 		struct i2c_client *client = codec->control_data;
348bfc4e861STimur Tabi 		if (i2c_smbus_write_byte_data(client, reg, value)) {
349bfc4e861STimur Tabi 			printk(KERN_ERR "cs4270: I2C write failed\n");
350b0c813ceSTimur Tabi 			return -EIO;
351b0c813ceSTimur Tabi 		}
352bfc4e861STimur Tabi 
353bfc4e861STimur Tabi 		/* We've written to the hardware, so update the cache */
354bfc4e861STimur Tabi 		cache[reg - CS4270_FIRSTREG] = value;
355bfc4e861STimur Tabi 	}
356bfc4e861STimur Tabi 
357bfc4e861STimur Tabi 	return 0;
358b0c813ceSTimur Tabi }
359b0c813ceSTimur Tabi 
360b0c813ceSTimur Tabi /*
361b0c813ceSTimur Tabi  * Program the CS4270 with the given hardware parameters.
362b0c813ceSTimur Tabi  *
363*dee89c4dSMark Brown  * The .ops functions are used to provide board-specific data, like
364b0c813ceSTimur Tabi  * input frequencies, to this driver.  This function takes that information,
365b0c813ceSTimur Tabi  * combines it with the hardware parameters provided, and programs the
366b0c813ceSTimur Tabi  * hardware accordingly.
367b0c813ceSTimur Tabi  */
368b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream,
369*dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
370*dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
371b0c813ceSTimur Tabi {
372b0c813ceSTimur Tabi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
373b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = rtd->socdev;
374b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = socdev->codec;
375b0c813ceSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
376e34ba212SRoel Kluin 	int ret;
377b0c813ceSTimur Tabi 	unsigned int i;
378b0c813ceSTimur Tabi 	unsigned int rate;
379b0c813ceSTimur Tabi 	unsigned int ratio;
380b0c813ceSTimur Tabi 	int reg;
381b0c813ceSTimur Tabi 
382b0c813ceSTimur Tabi 	/* Figure out which MCLK/LRCK ratio to use */
383b0c813ceSTimur Tabi 
384b0c813ceSTimur Tabi 	rate = params_rate(params);	/* Sampling rate, in Hz */
385b0c813ceSTimur Tabi 	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */
386b0c813ceSTimur Tabi 
3879dbd627bSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
3888432395fSTimur Tabi 		if (cs4270_mode_ratios[i].ratio == ratio)
389b0c813ceSTimur Tabi 			break;
390b0c813ceSTimur Tabi 	}
391b0c813ceSTimur Tabi 
3929dbd627bSTimur Tabi 	if (i == NUM_MCLK_RATIOS) {
393b0c813ceSTimur Tabi 		/* We did not find a matching ratio */
394b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not find matching ratio\n");
395b0c813ceSTimur Tabi 		return -EINVAL;
396b0c813ceSTimur Tabi 	}
397b0c813ceSTimur Tabi 
398b0c813ceSTimur Tabi 	/* Freeze and power-down the codec */
399b0c813ceSTimur Tabi 
400b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
401b0c813ceSTimur Tabi 			    CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
402b0c813ceSTimur Tabi 			    CS4270_PWRCTL_PDN);
403b0c813ceSTimur Tabi 	if (ret < 0) {
404b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
405b0c813ceSTimur Tabi 		return ret;
406b0c813ceSTimur Tabi 	}
407b0c813ceSTimur Tabi 
408b0c813ceSTimur Tabi 	/* Program the mode control register */
409b0c813ceSTimur Tabi 
410b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MODE);
411b0c813ceSTimur Tabi 	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
412b0c813ceSTimur Tabi 	reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
413b0c813ceSTimur Tabi 
414b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MODE, reg);
415b0c813ceSTimur Tabi 	if (ret < 0) {
416b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
417b0c813ceSTimur Tabi 		return ret;
418b0c813ceSTimur Tabi 	}
419b0c813ceSTimur Tabi 
420b0c813ceSTimur Tabi 	/* Program the format register */
421b0c813ceSTimur Tabi 
422b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_FORMAT);
423b0c813ceSTimur Tabi 	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
424b0c813ceSTimur Tabi 
425b0c813ceSTimur Tabi 	switch (cs4270->mode) {
426b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
427b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
428b0c813ceSTimur Tabi 		break;
429b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
430b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
431b0c813ceSTimur Tabi 		break;
432b0c813ceSTimur Tabi 	default:
433b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: unknown format\n");
434b0c813ceSTimur Tabi 		return -EINVAL;
435b0c813ceSTimur Tabi 	}
436b0c813ceSTimur Tabi 
437b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_FORMAT, reg);
438b0c813ceSTimur Tabi 	if (ret < 0) {
439b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
440b0c813ceSTimur Tabi 		return ret;
441b0c813ceSTimur Tabi 	}
442b0c813ceSTimur Tabi 
443b0c813ceSTimur Tabi 	/* Disable auto-mute.  This feature appears to be buggy, because in
444b0c813ceSTimur Tabi 	   some situations, auto-mute will not deactivate when it should. */
445b0c813ceSTimur Tabi 
446b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MUTE);
447b0c813ceSTimur Tabi 	reg &= ~CS4270_MUTE_AUTO;
448b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MUTE, reg);
449b0c813ceSTimur Tabi 	if (ret < 0) {
450b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
451b0c813ceSTimur Tabi 		return ret;
452b0c813ceSTimur Tabi 	}
453b0c813ceSTimur Tabi 
4540c235d1eSTimur Tabi 	/* Disable automatic volume control.  It's enabled by default, and
4550c235d1eSTimur Tabi 	 * it causes volume change commands to be delayed, sometimes until
4560c235d1eSTimur Tabi 	 * after playback has started.
4570c235d1eSTimur Tabi 	 */
4580c235d1eSTimur Tabi 
4590c235d1eSTimur Tabi 	reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
4600c235d1eSTimur Tabi 	reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
4610c235d1eSTimur Tabi 	ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
4620c235d1eSTimur Tabi 	if (ret < 0) {
4630c235d1eSTimur Tabi 		printk(KERN_ERR "I2C write failed\n");
4640c235d1eSTimur Tabi 		return ret;
4650c235d1eSTimur Tabi 	}
4660c235d1eSTimur Tabi 
467b0c813ceSTimur Tabi 	/* Thaw and power-up the codec */
468b0c813ceSTimur Tabi 
469b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
470b0c813ceSTimur Tabi 	if (ret < 0) {
471b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
472b0c813ceSTimur Tabi 		return ret;
473b0c813ceSTimur Tabi 	}
474b0c813ceSTimur Tabi 
475b0c813ceSTimur Tabi 	return ret;
476b0c813ceSTimur Tabi }
477b0c813ceSTimur Tabi 
478b0c813ceSTimur Tabi #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
479b0c813ceSTimur Tabi 
480b0c813ceSTimur Tabi /*
481b0c813ceSTimur Tabi  * Set the CS4270 external mute
482b0c813ceSTimur Tabi  *
483b0c813ceSTimur Tabi  * This function toggles the mute bits in the MUTE register.  The CS4270's
484b0c813ceSTimur Tabi  * mute capability is intended for external muting circuitry, so if the
485b0c813ceSTimur Tabi  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
486b0c813ceSTimur Tabi  * then this function will do nothing.
487b0c813ceSTimur Tabi  */
488e550e17fSLiam Girdwood static int cs4270_mute(struct snd_soc_dai *dai, int mute)
489b0c813ceSTimur Tabi {
490b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = dai->codec;
491b0c813ceSTimur Tabi 	int reg6;
492b0c813ceSTimur Tabi 
493b0c813ceSTimur Tabi 	reg6 = snd_soc_read(codec, CS4270_MUTE);
494b0c813ceSTimur Tabi 
495b0c813ceSTimur Tabi 	if (mute)
496b0c813ceSTimur Tabi 		reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
497b0c813ceSTimur Tabi 			CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
498b0c813ceSTimur Tabi 	else
499b0c813ceSTimur Tabi 		reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
500b0c813ceSTimur Tabi 			  CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
501b0c813ceSTimur Tabi 
502b0c813ceSTimur Tabi 	return snd_soc_write(codec, CS4270_MUTE, reg6);
503b0c813ceSTimur Tabi }
504b0c813ceSTimur Tabi 
505b0c813ceSTimur Tabi #endif
506b0c813ceSTimur Tabi 
507ec2cd95fSTimur Tabi static int cs4270_i2c_probe(struct i2c_client *, const struct i2c_device_id *);
508b0c813ceSTimur Tabi 
509b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */
510b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = {
511b0c813ceSTimur Tabi 	SOC_DOUBLE_R("Master Playback Volume",
512bfc4e861STimur Tabi 		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
513b0c813ceSTimur Tabi };
514b0c813ceSTimur Tabi 
515ec2cd95fSTimur Tabi static const struct i2c_device_id cs4270_id[] = {
516ec2cd95fSTimur Tabi 	{"cs4270", 0},
517ec2cd95fSTimur Tabi 	{}
518ec2cd95fSTimur Tabi };
519ec2cd95fSTimur Tabi MODULE_DEVICE_TABLE(i2c, cs4270_id);
520ec2cd95fSTimur Tabi 
521b0c813ceSTimur Tabi static struct i2c_driver cs4270_i2c_driver = {
522b0c813ceSTimur Tabi 	.driver = {
523b0c813ceSTimur Tabi 		.name = "CS4270 I2C",
524b0c813ceSTimur Tabi 		.owner = THIS_MODULE,
525b0c813ceSTimur Tabi 	},
526ec2cd95fSTimur Tabi 	.id_table = cs4270_id,
527ec2cd95fSTimur Tabi 	.probe = cs4270_i2c_probe,
528b0c813ceSTimur Tabi };
529b0c813ceSTimur Tabi 
530b0c813ceSTimur Tabi /*
531b0c813ceSTimur Tabi  * Global variable to store socdev for i2c probe function.
532b0c813ceSTimur Tabi  *
533b0c813ceSTimur Tabi  * If struct i2c_driver had a private_data field, we wouldn't need to use
534b0c813ceSTimur Tabi  * cs4270_socdec.  This is the only way to pass the socdev structure to
535b0c813ceSTimur Tabi  * cs4270_i2c_probe().
536b0c813ceSTimur Tabi  *
537b0c813ceSTimur Tabi  * The real solution to cs4270_socdev is to create a mechanism
538b0c813ceSTimur Tabi  * that maps I2C addresses to snd_soc_device structures.  Perhaps the
539b0c813ceSTimur Tabi  * creation of the snd_soc_device object should be moved out of
540b0c813ceSTimur Tabi  * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
541b0c813ceSTimur Tabi  * driver dependent on I2C.  The CS4270 supports "stand-alone" mode, whereby
542b0c813ceSTimur Tabi  * the chip is *not* connected to the I2C bus, but is instead configured via
543b0c813ceSTimur Tabi  * input pins.
544b0c813ceSTimur Tabi  */
545b0c813ceSTimur Tabi static struct snd_soc_device *cs4270_socdev;
546b0c813ceSTimur Tabi 
547b0c813ceSTimur Tabi /*
548b0c813ceSTimur Tabi  * Initialize the I2C interface of the CS4270
549b0c813ceSTimur Tabi  *
550b0c813ceSTimur Tabi  * This function is called for whenever the I2C subsystem finds a device
551b0c813ceSTimur Tabi  * at a particular address.
552b0c813ceSTimur Tabi  *
553b0c813ceSTimur Tabi  * Note: snd_soc_new_pcms() must be called before this function can be called,
554b0c813ceSTimur Tabi  * because of snd_ctl_add().
555b0c813ceSTimur Tabi  */
556ec2cd95fSTimur Tabi static int cs4270_i2c_probe(struct i2c_client *i2c_client,
557ec2cd95fSTimur Tabi 	const struct i2c_device_id *id)
558b0c813ceSTimur Tabi {
559b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = cs4270_socdev;
560b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = socdev->codec;
561b0c813ceSTimur Tabi 	int i;
562b0c813ceSTimur Tabi 	int ret = 0;
563b0c813ceSTimur Tabi 
564b0c813ceSTimur Tabi 	/* Probing all possible addresses has one drawback: if there are
565b0c813ceSTimur Tabi 	   multiple CS4270s on the bus, then you cannot specify which
566b0c813ceSTimur Tabi 	   socdev is matched with which CS4270.  For now, we just reject
567b0c813ceSTimur Tabi 	   this I2C device if the socdev already has one attached. */
568b0c813ceSTimur Tabi 	if (codec->control_data)
569b0c813ceSTimur Tabi 		return -ENODEV;
570b0c813ceSTimur Tabi 
571b0c813ceSTimur Tabi 	/* Note: codec_dai->codec is NULL here */
572b0c813ceSTimur Tabi 
573b0c813ceSTimur Tabi 	codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
574b0c813ceSTimur Tabi 	if (!codec->reg_cache) {
575b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not allocate register cache\n");
576b0c813ceSTimur Tabi 		ret = -ENOMEM;
577b0c813ceSTimur Tabi 		goto error;
578b0c813ceSTimur Tabi 	}
579b0c813ceSTimur Tabi 
580b0c813ceSTimur Tabi 	/* Verify that we have a CS4270 */
581b0c813ceSTimur Tabi 
582b0c813ceSTimur Tabi 	ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
583b0c813ceSTimur Tabi 	if (ret < 0) {
584b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to read I2C\n");
585b0c813ceSTimur Tabi 		goto error;
586b0c813ceSTimur Tabi 	}
587b0c813ceSTimur Tabi 	/* The top four bits of the chip ID should be 1100. */
588b0c813ceSTimur Tabi 	if ((ret & 0xF0) != 0xC0) {
589b0c813ceSTimur Tabi 		/* The device at this address is not a CS4270 codec */
590b0c813ceSTimur Tabi 		ret = -ENODEV;
591b0c813ceSTimur Tabi 		goto error;
592b0c813ceSTimur Tabi 	}
593b0c813ceSTimur Tabi 
594ec2cd95fSTimur Tabi 	printk(KERN_INFO "cs4270: found device at I2C address %X\n",
595ec2cd95fSTimur Tabi 		i2c_client->addr);
596b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
597b0c813ceSTimur Tabi 
598b0c813ceSTimur Tabi 	codec->control_data = i2c_client;
599b0c813ceSTimur Tabi 	codec->read = cs4270_read_reg_cache;
600b0c813ceSTimur Tabi 	codec->write = cs4270_i2c_write;
601b0c813ceSTimur Tabi 	codec->reg_cache_size = CS4270_NUMREGS;
602b0c813ceSTimur Tabi 
603b0c813ceSTimur Tabi 	/* The I2C interface is set up, so pre-fill our register cache */
604b0c813ceSTimur Tabi 
605b0c813ceSTimur Tabi 	ret = cs4270_fill_cache(codec);
606b0c813ceSTimur Tabi 	if (ret < 0) {
607b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to fill register cache\n");
608b0c813ceSTimur Tabi 		goto error;
609b0c813ceSTimur Tabi 	}
610b0c813ceSTimur Tabi 
611b0c813ceSTimur Tabi 	/* Add the non-DAPM controls */
612b0c813ceSTimur Tabi 
613b0c813ceSTimur Tabi 	for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
614b0c813ceSTimur Tabi 		struct snd_kcontrol *kctrl =
615b0c813ceSTimur Tabi 		snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
616b0c813ceSTimur Tabi 
617b0c813ceSTimur Tabi 		ret = snd_ctl_add(codec->card, kctrl);
618b0c813ceSTimur Tabi 		if (ret < 0)
619b0c813ceSTimur Tabi 			goto error;
620b0c813ceSTimur Tabi 	}
621b0c813ceSTimur Tabi 
622ec2cd95fSTimur Tabi 	i2c_set_clientdata(i2c_client, codec);
623ec2cd95fSTimur Tabi 
624b0c813ceSTimur Tabi 	return 0;
625b0c813ceSTimur Tabi 
626b0c813ceSTimur Tabi error:
627b0c813ceSTimur Tabi 	codec->control_data = NULL;
628b0c813ceSTimur Tabi 
629b0c813ceSTimur Tabi 	kfree(codec->reg_cache);
630b0c813ceSTimur Tabi 	codec->reg_cache = NULL;
631b0c813ceSTimur Tabi 	codec->reg_cache_size = 0;
632b0c813ceSTimur Tabi 
633b0c813ceSTimur Tabi 	return ret;
634b0c813ceSTimur Tabi }
635b0c813ceSTimur Tabi 
6368432395fSTimur Tabi #endif /* USE_I2C*/
637b0c813ceSTimur Tabi 
638e550e17fSLiam Girdwood struct snd_soc_dai cs4270_dai = {
639b0c813ceSTimur Tabi 	.name = "CS4270",
640b0c813ceSTimur Tabi 	.playback = {
641b0c813ceSTimur Tabi 		.stream_name = "Playback",
642b0c813ceSTimur Tabi 		.channels_min = 1,
643b0c813ceSTimur Tabi 		.channels_max = 2,
644b0c813ceSTimur Tabi 		.rates = 0,
645b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
646b0c813ceSTimur Tabi 	},
647b0c813ceSTimur Tabi 	.capture = {
648b0c813ceSTimur Tabi 		.stream_name = "Capture",
649b0c813ceSTimur Tabi 		.channels_min = 1,
650b0c813ceSTimur Tabi 		.channels_max = 2,
651b0c813ceSTimur Tabi 		.rates = 0,
652b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
653b0c813ceSTimur Tabi 	},
654b0c813ceSTimur Tabi };
655b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(cs4270_dai);
656b0c813ceSTimur Tabi 
657b0c813ceSTimur Tabi /*
658b0c813ceSTimur Tabi  * ASoC probe function
659b0c813ceSTimur Tabi  *
660b0c813ceSTimur Tabi  * This function is called when the machine driver calls
661b0c813ceSTimur Tabi  * platform_device_add().
662b0c813ceSTimur Tabi  */
663b0c813ceSTimur Tabi static int cs4270_probe(struct platform_device *pdev)
664b0c813ceSTimur Tabi {
665b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
666b0c813ceSTimur Tabi 	struct snd_soc_codec *codec;
667b0c813ceSTimur Tabi 	int ret = 0;
668b0c813ceSTimur Tabi 
669b0c813ceSTimur Tabi 	printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
670b0c813ceSTimur Tabi 
671b0c813ceSTimur Tabi 	/* Allocate enough space for the snd_soc_codec structure
672b0c813ceSTimur Tabi 	   and our private data together. */
673b0c813ceSTimur Tabi 	codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
674b0c813ceSTimur Tabi 			sizeof(struct cs4270_private), GFP_KERNEL);
675b0c813ceSTimur Tabi 	if (!codec) {
676b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
677b0c813ceSTimur Tabi 		return -ENOMEM;
678b0c813ceSTimur Tabi 	}
679b0c813ceSTimur Tabi 
680b0c813ceSTimur Tabi 	mutex_init(&codec->mutex);
681b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_widgets);
682b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_paths);
683b0c813ceSTimur Tabi 
684b0c813ceSTimur Tabi 	codec->name = "CS4270";
685b0c813ceSTimur Tabi 	codec->owner = THIS_MODULE;
686b0c813ceSTimur Tabi 	codec->dai = &cs4270_dai;
687b0c813ceSTimur Tabi 	codec->num_dai = 1;
6884df20535STimur Tabi 	codec->private_data = (void *) codec +
6894df20535STimur Tabi 		ALIGN(sizeof(struct snd_soc_codec), 4);
690b0c813ceSTimur Tabi 
691b0c813ceSTimur Tabi 	socdev->codec = codec;
692b0c813ceSTimur Tabi 
693b0c813ceSTimur Tabi 	/* Register PCMs */
694b0c813ceSTimur Tabi 
695b0c813ceSTimur Tabi 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
696b0c813ceSTimur Tabi 	if (ret < 0) {
697b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to create PCMs\n");
698e3145dfbSJean Delvare 		goto error_free_codec;
699b0c813ceSTimur Tabi 	}
700b0c813ceSTimur Tabi 
7019dbd627bSTimur Tabi #ifdef USE_I2C
702b0c813ceSTimur Tabi 	cs4270_socdev = socdev;
703b0c813ceSTimur Tabi 
704b0c813ceSTimur Tabi 	ret = i2c_add_driver(&cs4270_i2c_driver);
705b0c813ceSTimur Tabi 	if (ret) {
706b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to attach driver");
707e3145dfbSJean Delvare 		goto error_free_pcms;
708b0c813ceSTimur Tabi 	}
709b0c813ceSTimur Tabi 
710b0c813ceSTimur Tabi 	/* Did we find a CS4270 on the I2C bus? */
711b0c813ceSTimur Tabi 	if (codec->control_data) {
712b0c813ceSTimur Tabi 		/* Initialize codec ops */
713b0c813ceSTimur Tabi 		cs4270_dai.ops.hw_params = cs4270_hw_params;
714*dee89c4dSMark Brown 		cs4270_dai.ops.set_sysclk = cs4270_set_dai_sysclk;
715*dee89c4dSMark Brown 		cs4270_dai.ops.set_fmt = cs4270_set_dai_fmt;
716b0c813ceSTimur Tabi #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
717*dee89c4dSMark Brown 		cs4270_dai.ops.digital_mute = cs4270_mute;
718b0c813ceSTimur Tabi #endif
719b0c813ceSTimur Tabi 	} else
720b0c813ceSTimur Tabi 		printk(KERN_INFO "cs4270: no I2C device found, "
721b0c813ceSTimur Tabi 			"using stand-alone mode\n");
722b0c813ceSTimur Tabi #else
723b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
724b0c813ceSTimur Tabi #endif
725b0c813ceSTimur Tabi 
726b0c813ceSTimur Tabi 	ret = snd_soc_register_card(socdev);
727b0c813ceSTimur Tabi 	if (ret < 0) {
728b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to register card\n");
729e3145dfbSJean Delvare 		goto error_del_driver;
730b0c813ceSTimur Tabi 	}
731b0c813ceSTimur Tabi 
732e3145dfbSJean Delvare 	return 0;
733e3145dfbSJean Delvare 
734e3145dfbSJean Delvare error_del_driver:
735e3145dfbSJean Delvare #ifdef USE_I2C
736e3145dfbSJean Delvare 	i2c_del_driver(&cs4270_i2c_driver);
737e3145dfbSJean Delvare 
738e3145dfbSJean Delvare error_free_pcms:
739e3145dfbSJean Delvare #endif
740e3145dfbSJean Delvare 	snd_soc_free_pcms(socdev);
741e3145dfbSJean Delvare 
742e3145dfbSJean Delvare error_free_codec:
743e3145dfbSJean Delvare 	kfree(socdev->codec);
744e3145dfbSJean Delvare 	socdev->codec = NULL;
745e3145dfbSJean Delvare 
746b0c813ceSTimur Tabi 	return ret;
747b0c813ceSTimur Tabi }
748b0c813ceSTimur Tabi 
749b0c813ceSTimur Tabi static int cs4270_remove(struct platform_device *pdev)
750b0c813ceSTimur Tabi {
751b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
752b0c813ceSTimur Tabi 
753b0c813ceSTimur Tabi 	snd_soc_free_pcms(socdev);
754b0c813ceSTimur Tabi 
7559dbd627bSTimur Tabi #ifdef USE_I2C
756b0c813ceSTimur Tabi 	i2c_del_driver(&cs4270_i2c_driver);
757b0c813ceSTimur Tabi #endif
758b0c813ceSTimur Tabi 
759b0c813ceSTimur Tabi 	kfree(socdev->codec);
760b0c813ceSTimur Tabi 	socdev->codec = NULL;
761b0c813ceSTimur Tabi 
762b0c813ceSTimur Tabi 	return 0;
763b0c813ceSTimur Tabi }
764b0c813ceSTimur Tabi 
765b0c813ceSTimur Tabi /*
766b0c813ceSTimur Tabi  * ASoC codec device structure
767b0c813ceSTimur Tabi  *
768b0c813ceSTimur Tabi  * Assign this variable to the codec_dev field of the machine driver's
769b0c813ceSTimur Tabi  * snd_soc_device structure.
770b0c813ceSTimur Tabi  */
771b0c813ceSTimur Tabi struct snd_soc_codec_device soc_codec_device_cs4270 = {
772b0c813ceSTimur Tabi 	.probe = 	cs4270_probe,
773b0c813ceSTimur Tabi 	.remove = 	cs4270_remove
774b0c813ceSTimur Tabi };
775b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
776b0c813ceSTimur Tabi 
777b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
778b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
779b0c813ceSTimur Tabi MODULE_LICENSE("GPL");
780