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)
78 #define DA7210_ADC_L_EN (1 << 3)
79 #define DA7210_ADC_R_EN (1 << 7)
81 /* DAC/ADC HPF fields */
82 #define DA7210_VOICE_F0_MASK (0x7 << 4)
83 #define DA7210_VOICE_F0_25 (1 << 4)
84 #define DA7210_VOICE_EN (1 << 7)
86 /* DAC_SEL bit fields */
87 #define DA7210_DAC_L_SRC_DAI_L (4 << 0)
88 #define DA7210_DAC_L_EN (1 << 3)
89 #define DA7210_DAC_R_SRC_DAI_R (5 << 4)
90 #define DA7210_DAC_R_EN (1 << 7)
92 /* OUTMIX_L bit fields */
93 #define DA7210_OUT_L_EN (1 << 7)
95 /* OUTMIX_R bit fields */
96 #define DA7210_OUT_R_EN (1 << 7)
98 /* HP_CFG bit fields */
99 #define DA7210_HP_2CAP_MODE (1 << 1)
100 #define DA7210_HP_SENSE_EN (1 << 2)
101 #define DA7210_HP_L_EN (1 << 3)
102 #define DA7210_HP_MODE (1 << 6)
103 #define DA7210_HP_R_EN (1 << 7)
105 /* DAI_SRC_SEL bit fields */
106 #define DA7210_DAI_OUT_L_SRC (6 << 0)
107 #define DA7210_DAI_OUT_R_SRC (7 << 4)
109 /* DAI_CFG1 bit fields */
110 #define DA7210_DAI_WORD_S16_LE (0 << 0)
111 #define DA7210_DAI_WORD_S24_LE (2 << 0)
112 #define DA7210_DAI_FLEN_64BIT (1 << 2)
113 #define DA7210_DAI_MODE_MASTER (1 << 7)
115 /* DAI_CFG3 bit fields */
116 #define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
117 #define DA7210_DAI_OE (1 << 3)
118 #define DA7210_DAI_EN (1 << 7)
120 /*PLL_DIV3 bit fields */
121 #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
122 #define DA7210_PLL_BYP (1 << 6)
125 #define DA7210_PLL_FS_MASK (0xF << 0)
126 #define DA7210_PLL_FS_8000 (0x1 << 0)
127 #define DA7210_PLL_FS_12000 (0x3 << 0)
128 #define DA7210_PLL_FS_16000 (0x5 << 0)
129 #define DA7210_PLL_FS_24000 (0x7 << 0)
130 #define DA7210_PLL_FS_32000 (0x9 << 0)
131 #define DA7210_PLL_FS_48000 (0xB << 0)
132 #define DA7210_PLL_FS_96000 (0xF << 0)
135 #define DA7210_VERSION "0.0.1"
137 /* Codec private data */
139 struct snd_soc_codec codec;
142 static struct snd_soc_codec *da7210_codec;
147 static const u8 da7210_reg[] = {
148 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
150 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
151 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
152 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
153 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
154 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
155 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
158 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
163 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
164 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
169 * Read da7210 register cache
171 static inline u32 da7210_read_reg_cache(struct snd_soc_codec *codec, u32 reg)
173 u8 *cache = codec->reg_cache;
174 BUG_ON(reg > ARRAY_SIZE(da7210_reg));
179 * Write to the da7210 register space
181 static int da7210_write(struct snd_soc_codec *codec, u32 reg, u32 value)
183 u8 *cache = codec->reg_cache;
186 BUG_ON(codec->volatile_register);
188 data[0] = reg & 0xff;
189 data[1] = value & 0xff;
191 if (reg >= codec->reg_cache_size)
194 if (2 != codec->hw_write(codec->control_data, data, 2))
202 * Read from the da7210 register space.
204 static inline u32 da7210_read(struct snd_soc_codec *codec, u32 reg)
206 if (DA7210_STATUS == reg)
207 return i2c_smbus_read_byte_data(codec->control_data, reg);
209 return da7210_read_reg_cache(codec, reg);
212 static int da7210_startup(struct snd_pcm_substream *substream,
213 struct snd_soc_dai *dai)
215 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
216 struct snd_soc_codec *codec = dai->codec;
219 /* PlayBack Volume 40 */
220 snd_soc_update_bits(codec, DA7210_HP_L_VOL, 0x3F, 40);
221 snd_soc_update_bits(codec, DA7210_HP_R_VOL, 0x3F, 40);
224 snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
225 snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
229 snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
230 snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
233 snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
234 snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
241 * Set PCM DAI word length.
243 static int da7210_hw_params(struct snd_pcm_substream *substream,
244 struct snd_pcm_hw_params *params,
245 struct snd_soc_dai *dai)
247 struct snd_soc_pcm_runtime *rtd = substream->private_data;
248 struct snd_soc_device *socdev = rtd->socdev;
249 struct snd_soc_codec *codec = socdev->card->codec;
251 u32 hpf_reg, hpf_mask, hpf_value;
254 /* set DAI source to Left and Right ADC */
255 da7210_write(codec, DA7210_DAI_SRC_SEL,
256 DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
259 da7210_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
261 dai_cfg1 = 0xFC & da7210_read(codec, DA7210_DAI_CFG1);
263 switch (params_format(params)) {
264 case SNDRV_PCM_FORMAT_S16_LE:
265 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
267 case SNDRV_PCM_FORMAT_S24_LE:
268 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
274 da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
276 hpf_reg = (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) ?
277 DA7210_DAC_HPF : DA7210_ADC_HPF;
279 switch (params_rate(params)) {
281 fs = DA7210_PLL_FS_8000;
282 hpf_mask = DA7210_VOICE_F0_MASK | DA7210_VOICE_EN;
283 hpf_value = DA7210_VOICE_F0_25 | DA7210_VOICE_EN;
286 fs = DA7210_PLL_FS_12000;
287 hpf_mask = DA7210_VOICE_F0_MASK | DA7210_VOICE_EN;
288 hpf_value = DA7210_VOICE_F0_25 | DA7210_VOICE_EN;
291 fs = DA7210_PLL_FS_16000;
292 hpf_mask = DA7210_VOICE_F0_MASK | DA7210_VOICE_EN;
293 hpf_value = DA7210_VOICE_F0_25 | DA7210_VOICE_EN;
296 fs = DA7210_PLL_FS_32000;
297 hpf_mask = DA7210_VOICE_EN;
301 fs = DA7210_PLL_FS_48000;
302 hpf_mask = DA7210_VOICE_EN;
306 fs = DA7210_PLL_FS_96000;
307 hpf_mask = DA7210_VOICE_EN;
314 snd_soc_update_bits(codec, hpf_reg, hpf_mask, hpf_value);
315 snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
321 * Set DAI mode and Format
323 static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
325 struct snd_soc_codec *codec = codec_dai->codec;
329 dai_cfg1 = 0x7f & da7210_read(codec, DA7210_DAI_CFG1);
330 dai_cfg3 = 0xfc & da7210_read(codec, DA7210_DAI_CFG3);
332 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
333 case SND_SOC_DAIFMT_CBM_CFM:
334 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
342 * It support I2S only now
344 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
345 case SND_SOC_DAIFMT_I2S:
346 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
354 * It support 64bit data transmission only now
356 dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
358 da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
359 da7210_write(codec, DA7210_DAI_CFG3, dai_cfg3);
364 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
367 static struct snd_soc_dai_ops da7210_dai_ops = {
368 .startup = da7210_startup,
369 .hw_params = da7210_hw_params,
370 .set_fmt = da7210_set_dai_fmt,
373 struct snd_soc_dai da7210_dai = {
374 .name = "DA7210 IIS",
376 /* playback capabilities */
378 .stream_name = "Playback",
381 .rates = SNDRV_PCM_RATE_8000_96000,
382 .formats = DA7210_FORMATS,
384 /* capture capabilities */
386 .stream_name = "Capture",
389 .rates = SNDRV_PCM_RATE_8000_96000,
390 .formats = DA7210_FORMATS,
392 .ops = &da7210_dai_ops,
394 EXPORT_SYMBOL_GPL(da7210_dai);
397 * Initialize the DA7210 driver
398 * register the mixer and dsp interfaces with the kernel
400 static int da7210_init(struct da7210_priv *da7210)
402 struct snd_soc_codec *codec = &da7210->codec;
406 dev_err(codec->dev, "Another da7210 is registered\n");
410 mutex_init(&codec->mutex);
411 INIT_LIST_HEAD(&codec->dapm_widgets);
412 INIT_LIST_HEAD(&codec->dapm_paths);
414 codec->private_data = da7210;
415 codec->name = "DA7210";
416 codec->owner = THIS_MODULE;
417 codec->read = da7210_read;
418 codec->write = da7210_write;
419 codec->dai = &da7210_dai;
421 codec->hw_write = (hw_write_t)i2c_master_send;
422 codec->reg_cache_size = ARRAY_SIZE(da7210_reg);
423 codec->reg_cache = kmemdup(da7210_reg,
424 sizeof(da7210_reg), GFP_KERNEL);
426 if (!codec->reg_cache)
429 da7210_dai.dev = codec->dev;
430 da7210_codec = codec;
432 ret = snd_soc_register_codec(codec);
434 dev_err(codec->dev, "Failed to register CODEC: %d\n", ret);
438 ret = snd_soc_register_dai(&da7210_dai);
440 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
446 * This driver use fixed value here
453 /* Enable Left & Right MIC PGA and Mic Bias */
454 da7210_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
455 da7210_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
457 /* Enable Left and Right input PGA */
458 da7210_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
459 da7210_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
461 /* Enable Left and Right ADC */
462 da7210_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
468 /* Enable Left and Right DAC */
469 da7210_write(codec, DA7210_DAC_SEL,
470 DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
471 DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
473 /* Enable Left and Right out PGA */
474 da7210_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
475 da7210_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
477 /* Enable Left and Right HeadPhone PGA */
478 da7210_write(codec, DA7210_HP_CFG,
479 DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
480 DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
482 /* Diable PLL and bypass it */
483 da7210_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
485 /* Bypass PLL and set MCLK freq rang to 10-20MHz */
486 da7210_write(codec, DA7210_PLL_DIV3,
487 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
489 /* Activate all enabled subsystem */
490 da7210_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
495 kfree(codec->reg_cache);
496 codec->reg_cache = NULL;
502 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
503 static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
504 const struct i2c_device_id *id)
506 struct da7210_priv *da7210;
507 struct snd_soc_codec *codec;
510 da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
514 codec = &da7210->codec;
515 codec->dev = &i2c->dev;
517 i2c_set_clientdata(i2c, da7210);
518 codec->control_data = i2c;
520 ret = da7210_init(da7210);
522 pr_err("Failed to initialise da7210 audio codec\n");
527 static int __devexit da7210_i2c_remove(struct i2c_client *client)
529 struct da7210_priv *da7210 = i2c_get_clientdata(client);
531 snd_soc_unregister_dai(&da7210_dai);
532 kfree(da7210->codec.reg_cache);
539 static const struct i2c_device_id da7210_i2c_id[] = {
543 MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
545 /* I2C codec control layer */
546 static struct i2c_driver da7210_i2c_driver = {
548 .name = "DA7210 I2C Codec",
549 .owner = THIS_MODULE,
551 .probe = da7210_i2c_probe,
552 .remove = __devexit_p(da7210_i2c_remove),
553 .id_table = da7210_i2c_id,
557 static int da7210_probe(struct platform_device *pdev)
559 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
560 struct snd_soc_codec *codec;
564 dev_err(&pdev->dev, "Codec device not registered\n");
568 socdev->card->codec = da7210_codec;
569 codec = da7210_codec;
572 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
576 dev_info(&pdev->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
582 static int da7210_remove(struct platform_device *pdev)
584 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
586 snd_soc_free_pcms(socdev);
587 snd_soc_dapm_free(socdev);
592 struct snd_soc_codec_device soc_codec_dev_da7210 = {
593 .probe = da7210_probe,
594 .remove = da7210_remove,
596 EXPORT_SYMBOL_GPL(soc_codec_dev_da7210);
598 static int __init da7210_modinit(void)
601 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
602 ret = i2c_add_driver(&da7210_i2c_driver);
606 module_init(da7210_modinit);
608 static void __exit da7210_exit(void)
610 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
611 i2c_del_driver(&da7210_i2c_driver);
614 module_exit(da7210_exit);
616 MODULE_DESCRIPTION("ASoC DA7210 driver");
617 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
618 MODULE_LICENSE("GPL");