xref: /linux-6.15/drivers/extcon/extcon.c (revision 73b6ecdb)
1f68a8342SChanwoo Choi /*
2b9ec23c0SChanwoo Choi  *  drivers/extcon/extcon.c - External Connector (extcon) framework.
3f68a8342SChanwoo Choi  *
4f68a8342SChanwoo Choi  *  External connector (extcon) class driver
5f68a8342SChanwoo Choi  *
62a9de9c0SChanwoo Choi  * Copyright (C) 2015 Samsung Electronics
72a9de9c0SChanwoo Choi  * Author: Chanwoo Choi <[email protected]>
82a9de9c0SChanwoo Choi  *
9f68a8342SChanwoo Choi  * Copyright (C) 2012 Samsung Electronics
10f68a8342SChanwoo Choi  * Author: Donggeun Kim <[email protected]>
11f68a8342SChanwoo Choi  * Author: MyungJoo Ham <[email protected]>
12f68a8342SChanwoo Choi  *
13f68a8342SChanwoo Choi  * based on android/drivers/switch/switch_class.c
14f68a8342SChanwoo Choi  * Copyright (C) 2008 Google, Inc.
15f68a8342SChanwoo Choi  * Author: Mike Lockwood <[email protected]>
16f68a8342SChanwoo Choi  *
17f68a8342SChanwoo Choi  * This software is licensed under the terms of the GNU General Public
18f68a8342SChanwoo Choi  * License version 2, as published by the Free Software Foundation, and
19f68a8342SChanwoo Choi  * may be copied, distributed, and modified under those terms.
20f68a8342SChanwoo Choi  *
21f68a8342SChanwoo Choi  * This program is distributed in the hope that it will be useful,
22f68a8342SChanwoo Choi  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23f68a8342SChanwoo Choi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24f68a8342SChanwoo Choi  * GNU General Public License for more details.
25f68a8342SChanwoo Choi  */
26f68a8342SChanwoo Choi 
27f68a8342SChanwoo Choi #include <linux/module.h>
28f68a8342SChanwoo Choi #include <linux/types.h>
29f68a8342SChanwoo Choi #include <linux/init.h>
30f68a8342SChanwoo Choi #include <linux/device.h>
31f68a8342SChanwoo Choi #include <linux/fs.h>
32f68a8342SChanwoo Choi #include <linux/err.h>
33f68a8342SChanwoo Choi #include <linux/extcon.h>
34f68a8342SChanwoo Choi #include <linux/of.h>
35f68a8342SChanwoo Choi #include <linux/slab.h>
36f68a8342SChanwoo Choi #include <linux/sysfs.h>
37f68a8342SChanwoo Choi 
382a9de9c0SChanwoo Choi #define SUPPORTED_CABLE_MAX	32
392a9de9c0SChanwoo Choi #define CABLE_NAME_MAX		30
402a9de9c0SChanwoo Choi 
412a9de9c0SChanwoo Choi static const char *extcon_name[] =  {
42*73b6ecdbSChanwoo Choi 	[EXTCON_NONE]		= "NONE",
43*73b6ecdbSChanwoo Choi 
448e9bc36dSChanwoo Choi 	/* USB external connector */
45f68a8342SChanwoo Choi 	[EXTCON_USB]		= "USB",
468e9bc36dSChanwoo Choi 	[EXTCON_USB_HOST]	= "USB-HOST",
478e9bc36dSChanwoo Choi 
488e9bc36dSChanwoo Choi 	/* Charger external connector */
49f68a8342SChanwoo Choi 	[EXTCON_TA]		= "TA",
508e9bc36dSChanwoo Choi 	[EXTCON_FAST_CHARGER]	= "FAST-CHARGER",
518e9bc36dSChanwoo Choi 	[EXTCON_SLOW_CHARGER]	= "SLOW-CHARGER",
528e9bc36dSChanwoo Choi 	[EXTCON_CHARGE_DOWNSTREAM] = "CHARGE-DOWNSTREAM",
538e9bc36dSChanwoo Choi 
548e9bc36dSChanwoo Choi 	/* Audio/Video external connector */
558e9bc36dSChanwoo Choi 	[EXTCON_LINE_IN]	= "LINE-IN",
568e9bc36dSChanwoo Choi 	[EXTCON_LINE_OUT]	= "LINE-OUT",
578e9bc36dSChanwoo Choi 	[EXTCON_MICROPHONE]	= "MICROPHONE",
588e9bc36dSChanwoo Choi 	[EXTCON_HEADPHONE]	= "HEADPHONE",
598e9bc36dSChanwoo Choi 
60f68a8342SChanwoo Choi 	[EXTCON_HDMI]		= "HDMI",
61f68a8342SChanwoo Choi 	[EXTCON_MHL]		= "MHL",
62f68a8342SChanwoo Choi 	[EXTCON_DVI]		= "DVI",
63f68a8342SChanwoo Choi 	[EXTCON_VGA]		= "VGA",
648e9bc36dSChanwoo Choi 	[EXTCON_SPDIF_IN]	= "SPDIF-IN",
658e9bc36dSChanwoo Choi 	[EXTCON_SPDIF_OUT]	= "SPDIF-OUT",
668e9bc36dSChanwoo Choi 	[EXTCON_VIDEO_IN]	= "VIDEO-IN",
678e9bc36dSChanwoo Choi 	[EXTCON_VIDEO_OUT]	= "VIDEO-OUT",
688e9bc36dSChanwoo Choi 
698e9bc36dSChanwoo Choi 	/* Etc external connector */
708e9bc36dSChanwoo Choi 	[EXTCON_DOCK]		= "DOCK",
712a9de9c0SChanwoo Choi 	[EXTCON_JIG]		= "JIG",
728e9bc36dSChanwoo Choi 	[EXTCON_MECHANICAL]	= "MECHANICAL",
738e9bc36dSChanwoo Choi 
742a9de9c0SChanwoo Choi 	NULL,
75f68a8342SChanwoo Choi };
76f68a8342SChanwoo Choi 
77f68a8342SChanwoo Choi static struct class *extcon_class;
78f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
79f68a8342SChanwoo Choi static struct class_compat *switch_class;
80f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */
81f68a8342SChanwoo Choi 
82f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list);
83f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock);
84f68a8342SChanwoo Choi 
85f68a8342SChanwoo Choi /**
86f68a8342SChanwoo Choi  * check_mutually_exclusive - Check if new_state violates mutually_exclusive
87f68a8342SChanwoo Choi  *			      condition.
88f68a8342SChanwoo Choi  * @edev:	the extcon device
89f68a8342SChanwoo Choi  * @new_state:	new cable attach status for @edev
90f68a8342SChanwoo Choi  *
91f68a8342SChanwoo Choi  * Returns 0 if nothing violates. Returns the index + 1 for the first
92f68a8342SChanwoo Choi  * violated condition.
93f68a8342SChanwoo Choi  */
94f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
95f68a8342SChanwoo Choi {
96f68a8342SChanwoo Choi 	int i = 0;
97f68a8342SChanwoo Choi 
98f68a8342SChanwoo Choi 	if (!edev->mutually_exclusive)
99f68a8342SChanwoo Choi 		return 0;
100f68a8342SChanwoo Choi 
101f68a8342SChanwoo Choi 	for (i = 0; edev->mutually_exclusive[i]; i++) {
102f68a8342SChanwoo Choi 		int weight;
103f68a8342SChanwoo Choi 		u32 correspondants = new_state & edev->mutually_exclusive[i];
104f68a8342SChanwoo Choi 
105f68a8342SChanwoo Choi 		/* calculate the total number of bits set */
106f68a8342SChanwoo Choi 		weight = hweight32(correspondants);
107f68a8342SChanwoo Choi 		if (weight > 1)
108f68a8342SChanwoo Choi 			return i + 1;
109f68a8342SChanwoo Choi 	}
110f68a8342SChanwoo Choi 
111f68a8342SChanwoo Choi 	return 0;
112f68a8342SChanwoo Choi }
113f68a8342SChanwoo Choi 
114*73b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
1152a9de9c0SChanwoo Choi {
1162a9de9c0SChanwoo Choi 	int i;
1172a9de9c0SChanwoo Choi 
1182a9de9c0SChanwoo Choi 	/* Find the the index of extcon cable in edev->supported_cable */
1192a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
1202a9de9c0SChanwoo Choi 		if (edev->supported_cable[i] == id)
1212a9de9c0SChanwoo Choi 			return i;
1222a9de9c0SChanwoo Choi 	}
1232a9de9c0SChanwoo Choi 
1242a9de9c0SChanwoo Choi 	return -EINVAL;
1252a9de9c0SChanwoo Choi }
1262a9de9c0SChanwoo Choi 
1272a9de9c0SChanwoo Choi static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
1282a9de9c0SChanwoo Choi {
129*73b6ecdbSChanwoo Choi 	unsigned int id = EXTCON_NONE;
130*73b6ecdbSChanwoo Choi 	int i = 0;
1312a9de9c0SChanwoo Choi 
1322a9de9c0SChanwoo Choi 	if (edev->max_supported == 0)
1332a9de9c0SChanwoo Choi 		return -EINVAL;
1342a9de9c0SChanwoo Choi 
1352a9de9c0SChanwoo Choi 	/* Find the the number of extcon cable */
136*73b6ecdbSChanwoo Choi 	while (extcon_name[i]) {
1372a9de9c0SChanwoo Choi 		if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
1382a9de9c0SChanwoo Choi 			id = i;
1392a9de9c0SChanwoo Choi 			break;
1402a9de9c0SChanwoo Choi 		}
1412a9de9c0SChanwoo Choi 	}
1422a9de9c0SChanwoo Choi 
1432a9de9c0SChanwoo Choi 	if (id == EXTCON_NONE)
1442a9de9c0SChanwoo Choi 		return -EINVAL;
1452a9de9c0SChanwoo Choi 
1462a9de9c0SChanwoo Choi 	return find_cable_index_by_id(edev, id);
1472a9de9c0SChanwoo Choi }
1482a9de9c0SChanwoo Choi 
149046050f6SChanwoo Choi static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
150046050f6SChanwoo Choi {
151046050f6SChanwoo Choi 	if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
152046050f6SChanwoo Choi 		*attached = new ? true : false;
153046050f6SChanwoo Choi 		return true;
154046050f6SChanwoo Choi 	}
155046050f6SChanwoo Choi 
156046050f6SChanwoo Choi 	return false;
157046050f6SChanwoo Choi }
158046050f6SChanwoo Choi 
159f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr,
160f68a8342SChanwoo Choi 			  char *buf)
161f68a8342SChanwoo Choi {
162f68a8342SChanwoo Choi 	int i, count = 0;
163f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
164f68a8342SChanwoo Choi 
165f68a8342SChanwoo Choi 	if (edev->print_state) {
166f68a8342SChanwoo Choi 		int ret = edev->print_state(edev, buf);
167f68a8342SChanwoo Choi 
168f68a8342SChanwoo Choi 		if (ret >= 0)
169f68a8342SChanwoo Choi 			return ret;
170f68a8342SChanwoo Choi 		/* Use default if failed */
171f68a8342SChanwoo Choi 	}
172f68a8342SChanwoo Choi 
173f68a8342SChanwoo Choi 	if (edev->max_supported == 0)
174f68a8342SChanwoo Choi 		return sprintf(buf, "%u\n", edev->state);
175f68a8342SChanwoo Choi 
1762a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
177f68a8342SChanwoo Choi 		count += sprintf(buf + count, "%s=%d\n",
1782a9de9c0SChanwoo Choi 				extcon_name[edev->supported_cable[i]],
179f68a8342SChanwoo Choi 				 !!(edev->state & (1 << i)));
180f68a8342SChanwoo Choi 	}
181f68a8342SChanwoo Choi 
182f68a8342SChanwoo Choi 	return count;
183f68a8342SChanwoo Choi }
184f68a8342SChanwoo Choi 
185f68a8342SChanwoo Choi static ssize_t state_store(struct device *dev, struct device_attribute *attr,
186f68a8342SChanwoo Choi 			   const char *buf, size_t count)
187f68a8342SChanwoo Choi {
188f68a8342SChanwoo Choi 	u32 state;
189f68a8342SChanwoo Choi 	ssize_t ret = 0;
190f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
191f68a8342SChanwoo Choi 
192f68a8342SChanwoo Choi 	ret = sscanf(buf, "0x%x", &state);
193f68a8342SChanwoo Choi 	if (ret == 0)
194f68a8342SChanwoo Choi 		ret = -EINVAL;
195f68a8342SChanwoo Choi 	else
196f68a8342SChanwoo Choi 		ret = extcon_set_state(edev, state);
197f68a8342SChanwoo Choi 
198f68a8342SChanwoo Choi 	if (ret < 0)
199f68a8342SChanwoo Choi 		return ret;
200f68a8342SChanwoo Choi 
201f68a8342SChanwoo Choi 	return count;
202f68a8342SChanwoo Choi }
203f68a8342SChanwoo Choi static DEVICE_ATTR_RW(state);
204f68a8342SChanwoo Choi 
205f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr,
206f68a8342SChanwoo Choi 		char *buf)
207f68a8342SChanwoo Choi {
208f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
209f68a8342SChanwoo Choi 
21071c3ffa5SChanwoo Choi 	return sprintf(buf, "%s\n", edev->name);
211f68a8342SChanwoo Choi }
212f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name);
213f68a8342SChanwoo Choi 
214f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev,
215f68a8342SChanwoo Choi 			       struct device_attribute *attr, char *buf)
216f68a8342SChanwoo Choi {
217f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
218f68a8342SChanwoo Choi 						  attr_name);
2192a9de9c0SChanwoo Choi 	int i = cable->cable_index;
220f68a8342SChanwoo Choi 
221f68a8342SChanwoo Choi 	return sprintf(buf, "%s\n",
2222a9de9c0SChanwoo Choi 			extcon_name[cable->edev->supported_cable[i]]);
223f68a8342SChanwoo Choi }
224f68a8342SChanwoo Choi 
225f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev,
226f68a8342SChanwoo Choi 				struct device_attribute *attr, char *buf)
227f68a8342SChanwoo Choi {
228f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
229f68a8342SChanwoo Choi 						  attr_state);
230f68a8342SChanwoo Choi 
231f68a8342SChanwoo Choi 	return sprintf(buf, "%d\n",
232f68a8342SChanwoo Choi 		       extcon_get_cable_state_(cable->edev,
233f68a8342SChanwoo Choi 					       cable->cable_index));
234f68a8342SChanwoo Choi }
235f68a8342SChanwoo Choi 
236f68a8342SChanwoo Choi /**
237f68a8342SChanwoo Choi  * extcon_update_state() - Update the cable attach states of the extcon device
238f68a8342SChanwoo Choi  *			   only for the masked bits.
239f68a8342SChanwoo Choi  * @edev:	the extcon device
240f68a8342SChanwoo Choi  * @mask:	the bit mask to designate updated bits.
241f68a8342SChanwoo Choi  * @state:	new cable attach status for @edev
242f68a8342SChanwoo Choi  *
243f68a8342SChanwoo Choi  * Changing the state sends uevent with environment variable containing
244f68a8342SChanwoo Choi  * the name of extcon device (envp[0]) and the state output (envp[1]).
245f68a8342SChanwoo Choi  * Tizen uses this format for extcon device to get events from ports.
246f68a8342SChanwoo Choi  * Android uses this format as well.
247f68a8342SChanwoo Choi  *
248f68a8342SChanwoo Choi  * Note that the notifier provides which bits are changed in the state
249f68a8342SChanwoo Choi  * variable with the val parameter (second) to the callback.
250f68a8342SChanwoo Choi  */
251f68a8342SChanwoo Choi int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
252f68a8342SChanwoo Choi {
253f68a8342SChanwoo Choi 	char name_buf[120];
254f68a8342SChanwoo Choi 	char state_buf[120];
255f68a8342SChanwoo Choi 	char *prop_buf;
256f68a8342SChanwoo Choi 	char *envp[3];
257f68a8342SChanwoo Choi 	int env_offset = 0;
258f68a8342SChanwoo Choi 	int length;
259046050f6SChanwoo Choi 	int index;
260f68a8342SChanwoo Choi 	unsigned long flags;
261046050f6SChanwoo Choi 	bool attached;
262f68a8342SChanwoo Choi 
263f68a8342SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
264f68a8342SChanwoo Choi 
265f68a8342SChanwoo Choi 	if (edev->state != ((edev->state & ~mask) | (state & mask))) {
266f68a8342SChanwoo Choi 		if (check_mutually_exclusive(edev, (edev->state & ~mask) |
267f68a8342SChanwoo Choi 						   (state & mask))) {
268f68a8342SChanwoo Choi 			spin_unlock_irqrestore(&edev->lock, flags);
269f68a8342SChanwoo Choi 			return -EPERM;
270f68a8342SChanwoo Choi 		}
271f68a8342SChanwoo Choi 
272046050f6SChanwoo Choi 		for (index = 0; index < edev->max_supported; index++) {
273046050f6SChanwoo Choi 			if (is_extcon_changed(edev->state, state, index, &attached))
274046050f6SChanwoo Choi 				raw_notifier_call_chain(&edev->nh[index], attached, edev);
275046050f6SChanwoo Choi 		}
276046050f6SChanwoo Choi 
277f68a8342SChanwoo Choi 		edev->state &= ~mask;
278f68a8342SChanwoo Choi 		edev->state |= state & mask;
279f68a8342SChanwoo Choi 
280f68a8342SChanwoo Choi 		/* This could be in interrupt handler */
281f68a8342SChanwoo Choi 		prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
282f68a8342SChanwoo Choi 		if (prop_buf) {
283f68a8342SChanwoo Choi 			length = name_show(&edev->dev, NULL, prop_buf);
284f68a8342SChanwoo Choi 			if (length > 0) {
285f68a8342SChanwoo Choi 				if (prop_buf[length - 1] == '\n')
286f68a8342SChanwoo Choi 					prop_buf[length - 1] = 0;
287f68a8342SChanwoo Choi 				snprintf(name_buf, sizeof(name_buf),
288f68a8342SChanwoo Choi 					"NAME=%s", prop_buf);
289f68a8342SChanwoo Choi 				envp[env_offset++] = name_buf;
290f68a8342SChanwoo Choi 			}
291f68a8342SChanwoo Choi 			length = state_show(&edev->dev, NULL, prop_buf);
292f68a8342SChanwoo Choi 			if (length > 0) {
293f68a8342SChanwoo Choi 				if (prop_buf[length - 1] == '\n')
294f68a8342SChanwoo Choi 					prop_buf[length - 1] = 0;
295f68a8342SChanwoo Choi 				snprintf(state_buf, sizeof(state_buf),
296f68a8342SChanwoo Choi 					"STATE=%s", prop_buf);
297f68a8342SChanwoo Choi 				envp[env_offset++] = state_buf;
298f68a8342SChanwoo Choi 			}
299f68a8342SChanwoo Choi 			envp[env_offset] = NULL;
300f68a8342SChanwoo Choi 			/* Unlock early before uevent */
301f68a8342SChanwoo Choi 			spin_unlock_irqrestore(&edev->lock, flags);
302f68a8342SChanwoo Choi 
303f68a8342SChanwoo Choi 			kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
304f68a8342SChanwoo Choi 			free_page((unsigned long)prop_buf);
305f68a8342SChanwoo Choi 		} else {
306f68a8342SChanwoo Choi 			/* Unlock early before uevent */
307f68a8342SChanwoo Choi 			spin_unlock_irqrestore(&edev->lock, flags);
308f68a8342SChanwoo Choi 
309f68a8342SChanwoo Choi 			dev_err(&edev->dev, "out of memory in extcon_set_state\n");
310f68a8342SChanwoo Choi 			kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
311f68a8342SChanwoo Choi 		}
312f68a8342SChanwoo Choi 	} else {
313f68a8342SChanwoo Choi 		/* No changes */
314f68a8342SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
315f68a8342SChanwoo Choi 	}
316f68a8342SChanwoo Choi 
317f68a8342SChanwoo Choi 	return 0;
318f68a8342SChanwoo Choi }
319f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_update_state);
320f68a8342SChanwoo Choi 
321f68a8342SChanwoo Choi /**
322f68a8342SChanwoo Choi  * extcon_set_state() - Set the cable attach states of the extcon device.
323f68a8342SChanwoo Choi  * @edev:	the extcon device
324f68a8342SChanwoo Choi  * @state:	new cable attach status for @edev
325f68a8342SChanwoo Choi  *
326f68a8342SChanwoo Choi  * Note that notifier provides which bits are changed in the state
327f68a8342SChanwoo Choi  * variable with the val parameter (second) to the callback.
328f68a8342SChanwoo Choi  */
329f68a8342SChanwoo Choi int extcon_set_state(struct extcon_dev *edev, u32 state)
330f68a8342SChanwoo Choi {
331f68a8342SChanwoo Choi 	return extcon_update_state(edev, 0xffffffff, state);
332f68a8342SChanwoo Choi }
333f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state);
334f68a8342SChanwoo Choi 
335f68a8342SChanwoo Choi /**
336f68a8342SChanwoo Choi  * extcon_get_cable_state_() - Get the status of a specific cable.
337f68a8342SChanwoo Choi  * @edev:	the extcon device that has the cable.
3382a9de9c0SChanwoo Choi  * @id:		the unique id of each external connector in extcon enumeration.
339f68a8342SChanwoo Choi  */
340*73b6ecdbSChanwoo Choi int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
341f68a8342SChanwoo Choi {
3422a9de9c0SChanwoo Choi 	int index;
3432a9de9c0SChanwoo Choi 
3442a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
3452a9de9c0SChanwoo Choi 	if (index < 0)
3462a9de9c0SChanwoo Choi 		return index;
3472a9de9c0SChanwoo Choi 
3482a9de9c0SChanwoo Choi 	if (edev->max_supported && edev->max_supported <= index)
349f68a8342SChanwoo Choi 		return -EINVAL;
350f68a8342SChanwoo Choi 
351f68a8342SChanwoo Choi 	return !!(edev->state & (1 << index));
352f68a8342SChanwoo Choi }
353f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
354f68a8342SChanwoo Choi 
355f68a8342SChanwoo Choi /**
356f68a8342SChanwoo Choi  * extcon_get_cable_state() - Get the status of a specific cable.
357f68a8342SChanwoo Choi  * @edev:	the extcon device that has the cable.
358f68a8342SChanwoo Choi  * @cable_name:	cable name.
359f68a8342SChanwoo Choi  *
360f68a8342SChanwoo Choi  * Note that this is slower than extcon_get_cable_state_.
361f68a8342SChanwoo Choi  */
362f68a8342SChanwoo Choi int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
363f68a8342SChanwoo Choi {
3642a9de9c0SChanwoo Choi 	return extcon_get_cable_state_(edev, find_cable_index_by_name
365f68a8342SChanwoo Choi 						(edev, cable_name));
366f68a8342SChanwoo Choi }
367f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_cable_state);
368f68a8342SChanwoo Choi 
369f68a8342SChanwoo Choi /**
370f68a8342SChanwoo Choi  * extcon_set_cable_state_() - Set the status of a specific cable.
371f68a8342SChanwoo Choi  * @edev:		the extcon device that has the cable.
3722a9de9c0SChanwoo Choi  * @id:			the unique id of each external connector
3732a9de9c0SChanwoo Choi  *			in extcon enumeration.
3742a9de9c0SChanwoo Choi  * @state:		the new cable status. The default semantics is
375f68a8342SChanwoo Choi  *			true: attached / false: detached.
376f68a8342SChanwoo Choi  */
377*73b6ecdbSChanwoo Choi int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
3782a9de9c0SChanwoo Choi 				bool cable_state)
379f68a8342SChanwoo Choi {
380f68a8342SChanwoo Choi 	u32 state;
3812a9de9c0SChanwoo Choi 	int index;
382f68a8342SChanwoo Choi 
3832a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
3842a9de9c0SChanwoo Choi 	if (index < 0)
3852a9de9c0SChanwoo Choi 		return index;
3862a9de9c0SChanwoo Choi 
3872a9de9c0SChanwoo Choi 	if (edev->max_supported && edev->max_supported <= index)
388f68a8342SChanwoo Choi 		return -EINVAL;
389f68a8342SChanwoo Choi 
390f68a8342SChanwoo Choi 	state = cable_state ? (1 << index) : 0;
391f68a8342SChanwoo Choi 	return extcon_update_state(edev, 1 << index, state);
392f68a8342SChanwoo Choi }
393f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
394f68a8342SChanwoo Choi 
395f68a8342SChanwoo Choi /**
396f68a8342SChanwoo Choi  * extcon_set_cable_state() - Set the status of a specific cable.
397f68a8342SChanwoo Choi  * @edev:		the extcon device that has the cable.
398f68a8342SChanwoo Choi  * @cable_name:		cable name.
399f68a8342SChanwoo Choi  * @cable_state:	the new cable status. The default semantics is
400f68a8342SChanwoo Choi  *			true: attached / false: detached.
401f68a8342SChanwoo Choi  *
402f68a8342SChanwoo Choi  * Note that this is slower than extcon_set_cable_state_.
403f68a8342SChanwoo Choi  */
404f68a8342SChanwoo Choi int extcon_set_cable_state(struct extcon_dev *edev,
405f68a8342SChanwoo Choi 			const char *cable_name, bool cable_state)
406f68a8342SChanwoo Choi {
4072a9de9c0SChanwoo Choi 	return extcon_set_cable_state_(edev, find_cable_index_by_name
408f68a8342SChanwoo Choi 					(edev, cable_name), cable_state);
409f68a8342SChanwoo Choi }
410f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_cable_state);
411f68a8342SChanwoo Choi 
412f68a8342SChanwoo Choi /**
413f68a8342SChanwoo Choi  * extcon_get_extcon_dev() - Get the extcon device instance from the name
414f68a8342SChanwoo Choi  * @extcon_name:	The extcon name provided with extcon_dev_register()
415f68a8342SChanwoo Choi  */
416f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
417f68a8342SChanwoo Choi {
418f68a8342SChanwoo Choi 	struct extcon_dev *sd;
419f68a8342SChanwoo Choi 
420f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
421f68a8342SChanwoo Choi 	list_for_each_entry(sd, &extcon_dev_list, entry) {
422f68a8342SChanwoo Choi 		if (!strcmp(sd->name, extcon_name))
423f68a8342SChanwoo Choi 			goto out;
424f68a8342SChanwoo Choi 	}
425f68a8342SChanwoo Choi 	sd = NULL;
426f68a8342SChanwoo Choi out:
427f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
428f68a8342SChanwoo Choi 	return sd;
429f68a8342SChanwoo Choi }
430f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
431f68a8342SChanwoo Choi 
432f68a8342SChanwoo Choi /**
433f68a8342SChanwoo Choi  * extcon_register_interest() - Register a notifier for a state change of a
434f68a8342SChanwoo Choi  *				specific cable, not an entier set of cables of a
435f68a8342SChanwoo Choi  *				extcon device.
436f68a8342SChanwoo Choi  * @obj:		an empty extcon_specific_cable_nb object to be returned.
437f68a8342SChanwoo Choi  * @extcon_name:	the name of extcon device.
438f68a8342SChanwoo Choi  *			if NULL, extcon_register_interest will register
439f68a8342SChanwoo Choi  *			every cable with the target cable_name given.
440f68a8342SChanwoo Choi  * @cable_name:		the target cable name.
441f68a8342SChanwoo Choi  * @nb:			the notifier block to get notified.
442f68a8342SChanwoo Choi  *
443f68a8342SChanwoo Choi  * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
444f68a8342SChanwoo Choi  * the struct for you.
445f68a8342SChanwoo Choi  *
446f68a8342SChanwoo Choi  * extcon_register_interest is a helper function for those who want to get
447f68a8342SChanwoo Choi  * notification for a single specific cable's status change. If a user wants
448f68a8342SChanwoo Choi  * to get notification for any changes of all cables of a extcon device,
449f68a8342SChanwoo Choi  * he/she should use the general extcon_register_notifier().
450f68a8342SChanwoo Choi  *
451f68a8342SChanwoo Choi  * Note that the second parameter given to the callback of nb (val) is
452f68a8342SChanwoo Choi  * "old_state", not the current state. The current state can be retrieved
453f68a8342SChanwoo Choi  * by looking at the third pameter (edev pointer)'s state value.
454f68a8342SChanwoo Choi  */
455f68a8342SChanwoo Choi int extcon_register_interest(struct extcon_specific_cable_nb *obj,
456f68a8342SChanwoo Choi 			     const char *extcon_name, const char *cable_name,
457f68a8342SChanwoo Choi 			     struct notifier_block *nb)
458f68a8342SChanwoo Choi {
45966bee35fSHans de Goede 	unsigned long flags;
46066bee35fSHans de Goede 	int ret;
46166bee35fSHans de Goede 
462f68a8342SChanwoo Choi 	if (!obj || !cable_name || !nb)
463f68a8342SChanwoo Choi 		return -EINVAL;
464f68a8342SChanwoo Choi 
465f68a8342SChanwoo Choi 	if (extcon_name) {
466f68a8342SChanwoo Choi 		obj->edev = extcon_get_extcon_dev(extcon_name);
467f68a8342SChanwoo Choi 		if (!obj->edev)
468f68a8342SChanwoo Choi 			return -ENODEV;
469f68a8342SChanwoo Choi 
4702a9de9c0SChanwoo Choi 		obj->cable_index = find_cable_index_by_name(obj->edev,
471f68a8342SChanwoo Choi 							cable_name);
472f68a8342SChanwoo Choi 		if (obj->cable_index < 0)
473f68a8342SChanwoo Choi 			return obj->cable_index;
474f68a8342SChanwoo Choi 
475f68a8342SChanwoo Choi 		obj->user_nb = nb;
476f68a8342SChanwoo Choi 
47766bee35fSHans de Goede 		spin_lock_irqsave(&obj->edev->lock, flags);
478046050f6SChanwoo Choi 		ret = raw_notifier_chain_register(
479046050f6SChanwoo Choi 					&obj->edev->nh[obj->cable_index],
480046050f6SChanwoo Choi 					obj->user_nb);
48166bee35fSHans de Goede 		spin_unlock_irqrestore(&obj->edev->lock, flags);
482f68a8342SChanwoo Choi 	} else {
483f68a8342SChanwoo Choi 		struct class_dev_iter iter;
484f68a8342SChanwoo Choi 		struct extcon_dev *extd;
485f68a8342SChanwoo Choi 		struct device *dev;
486f68a8342SChanwoo Choi 
487f68a8342SChanwoo Choi 		if (!extcon_class)
488f68a8342SChanwoo Choi 			return -ENODEV;
489f68a8342SChanwoo Choi 		class_dev_iter_init(&iter, extcon_class, NULL, NULL);
490f68a8342SChanwoo Choi 		while ((dev = class_dev_iter_next(&iter))) {
491f68a8342SChanwoo Choi 			extd = dev_get_drvdata(dev);
492f68a8342SChanwoo Choi 
4932a9de9c0SChanwoo Choi 			if (find_cable_index_by_name(extd, cable_name) < 0)
494f68a8342SChanwoo Choi 				continue;
495f68a8342SChanwoo Choi 
496f68a8342SChanwoo Choi 			class_dev_iter_exit(&iter);
497f68a8342SChanwoo Choi 			return extcon_register_interest(obj, extd->name,
498f68a8342SChanwoo Choi 						cable_name, nb);
499f68a8342SChanwoo Choi 		}
500f68a8342SChanwoo Choi 
501b9ec23c0SChanwoo Choi 		ret = -ENODEV;
502f68a8342SChanwoo Choi 	}
503b9ec23c0SChanwoo Choi 
504b9ec23c0SChanwoo Choi 	return ret;
505f68a8342SChanwoo Choi }
506f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_interest);
507f68a8342SChanwoo Choi 
508f68a8342SChanwoo Choi /**
509f68a8342SChanwoo Choi  * extcon_unregister_interest() - Unregister the notifier registered by
510f68a8342SChanwoo Choi  *				  extcon_register_interest().
511f68a8342SChanwoo Choi  * @obj:	the extcon_specific_cable_nb object returned by
512f68a8342SChanwoo Choi  *		extcon_register_interest().
513f68a8342SChanwoo Choi  */
514f68a8342SChanwoo Choi int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
515f68a8342SChanwoo Choi {
51666bee35fSHans de Goede 	unsigned long flags;
51766bee35fSHans de Goede 	int ret;
51866bee35fSHans de Goede 
519f68a8342SChanwoo Choi 	if (!obj)
520f68a8342SChanwoo Choi 		return -EINVAL;
521f68a8342SChanwoo Choi 
52266bee35fSHans de Goede 	spin_lock_irqsave(&obj->edev->lock, flags);
523046050f6SChanwoo Choi 	ret = raw_notifier_chain_unregister(
524046050f6SChanwoo Choi 			&obj->edev->nh[obj->cable_index], obj->user_nb);
52566bee35fSHans de Goede 	spin_unlock_irqrestore(&obj->edev->lock, flags);
52666bee35fSHans de Goede 
52766bee35fSHans de Goede 	return ret;
528f68a8342SChanwoo Choi }
529f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_interest);
530f68a8342SChanwoo Choi 
531f68a8342SChanwoo Choi /**
532f68a8342SChanwoo Choi  * extcon_register_notifier() - Register a notifiee to get notified by
533f68a8342SChanwoo Choi  *				any attach status changes from the extcon.
534046050f6SChanwoo Choi  * @edev:	the extcon device that has the external connecotr.
535046050f6SChanwoo Choi  * @id:		the unique id of each external connector in extcon enumeration.
536f68a8342SChanwoo Choi  * @nb:		a notifier block to be registered.
537f68a8342SChanwoo Choi  *
538f68a8342SChanwoo Choi  * Note that the second parameter given to the callback of nb (val) is
539f68a8342SChanwoo Choi  * "old_state", not the current state. The current state can be retrieved
540f68a8342SChanwoo Choi  * by looking at the third pameter (edev pointer)'s state value.
541f68a8342SChanwoo Choi  */
542*73b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
543f68a8342SChanwoo Choi 			     struct notifier_block *nb)
544f68a8342SChanwoo Choi {
54566bee35fSHans de Goede 	unsigned long flags;
546046050f6SChanwoo Choi 	int ret, idx;
547046050f6SChanwoo Choi 
548046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
54966bee35fSHans de Goede 
55066bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
551046050f6SChanwoo Choi 	ret = raw_notifier_chain_register(&edev->nh[idx], nb);
55266bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
55366bee35fSHans de Goede 
55466bee35fSHans de Goede 	return ret;
555f68a8342SChanwoo Choi }
556f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier);
557f68a8342SChanwoo Choi 
558f68a8342SChanwoo Choi /**
559f68a8342SChanwoo Choi  * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
560046050f6SChanwoo Choi  * @edev:	the extcon device that has the external connecotr.
561046050f6SChanwoo Choi  * @id:		the unique id of each external connector in extcon enumeration.
562046050f6SChanwoo Choi  * @nb:		a notifier block to be registered.
563f68a8342SChanwoo Choi  */
564*73b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
565f68a8342SChanwoo Choi 				struct notifier_block *nb)
566f68a8342SChanwoo Choi {
56766bee35fSHans de Goede 	unsigned long flags;
568046050f6SChanwoo Choi 	int ret, idx;
569046050f6SChanwoo Choi 
570046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
57166bee35fSHans de Goede 
57266bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
573046050f6SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
57466bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
57566bee35fSHans de Goede 
57666bee35fSHans de Goede 	return ret;
577f68a8342SChanwoo Choi }
578f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
579f68a8342SChanwoo Choi 
580f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = {
581f68a8342SChanwoo Choi 	&dev_attr_state.attr,
582f68a8342SChanwoo Choi 	&dev_attr_name.attr,
583f68a8342SChanwoo Choi 	NULL,
584f68a8342SChanwoo Choi };
585f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon);
586f68a8342SChanwoo Choi 
587f68a8342SChanwoo Choi static int create_extcon_class(void)
588f68a8342SChanwoo Choi {
589f68a8342SChanwoo Choi 	if (!extcon_class) {
590f68a8342SChanwoo Choi 		extcon_class = class_create(THIS_MODULE, "extcon");
591f68a8342SChanwoo Choi 		if (IS_ERR(extcon_class))
592f68a8342SChanwoo Choi 			return PTR_ERR(extcon_class);
593f68a8342SChanwoo Choi 		extcon_class->dev_groups = extcon_groups;
594f68a8342SChanwoo Choi 
595f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
596f68a8342SChanwoo Choi 		switch_class = class_compat_register("switch");
597f68a8342SChanwoo Choi 		if (WARN(!switch_class, "cannot allocate"))
598f68a8342SChanwoo Choi 			return -ENOMEM;
599f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */
600f68a8342SChanwoo Choi 	}
601f68a8342SChanwoo Choi 
602f68a8342SChanwoo Choi 	return 0;
603f68a8342SChanwoo Choi }
604f68a8342SChanwoo Choi 
605f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev)
606f68a8342SChanwoo Choi {
607f68a8342SChanwoo Choi }
608f68a8342SChanwoo Choi 
609f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive";
610f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev)
611f68a8342SChanwoo Choi {
612f68a8342SChanwoo Choi }
613f68a8342SChanwoo Choi 
614f68a8342SChanwoo Choi /*
615f68a8342SChanwoo Choi  * extcon_dev_allocate() - Allocate the memory of extcon device.
6162a9de9c0SChanwoo Choi  * @supported_cable:	Array of supported extcon ending with EXTCON_NONE.
617f68a8342SChanwoo Choi  *			If supported_cable is NULL, cable name related APIs
618f68a8342SChanwoo Choi  *			are disabled.
619f68a8342SChanwoo Choi  *
620f68a8342SChanwoo Choi  * This function allocates the memory for extcon device without allocating
621f68a8342SChanwoo Choi  * memory in each extcon provider driver and initialize default setting for
622f68a8342SChanwoo Choi  * extcon device.
623f68a8342SChanwoo Choi  *
624f68a8342SChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail
625f68a8342SChanwoo Choi  */
626*73b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
627f68a8342SChanwoo Choi {
628f68a8342SChanwoo Choi 	struct extcon_dev *edev;
629f68a8342SChanwoo Choi 
630f68a8342SChanwoo Choi 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
631f68a8342SChanwoo Choi 	if (!edev)
632f68a8342SChanwoo Choi 		return ERR_PTR(-ENOMEM);
633f68a8342SChanwoo Choi 
634f68a8342SChanwoo Choi 	edev->max_supported = 0;
635f68a8342SChanwoo Choi 	edev->supported_cable = supported_cable;
636f68a8342SChanwoo Choi 
637f68a8342SChanwoo Choi 	return edev;
638f68a8342SChanwoo Choi }
639f68a8342SChanwoo Choi 
640f68a8342SChanwoo Choi /*
641f68a8342SChanwoo Choi  * extcon_dev_free() - Free the memory of extcon device.
642f68a8342SChanwoo Choi  * @edev:	the extcon device to free
643f68a8342SChanwoo Choi  */
644f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev)
645f68a8342SChanwoo Choi {
646f68a8342SChanwoo Choi 	kfree(edev);
647f68a8342SChanwoo Choi }
648f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free);
649f68a8342SChanwoo Choi 
650f68a8342SChanwoo Choi static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
651f68a8342SChanwoo Choi {
652f68a8342SChanwoo Choi 	struct extcon_dev **r = res;
653f68a8342SChanwoo Choi 
654f68a8342SChanwoo Choi 	if (WARN_ON(!r || !*r))
655f68a8342SChanwoo Choi 		return 0;
656f68a8342SChanwoo Choi 
657f68a8342SChanwoo Choi 	return *r == data;
658f68a8342SChanwoo Choi }
659f68a8342SChanwoo Choi 
660f68a8342SChanwoo Choi static void devm_extcon_dev_release(struct device *dev, void *res)
661f68a8342SChanwoo Choi {
662f68a8342SChanwoo Choi 	extcon_dev_free(*(struct extcon_dev **)res);
663f68a8342SChanwoo Choi }
664f68a8342SChanwoo Choi 
665f68a8342SChanwoo Choi /**
666f68a8342SChanwoo Choi  * devm_extcon_dev_allocate - Allocate managed extcon device
667f68a8342SChanwoo Choi  * @dev:		device owning the extcon device being created
6682a9de9c0SChanwoo Choi  * @supported_cable:	Array of supported extcon ending with EXTCON_NONE.
669f68a8342SChanwoo Choi  *			If supported_cable is NULL, cable name related APIs
670f68a8342SChanwoo Choi  *			are disabled.
671f68a8342SChanwoo Choi  *
672f68a8342SChanwoo Choi  * This function manages automatically the memory of extcon device using device
673f68a8342SChanwoo Choi  * resource management and simplify the control of freeing the memory of extcon
674f68a8342SChanwoo Choi  * device.
675f68a8342SChanwoo Choi  *
676f68a8342SChanwoo Choi  * Returns the pointer memory of allocated extcon_dev if success
677f68a8342SChanwoo Choi  * or ERR_PTR(err) if fail
678f68a8342SChanwoo Choi  */
679f68a8342SChanwoo Choi struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
680*73b6ecdbSChanwoo Choi 					const unsigned int *supported_cable)
681f68a8342SChanwoo Choi {
682f68a8342SChanwoo Choi 	struct extcon_dev **ptr, *edev;
683f68a8342SChanwoo Choi 
684f68a8342SChanwoo Choi 	ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
685f68a8342SChanwoo Choi 	if (!ptr)
686f68a8342SChanwoo Choi 		return ERR_PTR(-ENOMEM);
687f68a8342SChanwoo Choi 
688f68a8342SChanwoo Choi 	edev = extcon_dev_allocate(supported_cable);
689f68a8342SChanwoo Choi 	if (IS_ERR(edev)) {
690f68a8342SChanwoo Choi 		devres_free(ptr);
691f68a8342SChanwoo Choi 		return edev;
692f68a8342SChanwoo Choi 	}
693f68a8342SChanwoo Choi 
694f68a8342SChanwoo Choi 	edev->dev.parent = dev;
695f68a8342SChanwoo Choi 
696f68a8342SChanwoo Choi 	*ptr = edev;
697f68a8342SChanwoo Choi 	devres_add(dev, ptr);
698f68a8342SChanwoo Choi 
699f68a8342SChanwoo Choi 	return edev;
700f68a8342SChanwoo Choi }
701f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
702f68a8342SChanwoo Choi 
703f68a8342SChanwoo Choi void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
704f68a8342SChanwoo Choi {
705f68a8342SChanwoo Choi 	WARN_ON(devres_release(dev, devm_extcon_dev_release,
706f68a8342SChanwoo Choi 			       devm_extcon_dev_match, edev));
707f68a8342SChanwoo Choi }
708f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
709f68a8342SChanwoo Choi 
710f68a8342SChanwoo Choi /**
711f68a8342SChanwoo Choi  * extcon_dev_register() - Register a new extcon device
712f68a8342SChanwoo Choi  * @edev	: the new extcon device (should be allocated before calling)
713f68a8342SChanwoo Choi  *
714f68a8342SChanwoo Choi  * Among the members of edev struct, please set the "user initializing data"
715f68a8342SChanwoo Choi  * in any case and set the "optional callbacks" if required. However, please
716f68a8342SChanwoo Choi  * do not set the values of "internal data", which are initialized by
717f68a8342SChanwoo Choi  * this function.
718f68a8342SChanwoo Choi  */
719f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev)
720f68a8342SChanwoo Choi {
721f68a8342SChanwoo Choi 	int ret, index = 0;
72271c3ffa5SChanwoo Choi 	static atomic_t edev_no = ATOMIC_INIT(-1);
723f68a8342SChanwoo Choi 
724f68a8342SChanwoo Choi 	if (!extcon_class) {
725f68a8342SChanwoo Choi 		ret = create_extcon_class();
726f68a8342SChanwoo Choi 		if (ret < 0)
727f68a8342SChanwoo Choi 			return ret;
728f68a8342SChanwoo Choi 	}
729f68a8342SChanwoo Choi 
7302a9de9c0SChanwoo Choi 	if (!edev->supported_cable)
7312a9de9c0SChanwoo Choi 		return -EINVAL;
732f68a8342SChanwoo Choi 
7332a9de9c0SChanwoo Choi 	for (; edev->supported_cable[index] != EXTCON_NONE; index++);
7342a9de9c0SChanwoo Choi 
7352a9de9c0SChanwoo Choi 	edev->max_supported = index;
736f68a8342SChanwoo Choi 	if (index > SUPPORTED_CABLE_MAX) {
7372a9de9c0SChanwoo Choi 		dev_err(&edev->dev,
7382a9de9c0SChanwoo Choi 			"exceed the maximum number of supported cables\n");
739f68a8342SChanwoo Choi 		return -EINVAL;
740f68a8342SChanwoo Choi 	}
741f68a8342SChanwoo Choi 
742f68a8342SChanwoo Choi 	edev->dev.class = extcon_class;
743f68a8342SChanwoo Choi 	edev->dev.release = extcon_dev_release;
744f68a8342SChanwoo Choi 
74571c3ffa5SChanwoo Choi 	edev->name = dev_name(edev->dev.parent);
746f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(edev->name)) {
747f68a8342SChanwoo Choi 		dev_err(&edev->dev,
748f68a8342SChanwoo Choi 			"extcon device name is null\n");
749f68a8342SChanwoo Choi 		return -EINVAL;
750f68a8342SChanwoo Choi 	}
75171c3ffa5SChanwoo Choi 	dev_set_name(&edev->dev, "extcon%lu",
75271c3ffa5SChanwoo Choi 			(unsigned long)atomic_inc_return(&edev_no));
753f68a8342SChanwoo Choi 
754f68a8342SChanwoo Choi 	if (edev->max_supported) {
755f68a8342SChanwoo Choi 		char buf[10];
756f68a8342SChanwoo Choi 		char *str;
757f68a8342SChanwoo Choi 		struct extcon_cable *cable;
758f68a8342SChanwoo Choi 
759f68a8342SChanwoo Choi 		edev->cables = kzalloc(sizeof(struct extcon_cable) *
760f68a8342SChanwoo Choi 				       edev->max_supported, GFP_KERNEL);
761f68a8342SChanwoo Choi 		if (!edev->cables) {
762f68a8342SChanwoo Choi 			ret = -ENOMEM;
763f68a8342SChanwoo Choi 			goto err_sysfs_alloc;
764f68a8342SChanwoo Choi 		}
765f68a8342SChanwoo Choi 		for (index = 0; index < edev->max_supported; index++) {
766f68a8342SChanwoo Choi 			cable = &edev->cables[index];
767f68a8342SChanwoo Choi 
768f68a8342SChanwoo Choi 			snprintf(buf, 10, "cable.%d", index);
769f68a8342SChanwoo Choi 			str = kzalloc(sizeof(char) * (strlen(buf) + 1),
770f68a8342SChanwoo Choi 				      GFP_KERNEL);
771f68a8342SChanwoo Choi 			if (!str) {
772f68a8342SChanwoo Choi 				for (index--; index >= 0; index--) {
773f68a8342SChanwoo Choi 					cable = &edev->cables[index];
774f68a8342SChanwoo Choi 					kfree(cable->attr_g.name);
775f68a8342SChanwoo Choi 				}
776f68a8342SChanwoo Choi 				ret = -ENOMEM;
777f68a8342SChanwoo Choi 
778f68a8342SChanwoo Choi 				goto err_alloc_cables;
779f68a8342SChanwoo Choi 			}
780f68a8342SChanwoo Choi 			strcpy(str, buf);
781f68a8342SChanwoo Choi 
782f68a8342SChanwoo Choi 			cable->edev = edev;
783f68a8342SChanwoo Choi 			cable->cable_index = index;
784f68a8342SChanwoo Choi 			cable->attrs[0] = &cable->attr_name.attr;
785f68a8342SChanwoo Choi 			cable->attrs[1] = &cable->attr_state.attr;
786f68a8342SChanwoo Choi 			cable->attrs[2] = NULL;
787f68a8342SChanwoo Choi 			cable->attr_g.name = str;
788f68a8342SChanwoo Choi 			cable->attr_g.attrs = cable->attrs;
789f68a8342SChanwoo Choi 
790f68a8342SChanwoo Choi 			sysfs_attr_init(&cable->attr_name.attr);
791f68a8342SChanwoo Choi 			cable->attr_name.attr.name = "name";
792f68a8342SChanwoo Choi 			cable->attr_name.attr.mode = 0444;
793f68a8342SChanwoo Choi 			cable->attr_name.show = cable_name_show;
794f68a8342SChanwoo Choi 
795f68a8342SChanwoo Choi 			sysfs_attr_init(&cable->attr_state.attr);
796f68a8342SChanwoo Choi 			cable->attr_state.attr.name = "state";
797f68a8342SChanwoo Choi 			cable->attr_state.attr.mode = 0444;
798f68a8342SChanwoo Choi 			cable->attr_state.show = cable_state_show;
799f68a8342SChanwoo Choi 		}
800f68a8342SChanwoo Choi 	}
801f68a8342SChanwoo Choi 
802f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
803f68a8342SChanwoo Choi 		char buf[80];
804f68a8342SChanwoo Choi 		char *name;
805f68a8342SChanwoo Choi 
806f68a8342SChanwoo Choi 		/* Count the size of mutually_exclusive array */
807f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
808f68a8342SChanwoo Choi 			;
809f68a8342SChanwoo Choi 
810f68a8342SChanwoo Choi 		edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
811f68a8342SChanwoo Choi 					   (index + 1), GFP_KERNEL);
812f68a8342SChanwoo Choi 		if (!edev->attrs_muex) {
813f68a8342SChanwoo Choi 			ret = -ENOMEM;
814f68a8342SChanwoo Choi 			goto err_muex;
815f68a8342SChanwoo Choi 		}
816f68a8342SChanwoo Choi 
817f68a8342SChanwoo Choi 		edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
818f68a8342SChanwoo Choi 					     index, GFP_KERNEL);
819f68a8342SChanwoo Choi 		if (!edev->d_attrs_muex) {
820f68a8342SChanwoo Choi 			ret = -ENOMEM;
821f68a8342SChanwoo Choi 			kfree(edev->attrs_muex);
822f68a8342SChanwoo Choi 			goto err_muex;
823f68a8342SChanwoo Choi 		}
824f68a8342SChanwoo Choi 
825f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++) {
826f68a8342SChanwoo Choi 			sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
827f68a8342SChanwoo Choi 			name = kzalloc(sizeof(char) * (strlen(buf) + 1),
828f68a8342SChanwoo Choi 				       GFP_KERNEL);
829f68a8342SChanwoo Choi 			if (!name) {
830f68a8342SChanwoo Choi 				for (index--; index >= 0; index--) {
831f68a8342SChanwoo Choi 					kfree(edev->d_attrs_muex[index].attr.
832f68a8342SChanwoo Choi 					      name);
833f68a8342SChanwoo Choi 				}
834f68a8342SChanwoo Choi 				kfree(edev->d_attrs_muex);
835f68a8342SChanwoo Choi 				kfree(edev->attrs_muex);
836f68a8342SChanwoo Choi 				ret = -ENOMEM;
837f68a8342SChanwoo Choi 				goto err_muex;
838f68a8342SChanwoo Choi 			}
839f68a8342SChanwoo Choi 			strcpy(name, buf);
840f68a8342SChanwoo Choi 			sysfs_attr_init(&edev->d_attrs_muex[index].attr);
841f68a8342SChanwoo Choi 			edev->d_attrs_muex[index].attr.name = name;
842f68a8342SChanwoo Choi 			edev->d_attrs_muex[index].attr.mode = 0000;
843f68a8342SChanwoo Choi 			edev->attrs_muex[index] = &edev->d_attrs_muex[index]
844f68a8342SChanwoo Choi 							.attr;
845f68a8342SChanwoo Choi 		}
846f68a8342SChanwoo Choi 		edev->attr_g_muex.name = muex_name;
847f68a8342SChanwoo Choi 		edev->attr_g_muex.attrs = edev->attrs_muex;
848f68a8342SChanwoo Choi 
849f68a8342SChanwoo Choi 	}
850f68a8342SChanwoo Choi 
851f68a8342SChanwoo Choi 	if (edev->max_supported) {
852f68a8342SChanwoo Choi 		edev->extcon_dev_type.groups =
853f68a8342SChanwoo Choi 			kzalloc(sizeof(struct attribute_group *) *
854f68a8342SChanwoo Choi 				(edev->max_supported + 2), GFP_KERNEL);
855f68a8342SChanwoo Choi 		if (!edev->extcon_dev_type.groups) {
856f68a8342SChanwoo Choi 			ret = -ENOMEM;
857f68a8342SChanwoo Choi 			goto err_alloc_groups;
858f68a8342SChanwoo Choi 		}
859f68a8342SChanwoo Choi 
860f68a8342SChanwoo Choi 		edev->extcon_dev_type.name = dev_name(&edev->dev);
861f68a8342SChanwoo Choi 		edev->extcon_dev_type.release = dummy_sysfs_dev_release;
862f68a8342SChanwoo Choi 
863f68a8342SChanwoo Choi 		for (index = 0; index < edev->max_supported; index++)
864f68a8342SChanwoo Choi 			edev->extcon_dev_type.groups[index] =
865f68a8342SChanwoo Choi 				&edev->cables[index].attr_g;
866f68a8342SChanwoo Choi 		if (edev->mutually_exclusive)
867f68a8342SChanwoo Choi 			edev->extcon_dev_type.groups[index] =
868f68a8342SChanwoo Choi 				&edev->attr_g_muex;
869f68a8342SChanwoo Choi 
870f68a8342SChanwoo Choi 		edev->dev.type = &edev->extcon_dev_type;
871f68a8342SChanwoo Choi 	}
872f68a8342SChanwoo Choi 
873f68a8342SChanwoo Choi 	ret = device_register(&edev->dev);
874f68a8342SChanwoo Choi 	if (ret) {
875f68a8342SChanwoo Choi 		put_device(&edev->dev);
876f68a8342SChanwoo Choi 		goto err_dev;
877f68a8342SChanwoo Choi 	}
878f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
879f68a8342SChanwoo Choi 	if (switch_class)
880f68a8342SChanwoo Choi 		ret = class_compat_create_link(switch_class, &edev->dev, NULL);
881f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */
882f68a8342SChanwoo Choi 
883f68a8342SChanwoo Choi 	spin_lock_init(&edev->lock);
884f68a8342SChanwoo Choi 
885046050f6SChanwoo Choi 	edev->nh = devm_kzalloc(&edev->dev,
886046050f6SChanwoo Choi 			sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
887046050f6SChanwoo Choi 	if (!edev->nh) {
888046050f6SChanwoo Choi 		ret = -ENOMEM;
889046050f6SChanwoo Choi 		goto err_dev;
890046050f6SChanwoo Choi 	}
891046050f6SChanwoo Choi 
892046050f6SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
893046050f6SChanwoo Choi 		RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
894f68a8342SChanwoo Choi 
895f68a8342SChanwoo Choi 	dev_set_drvdata(&edev->dev, edev);
896f68a8342SChanwoo Choi 	edev->state = 0;
897f68a8342SChanwoo Choi 
898f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
899f68a8342SChanwoo Choi 	list_add(&edev->entry, &extcon_dev_list);
900f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
901f68a8342SChanwoo Choi 
902f68a8342SChanwoo Choi 	return 0;
903f68a8342SChanwoo Choi 
904f68a8342SChanwoo Choi err_dev:
905f68a8342SChanwoo Choi 	if (edev->max_supported)
906f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
907f68a8342SChanwoo Choi err_alloc_groups:
908f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
909f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
910f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
911f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
912f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
913f68a8342SChanwoo Choi 	}
914f68a8342SChanwoo Choi err_muex:
915f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
916f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
917f68a8342SChanwoo Choi err_alloc_cables:
918f68a8342SChanwoo Choi 	if (edev->max_supported)
919f68a8342SChanwoo Choi 		kfree(edev->cables);
920f68a8342SChanwoo Choi err_sysfs_alloc:
921f68a8342SChanwoo Choi 	return ret;
922f68a8342SChanwoo Choi }
923f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register);
924f68a8342SChanwoo Choi 
925f68a8342SChanwoo Choi /**
926f68a8342SChanwoo Choi  * extcon_dev_unregister() - Unregister the extcon device.
927f68a8342SChanwoo Choi  * @edev:	the extcon device instance to be unregistered.
928f68a8342SChanwoo Choi  *
929f68a8342SChanwoo Choi  * Note that this does not call kfree(edev) because edev was not allocated
930f68a8342SChanwoo Choi  * by this class.
931f68a8342SChanwoo Choi  */
932f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev)
933f68a8342SChanwoo Choi {
934f68a8342SChanwoo Choi 	int index;
935f68a8342SChanwoo Choi 
936f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
937f68a8342SChanwoo Choi 	list_del(&edev->entry);
938f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
939f68a8342SChanwoo Choi 
940f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
941f68a8342SChanwoo Choi 		dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
942f68a8342SChanwoo Choi 				dev_name(&edev->dev));
943f68a8342SChanwoo Choi 		return;
944f68a8342SChanwoo Choi 	}
945f68a8342SChanwoo Choi 
946f68a8342SChanwoo Choi 	device_unregister(&edev->dev);
947f68a8342SChanwoo Choi 
948f68a8342SChanwoo Choi 	if (edev->mutually_exclusive && edev->max_supported) {
949f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index];
950f68a8342SChanwoo Choi 				index++)
951f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
952f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
953f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
954f68a8342SChanwoo Choi 	}
955f68a8342SChanwoo Choi 
956f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
957f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
958f68a8342SChanwoo Choi 
959f68a8342SChanwoo Choi 	if (edev->max_supported) {
960f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
961f68a8342SChanwoo Choi 		kfree(edev->cables);
962f68a8342SChanwoo Choi 	}
963f68a8342SChanwoo Choi 
964f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
965f68a8342SChanwoo Choi 	if (switch_class)
966f68a8342SChanwoo Choi 		class_compat_remove_link(switch_class, &edev->dev, NULL);
967f68a8342SChanwoo Choi #endif
968f68a8342SChanwoo Choi 	put_device(&edev->dev);
969f68a8342SChanwoo Choi }
970f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister);
971f68a8342SChanwoo Choi 
972f68a8342SChanwoo Choi static void devm_extcon_dev_unreg(struct device *dev, void *res)
973f68a8342SChanwoo Choi {
974f68a8342SChanwoo Choi 	extcon_dev_unregister(*(struct extcon_dev **)res);
975f68a8342SChanwoo Choi }
976f68a8342SChanwoo Choi 
977f68a8342SChanwoo Choi /**
978f68a8342SChanwoo Choi  * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
979f68a8342SChanwoo Choi  * @dev:	device to allocate extcon device
980f68a8342SChanwoo Choi  * @edev:	the new extcon device to register
981f68a8342SChanwoo Choi  *
982f68a8342SChanwoo Choi  * Managed extcon_dev_register() function. If extcon device is attached with
983f68a8342SChanwoo Choi  * this function, that extcon device is automatically unregistered on driver
984f68a8342SChanwoo Choi  * detach. Internally this function calls extcon_dev_register() function.
985f68a8342SChanwoo Choi  * To get more information, refer that function.
986f68a8342SChanwoo Choi  *
987f68a8342SChanwoo Choi  * If extcon device is registered with this function and the device needs to be
988f68a8342SChanwoo Choi  * unregistered separately, devm_extcon_dev_unregister() should be used.
989f68a8342SChanwoo Choi  *
990f68a8342SChanwoo Choi  * Returns 0 if success or negaive error number if failure.
991f68a8342SChanwoo Choi  */
992f68a8342SChanwoo Choi int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
993f68a8342SChanwoo Choi {
994f68a8342SChanwoo Choi 	struct extcon_dev **ptr;
995f68a8342SChanwoo Choi 	int ret;
996f68a8342SChanwoo Choi 
997f68a8342SChanwoo Choi 	ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
998f68a8342SChanwoo Choi 	if (!ptr)
999f68a8342SChanwoo Choi 		return -ENOMEM;
1000f68a8342SChanwoo Choi 
1001f68a8342SChanwoo Choi 	ret = extcon_dev_register(edev);
1002f68a8342SChanwoo Choi 	if (ret) {
1003f68a8342SChanwoo Choi 		devres_free(ptr);
1004f68a8342SChanwoo Choi 		return ret;
1005f68a8342SChanwoo Choi 	}
1006f68a8342SChanwoo Choi 
1007f68a8342SChanwoo Choi 	*ptr = edev;
1008f68a8342SChanwoo Choi 	devres_add(dev, ptr);
1009f68a8342SChanwoo Choi 
1010f68a8342SChanwoo Choi 	return 0;
1011f68a8342SChanwoo Choi }
1012f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
1013f68a8342SChanwoo Choi 
1014f68a8342SChanwoo Choi /**
1015f68a8342SChanwoo Choi  * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1016f68a8342SChanwoo Choi  * @dev:	device the extcon belongs to
1017f68a8342SChanwoo Choi  * @edev:	the extcon device to unregister
1018f68a8342SChanwoo Choi  *
1019f68a8342SChanwoo Choi  * Unregister extcon device that is registered with devm_extcon_dev_register()
1020f68a8342SChanwoo Choi  * function.
1021f68a8342SChanwoo Choi  */
1022f68a8342SChanwoo Choi void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
1023f68a8342SChanwoo Choi {
1024f68a8342SChanwoo Choi 	WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1025f68a8342SChanwoo Choi 			       devm_extcon_dev_match, edev));
1026f68a8342SChanwoo Choi }
1027f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1028f68a8342SChanwoo Choi 
1029f68a8342SChanwoo Choi #ifdef CONFIG_OF
1030f68a8342SChanwoo Choi /*
1031f68a8342SChanwoo Choi  * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1032f68a8342SChanwoo Choi  * @dev - instance to the given device
1033f68a8342SChanwoo Choi  * @index - index into list of extcon_dev
1034f68a8342SChanwoo Choi  *
1035f68a8342SChanwoo Choi  * return the instance of extcon device
1036f68a8342SChanwoo Choi  */
1037f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1038f68a8342SChanwoo Choi {
1039f68a8342SChanwoo Choi 	struct device_node *node;
1040f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1041f68a8342SChanwoo Choi 
1042f68a8342SChanwoo Choi 	if (!dev->of_node) {
1043f68a8342SChanwoo Choi 		dev_err(dev, "device does not have a device node entry\n");
1044f68a8342SChanwoo Choi 		return ERR_PTR(-EINVAL);
1045f68a8342SChanwoo Choi 	}
1046f68a8342SChanwoo Choi 
1047f68a8342SChanwoo Choi 	node = of_parse_phandle(dev->of_node, "extcon", index);
1048f68a8342SChanwoo Choi 	if (!node) {
1049f68a8342SChanwoo Choi 		dev_err(dev, "failed to get phandle in %s node\n",
1050f68a8342SChanwoo Choi 			dev->of_node->full_name);
1051f68a8342SChanwoo Choi 		return ERR_PTR(-ENODEV);
1052f68a8342SChanwoo Choi 	}
1053f68a8342SChanwoo Choi 
1054f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1055f68a8342SChanwoo Choi 	list_for_each_entry(edev, &extcon_dev_list, entry) {
1056f68a8342SChanwoo Choi 		if (edev->dev.parent && edev->dev.parent->of_node == node) {
1057f68a8342SChanwoo Choi 			mutex_unlock(&extcon_dev_list_lock);
1058f68a8342SChanwoo Choi 			return edev;
1059f68a8342SChanwoo Choi 		}
1060f68a8342SChanwoo Choi 	}
1061f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1062f68a8342SChanwoo Choi 
1063f68a8342SChanwoo Choi 	return ERR_PTR(-EPROBE_DEFER);
1064f68a8342SChanwoo Choi }
1065f68a8342SChanwoo Choi #else
1066f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1067f68a8342SChanwoo Choi {
1068f68a8342SChanwoo Choi 	return ERR_PTR(-ENOSYS);
1069f68a8342SChanwoo Choi }
1070f68a8342SChanwoo Choi #endif /* CONFIG_OF */
1071f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1072f68a8342SChanwoo Choi 
1073707d7550SChanwoo Choi /**
1074707d7550SChanwoo Choi  * extcon_get_edev_name() - Get the name of the extcon device.
1075707d7550SChanwoo Choi  * @edev:	the extcon device
1076707d7550SChanwoo Choi  */
1077707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev)
1078707d7550SChanwoo Choi {
1079707d7550SChanwoo Choi 	return !edev ? NULL : edev->name;
1080707d7550SChanwoo Choi }
1081707d7550SChanwoo Choi 
1082f68a8342SChanwoo Choi static int __init extcon_class_init(void)
1083f68a8342SChanwoo Choi {
1084f68a8342SChanwoo Choi 	return create_extcon_class();
1085f68a8342SChanwoo Choi }
1086f68a8342SChanwoo Choi module_init(extcon_class_init);
1087f68a8342SChanwoo Choi 
1088f68a8342SChanwoo Choi static void __exit extcon_class_exit(void)
1089f68a8342SChanwoo Choi {
1090f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
1091f68a8342SChanwoo Choi 	class_compat_unregister(switch_class);
1092f68a8342SChanwoo Choi #endif
1093f68a8342SChanwoo Choi 	class_destroy(extcon_class);
1094f68a8342SChanwoo Choi }
1095f68a8342SChanwoo Choi module_exit(extcon_class_exit);
1096f68a8342SChanwoo Choi 
10972a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <[email protected]>");
1098f68a8342SChanwoo Choi MODULE_AUTHOR("Mike Lockwood <[email protected]>");
1099f68a8342SChanwoo Choi MODULE_AUTHOR("Donggeun Kim <[email protected]>");
1100f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <[email protected]>");
1101f68a8342SChanwoo Choi MODULE_DESCRIPTION("External connector (extcon) class driver");
1102f68a8342SChanwoo Choi MODULE_LICENSE("GPL");
1103