1 /*
2  * 88pm860x-codec.c -- 88PM860x ALSA SoC Audio Driver
3  *
4  * Copyright 2010 Marvell International Ltd.
5  * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/mfd/88pm860x.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/tlv.h>
24 #include <sound/initval.h>
25 #include <sound/jack.h>
26 #include <trace/events/asoc.h>
27 
28 #include "88pm860x-codec.h"
29 
30 #define MAX_NAME_LEN		20
31 #define REG_CACHE_SIZE		0x40
32 #define REG_CACHE_BASE		0xb0
33 
34 /* Status Register 1 (0x01) */
35 #define REG_STATUS_1		0x01
36 #define MIC_STATUS		(1 << 7)
37 #define HOOK_STATUS		(1 << 6)
38 #define HEADSET_STATUS		(1 << 5)
39 
40 /* Mic Detection Register (0x37) */
41 #define REG_MIC_DET		0x37
42 #define CONTINUOUS_POLLING	(3 << 1)
43 #define EN_MIC_DET		(1 << 0)
44 #define MICDET_MASK		0x07
45 
46 /* Headset Detection Register (0x38) */
47 #define REG_HS_DET		0x38
48 #define EN_HS_DET		(1 << 0)
49 
50 /* Misc2 Register (0x42) */
51 #define REG_MISC2		0x42
52 #define AUDIO_PLL		(1 << 5)
53 #define AUDIO_SECTION_RESET	(1 << 4)
54 #define AUDIO_SECTION_ON	(1 << 3)
55 
56 /* PCM Interface Register 2 (0xb1) */
57 #define PCM_INF2_BCLK		(1 << 6)	/* Bit clock polarity */
58 #define PCM_INF2_FS		(1 << 5)	/* Frame Sync polarity */
59 #define PCM_INF2_MASTER		(1 << 4)	/* Master / Slave */
60 #define PCM_INF2_18WL		(1 << 3)	/* 18 / 16 bits */
61 #define PCM_GENERAL_I2S		0
62 #define PCM_EXACT_I2S		1
63 #define PCM_LEFT_I2S		2
64 #define PCM_RIGHT_I2S		3
65 #define PCM_SHORT_FS		4
66 #define PCM_LONG_FS		5
67 #define PCM_MODE_MASK		7
68 
69 /* I2S Interface Register 4 (0xbe) */
70 #define I2S_EQU_BYP		(1 << 6)
71 
72 /* DAC Offset Register (0xcb) */
73 #define DAC_MUTE		(1 << 7)
74 #define MUTE_LEFT		(1 << 6)
75 #define MUTE_RIGHT		(1 << 2)
76 
77 /* ADC Analog Register 1 (0xd0) */
78 #define REG_ADC_ANA_1		0xd0
79 #define MIC1BIAS_MASK		0x60
80 
81 /* Earpiece/Speaker Control Register 2 (0xda) */
82 #define REG_EAR2		0xda
83 #define RSYNC_CHANGE		(1 << 2)
84 
85 /* Audio Supplies Register 2 (0xdc) */
86 #define REG_SUPPLIES2		0xdc
87 #define LDO15_READY		(1 << 4)
88 #define LDO15_EN		(1 << 3)
89 #define CPUMP_READY		(1 << 2)
90 #define CPUMP_EN		(1 << 1)
91 #define AUDIO_EN		(1 << 0)
92 #define SUPPLY_MASK		(LDO15_EN | CPUMP_EN | AUDIO_EN)
93 
94 /* Audio Enable Register 1 (0xdd) */
95 #define ADC_MOD_RIGHT		(1 << 1)
96 #define ADC_MOD_LEFT		(1 << 0)
97 
98 /* Audio Enable Register 2 (0xde) */
99 #define ADC_LEFT		(1 << 5)
100 #define ADC_RIGHT		(1 << 4)
101 
102 /* DAC Enable Register 2 (0xe1) */
103 #define DAC_LEFT		(1 << 5)
104 #define DAC_RIGHT		(1 << 4)
105 #define MODULATOR		(1 << 3)
106 
107 /* Shorts Register (0xeb) */
108 #define REG_SHORTS		0xeb
109 #define CLR_SHORT_LO2		(1 << 7)
110 #define SHORT_LO2		(1 << 6)
111 #define CLR_SHORT_LO1		(1 << 5)
112 #define SHORT_LO1		(1 << 4)
113 #define CLR_SHORT_HS2		(1 << 3)
114 #define SHORT_HS2		(1 << 2)
115 #define CLR_SHORT_HS1		(1 << 1)
116 #define SHORT_HS1		(1 << 0)
117 
118 /*
119  * This widget should be just after DAC & PGA in DAPM power-on sequence and
120  * before DAC & PGA in DAPM power-off sequence.
121  */
122 #define PM860X_DAPM_OUTPUT(wname, wevent)	\
123 	SND_SOC_DAPM_PGA_E(wname, SND_SOC_NOPM, 0, 0, NULL, 0, wevent, \
124 			    SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD)
125 
126 struct pm860x_det {
127 	struct snd_soc_jack	*hp_jack;
128 	struct snd_soc_jack	*mic_jack;
129 	int			hp_det;
130 	int			mic_det;
131 	int			hook_det;
132 	int			hs_shrt;
133 	int			lo_shrt;
134 };
135 
136 struct pm860x_priv {
137 	unsigned int		sysclk;
138 	unsigned int		pcmclk;
139 	unsigned int		dir;
140 	unsigned int		filter;
141 	struct snd_soc_codec	*codec;
142 	struct i2c_client	*i2c;
143 	struct pm860x_chip	*chip;
144 	struct pm860x_det	det;
145 
146 	int			irq[4];
147 	unsigned char		name[4][MAX_NAME_LEN];
148 };
149 
150 /* -9450dB to 0dB in 150dB steps ( mute instead of -9450dB) */
151 static const DECLARE_TLV_DB_SCALE(dpga_tlv, -9450, 150, 1);
152 
153 /* -9dB to 0db in 3dB steps */
154 static const DECLARE_TLV_DB_SCALE(adc_tlv, -900, 300, 0);
155 
156 /* {-23, -17, -13.5, -11, -9, -6, -3, 0}dB */
157 static const unsigned int mic_tlv[] = {
158 	TLV_DB_RANGE_HEAD(5),
159 	0, 0, TLV_DB_SCALE_ITEM(-2300, 0, 0),
160 	1, 1, TLV_DB_SCALE_ITEM(-1700, 0, 0),
161 	2, 2, TLV_DB_SCALE_ITEM(-1350, 0, 0),
162 	3, 3, TLV_DB_SCALE_ITEM(-1100, 0, 0),
163 	4, 7, TLV_DB_SCALE_ITEM(-900, 300, 0),
164 };
165 
166 /* {0, 0, 0, -6, 0, 6, 12, 18}dB */
167 static const unsigned int aux_tlv[] = {
168 	TLV_DB_RANGE_HEAD(2),
169 	0, 2, TLV_DB_SCALE_ITEM(0, 0, 0),
170 	3, 7, TLV_DB_SCALE_ITEM(-600, 600, 0),
171 };
172 
173 /* {-16, -13, -10, -7, -5.2, -3,3, -2.2, 0}dB, mute instead of -16dB */
174 static const unsigned int out_tlv[] = {
175 	TLV_DB_RANGE_HEAD(4),
176 	0, 3, TLV_DB_SCALE_ITEM(-1600, 300, 1),
177 	4, 4, TLV_DB_SCALE_ITEM(-520, 0, 0),
178 	5, 5, TLV_DB_SCALE_ITEM(-330, 0, 0),
179 	6, 7, TLV_DB_SCALE_ITEM(-220, 220, 0),
180 };
181 
182 static const unsigned int st_tlv[] = {
183 	TLV_DB_RANGE_HEAD(8),
184 	0, 1, TLV_DB_SCALE_ITEM(-12041, 602, 0),
185 	2, 3, TLV_DB_SCALE_ITEM(-11087, 250, 0),
186 	4, 5, TLV_DB_SCALE_ITEM(-10643, 158, 0),
187 	6, 7, TLV_DB_SCALE_ITEM(-10351, 116, 0),
188 	8, 9, TLV_DB_SCALE_ITEM(-10133, 92, 0),
189 	10, 13, TLV_DB_SCALE_ITEM(-9958, 70, 0),
190 	14, 17, TLV_DB_SCALE_ITEM(-9689, 53, 0),
191 	18, 271, TLV_DB_SCALE_ITEM(-9484, 37, 0),
192 };
193 
194 /* Sidetone Gain = M * 2^(-5-N) */
195 struct st_gain {
196 	unsigned int	db;
197 	unsigned int	m;
198 	unsigned int	n;
199 };
200 
201 static struct st_gain st_table[] = {
202 	{-12041,  1, 15}, {-11439,  1, 14}, {-11087,  3, 15}, {-10837,  1, 13},
203 	{-10643,  5, 15}, {-10485,  3, 14}, {-10351,  7, 15}, {-10235,  1, 12},
204 	{-10133,  9, 15}, {-10041,  5, 14}, { -9958, 11, 15}, { -9883,  3, 13},
205 	{ -9813, 13, 15}, { -9749,  7, 14}, { -9689, 15, 15}, { -9633,  1, 11},
206 	{ -9580, 17, 15}, { -9531,  9, 14}, { -9484, 19, 15}, { -9439,  5, 13},
207 	{ -9397, 21, 15}, { -9356, 11, 14}, { -9318, 23, 15}, { -9281,  3, 12},
208 	{ -9245, 25, 15}, { -9211, 13, 14}, { -9178, 27, 15}, { -9147,  7, 13},
209 	{ -9116, 29, 15}, { -9087, 15, 14}, { -9058, 31, 15}, { -9031,  1, 10},
210 	{ -8978, 17, 14}, { -8929,  9, 13}, { -8882, 19, 14}, { -8837,  5, 12},
211 	{ -8795, 21, 14}, { -8754, 11, 13}, { -8716, 23, 14}, { -8679,  3, 11},
212 	{ -8643, 25, 14}, { -8609, 13, 13}, { -8576, 27, 14}, { -8545,  7, 12},
213 	{ -8514, 29, 14}, { -8485, 15, 13}, { -8456, 31, 14}, { -8429,  1,  9},
214 	{ -8376, 17, 13}, { -8327,  9, 12}, { -8280, 19, 13}, { -8235,  5, 11},
215 	{ -8193, 21, 13}, { -8152, 11, 12}, { -8114, 23, 13}, { -8077,  3, 10},
216 	{ -8041, 25, 13}, { -8007, 13, 12}, { -7974, 27, 13}, { -7943,  7, 11},
217 	{ -7912, 29, 13}, { -7883, 15, 12}, { -7854, 31, 13}, { -7827,  1,  8},
218 	{ -7774, 17, 12}, { -7724,  9, 11}, { -7678, 19, 12}, { -7633,  5, 10},
219 	{ -7591, 21, 12}, { -7550, 11, 11}, { -7512, 23, 12}, { -7475,  3,  9},
220 	{ -7439, 25, 12}, { -7405, 13, 11}, { -7372, 27, 12}, { -7341,  7, 10},
221 	{ -7310, 29, 12}, { -7281, 15, 11}, { -7252, 31, 12}, { -7225,  1,  7},
222 	{ -7172, 17, 11}, { -7122,  9, 10}, { -7075, 19, 11}, { -7031,  5,  9},
223 	{ -6989, 21, 11}, { -6948, 11, 10}, { -6910, 23, 11}, { -6873,  3,  8},
224 	{ -6837, 25, 11}, { -6803, 13, 10}, { -6770, 27, 11}, { -6739,  7,  9},
225 	{ -6708, 29, 11}, { -6679, 15, 10}, { -6650, 31, 11}, { -6623,  1,  6},
226 	{ -6570, 17, 10}, { -6520,  9,  9}, { -6473, 19, 10}, { -6429,  5,  8},
227 	{ -6386, 21, 10}, { -6346, 11,  9}, { -6307, 23, 10}, { -6270,  3,  7},
228 	{ -6235, 25, 10}, { -6201, 13,  9}, { -6168, 27, 10}, { -6137,  7,  8},
229 	{ -6106, 29, 10}, { -6077, 15,  9}, { -6048, 31, 10}, { -6021,  1,  5},
230 	{ -5968, 17,  9}, { -5918,  9,  8}, { -5871, 19,  9}, { -5827,  5,  7},
231 	{ -5784, 21,  9}, { -5744, 11,  8}, { -5705, 23,  9}, { -5668,  3,  6},
232 	{ -5633, 25,  9}, { -5599, 13,  8}, { -5566, 27,  9}, { -5535,  7,  7},
233 	{ -5504, 29,  9}, { -5475, 15,  8}, { -5446, 31,  9}, { -5419,  1,  4},
234 	{ -5366, 17,  8}, { -5316,  9,  7}, { -5269, 19,  8}, { -5225,  5,  6},
235 	{ -5182, 21,  8}, { -5142, 11,  7}, { -5103, 23,  8}, { -5066,  3,  5},
236 	{ -5031, 25,  8}, { -4997, 13,  7}, { -4964, 27,  8}, { -4932,  7,  6},
237 	{ -4902, 29,  8}, { -4873, 15,  7}, { -4844, 31,  8}, { -4816,  1,  3},
238 	{ -4764, 17,  7}, { -4714,  9,  6}, { -4667, 19,  7}, { -4623,  5,  5},
239 	{ -4580, 21,  7}, { -4540, 11,  6}, { -4501, 23,  7}, { -4464,  3,  4},
240 	{ -4429, 25,  7}, { -4395, 13,  6}, { -4362, 27,  7}, { -4330,  7,  5},
241 	{ -4300, 29,  7}, { -4270, 15,  6}, { -4242, 31,  7}, { -4214,  1,  2},
242 	{ -4162, 17,  6}, { -4112,  9,  5}, { -4065, 19,  6}, { -4021,  5,  4},
243 	{ -3978, 21,  6}, { -3938, 11,  5}, { -3899, 23,  6}, { -3862,  3,  3},
244 	{ -3827, 25,  6}, { -3793, 13,  5}, { -3760, 27,  6}, { -3728,  7,  4},
245 	{ -3698, 29,  6}, { -3668, 15,  5}, { -3640, 31,  6}, { -3612,  1,  1},
246 	{ -3560, 17,  5}, { -3510,  9,  4}, { -3463, 19,  5}, { -3419,  5,  3},
247 	{ -3376, 21,  5}, { -3336, 11,  4}, { -3297, 23,  5}, { -3260,  3,  2},
248 	{ -3225, 25,  5}, { -3191, 13,  4}, { -3158, 27,  5}, { -3126,  7,  3},
249 	{ -3096, 29,  5}, { -3066, 15,  4}, { -3038, 31,  5}, { -3010,  1,  0},
250 	{ -2958, 17,  4}, { -2908,  9,  3}, { -2861, 19,  4}, { -2816,  5,  2},
251 	{ -2774, 21,  4}, { -2734, 11,  3}, { -2695, 23,  4}, { -2658,  3,  1},
252 	{ -2623, 25,  4}, { -2589, 13,  3}, { -2556, 27,  4}, { -2524,  7,  2},
253 	{ -2494, 29,  4}, { -2464, 15,  3}, { -2436, 31,  4}, { -2408,  2,  0},
254 	{ -2356, 17,  3}, { -2306,  9,  2}, { -2259, 19,  3}, { -2214,  5,  1},
255 	{ -2172, 21,  3}, { -2132, 11,  2}, { -2093, 23,  3}, { -2056,  3,  0},
256 	{ -2021, 25,  3}, { -1987, 13,  2}, { -1954, 27,  3}, { -1922,  7,  1},
257 	{ -1892, 29,  3}, { -1862, 15,  2}, { -1834, 31,  3}, { -1806,  4,  0},
258 	{ -1754, 17,  2}, { -1704,  9,  1}, { -1657, 19,  2}, { -1612,  5,  0},
259 	{ -1570, 21,  2}, { -1530, 11,  1}, { -1491, 23,  2}, { -1454,  6,  0},
260 	{ -1419, 25,  2}, { -1384, 13,  1}, { -1352, 27,  2}, { -1320,  7,  0},
261 	{ -1290, 29,  2}, { -1260, 15,  1}, { -1232, 31,  2}, { -1204,  8,  0},
262 	{ -1151, 17,  1}, { -1102,  9,  0}, { -1055, 19,  1}, { -1010, 10,  0},
263 	{  -968, 21,  1}, {  -928, 11,  0}, {  -889, 23,  1}, {  -852, 12,  0},
264 	{  -816, 25,  1}, {  -782, 13,  0}, {  -750, 27,  1}, {  -718, 14,  0},
265 	{  -688, 29,  1}, {  -658, 15,  0}, {  -630, 31,  1}, {  -602, 16,  0},
266 	{  -549, 17,  0}, {  -500, 18,  0}, {  -453, 19,  0}, {  -408, 20,  0},
267 	{  -366, 21,  0}, {  -325, 22,  0}, {  -287, 23,  0}, {  -250, 24,  0},
268 	{  -214, 25,  0}, {  -180, 26,  0}, {  -148, 27,  0}, {  -116, 28,  0},
269 	{   -86, 29,  0}, {   -56, 30,  0}, {   -28, 31,  0}, {     0,  0,  0},
270 };
271 
272 static int pm860x_volatile(unsigned int reg)
273 {
274 	BUG_ON(reg >= REG_CACHE_SIZE);
275 
276 	switch (reg) {
277 	case PM860X_AUDIO_SUPPLIES_2:
278 		return 1;
279 	}
280 
281 	return 0;
282 }
283 
284 static unsigned int pm860x_read_reg_cache(struct snd_soc_codec *codec,
285 					  unsigned int reg)
286 {
287 	unsigned char *cache = codec->reg_cache;
288 
289 	BUG_ON(reg >= REG_CACHE_SIZE);
290 
291 	if (pm860x_volatile(reg))
292 		return cache[reg];
293 
294 	reg += REG_CACHE_BASE;
295 
296 	return pm860x_reg_read(codec->control_data, reg);
297 }
298 
299 static int pm860x_write_reg_cache(struct snd_soc_codec *codec,
300 				  unsigned int reg, unsigned int value)
301 {
302 	unsigned char *cache = codec->reg_cache;
303 
304 	BUG_ON(reg >= REG_CACHE_SIZE);
305 
306 	if (!pm860x_volatile(reg))
307 		cache[reg] = (unsigned char)value;
308 
309 	reg += REG_CACHE_BASE;
310 
311 	return pm860x_reg_write(codec->control_data, reg, value);
312 }
313 
314 static int snd_soc_get_volsw_2r_st(struct snd_kcontrol *kcontrol,
315 				   struct snd_ctl_elem_value *ucontrol)
316 {
317 	struct soc_mixer_control *mc =
318 		(struct soc_mixer_control *)kcontrol->private_value;
319 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
320 	unsigned int reg = mc->reg;
321 	unsigned int reg2 = mc->rreg;
322 	int val[2], val2[2], i;
323 
324 	val[0] = snd_soc_read(codec, reg) & 0x3f;
325 	val[1] = (snd_soc_read(codec, PM860X_SIDETONE_SHIFT) >> 4) & 0xf;
326 	val2[0] = snd_soc_read(codec, reg2) & 0x3f;
327 	val2[1] = (snd_soc_read(codec, PM860X_SIDETONE_SHIFT)) & 0xf;
328 
329 	for (i = 0; i < ARRAY_SIZE(st_table); i++) {
330 		if ((st_table[i].m == val[0]) && (st_table[i].n == val[1]))
331 			ucontrol->value.integer.value[0] = i;
332 		if ((st_table[i].m == val2[0]) && (st_table[i].n == val2[1]))
333 			ucontrol->value.integer.value[1] = i;
334 	}
335 	return 0;
336 }
337 
338 static int snd_soc_put_volsw_2r_st(struct snd_kcontrol *kcontrol,
339 				   struct snd_ctl_elem_value *ucontrol)
340 {
341 	struct soc_mixer_control *mc =
342 		(struct soc_mixer_control *)kcontrol->private_value;
343 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
344 	unsigned int reg = mc->reg;
345 	unsigned int reg2 = mc->rreg;
346 	int err;
347 	unsigned int val, val2;
348 
349 	val = ucontrol->value.integer.value[0];
350 	val2 = ucontrol->value.integer.value[1];
351 
352 	err = snd_soc_update_bits(codec, reg, 0x3f, st_table[val].m);
353 	if (err < 0)
354 		return err;
355 	err = snd_soc_update_bits(codec, PM860X_SIDETONE_SHIFT, 0xf0,
356 				  st_table[val].n << 4);
357 	if (err < 0)
358 		return err;
359 
360 	err = snd_soc_update_bits(codec, reg2, 0x3f, st_table[val2].m);
361 	if (err < 0)
362 		return err;
363 	err = snd_soc_update_bits(codec, PM860X_SIDETONE_SHIFT, 0x0f,
364 				  st_table[val2].n);
365 	return err;
366 }
367 
368 static int snd_soc_get_volsw_2r_out(struct snd_kcontrol *kcontrol,
369 				    struct snd_ctl_elem_value *ucontrol)
370 {
371 	struct soc_mixer_control *mc =
372 		(struct soc_mixer_control *)kcontrol->private_value;
373 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
374 	unsigned int reg = mc->reg;
375 	unsigned int reg2 = mc->rreg;
376 	unsigned int shift = mc->shift;
377 	int max = mc->max, val, val2;
378 	unsigned int mask = (1 << fls(max)) - 1;
379 
380 	val = snd_soc_read(codec, reg) >> shift;
381 	val2 = snd_soc_read(codec, reg2) >> shift;
382 	ucontrol->value.integer.value[0] = (max - val) & mask;
383 	ucontrol->value.integer.value[1] = (max - val2) & mask;
384 
385 	return 0;
386 }
387 
388 static int snd_soc_put_volsw_2r_out(struct snd_kcontrol *kcontrol,
389 				    struct snd_ctl_elem_value *ucontrol)
390 {
391 	struct soc_mixer_control *mc =
392 		(struct soc_mixer_control *)kcontrol->private_value;
393 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
394 	unsigned int reg = mc->reg;
395 	unsigned int reg2 = mc->rreg;
396 	unsigned int shift = mc->shift;
397 	int max = mc->max;
398 	unsigned int mask = (1 << fls(max)) - 1;
399 	int err;
400 	unsigned int val, val2, val_mask;
401 
402 	val_mask = mask << shift;
403 	val = ((max - ucontrol->value.integer.value[0]) & mask);
404 	val2 = ((max - ucontrol->value.integer.value[1]) & mask);
405 
406 	val = val << shift;
407 	val2 = val2 << shift;
408 
409 	err = snd_soc_update_bits(codec, reg, val_mask, val);
410 	if (err < 0)
411 		return err;
412 
413 	err = snd_soc_update_bits(codec, reg2, val_mask, val2);
414 	return err;
415 }
416 
417 /* DAPM Widget Events */
418 /*
419  * A lot registers are belong to RSYNC domain. It requires enabling RSYNC bit
420  * after updating these registers. Otherwise, these updated registers won't
421  * be effective.
422  */
423 static int pm860x_rsync_event(struct snd_soc_dapm_widget *w,
424 			      struct snd_kcontrol *kcontrol, int event)
425 {
426 	struct snd_soc_codec *codec = w->codec;
427 
428 	/*
429 	 * In order to avoid current on the load, mute power-on and power-off
430 	 * should be transients.
431 	 * Unmute by DAC_MUTE. It should be unmuted when DAPM sequence is
432 	 * finished.
433 	 */
434 	snd_soc_update_bits(codec, PM860X_DAC_OFFSET, DAC_MUTE, 0);
435 	snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
436 			    RSYNC_CHANGE, RSYNC_CHANGE);
437 	return 0;
438 }
439 
440 static int pm860x_dac_event(struct snd_soc_dapm_widget *w,
441 			    struct snd_kcontrol *kcontrol, int event)
442 {
443 	struct snd_soc_codec *codec = w->codec;
444 	unsigned int dac = 0;
445 	int data;
446 
447 	if (!strcmp(w->name, "Left DAC"))
448 		dac = DAC_LEFT;
449 	if (!strcmp(w->name, "Right DAC"))
450 		dac = DAC_RIGHT;
451 	switch (event) {
452 	case SND_SOC_DAPM_PRE_PMU:
453 		if (dac) {
454 			/* Auto mute in power-on sequence. */
455 			dac |= MODULATOR;
456 			snd_soc_update_bits(codec, PM860X_DAC_OFFSET,
457 					    DAC_MUTE, DAC_MUTE);
458 			snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
459 					    RSYNC_CHANGE, RSYNC_CHANGE);
460 			/* update dac */
461 			snd_soc_update_bits(codec, PM860X_DAC_EN_2,
462 					    dac, dac);
463 		}
464 		break;
465 	case SND_SOC_DAPM_PRE_PMD:
466 		if (dac) {
467 			/* Auto mute in power-off sequence. */
468 			snd_soc_update_bits(codec, PM860X_DAC_OFFSET,
469 					    DAC_MUTE, DAC_MUTE);
470 			snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
471 					    RSYNC_CHANGE, RSYNC_CHANGE);
472 			/* update dac */
473 			data = snd_soc_read(codec, PM860X_DAC_EN_2);
474 			data &= ~dac;
475 			if (!(data & (DAC_LEFT | DAC_RIGHT)))
476 				data &= ~MODULATOR;
477 			snd_soc_write(codec, PM860X_DAC_EN_2, data);
478 		}
479 		break;
480 	}
481 	return 0;
482 }
483 
484 static const char *pm860x_opamp_texts[] = {"-50%", "-25%", "0%", "75%"};
485 
486 static const char *pm860x_pa_texts[] = {"-33%", "0%", "33%", "66%"};
487 
488 static const struct soc_enum pm860x_hs1_opamp_enum =
489 	SOC_ENUM_SINGLE(PM860X_HS1_CTRL, 5, 4, pm860x_opamp_texts);
490 
491 static const struct soc_enum pm860x_hs2_opamp_enum =
492 	SOC_ENUM_SINGLE(PM860X_HS2_CTRL, 5, 4, pm860x_opamp_texts);
493 
494 static const struct soc_enum pm860x_hs1_pa_enum =
495 	SOC_ENUM_SINGLE(PM860X_HS1_CTRL, 3, 4, pm860x_pa_texts);
496 
497 static const struct soc_enum pm860x_hs2_pa_enum =
498 	SOC_ENUM_SINGLE(PM860X_HS2_CTRL, 3, 4, pm860x_pa_texts);
499 
500 static const struct soc_enum pm860x_lo1_opamp_enum =
501 	SOC_ENUM_SINGLE(PM860X_LO1_CTRL, 5, 4, pm860x_opamp_texts);
502 
503 static const struct soc_enum pm860x_lo2_opamp_enum =
504 	SOC_ENUM_SINGLE(PM860X_LO2_CTRL, 5, 4, pm860x_opamp_texts);
505 
506 static const struct soc_enum pm860x_lo1_pa_enum =
507 	SOC_ENUM_SINGLE(PM860X_LO1_CTRL, 3, 4, pm860x_pa_texts);
508 
509 static const struct soc_enum pm860x_lo2_pa_enum =
510 	SOC_ENUM_SINGLE(PM860X_LO2_CTRL, 3, 4, pm860x_pa_texts);
511 
512 static const struct soc_enum pm860x_spk_pa_enum =
513 	SOC_ENUM_SINGLE(PM860X_EAR_CTRL_1, 5, 4, pm860x_pa_texts);
514 
515 static const struct soc_enum pm860x_ear_pa_enum =
516 	SOC_ENUM_SINGLE(PM860X_EAR_CTRL_2, 0, 4, pm860x_pa_texts);
517 
518 static const struct soc_enum pm860x_spk_ear_opamp_enum =
519 	SOC_ENUM_SINGLE(PM860X_EAR_CTRL_1, 3, 4, pm860x_opamp_texts);
520 
521 static const struct snd_kcontrol_new pm860x_snd_controls[] = {
522 	SOC_DOUBLE_R_TLV("ADC Capture Volume", PM860X_ADC_ANA_2,
523 			PM860X_ADC_ANA_3, 6, 3, 0, adc_tlv),
524 	SOC_DOUBLE_TLV("AUX Capture Volume", PM860X_ADC_ANA_3, 0, 3, 7, 0,
525 			aux_tlv),
526 	SOC_SINGLE_TLV("MIC1 Capture Volume", PM860X_ADC_ANA_2, 0, 7, 0,
527 			mic_tlv),
528 	SOC_SINGLE_TLV("MIC3 Capture Volume", PM860X_ADC_ANA_2, 3, 7, 0,
529 			mic_tlv),
530 	SOC_DOUBLE_R_EXT_TLV("Sidetone Volume", PM860X_SIDETONE_L_GAIN,
531 			     PM860X_SIDETONE_R_GAIN, 0, ARRAY_SIZE(st_table)-1,
532 			     0, snd_soc_get_volsw_2r_st,
533 			     snd_soc_put_volsw_2r_st, st_tlv),
534 	SOC_SINGLE_TLV("Speaker Playback Volume", PM860X_EAR_CTRL_1,
535 			0, 7, 0, out_tlv),
536 	SOC_DOUBLE_R_TLV("Line Playback Volume", PM860X_LO1_CTRL,
537 			 PM860X_LO2_CTRL, 0, 7, 0, out_tlv),
538 	SOC_DOUBLE_R_TLV("Headset Playback Volume", PM860X_HS1_CTRL,
539 			 PM860X_HS2_CTRL, 0, 7, 0, out_tlv),
540 	SOC_DOUBLE_R_EXT_TLV("Hifi Left Playback Volume",
541 			     PM860X_HIFIL_GAIN_LEFT,
542 			     PM860X_HIFIL_GAIN_RIGHT, 0, 63, 0,
543 			     snd_soc_get_volsw_2r_out,
544 			     snd_soc_put_volsw_2r_out, dpga_tlv),
545 	SOC_DOUBLE_R_EXT_TLV("Hifi Right Playback Volume",
546 			     PM860X_HIFIR_GAIN_LEFT,
547 			     PM860X_HIFIR_GAIN_RIGHT, 0, 63, 0,
548 			     snd_soc_get_volsw_2r_out,
549 			     snd_soc_put_volsw_2r_out, dpga_tlv),
550 	SOC_DOUBLE_R_EXT_TLV("Lofi Playback Volume", PM860X_LOFI_GAIN_LEFT,
551 			     PM860X_LOFI_GAIN_RIGHT, 0, 63, 0,
552 			     snd_soc_get_volsw_2r_out,
553 			     snd_soc_put_volsw_2r_out, dpga_tlv),
554 	SOC_ENUM("Headset1 Operational Amplifier Current",
555 		 pm860x_hs1_opamp_enum),
556 	SOC_ENUM("Headset2 Operational Amplifier Current",
557 		 pm860x_hs2_opamp_enum),
558 	SOC_ENUM("Headset1 Amplifier Current", pm860x_hs1_pa_enum),
559 	SOC_ENUM("Headset2 Amplifier Current", pm860x_hs2_pa_enum),
560 	SOC_ENUM("Lineout1 Operational Amplifier Current",
561 		 pm860x_lo1_opamp_enum),
562 	SOC_ENUM("Lineout2 Operational Amplifier Current",
563 		 pm860x_lo2_opamp_enum),
564 	SOC_ENUM("Lineout1 Amplifier Current", pm860x_lo1_pa_enum),
565 	SOC_ENUM("Lineout2 Amplifier Current", pm860x_lo2_pa_enum),
566 	SOC_ENUM("Speaker Operational Amplifier Current",
567 		 pm860x_spk_ear_opamp_enum),
568 	SOC_ENUM("Speaker Amplifier Current", pm860x_spk_pa_enum),
569 	SOC_ENUM("Earpiece Amplifier Current", pm860x_ear_pa_enum),
570 };
571 
572 /*
573  * DAPM Controls
574  */
575 
576 /* PCM Switch / PCM Interface */
577 static const struct snd_kcontrol_new pcm_switch_controls =
578 	SOC_DAPM_SINGLE("Switch", PM860X_ADC_EN_2, 0, 1, 0);
579 
580 /* AUX1 Switch */
581 static const struct snd_kcontrol_new aux1_switch_controls =
582 	SOC_DAPM_SINGLE("Switch", PM860X_ANA_TO_ANA, 4, 1, 0);
583 
584 /* AUX2 Switch */
585 static const struct snd_kcontrol_new aux2_switch_controls =
586 	SOC_DAPM_SINGLE("Switch", PM860X_ANA_TO_ANA, 5, 1, 0);
587 
588 /* Left Ex. PA Switch */
589 static const struct snd_kcontrol_new lepa_switch_controls =
590 	SOC_DAPM_SINGLE("Switch", PM860X_DAC_EN_2, 2, 1, 0);
591 
592 /* Right Ex. PA Switch */
593 static const struct snd_kcontrol_new repa_switch_controls =
594 	SOC_DAPM_SINGLE("Switch", PM860X_DAC_EN_2, 1, 1, 0);
595 
596 /* PCM Mux / Mux7 */
597 static const char *aif1_text[] = {
598 	"PCM L", "PCM R",
599 };
600 
601 static const struct soc_enum aif1_enum =
602 	SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 6, 2, aif1_text);
603 
604 static const struct snd_kcontrol_new aif1_mux =
605 	SOC_DAPM_ENUM("PCM Mux", aif1_enum);
606 
607 /* I2S Mux / Mux9 */
608 static const char *i2s_din_text[] = {
609 	"DIN", "DIN1",
610 };
611 
612 static const struct soc_enum i2s_din_enum =
613 	SOC_ENUM_SINGLE(PM860X_I2S_IFACE_3, 1, 2, i2s_din_text);
614 
615 static const struct snd_kcontrol_new i2s_din_mux =
616 	SOC_DAPM_ENUM("I2S DIN Mux", i2s_din_enum);
617 
618 /* I2S Mic Mux / Mux8 */
619 static const char *i2s_mic_text[] = {
620 	"Ex PA", "ADC",
621 };
622 
623 static const struct soc_enum i2s_mic_enum =
624 	SOC_ENUM_SINGLE(PM860X_I2S_IFACE_3, 4, 2, i2s_mic_text);
625 
626 static const struct snd_kcontrol_new i2s_mic_mux =
627 	SOC_DAPM_ENUM("I2S Mic Mux", i2s_mic_enum);
628 
629 /* ADCL Mux / Mux2 */
630 static const char *adcl_text[] = {
631 	"ADCR", "ADCL",
632 };
633 
634 static const struct soc_enum adcl_enum =
635 	SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 4, 2, adcl_text);
636 
637 static const struct snd_kcontrol_new adcl_mux =
638 	SOC_DAPM_ENUM("ADC Left Mux", adcl_enum);
639 
640 /* ADCR Mux / Mux3 */
641 static const char *adcr_text[] = {
642 	"ADCL", "ADCR",
643 };
644 
645 static const struct soc_enum adcr_enum =
646 	SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 2, 2, adcr_text);
647 
648 static const struct snd_kcontrol_new adcr_mux =
649 	SOC_DAPM_ENUM("ADC Right Mux", adcr_enum);
650 
651 /* ADCR EC Mux / Mux6 */
652 static const char *adcr_ec_text[] = {
653 	"ADCR", "EC",
654 };
655 
656 static const struct soc_enum adcr_ec_enum =
657 	SOC_ENUM_SINGLE(PM860X_ADC_EN_2, 3, 2, adcr_ec_text);
658 
659 static const struct snd_kcontrol_new adcr_ec_mux =
660 	SOC_DAPM_ENUM("ADCR EC Mux", adcr_ec_enum);
661 
662 /* EC Mux / Mux4 */
663 static const char *ec_text[] = {
664 	"Left", "Right", "Left + Right",
665 };
666 
667 static const struct soc_enum ec_enum =
668 	SOC_ENUM_SINGLE(PM860X_EC_PATH, 1, 3, ec_text);
669 
670 static const struct snd_kcontrol_new ec_mux =
671 	SOC_DAPM_ENUM("EC Mux", ec_enum);
672 
673 static const char *dac_text[] = {
674 	"No input", "Right", "Left", "No input",
675 };
676 
677 /* DAC Headset 1 Mux / Mux10 */
678 static const struct soc_enum dac_hs1_enum =
679 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 0, 4, dac_text);
680 
681 static const struct snd_kcontrol_new dac_hs1_mux =
682 	SOC_DAPM_ENUM("DAC HS1 Mux", dac_hs1_enum);
683 
684 /* DAC Headset 2 Mux / Mux11 */
685 static const struct soc_enum dac_hs2_enum =
686 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 2, 4, dac_text);
687 
688 static const struct snd_kcontrol_new dac_hs2_mux =
689 	SOC_DAPM_ENUM("DAC HS2 Mux", dac_hs2_enum);
690 
691 /* DAC Lineout 1 Mux / Mux12 */
692 static const struct soc_enum dac_lo1_enum =
693 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 4, 4, dac_text);
694 
695 static const struct snd_kcontrol_new dac_lo1_mux =
696 	SOC_DAPM_ENUM("DAC LO1 Mux", dac_lo1_enum);
697 
698 /* DAC Lineout 2 Mux / Mux13 */
699 static const struct soc_enum dac_lo2_enum =
700 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 6, 4, dac_text);
701 
702 static const struct snd_kcontrol_new dac_lo2_mux =
703 	SOC_DAPM_ENUM("DAC LO2 Mux", dac_lo2_enum);
704 
705 /* DAC Spearker Earphone Mux / Mux14 */
706 static const struct soc_enum dac_spk_ear_enum =
707 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_2, 0, 4, dac_text);
708 
709 static const struct snd_kcontrol_new dac_spk_ear_mux =
710 	SOC_DAPM_ENUM("DAC SP Mux", dac_spk_ear_enum);
711 
712 /* Headset 1 Mux / Mux15 */
713 static const char *in_text[] = {
714 	"Digital", "Analog",
715 };
716 
717 static const struct soc_enum hs1_enum =
718 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 0, 2, in_text);
719 
720 static const struct snd_kcontrol_new hs1_mux =
721 	SOC_DAPM_ENUM("Headset1 Mux", hs1_enum);
722 
723 /* Headset 2 Mux / Mux16 */
724 static const struct soc_enum hs2_enum =
725 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 1, 2, in_text);
726 
727 static const struct snd_kcontrol_new hs2_mux =
728 	SOC_DAPM_ENUM("Headset2 Mux", hs2_enum);
729 
730 /* Lineout 1 Mux / Mux17 */
731 static const struct soc_enum lo1_enum =
732 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 2, 2, in_text);
733 
734 static const struct snd_kcontrol_new lo1_mux =
735 	SOC_DAPM_ENUM("Lineout1 Mux", lo1_enum);
736 
737 /* Lineout 2 Mux / Mux18 */
738 static const struct soc_enum lo2_enum =
739 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 3, 2, in_text);
740 
741 static const struct snd_kcontrol_new lo2_mux =
742 	SOC_DAPM_ENUM("Lineout2 Mux", lo2_enum);
743 
744 /* Speaker Earpiece Demux */
745 static const char *spk_text[] = {
746 	"Earpiece", "Speaker",
747 };
748 
749 static const struct soc_enum spk_enum =
750 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 6, 2, spk_text);
751 
752 static const struct snd_kcontrol_new spk_demux =
753 	SOC_DAPM_ENUM("Speaker Earpiece Demux", spk_enum);
754 
755 /* MIC Mux / Mux1 */
756 static const char *mic_text[] = {
757 	"Mic 1", "Mic 2",
758 };
759 
760 static const struct soc_enum mic_enum =
761 	SOC_ENUM_SINGLE(PM860X_ADC_ANA_4, 4, 2, mic_text);
762 
763 static const struct snd_kcontrol_new mic_mux =
764 	SOC_DAPM_ENUM("MIC Mux", mic_enum);
765 
766 static const struct snd_soc_dapm_widget pm860x_dapm_widgets[] = {
767 	SND_SOC_DAPM_AIF_IN("PCM SDI", "PCM Playback", 0,
768 			    PM860X_ADC_EN_2, 0, 0),
769 	SND_SOC_DAPM_AIF_OUT("PCM SDO", "PCM Capture", 0,
770 			     PM860X_PCM_IFACE_3, 1, 1),
771 
772 
773 	SND_SOC_DAPM_AIF_IN("I2S DIN", "I2S Playback", 0,
774 			    SND_SOC_NOPM, 0, 0),
775 	SND_SOC_DAPM_AIF_IN("I2S DIN1", "I2S Playback", 0,
776 			    SND_SOC_NOPM, 0, 0),
777 	SND_SOC_DAPM_AIF_OUT("I2S DOUT", "I2S Capture", 0,
778 			     PM860X_I2S_IFACE_3, 5, 1),
779 	SND_SOC_DAPM_SUPPLY("I2S CLK", PM860X_DAC_EN_2, 0, 0, NULL, 0),
780 	SND_SOC_DAPM_MUX("I2S Mic Mux", SND_SOC_NOPM, 0, 0, &i2s_mic_mux),
781 	SND_SOC_DAPM_MUX("ADC Left Mux", SND_SOC_NOPM, 0, 0, &adcl_mux),
782 	SND_SOC_DAPM_MUX("ADC Right Mux", SND_SOC_NOPM, 0, 0, &adcr_mux),
783 	SND_SOC_DAPM_MUX("EC Mux", SND_SOC_NOPM, 0, 0, &ec_mux),
784 	SND_SOC_DAPM_MUX("ADCR EC Mux", SND_SOC_NOPM, 0, 0, &adcr_ec_mux),
785 	SND_SOC_DAPM_SWITCH("Left EPA", SND_SOC_NOPM, 0, 0,
786 			    &lepa_switch_controls),
787 	SND_SOC_DAPM_SWITCH("Right EPA", SND_SOC_NOPM, 0, 0,
788 			    &repa_switch_controls),
789 
790 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "Left ADC MOD", PM860X_ADC_EN_1,
791 			 0, 1, 1, 0),
792 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "Right ADC MOD", PM860X_ADC_EN_1,
793 			 1, 1, 1, 0),
794 	SND_SOC_DAPM_ADC("Left ADC", NULL, PM860X_ADC_EN_2, 5, 0),
795 	SND_SOC_DAPM_ADC("Right ADC", NULL, PM860X_ADC_EN_2, 4, 0),
796 
797 	SND_SOC_DAPM_SWITCH("AUX1 Switch", SND_SOC_NOPM, 0, 0,
798 			    &aux1_switch_controls),
799 	SND_SOC_DAPM_SWITCH("AUX2 Switch", SND_SOC_NOPM, 0, 0,
800 			    &aux2_switch_controls),
801 
802 	SND_SOC_DAPM_MUX("MIC Mux", SND_SOC_NOPM, 0, 0, &mic_mux),
803 	SND_SOC_DAPM_MICBIAS("Mic1 Bias", PM860X_ADC_ANA_1, 2, 0),
804 	SND_SOC_DAPM_MICBIAS("Mic3 Bias", PM860X_ADC_ANA_1, 7, 0),
805 	SND_SOC_DAPM_PGA("MIC1 Volume", PM860X_ADC_EN_1, 2, 0, NULL, 0),
806 	SND_SOC_DAPM_PGA("MIC3 Volume", PM860X_ADC_EN_1, 3, 0, NULL, 0),
807 	SND_SOC_DAPM_PGA("AUX1 Volume", PM860X_ADC_EN_1, 4, 0, NULL, 0),
808 	SND_SOC_DAPM_PGA("AUX2 Volume", PM860X_ADC_EN_1, 5, 0, NULL, 0),
809 	SND_SOC_DAPM_PGA("Sidetone PGA", PM860X_ADC_EN_2, 1, 0, NULL, 0),
810 	SND_SOC_DAPM_PGA("Lofi PGA", PM860X_ADC_EN_2, 2, 0, NULL, 0),
811 
812 	SND_SOC_DAPM_INPUT("AUX1"),
813 	SND_SOC_DAPM_INPUT("AUX2"),
814 	SND_SOC_DAPM_INPUT("MIC1P"),
815 	SND_SOC_DAPM_INPUT("MIC1N"),
816 	SND_SOC_DAPM_INPUT("MIC2P"),
817 	SND_SOC_DAPM_INPUT("MIC2N"),
818 	SND_SOC_DAPM_INPUT("MIC3P"),
819 	SND_SOC_DAPM_INPUT("MIC3N"),
820 
821 	SND_SOC_DAPM_DAC_E("Left DAC", NULL, SND_SOC_NOPM, 0, 0,
822 			   pm860x_dac_event,
823 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
824 	SND_SOC_DAPM_DAC_E("Right DAC", NULL, SND_SOC_NOPM, 0, 0,
825 			   pm860x_dac_event,
826 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
827 
828 	SND_SOC_DAPM_MUX("I2S DIN Mux", SND_SOC_NOPM, 0, 0, &i2s_din_mux),
829 	SND_SOC_DAPM_MUX("DAC HS1 Mux", SND_SOC_NOPM, 0, 0, &dac_hs1_mux),
830 	SND_SOC_DAPM_MUX("DAC HS2 Mux", SND_SOC_NOPM, 0, 0, &dac_hs2_mux),
831 	SND_SOC_DAPM_MUX("DAC LO1 Mux", SND_SOC_NOPM, 0, 0, &dac_lo1_mux),
832 	SND_SOC_DAPM_MUX("DAC LO2 Mux", SND_SOC_NOPM, 0, 0, &dac_lo2_mux),
833 	SND_SOC_DAPM_MUX("DAC SP Mux", SND_SOC_NOPM, 0, 0, &dac_spk_ear_mux),
834 	SND_SOC_DAPM_MUX("Headset1 Mux", SND_SOC_NOPM, 0, 0, &hs1_mux),
835 	SND_SOC_DAPM_MUX("Headset2 Mux", SND_SOC_NOPM, 0, 0, &hs2_mux),
836 	SND_SOC_DAPM_MUX("Lineout1 Mux", SND_SOC_NOPM, 0, 0, &lo1_mux),
837 	SND_SOC_DAPM_MUX("Lineout2 Mux", SND_SOC_NOPM, 0, 0, &lo2_mux),
838 	SND_SOC_DAPM_MUX("Speaker Earpiece Demux", SND_SOC_NOPM, 0, 0,
839 			 &spk_demux),
840 
841 
842 	SND_SOC_DAPM_PGA("Headset1 PGA", PM860X_DAC_EN_1, 0, 0, NULL, 0),
843 	SND_SOC_DAPM_PGA("Headset2 PGA", PM860X_DAC_EN_1, 1, 0, NULL, 0),
844 	SND_SOC_DAPM_OUTPUT("HS1"),
845 	SND_SOC_DAPM_OUTPUT("HS2"),
846 	SND_SOC_DAPM_PGA("Lineout1 PGA", PM860X_DAC_EN_1, 2, 0, NULL, 0),
847 	SND_SOC_DAPM_PGA("Lineout2 PGA", PM860X_DAC_EN_1, 3, 0, NULL, 0),
848 	SND_SOC_DAPM_OUTPUT("LINEOUT1"),
849 	SND_SOC_DAPM_OUTPUT("LINEOUT2"),
850 	SND_SOC_DAPM_PGA("Earpiece PGA", PM860X_DAC_EN_1, 4, 0, NULL, 0),
851 	SND_SOC_DAPM_OUTPUT("EARP"),
852 	SND_SOC_DAPM_OUTPUT("EARN"),
853 	SND_SOC_DAPM_PGA("Speaker PGA", PM860X_DAC_EN_1, 5, 0, NULL, 0),
854 	SND_SOC_DAPM_OUTPUT("LSP"),
855 	SND_SOC_DAPM_OUTPUT("LSN"),
856 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "VCODEC", PM860X_AUDIO_SUPPLIES_2,
857 			 0, SUPPLY_MASK, SUPPLY_MASK, 0),
858 
859 	PM860X_DAPM_OUTPUT("RSYNC", pm860x_rsync_event),
860 };
861 
862 static const struct snd_soc_dapm_route pm860x_dapm_routes[] = {
863 	/* supply */
864 	{"Left DAC", NULL, "VCODEC"},
865 	{"Right DAC", NULL, "VCODEC"},
866 	{"Left ADC", NULL, "VCODEC"},
867 	{"Right ADC", NULL, "VCODEC"},
868 	{"Left ADC", NULL, "Left ADC MOD"},
869 	{"Right ADC", NULL, "Right ADC MOD"},
870 
871 	/* I2S Clock */
872 	{"I2S DIN", NULL, "I2S CLK"},
873 	{"I2S DIN1", NULL, "I2S CLK"},
874 	{"I2S DOUT", NULL, "I2S CLK"},
875 
876 	/* PCM/AIF1 Inputs */
877 	{"PCM SDO", NULL, "ADC Left Mux"},
878 	{"PCM SDO", NULL, "ADCR EC Mux"},
879 
880 	/* PCM/AFI2 Outputs */
881 	{"Lofi PGA", NULL, "PCM SDI"},
882 	{"Lofi PGA", NULL, "Sidetone PGA"},
883 	{"Left DAC", NULL, "Lofi PGA"},
884 	{"Right DAC", NULL, "Lofi PGA"},
885 
886 	/* I2S/AIF2 Inputs */
887 	{"MIC Mux", "Mic 1", "MIC1P"},
888 	{"MIC Mux", "Mic 1", "MIC1N"},
889 	{"MIC Mux", "Mic 2", "MIC2P"},
890 	{"MIC Mux", "Mic 2", "MIC2N"},
891 	{"MIC1 Volume", NULL, "MIC Mux"},
892 	{"MIC3 Volume", NULL, "MIC3P"},
893 	{"MIC3 Volume", NULL, "MIC3N"},
894 	{"Left ADC", NULL, "MIC1 Volume"},
895 	{"Right ADC", NULL, "MIC3 Volume"},
896 	{"ADC Left Mux", "ADCR", "Right ADC"},
897 	{"ADC Left Mux", "ADCL", "Left ADC"},
898 	{"ADC Right Mux", "ADCL", "Left ADC"},
899 	{"ADC Right Mux", "ADCR", "Right ADC"},
900 	{"Left EPA", "Switch", "Left DAC"},
901 	{"Right EPA", "Switch", "Right DAC"},
902 	{"EC Mux", "Left", "Left DAC"},
903 	{"EC Mux", "Right", "Right DAC"},
904 	{"EC Mux", "Left + Right", "Left DAC"},
905 	{"EC Mux", "Left + Right", "Right DAC"},
906 	{"ADCR EC Mux", "ADCR", "ADC Right Mux"},
907 	{"ADCR EC Mux", "EC", "EC Mux"},
908 	{"I2S Mic Mux", "Ex PA", "Left EPA"},
909 	{"I2S Mic Mux", "Ex PA", "Right EPA"},
910 	{"I2S Mic Mux", "ADC", "ADC Left Mux"},
911 	{"I2S Mic Mux", "ADC", "ADCR EC Mux"},
912 	{"I2S DOUT", NULL, "I2S Mic Mux"},
913 
914 	/* I2S/AIF2 Outputs */
915 	{"I2S DIN Mux", "DIN", "I2S DIN"},
916 	{"I2S DIN Mux", "DIN1", "I2S DIN1"},
917 	{"Left DAC", NULL, "I2S DIN Mux"},
918 	{"Right DAC", NULL, "I2S DIN Mux"},
919 	{"DAC HS1 Mux", "Left", "Left DAC"},
920 	{"DAC HS1 Mux", "Right", "Right DAC"},
921 	{"DAC HS2 Mux", "Left", "Left DAC"},
922 	{"DAC HS2 Mux", "Right", "Right DAC"},
923 	{"DAC LO1 Mux", "Left", "Left DAC"},
924 	{"DAC LO1 Mux", "Right", "Right DAC"},
925 	{"DAC LO2 Mux", "Left", "Left DAC"},
926 	{"DAC LO2 Mux", "Right", "Right DAC"},
927 	{"Headset1 Mux", "Digital", "DAC HS1 Mux"},
928 	{"Headset2 Mux", "Digital", "DAC HS2 Mux"},
929 	{"Lineout1 Mux", "Digital", "DAC LO1 Mux"},
930 	{"Lineout2 Mux", "Digital", "DAC LO2 Mux"},
931 	{"Headset1 PGA", NULL, "Headset1 Mux"},
932 	{"Headset2 PGA", NULL, "Headset2 Mux"},
933 	{"Lineout1 PGA", NULL, "Lineout1 Mux"},
934 	{"Lineout2 PGA", NULL, "Lineout2 Mux"},
935 	{"DAC SP Mux", "Left", "Left DAC"},
936 	{"DAC SP Mux", "Right", "Right DAC"},
937 	{"Speaker Earpiece Demux", "Speaker", "DAC SP Mux"},
938 	{"Speaker PGA", NULL, "Speaker Earpiece Demux"},
939 	{"Earpiece PGA", NULL, "Speaker Earpiece Demux"},
940 
941 	{"RSYNC", NULL, "Headset1 PGA"},
942 	{"RSYNC", NULL, "Headset2 PGA"},
943 	{"RSYNC", NULL, "Lineout1 PGA"},
944 	{"RSYNC", NULL, "Lineout2 PGA"},
945 	{"RSYNC", NULL, "Speaker PGA"},
946 	{"RSYNC", NULL, "Speaker PGA"},
947 	{"RSYNC", NULL, "Earpiece PGA"},
948 	{"RSYNC", NULL, "Earpiece PGA"},
949 
950 	{"HS1", NULL, "RSYNC"},
951 	{"HS2", NULL, "RSYNC"},
952 	{"LINEOUT1", NULL, "RSYNC"},
953 	{"LINEOUT2", NULL, "RSYNC"},
954 	{"LSP", NULL, "RSYNC"},
955 	{"LSN", NULL, "RSYNC"},
956 	{"EARP", NULL, "RSYNC"},
957 	{"EARN", NULL, "RSYNC"},
958 };
959 
960 /*
961  * Use MUTE_LEFT & MUTE_RIGHT to implement digital mute.
962  * These bits can also be used to mute.
963  */
964 static int pm860x_digital_mute(struct snd_soc_dai *codec_dai, int mute)
965 {
966 	struct snd_soc_codec *codec = codec_dai->codec;
967 	int data = 0, mask = MUTE_LEFT | MUTE_RIGHT;
968 
969 	if (mute)
970 		data = mask;
971 	snd_soc_update_bits(codec, PM860X_DAC_OFFSET, mask, data);
972 	snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
973 			    RSYNC_CHANGE, RSYNC_CHANGE);
974 	return 0;
975 }
976 
977 static int pm860x_pcm_hw_params(struct snd_pcm_substream *substream,
978 				struct snd_pcm_hw_params *params,
979 				struct snd_soc_dai *dai)
980 {
981 	struct snd_soc_codec *codec = dai->codec;
982 	unsigned char inf = 0, mask = 0;
983 
984 	/* bit size */
985 	switch (params_format(params)) {
986 	case SNDRV_PCM_FORMAT_S16_LE:
987 		inf &= ~PCM_INF2_18WL;
988 		break;
989 	case SNDRV_PCM_FORMAT_S18_3LE:
990 		inf |= PCM_INF2_18WL;
991 		break;
992 	default:
993 		return -EINVAL;
994 	}
995 	mask |= PCM_INF2_18WL;
996 	snd_soc_update_bits(codec, PM860X_PCM_IFACE_2, mask, inf);
997 
998 	/* sample rate */
999 	switch (params_rate(params)) {
1000 	case 8000:
1001 		inf = 0;
1002 		break;
1003 	case 16000:
1004 		inf = 3;
1005 		break;
1006 	case 32000:
1007 		inf = 6;
1008 		break;
1009 	case 48000:
1010 		inf = 8;
1011 		break;
1012 	default:
1013 		return -EINVAL;
1014 	}
1015 	snd_soc_update_bits(codec, PM860X_PCM_RATE, 0x0f, inf);
1016 
1017 	return 0;
1018 }
1019 
1020 static int pm860x_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
1021 				  unsigned int fmt)
1022 {
1023 	struct snd_soc_codec *codec = codec_dai->codec;
1024 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1025 	unsigned char inf = 0, mask = 0;
1026 	int ret = -EINVAL;
1027 
1028 	mask |= PCM_INF2_BCLK | PCM_INF2_FS | PCM_INF2_MASTER;
1029 
1030 	/* set master/slave audio interface */
1031 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1032 	case SND_SOC_DAIFMT_CBM_CFM:
1033 	case SND_SOC_DAIFMT_CBM_CFS:
1034 		if (pm860x->dir == PM860X_CLK_DIR_OUT) {
1035 			inf |= PCM_INF2_MASTER;
1036 			ret = 0;
1037 		}
1038 		break;
1039 	case SND_SOC_DAIFMT_CBS_CFS:
1040 		if (pm860x->dir == PM860X_CLK_DIR_IN) {
1041 			inf &= ~PCM_INF2_MASTER;
1042 			ret = 0;
1043 		}
1044 		break;
1045 	}
1046 
1047 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1048 	case SND_SOC_DAIFMT_I2S:
1049 		inf |= PCM_EXACT_I2S;
1050 		ret = 0;
1051 		break;
1052 	}
1053 	mask |= PCM_MODE_MASK;
1054 	if (ret)
1055 		return ret;
1056 	snd_soc_update_bits(codec, PM860X_PCM_IFACE_2, mask, inf);
1057 	return 0;
1058 }
1059 
1060 static int pm860x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1061 				 int clk_id, unsigned int freq, int dir)
1062 {
1063 	struct snd_soc_codec *codec = codec_dai->codec;
1064 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1065 
1066 	if (dir == PM860X_CLK_DIR_OUT)
1067 		pm860x->dir = PM860X_CLK_DIR_OUT;
1068 	else {
1069 		pm860x->dir = PM860X_CLK_DIR_IN;
1070 		return -EINVAL;
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 static int pm860x_i2s_hw_params(struct snd_pcm_substream *substream,
1077 				struct snd_pcm_hw_params *params,
1078 				struct snd_soc_dai *dai)
1079 {
1080 	struct snd_soc_codec *codec = dai->codec;
1081 	unsigned char inf;
1082 
1083 	/* bit size */
1084 	switch (params_format(params)) {
1085 	case SNDRV_PCM_FORMAT_S16_LE:
1086 		inf = 0;
1087 		break;
1088 	case SNDRV_PCM_FORMAT_S18_3LE:
1089 		inf = PCM_INF2_18WL;
1090 		break;
1091 	default:
1092 		return -EINVAL;
1093 	}
1094 	snd_soc_update_bits(codec, PM860X_I2S_IFACE_2, PCM_INF2_18WL, inf);
1095 
1096 	/* sample rate */
1097 	switch (params_rate(params)) {
1098 	case 8000:
1099 		inf = 0;
1100 		break;
1101 	case 11025:
1102 		inf = 1;
1103 		break;
1104 	case 16000:
1105 		inf = 3;
1106 		break;
1107 	case 22050:
1108 		inf = 4;
1109 		break;
1110 	case 32000:
1111 		inf = 6;
1112 		break;
1113 	case 44100:
1114 		inf = 7;
1115 		break;
1116 	case 48000:
1117 		inf = 8;
1118 		break;
1119 	default:
1120 		return -EINVAL;
1121 	}
1122 	snd_soc_update_bits(codec, PM860X_I2S_IFACE_4, 0xf, inf);
1123 
1124 	return 0;
1125 }
1126 
1127 static int pm860x_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
1128 				  unsigned int fmt)
1129 {
1130 	struct snd_soc_codec *codec = codec_dai->codec;
1131 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1132 	unsigned char inf = 0, mask = 0;
1133 
1134 	mask |= PCM_INF2_BCLK | PCM_INF2_FS | PCM_INF2_MASTER;
1135 
1136 	/* set master/slave audio interface */
1137 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1138 	case SND_SOC_DAIFMT_CBM_CFM:
1139 		if (pm860x->dir == PM860X_CLK_DIR_OUT)
1140 			inf |= PCM_INF2_MASTER;
1141 		else
1142 			return -EINVAL;
1143 		break;
1144 	case SND_SOC_DAIFMT_CBS_CFS:
1145 		if (pm860x->dir == PM860X_CLK_DIR_IN)
1146 			inf &= ~PCM_INF2_MASTER;
1147 		else
1148 			return -EINVAL;
1149 		break;
1150 	default:
1151 		return -EINVAL;
1152 	}
1153 
1154 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1155 	case SND_SOC_DAIFMT_I2S:
1156 		inf |= PCM_EXACT_I2S;
1157 		break;
1158 	default:
1159 		return -EINVAL;
1160 	}
1161 	mask |= PCM_MODE_MASK;
1162 	snd_soc_update_bits(codec, PM860X_I2S_IFACE_2, mask, inf);
1163 	return 0;
1164 }
1165 
1166 static int pm860x_set_bias_level(struct snd_soc_codec *codec,
1167 				 enum snd_soc_bias_level level)
1168 {
1169 	int data;
1170 
1171 	switch (level) {
1172 	case SND_SOC_BIAS_ON:
1173 		break;
1174 
1175 	case SND_SOC_BIAS_PREPARE:
1176 		break;
1177 
1178 	case SND_SOC_BIAS_STANDBY:
1179 		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
1180 			/* Enable Audio PLL & Audio section */
1181 			data = AUDIO_PLL | AUDIO_SECTION_ON;
1182 			pm860x_reg_write(codec->control_data, REG_MISC2, data);
1183 			udelay(300);
1184 			data = AUDIO_PLL | AUDIO_SECTION_RESET
1185 				| AUDIO_SECTION_ON;
1186 			pm860x_reg_write(codec->control_data, REG_MISC2, data);
1187 		}
1188 		break;
1189 
1190 	case SND_SOC_BIAS_OFF:
1191 		data = AUDIO_PLL | AUDIO_SECTION_RESET | AUDIO_SECTION_ON;
1192 		pm860x_set_bits(codec->control_data, REG_MISC2, data, 0);
1193 		break;
1194 	}
1195 	codec->dapm.bias_level = level;
1196 	return 0;
1197 }
1198 
1199 static const struct snd_soc_dai_ops pm860x_pcm_dai_ops = {
1200 	.digital_mute	= pm860x_digital_mute,
1201 	.hw_params	= pm860x_pcm_hw_params,
1202 	.set_fmt	= pm860x_pcm_set_dai_fmt,
1203 	.set_sysclk	= pm860x_set_dai_sysclk,
1204 };
1205 
1206 static const struct snd_soc_dai_ops pm860x_i2s_dai_ops = {
1207 	.digital_mute	= pm860x_digital_mute,
1208 	.hw_params	= pm860x_i2s_hw_params,
1209 	.set_fmt	= pm860x_i2s_set_dai_fmt,
1210 	.set_sysclk	= pm860x_set_dai_sysclk,
1211 };
1212 
1213 #define PM860X_RATES	(SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |	\
1214 			 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000)
1215 
1216 static struct snd_soc_dai_driver pm860x_dai[] = {
1217 	{
1218 		/* DAI PCM */
1219 		.name	= "88pm860x-pcm",
1220 		.id	= 1,
1221 		.playback = {
1222 			.stream_name	= "PCM Playback",
1223 			.channels_min	= 2,
1224 			.channels_max	= 2,
1225 			.rates		= PM860X_RATES,
1226 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1227 					  SNDRV_PCM_FORMAT_S18_3LE,
1228 		},
1229 		.capture = {
1230 			.stream_name	= "PCM Capture",
1231 			.channels_min	= 2,
1232 			.channels_max	= 2,
1233 			.rates		= PM860X_RATES,
1234 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1235 					  SNDRV_PCM_FORMAT_S18_3LE,
1236 		},
1237 		.ops	= &pm860x_pcm_dai_ops,
1238 	}, {
1239 		/* DAI I2S */
1240 		.name	= "88pm860x-i2s",
1241 		.id	= 2,
1242 		.playback = {
1243 			.stream_name	= "I2S Playback",
1244 			.channels_min	= 2,
1245 			.channels_max	= 2,
1246 			.rates		= SNDRV_PCM_RATE_8000_48000,
1247 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1248 					  SNDRV_PCM_FORMAT_S18_3LE,
1249 		},
1250 		.capture = {
1251 			.stream_name	= "I2S Capture",
1252 			.channels_min	= 2,
1253 			.channels_max	= 2,
1254 			.rates		= SNDRV_PCM_RATE_8000_48000,
1255 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1256 					  SNDRV_PCM_FORMAT_S18_3LE,
1257 		},
1258 		.ops	= &pm860x_i2s_dai_ops,
1259 	},
1260 };
1261 
1262 static irqreturn_t pm860x_codec_handler(int irq, void *data)
1263 {
1264 	struct pm860x_priv *pm860x = data;
1265 	int status, shrt, report = 0, mic_report = 0;
1266 	int mask;
1267 
1268 	status = pm860x_reg_read(pm860x->i2c, REG_STATUS_1);
1269 	shrt = pm860x_reg_read(pm860x->i2c, REG_SHORTS);
1270 	mask = pm860x->det.hs_shrt | pm860x->det.hook_det | pm860x->det.lo_shrt
1271 		| pm860x->det.hp_det;
1272 
1273 #ifndef CONFIG_SND_SOC_88PM860X_MODULE
1274 	if (status & (HEADSET_STATUS | MIC_STATUS | SHORT_HS1 | SHORT_HS2 |
1275 		      SHORT_LO1 | SHORT_LO2))
1276 		trace_snd_soc_jack_irq(dev_name(pm860x->codec->dev));
1277 #endif
1278 
1279 	if ((pm860x->det.hp_det & SND_JACK_HEADPHONE)
1280 		&& (status & HEADSET_STATUS))
1281 		report |= SND_JACK_HEADPHONE;
1282 
1283 	if ((pm860x->det.mic_det & SND_JACK_MICROPHONE)
1284 		&& (status & MIC_STATUS))
1285 		mic_report |= SND_JACK_MICROPHONE;
1286 
1287 	if (pm860x->det.hs_shrt && (shrt & (SHORT_HS1 | SHORT_HS2)))
1288 		report |= pm860x->det.hs_shrt;
1289 
1290 	if (pm860x->det.hook_det && (status & HOOK_STATUS))
1291 		report |= pm860x->det.hook_det;
1292 
1293 	if (pm860x->det.lo_shrt && (shrt & (SHORT_LO1 | SHORT_LO2)))
1294 		report |= pm860x->det.lo_shrt;
1295 
1296 	if (report)
1297 		snd_soc_jack_report(pm860x->det.hp_jack, report, mask);
1298 	if (mic_report)
1299 		snd_soc_jack_report(pm860x->det.mic_jack, SND_JACK_MICROPHONE,
1300 				    SND_JACK_MICROPHONE);
1301 
1302 	dev_dbg(pm860x->codec->dev, "headphone report:0x%x, mask:%x\n",
1303 		report, mask);
1304 	dev_dbg(pm860x->codec->dev, "microphone report:0x%x\n", mic_report);
1305 	return IRQ_HANDLED;
1306 }
1307 
1308 int pm860x_hs_jack_detect(struct snd_soc_codec *codec,
1309 			  struct snd_soc_jack *jack,
1310 			  int det, int hook, int hs_shrt, int lo_shrt)
1311 {
1312 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1313 	int data;
1314 
1315 	pm860x->det.hp_jack = jack;
1316 	pm860x->det.hp_det = det;
1317 	pm860x->det.hook_det = hook;
1318 	pm860x->det.hs_shrt = hs_shrt;
1319 	pm860x->det.lo_shrt = lo_shrt;
1320 
1321 	if (det & SND_JACK_HEADPHONE)
1322 		pm860x_set_bits(codec->control_data, REG_HS_DET,
1323 				EN_HS_DET, EN_HS_DET);
1324 	/* headset short detect */
1325 	if (hs_shrt) {
1326 		data = CLR_SHORT_HS2 | CLR_SHORT_HS1;
1327 		pm860x_set_bits(codec->control_data, REG_SHORTS, data, data);
1328 	}
1329 	/* Lineout short detect */
1330 	if (lo_shrt) {
1331 		data = CLR_SHORT_LO2 | CLR_SHORT_LO1;
1332 		pm860x_set_bits(codec->control_data, REG_SHORTS, data, data);
1333 	}
1334 
1335 	/* sync status */
1336 	pm860x_codec_handler(0, pm860x);
1337 	return 0;
1338 }
1339 EXPORT_SYMBOL_GPL(pm860x_hs_jack_detect);
1340 
1341 int pm860x_mic_jack_detect(struct snd_soc_codec *codec,
1342 			   struct snd_soc_jack *jack, int det)
1343 {
1344 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1345 
1346 	pm860x->det.mic_jack = jack;
1347 	pm860x->det.mic_det = det;
1348 
1349 	if (det & SND_JACK_MICROPHONE)
1350 		pm860x_set_bits(codec->control_data, REG_MIC_DET,
1351 				MICDET_MASK, MICDET_MASK);
1352 
1353 	/* sync status */
1354 	pm860x_codec_handler(0, pm860x);
1355 	return 0;
1356 }
1357 EXPORT_SYMBOL_GPL(pm860x_mic_jack_detect);
1358 
1359 static int pm860x_probe(struct snd_soc_codec *codec)
1360 {
1361 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1362 	int i, ret;
1363 
1364 	pm860x->codec = codec;
1365 
1366 	codec->control_data = pm860x->i2c;
1367 
1368 	for (i = 0; i < 4; i++) {
1369 		ret = request_threaded_irq(pm860x->irq[i], NULL,
1370 					   pm860x_codec_handler, IRQF_ONESHOT,
1371 					   pm860x->name[i], pm860x);
1372 		if (ret < 0) {
1373 			dev_err(codec->dev, "Failed to request IRQ!\n");
1374 			goto out;
1375 		}
1376 	}
1377 
1378 	pm860x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1379 
1380 	ret = pm860x_bulk_read(codec->control_data, REG_CACHE_BASE,
1381 			       REG_CACHE_SIZE, codec->reg_cache);
1382 	if (ret < 0) {
1383 		dev_err(codec->dev, "Failed to fill register cache: %d\n",
1384 			ret);
1385 		goto out;
1386 	}
1387 
1388 	return 0;
1389 
1390 out:
1391 	while (--i >= 0)
1392 		free_irq(pm860x->irq[i], pm860x);
1393 	return ret;
1394 }
1395 
1396 static int pm860x_remove(struct snd_soc_codec *codec)
1397 {
1398 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1399 	int i;
1400 
1401 	for (i = 3; i >= 0; i--)
1402 		free_irq(pm860x->irq[i], pm860x);
1403 	pm860x_set_bias_level(codec, SND_SOC_BIAS_OFF);
1404 	return 0;
1405 }
1406 
1407 static struct snd_soc_codec_driver soc_codec_dev_pm860x = {
1408 	.probe		= pm860x_probe,
1409 	.remove		= pm860x_remove,
1410 	.read		= pm860x_read_reg_cache,
1411 	.write		= pm860x_write_reg_cache,
1412 	.reg_cache_size	= REG_CACHE_SIZE,
1413 	.reg_word_size	= sizeof(u8),
1414 	.set_bias_level	= pm860x_set_bias_level,
1415 
1416 	.controls = pm860x_snd_controls,
1417 	.num_controls = ARRAY_SIZE(pm860x_snd_controls),
1418 	.dapm_widgets = pm860x_dapm_widgets,
1419 	.num_dapm_widgets = ARRAY_SIZE(pm860x_dapm_widgets),
1420 	.dapm_routes = pm860x_dapm_routes,
1421 	.num_dapm_routes = ARRAY_SIZE(pm860x_dapm_routes),
1422 };
1423 
1424 static int pm860x_codec_probe(struct platform_device *pdev)
1425 {
1426 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
1427 	struct pm860x_priv *pm860x;
1428 	struct resource *res;
1429 	int i, ret;
1430 
1431 	pm860x = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_priv),
1432 			      GFP_KERNEL);
1433 	if (pm860x == NULL)
1434 		return -ENOMEM;
1435 
1436 	pm860x->chip = chip;
1437 	pm860x->i2c = (chip->id == CHIP_PM8607) ? chip->client
1438 			: chip->companion;
1439 	platform_set_drvdata(pdev, pm860x);
1440 
1441 	for (i = 0; i < 4; i++) {
1442 		res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1443 		if (!res) {
1444 			dev_err(&pdev->dev, "Failed to get IRQ resources\n");
1445 			return -EINVAL;
1446 		}
1447 		pm860x->irq[i] = res->start + chip->irq_base;
1448 		strncpy(pm860x->name[i], res->name, MAX_NAME_LEN);
1449 	}
1450 
1451 	ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pm860x,
1452 				     pm860x_dai, ARRAY_SIZE(pm860x_dai));
1453 	if (ret) {
1454 		dev_err(&pdev->dev, "Failed to register codec\n");
1455 		return -EINVAL;
1456 	}
1457 	return ret;
1458 }
1459 
1460 static int pm860x_codec_remove(struct platform_device *pdev)
1461 {
1462 	snd_soc_unregister_codec(&pdev->dev);
1463 	return 0;
1464 }
1465 
1466 static struct platform_driver pm860x_codec_driver = {
1467 	.driver	= {
1468 		.name	= "88pm860x-codec",
1469 		.owner	= THIS_MODULE,
1470 	},
1471 	.probe	= pm860x_codec_probe,
1472 	.remove	= pm860x_codec_remove,
1473 };
1474 
1475 module_platform_driver(pm860x_codec_driver);
1476 
1477 MODULE_DESCRIPTION("ASoC 88PM860x driver");
1478 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1479 MODULE_LICENSE("GPL");
1480 MODULE_ALIAS("platform:88pm860x-codec");
1481 
1482