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