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