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 <linux/slab.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <asm/div64.h>
38 /* DA7210 register space */
39 #define DA7210_STATUS 0x02
40 #define DA7210_STARTUP1 0x03
41 #define DA7210_MIC_L 0x07
42 #define DA7210_MIC_R 0x08
43 #define DA7210_INMIX_L 0x0D
44 #define DA7210_INMIX_R 0x0E
45 #define DA7210_ADC_HPF 0x0F
46 #define DA7210_ADC 0x10
47 #define DA7210_DAC_HPF 0x14
48 #define DA7210_DAC_L 0x15
49 #define DA7210_DAC_R 0x16
50 #define DA7210_DAC_SEL 0x17
51 #define DA7210_OUTMIX_L 0x1C
52 #define DA7210_OUTMIX_R 0x1D
53 #define DA7210_HP_L_VOL 0x21
54 #define DA7210_HP_R_VOL 0x22
55 #define DA7210_HP_CFG 0x23
56 #define DA7210_DAI_SRC_SEL 0x25
57 #define DA7210_DAI_CFG1 0x26
58 #define DA7210_DAI_CFG3 0x28
59 #define DA7210_PLL_DIV3 0x2B
60 #define DA7210_PLL 0x2C
62 /* STARTUP1 bit fields */
63 #define DA7210_SC_MST_EN (1 << 0)
65 /* MIC_L bit fields */
66 #define DA7210_MICBIAS_EN (1 << 6)
67 #define DA7210_MIC_L_EN (1 << 7)
69 /* MIC_R bit fields */
70 #define DA7210_MIC_R_EN (1 << 7)
72 /* INMIX_L bit fields */
73 #define DA7210_IN_L_EN (1 << 7)
75 /* INMIX_R bit fields */
76 #define DA7210_IN_R_EN (1 << 7)
78 /* ADC_HPF bit fields */
79 #define DA7210_ADC_VOICE_EN (1 << 7)
82 #define DA7210_ADC_L_EN (1 << 3)
83 #define DA7210_ADC_R_EN (1 << 7)
86 #define DA7210_DAC_VOICE_EN (1 << 7)
88 /* DAC_SEL bit fields */
89 #define DA7210_DAC_L_SRC_DAI_L (4 << 0)
90 #define DA7210_DAC_L_EN (1 << 3)
91 #define DA7210_DAC_R_SRC_DAI_R (5 << 4)
92 #define DA7210_DAC_R_EN (1 << 7)
94 /* OUTMIX_L bit fields */
95 #define DA7210_OUT_L_EN (1 << 7)
97 /* OUTMIX_R bit fields */
98 #define DA7210_OUT_R_EN (1 << 7)
100 /* HP_CFG bit fields */
101 #define DA7210_HP_2CAP_MODE (1 << 1)
102 #define DA7210_HP_SENSE_EN (1 << 2)
103 #define DA7210_HP_L_EN (1 << 3)
104 #define DA7210_HP_MODE (1 << 6)
105 #define DA7210_HP_R_EN (1 << 7)
107 /* DAI_SRC_SEL bit fields */
108 #define DA7210_DAI_OUT_L_SRC (6 << 0)
109 #define DA7210_DAI_OUT_R_SRC (7 << 4)
111 /* DAI_CFG1 bit fields */
112 #define DA7210_DAI_WORD_S16_LE (0 << 0)
113 #define DA7210_DAI_WORD_S24_LE (2 << 0)
114 #define DA7210_DAI_FLEN_64BIT (1 << 2)
115 #define DA7210_DAI_MODE_MASTER (1 << 7)
117 /* DAI_CFG3 bit fields */
118 #define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
119 #define DA7210_DAI_OE (1 << 3)
120 #define DA7210_DAI_EN (1 << 7)
122 /*PLL_DIV3 bit fields */
123 #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
124 #define DA7210_PLL_BYP (1 << 6)
127 #define DA7210_PLL_FS_48000 (11 << 0)
129 #define DA7210_VERSION "0.0.1"
131 /* Codec private data */
133 struct snd_soc_codec codec
;
136 static struct snd_soc_codec
*da7210_codec
;
141 static const u8 da7210_reg
[] = {
142 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
144 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
145 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
146 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
147 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
148 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
149 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
158 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
163 * Read da7210 register cache
165 static inline u32
da7210_read_reg_cache(struct snd_soc_codec
*codec
, u32 reg
)
167 u8
*cache
= codec
->reg_cache
;
168 BUG_ON(reg
> ARRAY_SIZE(da7210_reg
));
173 * Write to the da7210 register space
175 static int da7210_write(struct snd_soc_codec
*codec
, u32 reg
, u32 value
)
177 u8
*cache
= codec
->reg_cache
;
180 BUG_ON(codec
->volatile_register
);
182 data
[0] = reg
& 0xff;
183 data
[1] = value
& 0xff;
185 if (reg
>= codec
->reg_cache_size
)
188 if (2 != codec
->hw_write(codec
->control_data
, data
, 2))
196 * Read from the da7210 register space.
198 static inline u32
da7210_read(struct snd_soc_codec
*codec
, u32 reg
)
200 if (DA7210_STATUS
== reg
)
201 return i2c_smbus_read_byte_data(codec
->control_data
, reg
);
203 return da7210_read_reg_cache(codec
, reg
);
206 static int da7210_startup(struct snd_pcm_substream
*substream
,
207 struct snd_soc_dai
*dai
)
209 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
210 struct snd_soc_codec
*codec
= dai
->codec
;
213 /* PlayBack Volume 40 */
214 snd_soc_update_bits(codec
, DA7210_HP_L_VOL
, 0x3F, 40);
215 snd_soc_update_bits(codec
, DA7210_HP_R_VOL
, 0x3F, 40);
218 snd_soc_update_bits(codec
, DA7210_OUTMIX_L
, 0x1F, 0x10);
219 snd_soc_update_bits(codec
, DA7210_OUTMIX_R
, 0x1F, 0x10);
223 snd_soc_update_bits(codec
, DA7210_MIC_L
, 0x7, 0x7);
224 snd_soc_update_bits(codec
, DA7210_MIC_R
, 0x7, 0x7);
227 snd_soc_update_bits(codec
, DA7210_INMIX_L
, 0x1F, 0x1);
228 snd_soc_update_bits(codec
, DA7210_INMIX_R
, 0x1F, 0x1);
235 * Set PCM DAI word length.
237 static int da7210_hw_params(struct snd_pcm_substream
*substream
,
238 struct snd_pcm_hw_params
*params
,
239 struct snd_soc_dai
*dai
)
241 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
242 struct snd_soc_device
*socdev
= rtd
->socdev
;
243 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
247 /* set DAI source to Left and Right ADC */
248 da7210_write(codec
, DA7210_DAI_SRC_SEL
,
249 DA7210_DAI_OUT_R_SRC
| DA7210_DAI_OUT_L_SRC
);
252 da7210_write(codec
, DA7210_DAI_CFG3
, DA7210_DAI_OE
| DA7210_DAI_EN
);
254 dai_cfg1
= 0xFC & da7210_read(codec
, DA7210_DAI_CFG1
);
256 switch (params_format(params
)) {
257 case SNDRV_PCM_FORMAT_S16_LE
:
258 dai_cfg1
|= DA7210_DAI_WORD_S16_LE
;
260 case SNDRV_PCM_FORMAT_S24_LE
:
261 dai_cfg1
|= DA7210_DAI_WORD_S24_LE
;
267 da7210_write(codec
, DA7210_DAI_CFG1
, dai_cfg1
);
271 * It support 48K only now
273 switch (params_rate(params
)) {
275 if (SNDRV_PCM_STREAM_PLAYBACK
== substream
->stream
) {
276 reg
= DA7210_DAC_HPF
;
277 mask
= DA7210_DAC_VOICE_EN
;
279 reg
= DA7210_ADC_HPF
;
280 mask
= DA7210_ADC_VOICE_EN
;
287 snd_soc_update_bits(codec
, reg
, mask
, 0);
293 * Set DAI mode and Format
295 static int da7210_set_dai_fmt(struct snd_soc_dai
*codec_dai
, u32 fmt
)
297 struct snd_soc_codec
*codec
= codec_dai
->codec
;
301 dai_cfg1
= 0x7f & da7210_read(codec
, DA7210_DAI_CFG1
);
302 dai_cfg3
= 0xfc & da7210_read(codec
, DA7210_DAI_CFG3
);
304 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
305 case SND_SOC_DAIFMT_CBM_CFM
:
306 dai_cfg1
|= DA7210_DAI_MODE_MASTER
;
314 * It support I2S only now
316 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
317 case SND_SOC_DAIFMT_I2S
:
318 dai_cfg3
|= DA7210_DAI_FORMAT_I2SMODE
;
326 * It support 64bit data transmission only now
328 dai_cfg1
|= DA7210_DAI_FLEN_64BIT
;
330 da7210_write(codec
, DA7210_DAI_CFG1
, dai_cfg1
);
331 da7210_write(codec
, DA7210_DAI_CFG3
, dai_cfg3
);
336 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
339 static struct snd_soc_dai_ops da7210_dai_ops
= {
340 .startup
= da7210_startup
,
341 .hw_params
= da7210_hw_params
,
342 .set_fmt
= da7210_set_dai_fmt
,
345 struct snd_soc_dai da7210_dai
= {
346 .name
= "DA7210 IIS",
348 /* playback capabilities */
350 .stream_name
= "Playback",
353 .rates
= SNDRV_PCM_RATE_8000_96000
,
354 .formats
= DA7210_FORMATS
,
356 /* capture capabilities */
358 .stream_name
= "Capture",
361 .rates
= SNDRV_PCM_RATE_8000_96000
,
362 .formats
= DA7210_FORMATS
,
364 .ops
= &da7210_dai_ops
,
366 EXPORT_SYMBOL_GPL(da7210_dai
);
369 * Initialize the DA7210 driver
370 * register the mixer and dsp interfaces with the kernel
372 static int da7210_init(struct da7210_priv
*da7210
)
374 struct snd_soc_codec
*codec
= &da7210
->codec
;
378 dev_err(codec
->dev
, "Another da7210 is registered\n");
382 mutex_init(&codec
->mutex
);
383 INIT_LIST_HEAD(&codec
->dapm_widgets
);
384 INIT_LIST_HEAD(&codec
->dapm_paths
);
386 codec
->private_data
= da7210
;
387 codec
->name
= "DA7210";
388 codec
->owner
= THIS_MODULE
;
389 codec
->read
= da7210_read
;
390 codec
->write
= da7210_write
;
391 codec
->dai
= &da7210_dai
;
393 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
394 codec
->reg_cache_size
= ARRAY_SIZE(da7210_reg
);
395 codec
->reg_cache
= kmemdup(da7210_reg
,
396 sizeof(da7210_reg
), GFP_KERNEL
);
398 if (!codec
->reg_cache
)
401 da7210_dai
.dev
= codec
->dev
;
402 da7210_codec
= codec
;
404 ret
= snd_soc_register_codec(codec
);
406 dev_err(codec
->dev
, "Failed to register CODEC: %d\n", ret
);
410 ret
= snd_soc_register_dai(&da7210_dai
);
412 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
418 * This driver use fixed value here
425 /* Enable Left & Right MIC PGA and Mic Bias */
426 da7210_write(codec
, DA7210_MIC_L
, DA7210_MIC_L_EN
| DA7210_MICBIAS_EN
);
427 da7210_write(codec
, DA7210_MIC_R
, DA7210_MIC_R_EN
);
429 /* Enable Left and Right input PGA */
430 da7210_write(codec
, DA7210_INMIX_L
, DA7210_IN_L_EN
);
431 da7210_write(codec
, DA7210_INMIX_R
, DA7210_IN_R_EN
);
433 /* Enable Left and Right ADC */
434 da7210_write(codec
, DA7210_ADC
, DA7210_ADC_L_EN
| DA7210_ADC_R_EN
);
440 /* Enable Left and Right DAC */
441 da7210_write(codec
, DA7210_DAC_SEL
,
442 DA7210_DAC_L_SRC_DAI_L
| DA7210_DAC_L_EN
|
443 DA7210_DAC_R_SRC_DAI_R
| DA7210_DAC_R_EN
);
445 /* Enable Left and Right out PGA */
446 da7210_write(codec
, DA7210_OUTMIX_L
, DA7210_OUT_L_EN
);
447 da7210_write(codec
, DA7210_OUTMIX_R
, DA7210_OUT_R_EN
);
449 /* Enable Left and Right HeadPhone PGA */
450 da7210_write(codec
, DA7210_HP_CFG
,
451 DA7210_HP_2CAP_MODE
| DA7210_HP_SENSE_EN
|
452 DA7210_HP_L_EN
| DA7210_HP_MODE
| DA7210_HP_R_EN
);
454 /* Diable PLL and bypass it */
455 da7210_write(codec
, DA7210_PLL
, DA7210_PLL_FS_48000
);
457 /* Bypass PLL and set MCLK freq rang to 10-20MHz */
458 da7210_write(codec
, DA7210_PLL_DIV3
,
459 DA7210_MCLK_RANGE_10_20_MHZ
| DA7210_PLL_BYP
);
461 /* Activate all enabled subsystem */
462 da7210_write(codec
, DA7210_STARTUP1
, DA7210_SC_MST_EN
);
467 kfree(codec
->reg_cache
);
468 codec
->reg_cache
= NULL
;
474 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
475 static int __devinit
da7210_i2c_probe(struct i2c_client
*i2c
,
476 const struct i2c_device_id
*id
)
478 struct da7210_priv
*da7210
;
479 struct snd_soc_codec
*codec
;
482 da7210
= kzalloc(sizeof(struct da7210_priv
), GFP_KERNEL
);
486 codec
= &da7210
->codec
;
487 codec
->dev
= &i2c
->dev
;
489 i2c_set_clientdata(i2c
, da7210
);
490 codec
->control_data
= i2c
;
492 ret
= da7210_init(da7210
);
494 pr_err("Failed to initialise da7210 audio codec\n");
499 static int __devexit
da7210_i2c_remove(struct i2c_client
*client
)
501 struct da7210_priv
*da7210
= i2c_get_clientdata(client
);
503 snd_soc_unregister_dai(&da7210_dai
);
504 kfree(da7210
->codec
.reg_cache
);
511 static const struct i2c_device_id da7210_i2c_id
[] = {
515 MODULE_DEVICE_TABLE(i2c
, da7210_i2c_id
);
517 /* I2C codec control layer */
518 static struct i2c_driver da7210_i2c_driver
= {
520 .name
= "DA7210 I2C Codec",
521 .owner
= THIS_MODULE
,
523 .probe
= da7210_i2c_probe
,
524 .remove
= __devexit_p(da7210_i2c_remove
),
525 .id_table
= da7210_i2c_id
,
529 static int da7210_probe(struct platform_device
*pdev
)
531 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
532 struct snd_soc_codec
*codec
;
536 dev_err(&pdev
->dev
, "Codec device not registered\n");
540 socdev
->card
->codec
= da7210_codec
;
541 codec
= da7210_codec
;
544 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
548 dev_info(&pdev
->dev
, "DA7210 Audio Codec %s\n", DA7210_VERSION
);
554 static int da7210_remove(struct platform_device
*pdev
)
556 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
558 snd_soc_free_pcms(socdev
);
559 snd_soc_dapm_free(socdev
);
564 struct snd_soc_codec_device soc_codec_dev_da7210
= {
565 .probe
= da7210_probe
,
566 .remove
= da7210_remove
,
568 EXPORT_SYMBOL_GPL(soc_codec_dev_da7210
);
570 static int __init
da7210_modinit(void)
573 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
574 ret
= i2c_add_driver(&da7210_i2c_driver
);
578 module_init(da7210_modinit
);
580 static void __exit
da7210_exit(void)
582 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
583 i2c_del_driver(&da7210_i2c_driver
);
586 module_exit(da7210_exit
);
588 MODULE_DESCRIPTION("ASoC DA7210 driver");
589 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
590 MODULE_LICENSE("GPL");