extcon: Add documentation for EXTCON_CHG_USB_SLOW/FAST
[linux-2.6-block.git] / drivers / extcon / extcon.c
CommitLineData
de55d871 1/*
b9ec23c0 2 * drivers/extcon/extcon.c - External Connector (extcon) framework.
de55d871
MH
3 *
4 * External connector (extcon) class driver
5 *
2a9de9c0
CC
6 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 *
de55d871
MH
9 * Copyright (C) 2012 Samsung Electronics
10 * Author: Donggeun Kim <dg77.kim@samsung.com>
11 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
12 *
13 * based on android/drivers/switch/switch_class.c
14 * Copyright (C) 2008 Google, Inc.
15 * Author: Mike Lockwood <lockwood@android.com>
16 *
17 * This software is licensed under the terms of the GNU General Public
18 * License version 2, as published by the Free Software Foundation, and
19 * may be copied, distributed, and modified under those terms.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
b9ec23c0 25 */
de55d871
MH
26
27#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/init.h>
30#include <linux/device.h>
31#include <linux/fs.h>
32#include <linux/err.h>
f841afb1 33#include <linux/of.h>
de55d871 34#include <linux/slab.h>
9baf3220 35#include <linux/sysfs.h>
de55d871 36
e6cf0465
CC
37#include "extcon.h"
38
2a9de9c0
CC
39#define SUPPORTED_CABLE_MAX 32
40#define CABLE_NAME_MAX 30
41
55e4e2f1
CC
42struct __extcon_info {
43 unsigned int type;
44 unsigned int id;
45 const char *name;
46
47} extcon_info[] = {
48 [EXTCON_NONE] = {
49 .type = EXTCON_TYPE_MISC,
50 .id = EXTCON_NONE,
51 .name = "NONE",
52 },
73b6ecdb 53
8e9bc36d 54 /* USB external connector */
55e4e2f1
CC
55 [EXTCON_USB] = {
56 .type = EXTCON_TYPE_USB,
57 .id = EXTCON_USB,
58 .name = "USB",
59 },
60 [EXTCON_USB_HOST] = {
61 .type = EXTCON_TYPE_USB,
62 .id = EXTCON_USB_HOST,
63 .name = "USB_HOST",
64 },
8e9bc36d 65
11eecf91 66 /* Charging external connector */
55e4e2f1
CC
67 [EXTCON_CHG_USB_SDP] = {
68 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
69 .id = EXTCON_CHG_USB_SDP,
70 .name = "SDP",
71 },
72 [EXTCON_CHG_USB_DCP] = {
73 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
74 .id = EXTCON_CHG_USB_DCP,
75 .name = "DCP",
76 },
77 [EXTCON_CHG_USB_CDP] = {
78 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
79 .id = EXTCON_CHG_USB_CDP,
80 .name = "CDP",
81 },
82 [EXTCON_CHG_USB_ACA] = {
83 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
84 .id = EXTCON_CHG_USB_ACA,
85 .name = "ACA",
86 },
87 [EXTCON_CHG_USB_FAST] = {
88 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
89 .id = EXTCON_CHG_USB_FAST,
90 .name = "FAST-CHARGER",
91 },
92 [EXTCON_CHG_USB_SLOW] = {
93 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
94 .id = EXTCON_CHG_USB_SLOW,
95 .name = "SLOW-CHARGER",
96 },
7fe95fb8
CC
97 [EXTCON_CHG_WPT] = {
98 .type = EXTCON_TYPE_CHG,
99 .id = EXTCON_CHG_WPT,
100 .name = "WPT",
101 },
8e9bc36d 102
11eecf91 103 /* Jack external connector */
55e4e2f1
CC
104 [EXTCON_JACK_MICROPHONE] = {
105 .type = EXTCON_TYPE_JACK,
106 .id = EXTCON_JACK_MICROPHONE,
107 .name = "MICROPHONE",
108 },
109 [EXTCON_JACK_HEADPHONE] = {
110 .type = EXTCON_TYPE_JACK,
111 .id = EXTCON_JACK_HEADPHONE,
112 .name = "HEADPHONE",
113 },
114 [EXTCON_JACK_LINE_IN] = {
115 .type = EXTCON_TYPE_JACK,
116 .id = EXTCON_JACK_LINE_IN,
117 .name = "LINE-IN",
118 },
119 [EXTCON_JACK_LINE_OUT] = {
120 .type = EXTCON_TYPE_JACK,
121 .id = EXTCON_JACK_LINE_OUT,
122 .name = "LINE-OUT",
123 },
124 [EXTCON_JACK_VIDEO_IN] = {
125 .type = EXTCON_TYPE_JACK,
126 .id = EXTCON_JACK_VIDEO_IN,
127 .name = "VIDEO-IN",
128 },
129 [EXTCON_JACK_VIDEO_OUT] = {
130 .type = EXTCON_TYPE_JACK,
131 .id = EXTCON_JACK_VIDEO_OUT,
132 .name = "VIDEO-OUT",
133 },
134 [EXTCON_JACK_SPDIF_IN] = {
135 .type = EXTCON_TYPE_JACK,
136 .id = EXTCON_JACK_SPDIF_IN,
137 .name = "SPDIF-IN",
138 },
139 [EXTCON_JACK_SPDIF_OUT] = {
140 .type = EXTCON_TYPE_JACK,
141 .id = EXTCON_JACK_SPDIF_OUT,
142 .name = "SPDIF-OUT",
143 },
8e9bc36d 144
11eecf91 145 /* Display external connector */
55e4e2f1
CC
146 [EXTCON_DISP_HDMI] = {
147 .type = EXTCON_TYPE_DISP,
148 .id = EXTCON_DISP_HDMI,
149 .name = "HDMI",
150 },
151 [EXTCON_DISP_MHL] = {
152 .type = EXTCON_TYPE_DISP,
153 .id = EXTCON_DISP_MHL,
154 .name = "MHL",
155 },
156 [EXTCON_DISP_DVI] = {
157 .type = EXTCON_TYPE_DISP,
158 .id = EXTCON_DISP_DVI,
159 .name = "DVI",
160 },
161 [EXTCON_DISP_VGA] = {
162 .type = EXTCON_TYPE_DISP,
163 .id = EXTCON_DISP_VGA,
164 .name = "VGA",
165 },
2164188d
CZ
166 [EXTCON_DISP_DP] = {
167 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
168 .id = EXTCON_DISP_DP,
169 .name = "DP",
170 },
9c0595d6
CC
171 [EXTCON_DISP_HMD] = {
172 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
173 .id = EXTCON_DISP_HMD,
174 .name = "HMD",
175 },
8e9bc36d 176
11eecf91 177 /* Miscellaneous external connector */
55e4e2f1
CC
178 [EXTCON_DOCK] = {
179 .type = EXTCON_TYPE_MISC,
180 .id = EXTCON_DOCK,
181 .name = "DOCK",
182 },
183 [EXTCON_JIG] = {
184 .type = EXTCON_TYPE_MISC,
185 .id = EXTCON_JIG,
186 .name = "JIG",
187 },
188 [EXTCON_MECHANICAL] = {
189 .type = EXTCON_TYPE_MISC,
190 .id = EXTCON_MECHANICAL,
191 .name = "MECHANICAL",
192 },
193
194 { /* sentinel */ }
806d9dd7
MH
195};
196
20f7b53d
CC
197/**
198 * struct extcon_cable - An internal data for each cable of extcon device.
199 * @edev: The extcon device
200 * @cable_index: Index of this cable in the edev
201 * @attr_g: Attribute group for the cable
202 * @attr_name: "name" sysfs entry
203 * @attr_state: "state" sysfs entry
204 * @attrs: Array pointing to attr_name and attr_state for attr_g
205 */
206struct extcon_cable {
207 struct extcon_dev *edev;
208 int cable_index;
209
210 struct attribute_group attr_g;
211 struct device_attribute attr_name;
212 struct device_attribute attr_state;
213
214 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
792e7e9e
CC
215
216 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
217 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
218 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
219 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
7f2a0a16
CC
220
221 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
222 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
223 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
224 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
20f7b53d
CC
225};
226
be3a07f7 227static struct class *extcon_class;
449a2bf5 228#if defined(CONFIG_ANDROID)
de55d871 229static struct class_compat *switch_class;
449a2bf5 230#endif /* CONFIG_ANDROID */
de55d871 231
74c5d09b
DK
232static LIST_HEAD(extcon_dev_list);
233static DEFINE_MUTEX(extcon_dev_list_lock);
234
bde68e60
MH
235/**
236 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
a75e1c73 237 * condition.
bde68e60
MH
238 * @edev: the extcon device
239 * @new_state: new cable attach status for @edev
240 *
241 * Returns 0 if nothing violates. Returns the index + 1 for the first
242 * violated condition.
243 */
244static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
245{
246 int i = 0;
247
248 if (!edev->mutually_exclusive)
249 return 0;
250
251 for (i = 0; edev->mutually_exclusive[i]; i++) {
28c0ada6 252 int weight;
bde68e60 253 u32 correspondants = new_state & edev->mutually_exclusive[i];
bde68e60 254
28c0ada6 255 /* calculate the total number of bits set */
256 weight = hweight32(correspondants);
257 if (weight > 1)
258 return i + 1;
bde68e60
MH
259 }
260
261 return 0;
262}
263
73b6ecdb 264static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
2a9de9c0
CC
265{
266 int i;
267
268 /* Find the the index of extcon cable in edev->supported_cable */
269 for (i = 0; i < edev->max_supported; i++) {
270 if (edev->supported_cable[i] == id)
271 return i;
272 }
273
274 return -EINVAL;
275}
276
792e7e9e
CC
277static int get_extcon_type(unsigned int prop)
278{
279 switch (prop) {
280 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
281 return EXTCON_TYPE_USB;
282 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
283 return EXTCON_TYPE_CHG;
284 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
285 return EXTCON_TYPE_JACK;
286 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
287 return EXTCON_TYPE_DISP;
288 default:
289 return -EINVAL;
290 }
291}
292
293static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
294{
295 return !!(edev->state & BIT(index));
296}
297
ab11af04
CC
298static bool is_extcon_changed(struct extcon_dev *edev, int index,
299 bool new_state)
046050f6 300{
ab11af04
CC
301 int state = !!(edev->state & BIT(index));
302 return (state != new_state);
046050f6
CC
303}
304
792e7e9e
CC
305static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
306{
307 int type;
308
309 /* Check whether the property is supported or not. */
310 type = get_extcon_type(prop);
311 if (type < 0)
312 return false;
313
314 /* Check whether a specific extcon id supports the property or not. */
315 return !!(extcon_info[id].type & type);
316}
317
7f2a0a16
CC
318static int is_extcon_property_capability(struct extcon_dev *edev,
319 unsigned int id, int index,unsigned int prop)
320{
321 struct extcon_cable *cable;
322 int type, ret;
323
324 /* Check whether the property is supported or not. */
325 type = get_extcon_type(prop);
326 if (type < 0)
327 return type;
328
329 cable = &edev->cables[index];
330
331 switch (type) {
332 case EXTCON_TYPE_USB:
333 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
334 break;
335 case EXTCON_TYPE_CHG:
336 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
337 break;
338 case EXTCON_TYPE_JACK:
339 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
340 break;
341 case EXTCON_TYPE_DISP:
342 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
343 break;
344 default:
345 ret = -EINVAL;
346 }
347
348 return ret;
349}
350
792e7e9e
CC
351static void init_property(struct extcon_dev *edev, unsigned int id, int index)
352{
353 unsigned int type = extcon_info[id].type;
354 struct extcon_cable *cable = &edev->cables[index];
355
356 if (EXTCON_TYPE_USB & type)
357 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
358 if (EXTCON_TYPE_CHG & type)
359 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
360 if (EXTCON_TYPE_JACK & type)
361 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
362 if (EXTCON_TYPE_DISP & type)
363 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
364}
365
de55d871
MH
366static ssize_t state_show(struct device *dev, struct device_attribute *attr,
367 char *buf)
368{
806d9dd7 369 int i, count = 0;
cb8bb3a7 370 struct extcon_dev *edev = dev_get_drvdata(dev);
de55d871 371
806d9dd7
MH
372 if (edev->max_supported == 0)
373 return sprintf(buf, "%u\n", edev->state);
374
2a9de9c0 375 for (i = 0; i < edev->max_supported; i++) {
806d9dd7 376 count += sprintf(buf + count, "%s=%d\n",
55e4e2f1 377 extcon_info[edev->supported_cable[i]].name,
806d9dd7
MH
378 !!(edev->state & (1 << i)));
379 }
380
381 return count;
382}
5d5321e9 383static DEVICE_ATTR_RO(state);
de55d871
MH
384
385static ssize_t name_show(struct device *dev, struct device_attribute *attr,
386 char *buf)
387{
cb8bb3a7 388 struct extcon_dev *edev = dev_get_drvdata(dev);
de55d871 389
71c3ffa5 390 return sprintf(buf, "%s\n", edev->name);
de55d871 391}
af01da0e 392static DEVICE_ATTR_RO(name);
de55d871 393
806d9dd7
MH
394static ssize_t cable_name_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
396{
397 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
398 attr_name);
2a9de9c0 399 int i = cable->cable_index;
806d9dd7
MH
400
401 return sprintf(buf, "%s\n",
55e4e2f1 402 extcon_info[cable->edev->supported_cable[i]].name);
806d9dd7
MH
403}
404
405static ssize_t cable_state_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
409 attr_state);
410
be052cc8
RQ
411 int i = cable->cable_index;
412
806d9dd7 413 return sprintf(buf, "%d\n",
575c2b86 414 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
806d9dd7
MH
415}
416
de55d871 417/**
ab11af04
CC
418 * extcon_sync() - Synchronize the states for both the attached/detached
419 * @edev: the extcon device that has the cable.
74c5d09b 420 *
ab11af04
CC
421 * This function send a notification to synchronize the all states of a
422 * specific external connector
de55d871 423 */
ab11af04 424int extcon_sync(struct extcon_dev *edev, unsigned int id)
de55d871
MH
425{
426 char name_buf[120];
427 char state_buf[120];
428 char *prop_buf;
429 char *envp[3];
430 int env_offset = 0;
431 int length;
046050f6 432 int index;
ab11af04 433 int state;
806d9dd7 434 unsigned long flags;
de55d871 435
7eae43ae
CC
436 if (!edev)
437 return -EINVAL;
438
ab11af04
CC
439 index = find_cable_index_by_id(edev, id);
440 if (index < 0)
441 return index;
442
806d9dd7 443 spin_lock_irqsave(&edev->lock, flags);
de55d871 444
ab11af04
CC
445 state = !!(edev->state & BIT(index));
446 raw_notifier_call_chain(&edev->nh[index], state, edev);
447
448 /* This could be in interrupt handler */
449 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
450 if (!prop_buf) {
451 /* Unlock early before uevent */
806d9dd7 452 spin_unlock_irqrestore(&edev->lock, flags);
ab11af04
CC
453
454 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
455 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
456
e7d9dd5a 457 return -ENOMEM;
ab11af04
CC
458 }
459
460 length = name_show(&edev->dev, NULL, prop_buf);
461 if (length > 0) {
462 if (prop_buf[length - 1] == '\n')
463 prop_buf[length - 1] = 0;
464 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
465 envp[env_offset++] = name_buf;
466 }
467
468 length = state_show(&edev->dev, NULL, prop_buf);
469 if (length > 0) {
470 if (prop_buf[length - 1] == '\n')
471 prop_buf[length - 1] = 0;
472 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
473 envp[env_offset++] = state_buf;
de55d871 474 }
ab11af04
CC
475 envp[env_offset] = NULL;
476
477 /* Unlock early before uevent */
478 spin_unlock_irqrestore(&edev->lock, flags);
479 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
480 free_page((unsigned long)prop_buf);
bde68e60
MH
481
482 return 0;
de55d871 483}
ab11af04 484EXPORT_SYMBOL_GPL(extcon_sync);
de55d871 485
806d9dd7 486/**
575c2b86 487 * extcon_get_state() - Get the state of a external connector.
806d9dd7 488 * @edev: the extcon device that has the cable.
2a9de9c0 489 * @id: the unique id of each external connector in extcon enumeration.
806d9dd7 490 */
575c2b86 491int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
806d9dd7 492{
575c2b86
CC
493 int index, state;
494 unsigned long flags;
806d9dd7 495
7eae43ae
CC
496 if (!edev)
497 return -EINVAL;
498
2a9de9c0
CC
499 index = find_cable_index_by_id(edev, id);
500 if (index < 0)
501 return index;
806d9dd7 502
575c2b86
CC
503 spin_lock_irqsave(&edev->lock, flags);
504 state = is_extcon_attached(edev, index);
505 spin_unlock_irqrestore(&edev->lock, flags);
806d9dd7 506
575c2b86 507 return state;
806d9dd7 508}
575c2b86 509EXPORT_SYMBOL_GPL(extcon_get_state);
806d9dd7 510
806d9dd7 511/**
575c2b86 512 * extcon_set_state() - Set the state of a external connector.
ab11af04 513 * without a notification.
a75e1c73 514 * @edev: the extcon device that has the cable.
2a9de9c0
CC
515 * @id: the unique id of each external connector
516 * in extcon enumeration.
517 * @state: the new cable status. The default semantics is
806d9dd7 518 * true: attached / false: detached.
ab11af04
CC
519 *
520 * This function only set the state of a external connector without
521 * a notification. To synchronize the data of a external connector,
522 * use extcon_set_state_sync() and extcon_sync().
806d9dd7 523 */
575c2b86 524int extcon_set_state(struct extcon_dev *edev, unsigned int id,
2a9de9c0 525 bool cable_state)
806d9dd7 526{
ab11af04
CC
527 unsigned long flags;
528 int index, ret = 0;
806d9dd7 529
7eae43ae
CC
530 if (!edev)
531 return -EINVAL;
532
2a9de9c0
CC
533 index = find_cable_index_by_id(edev, id);
534 if (index < 0)
535 return index;
536
ab11af04
CC
537 spin_lock_irqsave(&edev->lock, flags);
538
539 /* Check whether the external connector's state is changed. */
540 if (!is_extcon_changed(edev, index, cable_state))
541 goto out;
542
543 if (check_mutually_exclusive(edev,
544 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
545 ret = -EPERM;
546 goto out;
547 }
548
792e7e9e
CC
549 /*
550 * Initialize the value of extcon property before setting
551 * the detached state for an external connector.
552 */
553 if (!cable_state)
554 init_property(edev, id, index);
555
ab11af04
CC
556 /* Update the state for a external connector. */
557 if (cable_state)
558 edev->state |= BIT(index);
559 else
560 edev->state &= ~(BIT(index));
561out:
562 spin_unlock_irqrestore(&edev->lock, flags);
563
564 return ret;
806d9dd7 565}
575c2b86 566EXPORT_SYMBOL_GPL(extcon_set_state);
806d9dd7 567
ab11af04
CC
568/**
569 * extcon_set_state_sync() - Set the state of a external connector
570 * with a notification.
571 * @edev: the extcon device that has the cable.
572 * @id: the unique id of each external connector
573 * in extcon enumeration.
574 * @state: the new cable status. The default semantics is
575 * true: attached / false: detached.
576 *
577 * This function set the state of external connector and synchronize the data
578 * by usning a notification.
579 */
580int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
581 bool cable_state)
582{
583 int ret, index;
584 unsigned long flags;
585
586 index = find_cable_index_by_id(edev, id);
587 if (index < 0)
588 return index;
589
590 /* Check whether the external connector's state is changed. */
591 spin_lock_irqsave(&edev->lock, flags);
592 ret = is_extcon_changed(edev, index, cable_state);
593 spin_unlock_irqrestore(&edev->lock, flags);
594 if (!ret)
595 return 0;
596
597 ret = extcon_set_state(edev, id, cable_state);
598 if (ret < 0)
599 return ret;
600
601 return extcon_sync(edev, id);
602}
603EXPORT_SYMBOL_GPL(extcon_set_state_sync);
604
792e7e9e
CC
605/**
606 * extcon_get_property() - Get the property value of a specific cable.
607 * @edev: the extcon device that has the cable.
608 * @id: the unique id of each external connector
609 * in extcon enumeration.
610 * @prop: the property id among enum extcon_property.
611 * @prop_val: the pointer which store the value of property.
612 *
613 * When getting the property value of external connector, the external connector
614 * should be attached. If detached state, function just return 0 without
615 * property value. Also, the each property should be included in the list of
616 * supported properties according to the type of external connectors.
617 *
618 * Returns 0 if success or error number if fail
619 */
620int extcon_get_property(struct extcon_dev *edev, unsigned int id,
621 unsigned int prop,
622 union extcon_property_value *prop_val)
623{
624 struct extcon_cable *cable;
625 unsigned long flags;
626 int index, ret = 0;
627
628 *prop_val = (union extcon_property_value)(0);
629
630 if (!edev)
631 return -EINVAL;
632
633 /* Check whether the property is supported or not */
634 if (!is_extcon_property_supported(id, prop))
635 return -EINVAL;
636
637 /* Find the cable index of external connector by using id */
638 index = find_cable_index_by_id(edev, id);
639 if (index < 0)
640 return index;
641
642 spin_lock_irqsave(&edev->lock, flags);
643
7f2a0a16
CC
644 /* Check whether the property is available or not. */
645 if (!is_extcon_property_capability(edev, id, index, prop)) {
646 spin_unlock_irqrestore(&edev->lock, flags);
647 return -EPERM;
648 }
649
792e7e9e
CC
650 /*
651 * Check whether the external connector is attached.
652 * If external connector is detached, the user can not
653 * get the property value.
654 */
655 if (!is_extcon_attached(edev, index)) {
656 spin_unlock_irqrestore(&edev->lock, flags);
657 return 0;
658 }
659
660 cable = &edev->cables[index];
661
662 /* Get the property value according to extcon type */
663 switch (prop) {
664 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
665 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
666 break;
667 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
668 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
669 break;
670 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
671 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
672 break;
673 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
674 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
675 break;
676 default:
677 ret = -EINVAL;
678 break;
679 }
680
681 spin_unlock_irqrestore(&edev->lock, flags);
682
683 return ret;
684}
685EXPORT_SYMBOL_GPL(extcon_get_property);
686
687/**
688 * extcon_set_property() - Set the property value of a specific cable.
689 * @edev: the extcon device that has the cable.
690 * @id: the unique id of each external connector
691 * in extcon enumeration.
692 * @prop: the property id among enum extcon_property.
693 * @prop_val: the pointer including the new value of property.
694 *
695 * The each property should be included in the list of supported properties
696 * according to the type of external connectors.
697 *
698 * Returns 0 if success or error number if fail
699 */
700int extcon_set_property(struct extcon_dev *edev, unsigned int id,
701 unsigned int prop,
702 union extcon_property_value prop_val)
703{
704 struct extcon_cable *cable;
705 unsigned long flags;
706 int index, ret = 0;
707
708 if (!edev)
709 return -EINVAL;
710
711 /* Check whether the property is supported or not */
712 if (!is_extcon_property_supported(id, prop))
713 return -EINVAL;
714
715 /* Find the cable index of external connector by using id */
716 index = find_cable_index_by_id(edev, id);
717 if (index < 0)
718 return index;
719
720 spin_lock_irqsave(&edev->lock, flags);
721
7f2a0a16
CC
722 /* Check whether the property is available or not. */
723 if (!is_extcon_property_capability(edev, id, index, prop)) {
724 spin_unlock_irqrestore(&edev->lock, flags);
725 return -EPERM;
726 }
727
792e7e9e
CC
728 cable = &edev->cables[index];
729
730 /* Set the property value according to extcon type */
731 switch (prop) {
732 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
733 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
734 break;
735 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
736 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
737 break;
738 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
739 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
740 break;
741 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
742 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
743 break;
744 default:
745 ret = -EINVAL;
746 break;
747 }
748
749 spin_unlock_irqrestore(&edev->lock, flags);
750
751 return ret;
752}
753EXPORT_SYMBOL_GPL(extcon_set_property);
754
ab11af04
CC
755/**
756 * extcon_set_property_sync() - Set the property value of a specific cable
757 with a notification.
758 * @prop_val: the pointer including the new value of property.
759 *
760 * When setting the property value of external connector, the external connector
761 * should be attached. The each property should be included in the list of
762 * supported properties according to the type of external connectors.
763 *
764 * Returns 0 if success or error number if fail
765 */
766int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
767 unsigned int prop,
768 union extcon_property_value prop_val)
769{
770 int ret;
771
772 ret = extcon_set_property(edev, id, prop, prop_val);
773 if (ret < 0)
774 return ret;
775
776 return extcon_sync(edev, id);
777}
778EXPORT_SYMBOL_GPL(extcon_set_property_sync);
779
7f2a0a16
CC
780/**
781 * extcon_get_property_capability() - Get the capability of property
782 * of an external connector.
783 * @edev: the extcon device that has the cable.
784 * @id: the unique id of each external connector
785 * in extcon enumeration.
786 * @prop: the property id among enum extcon_property.
787 *
788 * Returns 1 if the property is available or 0 if not available.
789 */
790int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
791 unsigned int prop)
792{
793 int index;
794
795 if (!edev)
796 return -EINVAL;
797
798 /* Check whether the property is supported or not */
799 if (!is_extcon_property_supported(id, prop))
800 return -EINVAL;
801
802 /* Find the cable index of external connector by using id */
803 index = find_cable_index_by_id(edev, id);
804 if (index < 0)
805 return index;
806
807 return is_extcon_property_capability(edev, id, index, prop);
808}
809EXPORT_SYMBOL_GPL(extcon_get_property_capability);
810
811/**
812 * extcon_set_property_capability() - Set the capability of a property
813 * of an external connector.
814 * @edev: the extcon device that has the cable.
815 * @id: the unique id of each external connector
816 * in extcon enumeration.
817 * @prop: the property id among enum extcon_property.
818 *
819 * This function set the capability of a property for an external connector
820 * to mark the bit in capability bitmap which mean the available state of
821 * a property.
822 *
823 * Returns 0 if success or error number if fail
824 */
825int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
826 unsigned int prop)
827{
828 struct extcon_cable *cable;
829 int index, type, ret = 0;
830
831 if (!edev)
832 return -EINVAL;
833
834 /* Check whether the property is supported or not. */
835 if (!is_extcon_property_supported(id, prop))
836 return -EINVAL;
837
838 /* Find the cable index of external connector by using id. */
839 index = find_cable_index_by_id(edev, id);
840 if (index < 0)
841 return index;
842
843 type = get_extcon_type(prop);
844 if (type < 0)
845 return type;
846
847 cable = &edev->cables[index];
848
849 switch (type) {
850 case EXTCON_TYPE_USB:
851 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
852 break;
853 case EXTCON_TYPE_CHG:
854 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
855 break;
856 case EXTCON_TYPE_JACK:
857 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
858 break;
859 case EXTCON_TYPE_DISP:
860 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
861 break;
862 default:
863 ret = -EINVAL;
864 }
865
866 return ret;
867}
868EXPORT_SYMBOL_GPL(extcon_set_property_capability);
869
74c5d09b
DK
870/**
871 * extcon_get_extcon_dev() - Get the extcon device instance from the name
872 * @extcon_name: The extcon name provided with extcon_dev_register()
873 */
874struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
875{
876 struct extcon_dev *sd;
877
7eae43ae
CC
878 if (!extcon_name)
879 return ERR_PTR(-EINVAL);
880
74c5d09b
DK
881 mutex_lock(&extcon_dev_list_lock);
882 list_for_each_entry(sd, &extcon_dev_list, entry) {
883 if (!strcmp(sd->name, extcon_name))
884 goto out;
885 }
886 sd = NULL;
887out:
888 mutex_unlock(&extcon_dev_list_lock);
889 return sd;
890}
891EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
892
893/**
c338bb03 894 * extcon_register_notifier() - Register a notifiee to get notified by
a75e1c73 895 * any attach status changes from the extcon.
046050f6
CC
896 * @edev: the extcon device that has the external connecotr.
897 * @id: the unique id of each external connector in extcon enumeration.
74c5d09b 898 * @nb: a notifier block to be registered.
806d9dd7
MH
899 *
900 * Note that the second parameter given to the callback of nb (val) is
901 * "old_state", not the current state. The current state can be retrieved
902 * by looking at the third pameter (edev pointer)'s state value.
74c5d09b 903 */
73b6ecdb 904int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
046050f6 905 struct notifier_block *nb)
74c5d09b 906{
66bee35f 907 unsigned long flags;
2c8116a1 908 int ret, idx = -EINVAL;
046050f6 909
01b4c9a1 910 if (!edev || !nb)
7eae43ae
CC
911 return -EINVAL;
912
01b4c9a1
CC
913 idx = find_cable_index_by_id(edev, id);
914 if (idx < 0)
915 return idx;
66bee35f 916
01b4c9a1
CC
917 spin_lock_irqsave(&edev->lock, flags);
918 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
919 spin_unlock_irqrestore(&edev->lock, flags);
66bee35f
HG
920
921 return ret;
74c5d09b
DK
922}
923EXPORT_SYMBOL_GPL(extcon_register_notifier);
924
925/**
c338bb03 926 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
046050f6
CC
927 * @edev: the extcon device that has the external connecotr.
928 * @id: the unique id of each external connector in extcon enumeration.
929 * @nb: a notifier block to be registered.
74c5d09b 930 */
73b6ecdb 931int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
046050f6 932 struct notifier_block *nb)
74c5d09b 933{
66bee35f 934 unsigned long flags;
046050f6
CC
935 int ret, idx;
936
7eae43ae
CC
937 if (!edev || !nb)
938 return -EINVAL;
939
046050f6 940 idx = find_cable_index_by_id(edev, id);
a05f44c8
SB
941 if (idx < 0)
942 return idx;
66bee35f
HG
943
944 spin_lock_irqsave(&edev->lock, flags);
046050f6 945 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
66bee35f
HG
946 spin_unlock_irqrestore(&edev->lock, flags);
947
948 return ret;
74c5d09b
DK
949}
950EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
951
af01da0e
GKH
952static struct attribute *extcon_attrs[] = {
953 &dev_attr_state.attr,
954 &dev_attr_name.attr,
955 NULL,
de55d871 956};
af01da0e 957ATTRIBUTE_GROUPS(extcon);
de55d871
MH
958
959static int create_extcon_class(void)
960{
961 if (!extcon_class) {
962 extcon_class = class_create(THIS_MODULE, "extcon");
963 if (IS_ERR(extcon_class))
964 return PTR_ERR(extcon_class);
af01da0e 965 extcon_class->dev_groups = extcon_groups;
de55d871 966
449a2bf5 967#if defined(CONFIG_ANDROID)
de55d871
MH
968 switch_class = class_compat_register("switch");
969 if (WARN(!switch_class, "cannot allocate"))
970 return -ENOMEM;
449a2bf5 971#endif /* CONFIG_ANDROID */
de55d871
MH
972 }
973
974 return 0;
975}
976
de55d871
MH
977static void extcon_dev_release(struct device *dev)
978{
de55d871
MH
979}
980
bde68e60 981static const char *muex_name = "mutually_exclusive";
806d9dd7
MH
982static void dummy_sysfs_dev_release(struct device *dev)
983{
984}
985
a9af6522
CC
986/*
987 * extcon_dev_allocate() - Allocate the memory of extcon device.
2a9de9c0 988 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
a9af6522
CC
989 * If supported_cable is NULL, cable name related APIs
990 * are disabled.
991 *
992 * This function allocates the memory for extcon device without allocating
993 * memory in each extcon provider driver and initialize default setting for
994 * extcon device.
995 *
996 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
997 */
73b6ecdb 998struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
a9af6522
CC
999{
1000 struct extcon_dev *edev;
1001
7eae43ae
CC
1002 if (!supported_cable)
1003 return ERR_PTR(-EINVAL);
1004
a9af6522
CC
1005 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1006 if (!edev)
1007 return ERR_PTR(-ENOMEM);
1008
1009 edev->max_supported = 0;
1010 edev->supported_cable = supported_cable;
1011
1012 return edev;
1013}
1014
1015/*
1016 * extcon_dev_free() - Free the memory of extcon device.
1017 * @edev: the extcon device to free
1018 */
1019void extcon_dev_free(struct extcon_dev *edev)
1020{
1021 kfree(edev);
1022}
1023EXPORT_SYMBOL_GPL(extcon_dev_free);
1024
de55d871
MH
1025/**
1026 * extcon_dev_register() - Register a new extcon device
1027 * @edev : the new extcon device (should be allocated before calling)
de55d871
MH
1028 *
1029 * Among the members of edev struct, please set the "user initializing data"
1030 * in any case and set the "optional callbacks" if required. However, please
1031 * do not set the values of "internal data", which are initialized by
1032 * this function.
1033 */
42d7d753 1034int extcon_dev_register(struct extcon_dev *edev)
de55d871 1035{
806d9dd7 1036 int ret, index = 0;
71c3ffa5 1037 static atomic_t edev_no = ATOMIC_INIT(-1);
de55d871
MH
1038
1039 if (!extcon_class) {
1040 ret = create_extcon_class();
1041 if (ret < 0)
1042 return ret;
1043 }
1044
7eae43ae 1045 if (!edev || !edev->supported_cable)
2a9de9c0
CC
1046 return -EINVAL;
1047
1048 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
806d9dd7 1049
2a9de9c0 1050 edev->max_supported = index;
806d9dd7 1051 if (index > SUPPORTED_CABLE_MAX) {
2a9de9c0
CC
1052 dev_err(&edev->dev,
1053 "exceed the maximum number of supported cables\n");
806d9dd7
MH
1054 return -EINVAL;
1055 }
1056
dae61651
CC
1057 edev->dev.class = extcon_class;
1058 edev->dev.release = extcon_dev_release;
de55d871 1059
71c3ffa5 1060 edev->name = dev_name(edev->dev.parent);
42d7d753
CC
1061 if (IS_ERR_OR_NULL(edev->name)) {
1062 dev_err(&edev->dev,
1063 "extcon device name is null\n");
1064 return -EINVAL;
1065 }
71c3ffa5
CC
1066 dev_set_name(&edev->dev, "extcon%lu",
1067 (unsigned long)atomic_inc_return(&edev_no));
806d9dd7
MH
1068
1069 if (edev->max_supported) {
1070 char buf[10];
1071 char *str;
1072 struct extcon_cable *cable;
1073
1074 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1075 edev->max_supported, GFP_KERNEL);
1076 if (!edev->cables) {
1077 ret = -ENOMEM;
1078 goto err_sysfs_alloc;
1079 }
1080 for (index = 0; index < edev->max_supported; index++) {
1081 cable = &edev->cables[index];
1082
1083 snprintf(buf, 10, "cable.%d", index);
1084 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1085 GFP_KERNEL);
1086 if (!str) {
1087 for (index--; index >= 0; index--) {
1088 cable = &edev->cables[index];
1089 kfree(cable->attr_g.name);
1090 }
1091 ret = -ENOMEM;
1092
1093 goto err_alloc_cables;
1094 }
1095 strcpy(str, buf);
1096
1097 cable->edev = edev;
1098 cable->cable_index = index;
1099 cable->attrs[0] = &cable->attr_name.attr;
1100 cable->attrs[1] = &cable->attr_state.attr;
1101 cable->attrs[2] = NULL;
1102 cable->attr_g.name = str;
1103 cable->attr_g.attrs = cable->attrs;
1104
9baf3220 1105 sysfs_attr_init(&cable->attr_name.attr);
806d9dd7
MH
1106 cable->attr_name.attr.name = "name";
1107 cable->attr_name.attr.mode = 0444;
1108 cable->attr_name.show = cable_name_show;
1109
9baf3220 1110 sysfs_attr_init(&cable->attr_state.attr);
806d9dd7 1111 cable->attr_state.attr.name = "state";
ea9dd9d6 1112 cable->attr_state.attr.mode = 0444;
806d9dd7 1113 cable->attr_state.show = cable_state_show;
806d9dd7
MH
1114 }
1115 }
1116
bde68e60
MH
1117 if (edev->max_supported && edev->mutually_exclusive) {
1118 char buf[80];
1119 char *name;
1120
1121 /* Count the size of mutually_exclusive array */
1122 for (index = 0; edev->mutually_exclusive[index]; index++)
1123 ;
1124
1125 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1126 (index + 1), GFP_KERNEL);
1127 if (!edev->attrs_muex) {
1128 ret = -ENOMEM;
1129 goto err_muex;
1130 }
1131
1132 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1133 index, GFP_KERNEL);
1134 if (!edev->d_attrs_muex) {
1135 ret = -ENOMEM;
1136 kfree(edev->attrs_muex);
1137 goto err_muex;
1138 }
1139
1140 for (index = 0; edev->mutually_exclusive[index]; index++) {
1141 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1142 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1143 GFP_KERNEL);
1144 if (!name) {
1145 for (index--; index >= 0; index--) {
1146 kfree(edev->d_attrs_muex[index].attr.
1147 name);
1148 }
1149 kfree(edev->d_attrs_muex);
1150 kfree(edev->attrs_muex);
1151 ret = -ENOMEM;
1152 goto err_muex;
1153 }
1154 strcpy(name, buf);
9baf3220 1155 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
bde68e60
MH
1156 edev->d_attrs_muex[index].attr.name = name;
1157 edev->d_attrs_muex[index].attr.mode = 0000;
1158 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1159 .attr;
1160 }
1161 edev->attr_g_muex.name = muex_name;
1162 edev->attr_g_muex.attrs = edev->attrs_muex;
1163
1164 }
1165
806d9dd7
MH
1166 if (edev->max_supported) {
1167 edev->extcon_dev_type.groups =
1168 kzalloc(sizeof(struct attribute_group *) *
bde68e60 1169 (edev->max_supported + 2), GFP_KERNEL);
806d9dd7
MH
1170 if (!edev->extcon_dev_type.groups) {
1171 ret = -ENOMEM;
1172 goto err_alloc_groups;
1173 }
1174
dae61651 1175 edev->extcon_dev_type.name = dev_name(&edev->dev);
806d9dd7
MH
1176 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1177
1178 for (index = 0; index < edev->max_supported; index++)
1179 edev->extcon_dev_type.groups[index] =
1180 &edev->cables[index].attr_g;
bde68e60
MH
1181 if (edev->mutually_exclusive)
1182 edev->extcon_dev_type.groups[index] =
1183 &edev->attr_g_muex;
806d9dd7 1184
dae61651 1185 edev->dev.type = &edev->extcon_dev_type;
806d9dd7
MH
1186 }
1187
dae61651 1188 ret = device_register(&edev->dev);
de55d871 1189 if (ret) {
dae61651 1190 put_device(&edev->dev);
de55d871
MH
1191 goto err_dev;
1192 }
449a2bf5 1193#if defined(CONFIG_ANDROID)
de55d871 1194 if (switch_class)
dae61651 1195 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
449a2bf5 1196#endif /* CONFIG_ANDROID */
de55d871 1197
806d9dd7
MH
1198 spin_lock_init(&edev->lock);
1199
046050f6
CC
1200 edev->nh = devm_kzalloc(&edev->dev,
1201 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1202 if (!edev->nh) {
1203 ret = -ENOMEM;
1204 goto err_dev;
1205 }
1206
1207 for (index = 0; index < edev->max_supported; index++)
1208 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
74c5d09b 1209
dae61651 1210 dev_set_drvdata(&edev->dev, edev);
de55d871 1211 edev->state = 0;
74c5d09b
DK
1212
1213 mutex_lock(&extcon_dev_list_lock);
1214 list_add(&edev->entry, &extcon_dev_list);
1215 mutex_unlock(&extcon_dev_list_lock);
1216
de55d871
MH
1217 return 0;
1218
1219err_dev:
806d9dd7
MH
1220 if (edev->max_supported)
1221 kfree(edev->extcon_dev_type.groups);
1222err_alloc_groups:
bde68e60
MH
1223 if (edev->max_supported && edev->mutually_exclusive) {
1224 for (index = 0; edev->mutually_exclusive[index]; index++)
1225 kfree(edev->d_attrs_muex[index].attr.name);
1226 kfree(edev->d_attrs_muex);
1227 kfree(edev->attrs_muex);
1228 }
1229err_muex:
806d9dd7
MH
1230 for (index = 0; index < edev->max_supported; index++)
1231 kfree(edev->cables[index].attr_g.name);
1232err_alloc_cables:
1233 if (edev->max_supported)
1234 kfree(edev->cables);
1235err_sysfs_alloc:
de55d871
MH
1236 return ret;
1237}
1238EXPORT_SYMBOL_GPL(extcon_dev_register);
1239
1240/**
1241 * extcon_dev_unregister() - Unregister the extcon device.
c338bb03 1242 * @edev: the extcon device instance to be unregistered.
de55d871
MH
1243 *
1244 * Note that this does not call kfree(edev) because edev was not allocated
1245 * by this class.
1246 */
1247void extcon_dev_unregister(struct extcon_dev *edev)
1248{
57e7cd37 1249 int index;
1250
7eae43ae
CC
1251 if (!edev)
1252 return;
1253
57e7cd37 1254 mutex_lock(&extcon_dev_list_lock);
1255 list_del(&edev->entry);
1256 mutex_unlock(&extcon_dev_list_lock);
1257
dae61651
CC
1258 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1259 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1260 dev_name(&edev->dev));
57e7cd37 1261 return;
1262 }
1263
7585ca0d
WX
1264 device_unregister(&edev->dev);
1265
57e7cd37 1266 if (edev->mutually_exclusive && edev->max_supported) {
1267 for (index = 0; edev->mutually_exclusive[index];
1268 index++)
1269 kfree(edev->d_attrs_muex[index].attr.name);
1270 kfree(edev->d_attrs_muex);
1271 kfree(edev->attrs_muex);
1272 }
1273
1274 for (index = 0; index < edev->max_supported; index++)
1275 kfree(edev->cables[index].attr_g.name);
1276
1277 if (edev->max_supported) {
1278 kfree(edev->extcon_dev_type.groups);
1279 kfree(edev->cables);
1280 }
1281
1282#if defined(CONFIG_ANDROID)
1283 if (switch_class)
dae61651 1284 class_compat_remove_link(switch_class, &edev->dev, NULL);
57e7cd37 1285#endif
dae61651 1286 put_device(&edev->dev);
de55d871
MH
1287}
1288EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1289
1ad94ffe
CC
1290#ifdef CONFIG_OF
1291/*
1292 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1293 * @dev - instance to the given device
1294 * @index - index into list of extcon_dev
1295 *
1296 * return the instance of extcon device
1297 */
1298struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1299{
1300 struct device_node *node;
1301 struct extcon_dev *edev;
1302
7eae43ae
CC
1303 if (!dev)
1304 return ERR_PTR(-EINVAL);
1305
1ad94ffe 1306 if (!dev->of_node) {
e8752b7a 1307 dev_dbg(dev, "device does not have a device node entry\n");
1ad94ffe
CC
1308 return ERR_PTR(-EINVAL);
1309 }
1310
1311 node = of_parse_phandle(dev->of_node, "extcon", index);
1312 if (!node) {
e8752b7a 1313 dev_dbg(dev, "failed to get phandle in %s node\n",
1ad94ffe
CC
1314 dev->of_node->full_name);
1315 return ERR_PTR(-ENODEV);
1316 }
1317
f841afb1
TF
1318 mutex_lock(&extcon_dev_list_lock);
1319 list_for_each_entry(edev, &extcon_dev_list, entry) {
1320 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1321 mutex_unlock(&extcon_dev_list_lock);
5d5c4c13 1322 of_node_put(node);
f841afb1
TF
1323 return edev;
1324 }
1ad94ffe 1325 }
f841afb1 1326 mutex_unlock(&extcon_dev_list_lock);
5d5c4c13 1327 of_node_put(node);
1ad94ffe 1328
f841afb1 1329 return ERR_PTR(-EPROBE_DEFER);
1ad94ffe
CC
1330}
1331#else
1332struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1333{
1334 return ERR_PTR(-ENOSYS);
1335}
1336#endif /* CONFIG_OF */
1337EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1338
707d7550
CC
1339/**
1340 * extcon_get_edev_name() - Get the name of the extcon device.
1341 * @edev: the extcon device
1342 */
1343const char *extcon_get_edev_name(struct extcon_dev *edev)
1344{
1345 return !edev ? NULL : edev->name;
1346}
1347
de55d871
MH
1348static int __init extcon_class_init(void)
1349{
1350 return create_extcon_class();
1351}
1352module_init(extcon_class_init);
1353
1354static void __exit extcon_class_exit(void)
1355{
0dc77b6d
PH
1356#if defined(CONFIG_ANDROID)
1357 class_compat_unregister(switch_class);
1358#endif
de55d871
MH
1359 class_destroy(extcon_class);
1360}
1361module_exit(extcon_class_exit);
1362
2a9de9c0 1363MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
de55d871
MH
1364MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1365MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1366MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1367MODULE_DESCRIPTION("External connector (extcon) class driver");
1368MODULE_LICENSE("GPL");