Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[pandora-kernel.git] / sound / soc / codecs / cs42l51.c
1 /*
2  * cs42l51.c
3  *
4  * ASoC Driver for Cirrus Logic CS42L51 codecs
5  *
6  * Copyright (c) 2010 Arnaud Patard <apatard@mandriva.com>
7  *
8  * Based on cs4270.c - Copyright (c) Freescale Semiconductor
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * For now:
20  *  - Only I2C is support. Not SPI
21  *  - master mode *NOT* supported
22  */
23
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <sound/core.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
30 #include <sound/initval.h>
31 #include <sound/pcm_params.h>
32 #include <sound/pcm.h>
33 #include <linux/i2c.h>
34
35 #include "cs42l51.h"
36
37 enum master_slave_mode {
38         MODE_SLAVE,
39         MODE_SLAVE_AUTO,
40         MODE_MASTER,
41 };
42
43 struct cs42l51_private {
44         enum snd_soc_control_type control_type;
45         unsigned int mclk;
46         unsigned int audio_mode;        /* The mode (I2S or left-justified) */
47         enum master_slave_mode func;
48 };
49
50 #define CS42L51_FORMATS ( \
51                 SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
52                 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
53                 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
54                 SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)
55
56 static int cs42l51_fill_cache(struct snd_soc_codec *codec)
57 {
58         u8 *cache = codec->reg_cache + 1;
59         struct i2c_client *i2c_client = to_i2c_client(codec->dev);
60         s32 length;
61
62         length = i2c_smbus_read_i2c_block_data(i2c_client,
63                         CS42L51_FIRSTREG | 0x80, CS42L51_NUMREGS, cache);
64         if (length != CS42L51_NUMREGS) {
65                 dev_err(&i2c_client->dev,
66                                 "I2C read failure, addr=0x%x (ret=%d vs %d)\n",
67                                 i2c_client->addr, length, CS42L51_NUMREGS);
68                 return -EIO;
69         }
70
71         return 0;
72 }
73
74 static int cs42l51_get_chan_mix(struct snd_kcontrol *kcontrol,
75                         struct snd_ctl_elem_value *ucontrol)
76 {
77         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
78         unsigned long value = snd_soc_read(codec, CS42L51_PCM_MIXER)&3;
79
80         switch (value) {
81         default:
82         case 0:
83                 ucontrol->value.integer.value[0] = 0;
84                 break;
85         /* same value : (L+R)/2 and (R+L)/2 */
86         case 1:
87         case 2:
88                 ucontrol->value.integer.value[0] = 1;
89                 break;
90         case 3:
91                 ucontrol->value.integer.value[0] = 2;
92                 break;
93         }
94
95         return 0;
96 }
97
98 #define CHAN_MIX_NORMAL 0x00
99 #define CHAN_MIX_BOTH   0x55
100 #define CHAN_MIX_SWAP   0xFF
101
102 static int cs42l51_set_chan_mix(struct snd_kcontrol *kcontrol,
103                         struct snd_ctl_elem_value *ucontrol)
104 {
105         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
106         unsigned char val;
107
108         switch (ucontrol->value.integer.value[0]) {
109         default:
110         case 0:
111                 val = CHAN_MIX_NORMAL;
112                 break;
113         case 1:
114                 val = CHAN_MIX_BOTH;
115                 break;
116         case 2:
117                 val = CHAN_MIX_SWAP;
118                 break;
119         }
120
121         snd_soc_write(codec, CS42L51_PCM_MIXER, val);
122
123         return 1;
124 }
125
126 static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0);
127 static const DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0);
128 /* This is a lie. after -102 db, it stays at -102 */
129 /* maybe a range would be better */
130 static const DECLARE_TLV_DB_SCALE(aout_tlv, -11550, 50, 0);
131
132 static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0);
133 static const char *chan_mix[] = {
134         "L R",
135         "L+R",
136         "R L",
137 };
138
139 static const struct soc_enum cs42l51_chan_mix =
140         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(chan_mix), chan_mix);
141
142 static const struct snd_kcontrol_new cs42l51_snd_controls[] = {
143         SOC_DOUBLE_R_SX_TLV("PCM Playback Volume",
144                         CS42L51_PCMA_VOL, CS42L51_PCMB_VOL,
145                         7, 0xffffff99, 0x18, adc_pcm_tlv),
146         SOC_DOUBLE_R("PCM Playback Switch",
147                         CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, 7, 1, 1),
148         SOC_DOUBLE_R_SX_TLV("Analog Playback Volume",
149                         CS42L51_AOUTA_VOL, CS42L51_AOUTB_VOL,
150                         8, 0xffffff19, 0x18, aout_tlv),
151         SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume",
152                         CS42L51_ADCA_VOL, CS42L51_ADCB_VOL,
153                         7, 0xffffff99, 0x18, adc_pcm_tlv),
154         SOC_DOUBLE_R("ADC Mixer Switch",
155                         CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1),
156         SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0),
157         SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0),
158         SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0),
159         SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0),
160         SOC_DOUBLE_TLV("Mic Boost Volume",
161                         CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv),
162         SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv),
163         SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv),
164         SOC_ENUM_EXT("PCM channel mixer",
165                         cs42l51_chan_mix,
166                         cs42l51_get_chan_mix, cs42l51_set_chan_mix),
167 };
168
169 /*
170  * to power down, one must:
171  * 1.) Enable the PDN bit
172  * 2.) enable power-down for the select channels
173  * 3.) disable the PDN bit.
174  */
175 static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w,
176                 struct snd_kcontrol *kcontrol, int event)
177 {
178         unsigned long value;
179
180         value = snd_soc_read(w->codec, CS42L51_POWER_CTL1);
181         value &= ~CS42L51_POWER_CTL1_PDN;
182
183         switch (event) {
184         case SND_SOC_DAPM_PRE_PMD:
185                 value |= CS42L51_POWER_CTL1_PDN;
186                 break;
187         default:
188         case SND_SOC_DAPM_POST_PMD:
189                 break;
190         }
191         snd_soc_update_bits(w->codec, CS42L51_POWER_CTL1,
192                 CS42L51_POWER_CTL1_PDN, value);
193
194         return 0;
195 }
196
197 static const char *cs42l51_dac_names[] = {"Direct PCM",
198         "DSP PCM", "ADC"};
199 static const struct soc_enum cs42l51_dac_mux_enum =
200         SOC_ENUM_SINGLE(CS42L51_DAC_CTL, 6, 3, cs42l51_dac_names);
201 static const struct snd_kcontrol_new cs42l51_dac_mux_controls =
202         SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum);
203
204 static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left",
205         "MIC Left", "MIC+preamp Left"};
206 static const struct soc_enum cs42l51_adcl_mux_enum =
207         SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 4, 4, cs42l51_adcl_names);
208 static const struct snd_kcontrol_new cs42l51_adcl_mux_controls =
209         SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum);
210
211 static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right",
212         "MIC Right", "MIC+preamp Right"};
213 static const struct soc_enum cs42l51_adcr_mux_enum =
214         SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 6, 4, cs42l51_adcr_names);
215 static const struct snd_kcontrol_new cs42l51_adcr_mux_controls =
216         SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum);
217
218 static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = {
219         SND_SOC_DAPM_MICBIAS("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1),
220         SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0,
221                 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
222         SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0,
223                 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
224         SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture",
225                 CS42L51_POWER_CTL1, 1, 1,
226                 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
227         SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture",
228                 CS42L51_POWER_CTL1, 2, 1,
229                 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
230         SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback",
231                 CS42L51_POWER_CTL1, 5, 1,
232                 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
233         SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback",
234                 CS42L51_POWER_CTL1, 6, 1,
235                 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
236
237         /* analog/mic */
238         SND_SOC_DAPM_INPUT("AIN1L"),
239         SND_SOC_DAPM_INPUT("AIN1R"),
240         SND_SOC_DAPM_INPUT("AIN2L"),
241         SND_SOC_DAPM_INPUT("AIN2R"),
242         SND_SOC_DAPM_INPUT("MICL"),
243         SND_SOC_DAPM_INPUT("MICR"),
244
245         SND_SOC_DAPM_MIXER("Mic Preamp Left",
246                 CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0),
247         SND_SOC_DAPM_MIXER("Mic Preamp Right",
248                 CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0),
249
250         /* HP */
251         SND_SOC_DAPM_OUTPUT("HPL"),
252         SND_SOC_DAPM_OUTPUT("HPR"),
253
254         /* mux */
255         SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0,
256                 &cs42l51_dac_mux_controls),
257         SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0,
258                 &cs42l51_adcl_mux_controls),
259         SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0,
260                 &cs42l51_adcr_mux_controls),
261 };
262
263 static const struct snd_soc_dapm_route cs42l51_routes[] = {
264         {"HPL", NULL, "Left DAC"},
265         {"HPR", NULL, "Right DAC"},
266
267         {"Left ADC", NULL, "Left PGA"},
268         {"Right ADC", NULL, "Right PGA"},
269
270         {"Mic Preamp Left",  NULL,  "MICL"},
271         {"Mic Preamp Right", NULL,  "MICR"},
272
273         {"PGA-ADC Mux Left",  "AIN1 Left",        "AIN1L" },
274         {"PGA-ADC Mux Left",  "AIN2 Left",        "AIN2L" },
275         {"PGA-ADC Mux Left",  "MIC Left",         "MICL"  },
276         {"PGA-ADC Mux Left",  "MIC+preamp Left",  "Mic Preamp Left" },
277         {"PGA-ADC Mux Right", "AIN1 Right",       "AIN1R" },
278         {"PGA-ADC Mux Right", "AIN2 Right",       "AIN2R" },
279         {"PGA-ADC Mux Right", "MIC Right",        "MICR" },
280         {"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" },
281
282         {"Left PGA", NULL, "PGA-ADC Mux Left"},
283         {"Right PGA", NULL, "PGA-ADC Mux Right"},
284 };
285
286 static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai,
287                 unsigned int format)
288 {
289         struct snd_soc_codec *codec = codec_dai->codec;
290         struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
291
292         switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
293         case SND_SOC_DAIFMT_I2S:
294         case SND_SOC_DAIFMT_LEFT_J:
295         case SND_SOC_DAIFMT_RIGHT_J:
296                 cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
297                 break;
298         default:
299                 dev_err(codec->dev, "invalid DAI format\n");
300                 return -EINVAL;
301         }
302
303         switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
304         case SND_SOC_DAIFMT_CBM_CFM:
305                 cs42l51->func = MODE_MASTER;
306                 break;
307         case SND_SOC_DAIFMT_CBS_CFS:
308                 cs42l51->func = MODE_SLAVE_AUTO;
309                 break;
310         default:
311                 dev_err(codec->dev, "Unknown master/slave configuration\n");
312                 return -EINVAL;
313         }
314
315         return 0;
316 }
317
318 struct cs42l51_ratios {
319         unsigned int ratio;
320         unsigned char speed_mode;
321         unsigned char mclk;
322 };
323
324 static struct cs42l51_ratios slave_ratios[] = {
325         {  512, CS42L51_QSM_MODE, 0 }, {  768, CS42L51_QSM_MODE, 0 },
326         { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
327         { 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 },
328         {  256, CS42L51_HSM_MODE, 0 }, {  384, CS42L51_HSM_MODE, 0 },
329         {  512, CS42L51_HSM_MODE, 0 }, {  768, CS42L51_HSM_MODE, 0 },
330         { 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 },
331         {  128, CS42L51_SSM_MODE, 0 }, {  192, CS42L51_SSM_MODE, 0 },
332         {  256, CS42L51_SSM_MODE, 0 }, {  384, CS42L51_SSM_MODE, 0 },
333         {  512, CS42L51_SSM_MODE, 0 }, {  768, CS42L51_SSM_MODE, 0 },
334         {  128, CS42L51_DSM_MODE, 0 }, {  192, CS42L51_DSM_MODE, 0 },
335         {  256, CS42L51_DSM_MODE, 0 }, {  384, CS42L51_DSM_MODE, 0 },
336 };
337
338 static struct cs42l51_ratios slave_auto_ratios[] = {
339         { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
340         { 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 },
341         {  512, CS42L51_HSM_MODE, 0 }, {  768, CS42L51_HSM_MODE, 0 },
342         { 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 },
343         {  256, CS42L51_SSM_MODE, 0 }, {  384, CS42L51_SSM_MODE, 0 },
344         {  512, CS42L51_SSM_MODE, 1 }, {  768, CS42L51_SSM_MODE, 1 },
345         {  128, CS42L51_DSM_MODE, 0 }, {  192, CS42L51_DSM_MODE, 0 },
346         {  256, CS42L51_DSM_MODE, 1 }, {  384, CS42L51_DSM_MODE, 1 },
347 };
348
349 static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai,
350                 int clk_id, unsigned int freq, int dir)
351 {
352         struct snd_soc_codec *codec = codec_dai->codec;
353         struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
354
355         cs42l51->mclk = freq;
356         return 0;
357 }
358
359 static int cs42l51_hw_params(struct snd_pcm_substream *substream,
360                 struct snd_pcm_hw_params *params,
361                 struct snd_soc_dai *dai)
362 {
363         struct snd_soc_pcm_runtime *rtd = substream->private_data;
364         struct snd_soc_codec *codec = rtd->codec;
365         struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
366         int ret;
367         unsigned int i;
368         unsigned int rate;
369         unsigned int ratio;
370         struct cs42l51_ratios *ratios = NULL;
371         int nr_ratios = 0;
372         int intf_ctl, power_ctl, fmt;
373
374         switch (cs42l51->func) {
375         case MODE_MASTER:
376                 return -EINVAL;
377         case MODE_SLAVE:
378                 ratios = slave_ratios;
379                 nr_ratios = ARRAY_SIZE(slave_ratios);
380                 break;
381         case MODE_SLAVE_AUTO:
382                 ratios = slave_auto_ratios;
383                 nr_ratios = ARRAY_SIZE(slave_auto_ratios);
384                 break;
385         }
386
387         /* Figure out which MCLK/LRCK ratio to use */
388         rate = params_rate(params);     /* Sampling rate, in Hz */
389         ratio = cs42l51->mclk / rate;    /* MCLK/LRCK ratio */
390         for (i = 0; i < nr_ratios; i++) {
391                 if (ratios[i].ratio == ratio)
392                         break;
393         }
394
395         if (i == nr_ratios) {
396                 /* We did not find a matching ratio */
397                 dev_err(codec->dev, "could not find matching ratio\n");
398                 return -EINVAL;
399         }
400
401         intf_ctl = snd_soc_read(codec, CS42L51_INTF_CTL);
402         power_ctl = snd_soc_read(codec, CS42L51_MIC_POWER_CTL);
403
404         intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S
405                         | CS42L51_INTF_CTL_DAC_FORMAT(7));
406         power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3)
407                         | CS42L51_MIC_POWER_CTL_MCLK_DIV2);
408
409         switch (cs42l51->func) {
410         case MODE_MASTER:
411                 intf_ctl |= CS42L51_INTF_CTL_MASTER;
412                 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
413                 break;
414         case MODE_SLAVE:
415                 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
416                 break;
417         case MODE_SLAVE_AUTO:
418                 power_ctl |= CS42L51_MIC_POWER_CTL_AUTO;
419                 break;
420         }
421
422         switch (cs42l51->audio_mode) {
423         case SND_SOC_DAIFMT_I2S:
424                 intf_ctl |= CS42L51_INTF_CTL_ADC_I2S;
425                 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S);
426                 break;
427         case SND_SOC_DAIFMT_LEFT_J:
428                 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24);
429                 break;
430         case SND_SOC_DAIFMT_RIGHT_J:
431                 switch (params_format(params)) {
432                 case SNDRV_PCM_FORMAT_S16_LE:
433                 case SNDRV_PCM_FORMAT_S16_BE:
434                         fmt = CS42L51_DAC_DIF_RJ16;
435                         break;
436                 case SNDRV_PCM_FORMAT_S18_3LE:
437                 case SNDRV_PCM_FORMAT_S18_3BE:
438                         fmt = CS42L51_DAC_DIF_RJ18;
439                         break;
440                 case SNDRV_PCM_FORMAT_S20_3LE:
441                 case SNDRV_PCM_FORMAT_S20_3BE:
442                         fmt = CS42L51_DAC_DIF_RJ20;
443                         break;
444                 case SNDRV_PCM_FORMAT_S24_LE:
445                 case SNDRV_PCM_FORMAT_S24_BE:
446                         fmt = CS42L51_DAC_DIF_RJ24;
447                         break;
448                 default:
449                         dev_err(codec->dev, "unknown format\n");
450                         return -EINVAL;
451                 }
452                 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt);
453                 break;
454         default:
455                 dev_err(codec->dev, "unknown format\n");
456                 return -EINVAL;
457         }
458
459         if (ratios[i].mclk)
460                 power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2;
461
462         ret = snd_soc_write(codec, CS42L51_INTF_CTL, intf_ctl);
463         if (ret < 0)
464                 return ret;
465
466         ret = snd_soc_write(codec, CS42L51_MIC_POWER_CTL, power_ctl);
467         if (ret < 0)
468                 return ret;
469
470         return 0;
471 }
472
473 static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute)
474 {
475         struct snd_soc_codec *codec = dai->codec;
476         int reg;
477         int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE;
478
479         reg = snd_soc_read(codec, CS42L51_DAC_OUT_CTL);
480
481         if (mute)
482                 reg |= mask;
483         else
484                 reg &= ~mask;
485
486         return snd_soc_write(codec, CS42L51_DAC_OUT_CTL, reg);
487 }
488
489 static struct snd_soc_dai_ops cs42l51_dai_ops = {
490         .hw_params      = cs42l51_hw_params,
491         .set_sysclk     = cs42l51_set_dai_sysclk,
492         .set_fmt        = cs42l51_set_dai_fmt,
493         .digital_mute   = cs42l51_dai_mute,
494 };
495
496 static struct snd_soc_dai_driver cs42l51_dai = {
497         .name = "cs42l51-hifi",
498         .playback = {
499                 .stream_name = "Playback",
500                 .channels_min = 1,
501                 .channels_max = 2,
502                 .rates = SNDRV_PCM_RATE_8000_96000,
503                 .formats = CS42L51_FORMATS,
504         },
505         .capture = {
506                 .stream_name = "Capture",
507                 .channels_min = 1,
508                 .channels_max = 2,
509                 .rates = SNDRV_PCM_RATE_8000_96000,
510                 .formats = CS42L51_FORMATS,
511         },
512         .ops = &cs42l51_dai_ops,
513 };
514
515 static int cs42l51_probe(struct snd_soc_codec *codec)
516 {
517         struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
518         struct snd_soc_dapm_context *dapm = &codec->dapm;
519         int ret, reg;
520
521         ret = cs42l51_fill_cache(codec);
522         if (ret < 0) {
523                 dev_err(codec->dev, "failed to fill register cache\n");
524                 return ret;
525         }
526
527         ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs42l51->control_type);
528         if (ret < 0) {
529                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
530                 return ret;
531         }
532
533         /*
534          * DAC configuration
535          * - Use signal processor
536          * - auto mute
537          * - vol changes immediate
538          * - no de-emphasize
539          */
540         reg = CS42L51_DAC_CTL_DATA_SEL(1)
541                 | CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0);
542         ret = snd_soc_write(codec, CS42L51_DAC_CTL, reg);
543         if (ret < 0)
544                 return ret;
545
546         snd_soc_add_controls(codec, cs42l51_snd_controls,
547                 ARRAY_SIZE(cs42l51_snd_controls));
548         snd_soc_dapm_new_controls(dapm, cs42l51_dapm_widgets,
549                 ARRAY_SIZE(cs42l51_dapm_widgets));
550         snd_soc_dapm_add_routes(dapm, cs42l51_routes,
551                 ARRAY_SIZE(cs42l51_routes));
552
553         return 0;
554 }
555
556 static struct snd_soc_codec_driver soc_codec_device_cs42l51 = {
557         .probe =        cs42l51_probe,
558         .reg_cache_size = CS42L51_NUMREGS + 1,
559         .reg_word_size = sizeof(u8),
560 };
561
562 static int cs42l51_i2c_probe(struct i2c_client *i2c_client,
563         const struct i2c_device_id *id)
564 {
565         struct cs42l51_private *cs42l51;
566         int ret;
567
568         /* Verify that we have a CS42L51 */
569         ret = i2c_smbus_read_byte_data(i2c_client, CS42L51_CHIP_REV_ID);
570         if (ret < 0) {
571                 dev_err(&i2c_client->dev, "failed to read I2C\n");
572                 goto error;
573         }
574
575         if ((ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) &&
576             (ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) {
577                 dev_err(&i2c_client->dev, "Invalid chip id\n");
578                 ret = -ENODEV;
579                 goto error;
580         }
581
582         dev_info(&i2c_client->dev, "found device cs42l51 rev %d\n",
583                                 ret & 7);
584
585         cs42l51 = kzalloc(sizeof(struct cs42l51_private), GFP_KERNEL);
586         if (!cs42l51) {
587                 dev_err(&i2c_client->dev, "could not allocate codec\n");
588                 return -ENOMEM;
589         }
590
591         i2c_set_clientdata(i2c_client, cs42l51);
592         cs42l51->control_type = SND_SOC_I2C;
593
594         ret =  snd_soc_register_codec(&i2c_client->dev,
595                         &soc_codec_device_cs42l51, &cs42l51_dai, 1);
596         if (ret < 0)
597                 kfree(cs42l51);
598 error:
599         return ret;
600 }
601
602 static int cs42l51_i2c_remove(struct i2c_client *client)
603 {
604         struct cs42l51_private *cs42l51 = i2c_get_clientdata(client);
605
606         snd_soc_unregister_codec(&client->dev);
607         kfree(cs42l51);
608         return 0;
609 }
610
611 static const struct i2c_device_id cs42l51_id[] = {
612         {"cs42l51", 0},
613         {}
614 };
615 MODULE_DEVICE_TABLE(i2c, cs42l51_id);
616
617 static struct i2c_driver cs42l51_i2c_driver = {
618         .driver = {
619                 .name = "cs42l51-codec",
620                 .owner = THIS_MODULE,
621         },
622         .id_table = cs42l51_id,
623         .probe = cs42l51_i2c_probe,
624         .remove = cs42l51_i2c_remove,
625 };
626
627 static int __init cs42l51_init(void)
628 {
629         int ret;
630
631         ret = i2c_add_driver(&cs42l51_i2c_driver);
632         if (ret != 0) {
633                 printk(KERN_ERR "%s: can't add i2c driver\n", __func__);
634                 return ret;
635         }
636         return 0;
637 }
638 module_init(cs42l51_init);
639
640 static void __exit cs42l51_exit(void)
641 {
642         i2c_del_driver(&cs42l51_i2c_driver);
643 }
644 module_exit(cs42l51_exit);
645
646 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
647 MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver");
648 MODULE_LICENSE("GPL");