2 * uda134x.c -- UDA134X ALSA SoC Codec driver
4 * Modifications by Christian Pellegrin <chripell@evolware.org>
6 * Copyright 2007 Dension Audio Systems Ltd.
9 * Based on the WM87xx drivers by Liam Girdwood and Richard Purdie
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <sound/initval.h>
24 #include <sound/uda134x.h>
30 #define UDA134X_RATES SNDRV_PCM_RATE_8000_48000
31 #define UDA134X_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
32 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE)
38 struct snd_pcm_substream
*master_substream
;
39 struct snd_pcm_substream
*slave_substream
;
42 /* In-data addresses are hard-coded into the reg-cache values */
43 static const char uda134x_reg
[UDA134X_REGS_NUM
] = {
44 /* Extended address registers */
45 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
46 /* Status, data regs */
47 0x00, 0x83, 0x00, 0x40, 0x80, 0xC0, 0x00,
51 * The codec has no support for reading its registers except for peak level...
53 static inline unsigned int uda134x_read_reg_cache(struct snd_soc_codec
*codec
,
56 u8
*cache
= codec
->reg_cache
;
58 if (reg
>= UDA134X_REGS_NUM
)
64 * Write the register cache
66 static inline void uda134x_write_reg_cache(struct snd_soc_codec
*codec
,
67 u8 reg
, unsigned int value
)
69 u8
*cache
= codec
->reg_cache
;
71 if (reg
>= UDA134X_REGS_NUM
)
77 * Write to the uda134x registers
80 static int uda134x_write(struct snd_soc_codec
*codec
, unsigned int reg
,
86 struct uda134x_platform_data
*pd
= codec
->control_data
;
88 pr_debug("%s reg: %02X, value:%02X\n", __func__
, reg
, value
);
90 if (reg
>= UDA134X_REGS_NUM
) {
91 printk(KERN_ERR
"%s unknown register: reg: %u",
96 uda134x_write_reg_cache(codec
, reg
, value
);
100 case UDA134X_STATUS1
:
101 addr
= UDA134X_STATUS_ADDR
;
103 case UDA134X_DATA000
:
104 case UDA134X_DATA001
:
105 case UDA134X_DATA010
:
106 case UDA134X_DATA011
:
107 addr
= UDA134X_DATA0_ADDR
;
110 addr
= UDA134X_DATA1_ADDR
;
113 /* It's an extended address register */
114 addr
= (reg
| UDA134X_EXTADDR_PREFIX
);
116 ret
= l3_write(&pd
->l3
,
117 UDA134X_DATA0_ADDR
, &addr
, 1);
121 addr
= UDA134X_DATA0_ADDR
;
122 data
= (value
| UDA134X_EXTDATA_PREFIX
);
126 ret
= l3_write(&pd
->l3
,
134 static inline void uda134x_reset(struct snd_soc_codec
*codec
)
136 u8 reset_reg
= uda134x_read_reg_cache(codec
, UDA134X_STATUS0
);
137 uda134x_write(codec
, UDA134X_STATUS0
, reset_reg
| (1<<6));
139 uda134x_write(codec
, UDA134X_STATUS0
, reset_reg
& ~(1<<6));
142 static int uda134x_mute(struct snd_soc_dai
*dai
, int mute
)
144 struct snd_soc_codec
*codec
= dai
->codec
;
145 u8 mute_reg
= uda134x_read_reg_cache(codec
, UDA134X_DATA010
);
147 pr_debug("%s mute: %d\n", __func__
, mute
);
154 uda134x_write(codec
, UDA134X_DATA010
, mute_reg
);
159 static int uda134x_startup(struct snd_pcm_substream
*substream
,
160 struct snd_soc_dai
*dai
)
162 struct snd_soc_codec
*codec
= dai
->codec
;
163 struct uda134x_priv
*uda134x
= snd_soc_codec_get_drvdata(codec
);
164 struct snd_pcm_runtime
*master_runtime
;
166 if (uda134x
->master_substream
) {
167 master_runtime
= uda134x
->master_substream
->runtime
;
169 pr_debug("%s constraining to %d bits at %d\n", __func__
,
170 master_runtime
->sample_bits
,
171 master_runtime
->rate
);
173 snd_pcm_hw_constraint_minmax(substream
->runtime
,
174 SNDRV_PCM_HW_PARAM_RATE
,
175 master_runtime
->rate
,
176 master_runtime
->rate
);
178 snd_pcm_hw_constraint_minmax(substream
->runtime
,
179 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
180 master_runtime
->sample_bits
,
181 master_runtime
->sample_bits
);
183 uda134x
->slave_substream
= substream
;
185 uda134x
->master_substream
= substream
;
190 static void uda134x_shutdown(struct snd_pcm_substream
*substream
,
191 struct snd_soc_dai
*dai
)
193 struct snd_soc_codec
*codec
= dai
->codec
;
194 struct uda134x_priv
*uda134x
= snd_soc_codec_get_drvdata(codec
);
196 if (uda134x
->master_substream
== substream
)
197 uda134x
->master_substream
= uda134x
->slave_substream
;
199 uda134x
->slave_substream
= NULL
;
202 static int uda134x_hw_params(struct snd_pcm_substream
*substream
,
203 struct snd_pcm_hw_params
*params
,
204 struct snd_soc_dai
*dai
)
206 struct snd_soc_codec
*codec
= dai
->codec
;
207 struct uda134x_priv
*uda134x
= snd_soc_codec_get_drvdata(codec
);
210 if (substream
== uda134x
->slave_substream
) {
211 pr_debug("%s ignoring hw_params for slave substream\n",
216 hw_params
= uda134x_read_reg_cache(codec
, UDA134X_STATUS0
);
217 hw_params
&= STATUS0_SYSCLK_MASK
;
218 hw_params
&= STATUS0_DAIFMT_MASK
;
220 pr_debug("%s sysclk: %d, rate:%d\n", __func__
,
221 uda134x
->sysclk
, params_rate(params
));
223 /* set SYSCLK / fs ratio */
224 switch (uda134x
->sysclk
/ params_rate(params
)) {
234 printk(KERN_ERR
"%s unsupported fs\n", __func__
);
238 pr_debug("%s dai_fmt: %d, params_format:%d\n", __func__
,
239 uda134x
->dai_fmt
, params_format(params
));
241 /* set DAI format and word length */
242 switch (uda134x
->dai_fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
243 case SND_SOC_DAIFMT_I2S
:
245 case SND_SOC_DAIFMT_RIGHT_J
:
246 switch (params_format(params
)) {
247 case SNDRV_PCM_FORMAT_S16_LE
:
250 case SNDRV_PCM_FORMAT_S18_3LE
:
253 case SNDRV_PCM_FORMAT_S20_3LE
:
254 hw_params
|= ((1<<2) | (1<<1));
257 printk(KERN_ERR
"%s unsupported format (right)\n",
262 case SND_SOC_DAIFMT_LEFT_J
:
266 printk(KERN_ERR
"%s unsupported format\n", __func__
);
270 uda134x_write(codec
, UDA134X_STATUS0
, hw_params
);
275 static int uda134x_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
276 int clk_id
, unsigned int freq
, int dir
)
278 struct snd_soc_codec
*codec
= codec_dai
->codec
;
279 struct uda134x_priv
*uda134x
= snd_soc_codec_get_drvdata(codec
);
281 pr_debug("%s clk_id: %d, freq: %u, dir: %d\n", __func__
,
284 /* Anything between 256fs*8Khz and 512fs*48Khz should be acceptable
285 because the codec is slave. Of course limitations of the clock
286 master (the IIS controller) apply.
287 We'll error out on set_hw_params if it's not OK */
288 if ((freq
>= (256 * 8000)) && (freq
<= (512 * 48000))) {
289 uda134x
->sysclk
= freq
;
293 printk(KERN_ERR
"%s unsupported sysclk\n", __func__
);
297 static int uda134x_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
300 struct snd_soc_codec
*codec
= codec_dai
->codec
;
301 struct uda134x_priv
*uda134x
= snd_soc_codec_get_drvdata(codec
);
303 pr_debug("%s fmt: %08X\n", __func__
, fmt
);
305 /* codec supports only full slave mode */
306 if ((fmt
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
) {
307 printk(KERN_ERR
"%s unsupported slave mode\n", __func__
);
311 /* no support for clock inversion */
312 if ((fmt
& SND_SOC_DAIFMT_INV_MASK
) != SND_SOC_DAIFMT_NB_NF
) {
313 printk(KERN_ERR
"%s unsupported clock inversion\n", __func__
);
317 /* We can't setup DAI format here as it depends on the word bit num */
318 /* so let's just store the value for later */
319 uda134x
->dai_fmt
= fmt
;
324 static int uda134x_set_bias_level(struct snd_soc_codec
*codec
,
325 enum snd_soc_bias_level level
)
327 struct uda134x_platform_data
*pd
= codec
->control_data
;
329 u8
*cache
= codec
->reg_cache
;
331 pr_debug("%s bias level %d\n", __func__
, level
);
334 case SND_SOC_BIAS_ON
:
336 case SND_SOC_BIAS_PREPARE
:
340 /* Sync reg_cache with the hardware */
341 for (i
= 0; i
< ARRAY_SIZE(uda134x_reg
); i
++)
342 codec
->driver
->write(codec
, i
, *cache
++);
345 case SND_SOC_BIAS_STANDBY
:
347 case SND_SOC_BIAS_OFF
:
353 codec
->dapm
.bias_level
= level
;
357 static const char *uda134x_dsp_setting
[] = {"Flat", "Minimum1",
358 "Minimum2", "Maximum"};
359 static const char *uda134x_deemph
[] = {"None", "32Khz", "44.1Khz", "48Khz"};
360 static const char *uda134x_mixmode
[] = {"Differential", "Analog1",
363 static const struct soc_enum uda134x_mixer_enum
[] = {
364 SOC_ENUM_SINGLE(UDA134X_DATA010
, 0, 0x04, uda134x_dsp_setting
),
365 SOC_ENUM_SINGLE(UDA134X_DATA010
, 3, 0x04, uda134x_deemph
),
366 SOC_ENUM_SINGLE(UDA134X_EA010
, 0, 0x04, uda134x_mixmode
),
369 static const struct snd_kcontrol_new uda1341_snd_controls
[] = {
370 SOC_SINGLE("Master Playback Volume", UDA134X_DATA000
, 0, 0x3F, 1),
371 SOC_SINGLE("Capture Volume", UDA134X_EA010
, 2, 0x07, 0),
372 SOC_SINGLE("Analog1 Volume", UDA134X_EA000
, 0, 0x1F, 1),
373 SOC_SINGLE("Analog2 Volume", UDA134X_EA001
, 0, 0x1F, 1),
375 SOC_SINGLE("Mic Sensitivity", UDA134X_EA010
, 2, 7, 0),
376 SOC_SINGLE("Mic Volume", UDA134X_EA101
, 0, 0x1F, 0),
378 SOC_SINGLE("Tone Control - Bass", UDA134X_DATA001
, 2, 0xF, 0),
379 SOC_SINGLE("Tone Control - Treble", UDA134X_DATA001
, 0, 3, 0),
381 SOC_ENUM("Sound Processing Filter", uda134x_mixer_enum
[0]),
382 SOC_ENUM("PCM Playback De-emphasis", uda134x_mixer_enum
[1]),
383 SOC_ENUM("Input Mux", uda134x_mixer_enum
[2]),
385 SOC_SINGLE("AGC Switch", UDA134X_EA100
, 4, 1, 0),
386 SOC_SINGLE("AGC Target Volume", UDA134X_EA110
, 0, 0x03, 1),
387 SOC_SINGLE("AGC Timing", UDA134X_EA110
, 2, 0x07, 0),
389 SOC_SINGLE("DAC +6dB Switch", UDA134X_STATUS1
, 6, 1, 0),
390 SOC_SINGLE("ADC +6dB Switch", UDA134X_STATUS1
, 5, 1, 0),
391 SOC_SINGLE("ADC Polarity Switch", UDA134X_STATUS1
, 4, 1, 0),
392 SOC_SINGLE("DAC Polarity Switch", UDA134X_STATUS1
, 3, 1, 0),
393 SOC_SINGLE("Double Speed Playback Switch", UDA134X_STATUS1
, 2, 1, 0),
394 SOC_SINGLE("DC Filter Enable Switch", UDA134X_STATUS0
, 0, 1, 0),
397 static const struct snd_kcontrol_new uda1340_snd_controls
[] = {
398 SOC_SINGLE("Master Playback Volume", UDA134X_DATA000
, 0, 0x3F, 1),
400 SOC_SINGLE("Tone Control - Bass", UDA134X_DATA001
, 2, 0xF, 0),
401 SOC_SINGLE("Tone Control - Treble", UDA134X_DATA001
, 0, 3, 0),
403 SOC_ENUM("Sound Processing Filter", uda134x_mixer_enum
[0]),
404 SOC_ENUM("PCM Playback De-emphasis", uda134x_mixer_enum
[1]),
406 SOC_SINGLE("DC Filter Enable Switch", UDA134X_STATUS0
, 0, 1, 0),
409 static const struct snd_kcontrol_new uda1345_snd_controls
[] = {
410 SOC_SINGLE("Master Playback Volume", UDA134X_DATA000
, 0, 0x3F, 1),
412 SOC_ENUM("PCM Playback De-emphasis", uda134x_mixer_enum
[1]),
414 SOC_SINGLE("DC Filter Enable Switch", UDA134X_STATUS0
, 0, 1, 0),
417 /* UDA1341 has the DAC/ADC power down in STATUS1 */
418 static const struct snd_soc_dapm_widget uda1341_dapm_widgets
[] = {
419 SND_SOC_DAPM_DAC("DAC", "Playback", UDA134X_STATUS1
, 0, 0),
420 SND_SOC_DAPM_ADC("ADC", "Capture", UDA134X_STATUS1
, 1, 0),
423 /* UDA1340/4/5 has the DAC/ADC pwoer down in DATA0 11 */
424 static const struct snd_soc_dapm_widget uda1340_dapm_widgets
[] = {
425 SND_SOC_DAPM_DAC("DAC", "Playback", UDA134X_DATA011
, 0, 0),
426 SND_SOC_DAPM_ADC("ADC", "Capture", UDA134X_DATA011
, 1, 0),
429 /* Common DAPM widgets */
430 static const struct snd_soc_dapm_widget uda134x_dapm_widgets
[] = {
431 SND_SOC_DAPM_INPUT("VINL1"),
432 SND_SOC_DAPM_INPUT("VINR1"),
433 SND_SOC_DAPM_INPUT("VINL2"),
434 SND_SOC_DAPM_INPUT("VINR2"),
435 SND_SOC_DAPM_OUTPUT("VOUTL"),
436 SND_SOC_DAPM_OUTPUT("VOUTR"),
439 static const struct snd_soc_dapm_route uda134x_dapm_routes
[] = {
440 { "ADC", NULL
, "VINL1" },
441 { "ADC", NULL
, "VINR1" },
442 { "ADC", NULL
, "VINL2" },
443 { "ADC", NULL
, "VINR2" },
444 { "VOUTL", NULL
, "DAC" },
445 { "VOUTR", NULL
, "DAC" },
448 static const struct snd_soc_dai_ops uda134x_dai_ops
= {
449 .startup
= uda134x_startup
,
450 .shutdown
= uda134x_shutdown
,
451 .hw_params
= uda134x_hw_params
,
452 .digital_mute
= uda134x_mute
,
453 .set_sysclk
= uda134x_set_dai_sysclk
,
454 .set_fmt
= uda134x_set_dai_fmt
,
457 static struct snd_soc_dai_driver uda134x_dai
= {
458 .name
= "uda134x-hifi",
459 /* playback capabilities */
461 .stream_name
= "Playback",
464 .rates
= UDA134X_RATES
,
465 .formats
= UDA134X_FORMATS
,
467 /* capture capabilities */
469 .stream_name
= "Capture",
472 .rates
= UDA134X_RATES
,
473 .formats
= UDA134X_FORMATS
,
476 .ops
= &uda134x_dai_ops
,
479 static int uda134x_soc_probe(struct snd_soc_codec
*codec
)
481 struct uda134x_priv
*uda134x
;
482 struct uda134x_platform_data
*pd
= codec
->card
->dev
->platform_data
;
483 const struct snd_soc_dapm_widget
*widgets
;
484 unsigned num_widgets
;
488 printk(KERN_INFO
"UDA134X SoC Audio Codec\n");
491 printk(KERN_ERR
"UDA134X SoC codec: "
492 "missing L3 bitbang function\n");
497 case UDA134X_UDA1340
:
498 case UDA134X_UDA1341
:
499 case UDA134X_UDA1344
:
500 case UDA134X_UDA1345
:
503 printk(KERN_ERR
"UDA134X SoC codec: "
504 "unsupported model %d\n",
509 uda134x
= kzalloc(sizeof(struct uda134x_priv
), GFP_KERNEL
);
512 snd_soc_codec_set_drvdata(codec
, uda134x
);
514 codec
->control_data
= pd
;
519 uda134x_reset(codec
);
521 if (pd
->is_powered_on_standby
)
522 uda134x_set_bias_level(codec
, SND_SOC_BIAS_ON
);
524 uda134x_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
526 if (pd
->model
== UDA134X_UDA1341
) {
527 widgets
= uda1341_dapm_widgets
;
528 num_widgets
= ARRAY_SIZE(uda1341_dapm_widgets
);
530 widgets
= uda1340_dapm_widgets
;
531 num_widgets
= ARRAY_SIZE(uda1340_dapm_widgets
);
534 ret
= snd_soc_dapm_new_controls(&codec
->dapm
, widgets
, num_widgets
);
536 printk(KERN_ERR
"%s failed to register dapm controls: %d",
543 case UDA134X_UDA1340
:
544 case UDA134X_UDA1344
:
545 ret
= snd_soc_add_codec_controls(codec
, uda1340_snd_controls
,
546 ARRAY_SIZE(uda1340_snd_controls
));
548 case UDA134X_UDA1341
:
549 ret
= snd_soc_add_codec_controls(codec
, uda1341_snd_controls
,
550 ARRAY_SIZE(uda1341_snd_controls
));
552 case UDA134X_UDA1345
:
553 ret
= snd_soc_add_codec_controls(codec
, uda1345_snd_controls
,
554 ARRAY_SIZE(uda1345_snd_controls
));
557 printk(KERN_ERR
"%s unknown codec type: %d",
558 __func__
, pd
->model
);
564 printk(KERN_ERR
"UDA134X: failed to register controls\n");
572 /* power down chip */
573 static int uda134x_soc_remove(struct snd_soc_codec
*codec
)
575 struct uda134x_priv
*uda134x
= snd_soc_codec_get_drvdata(codec
);
577 uda134x_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
578 uda134x_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
584 #if defined(CONFIG_PM)
585 static int uda134x_soc_suspend(struct snd_soc_codec
*codec
)
587 uda134x_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
588 uda134x_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
592 static int uda134x_soc_resume(struct snd_soc_codec
*codec
)
594 uda134x_set_bias_level(codec
, SND_SOC_BIAS_PREPARE
);
595 uda134x_set_bias_level(codec
, SND_SOC_BIAS_ON
);
599 #define uda134x_soc_suspend NULL
600 #define uda134x_soc_resume NULL
601 #endif /* CONFIG_PM */
603 static struct snd_soc_codec_driver soc_codec_dev_uda134x
= {
604 .probe
= uda134x_soc_probe
,
605 .remove
= uda134x_soc_remove
,
606 .suspend
= uda134x_soc_suspend
,
607 .resume
= uda134x_soc_resume
,
608 .reg_cache_size
= sizeof(uda134x_reg
),
609 .reg_word_size
= sizeof(u8
),
610 .reg_cache_default
= uda134x_reg
,
612 .read
= uda134x_read_reg_cache
,
613 .write
= uda134x_write
,
614 .set_bias_level
= uda134x_set_bias_level
,
615 .dapm_widgets
= uda134x_dapm_widgets
,
616 .num_dapm_widgets
= ARRAY_SIZE(uda134x_dapm_widgets
),
617 .dapm_routes
= uda134x_dapm_routes
,
618 .num_dapm_routes
= ARRAY_SIZE(uda134x_dapm_routes
),
621 static int uda134x_codec_probe(struct platform_device
*pdev
)
623 return snd_soc_register_codec(&pdev
->dev
,
624 &soc_codec_dev_uda134x
, &uda134x_dai
, 1);
627 static int uda134x_codec_remove(struct platform_device
*pdev
)
629 snd_soc_unregister_codec(&pdev
->dev
);
633 static struct platform_driver uda134x_codec_driver
= {
635 .name
= "uda134x-codec",
636 .owner
= THIS_MODULE
,
638 .probe
= uda134x_codec_probe
,
639 .remove
= uda134x_codec_remove
,
642 module_platform_driver(uda134x_codec_driver
);
644 MODULE_DESCRIPTION("UDA134X ALSA soc codec driver");
645 MODULE_AUTHOR("Zoltan Devai, Christian Pellegrin <chripell@evolware.org>");
646 MODULE_LICENSE("GPL");