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