aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/extcon
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-07-26 14:25:33 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-07-26 14:25:33 -0400
commitfa93669a1917f93b09142d4b2298329b82d7d36d (patch)
tree7f8d6dc9d1fbb086c59a799a3a7137c46fb9959e /drivers/extcon
parentb13bc8dda81c54a66a1c84e66f60b8feba659f28 (diff)
parent6791457a090d9a234a40b501c2536f0aefaeae4b (diff)
Merge tag 'driver-core-3.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core
Pull driver core changes from Greg Kroah-Hartman: "Here's the big driver core pull request for 3.6-rc1. Unlike 3.5, this kernel should be a lot tamer, with the printk changes now settled down. All we have here is some extcon driver updates, w1 driver updates, a few printk cleanups that weren't needed for 3.5, but are good to have now, and some other minor fixes/changes in the driver core. All of these have been in the linux-next releases for a while now. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>" * tag 'driver-core-3.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core: (38 commits) printk: Export struct log size and member offsets through vmcoreinfo Drivers: hv: Change the hex constant to a decimal constant driver core: don't trigger uevent after failure extcon: MAX77693: Add extcon-max77693 driver to support Maxim MAX77693 MUIC device sysfs: fail dentry revalidation after namespace change fix sysfs: fail dentry revalidation after namespace change extcon: spelling of detach in function doc extcon: arizona: Stop microphone detection if we give up on it extcon: arizona: Update cable reporting calls and split headset PM / Runtime: Do not increment device usage counts before probing kmsg - do not flush partial lines when the console is busy kmsg - export "continuation record" flag to /dev/kmsg kmsg - avoid warning for CONFIG_PRINTK=n compilations kmsg - properly print over-long continuation lines driver-core: Use kobj_to_dev instead of re-implementing it driver-core: Move kobj_to_dev from genhd.h to device.h driver core: Move deferred devices to the end of dpm_list before probing driver core: move uevent call to driver_register driver core: fix shutdown races with probe/remove(v3) Extcon: Arizona: Add driver for Wolfson Arizona class devices ...
Diffstat (limited to 'drivers/extcon')
-rw-r--r--drivers/extcon/Kconfig18
-rw-r--r--drivers/extcon/Makefile2
-rw-r--r--drivers/extcon/extcon-arizona.c490
-rw-r--r--drivers/extcon/extcon-max77693.c779
-rw-r--r--drivers/extcon/extcon_class.c2
-rw-r--r--drivers/extcon/extcon_gpio.c17
6 files changed, 1295 insertions, 13 deletions
diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
index 29c5cf852efc..16716356d1fe 100644
--- a/drivers/extcon/Kconfig
+++ b/drivers/extcon/Kconfig
@@ -21,6 +21,16 @@ config EXTCON_GPIO
21 Say Y here to enable GPIO based extcon support. Note that GPIO 21 Say Y here to enable GPIO based extcon support. Note that GPIO
22 extcon supports single state per extcon instance. 22 extcon supports single state per extcon instance.
23 23
24config EXTCON_MAX77693
25 tristate "MAX77693 EXTCON Support"
26 depends on MFD_MAX77693
27 select IRQ_DOMAIN
28 select REGMAP_I2C
29 help
30 If you say yes here you get support for the MUIC device of
31 Maxim MAX77693 PMIC. The MAX77693 MUIC is a USB port accessory
32 detector and switch.
33
24config EXTCON_MAX8997 34config EXTCON_MAX8997
25 tristate "MAX8997 EXTCON Support" 35 tristate "MAX8997 EXTCON Support"
26 depends on MFD_MAX8997 36 depends on MFD_MAX8997
@@ -29,4 +39,12 @@ config EXTCON_MAX8997
29 Maxim MAX8997 PMIC. The MAX8997 MUIC is a USB port accessory 39 Maxim MAX8997 PMIC. The MAX8997 MUIC is a USB port accessory
30 detector and switch. 40 detector and switch.
31 41
42config EXTCON_ARIZONA
43 tristate "Wolfson Arizona EXTCON support"
44 depends on MFD_ARIZONA
45 help
46 Say Y here to enable support for external accessory detection
47 with Wolfson Arizona devices. These are audio CODECs with
48 advanced audio accessory detection support.
49
32endif # MULTISTATE_SWITCH 50endif # MULTISTATE_SWITCH
diff --git a/drivers/extcon/Makefile b/drivers/extcon/Makefile
index 86020bdb6da0..88961b332348 100644
--- a/drivers/extcon/Makefile
+++ b/drivers/extcon/Makefile
@@ -4,4 +4,6 @@
4 4
5obj-$(CONFIG_EXTCON) += extcon_class.o 5obj-$(CONFIG_EXTCON) += extcon_class.o
6obj-$(CONFIG_EXTCON_GPIO) += extcon_gpio.o 6obj-$(CONFIG_EXTCON_GPIO) += extcon_gpio.o
7obj-$(CONFIG_EXTCON_MAX77693) += extcon-max77693.o
7obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o 8obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o
9obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o
diff --git a/drivers/extcon/extcon-arizona.c b/drivers/extcon/extcon-arizona.c
new file mode 100644
index 000000000000..427a289f32a5
--- /dev/null
+++ b/drivers/extcon/extcon-arizona.c
@@ -0,0 +1,490 @@
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.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/regulator/consumer.h>
27#include <linux/extcon.h>
28
29#include <linux/mfd/arizona/core.h>
30#include <linux/mfd/arizona/pdata.h>
31#include <linux/mfd/arizona/registers.h>
32
33struct arizona_extcon_info {
34 struct device *dev;
35 struct arizona *arizona;
36 struct mutex lock;
37 struct regulator *micvdd;
38
39 int micd_mode;
40 const struct arizona_micd_config *micd_modes;
41 int micd_num_modes;
42
43 bool micd_reva;
44
45 bool mic;
46 bool detecting;
47 int jack_flips;
48
49 struct extcon_dev edev;
50};
51
52static const struct arizona_micd_config micd_default_modes[] = {
53 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
54 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
55};
56
57#define ARIZONA_CABLE_MECHANICAL 0
58#define ARIZONA_CABLE_MICROPHONE 1
59#define ARIZONA_CABLE_HEADPHONE 2
60
61static const char *arizona_cable[] = {
62 "Mechanical",
63 "Microphone",
64 "Headphone",
65 NULL,
66};
67
68static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
69{
70 struct arizona *arizona = info->arizona;
71
72 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
73 info->micd_modes[mode].gpio);
74 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
75 ARIZONA_MICD_BIAS_SRC_MASK,
76 info->micd_modes[mode].bias);
77 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
78 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
79
80 info->micd_mode = mode;
81
82 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
83}
84
85static void arizona_start_mic(struct arizona_extcon_info *info)
86{
87 struct arizona *arizona = info->arizona;
88 bool change;
89 int ret;
90
91 info->detecting = true;
92 info->mic = false;
93 info->jack_flips = 0;
94
95 /* Microphone detection can't use idle mode */
96 pm_runtime_get(info->dev);
97
98 ret = regulator_enable(info->micvdd);
99 if (ret != 0) {
100 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
101 ret);
102 }
103
104 if (info->micd_reva) {
105 regmap_write(arizona->regmap, 0x80, 0x3);
106 regmap_write(arizona->regmap, 0x294, 0);
107 regmap_write(arizona->regmap, 0x80, 0x0);
108 }
109
110 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
111 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
112 &change);
113 if (!change) {
114 regulator_disable(info->micvdd);
115 pm_runtime_put_autosuspend(info->dev);
116 }
117}
118
119static void arizona_stop_mic(struct arizona_extcon_info *info)
120{
121 struct arizona *arizona = info->arizona;
122 bool change;
123
124 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
125 ARIZONA_MICD_ENA, 0,
126 &change);
127
128 if (info->micd_reva) {
129 regmap_write(arizona->regmap, 0x80, 0x3);
130 regmap_write(arizona->regmap, 0x294, 2);
131 regmap_write(arizona->regmap, 0x80, 0x0);
132 }
133
134 if (change) {
135 regulator_disable(info->micvdd);
136 pm_runtime_put_autosuspend(info->dev);
137 }
138}
139
140static irqreturn_t arizona_micdet(int irq, void *data)
141{
142 struct arizona_extcon_info *info = data;
143 struct arizona *arizona = info->arizona;
144 unsigned int val;
145 int ret;
146
147 mutex_lock(&info->lock);
148
149 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
150 if (ret != 0) {
151 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
152 return IRQ_NONE;
153 }
154
155 dev_dbg(arizona->dev, "MICDET: %x\n", val);
156
157 if (!(val & ARIZONA_MICD_VALID)) {
158 dev_warn(arizona->dev, "Microphone detection state invalid\n");
159 mutex_unlock(&info->lock);
160 return IRQ_NONE;
161 }
162
163 /* Due to jack detect this should never happen */
164 if (!(val & ARIZONA_MICD_STS)) {
165 dev_warn(arizona->dev, "Detected open circuit\n");
166 info->detecting = false;
167 goto handled;
168 }
169
170 /* If we got a high impedence we should have a headset, report it. */
171 if (info->detecting && (val & 0x400)) {
172 ret = extcon_update_state(&info->edev,
173 1 << ARIZONA_CABLE_MICROPHONE |
174 1 << ARIZONA_CABLE_HEADPHONE,
175 1 << ARIZONA_CABLE_MICROPHONE |
176 1 << ARIZONA_CABLE_HEADPHONE);
177
178 if (ret != 0)
179 dev_err(arizona->dev, "Headset report failed: %d\n",
180 ret);
181
182 info->mic = true;
183 info->detecting = false;
184 goto handled;
185 }
186
187 /* If we detected a lower impedence during initial startup
188 * then we probably have the wrong polarity, flip it. Don't
189 * do this for the lowest impedences to speed up detection of
190 * plain headphones. If both polarities report a low
191 * impedence then give up and report headphones.
192 */
193 if (info->detecting && (val & 0x3f8)) {
194 info->jack_flips++;
195
196 if (info->jack_flips >= info->micd_num_modes) {
197 dev_dbg(arizona->dev, "Detected headphone\n");
198 info->detecting = false;
199 arizona_stop_mic(info);
200
201 ret = extcon_set_cable_state_(&info->edev,
202 ARIZONA_CABLE_HEADPHONE,
203 true);
204 if (ret != 0)
205 dev_err(arizona->dev,
206 "Headphone report failed: %d\n",
207 ret);
208 } else {
209 info->micd_mode++;
210 if (info->micd_mode == info->micd_num_modes)
211 info->micd_mode = 0;
212 arizona_extcon_set_mode(info, info->micd_mode);
213
214 info->jack_flips++;
215 }
216
217 goto handled;
218 }
219
220 /*
221 * If we're still detecting and we detect a short then we've
222 * got a headphone. Otherwise it's a button press, the
223 * button reporting is stubbed out for now.
224 */
225 if (val & 0x3fc) {
226 if (info->mic) {
227 dev_dbg(arizona->dev, "Mic button detected\n");
228
229 } else if (info->detecting) {
230 dev_dbg(arizona->dev, "Headphone detected\n");
231 info->detecting = false;
232 arizona_stop_mic(info);
233
234 ret = extcon_set_cable_state_(&info->edev,
235 ARIZONA_CABLE_HEADPHONE,
236 true);
237 if (ret != 0)
238 dev_err(arizona->dev,
239 "Headphone report failed: %d\n",
240 ret);
241 } else {
242 dev_warn(arizona->dev, "Button with no mic: %x\n",
243 val);
244 }
245 } else {
246 dev_dbg(arizona->dev, "Mic button released\n");
247 }
248
249handled:
250 pm_runtime_mark_last_busy(info->dev);
251 mutex_unlock(&info->lock);
252
253 return IRQ_HANDLED;
254}
255
256static irqreturn_t arizona_jackdet(int irq, void *data)
257{
258 struct arizona_extcon_info *info = data;
259 struct arizona *arizona = info->arizona;
260 unsigned int val;
261 int ret;
262
263 pm_runtime_get_sync(info->dev);
264
265 mutex_lock(&info->lock);
266
267 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
268 if (ret != 0) {
269 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
270 ret);
271 mutex_unlock(&info->lock);
272 pm_runtime_put_autosuspend(info->dev);
273 return IRQ_NONE;
274 }
275
276 if (val & ARIZONA_JD1_STS) {
277 dev_dbg(arizona->dev, "Detected jack\n");
278 ret = extcon_set_cable_state_(&info->edev,
279 ARIZONA_CABLE_MECHANICAL, true);
280
281 if (ret != 0)
282 dev_err(arizona->dev, "Mechanical report failed: %d\n",
283 ret);
284
285 arizona_start_mic(info);
286 } else {
287 dev_dbg(arizona->dev, "Detected jack removal\n");
288
289 arizona_stop_mic(info);
290
291 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
292 if (ret != 0)
293 dev_err(arizona->dev, "Removal report failed: %d\n",
294 ret);
295 }
296
297 mutex_unlock(&info->lock);
298
299 pm_runtime_mark_last_busy(info->dev);
300 pm_runtime_put_autosuspend(info->dev);
301
302 return IRQ_HANDLED;
303}
304
305static int __devinit arizona_extcon_probe(struct platform_device *pdev)
306{
307 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
308 struct arizona_pdata *pdata;
309 struct arizona_extcon_info *info;
310 int ret, mode;
311
312 pdata = dev_get_platdata(arizona->dev);
313
314 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
315 if (!info) {
316 dev_err(&pdev->dev, "failed to allocate memory\n");
317 ret = -ENOMEM;
318 goto err;
319 }
320
321 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
322 if (IS_ERR(info->micvdd)) {
323 ret = PTR_ERR(info->micvdd);
324 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
325 goto err;
326 }
327
328 mutex_init(&info->lock);
329 info->arizona = arizona;
330 info->dev = &pdev->dev;
331 info->detecting = true;
332 platform_set_drvdata(pdev, info);
333
334 switch (arizona->type) {
335 case WM5102:
336 switch (arizona->rev) {
337 case 0:
338 info->micd_reva = true;
339 break;
340 default:
341 break;
342 }
343 break;
344 default:
345 break;
346 }
347
348 info->edev.name = "Headset Jack";
349 info->edev.supported_cable = arizona_cable;
350
351 ret = extcon_dev_register(&info->edev, arizona->dev);
352 if (ret < 0) {
353 dev_err(arizona->dev, "extcon_dev_regster() failed: %d\n",
354 ret);
355 goto err;
356 }
357
358 if (pdata->num_micd_configs) {
359 info->micd_modes = pdata->micd_configs;
360 info->micd_num_modes = pdata->num_micd_configs;
361 } else {
362 info->micd_modes = micd_default_modes;
363 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
364 }
365
366 if (arizona->pdata.micd_pol_gpio > 0) {
367 if (info->micd_modes[0].gpio)
368 mode = GPIOF_OUT_INIT_HIGH;
369 else
370 mode = GPIOF_OUT_INIT_LOW;
371
372 ret = devm_gpio_request_one(&pdev->dev,
373 arizona->pdata.micd_pol_gpio,
374 mode,
375 "MICD polarity");
376 if (ret != 0) {
377 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
378 arizona->pdata.micd_pol_gpio, ret);
379 goto err_register;
380 }
381 }
382
383 arizona_extcon_set_mode(info, 0);
384
385 pm_runtime_enable(&pdev->dev);
386 pm_runtime_idle(&pdev->dev);
387 pm_runtime_get_sync(&pdev->dev);
388
389 ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_RISE,
390 "JACKDET rise", arizona_jackdet, info);
391 if (ret != 0) {
392 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
393 ret);
394 goto err_register;
395 }
396
397 ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1);
398 if (ret != 0) {
399 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
400 ret);
401 goto err_rise;
402 }
403
404 ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_FALL,
405 "JACKDET fall", arizona_jackdet, info);
406 if (ret != 0) {
407 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
408 goto err_rise_wake;
409 }
410
411 ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 1);
412 if (ret != 0) {
413 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
414 ret);
415 goto err_fall;
416 }
417
418 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
419 "MICDET", arizona_micdet, info);
420 if (ret != 0) {
421 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
422 goto err_fall_wake;
423 }
424
425 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
426 ARIZONA_MICD_BIAS_STARTTIME_MASK |
427 ARIZONA_MICD_RATE_MASK,
428 7 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT |
429 8 << ARIZONA_MICD_RATE_SHIFT);
430
431 arizona_clk32k_enable(arizona);
432 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
433 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
434 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
435 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
436
437 pm_runtime_put(&pdev->dev);
438
439 return 0;
440
441err_fall_wake:
442 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
443err_fall:
444 arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
445err_rise_wake:
446 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
447err_rise:
448 arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
449err_register:
450 pm_runtime_disable(&pdev->dev);
451 extcon_dev_unregister(&info->edev);
452err:
453 return ret;
454}
455
456static int __devexit arizona_extcon_remove(struct platform_device *pdev)
457{
458 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
459 struct arizona *arizona = info->arizona;
460
461 pm_runtime_disable(&pdev->dev);
462
463 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
464 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
465 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
466 arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
467 arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
468 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
469 ARIZONA_JD1_ENA, 0);
470 arizona_clk32k_disable(arizona);
471 extcon_dev_unregister(&info->edev);
472
473 return 0;
474}
475
476static struct platform_driver arizona_extcon_driver = {
477 .driver = {
478 .name = "arizona-extcon",
479 .owner = THIS_MODULE,
480 },
481 .probe = arizona_extcon_probe,
482 .remove = __devexit_p(arizona_extcon_remove),
483};
484
485module_platform_driver(arizona_extcon_driver);
486
487MODULE_DESCRIPTION("Arizona Extcon driver");
488MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
489MODULE_LICENSE("GPL");
490MODULE_ALIAS("platform:extcon-arizona");
diff --git a/drivers/extcon/extcon-max77693.c b/drivers/extcon/extcon-max77693.c
new file mode 100644
index 000000000000..920a609b2c35
--- /dev/null
+++ b/drivers/extcon/extcon-max77693.c
@@ -0,0 +1,779 @@
1/*
2 * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
3 *
4 * Copyright (C) 2012 Samsung Electrnoics
5 * Chanwoo Choi <cw00.choi@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/slab.h>
22#include <linux/interrupt.h>
23#include <linux/err.h>
24#include <linux/platform_device.h>
25#include <linux/mfd/max77693.h>
26#include <linux/mfd/max77693-private.h>
27#include <linux/extcon.h>
28#include <linux/regmap.h>
29#include <linux/irqdomain.h>
30
31#define DEV_NAME "max77693-muic"
32
33/* MAX77693 MUIC - STATUS1~3 Register */
34#define STATUS1_ADC_SHIFT (0)
35#define STATUS1_ADCLOW_SHIFT (5)
36#define STATUS1_ADCERR_SHIFT (6)
37#define STATUS1_ADC1K_SHIFT (7)
38#define STATUS1_ADC_MASK (0x1f << STATUS1_ADC_SHIFT)
39#define STATUS1_ADCLOW_MASK (0x1 << STATUS1_ADCLOW_SHIFT)
40#define STATUS1_ADCERR_MASK (0x1 << STATUS1_ADCERR_SHIFT)
41#define STATUS1_ADC1K_MASK (0x1 << STATUS1_ADC1K_SHIFT)
42
43#define STATUS2_CHGTYP_SHIFT (0)
44#define STATUS2_CHGDETRUN_SHIFT (3)
45#define STATUS2_DCDTMR_SHIFT (4)
46#define STATUS2_DXOVP_SHIFT (5)
47#define STATUS2_VBVOLT_SHIFT (6)
48#define STATUS2_VIDRM_SHIFT (7)
49#define STATUS2_CHGTYP_MASK (0x7 << STATUS2_CHGTYP_SHIFT)
50#define STATUS2_CHGDETRUN_MASK (0x1 << STATUS2_CHGDETRUN_SHIFT)
51#define STATUS2_DCDTMR_MASK (0x1 << STATUS2_DCDTMR_SHIFT)
52#define STATUS2_DXOVP_MASK (0x1 << STATUS2_DXOVP_SHIFT)
53#define STATUS2_VBVOLT_MASK (0x1 << STATUS2_VBVOLT_SHIFT)
54#define STATUS2_VIDRM_MASK (0x1 << STATUS2_VIDRM_SHIFT)
55
56#define STATUS3_OVP_SHIFT (2)
57#define STATUS3_OVP_MASK (0x1 << STATUS3_OVP_SHIFT)
58
59/* MAX77693 CDETCTRL1~2 register */
60#define CDETCTRL1_CHGDETEN_SHIFT (0)
61#define CDETCTRL1_CHGTYPMAN_SHIFT (1)
62#define CDETCTRL1_DCDEN_SHIFT (2)
63#define CDETCTRL1_DCD2SCT_SHIFT (3)
64#define CDETCTRL1_CDDELAY_SHIFT (4)
65#define CDETCTRL1_DCDCPL_SHIFT (5)
66#define CDETCTRL1_CDPDET_SHIFT (7)
67#define CDETCTRL1_CHGDETEN_MASK (0x1 << CDETCTRL1_CHGDETEN_SHIFT)
68#define CDETCTRL1_CHGTYPMAN_MASK (0x1 << CDETCTRL1_CHGTYPMAN_SHIFT)
69#define CDETCTRL1_DCDEN_MASK (0x1 << CDETCTRL1_DCDEN_SHIFT)
70#define CDETCTRL1_DCD2SCT_MASK (0x1 << CDETCTRL1_DCD2SCT_SHIFT)
71#define CDETCTRL1_CDDELAY_MASK (0x1 << CDETCTRL1_CDDELAY_SHIFT)
72#define CDETCTRL1_DCDCPL_MASK (0x1 << CDETCTRL1_DCDCPL_SHIFT)
73#define CDETCTRL1_CDPDET_MASK (0x1 << CDETCTRL1_CDPDET_SHIFT)
74
75#define CDETCTRL2_VIDRMEN_SHIFT (1)
76#define CDETCTRL2_DXOVPEN_SHIFT (3)
77#define CDETCTRL2_VIDRMEN_MASK (0x1 << CDETCTRL2_VIDRMEN_SHIFT)
78#define CDETCTRL2_DXOVPEN_MASK (0x1 << CDETCTRL2_DXOVPEN_SHIFT)
79
80/* MAX77693 MUIC - CONTROL1~3 register */
81#define COMN1SW_SHIFT (0)
82#define COMP2SW_SHIFT (3)
83#define COMN1SW_MASK (0x7 << COMN1SW_SHIFT)
84#define COMP2SW_MASK (0x7 << COMP2SW_SHIFT)
85#define COMP_SW_MASK (COMP2SW_MASK | COMN1SW_MASK)
86#define CONTROL1_SW_USB ((1 << COMP2SW_SHIFT) \
87 | (1 << COMN1SW_SHIFT))
88#define CONTROL1_SW_AUDIO ((2 << COMP2SW_SHIFT) \
89 | (2 << COMN1SW_SHIFT))
90#define CONTROL1_SW_UART ((3 << COMP2SW_SHIFT) \
91 | (3 << COMN1SW_SHIFT))
92#define CONTROL1_SW_OPEN ((0 << COMP2SW_SHIFT) \
93 | (0 << COMN1SW_SHIFT))
94
95#define CONTROL2_LOWPWR_SHIFT (0)
96#define CONTROL2_ADCEN_SHIFT (1)
97#define CONTROL2_CPEN_SHIFT (2)
98#define CONTROL2_SFOUTASRT_SHIFT (3)
99#define CONTROL2_SFOUTORD_SHIFT (4)
100#define CONTROL2_ACCDET_SHIFT (5)
101#define CONTROL2_USBCPINT_SHIFT (6)
102#define CONTROL2_RCPS_SHIFT (7)
103#define CONTROL2_LOWPWR_MASK (0x1 << CONTROL2_LOWPWR_SHIFT)
104#define CONTROL2_ADCEN_MASK (0x1 << CONTROL2_ADCEN_SHIFT)
105#define CONTROL2_CPEN_MASK (0x1 << CONTROL2_CPEN_SHIFT)
106#define CONTROL2_SFOUTASRT_MASK (0x1 << CONTROL2_SFOUTASRT_SHIFT)
107#define CONTROL2_SFOUTORD_MASK (0x1 << CONTROL2_SFOUTORD_SHIFT)
108#define CONTROL2_ACCDET_MASK (0x1 << CONTROL2_ACCDET_SHIFT)
109#define CONTROL2_USBCPINT_MASK (0x1 << CONTROL2_USBCPINT_SHIFT)
110#define CONTROL2_RCPS_MASK (0x1 << CONTROL2_RCPS_SHIFT)
111
112#define CONTROL3_JIGSET_SHIFT (0)
113#define CONTROL3_BTLDSET_SHIFT (2)
114#define CONTROL3_ADCDBSET_SHIFT (4)
115#define CONTROL3_JIGSET_MASK (0x3 << CONTROL3_JIGSET_SHIFT)
116#define CONTROL3_BTLDSET_MASK (0x3 << CONTROL3_BTLDSET_SHIFT)
117#define CONTROL3_ADCDBSET_MASK (0x3 << CONTROL3_ADCDBSET_SHIFT)
118
119enum max77693_muic_adc_debounce_time {
120 ADC_DEBOUNCE_TIME_5MS = 0,
121 ADC_DEBOUNCE_TIME_10MS,
122 ADC_DEBOUNCE_TIME_25MS,
123 ADC_DEBOUNCE_TIME_38_62MS,
124};
125
126struct max77693_muic_info {
127 struct device *dev;
128 struct max77693_dev *max77693;
129 struct extcon_dev *edev;
130 int prev_adc;
131 int prev_adc_gnd;
132 int prev_chg_type;
133 u8 status[2];
134
135 int irq;
136 struct work_struct irq_work;
137 struct mutex mutex;
138};
139
140enum max77693_muic_charger_type {
141 MAX77693_CHARGER_TYPE_NONE = 0,
142 MAX77693_CHARGER_TYPE_USB,
143 MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
144 MAX77693_CHARGER_TYPE_DEDICATED_CHG,
145 MAX77693_CHARGER_TYPE_APPLE_500MA,
146 MAX77693_CHARGER_TYPE_APPLE_1A_2A,
147 MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
148};
149
150/**
151 * struct max77693_muic_irq
152 * @irq: the index of irq list of MUIC device.
153 * @name: the name of irq.
154 * @virq: the virtual irq to use irq domain
155 */
156struct max77693_muic_irq {
157 unsigned int irq;
158 const char *name;
159 unsigned int virq;
160};
161
162static struct max77693_muic_irq muic_irqs[] = {
163 { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" },
164 { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" },
165 { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" },
166 { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" },
167 { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
168 { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" },
169 { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
170 { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" },
171 { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
172 { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" },
173 { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" },
174 { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" },
175 { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" },
176 { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" },
177 { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" },
178 { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" },
179};
180
181/* Define supported accessory type */
182enum max77693_muic_acc_type {
183 MAX77693_MUIC_ADC_GROUND = 0x0,
184 MAX77693_MUIC_ADC_SEND_END_BUTTON,
185 MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
186 MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
187 MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
188 MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
189 MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
190 MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
191 MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
192 MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
193 MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
194 MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
195 MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
196 MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
197 MAX77693_MUIC_ADC_RESERVED_ACC_1,
198 MAX77693_MUIC_ADC_RESERVED_ACC_2,
199 MAX77693_MUIC_ADC_RESERVED_ACC_3,
200 MAX77693_MUIC_ADC_RESERVED_ACC_4,
201 MAX77693_MUIC_ADC_RESERVED_ACC_5,
202 MAX77693_MUIC_ADC_CEA936_AUDIO,
203 MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
204 MAX77693_MUIC_ADC_TTY_CONVERTER,
205 MAX77693_MUIC_ADC_UART_CABLE,
206 MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
207 MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
208 MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
209 MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
210 MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
211 MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
212 MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
213 MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
214 MAX77693_MUIC_ADC_OPEN,
215
216 /* The below accessories have same ADC value so ADCLow and
217 ADC1K bit is used to separate specific accessory */
218 MAX77693_MUIC_GND_USB_OTG = 0x100, /* ADC:0x0, ADCLow:0, ADC1K:0 */
219 MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, ADCLow:1, ADC1K:0 */
220 MAX77693_MUIC_GND_MHL_CABLE = 0x103, /* ADC:0x0, ADCLow:1, ADC1K:1 */
221};
222
223/* MAX77693 MUIC device support below list of accessories(external connector) */
224const char *max77693_extcon_cable[] = {
225 [0] = "USB",
226 [1] = "USB-Host",
227 [2] = "TA",
228 [3] = "Fast-charger",
229 [4] = "Slow-charger",
230 [5] = "Charge-downstream",
231 [6] = "MHL",
232 [7] = "Audio-video-load",
233 [8] = "Audio-video-noload",
234 [9] = "JIG",
235
236 NULL,
237};
238
239static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
240 enum max77693_muic_adc_debounce_time time)
241{
242 int ret = 0;
243 u8 ctrl3;
244
245 switch (time) {
246 case ADC_DEBOUNCE_TIME_5MS:
247 case ADC_DEBOUNCE_TIME_10MS:
248 case ADC_DEBOUNCE_TIME_25MS:
249 case ADC_DEBOUNCE_TIME_38_62MS:
250 ret = max77693_read_reg(info->max77693->regmap_muic,
251 MAX77693_MUIC_REG_CTRL3, &ctrl3);
252 ctrl3 &= ~CONTROL3_ADCDBSET_MASK;
253 ctrl3 |= (time << CONTROL3_ADCDBSET_SHIFT);
254
255 ret = max77693_write_reg(info->max77693->regmap_muic,
256 MAX77693_MUIC_REG_CTRL3, ctrl3);
257 if (ret) {
258 dev_err(info->dev, "failed to set ADC debounce time\n");
259 ret = -EINVAL;
260 }
261 break;
262 default:
263 dev_err(info->dev, "invalid ADC debounce time\n");
264 ret = -EINVAL;
265 break;
266 }
267
268 return ret;
269};
270
271static int max77693_muic_set_path(struct max77693_muic_info *info,
272 u8 val, bool attached)
273{
274 int ret = 0;
275 u8 ctrl1, ctrl2 = 0;
276
277 if (attached)
278 ctrl1 = val;
279 else
280 ctrl1 = CONTROL1_SW_OPEN;
281
282 ret = max77693_update_reg(info->max77693->regmap_muic,
283 MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
284 if (ret < 0) {
285 dev_err(info->dev, "failed to update MUIC register\n");
286 goto out;
287 }
288
289 if (attached)
290 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
291 else
292 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
293
294 ret = max77693_update_reg(info->max77693->regmap_muic,
295 MAX77693_MUIC_REG_CTRL2, ctrl2,
296 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
297 if (ret < 0) {
298 dev_err(info->dev, "failed to update MUIC register\n");
299 goto out;
300 }
301
302 dev_info(info->dev,
303 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
304 ctrl1, ctrl2, attached ? "attached" : "detached");
305out:
306 return ret;
307}
308
309static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info,
310 bool attached)
311{
312 int ret = 0;
313 int type;
314 int adc, adc1k, adclow;
315
316 if (attached) {
317 adc = info->status[0] & STATUS1_ADC_MASK;
318 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
319 adclow >>= STATUS1_ADCLOW_SHIFT;
320 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
321 adc1k >>= STATUS1_ADC1K_SHIFT;
322
323 /**
324 * [0x1][ADCLow][ADC1K]
325 * [0x1 0 0 ] : USB_OTG
326 * [0x1 1 0 ] : Audio Video Cable with load
327 * [0x1 1 1 ] : MHL
328 */
329 type = ((0x1 << 8) | (adclow << 1) | adc1k);
330
331 /* Store previous ADC value to handle accessory
332 when accessory will be detached */
333 info->prev_adc = adc;
334 info->prev_adc_gnd = type;
335 } else
336 type = info->prev_adc_gnd;
337
338 switch (type) {
339 case MAX77693_MUIC_GND_USB_OTG:
340 /* USB_OTG */
341 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
342 if (ret < 0)
343 goto out;
344 extcon_set_cable_state(info->edev, "USB-Host", attached);
345 break;
346 case MAX77693_MUIC_GND_AV_CABLE_LOAD:
347 /* Audio Video Cable with load */
348 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
349 if (ret < 0)
350 goto out;
351 extcon_set_cable_state(info->edev,
352 "Audio-video-load", attached);
353 break;
354 case MAX77693_MUIC_GND_MHL_CABLE:
355 /* MHL */
356 extcon_set_cable_state(info->edev, "MHL", attached);
357 break;
358 default:
359 dev_err(info->dev, "faild to detect %s accessory\n",
360 attached ? "attached" : "detached");
361 dev_err(info->dev, "- adc:0x%x, adclow:0x%x, adc1k:0x%x\n",
362 adc, adclow, adc1k);
363 ret = -EINVAL;
364 break;
365 }
366
367out:
368 return ret;
369}
370
371static int max77693_muic_adc_handler(struct max77693_muic_info *info,
372 int curr_adc, bool attached)
373{
374 int ret = 0;
375 int adc;
376
377 if (attached) {
378 /* Store ADC value to handle accessory
379 when accessory will be detached */
380 info->prev_adc = curr_adc;
381 adc = curr_adc;
382 } else
383 adc = info->prev_adc;
384
385 dev_info(info->dev,
386 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
387 attached ? "attached" : "detached", curr_adc, info->prev_adc);
388
389 switch (adc) {
390 case MAX77693_MUIC_ADC_GROUND:
391 /* USB_OTG/MHL/Audio */
392 max77693_muic_adc_ground_handler(info, attached);
393 break;
394 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
395 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
396 /* USB */
397 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
398 if (ret < 0)
399 goto out;
400 extcon_set_cable_state(info->edev, "USB", attached);
401 break;
402 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
403 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:
404 /* JIG */
405 ret = max77693_muic_set_path(info, CONTROL1_SW_UART, attached);
406 if (ret < 0)
407 goto out;
408 extcon_set_cable_state(info->edev, "JIG", attached);
409 break;
410 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:
411 /* Audio Video cable with no-load */
412 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
413 if (ret < 0)
414 goto out;
415 extcon_set_cable_state(info->edev,
416 "Audio-video-noload", attached);
417 break;
418 case MAX77693_MUIC_ADC_SEND_END_BUTTON:
419 case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
420 case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
421 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON:
422 case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
423 case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
424 case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
425 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON:
426 case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
427 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
428 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
429 case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
430 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON:
431 case MAX77693_MUIC_ADC_RESERVED_ACC_1:
432 case MAX77693_MUIC_ADC_RESERVED_ACC_2:
433 case MAX77693_MUIC_ADC_RESERVED_ACC_3:
434 case MAX77693_MUIC_ADC_RESERVED_ACC_4:
435 case MAX77693_MUIC_ADC_RESERVED_ACC_5:
436 case MAX77693_MUIC_ADC_CEA936_AUDIO:
437 case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
438 case MAX77693_MUIC_ADC_TTY_CONVERTER:
439 case MAX77693_MUIC_ADC_UART_CABLE:
440 case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
441 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:
442 case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
443 /* This accessory isn't used in general case if it is specially
444 needed to detect additional accessory, should implement
445 proper operation when this accessory is attached/detached. */
446 dev_info(info->dev,
447 "accessory is %s but it isn't used (adc:0x%x)\n",
448 attached ? "attached" : "detached", adc);
449 goto out;
450 default:
451 dev_err(info->dev,
452 "failed to detect %s accessory (adc:0x%x)\n",
453 attached ? "attached" : "detached", adc);
454 ret = -EINVAL;
455 goto out;
456 }
457
458out:
459 return ret;
460}
461
462static int max77693_muic_chg_handler(struct max77693_muic_info *info,
463 int curr_chg_type, bool attached)
464{
465 int ret = 0;
466 int chg_type;
467
468 if (attached) {
469 /* Store previous charger type to control
470 when charger accessory will be detached */
471 info->prev_chg_type = curr_chg_type;
472 chg_type = curr_chg_type;
473 } else
474 chg_type = info->prev_chg_type;
475
476 dev_info(info->dev,
477 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
478 attached ? "attached" : "detached",
479 curr_chg_type, info->prev_chg_type);
480
481 switch (chg_type) {
482 case MAX77693_CHARGER_TYPE_USB:
483 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
484 if (ret < 0)
485 goto out;
486 extcon_set_cable_state(info->edev, "USB", attached);
487 break;
488 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
489 extcon_set_cable_state(info->edev,
490 "Charge-downstream", attached);
491 break;
492 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
493 extcon_set_cable_state(info->edev, "TA", attached);
494 break;
495 case MAX77693_CHARGER_TYPE_APPLE_500MA:
496 extcon_set_cable_state(info->edev, "Slow-charger", attached);
497 break;
498 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
499 extcon_set_cable_state(info->edev, "Fast-charger", attached);
500 break;
501 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
502 break;
503 default:
504 dev_err(info->dev,
505 "failed to detect %s accessory (chg_type:0x%x)\n",
506 attached ? "attached" : "detached", chg_type);
507 ret = -EINVAL;
508 goto out;
509 }
510
511out:
512 return ret;
513}
514
515static void max77693_muic_irq_work(struct work_struct *work)
516{
517 struct max77693_muic_info *info = container_of(work,
518 struct max77693_muic_info, irq_work);
519 int curr_adc, curr_chg_type;
520 int irq_type = -1;
521 int i, ret = 0;
522 bool attached = true;
523
524 if (!info->edev)
525 return;
526
527 mutex_lock(&info->mutex);
528
529 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
530 if (info->irq == muic_irqs[i].virq)
531 irq_type = muic_irqs[i].irq;
532
533 ret = max77693_bulk_read(info->max77693->regmap_muic,
534 MAX77693_MUIC_REG_STATUS1, 2, info->status);
535 if (ret) {
536 dev_err(info->dev, "failed to read MUIC register\n");
537 mutex_unlock(&info->mutex);
538 return;
539 }
540
541 switch (irq_type) {
542 case MAX77693_MUIC_IRQ_INT1_ADC:
543 case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
544 case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
545 case MAX77693_MUIC_IRQ_INT1_ADC1K:
546 /* Handle all of accessory except for
547 type of charger accessory */
548 curr_adc = info->status[0] & STATUS1_ADC_MASK;
549 curr_adc >>= STATUS1_ADC_SHIFT;
550
551 /* Check accossory state which is either detached or attached */
552 if (curr_adc == MAX77693_MUIC_ADC_OPEN)
553 attached = false;
554
555 ret = max77693_muic_adc_handler(info, curr_adc, attached);
556 break;
557 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
558 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
559 case MAX77693_MUIC_IRQ_INT2_DCDTMR:
560 case MAX77693_MUIC_IRQ_INT2_DXOVP:
561 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
562 case MAX77693_MUIC_IRQ_INT2_VIDRM:
563 /* Handle charger accessory */
564 curr_chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
565 curr_chg_type >>= STATUS2_CHGTYP_SHIFT;
566
567 /* Check charger accossory state which
568 is either detached or attached */
569 if (curr_chg_type == MAX77693_CHARGER_TYPE_NONE)
570 attached = false;
571
572 ret = max77693_muic_chg_handler(info, curr_chg_type, attached);
573 break;
574 case MAX77693_MUIC_IRQ_INT3_EOC:
575 case MAX77693_MUIC_IRQ_INT3_CGMBC:
576 case MAX77693_MUIC_IRQ_INT3_OVP:
577 case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
578 case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
579 case MAX77693_MUIC_IRQ_INT3_BAT_DET:
580 break;
581 default:
582 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
583 irq_type);
584 break;
585 }
586
587 if (ret < 0)
588 dev_err(info->dev, "failed to handle MUIC interrupt\n");
589
590 mutex_unlock(&info->mutex);
591
592 return;
593}
594
595static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
596{
597 struct max77693_muic_info *info = data;
598
599 info->irq = irq;
600 schedule_work(&info->irq_work);
601
602 return IRQ_HANDLED;
603}
604
605static struct regmap_config max77693_muic_regmap_config = {
606 .reg_bits = 8,
607 .val_bits = 8,
608};
609
610static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
611{
612 int ret = 0;
613 int adc, chg_type;
614
615 mutex_lock(&info->mutex);
616
617 /* Read STATUSx register to detect accessory */
618 ret = max77693_bulk_read(info->max77693->regmap_muic,
619 MAX77693_MUIC_REG_STATUS1, 2, info->status);
620 if (ret) {
621 dev_err(info->dev, "failed to read MUIC register\n");
622 mutex_unlock(&info->mutex);
623 return -EINVAL;
624 }
625
626 adc = info->status[0] & STATUS1_ADC_MASK;
627 adc >>= STATUS1_ADC_SHIFT;
628
629 if (adc != MAX77693_MUIC_ADC_OPEN) {
630 dev_info(info->dev,
631 "external connector is attached (adc:0x%02x)\n", adc);
632
633 ret = max77693_muic_adc_handler(info, adc, true);
634 if (ret < 0)
635 dev_err(info->dev, "failed to detect accessory\n");
636 goto out;
637 }
638
639 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
640 chg_type >>= STATUS2_CHGTYP_SHIFT;
641
642 if (chg_type != MAX77693_CHARGER_TYPE_NONE) {
643 dev_info(info->dev,
644 "external connector is attached (chg_type:0x%x)\n",
645 chg_type);
646
647 max77693_muic_chg_handler(info, chg_type, true);
648 if (ret < 0)
649 dev_err(info->dev, "failed to detect charger accessory\n");
650 }
651
652out:
653 mutex_unlock(&info->mutex);
654 return ret;
655}
656
657static int __devinit max77693_muic_probe(struct platform_device *pdev)
658{
659 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
660 struct max77693_muic_info *info;
661 int ret, i;
662 u8 id;
663
664 info = kzalloc(sizeof(struct max77693_muic_info), GFP_KERNEL);
665 if (!info) {
666 dev_err(&pdev->dev, "failed to allocate memory\n");
667 ret = -ENOMEM;
668 goto err_kfree;
669 }
670 info->dev = &pdev->dev;
671 info->max77693 = max77693;
672 info->max77693->regmap_muic = regmap_init_i2c(info->max77693->muic,
673 &max77693_muic_regmap_config);
674 if (IS_ERR(info->max77693->regmap_muic)) {
675 ret = PTR_ERR(info->max77693->regmap_muic);
676 dev_err(max77693->dev,
677 "failed to allocate register map: %d\n", ret);
678 goto err_regmap;
679 }
680 platform_set_drvdata(pdev, info);
681 mutex_init(&info->mutex);
682
683 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
684
685 /* Support irq domain for MAX77693 MUIC device */
686 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
687 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
688 int virq = 0;
689
690 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
691 if (!virq)
692 goto err_irq;
693 muic_irq->virq = virq;
694
695 ret = request_threaded_irq(virq, NULL,
696 max77693_muic_irq_handler,
697 0, muic_irq->name, info);
698 if (ret) {
699 dev_err(&pdev->dev,
700 "failed: irq request (IRQ: %d,"
701 " error :%d)\n",
702 muic_irq->irq, ret);
703
704 for (i = i - 1; i >= 0; i--)
705 free_irq(muic_irq->virq, info);
706 goto err_irq;
707 }
708 }
709
710 /* Initialize extcon device */
711 info->edev = kzalloc(sizeof(struct extcon_dev), GFP_KERNEL);
712 if (!info->edev) {
713 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
714 ret = -ENOMEM;
715 goto err_irq;
716 }
717 info->edev->name = DEV_NAME;
718 info->edev->supported_cable = max77693_extcon_cable;
719 ret = extcon_dev_register(info->edev, NULL);
720 if (ret) {
721 dev_err(&pdev->dev, "failed to register extcon device\n");
722 goto err_extcon;
723 }
724
725 /* Check revision number of MUIC device*/
726 ret = max77693_read_reg(info->max77693->regmap_muic,
727 MAX77693_MUIC_REG_ID, &id);
728 if (ret < 0) {
729 dev_err(&pdev->dev, "failed to read revision number\n");
730 goto err_extcon;
731 }
732 dev_info(info->dev, "device ID : 0x%x\n", id);
733
734 /* Set ADC debounce time */
735 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
736
737 /* Detect accessory on boot */
738 max77693_muic_detect_accessory(info);
739
740 return ret;
741
742err_extcon:
743 kfree(info->edev);
744err_irq:
745err_regmap:
746 kfree(info);
747err_kfree:
748 return ret;
749}
750
751static int __devexit max77693_muic_remove(struct platform_device *pdev)
752{
753 struct max77693_muic_info *info = platform_get_drvdata(pdev);
754 int i;
755
756 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
757 free_irq(muic_irqs[i].virq, info);
758 cancel_work_sync(&info->irq_work);
759 extcon_dev_unregister(info->edev);
760 kfree(info);
761
762 return 0;
763}
764
765static struct platform_driver max77693_muic_driver = {
766 .driver = {
767 .name = DEV_NAME,
768 .owner = THIS_MODULE,
769 },
770 .probe = max77693_muic_probe,
771 .remove = __devexit_p(max77693_muic_remove),
772};
773
774module_platform_driver(max77693_muic_driver);
775
776MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
777MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
778MODULE_LICENSE("GPL");
779MODULE_ALIAS("platform:extcon-max77693");
diff --git a/drivers/extcon/extcon_class.c b/drivers/extcon/extcon_class.c
index 159aeb07b3ba..f6419f9db76c 100644
--- a/drivers/extcon/extcon_class.c
+++ b/drivers/extcon/extcon_class.c
@@ -65,7 +65,7 @@ const char *extcon_cable_name[] = {
65 NULL, 65 NULL,
66}; 66};
67 67
68struct class *extcon_class; 68static struct class *extcon_class;
69#if defined(CONFIG_ANDROID) 69#if defined(CONFIG_ANDROID)
70static struct class_compat *switch_class; 70static struct class_compat *switch_class;
71#endif /* CONFIG_ANDROID */ 71#endif /* CONFIG_ANDROID */
diff --git a/drivers/extcon/extcon_gpio.c b/drivers/extcon/extcon_gpio.c
index 8a0dcc11c7c7..fe3db45fa83c 100644
--- a/drivers/extcon/extcon_gpio.c
+++ b/drivers/extcon/extcon_gpio.c
@@ -105,25 +105,25 @@ static int __devinit gpio_extcon_probe(struct platform_device *pdev)
105 105
106 ret = extcon_dev_register(&extcon_data->edev, &pdev->dev); 106 ret = extcon_dev_register(&extcon_data->edev, &pdev->dev);
107 if (ret < 0) 107 if (ret < 0)
108 goto err_extcon_dev_register; 108 return ret;
109 109
110 ret = gpio_request_one(extcon_data->gpio, GPIOF_DIR_IN, pdev->name); 110 ret = gpio_request_one(extcon_data->gpio, GPIOF_DIR_IN, pdev->name);
111 if (ret < 0) 111 if (ret < 0)
112 goto err_request_gpio; 112 goto err;
113 113
114 INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work); 114 INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
115 115
116 extcon_data->irq = gpio_to_irq(extcon_data->gpio); 116 extcon_data->irq = gpio_to_irq(extcon_data->gpio);
117 if (extcon_data->irq < 0) { 117 if (extcon_data->irq < 0) {
118 ret = extcon_data->irq; 118 ret = extcon_data->irq;
119 goto err_detect_irq_num_failed; 119 goto err;
120 } 120 }
121 121
122 ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler, 122 ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
123 pdata->irq_flags, pdev->name, 123 pdata->irq_flags, pdev->name,
124 extcon_data); 124 extcon_data);
125 if (ret < 0) 125 if (ret < 0)
126 goto err_request_irq; 126 goto err;
127 127
128 platform_set_drvdata(pdev, extcon_data); 128 platform_set_drvdata(pdev, extcon_data);
129 /* Perform initial detection */ 129 /* Perform initial detection */
@@ -131,13 +131,8 @@ static int __devinit gpio_extcon_probe(struct platform_device *pdev)
131 131
132 return 0; 132 return 0;
133 133
134err_request_irq: 134err:
135err_detect_irq_num_failed:
136 gpio_free(extcon_data->gpio);
137err_request_gpio:
138 extcon_dev_unregister(&extcon_data->edev); 135 extcon_dev_unregister(&extcon_data->edev);
139err_extcon_dev_register:
140 devm_kfree(&pdev->dev, extcon_data);
141 136
142 return ret; 137 return ret;
143} 138}
@@ -148,9 +143,7 @@ static int __devexit gpio_extcon_remove(struct platform_device *pdev)
148 143
149 cancel_delayed_work_sync(&extcon_data->work); 144 cancel_delayed_work_sync(&extcon_data->work);
150 free_irq(extcon_data->irq, extcon_data); 145 free_irq(extcon_data->irq, extcon_data);
151 gpio_free(extcon_data->gpio);
152 extcon_dev_unregister(&extcon_data->edev); 146 extcon_dev_unregister(&extcon_data->edev);
153 devm_kfree(&pdev->dev, extcon_data);
154 147
155 return 0; 148 return 0;
156} 149}