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