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