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