xref: /linux/sound/soc/codecs/cs4270.c (revision 4df20535ec52fb7eba604eb1ba77148f92ab8edd)
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/driver.h>
32b0c813ceSTimur Tabi #include <sound/core.h>
33b0c813ceSTimur Tabi #include <sound/soc.h>
34b0c813ceSTimur Tabi #include <sound/initval.h>
35b0c813ceSTimur Tabi #include <linux/i2c.h>
36b0c813ceSTimur Tabi 
37b0c813ceSTimur Tabi #include "cs4270.h"
38b0c813ceSTimur Tabi 
399dbd627bSTimur Tabi /* If I2C is defined, then we support software mode.  However, if we're
409dbd627bSTimur Tabi    not compiled as module but I2C is, then we can't use I2C calls. */
419dbd627bSTimur Tabi #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
429dbd627bSTimur Tabi #define USE_I2C
439dbd627bSTimur Tabi #endif
449dbd627bSTimur Tabi 
45b0c813ceSTimur Tabi /* Private data for the CS4270 */
46b0c813ceSTimur Tabi struct cs4270_private {
47b0c813ceSTimur Tabi 	unsigned int mclk; /* Input frequency of the MCLK pin */
48b0c813ceSTimur Tabi 	unsigned int mode; /* The mode (I2S or left-justified) */
49b0c813ceSTimur Tabi };
50b0c813ceSTimur Tabi 
519dbd627bSTimur Tabi /* The number of MCLK/LRCK ratios supported by the CS4270 */
529dbd627bSTimur Tabi #define NUM_MCLK_RATIOS		9
539dbd627bSTimur Tabi 
549dbd627bSTimur Tabi /* The actual MCLK/LRCK ratios, in increasing numerical order */
559dbd627bSTimur Tabi static unsigned int mclk_ratios[NUM_MCLK_RATIOS] =
569dbd627bSTimur Tabi 	{64, 96, 128, 192, 256, 384, 512, 768, 1024};
579dbd627bSTimur Tabi 
589dbd627bSTimur Tabi /*
599dbd627bSTimur Tabi  * Determine the CS4270 samples rates.
609dbd627bSTimur Tabi  *
619dbd627bSTimur Tabi  * 'freq' is the input frequency to MCLK.  The other parameters are ignored.
629dbd627bSTimur Tabi  *
639dbd627bSTimur Tabi  * The value of MCLK is used to determine which sample rates are supported
649dbd627bSTimur Tabi  * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
659dbd627bSTimur Tabi  * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
669dbd627bSTimur Tabi  *
679dbd627bSTimur Tabi  * This function calculates the nine ratios and determines which ones match
689dbd627bSTimur Tabi  * a standard sample rate.  If there's a match, then it is added to the list
699dbd627bSTimur Tabi  * of support sample rates.
709dbd627bSTimur Tabi  *
719dbd627bSTimur Tabi  * This function must be called by the machine driver's 'startup' function,
729dbd627bSTimur Tabi  * otherwise the list of supported sample rates will not be available in
739dbd627bSTimur Tabi  * time for ALSA.
749dbd627bSTimur Tabi  *
759dbd627bSTimur Tabi  * Note that in stand-alone mode, the sample rate is determined by input
769dbd627bSTimur Tabi  * pins M0, M1, MDIV1, and MDIV2.  Also in stand-alone mode, divide-by-3
779dbd627bSTimur Tabi  * is not a programmable option.  However, divide-by-3 is not an available
789dbd627bSTimur Tabi  * option in stand-alone mode.  This cases two problems: a ratio of 768 is
799dbd627bSTimur Tabi  * not available (it requires divide-by-3) and B) ratios 192 and 384 can
809dbd627bSTimur Tabi  * only be selected with divide-by-1.5, but there is an errate that make
819dbd627bSTimur Tabi  * this selection difficult.
829dbd627bSTimur Tabi  *
839dbd627bSTimur Tabi  * In addition, there is no mechanism for communicating with the machine
849dbd627bSTimur Tabi  * driver what the input settings can be.  This would need to be implemented
859dbd627bSTimur Tabi  * for stand-alone mode to work.
869dbd627bSTimur Tabi  */
879dbd627bSTimur Tabi static int cs4270_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
889dbd627bSTimur Tabi 				 int clk_id, unsigned int freq, int dir)
899dbd627bSTimur Tabi {
909dbd627bSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
919dbd627bSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
929dbd627bSTimur Tabi 	unsigned int rates = 0;
939dbd627bSTimur Tabi 	unsigned int rate_min = -1;
949dbd627bSTimur Tabi 	unsigned int rate_max = 0;
959dbd627bSTimur Tabi 	unsigned int i;
969dbd627bSTimur Tabi 
979dbd627bSTimur Tabi 	cs4270->mclk = freq;
989dbd627bSTimur Tabi 
999dbd627bSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
100918f3a0eSClemens Ladisch 		unsigned int rate = freq / mclk_ratios[i];
101918f3a0eSClemens Ladisch 		rates |= snd_pcm_rate_to_rate_bit(rate);
1029dbd627bSTimur Tabi 		if (rate < rate_min)
1039dbd627bSTimur Tabi 			rate_min = rate;
1049dbd627bSTimur Tabi 		if (rate > rate_max)
1059dbd627bSTimur Tabi 			rate_max = rate;
1069dbd627bSTimur Tabi 	}
107918f3a0eSClemens Ladisch 	/* FIXME: soc should support a rate list */
108918f3a0eSClemens Ladisch 	rates &= ~SNDRV_PCM_RATE_KNOT;
1099dbd627bSTimur Tabi 
1109dbd627bSTimur Tabi 	if (!rates) {
1119dbd627bSTimur Tabi 		printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
1129dbd627bSTimur Tabi 		return -EINVAL;
1139dbd627bSTimur Tabi 	}
1149dbd627bSTimur Tabi 
1159dbd627bSTimur Tabi 	codec_dai->playback.rates = rates;
1169dbd627bSTimur Tabi 	codec_dai->playback.rate_min = rate_min;
1179dbd627bSTimur Tabi 	codec_dai->playback.rate_max = rate_max;
1189dbd627bSTimur Tabi 
1199dbd627bSTimur Tabi 	codec_dai->capture.rates = rates;
1209dbd627bSTimur Tabi 	codec_dai->capture.rate_min = rate_min;
1219dbd627bSTimur Tabi 	codec_dai->capture.rate_max = rate_max;
1229dbd627bSTimur Tabi 
1239dbd627bSTimur Tabi 	return 0;
1249dbd627bSTimur Tabi }
1259dbd627bSTimur Tabi 
1269dbd627bSTimur Tabi /*
1279dbd627bSTimur Tabi  * Configure the codec for the selected audio format
1289dbd627bSTimur Tabi  *
1299dbd627bSTimur Tabi  * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
1309dbd627bSTimur Tabi  * codec accordingly.
1319dbd627bSTimur Tabi  *
1329dbd627bSTimur Tabi  * Currently, this function only supports SND_SOC_DAIFMT_I2S and
1339dbd627bSTimur Tabi  * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
1349dbd627bSTimur Tabi  * data for playback only, but ASoC currently does not support different
1359dbd627bSTimur Tabi  * formats for playback vs. record.
1369dbd627bSTimur Tabi  */
1379dbd627bSTimur Tabi static int cs4270_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1389dbd627bSTimur Tabi 			      unsigned int format)
1399dbd627bSTimur Tabi {
1409dbd627bSTimur Tabi 	struct snd_soc_codec *codec = codec_dai->codec;
1419dbd627bSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
1429dbd627bSTimur Tabi 	int ret = 0;
1439dbd627bSTimur Tabi 
1449dbd627bSTimur Tabi 	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
1459dbd627bSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
1469dbd627bSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
1479dbd627bSTimur Tabi 		cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
1489dbd627bSTimur Tabi 		break;
1499dbd627bSTimur Tabi 	default:
1509dbd627bSTimur Tabi 		printk(KERN_ERR "cs4270: invalid DAI format\n");
1519dbd627bSTimur Tabi 		ret = -EINVAL;
1529dbd627bSTimur Tabi 	}
1539dbd627bSTimur Tabi 
1549dbd627bSTimur Tabi 	return ret;
1559dbd627bSTimur Tabi }
1569dbd627bSTimur Tabi 
157b0c813ceSTimur Tabi /*
158b0c813ceSTimur Tabi  * The codec isn't really big-endian or little-endian, since the I2S
159b0c813ceSTimur Tabi  * interface requires data to be sent serially with the MSbit first.
160b0c813ceSTimur Tabi  * However, to support BE and LE I2S devices, we specify both here.  That
161b0c813ceSTimur Tabi  * way, ALSA will always match the bit patterns.
162b0c813ceSTimur Tabi  */
163b0c813ceSTimur Tabi #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | \
164b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
165b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
166b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
167b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
168b0c813ceSTimur Tabi 			SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
169b0c813ceSTimur Tabi 
1709dbd627bSTimur Tabi #ifdef USE_I2C
171b0c813ceSTimur Tabi 
172b0c813ceSTimur Tabi /* CS4270 registers addresses */
173b0c813ceSTimur Tabi #define CS4270_CHIPID	0x01	/* Chip ID */
174b0c813ceSTimur Tabi #define CS4270_PWRCTL	0x02	/* Power Control */
175b0c813ceSTimur Tabi #define CS4270_MODE	0x03	/* Mode Control */
176b0c813ceSTimur Tabi #define CS4270_FORMAT	0x04	/* Serial Format, ADC/DAC Control */
177b0c813ceSTimur Tabi #define CS4270_TRANS	0x05	/* Transition Control */
178b0c813ceSTimur Tabi #define CS4270_MUTE	0x06	/* Mute Control */
179b0c813ceSTimur Tabi #define CS4270_VOLA	0x07	/* DAC Channel A Volume Control */
180b0c813ceSTimur Tabi #define CS4270_VOLB	0x08	/* DAC Channel B Volume Control */
181b0c813ceSTimur Tabi 
182b0c813ceSTimur Tabi #define CS4270_FIRSTREG	0x01
183b0c813ceSTimur Tabi #define CS4270_LASTREG	0x08
184b0c813ceSTimur Tabi #define CS4270_NUMREGS	(CS4270_LASTREG - CS4270_FIRSTREG + 1)
185b0c813ceSTimur Tabi 
186b0c813ceSTimur Tabi /* Bit masks for the CS4270 registers */
187b0c813ceSTimur Tabi #define CS4270_CHIPID_ID	0xF0
188b0c813ceSTimur Tabi #define CS4270_CHIPID_REV	0x0F
189b0c813ceSTimur Tabi #define CS4270_PWRCTL_FREEZE	0x80
190b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_ADC	0x20
191b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN_DAC	0x02
192b0c813ceSTimur Tabi #define CS4270_PWRCTL_PDN	0x01
193b0c813ceSTimur Tabi #define CS4270_MODE_SPEED_MASK	0x30
194b0c813ceSTimur Tabi #define CS4270_MODE_1X		0x00
195b0c813ceSTimur Tabi #define CS4270_MODE_2X		0x10
196b0c813ceSTimur Tabi #define CS4270_MODE_4X		0x20
197b0c813ceSTimur Tabi #define CS4270_MODE_SLAVE	0x30
198b0c813ceSTimur Tabi #define CS4270_MODE_DIV_MASK	0x0E
199b0c813ceSTimur Tabi #define CS4270_MODE_DIV1	0x00
200b0c813ceSTimur Tabi #define CS4270_MODE_DIV15	0x02
201b0c813ceSTimur Tabi #define CS4270_MODE_DIV2	0x04
202b0c813ceSTimur Tabi #define CS4270_MODE_DIV3	0x06
203b0c813ceSTimur Tabi #define CS4270_MODE_DIV4	0x08
204b0c813ceSTimur Tabi #define CS4270_MODE_POPGUARD	0x01
205b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_A	0x80
206b0c813ceSTimur Tabi #define CS4270_FORMAT_FREEZE_B	0x40
207b0c813ceSTimur Tabi #define CS4270_FORMAT_LOOPBACK	0x20
208b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_MASK	0x18
209b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_LJ	0x00
210b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_I2S	0x08
211b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ16	0x18
212b0c813ceSTimur Tabi #define CS4270_FORMAT_DAC_RJ24	0x10
213b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_MASK	0x01
214b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_LJ	0x00
215b0c813ceSTimur Tabi #define CS4270_FORMAT_ADC_I2S	0x01
216b0c813ceSTimur Tabi #define CS4270_TRANS_ONE_VOL	0x80
217b0c813ceSTimur Tabi #define CS4270_TRANS_SOFT	0x40
218b0c813ceSTimur Tabi #define CS4270_TRANS_ZERO	0x20
219b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_A	0x08
220b0c813ceSTimur Tabi #define CS4270_TRANS_INV_ADC_B	0x10
221b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_A	0x02
222b0c813ceSTimur Tabi #define CS4270_TRANS_INV_DAC_B	0x04
223b0c813ceSTimur Tabi #define CS4270_TRANS_DEEMPH	0x01
224b0c813ceSTimur Tabi #define CS4270_MUTE_AUTO	0x20
225b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_A	0x08
226b0c813ceSTimur Tabi #define CS4270_MUTE_ADC_B	0x10
227b0c813ceSTimur Tabi #define CS4270_MUTE_POLARITY	0x04
228b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_A	0x01
229b0c813ceSTimur Tabi #define CS4270_MUTE_DAC_B	0x02
230b0c813ceSTimur Tabi 
231b0c813ceSTimur Tabi /*
232b0c813ceSTimur Tabi  * A list of addresses on which this CS4270 could use.  I2C addresses are
233b0c813ceSTimur Tabi  * 7 bits.  For the CS4270, the upper four bits are always 1001, and the
234b0c813ceSTimur Tabi  * lower three bits are determined via the AD2, AD1, and AD0 pins
235b0c813ceSTimur Tabi  * (respectively).
236b0c813ceSTimur Tabi  */
237b0c813ceSTimur Tabi static unsigned short normal_i2c[] = {
238b0c813ceSTimur Tabi 	0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END
239b0c813ceSTimur Tabi };
240b0c813ceSTimur Tabi I2C_CLIENT_INSMOD;
241b0c813ceSTimur Tabi 
242b0c813ceSTimur Tabi /*
243b0c813ceSTimur Tabi  * Pre-fill the CS4270 register cache.
244b0c813ceSTimur Tabi  *
245b0c813ceSTimur Tabi  * We use the auto-increment feature of the CS4270 to read all registers in
246b0c813ceSTimur Tabi  * one shot.
247b0c813ceSTimur Tabi  */
248b0c813ceSTimur Tabi static int cs4270_fill_cache(struct snd_soc_codec *codec)
249b0c813ceSTimur Tabi {
250b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
251b0c813ceSTimur Tabi 	struct i2c_client *i2c_client = codec->control_data;
252b0c813ceSTimur Tabi 	s32 length;
253b0c813ceSTimur Tabi 
254b0c813ceSTimur Tabi 	length = i2c_smbus_read_i2c_block_data(i2c_client,
255b0c813ceSTimur Tabi 		CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
256b0c813ceSTimur Tabi 
257b0c813ceSTimur Tabi 	if (length != CS4270_NUMREGS) {
2589dbd627bSTimur Tabi 		printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
259b0c813ceSTimur Tabi 		       i2c_client->addr);
260b0c813ceSTimur Tabi 		return -EIO;
261b0c813ceSTimur Tabi 	}
262b0c813ceSTimur Tabi 
263b0c813ceSTimur Tabi 	return 0;
264b0c813ceSTimur Tabi }
265b0c813ceSTimur Tabi 
266b0c813ceSTimur Tabi /*
267b0c813ceSTimur Tabi  * Read from the CS4270 register cache.
268b0c813ceSTimur Tabi  *
269b0c813ceSTimur Tabi  * This CS4270 registers are cached to avoid excessive I2C I/O operations.
270b0c813ceSTimur Tabi  * After the initial read to pre-fill the cache, the CS4270 never updates
271b0c813ceSTimur Tabi  * the register values, so we won't have a cache coherncy problem.
272b0c813ceSTimur Tabi  */
273b0c813ceSTimur Tabi static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
274b0c813ceSTimur Tabi 	unsigned int reg)
275b0c813ceSTimur Tabi {
276b0c813ceSTimur Tabi 	u8 *cache = codec->reg_cache;
277b0c813ceSTimur Tabi 
278b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
279b0c813ceSTimur Tabi 		return -EIO;
280b0c813ceSTimur Tabi 
281b0c813ceSTimur Tabi 	return cache[reg - CS4270_FIRSTREG];
282b0c813ceSTimur Tabi }
283b0c813ceSTimur Tabi 
284b0c813ceSTimur Tabi /*
285b0c813ceSTimur Tabi  * Write to a CS4270 register via the I2C bus.
286b0c813ceSTimur Tabi  *
287b0c813ceSTimur Tabi  * This function writes the given value to the given CS4270 register, and
288b0c813ceSTimur Tabi  * also updates the register cache.
289b0c813ceSTimur Tabi  *
290b0c813ceSTimur Tabi  * Note that we don't use the hw_write function pointer of snd_soc_codec.
291b0c813ceSTimur Tabi  * That's because it's too clunky: the hw_write_t prototype does not match
292b0c813ceSTimur Tabi  * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
293b0c813ceSTimur Tabi  */
294b0c813ceSTimur Tabi static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
295b0c813ceSTimur Tabi 			    unsigned int value)
296b0c813ceSTimur Tabi {
297bfc4e861STimur Tabi 	u8 *cache = codec->reg_cache;
298bfc4e861STimur Tabi 
299b0c813ceSTimur Tabi 	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
300b0c813ceSTimur Tabi 		return -EIO;
301b0c813ceSTimur Tabi 
302bfc4e861STimur Tabi 	/* Only perform an I2C operation if the new value is different */
303bfc4e861STimur Tabi 	if (cache[reg - CS4270_FIRSTREG] != value) {
304bfc4e861STimur Tabi 		struct i2c_client *client = codec->control_data;
305bfc4e861STimur Tabi 		if (i2c_smbus_write_byte_data(client, reg, value)) {
306bfc4e861STimur Tabi 			printk(KERN_ERR "cs4270: I2C write failed\n");
307b0c813ceSTimur Tabi 			return -EIO;
308b0c813ceSTimur Tabi 		}
309bfc4e861STimur Tabi 
310bfc4e861STimur Tabi 		/* We've written to the hardware, so update the cache */
311bfc4e861STimur Tabi 		cache[reg - CS4270_FIRSTREG] = value;
312bfc4e861STimur Tabi 	}
313bfc4e861STimur Tabi 
314bfc4e861STimur Tabi 	return 0;
315b0c813ceSTimur Tabi }
316b0c813ceSTimur Tabi 
317b0c813ceSTimur Tabi /*
3189dbd627bSTimur Tabi  * Clock Ratio Selection for Master Mode with I2C enabled
319b0c813ceSTimur Tabi  *
320b0c813ceSTimur Tabi  * The data for this chart is taken from Table 5 of the CS4270 reference
321b0c813ceSTimur Tabi  * manual.
322b0c813ceSTimur Tabi  *
323b0c813ceSTimur Tabi  * This table is used to determine how to program the Mode Control register.
324b0c813ceSTimur Tabi  * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
325b0c813ceSTimur Tabi  * rates the CS4270 currently supports.
326b0c813ceSTimur Tabi  *
3279dbd627bSTimur Tabi  * Each element in this array corresponds to the ratios in mclk_ratios[].
3289dbd627bSTimur Tabi  * These two arrays need to be in sync.
329b0c813ceSTimur Tabi  *
330b0c813ceSTimur Tabi  * 'speed_mode' is the corresponding bit pattern to be written to the
331b0c813ceSTimur Tabi  * MODE bits of the Mode Control Register
332b0c813ceSTimur Tabi  *
333b0c813ceSTimur Tabi  * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
334b0c813ceSTimur Tabi  * the Mode Control Register.
335b0c813ceSTimur Tabi  *
336b0c813ceSTimur Tabi  * In situations where a single ratio is represented by multiple speed
337b0c813ceSTimur Tabi  * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
338b0c813ceSTimur Tabi  * double-speed instead of quad-speed.  However, the CS4270 errata states
339b0c813ceSTimur Tabi  * that Divide-By-1.5 can cause failures, so we avoid that mode where
340b0c813ceSTimur Tabi  * possible.
341b0c813ceSTimur Tabi  *
342b0c813ceSTimur Tabi  * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
343b0c813ceSTimur Tabi  * work if VD = 3.3V.  If this effects you, select the
344b0c813ceSTimur Tabi  * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
345b0c813ceSTimur Tabi  * never select any sample rates that require divide-by-1.5.
346b0c813ceSTimur Tabi  */
347b0c813ceSTimur Tabi static struct {
348b0c813ceSTimur Tabi 	u8 speed_mode;
349b0c813ceSTimur Tabi 	u8 mclk;
3509dbd627bSTimur Tabi } cs4270_mode_ratios[NUM_MCLK_RATIOS] = {
3519dbd627bSTimur Tabi 	{CS4270_MODE_4X, CS4270_MODE_DIV1},	/* 64 */
352b0c813ceSTimur Tabi #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
3539dbd627bSTimur Tabi 	{CS4270_MODE_4X, CS4270_MODE_DIV15},    /* 96 */
354b0c813ceSTimur Tabi #endif
3559dbd627bSTimur Tabi 	{CS4270_MODE_2X, CS4270_MODE_DIV1},     /* 128 */
3569dbd627bSTimur Tabi 	{CS4270_MODE_4X, CS4270_MODE_DIV3},     /* 192 */
3579dbd627bSTimur Tabi 	{CS4270_MODE_1X, CS4270_MODE_DIV1},     /* 256 */
3589dbd627bSTimur Tabi 	{CS4270_MODE_2X, CS4270_MODE_DIV3},     /* 384 */
3599dbd627bSTimur Tabi 	{CS4270_MODE_1X, CS4270_MODE_DIV2},     /* 512 */
3609dbd627bSTimur Tabi 	{CS4270_MODE_1X, CS4270_MODE_DIV3},     /* 768 */
3619dbd627bSTimur Tabi 	{CS4270_MODE_1X, CS4270_MODE_DIV4}      /* 1024 */
362b0c813ceSTimur Tabi };
363b0c813ceSTimur Tabi 
364b0c813ceSTimur Tabi /*
365b0c813ceSTimur Tabi  * Program the CS4270 with the given hardware parameters.
366b0c813ceSTimur Tabi  *
367b0c813ceSTimur Tabi  * The .dai_ops functions are used to provide board-specific data, like
368b0c813ceSTimur Tabi  * input frequencies, to this driver.  This function takes that information,
369b0c813ceSTimur Tabi  * combines it with the hardware parameters provided, and programs the
370b0c813ceSTimur Tabi  * hardware accordingly.
371b0c813ceSTimur Tabi  */
372b0c813ceSTimur Tabi static int cs4270_hw_params(struct snd_pcm_substream *substream,
373b0c813ceSTimur Tabi 			    struct snd_pcm_hw_params *params)
374b0c813ceSTimur Tabi {
375b0c813ceSTimur Tabi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
376b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = rtd->socdev;
377b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = socdev->codec;
378b0c813ceSTimur Tabi 	struct cs4270_private *cs4270 = codec->private_data;
379b0c813ceSTimur Tabi 	unsigned int ret = 0;
380b0c813ceSTimur Tabi 	unsigned int i;
381b0c813ceSTimur Tabi 	unsigned int rate;
382b0c813ceSTimur Tabi 	unsigned int ratio;
383b0c813ceSTimur Tabi 	int reg;
384b0c813ceSTimur Tabi 
385b0c813ceSTimur Tabi 	/* Figure out which MCLK/LRCK ratio to use */
386b0c813ceSTimur Tabi 
387b0c813ceSTimur Tabi 	rate = params_rate(params);	/* Sampling rate, in Hz */
388b0c813ceSTimur Tabi 	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */
389b0c813ceSTimur Tabi 
3909dbd627bSTimur Tabi 	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
3919dbd627bSTimur Tabi 		if (mclk_ratios[i] == ratio)
392b0c813ceSTimur Tabi 			break;
393b0c813ceSTimur Tabi 	}
394b0c813ceSTimur Tabi 
3959dbd627bSTimur Tabi 	if (i == NUM_MCLK_RATIOS) {
396b0c813ceSTimur Tabi 		/* We did not find a matching ratio */
397b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not find matching ratio\n");
398b0c813ceSTimur Tabi 		return -EINVAL;
399b0c813ceSTimur Tabi 	}
400b0c813ceSTimur Tabi 
401b0c813ceSTimur Tabi 	/* Freeze and power-down the codec */
402b0c813ceSTimur Tabi 
403b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
404b0c813ceSTimur Tabi 			    CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
405b0c813ceSTimur Tabi 			    CS4270_PWRCTL_PDN);
406b0c813ceSTimur Tabi 	if (ret < 0) {
407b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
408b0c813ceSTimur Tabi 		return ret;
409b0c813ceSTimur Tabi 	}
410b0c813ceSTimur Tabi 
411b0c813ceSTimur Tabi 	/* Program the mode control register */
412b0c813ceSTimur Tabi 
413b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MODE);
414b0c813ceSTimur Tabi 	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
415b0c813ceSTimur Tabi 	reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
416b0c813ceSTimur Tabi 
417b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MODE, reg);
418b0c813ceSTimur Tabi 	if (ret < 0) {
419b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
420b0c813ceSTimur Tabi 		return ret;
421b0c813ceSTimur Tabi 	}
422b0c813ceSTimur Tabi 
423b0c813ceSTimur Tabi 	/* Program the format register */
424b0c813ceSTimur Tabi 
425b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_FORMAT);
426b0c813ceSTimur Tabi 	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
427b0c813ceSTimur Tabi 
428b0c813ceSTimur Tabi 	switch (cs4270->mode) {
429b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_I2S:
430b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
431b0c813ceSTimur Tabi 		break;
432b0c813ceSTimur Tabi 	case SND_SOC_DAIFMT_LEFT_J:
433b0c813ceSTimur Tabi 		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
434b0c813ceSTimur Tabi 		break;
435b0c813ceSTimur Tabi 	default:
436b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: unknown format\n");
437b0c813ceSTimur Tabi 		return -EINVAL;
438b0c813ceSTimur Tabi 	}
439b0c813ceSTimur Tabi 
440b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_FORMAT, reg);
441b0c813ceSTimur Tabi 	if (ret < 0) {
442b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
443b0c813ceSTimur Tabi 		return ret;
444b0c813ceSTimur Tabi 	}
445b0c813ceSTimur Tabi 
446b0c813ceSTimur Tabi 	/* Disable auto-mute.  This feature appears to be buggy, because in
447b0c813ceSTimur Tabi 	   some situations, auto-mute will not deactivate when it should. */
448b0c813ceSTimur Tabi 
449b0c813ceSTimur Tabi 	reg = snd_soc_read(codec, CS4270_MUTE);
450b0c813ceSTimur Tabi 	reg &= ~CS4270_MUTE_AUTO;
451b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_MUTE, reg);
452b0c813ceSTimur Tabi 	if (ret < 0) {
453b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
454b0c813ceSTimur Tabi 		return ret;
455b0c813ceSTimur Tabi 	}
456b0c813ceSTimur Tabi 
457b0c813ceSTimur Tabi 	/* Thaw and power-up the codec */
458b0c813ceSTimur Tabi 
459b0c813ceSTimur Tabi 	ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
460b0c813ceSTimur Tabi 	if (ret < 0) {
461b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: I2C write failed\n");
462b0c813ceSTimur Tabi 		return ret;
463b0c813ceSTimur Tabi 	}
464b0c813ceSTimur Tabi 
465b0c813ceSTimur Tabi 	return ret;
466b0c813ceSTimur Tabi }
467b0c813ceSTimur Tabi 
468b0c813ceSTimur Tabi #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
469b0c813ceSTimur Tabi 
470b0c813ceSTimur Tabi /*
471b0c813ceSTimur Tabi  * Set the CS4270 external mute
472b0c813ceSTimur Tabi  *
473b0c813ceSTimur Tabi  * This function toggles the mute bits in the MUTE register.  The CS4270's
474b0c813ceSTimur Tabi  * mute capability is intended for external muting circuitry, so if the
475b0c813ceSTimur Tabi  * board does not have the MUTEA or MUTEB pins connected to such circuitry,
476b0c813ceSTimur Tabi  * then this function will do nothing.
477b0c813ceSTimur Tabi  */
478b0c813ceSTimur Tabi static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
479b0c813ceSTimur Tabi {
480b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = dai->codec;
481b0c813ceSTimur Tabi 	int reg6;
482b0c813ceSTimur Tabi 
483b0c813ceSTimur Tabi 	reg6 = snd_soc_read(codec, CS4270_MUTE);
484b0c813ceSTimur Tabi 
485b0c813ceSTimur Tabi 	if (mute)
486b0c813ceSTimur Tabi 		reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
487b0c813ceSTimur Tabi 			CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
488b0c813ceSTimur Tabi 	else
489b0c813ceSTimur Tabi 		reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
490b0c813ceSTimur Tabi 			  CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
491b0c813ceSTimur Tabi 
492b0c813ceSTimur Tabi 	return snd_soc_write(codec, CS4270_MUTE, reg6);
493b0c813ceSTimur Tabi }
494b0c813ceSTimur Tabi 
495b0c813ceSTimur Tabi #endif
496b0c813ceSTimur Tabi 
497b0c813ceSTimur Tabi static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
498b0c813ceSTimur Tabi 
499b0c813ceSTimur Tabi /*
500b0c813ceSTimur Tabi  * Notify the driver that a new I2C bus has been found.
501b0c813ceSTimur Tabi  *
502b0c813ceSTimur Tabi  * This function is called for each I2C bus in the system.  The function
503b0c813ceSTimur Tabi  * then asks the I2C subsystem to probe that bus at the addresses on which
504b0c813ceSTimur Tabi  * our device (the CS4270) could exist.  If a device is found at one of
505b0c813ceSTimur Tabi  * those addresses, then our probe function (cs4270_i2c_probe) is called.
506b0c813ceSTimur Tabi  */
507b0c813ceSTimur Tabi static int cs4270_i2c_attach(struct i2c_adapter *adapter)
508b0c813ceSTimur Tabi {
509b0c813ceSTimur Tabi 	return i2c_probe(adapter, &addr_data, cs4270_i2c_probe);
510b0c813ceSTimur Tabi }
511b0c813ceSTimur Tabi 
512b0c813ceSTimur Tabi static int cs4270_i2c_detach(struct i2c_client *client)
513b0c813ceSTimur Tabi {
514b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = i2c_get_clientdata(client);
515b0c813ceSTimur Tabi 
516b0c813ceSTimur Tabi 	i2c_detach_client(client);
517b0c813ceSTimur Tabi 	codec->control_data = NULL;
518b0c813ceSTimur Tabi 
519b0c813ceSTimur Tabi 	kfree(codec->reg_cache);
520b0c813ceSTimur Tabi 	codec->reg_cache = NULL;
521b0c813ceSTimur Tabi 
522b0c813ceSTimur Tabi 	kfree(client);
523b0c813ceSTimur Tabi 	return 0;
524b0c813ceSTimur Tabi }
525b0c813ceSTimur Tabi 
526b0c813ceSTimur Tabi /* A list of non-DAPM controls that the CS4270 supports */
527b0c813ceSTimur Tabi static const struct snd_kcontrol_new cs4270_snd_controls[] = {
528b0c813ceSTimur Tabi 	SOC_DOUBLE_R("Master Playback Volume",
529bfc4e861STimur Tabi 		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
530b0c813ceSTimur Tabi };
531b0c813ceSTimur Tabi 
532b0c813ceSTimur Tabi static struct i2c_driver cs4270_i2c_driver = {
533b0c813ceSTimur Tabi 	.driver = {
534b0c813ceSTimur Tabi 		.name = "CS4270 I2C",
535b0c813ceSTimur Tabi 		.owner = THIS_MODULE,
536b0c813ceSTimur Tabi 	},
537b0c813ceSTimur Tabi 	.id =             I2C_DRIVERID_CS4270,
538b0c813ceSTimur Tabi 	.attach_adapter = cs4270_i2c_attach,
539b0c813ceSTimur Tabi 	.detach_client =  cs4270_i2c_detach,
540b0c813ceSTimur Tabi };
541b0c813ceSTimur Tabi 
542b0c813ceSTimur Tabi /*
543b0c813ceSTimur Tabi  * Global variable to store socdev for i2c probe function.
544b0c813ceSTimur Tabi  *
545b0c813ceSTimur Tabi  * If struct i2c_driver had a private_data field, we wouldn't need to use
546b0c813ceSTimur Tabi  * cs4270_socdec.  This is the only way to pass the socdev structure to
547b0c813ceSTimur Tabi  * cs4270_i2c_probe().
548b0c813ceSTimur Tabi  *
549b0c813ceSTimur Tabi  * The real solution to cs4270_socdev is to create a mechanism
550b0c813ceSTimur Tabi  * that maps I2C addresses to snd_soc_device structures.  Perhaps the
551b0c813ceSTimur Tabi  * creation of the snd_soc_device object should be moved out of
552b0c813ceSTimur Tabi  * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
553b0c813ceSTimur Tabi  * driver dependent on I2C.  The CS4270 supports "stand-alone" mode, whereby
554b0c813ceSTimur Tabi  * the chip is *not* connected to the I2C bus, but is instead configured via
555b0c813ceSTimur Tabi  * input pins.
556b0c813ceSTimur Tabi  */
557b0c813ceSTimur Tabi static struct snd_soc_device *cs4270_socdev;
558b0c813ceSTimur Tabi 
559b0c813ceSTimur Tabi /*
560b0c813ceSTimur Tabi  * Initialize the I2C interface of the CS4270
561b0c813ceSTimur Tabi  *
562b0c813ceSTimur Tabi  * This function is called for whenever the I2C subsystem finds a device
563b0c813ceSTimur Tabi  * at a particular address.
564b0c813ceSTimur Tabi  *
565b0c813ceSTimur Tabi  * Note: snd_soc_new_pcms() must be called before this function can be called,
566b0c813ceSTimur Tabi  * because of snd_ctl_add().
567b0c813ceSTimur Tabi  */
568b0c813ceSTimur Tabi static int cs4270_i2c_probe(struct i2c_adapter *adapter, int addr, int kind)
569b0c813ceSTimur Tabi {
570b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = cs4270_socdev;
571b0c813ceSTimur Tabi 	struct snd_soc_codec *codec = socdev->codec;
572b0c813ceSTimur Tabi 	struct i2c_client *i2c_client = NULL;
573b0c813ceSTimur Tabi 	int i;
574b0c813ceSTimur Tabi 	int ret = 0;
575b0c813ceSTimur Tabi 
576b0c813ceSTimur Tabi 	/* Probing all possible addresses has one drawback: if there are
577b0c813ceSTimur Tabi 	   multiple CS4270s on the bus, then you cannot specify which
578b0c813ceSTimur Tabi 	   socdev is matched with which CS4270.  For now, we just reject
579b0c813ceSTimur Tabi 	   this I2C device if the socdev already has one attached. */
580b0c813ceSTimur Tabi 	if (codec->control_data)
581b0c813ceSTimur Tabi 		return -ENODEV;
582b0c813ceSTimur Tabi 
583b0c813ceSTimur Tabi 	/* Note: codec_dai->codec is NULL here */
584b0c813ceSTimur Tabi 
585b0c813ceSTimur Tabi 	i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
586b0c813ceSTimur Tabi 	if (!i2c_client) {
587b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not allocate I2C client\n");
588b0c813ceSTimur Tabi 		return -ENOMEM;
589b0c813ceSTimur Tabi 	}
590b0c813ceSTimur Tabi 
591b0c813ceSTimur Tabi 	codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
592b0c813ceSTimur Tabi 	if (!codec->reg_cache) {
593b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not allocate register cache\n");
594b0c813ceSTimur Tabi 		ret = -ENOMEM;
595b0c813ceSTimur Tabi 		goto error;
596b0c813ceSTimur Tabi 	}
597b0c813ceSTimur Tabi 
598b0c813ceSTimur Tabi 	i2c_set_clientdata(i2c_client, codec);
599b0c813ceSTimur Tabi 	strcpy(i2c_client->name, "CS4270");
600b0c813ceSTimur Tabi 
601b0c813ceSTimur Tabi 	i2c_client->driver = &cs4270_i2c_driver;
602b0c813ceSTimur Tabi 	i2c_client->adapter = adapter;
603b0c813ceSTimur Tabi 	i2c_client->addr = addr;
604b0c813ceSTimur Tabi 
605b0c813ceSTimur Tabi 	/* Verify that we have a CS4270 */
606b0c813ceSTimur Tabi 
607b0c813ceSTimur Tabi 	ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
608b0c813ceSTimur Tabi 	if (ret < 0) {
609b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to read I2C\n");
610b0c813ceSTimur Tabi 		goto error;
611b0c813ceSTimur Tabi 	}
612b0c813ceSTimur Tabi 	/* The top four bits of the chip ID should be 1100. */
613b0c813ceSTimur Tabi 	if ((ret & 0xF0) != 0xC0) {
614b0c813ceSTimur Tabi 		/* The device at this address is not a CS4270 codec */
615b0c813ceSTimur Tabi 		ret = -ENODEV;
616b0c813ceSTimur Tabi 		goto error;
617b0c813ceSTimur Tabi 	}
618b0c813ceSTimur Tabi 
619b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: found device at I2C address %X\n", addr);
620b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
621b0c813ceSTimur Tabi 
622b0c813ceSTimur Tabi 	/* Tell the I2C layer a new client has arrived */
623b0c813ceSTimur Tabi 
624b0c813ceSTimur Tabi 	ret = i2c_attach_client(i2c_client);
625b0c813ceSTimur Tabi 	if (ret) {
626b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: could not attach codec, "
627b0c813ceSTimur Tabi 			"I2C address %x, error code %i\n", addr, ret);
628b0c813ceSTimur Tabi 		goto error;
629b0c813ceSTimur Tabi 	}
630b0c813ceSTimur Tabi 
631b0c813ceSTimur Tabi 	codec->control_data = i2c_client;
632b0c813ceSTimur Tabi 	codec->read = cs4270_read_reg_cache;
633b0c813ceSTimur Tabi 	codec->write = cs4270_i2c_write;
634b0c813ceSTimur Tabi 	codec->reg_cache_size = CS4270_NUMREGS;
635b0c813ceSTimur Tabi 
636b0c813ceSTimur Tabi 	/* The I2C interface is set up, so pre-fill our register cache */
637b0c813ceSTimur Tabi 
638b0c813ceSTimur Tabi 	ret = cs4270_fill_cache(codec);
639b0c813ceSTimur Tabi 	if (ret < 0) {
640b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to fill register cache\n");
641b0c813ceSTimur Tabi 		goto error;
642b0c813ceSTimur Tabi 	}
643b0c813ceSTimur Tabi 
644b0c813ceSTimur Tabi 	/* Add the non-DAPM controls */
645b0c813ceSTimur Tabi 
646b0c813ceSTimur Tabi 	for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
647b0c813ceSTimur Tabi 		struct snd_kcontrol *kctrl =
648b0c813ceSTimur Tabi 		snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
649b0c813ceSTimur Tabi 
650b0c813ceSTimur Tabi 		ret = snd_ctl_add(codec->card, kctrl);
651b0c813ceSTimur Tabi 		if (ret < 0)
652b0c813ceSTimur Tabi 			goto error;
653b0c813ceSTimur Tabi 	}
654b0c813ceSTimur Tabi 
655b0c813ceSTimur Tabi 	return 0;
656b0c813ceSTimur Tabi 
657b0c813ceSTimur Tabi error:
658b0c813ceSTimur Tabi 	if (codec->control_data) {
659b0c813ceSTimur Tabi 		i2c_detach_client(i2c_client);
660b0c813ceSTimur Tabi 		codec->control_data = NULL;
661b0c813ceSTimur Tabi 	}
662b0c813ceSTimur Tabi 
663b0c813ceSTimur Tabi 	kfree(codec->reg_cache);
664b0c813ceSTimur Tabi 	codec->reg_cache = NULL;
665b0c813ceSTimur Tabi 	codec->reg_cache_size = 0;
666b0c813ceSTimur Tabi 
667b0c813ceSTimur Tabi 	kfree(i2c_client);
668b0c813ceSTimur Tabi 
669b0c813ceSTimur Tabi 	return ret;
670b0c813ceSTimur Tabi }
671b0c813ceSTimur Tabi 
672b0c813ceSTimur Tabi #endif
673b0c813ceSTimur Tabi 
674b0c813ceSTimur Tabi struct snd_soc_codec_dai cs4270_dai = {
675b0c813ceSTimur Tabi 	.name = "CS4270",
676b0c813ceSTimur Tabi 	.playback = {
677b0c813ceSTimur Tabi 		.stream_name = "Playback",
678b0c813ceSTimur Tabi 		.channels_min = 1,
679b0c813ceSTimur Tabi 		.channels_max = 2,
680b0c813ceSTimur Tabi 		.rates = 0,
681b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
682b0c813ceSTimur Tabi 	},
683b0c813ceSTimur Tabi 	.capture = {
684b0c813ceSTimur Tabi 		.stream_name = "Capture",
685b0c813ceSTimur Tabi 		.channels_min = 1,
686b0c813ceSTimur Tabi 		.channels_max = 2,
687b0c813ceSTimur Tabi 		.rates = 0,
688b0c813ceSTimur Tabi 		.formats = CS4270_FORMATS,
689b0c813ceSTimur Tabi 	},
690b0c813ceSTimur Tabi 	.dai_ops = {
691b0c813ceSTimur Tabi 		.set_sysclk = cs4270_set_dai_sysclk,
692b0c813ceSTimur Tabi 		.set_fmt = cs4270_set_dai_fmt,
693b0c813ceSTimur Tabi 	}
694b0c813ceSTimur Tabi };
695b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(cs4270_dai);
696b0c813ceSTimur Tabi 
697b0c813ceSTimur Tabi /*
698b0c813ceSTimur Tabi  * ASoC probe function
699b0c813ceSTimur Tabi  *
700b0c813ceSTimur Tabi  * This function is called when the machine driver calls
701b0c813ceSTimur Tabi  * platform_device_add().
702b0c813ceSTimur Tabi  */
703b0c813ceSTimur Tabi static int cs4270_probe(struct platform_device *pdev)
704b0c813ceSTimur Tabi {
705b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
706b0c813ceSTimur Tabi 	struct snd_soc_codec *codec;
707b0c813ceSTimur Tabi 	int ret = 0;
708b0c813ceSTimur Tabi 
709b0c813ceSTimur Tabi 	printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
710b0c813ceSTimur Tabi 
711b0c813ceSTimur Tabi 	/* Allocate enough space for the snd_soc_codec structure
712b0c813ceSTimur Tabi 	   and our private data together. */
713b0c813ceSTimur Tabi 	codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
714b0c813ceSTimur Tabi 			sizeof(struct cs4270_private), GFP_KERNEL);
715b0c813ceSTimur Tabi 	if (!codec) {
716b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
717b0c813ceSTimur Tabi 		return -ENOMEM;
718b0c813ceSTimur Tabi 	}
719b0c813ceSTimur Tabi 
720b0c813ceSTimur Tabi 	mutex_init(&codec->mutex);
721b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_widgets);
722b0c813ceSTimur Tabi 	INIT_LIST_HEAD(&codec->dapm_paths);
723b0c813ceSTimur Tabi 
724b0c813ceSTimur Tabi 	codec->name = "CS4270";
725b0c813ceSTimur Tabi 	codec->owner = THIS_MODULE;
726b0c813ceSTimur Tabi 	codec->dai = &cs4270_dai;
727b0c813ceSTimur Tabi 	codec->num_dai = 1;
728*4df20535STimur Tabi 	codec->private_data = (void *) codec +
729*4df20535STimur Tabi 		ALIGN(sizeof(struct snd_soc_codec), 4);
730b0c813ceSTimur Tabi 
731b0c813ceSTimur Tabi 	socdev->codec = codec;
732b0c813ceSTimur Tabi 
733b0c813ceSTimur Tabi 	/* Register PCMs */
734b0c813ceSTimur Tabi 
735b0c813ceSTimur Tabi 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
736b0c813ceSTimur Tabi 	if (ret < 0) {
737b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to create PCMs\n");
738b0c813ceSTimur Tabi 		return ret;
739b0c813ceSTimur Tabi 	}
740b0c813ceSTimur Tabi 
7419dbd627bSTimur Tabi #ifdef USE_I2C
742b0c813ceSTimur Tabi 	cs4270_socdev = socdev;
743b0c813ceSTimur Tabi 
744b0c813ceSTimur Tabi 	ret = i2c_add_driver(&cs4270_i2c_driver);
745b0c813ceSTimur Tabi 	if (ret) {
746b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to attach driver");
747b0c813ceSTimur Tabi 		snd_soc_free_pcms(socdev);
748b0c813ceSTimur Tabi 		return ret;
749b0c813ceSTimur Tabi 	}
750b0c813ceSTimur Tabi 
751b0c813ceSTimur Tabi 	/* Did we find a CS4270 on the I2C bus? */
752b0c813ceSTimur Tabi 	if (codec->control_data) {
753b0c813ceSTimur Tabi 		/* Initialize codec ops */
754b0c813ceSTimur Tabi 		cs4270_dai.ops.hw_params = cs4270_hw_params;
755b0c813ceSTimur Tabi #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
756b0c813ceSTimur Tabi 		cs4270_dai.dai_ops.digital_mute = cs4270_mute;
757b0c813ceSTimur Tabi #endif
758b0c813ceSTimur Tabi 	} else
759b0c813ceSTimur Tabi 		printk(KERN_INFO "cs4270: no I2C device found, "
760b0c813ceSTimur Tabi 			"using stand-alone mode\n");
761b0c813ceSTimur Tabi #else
762b0c813ceSTimur Tabi 	printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
763b0c813ceSTimur Tabi #endif
764b0c813ceSTimur Tabi 
765b0c813ceSTimur Tabi 	ret = snd_soc_register_card(socdev);
766b0c813ceSTimur Tabi 	if (ret < 0) {
767b0c813ceSTimur Tabi 		printk(KERN_ERR "cs4270: failed to register card\n");
768b0c813ceSTimur Tabi 		snd_soc_free_pcms(socdev);
769b0c813ceSTimur Tabi 		return ret;
770b0c813ceSTimur Tabi 	}
771b0c813ceSTimur Tabi 
772b0c813ceSTimur Tabi 	return ret;
773b0c813ceSTimur Tabi }
774b0c813ceSTimur Tabi 
775b0c813ceSTimur Tabi static int cs4270_remove(struct platform_device *pdev)
776b0c813ceSTimur Tabi {
777b0c813ceSTimur Tabi 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
778b0c813ceSTimur Tabi 
779b0c813ceSTimur Tabi 	snd_soc_free_pcms(socdev);
780b0c813ceSTimur Tabi 
7819dbd627bSTimur Tabi #ifdef USE_I2C
782b0c813ceSTimur Tabi 	if (socdev->codec->control_data)
783b0c813ceSTimur Tabi 		i2c_del_driver(&cs4270_i2c_driver);
784b0c813ceSTimur Tabi #endif
785b0c813ceSTimur Tabi 
786b0c813ceSTimur Tabi 	kfree(socdev->codec);
787b0c813ceSTimur Tabi 	socdev->codec = NULL;
788b0c813ceSTimur Tabi 
789b0c813ceSTimur Tabi 	return 0;
790b0c813ceSTimur Tabi }
791b0c813ceSTimur Tabi 
792b0c813ceSTimur Tabi /*
793b0c813ceSTimur Tabi  * ASoC codec device structure
794b0c813ceSTimur Tabi  *
795b0c813ceSTimur Tabi  * Assign this variable to the codec_dev field of the machine driver's
796b0c813ceSTimur Tabi  * snd_soc_device structure.
797b0c813ceSTimur Tabi  */
798b0c813ceSTimur Tabi struct snd_soc_codec_device soc_codec_device_cs4270 = {
799b0c813ceSTimur Tabi 	.probe = 	cs4270_probe,
800b0c813ceSTimur Tabi 	.remove = 	cs4270_remove
801b0c813ceSTimur Tabi };
802b0c813ceSTimur Tabi EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
803b0c813ceSTimur Tabi 
804b0c813ceSTimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
805b0c813ceSTimur Tabi MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
806b0c813ceSTimur Tabi MODULE_LICENSE("GPL");
807