aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/extcon
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-05-22 19:02:13 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-05-22 19:02:13 -0400
commit5d4e2d08e7fdf7339f84a1c670d296a77e02f881 (patch)
tree1c419660defa56191091dfdf50fdb57a72009173 /drivers/extcon
parentfb2123fad3b499f0898835b19dbb93b18d27ee98 (diff)
parent94ca629e40eb7e997be21d8065c25e4f3797b03f (diff)
Merge tag 'driver-core-3.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core
Pull driver core updates from Greg Kroah-Hartman: "Here's the driver core, and other driver subsystems, pull request for the 3.5-rc1 merge window. Outside of a few minor driver core changes, we ended up with the following different subsystem and core changes as well, due to interdependancies on the driver core: - hyperv driver updates - drivers/memory being created and some drivers moved into it - extcon driver subsystem created out of the old Android staging switch driver code - dynamic debug updates - printk rework, and /dev/kmsg changes All of this has been tested in the linux-next releases for a few weeks with no reported problems. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>" Fix up conflicts in drivers/extcon/extcon-max8997.c where git noticed that a patch to the deleted drivers/misc/max8997-muic.c driver needs to be applied to this one. * tag 'driver-core-3.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core: (90 commits) uio_pdrv_genirq: get irq through platform resource if not set otherwise memory: tegra{20,30}-mc: Remove empty *_remove() printk() - isolate KERN_CONT users from ordinary complete lines sysfs: get rid of some lockdep false positives Drivers: hv: util: Properly handle version negotiations. Drivers: hv: Get rid of an unnecessary check in vmbus_prep_negotiate_resp() memory: tegra{20,30}-mc: Use dev_err_ratelimited() driver core: Add dev_*_ratelimited() family Driver Core: don't oops with unregistered driver in driver_find_device() printk() - restore prefix/timestamp printing for multi-newline strings printk: add stub for prepend_timestamp() ARM: tegra30: Make MC optional in Kconfig ARM: tegra20: Make MC optional in Kconfig ARM: tegra30: MC: Remove unnecessary BUG*() ARM: tegra20: MC: Remove unnecessary BUG*() printk: correctly align __log_buf ARM: tegra30: Add Tegra Memory Controller(MC) driver ARM: tegra20: Add Tegra Memory Controller(MC) driver printk() - restore timestamp printing at console output printk() - do not merge continuation lines of different threads ...
Diffstat (limited to 'drivers/extcon')
-rw-r--r--drivers/extcon/Kconfig32
-rw-r--r--drivers/extcon/Makefile7
-rw-r--r--drivers/extcon/extcon-max8997.c535
-rw-r--r--drivers/extcon/extcon_class.c832
-rw-r--r--drivers/extcon/extcon_gpio.c169
5 files changed, 1575 insertions, 0 deletions
diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
new file mode 100644
index 000000000000..29c5cf852efc
--- /dev/null
+++ b/drivers/extcon/Kconfig
@@ -0,0 +1,32 @@
1menuconfig EXTCON
2 tristate "External Connector Class (extcon) support"
3 help
4 Say Y here to enable external connector class (extcon) support.
5 This allows monitoring external connectors by userspace
6 via sysfs and uevent and supports external connectors with
7 multiple states; i.e., an extcon that may have multiple
8 cables attached. For example, an external connector of a device
9 may be used to connect an HDMI cable and a AC adaptor, and to
10 host USB ports. Many of 30-pin connectors including PDMI are
11 also good examples.
12
13if EXTCON
14
15comment "Extcon Device Drivers"
16
17config EXTCON_GPIO
18 tristate "GPIO extcon support"
19 depends on GENERIC_GPIO
20 help
21 Say Y here to enable GPIO based extcon support. Note that GPIO
22 extcon supports single state per extcon instance.
23
24config EXTCON_MAX8997
25 tristate "MAX8997 EXTCON Support"
26 depends on MFD_MAX8997
27 help
28 If you say yes here you get support for the MUIC device of
29 Maxim MAX8997 PMIC. The MAX8997 MUIC is a USB port accessory
30 detector and switch.
31
32endif # MULTISTATE_SWITCH
diff --git a/drivers/extcon/Makefile b/drivers/extcon/Makefile
new file mode 100644
index 000000000000..86020bdb6da0
--- /dev/null
+++ b/drivers/extcon/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for external connector class (extcon) devices
3#
4
5obj-$(CONFIG_EXTCON) += extcon_class.o
6obj-$(CONFIG_EXTCON_GPIO) += extcon_gpio.o
7obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o
diff --git a/drivers/extcon/extcon-max8997.c b/drivers/extcon/extcon-max8997.c
new file mode 100644
index 000000000000..23416e443765
--- /dev/null
+++ b/drivers/extcon/extcon-max8997.c
@@ -0,0 +1,535 @@
1/*
2 * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
3 *
4 * Copyright (C) 2012 Samsung Electrnoics
5 * Donggeun Kim <dg77.kim@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/kobject.h>
26#include <linux/mfd/max8997.h>
27#include <linux/mfd/max8997-private.h>
28#include <linux/extcon.h>
29
30#define DEV_NAME "max8997-muic"
31
32/* MAX8997-MUIC STATUS1 register */
33#define STATUS1_ADC_SHIFT 0
34#define STATUS1_ADCLOW_SHIFT 5
35#define STATUS1_ADCERR_SHIFT 6
36#define STATUS1_ADC_MASK (0x1f << STATUS1_ADC_SHIFT)
37#define STATUS1_ADCLOW_MASK (0x1 << STATUS1_ADCLOW_SHIFT)
38#define STATUS1_ADCERR_MASK (0x1 << STATUS1_ADCERR_SHIFT)
39
40/* MAX8997-MUIC STATUS2 register */
41#define STATUS2_CHGTYP_SHIFT 0
42#define STATUS2_CHGDETRUN_SHIFT 3
43#define STATUS2_DCDTMR_SHIFT 4
44#define STATUS2_DBCHG_SHIFT 5
45#define STATUS2_VBVOLT_SHIFT 6
46#define STATUS2_CHGTYP_MASK (0x7 << STATUS2_CHGTYP_SHIFT)
47#define STATUS2_CHGDETRUN_MASK (0x1 << STATUS2_CHGDETRUN_SHIFT)
48#define STATUS2_DCDTMR_MASK (0x1 << STATUS2_DCDTMR_SHIFT)
49#define STATUS2_DBCHG_MASK (0x1 << STATUS2_DBCHG_SHIFT)
50#define STATUS2_VBVOLT_MASK (0x1 << STATUS2_VBVOLT_SHIFT)
51
52/* MAX8997-MUIC STATUS3 register */
53#define STATUS3_OVP_SHIFT 2
54#define STATUS3_OVP_MASK (0x1 << STATUS3_OVP_SHIFT)
55
56/* MAX8997-MUIC CONTROL1 register */
57#define COMN1SW_SHIFT 0
58#define COMP2SW_SHIFT 3
59#define COMN1SW_MASK (0x7 << COMN1SW_SHIFT)
60#define COMP2SW_MASK (0x7 << COMP2SW_SHIFT)
61#define SW_MASK (COMP2SW_MASK | COMN1SW_MASK)
62
63#define MAX8997_SW_USB ((1 << COMP2SW_SHIFT) | (1 << COMN1SW_SHIFT))
64#define MAX8997_SW_AUDIO ((2 << COMP2SW_SHIFT) | (2 << COMN1SW_SHIFT))
65#define MAX8997_SW_UART ((3 << COMP2SW_SHIFT) | (3 << COMN1SW_SHIFT))
66#define MAX8997_SW_OPEN ((0 << COMP2SW_SHIFT) | (0 << COMN1SW_SHIFT))
67
68#define MAX8997_ADC_GROUND 0x00
69#define MAX8997_ADC_MHL 0x01
70#define MAX8997_ADC_JIG_USB_1 0x18
71#define MAX8997_ADC_JIG_USB_2 0x19
72#define MAX8997_ADC_DESKDOCK 0x1a
73#define MAX8997_ADC_JIG_UART 0x1c
74#define MAX8997_ADC_CARDOCK 0x1d
75#define MAX8997_ADC_OPEN 0x1f
76
77struct max8997_muic_irq {
78 unsigned int irq;
79 const char *name;
80};
81
82static struct max8997_muic_irq muic_irqs[] = {
83 { MAX8997_MUICIRQ_ADCError, "muic-ADC_error" },
84 { MAX8997_MUICIRQ_ADCLow, "muic-ADC_low" },
85 { MAX8997_MUICIRQ_ADC, "muic-ADC" },
86 { MAX8997_MUICIRQ_VBVolt, "muic-VB_voltage" },
87 { MAX8997_MUICIRQ_DBChg, "muic-DB_charger" },
88 { MAX8997_MUICIRQ_DCDTmr, "muic-DCD_timer" },
89 { MAX8997_MUICIRQ_ChgDetRun, "muic-CDR_status" },
90 { MAX8997_MUICIRQ_ChgTyp, "muic-charger_type" },
91 { MAX8997_MUICIRQ_OVP, "muic-over_voltage" },
92};
93
94struct max8997_muic_info {
95 struct device *dev;
96 struct i2c_client *muic;
97 struct max8997_muic_platform_data *muic_pdata;
98
99 int irq;
100 struct work_struct irq_work;
101
102 enum max8997_muic_charger_type pre_charger_type;
103 int pre_adc;
104
105 struct mutex mutex;
106
107 struct extcon_dev *edev;
108};
109
110const char *max8997_extcon_cable[] = {
111 [0] = "USB",
112 [1] = "USB-Host",
113 [2] = "TA",
114 [3] = "Fast-charger",
115 [4] = "Slow-charger",
116 [5] = "Charge-downstream",
117 [6] = "MHL",
118 [7] = "Dock-desk",
119 [7] = "Dock-card",
120 [8] = "JIG",
121
122 NULL,
123};
124
125static int max8997_muic_handle_usb(struct max8997_muic_info *info,
126 enum max8997_muic_usb_type usb_type, bool attached)
127{
128 int ret = 0;
129
130 if (usb_type == MAX8997_USB_HOST) {
131 /* switch to USB */
132 ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
133 attached ? MAX8997_SW_USB : MAX8997_SW_OPEN,
134 SW_MASK);
135 if (ret) {
136 dev_err(info->dev, "failed to update muic register\n");
137 goto out;
138 }
139 }
140
141 switch (usb_type) {
142 case MAX8997_USB_HOST:
143 extcon_set_cable_state(info->edev, "USB-Host", attached);
144 break;
145 case MAX8997_USB_DEVICE:
146 extcon_set_cable_state(info->edev, "USB", attached);
147 break;
148 default:
149 ret = -EINVAL;
150 break;
151 }
152
153out:
154 return ret;
155}
156
157static int max8997_muic_handle_dock(struct max8997_muic_info *info,
158 int adc, bool attached)
159{
160 int ret = 0;
161
162 /* switch to AUDIO */
163 ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
164 attached ? MAX8997_SW_AUDIO : MAX8997_SW_OPEN,
165 SW_MASK);
166 if (ret) {
167 dev_err(info->dev, "failed to update muic register\n");
168 goto out;
169 }
170
171 switch (adc) {
172 case MAX8997_ADC_DESKDOCK:
173 extcon_set_cable_state(info->edev, "Dock-desk", attached);
174 break;
175 case MAX8997_ADC_CARDOCK:
176 extcon_set_cable_state(info->edev, "Dock-card", attached);
177 break;
178 default:
179 ret = -EINVAL;
180 break;
181 }
182out:
183 return ret;
184}
185
186static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
187 bool attached)
188{
189 int ret = 0;
190
191 /* switch to UART */
192 ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
193 attached ? MAX8997_SW_UART : MAX8997_SW_OPEN,
194 SW_MASK);
195 if (ret) {
196 dev_err(info->dev, "failed to update muic register\n");
197 goto out;
198 }
199
200 extcon_set_cable_state(info->edev, "JIG", attached);
201out:
202 return ret;
203}
204
205static int max8997_muic_handle_adc_detach(struct max8997_muic_info *info)
206{
207 int ret = 0;
208
209 switch (info->pre_adc) {
210 case MAX8997_ADC_GROUND:
211 ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, false);
212 break;
213 case MAX8997_ADC_MHL:
214 extcon_set_cable_state(info->edev, "MHL", false);
215 break;
216 case MAX8997_ADC_JIG_USB_1:
217 case MAX8997_ADC_JIG_USB_2:
218 ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, false);
219 break;
220 case MAX8997_ADC_DESKDOCK:
221 case MAX8997_ADC_CARDOCK:
222 ret = max8997_muic_handle_dock(info, info->pre_adc, false);
223 break;
224 case MAX8997_ADC_JIG_UART:
225 ret = max8997_muic_handle_jig_uart(info, false);
226 break;
227 default:
228 break;
229 }
230
231 return ret;
232}
233
234static int max8997_muic_handle_adc(struct max8997_muic_info *info, int adc)
235{
236 int ret = 0;
237
238 switch (adc) {
239 case MAX8997_ADC_GROUND:
240 ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, true);
241 break;
242 case MAX8997_ADC_MHL:
243 extcon_set_cable_state(info->edev, "MHL", true);
244 break;
245 case MAX8997_ADC_JIG_USB_1:
246 case MAX8997_ADC_JIG_USB_2:
247 ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, true);
248 break;
249 case MAX8997_ADC_DESKDOCK:
250 case MAX8997_ADC_CARDOCK:
251 ret = max8997_muic_handle_dock(info, adc, true);
252 break;
253 case MAX8997_ADC_JIG_UART:
254 ret = max8997_muic_handle_jig_uart(info, true);
255 break;
256 case MAX8997_ADC_OPEN:
257 ret = max8997_muic_handle_adc_detach(info);
258 break;
259 default:
260 ret = -EINVAL;
261 goto out;
262 }
263
264 info->pre_adc = adc;
265out:
266 return ret;
267}
268
269static int max8997_muic_handle_charger_type_detach(
270 struct max8997_muic_info *info)
271{
272 int ret = 0;
273
274 switch (info->pre_charger_type) {
275 case MAX8997_CHARGER_TYPE_USB:
276 extcon_set_cable_state(info->edev, "USB", false);
277 break;
278 case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
279 extcon_set_cable_state(info->edev, "Charge-downstream", false);
280 break;
281 case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
282 extcon_set_cable_state(info->edev, "TA", false);
283 break;
284 case MAX8997_CHARGER_TYPE_500MA:
285 extcon_set_cable_state(info->edev, "Slow-charger", false);
286 break;
287 case MAX8997_CHARGER_TYPE_1A:
288 extcon_set_cable_state(info->edev, "Fast-charger", false);
289 break;
290 default:
291 ret = -EINVAL;
292 break;
293 }
294
295 return ret;
296}
297
298static int max8997_muic_handle_charger_type(struct max8997_muic_info *info,
299 enum max8997_muic_charger_type charger_type)
300{
301 u8 adc;
302 int ret;
303
304 ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_STATUS1, &adc);
305 if (ret) {
306 dev_err(info->dev, "failed to read muic register\n");
307 goto out;
308 }
309
310 switch (charger_type) {
311 case MAX8997_CHARGER_TYPE_NONE:
312 ret = max8997_muic_handle_charger_type_detach(info);
313 break;
314 case MAX8997_CHARGER_TYPE_USB:
315 if ((adc & STATUS1_ADC_MASK) == MAX8997_ADC_OPEN) {
316 max8997_muic_handle_usb(info,
317 MAX8997_USB_DEVICE, true);
318 }
319 break;
320 case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
321 extcon_set_cable_state(info->edev, "Charge-downstream", true);
322 break;
323 case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
324 extcon_set_cable_state(info->edev, "TA", true);
325 break;
326 case MAX8997_CHARGER_TYPE_500MA:
327 extcon_set_cable_state(info->edev, "Slow-charger", true);
328 break;
329 case MAX8997_CHARGER_TYPE_1A:
330 extcon_set_cable_state(info->edev, "Fast-charger", true);
331 break;
332 default:
333 ret = -EINVAL;
334 goto out;
335 }
336
337 info->pre_charger_type = charger_type;
338out:
339 return ret;
340}
341
342static void max8997_muic_irq_work(struct work_struct *work)
343{
344 struct max8997_muic_info *info = container_of(work,
345 struct max8997_muic_info, irq_work);
346 struct max8997_dev *max8997 = i2c_get_clientdata(info->muic);
347 u8 status[2];
348 u8 adc, chg_type;
349
350 int irq_type = info->irq - max8997->irq_base;
351 int ret;
352
353 mutex_lock(&info->mutex);
354
355 ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
356 2, status);
357 if (ret) {
358 dev_err(info->dev, "failed to read muic register\n");
359 mutex_unlock(&info->mutex);
360 return;
361 }
362
363 dev_dbg(info->dev, "%s: STATUS1:0x%x, 2:0x%x\n", __func__,
364 status[0], status[1]);
365
366 switch (irq_type) {
367 case MAX8997_MUICIRQ_ADC:
368 adc = status[0] & STATUS1_ADC_MASK;
369 adc >>= STATUS1_ADC_SHIFT;
370
371 max8997_muic_handle_adc(info, adc);
372 break;
373 case MAX8997_MUICIRQ_ChgTyp:
374 chg_type = status[1] & STATUS2_CHGTYP_MASK;
375 chg_type >>= STATUS2_CHGTYP_SHIFT;
376
377 max8997_muic_handle_charger_type(info, chg_type);
378 break;
379 default:
380 dev_info(info->dev, "misc interrupt: irq %d occurred\n",
381 irq_type);
382 break;
383 }
384
385 mutex_unlock(&info->mutex);
386
387 return;
388}
389
390static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
391{
392 struct max8997_muic_info *info = data;
393
394 dev_dbg(info->dev, "irq:%d\n", irq);
395 info->irq = irq;
396
397 schedule_work(&info->irq_work);
398
399 return IRQ_HANDLED;
400}
401
402static void max8997_muic_detect_dev(struct max8997_muic_info *info)
403{
404 int ret;
405 u8 status[2], adc, chg_type;
406
407 ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
408 2, status);
409 if (ret) {
410 dev_err(info->dev, "failed to read muic register\n");
411 return;
412 }
413
414 dev_info(info->dev, "STATUS1:0x%x, STATUS2:0x%x\n",
415 status[0], status[1]);
416
417 adc = status[0] & STATUS1_ADC_MASK;
418 adc >>= STATUS1_ADC_SHIFT;
419
420 chg_type = status[1] & STATUS2_CHGTYP_MASK;
421 chg_type >>= STATUS2_CHGTYP_SHIFT;
422
423 max8997_muic_handle_adc(info, adc);
424 max8997_muic_handle_charger_type(info, chg_type);
425}
426
427static int __devinit max8997_muic_probe(struct platform_device *pdev)
428{
429 struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
430 struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
431 struct max8997_muic_info *info;
432 int ret, i;
433
434 info = kzalloc(sizeof(struct max8997_muic_info), GFP_KERNEL);
435 if (!info) {
436 dev_err(&pdev->dev, "failed to allocate memory\n");
437 ret = -ENOMEM;
438 goto err_kfree;
439 }
440
441 info->dev = &pdev->dev;
442 info->muic = max8997->muic;
443
444 platform_set_drvdata(pdev, info);
445 mutex_init(&info->mutex);
446
447 INIT_WORK(&info->irq_work, max8997_muic_irq_work);
448
449 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
450 struct max8997_muic_irq *muic_irq = &muic_irqs[i];
451
452 ret = request_threaded_irq(pdata->irq_base + muic_irq->irq,
453 NULL, max8997_muic_irq_handler,
454 0, muic_irq->name,
455 info);
456 if (ret) {
457 dev_err(&pdev->dev,
458 "failed: irq request (IRQ: %d,"
459 " error :%d)\n",
460 muic_irq->irq, ret);
461 goto err_irq;
462 }
463 }
464
465 /* External connector */
466 info->edev = kzalloc(sizeof(struct extcon_dev), GFP_KERNEL);
467 if (!info->edev) {
468 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
469 ret = -ENOMEM;
470 goto err_irq;
471 }
472 info->edev->name = DEV_NAME;
473 info->edev->supported_cable = max8997_extcon_cable;
474 ret = extcon_dev_register(info->edev, NULL);
475 if (ret) {
476 dev_err(&pdev->dev, "failed to register extcon device\n");
477 goto err_extcon;
478 }
479
480 /* Initialize registers according to platform data */
481 if (pdata->muic_pdata) {
482 struct max8997_muic_platform_data *mdata = info->muic_pdata;
483
484 for (i = 0; i < mdata->num_init_data; i++) {
485 max8997_write_reg(info->muic, mdata->init_data[i].addr,
486 mdata->init_data[i].data);
487 }
488 }
489
490 /* Initial device detection */
491 max8997_muic_detect_dev(info);
492
493 return ret;
494
495err_extcon:
496 kfree(info->edev);
497err_irq:
498 while (--i >= 0)
499 free_irq(pdata->irq_base + muic_irqs[i].irq, info);
500 kfree(info);
501err_kfree:
502 return ret;
503}
504
505static int __devexit max8997_muic_remove(struct platform_device *pdev)
506{
507 struct max8997_muic_info *info = platform_get_drvdata(pdev);
508 struct max8997_dev *max8997 = i2c_get_clientdata(info->muic);
509 int i;
510
511 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
512 free_irq(max8997->irq_base + muic_irqs[i].irq, info);
513 cancel_work_sync(&info->irq_work);
514
515 extcon_dev_unregister(info->edev);
516
517 kfree(info);
518
519 return 0;
520}
521
522static struct platform_driver max8997_muic_driver = {
523 .driver = {
524 .name = DEV_NAME,
525 .owner = THIS_MODULE,
526 },
527 .probe = max8997_muic_probe,
528 .remove = __devexit_p(max8997_muic_remove),
529};
530
531module_platform_driver(max8997_muic_driver);
532
533MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
534MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
535MODULE_LICENSE("GPL");
diff --git a/drivers/extcon/extcon_class.c b/drivers/extcon/extcon_class.c
new file mode 100644
index 000000000000..f598a700ec15
--- /dev/null
+++ b/drivers/extcon/extcon_class.c
@@ -0,0 +1,832 @@
1/*
2 * drivers/extcon/extcon_class.c
3 *
4 * External connector (extcon) class driver
5 *
6 * Copyright (C) 2012 Samsung Electronics
7 * Author: Donggeun Kim <dg77.kim@samsung.com>
8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
9 *
10 * based on android/drivers/switch/switch_class.c
11 * Copyright (C) 2008 Google, Inc.
12 * Author: Mike Lockwood <lockwood@android.com>
13 *
14 * This software is licensed under the terms of the GNU General Public
15 * License version 2, as published by the Free Software Foundation, and
16 * may be copied, distributed, and modified under those terms.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23*/
24
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/fs.h>
30#include <linux/err.h>
31#include <linux/extcon.h>
32#include <linux/slab.h>
33
34/*
35 * extcon_cable_name suggests the standard cable names for commonly used
36 * cable types.
37 *
38 * However, please do not use extcon_cable_name directly for extcon_dev
39 * struct's supported_cable pointer unless your device really supports
40 * every single port-type of the following cable names. Please choose cable
41 * names that are actually used in your extcon device.
42 */
43const char *extcon_cable_name[] = {
44 [EXTCON_USB] = "USB",
45 [EXTCON_USB_HOST] = "USB-Host",
46 [EXTCON_TA] = "TA",
47 [EXTCON_FAST_CHARGER] = "Fast-charger",
48 [EXTCON_SLOW_CHARGER] = "Slow-charger",
49 [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
50 [EXTCON_HDMI] = "HDMI",
51 [EXTCON_MHL] = "MHL",
52 [EXTCON_DVI] = "DVI",
53 [EXTCON_VGA] = "VGA",
54 [EXTCON_DOCK] = "Dock",
55 [EXTCON_LINE_IN] = "Line-in",
56 [EXTCON_LINE_OUT] = "Line-out",
57 [EXTCON_MIC_IN] = "Microphone",
58 [EXTCON_HEADPHONE_OUT] = "Headphone",
59 [EXTCON_SPDIF_IN] = "SPDIF-in",
60 [EXTCON_SPDIF_OUT] = "SPDIF-out",
61 [EXTCON_VIDEO_IN] = "Video-in",
62 [EXTCON_VIDEO_OUT] = "Video-out",
63 [EXTCON_MECHANICAL] = "Mechanical",
64
65 NULL,
66};
67
68struct class *extcon_class;
69#if defined(CONFIG_ANDROID)
70static struct class_compat *switch_class;
71#endif /* CONFIG_ANDROID */
72
73static LIST_HEAD(extcon_dev_list);
74static DEFINE_MUTEX(extcon_dev_list_lock);
75
76/**
77 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
78 * condition.
79 * @edev: the extcon device
80 * @new_state: new cable attach status for @edev
81 *
82 * Returns 0 if nothing violates. Returns the index + 1 for the first
83 * violated condition.
84 */
85static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
86{
87 int i = 0;
88
89 if (!edev->mutually_exclusive)
90 return 0;
91
92 for (i = 0; edev->mutually_exclusive[i]; i++) {
93 int count = 0, j;
94 u32 correspondants = new_state & edev->mutually_exclusive[i];
95 u32 exp = 1;
96
97 for (j = 0; j < 32; j++) {
98 if (exp & correspondants)
99 count++;
100 if (count > 1)
101 return i + 1;
102 exp <<= 1;
103 }
104 }
105
106 return 0;
107}
108
109static ssize_t state_show(struct device *dev, struct device_attribute *attr,
110 char *buf)
111{
112 int i, count = 0;
113 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
114
115 if (edev->print_state) {
116 int ret = edev->print_state(edev, buf);
117
118 if (ret >= 0)
119 return ret;
120 /* Use default if failed */
121 }
122
123 if (edev->max_supported == 0)
124 return sprintf(buf, "%u\n", edev->state);
125
126 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
127 if (!edev->supported_cable[i])
128 break;
129 count += sprintf(buf + count, "%s=%d\n",
130 edev->supported_cable[i],
131 !!(edev->state & (1 << i)));
132 }
133
134 return count;
135}
136
137int extcon_set_state(struct extcon_dev *edev, u32 state);
138static ssize_t state_store(struct device *dev, struct device_attribute *attr,
139 const char *buf, size_t count)
140{
141 u32 state;
142 ssize_t ret = 0;
143 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
144
145 ret = sscanf(buf, "0x%x", &state);
146 if (ret == 0)
147 ret = -EINVAL;
148 else
149 ret = extcon_set_state(edev, state);
150
151 if (ret < 0)
152 return ret;
153
154 return count;
155}
156
157static ssize_t name_show(struct device *dev, struct device_attribute *attr,
158 char *buf)
159{
160 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
161
162 /* Optional callback given by the user */
163 if (edev->print_name) {
164 int ret = edev->print_name(edev, buf);
165 if (ret >= 0)
166 return ret;
167 }
168
169 return sprintf(buf, "%s\n", dev_name(edev->dev));
170}
171
172static ssize_t cable_name_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
175 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
176 attr_name);
177
178 return sprintf(buf, "%s\n",
179 cable->edev->supported_cable[cable->cable_index]);
180}
181
182static ssize_t cable_state_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
185 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
186 attr_state);
187
188 return sprintf(buf, "%d\n",
189 extcon_get_cable_state_(cable->edev,
190 cable->cable_index));
191}
192
193static ssize_t cable_state_store(struct device *dev,
194 struct device_attribute *attr, const char *buf,
195 size_t count)
196{
197 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
198 attr_state);
199 int ret, state;
200
201 ret = sscanf(buf, "%d", &state);
202 if (ret == 0)
203 ret = -EINVAL;
204 else
205 ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
206 state);
207
208 if (ret < 0)
209 return ret;
210 return count;
211}
212
213/**
214 * extcon_update_state() - Update the cable attach states of the extcon device
215 * only for the masked bits.
216 * @edev: the extcon device
217 * @mask: the bit mask to designate updated bits.
218 * @state: new cable attach status for @edev
219 *
220 * Changing the state sends uevent with environment variable containing
221 * the name of extcon device (envp[0]) and the state output (envp[1]).
222 * Tizen uses this format for extcon device to get events from ports.
223 * Android uses this format as well.
224 *
225 * Note that the notifier provides which bits are changed in the state
226 * variable with the val parameter (second) to the callback.
227 */
228int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
229{
230 char name_buf[120];
231 char state_buf[120];
232 char *prop_buf;
233 char *envp[3];
234 int env_offset = 0;
235 int length;
236 unsigned long flags;
237
238 spin_lock_irqsave(&edev->lock, flags);
239
240 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
241 u32 old_state = edev->state;
242
243 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
244 (state & mask))) {
245 spin_unlock_irqrestore(&edev->lock, flags);
246 return -EPERM;
247 }
248
249 edev->state &= ~mask;
250 edev->state |= state & mask;
251
252 raw_notifier_call_chain(&edev->nh, old_state, edev);
253
254 /* This could be in interrupt handler */
255 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
256 if (prop_buf) {
257 length = name_show(edev->dev, NULL, prop_buf);
258 if (length > 0) {
259 if (prop_buf[length - 1] == '\n')
260 prop_buf[length - 1] = 0;
261 snprintf(name_buf, sizeof(name_buf),
262 "NAME=%s", prop_buf);
263 envp[env_offset++] = name_buf;
264 }
265 length = state_show(edev->dev, NULL, prop_buf);
266 if (length > 0) {
267 if (prop_buf[length - 1] == '\n')
268 prop_buf[length - 1] = 0;
269 snprintf(state_buf, sizeof(state_buf),
270 "STATE=%s", prop_buf);
271 envp[env_offset++] = state_buf;
272 }
273 envp[env_offset] = NULL;
274 /* Unlock early before uevent */
275 spin_unlock_irqrestore(&edev->lock, flags);
276
277 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
278 free_page((unsigned long)prop_buf);
279 } else {
280 /* Unlock early before uevent */
281 spin_unlock_irqrestore(&edev->lock, flags);
282
283 dev_err(edev->dev, "out of memory in extcon_set_state\n");
284 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
285 }
286 } else {
287 /* No changes */
288 spin_unlock_irqrestore(&edev->lock, flags);
289 }
290
291 return 0;
292}
293EXPORT_SYMBOL_GPL(extcon_update_state);
294
295/**
296 * extcon_set_state() - Set the cable attach states of the extcon device.
297 * @edev: the extcon device
298 * @state: new cable attach status for @edev
299 *
300 * Note that notifier provides which bits are changed in the state
301 * variable with the val parameter (second) to the callback.
302 */
303int extcon_set_state(struct extcon_dev *edev, u32 state)
304{
305 return extcon_update_state(edev, 0xffffffff, state);
306}
307EXPORT_SYMBOL_GPL(extcon_set_state);
308
309/**
310 * extcon_find_cable_index() - Get the cable index based on the cable name.
311 * @edev: the extcon device that has the cable.
312 * @cable_name: cable name to be searched.
313 *
314 * Note that accessing a cable state based on cable_index is faster than
315 * cable_name because using cable_name induces a loop with strncmp().
316 * Thus, when get/set_cable_state is repeatedly used, using cable_index
317 * is recommended.
318 */
319int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
320{
321 int i;
322
323 if (edev->supported_cable) {
324 for (i = 0; edev->supported_cable[i]; i++) {
325 if (!strncmp(edev->supported_cable[i],
326 cable_name, CABLE_NAME_MAX))
327 return i;
328 }
329 }
330
331 return -EINVAL;
332}
333EXPORT_SYMBOL_GPL(extcon_find_cable_index);
334
335/**
336 * extcon_get_cable_state_() - Get the status of a specific cable.
337 * @edev: the extcon device that has the cable.
338 * @index: cable index that can be retrieved by extcon_find_cable_index().
339 */
340int extcon_get_cable_state_(struct extcon_dev *edev, int index)
341{
342 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
343 return -EINVAL;
344
345 return !!(edev->state & (1 << index));
346}
347EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
348
349/**
350 * extcon_get_cable_state() - Get the status of a specific cable.
351 * @edev: the extcon device that has the cable.
352 * @cable_name: cable name.
353 *
354 * Note that this is slower than extcon_get_cable_state_.
355 */
356int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
357{
358 return extcon_get_cable_state_(edev, extcon_find_cable_index
359 (edev, cable_name));
360}
361EXPORT_SYMBOL_GPL(extcon_get_cable_state);
362
363/**
364 * extcon_get_cable_state_() - Set the status of a specific cable.
365 * @edev: the extcon device that has the cable.
366 * @index: cable index that can be retrieved by extcon_find_cable_index().
367 * @cable_state: the new cable status. The default semantics is
368 * true: attached / false: detached.
369 */
370int extcon_set_cable_state_(struct extcon_dev *edev,
371 int index, bool cable_state)
372{
373 u32 state;
374
375 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
376 return -EINVAL;
377
378 state = cable_state ? (1 << index) : 0;
379 return extcon_update_state(edev, 1 << index, state);
380}
381EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
382
383/**
384 * extcon_get_cable_state() - Set the status of a specific cable.
385 * @edev: the extcon device that has the cable.
386 * @cable_name: cable name.
387 * @cable_state: the new cable status. The default semantics is
388 * true: attached / false: detached.
389 *
390 * Note that this is slower than extcon_set_cable_state_.
391 */
392int extcon_set_cable_state(struct extcon_dev *edev,
393 const char *cable_name, bool cable_state)
394{
395 return extcon_set_cable_state_(edev, extcon_find_cable_index
396 (edev, cable_name), cable_state);
397}
398EXPORT_SYMBOL_GPL(extcon_set_cable_state);
399
400/**
401 * extcon_get_extcon_dev() - Get the extcon device instance from the name
402 * @extcon_name: The extcon name provided with extcon_dev_register()
403 */
404struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
405{
406 struct extcon_dev *sd;
407
408 mutex_lock(&extcon_dev_list_lock);
409 list_for_each_entry(sd, &extcon_dev_list, entry) {
410 if (!strcmp(sd->name, extcon_name))
411 goto out;
412 }
413 sd = NULL;
414out:
415 mutex_unlock(&extcon_dev_list_lock);
416 return sd;
417}
418EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
419
420static int _call_per_cable(struct notifier_block *nb, unsigned long val,
421 void *ptr)
422{
423 struct extcon_specific_cable_nb *obj = container_of(nb,
424 struct extcon_specific_cable_nb, internal_nb);
425 struct extcon_dev *edev = ptr;
426
427 if ((val & (1 << obj->cable_index)) !=
428 (edev->state & (1 << obj->cable_index))) {
429 bool cable_state = true;
430
431 obj->previous_value = val;
432
433 if (val & (1 << obj->cable_index))
434 cable_state = false;
435
436 return obj->user_nb->notifier_call(obj->user_nb,
437 cable_state, ptr);
438 }
439
440 return NOTIFY_OK;
441}
442
443/**
444 * extcon_register_interest() - Register a notifier for a state change of a
445 * specific cable, not a entier set of cables of a
446 * extcon device.
447 * @obj: an empty extcon_specific_cable_nb object to be returned.
448 * @extcon_name: the name of extcon device.
449 * @cable_name: the target cable name.
450 * @nb: the notifier block to get notified.
451 *
452 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
453 * the struct for you.
454 *
455 * extcon_register_interest is a helper function for those who want to get
456 * notification for a single specific cable's status change. If a user wants
457 * to get notification for any changes of all cables of a extcon device,
458 * he/she should use the general extcon_register_notifier().
459 *
460 * Note that the second parameter given to the callback of nb (val) is
461 * "old_state", not the current state. The current state can be retrieved
462 * by looking at the third pameter (edev pointer)'s state value.
463 */
464int extcon_register_interest(struct extcon_specific_cable_nb *obj,
465 const char *extcon_name, const char *cable_name,
466 struct notifier_block *nb)
467{
468 if (!obj || !extcon_name || !cable_name || !nb)
469 return -EINVAL;
470
471 obj->edev = extcon_get_extcon_dev(extcon_name);
472 if (!obj->edev)
473 return -ENODEV;
474
475 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
476 if (obj->cable_index < 0)
477 return -ENODEV;
478
479 obj->user_nb = nb;
480
481 obj->internal_nb.notifier_call = _call_per_cable;
482
483 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
484}
485
486/**
487 * extcon_unregister_interest() - Unregister the notifier registered by
488 * extcon_register_interest().
489 * @obj: the extcon_specific_cable_nb object returned by
490 * extcon_register_interest().
491 */
492int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
493{
494 if (!obj)
495 return -EINVAL;
496
497 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
498}
499
500/**
501 * extcon_register_notifier() - Register a notifee to get notified by
502 * any attach status changes from the extcon.
503 * @edev: the extcon device.
504 * @nb: a notifier block to be registered.
505 *
506 * Note that the second parameter given to the callback of nb (val) is
507 * "old_state", not the current state. The current state can be retrieved
508 * by looking at the third pameter (edev pointer)'s state value.
509 */
510int extcon_register_notifier(struct extcon_dev *edev,
511 struct notifier_block *nb)
512{
513 return raw_notifier_chain_register(&edev->nh, nb);
514}
515EXPORT_SYMBOL_GPL(extcon_register_notifier);
516
517/**
518 * extcon_unregister_notifier() - Unregister a notifee from the extcon device.
519 * @edev: the extcon device.
520 * @nb: a registered notifier block to be unregistered.
521 */
522int extcon_unregister_notifier(struct extcon_dev *edev,
523 struct notifier_block *nb)
524{
525 return raw_notifier_chain_unregister(&edev->nh, nb);
526}
527EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
528
529static struct device_attribute extcon_attrs[] = {
530 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
531 __ATTR_RO(name),
532 __ATTR_NULL,
533};
534
535static int create_extcon_class(void)
536{
537 if (!extcon_class) {
538 extcon_class = class_create(THIS_MODULE, "extcon");
539 if (IS_ERR(extcon_class))
540 return PTR_ERR(extcon_class);
541 extcon_class->dev_attrs = extcon_attrs;
542
543#if defined(CONFIG_ANDROID)
544 switch_class = class_compat_register("switch");
545 if (WARN(!switch_class, "cannot allocate"))
546 return -ENOMEM;
547#endif /* CONFIG_ANDROID */
548 }
549
550 return 0;
551}
552
553static void extcon_cleanup(struct extcon_dev *edev, bool skip)
554{
555 mutex_lock(&extcon_dev_list_lock);
556 list_del(&edev->entry);
557 mutex_unlock(&extcon_dev_list_lock);
558
559 if (!skip && get_device(edev->dev)) {
560 int index;
561
562 if (edev->mutually_exclusive && edev->max_supported) {
563 for (index = 0; edev->mutually_exclusive[index];
564 index++)
565 kfree(edev->d_attrs_muex[index].attr.name);
566 kfree(edev->d_attrs_muex);
567 kfree(edev->attrs_muex);
568 }
569
570 for (index = 0; index < edev->max_supported; index++)
571 kfree(edev->cables[index].attr_g.name);
572
573 if (edev->max_supported) {
574 kfree(edev->extcon_dev_type.groups);
575 kfree(edev->cables);
576 }
577
578 device_unregister(edev->dev);
579 put_device(edev->dev);
580 }
581
582 kfree(edev->dev);
583}
584
585static void extcon_dev_release(struct device *dev)
586{
587 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
588
589 extcon_cleanup(edev, true);
590}
591
592static const char *muex_name = "mutually_exclusive";
593static void dummy_sysfs_dev_release(struct device *dev)
594{
595}
596
597/**
598 * extcon_dev_register() - Register a new extcon device
599 * @edev : the new extcon device (should be allocated before calling)
600 * @dev : the parent device for this extcon device.
601 *
602 * Among the members of edev struct, please set the "user initializing data"
603 * in any case and set the "optional callbacks" if required. However, please
604 * do not set the values of "internal data", which are initialized by
605 * this function.
606 */
607int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
608{
609 int ret, index = 0;
610
611 if (!extcon_class) {
612 ret = create_extcon_class();
613 if (ret < 0)
614 return ret;
615 }
616
617 if (edev->supported_cable) {
618 /* Get size of array */
619 for (index = 0; edev->supported_cable[index]; index++)
620 ;
621 edev->max_supported = index;
622 } else {
623 edev->max_supported = 0;
624 }
625
626 if (index > SUPPORTED_CABLE_MAX) {
627 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
628 return -EINVAL;
629 }
630
631 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
632 if (!edev->dev)
633 return -ENOMEM;
634 edev->dev->parent = dev;
635 edev->dev->class = extcon_class;
636 edev->dev->release = extcon_dev_release;
637
638 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
639
640 if (edev->max_supported) {
641 char buf[10];
642 char *str;
643 struct extcon_cable *cable;
644
645 edev->cables = kzalloc(sizeof(struct extcon_cable) *
646 edev->max_supported, GFP_KERNEL);
647 if (!edev->cables) {
648 ret = -ENOMEM;
649 goto err_sysfs_alloc;
650 }
651 for (index = 0; index < edev->max_supported; index++) {
652 cable = &edev->cables[index];
653
654 snprintf(buf, 10, "cable.%d", index);
655 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
656 GFP_KERNEL);
657 if (!str) {
658 for (index--; index >= 0; index--) {
659 cable = &edev->cables[index];
660 kfree(cable->attr_g.name);
661 }
662 ret = -ENOMEM;
663
664 goto err_alloc_cables;
665 }
666 strcpy(str, buf);
667
668 cable->edev = edev;
669 cable->cable_index = index;
670 cable->attrs[0] = &cable->attr_name.attr;
671 cable->attrs[1] = &cable->attr_state.attr;
672 cable->attrs[2] = NULL;
673 cable->attr_g.name = str;
674 cable->attr_g.attrs = cable->attrs;
675
676 cable->attr_name.attr.name = "name";
677 cable->attr_name.attr.mode = 0444;
678 cable->attr_name.show = cable_name_show;
679
680 cable->attr_state.attr.name = "state";
681 cable->attr_state.attr.mode = 0644;
682 cable->attr_state.show = cable_state_show;
683 cable->attr_state.store = cable_state_store;
684 }
685 }
686
687 if (edev->max_supported && edev->mutually_exclusive) {
688 char buf[80];
689 char *name;
690
691 /* Count the size of mutually_exclusive array */
692 for (index = 0; edev->mutually_exclusive[index]; index++)
693 ;
694
695 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
696 (index + 1), GFP_KERNEL);
697 if (!edev->attrs_muex) {
698 ret = -ENOMEM;
699 goto err_muex;
700 }
701
702 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
703 index, GFP_KERNEL);
704 if (!edev->d_attrs_muex) {
705 ret = -ENOMEM;
706 kfree(edev->attrs_muex);
707 goto err_muex;
708 }
709
710 for (index = 0; edev->mutually_exclusive[index]; index++) {
711 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
712 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
713 GFP_KERNEL);
714 if (!name) {
715 for (index--; index >= 0; index--) {
716 kfree(edev->d_attrs_muex[index].attr.
717 name);
718 }
719 kfree(edev->d_attrs_muex);
720 kfree(edev->attrs_muex);
721 ret = -ENOMEM;
722 goto err_muex;
723 }
724 strcpy(name, buf);
725 edev->d_attrs_muex[index].attr.name = name;
726 edev->d_attrs_muex[index].attr.mode = 0000;
727 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
728 .attr;
729 }
730 edev->attr_g_muex.name = muex_name;
731 edev->attr_g_muex.attrs = edev->attrs_muex;
732
733 }
734
735 if (edev->max_supported) {
736 edev->extcon_dev_type.groups =
737 kzalloc(sizeof(struct attribute_group *) *
738 (edev->max_supported + 2), GFP_KERNEL);
739 if (!edev->extcon_dev_type.groups) {
740 ret = -ENOMEM;
741 goto err_alloc_groups;
742 }
743
744 edev->extcon_dev_type.name = dev_name(edev->dev);
745 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
746
747 for (index = 0; index < edev->max_supported; index++)
748 edev->extcon_dev_type.groups[index] =
749 &edev->cables[index].attr_g;
750 if (edev->mutually_exclusive)
751 edev->extcon_dev_type.groups[index] =
752 &edev->attr_g_muex;
753
754 edev->dev->type = &edev->extcon_dev_type;
755 }
756
757 ret = device_register(edev->dev);
758 if (ret) {
759 put_device(edev->dev);
760 goto err_dev;
761 }
762#if defined(CONFIG_ANDROID)
763 if (switch_class)
764 ret = class_compat_create_link(switch_class, edev->dev,
765 dev);
766#endif /* CONFIG_ANDROID */
767
768 spin_lock_init(&edev->lock);
769
770 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
771
772 dev_set_drvdata(edev->dev, edev);
773 edev->state = 0;
774
775 mutex_lock(&extcon_dev_list_lock);
776 list_add(&edev->entry, &extcon_dev_list);
777 mutex_unlock(&extcon_dev_list_lock);
778
779 return 0;
780
781err_dev:
782 if (edev->max_supported)
783 kfree(edev->extcon_dev_type.groups);
784err_alloc_groups:
785 if (edev->max_supported && edev->mutually_exclusive) {
786 for (index = 0; edev->mutually_exclusive[index]; index++)
787 kfree(edev->d_attrs_muex[index].attr.name);
788 kfree(edev->d_attrs_muex);
789 kfree(edev->attrs_muex);
790 }
791err_muex:
792 for (index = 0; index < edev->max_supported; index++)
793 kfree(edev->cables[index].attr_g.name);
794err_alloc_cables:
795 if (edev->max_supported)
796 kfree(edev->cables);
797err_sysfs_alloc:
798 kfree(edev->dev);
799 return ret;
800}
801EXPORT_SYMBOL_GPL(extcon_dev_register);
802
803/**
804 * extcon_dev_unregister() - Unregister the extcon device.
805 * @edev: the extcon device instance to be unregitered.
806 *
807 * Note that this does not call kfree(edev) because edev was not allocated
808 * by this class.
809 */
810void extcon_dev_unregister(struct extcon_dev *edev)
811{
812 extcon_cleanup(edev, false);
813}
814EXPORT_SYMBOL_GPL(extcon_dev_unregister);
815
816static int __init extcon_class_init(void)
817{
818 return create_extcon_class();
819}
820module_init(extcon_class_init);
821
822static void __exit extcon_class_exit(void)
823{
824 class_destroy(extcon_class);
825}
826module_exit(extcon_class_exit);
827
828MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
829MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
830MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
831MODULE_DESCRIPTION("External connector (extcon) class driver");
832MODULE_LICENSE("GPL");
diff --git a/drivers/extcon/extcon_gpio.c b/drivers/extcon/extcon_gpio.c
new file mode 100644
index 000000000000..fe7a07b47336
--- /dev/null
+++ b/drivers/extcon/extcon_gpio.c
@@ -0,0 +1,169 @@
1/*
2 * drivers/extcon/extcon_gpio.c
3 *
4 * Single-state GPIO extcon driver based on extcon class
5 *
6 * Copyright (C) 2008 Google, Inc.
7 * Author: Mike Lockwood <lockwood@android.com>
8 *
9 * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
10 * (originally switch class is supported)
11 *
12 * This software is licensed under the terms of the GNU General Public
13 * License version 2, as published by the Free Software Foundation, and
14 * may be copied, distributed, and modified under those terms.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21*/
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/platform_device.h>
28#include <linux/slab.h>
29#include <linux/extcon.h>
30#include <linux/workqueue.h>
31#include <linux/gpio.h>
32#include <linux/extcon.h>
33#include <linux/extcon/extcon_gpio.h>
34
35struct gpio_extcon_data {
36 struct extcon_dev edev;
37 unsigned gpio;
38 const char *state_on;
39 const char *state_off;
40 int irq;
41 struct delayed_work work;
42 unsigned long debounce_jiffies;
43};
44
45static void gpio_extcon_work(struct work_struct *work)
46{
47 int state;
48 struct gpio_extcon_data *data =
49 container_of(to_delayed_work(work), struct gpio_extcon_data,
50 work);
51
52 state = gpio_get_value(data->gpio);
53 extcon_set_state(&data->edev, state);
54}
55
56static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
57{
58 struct gpio_extcon_data *extcon_data = dev_id;
59
60 schedule_delayed_work(&extcon_data->work,
61 extcon_data->debounce_jiffies);
62 return IRQ_HANDLED;
63}
64
65static ssize_t extcon_gpio_print_state(struct extcon_dev *edev, char *buf)
66{
67 struct gpio_extcon_data *extcon_data =
68 container_of(edev, struct gpio_extcon_data, edev);
69 const char *state;
70 if (extcon_get_state(edev))
71 state = extcon_data->state_on;
72 else
73 state = extcon_data->state_off;
74
75 if (state)
76 return sprintf(buf, "%s\n", state);
77 return -EINVAL;
78}
79
80static int __devinit gpio_extcon_probe(struct platform_device *pdev)
81{
82 struct gpio_extcon_platform_data *pdata = pdev->dev.platform_data;
83 struct gpio_extcon_data *extcon_data;
84 int ret = 0;
85
86 if (!pdata)
87 return -EBUSY;
88 if (!pdata->irq_flags) {
89 dev_err(&pdev->dev, "IRQ flag is not specified.\n");
90 return -EINVAL;
91 }
92
93 extcon_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
94 GFP_KERNEL);
95 if (!extcon_data)
96 return -ENOMEM;
97
98 extcon_data->edev.name = pdata->name;
99 extcon_data->gpio = pdata->gpio;
100 extcon_data->state_on = pdata->state_on;
101 extcon_data->state_off = pdata->state_off;
102 if (pdata->state_on && pdata->state_off)
103 extcon_data->edev.print_state = extcon_gpio_print_state;
104 extcon_data->debounce_jiffies = msecs_to_jiffies(pdata->debounce);
105
106 ret = extcon_dev_register(&extcon_data->edev, &pdev->dev);
107 if (ret < 0)
108 goto err_extcon_dev_register;
109
110 ret = gpio_request_one(extcon_data->gpio, GPIOF_DIR_IN, pdev->name);
111 if (ret < 0)
112 goto err_request_gpio;
113
114 INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
115
116 extcon_data->irq = gpio_to_irq(extcon_data->gpio);
117 if (extcon_data->irq < 0) {
118 ret = extcon_data->irq;
119 goto err_detect_irq_num_failed;
120 }
121
122 ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
123 pdata->irq_flags, pdev->name,
124 extcon_data);
125 if (ret < 0)
126 goto err_request_irq;
127
128 /* Perform initial detection */
129 gpio_extcon_work(&extcon_data->work.work);
130
131 return 0;
132
133err_request_irq:
134err_detect_irq_num_failed:
135 gpio_free(extcon_data->gpio);
136err_request_gpio:
137 extcon_dev_unregister(&extcon_data->edev);
138err_extcon_dev_register:
139 devm_kfree(&pdev->dev, extcon_data);
140
141 return ret;
142}
143
144static int __devexit gpio_extcon_remove(struct platform_device *pdev)
145{
146 struct gpio_extcon_data *extcon_data = platform_get_drvdata(pdev);
147
148 cancel_delayed_work_sync(&extcon_data->work);
149 gpio_free(extcon_data->gpio);
150 extcon_dev_unregister(&extcon_data->edev);
151 devm_kfree(&pdev->dev, extcon_data);
152
153 return 0;
154}
155
156static struct platform_driver gpio_extcon_driver = {
157 .probe = gpio_extcon_probe,
158 .remove = __devexit_p(gpio_extcon_remove),
159 .driver = {
160 .name = "extcon-gpio",
161 .owner = THIS_MODULE,
162 },
163};
164
165module_platform_driver(gpio_extcon_driver);
166
167MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
168MODULE_DESCRIPTION("GPIO extcon driver");
169MODULE_LICENSE("GPL");