blob: fa7abb196ff53c106d25c7f13fa4d2058b4ebba6 [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>
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -070036#include <linux/wait.h>
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053037#include "wcd9304.h"
38
39#define WCD9304_RATES (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|\
40 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_48000)
Asish Bhattacharyab9afc732012-08-01 15:11:24 +053041#define ADC_DMIC_SEL_ADC 0
42#define ADC_DMIC_SEL_DMIC 1
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053043
Asish Bhattacharya34aa1982012-09-20 14:24:05 +053044#define NUM_AMIC 3
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053045#define NUM_DECIMATORS 4
46#define NUM_INTERPOLATORS 3
47#define BITS_PER_REG 8
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -080048#define SITAR_CFILT_FAST_MODE 0x00
49#define SITAR_CFILT_SLOW_MODE 0x40
50#define MBHC_FW_READ_ATTEMPTS 15
51#define MBHC_FW_READ_TIMEOUT 2000000
52
53#define SITAR_JACK_MASK (SND_JACK_HEADSET | SND_JACK_OC_HPHL | SND_JACK_OC_HPHR)
54
55#define SITAR_I2S_MASTER_MODE_MASK 0x08
56
57#define SITAR_OCP_ATTEMPT 1
58
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053059#define AIF1_PB 1
60#define AIF1_CAP 2
61#define NUM_CODEC_DAIS 2
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -070062#define SLIM_CLOSE_TIMEOUT 1000
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053063
64struct sitar_codec_dai_data {
65 u32 rate;
66 u32 *ch_num;
67 u32 ch_act;
68 u32 ch_tot;
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -070069 u32 ch_mask;
70 wait_queue_head_t dai_wait;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053071};
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053072
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -080073#define SITAR_MCLK_RATE_12288KHZ 12288000
74#define SITAR_MCLK_RATE_9600KHZ 9600000
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053075
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -080076#define SITAR_FAKE_INS_THRESHOLD_MS 2500
77#define SITAR_FAKE_REMOVAL_MIN_PERIOD_MS 50
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -070078#define SITAR_MBHC_BUTTON_MIN 0x8000
79#define SITAR_GPIO_IRQ_DEBOUNCE_TIME_US 5000
80
81#define SITAR_ACQUIRE_LOCK(x) do { mutex_lock(&x); } while (0)
82#define SITAR_RELEASE_LOCK(x) do { mutex_unlock(&x); } while (0)
83
84#define MBHC_NUM_DCE_PLUG_DETECT 3
85#define SITAR_MBHC_FAKE_INSERT_LOW 10
86#define SITAR_MBHC_FAKE_INSERT_HIGH 80
87#define SITAR_MBHC_FAKE_INSERT_VOLT_DELTA_MV 500
88#define SITAR_HS_DETECT_PLUG_TIME_MS (5 * 1000)
89#define SITAR_HS_DETECT_PLUG_INERVAL_MS 100
90#define NUM_ATTEMPTS_TO_REPORT 5
91#define SITAR_MBHC_STATUS_REL_DETECTION 0x0C
92#define SITAR_MBHC_GPIO_REL_DEBOUNCE_TIME_MS 200
Asish Bhattacharyab86c3472012-02-15 08:31:52 +053093
94static const DECLARE_TLV_DB_SCALE(digital_gain, 0, 1, 0);
95static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
96static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
97static struct snd_soc_dai_driver sitar_dai[];
98static int sitar_codec_enable_slimtx(struct snd_soc_dapm_widget *w,
99 struct snd_kcontrol *kcontrol, int event);
100static int sitar_codec_enable_slimrx(struct snd_soc_dapm_widget *w,
101 struct snd_kcontrol *kcontrol, int event);
102
103enum sitar_bandgap_type {
104 SITAR_BANDGAP_OFF = 0,
105 SITAR_BANDGAP_AUDIO_MODE,
106 SITAR_BANDGAP_MBHC_MODE,
107};
108
109struct mbhc_micbias_regs {
110 u16 cfilt_val;
111 u16 cfilt_ctl;
112 u16 mbhc_reg;
113 u16 int_rbias;
114 u16 ctl_reg;
115 u8 cfilt_sel;
116};
117
118/* Codec supports 2 IIR filters */
119enum {
120 IIR1 = 0,
121 IIR2,
122 IIR_MAX,
123};
124/* Codec supports 5 bands */
125enum {
126 BAND1 = 0,
127 BAND2,
128 BAND3,
129 BAND4,
130 BAND5,
131 BAND_MAX,
132};
133
134/* Flags to track of PA and DAC state.
135 * PA and DAC should be tracked separately as AUXPGA loopback requires
136 * only PA to be turned on without DAC being on. */
137enum sitar_priv_ack_flags {
138 SITAR_HPHL_PA_OFF_ACK = 0,
139 SITAR_HPHR_PA_OFF_ACK,
140 SITAR_HPHL_DAC_OFF_ACK,
141 SITAR_HPHR_DAC_OFF_ACK
142};
143
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800144/* Data used by MBHC */
145struct mbhc_internal_cal_data {
146 u16 dce_z;
147 u16 dce_mb;
148 u16 sta_z;
149 u16 sta_mb;
150 u32 t_sta_dce;
151 u32 t_dce;
152 u32 t_sta;
153 u32 micb_mv;
154 u16 v_ins_hu;
155 u16 v_ins_h;
156 u16 v_b1_hu;
157 u16 v_b1_h;
158 u16 v_b1_huc;
159 u16 v_brh;
160 u16 v_brl;
161 u16 v_no_mic;
162 u8 npoll;
163 u8 nbounce_wait;
164};
165
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -0700166enum sitar_mbhc_plug_type {
167 PLUG_TYPE_INVALID = -1,
168 PLUG_TYPE_NONE,
169 PLUG_TYPE_HEADSET,
170 PLUG_TYPE_HEADPHONE,
171 PLUG_TYPE_HIGH_HPH,
172};
173
174enum sitar_mbhc_state {
175 MBHC_STATE_NONE = -1,
176 MBHC_STATE_POTENTIAL,
177 MBHC_STATE_POTENTIAL_RECOVERY,
178 MBHC_STATE_RELEASE,
179};
180
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530181struct sitar_priv {
182 struct snd_soc_codec *codec;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800183 u32 mclk_freq;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530184 u32 adc_count;
185 u32 cfilt1_cnt;
186 u32 cfilt2_cnt;
187 u32 cfilt3_cnt;
188 u32 rx_bias_count;
189 enum sitar_bandgap_type bandgap_type;
190 bool mclk_enabled;
191 bool clock_active;
192 bool config_mode_active;
193 bool mbhc_polling_active;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800194 unsigned long mbhc_fake_ins_start;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530195 int buttons_pressed;
196
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800197 enum sitar_micbias_num micbias;
198 /* void* calibration contains:
199 * struct sitar_mbhc_general_cfg generic;
200 * struct sitar_mbhc_plug_detect_cfg plug_det;
201 * struct sitar_mbhc_plug_type_cfg plug_type;
202 * struct sitar_mbhc_btn_detect_cfg btn_det;
203 * struct sitar_mbhc_imped_detect_cfg imped_det;
204 * Note: various size depends on btn_det->num_btn
205 */
206 void *calibration;
207 struct mbhc_internal_cal_data mbhc_data;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530208
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530209 struct wcd9xxx_pdata *pdata;
210 u32 anc_slot;
211
212 bool no_mic_headset_override;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530213
214 struct mbhc_micbias_regs mbhc_bias_regs;
215 u8 cfilt_k_value;
216 bool mbhc_micbias_switched;
217
218 /* track PA/DAC state */
219 unsigned long hph_pa_dac_state;
220
221 /*track sitar interface type*/
222 u8 intf_type;
223
224 u32 hph_status; /* track headhpone status */
225 /* define separate work for left and right headphone OCP to avoid
226 * additional checking on which OCP event to report so no locking
227 * to ensure synchronization is required
228 */
229 struct work_struct hphlocp_work; /* reporting left hph ocp off */
230 struct work_struct hphrocp_work; /* reporting right hph ocp off */
231
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530232 u8 hphlocp_cnt; /* headphone left ocp retry */
233 u8 hphrocp_cnt; /* headphone right ocp retry */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800234
235 /* Callback function to enable MCLK */
236 int (*mclk_cb) (struct snd_soc_codec*, int);
237
238 /* Work to perform MBHC Firmware Read */
239 struct delayed_work mbhc_firmware_dwork;
240 const struct firmware *mbhc_fw;
241
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530242 /* num of slim ports required */
243 struct sitar_codec_dai_data dai[NUM_CODEC_DAIS];
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -0700244
245 /* Currently, only used for mbhc purpose, to protect
246 * concurrent execution of mbhc threaded irq handlers and
247 * kill race between DAPM and MBHC.But can serve as a
248 * general lock to protect codec resource
249 */
250 struct mutex codec_resource_lock;
251
252 struct sitar_mbhc_config mbhc_cfg;
253 bool in_gpio_handler;
254 u8 current_plug;
255 bool lpi_enabled;
256 enum sitar_mbhc_state mbhc_state;
257 struct work_struct hs_correct_plug_work;
258 bool hs_detect_work_stop;
259 struct delayed_work mbhc_btn_dwork;
260 unsigned long mbhc_last_resume; /* in jiffies */
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530261};
262
263#ifdef CONFIG_DEBUG_FS
264struct sitar_priv *debug_sitar_priv;
265#endif
266
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700267static int sitar_get_anc_slot(struct snd_kcontrol *kcontrol,
268 struct snd_ctl_elem_value *ucontrol)
269{
270 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
271 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
272 ucontrol->value.integer.value[0] = sitar->anc_slot;
273 return 0;
274}
275
276static int sitar_put_anc_slot(struct snd_kcontrol *kcontrol,
277 struct snd_ctl_elem_value *ucontrol)
278{
279 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
280 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
281 sitar->anc_slot = ucontrol->value.integer.value[0];
282 return 0;
283}
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530284
285static int sitar_pa_gain_get(struct snd_kcontrol *kcontrol,
286 struct snd_ctl_elem_value *ucontrol)
287{
288 u8 ear_pa_gain;
289 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
290
291 ear_pa_gain = snd_soc_read(codec, SITAR_A_RX_EAR_GAIN);
292
Bhalchandra Gajare36d82a42012-06-11 16:09:06 -0700293 ear_pa_gain &= 0xE0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530294
295 if (ear_pa_gain == 0x00) {
296 ucontrol->value.integer.value[0] = 0;
Bhalchandra Gajare36d82a42012-06-11 16:09:06 -0700297 } else if (ear_pa_gain == 0x80) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530298 ucontrol->value.integer.value[0] = 1;
Bhalchandra Gajare36d82a42012-06-11 16:09:06 -0700299 } else if (ear_pa_gain == 0xA0) {
300 ucontrol->value.integer.value[0] = 2;
301 } else if (ear_pa_gain == 0xE0) {
302 ucontrol->value.integer.value[0] = 3;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530303 } else {
304 pr_err("%s: ERROR: Unsupported Ear Gain = 0x%x\n",
305 __func__, ear_pa_gain);
306 return -EINVAL;
307 }
308
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530309 pr_debug("%s: ear_pa_gain = 0x%x\n", __func__, ear_pa_gain);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530310
311 return 0;
312}
313
314static int sitar_pa_gain_put(struct snd_kcontrol *kcontrol,
315 struct snd_ctl_elem_value *ucontrol)
316{
317 u8 ear_pa_gain;
318 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
319
320 pr_debug("%s: ucontrol->value.integer.value[0] = %ld\n", __func__,
321 ucontrol->value.integer.value[0]);
322
323 switch (ucontrol->value.integer.value[0]) {
324 case 0:
325 ear_pa_gain = 0x00;
326 break;
327 case 1:
328 ear_pa_gain = 0x80;
329 break;
Bhalchandra Gajare36d82a42012-06-11 16:09:06 -0700330 case 2:
331 ear_pa_gain = 0xA0;
332 break;
333 case 3:
334 ear_pa_gain = 0xE0;
335 break;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530336 default:
337 return -EINVAL;
338 }
339
Bhalchandra Gajare36d82a42012-06-11 16:09:06 -0700340 snd_soc_update_bits(codec, SITAR_A_RX_EAR_GAIN, 0xE0, ear_pa_gain);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530341 return 0;
342}
343
344static int sitar_get_iir_enable_audio_mixer(
345 struct snd_kcontrol *kcontrol,
346 struct snd_ctl_elem_value *ucontrol)
347{
348 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
349 int iir_idx = ((struct soc_multi_mixer_control *)
350 kcontrol->private_value)->reg;
351 int band_idx = ((struct soc_multi_mixer_control *)
352 kcontrol->private_value)->shift;
353
354 ucontrol->value.integer.value[0] =
355 snd_soc_read(codec, (SITAR_A_CDC_IIR1_CTL + 16 * iir_idx)) &
356 (1 << band_idx);
357
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530358 pr_debug("%s: IIR #%d band #%d enable %d\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530359 iir_idx, band_idx,
360 (uint32_t)ucontrol->value.integer.value[0]);
361 return 0;
362}
363
364static int sitar_put_iir_enable_audio_mixer(
365 struct snd_kcontrol *kcontrol,
366 struct snd_ctl_elem_value *ucontrol)
367{
368 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
369 int iir_idx = ((struct soc_multi_mixer_control *)
370 kcontrol->private_value)->reg;
371 int band_idx = ((struct soc_multi_mixer_control *)
372 kcontrol->private_value)->shift;
373 int value = ucontrol->value.integer.value[0];
374
375 /* Mask first 5 bits, 6-8 are reserved */
376 snd_soc_update_bits(codec, (SITAR_A_CDC_IIR1_CTL + 16 * iir_idx),
377 (1 << band_idx), (value << band_idx));
378
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530379 pr_debug("%s: IIR #%d band #%d enable %d\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530380 iir_idx, band_idx, value);
381 return 0;
382}
383static uint32_t get_iir_band_coeff(struct snd_soc_codec *codec,
384 int iir_idx, int band_idx,
385 int coeff_idx)
386{
387 /* Address does not automatically update if reading */
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530388 snd_soc_write(codec,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530389 (SITAR_A_CDC_IIR1_COEF_B1_CTL + 16 * iir_idx),
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530390 (band_idx * BAND_MAX + coeff_idx) & 0x1F);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530391
392 /* Mask bits top 2 bits since they are reserved */
393 return ((snd_soc_read(codec,
394 (SITAR_A_CDC_IIR1_COEF_B2_CTL + 16 * iir_idx)) << 24) |
395 (snd_soc_read(codec,
396 (SITAR_A_CDC_IIR1_COEF_B3_CTL + 16 * iir_idx)) << 16) |
397 (snd_soc_read(codec,
398 (SITAR_A_CDC_IIR1_COEF_B4_CTL + 16 * iir_idx)) << 8) |
399 (snd_soc_read(codec,
400 (SITAR_A_CDC_IIR1_COEF_B5_CTL + 16 * iir_idx)))) &
401 0x3FFFFFFF;
402}
403
404static int sitar_get_iir_band_audio_mixer(
405 struct snd_kcontrol *kcontrol,
406 struct snd_ctl_elem_value *ucontrol)
407{
408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
409 int iir_idx = ((struct soc_multi_mixer_control *)
410 kcontrol->private_value)->reg;
411 int band_idx = ((struct soc_multi_mixer_control *)
412 kcontrol->private_value)->shift;
413
414 ucontrol->value.integer.value[0] =
415 get_iir_band_coeff(codec, iir_idx, band_idx, 0);
416 ucontrol->value.integer.value[1] =
417 get_iir_band_coeff(codec, iir_idx, band_idx, 1);
418 ucontrol->value.integer.value[2] =
419 get_iir_band_coeff(codec, iir_idx, band_idx, 2);
420 ucontrol->value.integer.value[3] =
421 get_iir_band_coeff(codec, iir_idx, band_idx, 3);
422 ucontrol->value.integer.value[4] =
423 get_iir_band_coeff(codec, iir_idx, band_idx, 4);
424
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530425 pr_debug("%s: IIR #%d band #%d b0 = 0x%x\n"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530426 "%s: IIR #%d band #%d b1 = 0x%x\n"
427 "%s: IIR #%d band #%d b2 = 0x%x\n"
428 "%s: IIR #%d band #%d a1 = 0x%x\n"
429 "%s: IIR #%d band #%d a2 = 0x%x\n",
430 __func__, iir_idx, band_idx,
431 (uint32_t)ucontrol->value.integer.value[0],
432 __func__, iir_idx, band_idx,
433 (uint32_t)ucontrol->value.integer.value[1],
434 __func__, iir_idx, band_idx,
435 (uint32_t)ucontrol->value.integer.value[2],
436 __func__, iir_idx, band_idx,
437 (uint32_t)ucontrol->value.integer.value[3],
438 __func__, iir_idx, band_idx,
439 (uint32_t)ucontrol->value.integer.value[4]);
440 return 0;
441}
442
443static void set_iir_band_coeff(struct snd_soc_codec *codec,
444 int iir_idx, int band_idx,
445 int coeff_idx, uint32_t value)
446{
447 /* Mask top 3 bits, 6-8 are reserved */
448 /* Update address manually each time */
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530449 snd_soc_write(codec,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530450 (SITAR_A_CDC_IIR1_COEF_B1_CTL + 16 * iir_idx),
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530451 (band_idx * BAND_MAX + coeff_idx) & 0x1F);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530452
453 /* Mask top 2 bits, 7-8 are reserved */
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530454 snd_soc_write(codec,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530455 (SITAR_A_CDC_IIR1_COEF_B2_CTL + 16 * iir_idx),
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530456 (value >> 24) & 0x3F);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530457
458 /* Isolate 8bits at a time */
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530459 snd_soc_write(codec,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530460 (SITAR_A_CDC_IIR1_COEF_B3_CTL + 16 * iir_idx),
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530461 (value >> 16) & 0xFF);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530462
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530463 snd_soc_write(codec,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530464 (SITAR_A_CDC_IIR1_COEF_B4_CTL + 16 * iir_idx),
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530465 (value >> 8) & 0xFF);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530466
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530467 snd_soc_write(codec,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530468 (SITAR_A_CDC_IIR1_COEF_B5_CTL + 16 * iir_idx),
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530469 value & 0xFF);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530470}
471
472static int sitar_put_iir_band_audio_mixer(
473 struct snd_kcontrol *kcontrol,
474 struct snd_ctl_elem_value *ucontrol)
475{
476 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
477 int iir_idx = ((struct soc_multi_mixer_control *)
478 kcontrol->private_value)->reg;
479 int band_idx = ((struct soc_multi_mixer_control *)
480 kcontrol->private_value)->shift;
481
482 set_iir_band_coeff(codec, iir_idx, band_idx, 0,
483 ucontrol->value.integer.value[0]);
484 set_iir_band_coeff(codec, iir_idx, band_idx, 1,
485 ucontrol->value.integer.value[1]);
486 set_iir_band_coeff(codec, iir_idx, band_idx, 2,
487 ucontrol->value.integer.value[2]);
488 set_iir_band_coeff(codec, iir_idx, band_idx, 3,
489 ucontrol->value.integer.value[3]);
490 set_iir_band_coeff(codec, iir_idx, band_idx, 4,
491 ucontrol->value.integer.value[4]);
492
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530493 pr_debug("%s: IIR #%d band #%d b0 = 0x%x\n"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530494 "%s: IIR #%d band #%d b1 = 0x%x\n"
495 "%s: IIR #%d band #%d b2 = 0x%x\n"
496 "%s: IIR #%d band #%d a1 = 0x%x\n"
497 "%s: IIR #%d band #%d a2 = 0x%x\n",
498 __func__, iir_idx, band_idx,
499 get_iir_band_coeff(codec, iir_idx, band_idx, 0),
500 __func__, iir_idx, band_idx,
501 get_iir_band_coeff(codec, iir_idx, band_idx, 1),
502 __func__, iir_idx, band_idx,
503 get_iir_band_coeff(codec, iir_idx, band_idx, 2),
504 __func__, iir_idx, band_idx,
505 get_iir_band_coeff(codec, iir_idx, band_idx, 3),
506 __func__, iir_idx, band_idx,
507 get_iir_band_coeff(codec, iir_idx, band_idx, 4));
508 return 0;
509}
510
Bhalchandra Gajare36d82a42012-06-11 16:09:06 -0700511static const char * const sitar_ear_pa_gain_text[] = {"POS_6_DB",
512 "POS_2_DB", "NEG_2P5_DB", "NEG_12_DB"};
513
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530514static const struct soc_enum sitar_ear_pa_gain_enum[] = {
Bhalchandra Gajare36d82a42012-06-11 16:09:06 -0700515 SOC_ENUM_SINGLE_EXT(4, sitar_ear_pa_gain_text),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530516};
517
518/*cut of frequency for high pass filter*/
519static const char *cf_text[] = {
520 "MIN_3DB_4Hz", "MIN_3DB_75Hz", "MIN_3DB_150Hz"
521};
522
523static const struct soc_enum cf_dec1_enum =
524 SOC_ENUM_SINGLE(SITAR_A_CDC_TX1_MUX_CTL, 4, 3, cf_text);
525
526static const struct soc_enum cf_rxmix1_enum =
527 SOC_ENUM_SINGLE(SITAR_A_CDC_RX1_B4_CTL, 1, 3, cf_text);
528
529static const struct snd_kcontrol_new sitar_snd_controls[] = {
530
531 SOC_ENUM_EXT("EAR PA Gain", sitar_ear_pa_gain_enum[0],
532 sitar_pa_gain_get, sitar_pa_gain_put),
533
534 SOC_SINGLE_TLV("LINEOUT1 Volume", SITAR_A_RX_LINE_1_GAIN, 0, 12, 1,
535 line_gain),
536 SOC_SINGLE_TLV("LINEOUT2 Volume", SITAR_A_RX_LINE_2_GAIN, 0, 12, 1,
537 line_gain),
538
539 SOC_SINGLE_TLV("HPHL Volume", SITAR_A_RX_HPH_L_GAIN, 0, 12, 1,
540 line_gain),
541 SOC_SINGLE_TLV("HPHR Volume", SITAR_A_RX_HPH_R_GAIN, 0, 12, 1,
542 line_gain),
543
544 SOC_SINGLE_S8_TLV("RX1 Digital Volume", SITAR_A_CDC_RX1_VOL_CTL_B2_CTL,
545 -84, 40, digital_gain),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800546 SOC_SINGLE_S8_TLV("RX2 Digital Volume", SITAR_A_CDC_RX2_VOL_CTL_B2_CTL,
547 -84, 40, digital_gain),
548 SOC_SINGLE_S8_TLV("RX3 Digital Volume", SITAR_A_CDC_RX3_VOL_CTL_B2_CTL,
549 -84, 40, digital_gain),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530550
551 SOC_SINGLE_S8_TLV("DEC1 Volume", SITAR_A_CDC_TX1_VOL_CTL_GAIN, -84, 40,
552 digital_gain),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800553 SOC_SINGLE_S8_TLV("DEC2 Volume", SITAR_A_CDC_TX2_VOL_CTL_GAIN, -84, 40,
554 digital_gain),
555 SOC_SINGLE_S8_TLV("DEC3 Volume", SITAR_A_CDC_TX3_VOL_CTL_GAIN, -84, 40,
556 digital_gain),
557 SOC_SINGLE_S8_TLV("DEC4 Volume", SITAR_A_CDC_TX4_VOL_CTL_GAIN, -84, 40,
558 digital_gain),
559
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530560 SOC_SINGLE_S8_TLV("IIR1 INP1 Volume", SITAR_A_CDC_IIR1_GAIN_B1_CTL, -84,
561 40, digital_gain),
562 SOC_SINGLE_S8_TLV("IIR1 INP2 Volume", SITAR_A_CDC_IIR1_GAIN_B2_CTL, -84,
563 40, digital_gain),
564 SOC_SINGLE_S8_TLV("IIR1 INP3 Volume", SITAR_A_CDC_IIR1_GAIN_B3_CTL, -84,
565 40, digital_gain),
566 SOC_SINGLE_S8_TLV("IIR1 INP4 Volume", SITAR_A_CDC_IIR1_GAIN_B4_CTL, -84,
567 40, digital_gain),
568 SOC_SINGLE_TLV("ADC1 Volume", SITAR_A_TX_1_2_EN, 5, 3, 0, analog_gain),
569 SOC_SINGLE_TLV("ADC2 Volume", SITAR_A_TX_1_2_EN, 1, 3, 0, analog_gain),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800570 SOC_SINGLE_TLV("ADC3 Volume", SITAR_A_TX_3_EN, 5, 3, 0, analog_gain),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530571
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700572 SOC_SINGLE_EXT("ANC Slot", SND_SOC_NOPM, 0, 0, 100, sitar_get_anc_slot,
573 sitar_put_anc_slot),
574
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530575 SOC_ENUM("TX1 HPF cut off", cf_dec1_enum),
576
577 SOC_SINGLE("TX1 HPF Switch", SITAR_A_CDC_TX1_MUX_CTL, 3, 1, 0),
578
579 SOC_SINGLE("RX1 HPF Switch", SITAR_A_CDC_RX1_B5_CTL, 2, 1, 0),
580
581 SOC_ENUM("RX1 HPF cut off", cf_rxmix1_enum),
582
583 SOC_SINGLE_EXT("IIR1 Enable Band1", IIR1, BAND1, 1, 0,
584 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
585 SOC_SINGLE_EXT("IIR1 Enable Band2", IIR1, BAND2, 1, 0,
586 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
587 SOC_SINGLE_EXT("IIR1 Enable Band3", IIR1, BAND3, 1, 0,
588 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
589 SOC_SINGLE_EXT("IIR1 Enable Band4", IIR1, BAND4, 1, 0,
590 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
591 SOC_SINGLE_EXT("IIR1 Enable Band5", IIR1, BAND5, 1, 0,
592 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
593 SOC_SINGLE_EXT("IIR2 Enable Band1", IIR2, BAND1, 1, 0,
594 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
595 SOC_SINGLE_EXT("IIR2 Enable Band2", IIR2, BAND2, 1, 0,
596 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
597 SOC_SINGLE_EXT("IIR2 Enable Band3", IIR2, BAND3, 1, 0,
598 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
599 SOC_SINGLE_EXT("IIR2 Enable Band4", IIR2, BAND4, 1, 0,
600 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
601 SOC_SINGLE_EXT("IIR2 Enable Band5", IIR2, BAND5, 1, 0,
602 sitar_get_iir_enable_audio_mixer, sitar_put_iir_enable_audio_mixer),
603
604 SOC_SINGLE_MULTI_EXT("IIR1 Band1", IIR1, BAND1, 255, 0, 5,
605 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
606 SOC_SINGLE_MULTI_EXT("IIR1 Band2", IIR1, BAND2, 255, 0, 5,
607 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
608 SOC_SINGLE_MULTI_EXT("IIR1 Band3", IIR1, BAND3, 255, 0, 5,
609 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
610 SOC_SINGLE_MULTI_EXT("IIR1 Band4", IIR1, BAND4, 255, 0, 5,
611 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
612 SOC_SINGLE_MULTI_EXT("IIR1 Band5", IIR1, BAND5, 255, 0, 5,
613 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
614 SOC_SINGLE_MULTI_EXT("IIR2 Band1", IIR2, BAND1, 255, 0, 5,
615 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
616 SOC_SINGLE_MULTI_EXT("IIR2 Band2", IIR2, BAND2, 255, 0, 5,
617 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
618 SOC_SINGLE_MULTI_EXT("IIR2 Band3", IIR2, BAND3, 255, 0, 5,
619 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
620 SOC_SINGLE_MULTI_EXT("IIR2 Band4", IIR2, BAND4, 255, 0, 5,
621 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
622 SOC_SINGLE_MULTI_EXT("IIR2 Band5", IIR2, BAND5, 255, 0, 5,
623 sitar_get_iir_band_audio_mixer, sitar_put_iir_band_audio_mixer),
624};
625
626static const char *rx_mix1_text[] = {
627 "ZERO", "SRC1", "SRC2", "IIR1", "IIR2", "RX1", "RX2", "RX3", "RX4",
628 "RX5"
629};
630
631static const char *rx_dac1_text[] = {
632 "ZERO", "RX1", "RX2"
633};
634
635static const char *rx_dac2_text[] = {
636 "ZERO", "RX1",
637};
638
639static const char *rx_dac3_text[] = {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800640 "ZERO", "RX1", "INV_RX1", "RX2"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530641};
642
643static const char *rx_dac4_text[] = {
644 "ZERO", "ON"
645};
646
647static const char *sb_tx1_mux_text[] = {
648 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
649 "DEC1"
650};
651
652static const char *sb_tx2_mux_text[] = {
653 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
654 "DEC2"
655};
656
657static const char *sb_tx3_mux_text[] = {
658 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
659 "DEC3"
660};
661
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800662static const char *sb_tx4_mux_text[] = {
663 "ZERO", "RMIX1", "RMIX2", "RMIX3", "RMIX4", "RMIX5", "RMIX6", "RMIX7",
664 "DEC4"
665};
666
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530667static const char *sb_tx5_mux_text[] = {
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -0700668 "ZERO", "RMIX1", "RMIX2", "RMIX3", "DEC1", "DEC2", "DEC3", "DEC4"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530669};
670
671static const char *dec1_mux_text[] = {
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700672 "ZERO", "DMIC1", "ADC1", "ADC2", "ADC3", "MBADC", "DMIC4", "ANC1_FB",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530673};
674
675static const char *dec2_mux_text[] = {
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700676 "ZERO", "DMIC2", "ADC1", "ADC2", "ADC3", "MBADC", "DMIC3", "ANC2_FB",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530677};
678
679static const char *dec3_mux_text[] = {
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -0700680 "ZERO", "DMIC3", "ADC1", "ADC2", "ADC3", "MBADC", "DMIC2", "DMIC4"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530681};
682
683static const char *dec4_mux_text[] = {
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -0700684 "ZERO", "DMIC4", "ADC1", "ADC2", "ADC3", "DMIC3", "DMIC2", "DMIC1"
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530685};
686
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700687static const char const *anc_mux_text[] = {
688 "ZERO", "ADC1", "ADC2", "ADC3", "RSVD1", "RSVD2", "RSVD3",
689 "MBADC", "RSVD4", "DMIC1", "DMIC2", "DMIC3", "DMIC4"
690};
691
692static const char const *anc1_fb_mux_text[] = {
693 "ZERO", "EAR_HPH_L", "EAR_LINE_1",
694};
695
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530696static const char const *iir_inp1_text[] = {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530697 "ZERO", "DEC1", "DEC2", "DEC3", "DEC4", "ZERO", "ZERO", "ZERO",
698 "ZERO", "ZERO", "ZERO", "RX1", "RX2", "RX3", "RX4", "RX5",
699};
700
701static const struct soc_enum rx_mix1_inp1_chain_enum =
702 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX1_B1_CTL, 0, 10, rx_mix1_text);
703
704static const struct soc_enum rx_mix1_inp2_chain_enum =
705 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX1_B1_CTL, 4, 10, rx_mix1_text);
706
707static const struct soc_enum rx2_mix1_inp1_chain_enum =
708 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX2_B1_CTL, 0, 10, rx_mix1_text);
709
710static const struct soc_enum rx2_mix1_inp2_chain_enum =
711 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX2_B1_CTL, 4, 10, rx_mix1_text);
712
713static const struct soc_enum rx3_mix1_inp1_chain_enum =
714 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX3_B1_CTL, 0, 10, rx_mix1_text);
715
716static const struct soc_enum rx3_mix1_inp2_chain_enum =
717 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_RX3_B1_CTL, 4, 10, rx_mix1_text);
718
719static const struct soc_enum rx_dac1_enum =
720 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 6, 3, rx_dac1_text);
721
722static const struct soc_enum rx_dac2_enum =
723 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 4, 2, rx_dac2_text);
724
725static const struct soc_enum rx_dac3_enum =
726 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 2, 4, rx_dac3_text);
727
728static const struct soc_enum rx_dac4_enum =
729 SOC_ENUM_SINGLE(SITAR_A_CDC_TOP_RDAC_DOUT_CTL, 0, 2, rx_dac4_text);
730
731static const struct soc_enum sb_tx5_mux_enum =
732 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B5_CTL, 0, 9, sb_tx5_mux_text);
733
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800734static const struct soc_enum sb_tx4_mux_enum =
735 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B4_CTL, 0, 9, sb_tx4_mux_text);
736
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530737static const struct soc_enum sb_tx3_mux_enum =
738 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B3_CTL, 0, 9, sb_tx3_mux_text);
739
740static const struct soc_enum sb_tx2_mux_enum =
741 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B2_CTL, 0, 9, sb_tx2_mux_text);
742
743static const struct soc_enum sb_tx1_mux_enum =
744 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_SB_B1_CTL, 0, 9, sb_tx1_mux_text);
745
746static const struct soc_enum dec1_mux_enum =
747 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B1_CTL, 0, 8, dec1_mux_text);
748
749static const struct soc_enum dec2_mux_enum =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800750 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B1_CTL, 3, 8, dec2_mux_text);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530751
752static const struct soc_enum dec3_mux_enum =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800753 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B2_CTL, 0, 8, dec3_mux_text);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530754
755static const struct soc_enum dec4_mux_enum =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800756 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_TX_B2_CTL, 3, 8, dec4_mux_text);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530757
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700758static const struct soc_enum anc1_mux_enum =
759 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_ANC_B1_CTL, 0, 13, anc_mux_text);
760
761static const struct soc_enum anc2_mux_enum =
762 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_ANC_B1_CTL, 4, 13, anc_mux_text);
763
764static const struct soc_enum anc1_fb_mux_enum =
765 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_ANC_B2_CTL, 0, 3, anc1_fb_mux_text);
766
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530767static const struct soc_enum iir1_inp1_mux_enum =
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530768 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_EQ1_B1_CTL, 0, 16, iir_inp1_text);
769
770static const struct soc_enum iir2_inp1_mux_enum =
771 SOC_ENUM_SINGLE(SITAR_A_CDC_CONN_EQ2_B1_CTL, 0, 16, iir_inp1_text);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530772
773static const struct snd_kcontrol_new rx_mix1_inp1_mux =
774 SOC_DAPM_ENUM("RX1 MIX1 INP1 Mux", rx_mix1_inp1_chain_enum);
775
776static const struct snd_kcontrol_new rx_mix1_inp2_mux =
777 SOC_DAPM_ENUM("RX1 MIX1 INP2 Mux", rx_mix1_inp2_chain_enum);
778
779static const struct snd_kcontrol_new rx2_mix1_inp1_mux =
780 SOC_DAPM_ENUM("RX2 MIX1 INP1 Mux", rx2_mix1_inp1_chain_enum);
781
782static const struct snd_kcontrol_new rx2_mix1_inp2_mux =
783 SOC_DAPM_ENUM("RX2 MIX1 INP2 Mux", rx2_mix1_inp2_chain_enum);
784
785static const struct snd_kcontrol_new rx3_mix1_inp1_mux =
786 SOC_DAPM_ENUM("RX3 MIX1 INP1 Mux", rx3_mix1_inp1_chain_enum);
787
788static const struct snd_kcontrol_new rx3_mix1_inp2_mux =
789 SOC_DAPM_ENUM("RX3 MIX1 INP2 Mux", rx3_mix1_inp2_chain_enum);
790
791static const struct snd_kcontrol_new rx_dac1_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800792 SOC_DAPM_ENUM("RX DAC1 Mux", rx_dac1_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530793
794static const struct snd_kcontrol_new rx_dac2_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800795 SOC_DAPM_ENUM("RX DAC2 Mux", rx_dac2_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530796
797static const struct snd_kcontrol_new rx_dac3_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800798 SOC_DAPM_ENUM("RX DAC3 Mux", rx_dac3_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530799
800static const struct snd_kcontrol_new rx_dac4_mux =
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800801 SOC_DAPM_ENUM("RX DAC4 Mux", rx_dac4_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530802
803static const struct snd_kcontrol_new sb_tx5_mux =
804 SOC_DAPM_ENUM("SLIM TX5 MUX Mux", sb_tx5_mux_enum);
805
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -0800806static const struct snd_kcontrol_new sb_tx4_mux =
807 SOC_DAPM_ENUM("SLIM TX4 MUX Mux", sb_tx4_mux_enum);
808
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530809static const struct snd_kcontrol_new sb_tx3_mux =
810 SOC_DAPM_ENUM("SLIM TX3 MUX Mux", sb_tx3_mux_enum);
811
812static const struct snd_kcontrol_new sb_tx2_mux =
813 SOC_DAPM_ENUM("SLIM TX2 MUX Mux", sb_tx2_mux_enum);
814
815static const struct snd_kcontrol_new sb_tx1_mux =
816 SOC_DAPM_ENUM("SLIM TX1 MUX Mux", sb_tx1_mux_enum);
817
Asish Bhattacharyab9afc732012-08-01 15:11:24 +0530818static int wcd9304_put_dec_enum(struct snd_kcontrol *kcontrol,
819 struct snd_ctl_elem_value *ucontrol)
820 {
821 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
822 struct snd_soc_dapm_widget *w = wlist->widgets[0];
823 struct snd_soc_codec *codec = w->codec;
824 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
825 unsigned int dec_mux, decimator;
826 char *dec_name = NULL;
827 char *widget_name = NULL;
828 char *temp;
829 u16 tx_mux_ctl_reg;
830 u8 adc_dmic_sel = 0x0;
831 int ret = 0;
832
833 if (ucontrol->value.enumerated.item[0] > e->max - 1)
834 return -EINVAL;
835
836 dec_mux = ucontrol->value.enumerated.item[0];
837
838 widget_name = kstrndup(w->name, 15, GFP_KERNEL);
839 if (!widget_name)
840 return -ENOMEM;
841 temp = widget_name;
842
843 dec_name = strsep(&widget_name, " ");
844 widget_name = temp;
845 if (!dec_name) {
846 pr_err("%s: Invalid decimator = %s\n", __func__, w->name);
847 ret = -EINVAL;
848 goto out;
849 }
850
851 ret = kstrtouint(strpbrk(dec_name, "1234"), 10, &decimator);
852 if (ret < 0) {
853 pr_err("%s: Invalid decimator = %s\n", __func__, dec_name);
854 ret = -EINVAL;
855 goto out;
856 }
857
858 dev_dbg(w->dapm->dev, "%s(): widget = %s dec_name = %s decimator = %u"\
859 "dec_mux = %u\n", __func__, w->name, dec_name, decimator,
860 dec_mux);
861
862
863 switch (decimator) {
864 case 1:
865 case 2:
866 if ((dec_mux == 1) || (dec_mux == 6))
867 adc_dmic_sel = ADC_DMIC_SEL_DMIC;
868 else
869 adc_dmic_sel = ADC_DMIC_SEL_ADC;
870 break;
871 case 3:
872 if ((dec_mux == 1) || (dec_mux == 6) || (dec_mux == 7))
873 adc_dmic_sel = ADC_DMIC_SEL_DMIC;
874 else
875 adc_dmic_sel = ADC_DMIC_SEL_ADC;
876 break;
877 case 4:
878 if ((dec_mux == 1) || (dec_mux == 5)
879 || (dec_mux == 6) || (dec_mux == 7))
880 adc_dmic_sel = ADC_DMIC_SEL_DMIC;
881 else
882 adc_dmic_sel = ADC_DMIC_SEL_ADC;
883 break;
884 default:
885 pr_err("%s: Invalid Decimator = %u\n", __func__, decimator);
886 ret = -EINVAL;
887 goto out;
888 }
889
890 tx_mux_ctl_reg = SITAR_A_CDC_TX1_MUX_CTL + 8 * (decimator - 1);
891
892 snd_soc_update_bits(codec, tx_mux_ctl_reg, 0x1, adc_dmic_sel);
893
894 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
895out:
896 kfree(widget_name);
897 return ret;
898}
899
900#define WCD9304_DEC_ENUM(xname, xenum) \
901{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
902 .info = snd_soc_info_enum_double, \
903 .get = snd_soc_dapm_get_enum_double, \
904 .put = wcd9304_put_dec_enum, \
905 .private_value = (unsigned long)&xenum }
906
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530907static const struct snd_kcontrol_new dec1_mux =
Asish Bhattacharyab9afc732012-08-01 15:11:24 +0530908 WCD9304_DEC_ENUM("DEC1 MUX Mux", dec1_mux_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530909
910static const struct snd_kcontrol_new dec2_mux =
Asish Bhattacharyab9afc732012-08-01 15:11:24 +0530911 WCD9304_DEC_ENUM("DEC2 MUX Mux", dec2_mux_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530912
913static const struct snd_kcontrol_new dec3_mux =
Asish Bhattacharyab9afc732012-08-01 15:11:24 +0530914 WCD9304_DEC_ENUM("DEC3 MUX Mux", dec3_mux_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530915
916static const struct snd_kcontrol_new dec4_mux =
Asish Bhattacharyab9afc732012-08-01 15:11:24 +0530917 WCD9304_DEC_ENUM("DEC4 MUX Mux", dec4_mux_enum);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530918
919static const struct snd_kcontrol_new iir1_inp1_mux =
920 SOC_DAPM_ENUM("IIR1 INP1 Mux", iir1_inp1_mux_enum);
921
Asish Bhattacharyaa7354892012-05-28 12:49:01 +0530922static const struct snd_kcontrol_new iir2_inp1_mux =
923 SOC_DAPM_ENUM("IIR2 INP1 Mux", iir2_inp1_mux_enum);
924
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700925static const struct snd_kcontrol_new anc1_mux =
926 SOC_DAPM_ENUM("ANC1 MUX Mux", anc1_mux_enum);
927
928static const struct snd_kcontrol_new anc2_mux =
929 SOC_DAPM_ENUM("ANC2 MUX Mux", anc2_mux_enum);
930
931static const struct snd_kcontrol_new anc1_fb_mux =
932 SOC_DAPM_ENUM("ANC1 FB MUX Mux", anc1_fb_mux_enum);
933
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530934static const struct snd_kcontrol_new dac1_switch[] = {
935 SOC_DAPM_SINGLE("Switch", SITAR_A_RX_EAR_EN, 5, 1, 0),
936};
937
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530938static void sitar_codec_enable_adc_block(struct snd_soc_codec *codec,
939 int enable)
940{
941 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
942
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530943 pr_debug("%s %d\n", __func__, enable);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530944
945 if (enable) {
946 sitar->adc_count++;
Bhalchandra Gajare5d260e32012-07-18 16:30:29 -0700947 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_CTL,
948 0x02, 0x02);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530949 } else {
950 sitar->adc_count--;
951 if (!sitar->adc_count) {
952 if (!sitar->mbhc_polling_active)
Bhalchandra Gajare5d260e32012-07-18 16:30:29 -0700953 snd_soc_update_bits(codec,
954 SITAR_A_CDC_CLK_OTHR_CTL, 0xE0, 0x0);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530955 }
956 }
957}
958
959static int sitar_codec_enable_adc(struct snd_soc_dapm_widget *w,
960 struct snd_kcontrol *kcontrol, int event)
961{
962 struct snd_soc_codec *codec = w->codec;
963 u16 adc_reg;
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700964 u8 init_bit_shift;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530965
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +0530966 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530967
968 if (w->reg == SITAR_A_TX_1_2_EN)
969 adc_reg = SITAR_A_TX_1_2_TEST_CTL;
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700970 else if (w->reg == SITAR_A_TX_3_EN)
971 adc_reg = SITAR_A_TX_3_TEST_CTL;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530972 else {
973 pr_err("%s: Error, invalid adc register\n", __func__);
974 return -EINVAL;
975 }
976
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700977 if (w->shift == 3)
978 init_bit_shift = 6;
979 else if (w->shift == 7)
980 init_bit_shift = 7;
981 else {
982 pr_err("%s: Error, invalid init bit postion adc register\n",
983 __func__);
984 return -EINVAL;
985 }
986
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530987 switch (event) {
988 case SND_SOC_DAPM_PRE_PMU:
989 sitar_codec_enable_adc_block(codec, 1);
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700990 snd_soc_update_bits(codec, adc_reg, 1 << init_bit_shift,
991 1 << init_bit_shift);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530992 break;
993 case SND_SOC_DAPM_POST_PMU:
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -0700994
995 snd_soc_update_bits(codec, adc_reg, 1 << init_bit_shift, 0x00);
996
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530997 break;
998 case SND_SOC_DAPM_POST_PMD:
999 sitar_codec_enable_adc_block(codec, 0);
1000 break;
1001 }
1002 return 0;
1003}
1004
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001005static int sitar_lineout_dac_event(struct snd_soc_dapm_widget *w,
1006 struct snd_kcontrol *kcontrol, int event)
1007{
1008 struct snd_soc_codec *codec = w->codec;
1009
1010 pr_debug("%s %s %d\n", __func__, w->name, event);
1011
1012 switch (event) {
1013 case SND_SOC_DAPM_PRE_PMU:
1014 snd_soc_update_bits(codec, w->reg, 0x40, 0x40);
1015 break;
1016
1017 case SND_SOC_DAPM_POST_PMD:
1018 snd_soc_update_bits(codec, w->reg, 0x40, 0x00);
1019 break;
1020 }
1021 return 0;
1022}
1023
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301024static int sitar_codec_enable_lineout(struct snd_soc_dapm_widget *w,
1025 struct snd_kcontrol *kcontrol, int event)
1026{
1027 struct snd_soc_codec *codec = w->codec;
1028 u16 lineout_gain_reg;
1029
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301030 pr_debug("%s %d %s\n", __func__, event, w->name);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301031
1032 switch (w->shift) {
1033 case 0:
1034 lineout_gain_reg = SITAR_A_RX_LINE_1_GAIN;
1035 break;
1036 case 1:
1037 lineout_gain_reg = SITAR_A_RX_LINE_2_GAIN;
1038 break;
1039 default:
1040 pr_err("%s: Error, incorrect lineout register value\n",
1041 __func__);
1042 return -EINVAL;
1043 }
1044
1045 switch (event) {
1046 case SND_SOC_DAPM_PRE_PMU:
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001047 snd_soc_update_bits(codec, lineout_gain_reg, 0x10, 0x10);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301048 break;
1049 case SND_SOC_DAPM_POST_PMU:
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301050 pr_debug("%s: sleeping 16 ms after %s PA turn on\n",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301051 __func__, w->name);
1052 usleep_range(16000, 16000);
1053 break;
1054 case SND_SOC_DAPM_POST_PMD:
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001055 snd_soc_update_bits(codec, lineout_gain_reg, 0x10, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301056 break;
1057 }
1058 return 0;
1059}
1060
1061static int sitar_codec_enable_dmic(struct snd_soc_dapm_widget *w,
1062 struct snd_kcontrol *kcontrol, int event)
1063{
1064 struct snd_soc_codec *codec = w->codec;
Asish Bhattacharyab9afc732012-08-01 15:11:24 +05301065 u16 tx_dmic_ctl_reg;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301066 u8 dmic_clk_sel, dmic_clk_en;
1067 unsigned int dmic;
1068 int ret;
1069
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07001070 ret = kstrtouint(strpbrk(w->name, "1234"), 10, &dmic);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301071 if (ret < 0) {
1072 pr_err("%s: Invalid DMIC line on the codec\n", __func__);
1073 return -EINVAL;
1074 }
1075
1076 switch (dmic) {
1077 case 1:
1078 case 2:
1079 dmic_clk_sel = 0x02;
1080 dmic_clk_en = 0x01;
1081 break;
1082 case 3:
1083 case 4:
1084 dmic_clk_sel = 0x08;
1085 dmic_clk_en = 0x04;
1086 break;
1087
1088 break;
1089
1090 default:
1091 pr_err("%s: Invalid DMIC Selection\n", __func__);
1092 return -EINVAL;
1093 }
1094
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301095 tx_dmic_ctl_reg = SITAR_A_CDC_TX1_DMIC_CTL + 8 * (dmic - 1);
1096
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301097 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301098
1099 switch (event) {
1100 case SND_SOC_DAPM_PRE_PMU:
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301101 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_DMIC_CTL,
1102 dmic_clk_sel, dmic_clk_sel);
1103
1104 snd_soc_update_bits(codec, tx_dmic_ctl_reg, 0x1, 0x1);
1105
1106 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_DMIC_CTL,
1107 dmic_clk_en, dmic_clk_en);
1108 break;
1109 case SND_SOC_DAPM_POST_PMD:
1110 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_DMIC_CTL,
1111 dmic_clk_en, 0);
1112 break;
1113 }
1114 return 0;
1115}
1116
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07001117static int sitar_codec_enable_anc(struct snd_soc_dapm_widget *w,
1118 struct snd_kcontrol *kcontrol, int event)
1119{
1120 struct snd_soc_codec *codec = w->codec;
1121 const char *filename;
1122 const struct firmware *fw;
1123 int i;
1124 int ret;
1125 int num_anc_slots;
1126 struct anc_header *anc_head;
1127 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1128 u32 anc_writes_size = 0;
1129 int anc_size_remaining;
1130 u32 *anc_ptr;
1131 u16 reg;
1132 u8 mask, val, old_val;
1133
1134 pr_debug("%s %d\n", __func__, event);
1135 switch (event) {
1136 case SND_SOC_DAPM_PRE_PMU:
1137
1138 /* Use the same firmware file as that of WCD9310,
1139 * since the register sequences are same for
1140 * WCD9310 and WCD9304
1141 */
1142 filename = "wcd9310/wcd9310_anc.bin";
1143
1144 ret = request_firmware(&fw, filename, codec->dev);
1145 if (ret != 0) {
1146 dev_err(codec->dev, "Failed to acquire ANC data: %d\n",
1147 ret);
1148 return -ENODEV;
1149 }
1150
1151 if (fw->size < sizeof(struct anc_header)) {
1152 dev_err(codec->dev, "Not enough data\n");
1153 release_firmware(fw);
1154 return -ENOMEM;
1155 }
1156
1157 /* First number is the number of register writes */
1158 anc_head = (struct anc_header *)(fw->data);
1159 anc_ptr = (u32 *)((u32)fw->data + sizeof(struct anc_header));
1160 anc_size_remaining = fw->size - sizeof(struct anc_header);
1161 num_anc_slots = anc_head->num_anc_slots;
1162
1163 if (sitar->anc_slot >= num_anc_slots) {
1164 dev_err(codec->dev, "Invalid ANC slot selected\n");
1165 release_firmware(fw);
1166 return -EINVAL;
1167 }
1168
1169 for (i = 0; i < num_anc_slots; i++) {
1170
1171 if (anc_size_remaining < SITAR_PACKED_REG_SIZE) {
1172 dev_err(codec->dev, "Invalid register format\n");
1173 release_firmware(fw);
1174 return -EINVAL;
1175 }
1176 anc_writes_size = (u32)(*anc_ptr);
1177 anc_size_remaining -= sizeof(u32);
1178 anc_ptr += 1;
1179
1180 if (anc_writes_size * SITAR_PACKED_REG_SIZE
1181 > anc_size_remaining) {
1182 dev_err(codec->dev, "Invalid register format\n");
1183 release_firmware(fw);
1184 return -ENOMEM;
1185 }
1186
1187 if (sitar->anc_slot == i)
1188 break;
1189
1190 anc_size_remaining -= (anc_writes_size *
1191 SITAR_PACKED_REG_SIZE);
1192 anc_ptr += anc_writes_size;
1193 }
1194 if (i == num_anc_slots) {
1195 dev_err(codec->dev, "Selected ANC slot not present\n");
1196 release_firmware(fw);
1197 return -ENOMEM;
1198 }
1199
1200 for (i = 0; i < anc_writes_size; i++) {
1201 SITAR_CODEC_UNPACK_ENTRY(anc_ptr[i], reg,
1202 mask, val);
1203 old_val = snd_soc_read(codec, reg);
1204 snd_soc_write(codec, reg, (old_val & ~mask) |
1205 (val & mask));
1206 }
1207
1208 release_firmware(fw);
1209
1210 /* For Sitar, it is required to enable both Feed-forward
1211 * and Feed back clocks to enable ANC
1212 */
1213 snd_soc_write(codec, SITAR_A_CDC_CLK_ANC_CLK_EN_CTL, 0x0F);
1214
1215 break;
1216
1217 case SND_SOC_DAPM_POST_PMD:
1218 snd_soc_write(codec, SITAR_A_CDC_CLK_ANC_RESET_CTL, 0xFF);
1219 snd_soc_write(codec, SITAR_A_CDC_CLK_ANC_CLK_EN_CTL, 0x00);
1220 break;
1221 }
1222 return 0;
1223}
1224
1225
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301226static void sitar_codec_start_hs_polling(struct snd_soc_codec *codec)
1227{
1228 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001229 int mbhc_state = sitar->mbhc_state;
1230
1231 pr_debug("%s: enter\n", __func__);
1232 if (!sitar->mbhc_polling_active) {
1233 pr_debug("Polling is not active, do not start polling\n");
1234 return;
1235 }
1236 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
1237
1238
1239 if (!sitar->no_mic_headset_override) {
1240 if (mbhc_state == MBHC_STATE_POTENTIAL) {
1241 pr_debug("%s recovering MBHC state macine\n", __func__);
1242 sitar->mbhc_state = MBHC_STATE_POTENTIAL_RECOVERY;
1243 /* set to max button press threshold */
1244 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B2_CTL,
1245 0x7F);
1246 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B1_CTL,
1247 0xFF);
1248 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B4_CTL,
1249 0x7F);
1250 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B3_CTL,
1251 0xFF);
1252 /* set to max */
1253 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B6_CTL,
1254 0x7F);
1255 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B5_CTL,
1256 0xFF);
1257 }
1258 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301259
1260 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001261 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x1);
1262 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x0);
1263 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x1);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301264}
1265
1266static void sitar_codec_pause_hs_polling(struct snd_soc_codec *codec)
1267{
1268 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1269
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001270 pr_debug("%s: enter\n", __func__);
1271 if (!sitar->mbhc_polling_active) {
1272 pr_debug("polling not active, nothing to pause\n");
1273 return;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301274 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001275
1276 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
1277 pr_debug("%s: leave\n", __func__);
1278
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301279}
1280
1281static void sitar_codec_switch_cfilt_mode(struct snd_soc_codec *codec,
1282 int mode)
1283{
1284 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1285 u8 reg_mode_val, cur_mode_val;
1286 bool mbhc_was_polling = false;
1287
1288 if (mode)
1289 reg_mode_val = SITAR_CFILT_FAST_MODE;
1290 else
1291 reg_mode_val = SITAR_CFILT_SLOW_MODE;
1292
1293 cur_mode_val = snd_soc_read(codec,
1294 sitar->mbhc_bias_regs.cfilt_ctl) & 0x40;
1295
1296 if (cur_mode_val != reg_mode_val) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001297 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301298 if (sitar->mbhc_polling_active) {
1299 sitar_codec_pause_hs_polling(codec);
1300 mbhc_was_polling = true;
1301 }
1302 snd_soc_update_bits(codec,
1303 sitar->mbhc_bias_regs.cfilt_ctl, 0x40, reg_mode_val);
1304 if (mbhc_was_polling)
1305 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001306 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301307 pr_debug("%s: CFILT mode change (%x to %x)\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301308 cur_mode_val, reg_mode_val);
1309 } else {
1310 pr_err("%s: CFILT Value is already %x\n",
1311 __func__, cur_mode_val);
1312 }
1313}
1314
1315static void sitar_codec_update_cfilt_usage(struct snd_soc_codec *codec,
1316 u8 cfilt_sel, int inc)
1317{
1318 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1319 u32 *cfilt_cnt_ptr = NULL;
1320 u16 micb_cfilt_reg;
1321
1322 switch (cfilt_sel) {
1323 case SITAR_CFILT1_SEL:
1324 cfilt_cnt_ptr = &sitar->cfilt1_cnt;
1325 micb_cfilt_reg = SITAR_A_MICB_CFILT_1_CTL;
1326 break;
1327 case SITAR_CFILT2_SEL:
1328 cfilt_cnt_ptr = &sitar->cfilt2_cnt;
1329 micb_cfilt_reg = SITAR_A_MICB_CFILT_2_CTL;
1330 break;
1331 default:
1332 return; /* should not happen */
1333 }
1334
1335 if (inc) {
1336 if (!(*cfilt_cnt_ptr)++) {
1337 /* Switch CFILT to slow mode if MBHC CFILT being used */
1338 if (cfilt_sel == sitar->mbhc_bias_regs.cfilt_sel)
1339 sitar_codec_switch_cfilt_mode(codec, 0);
1340
1341 snd_soc_update_bits(codec, micb_cfilt_reg, 0x80, 0x80);
1342 }
1343 } else {
1344 /* check if count not zero, decrement
1345 * then check if zero, go ahead disable cfilter
1346 */
1347 if ((*cfilt_cnt_ptr) && !--(*cfilt_cnt_ptr)) {
1348 snd_soc_update_bits(codec, micb_cfilt_reg, 0x80, 0);
1349
1350 /* Switch CFILT to fast mode if MBHC CFILT being used */
1351 if (cfilt_sel == sitar->mbhc_bias_regs.cfilt_sel)
1352 sitar_codec_switch_cfilt_mode(codec, 1);
1353 }
1354 }
1355}
1356
1357static int sitar_find_k_value(unsigned int ldoh_v, unsigned int cfilt_mv)
1358{
1359 int rc = -EINVAL;
1360 unsigned min_mv, max_mv;
1361
1362 switch (ldoh_v) {
1363 case SITAR_LDOH_1P95_V:
1364 min_mv = 160;
1365 max_mv = 1800;
1366 break;
1367 case SITAR_LDOH_2P35_V:
1368 min_mv = 200;
1369 max_mv = 2200;
1370 break;
1371 case SITAR_LDOH_2P75_V:
1372 min_mv = 240;
1373 max_mv = 2600;
1374 break;
1375 case SITAR_LDOH_2P85_V:
1376 min_mv = 250;
1377 max_mv = 2700;
1378 break;
1379 default:
1380 goto done;
1381 }
1382
1383 if (cfilt_mv < min_mv || cfilt_mv > max_mv)
1384 goto done;
1385
1386 for (rc = 4; rc <= 44; rc++) {
1387 min_mv = max_mv * (rc) / 44;
1388 if (min_mv >= cfilt_mv) {
1389 rc -= 4;
1390 break;
1391 }
1392 }
1393done:
1394 return rc;
1395}
1396
1397static bool sitar_is_hph_pa_on(struct snd_soc_codec *codec)
1398{
1399 u8 hph_reg_val = 0;
1400 hph_reg_val = snd_soc_read(codec, SITAR_A_RX_HPH_CNP_EN);
1401
1402 return (hph_reg_val & 0x30) ? true : false;
1403}
1404
1405static bool sitar_is_hph_dac_on(struct snd_soc_codec *codec, int left)
1406{
1407 u8 hph_reg_val = 0;
1408 if (left)
1409 hph_reg_val = snd_soc_read(codec,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001410 SITAR_A_RX_HPH_L_DAC_CTL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301411 else
1412 hph_reg_val = snd_soc_read(codec,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001413 SITAR_A_RX_HPH_R_DAC_CTL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301414
1415 return (hph_reg_val & 0xC0) ? true : false;
1416}
1417
1418static void sitar_codec_switch_micbias(struct snd_soc_codec *codec,
1419 int vddio_switch)
1420{
1421 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1422 int cfilt_k_val;
1423 bool mbhc_was_polling = false;
1424
1425 switch (vddio_switch) {
1426 case 1:
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001427 if (sitar->mbhc_micbias_switched == 0 &&
1428 sitar->mbhc_polling_active) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301429
1430 sitar_codec_pause_hs_polling(codec);
1431 /* Enable Mic Bias switch to VDDIO */
1432 sitar->cfilt_k_value = snd_soc_read(codec,
1433 sitar->mbhc_bias_regs.cfilt_val);
1434 cfilt_k_val = sitar_find_k_value(
1435 sitar->pdata->micbias.ldoh_v, 1800);
1436 snd_soc_update_bits(codec,
1437 sitar->mbhc_bias_regs.cfilt_val,
1438 0xFC, (cfilt_k_val << 2));
1439
1440 snd_soc_update_bits(codec,
1441 sitar->mbhc_bias_regs.mbhc_reg, 0x80, 0x80);
1442 snd_soc_update_bits(codec,
1443 sitar->mbhc_bias_regs.mbhc_reg, 0x10, 0x00);
1444 sitar_codec_start_hs_polling(codec);
1445
1446 sitar->mbhc_micbias_switched = true;
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301447 pr_debug("%s: Enabled MBHC Mic bias to VDDIO Switch\n",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301448 __func__);
1449 }
1450 break;
1451
1452 case 0:
1453 if (sitar->mbhc_micbias_switched) {
1454 if (sitar->mbhc_polling_active) {
1455 sitar_codec_pause_hs_polling(codec);
1456 mbhc_was_polling = true;
1457 }
1458 /* Disable Mic Bias switch to VDDIO */
1459 if (sitar->cfilt_k_value != 0)
1460 snd_soc_update_bits(codec,
1461 sitar->mbhc_bias_regs.cfilt_val, 0XFC,
1462 sitar->cfilt_k_value);
1463 snd_soc_update_bits(codec,
1464 sitar->mbhc_bias_regs.mbhc_reg, 0x80, 0x00);
1465 snd_soc_update_bits(codec,
1466 sitar->mbhc_bias_regs.mbhc_reg, 0x10, 0x00);
1467
1468 if (mbhc_was_polling)
1469 sitar_codec_start_hs_polling(codec);
1470
1471 sitar->mbhc_micbias_switched = false;
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301472 pr_debug("%s: Disabled MBHC Mic bias to VDDIO Switch\n",
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301473 __func__);
1474 }
1475 break;
1476 }
1477}
1478
1479static int sitar_codec_enable_micbias(struct snd_soc_dapm_widget *w,
1480 struct snd_kcontrol *kcontrol, int event)
1481{
1482 struct snd_soc_codec *codec = w->codec;
1483 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1484 u16 micb_int_reg;
1485 int micb_line;
1486 u8 cfilt_sel_val = 0;
1487 char *internal1_text = "Internal1";
1488 char *internal2_text = "Internal2";
1489
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301490 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301491 switch (w->reg) {
1492 case SITAR_A_MICB_1_CTL:
1493 micb_int_reg = SITAR_A_MICB_1_INT_RBIAS;
1494 cfilt_sel_val = sitar->pdata->micbias.bias1_cfilt_sel;
1495 micb_line = SITAR_MICBIAS1;
1496 break;
1497 case SITAR_A_MICB_2_CTL:
1498 micb_int_reg = SITAR_A_MICB_2_INT_RBIAS;
1499 cfilt_sel_val = sitar->pdata->micbias.bias2_cfilt_sel;
1500 micb_line = SITAR_MICBIAS2;
1501 break;
1502 default:
1503 pr_err("%s: Error, invalid micbias register\n", __func__);
1504 return -EINVAL;
1505 }
1506
1507 switch (event) {
1508 case SND_SOC_DAPM_PRE_PMU:
1509 /* Decide whether to switch the micbias for MBHC */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001510 if (w->reg == sitar->mbhc_bias_regs.ctl_reg) {
1511 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301512 sitar_codec_switch_micbias(codec, 0);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001513 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
1514 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301515
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001516 snd_soc_update_bits(codec, w->reg, 0x1E, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301517 sitar_codec_update_cfilt_usage(codec, cfilt_sel_val, 1);
1518
1519 if (strnstr(w->name, internal1_text, 30))
1520 snd_soc_update_bits(codec, micb_int_reg, 0xFF, 0xA4);
1521 else if (strnstr(w->name, internal2_text, 30))
1522 snd_soc_update_bits(codec, micb_int_reg, 0x1C, 0x1C);
1523 break;
1524 case SND_SOC_DAPM_POST_PMU:
1525 if (sitar->mbhc_polling_active &&
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -07001526 sitar->mbhc_cfg.micbias == micb_line) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001527 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301528 sitar_codec_pause_hs_polling(codec);
1529 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001530 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301531 }
1532 break;
1533 case SND_SOC_DAPM_POST_PMD:
1534
1535 if ((w->reg == sitar->mbhc_bias_regs.ctl_reg)
1536 && sitar_is_hph_pa_on(codec))
1537 sitar_codec_switch_micbias(codec, 1);
1538
1539 if (strnstr(w->name, internal1_text, 30))
1540 snd_soc_update_bits(codec, micb_int_reg, 0x80, 0x00);
1541 else if (strnstr(w->name, internal2_text, 30))
1542 snd_soc_update_bits(codec, micb_int_reg, 0x10, 0x00);
1543 sitar_codec_update_cfilt_usage(codec, cfilt_sel_val, 0);
1544 break;
1545 }
1546
1547 return 0;
1548}
1549
1550static int sitar_codec_enable_dec(struct snd_soc_dapm_widget *w,
1551 struct snd_kcontrol *kcontrol, int event)
1552{
1553 struct snd_soc_codec *codec = w->codec;
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07001554 u16 dec_reset_reg, gain_reg;
1555 u8 current_gain;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301556
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301557 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301558
1559 if (w->reg == SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL)
1560 dec_reset_reg = SITAR_A_CDC_CLK_TX_RESET_B1_CTL;
1561 else {
1562 pr_err("%s: Error, incorrect dec\n", __func__);
1563 return -EINVAL;
1564 }
1565
1566 switch (event) {
1567 case SND_SOC_DAPM_PRE_PMU:
1568 snd_soc_update_bits(codec, dec_reset_reg, 1 << w->shift,
1569 1 << w->shift);
1570 snd_soc_update_bits(codec, dec_reset_reg, 1 << w->shift, 0x0);
1571 break;
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07001572 case SND_SOC_DAPM_POST_PMU:
1573 /* Reprogram the digital gain after power up of Decimator */
1574 gain_reg = SITAR_A_CDC_TX1_VOL_CTL_GAIN + (8 * w->shift);
1575 current_gain = snd_soc_read(codec, gain_reg);
1576 snd_soc_write(codec, gain_reg, current_gain);
1577 break;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301578 }
1579 return 0;
1580}
1581
1582static int sitar_codec_reset_interpolator(struct snd_soc_dapm_widget *w,
1583 struct snd_kcontrol *kcontrol, int event)
1584{
1585 struct snd_soc_codec *codec = w->codec;
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07001586 u16 gain_reg;
1587 u8 current_gain;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301588
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301589 pr_debug("%s %d %s\n", __func__, event, w->name);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301590
1591 switch (event) {
1592 case SND_SOC_DAPM_PRE_PMU:
1593 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_RX_RESET_CTL,
1594 1 << w->shift, 1 << w->shift);
1595 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_RX_RESET_CTL,
1596 1 << w->shift, 0x0);
1597 break;
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07001598 case SND_SOC_DAPM_POST_PMU:
1599 /* Reprogram gain after power up interpolator */
1600 gain_reg = SITAR_A_CDC_RX1_VOL_CTL_B2_CTL + (8 * w->shift);
1601 current_gain = snd_soc_read(codec, gain_reg);
1602 snd_soc_write(codec, gain_reg, current_gain);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301603 }
1604 return 0;
1605}
1606
1607static int sitar_codec_enable_ldo_h(struct snd_soc_dapm_widget *w,
1608 struct snd_kcontrol *kcontrol, int event)
1609{
1610 switch (event) {
1611 case SND_SOC_DAPM_POST_PMU:
1612 case SND_SOC_DAPM_POST_PMD:
1613 usleep_range(1000, 1000);
1614 pr_debug("LDO_H\n");
1615 break;
1616 }
1617 return 0;
1618}
1619
1620static void sitar_enable_rx_bias(struct snd_soc_codec *codec, u32 enable)
1621{
1622 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1623
1624 if (enable) {
1625 sitar->rx_bias_count++;
1626 if (sitar->rx_bias_count == 1)
1627 snd_soc_update_bits(codec, SITAR_A_RX_COM_BIAS,
1628 0x80, 0x80);
1629 } else {
1630 sitar->rx_bias_count--;
1631 if (!sitar->rx_bias_count)
1632 snd_soc_update_bits(codec, SITAR_A_RX_COM_BIAS,
1633 0x80, 0x00);
1634 }
1635}
1636
1637static int sitar_codec_enable_rx_bias(struct snd_soc_dapm_widget *w,
1638 struct snd_kcontrol *kcontrol, int event)
1639{
1640 struct snd_soc_codec *codec = w->codec;
1641
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301642 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301643
1644 switch (event) {
1645 case SND_SOC_DAPM_PRE_PMU:
1646 sitar_enable_rx_bias(codec, 1);
1647 break;
1648 case SND_SOC_DAPM_POST_PMD:
1649 sitar_enable_rx_bias(codec, 0);
1650 break;
1651 }
1652 return 0;
1653}
Bhalchandra Gajare66131712012-05-07 14:12:26 -07001654static int sitar_hph_dac_event(struct snd_soc_dapm_widget *w,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301655 struct snd_kcontrol *kcontrol, int event)
1656{
1657 struct snd_soc_codec *codec = w->codec;
1658
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301659 pr_debug("%s %s %d\n", __func__, w->name, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301660
1661 switch (event) {
1662 case SND_SOC_DAPM_PRE_PMU:
Bhalchandra Gajarecb370232012-07-26 10:44:53 -07001663 if (w->reg == SITAR_A_RX_HPH_L_DAC_CTL) {
1664 snd_soc_update_bits(codec, SITAR_A_CDC_CONN_CLSG_CTL,
1665 0x30, 0x20);
1666 snd_soc_update_bits(codec, SITAR_A_CDC_CONN_CLSG_CTL,
1667 0x0C, 0x08);
1668 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301669 snd_soc_update_bits(codec, w->reg, 0x40, 0x40);
1670 break;
1671 case SND_SOC_DAPM_POST_PMD:
1672 snd_soc_update_bits(codec, w->reg, 0x40, 0x00);
Bhalchandra Gajarecb370232012-07-26 10:44:53 -07001673 if (w->reg == SITAR_A_RX_HPH_L_DAC_CTL) {
1674 snd_soc_update_bits(codec, SITAR_A_CDC_CONN_CLSG_CTL,
1675 0x30, 0x10);
1676 snd_soc_update_bits(codec, SITAR_A_CDC_CONN_CLSG_CTL,
1677 0x0C, 0x04);
1678 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301679 break;
1680 }
1681 return 0;
1682}
1683
1684static void sitar_snd_soc_jack_report(struct sitar_priv *sitar,
1685 struct snd_soc_jack *jack, int status,
1686 int mask)
1687{
1688 /* XXX: wake_lock_timeout()? */
1689 snd_soc_jack_report(jack, status, mask);
1690}
1691
1692static void hphocp_off_report(struct sitar_priv *sitar,
1693 u32 jack_status, int irq)
1694{
1695 struct snd_soc_codec *codec;
1696
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001697 if (!sitar) {
1698 pr_err("%s: Bad sitar private data\n", __func__);
1699 return;
1700 }
1701
1702 pr_info("%s: clear ocp status %x\n", __func__, jack_status);
1703 codec = sitar->codec;
1704 if (sitar->hph_status & jack_status) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301705 sitar->hph_status &= ~jack_status;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001706 if (sitar->mbhc_cfg.headset_jack)
1707 sitar_snd_soc_jack_report(sitar,
1708 sitar->mbhc_cfg.headset_jack,
1709 sitar->hph_status,
1710 SITAR_JACK_MASK);
1711 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10, 0x00);
1712 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10, 0x10);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301713 /* reset retry counter as PA is turned off signifying
1714 * start of new OCP detection session
1715 */
Joonwoo Parkf6574c72012-10-10 17:29:57 -07001716 if (WCD9XXX_IRQ_HPH_PA_OCPL_FAULT)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301717 sitar->hphlocp_cnt = 0;
1718 else
1719 sitar->hphrocp_cnt = 0;
1720 wcd9xxx_enable_irq(codec->control_data, irq);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301721 }
1722}
1723
1724static void hphlocp_off_report(struct work_struct *work)
1725{
1726 struct sitar_priv *sitar = container_of(work, struct sitar_priv,
1727 hphlocp_work);
Joonwoo Parkf6574c72012-10-10 17:29:57 -07001728 hphocp_off_report(sitar, SND_JACK_OC_HPHL,
1729 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301730}
1731
1732static void hphrocp_off_report(struct work_struct *work)
1733{
1734 struct sitar_priv *sitar = container_of(work, struct sitar_priv,
1735 hphrocp_work);
Joonwoo Parkf6574c72012-10-10 17:29:57 -07001736 hphocp_off_report(sitar, SND_JACK_OC_HPHR,
1737 WCD9XXX_IRQ_HPH_PA_OCPR_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301738}
1739
1740static int sitar_hph_pa_event(struct snd_soc_dapm_widget *w,
1741 struct snd_kcontrol *kcontrol, int event)
1742{
1743 struct snd_soc_codec *codec = w->codec;
1744 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
1745 u8 mbhc_micb_ctl_val;
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301746 pr_debug("%s: event = %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301747
1748 switch (event) {
1749 case SND_SOC_DAPM_PRE_PMU:
1750 mbhc_micb_ctl_val = snd_soc_read(codec,
1751 sitar->mbhc_bias_regs.ctl_reg);
1752
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001753 if (!(mbhc_micb_ctl_val & 0x80)) {
1754 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301755 sitar_codec_switch_micbias(codec, 1);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001756 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
1757 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301758
1759 break;
1760
1761 case SND_SOC_DAPM_POST_PMD:
1762 /* schedule work is required because at the time HPH PA DAPM
1763 * event callback is called by DAPM framework, CODEC dapm mutex
1764 * would have been locked while snd_soc_jack_report also
1765 * attempts to acquire same lock.
1766 */
1767 if (w->shift == 5) {
1768 clear_bit(SITAR_HPHL_PA_OFF_ACK,
1769 &sitar->hph_pa_dac_state);
1770 clear_bit(SITAR_HPHL_DAC_OFF_ACK,
1771 &sitar->hph_pa_dac_state);
1772 if (sitar->hph_status & SND_JACK_OC_HPHL)
1773 schedule_work(&sitar->hphlocp_work);
1774 } else if (w->shift == 4) {
1775 clear_bit(SITAR_HPHR_PA_OFF_ACK,
1776 &sitar->hph_pa_dac_state);
1777 clear_bit(SITAR_HPHR_DAC_OFF_ACK,
1778 &sitar->hph_pa_dac_state);
1779 if (sitar->hph_status & SND_JACK_OC_HPHR)
1780 schedule_work(&sitar->hphrocp_work);
1781 }
1782
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07001783 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
1784 sitar_codec_switch_micbias(codec, 0);
1785 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301786
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301787 pr_debug("%s: sleep 10 ms after %s PA disable.\n", __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301788 w->name);
1789 usleep_range(10000, 10000);
1790
1791 break;
1792 }
1793 return 0;
1794}
1795
1796static void sitar_get_mbhc_micbias_regs(struct snd_soc_codec *codec,
1797 struct mbhc_micbias_regs *micbias_regs)
1798{
1799 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301800 unsigned int cfilt;
1801
Patrick Laia5062da2012-05-11 17:55:09 -07001802 switch (sitar->mbhc_cfg.micbias) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301803 case SITAR_MICBIAS1:
1804 cfilt = sitar->pdata->micbias.bias1_cfilt_sel;
1805 micbias_regs->mbhc_reg = SITAR_A_MICB_1_MBHC;
1806 micbias_regs->int_rbias = SITAR_A_MICB_1_INT_RBIAS;
1807 micbias_regs->ctl_reg = SITAR_A_MICB_1_CTL;
1808 break;
1809 case SITAR_MICBIAS2:
1810 cfilt = sitar->pdata->micbias.bias2_cfilt_sel;
1811 micbias_regs->mbhc_reg = SITAR_A_MICB_2_MBHC;
1812 micbias_regs->int_rbias = SITAR_A_MICB_2_INT_RBIAS;
1813 micbias_regs->ctl_reg = SITAR_A_MICB_2_CTL;
1814 break;
1815 default:
1816 /* Should never reach here */
1817 pr_err("%s: Invalid MIC BIAS for MBHC\n", __func__);
1818 return;
1819 }
1820
1821 micbias_regs->cfilt_sel = cfilt;
1822
1823 switch (cfilt) {
1824 case SITAR_CFILT1_SEL:
1825 micbias_regs->cfilt_val = SITAR_A_MICB_CFILT_1_VAL;
1826 micbias_regs->cfilt_ctl = SITAR_A_MICB_CFILT_1_CTL;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001827 sitar->mbhc_data.micb_mv = sitar->pdata->micbias.cfilt1_mv;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301828 break;
1829 case SITAR_CFILT2_SEL:
1830 micbias_regs->cfilt_val = SITAR_A_MICB_CFILT_2_VAL;
1831 micbias_regs->cfilt_ctl = SITAR_A_MICB_CFILT_2_CTL;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001832 sitar->mbhc_data.micb_mv = sitar->pdata->micbias.cfilt2_mv;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301833 break;
1834 }
1835}
1836
1837static int sitar_codec_enable_charge_pump(struct snd_soc_dapm_widget *w,
1838 struct snd_kcontrol *kcontrol, int event)
1839{
1840 struct snd_soc_codec *codec = w->codec;
1841
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05301842 pr_debug("%s %d\n", __func__, event);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301843 switch (event) {
1844 case SND_SOC_DAPM_POST_PMU:
Bhalchandra Gajare2776af12012-04-27 16:59:39 -07001845 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_RESET_CTL, 0x10,
1846 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301847 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_CTL, 0x01,
1848 0x01);
1849 snd_soc_update_bits(codec, SITAR_A_CDC_CLSG_CTL, 0x08, 0x08);
1850 usleep_range(200, 200);
1851 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x10, 0x00);
1852 break;
1853 case SND_SOC_DAPM_PRE_PMD:
1854 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_RESET_CTL, 0x10,
1855 0x10);
1856 usleep_range(20, 20);
1857 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x08, 0x08);
1858 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x10, 0x10);
1859 snd_soc_update_bits(codec, SITAR_A_CDC_CLSG_CTL, 0x08, 0x00);
1860 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_OTHR_CTL, 0x01,
1861 0x00);
1862 snd_soc_update_bits(codec, SITAR_A_CP_STATIC, 0x08, 0x00);
1863 break;
1864 }
1865 return 0;
1866}
1867
1868static const struct snd_soc_dapm_widget sitar_dapm_i2s_widgets[] = {
1869 SND_SOC_DAPM_SUPPLY("RX_I2S_CLK", SITAR_A_CDC_CLK_RX_I2S_CTL,
1870 4, 0, NULL, 0),
1871 SND_SOC_DAPM_SUPPLY("TX_I2S_CLK", SITAR_A_CDC_CLK_TX_I2S_CTL, 4,
1872 0, NULL, 0),
1873};
1874
1875static const struct snd_soc_dapm_widget sitar_dapm_widgets[] = {
1876 /*RX stuff */
1877 SND_SOC_DAPM_OUTPUT("EAR"),
1878
1879 SND_SOC_DAPM_PGA("EAR PA", SITAR_A_RX_EAR_EN, 4, 0, NULL, 0),
1880 SND_SOC_DAPM_MIXER("DAC1", SITAR_A_RX_EAR_EN, 6, 0, dac1_switch,
1881 ARRAY_SIZE(dac1_switch)),
1882 SND_SOC_DAPM_SUPPLY("EAR DRIVER", SITAR_A_RX_EAR_EN, 3, 0, NULL, 0),
1883 SND_SOC_DAPM_AIF_IN_E("SLIM RX1", "AIF1 Playback", 0, SND_SOC_NOPM, 0,
1884 0, sitar_codec_enable_slimrx,
1885 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1886 SND_SOC_DAPM_AIF_IN_E("SLIM RX2", "AIF1 Playback", 0, SND_SOC_NOPM, 0,
1887 0, sitar_codec_enable_slimrx,
1888 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1889 SND_SOC_DAPM_AIF_IN("SLIM RX3", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1890 SND_SOC_DAPM_AIF_IN("SLIM RX4", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1891 SND_SOC_DAPM_AIF_IN("SLIM RX5", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1892
1893 /* Headphone */
1894 SND_SOC_DAPM_OUTPUT("HEADPHONE"),
1895 SND_SOC_DAPM_PGA_E("HPHL", SITAR_A_RX_HPH_CNP_EN, 5, 0, NULL, 0,
1896 sitar_hph_pa_event, SND_SOC_DAPM_PRE_PMU |
1897 SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301898
1899 SND_SOC_DAPM_PGA_E("HPHR", SITAR_A_RX_HPH_CNP_EN, 4, 0, NULL, 0,
1900 sitar_hph_pa_event, SND_SOC_DAPM_PRE_PMU |
1901 SND_SOC_DAPM_POST_PMD),
1902
Bhalchandra Gajare66131712012-05-07 14:12:26 -07001903 SND_SOC_DAPM_DAC_E("HPHL DAC", NULL, SITAR_A_RX_HPH_L_DAC_CTL, 7, 0,
1904 sitar_hph_dac_event,
1905 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301906 SND_SOC_DAPM_DAC_E("HPHR DAC", NULL, SITAR_A_RX_HPH_R_DAC_CTL, 7, 0,
Bhalchandra Gajare66131712012-05-07 14:12:26 -07001907 sitar_hph_dac_event,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301908 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1909
1910 /* Speaker */
1911 SND_SOC_DAPM_OUTPUT("LINEOUT1"),
1912 SND_SOC_DAPM_OUTPUT("LINEOUT2"),
1913
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001914 SND_SOC_DAPM_DAC_E("LINEOUT1 DAC", NULL, SITAR_A_RX_LINE_1_DAC_CTL, 7, 0
1915 , sitar_lineout_dac_event,
1916 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1917 SND_SOC_DAPM_DAC_E("LINEOUT2 DAC", NULL, SITAR_A_RX_LINE_2_DAC_CTL, 7, 0
1918 , sitar_lineout_dac_event,
1919 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1920
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301921 SND_SOC_DAPM_PGA_E("LINEOUT1 PA", SITAR_A_RX_LINE_CNP_EN, 0, 0, NULL,
1922 0, sitar_codec_enable_lineout, SND_SOC_DAPM_PRE_PMU |
1923 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1924 SND_SOC_DAPM_PGA_E("LINEOUT2 PA", SITAR_A_RX_LINE_CNP_EN, 1, 0, NULL,
1925 0, sitar_codec_enable_lineout, SND_SOC_DAPM_PRE_PMU |
1926 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1927
1928 SND_SOC_DAPM_MIXER_E("RX1 MIX1", SITAR_A_CDC_CLK_RX_B1_CTL, 0, 0, NULL,
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07001929 0, sitar_codec_reset_interpolator,
1930 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301931 SND_SOC_DAPM_MIXER_E("RX2 MIX1", SITAR_A_CDC_CLK_RX_B1_CTL, 1, 0, NULL,
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07001932 0, sitar_codec_reset_interpolator,
1933 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301934 SND_SOC_DAPM_MIXER_E("RX3 MIX1", SITAR_A_CDC_CLK_RX_B1_CTL, 2, 0, NULL,
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07001935 0, sitar_codec_reset_interpolator,
1936 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301937
1938 SND_SOC_DAPM_MUX("DAC1 MUX", SND_SOC_NOPM, 0, 0,
1939 &rx_dac1_mux),
1940 SND_SOC_DAPM_MUX("DAC2 MUX", SND_SOC_NOPM, 0, 0,
1941 &rx_dac2_mux),
1942 SND_SOC_DAPM_MUX("DAC3 MUX", SND_SOC_NOPM, 0, 0,
1943 &rx_dac3_mux),
1944 SND_SOC_DAPM_MUX("DAC4 MUX", SND_SOC_NOPM, 0, 0,
1945 &rx_dac4_mux),
1946
1947 SND_SOC_DAPM_MIXER("RX1 CHAIN", SITAR_A_CDC_RX1_B6_CTL, 5, 0, NULL, 0),
1948 SND_SOC_DAPM_MIXER("RX2 CHAIN", SITAR_A_CDC_RX2_B6_CTL, 5, 0, NULL, 0),
1949 SND_SOC_DAPM_MIXER("RX3 CHAIN", SITAR_A_CDC_RX3_B6_CTL, 5, 0, NULL, 0),
1950
1951 SND_SOC_DAPM_MUX("RX1 MIX1 INP1", SND_SOC_NOPM, 0, 0,
1952 &rx_mix1_inp1_mux),
1953 SND_SOC_DAPM_MUX("RX1 MIX1 INP2", SND_SOC_NOPM, 0, 0,
1954 &rx_mix1_inp2_mux),
1955 SND_SOC_DAPM_MUX("RX2 MIX1 INP1", SND_SOC_NOPM, 0, 0,
1956 &rx2_mix1_inp1_mux),
1957 SND_SOC_DAPM_MUX("RX2 MIX1 INP2", SND_SOC_NOPM, 0, 0,
1958 &rx2_mix1_inp2_mux),
1959 SND_SOC_DAPM_MUX("RX3 MIX1 INP1", SND_SOC_NOPM, 0, 0,
1960 &rx3_mix1_inp1_mux),
1961 SND_SOC_DAPM_MUX("RX3 MIX1 INP2", SND_SOC_NOPM, 0, 0,
1962 &rx3_mix1_inp2_mux),
1963
1964 SND_SOC_DAPM_SUPPLY("CP", SITAR_A_CP_EN, 0, 0,
1965 sitar_codec_enable_charge_pump, SND_SOC_DAPM_POST_PMU |
1966 SND_SOC_DAPM_PRE_PMD),
1967 SND_SOC_DAPM_SUPPLY("RX_BIAS", SND_SOC_NOPM, 0, 0,
1968 sitar_codec_enable_rx_bias, SND_SOC_DAPM_PRE_PMU |
1969 SND_SOC_DAPM_POST_PMD),
1970
1971 SND_SOC_DAPM_SUPPLY("LDO_H", SITAR_A_LDO_H_MODE_1, 7, 0,
1972 sitar_codec_enable_ldo_h, SND_SOC_DAPM_POST_PMU),
1973 /* TX */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001974
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301975 SND_SOC_DAPM_SUPPLY("CDC_CONN", SITAR_A_CDC_CLK_OTHR_CTL, 2, 0, NULL,
1976 0),
1977 SND_SOC_DAPM_INPUT("AMIC1"),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08001978 SND_SOC_DAPM_INPUT("AMIC2"),
1979 SND_SOC_DAPM_INPUT("AMIC3"),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301980 SND_SOC_DAPM_MICBIAS_E("MIC BIAS1 External", SITAR_A_MICB_1_CTL, 7, 0,
1981 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1982 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1983 SND_SOC_DAPM_MICBIAS_E("MIC BIAS1 Internal1", SITAR_A_MICB_1_CTL, 7, 0,
1984 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1985 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1986
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301987 SND_SOC_DAPM_MICBIAS_E("MIC BIAS2 External", SITAR_A_MICB_2_CTL, 7, 0,
1988 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1989 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1990 SND_SOC_DAPM_MICBIAS_E("MIC BIAS2 Internal1", SITAR_A_MICB_2_CTL, 7, 0,
1991 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1992 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1993 SND_SOC_DAPM_MICBIAS_E("MIC BIAS2 Internal2", SITAR_A_MICB_2_CTL, 7, 0,
1994 sitar_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
1995 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1996
1997 SND_SOC_DAPM_ADC_E("ADC1", NULL, SITAR_A_TX_1_2_EN, 7, 0,
1998 sitar_codec_enable_adc, SND_SOC_DAPM_PRE_PMU |
1999 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
2000 SND_SOC_DAPM_ADC_E("ADC2", NULL, SITAR_A_TX_1_2_EN, 3, 0,
2001 sitar_codec_enable_adc, SND_SOC_DAPM_PRE_PMU |
2002 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002003 SND_SOC_DAPM_ADC_E("ADC3", NULL, SITAR_A_TX_3_EN, 7, 0,
2004 sitar_codec_enable_adc, SND_SOC_DAPM_PRE_PMU |
2005 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302006
2007 SND_SOC_DAPM_MUX_E("DEC1 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 0, 0,
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07002008 &dec1_mux, sitar_codec_enable_dec,
2009 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002010 SND_SOC_DAPM_MUX_E("DEC2 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 1, 0,
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07002011 &dec2_mux, sitar_codec_enable_dec,
2012 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002013 SND_SOC_DAPM_MUX_E("DEC3 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 2, 0,
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07002014 &dec3_mux, sitar_codec_enable_dec,
2015 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002016 SND_SOC_DAPM_MUX_E("DEC4 MUX", SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 3, 0,
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07002017 &dec4_mux, sitar_codec_enable_dec,
2018 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302019
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07002020 SND_SOC_DAPM_MUX("ANC1 MUX", SND_SOC_NOPM, 0, 0, &anc1_mux),
2021 SND_SOC_DAPM_MUX("ANC2 MUX", SND_SOC_NOPM, 0, 0, &anc2_mux),
2022
2023 SND_SOC_DAPM_MIXER_E("ANC", SND_SOC_NOPM, 0, 0, NULL, 0,
2024 sitar_codec_enable_anc, SND_SOC_DAPM_PRE_PMU |
2025 SND_SOC_DAPM_POST_PMD),
2026
2027 SND_SOC_DAPM_MUX("ANC1 FB MUX", SND_SOC_NOPM, 0, 0, &anc1_fb_mux),
2028
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302029 SND_SOC_DAPM_MUX("SLIM TX1 MUX", SND_SOC_NOPM, 0, 0, &sb_tx1_mux),
2030 SND_SOC_DAPM_MUX("SLIM TX2 MUX", SND_SOC_NOPM, 0, 0, &sb_tx2_mux),
2031 SND_SOC_DAPM_MUX("SLIM TX3 MUX", SND_SOC_NOPM, 0, 0, &sb_tx3_mux),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002032 SND_SOC_DAPM_MUX("SLIM TX4 MUX", SND_SOC_NOPM, 0, 0, &sb_tx4_mux),
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002033 SND_SOC_DAPM_MUX("SLIM TX5 MUX", SND_SOC_NOPM, 0, 0, &sb_tx5_mux),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302034
2035 SND_SOC_DAPM_AIF_OUT_E("SLIM TX1", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
2036 0, sitar_codec_enable_slimtx,
2037 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
2038
2039 SND_SOC_DAPM_AIF_OUT_E("SLIM TX2", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
2040 0, sitar_codec_enable_slimtx,
2041 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
2042
2043 SND_SOC_DAPM_AIF_OUT_E("SLIM TX3", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
2044 0, sitar_codec_enable_slimtx,
2045 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002046 SND_SOC_DAPM_AIF_OUT_E("SLIM TX4", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
2047 0, sitar_codec_enable_slimtx,
2048 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302049
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002050 SND_SOC_DAPM_AIF_OUT_E("SLIM TX5", "AIF1 Capture", 0, SND_SOC_NOPM, 0,
2051 0, sitar_codec_enable_slimtx,
2052 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
2053
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302054 /* Digital Mic Inputs */
2055 SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
2056 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
2057 SND_SOC_DAPM_POST_PMD),
2058 SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 0, 0,
2059 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
2060 SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302061 SND_SOC_DAPM_ADC_E("DMIC3", NULL, SND_SOC_NOPM, 0, 0,
2062 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
2063 SND_SOC_DAPM_POST_PMD),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302064 SND_SOC_DAPM_ADC_E("DMIC4", NULL, SND_SOC_NOPM, 0, 0,
2065 sitar_codec_enable_dmic, SND_SOC_DAPM_PRE_PMU |
2066 SND_SOC_DAPM_POST_PMD),
2067
2068 /* Sidetone */
2069 SND_SOC_DAPM_MUX("IIR1 INP1 MUX", SND_SOC_NOPM, 0, 0, &iir1_inp1_mux),
2070 SND_SOC_DAPM_PGA("IIR1", SITAR_A_CDC_CLK_SD_CTL, 0, 0, NULL, 0),
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302071 SND_SOC_DAPM_MUX("IIR2 INP1 MUX", SND_SOC_NOPM, 0, 0, &iir2_inp1_mux),
2072 SND_SOC_DAPM_PGA("IIR2", SITAR_A_CDC_CLK_SD_CTL, 1, 0, NULL, 0),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302073
2074};
2075
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05302076static const struct snd_soc_dapm_route audio_i2s_map[] = {
2077 {"RX_I2S_CLK", NULL, "CP"},
2078 {"RX_I2S_CLK", NULL, "CDC_CONN"},
2079 {"SLIM RX1", NULL, "RX_I2S_CLK"},
2080 {"SLIM RX2", NULL, "RX_I2S_CLK"},
2081 {"SLIM RX3", NULL, "RX_I2S_CLK"},
2082 {"SLIM RX4", NULL, "RX_I2S_CLK"},
2083
2084 {"SLIM TX1", NULL, "TX_I2S_CLK"},
2085 {"SLIM TX2", NULL, "TX_I2S_CLK"},
2086 {"SLIM TX3", NULL, "TX_I2S_CLK"},
2087 {"SLIM TX4", NULL, "TX_I2S_CLK"},
2088};
2089
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302090static const struct snd_soc_dapm_route audio_map[] = {
2091 /* Earpiece (RX MIX1) */
2092 {"EAR", NULL, "EAR PA"},
2093 {"EAR PA", "NULL", "DAC1"},
2094 {"DAC1", "Switch", "DAC1 MUX"},
2095 {"DAC1", NULL, "CP"},
2096 {"DAC1", NULL, "EAR DRIVER"},
2097
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002098 {"CP", NULL, "RX_BIAS"},
2099
2100 {"LINEOUT1 DAC", NULL, "RX_BIAS"},
2101 {"LINEOUT2 DAC", NULL, "RX_BIAS"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302102
2103 {"LINEOUT2", NULL, "LINEOUT2 PA"},
Bhalchandra Gajareaca4f5b2012-06-13 15:05:15 -07002104 {"LINEOUT2 PA", NULL, "CP"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002105 {"LINEOUT2 PA", NULL, "LINEOUT2 DAC"},
2106 {"LINEOUT2 DAC", NULL, "DAC3 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302107
2108 {"LINEOUT1", NULL, "LINEOUT1 PA"},
Bhalchandra Gajareaca4f5b2012-06-13 15:05:15 -07002109 {"LINEOUT2 PA", NULL, "CP"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002110 {"LINEOUT1 PA", NULL, "LINEOUT1 DAC"},
2111 {"LINEOUT1 DAC", NULL, "DAC2 MUX"},
2112
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07002113 {"ANC1 FB MUX", "EAR_HPH_L", "RX2 MIX1"},
2114 {"ANC1 FB MUX", "EAR_LINE_1", "RX3 MIX1"},
2115 {"ANC", NULL, "ANC1 FB MUX"},
2116
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302117
2118 /* Headset (RX MIX1 and RX MIX2) */
2119 {"HEADPHONE", NULL, "HPHL"},
2120 {"HEADPHONE", NULL, "HPHR"},
2121
Bhalchandra Gajarecb370232012-07-26 10:44:53 -07002122
Patrick Lai9b250b72012-05-24 14:45:57 -07002123 {"HPHL DAC", NULL, "CP"},
2124 {"HPHR DAC", NULL, "CP"},
2125
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302126 {"HPHL", NULL, "HPHL DAC"},
Bhalchandra Gajarecb370232012-07-26 10:44:53 -07002127 {"HPHL DAC", "NULL", "RX2 CHAIN"},
2128 {"RX2 CHAIN", NULL, "DAC4 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302129 {"HPHR", NULL, "HPHR DAC"},
Bhalchandra Gajarecb370232012-07-26 10:44:53 -07002130 {"HPHR DAC", NULL, "RX3 CHAIN"},
2131 {"RX3 CHAIN", NULL, "RX3 MIX1"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302132
2133 {"DAC1 MUX", "RX1", "RX1 CHAIN"},
2134 {"DAC2 MUX", "RX1", "RX1 CHAIN"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002135
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302136 {"DAC3 MUX", "RX1", "RX1 CHAIN"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002137 {"DAC3 MUX", "INV_RX1", "RX1 CHAIN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302138 {"DAC3 MUX", "RX2", "RX2 MIX1"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002139
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302140 {"DAC4 MUX", "ON", "RX2 MIX1"},
2141
2142 {"RX1 CHAIN", NULL, "RX1 MIX1"},
2143
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302144 {"RX1 MIX1", NULL, "RX1 MIX1 INP1"},
2145 {"RX1 MIX1", NULL, "RX1 MIX1 INP2"},
2146 {"RX2 MIX1", NULL, "RX2 MIX1 INP1"},
2147 {"RX2 MIX1", NULL, "RX2 MIX1 INP2"},
2148 {"RX3 MIX1", NULL, "RX3 MIX1 INP1"},
2149 {"RX3 MIX1", NULL, "RX3 MIX1 INP2"},
2150
Bhalchandra Gajare160c64a2012-06-06 18:31:50 -07002151 /* ANC */
2152 {"ANC", NULL, "ANC1 MUX"},
2153 {"ANC", NULL, "ANC2 MUX"},
2154 {"ANC1 MUX", "ADC1", "ADC1"},
2155 {"ANC1 MUX", "ADC2", "ADC2"},
2156 {"ANC1 MUX", "ADC3", "ADC3"},
2157 {"ANC2 MUX", "ADC1", "ADC1"},
2158 {"ANC2 MUX", "ADC2", "ADC2"},
2159 {"ANC2 MUX", "ADC3", "ADC3"},
2160
2161 {"ANC", NULL, "CDC_CONN"},
2162
2163 {"RX2 MIX1", NULL, "ANC"},
2164 {"RX3 MIX1", NULL, "ANC"},
2165
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302166 /* SLIMBUS Connections */
2167
2168 /* Slimbus port 5 is non functional in Sitar 1.0 */
2169 {"RX1 MIX1 INP1", "RX1", "SLIM RX1"},
2170 {"RX1 MIX1 INP1", "RX2", "SLIM RX2"},
2171 {"RX1 MIX1 INP1", "RX3", "SLIM RX3"},
2172 {"RX1 MIX1 INP1", "RX4", "SLIM RX4"},
2173 {"RX1 MIX1 INP1", "IIR1", "IIR1"},
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302174 {"RX1 MIX1 INP1", "IIR2", "IIR2"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302175 {"RX1 MIX1 INP2", "RX1", "SLIM RX1"},
2176 {"RX1 MIX1 INP2", "RX2", "SLIM RX2"},
2177 {"RX1 MIX1 INP2", "RX3", "SLIM RX3"},
2178 {"RX1 MIX1 INP2", "RX4", "SLIM RX4"},
2179 {"RX1 MIX1 INP2", "IIR1", "IIR1"},
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302180 {"RX1 MIX1 INP2", "IIR2", "IIR2"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302181 {"RX2 MIX1 INP1", "RX1", "SLIM RX1"},
2182 {"RX2 MIX1 INP1", "RX2", "SLIM RX2"},
2183 {"RX2 MIX1 INP1", "RX3", "SLIM RX3"},
2184 {"RX2 MIX1 INP1", "RX4", "SLIM RX4"},
2185 {"RX2 MIX1 INP1", "IIR1", "IIR1"},
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302186 {"RX2 MIX1 INP1", "IIR2", "IIR2"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302187 {"RX2 MIX1 INP2", "RX1", "SLIM RX1"},
2188 {"RX2 MIX1 INP2", "RX2", "SLIM RX2"},
2189 {"RX2 MIX1 INP2", "RX3", "SLIM RX3"},
2190 {"RX2 MIX1 INP2", "RX4", "SLIM RX4"},
2191 {"RX2 MIX1 INP2", "IIR1", "IIR1"},
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302192 {"RX2 MIX1 INP2", "IIR2", "IIR2"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302193 {"RX3 MIX1 INP1", "RX1", "SLIM RX1"},
2194 {"RX3 MIX1 INP1", "RX2", "SLIM RX2"},
2195 {"RX3 MIX1 INP1", "RX3", "SLIM RX3"},
2196 {"RX3 MIX1 INP1", "RX4", "SLIM RX4"},
2197 {"RX3 MIX1 INP1", "IIR1", "IIR1"},
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302198 {"RX3 MIX1 INP1", "IIR2", "IIR2"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302199 {"RX3 MIX1 INP2", "RX1", "SLIM RX1"},
2200 {"RX3 MIX1 INP2", "RX2", "SLIM RX2"},
2201 {"RX3 MIX1 INP2", "RX3", "SLIM RX3"},
2202 {"RX3 MIX1 INP2", "RX4", "SLIM RX4"},
2203 {"RX3 MIX1 INP2", "IIR1", "IIR1"},
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302204 {"RX3 MIX1 INP2", "IIR2", "IIR2"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302205
2206
2207 /* TX */
2208 {"SLIM TX1", NULL, "SLIM TX1 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302209 {"SLIM TX2", NULL, "SLIM TX2 MUX"},
2210 {"SLIM TX3", NULL, "SLIM TX3 MUX"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002211 {"SLIM TX4", NULL, "SLIM TX4 MUX"},
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002212 {"SLIM TX5", NULL, "SLIM TX5 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302213
2214 {"SLIM TX1 MUX", "DEC1", "DEC1 MUX"},
2215 {"SLIM TX2 MUX", "DEC2", "DEC2 MUX"},
2216 {"SLIM TX3 MUX", "DEC3", "DEC3 MUX"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002217 {"SLIM TX4 MUX", "DEC4", "DEC4 MUX"},
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002218 {"SLIM TX5 MUX", "DEC1", "DEC1 MUX"},
2219 {"SLIM TX5 MUX", "DEC2", "DEC2 MUX"},
2220 {"SLIM TX5 MUX", "DEC3", "DEC3 MUX"},
2221 {"SLIM TX5 MUX", "DEC4", "DEC4 MUX"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302222
2223 /* Decimator Inputs */
2224 {"DEC1 MUX", "DMIC1", "DMIC1"},
2225 {"DEC1 MUX", "DMIC4", "DMIC4"},
2226 {"DEC1 MUX", "ADC1", "ADC1"},
2227 {"DEC1 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002228 {"DEC1 MUX", "ADC3", "ADC3"},
2229 {"DEC1 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302230 {"DEC2 MUX", "DMIC2", "DMIC2"},
2231 {"DEC2 MUX", "DMIC3", "DMIC3"},
2232 {"DEC2 MUX", "ADC1", "ADC1"},
2233 {"DEC2 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002234 {"DEC2 MUX", "ADC3", "ADC3"},
2235 {"DEC2 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302236 {"DEC3 MUX", "DMIC3", "DMIC3"},
2237 {"DEC3 MUX", "ADC1", "ADC1"},
2238 {"DEC3 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002239 {"DEC3 MUX", "ADC3", "ADC3"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302240 {"DEC3 MUX", "DMIC2", "DMIC2"},
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -07002241 {"DEC3 MUX", "DMIC4", "DMIC4"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002242 {"DEC3 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302243 {"DEC4 MUX", "DMIC4", "DMIC4"},
2244 {"DEC4 MUX", "ADC1", "ADC1"},
2245 {"DEC4 MUX", "ADC2", "ADC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002246 {"DEC4 MUX", "ADC3", "ADC3"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302247 {"DEC4 MUX", "DMIC3", "DMIC3"},
2248 {"DEC4 MUX", "DMIC2", "DMIC2"},
2249 {"DEC4 MUX", "DMIC1", "DMIC1"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002250 {"DEC4 MUX", NULL, "CDC_CONN"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302251
2252 /* ADC Connections */
2253 {"ADC1", NULL, "AMIC1"},
2254 {"ADC2", NULL, "AMIC2"},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002255 {"ADC3", NULL, "AMIC3"},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302256
2257 /* IIR */
2258 {"IIR1", NULL, "IIR1 INP1 MUX"},
2259 {"IIR1 INP1 MUX", "DEC1", "DEC1 MUX"},
Asish Bhattacharyaa7354892012-05-28 12:49:01 +05302260 {"IIR1 INP1 MUX", "DEC2", "DEC2 MUX"},
2261 {"IIR1 INP1 MUX", "DEC3", "DEC3 MUX"},
2262 {"IIR1 INP1 MUX", "DEC4", "DEC4 MUX"},
2263 {"IIR1 INP1 MUX", "RX1", "SLIM RX1"},
2264 {"IIR1 INP1 MUX", "RX2", "SLIM RX2"},
2265 {"IIR1 INP1 MUX", "RX3", "SLIM RX3"},
2266 {"IIR1 INP1 MUX", "RX4", "SLIM RX4"},
2267 {"IIR1 INP1 MUX", "RX5", "SLIM RX5"},
2268
2269 {"IIR2", NULL, "IIR2 INP1 MUX"},
2270 {"IIR2 INP1 MUX", "DEC1", "DEC1 MUX"},
2271 {"IIR2 INP1 MUX", "DEC2", "DEC2 MUX"},
2272 {"IIR2 INP1 MUX", "DEC3", "DEC3 MUX"},
2273 {"IIR2 INP1 MUX", "DEC4", "DEC4 MUX"},
2274 {"IIR2 INP1 MUX", "RX1", "SLIM RX1"},
2275 {"IIR2 INP1 MUX", "RX2", "SLIM RX2"},
2276 {"IIR2 INP1 MUX", "RX3", "SLIM RX3"},
2277 {"IIR2 INP1 MUX", "RX4", "SLIM RX4"},
2278 {"IIR2 INP1 MUX", "RX5", "SLIM RX5"},
2279
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302280 {"MIC BIAS1 Internal1", NULL, "LDO_H"},
2281 {"MIC BIAS1 External", NULL, "LDO_H"},
2282 {"MIC BIAS2 Internal1", NULL, "LDO_H"},
2283 {"MIC BIAS2 External", NULL, "LDO_H"},
2284};
2285
2286static int sitar_readable(struct snd_soc_codec *ssc, unsigned int reg)
2287{
2288 return sitar_reg_readable[reg];
2289}
2290
2291static int sitar_volatile(struct snd_soc_codec *ssc, unsigned int reg)
2292{
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07002293 int i;
2294
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302295 /* Registers lower than 0x100 are top level registers which can be
2296 * written by the Sitar core driver.
2297 */
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302298 if ((reg >= SITAR_A_CDC_MBHC_EN_CTL) || (reg < 0x100))
2299 return 1;
2300
2301 /* IIR Coeff registers are not cacheable */
2302 if ((reg >= SITAR_A_CDC_IIR1_COEF_B1_CTL) &&
2303 (reg <= SITAR_A_CDC_IIR1_COEF_B5_CTL))
2304 return 1;
2305
Bhalchandra Gajareddd45102012-06-13 17:12:34 -07002306 for (i = 0; i < NUM_DECIMATORS; i++) {
2307 if (reg == SITAR_A_CDC_TX1_VOL_CTL_GAIN + (8 * i))
2308 return 1;
2309 }
2310
2311 for (i = 0; i < NUM_INTERPOLATORS; i++) {
2312 if (reg == SITAR_A_CDC_RX1_VOL_CTL_B2_CTL + (8 * i))
2313 return 1;
2314 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302315 return 0;
2316}
2317
2318#define SITAR_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
2319static int sitar_write(struct snd_soc_codec *codec, unsigned int reg,
2320 unsigned int value)
2321{
2322 int ret;
2323
2324 BUG_ON(reg > SITAR_MAX_REGISTER);
2325
2326 if (!sitar_volatile(codec, reg)) {
2327 ret = snd_soc_cache_write(codec, reg, value);
2328 if (ret != 0)
2329 dev_err(codec->dev, "Cache write to %x failed: %d\n",
2330 reg, ret);
2331 }
2332
2333 return wcd9xxx_reg_write(codec->control_data, reg, value);
2334}
2335static unsigned int sitar_read(struct snd_soc_codec *codec,
2336 unsigned int reg)
2337{
2338 unsigned int val;
2339 int ret;
2340
2341 BUG_ON(reg > SITAR_MAX_REGISTER);
2342
2343 if (!sitar_volatile(codec, reg) && sitar_readable(codec, reg) &&
2344 reg < codec->driver->reg_cache_size) {
2345 ret = snd_soc_cache_read(codec, reg, &val);
2346 if (ret >= 0) {
2347 return val;
2348 } else
2349 dev_err(codec->dev, "Cache read from %x failed: %d\n",
2350 reg, ret);
2351 }
2352
2353 val = wcd9xxx_reg_read(codec->control_data, reg);
2354 return val;
2355}
2356
2357static void sitar_codec_enable_audio_mode_bandgap(struct snd_soc_codec *codec)
2358{
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002359 struct wcd9xxx *sitar_core = dev_get_drvdata(codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302360
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002361 if (SITAR_IS_1P0(sitar_core->version))
2362 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1, 0x80, 0x80);
2363
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002364 snd_soc_update_bits(codec, SITAR_A_BIAS_CURR_CTL_2, 0x0C, 0x08);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302365 usleep_range(1000, 1000);
2366 snd_soc_write(codec, SITAR_A_BIAS_REF_CTL, 0x1C);
2367 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2368 0x80);
2369 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x04,
2370 0x04);
2371 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x01,
2372 0x01);
2373 usleep_range(1000, 1000);
2374 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2375 0x00);
2376}
2377
2378static void sitar_codec_enable_bandgap(struct snd_soc_codec *codec,
2379 enum sitar_bandgap_type choice)
2380{
2381 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002382 struct wcd9xxx *sitar_core = dev_get_drvdata(codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302383
2384 /* TODO lock resources accessed by audio streams and threaded
2385 * interrupt handlers
2386 */
2387
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302388 pr_debug("%s, choice is %d, current is %d\n", __func__, choice,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302389 sitar->bandgap_type);
2390
2391 if (sitar->bandgap_type == choice)
2392 return;
2393
2394 if ((sitar->bandgap_type == SITAR_BANDGAP_OFF) &&
2395 (choice == SITAR_BANDGAP_AUDIO_MODE)) {
2396 sitar_codec_enable_audio_mode_bandgap(codec);
2397 } else if (choice == SITAR_BANDGAP_MBHC_MODE) {
Bhalchandra Gajare7708ee32012-05-24 17:37:37 -07002398 snd_soc_update_bits(codec, SITAR_A_BIAS_CURR_CTL_2, 0x0C, 0x08);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302399 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x2,
2400 0x2);
2401 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2402 0x80);
2403 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x4,
2404 0x4);
2405 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x01,
2406 0x1);
2407 usleep_range(1000, 1000);
2408 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x80,
2409 0x00);
2410 } else if ((sitar->bandgap_type == SITAR_BANDGAP_MBHC_MODE) &&
2411 (choice == SITAR_BANDGAP_AUDIO_MODE)) {
2412 snd_soc_write(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x50);
2413 usleep_range(100, 100);
2414 sitar_codec_enable_audio_mode_bandgap(codec);
2415 } else if (choice == SITAR_BANDGAP_OFF) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002416 snd_soc_update_bits(codec, SITAR_A_BIAS_CURR_CTL_2, 0x0C, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302417 snd_soc_write(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x50);
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07002418 if (SITAR_IS_1P0(sitar_core->version))
2419 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1,
Asish Bhattacharya63032b42012-08-16 20:57:01 +05302420 0xF3, 0x61);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002421 usleep_range(1000, 1000);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302422 } else {
2423 pr_err("%s: Error, Invalid bandgap settings\n", __func__);
2424 }
2425 sitar->bandgap_type = choice;
2426}
2427
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002428static int sitar_codec_enable_config_mode(struct snd_soc_codec *codec,
2429 int enable)
2430{
2431 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2432
2433 if (enable) {
2434 snd_soc_update_bits(codec, SITAR_A_RC_OSC_FREQ, 0x10, 0);
2435 snd_soc_write(codec, SITAR_A_BIAS_OSC_BG_CTL, 0x17);
2436 usleep_range(5, 5);
2437 snd_soc_update_bits(codec, SITAR_A_RC_OSC_FREQ, 0x80,
2438 0x80);
2439 snd_soc_update_bits(codec, SITAR_A_RC_OSC_TEST, 0x80,
2440 0x80);
2441 usleep_range(10, 10);
2442 snd_soc_update_bits(codec, SITAR_A_RC_OSC_TEST, 0x80, 0);
2443 usleep_range(20, 20);
2444 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x08, 0x08);
2445 } else {
2446 snd_soc_update_bits(codec, SITAR_A_BIAS_OSC_BG_CTL, 0x1,
2447 0);
2448 snd_soc_update_bits(codec, SITAR_A_RC_OSC_FREQ, 0x80, 0);
2449 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x08, 0x00);
2450 }
2451 sitar->config_mode_active = enable ? true : false;
2452
2453 return 0;
2454}
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302455
2456static int sitar_codec_enable_clock_block(struct snd_soc_codec *codec,
2457 int config_mode)
2458{
2459 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2460
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302461 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302462
2463 if (config_mode) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002464 sitar_codec_enable_config_mode(codec, 1);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302465 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN2, 0x00);
2466 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN2, 0x02);
2467 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN1, 0x0D);
2468 usleep_range(1000, 1000);
2469 } else
2470 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x08, 0x00);
2471
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002472 if (!config_mode && sitar->mbhc_polling_active) {
2473 snd_soc_write(codec, SITAR_A_CLK_BUFF_EN2, 0x02);
2474 sitar_codec_enable_config_mode(codec, 0);
2475
2476 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302477
2478 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x05, 0x05);
2479 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x02, 0x00);
2480 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x04, 0x04);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302481 usleep_range(50, 50);
2482 sitar->clock_active = true;
2483 return 0;
2484}
2485static void sitar_codec_disable_clock_block(struct snd_soc_codec *codec)
2486{
2487 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302488 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302489 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x04, 0x00);
2490 ndelay(160);
2491 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN2, 0x02, 0x02);
2492 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x05, 0x00);
2493 sitar->clock_active = false;
2494}
2495
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002496static int sitar_codec_mclk_index(const struct sitar_priv *sitar)
2497{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002498 if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_12288KHZ)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002499 return 0;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002500 else if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_9600KHZ)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002501 return 1;
2502 else {
2503 BUG_ON(1);
2504 return -EINVAL;
2505 }
2506}
2507
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302508static void sitar_codec_calibrate_hs_polling(struct snd_soc_codec *codec)
2509{
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002510 u8 *n_ready, *n_cic;
2511 struct sitar_mbhc_btn_detect_cfg *btn_det;
2512 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302513
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002514 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(sitar->mbhc_cfg.calibration);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302515
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002516 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B1_CTL,
2517 sitar->mbhc_data.v_ins_hu & 0xFF);
2518 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B2_CTL,
2519 (sitar->mbhc_data.v_ins_hu >> 8) & 0xFF);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302520
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08002521 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B3_CTL,
2522 sitar->mbhc_data.v_b1_hu & 0xFF);
2523 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B4_CTL,
2524 (sitar->mbhc_data.v_b1_hu >> 8) & 0xFF);
2525
2526 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B5_CTL,
2527 sitar->mbhc_data.v_b1_h & 0xFF);
2528 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B6_CTL,
2529 (sitar->mbhc_data.v_b1_h >> 8) & 0xFF);
2530
2531 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B9_CTL,
2532 sitar->mbhc_data.v_brh & 0xFF);
2533 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B10_CTL,
2534 (sitar->mbhc_data.v_brh >> 8) & 0xFF);
2535
2536 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B11_CTL,
2537 sitar->mbhc_data.v_brl & 0xFF);
2538 snd_soc_write(codec, SITAR_A_CDC_MBHC_VOLT_B12_CTL,
2539 (sitar->mbhc_data.v_brl >> 8) & 0xFF);
2540
2541 n_ready = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_READY);
2542 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B1_CTL,
2543 n_ready[sitar_codec_mclk_index(sitar)]);
2544 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B2_CTL,
2545 sitar->mbhc_data.npoll);
2546 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B3_CTL,
2547 sitar->mbhc_data.nbounce_wait);
2548 n_cic = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_CIC);
2549 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B6_CTL,
2550 n_cic[sitar_codec_mclk_index(sitar)]);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302551}
2552
2553static int sitar_startup(struct snd_pcm_substream *substream,
2554 struct snd_soc_dai *dai)
2555{
Asish Bhattacharya1d069532012-03-07 13:25:24 -08002556 struct wcd9xxx *wcd9xxx = dev_get_drvdata(dai->codec->dev->parent);
2557 if ((wcd9xxx != NULL) && (wcd9xxx->dev != NULL) &&
2558 (wcd9xxx->dev->parent != NULL))
2559 pm_runtime_get_sync(wcd9xxx->dev->parent);
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302560 pr_debug("%s(): substream = %s stream = %d\n" , __func__,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302561 substream->name, substream->stream);
2562
2563 return 0;
2564}
2565
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07002566static void sitar_codec_pm_runtime_put(struct wcd9xxx *sitar)
2567{
2568 if (sitar->dev != NULL &&
2569 sitar->dev->parent != NULL) {
2570 pm_runtime_mark_last_busy(sitar->dev->parent);
2571 pm_runtime_put(sitar->dev->parent);
2572 }
2573}
2574
2575static void sitar_shutdown(struct snd_pcm_substream *substream,
2576 struct snd_soc_dai *dai)
2577{
2578 struct wcd9xxx *sitar_core = dev_get_drvdata(dai->codec->dev->parent);
2579 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(dai->codec);
2580 u32 active = 0;
2581
2582 pr_debug("%s(): substream = %s stream = %d\n" , __func__,
2583 substream->name, substream->stream);
2584 if (sitar->intf_type != WCD9XXX_INTERFACE_TYPE_SLIMBUS)
2585 return;
2586
2587 if (dai->id <= NUM_CODEC_DAIS) {
2588 if (sitar->dai[dai->id-1].ch_mask) {
2589 active = 1;
2590 pr_debug("%s(): Codec DAI: chmask[%d] = 0x%x\n",
2591 __func__, dai->id-1,
2592 sitar->dai[dai->id-1].ch_mask);
2593 }
2594 }
2595
2596 if (sitar_core != NULL && active == 0)
2597 sitar_codec_pm_runtime_put(sitar_core);
2598}
2599
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002600int sitar_mclk_enable(struct snd_soc_codec *codec, int mclk_enable, bool dapm)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302601{
2602 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
2603
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302604 pr_debug("%s() mclk_enable = %u\n", __func__, mclk_enable);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302605
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002606 if (dapm)
2607 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302608 if (mclk_enable) {
2609 sitar->mclk_enabled = true;
2610
2611 if (sitar->mbhc_polling_active && (sitar->mclk_enabled)) {
2612 sitar_codec_pause_hs_polling(codec);
2613 sitar_codec_enable_bandgap(codec,
2614 SITAR_BANDGAP_AUDIO_MODE);
2615 sitar_codec_enable_clock_block(codec, 0);
2616 sitar_codec_calibrate_hs_polling(codec);
2617 sitar_codec_start_hs_polling(codec);
2618 } else {
2619 sitar_codec_enable_bandgap(codec,
2620 SITAR_BANDGAP_AUDIO_MODE);
2621 sitar_codec_enable_clock_block(codec, 0);
2622 }
2623 } else {
2624
2625 if (!sitar->mclk_enabled) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002626 if (dapm)
2627 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302628 pr_err("Error, MCLK already diabled\n");
2629 return -EINVAL;
2630 }
2631 sitar->mclk_enabled = false;
2632
2633 if (sitar->mbhc_polling_active) {
2634 if (!sitar->mclk_enabled) {
2635 sitar_codec_pause_hs_polling(codec);
2636 sitar_codec_enable_bandgap(codec,
2637 SITAR_BANDGAP_MBHC_MODE);
2638 sitar_enable_rx_bias(codec, 1);
2639 sitar_codec_enable_clock_block(codec, 1);
2640 sitar_codec_calibrate_hs_polling(codec);
2641 sitar_codec_start_hs_polling(codec);
2642 }
2643 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1,
2644 0x05, 0x01);
2645 } else {
2646 sitar_codec_disable_clock_block(codec);
2647 sitar_codec_enable_bandgap(codec,
2648 SITAR_BANDGAP_OFF);
2649 }
2650 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07002651 if (dapm)
2652 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302653 return 0;
2654}
2655
2656static int sitar_set_dai_sysclk(struct snd_soc_dai *dai,
2657 int clk_id, unsigned int freq, int dir)
2658{
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302659 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302660 return 0;
2661}
2662
2663static int sitar_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2664{
2665 u8 val = 0;
2666 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(dai->codec);
2667
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302668 pr_debug("%s\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302669 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2670 case SND_SOC_DAIFMT_CBS_CFS:
2671 /* CPU is master */
2672 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2673 if (dai->id == AIF1_CAP)
2674 snd_soc_update_bits(dai->codec,
2675 SITAR_A_CDC_CLK_TX_I2S_CTL,
2676 SITAR_I2S_MASTER_MODE_MASK, 0);
2677 else if (dai->id == AIF1_PB)
2678 snd_soc_update_bits(dai->codec,
2679 SITAR_A_CDC_CLK_RX_I2S_CTL,
2680 SITAR_I2S_MASTER_MODE_MASK, 0);
2681 }
2682 break;
2683 case SND_SOC_DAIFMT_CBM_CFM:
2684 /* CPU is slave */
2685 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2686 val = SITAR_I2S_MASTER_MODE_MASK;
2687 if (dai->id == AIF1_CAP)
2688 snd_soc_update_bits(dai->codec,
2689 SITAR_A_CDC_CLK_TX_I2S_CTL, val, val);
2690 else if (dai->id == AIF1_PB)
2691 snd_soc_update_bits(dai->codec,
2692 SITAR_A_CDC_CLK_RX_I2S_CTL, val, val);
2693 }
2694 break;
2695 default:
2696 return -EINVAL;
2697 }
2698 return 0;
2699}
2700static int sitar_set_channel_map(struct snd_soc_dai *dai,
2701 unsigned int tx_num, unsigned int *tx_slot,
2702 unsigned int rx_num, unsigned int *rx_slot)
2703
2704{
2705 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(dai->codec);
2706 u32 i = 0;
2707 if (!tx_slot && !rx_slot) {
2708 pr_err("%s: Invalid\n", __func__);
2709 return -EINVAL;
2710 }
2711 pr_debug("%s: DAI-ID %x %d %d\n", __func__, dai->id, tx_num, rx_num);
2712
2713 if (dai->id == AIF1_PB) {
2714 for (i = 0; i < rx_num; i++) {
2715 sitar->dai[dai->id - 1].ch_num[i] = rx_slot[i];
2716 sitar->dai[dai->id - 1].ch_act = 0;
2717 sitar->dai[dai->id - 1].ch_tot = rx_num;
2718 }
2719 } else if (dai->id == AIF1_CAP) {
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07002720 sitar->dai[dai->id - 1].ch_tot = tx_num;
2721 /* If all channels are already active,
2722 * Do not reset ch_act flag
2723 */
2724 if ((sitar->dai[dai->id - 1].ch_tot != 0)
2725 && (sitar->dai[dai->id - 1].ch_act ==
2726 sitar->dai[dai->id - 1].ch_tot)) {
2727 pr_info("%s: ch_act = %d, ch_tot = %d\n", __func__,
2728 sitar->dai[dai->id - 1].ch_act,
2729 sitar->dai[dai->id - 1].ch_tot);
2730
2731 return 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302732 }
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07002733 sitar->dai[dai->id - 1].ch_act = 0;
2734
2735 for (i = 0; i < tx_num; i++)
2736 sitar->dai[dai->id - 1].ch_num[i] = tx_slot[i];
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302737 }
2738 return 0;
2739}
2740
2741static int sitar_get_channel_map(struct snd_soc_dai *dai,
2742 unsigned int *tx_num, unsigned int *tx_slot,
2743 unsigned int *rx_num, unsigned int *rx_slot)
2744
2745{
2746 struct wcd9xxx *sitar = dev_get_drvdata(dai->codec->control_data);
2747
2748 u32 cnt = 0;
2749 u32 tx_ch[SLIM_MAX_TX_PORTS];
2750 u32 rx_ch[SLIM_MAX_RX_PORTS];
2751
2752 if (!rx_slot && !tx_slot) {
2753 pr_err("%s: Invalid\n", __func__);
2754 return -EINVAL;
2755 }
2756 pr_debug("%s: DAI-ID %x\n", __func__, dai->id);
2757 /* for virtual port, codec driver needs to do
2758 * housekeeping, for now should be ok
2759 */
2760 wcd9xxx_get_channel(sitar, rx_ch, tx_ch);
2761 if (dai->id == AIF1_PB) {
2762 *rx_num = sitar_dai[dai->id - 1].playback.channels_max;
2763 while (cnt < *rx_num) {
2764 rx_slot[cnt] = rx_ch[cnt];
2765 cnt++;
2766 }
2767 } else if (dai->id == AIF1_CAP) {
2768 *tx_num = sitar_dai[dai->id - 1].capture.channels_max;
Bhalchandra Gajaree6a30f72012-05-23 19:46:21 -07002769 tx_slot[0] = tx_ch[cnt];
2770 tx_slot[1] = tx_ch[4 + cnt];
Bhalchandra Gajare9e5a4772012-05-31 12:22:33 -07002771 tx_slot[2] = tx_ch[2 + cnt];
2772 tx_slot[3] = tx_ch[3 + cnt];
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302773 }
2774 return 0;
2775}
2776
2777static int sitar_hw_params(struct snd_pcm_substream *substream,
2778 struct snd_pcm_hw_params *params,
2779 struct snd_soc_dai *dai)
2780{
2781 struct snd_soc_codec *codec = dai->codec;
2782 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(dai->codec);
2783 u8 path, shift;
2784 u16 tx_fs_reg, rx_fs_reg;
2785 u8 tx_fs_rate, rx_fs_rate, rx_state, tx_state;
2786
Asish Bhattacharya7147f8e2012-04-30 10:34:39 +05302787 pr_debug("%s: DAI-ID %x\n", __func__, dai->id);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302788
2789 switch (params_rate(params)) {
2790 case 8000:
2791 tx_fs_rate = 0x00;
2792 rx_fs_rate = 0x00;
2793 break;
2794 case 16000:
2795 tx_fs_rate = 0x01;
2796 rx_fs_rate = 0x20;
2797 break;
2798 case 32000:
2799 tx_fs_rate = 0x02;
2800 rx_fs_rate = 0x40;
2801 break;
2802 case 48000:
2803 tx_fs_rate = 0x03;
2804 rx_fs_rate = 0x60;
2805 break;
2806 default:
2807 pr_err("%s: Invalid sampling rate %d\n", __func__,
2808 params_rate(params));
2809 return -EINVAL;
2810 }
2811
2812
2813 /**
2814 * If current dai is a tx dai, set sample rate to
2815 * all the txfe paths that are currently not active
2816 */
2817 if (dai->id == AIF1_CAP) {
2818
2819 tx_state = snd_soc_read(codec,
2820 SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL);
2821
2822 for (path = 1, shift = 0;
2823 path <= NUM_DECIMATORS; path++, shift++) {
2824
2825 if (!(tx_state & (1 << shift))) {
2826 tx_fs_reg = SITAR_A_CDC_TX1_CLK_FS_CTL
2827 + (BITS_PER_REG*(path-1));
2828 snd_soc_update_bits(codec, tx_fs_reg,
2829 0x03, tx_fs_rate);
2830 }
2831 }
2832 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2833 switch (params_format(params)) {
2834 case SNDRV_PCM_FORMAT_S16_LE:
2835 snd_soc_update_bits(codec,
2836 SITAR_A_CDC_CLK_TX_I2S_CTL,
2837 0x20, 0x20);
2838 break;
2839 case SNDRV_PCM_FORMAT_S32_LE:
2840 snd_soc_update_bits(codec,
2841 SITAR_A_CDC_CLK_TX_I2S_CTL,
2842 0x20, 0x00);
2843 break;
2844 default:
2845 pr_err("invalid format\n");
2846 break;
2847 }
2848 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_TX_I2S_CTL,
2849 0x03, tx_fs_rate);
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05302850 } else {
2851 sitar->dai[dai->id - 1].rate = params_rate(params);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302852 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302853 }
2854
2855 /**
2856 * TODO: Need to handle case where same RX chain takes 2 or more inputs
2857 * with varying sample rates
2858 */
2859
2860 /**
2861 * If current dai is a rx dai, set sample rate to
2862 * all the rx paths that are currently not active
2863 */
2864 if (dai->id == AIF1_PB) {
2865
2866 rx_state = snd_soc_read(codec,
2867 SITAR_A_CDC_CLK_RX_B1_CTL);
2868
2869 for (path = 1, shift = 0;
2870 path <= NUM_INTERPOLATORS; path++, shift++) {
2871
2872 if (!(rx_state & (1 << shift))) {
2873 rx_fs_reg = SITAR_A_CDC_RX1_B5_CTL
2874 + (BITS_PER_REG*(path-1));
2875 snd_soc_update_bits(codec, rx_fs_reg,
2876 0xE0, rx_fs_rate);
2877 }
2878 }
2879 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
2880 switch (params_format(params)) {
2881 case SNDRV_PCM_FORMAT_S16_LE:
2882 snd_soc_update_bits(codec,
2883 SITAR_A_CDC_CLK_RX_I2S_CTL,
2884 0x20, 0x20);
2885 break;
2886 case SNDRV_PCM_FORMAT_S32_LE:
2887 snd_soc_update_bits(codec,
2888 SITAR_A_CDC_CLK_RX_I2S_CTL,
2889 0x20, 0x00);
2890 break;
2891 default:
2892 pr_err("invalid format\n");
2893 break;
2894 }
2895 snd_soc_update_bits(codec, SITAR_A_CDC_CLK_RX_I2S_CTL,
2896 0x03, (rx_fs_rate >> 0x05));
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05302897 } else {
2898 sitar->dai[dai->id - 1].rate = params_rate(params);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302899 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302900 }
2901
2902 return 0;
2903}
2904
2905static struct snd_soc_dai_ops sitar_dai_ops = {
2906 .startup = sitar_startup,
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07002907 .shutdown = sitar_shutdown,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05302908 .hw_params = sitar_hw_params,
2909 .set_sysclk = sitar_set_dai_sysclk,
2910 .set_fmt = sitar_set_dai_fmt,
2911 .set_channel_map = sitar_set_channel_map,
2912 .get_channel_map = sitar_get_channel_map,
2913};
2914
2915static struct snd_soc_dai_driver sitar_dai[] = {
2916 {
2917 .name = "sitar_rx1",
2918 .id = AIF1_PB,
2919 .playback = {
2920 .stream_name = "AIF1 Playback",
2921 .rates = WCD9304_RATES,
2922 .formats = SITAR_FORMATS,
2923 .rate_max = 48000,
2924 .rate_min = 8000,
2925 .channels_min = 1,
2926 .channels_max = 2,
2927 },
2928 .ops = &sitar_dai_ops,
2929 },
2930 {
2931 .name = "sitar_tx1",
2932 .id = AIF1_CAP,
2933 .capture = {
2934 .stream_name = "AIF1 Capture",
2935 .rates = WCD9304_RATES,
2936 .formats = SITAR_FORMATS,
2937 .rate_max = 48000,
2938 .rate_min = 8000,
2939 .channels_min = 1,
2940 .channels_max = 2,
2941 },
2942 .ops = &sitar_dai_ops,
2943 },
2944};
2945
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05302946static struct snd_soc_dai_driver sitar_i2s_dai[] = {
2947 {
2948 .name = "sitar_i2s_rx1",
2949 .id = AIF1_PB,
2950 .playback = {
2951 .stream_name = "AIF1 Playback",
2952 .rates = WCD9304_RATES,
2953 .formats = SITAR_FORMATS,
2954 .rate_max = 192000,
2955 .rate_min = 8000,
2956 .channels_min = 1,
2957 .channels_max = 4,
2958 },
2959 .ops = &sitar_dai_ops,
2960 },
2961 {
2962 .name = "sitar_i2s_tx1",
2963 .id = AIF1_CAP,
2964 .capture = {
2965 .stream_name = "AIF1 Capture",
2966 .rates = WCD9304_RATES,
2967 .formats = SITAR_FORMATS,
2968 .rate_max = 192000,
2969 .rate_min = 8000,
2970 .channels_min = 1,
2971 .channels_max = 4,
2972 },
2973 .ops = &sitar_dai_ops,
2974 },
2975};
2976
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07002977static int sitar_codec_enable_chmask(struct sitar_priv *sitar,
2978 int event, int index)
2979{
2980 int ret = 0;
2981 u32 k = 0;
2982 switch (event) {
2983 case SND_SOC_DAPM_POST_PMU:
2984 for (k = 0; k < sitar->dai[index].ch_tot; k++) {
2985 ret = wcd9xxx_get_slave_port(
2986 sitar->dai[index].ch_num[k]);
2987 if (ret < 0) {
2988 pr_err("%s: Invalid Slave port ID: %d\n",
2989 __func__, ret);
2990 ret = -EINVAL;
2991 break;
2992 }
2993 sitar->dai[index].ch_mask |= 1 << ret;
2994 }
2995 ret = 0;
2996 break;
2997 case SND_SOC_DAPM_POST_PMD:
2998 ret = wait_event_timeout(sitar->dai[index].dai_wait,
2999 (sitar->dai[index].ch_mask == 0),
3000 msecs_to_jiffies(SLIM_CLOSE_TIMEOUT));
3001 if (!ret) {
3002 pr_err("%s: slim close tx/rx timeout\n",
3003 __func__);
3004 ret = -EINVAL;
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07003005 } else {
3006 ret = 0;
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003007 }
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003008 break;
3009 }
3010 return ret;
3011}
3012
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303013static int sitar_codec_enable_slimrx(struct snd_soc_dapm_widget *w,
3014 struct snd_kcontrol *kcontrol, int event)
3015{
3016 struct wcd9xxx *sitar;
3017 struct snd_soc_codec *codec = w->codec;
3018 struct sitar_priv *sitar_p = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07003019 u32 j = 0;
3020 int ret = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303021 codec->control_data = dev_get_drvdata(codec->dev->parent);
3022 sitar = codec->control_data;
Bhalchandra Gajareac580a92012-07-10 17:14:33 -07003023
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303024 /* Execute the callback only if interface type is slimbus */
Bhalchandra Gajareac580a92012-07-10 17:14:33 -07003025 if (sitar_p->intf_type != WCD9XXX_INTERFACE_TYPE_SLIMBUS) {
3026 if (event == SND_SOC_DAPM_POST_PMD && (sitar != NULL))
3027 sitar_codec_pm_runtime_put(sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303028 return 0;
Bhalchandra Gajareac580a92012-07-10 17:14:33 -07003029 }
3030
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303031 switch (event) {
3032 case SND_SOC_DAPM_POST_PMU:
3033 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
3034 if (sitar_dai[j].id == AIF1_CAP)
3035 continue;
3036 if (!strncmp(w->sname,
3037 sitar_dai[j].playback.stream_name, 13)) {
3038 ++sitar_p->dai[j].ch_act;
3039 break;
3040 }
3041 }
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003042 if (sitar_p->dai[j].ch_act == sitar_p->dai[j].ch_tot) {
3043 ret = sitar_codec_enable_chmask(sitar_p, event, j);
3044 ret = wcd9xxx_cfg_slim_sch_rx(sitar,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303045 sitar_p->dai[j].ch_num,
3046 sitar_p->dai[j].ch_tot,
3047 sitar_p->dai[j].rate);
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003048 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303049 break;
3050 case SND_SOC_DAPM_POST_PMD:
3051 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
3052 if (sitar_dai[j].id == AIF1_CAP)
3053 continue;
3054 if (!strncmp(w->sname,
3055 sitar_dai[j].playback.stream_name, 13)) {
3056 --sitar_p->dai[j].ch_act;
3057 break;
3058 }
3059 }
3060 if (!sitar_p->dai[j].ch_act) {
3061 wcd9xxx_close_slim_sch_rx(sitar,
3062 sitar_p->dai[j].ch_num,
3063 sitar_p->dai[j].ch_tot);
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07003064 ret = sitar_codec_enable_chmask(sitar_p, event, j);
3065 if (ret < 0) {
3066 ret = wcd9xxx_disconnect_port(sitar,
3067 sitar_p->dai[j].ch_num,
3068 sitar_p->dai[j].ch_tot,
3069 1);
3070 pr_info("%s: Disconnect RX port ret = %d\n",
3071 __func__, ret);
3072 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303073 sitar_p->dai[j].rate = 0;
3074 memset(sitar_p->dai[j].ch_num, 0, (sizeof(u32)*
3075 sitar_p->dai[j].ch_tot));
3076 sitar_p->dai[j].ch_tot = 0;
Bhalchandra Gajareac580a92012-07-10 17:14:33 -07003077 if (sitar != NULL)
3078 sitar_codec_pm_runtime_put(sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303079 }
3080 }
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003081 return ret;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303082}
3083
3084static int sitar_codec_enable_slimtx(struct snd_soc_dapm_widget *w,
3085 struct snd_kcontrol *kcontrol, int event)
3086{
3087 struct wcd9xxx *sitar;
3088 struct snd_soc_codec *codec = w->codec;
3089 struct sitar_priv *sitar_p = snd_soc_codec_get_drvdata(codec);
3090 /* index to the DAI ID, for now hardcoding */
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07003091 u32 j = 0;
3092 int ret = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303093
3094 codec->control_data = dev_get_drvdata(codec->dev->parent);
3095 sitar = codec->control_data;
3096
3097 /* Execute the callback only if interface type is slimbus */
Bhalchandra Gajareac580a92012-07-10 17:14:33 -07003098 if (sitar_p->intf_type != WCD9XXX_INTERFACE_TYPE_SLIMBUS) {
3099 if (event == SND_SOC_DAPM_POST_PMD && (sitar != NULL))
3100 sitar_codec_pm_runtime_put(sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303101 return 0;
Bhalchandra Gajareac580a92012-07-10 17:14:33 -07003102 }
3103
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303104 switch (event) {
3105 case SND_SOC_DAPM_POST_PMU:
3106 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
3107 if (sitar_dai[j].id == AIF1_PB)
3108 continue;
3109 if (!strncmp(w->sname,
3110 sitar_dai[j].capture.stream_name, 13)) {
3111 ++sitar_p->dai[j].ch_act;
3112 break;
3113 }
3114 }
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003115 if (sitar_p->dai[j].ch_act == sitar_p->dai[j].ch_tot) {
3116 ret = sitar_codec_enable_chmask(sitar_p, event, j);
3117 ret = wcd9xxx_cfg_slim_sch_tx(sitar,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303118 sitar_p->dai[j].ch_num,
3119 sitar_p->dai[j].ch_tot,
3120 sitar_p->dai[j].rate);
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003121 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303122 break;
3123 case SND_SOC_DAPM_POST_PMD:
3124 for (j = 0; j < ARRAY_SIZE(sitar_dai); j++) {
3125 if (sitar_dai[j].id == AIF1_PB)
3126 continue;
3127 if (!strncmp(w->sname,
3128 sitar_dai[j].capture.stream_name, 13)) {
3129 --sitar_p->dai[j].ch_act;
3130 break;
3131 }
3132 }
3133 if (!sitar_p->dai[j].ch_act) {
3134 wcd9xxx_close_slim_sch_tx(sitar,
3135 sitar_p->dai[j].ch_num,
3136 sitar_p->dai[j].ch_tot);
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07003137 ret = sitar_codec_enable_chmask(sitar_p, event, j);
3138 if (ret < 0) {
3139 ret = wcd9xxx_disconnect_port(sitar,
3140 sitar_p->dai[j].ch_num,
3141 sitar_p->dai[j].ch_tot,
3142 0);
3143 pr_info("%s: Disconnect TX port, ret = %d\n",
3144 __func__, ret);
3145 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303146 sitar_p->dai[j].rate = 0;
3147 memset(sitar_p->dai[j].ch_num, 0, (sizeof(u32)*
3148 sitar_p->dai[j].ch_tot));
3149 sitar_p->dai[j].ch_tot = 0;
Bhalchandra Gajareac580a92012-07-10 17:14:33 -07003150 if (sitar != NULL)
3151 sitar_codec_pm_runtime_put(sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303152 }
3153 }
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07003154 return ret;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303155}
3156
3157
3158static short sitar_codec_read_sta_result(struct snd_soc_codec *codec)
3159{
3160 u8 bias_msb, bias_lsb;
3161 short bias_value;
3162
3163 bias_msb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B3_STATUS);
3164 bias_lsb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B2_STATUS);
3165 bias_value = (bias_msb << 8) | bias_lsb;
3166 return bias_value;
3167}
3168
3169static short sitar_codec_read_dce_result(struct snd_soc_codec *codec)
3170{
3171 u8 bias_msb, bias_lsb;
3172 short bias_value;
3173
3174 bias_msb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B5_STATUS);
3175 bias_lsb = snd_soc_read(codec, SITAR_A_CDC_MBHC_B4_STATUS);
3176 bias_value = (bias_msb << 8) | bias_lsb;
3177 return bias_value;
3178}
3179
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003180static void sitar_turn_onoff_rel_detection(struct snd_soc_codec *codec,
3181 bool on)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303182{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003183 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x02, on << 1);
3184}
3185
3186static short __sitar_codec_sta_dce(struct snd_soc_codec *codec, int dce,
3187 bool override_bypass, bool noreldetection)
3188{
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303189 short bias_value;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003190 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3191
Joonwoo Parkf6574c72012-10-10 17:29:57 -07003192 wcd9xxx_disable_irq(codec->control_data, WCD9XXX_IRQ_MBHC_POTENTIAL);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003193 if (noreldetection)
3194 sitar_turn_onoff_rel_detection(codec, false);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303195
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003196 /* Turn on the override */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003197 if (!override_bypass)
3198 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x4, 0x4);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303199 if (dce) {
3200 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
3201 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x4);
3202 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x0);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003203 usleep_range(sitar->mbhc_data.t_sta_dce,
3204 sitar->mbhc_data.t_sta_dce);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303205 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x4);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003206 usleep_range(sitar->mbhc_data.t_dce,
3207 sitar->mbhc_data.t_dce);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303208 bias_value = sitar_codec_read_dce_result(codec);
3209 } else {
3210 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
3211 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x2);
3212 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x0);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003213 usleep_range(sitar->mbhc_data.t_sta_dce,
3214 sitar->mbhc_data.t_sta_dce);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303215 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x2);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003216 usleep_range(sitar->mbhc_data.t_sta,
3217 sitar->mbhc_data.t_sta);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303218 bias_value = sitar_codec_read_sta_result(codec);
3219 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
3220 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x0);
3221 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003222 /* Turn off the override after measuring mic voltage */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003223 if (!override_bypass)
3224 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, 0x00);
3225
3226 if (noreldetection)
3227 sitar_turn_onoff_rel_detection(codec, true);
Joonwoo Parkf6574c72012-10-10 17:29:57 -07003228 wcd9xxx_enable_irq(codec->control_data, WCD9XXX_IRQ_MBHC_POTENTIAL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303229
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303230 return bias_value;
3231}
3232
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003233static short sitar_codec_sta_dce(struct snd_soc_codec *codec, int dce,
3234 bool norel)
3235{
3236 return __sitar_codec_sta_dce(codec, dce, false, norel);
3237}
3238
3239static void sitar_codec_shutdown_hs_removal_detect(struct snd_soc_codec *codec)
3240{
3241 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3242 const struct sitar_mbhc_general_cfg *generic =
3243 SITAR_MBHC_CAL_GENERAL_PTR(sitar->mbhc_cfg.calibration);
3244
3245 if (!sitar->mclk_enabled && !sitar->mbhc_polling_active)
3246 sitar_codec_enable_config_mode(codec, 1);
3247
3248 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x2, 0x2);
3249 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x6, 0x0);
3250
3251 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.mbhc_reg, 0x80, 0x00);
3252
3253 usleep_range(generic->t_shutdown_plug_rem,
3254 generic->t_shutdown_plug_rem);
3255
3256 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0xA, 0x8);
3257 if (!sitar->mclk_enabled && !sitar->mbhc_polling_active)
3258 sitar_codec_enable_config_mode(codec, 0);
3259
3260 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x00);
3261}
3262
3263static void sitar_codec_cleanup_hs_polling(struct snd_soc_codec *codec)
3264{
3265 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3266
3267 sitar_codec_shutdown_hs_removal_detect(codec);
3268
3269 if (!sitar->mclk_enabled) {
3270 sitar_codec_disable_clock_block(codec);
3271 sitar_codec_enable_bandgap(codec, SITAR_BANDGAP_OFF);
3272 }
3273
3274 sitar->mbhc_polling_active = false;
3275 sitar->mbhc_state = MBHC_STATE_NONE;
3276}
3277
3278/* called only from interrupt which is under codec_resource_lock acquisition */
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303279static short sitar_codec_setup_hs_polling(struct snd_soc_codec *codec)
3280{
3281 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303282 short bias_value;
3283 u8 cfilt_mode;
3284
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003285 if (!sitar->mbhc_cfg.calibration) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303286 pr_err("Error, no sitar calibration\n");
3287 return -ENODEV;
3288 }
3289
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303290 if (!sitar->mclk_enabled) {
3291 sitar_codec_enable_bandgap(codec, SITAR_BANDGAP_MBHC_MODE);
3292 sitar_enable_rx_bias(codec, 1);
3293 sitar_codec_enable_clock_block(codec, 1);
3294 }
3295
3296 snd_soc_update_bits(codec, SITAR_A_CLK_BUFF_EN1, 0x05, 0x01);
3297
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303298 /* Make sure CFILT is in fast mode, save current mode */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003299 cfilt_mode = snd_soc_read(codec, sitar->mbhc_bias_regs.cfilt_ctl);
3300 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x70, 0x00);
3301
3302 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg, 0x1F, 0x16);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303303
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303304 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x2, 0x2);
3305 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
3306
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003307 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN, 0x80, 0x80);
3308 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN, 0x1F, 0x1C);
3309 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_TEST_CTL, 0x40, 0x40);
3310
3311 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN, 0x80, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303312 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
3313 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x00);
3314
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003315 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x2, 0x2);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303316 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x8, 0x8);
3317
3318 sitar_codec_calibrate_hs_polling(codec);
3319
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003320 /* don't flip override */
3321 bias_value = __sitar_codec_sta_dce(codec, 1, true, true);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003322 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x40,
3323 cfilt_mode);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303324 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x13, 0x00);
3325
3326 return bias_value;
3327}
3328
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003329static int sitar_cancel_btn_work(struct sitar_priv *sitar)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303330{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003331 int r = 0;
3332 struct wcd9xxx *core = dev_get_drvdata(sitar->codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303333
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003334 if (cancel_delayed_work_sync(&sitar->mbhc_btn_dwork)) {
3335 /* if scheduled mbhc_btn_dwork is canceled from here,
3336 * we have to unlock from here instead btn_work */
3337 wcd9xxx_unlock_sleep(core);
3338 r = 1;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303339 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003340 return r;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303341}
3342
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003343
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003344static u16 sitar_codec_v_sta_dce(struct snd_soc_codec *codec, bool dce,
3345 s16 vin_mv)
3346{
3347 short diff, zero;
3348 struct sitar_priv *sitar;
3349 u32 mb_mv, in;
3350
3351 sitar = snd_soc_codec_get_drvdata(codec);
3352 mb_mv = sitar->mbhc_data.micb_mv;
3353
3354 if (mb_mv == 0) {
3355 pr_err("%s: Mic Bias voltage is set to zero\n", __func__);
3356 return -EINVAL;
3357 }
3358
3359 if (dce) {
3360 diff = sitar->mbhc_data.dce_mb - sitar->mbhc_data.dce_z;
3361 zero = sitar->mbhc_data.dce_z;
3362 } else {
3363 diff = sitar->mbhc_data.sta_mb - sitar->mbhc_data.sta_z;
3364 zero = sitar->mbhc_data.sta_z;
3365 }
3366 in = (u32) diff * vin_mv;
3367
3368 return (u16) (in / mb_mv) + zero;
3369}
3370
3371static s32 sitar_codec_sta_dce_v(struct snd_soc_codec *codec, s8 dce,
3372 u16 bias_value)
3373{
3374 struct sitar_priv *sitar;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003375 s16 value, z, mb;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003376 s32 mv;
3377
3378 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003379 value = bias_value;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003380
3381 if (dce) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003382 z = (sitar->mbhc_data.dce_z);
3383 mb = (sitar->mbhc_data.dce_mb);
3384 mv = (value - z) * (s32)sitar->mbhc_data.micb_mv / (mb - z);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003385 } else {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003386 z = (sitar->mbhc_data.sta_z);
3387 mb = (sitar->mbhc_data.sta_mb);
3388 mv = (value - z) * (s32)sitar->mbhc_data.micb_mv / (mb - z);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003389 }
3390
3391 return mv;
3392}
3393
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003394static void btn_lpress_fn(struct work_struct *work)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303395{
3396 struct delayed_work *delayed_work;
3397 struct sitar_priv *sitar;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003398 short bias_value;
3399 int dce_mv, sta_mv;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003400 struct wcd9xxx *core;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303401
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003402 pr_debug("%s:\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303403
3404 delayed_work = to_delayed_work(work);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003405 sitar = container_of(delayed_work, struct sitar_priv, mbhc_btn_dwork);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003406 core = dev_get_drvdata(sitar->codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303407
3408 if (sitar) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003409 if (sitar->mbhc_cfg.button_jack) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003410 bias_value = sitar_codec_read_sta_result(sitar->codec);
3411 sta_mv = sitar_codec_sta_dce_v(sitar->codec, 0,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003412 bias_value);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003413 bias_value = sitar_codec_read_dce_result(sitar->codec);
3414 dce_mv = sitar_codec_sta_dce_v(sitar->codec, 1,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003415 bias_value);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003416 pr_debug("%s: Reporting long button press event"
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003417 " STA: %d, DCE: %d\n", __func__, sta_mv, dce_mv);
3418 sitar_snd_soc_jack_report(sitar,
3419 sitar->mbhc_cfg.button_jack,
3420 sitar->buttons_pressed,
3421 sitar->buttons_pressed);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303422 }
3423 } else {
3424 pr_err("%s: Bad sitar private data\n", __func__);
3425 }
3426
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003427 pr_debug("%s: leave\n", __func__);
3428 wcd9xxx_unlock_sleep(core);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303429}
3430
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003431
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003432void sitar_mbhc_cal(struct snd_soc_codec *codec)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05303433{
3434 struct sitar_priv *sitar;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003435 struct sitar_mbhc_btn_detect_cfg *btn_det;
3436 u8 cfilt_mode, bg_mode;
3437 u8 ncic, nmeas, navg;
3438 u32 mclk_rate;
3439 u32 dce_wait, sta_wait;
3440 u8 *n_cic;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003441 void *calibration;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003442
3443 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003444 calibration = sitar->mbhc_cfg.calibration;
3445
Joonwoo Parkf6574c72012-10-10 17:29:57 -07003446 wcd9xxx_disable_irq(codec->control_data, WCD9XXX_IRQ_MBHC_POTENTIAL);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003447 sitar_turn_onoff_rel_detection(codec, false);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003448
3449 /* First compute the DCE / STA wait times
3450 * depending on tunable parameters.
3451 * The value is computed in microseconds
3452 */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003453 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003454 n_cic = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_CIC);
3455 ncic = n_cic[sitar_codec_mclk_index(sitar)];
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003456 nmeas = SITAR_MBHC_CAL_BTN_DET_PTR(calibration)->n_meas;
3457 navg = SITAR_MBHC_CAL_GENERAL_PTR(calibration)->mbhc_navg;
3458 mclk_rate = sitar->mbhc_cfg.mclk_rate;
3459 dce_wait = (1000 * 512 * 60 * (nmeas + 1)) / (mclk_rate / 1000);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003460 sta_wait = (1000 * 128 * (navg + 1)) / (mclk_rate / 1000);
3461
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003462 sitar->mbhc_data.t_dce = DEFAULT_DCE_WAIT;
3463 sitar->mbhc_data.t_sta = DEFAULT_STA_WAIT;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003464
3465 /* LDOH and CFILT are already configured during pdata handling.
3466 * Only need to make sure CFILT and bandgap are in Fast mode.
3467 * Need to restore defaults once calculation is done.
3468 */
3469 cfilt_mode = snd_soc_read(codec, sitar->mbhc_bias_regs.cfilt_ctl);
3470 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x40, 0x00);
3471 bg_mode = snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x02,
3472 0x02);
3473
3474 /* Micbias, CFILT, LDOH, MBHC MUX mode settings
3475 * to perform ADC calibration
3476 */
3477 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg, 0x60,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003478 sitar->mbhc_cfg.micbias << 5);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003479 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg, 0x01, 0x00);
3480 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1, 0x60, 0x60);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003481 snd_soc_write(codec, SITAR_A_TX_4_MBHC_TEST_CTL, 0x78);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003482 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, 0x04);
3483
3484 /* DCE measurement for 0 volts */
3485 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x0A);
3486 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x04);
3487 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x02);
3488 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x81);
3489 usleep_range(100, 100);
3490 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x04);
3491 usleep_range(sitar->mbhc_data.t_dce, sitar->mbhc_data.t_dce);
3492 sitar->mbhc_data.dce_z = sitar_codec_read_dce_result(codec);
3493
3494 /* DCE measurment for MB voltage */
3495 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x0A);
3496 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x02);
3497 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x82);
3498 usleep_range(100, 100);
3499 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x04);
3500 usleep_range(sitar->mbhc_data.t_dce, sitar->mbhc_data.t_dce);
3501 sitar->mbhc_data.dce_mb = sitar_codec_read_dce_result(codec);
3502
3503 /* Sta measuremnt for 0 volts */
3504 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x0A);
3505 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x02);
3506 snd_soc_write(codec, SITAR_A_CDC_MBHC_CLK_CTL, 0x02);
3507 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x81);
3508 usleep_range(100, 100);
3509 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x02);
3510 usleep_range(sitar->mbhc_data.t_sta, sitar->mbhc_data.t_sta);
3511 sitar->mbhc_data.sta_z = sitar_codec_read_sta_result(codec);
3512
3513 /* STA Measurement for MB Voltage */
3514 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x82);
3515 usleep_range(100, 100);
3516 snd_soc_write(codec, SITAR_A_CDC_MBHC_EN_CTL, 0x02);
3517 usleep_range(sitar->mbhc_data.t_sta, sitar->mbhc_data.t_sta);
3518 sitar->mbhc_data.sta_mb = sitar_codec_read_sta_result(codec);
3519
3520 /* Restore default settings. */
3521 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, 0x00);
3522 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl, 0x40,
3523 cfilt_mode);
3524 snd_soc_update_bits(codec, SITAR_A_BIAS_CENTRAL_BG_CTL, 0x02, bg_mode);
3525
3526 snd_soc_write(codec, SITAR_A_MBHC_SCALING_MUX_1, 0x84);
3527 usleep_range(100, 100);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003528
Joonwoo Parkf6574c72012-10-10 17:29:57 -07003529 wcd9xxx_enable_irq(codec->control_data, WCD9XXX_IRQ_MBHC_POTENTIAL);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003530 sitar_turn_onoff_rel_detection(codec, true);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003531}
3532
3533void *sitar_mbhc_cal_btn_det_mp(const struct sitar_mbhc_btn_detect_cfg* btn_det,
3534 const enum sitar_mbhc_btn_det_mem mem)
3535{
3536 void *ret = &btn_det->_v_btn_low;
3537
3538 switch (mem) {
3539 case SITAR_BTN_DET_GAIN:
3540 ret += sizeof(btn_det->_n_cic);
3541 case SITAR_BTN_DET_N_CIC:
3542 ret += sizeof(btn_det->_n_ready);
3543 case SITAR_BTN_DET_N_READY:
3544 ret += sizeof(btn_det->_v_btn_high[0]) * btn_det->num_btn;
3545 case SITAR_BTN_DET_V_BTN_HIGH:
3546 ret += sizeof(btn_det->_v_btn_low[0]) * btn_det->num_btn;
3547 case SITAR_BTN_DET_V_BTN_LOW:
3548 /* do nothing */
3549 break;
3550 default:
3551 ret = NULL;
3552 }
3553
3554 return ret;
3555}
3556
3557static void sitar_mbhc_calc_thres(struct snd_soc_codec *codec)
3558{
3559 struct sitar_priv *sitar;
3560 s16 btn_mv = 0, btn_delta_mv;
3561 struct sitar_mbhc_btn_detect_cfg *btn_det;
3562 struct sitar_mbhc_plug_type_cfg *plug_type;
3563 u16 *btn_high;
3564 u8 *n_ready;
3565 int i;
3566
3567 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003568 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(sitar->mbhc_cfg.calibration);
3569 plug_type = SITAR_MBHC_CAL_PLUG_TYPE_PTR(sitar->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003570
3571 n_ready = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_READY);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003572 if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_12288KHZ) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003573 sitar->mbhc_data.npoll = 9;
3574 sitar->mbhc_data.nbounce_wait = 30;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003575 } else if (sitar->mbhc_cfg.mclk_rate == SITAR_MCLK_RATE_9600KHZ) {
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003576 sitar->mbhc_data.npoll = 7;
3577 sitar->mbhc_data.nbounce_wait = 23;
3578 }
3579
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003580 sitar->mbhc_data.t_sta_dce = ((1000 * 256) /
3581 (sitar->mbhc_cfg.mclk_rate / 1000) *
3582 n_ready[sitar_codec_mclk_index(sitar)]) +
3583 10;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003584 sitar->mbhc_data.v_ins_hu =
3585 sitar_codec_v_sta_dce(codec, STA, plug_type->v_hs_max);
3586 sitar->mbhc_data.v_ins_h =
3587 sitar_codec_v_sta_dce(codec, DCE, plug_type->v_hs_max);
3588
3589 btn_high = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_V_BTN_HIGH);
3590 for (i = 0; i < btn_det->num_btn; i++)
3591 btn_mv = btn_high[i] > btn_mv ? btn_high[i] : btn_mv;
3592
3593 sitar->mbhc_data.v_b1_h = sitar_codec_v_sta_dce(codec, DCE, btn_mv);
3594 btn_delta_mv = btn_mv + btn_det->v_btn_press_delta_sta;
3595
3596 sitar->mbhc_data.v_b1_hu =
3597 sitar_codec_v_sta_dce(codec, STA, btn_delta_mv);
3598
3599 btn_delta_mv = btn_mv + btn_det->v_btn_press_delta_cic;
3600
3601 sitar->mbhc_data.v_b1_huc =
3602 sitar_codec_v_sta_dce(codec, DCE, btn_delta_mv);
3603
3604 sitar->mbhc_data.v_brh = sitar->mbhc_data.v_b1_h;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003605 sitar->mbhc_data.v_brl = SITAR_MBHC_BUTTON_MIN;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003606
3607 sitar->mbhc_data.v_no_mic =
3608 sitar_codec_v_sta_dce(codec, STA, plug_type->v_no_mic);
3609}
3610
3611void sitar_mbhc_init(struct snd_soc_codec *codec)
3612{
3613 struct sitar_priv *sitar;
3614 struct sitar_mbhc_general_cfg *generic;
3615 struct sitar_mbhc_btn_detect_cfg *btn_det;
3616 int n;
3617 u8 *n_cic, *gain;
3618
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003619 pr_err("%s(): ENTER\n", __func__);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003620 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003621 generic = SITAR_MBHC_CAL_GENERAL_PTR(sitar->mbhc_cfg.calibration);
3622 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(sitar->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003623
3624 for (n = 0; n < 8; n++) {
3625 if (n != 7) {
3626 snd_soc_update_bits(codec,
3627 SITAR_A_CDC_MBHC_FIR_B1_CFG,
3628 0x07, n);
3629 snd_soc_write(codec, SITAR_A_CDC_MBHC_FIR_B2_CFG,
3630 btn_det->c[n]);
3631 }
3632 }
3633 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B2_CTL, 0x07,
3634 btn_det->nc);
3635
3636 n_cic = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_N_CIC);
3637 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_TIMER_B6_CTL, 0xFF,
3638 n_cic[sitar_codec_mclk_index(sitar)]);
3639
3640 gain = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_GAIN);
3641 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B2_CTL, 0x78,
3642 gain[sitar_codec_mclk_index(sitar)] << 3);
3643
3644 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_TIMER_B4_CTL, 0x70,
3645 generic->mbhc_nsa << 4);
3646
3647 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_TIMER_B4_CTL, 0x0F,
3648 btn_det->n_meas);
3649
3650 snd_soc_write(codec, SITAR_A_CDC_MBHC_TIMER_B5_CTL, generic->mbhc_navg);
3651
3652 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x80, 0x80);
3653
3654 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x78,
3655 btn_det->mbhc_nsc << 3);
3656
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003657 snd_soc_update_bits(codec, SITAR_A_MICB_1_MBHC, 0x03,
3658 sitar->mbhc_cfg.micbias);
3659
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003660 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x02, 0x02);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003661
3662 snd_soc_update_bits(codec, SITAR_A_MBHC_SCALING_MUX_2, 0xF0, 0xF0);
3663
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08003664}
3665
3666static bool sitar_mbhc_fw_validate(const struct firmware *fw)
3667{
3668 u32 cfg_offset;
3669 struct sitar_mbhc_imped_detect_cfg *imped_cfg;
3670 struct sitar_mbhc_btn_detect_cfg *btn_cfg;
3671
3672 if (fw->size < SITAR_MBHC_CAL_MIN_SIZE)
3673 return false;
3674
3675 /* previous check guarantees that there is enough fw data up
3676 * to num_btn
3677 */
3678 btn_cfg = SITAR_MBHC_CAL_BTN_DET_PTR(fw->data);
3679 cfg_offset = (u32) ((void *) btn_cfg - (void *) fw->data);
3680 if (fw->size < (cfg_offset + SITAR_MBHC_CAL_BTN_SZ(btn_cfg)))
3681 return false;
3682
3683 /* previous check guarantees that there is enough fw data up
3684 * to start of impedance detection configuration
3685 */
3686 imped_cfg = SITAR_MBHC_CAL_IMPED_DET_PTR(fw->data);
3687 cfg_offset = (u32) ((void *) imped_cfg - (void *) fw->data);
3688
3689 if (fw->size < (cfg_offset + SITAR_MBHC_CAL_IMPED_MIN_SZ))
3690 return false;
3691
3692 if (fw->size < (cfg_offset + SITAR_MBHC_CAL_IMPED_SZ(imped_cfg)))
3693 return false;
3694
3695 return true;
3696}
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003697
3698
3699static void sitar_turn_onoff_override(struct snd_soc_codec *codec, bool on)
3700{
3701 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_B1_CTL, 0x04, on << 2);
3702}
3703
3704/* called under codec_resource_lock acquisition */
3705void sitar_set_and_turnoff_hph_padac(struct snd_soc_codec *codec)
3706{
3707 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3708 u8 wg_time;
3709
3710 wg_time = snd_soc_read(codec, SITAR_A_RX_HPH_CNP_WG_TIME) ;
3711 wg_time += 1;
3712
3713 /* If headphone PA is on, check if userspace receives
3714 * removal event to sync-up PA's state */
3715 if (sitar_is_hph_pa_on(codec)) {
3716 pr_debug("%s PA is on, setting PA_OFF_ACK\n", __func__);
3717 set_bit(SITAR_HPHL_PA_OFF_ACK, &sitar->hph_pa_dac_state);
3718 set_bit(SITAR_HPHR_PA_OFF_ACK, &sitar->hph_pa_dac_state);
3719 } else {
3720 pr_debug("%s PA is off\n", __func__);
3721 }
3722
3723 if (sitar_is_hph_dac_on(codec, 1))
3724 set_bit(SITAR_HPHL_DAC_OFF_ACK, &sitar->hph_pa_dac_state);
3725 if (sitar_is_hph_dac_on(codec, 0))
3726 set_bit(SITAR_HPHR_DAC_OFF_ACK, &sitar->hph_pa_dac_state);
3727
3728 snd_soc_update_bits(codec, SITAR_A_RX_HPH_CNP_EN, 0x30, 0x00);
3729 snd_soc_update_bits(codec, SITAR_A_RX_HPH_L_DAC_CTL,
3730 0xC0, 0x00);
3731 snd_soc_update_bits(codec, SITAR_A_RX_HPH_R_DAC_CTL,
3732 0xC0, 0x00);
3733 usleep_range(wg_time * 1000, wg_time * 1000);
3734}
3735
3736static void sitar_clr_and_turnon_hph_padac(struct sitar_priv *sitar)
3737{
3738 bool pa_turned_on = false;
3739 struct snd_soc_codec *codec = sitar->codec;
3740 u8 wg_time;
3741
3742 wg_time = snd_soc_read(codec, SITAR_A_RX_HPH_CNP_WG_TIME) ;
3743 wg_time += 1;
3744
3745 if (test_and_clear_bit(SITAR_HPHR_DAC_OFF_ACK,
3746 &sitar->hph_pa_dac_state)) {
3747 pr_debug("%s: HPHR clear flag and enable DAC\n", __func__);
3748 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_R_DAC_CTL,
3749 0xC0, 0xC0);
3750 }
3751 if (test_and_clear_bit(SITAR_HPHL_DAC_OFF_ACK,
3752 &sitar->hph_pa_dac_state)) {
3753 pr_debug("%s: HPHL clear flag and enable DAC\n", __func__);
3754 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_L_DAC_CTL,
3755 0xC0, 0xC0);
3756 }
3757
3758 if (test_and_clear_bit(SITAR_HPHR_PA_OFF_ACK,
3759 &sitar->hph_pa_dac_state)) {
3760 pr_debug("%s: HPHR clear flag and enable PA\n", __func__);
3761 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_CNP_EN, 0x10,
3762 1 << 4);
3763 pa_turned_on = true;
3764 }
3765 if (test_and_clear_bit(SITAR_HPHL_PA_OFF_ACK,
3766 &sitar->hph_pa_dac_state)) {
3767 pr_debug("%s: HPHL clear flag and enable PA\n", __func__);
3768 snd_soc_update_bits(sitar->codec, SITAR_A_RX_HPH_CNP_EN, 0x20,
3769 1 << 5);
3770 pa_turned_on = true;
3771 }
3772
3773 if (pa_turned_on) {
3774 pr_debug("%s: PA was turned off by MBHC and not by DAPM\n",
3775 __func__);
3776 usleep_range(wg_time * 1000, wg_time * 1000);
3777 }
3778}
3779
3780static void sitar_codec_report_plug(struct snd_soc_codec *codec, int insertion,
3781 enum snd_jack_types jack_type)
3782{
3783 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3784
3785 if (!insertion) {
3786 /* Report removal */
3787 sitar->hph_status &= ~jack_type;
3788 if (sitar->mbhc_cfg.headset_jack) {
3789 /* cancel possibly scheduled btn work and
3790 * report release if we reported button press */
3791 if (sitar_cancel_btn_work(sitar)) {
3792 pr_debug("%s: button press is canceled\n",
3793 __func__);
3794 } else if (sitar->buttons_pressed) {
3795 pr_debug("%s: Reporting release for reported "
3796 "button press %d\n", __func__,
3797 jack_type);
3798 sitar_snd_soc_jack_report(sitar,
3799 sitar->mbhc_cfg.button_jack, 0,
3800 sitar->buttons_pressed);
3801 sitar->buttons_pressed &=
3802 ~SITAR_JACK_BUTTON_MASK;
3803 }
3804 pr_debug("%s: Reporting removal %d\n", __func__,
3805 jack_type);
3806 sitar_snd_soc_jack_report(sitar,
3807 sitar->mbhc_cfg.headset_jack,
3808 sitar->hph_status,
3809 SITAR_JACK_MASK);
3810 }
3811 sitar_set_and_turnoff_hph_padac(codec);
3812 hphocp_off_report(sitar, SND_JACK_OC_HPHR,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07003813 WCD9XXX_IRQ_HPH_PA_OCPR_FAULT);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003814 hphocp_off_report(sitar, SND_JACK_OC_HPHL,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07003815 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07003816 sitar->current_plug = PLUG_TYPE_NONE;
3817 sitar->mbhc_polling_active = false;
3818 } else {
3819 /* Report insertion */
3820 sitar->hph_status |= jack_type;
3821
3822 if (jack_type == SND_JACK_HEADPHONE)
3823 sitar->current_plug = PLUG_TYPE_HEADPHONE;
3824 else if (jack_type == SND_JACK_HEADSET) {
3825 sitar->mbhc_polling_active = true;
3826 sitar->current_plug = PLUG_TYPE_HEADSET;
3827 }
3828 if (sitar->mbhc_cfg.headset_jack) {
3829 pr_debug("%s: Reporting insertion %d\n", __func__,
3830 jack_type);
3831 sitar_snd_soc_jack_report(sitar,
3832 sitar->mbhc_cfg.headset_jack,
3833 sitar->hph_status,
3834 SITAR_JACK_MASK);
3835 }
3836 sitar_clr_and_turnon_hph_padac(sitar);
3837 }
3838}
3839
3840
3841static bool sitar_hs_gpio_level_remove(struct sitar_priv *sitar)
3842{
3843 return (gpio_get_value_cansleep(sitar->mbhc_cfg.gpio) !=
3844 sitar->mbhc_cfg.gpio_level_insert);
3845}
3846
3847static bool sitar_is_invalid_insert_delta(struct snd_soc_codec *codec,
3848 int mic_volt, int mic_volt_prev)
3849{
3850 int delta = abs(mic_volt - mic_volt_prev);
3851 if (delta > SITAR_MBHC_FAKE_INSERT_VOLT_DELTA_MV) {
3852 pr_debug("%s: volt delta %dmv\n", __func__, delta);
3853 return true;
3854 }
3855 return false;
3856}
3857
3858static bool sitar_is_invalid_insertion_range(struct snd_soc_codec *codec,
3859 s32 mic_volt)
3860{
3861 bool invalid = false;
3862
3863 if (mic_volt < SITAR_MBHC_FAKE_INSERT_HIGH
3864 && (mic_volt > SITAR_MBHC_FAKE_INSERT_LOW)) {
3865 invalid = true;
3866 }
3867
3868 return invalid;
3869}
3870
3871static bool sitar_codec_is_invalid_plug(struct snd_soc_codec *codec,
3872 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT],
3873 enum sitar_mbhc_plug_type plug_type[MBHC_NUM_DCE_PLUG_DETECT])
3874{
3875 int i;
3876 bool r = false;
3877 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3878 struct sitar_mbhc_plug_type_cfg *plug_type_ptr =
3879 SITAR_MBHC_CAL_PLUG_TYPE_PTR(sitar->mbhc_cfg.calibration);
3880
3881 for (i = 0 ; i < MBHC_NUM_DCE_PLUG_DETECT && !r; i++) {
3882 if (mic_mv[i] < plug_type_ptr->v_no_mic)
3883 plug_type[i] = PLUG_TYPE_HEADPHONE;
3884 else if (mic_mv[i] < plug_type_ptr->v_hs_max)
3885 plug_type[i] = PLUG_TYPE_HEADSET;
3886 else if (mic_mv[i] > plug_type_ptr->v_hs_max)
3887 plug_type[i] = PLUG_TYPE_HIGH_HPH;
3888
3889 r = sitar_is_invalid_insertion_range(codec, mic_mv[i]);
3890 if (!r && i > 0) {
3891 if (plug_type[i-1] != plug_type[i])
3892 r = true;
3893 else
3894 r = sitar_is_invalid_insert_delta(codec,
3895 mic_mv[i],
3896 mic_mv[i - 1]);
3897 }
3898 }
3899
3900 return r;
3901}
3902
3903/* called under codec_resource_lock acquisition */
3904void sitar_find_plug_and_report(struct snd_soc_codec *codec,
3905 enum sitar_mbhc_plug_type plug_type)
3906{
3907 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
3908
3909 if (plug_type == PLUG_TYPE_HEADPHONE
3910 && sitar->current_plug == PLUG_TYPE_NONE) {
3911 /* Nothing was reported previously
3912 * reporte a headphone
3913 */
3914 sitar_codec_report_plug(codec, 1, SND_JACK_HEADPHONE);
3915 sitar_codec_cleanup_hs_polling(codec);
3916 } else if (plug_type == PLUG_TYPE_HEADSET) {
3917 /* If Headphone was reported previously, this will
3918 * only report the mic line
3919 */
3920 sitar_codec_report_plug(codec, 1, SND_JACK_HEADSET);
3921 msleep(100);
3922 sitar_codec_start_hs_polling(codec);
3923 } else if (plug_type == PLUG_TYPE_HIGH_HPH) {
3924 if (sitar->current_plug == PLUG_TYPE_NONE)
3925 sitar_codec_report_plug(codec, 1, SND_JACK_HEADPHONE);
3926 sitar_codec_cleanup_hs_polling(codec);
3927 pr_debug("setup mic trigger for further detection\n");
3928 sitar->lpi_enabled = true;
3929 /* TODO ::: sitar_codec_enable_hs_detect */
3930 pr_err("%s(): High impedence hph not supported\n", __func__);
3931 }
3932}
3933
3934/* should be called under interrupt context that hold suspend */
3935static void sitar_schedule_hs_detect_plug(struct sitar_priv *sitar)
3936{
3937 pr_debug("%s: scheduling sitar_hs_correct_gpio_plug\n", __func__);
3938 sitar->hs_detect_work_stop = false;
3939 wcd9xxx_lock_sleep(sitar->codec->control_data);
3940 schedule_work(&sitar->hs_correct_plug_work);
3941}
3942
3943/* called under codec_resource_lock acquisition */
3944static void sitar_cancel_hs_detect_plug(struct sitar_priv *sitar)
3945{
3946 pr_debug("%s: canceling hs_correct_plug_work\n", __func__);
3947 sitar->hs_detect_work_stop = true;
3948 wmb();
3949 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
3950 if (cancel_work_sync(&sitar->hs_correct_plug_work)) {
3951 pr_debug("%s: hs_correct_plug_work is canceled\n", __func__);
3952 wcd9xxx_unlock_sleep(sitar->codec->control_data);
3953 }
3954 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
3955}
3956
3957static void sitar_hs_correct_gpio_plug(struct work_struct *work)
3958{
3959 struct sitar_priv *sitar;
3960 struct snd_soc_codec *codec;
3961 int retry = 0, i;
3962 bool correction = false;
3963 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT];
3964 short mb_v[MBHC_NUM_DCE_PLUG_DETECT];
3965 enum sitar_mbhc_plug_type plug_type[MBHC_NUM_DCE_PLUG_DETECT];
3966 unsigned long timeout;
3967
3968 sitar = container_of(work, struct sitar_priv, hs_correct_plug_work);
3969 codec = sitar->codec;
3970
3971 pr_debug("%s: enter\n", __func__);
3972 sitar->mbhc_cfg.mclk_cb_fn(codec, 1, false);
3973
3974 /* Keep override on during entire plug type correction work.
3975 *
3976 * This is okay under the assumption that any GPIO irqs which use
3977 * MBHC block cancel and sync this work so override is off again
3978 * prior to GPIO interrupt handler's MBHC block usage.
3979 * Also while this correction work is running, we can guarantee
3980 * DAPM doesn't use any MBHC block as this work only runs with
3981 * headphone detection.
3982 */
3983 sitar_turn_onoff_override(codec, true);
3984
3985 timeout = jiffies + msecs_to_jiffies(SITAR_HS_DETECT_PLUG_TIME_MS);
3986 while (!time_after(jiffies, timeout)) {
3987 ++retry;
3988 rmb();
3989 if (sitar->hs_detect_work_stop) {
3990 pr_debug("%s: stop requested\n", __func__);
3991 break;
3992 }
3993
3994 msleep(SITAR_HS_DETECT_PLUG_INERVAL_MS);
3995 if (sitar_hs_gpio_level_remove(sitar)) {
3996 pr_debug("%s: GPIO value is low\n", __func__);
3997 break;
3998 }
3999
4000 /* can race with removal interrupt */
4001 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
4002 for (i = 0; i < MBHC_NUM_DCE_PLUG_DETECT; i++) {
4003 mb_v[i] = __sitar_codec_sta_dce(codec, 1, true, true);
4004 mic_mv[i] = sitar_codec_sta_dce_v(codec, 1 , mb_v[i]);
4005 pr_debug("%s : DCE run %d, mic_mv = %d(%x)\n",
4006 __func__, retry, mic_mv[i], mb_v[i]);
4007 }
4008 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
4009
4010 if (sitar_codec_is_invalid_plug(codec, mic_mv, plug_type)) {
4011 pr_debug("Invalid plug in attempt # %d\n", retry);
4012 if (retry == NUM_ATTEMPTS_TO_REPORT &&
4013 sitar->current_plug == PLUG_TYPE_NONE) {
4014 sitar_codec_report_plug(codec, 1,
4015 SND_JACK_HEADPHONE);
4016 }
4017 } else if (!sitar_codec_is_invalid_plug(codec, mic_mv,
4018 plug_type) &&
4019 plug_type[0] == PLUG_TYPE_HEADPHONE) {
4020 pr_debug("Good headphone detected, continue polling mic\n");
4021 if (sitar->current_plug == PLUG_TYPE_NONE) {
4022 sitar_codec_report_plug(codec, 1,
4023 SND_JACK_HEADPHONE);
4024 }
4025 } else {
4026 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
4027 /* Turn off override */
4028 sitar_turn_onoff_override(codec, false);
4029 sitar_find_plug_and_report(codec, plug_type[0]);
4030 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
4031 pr_debug("Attempt %d found correct plug %d\n", retry,
4032 plug_type[0]);
4033 correction = true;
4034 break;
4035 }
4036 }
4037
4038 /* Turn off override */
4039 if (!correction)
4040 sitar_turn_onoff_override(codec, false);
4041
4042 sitar->mbhc_cfg.mclk_cb_fn(codec, 0, false);
4043 pr_debug("%s: leave\n", __func__);
4044 /* unlock sleep */
4045 wcd9xxx_unlock_sleep(sitar->codec->control_data);
4046}
4047
4048/* called under codec_resource_lock acquisition */
4049static void sitar_codec_decide_gpio_plug(struct snd_soc_codec *codec)
4050{
4051 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4052 short mb_v[MBHC_NUM_DCE_PLUG_DETECT];
4053 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT];
4054 enum sitar_mbhc_plug_type plug_type[MBHC_NUM_DCE_PLUG_DETECT];
4055 int i;
4056
4057 pr_debug("%s: enter\n", __func__);
4058
4059 sitar_turn_onoff_override(codec, true);
4060 mb_v[0] = sitar_codec_setup_hs_polling(codec);
4061 mic_mv[0] = sitar_codec_sta_dce_v(codec, 1, mb_v[0]);
4062 pr_debug("%s: DCE run 1, mic_mv = %d\n", __func__, mic_mv[0]);
4063
4064 for (i = 1; i < MBHC_NUM_DCE_PLUG_DETECT; i++) {
4065 mb_v[i] = __sitar_codec_sta_dce(codec, 1, true, true);
4066 mic_mv[i] = sitar_codec_sta_dce_v(codec, 1 , mb_v[i]);
4067 pr_debug("%s: DCE run %d, mic_mv = %d\n", __func__, i + 1,
4068 mic_mv[i]);
4069 }
4070 sitar_turn_onoff_override(codec, false);
4071
4072 if (sitar_hs_gpio_level_remove(sitar)) {
4073 pr_debug("%s: GPIO value is low when determining plug\n",
4074 __func__);
4075 return;
4076 }
4077
4078 if (sitar_codec_is_invalid_plug(codec, mic_mv, plug_type)) {
4079 sitar_schedule_hs_detect_plug(sitar);
4080 } else if (plug_type[0] == PLUG_TYPE_HEADPHONE) {
4081 sitar_codec_report_plug(codec, 1, SND_JACK_HEADPHONE);
4082 sitar_schedule_hs_detect_plug(sitar);
4083 } else if (plug_type[0] == PLUG_TYPE_HEADSET) {
4084 pr_debug("%s: Valid plug found, determine plug type\n",
4085 __func__);
4086 sitar_find_plug_and_report(codec, plug_type[0]);
4087 }
4088
4089}
4090
4091/* called under codec_resource_lock acquisition */
4092static void sitar_codec_detect_plug_type(struct snd_soc_codec *codec)
4093{
4094 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4095 const struct sitar_mbhc_plug_detect_cfg *plug_det =
4096 SITAR_MBHC_CAL_PLUG_DET_PTR(sitar->mbhc_cfg.calibration);
4097
4098 if (plug_det->t_ins_complete > 20)
4099 msleep(plug_det->t_ins_complete);
4100 else
4101 usleep_range(plug_det->t_ins_complete * 1000,
4102 plug_det->t_ins_complete * 1000);
4103
4104 if (sitar_hs_gpio_level_remove(sitar))
4105 pr_debug("%s: GPIO value is low when determining "
4106 "plug\n", __func__);
4107 else
4108 sitar_codec_decide_gpio_plug(codec);
4109
4110 return;
4111}
4112
4113static void sitar_hs_gpio_handler(struct snd_soc_codec *codec)
4114{
4115 bool insert;
4116 struct sitar_priv *priv = snd_soc_codec_get_drvdata(codec);
4117 bool is_removed = false;
4118
4119 pr_debug("%s: enter\n", __func__);
4120
4121 priv->in_gpio_handler = true;
4122 /* Wait here for debounce time */
4123 usleep_range(SITAR_GPIO_IRQ_DEBOUNCE_TIME_US,
4124 SITAR_GPIO_IRQ_DEBOUNCE_TIME_US);
4125
4126 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
4127
4128 /* cancel pending button press */
4129 if (sitar_cancel_btn_work(priv))
4130 pr_debug("%s: button press is canceled\n", __func__);
4131
4132 insert = (gpio_get_value_cansleep(priv->mbhc_cfg.gpio) ==
4133 priv->mbhc_cfg.gpio_level_insert);
4134 if ((priv->current_plug == PLUG_TYPE_NONE) && insert) {
4135 priv->lpi_enabled = false;
4136 wmb();
4137
4138 /* cancel detect plug */
4139 sitar_cancel_hs_detect_plug(priv);
4140
4141 /* Disable Mic Bias pull down and HPH Switch to GND */
4142 snd_soc_update_bits(codec, priv->mbhc_bias_regs.ctl_reg, 0x01,
4143 0x00);
4144 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x01, 0x00);
4145 sitar_codec_detect_plug_type(codec);
4146 } else if ((priv->current_plug != PLUG_TYPE_NONE) && !insert) {
4147 priv->lpi_enabled = false;
4148 wmb();
4149
4150 /* cancel detect plug */
4151 sitar_cancel_hs_detect_plug(priv);
4152
4153 if (priv->current_plug == PLUG_TYPE_HEADPHONE) {
4154 sitar_codec_report_plug(codec, 0, SND_JACK_HEADPHONE);
4155 is_removed = true;
4156 } else if (priv->current_plug == PLUG_TYPE_HEADSET) {
4157 sitar_codec_pause_hs_polling(codec);
4158 sitar_codec_cleanup_hs_polling(codec);
4159 sitar_codec_report_plug(codec, 0, SND_JACK_HEADSET);
4160 is_removed = true;
4161 }
4162
4163 if (is_removed) {
4164 /* Enable Mic Bias pull down and HPH Switch to GND */
4165 snd_soc_update_bits(codec,
4166 priv->mbhc_bias_regs.ctl_reg, 0x01,
4167 0x01);
4168 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x01,
4169 0x01);
4170 /* Make sure mic trigger is turned off */
4171 snd_soc_update_bits(codec,
4172 priv->mbhc_bias_regs.ctl_reg,
4173 0x01, 0x01);
4174 snd_soc_update_bits(codec,
4175 priv->mbhc_bias_regs.mbhc_reg,
4176 0x90, 0x00);
4177 /* Reset MBHC State Machine */
4178 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL,
4179 0x08, 0x08);
4180 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_CLK_CTL,
4181 0x08, 0x00);
4182 /* Turn off override */
4183 sitar_turn_onoff_override(codec, false);
4184 }
4185 }
4186
4187 priv->in_gpio_handler = false;
4188 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
4189 pr_debug("%s: leave\n", __func__);
4190}
4191
4192static irqreturn_t sitar_mechanical_plug_detect_irq(int irq, void *data)
4193{
4194 int r = IRQ_HANDLED;
4195 struct snd_soc_codec *codec = data;
4196
4197 if (unlikely(wcd9xxx_lock_sleep(codec->control_data) == false)) {
4198 pr_warn("%s(): Failed to hold suspend\n", __func__);
4199 r = IRQ_NONE;
4200 } else {
4201 sitar_hs_gpio_handler(codec);
4202 wcd9xxx_unlock_sleep(codec->control_data);
4203 }
4204 return r;
4205}
4206
4207static int sitar_mbhc_init_and_calibrate(struct snd_soc_codec *codec)
4208{
4209 int rc = 0;
4210 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4211
4212 sitar->mbhc_cfg.mclk_cb_fn(codec, 1, false);
4213 sitar_mbhc_init(codec);
4214 sitar_mbhc_cal(codec);
4215 sitar_mbhc_calc_thres(codec);
4216 sitar->mbhc_cfg.mclk_cb_fn(codec, 0, false);
4217 sitar_codec_calibrate_hs_polling(codec);
4218
4219 /* Enable Mic Bias pull down and HPH Switch to GND */
4220 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.ctl_reg,
4221 0x01, 0x01);
4222 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH,
4223 0x01, 0x01);
4224
4225 rc = request_threaded_irq(sitar->mbhc_cfg.gpio_irq,
4226 NULL,
4227 sitar_mechanical_plug_detect_irq,
4228 (IRQF_TRIGGER_RISING |
4229 IRQF_TRIGGER_FALLING),
4230 "sitar-hs-gpio", codec);
4231
4232 if (!IS_ERR_VALUE(rc)) {
4233 rc = enable_irq_wake(sitar->mbhc_cfg.gpio_irq);
4234 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL,
4235 0x10, 0x10);
4236 wcd9xxx_enable_irq(codec->control_data,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07004237 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004238 wcd9xxx_enable_irq(codec->control_data,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07004239 WCD9XXX_IRQ_HPH_PA_OCPR_FAULT);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004240 /* Bootup time detection */
4241 sitar_hs_gpio_handler(codec);
4242 }
4243
4244 return rc;
4245}
4246
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004247static void mbhc_fw_read(struct work_struct *work)
4248{
4249 struct delayed_work *dwork;
4250 struct sitar_priv *sitar;
4251 struct snd_soc_codec *codec;
4252 const struct firmware *fw;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004253 int ret = -1, retry = 0;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004254
4255 dwork = to_delayed_work(work);
4256 sitar = container_of(dwork, struct sitar_priv,
4257 mbhc_firmware_dwork);
4258 codec = sitar->codec;
4259
4260 while (retry < MBHC_FW_READ_ATTEMPTS) {
4261 retry++;
4262 pr_info("%s:Attempt %d to request MBHC firmware\n",
4263 __func__, retry);
4264 ret = request_firmware(&fw, "wcd9310/wcd9310_mbhc.bin",
4265 codec->dev);
4266
4267 if (ret != 0) {
4268 usleep_range(MBHC_FW_READ_TIMEOUT,
4269 MBHC_FW_READ_TIMEOUT);
4270 } else {
4271 pr_info("%s: MBHC Firmware read succesful\n", __func__);
4272 break;
4273 }
4274 }
4275
4276 if (ret != 0) {
4277 pr_err("%s: Cannot load MBHC firmware use default cal\n",
4278 __func__);
4279 } else if (sitar_mbhc_fw_validate(fw) == false) {
4280 pr_err("%s: Invalid MBHC cal data size use default cal\n",
4281 __func__);
4282 release_firmware(fw);
4283 } else {
4284 sitar->calibration = (void *)fw->data;
4285 sitar->mbhc_fw = fw;
4286 }
4287
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004288 sitar_mbhc_init_and_calibrate(codec);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004289}
4290
4291int sitar_hs_detect(struct snd_soc_codec *codec,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004292 const struct sitar_mbhc_config *cfg)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004293{
4294 struct sitar_priv *sitar;
4295 int rc = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304296
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004297 if (!codec || !cfg->calibration) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304298 pr_err("Error: no codec or calibration\n");
4299 return -EINVAL;
4300 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004301
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004302 if (cfg->mclk_rate != SITAR_MCLK_RATE_12288KHZ) {
4303 if (cfg->mclk_rate == SITAR_MCLK_RATE_9600KHZ)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004304 pr_err("Error: clock rate %dHz is not yet supported\n",
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004305 cfg->mclk_rate);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004306 else
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004307 pr_err("Error: unsupported clock rate %d\n",
4308 cfg->mclk_rate);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004309 return -EINVAL;
4310 }
4311
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304312 sitar = snd_soc_codec_get_drvdata(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004313 sitar->mbhc_cfg = *cfg;
4314 sitar->in_gpio_handler = false;
4315 sitar->current_plug = PLUG_TYPE_NONE;
4316 sitar->lpi_enabled = false;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304317 sitar_get_mbhc_micbias_regs(codec, &sitar->mbhc_bias_regs);
4318
4319 /* Put CFILT in fast mode by default */
4320 snd_soc_update_bits(codec, sitar->mbhc_bias_regs.cfilt_ctl,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004321 0x40, SITAR_CFILT_FAST_MODE);
4322
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004323 INIT_DELAYED_WORK(&sitar->mbhc_firmware_dwork, mbhc_fw_read);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004324 INIT_DELAYED_WORK(&sitar->mbhc_btn_dwork, btn_lpress_fn);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304325 INIT_WORK(&sitar->hphlocp_work, hphlocp_off_report);
4326 INIT_WORK(&sitar->hphrocp_work, hphrocp_off_report);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004327 INIT_WORK(&sitar->hs_correct_plug_work,
4328 sitar_hs_correct_gpio_plug);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004329
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004330 if (!sitar->mbhc_cfg.read_fw_bin) {
4331 rc = sitar_mbhc_init_and_calibrate(codec);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004332 } else {
4333 schedule_delayed_work(&sitar->mbhc_firmware_dwork,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004334 usecs_to_jiffies(MBHC_FW_READ_TIMEOUT));
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304335 }
4336
4337 return rc;
4338}
4339EXPORT_SYMBOL_GPL(sitar_hs_detect);
4340
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004341static int sitar_determine_button(const struct sitar_priv *priv,
4342 const s32 bias_mv)
4343{
4344 s16 *v_btn_low, *v_btn_high;
4345 struct sitar_mbhc_btn_detect_cfg *btn_det;
4346 int i, btn = -1;
4347
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004348 btn_det = SITAR_MBHC_CAL_BTN_DET_PTR(priv->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004349 v_btn_low = sitar_mbhc_cal_btn_det_mp(btn_det, SITAR_BTN_DET_V_BTN_LOW);
4350 v_btn_high = sitar_mbhc_cal_btn_det_mp(btn_det,
4351 SITAR_BTN_DET_V_BTN_HIGH);
4352 for (i = 0; i < btn_det->num_btn; i++) {
4353 if ((v_btn_low[i] <= bias_mv) && (v_btn_high[i] >= bias_mv)) {
4354 btn = i;
4355 break;
4356 }
4357 }
4358
4359 if (btn == -1)
4360 pr_debug("%s: couldn't find button number for mic mv %d\n",
4361 __func__, bias_mv);
4362
4363 return btn;
4364}
4365
4366static int sitar_get_button_mask(const int btn)
4367{
4368 int mask = 0;
4369 switch (btn) {
4370 case 0:
4371 mask = SND_JACK_BTN_0;
4372 break;
4373 case 1:
4374 mask = SND_JACK_BTN_1;
4375 break;
4376 case 2:
4377 mask = SND_JACK_BTN_2;
4378 break;
4379 case 3:
4380 mask = SND_JACK_BTN_3;
4381 break;
4382 case 4:
4383 mask = SND_JACK_BTN_4;
4384 break;
4385 case 5:
4386 mask = SND_JACK_BTN_5;
4387 break;
4388 case 6:
4389 mask = SND_JACK_BTN_6;
4390 break;
4391 case 7:
4392 mask = SND_JACK_BTN_7;
4393 break;
4394 }
4395 return mask;
4396}
4397
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004398
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304399static irqreturn_t sitar_dce_handler(int irq, void *data)
4400{
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004401 int i, mask;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004402 short dce, sta, bias_value_dce;
4403 s32 mv, stamv, bias_mv_dce;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004404 int btn = -1, meas = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304405 struct sitar_priv *priv = data;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004406 const struct sitar_mbhc_btn_detect_cfg *d =
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004407 SITAR_MBHC_CAL_BTN_DET_PTR(priv->mbhc_cfg.calibration);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004408 short btnmeas[d->n_btn_meas + 1];
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304409 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharya1d069532012-03-07 13:25:24 -08004410 struct wcd9xxx *core = dev_get_drvdata(priv->codec->dev->parent);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004411 int n_btn_meas = d->n_btn_meas;
4412 u8 mbhc_status = snd_soc_read(codec, SITAR_A_CDC_MBHC_B1_STATUS) & 0x3E;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304413
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004414 pr_debug("%s: enter\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304415
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004416 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
4417 if (priv->mbhc_state == MBHC_STATE_POTENTIAL_RECOVERY) {
4418 pr_debug("%s: mbhc is being recovered, skip button press\n",
4419 __func__);
4420 goto done;
4421 }
4422
4423 priv->mbhc_state = MBHC_STATE_POTENTIAL;
4424
4425 if (!priv->mbhc_polling_active) {
4426 pr_warn("%s: mbhc polling is not active, skip button press\n",
4427 __func__);
4428 goto done;
4429 }
4430
4431 dce = sitar_codec_read_dce_result(codec);
4432 mv = sitar_codec_sta_dce_v(codec, 1, dce);
4433
4434 /* If GPIO interrupt already kicked in, ignore button press */
4435 if (priv->in_gpio_handler) {
4436 pr_debug("%s: GPIO State Changed, ignore button press\n",
4437 __func__);
4438 btn = -1;
4439 goto done;
4440 }
4441
4442 if (mbhc_status != SITAR_MBHC_STATUS_REL_DETECTION) {
4443 if (priv->mbhc_last_resume &&
4444 !time_after(jiffies, priv->mbhc_last_resume + HZ)) {
4445 pr_debug("%s: Button is already released shortly after "
4446 "resume\n", __func__);
4447 n_btn_meas = 0;
4448 } else {
4449 pr_debug("%s: Button is already released without "
4450 "resume", __func__);
4451 sta = sitar_codec_read_sta_result(codec);
4452 stamv = sitar_codec_sta_dce_v(codec, 0, sta);
4453 btn = sitar_determine_button(priv, mv);
4454 if (btn != sitar_determine_button(priv, stamv))
4455 btn = -1;
4456 goto done;
4457 }
4458 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304459
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004460 /* determine pressed button */
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004461 btnmeas[meas++] = sitar_determine_button(priv, mv);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004462 pr_debug("%s: meas %d - DCE %d,%d, button %d\n", __func__,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004463 meas - 1, dce, mv, btnmeas[meas - 1]);
4464 if (n_btn_meas == 0)
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004465 btn = btnmeas[0];
4466 for (; ((d->n_btn_meas) && (meas < (d->n_btn_meas + 1))); meas++) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004467 bias_value_dce = sitar_codec_sta_dce(codec, 1, false);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004468 bias_mv_dce = sitar_codec_sta_dce_v(codec, 1, bias_value_dce);
4469 btnmeas[meas] = sitar_determine_button(priv, bias_mv_dce);
4470 pr_debug("%s: meas %d - DCE %d,%d, button %d\n",
4471 __func__, meas, bias_value_dce, bias_mv_dce,
4472 btnmeas[meas]);
4473 /* if large enough measurements are collected,
4474 * start to check if last all n_btn_con measurements were
4475 * in same button low/high range */
4476 if (meas + 1 >= d->n_btn_con) {
4477 for (i = 0; i < d->n_btn_con; i++)
4478 if ((btnmeas[meas] < 0) ||
4479 (btnmeas[meas] != btnmeas[meas - i]))
4480 break;
4481 if (i == d->n_btn_con) {
4482 /* button pressed */
4483 btn = btnmeas[meas];
4484 break;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004485 } else if ((n_btn_meas - meas) < (d->n_btn_con - 1)) {
4486 /* if left measurements are less than n_btn_con,
4487 * it's impossible to find button number */
4488 break;
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004489 }
4490 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004491 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304492
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004493 if (btn >= 0) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004494 if (priv->in_gpio_handler) {
4495 pr_debug("%s: GPIO already triggered, ignore button "
4496 "press\n", __func__);
4497 goto done;
4498 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004499 mask = sitar_get_button_mask(btn);
4500 priv->buttons_pressed |= mask;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004501 wcd9xxx_lock_sleep(core);
4502 if (schedule_delayed_work(&priv->mbhc_btn_dwork,
4503 msecs_to_jiffies(400)) == 0) {
4504 WARN(1, "Button pressed twice without release"
4505 "event\n");
4506 wcd9xxx_unlock_sleep(core);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004507 }
4508 } else {
4509 pr_debug("%s: bogus button press, too short press?\n",
4510 __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304511 }
4512
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004513 done:
4514 pr_debug("%s: leave\n", __func__);
4515 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304516 return IRQ_HANDLED;
4517}
4518
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004519static int sitar_is_fake_press(struct sitar_priv *priv)
4520{
4521 int i;
4522 int r = 0;
4523 struct snd_soc_codec *codec = priv->codec;
4524 const int dces = MBHC_NUM_DCE_PLUG_DETECT;
4525 short mb_v;
4526
4527 for (i = 0; i < dces; i++) {
4528 usleep_range(10000, 10000);
4529 if (i == 0) {
4530 mb_v = sitar_codec_sta_dce(codec, 0, true);
4531 pr_debug("%s: STA[0]: %d,%d\n", __func__, mb_v,
4532 sitar_codec_sta_dce_v(codec, 0, mb_v));
4533 if (mb_v < (short)priv->mbhc_data.v_b1_hu ||
4534 mb_v > (short)priv->mbhc_data.v_ins_hu) {
4535 r = 1;
4536 break;
4537 }
4538 } else {
4539 mb_v = sitar_codec_sta_dce(codec, 1, true);
4540 pr_debug("%s: DCE[%d]: %d,%d\n", __func__, i, mb_v,
4541 sitar_codec_sta_dce_v(codec, 1, mb_v));
4542 if (mb_v < (short)priv->mbhc_data.v_b1_h ||
4543 mb_v > (short)priv->mbhc_data.v_ins_h) {
4544 r = 1;
4545 break;
4546 }
4547 }
4548 }
4549
4550 return r;
4551}
4552
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304553static irqreturn_t sitar_release_handler(int irq, void *data)
4554{
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004555 int ret;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304556 struct sitar_priv *priv = data;
4557 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304558
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004559 pr_debug("%s: enter\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304560
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004561 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
4562 priv->mbhc_state = MBHC_STATE_RELEASE;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304563
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004564 if (priv->buttons_pressed & SITAR_JACK_BUTTON_MASK) {
4565 ret = sitar_cancel_btn_work(priv);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304566 if (ret == 0) {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004567 pr_debug("%s: Reporting long button release event\n",
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004568 __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004569 if (priv->mbhc_cfg.button_jack)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304570 sitar_snd_soc_jack_report(priv,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004571 priv->mbhc_cfg.button_jack, 0,
4572 priv->buttons_pressed);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304573 } else {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004574 if (sitar_is_fake_press(priv)) {
4575 pr_debug("%s: Fake button press interrupt\n",
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004576 __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004577 } else if (priv->mbhc_cfg.button_jack) {
4578 if (priv->in_gpio_handler) {
4579 pr_debug("%s: GPIO kicked in, ignore\n",
4580 __func__);
4581 } else {
4582 pr_debug("%s: Reporting short button 0 "
4583 "press and release\n",
4584 __func__);
4585 sitar_snd_soc_jack_report(priv,
4586 priv->mbhc_cfg.button_jack,
4587 priv->buttons_pressed,
4588 priv->buttons_pressed);
4589 sitar_snd_soc_jack_report(priv,
4590 priv->mbhc_cfg.button_jack, 0,
4591 priv->buttons_pressed);
4592 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304593 }
4594 }
4595
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004596 priv->buttons_pressed &= ~SITAR_JACK_BUTTON_MASK;
4597 }
4598
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004599 sitar_codec_calibrate_hs_polling(codec);
4600
4601 if (priv->mbhc_cfg.gpio)
4602 msleep(SITAR_MBHC_GPIO_REL_DEBOUNCE_TIME_MS);
4603
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304604 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004605
4606 pr_debug("%s: leave\n", __func__);
4607 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
4608
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304609 return IRQ_HANDLED;
4610}
4611
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304612static irqreturn_t sitar_hphl_ocp_irq(int irq, void *data)
4613{
4614 struct sitar_priv *sitar = data;
4615 struct snd_soc_codec *codec;
4616
4617 pr_info("%s: received HPHL OCP irq\n", __func__);
4618
4619 if (sitar) {
4620 codec = sitar->codec;
4621 if (sitar->hphlocp_cnt++ < SITAR_OCP_ATTEMPT) {
4622 pr_info("%s: retry\n", __func__);
4623 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004624 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304625 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004626 0x10);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304627 } else {
4628 wcd9xxx_disable_irq(codec->control_data,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07004629 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304630 sitar->hphlocp_cnt = 0;
4631 sitar->hph_status |= SND_JACK_OC_HPHL;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004632 if (sitar->mbhc_cfg.headset_jack)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304633 sitar_snd_soc_jack_report(sitar,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004634 sitar->mbhc_cfg.headset_jack,
4635 sitar->hph_status,
4636 SITAR_JACK_MASK);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304637 }
4638 } else {
4639 pr_err("%s: Bad sitar private data\n", __func__);
4640 }
4641
4642 return IRQ_HANDLED;
4643}
4644
4645static irqreturn_t sitar_hphr_ocp_irq(int irq, void *data)
4646{
4647 struct sitar_priv *sitar = data;
4648 struct snd_soc_codec *codec;
4649
4650 pr_info("%s: received HPHR OCP irq\n", __func__);
4651
4652 if (sitar) {
4653 codec = sitar->codec;
4654 if (sitar->hphrocp_cnt++ < SITAR_OCP_ATTEMPT) {
4655 pr_info("%s: retry\n", __func__);
4656 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
4657 0x00);
4658 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL, 0x10,
4659 0x10);
4660 } else {
4661 wcd9xxx_disable_irq(codec->control_data,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07004662 WCD9XXX_IRQ_HPH_PA_OCPR_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304663 sitar->hphrocp_cnt = 0;
4664 sitar->hph_status |= SND_JACK_OC_HPHR;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004665 if (sitar->mbhc_cfg.headset_jack)
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304666 sitar_snd_soc_jack_report(sitar,
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004667 sitar->mbhc_cfg.headset_jack,
4668 sitar->hph_status,
4669 SITAR_JACK_MASK);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304670 }
4671 } else {
4672 pr_err("%s: Bad sitar private data\n", __func__);
4673 }
4674
4675 return IRQ_HANDLED;
4676}
4677
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304678static irqreturn_t sitar_hs_insert_irq(int irq, void *data)
4679{
4680 struct sitar_priv *priv = data;
4681 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304682
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004683 pr_debug("%s: enter\n", __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004684 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -07004685 wcd9xxx_disable_irq(codec->control_data, WCD9XXX_IRQ_MBHC_INSERTION);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304686
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304687 snd_soc_update_bits(codec, SITAR_A_CDC_MBHC_INT_CTL, 0x03, 0x00);
4688
4689 /* Turn off both HPH and MIC line schmitt triggers */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004690 snd_soc_update_bits(codec, priv->mbhc_bias_regs.mbhc_reg, 0x90, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304691 snd_soc_update_bits(codec, SITAR_A_MBHC_HPH, 0x13, 0x00);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004692 snd_soc_update_bits(codec, priv->mbhc_bias_regs.ctl_reg, 0x01, 0x00);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304693
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004694 pr_debug("%s: MIC trigger insertion interrupt\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304695
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004696 rmb();
4697 if (priv->lpi_enabled)
4698 msleep(100);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304699
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004700 rmb();
4701 if (!priv->lpi_enabled) {
4702 pr_debug("%s: lpi is disabled\n", __func__);
4703 } else if (gpio_get_value_cansleep(priv->mbhc_cfg.gpio) ==
4704 priv->mbhc_cfg.gpio_level_insert) {
4705 pr_debug("%s: Valid insertion, "
4706 "detect plug type\n", __func__);
4707 sitar_codec_decide_gpio_plug(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304708 } else {
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004709 pr_debug("%s: Invalid insertion, "
4710 "stop plug detection\n", __func__);
4711 }
4712 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
4713 return IRQ_HANDLED;
4714}
4715
4716static bool is_valid_mic_voltage(struct snd_soc_codec *codec, s32 mic_mv)
4717{
4718 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4719 struct sitar_mbhc_plug_type_cfg *plug_type =
4720 SITAR_MBHC_CAL_PLUG_TYPE_PTR(sitar->mbhc_cfg.calibration);
4721
4722 return (!(mic_mv > SITAR_MBHC_FAKE_INSERT_LOW
4723 && mic_mv < SITAR_MBHC_FAKE_INSERT_HIGH)
4724 && (mic_mv > plug_type->v_no_mic)
4725 && (mic_mv < plug_type->v_hs_max)) ? true : false;
4726}
4727
4728/* called under codec_resource_lock acquisition
4729 * returns true if mic voltage range is back to normal insertion
4730 * returns false either if timedout or removed */
4731static bool sitar_hs_remove_settle(struct snd_soc_codec *codec)
4732{
4733 int i;
4734 bool timedout, settled = false;
4735 s32 mic_mv[MBHC_NUM_DCE_PLUG_DETECT];
4736 short mb_v[MBHC_NUM_DCE_PLUG_DETECT];
4737 unsigned long retry = 0, timeout;
4738 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
4739
4740 timeout = jiffies + msecs_to_jiffies(SITAR_HS_DETECT_PLUG_TIME_MS);
4741 while (!(timedout = time_after(jiffies, timeout))) {
4742 retry++;
4743 if (sitar_hs_gpio_level_remove(sitar)) {
4744 pr_debug("%s: GPIO indicates removal\n", __func__);
4745 break;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304746 }
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004747
4748 if (retry > 1)
4749 msleep(250);
4750 else
4751 msleep(50);
4752
4753 if (sitar_hs_gpio_level_remove(sitar)) {
4754 pr_debug("%s: GPIO indicates removal\n", __func__);
4755 break;
4756 }
4757
4758 sitar_turn_onoff_override(codec, true);
4759 for (i = 0; i < MBHC_NUM_DCE_PLUG_DETECT; i++) {
4760 mb_v[i] = __sitar_codec_sta_dce(codec, 1, true, true);
4761 mic_mv[i] = sitar_codec_sta_dce_v(codec, 1 , mb_v[i]);
4762 pr_debug("%s : DCE run %lu, mic_mv = %d(%x)\n",
4763 __func__, retry, mic_mv[i], mb_v[i]);
4764 }
4765 sitar_turn_onoff_override(codec, false);
4766
4767 if (sitar_hs_gpio_level_remove(sitar)) {
4768 pr_debug("%s: GPIO indicates removal\n", __func__);
4769 break;
4770 }
4771
4772 for (i = 0; i < MBHC_NUM_DCE_PLUG_DETECT; i++)
4773 if (!is_valid_mic_voltage(codec, mic_mv[i]))
4774 break;
4775
4776 if (i == MBHC_NUM_DCE_PLUG_DETECT) {
4777 pr_debug("%s: MIC voltage settled\n", __func__);
4778 settled = true;
4779 msleep(200);
4780 break;
4781 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304782 }
4783
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004784 if (timedout)
4785 pr_debug("%s: Microphone did not settle in %d seconds\n",
4786 __func__, SITAR_HS_DETECT_PLUG_TIME_MS);
4787 return settled;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304788}
4789
4790static irqreturn_t sitar_hs_remove_irq(int irq, void *data)
4791{
4792 struct sitar_priv *priv = data;
4793 struct snd_soc_codec *codec = priv->codec;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304794
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004795 pr_debug("%s: enter, removal interrupt\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304796
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004797 SITAR_ACQUIRE_LOCK(priv->codec_resource_lock);
4798 if (sitar_hs_remove_settle(codec))
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304799 sitar_codec_start_hs_polling(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004800 pr_debug("%s: remove settle done\n", __func__);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304801
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07004802 SITAR_RELEASE_LOCK(priv->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304803 return IRQ_HANDLED;
4804}
4805
4806
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304807
4808static irqreturn_t sitar_slimbus_irq(int irq, void *data)
4809{
4810 struct sitar_priv *priv = data;
4811 struct snd_soc_codec *codec = priv->codec;
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07004812 unsigned long slimbus_value;
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07004813 int i, j, k, port_id, ch_mask_temp;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304814 u8 val;
4815
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304816
4817 for (i = 0; i < WCD9XXX_SLIM_NUM_PORT_REG; i++) {
4818 slimbus_value = wcd9xxx_interface_reg_read(codec->control_data,
4819 SITAR_SLIM_PGD_PORT_INT_STATUS0 + i);
4820 for_each_set_bit(j, &slimbus_value, BITS_PER_BYTE) {
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07004821 port_id = i*8 + j;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304822 val = wcd9xxx_interface_reg_read(codec->control_data,
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07004823 SITAR_SLIM_PGD_PORT_INT_SOURCE0 + port_id);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304824 if (val & 0x1)
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07004825 pr_err_ratelimited("overflow error on port %x, value %x\n",
4826 port_id, val);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304827 if (val & 0x2)
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07004828 pr_err_ratelimited("underflow error on port %x,value %x\n",
4829 port_id, val);
4830 if (val & 0x4) {
4831 pr_debug("%s: port %x disconnect value %x\n",
4832 __func__, port_id, val);
4833 for (k = 0; k < ARRAY_SIZE(sitar_dai); k++) {
4834 ch_mask_temp = 1 << port_id;
4835 if (ch_mask_temp &
4836 priv->dai[k].ch_mask) {
4837 priv->dai[k].ch_mask &=
4838 ~ch_mask_temp;
4839 if (!priv->dai[k].ch_mask)
4840 wake_up(
4841 &priv->dai[k].dai_wait);
4842 }
4843 }
4844 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304845 }
4846 wcd9xxx_interface_reg_write(codec->control_data,
Bhalchandra Gajarec131e9f2012-09-09 15:32:14 -07004847 SITAR_SLIM_PGD_PORT_INT_CLR0 + i, slimbus_value);
4848 val = 0x0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304849 }
4850
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304851 return IRQ_HANDLED;
4852}
4853
4854
4855static int sitar_handle_pdata(struct sitar_priv *sitar)
4856{
4857 struct snd_soc_codec *codec = sitar->codec;
4858 struct wcd9xxx_pdata *pdata = sitar->pdata;
4859 int k1, k2, rc = 0;
4860 u8 leg_mode = pdata->amic_settings.legacy_mode;
4861 u8 txfe_bypass = pdata->amic_settings.txfe_enable;
4862 u8 txfe_buff = pdata->amic_settings.txfe_buff;
4863 u8 flag = pdata->amic_settings.use_pdata;
4864 u8 i = 0, j = 0;
4865 u8 val_txfe = 0, value = 0;
Asish Bhattacharya34aa1982012-09-20 14:24:05 +05304866 int amic_reg_count = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304867
4868 if (!pdata) {
4869 rc = -ENODEV;
4870 goto done;
4871 }
4872
4873 /* Make sure settings are correct */
4874 if ((pdata->micbias.ldoh_v > SITAR_LDOH_2P85_V) ||
4875 (pdata->micbias.bias1_cfilt_sel > SITAR_CFILT2_SEL) ||
4876 (pdata->micbias.bias2_cfilt_sel > SITAR_CFILT2_SEL)) {
4877 rc = -EINVAL;
4878 goto done;
4879 }
4880
4881 /* figure out k value */
4882 k1 = sitar_find_k_value(pdata->micbias.ldoh_v,
4883 pdata->micbias.cfilt1_mv);
4884 k2 = sitar_find_k_value(pdata->micbias.ldoh_v,
4885 pdata->micbias.cfilt2_mv);
4886
4887 if (IS_ERR_VALUE(k1) || IS_ERR_VALUE(k2)) {
4888 rc = -EINVAL;
4889 goto done;
4890 }
4891
4892 /* Set voltage level and always use LDO */
Bhalchandra Gajare03d28d02012-05-18 17:39:32 -07004893 snd_soc_update_bits(codec, SITAR_A_LDO_H_MODE_1, 0x0C,
4894 (pdata->micbias.ldoh_v << 2));
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304895
4896 snd_soc_update_bits(codec, SITAR_A_MICB_CFILT_1_VAL, 0xFC,
4897 (k1 << 2));
4898 snd_soc_update_bits(codec, SITAR_A_MICB_CFILT_2_VAL, 0xFC,
4899 (k2 << 2));
4900
4901 snd_soc_update_bits(codec, SITAR_A_MICB_1_CTL, 0x60,
4902 (pdata->micbias.bias1_cfilt_sel << 5));
4903 snd_soc_update_bits(codec, SITAR_A_MICB_2_CTL, 0x60,
4904 (pdata->micbias.bias2_cfilt_sel << 5));
4905
Bhalchandra Gajare15dbeaa2012-06-26 12:53:07 -07004906 /* Set micbias capless mode */
4907 snd_soc_update_bits(codec, SITAR_A_MICB_1_CTL, 0x10,
4908 (pdata->micbias.bias1_cap_mode << 4));
4909 snd_soc_update_bits(codec, SITAR_A_MICB_2_CTL, 0x10,
4910 (pdata->micbias.bias2_cap_mode << 4));
4911
Asish Bhattacharya34aa1982012-09-20 14:24:05 +05304912 amic_reg_count = (NUM_AMIC % 2) ? NUM_AMIC + 1 : NUM_AMIC;
4913 for (i = 0; i < amic_reg_count; j++, i += 2) {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304914 if (flag & (0x01 << i)) {
4915 value = (leg_mode & (0x01 << i)) ? 0x10 : 0x00;
4916 val_txfe = (txfe_bypass & (0x01 << i)) ? 0x20 : 0x00;
4917 val_txfe = val_txfe |
4918 ((txfe_buff & (0x01 << i)) ? 0x10 : 0x00);
4919 snd_soc_update_bits(codec, SITAR_A_TX_1_2_EN + j * 10,
4920 0x10, value);
4921 snd_soc_update_bits(codec,
4922 SITAR_A_TX_1_2_TEST_EN + j * 10,
4923 0x30, val_txfe);
4924 }
4925 if (flag & (0x01 << (i + 1))) {
4926 value = (leg_mode & (0x01 << (i + 1))) ? 0x01 : 0x00;
4927 val_txfe = (txfe_bypass &
4928 (0x01 << (i + 1))) ? 0x02 : 0x00;
4929 val_txfe |= (txfe_buff &
4930 (0x01 << (i + 1))) ? 0x01 : 0x00;
4931 snd_soc_update_bits(codec, SITAR_A_TX_1_2_EN + j * 10,
4932 0x01, value);
4933 snd_soc_update_bits(codec,
4934 SITAR_A_TX_1_2_TEST_EN + j * 10,
4935 0x03, val_txfe);
4936 }
4937 }
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08004938 if (flag & 0x40) {
4939 value = (leg_mode & 0x40) ? 0x10 : 0x00;
4940 value = value | ((txfe_bypass & 0x40) ? 0x02 : 0x00);
4941 value = value | ((txfe_buff & 0x40) ? 0x01 : 0x00);
4942 snd_soc_update_bits(codec, SITAR_A_TX_4_MBHC_EN,
4943 0x13, value);
4944 }
4945
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304946
4947 if (pdata->ocp.use_pdata) {
4948 /* not defined in CODEC specification */
4949 if (pdata->ocp.hph_ocp_limit == 1 ||
4950 pdata->ocp.hph_ocp_limit == 5) {
4951 rc = -EINVAL;
4952 goto done;
4953 }
4954 snd_soc_update_bits(codec, SITAR_A_RX_COM_OCP_CTL,
4955 0x0F, pdata->ocp.num_attempts);
4956 snd_soc_write(codec, SITAR_A_RX_COM_OCP_COUNT,
4957 ((pdata->ocp.run_time << 4) | pdata->ocp.wait_time));
4958 snd_soc_update_bits(codec, SITAR_A_RX_HPH_OCP_CTL,
4959 0xE0, (pdata->ocp.hph_ocp_limit << 5));
4960 }
4961done:
4962 return rc;
4963}
4964
4965static const struct sitar_reg_mask_val sitar_1_1_reg_defaults[] = {
4966
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304967 SITAR_REG_VAL(SITAR_A_MICB_1_INT_RBIAS, 0x24),
4968 SITAR_REG_VAL(SITAR_A_MICB_2_INT_RBIAS, 0x24),
4969
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304970 SITAR_REG_VAL(SITAR_A_RX_HPH_BIAS_PA, 0x57),
4971 SITAR_REG_VAL(SITAR_A_RX_HPH_BIAS_LDO, 0x56),
4972
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304973 SITAR_REG_VAL(SITAR_A_RX_EAR_BIAS_PA, 0xA6),
4974 SITAR_REG_VAL(SITAR_A_RX_EAR_GAIN, 0x02),
4975 SITAR_REG_VAL(SITAR_A_RX_EAR_VCM, 0x03),
4976
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304977 SITAR_REG_VAL(SITAR_A_CDC_RX1_B5_CTL, 0x78),
Asish Bhattacharya2e1d7522012-05-25 15:08:40 +05304978 SITAR_REG_VAL(SITAR_A_CDC_RX2_B5_CTL, 0x78),
4979 SITAR_REG_VAL(SITAR_A_CDC_RX3_B5_CTL, 0x78),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304980
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304981 SITAR_REG_VAL(SITAR_A_CDC_RX1_B6_CTL, 0x80),
4982
4983 SITAR_REG_VAL(SITAR_A_CDC_CLSG_FREQ_THRESH_B3_CTL, 0x1B),
Bhalchandra Gajare8a0bb372012-10-03 18:41:01 -07004984 SITAR_REG_VAL(SITAR_A_CDC_CLSG_FREQ_THRESH_B4_CTL, 0x5B),
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05304985
4986};
4987
4988static void sitar_update_reg_defaults(struct snd_soc_codec *codec)
4989{
4990 u32 i;
4991 for (i = 0; i < ARRAY_SIZE(sitar_1_1_reg_defaults); i++)
4992 snd_soc_write(codec, sitar_1_1_reg_defaults[i].reg,
4993 sitar_1_1_reg_defaults[i].val);
4994
4995}
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05304996
4997static const struct sitar_reg_mask_val sitar_i2c_codec_reg_init_val[] = {
4998 {WCD9XXX_A_CHIP_CTL, 0x1, 0x1},
4999};
5000
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305001static const struct sitar_reg_mask_val sitar_codec_reg_init_val[] = {
5002 /* Initialize current threshold to 350MA
5003 * number of wait and run cycles to 4096
5004 */
Bhalchandra Gajare33f74302012-06-14 15:12:51 -07005005 {SITAR_A_RX_HPH_OCP_CTL, 0xE0, 0x60},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305006 {SITAR_A_RX_COM_OCP_COUNT, 0xFF, 0xFF},
5007
5008 {SITAR_A_QFUSE_CTL, 0xFF, 0x03},
5009
5010 /* Initialize gain registers to use register gain */
5011 {SITAR_A_RX_HPH_L_GAIN, 0x10, 0x10},
5012 {SITAR_A_RX_HPH_R_GAIN, 0x10, 0x10},
5013 {SITAR_A_RX_LINE_1_GAIN, 0x10, 0x10},
5014 {SITAR_A_RX_LINE_2_GAIN, 0x10, 0x10},
5015
Kuirong Wangccb29c62012-06-15 11:09:07 -07005016 /* Set the MICBIAS default output as pull down*/
5017 {SITAR_A_MICB_1_CTL, 0x01, 0x01},
5018 {SITAR_A_MICB_2_CTL, 0x01, 0x01},
5019
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305020 /* Initialize mic biases to differential mode */
5021 {SITAR_A_MICB_1_INT_RBIAS, 0x24, 0x24},
5022 {SITAR_A_MICB_2_INT_RBIAS, 0x24, 0x24},
5023
5024 {SITAR_A_CDC_CONN_CLSG_CTL, 0x3C, 0x14},
5025
5026 /* Use 16 bit sample size for TX1 to TX6 */
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08005027 {SITAR_A_CDC_CONN_TX_SB_B1_CTL, 0x30, 0x20},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305028 {SITAR_A_CDC_CONN_TX_SB_B2_CTL, 0x30, 0x20},
5029 {SITAR_A_CDC_CONN_TX_SB_B3_CTL, 0x30, 0x20},
5030 {SITAR_A_CDC_CONN_TX_SB_B4_CTL, 0x30, 0x20},
5031 {SITAR_A_CDC_CONN_TX_SB_B5_CTL, 0x30, 0x20},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08005032 {SITAR_A_CDC_CLK_TX_CLK_EN_B1_CTL, 0x1, 0x1},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305033
5034 /* Use 16 bit sample size for RX */
5035 {SITAR_A_CDC_CONN_RX_SB_B1_CTL, 0xFF, 0xAA},
5036 {SITAR_A_CDC_CONN_RX_SB_B2_CTL, 0x02, 0x02},
5037
5038 /*enable HPF filter for TX paths */
5039 {SITAR_A_CDC_TX1_MUX_CTL, 0x8, 0x0},
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08005040 {SITAR_A_CDC_TX2_MUX_CTL, 0x8, 0x0},
Bhalchandra Gajare2776af12012-04-27 16:59:39 -07005041
5042 /*enable External clock select*/
5043 {SITAR_A_CDC_CLK_MCLK_CTL, 0x01, 0x01},
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305044};
5045
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05305046static void sitar_i2c_codec_init_reg(struct snd_soc_codec *codec)
5047{
5048 u32 i;
5049 for (i = 0; i < ARRAY_SIZE(sitar_i2c_codec_reg_init_val); i++)
5050 snd_soc_update_bits(codec, sitar_i2c_codec_reg_init_val[i].reg,
5051 sitar_i2c_codec_reg_init_val[i].mask,
5052 sitar_i2c_codec_reg_init_val[i].val);
5053}
5054
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305055static void sitar_codec_init_reg(struct snd_soc_codec *codec)
5056{
5057 u32 i;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305058 for (i = 0; i < ARRAY_SIZE(sitar_codec_reg_init_val); i++)
5059 snd_soc_update_bits(codec, sitar_codec_reg_init_val[i].reg,
5060 sitar_codec_reg_init_val[i].mask,
5061 sitar_codec_reg_init_val[i].val);
5062}
5063
5064static int sitar_codec_probe(struct snd_soc_codec *codec)
5065{
5066 struct sitar *control;
5067 struct sitar_priv *sitar;
5068 struct snd_soc_dapm_context *dapm = &codec->dapm;
5069 int ret = 0;
5070 int i;
5071 u8 sitar_version;
5072 int ch_cnt;
5073
5074 codec->control_data = dev_get_drvdata(codec->dev->parent);
5075 control = codec->control_data;
5076
5077 sitar = kzalloc(sizeof(struct sitar_priv), GFP_KERNEL);
5078 if (!sitar) {
5079 dev_err(codec->dev, "Failed to allocate private data\n");
5080 return -ENOMEM;
5081 }
5082
5083 /* Make sure mbhc micbias register addresses are zeroed out */
5084 memset(&sitar->mbhc_bias_regs, 0,
5085 sizeof(struct mbhc_micbias_regs));
5086 sitar->cfilt_k_value = 0;
5087 sitar->mbhc_micbias_switched = false;
5088
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08005089 /* Make sure mbhc intenal calibration data is zeroed out */
5090 memset(&sitar->mbhc_data, 0,
5091 sizeof(struct mbhc_internal_cal_data));
5092 sitar->mbhc_data.t_sta_dce = DEFAULT_DCE_STA_WAIT;
5093 sitar->mbhc_data.t_dce = DEFAULT_DCE_WAIT;
5094 sitar->mbhc_data.t_sta = DEFAULT_STA_WAIT;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305095 snd_soc_codec_set_drvdata(codec, sitar);
5096
5097 sitar->mclk_enabled = false;
5098 sitar->bandgap_type = SITAR_BANDGAP_OFF;
5099 sitar->clock_active = false;
5100 sitar->config_mode_active = false;
5101 sitar->mbhc_polling_active = false;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305102 sitar->no_mic_headset_override = false;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005103 mutex_init(&sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305104 sitar->codec = codec;
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005105 sitar->mbhc_state = MBHC_STATE_NONE;
5106 sitar->mbhc_last_resume = 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305107 sitar->pdata = dev_get_platdata(codec->dev->parent);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305108 sitar_update_reg_defaults(codec);
5109 sitar_codec_init_reg(codec);
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05305110 sitar->intf_type = wcd9xxx_get_intf_type();
5111 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C)
5112 sitar_i2c_codec_init_reg(codec);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305113
5114 ret = sitar_handle_pdata(sitar);
5115 if (IS_ERR_VALUE(ret)) {
5116 pr_err("%s: bad pdata\n", __func__);
5117 goto err_pdata;
5118 }
5119
Steve Mucklef132c6c2012-06-06 18:30:57 -07005120 snd_soc_add_codec_controls(codec, sitar_snd_controls,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305121 ARRAY_SIZE(sitar_snd_controls));
5122 snd_soc_dapm_new_controls(dapm, sitar_dapm_widgets,
5123 ARRAY_SIZE(sitar_dapm_widgets));
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05305124 if (sitar->intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
5125 snd_soc_dapm_new_controls(dapm, sitar_dapm_i2s_widgets,
5126 ARRAY_SIZE(sitar_dapm_i2s_widgets));
5127 snd_soc_dapm_add_routes(dapm, audio_i2s_map,
5128 ARRAY_SIZE(audio_i2s_map));
5129 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305130 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
5131
5132 sitar_version = snd_soc_read(codec, WCD9XXX_A_CHIP_VERSION);
5133 pr_info("%s : Sitar version reg 0x%2x\n", __func__, (u32)sitar_version);
5134
5135 sitar_version &= 0x1F;
5136 pr_info("%s : Sitar version %u\n", __func__, (u32)sitar_version);
5137
5138 snd_soc_dapm_sync(dapm);
5139
5140
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005141 ret = wcd9xxx_request_irq(codec->control_data,
5142 WCD9XXX_IRQ_MBHC_INSERTION,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305143 sitar_hs_insert_irq, "Headset insert detect", sitar);
5144 if (ret) {
5145 pr_err("%s: Failed to request irq %d\n", __func__,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005146 WCD9XXX_IRQ_MBHC_INSERTION);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305147 goto err_insert_irq;
5148 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005149 wcd9xxx_disable_irq(codec->control_data, WCD9XXX_IRQ_MBHC_INSERTION);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305150
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005151 ret = wcd9xxx_request_irq(codec->control_data,
5152 WCD9XXX_IRQ_MBHC_REMOVAL,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305153 sitar_hs_remove_irq, "Headset remove detect", sitar);
5154 if (ret) {
5155 pr_err("%s: Failed to request irq %d\n", __func__,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005156 WCD9XXX_IRQ_MBHC_REMOVAL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305157 goto err_remove_irq;
5158 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305159
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005160 ret = wcd9xxx_request_irq(codec->control_data,
5161 WCD9XXX_IRQ_MBHC_POTENTIAL,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305162 sitar_dce_handler, "DC Estimation detect", sitar);
5163 if (ret) {
5164 pr_err("%s: Failed to request irq %d\n", __func__,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005165 WCD9XXX_IRQ_MBHC_POTENTIAL);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305166 goto err_potential_irq;
5167 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305168
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005169 ret = wcd9xxx_request_irq(codec->control_data,
5170 WCD9XXX_IRQ_MBHC_RELEASE,
5171 sitar_release_handler,
5172 "Button Release detect", sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305173 if (ret) {
5174 pr_err("%s: Failed to request irq %d\n", __func__,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005175 WCD9XXX_IRQ_MBHC_RELEASE);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305176 goto err_release_irq;
5177 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305178
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005179 ret = wcd9xxx_request_irq(codec->control_data, WCD9XXX_IRQ_SLIMBUS,
5180 sitar_slimbus_irq, "SLIMBUS Slave", sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305181 if (ret) {
5182 pr_err("%s: Failed to request irq %d\n", __func__,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005183 WCD9XXX_IRQ_SLIMBUS);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305184 goto err_slimbus_irq;
5185 }
5186
5187 for (i = 0; i < WCD9XXX_SLIM_NUM_PORT_REG; i++)
5188 wcd9xxx_interface_reg_write(codec->control_data,
5189 SITAR_SLIM_PGD_PORT_INT_EN0 + i, 0xFF);
5190
5191
5192 ret = wcd9xxx_request_irq(codec->control_data,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005193 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT,
5194 sitar_hphl_ocp_irq,
5195 "HPH_L OCP detect", sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305196 if (ret) {
5197 pr_err("%s: Failed to request irq %d\n", __func__,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005198 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305199 goto err_hphl_ocp_irq;
5200 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005201 wcd9xxx_disable_irq(codec->control_data,
5202 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305203
5204 ret = wcd9xxx_request_irq(codec->control_data,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005205 WCD9XXX_IRQ_HPH_PA_OCPR_FAULT,
5206 sitar_hphr_ocp_irq, "HPH_R OCP detect",
5207 sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305208 if (ret) {
5209 pr_err("%s: Failed to request irq %d\n", __func__,
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005210 WCD9XXX_IRQ_HPH_PA_OCPR_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305211 goto err_hphr_ocp_irq;
5212 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005213 wcd9xxx_disable_irq(codec->control_data, WCD9XXX_IRQ_HPH_PA_OCPR_FAULT);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305214
5215 for (i = 0; i < ARRAY_SIZE(sitar_dai); i++) {
5216 switch (sitar_dai[i].id) {
5217 case AIF1_PB:
5218 ch_cnt = sitar_dai[i].playback.channels_max;
5219 break;
5220 case AIF1_CAP:
5221 ch_cnt = sitar_dai[i].capture.channels_max;
5222 break;
5223 default:
5224 continue;
5225 }
5226 sitar->dai[i].ch_num = kzalloc((sizeof(unsigned int)*
5227 ch_cnt), GFP_KERNEL);
Bhalchandra Gajaree66e4bb2012-07-10 15:17:50 -07005228 init_waitqueue_head(&sitar->dai[i].dai_wait);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305229 }
5230
Steve Mucklef132c6c2012-06-06 18:30:57 -07005231 codec->ignore_pmdown_time = 1;
5232
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305233#ifdef CONFIG_DEBUG_FS
5234 debug_sitar_priv = sitar;
5235#endif
5236
5237 return ret;
5238
5239err_hphr_ocp_irq:
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005240 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_HPH_PA_OCPL_FAULT,
5241 sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305242err_hphl_ocp_irq:
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005243 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_SLIMBUS, sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305244err_slimbus_irq:
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005245 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_RELEASE, sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305246err_release_irq:
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005247 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_POTENTIAL,
5248 sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305249err_potential_irq:
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005250 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_REMOVAL, sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305251err_remove_irq:
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005252 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_INSERTION,
5253 sitar);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305254err_insert_irq:
5255err_pdata:
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005256 mutex_destroy(&sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305257 kfree(sitar);
5258 return ret;
5259}
5260static int sitar_codec_remove(struct snd_soc_codec *codec)
5261{
5262 int i;
5263 struct sitar_priv *sitar = snd_soc_codec_get_drvdata(codec);
Joonwoo Parkf6574c72012-10-10 17:29:57 -07005264 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_SLIMBUS, sitar);
5265 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_RELEASE, sitar);
5266 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_POTENTIAL,
5267 sitar);
5268 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_REMOVAL, sitar);
5269 wcd9xxx_free_irq(codec->control_data, WCD9XXX_IRQ_MBHC_INSERTION,
5270 sitar);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005271 SITAR_ACQUIRE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305272 sitar_codec_disable_clock_block(codec);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005273 SITAR_RELEASE_LOCK(sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305274 sitar_codec_enable_bandgap(codec, SITAR_BANDGAP_OFF);
Asish Bhattacharyaf868a1f2012-03-05 10:07:44 -08005275 if (sitar->mbhc_fw)
5276 release_firmware(sitar->mbhc_fw);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305277 for (i = 0; i < ARRAY_SIZE(sitar_dai); i++)
5278 kfree(sitar->dai[i].ch_num);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005279 mutex_destroy(&sitar->codec_resource_lock);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305280 kfree(sitar);
5281 return 0;
5282}
5283static struct snd_soc_codec_driver soc_codec_dev_sitar = {
5284 .probe = sitar_codec_probe,
5285 .remove = sitar_codec_remove,
5286 .read = sitar_read,
5287 .write = sitar_write,
5288
5289 .readable_register = sitar_readable,
5290 .volatile_register = sitar_volatile,
5291
5292 .reg_cache_size = SITAR_CACHE_SIZE,
5293 .reg_cache_default = sitar_reg_defaults,
5294 .reg_word_size = 1,
5295};
5296
5297#ifdef CONFIG_DEBUG_FS
5298static struct dentry *debugfs_poke;
5299
5300static int codec_debug_open(struct inode *inode, struct file *file)
5301{
5302 file->private_data = inode->i_private;
5303 return 0;
5304}
5305
5306static ssize_t codec_debug_write(struct file *filp,
5307 const char __user *ubuf, size_t cnt, loff_t *ppos)
5308{
5309 char lbuf[32];
5310 char *buf;
5311 int rc;
5312
5313 if (cnt > sizeof(lbuf) - 1)
5314 return -EINVAL;
5315
5316 rc = copy_from_user(lbuf, ubuf, cnt);
5317 if (rc)
5318 return -EFAULT;
5319
5320 lbuf[cnt] = '\0';
5321 buf = (char *)lbuf;
5322 debug_sitar_priv->no_mic_headset_override = (*strsep(&buf, " ") == '0')
5323 ? false : true;
5324
5325 return rc;
5326}
5327
5328static const struct file_operations codec_debug_ops = {
5329 .open = codec_debug_open,
5330 .write = codec_debug_write,
5331};
5332#endif
5333
5334#ifdef CONFIG_PM
5335static int sitar_suspend(struct device *dev)
5336{
Asish Bhattacharya1d069532012-03-07 13:25:24 -08005337 dev_dbg(dev, "%s: system suspend\n", __func__);
5338 return 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305339}
5340
5341static int sitar_resume(struct device *dev)
5342{
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005343 struct platform_device *pdev = to_platform_device(dev);
5344 struct sitar_priv *sitar = platform_get_drvdata(pdev);
Asish Bhattacharya1d069532012-03-07 13:25:24 -08005345 dev_dbg(dev, "%s: system resume\n", __func__);
Bhalchandra Gajare466aafe2012-05-02 17:55:26 -07005346 sitar->mbhc_last_resume = jiffies;
Asish Bhattacharya1d069532012-03-07 13:25:24 -08005347 return 0;
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305348}
5349
5350static const struct dev_pm_ops sitar_pm_ops = {
5351 .suspend = sitar_suspend,
5352 .resume = sitar_resume,
5353};
5354#endif
5355
5356static int __devinit sitar_probe(struct platform_device *pdev)
5357{
5358 int ret = 0;
5359 pr_err("%s\n", __func__);
5360#ifdef CONFIG_DEBUG_FS
5361 debugfs_poke = debugfs_create_file("TRRS",
5362 S_IFREG | S_IRUGO, NULL, (void *) "TRRS", &codec_debug_ops);
5363
5364#endif
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05305365 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_SLIMBUS)
5366 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_sitar,
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305367 sitar_dai, ARRAY_SIZE(sitar_dai));
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05305368 else if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
5369 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_sitar,
5370 sitar_i2s_dai, ARRAY_SIZE(sitar_i2s_dai));
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05305371 return ret;
5372}
5373static int __devexit sitar_remove(struct platform_device *pdev)
5374{
5375 snd_soc_unregister_codec(&pdev->dev);
5376
5377#ifdef CONFIG_DEBUG_FS
5378 debugfs_remove(debugfs_poke);
5379#endif
5380 return 0;
5381}
5382static struct platform_driver sitar_codec_driver = {
5383 .probe = sitar_probe,
5384 .remove = sitar_remove,
5385 .driver = {
5386 .name = "sitar_codec",
5387 .owner = THIS_MODULE,
5388#ifdef CONFIG_PM
5389 .pm = &sitar_pm_ops,
5390#endif
5391 },
5392};
5393
5394static int __init sitar_codec_init(void)
5395{
5396 return platform_driver_register(&sitar_codec_driver);
5397}
5398
5399static void __exit sitar_codec_exit(void)
5400{
5401 platform_driver_unregister(&sitar_codec_driver);
5402}
5403
5404module_init(sitar_codec_init);
5405module_exit(sitar_codec_exit);
5406
5407MODULE_DESCRIPTION("Sitar codec driver");
5408MODULE_VERSION("1.0");
5409MODULE_LICENSE("GPL v2");