blob: 4aad01c10c5308c4b95c786b6042963ae09f3ecd [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
Ashish Chavan5eda1942011-10-19 14:19:06 +053033#define DA7210_AUX2 0x0B
Kuninori Morimoto98615452009-12-14 13:21:56 +090034#define DA7210_INMIX_L 0x0D
35#define DA7210_INMIX_R 0x0E
36#define DA7210_ADC_HPF 0x0F
37#define DA7210_ADC 0x10
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +053038#define DA7210_ADC_EQ1_2 0X11
39#define DA7210_ADC_EQ3_4 0x12
40#define DA7210_ADC_EQ5 0x13
Kuninori Morimoto98615452009-12-14 13:21:56 +090041#define DA7210_DAC_HPF 0x14
42#define DA7210_DAC_L 0x15
43#define DA7210_DAC_R 0x16
44#define DA7210_DAC_SEL 0x17
Ashish Chavan5eda1942011-10-19 14:19:06 +053045#define DA7210_SOFTMUTE 0x18
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +053046#define DA7210_DAC_EQ1_2 0x19
47#define DA7210_DAC_EQ3_4 0x1A
48#define DA7210_DAC_EQ5 0x1B
Kuninori Morimoto98615452009-12-14 13:21:56 +090049#define DA7210_OUTMIX_L 0x1C
50#define DA7210_OUTMIX_R 0x1D
51#define DA7210_HP_L_VOL 0x21
52#define DA7210_HP_R_VOL 0x22
53#define DA7210_HP_CFG 0x23
Ashish Chavan5eda1942011-10-19 14:19:06 +053054#define DA7210_ZERO_CROSS 0x24
Kuninori Morimoto98615452009-12-14 13:21:56 +090055#define DA7210_DAI_SRC_SEL 0x25
56#define DA7210_DAI_CFG1 0x26
57#define DA7210_DAI_CFG3 0x28
Kuninori Morimoto960b3b42010-03-11 11:37:44 +090058#define DA7210_PLL_DIV1 0x29
59#define DA7210_PLL_DIV2 0x2A
Kuninori Morimoto98615452009-12-14 13:21:56 +090060#define DA7210_PLL_DIV3 0x2B
61#define DA7210_PLL 0x2C
Kuninori Morimoto960b3b42010-03-11 11:37:44 +090062#define DA7210_A_HID_UNLOCK 0x8A
63#define DA7210_A_TEST_UNLOCK 0x8B
64#define DA7210_A_PLL1 0x90
65#define DA7210_A_CP_MODE 0xA7
Kuninori Morimoto98615452009-12-14 13:21:56 +090066
67/* STARTUP1 bit fields */
68#define DA7210_SC_MST_EN (1 << 0)
69
70/* MIC_L bit fields */
71#define DA7210_MICBIAS_EN (1 << 6)
72#define DA7210_MIC_L_EN (1 << 7)
73
74/* MIC_R bit fields */
75#define DA7210_MIC_R_EN (1 << 7)
76
77/* INMIX_L bit fields */
78#define DA7210_IN_L_EN (1 << 7)
79
80/* INMIX_R bit fields */
81#define DA7210_IN_R_EN (1 << 7)
82
Kuninori Morimoto98615452009-12-14 13:21:56 +090083/* ADC bit fields */
84#define DA7210_ADC_L_EN (1 << 3)
85#define DA7210_ADC_R_EN (1 << 7)
86
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +090087/* DAC/ADC HPF fields */
88#define DA7210_VOICE_F0_MASK (0x7 << 4)
89#define DA7210_VOICE_F0_25 (1 << 4)
90#define DA7210_VOICE_EN (1 << 7)
Mark Brownc2151432009-12-16 20:36:37 +000091
Kuninori Morimoto98615452009-12-14 13:21:56 +090092/* DAC_SEL bit fields */
93#define DA7210_DAC_L_SRC_DAI_L (4 << 0)
94#define DA7210_DAC_L_EN (1 << 3)
95#define DA7210_DAC_R_SRC_DAI_R (5 << 4)
96#define DA7210_DAC_R_EN (1 << 7)
97
98/* OUTMIX_L bit fields */
99#define DA7210_OUT_L_EN (1 << 7)
100
101/* OUTMIX_R bit fields */
102#define DA7210_OUT_R_EN (1 << 7)
103
104/* HP_CFG bit fields */
105#define DA7210_HP_2CAP_MODE (1 << 1)
106#define DA7210_HP_SENSE_EN (1 << 2)
107#define DA7210_HP_L_EN (1 << 3)
108#define DA7210_HP_MODE (1 << 6)
109#define DA7210_HP_R_EN (1 << 7)
110
111/* DAI_SRC_SEL bit fields */
112#define DA7210_DAI_OUT_L_SRC (6 << 0)
113#define DA7210_DAI_OUT_R_SRC (7 << 4)
114
115/* DAI_CFG1 bit fields */
116#define DA7210_DAI_WORD_S16_LE (0 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530117#define DA7210_DAI_WORD_S20_3LE (1 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900118#define DA7210_DAI_WORD_S24_LE (2 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530119#define DA7210_DAI_WORD_S32_LE (3 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900120#define DA7210_DAI_FLEN_64BIT (1 << 2)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530121#define DA7210_DAI_MODE_SLAVE (0 << 7)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900122#define DA7210_DAI_MODE_MASTER (1 << 7)
123
124/* DAI_CFG3 bit fields */
125#define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530126#define DA7210_DAI_FORMAT_LEFT_J (1 << 0)
127#define DA7210_DAI_FORMAT_RIGHT_J (2 << 0)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900128#define DA7210_DAI_OE (1 << 3)
129#define DA7210_DAI_EN (1 << 7)
130
131/*PLL_DIV3 bit fields */
132#define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
133#define DA7210_PLL_BYP (1 << 6)
134
135/* PLL bit fields */
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900136#define DA7210_PLL_FS_MASK (0xF << 0)
137#define DA7210_PLL_FS_8000 (0x1 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900138#define DA7210_PLL_FS_11025 (0x2 << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900139#define DA7210_PLL_FS_12000 (0x3 << 0)
140#define DA7210_PLL_FS_16000 (0x5 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900141#define DA7210_PLL_FS_22050 (0x6 << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900142#define DA7210_PLL_FS_24000 (0x7 << 0)
143#define DA7210_PLL_FS_32000 (0x9 << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900144#define DA7210_PLL_FS_44100 (0xA << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900145#define DA7210_PLL_FS_48000 (0xB << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900146#define DA7210_PLL_FS_88200 (0xE << 0)
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900147#define DA7210_PLL_FS_96000 (0xF << 0)
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900148#define DA7210_PLL_EN (0x1 << 7)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900149
Ashish Chavan5eda1942011-10-19 14:19:06 +0530150/* SOFTMUTE bit fields */
151#define DA7210_RAMP_EN (1 << 6)
152
Kuninori Morimoto98615452009-12-14 13:21:56 +0900153#define DA7210_VERSION "0.0.1"
154
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900155/*
156 * Playback Volume
157 *
158 * max : 0x3F (+15.0 dB)
159 * (1.5 dB step)
160 * min : 0x11 (-54.0 dB)
161 * mute : 0x10
162 * reserved : 0x00 - 0x0F
163 *
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900164 * Reserved area are considered as "mute".
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900165 */
Ashish Chavan7a0e67b2011-10-14 16:25:25 +0530166static const unsigned int hp_out_tlv[] = {
167 TLV_DB_RANGE_HEAD(2),
168 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
169 /* -54 dB to +15 dB */
170 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0),
171};
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900172
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530173static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
174static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
175
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530176/* ADC and DAC high pass filter f0 value */
177static const char const *da7210_hpf_cutoff_txt[] = {
178 "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
179};
180
181static const struct soc_enum da7210_dac_hpf_cutoff =
182 SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
183
184static const struct soc_enum da7210_adc_hpf_cutoff =
185 SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
186
187/* ADC and DAC voice (8kHz) high pass cutoff value */
188static const char const *da7210_vf_cutoff_txt[] = {
189 "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
190};
191
192static const struct soc_enum da7210_dac_vf_cutoff =
193 SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
194
195static const struct soc_enum da7210_adc_vf_cutoff =
196 SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
197
Ashish Chavan5eda1942011-10-19 14:19:06 +0530198static const char *da7210_hp_mode_txt[] = {
199 "Class H", "Class G"
200};
201
202static const struct soc_enum da7210_hp_mode_sel =
203 SOC_ENUM_SINGLE(DA7210_HP_CFG, 0, 2, da7210_hp_mode_txt);
204
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900205static const struct snd_kcontrol_new da7210_snd_controls[] = {
206
207 SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
208 DA7210_HP_L_VOL, DA7210_HP_R_VOL,
209 0, 0x3F, 0, hp_out_tlv),
Ashish Chavan0ee6e9e2011-10-15 14:47:56 +0530210
211 /* DAC Equalizer controls */
212 SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
213 SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
214 eq_gain_tlv),
215 SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
216 eq_gain_tlv),
217 SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
218 eq_gain_tlv),
219 SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
220 eq_gain_tlv),
221 SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
222 eq_gain_tlv),
223
224 /* ADC Equalizer controls */
225 SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
226 SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
227 1, adc_eq_master_gain_tlv),
228 SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
229 eq_gain_tlv),
230 SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
231 eq_gain_tlv),
232 SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
233 eq_gain_tlv),
234 SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
235 eq_gain_tlv),
236 SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
237 eq_gain_tlv),
Ashish Chavan4ced2b92011-10-15 14:50:06 +0530238
239 SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
240 SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
241 SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
242 SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
243
244 SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
245 SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
246 SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
247 SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
Ashish Chavan5eda1942011-10-19 14:19:06 +0530248
249 /* Mute controls */
250 SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
251 SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
252 SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
253 SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
254 SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
255
256 /* Zero cross controls */
257 SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
258 SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
259 SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
260 SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
261
262 SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
Kuninori Morimotoa7e7cd52010-07-21 14:12:16 +0900263};
264
Kuninori Morimoto98615452009-12-14 13:21:56 +0900265/* Codec private data */
266struct da7210_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000267 enum snd_soc_control_type control_type;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900268};
269
Kuninori Morimoto98615452009-12-14 13:21:56 +0900270/*
271 * Register cache
272 */
273static const u8 da7210_reg[] = {
274 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
275 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
276 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
277 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
278 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
279 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
280 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
281 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
282 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
283 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
284 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
285 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
286 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
287 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
288 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
289 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
290 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
291 0x00, /* R88 */
292};
293
Axel Lin40a49712011-10-12 07:16:25 +0800294static int da7210_volatile_register(struct snd_soc_codec *codec,
295 unsigned int reg)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900296{
Axel Lin40a49712011-10-12 07:16:25 +0800297 switch (reg) {
298 case DA7210_STATUS:
299 return 1;
300 default:
301 return 0;
302 }
Kuninori Morimoto98615452009-12-14 13:21:56 +0900303}
Kuninori Morimoto98615452009-12-14 13:21:56 +0900304static int da7210_startup(struct snd_pcm_substream *substream,
305 struct snd_soc_dai *dai)
306{
307 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
308 struct snd_soc_codec *codec = dai->codec;
309
310 if (is_play) {
Kuninori Morimoto98615452009-12-14 13:21:56 +0900311 /* Enable Out */
312 snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
313 snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
314
315 } else {
316 /* Volume 7 */
317 snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
318 snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
319
320 /* Enable Mic */
321 snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
322 snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
323 }
324
325 return 0;
326}
327
328/*
329 * Set PCM DAI word length.
330 */
331static int da7210_hw_params(struct snd_pcm_substream *substream,
332 struct snd_pcm_hw_params *params,
333 struct snd_soc_dai *dai)
334{
335 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000336 struct snd_soc_codec *codec = rtd->codec;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900337 u32 dai_cfg1;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900338 u32 fs, bypass;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900339
340 /* set DAI source to Left and Right ADC */
Axel Lin40a49712011-10-12 07:16:25 +0800341 snd_soc_write(codec, DA7210_DAI_SRC_SEL,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900342 DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
343
344 /* Enable DAI */
Axel Lin40a49712011-10-12 07:16:25 +0800345 snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900346
Axel Lin40a49712011-10-12 07:16:25 +0800347 dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900348
349 switch (params_format(params)) {
350 case SNDRV_PCM_FORMAT_S16_LE:
351 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
352 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530353 case SNDRV_PCM_FORMAT_S20_3LE:
354 dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
355 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900356 case SNDRV_PCM_FORMAT_S24_LE:
357 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
358 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530359 case SNDRV_PCM_FORMAT_S32_LE:
360 dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
361 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900362 default:
363 return -EINVAL;
364 }
365
Axel Lin40a49712011-10-12 07:16:25 +0800366 snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900367
Kuninori Morimoto98615452009-12-14 13:21:56 +0900368 switch (params_rate(params)) {
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900369 case 8000:
370 fs = DA7210_PLL_FS_8000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900371 bypass = DA7210_PLL_BYP;
372 break;
373 case 11025:
374 fs = DA7210_PLL_FS_11025;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900375 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900376 break;
377 case 12000:
378 fs = DA7210_PLL_FS_12000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900379 bypass = DA7210_PLL_BYP;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900380 break;
381 case 16000:
382 fs = DA7210_PLL_FS_16000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900383 bypass = DA7210_PLL_BYP;
384 break;
385 case 22050:
386 fs = DA7210_PLL_FS_22050;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900387 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900388 break;
389 case 32000:
390 fs = DA7210_PLL_FS_32000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900391 bypass = DA7210_PLL_BYP;
392 break;
393 case 44100:
394 fs = DA7210_PLL_FS_44100;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900395 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900396 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900397 case 48000:
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900398 fs = DA7210_PLL_FS_48000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900399 bypass = DA7210_PLL_BYP;
400 break;
401 case 88200:
402 fs = DA7210_PLL_FS_88200;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900403 bypass = 0;
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900404 break;
405 case 96000:
406 fs = DA7210_PLL_FS_96000;
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900407 bypass = DA7210_PLL_BYP;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900408 break;
409 default:
410 return -EINVAL;
411 }
412
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900413 /* Disable active mode */
414 snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
415
Kuninori Morimoto3a9d6202010-03-01 10:10:43 +0900416 snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900417 snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
418
419 /* Enable active mode */
420 snd_soc_update_bits(codec, DA7210_STARTUP1,
421 DA7210_SC_MST_EN, DA7210_SC_MST_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900422
423 return 0;
424}
425
426/*
427 * Set DAI mode and Format
428 */
429static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
430{
431 struct snd_soc_codec *codec = codec_dai->codec;
432 u32 dai_cfg1;
433 u32 dai_cfg3;
434
Axel Lin40a49712011-10-12 07:16:25 +0800435 dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
436 dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900437
438 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
439 case SND_SOC_DAIFMT_CBM_CFM:
440 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
441 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530442 case SND_SOC_DAIFMT_CBS_CFS:
443 dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
444 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900445 default:
446 return -EINVAL;
447 }
448
449 /* FIXME
450 *
451 * It support I2S only now
452 */
453 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
454 case SND_SOC_DAIFMT_I2S:
455 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
456 break;
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530457 case SND_SOC_DAIFMT_LEFT_J:
458 dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
459 break;
460 case SND_SOC_DAIFMT_RIGHT_J:
461 dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
462 break;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900463 default:
464 return -EINVAL;
465 }
466
467 /* FIXME
468 *
469 * It support 64bit data transmission only now
470 */
471 dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
472
Axel Lin40a49712011-10-12 07:16:25 +0800473 snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
474 snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900475
476 return 0;
477}
478
Ashish Chavan5eda1942011-10-19 14:19:06 +0530479static int da7210_mute(struct snd_soc_dai *dai, int mute)
480{
481 struct snd_soc_codec *codec = dai->codec;
482 u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
483
484 if (mute)
485 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
486 else
487 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
488 return 0;
489}
490
Ashish Chavan0f8ea582011-10-12 20:33:21 +0530491#define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
492 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900493
494/* DAI operations */
495static struct snd_soc_dai_ops da7210_dai_ops = {
496 .startup = da7210_startup,
497 .hw_params = da7210_hw_params,
498 .set_fmt = da7210_set_dai_fmt,
Ashish Chavan5eda1942011-10-19 14:19:06 +0530499 .digital_mute = da7210_mute,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900500};
501
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502static struct snd_soc_dai_driver da7210_dai = {
503 .name = "da7210-hifi",
Kuninori Morimoto98615452009-12-14 13:21:56 +0900504 /* playback capabilities */
505 .playback = {
506 .stream_name = "Playback",
507 .channels_min = 1,
508 .channels_max = 2,
509 .rates = SNDRV_PCM_RATE_8000_96000,
510 .formats = DA7210_FORMATS,
511 },
512 /* capture capabilities */
513 .capture = {
514 .stream_name = "Capture",
515 .channels_min = 1,
516 .channels_max = 2,
517 .rates = SNDRV_PCM_RATE_8000_96000,
518 .formats = DA7210_FORMATS,
519 },
520 .ops = &da7210_dai_ops,
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900521 .symmetric_rates = 1,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900522};
Kuninori Morimoto98615452009-12-14 13:21:56 +0900523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524static int da7210_probe(struct snd_soc_codec *codec)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900525{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
Axel Lin40a49712011-10-12 07:16:25 +0800527 int ret;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900528
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900530
Axel Lin40a49712011-10-12 07:16:25 +0800531 ret = snd_soc_codec_set_cache_io(codec, 8, 8, da7210->control_type);
532 if (ret < 0) {
533 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
534 return ret;
535 }
Kuninori Morimoto98615452009-12-14 13:21:56 +0900536
537 /* FIXME
538 *
539 * This driver use fixed value here
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900540 * And below settings expects MCLK = 12.288MHz
541 *
542 * When you select different MCLK, please check...
543 * DA7210_PLL_DIV1 val
544 * DA7210_PLL_DIV2 val
545 * DA7210_PLL_DIV3 val
546 * DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
Kuninori Morimoto98615452009-12-14 13:21:56 +0900547 */
548
549 /*
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900550 * make sure that DA7210 use bypass mode before start up
551 */
Axel Lin40a49712011-10-12 07:16:25 +0800552 snd_soc_write(codec, DA7210_STARTUP1, 0);
553 snd_soc_write(codec, DA7210_PLL_DIV3,
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900554 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
555
556 /*
Kuninori Morimoto98615452009-12-14 13:21:56 +0900557 * ADC settings
558 */
559
560 /* Enable Left & Right MIC PGA and Mic Bias */
Axel Lin40a49712011-10-12 07:16:25 +0800561 snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
562 snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900563
564 /* Enable Left and Right input PGA */
Axel Lin40a49712011-10-12 07:16:25 +0800565 snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
566 snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900567
568 /* Enable Left and Right ADC */
Axel Lin40a49712011-10-12 07:16:25 +0800569 snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900570
571 /*
572 * DAC settings
573 */
574
575 /* Enable Left and Right DAC */
Axel Lin40a49712011-10-12 07:16:25 +0800576 snd_soc_write(codec, DA7210_DAC_SEL,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900577 DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
578 DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
579
580 /* Enable Left and Right out PGA */
Axel Lin40a49712011-10-12 07:16:25 +0800581 snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
582 snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900583
584 /* Enable Left and Right HeadPhone PGA */
Axel Lin40a49712011-10-12 07:16:25 +0800585 snd_soc_write(codec, DA7210_HP_CFG,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900586 DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
587 DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
588
Ashish Chavan5eda1942011-10-19 14:19:06 +0530589 /* Enable ramp mode for DAC gain update */
590 snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
591
Kuninori Morimoto98615452009-12-14 13:21:56 +0900592 /* Diable PLL and bypass it */
Axel Lin40a49712011-10-12 07:16:25 +0800593 snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900594
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900595 /*
596 * If 48kHz sound came, it use bypass mode,
597 * and when it is 44.1kHz, it use PLL.
598 *
599 * This time, this driver sets PLL always ON
600 * and controls bypass/PLL mode by switching
601 * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
602 * see da7210_hw_params
603 */
Axel Lin40a49712011-10-12 07:16:25 +0800604 snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
605 snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
606 snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
Kuninori Morimoto98615452009-12-14 13:21:56 +0900607 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
Kuninori Morimoto960b3b42010-03-11 11:37:44 +0900608 snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
609
610 /* As suggested by Dialog */
Axel Lin40a49712011-10-12 07:16:25 +0800611 snd_soc_write(codec, DA7210_A_HID_UNLOCK, 0x8B); /* unlock */
612 snd_soc_write(codec, DA7210_A_TEST_UNLOCK, 0xB4);
613 snd_soc_write(codec, DA7210_A_PLL1, 0x01);
614 snd_soc_write(codec, DA7210_A_CP_MODE, 0x7C);
615 snd_soc_write(codec, DA7210_A_HID_UNLOCK, 0x00); /* re-lock */
616 snd_soc_write(codec, DA7210_A_TEST_UNLOCK, 0x00);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900617
618 /* Activate all enabled subsystem */
Axel Lin40a49712011-10-12 07:16:25 +0800619 snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900622
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 return 0;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900624}
625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +0900627 .probe = da7210_probe,
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +0900628 .reg_cache_size = ARRAY_SIZE(da7210_reg),
629 .reg_word_size = sizeof(u8),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 .reg_cache_default = da7210_reg,
Axel Lin40a49712011-10-12 07:16:25 +0800631 .volatile_register = da7210_volatile_register,
Mark Browna6f096f2011-10-14 20:18:49 +0100632
633 .controls = da7210_snd_controls,
634 .num_controls = ARRAY_SIZE(da7210_snd_controls),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635};
636
Kuninori Morimoto98615452009-12-14 13:21:56 +0900637#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brown735fe4c2010-01-12 14:13:00 +0000638static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
639 const struct i2c_device_id *id)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900640{
641 struct da7210_priv *da7210;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900642 int ret;
643
644 da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
645 if (!da7210)
646 return -ENOMEM;
647
Kuninori Morimoto98615452009-12-14 13:21:56 +0900648 i2c_set_clientdata(i2c, da7210);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 da7210->control_type = SND_SOC_I2C;
Kuninori Morimoto98615452009-12-14 13:21:56 +0900650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651 ret = snd_soc_register_codec(&i2c->dev,
652 &soc_codec_dev_da7210, &da7210_dai, 1);
653 if (ret < 0)
Axel Lin085efd22010-07-23 05:53:45 +0000654 kfree(da7210);
Kuninori Morimoto98615452009-12-14 13:21:56 +0900655
656 return ret;
657}
658
Mark Brown735fe4c2010-01-12 14:13:00 +0000659static int __devexit da7210_i2c_remove(struct i2c_client *client)
Kuninori Morimoto98615452009-12-14 13:21:56 +0900660{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 snd_soc_unregister_codec(&client->dev);
662 kfree(i2c_get_clientdata(client));
Kuninori Morimoto98615452009-12-14 13:21:56 +0900663 return 0;
664}
665
666static const struct i2c_device_id da7210_i2c_id[] = {
667 { "da7210", 0 },
668 { }
669};
670MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
671
672/* I2C codec control layer */
673static struct i2c_driver da7210_i2c_driver = {
674 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 .name = "da7210-codec",
Kuninori Morimoto98615452009-12-14 13:21:56 +0900676 .owner = THIS_MODULE,
677 },
Kuninori Morimoto4c62ed92010-09-16 13:07:06 +0900678 .probe = da7210_i2c_probe,
679 .remove = __devexit_p(da7210_i2c_remove),
680 .id_table = da7210_i2c_id,
Kuninori Morimoto98615452009-12-14 13:21:56 +0900681};
682#endif
683
Kuninori Morimoto98615452009-12-14 13:21:56 +0900684static int __init da7210_modinit(void)
685{
686 int ret = 0;
687#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
688 ret = i2c_add_driver(&da7210_i2c_driver);
689#endif
690 return ret;
691}
692module_init(da7210_modinit);
693
694static void __exit da7210_exit(void)
695{
696#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
697 i2c_del_driver(&da7210_i2c_driver);
698#endif
699}
700module_exit(da7210_exit);
701
702MODULE_DESCRIPTION("ASoC DA7210 driver");
703MODULE_AUTHOR("David Chen, Kuninori Morimoto");
704MODULE_LICENSE("GPL");