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