[ALSA] ASoC CS4270 codec device driver
[linux-2.6/x86.git] / sound / soc / codecs / cs4270.c
blobb659a5dfd7768106ae257674c1a5013a22451a6d
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) Stand-alone and software mode is supported. Stand-alone is
16 * automatically selected if I2C is disabled or if a CS4270 is not found
17 * on the I2C bus.
18 * 2) Only I2C is supported, not SPI
19 * 3) Only Master mode is supported, not Slave.
20 * 4) The machine driver's 'startup' function must call
21 * cs4270_set_dai_sysclk() with the value of MCLK.
22 * 5) Only I2S and left-justified modes are supported
23 * 6) Power management is not supported
24 * 7) The only supported control is volume and hardware mute (if enabled)
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <sound/driver.h>
30 #include <sound/core.h>
31 #include <sound/soc.h>
32 #include <sound/initval.h>
33 #include <linux/i2c.h>
35 #include "cs4270.h"
37 /* Private data for the CS4270 */
38 struct cs4270_private {
39 unsigned int mclk; /* Input frequency of the MCLK pin */
40 unsigned int mode; /* The mode (I2S or left-justified) */
44 * The codec isn't really big-endian or little-endian, since the I2S
45 * interface requires data to be sent serially with the MSbit first.
46 * However, to support BE and LE I2S devices, we specify both here. That
47 * way, ALSA will always match the bit patterns.
49 #define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
50 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
51 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
52 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
53 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
54 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
56 #ifdef CONFIG_I2C
58 /* CS4270 registers addresses */
59 #define CS4270_CHIPID 0x01 /* Chip ID */
60 #define CS4270_PWRCTL 0x02 /* Power Control */
61 #define CS4270_MODE 0x03 /* Mode Control */
62 #define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
63 #define CS4270_TRANS 0x05 /* Transition Control */
64 #define CS4270_MUTE 0x06 /* Mute Control */
65 #define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
66 #define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
68 #define CS4270_FIRSTREG 0x01
69 #define CS4270_LASTREG 0x08
70 #define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
72 /* Bit masks for the CS4270 registers */
73 #define CS4270_CHIPID_ID 0xF0
74 #define CS4270_CHIPID_REV 0x0F
75 #define CS4270_PWRCTL_FREEZE 0x80
76 #define CS4270_PWRCTL_PDN_ADC 0x20
77 #define CS4270_PWRCTL_PDN_DAC 0x02
78 #define CS4270_PWRCTL_PDN 0x01
79 #define CS4270_MODE_SPEED_MASK 0x30
80 #define CS4270_MODE_1X 0x00
81 #define CS4270_MODE_2X 0x10
82 #define CS4270_MODE_4X 0x20
83 #define CS4270_MODE_SLAVE 0x30
84 #define CS4270_MODE_DIV_MASK 0x0E
85 #define CS4270_MODE_DIV1 0x00
86 #define CS4270_MODE_DIV15 0x02
87 #define CS4270_MODE_DIV2 0x04
88 #define CS4270_MODE_DIV3 0x06
89 #define CS4270_MODE_DIV4 0x08
90 #define CS4270_MODE_POPGUARD 0x01
91 #define CS4270_FORMAT_FREEZE_A 0x80
92 #define CS4270_FORMAT_FREEZE_B 0x40
93 #define CS4270_FORMAT_LOOPBACK 0x20
94 #define CS4270_FORMAT_DAC_MASK 0x18
95 #define CS4270_FORMAT_DAC_LJ 0x00
96 #define CS4270_FORMAT_DAC_I2S 0x08
97 #define CS4270_FORMAT_DAC_RJ16 0x18
98 #define CS4270_FORMAT_DAC_RJ24 0x10
99 #define CS4270_FORMAT_ADC_MASK 0x01
100 #define CS4270_FORMAT_ADC_LJ 0x00
101 #define CS4270_FORMAT_ADC_I2S 0x01
102 #define CS4270_TRANS_ONE_VOL 0x80
103 #define CS4270_TRANS_SOFT 0x40
104 #define CS4270_TRANS_ZERO 0x20
105 #define CS4270_TRANS_INV_ADC_A 0x08
106 #define CS4270_TRANS_INV_ADC_B 0x10
107 #define CS4270_TRANS_INV_DAC_A 0x02
108 #define CS4270_TRANS_INV_DAC_B 0x04
109 #define CS4270_TRANS_DEEMPH 0x01
110 #define CS4270_MUTE_AUTO 0x20
111 #define CS4270_MUTE_ADC_A 0x08
112 #define CS4270_MUTE_ADC_B 0x10
113 #define CS4270_MUTE_POLARITY 0x04
114 #define CS4270_MUTE_DAC_A 0x01
115 #define CS4270_MUTE_DAC_B 0x02
118 * A list of addresses on which this CS4270 could use. I2C addresses are
119 * 7 bits. For the CS4270, the upper four bits are always 1001, and the
120 * lower three bits are determined via the AD2, AD1, and AD0 pins
121 * (respectively).
123 static unsigned short normal_i2c[] = {
124 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END
126 I2C_CLIENT_INSMOD;
129 * Pre-fill the CS4270 register cache.
131 * We use the auto-increment feature of the CS4270 to read all registers in
132 * one shot.
134 static int cs4270_fill_cache(struct snd_soc_codec *codec)
136 u8 *cache = codec->reg_cache;
137 struct i2c_client *i2c_client = codec->control_data;
138 s32 length;
140 length = i2c_smbus_read_i2c_block_data(i2c_client,
141 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
143 if (length != CS4270_NUMREGS) {
144 printk(KERN_ERR "cs4270: I2C read failure, addr=%u\n",
145 i2c_client->addr);
146 return -EIO;
149 return 0;
153 * Read from the CS4270 register cache.
155 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
156 * After the initial read to pre-fill the cache, the CS4270 never updates
157 * the register values, so we won't have a cache coherncy problem.
159 static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
160 unsigned int reg)
162 u8 *cache = codec->reg_cache;
164 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
165 return -EIO;
167 return cache[reg - CS4270_FIRSTREG];
171 * Write to a CS4270 register via the I2C bus.
173 * This function writes the given value to the given CS4270 register, and
174 * also updates the register cache.
176 * Note that we don't use the hw_write function pointer of snd_soc_codec.
177 * That's because it's too clunky: the hw_write_t prototype does not match
178 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
180 static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
181 unsigned int value)
183 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
184 return -EIO;
186 if (i2c_smbus_write_byte_data(codec->control_data, reg, value) == 0) {
187 /* We've written to the hardware, so update the cache */
188 u8 *cache = codec->reg_cache;
189 cache[reg - CS4270_FIRSTREG] = value;
190 return 0;
191 } else {
192 printk(KERN_ERR "cs4270: I2C write failed\n");
193 return -EIO;
198 * Clock Ratio Selection for Master Mode.
200 * The data for this chart is taken from Table 5 of the CS4270 reference
201 * manual.
203 * This table is used to determine how to program the Mode Control register.
204 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
205 * rates the CS4270 currently supports.
207 * 'ratio' is the MCLK/LRCK ratio. MCLK is usually a fixed input frequency,
208 * and LRCK is equal to the sampling rate. The CS4270 only supports sampling
209 * rates where this ratio is one of: 64, 96, 128, 192, 256, 384, 512, 768 or
210 * 1024.
212 * 'speed_mode' is the corresponding bit pattern to be written to the
213 * MODE bits of the Mode Control Register
215 * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
216 * the Mode Control Register.
218 * In situations where a single ratio is represented by multiple speed
219 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
220 * double-speed instead of quad-speed. However, the CS4270 errata states
221 * that Divide-By-1.5 can cause failures, so we avoid that mode where
222 * possible.
224 * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
225 * work if VD = 3.3V. If this effects you, select the
226 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
227 * never select any sample rates that require divide-by-1.5.
229 static struct {
230 unsigned int ratio;
231 u8 speed_mode;
232 u8 mclk;
233 } cs4270_mode_ratios[] = {
234 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
235 #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
236 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
237 #endif
238 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
239 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
240 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
241 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
242 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
243 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
244 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
248 * Program the CS4270 with the given hardware parameters.
250 * The .dai_ops functions are used to provide board-specific data, like
251 * input frequencies, to this driver. This function takes that information,
252 * combines it with the hardware parameters provided, and programs the
253 * hardware accordingly.
255 static int cs4270_hw_params(struct snd_pcm_substream *substream,
256 struct snd_pcm_hw_params *params)
258 struct snd_soc_pcm_runtime *rtd = substream->private_data;
259 struct snd_soc_device *socdev = rtd->socdev;
260 struct snd_soc_codec *codec = socdev->codec;
261 struct cs4270_private *cs4270 = codec->private_data;
262 unsigned int ret = 0;
263 unsigned int i;
264 unsigned int rate;
265 unsigned int ratio;
266 int reg;
268 /* Figure out which MCLK/LRCK ratio to use */
270 rate = params_rate(params); /* Sampling rate, in Hz */
271 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
273 for (i = 0; i < ARRAY_SIZE(cs4270_mode_ratios); i++) {
274 if (cs4270_mode_ratios[i].ratio == ratio)
275 break;
278 if (i == ARRAY_SIZE(cs4270_mode_ratios)) {
279 /* We did not find a matching ratio */
280 printk(KERN_ERR "cs4270: could not find matching ratio\n");
281 return -EINVAL;
284 /* Freeze and power-down the codec */
286 ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
287 CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
288 CS4270_PWRCTL_PDN);
289 if (ret < 0) {
290 printk(KERN_ERR "cs4270: I2C write failed\n");
291 return ret;
294 /* Program the mode control register */
296 reg = snd_soc_read(codec, CS4270_MODE);
297 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
298 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
300 ret = snd_soc_write(codec, CS4270_MODE, reg);
301 if (ret < 0) {
302 printk(KERN_ERR "cs4270: I2C write failed\n");
303 return ret;
306 /* Program the format register */
308 reg = snd_soc_read(codec, CS4270_FORMAT);
309 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
311 switch (cs4270->mode) {
312 case SND_SOC_DAIFMT_I2S:
313 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
314 break;
315 case SND_SOC_DAIFMT_LEFT_J:
316 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
317 break;
318 default:
319 printk(KERN_ERR "cs4270: unknown format\n");
320 return -EINVAL;
323 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
324 if (ret < 0) {
325 printk(KERN_ERR "cs4270: I2C write failed\n");
326 return ret;
329 /* Disable auto-mute. This feature appears to be buggy, because in
330 some situations, auto-mute will not deactivate when it should. */
332 reg = snd_soc_read(codec, CS4270_MUTE);
333 reg &= ~CS4270_MUTE_AUTO;
334 ret = snd_soc_write(codec, CS4270_MUTE, reg);
335 if (ret < 0) {
336 printk(KERN_ERR "cs4270: I2C write failed\n");
337 return ret;
340 /* Thaw and power-up the codec */
342 ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
343 if (ret < 0) {
344 printk(KERN_ERR "cs4270: I2C write failed\n");
345 return ret;
348 return ret;
351 #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
354 * Set the CS4270 external mute
356 * This function toggles the mute bits in the MUTE register. The CS4270's
357 * mute capability is intended for external muting circuitry, so if the
358 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
359 * then this function will do nothing.
361 static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
363 struct snd_soc_codec *codec = dai->codec;
364 int reg6;
366 reg6 = snd_soc_read(codec, CS4270_MUTE);
368 if (mute)
369 reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
370 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
371 else
372 reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
373 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
375 return snd_soc_write(codec, CS4270_MUTE, reg6);
378 #endif
381 * Sampling rate <-> bit patter mapping
383 * This array maps sampling rates to their SNDRV_PCM_RATE_x equivalent.
385 * This is really something that ALSA should provide.
387 * This table is used by cs4270_set_dai_sysclk() to tell ALSA which sampling
388 * rates the CS4270 currently supports.
390 static struct {
391 unsigned int rate;
392 unsigned int bit;
393 } rate_map[] = {
394 {5512, SNDRV_PCM_RATE_5512},
395 {8000, SNDRV_PCM_RATE_8000},
396 {11025, SNDRV_PCM_RATE_11025},
397 {16000, SNDRV_PCM_RATE_16000},
398 {22050, SNDRV_PCM_RATE_22050},
399 {32000, SNDRV_PCM_RATE_32000},
400 {44100, SNDRV_PCM_RATE_44100},
401 {48000, SNDRV_PCM_RATE_48000},
402 {64000, SNDRV_PCM_RATE_64000},
403 {88200, SNDRV_PCM_RATE_88200},
404 {96000, SNDRV_PCM_RATE_96000},
405 {176400, SNDRV_PCM_RATE_176400},
406 {192000, SNDRV_PCM_RATE_192000}
410 * Determine the CS4270 samples rates.
412 * 'freq' is the input frequency to MCLK. The other parameters are ignored.
414 * The value of MCLK is used to determine which sample rates are supported
415 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
416 * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
418 * This function calculates the nine ratios and determines which ones match
419 * a standard sample rate. If there's a match, then it is added to the list
420 * of support sample rates.
422 * This function must be called by the machine driver's 'startup' function,
423 * otherwise the list of supported sample rates will not be available in
424 * time for ALSA.
426 static int cs4270_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
427 int clk_id, unsigned int freq, int dir)
429 struct snd_soc_codec *codec = codec_dai->codec;
430 struct cs4270_private *cs4270 = codec->private_data;
431 unsigned int rates = 0;
432 unsigned int rate_min = -1;
433 unsigned int rate_max = 0;
434 unsigned int i;
436 cs4270->mclk = freq;
438 for (i = 0; i < ARRAY_SIZE(cs4270_mode_ratios); i++) {
439 unsigned int rate;
440 unsigned int j;
441 rate = freq / cs4270_mode_ratios[i].ratio;
442 for (j = 0; j < ARRAY_SIZE(rate_map); j++) {
443 if (rate == rate_map[j].rate) {
444 rates |= rate_map[j].bit;
445 if (rate < rate_min)
446 rate_min = rate;
447 if (rate > rate_max)
448 rate_max = rate;
453 if (!rate_max) {
454 printk(KERN_ERR "cs4270: could not find a valid rate\n");
455 return -EINVAL;
458 codec_dai->playback.rates = rates;
459 codec_dai->playback.rate_min = rate_min;
460 codec_dai->playback.rate_max = rate_max;
462 codec_dai->capture.rates = rates;
463 codec_dai->capture.rate_min = rate_min;
464 codec_dai->capture.rate_max = rate_max;
466 return 0;
470 * Configure the codec for the selected audio format
472 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
473 * codec accordingly.
475 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
476 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
477 * data for playback only, but ASoC currently does not support different
478 * formats for playback vs. record.
480 static int cs4270_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
481 unsigned int format)
483 struct snd_soc_codec *codec = codec_dai->codec;
484 struct cs4270_private *cs4270 = codec->private_data;
485 int ret = 0;
487 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
488 case SND_SOC_DAIFMT_I2S:
489 case SND_SOC_DAIFMT_LEFT_J:
490 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
491 break;
492 default:
493 printk(KERN_ERR "cs4270: invalid DAI format\n");
494 ret = -EINVAL;
497 return ret;
500 static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
503 * Notify the driver that a new I2C bus has been found.
505 * This function is called for each I2C bus in the system. The function
506 * then asks the I2C subsystem to probe that bus at the addresses on which
507 * our device (the CS4270) could exist. If a device is found at one of
508 * those addresses, then our probe function (cs4270_i2c_probe) is called.
510 static int cs4270_i2c_attach(struct i2c_adapter *adapter)
512 return i2c_probe(adapter, &addr_data, cs4270_i2c_probe);
515 static int cs4270_i2c_detach(struct i2c_client *client)
517 struct snd_soc_codec *codec = i2c_get_clientdata(client);
519 i2c_detach_client(client);
520 codec->control_data = NULL;
522 kfree(codec->reg_cache);
523 codec->reg_cache = NULL;
525 kfree(client);
526 return 0;
529 /* A list of non-DAPM controls that the CS4270 supports */
530 static const struct snd_kcontrol_new cs4270_snd_controls[] = {
531 SOC_DOUBLE_R("Master Playback Volume",
532 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 0)
535 static struct i2c_driver cs4270_i2c_driver = {
536 .driver = {
537 .name = "CS4270 I2C",
538 .owner = THIS_MODULE,
540 .id = I2C_DRIVERID_CS4270,
541 .attach_adapter = cs4270_i2c_attach,
542 .detach_client = cs4270_i2c_detach,
546 * Global variable to store socdev for i2c probe function.
548 * If struct i2c_driver had a private_data field, we wouldn't need to use
549 * cs4270_socdec. This is the only way to pass the socdev structure to
550 * cs4270_i2c_probe().
552 * The real solution to cs4270_socdev is to create a mechanism
553 * that maps I2C addresses to snd_soc_device structures. Perhaps the
554 * creation of the snd_soc_device object should be moved out of
555 * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
556 * driver dependent on I2C. The CS4270 supports "stand-alone" mode, whereby
557 * the chip is *not* connected to the I2C bus, but is instead configured via
558 * input pins.
560 static struct snd_soc_device *cs4270_socdev;
563 * Initialize the I2C interface of the CS4270
565 * This function is called for whenever the I2C subsystem finds a device
566 * at a particular address.
568 * Note: snd_soc_new_pcms() must be called before this function can be called,
569 * because of snd_ctl_add().
571 static int cs4270_i2c_probe(struct i2c_adapter *adapter, int addr, int kind)
573 struct snd_soc_device *socdev = cs4270_socdev;
574 struct snd_soc_codec *codec = socdev->codec;
575 struct i2c_client *i2c_client = NULL;
576 int i;
577 int ret = 0;
579 /* Probing all possible addresses has one drawback: if there are
580 multiple CS4270s on the bus, then you cannot specify which
581 socdev is matched with which CS4270. For now, we just reject
582 this I2C device if the socdev already has one attached. */
583 if (codec->control_data)
584 return -ENODEV;
586 /* Note: codec_dai->codec is NULL here */
588 i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
589 if (!i2c_client) {
590 printk(KERN_ERR "cs4270: could not allocate I2C client\n");
591 return -ENOMEM;
594 codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
595 if (!codec->reg_cache) {
596 printk(KERN_ERR "cs4270: could not allocate register cache\n");
597 ret = -ENOMEM;
598 goto error;
601 i2c_set_clientdata(i2c_client, codec);
602 strcpy(i2c_client->name, "CS4270");
604 i2c_client->driver = &cs4270_i2c_driver;
605 i2c_client->adapter = adapter;
606 i2c_client->addr = addr;
608 /* Verify that we have a CS4270 */
610 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
611 if (ret < 0) {
612 printk(KERN_ERR "cs4270: failed to read I2C\n");
613 goto error;
615 /* The top four bits of the chip ID should be 1100. */
616 if ((ret & 0xF0) != 0xC0) {
617 /* The device at this address is not a CS4270 codec */
618 ret = -ENODEV;
619 goto error;
622 printk(KERN_INFO "cs4270: found device at I2C address %X\n", addr);
623 printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
625 /* Tell the I2C layer a new client has arrived */
627 ret = i2c_attach_client(i2c_client);
628 if (ret) {
629 printk(KERN_ERR "cs4270: could not attach codec, "
630 "I2C address %x, error code %i\n", addr, ret);
631 goto error;
634 codec->control_data = i2c_client;
635 codec->read = cs4270_read_reg_cache;
636 codec->write = cs4270_i2c_write;
637 codec->reg_cache_size = CS4270_NUMREGS;
639 /* The I2C interface is set up, so pre-fill our register cache */
641 ret = cs4270_fill_cache(codec);
642 if (ret < 0) {
643 printk(KERN_ERR "cs4270: failed to fill register cache\n");
644 goto error;
647 /* Add the non-DAPM controls */
649 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
650 struct snd_kcontrol *kctrl =
651 snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
653 ret = snd_ctl_add(codec->card, kctrl);
654 if (ret < 0)
655 goto error;
658 return 0;
660 error:
661 if (codec->control_data) {
662 i2c_detach_client(i2c_client);
663 codec->control_data = NULL;
666 kfree(codec->reg_cache);
667 codec->reg_cache = NULL;
668 codec->reg_cache_size = 0;
670 kfree(i2c_client);
672 return ret;
675 #endif
677 struct snd_soc_codec_dai cs4270_dai = {
678 .name = "CS4270",
679 .playback = {
680 .stream_name = "Playback",
681 .channels_min = 1,
682 .channels_max = 2,
683 .rates = 0,
684 .formats = CS4270_FORMATS,
686 .capture = {
687 .stream_name = "Capture",
688 .channels_min = 1,
689 .channels_max = 2,
690 .rates = 0,
691 .formats = CS4270_FORMATS,
693 .dai_ops = {
694 .set_sysclk = cs4270_set_dai_sysclk,
695 .set_fmt = cs4270_set_dai_fmt,
698 EXPORT_SYMBOL_GPL(cs4270_dai);
701 * ASoC probe function
703 * This function is called when the machine driver calls
704 * platform_device_add().
706 static int cs4270_probe(struct platform_device *pdev)
708 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
709 struct snd_soc_codec *codec;
710 int ret = 0;
712 printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
714 /* Allocate enough space for the snd_soc_codec structure
715 and our private data together. */
716 codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
717 sizeof(struct cs4270_private), GFP_KERNEL);
718 if (!codec) {
719 printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
720 return -ENOMEM;
723 mutex_init(&codec->mutex);
724 INIT_LIST_HEAD(&codec->dapm_widgets);
725 INIT_LIST_HEAD(&codec->dapm_paths);
727 codec->name = "CS4270";
728 codec->owner = THIS_MODULE;
729 codec->dai = &cs4270_dai;
730 codec->num_dai = 1;
731 codec->private_data = codec + ALIGN(sizeof(struct snd_soc_codec), 4);
733 socdev->codec = codec;
735 /* Register PCMs */
737 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
738 if (ret < 0) {
739 printk(KERN_ERR "cs4270: failed to create PCMs\n");
740 return ret;
743 #ifdef CONFIG_I2C
744 cs4270_socdev = socdev;
746 ret = i2c_add_driver(&cs4270_i2c_driver);
747 if (ret) {
748 printk(KERN_ERR "cs4270: failed to attach driver");
749 snd_soc_free_pcms(socdev);
750 return ret;
753 /* Did we find a CS4270 on the I2C bus? */
754 if (codec->control_data) {
755 /* Initialize codec ops */
756 cs4270_dai.ops.hw_params = cs4270_hw_params;
757 #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
758 cs4270_dai.dai_ops.digital_mute = cs4270_mute;
759 #endif
760 } else
761 printk(KERN_INFO "cs4270: no I2C device found, "
762 "using stand-alone mode\n");
763 #else
764 printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
765 #endif
767 ret = snd_soc_register_card(socdev);
768 if (ret < 0) {
769 printk(KERN_ERR "cs4270: failed to register card\n");
770 snd_soc_free_pcms(socdev);
771 return ret;
774 return ret;
777 static int cs4270_remove(struct platform_device *pdev)
779 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
781 snd_soc_free_pcms(socdev);
783 #ifdef CONFIG_I2C
784 if (socdev->codec->control_data)
785 i2c_del_driver(&cs4270_i2c_driver);
786 #endif
788 kfree(socdev->codec);
789 socdev->codec = NULL;
791 return 0;
795 * ASoC codec device structure
797 * Assign this variable to the codec_dev field of the machine driver's
798 * snd_soc_device structure.
800 struct snd_soc_codec_device soc_codec_device_cs4270 = {
801 .probe = cs4270_probe,
802 .remove = cs4270_remove
804 EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
806 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
807 MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
808 MODULE_LICENSE("GPL");