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