blob: 2e4ce04925e243e73d4e5ccb16eb6b43e6150375 [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 Tabiff637d32009-01-22 18:23:39 -060015 * 1) Software mode is supported. Stand-alone mode is not supported.
Timur Tabib0c813c2007-07-31 18:18:44 +020016 * 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>
Timur Tabib0c813c2007-07-31 18:18:44 +020027#include <sound/core.h>
28#include <sound/soc.h>
29#include <sound/initval.h>
30#include <linux/i2c.h>
31
Timur Tabib0c813c2007-07-31 18:18:44 +020032/* Private data for the CS4270 */
33struct cs4270_private {
34 unsigned int mclk; /* Input frequency of the MCLK pin */
35 unsigned int mode; /* The mode (I2S or left-justified) */
36};
37
38/*
39 * The codec isn't really big-endian or little-endian, since the I2S
40 * interface requires data to be sent serially with the MSbit first.
41 * However, to support BE and LE I2S devices, we specify both here. That
42 * way, ALSA will always match the bit patterns.
43 */
44#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
45 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
46 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
47 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
48 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
49 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
50
Timur Tabib0c813c2007-07-31 18:18:44 +020051/* CS4270 registers addresses */
52#define CS4270_CHIPID 0x01 /* Chip ID */
53#define CS4270_PWRCTL 0x02 /* Power Control */
54#define CS4270_MODE 0x03 /* Mode Control */
55#define CS4270_FORMAT 0x04 /* Serial Format, ADC/DAC Control */
56#define CS4270_TRANS 0x05 /* Transition Control */
57#define CS4270_MUTE 0x06 /* Mute Control */
58#define CS4270_VOLA 0x07 /* DAC Channel A Volume Control */
59#define CS4270_VOLB 0x08 /* DAC Channel B Volume Control */
60
61#define CS4270_FIRSTREG 0x01
62#define CS4270_LASTREG 0x08
63#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
64
65/* Bit masks for the CS4270 registers */
66#define CS4270_CHIPID_ID 0xF0
67#define CS4270_CHIPID_REV 0x0F
68#define CS4270_PWRCTL_FREEZE 0x80
69#define CS4270_PWRCTL_PDN_ADC 0x20
70#define CS4270_PWRCTL_PDN_DAC 0x02
71#define CS4270_PWRCTL_PDN 0x01
72#define CS4270_MODE_SPEED_MASK 0x30
73#define CS4270_MODE_1X 0x00
74#define CS4270_MODE_2X 0x10
75#define CS4270_MODE_4X 0x20
76#define CS4270_MODE_SLAVE 0x30
77#define CS4270_MODE_DIV_MASK 0x0E
78#define CS4270_MODE_DIV1 0x00
79#define CS4270_MODE_DIV15 0x02
80#define CS4270_MODE_DIV2 0x04
81#define CS4270_MODE_DIV3 0x06
82#define CS4270_MODE_DIV4 0x08
83#define CS4270_MODE_POPGUARD 0x01
84#define CS4270_FORMAT_FREEZE_A 0x80
85#define CS4270_FORMAT_FREEZE_B 0x40
86#define CS4270_FORMAT_LOOPBACK 0x20
87#define CS4270_FORMAT_DAC_MASK 0x18
88#define CS4270_FORMAT_DAC_LJ 0x00
89#define CS4270_FORMAT_DAC_I2S 0x08
90#define CS4270_FORMAT_DAC_RJ16 0x18
91#define CS4270_FORMAT_DAC_RJ24 0x10
92#define CS4270_FORMAT_ADC_MASK 0x01
93#define CS4270_FORMAT_ADC_LJ 0x00
94#define CS4270_FORMAT_ADC_I2S 0x01
95#define CS4270_TRANS_ONE_VOL 0x80
96#define CS4270_TRANS_SOFT 0x40
97#define CS4270_TRANS_ZERO 0x20
98#define CS4270_TRANS_INV_ADC_A 0x08
99#define CS4270_TRANS_INV_ADC_B 0x10
100#define CS4270_TRANS_INV_DAC_A 0x02
101#define CS4270_TRANS_INV_DAC_B 0x04
102#define CS4270_TRANS_DEEMPH 0x01
103#define CS4270_MUTE_AUTO 0x20
104#define CS4270_MUTE_ADC_A 0x08
105#define CS4270_MUTE_ADC_B 0x10
106#define CS4270_MUTE_POLARITY 0x04
107#define CS4270_MUTE_DAC_A 0x01
108#define CS4270_MUTE_DAC_B 0x02
109
110/*
Timur Tabi84323952007-12-18 15:42:53 +0100111 * Clock Ratio Selection for Master Mode with I2C enabled
112 *
113 * The data for this chart is taken from Table 5 of the CS4270 reference
114 * manual.
115 *
116 * This table is used to determine how to program the Mode Control register.
117 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
118 * rates the CS4270 currently supports.
119 *
120 * Each element in this array corresponds to the ratios in mclk_ratios[].
121 * These two arrays need to be in sync.
122 *
123 * 'speed_mode' is the corresponding bit pattern to be written to the
124 * MODE bits of the Mode Control Register
125 *
126 * 'mclk' is the corresponding bit pattern to be wirten to the MCLK bits of
127 * the Mode Control Register.
128 *
129 * In situations where a single ratio is represented by multiple speed
130 * modes, we favor the slowest speed. E.g, for a ratio of 128, we pick
131 * double-speed instead of quad-speed. However, the CS4270 errata states
132 * that Divide-By-1.5 can cause failures, so we avoid that mode where
133 * possible.
134 *
135 * ERRATA: There is an errata for the CS4270 where divide-by-1.5 does not
136 * work if VD = 3.3V. If this effects you, select the
137 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
138 * never select any sample rates that require divide-by-1.5.
139 */
140static struct {
141 unsigned int ratio;
142 u8 speed_mode;
143 u8 mclk;
144} cs4270_mode_ratios[] = {
145 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
146#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
147 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
148#endif
149 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
150 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
151 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
152 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
153 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
154 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
155 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
156};
157
158/* The number of MCLK/LRCK ratios supported by the CS4270 */
159#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
160
161/*
162 * Determine the CS4270 samples rates.
163 *
164 * 'freq' is the input frequency to MCLK. The other parameters are ignored.
165 *
166 * The value of MCLK is used to determine which sample rates are supported
167 * by the CS4270. The ratio of MCLK / Fs must be equal to one of nine
168 * support values: 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
169 *
170 * This function calculates the nine ratios and determines which ones match
171 * a standard sample rate. If there's a match, then it is added to the list
172 * of support sample rates.
173 *
174 * This function must be called by the machine driver's 'startup' function,
175 * otherwise the list of supported sample rates will not be available in
176 * time for ALSA.
177 *
178 * Note that in stand-alone mode, the sample rate is determined by input
179 * pins M0, M1, MDIV1, and MDIV2. Also in stand-alone mode, divide-by-3
180 * is not a programmable option. However, divide-by-3 is not an available
181 * option in stand-alone mode. This cases two problems: a ratio of 768 is
182 * not available (it requires divide-by-3) and B) ratios 192 and 384 can
183 * only be selected with divide-by-1.5, but there is an errate that make
184 * this selection difficult.
185 *
186 * In addition, there is no mechanism for communicating with the machine
187 * driver what the input settings can be. This would need to be implemented
188 * for stand-alone mode to work.
189 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100190static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100191 int clk_id, unsigned int freq, int dir)
192{
193 struct snd_soc_codec *codec = codec_dai->codec;
194 struct cs4270_private *cs4270 = codec->private_data;
195 unsigned int rates = 0;
196 unsigned int rate_min = -1;
197 unsigned int rate_max = 0;
198 unsigned int i;
199
200 cs4270->mclk = freq;
201
202 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
203 unsigned int rate = freq / cs4270_mode_ratios[i].ratio;
204 rates |= snd_pcm_rate_to_rate_bit(rate);
205 if (rate < rate_min)
206 rate_min = rate;
207 if (rate > rate_max)
208 rate_max = rate;
209 }
210 /* FIXME: soc should support a rate list */
211 rates &= ~SNDRV_PCM_RATE_KNOT;
212
213 if (!rates) {
214 printk(KERN_ERR "cs4270: could not find a valid sample rate\n");
215 return -EINVAL;
216 }
217
218 codec_dai->playback.rates = rates;
219 codec_dai->playback.rate_min = rate_min;
220 codec_dai->playback.rate_max = rate_max;
221
222 codec_dai->capture.rates = rates;
223 codec_dai->capture.rate_min = rate_min;
224 codec_dai->capture.rate_max = rate_max;
225
226 return 0;
227}
228
229/*
230 * Configure the codec for the selected audio format
231 *
232 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
233 * codec accordingly.
234 *
235 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
236 * SND_SOC_DAIFMT_LEFT_J. The CS4270 codec also supports right-justified
237 * data for playback only, but ASoC currently does not support different
238 * formats for playback vs. record.
239 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100240static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
Timur Tabi84323952007-12-18 15:42:53 +0100241 unsigned int format)
242{
243 struct snd_soc_codec *codec = codec_dai->codec;
244 struct cs4270_private *cs4270 = codec->private_data;
245 int ret = 0;
246
247 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
248 case SND_SOC_DAIFMT_I2S:
249 case SND_SOC_DAIFMT_LEFT_J:
250 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
251 break;
252 default:
253 printk(KERN_ERR "cs4270: invalid DAI format\n");
254 ret = -EINVAL;
255 }
256
257 return ret;
258}
259
260/*
Timur Tabib0c813c2007-07-31 18:18:44 +0200261 * Pre-fill the CS4270 register cache.
262 *
263 * We use the auto-increment feature of the CS4270 to read all registers in
264 * one shot.
265 */
266static int cs4270_fill_cache(struct snd_soc_codec *codec)
267{
268 u8 *cache = codec->reg_cache;
269 struct i2c_client *i2c_client = codec->control_data;
270 s32 length;
271
272 length = i2c_smbus_read_i2c_block_data(i2c_client,
273 CS4270_FIRSTREG | 0x80, CS4270_NUMREGS, cache);
274
275 if (length != CS4270_NUMREGS) {
Timur Tabi9dbd6272007-08-01 12:22:07 +0200276 printk(KERN_ERR "cs4270: I2C read failure, addr=0x%x\n",
Timur Tabib0c813c2007-07-31 18:18:44 +0200277 i2c_client->addr);
278 return -EIO;
279 }
280
281 return 0;
282}
283
284/*
285 * Read from the CS4270 register cache.
286 *
287 * This CS4270 registers are cached to avoid excessive I2C I/O operations.
288 * After the initial read to pre-fill the cache, the CS4270 never updates
289 * the register values, so we won't have a cache coherncy problem.
290 */
291static unsigned int cs4270_read_reg_cache(struct snd_soc_codec *codec,
292 unsigned int reg)
293{
294 u8 *cache = codec->reg_cache;
295
296 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
297 return -EIO;
298
299 return cache[reg - CS4270_FIRSTREG];
300}
301
302/*
303 * Write to a CS4270 register via the I2C bus.
304 *
305 * This function writes the given value to the given CS4270 register, and
306 * also updates the register cache.
307 *
308 * Note that we don't use the hw_write function pointer of snd_soc_codec.
309 * That's because it's too clunky: the hw_write_t prototype does not match
310 * i2c_smbus_write_byte_data(), and it's just another layer of overhead.
311 */
312static int cs4270_i2c_write(struct snd_soc_codec *codec, unsigned int reg,
313 unsigned int value)
314{
Timur Tabibfc4e862007-09-11 00:45:50 +0200315 u8 *cache = codec->reg_cache;
316
Timur Tabib0c813c2007-07-31 18:18:44 +0200317 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
318 return -EIO;
319
Timur Tabibfc4e862007-09-11 00:45:50 +0200320 /* Only perform an I2C operation if the new value is different */
321 if (cache[reg - CS4270_FIRSTREG] != value) {
322 struct i2c_client *client = codec->control_data;
323 if (i2c_smbus_write_byte_data(client, reg, value)) {
324 printk(KERN_ERR "cs4270: I2C write failed\n");
325 return -EIO;
326 }
327
Timur Tabib0c813c2007-07-31 18:18:44 +0200328 /* We've written to the hardware, so update the cache */
Timur Tabib0c813c2007-07-31 18:18:44 +0200329 cache[reg - CS4270_FIRSTREG] = value;
Timur Tabib0c813c2007-07-31 18:18:44 +0200330 }
Timur Tabibfc4e862007-09-11 00:45:50 +0200331
332 return 0;
Timur Tabib0c813c2007-07-31 18:18:44 +0200333}
334
335/*
Timur Tabib0c813c2007-07-31 18:18:44 +0200336 * Program the CS4270 with the given hardware parameters.
337 *
Mark Browndee89c42008-11-18 22:11:38 +0000338 * The .ops functions are used to provide board-specific data, like
Timur Tabib0c813c2007-07-31 18:18:44 +0200339 * input frequencies, to this driver. This function takes that information,
340 * combines it with the hardware parameters provided, and programs the
341 * hardware accordingly.
342 */
343static int cs4270_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000344 struct snd_pcm_hw_params *params,
345 struct snd_soc_dai *dai)
Timur Tabib0c813c2007-07-31 18:18:44 +0200346{
347 struct snd_soc_pcm_runtime *rtd = substream->private_data;
348 struct snd_soc_device *socdev = rtd->socdev;
349 struct snd_soc_codec *codec = socdev->codec;
350 struct cs4270_private *cs4270 = codec->private_data;
Roel Kluine34ba212008-04-17 18:58:34 +0200351 int ret;
Timur Tabib0c813c2007-07-31 18:18:44 +0200352 unsigned int i;
353 unsigned int rate;
354 unsigned int ratio;
355 int reg;
356
357 /* Figure out which MCLK/LRCK ratio to use */
358
359 rate = params_rate(params); /* Sampling rate, in Hz */
360 ratio = cs4270->mclk / rate; /* MCLK/LRCK ratio */
361
Timur Tabi9dbd6272007-08-01 12:22:07 +0200362 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
Timur Tabi84323952007-12-18 15:42:53 +0100363 if (cs4270_mode_ratios[i].ratio == ratio)
Timur Tabib0c813c2007-07-31 18:18:44 +0200364 break;
365 }
366
Timur Tabi9dbd6272007-08-01 12:22:07 +0200367 if (i == NUM_MCLK_RATIOS) {
Timur Tabib0c813c2007-07-31 18:18:44 +0200368 /* We did not find a matching ratio */
369 printk(KERN_ERR "cs4270: could not find matching ratio\n");
370 return -EINVAL;
371 }
372
373 /* Freeze and power-down the codec */
374
375 ret = snd_soc_write(codec, CS4270_PWRCTL, CS4270_PWRCTL_FREEZE |
376 CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC |
377 CS4270_PWRCTL_PDN);
378 if (ret < 0) {
379 printk(KERN_ERR "cs4270: I2C write failed\n");
380 return ret;
381 }
382
383 /* Program the mode control register */
384
385 reg = snd_soc_read(codec, CS4270_MODE);
386 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
387 reg |= cs4270_mode_ratios[i].speed_mode | cs4270_mode_ratios[i].mclk;
388
389 ret = snd_soc_write(codec, CS4270_MODE, reg);
390 if (ret < 0) {
391 printk(KERN_ERR "cs4270: I2C write failed\n");
392 return ret;
393 }
394
395 /* Program the format register */
396
397 reg = snd_soc_read(codec, CS4270_FORMAT);
398 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
399
400 switch (cs4270->mode) {
401 case SND_SOC_DAIFMT_I2S:
402 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
403 break;
404 case SND_SOC_DAIFMT_LEFT_J:
405 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
406 break;
407 default:
408 printk(KERN_ERR "cs4270: unknown format\n");
409 return -EINVAL;
410 }
411
412 ret = snd_soc_write(codec, CS4270_FORMAT, reg);
413 if (ret < 0) {
414 printk(KERN_ERR "cs4270: I2C write failed\n");
415 return ret;
416 }
417
418 /* Disable auto-mute. This feature appears to be buggy, because in
419 some situations, auto-mute will not deactivate when it should. */
420
421 reg = snd_soc_read(codec, CS4270_MUTE);
422 reg &= ~CS4270_MUTE_AUTO;
423 ret = snd_soc_write(codec, CS4270_MUTE, reg);
424 if (ret < 0) {
425 printk(KERN_ERR "cs4270: I2C write failed\n");
426 return ret;
427 }
428
Timur Tabi0c235d12008-08-07 11:22:32 -0500429 /* Disable automatic volume control. It's enabled by default, and
430 * it causes volume change commands to be delayed, sometimes until
431 * after playback has started.
432 */
433
434 reg = cs4270_read_reg_cache(codec, CS4270_TRANS);
435 reg &= ~(CS4270_TRANS_SOFT | CS4270_TRANS_ZERO);
436 ret = cs4270_i2c_write(codec, CS4270_TRANS, reg);
437 if (ret < 0) {
438 printk(KERN_ERR "I2C write failed\n");
439 return ret;
440 }
441
Timur Tabib0c813c2007-07-31 18:18:44 +0200442 /* Thaw and power-up the codec */
443
444 ret = snd_soc_write(codec, CS4270_PWRCTL, 0);
445 if (ret < 0) {
446 printk(KERN_ERR "cs4270: I2C write failed\n");
447 return ret;
448 }
449
450 return ret;
451}
452
453#ifdef CONFIG_SND_SOC_CS4270_HWMUTE
Timur Tabib0c813c2007-07-31 18:18:44 +0200454/*
455 * Set the CS4270 external mute
456 *
457 * This function toggles the mute bits in the MUTE register. The CS4270's
458 * mute capability is intended for external muting circuitry, so if the
459 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
460 * then this function will do nothing.
461 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100462static int cs4270_mute(struct snd_soc_dai *dai, int mute)
Timur Tabib0c813c2007-07-31 18:18:44 +0200463{
464 struct snd_soc_codec *codec = dai->codec;
465 int reg6;
466
467 reg6 = snd_soc_read(codec, CS4270_MUTE);
468
469 if (mute)
470 reg6 |= CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
471 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
472 else
473 reg6 &= ~(CS4270_MUTE_ADC_A | CS4270_MUTE_ADC_B |
474 CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
475
476 return snd_soc_write(codec, CS4270_MUTE, reg6);
477}
Timur Tabiff637d32009-01-22 18:23:39 -0600478#else
479#define cs4270_mute NULL
Timur Tabib0c813c2007-07-31 18:18:44 +0200480#endif
481
Timur Tabib0c813c2007-07-31 18:18:44 +0200482/* A list of non-DAPM controls that the CS4270 supports */
483static const struct snd_kcontrol_new cs4270_snd_controls[] = {
484 SOC_DOUBLE_R("Master Playback Volume",
Timur Tabibfc4e862007-09-11 00:45:50 +0200485 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1)
Timur Tabib0c813c2007-07-31 18:18:44 +0200486};
487
Timur Tabib0c813c2007-07-31 18:18:44 +0200488/*
489 * Global variable to store socdev for i2c probe function.
490 *
491 * If struct i2c_driver had a private_data field, we wouldn't need to use
492 * cs4270_socdec. This is the only way to pass the socdev structure to
493 * cs4270_i2c_probe().
494 *
495 * The real solution to cs4270_socdev is to create a mechanism
496 * that maps I2C addresses to snd_soc_device structures. Perhaps the
497 * creation of the snd_soc_device object should be moved out of
498 * cs4270_probe() and into cs4270_i2c_probe(), but that would make this
499 * driver dependent on I2C. The CS4270 supports "stand-alone" mode, whereby
500 * the chip is *not* connected to the I2C bus, but is instead configured via
501 * input pins.
502 */
503static struct snd_soc_device *cs4270_socdev;
504
505/*
506 * Initialize the I2C interface of the CS4270
507 *
508 * This function is called for whenever the I2C subsystem finds a device
509 * at a particular address.
510 *
511 * Note: snd_soc_new_pcms() must be called before this function can be called,
512 * because of snd_ctl_add().
513 */
Timur Tabiec2cd952008-07-29 16:35:52 -0500514static int cs4270_i2c_probe(struct i2c_client *i2c_client,
515 const struct i2c_device_id *id)
Timur Tabib0c813c2007-07-31 18:18:44 +0200516{
517 struct snd_soc_device *socdev = cs4270_socdev;
518 struct snd_soc_codec *codec = socdev->codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200519 int i;
520 int ret = 0;
521
522 /* Probing all possible addresses has one drawback: if there are
523 multiple CS4270s on the bus, then you cannot specify which
524 socdev is matched with which CS4270. For now, we just reject
525 this I2C device if the socdev already has one attached. */
526 if (codec->control_data)
527 return -ENODEV;
528
529 /* Note: codec_dai->codec is NULL here */
530
Timur Tabib0c813c2007-07-31 18:18:44 +0200531 codec->reg_cache = kzalloc(CS4270_NUMREGS, GFP_KERNEL);
532 if (!codec->reg_cache) {
533 printk(KERN_ERR "cs4270: could not allocate register cache\n");
534 ret = -ENOMEM;
535 goto error;
536 }
537
Timur Tabib0c813c2007-07-31 18:18:44 +0200538 /* Verify that we have a CS4270 */
539
540 ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
541 if (ret < 0) {
542 printk(KERN_ERR "cs4270: failed to read I2C\n");
543 goto error;
544 }
545 /* The top four bits of the chip ID should be 1100. */
546 if ((ret & 0xF0) != 0xC0) {
547 /* The device at this address is not a CS4270 codec */
548 ret = -ENODEV;
549 goto error;
550 }
551
Timur Tabiec2cd952008-07-29 16:35:52 -0500552 printk(KERN_INFO "cs4270: found device at I2C address %X\n",
553 i2c_client->addr);
Timur Tabib0c813c2007-07-31 18:18:44 +0200554 printk(KERN_INFO "cs4270: hardware revision %X\n", ret & 0xF);
555
Timur Tabib0c813c2007-07-31 18:18:44 +0200556 codec->control_data = i2c_client;
557 codec->read = cs4270_read_reg_cache;
558 codec->write = cs4270_i2c_write;
559 codec->reg_cache_size = CS4270_NUMREGS;
560
561 /* The I2C interface is set up, so pre-fill our register cache */
562
563 ret = cs4270_fill_cache(codec);
564 if (ret < 0) {
565 printk(KERN_ERR "cs4270: failed to fill register cache\n");
566 goto error;
567 }
568
569 /* Add the non-DAPM controls */
570
571 for (i = 0; i < ARRAY_SIZE(cs4270_snd_controls); i++) {
572 struct snd_kcontrol *kctrl =
573 snd_soc_cnew(&cs4270_snd_controls[i], codec, NULL);
574
575 ret = snd_ctl_add(codec->card, kctrl);
576 if (ret < 0)
577 goto error;
578 }
579
Timur Tabiec2cd952008-07-29 16:35:52 -0500580 i2c_set_clientdata(i2c_client, codec);
581
Timur Tabib0c813c2007-07-31 18:18:44 +0200582 return 0;
583
584error:
Jean Delvare9778e9a2008-09-27 20:30:52 +0200585 codec->control_data = NULL;
Timur Tabib0c813c2007-07-31 18:18:44 +0200586
587 kfree(codec->reg_cache);
588 codec->reg_cache = NULL;
589 codec->reg_cache_size = 0;
590
Timur Tabib0c813c2007-07-31 18:18:44 +0200591 return ret;
592}
593
Timur Tabiff637d32009-01-22 18:23:39 -0600594static const struct i2c_device_id cs4270_id[] = {
595 {"cs4270", 0},
596 {}
597};
598MODULE_DEVICE_TABLE(i2c, cs4270_id);
599
600static struct i2c_driver cs4270_i2c_driver = {
601 .driver = {
602 .name = "cs4270",
603 .owner = THIS_MODULE,
604 },
605 .id_table = cs4270_id,
606 .probe = cs4270_i2c_probe,
607};
Timur Tabib0c813c2007-07-31 18:18:44 +0200608
Liam Girdwoode550e172008-07-07 16:07:52 +0100609struct snd_soc_dai cs4270_dai = {
Timur Tabib0c813c2007-07-31 18:18:44 +0200610 .name = "CS4270",
611 .playback = {
612 .stream_name = "Playback",
613 .channels_min = 1,
614 .channels_max = 2,
615 .rates = 0,
616 .formats = CS4270_FORMATS,
617 },
618 .capture = {
619 .stream_name = "Capture",
620 .channels_min = 1,
621 .channels_max = 2,
622 .rates = 0,
623 .formats = CS4270_FORMATS,
624 },
Timur Tabib0c813c2007-07-31 18:18:44 +0200625};
626EXPORT_SYMBOL_GPL(cs4270_dai);
627
628/*
629 * ASoC probe function
630 *
631 * This function is called when the machine driver calls
632 * platform_device_add().
633 */
634static int cs4270_probe(struct platform_device *pdev)
635{
636 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
637 struct snd_soc_codec *codec;
638 int ret = 0;
639
640 printk(KERN_INFO "CS4270 ALSA SoC Codec\n");
641
642 /* Allocate enough space for the snd_soc_codec structure
643 and our private data together. */
644 codec = kzalloc(ALIGN(sizeof(struct snd_soc_codec), 4) +
645 sizeof(struct cs4270_private), GFP_KERNEL);
646 if (!codec) {
647 printk(KERN_ERR "cs4270: Could not allocate codec structure\n");
648 return -ENOMEM;
649 }
650
651 mutex_init(&codec->mutex);
652 INIT_LIST_HEAD(&codec->dapm_widgets);
653 INIT_LIST_HEAD(&codec->dapm_paths);
654
655 codec->name = "CS4270";
656 codec->owner = THIS_MODULE;
657 codec->dai = &cs4270_dai;
658 codec->num_dai = 1;
Timur Tabi4df20532007-11-14 12:07:58 +0100659 codec->private_data = (void *) codec +
660 ALIGN(sizeof(struct snd_soc_codec), 4);
Timur Tabib0c813c2007-07-31 18:18:44 +0200661
662 socdev->codec = codec;
663
664 /* Register PCMs */
665
666 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
667 if (ret < 0) {
668 printk(KERN_ERR "cs4270: failed to create PCMs\n");
Jean Delvaree3145df2008-09-30 11:40:37 +0200669 goto error_free_codec;
Timur Tabib0c813c2007-07-31 18:18:44 +0200670 }
671
Timur Tabib0c813c2007-07-31 18:18:44 +0200672 cs4270_socdev = socdev;
673
674 ret = i2c_add_driver(&cs4270_i2c_driver);
675 if (ret) {
676 printk(KERN_ERR "cs4270: failed to attach driver");
Jean Delvaree3145df2008-09-30 11:40:37 +0200677 goto error_free_pcms;
Timur Tabib0c813c2007-07-31 18:18:44 +0200678 }
679
680 /* Did we find a CS4270 on the I2C bus? */
Timur Tabiff637d32009-01-22 18:23:39 -0600681 if (!codec->control_data) {
682 printk(KERN_ERR "cs4270: failed to attach driver");
683 goto error_del_driver;
684 }
685
686 /* Initialize codec ops */
687 cs4270_dai.ops.hw_params = cs4270_hw_params;
688 cs4270_dai.ops.set_sysclk = cs4270_set_dai_sysclk;
689 cs4270_dai.ops.set_fmt = cs4270_set_dai_fmt;
690 cs4270_dai.ops.digital_mute = cs4270_mute;
Timur Tabib0c813c2007-07-31 18:18:44 +0200691
Mark Brown968a6022008-11-28 11:49:07 +0000692 ret = snd_soc_init_card(socdev);
Timur Tabib0c813c2007-07-31 18:18:44 +0200693 if (ret < 0) {
694 printk(KERN_ERR "cs4270: failed to register card\n");
Jean Delvaree3145df2008-09-30 11:40:37 +0200695 goto error_del_driver;
Timur Tabib0c813c2007-07-31 18:18:44 +0200696 }
697
Jean Delvaree3145df2008-09-30 11:40:37 +0200698 return 0;
699
700error_del_driver:
Jean Delvaree3145df2008-09-30 11:40:37 +0200701 i2c_del_driver(&cs4270_i2c_driver);
702
703error_free_pcms:
Jean Delvaree3145df2008-09-30 11:40:37 +0200704 snd_soc_free_pcms(socdev);
705
706error_free_codec:
707 kfree(socdev->codec);
708 socdev->codec = NULL;
709
Timur Tabib0c813c2007-07-31 18:18:44 +0200710 return ret;
711}
712
713static int cs4270_remove(struct platform_device *pdev)
714{
715 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
716
717 snd_soc_free_pcms(socdev);
718
Jean Delvaree3145df2008-09-30 11:40:37 +0200719 i2c_del_driver(&cs4270_i2c_driver);
Timur Tabib0c813c2007-07-31 18:18:44 +0200720
721 kfree(socdev->codec);
722 socdev->codec = NULL;
723
724 return 0;
725}
726
727/*
728 * ASoC codec device structure
729 *
730 * Assign this variable to the codec_dev field of the machine driver's
731 * snd_soc_device structure.
732 */
733struct snd_soc_codec_device soc_codec_device_cs4270 = {
734 .probe = cs4270_probe,
735 .remove = cs4270_remove
736};
737EXPORT_SYMBOL_GPL(soc_codec_device_cs4270);
738
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100739static int __init cs4270_init(void)
Mark Brown64089b82008-12-08 19:17:58 +0000740{
741 return snd_soc_register_dai(&cs4270_dai);
742}
743module_init(cs4270_init);
744
745static void __exit cs4270_exit(void)
746{
747 snd_soc_unregister_dai(&cs4270_dai);
748}
749module_exit(cs4270_exit);
750
Timur Tabib0c813c2007-07-31 18:18:44 +0200751MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
752MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
753MODULE_LICENSE("GPL");