~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/codecs/88pm860x-codec.c

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp