aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/cs4270.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/cs4270.c')
-rw-r--r--sound/soc/codecs/cs4270.c667
1 files changed, 338 insertions, 329 deletions
diff --git a/sound/soc/codecs/cs4270.c b/sound/soc/codecs/cs4270.c
index f1aa0c34421c..7fa09a387622 100644
--- a/sound/soc/codecs/cs4270.c
+++ b/sound/soc/codecs/cs4270.c
@@ -3,27 +3,22 @@
3 * 3 *
4 * Author: Timur Tabi <timur@freescale.com> 4 * Author: Timur Tabi <timur@freescale.com>
5 * 5 *
6 * Copyright 2007 Freescale Semiconductor, Inc. This file is licensed under 6 * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed
7 * the terms of the GNU General Public License version 2. This program 7 * under the terms of the GNU General Public License version 2. This
8 * is licensed "as is" without any warranty of any kind, whether express 8 * program is licensed "as is" without any warranty of any kind, whether
9 * or implied. 9 * express or implied.
10 * 10 *
11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec. 11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
12 * 12 *
13 * Current features/limitations: 13 * Current features/limitations:
14 * 14 *
15 * 1) Software mode is supported. Stand-alone mode is automatically 15 * - Software mode is supported. Stand-alone mode is not supported.
16 * selected if I2C is disabled or if a CS4270 is not found on the I2C 16 * - Only I2C is supported, not SPI
17 * bus. However, stand-alone mode is only partially implemented because 17 * - Support for master and slave mode
18 * there is no mechanism yet for this driver and the machine driver to 18 * - The machine driver's 'startup' function must call
19 * communicate the values of the M0, M1, MCLK1, and MCLK2 pins. 19 * cs4270_set_dai_sysclk() with the value of MCLK.
20 * 2) Only I2C is supported, not SPI 20 * - Only I2S and left-justified modes are supported
21 * 3) Only Master mode is supported, not Slave. 21 * - Power management is not supported
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 */ 22 */
28 23
29#include <linux/module.h> 24#include <linux/module.h>
@@ -35,18 +30,6 @@
35 30
36#include "cs4270.h" 31#include "cs4270.h"
37 32
38/* 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
44/* 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/* 33/*
51 * The codec isn't really big-endian or little-endian, since the I2S 34 * 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. 35 * interface requires data to be sent serially with the MSbit first.
@@ -60,8 +43,6 @@ struct cs4270_private {
60 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \ 43 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
61 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE) 44 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
62 45
63#ifdef USE_I2C
64
65/* CS4270 registers addresses */ 46/* CS4270 registers addresses */
66#define CS4270_CHIPID 0x01 /* Chip ID */ 47#define CS4270_CHIPID 0x01 /* Chip ID */
67#define CS4270_PWRCTL 0x02 /* Power Control */ 48#define CS4270_PWRCTL 0x02 /* Power Control */
@@ -121,8 +102,22 @@ struct cs4270_private {
121#define CS4270_MUTE_DAC_A 0x01 102#define CS4270_MUTE_DAC_A 0x01
122#define CS4270_MUTE_DAC_B 0x02 103#define CS4270_MUTE_DAC_B 0x02
123 104
124/* 105/* Private data for the CS4270 */
125 * Clock Ratio Selection for Master Mode with I2C enabled 106struct cs4270_private {
107 struct snd_soc_codec codec;
108 u8 reg_cache[CS4270_NUMREGS];
109 unsigned int mclk; /* Input frequency of the MCLK pin */
110 unsigned int mode; /* The mode (I2S or left-justified) */
111 unsigned int slave_mode;
112};
113
114/**
115 * struct cs4270_mode_ratios - clock ratio tables
116 * @ratio: the ratio of MCLK to the sample rate
117 * @speed_mode: the Speed Mode bits to set in the Mode Control register for
118 * this ratio
119 * @mclk: the Ratio Select bits to set in the Mode Control register for this
120 * ratio
126 * 121 *
127 * The data for this chart is taken from Table 5 of the CS4270 reference 122 * The data for this chart is taken from Table 5 of the CS4270 reference
128 * manual. 123 * manual.
@@ -131,31 +126,30 @@ struct cs4270_private {
131 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling 126 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
132 * rates the CS4270 currently supports. 127 * rates the CS4270 currently supports.
133 * 128 *
134 * Each element in this array corresponds to the ratios in mclk_ratios[]. 129 * @speed_mode is the corresponding bit pattern to be written to the
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 130 * MODE bits of the Mode Control Register
139 * 131 *
140 * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of 132 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
141 * the Mode Control Register. 133 * the Mode Control Register.
142 * 134 *
143 * In situations where a single ratio is represented by multiple speed 135 * 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 136 * 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 137 * 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 138 * that divide-By-1.5 can cause failures, so we avoid that mode where
147 * possible. 139 * possible.
148 * 140 *
149 * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not 141 * 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 142 * work if Vd is 3.3V. If this effects you, select the
151 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will 143 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
152 * never select any sample rates that require divide-by-1.5. 144 * never select any sample rates that require divide-by-1.5.
153 */ 145 */
154static struct { 146struct cs4270_mode_ratios {
155 unsigned int ratio; 147 unsigned int ratio;
156 u8 speed_mode; 148 u8 speed_mode;
157 u8 mclk; 149 u8 mclk;
158} cs4270_mode_ratios[] = { 150};
151
152static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
159 {64, CS4270_MODE_4X, CS4270_MODE_DIV1}, 153 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
160#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA 154#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
161 {96, CS4270_MODE_4X, CS4270_MODE_DIV15}, 155 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
@@ -172,34 +166,27 @@ static struct {
172/* The number of MCLK/LRCK ratios supported by the CS4270 */ 166/* The number of MCLK/LRCK ratios supported by the CS4270 */
173#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios) 167#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
174 168
175/* 169/**
176 * Determine the CS4270 samples rates. 170 * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
171 * @codec_dai: the codec DAI
172 * @clk_id: the clock ID (ignored)
173 * @freq: the MCLK input frequency
174 * @dir: the clock direction (ignored)
177 * 175 *
178 * 'freq' is the input frequency to MCLK. The other parameters are ignored. 176 * This function is used to tell the codec driver what the input MCLK
177 * frequency is.
179 * 178 *
180 * The value of MCLK is used to determine which sample rates are supported 179 * 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 180 * 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. 181 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
183 * 182 *
184 * This function calculates the nine ratios and determines which ones match 183 * 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 184 * a standard sample rate. If there's a match, then it is added to the list
186 * of support sample rates. 185 * of supported sample rates.
187 * 186 *
188 * This function must be called by the machine driver's 'startup' function, 187 * 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 188 * otherwise the list of supported sample rates will not be available in
190 * time for ALSA. 189 * 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 */ 190 */
204static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai, 191static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
205 int clk_id, unsigned int freq, int dir) 192 int clk_id, unsigned int freq, int dir)
@@ -225,7 +212,7 @@ static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
225 rates &= ~SNDRV_PCM_RATE_KNOT; 212 rates &= ~SNDRV_PCM_RATE_KNOT;
226 213
227 if (!rates) { 214 if (!rates) {
228 printk(KERN_ERR "cs4270: could not find a valid sample rate\n"); 215 dev_err(codec->dev, "could not find a valid sample rate\n");
229 return -EINVAL; 216 return -EINVAL;
230 } 217 }
231 218
@@ -240,8 +227,10 @@ static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
240 return 0; 227 return 0;
241} 228}
242 229
243/* 230/**
244 * Configure the codec for the selected audio format 231 * cs4270_set_dai_fmt - configure the codec for the selected audio format
232 * @codec_dai: the codec DAI
233 * @format: a SND_SOC_DAIFMT_x value indicating the data format
245 * 234 *
246 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the 235 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
247 * codec accordingly. 236 * codec accordingly.
@@ -258,32 +247,43 @@ static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
258 struct cs4270_private *cs4270 = codec->private_data; 247 struct cs4270_private *cs4270 = codec->private_data;
259 int ret = 0; 248 int ret = 0;
260 249
250 /* set DAI format */
261 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) { 251 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
262 case SND_SOC_DAIFMT_I2S: 252 case SND_SOC_DAIFMT_I2S:
263 case SND_SOC_DAIFMT_LEFT_J: 253 case SND_SOC_DAIFMT_LEFT_J:
264 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK; 254 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
265 break; 255 break;
266 default: 256 default:
267 printk(KERN_ERR "cs4270: invalid DAI format\n"); 257 dev_err(codec->dev, "invalid dai format\n");
258 ret = -EINVAL;
259 }
260
261 /* set master/slave audio interface */
262 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
263 case SND_SOC_DAIFMT_CBS_CFS:
264 cs4270->slave_mode = 1;
265 break;
266 case SND_SOC_DAIFMT_CBM_CFM:
267 cs4270->slave_mode = 0;
268 break;
269 default:
270 /* all other modes are unsupported by the hardware */
268 ret = -EINVAL; 271 ret = -EINVAL;
269 } 272 }
270 273
271 return ret; 274 return ret;
272} 275}
273 276
274/* 277/**
275 * A list of addresses on which this CS4270 could use. I2C addresses are 278 * cs4270_fill_cache - pre-fill the CS4270 register cache.
276 * 7 bits. For the CS4270, the upper four bits are always 1001, and the 279 * @codec: the codec for this CS4270
277 * lower three bits are determined via the AD2, AD1, and AD0 pins 280 *
278 * (respectively). 281 * This function fills in the CS4270 register cache by reading the register
279 */ 282 * values from the hardware.
280static const unsigned short normal_i2c[] = { 283 *
281 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, I2C_CLIENT_END 284 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
282}; 285 * After the initial read to pre-fill the cache, the CS4270 never updates
283I2C_CLIENT_INSMOD; 286 * the register values, so we won't have a cache coherency problem.
284
285/*
286 * Pre-fill the CS4270 register cache.
287 * 287 *
288 * We use the auto-increment feature of the CS4270 to read all registers in 288 * We use the auto-increment feature of the CS4270 to read all registers in
289 * one shot. 289 * one shot.
@@ -298,7 +298,7 @@ static int cs4270_fill_cache(struct snd_soc_codec *codec)
298 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache); 298 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
299 299
300 if (length != CS4270_NUMREGS) { 300 if (length != CS4270_NUMREGS) {
301 printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n", 301 dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
302 i2c_client->addr); 302 i2c_client->addr);
303 return -EIO; 303 return -EIO;
304 } 304 }
@@ -306,12 +306,17 @@ static int cs4270_fill_cache(struct snd_soc_codec *codec)
306 return 0; 306 return 0;
307} 307}
308 308
309/* 309/**
310 * Read from the CS4270 register cache. 310 * cs4270_read_reg_cache - read from the CS4270 register cache.
311 * @codec: the codec for this CS4270
312 * @reg: the register to read
313 *
314 * This function returns the value for a given register. It reads only from
315 * the register cache, not the hardware itself.
311 * 316 *
312 * This CS4270 registers are cached to avoid excessive I2C I/O operations. 317 * 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 318 * 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. 319 * the register values, so we won't have a cache coherency problem.
315 */ 320 */
316static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec, 321static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
317 unsigned int reg) 322 unsigned int reg)
@@ -324,8 +329,11 @@ static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
324 return cache[reg - CS4270_FIRSTREG]; 329 return cache[reg - CS4270_FIRSTREG];
325} 330}
326 331
327/* 332/**
328 * Write to a CS4270 register via the I2C bus. 333 * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
334 * @codec: the codec for this CS4270
335 * @reg: the register to write
336 * @value: the value to write to the register
329 * 337 *
330 * This function writes the given value to the given CS4270 register, and 338 * This function writes the given value to the given CS4270 register, and
331 * also updates the register cache. 339 * also updates the register cache.
@@ -346,7 +354,7 @@ static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
346 if (cache[reg - CS4270_FIRSTREG] != value) { 354 if (cache[reg - CS4270_FIRSTREG] != value) {
347 struct i2c_client *client = codec->control_data; 355 struct i2c_client *client = codec->control_data;
348 if (i2c_smbus_write_byte_data(client, reg, value)) { 356 if (i2c_smbus_write_byte_data(client, reg, value)) {
349 printk(KERN_ERR "cs4270: I2C write failed\n"); 357 dev_err(codec->dev, "i2c write failed\n");
350 return -EIO; 358 return -EIO;
351 } 359 }
352 360
@@ -357,11 +365,17 @@ static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
357 return 0; 365 return 0;
358} 366}
359 367
360/* 368/**
361 * Program the CS4270 with the given hardware parameters. 369 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
370 * @substream: the audio stream
371 * @params: the hardware parameters to set
372 * @dai: the SOC DAI (ignored)
373 *
374 * This function programs the hardware with the values provided.
375 * Specifically, the sample rate and the data format.
362 * 376 *
363 * The .ops functions are used to provide board-specific data, like 377 * The .ops functions are used to provide board-specific data, like input
364 * input frequencies, to this driver. This function takes that information, 378 * frequencies, to this driver. This function takes that information,
365 * combines it with the hardware parameters provided, and programs the 379 * combines it with the hardware parameters provided, and programs the
366 * hardware accordingly. 380 * hardware accordingly.
367 */ 381 */
@@ -371,7 +385,7 @@ static int cs4270_hw_params(struct snd_pcm_substream *substream,
371{ 385{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data; 386 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct snd_soc_device *socdev = rtd->socdev; 387 struct snd_soc_device *socdev = rtd->socdev;
374 struct snd_soc_codec *codec = socdev->codec; 388 struct snd_soc_codec *codec = socdev->card->codec;
375 struct cs4270_private *cs4270 = codec->private_data; 389 struct cs4270_private *cs4270 = codec->private_data;
376 int ret; 390 int ret;
377 unsigned int i; 391 unsigned int i;
@@ -391,33 +405,28 @@ static int cs4270_hw_params(struct snd_pcm_substream *substream,
391 405
392 if (i == NUM_MCLK_RATIOS) { 406 if (i == NUM_MCLK_RATIOS) {
393 /* We did not find a matching ratio */ 407 /* We did not find a matching ratio */
394 printk(KERN_ERR "cs4270: could not find matching ratio\n"); 408 dev_err(codec->dev, "could not find matching ratio\n");
395 return -EINVAL; 409 return -EINVAL;
396 } 410 }
397 411
398 /* Freeze and power-down the codec */ 412 /* Set the sample rate */
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 413
410 reg = snd_soc_read(codec, CS4270_MODE); 414 reg = snd_soc_read(codec, CS4270_MODE);
411 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK); 415 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
412 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk; 416 reg |= cs4270_mode_ratios[i].mclk;
417
418 if (cs4270->slave_mode)
419 reg |= CS4270_MODE_SLAVE;
420 else
421 reg |= cs4270_mode_ratios[i].speed_mode;
413 422
414 ret = snd_soc_write(codec, CS4270_MODE, reg); 423 ret = snd_soc_write(codec, CS4270_MODE, reg);
415 if (ret < 0) { 424 if (ret < 0) {
416 printk(KERN_ERR "cs4270: I2C write failed\n"); 425 dev_err(codec->dev, "i2c write failed\n");
417 return ret; 426 return ret;
418 } 427 }
419 428
420 /* Program the format register */ 429 /* Set the DAI format */
421 430
422 reg = snd_soc_read(codec, CS4270_FORMAT); 431 reg = snd_soc_read(codec, CS4270_FORMAT);
423 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK); 432 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
@@ -430,55 +439,23 @@ static int cs4270_hw_params(struct snd_pcm_substream *substream,
430 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ; 439 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
431 break; 440 break;
432 default: 441 default:
433 printk(KERN_ERR "cs4270: unknown format\n"); 442 dev_err(codec->dev, "unknown dai format\n");
434 return -EINVAL; 443 return -EINVAL;
435 } 444 }
436 445
437 ret = snd_soc_write(codec, CS4270_FORMAT, reg); 446 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
438 if (ret < 0) { 447 if (ret < 0) {
439 printk(KERN_ERR "cs4270: I2C write failed\n"); 448 dev_err(codec->dev, "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 /* Disable automatic volume control. It's enabled by default, and
455 * it causes volume change commands to be delayed, sometimes until
456 * after playback has started.
457 */
458
459 reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
460 reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
461 ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
462 if (ret < 0) {
463 printk(KERN_ERR "I2C write failed\n");
464 return ret;
465 }
466
467 /* Thaw and power-up the codec */
468
469 ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
470 if (ret < 0) {
471 printk(KERN_ERR "cs4270: I2C write failed\n");
472 return ret; 449 return ret;
473 } 450 }
474 451
475 return ret; 452 return ret;
476} 453}
477 454
478#ifdef CONFIG_SND_SOC_CS4270_HWMUTE 455/**
479 456 * cs4270_mute - enable/disable the CS4270 external mute
480/* 457 * @dai: the SOC DAI
481 * Set the CS4270 external mute 458 * @mute: 0 = disable mute, 1 = enable mute
482 * 459 *
483 * This function toggles the mute bits in the MUTE register. The CS4270's 460 * This function toggles the mute bits in the MUTE register. The CS4270's
484 * mute capability is intended for external muting circuitry, so if the 461 * mute capability is intended for external muting circuitry, so if the
@@ -493,276 +470,306 @@ static int cs4270_mute(struct snd_soc_dai *dai, int mute)
493 reg6 = snd_soc_read(codec, CS4270_MUTE); 470 reg6 = snd_soc_read(codec, CS4270_MUTE);
494 471
495 if (mute) 472 if (mute)
496 reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B | 473 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
497 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
498 else 474 else
499 reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B | 475 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
500 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
501 476
502 return snd_soc_write(codec, CS4270_MUTE, reg6); 477 return snd_soc_write(codec, CS4270_MUTE, reg6);
503} 478}
504 479
505#endif
506
507static int cs4270_i2c_probe(struct i2c_client *, const struct i2c_device_id *);
508
509/* A list of non-DAPM controls that the CS4270 supports */ 480/* A list of non-DAPM controls that the CS4270 supports */
510static const struct snd_kcontrol_new cs4270_snd_controls[] = { 481static const struct snd_kcontrol_new cs4270_snd_controls[] = {
511 SOC_DOUBLE_R("Master Playback Volume", 482 SOC_DOUBLE_R("Master Playback Volume",
512 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1) 483 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
513}; 484 SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
514 485 SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
515static const struct i2c_device_id cs4270_id[] = { 486 SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
516 {"cs4270", 0}, 487 SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
517 {} 488 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
518}; 489 SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 0)
519MODULE_DEVICE_TABLE(i2c, cs4270_id);
520
521static struct i2c_driver cs4270_i2c_driver = {
522 .driver = {
523 .name = "CS4270 I2C",
524 .owner = THIS_MODULE,
525 },
526 .id_table = cs4270_id,
527 .probe = cs4270_i2c_probe,
528}; 490};
529 491
530/* 492/*
531 * Global variable to store socdev for i2c probe function. 493 * cs4270_codec - global variable to store codec for the ASoC probe function
532 * 494 *
533 * If struct i2c_driver had a private_data field, we wouldn't need to use 495 * If struct i2c_driver had a private_data field, we wouldn't need to use
534 * cs4270_socdec. This is the only way to pass the socdev structure to 496 * cs4270_codec. This is the only way to pass the codec structure from
535 * cs4270_i2c_probe(). 497 * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
536 * 498 * way to synchronize these two functions. cs4270_i2c_probe() can be called
537 * The real solution to cs4270_socdev is to create a mechanism 499 * multiple times before cs4270_probe() is called even once. So for now, we
538 * that maps I2C addresses to snd_soc_device structures. Perhaps the 500 * also only allow cs4270_i2c_probe() to be run once. That means that we do
539 * creation of the snd_soc_device object should be moved out of 501 * not support more than one cs4270 device in the system, at least for now.
540 * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
541 * driver dependent on I2C. The CS4270 supports "stand-alone" mode, whereby
542 * the chip is *not* connected to the I2C bus, but is instead configured via
543 * input pins.
544 */ 502 */
545static struct snd_soc_device *cs4270_socdev; 503static struct snd_soc_codec *cs4270_codec;
546 504
547/* 505static struct snd_soc_dai_ops cs4270_dai_ops = {
548 * Initialize the I2C interface of the CS4270 506 .hw_params = cs4270_hw_params,
549 * 507 .set_sysclk = cs4270_set_dai_sysclk,
550 * This function is called for whenever the I2C subsystem finds a device 508 .set_fmt = cs4270_set_dai_fmt,
551 * at a particular address. 509 .digital_mute = cs4270_mute,
510};
511
512struct snd_soc_dai cs4270_dai = {
513 .name = "cs4270",
514 .playback = {
515 .stream_name = "Playback",
516 .channels_min = 1,
517 .channels_max = 2,
518 .rates = 0,
519 .formats = CS4270_FORMATS,
520 },
521 .capture = {
522 .stream_name = "Capture",
523 .channels_min = 1,
524 .channels_max = 2,
525 .rates = 0,
526 .formats = CS4270_FORMATS,
527 },
528 .ops = &cs4270_dai_ops,
529};
530EXPORT_SYMBOL_GPL(cs4270_dai);
531
532/**
533 * cs4270_probe - ASoC probe function
534 * @pdev: platform device
552 * 535 *
553 * Note: snd_soc_new_pcms() must be called before this function can be called, 536 * This function is called when ASoC has all the pieces it needs to
554 * because of snd_ctl_add(). 537 * instantiate a sound driver.
555 */ 538 */
556static int cs4270_i2c_probe(struct i2c_client *i2c_client, 539static int cs4270_probe(struct platform_device *pdev)
557 const struct i2c_device_id *id)
558{ 540{
559 struct snd_soc_device *socdev = cs4270_socdev; 541 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
560 struct snd_soc_codec *codec = socdev->codec; 542 struct snd_soc_codec *codec = cs4270_codec;
561 int i; 543 int ret;
562 int ret = 0;
563
564 /* Probing all possible addresses has one drawback: if there are
565 multiple CS4270s on the bus, then you cannot specify which
566 socdev is matched with which CS4270. For now, we just reject
567 this I2C device if the socdev already has one attached. */
568 if (codec->control_data)
569 return -ENODEV;
570
571 /* Note: codec_dai->codec is NULL here */
572
573 codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
574 if (!codec->reg_cache) {
575 printk(KERN_ERR "cs4270: could not allocate register cache\n");
576 ret = -ENOMEM;
577 goto error;
578 }
579 544
580 /* Verify that we have a CS4270 */ 545 /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
546 socdev->card->codec = codec;
581 547
582 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID); 548 /* Register PCMs */
549 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
583 if (ret < 0) { 550 if (ret < 0) {
584 printk(KERN_ERR "cs4270: failed to read I2C\n"); 551 dev_err(codec->dev, "failed to create pcms\n");
585 goto error; 552 return ret;
586 }
587 /* The top four bits of the chip ID should be 1100. */
588 if ((ret & 0xF0) != 0xC0) {
589 /* The device at this address is not a CS4270 codec */
590 ret = -ENODEV;
591 goto error;
592 } 553 }
593 554
594 printk(KERN_INFO "cs4270: found device at I2C address %X\n", 555 /* Add the non-DAPM controls */
595 i2c_client->addr); 556 ret = snd_soc_add_controls(codec, cs4270_snd_controls,
596 printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF); 557 ARRAY_SIZE(cs4270_snd_controls));
597
598 codec->control_data = i2c_client;
599 codec->read = cs4270_read_reg_cache;
600 codec->write = cs4270_i2c_write;
601 codec->reg_cache_size = CS4270_NUMREGS;
602
603 /* The I2C interface is set up, so pre-fill our register cache */
604
605 ret = cs4270_fill_cache(codec);
606 if (ret < 0) { 558 if (ret < 0) {
607 printk(KERN_ERR "cs4270: failed to fill register cache\n"); 559 dev_err(codec->dev, "failed to add controls\n");
608 goto error; 560 goto error_free_pcms;
609 } 561 }
610 562
611 /* Add the non-DAPM controls */ 563 /* And finally, register the socdev */
612 564 ret = snd_soc_init_card(socdev);
613 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) { 565 if (ret < 0) {
614 struct snd_kcontrol *kctrl = 566 dev_err(codec->dev, "failed to register card\n");
615 snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL); 567 goto error_free_pcms;
616
617 ret = snd_ctl_add(codec->card, kctrl);
618 if (ret < 0)
619 goto error;
620 } 568 }
621 569
622 i2c_set_clientdata(i2c_client, codec);
623
624 return 0; 570 return 0;
625 571
626error: 572error_free_pcms:
627 codec->control_data = NULL; 573 snd_soc_free_pcms(socdev);
628
629 kfree(codec->reg_cache);
630 codec->reg_cache = NULL;
631 codec->reg_cache_size = 0;
632 574
633 return ret; 575 return ret;
634} 576}
635 577
636#endif /* USE_I2C*/ 578/**
579 * cs4270_remove - ASoC remove function
580 * @pdev: platform device
581 *
582 * This function is the counterpart to cs4270_probe().
583 */
584static int cs4270_remove(struct platform_device *pdev)
585{
586 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
637 587
638struct snd_soc_dai cs4270_dai = { 588 snd_soc_free_pcms(socdev);
639 .name = "CS4270", 589
640 .playback = { 590 return 0;
641 .stream_name = "Playback",
642 .channels_min = 1,
643 .channels_max = 2,
644 .rates = 0,
645 .formats = CS4270_FORMATS,
646 },
647 .capture = {
648 .stream_name = "Capture",
649 .channels_min = 1,
650 .channels_max = 2,
651 .rates = 0,
652 .formats = CS4270_FORMATS,
653 },
654}; 591};
655EXPORT_SYMBOL_GPL(cs4270_dai);
656 592
657/* 593/**
658 * ASoC probe function 594 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
595 * @i2c_client: the I2C client object
596 * @id: the I2C device ID (ignored)
659 * 597 *
660 * This function is called when the machine driver calls 598 * This function is called whenever the I2C subsystem finds a device that
661 * platform_device_add(). 599 * matches the device ID given via a prior call to i2c_add_driver().
662 */ 600 */
663static int cs4270_probe(struct platform_device *pdev) 601static int cs4270_i2c_probe(struct i2c_client *i2c_client,
602 const struct i2c_device_id *id)
664{ 603{
665 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
666 struct snd_soc_codec *codec; 604 struct snd_soc_codec *codec;
667 int ret = 0; 605 struct cs4270_private *cs4270;
606 unsigned int reg;
607 int ret;
668 608
669 printk(KERN_INFO "CS4270 ALSA SoC Codec\n"); 609 /* For now, we only support one cs4270 device in the system. See the
610 * comment for cs4270_codec.
611 */
612 if (cs4270_codec) {
613 dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
614 i2c_client->addr);
615 dev_err(&i2c_client->dev, "only one per board allowed\n");
616 /* Should we return something other than ENODEV here? */
617 return -ENODEV;
618 }
619
620 /* Verify that we have a CS4270 */
621
622 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
623 if (ret < 0) {
624 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
625 i2c_client->addr);
626 return ret;
627 }
628 /* The top four bits of the chip ID should be 1100. */
629 if ((ret & 0xF0) != 0xC0) {
630 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
631 i2c_client->addr);
632 return -ENODEV;
633 }
634
635 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
636 i2c_client->addr);
637 dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
670 638
671 /* Allocate enough space for the snd_soc_codec structure 639 /* Allocate enough space for the snd_soc_codec structure
672 and our private data together. */ 640 and our private data together. */
673 codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) + 641 cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
674 sizeof(struct cs4270_private), GFP_KERNEL); 642 if (!cs4270) {
675 if (!codec) { 643 dev_err(&i2c_client->dev, "could not allocate codec\n");
676 printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
677 return -ENOMEM; 644 return -ENOMEM;
678 } 645 }
646 codec = &cs4270->codec;
679 647
680 mutex_init(&codec->mutex); 648 mutex_init(&codec->mutex);
681 INIT_LIST_HEAD(&codec->dapm_widgets); 649 INIT_LIST_HEAD(&codec->dapm_widgets);
682 INIT_LIST_HEAD(&codec->dapm_paths); 650 INIT_LIST_HEAD(&codec->dapm_paths);
683 651
652 codec->dev = &i2c_client->dev;
684 codec->name = "CS4270"; 653 codec->name = "CS4270";
685 codec->owner = THIS_MODULE; 654 codec->owner = THIS_MODULE;
686 codec->dai = &cs4270_dai; 655 codec->dai = &cs4270_dai;
687 codec->num_dai = 1; 656 codec->num_dai = 1;
688 codec->private_data = (void *) codec + 657 codec->private_data = cs4270;
689 ALIGN(sizeof(struct snd_soc_codec), 4); 658 codec->control_data = i2c_client;
690 659 codec->read = cs4270_read_reg_cache;
691 socdev->codec = codec; 660 codec->write = cs4270_i2c_write;
661 codec->reg_cache = cs4270->reg_cache;
662 codec->reg_cache_size = CS4270_NUMREGS;
692 663
693 /* Register PCMs */ 664 /* The I2C interface is set up, so pre-fill our register cache */
694 665
695 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 666 ret = cs4270_fill_cache(codec);
696 if (ret < 0) { 667 if (ret < 0) {
697 printk(KERN_ERR "cs4270: failed to create PCMs\n"); 668 dev_err(&i2c_client->dev, "failed to fill register cache\n");
698 goto error_free_codec; 669 goto error_free_codec;
699 } 670 }
700 671
701#ifdef USE_I2C 672 /* Disable auto-mute. This feature appears to be buggy. In some
702 cs4270_socdev = socdev; 673 * situations, auto-mute will not deactivate when it should, so we want
674 * this feature disabled by default. An application (e.g. alsactl) can
675 * re-enabled it by using the controls.
676 */
703 677
704 ret = i2c_add_driver(&cs4270_i2c_driver); 678 reg = cs4270_read_reg_cache(codec, CS4270_MUTE);
705 if (ret) { 679 reg &= ~CS4270_MUTE_AUTO;
706 printk(KERN_ERR "cs4270: failed to attach driver"); 680 ret = cs4270_i2c_write(codec, CS4270_MUTE, reg);
707 goto error_free_pcms; 681 if (ret < 0) {
682 dev_err(&i2c_client->dev, "i2c write failed\n");
683 return ret;
708 } 684 }
709 685
710 /* Did we find a CS4270 on the I2C bus? */ 686 /* Disable automatic volume control. The hardware enables, and it
711 if (codec->control_data) { 687 * causes volume change commands to be delayed, sometimes until after
712 /* Initialize codec ops */ 688 * playback has started. An application (e.g. alsactl) can
713 cs4270_dai.ops.hw_params = cs4270_hw_params; 689 * re-enabled it by using the controls.
714 cs4270_dai.ops.set_sysclk = cs4270_set_dai_sysclk; 690 */
715 cs4270_dai.ops.set_fmt = cs4270_set_dai_fmt;
716#ifdef CONFIG_SND_SOC_CS4270_HWMUTE
717 cs4270_dai.ops.digital_mute = cs4270_mute;
718#endif
719 } else
720 printk(KERN_INFO "cs4270: no I2C device found, "
721 "using stand-alone mode\n");
722#else
723 printk(KERN_INFO "cs4270: I2C disabled, using stand-alone mode\n");
724#endif
725 691
726 ret = snd_soc_init_card(socdev); 692 reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
693 reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
694 ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
727 if (ret < 0) { 695 if (ret < 0) {
728 printk(KERN_ERR "cs4270: failed to register card\n"); 696 dev_err(&i2c_client->dev, "i2c write failed\n");
729 goto error_del_driver; 697 return ret;
730 } 698 }
731 699
732 return 0; 700 /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
701 * structure for each CS4270 device, but the machine driver needs to
702 * have a pointer to the DAI structure, so for now it must be a global
703 * variable.
704 */
705 cs4270_dai.dev = &i2c_client->dev;
733 706
734error_del_driver: 707 /* Register the DAI. If all the other ASoC driver have already
735#ifdef USE_I2C 708 * registered, then this will call our probe function, so
736 i2c_del_driver(&cs4270_i2c_driver); 709 * cs4270_codec needs to be ready.
710 */
711 cs4270_codec = codec;
712 ret = snd_soc_register_dai(&cs4270_dai);
713 if (ret < 0) {
714 dev_err(&i2c_client->dev, "failed to register DAIe\n");
715 goto error_free_codec;
716 }
737 717
738error_free_pcms: 718 i2c_set_clientdata(i2c_client, cs4270);
739#endif 719
740 snd_soc_free_pcms(socdev); 720 return 0;
741 721
742error_free_codec: 722error_free_codec:
743 kfree(socdev->codec); 723 kfree(cs4270);
744 socdev->codec = NULL; 724 cs4270_codec = NULL;
725 cs4270_dai.dev = NULL;
745 726
746 return ret; 727 return ret;
747} 728}
748 729
749static int cs4270_remove(struct platform_device *pdev) 730/**
731 * cs4270_i2c_remove - remove an I2C device
732 * @i2c_client: the I2C client object
733 *
734 * This function is the counterpart to cs4270_i2c_probe().
735 */
736static int cs4270_i2c_remove(struct i2c_client *i2c_client)
750{ 737{
751 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 738 struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
752
753 snd_soc_free_pcms(socdev);
754
755#ifdef USE_I2C
756 i2c_del_driver(&cs4270_i2c_driver);
757#endif
758 739
759 kfree(socdev->codec); 740 kfree(cs4270);
760 socdev->codec = NULL; 741 cs4270_codec = NULL;
742 cs4270_dai.dev = NULL;
761 743
762 return 0; 744 return 0;
763} 745}
764 746
765/* 747/*
748 * cs4270_id - I2C device IDs supported by this driver
749 */
750static struct i2c_device_id cs4270_id[] = {
751 {"cs4270", 0},
752 {}
753};
754MODULE_DEVICE_TABLE(i2c, cs4270_id);
755
756/*
757 * cs4270_i2c_driver - I2C device identification
758 *
759 * This structure tells the I2C subsystem how to identify and support a
760 * given I2C device type.
761 */
762static struct i2c_driver cs4270_i2c_driver = {
763 .driver = {
764 .name = "cs4270",
765 .owner = THIS_MODULE,
766 },
767 .id_table = cs4270_id,
768 .probe = cs4270_i2c_probe,
769 .remove = cs4270_i2c_remove,
770};
771
772/*
766 * ASoC codec device structure 773 * ASoC codec device structure
767 * 774 *
768 * Assign this variable to the codec_dev field of the machine driver's 775 * Assign this variable to the codec_dev field of the machine driver's
@@ -776,13 +783,15 @@ EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
776 783
777static int __init cs4270_init(void) 784static int __init cs4270_init(void)
778{ 785{
779 return snd_soc_register_dai(&cs4270_dai); 786 pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
787
788 return i2c_add_driver(&cs4270_i2c_driver);
780} 789}
781module_init(cs4270_init); 790module_init(cs4270_init);
782 791
783static void __exit cs4270_exit(void) 792static void __exit cs4270_exit(void)
784{ 793{
785 snd_soc_unregister_dai(&cs4270_dai); 794 i2c_del_driver(&cs4270_i2c_driver);
786} 795}
787module_exit(cs4270_exit); 796module_exit(cs4270_exit);
788 797