2 * Driver for ADAU1701 SigmaDSP processor
4 * Copyright 2011 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
6 * based on an inital version by Cliff Cai <cliff.cai@analog.com>
8 * Licensed under the GPL-2 or later.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/sigma.h>
16 #include <linux/slab.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
24 #define ADAU1701_DSPCTRL 0x1c
25 #define ADAU1701_SEROCTL 0x1e
26 #define ADAU1701_SERICTL 0x1f
28 #define ADAU1701_AUXNPOW 0x22
30 #define ADAU1701_OSCIPOW 0x26
31 #define ADAU1701_DACSET 0x27
33 #define ADAU1701_NUM_REGS 0x28
35 #define ADAU1701_DSPCTRL_CR (1 << 2)
36 #define ADAU1701_DSPCTRL_DAM (1 << 3)
37 #define ADAU1701_DSPCTRL_ADM (1 << 4)
38 #define ADAU1701_DSPCTRL_SR_48 0x00
39 #define ADAU1701_DSPCTRL_SR_96 0x01
40 #define ADAU1701_DSPCTRL_SR_192 0x02
41 #define ADAU1701_DSPCTRL_SR_MASK 0x03
43 #define ADAU1701_SEROCTL_INV_LRCLK 0x2000
44 #define ADAU1701_SEROCTL_INV_BCLK 0x1000
45 #define ADAU1701_SEROCTL_MASTER 0x0800
47 #define ADAU1701_SEROCTL_OBF16 0x0000
48 #define ADAU1701_SEROCTL_OBF8 0x0200
49 #define ADAU1701_SEROCTL_OBF4 0x0400
50 #define ADAU1701_SEROCTL_OBF2 0x0600
51 #define ADAU1701_SEROCTL_OBF_MASK 0x0600
53 #define ADAU1701_SEROCTL_OLF1024 0x0000
54 #define ADAU1701_SEROCTL_OLF512 0x0080
55 #define ADAU1701_SEROCTL_OLF256 0x0100
56 #define ADAU1701_SEROCTL_OLF_MASK 0x0180
58 #define ADAU1701_SEROCTL_MSB_DEALY1 0x0000
59 #define ADAU1701_SEROCTL_MSB_DEALY0 0x0004
60 #define ADAU1701_SEROCTL_MSB_DEALY8 0x0008
61 #define ADAU1701_SEROCTL_MSB_DEALY12 0x000c
62 #define ADAU1701_SEROCTL_MSB_DEALY16 0x0010
63 #define ADAU1701_SEROCTL_MSB_DEALY_MASK 0x001c
65 #define ADAU1701_SEROCTL_WORD_LEN_24 0x0000
66 #define ADAU1701_SEROCTL_WORD_LEN_20 0x0001
67 #define ADAU1701_SEROCTL_WORD_LEN_16 0x0010
68 #define ADAU1701_SEROCTL_WORD_LEN_MASK 0x0003
70 #define ADAU1701_AUXNPOW_VBPD 0x40
71 #define ADAU1701_AUXNPOW_VRPD 0x20
73 #define ADAU1701_SERICTL_I2S 0
74 #define ADAU1701_SERICTL_LEFTJ 1
75 #define ADAU1701_SERICTL_TDM 2
76 #define ADAU1701_SERICTL_RIGHTJ_24 3
77 #define ADAU1701_SERICTL_RIGHTJ_20 4
78 #define ADAU1701_SERICTL_RIGHTJ_18 5
79 #define ADAU1701_SERICTL_RIGHTJ_16 6
80 #define ADAU1701_SERICTL_MODE_MASK 7
81 #define ADAU1701_SERICTL_INV_BCLK BIT(3)
82 #define ADAU1701_SERICTL_INV_LRCLK BIT(4)
84 #define ADAU1701_OSCIPOW_OPD 0x04
85 #define ADAU1701_DACSET_DACINIT 1
87 #define ADAU1701_FIRMWARE "adau1701.bin"
93 static const struct snd_kcontrol_new adau1701_controls
[] = {
94 SOC_SINGLE("Master Capture Switch", ADAU1701_DSPCTRL
, 4, 1, 0),
97 static const struct snd_soc_dapm_widget adau1701_dapm_widgets
[] = {
98 SND_SOC_DAPM_DAC("DAC0", "Playback", ADAU1701_AUXNPOW
, 3, 1),
99 SND_SOC_DAPM_DAC("DAC1", "Playback", ADAU1701_AUXNPOW
, 2, 1),
100 SND_SOC_DAPM_DAC("DAC2", "Playback", ADAU1701_AUXNPOW
, 1, 1),
101 SND_SOC_DAPM_DAC("DAC3", "Playback", ADAU1701_AUXNPOW
, 0, 1),
102 SND_SOC_DAPM_ADC("ADC", "Capture", ADAU1701_AUXNPOW
, 7, 1),
104 SND_SOC_DAPM_OUTPUT("OUT0"),
105 SND_SOC_DAPM_OUTPUT("OUT1"),
106 SND_SOC_DAPM_OUTPUT("OUT2"),
107 SND_SOC_DAPM_OUTPUT("OUT3"),
108 SND_SOC_DAPM_INPUT("IN0"),
109 SND_SOC_DAPM_INPUT("IN1"),
112 static const struct snd_soc_dapm_route adau1701_dapm_routes
[] = {
113 { "OUT0", NULL
, "DAC0" },
114 { "OUT1", NULL
, "DAC1" },
115 { "OUT2", NULL
, "DAC2" },
116 { "OUT3", NULL
, "DAC3" },
118 { "ADC", NULL
, "IN0" },
119 { "ADC", NULL
, "IN1" },
122 static unsigned int adau1701_register_size(struct snd_soc_codec
*codec
,
126 case ADAU1701_DSPCTRL
:
127 case ADAU1701_SEROCTL
:
128 case ADAU1701_AUXNPOW
:
129 case ADAU1701_OSCIPOW
:
130 case ADAU1701_DACSET
:
132 case ADAU1701_SERICTL
:
136 dev_err(codec
->dev
, "Unsupported register address: %d\n", reg
);
140 static int adau1701_write(struct snd_soc_codec
*codec
, unsigned int reg
,
148 size
= adau1701_register_size(codec
, reg
);
152 snd_soc_cache_write(codec
, reg
, value
);
157 for (i
= size
+ 1; i
>= 2; --i
) {
162 ret
= i2c_master_send(to_i2c_client(codec
->dev
), buf
, size
+ 2);
171 static unsigned int adau1701_read(struct snd_soc_codec
*codec
, unsigned int reg
)
176 ret
= snd_soc_cache_read(codec
, reg
, &value
);
183 static int adau1701_load_firmware(struct snd_soc_codec
*codec
)
185 return process_sigma_firmware(codec
->control_data
, ADAU1701_FIRMWARE
);
188 static int adau1701_set_capture_pcm_format(struct snd_soc_codec
*codec
,
189 snd_pcm_format_t format
)
191 struct adau1701
*adau1701
= snd_soc_codec_get_drvdata(codec
);
192 unsigned int mask
= ADAU1701_SEROCTL_WORD_LEN_MASK
;
196 case SNDRV_PCM_FORMAT_S16_LE
:
197 val
= ADAU1701_SEROCTL_WORD_LEN_16
;
199 case SNDRV_PCM_FORMAT_S20_3LE
:
200 val
= ADAU1701_SEROCTL_WORD_LEN_20
;
202 case SNDRV_PCM_FORMAT_S24_LE
:
203 val
= ADAU1701_SEROCTL_WORD_LEN_24
;
209 if (adau1701
->dai_fmt
== SND_SOC_DAIFMT_RIGHT_J
) {
211 case SNDRV_PCM_FORMAT_S16_LE
:
212 val
|= ADAU1701_SEROCTL_MSB_DEALY16
;
214 case SNDRV_PCM_FORMAT_S20_3LE
:
215 val
|= ADAU1701_SEROCTL_MSB_DEALY12
;
217 case SNDRV_PCM_FORMAT_S24_LE
:
218 val
|= ADAU1701_SEROCTL_MSB_DEALY8
;
221 mask
|= ADAU1701_SEROCTL_MSB_DEALY_MASK
;
224 snd_soc_update_bits(codec
, ADAU1701_SEROCTL
, mask
, val
);
229 static int adau1701_set_playback_pcm_format(struct snd_soc_codec
*codec
,
230 snd_pcm_format_t format
)
232 struct adau1701
*adau1701
= snd_soc_codec_get_drvdata(codec
);
235 if (adau1701
->dai_fmt
!= SND_SOC_DAIFMT_RIGHT_J
)
239 case SNDRV_PCM_FORMAT_S16_LE
:
240 val
= ADAU1701_SERICTL_RIGHTJ_16
;
242 case SNDRV_PCM_FORMAT_S20_3LE
:
243 val
= ADAU1701_SERICTL_RIGHTJ_20
;
245 case SNDRV_PCM_FORMAT_S24_LE
:
246 val
= ADAU1701_SERICTL_RIGHTJ_24
;
252 snd_soc_update_bits(codec
, ADAU1701_SERICTL
,
253 ADAU1701_SERICTL_MODE_MASK
, val
);
258 static int adau1701_hw_params(struct snd_pcm_substream
*substream
,
259 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
261 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
262 struct snd_soc_codec
*codec
= rtd
->codec
;
263 snd_pcm_format_t format
;
266 switch (params_rate(params
)) {
268 val
= ADAU1701_DSPCTRL_SR_192
;
271 val
= ADAU1701_DSPCTRL_SR_96
;
274 val
= ADAU1701_DSPCTRL_SR_48
;
280 snd_soc_update_bits(codec
, ADAU1701_DSPCTRL
,
281 ADAU1701_DSPCTRL_SR_MASK
, val
);
283 format
= params_format(params
);
284 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
285 return adau1701_set_playback_pcm_format(codec
, format
);
287 return adau1701_set_capture_pcm_format(codec
, format
);
290 static int adau1701_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
293 struct snd_soc_codec
*codec
= codec_dai
->codec
;
294 struct adau1701
*adau1701
= snd_soc_codec_get_drvdata(codec
);
295 unsigned int serictl
= 0x00, seroctl
= 0x00;
298 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
299 case SND_SOC_DAIFMT_CBM_CFM
:
300 /* master, 64-bits per sample, 1 frame per sample */
301 seroctl
|= ADAU1701_SEROCTL_MASTER
| ADAU1701_SEROCTL_OBF16
302 | ADAU1701_SEROCTL_OLF1024
;
304 case SND_SOC_DAIFMT_CBS_CFS
:
310 /* clock inversion */
311 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
312 case SND_SOC_DAIFMT_NB_NF
:
313 invert_lrclk
= false;
315 case SND_SOC_DAIFMT_NB_IF
:
318 case SND_SOC_DAIFMT_IB_NF
:
319 invert_lrclk
= false;
320 serictl
|= ADAU1701_SERICTL_INV_BCLK
;
321 seroctl
|= ADAU1701_SEROCTL_INV_BCLK
;
323 case SND_SOC_DAIFMT_IB_IF
:
325 serictl
|= ADAU1701_SERICTL_INV_BCLK
;
326 seroctl
|= ADAU1701_SEROCTL_INV_BCLK
;
332 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
333 case SND_SOC_DAIFMT_I2S
:
335 case SND_SOC_DAIFMT_LEFT_J
:
336 serictl
|= ADAU1701_SERICTL_LEFTJ
;
337 seroctl
|= ADAU1701_SEROCTL_MSB_DEALY0
;
338 invert_lrclk
= !invert_lrclk
;
340 case SND_SOC_DAIFMT_RIGHT_J
:
341 serictl
|= ADAU1701_SERICTL_RIGHTJ_24
;
342 seroctl
|= ADAU1701_SEROCTL_MSB_DEALY8
;
343 invert_lrclk
= !invert_lrclk
;
350 seroctl
|= ADAU1701_SEROCTL_INV_LRCLK
;
351 serictl
|= ADAU1701_SERICTL_INV_LRCLK
;
354 adau1701
->dai_fmt
= fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
356 snd_soc_write(codec
, ADAU1701_SERICTL
, serictl
);
357 snd_soc_update_bits(codec
, ADAU1701_SEROCTL
,
358 ~ADAU1701_SEROCTL_WORD_LEN_MASK
, seroctl
);
363 static int adau1701_set_bias_level(struct snd_soc_codec
*codec
,
364 enum snd_soc_bias_level level
)
366 unsigned int mask
= ADAU1701_AUXNPOW_VBPD
| ADAU1701_AUXNPOW_VRPD
;
369 case SND_SOC_BIAS_ON
:
371 case SND_SOC_BIAS_PREPARE
:
373 case SND_SOC_BIAS_STANDBY
:
374 /* Enable VREF and VREF buffer */
375 snd_soc_update_bits(codec
, ADAU1701_AUXNPOW
, mask
, 0x00);
377 case SND_SOC_BIAS_OFF
:
378 /* Disable VREF and VREF buffer */
379 snd_soc_update_bits(codec
, ADAU1701_AUXNPOW
, mask
, mask
);
383 codec
->dapm
.bias_level
= level
;
387 static int adau1701_digital_mute(struct snd_soc_dai
*dai
, int mute
)
389 struct snd_soc_codec
*codec
= dai
->codec
;
390 unsigned int mask
= ADAU1701_DSPCTRL_DAM
;
398 snd_soc_update_bits(codec
, ADAU1701_DSPCTRL
, mask
, val
);
403 static int adau1701_set_sysclk(struct snd_soc_codec
*codec
, int clk_id
,
404 unsigned int freq
, int dir
)
409 case ADAU1701_CLK_SRC_OSC
:
412 case ADAU1701_CLK_SRC_MCLK
:
413 val
= ADAU1701_OSCIPOW_OPD
;
419 snd_soc_update_bits(codec
, ADAU1701_OSCIPOW
, ADAU1701_OSCIPOW_OPD
, val
);
424 #define ADAU1701_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
425 SNDRV_PCM_RATE_192000)
427 #define ADAU1701_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
428 SNDRV_PCM_FMTBIT_S24_LE)
430 static const struct snd_soc_dai_ops adau1701_dai_ops
= {
431 .set_fmt
= adau1701_set_dai_fmt
,
432 .hw_params
= adau1701_hw_params
,
433 .digital_mute
= adau1701_digital_mute
,
436 static struct snd_soc_dai_driver adau1701_dai
= {
439 .stream_name
= "Playback",
442 .rates
= ADAU1701_RATES
,
443 .formats
= ADAU1701_FORMATS
,
446 .stream_name
= "Capture",
449 .rates
= ADAU1701_RATES
,
450 .formats
= ADAU1701_FORMATS
,
452 .ops
= &adau1701_dai_ops
,
453 .symmetric_rates
= 1,
456 static int adau1701_probe(struct snd_soc_codec
*codec
)
460 codec
->dapm
.idle_bias_off
= 1;
462 ret
= adau1701_load_firmware(codec
);
464 dev_warn(codec
->dev
, "Failed to load firmware\n");
466 snd_soc_write(codec
, ADAU1701_DACSET
, ADAU1701_DACSET_DACINIT
);
467 snd_soc_write(codec
, ADAU1701_DSPCTRL
, ADAU1701_DSPCTRL_CR
);
472 static struct snd_soc_codec_driver adau1701_codec_drv
= {
473 .probe
= adau1701_probe
,
474 .set_bias_level
= adau1701_set_bias_level
,
476 .reg_cache_size
= ADAU1701_NUM_REGS
,
477 .reg_word_size
= sizeof(u16
),
479 .controls
= adau1701_controls
,
480 .num_controls
= ARRAY_SIZE(adau1701_controls
),
481 .dapm_widgets
= adau1701_dapm_widgets
,
482 .num_dapm_widgets
= ARRAY_SIZE(adau1701_dapm_widgets
),
483 .dapm_routes
= adau1701_dapm_routes
,
484 .num_dapm_routes
= ARRAY_SIZE(adau1701_dapm_routes
),
486 .write
= adau1701_write
,
487 .read
= adau1701_read
,
489 .set_sysclk
= adau1701_set_sysclk
,
492 static __devinit
int adau1701_i2c_probe(struct i2c_client
*client
,
493 const struct i2c_device_id
*id
)
495 struct adau1701
*adau1701
;
498 adau1701
= kzalloc(sizeof(*adau1701
), GFP_KERNEL
);
502 i2c_set_clientdata(client
, adau1701
);
503 ret
= snd_soc_register_codec(&client
->dev
, &adau1701_codec_drv
,
511 static __devexit
int adau1701_i2c_remove(struct i2c_client
*client
)
513 snd_soc_unregister_codec(&client
->dev
);
514 kfree(i2c_get_clientdata(client
));
518 static const struct i2c_device_id adau1701_i2c_id
[] = {
522 MODULE_DEVICE_TABLE(i2c
, adau1701_i2c_id
);
524 static struct i2c_driver adau1701_i2c_driver
= {
527 .owner
= THIS_MODULE
,
529 .probe
= adau1701_i2c_probe
,
530 .remove
= __devexit_p(adau1701_i2c_remove
),
531 .id_table
= adau1701_i2c_id
,
534 static int __init
adau1701_init(void)
536 return i2c_add_driver(&adau1701_i2c_driver
);
538 module_init(adau1701_init
);
540 static void __exit
adau1701_exit(void)
542 i2c_del_driver(&adau1701_i2c_driver
);
544 module_exit(adau1701_exit
);
546 MODULE_DESCRIPTION("ASoC ADAU1701 SigmaDSP driver");
547 MODULE_AUTHOR("Cliff Cai <cliff.cai@analog.com>");
548 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
549 MODULE_LICENSE("GPL");