xref: /linux-6.15/drivers/extcon/extcon.c (revision 5dcc2afe)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f68a8342SChanwoo Choi /*
3b9ec23c0SChanwoo Choi  * drivers/extcon/extcon.c - External Connector (extcon) framework.
4f68a8342SChanwoo Choi  *
52a9de9c0SChanwoo Choi  * Copyright (C) 2015 Samsung Electronics
62a9de9c0SChanwoo Choi  * Author: Chanwoo Choi <[email protected]>
72a9de9c0SChanwoo Choi  *
8f68a8342SChanwoo Choi  * Copyright (C) 2012 Samsung Electronics
9f68a8342SChanwoo Choi  * Author: Donggeun Kim <[email protected]>
10f68a8342SChanwoo Choi  * Author: MyungJoo Ham <[email protected]>
11f68a8342SChanwoo Choi  *
12f68a8342SChanwoo Choi  * based on android/drivers/switch/switch_class.c
13f68a8342SChanwoo Choi  * Copyright (C) 2008 Google, Inc.
14f68a8342SChanwoo Choi  * Author: Mike Lockwood <[email protected]>
15f68a8342SChanwoo Choi  */
16f68a8342SChanwoo Choi 
17f68a8342SChanwoo Choi #include <linux/module.h>
18f68a8342SChanwoo Choi #include <linux/types.h>
19f68a8342SChanwoo Choi #include <linux/init.h>
20f68a8342SChanwoo Choi #include <linux/device.h>
21f68a8342SChanwoo Choi #include <linux/fs.h>
22f68a8342SChanwoo Choi #include <linux/err.h>
23f68a8342SChanwoo Choi #include <linux/of.h>
24f68a8342SChanwoo Choi #include <linux/slab.h>
25f68a8342SChanwoo Choi #include <linux/sysfs.h>
26f68a8342SChanwoo Choi 
27e6cf0465SChanwoo Choi #include "extcon.h"
28e6cf0465SChanwoo Choi 
292a9de9c0SChanwoo Choi #define SUPPORTED_CABLE_MAX	32
302a9de9c0SChanwoo Choi 
315183240cSColin Ian King static const struct __extcon_info {
3255e4e2f1SChanwoo Choi 	unsigned int type;
3355e4e2f1SChanwoo Choi 	unsigned int id;
3455e4e2f1SChanwoo Choi 	const char *name;
3555e4e2f1SChanwoo Choi 
3655e4e2f1SChanwoo Choi } extcon_info[] = {
3755e4e2f1SChanwoo Choi 	[EXTCON_NONE] = {
3855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
3955e4e2f1SChanwoo Choi 		.id = EXTCON_NONE,
4055e4e2f1SChanwoo Choi 		.name = "NONE",
4155e4e2f1SChanwoo Choi 	},
4273b6ecdbSChanwoo Choi 
438e9bc36dSChanwoo Choi 	/* USB external connector */
4455e4e2f1SChanwoo Choi 	[EXTCON_USB] = {
4555e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_USB,
4655e4e2f1SChanwoo Choi 		.id = EXTCON_USB,
4755e4e2f1SChanwoo Choi 		.name = "USB",
4855e4e2f1SChanwoo Choi 	},
4955e4e2f1SChanwoo Choi 	[EXTCON_USB_HOST] = {
5055e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_USB,
5155e4e2f1SChanwoo Choi 		.id = EXTCON_USB_HOST,
5286d6cda6SChanwoo Choi 		.name = "USB-HOST",
5355e4e2f1SChanwoo Choi 	},
548e9bc36dSChanwoo Choi 
5511eecf91SChanwoo Choi 	/* Charging external connector */
5655e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_SDP] = {
5755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
5855e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_SDP,
5955e4e2f1SChanwoo Choi 		.name = "SDP",
6055e4e2f1SChanwoo Choi 	},
6155e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_DCP] = {
6255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
6355e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_DCP,
6455e4e2f1SChanwoo Choi 		.name = "DCP",
6555e4e2f1SChanwoo Choi 	},
6655e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_CDP] = {
6755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
6855e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_CDP,
6955e4e2f1SChanwoo Choi 		.name = "CDP",
7055e4e2f1SChanwoo Choi 	},
7155e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_ACA] = {
7255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
7355e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_ACA,
7455e4e2f1SChanwoo Choi 		.name = "ACA",
7555e4e2f1SChanwoo Choi 	},
7655e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_FAST] = {
7755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
7855e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_FAST,
7955e4e2f1SChanwoo Choi 		.name = "FAST-CHARGER",
8055e4e2f1SChanwoo Choi 	},
8155e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_SLOW] = {
8255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
8355e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_SLOW,
8455e4e2f1SChanwoo Choi 		.name = "SLOW-CHARGER",
8555e4e2f1SChanwoo Choi 	},
867fe95fb8SChanwoo Choi 	[EXTCON_CHG_WPT] = {
877fe95fb8SChanwoo Choi 		.type = EXTCON_TYPE_CHG,
887fe95fb8SChanwoo Choi 		.id = EXTCON_CHG_WPT,
897fe95fb8SChanwoo Choi 		.name = "WPT",
907fe95fb8SChanwoo Choi 	},
913c5f0e07SChanwoo Choi 	[EXTCON_CHG_USB_PD] = {
923c5f0e07SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
933c5f0e07SChanwoo Choi 		.id = EXTCON_CHG_USB_PD,
943c5f0e07SChanwoo Choi 		.name = "PD",
953c5f0e07SChanwoo 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 	},
1602164188dSChris Zhong 	[EXTCON_DISP_DP] = {
1612164188dSChris Zhong 		.type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
1622164188dSChris Zhong 		.id = EXTCON_DISP_DP,
1632164188dSChris Zhong 		.name = "DP",
1642164188dSChris Zhong 	},
1659c0595d6SChanwoo Choi 	[EXTCON_DISP_HMD] = {
1669c0595d6SChanwoo Choi 		.type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
1679c0595d6SChanwoo Choi 		.id = EXTCON_DISP_HMD,
1689c0595d6SChanwoo Choi 		.name = "HMD",
1699c0595d6SChanwoo Choi 	},
1708e9bc36dSChanwoo Choi 
17111eecf91SChanwoo Choi 	/* Miscellaneous external connector */
17255e4e2f1SChanwoo Choi 	[EXTCON_DOCK] = {
17355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
17455e4e2f1SChanwoo Choi 		.id = EXTCON_DOCK,
17555e4e2f1SChanwoo Choi 		.name = "DOCK",
17655e4e2f1SChanwoo Choi 	},
17755e4e2f1SChanwoo Choi 	[EXTCON_JIG] = {
17855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
17955e4e2f1SChanwoo Choi 		.id = EXTCON_JIG,
18055e4e2f1SChanwoo Choi 		.name = "JIG",
18155e4e2f1SChanwoo Choi 	},
18255e4e2f1SChanwoo Choi 	[EXTCON_MECHANICAL] = {
18355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
18455e4e2f1SChanwoo Choi 		.id = EXTCON_MECHANICAL,
18555e4e2f1SChanwoo Choi 		.name = "MECHANICAL",
18655e4e2f1SChanwoo Choi 	},
1878e9bc36dSChanwoo Choi 
18855e4e2f1SChanwoo Choi 	{ /* sentinel */ }
189f68a8342SChanwoo Choi };
190f68a8342SChanwoo Choi 
19120f7b53dSChanwoo Choi /**
1926ab6094fSChanwoo Choi  * struct extcon_cable - An internal data for an external connector.
1936ab6094fSChanwoo Choi  * @edev:		the extcon device
1946ab6094fSChanwoo Choi  * @cable_index:	the index of this cable in the edev
1956ab6094fSChanwoo Choi  * @attr_g:		the attribute group for the cable
19620f7b53dSChanwoo Choi  * @attr_name:		"name" sysfs entry
19720f7b53dSChanwoo Choi  * @attr_state:		"state" sysfs entry
1986ab6094fSChanwoo Choi  * @attrs:		the array pointing to attr_name and attr_state for attr_g
19920f7b53dSChanwoo Choi  */
20020f7b53dSChanwoo Choi struct extcon_cable {
20120f7b53dSChanwoo Choi 	struct extcon_dev *edev;
20220f7b53dSChanwoo Choi 	int cable_index;
20320f7b53dSChanwoo Choi 
20420f7b53dSChanwoo Choi 	struct attribute_group attr_g;
20520f7b53dSChanwoo Choi 	struct device_attribute attr_name;
20620f7b53dSChanwoo Choi 	struct device_attribute attr_state;
20720f7b53dSChanwoo Choi 
20820f7b53dSChanwoo Choi 	struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
209792e7e9eSChanwoo Choi 
210792e7e9eSChanwoo Choi 	union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
211792e7e9eSChanwoo Choi 	union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
212792e7e9eSChanwoo Choi 	union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
213792e7e9eSChanwoo Choi 	union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
2147f2a0a16SChanwoo Choi 
2157f2a0a16SChanwoo Choi 	unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
2167f2a0a16SChanwoo Choi 	unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
2177f2a0a16SChanwoo Choi 	unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
2187f2a0a16SChanwoo Choi 	unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
21920f7b53dSChanwoo Choi };
22020f7b53dSChanwoo Choi 
221f68a8342SChanwoo Choi static struct class *extcon_class;
222f68a8342SChanwoo Choi 
223f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list);
224f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock);
225f68a8342SChanwoo Choi 
226f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
227f68a8342SChanwoo Choi {
228f68a8342SChanwoo Choi 	int i = 0;
229f68a8342SChanwoo Choi 
230f68a8342SChanwoo Choi 	if (!edev->mutually_exclusive)
231f68a8342SChanwoo Choi 		return 0;
232f68a8342SChanwoo Choi 
233f68a8342SChanwoo Choi 	for (i = 0; edev->mutually_exclusive[i]; i++) {
234f68a8342SChanwoo Choi 		int weight;
235f68a8342SChanwoo Choi 		u32 correspondants = new_state & edev->mutually_exclusive[i];
236f68a8342SChanwoo Choi 
237f68a8342SChanwoo Choi 		/* calculate the total number of bits set */
238f68a8342SChanwoo Choi 		weight = hweight32(correspondants);
239f68a8342SChanwoo Choi 		if (weight > 1)
240f68a8342SChanwoo Choi 			return i + 1;
241f68a8342SChanwoo Choi 	}
242f68a8342SChanwoo Choi 
243f68a8342SChanwoo Choi 	return 0;
244f68a8342SChanwoo Choi }
245f68a8342SChanwoo Choi 
24673b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
2472a9de9c0SChanwoo Choi {
2482a9de9c0SChanwoo Choi 	int i;
2492a9de9c0SChanwoo Choi 
2502a9de9c0SChanwoo Choi 	/* Find the the index of extcon cable in edev->supported_cable */
2512a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
2522a9de9c0SChanwoo Choi 		if (edev->supported_cable[i] == id)
2532a9de9c0SChanwoo Choi 			return i;
2542a9de9c0SChanwoo Choi 	}
2552a9de9c0SChanwoo Choi 
2562a9de9c0SChanwoo Choi 	return -EINVAL;
2572a9de9c0SChanwoo Choi }
2582a9de9c0SChanwoo Choi 
259792e7e9eSChanwoo Choi static int get_extcon_type(unsigned int prop)
260792e7e9eSChanwoo Choi {
261792e7e9eSChanwoo Choi 	switch (prop) {
262792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
263792e7e9eSChanwoo Choi 		return EXTCON_TYPE_USB;
264792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
265792e7e9eSChanwoo Choi 		return EXTCON_TYPE_CHG;
266792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
267792e7e9eSChanwoo Choi 		return EXTCON_TYPE_JACK;
268792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
269792e7e9eSChanwoo Choi 		return EXTCON_TYPE_DISP;
270792e7e9eSChanwoo Choi 	default:
271792e7e9eSChanwoo Choi 		return -EINVAL;
272792e7e9eSChanwoo Choi 	}
273792e7e9eSChanwoo Choi }
274792e7e9eSChanwoo Choi 
275792e7e9eSChanwoo Choi static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
276792e7e9eSChanwoo Choi {
277792e7e9eSChanwoo Choi 	return !!(edev->state & BIT(index));
278792e7e9eSChanwoo Choi }
279792e7e9eSChanwoo Choi 
280ab11af04SChanwoo Choi static bool is_extcon_changed(struct extcon_dev *edev, int index,
281ab11af04SChanwoo Choi 				bool new_state)
282046050f6SChanwoo Choi {
283ab11af04SChanwoo Choi 	int state = !!(edev->state & BIT(index));
284ab11af04SChanwoo Choi 	return (state != new_state);
285046050f6SChanwoo Choi }
286046050f6SChanwoo Choi 
287792e7e9eSChanwoo Choi static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
288792e7e9eSChanwoo Choi {
289792e7e9eSChanwoo Choi 	int type;
290792e7e9eSChanwoo Choi 
291792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not. */
292792e7e9eSChanwoo Choi 	type = get_extcon_type(prop);
293792e7e9eSChanwoo Choi 	if (type < 0)
294792e7e9eSChanwoo Choi 		return false;
295792e7e9eSChanwoo Choi 
296792e7e9eSChanwoo Choi 	/* Check whether a specific extcon id supports the property or not. */
297792e7e9eSChanwoo Choi 	return !!(extcon_info[id].type & type);
298792e7e9eSChanwoo Choi }
299792e7e9eSChanwoo Choi 
3007f2a0a16SChanwoo Choi static int is_extcon_property_capability(struct extcon_dev *edev,
3017f2a0a16SChanwoo Choi 				unsigned int id, int index,unsigned int prop)
3027f2a0a16SChanwoo Choi {
3037f2a0a16SChanwoo Choi 	struct extcon_cable *cable;
3047f2a0a16SChanwoo Choi 	int type, ret;
3057f2a0a16SChanwoo Choi 
3067f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not. */
3077f2a0a16SChanwoo Choi 	type = get_extcon_type(prop);
3087f2a0a16SChanwoo Choi 	if (type < 0)
3097f2a0a16SChanwoo Choi 		return type;
3107f2a0a16SChanwoo Choi 
3117f2a0a16SChanwoo Choi 	cable = &edev->cables[index];
3127f2a0a16SChanwoo Choi 
3137f2a0a16SChanwoo Choi 	switch (type) {
3147f2a0a16SChanwoo Choi 	case EXTCON_TYPE_USB:
3157f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
3167f2a0a16SChanwoo Choi 		break;
3177f2a0a16SChanwoo Choi 	case EXTCON_TYPE_CHG:
3187f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
3197f2a0a16SChanwoo Choi 		break;
3207f2a0a16SChanwoo Choi 	case EXTCON_TYPE_JACK:
3217f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
3227f2a0a16SChanwoo Choi 		break;
3237f2a0a16SChanwoo Choi 	case EXTCON_TYPE_DISP:
3247f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
3257f2a0a16SChanwoo Choi 		break;
3267f2a0a16SChanwoo Choi 	default:
3277f2a0a16SChanwoo Choi 		ret = -EINVAL;
3287f2a0a16SChanwoo Choi 	}
3297f2a0a16SChanwoo Choi 
3307f2a0a16SChanwoo Choi 	return ret;
3317f2a0a16SChanwoo Choi }
3327f2a0a16SChanwoo Choi 
333792e7e9eSChanwoo Choi static void init_property(struct extcon_dev *edev, unsigned int id, int index)
334792e7e9eSChanwoo Choi {
335792e7e9eSChanwoo Choi 	unsigned int type = extcon_info[id].type;
336792e7e9eSChanwoo Choi 	struct extcon_cable *cable = &edev->cables[index];
337792e7e9eSChanwoo Choi 
338792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_USB & type)
339792e7e9eSChanwoo Choi 		memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
340792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_CHG & type)
341792e7e9eSChanwoo Choi 		memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
342792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_JACK & type)
343792e7e9eSChanwoo Choi 		memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
344792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_DISP & type)
345792e7e9eSChanwoo Choi 		memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
346792e7e9eSChanwoo Choi }
347792e7e9eSChanwoo Choi 
348f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr,
349f68a8342SChanwoo Choi 			  char *buf)
350f68a8342SChanwoo Choi {
351f68a8342SChanwoo Choi 	int i, count = 0;
352f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
353f68a8342SChanwoo Choi 
354f68a8342SChanwoo Choi 	if (edev->max_supported == 0)
355f68a8342SChanwoo Choi 		return sprintf(buf, "%u\n", edev->state);
356f68a8342SChanwoo Choi 
3572a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
358f68a8342SChanwoo Choi 		count += sprintf(buf + count, "%s=%d\n",
35955e4e2f1SChanwoo Choi 				extcon_info[edev->supported_cable[i]].name,
36070641a0aSChanwoo Choi 				 !!(edev->state & BIT(i)));
361f68a8342SChanwoo Choi 	}
362f68a8342SChanwoo Choi 
363f68a8342SChanwoo Choi 	return count;
364f68a8342SChanwoo Choi }
3655d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state);
366f68a8342SChanwoo Choi 
367f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr,
368f68a8342SChanwoo Choi 		char *buf)
369f68a8342SChanwoo Choi {
370f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
371f68a8342SChanwoo Choi 
37271c3ffa5SChanwoo Choi 	return sprintf(buf, "%s\n", edev->name);
373f68a8342SChanwoo Choi }
374f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name);
375f68a8342SChanwoo Choi 
376f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev,
377f68a8342SChanwoo Choi 			       struct device_attribute *attr, char *buf)
378f68a8342SChanwoo Choi {
379f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
380f68a8342SChanwoo Choi 						  attr_name);
3812a9de9c0SChanwoo Choi 	int i = cable->cable_index;
382f68a8342SChanwoo Choi 
383f68a8342SChanwoo Choi 	return sprintf(buf, "%s\n",
38455e4e2f1SChanwoo Choi 			extcon_info[cable->edev->supported_cable[i]].name);
385f68a8342SChanwoo Choi }
386f68a8342SChanwoo Choi 
387f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev,
388f68a8342SChanwoo Choi 				struct device_attribute *attr, char *buf)
389f68a8342SChanwoo Choi {
390f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
391f68a8342SChanwoo Choi 						  attr_state);
392f68a8342SChanwoo Choi 
393be052cc8SRoger Quadros 	int i = cable->cable_index;
394be052cc8SRoger Quadros 
395f68a8342SChanwoo Choi 	return sprintf(buf, "%d\n",
396575c2b86SChanwoo Choi 		extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
397f68a8342SChanwoo Choi }
398f68a8342SChanwoo Choi 
399f68a8342SChanwoo Choi /**
4006ab6094fSChanwoo Choi  * extcon_sync() - Synchronize the state for an external connector.
4016ab6094fSChanwoo Choi  * @edev:	the extcon device
4026506f6a0SYang Li  * @id:		the unique id indicating an external connector
403f68a8342SChanwoo Choi  *
4046ab6094fSChanwoo Choi  * Note that this function send a notification in order to synchronize
4056ab6094fSChanwoo Choi  * the state and property of an external connector.
4066ab6094fSChanwoo Choi  *
4076ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
408f68a8342SChanwoo Choi  */
409ab11af04SChanwoo Choi int extcon_sync(struct extcon_dev *edev, unsigned int id)
410f68a8342SChanwoo Choi {
411f68a8342SChanwoo Choi 	char name_buf[120];
412f68a8342SChanwoo Choi 	char state_buf[120];
413f68a8342SChanwoo Choi 	char *prop_buf;
414f68a8342SChanwoo Choi 	char *envp[3];
415f68a8342SChanwoo Choi 	int env_offset = 0;
416f68a8342SChanwoo Choi 	int length;
417046050f6SChanwoo Choi 	int index;
418ab11af04SChanwoo Choi 	int state;
419f68a8342SChanwoo Choi 	unsigned long flags;
420f68a8342SChanwoo Choi 
4217eae43aeSChanwoo Choi 	if (!edev)
4227eae43aeSChanwoo Choi 		return -EINVAL;
4237eae43aeSChanwoo Choi 
424ab11af04SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
425ab11af04SChanwoo Choi 	if (index < 0)
426ab11af04SChanwoo Choi 		return index;
427ab11af04SChanwoo Choi 
428f68a8342SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
429ab11af04SChanwoo Choi 	state = !!(edev->state & BIT(index));
4308a9dbb77SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
431815429b3SChanwoo Choi 
432815429b3SChanwoo Choi 	/*
433815429b3SChanwoo Choi 	 * Call functions in a raw notifier chain for the specific one
434815429b3SChanwoo Choi 	 * external connector.
435815429b3SChanwoo Choi 	 */
436ab11af04SChanwoo Choi 	raw_notifier_call_chain(&edev->nh[index], state, edev);
437f7a89811SRoger Quadros 
438815429b3SChanwoo Choi 	/*
439815429b3SChanwoo Choi 	 * Call functions in a raw notifier chain for the all supported
440815429b3SChanwoo Choi 	 * external connectors.
441815429b3SChanwoo Choi 	 */
442815429b3SChanwoo Choi 	raw_notifier_call_chain(&edev->nh_all, state, edev);
443815429b3SChanwoo Choi 
4448a9dbb77SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
445f68a8342SChanwoo Choi 	/* This could be in interrupt handler */
446f68a8342SChanwoo Choi 	prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
447ab11af04SChanwoo Choi 	if (!prop_buf) {
448f68a8342SChanwoo Choi 		/* Unlock early before uevent */
449f68a8342SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
450f68a8342SChanwoo Choi 
451f68a8342SChanwoo Choi 		dev_err(&edev->dev, "out of memory in extcon_set_state\n");
452f68a8342SChanwoo Choi 		kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
453f68a8342SChanwoo Choi 
454e7d9dd5aSPan Bian 		return -ENOMEM;
455f68a8342SChanwoo Choi 	}
456f68a8342SChanwoo Choi 
457ab11af04SChanwoo Choi 	length = name_show(&edev->dev, NULL, prop_buf);
458ab11af04SChanwoo Choi 	if (length > 0) {
459ab11af04SChanwoo Choi 		if (prop_buf[length - 1] == '\n')
460ab11af04SChanwoo Choi 			prop_buf[length - 1] = 0;
461ab11af04SChanwoo Choi 		snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
462ab11af04SChanwoo Choi 		envp[env_offset++] = name_buf;
463ab11af04SChanwoo Choi 	}
464ab11af04SChanwoo Choi 
465ab11af04SChanwoo Choi 	length = state_show(&edev->dev, NULL, prop_buf);
466ab11af04SChanwoo Choi 	if (length > 0) {
467ab11af04SChanwoo Choi 		if (prop_buf[length - 1] == '\n')
468ab11af04SChanwoo Choi 			prop_buf[length - 1] = 0;
469ab11af04SChanwoo Choi 		snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
470ab11af04SChanwoo Choi 		envp[env_offset++] = state_buf;
471ab11af04SChanwoo Choi 	}
472ab11af04SChanwoo Choi 	envp[env_offset] = NULL;
473ab11af04SChanwoo Choi 
474ab11af04SChanwoo Choi 	/* Unlock early before uevent */
475ab11af04SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
476ab11af04SChanwoo Choi 	kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
477ab11af04SChanwoo Choi 	free_page((unsigned long)prop_buf);
478ab11af04SChanwoo Choi 
479ab11af04SChanwoo Choi 	return 0;
480ab11af04SChanwoo Choi }
481ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_sync);
482ab11af04SChanwoo Choi 
483f68a8342SChanwoo Choi /**
4846ab6094fSChanwoo Choi  * extcon_get_state() - Get the state of an external connector.
4856ab6094fSChanwoo Choi  * @edev:	the extcon device
4866ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
4876ab6094fSChanwoo Choi  *
4886ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
489f68a8342SChanwoo Choi  */
490575c2b86SChanwoo Choi int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
491f68a8342SChanwoo Choi {
492575c2b86SChanwoo Choi 	int index, state;
493575c2b86SChanwoo Choi 	unsigned long flags;
4942a9de9c0SChanwoo Choi 
4957eae43aeSChanwoo Choi 	if (!edev)
4967eae43aeSChanwoo Choi 		return -EINVAL;
4977eae43aeSChanwoo Choi 
4982a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
4992a9de9c0SChanwoo Choi 	if (index < 0)
5002a9de9c0SChanwoo Choi 		return index;
5012a9de9c0SChanwoo Choi 
502575c2b86SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
503575c2b86SChanwoo Choi 	state = is_extcon_attached(edev, index);
504575c2b86SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
505f68a8342SChanwoo Choi 
506575c2b86SChanwoo Choi 	return state;
507f68a8342SChanwoo Choi }
508575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_state);
509f68a8342SChanwoo Choi 
510f68a8342SChanwoo Choi /**
5116ab6094fSChanwoo Choi  * extcon_set_state() - Set the state of an external connector.
5126ab6094fSChanwoo Choi  * @edev:	the extcon device
5136ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5146ab6094fSChanwoo Choi  * @state:	the new state of an external connector.
5156ab6094fSChanwoo Choi  *		the default semantics is true: attached / false: detached.
516ab11af04SChanwoo Choi  *
5176ab6094fSChanwoo Choi  * Note that this function set the state of an external connector without
5186ab6094fSChanwoo Choi  * a notification. To synchronize the state of an external connector,
5196ab6094fSChanwoo Choi  * have to use extcon_set_state_sync() and extcon_sync().
5206ab6094fSChanwoo Choi  *
5216ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
522f68a8342SChanwoo Choi  */
5236ab6094fSChanwoo Choi int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
524f68a8342SChanwoo Choi {
525ab11af04SChanwoo Choi 	unsigned long flags;
526ab11af04SChanwoo Choi 	int index, ret = 0;
527f68a8342SChanwoo Choi 
5287eae43aeSChanwoo Choi 	if (!edev)
5297eae43aeSChanwoo Choi 		return -EINVAL;
5307eae43aeSChanwoo Choi 
5312a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
5322a9de9c0SChanwoo Choi 	if (index < 0)
5332a9de9c0SChanwoo Choi 		return index;
5342a9de9c0SChanwoo Choi 
535ab11af04SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
536ab11af04SChanwoo Choi 
537ab11af04SChanwoo Choi 	/* Check whether the external connector's state is changed. */
5386ab6094fSChanwoo Choi 	if (!is_extcon_changed(edev, index, state))
539ab11af04SChanwoo Choi 		goto out;
540ab11af04SChanwoo Choi 
541ab11af04SChanwoo Choi 	if (check_mutually_exclusive(edev,
5426ab6094fSChanwoo Choi 		(edev->state & ~BIT(index)) | (state & BIT(index)))) {
543ab11af04SChanwoo Choi 		ret = -EPERM;
544ab11af04SChanwoo Choi 		goto out;
545ab11af04SChanwoo Choi 	}
546ab11af04SChanwoo Choi 
547792e7e9eSChanwoo Choi 	/*
548792e7e9eSChanwoo Choi 	 * Initialize the value of extcon property before setting
549792e7e9eSChanwoo Choi 	 * the detached state for an external connector.
550792e7e9eSChanwoo Choi 	 */
5516ab6094fSChanwoo Choi 	if (!state)
552792e7e9eSChanwoo Choi 		init_property(edev, id, index);
553792e7e9eSChanwoo Choi 
5546ab6094fSChanwoo Choi 	/* Update the state for an external connector. */
5556ab6094fSChanwoo Choi 	if (state)
556ab11af04SChanwoo Choi 		edev->state |= BIT(index);
557ab11af04SChanwoo Choi 	else
558ab11af04SChanwoo Choi 		edev->state &= ~(BIT(index));
559ab11af04SChanwoo Choi out:
560ab11af04SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
561ab11af04SChanwoo Choi 
562ab11af04SChanwoo Choi 	return ret;
563f68a8342SChanwoo Choi }
564575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state);
565f68a8342SChanwoo Choi 
566f68a8342SChanwoo Choi /**
5676ab6094fSChanwoo Choi  * extcon_set_state_sync() - Set the state of an external connector with sync.
5686ab6094fSChanwoo Choi  * @edev:	the extcon device
5696ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5706ab6094fSChanwoo Choi  * @state:	the new state of external connector.
5716ab6094fSChanwoo Choi  *		the default semantics is true: attached / false: detached.
572ab11af04SChanwoo Choi  *
5736ab6094fSChanwoo Choi  * Note that this function set the state of external connector
5746ab6094fSChanwoo Choi  * and synchronize the state by sending a notification.
5756ab6094fSChanwoo Choi  *
5766ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
577ab11af04SChanwoo Choi  */
5786ab6094fSChanwoo Choi int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
579ab11af04SChanwoo Choi {
5802da3db7fSAlexander Stein 	int ret;
581ab11af04SChanwoo Choi 
5826ab6094fSChanwoo Choi 	ret = extcon_set_state(edev, id, state);
583ab11af04SChanwoo Choi 	if (ret < 0)
584ab11af04SChanwoo Choi 		return ret;
585ab11af04SChanwoo Choi 
586ab11af04SChanwoo Choi 	return extcon_sync(edev, id);
587ab11af04SChanwoo Choi }
588ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state_sync);
589ab11af04SChanwoo Choi 
590ab11af04SChanwoo Choi /**
5916ab6094fSChanwoo Choi  * extcon_get_property() - Get the property value of an external connector.
5926ab6094fSChanwoo Choi  * @edev:	the extcon device
5936ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5946ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
5956ab6094fSChanwoo Choi  * @prop_val:	the pointer which store the value of extcon property
596792e7e9eSChanwoo Choi  *
5976ab6094fSChanwoo Choi  * Note that when getting the property value of external connector,
5986ab6094fSChanwoo Choi  * the external connector should be attached. If detached state, function
5996ab6094fSChanwoo Choi  * return 0 without property value. Also, the each property should be
6006ab6094fSChanwoo Choi  * included in the list of supported properties according to extcon type.
601792e7e9eSChanwoo Choi  *
6026ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
603792e7e9eSChanwoo Choi  */
604792e7e9eSChanwoo Choi int extcon_get_property(struct extcon_dev *edev, unsigned int id,
605792e7e9eSChanwoo Choi 				unsigned int prop,
606792e7e9eSChanwoo Choi 				union extcon_property_value *prop_val)
607792e7e9eSChanwoo Choi {
608792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
609792e7e9eSChanwoo Choi 	unsigned long flags;
610792e7e9eSChanwoo Choi 	int index, ret = 0;
611792e7e9eSChanwoo Choi 
612cff7499dSAndy Shevchenko 	*prop_val = (union extcon_property_value){0};
613792e7e9eSChanwoo Choi 
614792e7e9eSChanwoo Choi 	if (!edev)
615792e7e9eSChanwoo Choi 		return -EINVAL;
616792e7e9eSChanwoo Choi 
617792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
618792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
619792e7e9eSChanwoo Choi 		return -EINVAL;
620792e7e9eSChanwoo Choi 
621792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
622792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
623792e7e9eSChanwoo Choi 	if (index < 0)
624792e7e9eSChanwoo Choi 		return index;
625792e7e9eSChanwoo Choi 
626792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
627792e7e9eSChanwoo Choi 
6287f2a0a16SChanwoo Choi 	/* Check whether the property is available or not. */
6297f2a0a16SChanwoo Choi 	if (!is_extcon_property_capability(edev, id, index, prop)) {
6307f2a0a16SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
6317f2a0a16SChanwoo Choi 		return -EPERM;
6327f2a0a16SChanwoo Choi 	}
6337f2a0a16SChanwoo Choi 
634792e7e9eSChanwoo Choi 	/*
635792e7e9eSChanwoo Choi 	 * Check whether the external connector is attached.
636792e7e9eSChanwoo Choi 	 * If external connector is detached, the user can not
637792e7e9eSChanwoo Choi 	 * get the property value.
638792e7e9eSChanwoo Choi 	 */
639792e7e9eSChanwoo Choi 	if (!is_extcon_attached(edev, index)) {
640792e7e9eSChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
641792e7e9eSChanwoo Choi 		return 0;
642792e7e9eSChanwoo Choi 	}
643792e7e9eSChanwoo Choi 
644792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
645792e7e9eSChanwoo Choi 
646792e7e9eSChanwoo Choi 	/* Get the property value according to extcon type */
647792e7e9eSChanwoo Choi 	switch (prop) {
648792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
649792e7e9eSChanwoo Choi 		*prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
650792e7e9eSChanwoo Choi 		break;
651792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
652792e7e9eSChanwoo Choi 		*prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
653792e7e9eSChanwoo Choi 		break;
654792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
655792e7e9eSChanwoo Choi 		*prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
656792e7e9eSChanwoo Choi 		break;
657792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
658792e7e9eSChanwoo Choi 		*prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
659792e7e9eSChanwoo Choi 		break;
660792e7e9eSChanwoo Choi 	default:
661792e7e9eSChanwoo Choi 		ret = -EINVAL;
662792e7e9eSChanwoo Choi 		break;
663792e7e9eSChanwoo Choi 	}
664792e7e9eSChanwoo Choi 
665792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
666792e7e9eSChanwoo Choi 
667792e7e9eSChanwoo Choi 	return ret;
668792e7e9eSChanwoo Choi }
669792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property);
670792e7e9eSChanwoo Choi 
671792e7e9eSChanwoo Choi /**
6726ab6094fSChanwoo Choi  * extcon_set_property() - Set the property value of an external connector.
6736ab6094fSChanwoo Choi  * @edev:	the extcon device
6746ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
6756ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
6766ab6094fSChanwoo Choi  * @prop_val:	the pointer including the new value of extcon property
677792e7e9eSChanwoo Choi  *
6786ab6094fSChanwoo Choi  * Note that each property should be included in the list of supported
6796ab6094fSChanwoo Choi  * properties according to the extcon type.
680792e7e9eSChanwoo Choi  *
6816ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
682792e7e9eSChanwoo Choi  */
683792e7e9eSChanwoo Choi int extcon_set_property(struct extcon_dev *edev, unsigned int id,
684792e7e9eSChanwoo Choi 				unsigned int prop,
685792e7e9eSChanwoo Choi 				union extcon_property_value prop_val)
686792e7e9eSChanwoo Choi {
687792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
688792e7e9eSChanwoo Choi 	unsigned long flags;
689792e7e9eSChanwoo Choi 	int index, ret = 0;
690792e7e9eSChanwoo Choi 
691792e7e9eSChanwoo Choi 	if (!edev)
692792e7e9eSChanwoo Choi 		return -EINVAL;
693792e7e9eSChanwoo Choi 
694792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
695792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
696792e7e9eSChanwoo Choi 		return -EINVAL;
697792e7e9eSChanwoo Choi 
698792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
699792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
700792e7e9eSChanwoo Choi 	if (index < 0)
701792e7e9eSChanwoo Choi 		return index;
702792e7e9eSChanwoo Choi 
703792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
704792e7e9eSChanwoo Choi 
7057f2a0a16SChanwoo Choi 	/* Check whether the property is available or not. */
7067f2a0a16SChanwoo Choi 	if (!is_extcon_property_capability(edev, id, index, prop)) {
7077f2a0a16SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
7087f2a0a16SChanwoo Choi 		return -EPERM;
7097f2a0a16SChanwoo Choi 	}
7107f2a0a16SChanwoo Choi 
711792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
712792e7e9eSChanwoo Choi 
713792e7e9eSChanwoo Choi 	/* Set the property value according to extcon type */
714792e7e9eSChanwoo Choi 	switch (prop) {
715792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
716792e7e9eSChanwoo Choi 		cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
717792e7e9eSChanwoo Choi 		break;
718792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
719792e7e9eSChanwoo Choi 		cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
720792e7e9eSChanwoo Choi 		break;
721792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
722792e7e9eSChanwoo Choi 		cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
723792e7e9eSChanwoo Choi 		break;
724792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
725792e7e9eSChanwoo Choi 		cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
726792e7e9eSChanwoo Choi 		break;
727792e7e9eSChanwoo Choi 	default:
728792e7e9eSChanwoo Choi 		ret = -EINVAL;
729792e7e9eSChanwoo Choi 		break;
730792e7e9eSChanwoo Choi 	}
731792e7e9eSChanwoo Choi 
732792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
733792e7e9eSChanwoo Choi 
734792e7e9eSChanwoo Choi 	return ret;
735792e7e9eSChanwoo Choi }
736792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property);
737792e7e9eSChanwoo Choi 
738792e7e9eSChanwoo Choi /**
7396ab6094fSChanwoo Choi  * extcon_set_property_sync() - Set property of an external connector with sync.
7406506f6a0SYang Li  * @edev:	the extcon device
7416506f6a0SYang Li  * @id:		the unique id indicating an external connector
7426506f6a0SYang Li  * @prop:	the property id indicating an extcon property
7436ab6094fSChanwoo Choi  * @prop_val:	the pointer including the new value of extcon property
744ab11af04SChanwoo Choi  *
7456ab6094fSChanwoo Choi  * Note that when setting the property value of external connector,
7466ab6094fSChanwoo Choi  * the external connector should be attached. The each property should
7476ab6094fSChanwoo Choi  * be included in the list of supported properties according to extcon type.
748ab11af04SChanwoo Choi  *
7496ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
750ab11af04SChanwoo Choi  */
751ab11af04SChanwoo Choi int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
752ab11af04SChanwoo Choi 				unsigned int prop,
753ab11af04SChanwoo Choi 				union extcon_property_value prop_val)
754ab11af04SChanwoo Choi {
755ab11af04SChanwoo Choi 	int ret;
756ab11af04SChanwoo Choi 
757ab11af04SChanwoo Choi 	ret = extcon_set_property(edev, id, prop, prop_val);
758ab11af04SChanwoo Choi 	if (ret < 0)
759ab11af04SChanwoo Choi 		return ret;
760ab11af04SChanwoo Choi 
761ab11af04SChanwoo Choi 	return extcon_sync(edev, id);
762ab11af04SChanwoo Choi }
763ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_sync);
764ab11af04SChanwoo Choi 
765ab11af04SChanwoo Choi /**
7666ab6094fSChanwoo Choi  * extcon_get_property_capability() - Get the capability of the property
7676ab6094fSChanwoo Choi  *					for an external connector.
7686ab6094fSChanwoo Choi  * @edev:	the extcon device
7696ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
7706ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
7717f2a0a16SChanwoo Choi  *
7727f2a0a16SChanwoo Choi  * Returns 1 if the property is available or 0 if not available.
7737f2a0a16SChanwoo Choi  */
7747f2a0a16SChanwoo Choi int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
7757f2a0a16SChanwoo Choi 					unsigned int prop)
7767f2a0a16SChanwoo Choi {
7777f2a0a16SChanwoo Choi 	int index;
7787f2a0a16SChanwoo Choi 
7797f2a0a16SChanwoo Choi 	if (!edev)
7807f2a0a16SChanwoo Choi 		return -EINVAL;
7817f2a0a16SChanwoo Choi 
7827f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not */
7837f2a0a16SChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
7847f2a0a16SChanwoo Choi 		return -EINVAL;
7857f2a0a16SChanwoo Choi 
7867f2a0a16SChanwoo Choi 	/* Find the cable index of external connector by using id */
7877f2a0a16SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
7887f2a0a16SChanwoo Choi 	if (index < 0)
7897f2a0a16SChanwoo Choi 		return index;
7907f2a0a16SChanwoo Choi 
7917f2a0a16SChanwoo Choi 	return is_extcon_property_capability(edev, id, index, prop);
7927f2a0a16SChanwoo Choi }
7937f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property_capability);
7947f2a0a16SChanwoo Choi 
7957f2a0a16SChanwoo Choi /**
7966ab6094fSChanwoo Choi  * extcon_set_property_capability() - Set the capability of the property
7976ab6094fSChanwoo Choi  *					for an external connector.
7986ab6094fSChanwoo Choi  * @edev:	the extcon device
7996ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
8006ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
8017f2a0a16SChanwoo Choi  *
8026ab6094fSChanwoo Choi  * Note that this function set the capability of the property
8036ab6094fSChanwoo Choi  * for an external connector in order to mark the bit in capability
8046ab6094fSChanwoo Choi  * bitmap which mean the available state of the property.
8057f2a0a16SChanwoo Choi  *
8066ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
8077f2a0a16SChanwoo Choi  */
8087f2a0a16SChanwoo Choi int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
8097f2a0a16SChanwoo Choi 					unsigned int prop)
8107f2a0a16SChanwoo Choi {
8117f2a0a16SChanwoo Choi 	struct extcon_cable *cable;
8127f2a0a16SChanwoo Choi 	int index, type, ret = 0;
8137f2a0a16SChanwoo Choi 
8147f2a0a16SChanwoo Choi 	if (!edev)
8157f2a0a16SChanwoo Choi 		return -EINVAL;
8167f2a0a16SChanwoo Choi 
8177f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not. */
8187f2a0a16SChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
8197f2a0a16SChanwoo Choi 		return -EINVAL;
8207f2a0a16SChanwoo Choi 
8217f2a0a16SChanwoo Choi 	/* Find the cable index of external connector by using id. */
8227f2a0a16SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
8237f2a0a16SChanwoo Choi 	if (index < 0)
8247f2a0a16SChanwoo Choi 		return index;
8257f2a0a16SChanwoo Choi 
8267f2a0a16SChanwoo Choi 	type = get_extcon_type(prop);
8277f2a0a16SChanwoo Choi 	if (type < 0)
8287f2a0a16SChanwoo Choi 		return type;
8297f2a0a16SChanwoo Choi 
8307f2a0a16SChanwoo Choi 	cable = &edev->cables[index];
8317f2a0a16SChanwoo Choi 
8327f2a0a16SChanwoo Choi 	switch (type) {
8337f2a0a16SChanwoo Choi 	case EXTCON_TYPE_USB:
8347f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
8357f2a0a16SChanwoo Choi 		break;
8367f2a0a16SChanwoo Choi 	case EXTCON_TYPE_CHG:
8377f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
8387f2a0a16SChanwoo Choi 		break;
8397f2a0a16SChanwoo Choi 	case EXTCON_TYPE_JACK:
8407f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
8417f2a0a16SChanwoo Choi 		break;
8427f2a0a16SChanwoo Choi 	case EXTCON_TYPE_DISP:
8437f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
8447f2a0a16SChanwoo Choi 		break;
8457f2a0a16SChanwoo Choi 	default:
8467f2a0a16SChanwoo Choi 		ret = -EINVAL;
8477f2a0a16SChanwoo Choi 	}
8487f2a0a16SChanwoo Choi 
8497f2a0a16SChanwoo Choi 	return ret;
8507f2a0a16SChanwoo Choi }
8517f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_capability);
8527f2a0a16SChanwoo Choi 
8537f2a0a16SChanwoo Choi /**
8546ab6094fSChanwoo Choi  * extcon_get_extcon_dev() - Get the extcon device instance from the name.
8556ab6094fSChanwoo Choi  * @extcon_name:	the extcon name provided with extcon_dev_register()
8566ab6094fSChanwoo Choi  *
8576ab6094fSChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
85858e4a2d2SDan Carpenter  * NOTE: This function returns -EPROBE_DEFER so it may only be called from
85958e4a2d2SDan Carpenter  * probe() functions.
860f68a8342SChanwoo Choi  */
861f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
862f68a8342SChanwoo Choi {
863f68a8342SChanwoo Choi 	struct extcon_dev *sd;
864f68a8342SChanwoo Choi 
8657eae43aeSChanwoo Choi 	if (!extcon_name)
8667eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
8677eae43aeSChanwoo Choi 
868f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
869f68a8342SChanwoo Choi 	list_for_each_entry(sd, &extcon_dev_list, entry) {
870f68a8342SChanwoo Choi 		if (!strcmp(sd->name, extcon_name))
871f68a8342SChanwoo Choi 			goto out;
872f68a8342SChanwoo Choi 	}
87358e4a2d2SDan Carpenter 	sd = ERR_PTR(-EPROBE_DEFER);
874f68a8342SChanwoo Choi out:
875f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
876f68a8342SChanwoo Choi 	return sd;
877f68a8342SChanwoo Choi }
878f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
879f68a8342SChanwoo Choi 
880f68a8342SChanwoo Choi /**
8816ab6094fSChanwoo Choi  * extcon_register_notifier() - Register a notifier block to get notified by
8826ab6094fSChanwoo Choi  *				any state changes from the extcon.
8836ab6094fSChanwoo Choi  * @edev:	the extcon device
8846ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
8856ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
886f68a8342SChanwoo Choi  *
887f68a8342SChanwoo Choi  * Note that the second parameter given to the callback of nb (val) is
8886ab6094fSChanwoo Choi  * the current state of an external connector and the third pameter
8896ab6094fSChanwoo Choi  * is the pointer of extcon device.
8906ab6094fSChanwoo Choi  *
8916ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
892f68a8342SChanwoo Choi  */
89373b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
894f68a8342SChanwoo Choi 			     struct notifier_block *nb)
895f68a8342SChanwoo Choi {
89666bee35fSHans de Goede 	unsigned long flags;
8971fa80f18SColin Ian King 	int ret, idx;
898046050f6SChanwoo Choi 
89901b4c9a1SChanwoo Choi 	if (!edev || !nb)
9007eae43aeSChanwoo Choi 		return -EINVAL;
9017eae43aeSChanwoo Choi 
902046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
903a05f44c8SStephen Boyd 	if (idx < 0)
904a05f44c8SStephen Boyd 		return idx;
90566bee35fSHans de Goede 
90666bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
907046050f6SChanwoo Choi 	ret = raw_notifier_chain_register(&edev->nh[idx], nb);
90866bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
90966bee35fSHans de Goede 
91066bee35fSHans de Goede 	return ret;
911f68a8342SChanwoo Choi }
912f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier);
913f68a8342SChanwoo Choi 
914f68a8342SChanwoo Choi /**
9156ab6094fSChanwoo Choi  * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
9166ab6094fSChanwoo Choi  * @edev:	the extcon device
9176ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
9186ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
9196ab6094fSChanwoo Choi  *
9206ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
921f68a8342SChanwoo Choi  */
92273b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
923f68a8342SChanwoo Choi 				struct notifier_block *nb)
924f68a8342SChanwoo Choi {
92566bee35fSHans de Goede 	unsigned long flags;
926046050f6SChanwoo Choi 	int ret, idx;
927046050f6SChanwoo Choi 
9287eae43aeSChanwoo Choi 	if (!edev || !nb)
9297eae43aeSChanwoo Choi 		return -EINVAL;
9307eae43aeSChanwoo Choi 
931046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
932a05f44c8SStephen Boyd 	if (idx < 0)
933a05f44c8SStephen Boyd 		return idx;
93466bee35fSHans de Goede 
93566bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
936046050f6SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
93766bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
93866bee35fSHans de Goede 
93966bee35fSHans de Goede 	return ret;
940f68a8342SChanwoo Choi }
941f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
942f68a8342SChanwoo Choi 
943815429b3SChanwoo Choi /**
9446ab6094fSChanwoo Choi  * extcon_register_notifier_all() - Register a notifier block for all connectors.
9456ab6094fSChanwoo Choi  * @edev:	the extcon device
9466ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
947815429b3SChanwoo Choi  *
9486ab6094fSChanwoo Choi  * Note that this function registers a notifier block in order to receive
9496ab6094fSChanwoo Choi  * the state change of all supported external connectors from extcon device.
950826a47e9SMarkus Elfring  * And the second parameter given to the callback of nb (val) is
9516ab6094fSChanwoo Choi  * the current state and the third pameter is the pointer of extcon device.
952815429b3SChanwoo Choi  *
9536ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
954815429b3SChanwoo Choi  */
955815429b3SChanwoo Choi int extcon_register_notifier_all(struct extcon_dev *edev,
956815429b3SChanwoo Choi 				struct notifier_block *nb)
957815429b3SChanwoo Choi {
958815429b3SChanwoo Choi 	unsigned long flags;
959815429b3SChanwoo Choi 	int ret;
960815429b3SChanwoo Choi 
961815429b3SChanwoo Choi 	if (!edev || !nb)
962815429b3SChanwoo Choi 		return -EINVAL;
963815429b3SChanwoo Choi 
964815429b3SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
965815429b3SChanwoo Choi 	ret = raw_notifier_chain_register(&edev->nh_all, nb);
966815429b3SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
967815429b3SChanwoo Choi 
968815429b3SChanwoo Choi 	return ret;
969815429b3SChanwoo Choi }
970815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
971815429b3SChanwoo Choi 
972815429b3SChanwoo Choi /**
973815429b3SChanwoo Choi  * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
9746ab6094fSChanwoo Choi  * @edev:	the extcon device
9756ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
976815429b3SChanwoo Choi  *
9776ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
978815429b3SChanwoo Choi  */
979815429b3SChanwoo Choi int extcon_unregister_notifier_all(struct extcon_dev *edev,
980815429b3SChanwoo Choi 				struct notifier_block *nb)
981815429b3SChanwoo Choi {
982815429b3SChanwoo Choi 	unsigned long flags;
983815429b3SChanwoo Choi 	int ret;
984815429b3SChanwoo Choi 
985815429b3SChanwoo Choi 	if (!edev || !nb)
986815429b3SChanwoo Choi 		return -EINVAL;
987815429b3SChanwoo Choi 
988815429b3SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
989815429b3SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
990815429b3SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
991815429b3SChanwoo Choi 
992815429b3SChanwoo Choi 	return ret;
993815429b3SChanwoo Choi }
994815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
995815429b3SChanwoo Choi 
996f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = {
997f68a8342SChanwoo Choi 	&dev_attr_state.attr,
998f68a8342SChanwoo Choi 	&dev_attr_name.attr,
999f68a8342SChanwoo Choi 	NULL,
1000f68a8342SChanwoo Choi };
1001f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon);
1002f68a8342SChanwoo Choi 
1003f68a8342SChanwoo Choi static int create_extcon_class(void)
1004f68a8342SChanwoo Choi {
1005f68a8342SChanwoo Choi 	if (!extcon_class) {
1006f68a8342SChanwoo Choi 		extcon_class = class_create(THIS_MODULE, "extcon");
1007f68a8342SChanwoo Choi 		if (IS_ERR(extcon_class))
1008f68a8342SChanwoo Choi 			return PTR_ERR(extcon_class);
1009f68a8342SChanwoo Choi 		extcon_class->dev_groups = extcon_groups;
1010f68a8342SChanwoo Choi 	}
1011f68a8342SChanwoo Choi 
1012f68a8342SChanwoo Choi 	return 0;
1013f68a8342SChanwoo Choi }
1014f68a8342SChanwoo Choi 
1015f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev)
1016f68a8342SChanwoo Choi {
1017f68a8342SChanwoo Choi }
1018f68a8342SChanwoo Choi 
1019f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive";
1020f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev)
1021f68a8342SChanwoo Choi {
1022f68a8342SChanwoo Choi }
1023f68a8342SChanwoo Choi 
1024f68a8342SChanwoo Choi /*
1025f68a8342SChanwoo Choi  * extcon_dev_allocate() - Allocate the memory of extcon device.
10266ab6094fSChanwoo Choi  * @supported_cable:	the array of the supported external connectors
10276ab6094fSChanwoo Choi  *			ending with EXTCON_NONE.
1028f68a8342SChanwoo Choi  *
10296ab6094fSChanwoo Choi  * Note that this function allocates the memory for extcon device
10306ab6094fSChanwoo Choi  * and initialize default setting for the extcon device.
1031f68a8342SChanwoo Choi  *
10326ab6094fSChanwoo Choi  * Returns the pointer memory of allocated extcon_dev if success
10336ab6094fSChanwoo Choi  * or ERR_PTR(err) if fail.
1034f68a8342SChanwoo Choi  */
103573b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1036f68a8342SChanwoo Choi {
1037f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1038f68a8342SChanwoo Choi 
10397eae43aeSChanwoo Choi 	if (!supported_cable)
10407eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
10417eae43aeSChanwoo Choi 
1042f68a8342SChanwoo Choi 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1043f68a8342SChanwoo Choi 	if (!edev)
1044f68a8342SChanwoo Choi 		return ERR_PTR(-ENOMEM);
1045f68a8342SChanwoo Choi 
1046f68a8342SChanwoo Choi 	edev->max_supported = 0;
1047f68a8342SChanwoo Choi 	edev->supported_cable = supported_cable;
1048f68a8342SChanwoo Choi 
1049f68a8342SChanwoo Choi 	return edev;
1050f68a8342SChanwoo Choi }
1051f68a8342SChanwoo Choi 
1052f68a8342SChanwoo Choi /*
1053f68a8342SChanwoo Choi  * extcon_dev_free() - Free the memory of extcon device.
10546ab6094fSChanwoo Choi  * @edev:	the extcon device
1055f68a8342SChanwoo Choi  */
1056f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev)
1057f68a8342SChanwoo Choi {
1058f68a8342SChanwoo Choi 	kfree(edev);
1059f68a8342SChanwoo Choi }
1060f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free);
1061f68a8342SChanwoo Choi 
1062f68a8342SChanwoo Choi /**
10636ab6094fSChanwoo Choi  * extcon_dev_register() - Register an new extcon device
10646ab6094fSChanwoo Choi  * @edev:	the extcon device to be registered
1065f68a8342SChanwoo Choi  *
1066f68a8342SChanwoo Choi  * Among the members of edev struct, please set the "user initializing data"
1067f68a8342SChanwoo Choi  * do not set the values of "internal data", which are initialized by
1068f68a8342SChanwoo Choi  * this function.
10696ab6094fSChanwoo Choi  *
10706ab6094fSChanwoo Choi  * Note that before calling this funciton, have to allocate the memory
10716ab6094fSChanwoo Choi  * of an extcon device by using the extcon_dev_allocate(). And the extcon
10726ab6094fSChanwoo Choi  * dev should include the supported_cable information.
10736ab6094fSChanwoo Choi  *
10746ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
1075f68a8342SChanwoo Choi  */
1076f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev)
1077f68a8342SChanwoo Choi {
1078f68a8342SChanwoo Choi 	int ret, index = 0;
107971c3ffa5SChanwoo Choi 	static atomic_t edev_no = ATOMIC_INIT(-1);
1080f68a8342SChanwoo Choi 
1081f68a8342SChanwoo Choi 	if (!extcon_class) {
1082f68a8342SChanwoo Choi 		ret = create_extcon_class();
1083f68a8342SChanwoo Choi 		if (ret < 0)
1084f68a8342SChanwoo Choi 			return ret;
1085f68a8342SChanwoo Choi 	}
1086f68a8342SChanwoo Choi 
10877eae43aeSChanwoo Choi 	if (!edev || !edev->supported_cable)
10882a9de9c0SChanwoo Choi 		return -EINVAL;
1089f68a8342SChanwoo Choi 
10902a9de9c0SChanwoo Choi 	for (; edev->supported_cable[index] != EXTCON_NONE; index++);
10912a9de9c0SChanwoo Choi 
10922a9de9c0SChanwoo Choi 	edev->max_supported = index;
1093f68a8342SChanwoo Choi 	if (index > SUPPORTED_CABLE_MAX) {
10942a9de9c0SChanwoo Choi 		dev_err(&edev->dev,
10952a9de9c0SChanwoo Choi 			"exceed the maximum number of supported cables\n");
1096f68a8342SChanwoo Choi 		return -EINVAL;
1097f68a8342SChanwoo Choi 	}
1098f68a8342SChanwoo Choi 
1099f68a8342SChanwoo Choi 	edev->dev.class = extcon_class;
1100f68a8342SChanwoo Choi 	edev->dev.release = extcon_dev_release;
1101f68a8342SChanwoo Choi 
110271c3ffa5SChanwoo Choi 	edev->name = dev_name(edev->dev.parent);
1103f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(edev->name)) {
1104f68a8342SChanwoo Choi 		dev_err(&edev->dev,
1105f68a8342SChanwoo Choi 			"extcon device name is null\n");
1106f68a8342SChanwoo Choi 		return -EINVAL;
1107f68a8342SChanwoo Choi 	}
110871c3ffa5SChanwoo Choi 	dev_set_name(&edev->dev, "extcon%lu",
110971c3ffa5SChanwoo Choi 			(unsigned long)atomic_inc_return(&edev_no));
1110f68a8342SChanwoo Choi 
1111f68a8342SChanwoo Choi 	if (edev->max_supported) {
1112f68a8342SChanwoo Choi 		char *str;
1113f68a8342SChanwoo Choi 		struct extcon_cable *cable;
1114f68a8342SChanwoo Choi 
11156396bb22SKees Cook 		edev->cables = kcalloc(edev->max_supported,
11166396bb22SKees Cook 				       sizeof(struct extcon_cable),
11176396bb22SKees Cook 				       GFP_KERNEL);
1118f68a8342SChanwoo Choi 		if (!edev->cables) {
1119f68a8342SChanwoo Choi 			ret = -ENOMEM;
1120f68a8342SChanwoo Choi 			goto err_sysfs_alloc;
1121f68a8342SChanwoo Choi 		}
1122f68a8342SChanwoo Choi 		for (index = 0; index < edev->max_supported; index++) {
1123f68a8342SChanwoo Choi 			cable = &edev->cables[index];
1124f68a8342SChanwoo Choi 
112569f75a4fSAndy Shevchenko 			str = kasprintf(GFP_KERNEL, "cable.%d", index);
1126f68a8342SChanwoo Choi 			if (!str) {
1127f68a8342SChanwoo Choi 				for (index--; index >= 0; index--) {
1128f68a8342SChanwoo Choi 					cable = &edev->cables[index];
1129f68a8342SChanwoo Choi 					kfree(cable->attr_g.name);
1130f68a8342SChanwoo Choi 				}
1131f68a8342SChanwoo Choi 				ret = -ENOMEM;
1132f68a8342SChanwoo Choi 
1133f68a8342SChanwoo Choi 				goto err_alloc_cables;
1134f68a8342SChanwoo Choi 			}
1135f68a8342SChanwoo Choi 
1136f68a8342SChanwoo Choi 			cable->edev = edev;
1137f68a8342SChanwoo Choi 			cable->cable_index = index;
1138f68a8342SChanwoo Choi 			cable->attrs[0] = &cable->attr_name.attr;
1139f68a8342SChanwoo Choi 			cable->attrs[1] = &cable->attr_state.attr;
1140f68a8342SChanwoo Choi 			cable->attrs[2] = NULL;
1141f68a8342SChanwoo Choi 			cable->attr_g.name = str;
1142f68a8342SChanwoo Choi 			cable->attr_g.attrs = cable->attrs;
1143f68a8342SChanwoo Choi 
1144f68a8342SChanwoo Choi 			sysfs_attr_init(&cable->attr_name.attr);
1145f68a8342SChanwoo Choi 			cable->attr_name.attr.name = "name";
1146f68a8342SChanwoo Choi 			cable->attr_name.attr.mode = 0444;
1147f68a8342SChanwoo Choi 			cable->attr_name.show = cable_name_show;
1148f68a8342SChanwoo Choi 
1149f68a8342SChanwoo Choi 			sysfs_attr_init(&cable->attr_state.attr);
1150f68a8342SChanwoo Choi 			cable->attr_state.attr.name = "state";
1151f68a8342SChanwoo Choi 			cable->attr_state.attr.mode = 0444;
1152f68a8342SChanwoo Choi 			cable->attr_state.show = cable_state_show;
1153f68a8342SChanwoo Choi 		}
1154f68a8342SChanwoo Choi 	}
1155f68a8342SChanwoo Choi 
1156f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
1157f68a8342SChanwoo Choi 		char *name;
1158f68a8342SChanwoo Choi 
1159f68a8342SChanwoo Choi 		/* Count the size of mutually_exclusive array */
1160f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
1161f68a8342SChanwoo Choi 			;
1162f68a8342SChanwoo Choi 
11636396bb22SKees Cook 		edev->attrs_muex = kcalloc(index + 1,
11646396bb22SKees Cook 					   sizeof(struct attribute *),
11656396bb22SKees Cook 					   GFP_KERNEL);
1166f68a8342SChanwoo Choi 		if (!edev->attrs_muex) {
1167f68a8342SChanwoo Choi 			ret = -ENOMEM;
1168f68a8342SChanwoo Choi 			goto err_muex;
1169f68a8342SChanwoo Choi 		}
1170f68a8342SChanwoo Choi 
11716396bb22SKees Cook 		edev->d_attrs_muex = kcalloc(index,
11726396bb22SKees Cook 					     sizeof(struct device_attribute),
11736396bb22SKees Cook 					     GFP_KERNEL);
1174f68a8342SChanwoo Choi 		if (!edev->d_attrs_muex) {
1175f68a8342SChanwoo Choi 			ret = -ENOMEM;
1176f68a8342SChanwoo Choi 			kfree(edev->attrs_muex);
1177f68a8342SChanwoo Choi 			goto err_muex;
1178f68a8342SChanwoo Choi 		}
1179f68a8342SChanwoo Choi 
1180f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++) {
118169f75a4fSAndy Shevchenko 			name = kasprintf(GFP_KERNEL, "0x%x",
118269f75a4fSAndy Shevchenko 					 edev->mutually_exclusive[index]);
1183f68a8342SChanwoo Choi 			if (!name) {
1184f68a8342SChanwoo Choi 				for (index--; index >= 0; index--) {
1185f68a8342SChanwoo Choi 					kfree(edev->d_attrs_muex[index].attr.
1186f68a8342SChanwoo Choi 					      name);
1187f68a8342SChanwoo Choi 				}
1188f68a8342SChanwoo Choi 				kfree(edev->d_attrs_muex);
1189f68a8342SChanwoo Choi 				kfree(edev->attrs_muex);
1190f68a8342SChanwoo Choi 				ret = -ENOMEM;
1191f68a8342SChanwoo Choi 				goto err_muex;
1192f68a8342SChanwoo Choi 			}
1193f68a8342SChanwoo Choi 			sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1194f68a8342SChanwoo Choi 			edev->d_attrs_muex[index].attr.name = name;
1195f68a8342SChanwoo Choi 			edev->d_attrs_muex[index].attr.mode = 0000;
1196f68a8342SChanwoo Choi 			edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1197f68a8342SChanwoo Choi 							.attr;
1198f68a8342SChanwoo Choi 		}
1199f68a8342SChanwoo Choi 		edev->attr_g_muex.name = muex_name;
1200f68a8342SChanwoo Choi 		edev->attr_g_muex.attrs = edev->attrs_muex;
1201f68a8342SChanwoo Choi 
1202f68a8342SChanwoo Choi 	}
1203f68a8342SChanwoo Choi 
1204f68a8342SChanwoo Choi 	if (edev->max_supported) {
1205f68a8342SChanwoo Choi 		edev->extcon_dev_type.groups =
12066396bb22SKees Cook 			kcalloc(edev->max_supported + 2,
12076396bb22SKees Cook 				sizeof(struct attribute_group *),
12086396bb22SKees Cook 				GFP_KERNEL);
1209f68a8342SChanwoo Choi 		if (!edev->extcon_dev_type.groups) {
1210f68a8342SChanwoo Choi 			ret = -ENOMEM;
1211f68a8342SChanwoo Choi 			goto err_alloc_groups;
1212f68a8342SChanwoo Choi 		}
1213f68a8342SChanwoo Choi 
1214f68a8342SChanwoo Choi 		edev->extcon_dev_type.name = dev_name(&edev->dev);
1215f68a8342SChanwoo Choi 		edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1216f68a8342SChanwoo Choi 
1217f68a8342SChanwoo Choi 		for (index = 0; index < edev->max_supported; index++)
1218f68a8342SChanwoo Choi 			edev->extcon_dev_type.groups[index] =
1219f68a8342SChanwoo Choi 				&edev->cables[index].attr_g;
1220f68a8342SChanwoo Choi 		if (edev->mutually_exclusive)
1221f68a8342SChanwoo Choi 			edev->extcon_dev_type.groups[index] =
1222f68a8342SChanwoo Choi 				&edev->attr_g_muex;
1223f68a8342SChanwoo Choi 
1224f68a8342SChanwoo Choi 		edev->dev.type = &edev->extcon_dev_type;
1225f68a8342SChanwoo Choi 	}
1226f68a8342SChanwoo Choi 
1227f68a8342SChanwoo Choi 	spin_lock_init(&edev->lock);
1228*5dcc2afeSbumwoo lee 	if (edev->max_supported) {
1229*5dcc2afeSbumwoo lee 		edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
1230*5dcc2afeSbumwoo lee 				GFP_KERNEL);
1231046050f6SChanwoo Choi 		if (!edev->nh) {
1232046050f6SChanwoo Choi 			ret = -ENOMEM;
1233*5dcc2afeSbumwoo lee 			goto err_alloc_nh;
1234*5dcc2afeSbumwoo lee 		}
1235046050f6SChanwoo Choi 	}
1236046050f6SChanwoo Choi 
1237046050f6SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1238046050f6SChanwoo Choi 		RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1239f68a8342SChanwoo Choi 
1240815429b3SChanwoo Choi 	RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1241815429b3SChanwoo Choi 
1242f68a8342SChanwoo Choi 	dev_set_drvdata(&edev->dev, edev);
1243f68a8342SChanwoo Choi 	edev->state = 0;
1244f68a8342SChanwoo Choi 
1245*5dcc2afeSbumwoo lee 	ret = device_register(&edev->dev);
1246*5dcc2afeSbumwoo lee 	if (ret) {
1247*5dcc2afeSbumwoo lee 		put_device(&edev->dev);
1248*5dcc2afeSbumwoo lee 		goto err_dev;
1249*5dcc2afeSbumwoo lee 	}
1250*5dcc2afeSbumwoo lee 
1251f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1252f68a8342SChanwoo Choi 	list_add(&edev->entry, &extcon_dev_list);
1253f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1254f68a8342SChanwoo Choi 
1255f68a8342SChanwoo Choi 	return 0;
1256f68a8342SChanwoo Choi 
1257f68a8342SChanwoo Choi err_dev:
1258f68a8342SChanwoo Choi 	if (edev->max_supported)
1259*5dcc2afeSbumwoo lee 		kfree(edev->nh);
1260*5dcc2afeSbumwoo lee err_alloc_nh:
1261*5dcc2afeSbumwoo lee 	if (edev->max_supported)
1262f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1263f68a8342SChanwoo Choi err_alloc_groups:
1264f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
1265f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
1266f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1267f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1268f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1269f68a8342SChanwoo Choi 	}
1270f68a8342SChanwoo Choi err_muex:
1271f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1272f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1273f68a8342SChanwoo Choi err_alloc_cables:
1274f68a8342SChanwoo Choi 	if (edev->max_supported)
1275f68a8342SChanwoo Choi 		kfree(edev->cables);
1276f68a8342SChanwoo Choi err_sysfs_alloc:
1277f68a8342SChanwoo Choi 	return ret;
1278f68a8342SChanwoo Choi }
1279f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register);
1280f68a8342SChanwoo Choi 
1281f68a8342SChanwoo Choi /**
1282f68a8342SChanwoo Choi  * extcon_dev_unregister() - Unregister the extcon device.
12836ab6094fSChanwoo Choi  * @edev:	the extcon device to be unregistered.
1284f68a8342SChanwoo Choi  *
1285f68a8342SChanwoo Choi  * Note that this does not call kfree(edev) because edev was not allocated
1286f68a8342SChanwoo Choi  * by this class.
1287f68a8342SChanwoo Choi  */
1288f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev)
1289f68a8342SChanwoo Choi {
1290f68a8342SChanwoo Choi 	int index;
1291f68a8342SChanwoo Choi 
12927eae43aeSChanwoo Choi 	if (!edev)
12937eae43aeSChanwoo Choi 		return;
12947eae43aeSChanwoo Choi 
1295f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1296f68a8342SChanwoo Choi 	list_del(&edev->entry);
1297f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1298f68a8342SChanwoo Choi 
1299f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1300f68a8342SChanwoo Choi 		dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1301f68a8342SChanwoo Choi 				dev_name(&edev->dev));
1302f68a8342SChanwoo Choi 		return;
1303f68a8342SChanwoo Choi 	}
1304f68a8342SChanwoo Choi 
1305f68a8342SChanwoo Choi 	device_unregister(&edev->dev);
1306f68a8342SChanwoo Choi 
1307f68a8342SChanwoo Choi 	if (edev->mutually_exclusive && edev->max_supported) {
1308f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index];
1309f68a8342SChanwoo Choi 				index++)
1310f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1311f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1312f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1313f68a8342SChanwoo Choi 	}
1314f68a8342SChanwoo Choi 
1315f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1316f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1317f68a8342SChanwoo Choi 
1318f68a8342SChanwoo Choi 	if (edev->max_supported) {
1319f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1320f68a8342SChanwoo Choi 		kfree(edev->cables);
1321*5dcc2afeSbumwoo lee 		kfree(edev->nh);
1322f68a8342SChanwoo Choi 	}
1323f68a8342SChanwoo Choi 
1324f68a8342SChanwoo Choi 	put_device(&edev->dev);
1325f68a8342SChanwoo Choi }
1326f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1327f68a8342SChanwoo Choi 
1328f68a8342SChanwoo Choi #ifdef CONFIG_OF
1329370ed7a9SAndrzej Hajda 
1330370ed7a9SAndrzej Hajda /*
1331370ed7a9SAndrzej Hajda  * extcon_find_edev_by_node - Find the extcon device from devicetree.
1332370ed7a9SAndrzej Hajda  * @node	: OF node identifying edev
1333370ed7a9SAndrzej Hajda  *
1334370ed7a9SAndrzej Hajda  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1335370ed7a9SAndrzej Hajda  */
1336370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1337370ed7a9SAndrzej Hajda {
1338370ed7a9SAndrzej Hajda 	struct extcon_dev *edev;
1339370ed7a9SAndrzej Hajda 
1340370ed7a9SAndrzej Hajda 	mutex_lock(&extcon_dev_list_lock);
1341370ed7a9SAndrzej Hajda 	list_for_each_entry(edev, &extcon_dev_list, entry)
1342370ed7a9SAndrzej Hajda 		if (edev->dev.parent && edev->dev.parent->of_node == node)
1343370ed7a9SAndrzej Hajda 			goto out;
1344370ed7a9SAndrzej Hajda 	edev = ERR_PTR(-EPROBE_DEFER);
1345370ed7a9SAndrzej Hajda out:
1346370ed7a9SAndrzej Hajda 	mutex_unlock(&extcon_dev_list_lock);
1347370ed7a9SAndrzej Hajda 
1348370ed7a9SAndrzej Hajda 	return edev;
1349370ed7a9SAndrzej Hajda }
1350370ed7a9SAndrzej Hajda 
1351f68a8342SChanwoo Choi /*
13526ab6094fSChanwoo Choi  * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
13536ab6094fSChanwoo Choi  * @dev		: the instance to the given device
13546ab6094fSChanwoo Choi  * @index	: the index into list of extcon_dev
1355f68a8342SChanwoo Choi  *
13566ab6094fSChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1357f68a8342SChanwoo Choi  */
1358f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1359f68a8342SChanwoo Choi {
1360f68a8342SChanwoo Choi 	struct device_node *node;
1361f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1362f68a8342SChanwoo Choi 
13637eae43aeSChanwoo Choi 	if (!dev)
13647eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
13657eae43aeSChanwoo Choi 
1366f68a8342SChanwoo Choi 	if (!dev->of_node) {
1367e8752b7aSStephen Boyd 		dev_dbg(dev, "device does not have a device node entry\n");
1368f68a8342SChanwoo Choi 		return ERR_PTR(-EINVAL);
1369f68a8342SChanwoo Choi 	}
1370f68a8342SChanwoo Choi 
1371f68a8342SChanwoo Choi 	node = of_parse_phandle(dev->of_node, "extcon", index);
1372f68a8342SChanwoo Choi 	if (!node) {
13735c27036dSRob Herring 		dev_dbg(dev, "failed to get phandle in %pOF node\n",
13745c27036dSRob Herring 			dev->of_node);
1375f68a8342SChanwoo Choi 		return ERR_PTR(-ENODEV);
1376f68a8342SChanwoo Choi 	}
1377f68a8342SChanwoo Choi 
1378370ed7a9SAndrzej Hajda 	edev = extcon_find_edev_by_node(node);
13795d5c4c13SPeter Chen 	of_node_put(node);
1380f68a8342SChanwoo Choi 
1381370ed7a9SAndrzej Hajda 	return edev;
1382f68a8342SChanwoo Choi }
1383370ed7a9SAndrzej Hajda 
1384f68a8342SChanwoo Choi #else
1385370ed7a9SAndrzej Hajda 
1386370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1387370ed7a9SAndrzej Hajda {
1388370ed7a9SAndrzej Hajda 	return ERR_PTR(-ENOSYS);
1389370ed7a9SAndrzej Hajda }
1390370ed7a9SAndrzej Hajda 
1391f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1392f68a8342SChanwoo Choi {
1393f68a8342SChanwoo Choi 	return ERR_PTR(-ENOSYS);
1394f68a8342SChanwoo Choi }
1395370ed7a9SAndrzej Hajda 
1396f68a8342SChanwoo Choi #endif /* CONFIG_OF */
1397370ed7a9SAndrzej Hajda 
1398370ed7a9SAndrzej Hajda EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
1399f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1400f68a8342SChanwoo Choi 
1401707d7550SChanwoo Choi /**
1402707d7550SChanwoo Choi  * extcon_get_edev_name() - Get the name of the extcon device.
1403707d7550SChanwoo Choi  * @edev:	the extcon device
1404707d7550SChanwoo Choi  */
1405707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev)
1406707d7550SChanwoo Choi {
1407707d7550SChanwoo Choi 	return !edev ? NULL : edev->name;
1408707d7550SChanwoo Choi }
1409995bb109SMayank Rana EXPORT_SYMBOL_GPL(extcon_get_edev_name);
1410707d7550SChanwoo Choi 
1411f68a8342SChanwoo Choi static int __init extcon_class_init(void)
1412f68a8342SChanwoo Choi {
1413f68a8342SChanwoo Choi 	return create_extcon_class();
1414f68a8342SChanwoo Choi }
1415f68a8342SChanwoo Choi module_init(extcon_class_init);
1416f68a8342SChanwoo Choi 
1417f68a8342SChanwoo Choi static void __exit extcon_class_exit(void)
1418f68a8342SChanwoo Choi {
1419f68a8342SChanwoo Choi 	class_destroy(extcon_class);
1420f68a8342SChanwoo Choi }
1421f68a8342SChanwoo Choi module_exit(extcon_class_exit);
1422f68a8342SChanwoo Choi 
14232a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <[email protected]>");
1424f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <[email protected]>");
14256ab6094fSChanwoo Choi MODULE_DESCRIPTION("External Connector (extcon) framework");
14266ab6094fSChanwoo Choi MODULE_LICENSE("GPL v2");
1427