1f68a8342SChanwoo Choi /* 2b9ec23c0SChanwoo Choi * drivers/extcon/extcon.c - External Connector (extcon) framework. 3f68a8342SChanwoo Choi * 4f68a8342SChanwoo Choi * External connector (extcon) class driver 5f68a8342SChanwoo Choi * 62a9de9c0SChanwoo Choi * Copyright (C) 2015 Samsung Electronics 72a9de9c0SChanwoo Choi * Author: Chanwoo Choi <[email protected]> 82a9de9c0SChanwoo Choi * 9f68a8342SChanwoo Choi * Copyright (C) 2012 Samsung Electronics 10f68a8342SChanwoo Choi * Author: Donggeun Kim <[email protected]> 11f68a8342SChanwoo Choi * Author: MyungJoo Ham <[email protected]> 12f68a8342SChanwoo Choi * 13f68a8342SChanwoo Choi * based on android/drivers/switch/switch_class.c 14f68a8342SChanwoo Choi * Copyright (C) 2008 Google, Inc. 15f68a8342SChanwoo Choi * Author: Mike Lockwood <[email protected]> 16f68a8342SChanwoo Choi * 17f68a8342SChanwoo Choi * This software is licensed under the terms of the GNU General Public 18f68a8342SChanwoo Choi * License version 2, as published by the Free Software Foundation, and 19f68a8342SChanwoo Choi * may be copied, distributed, and modified under those terms. 20f68a8342SChanwoo Choi * 21f68a8342SChanwoo Choi * This program is distributed in the hope that it will be useful, 22f68a8342SChanwoo Choi * but WITHOUT ANY WARRANTY; without even the implied warranty of 23f68a8342SChanwoo Choi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24f68a8342SChanwoo Choi * GNU General Public License for more details. 25f68a8342SChanwoo Choi */ 26f68a8342SChanwoo Choi 27f68a8342SChanwoo Choi #include <linux/module.h> 28f68a8342SChanwoo Choi #include <linux/types.h> 29f68a8342SChanwoo Choi #include <linux/init.h> 30f68a8342SChanwoo Choi #include <linux/device.h> 31f68a8342SChanwoo Choi #include <linux/fs.h> 32f68a8342SChanwoo Choi #include <linux/err.h> 33f68a8342SChanwoo Choi #include <linux/extcon.h> 34f68a8342SChanwoo Choi #include <linux/of.h> 35f68a8342SChanwoo Choi #include <linux/slab.h> 36f68a8342SChanwoo Choi #include <linux/sysfs.h> 37f68a8342SChanwoo Choi 382a9de9c0SChanwoo Choi #define SUPPORTED_CABLE_MAX 32 392a9de9c0SChanwoo Choi #define CABLE_NAME_MAX 30 402a9de9c0SChanwoo Choi 4155e4e2f1SChanwoo Choi struct __extcon_info { 4255e4e2f1SChanwoo Choi unsigned int type; 4355e4e2f1SChanwoo Choi unsigned int id; 4455e4e2f1SChanwoo Choi const char *name; 4555e4e2f1SChanwoo Choi 4655e4e2f1SChanwoo Choi } extcon_info[] = { 4755e4e2f1SChanwoo Choi [EXTCON_NONE] = { 4855e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 4955e4e2f1SChanwoo Choi .id = EXTCON_NONE, 5055e4e2f1SChanwoo Choi .name = "NONE", 5155e4e2f1SChanwoo Choi }, 5273b6ecdbSChanwoo Choi 538e9bc36dSChanwoo Choi /* USB external connector */ 5455e4e2f1SChanwoo Choi [EXTCON_USB] = { 5555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 5655e4e2f1SChanwoo Choi .id = EXTCON_USB, 5755e4e2f1SChanwoo Choi .name = "USB", 5855e4e2f1SChanwoo Choi }, 5955e4e2f1SChanwoo Choi [EXTCON_USB_HOST] = { 6055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 6155e4e2f1SChanwoo Choi .id = EXTCON_USB_HOST, 6255e4e2f1SChanwoo Choi .name = "USB_HOST", 6355e4e2f1SChanwoo Choi }, 648e9bc36dSChanwoo Choi 6511eecf91SChanwoo Choi /* Charging external connector */ 6655e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SDP] = { 6755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 6855e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SDP, 6955e4e2f1SChanwoo Choi .name = "SDP", 7055e4e2f1SChanwoo Choi }, 7155e4e2f1SChanwoo Choi [EXTCON_CHG_USB_DCP] = { 7255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 7355e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_DCP, 7455e4e2f1SChanwoo Choi .name = "DCP", 7555e4e2f1SChanwoo Choi }, 7655e4e2f1SChanwoo Choi [EXTCON_CHG_USB_CDP] = { 7755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 7855e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_CDP, 7955e4e2f1SChanwoo Choi .name = "CDP", 8055e4e2f1SChanwoo Choi }, 8155e4e2f1SChanwoo Choi [EXTCON_CHG_USB_ACA] = { 8255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 8355e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_ACA, 8455e4e2f1SChanwoo Choi .name = "ACA", 8555e4e2f1SChanwoo Choi }, 8655e4e2f1SChanwoo Choi [EXTCON_CHG_USB_FAST] = { 8755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 8855e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_FAST, 8955e4e2f1SChanwoo Choi .name = "FAST-CHARGER", 9055e4e2f1SChanwoo Choi }, 9155e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SLOW] = { 9255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 9355e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SLOW, 9455e4e2f1SChanwoo Choi .name = "SLOW-CHARGER", 9555e4e2f1SChanwoo Choi }, 968e9bc36dSChanwoo Choi 9711eecf91SChanwoo Choi /* Jack external connector */ 9855e4e2f1SChanwoo Choi [EXTCON_JACK_MICROPHONE] = { 9955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 10055e4e2f1SChanwoo Choi .id = EXTCON_JACK_MICROPHONE, 10155e4e2f1SChanwoo Choi .name = "MICROPHONE", 10255e4e2f1SChanwoo Choi }, 10355e4e2f1SChanwoo Choi [EXTCON_JACK_HEADPHONE] = { 10455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 10555e4e2f1SChanwoo Choi .id = EXTCON_JACK_HEADPHONE, 10655e4e2f1SChanwoo Choi .name = "HEADPHONE", 10755e4e2f1SChanwoo Choi }, 10855e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_IN] = { 10955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 11055e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_IN, 11155e4e2f1SChanwoo Choi .name = "LINE-IN", 11255e4e2f1SChanwoo Choi }, 11355e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_OUT] = { 11455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 11555e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_OUT, 11655e4e2f1SChanwoo Choi .name = "LINE-OUT", 11755e4e2f1SChanwoo Choi }, 11855e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_IN] = { 11955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 12055e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_IN, 12155e4e2f1SChanwoo Choi .name = "VIDEO-IN", 12255e4e2f1SChanwoo Choi }, 12355e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_OUT] = { 12455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 12555e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_OUT, 12655e4e2f1SChanwoo Choi .name = "VIDEO-OUT", 12755e4e2f1SChanwoo Choi }, 12855e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_IN] = { 12955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 13055e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_IN, 13155e4e2f1SChanwoo Choi .name = "SPDIF-IN", 13255e4e2f1SChanwoo Choi }, 13355e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_OUT] = { 13455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 13555e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_OUT, 13655e4e2f1SChanwoo Choi .name = "SPDIF-OUT", 13755e4e2f1SChanwoo Choi }, 1388e9bc36dSChanwoo Choi 13911eecf91SChanwoo Choi /* Display external connector */ 14055e4e2f1SChanwoo Choi [EXTCON_DISP_HDMI] = { 14155e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 14255e4e2f1SChanwoo Choi .id = EXTCON_DISP_HDMI, 14355e4e2f1SChanwoo Choi .name = "HDMI", 14455e4e2f1SChanwoo Choi }, 14555e4e2f1SChanwoo Choi [EXTCON_DISP_MHL] = { 14655e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 14755e4e2f1SChanwoo Choi .id = EXTCON_DISP_MHL, 14855e4e2f1SChanwoo Choi .name = "MHL", 14955e4e2f1SChanwoo Choi }, 15055e4e2f1SChanwoo Choi [EXTCON_DISP_DVI] = { 15155e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 15255e4e2f1SChanwoo Choi .id = EXTCON_DISP_DVI, 15355e4e2f1SChanwoo Choi .name = "DVI", 15455e4e2f1SChanwoo Choi }, 15555e4e2f1SChanwoo Choi [EXTCON_DISP_VGA] = { 15655e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 15755e4e2f1SChanwoo Choi .id = EXTCON_DISP_VGA, 15855e4e2f1SChanwoo Choi .name = "VGA", 15955e4e2f1SChanwoo Choi }, 1608e9bc36dSChanwoo Choi 16111eecf91SChanwoo Choi /* Miscellaneous external connector */ 16255e4e2f1SChanwoo Choi [EXTCON_DOCK] = { 16355e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 16455e4e2f1SChanwoo Choi .id = EXTCON_DOCK, 16555e4e2f1SChanwoo Choi .name = "DOCK", 16655e4e2f1SChanwoo Choi }, 16755e4e2f1SChanwoo Choi [EXTCON_JIG] = { 16855e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 16955e4e2f1SChanwoo Choi .id = EXTCON_JIG, 17055e4e2f1SChanwoo Choi .name = "JIG", 17155e4e2f1SChanwoo Choi }, 17255e4e2f1SChanwoo Choi [EXTCON_MECHANICAL] = { 17355e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 17455e4e2f1SChanwoo Choi .id = EXTCON_MECHANICAL, 17555e4e2f1SChanwoo Choi .name = "MECHANICAL", 17655e4e2f1SChanwoo Choi }, 1778e9bc36dSChanwoo Choi 17855e4e2f1SChanwoo Choi { /* sentinel */ } 179f68a8342SChanwoo Choi }; 180f68a8342SChanwoo Choi 18120f7b53dSChanwoo Choi /** 18220f7b53dSChanwoo Choi * struct extcon_cable - An internal data for each cable of extcon device. 18320f7b53dSChanwoo Choi * @edev: The extcon device 18420f7b53dSChanwoo Choi * @cable_index: Index of this cable in the edev 18520f7b53dSChanwoo Choi * @attr_g: Attribute group for the cable 18620f7b53dSChanwoo Choi * @attr_name: "name" sysfs entry 18720f7b53dSChanwoo Choi * @attr_state: "state" sysfs entry 18820f7b53dSChanwoo Choi * @attrs: Array pointing to attr_name and attr_state for attr_g 18920f7b53dSChanwoo Choi */ 19020f7b53dSChanwoo Choi struct extcon_cable { 19120f7b53dSChanwoo Choi struct extcon_dev *edev; 19220f7b53dSChanwoo Choi int cable_index; 19320f7b53dSChanwoo Choi 19420f7b53dSChanwoo Choi struct attribute_group attr_g; 19520f7b53dSChanwoo Choi struct device_attribute attr_name; 19620f7b53dSChanwoo Choi struct device_attribute attr_state; 19720f7b53dSChanwoo Choi 19820f7b53dSChanwoo Choi struct attribute *attrs[3]; /* to be fed to attr_g.attrs */ 199792e7e9eSChanwoo Choi 200792e7e9eSChanwoo Choi union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT]; 201792e7e9eSChanwoo Choi union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT]; 202792e7e9eSChanwoo Choi union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT]; 203792e7e9eSChanwoo Choi union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT]; 2047f2a0a16SChanwoo Choi 2057f2a0a16SChanwoo Choi unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)]; 2067f2a0a16SChanwoo Choi unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)]; 2077f2a0a16SChanwoo Choi unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)]; 2087f2a0a16SChanwoo Choi unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)]; 20920f7b53dSChanwoo Choi }; 21020f7b53dSChanwoo Choi 211f68a8342SChanwoo Choi static struct class *extcon_class; 212f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 213f68a8342SChanwoo Choi static struct class_compat *switch_class; 214f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 215f68a8342SChanwoo Choi 216f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list); 217f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock); 218f68a8342SChanwoo Choi 219f68a8342SChanwoo Choi /** 220f68a8342SChanwoo Choi * check_mutually_exclusive - Check if new_state violates mutually_exclusive 221f68a8342SChanwoo Choi * condition. 222f68a8342SChanwoo Choi * @edev: the extcon device 223f68a8342SChanwoo Choi * @new_state: new cable attach status for @edev 224f68a8342SChanwoo Choi * 225f68a8342SChanwoo Choi * Returns 0 if nothing violates. Returns the index + 1 for the first 226f68a8342SChanwoo Choi * violated condition. 227f68a8342SChanwoo Choi */ 228f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) 229f68a8342SChanwoo Choi { 230f68a8342SChanwoo Choi int i = 0; 231f68a8342SChanwoo Choi 232f68a8342SChanwoo Choi if (!edev->mutually_exclusive) 233f68a8342SChanwoo Choi return 0; 234f68a8342SChanwoo Choi 235f68a8342SChanwoo Choi for (i = 0; edev->mutually_exclusive[i]; i++) { 236f68a8342SChanwoo Choi int weight; 237f68a8342SChanwoo Choi u32 correspondants = new_state & edev->mutually_exclusive[i]; 238f68a8342SChanwoo Choi 239f68a8342SChanwoo Choi /* calculate the total number of bits set */ 240f68a8342SChanwoo Choi weight = hweight32(correspondants); 241f68a8342SChanwoo Choi if (weight > 1) 242f68a8342SChanwoo Choi return i + 1; 243f68a8342SChanwoo Choi } 244f68a8342SChanwoo Choi 245f68a8342SChanwoo Choi return 0; 246f68a8342SChanwoo Choi } 247f68a8342SChanwoo Choi 24873b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id) 2492a9de9c0SChanwoo Choi { 2502a9de9c0SChanwoo Choi int i; 2512a9de9c0SChanwoo Choi 2522a9de9c0SChanwoo Choi /* Find the the index of extcon cable in edev->supported_cable */ 2532a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 2542a9de9c0SChanwoo Choi if (edev->supported_cable[i] == id) 2552a9de9c0SChanwoo Choi return i; 2562a9de9c0SChanwoo Choi } 2572a9de9c0SChanwoo Choi 2582a9de9c0SChanwoo Choi return -EINVAL; 2592a9de9c0SChanwoo Choi } 2602a9de9c0SChanwoo Choi 261792e7e9eSChanwoo Choi static int get_extcon_type(unsigned int prop) 262792e7e9eSChanwoo Choi { 263792e7e9eSChanwoo Choi switch (prop) { 264792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 265792e7e9eSChanwoo Choi return EXTCON_TYPE_USB; 266792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 267792e7e9eSChanwoo Choi return EXTCON_TYPE_CHG; 268792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 269792e7e9eSChanwoo Choi return EXTCON_TYPE_JACK; 270792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 271792e7e9eSChanwoo Choi return EXTCON_TYPE_DISP; 272792e7e9eSChanwoo Choi default: 273792e7e9eSChanwoo Choi return -EINVAL; 274792e7e9eSChanwoo Choi } 275792e7e9eSChanwoo Choi } 276792e7e9eSChanwoo Choi 277792e7e9eSChanwoo Choi static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index) 278792e7e9eSChanwoo Choi { 279792e7e9eSChanwoo Choi return !!(edev->state & BIT(index)); 280792e7e9eSChanwoo Choi } 281792e7e9eSChanwoo Choi 282*ab11af04SChanwoo Choi static bool is_extcon_changed(struct extcon_dev *edev, int index, 283*ab11af04SChanwoo Choi bool new_state) 284046050f6SChanwoo Choi { 285*ab11af04SChanwoo Choi int state = !!(edev->state & BIT(index)); 286*ab11af04SChanwoo Choi return (state != new_state); 287046050f6SChanwoo Choi } 288046050f6SChanwoo Choi 289792e7e9eSChanwoo Choi static bool is_extcon_property_supported(unsigned int id, unsigned int prop) 290792e7e9eSChanwoo Choi { 291792e7e9eSChanwoo Choi int type; 292792e7e9eSChanwoo Choi 293792e7e9eSChanwoo Choi /* Check whether the property is supported or not. */ 294792e7e9eSChanwoo Choi type = get_extcon_type(prop); 295792e7e9eSChanwoo Choi if (type < 0) 296792e7e9eSChanwoo Choi return false; 297792e7e9eSChanwoo Choi 298792e7e9eSChanwoo Choi /* Check whether a specific extcon id supports the property or not. */ 299792e7e9eSChanwoo Choi return !!(extcon_info[id].type & type); 300792e7e9eSChanwoo Choi } 301792e7e9eSChanwoo Choi 3027f2a0a16SChanwoo Choi static int is_extcon_property_capability(struct extcon_dev *edev, 3037f2a0a16SChanwoo Choi unsigned int id, int index,unsigned int prop) 3047f2a0a16SChanwoo Choi { 3057f2a0a16SChanwoo Choi struct extcon_cable *cable; 3067f2a0a16SChanwoo Choi int type, ret; 3077f2a0a16SChanwoo Choi 3087f2a0a16SChanwoo Choi /* Check whether the property is supported or not. */ 3097f2a0a16SChanwoo Choi type = get_extcon_type(prop); 3107f2a0a16SChanwoo Choi if (type < 0) 3117f2a0a16SChanwoo Choi return type; 3127f2a0a16SChanwoo Choi 3137f2a0a16SChanwoo Choi cable = &edev->cables[index]; 3147f2a0a16SChanwoo Choi 3157f2a0a16SChanwoo Choi switch (type) { 3167f2a0a16SChanwoo Choi case EXTCON_TYPE_USB: 3177f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); 3187f2a0a16SChanwoo Choi break; 3197f2a0a16SChanwoo Choi case EXTCON_TYPE_CHG: 3207f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); 3217f2a0a16SChanwoo Choi break; 3227f2a0a16SChanwoo Choi case EXTCON_TYPE_JACK: 3237f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); 3247f2a0a16SChanwoo Choi break; 3257f2a0a16SChanwoo Choi case EXTCON_TYPE_DISP: 3267f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); 3277f2a0a16SChanwoo Choi break; 3287f2a0a16SChanwoo Choi default: 3297f2a0a16SChanwoo Choi ret = -EINVAL; 3307f2a0a16SChanwoo Choi } 3317f2a0a16SChanwoo Choi 3327f2a0a16SChanwoo Choi return ret; 3337f2a0a16SChanwoo Choi } 3347f2a0a16SChanwoo Choi 335792e7e9eSChanwoo Choi static void init_property(struct extcon_dev *edev, unsigned int id, int index) 336792e7e9eSChanwoo Choi { 337792e7e9eSChanwoo Choi unsigned int type = extcon_info[id].type; 338792e7e9eSChanwoo Choi struct extcon_cable *cable = &edev->cables[index]; 339792e7e9eSChanwoo Choi 340792e7e9eSChanwoo Choi if (EXTCON_TYPE_USB & type) 341792e7e9eSChanwoo Choi memset(cable->usb_propval, 0, sizeof(cable->usb_propval)); 342792e7e9eSChanwoo Choi if (EXTCON_TYPE_CHG & type) 343792e7e9eSChanwoo Choi memset(cable->chg_propval, 0, sizeof(cable->chg_propval)); 344792e7e9eSChanwoo Choi if (EXTCON_TYPE_JACK & type) 345792e7e9eSChanwoo Choi memset(cable->jack_propval, 0, sizeof(cable->jack_propval)); 346792e7e9eSChanwoo Choi if (EXTCON_TYPE_DISP & type) 347792e7e9eSChanwoo Choi memset(cable->disp_propval, 0, sizeof(cable->disp_propval)); 348792e7e9eSChanwoo Choi } 349792e7e9eSChanwoo Choi 350f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr, 351f68a8342SChanwoo Choi char *buf) 352f68a8342SChanwoo Choi { 353f68a8342SChanwoo Choi int i, count = 0; 354f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 355f68a8342SChanwoo Choi 356f68a8342SChanwoo Choi if (edev->max_supported == 0) 357f68a8342SChanwoo Choi return sprintf(buf, "%u\n", edev->state); 358f68a8342SChanwoo Choi 3592a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 360f68a8342SChanwoo Choi count += sprintf(buf + count, "%s=%d\n", 36155e4e2f1SChanwoo Choi extcon_info[edev->supported_cable[i]].name, 362f68a8342SChanwoo Choi !!(edev->state & (1 << i))); 363f68a8342SChanwoo Choi } 364f68a8342SChanwoo Choi 365f68a8342SChanwoo Choi return count; 366f68a8342SChanwoo Choi } 3675d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state); 368f68a8342SChanwoo Choi 369f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr, 370f68a8342SChanwoo Choi char *buf) 371f68a8342SChanwoo Choi { 372f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 373f68a8342SChanwoo Choi 37471c3ffa5SChanwoo Choi return sprintf(buf, "%s\n", edev->name); 375f68a8342SChanwoo Choi } 376f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name); 377f68a8342SChanwoo Choi 378f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev, 379f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 380f68a8342SChanwoo Choi { 381f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 382f68a8342SChanwoo Choi attr_name); 3832a9de9c0SChanwoo Choi int i = cable->cable_index; 384f68a8342SChanwoo Choi 385f68a8342SChanwoo Choi return sprintf(buf, "%s\n", 38655e4e2f1SChanwoo Choi extcon_info[cable->edev->supported_cable[i]].name); 387f68a8342SChanwoo Choi } 388f68a8342SChanwoo Choi 389f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev, 390f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 391f68a8342SChanwoo Choi { 392f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 393f68a8342SChanwoo Choi attr_state); 394f68a8342SChanwoo Choi 395be052cc8SRoger Quadros int i = cable->cable_index; 396be052cc8SRoger Quadros 397f68a8342SChanwoo Choi return sprintf(buf, "%d\n", 398575c2b86SChanwoo Choi extcon_get_state(cable->edev, cable->edev->supported_cable[i])); 399f68a8342SChanwoo Choi } 400f68a8342SChanwoo Choi 401f68a8342SChanwoo Choi /** 402*ab11af04SChanwoo Choi * extcon_sync() - Synchronize the states for both the attached/detached 403*ab11af04SChanwoo Choi * @edev: the extcon device that has the cable. 404f68a8342SChanwoo Choi * 405*ab11af04SChanwoo Choi * This function send a notification to synchronize the all states of a 406*ab11af04SChanwoo Choi * specific external connector 407f68a8342SChanwoo Choi */ 408*ab11af04SChanwoo Choi int extcon_sync(struct extcon_dev *edev, unsigned int id) 409f68a8342SChanwoo Choi { 410f68a8342SChanwoo Choi char name_buf[120]; 411f68a8342SChanwoo Choi char state_buf[120]; 412f68a8342SChanwoo Choi char *prop_buf; 413f68a8342SChanwoo Choi char *envp[3]; 414f68a8342SChanwoo Choi int env_offset = 0; 415f68a8342SChanwoo Choi int length; 416046050f6SChanwoo Choi int index; 417*ab11af04SChanwoo Choi int state; 418f68a8342SChanwoo Choi unsigned long flags; 419f68a8342SChanwoo Choi 4207eae43aeSChanwoo Choi if (!edev) 4217eae43aeSChanwoo Choi return -EINVAL; 4227eae43aeSChanwoo Choi 423*ab11af04SChanwoo Choi index = find_cable_index_by_id(edev, id); 424*ab11af04SChanwoo Choi if (index < 0) 425*ab11af04SChanwoo Choi return index; 426*ab11af04SChanwoo Choi 427f68a8342SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 428f68a8342SChanwoo Choi 429*ab11af04SChanwoo Choi state = !!(edev->state & BIT(index)); 430*ab11af04SChanwoo Choi raw_notifier_call_chain(&edev->nh[index], state, edev); 431f7a89811SRoger Quadros 432f68a8342SChanwoo Choi /* This could be in interrupt handler */ 433f68a8342SChanwoo Choi prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); 434*ab11af04SChanwoo Choi if (!prop_buf) { 435f68a8342SChanwoo Choi /* Unlock early before uevent */ 436f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 437f68a8342SChanwoo Choi 438f68a8342SChanwoo Choi dev_err(&edev->dev, "out of memory in extcon_set_state\n"); 439f68a8342SChanwoo Choi kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); 440f68a8342SChanwoo Choi 441f68a8342SChanwoo Choi return 0; 442f68a8342SChanwoo Choi } 443f68a8342SChanwoo Choi 444*ab11af04SChanwoo Choi length = name_show(&edev->dev, NULL, prop_buf); 445*ab11af04SChanwoo Choi if (length > 0) { 446*ab11af04SChanwoo Choi if (prop_buf[length - 1] == '\n') 447*ab11af04SChanwoo Choi prop_buf[length - 1] = 0; 448*ab11af04SChanwoo Choi snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf); 449*ab11af04SChanwoo Choi envp[env_offset++] = name_buf; 450*ab11af04SChanwoo Choi } 451*ab11af04SChanwoo Choi 452*ab11af04SChanwoo Choi length = state_show(&edev->dev, NULL, prop_buf); 453*ab11af04SChanwoo Choi if (length > 0) { 454*ab11af04SChanwoo Choi if (prop_buf[length - 1] == '\n') 455*ab11af04SChanwoo Choi prop_buf[length - 1] = 0; 456*ab11af04SChanwoo Choi snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf); 457*ab11af04SChanwoo Choi envp[env_offset++] = state_buf; 458*ab11af04SChanwoo Choi } 459*ab11af04SChanwoo Choi envp[env_offset] = NULL; 460*ab11af04SChanwoo Choi 461*ab11af04SChanwoo Choi /* Unlock early before uevent */ 462*ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 463*ab11af04SChanwoo Choi kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); 464*ab11af04SChanwoo Choi free_page((unsigned long)prop_buf); 465*ab11af04SChanwoo Choi 466*ab11af04SChanwoo Choi return 0; 467*ab11af04SChanwoo Choi } 468*ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_sync); 469*ab11af04SChanwoo Choi 470f68a8342SChanwoo Choi /** 471575c2b86SChanwoo Choi * extcon_get_state() - Get the state of a external connector. 472f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 4732a9de9c0SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 474f68a8342SChanwoo Choi */ 475575c2b86SChanwoo Choi int extcon_get_state(struct extcon_dev *edev, const unsigned int id) 476f68a8342SChanwoo Choi { 477575c2b86SChanwoo Choi int index, state; 478575c2b86SChanwoo Choi unsigned long flags; 4792a9de9c0SChanwoo Choi 4807eae43aeSChanwoo Choi if (!edev) 4817eae43aeSChanwoo Choi return -EINVAL; 4827eae43aeSChanwoo Choi 4832a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 4842a9de9c0SChanwoo Choi if (index < 0) 4852a9de9c0SChanwoo Choi return index; 4862a9de9c0SChanwoo Choi 487575c2b86SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 488575c2b86SChanwoo Choi state = is_extcon_attached(edev, index); 489575c2b86SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 490f68a8342SChanwoo Choi 491575c2b86SChanwoo Choi return state; 492f68a8342SChanwoo Choi } 493575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_state); 494f68a8342SChanwoo Choi 495f68a8342SChanwoo Choi /** 496575c2b86SChanwoo Choi * extcon_set_state() - Set the state of a external connector. 497*ab11af04SChanwoo Choi * without a notification. 498f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 4992a9de9c0SChanwoo Choi * @id: the unique id of each external connector 5002a9de9c0SChanwoo Choi * in extcon enumeration. 5012a9de9c0SChanwoo Choi * @state: the new cable status. The default semantics is 502f68a8342SChanwoo Choi * true: attached / false: detached. 503*ab11af04SChanwoo Choi * 504*ab11af04SChanwoo Choi * This function only set the state of a external connector without 505*ab11af04SChanwoo Choi * a notification. To synchronize the data of a external connector, 506*ab11af04SChanwoo Choi * use extcon_set_state_sync() and extcon_sync(). 507f68a8342SChanwoo Choi */ 508575c2b86SChanwoo Choi int extcon_set_state(struct extcon_dev *edev, unsigned int id, 5092a9de9c0SChanwoo Choi bool cable_state) 510f68a8342SChanwoo Choi { 511*ab11af04SChanwoo Choi unsigned long flags; 512*ab11af04SChanwoo Choi int index, ret = 0; 513f68a8342SChanwoo Choi 5147eae43aeSChanwoo Choi if (!edev) 5157eae43aeSChanwoo Choi return -EINVAL; 5167eae43aeSChanwoo Choi 5172a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 5182a9de9c0SChanwoo Choi if (index < 0) 5192a9de9c0SChanwoo Choi return index; 5202a9de9c0SChanwoo Choi 521*ab11af04SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 522*ab11af04SChanwoo Choi 523*ab11af04SChanwoo Choi /* Check whether the external connector's state is changed. */ 524*ab11af04SChanwoo Choi if (!is_extcon_changed(edev, index, cable_state)) 525*ab11af04SChanwoo Choi goto out; 526*ab11af04SChanwoo Choi 527*ab11af04SChanwoo Choi if (check_mutually_exclusive(edev, 528*ab11af04SChanwoo Choi (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) { 529*ab11af04SChanwoo Choi ret = -EPERM; 530*ab11af04SChanwoo Choi goto out; 531*ab11af04SChanwoo Choi } 532*ab11af04SChanwoo Choi 533792e7e9eSChanwoo Choi /* 534792e7e9eSChanwoo Choi * Initialize the value of extcon property before setting 535792e7e9eSChanwoo Choi * the detached state for an external connector. 536792e7e9eSChanwoo Choi */ 537792e7e9eSChanwoo Choi if (!cable_state) 538792e7e9eSChanwoo Choi init_property(edev, id, index); 539792e7e9eSChanwoo Choi 540*ab11af04SChanwoo Choi /* Update the state for a external connector. */ 541*ab11af04SChanwoo Choi if (cable_state) 542*ab11af04SChanwoo Choi edev->state |= BIT(index); 543*ab11af04SChanwoo Choi else 544*ab11af04SChanwoo Choi edev->state &= ~(BIT(index)); 545*ab11af04SChanwoo Choi out: 546*ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 547*ab11af04SChanwoo Choi 548*ab11af04SChanwoo Choi return ret; 549f68a8342SChanwoo Choi } 550575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state); 551f68a8342SChanwoo Choi 552f68a8342SChanwoo Choi /** 553*ab11af04SChanwoo Choi * extcon_set_state_sync() - Set the state of a external connector 554*ab11af04SChanwoo Choi * with a notification. 555*ab11af04SChanwoo Choi * @edev: the extcon device that has the cable. 556*ab11af04SChanwoo Choi * @id: the unique id of each external connector 557*ab11af04SChanwoo Choi * in extcon enumeration. 558*ab11af04SChanwoo Choi * @state: the new cable status. The default semantics is 559*ab11af04SChanwoo Choi * true: attached / false: detached. 560*ab11af04SChanwoo Choi * 561*ab11af04SChanwoo Choi * This function set the state of external connector and synchronize the data 562*ab11af04SChanwoo Choi * by usning a notification. 563*ab11af04SChanwoo Choi */ 564*ab11af04SChanwoo Choi int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, 565*ab11af04SChanwoo Choi bool cable_state) 566*ab11af04SChanwoo Choi { 567*ab11af04SChanwoo Choi int ret, index; 568*ab11af04SChanwoo Choi unsigned long flags; 569*ab11af04SChanwoo Choi 570*ab11af04SChanwoo Choi index = find_cable_index_by_id(edev, id); 571*ab11af04SChanwoo Choi if (index < 0) 572*ab11af04SChanwoo Choi return index; 573*ab11af04SChanwoo Choi 574*ab11af04SChanwoo Choi /* Check whether the external connector's state is changed. */ 575*ab11af04SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 576*ab11af04SChanwoo Choi ret = is_extcon_changed(edev, index, cable_state); 577*ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 578*ab11af04SChanwoo Choi if (!ret) 579*ab11af04SChanwoo Choi return 0; 580*ab11af04SChanwoo Choi 581*ab11af04SChanwoo Choi ret = extcon_set_state(edev, id, cable_state); 582*ab11af04SChanwoo Choi if (ret < 0) 583*ab11af04SChanwoo Choi return ret; 584*ab11af04SChanwoo Choi 585*ab11af04SChanwoo Choi return extcon_sync(edev, id); 586*ab11af04SChanwoo Choi } 587*ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state_sync); 588*ab11af04SChanwoo Choi 589*ab11af04SChanwoo Choi /** 590792e7e9eSChanwoo Choi * extcon_get_property() - Get the property value of a specific cable. 591792e7e9eSChanwoo Choi * @edev: the extcon device that has the cable. 592792e7e9eSChanwoo Choi * @id: the unique id of each external connector 593792e7e9eSChanwoo Choi * in extcon enumeration. 594792e7e9eSChanwoo Choi * @prop: the property id among enum extcon_property. 595792e7e9eSChanwoo Choi * @prop_val: the pointer which store the value of property. 596792e7e9eSChanwoo Choi * 597792e7e9eSChanwoo Choi * When getting the property value of external connector, the external connector 598792e7e9eSChanwoo Choi * should be attached. If detached state, function just return 0 without 599792e7e9eSChanwoo Choi * property value. Also, the each property should be included in the list of 600792e7e9eSChanwoo Choi * supported properties according to the type of external connectors. 601792e7e9eSChanwoo Choi * 602792e7e9eSChanwoo Choi * Returns 0 if success or error number if fail 603792e7e9eSChanwoo Choi */ 604792e7e9eSChanwoo Choi int extcon_get_property(struct extcon_dev *edev, unsigned int id, 605792e7e9eSChanwoo Choi unsigned int prop, 606792e7e9eSChanwoo Choi union extcon_property_value *prop_val) 607792e7e9eSChanwoo Choi { 608792e7e9eSChanwoo Choi struct extcon_cable *cable; 609792e7e9eSChanwoo Choi unsigned long flags; 610792e7e9eSChanwoo Choi int index, ret = 0; 611792e7e9eSChanwoo Choi 612792e7e9eSChanwoo Choi *prop_val = (union extcon_property_value)(0); 613792e7e9eSChanwoo Choi 614792e7e9eSChanwoo Choi if (!edev) 615792e7e9eSChanwoo Choi return -EINVAL; 616792e7e9eSChanwoo Choi 617792e7e9eSChanwoo Choi /* Check whether the property is supported or not */ 618792e7e9eSChanwoo Choi if (!is_extcon_property_supported(id, prop)) 619792e7e9eSChanwoo Choi return -EINVAL; 620792e7e9eSChanwoo Choi 621792e7e9eSChanwoo Choi /* Find the cable index of external connector by using id */ 622792e7e9eSChanwoo Choi index = find_cable_index_by_id(edev, id); 623792e7e9eSChanwoo Choi if (index < 0) 624792e7e9eSChanwoo Choi return index; 625792e7e9eSChanwoo Choi 626792e7e9eSChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 627792e7e9eSChanwoo Choi 6287f2a0a16SChanwoo Choi /* Check whether the property is available or not. */ 6297f2a0a16SChanwoo Choi if (!is_extcon_property_capability(edev, id, index, prop)) { 6307f2a0a16SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 6317f2a0a16SChanwoo Choi return -EPERM; 6327f2a0a16SChanwoo Choi } 6337f2a0a16SChanwoo Choi 634792e7e9eSChanwoo Choi /* 635792e7e9eSChanwoo Choi * Check whether the external connector is attached. 636792e7e9eSChanwoo Choi * If external connector is detached, the user can not 637792e7e9eSChanwoo Choi * get the property value. 638792e7e9eSChanwoo Choi */ 639792e7e9eSChanwoo Choi if (!is_extcon_attached(edev, index)) { 640792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 641792e7e9eSChanwoo Choi return 0; 642792e7e9eSChanwoo Choi } 643792e7e9eSChanwoo Choi 644792e7e9eSChanwoo Choi cable = &edev->cables[index]; 645792e7e9eSChanwoo Choi 646792e7e9eSChanwoo Choi /* Get the property value according to extcon type */ 647792e7e9eSChanwoo Choi switch (prop) { 648792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 649792e7e9eSChanwoo Choi *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN]; 650792e7e9eSChanwoo Choi break; 651792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 652792e7e9eSChanwoo Choi *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN]; 653792e7e9eSChanwoo Choi break; 654792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 655792e7e9eSChanwoo Choi *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN]; 656792e7e9eSChanwoo Choi break; 657792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 658792e7e9eSChanwoo Choi *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN]; 659792e7e9eSChanwoo Choi break; 660792e7e9eSChanwoo Choi default: 661792e7e9eSChanwoo Choi ret = -EINVAL; 662792e7e9eSChanwoo Choi break; 663792e7e9eSChanwoo Choi } 664792e7e9eSChanwoo Choi 665792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 666792e7e9eSChanwoo Choi 667792e7e9eSChanwoo Choi return ret; 668792e7e9eSChanwoo Choi } 669792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property); 670792e7e9eSChanwoo Choi 671792e7e9eSChanwoo Choi /** 672792e7e9eSChanwoo Choi * extcon_set_property() - Set the property value of a specific cable. 673792e7e9eSChanwoo Choi * @edev: the extcon device that has the cable. 674792e7e9eSChanwoo Choi * @id: the unique id of each external connector 675792e7e9eSChanwoo Choi * in extcon enumeration. 676792e7e9eSChanwoo Choi * @prop: the property id among enum extcon_property. 677792e7e9eSChanwoo Choi * @prop_val: the pointer including the new value of property. 678792e7e9eSChanwoo Choi * 679792e7e9eSChanwoo Choi * The each property should be included in the list of supported properties 680792e7e9eSChanwoo Choi * according to the type of external connectors. 681792e7e9eSChanwoo Choi * 682792e7e9eSChanwoo Choi * Returns 0 if success or error number if fail 683792e7e9eSChanwoo Choi */ 684792e7e9eSChanwoo Choi int extcon_set_property(struct extcon_dev *edev, unsigned int id, 685792e7e9eSChanwoo Choi unsigned int prop, 686792e7e9eSChanwoo Choi union extcon_property_value prop_val) 687792e7e9eSChanwoo Choi { 688792e7e9eSChanwoo Choi struct extcon_cable *cable; 689792e7e9eSChanwoo Choi unsigned long flags; 690792e7e9eSChanwoo Choi int index, ret = 0; 691792e7e9eSChanwoo Choi 692792e7e9eSChanwoo Choi if (!edev) 693792e7e9eSChanwoo Choi return -EINVAL; 694792e7e9eSChanwoo Choi 695792e7e9eSChanwoo Choi /* Check whether the property is supported or not */ 696792e7e9eSChanwoo Choi if (!is_extcon_property_supported(id, prop)) 697792e7e9eSChanwoo Choi return -EINVAL; 698792e7e9eSChanwoo Choi 699792e7e9eSChanwoo Choi /* Find the cable index of external connector by using id */ 700792e7e9eSChanwoo Choi index = find_cable_index_by_id(edev, id); 701792e7e9eSChanwoo Choi if (index < 0) 702792e7e9eSChanwoo Choi return index; 703792e7e9eSChanwoo Choi 704792e7e9eSChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 705792e7e9eSChanwoo Choi 7067f2a0a16SChanwoo Choi /* Check whether the property is available or not. */ 7077f2a0a16SChanwoo Choi if (!is_extcon_property_capability(edev, id, index, prop)) { 7087f2a0a16SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 7097f2a0a16SChanwoo Choi return -EPERM; 7107f2a0a16SChanwoo Choi } 7117f2a0a16SChanwoo Choi 712792e7e9eSChanwoo Choi cable = &edev->cables[index]; 713792e7e9eSChanwoo Choi 714792e7e9eSChanwoo Choi /* Set the property value according to extcon type */ 715792e7e9eSChanwoo Choi switch (prop) { 716792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 717792e7e9eSChanwoo Choi cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val; 718792e7e9eSChanwoo Choi break; 719792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 720792e7e9eSChanwoo Choi cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val; 721792e7e9eSChanwoo Choi break; 722792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 723792e7e9eSChanwoo Choi cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val; 724792e7e9eSChanwoo Choi break; 725792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 726792e7e9eSChanwoo Choi cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val; 727792e7e9eSChanwoo Choi break; 728792e7e9eSChanwoo Choi default: 729792e7e9eSChanwoo Choi ret = -EINVAL; 730792e7e9eSChanwoo Choi break; 731792e7e9eSChanwoo Choi } 732792e7e9eSChanwoo Choi 733792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 734792e7e9eSChanwoo Choi 735792e7e9eSChanwoo Choi return ret; 736792e7e9eSChanwoo Choi } 737792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property); 738792e7e9eSChanwoo Choi 739792e7e9eSChanwoo Choi /** 740*ab11af04SChanwoo Choi * extcon_set_property_sync() - Set the property value of a specific cable 741*ab11af04SChanwoo Choi with a notification. 742*ab11af04SChanwoo Choi * @prop_val: the pointer including the new value of property. 743*ab11af04SChanwoo Choi * 744*ab11af04SChanwoo Choi * When setting the property value of external connector, the external connector 745*ab11af04SChanwoo Choi * should be attached. The each property should be included in the list of 746*ab11af04SChanwoo Choi * supported properties according to the type of external connectors. 747*ab11af04SChanwoo Choi * 748*ab11af04SChanwoo Choi * Returns 0 if success or error number if fail 749*ab11af04SChanwoo Choi */ 750*ab11af04SChanwoo Choi int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id, 751*ab11af04SChanwoo Choi unsigned int prop, 752*ab11af04SChanwoo Choi union extcon_property_value prop_val) 753*ab11af04SChanwoo Choi { 754*ab11af04SChanwoo Choi int ret; 755*ab11af04SChanwoo Choi 756*ab11af04SChanwoo Choi ret = extcon_set_property(edev, id, prop, prop_val); 757*ab11af04SChanwoo Choi if (ret < 0) 758*ab11af04SChanwoo Choi return ret; 759*ab11af04SChanwoo Choi 760*ab11af04SChanwoo Choi return extcon_sync(edev, id); 761*ab11af04SChanwoo Choi } 762*ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_sync); 763*ab11af04SChanwoo Choi 764*ab11af04SChanwoo Choi /** 7657f2a0a16SChanwoo Choi * extcon_get_property_capability() - Get the capability of property 7667f2a0a16SChanwoo Choi * of an external connector. 7677f2a0a16SChanwoo Choi * @edev: the extcon device that has the cable. 7687f2a0a16SChanwoo Choi * @id: the unique id of each external connector 7697f2a0a16SChanwoo Choi * in extcon enumeration. 7707f2a0a16SChanwoo Choi * @prop: the property id among enum extcon_property. 7717f2a0a16SChanwoo Choi * 7727f2a0a16SChanwoo Choi * Returns 1 if the property is available or 0 if not available. 7737f2a0a16SChanwoo Choi */ 7747f2a0a16SChanwoo Choi int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id, 7757f2a0a16SChanwoo Choi unsigned int prop) 7767f2a0a16SChanwoo Choi { 7777f2a0a16SChanwoo Choi int index; 7787f2a0a16SChanwoo Choi 7797f2a0a16SChanwoo Choi if (!edev) 7807f2a0a16SChanwoo Choi return -EINVAL; 7817f2a0a16SChanwoo Choi 7827f2a0a16SChanwoo Choi /* Check whether the property is supported or not */ 7837f2a0a16SChanwoo Choi if (!is_extcon_property_supported(id, prop)) 7847f2a0a16SChanwoo Choi return -EINVAL; 7857f2a0a16SChanwoo Choi 7867f2a0a16SChanwoo Choi /* Find the cable index of external connector by using id */ 7877f2a0a16SChanwoo Choi index = find_cable_index_by_id(edev, id); 7887f2a0a16SChanwoo Choi if (index < 0) 7897f2a0a16SChanwoo Choi return index; 7907f2a0a16SChanwoo Choi 7917f2a0a16SChanwoo Choi return is_extcon_property_capability(edev, id, index, prop); 7927f2a0a16SChanwoo Choi } 7937f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property_capability); 7947f2a0a16SChanwoo Choi 7957f2a0a16SChanwoo Choi /** 7967f2a0a16SChanwoo Choi * extcon_set_property_capability() - Set the capability of a property 7977f2a0a16SChanwoo Choi * of an external connector. 7987f2a0a16SChanwoo Choi * @edev: the extcon device that has the cable. 7997f2a0a16SChanwoo Choi * @id: the unique id of each external connector 8007f2a0a16SChanwoo Choi * in extcon enumeration. 8017f2a0a16SChanwoo Choi * @prop: the property id among enum extcon_property. 8027f2a0a16SChanwoo Choi * 8037f2a0a16SChanwoo Choi * This function set the capability of a property for an external connector 8047f2a0a16SChanwoo Choi * to mark the bit in capability bitmap which mean the available state of 8057f2a0a16SChanwoo Choi * a property. 8067f2a0a16SChanwoo Choi * 8077f2a0a16SChanwoo Choi * Returns 0 if success or error number if fail 8087f2a0a16SChanwoo Choi */ 8097f2a0a16SChanwoo Choi int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id, 8107f2a0a16SChanwoo Choi unsigned int prop) 8117f2a0a16SChanwoo Choi { 8127f2a0a16SChanwoo Choi struct extcon_cable *cable; 8137f2a0a16SChanwoo Choi int index, type, ret = 0; 8147f2a0a16SChanwoo Choi 8157f2a0a16SChanwoo Choi if (!edev) 8167f2a0a16SChanwoo Choi return -EINVAL; 8177f2a0a16SChanwoo Choi 8187f2a0a16SChanwoo Choi /* Check whether the property is supported or not. */ 8197f2a0a16SChanwoo Choi if (!is_extcon_property_supported(id, prop)) 8207f2a0a16SChanwoo Choi return -EINVAL; 8217f2a0a16SChanwoo Choi 8227f2a0a16SChanwoo Choi /* Find the cable index of external connector by using id. */ 8237f2a0a16SChanwoo Choi index = find_cable_index_by_id(edev, id); 8247f2a0a16SChanwoo Choi if (index < 0) 8257f2a0a16SChanwoo Choi return index; 8267f2a0a16SChanwoo Choi 8277f2a0a16SChanwoo Choi type = get_extcon_type(prop); 8287f2a0a16SChanwoo Choi if (type < 0) 8297f2a0a16SChanwoo Choi return type; 8307f2a0a16SChanwoo Choi 8317f2a0a16SChanwoo Choi cable = &edev->cables[index]; 8327f2a0a16SChanwoo Choi 8337f2a0a16SChanwoo Choi switch (type) { 8347f2a0a16SChanwoo Choi case EXTCON_TYPE_USB: 8357f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); 8367f2a0a16SChanwoo Choi break; 8377f2a0a16SChanwoo Choi case EXTCON_TYPE_CHG: 8387f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); 8397f2a0a16SChanwoo Choi break; 8407f2a0a16SChanwoo Choi case EXTCON_TYPE_JACK: 8417f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); 8427f2a0a16SChanwoo Choi break; 8437f2a0a16SChanwoo Choi case EXTCON_TYPE_DISP: 8447f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); 8457f2a0a16SChanwoo Choi break; 8467f2a0a16SChanwoo Choi default: 8477f2a0a16SChanwoo Choi ret = -EINVAL; 8487f2a0a16SChanwoo Choi } 8497f2a0a16SChanwoo Choi 8507f2a0a16SChanwoo Choi return ret; 8517f2a0a16SChanwoo Choi } 8527f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_capability); 8537f2a0a16SChanwoo Choi 8547f2a0a16SChanwoo Choi /** 855f68a8342SChanwoo Choi * extcon_get_extcon_dev() - Get the extcon device instance from the name 856f68a8342SChanwoo Choi * @extcon_name: The extcon name provided with extcon_dev_register() 857f68a8342SChanwoo Choi */ 858f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) 859f68a8342SChanwoo Choi { 860f68a8342SChanwoo Choi struct extcon_dev *sd; 861f68a8342SChanwoo Choi 8627eae43aeSChanwoo Choi if (!extcon_name) 8637eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 8647eae43aeSChanwoo Choi 865f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 866f68a8342SChanwoo Choi list_for_each_entry(sd, &extcon_dev_list, entry) { 867f68a8342SChanwoo Choi if (!strcmp(sd->name, extcon_name)) 868f68a8342SChanwoo Choi goto out; 869f68a8342SChanwoo Choi } 870f68a8342SChanwoo Choi sd = NULL; 871f68a8342SChanwoo Choi out: 872f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 873f68a8342SChanwoo Choi return sd; 874f68a8342SChanwoo Choi } 875f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); 876f68a8342SChanwoo Choi 877f68a8342SChanwoo Choi /** 878f68a8342SChanwoo Choi * extcon_register_notifier() - Register a notifiee to get notified by 879f68a8342SChanwoo Choi * any attach status changes from the extcon. 880046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 881046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 882f68a8342SChanwoo Choi * @nb: a notifier block to be registered. 883f68a8342SChanwoo Choi * 884f68a8342SChanwoo Choi * Note that the second parameter given to the callback of nb (val) is 885f68a8342SChanwoo Choi * "old_state", not the current state. The current state can be retrieved 886f68a8342SChanwoo Choi * by looking at the third pameter (edev pointer)'s state value. 887f68a8342SChanwoo Choi */ 88873b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, 889f68a8342SChanwoo Choi struct notifier_block *nb) 890f68a8342SChanwoo Choi { 89166bee35fSHans de Goede unsigned long flags; 8922c8116a1SManinder Singh int ret, idx = -EINVAL; 893046050f6SChanwoo Choi 894830ae442SChanwoo Choi if (!nb) 8957eae43aeSChanwoo Choi return -EINVAL; 8967eae43aeSChanwoo Choi 897830ae442SChanwoo Choi if (edev) { 898046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 899a05f44c8SStephen Boyd if (idx < 0) 900a05f44c8SStephen Boyd return idx; 90166bee35fSHans de Goede 90266bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 903046050f6SChanwoo Choi ret = raw_notifier_chain_register(&edev->nh[idx], nb); 90466bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 905830ae442SChanwoo Choi } else { 906830ae442SChanwoo Choi struct extcon_dev *extd; 907830ae442SChanwoo Choi 908830ae442SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 909830ae442SChanwoo Choi list_for_each_entry(extd, &extcon_dev_list, entry) { 910830ae442SChanwoo Choi idx = find_cable_index_by_id(extd, id); 911830ae442SChanwoo Choi if (idx >= 0) 912830ae442SChanwoo Choi break; 913830ae442SChanwoo Choi } 914830ae442SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 915830ae442SChanwoo Choi 916830ae442SChanwoo Choi if (idx >= 0) { 917830ae442SChanwoo Choi edev = extd; 918830ae442SChanwoo Choi return extcon_register_notifier(extd, id, nb); 919830ae442SChanwoo Choi } else { 920830ae442SChanwoo Choi ret = -ENODEV; 921830ae442SChanwoo Choi } 922830ae442SChanwoo Choi } 92366bee35fSHans de Goede 92466bee35fSHans de Goede return ret; 925f68a8342SChanwoo Choi } 926f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier); 927f68a8342SChanwoo Choi 928f68a8342SChanwoo Choi /** 929f68a8342SChanwoo Choi * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. 930046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 931046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 932046050f6SChanwoo Choi * @nb: a notifier block to be registered. 933f68a8342SChanwoo Choi */ 93473b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, 935f68a8342SChanwoo Choi struct notifier_block *nb) 936f68a8342SChanwoo Choi { 93766bee35fSHans de Goede unsigned long flags; 938046050f6SChanwoo Choi int ret, idx; 939046050f6SChanwoo Choi 9407eae43aeSChanwoo Choi if (!edev || !nb) 9417eae43aeSChanwoo Choi return -EINVAL; 9427eae43aeSChanwoo Choi 943046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 944a05f44c8SStephen Boyd if (idx < 0) 945a05f44c8SStephen Boyd return idx; 94666bee35fSHans de Goede 94766bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 948046050f6SChanwoo Choi ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); 94966bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 95066bee35fSHans de Goede 95166bee35fSHans de Goede return ret; 952f68a8342SChanwoo Choi } 953f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier); 954f68a8342SChanwoo Choi 955f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = { 956f68a8342SChanwoo Choi &dev_attr_state.attr, 957f68a8342SChanwoo Choi &dev_attr_name.attr, 958f68a8342SChanwoo Choi NULL, 959f68a8342SChanwoo Choi }; 960f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon); 961f68a8342SChanwoo Choi 962f68a8342SChanwoo Choi static int create_extcon_class(void) 963f68a8342SChanwoo Choi { 964f68a8342SChanwoo Choi if (!extcon_class) { 965f68a8342SChanwoo Choi extcon_class = class_create(THIS_MODULE, "extcon"); 966f68a8342SChanwoo Choi if (IS_ERR(extcon_class)) 967f68a8342SChanwoo Choi return PTR_ERR(extcon_class); 968f68a8342SChanwoo Choi extcon_class->dev_groups = extcon_groups; 969f68a8342SChanwoo Choi 970f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 971f68a8342SChanwoo Choi switch_class = class_compat_register("switch"); 972f68a8342SChanwoo Choi if (WARN(!switch_class, "cannot allocate")) 973f68a8342SChanwoo Choi return -ENOMEM; 974f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 975f68a8342SChanwoo Choi } 976f68a8342SChanwoo Choi 977f68a8342SChanwoo Choi return 0; 978f68a8342SChanwoo Choi } 979f68a8342SChanwoo Choi 980f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev) 981f68a8342SChanwoo Choi { 982f68a8342SChanwoo Choi } 983f68a8342SChanwoo Choi 984f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive"; 985f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev) 986f68a8342SChanwoo Choi { 987f68a8342SChanwoo Choi } 988f68a8342SChanwoo Choi 989f68a8342SChanwoo Choi /* 990f68a8342SChanwoo Choi * extcon_dev_allocate() - Allocate the memory of extcon device. 9912a9de9c0SChanwoo Choi * @supported_cable: Array of supported extcon ending with EXTCON_NONE. 992f68a8342SChanwoo Choi * If supported_cable is NULL, cable name related APIs 993f68a8342SChanwoo Choi * are disabled. 994f68a8342SChanwoo Choi * 995f68a8342SChanwoo Choi * This function allocates the memory for extcon device without allocating 996f68a8342SChanwoo Choi * memory in each extcon provider driver and initialize default setting for 997f68a8342SChanwoo Choi * extcon device. 998f68a8342SChanwoo Choi * 999f68a8342SChanwoo Choi * Return the pointer of extcon device if success or ERR_PTR(err) if fail 1000f68a8342SChanwoo Choi */ 100173b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) 1002f68a8342SChanwoo Choi { 1003f68a8342SChanwoo Choi struct extcon_dev *edev; 1004f68a8342SChanwoo Choi 10057eae43aeSChanwoo Choi if (!supported_cable) 10067eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 10077eae43aeSChanwoo Choi 1008f68a8342SChanwoo Choi edev = kzalloc(sizeof(*edev), GFP_KERNEL); 1009f68a8342SChanwoo Choi if (!edev) 1010f68a8342SChanwoo Choi return ERR_PTR(-ENOMEM); 1011f68a8342SChanwoo Choi 1012f68a8342SChanwoo Choi edev->max_supported = 0; 1013f68a8342SChanwoo Choi edev->supported_cable = supported_cable; 1014f68a8342SChanwoo Choi 1015f68a8342SChanwoo Choi return edev; 1016f68a8342SChanwoo Choi } 1017f68a8342SChanwoo Choi 1018f68a8342SChanwoo Choi /* 1019f68a8342SChanwoo Choi * extcon_dev_free() - Free the memory of extcon device. 1020f68a8342SChanwoo Choi * @edev: the extcon device to free 1021f68a8342SChanwoo Choi */ 1022f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev) 1023f68a8342SChanwoo Choi { 1024f68a8342SChanwoo Choi kfree(edev); 1025f68a8342SChanwoo Choi } 1026f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free); 1027f68a8342SChanwoo Choi 1028f68a8342SChanwoo Choi /** 1029f68a8342SChanwoo Choi * extcon_dev_register() - Register a new extcon device 1030f68a8342SChanwoo Choi * @edev : the new extcon device (should be allocated before calling) 1031f68a8342SChanwoo Choi * 1032f68a8342SChanwoo Choi * Among the members of edev struct, please set the "user initializing data" 1033f68a8342SChanwoo Choi * in any case and set the "optional callbacks" if required. However, please 1034f68a8342SChanwoo Choi * do not set the values of "internal data", which are initialized by 1035f68a8342SChanwoo Choi * this function. 1036f68a8342SChanwoo Choi */ 1037f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev) 1038f68a8342SChanwoo Choi { 1039f68a8342SChanwoo Choi int ret, index = 0; 104071c3ffa5SChanwoo Choi static atomic_t edev_no = ATOMIC_INIT(-1); 1041f68a8342SChanwoo Choi 1042f68a8342SChanwoo Choi if (!extcon_class) { 1043f68a8342SChanwoo Choi ret = create_extcon_class(); 1044f68a8342SChanwoo Choi if (ret < 0) 1045f68a8342SChanwoo Choi return ret; 1046f68a8342SChanwoo Choi } 1047f68a8342SChanwoo Choi 10487eae43aeSChanwoo Choi if (!edev || !edev->supported_cable) 10492a9de9c0SChanwoo Choi return -EINVAL; 1050f68a8342SChanwoo Choi 10512a9de9c0SChanwoo Choi for (; edev->supported_cable[index] != EXTCON_NONE; index++); 10522a9de9c0SChanwoo Choi 10532a9de9c0SChanwoo Choi edev->max_supported = index; 1054f68a8342SChanwoo Choi if (index > SUPPORTED_CABLE_MAX) { 10552a9de9c0SChanwoo Choi dev_err(&edev->dev, 10562a9de9c0SChanwoo Choi "exceed the maximum number of supported cables\n"); 1057f68a8342SChanwoo Choi return -EINVAL; 1058f68a8342SChanwoo Choi } 1059f68a8342SChanwoo Choi 1060f68a8342SChanwoo Choi edev->dev.class = extcon_class; 1061f68a8342SChanwoo Choi edev->dev.release = extcon_dev_release; 1062f68a8342SChanwoo Choi 106371c3ffa5SChanwoo Choi edev->name = dev_name(edev->dev.parent); 1064f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(edev->name)) { 1065f68a8342SChanwoo Choi dev_err(&edev->dev, 1066f68a8342SChanwoo Choi "extcon device name is null\n"); 1067f68a8342SChanwoo Choi return -EINVAL; 1068f68a8342SChanwoo Choi } 106971c3ffa5SChanwoo Choi dev_set_name(&edev->dev, "extcon%lu", 107071c3ffa5SChanwoo Choi (unsigned long)atomic_inc_return(&edev_no)); 1071f68a8342SChanwoo Choi 1072f68a8342SChanwoo Choi if (edev->max_supported) { 1073f68a8342SChanwoo Choi char buf[10]; 1074f68a8342SChanwoo Choi char *str; 1075f68a8342SChanwoo Choi struct extcon_cable *cable; 1076f68a8342SChanwoo Choi 1077f68a8342SChanwoo Choi edev->cables = kzalloc(sizeof(struct extcon_cable) * 1078f68a8342SChanwoo Choi edev->max_supported, GFP_KERNEL); 1079f68a8342SChanwoo Choi if (!edev->cables) { 1080f68a8342SChanwoo Choi ret = -ENOMEM; 1081f68a8342SChanwoo Choi goto err_sysfs_alloc; 1082f68a8342SChanwoo Choi } 1083f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) { 1084f68a8342SChanwoo Choi cable = &edev->cables[index]; 1085f68a8342SChanwoo Choi 1086f68a8342SChanwoo Choi snprintf(buf, 10, "cable.%d", index); 1087f68a8342SChanwoo Choi str = kzalloc(sizeof(char) * (strlen(buf) + 1), 1088f68a8342SChanwoo Choi GFP_KERNEL); 1089f68a8342SChanwoo Choi if (!str) { 1090f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 1091f68a8342SChanwoo Choi cable = &edev->cables[index]; 1092f68a8342SChanwoo Choi kfree(cable->attr_g.name); 1093f68a8342SChanwoo Choi } 1094f68a8342SChanwoo Choi ret = -ENOMEM; 1095f68a8342SChanwoo Choi 1096f68a8342SChanwoo Choi goto err_alloc_cables; 1097f68a8342SChanwoo Choi } 1098f68a8342SChanwoo Choi strcpy(str, buf); 1099f68a8342SChanwoo Choi 1100f68a8342SChanwoo Choi cable->edev = edev; 1101f68a8342SChanwoo Choi cable->cable_index = index; 1102f68a8342SChanwoo Choi cable->attrs[0] = &cable->attr_name.attr; 1103f68a8342SChanwoo Choi cable->attrs[1] = &cable->attr_state.attr; 1104f68a8342SChanwoo Choi cable->attrs[2] = NULL; 1105f68a8342SChanwoo Choi cable->attr_g.name = str; 1106f68a8342SChanwoo Choi cable->attr_g.attrs = cable->attrs; 1107f68a8342SChanwoo Choi 1108f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_name.attr); 1109f68a8342SChanwoo Choi cable->attr_name.attr.name = "name"; 1110f68a8342SChanwoo Choi cable->attr_name.attr.mode = 0444; 1111f68a8342SChanwoo Choi cable->attr_name.show = cable_name_show; 1112f68a8342SChanwoo Choi 1113f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_state.attr); 1114f68a8342SChanwoo Choi cable->attr_state.attr.name = "state"; 1115f68a8342SChanwoo Choi cable->attr_state.attr.mode = 0444; 1116f68a8342SChanwoo Choi cable->attr_state.show = cable_state_show; 1117f68a8342SChanwoo Choi } 1118f68a8342SChanwoo Choi } 1119f68a8342SChanwoo Choi 1120f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 1121f68a8342SChanwoo Choi char buf[80]; 1122f68a8342SChanwoo Choi char *name; 1123f68a8342SChanwoo Choi 1124f68a8342SChanwoo Choi /* Count the size of mutually_exclusive array */ 1125f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 1126f68a8342SChanwoo Choi ; 1127f68a8342SChanwoo Choi 1128f68a8342SChanwoo Choi edev->attrs_muex = kzalloc(sizeof(struct attribute *) * 1129f68a8342SChanwoo Choi (index + 1), GFP_KERNEL); 1130f68a8342SChanwoo Choi if (!edev->attrs_muex) { 1131f68a8342SChanwoo Choi ret = -ENOMEM; 1132f68a8342SChanwoo Choi goto err_muex; 1133f68a8342SChanwoo Choi } 1134f68a8342SChanwoo Choi 1135f68a8342SChanwoo Choi edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * 1136f68a8342SChanwoo Choi index, GFP_KERNEL); 1137f68a8342SChanwoo Choi if (!edev->d_attrs_muex) { 1138f68a8342SChanwoo Choi ret = -ENOMEM; 1139f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1140f68a8342SChanwoo Choi goto err_muex; 1141f68a8342SChanwoo Choi } 1142f68a8342SChanwoo Choi 1143f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) { 1144f68a8342SChanwoo Choi sprintf(buf, "0x%x", edev->mutually_exclusive[index]); 1145f68a8342SChanwoo Choi name = kzalloc(sizeof(char) * (strlen(buf) + 1), 1146f68a8342SChanwoo Choi GFP_KERNEL); 1147f68a8342SChanwoo Choi if (!name) { 1148f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 1149f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr. 1150f68a8342SChanwoo Choi name); 1151f68a8342SChanwoo Choi } 1152f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 1153f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1154f68a8342SChanwoo Choi ret = -ENOMEM; 1155f68a8342SChanwoo Choi goto err_muex; 1156f68a8342SChanwoo Choi } 1157f68a8342SChanwoo Choi strcpy(name, buf); 1158f68a8342SChanwoo Choi sysfs_attr_init(&edev->d_attrs_muex[index].attr); 1159f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.name = name; 1160f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.mode = 0000; 1161f68a8342SChanwoo Choi edev->attrs_muex[index] = &edev->d_attrs_muex[index] 1162f68a8342SChanwoo Choi .attr; 1163f68a8342SChanwoo Choi } 1164f68a8342SChanwoo Choi edev->attr_g_muex.name = muex_name; 1165f68a8342SChanwoo Choi edev->attr_g_muex.attrs = edev->attrs_muex; 1166f68a8342SChanwoo Choi 1167f68a8342SChanwoo Choi } 1168f68a8342SChanwoo Choi 1169f68a8342SChanwoo Choi if (edev->max_supported) { 1170f68a8342SChanwoo Choi edev->extcon_dev_type.groups = 1171f68a8342SChanwoo Choi kzalloc(sizeof(struct attribute_group *) * 1172f68a8342SChanwoo Choi (edev->max_supported + 2), GFP_KERNEL); 1173f68a8342SChanwoo Choi if (!edev->extcon_dev_type.groups) { 1174f68a8342SChanwoo Choi ret = -ENOMEM; 1175f68a8342SChanwoo Choi goto err_alloc_groups; 1176f68a8342SChanwoo Choi } 1177f68a8342SChanwoo Choi 1178f68a8342SChanwoo Choi edev->extcon_dev_type.name = dev_name(&edev->dev); 1179f68a8342SChanwoo Choi edev->extcon_dev_type.release = dummy_sysfs_dev_release; 1180f68a8342SChanwoo Choi 1181f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1182f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 1183f68a8342SChanwoo Choi &edev->cables[index].attr_g; 1184f68a8342SChanwoo Choi if (edev->mutually_exclusive) 1185f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 1186f68a8342SChanwoo Choi &edev->attr_g_muex; 1187f68a8342SChanwoo Choi 1188f68a8342SChanwoo Choi edev->dev.type = &edev->extcon_dev_type; 1189f68a8342SChanwoo Choi } 1190f68a8342SChanwoo Choi 1191f68a8342SChanwoo Choi ret = device_register(&edev->dev); 1192f68a8342SChanwoo Choi if (ret) { 1193f68a8342SChanwoo Choi put_device(&edev->dev); 1194f68a8342SChanwoo Choi goto err_dev; 1195f68a8342SChanwoo Choi } 1196f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 1197f68a8342SChanwoo Choi if (switch_class) 1198f68a8342SChanwoo Choi ret = class_compat_create_link(switch_class, &edev->dev, NULL); 1199f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 1200f68a8342SChanwoo Choi 1201f68a8342SChanwoo Choi spin_lock_init(&edev->lock); 1202f68a8342SChanwoo Choi 1203046050f6SChanwoo Choi edev->nh = devm_kzalloc(&edev->dev, 1204046050f6SChanwoo Choi sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL); 1205046050f6SChanwoo Choi if (!edev->nh) { 1206046050f6SChanwoo Choi ret = -ENOMEM; 1207046050f6SChanwoo Choi goto err_dev; 1208046050f6SChanwoo Choi } 1209046050f6SChanwoo Choi 1210046050f6SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1211046050f6SChanwoo Choi RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); 1212f68a8342SChanwoo Choi 1213f68a8342SChanwoo Choi dev_set_drvdata(&edev->dev, edev); 1214f68a8342SChanwoo Choi edev->state = 0; 1215f68a8342SChanwoo Choi 1216f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 1217f68a8342SChanwoo Choi list_add(&edev->entry, &extcon_dev_list); 1218f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 1219f68a8342SChanwoo Choi 1220f68a8342SChanwoo Choi return 0; 1221f68a8342SChanwoo Choi 1222f68a8342SChanwoo Choi err_dev: 1223f68a8342SChanwoo Choi if (edev->max_supported) 1224f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 1225f68a8342SChanwoo Choi err_alloc_groups: 1226f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 1227f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 1228f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 1229f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 1230f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1231f68a8342SChanwoo Choi } 1232f68a8342SChanwoo Choi err_muex: 1233f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1234f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 1235f68a8342SChanwoo Choi err_alloc_cables: 1236f68a8342SChanwoo Choi if (edev->max_supported) 1237f68a8342SChanwoo Choi kfree(edev->cables); 1238f68a8342SChanwoo Choi err_sysfs_alloc: 1239f68a8342SChanwoo Choi return ret; 1240f68a8342SChanwoo Choi } 1241f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register); 1242f68a8342SChanwoo Choi 1243f68a8342SChanwoo Choi /** 1244f68a8342SChanwoo Choi * extcon_dev_unregister() - Unregister the extcon device. 1245f68a8342SChanwoo Choi * @edev: the extcon device instance to be unregistered. 1246f68a8342SChanwoo Choi * 1247f68a8342SChanwoo Choi * Note that this does not call kfree(edev) because edev was not allocated 1248f68a8342SChanwoo Choi * by this class. 1249f68a8342SChanwoo Choi */ 1250f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev) 1251f68a8342SChanwoo Choi { 1252f68a8342SChanwoo Choi int index; 1253f68a8342SChanwoo Choi 12547eae43aeSChanwoo Choi if (!edev) 12557eae43aeSChanwoo Choi return; 12567eae43aeSChanwoo Choi 1257f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 1258f68a8342SChanwoo Choi list_del(&edev->entry); 1259f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 1260f68a8342SChanwoo Choi 1261f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(get_device(&edev->dev))) { 1262f68a8342SChanwoo Choi dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", 1263f68a8342SChanwoo Choi dev_name(&edev->dev)); 1264f68a8342SChanwoo Choi return; 1265f68a8342SChanwoo Choi } 1266f68a8342SChanwoo Choi 1267f68a8342SChanwoo Choi device_unregister(&edev->dev); 1268f68a8342SChanwoo Choi 1269f68a8342SChanwoo Choi if (edev->mutually_exclusive && edev->max_supported) { 1270f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; 1271f68a8342SChanwoo Choi index++) 1272f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 1273f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 1274f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1275f68a8342SChanwoo Choi } 1276f68a8342SChanwoo Choi 1277f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1278f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 1279f68a8342SChanwoo Choi 1280f68a8342SChanwoo Choi if (edev->max_supported) { 1281f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 1282f68a8342SChanwoo Choi kfree(edev->cables); 1283f68a8342SChanwoo Choi } 1284f68a8342SChanwoo Choi 1285f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 1286f68a8342SChanwoo Choi if (switch_class) 1287f68a8342SChanwoo Choi class_compat_remove_link(switch_class, &edev->dev, NULL); 1288f68a8342SChanwoo Choi #endif 1289f68a8342SChanwoo Choi put_device(&edev->dev); 1290f68a8342SChanwoo Choi } 1291f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister); 1292f68a8342SChanwoo Choi 1293f68a8342SChanwoo Choi #ifdef CONFIG_OF 1294f68a8342SChanwoo Choi /* 1295f68a8342SChanwoo Choi * extcon_get_edev_by_phandle - Get the extcon device from devicetree 1296f68a8342SChanwoo Choi * @dev - instance to the given device 1297f68a8342SChanwoo Choi * @index - index into list of extcon_dev 1298f68a8342SChanwoo Choi * 1299f68a8342SChanwoo Choi * return the instance of extcon device 1300f68a8342SChanwoo Choi */ 1301f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1302f68a8342SChanwoo Choi { 1303f68a8342SChanwoo Choi struct device_node *node; 1304f68a8342SChanwoo Choi struct extcon_dev *edev; 1305f68a8342SChanwoo Choi 13067eae43aeSChanwoo Choi if (!dev) 13077eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 13087eae43aeSChanwoo Choi 1309f68a8342SChanwoo Choi if (!dev->of_node) { 1310e8752b7aSStephen Boyd dev_dbg(dev, "device does not have a device node entry\n"); 1311f68a8342SChanwoo Choi return ERR_PTR(-EINVAL); 1312f68a8342SChanwoo Choi } 1313f68a8342SChanwoo Choi 1314f68a8342SChanwoo Choi node = of_parse_phandle(dev->of_node, "extcon", index); 1315f68a8342SChanwoo Choi if (!node) { 1316e8752b7aSStephen Boyd dev_dbg(dev, "failed to get phandle in %s node\n", 1317f68a8342SChanwoo Choi dev->of_node->full_name); 1318f68a8342SChanwoo Choi return ERR_PTR(-ENODEV); 1319f68a8342SChanwoo Choi } 1320f68a8342SChanwoo Choi 1321f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 1322f68a8342SChanwoo Choi list_for_each_entry(edev, &extcon_dev_list, entry) { 1323f68a8342SChanwoo Choi if (edev->dev.parent && edev->dev.parent->of_node == node) { 1324f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 13255d5c4c13SPeter Chen of_node_put(node); 1326f68a8342SChanwoo Choi return edev; 1327f68a8342SChanwoo Choi } 1328f68a8342SChanwoo Choi } 1329f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 13305d5c4c13SPeter Chen of_node_put(node); 1331f68a8342SChanwoo Choi 1332f68a8342SChanwoo Choi return ERR_PTR(-EPROBE_DEFER); 1333f68a8342SChanwoo Choi } 1334f68a8342SChanwoo Choi #else 1335f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1336f68a8342SChanwoo Choi { 1337f68a8342SChanwoo Choi return ERR_PTR(-ENOSYS); 1338f68a8342SChanwoo Choi } 1339f68a8342SChanwoo Choi #endif /* CONFIG_OF */ 1340f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); 1341f68a8342SChanwoo Choi 1342707d7550SChanwoo Choi /** 1343707d7550SChanwoo Choi * extcon_get_edev_name() - Get the name of the extcon device. 1344707d7550SChanwoo Choi * @edev: the extcon device 1345707d7550SChanwoo Choi */ 1346707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev) 1347707d7550SChanwoo Choi { 1348707d7550SChanwoo Choi return !edev ? NULL : edev->name; 1349707d7550SChanwoo Choi } 1350707d7550SChanwoo Choi 1351f68a8342SChanwoo Choi static int __init extcon_class_init(void) 1352f68a8342SChanwoo Choi { 1353f68a8342SChanwoo Choi return create_extcon_class(); 1354f68a8342SChanwoo Choi } 1355f68a8342SChanwoo Choi module_init(extcon_class_init); 1356f68a8342SChanwoo Choi 1357f68a8342SChanwoo Choi static void __exit extcon_class_exit(void) 1358f68a8342SChanwoo Choi { 1359f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 1360f68a8342SChanwoo Choi class_compat_unregister(switch_class); 1361f68a8342SChanwoo Choi #endif 1362f68a8342SChanwoo Choi class_destroy(extcon_class); 1363f68a8342SChanwoo Choi } 1364f68a8342SChanwoo Choi module_exit(extcon_class_exit); 1365f68a8342SChanwoo Choi 13662a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <[email protected]>"); 1367f68a8342SChanwoo Choi MODULE_AUTHOR("Mike Lockwood <[email protected]>"); 1368f68a8342SChanwoo Choi MODULE_AUTHOR("Donggeun Kim <[email protected]>"); 1369f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <[email protected]>"); 1370f68a8342SChanwoo Choi MODULE_DESCRIPTION("External connector (extcon) class driver"); 1371f68a8342SChanwoo Choi MODULE_LICENSE("GPL"); 1372