1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * extcon-arizona.c - Extcon driver Wolfson Arizona devices
4 *
5 * Copyright (C) 2012-2014 Wolfson Microelectronics plc
6 */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/interrupt.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/gpio.h>
15 #include <linux/input.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/property.h>
18 #include <linux/regulator/consumer.h>
19
20 #include <sound/jack.h>
21 #include <sound/soc.h>
22
23 #include <linux/mfd/arizona/core.h>
24 #include <linux/mfd/arizona/pdata.h>
25 #include <linux/mfd/arizona/registers.h>
26 #include <dt-bindings/mfd/arizona.h>
27
28 #include "arizona.h"
29
30 #define ARIZONA_MAX_MICD_RANGE 8
31
32 /*
33 * The hardware supports 8 ranges / buttons, but the snd-jack interface
34 * only supports 6 buttons (button 0-5).
35 */
36 #define ARIZONA_MAX_MICD_BUTTONS 6
37
38 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
39 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
40 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
41 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
42
43 #define ARIZONA_TST_CAP_DEFAULT 0x3
44 #define ARIZONA_TST_CAP_CLAMP 0x1
45
46 #define ARIZONA_HPDET_MAX 10000
47
48 #define HPDET_DEBOUNCE 500
49 #define DEFAULT_MICD_TIMEOUT 2000
50
51 #define ARIZONA_HPDET_WAIT_COUNT 15
52 #define ARIZONA_HPDET_WAIT_DELAY_MS 20
53
54 #define QUICK_HEADPHONE_MAX_OHM 3
55 #define MICROPHONE_MIN_OHM 1257
56 #define MICROPHONE_MAX_OHM 30000
57
58 #define MICD_DBTIME_TWO_READINGS 2
59 #define MICD_DBTIME_FOUR_READINGS 4
60
61 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
62 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
63 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
64 ARIZONA_MICD_LVL_7)
65
66 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
67
68 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
69
70 static const struct arizona_micd_config micd_default_modes[] = {
71 { ARIZONA_ACCDET_SRC, 1, 0 },
72 { 0, 2, 1 },
73 };
74
75 static const struct arizona_micd_range micd_default_ranges[] = {
76 { .max = 11, .key = BTN_0 },
77 { .max = 28, .key = BTN_1 },
78 { .max = 54, .key = BTN_2 },
79 { .max = 100, .key = BTN_3 },
80 { .max = 186, .key = BTN_4 },
81 { .max = 430, .key = BTN_5 },
82 };
83
84 /* The number of levels in arizona_micd_levels valid for button thresholds */
85 #define ARIZONA_NUM_MICD_BUTTON_LEVELS 64
86
87 static const int arizona_micd_levels[] = {
88 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
89 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
90 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
91 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
92 1257, 30000,
93 };
94
95 static void arizona_start_hpdet_acc_id(struct arizona_priv *info);
96
arizona_extcon_hp_clamp(struct arizona_priv * info,bool clamp)97 static void arizona_extcon_hp_clamp(struct arizona_priv *info,
98 bool clamp)
99 {
100 struct arizona *arizona = info->arizona;
101 unsigned int mask = 0, val = 0;
102 unsigned int cap_sel = 0;
103 int ret;
104
105 switch (arizona->type) {
106 case WM8998:
107 case WM1814:
108 mask = 0;
109 break;
110 case WM5110:
111 case WM8280:
112 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
113 ARIZONA_HP1L_SHRTI;
114 if (clamp) {
115 val = ARIZONA_HP1L_SHRTO;
116 cap_sel = ARIZONA_TST_CAP_CLAMP;
117 } else {
118 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
119 cap_sel = ARIZONA_TST_CAP_DEFAULT;
120 }
121
122 ret = regmap_update_bits(arizona->regmap,
123 ARIZONA_HP_TEST_CTRL_1,
124 ARIZONA_HP1_TST_CAP_SEL_MASK,
125 cap_sel);
126 if (ret)
127 dev_warn(arizona->dev, "Failed to set TST_CAP_SEL: %d\n", ret);
128 break;
129 default:
130 mask = ARIZONA_RMV_SHRT_HP1L;
131 if (clamp)
132 val = ARIZONA_RMV_SHRT_HP1L;
133 break;
134 }
135
136 snd_soc_dapm_mutex_lock(arizona->dapm);
137
138 arizona->hpdet_clamp = clamp;
139
140 /* Keep the HP output stages disabled while doing the clamp */
141 if (clamp) {
142 ret = regmap_update_bits(arizona->regmap,
143 ARIZONA_OUTPUT_ENABLES_1,
144 ARIZONA_OUT1L_ENA |
145 ARIZONA_OUT1R_ENA, 0);
146 if (ret)
147 dev_warn(arizona->dev, "Failed to disable headphone outputs: %d\n", ret);
148 }
149
150 if (mask) {
151 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
152 mask, val);
153 if (ret)
154 dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
155
156 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
157 mask, val);
158 if (ret)
159 dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
160 }
161
162 /* Restore the desired state while not doing the clamp */
163 if (!clamp) {
164 ret = regmap_update_bits(arizona->regmap,
165 ARIZONA_OUTPUT_ENABLES_1,
166 ARIZONA_OUT1L_ENA |
167 ARIZONA_OUT1R_ENA, arizona->hp_ena);
168 if (ret)
169 dev_warn(arizona->dev, "Failed to restore headphone outputs: %d\n", ret);
170 }
171
172 snd_soc_dapm_mutex_unlock(arizona->dapm);
173 }
174
arizona_extcon_set_mode(struct arizona_priv * info,int mode)175 static void arizona_extcon_set_mode(struct arizona_priv *info, int mode)
176 {
177 struct arizona *arizona = info->arizona;
178
179 mode %= info->micd_num_modes;
180
181 gpiod_set_value_cansleep(info->micd_pol_gpio,
182 info->micd_modes[mode].gpio);
183
184 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
185 ARIZONA_MICD_BIAS_SRC_MASK,
186 info->micd_modes[mode].bias <<
187 ARIZONA_MICD_BIAS_SRC_SHIFT);
188 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
189 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
190
191 info->micd_mode = mode;
192
193 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
194 }
195
arizona_extcon_get_micbias(struct arizona_priv * info)196 static const char *arizona_extcon_get_micbias(struct arizona_priv *info)
197 {
198 switch (info->micd_modes[0].bias) {
199 case 1:
200 return "MICBIAS1";
201 case 2:
202 return "MICBIAS2";
203 case 3:
204 return "MICBIAS3";
205 default:
206 return "MICVDD";
207 }
208 }
209
arizona_extcon_pulse_micbias(struct arizona_priv * info)210 static void arizona_extcon_pulse_micbias(struct arizona_priv *info)
211 {
212 struct arizona *arizona = info->arizona;
213 const char *widget = arizona_extcon_get_micbias(info);
214 struct snd_soc_dapm_context *dapm = arizona->dapm;
215 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
216 int ret;
217
218 ret = snd_soc_component_force_enable_pin(component, widget);
219 if (ret)
220 dev_warn(arizona->dev, "Failed to enable %s: %d\n", widget, ret);
221
222 snd_soc_dapm_sync(dapm);
223
224 if (!arizona->pdata.micd_force_micbias) {
225 ret = snd_soc_component_disable_pin(component, widget);
226 if (ret)
227 dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
228
229 snd_soc_dapm_sync(dapm);
230 }
231 }
232
arizona_start_mic(struct arizona_priv * info)233 static void arizona_start_mic(struct arizona_priv *info)
234 {
235 struct arizona *arizona = info->arizona;
236 bool change;
237 int ret;
238 unsigned int mode;
239
240 /* Microphone detection can't use idle mode */
241 pm_runtime_get_sync(arizona->dev);
242
243 if (info->detecting) {
244 ret = regulator_allow_bypass(info->micvdd, false);
245 if (ret)
246 dev_err(arizona->dev, "Failed to regulate MICVDD: %d\n", ret);
247 }
248
249 ret = regulator_enable(info->micvdd);
250 if (ret)
251 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n", ret);
252
253 if (info->micd_reva) {
254 const struct reg_sequence reva[] = {
255 { 0x80, 0x3 },
256 { 0x294, 0x0 },
257 { 0x80, 0x0 },
258 };
259
260 regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
261 }
262
263 if (info->detecting && arizona->pdata.micd_software_compare)
264 mode = ARIZONA_ACCDET_MODE_ADC;
265 else
266 mode = ARIZONA_ACCDET_MODE_MIC;
267
268 regmap_update_bits(arizona->regmap,
269 ARIZONA_ACCESSORY_DETECT_MODE_1,
270 ARIZONA_ACCDET_MODE_MASK, mode);
271
272 arizona_extcon_pulse_micbias(info);
273
274 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
275 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
276 &change);
277 if (ret < 0) {
278 dev_err(arizona->dev, "Failed to enable micd: %d\n", ret);
279 } else if (!change) {
280 regulator_disable(info->micvdd);
281 pm_runtime_put_autosuspend(arizona->dev);
282 }
283 }
284
arizona_stop_mic(struct arizona_priv * info)285 static void arizona_stop_mic(struct arizona_priv *info)
286 {
287 struct arizona *arizona = info->arizona;
288 const char *widget = arizona_extcon_get_micbias(info);
289 struct snd_soc_dapm_context *dapm = arizona->dapm;
290 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
291 bool change = false;
292 int ret;
293
294 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
295 ARIZONA_MICD_ENA, 0,
296 &change);
297 if (ret < 0)
298 dev_err(arizona->dev, "Failed to disable micd: %d\n", ret);
299
300 ret = snd_soc_component_disable_pin(component, widget);
301 if (ret)
302 dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
303
304 snd_soc_dapm_sync(dapm);
305
306 if (info->micd_reva) {
307 const struct reg_sequence reva[] = {
308 { 0x80, 0x3 },
309 { 0x294, 0x2 },
310 { 0x80, 0x0 },
311 };
312
313 regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
314 }
315
316 ret = regulator_allow_bypass(info->micvdd, true);
317 if (ret)
318 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
319
320 if (change) {
321 regulator_disable(info->micvdd);
322 pm_runtime_put_autosuspend(arizona->dev);
323 }
324 }
325
326 static struct {
327 unsigned int threshold;
328 unsigned int factor_a;
329 unsigned int factor_b;
330 } arizona_hpdet_b_ranges[] = {
331 { 100, 5528, 362464 },
332 { 169, 11084, 6186851 },
333 { 169, 11065, 65460395 },
334 };
335
336 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
337
338 static struct {
339 int min;
340 int max;
341 } arizona_hpdet_c_ranges[] = {
342 { 0, 30 },
343 { 8, 100 },
344 { 100, 1000 },
345 { 1000, 10000 },
346 };
347
arizona_hpdet_read(struct arizona_priv * info)348 static int arizona_hpdet_read(struct arizona_priv *info)
349 {
350 struct arizona *arizona = info->arizona;
351 unsigned int val, range;
352 int ret;
353
354 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
355 if (ret) {
356 dev_err(arizona->dev, "Failed to read HPDET status: %d\n", ret);
357 return ret;
358 }
359
360 switch (info->hpdet_ip_version) {
361 case 0:
362 if (!(val & ARIZONA_HP_DONE)) {
363 dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
364 return -EAGAIN;
365 }
366
367 val &= ARIZONA_HP_LVL_MASK;
368 break;
369
370 case 1:
371 if (!(val & ARIZONA_HP_DONE_B)) {
372 dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
373 return -EAGAIN;
374 }
375
376 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
377 if (ret) {
378 dev_err(arizona->dev, "Failed to read HP value: %d\n", ret);
379 return -EAGAIN;
380 }
381
382 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
383 &range);
384 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
385 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
386
387 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
388 (val < arizona_hpdet_b_ranges[range].threshold ||
389 val >= ARIZONA_HPDET_B_RANGE_MAX)) {
390 range++;
391 dev_dbg(arizona->dev, "Moving to HPDET range %d\n", range);
392 regmap_update_bits(arizona->regmap,
393 ARIZONA_HEADPHONE_DETECT_1,
394 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
395 range <<
396 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
397 return -EAGAIN;
398 }
399
400 /* If we go out of range report top of range */
401 if (val < arizona_hpdet_b_ranges[range].threshold ||
402 val >= ARIZONA_HPDET_B_RANGE_MAX) {
403 dev_dbg(arizona->dev, "Measurement out of range\n");
404 return ARIZONA_HPDET_MAX;
405 }
406
407 dev_dbg(arizona->dev, "HPDET read %d in range %d\n", val, range);
408
409 val = arizona_hpdet_b_ranges[range].factor_b
410 / ((val * 100) -
411 arizona_hpdet_b_ranges[range].factor_a);
412 break;
413
414 case 2:
415 if (!(val & ARIZONA_HP_DONE_B)) {
416 dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
417 return -EAGAIN;
418 }
419
420 val &= ARIZONA_HP_LVL_B_MASK;
421 /* Convert to ohms, the value is in 0.5 ohm increments */
422 val /= 2;
423
424 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
425 &range);
426 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
427 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
428
429 /* Skip up a range, or report? */
430 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
431 (val >= arizona_hpdet_c_ranges[range].max)) {
432 range++;
433 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
434 arizona_hpdet_c_ranges[range].min,
435 arizona_hpdet_c_ranges[range].max);
436 regmap_update_bits(arizona->regmap,
437 ARIZONA_HEADPHONE_DETECT_1,
438 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
439 range <<
440 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
441 return -EAGAIN;
442 }
443
444 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
445 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
446 arizona_hpdet_c_ranges[range].min);
447 val = arizona_hpdet_c_ranges[range].min;
448 }
449 break;
450
451 default:
452 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n", info->hpdet_ip_version);
453 return -EINVAL;
454 }
455
456 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
457 return val;
458 }
459
arizona_hpdet_do_id(struct arizona_priv * info,int * reading,bool * mic)460 static int arizona_hpdet_do_id(struct arizona_priv *info, int *reading,
461 bool *mic)
462 {
463 struct arizona *arizona = info->arizona;
464 int id_gpio = arizona->pdata.hpdet_id_gpio;
465
466 if (!arizona->pdata.hpdet_acc_id)
467 return 0;
468
469 /*
470 * If we're using HPDET for accessory identification we need
471 * to take multiple measurements, step through them in sequence.
472 */
473 info->hpdet_res[info->num_hpdet_res++] = *reading;
474
475 /* Only check the mic directly if we didn't already ID it */
476 if (id_gpio && info->num_hpdet_res == 1) {
477 dev_dbg(arizona->dev, "Measuring mic\n");
478
479 regmap_update_bits(arizona->regmap,
480 ARIZONA_ACCESSORY_DETECT_MODE_1,
481 ARIZONA_ACCDET_MODE_MASK |
482 ARIZONA_ACCDET_SRC,
483 ARIZONA_ACCDET_MODE_HPR |
484 info->micd_modes[0].src);
485
486 gpio_set_value_cansleep(id_gpio, 1);
487
488 regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
489 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
490 return -EAGAIN;
491 }
492
493 /* OK, got both. Now, compare... */
494 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
495 info->hpdet_res[0], info->hpdet_res[1]);
496
497 /* Take the headphone impedance for the main report */
498 *reading = info->hpdet_res[0];
499
500 /* Sometimes we get false readings due to slow insert */
501 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
502 dev_dbg(arizona->dev, "Retrying high impedance\n");
503 info->num_hpdet_res = 0;
504 info->hpdet_retried = true;
505 arizona_start_hpdet_acc_id(info);
506 pm_runtime_put(arizona->dev);
507 return -EAGAIN;
508 }
509
510 /*
511 * If we measure the mic as high impedance
512 */
513 if (!id_gpio || info->hpdet_res[1] > 50) {
514 dev_dbg(arizona->dev, "Detected mic\n");
515 *mic = true;
516 info->detecting = true;
517 } else {
518 dev_dbg(arizona->dev, "Detected headphone\n");
519 }
520
521 /* Make sure everything is reset back to the real polarity */
522 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
523 ARIZONA_ACCDET_SRC, info->micd_modes[0].src);
524
525 return 0;
526 }
527
arizona_hpdet_irq(int irq,void * data)528 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
529 {
530 struct arizona_priv *info = data;
531 struct arizona *arizona = info->arizona;
532 int id_gpio = arizona->pdata.hpdet_id_gpio;
533 int ret, reading, state, report;
534 bool mic = false;
535
536 mutex_lock(&info->lock);
537
538 /* If we got a spurious IRQ for some reason then ignore it */
539 if (!info->hpdet_active) {
540 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
541 mutex_unlock(&info->lock);
542 return IRQ_NONE;
543 }
544
545 /* If the cable was removed while measuring ignore the result */
546 state = info->jack->status & SND_JACK_MECHANICAL;
547 if (!state) {
548 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
549 goto done;
550 }
551
552 ret = arizona_hpdet_read(info);
553 if (ret == -EAGAIN)
554 goto out;
555 else if (ret < 0)
556 goto done;
557 reading = ret;
558
559 /* Reset back to starting range */
560 regmap_update_bits(arizona->regmap,
561 ARIZONA_HEADPHONE_DETECT_1,
562 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
563 0);
564
565 ret = arizona_hpdet_do_id(info, &reading, &mic);
566 if (ret == -EAGAIN)
567 goto out;
568 else if (ret < 0)
569 goto done;
570
571 /* Report high impedence cables as line outputs */
572 if (reading >= 5000)
573 report = SND_JACK_LINEOUT;
574 else
575 report = SND_JACK_HEADPHONE;
576
577 snd_soc_jack_report(info->jack, report, SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
578
579 done:
580 /* Reset back to starting range */
581 regmap_update_bits(arizona->regmap,
582 ARIZONA_HEADPHONE_DETECT_1,
583 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
584 0);
585
586 arizona_extcon_hp_clamp(info, false);
587
588 if (id_gpio)
589 gpio_set_value_cansleep(id_gpio, 0);
590
591 /* If we have a mic then reenable MICDET */
592 if (state && (mic || info->mic))
593 arizona_start_mic(info);
594
595 if (info->hpdet_active) {
596 pm_runtime_put_autosuspend(arizona->dev);
597 info->hpdet_active = false;
598 }
599
600 /* Do not set hp_det done when the cable has been unplugged */
601 if (state)
602 info->hpdet_done = true;
603
604 out:
605 mutex_unlock(&info->lock);
606
607 return IRQ_HANDLED;
608 }
609
arizona_identify_headphone(struct arizona_priv * info)610 static void arizona_identify_headphone(struct arizona_priv *info)
611 {
612 struct arizona *arizona = info->arizona;
613 int ret;
614
615 if (info->hpdet_done)
616 return;
617
618 dev_dbg(arizona->dev, "Starting HPDET\n");
619
620 /* Make sure we keep the device enabled during the measurement */
621 pm_runtime_get_sync(arizona->dev);
622
623 info->hpdet_active = true;
624
625 arizona_stop_mic(info);
626
627 arizona_extcon_hp_clamp(info, true);
628
629 ret = regmap_update_bits(arizona->regmap,
630 ARIZONA_ACCESSORY_DETECT_MODE_1,
631 ARIZONA_ACCDET_MODE_MASK,
632 arizona->pdata.hpdet_channel);
633 if (ret != 0) {
634 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
635 goto err;
636 }
637
638 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
639 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
640 if (ret) {
641 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
642 goto err;
643 }
644
645 return;
646
647 err:
648 arizona_extcon_hp_clamp(info, false);
649 pm_runtime_put_autosuspend(arizona->dev);
650
651 /* Just report headphone */
652 snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
653 SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
654
655 if (info->mic)
656 arizona_start_mic(info);
657
658 info->hpdet_active = false;
659 }
660
arizona_start_hpdet_acc_id(struct arizona_priv * info)661 static void arizona_start_hpdet_acc_id(struct arizona_priv *info)
662 {
663 struct arizona *arizona = info->arizona;
664 int hp_reading = 32;
665 bool mic;
666 int ret;
667
668 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
669
670 /* Make sure we keep the device enabled during the measurement */
671 pm_runtime_get_sync(arizona->dev);
672
673 info->hpdet_active = true;
674
675 arizona_extcon_hp_clamp(info, true);
676
677 ret = regmap_update_bits(arizona->regmap,
678 ARIZONA_ACCESSORY_DETECT_MODE_1,
679 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
680 info->micd_modes[0].src |
681 arizona->pdata.hpdet_channel);
682 if (ret != 0) {
683 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
684 goto err;
685 }
686
687 if (arizona->pdata.hpdet_acc_id_line) {
688 ret = regmap_update_bits(arizona->regmap,
689 ARIZONA_HEADPHONE_DETECT_1,
690 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
691 if (ret) {
692 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
693 goto err;
694 }
695 } else {
696 arizona_hpdet_do_id(info, &hp_reading, &mic);
697 }
698
699 return;
700
701 err:
702 /* Just report headphone */
703 snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
704 SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
705
706 info->hpdet_active = false;
707 }
708
arizona_micd_timeout_work(struct work_struct * work)709 static void arizona_micd_timeout_work(struct work_struct *work)
710 {
711 struct arizona_priv *info = container_of(work,
712 struct arizona_priv,
713 micd_timeout_work.work);
714
715 mutex_lock(&info->lock);
716
717 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
718
719 info->detecting = false;
720
721 arizona_identify_headphone(info);
722
723 mutex_unlock(&info->lock);
724 }
725
arizona_micd_adc_read(struct arizona_priv * info)726 static int arizona_micd_adc_read(struct arizona_priv *info)
727 {
728 struct arizona *arizona = info->arizona;
729 unsigned int val;
730 int ret;
731
732 /* Must disable MICD before we read the ADCVAL */
733 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
734 ARIZONA_MICD_ENA, 0);
735
736 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val);
737 if (ret) {
738 dev_err(arizona->dev, "Failed to read MICDET_ADCVAL: %d\n", ret);
739 return ret;
740 }
741
742 dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val);
743
744 val &= ARIZONA_MICDET_ADCVAL_MASK;
745 if (val < ARRAY_SIZE(arizona_micd_levels))
746 val = arizona_micd_levels[val];
747 else
748 val = INT_MAX;
749
750 if (val <= QUICK_HEADPHONE_MAX_OHM)
751 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0;
752 else if (val <= MICROPHONE_MIN_OHM)
753 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1;
754 else if (val <= MICROPHONE_MAX_OHM)
755 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8;
756 else
757 val = ARIZONA_MICD_LVL_8;
758
759 return val;
760 }
761
arizona_micd_read(struct arizona_priv * info)762 static int arizona_micd_read(struct arizona_priv *info)
763 {
764 struct arizona *arizona = info->arizona;
765 unsigned int val = 0;
766 int ret, i;
767
768 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
769 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
770 if (ret) {
771 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
772 return ret;
773 }
774
775 dev_dbg(arizona->dev, "MICDET: %x\n", val);
776
777 if (!(val & ARIZONA_MICD_VALID)) {
778 dev_warn(arizona->dev, "Microphone detection state invalid\n");
779 return -EINVAL;
780 }
781 }
782
783 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
784 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
785 return -EINVAL;
786 }
787
788 return val;
789 }
790
arizona_micdet_reading(void * priv)791 static int arizona_micdet_reading(void *priv)
792 {
793 struct arizona_priv *info = priv;
794 struct arizona *arizona = info->arizona;
795 int ret, val;
796
797 if (info->detecting && arizona->pdata.micd_software_compare)
798 ret = arizona_micd_adc_read(info);
799 else
800 ret = arizona_micd_read(info);
801 if (ret < 0)
802 return ret;
803
804 val = ret;
805
806 /* Due to jack detect this should never happen */
807 if (!(val & ARIZONA_MICD_STS)) {
808 dev_warn(arizona->dev, "Detected open circuit\n");
809 info->mic = false;
810 info->detecting = false;
811 arizona_identify_headphone(info);
812 return 0;
813 }
814
815 /* If we got a high impedence we should have a headset, report it. */
816 if (val & ARIZONA_MICD_LVL_8) {
817 info->mic = true;
818 info->detecting = false;
819
820 arizona_identify_headphone(info);
821
822 snd_soc_jack_report(info->jack, SND_JACK_MICROPHONE, SND_JACK_MICROPHONE);
823
824 /* Don't need to regulate for button detection */
825 ret = regulator_allow_bypass(info->micvdd, true);
826 if (ret)
827 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
828
829 return 0;
830 }
831
832 /* If we detected a lower impedence during initial startup
833 * then we probably have the wrong polarity, flip it. Don't
834 * do this for the lowest impedences to speed up detection of
835 * plain headphones. If both polarities report a low
836 * impedence then give up and report headphones.
837 */
838 if (val & MICD_LVL_1_TO_7) {
839 if (info->jack_flips >= info->micd_num_modes * 10) {
840 dev_dbg(arizona->dev, "Detected HP/line\n");
841
842 info->detecting = false;
843
844 arizona_identify_headphone(info);
845 } else {
846 info->micd_mode++;
847 if (info->micd_mode == info->micd_num_modes)
848 info->micd_mode = 0;
849 arizona_extcon_set_mode(info, info->micd_mode);
850
851 info->jack_flips++;
852
853 if (arizona->pdata.micd_software_compare)
854 regmap_update_bits(arizona->regmap,
855 ARIZONA_MIC_DETECT_1,
856 ARIZONA_MICD_ENA,
857 ARIZONA_MICD_ENA);
858
859 queue_delayed_work(system_power_efficient_wq,
860 &info->micd_timeout_work,
861 msecs_to_jiffies(arizona->pdata.micd_timeout));
862 }
863
864 return 0;
865 }
866
867 /*
868 * If we're still detecting and we detect a short then we've
869 * got a headphone.
870 */
871 dev_dbg(arizona->dev, "Headphone detected\n");
872 info->detecting = false;
873
874 arizona_identify_headphone(info);
875
876 return 0;
877 }
878
arizona_button_reading(void * priv)879 static int arizona_button_reading(void *priv)
880 {
881 struct arizona_priv *info = priv;
882 struct arizona *arizona = info->arizona;
883 int val, key, lvl;
884
885 val = arizona_micd_read(info);
886 if (val < 0)
887 return val;
888
889 /*
890 * If we're still detecting and we detect a short then we've
891 * got a headphone. Otherwise it's a button press.
892 */
893 if (val & MICD_LVL_0_TO_7) {
894 if (info->mic) {
895 dev_dbg(arizona->dev, "Mic button detected\n");
896
897 lvl = val & ARIZONA_MICD_LVL_MASK;
898 lvl >>= ARIZONA_MICD_LVL_SHIFT;
899
900 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
901 key = ffs(lvl) - 1;
902 snd_soc_jack_report(info->jack,
903 SND_JACK_BTN_0 >> key,
904 info->micd_button_mask);
905 } else {
906 dev_err(arizona->dev, "Button out of range\n");
907 }
908 } else {
909 dev_warn(arizona->dev, "Button with no mic: %x\n", val);
910 }
911 } else {
912 dev_dbg(arizona->dev, "Mic button released\n");
913 snd_soc_jack_report(info->jack, 0, info->micd_button_mask);
914 arizona_extcon_pulse_micbias(info);
915 }
916
917 return 0;
918 }
919
arizona_micd_detect(struct work_struct * work)920 static void arizona_micd_detect(struct work_struct *work)
921 {
922 struct arizona_priv *info = container_of(work,
923 struct arizona_priv,
924 micd_detect_work.work);
925 struct arizona *arizona = info->arizona;
926
927 cancel_delayed_work_sync(&info->micd_timeout_work);
928
929 mutex_lock(&info->lock);
930
931 /* If the cable was removed while measuring ignore the result */
932 if (!(info->jack->status & SND_JACK_MECHANICAL)) {
933 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
934 mutex_unlock(&info->lock);
935 return;
936 }
937
938 if (info->detecting)
939 arizona_micdet_reading(info);
940 else
941 arizona_button_reading(info);
942
943 pm_runtime_mark_last_busy(arizona->dev);
944 mutex_unlock(&info->lock);
945 }
946
arizona_micdet(int irq,void * data)947 static irqreturn_t arizona_micdet(int irq, void *data)
948 {
949 struct arizona_priv *info = data;
950 struct arizona *arizona = info->arizona;
951 int debounce = arizona->pdata.micd_detect_debounce;
952
953 cancel_delayed_work_sync(&info->micd_detect_work);
954 cancel_delayed_work_sync(&info->micd_timeout_work);
955
956 mutex_lock(&info->lock);
957 if (!info->detecting)
958 debounce = 0;
959 mutex_unlock(&info->lock);
960
961 if (debounce)
962 queue_delayed_work(system_power_efficient_wq,
963 &info->micd_detect_work,
964 msecs_to_jiffies(debounce));
965 else
966 arizona_micd_detect(&info->micd_detect_work.work);
967
968 return IRQ_HANDLED;
969 }
970
arizona_hpdet_work(struct work_struct * work)971 static void arizona_hpdet_work(struct work_struct *work)
972 {
973 struct arizona_priv *info = container_of(work,
974 struct arizona_priv,
975 hpdet_work.work);
976
977 mutex_lock(&info->lock);
978 arizona_start_hpdet_acc_id(info);
979 mutex_unlock(&info->lock);
980 }
981
arizona_hpdet_wait(struct arizona_priv * info)982 static int arizona_hpdet_wait(struct arizona_priv *info)
983 {
984 struct arizona *arizona = info->arizona;
985 unsigned int val;
986 int i, ret;
987
988 for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
989 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
990 &val);
991 if (ret) {
992 dev_err(arizona->dev, "Failed to read HPDET state: %d\n", ret);
993 return ret;
994 }
995
996 switch (info->hpdet_ip_version) {
997 case 0:
998 if (val & ARIZONA_HP_DONE)
999 return 0;
1000 break;
1001 default:
1002 if (val & ARIZONA_HP_DONE_B)
1003 return 0;
1004 break;
1005 }
1006
1007 msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
1008 }
1009
1010 dev_warn(arizona->dev, "HPDET did not appear to complete\n");
1011
1012 return -ETIMEDOUT;
1013 }
1014
arizona_jackdet(int irq,void * data)1015 static irqreturn_t arizona_jackdet(int irq, void *data)
1016 {
1017 struct arizona_priv *info = data;
1018 struct arizona *arizona = info->arizona;
1019 unsigned int val, present, mask;
1020 bool cancelled_hp, cancelled_mic;
1021 int ret, i;
1022
1023 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
1024 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
1025
1026 pm_runtime_get_sync(arizona->dev);
1027
1028 mutex_lock(&info->lock);
1029
1030 if (info->micd_clamp) {
1031 mask = ARIZONA_MICD_CLAMP_STS;
1032 present = 0;
1033 } else {
1034 mask = ARIZONA_JD1_STS;
1035 if (arizona->pdata.jd_invert)
1036 present = 0;
1037 else
1038 present = ARIZONA_JD1_STS;
1039 }
1040
1041 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
1042 if (ret) {
1043 dev_err(arizona->dev, "Failed to read jackdet status: %d\n", ret);
1044 mutex_unlock(&info->lock);
1045 pm_runtime_put_autosuspend(arizona->dev);
1046 return IRQ_NONE;
1047 }
1048
1049 val &= mask;
1050 if (val == info->last_jackdet) {
1051 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1052 if (cancelled_hp)
1053 queue_delayed_work(system_power_efficient_wq,
1054 &info->hpdet_work,
1055 msecs_to_jiffies(HPDET_DEBOUNCE));
1056
1057 if (cancelled_mic) {
1058 int micd_timeout = arizona->pdata.micd_timeout;
1059
1060 queue_delayed_work(system_power_efficient_wq,
1061 &info->micd_timeout_work,
1062 msecs_to_jiffies(micd_timeout));
1063 }
1064
1065 goto out;
1066 }
1067 info->last_jackdet = val;
1068
1069 if (info->last_jackdet == present) {
1070 dev_dbg(arizona->dev, "Detected jack\n");
1071 snd_soc_jack_report(info->jack, SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
1072
1073 info->detecting = true;
1074 info->mic = false;
1075 info->jack_flips = 0;
1076
1077 if (!arizona->pdata.hpdet_acc_id) {
1078 arizona_start_mic(info);
1079 } else {
1080 queue_delayed_work(system_power_efficient_wq,
1081 &info->hpdet_work,
1082 msecs_to_jiffies(HPDET_DEBOUNCE));
1083 }
1084
1085 if (info->micd_clamp || !arizona->pdata.jd_invert)
1086 regmap_update_bits(arizona->regmap,
1087 ARIZONA_JACK_DETECT_DEBOUNCE,
1088 ARIZONA_MICD_CLAMP_DB |
1089 ARIZONA_JD1_DB, 0);
1090 } else {
1091 dev_dbg(arizona->dev, "Detected jack removal\n");
1092
1093 arizona_stop_mic(info);
1094
1095 info->num_hpdet_res = 0;
1096 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1097 info->hpdet_res[i] = 0;
1098 info->mic = false;
1099 info->hpdet_done = false;
1100 info->hpdet_retried = false;
1101
1102 snd_soc_jack_report(info->jack, 0, ARIZONA_JACK_MASK | info->micd_button_mask);
1103
1104 /*
1105 * If the jack was removed during a headphone detection we
1106 * need to wait for the headphone detection to finish, as
1107 * it can not be aborted. We don't want to be able to start
1108 * a new headphone detection from a fresh insert until this
1109 * one is finished.
1110 */
1111 arizona_hpdet_wait(info);
1112
1113 regmap_update_bits(arizona->regmap,
1114 ARIZONA_JACK_DETECT_DEBOUNCE,
1115 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1116 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1117 }
1118
1119 out:
1120 /* Clear trig_sts to make sure DCVDD is not forced up */
1121 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1122 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1123 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1124 ARIZONA_JD1_FALL_TRIG_STS |
1125 ARIZONA_JD1_RISE_TRIG_STS);
1126
1127 mutex_unlock(&info->lock);
1128
1129 pm_runtime_put_autosuspend(arizona->dev);
1130
1131 return IRQ_HANDLED;
1132 }
1133
1134 /* Map a level onto a slot in the register bank */
arizona_micd_set_level(struct arizona * arizona,int index,unsigned int level)1135 static void arizona_micd_set_level(struct arizona *arizona, int index,
1136 unsigned int level)
1137 {
1138 int reg;
1139 unsigned int mask;
1140
1141 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1142
1143 if (!(index % 2)) {
1144 mask = 0x3f00;
1145 level <<= 8;
1146 } else {
1147 mask = 0x3f;
1148 }
1149
1150 /* Program the level itself */
1151 regmap_update_bits(arizona->regmap, reg, mask, level);
1152 }
1153
arizona_extcon_get_micd_configs(struct device * dev,struct arizona * arizona)1154 static int arizona_extcon_get_micd_configs(struct device *dev,
1155 struct arizona *arizona)
1156 {
1157 const char * const prop = "wlf,micd-configs";
1158 const int entries_per_config = 3;
1159 struct arizona_micd_config *micd_configs;
1160 int nconfs, ret;
1161 int i, j;
1162 u32 *vals;
1163
1164 nconfs = device_property_count_u32(arizona->dev, prop);
1165 if (nconfs <= 0)
1166 return 0;
1167
1168 vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL);
1169 if (!vals)
1170 return -ENOMEM;
1171
1172 ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs);
1173 if (ret < 0)
1174 goto out;
1175
1176 nconfs /= entries_per_config;
1177 micd_configs = devm_kcalloc(dev, nconfs, sizeof(*micd_configs),
1178 GFP_KERNEL);
1179 if (!micd_configs) {
1180 ret = -ENOMEM;
1181 goto out;
1182 }
1183
1184 for (i = 0, j = 0; i < nconfs; ++i) {
1185 micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0;
1186 micd_configs[i].bias = vals[j++];
1187 micd_configs[i].gpio = vals[j++];
1188 }
1189
1190 arizona->pdata.micd_configs = micd_configs;
1191 arizona->pdata.num_micd_configs = nconfs;
1192
1193 out:
1194 kfree(vals);
1195 return ret;
1196 }
1197
arizona_extcon_device_get_pdata(struct device * dev,struct arizona * arizona)1198 static int arizona_extcon_device_get_pdata(struct device *dev,
1199 struct arizona *arizona)
1200 {
1201 struct arizona_pdata *pdata = &arizona->pdata;
1202 unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1203 int ret;
1204
1205 device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
1206 switch (val) {
1207 case ARIZONA_ACCDET_MODE_HPL:
1208 case ARIZONA_ACCDET_MODE_HPR:
1209 pdata->hpdet_channel = val;
1210 break;
1211 default:
1212 dev_err(arizona->dev, "Wrong wlf,hpdet-channel DT value %d\n", val);
1213 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1214 }
1215
1216 device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
1217 &pdata->micd_detect_debounce);
1218
1219 device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
1220 &pdata->micd_bias_start_time);
1221
1222 device_property_read_u32(arizona->dev, "wlf,micd-rate",
1223 &pdata->micd_rate);
1224
1225 device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
1226 &pdata->micd_dbtime);
1227
1228 device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms",
1229 &pdata->micd_timeout);
1230
1231 pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
1232 "wlf,micd-force-micbias");
1233
1234 pdata->micd_software_compare = device_property_read_bool(arizona->dev,
1235 "wlf,micd-software-compare");
1236
1237 pdata->jd_invert = device_property_read_bool(arizona->dev,
1238 "wlf,jd-invert");
1239
1240 device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw);
1241
1242 pdata->jd_gpio5 = device_property_read_bool(arizona->dev,
1243 "wlf,use-jd2");
1244 pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev,
1245 "wlf,use-jd2-nopull");
1246
1247 ret = arizona_extcon_get_micd_configs(dev, arizona);
1248 if (ret < 0)
1249 dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret);
1250
1251 return 0;
1252 }
1253
arizona_jack_codec_dev_probe(struct arizona_priv * info,struct device * dev)1254 int arizona_jack_codec_dev_probe(struct arizona_priv *info, struct device *dev)
1255 {
1256 struct arizona *arizona = info->arizona;
1257 struct arizona_pdata *pdata = &arizona->pdata;
1258 int ret, mode;
1259
1260 if (!dev_get_platdata(arizona->dev))
1261 arizona_extcon_device_get_pdata(dev, arizona);
1262
1263 info->micvdd = devm_regulator_get(dev, "MICVDD");
1264 if (IS_ERR(info->micvdd))
1265 return dev_err_probe(arizona->dev, PTR_ERR(info->micvdd), "getting MICVDD\n");
1266
1267 mutex_init(&info->lock);
1268 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1269 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1270 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1271 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1272
1273 switch (arizona->type) {
1274 case WM5102:
1275 switch (arizona->rev) {
1276 case 0:
1277 info->micd_reva = true;
1278 break;
1279 default:
1280 info->micd_clamp = true;
1281 info->hpdet_ip_version = 1;
1282 break;
1283 }
1284 break;
1285 case WM5110:
1286 case WM8280:
1287 switch (arizona->rev) {
1288 case 0 ... 2:
1289 break;
1290 default:
1291 info->micd_clamp = true;
1292 info->hpdet_ip_version = 2;
1293 break;
1294 }
1295 break;
1296 case WM8998:
1297 case WM1814:
1298 info->micd_clamp = true;
1299 info->hpdet_ip_version = 2;
1300 break;
1301 default:
1302 break;
1303 }
1304
1305 if (!pdata->micd_timeout)
1306 pdata->micd_timeout = DEFAULT_MICD_TIMEOUT;
1307
1308 if (pdata->num_micd_configs) {
1309 info->micd_modes = pdata->micd_configs;
1310 info->micd_num_modes = pdata->num_micd_configs;
1311 } else {
1312 info->micd_modes = micd_default_modes;
1313 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1314 }
1315
1316 if (arizona->pdata.gpsw > 0)
1317 regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1,
1318 ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw);
1319
1320 if (pdata->micd_pol_gpio > 0) {
1321 if (info->micd_modes[0].gpio)
1322 mode = GPIOF_OUT_INIT_HIGH;
1323 else
1324 mode = GPIOF_OUT_INIT_LOW;
1325
1326 ret = devm_gpio_request_one(dev, pdata->micd_pol_gpio,
1327 mode, "MICD polarity");
1328 if (ret != 0) {
1329 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1330 pdata->micd_pol_gpio, ret);
1331 return ret;
1332 }
1333
1334 info->micd_pol_gpio = gpio_to_desc(pdata->micd_pol_gpio);
1335 } else {
1336 if (info->micd_modes[0].gpio)
1337 mode = GPIOD_OUT_HIGH;
1338 else
1339 mode = GPIOD_OUT_LOW;
1340
1341 /* We can't use devm here because we need to do the get
1342 * against the MFD device, as that is where the of_node
1343 * will reside, but if we devm against that the GPIO
1344 * will not be freed if the extcon driver is unloaded.
1345 */
1346 info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
1347 "wlf,micd-pol",
1348 mode);
1349 if (IS_ERR(info->micd_pol_gpio)) {
1350 ret = PTR_ERR(info->micd_pol_gpio);
1351 dev_err_probe(arizona->dev, ret, "getting microphone polarity GPIO\n");
1352 return ret;
1353 }
1354 }
1355
1356 if (arizona->pdata.hpdet_id_gpio > 0) {
1357 ret = devm_gpio_request_one(dev, arizona->pdata.hpdet_id_gpio,
1358 GPIOF_OUT_INIT_LOW,
1359 "HPDET");
1360 if (ret != 0) {
1361 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1362 arizona->pdata.hpdet_id_gpio, ret);
1363 gpiod_put(info->micd_pol_gpio);
1364 return ret;
1365 }
1366 }
1367
1368 return 0;
1369 }
1370 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_probe);
1371
arizona_jack_codec_dev_remove(struct arizona_priv * info)1372 int arizona_jack_codec_dev_remove(struct arizona_priv *info)
1373 {
1374 gpiod_put(info->micd_pol_gpio);
1375 return 0;
1376 }
1377 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_remove);
1378
arizona_jack_enable_jack_detect(struct arizona_priv * info,struct snd_soc_jack * jack)1379 static int arizona_jack_enable_jack_detect(struct arizona_priv *info,
1380 struct snd_soc_jack *jack)
1381 {
1382 struct arizona *arizona = info->arizona;
1383 struct arizona_pdata *pdata = &arizona->pdata;
1384 unsigned int val;
1385 unsigned int clamp_mode;
1386 int jack_irq_fall, jack_irq_rise;
1387 int ret, i, j;
1388
1389 if (arizona->pdata.micd_bias_start_time)
1390 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1391 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1392 arizona->pdata.micd_bias_start_time
1393 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1394
1395 if (arizona->pdata.micd_rate)
1396 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1397 ARIZONA_MICD_RATE_MASK,
1398 arizona->pdata.micd_rate
1399 << ARIZONA_MICD_RATE_SHIFT);
1400
1401 switch (arizona->pdata.micd_dbtime) {
1402 case MICD_DBTIME_FOUR_READINGS:
1403 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1404 ARIZONA_MICD_DBTIME_MASK,
1405 ARIZONA_MICD_DBTIME);
1406 break;
1407 case MICD_DBTIME_TWO_READINGS:
1408 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1409 ARIZONA_MICD_DBTIME_MASK, 0);
1410 break;
1411 default:
1412 break;
1413 }
1414
1415 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) <
1416 ARIZONA_NUM_MICD_BUTTON_LEVELS);
1417
1418 if (arizona->pdata.num_micd_ranges) {
1419 info->micd_ranges = pdata->micd_ranges;
1420 info->num_micd_ranges = pdata->num_micd_ranges;
1421 } else {
1422 info->micd_ranges = micd_default_ranges;
1423 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1424 }
1425
1426 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_BUTTONS) {
1427 dev_err(arizona->dev, "Too many MICD ranges: %d > %d\n",
1428 arizona->pdata.num_micd_ranges, ARIZONA_MAX_MICD_BUTTONS);
1429 return -EINVAL;
1430 }
1431
1432 if (info->num_micd_ranges > 1) {
1433 for (i = 1; i < info->num_micd_ranges; i++) {
1434 if (info->micd_ranges[i - 1].max >
1435 info->micd_ranges[i].max) {
1436 dev_err(arizona->dev, "MICD ranges must be sorted\n");
1437 return -EINVAL;
1438 }
1439 }
1440 }
1441
1442 /* Disable all buttons by default */
1443 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1444 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1445
1446 /* Set up all the buttons the user specified */
1447 for (i = 0; i < info->num_micd_ranges; i++) {
1448 for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++)
1449 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1450 break;
1451
1452 if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) {
1453 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1454 info->micd_ranges[i].max);
1455 return -EINVAL;
1456 }
1457
1458 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1459 arizona_micd_levels[j], i);
1460
1461 arizona_micd_set_level(arizona, i, j);
1462
1463 /* SND_JACK_BTN_# masks start with the most significant bit */
1464 info->micd_button_mask |= SND_JACK_BTN_0 >> i;
1465 snd_jack_set_key(jack->jack, SND_JACK_BTN_0 >> i,
1466 info->micd_ranges[i].key);
1467
1468 /* Enable reporting of that range */
1469 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1470 1 << i, 1 << i);
1471 }
1472
1473 /* Set all the remaining keys to a maximum */
1474 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1475 arizona_micd_set_level(arizona, i, 0x3f);
1476
1477 /*
1478 * If we have a clamp use it, activating in conjunction with
1479 * GPIO5 if that is connected for jack detect operation.
1480 */
1481 if (info->micd_clamp) {
1482 if (arizona->pdata.jd_gpio5) {
1483 /* Put the GPIO into input mode with optional pull */
1484 val = 0xc101;
1485 if (arizona->pdata.jd_gpio5_nopull)
1486 val &= ~ARIZONA_GPN_PU;
1487
1488 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1489 val);
1490
1491 if (arizona->pdata.jd_invert)
1492 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1493 else
1494 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1495 } else {
1496 if (arizona->pdata.jd_invert)
1497 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1498 else
1499 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1500 }
1501
1502 regmap_update_bits(arizona->regmap,
1503 ARIZONA_MICD_CLAMP_CONTROL,
1504 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1505
1506 regmap_update_bits(arizona->regmap,
1507 ARIZONA_JACK_DETECT_DEBOUNCE,
1508 ARIZONA_MICD_CLAMP_DB,
1509 ARIZONA_MICD_CLAMP_DB);
1510 }
1511
1512 arizona_extcon_set_mode(info, 0);
1513
1514 info->jack = jack;
1515
1516 pm_runtime_get_sync(arizona->dev);
1517
1518 if (info->micd_clamp) {
1519 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1520 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1521 } else {
1522 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1523 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1524 }
1525
1526 ret = arizona_request_irq(arizona, jack_irq_rise,
1527 "JACKDET rise", arizona_jackdet, info);
1528 if (ret != 0) {
1529 dev_err(arizona->dev, "Failed to get JACKDET rise IRQ: %d\n", ret);
1530 goto err_pm;
1531 }
1532
1533 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1534 if (ret != 0) {
1535 dev_err(arizona->dev, "Failed to set JD rise IRQ wake: %d\n", ret);
1536 goto err_rise;
1537 }
1538
1539 ret = arizona_request_irq(arizona, jack_irq_fall,
1540 "JACKDET fall", arizona_jackdet, info);
1541 if (ret != 0) {
1542 dev_err(arizona->dev, "Failed to get JD fall IRQ: %d\n", ret);
1543 goto err_rise_wake;
1544 }
1545
1546 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1547 if (ret != 0) {
1548 dev_err(arizona->dev, "Failed to set JD fall IRQ wake: %d\n", ret);
1549 goto err_fall;
1550 }
1551
1552 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1553 "MICDET", arizona_micdet, info);
1554 if (ret != 0) {
1555 dev_err(arizona->dev, "Failed to get MICDET IRQ: %d\n", ret);
1556 goto err_fall_wake;
1557 }
1558
1559 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1560 "HPDET", arizona_hpdet_irq, info);
1561 if (ret != 0) {
1562 dev_err(arizona->dev, "Failed to get HPDET IRQ: %d\n", ret);
1563 goto err_micdet;
1564 }
1565
1566 arizona_clk32k_enable(arizona);
1567 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1568 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1569 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1570 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1571
1572 ret = regulator_allow_bypass(info->micvdd, true);
1573 if (ret != 0)
1574 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n", ret);
1575
1576 pm_runtime_put(arizona->dev);
1577
1578 return 0;
1579
1580 err_micdet:
1581 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1582 err_fall_wake:
1583 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1584 err_fall:
1585 arizona_free_irq(arizona, jack_irq_fall, info);
1586 err_rise_wake:
1587 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1588 err_rise:
1589 arizona_free_irq(arizona, jack_irq_rise, info);
1590 err_pm:
1591 pm_runtime_put(arizona->dev);
1592 info->jack = NULL;
1593 return ret;
1594 }
1595
arizona_jack_disable_jack_detect(struct arizona_priv * info)1596 static int arizona_jack_disable_jack_detect(struct arizona_priv *info)
1597 {
1598 struct arizona *arizona = info->arizona;
1599 int jack_irq_rise, jack_irq_fall;
1600 bool change;
1601 int ret;
1602
1603 if (!info->jack)
1604 return 0;
1605
1606 if (info->micd_clamp) {
1607 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1608 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1609 } else {
1610 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1611 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1612 }
1613
1614 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1615 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1616 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1617 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1618 arizona_free_irq(arizona, jack_irq_rise, info);
1619 arizona_free_irq(arizona, jack_irq_fall, info);
1620 cancel_delayed_work_sync(&info->hpdet_work);
1621 cancel_delayed_work_sync(&info->micd_detect_work);
1622 cancel_delayed_work_sync(&info->micd_timeout_work);
1623
1624 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
1625 ARIZONA_MICD_ENA, 0,
1626 &change);
1627 if (ret < 0) {
1628 dev_err(arizona->dev, "Failed to disable micd on remove: %d\n", ret);
1629 } else if (change) {
1630 regulator_disable(info->micvdd);
1631 pm_runtime_put(arizona->dev);
1632 }
1633
1634 regmap_update_bits(arizona->regmap,
1635 ARIZONA_MICD_CLAMP_CONTROL,
1636 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1637 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1638 ARIZONA_JD1_ENA, 0);
1639 arizona_clk32k_disable(arizona);
1640 info->jack = NULL;
1641
1642 return 0;
1643 }
1644
arizona_jack_set_jack(struct snd_soc_component * component,struct snd_soc_jack * jack,void * data)1645 int arizona_jack_set_jack(struct snd_soc_component *component,
1646 struct snd_soc_jack *jack, void *data)
1647 {
1648 struct arizona_priv *info = snd_soc_component_get_drvdata(component);
1649
1650 if (jack)
1651 return arizona_jack_enable_jack_detect(info, jack);
1652 else
1653 return arizona_jack_disable_jack_detect(info);
1654 }
1655 EXPORT_SYMBOL_GPL(arizona_jack_set_jack);
1656