xref: /openbmc/linux/drivers/extcon/extcon.c (revision 04151575)
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 <cw00.choi@samsung.com>
72a9de9c0SChanwoo Choi  *
8f68a8342SChanwoo Choi  * Copyright (C) 2012 Samsung Electronics
9f68a8342SChanwoo Choi  * Author: Donggeun Kim <dg77.kim@samsung.com>
10f68a8342SChanwoo Choi  * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
11f68a8342SChanwoo Choi  *
12f68a8342SChanwoo Choi  * based on android/drivers/switch/switch_class.c
13f68a8342SChanwoo Choi  * Copyright (C) 2008 Google, Inc.
14f68a8342SChanwoo Choi  * Author: Mike Lockwood <lockwood@android.com>
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 	},
1703a06ed80SMichael Wu 	[EXTCON_DISP_CVBS] = {
1713a06ed80SMichael Wu 		.type = EXTCON_TYPE_DISP,
1723a06ed80SMichael Wu 		.id = EXTCON_DISP_CVBS,
1733a06ed80SMichael Wu 		.name = "CVBS",
1743a06ed80SMichael Wu 	},
1753a06ed80SMichael Wu 	[EXTCON_DISP_EDP] = {
1763a06ed80SMichael Wu 		.type = EXTCON_TYPE_DISP,
1773a06ed80SMichael Wu 		.id = EXTCON_DISP_EDP,
1783a06ed80SMichael Wu 		.name = "EDP",
1793a06ed80SMichael Wu 	},
1808e9bc36dSChanwoo Choi 
18111eecf91SChanwoo Choi 	/* Miscellaneous external connector */
18255e4e2f1SChanwoo Choi 	[EXTCON_DOCK] = {
18355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
18455e4e2f1SChanwoo Choi 		.id = EXTCON_DOCK,
18555e4e2f1SChanwoo Choi 		.name = "DOCK",
18655e4e2f1SChanwoo Choi 	},
18755e4e2f1SChanwoo Choi 	[EXTCON_JIG] = {
18855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
18955e4e2f1SChanwoo Choi 		.id = EXTCON_JIG,
19055e4e2f1SChanwoo Choi 		.name = "JIG",
19155e4e2f1SChanwoo Choi 	},
19255e4e2f1SChanwoo Choi 	[EXTCON_MECHANICAL] = {
19355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
19455e4e2f1SChanwoo Choi 		.id = EXTCON_MECHANICAL,
19555e4e2f1SChanwoo Choi 		.name = "MECHANICAL",
19655e4e2f1SChanwoo Choi 	},
1978e9bc36dSChanwoo Choi 
19855e4e2f1SChanwoo Choi 	{ /* sentinel */ }
199f68a8342SChanwoo Choi };
200f68a8342SChanwoo Choi 
20120f7b53dSChanwoo Choi /**
2026ab6094fSChanwoo Choi  * struct extcon_cable - An internal data for an external connector.
2036ab6094fSChanwoo Choi  * @edev:		the extcon device
2046ab6094fSChanwoo Choi  * @cable_index:	the index of this cable in the edev
2056ab6094fSChanwoo Choi  * @attr_g:		the attribute group for the cable
20620f7b53dSChanwoo Choi  * @attr_name:		"name" sysfs entry
20720f7b53dSChanwoo Choi  * @attr_state:		"state" sysfs entry
2086ab6094fSChanwoo Choi  * @attrs:		the array pointing to attr_name and attr_state for attr_g
20920f7b53dSChanwoo Choi  */
21020f7b53dSChanwoo Choi struct extcon_cable {
21120f7b53dSChanwoo Choi 	struct extcon_dev *edev;
21220f7b53dSChanwoo Choi 	int cable_index;
21320f7b53dSChanwoo Choi 
21420f7b53dSChanwoo Choi 	struct attribute_group attr_g;
21520f7b53dSChanwoo Choi 	struct device_attribute attr_name;
21620f7b53dSChanwoo Choi 	struct device_attribute attr_state;
21720f7b53dSChanwoo Choi 
21820f7b53dSChanwoo Choi 	struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
219792e7e9eSChanwoo Choi 
220792e7e9eSChanwoo Choi 	union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
221792e7e9eSChanwoo Choi 	union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
222792e7e9eSChanwoo Choi 	union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
223792e7e9eSChanwoo Choi 	union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
2247f2a0a16SChanwoo Choi 
2257f2a0a16SChanwoo Choi 	unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
2267f2a0a16SChanwoo Choi 	unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
2277f2a0a16SChanwoo Choi 	unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
2287f2a0a16SChanwoo Choi 	unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
22920f7b53dSChanwoo Choi };
23020f7b53dSChanwoo Choi 
231f68a8342SChanwoo Choi static struct class *extcon_class;
232f68a8342SChanwoo Choi 
233f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list);
234f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock);
235f68a8342SChanwoo Choi 
236f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
237f68a8342SChanwoo Choi {
238f68a8342SChanwoo Choi 	int i = 0;
239f68a8342SChanwoo Choi 
240f68a8342SChanwoo Choi 	if (!edev->mutually_exclusive)
241f68a8342SChanwoo Choi 		return 0;
242f68a8342SChanwoo Choi 
243f68a8342SChanwoo Choi 	for (i = 0; edev->mutually_exclusive[i]; i++) {
244f68a8342SChanwoo Choi 		int weight;
245f68a8342SChanwoo Choi 		u32 correspondants = new_state & edev->mutually_exclusive[i];
246f68a8342SChanwoo Choi 
247f68a8342SChanwoo Choi 		/* calculate the total number of bits set */
248f68a8342SChanwoo Choi 		weight = hweight32(correspondants);
249f68a8342SChanwoo Choi 		if (weight > 1)
250f68a8342SChanwoo Choi 			return i + 1;
251f68a8342SChanwoo Choi 	}
252f68a8342SChanwoo Choi 
253f68a8342SChanwoo Choi 	return 0;
254f68a8342SChanwoo Choi }
255f68a8342SChanwoo Choi 
25673b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
2572a9de9c0SChanwoo Choi {
2582a9de9c0SChanwoo Choi 	int i;
2592a9de9c0SChanwoo Choi 
26097e1bb93SJiang Jian 	/* Find the index of extcon cable in edev->supported_cable */
2612a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
2622a9de9c0SChanwoo Choi 		if (edev->supported_cable[i] == id)
2632a9de9c0SChanwoo Choi 			return i;
2642a9de9c0SChanwoo Choi 	}
2652a9de9c0SChanwoo Choi 
2662a9de9c0SChanwoo Choi 	return -EINVAL;
2672a9de9c0SChanwoo Choi }
2682a9de9c0SChanwoo Choi 
269792e7e9eSChanwoo Choi static int get_extcon_type(unsigned int prop)
270792e7e9eSChanwoo Choi {
271792e7e9eSChanwoo Choi 	switch (prop) {
272792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
273792e7e9eSChanwoo Choi 		return EXTCON_TYPE_USB;
274792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
275792e7e9eSChanwoo Choi 		return EXTCON_TYPE_CHG;
276792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
277792e7e9eSChanwoo Choi 		return EXTCON_TYPE_JACK;
278792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
279792e7e9eSChanwoo Choi 		return EXTCON_TYPE_DISP;
280792e7e9eSChanwoo Choi 	default:
281792e7e9eSChanwoo Choi 		return -EINVAL;
282792e7e9eSChanwoo Choi 	}
283792e7e9eSChanwoo Choi }
284792e7e9eSChanwoo Choi 
285792e7e9eSChanwoo Choi static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
286792e7e9eSChanwoo Choi {
287792e7e9eSChanwoo Choi 	return !!(edev->state & BIT(index));
288792e7e9eSChanwoo Choi }
289792e7e9eSChanwoo Choi 
290ab11af04SChanwoo Choi static bool is_extcon_changed(struct extcon_dev *edev, int index,
291ab11af04SChanwoo Choi 				bool new_state)
292046050f6SChanwoo Choi {
293ab11af04SChanwoo Choi 	int state = !!(edev->state & BIT(index));
294ab11af04SChanwoo Choi 	return (state != new_state);
295046050f6SChanwoo Choi }
296046050f6SChanwoo Choi 
297792e7e9eSChanwoo Choi static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
298792e7e9eSChanwoo Choi {
299792e7e9eSChanwoo Choi 	int type;
300792e7e9eSChanwoo Choi 
301792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not. */
302792e7e9eSChanwoo Choi 	type = get_extcon_type(prop);
303792e7e9eSChanwoo Choi 	if (type < 0)
304792e7e9eSChanwoo Choi 		return false;
305792e7e9eSChanwoo Choi 
306792e7e9eSChanwoo Choi 	/* Check whether a specific extcon id supports the property or not. */
307792e7e9eSChanwoo Choi 	return !!(extcon_info[id].type & type);
308792e7e9eSChanwoo Choi }
309792e7e9eSChanwoo Choi 
3107f2a0a16SChanwoo Choi static int is_extcon_property_capability(struct extcon_dev *edev,
3117f2a0a16SChanwoo Choi 				unsigned int id, int index,unsigned int prop)
3127f2a0a16SChanwoo Choi {
3137f2a0a16SChanwoo Choi 	struct extcon_cable *cable;
3147f2a0a16SChanwoo Choi 	int type, ret;
3157f2a0a16SChanwoo Choi 
3167f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not. */
3177f2a0a16SChanwoo Choi 	type = get_extcon_type(prop);
3187f2a0a16SChanwoo Choi 	if (type < 0)
3197f2a0a16SChanwoo Choi 		return type;
3207f2a0a16SChanwoo Choi 
3217f2a0a16SChanwoo Choi 	cable = &edev->cables[index];
3227f2a0a16SChanwoo Choi 
3237f2a0a16SChanwoo Choi 	switch (type) {
3247f2a0a16SChanwoo Choi 	case EXTCON_TYPE_USB:
3257f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
3267f2a0a16SChanwoo Choi 		break;
3277f2a0a16SChanwoo Choi 	case EXTCON_TYPE_CHG:
3287f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
3297f2a0a16SChanwoo Choi 		break;
3307f2a0a16SChanwoo Choi 	case EXTCON_TYPE_JACK:
3317f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
3327f2a0a16SChanwoo Choi 		break;
3337f2a0a16SChanwoo Choi 	case EXTCON_TYPE_DISP:
3347f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
3357f2a0a16SChanwoo Choi 		break;
3367f2a0a16SChanwoo Choi 	default:
3377f2a0a16SChanwoo Choi 		ret = -EINVAL;
3387f2a0a16SChanwoo Choi 	}
3397f2a0a16SChanwoo Choi 
3407f2a0a16SChanwoo Choi 	return ret;
3417f2a0a16SChanwoo Choi }
3427f2a0a16SChanwoo Choi 
343792e7e9eSChanwoo Choi static void init_property(struct extcon_dev *edev, unsigned int id, int index)
344792e7e9eSChanwoo Choi {
345792e7e9eSChanwoo Choi 	unsigned int type = extcon_info[id].type;
346792e7e9eSChanwoo Choi 	struct extcon_cable *cable = &edev->cables[index];
347792e7e9eSChanwoo Choi 
348792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_USB & type)
349792e7e9eSChanwoo Choi 		memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
350792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_CHG & type)
351792e7e9eSChanwoo Choi 		memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
352792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_JACK & type)
353792e7e9eSChanwoo Choi 		memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
354792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_DISP & type)
355792e7e9eSChanwoo Choi 		memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
356792e7e9eSChanwoo Choi }
357792e7e9eSChanwoo Choi 
358f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr,
359f68a8342SChanwoo Choi 			  char *buf)
360f68a8342SChanwoo Choi {
361f68a8342SChanwoo Choi 	int i, count = 0;
362f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
363f68a8342SChanwoo Choi 
364f68a8342SChanwoo Choi 	if (edev->max_supported == 0)
365f68a8342SChanwoo Choi 		return sprintf(buf, "%u\n", edev->state);
366f68a8342SChanwoo Choi 
3672a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
368f68a8342SChanwoo Choi 		count += sprintf(buf + count, "%s=%d\n",
36955e4e2f1SChanwoo Choi 				extcon_info[edev->supported_cable[i]].name,
37070641a0aSChanwoo Choi 				 !!(edev->state & BIT(i)));
371f68a8342SChanwoo Choi 	}
372f68a8342SChanwoo Choi 
373f68a8342SChanwoo Choi 	return count;
374f68a8342SChanwoo Choi }
3755d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state);
376f68a8342SChanwoo Choi 
377f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr,
378f68a8342SChanwoo Choi 		char *buf)
379f68a8342SChanwoo Choi {
380f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
381f68a8342SChanwoo Choi 
38271c3ffa5SChanwoo Choi 	return sprintf(buf, "%s\n", edev->name);
383f68a8342SChanwoo Choi }
384f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name);
385f68a8342SChanwoo Choi 
386f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev,
387f68a8342SChanwoo Choi 			       struct device_attribute *attr, char *buf)
388f68a8342SChanwoo Choi {
389f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
390f68a8342SChanwoo Choi 						  attr_name);
3912a9de9c0SChanwoo Choi 	int i = cable->cable_index;
392f68a8342SChanwoo Choi 
393f68a8342SChanwoo Choi 	return sprintf(buf, "%s\n",
39455e4e2f1SChanwoo Choi 			extcon_info[cable->edev->supported_cable[i]].name);
395f68a8342SChanwoo Choi }
396f68a8342SChanwoo Choi 
397f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev,
398f68a8342SChanwoo Choi 				struct device_attribute *attr, char *buf)
399f68a8342SChanwoo Choi {
400f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
401f68a8342SChanwoo Choi 						  attr_state);
402f68a8342SChanwoo Choi 
403be052cc8SRoger Quadros 	int i = cable->cable_index;
404be052cc8SRoger Quadros 
405f68a8342SChanwoo Choi 	return sprintf(buf, "%d\n",
406575c2b86SChanwoo Choi 		extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
407f68a8342SChanwoo Choi }
408f68a8342SChanwoo Choi 
409f68a8342SChanwoo Choi /**
4106ab6094fSChanwoo Choi  * extcon_sync() - Synchronize the state for an external connector.
4116ab6094fSChanwoo Choi  * @edev:	the extcon device
4126506f6a0SYang Li  * @id:		the unique id indicating an external connector
413f68a8342SChanwoo Choi  *
4146ab6094fSChanwoo Choi  * Note that this function send a notification in order to synchronize
4156ab6094fSChanwoo Choi  * the state and property of an external connector.
4166ab6094fSChanwoo Choi  *
4176ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
418f68a8342SChanwoo Choi  */
419ab11af04SChanwoo Choi int extcon_sync(struct extcon_dev *edev, unsigned int id)
420f68a8342SChanwoo Choi {
421f68a8342SChanwoo Choi 	char name_buf[120];
422f68a8342SChanwoo Choi 	char state_buf[120];
423f68a8342SChanwoo Choi 	char *prop_buf;
424f68a8342SChanwoo Choi 	char *envp[3];
425f68a8342SChanwoo Choi 	int env_offset = 0;
426f68a8342SChanwoo Choi 	int length;
427046050f6SChanwoo Choi 	int index;
428ab11af04SChanwoo Choi 	int state;
429f68a8342SChanwoo Choi 	unsigned long flags;
430f68a8342SChanwoo Choi 
4317eae43aeSChanwoo Choi 	if (!edev)
4327eae43aeSChanwoo Choi 		return -EINVAL;
4337eae43aeSChanwoo Choi 
434ab11af04SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
435ab11af04SChanwoo Choi 	if (index < 0)
436ab11af04SChanwoo Choi 		return index;
437ab11af04SChanwoo Choi 
438f68a8342SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
439ab11af04SChanwoo Choi 	state = !!(edev->state & BIT(index));
4408a9dbb77SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
441815429b3SChanwoo Choi 
442815429b3SChanwoo Choi 	/*
443815429b3SChanwoo Choi 	 * Call functions in a raw notifier chain for the specific one
444815429b3SChanwoo Choi 	 * external connector.
445815429b3SChanwoo Choi 	 */
446ab11af04SChanwoo Choi 	raw_notifier_call_chain(&edev->nh[index], state, edev);
447f7a89811SRoger Quadros 
448815429b3SChanwoo Choi 	/*
449815429b3SChanwoo Choi 	 * Call functions in a raw notifier chain for the all supported
450815429b3SChanwoo Choi 	 * external connectors.
451815429b3SChanwoo Choi 	 */
452815429b3SChanwoo Choi 	raw_notifier_call_chain(&edev->nh_all, state, edev);
453815429b3SChanwoo Choi 
4548a9dbb77SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
455f68a8342SChanwoo Choi 	/* This could be in interrupt handler */
456f68a8342SChanwoo Choi 	prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
457ab11af04SChanwoo Choi 	if (!prop_buf) {
458f68a8342SChanwoo Choi 		/* Unlock early before uevent */
459f68a8342SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
460f68a8342SChanwoo Choi 
461f68a8342SChanwoo Choi 		dev_err(&edev->dev, "out of memory in extcon_set_state\n");
462f68a8342SChanwoo Choi 		kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
463f68a8342SChanwoo Choi 
464e7d9dd5aSPan Bian 		return -ENOMEM;
465f68a8342SChanwoo Choi 	}
466f68a8342SChanwoo Choi 
467ab11af04SChanwoo Choi 	length = name_show(&edev->dev, NULL, prop_buf);
468ab11af04SChanwoo Choi 	if (length > 0) {
469ab11af04SChanwoo Choi 		if (prop_buf[length - 1] == '\n')
470ab11af04SChanwoo Choi 			prop_buf[length - 1] = 0;
471ab11af04SChanwoo Choi 		snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
472ab11af04SChanwoo Choi 		envp[env_offset++] = name_buf;
473ab11af04SChanwoo Choi 	}
474ab11af04SChanwoo Choi 
475ab11af04SChanwoo Choi 	length = state_show(&edev->dev, NULL, prop_buf);
476ab11af04SChanwoo Choi 	if (length > 0) {
477ab11af04SChanwoo Choi 		if (prop_buf[length - 1] == '\n')
478ab11af04SChanwoo Choi 			prop_buf[length - 1] = 0;
479ab11af04SChanwoo Choi 		snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
480ab11af04SChanwoo Choi 		envp[env_offset++] = state_buf;
481ab11af04SChanwoo Choi 	}
482ab11af04SChanwoo Choi 	envp[env_offset] = NULL;
483ab11af04SChanwoo Choi 
484ab11af04SChanwoo Choi 	/* Unlock early before uevent */
485ab11af04SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
486ab11af04SChanwoo Choi 	kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
487ab11af04SChanwoo Choi 	free_page((unsigned long)prop_buf);
488ab11af04SChanwoo Choi 
489ab11af04SChanwoo Choi 	return 0;
490ab11af04SChanwoo Choi }
491ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_sync);
492ab11af04SChanwoo Choi 
493f68a8342SChanwoo Choi /**
4946ab6094fSChanwoo Choi  * extcon_get_state() - Get the state of an external connector.
4956ab6094fSChanwoo Choi  * @edev:	the extcon device
4966ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
4976ab6094fSChanwoo Choi  *
4986ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
499f68a8342SChanwoo Choi  */
500575c2b86SChanwoo Choi int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
501f68a8342SChanwoo Choi {
502575c2b86SChanwoo Choi 	int index, state;
503575c2b86SChanwoo Choi 	unsigned long flags;
5042a9de9c0SChanwoo Choi 
5057eae43aeSChanwoo Choi 	if (!edev)
5067eae43aeSChanwoo Choi 		return -EINVAL;
5077eae43aeSChanwoo Choi 
5082a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
5092a9de9c0SChanwoo Choi 	if (index < 0)
5102a9de9c0SChanwoo Choi 		return index;
5112a9de9c0SChanwoo Choi 
512575c2b86SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
513575c2b86SChanwoo Choi 	state = is_extcon_attached(edev, index);
514575c2b86SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
515f68a8342SChanwoo Choi 
516575c2b86SChanwoo Choi 	return state;
517f68a8342SChanwoo Choi }
518575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_state);
519f68a8342SChanwoo Choi 
520f68a8342SChanwoo Choi /**
5216ab6094fSChanwoo Choi  * extcon_set_state() - Set the state of an external connector.
5226ab6094fSChanwoo Choi  * @edev:	the extcon device
5236ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5246ab6094fSChanwoo Choi  * @state:	the new state of an external connector.
5256ab6094fSChanwoo Choi  *		the default semantics is true: attached / false: detached.
526ab11af04SChanwoo Choi  *
5276ab6094fSChanwoo Choi  * Note that this function set the state of an external connector without
5286ab6094fSChanwoo Choi  * a notification. To synchronize the state of an external connector,
5296ab6094fSChanwoo Choi  * have to use extcon_set_state_sync() and extcon_sync().
5306ab6094fSChanwoo Choi  *
5316ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
532f68a8342SChanwoo Choi  */
5336ab6094fSChanwoo Choi int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
534f68a8342SChanwoo Choi {
535ab11af04SChanwoo Choi 	unsigned long flags;
536ab11af04SChanwoo Choi 	int index, ret = 0;
537f68a8342SChanwoo Choi 
5387eae43aeSChanwoo Choi 	if (!edev)
5397eae43aeSChanwoo Choi 		return -EINVAL;
5407eae43aeSChanwoo Choi 
5412a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
5422a9de9c0SChanwoo Choi 	if (index < 0)
5432a9de9c0SChanwoo Choi 		return index;
5442a9de9c0SChanwoo Choi 
545ab11af04SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
546ab11af04SChanwoo Choi 
547ab11af04SChanwoo Choi 	/* Check whether the external connector's state is changed. */
5486ab6094fSChanwoo Choi 	if (!is_extcon_changed(edev, index, state))
549ab11af04SChanwoo Choi 		goto out;
550ab11af04SChanwoo Choi 
551ab11af04SChanwoo Choi 	if (check_mutually_exclusive(edev,
5526ab6094fSChanwoo Choi 		(edev->state & ~BIT(index)) | (state & BIT(index)))) {
553ab11af04SChanwoo Choi 		ret = -EPERM;
554ab11af04SChanwoo Choi 		goto out;
555ab11af04SChanwoo Choi 	}
556ab11af04SChanwoo Choi 
557792e7e9eSChanwoo Choi 	/*
558792e7e9eSChanwoo Choi 	 * Initialize the value of extcon property before setting
559792e7e9eSChanwoo Choi 	 * the detached state for an external connector.
560792e7e9eSChanwoo Choi 	 */
5616ab6094fSChanwoo Choi 	if (!state)
562792e7e9eSChanwoo Choi 		init_property(edev, id, index);
563792e7e9eSChanwoo Choi 
5646ab6094fSChanwoo Choi 	/* Update the state for an external connector. */
5656ab6094fSChanwoo Choi 	if (state)
566ab11af04SChanwoo Choi 		edev->state |= BIT(index);
567ab11af04SChanwoo Choi 	else
568ab11af04SChanwoo Choi 		edev->state &= ~(BIT(index));
569ab11af04SChanwoo Choi out:
570ab11af04SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
571ab11af04SChanwoo Choi 
572ab11af04SChanwoo Choi 	return ret;
573f68a8342SChanwoo Choi }
574575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state);
575f68a8342SChanwoo Choi 
576f68a8342SChanwoo Choi /**
5776ab6094fSChanwoo Choi  * extcon_set_state_sync() - Set the state of an external connector with sync.
5786ab6094fSChanwoo Choi  * @edev:	the extcon device
5796ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5806ab6094fSChanwoo Choi  * @state:	the new state of external connector.
5816ab6094fSChanwoo Choi  *		the default semantics is true: attached / false: detached.
582ab11af04SChanwoo Choi  *
5836ab6094fSChanwoo Choi  * Note that this function set the state of external connector
5846ab6094fSChanwoo Choi  * and synchronize the state by sending a notification.
5856ab6094fSChanwoo Choi  *
5866ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
587ab11af04SChanwoo Choi  */
5886ab6094fSChanwoo Choi int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
589ab11af04SChanwoo Choi {
5902da3db7fSAlexander Stein 	int ret;
591ab11af04SChanwoo Choi 
5926ab6094fSChanwoo Choi 	ret = extcon_set_state(edev, id, state);
593ab11af04SChanwoo Choi 	if (ret < 0)
594ab11af04SChanwoo Choi 		return ret;
595ab11af04SChanwoo Choi 
596ab11af04SChanwoo Choi 	return extcon_sync(edev, id);
597ab11af04SChanwoo Choi }
598ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state_sync);
599ab11af04SChanwoo Choi 
600ab11af04SChanwoo Choi /**
6016ab6094fSChanwoo Choi  * extcon_get_property() - Get the property value of an external connector.
6026ab6094fSChanwoo Choi  * @edev:	the extcon device
6036ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
6046ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
6056ab6094fSChanwoo Choi  * @prop_val:	the pointer which store the value of extcon property
606792e7e9eSChanwoo Choi  *
6076ab6094fSChanwoo Choi  * Note that when getting the property value of external connector,
6086ab6094fSChanwoo Choi  * the external connector should be attached. If detached state, function
6096ab6094fSChanwoo Choi  * return 0 without property value. Also, the each property should be
6106ab6094fSChanwoo Choi  * included in the list of supported properties according to extcon type.
611792e7e9eSChanwoo Choi  *
6126ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
613792e7e9eSChanwoo Choi  */
614792e7e9eSChanwoo Choi int extcon_get_property(struct extcon_dev *edev, unsigned int id,
615792e7e9eSChanwoo Choi 				unsigned int prop,
616792e7e9eSChanwoo Choi 				union extcon_property_value *prop_val)
617792e7e9eSChanwoo Choi {
618792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
619792e7e9eSChanwoo Choi 	unsigned long flags;
620792e7e9eSChanwoo Choi 	int index, ret = 0;
621792e7e9eSChanwoo Choi 
622cff7499dSAndy Shevchenko 	*prop_val = (union extcon_property_value){0};
623792e7e9eSChanwoo Choi 
624792e7e9eSChanwoo Choi 	if (!edev)
625792e7e9eSChanwoo Choi 		return -EINVAL;
626792e7e9eSChanwoo Choi 
627792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
628792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
629792e7e9eSChanwoo Choi 		return -EINVAL;
630792e7e9eSChanwoo Choi 
631792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
632792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
633792e7e9eSChanwoo Choi 	if (index < 0)
634792e7e9eSChanwoo Choi 		return index;
635792e7e9eSChanwoo Choi 
636792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
637792e7e9eSChanwoo Choi 
6387f2a0a16SChanwoo Choi 	/* Check whether the property is available or not. */
6397f2a0a16SChanwoo Choi 	if (!is_extcon_property_capability(edev, id, index, prop)) {
6407f2a0a16SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
6417f2a0a16SChanwoo Choi 		return -EPERM;
6427f2a0a16SChanwoo Choi 	}
6437f2a0a16SChanwoo Choi 
644792e7e9eSChanwoo Choi 	/*
645792e7e9eSChanwoo Choi 	 * Check whether the external connector is attached.
646792e7e9eSChanwoo Choi 	 * If external connector is detached, the user can not
647792e7e9eSChanwoo Choi 	 * get the property value.
648792e7e9eSChanwoo Choi 	 */
649792e7e9eSChanwoo Choi 	if (!is_extcon_attached(edev, index)) {
650792e7e9eSChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
651792e7e9eSChanwoo Choi 		return 0;
652792e7e9eSChanwoo Choi 	}
653792e7e9eSChanwoo Choi 
654792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
655792e7e9eSChanwoo Choi 
656792e7e9eSChanwoo Choi 	/* Get the property value according to extcon type */
657792e7e9eSChanwoo Choi 	switch (prop) {
658792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
659792e7e9eSChanwoo Choi 		*prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
660792e7e9eSChanwoo Choi 		break;
661792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
662792e7e9eSChanwoo Choi 		*prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
663792e7e9eSChanwoo Choi 		break;
664792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
665792e7e9eSChanwoo Choi 		*prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
666792e7e9eSChanwoo Choi 		break;
667792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
668792e7e9eSChanwoo Choi 		*prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
669792e7e9eSChanwoo Choi 		break;
670792e7e9eSChanwoo Choi 	default:
671792e7e9eSChanwoo Choi 		ret = -EINVAL;
672792e7e9eSChanwoo Choi 		break;
673792e7e9eSChanwoo Choi 	}
674792e7e9eSChanwoo Choi 
675792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
676792e7e9eSChanwoo Choi 
677792e7e9eSChanwoo Choi 	return ret;
678792e7e9eSChanwoo Choi }
679792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property);
680792e7e9eSChanwoo Choi 
681792e7e9eSChanwoo Choi /**
6826ab6094fSChanwoo Choi  * extcon_set_property() - Set the property value of an external connector.
6836ab6094fSChanwoo Choi  * @edev:	the extcon device
6846ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
6856ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
6866ab6094fSChanwoo Choi  * @prop_val:	the pointer including the new value of extcon property
687792e7e9eSChanwoo Choi  *
6886ab6094fSChanwoo Choi  * Note that each property should be included in the list of supported
6896ab6094fSChanwoo Choi  * properties according to the extcon type.
690792e7e9eSChanwoo Choi  *
6916ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
692792e7e9eSChanwoo Choi  */
693792e7e9eSChanwoo Choi int extcon_set_property(struct extcon_dev *edev, unsigned int id,
694792e7e9eSChanwoo Choi 				unsigned int prop,
695792e7e9eSChanwoo Choi 				union extcon_property_value prop_val)
696792e7e9eSChanwoo Choi {
697792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
698792e7e9eSChanwoo Choi 	unsigned long flags;
699792e7e9eSChanwoo Choi 	int index, ret = 0;
700792e7e9eSChanwoo Choi 
701792e7e9eSChanwoo Choi 	if (!edev)
702792e7e9eSChanwoo Choi 		return -EINVAL;
703792e7e9eSChanwoo Choi 
704792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
705792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
706792e7e9eSChanwoo Choi 		return -EINVAL;
707792e7e9eSChanwoo Choi 
708792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
709792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
710792e7e9eSChanwoo Choi 	if (index < 0)
711792e7e9eSChanwoo Choi 		return index;
712792e7e9eSChanwoo Choi 
713792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
714792e7e9eSChanwoo Choi 
7157f2a0a16SChanwoo Choi 	/* Check whether the property is available or not. */
7167f2a0a16SChanwoo Choi 	if (!is_extcon_property_capability(edev, id, index, prop)) {
7177f2a0a16SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
7187f2a0a16SChanwoo Choi 		return -EPERM;
7197f2a0a16SChanwoo Choi 	}
7207f2a0a16SChanwoo Choi 
721792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
722792e7e9eSChanwoo Choi 
723792e7e9eSChanwoo Choi 	/* Set the property value according to extcon type */
724792e7e9eSChanwoo Choi 	switch (prop) {
725792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
726792e7e9eSChanwoo Choi 		cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
727792e7e9eSChanwoo Choi 		break;
728792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
729792e7e9eSChanwoo Choi 		cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
730792e7e9eSChanwoo Choi 		break;
731792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
732792e7e9eSChanwoo Choi 		cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
733792e7e9eSChanwoo Choi 		break;
734792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
735792e7e9eSChanwoo Choi 		cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
736792e7e9eSChanwoo Choi 		break;
737792e7e9eSChanwoo Choi 	default:
738792e7e9eSChanwoo Choi 		ret = -EINVAL;
739792e7e9eSChanwoo Choi 		break;
740792e7e9eSChanwoo Choi 	}
741792e7e9eSChanwoo Choi 
742792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
743792e7e9eSChanwoo Choi 
744792e7e9eSChanwoo Choi 	return ret;
745792e7e9eSChanwoo Choi }
746792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property);
747792e7e9eSChanwoo Choi 
748792e7e9eSChanwoo Choi /**
7496ab6094fSChanwoo Choi  * extcon_set_property_sync() - Set property of an external connector with sync.
7506506f6a0SYang Li  * @edev:	the extcon device
7516506f6a0SYang Li  * @id:		the unique id indicating an external connector
7526506f6a0SYang Li  * @prop:	the property id indicating an extcon property
7536ab6094fSChanwoo Choi  * @prop_val:	the pointer including the new value of extcon property
754ab11af04SChanwoo Choi  *
7556ab6094fSChanwoo Choi  * Note that when setting the property value of external connector,
7566ab6094fSChanwoo Choi  * the external connector should be attached. The each property should
7576ab6094fSChanwoo Choi  * be included in the list of supported properties according to extcon type.
758ab11af04SChanwoo Choi  *
7596ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
760ab11af04SChanwoo Choi  */
761ab11af04SChanwoo Choi int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
762ab11af04SChanwoo Choi 				unsigned int prop,
763ab11af04SChanwoo Choi 				union extcon_property_value prop_val)
764ab11af04SChanwoo Choi {
765ab11af04SChanwoo Choi 	int ret;
766ab11af04SChanwoo Choi 
767ab11af04SChanwoo Choi 	ret = extcon_set_property(edev, id, prop, prop_val);
768ab11af04SChanwoo Choi 	if (ret < 0)
769ab11af04SChanwoo Choi 		return ret;
770ab11af04SChanwoo Choi 
771ab11af04SChanwoo Choi 	return extcon_sync(edev, id);
772ab11af04SChanwoo Choi }
773ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_sync);
774ab11af04SChanwoo Choi 
775ab11af04SChanwoo Choi /**
7766ab6094fSChanwoo Choi  * extcon_get_property_capability() - Get the capability of the property
7776ab6094fSChanwoo Choi  *					for an external connector.
7786ab6094fSChanwoo Choi  * @edev:	the extcon device
7796ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
7806ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
7817f2a0a16SChanwoo Choi  *
7827f2a0a16SChanwoo Choi  * Returns 1 if the property is available or 0 if not available.
7837f2a0a16SChanwoo Choi  */
7847f2a0a16SChanwoo Choi int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
7857f2a0a16SChanwoo Choi 					unsigned int prop)
7867f2a0a16SChanwoo Choi {
7877f2a0a16SChanwoo Choi 	int index;
7887f2a0a16SChanwoo Choi 
7897f2a0a16SChanwoo Choi 	if (!edev)
7907f2a0a16SChanwoo Choi 		return -EINVAL;
7917f2a0a16SChanwoo Choi 
7927f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not */
7937f2a0a16SChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
7947f2a0a16SChanwoo Choi 		return -EINVAL;
7957f2a0a16SChanwoo Choi 
7967f2a0a16SChanwoo Choi 	/* Find the cable index of external connector by using id */
7977f2a0a16SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
7987f2a0a16SChanwoo Choi 	if (index < 0)
7997f2a0a16SChanwoo Choi 		return index;
8007f2a0a16SChanwoo Choi 
8017f2a0a16SChanwoo Choi 	return is_extcon_property_capability(edev, id, index, prop);
8027f2a0a16SChanwoo Choi }
8037f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property_capability);
8047f2a0a16SChanwoo Choi 
8057f2a0a16SChanwoo Choi /**
8066ab6094fSChanwoo Choi  * extcon_set_property_capability() - Set the capability of the property
8076ab6094fSChanwoo Choi  *					for an external connector.
8086ab6094fSChanwoo Choi  * @edev:	the extcon device
8096ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
8106ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
8117f2a0a16SChanwoo Choi  *
8126ab6094fSChanwoo Choi  * Note that this function set the capability of the property
8136ab6094fSChanwoo Choi  * for an external connector in order to mark the bit in capability
8146ab6094fSChanwoo Choi  * bitmap which mean the available state of the property.
8157f2a0a16SChanwoo Choi  *
8166ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
8177f2a0a16SChanwoo Choi  */
8187f2a0a16SChanwoo Choi int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
8197f2a0a16SChanwoo Choi 					unsigned int prop)
8207f2a0a16SChanwoo Choi {
8217f2a0a16SChanwoo Choi 	struct extcon_cable *cable;
8227f2a0a16SChanwoo Choi 	int index, type, ret = 0;
8237f2a0a16SChanwoo Choi 
8247f2a0a16SChanwoo Choi 	if (!edev)
8257f2a0a16SChanwoo Choi 		return -EINVAL;
8267f2a0a16SChanwoo Choi 
8277f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not. */
8287f2a0a16SChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
8297f2a0a16SChanwoo Choi 		return -EINVAL;
8307f2a0a16SChanwoo Choi 
8317f2a0a16SChanwoo Choi 	/* Find the cable index of external connector by using id. */
8327f2a0a16SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
8337f2a0a16SChanwoo Choi 	if (index < 0)
8347f2a0a16SChanwoo Choi 		return index;
8357f2a0a16SChanwoo Choi 
8367f2a0a16SChanwoo Choi 	type = get_extcon_type(prop);
8377f2a0a16SChanwoo Choi 	if (type < 0)
8387f2a0a16SChanwoo Choi 		return type;
8397f2a0a16SChanwoo Choi 
8407f2a0a16SChanwoo Choi 	cable = &edev->cables[index];
8417f2a0a16SChanwoo Choi 
8427f2a0a16SChanwoo Choi 	switch (type) {
8437f2a0a16SChanwoo Choi 	case EXTCON_TYPE_USB:
8447f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
8457f2a0a16SChanwoo Choi 		break;
8467f2a0a16SChanwoo Choi 	case EXTCON_TYPE_CHG:
8477f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
8487f2a0a16SChanwoo Choi 		break;
8497f2a0a16SChanwoo Choi 	case EXTCON_TYPE_JACK:
8507f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
8517f2a0a16SChanwoo Choi 		break;
8527f2a0a16SChanwoo Choi 	case EXTCON_TYPE_DISP:
8537f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
8547f2a0a16SChanwoo Choi 		break;
8557f2a0a16SChanwoo Choi 	default:
8567f2a0a16SChanwoo Choi 		ret = -EINVAL;
8577f2a0a16SChanwoo Choi 	}
8587f2a0a16SChanwoo Choi 
8597f2a0a16SChanwoo Choi 	return ret;
8607f2a0a16SChanwoo Choi }
8617f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_capability);
8627f2a0a16SChanwoo Choi 
8637f2a0a16SChanwoo Choi /**
8646ab6094fSChanwoo Choi  * extcon_get_extcon_dev() - Get the extcon device instance from the name.
8656ab6094fSChanwoo Choi  * @extcon_name:	the extcon name provided with extcon_dev_register()
8666ab6094fSChanwoo Choi  *
8676ab6094fSChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
86858e4a2d2SDan Carpenter  * NOTE: This function returns -EPROBE_DEFER so it may only be called from
86958e4a2d2SDan Carpenter  * probe() functions.
870f68a8342SChanwoo Choi  */
871f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
872f68a8342SChanwoo Choi {
873f68a8342SChanwoo Choi 	struct extcon_dev *sd;
874f68a8342SChanwoo Choi 
8757eae43aeSChanwoo Choi 	if (!extcon_name)
8767eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
8777eae43aeSChanwoo Choi 
878f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
879f68a8342SChanwoo Choi 	list_for_each_entry(sd, &extcon_dev_list, entry) {
880f68a8342SChanwoo Choi 		if (!strcmp(sd->name, extcon_name))
881f68a8342SChanwoo Choi 			goto out;
882f68a8342SChanwoo Choi 	}
88358e4a2d2SDan Carpenter 	sd = ERR_PTR(-EPROBE_DEFER);
884f68a8342SChanwoo Choi out:
885f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
886f68a8342SChanwoo Choi 	return sd;
887f68a8342SChanwoo Choi }
888f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
889f68a8342SChanwoo Choi 
890f68a8342SChanwoo Choi /**
8916ab6094fSChanwoo Choi  * extcon_register_notifier() - Register a notifier block to get notified by
8926ab6094fSChanwoo Choi  *				any state changes from the extcon.
8936ab6094fSChanwoo Choi  * @edev:	the extcon device
8946ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
8956ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
896f68a8342SChanwoo Choi  *
897f68a8342SChanwoo Choi  * Note that the second parameter given to the callback of nb (val) is
8986ab6094fSChanwoo Choi  * the current state of an external connector and the third pameter
8996ab6094fSChanwoo Choi  * is the pointer of extcon device.
9006ab6094fSChanwoo Choi  *
9016ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
902f68a8342SChanwoo Choi  */
90373b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
904f68a8342SChanwoo Choi 			     struct notifier_block *nb)
905f68a8342SChanwoo Choi {
90666bee35fSHans de Goede 	unsigned long flags;
9071fa80f18SColin Ian King 	int ret, idx;
908046050f6SChanwoo Choi 
90901b4c9a1SChanwoo Choi 	if (!edev || !nb)
9107eae43aeSChanwoo Choi 		return -EINVAL;
9117eae43aeSChanwoo Choi 
912046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
913a05f44c8SStephen Boyd 	if (idx < 0)
914a05f44c8SStephen Boyd 		return idx;
91566bee35fSHans de Goede 
91666bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
917046050f6SChanwoo Choi 	ret = raw_notifier_chain_register(&edev->nh[idx], nb);
91866bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
91966bee35fSHans de Goede 
92066bee35fSHans de Goede 	return ret;
921f68a8342SChanwoo Choi }
922f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier);
923f68a8342SChanwoo Choi 
924f68a8342SChanwoo Choi /**
9256ab6094fSChanwoo Choi  * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
9266ab6094fSChanwoo Choi  * @edev:	the extcon device
9276ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
9286ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
9296ab6094fSChanwoo Choi  *
9306ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
931f68a8342SChanwoo Choi  */
93273b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
933f68a8342SChanwoo Choi 				struct notifier_block *nb)
934f68a8342SChanwoo Choi {
93566bee35fSHans de Goede 	unsigned long flags;
936046050f6SChanwoo Choi 	int ret, idx;
937046050f6SChanwoo Choi 
9387eae43aeSChanwoo Choi 	if (!edev || !nb)
9397eae43aeSChanwoo Choi 		return -EINVAL;
9407eae43aeSChanwoo Choi 
941046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
942a05f44c8SStephen Boyd 	if (idx < 0)
943a05f44c8SStephen Boyd 		return idx;
94466bee35fSHans de Goede 
94566bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
946046050f6SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
94766bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
94866bee35fSHans de Goede 
94966bee35fSHans de Goede 	return ret;
950f68a8342SChanwoo Choi }
951f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
952f68a8342SChanwoo Choi 
953815429b3SChanwoo Choi /**
9546ab6094fSChanwoo Choi  * extcon_register_notifier_all() - Register a notifier block for all connectors.
9556ab6094fSChanwoo Choi  * @edev:	the extcon device
9566ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
957815429b3SChanwoo Choi  *
9586ab6094fSChanwoo Choi  * Note that this function registers a notifier block in order to receive
9596ab6094fSChanwoo Choi  * the state change of all supported external connectors from extcon device.
960826a47e9SMarkus Elfring  * And the second parameter given to the callback of nb (val) is
9616ab6094fSChanwoo Choi  * the current state and the third pameter is the pointer of extcon device.
962815429b3SChanwoo Choi  *
9636ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
964815429b3SChanwoo Choi  */
965815429b3SChanwoo Choi int extcon_register_notifier_all(struct extcon_dev *edev,
966815429b3SChanwoo Choi 				struct notifier_block *nb)
967815429b3SChanwoo Choi {
968815429b3SChanwoo Choi 	unsigned long flags;
969815429b3SChanwoo Choi 	int ret;
970815429b3SChanwoo Choi 
971815429b3SChanwoo Choi 	if (!edev || !nb)
972815429b3SChanwoo Choi 		return -EINVAL;
973815429b3SChanwoo Choi 
974815429b3SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
975815429b3SChanwoo Choi 	ret = raw_notifier_chain_register(&edev->nh_all, nb);
976815429b3SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
977815429b3SChanwoo Choi 
978815429b3SChanwoo Choi 	return ret;
979815429b3SChanwoo Choi }
980815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
981815429b3SChanwoo Choi 
982815429b3SChanwoo Choi /**
983815429b3SChanwoo Choi  * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
9846ab6094fSChanwoo Choi  * @edev:	the extcon device
9856ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
986815429b3SChanwoo Choi  *
9876ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
988815429b3SChanwoo Choi  */
989815429b3SChanwoo Choi int extcon_unregister_notifier_all(struct extcon_dev *edev,
990815429b3SChanwoo Choi 				struct notifier_block *nb)
991815429b3SChanwoo Choi {
992815429b3SChanwoo Choi 	unsigned long flags;
993815429b3SChanwoo Choi 	int ret;
994815429b3SChanwoo Choi 
995815429b3SChanwoo Choi 	if (!edev || !nb)
996815429b3SChanwoo Choi 		return -EINVAL;
997815429b3SChanwoo Choi 
998815429b3SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
999815429b3SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
1000815429b3SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
1001815429b3SChanwoo Choi 
1002815429b3SChanwoo Choi 	return ret;
1003815429b3SChanwoo Choi }
1004815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
1005815429b3SChanwoo Choi 
1006f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = {
1007f68a8342SChanwoo Choi 	&dev_attr_state.attr,
1008f68a8342SChanwoo Choi 	&dev_attr_name.attr,
1009f68a8342SChanwoo Choi 	NULL,
1010f68a8342SChanwoo Choi };
1011f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon);
1012f68a8342SChanwoo Choi 
1013f68a8342SChanwoo Choi static int create_extcon_class(void)
1014f68a8342SChanwoo Choi {
10157f4c9bc2SBumwoo Lee 	if (extcon_class)
10167f4c9bc2SBumwoo Lee 		return 0;
10177f4c9bc2SBumwoo Lee 
10181aaba11dSGreg Kroah-Hartman 	extcon_class = class_create("extcon");
1019f68a8342SChanwoo Choi 	if (IS_ERR(extcon_class))
1020f68a8342SChanwoo Choi 		return PTR_ERR(extcon_class);
1021f68a8342SChanwoo Choi 	extcon_class->dev_groups = extcon_groups;
1022f68a8342SChanwoo Choi 
1023f68a8342SChanwoo Choi 	return 0;
1024f68a8342SChanwoo Choi }
1025f68a8342SChanwoo Choi 
1026f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev)
1027f68a8342SChanwoo Choi {
1028f68a8342SChanwoo Choi }
1029f68a8342SChanwoo Choi 
1030f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive";
1031f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev)
1032f68a8342SChanwoo Choi {
1033f68a8342SChanwoo Choi }
1034f68a8342SChanwoo Choi 
1035f68a8342SChanwoo Choi /*
1036f68a8342SChanwoo Choi  * extcon_dev_allocate() - Allocate the memory of extcon device.
10376ab6094fSChanwoo Choi  * @supported_cable:	the array of the supported external connectors
10386ab6094fSChanwoo Choi  *			ending with EXTCON_NONE.
1039f68a8342SChanwoo Choi  *
10406ab6094fSChanwoo Choi  * Note that this function allocates the memory for extcon device
10416ab6094fSChanwoo Choi  * and initialize default setting for the extcon device.
1042f68a8342SChanwoo Choi  *
10436ab6094fSChanwoo Choi  * Returns the pointer memory of allocated extcon_dev if success
10446ab6094fSChanwoo Choi  * or ERR_PTR(err) if fail.
1045f68a8342SChanwoo Choi  */
104673b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1047f68a8342SChanwoo Choi {
1048f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1049f68a8342SChanwoo Choi 
10507eae43aeSChanwoo Choi 	if (!supported_cable)
10517eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
10527eae43aeSChanwoo Choi 
1053f68a8342SChanwoo Choi 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1054f68a8342SChanwoo Choi 	if (!edev)
1055f68a8342SChanwoo Choi 		return ERR_PTR(-ENOMEM);
1056f68a8342SChanwoo Choi 
1057f68a8342SChanwoo Choi 	edev->max_supported = 0;
1058f68a8342SChanwoo Choi 	edev->supported_cable = supported_cable;
1059f68a8342SChanwoo Choi 
1060f68a8342SChanwoo Choi 	return edev;
1061f68a8342SChanwoo Choi }
1062f68a8342SChanwoo Choi 
1063f68a8342SChanwoo Choi /*
1064f68a8342SChanwoo Choi  * extcon_dev_free() - Free the memory of extcon device.
10656ab6094fSChanwoo Choi  * @edev:	the extcon device
1066f68a8342SChanwoo Choi  */
1067f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev)
1068f68a8342SChanwoo Choi {
1069f68a8342SChanwoo Choi 	kfree(edev);
1070f68a8342SChanwoo Choi }
1071f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free);
1072f68a8342SChanwoo Choi 
1073f68a8342SChanwoo Choi /**
10743d9138e5SBumwoo Lee  * extcon_alloc_cables() - alloc the cables for extcon device
10753d9138e5SBumwoo Lee  * @edev:	extcon device which has cables
10763d9138e5SBumwoo Lee  *
10773d9138e5SBumwoo Lee  * Returns 0 if success or error number if fail.
10783d9138e5SBumwoo Lee  */
10793d9138e5SBumwoo Lee static int extcon_alloc_cables(struct extcon_dev *edev)
10803d9138e5SBumwoo Lee {
10813d9138e5SBumwoo Lee 	int index;
10823d9138e5SBumwoo Lee 	char *str;
10833d9138e5SBumwoo Lee 	struct extcon_cable *cable;
10843d9138e5SBumwoo Lee 
10853d9138e5SBumwoo Lee 	if (!edev)
10863d9138e5SBumwoo Lee 		return -EINVAL;
10873d9138e5SBumwoo Lee 
10883d9138e5SBumwoo Lee 	if (!edev->max_supported)
10893d9138e5SBumwoo Lee 		return 0;
10903d9138e5SBumwoo Lee 
10913d9138e5SBumwoo Lee 	edev->cables = kcalloc(edev->max_supported,
10923d9138e5SBumwoo Lee 			       sizeof(struct extcon_cable),
10933d9138e5SBumwoo Lee 			       GFP_KERNEL);
10943d9138e5SBumwoo Lee 	if (!edev->cables)
10953d9138e5SBumwoo Lee 		return -ENOMEM;
10963d9138e5SBumwoo Lee 
10973d9138e5SBumwoo Lee 	for (index = 0; index < edev->max_supported; index++) {
10983d9138e5SBumwoo Lee 		cable = &edev->cables[index];
10993d9138e5SBumwoo Lee 
11003d9138e5SBumwoo Lee 		str = kasprintf(GFP_KERNEL, "cable.%d", index);
11013d9138e5SBumwoo Lee 		if (!str) {
11023d9138e5SBumwoo Lee 			for (index--; index >= 0; index--) {
11033d9138e5SBumwoo Lee 				cable = &edev->cables[index];
11043d9138e5SBumwoo Lee 				kfree(cable->attr_g.name);
11053d9138e5SBumwoo Lee 			}
11063d9138e5SBumwoo Lee 
11073d9138e5SBumwoo Lee 			kfree(edev->cables);
11083d9138e5SBumwoo Lee 			return -ENOMEM;
11093d9138e5SBumwoo Lee 		}
11103d9138e5SBumwoo Lee 
11113d9138e5SBumwoo Lee 		cable->edev = edev;
11123d9138e5SBumwoo Lee 		cable->cable_index = index;
11133d9138e5SBumwoo Lee 		cable->attrs[0] = &cable->attr_name.attr;
11143d9138e5SBumwoo Lee 		cable->attrs[1] = &cable->attr_state.attr;
11153d9138e5SBumwoo Lee 		cable->attrs[2] = NULL;
11163d9138e5SBumwoo Lee 		cable->attr_g.name = str;
11173d9138e5SBumwoo Lee 		cable->attr_g.attrs = cable->attrs;
11183d9138e5SBumwoo Lee 
11193d9138e5SBumwoo Lee 		sysfs_attr_init(&cable->attr_name.attr);
11203d9138e5SBumwoo Lee 		cable->attr_name.attr.name = "name";
11213d9138e5SBumwoo Lee 		cable->attr_name.attr.mode = 0444;
11223d9138e5SBumwoo Lee 		cable->attr_name.show = cable_name_show;
11233d9138e5SBumwoo Lee 
11243d9138e5SBumwoo Lee 		sysfs_attr_init(&cable->attr_state.attr);
11253d9138e5SBumwoo Lee 		cable->attr_state.attr.name = "state";
11263d9138e5SBumwoo Lee 		cable->attr_state.attr.mode = 0444;
11273d9138e5SBumwoo Lee 		cable->attr_state.show = cable_state_show;
11283d9138e5SBumwoo Lee 	}
11293d9138e5SBumwoo Lee 
11303d9138e5SBumwoo Lee 	return 0;
11313d9138e5SBumwoo Lee }
11323d9138e5SBumwoo Lee 
11333d9138e5SBumwoo Lee /**
11343e70a014SBumwoo Lee  * extcon_alloc_muex() - alloc the mutual exclusive for extcon device
11353e70a014SBumwoo Lee  * @edev:	extcon device
11363e70a014SBumwoo Lee  *
11373e70a014SBumwoo Lee  * Returns 0 if success or error number if fail.
11383e70a014SBumwoo Lee  */
11393e70a014SBumwoo Lee static int extcon_alloc_muex(struct extcon_dev *edev)
11403e70a014SBumwoo Lee {
11413e70a014SBumwoo Lee 	char *name;
11423e70a014SBumwoo Lee 	int index;
11433e70a014SBumwoo Lee 
11443e70a014SBumwoo Lee 	if (!edev)
11453e70a014SBumwoo Lee 		return -EINVAL;
11463e70a014SBumwoo Lee 
11473e70a014SBumwoo Lee 	if (!(edev->max_supported && edev->mutually_exclusive))
11483e70a014SBumwoo Lee 		return 0;
11493e70a014SBumwoo Lee 
11503e70a014SBumwoo Lee 	/* Count the size of mutually_exclusive array */
11513e70a014SBumwoo Lee 	for (index = 0; edev->mutually_exclusive[index]; index++)
11523e70a014SBumwoo Lee 		;
11533e70a014SBumwoo Lee 
11543e70a014SBumwoo Lee 	edev->attrs_muex = kcalloc(index + 1,
11553e70a014SBumwoo Lee 				   sizeof(struct attribute *),
11563e70a014SBumwoo Lee 				   GFP_KERNEL);
11573e70a014SBumwoo Lee 	if (!edev->attrs_muex)
11583e70a014SBumwoo Lee 		return -ENOMEM;
11593e70a014SBumwoo Lee 
11603e70a014SBumwoo Lee 	edev->d_attrs_muex = kcalloc(index,
11613e70a014SBumwoo Lee 				     sizeof(struct device_attribute),
11623e70a014SBumwoo Lee 				     GFP_KERNEL);
11633e70a014SBumwoo Lee 	if (!edev->d_attrs_muex) {
11643e70a014SBumwoo Lee 		kfree(edev->attrs_muex);
11653e70a014SBumwoo Lee 		return -ENOMEM;
11663e70a014SBumwoo Lee 	}
11673e70a014SBumwoo Lee 
11683e70a014SBumwoo Lee 	for (index = 0; edev->mutually_exclusive[index]; index++) {
11693e70a014SBumwoo Lee 		name = kasprintf(GFP_KERNEL, "0x%x",
11703e70a014SBumwoo Lee 				 edev->mutually_exclusive[index]);
11713e70a014SBumwoo Lee 		if (!name) {
11723e70a014SBumwoo Lee 			for (index--; index >= 0; index--)
11733e70a014SBumwoo Lee 				kfree(edev->d_attrs_muex[index].attr.name);
11743e70a014SBumwoo Lee 
11753e70a014SBumwoo Lee 			kfree(edev->d_attrs_muex);
11763e70a014SBumwoo Lee 			kfree(edev->attrs_muex);
11773e70a014SBumwoo Lee 			return -ENOMEM;
11783e70a014SBumwoo Lee 		}
11793e70a014SBumwoo Lee 		sysfs_attr_init(&edev->d_attrs_muex[index].attr);
11803e70a014SBumwoo Lee 		edev->d_attrs_muex[index].attr.name = name;
11813e70a014SBumwoo Lee 		edev->d_attrs_muex[index].attr.mode = 0000;
11823e70a014SBumwoo Lee 		edev->attrs_muex[index] = &edev->d_attrs_muex[index].attr;
11833e70a014SBumwoo Lee 	}
11843e70a014SBumwoo Lee 	edev->attr_g_muex.name = muex_name;
11853e70a014SBumwoo Lee 	edev->attr_g_muex.attrs = edev->attrs_muex;
11863e70a014SBumwoo Lee 
11873e70a014SBumwoo Lee 	return 0;
11883e70a014SBumwoo Lee }
11893e70a014SBumwoo Lee 
11903e70a014SBumwoo Lee /**
1191*04151575SBumwoo Lee  * extcon_alloc_groups() - alloc the groups for extcon device
1192*04151575SBumwoo Lee  * @edev:	extcon device
1193*04151575SBumwoo Lee  *
1194*04151575SBumwoo Lee  * Returns 0 if success or error number if fail.
1195*04151575SBumwoo Lee  */
1196*04151575SBumwoo Lee static int extcon_alloc_groups(struct extcon_dev *edev)
1197*04151575SBumwoo Lee {
1198*04151575SBumwoo Lee 	int index;
1199*04151575SBumwoo Lee 
1200*04151575SBumwoo Lee 	if (!edev)
1201*04151575SBumwoo Lee 		return -EINVAL;
1202*04151575SBumwoo Lee 
1203*04151575SBumwoo Lee 	if (!edev->max_supported)
1204*04151575SBumwoo Lee 		return 0;
1205*04151575SBumwoo Lee 
1206*04151575SBumwoo Lee 	edev->extcon_dev_type.groups = kcalloc(edev->max_supported + 2,
1207*04151575SBumwoo Lee 			sizeof(struct attribute_group *),
1208*04151575SBumwoo Lee 			GFP_KERNEL);
1209*04151575SBumwoo Lee 	if (!edev->extcon_dev_type.groups)
1210*04151575SBumwoo Lee 		return -ENOMEM;
1211*04151575SBumwoo Lee 
1212*04151575SBumwoo Lee 	edev->extcon_dev_type.name = dev_name(&edev->dev);
1213*04151575SBumwoo Lee 	edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1214*04151575SBumwoo Lee 
1215*04151575SBumwoo Lee 	for (index = 0; index < edev->max_supported; index++)
1216*04151575SBumwoo Lee 		edev->extcon_dev_type.groups[index] = &edev->cables[index].attr_g;
1217*04151575SBumwoo Lee 
1218*04151575SBumwoo Lee 	if (edev->mutually_exclusive)
1219*04151575SBumwoo Lee 		edev->extcon_dev_type.groups[index] = &edev->attr_g_muex;
1220*04151575SBumwoo Lee 
1221*04151575SBumwoo Lee 	edev->dev.type = &edev->extcon_dev_type;
1222*04151575SBumwoo Lee 
1223*04151575SBumwoo Lee 	return 0;
1224*04151575SBumwoo Lee }
1225*04151575SBumwoo Lee 
1226*04151575SBumwoo Lee /**
12276ab6094fSChanwoo Choi  * extcon_dev_register() - Register an new extcon device
12286ab6094fSChanwoo Choi  * @edev:	the extcon device to be registered
1229f68a8342SChanwoo Choi  *
1230f68a8342SChanwoo Choi  * Among the members of edev struct, please set the "user initializing data"
1231f68a8342SChanwoo Choi  * do not set the values of "internal data", which are initialized by
1232f68a8342SChanwoo Choi  * this function.
12336ab6094fSChanwoo Choi  *
12346ab6094fSChanwoo Choi  * Note that before calling this funciton, have to allocate the memory
12356ab6094fSChanwoo Choi  * of an extcon device by using the extcon_dev_allocate(). And the extcon
12366ab6094fSChanwoo Choi  * dev should include the supported_cable information.
12376ab6094fSChanwoo Choi  *
12386ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
1239f68a8342SChanwoo Choi  */
1240f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev)
1241f68a8342SChanwoo Choi {
1242f68a8342SChanwoo Choi 	int ret, index = 0;
124371c3ffa5SChanwoo Choi 	static atomic_t edev_no = ATOMIC_INIT(-1);
1244f68a8342SChanwoo Choi 
1245f68a8342SChanwoo Choi 	ret = create_extcon_class();
1246f68a8342SChanwoo Choi 	if (ret < 0)
1247f68a8342SChanwoo Choi 		return ret;
1248f68a8342SChanwoo Choi 
12497eae43aeSChanwoo Choi 	if (!edev || !edev->supported_cable)
12502a9de9c0SChanwoo Choi 		return -EINVAL;
1251f68a8342SChanwoo Choi 
12522a9de9c0SChanwoo Choi 	for (; edev->supported_cable[index] != EXTCON_NONE; index++);
12532a9de9c0SChanwoo Choi 
12542a9de9c0SChanwoo Choi 	edev->max_supported = index;
1255f68a8342SChanwoo Choi 	if (index > SUPPORTED_CABLE_MAX) {
12562a9de9c0SChanwoo Choi 		dev_err(&edev->dev,
12572a9de9c0SChanwoo Choi 			"exceed the maximum number of supported cables\n");
1258f68a8342SChanwoo Choi 		return -EINVAL;
1259f68a8342SChanwoo Choi 	}
1260f68a8342SChanwoo Choi 
1261f68a8342SChanwoo Choi 	edev->dev.class = extcon_class;
1262f68a8342SChanwoo Choi 	edev->dev.release = extcon_dev_release;
1263f68a8342SChanwoo Choi 
126471c3ffa5SChanwoo Choi 	edev->name = dev_name(edev->dev.parent);
1265f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(edev->name)) {
1266f68a8342SChanwoo Choi 		dev_err(&edev->dev,
1267f68a8342SChanwoo Choi 			"extcon device name is null\n");
1268f68a8342SChanwoo Choi 		return -EINVAL;
1269f68a8342SChanwoo Choi 	}
127071c3ffa5SChanwoo Choi 	dev_set_name(&edev->dev, "extcon%lu",
127171c3ffa5SChanwoo Choi 			(unsigned long)atomic_inc_return(&edev_no));
1272f68a8342SChanwoo Choi 
12733d9138e5SBumwoo Lee 	ret = extcon_alloc_cables(edev);
12743d9138e5SBumwoo Lee 	if (ret < 0)
1275f68a8342SChanwoo Choi 		goto err_alloc_cables;
1276f68a8342SChanwoo Choi 
12773e70a014SBumwoo Lee 	ret = extcon_alloc_muex(edev);
12783e70a014SBumwoo Lee 	if (ret < 0)
12793e70a014SBumwoo Lee 		goto err_alloc_muex;
1280f68a8342SChanwoo Choi 
1281*04151575SBumwoo Lee 	ret = extcon_alloc_groups(edev);
1282*04151575SBumwoo Lee 	if (ret < 0)
1283f68a8342SChanwoo Choi 		goto err_alloc_groups;
1284f68a8342SChanwoo Choi 
1285f68a8342SChanwoo Choi 	spin_lock_init(&edev->lock);
12865dcc2afeSbumwoo lee 	if (edev->max_supported) {
12875dcc2afeSbumwoo lee 		edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
12885dcc2afeSbumwoo lee 				GFP_KERNEL);
1289046050f6SChanwoo Choi 		if (!edev->nh) {
1290046050f6SChanwoo Choi 			ret = -ENOMEM;
12915dcc2afeSbumwoo lee 			goto err_alloc_nh;
12925dcc2afeSbumwoo lee 		}
1293046050f6SChanwoo Choi 	}
1294046050f6SChanwoo Choi 
1295046050f6SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1296046050f6SChanwoo Choi 		RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1297f68a8342SChanwoo Choi 
1298815429b3SChanwoo Choi 	RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1299815429b3SChanwoo Choi 
1300f68a8342SChanwoo Choi 	dev_set_drvdata(&edev->dev, edev);
1301f68a8342SChanwoo Choi 	edev->state = 0;
1302f68a8342SChanwoo Choi 
13035dcc2afeSbumwoo lee 	ret = device_register(&edev->dev);
13045dcc2afeSbumwoo lee 	if (ret) {
13055dcc2afeSbumwoo lee 		put_device(&edev->dev);
13065dcc2afeSbumwoo lee 		goto err_dev;
13075dcc2afeSbumwoo lee 	}
13085dcc2afeSbumwoo lee 
1309f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1310f68a8342SChanwoo Choi 	list_add(&edev->entry, &extcon_dev_list);
1311f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1312f68a8342SChanwoo Choi 
1313f68a8342SChanwoo Choi 	return 0;
1314f68a8342SChanwoo Choi 
1315f68a8342SChanwoo Choi err_dev:
1316f68a8342SChanwoo Choi 	if (edev->max_supported)
13175dcc2afeSbumwoo lee 		kfree(edev->nh);
13185dcc2afeSbumwoo lee err_alloc_nh:
13195dcc2afeSbumwoo lee 	if (edev->max_supported)
1320f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1321f68a8342SChanwoo Choi err_alloc_groups:
1322f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
1323f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
1324f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1325f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1326f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1327f68a8342SChanwoo Choi 	}
13283e70a014SBumwoo Lee err_alloc_muex:
1329f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1330f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1331f68a8342SChanwoo Choi 	if (edev->max_supported)
1332f68a8342SChanwoo Choi 		kfree(edev->cables);
13333d9138e5SBumwoo Lee err_alloc_cables:
13343d9138e5SBumwoo Lee 
1335f68a8342SChanwoo Choi 	return ret;
1336f68a8342SChanwoo Choi }
1337f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register);
1338f68a8342SChanwoo Choi 
1339f68a8342SChanwoo Choi /**
1340f68a8342SChanwoo Choi  * extcon_dev_unregister() - Unregister the extcon device.
13416ab6094fSChanwoo Choi  * @edev:	the extcon device to be unregistered.
1342f68a8342SChanwoo Choi  *
1343f68a8342SChanwoo Choi  * Note that this does not call kfree(edev) because edev was not allocated
1344f68a8342SChanwoo Choi  * by this class.
1345f68a8342SChanwoo Choi  */
1346f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev)
1347f68a8342SChanwoo Choi {
1348f68a8342SChanwoo Choi 	int index;
1349f68a8342SChanwoo Choi 
13507eae43aeSChanwoo Choi 	if (!edev)
13517eae43aeSChanwoo Choi 		return;
13527eae43aeSChanwoo Choi 
1353f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1354f68a8342SChanwoo Choi 	list_del(&edev->entry);
1355f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1356f68a8342SChanwoo Choi 
1357f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1358f68a8342SChanwoo Choi 		dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1359f68a8342SChanwoo Choi 				dev_name(&edev->dev));
1360f68a8342SChanwoo Choi 		return;
1361f68a8342SChanwoo Choi 	}
1362f68a8342SChanwoo Choi 
1363f68a8342SChanwoo Choi 	device_unregister(&edev->dev);
1364f68a8342SChanwoo Choi 
1365f68a8342SChanwoo Choi 	if (edev->mutually_exclusive && edev->max_supported) {
1366f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index];
1367f68a8342SChanwoo Choi 				index++)
1368f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1369f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1370f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1371f68a8342SChanwoo Choi 	}
1372f68a8342SChanwoo Choi 
1373f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1374f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1375f68a8342SChanwoo Choi 
1376f68a8342SChanwoo Choi 	if (edev->max_supported) {
1377f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1378f68a8342SChanwoo Choi 		kfree(edev->cables);
13795dcc2afeSbumwoo lee 		kfree(edev->nh);
1380f68a8342SChanwoo Choi 	}
1381f68a8342SChanwoo Choi 
1382f68a8342SChanwoo Choi 	put_device(&edev->dev);
1383f68a8342SChanwoo Choi }
1384f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1385f68a8342SChanwoo Choi 
1386f68a8342SChanwoo Choi #ifdef CONFIG_OF
1387370ed7a9SAndrzej Hajda 
1388370ed7a9SAndrzej Hajda /*
1389370ed7a9SAndrzej Hajda  * extcon_find_edev_by_node - Find the extcon device from devicetree.
1390370ed7a9SAndrzej Hajda  * @node	: OF node identifying edev
1391370ed7a9SAndrzej Hajda  *
1392370ed7a9SAndrzej Hajda  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1393370ed7a9SAndrzej Hajda  */
1394370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1395370ed7a9SAndrzej Hajda {
1396370ed7a9SAndrzej Hajda 	struct extcon_dev *edev;
1397370ed7a9SAndrzej Hajda 
1398370ed7a9SAndrzej Hajda 	mutex_lock(&extcon_dev_list_lock);
1399370ed7a9SAndrzej Hajda 	list_for_each_entry(edev, &extcon_dev_list, entry)
1400370ed7a9SAndrzej Hajda 		if (edev->dev.parent && edev->dev.parent->of_node == node)
1401370ed7a9SAndrzej Hajda 			goto out;
1402370ed7a9SAndrzej Hajda 	edev = ERR_PTR(-EPROBE_DEFER);
1403370ed7a9SAndrzej Hajda out:
1404370ed7a9SAndrzej Hajda 	mutex_unlock(&extcon_dev_list_lock);
1405370ed7a9SAndrzej Hajda 
1406370ed7a9SAndrzej Hajda 	return edev;
1407370ed7a9SAndrzej Hajda }
1408370ed7a9SAndrzej Hajda 
1409f68a8342SChanwoo Choi /*
14106ab6094fSChanwoo Choi  * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
14116ab6094fSChanwoo Choi  * @dev		: the instance to the given device
14126ab6094fSChanwoo Choi  * @index	: the index into list of extcon_dev
1413f68a8342SChanwoo Choi  *
14146ab6094fSChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1415f68a8342SChanwoo Choi  */
1416f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1417f68a8342SChanwoo Choi {
1418f68a8342SChanwoo Choi 	struct device_node *node;
1419f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1420f68a8342SChanwoo Choi 
14217eae43aeSChanwoo Choi 	if (!dev)
14227eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
14237eae43aeSChanwoo Choi 
1424f68a8342SChanwoo Choi 	if (!dev->of_node) {
1425e8752b7aSStephen Boyd 		dev_dbg(dev, "device does not have a device node entry\n");
1426f68a8342SChanwoo Choi 		return ERR_PTR(-EINVAL);
1427f68a8342SChanwoo Choi 	}
1428f68a8342SChanwoo Choi 
1429f68a8342SChanwoo Choi 	node = of_parse_phandle(dev->of_node, "extcon", index);
1430f68a8342SChanwoo Choi 	if (!node) {
14315c27036dSRob Herring 		dev_dbg(dev, "failed to get phandle in %pOF node\n",
14325c27036dSRob Herring 			dev->of_node);
1433f68a8342SChanwoo Choi 		return ERR_PTR(-ENODEV);
1434f68a8342SChanwoo Choi 	}
1435f68a8342SChanwoo Choi 
1436370ed7a9SAndrzej Hajda 	edev = extcon_find_edev_by_node(node);
14375d5c4c13SPeter Chen 	of_node_put(node);
1438f68a8342SChanwoo Choi 
1439370ed7a9SAndrzej Hajda 	return edev;
1440f68a8342SChanwoo Choi }
1441370ed7a9SAndrzej Hajda 
1442f68a8342SChanwoo Choi #else
1443370ed7a9SAndrzej Hajda 
1444370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1445370ed7a9SAndrzej Hajda {
1446370ed7a9SAndrzej Hajda 	return ERR_PTR(-ENOSYS);
1447370ed7a9SAndrzej Hajda }
1448370ed7a9SAndrzej Hajda 
1449f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1450f68a8342SChanwoo Choi {
1451f68a8342SChanwoo Choi 	return ERR_PTR(-ENOSYS);
1452f68a8342SChanwoo Choi }
1453370ed7a9SAndrzej Hajda 
1454f68a8342SChanwoo Choi #endif /* CONFIG_OF */
1455370ed7a9SAndrzej Hajda 
1456370ed7a9SAndrzej Hajda EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
1457f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1458f68a8342SChanwoo Choi 
1459707d7550SChanwoo Choi /**
1460707d7550SChanwoo Choi  * extcon_get_edev_name() - Get the name of the extcon device.
1461707d7550SChanwoo Choi  * @edev:	the extcon device
1462707d7550SChanwoo Choi  */
1463707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev)
1464707d7550SChanwoo Choi {
1465707d7550SChanwoo Choi 	return !edev ? NULL : edev->name;
1466707d7550SChanwoo Choi }
1467995bb109SMayank Rana EXPORT_SYMBOL_GPL(extcon_get_edev_name);
1468707d7550SChanwoo Choi 
1469f68a8342SChanwoo Choi static int __init extcon_class_init(void)
1470f68a8342SChanwoo Choi {
1471f68a8342SChanwoo Choi 	return create_extcon_class();
1472f68a8342SChanwoo Choi }
1473f68a8342SChanwoo Choi module_init(extcon_class_init);
1474f68a8342SChanwoo Choi 
1475f68a8342SChanwoo Choi static void __exit extcon_class_exit(void)
1476f68a8342SChanwoo Choi {
1477f68a8342SChanwoo Choi 	class_destroy(extcon_class);
1478f68a8342SChanwoo Choi }
1479f68a8342SChanwoo Choi module_exit(extcon_class_exit);
1480f68a8342SChanwoo Choi 
14812a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1482f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
14836ab6094fSChanwoo Choi MODULE_DESCRIPTION("External Connector (extcon) framework");
14846ab6094fSChanwoo Choi MODULE_LICENSE("GPL v2");
1485