xref: /linux/sound/soc/codecs/cs4270.c (revision e34ba212225a27cdf5f974be22cc539ae7ee7ca5)
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  */
2048432395fSTimur Tabi static int cs4270_set_dai_sysclk(struct snd_soc_codec_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  */
2548432395fSTimur Tabi static int cs4270_set_dai_fmt(struct snd_soc_codec_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  *
363b0c813ceSTimur Tabi  * The .dai_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,
369b0c813ceSTimur Tabi 			    struct snd_pcm_hw_params *params)
370b0c813ceSTimur Tabi {
371b0c813ceSTimur Tabi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
372b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = rtd->socdev;
373b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = socdev->codec;
374b0c813ceSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
375*e34ba212SRoel Kluin 	int ret;
376b0c813ceSTimur Tabi 	unsigned int i;
377b0c813ceSTimur Tabi 	unsigned int rate;
378b0c813ceSTimur Tabi 	unsigned int ratio;
379b0c813ceSTimur Tabi 	int reg;
380b0c813ceSTimur Tabi 
381b0c813ceSTimur Tabi 	/* Figure out which MCLK/LRCK ratio to use */
382b0c813ceSTimur Tabi 
383b0c813ceSTimur Tabi 	rate = params_rate(params);	/* Sampling rate, in Hz */
384b0c813ceSTimur Tabi 	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */
385b0c813ceSTimur Tabi 
3869dbd627bSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
3878432395fSTimur Tabi 		if (cs4270_mode_ratios[i].ratio == ratio)
388b0c813ceSTimur Tabi 			break;
389b0c813ceSTimur Tabi 	}
390b0c813ceSTimur Tabi 
3919dbd627bSTimur Tabi 	if (i == NUM_MCLK_RATIOS) {
392b0c813ceSTimur Tabi 		/* We did not find a matching ratio */
393b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not find matching ratio\n");
394b0c813ceSTimur Tabi 		return -EINVAL;
395b0c813ceSTimur Tabi 	}
396b0c813ceSTimur Tabi 
397b0c813ceSTimur Tabi 	/* Freeze and power-down the codec */
398b0c813ceSTimur Tabi 
399b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
400b0c813ceSTimur Tabi 			    CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
401b0c813ceSTimur Tabi 			    CS4270_PWRCTL_PDN);
402b0c813ceSTimur Tabi 	if (ret < 0) {
403b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
404b0c813ceSTimur Tabi 		return ret;
405b0c813ceSTimur Tabi 	}
406b0c813ceSTimur Tabi 
407b0c813ceSTimur Tabi 	/* Program the mode control register */
408b0c813ceSTimur Tabi 
409b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MODE);
410b0c813ceSTimur Tabi 	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
411b0c813ceSTimur Tabi 	reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
412b0c813ceSTimur Tabi 
413b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MODE, reg);
414b0c813ceSTimur Tabi 	if (ret < 0) {
415b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
416b0c813ceSTimur Tabi 		return ret;
417b0c813ceSTimur Tabi 	}
418b0c813ceSTimur Tabi 
419b0c813ceSTimur Tabi 	/* Program the format register */
420b0c813ceSTimur Tabi 
421b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_FORMAT);
422b0c813ceSTimur Tabi 	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
423b0c813ceSTimur Tabi 
424b0c813ceSTimur Tabi 	switch (cs4270->mode) {
425b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
426b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
427b0c813ceSTimur Tabi 		break;
428b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
429b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
430b0c813ceSTimur Tabi 		break;
431b0c813ceSTimur Tabi 	default:
432b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: unknown format\n");
433b0c813ceSTimur Tabi 		return -EINVAL;
434b0c813ceSTimur Tabi 	}
435b0c813ceSTimur Tabi 
436b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_FORMAT, reg);
437b0c813ceSTimur Tabi 	if (ret < 0) {
438b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
439b0c813ceSTimur Tabi 		return ret;
440b0c813ceSTimur Tabi 	}
441b0c813ceSTimur Tabi 
442b0c813ceSTimur Tabi 	/* Disable auto-mute.  This feature appears to be buggy, because in
443b0c813ceSTimur Tabi 	   some situations, auto-mute will not deactivate when it should. */
444b0c813ceSTimur Tabi 
445b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MUTE);
446b0c813ceSTimur Tabi 	reg &= ~CS4270_MUTE_AUTO;
447b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MUTE, reg);
448b0c813ceSTimur Tabi 	if (ret < 0) {
449b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
450b0c813ceSTimur Tabi 		return ret;
451b0c813ceSTimur Tabi 	}
452b0c813ceSTimur Tabi 
453b0c813ceSTimur Tabi 	/* Thaw and power-up the codec */
454b0c813ceSTimur Tabi 
455b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
456b0c813ceSTimur Tabi 	if (ret < 0) {
457b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
458b0c813ceSTimur Tabi 		return ret;
459b0c813ceSTimur Tabi 	}
460b0c813ceSTimur Tabi 
461b0c813ceSTimur Tabi 	return ret;
462b0c813ceSTimur Tabi }
463b0c813ceSTimur Tabi 
464b0c813ceSTimur Tabi #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
465b0c813ceSTimur Tabi 
466b0c813ceSTimur Tabi /*
467b0c813ceSTimur Tabi  * Set the CS4270 external mute
468b0c813ceSTimur Tabi  *
469b0c813ceSTimur Tabi  * This function toggles the mute bits in the MUTE register.  The CS4270's
470b0c813ceSTimur Tabi  * mute capability is intended for external muting circuitry, so if the
471b0c813ceSTimur Tabi  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
472b0c813ceSTimur Tabi  * then this function will do nothing.
473b0c813ceSTimur Tabi  */
474b0c813ceSTimur Tabi static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
475b0c813ceSTimur Tabi {
476b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = dai->codec;
477b0c813ceSTimur Tabi 	int reg6;
478b0c813ceSTimur Tabi 
479b0c813ceSTimur Tabi 	reg6 = snd_soc_read(codec, CS4270_MUTE);
480b0c813ceSTimur Tabi 
481b0c813ceSTimur Tabi 	if (mute)
482b0c813ceSTimur Tabi 		reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
483b0c813ceSTimur Tabi 			CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
484b0c813ceSTimur Tabi 	else
485b0c813ceSTimur Tabi 		reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
486b0c813ceSTimur Tabi 			  CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
487b0c813ceSTimur Tabi 
488b0c813ceSTimur Tabi 	return snd_soc_write(codec, CS4270_MUTE, reg6);
489b0c813ceSTimur Tabi }
490b0c813ceSTimur Tabi 
491b0c813ceSTimur Tabi #endif
492b0c813ceSTimur Tabi 
493b0c813ceSTimur Tabi static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
494b0c813ceSTimur Tabi 
495b0c813ceSTimur Tabi /*
496b0c813ceSTimur Tabi  * Notify the driver that a new I2C bus has been found.
497b0c813ceSTimur Tabi  *
498b0c813ceSTimur Tabi  * This function is called for each I2C bus in the system.  The function
499b0c813ceSTimur Tabi  * then asks the I2C subsystem to probe that bus at the addresses on which
500b0c813ceSTimur Tabi  * our device (the CS4270) could exist.  If a device is found at one of
501b0c813ceSTimur Tabi  * those addresses, then our probe function (cs4270_i2c_probe) is called.
502b0c813ceSTimur Tabi  */
503b0c813ceSTimur Tabi static int cs4270_i2c_attach(struct i2c_adapter *adapter)
504b0c813ceSTimur Tabi {
505b0c813ceSTimur Tabi 	return i2c_probe(adapter, &addr_data, cs4270_i2c_probe);
506b0c813ceSTimur Tabi }
507b0c813ceSTimur Tabi 
508b0c813ceSTimur Tabi static int cs4270_i2c_detach(struct i2c_client *client)
509b0c813ceSTimur Tabi {
510b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = i2c_get_clientdata(client);
511b0c813ceSTimur Tabi 
512b0c813ceSTimur Tabi 	i2c_detach_client(client);
513b0c813ceSTimur Tabi 	codec->control_data = NULL;
514b0c813ceSTimur Tabi 
515b0c813ceSTimur Tabi 	kfree(codec->reg_cache);
516b0c813ceSTimur Tabi 	codec->reg_cache = NULL;
517b0c813ceSTimur Tabi 
518b0c813ceSTimur Tabi 	kfree(client);
519b0c813ceSTimur Tabi 	return 0;
520b0c813ceSTimur Tabi }
521b0c813ceSTimur Tabi 
522b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */
523b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = {
524b0c813ceSTimur Tabi 	SOC_DOUBLE_R("Master Playback Volume",
525bfc4e861STimur Tabi 		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
526b0c813ceSTimur Tabi };
527b0c813ceSTimur Tabi 
528b0c813ceSTimur Tabi static struct i2c_driver cs4270_i2c_driver = {
529b0c813ceSTimur Tabi 	.driver = {
530b0c813ceSTimur Tabi 		.name = "CS4270 I2C",
531b0c813ceSTimur Tabi 		.owner = THIS_MODULE,
532b0c813ceSTimur Tabi 	},
533b0c813ceSTimur Tabi 	.id =             I2C_DRIVERID_CS4270,
534b0c813ceSTimur Tabi 	.attach_adapter = cs4270_i2c_attach,
535b0c813ceSTimur Tabi 	.detach_client =  cs4270_i2c_detach,
536b0c813ceSTimur Tabi };
537b0c813ceSTimur Tabi 
538b0c813ceSTimur Tabi /*
539b0c813ceSTimur Tabi  * Global variable to store socdev for i2c probe function.
540b0c813ceSTimur Tabi  *
541b0c813ceSTimur Tabi  * If struct i2c_driver had a private_data field, we wouldn't need to use
542b0c813ceSTimur Tabi  * cs4270_socdec.  This is the only way to pass the socdev structure to
543b0c813ceSTimur Tabi  * cs4270_i2c_probe().
544b0c813ceSTimur Tabi  *
545b0c813ceSTimur Tabi  * The real solution to cs4270_socdev is to create a mechanism
546b0c813ceSTimur Tabi  * that maps I2C addresses to snd_soc_device structures.  Perhaps the
547b0c813ceSTimur Tabi  * creation of the snd_soc_device object should be moved out of
548b0c813ceSTimur Tabi  * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
549b0c813ceSTimur Tabi  * driver dependent on I2C.  The CS4270 supports "stand-alone" mode, whereby
550b0c813ceSTimur Tabi  * the chip is *not* connected to the I2C bus, but is instead configured via
551b0c813ceSTimur Tabi  * input pins.
552b0c813ceSTimur Tabi  */
553b0c813ceSTimur Tabi static struct snd_soc_device *cs4270_socdev;
554b0c813ceSTimur Tabi 
555b0c813ceSTimur Tabi /*
556b0c813ceSTimur Tabi  * Initialize the I2C interface of the CS4270
557b0c813ceSTimur Tabi  *
558b0c813ceSTimur Tabi  * This function is called for whenever the I2C subsystem finds a device
559b0c813ceSTimur Tabi  * at a particular address.
560b0c813ceSTimur Tabi  *
561b0c813ceSTimur Tabi  * Note: snd_soc_new_pcms() must be called before this function can be called,
562b0c813ceSTimur Tabi  * because of snd_ctl_add().
563b0c813ceSTimur Tabi  */
564b0c813ceSTimur Tabi static int cs4270_i2c_probe(struct i2c_adapter *adapter, int addr, int kind)
565b0c813ceSTimur Tabi {
566b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = cs4270_socdev;
567b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = socdev->codec;
568b0c813ceSTimur Tabi 	struct i2c_client *i2c_client = NULL;
569b0c813ceSTimur Tabi 	int i;
570b0c813ceSTimur Tabi 	int ret = 0;
571b0c813ceSTimur Tabi 
572b0c813ceSTimur Tabi 	/* Probing all possible addresses has one drawback: if there are
573b0c813ceSTimur Tabi 	   multiple CS4270s on the bus, then you cannot specify which
574b0c813ceSTimur Tabi 	   socdev is matched with which CS4270.  For now, we just reject
575b0c813ceSTimur Tabi 	   this I2C device if the socdev already has one attached. */
576b0c813ceSTimur Tabi 	if (codec->control_data)
577b0c813ceSTimur Tabi 		return -ENODEV;
578b0c813ceSTimur Tabi 
579b0c813ceSTimur Tabi 	/* Note: codec_dai->codec is NULL here */
580b0c813ceSTimur Tabi 
581b0c813ceSTimur Tabi 	i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
582b0c813ceSTimur Tabi 	if (!i2c_client) {
583b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not allocate I2C client\n");
584b0c813ceSTimur Tabi 		return -ENOMEM;
585b0c813ceSTimur Tabi 	}
586b0c813ceSTimur Tabi 
587b0c813ceSTimur Tabi 	codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
588b0c813ceSTimur Tabi 	if (!codec->reg_cache) {
589b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not allocate register cache\n");
590b0c813ceSTimur Tabi 		ret = -ENOMEM;
591b0c813ceSTimur Tabi 		goto error;
592b0c813ceSTimur Tabi 	}
593b0c813ceSTimur Tabi 
594b0c813ceSTimur Tabi 	i2c_set_clientdata(i2c_client, codec);
595b0c813ceSTimur Tabi 	strcpy(i2c_client->name, "CS4270");
596b0c813ceSTimur Tabi 
597b0c813ceSTimur Tabi 	i2c_client->driver = &cs4270_i2c_driver;
598b0c813ceSTimur Tabi 	i2c_client->adapter = adapter;
599b0c813ceSTimur Tabi 	i2c_client->addr = addr;
600b0c813ceSTimur Tabi 
601b0c813ceSTimur Tabi 	/* Verify that we have a CS4270 */
602b0c813ceSTimur Tabi 
603b0c813ceSTimur Tabi 	ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
604b0c813ceSTimur Tabi 	if (ret < 0) {
605b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to read I2C\n");
606b0c813ceSTimur Tabi 		goto error;
607b0c813ceSTimur Tabi 	}
608b0c813ceSTimur Tabi 	/* The top four bits of the chip ID should be 1100. */
609b0c813ceSTimur Tabi 	if ((ret & 0xF0) != 0xC0) {
610b0c813ceSTimur Tabi 		/* The device at this address is not a CS4270 codec */
611b0c813ceSTimur Tabi 		ret = -ENODEV;
612b0c813ceSTimur Tabi 		goto error;
613b0c813ceSTimur Tabi 	}
614b0c813ceSTimur Tabi 
615b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: found device at I2C address %X\n", addr);
616b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
617b0c813ceSTimur Tabi 
618b0c813ceSTimur Tabi 	/* Tell the I2C layer a new client has arrived */
619b0c813ceSTimur Tabi 
620b0c813ceSTimur Tabi 	ret = i2c_attach_client(i2c_client);
621b0c813ceSTimur Tabi 	if (ret) {
622b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not attach codec, "
623b0c813ceSTimur Tabi 			"I2C address %x, error code %i\n", addr, ret);
624b0c813ceSTimur Tabi 		goto error;
625b0c813ceSTimur Tabi 	}
626b0c813ceSTimur Tabi 
627b0c813ceSTimur Tabi 	codec->control_data = i2c_client;
628b0c813ceSTimur Tabi 	codec->read = cs4270_read_reg_cache;
629b0c813ceSTimur Tabi 	codec->write = cs4270_i2c_write;
630b0c813ceSTimur Tabi 	codec->reg_cache_size = CS4270_NUMREGS;
631b0c813ceSTimur Tabi 
632b0c813ceSTimur Tabi 	/* The I2C interface is set up, so pre-fill our register cache */
633b0c813ceSTimur Tabi 
634b0c813ceSTimur Tabi 	ret = cs4270_fill_cache(codec);
635b0c813ceSTimur Tabi 	if (ret < 0) {
636b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to fill register cache\n");
637b0c813ceSTimur Tabi 		goto error;
638b0c813ceSTimur Tabi 	}
639b0c813ceSTimur Tabi 
640b0c813ceSTimur Tabi 	/* Add the non-DAPM controls */
641b0c813ceSTimur Tabi 
642b0c813ceSTimur Tabi 	for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
643b0c813ceSTimur Tabi 		struct snd_kcontrol *kctrl =
644b0c813ceSTimur Tabi 		snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
645b0c813ceSTimur Tabi 
646b0c813ceSTimur Tabi 		ret = snd_ctl_add(codec->card, kctrl);
647b0c813ceSTimur Tabi 		if (ret < 0)
648b0c813ceSTimur Tabi 			goto error;
649b0c813ceSTimur Tabi 	}
650b0c813ceSTimur Tabi 
651b0c813ceSTimur Tabi 	return 0;
652b0c813ceSTimur Tabi 
653b0c813ceSTimur Tabi error:
654b0c813ceSTimur Tabi 	if (codec->control_data) {
655b0c813ceSTimur Tabi 		i2c_detach_client(i2c_client);
656b0c813ceSTimur Tabi 		codec->control_data = NULL;
657b0c813ceSTimur Tabi 	}
658b0c813ceSTimur Tabi 
659b0c813ceSTimur Tabi 	kfree(codec->reg_cache);
660b0c813ceSTimur Tabi 	codec->reg_cache = NULL;
661b0c813ceSTimur Tabi 	codec->reg_cache_size = 0;
662b0c813ceSTimur Tabi 
663b0c813ceSTimur Tabi 	kfree(i2c_client);
664b0c813ceSTimur Tabi 
665b0c813ceSTimur Tabi 	return ret;
666b0c813ceSTimur Tabi }
667b0c813ceSTimur Tabi 
6688432395fSTimur Tabi #endif /* USE_I2C*/
669b0c813ceSTimur Tabi 
670b0c813ceSTimur Tabi struct snd_soc_codec_dai cs4270_dai = {
671b0c813ceSTimur Tabi 	.name = "CS4270",
672b0c813ceSTimur Tabi 	.playback = {
673b0c813ceSTimur Tabi 		.stream_name = "Playback",
674b0c813ceSTimur Tabi 		.channels_min = 1,
675b0c813ceSTimur Tabi 		.channels_max = 2,
676b0c813ceSTimur Tabi 		.rates = 0,
677b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
678b0c813ceSTimur Tabi 	},
679b0c813ceSTimur Tabi 	.capture = {
680b0c813ceSTimur Tabi 		.stream_name = "Capture",
681b0c813ceSTimur Tabi 		.channels_min = 1,
682b0c813ceSTimur Tabi 		.channels_max = 2,
683b0c813ceSTimur Tabi 		.rates = 0,
684b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
685b0c813ceSTimur Tabi 	},
686b0c813ceSTimur Tabi };
687b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(cs4270_dai);
688b0c813ceSTimur Tabi 
689b0c813ceSTimur Tabi /*
690b0c813ceSTimur Tabi  * ASoC probe function
691b0c813ceSTimur Tabi  *
692b0c813ceSTimur Tabi  * This function is called when the machine driver calls
693b0c813ceSTimur Tabi  * platform_device_add().
694b0c813ceSTimur Tabi  */
695b0c813ceSTimur Tabi static int cs4270_probe(struct platform_device *pdev)
696b0c813ceSTimur Tabi {
697b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
698b0c813ceSTimur Tabi 	struct snd_soc_codec *codec;
699b0c813ceSTimur Tabi 	int ret = 0;
700b0c813ceSTimur Tabi 
701b0c813ceSTimur Tabi 	printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
702b0c813ceSTimur Tabi 
703b0c813ceSTimur Tabi 	/* Allocate enough space for the snd_soc_codec structure
704b0c813ceSTimur Tabi 	   and our private data together. */
705b0c813ceSTimur Tabi 	codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
706b0c813ceSTimur Tabi 			sizeof(struct cs4270_private), GFP_KERNEL);
707b0c813ceSTimur Tabi 	if (!codec) {
708b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
709b0c813ceSTimur Tabi 		return -ENOMEM;
710b0c813ceSTimur Tabi 	}
711b0c813ceSTimur Tabi 
712b0c813ceSTimur Tabi 	mutex_init(&codec->mutex);
713b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_widgets);
714b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_paths);
715b0c813ceSTimur Tabi 
716b0c813ceSTimur Tabi 	codec->name = "CS4270";
717b0c813ceSTimur Tabi 	codec->owner = THIS_MODULE;
718b0c813ceSTimur Tabi 	codec->dai = &cs4270_dai;
719b0c813ceSTimur Tabi 	codec->num_dai = 1;
7204df20535STimur Tabi 	codec->private_data = (void *) codec +
7214df20535STimur Tabi 		ALIGN(sizeof(struct snd_soc_codec), 4);
722b0c813ceSTimur Tabi 
723b0c813ceSTimur Tabi 	socdev->codec = codec;
724b0c813ceSTimur Tabi 
725b0c813ceSTimur Tabi 	/* Register PCMs */
726b0c813ceSTimur Tabi 
727b0c813ceSTimur Tabi 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
728b0c813ceSTimur Tabi 	if (ret < 0) {
729b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to create PCMs\n");
730b0c813ceSTimur Tabi 		return ret;
731b0c813ceSTimur Tabi 	}
732b0c813ceSTimur Tabi 
7339dbd627bSTimur Tabi #ifdef USE_I2C
734b0c813ceSTimur Tabi 	cs4270_socdev = socdev;
735b0c813ceSTimur Tabi 
736b0c813ceSTimur Tabi 	ret = i2c_add_driver(&cs4270_i2c_driver);
737b0c813ceSTimur Tabi 	if (ret) {
738b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to attach driver");
739b0c813ceSTimur Tabi 		snd_soc_free_pcms(socdev);
740b0c813ceSTimur Tabi 		return ret;
741b0c813ceSTimur Tabi 	}
742b0c813ceSTimur Tabi 
743b0c813ceSTimur Tabi 	/* Did we find a CS4270 on the I2C bus? */
744b0c813ceSTimur Tabi 	if (codec->control_data) {
745b0c813ceSTimur Tabi 		/* Initialize codec ops */
746b0c813ceSTimur Tabi 		cs4270_dai.ops.hw_params = cs4270_hw_params;
7478432395fSTimur Tabi 		cs4270_dai.dai_ops.set_sysclk = cs4270_set_dai_sysclk;
7488432395fSTimur Tabi 		cs4270_dai.dai_ops.set_fmt = cs4270_set_dai_fmt;
749b0c813ceSTimur Tabi #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
750b0c813ceSTimur Tabi 		cs4270_dai.dai_ops.digital_mute = cs4270_mute;
751b0c813ceSTimur Tabi #endif
752b0c813ceSTimur Tabi 	} else
753b0c813ceSTimur Tabi 		printk(KERN_INFO "cs4270: no I2C device found, "
754b0c813ceSTimur Tabi 			"using stand-alone mode\n");
755b0c813ceSTimur Tabi #else
756b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
757b0c813ceSTimur Tabi #endif
758b0c813ceSTimur Tabi 
759b0c813ceSTimur Tabi 	ret = snd_soc_register_card(socdev);
760b0c813ceSTimur Tabi 	if (ret < 0) {
761b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to register card\n");
762b0c813ceSTimur Tabi 		snd_soc_free_pcms(socdev);
763b0c813ceSTimur Tabi 		return ret;
764b0c813ceSTimur Tabi 	}
765b0c813ceSTimur Tabi 
766b0c813ceSTimur Tabi 	return ret;
767b0c813ceSTimur Tabi }
768b0c813ceSTimur Tabi 
769b0c813ceSTimur Tabi static int cs4270_remove(struct platform_device *pdev)
770b0c813ceSTimur Tabi {
771b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
772b0c813ceSTimur Tabi 
773b0c813ceSTimur Tabi 	snd_soc_free_pcms(socdev);
774b0c813ceSTimur Tabi 
7759dbd627bSTimur Tabi #ifdef USE_I2C
776b0c813ceSTimur Tabi 	if (socdev->codec->control_data)
777b0c813ceSTimur Tabi 		i2c_del_driver(&cs4270_i2c_driver);
778b0c813ceSTimur Tabi #endif
779b0c813ceSTimur Tabi 
780b0c813ceSTimur Tabi 	kfree(socdev->codec);
781b0c813ceSTimur Tabi 	socdev->codec = NULL;
782b0c813ceSTimur Tabi 
783b0c813ceSTimur Tabi 	return 0;
784b0c813ceSTimur Tabi }
785b0c813ceSTimur Tabi 
786b0c813ceSTimur Tabi /*
787b0c813ceSTimur Tabi  * ASoC codec device structure
788b0c813ceSTimur Tabi  *
789b0c813ceSTimur Tabi  * Assign this variable to the codec_dev field of the machine driver's
790b0c813ceSTimur Tabi  * snd_soc_device structure.
791b0c813ceSTimur Tabi  */
792b0c813ceSTimur Tabi struct snd_soc_codec_device soc_codec_device_cs4270 = {
793b0c813ceSTimur Tabi 	.probe = 	cs4270_probe,
794b0c813ceSTimur Tabi 	.remove = 	cs4270_remove
795b0c813ceSTimur Tabi };
796b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
797b0c813ceSTimur Tabi 
798b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
799b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
800b0c813ceSTimur Tabi MODULE_LICENSE("GPL");
801