blob: 8beae65d083cdaa8c217a4f787a8d2e7861a79bb [file] [log] [blame]
Timur Tabib0c813c2007-07-31 18:18:44 +02001/*
2 * CS4270 ALSA SoC (ASoC) codec driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
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.
10 *
11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
12 *
13 * Current features/limitations:
14 *
Timur Tabi9dbd6272007-08-01 12:22:07 +020015 * 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.
Timur Tabib0c813c2007-07-31 18:18:44 +020020 * 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)
27 */
28
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>
36
37#include "cs4270.h"
38
Timur Tabi9dbd6272007-08-01 12:22:07 +020039/* 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
44
Timur Tabib0c813c2007-07-31 18:18:44 +020045/* Private data for the CS4270 */
46struct cs4270_private {
47 unsigned int mclk; /* Input frequency of the MCLK pin */
48 unsigned int mode; /* The mode (I2S or left-justified) */
49};
50
Timur Tabi9dbd6272007-08-01 12:22:07 +020051/* The number of MCLK/LRCK ratios supported by the CS4270 */
52#define NUM_MCLK_RATIOS 9
53
54/* The actual MCLK/LRCK ratios, in increasing numerical order */
55static unsigned int mclk_ratios[NUM_MCLK_RATIOS] =
56 {64, 96, 128, 192, 256, 384, 512, 768, 1024};
57
58/*
59 * Sampling rate <-> bit patter mapping
60 *
61 * This array maps sampling rates to their SNDRV_PCM_RATE_x equivalent.
62 *
63 * This is really something that ALSA should provide.
64 *
65 * This table is used by cs4270_set_dai_sysclk() to tell ALSA which sampling
66 * rates the CS4270 currently supports.
67 */
68static struct {
69 unsigned int rate;
70 unsigned int bit;
71} rate_map[] = {
72 {5512, SNDRV_PCM_RATE_5512},
73 {8000, SNDRV_PCM_RATE_8000},
74 {11025, SNDRV_PCM_RATE_11025},
75 {16000, SNDRV_PCM_RATE_16000},
76 {22050, SNDRV_PCM_RATE_22050},
77 {32000, SNDRV_PCM_RATE_32000},
78 {44100, SNDRV_PCM_RATE_44100},
79 {48000, SNDRV_PCM_RATE_48000},
80 {64000, SNDRV_PCM_RATE_64000},
81 {88200, SNDRV_PCM_RATE_88200},
82 {96000, SNDRV_PCM_RATE_96000},
83 {176400, SNDRV_PCM_RATE_176400},
84 {192000, SNDRV_PCM_RATE_192000}
85};
86
87/*
88 * Determine the CS4270 samples rates.
89 *
90 * 'freq' is the input frequency to MCLK. The other parameters are ignored.
91 *
92 * The value of MCLK is used to determine which sample rates are supported
93 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
94 * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
95 *
96 * This function calculates the nine ratios and determines which ones match
97 * a standard sample rate. If there's a match, then it is added to the list
98 * of support sample rates.
99 *
100 * This function must be called by the machine driver's 'startup' function,
101 * otherwise the list of supported sample rates will not be available in
102 * time for ALSA.
103 *
104 * Note that in stand-alone mode, the sample rate is determined by input
105 * pins M0, M1, MDIV1, and MDIV2. Also in stand-alone mode, divide-by-3
106 * is not a programmable option. However, divide-by-3 is not an available
107 * option in stand-alone mode. This cases two problems: a ratio of 768 is
108 * not available (it requires divide-by-3) and B) ratios 192 and 384 can
109 * only be selected with divide-by-1.5, but there is an errate that make
110 * this selection difficult.
111 *
112 * In addition, there is no mechanism for communicating with the machine
113 * driver what the input settings can be. This would need to be implemented
114 * for stand-alone mode to work.
115 */
116static int cs4270_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
117 int clk_id, unsigned int freq, int dir)
118{
119 struct snd_soc_codec *codec = codec_dai->codec;
120 struct cs4270_private *cs4270 = codec->private_data;
121 unsigned int rates = 0;
122 unsigned int rate_min = -1;
123 unsigned int rate_max = 0;
124 unsigned int i;
125
126 cs4270->mclk = freq;
127
128 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
129 unsigned int rate;
130 unsigned int j;
131 rate = freq / mclk_ratios[i];
132 for (j = 0; j < ARRAY_SIZE(rate_map); j++) {
133 if (rate == rate_map[j].rate) {
134 rates |= rate_map[j].bit;
135 if (rate < rate_min)
136 rate_min = rate;
137 if (rate > rate_max)
138 rate_max = rate;
139 }
140 }
141 }
142
143 if (!rates) {
144 printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
145 return -EINVAL;
146 }
147
148 codec_dai->playback.rates = rates;
149 codec_dai->playback.rate_min = rate_min;
150 codec_dai->playback.rate_max = rate_max;
151
152 codec_dai->capture.rates = rates;
153 codec_dai->capture.rate_min = rate_min;
154 codec_dai->capture.rate_max = rate_max;
155
156 return 0;
157}
158
159/*
160 * Configure the codec for the selected audio format
161 *
162 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
163 * codec accordingly.
164 *
165 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
166 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
167 * data for playback only, but ASoC currently does not support different
168 * formats for playback vs. record.
169 */
170static int cs4270_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
171 unsigned int format)
172{
173 struct snd_soc_codec *codec = codec_dai->codec;
174 struct cs4270_private *cs4270 = codec->private_data;
175 int ret = 0;
176
177 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
178 case SND_SOC_DAIFMT_I2S:
179 case SND_SOC_DAIFMT_LEFT_J:
180 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
181 break;
182 default:
183 printk(KERN_ERR "cs4270: invalid DAI format\n");
184 ret = -EINVAL;
185 }
186
187 return ret;
188}
189
Timur Tabib0c813c2007-07-31 18:18:44 +0200190/*
191 * The codec isn't really big-endian or little-endian, since the I2S
192 * interface requires data to be sent serially with the MSbit first.
193 * However, to support BE and LE I2S devices, we specify both here. That
194 * way, ALSA will always match the bit patterns.
195 */
196#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
197 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
198 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
199 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
200 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
201 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
202
Timur Tabi9dbd6272007-08-01 12:22:07 +0200203#ifdef USE_I2C
Timur Tabib0c813c2007-07-31 18:18:44 +0200204
205/* CS4270 registers addresses */
206#define CS4270_CHIPID 0x01 /* Chip ID */
207#define CS4270_PWRCTL 0x02 /* Power Control */
208#define CS4270_MODE 0x03 /* Mode Control */
209#define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
210#define CS4270_TRANS 0x05 /* Transition Control */
211#define CS4270_MUTE 0x06 /* Mute Control */
212#define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
213#define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
214
215#define CS4270_FIRSTREG 0x01
216#define CS4270_LASTREG 0x08
217#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
218
219/* Bit masks for the CS4270 registers */
220#define CS4270_CHIPID_ID 0xF0
221#define CS4270_CHIPID_REV 0x0F
222#define CS4270_PWRCTL_FREEZE 0x80
223#define CS4270_PWRCTL_PDN_ADC 0x20
224#define CS4270_PWRCTL_PDN_DAC 0x02
225#define CS4270_PWRCTL_PDN 0x01
226#define CS4270_MODE_SPEED_MASK 0x30
227#define CS4270_MODE_1X 0x00
228#define CS4270_MODE_2X 0x10
229#define CS4270_MODE_4X 0x20
230#define CS4270_MODE_SLAVE 0x30
231#define CS4270_MODE_DIV_MASK 0x0E
232#define CS4270_MODE_DIV1 0x00
233#define CS4270_MODE_DIV15 0x02
234#define CS4270_MODE_DIV2 0x04
235#define CS4270_MODE_DIV3 0x06
236#define CS4270_MODE_DIV4 0x08
237#define CS4270_MODE_POPGUARD 0x01
238#define CS4270_FORMAT_FREEZE_A 0x80
239#define CS4270_FORMAT_FREEZE_B 0x40
240#define CS4270_FORMAT_LOOPBACK 0x20
241#define CS4270_FORMAT_DAC_MASK 0x18
242#define CS4270_FORMAT_DAC_LJ 0x00
243#define CS4270_FORMAT_DAC_I2S 0x08
244#define CS4270_FORMAT_DAC_RJ16 0x18
245#define CS4270_FORMAT_DAC_RJ24 0x10
246#define CS4270_FORMAT_ADC_MASK 0x01
247#define CS4270_FORMAT_ADC_LJ 0x00
248#define CS4270_FORMAT_ADC_I2S 0x01
249#define CS4270_TRANS_ONE_VOL 0x80
250#define CS4270_TRANS_SOFT 0x40
251#define CS4270_TRANS_ZERO 0x20
252#define CS4270_TRANS_INV_ADC_A 0x08
253#define CS4270_TRANS_INV_ADC_B 0x10
254#define CS4270_TRANS_INV_DAC_A 0x02
255#define CS4270_TRANS_INV_DAC_B 0x04
256#define CS4270_TRANS_DEEMPH 0x01
257#define CS4270_MUTE_AUTO 0x20
258#define CS4270_MUTE_ADC_A 0x08
259#define CS4270_MUTE_ADC_B 0x10
260#define CS4270_MUTE_POLARITY 0x04
261#define CS4270_MUTE_DAC_A 0x01
262#define CS4270_MUTE_DAC_B 0x02
263
264/*
265 * A list of addresses on which this CS4270 could use. I2C addresses are
266 * 7 bits. For the CS4270, the upper four bits are always 1001, and the
267 * lower three bits are determined via the AD2, AD1, and AD0 pins
268 * (respectively).
269 */
270static unsigned short normal_i2c[] = {
271 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END
272};
273I2C_CLIENT_INSMOD;
274
275/*
276 * Pre-fill the CS4270 register cache.
277 *
278 * We use the auto-increment feature of the CS4270 to read all registers in
279 * one shot.
280 */
281static int cs4270_fill_cache(struct snd_soc_codec *codec)
282{
283 u8 *cache = codec->reg_cache;
284 struct i2c_client *i2c_client = codec->control_data;
285 s32 length;
286
287 length = i2c_smbus_read_i2c_block_data(i2c_client,
288 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
289
290 if (length != CS4270_NUMREGS) {
Timur Tabi9dbd6272007-08-01 12:22:07 +0200291 printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
Timur Tabib0c813c2007-07-31 18:18:44 +0200292 i2c_client->addr);
293 return -EIO;
294 }
295
296 return 0;
297}
298
299/*
300 * Read from the CS4270 register cache.
301 *
302 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
303 * After the initial read to pre-fill the cache, the CS4270 never updates
304 * the register values, so we won't have a cache coherncy problem.
305 */
306static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
307 unsigned int reg)
308{
309 u8 *cache = codec->reg_cache;
310
311 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
312 return -EIO;
313
314 return cache[reg - CS4270_FIRSTREG];
315}
316
317/*
318 * Write to a CS4270 register via the I2C bus.
319 *
320 * This function writes the given value to the given CS4270 register, and
321 * also updates the register cache.
322 *
323 * Note that we don't use the hw_write function pointer of snd_soc_codec.
324 * That's because it's too clunky: the hw_write_t prototype does not match
325 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
326 */
327static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
328 unsigned int value)
329{
330 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
331 return -EIO;
332
333 if (i2c_smbus_write_byte_data(codec->control_data, reg, value) == 0) {
334 /* We've written to the hardware, so update the cache */
335 u8 *cache = codec->reg_cache;
336 cache[reg - CS4270_FIRSTREG] = value;
337 return 0;
338 } else {
Timur Tabi9dbd6272007-08-01 12:22:07 +0200339 printk(KERN_ERR "cs4270: I2C write of register %u failed\n",
340 reg);
Timur Tabib0c813c2007-07-31 18:18:44 +0200341 return -EIO;
342 }
343}
344
345/*
Timur Tabi9dbd6272007-08-01 12:22:07 +0200346 * Clock Ratio Selection for Master Mode with I2C enabled
Timur Tabib0c813c2007-07-31 18:18:44 +0200347 *
348 * The data for this chart is taken from Table 5 of the CS4270 reference
349 * manual.
350 *
351 * This table is used to determine how to program the Mode Control register.
352 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
353 * rates the CS4270 currently supports.
354 *
Timur Tabi9dbd6272007-08-01 12:22:07 +0200355 * Each element in this array corresponds to the ratios in mclk_ratios[].
356 * These two arrays need to be in sync.
Timur Tabib0c813c2007-07-31 18:18:44 +0200357 *
358 * 'speed_mode' is the corresponding bit pattern to be written to the
359 * MODE bits of the Mode Control Register
360 *
361 * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
362 * the Mode Control Register.
363 *
364 * In situations where a single ratio is represented by multiple speed
365 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
366 * double-speed instead of quad-speed. However, the CS4270 errata states
367 * that Divide-By-1.5 can cause failures, so we avoid that mode where
368 * possible.
369 *
370 * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
371 * work if VD = 3.3V. If this effects you, select the
372 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
373 * never select any sample rates that require divide-by-1.5.
374 */
375static struct {
Timur Tabib0c813c2007-07-31 18:18:44 +0200376 u8 speed_mode;
377 u8 mclk;
Timur Tabi9dbd6272007-08-01 12:22:07 +0200378} cs4270_mode_ratios[NUM_MCLK_RATIOS] = {
379 {CS4270_MODE_4X, CS4270_MODE_DIV1}, /* 64 */
Timur Tabib0c813c2007-07-31 18:18:44 +0200380#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
Timur Tabi9dbd6272007-08-01 12:22:07 +0200381 {CS4270_MODE_4X, CS4270_MODE_DIV15}, /* 96 */
Timur Tabib0c813c2007-07-31 18:18:44 +0200382#endif
Timur Tabi9dbd6272007-08-01 12:22:07 +0200383 {CS4270_MODE_2X, CS4270_MODE_DIV1}, /* 128 */
384 {CS4270_MODE_4X, CS4270_MODE_DIV3}, /* 192 */
385 {CS4270_MODE_1X, CS4270_MODE_DIV1}, /* 256 */
386 {CS4270_MODE_2X, CS4270_MODE_DIV3}, /* 384 */
387 {CS4270_MODE_1X, CS4270_MODE_DIV2}, /* 512 */
388 {CS4270_MODE_1X, CS4270_MODE_DIV3}, /* 768 */
389 {CS4270_MODE_1X, CS4270_MODE_DIV4} /* 1024 */
Timur Tabib0c813c2007-07-31 18:18:44 +0200390};
391
392/*
393 * Program the CS4270 with the given hardware parameters.
394 *
395 * The .dai_ops functions are used to provide board-specific data, like
396 * input frequencies, to this driver. This function takes that information,
397 * combines it with the hardware parameters provided, and programs the
398 * hardware accordingly.
399 */
400static int cs4270_hw_params(struct snd_pcm_substream *substream,
401 struct snd_pcm_hw_params *params)
402{
403 struct snd_soc_pcm_runtime *rtd = substream->private_data;
404 struct snd_soc_device *socdev = rtd->socdev;
405 struct snd_soc_codec *codec = socdev->codec;
406 struct cs4270_private *cs4270 = codec->private_data;
407 unsigned int ret = 0;
408 unsigned int i;
409 unsigned int rate;
410 unsigned int ratio;
411 int reg;
412
413 /* Figure out which MCLK/LRCK ratio to use */
414
415 rate = params_rate(params); /* Sampling rate, in Hz */
416 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
417
Timur Tabi9dbd6272007-08-01 12:22:07 +0200418 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
419 if (mclk_ratios[i] == ratio)
Timur Tabib0c813c2007-07-31 18:18:44 +0200420 break;
421 }
422
Timur Tabi9dbd6272007-08-01 12:22:07 +0200423 if (i == NUM_MCLK_RATIOS) {
Timur Tabib0c813c2007-07-31 18:18:44 +0200424 /* We did not find a matching ratio */
425 printk(KERN_ERR "cs4270: could not find matching ratio\n");
426 return -EINVAL;
427 }
428
429 /* Freeze and power-down the codec */
430
431 ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
432 CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
433 CS4270_PWRCTL_PDN);
434 if (ret < 0) {
435 printk(KERN_ERR "cs4270: I2C write failed\n");
436 return ret;
437 }
438
439 /* Program the mode control register */
440
441 reg = snd_soc_read(codec, CS4270_MODE);
442 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
443 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
444
445 ret = snd_soc_write(codec, CS4270_MODE, reg);
446 if (ret < 0) {
447 printk(KERN_ERR "cs4270: I2C write failed\n");
448 return ret;
449 }
450
451 /* Program the format register */
452
453 reg = snd_soc_read(codec, CS4270_FORMAT);
454 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
455
456 switch (cs4270->mode) {
457 case SND_SOC_DAIFMT_I2S:
458 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
459 break;
460 case SND_SOC_DAIFMT_LEFT_J:
461 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
462 break;
463 default:
464 printk(KERN_ERR "cs4270: unknown format\n");
465 return -EINVAL;
466 }
467
468 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
469 if (ret < 0) {
470 printk(KERN_ERR "cs4270: I2C write failed\n");
471 return ret;
472 }
473
474 /* Disable auto-mute. This feature appears to be buggy, because in
475 some situations, auto-mute will not deactivate when it should. */
476
477 reg = snd_soc_read(codec, CS4270_MUTE);
478 reg &= ~CS4270_MUTE_AUTO;
479 ret = snd_soc_write(codec, CS4270_MUTE, reg);
480 if (ret < 0) {
481 printk(KERN_ERR "cs4270: I2C write failed\n");
482 return ret;
483 }
484
485 /* Thaw and power-up the codec */
486
487 ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
488 if (ret < 0) {
489 printk(KERN_ERR "cs4270: I2C write failed\n");
490 return ret;
491 }
492
493 return ret;
494}
495
496#ifdef CONFIG_SND_SOC_CS4270_HWMUTE
497
498/*
499 * Set the CS4270 external mute
500 *
501 * This function toggles the mute bits in the MUTE register. The CS4270's
502 * mute capability is intended for external muting circuitry, so if the
503 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
504 * then this function will do nothing.
505 */
506static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
507{
508 struct snd_soc_codec *codec = dai->codec;
509 int reg6;
510
511 reg6 = snd_soc_read(codec, CS4270_MUTE);
512
513 if (mute)
514 reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
515 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
516 else
517 reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
518 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
519
520 return snd_soc_write(codec, CS4270_MUTE, reg6);
521}
522
523#endif
524
Timur Tabib0c813c2007-07-31 18:18:44 +0200525static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
526
527/*
528 * Notify the driver that a new I2C bus has been found.
529 *
530 * This function is called for each I2C bus in the system. The function
531 * then asks the I2C subsystem to probe that bus at the addresses on which
532 * our device (the CS4270) could exist. If a device is found at one of
533 * those addresses, then our probe function (cs4270_i2c_probe) is called.
534 */
535static int cs4270_i2c_attach(struct i2c_adapter *adapter)
536{
537 return i2c_probe(adapter, &addr_data, cs4270_i2c_probe);
538}
539
540static int cs4270_i2c_detach(struct i2c_client *client)
541{
542 struct snd_soc_codec *codec = i2c_get_clientdata(client);
543
544 i2c_detach_client(client);
545 codec->control_data = NULL;
546
547 kfree(codec->reg_cache);
548 codec->reg_cache = NULL;
549
550 kfree(client);
551 return 0;
552}
553
554/* A list of non-DAPM controls that the CS4270 supports */
555static const struct snd_kcontrol_new cs4270_snd_controls[] = {
556 SOC_DOUBLE_R("Master Playback Volume",
557 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 0)
558};
559
560static struct i2c_driver cs4270_i2c_driver = {
561 .driver = {
562 .name = "CS4270 I2C",
563 .owner = THIS_MODULE,
564 },
565 .id = I2C_DRIVERID_CS4270,
566 .attach_adapter = cs4270_i2c_attach,
567 .detach_client = cs4270_i2c_detach,
568};
569
570/*
571 * Global variable to store socdev for i2c probe function.
572 *
573 * If struct i2c_driver had a private_data field, we wouldn't need to use
574 * cs4270_socdec. This is the only way to pass the socdev structure to
575 * cs4270_i2c_probe().
576 *
577 * The real solution to cs4270_socdev is to create a mechanism
578 * that maps I2C addresses to snd_soc_device structures. Perhaps the
579 * creation of the snd_soc_device object should be moved out of
580 * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
581 * driver dependent on I2C. The CS4270 supports "stand-alone" mode, whereby
582 * the chip is *not* connected to the I2C bus, but is instead configured via
583 * input pins.
584 */
585static struct snd_soc_device *cs4270_socdev;
586
587/*
588 * Initialize the I2C interface of the CS4270
589 *
590 * This function is called for whenever the I2C subsystem finds a device
591 * at a particular address.
592 *
593 * Note: snd_soc_new_pcms() must be called before this function can be called,
594 * because of snd_ctl_add().
595 */
596static int cs4270_i2c_probe(struct i2c_adapter *adapter, int addr, int kind)
597{
598 struct snd_soc_device *socdev = cs4270_socdev;
599 struct snd_soc_codec *codec = socdev->codec;
600 struct i2c_client *i2c_client = NULL;
601 int i;
602 int ret = 0;
603
604 /* Probing all possible addresses has one drawback: if there are
605 multiple CS4270s on the bus, then you cannot specify which
606 socdev is matched with which CS4270. For now, we just reject
607 this I2C device if the socdev already has one attached. */
608 if (codec->control_data)
609 return -ENODEV;
610
611 /* Note: codec_dai->codec is NULL here */
612
613 i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
614 if (!i2c_client) {
615 printk(KERN_ERR "cs4270: could not allocate I2C client\n");
616 return -ENOMEM;
617 }
618
619 codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
620 if (!codec->reg_cache) {
621 printk(KERN_ERR "cs4270: could not allocate register cache\n");
622 ret = -ENOMEM;
623 goto error;
624 }
625
626 i2c_set_clientdata(i2c_client, codec);
627 strcpy(i2c_client->name, "CS4270");
628
629 i2c_client->driver = &cs4270_i2c_driver;
630 i2c_client->adapter = adapter;
631 i2c_client->addr = addr;
632
633 /* Verify that we have a CS4270 */
634
635 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
636 if (ret < 0) {
637 printk(KERN_ERR "cs4270: failed to read I2C\n");
638 goto error;
639 }
640 /* The top four bits of the chip ID should be 1100. */
641 if ((ret & 0xF0) != 0xC0) {
642 /* The device at this address is not a CS4270 codec */
643 ret = -ENODEV;
644 goto error;
645 }
646
647 printk(KERN_INFO "cs4270: found device at I2C address %X\n", addr);
648 printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
649
650 /* Tell the I2C layer a new client has arrived */
651
652 ret = i2c_attach_client(i2c_client);
653 if (ret) {
654 printk(KERN_ERR "cs4270: could not attach codec, "
655 "I2C address %x, error code %i\n", addr, ret);
656 goto error;
657 }
658
659 codec->control_data = i2c_client;
660 codec->read = cs4270_read_reg_cache;
661 codec->write = cs4270_i2c_write;
662 codec->reg_cache_size = CS4270_NUMREGS;
663
664 /* The I2C interface is set up, so pre-fill our register cache */
665
666 ret = cs4270_fill_cache(codec);
667 if (ret < 0) {
668 printk(KERN_ERR "cs4270: failed to fill register cache\n");
669 goto error;
670 }
671
672 /* Add the non-DAPM controls */
673
674 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
675 struct snd_kcontrol *kctrl =
676 snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
677
678 ret = snd_ctl_add(codec->card, kctrl);
679 if (ret < 0)
680 goto error;
681 }
682
683 return 0;
684
685error:
686 if (codec->control_data) {
687 i2c_detach_client(i2c_client);
688 codec->control_data = NULL;
689 }
690
691 kfree(codec->reg_cache);
692 codec->reg_cache = NULL;
693 codec->reg_cache_size = 0;
694
695 kfree(i2c_client);
696
697 return ret;
698}
699
700#endif
701
702struct snd_soc_codec_dai cs4270_dai = {
703 .name = "CS4270",
704 .playback = {
705 .stream_name = "Playback",
706 .channels_min = 1,
707 .channels_max = 2,
708 .rates = 0,
709 .formats = CS4270_FORMATS,
710 },
711 .capture = {
712 .stream_name = "Capture",
713 .channels_min = 1,
714 .channels_max = 2,
715 .rates = 0,
716 .formats = CS4270_FORMATS,
717 },
718 .dai_ops = {
719 .set_sysclk = cs4270_set_dai_sysclk,
720 .set_fmt = cs4270_set_dai_fmt,
721 }
722};
723EXPORT_SYMBOL_GPL(cs4270_dai);
724
725/*
726 * ASoC probe function
727 *
728 * This function is called when the machine driver calls
729 * platform_device_add().
730 */
731static int cs4270_probe(struct platform_device *pdev)
732{
733 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
734 struct snd_soc_codec *codec;
735 int ret = 0;
736
737 printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
738
739 /* Allocate enough space for the snd_soc_codec structure
740 and our private data together. */
741 codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
742 sizeof(struct cs4270_private), GFP_KERNEL);
743 if (!codec) {
744 printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
745 return -ENOMEM;
746 }
747
748 mutex_init(&codec->mutex);
749 INIT_LIST_HEAD(&codec->dapm_widgets);
750 INIT_LIST_HEAD(&codec->dapm_paths);
751
752 codec->name = "CS4270";
753 codec->owner = THIS_MODULE;
754 codec->dai = &cs4270_dai;
755 codec->num_dai = 1;
756 codec->private_data = codec + ALIGN(sizeof(struct snd_soc_codec), 4);
757
758 socdev->codec = codec;
759
760 /* Register PCMs */
761
762 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
763 if (ret < 0) {
764 printk(KERN_ERR "cs4270: failed to create PCMs\n");
765 return ret;
766 }
767
Timur Tabi9dbd6272007-08-01 12:22:07 +0200768#ifdef USE_I2C
Timur Tabib0c813c2007-07-31 18:18:44 +0200769 cs4270_socdev = socdev;
770
771 ret = i2c_add_driver(&cs4270_i2c_driver);
772 if (ret) {
773 printk(KERN_ERR "cs4270: failed to attach driver");
774 snd_soc_free_pcms(socdev);
775 return ret;
776 }
777
778 /* Did we find a CS4270 on the I2C bus? */
779 if (codec->control_data) {
780 /* Initialize codec ops */
781 cs4270_dai.ops.hw_params = cs4270_hw_params;
782#ifdef CONFIG_SND_SOC_CS4270_HWMUTE
783 cs4270_dai.dai_ops.digital_mute = cs4270_mute;
784#endif
785 } else
786 printk(KERN_INFO "cs4270: no I2C device found, "
787 "using stand-alone mode\n");
788#else
789 printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
790#endif
791
792 ret = snd_soc_register_card(socdev);
793 if (ret < 0) {
794 printk(KERN_ERR "cs4270: failed to register card\n");
795 snd_soc_free_pcms(socdev);
796 return ret;
797 }
798
799 return ret;
800}
801
802static int cs4270_remove(struct platform_device *pdev)
803{
804 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
805
806 snd_soc_free_pcms(socdev);
807
Timur Tabi9dbd6272007-08-01 12:22:07 +0200808#ifdef USE_I2C
Timur Tabib0c813c2007-07-31 18:18:44 +0200809 if (socdev->codec->control_data)
810 i2c_del_driver(&cs4270_i2c_driver);
811#endif
812
813 kfree(socdev->codec);
814 socdev->codec = NULL;
815
816 return 0;
817}
818
819/*
820 * ASoC codec device structure
821 *
822 * Assign this variable to the codec_dev field of the machine driver's
823 * snd_soc_device structure.
824 */
825struct snd_soc_codec_device soc_codec_device_cs4270 = {
826 .probe = cs4270_probe,
827 .remove = cs4270_remove
828};
829EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
830
831MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
832MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
833MODULE_LICENSE("GPL");