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