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