blob: fa0d5125e70ba9f54277d9ae328b8fe61ccac5dd [file] [log] [blame]
Kuninori Morimoto98615452009-12-14 13:21:56 +09001/*
2 * DA7210 ALSA Soc codec driver
3 *
4 * Copyright (c) 2009 Dialog Semiconductor
5 * Written by David Chen <Dajun.chen@diasemi.com>
6 *
7 * Copyright (C) 2009 Renesas Solutions Corp.
8 * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
9 *
10 * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
11 *
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.
16 */
17
Kuninori Morimoto98615452009-12-14 13:21:56 +090018#include <linux/delay.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090019#include <linux/i2c.h>
20#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090022#include <sound/pcm.h>
23#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020024#include <sound/soc.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090025#include <sound/initval.h>
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +090026#include <sound/tlv.h>
Kuninori Morimoto98615452009-12-14 13:21:56 +090027
Kuninori Morimoto98615452009-12-14 13:21:56 +090028/* DA7210 register space */
29#define DA7210_STATUS 0x02
30#define DA7210_STARTUP1 0x03
31#define DA7210_MIC_L 0x07
32#define DA7210_MIC_R 0x08
33#define DA7210_INMIX_L 0x0D
34#define DA7210_INMIX_R 0x0E
35#define DA7210_ADC_HPF 0x0F
36#define DA7210_ADC 0x10
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +053037#define DA7210_ADC_EQ1_2 0X11
38#define DA7210_ADC_EQ3_4 0x12
39#define DA7210_ADC_EQ5 0x13
Kuninori Morimoto98615452009-12-14 13:21:56 +090040#define DA7210_DAC_HPF 0x14
41#define DA7210_DAC_L 0x15
42#define DA7210_DAC_R 0x16
43#define DA7210_DAC_SEL 0x17
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +053044#define DA7210_DAC_EQ1_2 0x19
45#define DA7210_DAC_EQ3_4 0x1A
46#define DA7210_DAC_EQ5 0x1B
Kuninori Morimoto98615452009-12-14 13:21:56 +090047#define DA7210_OUTMIX_L 0x1C
48#define DA7210_OUTMIX_R 0x1D
49#define DA7210_HP_L_VOL 0x21
50#define DA7210_HP_R_VOL 0x22
51#define DA7210_HP_CFG 0x23
52#define DA7210_DAI_SRC_SEL 0x25
53#define DA7210_DAI_CFG1 0x26
54#define DA7210_DAI_CFG3 0x28
Kuninori Morimoto960b3b42010-03-11 11:37:44 +090055#define DA7210_PLL_DIV1 0x29
56#define DA7210_PLL_DIV2 0x2A
Kuninori Morimoto98615452009-12-14 13:21:56 +090057#define DA7210_PLL_DIV3 0x2B
58#define DA7210_PLL 0x2C
Kuninori Morimoto960b3b42010-03-11 11:37:44 +090059#define DA7210_A_HID_UNLOCK 0x8A
60#define DA7210_A_TEST_UNLOCK 0x8B
61#define DA7210_A_PLL1 0x90
62#define DA7210_A_CP_MODE 0xA7
Kuninori Morimoto98615452009-12-14 13:21:56 +090063
64/* STARTUP1 bit fields */
65#define DA7210_SC_MST_EN (1 << 0)
66
67/* MIC_L bit fields */
68#define DA7210_MICBIAS_EN (1 << 6)
69#define DA7210_MIC_L_EN (1 << 7)
70
71/* MIC_R bit fields */
72#define DA7210_MIC_R_EN (1 << 7)
73
74/* INMIX_L bit fields */
75#define DA7210_IN_L_EN (1 << 7)
76
77/* INMIX_R bit fields */
78#define DA7210_IN_R_EN (1 << 7)
79
Kuninori Morimoto98615452009-12-14 13:21:56 +090080/* ADC bit fields */
81#define DA7210_ADC_L_EN (1 << 3)
82#define DA7210_ADC_R_EN (1 << 7)
83
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +090084/* DAC/ADC HPF fields */
85#define DA7210_VOICE_F0_MASK (0x7 << 4)
86#define DA7210_VOICE_F0_25 (1 << 4)
87#define DA7210_VOICE_EN (1 << 7)
Mark Brownc2151432009-12-16 20:36:37 +000088
Kuninori Morimoto98615452009-12-14 13:21:56 +090089/* DAC_SEL bit fields */
90#define DA7210_DAC_L_SRC_DAI_L (4 << 0)
91#define DA7210_DAC_L_EN (1 << 3)
92#define DA7210_DAC_R_SRC_DAI_R (5 << 4)
93#define DA7210_DAC_R_EN (1 << 7)
94
95/* OUTMIX_L bit fields */
96#define DA7210_OUT_L_EN (1 << 7)
97
98/* OUTMIX_R bit fields */
99#define DA7210_OUT_R_EN (1 << 7)
100
101/* HP_CFG bit fields */
102#define DA7210_HP_2CAP_MODE (1 << 1)
103#define DA7210_HP_SENSE_EN (1 << 2)
104#define DA7210_HP_L_EN (1 << 3)
105#define DA7210_HP_MODE (1 << 6)
106#define DA7210_HP_R_EN (1 << 7)
107
108/* DAI_SRC_SEL bit fields */
109#define DA7210_DAI_OUT_L_SRC (6 << 0)
110#define DA7210_DAI_OUT_R_SRC (7 << 4)
111
112/* DAI_CFG1 bit fields */
113#define DA7210_DAI_WORD_S16_LE (0 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530114#define DA7210_DAI_WORD_S20_3LE (1 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900115#define DA7210_DAI_WORD_S24_LE (2 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530116#define DA7210_DAI_WORD_S32_LE (3 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900117#define DA7210_DAI_FLEN_64BIT (1 << 2)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530118#define DA7210_DAI_MODE_SLAVE (0 << 7)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900119#define DA7210_DAI_MODE_MASTER (1 << 7)
120
121/* DAI_CFG3 bit fields */
122#define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530123#define DA7210_DAI_FORMAT_LEFT_J (1 << 0)
124#define DA7210_DAI_FORMAT_RIGHT_J (2 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900125#define DA7210_DAI_OE (1 << 3)
126#define DA7210_DAI_EN (1 << 7)
127
128/*PLL_DIV3 bit fields */
129#define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
130#define DA7210_PLL_BYP (1 << 6)
131
132/* PLL bit fields */
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900133#define DA7210_PLL_FS_MASK (0xF << 0)
134#define DA7210_PLL_FS_8000 (0x1 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900135#define DA7210_PLL_FS_11025 (0x2 << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900136#define DA7210_PLL_FS_12000 (0x3 << 0)
137#define DA7210_PLL_FS_16000 (0x5 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900138#define DA7210_PLL_FS_22050 (0x6 << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900139#define DA7210_PLL_FS_24000 (0x7 << 0)
140#define DA7210_PLL_FS_32000 (0x9 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900141#define DA7210_PLL_FS_44100 (0xA << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900142#define DA7210_PLL_FS_48000 (0xB << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900143#define DA7210_PLL_FS_88200 (0xE << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900144#define DA7210_PLL_FS_96000 (0xF << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900145#define DA7210_PLL_EN (0x1 << 7)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900146
147#define DA7210_VERSION "0.0.1"
148
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900149/*
150 * Playback Volume
151 *
152 * max : 0x3F (+15.0 dB)
153 * (1.5 dB step)
154 * min : 0x11 (-54.0 dB)
155 * mute : 0x10
156 * reserved : 0x00 - 0x0F
157 *
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900158 * Reserved area are considered as "mute".
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900159 */
Ashish Chavan7a0e67b2011-10-14 16:25:25 +0530160static const unsigned int hp_out_tlv[] = {
161 TLV_DB_RANGE_HEAD(2),
162 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
163 /* -54 dB to +15 dB */
164 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0),
165};
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900166
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530167static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
168static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
169
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530170/* ADC and DAC high pass filter f0 value */
171static const char const *da7210_hpf_cutoff_txt[] = {
172 "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
173};
174
175static const struct soc_enum da7210_dac_hpf_cutoff =
176 SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
177
178static const struct soc_enum da7210_adc_hpf_cutoff =
179 SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
180
181/* ADC and DAC voice (8kHz) high pass cutoff value */
182static const char const *da7210_vf_cutoff_txt[] = {
183 "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
184};
185
186static const struct soc_enum da7210_dac_vf_cutoff =
187 SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
188
189static const struct soc_enum da7210_adc_vf_cutoff =
190 SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
191
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900192static const struct snd_kcontrol_new da7210_snd_controls[] = {
193
194 SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
195 DA7210_HP_L_VOL, DA7210_HP_R_VOL,
196 0, 0x3F, 0, hp_out_tlv),
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530197
198 /* DAC Equalizer controls */
199 SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
200 SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
201 eq_gain_tlv),
202 SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
203 eq_gain_tlv),
204 SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
205 eq_gain_tlv),
206 SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
207 eq_gain_tlv),
208 SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
209 eq_gain_tlv),
210
211 /* ADC Equalizer controls */
212 SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
213 SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
214 1, adc_eq_master_gain_tlv),
215 SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
216 eq_gain_tlv),
217 SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
218 eq_gain_tlv),
219 SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
220 eq_gain_tlv),
221 SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
222 eq_gain_tlv),
223 SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
224 eq_gain_tlv),
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530225
226 SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
227 SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
228 SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
229 SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
230
231 SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
232 SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
233 SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
234 SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900235};
236
Kuninori Morimoto98615452009-12-14 13:21:56 +0900237/* Codec private data */
238struct da7210_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000239 enum snd_soc_control_type control_type;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900240};
241
Kuninori Morimoto98615452009-12-14 13:21:56 +0900242/*
243 * Register cache
244 */
245static const u8 da7210_reg[] = {
246 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
248 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
249 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
250 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
251 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
252 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
253 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
254 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
255 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
256 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
257 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
259 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
261 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
262 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
263 0x00, /* R88 */
264};
265
Axel Lin40a49712011-10-12 07:16:25 +0800266static int da7210_volatile_register(struct snd_soc_codec *codec,
267 unsigned int reg)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900268{
Axel Lin40a49712011-10-12 07:16:25 +0800269 switch (reg) {
270 case DA7210_STATUS:
271 return 1;
272 default:
273 return 0;
274 }
Kuninori Morimoto98615452009-12-14 13:21:56 +0900275}
Kuninori Morimoto98615452009-12-14 13:21:56 +0900276static int da7210_startup(struct snd_pcm_substream *substream,
277 struct snd_soc_dai *dai)
278{
279 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
280 struct snd_soc_codec *codec = dai->codec;
281
282 if (is_play) {
Kuninori Morimoto98615452009-12-14 13:21:56 +0900283 /* Enable Out */
284 snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
285 snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
286
287 } else {
288 /* Volume 7 */
289 snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
290 snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
291
292 /* Enable Mic */
293 snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
294 snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
295 }
296
297 return 0;
298}
299
300/*
301 * Set PCM DAI word length.
302 */
303static int da7210_hw_params(struct snd_pcm_substream *substream,
304 struct snd_pcm_hw_params *params,
305 struct snd_soc_dai *dai)
306{
307 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000308 struct snd_soc_codec *codec = rtd->codec;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900309 u32 dai_cfg1;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900310 u32 fs, bypass;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900311
312 /* set DAI source to Left and Right ADC */
Axel Lin40a49712011-10-12 07:16:25 +0800313 snd_soc_write(codec, DA7210_DAI_SRC_SEL,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900314 DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
315
316 /* Enable DAI */
Axel Lin40a49712011-10-12 07:16:25 +0800317 snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900318
Axel Lin40a49712011-10-12 07:16:25 +0800319 dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900320
321 switch (params_format(params)) {
322 case SNDRV_PCM_FORMAT_S16_LE:
323 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
324 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530325 case SNDRV_PCM_FORMAT_S20_3LE:
326 dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
327 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900328 case SNDRV_PCM_FORMAT_S24_LE:
329 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
330 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530331 case SNDRV_PCM_FORMAT_S32_LE:
332 dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
333 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900334 default:
335 return -EINVAL;
336 }
337
Axel Lin40a49712011-10-12 07:16:25 +0800338 snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900339
Kuninori Morimoto98615452009-12-14 13:21:56 +0900340 switch (params_rate(params)) {
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900341 case 8000:
342 fs = DA7210_PLL_FS_8000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900343 bypass = DA7210_PLL_BYP;
344 break;
345 case 11025:
346 fs = DA7210_PLL_FS_11025;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900347 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900348 break;
349 case 12000:
350 fs = DA7210_PLL_FS_12000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900351 bypass = DA7210_PLL_BYP;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900352 break;
353 case 16000:
354 fs = DA7210_PLL_FS_16000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900355 bypass = DA7210_PLL_BYP;
356 break;
357 case 22050:
358 fs = DA7210_PLL_FS_22050;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900359 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900360 break;
361 case 32000:
362 fs = DA7210_PLL_FS_32000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900363 bypass = DA7210_PLL_BYP;
364 break;
365 case 44100:
366 fs = DA7210_PLL_FS_44100;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900367 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900368 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900369 case 48000:
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900370 fs = DA7210_PLL_FS_48000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900371 bypass = DA7210_PLL_BYP;
372 break;
373 case 88200:
374 fs = DA7210_PLL_FS_88200;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900375 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900376 break;
377 case 96000:
378 fs = DA7210_PLL_FS_96000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900379 bypass = DA7210_PLL_BYP;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900380 break;
381 default:
382 return -EINVAL;
383 }
384
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900385 /* Disable active mode */
386 snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
387
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900388 snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900389 snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
390
391 /* Enable active mode */
392 snd_soc_update_bits(codec, DA7210_STARTUP1,
393 DA7210_SC_MST_EN, DA7210_SC_MST_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900394
395 return 0;
396}
397
398/*
399 * Set DAI mode and Format
400 */
401static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
402{
403 struct snd_soc_codec *codec = codec_dai->codec;
404 u32 dai_cfg1;
405 u32 dai_cfg3;
406
Axel Lin40a49712011-10-12 07:16:25 +0800407 dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
408 dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900409
410 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
411 case SND_SOC_DAIFMT_CBM_CFM:
412 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
413 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530414 case SND_SOC_DAIFMT_CBS_CFS:
415 dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
416 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900417 default:
418 return -EINVAL;
419 }
420
421 /* FIXME
422 *
423 * It support I2S only now
424 */
425 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
426 case SND_SOC_DAIFMT_I2S:
427 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
428 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530429 case SND_SOC_DAIFMT_LEFT_J:
430 dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
431 break;
432 case SND_SOC_DAIFMT_RIGHT_J:
433 dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
434 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900435 default:
436 return -EINVAL;
437 }
438
439 /* FIXME
440 *
441 * It support 64bit data transmission only now
442 */
443 dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
444
Axel Lin40a49712011-10-12 07:16:25 +0800445 snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
446 snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900447
448 return 0;
449}
450
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530451#define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
452 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900453
454/* DAI operations */
455static struct snd_soc_dai_ops da7210_dai_ops = {
456 .startup = da7210_startup,
457 .hw_params = da7210_hw_params,
458 .set_fmt = da7210_set_dai_fmt,
459};
460
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000461static struct snd_soc_dai_driver da7210_dai = {
462 .name = "da7210-hifi",
Kuninori Morimoto98615452009-12-14 13:21:56 +0900463 /* playback capabilities */
464 .playback = {
465 .stream_name = "Playback",
466 .channels_min = 1,
467 .channels_max = 2,
468 .rates = SNDRV_PCM_RATE_8000_96000,
469 .formats = DA7210_FORMATS,
470 },
471 /* capture capabilities */
472 .capture = {
473 .stream_name = "Capture",
474 .channels_min = 1,
475 .channels_max = 2,
476 .rates = SNDRV_PCM_RATE_8000_96000,
477 .formats = DA7210_FORMATS,
478 },
479 .ops = &da7210_dai_ops,
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900480 .symmetric_rates = 1,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900481};
Kuninori Morimoto98615452009-12-14 13:21:56 +0900482
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000483static int da7210_probe(struct snd_soc_codec *codec)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900484{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000485 struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
Axel Lin40a49712011-10-12 07:16:25 +0800486 int ret;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900487
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000488 dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900489
Axel Lin40a49712011-10-12 07:16:25 +0800490 ret = snd_soc_codec_set_cache_io(codec, 8, 8, da7210->control_type);
491 if (ret < 0) {
492 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
493 return ret;
494 }
Kuninori Morimoto98615452009-12-14 13:21:56 +0900495
496 /* FIXME
497 *
498 * This driver use fixed value here
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900499 * And below settings expects MCLK = 12.288MHz
500 *
501 * When you select different MCLK, please check...
502 * DA7210_PLL_DIV1 val
503 * DA7210_PLL_DIV2 val
504 * DA7210_PLL_DIV3 val
505 * DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
Kuninori Morimoto98615452009-12-14 13:21:56 +0900506 */
507
508 /*
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900509 * make sure that DA7210 use bypass mode before start up
510 */
Axel Lin40a49712011-10-12 07:16:25 +0800511 snd_soc_write(codec, DA7210_STARTUP1, 0);
512 snd_soc_write(codec, DA7210_PLL_DIV3,
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900513 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
514
515 /*
Kuninori Morimoto98615452009-12-14 13:21:56 +0900516 * ADC settings
517 */
518
519 /* Enable Left & Right MIC PGA and Mic Bias */
Axel Lin40a49712011-10-12 07:16:25 +0800520 snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
521 snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900522
523 /* Enable Left and Right input PGA */
Axel Lin40a49712011-10-12 07:16:25 +0800524 snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
525 snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900526
527 /* Enable Left and Right ADC */
Axel Lin40a49712011-10-12 07:16:25 +0800528 snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900529
530 /*
531 * DAC settings
532 */
533
534 /* Enable Left and Right DAC */
Axel Lin40a49712011-10-12 07:16:25 +0800535 snd_soc_write(codec, DA7210_DAC_SEL,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900536 DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
537 DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
538
539 /* Enable Left and Right out PGA */
Axel Lin40a49712011-10-12 07:16:25 +0800540 snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
541 snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900542
543 /* Enable Left and Right HeadPhone PGA */
Axel Lin40a49712011-10-12 07:16:25 +0800544 snd_soc_write(codec, DA7210_HP_CFG,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900545 DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
546 DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
547
548 /* Diable PLL and bypass it */
Axel Lin40a49712011-10-12 07:16:25 +0800549 snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900550
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900551 /*
552 * If 48kHz sound came, it use bypass mode,
553 * and when it is 44.1kHz, it use PLL.
554 *
555 * This time, this driver sets PLL always ON
556 * and controls bypass/PLL mode by switching
557 * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
558 * see da7210_hw_params
559 */
Axel Lin40a49712011-10-12 07:16:25 +0800560 snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
561 snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
562 snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
Kuninori Morimoto98615452009-12-14 13:21:56 +0900563 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900564 snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
565
566 /* As suggested by Dialog */
Axel Lin40a49712011-10-12 07:16:25 +0800567 snd_soc_write(codec, DA7210_A_HID_UNLOCK, 0x8B); /* unlock */
568 snd_soc_write(codec, DA7210_A_TEST_UNLOCK, 0xB4);
569 snd_soc_write(codec, DA7210_A_PLL1, 0x01);
570 snd_soc_write(codec, DA7210_A_CP_MODE, 0x7C);
571 snd_soc_write(codec, DA7210_A_HID_UNLOCK, 0x00); /* re-lock */
572 snd_soc_write(codec, DA7210_A_TEST_UNLOCK, 0x00);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900573
574 /* Activate all enabled subsystem */
Axel Lin40a49712011-10-12 07:16:25 +0800575 snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900576
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900578
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 return 0;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900580}
581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +0900583 .probe = da7210_probe,
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +0900584 .reg_cache_size = ARRAY_SIZE(da7210_reg),
585 .reg_word_size = sizeof(u8),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 .reg_cache_default = da7210_reg,
Axel Lin40a49712011-10-12 07:16:25 +0800587 .volatile_register = da7210_volatile_register,
Mark Browna6f096f2011-10-14 20:18:49 +0100588
589 .controls = da7210_snd_controls,
590 .num_controls = ARRAY_SIZE(da7210_snd_controls),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591};
592
Kuninori Morimoto98615452009-12-14 13:21:56 +0900593#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brown735fe4c2010-01-12 14:13:00 +0000594static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
595 const struct i2c_device_id *id)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900596{
597 struct da7210_priv *da7210;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900598 int ret;
599
600 da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
601 if (!da7210)
602 return -ENOMEM;
603
Kuninori Morimoto98615452009-12-14 13:21:56 +0900604 i2c_set_clientdata(i2c, da7210);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 da7210->control_type = SND_SOC_I2C;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900606
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 ret = snd_soc_register_codec(&i2c->dev,
608 &soc_codec_dev_da7210, &da7210_dai, 1);
609 if (ret < 0)
Axel Lin085efd22010-07-23 05:53:45 +0000610 kfree(da7210);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900611
612 return ret;
613}
614
Mark Brown735fe4c2010-01-12 14:13:00 +0000615static int __devexit da7210_i2c_remove(struct i2c_client *client)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900616{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 snd_soc_unregister_codec(&client->dev);
618 kfree(i2c_get_clientdata(client));
Kuninori Morimoto98615452009-12-14 13:21:56 +0900619 return 0;
620}
621
622static const struct i2c_device_id da7210_i2c_id[] = {
623 { "da7210", 0 },
624 { }
625};
626MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
627
628/* I2C codec control layer */
629static struct i2c_driver da7210_i2c_driver = {
630 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 .name = "da7210-codec",
Kuninori Morimoto98615452009-12-14 13:21:56 +0900632 .owner = THIS_MODULE,
633 },
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +0900634 .probe = da7210_i2c_probe,
635 .remove = __devexit_p(da7210_i2c_remove),
636 .id_table = da7210_i2c_id,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900637};
638#endif
639
Kuninori Morimoto98615452009-12-14 13:21:56 +0900640static int __init da7210_modinit(void)
641{
642 int ret = 0;
643#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
644 ret = i2c_add_driver(&da7210_i2c_driver);
645#endif
646 return ret;
647}
648module_init(da7210_modinit);
649
650static void __exit da7210_exit(void)
651{
652#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
653 i2c_del_driver(&da7210_i2c_driver);
654#endif
655}
656module_exit(da7210_exit);
657
658MODULE_DESCRIPTION("ASoC DA7210 driver");
659MODULE_AUTHOR("David Chen, Kuninori Morimoto");
660MODULE_LICENSE("GPL");