mtd: fix the wrong timeo for panic_nand_wait()
[linux-2.6.git] / drivers / extcon / extcon-arizona.c
blob414aed50b1bc0aa52c32a49019eef746f7d63617
1 /*
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 {
37 struct device *dev;
38 struct arizona *arizona;
39 struct mutex lock;
40 struct regulator *micvdd;
41 struct input_dev *input;
43 int micd_mode;
44 const struct arizona_micd_config *micd_modes;
45 int micd_num_modes;
47 bool micd_reva;
49 bool mic;
50 bool detecting;
51 int jack_flips;
53 struct extcon_dev edev;
56 static const struct arizona_micd_config micd_default_modes[] = {
57 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
58 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
61 static struct {
62 u16 status;
63 int report;
64 } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
65 { 0x1, BTN_0 },
66 { 0x2, BTN_1 },
67 { 0x4, BTN_2 },
68 { 0x8, BTN_3 },
69 { 0x10, BTN_4 },
70 { 0x20, BTN_5 },
73 #define ARIZONA_CABLE_MECHANICAL 0
74 #define ARIZONA_CABLE_MICROPHONE 1
75 #define ARIZONA_CABLE_HEADPHONE 2
77 static const char *arizona_cable[] = {
78 "Mechanical",
79 "Microphone",
80 "Headphone",
81 NULL,
84 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
86 struct arizona *arizona = info->arizona;
88 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
89 info->micd_modes[mode].gpio);
90 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
91 ARIZONA_MICD_BIAS_SRC_MASK,
92 info->micd_modes[mode].bias);
93 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
94 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
96 info->micd_mode = mode;
98 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
101 static void arizona_start_mic(struct arizona_extcon_info *info)
103 struct arizona *arizona = info->arizona;
104 bool change;
105 int ret;
107 info->detecting = true;
108 info->mic = false;
109 info->jack_flips = 0;
111 /* Microphone detection can't use idle mode */
112 pm_runtime_get(info->dev);
114 ret = regulator_enable(info->micvdd);
115 if (ret != 0) {
116 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
117 ret);
120 if (info->micd_reva) {
121 regmap_write(arizona->regmap, 0x80, 0x3);
122 regmap_write(arizona->regmap, 0x294, 0);
123 regmap_write(arizona->regmap, 0x80, 0x0);
126 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
127 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
128 &change);
129 if (!change) {
130 regulator_disable(info->micvdd);
131 pm_runtime_put_autosuspend(info->dev);
135 static void arizona_stop_mic(struct arizona_extcon_info *info)
137 struct arizona *arizona = info->arizona;
138 bool change;
140 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
141 ARIZONA_MICD_ENA, 0,
142 &change);
144 if (info->micd_reva) {
145 regmap_write(arizona->regmap, 0x80, 0x3);
146 regmap_write(arizona->regmap, 0x294, 2);
147 regmap_write(arizona->regmap, 0x80, 0x0);
150 if (change) {
151 regulator_disable(info->micvdd);
152 pm_runtime_mark_last_busy(info->dev);
153 pm_runtime_put_autosuspend(info->dev);
157 static irqreturn_t arizona_micdet(int irq, void *data)
159 struct arizona_extcon_info *info = data;
160 struct arizona *arizona = info->arizona;
161 unsigned int val, lvl;
162 int ret, i;
164 mutex_lock(&info->lock);
166 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
167 if (ret != 0) {
168 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
169 mutex_unlock(&info->lock);
170 return IRQ_NONE;
173 dev_dbg(arizona->dev, "MICDET: %x\n", val);
175 if (!(val & ARIZONA_MICD_VALID)) {
176 dev_warn(arizona->dev, "Microphone detection state invalid\n");
177 mutex_unlock(&info->lock);
178 return IRQ_NONE;
181 /* Due to jack detect this should never happen */
182 if (!(val & ARIZONA_MICD_STS)) {
183 dev_warn(arizona->dev, "Detected open circuit\n");
184 info->detecting = false;
185 goto handled;
188 /* If we got a high impedence we should have a headset, report it. */
189 if (info->detecting && (val & 0x400)) {
190 ret = extcon_update_state(&info->edev,
191 1 << ARIZONA_CABLE_MICROPHONE |
192 1 << ARIZONA_CABLE_HEADPHONE,
193 1 << ARIZONA_CABLE_MICROPHONE |
194 1 << ARIZONA_CABLE_HEADPHONE);
196 if (ret != 0)
197 dev_err(arizona->dev, "Headset report failed: %d\n",
198 ret);
200 info->mic = true;
201 info->detecting = false;
202 goto handled;
205 /* If we detected a lower impedence during initial startup
206 * then we probably have the wrong polarity, flip it. Don't
207 * do this for the lowest impedences to speed up detection of
208 * plain headphones. If both polarities report a low
209 * impedence then give up and report headphones.
211 if (info->detecting && (val & 0x3f8)) {
212 info->jack_flips++;
214 if (info->jack_flips >= info->micd_num_modes) {
215 dev_dbg(arizona->dev, "Detected headphone\n");
216 info->detecting = false;
217 arizona_stop_mic(info);
219 ret = extcon_set_cable_state_(&info->edev,
220 ARIZONA_CABLE_HEADPHONE,
221 true);
222 if (ret != 0)
223 dev_err(arizona->dev,
224 "Headphone report failed: %d\n",
225 ret);
226 } else {
227 info->micd_mode++;
228 if (info->micd_mode == info->micd_num_modes)
229 info->micd_mode = 0;
230 arizona_extcon_set_mode(info, info->micd_mode);
232 info->jack_flips++;
235 goto handled;
239 * If we're still detecting and we detect a short then we've
240 * got a headphone. Otherwise it's a button press.
242 if (val & 0x3fc) {
243 if (info->mic) {
244 dev_dbg(arizona->dev, "Mic button detected\n");
246 lvl = val & ARIZONA_MICD_LVL_MASK;
247 lvl >>= ARIZONA_MICD_LVL_SHIFT;
249 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
250 if (lvl & arizona_lvl_to_key[i].status)
251 input_report_key(info->input,
252 arizona_lvl_to_key[i].report,
254 input_sync(info->input);
256 } else if (info->detecting) {
257 dev_dbg(arizona->dev, "Headphone detected\n");
258 info->detecting = false;
259 arizona_stop_mic(info);
261 ret = extcon_set_cable_state_(&info->edev,
262 ARIZONA_CABLE_HEADPHONE,
263 true);
264 if (ret != 0)
265 dev_err(arizona->dev,
266 "Headphone report failed: %d\n",
267 ret);
268 } else {
269 dev_warn(arizona->dev, "Button with no mic: %x\n",
270 val);
272 } else {
273 dev_dbg(arizona->dev, "Mic button released\n");
274 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
275 input_report_key(info->input,
276 arizona_lvl_to_key[i].report, 0);
277 input_sync(info->input);
280 handled:
281 pm_runtime_mark_last_busy(info->dev);
282 mutex_unlock(&info->lock);
284 return IRQ_HANDLED;
287 static irqreturn_t arizona_jackdet(int irq, void *data)
289 struct arizona_extcon_info *info = data;
290 struct arizona *arizona = info->arizona;
291 unsigned int val;
292 int ret, i;
294 pm_runtime_get_sync(info->dev);
296 mutex_lock(&info->lock);
298 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
299 if (ret != 0) {
300 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
301 ret);
302 mutex_unlock(&info->lock);
303 pm_runtime_put_autosuspend(info->dev);
304 return IRQ_NONE;
307 if (val & ARIZONA_JD1_STS) {
308 dev_dbg(arizona->dev, "Detected jack\n");
309 ret = extcon_set_cable_state_(&info->edev,
310 ARIZONA_CABLE_MECHANICAL, true);
312 if (ret != 0)
313 dev_err(arizona->dev, "Mechanical report failed: %d\n",
314 ret);
316 arizona_start_mic(info);
317 } else {
318 dev_dbg(arizona->dev, "Detected jack removal\n");
320 arizona_stop_mic(info);
322 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
323 input_report_key(info->input,
324 arizona_lvl_to_key[i].report, 0);
325 input_sync(info->input);
327 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
328 if (ret != 0)
329 dev_err(arizona->dev, "Removal report failed: %d\n",
330 ret);
333 mutex_unlock(&info->lock);
335 pm_runtime_mark_last_busy(info->dev);
336 pm_runtime_put_autosuspend(info->dev);
338 return IRQ_HANDLED;
341 static int arizona_extcon_probe(struct platform_device *pdev)
343 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
344 struct arizona_pdata *pdata;
345 struct arizona_extcon_info *info;
346 int ret, mode, i;
348 pdata = dev_get_platdata(arizona->dev);
350 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
351 if (!info) {
352 dev_err(&pdev->dev, "Failed to allocate memory\n");
353 ret = -ENOMEM;
354 goto err;
357 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
358 if (IS_ERR(info->micvdd)) {
359 ret = PTR_ERR(info->micvdd);
360 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
361 goto err;
364 mutex_init(&info->lock);
365 info->arizona = arizona;
366 info->dev = &pdev->dev;
367 info->detecting = true;
368 platform_set_drvdata(pdev, info);
370 switch (arizona->type) {
371 case WM5102:
372 switch (arizona->rev) {
373 case 0:
374 info->micd_reva = true;
375 break;
376 default:
377 break;
379 break;
380 default:
381 break;
384 info->edev.name = "Headset Jack";
385 info->edev.supported_cable = arizona_cable;
387 ret = extcon_dev_register(&info->edev, arizona->dev);
388 if (ret < 0) {
389 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
390 ret);
391 goto err;
394 if (pdata->num_micd_configs) {
395 info->micd_modes = pdata->micd_configs;
396 info->micd_num_modes = pdata->num_micd_configs;
397 } else {
398 info->micd_modes = micd_default_modes;
399 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
402 if (arizona->pdata.micd_pol_gpio > 0) {
403 if (info->micd_modes[0].gpio)
404 mode = GPIOF_OUT_INIT_HIGH;
405 else
406 mode = GPIOF_OUT_INIT_LOW;
408 ret = devm_gpio_request_one(&pdev->dev,
409 arizona->pdata.micd_pol_gpio,
410 mode,
411 "MICD polarity");
412 if (ret != 0) {
413 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
414 arizona->pdata.micd_pol_gpio, ret);
415 goto err_register;
419 arizona_extcon_set_mode(info, 0);
421 info->input = input_allocate_device();
422 if (!info->input) {
423 dev_err(arizona->dev, "Can't allocate input dev\n");
424 ret = -ENOMEM;
425 goto err_register;
428 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
429 input_set_capability(info->input, EV_KEY,
430 arizona_lvl_to_key[i].report);
431 info->input->name = "Headset";
432 info->input->phys = "arizona/extcon";
433 info->input->dev.parent = &pdev->dev;
435 pm_runtime_enable(&pdev->dev);
436 pm_runtime_idle(&pdev->dev);
437 pm_runtime_get_sync(&pdev->dev);
439 ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_RISE,
440 "JACKDET rise", arizona_jackdet, info);
441 if (ret != 0) {
442 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
443 ret);
444 goto err_input;
447 ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1);
448 if (ret != 0) {
449 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
450 ret);
451 goto err_rise;
454 ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_FALL,
455 "JACKDET fall", arizona_jackdet, info);
456 if (ret != 0) {
457 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
458 goto err_rise_wake;
461 ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 1);
462 if (ret != 0) {
463 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
464 ret);
465 goto err_fall;
468 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
469 "MICDET", arizona_micdet, info);
470 if (ret != 0) {
471 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
472 goto err_fall_wake;
475 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
476 ARIZONA_MICD_BIAS_STARTTIME_MASK |
477 ARIZONA_MICD_RATE_MASK,
478 7 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT |
479 8 << ARIZONA_MICD_RATE_SHIFT);
481 arizona_clk32k_enable(arizona);
482 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
483 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
484 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
485 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
487 ret = regulator_allow_bypass(info->micvdd, true);
488 if (ret != 0)
489 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
490 ret);
492 pm_runtime_put(&pdev->dev);
494 ret = input_register_device(info->input);
495 if (ret) {
496 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
497 goto err_micdet;
500 return 0;
502 err_micdet:
503 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
504 err_fall_wake:
505 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
506 err_fall:
507 arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
508 err_rise_wake:
509 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
510 err_rise:
511 arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
512 err_input:
513 input_free_device(info->input);
514 err_register:
515 pm_runtime_disable(&pdev->dev);
516 extcon_dev_unregister(&info->edev);
517 err:
518 return ret;
521 static int arizona_extcon_remove(struct platform_device *pdev)
523 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
524 struct arizona *arizona = info->arizona;
526 pm_runtime_disable(&pdev->dev);
528 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
529 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
530 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
531 arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
532 arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
533 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
534 ARIZONA_JD1_ENA, 0);
535 arizona_clk32k_disable(arizona);
536 input_unregister_device(info->input);
537 extcon_dev_unregister(&info->edev);
539 return 0;
542 static struct platform_driver arizona_extcon_driver = {
543 .driver = {
544 .name = "arizona-extcon",
545 .owner = THIS_MODULE,
547 .probe = arizona_extcon_probe,
548 .remove = arizona_extcon_remove,
551 module_platform_driver(arizona_extcon_driver);
553 MODULE_DESCRIPTION("Arizona Extcon driver");
554 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
555 MODULE_LICENSE("GPL");
556 MODULE_ALIAS("platform:extcon-arizona");