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