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 	if (val >= ARRAY_SIZE(st_table) || val2 >= ARRAY_SIZE(st_table))
353 		return -EINVAL;
354 
355 	err = snd_soc_update_bits(codec, reg, 0x3f, st_table[val].m);
356 	if (err < 0)
357 		return err;
358 	err = snd_soc_update_bits(codec, PM860X_SIDETONE_SHIFT, 0xf0,
359 				  st_table[val].n << 4);
360 	if (err < 0)
361 		return err;
362 
363 	err = snd_soc_update_bits(codec, reg2, 0x3f, st_table[val2].m);
364 	if (err < 0)
365 		return err;
366 	err = snd_soc_update_bits(codec, PM860X_SIDETONE_SHIFT, 0x0f,
367 				  st_table[val2].n);
368 	return err;
369 }
370 
371 static int snd_soc_get_volsw_2r_out(struct snd_kcontrol *kcontrol,
372 				    struct snd_ctl_elem_value *ucontrol)
373 {
374 	struct soc_mixer_control *mc =
375 		(struct soc_mixer_control *)kcontrol->private_value;
376 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
377 	unsigned int reg = mc->reg;
378 	unsigned int reg2 = mc->rreg;
379 	unsigned int shift = mc->shift;
380 	int max = mc->max, val, val2;
381 	unsigned int mask = (1 << fls(max)) - 1;
382 
383 	val = snd_soc_read(codec, reg) >> shift;
384 	val2 = snd_soc_read(codec, reg2) >> shift;
385 	ucontrol->value.integer.value[0] = (max - val) & mask;
386 	ucontrol->value.integer.value[1] = (max - val2) & mask;
387 
388 	return 0;
389 }
390 
391 static int snd_soc_put_volsw_2r_out(struct snd_kcontrol *kcontrol,
392 				    struct snd_ctl_elem_value *ucontrol)
393 {
394 	struct soc_mixer_control *mc =
395 		(struct soc_mixer_control *)kcontrol->private_value;
396 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
397 	unsigned int reg = mc->reg;
398 	unsigned int reg2 = mc->rreg;
399 	unsigned int shift = mc->shift;
400 	int max = mc->max;
401 	unsigned int mask = (1 << fls(max)) - 1;
402 	int err;
403 	unsigned int val, val2, val_mask;
404 
405 	val_mask = mask << shift;
406 	val = ((max - ucontrol->value.integer.value[0]) & mask);
407 	val2 = ((max - ucontrol->value.integer.value[1]) & mask);
408 
409 	val = val << shift;
410 	val2 = val2 << shift;
411 
412 	err = snd_soc_update_bits(codec, reg, val_mask, val);
413 	if (err < 0)
414 		return err;
415 
416 	err = snd_soc_update_bits(codec, reg2, val_mask, val2);
417 	return err;
418 }
419 
420 /* DAPM Widget Events */
421 /*
422  * A lot registers are belong to RSYNC domain. It requires enabling RSYNC bit
423  * after updating these registers. Otherwise, these updated registers won't
424  * be effective.
425  */
426 static int pm860x_rsync_event(struct snd_soc_dapm_widget *w,
427 			      struct snd_kcontrol *kcontrol, int event)
428 {
429 	struct snd_soc_codec *codec = w->codec;
430 
431 	/*
432 	 * In order to avoid current on the load, mute power-on and power-off
433 	 * should be transients.
434 	 * Unmute by DAC_MUTE. It should be unmuted when DAPM sequence is
435 	 * finished.
436 	 */
437 	snd_soc_update_bits(codec, PM860X_DAC_OFFSET, DAC_MUTE, 0);
438 	snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
439 			    RSYNC_CHANGE, RSYNC_CHANGE);
440 	return 0;
441 }
442 
443 static int pm860x_dac_event(struct snd_soc_dapm_widget *w,
444 			    struct snd_kcontrol *kcontrol, int event)
445 {
446 	struct snd_soc_codec *codec = w->codec;
447 	unsigned int dac = 0;
448 	int data;
449 
450 	if (!strcmp(w->name, "Left DAC"))
451 		dac = DAC_LEFT;
452 	if (!strcmp(w->name, "Right DAC"))
453 		dac = DAC_RIGHT;
454 	switch (event) {
455 	case SND_SOC_DAPM_PRE_PMU:
456 		if (dac) {
457 			/* Auto mute in power-on sequence. */
458 			dac |= MODULATOR;
459 			snd_soc_update_bits(codec, PM860X_DAC_OFFSET,
460 					    DAC_MUTE, DAC_MUTE);
461 			snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
462 					    RSYNC_CHANGE, RSYNC_CHANGE);
463 			/* update dac */
464 			snd_soc_update_bits(codec, PM860X_DAC_EN_2,
465 					    dac, dac);
466 		}
467 		break;
468 	case SND_SOC_DAPM_PRE_PMD:
469 		if (dac) {
470 			/* Auto mute in power-off sequence. */
471 			snd_soc_update_bits(codec, PM860X_DAC_OFFSET,
472 					    DAC_MUTE, DAC_MUTE);
473 			snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
474 					    RSYNC_CHANGE, RSYNC_CHANGE);
475 			/* update dac */
476 			data = snd_soc_read(codec, PM860X_DAC_EN_2);
477 			data &= ~dac;
478 			if (!(data & (DAC_LEFT | DAC_RIGHT)))
479 				data &= ~MODULATOR;
480 			snd_soc_write(codec, PM860X_DAC_EN_2, data);
481 		}
482 		break;
483 	}
484 	return 0;
485 }
486 
487 static const char *pm860x_opamp_texts[] = {"-50%", "-25%", "0%", "75%"};
488 
489 static const char *pm860x_pa_texts[] = {"-33%", "0%", "33%", "66%"};
490 
491 static const struct soc_enum pm860x_hs1_opamp_enum =
492 	SOC_ENUM_SINGLE(PM860X_HS1_CTRL, 5, 4, pm860x_opamp_texts);
493 
494 static const struct soc_enum pm860x_hs2_opamp_enum =
495 	SOC_ENUM_SINGLE(PM860X_HS2_CTRL, 5, 4, pm860x_opamp_texts);
496 
497 static const struct soc_enum pm860x_hs1_pa_enum =
498 	SOC_ENUM_SINGLE(PM860X_HS1_CTRL, 3, 4, pm860x_pa_texts);
499 
500 static const struct soc_enum pm860x_hs2_pa_enum =
501 	SOC_ENUM_SINGLE(PM860X_HS2_CTRL, 3, 4, pm860x_pa_texts);
502 
503 static const struct soc_enum pm860x_lo1_opamp_enum =
504 	SOC_ENUM_SINGLE(PM860X_LO1_CTRL, 5, 4, pm860x_opamp_texts);
505 
506 static const struct soc_enum pm860x_lo2_opamp_enum =
507 	SOC_ENUM_SINGLE(PM860X_LO2_CTRL, 5, 4, pm860x_opamp_texts);
508 
509 static const struct soc_enum pm860x_lo1_pa_enum =
510 	SOC_ENUM_SINGLE(PM860X_LO1_CTRL, 3, 4, pm860x_pa_texts);
511 
512 static const struct soc_enum pm860x_lo2_pa_enum =
513 	SOC_ENUM_SINGLE(PM860X_LO2_CTRL, 3, 4, pm860x_pa_texts);
514 
515 static const struct soc_enum pm860x_spk_pa_enum =
516 	SOC_ENUM_SINGLE(PM860X_EAR_CTRL_1, 5, 4, pm860x_pa_texts);
517 
518 static const struct soc_enum pm860x_ear_pa_enum =
519 	SOC_ENUM_SINGLE(PM860X_EAR_CTRL_2, 0, 4, pm860x_pa_texts);
520 
521 static const struct soc_enum pm860x_spk_ear_opamp_enum =
522 	SOC_ENUM_SINGLE(PM860X_EAR_CTRL_1, 3, 4, pm860x_opamp_texts);
523 
524 static const struct snd_kcontrol_new pm860x_snd_controls[] = {
525 	SOC_DOUBLE_R_TLV("ADC Capture Volume", PM860X_ADC_ANA_2,
526 			PM860X_ADC_ANA_3, 6, 3, 0, adc_tlv),
527 	SOC_DOUBLE_TLV("AUX Capture Volume", PM860X_ADC_ANA_3, 0, 3, 7, 0,
528 			aux_tlv),
529 	SOC_SINGLE_TLV("MIC1 Capture Volume", PM860X_ADC_ANA_2, 0, 7, 0,
530 			mic_tlv),
531 	SOC_SINGLE_TLV("MIC3 Capture Volume", PM860X_ADC_ANA_2, 3, 7, 0,
532 			mic_tlv),
533 	SOC_DOUBLE_R_EXT_TLV("Sidetone Volume", PM860X_SIDETONE_L_GAIN,
534 			     PM860X_SIDETONE_R_GAIN, 0, ARRAY_SIZE(st_table)-1,
535 			     0, snd_soc_get_volsw_2r_st,
536 			     snd_soc_put_volsw_2r_st, st_tlv),
537 	SOC_SINGLE_TLV("Speaker Playback Volume", PM860X_EAR_CTRL_1,
538 			0, 7, 0, out_tlv),
539 	SOC_DOUBLE_R_TLV("Line Playback Volume", PM860X_LO1_CTRL,
540 			 PM860X_LO2_CTRL, 0, 7, 0, out_tlv),
541 	SOC_DOUBLE_R_TLV("Headset Playback Volume", PM860X_HS1_CTRL,
542 			 PM860X_HS2_CTRL, 0, 7, 0, out_tlv),
543 	SOC_DOUBLE_R_EXT_TLV("Hifi Left Playback Volume",
544 			     PM860X_HIFIL_GAIN_LEFT,
545 			     PM860X_HIFIL_GAIN_RIGHT, 0, 63, 0,
546 			     snd_soc_get_volsw_2r_out,
547 			     snd_soc_put_volsw_2r_out, dpga_tlv),
548 	SOC_DOUBLE_R_EXT_TLV("Hifi Right Playback Volume",
549 			     PM860X_HIFIR_GAIN_LEFT,
550 			     PM860X_HIFIR_GAIN_RIGHT, 0, 63, 0,
551 			     snd_soc_get_volsw_2r_out,
552 			     snd_soc_put_volsw_2r_out, dpga_tlv),
553 	SOC_DOUBLE_R_EXT_TLV("Lofi Playback Volume", PM860X_LOFI_GAIN_LEFT,
554 			     PM860X_LOFI_GAIN_RIGHT, 0, 63, 0,
555 			     snd_soc_get_volsw_2r_out,
556 			     snd_soc_put_volsw_2r_out, dpga_tlv),
557 	SOC_ENUM("Headset1 Operational Amplifier Current",
558 		 pm860x_hs1_opamp_enum),
559 	SOC_ENUM("Headset2 Operational Amplifier Current",
560 		 pm860x_hs2_opamp_enum),
561 	SOC_ENUM("Headset1 Amplifier Current", pm860x_hs1_pa_enum),
562 	SOC_ENUM("Headset2 Amplifier Current", pm860x_hs2_pa_enum),
563 	SOC_ENUM("Lineout1 Operational Amplifier Current",
564 		 pm860x_lo1_opamp_enum),
565 	SOC_ENUM("Lineout2 Operational Amplifier Current",
566 		 pm860x_lo2_opamp_enum),
567 	SOC_ENUM("Lineout1 Amplifier Current", pm860x_lo1_pa_enum),
568 	SOC_ENUM("Lineout2 Amplifier Current", pm860x_lo2_pa_enum),
569 	SOC_ENUM("Speaker Operational Amplifier Current",
570 		 pm860x_spk_ear_opamp_enum),
571 	SOC_ENUM("Speaker Amplifier Current", pm860x_spk_pa_enum),
572 	SOC_ENUM("Earpiece Amplifier Current", pm860x_ear_pa_enum),
573 };
574 
575 /*
576  * DAPM Controls
577  */
578 
579 /* PCM Switch / PCM Interface */
580 static const struct snd_kcontrol_new pcm_switch_controls =
581 	SOC_DAPM_SINGLE("Switch", PM860X_ADC_EN_2, 0, 1, 0);
582 
583 /* AUX1 Switch */
584 static const struct snd_kcontrol_new aux1_switch_controls =
585 	SOC_DAPM_SINGLE("Switch", PM860X_ANA_TO_ANA, 4, 1, 0);
586 
587 /* AUX2 Switch */
588 static const struct snd_kcontrol_new aux2_switch_controls =
589 	SOC_DAPM_SINGLE("Switch", PM860X_ANA_TO_ANA, 5, 1, 0);
590 
591 /* Left Ex. PA Switch */
592 static const struct snd_kcontrol_new lepa_switch_controls =
593 	SOC_DAPM_SINGLE("Switch", PM860X_DAC_EN_2, 2, 1, 0);
594 
595 /* Right Ex. PA Switch */
596 static const struct snd_kcontrol_new repa_switch_controls =
597 	SOC_DAPM_SINGLE("Switch", PM860X_DAC_EN_2, 1, 1, 0);
598 
599 /* PCM Mux / Mux7 */
600 static const char *aif1_text[] = {
601 	"PCM L", "PCM R",
602 };
603 
604 static const struct soc_enum aif1_enum =
605 	SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 6, 2, aif1_text);
606 
607 static const struct snd_kcontrol_new aif1_mux =
608 	SOC_DAPM_ENUM("PCM Mux", aif1_enum);
609 
610 /* I2S Mux / Mux9 */
611 static const char *i2s_din_text[] = {
612 	"DIN", "DIN1",
613 };
614 
615 static const struct soc_enum i2s_din_enum =
616 	SOC_ENUM_SINGLE(PM860X_I2S_IFACE_3, 1, 2, i2s_din_text);
617 
618 static const struct snd_kcontrol_new i2s_din_mux =
619 	SOC_DAPM_ENUM("I2S DIN Mux", i2s_din_enum);
620 
621 /* I2S Mic Mux / Mux8 */
622 static const char *i2s_mic_text[] = {
623 	"Ex PA", "ADC",
624 };
625 
626 static const struct soc_enum i2s_mic_enum =
627 	SOC_ENUM_SINGLE(PM860X_I2S_IFACE_3, 4, 2, i2s_mic_text);
628 
629 static const struct snd_kcontrol_new i2s_mic_mux =
630 	SOC_DAPM_ENUM("I2S Mic Mux", i2s_mic_enum);
631 
632 /* ADCL Mux / Mux2 */
633 static const char *adcl_text[] = {
634 	"ADCR", "ADCL",
635 };
636 
637 static const struct soc_enum adcl_enum =
638 	SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 4, 2, adcl_text);
639 
640 static const struct snd_kcontrol_new adcl_mux =
641 	SOC_DAPM_ENUM("ADC Left Mux", adcl_enum);
642 
643 /* ADCR Mux / Mux3 */
644 static const char *adcr_text[] = {
645 	"ADCL", "ADCR",
646 };
647 
648 static const struct soc_enum adcr_enum =
649 	SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 2, 2, adcr_text);
650 
651 static const struct snd_kcontrol_new adcr_mux =
652 	SOC_DAPM_ENUM("ADC Right Mux", adcr_enum);
653 
654 /* ADCR EC Mux / Mux6 */
655 static const char *adcr_ec_text[] = {
656 	"ADCR", "EC",
657 };
658 
659 static const struct soc_enum adcr_ec_enum =
660 	SOC_ENUM_SINGLE(PM860X_ADC_EN_2, 3, 2, adcr_ec_text);
661 
662 static const struct snd_kcontrol_new adcr_ec_mux =
663 	SOC_DAPM_ENUM("ADCR EC Mux", adcr_ec_enum);
664 
665 /* EC Mux / Mux4 */
666 static const char *ec_text[] = {
667 	"Left", "Right", "Left + Right",
668 };
669 
670 static const struct soc_enum ec_enum =
671 	SOC_ENUM_SINGLE(PM860X_EC_PATH, 1, 3, ec_text);
672 
673 static const struct snd_kcontrol_new ec_mux =
674 	SOC_DAPM_ENUM("EC Mux", ec_enum);
675 
676 static const char *dac_text[] = {
677 	"No input", "Right", "Left", "No input",
678 };
679 
680 /* DAC Headset 1 Mux / Mux10 */
681 static const struct soc_enum dac_hs1_enum =
682 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 0, 4, dac_text);
683 
684 static const struct snd_kcontrol_new dac_hs1_mux =
685 	SOC_DAPM_ENUM("DAC HS1 Mux", dac_hs1_enum);
686 
687 /* DAC Headset 2 Mux / Mux11 */
688 static const struct soc_enum dac_hs2_enum =
689 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 2, 4, dac_text);
690 
691 static const struct snd_kcontrol_new dac_hs2_mux =
692 	SOC_DAPM_ENUM("DAC HS2 Mux", dac_hs2_enum);
693 
694 /* DAC Lineout 1 Mux / Mux12 */
695 static const struct soc_enum dac_lo1_enum =
696 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 4, 4, dac_text);
697 
698 static const struct snd_kcontrol_new dac_lo1_mux =
699 	SOC_DAPM_ENUM("DAC LO1 Mux", dac_lo1_enum);
700 
701 /* DAC Lineout 2 Mux / Mux13 */
702 static const struct soc_enum dac_lo2_enum =
703 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 6, 4, dac_text);
704 
705 static const struct snd_kcontrol_new dac_lo2_mux =
706 	SOC_DAPM_ENUM("DAC LO2 Mux", dac_lo2_enum);
707 
708 /* DAC Spearker Earphone Mux / Mux14 */
709 static const struct soc_enum dac_spk_ear_enum =
710 	SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_2, 0, 4, dac_text);
711 
712 static const struct snd_kcontrol_new dac_spk_ear_mux =
713 	SOC_DAPM_ENUM("DAC SP Mux", dac_spk_ear_enum);
714 
715 /* Headset 1 Mux / Mux15 */
716 static const char *in_text[] = {
717 	"Digital", "Analog",
718 };
719 
720 static const struct soc_enum hs1_enum =
721 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 0, 2, in_text);
722 
723 static const struct snd_kcontrol_new hs1_mux =
724 	SOC_DAPM_ENUM("Headset1 Mux", hs1_enum);
725 
726 /* Headset 2 Mux / Mux16 */
727 static const struct soc_enum hs2_enum =
728 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 1, 2, in_text);
729 
730 static const struct snd_kcontrol_new hs2_mux =
731 	SOC_DAPM_ENUM("Headset2 Mux", hs2_enum);
732 
733 /* Lineout 1 Mux / Mux17 */
734 static const struct soc_enum lo1_enum =
735 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 2, 2, in_text);
736 
737 static const struct snd_kcontrol_new lo1_mux =
738 	SOC_DAPM_ENUM("Lineout1 Mux", lo1_enum);
739 
740 /* Lineout 2 Mux / Mux18 */
741 static const struct soc_enum lo2_enum =
742 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 3, 2, in_text);
743 
744 static const struct snd_kcontrol_new lo2_mux =
745 	SOC_DAPM_ENUM("Lineout2 Mux", lo2_enum);
746 
747 /* Speaker Earpiece Demux */
748 static const char *spk_text[] = {
749 	"Earpiece", "Speaker",
750 };
751 
752 static const struct soc_enum spk_enum =
753 	SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 6, 2, spk_text);
754 
755 static const struct snd_kcontrol_new spk_demux =
756 	SOC_DAPM_ENUM("Speaker Earpiece Demux", spk_enum);
757 
758 /* MIC Mux / Mux1 */
759 static const char *mic_text[] = {
760 	"Mic 1", "Mic 2",
761 };
762 
763 static const struct soc_enum mic_enum =
764 	SOC_ENUM_SINGLE(PM860X_ADC_ANA_4, 4, 2, mic_text);
765 
766 static const struct snd_kcontrol_new mic_mux =
767 	SOC_DAPM_ENUM("MIC Mux", mic_enum);
768 
769 static const struct snd_soc_dapm_widget pm860x_dapm_widgets[] = {
770 	SND_SOC_DAPM_AIF_IN("PCM SDI", "PCM Playback", 0,
771 			    PM860X_ADC_EN_2, 0, 0),
772 	SND_SOC_DAPM_AIF_OUT("PCM SDO", "PCM Capture", 0,
773 			     PM860X_PCM_IFACE_3, 1, 1),
774 
775 
776 	SND_SOC_DAPM_AIF_IN("I2S DIN", "I2S Playback", 0,
777 			    SND_SOC_NOPM, 0, 0),
778 	SND_SOC_DAPM_AIF_IN("I2S DIN1", "I2S Playback", 0,
779 			    SND_SOC_NOPM, 0, 0),
780 	SND_SOC_DAPM_AIF_OUT("I2S DOUT", "I2S Capture", 0,
781 			     PM860X_I2S_IFACE_3, 5, 1),
782 	SND_SOC_DAPM_SUPPLY("I2S CLK", PM860X_DAC_EN_2, 0, 0, NULL, 0),
783 	SND_SOC_DAPM_MUX("I2S Mic Mux", SND_SOC_NOPM, 0, 0, &i2s_mic_mux),
784 	SND_SOC_DAPM_MUX("ADC Left Mux", SND_SOC_NOPM, 0, 0, &adcl_mux),
785 	SND_SOC_DAPM_MUX("ADC Right Mux", SND_SOC_NOPM, 0, 0, &adcr_mux),
786 	SND_SOC_DAPM_MUX("EC Mux", SND_SOC_NOPM, 0, 0, &ec_mux),
787 	SND_SOC_DAPM_MUX("ADCR EC Mux", SND_SOC_NOPM, 0, 0, &adcr_ec_mux),
788 	SND_SOC_DAPM_SWITCH("Left EPA", SND_SOC_NOPM, 0, 0,
789 			    &lepa_switch_controls),
790 	SND_SOC_DAPM_SWITCH("Right EPA", SND_SOC_NOPM, 0, 0,
791 			    &repa_switch_controls),
792 
793 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "Left ADC MOD", PM860X_ADC_EN_1,
794 			 0, 1, 1, 0),
795 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "Right ADC MOD", PM860X_ADC_EN_1,
796 			 1, 1, 1, 0),
797 	SND_SOC_DAPM_ADC("Left ADC", NULL, PM860X_ADC_EN_2, 5, 0),
798 	SND_SOC_DAPM_ADC("Right ADC", NULL, PM860X_ADC_EN_2, 4, 0),
799 
800 	SND_SOC_DAPM_SWITCH("AUX1 Switch", SND_SOC_NOPM, 0, 0,
801 			    &aux1_switch_controls),
802 	SND_SOC_DAPM_SWITCH("AUX2 Switch", SND_SOC_NOPM, 0, 0,
803 			    &aux2_switch_controls),
804 
805 	SND_SOC_DAPM_MUX("MIC Mux", SND_SOC_NOPM, 0, 0, &mic_mux),
806 	SND_SOC_DAPM_MICBIAS("Mic1 Bias", PM860X_ADC_ANA_1, 2, 0),
807 	SND_SOC_DAPM_MICBIAS("Mic3 Bias", PM860X_ADC_ANA_1, 7, 0),
808 	SND_SOC_DAPM_PGA("MIC1 Volume", PM860X_ADC_EN_1, 2, 0, NULL, 0),
809 	SND_SOC_DAPM_PGA("MIC3 Volume", PM860X_ADC_EN_1, 3, 0, NULL, 0),
810 	SND_SOC_DAPM_PGA("AUX1 Volume", PM860X_ADC_EN_1, 4, 0, NULL, 0),
811 	SND_SOC_DAPM_PGA("AUX2 Volume", PM860X_ADC_EN_1, 5, 0, NULL, 0),
812 	SND_SOC_DAPM_PGA("Sidetone PGA", PM860X_ADC_EN_2, 1, 0, NULL, 0),
813 	SND_SOC_DAPM_PGA("Lofi PGA", PM860X_ADC_EN_2, 2, 0, NULL, 0),
814 
815 	SND_SOC_DAPM_INPUT("AUX1"),
816 	SND_SOC_DAPM_INPUT("AUX2"),
817 	SND_SOC_DAPM_INPUT("MIC1P"),
818 	SND_SOC_DAPM_INPUT("MIC1N"),
819 	SND_SOC_DAPM_INPUT("MIC2P"),
820 	SND_SOC_DAPM_INPUT("MIC2N"),
821 	SND_SOC_DAPM_INPUT("MIC3P"),
822 	SND_SOC_DAPM_INPUT("MIC3N"),
823 
824 	SND_SOC_DAPM_DAC_E("Left DAC", NULL, SND_SOC_NOPM, 0, 0,
825 			   pm860x_dac_event,
826 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
827 	SND_SOC_DAPM_DAC_E("Right DAC", NULL, SND_SOC_NOPM, 0, 0,
828 			   pm860x_dac_event,
829 			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
830 
831 	SND_SOC_DAPM_MUX("I2S DIN Mux", SND_SOC_NOPM, 0, 0, &i2s_din_mux),
832 	SND_SOC_DAPM_MUX("DAC HS1 Mux", SND_SOC_NOPM, 0, 0, &dac_hs1_mux),
833 	SND_SOC_DAPM_MUX("DAC HS2 Mux", SND_SOC_NOPM, 0, 0, &dac_hs2_mux),
834 	SND_SOC_DAPM_MUX("DAC LO1 Mux", SND_SOC_NOPM, 0, 0, &dac_lo1_mux),
835 	SND_SOC_DAPM_MUX("DAC LO2 Mux", SND_SOC_NOPM, 0, 0, &dac_lo2_mux),
836 	SND_SOC_DAPM_MUX("DAC SP Mux", SND_SOC_NOPM, 0, 0, &dac_spk_ear_mux),
837 	SND_SOC_DAPM_MUX("Headset1 Mux", SND_SOC_NOPM, 0, 0, &hs1_mux),
838 	SND_SOC_DAPM_MUX("Headset2 Mux", SND_SOC_NOPM, 0, 0, &hs2_mux),
839 	SND_SOC_DAPM_MUX("Lineout1 Mux", SND_SOC_NOPM, 0, 0, &lo1_mux),
840 	SND_SOC_DAPM_MUX("Lineout2 Mux", SND_SOC_NOPM, 0, 0, &lo2_mux),
841 	SND_SOC_DAPM_MUX("Speaker Earpiece Demux", SND_SOC_NOPM, 0, 0,
842 			 &spk_demux),
843 
844 
845 	SND_SOC_DAPM_PGA("Headset1 PGA", PM860X_DAC_EN_1, 0, 0, NULL, 0),
846 	SND_SOC_DAPM_PGA("Headset2 PGA", PM860X_DAC_EN_1, 1, 0, NULL, 0),
847 	SND_SOC_DAPM_OUTPUT("HS1"),
848 	SND_SOC_DAPM_OUTPUT("HS2"),
849 	SND_SOC_DAPM_PGA("Lineout1 PGA", PM860X_DAC_EN_1, 2, 0, NULL, 0),
850 	SND_SOC_DAPM_PGA("Lineout2 PGA", PM860X_DAC_EN_1, 3, 0, NULL, 0),
851 	SND_SOC_DAPM_OUTPUT("LINEOUT1"),
852 	SND_SOC_DAPM_OUTPUT("LINEOUT2"),
853 	SND_SOC_DAPM_PGA("Earpiece PGA", PM860X_DAC_EN_1, 4, 0, NULL, 0),
854 	SND_SOC_DAPM_OUTPUT("EARP"),
855 	SND_SOC_DAPM_OUTPUT("EARN"),
856 	SND_SOC_DAPM_PGA("Speaker PGA", PM860X_DAC_EN_1, 5, 0, NULL, 0),
857 	SND_SOC_DAPM_OUTPUT("LSP"),
858 	SND_SOC_DAPM_OUTPUT("LSN"),
859 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "VCODEC", PM860X_AUDIO_SUPPLIES_2,
860 			 0, SUPPLY_MASK, SUPPLY_MASK, 0),
861 
862 	PM860X_DAPM_OUTPUT("RSYNC", pm860x_rsync_event),
863 };
864 
865 static const struct snd_soc_dapm_route pm860x_dapm_routes[] = {
866 	/* supply */
867 	{"Left DAC", NULL, "VCODEC"},
868 	{"Right DAC", NULL, "VCODEC"},
869 	{"Left ADC", NULL, "VCODEC"},
870 	{"Right ADC", NULL, "VCODEC"},
871 	{"Left ADC", NULL, "Left ADC MOD"},
872 	{"Right ADC", NULL, "Right ADC MOD"},
873 
874 	/* I2S Clock */
875 	{"I2S DIN", NULL, "I2S CLK"},
876 	{"I2S DIN1", NULL, "I2S CLK"},
877 	{"I2S DOUT", NULL, "I2S CLK"},
878 
879 	/* PCM/AIF1 Inputs */
880 	{"PCM SDO", NULL, "ADC Left Mux"},
881 	{"PCM SDO", NULL, "ADCR EC Mux"},
882 
883 	/* PCM/AFI2 Outputs */
884 	{"Lofi PGA", NULL, "PCM SDI"},
885 	{"Lofi PGA", NULL, "Sidetone PGA"},
886 	{"Left DAC", NULL, "Lofi PGA"},
887 	{"Right DAC", NULL, "Lofi PGA"},
888 
889 	/* I2S/AIF2 Inputs */
890 	{"MIC Mux", "Mic 1", "MIC1P"},
891 	{"MIC Mux", "Mic 1", "MIC1N"},
892 	{"MIC Mux", "Mic 2", "MIC2P"},
893 	{"MIC Mux", "Mic 2", "MIC2N"},
894 	{"MIC1 Volume", NULL, "MIC Mux"},
895 	{"MIC3 Volume", NULL, "MIC3P"},
896 	{"MIC3 Volume", NULL, "MIC3N"},
897 	{"Left ADC", NULL, "MIC1 Volume"},
898 	{"Right ADC", NULL, "MIC3 Volume"},
899 	{"ADC Left Mux", "ADCR", "Right ADC"},
900 	{"ADC Left Mux", "ADCL", "Left ADC"},
901 	{"ADC Right Mux", "ADCL", "Left ADC"},
902 	{"ADC Right Mux", "ADCR", "Right ADC"},
903 	{"Left EPA", "Switch", "Left DAC"},
904 	{"Right EPA", "Switch", "Right DAC"},
905 	{"EC Mux", "Left", "Left DAC"},
906 	{"EC Mux", "Right", "Right DAC"},
907 	{"EC Mux", "Left + Right", "Left DAC"},
908 	{"EC Mux", "Left + Right", "Right DAC"},
909 	{"ADCR EC Mux", "ADCR", "ADC Right Mux"},
910 	{"ADCR EC Mux", "EC", "EC Mux"},
911 	{"I2S Mic Mux", "Ex PA", "Left EPA"},
912 	{"I2S Mic Mux", "Ex PA", "Right EPA"},
913 	{"I2S Mic Mux", "ADC", "ADC Left Mux"},
914 	{"I2S Mic Mux", "ADC", "ADCR EC Mux"},
915 	{"I2S DOUT", NULL, "I2S Mic Mux"},
916 
917 	/* I2S/AIF2 Outputs */
918 	{"I2S DIN Mux", "DIN", "I2S DIN"},
919 	{"I2S DIN Mux", "DIN1", "I2S DIN1"},
920 	{"Left DAC", NULL, "I2S DIN Mux"},
921 	{"Right DAC", NULL, "I2S DIN Mux"},
922 	{"DAC HS1 Mux", "Left", "Left DAC"},
923 	{"DAC HS1 Mux", "Right", "Right DAC"},
924 	{"DAC HS2 Mux", "Left", "Left DAC"},
925 	{"DAC HS2 Mux", "Right", "Right DAC"},
926 	{"DAC LO1 Mux", "Left", "Left DAC"},
927 	{"DAC LO1 Mux", "Right", "Right DAC"},
928 	{"DAC LO2 Mux", "Left", "Left DAC"},
929 	{"DAC LO2 Mux", "Right", "Right DAC"},
930 	{"Headset1 Mux", "Digital", "DAC HS1 Mux"},
931 	{"Headset2 Mux", "Digital", "DAC HS2 Mux"},
932 	{"Lineout1 Mux", "Digital", "DAC LO1 Mux"},
933 	{"Lineout2 Mux", "Digital", "DAC LO2 Mux"},
934 	{"Headset1 PGA", NULL, "Headset1 Mux"},
935 	{"Headset2 PGA", NULL, "Headset2 Mux"},
936 	{"Lineout1 PGA", NULL, "Lineout1 Mux"},
937 	{"Lineout2 PGA", NULL, "Lineout2 Mux"},
938 	{"DAC SP Mux", "Left", "Left DAC"},
939 	{"DAC SP Mux", "Right", "Right DAC"},
940 	{"Speaker Earpiece Demux", "Speaker", "DAC SP Mux"},
941 	{"Speaker PGA", NULL, "Speaker Earpiece Demux"},
942 	{"Earpiece PGA", NULL, "Speaker Earpiece Demux"},
943 
944 	{"RSYNC", NULL, "Headset1 PGA"},
945 	{"RSYNC", NULL, "Headset2 PGA"},
946 	{"RSYNC", NULL, "Lineout1 PGA"},
947 	{"RSYNC", NULL, "Lineout2 PGA"},
948 	{"RSYNC", NULL, "Speaker PGA"},
949 	{"RSYNC", NULL, "Speaker PGA"},
950 	{"RSYNC", NULL, "Earpiece PGA"},
951 	{"RSYNC", NULL, "Earpiece PGA"},
952 
953 	{"HS1", NULL, "RSYNC"},
954 	{"HS2", NULL, "RSYNC"},
955 	{"LINEOUT1", NULL, "RSYNC"},
956 	{"LINEOUT2", NULL, "RSYNC"},
957 	{"LSP", NULL, "RSYNC"},
958 	{"LSN", NULL, "RSYNC"},
959 	{"EARP", NULL, "RSYNC"},
960 	{"EARN", NULL, "RSYNC"},
961 };
962 
963 /*
964  * Use MUTE_LEFT & MUTE_RIGHT to implement digital mute.
965  * These bits can also be used to mute.
966  */
967 static int pm860x_digital_mute(struct snd_soc_dai *codec_dai, int mute)
968 {
969 	struct snd_soc_codec *codec = codec_dai->codec;
970 	int data = 0, mask = MUTE_LEFT | MUTE_RIGHT;
971 
972 	if (mute)
973 		data = mask;
974 	snd_soc_update_bits(codec, PM860X_DAC_OFFSET, mask, data);
975 	snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
976 			    RSYNC_CHANGE, RSYNC_CHANGE);
977 	return 0;
978 }
979 
980 static int pm860x_pcm_hw_params(struct snd_pcm_substream *substream,
981 				struct snd_pcm_hw_params *params,
982 				struct snd_soc_dai *dai)
983 {
984 	struct snd_soc_codec *codec = dai->codec;
985 	unsigned char inf = 0, mask = 0;
986 
987 	/* bit size */
988 	switch (params_format(params)) {
989 	case SNDRV_PCM_FORMAT_S16_LE:
990 		inf &= ~PCM_INF2_18WL;
991 		break;
992 	case SNDRV_PCM_FORMAT_S18_3LE:
993 		inf |= PCM_INF2_18WL;
994 		break;
995 	default:
996 		return -EINVAL;
997 	}
998 	mask |= PCM_INF2_18WL;
999 	snd_soc_update_bits(codec, PM860X_PCM_IFACE_2, mask, inf);
1000 
1001 	/* sample rate */
1002 	switch (params_rate(params)) {
1003 	case 8000:
1004 		inf = 0;
1005 		break;
1006 	case 16000:
1007 		inf = 3;
1008 		break;
1009 	case 32000:
1010 		inf = 6;
1011 		break;
1012 	case 48000:
1013 		inf = 8;
1014 		break;
1015 	default:
1016 		return -EINVAL;
1017 	}
1018 	snd_soc_update_bits(codec, PM860X_PCM_RATE, 0x0f, inf);
1019 
1020 	return 0;
1021 }
1022 
1023 static int pm860x_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
1024 				  unsigned int fmt)
1025 {
1026 	struct snd_soc_codec *codec = codec_dai->codec;
1027 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1028 	unsigned char inf = 0, mask = 0;
1029 	int ret = -EINVAL;
1030 
1031 	mask |= PCM_INF2_BCLK | PCM_INF2_FS | PCM_INF2_MASTER;
1032 
1033 	/* set master/slave audio interface */
1034 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1035 	case SND_SOC_DAIFMT_CBM_CFM:
1036 	case SND_SOC_DAIFMT_CBM_CFS:
1037 		if (pm860x->dir == PM860X_CLK_DIR_OUT) {
1038 			inf |= PCM_INF2_MASTER;
1039 			ret = 0;
1040 		}
1041 		break;
1042 	case SND_SOC_DAIFMT_CBS_CFS:
1043 		if (pm860x->dir == PM860X_CLK_DIR_IN) {
1044 			inf &= ~PCM_INF2_MASTER;
1045 			ret = 0;
1046 		}
1047 		break;
1048 	}
1049 
1050 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1051 	case SND_SOC_DAIFMT_I2S:
1052 		inf |= PCM_EXACT_I2S;
1053 		ret = 0;
1054 		break;
1055 	}
1056 	mask |= PCM_MODE_MASK;
1057 	if (ret)
1058 		return ret;
1059 	snd_soc_update_bits(codec, PM860X_PCM_IFACE_2, mask, inf);
1060 	return 0;
1061 }
1062 
1063 static int pm860x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1064 				 int clk_id, unsigned int freq, int dir)
1065 {
1066 	struct snd_soc_codec *codec = codec_dai->codec;
1067 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1068 
1069 	if (dir == PM860X_CLK_DIR_OUT)
1070 		pm860x->dir = PM860X_CLK_DIR_OUT;
1071 	else {
1072 		pm860x->dir = PM860X_CLK_DIR_IN;
1073 		return -EINVAL;
1074 	}
1075 
1076 	return 0;
1077 }
1078 
1079 static int pm860x_i2s_hw_params(struct snd_pcm_substream *substream,
1080 				struct snd_pcm_hw_params *params,
1081 				struct snd_soc_dai *dai)
1082 {
1083 	struct snd_soc_codec *codec = dai->codec;
1084 	unsigned char inf;
1085 
1086 	/* bit size */
1087 	switch (params_format(params)) {
1088 	case SNDRV_PCM_FORMAT_S16_LE:
1089 		inf = 0;
1090 		break;
1091 	case SNDRV_PCM_FORMAT_S18_3LE:
1092 		inf = PCM_INF2_18WL;
1093 		break;
1094 	default:
1095 		return -EINVAL;
1096 	}
1097 	snd_soc_update_bits(codec, PM860X_I2S_IFACE_2, PCM_INF2_18WL, inf);
1098 
1099 	/* sample rate */
1100 	switch (params_rate(params)) {
1101 	case 8000:
1102 		inf = 0;
1103 		break;
1104 	case 11025:
1105 		inf = 1;
1106 		break;
1107 	case 16000:
1108 		inf = 3;
1109 		break;
1110 	case 22050:
1111 		inf = 4;
1112 		break;
1113 	case 32000:
1114 		inf = 6;
1115 		break;
1116 	case 44100:
1117 		inf = 7;
1118 		break;
1119 	case 48000:
1120 		inf = 8;
1121 		break;
1122 	default:
1123 		return -EINVAL;
1124 	}
1125 	snd_soc_update_bits(codec, PM860X_I2S_IFACE_4, 0xf, inf);
1126 
1127 	return 0;
1128 }
1129 
1130 static int pm860x_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
1131 				  unsigned int fmt)
1132 {
1133 	struct snd_soc_codec *codec = codec_dai->codec;
1134 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1135 	unsigned char inf = 0, mask = 0;
1136 
1137 	mask |= PCM_INF2_BCLK | PCM_INF2_FS | PCM_INF2_MASTER;
1138 
1139 	/* set master/slave audio interface */
1140 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1141 	case SND_SOC_DAIFMT_CBM_CFM:
1142 		if (pm860x->dir == PM860X_CLK_DIR_OUT)
1143 			inf |= PCM_INF2_MASTER;
1144 		else
1145 			return -EINVAL;
1146 		break;
1147 	case SND_SOC_DAIFMT_CBS_CFS:
1148 		if (pm860x->dir == PM860X_CLK_DIR_IN)
1149 			inf &= ~PCM_INF2_MASTER;
1150 		else
1151 			return -EINVAL;
1152 		break;
1153 	default:
1154 		return -EINVAL;
1155 	}
1156 
1157 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1158 	case SND_SOC_DAIFMT_I2S:
1159 		inf |= PCM_EXACT_I2S;
1160 		break;
1161 	default:
1162 		return -EINVAL;
1163 	}
1164 	mask |= PCM_MODE_MASK;
1165 	snd_soc_update_bits(codec, PM860X_I2S_IFACE_2, mask, inf);
1166 	return 0;
1167 }
1168 
1169 static int pm860x_set_bias_level(struct snd_soc_codec *codec,
1170 				 enum snd_soc_bias_level level)
1171 {
1172 	int data;
1173 
1174 	switch (level) {
1175 	case SND_SOC_BIAS_ON:
1176 		break;
1177 
1178 	case SND_SOC_BIAS_PREPARE:
1179 		break;
1180 
1181 	case SND_SOC_BIAS_STANDBY:
1182 		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
1183 			/* Enable Audio PLL & Audio section */
1184 			data = AUDIO_PLL | AUDIO_SECTION_ON;
1185 			pm860x_reg_write(codec->control_data, REG_MISC2, data);
1186 			udelay(300);
1187 			data = AUDIO_PLL | AUDIO_SECTION_RESET
1188 				| AUDIO_SECTION_ON;
1189 			pm860x_reg_write(codec->control_data, REG_MISC2, data);
1190 		}
1191 		break;
1192 
1193 	case SND_SOC_BIAS_OFF:
1194 		data = AUDIO_PLL | AUDIO_SECTION_RESET | AUDIO_SECTION_ON;
1195 		pm860x_set_bits(codec->control_data, REG_MISC2, data, 0);
1196 		break;
1197 	}
1198 	codec->dapm.bias_level = level;
1199 	return 0;
1200 }
1201 
1202 static const struct snd_soc_dai_ops pm860x_pcm_dai_ops = {
1203 	.digital_mute	= pm860x_digital_mute,
1204 	.hw_params	= pm860x_pcm_hw_params,
1205 	.set_fmt	= pm860x_pcm_set_dai_fmt,
1206 	.set_sysclk	= pm860x_set_dai_sysclk,
1207 };
1208 
1209 static const struct snd_soc_dai_ops pm860x_i2s_dai_ops = {
1210 	.digital_mute	= pm860x_digital_mute,
1211 	.hw_params	= pm860x_i2s_hw_params,
1212 	.set_fmt	= pm860x_i2s_set_dai_fmt,
1213 	.set_sysclk	= pm860x_set_dai_sysclk,
1214 };
1215 
1216 #define PM860X_RATES	(SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |	\
1217 			 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000)
1218 
1219 static struct snd_soc_dai_driver pm860x_dai[] = {
1220 	{
1221 		/* DAI PCM */
1222 		.name	= "88pm860x-pcm",
1223 		.id	= 1,
1224 		.playback = {
1225 			.stream_name	= "PCM Playback",
1226 			.channels_min	= 2,
1227 			.channels_max	= 2,
1228 			.rates		= PM860X_RATES,
1229 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1230 					  SNDRV_PCM_FORMAT_S18_3LE,
1231 		},
1232 		.capture = {
1233 			.stream_name	= "PCM Capture",
1234 			.channels_min	= 2,
1235 			.channels_max	= 2,
1236 			.rates		= PM860X_RATES,
1237 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1238 					  SNDRV_PCM_FORMAT_S18_3LE,
1239 		},
1240 		.ops	= &pm860x_pcm_dai_ops,
1241 	}, {
1242 		/* DAI I2S */
1243 		.name	= "88pm860x-i2s",
1244 		.id	= 2,
1245 		.playback = {
1246 			.stream_name	= "I2S Playback",
1247 			.channels_min	= 2,
1248 			.channels_max	= 2,
1249 			.rates		= SNDRV_PCM_RATE_8000_48000,
1250 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1251 					  SNDRV_PCM_FORMAT_S18_3LE,
1252 		},
1253 		.capture = {
1254 			.stream_name	= "I2S Capture",
1255 			.channels_min	= 2,
1256 			.channels_max	= 2,
1257 			.rates		= SNDRV_PCM_RATE_8000_48000,
1258 			.formats	= SNDRV_PCM_FORMAT_S16_LE | \
1259 					  SNDRV_PCM_FORMAT_S18_3LE,
1260 		},
1261 		.ops	= &pm860x_i2s_dai_ops,
1262 	},
1263 };
1264 
1265 static irqreturn_t pm860x_codec_handler(int irq, void *data)
1266 {
1267 	struct pm860x_priv *pm860x = data;
1268 	int status, shrt, report = 0, mic_report = 0;
1269 	int mask;
1270 
1271 	status = pm860x_reg_read(pm860x->i2c, REG_STATUS_1);
1272 	shrt = pm860x_reg_read(pm860x->i2c, REG_SHORTS);
1273 	mask = pm860x->det.hs_shrt | pm860x->det.hook_det | pm860x->det.lo_shrt
1274 		| pm860x->det.hp_det;
1275 
1276 #ifndef CONFIG_SND_SOC_88PM860X_MODULE
1277 	if (status & (HEADSET_STATUS | MIC_STATUS | SHORT_HS1 | SHORT_HS2 |
1278 		      SHORT_LO1 | SHORT_LO2))
1279 		trace_snd_soc_jack_irq(dev_name(pm860x->codec->dev));
1280 #endif
1281 
1282 	if ((pm860x->det.hp_det & SND_JACK_HEADPHONE)
1283 		&& (status & HEADSET_STATUS))
1284 		report |= SND_JACK_HEADPHONE;
1285 
1286 	if ((pm860x->det.mic_det & SND_JACK_MICROPHONE)
1287 		&& (status & MIC_STATUS))
1288 		mic_report |= SND_JACK_MICROPHONE;
1289 
1290 	if (pm860x->det.hs_shrt && (shrt & (SHORT_HS1 | SHORT_HS2)))
1291 		report |= pm860x->det.hs_shrt;
1292 
1293 	if (pm860x->det.hook_det && (status & HOOK_STATUS))
1294 		report |= pm860x->det.hook_det;
1295 
1296 	if (pm860x->det.lo_shrt && (shrt & (SHORT_LO1 | SHORT_LO2)))
1297 		report |= pm860x->det.lo_shrt;
1298 
1299 	if (report)
1300 		snd_soc_jack_report(pm860x->det.hp_jack, report, mask);
1301 	if (mic_report)
1302 		snd_soc_jack_report(pm860x->det.mic_jack, SND_JACK_MICROPHONE,
1303 				    SND_JACK_MICROPHONE);
1304 
1305 	dev_dbg(pm860x->codec->dev, "headphone report:0x%x, mask:%x\n",
1306 		report, mask);
1307 	dev_dbg(pm860x->codec->dev, "microphone report:0x%x\n", mic_report);
1308 	return IRQ_HANDLED;
1309 }
1310 
1311 int pm860x_hs_jack_detect(struct snd_soc_codec *codec,
1312 			  struct snd_soc_jack *jack,
1313 			  int det, int hook, int hs_shrt, int lo_shrt)
1314 {
1315 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1316 	int data;
1317 
1318 	pm860x->det.hp_jack = jack;
1319 	pm860x->det.hp_det = det;
1320 	pm860x->det.hook_det = hook;
1321 	pm860x->det.hs_shrt = hs_shrt;
1322 	pm860x->det.lo_shrt = lo_shrt;
1323 
1324 	if (det & SND_JACK_HEADPHONE)
1325 		pm860x_set_bits(codec->control_data, REG_HS_DET,
1326 				EN_HS_DET, EN_HS_DET);
1327 	/* headset short detect */
1328 	if (hs_shrt) {
1329 		data = CLR_SHORT_HS2 | CLR_SHORT_HS1;
1330 		pm860x_set_bits(codec->control_data, REG_SHORTS, data, data);
1331 	}
1332 	/* Lineout short detect */
1333 	if (lo_shrt) {
1334 		data = CLR_SHORT_LO2 | CLR_SHORT_LO1;
1335 		pm860x_set_bits(codec->control_data, REG_SHORTS, data, data);
1336 	}
1337 
1338 	/* sync status */
1339 	pm860x_codec_handler(0, pm860x);
1340 	return 0;
1341 }
1342 EXPORT_SYMBOL_GPL(pm860x_hs_jack_detect);
1343 
1344 int pm860x_mic_jack_detect(struct snd_soc_codec *codec,
1345 			   struct snd_soc_jack *jack, int det)
1346 {
1347 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1348 
1349 	pm860x->det.mic_jack = jack;
1350 	pm860x->det.mic_det = det;
1351 
1352 	if (det & SND_JACK_MICROPHONE)
1353 		pm860x_set_bits(codec->control_data, REG_MIC_DET,
1354 				MICDET_MASK, MICDET_MASK);
1355 
1356 	/* sync status */
1357 	pm860x_codec_handler(0, pm860x);
1358 	return 0;
1359 }
1360 EXPORT_SYMBOL_GPL(pm860x_mic_jack_detect);
1361 
1362 static int pm860x_probe(struct snd_soc_codec *codec)
1363 {
1364 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1365 	int i, ret;
1366 
1367 	pm860x->codec = codec;
1368 
1369 	codec->control_data = pm860x->i2c;
1370 
1371 	for (i = 0; i < 4; i++) {
1372 		ret = request_threaded_irq(pm860x->irq[i], NULL,
1373 					   pm860x_codec_handler, IRQF_ONESHOT,
1374 					   pm860x->name[i], pm860x);
1375 		if (ret < 0) {
1376 			dev_err(codec->dev, "Failed to request IRQ!\n");
1377 			goto out;
1378 		}
1379 	}
1380 
1381 	pm860x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1382 
1383 	ret = pm860x_bulk_read(codec->control_data, REG_CACHE_BASE,
1384 			       REG_CACHE_SIZE, codec->reg_cache);
1385 	if (ret < 0) {
1386 		dev_err(codec->dev, "Failed to fill register cache: %d\n",
1387 			ret);
1388 		goto out;
1389 	}
1390 
1391 	return 0;
1392 
1393 out:
1394 	while (--i >= 0)
1395 		free_irq(pm860x->irq[i], pm860x);
1396 	return ret;
1397 }
1398 
1399 static int pm860x_remove(struct snd_soc_codec *codec)
1400 {
1401 	struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
1402 	int i;
1403 
1404 	for (i = 3; i >= 0; i--)
1405 		free_irq(pm860x->irq[i], pm860x);
1406 	pm860x_set_bias_level(codec, SND_SOC_BIAS_OFF);
1407 	return 0;
1408 }
1409 
1410 static struct snd_soc_codec_driver soc_codec_dev_pm860x = {
1411 	.probe		= pm860x_probe,
1412 	.remove		= pm860x_remove,
1413 	.read		= pm860x_read_reg_cache,
1414 	.write		= pm860x_write_reg_cache,
1415 	.reg_cache_size	= REG_CACHE_SIZE,
1416 	.reg_word_size	= sizeof(u8),
1417 	.set_bias_level	= pm860x_set_bias_level,
1418 
1419 	.controls = pm860x_snd_controls,
1420 	.num_controls = ARRAY_SIZE(pm860x_snd_controls),
1421 	.dapm_widgets = pm860x_dapm_widgets,
1422 	.num_dapm_widgets = ARRAY_SIZE(pm860x_dapm_widgets),
1423 	.dapm_routes = pm860x_dapm_routes,
1424 	.num_dapm_routes = ARRAY_SIZE(pm860x_dapm_routes),
1425 };
1426 
1427 static int pm860x_codec_probe(struct platform_device *pdev)
1428 {
1429 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
1430 	struct pm860x_priv *pm860x;
1431 	struct resource *res;
1432 	int i, ret;
1433 
1434 	pm860x = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_priv),
1435 			      GFP_KERNEL);
1436 	if (pm860x == NULL)
1437 		return -ENOMEM;
1438 
1439 	pm860x->chip = chip;
1440 	pm860x->i2c = (chip->id == CHIP_PM8607) ? chip->client
1441 			: chip->companion;
1442 	platform_set_drvdata(pdev, pm860x);
1443 
1444 	for (i = 0; i < 4; i++) {
1445 		res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1446 		if (!res) {
1447 			dev_err(&pdev->dev, "Failed to get IRQ resources\n");
1448 			return -EINVAL;
1449 		}
1450 		pm860x->irq[i] = res->start + chip->irq_base;
1451 		strncpy(pm860x->name[i], res->name, MAX_NAME_LEN);
1452 	}
1453 
1454 	ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pm860x,
1455 				     pm860x_dai, ARRAY_SIZE(pm860x_dai));
1456 	if (ret) {
1457 		dev_err(&pdev->dev, "Failed to register codec\n");
1458 		return -EINVAL;
1459 	}
1460 	return ret;
1461 }
1462 
1463 static int pm860x_codec_remove(struct platform_device *pdev)
1464 {
1465 	snd_soc_unregister_codec(&pdev->dev);
1466 	return 0;
1467 }
1468 
1469 static struct platform_driver pm860x_codec_driver = {
1470 	.driver	= {
1471 		.name	= "88pm860x-codec",
1472 		.owner	= THIS_MODULE,
1473 	},
1474 	.probe	= pm860x_codec_probe,
1475 	.remove	= pm860x_codec_remove,
1476 };
1477 
1478 module_platform_driver(pm860x_codec_driver);
1479 
1480 MODULE_DESCRIPTION("ASoC 88PM860x driver");
1481 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1482 MODULE_LICENSE("GPL");
1483 MODULE_ALIAS("platform:88pm860x-codec");
1484 
1485