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