1f68a8342SChanwoo Choi /* 2b9ec23c0SChanwoo Choi * drivers/extcon/extcon.c - External Connector (extcon) framework. 3f68a8342SChanwoo Choi * 42a9de9c0SChanwoo Choi * Copyright (C) 2015 Samsung Electronics 52a9de9c0SChanwoo Choi * Author: Chanwoo Choi <[email protected]> 62a9de9c0SChanwoo Choi * 7f68a8342SChanwoo Choi * Copyright (C) 2012 Samsung Electronics 8f68a8342SChanwoo Choi * Author: Donggeun Kim <[email protected]> 9f68a8342SChanwoo Choi * Author: MyungJoo Ham <[email protected]> 10f68a8342SChanwoo Choi * 11f68a8342SChanwoo Choi * based on android/drivers/switch/switch_class.c 12f68a8342SChanwoo Choi * Copyright (C) 2008 Google, Inc. 13f68a8342SChanwoo Choi * Author: Mike Lockwood <[email protected]> 14f68a8342SChanwoo Choi * 15f68a8342SChanwoo Choi * This software is licensed under the terms of the GNU General Public 16f68a8342SChanwoo Choi * License version 2, as published by the Free Software Foundation, and 17f68a8342SChanwoo Choi * may be copied, distributed, and modified under those terms. 18f68a8342SChanwoo Choi * 19f68a8342SChanwoo Choi * This program is distributed in the hope that it will be useful, 20f68a8342SChanwoo Choi * but WITHOUT ANY WARRANTY; without even the implied warranty of 21f68a8342SChanwoo Choi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22f68a8342SChanwoo Choi * GNU General Public License for more details. 23f68a8342SChanwoo Choi */ 24f68a8342SChanwoo Choi 25f68a8342SChanwoo Choi #include <linux/module.h> 26f68a8342SChanwoo Choi #include <linux/types.h> 27f68a8342SChanwoo Choi #include <linux/init.h> 28f68a8342SChanwoo Choi #include <linux/device.h> 29f68a8342SChanwoo Choi #include <linux/fs.h> 30f68a8342SChanwoo Choi #include <linux/err.h> 31f68a8342SChanwoo Choi #include <linux/of.h> 32f68a8342SChanwoo Choi #include <linux/slab.h> 33f68a8342SChanwoo Choi #include <linux/sysfs.h> 34f68a8342SChanwoo Choi 35e6cf0465SChanwoo Choi #include "extcon.h" 36e6cf0465SChanwoo Choi 372a9de9c0SChanwoo Choi #define SUPPORTED_CABLE_MAX 32 382a9de9c0SChanwoo Choi 395183240cSColin Ian King static const struct __extcon_info { 4055e4e2f1SChanwoo Choi unsigned int type; 4155e4e2f1SChanwoo Choi unsigned int id; 4255e4e2f1SChanwoo Choi const char *name; 4355e4e2f1SChanwoo Choi 4455e4e2f1SChanwoo Choi } extcon_info[] = { 4555e4e2f1SChanwoo Choi [EXTCON_NONE] = { 4655e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 4755e4e2f1SChanwoo Choi .id = EXTCON_NONE, 4855e4e2f1SChanwoo Choi .name = "NONE", 4955e4e2f1SChanwoo Choi }, 5073b6ecdbSChanwoo Choi 518e9bc36dSChanwoo Choi /* USB external connector */ 5255e4e2f1SChanwoo Choi [EXTCON_USB] = { 5355e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 5455e4e2f1SChanwoo Choi .id = EXTCON_USB, 5555e4e2f1SChanwoo Choi .name = "USB", 5655e4e2f1SChanwoo Choi }, 5755e4e2f1SChanwoo Choi [EXTCON_USB_HOST] = { 5855e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 5955e4e2f1SChanwoo Choi .id = EXTCON_USB_HOST, 6086d6cda6SChanwoo Choi .name = "USB-HOST", 6155e4e2f1SChanwoo Choi }, 628e9bc36dSChanwoo Choi 6311eecf91SChanwoo Choi /* Charging external connector */ 6455e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SDP] = { 6555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 6655e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SDP, 6755e4e2f1SChanwoo Choi .name = "SDP", 6855e4e2f1SChanwoo Choi }, 6955e4e2f1SChanwoo Choi [EXTCON_CHG_USB_DCP] = { 7055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 7155e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_DCP, 7255e4e2f1SChanwoo Choi .name = "DCP", 7355e4e2f1SChanwoo Choi }, 7455e4e2f1SChanwoo Choi [EXTCON_CHG_USB_CDP] = { 7555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 7655e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_CDP, 7755e4e2f1SChanwoo Choi .name = "CDP", 7855e4e2f1SChanwoo Choi }, 7955e4e2f1SChanwoo Choi [EXTCON_CHG_USB_ACA] = { 8055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 8155e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_ACA, 8255e4e2f1SChanwoo Choi .name = "ACA", 8355e4e2f1SChanwoo Choi }, 8455e4e2f1SChanwoo Choi [EXTCON_CHG_USB_FAST] = { 8555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 8655e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_FAST, 8755e4e2f1SChanwoo Choi .name = "FAST-CHARGER", 8855e4e2f1SChanwoo Choi }, 8955e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SLOW] = { 9055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 9155e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SLOW, 9255e4e2f1SChanwoo Choi .name = "SLOW-CHARGER", 9355e4e2f1SChanwoo Choi }, 947fe95fb8SChanwoo Choi [EXTCON_CHG_WPT] = { 957fe95fb8SChanwoo Choi .type = EXTCON_TYPE_CHG, 967fe95fb8SChanwoo Choi .id = EXTCON_CHG_WPT, 977fe95fb8SChanwoo Choi .name = "WPT", 987fe95fb8SChanwoo Choi }, 993c5f0e07SChanwoo Choi [EXTCON_CHG_USB_PD] = { 1003c5f0e07SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 1013c5f0e07SChanwoo Choi .id = EXTCON_CHG_USB_PD, 1023c5f0e07SChanwoo Choi .name = "PD", 1033c5f0e07SChanwoo Choi }, 1048e9bc36dSChanwoo Choi 10511eecf91SChanwoo Choi /* Jack external connector */ 10655e4e2f1SChanwoo Choi [EXTCON_JACK_MICROPHONE] = { 10755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 10855e4e2f1SChanwoo Choi .id = EXTCON_JACK_MICROPHONE, 10955e4e2f1SChanwoo Choi .name = "MICROPHONE", 11055e4e2f1SChanwoo Choi }, 11155e4e2f1SChanwoo Choi [EXTCON_JACK_HEADPHONE] = { 11255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 11355e4e2f1SChanwoo Choi .id = EXTCON_JACK_HEADPHONE, 11455e4e2f1SChanwoo Choi .name = "HEADPHONE", 11555e4e2f1SChanwoo Choi }, 11655e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_IN] = { 11755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 11855e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_IN, 11955e4e2f1SChanwoo Choi .name = "LINE-IN", 12055e4e2f1SChanwoo Choi }, 12155e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_OUT] = { 12255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 12355e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_OUT, 12455e4e2f1SChanwoo Choi .name = "LINE-OUT", 12555e4e2f1SChanwoo Choi }, 12655e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_IN] = { 12755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 12855e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_IN, 12955e4e2f1SChanwoo Choi .name = "VIDEO-IN", 13055e4e2f1SChanwoo Choi }, 13155e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_OUT] = { 13255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 13355e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_OUT, 13455e4e2f1SChanwoo Choi .name = "VIDEO-OUT", 13555e4e2f1SChanwoo Choi }, 13655e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_IN] = { 13755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 13855e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_IN, 13955e4e2f1SChanwoo Choi .name = "SPDIF-IN", 14055e4e2f1SChanwoo Choi }, 14155e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_OUT] = { 14255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 14355e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_OUT, 14455e4e2f1SChanwoo Choi .name = "SPDIF-OUT", 14555e4e2f1SChanwoo Choi }, 1468e9bc36dSChanwoo Choi 14711eecf91SChanwoo Choi /* Display external connector */ 14855e4e2f1SChanwoo Choi [EXTCON_DISP_HDMI] = { 14955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 15055e4e2f1SChanwoo Choi .id = EXTCON_DISP_HDMI, 15155e4e2f1SChanwoo Choi .name = "HDMI", 15255e4e2f1SChanwoo Choi }, 15355e4e2f1SChanwoo Choi [EXTCON_DISP_MHL] = { 15455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 15555e4e2f1SChanwoo Choi .id = EXTCON_DISP_MHL, 15655e4e2f1SChanwoo Choi .name = "MHL", 15755e4e2f1SChanwoo Choi }, 15855e4e2f1SChanwoo Choi [EXTCON_DISP_DVI] = { 15955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 16055e4e2f1SChanwoo Choi .id = EXTCON_DISP_DVI, 16155e4e2f1SChanwoo Choi .name = "DVI", 16255e4e2f1SChanwoo Choi }, 16355e4e2f1SChanwoo Choi [EXTCON_DISP_VGA] = { 16455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 16555e4e2f1SChanwoo Choi .id = EXTCON_DISP_VGA, 16655e4e2f1SChanwoo Choi .name = "VGA", 16755e4e2f1SChanwoo Choi }, 1682164188dSChris Zhong [EXTCON_DISP_DP] = { 1692164188dSChris Zhong .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB, 1702164188dSChris Zhong .id = EXTCON_DISP_DP, 1712164188dSChris Zhong .name = "DP", 1722164188dSChris Zhong }, 1739c0595d6SChanwoo Choi [EXTCON_DISP_HMD] = { 1749c0595d6SChanwoo Choi .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB, 1759c0595d6SChanwoo Choi .id = EXTCON_DISP_HMD, 1769c0595d6SChanwoo Choi .name = "HMD", 1779c0595d6SChanwoo Choi }, 1788e9bc36dSChanwoo Choi 17911eecf91SChanwoo Choi /* Miscellaneous external connector */ 18055e4e2f1SChanwoo Choi [EXTCON_DOCK] = { 18155e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 18255e4e2f1SChanwoo Choi .id = EXTCON_DOCK, 18355e4e2f1SChanwoo Choi .name = "DOCK", 18455e4e2f1SChanwoo Choi }, 18555e4e2f1SChanwoo Choi [EXTCON_JIG] = { 18655e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 18755e4e2f1SChanwoo Choi .id = EXTCON_JIG, 18855e4e2f1SChanwoo Choi .name = "JIG", 18955e4e2f1SChanwoo Choi }, 19055e4e2f1SChanwoo Choi [EXTCON_MECHANICAL] = { 19155e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 19255e4e2f1SChanwoo Choi .id = EXTCON_MECHANICAL, 19355e4e2f1SChanwoo Choi .name = "MECHANICAL", 19455e4e2f1SChanwoo Choi }, 1958e9bc36dSChanwoo Choi 19655e4e2f1SChanwoo Choi { /* sentinel */ } 197f68a8342SChanwoo Choi }; 198f68a8342SChanwoo Choi 19920f7b53dSChanwoo Choi /** 2006ab6094fSChanwoo Choi * struct extcon_cable - An internal data for an external connector. 2016ab6094fSChanwoo Choi * @edev: the extcon device 2026ab6094fSChanwoo Choi * @cable_index: the index of this cable in the edev 2036ab6094fSChanwoo Choi * @attr_g: the attribute group for the cable 20420f7b53dSChanwoo Choi * @attr_name: "name" sysfs entry 20520f7b53dSChanwoo Choi * @attr_state: "state" sysfs entry 2066ab6094fSChanwoo Choi * @attrs: the array pointing to attr_name and attr_state for attr_g 20720f7b53dSChanwoo Choi */ 20820f7b53dSChanwoo Choi struct extcon_cable { 20920f7b53dSChanwoo Choi struct extcon_dev *edev; 21020f7b53dSChanwoo Choi int cable_index; 21120f7b53dSChanwoo Choi 21220f7b53dSChanwoo Choi struct attribute_group attr_g; 21320f7b53dSChanwoo Choi struct device_attribute attr_name; 21420f7b53dSChanwoo Choi struct device_attribute attr_state; 21520f7b53dSChanwoo Choi 21620f7b53dSChanwoo Choi struct attribute *attrs[3]; /* to be fed to attr_g.attrs */ 217792e7e9eSChanwoo Choi 218792e7e9eSChanwoo Choi union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT]; 219792e7e9eSChanwoo Choi union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT]; 220792e7e9eSChanwoo Choi union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT]; 221792e7e9eSChanwoo Choi union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT]; 2227f2a0a16SChanwoo Choi 2237f2a0a16SChanwoo Choi unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)]; 2247f2a0a16SChanwoo Choi unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)]; 2257f2a0a16SChanwoo Choi unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)]; 2267f2a0a16SChanwoo Choi unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)]; 22720f7b53dSChanwoo Choi }; 22820f7b53dSChanwoo Choi 229f68a8342SChanwoo Choi static struct class *extcon_class; 230f68a8342SChanwoo Choi 231f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list); 232f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock); 233f68a8342SChanwoo Choi 234f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) 235f68a8342SChanwoo Choi { 236f68a8342SChanwoo Choi int i = 0; 237f68a8342SChanwoo Choi 238f68a8342SChanwoo Choi if (!edev->mutually_exclusive) 239f68a8342SChanwoo Choi return 0; 240f68a8342SChanwoo Choi 241f68a8342SChanwoo Choi for (i = 0; edev->mutually_exclusive[i]; i++) { 242f68a8342SChanwoo Choi int weight; 243f68a8342SChanwoo Choi u32 correspondants = new_state & edev->mutually_exclusive[i]; 244f68a8342SChanwoo Choi 245f68a8342SChanwoo Choi /* calculate the total number of bits set */ 246f68a8342SChanwoo Choi weight = hweight32(correspondants); 247f68a8342SChanwoo Choi if (weight > 1) 248f68a8342SChanwoo Choi return i + 1; 249f68a8342SChanwoo Choi } 250f68a8342SChanwoo Choi 251f68a8342SChanwoo Choi return 0; 252f68a8342SChanwoo Choi } 253f68a8342SChanwoo Choi 25473b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id) 2552a9de9c0SChanwoo Choi { 2562a9de9c0SChanwoo Choi int i; 2572a9de9c0SChanwoo Choi 2582a9de9c0SChanwoo Choi /* Find the the index of extcon cable in edev->supported_cable */ 2592a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 2602a9de9c0SChanwoo Choi if (edev->supported_cable[i] == id) 2612a9de9c0SChanwoo Choi return i; 2622a9de9c0SChanwoo Choi } 2632a9de9c0SChanwoo Choi 2642a9de9c0SChanwoo Choi return -EINVAL; 2652a9de9c0SChanwoo Choi } 2662a9de9c0SChanwoo Choi 267792e7e9eSChanwoo Choi static int get_extcon_type(unsigned int prop) 268792e7e9eSChanwoo Choi { 269792e7e9eSChanwoo Choi switch (prop) { 270792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 271792e7e9eSChanwoo Choi return EXTCON_TYPE_USB; 272792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 273792e7e9eSChanwoo Choi return EXTCON_TYPE_CHG; 274792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 275792e7e9eSChanwoo Choi return EXTCON_TYPE_JACK; 276792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 277792e7e9eSChanwoo Choi return EXTCON_TYPE_DISP; 278792e7e9eSChanwoo Choi default: 279792e7e9eSChanwoo Choi return -EINVAL; 280792e7e9eSChanwoo Choi } 281792e7e9eSChanwoo Choi } 282792e7e9eSChanwoo Choi 283792e7e9eSChanwoo Choi static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index) 284792e7e9eSChanwoo Choi { 285792e7e9eSChanwoo Choi return !!(edev->state & BIT(index)); 286792e7e9eSChanwoo Choi } 287792e7e9eSChanwoo Choi 288ab11af04SChanwoo Choi static bool is_extcon_changed(struct extcon_dev *edev, int index, 289ab11af04SChanwoo Choi bool new_state) 290046050f6SChanwoo Choi { 291ab11af04SChanwoo Choi int state = !!(edev->state & BIT(index)); 292ab11af04SChanwoo Choi return (state != new_state); 293046050f6SChanwoo Choi } 294046050f6SChanwoo Choi 295792e7e9eSChanwoo Choi static bool is_extcon_property_supported(unsigned int id, unsigned int prop) 296792e7e9eSChanwoo Choi { 297792e7e9eSChanwoo Choi int type; 298792e7e9eSChanwoo Choi 299792e7e9eSChanwoo Choi /* Check whether the property is supported or not. */ 300792e7e9eSChanwoo Choi type = get_extcon_type(prop); 301792e7e9eSChanwoo Choi if (type < 0) 302792e7e9eSChanwoo Choi return false; 303792e7e9eSChanwoo Choi 304792e7e9eSChanwoo Choi /* Check whether a specific extcon id supports the property or not. */ 305792e7e9eSChanwoo Choi return !!(extcon_info[id].type & type); 306792e7e9eSChanwoo Choi } 307792e7e9eSChanwoo Choi 3087f2a0a16SChanwoo Choi static int is_extcon_property_capability(struct extcon_dev *edev, 3097f2a0a16SChanwoo Choi unsigned int id, int index,unsigned int prop) 3107f2a0a16SChanwoo Choi { 3117f2a0a16SChanwoo Choi struct extcon_cable *cable; 3127f2a0a16SChanwoo Choi int type, ret; 3137f2a0a16SChanwoo Choi 3147f2a0a16SChanwoo Choi /* Check whether the property is supported or not. */ 3157f2a0a16SChanwoo Choi type = get_extcon_type(prop); 3167f2a0a16SChanwoo Choi if (type < 0) 3177f2a0a16SChanwoo Choi return type; 3187f2a0a16SChanwoo Choi 3197f2a0a16SChanwoo Choi cable = &edev->cables[index]; 3207f2a0a16SChanwoo Choi 3217f2a0a16SChanwoo Choi switch (type) { 3227f2a0a16SChanwoo Choi case EXTCON_TYPE_USB: 3237f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); 3247f2a0a16SChanwoo Choi break; 3257f2a0a16SChanwoo Choi case EXTCON_TYPE_CHG: 3267f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); 3277f2a0a16SChanwoo Choi break; 3287f2a0a16SChanwoo Choi case EXTCON_TYPE_JACK: 3297f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); 3307f2a0a16SChanwoo Choi break; 3317f2a0a16SChanwoo Choi case EXTCON_TYPE_DISP: 3327f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); 3337f2a0a16SChanwoo Choi break; 3347f2a0a16SChanwoo Choi default: 3357f2a0a16SChanwoo Choi ret = -EINVAL; 3367f2a0a16SChanwoo Choi } 3377f2a0a16SChanwoo Choi 3387f2a0a16SChanwoo Choi return ret; 3397f2a0a16SChanwoo Choi } 3407f2a0a16SChanwoo Choi 341792e7e9eSChanwoo Choi static void init_property(struct extcon_dev *edev, unsigned int id, int index) 342792e7e9eSChanwoo Choi { 343792e7e9eSChanwoo Choi unsigned int type = extcon_info[id].type; 344792e7e9eSChanwoo Choi struct extcon_cable *cable = &edev->cables[index]; 345792e7e9eSChanwoo Choi 346792e7e9eSChanwoo Choi if (EXTCON_TYPE_USB & type) 347792e7e9eSChanwoo Choi memset(cable->usb_propval, 0, sizeof(cable->usb_propval)); 348792e7e9eSChanwoo Choi if (EXTCON_TYPE_CHG & type) 349792e7e9eSChanwoo Choi memset(cable->chg_propval, 0, sizeof(cable->chg_propval)); 350792e7e9eSChanwoo Choi if (EXTCON_TYPE_JACK & type) 351792e7e9eSChanwoo Choi memset(cable->jack_propval, 0, sizeof(cable->jack_propval)); 352792e7e9eSChanwoo Choi if (EXTCON_TYPE_DISP & type) 353792e7e9eSChanwoo Choi memset(cable->disp_propval, 0, sizeof(cable->disp_propval)); 354792e7e9eSChanwoo Choi } 355792e7e9eSChanwoo Choi 356f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr, 357f68a8342SChanwoo Choi char *buf) 358f68a8342SChanwoo Choi { 359f68a8342SChanwoo Choi int i, count = 0; 360f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 361f68a8342SChanwoo Choi 362f68a8342SChanwoo Choi if (edev->max_supported == 0) 363f68a8342SChanwoo Choi return sprintf(buf, "%u\n", edev->state); 364f68a8342SChanwoo Choi 3652a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 366f68a8342SChanwoo Choi count += sprintf(buf + count, "%s=%d\n", 36755e4e2f1SChanwoo Choi extcon_info[edev->supported_cable[i]].name, 36870641a0aSChanwoo Choi !!(edev->state & BIT(i))); 369f68a8342SChanwoo Choi } 370f68a8342SChanwoo Choi 371f68a8342SChanwoo Choi return count; 372f68a8342SChanwoo Choi } 3735d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state); 374f68a8342SChanwoo Choi 375f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr, 376f68a8342SChanwoo Choi char *buf) 377f68a8342SChanwoo Choi { 378f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 379f68a8342SChanwoo Choi 38071c3ffa5SChanwoo Choi return sprintf(buf, "%s\n", edev->name); 381f68a8342SChanwoo Choi } 382f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name); 383f68a8342SChanwoo Choi 384f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev, 385f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 386f68a8342SChanwoo Choi { 387f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 388f68a8342SChanwoo Choi attr_name); 3892a9de9c0SChanwoo Choi int i = cable->cable_index; 390f68a8342SChanwoo Choi 391f68a8342SChanwoo Choi return sprintf(buf, "%s\n", 39255e4e2f1SChanwoo Choi extcon_info[cable->edev->supported_cable[i]].name); 393f68a8342SChanwoo Choi } 394f68a8342SChanwoo Choi 395f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev, 396f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 397f68a8342SChanwoo Choi { 398f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 399f68a8342SChanwoo Choi attr_state); 400f68a8342SChanwoo Choi 401be052cc8SRoger Quadros int i = cable->cable_index; 402be052cc8SRoger Quadros 403f68a8342SChanwoo Choi return sprintf(buf, "%d\n", 404575c2b86SChanwoo Choi extcon_get_state(cable->edev, cable->edev->supported_cable[i])); 405f68a8342SChanwoo Choi } 406f68a8342SChanwoo Choi 407f68a8342SChanwoo Choi /** 4086ab6094fSChanwoo Choi * extcon_sync() - Synchronize the state for an external connector. 4096ab6094fSChanwoo Choi * @edev: the extcon device 410f68a8342SChanwoo Choi * 4116ab6094fSChanwoo Choi * Note that this function send a notification in order to synchronize 4126ab6094fSChanwoo Choi * the state and property of an external connector. 4136ab6094fSChanwoo Choi * 4146ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 415f68a8342SChanwoo Choi */ 416ab11af04SChanwoo Choi int extcon_sync(struct extcon_dev *edev, unsigned int id) 417f68a8342SChanwoo Choi { 418f68a8342SChanwoo Choi char name_buf[120]; 419f68a8342SChanwoo Choi char state_buf[120]; 420f68a8342SChanwoo Choi char *prop_buf; 421f68a8342SChanwoo Choi char *envp[3]; 422f68a8342SChanwoo Choi int env_offset = 0; 423f68a8342SChanwoo Choi int length; 424046050f6SChanwoo Choi int index; 425ab11af04SChanwoo Choi int state; 426f68a8342SChanwoo Choi unsigned long flags; 427f68a8342SChanwoo Choi 4287eae43aeSChanwoo Choi if (!edev) 4297eae43aeSChanwoo Choi return -EINVAL; 4307eae43aeSChanwoo Choi 431ab11af04SChanwoo Choi index = find_cable_index_by_id(edev, id); 432ab11af04SChanwoo Choi if (index < 0) 433ab11af04SChanwoo Choi return index; 434ab11af04SChanwoo Choi 435f68a8342SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 436ab11af04SChanwoo Choi state = !!(edev->state & BIT(index)); 437*8a9dbb77SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 438815429b3SChanwoo Choi 439815429b3SChanwoo Choi /* 440815429b3SChanwoo Choi * Call functions in a raw notifier chain for the specific one 441815429b3SChanwoo Choi * external connector. 442815429b3SChanwoo Choi */ 443ab11af04SChanwoo Choi raw_notifier_call_chain(&edev->nh[index], state, edev); 444f7a89811SRoger Quadros 445815429b3SChanwoo Choi /* 446815429b3SChanwoo Choi * Call functions in a raw notifier chain for the all supported 447815429b3SChanwoo Choi * external connectors. 448815429b3SChanwoo Choi */ 449815429b3SChanwoo Choi raw_notifier_call_chain(&edev->nh_all, state, edev); 450815429b3SChanwoo Choi 451*8a9dbb77SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 452f68a8342SChanwoo Choi /* This could be in interrupt handler */ 453f68a8342SChanwoo Choi prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); 454ab11af04SChanwoo Choi if (!prop_buf) { 455f68a8342SChanwoo Choi /* Unlock early before uevent */ 456f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 457f68a8342SChanwoo Choi 458f68a8342SChanwoo Choi dev_err(&edev->dev, "out of memory in extcon_set_state\n"); 459f68a8342SChanwoo Choi kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); 460f68a8342SChanwoo Choi 461e7d9dd5aSPan Bian return -ENOMEM; 462f68a8342SChanwoo Choi } 463f68a8342SChanwoo Choi 464ab11af04SChanwoo Choi length = name_show(&edev->dev, NULL, prop_buf); 465ab11af04SChanwoo Choi if (length > 0) { 466ab11af04SChanwoo Choi if (prop_buf[length - 1] == '\n') 467ab11af04SChanwoo Choi prop_buf[length - 1] = 0; 468ab11af04SChanwoo Choi snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf); 469ab11af04SChanwoo Choi envp[env_offset++] = name_buf; 470ab11af04SChanwoo Choi } 471ab11af04SChanwoo Choi 472ab11af04SChanwoo Choi length = state_show(&edev->dev, NULL, prop_buf); 473ab11af04SChanwoo Choi if (length > 0) { 474ab11af04SChanwoo Choi if (prop_buf[length - 1] == '\n') 475ab11af04SChanwoo Choi prop_buf[length - 1] = 0; 476ab11af04SChanwoo Choi snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf); 477ab11af04SChanwoo Choi envp[env_offset++] = state_buf; 478ab11af04SChanwoo Choi } 479ab11af04SChanwoo Choi envp[env_offset] = NULL; 480ab11af04SChanwoo Choi 481ab11af04SChanwoo Choi /* Unlock early before uevent */ 482ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 483ab11af04SChanwoo Choi kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); 484ab11af04SChanwoo Choi free_page((unsigned long)prop_buf); 485ab11af04SChanwoo Choi 486ab11af04SChanwoo Choi return 0; 487ab11af04SChanwoo Choi } 488ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_sync); 489ab11af04SChanwoo Choi 490f68a8342SChanwoo Choi /** 4916ab6094fSChanwoo Choi * extcon_get_state() - Get the state of an external connector. 4926ab6094fSChanwoo Choi * @edev: the extcon device 4936ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 4946ab6094fSChanwoo Choi * 4956ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 496f68a8342SChanwoo Choi */ 497575c2b86SChanwoo Choi int extcon_get_state(struct extcon_dev *edev, const unsigned int id) 498f68a8342SChanwoo Choi { 499575c2b86SChanwoo Choi int index, state; 500575c2b86SChanwoo Choi unsigned long flags; 5012a9de9c0SChanwoo Choi 5027eae43aeSChanwoo Choi if (!edev) 5037eae43aeSChanwoo Choi return -EINVAL; 5047eae43aeSChanwoo Choi 5052a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 5062a9de9c0SChanwoo Choi if (index < 0) 5072a9de9c0SChanwoo Choi return index; 5082a9de9c0SChanwoo Choi 509575c2b86SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 510575c2b86SChanwoo Choi state = is_extcon_attached(edev, index); 511575c2b86SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 512f68a8342SChanwoo Choi 513575c2b86SChanwoo Choi return state; 514f68a8342SChanwoo Choi } 515575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_state); 516f68a8342SChanwoo Choi 517f68a8342SChanwoo Choi /** 5186ab6094fSChanwoo Choi * extcon_set_state() - Set the state of an external connector. 5196ab6094fSChanwoo Choi * @edev: the extcon device 5206ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 5216ab6094fSChanwoo Choi * @state: the new state of an external connector. 5226ab6094fSChanwoo Choi * the default semantics is true: attached / false: detached. 523ab11af04SChanwoo Choi * 5246ab6094fSChanwoo Choi * Note that this function set the state of an external connector without 5256ab6094fSChanwoo Choi * a notification. To synchronize the state of an external connector, 5266ab6094fSChanwoo Choi * have to use extcon_set_state_sync() and extcon_sync(). 5276ab6094fSChanwoo Choi * 5286ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 529f68a8342SChanwoo Choi */ 5306ab6094fSChanwoo Choi int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state) 531f68a8342SChanwoo Choi { 532ab11af04SChanwoo Choi unsigned long flags; 533ab11af04SChanwoo Choi int index, ret = 0; 534f68a8342SChanwoo Choi 5357eae43aeSChanwoo Choi if (!edev) 5367eae43aeSChanwoo Choi return -EINVAL; 5377eae43aeSChanwoo Choi 5382a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 5392a9de9c0SChanwoo Choi if (index < 0) 5402a9de9c0SChanwoo Choi return index; 5412a9de9c0SChanwoo Choi 542ab11af04SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 543ab11af04SChanwoo Choi 544ab11af04SChanwoo Choi /* Check whether the external connector's state is changed. */ 5456ab6094fSChanwoo Choi if (!is_extcon_changed(edev, index, state)) 546ab11af04SChanwoo Choi goto out; 547ab11af04SChanwoo Choi 548ab11af04SChanwoo Choi if (check_mutually_exclusive(edev, 5496ab6094fSChanwoo Choi (edev->state & ~BIT(index)) | (state & BIT(index)))) { 550ab11af04SChanwoo Choi ret = -EPERM; 551ab11af04SChanwoo Choi goto out; 552ab11af04SChanwoo Choi } 553ab11af04SChanwoo Choi 554792e7e9eSChanwoo Choi /* 555792e7e9eSChanwoo Choi * Initialize the value of extcon property before setting 556792e7e9eSChanwoo Choi * the detached state for an external connector. 557792e7e9eSChanwoo Choi */ 5586ab6094fSChanwoo Choi if (!state) 559792e7e9eSChanwoo Choi init_property(edev, id, index); 560792e7e9eSChanwoo Choi 5616ab6094fSChanwoo Choi /* Update the state for an external connector. */ 5626ab6094fSChanwoo Choi if (state) 563ab11af04SChanwoo Choi edev->state |= BIT(index); 564ab11af04SChanwoo Choi else 565ab11af04SChanwoo Choi edev->state &= ~(BIT(index)); 566ab11af04SChanwoo Choi out: 567ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 568ab11af04SChanwoo Choi 569ab11af04SChanwoo Choi return ret; 570f68a8342SChanwoo Choi } 571575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state); 572f68a8342SChanwoo Choi 573f68a8342SChanwoo Choi /** 5746ab6094fSChanwoo Choi * extcon_set_state_sync() - Set the state of an external connector with sync. 5756ab6094fSChanwoo Choi * @edev: the extcon device 5766ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 5776ab6094fSChanwoo Choi * @state: the new state of external connector. 5786ab6094fSChanwoo Choi * the default semantics is true: attached / false: detached. 579ab11af04SChanwoo Choi * 5806ab6094fSChanwoo Choi * Note that this function set the state of external connector 5816ab6094fSChanwoo Choi * and synchronize the state by sending a notification. 5826ab6094fSChanwoo Choi * 5836ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 584ab11af04SChanwoo Choi */ 5856ab6094fSChanwoo Choi int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state) 586ab11af04SChanwoo Choi { 587ab11af04SChanwoo Choi int ret, index; 588ab11af04SChanwoo Choi unsigned long flags; 589ab11af04SChanwoo Choi 590ab11af04SChanwoo Choi index = find_cable_index_by_id(edev, id); 591ab11af04SChanwoo Choi if (index < 0) 592ab11af04SChanwoo Choi return index; 593ab11af04SChanwoo Choi 594ab11af04SChanwoo Choi /* Check whether the external connector's state is changed. */ 595ab11af04SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 5966ab6094fSChanwoo Choi ret = is_extcon_changed(edev, index, state); 597ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 598ab11af04SChanwoo Choi if (!ret) 599ab11af04SChanwoo Choi return 0; 600ab11af04SChanwoo Choi 6016ab6094fSChanwoo Choi ret = extcon_set_state(edev, id, state); 602ab11af04SChanwoo Choi if (ret < 0) 603ab11af04SChanwoo Choi return ret; 604ab11af04SChanwoo Choi 605ab11af04SChanwoo Choi return extcon_sync(edev, id); 606ab11af04SChanwoo Choi } 607ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state_sync); 608ab11af04SChanwoo Choi 609ab11af04SChanwoo Choi /** 6106ab6094fSChanwoo Choi * extcon_get_property() - Get the property value of an external connector. 6116ab6094fSChanwoo Choi * @edev: the extcon device 6126ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 6136ab6094fSChanwoo Choi * @prop: the property id indicating an extcon property 6146ab6094fSChanwoo Choi * @prop_val: the pointer which store the value of extcon property 615792e7e9eSChanwoo Choi * 6166ab6094fSChanwoo Choi * Note that when getting the property value of external connector, 6176ab6094fSChanwoo Choi * the external connector should be attached. If detached state, function 6186ab6094fSChanwoo Choi * return 0 without property value. Also, the each property should be 6196ab6094fSChanwoo Choi * included in the list of supported properties according to extcon type. 620792e7e9eSChanwoo Choi * 6216ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 622792e7e9eSChanwoo Choi */ 623792e7e9eSChanwoo Choi int extcon_get_property(struct extcon_dev *edev, unsigned int id, 624792e7e9eSChanwoo Choi unsigned int prop, 625792e7e9eSChanwoo Choi union extcon_property_value *prop_val) 626792e7e9eSChanwoo Choi { 627792e7e9eSChanwoo Choi struct extcon_cable *cable; 628792e7e9eSChanwoo Choi unsigned long flags; 629792e7e9eSChanwoo Choi int index, ret = 0; 630792e7e9eSChanwoo Choi 631792e7e9eSChanwoo Choi *prop_val = (union extcon_property_value)(0); 632792e7e9eSChanwoo Choi 633792e7e9eSChanwoo Choi if (!edev) 634792e7e9eSChanwoo Choi return -EINVAL; 635792e7e9eSChanwoo Choi 636792e7e9eSChanwoo Choi /* Check whether the property is supported or not */ 637792e7e9eSChanwoo Choi if (!is_extcon_property_supported(id, prop)) 638792e7e9eSChanwoo Choi return -EINVAL; 639792e7e9eSChanwoo Choi 640792e7e9eSChanwoo Choi /* Find the cable index of external connector by using id */ 641792e7e9eSChanwoo Choi index = find_cable_index_by_id(edev, id); 642792e7e9eSChanwoo Choi if (index < 0) 643792e7e9eSChanwoo Choi return index; 644792e7e9eSChanwoo Choi 645792e7e9eSChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 646792e7e9eSChanwoo Choi 6477f2a0a16SChanwoo Choi /* Check whether the property is available or not. */ 6487f2a0a16SChanwoo Choi if (!is_extcon_property_capability(edev, id, index, prop)) { 6497f2a0a16SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 6507f2a0a16SChanwoo Choi return -EPERM; 6517f2a0a16SChanwoo Choi } 6527f2a0a16SChanwoo Choi 653792e7e9eSChanwoo Choi /* 654792e7e9eSChanwoo Choi * Check whether the external connector is attached. 655792e7e9eSChanwoo Choi * If external connector is detached, the user can not 656792e7e9eSChanwoo Choi * get the property value. 657792e7e9eSChanwoo Choi */ 658792e7e9eSChanwoo Choi if (!is_extcon_attached(edev, index)) { 659792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 660792e7e9eSChanwoo Choi return 0; 661792e7e9eSChanwoo Choi } 662792e7e9eSChanwoo Choi 663792e7e9eSChanwoo Choi cable = &edev->cables[index]; 664792e7e9eSChanwoo Choi 665792e7e9eSChanwoo Choi /* Get the property value according to extcon type */ 666792e7e9eSChanwoo Choi switch (prop) { 667792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 668792e7e9eSChanwoo Choi *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN]; 669792e7e9eSChanwoo Choi break; 670792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 671792e7e9eSChanwoo Choi *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN]; 672792e7e9eSChanwoo Choi break; 673792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 674792e7e9eSChanwoo Choi *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN]; 675792e7e9eSChanwoo Choi break; 676792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 677792e7e9eSChanwoo Choi *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN]; 678792e7e9eSChanwoo Choi break; 679792e7e9eSChanwoo Choi default: 680792e7e9eSChanwoo Choi ret = -EINVAL; 681792e7e9eSChanwoo Choi break; 682792e7e9eSChanwoo Choi } 683792e7e9eSChanwoo Choi 684792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 685792e7e9eSChanwoo Choi 686792e7e9eSChanwoo Choi return ret; 687792e7e9eSChanwoo Choi } 688792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property); 689792e7e9eSChanwoo Choi 690792e7e9eSChanwoo Choi /** 6916ab6094fSChanwoo Choi * extcon_set_property() - Set the property value of an external connector. 6926ab6094fSChanwoo Choi * @edev: the extcon device 6936ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 6946ab6094fSChanwoo Choi * @prop: the property id indicating an extcon property 6956ab6094fSChanwoo Choi * @prop_val: the pointer including the new value of extcon property 696792e7e9eSChanwoo Choi * 6976ab6094fSChanwoo Choi * Note that each property should be included in the list of supported 6986ab6094fSChanwoo Choi * properties according to the extcon type. 699792e7e9eSChanwoo Choi * 7006ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 701792e7e9eSChanwoo Choi */ 702792e7e9eSChanwoo Choi int extcon_set_property(struct extcon_dev *edev, unsigned int id, 703792e7e9eSChanwoo Choi unsigned int prop, 704792e7e9eSChanwoo Choi union extcon_property_value prop_val) 705792e7e9eSChanwoo Choi { 706792e7e9eSChanwoo Choi struct extcon_cable *cable; 707792e7e9eSChanwoo Choi unsigned long flags; 708792e7e9eSChanwoo Choi int index, ret = 0; 709792e7e9eSChanwoo Choi 710792e7e9eSChanwoo Choi if (!edev) 711792e7e9eSChanwoo Choi return -EINVAL; 712792e7e9eSChanwoo Choi 713792e7e9eSChanwoo Choi /* Check whether the property is supported or not */ 714792e7e9eSChanwoo Choi if (!is_extcon_property_supported(id, prop)) 715792e7e9eSChanwoo Choi return -EINVAL; 716792e7e9eSChanwoo Choi 717792e7e9eSChanwoo Choi /* Find the cable index of external connector by using id */ 718792e7e9eSChanwoo Choi index = find_cable_index_by_id(edev, id); 719792e7e9eSChanwoo Choi if (index < 0) 720792e7e9eSChanwoo Choi return index; 721792e7e9eSChanwoo Choi 722792e7e9eSChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 723792e7e9eSChanwoo Choi 7247f2a0a16SChanwoo Choi /* Check whether the property is available or not. */ 7257f2a0a16SChanwoo Choi if (!is_extcon_property_capability(edev, id, index, prop)) { 7267f2a0a16SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 7277f2a0a16SChanwoo Choi return -EPERM; 7287f2a0a16SChanwoo Choi } 7297f2a0a16SChanwoo Choi 730792e7e9eSChanwoo Choi cable = &edev->cables[index]; 731792e7e9eSChanwoo Choi 732792e7e9eSChanwoo Choi /* Set the property value according to extcon type */ 733792e7e9eSChanwoo Choi switch (prop) { 734792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 735792e7e9eSChanwoo Choi cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val; 736792e7e9eSChanwoo Choi break; 737792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 738792e7e9eSChanwoo Choi cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val; 739792e7e9eSChanwoo Choi break; 740792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 741792e7e9eSChanwoo Choi cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val; 742792e7e9eSChanwoo Choi break; 743792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 744792e7e9eSChanwoo Choi cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val; 745792e7e9eSChanwoo Choi break; 746792e7e9eSChanwoo Choi default: 747792e7e9eSChanwoo Choi ret = -EINVAL; 748792e7e9eSChanwoo Choi break; 749792e7e9eSChanwoo Choi } 750792e7e9eSChanwoo Choi 751792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 752792e7e9eSChanwoo Choi 753792e7e9eSChanwoo Choi return ret; 754792e7e9eSChanwoo Choi } 755792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property); 756792e7e9eSChanwoo Choi 757792e7e9eSChanwoo Choi /** 7586ab6094fSChanwoo Choi * extcon_set_property_sync() - Set property of an external connector with sync. 7596ab6094fSChanwoo Choi * @prop_val: the pointer including the new value of extcon property 760ab11af04SChanwoo Choi * 7616ab6094fSChanwoo Choi * Note that when setting the property value of external connector, 7626ab6094fSChanwoo Choi * the external connector should be attached. The each property should 7636ab6094fSChanwoo Choi * be included in the list of supported properties according to extcon type. 764ab11af04SChanwoo Choi * 7656ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 766ab11af04SChanwoo Choi */ 767ab11af04SChanwoo Choi int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id, 768ab11af04SChanwoo Choi unsigned int prop, 769ab11af04SChanwoo Choi union extcon_property_value prop_val) 770ab11af04SChanwoo Choi { 771ab11af04SChanwoo Choi int ret; 772ab11af04SChanwoo Choi 773ab11af04SChanwoo Choi ret = extcon_set_property(edev, id, prop, prop_val); 774ab11af04SChanwoo Choi if (ret < 0) 775ab11af04SChanwoo Choi return ret; 776ab11af04SChanwoo Choi 777ab11af04SChanwoo Choi return extcon_sync(edev, id); 778ab11af04SChanwoo Choi } 779ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_sync); 780ab11af04SChanwoo Choi 781ab11af04SChanwoo Choi /** 7826ab6094fSChanwoo Choi * extcon_get_property_capability() - Get the capability of the property 7836ab6094fSChanwoo Choi * for an external connector. 7846ab6094fSChanwoo Choi * @edev: the extcon device 7856ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 7866ab6094fSChanwoo Choi * @prop: the property id indicating an extcon property 7877f2a0a16SChanwoo Choi * 7887f2a0a16SChanwoo Choi * Returns 1 if the property is available or 0 if not available. 7897f2a0a16SChanwoo Choi */ 7907f2a0a16SChanwoo Choi int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id, 7917f2a0a16SChanwoo Choi unsigned int prop) 7927f2a0a16SChanwoo Choi { 7937f2a0a16SChanwoo Choi int index; 7947f2a0a16SChanwoo Choi 7957f2a0a16SChanwoo Choi if (!edev) 7967f2a0a16SChanwoo Choi return -EINVAL; 7977f2a0a16SChanwoo Choi 7987f2a0a16SChanwoo Choi /* Check whether the property is supported or not */ 7997f2a0a16SChanwoo Choi if (!is_extcon_property_supported(id, prop)) 8007f2a0a16SChanwoo Choi return -EINVAL; 8017f2a0a16SChanwoo Choi 8027f2a0a16SChanwoo Choi /* Find the cable index of external connector by using id */ 8037f2a0a16SChanwoo Choi index = find_cable_index_by_id(edev, id); 8047f2a0a16SChanwoo Choi if (index < 0) 8057f2a0a16SChanwoo Choi return index; 8067f2a0a16SChanwoo Choi 8077f2a0a16SChanwoo Choi return is_extcon_property_capability(edev, id, index, prop); 8087f2a0a16SChanwoo Choi } 8097f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property_capability); 8107f2a0a16SChanwoo Choi 8117f2a0a16SChanwoo Choi /** 8126ab6094fSChanwoo Choi * extcon_set_property_capability() - Set the capability of the property 8136ab6094fSChanwoo Choi * for an external connector. 8146ab6094fSChanwoo Choi * @edev: the extcon device 8156ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 8166ab6094fSChanwoo Choi * @prop: the property id indicating an extcon property 8177f2a0a16SChanwoo Choi * 8186ab6094fSChanwoo Choi * Note that this function set the capability of the property 8196ab6094fSChanwoo Choi * for an external connector in order to mark the bit in capability 8206ab6094fSChanwoo Choi * bitmap which mean the available state of the property. 8217f2a0a16SChanwoo Choi * 8226ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 8237f2a0a16SChanwoo Choi */ 8247f2a0a16SChanwoo Choi int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id, 8257f2a0a16SChanwoo Choi unsigned int prop) 8267f2a0a16SChanwoo Choi { 8277f2a0a16SChanwoo Choi struct extcon_cable *cable; 8287f2a0a16SChanwoo Choi int index, type, ret = 0; 8297f2a0a16SChanwoo Choi 8307f2a0a16SChanwoo Choi if (!edev) 8317f2a0a16SChanwoo Choi return -EINVAL; 8327f2a0a16SChanwoo Choi 8337f2a0a16SChanwoo Choi /* Check whether the property is supported or not. */ 8347f2a0a16SChanwoo Choi if (!is_extcon_property_supported(id, prop)) 8357f2a0a16SChanwoo Choi return -EINVAL; 8367f2a0a16SChanwoo Choi 8377f2a0a16SChanwoo Choi /* Find the cable index of external connector by using id. */ 8387f2a0a16SChanwoo Choi index = find_cable_index_by_id(edev, id); 8397f2a0a16SChanwoo Choi if (index < 0) 8407f2a0a16SChanwoo Choi return index; 8417f2a0a16SChanwoo Choi 8427f2a0a16SChanwoo Choi type = get_extcon_type(prop); 8437f2a0a16SChanwoo Choi if (type < 0) 8447f2a0a16SChanwoo Choi return type; 8457f2a0a16SChanwoo Choi 8467f2a0a16SChanwoo Choi cable = &edev->cables[index]; 8477f2a0a16SChanwoo Choi 8487f2a0a16SChanwoo Choi switch (type) { 8497f2a0a16SChanwoo Choi case EXTCON_TYPE_USB: 8507f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); 8517f2a0a16SChanwoo Choi break; 8527f2a0a16SChanwoo Choi case EXTCON_TYPE_CHG: 8537f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); 8547f2a0a16SChanwoo Choi break; 8557f2a0a16SChanwoo Choi case EXTCON_TYPE_JACK: 8567f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); 8577f2a0a16SChanwoo Choi break; 8587f2a0a16SChanwoo Choi case EXTCON_TYPE_DISP: 8597f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); 8607f2a0a16SChanwoo Choi break; 8617f2a0a16SChanwoo Choi default: 8627f2a0a16SChanwoo Choi ret = -EINVAL; 8637f2a0a16SChanwoo Choi } 8647f2a0a16SChanwoo Choi 8657f2a0a16SChanwoo Choi return ret; 8667f2a0a16SChanwoo Choi } 8677f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_capability); 8687f2a0a16SChanwoo Choi 8697f2a0a16SChanwoo Choi /** 8706ab6094fSChanwoo Choi * extcon_get_extcon_dev() - Get the extcon device instance from the name. 8716ab6094fSChanwoo Choi * @extcon_name: the extcon name provided with extcon_dev_register() 8726ab6094fSChanwoo Choi * 8736ab6094fSChanwoo Choi * Return the pointer of extcon device if success or ERR_PTR(err) if fail. 874f68a8342SChanwoo Choi */ 875f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) 876f68a8342SChanwoo Choi { 877f68a8342SChanwoo Choi struct extcon_dev *sd; 878f68a8342SChanwoo Choi 8797eae43aeSChanwoo Choi if (!extcon_name) 8807eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 8817eae43aeSChanwoo Choi 882f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 883f68a8342SChanwoo Choi list_for_each_entry(sd, &extcon_dev_list, entry) { 884f68a8342SChanwoo Choi if (!strcmp(sd->name, extcon_name)) 885f68a8342SChanwoo Choi goto out; 886f68a8342SChanwoo Choi } 887f68a8342SChanwoo Choi sd = NULL; 888f68a8342SChanwoo Choi out: 889f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 890f68a8342SChanwoo Choi return sd; 891f68a8342SChanwoo Choi } 892f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); 893f68a8342SChanwoo Choi 894f68a8342SChanwoo Choi /** 8956ab6094fSChanwoo Choi * extcon_register_notifier() - Register a notifier block to get notified by 8966ab6094fSChanwoo Choi * any state changes from the extcon. 8976ab6094fSChanwoo Choi * @edev: the extcon device 8986ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 8996ab6094fSChanwoo Choi * @nb: a notifier block to be registered 900f68a8342SChanwoo Choi * 901f68a8342SChanwoo Choi * Note that the second parameter given to the callback of nb (val) is 9026ab6094fSChanwoo Choi * the current state of an external connector and the third pameter 9036ab6094fSChanwoo Choi * is the pointer of extcon device. 9046ab6094fSChanwoo Choi * 9056ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 906f68a8342SChanwoo Choi */ 90773b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, 908f68a8342SChanwoo Choi struct notifier_block *nb) 909f68a8342SChanwoo Choi { 91066bee35fSHans de Goede unsigned long flags; 9112c8116a1SManinder Singh int ret, idx = -EINVAL; 912046050f6SChanwoo Choi 91301b4c9a1SChanwoo Choi if (!edev || !nb) 9147eae43aeSChanwoo Choi return -EINVAL; 9157eae43aeSChanwoo Choi 916046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 917a05f44c8SStephen Boyd if (idx < 0) 918a05f44c8SStephen Boyd return idx; 91966bee35fSHans de Goede 92066bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 921046050f6SChanwoo Choi ret = raw_notifier_chain_register(&edev->nh[idx], nb); 92266bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 92366bee35fSHans de Goede 92466bee35fSHans de Goede return ret; 925f68a8342SChanwoo Choi } 926f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier); 927f68a8342SChanwoo Choi 928f68a8342SChanwoo Choi /** 9296ab6094fSChanwoo Choi * extcon_unregister_notifier() - Unregister a notifier block from the extcon. 9306ab6094fSChanwoo Choi * @edev: the extcon device 9316ab6094fSChanwoo Choi * @id: the unique id indicating an external connector 9326ab6094fSChanwoo Choi * @nb: a notifier block to be registered 9336ab6094fSChanwoo Choi * 9346ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 935f68a8342SChanwoo Choi */ 93673b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, 937f68a8342SChanwoo Choi struct notifier_block *nb) 938f68a8342SChanwoo Choi { 93966bee35fSHans de Goede unsigned long flags; 940046050f6SChanwoo Choi int ret, idx; 941046050f6SChanwoo Choi 9427eae43aeSChanwoo Choi if (!edev || !nb) 9437eae43aeSChanwoo Choi return -EINVAL; 9447eae43aeSChanwoo Choi 945046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 946a05f44c8SStephen Boyd if (idx < 0) 947a05f44c8SStephen Boyd return idx; 94866bee35fSHans de Goede 94966bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 950046050f6SChanwoo Choi ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); 95166bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 95266bee35fSHans de Goede 95366bee35fSHans de Goede return ret; 954f68a8342SChanwoo Choi } 955f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier); 956f68a8342SChanwoo Choi 957815429b3SChanwoo Choi /** 9586ab6094fSChanwoo Choi * extcon_register_notifier_all() - Register a notifier block for all connectors. 9596ab6094fSChanwoo Choi * @edev: the extcon device 9606ab6094fSChanwoo Choi * @nb: a notifier block to be registered 961815429b3SChanwoo Choi * 9626ab6094fSChanwoo Choi * Note that this function registers a notifier block in order to receive 9636ab6094fSChanwoo Choi * the state change of all supported external connectors from extcon device. 964826a47e9SMarkus Elfring * And the second parameter given to the callback of nb (val) is 9656ab6094fSChanwoo Choi * the current state and the third pameter is the pointer of extcon device. 966815429b3SChanwoo Choi * 9676ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 968815429b3SChanwoo Choi */ 969815429b3SChanwoo Choi int extcon_register_notifier_all(struct extcon_dev *edev, 970815429b3SChanwoo Choi struct notifier_block *nb) 971815429b3SChanwoo Choi { 972815429b3SChanwoo Choi unsigned long flags; 973815429b3SChanwoo Choi int ret; 974815429b3SChanwoo Choi 975815429b3SChanwoo Choi if (!edev || !nb) 976815429b3SChanwoo Choi return -EINVAL; 977815429b3SChanwoo Choi 978815429b3SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 979815429b3SChanwoo Choi ret = raw_notifier_chain_register(&edev->nh_all, nb); 980815429b3SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 981815429b3SChanwoo Choi 982815429b3SChanwoo Choi return ret; 983815429b3SChanwoo Choi } 984815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier_all); 985815429b3SChanwoo Choi 986815429b3SChanwoo Choi /** 987815429b3SChanwoo Choi * extcon_unregister_notifier_all() - Unregister a notifier block from extcon. 9886ab6094fSChanwoo Choi * @edev: the extcon device 9896ab6094fSChanwoo Choi * @nb: a notifier block to be registered 990815429b3SChanwoo Choi * 9916ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 992815429b3SChanwoo Choi */ 993815429b3SChanwoo Choi int extcon_unregister_notifier_all(struct extcon_dev *edev, 994815429b3SChanwoo Choi struct notifier_block *nb) 995815429b3SChanwoo Choi { 996815429b3SChanwoo Choi unsigned long flags; 997815429b3SChanwoo Choi int ret; 998815429b3SChanwoo Choi 999815429b3SChanwoo Choi if (!edev || !nb) 1000815429b3SChanwoo Choi return -EINVAL; 1001815429b3SChanwoo Choi 1002815429b3SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 1003815429b3SChanwoo Choi ret = raw_notifier_chain_unregister(&edev->nh_all, nb); 1004815429b3SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 1005815429b3SChanwoo Choi 1006815429b3SChanwoo Choi return ret; 1007815429b3SChanwoo Choi } 1008815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all); 1009815429b3SChanwoo Choi 1010f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = { 1011f68a8342SChanwoo Choi &dev_attr_state.attr, 1012f68a8342SChanwoo Choi &dev_attr_name.attr, 1013f68a8342SChanwoo Choi NULL, 1014f68a8342SChanwoo Choi }; 1015f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon); 1016f68a8342SChanwoo Choi 1017f68a8342SChanwoo Choi static int create_extcon_class(void) 1018f68a8342SChanwoo Choi { 1019f68a8342SChanwoo Choi if (!extcon_class) { 1020f68a8342SChanwoo Choi extcon_class = class_create(THIS_MODULE, "extcon"); 1021f68a8342SChanwoo Choi if (IS_ERR(extcon_class)) 1022f68a8342SChanwoo Choi return PTR_ERR(extcon_class); 1023f68a8342SChanwoo Choi extcon_class->dev_groups = extcon_groups; 1024f68a8342SChanwoo Choi } 1025f68a8342SChanwoo Choi 1026f68a8342SChanwoo Choi return 0; 1027f68a8342SChanwoo Choi } 1028f68a8342SChanwoo Choi 1029f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev) 1030f68a8342SChanwoo Choi { 1031f68a8342SChanwoo Choi } 1032f68a8342SChanwoo Choi 1033f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive"; 1034f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev) 1035f68a8342SChanwoo Choi { 1036f68a8342SChanwoo Choi } 1037f68a8342SChanwoo Choi 1038f68a8342SChanwoo Choi /* 1039f68a8342SChanwoo Choi * extcon_dev_allocate() - Allocate the memory of extcon device. 10406ab6094fSChanwoo Choi * @supported_cable: the array of the supported external connectors 10416ab6094fSChanwoo Choi * ending with EXTCON_NONE. 1042f68a8342SChanwoo Choi * 10436ab6094fSChanwoo Choi * Note that this function allocates the memory for extcon device 10446ab6094fSChanwoo Choi * and initialize default setting for the extcon device. 1045f68a8342SChanwoo Choi * 10466ab6094fSChanwoo Choi * Returns the pointer memory of allocated extcon_dev if success 10476ab6094fSChanwoo Choi * or ERR_PTR(err) if fail. 1048f68a8342SChanwoo Choi */ 104973b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) 1050f68a8342SChanwoo Choi { 1051f68a8342SChanwoo Choi struct extcon_dev *edev; 1052f68a8342SChanwoo Choi 10537eae43aeSChanwoo Choi if (!supported_cable) 10547eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 10557eae43aeSChanwoo Choi 1056f68a8342SChanwoo Choi edev = kzalloc(sizeof(*edev), GFP_KERNEL); 1057f68a8342SChanwoo Choi if (!edev) 1058f68a8342SChanwoo Choi return ERR_PTR(-ENOMEM); 1059f68a8342SChanwoo Choi 1060f68a8342SChanwoo Choi edev->max_supported = 0; 1061f68a8342SChanwoo Choi edev->supported_cable = supported_cable; 1062f68a8342SChanwoo Choi 1063f68a8342SChanwoo Choi return edev; 1064f68a8342SChanwoo Choi } 1065f68a8342SChanwoo Choi 1066f68a8342SChanwoo Choi /* 1067f68a8342SChanwoo Choi * extcon_dev_free() - Free the memory of extcon device. 10686ab6094fSChanwoo Choi * @edev: the extcon device 1069f68a8342SChanwoo Choi */ 1070f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev) 1071f68a8342SChanwoo Choi { 1072f68a8342SChanwoo Choi kfree(edev); 1073f68a8342SChanwoo Choi } 1074f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free); 1075f68a8342SChanwoo Choi 1076f68a8342SChanwoo Choi /** 10776ab6094fSChanwoo Choi * extcon_dev_register() - Register an new extcon device 10786ab6094fSChanwoo Choi * @edev: the extcon device to be registered 1079f68a8342SChanwoo Choi * 1080f68a8342SChanwoo Choi * Among the members of edev struct, please set the "user initializing data" 1081f68a8342SChanwoo Choi * do not set the values of "internal data", which are initialized by 1082f68a8342SChanwoo Choi * this function. 10836ab6094fSChanwoo Choi * 10846ab6094fSChanwoo Choi * Note that before calling this funciton, have to allocate the memory 10856ab6094fSChanwoo Choi * of an extcon device by using the extcon_dev_allocate(). And the extcon 10866ab6094fSChanwoo Choi * dev should include the supported_cable information. 10876ab6094fSChanwoo Choi * 10886ab6094fSChanwoo Choi * Returns 0 if success or error number if fail. 1089f68a8342SChanwoo Choi */ 1090f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev) 1091f68a8342SChanwoo Choi { 1092f68a8342SChanwoo Choi int ret, index = 0; 109371c3ffa5SChanwoo Choi static atomic_t edev_no = ATOMIC_INIT(-1); 1094f68a8342SChanwoo Choi 1095f68a8342SChanwoo Choi if (!extcon_class) { 1096f68a8342SChanwoo Choi ret = create_extcon_class(); 1097f68a8342SChanwoo Choi if (ret < 0) 1098f68a8342SChanwoo Choi return ret; 1099f68a8342SChanwoo Choi } 1100f68a8342SChanwoo Choi 11017eae43aeSChanwoo Choi if (!edev || !edev->supported_cable) 11022a9de9c0SChanwoo Choi return -EINVAL; 1103f68a8342SChanwoo Choi 11042a9de9c0SChanwoo Choi for (; edev->supported_cable[index] != EXTCON_NONE; index++); 11052a9de9c0SChanwoo Choi 11062a9de9c0SChanwoo Choi edev->max_supported = index; 1107f68a8342SChanwoo Choi if (index > SUPPORTED_CABLE_MAX) { 11082a9de9c0SChanwoo Choi dev_err(&edev->dev, 11092a9de9c0SChanwoo Choi "exceed the maximum number of supported cables\n"); 1110f68a8342SChanwoo Choi return -EINVAL; 1111f68a8342SChanwoo Choi } 1112f68a8342SChanwoo Choi 1113f68a8342SChanwoo Choi edev->dev.class = extcon_class; 1114f68a8342SChanwoo Choi edev->dev.release = extcon_dev_release; 1115f68a8342SChanwoo Choi 111671c3ffa5SChanwoo Choi edev->name = dev_name(edev->dev.parent); 1117f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(edev->name)) { 1118f68a8342SChanwoo Choi dev_err(&edev->dev, 1119f68a8342SChanwoo Choi "extcon device name is null\n"); 1120f68a8342SChanwoo Choi return -EINVAL; 1121f68a8342SChanwoo Choi } 112271c3ffa5SChanwoo Choi dev_set_name(&edev->dev, "extcon%lu", 112371c3ffa5SChanwoo Choi (unsigned long)atomic_inc_return(&edev_no)); 1124f68a8342SChanwoo Choi 1125f68a8342SChanwoo Choi if (edev->max_supported) { 1126f68a8342SChanwoo Choi char buf[10]; 1127f68a8342SChanwoo Choi char *str; 1128f68a8342SChanwoo Choi struct extcon_cable *cable; 1129f68a8342SChanwoo Choi 11306396bb22SKees Cook edev->cables = kcalloc(edev->max_supported, 11316396bb22SKees Cook sizeof(struct extcon_cable), 11326396bb22SKees Cook GFP_KERNEL); 1133f68a8342SChanwoo Choi if (!edev->cables) { 1134f68a8342SChanwoo Choi ret = -ENOMEM; 1135f68a8342SChanwoo Choi goto err_sysfs_alloc; 1136f68a8342SChanwoo Choi } 1137f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) { 1138f68a8342SChanwoo Choi cable = &edev->cables[index]; 1139f68a8342SChanwoo Choi 1140f68a8342SChanwoo Choi snprintf(buf, 10, "cable.%d", index); 11416396bb22SKees Cook str = kzalloc(strlen(buf) + 1, 1142f68a8342SChanwoo Choi GFP_KERNEL); 1143f68a8342SChanwoo Choi if (!str) { 1144f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 1145f68a8342SChanwoo Choi cable = &edev->cables[index]; 1146f68a8342SChanwoo Choi kfree(cable->attr_g.name); 1147f68a8342SChanwoo Choi } 1148f68a8342SChanwoo Choi ret = -ENOMEM; 1149f68a8342SChanwoo Choi 1150f68a8342SChanwoo Choi goto err_alloc_cables; 1151f68a8342SChanwoo Choi } 1152f68a8342SChanwoo Choi strcpy(str, buf); 1153f68a8342SChanwoo Choi 1154f68a8342SChanwoo Choi cable->edev = edev; 1155f68a8342SChanwoo Choi cable->cable_index = index; 1156f68a8342SChanwoo Choi cable->attrs[0] = &cable->attr_name.attr; 1157f68a8342SChanwoo Choi cable->attrs[1] = &cable->attr_state.attr; 1158f68a8342SChanwoo Choi cable->attrs[2] = NULL; 1159f68a8342SChanwoo Choi cable->attr_g.name = str; 1160f68a8342SChanwoo Choi cable->attr_g.attrs = cable->attrs; 1161f68a8342SChanwoo Choi 1162f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_name.attr); 1163f68a8342SChanwoo Choi cable->attr_name.attr.name = "name"; 1164f68a8342SChanwoo Choi cable->attr_name.attr.mode = 0444; 1165f68a8342SChanwoo Choi cable->attr_name.show = cable_name_show; 1166f68a8342SChanwoo Choi 1167f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_state.attr); 1168f68a8342SChanwoo Choi cable->attr_state.attr.name = "state"; 1169f68a8342SChanwoo Choi cable->attr_state.attr.mode = 0444; 1170f68a8342SChanwoo Choi cable->attr_state.show = cable_state_show; 1171f68a8342SChanwoo Choi } 1172f68a8342SChanwoo Choi } 1173f68a8342SChanwoo Choi 1174f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 1175f68a8342SChanwoo Choi char buf[80]; 1176f68a8342SChanwoo Choi char *name; 1177f68a8342SChanwoo Choi 1178f68a8342SChanwoo Choi /* Count the size of mutually_exclusive array */ 1179f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 1180f68a8342SChanwoo Choi ; 1181f68a8342SChanwoo Choi 11826396bb22SKees Cook edev->attrs_muex = kcalloc(index + 1, 11836396bb22SKees Cook sizeof(struct attribute *), 11846396bb22SKees Cook GFP_KERNEL); 1185f68a8342SChanwoo Choi if (!edev->attrs_muex) { 1186f68a8342SChanwoo Choi ret = -ENOMEM; 1187f68a8342SChanwoo Choi goto err_muex; 1188f68a8342SChanwoo Choi } 1189f68a8342SChanwoo Choi 11906396bb22SKees Cook edev->d_attrs_muex = kcalloc(index, 11916396bb22SKees Cook sizeof(struct device_attribute), 11926396bb22SKees Cook GFP_KERNEL); 1193f68a8342SChanwoo Choi if (!edev->d_attrs_muex) { 1194f68a8342SChanwoo Choi ret = -ENOMEM; 1195f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1196f68a8342SChanwoo Choi goto err_muex; 1197f68a8342SChanwoo Choi } 1198f68a8342SChanwoo Choi 1199f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) { 1200f68a8342SChanwoo Choi sprintf(buf, "0x%x", edev->mutually_exclusive[index]); 12016396bb22SKees Cook name = kzalloc(strlen(buf) + 1, 1202f68a8342SChanwoo Choi GFP_KERNEL); 1203f68a8342SChanwoo Choi if (!name) { 1204f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 1205f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr. 1206f68a8342SChanwoo Choi name); 1207f68a8342SChanwoo Choi } 1208f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 1209f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1210f68a8342SChanwoo Choi ret = -ENOMEM; 1211f68a8342SChanwoo Choi goto err_muex; 1212f68a8342SChanwoo Choi } 1213f68a8342SChanwoo Choi strcpy(name, buf); 1214f68a8342SChanwoo Choi sysfs_attr_init(&edev->d_attrs_muex[index].attr); 1215f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.name = name; 1216f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.mode = 0000; 1217f68a8342SChanwoo Choi edev->attrs_muex[index] = &edev->d_attrs_muex[index] 1218f68a8342SChanwoo Choi .attr; 1219f68a8342SChanwoo Choi } 1220f68a8342SChanwoo Choi edev->attr_g_muex.name = muex_name; 1221f68a8342SChanwoo Choi edev->attr_g_muex.attrs = edev->attrs_muex; 1222f68a8342SChanwoo Choi 1223f68a8342SChanwoo Choi } 1224f68a8342SChanwoo Choi 1225f68a8342SChanwoo Choi if (edev->max_supported) { 1226f68a8342SChanwoo Choi edev->extcon_dev_type.groups = 12276396bb22SKees Cook kcalloc(edev->max_supported + 2, 12286396bb22SKees Cook sizeof(struct attribute_group *), 12296396bb22SKees Cook GFP_KERNEL); 1230f68a8342SChanwoo Choi if (!edev->extcon_dev_type.groups) { 1231f68a8342SChanwoo Choi ret = -ENOMEM; 1232f68a8342SChanwoo Choi goto err_alloc_groups; 1233f68a8342SChanwoo Choi } 1234f68a8342SChanwoo Choi 1235f68a8342SChanwoo Choi edev->extcon_dev_type.name = dev_name(&edev->dev); 1236f68a8342SChanwoo Choi edev->extcon_dev_type.release = dummy_sysfs_dev_release; 1237f68a8342SChanwoo Choi 1238f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1239f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 1240f68a8342SChanwoo Choi &edev->cables[index].attr_g; 1241f68a8342SChanwoo Choi if (edev->mutually_exclusive) 1242f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 1243f68a8342SChanwoo Choi &edev->attr_g_muex; 1244f68a8342SChanwoo Choi 1245f68a8342SChanwoo Choi edev->dev.type = &edev->extcon_dev_type; 1246f68a8342SChanwoo Choi } 1247f68a8342SChanwoo Choi 1248f68a8342SChanwoo Choi ret = device_register(&edev->dev); 1249f68a8342SChanwoo Choi if (ret) { 1250f68a8342SChanwoo Choi put_device(&edev->dev); 1251f68a8342SChanwoo Choi goto err_dev; 1252f68a8342SChanwoo Choi } 1253f68a8342SChanwoo Choi 1254f68a8342SChanwoo Choi spin_lock_init(&edev->lock); 12553f5071a8SMarkus Elfring edev->nh = devm_kcalloc(&edev->dev, edev->max_supported, 12563f5071a8SMarkus Elfring sizeof(*edev->nh), GFP_KERNEL); 1257046050f6SChanwoo Choi if (!edev->nh) { 1258046050f6SChanwoo Choi ret = -ENOMEM; 1259046050f6SChanwoo Choi goto err_dev; 1260046050f6SChanwoo Choi } 1261046050f6SChanwoo Choi 1262046050f6SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1263046050f6SChanwoo Choi RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); 1264f68a8342SChanwoo Choi 1265815429b3SChanwoo Choi RAW_INIT_NOTIFIER_HEAD(&edev->nh_all); 1266815429b3SChanwoo Choi 1267f68a8342SChanwoo Choi dev_set_drvdata(&edev->dev, edev); 1268f68a8342SChanwoo Choi edev->state = 0; 1269f68a8342SChanwoo Choi 1270f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 1271f68a8342SChanwoo Choi list_add(&edev->entry, &extcon_dev_list); 1272f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 1273f68a8342SChanwoo Choi 1274f68a8342SChanwoo Choi return 0; 1275f68a8342SChanwoo Choi 1276f68a8342SChanwoo Choi err_dev: 1277f68a8342SChanwoo Choi if (edev->max_supported) 1278f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 1279f68a8342SChanwoo Choi err_alloc_groups: 1280f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 1281f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 1282f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 1283f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 1284f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1285f68a8342SChanwoo Choi } 1286f68a8342SChanwoo Choi err_muex: 1287f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1288f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 1289f68a8342SChanwoo Choi err_alloc_cables: 1290f68a8342SChanwoo Choi if (edev->max_supported) 1291f68a8342SChanwoo Choi kfree(edev->cables); 1292f68a8342SChanwoo Choi err_sysfs_alloc: 1293f68a8342SChanwoo Choi return ret; 1294f68a8342SChanwoo Choi } 1295f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register); 1296f68a8342SChanwoo Choi 1297f68a8342SChanwoo Choi /** 1298f68a8342SChanwoo Choi * extcon_dev_unregister() - Unregister the extcon device. 12996ab6094fSChanwoo Choi * @edev: the extcon device to be unregistered. 1300f68a8342SChanwoo Choi * 1301f68a8342SChanwoo Choi * Note that this does not call kfree(edev) because edev was not allocated 1302f68a8342SChanwoo Choi * by this class. 1303f68a8342SChanwoo Choi */ 1304f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev) 1305f68a8342SChanwoo Choi { 1306f68a8342SChanwoo Choi int index; 1307f68a8342SChanwoo Choi 13087eae43aeSChanwoo Choi if (!edev) 13097eae43aeSChanwoo Choi return; 13107eae43aeSChanwoo Choi 1311f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 1312f68a8342SChanwoo Choi list_del(&edev->entry); 1313f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 1314f68a8342SChanwoo Choi 1315f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(get_device(&edev->dev))) { 1316f68a8342SChanwoo Choi dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", 1317f68a8342SChanwoo Choi dev_name(&edev->dev)); 1318f68a8342SChanwoo Choi return; 1319f68a8342SChanwoo Choi } 1320f68a8342SChanwoo Choi 1321f68a8342SChanwoo Choi device_unregister(&edev->dev); 1322f68a8342SChanwoo Choi 1323f68a8342SChanwoo Choi if (edev->mutually_exclusive && edev->max_supported) { 1324f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; 1325f68a8342SChanwoo Choi index++) 1326f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 1327f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 1328f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1329f68a8342SChanwoo Choi } 1330f68a8342SChanwoo Choi 1331f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 1332f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 1333f68a8342SChanwoo Choi 1334f68a8342SChanwoo Choi if (edev->max_supported) { 1335f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 1336f68a8342SChanwoo Choi kfree(edev->cables); 1337f68a8342SChanwoo Choi } 1338f68a8342SChanwoo Choi 1339f68a8342SChanwoo Choi put_device(&edev->dev); 1340f68a8342SChanwoo Choi } 1341f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister); 1342f68a8342SChanwoo Choi 1343f68a8342SChanwoo Choi #ifdef CONFIG_OF 1344370ed7a9SAndrzej Hajda 1345370ed7a9SAndrzej Hajda /* 1346370ed7a9SAndrzej Hajda * extcon_find_edev_by_node - Find the extcon device from devicetree. 1347370ed7a9SAndrzej Hajda * @node : OF node identifying edev 1348370ed7a9SAndrzej Hajda * 1349370ed7a9SAndrzej Hajda * Return the pointer of extcon device if success or ERR_PTR(err) if fail. 1350370ed7a9SAndrzej Hajda */ 1351370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node) 1352370ed7a9SAndrzej Hajda { 1353370ed7a9SAndrzej Hajda struct extcon_dev *edev; 1354370ed7a9SAndrzej Hajda 1355370ed7a9SAndrzej Hajda mutex_lock(&extcon_dev_list_lock); 1356370ed7a9SAndrzej Hajda list_for_each_entry(edev, &extcon_dev_list, entry) 1357370ed7a9SAndrzej Hajda if (edev->dev.parent && edev->dev.parent->of_node == node) 1358370ed7a9SAndrzej Hajda goto out; 1359370ed7a9SAndrzej Hajda edev = ERR_PTR(-EPROBE_DEFER); 1360370ed7a9SAndrzej Hajda out: 1361370ed7a9SAndrzej Hajda mutex_unlock(&extcon_dev_list_lock); 1362370ed7a9SAndrzej Hajda 1363370ed7a9SAndrzej Hajda return edev; 1364370ed7a9SAndrzej Hajda } 1365370ed7a9SAndrzej Hajda 1366f68a8342SChanwoo Choi /* 13676ab6094fSChanwoo Choi * extcon_get_edev_by_phandle - Get the extcon device from devicetree. 13686ab6094fSChanwoo Choi * @dev : the instance to the given device 13696ab6094fSChanwoo Choi * @index : the index into list of extcon_dev 1370f68a8342SChanwoo Choi * 13716ab6094fSChanwoo Choi * Return the pointer of extcon device if success or ERR_PTR(err) if fail. 1372f68a8342SChanwoo Choi */ 1373f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1374f68a8342SChanwoo Choi { 1375f68a8342SChanwoo Choi struct device_node *node; 1376f68a8342SChanwoo Choi struct extcon_dev *edev; 1377f68a8342SChanwoo Choi 13787eae43aeSChanwoo Choi if (!dev) 13797eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 13807eae43aeSChanwoo Choi 1381f68a8342SChanwoo Choi if (!dev->of_node) { 1382e8752b7aSStephen Boyd dev_dbg(dev, "device does not have a device node entry\n"); 1383f68a8342SChanwoo Choi return ERR_PTR(-EINVAL); 1384f68a8342SChanwoo Choi } 1385f68a8342SChanwoo Choi 1386f68a8342SChanwoo Choi node = of_parse_phandle(dev->of_node, "extcon", index); 1387f68a8342SChanwoo Choi if (!node) { 13885c27036dSRob Herring dev_dbg(dev, "failed to get phandle in %pOF node\n", 13895c27036dSRob Herring dev->of_node); 1390f68a8342SChanwoo Choi return ERR_PTR(-ENODEV); 1391f68a8342SChanwoo Choi } 1392f68a8342SChanwoo Choi 1393370ed7a9SAndrzej Hajda edev = extcon_find_edev_by_node(node); 13945d5c4c13SPeter Chen of_node_put(node); 1395f68a8342SChanwoo Choi 1396370ed7a9SAndrzej Hajda return edev; 1397f68a8342SChanwoo Choi } 1398370ed7a9SAndrzej Hajda 1399f68a8342SChanwoo Choi #else 1400370ed7a9SAndrzej Hajda 1401370ed7a9SAndrzej Hajda struct extcon_dev *extcon_find_edev_by_node(struct device_node *node) 1402370ed7a9SAndrzej Hajda { 1403370ed7a9SAndrzej Hajda return ERR_PTR(-ENOSYS); 1404370ed7a9SAndrzej Hajda } 1405370ed7a9SAndrzej Hajda 1406f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1407f68a8342SChanwoo Choi { 1408f68a8342SChanwoo Choi return ERR_PTR(-ENOSYS); 1409f68a8342SChanwoo Choi } 1410370ed7a9SAndrzej Hajda 1411f68a8342SChanwoo Choi #endif /* CONFIG_OF */ 1412370ed7a9SAndrzej Hajda 1413370ed7a9SAndrzej Hajda EXPORT_SYMBOL_GPL(extcon_find_edev_by_node); 1414f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); 1415f68a8342SChanwoo Choi 1416707d7550SChanwoo Choi /** 1417707d7550SChanwoo Choi * extcon_get_edev_name() - Get the name of the extcon device. 1418707d7550SChanwoo Choi * @edev: the extcon device 1419707d7550SChanwoo Choi */ 1420707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev) 1421707d7550SChanwoo Choi { 1422707d7550SChanwoo Choi return !edev ? NULL : edev->name; 1423707d7550SChanwoo Choi } 1424707d7550SChanwoo Choi 1425f68a8342SChanwoo Choi static int __init extcon_class_init(void) 1426f68a8342SChanwoo Choi { 1427f68a8342SChanwoo Choi return create_extcon_class(); 1428f68a8342SChanwoo Choi } 1429f68a8342SChanwoo Choi module_init(extcon_class_init); 1430f68a8342SChanwoo Choi 1431f68a8342SChanwoo Choi static void __exit extcon_class_exit(void) 1432f68a8342SChanwoo Choi { 1433f68a8342SChanwoo Choi class_destroy(extcon_class); 1434f68a8342SChanwoo Choi } 1435f68a8342SChanwoo Choi module_exit(extcon_class_exit); 1436f68a8342SChanwoo Choi 14372a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <[email protected]>"); 1438f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <[email protected]>"); 14396ab6094fSChanwoo Choi MODULE_DESCRIPTION("External Connector (extcon) framework"); 14406ab6094fSChanwoo Choi MODULE_LICENSE("GPL v2"); 1441