[ALSA] pcm: add snd_pcm_rate_to_rate_bit() helper
[linux-2.6/kmemtrace.git] / sound / soc / codecs / cs4270.c
blob43d50a4d8089bfd2dc1f4433c655c8d292a93a83
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 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
298 return -EIO;
300 if (i2c_smbus_write_byte_data(codec->control_data, reg, value) == 0) {
301 /* We've written to the hardware, so update the cache */
302 u8 *cache = codec->reg_cache;
303 cache[reg - CS4270_FIRSTREG] = value;
304 return 0;
305 } else {
306 printk(KERN_ERR "cs4270: I2C write of register %u failed\n",
307 reg);
308 return -EIO;
313 * Clock Ratio Selection for Master Mode with I2C enabled
315 * The data for this chart is taken from Table 5 of the CS4270 reference
316 * manual.
318 * This table is used to determine how to program the Mode Control register.
319 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
320 * rates the CS4270 currently supports.
322 * Each element in this array corresponds to the ratios in mclk_ratios[].
323 * These two arrays need to be in sync.
325 * 'speed_mode' is the corresponding bit pattern to be written to the
326 * MODE bits of the Mode Control Register
328 * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
329 * the Mode Control Register.
331 * In situations where a single ratio is represented by multiple speed
332 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
333 * double-speed instead of quad-speed. However, the CS4270 errata states
334 * that Divide-By-1.5 can cause failures, so we avoid that mode where
335 * possible.
337 * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
338 * work if VD = 3.3V. If this effects you, select the
339 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
340 * never select any sample rates that require divide-by-1.5.
342 static struct {
343 u8 speed_mode;
344 u8 mclk;
345 } cs4270_mode_ratios[NUM_MCLK_RATIOS] = {
346 {CS4270_MODE_4X, CS4270_MODE_DIV1}, /* 64 */
347 #ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
348 {CS4270_MODE_4X, CS4270_MODE_DIV15}, /* 96 */
349 #endif
350 {CS4270_MODE_2X, CS4270_MODE_DIV1}, /* 128 */
351 {CS4270_MODE_4X, CS4270_MODE_DIV3}, /* 192 */
352 {CS4270_MODE_1X, CS4270_MODE_DIV1}, /* 256 */
353 {CS4270_MODE_2X, CS4270_MODE_DIV3}, /* 384 */
354 {CS4270_MODE_1X, CS4270_MODE_DIV2}, /* 512 */
355 {CS4270_MODE_1X, CS4270_MODE_DIV3}, /* 768 */
356 {CS4270_MODE_1X, CS4270_MODE_DIV4} /* 1024 */
360 * Program the CS4270 with the given hardware parameters.
362 * The .dai_ops functions are used to provide board-specific data, like
363 * input frequencies, to this driver. This function takes that information,
364 * combines it with the hardware parameters provided, and programs the
365 * hardware accordingly.
367 static int cs4270_hw_params(struct snd_pcm_substream *substream,
368 struct snd_pcm_hw_params *params)
370 struct snd_soc_pcm_runtime *rtd = substream->private_data;
371 struct snd_soc_device *socdev = rtd->socdev;
372 struct snd_soc_codec *codec = socdev->codec;
373 struct cs4270_private *cs4270 = codec->private_data;
374 unsigned int ret = 0;
375 unsigned int i;
376 unsigned int rate;
377 unsigned int ratio;
378 int reg;
380 /* Figure out which MCLK/LRCK ratio to use */
382 rate = params_rate(params); /* Sampling rate, in Hz */
383 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
385 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
386 if (mclk_ratios[i] == ratio)
387 break;
390 if (i == NUM_MCLK_RATIOS) {
391 /* We did not find a matching ratio */
392 printk(KERN_ERR "cs4270: could not find matching ratio\n");
393 return -EINVAL;
396 /* Freeze and power-down the codec */
398 ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
399 CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
400 CS4270_PWRCTL_PDN);
401 if (ret < 0) {
402 printk(KERN_ERR "cs4270: I2C write failed\n");
403 return ret;
406 /* Program the mode control register */
408 reg = snd_soc_read(codec, CS4270_MODE);
409 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
410 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
412 ret = snd_soc_write(codec, CS4270_MODE, reg);
413 if (ret < 0) {
414 printk(KERN_ERR "cs4270: I2C write failed\n");
415 return ret;
418 /* Program the format register */
420 reg = snd_soc_read(codec, CS4270_FORMAT);
421 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
423 switch (cs4270->mode) {
424 case SND_SOC_DAIFMT_I2S:
425 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
426 break;
427 case SND_SOC_DAIFMT_LEFT_J:
428 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
429 break;
430 default:
431 printk(KERN_ERR "cs4270: unknown format\n");
432 return -EINVAL;
435 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
436 if (ret < 0) {
437 printk(KERN_ERR "cs4270: I2C write failed\n");
438 return ret;
441 /* Disable auto-mute. This feature appears to be buggy, because in
442 some situations, auto-mute will not deactivate when it should. */
444 reg = snd_soc_read(codec, CS4270_MUTE);
445 reg &= ~CS4270_MUTE_AUTO;
446 ret = snd_soc_write(codec, CS4270_MUTE, reg);
447 if (ret < 0) {
448 printk(KERN_ERR "cs4270: I2C write failed\n");
449 return ret;
452 /* Thaw and power-up the codec */
454 ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
455 if (ret < 0) {
456 printk(KERN_ERR "cs4270: I2C write failed\n");
457 return ret;
460 return ret;
463 #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
466 * Set the CS4270 external mute
468 * This function toggles the mute bits in the MUTE register. The CS4270's
469 * mute capability is intended for external muting circuitry, so if the
470 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
471 * then this function will do nothing.
473 static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
475 struct snd_soc_codec *codec = dai->codec;
476 int reg6;
478 reg6 = snd_soc_read(codec, CS4270_MUTE);
480 if (mute)
481 reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
482 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
483 else
484 reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
485 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
487 return snd_soc_write(codec, CS4270_MUTE, reg6);
490 #endif
492 static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
495 * Notify the driver that a new I2C bus has been found.
497 * This function is called for each I2C bus in the system. The function
498 * then asks the I2C subsystem to probe that bus at the addresses on which
499 * our device (the CS4270) could exist. If a device is found at one of
500 * those addresses, then our probe function (cs4270_i2c_probe) is called.
502 static int cs4270_i2c_attach(struct i2c_adapter *adapter)
504 return i2c_probe(adapter, &addr_data, cs4270_i2c_probe);
507 static int cs4270_i2c_detach(struct i2c_client *client)
509 struct snd_soc_codec *codec = i2c_get_clientdata(client);
511 i2c_detach_client(client);
512 codec->control_data = NULL;
514 kfree(codec->reg_cache);
515 codec->reg_cache = NULL;
517 kfree(client);
518 return 0;
521 /* A list of non-DAPM controls that the CS4270 supports */
522 static const struct snd_kcontrol_new cs4270_snd_controls[] = {
523 SOC_DOUBLE_R("Master Playback Volume",
524 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 0)
527 static struct i2c_driver cs4270_i2c_driver = {
528 .driver = {
529 .name = "CS4270 I2C",
530 .owner = THIS_MODULE,
532 .id = I2C_DRIVERID_CS4270,
533 .attach_adapter = cs4270_i2c_attach,
534 .detach_client = cs4270_i2c_detach,
538 * Global variable to store socdev for i2c probe function.
540 * If struct i2c_driver had a private_data field, we wouldn't need to use
541 * cs4270_socdec. This is the only way to pass the socdev structure to
542 * cs4270_i2c_probe().
544 * The real solution to cs4270_socdev is to create a mechanism
545 * that maps I2C addresses to snd_soc_device structures. Perhaps the
546 * creation of the snd_soc_device object should be moved out of
547 * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
548 * driver dependent on I2C. The CS4270 supports "stand-alone" mode, whereby
549 * the chip is *not* connected to the I2C bus, but is instead configured via
550 * input pins.
552 static struct snd_soc_device *cs4270_socdev;
555 * Initialize the I2C interface of the CS4270
557 * This function is called for whenever the I2C subsystem finds a device
558 * at a particular address.
560 * Note: snd_soc_new_pcms() must be called before this function can be called,
561 * because of snd_ctl_add().
563 static int cs4270_i2c_probe(struct i2c_adapter *adapter, int addr, int kind)
565 struct snd_soc_device *socdev = cs4270_socdev;
566 struct snd_soc_codec *codec = socdev->codec;
567 struct i2c_client *i2c_client = NULL;
568 int i;
569 int ret = 0;
571 /* Probing all possible addresses has one drawback: if there are
572 multiple CS4270s on the bus, then you cannot specify which
573 socdev is matched with which CS4270. For now, we just reject
574 this I2C device if the socdev already has one attached. */
575 if (codec->control_data)
576 return -ENODEV;
578 /* Note: codec_dai->codec is NULL here */
580 i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
581 if (!i2c_client) {
582 printk(KERN_ERR "cs4270: could not allocate I2C client\n");
583 return -ENOMEM;
586 codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
587 if (!codec->reg_cache) {
588 printk(KERN_ERR "cs4270: could not allocate register cache\n");
589 ret = -ENOMEM;
590 goto error;
593 i2c_set_clientdata(i2c_client, codec);
594 strcpy(i2c_client->name, "CS4270");
596 i2c_client->driver = &cs4270_i2c_driver;
597 i2c_client->adapter = adapter;
598 i2c_client->addr = addr;
600 /* Verify that we have a CS4270 */
602 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
603 if (ret < 0) {
604 printk(KERN_ERR "cs4270: failed to read I2C\n");
605 goto error;
607 /* The top four bits of the chip ID should be 1100. */
608 if ((ret & 0xF0) != 0xC0) {
609 /* The device at this address is not a CS4270 codec */
610 ret = -ENODEV;
611 goto error;
614 printk(KERN_INFO "cs4270: found device at I2C address %X\n", addr);
615 printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
617 /* Tell the I2C layer a new client has arrived */
619 ret = i2c_attach_client(i2c_client);
620 if (ret) {
621 printk(KERN_ERR "cs4270: could not attach codec, "
622 "I2C address %x, error code %i\n", addr, ret);
623 goto error;
626 codec->control_data = i2c_client;
627 codec->read = cs4270_read_reg_cache;
628 codec->write = cs4270_i2c_write;
629 codec->reg_cache_size = CS4270_NUMREGS;
631 /* The I2C interface is set up, so pre-fill our register cache */
633 ret = cs4270_fill_cache(codec);
634 if (ret < 0) {
635 printk(KERN_ERR "cs4270: failed to fill register cache\n");
636 goto error;
639 /* Add the non-DAPM controls */
641 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
642 struct snd_kcontrol *kctrl =
643 snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
645 ret = snd_ctl_add(codec->card, kctrl);
646 if (ret < 0)
647 goto error;
650 return 0;
652 error:
653 if (codec->control_data) {
654 i2c_detach_client(i2c_client);
655 codec->control_data = NULL;
658 kfree(codec->reg_cache);
659 codec->reg_cache = NULL;
660 codec->reg_cache_size = 0;
662 kfree(i2c_client);
664 return ret;
667 #endif
669 struct snd_soc_codec_dai cs4270_dai = {
670 .name = "CS4270",
671 .playback = {
672 .stream_name = "Playback",
673 .channels_min = 1,
674 .channels_max = 2,
675 .rates = 0,
676 .formats = CS4270_FORMATS,
678 .capture = {
679 .stream_name = "Capture",
680 .channels_min = 1,
681 .channels_max = 2,
682 .rates = 0,
683 .formats = CS4270_FORMATS,
685 .dai_ops = {
686 .set_sysclk = cs4270_set_dai_sysclk,
687 .set_fmt = cs4270_set_dai_fmt,
690 EXPORT_SYMBOL_GPL(cs4270_dai);
693 * ASoC probe function
695 * This function is called when the machine driver calls
696 * platform_device_add().
698 static int cs4270_probe(struct platform_device *pdev)
700 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
701 struct snd_soc_codec *codec;
702 int ret = 0;
704 printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
706 /* Allocate enough space for the snd_soc_codec structure
707 and our private data together. */
708 codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
709 sizeof(struct cs4270_private), GFP_KERNEL);
710 if (!codec) {
711 printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
712 return -ENOMEM;
715 mutex_init(&codec->mutex);
716 INIT_LIST_HEAD(&codec->dapm_widgets);
717 INIT_LIST_HEAD(&codec->dapm_paths);
719 codec->name = "CS4270";
720 codec->owner = THIS_MODULE;
721 codec->dai = &cs4270_dai;
722 codec->num_dai = 1;
723 codec->private_data = codec + ALIGN(sizeof(struct snd_soc_codec), 4);
725 socdev->codec = codec;
727 /* Register PCMs */
729 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
730 if (ret < 0) {
731 printk(KERN_ERR "cs4270: failed to create PCMs\n");
732 return ret;
735 #ifdef USE_I2C
736 cs4270_socdev = socdev;
738 ret = i2c_add_driver(&cs4270_i2c_driver);
739 if (ret) {
740 printk(KERN_ERR "cs4270: failed to attach driver");
741 snd_soc_free_pcms(socdev);
742 return ret;
745 /* Did we find a CS4270 on the I2C bus? */
746 if (codec->control_data) {
747 /* Initialize codec ops */
748 cs4270_dai.ops.hw_params = cs4270_hw_params;
749 #ifdef CONFIG_SND_SOC_CS4270_HWMUTE
750 cs4270_dai.dai_ops.digital_mute = cs4270_mute;
751 #endif
752 } else
753 printk(KERN_INFO "cs4270: no I2C device found, "
754 "using stand-alone mode\n");
755 #else
756 printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
757 #endif
759 ret = snd_soc_register_card(socdev);
760 if (ret < 0) {
761 printk(KERN_ERR "cs4270: failed to register card\n");
762 snd_soc_free_pcms(socdev);
763 return ret;
766 return ret;
769 static int cs4270_remove(struct platform_device *pdev)
771 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
773 snd_soc_free_pcms(socdev);
775 #ifdef USE_I2C
776 if (socdev->codec->control_data)
777 i2c_del_driver(&cs4270_i2c_driver);
778 #endif
780 kfree(socdev->codec);
781 socdev->codec = NULL;
783 return 0;
787 * ASoC codec device structure
789 * Assign this variable to the codec_dev field of the machine driver's
790 * snd_soc_device structure.
792 struct snd_soc_codec_device soc_codec_device_cs4270 = {
793 .probe = cs4270_probe,
794 .remove = cs4270_remove
796 EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
798 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
799 MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
800 MODULE_LICENSE("GPL");