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)
84 /* DAC_SEL bit fields */
85 #define DA7210_DAC_L_SRC_DAI_L (4 << 0)
86 #define DA7210_DAC_L_EN (1 << 3)
87 #define DA7210_DAC_R_SRC_DAI_R (5 << 4)
88 #define DA7210_DAC_R_EN (1 << 7)
90 /* OUTMIX_L bit fields */
91 #define DA7210_OUT_L_EN (1 << 7)
93 /* OUTMIX_R bit fields */
94 #define DA7210_OUT_R_EN (1 << 7)
96 /* HP_CFG bit fields */
97 #define DA7210_HP_2CAP_MODE (1 << 1)
98 #define DA7210_HP_SENSE_EN (1 << 2)
99 #define DA7210_HP_L_EN (1 << 3)
100 #define DA7210_HP_MODE (1 << 6)
101 #define DA7210_HP_R_EN (1 << 7)
103 /* DAI_SRC_SEL bit fields */
104 #define DA7210_DAI_OUT_L_SRC (6 << 0)
105 #define DA7210_DAI_OUT_R_SRC (7 << 4)
107 /* DAI_CFG1 bit fields */
108 #define DA7210_DAI_WORD_S16_LE (0 << 0)
109 #define DA7210_DAI_WORD_S24_LE (2 << 0)
110 #define DA7210_DAI_FLEN_64BIT (1 << 2)
111 #define DA7210_DAI_MODE_MASTER (1 << 7)
113 /* DAI_CFG3 bit fields */
114 #define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
115 #define DA7210_DAI_OE (1 << 3)
116 #define DA7210_DAI_EN (1 << 7)
118 /*PLL_DIV3 bit fields */
119 #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
120 #define DA7210_PLL_BYP (1 << 6)
123 #define DA7210_PLL_FS_48000 (11 << 0)
125 #define DA7210_VERSION "0.0.1"
127 /* Codec private data */
129 struct snd_soc_codec codec
;
132 static struct snd_soc_codec
*da7210_codec
;
137 static const u8 da7210_reg
[] = {
138 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
140 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
141 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
142 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
143 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
144 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
145 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
146 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
154 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
159 * Read da7210 register cache
161 static inline u32
da7210_read_reg_cache(struct snd_soc_codec
*codec
, u32 reg
)
163 u8
*cache
= codec
->reg_cache
;
164 BUG_ON(reg
> ARRAY_SIZE(da7210_reg
));
169 * Write to the da7210 register space
171 static int da7210_write(struct snd_soc_codec
*codec
, u32 reg
, u32 value
)
173 u8
*cache
= codec
->reg_cache
;
176 BUG_ON(codec
->volatile_register
);
178 data
[0] = reg
& 0xff;
179 data
[1] = value
& 0xff;
181 if (reg
>= codec
->reg_cache_size
)
184 if (2 != codec
->hw_write(codec
->control_data
, data
, 2))
192 * Read from the da7210 register space.
194 static inline u32
da7210_read(struct snd_soc_codec
*codec
, u32 reg
)
196 if (DA7210_STATUS
== reg
)
197 return i2c_smbus_read_byte_data(codec
->control_data
, reg
);
199 return da7210_read_reg_cache(codec
, reg
);
202 static int da7210_startup(struct snd_pcm_substream
*substream
,
203 struct snd_soc_dai
*dai
)
205 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
206 struct snd_soc_codec
*codec
= dai
->codec
;
209 /* PlayBack Volume 40 */
210 snd_soc_update_bits(codec
, DA7210_HP_L_VOL
, 0x3F, 40);
211 snd_soc_update_bits(codec
, DA7210_HP_R_VOL
, 0x3F, 40);
214 snd_soc_update_bits(codec
, DA7210_OUTMIX_L
, 0x1F, 0x10);
215 snd_soc_update_bits(codec
, DA7210_OUTMIX_R
, 0x1F, 0x10);
219 snd_soc_update_bits(codec
, DA7210_MIC_L
, 0x7, 0x7);
220 snd_soc_update_bits(codec
, DA7210_MIC_R
, 0x7, 0x7);
223 snd_soc_update_bits(codec
, DA7210_INMIX_L
, 0x1F, 0x1);
224 snd_soc_update_bits(codec
, DA7210_INMIX_R
, 0x1F, 0x1);
231 * Set PCM DAI word length.
233 static int da7210_hw_params(struct snd_pcm_substream
*substream
,
234 struct snd_pcm_hw_params
*params
,
235 struct snd_soc_dai
*dai
)
237 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
238 struct snd_soc_device
*socdev
= rtd
->socdev
;
239 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
243 /* set DAI source to Left and Right ADC */
244 da7210_write(codec
, DA7210_DAI_SRC_SEL
,
245 DA7210_DAI_OUT_R_SRC
| DA7210_DAI_OUT_L_SRC
);
248 da7210_write(codec
, DA7210_DAI_CFG3
, DA7210_DAI_OE
| DA7210_DAI_EN
);
250 dai_cfg1
= 0xFC & da7210_read(codec
, DA7210_DAI_CFG1
);
252 switch (params_format(params
)) {
253 case SNDRV_PCM_FORMAT_S16_LE
:
254 dai_cfg1
|= DA7210_DAI_WORD_S16_LE
;
256 case SNDRV_PCM_FORMAT_S24_LE
:
257 dai_cfg1
|= DA7210_DAI_WORD_S24_LE
;
263 da7210_write(codec
, DA7210_DAI_CFG1
, dai_cfg1
);
267 * It support 48K only now
269 switch (params_rate(params
)) {
271 if (SNDRV_PCM_STREAM_PLAYBACK
== substream
->stream
) {
272 reg
= DA7210_DAC_HPF
;
273 mask
= DA7210_DAC_VOICE_EN
;
275 reg
= DA7210_ADC_HPF
;
276 mask
= DA7210_ADC_VOICE_EN
;
283 snd_soc_update_bits(codec
, reg
, mask
, 0);
289 * Set DAI mode and Format
291 static int da7210_set_dai_fmt(struct snd_soc_dai
*codec_dai
, u32 fmt
)
293 struct snd_soc_codec
*codec
= codec_dai
->codec
;
297 dai_cfg1
= 0x7f & da7210_read(codec
, DA7210_DAI_CFG1
);
298 dai_cfg3
= 0xfc & da7210_read(codec
, DA7210_DAI_CFG3
);
300 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
301 case SND_SOC_DAIFMT_CBM_CFM
:
302 dai_cfg1
|= DA7210_DAI_MODE_MASTER
;
310 * It support I2S only now
312 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
313 case SND_SOC_DAIFMT_I2S
:
314 dai_cfg3
|= DA7210_DAI_FORMAT_I2SMODE
;
322 * It support 64bit data transmission only now
324 dai_cfg1
|= DA7210_DAI_FLEN_64BIT
;
326 da7210_write(codec
, DA7210_DAI_CFG1
, dai_cfg1
);
327 da7210_write(codec
, DA7210_DAI_CFG3
, dai_cfg3
);
332 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
335 static struct snd_soc_dai_ops da7210_dai_ops
= {
336 .startup
= da7210_startup
,
337 .hw_params
= da7210_hw_params
,
338 .set_fmt
= da7210_set_dai_fmt
,
341 struct snd_soc_dai da7210_dai
= {
342 .name
= "DA7210 IIS",
344 /* playback capabilities */
346 .stream_name
= "Playback",
349 .rates
= SNDRV_PCM_RATE_8000_96000
,
350 .formats
= DA7210_FORMATS
,
352 /* capture capabilities */
354 .stream_name
= "Capture",
357 .rates
= SNDRV_PCM_RATE_8000_96000
,
358 .formats
= DA7210_FORMATS
,
360 .ops
= &da7210_dai_ops
,
362 EXPORT_SYMBOL_GPL(da7210_dai
);
365 * Initialize the DA7210 driver
366 * register the mixer and dsp interfaces with the kernel
368 static int da7210_init(struct da7210_priv
*da7210
)
370 struct snd_soc_codec
*codec
= &da7210
->codec
;
374 dev_err(codec
->dev
, "Another da7210 is registered\n");
378 mutex_init(&codec
->mutex
);
379 INIT_LIST_HEAD(&codec
->dapm_widgets
);
380 INIT_LIST_HEAD(&codec
->dapm_paths
);
382 codec
->private_data
= da7210
;
383 codec
->name
= "DA7210";
384 codec
->owner
= THIS_MODULE
;
385 codec
->read
= da7210_read
;
386 codec
->write
= da7210_write
;
387 codec
->dai
= &da7210_dai
;
389 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
390 codec
->reg_cache_size
= ARRAY_SIZE(da7210_reg
);
391 codec
->reg_cache
= kmemdup(da7210_reg
,
392 sizeof(da7210_reg
), GFP_KERNEL
);
394 if (!codec
->reg_cache
)
397 da7210_dai
.dev
= codec
->dev
;
398 da7210_codec
= codec
;
400 ret
= snd_soc_register_codec(codec
);
402 dev_err(codec
->dev
, "Failed to register CODEC: %d\n", ret
);
406 ret
= snd_soc_register_dai(&da7210_dai
);
408 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
414 * This driver use fixed value here
421 /* Enable Left & Right MIC PGA and Mic Bias */
422 da7210_write(codec
, DA7210_MIC_L
, DA7210_MIC_L_EN
| DA7210_MICBIAS_EN
);
423 da7210_write(codec
, DA7210_MIC_R
, DA7210_MIC_R_EN
);
425 /* Enable Left and Right input PGA */
426 da7210_write(codec
, DA7210_INMIX_L
, DA7210_IN_L_EN
);
427 da7210_write(codec
, DA7210_INMIX_R
, DA7210_IN_R_EN
);
429 /* Enable Left and Right ADC */
430 da7210_write(codec
, DA7210_ADC
, DA7210_ADC_L_EN
| DA7210_ADC_R_EN
);
436 /* Enable Left and Right DAC */
437 da7210_write(codec
, DA7210_DAC_SEL
,
438 DA7210_DAC_L_SRC_DAI_L
| DA7210_DAC_L_EN
|
439 DA7210_DAC_R_SRC_DAI_R
| DA7210_DAC_R_EN
);
441 /* Enable Left and Right out PGA */
442 da7210_write(codec
, DA7210_OUTMIX_L
, DA7210_OUT_L_EN
);
443 da7210_write(codec
, DA7210_OUTMIX_R
, DA7210_OUT_R_EN
);
445 /* Enable Left and Right HeadPhone PGA */
446 da7210_write(codec
, DA7210_HP_CFG
,
447 DA7210_HP_2CAP_MODE
| DA7210_HP_SENSE_EN
|
448 DA7210_HP_L_EN
| DA7210_HP_MODE
| DA7210_HP_R_EN
);
450 /* Diable PLL and bypass it */
451 da7210_write(codec
, DA7210_PLL
, DA7210_PLL_FS_48000
);
453 /* Bypass PLL and set MCLK freq rang to 10-20MHz */
454 da7210_write(codec
, DA7210_PLL_DIV3
,
455 DA7210_MCLK_RANGE_10_20_MHZ
| DA7210_PLL_BYP
);
457 /* Activate all enabled subsystem */
458 da7210_write(codec
, DA7210_STARTUP1
, DA7210_SC_MST_EN
);
463 kfree(codec
->reg_cache
);
464 codec
->reg_cache
= NULL
;
470 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
471 static int da7210_i2c_probe(struct i2c_client
*i2c
,
472 const struct i2c_device_id
*id
)
474 struct da7210_priv
*da7210
;
475 struct snd_soc_codec
*codec
;
478 da7210
= kzalloc(sizeof(struct da7210_priv
), GFP_KERNEL
);
482 codec
= &da7210
->codec
;
483 codec
->dev
= &i2c
->dev
;
485 i2c_set_clientdata(i2c
, da7210
);
486 codec
->control_data
= i2c
;
488 ret
= da7210_init(da7210
);
490 pr_err("Failed to initialise da7210 audio codec\n");
495 static int da7210_i2c_remove(struct i2c_client
*client
)
497 struct da7210_priv
*da7210
= i2c_get_clientdata(client
);
499 snd_soc_unregister_dai(&da7210_dai
);
500 kfree(da7210
->codec
.reg_cache
);
507 static const struct i2c_device_id da7210_i2c_id
[] = {
511 MODULE_DEVICE_TABLE(i2c
, da7210_i2c_id
);
513 /* I2C codec control layer */
514 static struct i2c_driver da7210_i2c_driver
= {
516 .name
= "DA7210 I2C Codec",
517 .owner
= THIS_MODULE
,
519 .probe
= da7210_i2c_probe
,
520 .remove
= __devexit_p(da7210_i2c_remove
),
521 .id_table
= da7210_i2c_id
,
525 static int da7210_probe(struct platform_device
*pdev
)
527 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
528 struct snd_soc_codec
*codec
;
532 dev_err(&pdev
->dev
, "Codec device not registered\n");
536 socdev
->card
->codec
= da7210_codec
;
537 codec
= da7210_codec
;
540 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
544 dev_info(&pdev
->dev
, "DA7210 Audio Codec %s\n", DA7210_VERSION
);
550 static int da7210_remove(struct platform_device
*pdev
)
552 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
554 snd_soc_free_pcms(socdev
);
555 snd_soc_dapm_free(socdev
);
560 struct snd_soc_codec_device soc_codec_dev_da7210
= {
561 .probe
= da7210_probe
,
562 .remove
= da7210_remove
,
564 EXPORT_SYMBOL_GPL(soc_codec_dev_da7210
);
566 static int __init
da7210_modinit(void)
569 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
570 ret
= i2c_add_driver(&da7210_i2c_driver
);
574 module_init(da7210_modinit
);
576 static void __exit
da7210_exit(void)
578 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
579 i2c_del_driver(&da7210_i2c_driver
);
582 module_exit(da7210_exit
);
584 MODULE_DESCRIPTION("ASoC DA7210 driver");
585 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
586 MODULE_LICENSE("GPL");