blob: 43d50a4d8089bfd2dc1f4433c655c8d292a93a83 [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/*
Timur Tabi9dbd6272007-08-01 12:22:07 +020059 * Determine the CS4270 samples rates.
60 *
61 * 'freq' is the input frequency to MCLK. The other parameters are ignored.
62 *
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.
66 *
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.
70 *
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.
74 *
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.
82 *
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.
86 */
87static int cs4270_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
88 int clk_id, unsigned int freq, int dir)
89{
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;
96
97 cs4270->mclk = freq;
98
99 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200100 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;
Timur Tabi9dbd6272007-08-01 12:22:07 +0200106 }
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200107 /* FIXME: soc should support a rate list */
108 rates &= ~SNDRV_PCM_RATE_KNOT;
Timur Tabi9dbd6272007-08-01 12:22:07 +0200109
110 if (!rates) {
111 printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
112 return -EINVAL;
113 }
114
115 codec_dai->playback.rates = rates;
116 codec_dai->playback.rate_min = rate_min;
117 codec_dai->playback.rate_max = rate_max;
118
119 codec_dai->capture.rates = rates;
120 codec_dai->capture.rate_min = rate_min;
121 codec_dai->capture.rate_max = rate_max;
122
123 return 0;
124}
125
126/*
127 * Configure the codec for the selected audio format
128 *
129 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
130 * codec accordingly.
131 *
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.
136 */
137static int cs4270_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
138 unsigned int format)
139{
140 struct snd_soc_codec *codec = codec_dai->codec;
141 struct cs4270_private *cs4270 = codec->private_data;
142 int ret = 0;
143
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;
152 }
153
154 return ret;
155}
156
Timur Tabib0c813c2007-07-31 18:18:44 +0200157/*
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.
162 */
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)
169
Timur Tabi9dbd6272007-08-01 12:22:07 +0200170#ifdef USE_I2C
Timur Tabib0c813c2007-07-31 18:18:44 +0200171
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 */
181
182#define CS4270_FIRSTREG 0x01
183#define CS4270_LASTREG 0x08
184#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
185
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
230
231/*
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).
236 */
237static unsigned short normal_i2c[] = {
238 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END
239};
240I2C_CLIENT_INSMOD;
241
242/*
243 * Pre-fill the CS4270 register cache.
244 *
245 * We use the auto-increment feature of the CS4270 to read all registers in
246 * one shot.
247 */
248static int cs4270_fill_cache(struct snd_soc_codec *codec)
249{
250 u8 *cache = codec->reg_cache;
251 struct i2c_client *i2c_client = codec->control_data;
252 s32 length;
253
254 length = i2c_smbus_read_i2c_block_data(i2c_client,
255 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
256
257 if (length != CS4270_NUMREGS) {
Timur Tabi9dbd6272007-08-01 12:22:07 +0200258 printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
Timur Tabib0c813c2007-07-31 18:18:44 +0200259 i2c_client->addr);
260 return -EIO;
261 }
262
263 return 0;
264}
265
266/*
267 * Read from the CS4270 register cache.
268 *
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.
272 */
273static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
274 unsigned int reg)
275{
276 u8 *cache = codec->reg_cache;
277
278 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
279 return -EIO;
280
281 return cache[reg - CS4270_FIRSTREG];
282}
283
284/*
285 * Write to a CS4270 register via the I2C bus.
286 *
287 * This function writes the given value to the given CS4270 register, and
288 * also updates the register cache.
289 *
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.
293 */
294static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
295 unsigned int value)
296{
297 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
298 return -EIO;
299
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 {
Timur Tabi9dbd6272007-08-01 12:22:07 +0200306 printk(KERN_ERR "cs4270: I2C write of register %u failed\n",
307 reg);
Timur Tabib0c813c2007-07-31 18:18:44 +0200308 return -EIO;
309 }
310}
311
312/*
Timur Tabi9dbd6272007-08-01 12:22:07 +0200313 * Clock Ratio Selection for Master Mode with I2C enabled
Timur Tabib0c813c2007-07-31 18:18:44 +0200314 *
315 * The data for this chart is taken from Table 5 of the CS4270 reference
316 * manual.
317 *
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.
321 *
Timur Tabi9dbd6272007-08-01 12:22:07 +0200322 * Each element in this array corresponds to the ratios in mclk_ratios[].
323 * These two arrays need to be in sync.
Timur Tabib0c813c2007-07-31 18:18:44 +0200324 *
325 * 'speed_mode' is the corresponding bit pattern to be written to the
326 * MODE bits of the Mode Control Register
327 *
328 * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
329 * the Mode Control Register.
330 *
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.
336 *
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.
341 */
342static struct {
Timur Tabib0c813c2007-07-31 18:18:44 +0200343 u8 speed_mode;
344 u8 mclk;
Timur Tabi9dbd6272007-08-01 12:22:07 +0200345} cs4270_mode_ratios[NUM_MCLK_RATIOS] = {
346 {CS4270_MODE_4X, CS4270_MODE_DIV1}, /* 64 */
Timur Tabib0c813c2007-07-31 18:18:44 +0200347#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
Timur Tabi9dbd6272007-08-01 12:22:07 +0200348 {CS4270_MODE_4X, CS4270_MODE_DIV15}, /* 96 */
Timur Tabib0c813c2007-07-31 18:18:44 +0200349#endif
Timur Tabi9dbd6272007-08-01 12:22:07 +0200350 {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 */
Timur Tabib0c813c2007-07-31 18:18:44 +0200357};
358
359/*
360 * Program the CS4270 with the given hardware parameters.
361 *
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.
366 */
367static int cs4270_hw_params(struct snd_pcm_substream *substream,
368 struct snd_pcm_hw_params *params)
369{
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;
379
380 /* Figure out which MCLK/LRCK ratio to use */
381
382 rate = params_rate(params); /* Sampling rate, in Hz */
383 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
384
Timur Tabi9dbd6272007-08-01 12:22:07 +0200385 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
386 if (mclk_ratios[i] == ratio)
Timur Tabib0c813c2007-07-31 18:18:44 +0200387 break;
388 }
389
Timur Tabi9dbd6272007-08-01 12:22:07 +0200390 if (i == NUM_MCLK_RATIOS) {
Timur Tabib0c813c2007-07-31 18:18:44 +0200391 /* We did not find a matching ratio */
392 printk(KERN_ERR "cs4270: could not find matching ratio\n");
393 return -EINVAL;
394 }
395
396 /* Freeze and power-down the codec */
397
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;
404 }
405
406 /* Program the mode control register */
407
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;
411
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;
416 }
417
418 /* Program the format register */
419
420 reg = snd_soc_read(codec, CS4270_FORMAT);
421 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
422
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;
433 }
434
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;
439 }
440
441 /* Disable auto-mute. This feature appears to be buggy, because in
442 some situations, auto-mute will not deactivate when it should. */
443
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;
450 }
451
452 /* Thaw and power-up the codec */
453
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;
458 }
459
460 return ret;
461}
462
463#ifdef CONFIG_SND_SOC_CS4270_HWMUTE
464
465/*
466 * Set the CS4270 external mute
467 *
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.
472 */
473static int cs4270_mute(struct snd_soc_codec_dai *dai, int mute)
474{
475 struct snd_soc_codec *codec = dai->codec;
476 int reg6;
477
478 reg6 = snd_soc_read(codec, CS4270_MUTE);
479
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);
486
487 return snd_soc_write(codec, CS4270_MUTE, reg6);
488}
489
490#endif
491
Timur Tabib0c813c2007-07-31 18:18:44 +0200492static int cs4270_i2c_probe(struct i2c_adapter *adap, int addr, int kind);
493
494/*
495 * Notify the driver that a new I2C bus has been found.
496 *
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.
501 */
502static int cs4270_i2c_attach(struct i2c_adapter *adapter)
503{
504 return i2c_probe(adapter, &addr_data, cs4270_i2c_probe);
505}
506
507static int cs4270_i2c_detach(struct i2c_client *client)
508{
509 struct snd_soc_codec *codec = i2c_get_clientdata(client);
510
511 i2c_detach_client(client);
512 codec->control_data = NULL;
513
514 kfree(codec->reg_cache);
515 codec->reg_cache = NULL;
516
517 kfree(client);
518 return 0;
519}
520
521/* A list of non-DAPM controls that the CS4270 supports */
522static const struct snd_kcontrol_new cs4270_snd_controls[] = {
523 SOC_DOUBLE_R("Master Playback Volume",
524 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 0)
525};
526
527static struct i2c_driver cs4270_i2c_driver = {
528 .driver = {
529 .name = "CS4270 I2C",
530 .owner = THIS_MODULE,
531 },
532 .id = I2C_DRIVERID_CS4270,
533 .attach_adapter = cs4270_i2c_attach,
534 .detach_client = cs4270_i2c_detach,
535};
536
537/*
538 * Global variable to store socdev for i2c probe function.
539 *
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().
543 *
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.
551 */
552static struct snd_soc_device *cs4270_socdev;
553
554/*
555 * Initialize the I2C interface of the CS4270
556 *
557 * This function is called for whenever the I2C subsystem finds a device
558 * at a particular address.
559 *
560 * Note: snd_soc_new_pcms() must be called before this function can be called,
561 * because of snd_ctl_add().
562 */
563static int cs4270_i2c_probe(struct i2c_adapter *adapter, int addr, int kind)
564{
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;
570
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;
577
578 /* Note: codec_dai->codec is NULL here */
579
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;
584 }
585
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;
591 }
592
593 i2c_set_clientdata(i2c_client, codec);
594 strcpy(i2c_client->name, "CS4270");
595
596 i2c_client->driver = &cs4270_i2c_driver;
597 i2c_client->adapter = adapter;
598 i2c_client->addr = addr;
599
600 /* Verify that we have a CS4270 */
601
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;
606 }
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;
612 }
613
614 printk(KERN_INFO "cs4270: found device at I2C address %X\n", addr);
615 printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
616
617 /* Tell the I2C layer a new client has arrived */
618
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;
624 }
625
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;
630
631 /* The I2C interface is set up, so pre-fill our register cache */
632
633 ret = cs4270_fill_cache(codec);
634 if (ret < 0) {
635 printk(KERN_ERR "cs4270: failed to fill register cache\n");
636 goto error;
637 }
638
639 /* Add the non-DAPM controls */
640
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);
644
645 ret = snd_ctl_add(codec->card, kctrl);
646 if (ret < 0)
647 goto error;
648 }
649
650 return 0;
651
652error:
653 if (codec->control_data) {
654 i2c_detach_client(i2c_client);
655 codec->control_data = NULL;
656 }
657
658 kfree(codec->reg_cache);
659 codec->reg_cache = NULL;
660 codec->reg_cache_size = 0;
661
662 kfree(i2c_client);
663
664 return ret;
665}
666
667#endif
668
669struct 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,
677 },
678 .capture = {
679 .stream_name = "Capture",
680 .channels_min = 1,
681 .channels_max = 2,
682 .rates = 0,
683 .formats = CS4270_FORMATS,
684 },
685 .dai_ops = {
686 .set_sysclk = cs4270_set_dai_sysclk,
687 .set_fmt = cs4270_set_dai_fmt,
688 }
689};
690EXPORT_SYMBOL_GPL(cs4270_dai);
691
692/*
693 * ASoC probe function
694 *
695 * This function is called when the machine driver calls
696 * platform_device_add().
697 */
698static int cs4270_probe(struct platform_device *pdev)
699{
700 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
701 struct snd_soc_codec *codec;
702 int ret = 0;
703
704 printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
705
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;
713 }
714
715 mutex_init(&codec->mutex);
716 INIT_LIST_HEAD(&codec->dapm_widgets);
717 INIT_LIST_HEAD(&codec->dapm_paths);
718
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);
724
725 socdev->codec = codec;
726
727 /* Register PCMs */
728
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;
733 }
734
Timur Tabi9dbd6272007-08-01 12:22:07 +0200735#ifdef USE_I2C
Timur Tabib0c813c2007-07-31 18:18:44 +0200736 cs4270_socdev = socdev;
737
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;
743 }
744
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
758
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;
764 }
765
766 return ret;
767}
768
769static int cs4270_remove(struct platform_device *pdev)
770{
771 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
772
773 snd_soc_free_pcms(socdev);
774
Timur Tabi9dbd6272007-08-01 12:22:07 +0200775#ifdef USE_I2C
Timur Tabib0c813c2007-07-31 18:18:44 +0200776 if (socdev->codec->control_data)
777 i2c_del_driver(&cs4270_i2c_driver);
778#endif
779
780 kfree(socdev->codec);
781 socdev->codec = NULL;
782
783 return 0;
784}
785
786/*
787 * ASoC codec device structure
788 *
789 * Assign this variable to the codec_dev field of the machine driver's
790 * snd_soc_device structure.
791 */
792struct snd_soc_codec_device soc_codec_device_cs4270 = {
793 .probe = cs4270_probe,
794 .remove = cs4270_remove
795};
796EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
797
798MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
799MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
800MODULE_LICENSE("GPL");