xref: /linux-6.15/drivers/extcon/extcon.c (revision 792e7e9e)
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 
4155e4e2f1SChanwoo Choi struct __extcon_info {
4255e4e2f1SChanwoo Choi 	unsigned int type;
4355e4e2f1SChanwoo Choi 	unsigned int id;
4455e4e2f1SChanwoo Choi 	const char *name;
4555e4e2f1SChanwoo Choi 
4655e4e2f1SChanwoo Choi } extcon_info[] = {
4755e4e2f1SChanwoo Choi 	[EXTCON_NONE] = {
4855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
4955e4e2f1SChanwoo Choi 		.id = EXTCON_NONE,
5055e4e2f1SChanwoo Choi 		.name = "NONE",
5155e4e2f1SChanwoo Choi 	},
5273b6ecdbSChanwoo Choi 
538e9bc36dSChanwoo Choi 	/* USB external connector */
5455e4e2f1SChanwoo Choi 	[EXTCON_USB] = {
5555e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_USB,
5655e4e2f1SChanwoo Choi 		.id = EXTCON_USB,
5755e4e2f1SChanwoo Choi 		.name = "USB",
5855e4e2f1SChanwoo Choi 	},
5955e4e2f1SChanwoo Choi 	[EXTCON_USB_HOST] = {
6055e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_USB,
6155e4e2f1SChanwoo Choi 		.id = EXTCON_USB_HOST,
6255e4e2f1SChanwoo Choi 		.name = "USB_HOST",
6355e4e2f1SChanwoo Choi 	},
648e9bc36dSChanwoo Choi 
6511eecf91SChanwoo Choi 	/* Charging external connector */
6655e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_SDP] = {
6755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
6855e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_SDP,
6955e4e2f1SChanwoo Choi 		.name = "SDP",
7055e4e2f1SChanwoo Choi 	},
7155e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_DCP] = {
7255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
7355e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_DCP,
7455e4e2f1SChanwoo Choi 		.name = "DCP",
7555e4e2f1SChanwoo Choi 	},
7655e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_CDP] = {
7755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
7855e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_CDP,
7955e4e2f1SChanwoo Choi 		.name = "CDP",
8055e4e2f1SChanwoo Choi 	},
8155e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_ACA] = {
8255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
8355e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_ACA,
8455e4e2f1SChanwoo Choi 		.name = "ACA",
8555e4e2f1SChanwoo Choi 	},
8655e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_FAST] = {
8755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
8855e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_FAST,
8955e4e2f1SChanwoo Choi 		.name = "FAST-CHARGER",
9055e4e2f1SChanwoo Choi 	},
9155e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_SLOW] = {
9255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
9355e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_SLOW,
9455e4e2f1SChanwoo Choi 		.name = "SLOW-CHARGER",
9555e4e2f1SChanwoo Choi 	},
968e9bc36dSChanwoo Choi 
9711eecf91SChanwoo Choi 	/* Jack external connector */
9855e4e2f1SChanwoo Choi 	[EXTCON_JACK_MICROPHONE] = {
9955e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
10055e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_MICROPHONE,
10155e4e2f1SChanwoo Choi 		.name = "MICROPHONE",
10255e4e2f1SChanwoo Choi 	},
10355e4e2f1SChanwoo Choi 	[EXTCON_JACK_HEADPHONE] = {
10455e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
10555e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_HEADPHONE,
10655e4e2f1SChanwoo Choi 		.name = "HEADPHONE",
10755e4e2f1SChanwoo Choi 	},
10855e4e2f1SChanwoo Choi 	[EXTCON_JACK_LINE_IN] = {
10955e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
11055e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_LINE_IN,
11155e4e2f1SChanwoo Choi 		.name = "LINE-IN",
11255e4e2f1SChanwoo Choi 	},
11355e4e2f1SChanwoo Choi 	[EXTCON_JACK_LINE_OUT] = {
11455e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
11555e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_LINE_OUT,
11655e4e2f1SChanwoo Choi 		.name = "LINE-OUT",
11755e4e2f1SChanwoo Choi 	},
11855e4e2f1SChanwoo Choi 	[EXTCON_JACK_VIDEO_IN] = {
11955e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
12055e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_VIDEO_IN,
12155e4e2f1SChanwoo Choi 		.name = "VIDEO-IN",
12255e4e2f1SChanwoo Choi 	},
12355e4e2f1SChanwoo Choi 	[EXTCON_JACK_VIDEO_OUT] = {
12455e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
12555e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_VIDEO_OUT,
12655e4e2f1SChanwoo Choi 		.name = "VIDEO-OUT",
12755e4e2f1SChanwoo Choi 	},
12855e4e2f1SChanwoo Choi 	[EXTCON_JACK_SPDIF_IN] = {
12955e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
13055e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_SPDIF_IN,
13155e4e2f1SChanwoo Choi 		.name = "SPDIF-IN",
13255e4e2f1SChanwoo Choi 	},
13355e4e2f1SChanwoo Choi 	[EXTCON_JACK_SPDIF_OUT] = {
13455e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
13555e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_SPDIF_OUT,
13655e4e2f1SChanwoo Choi 		.name = "SPDIF-OUT",
13755e4e2f1SChanwoo Choi 	},
1388e9bc36dSChanwoo Choi 
13911eecf91SChanwoo Choi 	/* Display external connector */
14055e4e2f1SChanwoo Choi 	[EXTCON_DISP_HDMI] = {
14155e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
14255e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_HDMI,
14355e4e2f1SChanwoo Choi 		.name = "HDMI",
14455e4e2f1SChanwoo Choi 	},
14555e4e2f1SChanwoo Choi 	[EXTCON_DISP_MHL] = {
14655e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
14755e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_MHL,
14855e4e2f1SChanwoo Choi 		.name = "MHL",
14955e4e2f1SChanwoo Choi 	},
15055e4e2f1SChanwoo Choi 	[EXTCON_DISP_DVI] = {
15155e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
15255e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_DVI,
15355e4e2f1SChanwoo Choi 		.name = "DVI",
15455e4e2f1SChanwoo Choi 	},
15555e4e2f1SChanwoo Choi 	[EXTCON_DISP_VGA] = {
15655e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
15755e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_VGA,
15855e4e2f1SChanwoo Choi 		.name = "VGA",
15955e4e2f1SChanwoo Choi 	},
1608e9bc36dSChanwoo Choi 
16111eecf91SChanwoo Choi 	/* Miscellaneous external connector */
16255e4e2f1SChanwoo Choi 	[EXTCON_DOCK] = {
16355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
16455e4e2f1SChanwoo Choi 		.id = EXTCON_DOCK,
16555e4e2f1SChanwoo Choi 		.name = "DOCK",
16655e4e2f1SChanwoo Choi 	},
16755e4e2f1SChanwoo Choi 	[EXTCON_JIG] = {
16855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
16955e4e2f1SChanwoo Choi 		.id = EXTCON_JIG,
17055e4e2f1SChanwoo Choi 		.name = "JIG",
17155e4e2f1SChanwoo Choi 	},
17255e4e2f1SChanwoo Choi 	[EXTCON_MECHANICAL] = {
17355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
17455e4e2f1SChanwoo Choi 		.id = EXTCON_MECHANICAL,
17555e4e2f1SChanwoo Choi 		.name = "MECHANICAL",
17655e4e2f1SChanwoo Choi 	},
1778e9bc36dSChanwoo Choi 
17855e4e2f1SChanwoo Choi 	{ /* sentinel */ }
179f68a8342SChanwoo Choi };
180f68a8342SChanwoo Choi 
18120f7b53dSChanwoo Choi /**
18220f7b53dSChanwoo Choi  * struct extcon_cable - An internal data for each cable of extcon device.
18320f7b53dSChanwoo Choi  * @edev:		The extcon device
18420f7b53dSChanwoo Choi  * @cable_index:	Index of this cable in the edev
18520f7b53dSChanwoo Choi  * @attr_g:		Attribute group for the cable
18620f7b53dSChanwoo Choi  * @attr_name:		"name" sysfs entry
18720f7b53dSChanwoo Choi  * @attr_state:		"state" sysfs entry
18820f7b53dSChanwoo Choi  * @attrs:		Array pointing to attr_name and attr_state for attr_g
18920f7b53dSChanwoo Choi  */
19020f7b53dSChanwoo Choi struct extcon_cable {
19120f7b53dSChanwoo Choi 	struct extcon_dev *edev;
19220f7b53dSChanwoo Choi 	int cable_index;
19320f7b53dSChanwoo Choi 
19420f7b53dSChanwoo Choi 	struct attribute_group attr_g;
19520f7b53dSChanwoo Choi 	struct device_attribute attr_name;
19620f7b53dSChanwoo Choi 	struct device_attribute attr_state;
19720f7b53dSChanwoo Choi 
19820f7b53dSChanwoo Choi 	struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
199*792e7e9eSChanwoo Choi 
200*792e7e9eSChanwoo Choi 	union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
201*792e7e9eSChanwoo Choi 	union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
202*792e7e9eSChanwoo Choi 	union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
203*792e7e9eSChanwoo Choi 	union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
20420f7b53dSChanwoo Choi };
20520f7b53dSChanwoo Choi 
206f68a8342SChanwoo Choi static struct class *extcon_class;
207f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
208f68a8342SChanwoo Choi static struct class_compat *switch_class;
209f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */
210f68a8342SChanwoo Choi 
211f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list);
212f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock);
213f68a8342SChanwoo Choi 
214f68a8342SChanwoo Choi /**
215f68a8342SChanwoo Choi  * check_mutually_exclusive - Check if new_state violates mutually_exclusive
216f68a8342SChanwoo Choi  *			      condition.
217f68a8342SChanwoo Choi  * @edev:	the extcon device
218f68a8342SChanwoo Choi  * @new_state:	new cable attach status for @edev
219f68a8342SChanwoo Choi  *
220f68a8342SChanwoo Choi  * Returns 0 if nothing violates. Returns the index + 1 for the first
221f68a8342SChanwoo Choi  * violated condition.
222f68a8342SChanwoo Choi  */
223f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
224f68a8342SChanwoo Choi {
225f68a8342SChanwoo Choi 	int i = 0;
226f68a8342SChanwoo Choi 
227f68a8342SChanwoo Choi 	if (!edev->mutually_exclusive)
228f68a8342SChanwoo Choi 		return 0;
229f68a8342SChanwoo Choi 
230f68a8342SChanwoo Choi 	for (i = 0; edev->mutually_exclusive[i]; i++) {
231f68a8342SChanwoo Choi 		int weight;
232f68a8342SChanwoo Choi 		u32 correspondants = new_state & edev->mutually_exclusive[i];
233f68a8342SChanwoo Choi 
234f68a8342SChanwoo Choi 		/* calculate the total number of bits set */
235f68a8342SChanwoo Choi 		weight = hweight32(correspondants);
236f68a8342SChanwoo Choi 		if (weight > 1)
237f68a8342SChanwoo Choi 			return i + 1;
238f68a8342SChanwoo Choi 	}
239f68a8342SChanwoo Choi 
240f68a8342SChanwoo Choi 	return 0;
241f68a8342SChanwoo Choi }
242f68a8342SChanwoo Choi 
24373b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
2442a9de9c0SChanwoo Choi {
2452a9de9c0SChanwoo Choi 	int i;
2462a9de9c0SChanwoo Choi 
2472a9de9c0SChanwoo Choi 	/* Find the the index of extcon cable in edev->supported_cable */
2482a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
2492a9de9c0SChanwoo Choi 		if (edev->supported_cable[i] == id)
2502a9de9c0SChanwoo Choi 			return i;
2512a9de9c0SChanwoo Choi 	}
2522a9de9c0SChanwoo Choi 
2532a9de9c0SChanwoo Choi 	return -EINVAL;
2542a9de9c0SChanwoo Choi }
2552a9de9c0SChanwoo Choi 
256*792e7e9eSChanwoo Choi static int get_extcon_type(unsigned int prop)
257*792e7e9eSChanwoo Choi {
258*792e7e9eSChanwoo Choi 	switch (prop) {
259*792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
260*792e7e9eSChanwoo Choi 		return EXTCON_TYPE_USB;
261*792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
262*792e7e9eSChanwoo Choi 		return EXTCON_TYPE_CHG;
263*792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
264*792e7e9eSChanwoo Choi 		return EXTCON_TYPE_JACK;
265*792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
266*792e7e9eSChanwoo Choi 		return EXTCON_TYPE_DISP;
267*792e7e9eSChanwoo Choi 	default:
268*792e7e9eSChanwoo Choi 		return -EINVAL;
269*792e7e9eSChanwoo Choi 	}
270*792e7e9eSChanwoo Choi }
271*792e7e9eSChanwoo Choi 
272*792e7e9eSChanwoo Choi static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
273*792e7e9eSChanwoo Choi {
274*792e7e9eSChanwoo Choi 	return !!(edev->state & BIT(index));
275*792e7e9eSChanwoo Choi }
276*792e7e9eSChanwoo Choi 
277046050f6SChanwoo Choi static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
278046050f6SChanwoo Choi {
279046050f6SChanwoo Choi 	if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
280f4513b06SHans de Goede 		*attached = ((new >> idx) & 0x1) ? true : false;
281046050f6SChanwoo Choi 		return true;
282046050f6SChanwoo Choi 	}
283046050f6SChanwoo Choi 
284046050f6SChanwoo Choi 	return false;
285046050f6SChanwoo Choi }
286046050f6SChanwoo Choi 
287*792e7e9eSChanwoo Choi static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
288*792e7e9eSChanwoo Choi {
289*792e7e9eSChanwoo Choi 	int type;
290*792e7e9eSChanwoo Choi 
291*792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not. */
292*792e7e9eSChanwoo Choi 	type = get_extcon_type(prop);
293*792e7e9eSChanwoo Choi 	if (type < 0)
294*792e7e9eSChanwoo Choi 		return false;
295*792e7e9eSChanwoo Choi 
296*792e7e9eSChanwoo Choi 	/* Check whether a specific extcon id supports the property or not. */
297*792e7e9eSChanwoo Choi 	return !!(extcon_info[id].type & type);
298*792e7e9eSChanwoo Choi }
299*792e7e9eSChanwoo Choi 
300*792e7e9eSChanwoo Choi static void init_property(struct extcon_dev *edev, unsigned int id, int index)
301*792e7e9eSChanwoo Choi {
302*792e7e9eSChanwoo Choi 	unsigned int type = extcon_info[id].type;
303*792e7e9eSChanwoo Choi 	struct extcon_cable *cable = &edev->cables[index];
304*792e7e9eSChanwoo Choi 
305*792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_USB & type)
306*792e7e9eSChanwoo Choi 		memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
307*792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_CHG & type)
308*792e7e9eSChanwoo Choi 		memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
309*792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_JACK & type)
310*792e7e9eSChanwoo Choi 		memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
311*792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_DISP & type)
312*792e7e9eSChanwoo Choi 		memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
313*792e7e9eSChanwoo Choi }
314*792e7e9eSChanwoo Choi 
315f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr,
316f68a8342SChanwoo Choi 			  char *buf)
317f68a8342SChanwoo Choi {
318f68a8342SChanwoo Choi 	int i, count = 0;
319f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
320f68a8342SChanwoo Choi 
321f68a8342SChanwoo Choi 	if (edev->max_supported == 0)
322f68a8342SChanwoo Choi 		return sprintf(buf, "%u\n", edev->state);
323f68a8342SChanwoo Choi 
3242a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
325f68a8342SChanwoo Choi 		count += sprintf(buf + count, "%s=%d\n",
32655e4e2f1SChanwoo Choi 				extcon_info[edev->supported_cable[i]].name,
327f68a8342SChanwoo Choi 				 !!(edev->state & (1 << i)));
328f68a8342SChanwoo Choi 	}
329f68a8342SChanwoo Choi 
330f68a8342SChanwoo Choi 	return count;
331f68a8342SChanwoo Choi }
3325d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state);
333f68a8342SChanwoo Choi 
334f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr,
335f68a8342SChanwoo Choi 		char *buf)
336f68a8342SChanwoo Choi {
337f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
338f68a8342SChanwoo Choi 
33971c3ffa5SChanwoo Choi 	return sprintf(buf, "%s\n", edev->name);
340f68a8342SChanwoo Choi }
341f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name);
342f68a8342SChanwoo Choi 
343f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev,
344f68a8342SChanwoo Choi 			       struct device_attribute *attr, char *buf)
345f68a8342SChanwoo Choi {
346f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
347f68a8342SChanwoo Choi 						  attr_name);
3482a9de9c0SChanwoo Choi 	int i = cable->cable_index;
349f68a8342SChanwoo Choi 
350f68a8342SChanwoo Choi 	return sprintf(buf, "%s\n",
35155e4e2f1SChanwoo Choi 			extcon_info[cable->edev->supported_cable[i]].name);
352f68a8342SChanwoo Choi }
353f68a8342SChanwoo Choi 
354f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev,
355f68a8342SChanwoo Choi 				struct device_attribute *attr, char *buf)
356f68a8342SChanwoo Choi {
357f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
358f68a8342SChanwoo Choi 						  attr_state);
359f68a8342SChanwoo Choi 
360be052cc8SRoger Quadros 	int i = cable->cable_index;
361be052cc8SRoger Quadros 
362f68a8342SChanwoo Choi 	return sprintf(buf, "%d\n",
363f68a8342SChanwoo Choi 		       extcon_get_cable_state_(cable->edev,
364be052cc8SRoger Quadros 					       cable->edev->supported_cable[i]));
365f68a8342SChanwoo Choi }
366f68a8342SChanwoo Choi 
367f68a8342SChanwoo Choi /**
368f68a8342SChanwoo Choi  * extcon_update_state() - Update the cable attach states of the extcon device
369f68a8342SChanwoo Choi  *			   only for the masked bits.
370f68a8342SChanwoo Choi  * @edev:	the extcon device
371f68a8342SChanwoo Choi  * @mask:	the bit mask to designate updated bits.
372f68a8342SChanwoo Choi  * @state:	new cable attach status for @edev
373f68a8342SChanwoo Choi  *
374f68a8342SChanwoo Choi  * Changing the state sends uevent with environment variable containing
375f68a8342SChanwoo Choi  * the name of extcon device (envp[0]) and the state output (envp[1]).
376f68a8342SChanwoo Choi  * Tizen uses this format for extcon device to get events from ports.
377f68a8342SChanwoo Choi  * Android uses this format as well.
378f68a8342SChanwoo Choi  *
379f68a8342SChanwoo Choi  * Note that the notifier provides which bits are changed in the state
380f68a8342SChanwoo Choi  * variable with the val parameter (second) to the callback.
381f68a8342SChanwoo Choi  */
382912465bcSChanwoo Choi static int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
383f68a8342SChanwoo Choi {
384f68a8342SChanwoo Choi 	char name_buf[120];
385f68a8342SChanwoo Choi 	char state_buf[120];
386f68a8342SChanwoo Choi 	char *prop_buf;
387f68a8342SChanwoo Choi 	char *envp[3];
388f68a8342SChanwoo Choi 	int env_offset = 0;
389f68a8342SChanwoo Choi 	int length;
390046050f6SChanwoo Choi 	int index;
391f68a8342SChanwoo Choi 	unsigned long flags;
392046050f6SChanwoo Choi 	bool attached;
393f68a8342SChanwoo Choi 
3947eae43aeSChanwoo Choi 	if (!edev)
3957eae43aeSChanwoo Choi 		return -EINVAL;
3967eae43aeSChanwoo Choi 
397f68a8342SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
398f68a8342SChanwoo Choi 
399f68a8342SChanwoo Choi 	if (edev->state != ((edev->state & ~mask) | (state & mask))) {
400f7a89811SRoger Quadros 		u32 old_state;
401f7a89811SRoger Quadros 
402f68a8342SChanwoo Choi 		if (check_mutually_exclusive(edev, (edev->state & ~mask) |
403f68a8342SChanwoo Choi 						   (state & mask))) {
404f68a8342SChanwoo Choi 			spin_unlock_irqrestore(&edev->lock, flags);
405f68a8342SChanwoo Choi 			return -EPERM;
406f68a8342SChanwoo Choi 		}
407f68a8342SChanwoo Choi 
408f7a89811SRoger Quadros 		old_state = edev->state;
409f68a8342SChanwoo Choi 		edev->state &= ~mask;
410f68a8342SChanwoo Choi 		edev->state |= state & mask;
411f68a8342SChanwoo Choi 
412f7a89811SRoger Quadros 		for (index = 0; index < edev->max_supported; index++) {
413f7a89811SRoger Quadros 			if (is_extcon_changed(old_state, edev->state, index,
414f7a89811SRoger Quadros 					      &attached))
415f7a89811SRoger Quadros 				raw_notifier_call_chain(&edev->nh[index],
416f7a89811SRoger Quadros 							attached, edev);
417f7a89811SRoger Quadros 		}
418f7a89811SRoger Quadros 
419f68a8342SChanwoo Choi 		/* This could be in interrupt handler */
420f68a8342SChanwoo Choi 		prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
421f68a8342SChanwoo Choi 		if (prop_buf) {
422f68a8342SChanwoo Choi 			length = name_show(&edev->dev, NULL, prop_buf);
423f68a8342SChanwoo Choi 			if (length > 0) {
424f68a8342SChanwoo Choi 				if (prop_buf[length - 1] == '\n')
425f68a8342SChanwoo Choi 					prop_buf[length - 1] = 0;
426f68a8342SChanwoo Choi 				snprintf(name_buf, sizeof(name_buf),
427f68a8342SChanwoo Choi 					"NAME=%s", prop_buf);
428f68a8342SChanwoo Choi 				envp[env_offset++] = name_buf;
429f68a8342SChanwoo Choi 			}
430f68a8342SChanwoo Choi 			length = state_show(&edev->dev, NULL, prop_buf);
431f68a8342SChanwoo Choi 			if (length > 0) {
432f68a8342SChanwoo Choi 				if (prop_buf[length - 1] == '\n')
433f68a8342SChanwoo Choi 					prop_buf[length - 1] = 0;
434f68a8342SChanwoo Choi 				snprintf(state_buf, sizeof(state_buf),
435f68a8342SChanwoo Choi 					"STATE=%s", prop_buf);
436f68a8342SChanwoo Choi 				envp[env_offset++] = state_buf;
437f68a8342SChanwoo Choi 			}
438f68a8342SChanwoo Choi 			envp[env_offset] = NULL;
439f68a8342SChanwoo Choi 			/* Unlock early before uevent */
440f68a8342SChanwoo Choi 			spin_unlock_irqrestore(&edev->lock, flags);
441f68a8342SChanwoo Choi 
442f68a8342SChanwoo Choi 			kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
443f68a8342SChanwoo Choi 			free_page((unsigned long)prop_buf);
444f68a8342SChanwoo Choi 		} else {
445f68a8342SChanwoo Choi 			/* Unlock early before uevent */
446f68a8342SChanwoo Choi 			spin_unlock_irqrestore(&edev->lock, flags);
447f68a8342SChanwoo Choi 
448f68a8342SChanwoo Choi 			dev_err(&edev->dev, "out of memory in extcon_set_state\n");
449f68a8342SChanwoo Choi 			kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
450f68a8342SChanwoo Choi 		}
451f68a8342SChanwoo Choi 	} else {
452f68a8342SChanwoo Choi 		/* No changes */
453f68a8342SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
454f68a8342SChanwoo Choi 	}
455f68a8342SChanwoo Choi 
456f68a8342SChanwoo Choi 	return 0;
457f68a8342SChanwoo Choi }
458f68a8342SChanwoo Choi 
459f68a8342SChanwoo Choi /**
460f68a8342SChanwoo Choi  * extcon_get_cable_state_() - Get the status of a specific cable.
461f68a8342SChanwoo Choi  * @edev:	the extcon device that has the cable.
4622a9de9c0SChanwoo Choi  * @id:		the unique id of each external connector in extcon enumeration.
463f68a8342SChanwoo Choi  */
46473b6ecdbSChanwoo Choi int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
465f68a8342SChanwoo Choi {
4662a9de9c0SChanwoo Choi 	int index;
4672a9de9c0SChanwoo Choi 
4687eae43aeSChanwoo Choi 	if (!edev)
4697eae43aeSChanwoo Choi 		return -EINVAL;
4707eae43aeSChanwoo Choi 
4712a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
4722a9de9c0SChanwoo Choi 	if (index < 0)
4732a9de9c0SChanwoo Choi 		return index;
4742a9de9c0SChanwoo Choi 
4752a9de9c0SChanwoo Choi 	if (edev->max_supported && edev->max_supported <= index)
476f68a8342SChanwoo Choi 		return -EINVAL;
477f68a8342SChanwoo Choi 
478*792e7e9eSChanwoo Choi 	return is_extcon_attached(edev, index);
479f68a8342SChanwoo Choi }
480f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
481f68a8342SChanwoo Choi 
482f68a8342SChanwoo Choi /**
483f68a8342SChanwoo Choi  * extcon_set_cable_state_() - Set the status of a specific cable.
484f68a8342SChanwoo Choi  * @edev:		the extcon device that has the cable.
4852a9de9c0SChanwoo Choi  * @id:			the unique id of each external connector
4862a9de9c0SChanwoo Choi  *			in extcon enumeration.
4872a9de9c0SChanwoo Choi  * @state:		the new cable status. The default semantics is
488f68a8342SChanwoo Choi  *			true: attached / false: detached.
489f68a8342SChanwoo Choi  */
49073b6ecdbSChanwoo Choi int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
4912a9de9c0SChanwoo Choi 				bool cable_state)
492f68a8342SChanwoo Choi {
493f68a8342SChanwoo Choi 	u32 state;
4942a9de9c0SChanwoo Choi 	int index;
495f68a8342SChanwoo Choi 
4967eae43aeSChanwoo Choi 	if (!edev)
4977eae43aeSChanwoo Choi 		return -EINVAL;
4987eae43aeSChanwoo Choi 
4992a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
5002a9de9c0SChanwoo Choi 	if (index < 0)
5012a9de9c0SChanwoo Choi 		return index;
5022a9de9c0SChanwoo Choi 
5032a9de9c0SChanwoo Choi 	if (edev->max_supported && edev->max_supported <= index)
504f68a8342SChanwoo Choi 		return -EINVAL;
505f68a8342SChanwoo Choi 
506*792e7e9eSChanwoo Choi 	/*
507*792e7e9eSChanwoo Choi 	 * Initialize the value of extcon property before setting
508*792e7e9eSChanwoo Choi 	 * the detached state for an external connector.
509*792e7e9eSChanwoo Choi 	 */
510*792e7e9eSChanwoo Choi 	if (!cable_state)
511*792e7e9eSChanwoo Choi 		init_property(edev, id, index);
512*792e7e9eSChanwoo Choi 
513f68a8342SChanwoo Choi 	state = cable_state ? (1 << index) : 0;
514f68a8342SChanwoo Choi 	return extcon_update_state(edev, 1 << index, state);
515f68a8342SChanwoo Choi }
516f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
517f68a8342SChanwoo Choi 
518f68a8342SChanwoo Choi /**
519*792e7e9eSChanwoo Choi  * extcon_get_property() - Get the property value of a specific cable.
520*792e7e9eSChanwoo Choi  * @edev:		the extcon device that has the cable.
521*792e7e9eSChanwoo Choi  * @id:			the unique id of each external connector
522*792e7e9eSChanwoo Choi  *			in extcon enumeration.
523*792e7e9eSChanwoo Choi  * @prop:		the property id among enum extcon_property.
524*792e7e9eSChanwoo Choi  * @prop_val:		the pointer which store the value of property.
525*792e7e9eSChanwoo Choi  *
526*792e7e9eSChanwoo Choi  * When getting the property value of external connector, the external connector
527*792e7e9eSChanwoo Choi  * should be attached. If detached state, function just return 0 without
528*792e7e9eSChanwoo Choi  * property value. Also, the each property should be included in the list of
529*792e7e9eSChanwoo Choi  * supported properties according to the type of external connectors.
530*792e7e9eSChanwoo Choi  *
531*792e7e9eSChanwoo Choi  * Returns 0 if success or error number if fail
532*792e7e9eSChanwoo Choi  */
533*792e7e9eSChanwoo Choi int extcon_get_property(struct extcon_dev *edev, unsigned int id,
534*792e7e9eSChanwoo Choi 				unsigned int prop,
535*792e7e9eSChanwoo Choi 				union extcon_property_value *prop_val)
536*792e7e9eSChanwoo Choi {
537*792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
538*792e7e9eSChanwoo Choi 	unsigned long flags;
539*792e7e9eSChanwoo Choi 	int index, ret = 0;
540*792e7e9eSChanwoo Choi 
541*792e7e9eSChanwoo Choi 	*prop_val = (union extcon_property_value)(0);
542*792e7e9eSChanwoo Choi 
543*792e7e9eSChanwoo Choi 	if (!edev)
544*792e7e9eSChanwoo Choi 		return -EINVAL;
545*792e7e9eSChanwoo Choi 
546*792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
547*792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
548*792e7e9eSChanwoo Choi 		return -EINVAL;
549*792e7e9eSChanwoo Choi 
550*792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
551*792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
552*792e7e9eSChanwoo Choi 	if (index < 0)
553*792e7e9eSChanwoo Choi 		return index;
554*792e7e9eSChanwoo Choi 
555*792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
556*792e7e9eSChanwoo Choi 
557*792e7e9eSChanwoo Choi 	/*
558*792e7e9eSChanwoo Choi 	 * Check whether the external connector is attached.
559*792e7e9eSChanwoo Choi 	 * If external connector is detached, the user can not
560*792e7e9eSChanwoo Choi 	 * get the property value.
561*792e7e9eSChanwoo Choi 	 */
562*792e7e9eSChanwoo Choi 	if (!is_extcon_attached(edev, index)) {
563*792e7e9eSChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
564*792e7e9eSChanwoo Choi 		return 0;
565*792e7e9eSChanwoo Choi 	}
566*792e7e9eSChanwoo Choi 
567*792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
568*792e7e9eSChanwoo Choi 
569*792e7e9eSChanwoo Choi 	/* Get the property value according to extcon type */
570*792e7e9eSChanwoo Choi 	switch (prop) {
571*792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
572*792e7e9eSChanwoo Choi 		*prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
573*792e7e9eSChanwoo Choi 		break;
574*792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
575*792e7e9eSChanwoo Choi 		*prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
576*792e7e9eSChanwoo Choi 		break;
577*792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
578*792e7e9eSChanwoo Choi 		*prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
579*792e7e9eSChanwoo Choi 		break;
580*792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
581*792e7e9eSChanwoo Choi 		*prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
582*792e7e9eSChanwoo Choi 		break;
583*792e7e9eSChanwoo Choi 	default:
584*792e7e9eSChanwoo Choi 		ret = -EINVAL;
585*792e7e9eSChanwoo Choi 		break;
586*792e7e9eSChanwoo Choi 	}
587*792e7e9eSChanwoo Choi 
588*792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
589*792e7e9eSChanwoo Choi 
590*792e7e9eSChanwoo Choi 	return ret;
591*792e7e9eSChanwoo Choi }
592*792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property);
593*792e7e9eSChanwoo Choi 
594*792e7e9eSChanwoo Choi /**
595*792e7e9eSChanwoo Choi  * extcon_set_property() - Set the property value of a specific cable.
596*792e7e9eSChanwoo Choi  * @edev:		the extcon device that has the cable.
597*792e7e9eSChanwoo Choi  * @id:			the unique id of each external connector
598*792e7e9eSChanwoo Choi  *			in extcon enumeration.
599*792e7e9eSChanwoo Choi  * @prop:		the property id among enum extcon_property.
600*792e7e9eSChanwoo Choi  * @prop_val:		the pointer including the new value of property.
601*792e7e9eSChanwoo Choi  *
602*792e7e9eSChanwoo Choi  * The each property should be included in the list of supported properties
603*792e7e9eSChanwoo Choi  * according to the type of external connectors.
604*792e7e9eSChanwoo Choi  *
605*792e7e9eSChanwoo Choi  * Returns 0 if success or error number if fail
606*792e7e9eSChanwoo Choi  */
607*792e7e9eSChanwoo Choi int extcon_set_property(struct extcon_dev *edev, unsigned int id,
608*792e7e9eSChanwoo Choi 				unsigned int prop,
609*792e7e9eSChanwoo Choi 				union extcon_property_value prop_val)
610*792e7e9eSChanwoo Choi {
611*792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
612*792e7e9eSChanwoo Choi 	unsigned long flags;
613*792e7e9eSChanwoo Choi 	int index, ret = 0;
614*792e7e9eSChanwoo Choi 
615*792e7e9eSChanwoo Choi 	if (!edev)
616*792e7e9eSChanwoo Choi 		return -EINVAL;
617*792e7e9eSChanwoo Choi 
618*792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
619*792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
620*792e7e9eSChanwoo Choi 		return -EINVAL;
621*792e7e9eSChanwoo Choi 
622*792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
623*792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
624*792e7e9eSChanwoo Choi 	if (index < 0)
625*792e7e9eSChanwoo Choi 		return index;
626*792e7e9eSChanwoo Choi 
627*792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
628*792e7e9eSChanwoo Choi 
629*792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
630*792e7e9eSChanwoo Choi 
631*792e7e9eSChanwoo Choi 	/* Set the property value according to extcon type */
632*792e7e9eSChanwoo Choi 	switch (prop) {
633*792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
634*792e7e9eSChanwoo Choi 		cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
635*792e7e9eSChanwoo Choi 		break;
636*792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
637*792e7e9eSChanwoo Choi 		cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
638*792e7e9eSChanwoo Choi 		break;
639*792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
640*792e7e9eSChanwoo Choi 		cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
641*792e7e9eSChanwoo Choi 		break;
642*792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
643*792e7e9eSChanwoo Choi 		cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
644*792e7e9eSChanwoo Choi 		break;
645*792e7e9eSChanwoo Choi 	default:
646*792e7e9eSChanwoo Choi 		ret = -EINVAL;
647*792e7e9eSChanwoo Choi 		break;
648*792e7e9eSChanwoo Choi 	}
649*792e7e9eSChanwoo Choi 
650*792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
651*792e7e9eSChanwoo Choi 
652*792e7e9eSChanwoo Choi 	return ret;
653*792e7e9eSChanwoo Choi }
654*792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property);
655*792e7e9eSChanwoo Choi 
656*792e7e9eSChanwoo Choi /**
657f68a8342SChanwoo Choi  * extcon_get_extcon_dev() - Get the extcon device instance from the name
658f68a8342SChanwoo Choi  * @extcon_name:	The extcon name provided with extcon_dev_register()
659f68a8342SChanwoo Choi  */
660f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
661f68a8342SChanwoo Choi {
662f68a8342SChanwoo Choi 	struct extcon_dev *sd;
663f68a8342SChanwoo Choi 
6647eae43aeSChanwoo Choi 	if (!extcon_name)
6657eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
6667eae43aeSChanwoo Choi 
667f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
668f68a8342SChanwoo Choi 	list_for_each_entry(sd, &extcon_dev_list, entry) {
669f68a8342SChanwoo Choi 		if (!strcmp(sd->name, extcon_name))
670f68a8342SChanwoo Choi 			goto out;
671f68a8342SChanwoo Choi 	}
672f68a8342SChanwoo Choi 	sd = NULL;
673f68a8342SChanwoo Choi out:
674f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
675f68a8342SChanwoo Choi 	return sd;
676f68a8342SChanwoo Choi }
677f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
678f68a8342SChanwoo Choi 
679f68a8342SChanwoo Choi /**
680f68a8342SChanwoo Choi  * extcon_register_notifier() - Register a notifiee to get notified by
681f68a8342SChanwoo Choi  *				any attach status changes from the extcon.
682046050f6SChanwoo Choi  * @edev:	the extcon device that has the external connecotr.
683046050f6SChanwoo Choi  * @id:		the unique id of each external connector in extcon enumeration.
684f68a8342SChanwoo Choi  * @nb:		a notifier block to be registered.
685f68a8342SChanwoo Choi  *
686f68a8342SChanwoo Choi  * Note that the second parameter given to the callback of nb (val) is
687f68a8342SChanwoo Choi  * "old_state", not the current state. The current state can be retrieved
688f68a8342SChanwoo Choi  * by looking at the third pameter (edev pointer)'s state value.
689f68a8342SChanwoo Choi  */
69073b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
691f68a8342SChanwoo Choi 			     struct notifier_block *nb)
692f68a8342SChanwoo Choi {
69366bee35fSHans de Goede 	unsigned long flags;
6942c8116a1SManinder Singh 	int ret, idx = -EINVAL;
695046050f6SChanwoo Choi 
696830ae442SChanwoo Choi 	if (!nb)
6977eae43aeSChanwoo Choi 		return -EINVAL;
6987eae43aeSChanwoo Choi 
699830ae442SChanwoo Choi 	if (edev) {
700046050f6SChanwoo Choi 		idx = find_cable_index_by_id(edev, id);
701a05f44c8SStephen Boyd 		if (idx < 0)
702a05f44c8SStephen Boyd 			return idx;
70366bee35fSHans de Goede 
70466bee35fSHans de Goede 		spin_lock_irqsave(&edev->lock, flags);
705046050f6SChanwoo Choi 		ret = raw_notifier_chain_register(&edev->nh[idx], nb);
70666bee35fSHans de Goede 		spin_unlock_irqrestore(&edev->lock, flags);
707830ae442SChanwoo Choi 	} else {
708830ae442SChanwoo Choi 		struct extcon_dev *extd;
709830ae442SChanwoo Choi 
710830ae442SChanwoo Choi 		mutex_lock(&extcon_dev_list_lock);
711830ae442SChanwoo Choi 		list_for_each_entry(extd, &extcon_dev_list, entry) {
712830ae442SChanwoo Choi 			idx = find_cable_index_by_id(extd, id);
713830ae442SChanwoo Choi 			if (idx >= 0)
714830ae442SChanwoo Choi 				break;
715830ae442SChanwoo Choi 		}
716830ae442SChanwoo Choi 		mutex_unlock(&extcon_dev_list_lock);
717830ae442SChanwoo Choi 
718830ae442SChanwoo Choi 		if (idx >= 0) {
719830ae442SChanwoo Choi 			edev = extd;
720830ae442SChanwoo Choi 			return extcon_register_notifier(extd, id, nb);
721830ae442SChanwoo Choi 		} else {
722830ae442SChanwoo Choi 			ret = -ENODEV;
723830ae442SChanwoo Choi 		}
724830ae442SChanwoo Choi 	}
72566bee35fSHans de Goede 
72666bee35fSHans de Goede 	return ret;
727f68a8342SChanwoo Choi }
728f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier);
729f68a8342SChanwoo Choi 
730f68a8342SChanwoo Choi /**
731f68a8342SChanwoo Choi  * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
732046050f6SChanwoo Choi  * @edev:	the extcon device that has the external connecotr.
733046050f6SChanwoo Choi  * @id:		the unique id of each external connector in extcon enumeration.
734046050f6SChanwoo Choi  * @nb:		a notifier block to be registered.
735f68a8342SChanwoo Choi  */
73673b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
737f68a8342SChanwoo Choi 				struct notifier_block *nb)
738f68a8342SChanwoo Choi {
73966bee35fSHans de Goede 	unsigned long flags;
740046050f6SChanwoo Choi 	int ret, idx;
741046050f6SChanwoo Choi 
7427eae43aeSChanwoo Choi 	if (!edev || !nb)
7437eae43aeSChanwoo Choi 		return -EINVAL;
7447eae43aeSChanwoo Choi 
745046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
746a05f44c8SStephen Boyd 	if (idx < 0)
747a05f44c8SStephen Boyd 		return idx;
74866bee35fSHans de Goede 
74966bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
750046050f6SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
75166bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
75266bee35fSHans de Goede 
75366bee35fSHans de Goede 	return ret;
754f68a8342SChanwoo Choi }
755f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
756f68a8342SChanwoo Choi 
757f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = {
758f68a8342SChanwoo Choi 	&dev_attr_state.attr,
759f68a8342SChanwoo Choi 	&dev_attr_name.attr,
760f68a8342SChanwoo Choi 	NULL,
761f68a8342SChanwoo Choi };
762f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon);
763f68a8342SChanwoo Choi 
764f68a8342SChanwoo Choi static int create_extcon_class(void)
765f68a8342SChanwoo Choi {
766f68a8342SChanwoo Choi 	if (!extcon_class) {
767f68a8342SChanwoo Choi 		extcon_class = class_create(THIS_MODULE, "extcon");
768f68a8342SChanwoo Choi 		if (IS_ERR(extcon_class))
769f68a8342SChanwoo Choi 			return PTR_ERR(extcon_class);
770f68a8342SChanwoo Choi 		extcon_class->dev_groups = extcon_groups;
771f68a8342SChanwoo Choi 
772f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
773f68a8342SChanwoo Choi 		switch_class = class_compat_register("switch");
774f68a8342SChanwoo Choi 		if (WARN(!switch_class, "cannot allocate"))
775f68a8342SChanwoo Choi 			return -ENOMEM;
776f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */
777f68a8342SChanwoo Choi 	}
778f68a8342SChanwoo Choi 
779f68a8342SChanwoo Choi 	return 0;
780f68a8342SChanwoo Choi }
781f68a8342SChanwoo Choi 
782f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev)
783f68a8342SChanwoo Choi {
784f68a8342SChanwoo Choi }
785f68a8342SChanwoo Choi 
786f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive";
787f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev)
788f68a8342SChanwoo Choi {
789f68a8342SChanwoo Choi }
790f68a8342SChanwoo Choi 
791f68a8342SChanwoo Choi /*
792f68a8342SChanwoo Choi  * extcon_dev_allocate() - Allocate the memory of extcon device.
7932a9de9c0SChanwoo Choi  * @supported_cable:	Array of supported extcon ending with EXTCON_NONE.
794f68a8342SChanwoo Choi  *			If supported_cable is NULL, cable name related APIs
795f68a8342SChanwoo Choi  *			are disabled.
796f68a8342SChanwoo Choi  *
797f68a8342SChanwoo Choi  * This function allocates the memory for extcon device without allocating
798f68a8342SChanwoo Choi  * memory in each extcon provider driver and initialize default setting for
799f68a8342SChanwoo Choi  * extcon device.
800f68a8342SChanwoo Choi  *
801f68a8342SChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail
802f68a8342SChanwoo Choi  */
80373b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
804f68a8342SChanwoo Choi {
805f68a8342SChanwoo Choi 	struct extcon_dev *edev;
806f68a8342SChanwoo Choi 
8077eae43aeSChanwoo Choi 	if (!supported_cable)
8087eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
8097eae43aeSChanwoo Choi 
810f68a8342SChanwoo Choi 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
811f68a8342SChanwoo Choi 	if (!edev)
812f68a8342SChanwoo Choi 		return ERR_PTR(-ENOMEM);
813f68a8342SChanwoo Choi 
814f68a8342SChanwoo Choi 	edev->max_supported = 0;
815f68a8342SChanwoo Choi 	edev->supported_cable = supported_cable;
816f68a8342SChanwoo Choi 
817f68a8342SChanwoo Choi 	return edev;
818f68a8342SChanwoo Choi }
819f68a8342SChanwoo Choi 
820f68a8342SChanwoo Choi /*
821f68a8342SChanwoo Choi  * extcon_dev_free() - Free the memory of extcon device.
822f68a8342SChanwoo Choi  * @edev:	the extcon device to free
823f68a8342SChanwoo Choi  */
824f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev)
825f68a8342SChanwoo Choi {
826f68a8342SChanwoo Choi 	kfree(edev);
827f68a8342SChanwoo Choi }
828f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free);
829f68a8342SChanwoo Choi 
830f68a8342SChanwoo Choi /**
831f68a8342SChanwoo Choi  * extcon_dev_register() - Register a new extcon device
832f68a8342SChanwoo Choi  * @edev	: the new extcon device (should be allocated before calling)
833f68a8342SChanwoo Choi  *
834f68a8342SChanwoo Choi  * Among the members of edev struct, please set the "user initializing data"
835f68a8342SChanwoo Choi  * in any case and set the "optional callbacks" if required. However, please
836f68a8342SChanwoo Choi  * do not set the values of "internal data", which are initialized by
837f68a8342SChanwoo Choi  * this function.
838f68a8342SChanwoo Choi  */
839f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev)
840f68a8342SChanwoo Choi {
841f68a8342SChanwoo Choi 	int ret, index = 0;
84271c3ffa5SChanwoo Choi 	static atomic_t edev_no = ATOMIC_INIT(-1);
843f68a8342SChanwoo Choi 
844f68a8342SChanwoo Choi 	if (!extcon_class) {
845f68a8342SChanwoo Choi 		ret = create_extcon_class();
846f68a8342SChanwoo Choi 		if (ret < 0)
847f68a8342SChanwoo Choi 			return ret;
848f68a8342SChanwoo Choi 	}
849f68a8342SChanwoo Choi 
8507eae43aeSChanwoo Choi 	if (!edev || !edev->supported_cable)
8512a9de9c0SChanwoo Choi 		return -EINVAL;
852f68a8342SChanwoo Choi 
8532a9de9c0SChanwoo Choi 	for (; edev->supported_cable[index] != EXTCON_NONE; index++);
8542a9de9c0SChanwoo Choi 
8552a9de9c0SChanwoo Choi 	edev->max_supported = index;
856f68a8342SChanwoo Choi 	if (index > SUPPORTED_CABLE_MAX) {
8572a9de9c0SChanwoo Choi 		dev_err(&edev->dev,
8582a9de9c0SChanwoo Choi 			"exceed the maximum number of supported cables\n");
859f68a8342SChanwoo Choi 		return -EINVAL;
860f68a8342SChanwoo Choi 	}
861f68a8342SChanwoo Choi 
862f68a8342SChanwoo Choi 	edev->dev.class = extcon_class;
863f68a8342SChanwoo Choi 	edev->dev.release = extcon_dev_release;
864f68a8342SChanwoo Choi 
86571c3ffa5SChanwoo Choi 	edev->name = dev_name(edev->dev.parent);
866f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(edev->name)) {
867f68a8342SChanwoo Choi 		dev_err(&edev->dev,
868f68a8342SChanwoo Choi 			"extcon device name is null\n");
869f68a8342SChanwoo Choi 		return -EINVAL;
870f68a8342SChanwoo Choi 	}
87171c3ffa5SChanwoo Choi 	dev_set_name(&edev->dev, "extcon%lu",
87271c3ffa5SChanwoo Choi 			(unsigned long)atomic_inc_return(&edev_no));
873f68a8342SChanwoo Choi 
874f68a8342SChanwoo Choi 	if (edev->max_supported) {
875f68a8342SChanwoo Choi 		char buf[10];
876f68a8342SChanwoo Choi 		char *str;
877f68a8342SChanwoo Choi 		struct extcon_cable *cable;
878f68a8342SChanwoo Choi 
879f68a8342SChanwoo Choi 		edev->cables = kzalloc(sizeof(struct extcon_cable) *
880f68a8342SChanwoo Choi 				       edev->max_supported, GFP_KERNEL);
881f68a8342SChanwoo Choi 		if (!edev->cables) {
882f68a8342SChanwoo Choi 			ret = -ENOMEM;
883f68a8342SChanwoo Choi 			goto err_sysfs_alloc;
884f68a8342SChanwoo Choi 		}
885f68a8342SChanwoo Choi 		for (index = 0; index < edev->max_supported; index++) {
886f68a8342SChanwoo Choi 			cable = &edev->cables[index];
887f68a8342SChanwoo Choi 
888f68a8342SChanwoo Choi 			snprintf(buf, 10, "cable.%d", index);
889f68a8342SChanwoo Choi 			str = kzalloc(sizeof(char) * (strlen(buf) + 1),
890f68a8342SChanwoo Choi 				      GFP_KERNEL);
891f68a8342SChanwoo Choi 			if (!str) {
892f68a8342SChanwoo Choi 				for (index--; index >= 0; index--) {
893f68a8342SChanwoo Choi 					cable = &edev->cables[index];
894f68a8342SChanwoo Choi 					kfree(cable->attr_g.name);
895f68a8342SChanwoo Choi 				}
896f68a8342SChanwoo Choi 				ret = -ENOMEM;
897f68a8342SChanwoo Choi 
898f68a8342SChanwoo Choi 				goto err_alloc_cables;
899f68a8342SChanwoo Choi 			}
900f68a8342SChanwoo Choi 			strcpy(str, buf);
901f68a8342SChanwoo Choi 
902f68a8342SChanwoo Choi 			cable->edev = edev;
903f68a8342SChanwoo Choi 			cable->cable_index = index;
904f68a8342SChanwoo Choi 			cable->attrs[0] = &cable->attr_name.attr;
905f68a8342SChanwoo Choi 			cable->attrs[1] = &cable->attr_state.attr;
906f68a8342SChanwoo Choi 			cable->attrs[2] = NULL;
907f68a8342SChanwoo Choi 			cable->attr_g.name = str;
908f68a8342SChanwoo Choi 			cable->attr_g.attrs = cable->attrs;
909f68a8342SChanwoo Choi 
910f68a8342SChanwoo Choi 			sysfs_attr_init(&cable->attr_name.attr);
911f68a8342SChanwoo Choi 			cable->attr_name.attr.name = "name";
912f68a8342SChanwoo Choi 			cable->attr_name.attr.mode = 0444;
913f68a8342SChanwoo Choi 			cable->attr_name.show = cable_name_show;
914f68a8342SChanwoo Choi 
915f68a8342SChanwoo Choi 			sysfs_attr_init(&cable->attr_state.attr);
916f68a8342SChanwoo Choi 			cable->attr_state.attr.name = "state";
917f68a8342SChanwoo Choi 			cable->attr_state.attr.mode = 0444;
918f68a8342SChanwoo Choi 			cable->attr_state.show = cable_state_show;
919f68a8342SChanwoo Choi 		}
920f68a8342SChanwoo Choi 	}
921f68a8342SChanwoo Choi 
922f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
923f68a8342SChanwoo Choi 		char buf[80];
924f68a8342SChanwoo Choi 		char *name;
925f68a8342SChanwoo Choi 
926f68a8342SChanwoo Choi 		/* Count the size of mutually_exclusive array */
927f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
928f68a8342SChanwoo Choi 			;
929f68a8342SChanwoo Choi 
930f68a8342SChanwoo Choi 		edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
931f68a8342SChanwoo Choi 					   (index + 1), GFP_KERNEL);
932f68a8342SChanwoo Choi 		if (!edev->attrs_muex) {
933f68a8342SChanwoo Choi 			ret = -ENOMEM;
934f68a8342SChanwoo Choi 			goto err_muex;
935f68a8342SChanwoo Choi 		}
936f68a8342SChanwoo Choi 
937f68a8342SChanwoo Choi 		edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
938f68a8342SChanwoo Choi 					     index, GFP_KERNEL);
939f68a8342SChanwoo Choi 		if (!edev->d_attrs_muex) {
940f68a8342SChanwoo Choi 			ret = -ENOMEM;
941f68a8342SChanwoo Choi 			kfree(edev->attrs_muex);
942f68a8342SChanwoo Choi 			goto err_muex;
943f68a8342SChanwoo Choi 		}
944f68a8342SChanwoo Choi 
945f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++) {
946f68a8342SChanwoo Choi 			sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
947f68a8342SChanwoo Choi 			name = kzalloc(sizeof(char) * (strlen(buf) + 1),
948f68a8342SChanwoo Choi 				       GFP_KERNEL);
949f68a8342SChanwoo Choi 			if (!name) {
950f68a8342SChanwoo Choi 				for (index--; index >= 0; index--) {
951f68a8342SChanwoo Choi 					kfree(edev->d_attrs_muex[index].attr.
952f68a8342SChanwoo Choi 					      name);
953f68a8342SChanwoo Choi 				}
954f68a8342SChanwoo Choi 				kfree(edev->d_attrs_muex);
955f68a8342SChanwoo Choi 				kfree(edev->attrs_muex);
956f68a8342SChanwoo Choi 				ret = -ENOMEM;
957f68a8342SChanwoo Choi 				goto err_muex;
958f68a8342SChanwoo Choi 			}
959f68a8342SChanwoo Choi 			strcpy(name, buf);
960f68a8342SChanwoo Choi 			sysfs_attr_init(&edev->d_attrs_muex[index].attr);
961f68a8342SChanwoo Choi 			edev->d_attrs_muex[index].attr.name = name;
962f68a8342SChanwoo Choi 			edev->d_attrs_muex[index].attr.mode = 0000;
963f68a8342SChanwoo Choi 			edev->attrs_muex[index] = &edev->d_attrs_muex[index]
964f68a8342SChanwoo Choi 							.attr;
965f68a8342SChanwoo Choi 		}
966f68a8342SChanwoo Choi 		edev->attr_g_muex.name = muex_name;
967f68a8342SChanwoo Choi 		edev->attr_g_muex.attrs = edev->attrs_muex;
968f68a8342SChanwoo Choi 
969f68a8342SChanwoo Choi 	}
970f68a8342SChanwoo Choi 
971f68a8342SChanwoo Choi 	if (edev->max_supported) {
972f68a8342SChanwoo Choi 		edev->extcon_dev_type.groups =
973f68a8342SChanwoo Choi 			kzalloc(sizeof(struct attribute_group *) *
974f68a8342SChanwoo Choi 				(edev->max_supported + 2), GFP_KERNEL);
975f68a8342SChanwoo Choi 		if (!edev->extcon_dev_type.groups) {
976f68a8342SChanwoo Choi 			ret = -ENOMEM;
977f68a8342SChanwoo Choi 			goto err_alloc_groups;
978f68a8342SChanwoo Choi 		}
979f68a8342SChanwoo Choi 
980f68a8342SChanwoo Choi 		edev->extcon_dev_type.name = dev_name(&edev->dev);
981f68a8342SChanwoo Choi 		edev->extcon_dev_type.release = dummy_sysfs_dev_release;
982f68a8342SChanwoo Choi 
983f68a8342SChanwoo Choi 		for (index = 0; index < edev->max_supported; index++)
984f68a8342SChanwoo Choi 			edev->extcon_dev_type.groups[index] =
985f68a8342SChanwoo Choi 				&edev->cables[index].attr_g;
986f68a8342SChanwoo Choi 		if (edev->mutually_exclusive)
987f68a8342SChanwoo Choi 			edev->extcon_dev_type.groups[index] =
988f68a8342SChanwoo Choi 				&edev->attr_g_muex;
989f68a8342SChanwoo Choi 
990f68a8342SChanwoo Choi 		edev->dev.type = &edev->extcon_dev_type;
991f68a8342SChanwoo Choi 	}
992f68a8342SChanwoo Choi 
993f68a8342SChanwoo Choi 	ret = device_register(&edev->dev);
994f68a8342SChanwoo Choi 	if (ret) {
995f68a8342SChanwoo Choi 		put_device(&edev->dev);
996f68a8342SChanwoo Choi 		goto err_dev;
997f68a8342SChanwoo Choi 	}
998f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
999f68a8342SChanwoo Choi 	if (switch_class)
1000f68a8342SChanwoo Choi 		ret = class_compat_create_link(switch_class, &edev->dev, NULL);
1001f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */
1002f68a8342SChanwoo Choi 
1003f68a8342SChanwoo Choi 	spin_lock_init(&edev->lock);
1004f68a8342SChanwoo Choi 
1005046050f6SChanwoo Choi 	edev->nh = devm_kzalloc(&edev->dev,
1006046050f6SChanwoo Choi 			sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1007046050f6SChanwoo Choi 	if (!edev->nh) {
1008046050f6SChanwoo Choi 		ret = -ENOMEM;
1009046050f6SChanwoo Choi 		goto err_dev;
1010046050f6SChanwoo Choi 	}
1011046050f6SChanwoo Choi 
1012046050f6SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1013046050f6SChanwoo Choi 		RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1014f68a8342SChanwoo Choi 
1015f68a8342SChanwoo Choi 	dev_set_drvdata(&edev->dev, edev);
1016f68a8342SChanwoo Choi 	edev->state = 0;
1017f68a8342SChanwoo Choi 
1018f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1019f68a8342SChanwoo Choi 	list_add(&edev->entry, &extcon_dev_list);
1020f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1021f68a8342SChanwoo Choi 
1022f68a8342SChanwoo Choi 	return 0;
1023f68a8342SChanwoo Choi 
1024f68a8342SChanwoo Choi err_dev:
1025f68a8342SChanwoo Choi 	if (edev->max_supported)
1026f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1027f68a8342SChanwoo Choi err_alloc_groups:
1028f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
1029f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
1030f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1031f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1032f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1033f68a8342SChanwoo Choi 	}
1034f68a8342SChanwoo Choi err_muex:
1035f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1036f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1037f68a8342SChanwoo Choi err_alloc_cables:
1038f68a8342SChanwoo Choi 	if (edev->max_supported)
1039f68a8342SChanwoo Choi 		kfree(edev->cables);
1040f68a8342SChanwoo Choi err_sysfs_alloc:
1041f68a8342SChanwoo Choi 	return ret;
1042f68a8342SChanwoo Choi }
1043f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register);
1044f68a8342SChanwoo Choi 
1045f68a8342SChanwoo Choi /**
1046f68a8342SChanwoo Choi  * extcon_dev_unregister() - Unregister the extcon device.
1047f68a8342SChanwoo Choi  * @edev:	the extcon device instance to be unregistered.
1048f68a8342SChanwoo Choi  *
1049f68a8342SChanwoo Choi  * Note that this does not call kfree(edev) because edev was not allocated
1050f68a8342SChanwoo Choi  * by this class.
1051f68a8342SChanwoo Choi  */
1052f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev)
1053f68a8342SChanwoo Choi {
1054f68a8342SChanwoo Choi 	int index;
1055f68a8342SChanwoo Choi 
10567eae43aeSChanwoo Choi 	if (!edev)
10577eae43aeSChanwoo Choi 		return;
10587eae43aeSChanwoo Choi 
1059f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1060f68a8342SChanwoo Choi 	list_del(&edev->entry);
1061f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1062f68a8342SChanwoo Choi 
1063f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1064f68a8342SChanwoo Choi 		dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1065f68a8342SChanwoo Choi 				dev_name(&edev->dev));
1066f68a8342SChanwoo Choi 		return;
1067f68a8342SChanwoo Choi 	}
1068f68a8342SChanwoo Choi 
1069f68a8342SChanwoo Choi 	device_unregister(&edev->dev);
1070f68a8342SChanwoo Choi 
1071f68a8342SChanwoo Choi 	if (edev->mutually_exclusive && edev->max_supported) {
1072f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index];
1073f68a8342SChanwoo Choi 				index++)
1074f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1075f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1076f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1077f68a8342SChanwoo Choi 	}
1078f68a8342SChanwoo Choi 
1079f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1080f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1081f68a8342SChanwoo Choi 
1082f68a8342SChanwoo Choi 	if (edev->max_supported) {
1083f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1084f68a8342SChanwoo Choi 		kfree(edev->cables);
1085f68a8342SChanwoo Choi 	}
1086f68a8342SChanwoo Choi 
1087f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
1088f68a8342SChanwoo Choi 	if (switch_class)
1089f68a8342SChanwoo Choi 		class_compat_remove_link(switch_class, &edev->dev, NULL);
1090f68a8342SChanwoo Choi #endif
1091f68a8342SChanwoo Choi 	put_device(&edev->dev);
1092f68a8342SChanwoo Choi }
1093f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1094f68a8342SChanwoo Choi 
1095f68a8342SChanwoo Choi #ifdef CONFIG_OF
1096f68a8342SChanwoo Choi /*
1097f68a8342SChanwoo Choi  * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1098f68a8342SChanwoo Choi  * @dev - instance to the given device
1099f68a8342SChanwoo Choi  * @index - index into list of extcon_dev
1100f68a8342SChanwoo Choi  *
1101f68a8342SChanwoo Choi  * return the instance of extcon device
1102f68a8342SChanwoo Choi  */
1103f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1104f68a8342SChanwoo Choi {
1105f68a8342SChanwoo Choi 	struct device_node *node;
1106f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1107f68a8342SChanwoo Choi 
11087eae43aeSChanwoo Choi 	if (!dev)
11097eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
11107eae43aeSChanwoo Choi 
1111f68a8342SChanwoo Choi 	if (!dev->of_node) {
1112e8752b7aSStephen Boyd 		dev_dbg(dev, "device does not have a device node entry\n");
1113f68a8342SChanwoo Choi 		return ERR_PTR(-EINVAL);
1114f68a8342SChanwoo Choi 	}
1115f68a8342SChanwoo Choi 
1116f68a8342SChanwoo Choi 	node = of_parse_phandle(dev->of_node, "extcon", index);
1117f68a8342SChanwoo Choi 	if (!node) {
1118e8752b7aSStephen Boyd 		dev_dbg(dev, "failed to get phandle in %s node\n",
1119f68a8342SChanwoo Choi 			dev->of_node->full_name);
1120f68a8342SChanwoo Choi 		return ERR_PTR(-ENODEV);
1121f68a8342SChanwoo Choi 	}
1122f68a8342SChanwoo Choi 
1123f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1124f68a8342SChanwoo Choi 	list_for_each_entry(edev, &extcon_dev_list, entry) {
1125f68a8342SChanwoo Choi 		if (edev->dev.parent && edev->dev.parent->of_node == node) {
1126f68a8342SChanwoo Choi 			mutex_unlock(&extcon_dev_list_lock);
11275d5c4c13SPeter Chen 			of_node_put(node);
1128f68a8342SChanwoo Choi 			return edev;
1129f68a8342SChanwoo Choi 		}
1130f68a8342SChanwoo Choi 	}
1131f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
11325d5c4c13SPeter Chen 	of_node_put(node);
1133f68a8342SChanwoo Choi 
1134f68a8342SChanwoo Choi 	return ERR_PTR(-EPROBE_DEFER);
1135f68a8342SChanwoo Choi }
1136f68a8342SChanwoo Choi #else
1137f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1138f68a8342SChanwoo Choi {
1139f68a8342SChanwoo Choi 	return ERR_PTR(-ENOSYS);
1140f68a8342SChanwoo Choi }
1141f68a8342SChanwoo Choi #endif /* CONFIG_OF */
1142f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1143f68a8342SChanwoo Choi 
1144707d7550SChanwoo Choi /**
1145707d7550SChanwoo Choi  * extcon_get_edev_name() - Get the name of the extcon device.
1146707d7550SChanwoo Choi  * @edev:	the extcon device
1147707d7550SChanwoo Choi  */
1148707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev)
1149707d7550SChanwoo Choi {
1150707d7550SChanwoo Choi 	return !edev ? NULL : edev->name;
1151707d7550SChanwoo Choi }
1152707d7550SChanwoo Choi 
1153f68a8342SChanwoo Choi static int __init extcon_class_init(void)
1154f68a8342SChanwoo Choi {
1155f68a8342SChanwoo Choi 	return create_extcon_class();
1156f68a8342SChanwoo Choi }
1157f68a8342SChanwoo Choi module_init(extcon_class_init);
1158f68a8342SChanwoo Choi 
1159f68a8342SChanwoo Choi static void __exit extcon_class_exit(void)
1160f68a8342SChanwoo Choi {
1161f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID)
1162f68a8342SChanwoo Choi 	class_compat_unregister(switch_class);
1163f68a8342SChanwoo Choi #endif
1164f68a8342SChanwoo Choi 	class_destroy(extcon_class);
1165f68a8342SChanwoo Choi }
1166f68a8342SChanwoo Choi module_exit(extcon_class_exit);
1167f68a8342SChanwoo Choi 
11682a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <[email protected]>");
1169f68a8342SChanwoo Choi MODULE_AUTHOR("Mike Lockwood <[email protected]>");
1170f68a8342SChanwoo Choi MODULE_AUTHOR("Donggeun Kim <[email protected]>");
1171f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <[email protected]>");
1172f68a8342SChanwoo Choi MODULE_DESCRIPTION("External connector (extcon) class driver");
1173f68a8342SChanwoo Choi MODULE_LICENSE("GPL");
1174