2 * DA7210 ALSA Soc codec driver
4 * Copyright (c) 2009 Dialog Semiconductor
5 * Written by David Chen <Dajun.chen@diasemi.com>
7 * Copyright (C) 2009 Renesas Solutions Corp.
8 * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/platform_device.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
31 #include <sound/tlv.h>
32 #include <sound/initval.h>
33 #include <asm/div64.h>
37 /* DA7210 register space */
38 #define DA7210_STATUS 0x02
39 #define DA7210_STARTUP1 0x03
40 #define DA7210_MIC_L 0x07
41 #define DA7210_MIC_R 0x08
42 #define DA7210_INMIX_L 0x0D
43 #define DA7210_INMIX_R 0x0E
44 #define DA7210_ADC_HPF 0x0F
45 #define DA7210_ADC 0x10
46 #define DA7210_DAC_HPF 0x14
47 #define DA7210_DAC_L 0x15
48 #define DA7210_DAC_R 0x16
49 #define DA7210_DAC_SEL 0x17
50 #define DA7210_OUTMIX_L 0x1C
51 #define DA7210_OUTMIX_R 0x1D
52 #define DA7210_HP_L_VOL 0x21
53 #define DA7210_HP_R_VOL 0x22
54 #define DA7210_HP_CFG 0x23
55 #define DA7210_DAI_SRC_SEL 0x25
56 #define DA7210_DAI_CFG1 0x26
57 #define DA7210_DAI_CFG3 0x28
58 #define DA7210_PLL_DIV3 0x2B
59 #define DA7210_PLL 0x2C
61 /* STARTUP1 bit fields */
62 #define DA7210_SC_MST_EN (1 << 0)
64 /* MIC_L bit fields */
65 #define DA7210_MICBIAS_EN (1 << 6)
66 #define DA7210_MIC_L_EN (1 << 7)
68 /* MIC_R bit fields */
69 #define DA7210_MIC_R_EN (1 << 7)
71 /* INMIX_L bit fields */
72 #define DA7210_IN_L_EN (1 << 7)
74 /* INMIX_R bit fields */
75 #define DA7210_IN_R_EN (1 << 7)
77 /* ADC_HPF bit fields */
78 #define DA7210_ADC_VOICE_EN (1 << 7)
81 #define DA7210_ADC_L_EN (1 << 3)
82 #define DA7210_ADC_R_EN (1 << 7)
85 #define DA7210_DAC_VOICE_EN (1 << 7)
87 /* DAC_SEL bit fields */
88 #define DA7210_DAC_L_SRC_DAI_L (4 << 0)
89 #define DA7210_DAC_L_EN (1 << 3)
90 #define DA7210_DAC_R_SRC_DAI_R (5 << 4)
91 #define DA7210_DAC_R_EN (1 << 7)
93 /* OUTMIX_L bit fields */
94 #define DA7210_OUT_L_EN (1 << 7)
96 /* OUTMIX_R bit fields */
97 #define DA7210_OUT_R_EN (1 << 7)
99 /* HP_CFG bit fields */
100 #define DA7210_HP_2CAP_MODE (1 << 1)
101 #define DA7210_HP_SENSE_EN (1 << 2)
102 #define DA7210_HP_L_EN (1 << 3)
103 #define DA7210_HP_MODE (1 << 6)
104 #define DA7210_HP_R_EN (1 << 7)
106 /* DAI_SRC_SEL bit fields */
107 #define DA7210_DAI_OUT_L_SRC (6 << 0)
108 #define DA7210_DAI_OUT_R_SRC (7 << 4)
110 /* DAI_CFG1 bit fields */
111 #define DA7210_DAI_WORD_S16_LE (0 << 0)
112 #define DA7210_DAI_WORD_S24_LE (2 << 0)
113 #define DA7210_DAI_FLEN_64BIT (1 << 2)
114 #define DA7210_DAI_MODE_MASTER (1 << 7)
116 /* DAI_CFG3 bit fields */
117 #define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
118 #define DA7210_DAI_OE (1 << 3)
119 #define DA7210_DAI_EN (1 << 7)
121 /*PLL_DIV3 bit fields */
122 #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
123 #define DA7210_PLL_BYP (1 << 6)
126 #define DA7210_PLL_FS_48000 (11 << 0)
128 #define DA7210_VERSION "0.0.1"
130 /* Codec private data */
132 struct snd_soc_codec codec
;
135 static struct snd_soc_codec
*da7210_codec
;
140 static const u8 da7210_reg
[] = {
141 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
143 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
144 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
145 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
146 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
147 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
148 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
157 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
162 * Read da7210 register cache
164 static inline u32
da7210_read_reg_cache(struct snd_soc_codec
*codec
, u32 reg
)
166 u8
*cache
= codec
->reg_cache
;
167 BUG_ON(reg
> ARRAY_SIZE(da7210_reg
));
172 * Write to the da7210 register space
174 static int da7210_write(struct snd_soc_codec
*codec
, u32 reg
, u32 value
)
176 u8
*cache
= codec
->reg_cache
;
179 BUG_ON(codec
->volatile_register
);
181 data
[0] = reg
& 0xff;
182 data
[1] = value
& 0xff;
184 if (reg
>= codec
->reg_cache_size
)
187 if (2 != codec
->hw_write(codec
->control_data
, data
, 2))
195 * Read from the da7210 register space.
197 static inline u32
da7210_read(struct snd_soc_codec
*codec
, u32 reg
)
199 if (DA7210_STATUS
== reg
)
200 return i2c_smbus_read_byte_data(codec
->control_data
, reg
);
202 return da7210_read_reg_cache(codec
, reg
);
205 static int da7210_startup(struct snd_pcm_substream
*substream
,
206 struct snd_soc_dai
*dai
)
208 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
209 struct snd_soc_codec
*codec
= dai
->codec
;
212 /* PlayBack Volume 40 */
213 snd_soc_update_bits(codec
, DA7210_HP_L_VOL
, 0x3F, 40);
214 snd_soc_update_bits(codec
, DA7210_HP_R_VOL
, 0x3F, 40);
217 snd_soc_update_bits(codec
, DA7210_OUTMIX_L
, 0x1F, 0x10);
218 snd_soc_update_bits(codec
, DA7210_OUTMIX_R
, 0x1F, 0x10);
222 snd_soc_update_bits(codec
, DA7210_MIC_L
, 0x7, 0x7);
223 snd_soc_update_bits(codec
, DA7210_MIC_R
, 0x7, 0x7);
226 snd_soc_update_bits(codec
, DA7210_INMIX_L
, 0x1F, 0x1);
227 snd_soc_update_bits(codec
, DA7210_INMIX_R
, 0x1F, 0x1);
234 * Set PCM DAI word length.
236 static int da7210_hw_params(struct snd_pcm_substream
*substream
,
237 struct snd_pcm_hw_params
*params
,
238 struct snd_soc_dai
*dai
)
240 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
241 struct snd_soc_device
*socdev
= rtd
->socdev
;
242 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
246 /* set DAI source to Left and Right ADC */
247 da7210_write(codec
, DA7210_DAI_SRC_SEL
,
248 DA7210_DAI_OUT_R_SRC
| DA7210_DAI_OUT_L_SRC
);
251 da7210_write(codec
, DA7210_DAI_CFG3
, DA7210_DAI_OE
| DA7210_DAI_EN
);
253 dai_cfg1
= 0xFC & da7210_read(codec
, DA7210_DAI_CFG1
);
255 switch (params_format(params
)) {
256 case SNDRV_PCM_FORMAT_S16_LE
:
257 dai_cfg1
|= DA7210_DAI_WORD_S16_LE
;
259 case SNDRV_PCM_FORMAT_S24_LE
:
260 dai_cfg1
|= DA7210_DAI_WORD_S24_LE
;
266 da7210_write(codec
, DA7210_DAI_CFG1
, dai_cfg1
);
270 * It support 48K only now
272 switch (params_rate(params
)) {
274 if (SNDRV_PCM_STREAM_PLAYBACK
== substream
->stream
) {
275 reg
= DA7210_DAC_HPF
;
276 mask
= DA7210_DAC_VOICE_EN
;
278 reg
= DA7210_ADC_HPF
;
279 mask
= DA7210_ADC_VOICE_EN
;
286 snd_soc_update_bits(codec
, reg
, mask
, 0);
292 * Set DAI mode and Format
294 static int da7210_set_dai_fmt(struct snd_soc_dai
*codec_dai
, u32 fmt
)
296 struct snd_soc_codec
*codec
= codec_dai
->codec
;
300 dai_cfg1
= 0x7f & da7210_read(codec
, DA7210_DAI_CFG1
);
301 dai_cfg3
= 0xfc & da7210_read(codec
, DA7210_DAI_CFG3
);
303 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
304 case SND_SOC_DAIFMT_CBM_CFM
:
305 dai_cfg1
|= DA7210_DAI_MODE_MASTER
;
313 * It support I2S only now
315 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
316 case SND_SOC_DAIFMT_I2S
:
317 dai_cfg3
|= DA7210_DAI_FORMAT_I2SMODE
;
325 * It support 64bit data transmission only now
327 dai_cfg1
|= DA7210_DAI_FLEN_64BIT
;
329 da7210_write(codec
, DA7210_DAI_CFG1
, dai_cfg1
);
330 da7210_write(codec
, DA7210_DAI_CFG3
, dai_cfg3
);
335 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
338 static struct snd_soc_dai_ops da7210_dai_ops
= {
339 .startup
= da7210_startup
,
340 .hw_params
= da7210_hw_params
,
341 .set_fmt
= da7210_set_dai_fmt
,
344 struct snd_soc_dai da7210_dai
= {
345 .name
= "DA7210 IIS",
347 /* playback capabilities */
349 .stream_name
= "Playback",
352 .rates
= SNDRV_PCM_RATE_8000_96000
,
353 .formats
= DA7210_FORMATS
,
355 /* capture capabilities */
357 .stream_name
= "Capture",
360 .rates
= SNDRV_PCM_RATE_8000_96000
,
361 .formats
= DA7210_FORMATS
,
363 .ops
= &da7210_dai_ops
,
365 EXPORT_SYMBOL_GPL(da7210_dai
);
368 * Initialize the DA7210 driver
369 * register the mixer and dsp interfaces with the kernel
371 static int da7210_init(struct da7210_priv
*da7210
)
373 struct snd_soc_codec
*codec
= &da7210
->codec
;
377 dev_err(codec
->dev
, "Another da7210 is registered\n");
381 mutex_init(&codec
->mutex
);
382 INIT_LIST_HEAD(&codec
->dapm_widgets
);
383 INIT_LIST_HEAD(&codec
->dapm_paths
);
385 codec
->private_data
= da7210
;
386 codec
->name
= "DA7210";
387 codec
->owner
= THIS_MODULE
;
388 codec
->read
= da7210_read
;
389 codec
->write
= da7210_write
;
390 codec
->dai
= &da7210_dai
;
392 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
393 codec
->reg_cache_size
= ARRAY_SIZE(da7210_reg
);
394 codec
->reg_cache
= kmemdup(da7210_reg
,
395 sizeof(da7210_reg
), GFP_KERNEL
);
397 if (!codec
->reg_cache
)
400 da7210_dai
.dev
= codec
->dev
;
401 da7210_codec
= codec
;
403 ret
= snd_soc_register_codec(codec
);
405 dev_err(codec
->dev
, "Failed to register CODEC: %d\n", ret
);
409 ret
= snd_soc_register_dai(&da7210_dai
);
411 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
417 * This driver use fixed value here
424 /* Enable Left & Right MIC PGA and Mic Bias */
425 da7210_write(codec
, DA7210_MIC_L
, DA7210_MIC_L_EN
| DA7210_MICBIAS_EN
);
426 da7210_write(codec
, DA7210_MIC_R
, DA7210_MIC_R_EN
);
428 /* Enable Left and Right input PGA */
429 da7210_write(codec
, DA7210_INMIX_L
, DA7210_IN_L_EN
);
430 da7210_write(codec
, DA7210_INMIX_R
, DA7210_IN_R_EN
);
432 /* Enable Left and Right ADC */
433 da7210_write(codec
, DA7210_ADC
, DA7210_ADC_L_EN
| DA7210_ADC_R_EN
);
439 /* Enable Left and Right DAC */
440 da7210_write(codec
, DA7210_DAC_SEL
,
441 DA7210_DAC_L_SRC_DAI_L
| DA7210_DAC_L_EN
|
442 DA7210_DAC_R_SRC_DAI_R
| DA7210_DAC_R_EN
);
444 /* Enable Left and Right out PGA */
445 da7210_write(codec
, DA7210_OUTMIX_L
, DA7210_OUT_L_EN
);
446 da7210_write(codec
, DA7210_OUTMIX_R
, DA7210_OUT_R_EN
);
448 /* Enable Left and Right HeadPhone PGA */
449 da7210_write(codec
, DA7210_HP_CFG
,
450 DA7210_HP_2CAP_MODE
| DA7210_HP_SENSE_EN
|
451 DA7210_HP_L_EN
| DA7210_HP_MODE
| DA7210_HP_R_EN
);
453 /* Diable PLL and bypass it */
454 da7210_write(codec
, DA7210_PLL
, DA7210_PLL_FS_48000
);
456 /* Bypass PLL and set MCLK freq rang to 10-20MHz */
457 da7210_write(codec
, DA7210_PLL_DIV3
,
458 DA7210_MCLK_RANGE_10_20_MHZ
| DA7210_PLL_BYP
);
460 /* Activate all enabled subsystem */
461 da7210_write(codec
, DA7210_STARTUP1
, DA7210_SC_MST_EN
);
466 kfree(codec
->reg_cache
);
467 codec
->reg_cache
= NULL
;
473 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
474 static int __devinit
da7210_i2c_probe(struct i2c_client
*i2c
,
475 const struct i2c_device_id
*id
)
477 struct da7210_priv
*da7210
;
478 struct snd_soc_codec
*codec
;
481 da7210
= kzalloc(sizeof(struct da7210_priv
), GFP_KERNEL
);
485 codec
= &da7210
->codec
;
486 codec
->dev
= &i2c
->dev
;
488 i2c_set_clientdata(i2c
, da7210
);
489 codec
->control_data
= i2c
;
491 ret
= da7210_init(da7210
);
493 pr_err("Failed to initialise da7210 audio codec\n");
498 static int __devexit
da7210_i2c_remove(struct i2c_client
*client
)
500 struct da7210_priv
*da7210
= i2c_get_clientdata(client
);
502 snd_soc_unregister_dai(&da7210_dai
);
503 kfree(da7210
->codec
.reg_cache
);
510 static const struct i2c_device_id da7210_i2c_id
[] = {
514 MODULE_DEVICE_TABLE(i2c
, da7210_i2c_id
);
516 /* I2C codec control layer */
517 static struct i2c_driver da7210_i2c_driver
= {
519 .name
= "DA7210 I2C Codec",
520 .owner
= THIS_MODULE
,
522 .probe
= da7210_i2c_probe
,
523 .remove
= __devexit_p(da7210_i2c_remove
),
524 .id_table
= da7210_i2c_id
,
528 static int da7210_probe(struct platform_device
*pdev
)
530 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
531 struct snd_soc_codec
*codec
;
535 dev_err(&pdev
->dev
, "Codec device not registered\n");
539 socdev
->card
->codec
= da7210_codec
;
540 codec
= da7210_codec
;
543 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
547 dev_info(&pdev
->dev
, "DA7210 Audio Codec %s\n", DA7210_VERSION
);
553 static int da7210_remove(struct platform_device
*pdev
)
555 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
557 snd_soc_free_pcms(socdev
);
558 snd_soc_dapm_free(socdev
);
563 struct snd_soc_codec_device soc_codec_dev_da7210
= {
564 .probe
= da7210_probe
,
565 .remove
= da7210_remove
,
567 EXPORT_SYMBOL_GPL(soc_codec_dev_da7210
);
569 static int __init
da7210_modinit(void)
572 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
573 ret
= i2c_add_driver(&da7210_i2c_driver
);
577 module_init(da7210_modinit
);
579 static void __exit
da7210_exit(void)
581 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
582 i2c_del_driver(&da7210_i2c_driver
);
585 module_exit(da7210_exit
);
587 MODULE_DESCRIPTION("ASoC DA7210 driver");
588 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
589 MODULE_LICENSE("GPL");