blob: 54f527c41430fce1b0d0b0638803e0fe1ae93daa [file] [log] [blame]
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/firmware.h>
15#include <linux/slab.h>
16#include <linux/platform_device.h>
17#include <linux/device.h>
18#include <linux/printk.h>
19#include <linux/ratelimit.h>
20#include <linux/debugfs.h>
21#include <linux/mfd/wcd9xxx/core.h>
22#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
23#include <linux/mfd/wcd9xxx/wcd9304_registers.h>
24#include <linux/mfd/wcd9xxx/pdata.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/jack.h>
28#include <sound/soc.h>
29#include <sound/soc-dapm.h>
30#include <sound/tlv.h>
31#include <linux/bitops.h>
32#include <linux/delay.h>
Asish Bhattacharya1d069532012-03-07 13:25:24 -080033#include <linux/pm_runtime.h>
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -070034#include <linux/kernel.h>
35#include <linux/gpio.h>
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053036#include "wcd9304.h"
37
38#define WCD9304_RATES (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|\
39 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_48000)
40
41#define NUM_DECIMATORS 4
42#define NUM_INTERPOLATORS 3
43#define BITS_PER_REG 8
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -080044#define SITAR_CFILT_FAST_MODE 0x00
45#define SITAR_CFILT_SLOW_MODE 0x40
46#define MBHC_FW_READ_ATTEMPTS 15
47#define MBHC_FW_READ_TIMEOUT 2000000
48
49#define SITAR_JACK_MASK (SND_JACK_HEADSET | SND_JACK_OC_HPHL | SND_JACK_OC_HPHR)
50
51#define SITAR_I2S_MASTER_MODE_MASK 0x08
52
53#define SITAR_OCP_ATTEMPT 1
54
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053055#define AIF1_PB 1
56#define AIF1_CAP 2
57#define NUM_CODEC_DAIS 2
58
59struct sitar_codec_dai_data {
60 u32 rate;
61 u32 *ch_num;
62 u32 ch_act;
63 u32 ch_tot;
64};
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053065
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -080066#define SITAR_MCLK_RATE_12288KHZ 12288000
67#define SITAR_MCLK_RATE_9600KHZ 9600000
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053068
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -080069#define SITAR_FAKE_INS_THRESHOLD_MS 2500
70#define SITAR_FAKE_REMOVAL_MIN_PERIOD_MS 50
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -070071#define SITAR_MBHC_BUTTON_MIN 0x8000
72#define SITAR_GPIO_IRQ_DEBOUNCE_TIME_US 5000
73
74#define SITAR_ACQUIRE_LOCK(x) do { mutex_lock(&x); } while (0)
75#define SITAR_RELEASE_LOCK(x) do { mutex_unlock(&x); } while (0)
76
77#define MBHC_NUM_DCE_PLUG_DETECT 3
78#define SITAR_MBHC_FAKE_INSERT_LOW 10
79#define SITAR_MBHC_FAKE_INSERT_HIGH 80
80#define SITAR_MBHC_FAKE_INSERT_VOLT_DELTA_MV 500
81#define SITAR_HS_DETECT_PLUG_TIME_MS (5 * 1000)
82#define SITAR_HS_DETECT_PLUG_INERVAL_MS 100
83#define NUM_ATTEMPTS_TO_REPORT 5
84#define SITAR_MBHC_STATUS_REL_DETECTION 0x0C
85#define SITAR_MBHC_GPIO_REL_DEBOUNCE_TIME_MS 200
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053086
87static const DECLARE_TLV_DB_SCALE(digital_gain, 0, 1, 0);
88static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
89static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
90static struct snd_soc_dai_driver sitar_dai[];
91static int sitar_codec_enable_slimtx(struct snd_soc_dapm_widget *w,
92 struct snd_kcontrol *kcontrol, int event);
93static int sitar_codec_enable_slimrx(struct snd_soc_dapm_widget *w,
94 struct snd_kcontrol *kcontrol, int event);
95
96enum sitar_bandgap_type {
97 SITAR_BANDGAP_OFF = 0,
98 SITAR_BANDGAP_AUDIO_MODE,
99 SITAR_BANDGAP_MBHC_MODE,
100};
101
102struct mbhc_micbias_regs {
103 u16 cfilt_val;
104 u16 cfilt_ctl;
105 u16 mbhc_reg;
106 u16 int_rbias;
107 u16 ctl_reg;
108 u8 cfilt_sel;
109};
110
111/* Codec supports 2 IIR filters */
112enum {
113 IIR1 = 0,
114 IIR2,
115 IIR_MAX,
116};
117/* Codec supports 5 bands */
118enum {
119 BAND1 = 0,
120 BAND2,
121 BAND3,
122 BAND4,
123 BAND5,
124 BAND_MAX,
125};
126
127/* Flags to track of PA and DAC state.
128 * PA and DAC should be tracked separately as AUXPGA loopback requires
129 * only PA to be turned on without DAC being on. */
130enum sitar_priv_ack_flags {
131 SITAR_HPHL_PA_OFF_ACK = 0,
132 SITAR_HPHR_PA_OFF_ACK,
133 SITAR_HPHL_DAC_OFF_ACK,
134 SITAR_HPHR_DAC_OFF_ACK
135};
136
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800137/* Data used by MBHC */
138struct mbhc_internal_cal_data {
139 u16 dce_z;
140 u16 dce_mb;
141 u16 sta_z;
142 u16 sta_mb;
143 u32 t_sta_dce;
144 u32 t_dce;
145 u32 t_sta;
146 u32 micb_mv;
147 u16 v_ins_hu;
148 u16 v_ins_h;
149 u16 v_b1_hu;
150 u16 v_b1_h;
151 u16 v_b1_huc;
152 u16 v_brh;
153 u16 v_brl;
154 u16 v_no_mic;
155 u8 npoll;
156 u8 nbounce_wait;
157};
158
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -0700159enum sitar_mbhc_plug_type {
160 PLUG_TYPE_INVALID = -1,
161 PLUG_TYPE_NONE,
162 PLUG_TYPE_HEADSET,
163 PLUG_TYPE_HEADPHONE,
164 PLUG_TYPE_HIGH_HPH,
165};
166
167enum sitar_mbhc_state {
168 MBHC_STATE_NONE = -1,
169 MBHC_STATE_POTENTIAL,
170 MBHC_STATE_POTENTIAL_RECOVERY,
171 MBHC_STATE_RELEASE,
172};
173
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530174struct sitar_priv {
175 struct snd_soc_codec *codec;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800176 u32 mclk_freq;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530177 u32 adc_count;
178 u32 cfilt1_cnt;
179 u32 cfilt2_cnt;
180 u32 cfilt3_cnt;
181 u32 rx_bias_count;
182 enum sitar_bandgap_type bandgap_type;
183 bool mclk_enabled;
184 bool clock_active;
185 bool config_mode_active;
186 bool mbhc_polling_active;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800187 unsigned long mbhc_fake_ins_start;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530188 int buttons_pressed;
189
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800190 enum sitar_micbias_num micbias;
191 /* void* calibration contains:
192 * struct sitar_mbhc_general_cfg generic;
193 * struct sitar_mbhc_plug_detect_cfg plug_det;
194 * struct sitar_mbhc_plug_type_cfg plug_type;
195 * struct sitar_mbhc_btn_detect_cfg btn_det;
196 * struct sitar_mbhc_imped_detect_cfg imped_det;
197 * Note: various size depends on btn_det->num_btn
198 */
199 void *calibration;
200 struct mbhc_internal_cal_data mbhc_data;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530201
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530202 struct wcd9xxx_pdata *pdata;
203 u32 anc_slot;
204
205 bool no_mic_headset_override;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530206
207 struct mbhc_micbias_regs mbhc_bias_regs;
208 u8 cfilt_k_value;
209 bool mbhc_micbias_switched;
210
211 /* track PA/DAC state */
212 unsigned long hph_pa_dac_state;
213
214 /*track sitar interface type*/
215 u8 intf_type;
216
217 u32 hph_status; /* track headhpone status */
218 /* define separate work for left and right headphone OCP to avoid
219 * additional checking on which OCP event to report so no locking
220 * to ensure synchronization is required
221 */
222 struct work_struct hphlocp_work; /* reporting left hph ocp off */
223 struct work_struct hphrocp_work; /* reporting right hph ocp off */
224
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530225 u8 hphlocp_cnt; /* headphone left ocp retry */
226 u8 hphrocp_cnt; /* headphone right ocp retry */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800227
228 /* Callback function to enable MCLK */
229 int (*mclk_cb) (struct snd_soc_codec*, int);
230
231 /* Work to perform MBHC Firmware Read */
232 struct delayed_work mbhc_firmware_dwork;
233 const struct firmware *mbhc_fw;
234
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530235 /* num of slim ports required */
236 struct sitar_codec_dai_data dai[NUM_CODEC_DAIS];
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -0700237
238 /* Currently, only used for mbhc purpose, to protect
239 * concurrent execution of mbhc threaded irq handlers and
240 * kill race between DAPM and MBHC.But can serve as a
241 * general lock to protect codec resource
242 */
243 struct mutex codec_resource_lock;
244
245 struct sitar_mbhc_config mbhc_cfg;
246 bool in_gpio_handler;
247 u8 current_plug;
248 bool lpi_enabled;
249 enum sitar_mbhc_state mbhc_state;
250 struct work_struct hs_correct_plug_work;
251 bool hs_detect_work_stop;
252 struct delayed_work mbhc_btn_dwork;
253 unsigned long mbhc_last_resume; /* in jiffies */
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530254};
255
256#ifdef CONFIG_DEBUG_FS
257struct sitar_priv *debug_sitar_priv;
258#endif
259
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700260static int sitar_get_anc_slot(struct snd_kcontrol *kcontrol,
261 struct snd_ctl_elem_value *ucontrol)
262{
263 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
264 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
265 ucontrol->value.integer.value[0] = sitar->anc_slot;
266 return 0;
267}
268
269static int sitar_put_anc_slot(struct snd_kcontrol *kcontrol,
270 struct snd_ctl_elem_value *ucontrol)
271{
272 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
273 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
274 sitar->anc_slot = ucontrol->value.integer.value[0];
275 return 0;
276}
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530277
278static int sitar_pa_gain_get(struct snd_kcontrol *kcontrol,
279 struct snd_ctl_elem_value *ucontrol)
280{
281 u8 ear_pa_gain;
282 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
283
284 ear_pa_gain = snd_soc_read(codec, SITAR_A_RX_EAR_GAIN);
285
286 ear_pa_gain = ear_pa_gain >> 5;
287
288 if (ear_pa_gain == 0x00) {
289 ucontrol->value.integer.value[0] = 0;
290 } else if (ear_pa_gain == 0x04) {
291 ucontrol->value.integer.value[0] = 1;
292 } else {
293 pr_err("%s: ERROR: Unsupported Ear Gain = 0x%x\n",
294 __func__, ear_pa_gain);
295 return -EINVAL;
296 }
297
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530298 pr_debug("%s: ear_pa_gain = 0x%x\n", __func__, ear_pa_gain);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530299
300 return 0;
301}
302
303static int sitar_pa_gain_put(struct snd_kcontrol *kcontrol,
304 struct snd_ctl_elem_value *ucontrol)
305{
306 u8 ear_pa_gain;
307 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
308
309 pr_debug("%s: ucontrol->value.integer.value[0] = %ld\n", __func__,
310 ucontrol->value.integer.value[0]);
311
312 switch (ucontrol->value.integer.value[0]) {
313 case 0:
314 ear_pa_gain = 0x00;
315 break;
316 case 1:
317 ear_pa_gain = 0x80;
318 break;
319 default:
320 return -EINVAL;
321 }
322
323 snd_soc_write(codec, SITAR_A_RX_EAR_GAIN, ear_pa_gain);
324 return 0;
325}
326
327static int sitar_get_iir_enable_audio_mixer(
328 struct snd_kcontrol *kcontrol,
329 struct snd_ctl_elem_value *ucontrol)
330{
331 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
332 int iir_idx = ((struct soc_multi_mixer_control *)
333 kcontrol->private_value)->reg;
334 int band_idx = ((struct soc_multi_mixer_control *)
335 kcontrol->private_value)->shift;
336
337 ucontrol->value.integer.value[0] =
338 snd_soc_read(codec, (SITAR_A_CDC_IIR1_CTL + 16 * iir_idx)) &
339 (1 << band_idx);
340
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530341 pr_debug("%s: IIR #%d band #%d enable %d\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530342 iir_idx, band_idx,
343 (uint32_t)ucontrol->value.integer.value[0]);
344 return 0;
345}
346
347static int sitar_put_iir_enable_audio_mixer(
348 struct snd_kcontrol *kcontrol,
349 struct snd_ctl_elem_value *ucontrol)
350{
351 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
352 int iir_idx = ((struct soc_multi_mixer_control *)
353 kcontrol->private_value)->reg;
354 int band_idx = ((struct soc_multi_mixer_control *)
355 kcontrol->private_value)->shift;
356 int value = ucontrol->value.integer.value[0];
357
358 /* Mask first 5 bits, 6-8 are reserved */
359 snd_soc_update_bits(codec, (SITAR_A_CDC_IIR1_CTL + 16 * iir_idx),
360 (1 << band_idx), (value << band_idx));
361
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530362 pr_debug("%s: IIR #%d band #%d enable %d\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530363 iir_idx, band_idx, value);
364 return 0;
365}
366static uint32_t get_iir_band_coeff(struct snd_soc_codec *codec,
367 int iir_idx, int band_idx,
368 int coeff_idx)
369{
370 /* Address does not automatically update if reading */
371 snd_soc_update_bits(codec,
372 (SITAR_A_CDC_IIR1_COEF_B1_CTL + 16 * iir_idx),
373 0x1F, band_idx * BAND_MAX + coeff_idx);
374
375 /* Mask bits top 2 bits since they are reserved */
376 return ((snd_soc_read(codec,
377 (SITAR_A_CDC_IIR1_COEF_B2_CTL + 16 * iir_idx)) << 24) |
378 (snd_soc_read(codec,
379 (SITAR_A_CDC_IIR1_COEF_B3_CTL + 16 * iir_idx)) << 16) |
380 (snd_soc_read(codec,
381 (SITAR_A_CDC_IIR1_COEF_B4_CTL + 16 * iir_idx)) << 8) |
382 (snd_soc_read(codec,
383 (SITAR_A_CDC_IIR1_COEF_B5_CTL + 16 * iir_idx)))) &
384 0x3FFFFFFF;
385}
386
387static int sitar_get_iir_band_audio_mixer(
388 struct snd_kcontrol *kcontrol,
389 struct snd_ctl_elem_value *ucontrol)
390{
391 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
392 int iir_idx = ((struct soc_multi_mixer_control *)
393 kcontrol->private_value)->reg;
394 int band_idx = ((struct soc_multi_mixer_control *)
395 kcontrol->private_value)->shift;
396
397 ucontrol->value.integer.value[0] =
398 get_iir_band_coeff(codec, iir_idx, band_idx, 0);
399 ucontrol->value.integer.value[1] =
400 get_iir_band_coeff(codec, iir_idx, band_idx, 1);
401 ucontrol->value.integer.value[2] =
402 get_iir_band_coeff(codec, iir_idx, band_idx, 2);
403 ucontrol->value.integer.value[3] =
404 get_iir_band_coeff(codec, iir_idx, band_idx, 3);
405 ucontrol->value.integer.value[4] =
406 get_iir_band_coeff(codec, iir_idx, band_idx, 4);
407
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530408 pr_debug("%s: IIR #%d band #%d b0 = 0x%x\n"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530409 "%s: IIR #%d band #%d b1 = 0x%x\n"
410 "%s: IIR #%d band #%d b2 = 0x%x\n"
411 "%s: IIR #%d band #%d a1 = 0x%x\n"
412 "%s: IIR #%d band #%d a2 = 0x%x\n",
413 __func__, iir_idx, band_idx,
414 (uint32_t)ucontrol->value.integer.value[0],
415 __func__, iir_idx, band_idx,
416 (uint32_t)ucontrol->value.integer.value[1],
417 __func__, iir_idx, band_idx,
418 (uint32_t)ucontrol->value.integer.value[2],
419 __func__, iir_idx, band_idx,
420 (uint32_t)ucontrol->value.integer.value[3],
421 __func__, iir_idx, band_idx,
422 (uint32_t)ucontrol->value.integer.value[4]);
423 return 0;
424}
425
426static void set_iir_band_coeff(struct snd_soc_codec *codec,
427 int iir_idx, int band_idx,
428 int coeff_idx, uint32_t value)
429{
430 /* Mask top 3 bits, 6-8 are reserved */
431 /* Update address manually each time */
432 snd_soc_update_bits(codec,
433 (SITAR_A_CDC_IIR1_COEF_B1_CTL + 16 * iir_idx),
434 0x1F, band_idx * BAND_MAX + coeff_idx);
435
436 /* Mask top 2 bits, 7-8 are reserved */
437 snd_soc_update_bits(codec,
438 (SITAR_A_CDC_IIR1_COEF_B2_CTL + 16 * iir_idx),
439 0x3F, (value >> 24) & 0x3F);
440
441 /* Isolate 8bits at a time */
442 snd_soc_update_bits(codec,
443 (SITAR_A_CDC_IIR1_COEF_B3_CTL + 16 * iir_idx),
444 0xFF, (value >> 16) & 0xFF);
445
446 snd_soc_update_bits(codec,
447 (SITAR_A_CDC_IIR1_COEF_B4_CTL + 16 * iir_idx),
448 0xFF, (value >> 8) & 0xFF);
449
450 snd_soc_update_bits(codec,
451 (SITAR_A_CDC_IIR1_COEF_B5_CTL + 16 * iir_idx),
452 0xFF, value & 0xFF);
453}
454
455static int sitar_put_iir_band_audio_mixer(
456 struct snd_kcontrol *kcontrol,
457 struct snd_ctl_elem_value *ucontrol)
458{
459 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
460 int iir_idx = ((struct soc_multi_mixer_control *)
461 kcontrol->private_value)->reg;
462 int band_idx = ((struct soc_multi_mixer_control *)
463 kcontrol->private_value)->shift;
464
465 set_iir_band_coeff(codec, iir_idx, band_idx, 0,
466 ucontrol->value.integer.value[0]);
467 set_iir_band_coeff(codec, iir_idx, band_idx, 1,
468 ucontrol->value.integer.value[1]);
469 set_iir_band_coeff(codec, iir_idx, band_idx, 2,
470 ucontrol->value.integer.value[2]);
471 set_iir_band_coeff(codec, iir_idx, band_idx, 3,
472 ucontrol->value.integer.value[3]);
473 set_iir_band_coeff(codec, iir_idx, band_idx, 4,
474 ucontrol->value.integer.value[4]);
475
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530476 pr_debug("%s: IIR #%d band #%d b0 = 0x%x\n"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530477 "%s: IIR #%d band #%d b1 = 0x%x\n"
478 "%s: IIR #%d band #%d b2 = 0x%x\n"
479 "%s: IIR #%d band #%d a1 = 0x%x\n"
480 "%s: IIR #%d band #%d a2 = 0x%x\n",
481 __func__, iir_idx, band_idx,
482 get_iir_band_coeff(codec, iir_idx, band_idx, 0),
483 __func__, iir_idx, band_idx,
484 get_iir_band_coeff(codec, iir_idx, band_idx, 1),
485 __func__, iir_idx, band_idx,
486 get_iir_band_coeff(codec, iir_idx, band_idx, 2),
487 __func__, iir_idx, band_idx,
488 get_iir_band_coeff(codec, iir_idx, band_idx, 3),
489 __func__, iir_idx, band_idx,
490 get_iir_band_coeff(codec, iir_idx, band_idx, 4));
491 return 0;
492}
493
494static const char *sitar_ear_pa_gain_text[] = {"POS_6_DB", "POS_2_DB"};
495static const struct soc_enum sitar_ear_pa_gain_enum[] = {
496 SOC_ENUM_SINGLE_EXT(2, sitar_ear_pa_gain_text),
497};
498
499/*cut of frequency for high pass filter*/
500static const char *cf_text[] = {
501 "MIN_3DB_4Hz", "MIN_3DB_75Hz", "MIN_3DB_150Hz"
502};
503
504static const struct soc_enum cf_dec1_enum =
505 SOC_ENUM_SINGLE(SITAR_A_CDC_TX1_MUX_CTL, 4, 3, cf_text);
506
507static const struct soc_enum cf_rxmix1_enum =
508 SOC_ENUM_SINGLE(SITAR_A_CDC_RX1_B4_CTL, 1, 3, cf_text);
509
510static const struct snd_kcontrol_new sitar_snd_controls[] = {
511
512 SOC_ENUM_EXT("EAR PA Gain", sitar_ear_pa_gain_enum[0],
513 sitar_pa_gain_get, sitar_pa_gain_put),
514
515 SOC_SINGLE_TLV("LINEOUT1 Volume", SITAR_A_RX_LINE_1_GAIN, 0, 12, 1,
516 line_gain),
517 SOC_SINGLE_TLV("LINEOUT2 Volume", SITAR_A_RX_LINE_2_GAIN, 0, 12, 1,
518 line_gain),
519
520 SOC_SINGLE_TLV("HPHL Volume", SITAR_A_RX_HPH_L_GAIN, 0, 12, 1,
521 line_gain),
522 SOC_SINGLE_TLV("HPHR Volume", SITAR_A_RX_HPH_R_GAIN, 0, 12, 1,
523 line_gain),
524
525 SOC_SINGLE_S8_TLV("RX1 Digital Volume", SITAR_A_CDC_RX1_VOL_CTL_B2_CTL,
526 -84, 40, digital_gain),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800527 SOC_SINGLE_S8_TLV("RX2 Digital Volume", SITAR_A_CDC_RX2_VOL_CTL_B2_CTL,
528 -84, 40, digital_gain),
529 SOC_SINGLE_S8_TLV("RX3 Digital Volume", SITAR_A_CDC_RX3_VOL_CTL_B2_CTL,
530 -84, 40, digital_gain),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530531
532 SOC_SINGLE_S8_TLV("DEC1 Volume", SITAR_A_CDC_TX1_VOL_CTL_GAIN, -84, 40,
533 digital_gain),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800534 SOC_SINGLE_S8_TLV("DEC2 Volume", SITAR_A_CDC_TX2_VOL_CTL_GAIN, -84, 40,
535 digital_gain),
536 SOC_SINGLE_S8_TLV("DEC3 Volume", SITAR_A_CDC_TX3_VOL_CTL_GAIN, -84, 40,
537 digital_gain),
538 SOC_SINGLE_S8_TLV("DEC4 Volume", SITAR_A_CDC_TX4_VOL_CTL_GAIN, -84, 40,
539 digital_gain),
540
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530541 SOC_SINGLE_S8_TLV("IIR1 INP1 Volume", SITAR_A_CDC_IIR1_GAIN_B1_CTL, -84,
542 40, digital_gain),
543 SOC_SINGLE_S8_TLV("IIR1 INP2 Volume", SITAR_A_CDC_IIR1_GAIN_B2_CTL, -84,
544 40, digital_gain),
545 SOC_SINGLE_S8_TLV("IIR1 INP3 Volume", SITAR_A_CDC_IIR1_GAIN_B3_CTL, -84,
546 40, digital_gain),
547 SOC_SINGLE_S8_TLV("IIR1 INP4 Volume", SITAR_A_CDC_IIR1_GAIN_B4_CTL, -84,
548 40, digital_gain),
549 SOC_SINGLE_TLV("ADC1 Volume", SITAR_A_TX_1_2_EN, 5, 3, 0, analog_gain),
550 SOC_SINGLE_TLV("ADC2 Volume", SITAR_A_TX_1_2_EN, 1, 3, 0, analog_gain),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800551 SOC_SINGLE_TLV("ADC3 Volume", SITAR_A_TX_3_EN, 5, 3, 0, analog_gain),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530552
553 SOC_SINGLE("MICBIAS1 CAPLESS Switch", SITAR_A_MICB_1_CTL, 4, 1, 1),
554 SOC_SINGLE("MICBIAS2 CAPLESS Switch", SITAR_A_MICB_2_CTL, 4, 1, 1),
555
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700556 SOC_SINGLE_EXT("ANC Slot", SND_SOC_NOPM, 0, 0, 100, sitar_get_anc_slot,
557 sitar_put_anc_slot),
558
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530559 SOC_ENUM("TX1 HPF cut off", cf_dec1_enum),
560
561 SOC_SINGLE("TX1 HPF Switch", SITAR_A_CDC_TX1_MUX_CTL, 3, 1, 0),
562
563 SOC_SINGLE("RX1 HPF Switch", SITAR_A_CDC_RX1_B5_CTL, 2, 1, 0),
564
565 SOC_ENUM("RX1 HPF cut off", cf_rxmix1_enum),
566
567 SOC_SINGLE_EXT("IIR1 Enable Band1", IIR1, BAND1, 1, 0,
568 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
569 SOC_SINGLE_EXT("IIR1 Enable Band2", IIR1, BAND2, 1, 0,
570 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
571 SOC_SINGLE_EXT("IIR1 Enable Band3", IIR1, BAND3, 1, 0,
572 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
573 SOC_SINGLE_EXT("IIR1 Enable Band4", IIR1, BAND4, 1, 0,
574 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
575 SOC_SINGLE_EXT("IIR1 Enable Band5", IIR1, BAND5, 1, 0,
576 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
577 SOC_SINGLE_EXT("IIR2 Enable Band1", IIR2, BAND1, 1, 0,
578 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
579 SOC_SINGLE_EXT("IIR2 Enable Band2", IIR2, BAND2, 1, 0,
580 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
581 SOC_SINGLE_EXT("IIR2 Enable Band3", IIR2, BAND3, 1, 0,
582 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
583 SOC_SINGLE_EXT("IIR2 Enable Band4", IIR2, BAND4, 1, 0,
584 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
585 SOC_SINGLE_EXT("IIR2 Enable Band5", IIR2, BAND5, 1, 0,
586 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
587
588 SOC_SINGLE_MULTI_EXT("IIR1 Band1", IIR1, BAND1, 255, 0, 5,
589 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
590 SOC_SINGLE_MULTI_EXT("IIR1 Band2", IIR1, BAND2, 255, 0, 5,
591 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
592 SOC_SINGLE_MULTI_EXT("IIR1 Band3", IIR1, BAND3, 255, 0, 5,
593 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
594 SOC_SINGLE_MULTI_EXT("IIR1 Band4", IIR1, BAND4, 255, 0, 5,
595 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
596 SOC_SINGLE_MULTI_EXT("IIR1 Band5", IIR1, BAND5, 255, 0, 5,
597 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
598 SOC_SINGLE_MULTI_EXT("IIR2 Band1", IIR2, BAND1, 255, 0, 5,
599 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
600 SOC_SINGLE_MULTI_EXT("IIR2 Band2", IIR2, BAND2, 255, 0, 5,
601 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
602 SOC_SINGLE_MULTI_EXT("IIR2 Band3", IIR2, BAND3, 255, 0, 5,
603 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
604 SOC_SINGLE_MULTI_EXT("IIR2 Band4", IIR2, BAND4, 255, 0, 5,
605 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
606 SOC_SINGLE_MULTI_EXT("IIR2 Band5", IIR2, BAND5, 255, 0, 5,
607 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
608};
609
610static const char *rx_mix1_text[] = {
611 "ZERO", "SRC1", "SRC2", "IIR1", "IIR2", "RX1", "RX2", "RX3", "RX4",
612 "RX5"
613};
614
615static const char *rx_dac1_text[] = {
616 "ZERO", "RX1", "RX2"
617};
618
619static const char *rx_dac2_text[] = {
620 "ZERO", "RX1",
621};
622
623static const char *rx_dac3_text[] = {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800624 "ZERO", "RX1", "INV_RX1", "RX2"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530625};
626
627static const char *rx_dac4_text[] = {
628 "ZERO", "ON"
629};
630
631static const char *sb_tx1_mux_text[] = {
632 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
633 "DEC1"
634};
635
636static const char *sb_tx2_mux_text[] = {
637 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
638 "DEC2"
639};
640
641static const char *sb_tx3_mux_text[] = {
642 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
643 "DEC3"
644};
645
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800646static const char *sb_tx4_mux_text[] = {
647 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
648 "DEC4"
649};
650
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530651static const char *sb_tx5_mux_text[] = {
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -0700652 "ZERO", "RMIX1", "RMIX2", "RMIX3", "DEC1", "DEC2", "DEC3", "DEC4"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530653};
654
655static const char *dec1_mux_text[] = {
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700656 "ZERO", "DMIC1", "ADC1", "ADC2", "ADC3", "MBADC", "DMIC4", "ANC1_FB",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530657};
658
659static const char *dec2_mux_text[] = {
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700660 "ZERO", "DMIC2", "ADC1", "ADC2", "ADC3", "MBADC", "DMIC3", "ANC2_FB",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530661};
662
663static const char *dec3_mux_text[] = {
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -0700664 "ZERO", "DMIC3", "ADC1", "ADC2", "ADC3", "MBADC", "DMIC2", "DMIC4"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530665};
666
667static const char *dec4_mux_text[] = {
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -0700668 "ZERO", "DMIC4", "ADC1", "ADC2", "ADC3", "DMIC3", "DMIC2", "DMIC1"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530669};
670
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700671static const char const *anc_mux_text[] = {
672 "ZERO", "ADC1", "ADC2", "ADC3", "RSVD1", "RSVD2", "RSVD3",
673 "MBADC", "RSVD4", "DMIC1", "DMIC2", "DMIC3", "DMIC4"
674};
675
676static const char const *anc1_fb_mux_text[] = {
677 "ZERO", "EAR_HPH_L", "EAR_LINE_1",
678};
679
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530680static const char *iir1_inp1_text[] = {
681 "ZERO", "DEC1", "DEC2", "DEC3", "DEC4", "ZERO", "ZERO", "ZERO",
682 "ZERO", "ZERO", "ZERO", "RX1", "RX2", "RX3", "RX4", "RX5",
683};
684
685static const struct soc_enum rx_mix1_inp1_chain_enum =
686 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX1_B1_CTL, 0, 10, rx_mix1_text);
687
688static const struct soc_enum rx_mix1_inp2_chain_enum =
689 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX1_B1_CTL, 4, 10, rx_mix1_text);
690
691static const struct soc_enum rx2_mix1_inp1_chain_enum =
692 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX2_B1_CTL, 0, 10, rx_mix1_text);
693
694static const struct soc_enum rx2_mix1_inp2_chain_enum =
695 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX2_B1_CTL, 4, 10, rx_mix1_text);
696
697static const struct soc_enum rx3_mix1_inp1_chain_enum =
698 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX3_B1_CTL, 0, 10, rx_mix1_text);
699
700static const struct soc_enum rx3_mix1_inp2_chain_enum =
701 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX3_B1_CTL, 4, 10, rx_mix1_text);
702
703static const struct soc_enum rx_dac1_enum =
704 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 6, 3, rx_dac1_text);
705
706static const struct soc_enum rx_dac2_enum =
707 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 4, 2, rx_dac2_text);
708
709static const struct soc_enum rx_dac3_enum =
710 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 2, 4, rx_dac3_text);
711
712static const struct soc_enum rx_dac4_enum =
713 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 0, 2, rx_dac4_text);
714
715static const struct soc_enum sb_tx5_mux_enum =
716 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B5_CTL, 0, 9, sb_tx5_mux_text);
717
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800718static const struct soc_enum sb_tx4_mux_enum =
719 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B4_CTL, 0, 9, sb_tx4_mux_text);
720
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530721static const struct soc_enum sb_tx3_mux_enum =
722 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B3_CTL, 0, 9, sb_tx3_mux_text);
723
724static const struct soc_enum sb_tx2_mux_enum =
725 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B2_CTL, 0, 9, sb_tx2_mux_text);
726
727static const struct soc_enum sb_tx1_mux_enum =
728 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B1_CTL, 0, 9, sb_tx1_mux_text);
729
730static const struct soc_enum dec1_mux_enum =
731 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B1_CTL, 0, 8, dec1_mux_text);
732
733static const struct soc_enum dec2_mux_enum =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800734 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B1_CTL, 3, 8, dec2_mux_text);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530735
736static const struct soc_enum dec3_mux_enum =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800737 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B2_CTL, 0, 8, dec3_mux_text);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530738
739static const struct soc_enum dec4_mux_enum =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800740 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B2_CTL, 3, 8, dec4_mux_text);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530741
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700742static const struct soc_enum anc1_mux_enum =
743 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_ANC_B1_CTL, 0, 13, anc_mux_text);
744
745static const struct soc_enum anc2_mux_enum =
746 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_ANC_B1_CTL, 4, 13, anc_mux_text);
747
748static const struct soc_enum anc1_fb_mux_enum =
749 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_ANC_B2_CTL, 0, 3, anc1_fb_mux_text);
750
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530751static const struct soc_enum iir1_inp1_mux_enum =
752 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_EQ1_B1_CTL, 0, 16, iir1_inp1_text);
753
754static const struct snd_kcontrol_new rx_mix1_inp1_mux =
755 SOC_DAPM_ENUM("RX1 MIX1 INP1 Mux", rx_mix1_inp1_chain_enum);
756
757static const struct snd_kcontrol_new rx_mix1_inp2_mux =
758 SOC_DAPM_ENUM("RX1 MIX1 INP2 Mux", rx_mix1_inp2_chain_enum);
759
760static const struct snd_kcontrol_new rx2_mix1_inp1_mux =
761 SOC_DAPM_ENUM("RX2 MIX1 INP1 Mux", rx2_mix1_inp1_chain_enum);
762
763static const struct snd_kcontrol_new rx2_mix1_inp2_mux =
764 SOC_DAPM_ENUM("RX2 MIX1 INP2 Mux", rx2_mix1_inp2_chain_enum);
765
766static const struct snd_kcontrol_new rx3_mix1_inp1_mux =
767 SOC_DAPM_ENUM("RX3 MIX1 INP1 Mux", rx3_mix1_inp1_chain_enum);
768
769static const struct snd_kcontrol_new rx3_mix1_inp2_mux =
770 SOC_DAPM_ENUM("RX3 MIX1 INP2 Mux", rx3_mix1_inp2_chain_enum);
771
772static const struct snd_kcontrol_new rx_dac1_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800773 SOC_DAPM_ENUM("RX DAC1 Mux", rx_dac1_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530774
775static const struct snd_kcontrol_new rx_dac2_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800776 SOC_DAPM_ENUM("RX DAC2 Mux", rx_dac2_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530777
778static const struct snd_kcontrol_new rx_dac3_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800779 SOC_DAPM_ENUM("RX DAC3 Mux", rx_dac3_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530780
781static const struct snd_kcontrol_new rx_dac4_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800782 SOC_DAPM_ENUM("RX DAC4 Mux", rx_dac4_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530783
784static const struct snd_kcontrol_new sb_tx5_mux =
785 SOC_DAPM_ENUM("SLIM TX5 MUX Mux", sb_tx5_mux_enum);
786
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800787static const struct snd_kcontrol_new sb_tx4_mux =
788 SOC_DAPM_ENUM("SLIM TX4 MUX Mux", sb_tx4_mux_enum);
789
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530790static const struct snd_kcontrol_new sb_tx3_mux =
791 SOC_DAPM_ENUM("SLIM TX3 MUX Mux", sb_tx3_mux_enum);
792
793static const struct snd_kcontrol_new sb_tx2_mux =
794 SOC_DAPM_ENUM("SLIM TX2 MUX Mux", sb_tx2_mux_enum);
795
796static const struct snd_kcontrol_new sb_tx1_mux =
797 SOC_DAPM_ENUM("SLIM TX1 MUX Mux", sb_tx1_mux_enum);
798
799static const struct snd_kcontrol_new dec1_mux =
800 SOC_DAPM_ENUM("DEC1 MUX Mux", dec1_mux_enum);
801
802static const struct snd_kcontrol_new dec2_mux =
803 SOC_DAPM_ENUM("DEC2 MUX Mux", dec2_mux_enum);
804
805static const struct snd_kcontrol_new dec3_mux =
806 SOC_DAPM_ENUM("DEC3 MUX Mux", dec3_mux_enum);
807
808static const struct snd_kcontrol_new dec4_mux =
809 SOC_DAPM_ENUM("DEC4 MUX Mux", dec4_mux_enum);
810
811static const struct snd_kcontrol_new iir1_inp1_mux =
812 SOC_DAPM_ENUM("IIR1 INP1 Mux", iir1_inp1_mux_enum);
813
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700814static const struct snd_kcontrol_new anc1_mux =
815 SOC_DAPM_ENUM("ANC1 MUX Mux", anc1_mux_enum);
816
817static const struct snd_kcontrol_new anc2_mux =
818 SOC_DAPM_ENUM("ANC2 MUX Mux", anc2_mux_enum);
819
820static const struct snd_kcontrol_new anc1_fb_mux =
821 SOC_DAPM_ENUM("ANC1 FB MUX Mux", anc1_fb_mux_enum);
822
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530823static const struct snd_kcontrol_new dac1_switch[] = {
824 SOC_DAPM_SINGLE("Switch", SITAR_A_RX_EAR_EN, 5, 1, 0),
825};
826
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530827static void sitar_codec_enable_adc_block(struct snd_soc_codec *codec,
828 int enable)
829{
830 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
831
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530832 pr_debug("%s %d\n", __func__, enable);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530833
834 if (enable) {
835 sitar->adc_count++;
836 snd_soc_update_bits(codec, SITAR_A_TX_COM_BIAS, 0xE0, 0xE0);
837
838 } else {
839 sitar->adc_count--;
840 if (!sitar->adc_count) {
841 if (!sitar->mbhc_polling_active)
842 snd_soc_update_bits(codec, SITAR_A_TX_COM_BIAS,
843 0xE0, 0x0);
844 }
845 }
846}
847
848static int sitar_codec_enable_adc(struct snd_soc_dapm_widget *w,
849 struct snd_kcontrol *kcontrol, int event)
850{
851 struct snd_soc_codec *codec = w->codec;
852 u16 adc_reg;
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700853 u8 init_bit_shift;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530854
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530855 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530856
857 if (w->reg == SITAR_A_TX_1_2_EN)
858 adc_reg = SITAR_A_TX_1_2_TEST_CTL;
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700859 else if (w->reg == SITAR_A_TX_3_EN)
860 adc_reg = SITAR_A_TX_3_TEST_CTL;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530861 else {
862 pr_err("%s: Error, invalid adc register\n", __func__);
863 return -EINVAL;
864 }
865
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700866 if (w->shift == 3)
867 init_bit_shift = 6;
868 else if (w->shift == 7)
869 init_bit_shift = 7;
870 else {
871 pr_err("%s: Error, invalid init bit postion adc register\n",
872 __func__);
873 return -EINVAL;
874 }
875
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530876 switch (event) {
877 case SND_SOC_DAPM_PRE_PMU:
878 sitar_codec_enable_adc_block(codec, 1);
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700879 snd_soc_update_bits(codec, adc_reg, 1 << init_bit_shift,
880 1 << init_bit_shift);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530881 break;
882 case SND_SOC_DAPM_POST_PMU:
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700883
884 snd_soc_update_bits(codec, adc_reg, 1 << init_bit_shift, 0x00);
885
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530886 break;
887 case SND_SOC_DAPM_POST_PMD:
888 sitar_codec_enable_adc_block(codec, 0);
889 break;
890 }
891 return 0;
892}
893
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800894static int sitar_lineout_dac_event(struct snd_soc_dapm_widget *w,
895 struct snd_kcontrol *kcontrol, int event)
896{
897 struct snd_soc_codec *codec = w->codec;
898
899 pr_debug("%s %s %d\n", __func__, w->name, event);
900
901 switch (event) {
902 case SND_SOC_DAPM_PRE_PMU:
903 snd_soc_update_bits(codec, w->reg, 0x40, 0x40);
904 break;
905
906 case SND_SOC_DAPM_POST_PMD:
907 snd_soc_update_bits(codec, w->reg, 0x40, 0x00);
908 break;
909 }
910 return 0;
911}
912
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530913static int sitar_codec_enable_lineout(struct snd_soc_dapm_widget *w,
914 struct snd_kcontrol *kcontrol, int event)
915{
916 struct snd_soc_codec *codec = w->codec;
917 u16 lineout_gain_reg;
918
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530919 pr_debug("%s %d %s\n", __func__, event, w->name);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530920
921 switch (w->shift) {
922 case 0:
923 lineout_gain_reg = SITAR_A_RX_LINE_1_GAIN;
924 break;
925 case 1:
926 lineout_gain_reg = SITAR_A_RX_LINE_2_GAIN;
927 break;
928 default:
929 pr_err("%s: Error, incorrect lineout register value\n",
930 __func__);
931 return -EINVAL;
932 }
933
934 switch (event) {
935 case SND_SOC_DAPM_PRE_PMU:
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800936 snd_soc_update_bits(codec, lineout_gain_reg, 0x10, 0x10);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530937 break;
938 case SND_SOC_DAPM_POST_PMU:
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530939 pr_debug("%s: sleeping 16 ms after %s PA turn on\n",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530940 __func__, w->name);
941 usleep_range(16000, 16000);
942 break;
943 case SND_SOC_DAPM_POST_PMD:
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800944 snd_soc_update_bits(codec, lineout_gain_reg, 0x10, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530945 break;
946 }
947 return 0;
948}
949
950static int sitar_codec_enable_dmic(struct snd_soc_dapm_widget *w,
951 struct snd_kcontrol *kcontrol, int event)
952{
953 struct snd_soc_codec *codec = w->codec;
Bhalchandra Gajare37f4ab72012-04-10 18:52:37 -0700954 u16 tx_dmic_ctl_reg, tx_mux_ctl_reg;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530955 u8 dmic_clk_sel, dmic_clk_en;
956 unsigned int dmic;
957 int ret;
958
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -0700959 ret = kstrtouint(strpbrk(w->name, "1234"), 10, &dmic);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530960 if (ret < 0) {
961 pr_err("%s: Invalid DMIC line on the codec\n", __func__);
962 return -EINVAL;
963 }
964
965 switch (dmic) {
966 case 1:
967 case 2:
968 dmic_clk_sel = 0x02;
969 dmic_clk_en = 0x01;
970 break;
971 case 3:
972 case 4:
973 dmic_clk_sel = 0x08;
974 dmic_clk_en = 0x04;
975 break;
976
977 break;
978
979 default:
980 pr_err("%s: Invalid DMIC Selection\n", __func__);
981 return -EINVAL;
982 }
983
Bhalchandra Gajare37f4ab72012-04-10 18:52:37 -0700984 tx_mux_ctl_reg = SITAR_A_CDC_TX1_MUX_CTL + 8 * (dmic - 1);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530985 tx_dmic_ctl_reg = SITAR_A_CDC_TX1_DMIC_CTL + 8 * (dmic - 1);
986
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530987 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530988
989 switch (event) {
990 case SND_SOC_DAPM_PRE_PMU:
Bhalchandra Gajare37f4ab72012-04-10 18:52:37 -0700991 snd_soc_update_bits(codec, tx_mux_ctl_reg, 0x01, 0x01);
992
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530993 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_DMIC_CTL,
994 dmic_clk_sel, dmic_clk_sel);
995
996 snd_soc_update_bits(codec, tx_dmic_ctl_reg, 0x1, 0x1);
997
998 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_DMIC_CTL,
999 dmic_clk_en, dmic_clk_en);
1000 break;
1001 case SND_SOC_DAPM_POST_PMD:
1002 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_DMIC_CTL,
1003 dmic_clk_en, 0);
Bhalchandra Gajare37f4ab72012-04-10 18:52:37 -07001004
1005 snd_soc_update_bits(codec, tx_mux_ctl_reg, 0x01, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301006 break;
1007 }
1008 return 0;
1009}
1010
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07001011static int sitar_codec_enable_anc(struct snd_soc_dapm_widget *w,
1012 struct snd_kcontrol *kcontrol, int event)
1013{
1014 struct snd_soc_codec *codec = w->codec;
1015 const char *filename;
1016 const struct firmware *fw;
1017 int i;
1018 int ret;
1019 int num_anc_slots;
1020 struct anc_header *anc_head;
1021 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1022 u32 anc_writes_size = 0;
1023 int anc_size_remaining;
1024 u32 *anc_ptr;
1025 u16 reg;
1026 u8 mask, val, old_val;
1027
1028 pr_debug("%s %d\n", __func__, event);
1029 switch (event) {
1030 case SND_SOC_DAPM_PRE_PMU:
1031
1032 /* Use the same firmware file as that of WCD9310,
1033 * since the register sequences are same for
1034 * WCD9310 and WCD9304
1035 */
1036 filename = "wcd9310/wcd9310_anc.bin";
1037
1038 ret = request_firmware(&fw, filename, codec->dev);
1039 if (ret != 0) {
1040 dev_err(codec->dev, "Failed to acquire ANC data: %d\n",
1041 ret);
1042 return -ENODEV;
1043 }
1044
1045 if (fw->size < sizeof(struct anc_header)) {
1046 dev_err(codec->dev, "Not enough data\n");
1047 release_firmware(fw);
1048 return -ENOMEM;
1049 }
1050
1051 /* First number is the number of register writes */
1052 anc_head = (struct anc_header *)(fw->data);
1053 anc_ptr = (u32 *)((u32)fw->data + sizeof(struct anc_header));
1054 anc_size_remaining = fw->size - sizeof(struct anc_header);
1055 num_anc_slots = anc_head->num_anc_slots;
1056
1057 if (sitar->anc_slot >= num_anc_slots) {
1058 dev_err(codec->dev, "Invalid ANC slot selected\n");
1059 release_firmware(fw);
1060 return -EINVAL;
1061 }
1062
1063 for (i = 0; i < num_anc_slots; i++) {
1064
1065 if (anc_size_remaining < SITAR_PACKED_REG_SIZE) {
1066 dev_err(codec->dev, "Invalid register format\n");
1067 release_firmware(fw);
1068 return -EINVAL;
1069 }
1070 anc_writes_size = (u32)(*anc_ptr);
1071 anc_size_remaining -= sizeof(u32);
1072 anc_ptr += 1;
1073
1074 if (anc_writes_size * SITAR_PACKED_REG_SIZE
1075 > anc_size_remaining) {
1076 dev_err(codec->dev, "Invalid register format\n");
1077 release_firmware(fw);
1078 return -ENOMEM;
1079 }
1080
1081 if (sitar->anc_slot == i)
1082 break;
1083
1084 anc_size_remaining -= (anc_writes_size *
1085 SITAR_PACKED_REG_SIZE);
1086 anc_ptr += anc_writes_size;
1087 }
1088 if (i == num_anc_slots) {
1089 dev_err(codec->dev, "Selected ANC slot not present\n");
1090 release_firmware(fw);
1091 return -ENOMEM;
1092 }
1093
1094 for (i = 0; i < anc_writes_size; i++) {
1095 SITAR_CODEC_UNPACK_ENTRY(anc_ptr[i], reg,
1096 mask, val);
1097 old_val = snd_soc_read(codec, reg);
1098 snd_soc_write(codec, reg, (old_val & ~mask) |
1099 (val & mask));
1100 }
1101
1102 release_firmware(fw);
1103
1104 /* For Sitar, it is required to enable both Feed-forward
1105 * and Feed back clocks to enable ANC
1106 */
1107 snd_soc_write(codec, SITAR_A_CDC_CLK_ANC_CLK_EN_CTL, 0x0F);
1108
1109 break;
1110
1111 case SND_SOC_DAPM_POST_PMD:
1112 snd_soc_write(codec, SITAR_A_CDC_CLK_ANC_RESET_CTL, 0xFF);
1113 snd_soc_write(codec, SITAR_A_CDC_CLK_ANC_CLK_EN_CTL, 0x00);
1114 break;
1115 }
1116 return 0;
1117}
1118
1119
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301120static void sitar_codec_start_hs_polling(struct snd_soc_codec *codec)
1121{
1122 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001123 int mbhc_state = sitar->mbhc_state;
1124
1125 pr_debug("%s: enter\n", __func__);
1126 if (!sitar->mbhc_polling_active) {
1127 pr_debug("Polling is not active, do not start polling\n");
1128 return;
1129 }
1130 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
1131
1132
1133 if (!sitar->no_mic_headset_override) {
1134 if (mbhc_state == MBHC_STATE_POTENTIAL) {
1135 pr_debug("%s recovering MBHC state macine\n", __func__);
1136 sitar->mbhc_state = MBHC_STATE_POTENTIAL_RECOVERY;
1137 /* set to max button press threshold */
1138 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B2_CTL,
1139 0x7F);
1140 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B1_CTL,
1141 0xFF);
1142 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B4_CTL,
1143 0x7F);
1144 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B3_CTL,
1145 0xFF);
1146 /* set to max */
1147 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B6_CTL,
1148 0x7F);
1149 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B5_CTL,
1150 0xFF);
1151 }
1152 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301153
1154 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001155 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x1);
1156 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x0);
1157 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x1);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301158}
1159
1160static void sitar_codec_pause_hs_polling(struct snd_soc_codec *codec)
1161{
1162 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1163
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001164 pr_debug("%s: enter\n", __func__);
1165 if (!sitar->mbhc_polling_active) {
1166 pr_debug("polling not active, nothing to pause\n");
1167 return;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301168 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001169
1170 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
1171 pr_debug("%s: leave\n", __func__);
1172
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301173}
1174
1175static void sitar_codec_switch_cfilt_mode(struct snd_soc_codec *codec,
1176 int mode)
1177{
1178 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1179 u8 reg_mode_val, cur_mode_val;
1180 bool mbhc_was_polling = false;
1181
1182 if (mode)
1183 reg_mode_val = SITAR_CFILT_FAST_MODE;
1184 else
1185 reg_mode_val = SITAR_CFILT_SLOW_MODE;
1186
1187 cur_mode_val = snd_soc_read(codec,
1188 sitar->mbhc_bias_regs.cfilt_ctl) & 0x40;
1189
1190 if (cur_mode_val != reg_mode_val) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001191 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301192 if (sitar->mbhc_polling_active) {
1193 sitar_codec_pause_hs_polling(codec);
1194 mbhc_was_polling = true;
1195 }
1196 snd_soc_update_bits(codec,
1197 sitar->mbhc_bias_regs.cfilt_ctl, 0x40, reg_mode_val);
1198 if (mbhc_was_polling)
1199 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001200 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301201 pr_debug("%s: CFILT mode change (%x to %x)\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301202 cur_mode_val, reg_mode_val);
1203 } else {
1204 pr_err("%s: CFILT Value is already %x\n",
1205 __func__, cur_mode_val);
1206 }
1207}
1208
1209static void sitar_codec_update_cfilt_usage(struct snd_soc_codec *codec,
1210 u8 cfilt_sel, int inc)
1211{
1212 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1213 u32 *cfilt_cnt_ptr = NULL;
1214 u16 micb_cfilt_reg;
1215
1216 switch (cfilt_sel) {
1217 case SITAR_CFILT1_SEL:
1218 cfilt_cnt_ptr = &sitar->cfilt1_cnt;
1219 micb_cfilt_reg = SITAR_A_MICB_CFILT_1_CTL;
1220 break;
1221 case SITAR_CFILT2_SEL:
1222 cfilt_cnt_ptr = &sitar->cfilt2_cnt;
1223 micb_cfilt_reg = SITAR_A_MICB_CFILT_2_CTL;
1224 break;
1225 default:
1226 return; /* should not happen */
1227 }
1228
1229 if (inc) {
1230 if (!(*cfilt_cnt_ptr)++) {
1231 /* Switch CFILT to slow mode if MBHC CFILT being used */
1232 if (cfilt_sel == sitar->mbhc_bias_regs.cfilt_sel)
1233 sitar_codec_switch_cfilt_mode(codec, 0);
1234
1235 snd_soc_update_bits(codec, micb_cfilt_reg, 0x80, 0x80);
1236 }
1237 } else {
1238 /* check if count not zero, decrement
1239 * then check if zero, go ahead disable cfilter
1240 */
1241 if ((*cfilt_cnt_ptr) && !--(*cfilt_cnt_ptr)) {
1242 snd_soc_update_bits(codec, micb_cfilt_reg, 0x80, 0);
1243
1244 /* Switch CFILT to fast mode if MBHC CFILT being used */
1245 if (cfilt_sel == sitar->mbhc_bias_regs.cfilt_sel)
1246 sitar_codec_switch_cfilt_mode(codec, 1);
1247 }
1248 }
1249}
1250
1251static int sitar_find_k_value(unsigned int ldoh_v, unsigned int cfilt_mv)
1252{
1253 int rc = -EINVAL;
1254 unsigned min_mv, max_mv;
1255
1256 switch (ldoh_v) {
1257 case SITAR_LDOH_1P95_V:
1258 min_mv = 160;
1259 max_mv = 1800;
1260 break;
1261 case SITAR_LDOH_2P35_V:
1262 min_mv = 200;
1263 max_mv = 2200;
1264 break;
1265 case SITAR_LDOH_2P75_V:
1266 min_mv = 240;
1267 max_mv = 2600;
1268 break;
1269 case SITAR_LDOH_2P85_V:
1270 min_mv = 250;
1271 max_mv = 2700;
1272 break;
1273 default:
1274 goto done;
1275 }
1276
1277 if (cfilt_mv < min_mv || cfilt_mv > max_mv)
1278 goto done;
1279
1280 for (rc = 4; rc <= 44; rc++) {
1281 min_mv = max_mv * (rc) / 44;
1282 if (min_mv >= cfilt_mv) {
1283 rc -= 4;
1284 break;
1285 }
1286 }
1287done:
1288 return rc;
1289}
1290
1291static bool sitar_is_hph_pa_on(struct snd_soc_codec *codec)
1292{
1293 u8 hph_reg_val = 0;
1294 hph_reg_val = snd_soc_read(codec, SITAR_A_RX_HPH_CNP_EN);
1295
1296 return (hph_reg_val & 0x30) ? true : false;
1297}
1298
1299static bool sitar_is_hph_dac_on(struct snd_soc_codec *codec, int left)
1300{
1301 u8 hph_reg_val = 0;
1302 if (left)
1303 hph_reg_val = snd_soc_read(codec,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001304 SITAR_A_RX_HPH_L_DAC_CTL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301305 else
1306 hph_reg_val = snd_soc_read(codec,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001307 SITAR_A_RX_HPH_R_DAC_CTL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301308
1309 return (hph_reg_val & 0xC0) ? true : false;
1310}
1311
1312static void sitar_codec_switch_micbias(struct snd_soc_codec *codec,
1313 int vddio_switch)
1314{
1315 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1316 int cfilt_k_val;
1317 bool mbhc_was_polling = false;
1318
1319 switch (vddio_switch) {
1320 case 1:
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001321 if (sitar->mbhc_micbias_switched == 0 &&
1322 sitar->mbhc_polling_active) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301323
1324 sitar_codec_pause_hs_polling(codec);
1325 /* Enable Mic Bias switch to VDDIO */
1326 sitar->cfilt_k_value = snd_soc_read(codec,
1327 sitar->mbhc_bias_regs.cfilt_val);
1328 cfilt_k_val = sitar_find_k_value(
1329 sitar->pdata->micbias.ldoh_v, 1800);
1330 snd_soc_update_bits(codec,
1331 sitar->mbhc_bias_regs.cfilt_val,
1332 0xFC, (cfilt_k_val << 2));
1333
1334 snd_soc_update_bits(codec,
1335 sitar->mbhc_bias_regs.mbhc_reg, 0x80, 0x80);
1336 snd_soc_update_bits(codec,
1337 sitar->mbhc_bias_regs.mbhc_reg, 0x10, 0x00);
1338 sitar_codec_start_hs_polling(codec);
1339
1340 sitar->mbhc_micbias_switched = true;
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301341 pr_debug("%s: Enabled MBHC Mic bias to VDDIO Switch\n",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301342 __func__);
1343 }
1344 break;
1345
1346 case 0:
1347 if (sitar->mbhc_micbias_switched) {
1348 if (sitar->mbhc_polling_active) {
1349 sitar_codec_pause_hs_polling(codec);
1350 mbhc_was_polling = true;
1351 }
1352 /* Disable Mic Bias switch to VDDIO */
1353 if (sitar->cfilt_k_value != 0)
1354 snd_soc_update_bits(codec,
1355 sitar->mbhc_bias_regs.cfilt_val, 0XFC,
1356 sitar->cfilt_k_value);
1357 snd_soc_update_bits(codec,
1358 sitar->mbhc_bias_regs.mbhc_reg, 0x80, 0x00);
1359 snd_soc_update_bits(codec,
1360 sitar->mbhc_bias_regs.mbhc_reg, 0x10, 0x00);
1361
1362 if (mbhc_was_polling)
1363 sitar_codec_start_hs_polling(codec);
1364
1365 sitar->mbhc_micbias_switched = false;
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301366 pr_debug("%s: Disabled MBHC Mic bias to VDDIO Switch\n",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301367 __func__);
1368 }
1369 break;
1370 }
1371}
1372
1373static int sitar_codec_enable_micbias(struct snd_soc_dapm_widget *w,
1374 struct snd_kcontrol *kcontrol, int event)
1375{
1376 struct snd_soc_codec *codec = w->codec;
1377 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1378 u16 micb_int_reg;
1379 int micb_line;
1380 u8 cfilt_sel_val = 0;
1381 char *internal1_text = "Internal1";
1382 char *internal2_text = "Internal2";
1383
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301384 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301385 switch (w->reg) {
1386 case SITAR_A_MICB_1_CTL:
1387 micb_int_reg = SITAR_A_MICB_1_INT_RBIAS;
1388 cfilt_sel_val = sitar->pdata->micbias.bias1_cfilt_sel;
1389 micb_line = SITAR_MICBIAS1;
1390 break;
1391 case SITAR_A_MICB_2_CTL:
1392 micb_int_reg = SITAR_A_MICB_2_INT_RBIAS;
1393 cfilt_sel_val = sitar->pdata->micbias.bias2_cfilt_sel;
1394 micb_line = SITAR_MICBIAS2;
1395 break;
1396 default:
1397 pr_err("%s: Error, invalid micbias register\n", __func__);
1398 return -EINVAL;
1399 }
1400
1401 switch (event) {
1402 case SND_SOC_DAPM_PRE_PMU:
1403 /* Decide whether to switch the micbias for MBHC */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001404 if (w->reg == sitar->mbhc_bias_regs.ctl_reg) {
1405 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301406 sitar_codec_switch_micbias(codec, 0);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001407 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
1408 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301409
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001410 snd_soc_update_bits(codec, w->reg, 0x1E, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301411 sitar_codec_update_cfilt_usage(codec, cfilt_sel_val, 1);
1412
1413 if (strnstr(w->name, internal1_text, 30))
1414 snd_soc_update_bits(codec, micb_int_reg, 0xFF, 0xA4);
1415 else if (strnstr(w->name, internal2_text, 30))
1416 snd_soc_update_bits(codec, micb_int_reg, 0x1C, 0x1C);
1417 break;
1418 case SND_SOC_DAPM_POST_PMU:
1419 if (sitar->mbhc_polling_active &&
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -07001420 sitar->mbhc_cfg.micbias == micb_line) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001421 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301422 sitar_codec_pause_hs_polling(codec);
1423 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001424 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301425 }
1426 break;
1427 case SND_SOC_DAPM_POST_PMD:
1428
1429 if ((w->reg == sitar->mbhc_bias_regs.ctl_reg)
1430 && sitar_is_hph_pa_on(codec))
1431 sitar_codec_switch_micbias(codec, 1);
1432
1433 if (strnstr(w->name, internal1_text, 30))
1434 snd_soc_update_bits(codec, micb_int_reg, 0x80, 0x00);
1435 else if (strnstr(w->name, internal2_text, 30))
1436 snd_soc_update_bits(codec, micb_int_reg, 0x10, 0x00);
1437 sitar_codec_update_cfilt_usage(codec, cfilt_sel_val, 0);
1438 break;
1439 }
1440
1441 return 0;
1442}
1443
1444static int sitar_codec_enable_dec(struct snd_soc_dapm_widget *w,
1445 struct snd_kcontrol *kcontrol, int event)
1446{
1447 struct snd_soc_codec *codec = w->codec;
1448 u16 dec_reset_reg;
1449
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301450 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301451
1452 if (w->reg == SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL)
1453 dec_reset_reg = SITAR_A_CDC_CLK_TX_RESET_B1_CTL;
1454 else {
1455 pr_err("%s: Error, incorrect dec\n", __func__);
1456 return -EINVAL;
1457 }
1458
1459 switch (event) {
1460 case SND_SOC_DAPM_PRE_PMU:
1461 snd_soc_update_bits(codec, dec_reset_reg, 1 << w->shift,
1462 1 << w->shift);
1463 snd_soc_update_bits(codec, dec_reset_reg, 1 << w->shift, 0x0);
1464 break;
1465 }
1466 return 0;
1467}
1468
1469static int sitar_codec_reset_interpolator(struct snd_soc_dapm_widget *w,
1470 struct snd_kcontrol *kcontrol, int event)
1471{
1472 struct snd_soc_codec *codec = w->codec;
1473
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301474 pr_debug("%s %d %s\n", __func__, event, w->name);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301475
1476 switch (event) {
1477 case SND_SOC_DAPM_PRE_PMU:
1478 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_RX_RESET_CTL,
1479 1 << w->shift, 1 << w->shift);
1480 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_RX_RESET_CTL,
1481 1 << w->shift, 0x0);
1482 break;
1483 }
1484 return 0;
1485}
1486
1487static int sitar_codec_enable_ldo_h(struct snd_soc_dapm_widget *w,
1488 struct snd_kcontrol *kcontrol, int event)
1489{
1490 switch (event) {
1491 case SND_SOC_DAPM_POST_PMU:
1492 case SND_SOC_DAPM_POST_PMD:
1493 usleep_range(1000, 1000);
1494 pr_debug("LDO_H\n");
1495 break;
1496 }
1497 return 0;
1498}
1499
1500static void sitar_enable_rx_bias(struct snd_soc_codec *codec, u32 enable)
1501{
1502 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1503
1504 if (enable) {
1505 sitar->rx_bias_count++;
1506 if (sitar->rx_bias_count == 1)
1507 snd_soc_update_bits(codec, SITAR_A_RX_COM_BIAS,
1508 0x80, 0x80);
1509 } else {
1510 sitar->rx_bias_count--;
1511 if (!sitar->rx_bias_count)
1512 snd_soc_update_bits(codec, SITAR_A_RX_COM_BIAS,
1513 0x80, 0x00);
1514 }
1515}
1516
1517static int sitar_codec_enable_rx_bias(struct snd_soc_dapm_widget *w,
1518 struct snd_kcontrol *kcontrol, int event)
1519{
1520 struct snd_soc_codec *codec = w->codec;
1521
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301522 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301523
1524 switch (event) {
1525 case SND_SOC_DAPM_PRE_PMU:
1526 sitar_enable_rx_bias(codec, 1);
1527 break;
1528 case SND_SOC_DAPM_POST_PMD:
1529 sitar_enable_rx_bias(codec, 0);
1530 break;
1531 }
1532 return 0;
1533}
Bhalchandra Gajare66131712012-05-07 14:12:26 -07001534static int sitar_hph_dac_event(struct snd_soc_dapm_widget *w,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301535 struct snd_kcontrol *kcontrol, int event)
1536{
1537 struct snd_soc_codec *codec = w->codec;
1538
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301539 pr_debug("%s %s %d\n", __func__, w->name, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301540
1541 switch (event) {
1542 case SND_SOC_DAPM_PRE_PMU:
1543 snd_soc_update_bits(codec, w->reg, 0x40, 0x40);
1544 break;
1545 case SND_SOC_DAPM_POST_PMD:
1546 snd_soc_update_bits(codec, w->reg, 0x40, 0x00);
1547 break;
1548 }
1549 return 0;
1550}
1551
1552static void sitar_snd_soc_jack_report(struct sitar_priv *sitar,
1553 struct snd_soc_jack *jack, int status,
1554 int mask)
1555{
1556 /* XXX: wake_lock_timeout()? */
1557 snd_soc_jack_report(jack, status, mask);
1558}
1559
1560static void hphocp_off_report(struct sitar_priv *sitar,
1561 u32 jack_status, int irq)
1562{
1563 struct snd_soc_codec *codec;
1564
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001565 if (!sitar) {
1566 pr_err("%s: Bad sitar private data\n", __func__);
1567 return;
1568 }
1569
1570 pr_info("%s: clear ocp status %x\n", __func__, jack_status);
1571 codec = sitar->codec;
1572 if (sitar->hph_status & jack_status) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301573 sitar->hph_status &= ~jack_status;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001574 if (sitar->mbhc_cfg.headset_jack)
1575 sitar_snd_soc_jack_report(sitar,
1576 sitar->mbhc_cfg.headset_jack,
1577 sitar->hph_status,
1578 SITAR_JACK_MASK);
1579 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10, 0x00);
1580 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10, 0x10);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301581 /* reset retry counter as PA is turned off signifying
1582 * start of new OCP detection session
1583 */
1584 if (SITAR_IRQ_HPH_PA_OCPL_FAULT)
1585 sitar->hphlocp_cnt = 0;
1586 else
1587 sitar->hphrocp_cnt = 0;
1588 wcd9xxx_enable_irq(codec->control_data, irq);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301589 }
1590}
1591
1592static void hphlocp_off_report(struct work_struct *work)
1593{
1594 struct sitar_priv *sitar = container_of(work, struct sitar_priv,
1595 hphlocp_work);
1596 hphocp_off_report(sitar, SND_JACK_OC_HPHL, SITAR_IRQ_HPH_PA_OCPL_FAULT);
1597}
1598
1599static void hphrocp_off_report(struct work_struct *work)
1600{
1601 struct sitar_priv *sitar = container_of(work, struct sitar_priv,
1602 hphrocp_work);
1603 hphocp_off_report(sitar, SND_JACK_OC_HPHR, SITAR_IRQ_HPH_PA_OCPR_FAULT);
1604}
1605
1606static int sitar_hph_pa_event(struct snd_soc_dapm_widget *w,
1607 struct snd_kcontrol *kcontrol, int event)
1608{
1609 struct snd_soc_codec *codec = w->codec;
1610 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1611 u8 mbhc_micb_ctl_val;
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301612 pr_debug("%s: event = %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301613
1614 switch (event) {
1615 case SND_SOC_DAPM_PRE_PMU:
1616 mbhc_micb_ctl_val = snd_soc_read(codec,
1617 sitar->mbhc_bias_regs.ctl_reg);
1618
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001619 if (!(mbhc_micb_ctl_val & 0x80)) {
1620 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301621 sitar_codec_switch_micbias(codec, 1);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001622 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
1623 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301624
1625 break;
1626
1627 case SND_SOC_DAPM_POST_PMD:
1628 /* schedule work is required because at the time HPH PA DAPM
1629 * event callback is called by DAPM framework, CODEC dapm mutex
1630 * would have been locked while snd_soc_jack_report also
1631 * attempts to acquire same lock.
1632 */
1633 if (w->shift == 5) {
1634 clear_bit(SITAR_HPHL_PA_OFF_ACK,
1635 &sitar->hph_pa_dac_state);
1636 clear_bit(SITAR_HPHL_DAC_OFF_ACK,
1637 &sitar->hph_pa_dac_state);
1638 if (sitar->hph_status & SND_JACK_OC_HPHL)
1639 schedule_work(&sitar->hphlocp_work);
1640 } else if (w->shift == 4) {
1641 clear_bit(SITAR_HPHR_PA_OFF_ACK,
1642 &sitar->hph_pa_dac_state);
1643 clear_bit(SITAR_HPHR_DAC_OFF_ACK,
1644 &sitar->hph_pa_dac_state);
1645 if (sitar->hph_status & SND_JACK_OC_HPHR)
1646 schedule_work(&sitar->hphrocp_work);
1647 }
1648
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001649 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
1650 sitar_codec_switch_micbias(codec, 0);
1651 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301652
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301653 pr_debug("%s: sleep 10 ms after %s PA disable.\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301654 w->name);
1655 usleep_range(10000, 10000);
1656
1657 break;
1658 }
1659 return 0;
1660}
1661
1662static void sitar_get_mbhc_micbias_regs(struct snd_soc_codec *codec,
1663 struct mbhc_micbias_regs *micbias_regs)
1664{
1665 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301666 unsigned int cfilt;
1667
Patrick Laia5062da2012-05-11 17:55:09 -07001668 switch (sitar->mbhc_cfg.micbias) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301669 case SITAR_MICBIAS1:
1670 cfilt = sitar->pdata->micbias.bias1_cfilt_sel;
1671 micbias_regs->mbhc_reg = SITAR_A_MICB_1_MBHC;
1672 micbias_regs->int_rbias = SITAR_A_MICB_1_INT_RBIAS;
1673 micbias_regs->ctl_reg = SITAR_A_MICB_1_CTL;
1674 break;
1675 case SITAR_MICBIAS2:
1676 cfilt = sitar->pdata->micbias.bias2_cfilt_sel;
1677 micbias_regs->mbhc_reg = SITAR_A_MICB_2_MBHC;
1678 micbias_regs->int_rbias = SITAR_A_MICB_2_INT_RBIAS;
1679 micbias_regs->ctl_reg = SITAR_A_MICB_2_CTL;
1680 break;
1681 default:
1682 /* Should never reach here */
1683 pr_err("%s: Invalid MIC BIAS for MBHC\n", __func__);
1684 return;
1685 }
1686
1687 micbias_regs->cfilt_sel = cfilt;
1688
1689 switch (cfilt) {
1690 case SITAR_CFILT1_SEL:
1691 micbias_regs->cfilt_val = SITAR_A_MICB_CFILT_1_VAL;
1692 micbias_regs->cfilt_ctl = SITAR_A_MICB_CFILT_1_CTL;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001693 sitar->mbhc_data.micb_mv = sitar->pdata->micbias.cfilt1_mv;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301694 break;
1695 case SITAR_CFILT2_SEL:
1696 micbias_regs->cfilt_val = SITAR_A_MICB_CFILT_2_VAL;
1697 micbias_regs->cfilt_ctl = SITAR_A_MICB_CFILT_2_CTL;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001698 sitar->mbhc_data.micb_mv = sitar->pdata->micbias.cfilt2_mv;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301699 break;
1700 }
1701}
1702
1703static int sitar_codec_enable_charge_pump(struct snd_soc_dapm_widget *w,
1704 struct snd_kcontrol *kcontrol, int event)
1705{
1706 struct snd_soc_codec *codec = w->codec;
1707
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301708 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301709 switch (event) {
1710 case SND_SOC_DAPM_POST_PMU:
Bhalchandra Gajare2776af12012-04-27 16:59:39 -07001711 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_RESET_CTL, 0x10,
1712 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301713 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_CTL, 0x01,
1714 0x01);
1715 snd_soc_update_bits(codec, SITAR_A_CDC_CLSG_CTL, 0x08, 0x08);
1716 usleep_range(200, 200);
1717 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x10, 0x00);
1718 break;
1719 case SND_SOC_DAPM_PRE_PMD:
1720 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_RESET_CTL, 0x10,
1721 0x10);
1722 usleep_range(20, 20);
1723 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x08, 0x08);
1724 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x10, 0x10);
1725 snd_soc_update_bits(codec, SITAR_A_CDC_CLSG_CTL, 0x08, 0x00);
1726 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_CTL, 0x01,
1727 0x00);
1728 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x08, 0x00);
1729 break;
1730 }
1731 return 0;
1732}
1733
1734static const struct snd_soc_dapm_widget sitar_dapm_i2s_widgets[] = {
1735 SND_SOC_DAPM_SUPPLY("RX_I2S_CLK", SITAR_A_CDC_CLK_RX_I2S_CTL,
1736 4, 0, NULL, 0),
1737 SND_SOC_DAPM_SUPPLY("TX_I2S_CLK", SITAR_A_CDC_CLK_TX_I2S_CTL, 4,
1738 0, NULL, 0),
1739};
1740
1741static const struct snd_soc_dapm_widget sitar_dapm_widgets[] = {
1742 /*RX stuff */
1743 SND_SOC_DAPM_OUTPUT("EAR"),
1744
1745 SND_SOC_DAPM_PGA("EAR PA", SITAR_A_RX_EAR_EN, 4, 0, NULL, 0),
1746 SND_SOC_DAPM_MIXER("DAC1", SITAR_A_RX_EAR_EN, 6, 0, dac1_switch,
1747 ARRAY_SIZE(dac1_switch)),
1748 SND_SOC_DAPM_SUPPLY("EAR DRIVER", SITAR_A_RX_EAR_EN, 3, 0, NULL, 0),
1749 SND_SOC_DAPM_AIF_IN_E("SLIM RX1", "AIF1 Playback", 0, SND_SOC_NOPM, 0,
1750 0, sitar_codec_enable_slimrx,
1751 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1752 SND_SOC_DAPM_AIF_IN_E("SLIM RX2", "AIF1 Playback", 0, SND_SOC_NOPM, 0,
1753 0, sitar_codec_enable_slimrx,
1754 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1755 SND_SOC_DAPM_AIF_IN("SLIM RX3", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1756 SND_SOC_DAPM_AIF_IN("SLIM RX4", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1757 SND_SOC_DAPM_AIF_IN("SLIM RX5", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1758
1759 /* Headphone */
1760 SND_SOC_DAPM_OUTPUT("HEADPHONE"),
1761 SND_SOC_DAPM_PGA_E("HPHL", SITAR_A_RX_HPH_CNP_EN, 5, 0, NULL, 0,
1762 sitar_hph_pa_event, SND_SOC_DAPM_PRE_PMU |
1763 SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301764
1765 SND_SOC_DAPM_PGA_E("HPHR", SITAR_A_RX_HPH_CNP_EN, 4, 0, NULL, 0,
1766 sitar_hph_pa_event, SND_SOC_DAPM_PRE_PMU |
1767 SND_SOC_DAPM_POST_PMD),
1768
Bhalchandra Gajare66131712012-05-07 14:12:26 -07001769 SND_SOC_DAPM_DAC_E("HPHL DAC", NULL, SITAR_A_RX_HPH_L_DAC_CTL, 7, 0,
1770 sitar_hph_dac_event,
1771 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301772 SND_SOC_DAPM_DAC_E("HPHR DAC", NULL, SITAR_A_RX_HPH_R_DAC_CTL, 7, 0,
Bhalchandra Gajare66131712012-05-07 14:12:26 -07001773 sitar_hph_dac_event,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301774 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1775
1776 /* Speaker */
1777 SND_SOC_DAPM_OUTPUT("LINEOUT1"),
1778 SND_SOC_DAPM_OUTPUT("LINEOUT2"),
1779
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001780 SND_SOC_DAPM_DAC_E("LINEOUT1 DAC", NULL, SITAR_A_RX_LINE_1_DAC_CTL, 7, 0
1781 , sitar_lineout_dac_event,
1782 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1783 SND_SOC_DAPM_DAC_E("LINEOUT2 DAC", NULL, SITAR_A_RX_LINE_2_DAC_CTL, 7, 0
1784 , sitar_lineout_dac_event,
1785 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1786
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301787 SND_SOC_DAPM_PGA_E("LINEOUT1 PA", SITAR_A_RX_LINE_CNP_EN, 0, 0, NULL,
1788 0, sitar_codec_enable_lineout, SND_SOC_DAPM_PRE_PMU |
1789 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1790 SND_SOC_DAPM_PGA_E("LINEOUT2 PA", SITAR_A_RX_LINE_CNP_EN, 1, 0, NULL,
1791 0, sitar_codec_enable_lineout, SND_SOC_DAPM_PRE_PMU |
1792 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1793
1794 SND_SOC_DAPM_MIXER_E("RX1 MIX1", SITAR_A_CDC_CLK_RX_B1_CTL, 0, 0, NULL,
1795 0, sitar_codec_reset_interpolator, SND_SOC_DAPM_PRE_PMU),
1796 SND_SOC_DAPM_MIXER_E("RX2 MIX1", SITAR_A_CDC_CLK_RX_B1_CTL, 1, 0, NULL,
1797 0, sitar_codec_reset_interpolator, SND_SOC_DAPM_PRE_PMU),
1798 SND_SOC_DAPM_MIXER_E("RX3 MIX1", SITAR_A_CDC_CLK_RX_B1_CTL, 2, 0, NULL,
1799 0, sitar_codec_reset_interpolator, SND_SOC_DAPM_PRE_PMU),
1800
1801 SND_SOC_DAPM_MUX("DAC1 MUX", SND_SOC_NOPM, 0, 0,
1802 &rx_dac1_mux),
1803 SND_SOC_DAPM_MUX("DAC2 MUX", SND_SOC_NOPM, 0, 0,
1804 &rx_dac2_mux),
1805 SND_SOC_DAPM_MUX("DAC3 MUX", SND_SOC_NOPM, 0, 0,
1806 &rx_dac3_mux),
1807 SND_SOC_DAPM_MUX("DAC4 MUX", SND_SOC_NOPM, 0, 0,
1808 &rx_dac4_mux),
1809
1810 SND_SOC_DAPM_MIXER("RX1 CHAIN", SITAR_A_CDC_RX1_B6_CTL, 5, 0, NULL, 0),
1811 SND_SOC_DAPM_MIXER("RX2 CHAIN", SITAR_A_CDC_RX2_B6_CTL, 5, 0, NULL, 0),
1812 SND_SOC_DAPM_MIXER("RX3 CHAIN", SITAR_A_CDC_RX3_B6_CTL, 5, 0, NULL, 0),
1813
1814 SND_SOC_DAPM_MUX("RX1 MIX1 INP1", SND_SOC_NOPM, 0, 0,
1815 &rx_mix1_inp1_mux),
1816 SND_SOC_DAPM_MUX("RX1 MIX1 INP2", SND_SOC_NOPM, 0, 0,
1817 &rx_mix1_inp2_mux),
1818 SND_SOC_DAPM_MUX("RX2 MIX1 INP1", SND_SOC_NOPM, 0, 0,
1819 &rx2_mix1_inp1_mux),
1820 SND_SOC_DAPM_MUX("RX2 MIX1 INP2", SND_SOC_NOPM, 0, 0,
1821 &rx2_mix1_inp2_mux),
1822 SND_SOC_DAPM_MUX("RX3 MIX1 INP1", SND_SOC_NOPM, 0, 0,
1823 &rx3_mix1_inp1_mux),
1824 SND_SOC_DAPM_MUX("RX3 MIX1 INP2", SND_SOC_NOPM, 0, 0,
1825 &rx3_mix1_inp2_mux),
1826
1827 SND_SOC_DAPM_SUPPLY("CP", SITAR_A_CP_EN, 0, 0,
1828 sitar_codec_enable_charge_pump, SND_SOC_DAPM_POST_PMU |
1829 SND_SOC_DAPM_PRE_PMD),
1830 SND_SOC_DAPM_SUPPLY("RX_BIAS", SND_SOC_NOPM, 0, 0,
1831 sitar_codec_enable_rx_bias, SND_SOC_DAPM_PRE_PMU |
1832 SND_SOC_DAPM_POST_PMD),
1833
1834 SND_SOC_DAPM_SUPPLY("LDO_H", SITAR_A_LDO_H_MODE_1, 7, 0,
1835 sitar_codec_enable_ldo_h, SND_SOC_DAPM_POST_PMU),
1836 /* TX */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001837
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301838 SND_SOC_DAPM_SUPPLY("CDC_CONN", SITAR_A_CDC_CLK_OTHR_CTL, 2, 0, NULL,
1839 0),
1840 SND_SOC_DAPM_INPUT("AMIC1"),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001841 SND_SOC_DAPM_INPUT("AMIC2"),
1842 SND_SOC_DAPM_INPUT("AMIC3"),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301843 SND_SOC_DAPM_MICBIAS_E("MIC BIAS1 External", SITAR_A_MICB_1_CTL, 7, 0,
1844 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1845 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1846 SND_SOC_DAPM_MICBIAS_E("MIC BIAS1 Internal1", SITAR_A_MICB_1_CTL, 7, 0,
1847 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1848 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1849
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301850 SND_SOC_DAPM_MICBIAS_E("MIC BIAS2 External", SITAR_A_MICB_2_CTL, 7, 0,
1851 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1852 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1853 SND_SOC_DAPM_MICBIAS_E("MIC BIAS2 Internal1", SITAR_A_MICB_2_CTL, 7, 0,
1854 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1855 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1856 SND_SOC_DAPM_MICBIAS_E("MIC BIAS2 Internal2", SITAR_A_MICB_2_CTL, 7, 0,
1857 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1858 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1859
1860 SND_SOC_DAPM_ADC_E("ADC1", NULL, SITAR_A_TX_1_2_EN, 7, 0,
1861 sitar_codec_enable_adc, SND_SOC_DAPM_PRE_PMU |
1862 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1863 SND_SOC_DAPM_ADC_E("ADC2", NULL, SITAR_A_TX_1_2_EN, 3, 0,
1864 sitar_codec_enable_adc, SND_SOC_DAPM_PRE_PMU |
1865 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001866 SND_SOC_DAPM_ADC_E("ADC3", NULL, SITAR_A_TX_3_EN, 7, 0,
1867 sitar_codec_enable_adc, SND_SOC_DAPM_PRE_PMU |
1868 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301869
1870 SND_SOC_DAPM_MUX_E("DEC1 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 0, 0,
1871 &dec1_mux, sitar_codec_enable_dec, SND_SOC_DAPM_PRE_PMU),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001872 SND_SOC_DAPM_MUX_E("DEC2 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 1, 0,
1873 &dec2_mux, sitar_codec_enable_dec, SND_SOC_DAPM_PRE_PMU),
1874 SND_SOC_DAPM_MUX_E("DEC3 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 2, 0,
1875 &dec3_mux, sitar_codec_enable_dec, SND_SOC_DAPM_PRE_PMU),
1876 SND_SOC_DAPM_MUX_E("DEC4 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 3, 0,
1877 &dec4_mux, sitar_codec_enable_dec, SND_SOC_DAPM_PRE_PMU),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301878
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07001879 SND_SOC_DAPM_MUX("ANC1 MUX", SND_SOC_NOPM, 0, 0, &anc1_mux),
1880 SND_SOC_DAPM_MUX("ANC2 MUX", SND_SOC_NOPM, 0, 0, &anc2_mux),
1881
1882 SND_SOC_DAPM_MIXER_E("ANC", SND_SOC_NOPM, 0, 0, NULL, 0,
1883 sitar_codec_enable_anc, SND_SOC_DAPM_PRE_PMU |
1884 SND_SOC_DAPM_POST_PMD),
1885
1886 SND_SOC_DAPM_MUX("ANC1 FB MUX", SND_SOC_NOPM, 0, 0, &anc1_fb_mux),
1887
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301888 SND_SOC_DAPM_MUX("SLIM TX1 MUX", SND_SOC_NOPM, 0, 0, &sb_tx1_mux),
1889 SND_SOC_DAPM_MUX("SLIM TX2 MUX", SND_SOC_NOPM, 0, 0, &sb_tx2_mux),
1890 SND_SOC_DAPM_MUX("SLIM TX3 MUX", SND_SOC_NOPM, 0, 0, &sb_tx3_mux),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001891 SND_SOC_DAPM_MUX("SLIM TX4 MUX", SND_SOC_NOPM, 0, 0, &sb_tx4_mux),
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07001892 SND_SOC_DAPM_MUX("SLIM TX5 MUX", SND_SOC_NOPM, 0, 0, &sb_tx5_mux),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301893
1894 SND_SOC_DAPM_AIF_OUT_E("SLIM TX1", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
1895 0, sitar_codec_enable_slimtx,
1896 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1897
1898 SND_SOC_DAPM_AIF_OUT_E("SLIM TX2", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
1899 0, sitar_codec_enable_slimtx,
1900 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1901
1902 SND_SOC_DAPM_AIF_OUT_E("SLIM TX3", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
1903 0, sitar_codec_enable_slimtx,
1904 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001905 SND_SOC_DAPM_AIF_OUT_E("SLIM TX4", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
1906 0, sitar_codec_enable_slimtx,
1907 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301908
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07001909 SND_SOC_DAPM_AIF_OUT_E("SLIM TX5", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
1910 0, sitar_codec_enable_slimtx,
1911 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1912
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301913 /* Digital Mic Inputs */
1914 SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
1915 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
1916 SND_SOC_DAPM_POST_PMD),
1917 SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 0, 0,
1918 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
1919 SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301920 SND_SOC_DAPM_ADC_E("DMIC3", NULL, SND_SOC_NOPM, 0, 0,
1921 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
1922 SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301923 SND_SOC_DAPM_ADC_E("DMIC4", NULL, SND_SOC_NOPM, 0, 0,
1924 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
1925 SND_SOC_DAPM_POST_PMD),
1926
1927 /* Sidetone */
1928 SND_SOC_DAPM_MUX("IIR1 INP1 MUX", SND_SOC_NOPM, 0, 0, &iir1_inp1_mux),
1929 SND_SOC_DAPM_PGA("IIR1", SITAR_A_CDC_CLK_SD_CTL, 0, 0, NULL, 0),
1930
1931};
1932
1933static const struct snd_soc_dapm_route audio_map[] = {
1934 /* Earpiece (RX MIX1) */
1935 {"EAR", NULL, "EAR PA"},
1936 {"EAR PA", "NULL", "DAC1"},
1937 {"DAC1", "Switch", "DAC1 MUX"},
1938 {"DAC1", NULL, "CP"},
1939 {"DAC1", NULL, "EAR DRIVER"},
1940
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001941 {"CP", NULL, "RX_BIAS"},
1942
1943 {"LINEOUT1 DAC", NULL, "RX_BIAS"},
1944 {"LINEOUT2 DAC", NULL, "RX_BIAS"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301945
1946 {"LINEOUT2", NULL, "LINEOUT2 PA"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001947 {"LINEOUT2 PA", NULL, "LINEOUT2 DAC"},
1948 {"LINEOUT2 DAC", NULL, "DAC3 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301949
1950 {"LINEOUT1", NULL, "LINEOUT1 PA"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001951 {"LINEOUT1 PA", NULL, "LINEOUT1 DAC"},
1952 {"LINEOUT1 DAC", NULL, "DAC2 MUX"},
1953
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07001954 {"ANC1 FB MUX", "EAR_HPH_L", "RX2 MIX1"},
1955 {"ANC1 FB MUX", "EAR_LINE_1", "RX3 MIX1"},
1956 {"ANC", NULL, "ANC1 FB MUX"},
1957
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301958
1959 /* Headset (RX MIX1 and RX MIX2) */
1960 {"HEADPHONE", NULL, "HPHL"},
1961 {"HEADPHONE", NULL, "HPHR"},
1962
Patrick Lai9b250b72012-05-24 14:45:57 -07001963 {"HPHL DAC", NULL, "CP"},
1964 {"HPHR DAC", NULL, "CP"},
1965
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301966 {"HPHL", NULL, "HPHL DAC"},
Bhalchandra Gajare66131712012-05-07 14:12:26 -07001967 {"HPHL DAC", "NULL", "DAC4 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301968 {"HPHR", NULL, "HPHR DAC"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001969 {"HPHR DAC", NULL, "RX3 MIX1"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301970
1971 {"DAC1 MUX", "RX1", "RX1 CHAIN"},
1972 {"DAC2 MUX", "RX1", "RX1 CHAIN"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001973
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301974 {"DAC3 MUX", "RX1", "RX1 CHAIN"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001975 {"DAC3 MUX", "INV_RX1", "RX1 CHAIN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301976 {"DAC3 MUX", "RX2", "RX2 MIX1"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001977
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301978 {"DAC4 MUX", "ON", "RX2 MIX1"},
1979
1980 {"RX1 CHAIN", NULL, "RX1 MIX1"},
1981
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301982 {"RX1 MIX1", NULL, "RX1 MIX1 INP1"},
1983 {"RX1 MIX1", NULL, "RX1 MIX1 INP2"},
1984 {"RX2 MIX1", NULL, "RX2 MIX1 INP1"},
1985 {"RX2 MIX1", NULL, "RX2 MIX1 INP2"},
1986 {"RX3 MIX1", NULL, "RX3 MIX1 INP1"},
1987 {"RX3 MIX1", NULL, "RX3 MIX1 INP2"},
1988
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07001989 /* ANC */
1990 {"ANC", NULL, "ANC1 MUX"},
1991 {"ANC", NULL, "ANC2 MUX"},
1992 {"ANC1 MUX", "ADC1", "ADC1"},
1993 {"ANC1 MUX", "ADC2", "ADC2"},
1994 {"ANC1 MUX", "ADC3", "ADC3"},
1995 {"ANC2 MUX", "ADC1", "ADC1"},
1996 {"ANC2 MUX", "ADC2", "ADC2"},
1997 {"ANC2 MUX", "ADC3", "ADC3"},
1998
1999 {"ANC", NULL, "CDC_CONN"},
2000
2001 {"RX2 MIX1", NULL, "ANC"},
2002 {"RX3 MIX1", NULL, "ANC"},
2003
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302004 /* SLIMBUS Connections */
2005
2006 /* Slimbus port 5 is non functional in Sitar 1.0 */
2007 {"RX1 MIX1 INP1", "RX1", "SLIM RX1"},
2008 {"RX1 MIX1 INP1", "RX2", "SLIM RX2"},
2009 {"RX1 MIX1 INP1", "RX3", "SLIM RX3"},
2010 {"RX1 MIX1 INP1", "RX4", "SLIM RX4"},
2011 {"RX1 MIX1 INP1", "IIR1", "IIR1"},
2012 {"RX1 MIX1 INP2", "RX1", "SLIM RX1"},
2013 {"RX1 MIX1 INP2", "RX2", "SLIM RX2"},
2014 {"RX1 MIX1 INP2", "RX3", "SLIM RX3"},
2015 {"RX1 MIX1 INP2", "RX4", "SLIM RX4"},
2016 {"RX1 MIX1 INP2", "IIR1", "IIR1"},
2017 {"RX2 MIX1 INP1", "RX1", "SLIM RX1"},
2018 {"RX2 MIX1 INP1", "RX2", "SLIM RX2"},
2019 {"RX2 MIX1 INP1", "RX3", "SLIM RX3"},
2020 {"RX2 MIX1 INP1", "RX4", "SLIM RX4"},
2021 {"RX2 MIX1 INP1", "IIR1", "IIR1"},
2022 {"RX2 MIX1 INP2", "RX1", "SLIM RX1"},
2023 {"RX2 MIX1 INP2", "RX2", "SLIM RX2"},
2024 {"RX2 MIX1 INP2", "RX3", "SLIM RX3"},
2025 {"RX2 MIX1 INP2", "RX4", "SLIM RX4"},
2026 {"RX2 MIX1 INP2", "IIR1", "IIR1"},
2027 {"RX3 MIX1 INP1", "RX1", "SLIM RX1"},
2028 {"RX3 MIX1 INP1", "RX2", "SLIM RX2"},
2029 {"RX3 MIX1 INP1", "RX3", "SLIM RX3"},
2030 {"RX3 MIX1 INP1", "RX4", "SLIM RX4"},
2031 {"RX3 MIX1 INP1", "IIR1", "IIR1"},
2032 {"RX3 MIX1 INP2", "RX1", "SLIM RX1"},
2033 {"RX3 MIX1 INP2", "RX2", "SLIM RX2"},
2034 {"RX3 MIX1 INP2", "RX3", "SLIM RX3"},
2035 {"RX3 MIX1 INP2", "RX4", "SLIM RX4"},
2036 {"RX3 MIX1 INP2", "IIR1", "IIR1"},
2037
2038
2039 /* TX */
2040 {"SLIM TX1", NULL, "SLIM TX1 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302041 {"SLIM TX2", NULL, "SLIM TX2 MUX"},
2042 {"SLIM TX3", NULL, "SLIM TX3 MUX"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002043 {"SLIM TX4", NULL, "SLIM TX4 MUX"},
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002044 {"SLIM TX5", NULL, "SLIM TX5 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302045
2046 {"SLIM TX1 MUX", "DEC1", "DEC1 MUX"},
2047 {"SLIM TX2 MUX", "DEC2", "DEC2 MUX"},
2048 {"SLIM TX3 MUX", "DEC3", "DEC3 MUX"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002049 {"SLIM TX4 MUX", "DEC4", "DEC4 MUX"},
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002050 {"SLIM TX5 MUX", "DEC1", "DEC1 MUX"},
2051 {"SLIM TX5 MUX", "DEC2", "DEC2 MUX"},
2052 {"SLIM TX5 MUX", "DEC3", "DEC3 MUX"},
2053 {"SLIM TX5 MUX", "DEC4", "DEC4 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302054
2055 /* Decimator Inputs */
2056 {"DEC1 MUX", "DMIC1", "DMIC1"},
2057 {"DEC1 MUX", "DMIC4", "DMIC4"},
2058 {"DEC1 MUX", "ADC1", "ADC1"},
2059 {"DEC1 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002060 {"DEC1 MUX", "ADC3", "ADC3"},
2061 {"DEC1 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302062 {"DEC2 MUX", "DMIC2", "DMIC2"},
2063 {"DEC2 MUX", "DMIC3", "DMIC3"},
2064 {"DEC2 MUX", "ADC1", "ADC1"},
2065 {"DEC2 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002066 {"DEC2 MUX", "ADC3", "ADC3"},
2067 {"DEC2 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302068 {"DEC3 MUX", "DMIC3", "DMIC3"},
2069 {"DEC3 MUX", "ADC1", "ADC1"},
2070 {"DEC3 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002071 {"DEC3 MUX", "ADC3", "ADC3"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302072 {"DEC3 MUX", "DMIC2", "DMIC2"},
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -07002073 {"DEC3 MUX", "DMIC4", "DMIC4"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002074 {"DEC3 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302075 {"DEC4 MUX", "DMIC4", "DMIC4"},
2076 {"DEC4 MUX", "ADC1", "ADC1"},
2077 {"DEC4 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002078 {"DEC4 MUX", "ADC3", "ADC3"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302079 {"DEC4 MUX", "DMIC3", "DMIC3"},
2080 {"DEC4 MUX", "DMIC2", "DMIC2"},
2081 {"DEC4 MUX", "DMIC1", "DMIC1"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002082 {"DEC4 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302083
2084 /* ADC Connections */
2085 {"ADC1", NULL, "AMIC1"},
2086 {"ADC2", NULL, "AMIC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002087 {"ADC3", NULL, "AMIC3"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302088
2089 /* IIR */
2090 {"IIR1", NULL, "IIR1 INP1 MUX"},
2091 {"IIR1 INP1 MUX", "DEC1", "DEC1 MUX"},
2092 {"MIC BIAS1 Internal1", NULL, "LDO_H"},
2093 {"MIC BIAS1 External", NULL, "LDO_H"},
2094 {"MIC BIAS2 Internal1", NULL, "LDO_H"},
2095 {"MIC BIAS2 External", NULL, "LDO_H"},
2096};
2097
2098static int sitar_readable(struct snd_soc_codec *ssc, unsigned int reg)
2099{
2100 return sitar_reg_readable[reg];
2101}
2102
2103static int sitar_volatile(struct snd_soc_codec *ssc, unsigned int reg)
2104{
2105 /* Registers lower than 0x100 are top level registers which can be
2106 * written by the Sitar core driver.
2107 */
2108
2109 if ((reg >= SITAR_A_CDC_MBHC_EN_CTL) || (reg < 0x100))
2110 return 1;
2111
2112 /* IIR Coeff registers are not cacheable */
2113 if ((reg >= SITAR_A_CDC_IIR1_COEF_B1_CTL) &&
2114 (reg <= SITAR_A_CDC_IIR1_COEF_B5_CTL))
2115 return 1;
2116
2117 return 0;
2118}
2119
2120#define SITAR_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
2121static int sitar_write(struct snd_soc_codec *codec, unsigned int reg,
2122 unsigned int value)
2123{
2124 int ret;
2125
2126 BUG_ON(reg > SITAR_MAX_REGISTER);
2127
2128 if (!sitar_volatile(codec, reg)) {
2129 ret = snd_soc_cache_write(codec, reg, value);
2130 if (ret != 0)
2131 dev_err(codec->dev, "Cache write to %x failed: %d\n",
2132 reg, ret);
2133 }
2134
2135 return wcd9xxx_reg_write(codec->control_data, reg, value);
2136}
2137static unsigned int sitar_read(struct snd_soc_codec *codec,
2138 unsigned int reg)
2139{
2140 unsigned int val;
2141 int ret;
2142
2143 BUG_ON(reg > SITAR_MAX_REGISTER);
2144
2145 if (!sitar_volatile(codec, reg) && sitar_readable(codec, reg) &&
2146 reg < codec->driver->reg_cache_size) {
2147 ret = snd_soc_cache_read(codec, reg, &val);
2148 if (ret >= 0) {
2149 return val;
2150 } else
2151 dev_err(codec->dev, "Cache read from %x failed: %d\n",
2152 reg, ret);
2153 }
2154
2155 val = wcd9xxx_reg_read(codec->control_data, reg);
2156 return val;
2157}
2158
2159static void sitar_codec_enable_audio_mode_bandgap(struct snd_soc_codec *codec)
2160{
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002161 struct wcd9xxx *sitar_core = dev_get_drvdata(codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302162
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002163 if (SITAR_IS_1P0(sitar_core->version))
2164 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1, 0x80, 0x80);
2165
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002166 snd_soc_update_bits(codec, SITAR_A_BIAS_CURR_CTL_2, 0x0C, 0x08);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302167 usleep_range(1000, 1000);
2168 snd_soc_write(codec, SITAR_A_BIAS_REF_CTL, 0x1C);
2169 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2170 0x80);
2171 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x04,
2172 0x04);
2173 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x01,
2174 0x01);
2175 usleep_range(1000, 1000);
2176 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2177 0x00);
2178}
2179
2180static void sitar_codec_enable_bandgap(struct snd_soc_codec *codec,
2181 enum sitar_bandgap_type choice)
2182{
2183 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002184 struct wcd9xxx *sitar_core = dev_get_drvdata(codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302185
2186 /* TODO lock resources accessed by audio streams and threaded
2187 * interrupt handlers
2188 */
2189
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302190 pr_debug("%s, choice is %d, current is %d\n", __func__, choice,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302191 sitar->bandgap_type);
2192
2193 if (sitar->bandgap_type == choice)
2194 return;
2195
2196 if ((sitar->bandgap_type == SITAR_BANDGAP_OFF) &&
2197 (choice == SITAR_BANDGAP_AUDIO_MODE)) {
2198 sitar_codec_enable_audio_mode_bandgap(codec);
2199 } else if (choice == SITAR_BANDGAP_MBHC_MODE) {
Bhalchandra Gajare7708ee32012-05-24 17:37:37 -07002200 snd_soc_update_bits(codec, SITAR_A_BIAS_CURR_CTL_2, 0x0C, 0x08);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302201 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x2,
2202 0x2);
2203 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2204 0x80);
2205 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x4,
2206 0x4);
2207 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x01,
2208 0x1);
2209 usleep_range(1000, 1000);
2210 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2211 0x00);
2212 } else if ((sitar->bandgap_type == SITAR_BANDGAP_MBHC_MODE) &&
2213 (choice == SITAR_BANDGAP_AUDIO_MODE)) {
2214 snd_soc_write(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x50);
2215 usleep_range(100, 100);
2216 sitar_codec_enable_audio_mode_bandgap(codec);
2217 } else if (choice == SITAR_BANDGAP_OFF) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002218 snd_soc_update_bits(codec, SITAR_A_BIAS_CURR_CTL_2, 0x0C, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302219 snd_soc_write(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x50);
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002220 if (SITAR_IS_1P0(sitar_core->version))
2221 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1,
2222 0xFF, 0x65);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002223 usleep_range(1000, 1000);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302224 } else {
2225 pr_err("%s: Error, Invalid bandgap settings\n", __func__);
2226 }
2227 sitar->bandgap_type = choice;
2228}
2229
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002230static int sitar_codec_enable_config_mode(struct snd_soc_codec *codec,
2231 int enable)
2232{
2233 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2234
2235 if (enable) {
2236 snd_soc_update_bits(codec, SITAR_A_RC_OSC_FREQ, 0x10, 0);
2237 snd_soc_write(codec, SITAR_A_BIAS_OSC_BG_CTL, 0x17);
2238 usleep_range(5, 5);
2239 snd_soc_update_bits(codec, SITAR_A_RC_OSC_FREQ, 0x80,
2240 0x80);
2241 snd_soc_update_bits(codec, SITAR_A_RC_OSC_TEST, 0x80,
2242 0x80);
2243 usleep_range(10, 10);
2244 snd_soc_update_bits(codec, SITAR_A_RC_OSC_TEST, 0x80, 0);
2245 usleep_range(20, 20);
2246 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x08, 0x08);
2247 } else {
2248 snd_soc_update_bits(codec, SITAR_A_BIAS_OSC_BG_CTL, 0x1,
2249 0);
2250 snd_soc_update_bits(codec, SITAR_A_RC_OSC_FREQ, 0x80, 0);
2251 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x08, 0x00);
2252 }
2253 sitar->config_mode_active = enable ? true : false;
2254
2255 return 0;
2256}
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302257
2258static int sitar_codec_enable_clock_block(struct snd_soc_codec *codec,
2259 int config_mode)
2260{
2261 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2262
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302263 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302264
2265 if (config_mode) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002266 sitar_codec_enable_config_mode(codec, 1);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302267 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN2, 0x00);
2268 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN2, 0x02);
2269 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN1, 0x0D);
2270 usleep_range(1000, 1000);
2271 } else
2272 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x08, 0x00);
2273
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002274 if (!config_mode && sitar->mbhc_polling_active) {
2275 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN2, 0x02);
2276 sitar_codec_enable_config_mode(codec, 0);
2277
2278 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302279
2280 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x05, 0x05);
2281 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x02, 0x00);
2282 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x04, 0x04);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302283 usleep_range(50, 50);
2284 sitar->clock_active = true;
2285 return 0;
2286}
2287static void sitar_codec_disable_clock_block(struct snd_soc_codec *codec)
2288{
2289 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302290 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302291 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x04, 0x00);
2292 ndelay(160);
2293 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x02, 0x02);
2294 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x05, 0x00);
2295 sitar->clock_active = false;
2296}
2297
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002298static int sitar_codec_mclk_index(const struct sitar_priv *sitar)
2299{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002300 if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_12288KHZ)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002301 return 0;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002302 else if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_9600KHZ)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002303 return 1;
2304 else {
2305 BUG_ON(1);
2306 return -EINVAL;
2307 }
2308}
2309
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302310static void sitar_codec_calibrate_hs_polling(struct snd_soc_codec *codec)
2311{
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002312 u8 *n_ready, *n_cic;
2313 struct sitar_mbhc_btn_detect_cfg *btn_det;
2314 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302315
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002316 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(sitar->mbhc_cfg.calibration);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302317
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002318 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B1_CTL,
2319 sitar->mbhc_data.v_ins_hu & 0xFF);
2320 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B2_CTL,
2321 (sitar->mbhc_data.v_ins_hu >> 8) & 0xFF);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302322
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002323 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B3_CTL,
2324 sitar->mbhc_data.v_b1_hu & 0xFF);
2325 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B4_CTL,
2326 (sitar->mbhc_data.v_b1_hu >> 8) & 0xFF);
2327
2328 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B5_CTL,
2329 sitar->mbhc_data.v_b1_h & 0xFF);
2330 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B6_CTL,
2331 (sitar->mbhc_data.v_b1_h >> 8) & 0xFF);
2332
2333 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B9_CTL,
2334 sitar->mbhc_data.v_brh & 0xFF);
2335 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B10_CTL,
2336 (sitar->mbhc_data.v_brh >> 8) & 0xFF);
2337
2338 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B11_CTL,
2339 sitar->mbhc_data.v_brl & 0xFF);
2340 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B12_CTL,
2341 (sitar->mbhc_data.v_brl >> 8) & 0xFF);
2342
2343 n_ready = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_READY);
2344 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B1_CTL,
2345 n_ready[sitar_codec_mclk_index(sitar)]);
2346 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B2_CTL,
2347 sitar->mbhc_data.npoll);
2348 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B3_CTL,
2349 sitar->mbhc_data.nbounce_wait);
2350 n_cic = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_CIC);
2351 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B6_CTL,
2352 n_cic[sitar_codec_mclk_index(sitar)]);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302353}
2354
2355static int sitar_startup(struct snd_pcm_substream *substream,
2356 struct snd_soc_dai *dai)
2357{
Asish Bhattacharya1d069532012-03-07 13:25:24 -08002358 struct wcd9xxx *wcd9xxx = dev_get_drvdata(dai->codec->dev->parent);
2359 if ((wcd9xxx != NULL) && (wcd9xxx->dev != NULL) &&
2360 (wcd9xxx->dev->parent != NULL))
2361 pm_runtime_get_sync(wcd9xxx->dev->parent);
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302362 pr_debug("%s(): substream = %s stream = %d\n" , __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302363 substream->name, substream->stream);
2364
2365 return 0;
2366}
2367
2368static void sitar_shutdown(struct snd_pcm_substream *substream,
2369 struct snd_soc_dai *dai)
2370{
Asish Bhattacharya1d069532012-03-07 13:25:24 -08002371 struct wcd9xxx *wcd9xxx = dev_get_drvdata(dai->codec->dev->parent);
2372 if ((wcd9xxx != NULL) && (wcd9xxx->dev != NULL) &&
2373 (wcd9xxx->dev->parent != NULL)) {
2374 pm_runtime_mark_last_busy(wcd9xxx->dev->parent);
2375 pm_runtime_put(wcd9xxx->dev->parent);
2376 }
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302377 pr_debug("%s(): substream = %s stream = %d\n" , __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302378 substream->name, substream->stream);
2379}
2380
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002381int sitar_mclk_enable(struct snd_soc_codec *codec, int mclk_enable, bool dapm)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302382{
2383 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2384
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302385 pr_debug("%s() mclk_enable = %u\n", __func__, mclk_enable);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302386
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002387 if (dapm)
2388 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302389 if (mclk_enable) {
2390 sitar->mclk_enabled = true;
2391
2392 if (sitar->mbhc_polling_active && (sitar->mclk_enabled)) {
2393 sitar_codec_pause_hs_polling(codec);
2394 sitar_codec_enable_bandgap(codec,
2395 SITAR_BANDGAP_AUDIO_MODE);
2396 sitar_codec_enable_clock_block(codec, 0);
2397 sitar_codec_calibrate_hs_polling(codec);
2398 sitar_codec_start_hs_polling(codec);
2399 } else {
2400 sitar_codec_enable_bandgap(codec,
2401 SITAR_BANDGAP_AUDIO_MODE);
2402 sitar_codec_enable_clock_block(codec, 0);
2403 }
2404 } else {
2405
2406 if (!sitar->mclk_enabled) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002407 if (dapm)
2408 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302409 pr_err("Error, MCLK already diabled\n");
2410 return -EINVAL;
2411 }
2412 sitar->mclk_enabled = false;
2413
2414 if (sitar->mbhc_polling_active) {
2415 if (!sitar->mclk_enabled) {
2416 sitar_codec_pause_hs_polling(codec);
2417 sitar_codec_enable_bandgap(codec,
2418 SITAR_BANDGAP_MBHC_MODE);
2419 sitar_enable_rx_bias(codec, 1);
2420 sitar_codec_enable_clock_block(codec, 1);
2421 sitar_codec_calibrate_hs_polling(codec);
2422 sitar_codec_start_hs_polling(codec);
2423 }
2424 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1,
2425 0x05, 0x01);
2426 } else {
2427 sitar_codec_disable_clock_block(codec);
2428 sitar_codec_enable_bandgap(codec,
2429 SITAR_BANDGAP_OFF);
2430 }
2431 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002432 if (dapm)
2433 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302434 return 0;
2435}
2436
2437static int sitar_set_dai_sysclk(struct snd_soc_dai *dai,
2438 int clk_id, unsigned int freq, int dir)
2439{
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302440 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302441 return 0;
2442}
2443
2444static int sitar_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2445{
2446 u8 val = 0;
2447 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(dai->codec);
2448
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302449 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302450 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2451 case SND_SOC_DAIFMT_CBS_CFS:
2452 /* CPU is master */
2453 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2454 if (dai->id == AIF1_CAP)
2455 snd_soc_update_bits(dai->codec,
2456 SITAR_A_CDC_CLK_TX_I2S_CTL,
2457 SITAR_I2S_MASTER_MODE_MASK, 0);
2458 else if (dai->id == AIF1_PB)
2459 snd_soc_update_bits(dai->codec,
2460 SITAR_A_CDC_CLK_RX_I2S_CTL,
2461 SITAR_I2S_MASTER_MODE_MASK, 0);
2462 }
2463 break;
2464 case SND_SOC_DAIFMT_CBM_CFM:
2465 /* CPU is slave */
2466 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2467 val = SITAR_I2S_MASTER_MODE_MASK;
2468 if (dai->id == AIF1_CAP)
2469 snd_soc_update_bits(dai->codec,
2470 SITAR_A_CDC_CLK_TX_I2S_CTL, val, val);
2471 else if (dai->id == AIF1_PB)
2472 snd_soc_update_bits(dai->codec,
2473 SITAR_A_CDC_CLK_RX_I2S_CTL, val, val);
2474 }
2475 break;
2476 default:
2477 return -EINVAL;
2478 }
2479 return 0;
2480}
2481static int sitar_set_channel_map(struct snd_soc_dai *dai,
2482 unsigned int tx_num, unsigned int *tx_slot,
2483 unsigned int rx_num, unsigned int *rx_slot)
2484
2485{
2486 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(dai->codec);
2487 u32 i = 0;
2488 if (!tx_slot && !rx_slot) {
2489 pr_err("%s: Invalid\n", __func__);
2490 return -EINVAL;
2491 }
2492 pr_debug("%s: DAI-ID %x %d %d\n", __func__, dai->id, tx_num, rx_num);
2493
2494 if (dai->id == AIF1_PB) {
2495 for (i = 0; i < rx_num; i++) {
2496 sitar->dai[dai->id - 1].ch_num[i] = rx_slot[i];
2497 sitar->dai[dai->id - 1].ch_act = 0;
2498 sitar->dai[dai->id - 1].ch_tot = rx_num;
2499 }
2500 } else if (dai->id == AIF1_CAP) {
2501 for (i = 0; i < tx_num; i++) {
2502 sitar->dai[dai->id - 1].ch_num[i] = tx_slot[i];
2503 sitar->dai[dai->id - 1].ch_act = 0;
2504 sitar->dai[dai->id - 1].ch_tot = tx_num;
2505 }
2506 }
2507 return 0;
2508}
2509
2510static int sitar_get_channel_map(struct snd_soc_dai *dai,
2511 unsigned int *tx_num, unsigned int *tx_slot,
2512 unsigned int *rx_num, unsigned int *rx_slot)
2513
2514{
2515 struct wcd9xxx *sitar = dev_get_drvdata(dai->codec->control_data);
2516
2517 u32 cnt = 0;
2518 u32 tx_ch[SLIM_MAX_TX_PORTS];
2519 u32 rx_ch[SLIM_MAX_RX_PORTS];
2520
2521 if (!rx_slot && !tx_slot) {
2522 pr_err("%s: Invalid\n", __func__);
2523 return -EINVAL;
2524 }
2525 pr_debug("%s: DAI-ID %x\n", __func__, dai->id);
2526 /* for virtual port, codec driver needs to do
2527 * housekeeping, for now should be ok
2528 */
2529 wcd9xxx_get_channel(sitar, rx_ch, tx_ch);
2530 if (dai->id == AIF1_PB) {
2531 *rx_num = sitar_dai[dai->id - 1].playback.channels_max;
2532 while (cnt < *rx_num) {
2533 rx_slot[cnt] = rx_ch[cnt];
2534 cnt++;
2535 }
2536 } else if (dai->id == AIF1_CAP) {
2537 *tx_num = sitar_dai[dai->id - 1].capture.channels_max;
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002538 tx_slot[0] = tx_ch[cnt];
2539 tx_slot[1] = tx_ch[4 + cnt];
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -07002540 tx_slot[2] = tx_ch[2 + cnt];
2541 tx_slot[3] = tx_ch[3 + cnt];
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302542 }
2543 return 0;
2544}
2545
2546static int sitar_hw_params(struct snd_pcm_substream *substream,
2547 struct snd_pcm_hw_params *params,
2548 struct snd_soc_dai *dai)
2549{
2550 struct snd_soc_codec *codec = dai->codec;
2551 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(dai->codec);
2552 u8 path, shift;
2553 u16 tx_fs_reg, rx_fs_reg;
2554 u8 tx_fs_rate, rx_fs_rate, rx_state, tx_state;
2555
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302556 pr_debug("%s: DAI-ID %x\n", __func__, dai->id);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302557
2558 switch (params_rate(params)) {
2559 case 8000:
2560 tx_fs_rate = 0x00;
2561 rx_fs_rate = 0x00;
2562 break;
2563 case 16000:
2564 tx_fs_rate = 0x01;
2565 rx_fs_rate = 0x20;
2566 break;
2567 case 32000:
2568 tx_fs_rate = 0x02;
2569 rx_fs_rate = 0x40;
2570 break;
2571 case 48000:
2572 tx_fs_rate = 0x03;
2573 rx_fs_rate = 0x60;
2574 break;
2575 default:
2576 pr_err("%s: Invalid sampling rate %d\n", __func__,
2577 params_rate(params));
2578 return -EINVAL;
2579 }
2580
2581
2582 /**
2583 * If current dai is a tx dai, set sample rate to
2584 * all the txfe paths that are currently not active
2585 */
2586 if (dai->id == AIF1_CAP) {
2587
2588 tx_state = snd_soc_read(codec,
2589 SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL);
2590
2591 for (path = 1, shift = 0;
2592 path <= NUM_DECIMATORS; path++, shift++) {
2593
2594 if (!(tx_state & (1 << shift))) {
2595 tx_fs_reg = SITAR_A_CDC_TX1_CLK_FS_CTL
2596 + (BITS_PER_REG*(path-1));
2597 snd_soc_update_bits(codec, tx_fs_reg,
2598 0x03, tx_fs_rate);
2599 }
2600 }
2601 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2602 switch (params_format(params)) {
2603 case SNDRV_PCM_FORMAT_S16_LE:
2604 snd_soc_update_bits(codec,
2605 SITAR_A_CDC_CLK_TX_I2S_CTL,
2606 0x20, 0x20);
2607 break;
2608 case SNDRV_PCM_FORMAT_S32_LE:
2609 snd_soc_update_bits(codec,
2610 SITAR_A_CDC_CLK_TX_I2S_CTL,
2611 0x20, 0x00);
2612 break;
2613 default:
2614 pr_err("invalid format\n");
2615 break;
2616 }
2617 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_TX_I2S_CTL,
2618 0x03, tx_fs_rate);
2619 }
2620 } else {
2621 sitar->dai[dai->id - 1].rate = params_rate(params);
2622 }
2623
2624 /**
2625 * TODO: Need to handle case where same RX chain takes 2 or more inputs
2626 * with varying sample rates
2627 */
2628
2629 /**
2630 * If current dai is a rx dai, set sample rate to
2631 * all the rx paths that are currently not active
2632 */
2633 if (dai->id == AIF1_PB) {
2634
2635 rx_state = snd_soc_read(codec,
2636 SITAR_A_CDC_CLK_RX_B1_CTL);
2637
2638 for (path = 1, shift = 0;
2639 path <= NUM_INTERPOLATORS; path++, shift++) {
2640
2641 if (!(rx_state & (1 << shift))) {
2642 rx_fs_reg = SITAR_A_CDC_RX1_B5_CTL
2643 + (BITS_PER_REG*(path-1));
2644 snd_soc_update_bits(codec, rx_fs_reg,
2645 0xE0, rx_fs_rate);
2646 }
2647 }
2648 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2649 switch (params_format(params)) {
2650 case SNDRV_PCM_FORMAT_S16_LE:
2651 snd_soc_update_bits(codec,
2652 SITAR_A_CDC_CLK_RX_I2S_CTL,
2653 0x20, 0x20);
2654 break;
2655 case SNDRV_PCM_FORMAT_S32_LE:
2656 snd_soc_update_bits(codec,
2657 SITAR_A_CDC_CLK_RX_I2S_CTL,
2658 0x20, 0x00);
2659 break;
2660 default:
2661 pr_err("invalid format\n");
2662 break;
2663 }
2664 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_RX_I2S_CTL,
2665 0x03, (rx_fs_rate >> 0x05));
2666 }
2667 } else {
2668 sitar->dai[dai->id - 1].rate = params_rate(params);
2669 }
2670
2671 return 0;
2672}
2673
2674static struct snd_soc_dai_ops sitar_dai_ops = {
2675 .startup = sitar_startup,
2676 .shutdown = sitar_shutdown,
2677 .hw_params = sitar_hw_params,
2678 .set_sysclk = sitar_set_dai_sysclk,
2679 .set_fmt = sitar_set_dai_fmt,
2680 .set_channel_map = sitar_set_channel_map,
2681 .get_channel_map = sitar_get_channel_map,
2682};
2683
2684static struct snd_soc_dai_driver sitar_dai[] = {
2685 {
2686 .name = "sitar_rx1",
2687 .id = AIF1_PB,
2688 .playback = {
2689 .stream_name = "AIF1 Playback",
2690 .rates = WCD9304_RATES,
2691 .formats = SITAR_FORMATS,
2692 .rate_max = 48000,
2693 .rate_min = 8000,
2694 .channels_min = 1,
2695 .channels_max = 2,
2696 },
2697 .ops = &sitar_dai_ops,
2698 },
2699 {
2700 .name = "sitar_tx1",
2701 .id = AIF1_CAP,
2702 .capture = {
2703 .stream_name = "AIF1 Capture",
2704 .rates = WCD9304_RATES,
2705 .formats = SITAR_FORMATS,
2706 .rate_max = 48000,
2707 .rate_min = 8000,
2708 .channels_min = 1,
2709 .channels_max = 2,
2710 },
2711 .ops = &sitar_dai_ops,
2712 },
2713};
2714
2715static int sitar_codec_enable_slimrx(struct snd_soc_dapm_widget *w,
2716 struct snd_kcontrol *kcontrol, int event)
2717{
2718 struct wcd9xxx *sitar;
2719 struct snd_soc_codec *codec = w->codec;
2720 struct sitar_priv *sitar_p = snd_soc_codec_get_drvdata(codec);
2721 u32 j = 0;
2722 codec->control_data = dev_get_drvdata(codec->dev->parent);
2723 sitar = codec->control_data;
2724 /* Execute the callback only if interface type is slimbus */
2725 if (sitar_p->intf_type != WCD9XXX_INTERFACE_TYPE_SLIMBUS)
2726 return 0;
2727 switch (event) {
2728 case SND_SOC_DAPM_POST_PMU:
2729 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
2730 if (sitar_dai[j].id == AIF1_CAP)
2731 continue;
2732 if (!strncmp(w->sname,
2733 sitar_dai[j].playback.stream_name, 13)) {
2734 ++sitar_p->dai[j].ch_act;
2735 break;
2736 }
2737 }
2738 if (sitar_p->dai[j].ch_act == sitar_p->dai[j].ch_tot)
2739 wcd9xxx_cfg_slim_sch_rx(sitar,
2740 sitar_p->dai[j].ch_num,
2741 sitar_p->dai[j].ch_tot,
2742 sitar_p->dai[j].rate);
2743 break;
2744 case SND_SOC_DAPM_POST_PMD:
2745 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
2746 if (sitar_dai[j].id == AIF1_CAP)
2747 continue;
2748 if (!strncmp(w->sname,
2749 sitar_dai[j].playback.stream_name, 13)) {
2750 --sitar_p->dai[j].ch_act;
2751 break;
2752 }
2753 }
2754 if (!sitar_p->dai[j].ch_act) {
2755 wcd9xxx_close_slim_sch_rx(sitar,
2756 sitar_p->dai[j].ch_num,
2757 sitar_p->dai[j].ch_tot);
Bhalchandra Gajare1ff24fd2012-03-30 11:24:47 -07002758 /* Wait for remove channel to complete
2759 * before derouting Rx path
2760 */
2761 usleep_range(15000, 15000);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302762 sitar_p->dai[j].rate = 0;
2763 memset(sitar_p->dai[j].ch_num, 0, (sizeof(u32)*
2764 sitar_p->dai[j].ch_tot));
2765 sitar_p->dai[j].ch_tot = 0;
2766 }
2767 }
2768 return 0;
2769}
2770
2771static int sitar_codec_enable_slimtx(struct snd_soc_dapm_widget *w,
2772 struct snd_kcontrol *kcontrol, int event)
2773{
2774 struct wcd9xxx *sitar;
2775 struct snd_soc_codec *codec = w->codec;
2776 struct sitar_priv *sitar_p = snd_soc_codec_get_drvdata(codec);
2777 /* index to the DAI ID, for now hardcoding */
2778 u32 j = 0;
2779
2780 codec->control_data = dev_get_drvdata(codec->dev->parent);
2781 sitar = codec->control_data;
2782
2783 /* Execute the callback only if interface type is slimbus */
2784 if (sitar_p->intf_type != WCD9XXX_INTERFACE_TYPE_SLIMBUS)
2785 return 0;
2786 switch (event) {
2787 case SND_SOC_DAPM_POST_PMU:
2788 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
2789 if (sitar_dai[j].id == AIF1_PB)
2790 continue;
2791 if (!strncmp(w->sname,
2792 sitar_dai[j].capture.stream_name, 13)) {
2793 ++sitar_p->dai[j].ch_act;
2794 break;
2795 }
2796 }
2797 if (sitar_p->dai[j].ch_act == sitar_p->dai[j].ch_tot)
2798 wcd9xxx_cfg_slim_sch_tx(sitar,
2799 sitar_p->dai[j].ch_num,
2800 sitar_p->dai[j].ch_tot,
2801 sitar_p->dai[j].rate);
2802 break;
2803 case SND_SOC_DAPM_POST_PMD:
2804 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
2805 if (sitar_dai[j].id == AIF1_PB)
2806 continue;
2807 if (!strncmp(w->sname,
2808 sitar_dai[j].capture.stream_name, 13)) {
2809 --sitar_p->dai[j].ch_act;
2810 break;
2811 }
2812 }
2813 if (!sitar_p->dai[j].ch_act) {
2814 wcd9xxx_close_slim_sch_tx(sitar,
2815 sitar_p->dai[j].ch_num,
2816 sitar_p->dai[j].ch_tot);
2817 sitar_p->dai[j].rate = 0;
2818 memset(sitar_p->dai[j].ch_num, 0, (sizeof(u32)*
2819 sitar_p->dai[j].ch_tot));
2820 sitar_p->dai[j].ch_tot = 0;
2821 }
2822 }
2823 return 0;
2824}
2825
2826
2827static short sitar_codec_read_sta_result(struct snd_soc_codec *codec)
2828{
2829 u8 bias_msb, bias_lsb;
2830 short bias_value;
2831
2832 bias_msb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B3_STATUS);
2833 bias_lsb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B2_STATUS);
2834 bias_value = (bias_msb << 8) | bias_lsb;
2835 return bias_value;
2836}
2837
2838static short sitar_codec_read_dce_result(struct snd_soc_codec *codec)
2839{
2840 u8 bias_msb, bias_lsb;
2841 short bias_value;
2842
2843 bias_msb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B5_STATUS);
2844 bias_lsb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B4_STATUS);
2845 bias_value = (bias_msb << 8) | bias_lsb;
2846 return bias_value;
2847}
2848
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002849static void sitar_turn_onoff_rel_detection(struct snd_soc_codec *codec,
2850 bool on)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302851{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002852 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x02, on << 1);
2853}
2854
2855static short __sitar_codec_sta_dce(struct snd_soc_codec *codec, int dce,
2856 bool override_bypass, bool noreldetection)
2857{
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302858 short bias_value;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002859 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2860
2861 wcd9xxx_disable_irq(codec->control_data, SITAR_IRQ_MBHC_POTENTIAL);
2862 if (noreldetection)
2863 sitar_turn_onoff_rel_detection(codec, false);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302864
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002865 /* Turn on the override */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002866 if (!override_bypass)
2867 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x4, 0x4);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302868 if (dce) {
2869 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
2870 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x4);
2871 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x0);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002872 usleep_range(sitar->mbhc_data.t_sta_dce,
2873 sitar->mbhc_data.t_sta_dce);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302874 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x4);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002875 usleep_range(sitar->mbhc_data.t_dce,
2876 sitar->mbhc_data.t_dce);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302877 bias_value = sitar_codec_read_dce_result(codec);
2878 } else {
2879 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
2880 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x2);
2881 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x0);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002882 usleep_range(sitar->mbhc_data.t_sta_dce,
2883 sitar->mbhc_data.t_sta_dce);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302884 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x2);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002885 usleep_range(sitar->mbhc_data.t_sta,
2886 sitar->mbhc_data.t_sta);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302887 bias_value = sitar_codec_read_sta_result(codec);
2888 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
2889 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x0);
2890 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002891 /* Turn off the override after measuring mic voltage */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002892 if (!override_bypass)
2893 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, 0x00);
2894
2895 if (noreldetection)
2896 sitar_turn_onoff_rel_detection(codec, true);
2897 wcd9xxx_enable_irq(codec->control_data, SITAR_IRQ_MBHC_POTENTIAL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302898
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302899 return bias_value;
2900}
2901
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002902static short sitar_codec_sta_dce(struct snd_soc_codec *codec, int dce,
2903 bool norel)
2904{
2905 return __sitar_codec_sta_dce(codec, dce, false, norel);
2906}
2907
2908static void sitar_codec_shutdown_hs_removal_detect(struct snd_soc_codec *codec)
2909{
2910 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2911 const struct sitar_mbhc_general_cfg *generic =
2912 SITAR_MBHC_CAL_GENERAL_PTR(sitar->mbhc_cfg.calibration);
2913
2914 if (!sitar->mclk_enabled && !sitar->mbhc_polling_active)
2915 sitar_codec_enable_config_mode(codec, 1);
2916
2917 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x2, 0x2);
2918 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x6, 0x0);
2919
2920 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.mbhc_reg, 0x80, 0x00);
2921
2922 usleep_range(generic->t_shutdown_plug_rem,
2923 generic->t_shutdown_plug_rem);
2924
2925 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0xA, 0x8);
2926 if (!sitar->mclk_enabled && !sitar->mbhc_polling_active)
2927 sitar_codec_enable_config_mode(codec, 0);
2928
2929 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x00);
2930}
2931
2932static void sitar_codec_cleanup_hs_polling(struct snd_soc_codec *codec)
2933{
2934 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2935
2936 sitar_codec_shutdown_hs_removal_detect(codec);
2937
2938 if (!sitar->mclk_enabled) {
2939 sitar_codec_disable_clock_block(codec);
2940 sitar_codec_enable_bandgap(codec, SITAR_BANDGAP_OFF);
2941 }
2942
2943 sitar->mbhc_polling_active = false;
2944 sitar->mbhc_state = MBHC_STATE_NONE;
2945}
2946
2947/* called only from interrupt which is under codec_resource_lock acquisition */
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302948static short sitar_codec_setup_hs_polling(struct snd_soc_codec *codec)
2949{
2950 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302951 short bias_value;
2952 u8 cfilt_mode;
2953
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002954 if (!sitar->mbhc_cfg.calibration) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302955 pr_err("Error, no sitar calibration\n");
2956 return -ENODEV;
2957 }
2958
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302959 if (!sitar->mclk_enabled) {
2960 sitar_codec_enable_bandgap(codec, SITAR_BANDGAP_MBHC_MODE);
2961 sitar_enable_rx_bias(codec, 1);
2962 sitar_codec_enable_clock_block(codec, 1);
2963 }
2964
2965 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x05, 0x01);
2966
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302967 /* Make sure CFILT is in fast mode, save current mode */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002968 cfilt_mode = snd_soc_read(codec, sitar->mbhc_bias_regs.cfilt_ctl);
2969 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x70, 0x00);
2970
2971 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg, 0x1F, 0x16);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302972
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302973 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x2, 0x2);
2974 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
2975
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002976 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN, 0x80, 0x80);
2977 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN, 0x1F, 0x1C);
2978 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_TEST_CTL, 0x40, 0x40);
2979
2980 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN, 0x80, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302981 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
2982 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x00);
2983
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002984 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x2, 0x2);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302985 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
2986
2987 sitar_codec_calibrate_hs_polling(codec);
2988
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002989 /* don't flip override */
2990 bias_value = __sitar_codec_sta_dce(codec, 1, true, true);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002991 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x40,
2992 cfilt_mode);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302993 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x13, 0x00);
2994
2995 return bias_value;
2996}
2997
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002998static int sitar_cancel_btn_work(struct sitar_priv *sitar)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302999{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003000 int r = 0;
3001 struct wcd9xxx *core = dev_get_drvdata(sitar->codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303002
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003003 if (cancel_delayed_work_sync(&sitar->mbhc_btn_dwork)) {
3004 /* if scheduled mbhc_btn_dwork is canceled from here,
3005 * we have to unlock from here instead btn_work */
3006 wcd9xxx_unlock_sleep(core);
3007 r = 1;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303008 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003009 return r;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303010}
3011
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003012
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003013static u16 sitar_codec_v_sta_dce(struct snd_soc_codec *codec, bool dce,
3014 s16 vin_mv)
3015{
3016 short diff, zero;
3017 struct sitar_priv *sitar;
3018 u32 mb_mv, in;
3019
3020 sitar = snd_soc_codec_get_drvdata(codec);
3021 mb_mv = sitar->mbhc_data.micb_mv;
3022
3023 if (mb_mv == 0) {
3024 pr_err("%s: Mic Bias voltage is set to zero\n", __func__);
3025 return -EINVAL;
3026 }
3027
3028 if (dce) {
3029 diff = sitar->mbhc_data.dce_mb - sitar->mbhc_data.dce_z;
3030 zero = sitar->mbhc_data.dce_z;
3031 } else {
3032 diff = sitar->mbhc_data.sta_mb - sitar->mbhc_data.sta_z;
3033 zero = sitar->mbhc_data.sta_z;
3034 }
3035 in = (u32) diff * vin_mv;
3036
3037 return (u16) (in / mb_mv) + zero;
3038}
3039
3040static s32 sitar_codec_sta_dce_v(struct snd_soc_codec *codec, s8 dce,
3041 u16 bias_value)
3042{
3043 struct sitar_priv *sitar;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003044 s16 value, z, mb;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003045 s32 mv;
3046
3047 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003048 value = bias_value;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003049
3050 if (dce) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003051 z = (sitar->mbhc_data.dce_z);
3052 mb = (sitar->mbhc_data.dce_mb);
3053 mv = (value - z) * (s32)sitar->mbhc_data.micb_mv / (mb - z);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003054 } else {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003055 z = (sitar->mbhc_data.sta_z);
3056 mb = (sitar->mbhc_data.sta_mb);
3057 mv = (value - z) * (s32)sitar->mbhc_data.micb_mv / (mb - z);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003058 }
3059
3060 return mv;
3061}
3062
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003063static void btn_lpress_fn(struct work_struct *work)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303064{
3065 struct delayed_work *delayed_work;
3066 struct sitar_priv *sitar;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003067 short bias_value;
3068 int dce_mv, sta_mv;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003069 struct wcd9xxx *core;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303070
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003071 pr_debug("%s:\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303072
3073 delayed_work = to_delayed_work(work);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003074 sitar = container_of(delayed_work, struct sitar_priv, mbhc_btn_dwork);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003075 core = dev_get_drvdata(sitar->codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303076
3077 if (sitar) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003078 if (sitar->mbhc_cfg.button_jack) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003079 bias_value = sitar_codec_read_sta_result(sitar->codec);
3080 sta_mv = sitar_codec_sta_dce_v(sitar->codec, 0,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003081 bias_value);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003082 bias_value = sitar_codec_read_dce_result(sitar->codec);
3083 dce_mv = sitar_codec_sta_dce_v(sitar->codec, 1,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003084 bias_value);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003085 pr_debug("%s: Reporting long button press event"
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003086 " STA: %d, DCE: %d\n", __func__, sta_mv, dce_mv);
3087 sitar_snd_soc_jack_report(sitar,
3088 sitar->mbhc_cfg.button_jack,
3089 sitar->buttons_pressed,
3090 sitar->buttons_pressed);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303091 }
3092 } else {
3093 pr_err("%s: Bad sitar private data\n", __func__);
3094 }
3095
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003096 pr_debug("%s: leave\n", __func__);
3097 wcd9xxx_unlock_sleep(core);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303098}
3099
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003100
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003101void sitar_mbhc_cal(struct snd_soc_codec *codec)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303102{
3103 struct sitar_priv *sitar;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003104 struct sitar_mbhc_btn_detect_cfg *btn_det;
3105 u8 cfilt_mode, bg_mode;
3106 u8 ncic, nmeas, navg;
3107 u32 mclk_rate;
3108 u32 dce_wait, sta_wait;
3109 u8 *n_cic;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003110 void *calibration;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003111
3112 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003113 calibration = sitar->mbhc_cfg.calibration;
3114
3115 wcd9xxx_disable_irq(codec->control_data, SITAR_IRQ_MBHC_POTENTIAL);
3116 sitar_turn_onoff_rel_detection(codec, false);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003117
3118 /* First compute the DCE / STA wait times
3119 * depending on tunable parameters.
3120 * The value is computed in microseconds
3121 */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003122 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003123 n_cic = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_CIC);
3124 ncic = n_cic[sitar_codec_mclk_index(sitar)];
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003125 nmeas = SITAR_MBHC_CAL_BTN_DET_PTR(calibration)->n_meas;
3126 navg = SITAR_MBHC_CAL_GENERAL_PTR(calibration)->mbhc_navg;
3127 mclk_rate = sitar->mbhc_cfg.mclk_rate;
3128 dce_wait = (1000 * 512 * 60 * (nmeas + 1)) / (mclk_rate / 1000);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003129 sta_wait = (1000 * 128 * (navg + 1)) / (mclk_rate / 1000);
3130
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003131 sitar->mbhc_data.t_dce = DEFAULT_DCE_WAIT;
3132 sitar->mbhc_data.t_sta = DEFAULT_STA_WAIT;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003133
3134 /* LDOH and CFILT are already configured during pdata handling.
3135 * Only need to make sure CFILT and bandgap are in Fast mode.
3136 * Need to restore defaults once calculation is done.
3137 */
3138 cfilt_mode = snd_soc_read(codec, sitar->mbhc_bias_regs.cfilt_ctl);
3139 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x40, 0x00);
3140 bg_mode = snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x02,
3141 0x02);
3142
3143 /* Micbias, CFILT, LDOH, MBHC MUX mode settings
3144 * to perform ADC calibration
3145 */
3146 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg, 0x60,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003147 sitar->mbhc_cfg.micbias << 5);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003148 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg, 0x01, 0x00);
3149 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1, 0x60, 0x60);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003150 snd_soc_write(codec, SITAR_A_TX_4_MBHC_TEST_CTL, 0x78);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003151 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, 0x04);
3152
3153 /* DCE measurement for 0 volts */
3154 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x0A);
3155 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x04);
3156 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x02);
3157 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x81);
3158 usleep_range(100, 100);
3159 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x04);
3160 usleep_range(sitar->mbhc_data.t_dce, sitar->mbhc_data.t_dce);
3161 sitar->mbhc_data.dce_z = sitar_codec_read_dce_result(codec);
3162
3163 /* DCE measurment for MB voltage */
3164 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x0A);
3165 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x02);
3166 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x82);
3167 usleep_range(100, 100);
3168 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x04);
3169 usleep_range(sitar->mbhc_data.t_dce, sitar->mbhc_data.t_dce);
3170 sitar->mbhc_data.dce_mb = sitar_codec_read_dce_result(codec);
3171
3172 /* Sta measuremnt for 0 volts */
3173 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x0A);
3174 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x02);
3175 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x02);
3176 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x81);
3177 usleep_range(100, 100);
3178 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x02);
3179 usleep_range(sitar->mbhc_data.t_sta, sitar->mbhc_data.t_sta);
3180 sitar->mbhc_data.sta_z = sitar_codec_read_sta_result(codec);
3181
3182 /* STA Measurement for MB Voltage */
3183 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x82);
3184 usleep_range(100, 100);
3185 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x02);
3186 usleep_range(sitar->mbhc_data.t_sta, sitar->mbhc_data.t_sta);
3187 sitar->mbhc_data.sta_mb = sitar_codec_read_sta_result(codec);
3188
3189 /* Restore default settings. */
3190 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, 0x00);
3191 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x40,
3192 cfilt_mode);
3193 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x02, bg_mode);
3194
3195 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
3196 usleep_range(100, 100);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003197
3198 wcd9xxx_enable_irq(codec->control_data, SITAR_IRQ_MBHC_POTENTIAL);
3199 sitar_turn_onoff_rel_detection(codec, true);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003200}
3201
3202void *sitar_mbhc_cal_btn_det_mp(const struct sitar_mbhc_btn_detect_cfg* btn_det,
3203 const enum sitar_mbhc_btn_det_mem mem)
3204{
3205 void *ret = &btn_det->_v_btn_low;
3206
3207 switch (mem) {
3208 case SITAR_BTN_DET_GAIN:
3209 ret += sizeof(btn_det->_n_cic);
3210 case SITAR_BTN_DET_N_CIC:
3211 ret += sizeof(btn_det->_n_ready);
3212 case SITAR_BTN_DET_N_READY:
3213 ret += sizeof(btn_det->_v_btn_high[0]) * btn_det->num_btn;
3214 case SITAR_BTN_DET_V_BTN_HIGH:
3215 ret += sizeof(btn_det->_v_btn_low[0]) * btn_det->num_btn;
3216 case SITAR_BTN_DET_V_BTN_LOW:
3217 /* do nothing */
3218 break;
3219 default:
3220 ret = NULL;
3221 }
3222
3223 return ret;
3224}
3225
3226static void sitar_mbhc_calc_thres(struct snd_soc_codec *codec)
3227{
3228 struct sitar_priv *sitar;
3229 s16 btn_mv = 0, btn_delta_mv;
3230 struct sitar_mbhc_btn_detect_cfg *btn_det;
3231 struct sitar_mbhc_plug_type_cfg *plug_type;
3232 u16 *btn_high;
3233 u8 *n_ready;
3234 int i;
3235
3236 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003237 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(sitar->mbhc_cfg.calibration);
3238 plug_type = SITAR_MBHC_CAL_PLUG_TYPE_PTR(sitar->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003239
3240 n_ready = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_READY);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003241 if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_12288KHZ) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003242 sitar->mbhc_data.npoll = 9;
3243 sitar->mbhc_data.nbounce_wait = 30;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003244 } else if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_9600KHZ) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003245 sitar->mbhc_data.npoll = 7;
3246 sitar->mbhc_data.nbounce_wait = 23;
3247 }
3248
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003249 sitar->mbhc_data.t_sta_dce = ((1000 * 256) /
3250 (sitar->mbhc_cfg.mclk_rate / 1000) *
3251 n_ready[sitar_codec_mclk_index(sitar)]) +
3252 10;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003253 sitar->mbhc_data.v_ins_hu =
3254 sitar_codec_v_sta_dce(codec, STA, plug_type->v_hs_max);
3255 sitar->mbhc_data.v_ins_h =
3256 sitar_codec_v_sta_dce(codec, DCE, plug_type->v_hs_max);
3257
3258 btn_high = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_V_BTN_HIGH);
3259 for (i = 0; i < btn_det->num_btn; i++)
3260 btn_mv = btn_high[i] > btn_mv ? btn_high[i] : btn_mv;
3261
3262 sitar->mbhc_data.v_b1_h = sitar_codec_v_sta_dce(codec, DCE, btn_mv);
3263 btn_delta_mv = btn_mv + btn_det->v_btn_press_delta_sta;
3264
3265 sitar->mbhc_data.v_b1_hu =
3266 sitar_codec_v_sta_dce(codec, STA, btn_delta_mv);
3267
3268 btn_delta_mv = btn_mv + btn_det->v_btn_press_delta_cic;
3269
3270 sitar->mbhc_data.v_b1_huc =
3271 sitar_codec_v_sta_dce(codec, DCE, btn_delta_mv);
3272
3273 sitar->mbhc_data.v_brh = sitar->mbhc_data.v_b1_h;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003274 sitar->mbhc_data.v_brl = SITAR_MBHC_BUTTON_MIN;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003275
3276 sitar->mbhc_data.v_no_mic =
3277 sitar_codec_v_sta_dce(codec, STA, plug_type->v_no_mic);
3278}
3279
3280void sitar_mbhc_init(struct snd_soc_codec *codec)
3281{
3282 struct sitar_priv *sitar;
3283 struct sitar_mbhc_general_cfg *generic;
3284 struct sitar_mbhc_btn_detect_cfg *btn_det;
3285 int n;
3286 u8 *n_cic, *gain;
3287
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003288 pr_err("%s(): ENTER\n", __func__);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003289 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003290 generic = SITAR_MBHC_CAL_GENERAL_PTR(sitar->mbhc_cfg.calibration);
3291 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(sitar->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003292
3293 for (n = 0; n < 8; n++) {
3294 if (n != 7) {
3295 snd_soc_update_bits(codec,
3296 SITAR_A_CDC_MBHC_FIR_B1_CFG,
3297 0x07, n);
3298 snd_soc_write(codec, SITAR_A_CDC_MBHC_FIR_B2_CFG,
3299 btn_det->c[n]);
3300 }
3301 }
3302 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B2_CTL, 0x07,
3303 btn_det->nc);
3304
3305 n_cic = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_CIC);
3306 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_TIMER_B6_CTL, 0xFF,
3307 n_cic[sitar_codec_mclk_index(sitar)]);
3308
3309 gain = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_GAIN);
3310 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B2_CTL, 0x78,
3311 gain[sitar_codec_mclk_index(sitar)] << 3);
3312
3313 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_TIMER_B4_CTL, 0x70,
3314 generic->mbhc_nsa << 4);
3315
3316 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_TIMER_B4_CTL, 0x0F,
3317 btn_det->n_meas);
3318
3319 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B5_CTL, generic->mbhc_navg);
3320
3321 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x80, 0x80);
3322
3323 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x78,
3324 btn_det->mbhc_nsc << 3);
3325
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003326 snd_soc_update_bits(codec, SITAR_A_MICB_1_MBHC, 0x03,
3327 sitar->mbhc_cfg.micbias);
3328
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003329 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x02, 0x02);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003330
3331 snd_soc_update_bits(codec, SITAR_A_MBHC_SCALING_MUX_2, 0xF0, 0xF0);
3332
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003333}
3334
3335static bool sitar_mbhc_fw_validate(const struct firmware *fw)
3336{
3337 u32 cfg_offset;
3338 struct sitar_mbhc_imped_detect_cfg *imped_cfg;
3339 struct sitar_mbhc_btn_detect_cfg *btn_cfg;
3340
3341 if (fw->size < SITAR_MBHC_CAL_MIN_SIZE)
3342 return false;
3343
3344 /* previous check guarantees that there is enough fw data up
3345 * to num_btn
3346 */
3347 btn_cfg = SITAR_MBHC_CAL_BTN_DET_PTR(fw->data);
3348 cfg_offset = (u32) ((void *) btn_cfg - (void *) fw->data);
3349 if (fw->size < (cfg_offset + SITAR_MBHC_CAL_BTN_SZ(btn_cfg)))
3350 return false;
3351
3352 /* previous check guarantees that there is enough fw data up
3353 * to start of impedance detection configuration
3354 */
3355 imped_cfg = SITAR_MBHC_CAL_IMPED_DET_PTR(fw->data);
3356 cfg_offset = (u32) ((void *) imped_cfg - (void *) fw->data);
3357
3358 if (fw->size < (cfg_offset + SITAR_MBHC_CAL_IMPED_MIN_SZ))
3359 return false;
3360
3361 if (fw->size < (cfg_offset + SITAR_MBHC_CAL_IMPED_SZ(imped_cfg)))
3362 return false;
3363
3364 return true;
3365}
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003366
3367
3368static void sitar_turn_onoff_override(struct snd_soc_codec *codec, bool on)
3369{
3370 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, on << 2);
3371}
3372
3373/* called under codec_resource_lock acquisition */
3374void sitar_set_and_turnoff_hph_padac(struct snd_soc_codec *codec)
3375{
3376 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3377 u8 wg_time;
3378
3379 wg_time = snd_soc_read(codec, SITAR_A_RX_HPH_CNP_WG_TIME) ;
3380 wg_time += 1;
3381
3382 /* If headphone PA is on, check if userspace receives
3383 * removal event to sync-up PA's state */
3384 if (sitar_is_hph_pa_on(codec)) {
3385 pr_debug("%s PA is on, setting PA_OFF_ACK\n", __func__);
3386 set_bit(SITAR_HPHL_PA_OFF_ACK, &sitar->hph_pa_dac_state);
3387 set_bit(SITAR_HPHR_PA_OFF_ACK, &sitar->hph_pa_dac_state);
3388 } else {
3389 pr_debug("%s PA is off\n", __func__);
3390 }
3391
3392 if (sitar_is_hph_dac_on(codec, 1))
3393 set_bit(SITAR_HPHL_DAC_OFF_ACK, &sitar->hph_pa_dac_state);
3394 if (sitar_is_hph_dac_on(codec, 0))
3395 set_bit(SITAR_HPHR_DAC_OFF_ACK, &sitar->hph_pa_dac_state);
3396
3397 snd_soc_update_bits(codec, SITAR_A_RX_HPH_CNP_EN, 0x30, 0x00);
3398 snd_soc_update_bits(codec, SITAR_A_RX_HPH_L_DAC_CTL,
3399 0xC0, 0x00);
3400 snd_soc_update_bits(codec, SITAR_A_RX_HPH_R_DAC_CTL,
3401 0xC0, 0x00);
3402 usleep_range(wg_time * 1000, wg_time * 1000);
3403}
3404
3405static void sitar_clr_and_turnon_hph_padac(struct sitar_priv *sitar)
3406{
3407 bool pa_turned_on = false;
3408 struct snd_soc_codec *codec = sitar->codec;
3409 u8 wg_time;
3410
3411 wg_time = snd_soc_read(codec, SITAR_A_RX_HPH_CNP_WG_TIME) ;
3412 wg_time += 1;
3413
3414 if (test_and_clear_bit(SITAR_HPHR_DAC_OFF_ACK,
3415 &sitar->hph_pa_dac_state)) {
3416 pr_debug("%s: HPHR clear flag and enable DAC\n", __func__);
3417 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_R_DAC_CTL,
3418 0xC0, 0xC0);
3419 }
3420 if (test_and_clear_bit(SITAR_HPHL_DAC_OFF_ACK,
3421 &sitar->hph_pa_dac_state)) {
3422 pr_debug("%s: HPHL clear flag and enable DAC\n", __func__);
3423 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_L_DAC_CTL,
3424 0xC0, 0xC0);
3425 }
3426
3427 if (test_and_clear_bit(SITAR_HPHR_PA_OFF_ACK,
3428 &sitar->hph_pa_dac_state)) {
3429 pr_debug("%s: HPHR clear flag and enable PA\n", __func__);
3430 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_CNP_EN, 0x10,
3431 1 << 4);
3432 pa_turned_on = true;
3433 }
3434 if (test_and_clear_bit(SITAR_HPHL_PA_OFF_ACK,
3435 &sitar->hph_pa_dac_state)) {
3436 pr_debug("%s: HPHL clear flag and enable PA\n", __func__);
3437 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_CNP_EN, 0x20,
3438 1 << 5);
3439 pa_turned_on = true;
3440 }
3441
3442 if (pa_turned_on) {
3443 pr_debug("%s: PA was turned off by MBHC and not by DAPM\n",
3444 __func__);
3445 usleep_range(wg_time * 1000, wg_time * 1000);
3446 }
3447}
3448
3449static void sitar_codec_report_plug(struct snd_soc_codec *codec, int insertion,
3450 enum snd_jack_types jack_type)
3451{
3452 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3453
3454 if (!insertion) {
3455 /* Report removal */
3456 sitar->hph_status &= ~jack_type;
3457 if (sitar->mbhc_cfg.headset_jack) {
3458 /* cancel possibly scheduled btn work and
3459 * report release if we reported button press */
3460 if (sitar_cancel_btn_work(sitar)) {
3461 pr_debug("%s: button press is canceled\n",
3462 __func__);
3463 } else if (sitar->buttons_pressed) {
3464 pr_debug("%s: Reporting release for reported "
3465 "button press %d\n", __func__,
3466 jack_type);
3467 sitar_snd_soc_jack_report(sitar,
3468 sitar->mbhc_cfg.button_jack, 0,
3469 sitar->buttons_pressed);
3470 sitar->buttons_pressed &=
3471 ~SITAR_JACK_BUTTON_MASK;
3472 }
3473 pr_debug("%s: Reporting removal %d\n", __func__,
3474 jack_type);
3475 sitar_snd_soc_jack_report(sitar,
3476 sitar->mbhc_cfg.headset_jack,
3477 sitar->hph_status,
3478 SITAR_JACK_MASK);
3479 }
3480 sitar_set_and_turnoff_hph_padac(codec);
3481 hphocp_off_report(sitar, SND_JACK_OC_HPHR,
3482 SITAR_IRQ_HPH_PA_OCPR_FAULT);
3483 hphocp_off_report(sitar, SND_JACK_OC_HPHL,
3484 SITAR_IRQ_HPH_PA_OCPL_FAULT);
3485 sitar->current_plug = PLUG_TYPE_NONE;
3486 sitar->mbhc_polling_active = false;
3487 } else {
3488 /* Report insertion */
3489 sitar->hph_status |= jack_type;
3490
3491 if (jack_type == SND_JACK_HEADPHONE)
3492 sitar->current_plug = PLUG_TYPE_HEADPHONE;
3493 else if (jack_type == SND_JACK_HEADSET) {
3494 sitar->mbhc_polling_active = true;
3495 sitar->current_plug = PLUG_TYPE_HEADSET;
3496 }
3497 if (sitar->mbhc_cfg.headset_jack) {
3498 pr_debug("%s: Reporting insertion %d\n", __func__,
3499 jack_type);
3500 sitar_snd_soc_jack_report(sitar,
3501 sitar->mbhc_cfg.headset_jack,
3502 sitar->hph_status,
3503 SITAR_JACK_MASK);
3504 }
3505 sitar_clr_and_turnon_hph_padac(sitar);
3506 }
3507}
3508
3509
3510static bool sitar_hs_gpio_level_remove(struct sitar_priv *sitar)
3511{
3512 return (gpio_get_value_cansleep(sitar->mbhc_cfg.gpio) !=
3513 sitar->mbhc_cfg.gpio_level_insert);
3514}
3515
3516static bool sitar_is_invalid_insert_delta(struct snd_soc_codec *codec,
3517 int mic_volt, int mic_volt_prev)
3518{
3519 int delta = abs(mic_volt - mic_volt_prev);
3520 if (delta > SITAR_MBHC_FAKE_INSERT_VOLT_DELTA_MV) {
3521 pr_debug("%s: volt delta %dmv\n", __func__, delta);
3522 return true;
3523 }
3524 return false;
3525}
3526
3527static bool sitar_is_invalid_insertion_range(struct snd_soc_codec *codec,
3528 s32 mic_volt)
3529{
3530 bool invalid = false;
3531
3532 if (mic_volt < SITAR_MBHC_FAKE_INSERT_HIGH
3533 && (mic_volt > SITAR_MBHC_FAKE_INSERT_LOW)) {
3534 invalid = true;
3535 }
3536
3537 return invalid;
3538}
3539
3540static bool sitar_codec_is_invalid_plug(struct snd_soc_codec *codec,
3541 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT],
3542 enum sitar_mbhc_plug_type plug_type[MBHC_NUM_DCE_PLUG_DETECT])
3543{
3544 int i;
3545 bool r = false;
3546 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3547 struct sitar_mbhc_plug_type_cfg *plug_type_ptr =
3548 SITAR_MBHC_CAL_PLUG_TYPE_PTR(sitar->mbhc_cfg.calibration);
3549
3550 for (i = 0 ; i < MBHC_NUM_DCE_PLUG_DETECT && !r; i++) {
3551 if (mic_mv[i] < plug_type_ptr->v_no_mic)
3552 plug_type[i] = PLUG_TYPE_HEADPHONE;
3553 else if (mic_mv[i] < plug_type_ptr->v_hs_max)
3554 plug_type[i] = PLUG_TYPE_HEADSET;
3555 else if (mic_mv[i] > plug_type_ptr->v_hs_max)
3556 plug_type[i] = PLUG_TYPE_HIGH_HPH;
3557
3558 r = sitar_is_invalid_insertion_range(codec, mic_mv[i]);
3559 if (!r && i > 0) {
3560 if (plug_type[i-1] != plug_type[i])
3561 r = true;
3562 else
3563 r = sitar_is_invalid_insert_delta(codec,
3564 mic_mv[i],
3565 mic_mv[i - 1]);
3566 }
3567 }
3568
3569 return r;
3570}
3571
3572/* called under codec_resource_lock acquisition */
3573void sitar_find_plug_and_report(struct snd_soc_codec *codec,
3574 enum sitar_mbhc_plug_type plug_type)
3575{
3576 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3577
3578 if (plug_type == PLUG_TYPE_HEADPHONE
3579 && sitar->current_plug == PLUG_TYPE_NONE) {
3580 /* Nothing was reported previously
3581 * reporte a headphone
3582 */
3583 sitar_codec_report_plug(codec, 1, SND_JACK_HEADPHONE);
3584 sitar_codec_cleanup_hs_polling(codec);
3585 } else if (plug_type == PLUG_TYPE_HEADSET) {
3586 /* If Headphone was reported previously, this will
3587 * only report the mic line
3588 */
3589 sitar_codec_report_plug(codec, 1, SND_JACK_HEADSET);
3590 msleep(100);
3591 sitar_codec_start_hs_polling(codec);
3592 } else if (plug_type == PLUG_TYPE_HIGH_HPH) {
3593 if (sitar->current_plug == PLUG_TYPE_NONE)
3594 sitar_codec_report_plug(codec, 1, SND_JACK_HEADPHONE);
3595 sitar_codec_cleanup_hs_polling(codec);
3596 pr_debug("setup mic trigger for further detection\n");
3597 sitar->lpi_enabled = true;
3598 /* TODO ::: sitar_codec_enable_hs_detect */
3599 pr_err("%s(): High impedence hph not supported\n", __func__);
3600 }
3601}
3602
3603/* should be called under interrupt context that hold suspend */
3604static void sitar_schedule_hs_detect_plug(struct sitar_priv *sitar)
3605{
3606 pr_debug("%s: scheduling sitar_hs_correct_gpio_plug\n", __func__);
3607 sitar->hs_detect_work_stop = false;
3608 wcd9xxx_lock_sleep(sitar->codec->control_data);
3609 schedule_work(&sitar->hs_correct_plug_work);
3610}
3611
3612/* called under codec_resource_lock acquisition */
3613static void sitar_cancel_hs_detect_plug(struct sitar_priv *sitar)
3614{
3615 pr_debug("%s: canceling hs_correct_plug_work\n", __func__);
3616 sitar->hs_detect_work_stop = true;
3617 wmb();
3618 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
3619 if (cancel_work_sync(&sitar->hs_correct_plug_work)) {
3620 pr_debug("%s: hs_correct_plug_work is canceled\n", __func__);
3621 wcd9xxx_unlock_sleep(sitar->codec->control_data);
3622 }
3623 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
3624}
3625
3626static void sitar_hs_correct_gpio_plug(struct work_struct *work)
3627{
3628 struct sitar_priv *sitar;
3629 struct snd_soc_codec *codec;
3630 int retry = 0, i;
3631 bool correction = false;
3632 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT];
3633 short mb_v[MBHC_NUM_DCE_PLUG_DETECT];
3634 enum sitar_mbhc_plug_type plug_type[MBHC_NUM_DCE_PLUG_DETECT];
3635 unsigned long timeout;
3636
3637 sitar = container_of(work, struct sitar_priv, hs_correct_plug_work);
3638 codec = sitar->codec;
3639
3640 pr_debug("%s: enter\n", __func__);
3641 sitar->mbhc_cfg.mclk_cb_fn(codec, 1, false);
3642
3643 /* Keep override on during entire plug type correction work.
3644 *
3645 * This is okay under the assumption that any GPIO irqs which use
3646 * MBHC block cancel and sync this work so override is off again
3647 * prior to GPIO interrupt handler's MBHC block usage.
3648 * Also while this correction work is running, we can guarantee
3649 * DAPM doesn't use any MBHC block as this work only runs with
3650 * headphone detection.
3651 */
3652 sitar_turn_onoff_override(codec, true);
3653
3654 timeout = jiffies + msecs_to_jiffies(SITAR_HS_DETECT_PLUG_TIME_MS);
3655 while (!time_after(jiffies, timeout)) {
3656 ++retry;
3657 rmb();
3658 if (sitar->hs_detect_work_stop) {
3659 pr_debug("%s: stop requested\n", __func__);
3660 break;
3661 }
3662
3663 msleep(SITAR_HS_DETECT_PLUG_INERVAL_MS);
3664 if (sitar_hs_gpio_level_remove(sitar)) {
3665 pr_debug("%s: GPIO value is low\n", __func__);
3666 break;
3667 }
3668
3669 /* can race with removal interrupt */
3670 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
3671 for (i = 0; i < MBHC_NUM_DCE_PLUG_DETECT; i++) {
3672 mb_v[i] = __sitar_codec_sta_dce(codec, 1, true, true);
3673 mic_mv[i] = sitar_codec_sta_dce_v(codec, 1 , mb_v[i]);
3674 pr_debug("%s : DCE run %d, mic_mv = %d(%x)\n",
3675 __func__, retry, mic_mv[i], mb_v[i]);
3676 }
3677 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
3678
3679 if (sitar_codec_is_invalid_plug(codec, mic_mv, plug_type)) {
3680 pr_debug("Invalid plug in attempt # %d\n", retry);
3681 if (retry == NUM_ATTEMPTS_TO_REPORT &&
3682 sitar->current_plug == PLUG_TYPE_NONE) {
3683 sitar_codec_report_plug(codec, 1,
3684 SND_JACK_HEADPHONE);
3685 }
3686 } else if (!sitar_codec_is_invalid_plug(codec, mic_mv,
3687 plug_type) &&
3688 plug_type[0] == PLUG_TYPE_HEADPHONE) {
3689 pr_debug("Good headphone detected, continue polling mic\n");
3690 if (sitar->current_plug == PLUG_TYPE_NONE) {
3691 sitar_codec_report_plug(codec, 1,
3692 SND_JACK_HEADPHONE);
3693 }
3694 } else {
3695 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
3696 /* Turn off override */
3697 sitar_turn_onoff_override(codec, false);
3698 sitar_find_plug_and_report(codec, plug_type[0]);
3699 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
3700 pr_debug("Attempt %d found correct plug %d\n", retry,
3701 plug_type[0]);
3702 correction = true;
3703 break;
3704 }
3705 }
3706
3707 /* Turn off override */
3708 if (!correction)
3709 sitar_turn_onoff_override(codec, false);
3710
3711 sitar->mbhc_cfg.mclk_cb_fn(codec, 0, false);
3712 pr_debug("%s: leave\n", __func__);
3713 /* unlock sleep */
3714 wcd9xxx_unlock_sleep(sitar->codec->control_data);
3715}
3716
3717/* called under codec_resource_lock acquisition */
3718static void sitar_codec_decide_gpio_plug(struct snd_soc_codec *codec)
3719{
3720 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3721 short mb_v[MBHC_NUM_DCE_PLUG_DETECT];
3722 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT];
3723 enum sitar_mbhc_plug_type plug_type[MBHC_NUM_DCE_PLUG_DETECT];
3724 int i;
3725
3726 pr_debug("%s: enter\n", __func__);
3727
3728 sitar_turn_onoff_override(codec, true);
3729 mb_v[0] = sitar_codec_setup_hs_polling(codec);
3730 mic_mv[0] = sitar_codec_sta_dce_v(codec, 1, mb_v[0]);
3731 pr_debug("%s: DCE run 1, mic_mv = %d\n", __func__, mic_mv[0]);
3732
3733 for (i = 1; i < MBHC_NUM_DCE_PLUG_DETECT; i++) {
3734 mb_v[i] = __sitar_codec_sta_dce(codec, 1, true, true);
3735 mic_mv[i] = sitar_codec_sta_dce_v(codec, 1 , mb_v[i]);
3736 pr_debug("%s: DCE run %d, mic_mv = %d\n", __func__, i + 1,
3737 mic_mv[i]);
3738 }
3739 sitar_turn_onoff_override(codec, false);
3740
3741 if (sitar_hs_gpio_level_remove(sitar)) {
3742 pr_debug("%s: GPIO value is low when determining plug\n",
3743 __func__);
3744 return;
3745 }
3746
3747 if (sitar_codec_is_invalid_plug(codec, mic_mv, plug_type)) {
3748 sitar_schedule_hs_detect_plug(sitar);
3749 } else if (plug_type[0] == PLUG_TYPE_HEADPHONE) {
3750 sitar_codec_report_plug(codec, 1, SND_JACK_HEADPHONE);
3751 sitar_schedule_hs_detect_plug(sitar);
3752 } else if (plug_type[0] == PLUG_TYPE_HEADSET) {
3753 pr_debug("%s: Valid plug found, determine plug type\n",
3754 __func__);
3755 sitar_find_plug_and_report(codec, plug_type[0]);
3756 }
3757
3758}
3759
3760/* called under codec_resource_lock acquisition */
3761static void sitar_codec_detect_plug_type(struct snd_soc_codec *codec)
3762{
3763 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3764 const struct sitar_mbhc_plug_detect_cfg *plug_det =
3765 SITAR_MBHC_CAL_PLUG_DET_PTR(sitar->mbhc_cfg.calibration);
3766
3767 if (plug_det->t_ins_complete > 20)
3768 msleep(plug_det->t_ins_complete);
3769 else
3770 usleep_range(plug_det->t_ins_complete * 1000,
3771 plug_det->t_ins_complete * 1000);
3772
3773 if (sitar_hs_gpio_level_remove(sitar))
3774 pr_debug("%s: GPIO value is low when determining "
3775 "plug\n", __func__);
3776 else
3777 sitar_codec_decide_gpio_plug(codec);
3778
3779 return;
3780}
3781
3782static void sitar_hs_gpio_handler(struct snd_soc_codec *codec)
3783{
3784 bool insert;
3785 struct sitar_priv *priv = snd_soc_codec_get_drvdata(codec);
3786 bool is_removed = false;
3787
3788 pr_debug("%s: enter\n", __func__);
3789
3790 priv->in_gpio_handler = true;
3791 /* Wait here for debounce time */
3792 usleep_range(SITAR_GPIO_IRQ_DEBOUNCE_TIME_US,
3793 SITAR_GPIO_IRQ_DEBOUNCE_TIME_US);
3794
3795 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
3796
3797 /* cancel pending button press */
3798 if (sitar_cancel_btn_work(priv))
3799 pr_debug("%s: button press is canceled\n", __func__);
3800
3801 insert = (gpio_get_value_cansleep(priv->mbhc_cfg.gpio) ==
3802 priv->mbhc_cfg.gpio_level_insert);
3803 if ((priv->current_plug == PLUG_TYPE_NONE) && insert) {
3804 priv->lpi_enabled = false;
3805 wmb();
3806
3807 /* cancel detect plug */
3808 sitar_cancel_hs_detect_plug(priv);
3809
3810 /* Disable Mic Bias pull down and HPH Switch to GND */
3811 snd_soc_update_bits(codec, priv->mbhc_bias_regs.ctl_reg, 0x01,
3812 0x00);
3813 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x01, 0x00);
3814 sitar_codec_detect_plug_type(codec);
3815 } else if ((priv->current_plug != PLUG_TYPE_NONE) && !insert) {
3816 priv->lpi_enabled = false;
3817 wmb();
3818
3819 /* cancel detect plug */
3820 sitar_cancel_hs_detect_plug(priv);
3821
3822 if (priv->current_plug == PLUG_TYPE_HEADPHONE) {
3823 sitar_codec_report_plug(codec, 0, SND_JACK_HEADPHONE);
3824 is_removed = true;
3825 } else if (priv->current_plug == PLUG_TYPE_HEADSET) {
3826 sitar_codec_pause_hs_polling(codec);
3827 sitar_codec_cleanup_hs_polling(codec);
3828 sitar_codec_report_plug(codec, 0, SND_JACK_HEADSET);
3829 is_removed = true;
3830 }
3831
3832 if (is_removed) {
3833 /* Enable Mic Bias pull down and HPH Switch to GND */
3834 snd_soc_update_bits(codec,
3835 priv->mbhc_bias_regs.ctl_reg, 0x01,
3836 0x01);
3837 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x01,
3838 0x01);
3839 /* Make sure mic trigger is turned off */
3840 snd_soc_update_bits(codec,
3841 priv->mbhc_bias_regs.ctl_reg,
3842 0x01, 0x01);
3843 snd_soc_update_bits(codec,
3844 priv->mbhc_bias_regs.mbhc_reg,
3845 0x90, 0x00);
3846 /* Reset MBHC State Machine */
3847 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL,
3848 0x08, 0x08);
3849 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL,
3850 0x08, 0x00);
3851 /* Turn off override */
3852 sitar_turn_onoff_override(codec, false);
3853 }
3854 }
3855
3856 priv->in_gpio_handler = false;
3857 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
3858 pr_debug("%s: leave\n", __func__);
3859}
3860
3861static irqreturn_t sitar_mechanical_plug_detect_irq(int irq, void *data)
3862{
3863 int r = IRQ_HANDLED;
3864 struct snd_soc_codec *codec = data;
3865
3866 if (unlikely(wcd9xxx_lock_sleep(codec->control_data) == false)) {
3867 pr_warn("%s(): Failed to hold suspend\n", __func__);
3868 r = IRQ_NONE;
3869 } else {
3870 sitar_hs_gpio_handler(codec);
3871 wcd9xxx_unlock_sleep(codec->control_data);
3872 }
3873 return r;
3874}
3875
3876static int sitar_mbhc_init_and_calibrate(struct snd_soc_codec *codec)
3877{
3878 int rc = 0;
3879 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3880
3881 sitar->mbhc_cfg.mclk_cb_fn(codec, 1, false);
3882 sitar_mbhc_init(codec);
3883 sitar_mbhc_cal(codec);
3884 sitar_mbhc_calc_thres(codec);
3885 sitar->mbhc_cfg.mclk_cb_fn(codec, 0, false);
3886 sitar_codec_calibrate_hs_polling(codec);
3887
3888 /* Enable Mic Bias pull down and HPH Switch to GND */
3889 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg,
3890 0x01, 0x01);
3891 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH,
3892 0x01, 0x01);
3893
3894 rc = request_threaded_irq(sitar->mbhc_cfg.gpio_irq,
3895 NULL,
3896 sitar_mechanical_plug_detect_irq,
3897 (IRQF_TRIGGER_RISING |
3898 IRQF_TRIGGER_FALLING),
3899 "sitar-hs-gpio", codec);
3900
3901 if (!IS_ERR_VALUE(rc)) {
3902 rc = enable_irq_wake(sitar->mbhc_cfg.gpio_irq);
3903 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL,
3904 0x10, 0x10);
3905 wcd9xxx_enable_irq(codec->control_data,
3906 SITAR_IRQ_HPH_PA_OCPL_FAULT);
3907 wcd9xxx_enable_irq(codec->control_data,
3908 SITAR_IRQ_HPH_PA_OCPR_FAULT);
3909 /* Bootup time detection */
3910 sitar_hs_gpio_handler(codec);
3911 }
3912
3913 return rc;
3914}
3915
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003916static void mbhc_fw_read(struct work_struct *work)
3917{
3918 struct delayed_work *dwork;
3919 struct sitar_priv *sitar;
3920 struct snd_soc_codec *codec;
3921 const struct firmware *fw;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003922 int ret = -1, retry = 0;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003923
3924 dwork = to_delayed_work(work);
3925 sitar = container_of(dwork, struct sitar_priv,
3926 mbhc_firmware_dwork);
3927 codec = sitar->codec;
3928
3929 while (retry < MBHC_FW_READ_ATTEMPTS) {
3930 retry++;
3931 pr_info("%s:Attempt %d to request MBHC firmware\n",
3932 __func__, retry);
3933 ret = request_firmware(&fw, "wcd9310/wcd9310_mbhc.bin",
3934 codec->dev);
3935
3936 if (ret != 0) {
3937 usleep_range(MBHC_FW_READ_TIMEOUT,
3938 MBHC_FW_READ_TIMEOUT);
3939 } else {
3940 pr_info("%s: MBHC Firmware read succesful\n", __func__);
3941 break;
3942 }
3943 }
3944
3945 if (ret != 0) {
3946 pr_err("%s: Cannot load MBHC firmware use default cal\n",
3947 __func__);
3948 } else if (sitar_mbhc_fw_validate(fw) == false) {
3949 pr_err("%s: Invalid MBHC cal data size use default cal\n",
3950 __func__);
3951 release_firmware(fw);
3952 } else {
3953 sitar->calibration = (void *)fw->data;
3954 sitar->mbhc_fw = fw;
3955 }
3956
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003957 sitar_mbhc_init_and_calibrate(codec);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003958}
3959
3960int sitar_hs_detect(struct snd_soc_codec *codec,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003961 const struct sitar_mbhc_config *cfg)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003962{
3963 struct sitar_priv *sitar;
3964 int rc = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303965
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003966 if (!codec || !cfg->calibration) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303967 pr_err("Error: no codec or calibration\n");
3968 return -EINVAL;
3969 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003970
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003971 if (cfg->mclk_rate != SITAR_MCLK_RATE_12288KHZ) {
3972 if (cfg->mclk_rate == SITAR_MCLK_RATE_9600KHZ)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003973 pr_err("Error: clock rate %dHz is not yet supported\n",
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003974 cfg->mclk_rate);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003975 else
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003976 pr_err("Error: unsupported clock rate %d\n",
3977 cfg->mclk_rate);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003978 return -EINVAL;
3979 }
3980
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303981 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003982 sitar->mbhc_cfg = *cfg;
3983 sitar->in_gpio_handler = false;
3984 sitar->current_plug = PLUG_TYPE_NONE;
3985 sitar->lpi_enabled = false;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303986 sitar_get_mbhc_micbias_regs(codec, &sitar->mbhc_bias_regs);
3987
3988 /* Put CFILT in fast mode by default */
3989 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003990 0x40, SITAR_CFILT_FAST_MODE);
3991
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003992 INIT_DELAYED_WORK(&sitar->mbhc_firmware_dwork, mbhc_fw_read);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003993 INIT_DELAYED_WORK(&sitar->mbhc_btn_dwork, btn_lpress_fn);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303994 INIT_WORK(&sitar->hphlocp_work, hphlocp_off_report);
3995 INIT_WORK(&sitar->hphrocp_work, hphrocp_off_report);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003996 INIT_WORK(&sitar->hs_correct_plug_work,
3997 sitar_hs_correct_gpio_plug);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003998
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003999 if (!sitar->mbhc_cfg.read_fw_bin) {
4000 rc = sitar_mbhc_init_and_calibrate(codec);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004001 } else {
4002 schedule_delayed_work(&sitar->mbhc_firmware_dwork,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004003 usecs_to_jiffies(MBHC_FW_READ_TIMEOUT));
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304004 }
4005
4006 return rc;
4007}
4008EXPORT_SYMBOL_GPL(sitar_hs_detect);
4009
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004010static int sitar_determine_button(const struct sitar_priv *priv,
4011 const s32 bias_mv)
4012{
4013 s16 *v_btn_low, *v_btn_high;
4014 struct sitar_mbhc_btn_detect_cfg *btn_det;
4015 int i, btn = -1;
4016
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004017 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(priv->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004018 v_btn_low = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_V_BTN_LOW);
4019 v_btn_high = sitar_mbhc_cal_btn_det_mp(btn_det,
4020 SITAR_BTN_DET_V_BTN_HIGH);
4021 for (i = 0; i < btn_det->num_btn; i++) {
4022 if ((v_btn_low[i] <= bias_mv) && (v_btn_high[i] >= bias_mv)) {
4023 btn = i;
4024 break;
4025 }
4026 }
4027
4028 if (btn == -1)
4029 pr_debug("%s: couldn't find button number for mic mv %d\n",
4030 __func__, bias_mv);
4031
4032 return btn;
4033}
4034
4035static int sitar_get_button_mask(const int btn)
4036{
4037 int mask = 0;
4038 switch (btn) {
4039 case 0:
4040 mask = SND_JACK_BTN_0;
4041 break;
4042 case 1:
4043 mask = SND_JACK_BTN_1;
4044 break;
4045 case 2:
4046 mask = SND_JACK_BTN_2;
4047 break;
4048 case 3:
4049 mask = SND_JACK_BTN_3;
4050 break;
4051 case 4:
4052 mask = SND_JACK_BTN_4;
4053 break;
4054 case 5:
4055 mask = SND_JACK_BTN_5;
4056 break;
4057 case 6:
4058 mask = SND_JACK_BTN_6;
4059 break;
4060 case 7:
4061 mask = SND_JACK_BTN_7;
4062 break;
4063 }
4064 return mask;
4065}
4066
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004067
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304068static irqreturn_t sitar_dce_handler(int irq, void *data)
4069{
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004070 int i, mask;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004071 short dce, sta, bias_value_dce;
4072 s32 mv, stamv, bias_mv_dce;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004073 int btn = -1, meas = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304074 struct sitar_priv *priv = data;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004075 const struct sitar_mbhc_btn_detect_cfg *d =
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004076 SITAR_MBHC_CAL_BTN_DET_PTR(priv->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004077 short btnmeas[d->n_btn_meas + 1];
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304078 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharya1d069532012-03-07 13:25:24 -08004079 struct wcd9xxx *core = dev_get_drvdata(priv->codec->dev->parent);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004080 int n_btn_meas = d->n_btn_meas;
4081 u8 mbhc_status = snd_soc_read(codec, SITAR_A_CDC_MBHC_B1_STATUS) & 0x3E;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304082
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004083 pr_debug("%s: enter\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304084
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004085 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
4086 if (priv->mbhc_state == MBHC_STATE_POTENTIAL_RECOVERY) {
4087 pr_debug("%s: mbhc is being recovered, skip button press\n",
4088 __func__);
4089 goto done;
4090 }
4091
4092 priv->mbhc_state = MBHC_STATE_POTENTIAL;
4093
4094 if (!priv->mbhc_polling_active) {
4095 pr_warn("%s: mbhc polling is not active, skip button press\n",
4096 __func__);
4097 goto done;
4098 }
4099
4100 dce = sitar_codec_read_dce_result(codec);
4101 mv = sitar_codec_sta_dce_v(codec, 1, dce);
4102
4103 /* If GPIO interrupt already kicked in, ignore button press */
4104 if (priv->in_gpio_handler) {
4105 pr_debug("%s: GPIO State Changed, ignore button press\n",
4106 __func__);
4107 btn = -1;
4108 goto done;
4109 }
4110
4111 if (mbhc_status != SITAR_MBHC_STATUS_REL_DETECTION) {
4112 if (priv->mbhc_last_resume &&
4113 !time_after(jiffies, priv->mbhc_last_resume + HZ)) {
4114 pr_debug("%s: Button is already released shortly after "
4115 "resume\n", __func__);
4116 n_btn_meas = 0;
4117 } else {
4118 pr_debug("%s: Button is already released without "
4119 "resume", __func__);
4120 sta = sitar_codec_read_sta_result(codec);
4121 stamv = sitar_codec_sta_dce_v(codec, 0, sta);
4122 btn = sitar_determine_button(priv, mv);
4123 if (btn != sitar_determine_button(priv, stamv))
4124 btn = -1;
4125 goto done;
4126 }
4127 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304128
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004129 /* determine pressed button */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004130 btnmeas[meas++] = sitar_determine_button(priv, mv);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004131 pr_debug("%s: meas %d - DCE %d,%d, button %d\n", __func__,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004132 meas - 1, dce, mv, btnmeas[meas - 1]);
4133 if (n_btn_meas == 0)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004134 btn = btnmeas[0];
4135 for (; ((d->n_btn_meas) && (meas < (d->n_btn_meas + 1))); meas++) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004136 bias_value_dce = sitar_codec_sta_dce(codec, 1, false);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004137 bias_mv_dce = sitar_codec_sta_dce_v(codec, 1, bias_value_dce);
4138 btnmeas[meas] = sitar_determine_button(priv, bias_mv_dce);
4139 pr_debug("%s: meas %d - DCE %d,%d, button %d\n",
4140 __func__, meas, bias_value_dce, bias_mv_dce,
4141 btnmeas[meas]);
4142 /* if large enough measurements are collected,
4143 * start to check if last all n_btn_con measurements were
4144 * in same button low/high range */
4145 if (meas + 1 >= d->n_btn_con) {
4146 for (i = 0; i < d->n_btn_con; i++)
4147 if ((btnmeas[meas] < 0) ||
4148 (btnmeas[meas] != btnmeas[meas - i]))
4149 break;
4150 if (i == d->n_btn_con) {
4151 /* button pressed */
4152 btn = btnmeas[meas];
4153 break;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004154 } else if ((n_btn_meas - meas) < (d->n_btn_con - 1)) {
4155 /* if left measurements are less than n_btn_con,
4156 * it's impossible to find button number */
4157 break;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004158 }
4159 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004160 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304161
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004162 if (btn >= 0) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004163 if (priv->in_gpio_handler) {
4164 pr_debug("%s: GPIO already triggered, ignore button "
4165 "press\n", __func__);
4166 goto done;
4167 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004168 mask = sitar_get_button_mask(btn);
4169 priv->buttons_pressed |= mask;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004170 wcd9xxx_lock_sleep(core);
4171 if (schedule_delayed_work(&priv->mbhc_btn_dwork,
4172 msecs_to_jiffies(400)) == 0) {
4173 WARN(1, "Button pressed twice without release"
4174 "event\n");
4175 wcd9xxx_unlock_sleep(core);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004176 }
4177 } else {
4178 pr_debug("%s: bogus button press, too short press?\n",
4179 __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304180 }
4181
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004182 done:
4183 pr_debug("%s: leave\n", __func__);
4184 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304185 return IRQ_HANDLED;
4186}
4187
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004188static int sitar_is_fake_press(struct sitar_priv *priv)
4189{
4190 int i;
4191 int r = 0;
4192 struct snd_soc_codec *codec = priv->codec;
4193 const int dces = MBHC_NUM_DCE_PLUG_DETECT;
4194 short mb_v;
4195
4196 for (i = 0; i < dces; i++) {
4197 usleep_range(10000, 10000);
4198 if (i == 0) {
4199 mb_v = sitar_codec_sta_dce(codec, 0, true);
4200 pr_debug("%s: STA[0]: %d,%d\n", __func__, mb_v,
4201 sitar_codec_sta_dce_v(codec, 0, mb_v));
4202 if (mb_v < (short)priv->mbhc_data.v_b1_hu ||
4203 mb_v > (short)priv->mbhc_data.v_ins_hu) {
4204 r = 1;
4205 break;
4206 }
4207 } else {
4208 mb_v = sitar_codec_sta_dce(codec, 1, true);
4209 pr_debug("%s: DCE[%d]: %d,%d\n", __func__, i, mb_v,
4210 sitar_codec_sta_dce_v(codec, 1, mb_v));
4211 if (mb_v < (short)priv->mbhc_data.v_b1_h ||
4212 mb_v > (short)priv->mbhc_data.v_ins_h) {
4213 r = 1;
4214 break;
4215 }
4216 }
4217 }
4218
4219 return r;
4220}
4221
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304222static irqreturn_t sitar_release_handler(int irq, void *data)
4223{
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004224 int ret;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304225 struct sitar_priv *priv = data;
4226 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304227
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004228 pr_debug("%s: enter\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304229
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004230 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
4231 priv->mbhc_state = MBHC_STATE_RELEASE;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304232
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004233 if (priv->buttons_pressed & SITAR_JACK_BUTTON_MASK) {
4234 ret = sitar_cancel_btn_work(priv);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304235 if (ret == 0) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004236 pr_debug("%s: Reporting long button release event\n",
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004237 __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004238 if (priv->mbhc_cfg.button_jack)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304239 sitar_snd_soc_jack_report(priv,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004240 priv->mbhc_cfg.button_jack, 0,
4241 priv->buttons_pressed);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304242 } else {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004243 if (sitar_is_fake_press(priv)) {
4244 pr_debug("%s: Fake button press interrupt\n",
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004245 __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004246 } else if (priv->mbhc_cfg.button_jack) {
4247 if (priv->in_gpio_handler) {
4248 pr_debug("%s: GPIO kicked in, ignore\n",
4249 __func__);
4250 } else {
4251 pr_debug("%s: Reporting short button 0 "
4252 "press and release\n",
4253 __func__);
4254 sitar_snd_soc_jack_report(priv,
4255 priv->mbhc_cfg.button_jack,
4256 priv->buttons_pressed,
4257 priv->buttons_pressed);
4258 sitar_snd_soc_jack_report(priv,
4259 priv->mbhc_cfg.button_jack, 0,
4260 priv->buttons_pressed);
4261 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304262 }
4263 }
4264
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004265 priv->buttons_pressed &= ~SITAR_JACK_BUTTON_MASK;
4266 }
4267
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004268 sitar_codec_calibrate_hs_polling(codec);
4269
4270 if (priv->mbhc_cfg.gpio)
4271 msleep(SITAR_MBHC_GPIO_REL_DEBOUNCE_TIME_MS);
4272
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304273 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004274
4275 pr_debug("%s: leave\n", __func__);
4276 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
4277
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304278 return IRQ_HANDLED;
4279}
4280
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304281static irqreturn_t sitar_hphl_ocp_irq(int irq, void *data)
4282{
4283 struct sitar_priv *sitar = data;
4284 struct snd_soc_codec *codec;
4285
4286 pr_info("%s: received HPHL OCP irq\n", __func__);
4287
4288 if (sitar) {
4289 codec = sitar->codec;
4290 if (sitar->hphlocp_cnt++ < SITAR_OCP_ATTEMPT) {
4291 pr_info("%s: retry\n", __func__);
4292 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004293 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304294 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004295 0x10);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304296 } else {
4297 wcd9xxx_disable_irq(codec->control_data,
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004298 SITAR_IRQ_HPH_PA_OCPL_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304299 sitar->hphlocp_cnt = 0;
4300 sitar->hph_status |= SND_JACK_OC_HPHL;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004301 if (sitar->mbhc_cfg.headset_jack)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304302 sitar_snd_soc_jack_report(sitar,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004303 sitar->mbhc_cfg.headset_jack,
4304 sitar->hph_status,
4305 SITAR_JACK_MASK);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304306 }
4307 } else {
4308 pr_err("%s: Bad sitar private data\n", __func__);
4309 }
4310
4311 return IRQ_HANDLED;
4312}
4313
4314static irqreturn_t sitar_hphr_ocp_irq(int irq, void *data)
4315{
4316 struct sitar_priv *sitar = data;
4317 struct snd_soc_codec *codec;
4318
4319 pr_info("%s: received HPHR OCP irq\n", __func__);
4320
4321 if (sitar) {
4322 codec = sitar->codec;
4323 if (sitar->hphrocp_cnt++ < SITAR_OCP_ATTEMPT) {
4324 pr_info("%s: retry\n", __func__);
4325 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
4326 0x00);
4327 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
4328 0x10);
4329 } else {
4330 wcd9xxx_disable_irq(codec->control_data,
4331 SITAR_IRQ_HPH_PA_OCPR_FAULT);
4332 sitar->hphrocp_cnt = 0;
4333 sitar->hph_status |= SND_JACK_OC_HPHR;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004334 if (sitar->mbhc_cfg.headset_jack)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304335 sitar_snd_soc_jack_report(sitar,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004336 sitar->mbhc_cfg.headset_jack,
4337 sitar->hph_status,
4338 SITAR_JACK_MASK);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304339 }
4340 } else {
4341 pr_err("%s: Bad sitar private data\n", __func__);
4342 }
4343
4344 return IRQ_HANDLED;
4345}
4346
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304347static irqreturn_t sitar_hs_insert_irq(int irq, void *data)
4348{
4349 struct sitar_priv *priv = data;
4350 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304351
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004352 pr_debug("%s: enter\n", __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004353 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304354 wcd9xxx_disable_irq(codec->control_data, SITAR_IRQ_MBHC_INSERTION);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304355
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304356 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_INT_CTL, 0x03, 0x00);
4357
4358 /* Turn off both HPH and MIC line schmitt triggers */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004359 snd_soc_update_bits(codec, priv->mbhc_bias_regs.mbhc_reg, 0x90, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304360 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x13, 0x00);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004361 snd_soc_update_bits(codec, priv->mbhc_bias_regs.ctl_reg, 0x01, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304362
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004363 pr_debug("%s: MIC trigger insertion interrupt\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304364
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004365 rmb();
4366 if (priv->lpi_enabled)
4367 msleep(100);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304368
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004369 rmb();
4370 if (!priv->lpi_enabled) {
4371 pr_debug("%s: lpi is disabled\n", __func__);
4372 } else if (gpio_get_value_cansleep(priv->mbhc_cfg.gpio) ==
4373 priv->mbhc_cfg.gpio_level_insert) {
4374 pr_debug("%s: Valid insertion, "
4375 "detect plug type\n", __func__);
4376 sitar_codec_decide_gpio_plug(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304377 } else {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004378 pr_debug("%s: Invalid insertion, "
4379 "stop plug detection\n", __func__);
4380 }
4381 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
4382 return IRQ_HANDLED;
4383}
4384
4385static bool is_valid_mic_voltage(struct snd_soc_codec *codec, s32 mic_mv)
4386{
4387 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4388 struct sitar_mbhc_plug_type_cfg *plug_type =
4389 SITAR_MBHC_CAL_PLUG_TYPE_PTR(sitar->mbhc_cfg.calibration);
4390
4391 return (!(mic_mv > SITAR_MBHC_FAKE_INSERT_LOW
4392 && mic_mv < SITAR_MBHC_FAKE_INSERT_HIGH)
4393 && (mic_mv > plug_type->v_no_mic)
4394 && (mic_mv < plug_type->v_hs_max)) ? true : false;
4395}
4396
4397/* called under codec_resource_lock acquisition
4398 * returns true if mic voltage range is back to normal insertion
4399 * returns false either if timedout or removed */
4400static bool sitar_hs_remove_settle(struct snd_soc_codec *codec)
4401{
4402 int i;
4403 bool timedout, settled = false;
4404 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT];
4405 short mb_v[MBHC_NUM_DCE_PLUG_DETECT];
4406 unsigned long retry = 0, timeout;
4407 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4408
4409 timeout = jiffies + msecs_to_jiffies(SITAR_HS_DETECT_PLUG_TIME_MS);
4410 while (!(timedout = time_after(jiffies, timeout))) {
4411 retry++;
4412 if (sitar_hs_gpio_level_remove(sitar)) {
4413 pr_debug("%s: GPIO indicates removal\n", __func__);
4414 break;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304415 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004416
4417 if (retry > 1)
4418 msleep(250);
4419 else
4420 msleep(50);
4421
4422 if (sitar_hs_gpio_level_remove(sitar)) {
4423 pr_debug("%s: GPIO indicates removal\n", __func__);
4424 break;
4425 }
4426
4427 sitar_turn_onoff_override(codec, true);
4428 for (i = 0; i < MBHC_NUM_DCE_PLUG_DETECT; i++) {
4429 mb_v[i] = __sitar_codec_sta_dce(codec, 1, true, true);
4430 mic_mv[i] = sitar_codec_sta_dce_v(codec, 1 , mb_v[i]);
4431 pr_debug("%s : DCE run %lu, mic_mv = %d(%x)\n",
4432 __func__, retry, mic_mv[i], mb_v[i]);
4433 }
4434 sitar_turn_onoff_override(codec, false);
4435
4436 if (sitar_hs_gpio_level_remove(sitar)) {
4437 pr_debug("%s: GPIO indicates removal\n", __func__);
4438 break;
4439 }
4440
4441 for (i = 0; i < MBHC_NUM_DCE_PLUG_DETECT; i++)
4442 if (!is_valid_mic_voltage(codec, mic_mv[i]))
4443 break;
4444
4445 if (i == MBHC_NUM_DCE_PLUG_DETECT) {
4446 pr_debug("%s: MIC voltage settled\n", __func__);
4447 settled = true;
4448 msleep(200);
4449 break;
4450 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304451 }
4452
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004453 if (timedout)
4454 pr_debug("%s: Microphone did not settle in %d seconds\n",
4455 __func__, SITAR_HS_DETECT_PLUG_TIME_MS);
4456 return settled;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304457}
4458
4459static irqreturn_t sitar_hs_remove_irq(int irq, void *data)
4460{
4461 struct sitar_priv *priv = data;
4462 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304463
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004464 pr_debug("%s: enter, removal interrupt\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304465
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004466 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
4467 if (sitar_hs_remove_settle(codec))
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304468 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004469 pr_debug("%s: remove settle done\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304470
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004471 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304472 return IRQ_HANDLED;
4473}
4474
4475
4476static unsigned long slimbus_value;
4477
4478static irqreturn_t sitar_slimbus_irq(int irq, void *data)
4479{
4480 struct sitar_priv *priv = data;
4481 struct snd_soc_codec *codec = priv->codec;
4482 int i, j;
4483 u8 val;
4484
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304485
4486 for (i = 0; i < WCD9XXX_SLIM_NUM_PORT_REG; i++) {
4487 slimbus_value = wcd9xxx_interface_reg_read(codec->control_data,
4488 SITAR_SLIM_PGD_PORT_INT_STATUS0 + i);
4489 for_each_set_bit(j, &slimbus_value, BITS_PER_BYTE) {
4490 val = wcd9xxx_interface_reg_read(codec->control_data,
4491 SITAR_SLIM_PGD_PORT_INT_SOURCE0 + i*8 + j);
4492 if (val & 0x1)
4493 pr_err_ratelimited("overflow error on port %x,"
4494 " value %x\n", i*8 + j, val);
4495 if (val & 0x2)
4496 pr_err_ratelimited("underflow error on port %x,"
4497 " value %x\n", i*8 + j, val);
4498 }
4499 wcd9xxx_interface_reg_write(codec->control_data,
4500 SITAR_SLIM_PGD_PORT_INT_CLR0 + i, 0xFF);
4501 }
4502
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304503 return IRQ_HANDLED;
4504}
4505
4506
4507static int sitar_handle_pdata(struct sitar_priv *sitar)
4508{
4509 struct snd_soc_codec *codec = sitar->codec;
4510 struct wcd9xxx_pdata *pdata = sitar->pdata;
4511 int k1, k2, rc = 0;
4512 u8 leg_mode = pdata->amic_settings.legacy_mode;
4513 u8 txfe_bypass = pdata->amic_settings.txfe_enable;
4514 u8 txfe_buff = pdata->amic_settings.txfe_buff;
4515 u8 flag = pdata->amic_settings.use_pdata;
4516 u8 i = 0, j = 0;
4517 u8 val_txfe = 0, value = 0;
4518
4519 if (!pdata) {
4520 rc = -ENODEV;
4521 goto done;
4522 }
4523
4524 /* Make sure settings are correct */
4525 if ((pdata->micbias.ldoh_v > SITAR_LDOH_2P85_V) ||
4526 (pdata->micbias.bias1_cfilt_sel > SITAR_CFILT2_SEL) ||
4527 (pdata->micbias.bias2_cfilt_sel > SITAR_CFILT2_SEL)) {
4528 rc = -EINVAL;
4529 goto done;
4530 }
4531
4532 /* figure out k value */
4533 k1 = sitar_find_k_value(pdata->micbias.ldoh_v,
4534 pdata->micbias.cfilt1_mv);
4535 k2 = sitar_find_k_value(pdata->micbias.ldoh_v,
4536 pdata->micbias.cfilt2_mv);
4537
4538 if (IS_ERR_VALUE(k1) || IS_ERR_VALUE(k2)) {
4539 rc = -EINVAL;
4540 goto done;
4541 }
4542
4543 /* Set voltage level and always use LDO */
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07004544 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1, 0x0C,
4545 (pdata->micbias.ldoh_v << 2));
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304546
4547 snd_soc_update_bits(codec, SITAR_A_MICB_CFILT_1_VAL, 0xFC,
4548 (k1 << 2));
4549 snd_soc_update_bits(codec, SITAR_A_MICB_CFILT_2_VAL, 0xFC,
4550 (k2 << 2));
4551
4552 snd_soc_update_bits(codec, SITAR_A_MICB_1_CTL, 0x60,
4553 (pdata->micbias.bias1_cfilt_sel << 5));
4554 snd_soc_update_bits(codec, SITAR_A_MICB_2_CTL, 0x60,
4555 (pdata->micbias.bias2_cfilt_sel << 5));
4556
4557 for (i = 0; i < 6; j++, i += 2) {
4558 if (flag & (0x01 << i)) {
4559 value = (leg_mode & (0x01 << i)) ? 0x10 : 0x00;
4560 val_txfe = (txfe_bypass & (0x01 << i)) ? 0x20 : 0x00;
4561 val_txfe = val_txfe |
4562 ((txfe_buff & (0x01 << i)) ? 0x10 : 0x00);
4563 snd_soc_update_bits(codec, SITAR_A_TX_1_2_EN + j * 10,
4564 0x10, value);
4565 snd_soc_update_bits(codec,
4566 SITAR_A_TX_1_2_TEST_EN + j * 10,
4567 0x30, val_txfe);
4568 }
4569 if (flag & (0x01 << (i + 1))) {
4570 value = (leg_mode & (0x01 << (i + 1))) ? 0x01 : 0x00;
4571 val_txfe = (txfe_bypass &
4572 (0x01 << (i + 1))) ? 0x02 : 0x00;
4573 val_txfe |= (txfe_buff &
4574 (0x01 << (i + 1))) ? 0x01 : 0x00;
4575 snd_soc_update_bits(codec, SITAR_A_TX_1_2_EN + j * 10,
4576 0x01, value);
4577 snd_soc_update_bits(codec,
4578 SITAR_A_TX_1_2_TEST_EN + j * 10,
4579 0x03, val_txfe);
4580 }
4581 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004582 if (flag & 0x40) {
4583 value = (leg_mode & 0x40) ? 0x10 : 0x00;
4584 value = value | ((txfe_bypass & 0x40) ? 0x02 : 0x00);
4585 value = value | ((txfe_buff & 0x40) ? 0x01 : 0x00);
4586 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN,
4587 0x13, value);
4588 }
4589
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304590
4591 if (pdata->ocp.use_pdata) {
4592 /* not defined in CODEC specification */
4593 if (pdata->ocp.hph_ocp_limit == 1 ||
4594 pdata->ocp.hph_ocp_limit == 5) {
4595 rc = -EINVAL;
4596 goto done;
4597 }
4598 snd_soc_update_bits(codec, SITAR_A_RX_COM_OCP_CTL,
4599 0x0F, pdata->ocp.num_attempts);
4600 snd_soc_write(codec, SITAR_A_RX_COM_OCP_COUNT,
4601 ((pdata->ocp.run_time << 4) | pdata->ocp.wait_time));
4602 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL,
4603 0xE0, (pdata->ocp.hph_ocp_limit << 5));
4604 }
4605done:
4606 return rc;
4607}
4608
4609static const struct sitar_reg_mask_val sitar_1_1_reg_defaults[] = {
4610
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304611 SITAR_REG_VAL(SITAR_A_MICB_1_INT_RBIAS, 0x24),
4612 SITAR_REG_VAL(SITAR_A_MICB_2_INT_RBIAS, 0x24),
4613
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304614 SITAR_REG_VAL(SITAR_A_RX_HPH_BIAS_PA, 0x57),
4615 SITAR_REG_VAL(SITAR_A_RX_HPH_BIAS_LDO, 0x56),
4616
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304617 SITAR_REG_VAL(SITAR_A_RX_EAR_BIAS_PA, 0xA6),
4618 SITAR_REG_VAL(SITAR_A_RX_EAR_GAIN, 0x02),
4619 SITAR_REG_VAL(SITAR_A_RX_EAR_VCM, 0x03),
4620
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304621 SITAR_REG_VAL(SITAR_A_CDC_RX1_B5_CTL, 0x78),
Asish Bhattacharya2e1d7522012-05-25 15:08:40 +05304622 SITAR_REG_VAL(SITAR_A_CDC_RX2_B5_CTL, 0x78),
4623 SITAR_REG_VAL(SITAR_A_CDC_RX3_B5_CTL, 0x78),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304624
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304625 SITAR_REG_VAL(SITAR_A_CDC_RX1_B6_CTL, 0x80),
4626
4627 SITAR_REG_VAL(SITAR_A_CDC_CLSG_FREQ_THRESH_B3_CTL, 0x1B),
4628
4629};
4630
4631static void sitar_update_reg_defaults(struct snd_soc_codec *codec)
4632{
4633 u32 i;
4634 for (i = 0; i < ARRAY_SIZE(sitar_1_1_reg_defaults); i++)
4635 snd_soc_write(codec, sitar_1_1_reg_defaults[i].reg,
4636 sitar_1_1_reg_defaults[i].val);
4637
4638}
4639static const struct sitar_reg_mask_val sitar_codec_reg_init_val[] = {
4640 /* Initialize current threshold to 350MA
4641 * number of wait and run cycles to 4096
4642 */
4643 {SITAR_A_RX_HPH_OCP_CTL, 0xF8, 0x60},
4644 {SITAR_A_RX_COM_OCP_COUNT, 0xFF, 0xFF},
4645
4646 {SITAR_A_QFUSE_CTL, 0xFF, 0x03},
4647
4648 /* Initialize gain registers to use register gain */
4649 {SITAR_A_RX_HPH_L_GAIN, 0x10, 0x10},
4650 {SITAR_A_RX_HPH_R_GAIN, 0x10, 0x10},
4651 {SITAR_A_RX_LINE_1_GAIN, 0x10, 0x10},
4652 {SITAR_A_RX_LINE_2_GAIN, 0x10, 0x10},
4653
4654 /* Initialize mic biases to differential mode */
4655 {SITAR_A_MICB_1_INT_RBIAS, 0x24, 0x24},
4656 {SITAR_A_MICB_2_INT_RBIAS, 0x24, 0x24},
4657
4658 {SITAR_A_CDC_CONN_CLSG_CTL, 0x3C, 0x14},
4659
4660 /* Use 16 bit sample size for TX1 to TX6 */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004661 {SITAR_A_CDC_CONN_TX_SB_B1_CTL, 0x30, 0x20},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304662 {SITAR_A_CDC_CONN_TX_SB_B2_CTL, 0x30, 0x20},
4663 {SITAR_A_CDC_CONN_TX_SB_B3_CTL, 0x30, 0x20},
4664 {SITAR_A_CDC_CONN_TX_SB_B4_CTL, 0x30, 0x20},
4665 {SITAR_A_CDC_CONN_TX_SB_B5_CTL, 0x30, 0x20},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004666 {SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 0x1, 0x1},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304667
4668 /* Use 16 bit sample size for RX */
4669 {SITAR_A_CDC_CONN_RX_SB_B1_CTL, 0xFF, 0xAA},
4670 {SITAR_A_CDC_CONN_RX_SB_B2_CTL, 0x02, 0x02},
4671
4672 /*enable HPF filter for TX paths */
4673 {SITAR_A_CDC_TX1_MUX_CTL, 0x8, 0x0},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004674 {SITAR_A_CDC_TX2_MUX_CTL, 0x8, 0x0},
Bhalchandra Gajare2776af12012-04-27 16:59:39 -07004675
4676 /*enable External clock select*/
4677 {SITAR_A_CDC_CLK_MCLK_CTL, 0x01, 0x01},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304678};
4679
4680static void sitar_codec_init_reg(struct snd_soc_codec *codec)
4681{
4682 u32 i;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304683 for (i = 0; i < ARRAY_SIZE(sitar_codec_reg_init_val); i++)
4684 snd_soc_update_bits(codec, sitar_codec_reg_init_val[i].reg,
4685 sitar_codec_reg_init_val[i].mask,
4686 sitar_codec_reg_init_val[i].val);
4687}
4688
4689static int sitar_codec_probe(struct snd_soc_codec *codec)
4690{
4691 struct sitar *control;
4692 struct sitar_priv *sitar;
4693 struct snd_soc_dapm_context *dapm = &codec->dapm;
4694 int ret = 0;
4695 int i;
4696 u8 sitar_version;
4697 int ch_cnt;
4698
4699 codec->control_data = dev_get_drvdata(codec->dev->parent);
4700 control = codec->control_data;
4701
4702 sitar = kzalloc(sizeof(struct sitar_priv), GFP_KERNEL);
4703 if (!sitar) {
4704 dev_err(codec->dev, "Failed to allocate private data\n");
4705 return -ENOMEM;
4706 }
4707
4708 /* Make sure mbhc micbias register addresses are zeroed out */
4709 memset(&sitar->mbhc_bias_regs, 0,
4710 sizeof(struct mbhc_micbias_regs));
4711 sitar->cfilt_k_value = 0;
4712 sitar->mbhc_micbias_switched = false;
4713
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004714 /* Make sure mbhc intenal calibration data is zeroed out */
4715 memset(&sitar->mbhc_data, 0,
4716 sizeof(struct mbhc_internal_cal_data));
4717 sitar->mbhc_data.t_sta_dce = DEFAULT_DCE_STA_WAIT;
4718 sitar->mbhc_data.t_dce = DEFAULT_DCE_WAIT;
4719 sitar->mbhc_data.t_sta = DEFAULT_STA_WAIT;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304720 snd_soc_codec_set_drvdata(codec, sitar);
4721
4722 sitar->mclk_enabled = false;
4723 sitar->bandgap_type = SITAR_BANDGAP_OFF;
4724 sitar->clock_active = false;
4725 sitar->config_mode_active = false;
4726 sitar->mbhc_polling_active = false;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304727 sitar->no_mic_headset_override = false;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004728 mutex_init(&sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304729 sitar->codec = codec;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004730 sitar->mbhc_state = MBHC_STATE_NONE;
4731 sitar->mbhc_last_resume = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304732 sitar->pdata = dev_get_platdata(codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304733 sitar_update_reg_defaults(codec);
4734 sitar_codec_init_reg(codec);
4735
4736 ret = sitar_handle_pdata(sitar);
4737 if (IS_ERR_VALUE(ret)) {
4738 pr_err("%s: bad pdata\n", __func__);
4739 goto err_pdata;
4740 }
4741
4742 snd_soc_add_controls(codec, sitar_snd_controls,
4743 ARRAY_SIZE(sitar_snd_controls));
4744 snd_soc_dapm_new_controls(dapm, sitar_dapm_widgets,
4745 ARRAY_SIZE(sitar_dapm_widgets));
4746 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
4747
4748 sitar_version = snd_soc_read(codec, WCD9XXX_A_CHIP_VERSION);
4749 pr_info("%s : Sitar version reg 0x%2x\n", __func__, (u32)sitar_version);
4750
4751 sitar_version &= 0x1F;
4752 pr_info("%s : Sitar version %u\n", __func__, (u32)sitar_version);
4753
4754 snd_soc_dapm_sync(dapm);
4755
4756
4757 ret = wcd9xxx_request_irq(codec->control_data, SITAR_IRQ_MBHC_INSERTION,
4758 sitar_hs_insert_irq, "Headset insert detect", sitar);
4759 if (ret) {
4760 pr_err("%s: Failed to request irq %d\n", __func__,
4761 SITAR_IRQ_MBHC_INSERTION);
4762 goto err_insert_irq;
4763 }
4764 wcd9xxx_disable_irq(codec->control_data, SITAR_IRQ_MBHC_INSERTION);
4765
4766 ret = wcd9xxx_request_irq(codec->control_data, SITAR_IRQ_MBHC_REMOVAL,
4767 sitar_hs_remove_irq, "Headset remove detect", sitar);
4768 if (ret) {
4769 pr_err("%s: Failed to request irq %d\n", __func__,
4770 SITAR_IRQ_MBHC_REMOVAL);
4771 goto err_remove_irq;
4772 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304773
4774 ret = wcd9xxx_request_irq(codec->control_data, SITAR_IRQ_MBHC_POTENTIAL,
4775 sitar_dce_handler, "DC Estimation detect", sitar);
4776 if (ret) {
4777 pr_err("%s: Failed to request irq %d\n", __func__,
4778 SITAR_IRQ_MBHC_POTENTIAL);
4779 goto err_potential_irq;
4780 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304781
4782 ret = wcd9xxx_request_irq(codec->control_data, SITAR_IRQ_MBHC_RELEASE,
4783 sitar_release_handler, "Button Release detect", sitar);
4784 if (ret) {
4785 pr_err("%s: Failed to request irq %d\n", __func__,
4786 SITAR_IRQ_MBHC_RELEASE);
4787 goto err_release_irq;
4788 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304789
4790 ret = wcd9xxx_request_irq(codec->control_data, SITAR_IRQ_SLIMBUS,
4791 sitar_slimbus_irq, "SLIMBUS Slave", sitar);
4792 if (ret) {
4793 pr_err("%s: Failed to request irq %d\n", __func__,
4794 SITAR_IRQ_SLIMBUS);
4795 goto err_slimbus_irq;
4796 }
4797
4798 for (i = 0; i < WCD9XXX_SLIM_NUM_PORT_REG; i++)
4799 wcd9xxx_interface_reg_write(codec->control_data,
4800 SITAR_SLIM_PGD_PORT_INT_EN0 + i, 0xFF);
4801
4802
4803 ret = wcd9xxx_request_irq(codec->control_data,
4804 SITAR_IRQ_HPH_PA_OCPL_FAULT, sitar_hphl_ocp_irq,
4805 "HPH_L OCP detect", sitar);
4806 if (ret) {
4807 pr_err("%s: Failed to request irq %d\n", __func__,
4808 SITAR_IRQ_HPH_PA_OCPL_FAULT);
4809 goto err_hphl_ocp_irq;
4810 }
4811 wcd9xxx_disable_irq(codec->control_data, SITAR_IRQ_HPH_PA_OCPL_FAULT);
4812
4813 ret = wcd9xxx_request_irq(codec->control_data,
4814 SITAR_IRQ_HPH_PA_OCPR_FAULT, sitar_hphr_ocp_irq,
4815 "HPH_R OCP detect", sitar);
4816 if (ret) {
4817 pr_err("%s: Failed to request irq %d\n", __func__,
4818 SITAR_IRQ_HPH_PA_OCPR_FAULT);
4819 goto err_hphr_ocp_irq;
4820 }
4821 wcd9xxx_disable_irq(codec->control_data, SITAR_IRQ_HPH_PA_OCPR_FAULT);
4822
4823 for (i = 0; i < ARRAY_SIZE(sitar_dai); i++) {
4824 switch (sitar_dai[i].id) {
4825 case AIF1_PB:
4826 ch_cnt = sitar_dai[i].playback.channels_max;
4827 break;
4828 case AIF1_CAP:
4829 ch_cnt = sitar_dai[i].capture.channels_max;
4830 break;
4831 default:
4832 continue;
4833 }
4834 sitar->dai[i].ch_num = kzalloc((sizeof(unsigned int)*
4835 ch_cnt), GFP_KERNEL);
4836 }
4837
4838#ifdef CONFIG_DEBUG_FS
4839 debug_sitar_priv = sitar;
4840#endif
4841
4842 return ret;
4843
4844err_hphr_ocp_irq:
4845 wcd9xxx_free_irq(codec->control_data,
4846 SITAR_IRQ_HPH_PA_OCPL_FAULT, sitar);
4847err_hphl_ocp_irq:
4848 wcd9xxx_free_irq(codec->control_data,
4849 SITAR_IRQ_SLIMBUS, sitar);
4850err_slimbus_irq:
4851 wcd9xxx_free_irq(codec->control_data,
4852 SITAR_IRQ_MBHC_RELEASE, sitar);
4853err_release_irq:
4854 wcd9xxx_free_irq(codec->control_data,
4855 SITAR_IRQ_MBHC_POTENTIAL, sitar);
4856err_potential_irq:
4857 wcd9xxx_free_irq(codec->control_data,
4858 SITAR_IRQ_MBHC_REMOVAL, sitar);
4859err_remove_irq:
4860 wcd9xxx_free_irq(codec->control_data,
4861 SITAR_IRQ_MBHC_INSERTION, sitar);
4862err_insert_irq:
4863err_pdata:
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004864 mutex_destroy(&sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304865 kfree(sitar);
4866 return ret;
4867}
4868static int sitar_codec_remove(struct snd_soc_codec *codec)
4869{
4870 int i;
4871 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4872 wcd9xxx_free_irq(codec->control_data, SITAR_IRQ_SLIMBUS, sitar);
4873 wcd9xxx_free_irq(codec->control_data, SITAR_IRQ_MBHC_RELEASE, sitar);
4874 wcd9xxx_free_irq(codec->control_data, SITAR_IRQ_MBHC_POTENTIAL, sitar);
4875 wcd9xxx_free_irq(codec->control_data, SITAR_IRQ_MBHC_REMOVAL, sitar);
4876 wcd9xxx_free_irq(codec->control_data, SITAR_IRQ_MBHC_INSERTION, sitar);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004877 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304878 sitar_codec_disable_clock_block(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004879 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304880 sitar_codec_enable_bandgap(codec, SITAR_BANDGAP_OFF);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004881 if (sitar->mbhc_fw)
4882 release_firmware(sitar->mbhc_fw);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304883 for (i = 0; i < ARRAY_SIZE(sitar_dai); i++)
4884 kfree(sitar->dai[i].ch_num);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004885 mutex_destroy(&sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304886 kfree(sitar);
4887 return 0;
4888}
4889static struct snd_soc_codec_driver soc_codec_dev_sitar = {
4890 .probe = sitar_codec_probe,
4891 .remove = sitar_codec_remove,
4892 .read = sitar_read,
4893 .write = sitar_write,
4894
4895 .readable_register = sitar_readable,
4896 .volatile_register = sitar_volatile,
4897
4898 .reg_cache_size = SITAR_CACHE_SIZE,
4899 .reg_cache_default = sitar_reg_defaults,
4900 .reg_word_size = 1,
4901};
4902
4903#ifdef CONFIG_DEBUG_FS
4904static struct dentry *debugfs_poke;
4905
4906static int codec_debug_open(struct inode *inode, struct file *file)
4907{
4908 file->private_data = inode->i_private;
4909 return 0;
4910}
4911
4912static ssize_t codec_debug_write(struct file *filp,
4913 const char __user *ubuf, size_t cnt, loff_t *ppos)
4914{
4915 char lbuf[32];
4916 char *buf;
4917 int rc;
4918
4919 if (cnt > sizeof(lbuf) - 1)
4920 return -EINVAL;
4921
4922 rc = copy_from_user(lbuf, ubuf, cnt);
4923 if (rc)
4924 return -EFAULT;
4925
4926 lbuf[cnt] = '\0';
4927 buf = (char *)lbuf;
4928 debug_sitar_priv->no_mic_headset_override = (*strsep(&buf, " ") == '0')
4929 ? false : true;
4930
4931 return rc;
4932}
4933
4934static const struct file_operations codec_debug_ops = {
4935 .open = codec_debug_open,
4936 .write = codec_debug_write,
4937};
4938#endif
4939
4940#ifdef CONFIG_PM
4941static int sitar_suspend(struct device *dev)
4942{
Asish Bhattacharya1d069532012-03-07 13:25:24 -08004943 dev_dbg(dev, "%s: system suspend\n", __func__);
4944 return 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304945}
4946
4947static int sitar_resume(struct device *dev)
4948{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004949 struct platform_device *pdev = to_platform_device(dev);
4950 struct sitar_priv *sitar = platform_get_drvdata(pdev);
Asish Bhattacharya1d069532012-03-07 13:25:24 -08004951 dev_dbg(dev, "%s: system resume\n", __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004952 sitar->mbhc_last_resume = jiffies;
Asish Bhattacharya1d069532012-03-07 13:25:24 -08004953 return 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304954}
4955
4956static const struct dev_pm_ops sitar_pm_ops = {
4957 .suspend = sitar_suspend,
4958 .resume = sitar_resume,
4959};
4960#endif
4961
4962static int __devinit sitar_probe(struct platform_device *pdev)
4963{
4964 int ret = 0;
4965 pr_err("%s\n", __func__);
4966#ifdef CONFIG_DEBUG_FS
4967 debugfs_poke = debugfs_create_file("TRRS",
4968 S_IFREG | S_IRUGO, NULL, (void *) "TRRS", &codec_debug_ops);
4969
4970#endif
4971 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_sitar,
4972 sitar_dai, ARRAY_SIZE(sitar_dai));
4973 return ret;
4974}
4975static int __devexit sitar_remove(struct platform_device *pdev)
4976{
4977 snd_soc_unregister_codec(&pdev->dev);
4978
4979#ifdef CONFIG_DEBUG_FS
4980 debugfs_remove(debugfs_poke);
4981#endif
4982 return 0;
4983}
4984static struct platform_driver sitar_codec_driver = {
4985 .probe = sitar_probe,
4986 .remove = sitar_remove,
4987 .driver = {
4988 .name = "sitar_codec",
4989 .owner = THIS_MODULE,
4990#ifdef CONFIG_PM
4991 .pm = &sitar_pm_ops,
4992#endif
4993 },
4994};
4995
4996static int __init sitar_codec_init(void)
4997{
4998 return platform_driver_register(&sitar_codec_driver);
4999}
5000
5001static void __exit sitar_codec_exit(void)
5002{
5003 platform_driver_unregister(&sitar_codec_driver);
5004}
5005
5006module_init(sitar_codec_init);
5007module_exit(sitar_codec_exit);
5008
5009MODULE_DESCRIPTION("Sitar codec driver");
5010MODULE_VERSION("1.0");
5011MODULE_LICENSE("GPL v2");