2 * extcon-arizona.c - Extcon driver Wolfson Arizona devices
4 * Copyright (C) 2012 Wolfson Microelectronics plc
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.
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.
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.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/extcon.h>
30 #include <linux/mfd/arizona/core.h>
31 #include <linux/mfd/arizona/pdata.h>
32 #include <linux/mfd/arizona/registers.h>
34 #define ARIZONA_NUM_BUTTONS 6
36 struct arizona_extcon_info {
38 struct arizona *arizona;
40 struct regulator *micvdd;
41 struct input_dev *input;
44 const struct arizona_micd_config *micd_modes;
54 struct extcon_dev edev;
57 static const struct arizona_micd_config micd_default_modes[] = {
58 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
59 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
65 } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
74 #define ARIZONA_CABLE_MECHANICAL 0
75 #define ARIZONA_CABLE_MICROPHONE 1
76 #define ARIZONA_CABLE_HEADPHONE 2
78 static const char *arizona_cable[] = {
85 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
87 struct arizona *arizona = info->arizona;
89 if (arizona->pdata.micd_pol_gpio > 0)
90 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
91 info->micd_modes[mode].gpio);
92 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
93 ARIZONA_MICD_BIAS_SRC_MASK,
94 info->micd_modes[mode].bias);
95 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
96 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
98 info->micd_mode = mode;
100 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
103 static void arizona_start_mic(struct arizona_extcon_info *info)
105 struct arizona *arizona = info->arizona;
109 info->detecting = true;
111 info->jack_flips = 0;
113 /* Microphone detection can't use idle mode */
114 pm_runtime_get(info->dev);
116 ret = regulator_enable(info->micvdd);
118 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
122 if (info->micd_reva) {
123 regmap_write(arizona->regmap, 0x80, 0x3);
124 regmap_write(arizona->regmap, 0x294, 0);
125 regmap_write(arizona->regmap, 0x80, 0x0);
128 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
129 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
132 regulator_disable(info->micvdd);
133 pm_runtime_put_autosuspend(info->dev);
137 static void arizona_stop_mic(struct arizona_extcon_info *info)
139 struct arizona *arizona = info->arizona;
142 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
146 if (info->micd_reva) {
147 regmap_write(arizona->regmap, 0x80, 0x3);
148 regmap_write(arizona->regmap, 0x294, 2);
149 regmap_write(arizona->regmap, 0x80, 0x0);
153 regulator_disable(info->micvdd);
154 pm_runtime_mark_last_busy(info->dev);
155 pm_runtime_put_autosuspend(info->dev);
159 static irqreturn_t arizona_micdet(int irq, void *data)
161 struct arizona_extcon_info *info = data;
162 struct arizona *arizona = info->arizona;
163 unsigned int val, lvl;
166 mutex_lock(&info->lock);
168 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
170 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
171 mutex_unlock(&info->lock);
175 dev_dbg(arizona->dev, "MICDET: %x\n", val);
177 if (!(val & ARIZONA_MICD_VALID)) {
178 dev_warn(arizona->dev, "Microphone detection state invalid\n");
179 mutex_unlock(&info->lock);
183 /* Due to jack detect this should never happen */
184 if (!(val & ARIZONA_MICD_STS)) {
185 dev_warn(arizona->dev, "Detected open circuit\n");
186 info->detecting = false;
190 /* If we got a high impedence we should have a headset, report it. */
191 if (info->detecting && (val & 0x400)) {
192 ret = extcon_update_state(&info->edev,
193 1 << ARIZONA_CABLE_MICROPHONE |
194 1 << ARIZONA_CABLE_HEADPHONE,
195 1 << ARIZONA_CABLE_MICROPHONE |
196 1 << ARIZONA_CABLE_HEADPHONE);
199 dev_err(arizona->dev, "Headset report failed: %d\n",
203 info->detecting = false;
207 /* If we detected a lower impedence during initial startup
208 * then we probably have the wrong polarity, flip it. Don't
209 * do this for the lowest impedences to speed up detection of
210 * plain headphones. If both polarities report a low
211 * impedence then give up and report headphones.
213 if (info->detecting && (val & 0x3f8)) {
216 if (info->jack_flips >= info->micd_num_modes) {
217 dev_dbg(arizona->dev, "Detected headphone\n");
218 info->detecting = false;
219 arizona_stop_mic(info);
221 ret = extcon_set_cable_state_(&info->edev,
222 ARIZONA_CABLE_HEADPHONE,
225 dev_err(arizona->dev,
226 "Headphone report failed: %d\n",
230 if (info->micd_mode == info->micd_num_modes)
232 arizona_extcon_set_mode(info, info->micd_mode);
241 * If we're still detecting and we detect a short then we've
242 * got a headphone. Otherwise it's a button press.
246 dev_dbg(arizona->dev, "Mic button detected\n");
248 lvl = val & ARIZONA_MICD_LVL_MASK;
249 lvl >>= ARIZONA_MICD_LVL_SHIFT;
251 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
252 if (lvl & arizona_lvl_to_key[i].status)
253 input_report_key(info->input,
254 arizona_lvl_to_key[i].report,
256 input_sync(info->input);
258 } else if (info->detecting) {
259 dev_dbg(arizona->dev, "Headphone detected\n");
260 info->detecting = false;
261 arizona_stop_mic(info);
263 ret = extcon_set_cable_state_(&info->edev,
264 ARIZONA_CABLE_HEADPHONE,
267 dev_err(arizona->dev,
268 "Headphone report failed: %d\n",
271 dev_warn(arizona->dev, "Button with no mic: %x\n",
275 dev_dbg(arizona->dev, "Mic button released\n");
276 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
277 input_report_key(info->input,
278 arizona_lvl_to_key[i].report, 0);
279 input_sync(info->input);
283 pm_runtime_mark_last_busy(info->dev);
284 mutex_unlock(&info->lock);
289 static irqreturn_t arizona_jackdet(int irq, void *data)
291 struct arizona_extcon_info *info = data;
292 struct arizona *arizona = info->arizona;
293 unsigned int val, present, mask;
296 pm_runtime_get_sync(info->dev);
298 mutex_lock(&info->lock);
300 if (arizona->pdata.jd_gpio5) {
301 mask = ARIZONA_MICD_CLAMP_STS;
304 mask = ARIZONA_JD1_STS;
305 present = ARIZONA_JD1_STS;
308 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
310 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
312 mutex_unlock(&info->lock);
313 pm_runtime_put_autosuspend(info->dev);
317 if ((val & mask) == present) {
318 dev_dbg(arizona->dev, "Detected jack\n");
319 ret = extcon_set_cable_state_(&info->edev,
320 ARIZONA_CABLE_MECHANICAL, true);
323 dev_err(arizona->dev, "Mechanical report failed: %d\n",
326 arizona_start_mic(info);
328 dev_dbg(arizona->dev, "Detected jack removal\n");
330 arizona_stop_mic(info);
333 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
334 input_report_key(info->input,
335 arizona_lvl_to_key[i].report, 0);
336 input_sync(info->input);
338 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
340 dev_err(arizona->dev, "Removal report failed: %d\n",
344 mutex_unlock(&info->lock);
346 pm_runtime_mark_last_busy(info->dev);
347 pm_runtime_put_autosuspend(info->dev);
352 static int arizona_extcon_probe(struct platform_device *pdev)
354 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
355 struct arizona_pdata *pdata;
356 struct arizona_extcon_info *info;
357 int jack_irq_fall, jack_irq_rise;
360 pdata = dev_get_platdata(arizona->dev);
362 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
364 dev_err(&pdev->dev, "Failed to allocate memory\n");
369 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
370 if (IS_ERR(info->micvdd)) {
371 ret = PTR_ERR(info->micvdd);
372 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
376 mutex_init(&info->lock);
377 info->arizona = arizona;
378 info->dev = &pdev->dev;
379 info->detecting = true;
380 platform_set_drvdata(pdev, info);
382 switch (arizona->type) {
384 switch (arizona->rev) {
386 info->micd_reva = true;
389 info->micd_clamp = true;
397 info->edev.name = "Headset Jack";
398 info->edev.supported_cable = arizona_cable;
400 ret = extcon_dev_register(&info->edev, arizona->dev);
402 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
407 if (pdata->num_micd_configs) {
408 info->micd_modes = pdata->micd_configs;
409 info->micd_num_modes = pdata->num_micd_configs;
411 info->micd_modes = micd_default_modes;
412 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
415 if (arizona->pdata.micd_pol_gpio > 0) {
416 if (info->micd_modes[0].gpio)
417 mode = GPIOF_OUT_INIT_HIGH;
419 mode = GPIOF_OUT_INIT_LOW;
421 ret = devm_gpio_request_one(&pdev->dev,
422 arizona->pdata.micd_pol_gpio,
426 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
427 arizona->pdata.micd_pol_gpio, ret);
432 if (arizona->pdata.micd_bias_start_time)
433 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
434 ARIZONA_MICD_BIAS_STARTTIME_MASK,
435 arizona->pdata.micd_bias_start_time
436 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
439 * If we have a clamp use it, activating in conjunction with
440 * GPIO5 if that is connected for jack detect operation.
442 if (info->micd_clamp) {
443 if (arizona->pdata.jd_gpio5) {
444 /* Put the GPIO into input mode */
445 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
448 regmap_update_bits(arizona->regmap,
449 ARIZONA_MICD_CLAMP_CONTROL,
450 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
452 regmap_update_bits(arizona->regmap,
453 ARIZONA_MICD_CLAMP_CONTROL,
454 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
457 regmap_update_bits(arizona->regmap,
458 ARIZONA_JACK_DETECT_DEBOUNCE,
459 ARIZONA_MICD_CLAMP_DB,
460 ARIZONA_MICD_CLAMP_DB);
463 arizona_extcon_set_mode(info, 0);
465 info->input = devm_input_allocate_device(&pdev->dev);
467 dev_err(arizona->dev, "Can't allocate input dev\n");
472 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
473 input_set_capability(info->input, EV_KEY,
474 arizona_lvl_to_key[i].report);
475 info->input->name = "Headset";
476 info->input->phys = "arizona/extcon";
477 info->input->dev.parent = &pdev->dev;
479 pm_runtime_enable(&pdev->dev);
480 pm_runtime_idle(&pdev->dev);
481 pm_runtime_get_sync(&pdev->dev);
483 if (arizona->pdata.jd_gpio5) {
484 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
485 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
487 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
488 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
491 ret = arizona_request_irq(arizona, jack_irq_rise,
492 "JACKDET rise", arizona_jackdet, info);
494 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
499 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
501 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
506 ret = arizona_request_irq(arizona, jack_irq_fall,
507 "JACKDET fall", arizona_jackdet, info);
509 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
513 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
515 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
520 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
521 "MICDET", arizona_micdet, info);
523 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
527 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
528 ARIZONA_MICD_RATE_MASK,
529 8 << ARIZONA_MICD_RATE_SHIFT);
531 arizona_clk32k_enable(arizona);
532 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
533 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
534 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
535 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
537 ret = regulator_allow_bypass(info->micvdd, true);
539 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
542 pm_runtime_put(&pdev->dev);
544 ret = input_register_device(info->input);
546 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
553 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
555 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
557 arizona_free_irq(arizona, jack_irq_fall, info);
559 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
561 arizona_free_irq(arizona, jack_irq_rise, info);
564 pm_runtime_disable(&pdev->dev);
565 extcon_dev_unregister(&info->edev);
570 static int arizona_extcon_remove(struct platform_device *pdev)
572 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
573 struct arizona *arizona = info->arizona;
574 int jack_irq_rise, jack_irq_fall;
576 pm_runtime_disable(&pdev->dev);
578 regmap_update_bits(arizona->regmap,
579 ARIZONA_MICD_CLAMP_CONTROL,
580 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
582 if (arizona->pdata.jd_gpio5) {
583 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
584 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
586 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
587 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
590 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
591 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
592 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
593 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
594 arizona_free_irq(arizona, jack_irq_rise, info);
595 arizona_free_irq(arizona, jack_irq_fall, info);
596 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
598 arizona_clk32k_disable(arizona);
599 extcon_dev_unregister(&info->edev);
604 static struct platform_driver arizona_extcon_driver = {
606 .name = "arizona-extcon",
607 .owner = THIS_MODULE,
609 .probe = arizona_extcon_probe,
610 .remove = arizona_extcon_remove,
613 module_platform_driver(arizona_extcon_driver);
615 MODULE_DESCRIPTION("Arizona Extcon driver");
616 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
617 MODULE_LICENSE("GPL");
618 MODULE_ALIAS("platform:extcon-arizona");