Linux 2.6.24.5
[linux/fpc-iii.git] / sound / soc / codecs / cs4270.c
blobabac62866da80e2db722d6ddb7937119a7124caa
1 /*
2 * CS4270 ALSA SoC (ASoC) codec driver
4 * Author: Timur Tabi <timur@freescale.com>
6 * Copyright 2007 Freescale Semiconductor, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
9 * or implied.
11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
13 * Current features/limitations:
15 * 1) Software mode is supported. Stand-alone mode is automatically
16 * selected if I2C is disabled or if a CS4270 is not found on the I2C
17 * bus. However, stand-alone mode is only partially implemented because
18 * there is no mechanism yet for this driver and the machine driver to
19 * communicate the values of the M0, M1, MCLK1, and MCLK2 pins.
20 * 2) Only I2C is supported, not SPI
21 * 3) Only Master mode is supported, not Slave.
22 * 4) The machine driver's 'startup' function must call
23 * cs4270_set_dai_sysclk() with the value of MCLK.
24 * 5) Only I2S and left-justified modes are supported
25 * 6) Power management is not supported
26 * 7) The only supported control is volume and hardware mute (if enabled)
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <sound/driver.h>
32 #include <sound/core.h>
33 #include <sound/soc.h>
34 #include <sound/initval.h>
35 #include <linux/i2c.h>
37 #include "cs4270.h"
39 /* If I2C is defined, then we support software mode. However, if we're
40 not compiled as module but I2C is, then we can't use I2C calls. */
41 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
42 #define USE_I2C
43 #endif
45 /* Private data for the CS4270 */
46 struct cs4270_private {
47 unsigned int mclk; /* Input frequency of the MCLK pin */
48 unsigned int mode; /* The mode (I2S or left-justified) */
51 /* The number of MCLK/LRCK ratios supported by the CS4270 */
52 #define NUM_MCLK_RATIOS 9
54 /* The actual MCLK/LRCK ratios, in increasing numerical order */
55 static unsigned int mclk_ratios[NUM_MCLK_RATIOS] =
56 {64, 96, 128, 192, 256, 384, 512, 768, 1024};
59 * Determine the CS4270 samples rates.
61 * 'freq' is the input frequency to MCLK. The other parameters are ignored.
63 * The value of MCLK is used to determine which sample rates are supported
64 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
65 * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
67 * This function calculates the nine ratios and determines which ones match
68 * a standard sample rate. If there's a match, then it is added to the list
69 * of support sample rates.
71 * This function must be called by the machine driver's 'startup' function,
72 * otherwise the list of supported sample rates will not be available in
73 * time for ALSA.
75 * Note that in stand-alone mode, the sample rate is determined by input
76 * pins M0, M1, MDIV1, and MDIV2. Also in stand-alone mode, divide-by-3
77 * is not a programmable option. However, divide-by-3 is not an available
78 * option in stand-alone mode. This cases two problems: a ratio of 768 is
79 * not available (it requires divide-by-3) and B) ratios 192 and 384 can
80 * only be selected with divide-by-1.5, but there is an errate that make
81 * this selection difficult.
83 * In addition, there is no mechanism for communicating with the machine
84 * driver what the input settings can be. This would need to be implemented
85 * for stand-alone mode to work.
87 static int cs4270_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
88 int clk_id, unsigned int freq, int dir)
90 struct snd_soc_codec *codec = codec_dai->codec;
91 struct cs4270_private *cs4270 = codec->private_data;
92 unsigned int rates = 0;
93 unsigned int rate_min = -1;
94 unsigned int rate_max = 0;
95 unsigned int i;
97 cs4270->mclk = freq;
99 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
100 unsigned int rate = freq / mclk_ratios[i];
101 rates |= snd_pcm_rate_to_rate_bit(rate);
102 if (rate < rate_min)
103 rate_min = rate;
104 if (rate > rate_max)
105 rate_max = rate;
107 /* FIXME: soc should support a rate list */
108 rates &= ~SNDRV_PCM_RATE_KNOT;
110 if (!rates) {
111 printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
112 return -EINVAL;
115 codec_dai->playback.rates = rates;
116 codec_dai->playback.rate_min = rate_min;
117 codec_dai->playback.rate_max = rate_max;
119 codec_dai->capture.rates = rates;
120 codec_dai->capture.rate_min = rate_min;
121 codec_dai->capture.rate_max = rate_max;
123 return 0;
127 * Configure the codec for the selected audio format
129 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
130 * codec accordingly.
132 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
133 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
134 * data for playback only, but ASoC currently does not support different
135 * formats for playback vs. record.
137 static int cs4270_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
138 unsigned int format)
140 struct snd_soc_codec *codec = codec_dai->codec;
141 struct cs4270_private *cs4270 = codec->private_data;
142 int ret = 0;
144 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
145 case SND_SOC_DAIFMT_I2S:
146 case SND_SOC_DAIFMT_LEFT_J:
147 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
148 break;
149 default:
150 printk(KERN_ERR "cs4270: invalid DAI format\n");
151 ret = -EINVAL;
154 return ret;
158 * The codec isn't really big-endian or little-endian, since the I2S
159 * interface requires data to be sent serially with the MSbit first.
160 * However, to support BE and LE I2S devices, we specify both here. That
161 * way, ALSA will always match the bit patterns.
163 #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
164 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
165 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
166 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
167 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
168 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
170 #ifdef USE_I2C
172 /* CS4270 registers addresses */
173 #define CS4270_CHIPID 0x01 /* Chip ID */
174 #define CS4270_PWRCTL 0x02 /* Power Control */
175 #define CS4270_MODE 0x03 /* Mode Control */
176 #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
177 #define CS4270_TRANS 0x05 /* Transition Control */
178 #define CS4270_MUTE 0x06 /* Mute Control */
179 #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
180 #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
182 #define CS4270_FIRSTREG 0x01
183 #define CS4270_LASTREG 0x08
184 #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
186 /* Bit masks for the CS4270 registers */
187 #define CS4270_CHIPID_ID 0xF0
188 #define CS4270_CHIPID_REV 0x0F
189 #define CS4270_PWRCTL_FREEZE 0x80
190 #define CS4270_PWRCTL_PDN_ADC 0x20
191 #define CS4270_PWRCTL_PDN_DAC 0x02
192 #define CS4270_PWRCTL_PDN 0x01
193 #define CS4270_MODE_SPEED_MASK 0x30
194 #define CS4270_MODE_1X 0x00
195 #define CS4270_MODE_2X 0x10
196 #define CS4270_MODE_4X 0x20
197 #define CS4270_MODE_SLAVE 0x30
198 #define CS4270_MODE_DIV_MASK 0x0E
199 #define CS4270_MODE_DIV1 0x00
200 #define CS4270_MODE_DIV15 0x02
201 #define CS4270_MODE_DIV2 0x04
202 #define CS4270_MODE_DIV3 0x06
203 #define CS4270_MODE_DIV4 0x08
204 #define CS4270_MODE_POPGUARD 0x01
205 #define CS4270_FORMAT_FREEZE_A 0x80
206 #define CS4270_FORMAT_FREEZE_B 0x40
207 #define CS4270_FORMAT_LOOPBACK 0x20
208 #define CS4270_FORMAT_DAC_MASK 0x18
209 #define CS4270_FORMAT_DAC_LJ 0x00
210 #define CS4270_FORMAT_DAC_I2S 0x08
211 #define CS4270_FORMAT_DAC_RJ16 0x18
212 #define CS4270_FORMAT_DAC_RJ24 0x10
213 #define CS4270_FORMAT_ADC_MASK 0x01
214 #define CS4270_FORMAT_ADC_LJ 0x00
215 #define CS4270_FORMAT_ADC_I2S 0x01
216 #define CS4270_TRANS_ONE_VOL 0x80
217 #define CS4270_TRANS_SOFT 0x40
218 #define CS4270_TRANS_ZERO 0x20
219 #define CS4270_TRANS_INV_ADC_A 0x08
220 #define CS4270_TRANS_INV_ADC_B 0x10
221 #define CS4270_TRANS_INV_DAC_A 0x02
222 #define CS4270_TRANS_INV_DAC_B 0x04
223 #define CS4270_TRANS_DEEMPH 0x01
224 #define CS4270_MUTE_AUTO 0x20
225 #define CS4270_MUTE_ADC_A 0x08
226 #define CS4270_MUTE_ADC_B 0x10
227 #define CS4270_MUTE_POLARITY 0x04
228 #define CS4270_MUTE_DAC_A 0x01
229 #define CS4270_MUTE_DAC_B 0x02
232 * A list of addresses on which this CS4270 could use. I2C addresses are
233 * 7 bits. For the CS4270, the upper four bits are always 1001, and the
234 * lower three bits are determined via the AD2, AD1, and AD0 pins
235 * (respectively).
237 static unsigned short normal_i2c[] = {
238 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END
240 I2C_CLIENT_INSMOD;
243 * Pre-fill the CS4270 register cache.
245 * We use the auto-increment feature of the CS4270 to read all registers in
246 * one shot.
248 static int cs4270_fill_cache(struct snd_soc_codec *codec)
250 u8 *cache = codec->reg_cache;
251 struct i2c_client *i2c_client = codec->control_data;
252 s32 length;
254 length = i2c_smbus_read_i2c_block_data(i2c_client,
255 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
257 if (length != CS4270_NUMREGS) {
258 printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
259 i2c_client->addr);
260 return -EIO;
263 return 0;
267 * Read from the CS4270 register cache.
269 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
270 * After the initial read to pre-fill the cache, the CS4270 never updates
271 * the register values, so we won't have a cache coherncy problem.
273 static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
274 unsigned int reg)
276 u8 *cache = codec->reg_cache;
278 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
279 return -EIO;
281 return cache[reg - CS4270_FIRSTREG];
285 * Write to a CS4270 register via the I2C bus.
287 * This function writes the given value to the given CS4270 register, and
288 * also updates the register cache.
290 * Note that we don't use the hw_write function pointer of snd_soc_codec.
291 * That's because it's too clunky: the hw_write_t prototype does not match
292 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
294 static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
295 unsigned int value)
297 u8 *cache = codec->reg_cache;
299 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
300 return -EIO;
302 /* Only perform an I2C operation if the new value is different */
303 if (cache[reg - CS4270_FIRSTREG] != value) {
304 struct i2c_client *client = codec->control_data;
305 if (i2c_smbus_write_byte_data(client, reg, value)) {
306 printk(KERN_ERR "cs4270: I2C write failed\n");
307 return -EIO;
310 /* We've written to the hardware, so update the cache */
311 cache[reg - CS4270_FIRSTREG] = value;
314 return 0;
318 * Clock Ratio Selection for Master Mode with I2C enabled
320 * The data for this chart is taken from Table 5 of the CS4270 reference
321 * manual.
323 * This table is used to determine how to program the Mode Control register.
324 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
325 * rates the CS4270 currently supports.
327 * Each element in this array corresponds to the ratios in mclk_ratios[].
328 * These two arrays need to be in sync.
330 * 'speed_mode' is the corresponding bit pattern to be written to the
331 * MODE bits of the Mode Control Register
333 * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
334 * the Mode Control Register.
336 * In situations where a single ratio is represented by multiple speed
337 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
338 * double-speed instead of quad-speed. However, the CS4270 errata states
339 * that Divide-By-1.5 can cause failures, so we avoid that mode where
340 * possible.
342 * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
343 * work if VD = 3.3V. If this effects you, select the
344 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
345 * never select any sample rates that require divide-by-1.5.
347 static struct {
348 u8 speed_mode;
349 u8 mclk;
350 } cs4270_mode_ratios[NUM_MCLK_RATIOS] = {
351 {CS4270_MODE_4X, CS4270_MODE_DIV1}, /* 64 */
352 #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
353 {CS4270_MODE_4X, CS4270_MODE_DIV15}, /* 96 */
354 #endif
355 {CS4270_MODE_2X, CS4270_MODE_DIV1}, /* 128 */
356 {CS4270_MODE_4X, CS4270_MODE_DIV3}, /* 192 */
357 {CS4270_MODE_1X, CS4270_MODE_DIV1}, /* 256 */
358 {CS4270_MODE_2X, CS4270_MODE_DIV3}, /* 384 */
359 {CS4270_MODE_1X, CS4270_MODE_DIV2}, /* 512 */
360 {CS4270_MODE_1X, CS4270_MODE_DIV3}, /* 768 */
361 {CS4270_MODE_1X, CS4270_MODE_DIV4} /* 1024 */
365 * Program the CS4270 with the given hardware parameters.
367 * The .dai_ops functions are used to provide board-specific data, like
368 * input frequencies, to this driver. This function takes that information,
369 * combines it with the hardware parameters provided, and programs the
370 * hardware accordingly.
372 static int cs4270_hw_params(struct snd_pcm_substream *substream,
373 struct snd_pcm_hw_params *params)
375 struct snd_soc_pcm_runtime *rtd = substream->private_data;
376 struct snd_soc_device *socdev = rtd->socdev;
377 struct snd_soc_codec *codec = socdev->codec;
378 struct cs4270_private *cs4270 = codec->private_data;
379 unsigned int ret = 0;
380 unsigned int i;
381 unsigned int rate;
382 unsigned int ratio;
383 int reg;
385 /* Figure out which MCLK/LRCK ratio to use */
387 rate = params_rate(params); /* Sampling rate, in Hz */
388 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
390 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
391 if (mclk_ratios[i] == ratio)
392 break;
395 if (i == NUM_MCLK_RATIOS) {
396 /* We did not find a matching ratio */
397 printk(KERN_ERR "cs4270: could not find matching ratio\n");
398 return -EINVAL;
401 /* Freeze and power-down the codec */
403 ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
404 CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
405 CS4270_PWRCTL_PDN);
406 if (ret < 0) {
407 printk(KERN_ERR "cs4270: I2C write failed\n");
408 return ret;
411 /* Program the mode control register */
413 reg = snd_soc_read(codec, CS4270_MODE);
414 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
415 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
417 ret = snd_soc_write(codec, CS4270_MODE, reg);
418 if (ret < 0) {
419 printk(KERN_ERR "cs4270: I2C write failed\n");
420 return ret;
423 /* Program the format register */
425 reg = snd_soc_read(codec, CS4270_FORMAT);
426 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
428 switch (cs4270->mode) {
429 case SND_SOC_DAIFMT_I2S:
430 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
431 break;
432 case SND_SOC_DAIFMT_LEFT_J:
433 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
434 break;
435 default:
436 printk(KERN_ERR "cs4270: unknown format\n");
437 return -EINVAL;
440 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
441 if (ret < 0) {
442 printk(KERN_ERR "cs4270: I2C write failed\n");
443 return ret;
446 /* Disable auto-mute. This feature appears to be buggy, because in
447 some situations, auto-mute will not deactivate when it should. */
449 reg = snd_soc_read(codec, CS4270_MUTE);
450 reg &= ~CS4270_MUTE_AUTO;
451 ret = snd_soc_write(codec, CS4270_MUTE, reg);
452 if (ret < 0) {
453 printk(KERN_ERR "cs4270: I2C write failed\n");
454 return ret;
457 /* Thaw and power-up the codec */
459 ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
460 if (ret < 0) {
461 printk(KERN_ERR "cs4270: I2C write failed\n");
462 return ret;
465 return ret;
468 #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
471 * Set the CS4270 external mute
473 * This function toggles the mute bits in the MUTE register. The CS4270's
474 * mute capability is intended for external muting circuitry, so if the
475 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
476 * then this function will do nothing.
478 static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
480 struct snd_soc_codec *codec = dai->codec;
481 int reg6;
483 reg6 = snd_soc_read(codec, CS4270_MUTE);
485 if (mute)
486 reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
487 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
488 else
489 reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
490 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
492 return snd_soc_write(codec, CS4270_MUTE, reg6);
495 #endif
497 static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
500 * Notify the driver that a new I2C bus has been found.
502 * This function is called for each I2C bus in the system. The function
503 * then asks the I2C subsystem to probe that bus at the addresses on which
504 * our device (the CS4270) could exist. If a device is found at one of
505 * those addresses, then our probe function (cs4270_i2c_probe) is called.
507 static int cs4270_i2c_attach(struct i2c_adapter *adapter)
509 return i2c_probe(adapter, &addr_data, cs4270_i2c_probe);
512 static int cs4270_i2c_detach(struct i2c_client *client)
514 struct snd_soc_codec *codec = i2c_get_clientdata(client);
516 i2c_detach_client(client);
517 codec->control_data = NULL;
519 kfree(codec->reg_cache);
520 codec->reg_cache = NULL;
522 kfree(client);
523 return 0;
526 /* A list of non-DAPM controls that the CS4270 supports */
527 static const struct snd_kcontrol_new cs4270_snd_controls[] = {
528 SOC_DOUBLE_R("Master Playback Volume",
529 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
532 static struct i2c_driver cs4270_i2c_driver = {
533 .driver = {
534 .name = "CS4270 I2C",
535 .owner = THIS_MODULE,
537 .id = I2C_DRIVERID_CS4270,
538 .attach_adapter = cs4270_i2c_attach,
539 .detach_client = cs4270_i2c_detach,
543 * Global variable to store socdev for i2c probe function.
545 * If struct i2c_driver had a private_data field, we wouldn't need to use
546 * cs4270_socdec. This is the only way to pass the socdev structure to
547 * cs4270_i2c_probe().
549 * The real solution to cs4270_socdev is to create a mechanism
550 * that maps I2C addresses to snd_soc_device structures. Perhaps the
551 * creation of the snd_soc_device object should be moved out of
552 * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
553 * driver dependent on I2C. The CS4270 supports "stand-alone" mode, whereby
554 * the chip is *not* connected to the I2C bus, but is instead configured via
555 * input pins.
557 static struct snd_soc_device *cs4270_socdev;
560 * Initialize the I2C interface of the CS4270
562 * This function is called for whenever the I2C subsystem finds a device
563 * at a particular address.
565 * Note: snd_soc_new_pcms() must be called before this function can be called,
566 * because of snd_ctl_add().
568 static int cs4270_i2c_probe(struct i2c_adapter *adapter, int addr, int kind)
570 struct snd_soc_device *socdev = cs4270_socdev;
571 struct snd_soc_codec *codec = socdev->codec;
572 struct i2c_client *i2c_client = NULL;
573 int i;
574 int ret = 0;
576 /* Probing all possible addresses has one drawback: if there are
577 multiple CS4270s on the bus, then you cannot specify which
578 socdev is matched with which CS4270. For now, we just reject
579 this I2C device if the socdev already has one attached. */
580 if (codec->control_data)
581 return -ENODEV;
583 /* Note: codec_dai->codec is NULL here */
585 i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
586 if (!i2c_client) {
587 printk(KERN_ERR "cs4270: could not allocate I2C client\n");
588 return -ENOMEM;
591 codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
592 if (!codec->reg_cache) {
593 printk(KERN_ERR "cs4270: could not allocate register cache\n");
594 ret = -ENOMEM;
595 goto error;
598 i2c_set_clientdata(i2c_client, codec);
599 strcpy(i2c_client->name, "CS4270");
601 i2c_client->driver = &cs4270_i2c_driver;
602 i2c_client->adapter = adapter;
603 i2c_client->addr = addr;
605 /* Verify that we have a CS4270 */
607 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
608 if (ret < 0) {
609 printk(KERN_ERR "cs4270: failed to read I2C\n");
610 goto error;
612 /* The top four bits of the chip ID should be 1100. */
613 if ((ret & 0xF0) != 0xC0) {
614 /* The device at this address is not a CS4270 codec */
615 ret = -ENODEV;
616 goto error;
619 printk(KERN_INFO "cs4270: found device at I2C address %X\n", addr);
620 printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
622 /* Tell the I2C layer a new client has arrived */
624 ret = i2c_attach_client(i2c_client);
625 if (ret) {
626 printk(KERN_ERR "cs4270: could not attach codec, "
627 "I2C address %x, error code %i\n", addr, ret);
628 goto error;
631 codec->control_data = i2c_client;
632 codec->read = cs4270_read_reg_cache;
633 codec->write = cs4270_i2c_write;
634 codec->reg_cache_size = CS4270_NUMREGS;
636 /* The I2C interface is set up, so pre-fill our register cache */
638 ret = cs4270_fill_cache(codec);
639 if (ret < 0) {
640 printk(KERN_ERR "cs4270: failed to fill register cache\n");
641 goto error;
644 /* Add the non-DAPM controls */
646 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
647 struct snd_kcontrol *kctrl =
648 snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
650 ret = snd_ctl_add(codec->card, kctrl);
651 if (ret < 0)
652 goto error;
655 return 0;
657 error:
658 if (codec->control_data) {
659 i2c_detach_client(i2c_client);
660 codec->control_data = NULL;
663 kfree(codec->reg_cache);
664 codec->reg_cache = NULL;
665 codec->reg_cache_size = 0;
667 kfree(i2c_client);
669 return ret;
672 #endif
674 struct snd_soc_codec_dai cs4270_dai = {
675 .name = "CS4270",
676 .playback = {
677 .stream_name = "Playback",
678 .channels_min = 1,
679 .channels_max = 2,
680 .rates = 0,
681 .formats = CS4270_FORMATS,
683 .capture = {
684 .stream_name = "Capture",
685 .channels_min = 1,
686 .channels_max = 2,
687 .rates = 0,
688 .formats = CS4270_FORMATS,
690 .dai_ops = {
691 .set_sysclk = cs4270_set_dai_sysclk,
692 .set_fmt = cs4270_set_dai_fmt,
695 EXPORT_SYMBOL_GPL(cs4270_dai);
698 * ASoC probe function
700 * This function is called when the machine driver calls
701 * platform_device_add().
703 static int cs4270_probe(struct platform_device *pdev)
705 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
706 struct snd_soc_codec *codec;
707 int ret = 0;
709 printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
711 /* Allocate enough space for the snd_soc_codec structure
712 and our private data together. */
713 codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
714 sizeof(struct cs4270_private), GFP_KERNEL);
715 if (!codec) {
716 printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
717 return -ENOMEM;
720 mutex_init(&codec->mutex);
721 INIT_LIST_HEAD(&codec->dapm_widgets);
722 INIT_LIST_HEAD(&codec->dapm_paths);
724 codec->name = "CS4270";
725 codec->owner = THIS_MODULE;
726 codec->dai = &cs4270_dai;
727 codec->num_dai = 1;
728 codec->private_data = (void *) codec +
729 ALIGN(sizeof(struct snd_soc_codec), 4);
731 socdev->codec = codec;
733 /* Register PCMs */
735 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
736 if (ret < 0) {
737 printk(KERN_ERR "cs4270: failed to create PCMs\n");
738 return ret;
741 #ifdef USE_I2C
742 cs4270_socdev = socdev;
744 ret = i2c_add_driver(&cs4270_i2c_driver);
745 if (ret) {
746 printk(KERN_ERR "cs4270: failed to attach driver");
747 snd_soc_free_pcms(socdev);
748 return ret;
751 /* Did we find a CS4270 on the I2C bus? */
752 if (codec->control_data) {
753 /* Initialize codec ops */
754 cs4270_dai.ops.hw_params = cs4270_hw_params;
755 #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
756 cs4270_dai.dai_ops.digital_mute = cs4270_mute;
757 #endif
758 } else
759 printk(KERN_INFO "cs4270: no I2C device found, "
760 "using stand-alone mode\n");
761 #else
762 printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
763 #endif
765 ret = snd_soc_register_card(socdev);
766 if (ret < 0) {
767 printk(KERN_ERR "cs4270: failed to register card\n");
768 snd_soc_free_pcms(socdev);
769 return ret;
772 return ret;
775 static int cs4270_remove(struct platform_device *pdev)
777 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
779 snd_soc_free_pcms(socdev);
781 #ifdef USE_I2C
782 if (socdev->codec->control_data)
783 i2c_del_driver(&cs4270_i2c_driver);
784 #endif
786 kfree(socdev->codec);
787 socdev->codec = NULL;
789 return 0;
793 * ASoC codec device structure
795 * Assign this variable to the codec_dev field of the machine driver's
796 * snd_soc_device structure.
798 struct snd_soc_codec_device soc_codec_device_cs4270 = {
799 .probe = cs4270_probe,
800 .remove = cs4270_remove
802 EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
804 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
805 MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
806 MODULE_LICENSE("GPL");