blob: e378c4d520275721f130adab191b3441ab70351f [file] [log] [blame]
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +02001/*
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>
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020029#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
37enum master_slave_mode {
38 MODE_SLAVE,
39 MODE_SLAVE_AUTO,
40 MODE_MASTER,
41};
42
43struct cs42l51_private {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000044 enum snd_soc_control_type control_type;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020045 unsigned int mclk;
46 unsigned int audio_mode; /* The mode (I2S or left-justified) */
47 enum master_slave_mode func;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020048};
49
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020050#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
56static int cs42l51_fill_cache(struct snd_soc_codec *codec)
57{
58 u8 *cache = codec->reg_cache + 1;
Axel Lin6d4f7092011-09-28 10:11:54 +080059 struct i2c_client *i2c_client = to_i2c_client(codec->dev);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020060 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
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +020074static 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
102static 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
126static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0);
127static 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 */
130static const DECLARE_TLV_DB_SCALE(aout_tlv, -11550, 50, 0);
131
132static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0);
133static const char *chan_mix[] = {
134 "L R",
135 "L+R",
136 "R L",
137};
138
139static const struct soc_enum cs42l51_chan_mix =
140 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(chan_mix), chan_mix);
141
142static 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 */
175static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w,
176 struct snd_kcontrol *kcontrol, int event)
177{
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200178 switch (event) {
179 case SND_SOC_DAPM_PRE_PMD:
Axel Line94de1e2011-11-01 15:17:57 +0800180 snd_soc_update_bits(w->codec, CS42L51_POWER_CTL1,
181 CS42L51_POWER_CTL1_PDN,
182 CS42L51_POWER_CTL1_PDN);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200183 break;
184 default:
185 case SND_SOC_DAPM_POST_PMD:
Axel Line94de1e2011-11-01 15:17:57 +0800186 snd_soc_update_bits(w->codec, CS42L51_POWER_CTL1,
187 CS42L51_POWER_CTL1_PDN, 0);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200188 break;
189 }
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200190
191 return 0;
192}
193
194static const char *cs42l51_dac_names[] = {"Direct PCM",
195 "DSP PCM", "ADC"};
196static const struct soc_enum cs42l51_dac_mux_enum =
197 SOC_ENUM_SINGLE(CS42L51_DAC_CTL, 6, 3, cs42l51_dac_names);
198static const struct snd_kcontrol_new cs42l51_dac_mux_controls =
199 SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum);
200
201static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left",
202 "MIC Left", "MIC+preamp Left"};
203static const struct soc_enum cs42l51_adcl_mux_enum =
204 SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 4, 4, cs42l51_adcl_names);
205static const struct snd_kcontrol_new cs42l51_adcl_mux_controls =
206 SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum);
207
208static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right",
209 "MIC Right", "MIC+preamp Right"};
210static const struct soc_enum cs42l51_adcr_mux_enum =
211 SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 6, 4, cs42l51_adcr_names);
212static const struct snd_kcontrol_new cs42l51_adcr_mux_controls =
213 SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum);
214
215static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = {
216 SND_SOC_DAPM_MICBIAS("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1),
217 SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0,
218 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
219 SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0,
220 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
221 SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture",
222 CS42L51_POWER_CTL1, 1, 1,
223 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
224 SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture",
225 CS42L51_POWER_CTL1, 2, 1,
226 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
227 SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback",
228 CS42L51_POWER_CTL1, 5, 1,
229 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
230 SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback",
231 CS42L51_POWER_CTL1, 6, 1,
232 cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
233
234 /* analog/mic */
235 SND_SOC_DAPM_INPUT("AIN1L"),
236 SND_SOC_DAPM_INPUT("AIN1R"),
237 SND_SOC_DAPM_INPUT("AIN2L"),
238 SND_SOC_DAPM_INPUT("AIN2R"),
239 SND_SOC_DAPM_INPUT("MICL"),
240 SND_SOC_DAPM_INPUT("MICR"),
241
242 SND_SOC_DAPM_MIXER("Mic Preamp Left",
243 CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0),
244 SND_SOC_DAPM_MIXER("Mic Preamp Right",
245 CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0),
246
247 /* HP */
248 SND_SOC_DAPM_OUTPUT("HPL"),
249 SND_SOC_DAPM_OUTPUT("HPR"),
250
251 /* mux */
252 SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0,
253 &cs42l51_dac_mux_controls),
254 SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0,
255 &cs42l51_adcl_mux_controls),
256 SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0,
257 &cs42l51_adcr_mux_controls),
258};
259
260static const struct snd_soc_dapm_route cs42l51_routes[] = {
261 {"HPL", NULL, "Left DAC"},
262 {"HPR", NULL, "Right DAC"},
263
264 {"Left ADC", NULL, "Left PGA"},
265 {"Right ADC", NULL, "Right PGA"},
266
267 {"Mic Preamp Left", NULL, "MICL"},
268 {"Mic Preamp Right", NULL, "MICR"},
269
270 {"PGA-ADC Mux Left", "AIN1 Left", "AIN1L" },
271 {"PGA-ADC Mux Left", "AIN2 Left", "AIN2L" },
272 {"PGA-ADC Mux Left", "MIC Left", "MICL" },
273 {"PGA-ADC Mux Left", "MIC+preamp Left", "Mic Preamp Left" },
274 {"PGA-ADC Mux Right", "AIN1 Right", "AIN1R" },
275 {"PGA-ADC Mux Right", "AIN2 Right", "AIN2R" },
276 {"PGA-ADC Mux Right", "MIC Right", "MICR" },
277 {"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" },
278
279 {"Left PGA", NULL, "PGA-ADC Mux Left"},
280 {"Right PGA", NULL, "PGA-ADC Mux Right"},
281};
282
283static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai,
284 unsigned int format)
285{
286 struct snd_soc_codec *codec = codec_dai->codec;
287 struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200288
289 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
290 case SND_SOC_DAIFMT_I2S:
291 case SND_SOC_DAIFMT_LEFT_J:
292 case SND_SOC_DAIFMT_RIGHT_J:
293 cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
294 break;
295 default:
296 dev_err(codec->dev, "invalid DAI format\n");
Axel Linac601552011-10-06 07:29:56 +0800297 return -EINVAL;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200298 }
299
300 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
301 case SND_SOC_DAIFMT_CBM_CFM:
302 cs42l51->func = MODE_MASTER;
303 break;
304 case SND_SOC_DAIFMT_CBS_CFS:
305 cs42l51->func = MODE_SLAVE_AUTO;
306 break;
307 default:
Axel Linac601552011-10-06 07:29:56 +0800308 dev_err(codec->dev, "Unknown master/slave configuration\n");
309 return -EINVAL;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200310 }
311
Axel Linac601552011-10-06 07:29:56 +0800312 return 0;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200313}
314
315struct cs42l51_ratios {
316 unsigned int ratio;
317 unsigned char speed_mode;
318 unsigned char mclk;
319};
320
321static struct cs42l51_ratios slave_ratios[] = {
322 { 512, CS42L51_QSM_MODE, 0 }, { 768, CS42L51_QSM_MODE, 0 },
323 { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
324 { 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 },
325 { 256, CS42L51_HSM_MODE, 0 }, { 384, CS42L51_HSM_MODE, 0 },
326 { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
327 { 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 },
328 { 128, CS42L51_SSM_MODE, 0 }, { 192, CS42L51_SSM_MODE, 0 },
329 { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
330 { 512, CS42L51_SSM_MODE, 0 }, { 768, CS42L51_SSM_MODE, 0 },
331 { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
332 { 256, CS42L51_DSM_MODE, 0 }, { 384, CS42L51_DSM_MODE, 0 },
333};
334
335static struct cs42l51_ratios slave_auto_ratios[] = {
336 { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
337 { 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 },
338 { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
339 { 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 },
340 { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
341 { 512, CS42L51_SSM_MODE, 1 }, { 768, CS42L51_SSM_MODE, 1 },
342 { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
343 { 256, CS42L51_DSM_MODE, 1 }, { 384, CS42L51_DSM_MODE, 1 },
344};
345
346static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai,
347 int clk_id, unsigned int freq, int dir)
348{
349 struct snd_soc_codec *codec = codec_dai->codec;
350 struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200351
352 cs42l51->mclk = freq;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200353 return 0;
354}
355
356static int cs42l51_hw_params(struct snd_pcm_substream *substream,
357 struct snd_pcm_hw_params *params,
358 struct snd_soc_dai *dai)
359{
360 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000361 struct snd_soc_codec *codec = rtd->codec;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200362 struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
363 int ret;
364 unsigned int i;
365 unsigned int rate;
366 unsigned int ratio;
367 struct cs42l51_ratios *ratios = NULL;
368 int nr_ratios = 0;
369 int intf_ctl, power_ctl, fmt;
370
371 switch (cs42l51->func) {
372 case MODE_MASTER:
373 return -EINVAL;
374 case MODE_SLAVE:
375 ratios = slave_ratios;
376 nr_ratios = ARRAY_SIZE(slave_ratios);
377 break;
378 case MODE_SLAVE_AUTO:
379 ratios = slave_auto_ratios;
380 nr_ratios = ARRAY_SIZE(slave_auto_ratios);
381 break;
382 }
383
384 /* Figure out which MCLK/LRCK ratio to use */
385 rate = params_rate(params); /* Sampling rate, in Hz */
386 ratio = cs42l51->mclk / rate; /* MCLK/LRCK ratio */
387 for (i = 0; i < nr_ratios; i++) {
388 if (ratios[i].ratio == ratio)
389 break;
390 }
391
392 if (i == nr_ratios) {
393 /* We did not find a matching ratio */
394 dev_err(codec->dev, "could not find matching ratio\n");
395 return -EINVAL;
396 }
397
398 intf_ctl = snd_soc_read(codec, CS42L51_INTF_CTL);
399 power_ctl = snd_soc_read(codec, CS42L51_MIC_POWER_CTL);
400
401 intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S
402 | CS42L51_INTF_CTL_DAC_FORMAT(7));
403 power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3)
404 | CS42L51_MIC_POWER_CTL_MCLK_DIV2);
405
406 switch (cs42l51->func) {
407 case MODE_MASTER:
408 intf_ctl |= CS42L51_INTF_CTL_MASTER;
409 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
410 break;
411 case MODE_SLAVE:
412 power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
413 break;
414 case MODE_SLAVE_AUTO:
415 power_ctl |= CS42L51_MIC_POWER_CTL_AUTO;
416 break;
417 }
418
419 switch (cs42l51->audio_mode) {
420 case SND_SOC_DAIFMT_I2S:
421 intf_ctl |= CS42L51_INTF_CTL_ADC_I2S;
422 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S);
423 break;
424 case SND_SOC_DAIFMT_LEFT_J:
425 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24);
426 break;
427 case SND_SOC_DAIFMT_RIGHT_J:
428 switch (params_format(params)) {
429 case SNDRV_PCM_FORMAT_S16_LE:
430 case SNDRV_PCM_FORMAT_S16_BE:
431 fmt = CS42L51_DAC_DIF_RJ16;
432 break;
433 case SNDRV_PCM_FORMAT_S18_3LE:
434 case SNDRV_PCM_FORMAT_S18_3BE:
435 fmt = CS42L51_DAC_DIF_RJ18;
436 break;
437 case SNDRV_PCM_FORMAT_S20_3LE:
438 case SNDRV_PCM_FORMAT_S20_3BE:
439 fmt = CS42L51_DAC_DIF_RJ20;
440 break;
441 case SNDRV_PCM_FORMAT_S24_LE:
442 case SNDRV_PCM_FORMAT_S24_BE:
443 fmt = CS42L51_DAC_DIF_RJ24;
444 break;
445 default:
446 dev_err(codec->dev, "unknown format\n");
447 return -EINVAL;
448 }
449 intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt);
450 break;
451 default:
452 dev_err(codec->dev, "unknown format\n");
453 return -EINVAL;
454 }
455
456 if (ratios[i].mclk)
457 power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2;
458
459 ret = snd_soc_write(codec, CS42L51_INTF_CTL, intf_ctl);
460 if (ret < 0)
461 return ret;
462
463 ret = snd_soc_write(codec, CS42L51_MIC_POWER_CTL, power_ctl);
464 if (ret < 0)
465 return ret;
466
467 return 0;
468}
469
470static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute)
471{
472 struct snd_soc_codec *codec = dai->codec;
473 int reg;
474 int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE;
475
476 reg = snd_soc_read(codec, CS42L51_DAC_OUT_CTL);
477
478 if (mute)
479 reg |= mask;
480 else
481 reg &= ~mask;
482
483 return snd_soc_write(codec, CS42L51_DAC_OUT_CTL, reg);
484}
485
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100486static const struct snd_soc_dai_ops cs42l51_dai_ops = {
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200487 .hw_params = cs42l51_hw_params,
488 .set_sysclk = cs42l51_set_dai_sysclk,
489 .set_fmt = cs42l51_set_dai_fmt,
490 .digital_mute = cs42l51_dai_mute,
491};
492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493static struct snd_soc_dai_driver cs42l51_dai = {
494 .name = "cs42l51-hifi",
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200495 .playback = {
496 .stream_name = "Playback",
497 .channels_min = 1,
498 .channels_max = 2,
499 .rates = SNDRV_PCM_RATE_8000_96000,
500 .formats = CS42L51_FORMATS,
501 },
502 .capture = {
503 .stream_name = "Capture",
504 .channels_min = 1,
505 .channels_max = 2,
506 .rates = SNDRV_PCM_RATE_8000_96000,
507 .formats = CS42L51_FORMATS,
508 },
509 .ops = &cs42l51_dai_ops,
510};
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200511
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512static int cs42l51_probe(struct snd_soc_codec *codec)
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200513{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000514 struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200515 struct snd_soc_dapm_context *dapm = &codec->dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 int ret, reg;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200517
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 ret = cs42l51_fill_cache(codec);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200519 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 dev_err(codec->dev, "failed to fill register cache\n");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200521 return ret;
522 }
523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs42l51->control_type);
525 if (ret < 0) {
526 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
527 return ret;
528 }
529
530 /*
531 * DAC configuration
532 * - Use signal processor
533 * - auto mute
534 * - vol changes immediate
535 * - no de-emphasize
536 */
537 reg = CS42L51_DAC_CTL_DATA_SEL(1)
538 | CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0);
539 ret = snd_soc_write(codec, CS42L51_DAC_CTL, reg);
540 if (ret < 0)
541 return ret;
542
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200543 snd_soc_add_controls(codec, cs42l51_snd_controls,
544 ARRAY_SIZE(cs42l51_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200545 snd_soc_dapm_new_controls(dapm, cs42l51_dapm_widgets,
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200546 ARRAY_SIZE(cs42l51_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200547 snd_soc_dapm_add_routes(dapm, cs42l51_routes,
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200548 ARRAY_SIZE(cs42l51_routes));
549
550 return 0;
551}
552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553static struct snd_soc_codec_driver soc_codec_device_cs42l51 = {
554 .probe = cs42l51_probe,
555 .reg_cache_size = CS42L51_NUMREGS,
556 .reg_word_size = sizeof(u8),
557};
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559static int cs42l51_i2c_probe(struct i2c_client *i2c_client,
560 const struct i2c_device_id *id)
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200561{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 struct cs42l51_private *cs42l51;
563 int ret;
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200564
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 /* Verify that we have a CS42L51 */
566 ret = i2c_smbus_read_byte_data(i2c_client, CS42L51_CHIP_REV_ID);
567 if (ret < 0) {
568 dev_err(&i2c_client->dev, "failed to read I2C\n");
569 goto error;
570 }
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200571
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 if ((ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) &&
573 (ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) {
574 dev_err(&i2c_client->dev, "Invalid chip id\n");
575 ret = -ENODEV;
576 goto error;
577 }
578
579 dev_info(&i2c_client->dev, "found device cs42l51 rev %d\n",
580 ret & 7);
581
582 cs42l51 = kzalloc(sizeof(struct cs42l51_private), GFP_KERNEL);
583 if (!cs42l51) {
584 dev_err(&i2c_client->dev, "could not allocate codec\n");
585 return -ENOMEM;
586 }
587
588 i2c_set_clientdata(i2c_client, cs42l51);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 cs42l51->control_type = SND_SOC_I2C;
590
591 ret = snd_soc_register_codec(&i2c_client->dev,
592 &soc_codec_device_cs42l51, &cs42l51_dai, 1);
593 if (ret < 0)
594 kfree(cs42l51);
595error:
596 return ret;
597}
598
599static int cs42l51_i2c_remove(struct i2c_client *client)
600{
601 struct cs42l51_private *cs42l51 = i2c_get_clientdata(client);
602
603 snd_soc_unregister_codec(&client->dev);
604 kfree(cs42l51);
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200605 return 0;
606}
607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608static const struct i2c_device_id cs42l51_id[] = {
609 {"cs42l51", 0},
610 {}
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200611};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612MODULE_DEVICE_TABLE(i2c, cs42l51_id);
613
614static struct i2c_driver cs42l51_i2c_driver = {
615 .driver = {
Arnaud Patard (Rtp)c88e7b92010-08-30 16:00:05 +0200616 .name = "cs42l51-codec",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 .owner = THIS_MODULE,
618 },
619 .id_table = cs42l51_id,
620 .probe = cs42l51_i2c_probe,
621 .remove = cs42l51_i2c_remove,
622};
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200623
624static int __init cs42l51_init(void)
625{
626 int ret;
627
628 ret = i2c_add_driver(&cs42l51_i2c_driver);
629 if (ret != 0) {
630 printk(KERN_ERR "%s: can't add i2c driver\n", __func__);
631 return ret;
632 }
633 return 0;
634}
635module_init(cs42l51_init);
636
637static void __exit cs42l51_exit(void)
638{
639 i2c_del_driver(&cs42l51_i2c_driver);
640}
641module_exit(cs42l51_exit);
642
Arnaud Patard (Rtp)69737892010-09-09 14:10:33 +0200643MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
apatard@mandriva.com72ed5a82010-05-27 14:57:41 +0200644MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver");
645MODULE_LICENSE("GPL");