blob: 926797a014c75716f069db87d03a1f7dbd3e43ba [file] [log] [blame]
Joonyoung Shim2312fd82009-09-10 00:12:43 +09001/*
2 * ak4671.c -- audio driver for AK4671
3 *
4 * Copyright (C) 2009 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/i2c.h>
17#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Joonyoung Shim2312fd82009-09-10 00:12:43 +090019#include <sound/soc.h>
20#include <sound/soc-dapm.h>
21#include <sound/initval.h>
22#include <sound/tlv.h>
23
24#include "ak4671.h"
25
26static struct snd_soc_codec *ak4671_codec;
27
28/* codec private data */
29struct ak4671_priv {
30 struct snd_soc_codec codec;
31 u8 reg_cache[AK4671_CACHEREGNUM];
32};
33
34/* ak4671 register cache & default register settings */
35static const u8 ak4671_reg[AK4671_CACHEREGNUM] = {
36 0x00, /* AK4671_AD_DA_POWER_MANAGEMENT (0x00) */
37 0xf6, /* AK4671_PLL_MODE_SELECT0 (0x01) */
38 0x00, /* AK4671_PLL_MODE_SELECT1 (0x02) */
39 0x02, /* AK4671_FORMAT_SELECT (0x03) */
40 0x00, /* AK4671_MIC_SIGNAL_SELECT (0x04) */
41 0x55, /* AK4671_MIC_AMP_GAIN (0x05) */
42 0x00, /* AK4671_MIXING_POWER_MANAGEMENT0 (0x06) */
43 0x00, /* AK4671_MIXING_POWER_MANAGEMENT1 (0x07) */
44 0xb5, /* AK4671_OUTPUT_VOLUME_CONTROL (0x08) */
45 0x00, /* AK4671_LOUT1_SIGNAL_SELECT (0x09) */
46 0x00, /* AK4671_ROUT1_SIGNAL_SELECT (0x0a) */
47 0x00, /* AK4671_LOUT2_SIGNAL_SELECT (0x0b) */
48 0x00, /* AK4671_ROUT2_SIGNAL_SELECT (0x0c) */
49 0x00, /* AK4671_LOUT3_SIGNAL_SELECT (0x0d) */
50 0x00, /* AK4671_ROUT3_SIGNAL_SELECT (0x0e) */
51 0x00, /* AK4671_LOUT1_POWER_MANAGERMENT (0x0f) */
52 0x00, /* AK4671_LOUT2_POWER_MANAGERMENT (0x10) */
53 0x80, /* AK4671_LOUT3_POWER_MANAGERMENT (0x11) */
54 0x91, /* AK4671_LCH_INPUT_VOLUME_CONTROL (0x12) */
55 0x91, /* AK4671_RCH_INPUT_VOLUME_CONTROL (0x13) */
56 0xe1, /* AK4671_ALC_REFERENCE_SELECT (0x14) */
57 0x00, /* AK4671_DIGITAL_MIXING_CONTROL (0x15) */
58 0x00, /* AK4671_ALC_TIMER_SELECT (0x16) */
59 0x00, /* AK4671_ALC_MODE_CONTROL (0x17) */
60 0x02, /* AK4671_MODE_CONTROL1 (0x18) */
61 0x01, /* AK4671_MODE_CONTROL2 (0x19) */
62 0x18, /* AK4671_LCH_OUTPUT_VOLUME_CONTROL (0x1a) */
63 0x18, /* AK4671_RCH_OUTPUT_VOLUME_CONTROL (0x1b) */
64 0x00, /* AK4671_SIDETONE_A_CONTROL (0x1c) */
65 0x02, /* AK4671_DIGITAL_FILTER_SELECT (0x1d) */
66 0x00, /* AK4671_FIL3_COEFFICIENT0 (0x1e) */
67 0x00, /* AK4671_FIL3_COEFFICIENT1 (0x1f) */
68 0x00, /* AK4671_FIL3_COEFFICIENT2 (0x20) */
69 0x00, /* AK4671_FIL3_COEFFICIENT3 (0x21) */
70 0x00, /* AK4671_EQ_COEFFICIENT0 (0x22) */
71 0x00, /* AK4671_EQ_COEFFICIENT1 (0x23) */
72 0x00, /* AK4671_EQ_COEFFICIENT2 (0x24) */
73 0x00, /* AK4671_EQ_COEFFICIENT3 (0x25) */
74 0x00, /* AK4671_EQ_COEFFICIENT4 (0x26) */
75 0x00, /* AK4671_EQ_COEFFICIENT5 (0x27) */
76 0xa9, /* AK4671_FIL1_COEFFICIENT0 (0x28) */
77 0x1f, /* AK4671_FIL1_COEFFICIENT1 (0x29) */
78 0xad, /* AK4671_FIL1_COEFFICIENT2 (0x2a) */
79 0x20, /* AK4671_FIL1_COEFFICIENT3 (0x2b) */
80 0x00, /* AK4671_FIL2_COEFFICIENT0 (0x2c) */
81 0x00, /* AK4671_FIL2_COEFFICIENT1 (0x2d) */
82 0x00, /* AK4671_FIL2_COEFFICIENT2 (0x2e) */
83 0x00, /* AK4671_FIL2_COEFFICIENT3 (0x2f) */
84 0x00, /* AK4671_DIGITAL_FILTER_SELECT2 (0x30) */
85 0x00, /* this register not used */
86 0x00, /* AK4671_E1_COEFFICIENT0 (0x32) */
87 0x00, /* AK4671_E1_COEFFICIENT1 (0x33) */
88 0x00, /* AK4671_E1_COEFFICIENT2 (0x34) */
89 0x00, /* AK4671_E1_COEFFICIENT3 (0x35) */
90 0x00, /* AK4671_E1_COEFFICIENT4 (0x36) */
91 0x00, /* AK4671_E1_COEFFICIENT5 (0x37) */
92 0x00, /* AK4671_E2_COEFFICIENT0 (0x38) */
93 0x00, /* AK4671_E2_COEFFICIENT1 (0x39) */
94 0x00, /* AK4671_E2_COEFFICIENT2 (0x3a) */
95 0x00, /* AK4671_E2_COEFFICIENT3 (0x3b) */
96 0x00, /* AK4671_E2_COEFFICIENT4 (0x3c) */
97 0x00, /* AK4671_E2_COEFFICIENT5 (0x3d) */
98 0x00, /* AK4671_E3_COEFFICIENT0 (0x3e) */
99 0x00, /* AK4671_E3_COEFFICIENT1 (0x3f) */
100 0x00, /* AK4671_E3_COEFFICIENT2 (0x40) */
101 0x00, /* AK4671_E3_COEFFICIENT3 (0x41) */
102 0x00, /* AK4671_E3_COEFFICIENT4 (0x42) */
103 0x00, /* AK4671_E3_COEFFICIENT5 (0x43) */
104 0x00, /* AK4671_E4_COEFFICIENT0 (0x44) */
105 0x00, /* AK4671_E4_COEFFICIENT1 (0x45) */
106 0x00, /* AK4671_E4_COEFFICIENT2 (0x46) */
107 0x00, /* AK4671_E4_COEFFICIENT3 (0x47) */
108 0x00, /* AK4671_E4_COEFFICIENT4 (0x48) */
109 0x00, /* AK4671_E4_COEFFICIENT5 (0x49) */
110 0x00, /* AK4671_E5_COEFFICIENT0 (0x4a) */
111 0x00, /* AK4671_E5_COEFFICIENT1 (0x4b) */
112 0x00, /* AK4671_E5_COEFFICIENT2 (0x4c) */
113 0x00, /* AK4671_E5_COEFFICIENT3 (0x4d) */
114 0x00, /* AK4671_E5_COEFFICIENT4 (0x4e) */
115 0x00, /* AK4671_E5_COEFFICIENT5 (0x4f) */
116 0x88, /* AK4671_EQ_CONTROL_250HZ_100HZ (0x50) */
117 0x88, /* AK4671_EQ_CONTROL_3500HZ_1KHZ (0x51) */
118 0x08, /* AK4671_EQ_CONTRO_10KHZ (0x52) */
119 0x00, /* AK4671_PCM_IF_CONTROL0 (0x53) */
120 0x00, /* AK4671_PCM_IF_CONTROL1 (0x54) */
121 0x00, /* AK4671_PCM_IF_CONTROL2 (0x55) */
122 0x18, /* AK4671_DIGITAL_VOLUME_B_CONTROL (0x56) */
123 0x18, /* AK4671_DIGITAL_VOLUME_C_CONTROL (0x57) */
124 0x00, /* AK4671_SIDETONE_VOLUME_CONTROL (0x58) */
125 0x00, /* AK4671_DIGITAL_MIXING_CONTROL2 (0x59) */
126 0x00, /* AK4671_SAR_ADC_CONTROL (0x5a) */
127};
128
129/*
130 * LOUT1/ROUT1 output volume control:
131 * from -24 to 6 dB in 6 dB steps (mute instead of -30 dB)
132 */
133static DECLARE_TLV_DB_SCALE(out1_tlv, -3000, 600, 1);
134
135/*
136 * LOUT2/ROUT2 output volume control:
137 * from -33 to 6 dB in 3 dB steps (mute instead of -33 dB)
138 */
139static DECLARE_TLV_DB_SCALE(out2_tlv, -3300, 300, 1);
140
141/*
142 * LOUT3/ROUT3 output volume control:
143 * from -6 to 3 dB in 3 dB steps
144 */
145static DECLARE_TLV_DB_SCALE(out3_tlv, -600, 300, 0);
146
147/*
148 * Mic amp gain control:
149 * from -15 to 30 dB in 3 dB steps
150 * REVISIT: The actual min value(0x01) is -12 dB and the reg value 0x00 is not
151 * available
152 */
153static DECLARE_TLV_DB_SCALE(mic_amp_tlv, -1500, 300, 0);
154
155static const struct snd_kcontrol_new ak4671_snd_controls[] = {
156 /* Common playback gain controls */
157 SOC_SINGLE_TLV("Line Output1 Playback Volume",
158 AK4671_OUTPUT_VOLUME_CONTROL, 0, 0x6, 0, out1_tlv),
159 SOC_SINGLE_TLV("Headphone Output2 Playback Volume",
160 AK4671_OUTPUT_VOLUME_CONTROL, 4, 0xd, 0, out2_tlv),
161 SOC_SINGLE_TLV("Line Output3 Playback Volume",
162 AK4671_LOUT3_POWER_MANAGERMENT, 6, 0x3, 0, out3_tlv),
163
164 /* Common capture gain controls */
165 SOC_DOUBLE_TLV("Mic Amp Capture Volume",
166 AK4671_MIC_AMP_GAIN, 0, 4, 0xf, 0, mic_amp_tlv),
167};
168
169/* event handlers */
170static int ak4671_out2_event(struct snd_soc_dapm_widget *w,
171 struct snd_kcontrol *kcontrol, int event)
172{
173 struct snd_soc_codec *codec = w->codec;
174 u8 reg;
175
176 switch (event) {
177 case SND_SOC_DAPM_POST_PMU:
178 reg = snd_soc_read(codec, AK4671_LOUT2_POWER_MANAGERMENT);
179 reg |= AK4671_MUTEN;
180 snd_soc_write(codec, AK4671_LOUT2_POWER_MANAGERMENT, reg);
181 break;
182 case SND_SOC_DAPM_PRE_PMD:
183 reg = snd_soc_read(codec, AK4671_LOUT2_POWER_MANAGERMENT);
184 reg &= ~AK4671_MUTEN;
185 snd_soc_write(codec, AK4671_LOUT2_POWER_MANAGERMENT, reg);
186 break;
187 }
188
189 return 0;
190}
191
192/* Output Mixers */
193static const struct snd_kcontrol_new ak4671_lout1_mixer_controls[] = {
194 SOC_DAPM_SINGLE("DACL", AK4671_LOUT1_SIGNAL_SELECT, 0, 1, 0),
195 SOC_DAPM_SINGLE("LINL1", AK4671_LOUT1_SIGNAL_SELECT, 1, 1, 0),
196 SOC_DAPM_SINGLE("LINL2", AK4671_LOUT1_SIGNAL_SELECT, 2, 1, 0),
197 SOC_DAPM_SINGLE("LINL3", AK4671_LOUT1_SIGNAL_SELECT, 3, 1, 0),
198 SOC_DAPM_SINGLE("LINL4", AK4671_LOUT1_SIGNAL_SELECT, 4, 1, 0),
199 SOC_DAPM_SINGLE("LOOPL", AK4671_LOUT1_SIGNAL_SELECT, 5, 1, 0),
200};
201
202static const struct snd_kcontrol_new ak4671_rout1_mixer_controls[] = {
203 SOC_DAPM_SINGLE("DACR", AK4671_ROUT1_SIGNAL_SELECT, 0, 1, 0),
204 SOC_DAPM_SINGLE("RINR1", AK4671_ROUT1_SIGNAL_SELECT, 1, 1, 0),
205 SOC_DAPM_SINGLE("RINR2", AK4671_ROUT1_SIGNAL_SELECT, 2, 1, 0),
206 SOC_DAPM_SINGLE("RINR3", AK4671_ROUT1_SIGNAL_SELECT, 3, 1, 0),
207 SOC_DAPM_SINGLE("RINR4", AK4671_ROUT1_SIGNAL_SELECT, 4, 1, 0),
208 SOC_DAPM_SINGLE("LOOPR", AK4671_ROUT1_SIGNAL_SELECT, 5, 1, 0),
209};
210
211static const struct snd_kcontrol_new ak4671_lout2_mixer_controls[] = {
212 SOC_DAPM_SINGLE("DACHL", AK4671_LOUT2_SIGNAL_SELECT, 0, 1, 0),
213 SOC_DAPM_SINGLE("LINH1", AK4671_LOUT2_SIGNAL_SELECT, 1, 1, 0),
214 SOC_DAPM_SINGLE("LINH2", AK4671_LOUT2_SIGNAL_SELECT, 2, 1, 0),
215 SOC_DAPM_SINGLE("LINH3", AK4671_LOUT2_SIGNAL_SELECT, 3, 1, 0),
216 SOC_DAPM_SINGLE("LINH4", AK4671_LOUT2_SIGNAL_SELECT, 4, 1, 0),
217 SOC_DAPM_SINGLE("LOOPHL", AK4671_LOUT2_SIGNAL_SELECT, 5, 1, 0),
218};
219
220static const struct snd_kcontrol_new ak4671_rout2_mixer_controls[] = {
221 SOC_DAPM_SINGLE("DACHR", AK4671_ROUT2_SIGNAL_SELECT, 0, 1, 0),
222 SOC_DAPM_SINGLE("RINH1", AK4671_ROUT2_SIGNAL_SELECT, 1, 1, 0),
223 SOC_DAPM_SINGLE("RINH2", AK4671_ROUT2_SIGNAL_SELECT, 2, 1, 0),
224 SOC_DAPM_SINGLE("RINH3", AK4671_ROUT2_SIGNAL_SELECT, 3, 1, 0),
225 SOC_DAPM_SINGLE("RINH4", AK4671_ROUT2_SIGNAL_SELECT, 4, 1, 0),
226 SOC_DAPM_SINGLE("LOOPHR", AK4671_ROUT2_SIGNAL_SELECT, 5, 1, 0),
227};
228
229static const struct snd_kcontrol_new ak4671_lout3_mixer_controls[] = {
230 SOC_DAPM_SINGLE("DACSL", AK4671_LOUT3_SIGNAL_SELECT, 0, 1, 0),
231 SOC_DAPM_SINGLE("LINS1", AK4671_LOUT3_SIGNAL_SELECT, 1, 1, 0),
232 SOC_DAPM_SINGLE("LINS2", AK4671_LOUT3_SIGNAL_SELECT, 2, 1, 0),
233 SOC_DAPM_SINGLE("LINS3", AK4671_LOUT3_SIGNAL_SELECT, 3, 1, 0),
234 SOC_DAPM_SINGLE("LINS4", AK4671_LOUT3_SIGNAL_SELECT, 4, 1, 0),
235 SOC_DAPM_SINGLE("LOOPSL", AK4671_LOUT3_SIGNAL_SELECT, 5, 1, 0),
236};
237
238static const struct snd_kcontrol_new ak4671_rout3_mixer_controls[] = {
239 SOC_DAPM_SINGLE("DACSR", AK4671_ROUT3_SIGNAL_SELECT, 0, 1, 0),
240 SOC_DAPM_SINGLE("RINS1", AK4671_ROUT3_SIGNAL_SELECT, 1, 1, 0),
241 SOC_DAPM_SINGLE("RINS2", AK4671_ROUT3_SIGNAL_SELECT, 2, 1, 0),
242 SOC_DAPM_SINGLE("RINS3", AK4671_ROUT3_SIGNAL_SELECT, 3, 1, 0),
243 SOC_DAPM_SINGLE("RINS4", AK4671_ROUT3_SIGNAL_SELECT, 4, 1, 0),
244 SOC_DAPM_SINGLE("LOOPSR", AK4671_ROUT3_SIGNAL_SELECT, 5, 1, 0),
245};
246
247/* Input MUXs */
248static const char *ak4671_lin_mux_texts[] =
249 {"LIN1", "LIN2", "LIN3", "LIN4"};
250static const struct soc_enum ak4671_lin_mux_enum =
251 SOC_ENUM_SINGLE(AK4671_MIC_SIGNAL_SELECT, 0,
252 ARRAY_SIZE(ak4671_lin_mux_texts),
253 ak4671_lin_mux_texts);
254static const struct snd_kcontrol_new ak4671_lin_mux_control =
255 SOC_DAPM_ENUM("Route", ak4671_lin_mux_enum);
256
257static const char *ak4671_rin_mux_texts[] =
258 {"RIN1", "RIN2", "RIN3", "RIN4"};
259static const struct soc_enum ak4671_rin_mux_enum =
260 SOC_ENUM_SINGLE(AK4671_MIC_SIGNAL_SELECT, 2,
261 ARRAY_SIZE(ak4671_rin_mux_texts),
262 ak4671_rin_mux_texts);
263static const struct snd_kcontrol_new ak4671_rin_mux_control =
264 SOC_DAPM_ENUM("Route", ak4671_rin_mux_enum);
265
266static const struct snd_soc_dapm_widget ak4671_dapm_widgets[] = {
267 /* Inputs */
268 SND_SOC_DAPM_INPUT("LIN1"),
269 SND_SOC_DAPM_INPUT("RIN1"),
270 SND_SOC_DAPM_INPUT("LIN2"),
271 SND_SOC_DAPM_INPUT("RIN2"),
272 SND_SOC_DAPM_INPUT("LIN3"),
273 SND_SOC_DAPM_INPUT("RIN3"),
274 SND_SOC_DAPM_INPUT("LIN4"),
275 SND_SOC_DAPM_INPUT("RIN4"),
276
277 /* Outputs */
278 SND_SOC_DAPM_OUTPUT("LOUT1"),
279 SND_SOC_DAPM_OUTPUT("ROUT1"),
280 SND_SOC_DAPM_OUTPUT("LOUT2"),
281 SND_SOC_DAPM_OUTPUT("ROUT2"),
282 SND_SOC_DAPM_OUTPUT("LOUT3"),
283 SND_SOC_DAPM_OUTPUT("ROUT3"),
284
285 /* DAC */
286 SND_SOC_DAPM_DAC("DAC Left", "Left HiFi Playback",
287 AK4671_AD_DA_POWER_MANAGEMENT, 6, 0),
288 SND_SOC_DAPM_DAC("DAC Right", "Right HiFi Playback",
289 AK4671_AD_DA_POWER_MANAGEMENT, 7, 0),
290
291 /* ADC */
292 SND_SOC_DAPM_ADC("ADC Left", "Left HiFi Capture",
293 AK4671_AD_DA_POWER_MANAGEMENT, 4, 0),
294 SND_SOC_DAPM_ADC("ADC Right", "Right HiFi Capture",
295 AK4671_AD_DA_POWER_MANAGEMENT, 5, 0),
296
297 /* PGA */
298 SND_SOC_DAPM_PGA("LOUT2 Mix Amp",
299 AK4671_LOUT2_POWER_MANAGERMENT, 5, 0, NULL, 0),
300 SND_SOC_DAPM_PGA("ROUT2 Mix Amp",
301 AK4671_LOUT2_POWER_MANAGERMENT, 6, 0, NULL, 0),
302
303 SND_SOC_DAPM_PGA("LIN1 Mixing Circuit",
304 AK4671_MIXING_POWER_MANAGEMENT1, 0, 0, NULL, 0),
305 SND_SOC_DAPM_PGA("RIN1 Mixing Circuit",
306 AK4671_MIXING_POWER_MANAGEMENT1, 1, 0, NULL, 0),
307 SND_SOC_DAPM_PGA("LIN2 Mixing Circuit",
308 AK4671_MIXING_POWER_MANAGEMENT1, 2, 0, NULL, 0),
309 SND_SOC_DAPM_PGA("RIN2 Mixing Circuit",
310 AK4671_MIXING_POWER_MANAGEMENT1, 3, 0, NULL, 0),
311 SND_SOC_DAPM_PGA("LIN3 Mixing Circuit",
312 AK4671_MIXING_POWER_MANAGEMENT1, 4, 0, NULL, 0),
313 SND_SOC_DAPM_PGA("RIN3 Mixing Circuit",
314 AK4671_MIXING_POWER_MANAGEMENT1, 5, 0, NULL, 0),
315 SND_SOC_DAPM_PGA("LIN4 Mixing Circuit",
316 AK4671_MIXING_POWER_MANAGEMENT1, 6, 0, NULL, 0),
317 SND_SOC_DAPM_PGA("RIN4 Mixing Circuit",
318 AK4671_MIXING_POWER_MANAGEMENT1, 7, 0, NULL, 0),
319
320 /* Output Mixers */
321 SND_SOC_DAPM_MIXER("LOUT1 Mixer", AK4671_LOUT1_POWER_MANAGERMENT, 0, 0,
322 &ak4671_lout1_mixer_controls[0],
323 ARRAY_SIZE(ak4671_lout1_mixer_controls)),
324 SND_SOC_DAPM_MIXER("ROUT1 Mixer", AK4671_LOUT1_POWER_MANAGERMENT, 1, 0,
325 &ak4671_rout1_mixer_controls[0],
326 ARRAY_SIZE(ak4671_rout1_mixer_controls)),
327 SND_SOC_DAPM_MIXER_E("LOUT2 Mixer", AK4671_LOUT2_POWER_MANAGERMENT,
328 0, 0, &ak4671_lout2_mixer_controls[0],
329 ARRAY_SIZE(ak4671_lout2_mixer_controls),
330 ak4671_out2_event,
331 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_PRE_PMD),
332 SND_SOC_DAPM_MIXER_E("ROUT2 Mixer", AK4671_LOUT2_POWER_MANAGERMENT,
333 1, 0, &ak4671_rout2_mixer_controls[0],
334 ARRAY_SIZE(ak4671_rout2_mixer_controls),
335 ak4671_out2_event,
336 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_PRE_PMD),
337 SND_SOC_DAPM_MIXER("LOUT3 Mixer", AK4671_LOUT3_POWER_MANAGERMENT, 0, 0,
338 &ak4671_lout3_mixer_controls[0],
339 ARRAY_SIZE(ak4671_lout3_mixer_controls)),
340 SND_SOC_DAPM_MIXER("ROUT3 Mixer", AK4671_LOUT3_POWER_MANAGERMENT, 1, 0,
341 &ak4671_rout3_mixer_controls[0],
342 ARRAY_SIZE(ak4671_rout3_mixer_controls)),
343
344 /* Input MUXs */
345 SND_SOC_DAPM_MUX("LIN MUX", AK4671_AD_DA_POWER_MANAGEMENT, 2, 0,
346 &ak4671_lin_mux_control),
347 SND_SOC_DAPM_MUX("RIN MUX", AK4671_AD_DA_POWER_MANAGEMENT, 3, 0,
348 &ak4671_rin_mux_control),
349
350 /* Mic Power */
351 SND_SOC_DAPM_MICBIAS("Mic Bias", AK4671_AD_DA_POWER_MANAGEMENT, 1, 0),
352
353 /* Supply */
354 SND_SOC_DAPM_SUPPLY("PMPLL", AK4671_PLL_MODE_SELECT1, 0, 0, NULL, 0),
355};
356
357static const struct snd_soc_dapm_route intercon[] = {
358 {"DAC Left", "NULL", "PMPLL"},
359 {"DAC Right", "NULL", "PMPLL"},
360 {"ADC Left", "NULL", "PMPLL"},
361 {"ADC Right", "NULL", "PMPLL"},
362
363 /* Outputs */
364 {"LOUT1", "NULL", "LOUT1 Mixer"},
365 {"ROUT1", "NULL", "ROUT1 Mixer"},
366 {"LOUT2", "NULL", "LOUT2 Mix Amp"},
367 {"ROUT2", "NULL", "ROUT2 Mix Amp"},
368 {"LOUT3", "NULL", "LOUT3 Mixer"},
369 {"ROUT3", "NULL", "ROUT3 Mixer"},
370
371 {"LOUT1 Mixer", "DACL", "DAC Left"},
372 {"ROUT1 Mixer", "DACR", "DAC Right"},
373 {"LOUT2 Mixer", "DACHL", "DAC Left"},
374 {"ROUT2 Mixer", "DACHR", "DAC Right"},
375 {"LOUT2 Mix Amp", "NULL", "LOUT2 Mixer"},
376 {"ROUT2 Mix Amp", "NULL", "ROUT2 Mixer"},
377 {"LOUT3 Mixer", "DACSL", "DAC Left"},
378 {"ROUT3 Mixer", "DACSR", "DAC Right"},
379
380 /* Inputs */
381 {"LIN MUX", "LIN1", "LIN1"},
382 {"LIN MUX", "LIN2", "LIN2"},
383 {"LIN MUX", "LIN3", "LIN3"},
384 {"LIN MUX", "LIN4", "LIN4"},
385
386 {"RIN MUX", "RIN1", "RIN1"},
387 {"RIN MUX", "RIN2", "RIN2"},
388 {"RIN MUX", "RIN3", "RIN3"},
389 {"RIN MUX", "RIN4", "RIN4"},
390
391 {"LIN1", NULL, "Mic Bias"},
392 {"RIN1", NULL, "Mic Bias"},
393 {"LIN2", NULL, "Mic Bias"},
394 {"RIN2", NULL, "Mic Bias"},
395
396 {"ADC Left", "NULL", "LIN MUX"},
397 {"ADC Right", "NULL", "RIN MUX"},
398
399 /* Analog Loops */
400 {"LIN1 Mixing Circuit", "NULL", "LIN1"},
401 {"RIN1 Mixing Circuit", "NULL", "RIN1"},
402 {"LIN2 Mixing Circuit", "NULL", "LIN2"},
403 {"RIN2 Mixing Circuit", "NULL", "RIN2"},
404 {"LIN3 Mixing Circuit", "NULL", "LIN3"},
405 {"RIN3 Mixing Circuit", "NULL", "RIN3"},
406 {"LIN4 Mixing Circuit", "NULL", "LIN4"},
407 {"RIN4 Mixing Circuit", "NULL", "RIN4"},
408
409 {"LOUT1 Mixer", "LINL1", "LIN1 Mixing Circuit"},
410 {"ROUT1 Mixer", "RINR1", "RIN1 Mixing Circuit"},
411 {"LOUT2 Mixer", "LINH1", "LIN1 Mixing Circuit"},
412 {"ROUT2 Mixer", "RINH1", "RIN1 Mixing Circuit"},
413 {"LOUT3 Mixer", "LINS1", "LIN1 Mixing Circuit"},
414 {"ROUT3 Mixer", "RINS1", "RIN1 Mixing Circuit"},
415
416 {"LOUT1 Mixer", "LINL2", "LIN2 Mixing Circuit"},
417 {"ROUT1 Mixer", "RINR2", "RIN2 Mixing Circuit"},
418 {"LOUT2 Mixer", "LINH2", "LIN2 Mixing Circuit"},
419 {"ROUT2 Mixer", "RINH2", "RIN2 Mixing Circuit"},
420 {"LOUT3 Mixer", "LINS2", "LIN2 Mixing Circuit"},
421 {"ROUT3 Mixer", "RINS2", "RIN2 Mixing Circuit"},
422
423 {"LOUT1 Mixer", "LINL3", "LIN3 Mixing Circuit"},
424 {"ROUT1 Mixer", "RINR3", "RIN3 Mixing Circuit"},
425 {"LOUT2 Mixer", "LINH3", "LIN3 Mixing Circuit"},
426 {"ROUT2 Mixer", "RINH3", "RIN3 Mixing Circuit"},
427 {"LOUT3 Mixer", "LINS3", "LIN3 Mixing Circuit"},
428 {"ROUT3 Mixer", "RINS3", "RIN3 Mixing Circuit"},
429
430 {"LOUT1 Mixer", "LINL4", "LIN4 Mixing Circuit"},
431 {"ROUT1 Mixer", "RINR4", "RIN4 Mixing Circuit"},
432 {"LOUT2 Mixer", "LINH4", "LIN4 Mixing Circuit"},
433 {"ROUT2 Mixer", "RINH4", "RIN4 Mixing Circuit"},
434 {"LOUT3 Mixer", "LINS4", "LIN4 Mixing Circuit"},
435 {"ROUT3 Mixer", "RINS4", "RIN4 Mixing Circuit"},
436};
437
438static int ak4671_add_widgets(struct snd_soc_codec *codec)
439{
440 snd_soc_dapm_new_controls(codec, ak4671_dapm_widgets,
441 ARRAY_SIZE(ak4671_dapm_widgets));
442
443 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
444
Joonyoung Shim2312fd82009-09-10 00:12:43 +0900445 return 0;
446}
447
448static int ak4671_hw_params(struct snd_pcm_substream *substream,
449 struct snd_pcm_hw_params *params,
450 struct snd_soc_dai *dai)
451{
452 struct snd_soc_codec *codec = dai->codec;
453 u8 fs;
454
455 fs = snd_soc_read(codec, AK4671_PLL_MODE_SELECT0);
456 fs &= ~AK4671_FS;
457
458 switch (params_rate(params)) {
459 case 8000:
460 fs |= AK4671_FS_8KHZ;
461 break;
462 case 12000:
463 fs |= AK4671_FS_12KHZ;
464 break;
465 case 16000:
466 fs |= AK4671_FS_16KHZ;
467 break;
468 case 24000:
469 fs |= AK4671_FS_24KHZ;
470 break;
471 case 11025:
472 fs |= AK4671_FS_11_025KHZ;
473 break;
474 case 22050:
475 fs |= AK4671_FS_22_05KHZ;
476 break;
477 case 32000:
478 fs |= AK4671_FS_32KHZ;
479 break;
480 case 44100:
481 fs |= AK4671_FS_44_1KHZ;
482 break;
483 case 48000:
484 fs |= AK4671_FS_48KHZ;
485 break;
486 default:
487 return -EINVAL;
488 }
489
490 snd_soc_write(codec, AK4671_PLL_MODE_SELECT0, fs);
491
492 return 0;
493}
494
495static int ak4671_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
496 unsigned int freq, int dir)
497{
498 struct snd_soc_codec *codec = dai->codec;
499 u8 pll;
500
501 pll = snd_soc_read(codec, AK4671_PLL_MODE_SELECT0);
502 pll &= ~AK4671_PLL;
503
504 switch (freq) {
505 case 11289600:
506 pll |= AK4671_PLL_11_2896MHZ;
507 break;
508 case 12000000:
509 pll |= AK4671_PLL_12MHZ;
510 break;
511 case 12288000:
512 pll |= AK4671_PLL_12_288MHZ;
513 break;
514 case 13000000:
515 pll |= AK4671_PLL_13MHZ;
516 break;
517 case 13500000:
518 pll |= AK4671_PLL_13_5MHZ;
519 break;
520 case 19200000:
521 pll |= AK4671_PLL_19_2MHZ;
522 break;
523 case 24000000:
524 pll |= AK4671_PLL_24MHZ;
525 break;
526 case 26000000:
527 pll |= AK4671_PLL_26MHZ;
528 break;
529 case 27000000:
530 pll |= AK4671_PLL_27MHZ;
531 break;
532 default:
533 return -EINVAL;
534 }
535
536 snd_soc_write(codec, AK4671_PLL_MODE_SELECT0, pll);
537
538 return 0;
539}
540
541static int ak4671_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
542{
543 struct snd_soc_codec *codec = dai->codec;
544 u8 mode;
545 u8 format;
546
547 /* set master/slave audio interface */
548 mode = snd_soc_read(codec, AK4671_PLL_MODE_SELECT1);
549
550 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
551 case SND_SOC_DAIFMT_CBM_CFM:
552 mode |= AK4671_M_S;
553 break;
554 case SND_SOC_DAIFMT_CBM_CFS:
555 mode &= ~(AK4671_M_S);
556 break;
557 default:
558 return -EINVAL;
559 }
560
561 /* interface format */
562 format = snd_soc_read(codec, AK4671_FORMAT_SELECT);
563 format &= ~AK4671_DIF;
564
565 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
566 case SND_SOC_DAIFMT_I2S:
567 format |= AK4671_DIF_I2S_MODE;
568 break;
569 case SND_SOC_DAIFMT_LEFT_J:
570 format |= AK4671_DIF_MSB_MODE;
571 break;
572 case SND_SOC_DAIFMT_DSP_A:
573 format |= AK4671_DIF_DSP_MODE;
574 format |= AK4671_BCKP;
575 format |= AK4671_MSBS;
576 break;
577 default:
578 return -EINVAL;
579 }
580
581 /* set mode and format */
582 snd_soc_write(codec, AK4671_PLL_MODE_SELECT1, mode);
583 snd_soc_write(codec, AK4671_FORMAT_SELECT, format);
584
585 return 0;
586}
587
588static int ak4671_set_bias_level(struct snd_soc_codec *codec,
589 enum snd_soc_bias_level level)
590{
591 u8 reg;
592
593 switch (level) {
594 case SND_SOC_BIAS_ON:
595 case SND_SOC_BIAS_PREPARE:
596 case SND_SOC_BIAS_STANDBY:
597 reg = snd_soc_read(codec, AK4671_AD_DA_POWER_MANAGEMENT);
598 snd_soc_write(codec, AK4671_AD_DA_POWER_MANAGEMENT,
599 reg | AK4671_PMVCM);
600 break;
601 case SND_SOC_BIAS_OFF:
602 snd_soc_write(codec, AK4671_AD_DA_POWER_MANAGEMENT, 0x00);
603 break;
604 }
605 codec->bias_level = level;
606 return 0;
607}
608
609#define AK4671_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
610 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
611 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
612 SNDRV_PCM_RATE_48000)
613
614#define AK4671_FORMATS SNDRV_PCM_FMTBIT_S16_LE
615
616static struct snd_soc_dai_ops ak4671_dai_ops = {
617 .hw_params = ak4671_hw_params,
618 .set_sysclk = ak4671_set_dai_sysclk,
619 .set_fmt = ak4671_set_dai_fmt,
620};
621
622struct snd_soc_dai ak4671_dai = {
623 .name = "AK4671",
624 .playback = {
625 .stream_name = "Playback",
626 .channels_min = 1,
627 .channels_max = 2,
628 .rates = AK4671_RATES,
629 .formats = AK4671_FORMATS,},
630 .capture = {
631 .stream_name = "Capture",
632 .channels_min = 1,
633 .channels_max = 2,
634 .rates = AK4671_RATES,
635 .formats = AK4671_FORMATS,},
636 .ops = &ak4671_dai_ops,
637};
638EXPORT_SYMBOL_GPL(ak4671_dai);
639
640static int ak4671_probe(struct platform_device *pdev)
641{
642 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
643 struct snd_soc_codec *codec;
644 int ret = 0;
645
646 if (ak4671_codec == NULL) {
647 dev_err(&pdev->dev, "Codec device not registered\n");
648 return -ENODEV;
649 }
650
651 socdev->card->codec = ak4671_codec;
652 codec = ak4671_codec;
653
654 /* register pcms */
655 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
656 if (ret < 0) {
657 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
658 goto pcm_err;
659 }
660
661 snd_soc_add_controls(codec, ak4671_snd_controls,
662 ARRAY_SIZE(ak4671_snd_controls));
663 ak4671_add_widgets(codec);
664
Joonyoung Shim2312fd82009-09-10 00:12:43 +0900665 ak4671_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
666
667 return ret;
668
Joonyoung Shim2312fd82009-09-10 00:12:43 +0900669pcm_err:
670 return ret;
671}
672
673static int ak4671_remove(struct platform_device *pdev)
674{
675 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
676
677 snd_soc_free_pcms(socdev);
678 snd_soc_dapm_free(socdev);
679
680 return 0;
681}
682
683struct snd_soc_codec_device soc_codec_dev_ak4671 = {
684 .probe = ak4671_probe,
685 .remove = ak4671_remove,
686};
687EXPORT_SYMBOL_GPL(soc_codec_dev_ak4671);
688
689static int ak4671_register(struct ak4671_priv *ak4671,
690 enum snd_soc_control_type control)
691{
692 int ret;
693 struct snd_soc_codec *codec = &ak4671->codec;
694
695 if (ak4671_codec) {
696 dev_err(codec->dev, "Another AK4671 is registered\n");
697 ret = -EINVAL;
698 goto err;
699 }
700
701 mutex_init(&codec->mutex);
702 INIT_LIST_HEAD(&codec->dapm_widgets);
703 INIT_LIST_HEAD(&codec->dapm_paths);
704
705 codec->private_data = ak4671;
706 codec->name = "AK4671";
707 codec->owner = THIS_MODULE;
708 codec->bias_level = SND_SOC_BIAS_OFF;
709 codec->set_bias_level = ak4671_set_bias_level;
710 codec->dai = &ak4671_dai;
711 codec->num_dai = 1;
712 codec->reg_cache_size = AK4671_CACHEREGNUM;
713 codec->reg_cache = &ak4671->reg_cache;
714
715 memcpy(codec->reg_cache, ak4671_reg, sizeof(ak4671_reg));
716
717 ret = snd_soc_codec_set_cache_io(codec, 8, 8, control);
718 if (ret < 0) {
719 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
720 goto err;
721 }
722
723 ak4671_dai.dev = codec->dev;
724 ak4671_codec = codec;
725
726 ret = snd_soc_register_codec(codec);
727 if (ret != 0) {
728 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
729 goto err;
730 }
731
732 ret = snd_soc_register_dai(&ak4671_dai);
733 if (ret != 0) {
734 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
735 goto err_codec;
736 }
737
738 return 0;
739
740err_codec:
741 snd_soc_unregister_codec(codec);
742err:
743 kfree(ak4671);
744 return ret;
745}
746
747static void ak4671_unregister(struct ak4671_priv *ak4671)
748{
749 ak4671_set_bias_level(&ak4671->codec, SND_SOC_BIAS_OFF);
750 snd_soc_unregister_dai(&ak4671_dai);
751 snd_soc_unregister_codec(&ak4671->codec);
752 kfree(ak4671);
753 ak4671_codec = NULL;
754}
755
756static int __devinit ak4671_i2c_probe(struct i2c_client *client,
757 const struct i2c_device_id *id)
758{
759 struct ak4671_priv *ak4671;
760 struct snd_soc_codec *codec;
761
762 ak4671 = kzalloc(sizeof(struct ak4671_priv), GFP_KERNEL);
763 if (ak4671 == NULL)
764 return -ENOMEM;
765
766 codec = &ak4671->codec;
767 codec->hw_write = (hw_write_t)i2c_master_send;
768
769 i2c_set_clientdata(client, ak4671);
770 codec->control_data = client;
771
772 codec->dev = &client->dev;
773
774 return ak4671_register(ak4671, SND_SOC_I2C);
775}
776
777static __devexit int ak4671_i2c_remove(struct i2c_client *client)
778{
779 struct ak4671_priv *ak4671 = i2c_get_clientdata(client);
780
781 ak4671_unregister(ak4671);
782
783 return 0;
784}
785
786static const struct i2c_device_id ak4671_i2c_id[] = {
787 { "ak4671", 0 },
788 { }
789};
790MODULE_DEVICE_TABLE(i2c, ak4671_i2c_id);
791
792static struct i2c_driver ak4671_i2c_driver = {
793 .driver = {
794 .name = "ak4671",
795 .owner = THIS_MODULE,
796 },
797 .probe = ak4671_i2c_probe,
798 .remove = __devexit_p(ak4671_i2c_remove),
799 .id_table = ak4671_i2c_id,
800};
801
802static int __init ak4671_modinit(void)
803{
804 return i2c_add_driver(&ak4671_i2c_driver);
805}
806module_init(ak4671_modinit);
807
808static void __exit ak4671_exit(void)
809{
810 i2c_del_driver(&ak4671_i2c_driver);
811}
812module_exit(ak4671_exit);
813
814MODULE_DESCRIPTION("ASoC AK4671 codec driver");
815MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
816MODULE_LICENSE("GPL");