Merge branch 'for-2.6.29' into for-2.6.30
[deliverable/linux.git] / sound / soc / codecs / cs4270.c
CommitLineData
b0c813ce
TT
1/*
2 * CS4270 ALSA SoC (ASoC) codec driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
ff7bf02f
TT
6 * Copyright 2007-2009 Freescale Semiconductor, Inc. This file is licensed
7 * under the terms of the GNU General Public License version 2. This
8 * program is licensed "as is" without any warranty of any kind, whether
9 * express or implied.
b0c813ce
TT
10 *
11 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
12 *
13 * Current features/limitations:
14 *
ff637d38 15 * 1) Software mode is supported. Stand-alone mode is not supported.
b0c813ce
TT
16 * 2) Only I2C is supported, not SPI
17 * 3) Only Master mode is supported, not Slave.
18 * 4) The machine driver's 'startup' function must call
19 * cs4270_set_dai_sysclk() with the value of MCLK.
20 * 5) Only I2S and left-justified modes are supported
21 * 6) Power management is not supported
22 * 7) The only supported control is volume and hardware mute (if enabled)
23 */
24
25#include <linux/module.h>
26#include <linux/platform_device.h>
b0c813ce
TT
27#include <sound/core.h>
28#include <sound/soc.h>
29#include <sound/initval.h>
30#include <linux/i2c.h>
31
01e097d6
MB
32#include "cs4270.h"
33
8432395f
TT
34/*
35 * The codec isn't really big-endian or little-endian, since the I2S
36 * interface requires data to be sent serially with the MSbit first.
37 * However, to support BE and LE I2S devices, we specify both here. That
38 * way, ALSA will always match the bit patterns.
39 */
40#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
41 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
42 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
43 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
44 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
45 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
46
8432395f
TT
47/* CS4270 registers addresses */
48#define CS4270_CHIPID 0x01 /* Chip ID */
49#define CS4270_PWRCTL 0x02 /* Power Control */
50#define CS4270_MODE 0x03 /* Mode Control */
51#define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
52#define CS4270_TRANS 0x05 /* Transition Control */
53#define CS4270_MUTE 0x06 /* Mute Control */
54#define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
55#define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
56
57#define CS4270_FIRSTREG 0x01
58#define CS4270_LASTREG 0x08
59#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
9dbd627b 60
8432395f
TT
61/* Bit masks for the CS4270 registers */
62#define CS4270_CHIPID_ID 0xF0
63#define CS4270_CHIPID_REV 0x0F
64#define CS4270_PWRCTL_FREEZE 0x80
65#define CS4270_PWRCTL_PDN_ADC 0x20
66#define CS4270_PWRCTL_PDN_DAC 0x02
67#define CS4270_PWRCTL_PDN 0x01
68#define CS4270_MODE_SPEED_MASK 0x30
69#define CS4270_MODE_1X 0x00
70#define CS4270_MODE_2X 0x10
71#define CS4270_MODE_4X 0x20
72#define CS4270_MODE_SLAVE 0x30
73#define CS4270_MODE_DIV_MASK 0x0E
74#define CS4270_MODE_DIV1 0x00
75#define CS4270_MODE_DIV15 0x02
76#define CS4270_MODE_DIV2 0x04
77#define CS4270_MODE_DIV3 0x06
78#define CS4270_MODE_DIV4 0x08
79#define CS4270_MODE_POPGUARD 0x01
80#define CS4270_FORMAT_FREEZE_A 0x80
81#define CS4270_FORMAT_FREEZE_B 0x40
82#define CS4270_FORMAT_LOOPBACK 0x20
83#define CS4270_FORMAT_DAC_MASK 0x18
84#define CS4270_FORMAT_DAC_LJ 0x00
85#define CS4270_FORMAT_DAC_I2S 0x08
86#define CS4270_FORMAT_DAC_RJ16 0x18
87#define CS4270_FORMAT_DAC_RJ24 0x10
88#define CS4270_FORMAT_ADC_MASK 0x01
89#define CS4270_FORMAT_ADC_LJ 0x00
90#define CS4270_FORMAT_ADC_I2S 0x01
91#define CS4270_TRANS_ONE_VOL 0x80
92#define CS4270_TRANS_SOFT 0x40
93#define CS4270_TRANS_ZERO 0x20
94#define CS4270_TRANS_INV_ADC_A 0x08
95#define CS4270_TRANS_INV_ADC_B 0x10
96#define CS4270_TRANS_INV_DAC_A 0x02
97#define CS4270_TRANS_INV_DAC_B 0x04
98#define CS4270_TRANS_DEEMPH 0x01
99#define CS4270_MUTE_AUTO 0x20
100#define CS4270_MUTE_ADC_A 0x08
101#define CS4270_MUTE_ADC_B 0x10
102#define CS4270_MUTE_POLARITY 0x04
103#define CS4270_MUTE_DAC_A 0x01
104#define CS4270_MUTE_DAC_B 0x02
105
0db4d070
TT
106/* Private data for the CS4270 */
107struct cs4270_private {
108 struct snd_soc_codec codec;
109 u8 reg_cache[CS4270_NUMREGS];
110 unsigned int mclk; /* Input frequency of the MCLK pin */
111 unsigned int mode; /* The mode (I2S or left-justified) */
112};
113
ff7bf02f
TT
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
8432395f
TT
121 *
122 * The data for this chart is taken from Table 5 of the CS4270 reference
123 * manual.
124 *
125 * This table is used to determine how to program the Mode Control register.
126 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
127 * rates the CS4270 currently supports.
128 *
ff7bf02f 129 * @speed_mode is the corresponding bit pattern to be written to the
8432395f
TT
130 * MODE bits of the Mode Control Register
131 *
ff7bf02f 132 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
8432395f
TT
133 * the Mode Control Register.
134 *
135 * In situations where a single ratio is represented by multiple speed
136 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
137 * double-speed instead of quad-speed. However, the CS4270 errata states
ff7bf02f 138 * that divide-By-1.5 can cause failures, so we avoid that mode where
8432395f
TT
139 * possible.
140 *
ff7bf02f
TT
141 * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
142 * work if Vd is 3.3V. If this effects you, select the
8432395f
TT
143 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
144 * never select any sample rates that require divide-by-1.5.
145 */
ff7bf02f 146struct cs4270_mode_ratios {
8432395f
TT
147 unsigned int ratio;
148 u8 speed_mode;
149 u8 mclk;
ff7bf02f
TT
150};
151
d9fb7fbd 152static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
8432395f
TT
153 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
154#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
155 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
156#endif
157 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
158 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
159 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
160 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
161 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
162 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
163 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
164};
165
166/* The number of MCLK/LRCK ratios supported by the CS4270 */
167#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
9dbd627b 168
ff7bf02f
TT
169/**
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)
9dbd627b 175 *
ff7bf02f
TT
176 * This function is used to tell the codec driver what the input MCLK
177 * frequency is.
9dbd627b
TT
178 *
179 * The value of MCLK is used to determine which sample rates are supported
180 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
ff7bf02f 181 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
9dbd627b
TT
182 *
183 * This function calculates the nine ratios and determines which ones match
184 * a standard sample rate. If there's a match, then it is added to the list
ff7bf02f 185 * of supported sample rates.
9dbd627b
TT
186 *
187 * This function must be called by the machine driver's 'startup' function,
188 * otherwise the list of supported sample rates will not be available in
189 * time for ALSA.
9dbd627b 190 */
e550e17f 191static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
9dbd627b
TT
192 int clk_id, unsigned int freq, int dir)
193{
194 struct snd_soc_codec *codec = codec_dai->codec;
195 struct cs4270_private *cs4270 = codec->private_data;
196 unsigned int rates = 0;
197 unsigned int rate_min = -1;
198 unsigned int rate_max = 0;
199 unsigned int i;
200
201 cs4270->mclk = freq;
202
203 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
8432395f 204 unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
918f3a0e
CL
205 rates |= snd_pcm_rate_to_rate_bit(rate);
206 if (rate < rate_min)
207 rate_min = rate;
208 if (rate > rate_max)
209 rate_max = rate;
9dbd627b 210 }
918f3a0e
CL
211 /* FIXME: soc should support a rate list */
212 rates &= ~SNDRV_PCM_RATE_KNOT;
9dbd627b
TT
213
214 if (!rates) {
a6c255e0 215 dev_err(codec->dev, "could not find a valid sample rate\n");
9dbd627b
TT
216 return -EINVAL;
217 }
218
219 codec_dai->playback.rates = rates;
220 codec_dai->playback.rate_min = rate_min;
221 codec_dai->playback.rate_max = rate_max;
222
223 codec_dai->capture.rates = rates;
224 codec_dai->capture.rate_min = rate_min;
225 codec_dai->capture.rate_max = rate_max;
226
227 return 0;
228}
229
ff7bf02f
TT
230/**
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
9dbd627b
TT
234 *
235 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
236 * codec accordingly.
237 *
238 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
239 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
240 * data for playback only, but ASoC currently does not support different
241 * formats for playback vs. record.
242 */
e550e17f 243static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
9dbd627b
TT
244 unsigned int format)
245{
246 struct snd_soc_codec *codec = codec_dai->codec;
247 struct cs4270_private *cs4270 = codec->private_data;
248 int ret = 0;
249
250 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
251 case SND_SOC_DAIFMT_I2S:
252 case SND_SOC_DAIFMT_LEFT_J:
253 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
254 break;
255 default:
a6c255e0 256 dev_err(codec->dev, "invalid dai format\n");
9dbd627b
TT
257 ret = -EINVAL;
258 }
259
260 return ret;
261}
262
ff7bf02f
TT
263/**
264 * cs4270_fill_cache - pre-fill the CS4270 register cache.
265 * @codec: the codec for this CS4270
266 *
267 * This function fills in the CS4270 register cache by reading the register
268 * values from the hardware.
269 *
270 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
271 * After the initial read to pre-fill the cache, the CS4270 never updates
272 * the register values, so we won't have a cache coherency problem.
b0c813ce
TT
273 *
274 * We use the auto-increment feature of the CS4270 to read all registers in
275 * one shot.
276 */
277static int cs4270_fill_cache(struct snd_soc_codec *codec)
278{
279 u8 *cache = codec->reg_cache;
280 struct i2c_client *i2c_client = codec->control_data;
281 s32 length;
282
283 length = i2c_smbus_read_i2c_block_data(i2c_client,
284 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
285
286 if (length != CS4270_NUMREGS) {
a6c255e0 287 dev_err(codec->dev, "i2c read failure, addr=0x%x\n",
b0c813ce
TT
288 i2c_client->addr);
289 return -EIO;
290 }
291
292 return 0;
293}
294
ff7bf02f
TT
295/**
296 * cs4270_read_reg_cache - read from the CS4270 register cache.
297 * @codec: the codec for this CS4270
298 * @reg: the register to read
299 *
300 * This function returns the value for a given register. It reads only from
301 * the register cache, not the hardware itself.
b0c813ce
TT
302 *
303 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
304 * After the initial read to pre-fill the cache, the CS4270 never updates
ff7bf02f 305 * the register values, so we won't have a cache coherency problem.
b0c813ce
TT
306 */
307static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
308 unsigned int reg)
309{
310 u8 *cache = codec->reg_cache;
311
312 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
313 return -EIO;
314
315 return cache[reg - CS4270_FIRSTREG];
316}
317
ff7bf02f
TT
318/**
319 * cs4270_i2c_write - write to a CS4270 register via the I2C bus.
320 * @codec: the codec for this CS4270
321 * @reg: the register to write
322 * @value: the value to write to the register
b0c813ce
TT
323 *
324 * This function writes the given value to the given CS4270 register, and
325 * also updates the register cache.
326 *
327 * Note that we don't use the hw_write function pointer of snd_soc_codec.
328 * That's because it's too clunky: the hw_write_t prototype does not match
329 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
330 */
331static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
332 unsigned int value)
333{
bfc4e861
TT
334 u8 *cache = codec->reg_cache;
335
b0c813ce
TT
336 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
337 return -EIO;
338
bfc4e861
TT
339 /* Only perform an I2C operation if the new value is different */
340 if (cache[reg - CS4270_FIRSTREG] != value) {
341 struct i2c_client *client = codec->control_data;
342 if (i2c_smbus_write_byte_data(client, reg, value)) {
a6c255e0 343 dev_err(codec->dev, "i2c write failed\n");
bfc4e861
TT
344 return -EIO;
345 }
346
b0c813ce 347 /* We've written to the hardware, so update the cache */
b0c813ce 348 cache[reg - CS4270_FIRSTREG] = value;
b0c813ce 349 }
bfc4e861
TT
350
351 return 0;
b0c813ce
TT
352}
353
ff7bf02f
TT
354/**
355 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
356 * @substream: the audio stream
357 * @params: the hardware parameters to set
358 * @dai: the SOC DAI (ignored)
b0c813ce 359 *
ff7bf02f
TT
360 * This function programs the hardware with the values provided.
361 * Specifically, the sample rate and the data format.
362 *
363 * The .ops functions are used to provide board-specific data, like input
364 * frequencies, to this driver. This function takes that information,
b0c813ce
TT
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,
dee89c4d
MB
369 struct snd_pcm_hw_params *params,
370 struct snd_soc_dai *dai)
b0c813ce
TT
371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct snd_soc_device *socdev = rtd->socdev;
6627a653 374 struct snd_soc_codec *codec = socdev->card->codec;
b0c813ce 375 struct cs4270_private *cs4270 = codec->private_data;
e34ba212 376 int ret;
b0c813ce
TT
377 unsigned int i;
378 unsigned int rate;
379 unsigned int ratio;
380 int reg;
381
382 /* Figure out which MCLK/LRCK ratio to use */
383
384 rate = params_rate(params); /* Sampling rate, in Hz */
385 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
386
9dbd627b 387 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
8432395f 388 if (cs4270_mode_ratios[i].ratio == ratio)
b0c813ce
TT
389 break;
390 }
391
9dbd627b 392 if (i == NUM_MCLK_RATIOS) {
b0c813ce 393 /* We did not find a matching ratio */
a6c255e0 394 dev_err(codec->dev, "could not find matching ratio\n");
b0c813ce
TT
395 return -EINVAL;
396 }
397
398 /* Freeze and power-down the codec */
399
400 ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
401 CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
402 CS4270_PWRCTL_PDN);
403 if (ret < 0) {
a6c255e0 404 dev_err(codec->dev, "i2c write failed\n");
b0c813ce
TT
405 return ret;
406 }
407
408 /* Program the mode control register */
409
410 reg = snd_soc_read(codec, CS4270_MODE);
411 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
412 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
413
414 ret = snd_soc_write(codec, CS4270_MODE, reg);
415 if (ret < 0) {
a6c255e0 416 dev_err(codec->dev, "i2c write failed\n");
b0c813ce
TT
417 return ret;
418 }
419
420 /* Program the format register */
421
422 reg = snd_soc_read(codec, CS4270_FORMAT);
423 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
424
425 switch (cs4270->mode) {
426 case SND_SOC_DAIFMT_I2S:
427 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
428 break;
429 case SND_SOC_DAIFMT_LEFT_J:
430 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
431 break;
432 default:
a6c255e0 433 dev_err(codec->dev, "unknown dai format\n");
b0c813ce
TT
434 return -EINVAL;
435 }
436
437 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
438 if (ret < 0) {
a6c255e0 439 dev_err(codec->dev, "i2c write failed\n");
b0c813ce
TT
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) {
a6c255e0 450 dev_err(codec->dev, "i2c write failed\n");
b0c813ce
TT
451 return ret;
452 }
453
0c235d1e
TT
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) {
a6c255e0 463 dev_err(codec->dev, "i2c write failed\n");
0c235d1e
TT
464 return ret;
465 }
466
b0c813ce
TT
467 /* Thaw and power-up the codec */
468
469 ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
470 if (ret < 0) {
a6c255e0 471 dev_err(codec->dev, "i2c write failed\n");
b0c813ce
TT
472 return ret;
473 }
474
475 return ret;
476}
477
478#ifdef CONFIG_SND_SOC_CS4270_HWMUTE
ff7bf02f
TT
479/**
480 * cs4270_mute - enable/disable the CS4270 external mute
481 * @dai: the SOC DAI
482 * @mute: 0 = disable mute, 1 = enable mute
b0c813ce
TT
483 *
484 * This function toggles the mute bits in the MUTE register. The CS4270's
485 * mute capability is intended for external muting circuitry, so if the
486 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
487 * then this function will do nothing.
488 */
e550e17f 489static int cs4270_mute(struct snd_soc_dai *dai, int mute)
b0c813ce
TT
490{
491 struct snd_soc_codec *codec = dai->codec;
492 int reg6;
493
494 reg6 = snd_soc_read(codec, CS4270_MUTE);
495
496 if (mute)
497 reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
498 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
499 else
500 reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
501 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
502
503 return snd_soc_write(codec, CS4270_MUTE, reg6);
504}
ff637d38
TT
505#else
506#define cs4270_mute NULL
b0c813ce
TT
507#endif
508
b0c813ce
TT
509/* A list of non-DAPM controls that the CS4270 supports */
510static const struct snd_kcontrol_new cs4270_snd_controls[] = {
511 SOC_DOUBLE_R("Master Playback Volume",
bfc4e861 512 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
b0c813ce
TT
513};
514
b0c813ce 515/*
ff7bf02f 516 * cs4270_codec - global variable to store codec for the ASoC probe function
b0c813ce
TT
517 *
518 * If struct i2c_driver had a private_data field, we wouldn't need to use
04eb093c
TT
519 * cs4270_codec. This is the only way to pass the codec structure from
520 * cs4270_i2c_probe() to cs4270_probe(). Unfortunately, there is no good
521 * way to synchronize these two functions. cs4270_i2c_probe() can be called
522 * multiple times before cs4270_probe() is called even once. So for now, we
523 * also only allow cs4270_i2c_probe() to be run once. That means that we do
524 * not support more than one cs4270 device in the system, at least for now.
b0c813ce 525 */
04eb093c 526static struct snd_soc_codec *cs4270_codec;
b0c813ce 527
0db4d070
TT
528struct snd_soc_dai cs4270_dai = {
529 .name = "cs4270",
530 .playback = {
531 .stream_name = "Playback",
532 .channels_min = 1,
533 .channels_max = 2,
534 .rates = 0,
535 .formats = CS4270_FORMATS,
536 },
537 .capture = {
538 .stream_name = "Capture",
539 .channels_min = 1,
540 .channels_max = 2,
541 .rates = 0,
542 .formats = CS4270_FORMATS,
543 },
544 .ops = {
545 .hw_params = cs4270_hw_params,
546 .set_sysclk = cs4270_set_dai_sysclk,
547 .set_fmt = cs4270_set_dai_fmt,
548 .digital_mute = cs4270_mute,
549 },
550};
551EXPORT_SYMBOL_GPL(cs4270_dai);
552
ff7bf02f
TT
553/**
554 * cs4270_probe - ASoC probe function
555 * @pdev: platform device
556 *
557 * This function is called when ASoC has all the pieces it needs to
558 * instantiate a sound driver.
04eb093c
TT
559 */
560static int cs4270_probe(struct platform_device *pdev)
561{
562 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
563 struct snd_soc_codec *codec = cs4270_codec;
564 unsigned int i;
565 int ret;
566
567 /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
568 socdev->card->codec = codec;
569
570 /* Register PCMs */
571 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
572 if (ret < 0) {
a6c255e0 573 dev_err(codec->dev, "failed to create pcms\n");
04eb093c
TT
574 return ret;
575 }
576
577 /* Add the non-DAPM controls */
578 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
579 struct snd_kcontrol *kctrl;
580
581 kctrl = snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
582 if (!kctrl) {
a6c255e0 583 dev_err(codec->dev, "error creating control '%s'\n",
04eb093c
TT
584 cs4270_snd_controls[i].name);
585 ret = -ENOMEM;
586 goto error_free_pcms;
587 }
588
589 ret = snd_ctl_add(codec->card, kctrl);
590 if (ret < 0) {
a6c255e0 591 dev_err(codec->dev, "error adding control '%s'\n",
04eb093c
TT
592 cs4270_snd_controls[i].name);
593 goto error_free_pcms;
594 }
595 }
596
597 /* And finally, register the socdev */
598 ret = snd_soc_init_card(socdev);
599 if (ret < 0) {
a6c255e0 600 dev_err(codec->dev, "failed to register card\n");
04eb093c
TT
601 goto error_free_pcms;
602 }
603
604 return 0;
605
606error_free_pcms:
607 snd_soc_free_pcms(socdev);
608
609 return ret;
610}
611
ff7bf02f
TT
612/**
613 * cs4270_remove - ASoC remove function
614 * @pdev: platform device
615 *
616 * This function is the counterpart to cs4270_probe().
617 */
04eb093c
TT
618static int cs4270_remove(struct platform_device *pdev)
619{
620 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
621
622 snd_soc_free_pcms(socdev);
623
624 return 0;
625};
626
ff7bf02f
TT
627/**
628 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
629 * @i2c_client: the I2C client object
630 * @id: the I2C device ID (ignored)
b0c813ce 631 *
ff7bf02f
TT
632 * This function is called whenever the I2C subsystem finds a device that
633 * matches the device ID given via a prior call to i2c_add_driver().
b0c813ce 634 */
ec2cd95f
TT
635static int cs4270_i2c_probe(struct i2c_client *i2c_client,
636 const struct i2c_device_id *id)
b0c813ce 637{
0db4d070
TT
638 struct snd_soc_codec *codec;
639 struct cs4270_private *cs4270;
04eb093c
TT
640 int ret;
641
642 /* For now, we only support one cs4270 device in the system. See the
643 * comment for cs4270_codec.
644 */
645 if (cs4270_codec) {
a6c255e0 646 dev_err(&i2c_client->dev, "ignoring CS4270 at addr %X\n",
04eb093c 647 i2c_client->addr);
a6c255e0 648 dev_err(&i2c_client->dev, "only one per board allowed\n");
04eb093c
TT
649 /* Should we return something other than ENODEV here? */
650 return -ENODEV;
651 }
b0c813ce 652
b0c813ce
TT
653 /* Verify that we have a CS4270 */
654
655 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
656 if (ret < 0) {
a6c255e0 657 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
04eb093c 658 i2c_client->addr);
0db4d070 659 return ret;
b0c813ce
TT
660 }
661 /* The top four bits of the chip ID should be 1100. */
662 if ((ret & 0xF0) != 0xC0) {
a6c255e0 663 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
0db4d070
TT
664 i2c_client->addr);
665 return -ENODEV;
b0c813ce
TT
666 }
667
a6c255e0 668 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
ec2cd95f 669 i2c_client->addr);
a6c255e0 670 dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);
b0c813ce 671
0db4d070
TT
672 /* Allocate enough space for the snd_soc_codec structure
673 and our private data together. */
674 cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
675 if (!cs4270) {
a6c255e0 676 dev_err(&i2c_client->dev, "could not allocate codec\n");
0db4d070
TT
677 return -ENOMEM;
678 }
679 codec = &cs4270->codec;
0db4d070
TT
680
681 mutex_init(&codec->mutex);
682 INIT_LIST_HEAD(&codec->dapm_widgets);
683 INIT_LIST_HEAD(&codec->dapm_paths);
684
a6c255e0 685 codec->dev = &i2c_client->dev;
0db4d070
TT
686 codec->name = "CS4270";
687 codec->owner = THIS_MODULE;
688 codec->dai = &cs4270_dai;
689 codec->num_dai = 1;
690 codec->private_data = cs4270;
b0c813ce
TT
691 codec->control_data = i2c_client;
692 codec->read = cs4270_read_reg_cache;
693 codec->write = cs4270_i2c_write;
0db4d070 694 codec->reg_cache = cs4270->reg_cache;
b0c813ce
TT
695 codec->reg_cache_size = CS4270_NUMREGS;
696
697 /* The I2C interface is set up, so pre-fill our register cache */
698
699 ret = cs4270_fill_cache(codec);
700 if (ret < 0) {
a6c255e0 701 dev_err(&i2c_client->dev, "failed to fill register cache\n");
0db4d070
TT
702 goto error_free_codec;
703 }
704
a6c255e0
TT
705 /* Initialize the DAI. Normally, we'd prefer to have a kmalloc'd DAI
706 * structure for each CS4270 device, but the machine driver needs to
707 * have a pointer to the DAI structure, so for now it must be a global
708 * variable.
709 */
710 cs4270_dai.dev = &i2c_client->dev;
711
04eb093c
TT
712 /* Register the DAI. If all the other ASoC driver have already
713 * registered, then this will call our probe function, so
714 * cs4270_codec needs to be ready.
715 */
a6c255e0 716 cs4270_codec = codec;
04eb093c 717 ret = snd_soc_register_dai(&cs4270_dai);
0db4d070 718 if (ret < 0) {
a6c255e0 719 dev_err(&i2c_client->dev, "failed to register DAIe\n");
0db4d070 720 goto error_free_codec;
b0c813ce
TT
721 }
722
04eb093c 723 i2c_set_clientdata(i2c_client, cs4270);
ec2cd95f 724
b0c813ce
TT
725 return 0;
726
0db4d070
TT
727error_free_codec:
728 kfree(cs4270);
a6c255e0
TT
729 cs4270_codec = NULL;
730 cs4270_dai.dev = NULL;
b0c813ce 731
b0c813ce
TT
732 return ret;
733}
734
ff7bf02f
TT
735/**
736 * cs4270_i2c_remove - remove an I2C device
737 * @i2c_client: the I2C client object
738 *
739 * This function is the counterpart to cs4270_i2c_probe().
740 */
0db4d070
TT
741static int cs4270_i2c_remove(struct i2c_client *i2c_client)
742{
04eb093c 743 struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
0db4d070 744
0db4d070 745 kfree(cs4270);
a6c255e0
TT
746 cs4270_codec = NULL;
747 cs4270_dai.dev = NULL;
0db4d070
TT
748
749 return 0;
750}
751
ff7bf02f
TT
752/*
753 * cs4270_id - I2C device IDs supported by this driver
754 */
0db4d070 755static struct i2c_device_id cs4270_id[] = {
ff637d38
TT
756 {"cs4270", 0},
757 {}
758};
759MODULE_DEVICE_TABLE(i2c, cs4270_id);
760
ff7bf02f
TT
761/*
762 * cs4270_i2c_driver - I2C device identification
763 *
764 * This structure tells the I2C subsystem how to identify and support a
765 * given I2C device type.
766 */
ff637d38
TT
767static struct i2c_driver cs4270_i2c_driver = {
768 .driver = {
769 .name = "cs4270",
770 .owner = THIS_MODULE,
771 },
772 .id_table = cs4270_id,
773 .probe = cs4270_i2c_probe,
0db4d070 774 .remove = cs4270_i2c_remove,
ff637d38 775};
b0c813ce 776
b0c813ce
TT
777/*
778 * ASoC codec device structure
779 *
780 * Assign this variable to the codec_dev field of the machine driver's
781 * snd_soc_device structure.
782 */
783struct snd_soc_codec_device soc_codec_device_cs4270 = {
784 .probe = cs4270_probe,
785 .remove = cs4270_remove
786};
787EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
788
c9b3a40f 789static int __init cs4270_init(void)
64089b84 790{
a6c255e0 791 pr_info("Cirrus Logic CS4270 ALSA SoC Codec Driver\n");
0db4d070 792
04eb093c 793 return i2c_add_driver(&cs4270_i2c_driver);
64089b84
MB
794}
795module_init(cs4270_init);
796
797static void __exit cs4270_exit(void)
798{
04eb093c 799 i2c_del_driver(&cs4270_i2c_driver);
64089b84
MB
800}
801module_exit(cs4270_exit);
802
b0c813ce
TT
803MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
804MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
805MODULE_LICENSE("GPL");
This page took 0.183616 seconds and 5 git commands to generate.