xref: /openbmc/linux/drivers/extcon/extcon.c (revision 8b755fc7)
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>
197bba9e81SAndy Shevchenko #include <linux/idr.h>
20f68a8342SChanwoo Choi #include <linux/init.h>
21f68a8342SChanwoo Choi #include <linux/device.h>
22f68a8342SChanwoo Choi #include <linux/fs.h>
23f68a8342SChanwoo Choi #include <linux/err.h>
24f68a8342SChanwoo Choi #include <linux/of.h>
25f68a8342SChanwoo Choi #include <linux/slab.h>
26f68a8342SChanwoo Choi #include <linux/sysfs.h>
27f68a8342SChanwoo Choi 
28e6cf0465SChanwoo Choi #include "extcon.h"
29e6cf0465SChanwoo Choi 
302a9de9c0SChanwoo Choi #define SUPPORTED_CABLE_MAX	32
312a9de9c0SChanwoo Choi 
325183240cSColin Ian King static const struct __extcon_info {
3355e4e2f1SChanwoo Choi 	unsigned int type;
3455e4e2f1SChanwoo Choi 	unsigned int id;
3555e4e2f1SChanwoo Choi 	const char *name;
3655e4e2f1SChanwoo Choi 
3755e4e2f1SChanwoo Choi } extcon_info[] = {
3855e4e2f1SChanwoo Choi 	[EXTCON_NONE] = {
3955e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
4055e4e2f1SChanwoo Choi 		.id = EXTCON_NONE,
4155e4e2f1SChanwoo Choi 		.name = "NONE",
4255e4e2f1SChanwoo Choi 	},
4373b6ecdbSChanwoo Choi 
448e9bc36dSChanwoo Choi 	/* USB external connector */
4555e4e2f1SChanwoo Choi 	[EXTCON_USB] = {
4655e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_USB,
4755e4e2f1SChanwoo Choi 		.id = EXTCON_USB,
4855e4e2f1SChanwoo Choi 		.name = "USB",
4955e4e2f1SChanwoo Choi 	},
5055e4e2f1SChanwoo Choi 	[EXTCON_USB_HOST] = {
5155e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_USB,
5255e4e2f1SChanwoo Choi 		.id = EXTCON_USB_HOST,
5386d6cda6SChanwoo Choi 		.name = "USB-HOST",
5455e4e2f1SChanwoo Choi 	},
558e9bc36dSChanwoo Choi 
5611eecf91SChanwoo Choi 	/* Charging external connector */
5755e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_SDP] = {
5855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
5955e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_SDP,
6055e4e2f1SChanwoo Choi 		.name = "SDP",
6155e4e2f1SChanwoo Choi 	},
6255e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_DCP] = {
6355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
6455e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_DCP,
6555e4e2f1SChanwoo Choi 		.name = "DCP",
6655e4e2f1SChanwoo Choi 	},
6755e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_CDP] = {
6855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
6955e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_CDP,
7055e4e2f1SChanwoo Choi 		.name = "CDP",
7155e4e2f1SChanwoo Choi 	},
7255e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_ACA] = {
7355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
7455e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_ACA,
7555e4e2f1SChanwoo Choi 		.name = "ACA",
7655e4e2f1SChanwoo Choi 	},
7755e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_FAST] = {
7855e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
7955e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_FAST,
8055e4e2f1SChanwoo Choi 		.name = "FAST-CHARGER",
8155e4e2f1SChanwoo Choi 	},
8255e4e2f1SChanwoo Choi 	[EXTCON_CHG_USB_SLOW] = {
8355e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
8455e4e2f1SChanwoo Choi 		.id = EXTCON_CHG_USB_SLOW,
8555e4e2f1SChanwoo Choi 		.name = "SLOW-CHARGER",
8655e4e2f1SChanwoo Choi 	},
877fe95fb8SChanwoo Choi 	[EXTCON_CHG_WPT] = {
887fe95fb8SChanwoo Choi 		.type = EXTCON_TYPE_CHG,
897fe95fb8SChanwoo Choi 		.id = EXTCON_CHG_WPT,
907fe95fb8SChanwoo Choi 		.name = "WPT",
917fe95fb8SChanwoo Choi 	},
923c5f0e07SChanwoo Choi 	[EXTCON_CHG_USB_PD] = {
933c5f0e07SChanwoo Choi 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
943c5f0e07SChanwoo Choi 		.id = EXTCON_CHG_USB_PD,
953c5f0e07SChanwoo Choi 		.name = "PD",
963c5f0e07SChanwoo Choi 	},
978e9bc36dSChanwoo Choi 
9811eecf91SChanwoo Choi 	/* Jack external connector */
9955e4e2f1SChanwoo Choi 	[EXTCON_JACK_MICROPHONE] = {
10055e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
10155e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_MICROPHONE,
10255e4e2f1SChanwoo Choi 		.name = "MICROPHONE",
10355e4e2f1SChanwoo Choi 	},
10455e4e2f1SChanwoo Choi 	[EXTCON_JACK_HEADPHONE] = {
10555e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
10655e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_HEADPHONE,
10755e4e2f1SChanwoo Choi 		.name = "HEADPHONE",
10855e4e2f1SChanwoo Choi 	},
10955e4e2f1SChanwoo Choi 	[EXTCON_JACK_LINE_IN] = {
11055e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
11155e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_LINE_IN,
11255e4e2f1SChanwoo Choi 		.name = "LINE-IN",
11355e4e2f1SChanwoo Choi 	},
11455e4e2f1SChanwoo Choi 	[EXTCON_JACK_LINE_OUT] = {
11555e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
11655e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_LINE_OUT,
11755e4e2f1SChanwoo Choi 		.name = "LINE-OUT",
11855e4e2f1SChanwoo Choi 	},
11955e4e2f1SChanwoo Choi 	[EXTCON_JACK_VIDEO_IN] = {
12055e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
12155e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_VIDEO_IN,
12255e4e2f1SChanwoo Choi 		.name = "VIDEO-IN",
12355e4e2f1SChanwoo Choi 	},
12455e4e2f1SChanwoo Choi 	[EXTCON_JACK_VIDEO_OUT] = {
12555e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
12655e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_VIDEO_OUT,
12755e4e2f1SChanwoo Choi 		.name = "VIDEO-OUT",
12855e4e2f1SChanwoo Choi 	},
12955e4e2f1SChanwoo Choi 	[EXTCON_JACK_SPDIF_IN] = {
13055e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
13155e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_SPDIF_IN,
13255e4e2f1SChanwoo Choi 		.name = "SPDIF-IN",
13355e4e2f1SChanwoo Choi 	},
13455e4e2f1SChanwoo Choi 	[EXTCON_JACK_SPDIF_OUT] = {
13555e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_JACK,
13655e4e2f1SChanwoo Choi 		.id = EXTCON_JACK_SPDIF_OUT,
13755e4e2f1SChanwoo Choi 		.name = "SPDIF-OUT",
13855e4e2f1SChanwoo Choi 	},
1398e9bc36dSChanwoo Choi 
14011eecf91SChanwoo Choi 	/* Display external connector */
14155e4e2f1SChanwoo Choi 	[EXTCON_DISP_HDMI] = {
14255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
14355e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_HDMI,
14455e4e2f1SChanwoo Choi 		.name = "HDMI",
14555e4e2f1SChanwoo Choi 	},
14655e4e2f1SChanwoo Choi 	[EXTCON_DISP_MHL] = {
14755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
14855e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_MHL,
14955e4e2f1SChanwoo Choi 		.name = "MHL",
15055e4e2f1SChanwoo Choi 	},
15155e4e2f1SChanwoo Choi 	[EXTCON_DISP_DVI] = {
15255e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
15355e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_DVI,
15455e4e2f1SChanwoo Choi 		.name = "DVI",
15555e4e2f1SChanwoo Choi 	},
15655e4e2f1SChanwoo Choi 	[EXTCON_DISP_VGA] = {
15755e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_DISP,
15855e4e2f1SChanwoo Choi 		.id = EXTCON_DISP_VGA,
15955e4e2f1SChanwoo Choi 		.name = "VGA",
16055e4e2f1SChanwoo Choi 	},
1612164188dSChris Zhong 	[EXTCON_DISP_DP] = {
1622164188dSChris Zhong 		.type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
1632164188dSChris Zhong 		.id = EXTCON_DISP_DP,
1642164188dSChris Zhong 		.name = "DP",
1652164188dSChris Zhong 	},
1669c0595d6SChanwoo Choi 	[EXTCON_DISP_HMD] = {
1679c0595d6SChanwoo Choi 		.type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
1689c0595d6SChanwoo Choi 		.id = EXTCON_DISP_HMD,
1699c0595d6SChanwoo Choi 		.name = "HMD",
1709c0595d6SChanwoo Choi 	},
1713a06ed80SMichael Wu 	[EXTCON_DISP_CVBS] = {
1723a06ed80SMichael Wu 		.type = EXTCON_TYPE_DISP,
1733a06ed80SMichael Wu 		.id = EXTCON_DISP_CVBS,
1743a06ed80SMichael Wu 		.name = "CVBS",
1753a06ed80SMichael Wu 	},
1763a06ed80SMichael Wu 	[EXTCON_DISP_EDP] = {
1773a06ed80SMichael Wu 		.type = EXTCON_TYPE_DISP,
1783a06ed80SMichael Wu 		.id = EXTCON_DISP_EDP,
1793a06ed80SMichael Wu 		.name = "EDP",
1803a06ed80SMichael Wu 	},
1818e9bc36dSChanwoo Choi 
18211eecf91SChanwoo Choi 	/* Miscellaneous external connector */
18355e4e2f1SChanwoo Choi 	[EXTCON_DOCK] = {
18455e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
18555e4e2f1SChanwoo Choi 		.id = EXTCON_DOCK,
18655e4e2f1SChanwoo Choi 		.name = "DOCK",
18755e4e2f1SChanwoo Choi 	},
18855e4e2f1SChanwoo Choi 	[EXTCON_JIG] = {
18955e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
19055e4e2f1SChanwoo Choi 		.id = EXTCON_JIG,
19155e4e2f1SChanwoo Choi 		.name = "JIG",
19255e4e2f1SChanwoo Choi 	},
19355e4e2f1SChanwoo Choi 	[EXTCON_MECHANICAL] = {
19455e4e2f1SChanwoo Choi 		.type = EXTCON_TYPE_MISC,
19555e4e2f1SChanwoo Choi 		.id = EXTCON_MECHANICAL,
19655e4e2f1SChanwoo Choi 		.name = "MECHANICAL",
19755e4e2f1SChanwoo Choi 	},
1988e9bc36dSChanwoo Choi 
19955e4e2f1SChanwoo Choi 	{ /* sentinel */ }
200f68a8342SChanwoo Choi };
201f68a8342SChanwoo Choi 
20220f7b53dSChanwoo Choi /**
2036ab6094fSChanwoo Choi  * struct extcon_cable - An internal data for an external connector.
2046ab6094fSChanwoo Choi  * @edev:		the extcon device
2056ab6094fSChanwoo Choi  * @cable_index:	the index of this cable in the edev
2066ab6094fSChanwoo Choi  * @attr_g:		the attribute group for the cable
20720f7b53dSChanwoo Choi  * @attr_name:		"name" sysfs entry
20820f7b53dSChanwoo Choi  * @attr_state:		"state" sysfs entry
2096ab6094fSChanwoo Choi  * @attrs:		the array pointing to attr_name and attr_state for attr_g
2107e77e0b7SAndy Shevchenko  * @usb_propval:	the array of USB connector properties
2117e77e0b7SAndy Shevchenko  * @chg_propval:	the array of charger connector properties
2127e77e0b7SAndy Shevchenko  * @jack_propval:	the array of jack connector properties
2137e77e0b7SAndy Shevchenko  * @disp_propval:	the array of display connector properties
21473346b99SAndy Shevchenko  * @usb_bits:		the bit array of the USB connector property capabilities
21573346b99SAndy Shevchenko  * @chg_bits:		the bit array of the charger connector property capabilities
21673346b99SAndy Shevchenko  * @jack_bits:		the bit array of the jack connector property capabilities
21773346b99SAndy Shevchenko  * @disp_bits:		the bit array of the display connector property capabilities
21820f7b53dSChanwoo Choi  */
21920f7b53dSChanwoo Choi struct extcon_cable {
22020f7b53dSChanwoo Choi 	struct extcon_dev *edev;
22120f7b53dSChanwoo Choi 	int cable_index;
22220f7b53dSChanwoo Choi 
22320f7b53dSChanwoo Choi 	struct attribute_group attr_g;
22420f7b53dSChanwoo Choi 	struct device_attribute attr_name;
22520f7b53dSChanwoo Choi 	struct device_attribute attr_state;
22620f7b53dSChanwoo Choi 
22720f7b53dSChanwoo Choi 	struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
228792e7e9eSChanwoo Choi 
229792e7e9eSChanwoo Choi 	union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
230792e7e9eSChanwoo Choi 	union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
231792e7e9eSChanwoo Choi 	union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
232792e7e9eSChanwoo Choi 	union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
2337f2a0a16SChanwoo Choi 
2346e4e8670SAndy Shevchenko 	DECLARE_BITMAP(usb_bits, EXTCON_PROP_USB_CNT);
2356e4e8670SAndy Shevchenko 	DECLARE_BITMAP(chg_bits, EXTCON_PROP_CHG_CNT);
2366e4e8670SAndy Shevchenko 	DECLARE_BITMAP(jack_bits, EXTCON_PROP_JACK_CNT);
2376e4e8670SAndy Shevchenko 	DECLARE_BITMAP(disp_bits, EXTCON_PROP_DISP_CNT);
23820f7b53dSChanwoo Choi };
23920f7b53dSChanwoo Choi 
240f68a8342SChanwoo Choi static struct class *extcon_class;
241f68a8342SChanwoo Choi 
2427bba9e81SAndy Shevchenko static DEFINE_IDA(extcon_dev_ids);
243f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list);
244f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock);
245f68a8342SChanwoo Choi 
check_mutually_exclusive(struct extcon_dev * edev,u32 new_state)246f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
247f68a8342SChanwoo Choi {
24893e60cd5SAndy Shevchenko 	int i;
249f68a8342SChanwoo Choi 
250f68a8342SChanwoo Choi 	if (!edev->mutually_exclusive)
251f68a8342SChanwoo Choi 		return 0;
252f68a8342SChanwoo Choi 
253f68a8342SChanwoo Choi 	for (i = 0; edev->mutually_exclusive[i]; i++) {
254f68a8342SChanwoo Choi 		int weight;
255f68a8342SChanwoo Choi 		u32 correspondants = new_state & edev->mutually_exclusive[i];
256f68a8342SChanwoo Choi 
257f68a8342SChanwoo Choi 		/* calculate the total number of bits set */
258f68a8342SChanwoo Choi 		weight = hweight32(correspondants);
259f68a8342SChanwoo Choi 		if (weight > 1)
260f68a8342SChanwoo Choi 			return i + 1;
261f68a8342SChanwoo Choi 	}
262f68a8342SChanwoo Choi 
263f68a8342SChanwoo Choi 	return 0;
264f68a8342SChanwoo Choi }
265f68a8342SChanwoo Choi 
find_cable_index_by_id(struct extcon_dev * edev,const unsigned int id)26673b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
2672a9de9c0SChanwoo Choi {
2682a9de9c0SChanwoo Choi 	int i;
2692a9de9c0SChanwoo Choi 
27097e1bb93SJiang Jian 	/* Find the index of extcon cable in edev->supported_cable */
2712a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
2722a9de9c0SChanwoo Choi 		if (edev->supported_cable[i] == id)
2732a9de9c0SChanwoo Choi 			return i;
2742a9de9c0SChanwoo Choi 	}
2752a9de9c0SChanwoo Choi 
2762a9de9c0SChanwoo Choi 	return -EINVAL;
2772a9de9c0SChanwoo Choi }
2782a9de9c0SChanwoo Choi 
get_extcon_type(unsigned int prop)279792e7e9eSChanwoo Choi static int get_extcon_type(unsigned int prop)
280792e7e9eSChanwoo Choi {
281792e7e9eSChanwoo Choi 	switch (prop) {
282792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
283792e7e9eSChanwoo Choi 		return EXTCON_TYPE_USB;
284792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
285792e7e9eSChanwoo Choi 		return EXTCON_TYPE_CHG;
286792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
287792e7e9eSChanwoo Choi 		return EXTCON_TYPE_JACK;
288792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
289792e7e9eSChanwoo Choi 		return EXTCON_TYPE_DISP;
290792e7e9eSChanwoo Choi 	default:
291792e7e9eSChanwoo Choi 		return -EINVAL;
292792e7e9eSChanwoo Choi 	}
293792e7e9eSChanwoo Choi }
294792e7e9eSChanwoo Choi 
is_extcon_attached(struct extcon_dev * edev,unsigned int index)295792e7e9eSChanwoo Choi static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
296792e7e9eSChanwoo Choi {
297792e7e9eSChanwoo Choi 	return !!(edev->state & BIT(index));
298792e7e9eSChanwoo Choi }
299792e7e9eSChanwoo Choi 
is_extcon_changed(struct extcon_dev * edev,int index,bool new_state)300ab11af04SChanwoo Choi static bool is_extcon_changed(struct extcon_dev *edev, int index,
301ab11af04SChanwoo Choi 				bool new_state)
302046050f6SChanwoo Choi {
303ab11af04SChanwoo Choi 	int state = !!(edev->state & BIT(index));
304ab11af04SChanwoo Choi 	return (state != new_state);
305046050f6SChanwoo Choi }
306046050f6SChanwoo Choi 
is_extcon_property_supported(unsigned int id,unsigned int prop)307792e7e9eSChanwoo Choi static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
308792e7e9eSChanwoo Choi {
309792e7e9eSChanwoo Choi 	int type;
310792e7e9eSChanwoo Choi 
311792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not. */
312792e7e9eSChanwoo Choi 	type = get_extcon_type(prop);
313792e7e9eSChanwoo Choi 	if (type < 0)
314792e7e9eSChanwoo Choi 		return false;
315792e7e9eSChanwoo Choi 
316792e7e9eSChanwoo Choi 	/* Check whether a specific extcon id supports the property or not. */
317792e7e9eSChanwoo Choi 	return !!(extcon_info[id].type & type);
318792e7e9eSChanwoo Choi }
319792e7e9eSChanwoo Choi 
is_extcon_property_capability(struct extcon_dev * edev,unsigned int id,int index,unsigned int prop)3207f2a0a16SChanwoo Choi static int is_extcon_property_capability(struct extcon_dev *edev,
3217f2a0a16SChanwoo Choi 				unsigned int id, int index,unsigned int prop)
3227f2a0a16SChanwoo Choi {
3237f2a0a16SChanwoo Choi 	struct extcon_cable *cable;
3247f2a0a16SChanwoo Choi 	int type, ret;
3257f2a0a16SChanwoo Choi 
3267f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not. */
3277f2a0a16SChanwoo Choi 	type = get_extcon_type(prop);
3287f2a0a16SChanwoo Choi 	if (type < 0)
3297f2a0a16SChanwoo Choi 		return type;
3307f2a0a16SChanwoo Choi 
3317f2a0a16SChanwoo Choi 	cable = &edev->cables[index];
3327f2a0a16SChanwoo Choi 
3337f2a0a16SChanwoo Choi 	switch (type) {
3347f2a0a16SChanwoo Choi 	case EXTCON_TYPE_USB:
3357f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
3367f2a0a16SChanwoo Choi 		break;
3377f2a0a16SChanwoo Choi 	case EXTCON_TYPE_CHG:
3387f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
3397f2a0a16SChanwoo Choi 		break;
3407f2a0a16SChanwoo Choi 	case EXTCON_TYPE_JACK:
3417f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
3427f2a0a16SChanwoo Choi 		break;
3437f2a0a16SChanwoo Choi 	case EXTCON_TYPE_DISP:
3447f2a0a16SChanwoo Choi 		ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
3457f2a0a16SChanwoo Choi 		break;
3467f2a0a16SChanwoo Choi 	default:
3477f2a0a16SChanwoo Choi 		ret = -EINVAL;
3487f2a0a16SChanwoo Choi 	}
3497f2a0a16SChanwoo Choi 
3507f2a0a16SChanwoo Choi 	return ret;
3517f2a0a16SChanwoo Choi }
3527f2a0a16SChanwoo Choi 
init_property(struct extcon_dev * edev,unsigned int id,int index)353792e7e9eSChanwoo Choi static void init_property(struct extcon_dev *edev, unsigned int id, int index)
354792e7e9eSChanwoo Choi {
355792e7e9eSChanwoo Choi 	unsigned int type = extcon_info[id].type;
356792e7e9eSChanwoo Choi 	struct extcon_cable *cable = &edev->cables[index];
357792e7e9eSChanwoo Choi 
358792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_USB & type)
359792e7e9eSChanwoo Choi 		memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
360792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_CHG & type)
361792e7e9eSChanwoo Choi 		memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
362792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_JACK & type)
363792e7e9eSChanwoo Choi 		memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
364792e7e9eSChanwoo Choi 	if (EXTCON_TYPE_DISP & type)
365792e7e9eSChanwoo Choi 		memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
366792e7e9eSChanwoo Choi }
367792e7e9eSChanwoo Choi 
state_show(struct device * dev,struct device_attribute * attr,char * buf)368f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr,
369f68a8342SChanwoo Choi 			  char *buf)
370f68a8342SChanwoo Choi {
371f68a8342SChanwoo Choi 	int i, count = 0;
372f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
373f68a8342SChanwoo Choi 
374f68a8342SChanwoo Choi 	if (edev->max_supported == 0)
3756ee0a22eSAndy Shevchenko 		return sysfs_emit(buf, "%u\n", edev->state);
376f68a8342SChanwoo Choi 
3772a9de9c0SChanwoo Choi 	for (i = 0; i < edev->max_supported; i++) {
3786ee0a22eSAndy Shevchenko 		count += sysfs_emit_at(buf, count, "%s=%d\n",
37955e4e2f1SChanwoo Choi 				       extcon_info[edev->supported_cable[i]].name,
38070641a0aSChanwoo Choi 				       !!(edev->state & BIT(i)));
381f68a8342SChanwoo Choi 	}
382f68a8342SChanwoo Choi 
383f68a8342SChanwoo Choi 	return count;
384f68a8342SChanwoo Choi }
3855d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state);
386f68a8342SChanwoo Choi 
name_show(struct device * dev,struct device_attribute * attr,char * buf)387f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr,
388f68a8342SChanwoo Choi 		char *buf)
389f68a8342SChanwoo Choi {
390f68a8342SChanwoo Choi 	struct extcon_dev *edev = dev_get_drvdata(dev);
391f68a8342SChanwoo Choi 
3926ee0a22eSAndy Shevchenko 	return sysfs_emit(buf, "%s\n", edev->name);
393f68a8342SChanwoo Choi }
394f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name);
395f68a8342SChanwoo Choi 
cable_name_show(struct device * dev,struct device_attribute * attr,char * buf)396f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev,
397f68a8342SChanwoo Choi 			       struct device_attribute *attr, char *buf)
398f68a8342SChanwoo Choi {
399f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
400f68a8342SChanwoo Choi 						  attr_name);
4012a9de9c0SChanwoo Choi 	int i = cable->cable_index;
402f68a8342SChanwoo Choi 
4036ee0a22eSAndy Shevchenko 	return sysfs_emit(buf, "%s\n",
40455e4e2f1SChanwoo Choi 			  extcon_info[cable->edev->supported_cable[i]].name);
405f68a8342SChanwoo Choi }
406f68a8342SChanwoo Choi 
cable_state_show(struct device * dev,struct device_attribute * attr,char * buf)407f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev,
408f68a8342SChanwoo Choi 				struct device_attribute *attr, char *buf)
409f68a8342SChanwoo Choi {
410f68a8342SChanwoo Choi 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
411f68a8342SChanwoo Choi 						  attr_state);
412f68a8342SChanwoo Choi 
413be052cc8SRoger Quadros 	int i = cable->cable_index;
414be052cc8SRoger Quadros 
4156ee0a22eSAndy Shevchenko 	return sysfs_emit(buf, "%d\n",
416575c2b86SChanwoo Choi 			  extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
417f68a8342SChanwoo Choi }
418f68a8342SChanwoo Choi 
419f68a8342SChanwoo Choi /**
4206ab6094fSChanwoo Choi  * extcon_sync() - Synchronize the state for an external connector.
4216ab6094fSChanwoo Choi  * @edev:	the extcon device
4226506f6a0SYang Li  * @id:		the unique id indicating an external connector
423f68a8342SChanwoo Choi  *
4246ab6094fSChanwoo Choi  * Note that this function send a notification in order to synchronize
4256ab6094fSChanwoo Choi  * the state and property of an external connector.
4266ab6094fSChanwoo Choi  *
4276ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
428f68a8342SChanwoo Choi  */
extcon_sync(struct extcon_dev * edev,unsigned int id)429ab11af04SChanwoo Choi int extcon_sync(struct extcon_dev *edev, unsigned int id)
430f68a8342SChanwoo Choi {
431f68a8342SChanwoo Choi 	char name_buf[120];
432f68a8342SChanwoo Choi 	char state_buf[120];
433f68a8342SChanwoo Choi 	char *prop_buf;
434f68a8342SChanwoo Choi 	char *envp[3];
435f68a8342SChanwoo Choi 	int env_offset = 0;
436f68a8342SChanwoo Choi 	int length;
437046050f6SChanwoo Choi 	int index;
438ab11af04SChanwoo Choi 	int state;
439f68a8342SChanwoo Choi 	unsigned long flags;
440f68a8342SChanwoo Choi 
4417eae43aeSChanwoo Choi 	if (!edev)
4427eae43aeSChanwoo Choi 		return -EINVAL;
4437eae43aeSChanwoo Choi 
444ab11af04SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
445ab11af04SChanwoo Choi 	if (index < 0)
446ab11af04SChanwoo Choi 		return index;
447ab11af04SChanwoo Choi 
448f68a8342SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
449ab11af04SChanwoo Choi 	state = !!(edev->state & BIT(index));
4508a9dbb77SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
451815429b3SChanwoo Choi 
452815429b3SChanwoo Choi 	/*
453815429b3SChanwoo Choi 	 * Call functions in a raw notifier chain for the specific one
454815429b3SChanwoo Choi 	 * external connector.
455815429b3SChanwoo Choi 	 */
456ab11af04SChanwoo Choi 	raw_notifier_call_chain(&edev->nh[index], state, edev);
457f7a89811SRoger Quadros 
458815429b3SChanwoo Choi 	/*
459815429b3SChanwoo Choi 	 * Call functions in a raw notifier chain for the all supported
460815429b3SChanwoo Choi 	 * external connectors.
461815429b3SChanwoo Choi 	 */
462815429b3SChanwoo Choi 	raw_notifier_call_chain(&edev->nh_all, state, edev);
463815429b3SChanwoo Choi 
4648a9dbb77SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
465f68a8342SChanwoo Choi 	/* This could be in interrupt handler */
466f68a8342SChanwoo Choi 	prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
467ab11af04SChanwoo Choi 	if (!prop_buf) {
468f68a8342SChanwoo Choi 		/* Unlock early before uevent */
469f68a8342SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
470f68a8342SChanwoo Choi 
471f68a8342SChanwoo Choi 		dev_err(&edev->dev, "out of memory in extcon_set_state\n");
472f68a8342SChanwoo Choi 		kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
473f68a8342SChanwoo Choi 
474e7d9dd5aSPan Bian 		return -ENOMEM;
475f68a8342SChanwoo Choi 	}
476f68a8342SChanwoo Choi 
477ab11af04SChanwoo Choi 	length = name_show(&edev->dev, NULL, prop_buf);
478ab11af04SChanwoo Choi 	if (length > 0) {
479ab11af04SChanwoo Choi 		if (prop_buf[length - 1] == '\n')
480ab11af04SChanwoo Choi 			prop_buf[length - 1] = 0;
481ab11af04SChanwoo Choi 		snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
482ab11af04SChanwoo Choi 		envp[env_offset++] = name_buf;
483ab11af04SChanwoo Choi 	}
484ab11af04SChanwoo Choi 
485ab11af04SChanwoo Choi 	length = state_show(&edev->dev, NULL, prop_buf);
486ab11af04SChanwoo Choi 	if (length > 0) {
487ab11af04SChanwoo Choi 		if (prop_buf[length - 1] == '\n')
488ab11af04SChanwoo Choi 			prop_buf[length - 1] = 0;
489ab11af04SChanwoo Choi 		snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
490ab11af04SChanwoo Choi 		envp[env_offset++] = state_buf;
491ab11af04SChanwoo Choi 	}
492ab11af04SChanwoo Choi 	envp[env_offset] = NULL;
493ab11af04SChanwoo Choi 
494ab11af04SChanwoo Choi 	/* Unlock early before uevent */
495ab11af04SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
496ab11af04SChanwoo Choi 	kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
497ab11af04SChanwoo Choi 	free_page((unsigned long)prop_buf);
498ab11af04SChanwoo Choi 
499ab11af04SChanwoo Choi 	return 0;
500ab11af04SChanwoo Choi }
501ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_sync);
502ab11af04SChanwoo Choi 
503f68a8342SChanwoo Choi /**
5046ab6094fSChanwoo Choi  * extcon_get_state() - Get the state of an external connector.
5056ab6094fSChanwoo Choi  * @edev:	the extcon device
5066ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5076ab6094fSChanwoo Choi  *
5086ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
509f68a8342SChanwoo Choi  */
extcon_get_state(struct extcon_dev * edev,const unsigned int id)510575c2b86SChanwoo Choi int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
511f68a8342SChanwoo Choi {
512575c2b86SChanwoo Choi 	int index, state;
513575c2b86SChanwoo Choi 	unsigned long flags;
5142a9de9c0SChanwoo Choi 
5157eae43aeSChanwoo Choi 	if (!edev)
5167eae43aeSChanwoo Choi 		return -EINVAL;
5177eae43aeSChanwoo Choi 
5182a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
5192a9de9c0SChanwoo Choi 	if (index < 0)
5202a9de9c0SChanwoo Choi 		return index;
5212a9de9c0SChanwoo Choi 
522575c2b86SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
523575c2b86SChanwoo Choi 	state = is_extcon_attached(edev, index);
524575c2b86SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
525f68a8342SChanwoo Choi 
526575c2b86SChanwoo Choi 	return state;
527f68a8342SChanwoo Choi }
528575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_state);
529f68a8342SChanwoo Choi 
530f68a8342SChanwoo Choi /**
5316ab6094fSChanwoo Choi  * extcon_set_state() - Set the state of an external connector.
5326ab6094fSChanwoo Choi  * @edev:	the extcon device
5336ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5346ab6094fSChanwoo Choi  * @state:	the new state of an external connector.
5356ab6094fSChanwoo Choi  *		the default semantics is true: attached / false: detached.
536ab11af04SChanwoo Choi  *
5376ab6094fSChanwoo Choi  * Note that this function set the state of an external connector without
5386ab6094fSChanwoo Choi  * a notification. To synchronize the state of an external connector,
5396ab6094fSChanwoo Choi  * have to use extcon_set_state_sync() and extcon_sync().
5406ab6094fSChanwoo Choi  *
5416ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
542f68a8342SChanwoo Choi  */
extcon_set_state(struct extcon_dev * edev,unsigned int id,bool state)5436ab6094fSChanwoo Choi int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
544f68a8342SChanwoo Choi {
545ab11af04SChanwoo Choi 	unsigned long flags;
546ab11af04SChanwoo Choi 	int index, ret = 0;
547f68a8342SChanwoo Choi 
5487eae43aeSChanwoo Choi 	if (!edev)
5497eae43aeSChanwoo Choi 		return -EINVAL;
5507eae43aeSChanwoo Choi 
5512a9de9c0SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
5522a9de9c0SChanwoo Choi 	if (index < 0)
5532a9de9c0SChanwoo Choi 		return index;
5542a9de9c0SChanwoo Choi 
555ab11af04SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
556ab11af04SChanwoo Choi 
557ab11af04SChanwoo Choi 	/* Check whether the external connector's state is changed. */
5586ab6094fSChanwoo Choi 	if (!is_extcon_changed(edev, index, state))
559ab11af04SChanwoo Choi 		goto out;
560ab11af04SChanwoo Choi 
561ab11af04SChanwoo Choi 	if (check_mutually_exclusive(edev,
5626ab6094fSChanwoo Choi 		(edev->state & ~BIT(index)) | (state & BIT(index)))) {
563ab11af04SChanwoo Choi 		ret = -EPERM;
564ab11af04SChanwoo Choi 		goto out;
565ab11af04SChanwoo Choi 	}
566ab11af04SChanwoo Choi 
567792e7e9eSChanwoo Choi 	/*
568792e7e9eSChanwoo Choi 	 * Initialize the value of extcon property before setting
569792e7e9eSChanwoo Choi 	 * the detached state for an external connector.
570792e7e9eSChanwoo Choi 	 */
5716ab6094fSChanwoo Choi 	if (!state)
572792e7e9eSChanwoo Choi 		init_property(edev, id, index);
573792e7e9eSChanwoo Choi 
5746ab6094fSChanwoo Choi 	/* Update the state for an external connector. */
5756ab6094fSChanwoo Choi 	if (state)
576ab11af04SChanwoo Choi 		edev->state |= BIT(index);
577ab11af04SChanwoo Choi 	else
578ab11af04SChanwoo Choi 		edev->state &= ~(BIT(index));
579ab11af04SChanwoo Choi out:
580ab11af04SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
581ab11af04SChanwoo Choi 
582ab11af04SChanwoo Choi 	return ret;
583f68a8342SChanwoo Choi }
584575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state);
585f68a8342SChanwoo Choi 
586f68a8342SChanwoo Choi /**
5876ab6094fSChanwoo Choi  * extcon_set_state_sync() - Set the state of an external connector with sync.
5886ab6094fSChanwoo Choi  * @edev:	the extcon device
5896ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
5906ab6094fSChanwoo Choi  * @state:	the new state of external connector.
5916ab6094fSChanwoo Choi  *		the default semantics is true: attached / false: detached.
592ab11af04SChanwoo Choi  *
5936ab6094fSChanwoo Choi  * Note that this function set the state of external connector
5946ab6094fSChanwoo Choi  * and synchronize the state by sending a notification.
5956ab6094fSChanwoo Choi  *
5966ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
597ab11af04SChanwoo Choi  */
extcon_set_state_sync(struct extcon_dev * edev,unsigned int id,bool state)5986ab6094fSChanwoo Choi int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
599ab11af04SChanwoo Choi {
6002da3db7fSAlexander Stein 	int ret;
601ab11af04SChanwoo Choi 
6026ab6094fSChanwoo Choi 	ret = extcon_set_state(edev, id, state);
603ab11af04SChanwoo Choi 	if (ret < 0)
604ab11af04SChanwoo Choi 		return ret;
605ab11af04SChanwoo Choi 
606ab11af04SChanwoo Choi 	return extcon_sync(edev, id);
607ab11af04SChanwoo Choi }
608ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state_sync);
609ab11af04SChanwoo Choi 
610ab11af04SChanwoo Choi /**
6116ab6094fSChanwoo Choi  * extcon_get_property() - Get the property value of an external connector.
6126ab6094fSChanwoo Choi  * @edev:	the extcon device
6136ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
6146ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
6156ab6094fSChanwoo Choi  * @prop_val:	the pointer which store the value of extcon property
616792e7e9eSChanwoo Choi  *
6176ab6094fSChanwoo Choi  * Note that when getting the property value of external connector,
6186ab6094fSChanwoo Choi  * the external connector should be attached. If detached state, function
6196ab6094fSChanwoo Choi  * return 0 without property value. Also, the each property should be
6206ab6094fSChanwoo Choi  * included in the list of supported properties according to extcon type.
621792e7e9eSChanwoo Choi  *
6226ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
623792e7e9eSChanwoo Choi  */
extcon_get_property(struct extcon_dev * edev,unsigned int id,unsigned int prop,union extcon_property_value * prop_val)624792e7e9eSChanwoo Choi int extcon_get_property(struct extcon_dev *edev, unsigned int id,
625792e7e9eSChanwoo Choi 				unsigned int prop,
626792e7e9eSChanwoo Choi 				union extcon_property_value *prop_val)
627792e7e9eSChanwoo Choi {
628792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
629792e7e9eSChanwoo Choi 	unsigned long flags;
630792e7e9eSChanwoo Choi 	int index, ret = 0;
631792e7e9eSChanwoo Choi 
632cff7499dSAndy Shevchenko 	*prop_val = (union extcon_property_value){0};
633792e7e9eSChanwoo Choi 
634792e7e9eSChanwoo Choi 	if (!edev)
635792e7e9eSChanwoo Choi 		return -EINVAL;
636792e7e9eSChanwoo Choi 
637792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
638792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
639792e7e9eSChanwoo Choi 		return -EINVAL;
640792e7e9eSChanwoo Choi 
641792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
642792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
643792e7e9eSChanwoo Choi 	if (index < 0)
644792e7e9eSChanwoo Choi 		return index;
645792e7e9eSChanwoo Choi 
646792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
647792e7e9eSChanwoo Choi 
6487f2a0a16SChanwoo Choi 	/* Check whether the property is available or not. */
6497f2a0a16SChanwoo Choi 	if (!is_extcon_property_capability(edev, id, index, prop)) {
6507f2a0a16SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
6517f2a0a16SChanwoo Choi 		return -EPERM;
6527f2a0a16SChanwoo Choi 	}
6537f2a0a16SChanwoo Choi 
654792e7e9eSChanwoo Choi 	/*
655792e7e9eSChanwoo Choi 	 * Check whether the external connector is attached.
656792e7e9eSChanwoo Choi 	 * If external connector is detached, the user can not
657792e7e9eSChanwoo Choi 	 * get the property value.
658792e7e9eSChanwoo Choi 	 */
659792e7e9eSChanwoo Choi 	if (!is_extcon_attached(edev, index)) {
660792e7e9eSChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
661792e7e9eSChanwoo Choi 		return 0;
662792e7e9eSChanwoo Choi 	}
663792e7e9eSChanwoo Choi 
664792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
665792e7e9eSChanwoo Choi 
666792e7e9eSChanwoo Choi 	/* Get the property value according to extcon type */
667792e7e9eSChanwoo Choi 	switch (prop) {
668792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
669792e7e9eSChanwoo Choi 		*prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
670792e7e9eSChanwoo Choi 		break;
671792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
672792e7e9eSChanwoo Choi 		*prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
673792e7e9eSChanwoo Choi 		break;
674792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
675792e7e9eSChanwoo Choi 		*prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
676792e7e9eSChanwoo Choi 		break;
677792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
678792e7e9eSChanwoo Choi 		*prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
679792e7e9eSChanwoo Choi 		break;
680792e7e9eSChanwoo Choi 	default:
681792e7e9eSChanwoo Choi 		ret = -EINVAL;
682792e7e9eSChanwoo Choi 		break;
683792e7e9eSChanwoo Choi 	}
684792e7e9eSChanwoo Choi 
685792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
686792e7e9eSChanwoo Choi 
687792e7e9eSChanwoo Choi 	return ret;
688792e7e9eSChanwoo Choi }
689792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property);
690792e7e9eSChanwoo Choi 
691792e7e9eSChanwoo Choi /**
6926ab6094fSChanwoo Choi  * extcon_set_property() - Set the property value of an external connector.
6936ab6094fSChanwoo Choi  * @edev:	the extcon device
6946ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
6956ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
6966ab6094fSChanwoo Choi  * @prop_val:	the pointer including the new value of extcon property
697792e7e9eSChanwoo Choi  *
6986ab6094fSChanwoo Choi  * Note that each property should be included in the list of supported
6996ab6094fSChanwoo Choi  * properties according to the extcon type.
700792e7e9eSChanwoo Choi  *
7016ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
702792e7e9eSChanwoo Choi  */
extcon_set_property(struct extcon_dev * edev,unsigned int id,unsigned int prop,union extcon_property_value prop_val)703792e7e9eSChanwoo Choi int extcon_set_property(struct extcon_dev *edev, unsigned int id,
704792e7e9eSChanwoo Choi 				unsigned int prop,
705792e7e9eSChanwoo Choi 				union extcon_property_value prop_val)
706792e7e9eSChanwoo Choi {
707792e7e9eSChanwoo Choi 	struct extcon_cable *cable;
708792e7e9eSChanwoo Choi 	unsigned long flags;
709792e7e9eSChanwoo Choi 	int index, ret = 0;
710792e7e9eSChanwoo Choi 
711792e7e9eSChanwoo Choi 	if (!edev)
712792e7e9eSChanwoo Choi 		return -EINVAL;
713792e7e9eSChanwoo Choi 
714792e7e9eSChanwoo Choi 	/* Check whether the property is supported or not */
715792e7e9eSChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
716792e7e9eSChanwoo Choi 		return -EINVAL;
717792e7e9eSChanwoo Choi 
718792e7e9eSChanwoo Choi 	/* Find the cable index of external connector by using id */
719792e7e9eSChanwoo Choi 	index = find_cable_index_by_id(edev, id);
720792e7e9eSChanwoo Choi 	if (index < 0)
721792e7e9eSChanwoo Choi 		return index;
722792e7e9eSChanwoo Choi 
723792e7e9eSChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
724792e7e9eSChanwoo Choi 
7257f2a0a16SChanwoo Choi 	/* Check whether the property is available or not. */
7267f2a0a16SChanwoo Choi 	if (!is_extcon_property_capability(edev, id, index, prop)) {
7277f2a0a16SChanwoo Choi 		spin_unlock_irqrestore(&edev->lock, flags);
7287f2a0a16SChanwoo Choi 		return -EPERM;
7297f2a0a16SChanwoo Choi 	}
7307f2a0a16SChanwoo Choi 
731792e7e9eSChanwoo Choi 	cable = &edev->cables[index];
732792e7e9eSChanwoo Choi 
733792e7e9eSChanwoo Choi 	/* Set the property value according to extcon type */
734792e7e9eSChanwoo Choi 	switch (prop) {
735792e7e9eSChanwoo Choi 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
736792e7e9eSChanwoo Choi 		cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
737792e7e9eSChanwoo Choi 		break;
738792e7e9eSChanwoo Choi 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
739792e7e9eSChanwoo Choi 		cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
740792e7e9eSChanwoo Choi 		break;
741792e7e9eSChanwoo Choi 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
742792e7e9eSChanwoo Choi 		cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
743792e7e9eSChanwoo Choi 		break;
744792e7e9eSChanwoo Choi 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
745792e7e9eSChanwoo Choi 		cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
746792e7e9eSChanwoo Choi 		break;
747792e7e9eSChanwoo Choi 	default:
748792e7e9eSChanwoo Choi 		ret = -EINVAL;
749792e7e9eSChanwoo Choi 		break;
750792e7e9eSChanwoo Choi 	}
751792e7e9eSChanwoo Choi 
752792e7e9eSChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
753792e7e9eSChanwoo Choi 
754792e7e9eSChanwoo Choi 	return ret;
755792e7e9eSChanwoo Choi }
756792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property);
757792e7e9eSChanwoo Choi 
758792e7e9eSChanwoo Choi /**
7596ab6094fSChanwoo Choi  * extcon_set_property_sync() - Set property of an external connector with sync.
7606506f6a0SYang Li  * @edev:	the extcon device
7616506f6a0SYang Li  * @id:		the unique id indicating an external connector
7626506f6a0SYang Li  * @prop:	the property id indicating an extcon property
7636ab6094fSChanwoo Choi  * @prop_val:	the pointer including the new value of extcon property
764ab11af04SChanwoo Choi  *
7656ab6094fSChanwoo Choi  * Note that when setting the property value of external connector,
7666ab6094fSChanwoo Choi  * the external connector should be attached. The each property should
7676ab6094fSChanwoo Choi  * be included in the list of supported properties according to extcon type.
768ab11af04SChanwoo Choi  *
7696ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
770ab11af04SChanwoo Choi  */
extcon_set_property_sync(struct extcon_dev * edev,unsigned int id,unsigned int prop,union extcon_property_value prop_val)771ab11af04SChanwoo Choi int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
772ab11af04SChanwoo Choi 				unsigned int prop,
773ab11af04SChanwoo Choi 				union extcon_property_value prop_val)
774ab11af04SChanwoo Choi {
775ab11af04SChanwoo Choi 	int ret;
776ab11af04SChanwoo Choi 
777ab11af04SChanwoo Choi 	ret = extcon_set_property(edev, id, prop, prop_val);
778ab11af04SChanwoo Choi 	if (ret < 0)
779ab11af04SChanwoo Choi 		return ret;
780ab11af04SChanwoo Choi 
781ab11af04SChanwoo Choi 	return extcon_sync(edev, id);
782ab11af04SChanwoo Choi }
783ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_sync);
784ab11af04SChanwoo Choi 
785ab11af04SChanwoo Choi /**
7866ab6094fSChanwoo Choi  * extcon_get_property_capability() - Get the capability of the property
7876ab6094fSChanwoo Choi  *					for an external connector.
7886ab6094fSChanwoo Choi  * @edev:	the extcon device
7896ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
7906ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
7917f2a0a16SChanwoo Choi  *
7927f2a0a16SChanwoo Choi  * Returns 1 if the property is available or 0 if not available.
7937f2a0a16SChanwoo Choi  */
extcon_get_property_capability(struct extcon_dev * edev,unsigned int id,unsigned int prop)7947f2a0a16SChanwoo Choi int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
7957f2a0a16SChanwoo Choi 					unsigned int prop)
7967f2a0a16SChanwoo Choi {
7977f2a0a16SChanwoo Choi 	int index;
7987f2a0a16SChanwoo Choi 
7997f2a0a16SChanwoo Choi 	if (!edev)
8007f2a0a16SChanwoo Choi 		return -EINVAL;
8017f2a0a16SChanwoo Choi 
8027f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not */
8037f2a0a16SChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
8047f2a0a16SChanwoo Choi 		return -EINVAL;
8057f2a0a16SChanwoo Choi 
8067f2a0a16SChanwoo Choi 	/* Find the cable index of external connector by using id */
8077f2a0a16SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
8087f2a0a16SChanwoo Choi 	if (index < 0)
8097f2a0a16SChanwoo Choi 		return index;
8107f2a0a16SChanwoo Choi 
8117f2a0a16SChanwoo Choi 	return is_extcon_property_capability(edev, id, index, prop);
8127f2a0a16SChanwoo Choi }
8137f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property_capability);
8147f2a0a16SChanwoo Choi 
8157f2a0a16SChanwoo Choi /**
8166ab6094fSChanwoo Choi  * extcon_set_property_capability() - Set the capability of the property
8176ab6094fSChanwoo Choi  *					for an external connector.
8186ab6094fSChanwoo Choi  * @edev:	the extcon device
8196ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
8206ab6094fSChanwoo Choi  * @prop:	the property id indicating an extcon property
8217f2a0a16SChanwoo Choi  *
8226ab6094fSChanwoo Choi  * Note that this function set the capability of the property
8236ab6094fSChanwoo Choi  * for an external connector in order to mark the bit in capability
8246ab6094fSChanwoo Choi  * bitmap which mean the available state of the property.
8257f2a0a16SChanwoo Choi  *
8266ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
8277f2a0a16SChanwoo Choi  */
extcon_set_property_capability(struct extcon_dev * edev,unsigned int id,unsigned int prop)8287f2a0a16SChanwoo Choi int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
8297f2a0a16SChanwoo Choi 					unsigned int prop)
8307f2a0a16SChanwoo Choi {
8317f2a0a16SChanwoo Choi 	struct extcon_cable *cable;
8327f2a0a16SChanwoo Choi 	int index, type, ret = 0;
8337f2a0a16SChanwoo Choi 
8347f2a0a16SChanwoo Choi 	if (!edev)
8357f2a0a16SChanwoo Choi 		return -EINVAL;
8367f2a0a16SChanwoo Choi 
8377f2a0a16SChanwoo Choi 	/* Check whether the property is supported or not. */
8387f2a0a16SChanwoo Choi 	if (!is_extcon_property_supported(id, prop))
8397f2a0a16SChanwoo Choi 		return -EINVAL;
8407f2a0a16SChanwoo Choi 
8417f2a0a16SChanwoo Choi 	/* Find the cable index of external connector by using id. */
8427f2a0a16SChanwoo Choi 	index = find_cable_index_by_id(edev, id);
8437f2a0a16SChanwoo Choi 	if (index < 0)
8447f2a0a16SChanwoo Choi 		return index;
8457f2a0a16SChanwoo Choi 
8467f2a0a16SChanwoo Choi 	type = get_extcon_type(prop);
8477f2a0a16SChanwoo Choi 	if (type < 0)
8487f2a0a16SChanwoo Choi 		return type;
8497f2a0a16SChanwoo Choi 
8507f2a0a16SChanwoo Choi 	cable = &edev->cables[index];
8517f2a0a16SChanwoo Choi 
8527f2a0a16SChanwoo Choi 	switch (type) {
8537f2a0a16SChanwoo Choi 	case EXTCON_TYPE_USB:
8547f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
8557f2a0a16SChanwoo Choi 		break;
8567f2a0a16SChanwoo Choi 	case EXTCON_TYPE_CHG:
8577f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
8587f2a0a16SChanwoo Choi 		break;
8597f2a0a16SChanwoo Choi 	case EXTCON_TYPE_JACK:
8607f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
8617f2a0a16SChanwoo Choi 		break;
8627f2a0a16SChanwoo Choi 	case EXTCON_TYPE_DISP:
8637f2a0a16SChanwoo Choi 		__set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
8647f2a0a16SChanwoo Choi 		break;
8657f2a0a16SChanwoo Choi 	default:
8667f2a0a16SChanwoo Choi 		ret = -EINVAL;
8677f2a0a16SChanwoo Choi 	}
8687f2a0a16SChanwoo Choi 
8697f2a0a16SChanwoo Choi 	return ret;
8707f2a0a16SChanwoo Choi }
8717f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_capability);
8727f2a0a16SChanwoo Choi 
8737f2a0a16SChanwoo Choi /**
8746ab6094fSChanwoo Choi  * extcon_get_extcon_dev() - Get the extcon device instance from the name.
8756ab6094fSChanwoo Choi  * @extcon_name:	the extcon name provided with extcon_dev_register()
8766ab6094fSChanwoo Choi  *
8776ab6094fSChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
87858e4a2d2SDan Carpenter  * NOTE: This function returns -EPROBE_DEFER so it may only be called from
87958e4a2d2SDan Carpenter  * probe() functions.
880f68a8342SChanwoo Choi  */
extcon_get_extcon_dev(const char * extcon_name)881f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
882f68a8342SChanwoo Choi {
883f68a8342SChanwoo Choi 	struct extcon_dev *sd;
884f68a8342SChanwoo Choi 
8857eae43aeSChanwoo Choi 	if (!extcon_name)
8867eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
8877eae43aeSChanwoo Choi 
888f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
889f68a8342SChanwoo Choi 	list_for_each_entry(sd, &extcon_dev_list, entry) {
890f68a8342SChanwoo Choi 		if (!strcmp(sd->name, extcon_name))
891f68a8342SChanwoo Choi 			goto out;
892f68a8342SChanwoo Choi 	}
89358e4a2d2SDan Carpenter 	sd = ERR_PTR(-EPROBE_DEFER);
894f68a8342SChanwoo Choi out:
895f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
896f68a8342SChanwoo Choi 	return sd;
897f68a8342SChanwoo Choi }
898f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
899f68a8342SChanwoo Choi 
900f68a8342SChanwoo Choi /**
9016ab6094fSChanwoo Choi  * extcon_register_notifier() - Register a notifier block to get notified by
9026ab6094fSChanwoo Choi  *				any state changes from the extcon.
9036ab6094fSChanwoo Choi  * @edev:	the extcon device
9046ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
9056ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
906f68a8342SChanwoo Choi  *
907f68a8342SChanwoo Choi  * Note that the second parameter given to the callback of nb (val) is
9086ab6094fSChanwoo Choi  * the current state of an external connector and the third pameter
9096ab6094fSChanwoo Choi  * is the pointer of extcon device.
9106ab6094fSChanwoo Choi  *
9116ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
912f68a8342SChanwoo Choi  */
extcon_register_notifier(struct extcon_dev * edev,unsigned int id,struct notifier_block * nb)91373b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
914f68a8342SChanwoo Choi 			     struct notifier_block *nb)
915f68a8342SChanwoo Choi {
91666bee35fSHans de Goede 	unsigned long flags;
9171fa80f18SColin Ian King 	int ret, idx;
918046050f6SChanwoo Choi 
91901b4c9a1SChanwoo Choi 	if (!edev || !nb)
9207eae43aeSChanwoo Choi 		return -EINVAL;
9217eae43aeSChanwoo Choi 
922046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
923a05f44c8SStephen Boyd 	if (idx < 0)
924a05f44c8SStephen Boyd 		return idx;
92566bee35fSHans de Goede 
92666bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
927046050f6SChanwoo Choi 	ret = raw_notifier_chain_register(&edev->nh[idx], nb);
92866bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
92966bee35fSHans de Goede 
93066bee35fSHans de Goede 	return ret;
931f68a8342SChanwoo Choi }
932f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier);
933f68a8342SChanwoo Choi 
934f68a8342SChanwoo Choi /**
9356ab6094fSChanwoo Choi  * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
9366ab6094fSChanwoo Choi  * @edev:	the extcon device
9376ab6094fSChanwoo Choi  * @id:		the unique id indicating an external connector
9386ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
9396ab6094fSChanwoo Choi  *
9406ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
941f68a8342SChanwoo Choi  */
extcon_unregister_notifier(struct extcon_dev * edev,unsigned int id,struct notifier_block * nb)94273b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
943f68a8342SChanwoo Choi 				struct notifier_block *nb)
944f68a8342SChanwoo Choi {
94566bee35fSHans de Goede 	unsigned long flags;
946046050f6SChanwoo Choi 	int ret, idx;
947046050f6SChanwoo Choi 
9487eae43aeSChanwoo Choi 	if (!edev || !nb)
9497eae43aeSChanwoo Choi 		return -EINVAL;
9507eae43aeSChanwoo Choi 
951046050f6SChanwoo Choi 	idx = find_cable_index_by_id(edev, id);
952a05f44c8SStephen Boyd 	if (idx < 0)
953a05f44c8SStephen Boyd 		return idx;
95466bee35fSHans de Goede 
95566bee35fSHans de Goede 	spin_lock_irqsave(&edev->lock, flags);
956046050f6SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
95766bee35fSHans de Goede 	spin_unlock_irqrestore(&edev->lock, flags);
95866bee35fSHans de Goede 
95966bee35fSHans de Goede 	return ret;
960f68a8342SChanwoo Choi }
961f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
962f68a8342SChanwoo Choi 
963815429b3SChanwoo Choi /**
9646ab6094fSChanwoo Choi  * extcon_register_notifier_all() - Register a notifier block for all connectors.
9656ab6094fSChanwoo Choi  * @edev:	the extcon device
9666ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
967815429b3SChanwoo Choi  *
9686ab6094fSChanwoo Choi  * Note that this function registers a notifier block in order to receive
9696ab6094fSChanwoo Choi  * the state change of all supported external connectors from extcon device.
970826a47e9SMarkus Elfring  * And the second parameter given to the callback of nb (val) is
9716ab6094fSChanwoo Choi  * the current state and the third pameter is the pointer of extcon device.
972815429b3SChanwoo Choi  *
9736ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
974815429b3SChanwoo Choi  */
extcon_register_notifier_all(struct extcon_dev * edev,struct notifier_block * nb)975815429b3SChanwoo Choi int extcon_register_notifier_all(struct extcon_dev *edev,
976815429b3SChanwoo Choi 				struct notifier_block *nb)
977815429b3SChanwoo Choi {
978815429b3SChanwoo Choi 	unsigned long flags;
979815429b3SChanwoo Choi 	int ret;
980815429b3SChanwoo Choi 
981815429b3SChanwoo Choi 	if (!edev || !nb)
982815429b3SChanwoo Choi 		return -EINVAL;
983815429b3SChanwoo Choi 
984815429b3SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
985815429b3SChanwoo Choi 	ret = raw_notifier_chain_register(&edev->nh_all, nb);
986815429b3SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
987815429b3SChanwoo Choi 
988815429b3SChanwoo Choi 	return ret;
989815429b3SChanwoo Choi }
990815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
991815429b3SChanwoo Choi 
992815429b3SChanwoo Choi /**
993815429b3SChanwoo Choi  * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
9946ab6094fSChanwoo Choi  * @edev:	the extcon device
9956ab6094fSChanwoo Choi  * @nb:		a notifier block to be registered
996815429b3SChanwoo Choi  *
9976ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
998815429b3SChanwoo Choi  */
extcon_unregister_notifier_all(struct extcon_dev * edev,struct notifier_block * nb)999815429b3SChanwoo Choi int extcon_unregister_notifier_all(struct extcon_dev *edev,
1000815429b3SChanwoo Choi 				struct notifier_block *nb)
1001815429b3SChanwoo Choi {
1002815429b3SChanwoo Choi 	unsigned long flags;
1003815429b3SChanwoo Choi 	int ret;
1004815429b3SChanwoo Choi 
1005815429b3SChanwoo Choi 	if (!edev || !nb)
1006815429b3SChanwoo Choi 		return -EINVAL;
1007815429b3SChanwoo Choi 
1008815429b3SChanwoo Choi 	spin_lock_irqsave(&edev->lock, flags);
1009815429b3SChanwoo Choi 	ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
1010815429b3SChanwoo Choi 	spin_unlock_irqrestore(&edev->lock, flags);
1011815429b3SChanwoo Choi 
1012815429b3SChanwoo Choi 	return ret;
1013815429b3SChanwoo Choi }
1014815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
1015815429b3SChanwoo Choi 
1016f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = {
1017f68a8342SChanwoo Choi 	&dev_attr_state.attr,
1018f68a8342SChanwoo Choi 	&dev_attr_name.attr,
1019f68a8342SChanwoo Choi 	NULL,
1020f68a8342SChanwoo Choi };
1021f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon);
1022f68a8342SChanwoo Choi 
create_extcon_class(void)1023f68a8342SChanwoo Choi static int create_extcon_class(void)
1024f68a8342SChanwoo Choi {
10257f4c9bc2SBumwoo Lee 	if (extcon_class)
10267f4c9bc2SBumwoo Lee 		return 0;
10277f4c9bc2SBumwoo Lee 
10281aaba11dSGreg Kroah-Hartman 	extcon_class = class_create("extcon");
1029f68a8342SChanwoo Choi 	if (IS_ERR(extcon_class))
1030f68a8342SChanwoo Choi 		return PTR_ERR(extcon_class);
1031f68a8342SChanwoo Choi 	extcon_class->dev_groups = extcon_groups;
1032f68a8342SChanwoo Choi 
1033f68a8342SChanwoo Choi 	return 0;
1034f68a8342SChanwoo Choi }
1035f68a8342SChanwoo Choi 
extcon_dev_release(struct device * dev)1036f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev)
1037f68a8342SChanwoo Choi {
1038f68a8342SChanwoo Choi }
1039f68a8342SChanwoo Choi 
1040f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive";
dummy_sysfs_dev_release(struct device * dev)1041f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev)
1042f68a8342SChanwoo Choi {
1043f68a8342SChanwoo Choi }
1044f68a8342SChanwoo Choi 
1045f68a8342SChanwoo Choi /*
1046f68a8342SChanwoo Choi  * extcon_dev_allocate() - Allocate the memory of extcon device.
10476ab6094fSChanwoo Choi  * @supported_cable:	the array of the supported external connectors
10486ab6094fSChanwoo Choi  *			ending with EXTCON_NONE.
1049f68a8342SChanwoo Choi  *
10506ab6094fSChanwoo Choi  * Note that this function allocates the memory for extcon device
10516ab6094fSChanwoo Choi  * and initialize default setting for the extcon device.
1052f68a8342SChanwoo Choi  *
10536ab6094fSChanwoo Choi  * Returns the pointer memory of allocated extcon_dev if success
10546ab6094fSChanwoo Choi  * or ERR_PTR(err) if fail.
1055f68a8342SChanwoo Choi  */
extcon_dev_allocate(const unsigned int * supported_cable)105673b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1057f68a8342SChanwoo Choi {
1058f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1059f68a8342SChanwoo Choi 
10607eae43aeSChanwoo Choi 	if (!supported_cable)
10617eae43aeSChanwoo Choi 		return ERR_PTR(-EINVAL);
10627eae43aeSChanwoo Choi 
1063f68a8342SChanwoo Choi 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1064f68a8342SChanwoo Choi 	if (!edev)
1065f68a8342SChanwoo Choi 		return ERR_PTR(-ENOMEM);
1066f68a8342SChanwoo Choi 
1067f68a8342SChanwoo Choi 	edev->max_supported = 0;
1068f68a8342SChanwoo Choi 	edev->supported_cable = supported_cable;
1069f68a8342SChanwoo Choi 
1070f68a8342SChanwoo Choi 	return edev;
1071f68a8342SChanwoo Choi }
1072f68a8342SChanwoo Choi 
1073f68a8342SChanwoo Choi /*
1074f68a8342SChanwoo Choi  * extcon_dev_free() - Free the memory of extcon device.
10756ab6094fSChanwoo Choi  * @edev:	the extcon device
1076f68a8342SChanwoo Choi  */
extcon_dev_free(struct extcon_dev * edev)1077f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev)
1078f68a8342SChanwoo Choi {
1079f68a8342SChanwoo Choi 	kfree(edev);
1080f68a8342SChanwoo Choi }
1081f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free);
1082f68a8342SChanwoo Choi 
1083f68a8342SChanwoo Choi /**
10843d9138e5SBumwoo Lee  * extcon_alloc_cables() - alloc the cables for extcon device
10853d9138e5SBumwoo Lee  * @edev:	extcon device which has cables
10863d9138e5SBumwoo Lee  *
10873d9138e5SBumwoo Lee  * Returns 0 if success or error number if fail.
10883d9138e5SBumwoo Lee  */
extcon_alloc_cables(struct extcon_dev * edev)10893d9138e5SBumwoo Lee static int extcon_alloc_cables(struct extcon_dev *edev)
10903d9138e5SBumwoo Lee {
10913d9138e5SBumwoo Lee 	int index;
10923d9138e5SBumwoo Lee 	char *str;
10933d9138e5SBumwoo Lee 	struct extcon_cable *cable;
10943d9138e5SBumwoo Lee 
10953d9138e5SBumwoo Lee 	if (!edev)
10963d9138e5SBumwoo Lee 		return -EINVAL;
10973d9138e5SBumwoo Lee 
10983d9138e5SBumwoo Lee 	if (!edev->max_supported)
10993d9138e5SBumwoo Lee 		return 0;
11003d9138e5SBumwoo Lee 
1101ef753fb4SAndy Shevchenko 	edev->cables = kcalloc(edev->max_supported, sizeof(*edev->cables),
11023d9138e5SBumwoo Lee 			       GFP_KERNEL);
11033d9138e5SBumwoo Lee 	if (!edev->cables)
11043d9138e5SBumwoo Lee 		return -ENOMEM;
11053d9138e5SBumwoo Lee 
11063d9138e5SBumwoo Lee 	for (index = 0; index < edev->max_supported; index++) {
11073d9138e5SBumwoo Lee 		cable = &edev->cables[index];
11083d9138e5SBumwoo Lee 
11093d9138e5SBumwoo Lee 		str = kasprintf(GFP_KERNEL, "cable.%d", index);
11103d9138e5SBumwoo Lee 		if (!str) {
11113d9138e5SBumwoo Lee 			for (index--; index >= 0; index--) {
11123d9138e5SBumwoo Lee 				cable = &edev->cables[index];
11133d9138e5SBumwoo Lee 				kfree(cable->attr_g.name);
11143d9138e5SBumwoo Lee 			}
11153d9138e5SBumwoo Lee 
11163d9138e5SBumwoo Lee 			kfree(edev->cables);
11173d9138e5SBumwoo Lee 			return -ENOMEM;
11183d9138e5SBumwoo Lee 		}
11193d9138e5SBumwoo Lee 
11203d9138e5SBumwoo Lee 		cable->edev = edev;
11213d9138e5SBumwoo Lee 		cable->cable_index = index;
11223d9138e5SBumwoo Lee 		cable->attrs[0] = &cable->attr_name.attr;
11233d9138e5SBumwoo Lee 		cable->attrs[1] = &cable->attr_state.attr;
11243d9138e5SBumwoo Lee 		cable->attrs[2] = NULL;
11253d9138e5SBumwoo Lee 		cable->attr_g.name = str;
11263d9138e5SBumwoo Lee 		cable->attr_g.attrs = cable->attrs;
11273d9138e5SBumwoo Lee 
11283d9138e5SBumwoo Lee 		sysfs_attr_init(&cable->attr_name.attr);
11293d9138e5SBumwoo Lee 		cable->attr_name.attr.name = "name";
11303d9138e5SBumwoo Lee 		cable->attr_name.attr.mode = 0444;
11313d9138e5SBumwoo Lee 		cable->attr_name.show = cable_name_show;
11323d9138e5SBumwoo Lee 
11333d9138e5SBumwoo Lee 		sysfs_attr_init(&cable->attr_state.attr);
11343d9138e5SBumwoo Lee 		cable->attr_state.attr.name = "state";
11353d9138e5SBumwoo Lee 		cable->attr_state.attr.mode = 0444;
11363d9138e5SBumwoo Lee 		cable->attr_state.show = cable_state_show;
11373d9138e5SBumwoo Lee 	}
11383d9138e5SBumwoo Lee 
11393d9138e5SBumwoo Lee 	return 0;
11403d9138e5SBumwoo Lee }
11413d9138e5SBumwoo Lee 
11423d9138e5SBumwoo Lee /**
11433e70a014SBumwoo Lee  * extcon_alloc_muex() - alloc the mutual exclusive for extcon device
11443e70a014SBumwoo Lee  * @edev:	extcon device
11453e70a014SBumwoo Lee  *
11463e70a014SBumwoo Lee  * Returns 0 if success or error number if fail.
11473e70a014SBumwoo Lee  */
extcon_alloc_muex(struct extcon_dev * edev)11483e70a014SBumwoo Lee static int extcon_alloc_muex(struct extcon_dev *edev)
11493e70a014SBumwoo Lee {
11503e70a014SBumwoo Lee 	char *name;
11513e70a014SBumwoo Lee 	int index;
11523e70a014SBumwoo Lee 
11533e70a014SBumwoo Lee 	if (!edev)
11543e70a014SBumwoo Lee 		return -EINVAL;
11553e70a014SBumwoo Lee 
11563e70a014SBumwoo Lee 	if (!(edev->max_supported && edev->mutually_exclusive))
11573e70a014SBumwoo Lee 		return 0;
11583e70a014SBumwoo Lee 
11593e70a014SBumwoo Lee 	/* Count the size of mutually_exclusive array */
11603e70a014SBumwoo Lee 	for (index = 0; edev->mutually_exclusive[index]; index++)
11613e70a014SBumwoo Lee 		;
11623e70a014SBumwoo Lee 
1163ef753fb4SAndy Shevchenko 	edev->attrs_muex = kcalloc(index + 1, sizeof(*edev->attrs_muex),
11643e70a014SBumwoo Lee 				   GFP_KERNEL);
11653e70a014SBumwoo Lee 	if (!edev->attrs_muex)
11663e70a014SBumwoo Lee 		return -ENOMEM;
11673e70a014SBumwoo Lee 
1168ef753fb4SAndy Shevchenko 	edev->d_attrs_muex = kcalloc(index, sizeof(*edev->d_attrs_muex),
11693e70a014SBumwoo Lee 				     GFP_KERNEL);
11703e70a014SBumwoo Lee 	if (!edev->d_attrs_muex) {
11713e70a014SBumwoo Lee 		kfree(edev->attrs_muex);
11723e70a014SBumwoo Lee 		return -ENOMEM;
11733e70a014SBumwoo Lee 	}
11743e70a014SBumwoo Lee 
11753e70a014SBumwoo Lee 	for (index = 0; edev->mutually_exclusive[index]; index++) {
11763e70a014SBumwoo Lee 		name = kasprintf(GFP_KERNEL, "0x%x",
11773e70a014SBumwoo Lee 				 edev->mutually_exclusive[index]);
11783e70a014SBumwoo Lee 		if (!name) {
11793e70a014SBumwoo Lee 			for (index--; index >= 0; index--)
11803e70a014SBumwoo Lee 				kfree(edev->d_attrs_muex[index].attr.name);
11813e70a014SBumwoo Lee 
11823e70a014SBumwoo Lee 			kfree(edev->d_attrs_muex);
11833e70a014SBumwoo Lee 			kfree(edev->attrs_muex);
11843e70a014SBumwoo Lee 			return -ENOMEM;
11853e70a014SBumwoo Lee 		}
11863e70a014SBumwoo Lee 		sysfs_attr_init(&edev->d_attrs_muex[index].attr);
11873e70a014SBumwoo Lee 		edev->d_attrs_muex[index].attr.name = name;
11883e70a014SBumwoo Lee 		edev->d_attrs_muex[index].attr.mode = 0000;
11893e70a014SBumwoo Lee 		edev->attrs_muex[index] = &edev->d_attrs_muex[index].attr;
11903e70a014SBumwoo Lee 	}
11913e70a014SBumwoo Lee 	edev->attr_g_muex.name = muex_name;
11923e70a014SBumwoo Lee 	edev->attr_g_muex.attrs = edev->attrs_muex;
11933e70a014SBumwoo Lee 
11943e70a014SBumwoo Lee 	return 0;
11953e70a014SBumwoo Lee }
11963e70a014SBumwoo Lee 
11973e70a014SBumwoo Lee /**
119804151575SBumwoo Lee  * extcon_alloc_groups() - alloc the groups for extcon device
119904151575SBumwoo Lee  * @edev:	extcon device
120004151575SBumwoo Lee  *
120104151575SBumwoo Lee  * Returns 0 if success or error number if fail.
120204151575SBumwoo Lee  */
extcon_alloc_groups(struct extcon_dev * edev)120304151575SBumwoo Lee static int extcon_alloc_groups(struct extcon_dev *edev)
120404151575SBumwoo Lee {
120504151575SBumwoo Lee 	int index;
120604151575SBumwoo Lee 
120704151575SBumwoo Lee 	if (!edev)
120804151575SBumwoo Lee 		return -EINVAL;
120904151575SBumwoo Lee 
121004151575SBumwoo Lee 	if (!edev->max_supported)
121104151575SBumwoo Lee 		return 0;
121204151575SBumwoo Lee 
121304151575SBumwoo Lee 	edev->extcon_dev_type.groups = kcalloc(edev->max_supported + 2,
1214ef753fb4SAndy Shevchenko 					  sizeof(*edev->extcon_dev_type.groups),
121504151575SBumwoo Lee 					  GFP_KERNEL);
121604151575SBumwoo Lee 	if (!edev->extcon_dev_type.groups)
121704151575SBumwoo Lee 		return -ENOMEM;
121804151575SBumwoo Lee 
121904151575SBumwoo Lee 	edev->extcon_dev_type.name = dev_name(&edev->dev);
122004151575SBumwoo Lee 	edev->extcon_dev_type.release = dummy_sysfs_dev_release;
122104151575SBumwoo Lee 
122204151575SBumwoo Lee 	for (index = 0; index < edev->max_supported; index++)
122304151575SBumwoo Lee 		edev->extcon_dev_type.groups[index] = &edev->cables[index].attr_g;
122404151575SBumwoo Lee 
122504151575SBumwoo Lee 	if (edev->mutually_exclusive)
122604151575SBumwoo Lee 		edev->extcon_dev_type.groups[index] = &edev->attr_g_muex;
122704151575SBumwoo Lee 
122804151575SBumwoo Lee 	edev->dev.type = &edev->extcon_dev_type;
122904151575SBumwoo Lee 
123004151575SBumwoo Lee 	return 0;
123104151575SBumwoo Lee }
123204151575SBumwoo Lee 
123304151575SBumwoo Lee /**
12346ab6094fSChanwoo Choi  * extcon_dev_register() - Register an new extcon device
12356ab6094fSChanwoo Choi  * @edev:	the extcon device to be registered
1236f68a8342SChanwoo Choi  *
1237f68a8342SChanwoo Choi  * Among the members of edev struct, please set the "user initializing data"
1238f68a8342SChanwoo Choi  * do not set the values of "internal data", which are initialized by
1239f68a8342SChanwoo Choi  * this function.
12406ab6094fSChanwoo Choi  *
12416ab6094fSChanwoo Choi  * Note that before calling this funciton, have to allocate the memory
12426ab6094fSChanwoo Choi  * of an extcon device by using the extcon_dev_allocate(). And the extcon
12436ab6094fSChanwoo Choi  * dev should include the supported_cable information.
12446ab6094fSChanwoo Choi  *
12456ab6094fSChanwoo Choi  * Returns 0 if success or error number if fail.
1246f68a8342SChanwoo Choi  */
extcon_dev_register(struct extcon_dev * edev)1247f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev)
1248f68a8342SChanwoo Choi {
124993e60cd5SAndy Shevchenko 	int ret, index;
1250f68a8342SChanwoo Choi 
1251f68a8342SChanwoo Choi 	ret = create_extcon_class();
1252f68a8342SChanwoo Choi 	if (ret < 0)
1253f68a8342SChanwoo Choi 		return ret;
1254f68a8342SChanwoo Choi 
12557eae43aeSChanwoo Choi 	if (!edev || !edev->supported_cable)
12562a9de9c0SChanwoo Choi 		return -EINVAL;
1257f68a8342SChanwoo Choi 
125893e60cd5SAndy Shevchenko 	for (index = 0; edev->supported_cable[index] != EXTCON_NONE; index++);
12592a9de9c0SChanwoo Choi 
12602a9de9c0SChanwoo Choi 	edev->max_supported = index;
1261f68a8342SChanwoo Choi 	if (index > SUPPORTED_CABLE_MAX) {
12622a9de9c0SChanwoo Choi 		dev_err(&edev->dev,
12632a9de9c0SChanwoo Choi 			"exceed the maximum number of supported cables\n");
1264f68a8342SChanwoo Choi 		return -EINVAL;
1265f68a8342SChanwoo Choi 	}
1266f68a8342SChanwoo Choi 
1267f68a8342SChanwoo Choi 	edev->dev.class = extcon_class;
1268f68a8342SChanwoo Choi 	edev->dev.release = extcon_dev_release;
1269f68a8342SChanwoo Choi 
127071c3ffa5SChanwoo Choi 	edev->name = dev_name(edev->dev.parent);
1271f68a8342SChanwoo Choi 	if (IS_ERR_OR_NULL(edev->name)) {
1272f68a8342SChanwoo Choi 		dev_err(&edev->dev,
1273f68a8342SChanwoo Choi 			"extcon device name is null\n");
1274f68a8342SChanwoo Choi 		return -EINVAL;
1275f68a8342SChanwoo Choi 	}
12767bba9e81SAndy Shevchenko 
12777bba9e81SAndy Shevchenko 	ret = ida_alloc(&extcon_dev_ids, GFP_KERNEL);
12787bba9e81SAndy Shevchenko 	if (ret < 0)
12797bba9e81SAndy Shevchenko 		return ret;
12807bba9e81SAndy Shevchenko 
12817bba9e81SAndy Shevchenko 	edev->id = ret;
12827bba9e81SAndy Shevchenko 
12833d9138e5SBumwoo Lee 	ret = extcon_alloc_cables(edev);
12843d9138e5SBumwoo Lee 	if (ret < 0)
1285f68a8342SChanwoo Choi 		goto err_alloc_cables;
1286f68a8342SChanwoo Choi 
12873e70a014SBumwoo Lee 	ret = extcon_alloc_muex(edev);
12883e70a014SBumwoo Lee 	if (ret < 0)
12893e70a014SBumwoo Lee 		goto err_alloc_muex;
1290f68a8342SChanwoo Choi 
129104151575SBumwoo Lee 	ret = extcon_alloc_groups(edev);
129204151575SBumwoo Lee 	if (ret < 0)
1293f68a8342SChanwoo Choi 		goto err_alloc_groups;
1294f68a8342SChanwoo Choi 
1295f68a8342SChanwoo Choi 	spin_lock_init(&edev->lock);
12965dcc2afeSbumwoo lee 	if (edev->max_supported) {
12975dcc2afeSbumwoo lee 		edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
12985dcc2afeSbumwoo lee 				GFP_KERNEL);
1299046050f6SChanwoo Choi 		if (!edev->nh) {
1300046050f6SChanwoo Choi 			ret = -ENOMEM;
13015dcc2afeSbumwoo lee 			goto err_alloc_nh;
13025dcc2afeSbumwoo lee 		}
1303046050f6SChanwoo Choi 	}
1304046050f6SChanwoo Choi 
1305046050f6SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1306046050f6SChanwoo Choi 		RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1307f68a8342SChanwoo Choi 
1308815429b3SChanwoo Choi 	RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1309815429b3SChanwoo Choi 
1310f68a8342SChanwoo Choi 	dev_set_drvdata(&edev->dev, edev);
1311*8b755fc7SYaxiong Tian 	dev_set_name(&edev->dev, "extcon%d", edev->id);
1312f68a8342SChanwoo Choi 	edev->state = 0;
1313f68a8342SChanwoo Choi 
13145dcc2afeSbumwoo lee 	ret = device_register(&edev->dev);
13155dcc2afeSbumwoo lee 	if (ret) {
13165dcc2afeSbumwoo lee 		put_device(&edev->dev);
13175dcc2afeSbumwoo lee 		goto err_dev;
13185dcc2afeSbumwoo lee 	}
13195dcc2afeSbumwoo lee 
1320f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1321f68a8342SChanwoo Choi 	list_add(&edev->entry, &extcon_dev_list);
1322f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1323f68a8342SChanwoo Choi 
1324f68a8342SChanwoo Choi 	return 0;
1325f68a8342SChanwoo Choi 
1326f68a8342SChanwoo Choi err_dev:
1327f68a8342SChanwoo Choi 	if (edev->max_supported)
13285dcc2afeSbumwoo lee 		kfree(edev->nh);
13295dcc2afeSbumwoo lee err_alloc_nh:
13305dcc2afeSbumwoo lee 	if (edev->max_supported)
1331f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1332f68a8342SChanwoo Choi err_alloc_groups:
1333f68a8342SChanwoo Choi 	if (edev->max_supported && edev->mutually_exclusive) {
1334f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index]; index++)
1335f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1336f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1337f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1338f68a8342SChanwoo Choi 	}
13393e70a014SBumwoo Lee err_alloc_muex:
1340f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1341f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1342f68a8342SChanwoo Choi 	if (edev->max_supported)
1343f68a8342SChanwoo Choi 		kfree(edev->cables);
13443d9138e5SBumwoo Lee err_alloc_cables:
13457bba9e81SAndy Shevchenko 	ida_free(&extcon_dev_ids, edev->id);
13463d9138e5SBumwoo Lee 
1347f68a8342SChanwoo Choi 	return ret;
1348f68a8342SChanwoo Choi }
1349f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register);
1350f68a8342SChanwoo Choi 
1351f68a8342SChanwoo Choi /**
1352f68a8342SChanwoo Choi  * extcon_dev_unregister() - Unregister the extcon device.
13536ab6094fSChanwoo Choi  * @edev:	the extcon device to be unregistered.
1354f68a8342SChanwoo Choi  *
1355f68a8342SChanwoo Choi  * Note that this does not call kfree(edev) because edev was not allocated
1356f68a8342SChanwoo Choi  * by this class.
1357f68a8342SChanwoo Choi  */
extcon_dev_unregister(struct extcon_dev * edev)1358f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev)
1359f68a8342SChanwoo Choi {
1360f68a8342SChanwoo Choi 	int index;
1361f68a8342SChanwoo Choi 
13627eae43aeSChanwoo Choi 	if (!edev)
13637eae43aeSChanwoo Choi 		return;
13647eae43aeSChanwoo Choi 
1365f68a8342SChanwoo Choi 	mutex_lock(&extcon_dev_list_lock);
1366f68a8342SChanwoo Choi 	list_del(&edev->entry);
1367f68a8342SChanwoo Choi 	mutex_unlock(&extcon_dev_list_lock);
1368f68a8342SChanwoo Choi 
1369566825a3SAndy Shevchenko 	if (!get_device(&edev->dev)) {
1370566825a3SAndy Shevchenko 		dev_err(&edev->dev, "Failed to unregister extcon_dev\n");
1371f68a8342SChanwoo Choi 		return;
1372f68a8342SChanwoo Choi 	}
1373f68a8342SChanwoo Choi 
13747bba9e81SAndy Shevchenko 	ida_free(&extcon_dev_ids, edev->id);
13757bba9e81SAndy Shevchenko 
1376f68a8342SChanwoo Choi 	device_unregister(&edev->dev);
1377f68a8342SChanwoo Choi 
1378f68a8342SChanwoo Choi 	if (edev->mutually_exclusive && edev->max_supported) {
1379f68a8342SChanwoo Choi 		for (index = 0; edev->mutually_exclusive[index];
1380f68a8342SChanwoo Choi 				index++)
1381f68a8342SChanwoo Choi 			kfree(edev->d_attrs_muex[index].attr.name);
1382f68a8342SChanwoo Choi 		kfree(edev->d_attrs_muex);
1383f68a8342SChanwoo Choi 		kfree(edev->attrs_muex);
1384f68a8342SChanwoo Choi 	}
1385f68a8342SChanwoo Choi 
1386f68a8342SChanwoo Choi 	for (index = 0; index < edev->max_supported; index++)
1387f68a8342SChanwoo Choi 		kfree(edev->cables[index].attr_g.name);
1388f68a8342SChanwoo Choi 
1389f68a8342SChanwoo Choi 	if (edev->max_supported) {
1390f68a8342SChanwoo Choi 		kfree(edev->extcon_dev_type.groups);
1391f68a8342SChanwoo Choi 		kfree(edev->cables);
13925dcc2afeSbumwoo lee 		kfree(edev->nh);
1393f68a8342SChanwoo Choi 	}
1394f68a8342SChanwoo Choi 
1395f68a8342SChanwoo Choi 	put_device(&edev->dev);
1396f68a8342SChanwoo Choi }
1397f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1398f68a8342SChanwoo Choi 
1399f68a8342SChanwoo Choi #ifdef CONFIG_OF
1400370ed7a9SAndrzej Hajda 
1401370ed7a9SAndrzej Hajda /*
1402370ed7a9SAndrzej Hajda  * extcon_find_edev_by_node - Find the extcon device from devicetree.
1403370ed7a9SAndrzej Hajda  * @node	: OF node identifying edev
1404370ed7a9SAndrzej Hajda  *
1405370ed7a9SAndrzej Hajda  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1406370ed7a9SAndrzej Hajda  */
extcon_find_edev_by_node(struct device_node * node)1407370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1408370ed7a9SAndrzej Hajda {
1409370ed7a9SAndrzej Hajda 	struct extcon_dev *edev;
1410370ed7a9SAndrzej Hajda 
1411370ed7a9SAndrzej Hajda 	mutex_lock(&extcon_dev_list_lock);
1412370ed7a9SAndrzej Hajda 	list_for_each_entry(edev, &extcon_dev_list, entry)
14130146f56bSAndy Shevchenko 		if (edev->dev.parent && device_match_of_node(edev->dev.parent, node))
1414370ed7a9SAndrzej Hajda 			goto out;
1415370ed7a9SAndrzej Hajda 	edev = ERR_PTR(-EPROBE_DEFER);
1416370ed7a9SAndrzej Hajda out:
1417370ed7a9SAndrzej Hajda 	mutex_unlock(&extcon_dev_list_lock);
1418370ed7a9SAndrzej Hajda 
1419370ed7a9SAndrzej Hajda 	return edev;
1420370ed7a9SAndrzej Hajda }
1421370ed7a9SAndrzej Hajda 
1422f68a8342SChanwoo Choi /*
14236ab6094fSChanwoo Choi  * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
14246ab6094fSChanwoo Choi  * @dev		: the instance to the given device
14256ab6094fSChanwoo Choi  * @index	: the index into list of extcon_dev
1426f68a8342SChanwoo Choi  *
14276ab6094fSChanwoo Choi  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1428f68a8342SChanwoo Choi  */
extcon_get_edev_by_phandle(struct device * dev,int index)1429f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1430f68a8342SChanwoo Choi {
14319b4aea51SAndy Shevchenko 	struct device_node *node, *np = dev_of_node(dev);
1432f68a8342SChanwoo Choi 	struct extcon_dev *edev;
1433f68a8342SChanwoo Choi 
14349b4aea51SAndy Shevchenko 	if (!np) {
1435e8752b7aSStephen Boyd 		dev_dbg(dev, "device does not have a device node entry\n");
1436f68a8342SChanwoo Choi 		return ERR_PTR(-EINVAL);
1437f68a8342SChanwoo Choi 	}
1438f68a8342SChanwoo Choi 
14399b4aea51SAndy Shevchenko 	node = of_parse_phandle(np, "extcon", index);
1440f68a8342SChanwoo Choi 	if (!node) {
14419b4aea51SAndy Shevchenko 		dev_dbg(dev, "failed to get phandle in %pOF node\n", np);
1442f68a8342SChanwoo Choi 		return ERR_PTR(-ENODEV);
1443f68a8342SChanwoo Choi 	}
1444f68a8342SChanwoo Choi 
1445370ed7a9SAndrzej Hajda 	edev = extcon_find_edev_by_node(node);
14465d5c4c13SPeter Chen 	of_node_put(node);
1447f68a8342SChanwoo Choi 
1448370ed7a9SAndrzej Hajda 	return edev;
1449f68a8342SChanwoo Choi }
1450370ed7a9SAndrzej Hajda 
1451f68a8342SChanwoo Choi #else
1452370ed7a9SAndrzej Hajda 
extcon_find_edev_by_node(struct device_node * node)1453370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1454370ed7a9SAndrzej Hajda {
1455370ed7a9SAndrzej Hajda 	return ERR_PTR(-ENOSYS);
1456370ed7a9SAndrzej Hajda }
1457370ed7a9SAndrzej Hajda 
extcon_get_edev_by_phandle(struct device * dev,int index)1458f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1459f68a8342SChanwoo Choi {
1460f68a8342SChanwoo Choi 	return ERR_PTR(-ENOSYS);
1461f68a8342SChanwoo Choi }
1462370ed7a9SAndrzej Hajda 
1463f68a8342SChanwoo Choi #endif /* CONFIG_OF */
1464370ed7a9SAndrzej Hajda 
1465370ed7a9SAndrzej Hajda EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
1466f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1467f68a8342SChanwoo Choi 
1468707d7550SChanwoo Choi /**
1469707d7550SChanwoo Choi  * extcon_get_edev_name() - Get the name of the extcon device.
1470707d7550SChanwoo Choi  * @edev:	the extcon device
1471707d7550SChanwoo Choi  */
extcon_get_edev_name(struct extcon_dev * edev)1472707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev)
1473707d7550SChanwoo Choi {
1474707d7550SChanwoo Choi 	return !edev ? NULL : edev->name;
1475707d7550SChanwoo Choi }
1476995bb109SMayank Rana EXPORT_SYMBOL_GPL(extcon_get_edev_name);
1477707d7550SChanwoo Choi 
extcon_class_init(void)1478f68a8342SChanwoo Choi static int __init extcon_class_init(void)
1479f68a8342SChanwoo Choi {
1480f68a8342SChanwoo Choi 	return create_extcon_class();
1481f68a8342SChanwoo Choi }
1482f68a8342SChanwoo Choi module_init(extcon_class_init);
1483f68a8342SChanwoo Choi 
extcon_class_exit(void)1484f68a8342SChanwoo Choi static void __exit extcon_class_exit(void)
1485f68a8342SChanwoo Choi {
1486f68a8342SChanwoo Choi 	class_destroy(extcon_class);
1487f68a8342SChanwoo Choi }
1488f68a8342SChanwoo Choi module_exit(extcon_class_exit);
1489f68a8342SChanwoo Choi 
14902a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1491f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
14926ab6094fSChanwoo Choi MODULE_DESCRIPTION("External Connector (extcon) framework");
14936ab6094fSChanwoo Choi MODULE_LICENSE("GPL v2");
1494