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