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 <cw00.choi@samsung.com> 82a9de9c0SChanwoo Choi * 9f68a8342SChanwoo Choi * Copyright (C) 2012 Samsung Electronics 10f68a8342SChanwoo Choi * Author: Donggeun Kim <dg77.kim@samsung.com> 11f68a8342SChanwoo Choi * Author: MyungJoo Ham <myungjoo.ham@samsung.com> 12f68a8342SChanwoo Choi * 13f68a8342SChanwoo Choi * based on android/drivers/switch/switch_class.c 14f68a8342SChanwoo Choi * Copyright (C) 2008 Google, Inc. 15f68a8342SChanwoo Choi * Author: Mike Lockwood <lockwood@android.com> 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/of.h> 34f68a8342SChanwoo Choi #include <linux/slab.h> 35f68a8342SChanwoo Choi #include <linux/sysfs.h> 36f68a8342SChanwoo Choi 37e6cf0465SChanwoo Choi #include "extcon.h" 38e6cf0465SChanwoo Choi 392a9de9c0SChanwoo Choi #define SUPPORTED_CABLE_MAX 32 402a9de9c0SChanwoo Choi #define CABLE_NAME_MAX 30 412a9de9c0SChanwoo Choi 4255e4e2f1SChanwoo Choi struct __extcon_info { 4355e4e2f1SChanwoo Choi unsigned int type; 4455e4e2f1SChanwoo Choi unsigned int id; 4555e4e2f1SChanwoo Choi const char *name; 4655e4e2f1SChanwoo Choi 4755e4e2f1SChanwoo Choi } extcon_info[] = { 4855e4e2f1SChanwoo Choi [EXTCON_NONE] = { 4955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 5055e4e2f1SChanwoo Choi .id = EXTCON_NONE, 5155e4e2f1SChanwoo Choi .name = "NONE", 5255e4e2f1SChanwoo Choi }, 5373b6ecdbSChanwoo Choi 548e9bc36dSChanwoo Choi /* USB external connector */ 5555e4e2f1SChanwoo Choi [EXTCON_USB] = { 5655e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 5755e4e2f1SChanwoo Choi .id = EXTCON_USB, 5855e4e2f1SChanwoo Choi .name = "USB", 5955e4e2f1SChanwoo Choi }, 6055e4e2f1SChanwoo Choi [EXTCON_USB_HOST] = { 6155e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 6255e4e2f1SChanwoo Choi .id = EXTCON_USB_HOST, 6386d6cda6SChanwoo Choi .name = "USB-HOST", 6455e4e2f1SChanwoo Choi }, 658e9bc36dSChanwoo Choi 6611eecf91SChanwoo Choi /* Charging external connector */ 6755e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SDP] = { 6855e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 6955e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SDP, 7055e4e2f1SChanwoo Choi .name = "SDP", 7155e4e2f1SChanwoo Choi }, 7255e4e2f1SChanwoo Choi [EXTCON_CHG_USB_DCP] = { 7355e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 7455e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_DCP, 7555e4e2f1SChanwoo Choi .name = "DCP", 7655e4e2f1SChanwoo Choi }, 7755e4e2f1SChanwoo Choi [EXTCON_CHG_USB_CDP] = { 7855e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 7955e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_CDP, 8055e4e2f1SChanwoo Choi .name = "CDP", 8155e4e2f1SChanwoo Choi }, 8255e4e2f1SChanwoo Choi [EXTCON_CHG_USB_ACA] = { 8355e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 8455e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_ACA, 8555e4e2f1SChanwoo Choi .name = "ACA", 8655e4e2f1SChanwoo Choi }, 8755e4e2f1SChanwoo Choi [EXTCON_CHG_USB_FAST] = { 8855e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 8955e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_FAST, 9055e4e2f1SChanwoo Choi .name = "FAST-CHARGER", 9155e4e2f1SChanwoo Choi }, 9255e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SLOW] = { 9355e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 9455e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SLOW, 9555e4e2f1SChanwoo Choi .name = "SLOW-CHARGER", 9655e4e2f1SChanwoo Choi }, 977fe95fb8SChanwoo Choi [EXTCON_CHG_WPT] = { 987fe95fb8SChanwoo Choi .type = EXTCON_TYPE_CHG, 997fe95fb8SChanwoo Choi .id = EXTCON_CHG_WPT, 1007fe95fb8SChanwoo Choi .name = "WPT", 1017fe95fb8SChanwoo Choi }, 1023c5f0e07SChanwoo Choi [EXTCON_CHG_USB_PD] = { 1033c5f0e07SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 1043c5f0e07SChanwoo Choi .id = EXTCON_CHG_USB_PD, 1053c5f0e07SChanwoo Choi .name = "PD", 1063c5f0e07SChanwoo Choi }, 1078e9bc36dSChanwoo Choi 10811eecf91SChanwoo Choi /* Jack external connector */ 10955e4e2f1SChanwoo Choi [EXTCON_JACK_MICROPHONE] = { 11055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 11155e4e2f1SChanwoo Choi .id = EXTCON_JACK_MICROPHONE, 11255e4e2f1SChanwoo Choi .name = "MICROPHONE", 11355e4e2f1SChanwoo Choi }, 11455e4e2f1SChanwoo Choi [EXTCON_JACK_HEADPHONE] = { 11555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 11655e4e2f1SChanwoo Choi .id = EXTCON_JACK_HEADPHONE, 11755e4e2f1SChanwoo Choi .name = "HEADPHONE", 11855e4e2f1SChanwoo Choi }, 11955e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_IN] = { 12055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 12155e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_IN, 12255e4e2f1SChanwoo Choi .name = "LINE-IN", 12355e4e2f1SChanwoo Choi }, 12455e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_OUT] = { 12555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 12655e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_OUT, 12755e4e2f1SChanwoo Choi .name = "LINE-OUT", 12855e4e2f1SChanwoo Choi }, 12955e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_IN] = { 13055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 13155e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_IN, 13255e4e2f1SChanwoo Choi .name = "VIDEO-IN", 13355e4e2f1SChanwoo Choi }, 13455e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_OUT] = { 13555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 13655e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_OUT, 13755e4e2f1SChanwoo Choi .name = "VIDEO-OUT", 13855e4e2f1SChanwoo Choi }, 13955e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_IN] = { 14055e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 14155e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_IN, 14255e4e2f1SChanwoo Choi .name = "SPDIF-IN", 14355e4e2f1SChanwoo Choi }, 14455e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_OUT] = { 14555e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 14655e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_OUT, 14755e4e2f1SChanwoo Choi .name = "SPDIF-OUT", 14855e4e2f1SChanwoo Choi }, 1498e9bc36dSChanwoo Choi 15011eecf91SChanwoo Choi /* Display external connector */ 15155e4e2f1SChanwoo Choi [EXTCON_DISP_HDMI] = { 15255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 15355e4e2f1SChanwoo Choi .id = EXTCON_DISP_HDMI, 15455e4e2f1SChanwoo Choi .name = "HDMI", 15555e4e2f1SChanwoo Choi }, 15655e4e2f1SChanwoo Choi [EXTCON_DISP_MHL] = { 15755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 15855e4e2f1SChanwoo Choi .id = EXTCON_DISP_MHL, 15955e4e2f1SChanwoo Choi .name = "MHL", 16055e4e2f1SChanwoo Choi }, 16155e4e2f1SChanwoo Choi [EXTCON_DISP_DVI] = { 16255e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 16355e4e2f1SChanwoo Choi .id = EXTCON_DISP_DVI, 16455e4e2f1SChanwoo Choi .name = "DVI", 16555e4e2f1SChanwoo Choi }, 16655e4e2f1SChanwoo Choi [EXTCON_DISP_VGA] = { 16755e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 16855e4e2f1SChanwoo Choi .id = EXTCON_DISP_VGA, 16955e4e2f1SChanwoo Choi .name = "VGA", 17055e4e2f1SChanwoo Choi }, 1712164188dSChris Zhong [EXTCON_DISP_DP] = { 1722164188dSChris Zhong .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB, 1732164188dSChris Zhong .id = EXTCON_DISP_DP, 1742164188dSChris Zhong .name = "DP", 1752164188dSChris Zhong }, 1769c0595d6SChanwoo Choi [EXTCON_DISP_HMD] = { 1779c0595d6SChanwoo Choi .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB, 1789c0595d6SChanwoo Choi .id = EXTCON_DISP_HMD, 1799c0595d6SChanwoo Choi .name = "HMD", 1809c0595d6SChanwoo Choi }, 1818e9bc36dSChanwoo Choi 18211eecf91SChanwoo Choi /* Miscellaneous external connector */ 18355e4e2f1SChanwoo Choi [EXTCON_DOCK] = { 18455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 18555e4e2f1SChanwoo Choi .id = EXTCON_DOCK, 18655e4e2f1SChanwoo Choi .name = "DOCK", 18755e4e2f1SChanwoo Choi }, 18855e4e2f1SChanwoo Choi [EXTCON_JIG] = { 18955e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 19055e4e2f1SChanwoo Choi .id = EXTCON_JIG, 19155e4e2f1SChanwoo Choi .name = "JIG", 19255e4e2f1SChanwoo Choi }, 19355e4e2f1SChanwoo Choi [EXTCON_MECHANICAL] = { 19455e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 19555e4e2f1SChanwoo Choi .id = EXTCON_MECHANICAL, 19655e4e2f1SChanwoo Choi .name = "MECHANICAL", 19755e4e2f1SChanwoo Choi }, 1988e9bc36dSChanwoo Choi 19955e4e2f1SChanwoo Choi { /* sentinel */ } 200f68a8342SChanwoo Choi }; 201f68a8342SChanwoo Choi 20220f7b53dSChanwoo Choi /** 20320f7b53dSChanwoo Choi * struct extcon_cable - An internal data for each cable of extcon device. 20420f7b53dSChanwoo Choi * @edev: The extcon device 20520f7b53dSChanwoo Choi * @cable_index: Index of this cable in the edev 20620f7b53dSChanwoo Choi * @attr_g: Attribute group for the cable 20720f7b53dSChanwoo Choi * @attr_name: "name" sysfs entry 20820f7b53dSChanwoo Choi * @attr_state: "state" sysfs entry 20920f7b53dSChanwoo Choi * @attrs: Array pointing to attr_name and attr_state for attr_g 21020f7b53dSChanwoo Choi */ 21120f7b53dSChanwoo Choi struct extcon_cable { 21220f7b53dSChanwoo Choi struct extcon_dev *edev; 21320f7b53dSChanwoo Choi int cable_index; 21420f7b53dSChanwoo Choi 21520f7b53dSChanwoo Choi struct attribute_group attr_g; 21620f7b53dSChanwoo Choi struct device_attribute attr_name; 21720f7b53dSChanwoo Choi struct device_attribute attr_state; 21820f7b53dSChanwoo Choi 21920f7b53dSChanwoo Choi struct attribute *attrs[3]; /* to be fed to attr_g.attrs */ 220792e7e9eSChanwoo Choi 221792e7e9eSChanwoo Choi union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT]; 222792e7e9eSChanwoo Choi union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT]; 223792e7e9eSChanwoo Choi union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT]; 224792e7e9eSChanwoo Choi union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT]; 2257f2a0a16SChanwoo Choi 2267f2a0a16SChanwoo Choi unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)]; 2277f2a0a16SChanwoo Choi unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)]; 2287f2a0a16SChanwoo Choi unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)]; 2297f2a0a16SChanwoo Choi unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)]; 23020f7b53dSChanwoo Choi }; 23120f7b53dSChanwoo Choi 232f68a8342SChanwoo Choi static struct class *extcon_class; 233f68a8342SChanwoo Choi 234f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list); 235f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock); 236f68a8342SChanwoo Choi 237f68a8342SChanwoo Choi /** 238f68a8342SChanwoo Choi * check_mutually_exclusive - Check if new_state violates mutually_exclusive 239f68a8342SChanwoo Choi * condition. 240f68a8342SChanwoo Choi * @edev: the extcon device 241f68a8342SChanwoo Choi * @new_state: new cable attach status for @edev 242f68a8342SChanwoo Choi * 243f68a8342SChanwoo Choi * Returns 0 if nothing violates. Returns the index + 1 for the first 244f68a8342SChanwoo Choi * violated condition. 245f68a8342SChanwoo Choi */ 246f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) 247f68a8342SChanwoo Choi { 248f68a8342SChanwoo Choi int i = 0; 249f68a8342SChanwoo Choi 250f68a8342SChanwoo Choi if (!edev->mutually_exclusive) 251f68a8342SChanwoo Choi return 0; 252f68a8342SChanwoo Choi 253f68a8342SChanwoo Choi for (i = 0; edev->mutually_exclusive[i]; i++) { 254f68a8342SChanwoo Choi int weight; 255f68a8342SChanwoo Choi u32 correspondants = new_state & edev->mutually_exclusive[i]; 256f68a8342SChanwoo Choi 257f68a8342SChanwoo Choi /* calculate the total number of bits set */ 258f68a8342SChanwoo Choi weight = hweight32(correspondants); 259f68a8342SChanwoo Choi if (weight > 1) 260f68a8342SChanwoo Choi return i + 1; 261f68a8342SChanwoo Choi } 262f68a8342SChanwoo Choi 263f68a8342SChanwoo Choi return 0; 264f68a8342SChanwoo Choi } 265f68a8342SChanwoo Choi 26673b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id) 2672a9de9c0SChanwoo Choi { 2682a9de9c0SChanwoo Choi int i; 2692a9de9c0SChanwoo Choi 2702a9de9c0SChanwoo Choi /* Find the the index of extcon cable in edev->supported_cable */ 2712a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 2722a9de9c0SChanwoo Choi if (edev->supported_cable[i] == id) 2732a9de9c0SChanwoo Choi return i; 2742a9de9c0SChanwoo Choi } 2752a9de9c0SChanwoo Choi 2762a9de9c0SChanwoo Choi return -EINVAL; 2772a9de9c0SChanwoo Choi } 2782a9de9c0SChanwoo Choi 279792e7e9eSChanwoo Choi static int get_extcon_type(unsigned int prop) 280792e7e9eSChanwoo Choi { 281792e7e9eSChanwoo Choi switch (prop) { 282792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 283792e7e9eSChanwoo Choi return EXTCON_TYPE_USB; 284792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 285792e7e9eSChanwoo Choi return EXTCON_TYPE_CHG; 286792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 287792e7e9eSChanwoo Choi return EXTCON_TYPE_JACK; 288792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 289792e7e9eSChanwoo Choi return EXTCON_TYPE_DISP; 290792e7e9eSChanwoo Choi default: 291792e7e9eSChanwoo Choi return -EINVAL; 292792e7e9eSChanwoo Choi } 293792e7e9eSChanwoo Choi } 294792e7e9eSChanwoo Choi 295792e7e9eSChanwoo Choi static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index) 296792e7e9eSChanwoo Choi { 297792e7e9eSChanwoo Choi return !!(edev->state & BIT(index)); 298792e7e9eSChanwoo Choi } 299792e7e9eSChanwoo Choi 300ab11af04SChanwoo Choi static bool is_extcon_changed(struct extcon_dev *edev, int index, 301ab11af04SChanwoo Choi bool new_state) 302046050f6SChanwoo Choi { 303ab11af04SChanwoo Choi int state = !!(edev->state & BIT(index)); 304ab11af04SChanwoo Choi return (state != new_state); 305046050f6SChanwoo Choi } 306046050f6SChanwoo Choi 307792e7e9eSChanwoo Choi static bool is_extcon_property_supported(unsigned int id, unsigned int prop) 308792e7e9eSChanwoo Choi { 309792e7e9eSChanwoo Choi int type; 310792e7e9eSChanwoo Choi 311792e7e9eSChanwoo Choi /* Check whether the property is supported or not. */ 312792e7e9eSChanwoo Choi type = get_extcon_type(prop); 313792e7e9eSChanwoo Choi if (type < 0) 314792e7e9eSChanwoo Choi return false; 315792e7e9eSChanwoo Choi 316792e7e9eSChanwoo Choi /* Check whether a specific extcon id supports the property or not. */ 317792e7e9eSChanwoo Choi return !!(extcon_info[id].type & type); 318792e7e9eSChanwoo Choi } 319792e7e9eSChanwoo Choi 3207f2a0a16SChanwoo Choi static int is_extcon_property_capability(struct extcon_dev *edev, 3217f2a0a16SChanwoo Choi unsigned int id, int index,unsigned int prop) 3227f2a0a16SChanwoo Choi { 3237f2a0a16SChanwoo Choi struct extcon_cable *cable; 3247f2a0a16SChanwoo Choi int type, ret; 3257f2a0a16SChanwoo Choi 3267f2a0a16SChanwoo Choi /* Check whether the property is supported or not. */ 3277f2a0a16SChanwoo Choi type = get_extcon_type(prop); 3287f2a0a16SChanwoo Choi if (type < 0) 3297f2a0a16SChanwoo Choi return type; 3307f2a0a16SChanwoo Choi 3317f2a0a16SChanwoo Choi cable = &edev->cables[index]; 3327f2a0a16SChanwoo Choi 3337f2a0a16SChanwoo Choi switch (type) { 3347f2a0a16SChanwoo Choi case EXTCON_TYPE_USB: 3357f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); 3367f2a0a16SChanwoo Choi break; 3377f2a0a16SChanwoo Choi case EXTCON_TYPE_CHG: 3387f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); 3397f2a0a16SChanwoo Choi break; 3407f2a0a16SChanwoo Choi case EXTCON_TYPE_JACK: 3417f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); 3427f2a0a16SChanwoo Choi break; 3437f2a0a16SChanwoo Choi case EXTCON_TYPE_DISP: 3447f2a0a16SChanwoo Choi ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); 3457f2a0a16SChanwoo Choi break; 3467f2a0a16SChanwoo Choi default: 3477f2a0a16SChanwoo Choi ret = -EINVAL; 3487f2a0a16SChanwoo Choi } 3497f2a0a16SChanwoo Choi 3507f2a0a16SChanwoo Choi return ret; 3517f2a0a16SChanwoo Choi } 3527f2a0a16SChanwoo Choi 353792e7e9eSChanwoo Choi static void init_property(struct extcon_dev *edev, unsigned int id, int index) 354792e7e9eSChanwoo Choi { 355792e7e9eSChanwoo Choi unsigned int type = extcon_info[id].type; 356792e7e9eSChanwoo Choi struct extcon_cable *cable = &edev->cables[index]; 357792e7e9eSChanwoo Choi 358792e7e9eSChanwoo Choi if (EXTCON_TYPE_USB & type) 359792e7e9eSChanwoo Choi memset(cable->usb_propval, 0, sizeof(cable->usb_propval)); 360792e7e9eSChanwoo Choi if (EXTCON_TYPE_CHG & type) 361792e7e9eSChanwoo Choi memset(cable->chg_propval, 0, sizeof(cable->chg_propval)); 362792e7e9eSChanwoo Choi if (EXTCON_TYPE_JACK & type) 363792e7e9eSChanwoo Choi memset(cable->jack_propval, 0, sizeof(cable->jack_propval)); 364792e7e9eSChanwoo Choi if (EXTCON_TYPE_DISP & type) 365792e7e9eSChanwoo Choi memset(cable->disp_propval, 0, sizeof(cable->disp_propval)); 366792e7e9eSChanwoo Choi } 367792e7e9eSChanwoo Choi 368f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr, 369f68a8342SChanwoo Choi char *buf) 370f68a8342SChanwoo Choi { 371f68a8342SChanwoo Choi int i, count = 0; 372f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 373f68a8342SChanwoo Choi 374f68a8342SChanwoo Choi if (edev->max_supported == 0) 375f68a8342SChanwoo Choi return sprintf(buf, "%u\n", edev->state); 376f68a8342SChanwoo Choi 3772a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 378f68a8342SChanwoo Choi count += sprintf(buf + count, "%s=%d\n", 37955e4e2f1SChanwoo Choi extcon_info[edev->supported_cable[i]].name, 38070641a0aSChanwoo Choi !!(edev->state & BIT(i))); 381f68a8342SChanwoo Choi } 382f68a8342SChanwoo Choi 383f68a8342SChanwoo Choi return count; 384f68a8342SChanwoo Choi } 3855d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state); 386f68a8342SChanwoo Choi 387f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr, 388f68a8342SChanwoo Choi char *buf) 389f68a8342SChanwoo Choi { 390f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 391f68a8342SChanwoo Choi 39271c3ffa5SChanwoo Choi return sprintf(buf, "%s\n", edev->name); 393f68a8342SChanwoo Choi } 394f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name); 395f68a8342SChanwoo Choi 396f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev, 397f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 398f68a8342SChanwoo Choi { 399f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 400f68a8342SChanwoo Choi attr_name); 4012a9de9c0SChanwoo Choi int i = cable->cable_index; 402f68a8342SChanwoo Choi 403f68a8342SChanwoo Choi return sprintf(buf, "%s\n", 40455e4e2f1SChanwoo Choi extcon_info[cable->edev->supported_cable[i]].name); 405f68a8342SChanwoo Choi } 406f68a8342SChanwoo Choi 407f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev, 408f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 409f68a8342SChanwoo Choi { 410f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 411f68a8342SChanwoo Choi attr_state); 412f68a8342SChanwoo Choi 413be052cc8SRoger Quadros int i = cable->cable_index; 414be052cc8SRoger Quadros 415f68a8342SChanwoo Choi return sprintf(buf, "%d\n", 416575c2b86SChanwoo Choi extcon_get_state(cable->edev, cable->edev->supported_cable[i])); 417f68a8342SChanwoo Choi } 418f68a8342SChanwoo Choi 419f68a8342SChanwoo Choi /** 420ab11af04SChanwoo Choi * extcon_sync() - Synchronize the states for both the attached/detached 421ab11af04SChanwoo Choi * @edev: the extcon device that has the cable. 422f68a8342SChanwoo Choi * 423ab11af04SChanwoo Choi * This function send a notification to synchronize the all states of a 424ab11af04SChanwoo Choi * specific external connector 425f68a8342SChanwoo Choi */ 426ab11af04SChanwoo Choi int extcon_sync(struct extcon_dev *edev, unsigned int id) 427f68a8342SChanwoo Choi { 428f68a8342SChanwoo Choi char name_buf[120]; 429f68a8342SChanwoo Choi char state_buf[120]; 430f68a8342SChanwoo Choi char *prop_buf; 431f68a8342SChanwoo Choi char *envp[3]; 432f68a8342SChanwoo Choi int env_offset = 0; 433f68a8342SChanwoo Choi int length; 434046050f6SChanwoo Choi int index; 435ab11af04SChanwoo Choi int state; 436f68a8342SChanwoo Choi unsigned long flags; 437f68a8342SChanwoo Choi 4387eae43aeSChanwoo Choi if (!edev) 4397eae43aeSChanwoo Choi return -EINVAL; 4407eae43aeSChanwoo Choi 441ab11af04SChanwoo Choi index = find_cable_index_by_id(edev, id); 442ab11af04SChanwoo Choi if (index < 0) 443ab11af04SChanwoo Choi return index; 444ab11af04SChanwoo Choi 445f68a8342SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 446f68a8342SChanwoo Choi 447ab11af04SChanwoo Choi state = !!(edev->state & BIT(index)); 448815429b3SChanwoo Choi 449815429b3SChanwoo Choi /* 450815429b3SChanwoo Choi * Call functions in a raw notifier chain for the specific one 451815429b3SChanwoo Choi * external connector. 452815429b3SChanwoo Choi */ 453ab11af04SChanwoo Choi raw_notifier_call_chain(&edev->nh[index], state, edev); 454f7a89811SRoger Quadros 455815429b3SChanwoo Choi /* 456815429b3SChanwoo Choi * Call functions in a raw notifier chain for the all supported 457815429b3SChanwoo Choi * external connectors. 458815429b3SChanwoo Choi */ 459815429b3SChanwoo Choi raw_notifier_call_chain(&edev->nh_all, state, edev); 460815429b3SChanwoo Choi 461f68a8342SChanwoo Choi /* This could be in interrupt handler */ 462f68a8342SChanwoo Choi prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); 463ab11af04SChanwoo Choi if (!prop_buf) { 464f68a8342SChanwoo Choi /* Unlock early before uevent */ 465f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 466f68a8342SChanwoo Choi 467f68a8342SChanwoo Choi dev_err(&edev->dev, "out of memory in extcon_set_state\n"); 468f68a8342SChanwoo Choi kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); 469f68a8342SChanwoo Choi 470e7d9dd5aSPan Bian return -ENOMEM; 471f68a8342SChanwoo Choi } 472f68a8342SChanwoo Choi 473ab11af04SChanwoo Choi length = name_show(&edev->dev, NULL, prop_buf); 474ab11af04SChanwoo Choi if (length > 0) { 475ab11af04SChanwoo Choi if (prop_buf[length - 1] == '\n') 476ab11af04SChanwoo Choi prop_buf[length - 1] = 0; 477ab11af04SChanwoo Choi snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf); 478ab11af04SChanwoo Choi envp[env_offset++] = name_buf; 479ab11af04SChanwoo Choi } 480ab11af04SChanwoo Choi 481ab11af04SChanwoo Choi length = state_show(&edev->dev, NULL, prop_buf); 482ab11af04SChanwoo Choi if (length > 0) { 483ab11af04SChanwoo Choi if (prop_buf[length - 1] == '\n') 484ab11af04SChanwoo Choi prop_buf[length - 1] = 0; 485ab11af04SChanwoo Choi snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf); 486ab11af04SChanwoo Choi envp[env_offset++] = state_buf; 487ab11af04SChanwoo Choi } 488ab11af04SChanwoo Choi envp[env_offset] = NULL; 489ab11af04SChanwoo Choi 490ab11af04SChanwoo Choi /* Unlock early before uevent */ 491ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 492ab11af04SChanwoo Choi kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); 493ab11af04SChanwoo Choi free_page((unsigned long)prop_buf); 494ab11af04SChanwoo Choi 495ab11af04SChanwoo Choi return 0; 496ab11af04SChanwoo Choi } 497ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_sync); 498ab11af04SChanwoo Choi 499f68a8342SChanwoo Choi /** 500575c2b86SChanwoo Choi * extcon_get_state() - Get the state of a external connector. 501f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 5022a9de9c0SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 503f68a8342SChanwoo Choi */ 504575c2b86SChanwoo Choi int extcon_get_state(struct extcon_dev *edev, const unsigned int id) 505f68a8342SChanwoo Choi { 506575c2b86SChanwoo Choi int index, state; 507575c2b86SChanwoo Choi unsigned long flags; 5082a9de9c0SChanwoo Choi 5097eae43aeSChanwoo Choi if (!edev) 5107eae43aeSChanwoo Choi return -EINVAL; 5117eae43aeSChanwoo Choi 5122a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 5132a9de9c0SChanwoo Choi if (index < 0) 5142a9de9c0SChanwoo Choi return index; 5152a9de9c0SChanwoo Choi 516575c2b86SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 517575c2b86SChanwoo Choi state = is_extcon_attached(edev, index); 518575c2b86SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 519f68a8342SChanwoo Choi 520575c2b86SChanwoo Choi return state; 521f68a8342SChanwoo Choi } 522575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_state); 523f68a8342SChanwoo Choi 524f68a8342SChanwoo Choi /** 525575c2b86SChanwoo Choi * extcon_set_state() - Set the state of a external connector. 526ab11af04SChanwoo Choi * without a notification. 527f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 5282a9de9c0SChanwoo Choi * @id: the unique id of each external connector 5292a9de9c0SChanwoo Choi * in extcon enumeration. 5302a9de9c0SChanwoo Choi * @state: the new cable status. The default semantics is 531f68a8342SChanwoo Choi * true: attached / false: detached. 532ab11af04SChanwoo Choi * 533ab11af04SChanwoo Choi * This function only set the state of a external connector without 534ab11af04SChanwoo Choi * a notification. To synchronize the data of a external connector, 535ab11af04SChanwoo Choi * use extcon_set_state_sync() and extcon_sync(). 536f68a8342SChanwoo Choi */ 537575c2b86SChanwoo Choi int extcon_set_state(struct extcon_dev *edev, unsigned int id, 5382a9de9c0SChanwoo Choi bool cable_state) 539f68a8342SChanwoo Choi { 540ab11af04SChanwoo Choi unsigned long flags; 541ab11af04SChanwoo Choi int index, ret = 0; 542f68a8342SChanwoo Choi 5437eae43aeSChanwoo Choi if (!edev) 5447eae43aeSChanwoo Choi return -EINVAL; 5457eae43aeSChanwoo Choi 5462a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 5472a9de9c0SChanwoo Choi if (index < 0) 5482a9de9c0SChanwoo Choi return index; 5492a9de9c0SChanwoo Choi 550ab11af04SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 551ab11af04SChanwoo Choi 552ab11af04SChanwoo Choi /* Check whether the external connector's state is changed. */ 553ab11af04SChanwoo Choi if (!is_extcon_changed(edev, index, cable_state)) 554ab11af04SChanwoo Choi goto out; 555ab11af04SChanwoo Choi 556ab11af04SChanwoo Choi if (check_mutually_exclusive(edev, 557ab11af04SChanwoo Choi (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) { 558ab11af04SChanwoo Choi ret = -EPERM; 559ab11af04SChanwoo Choi goto out; 560ab11af04SChanwoo Choi } 561ab11af04SChanwoo Choi 562792e7e9eSChanwoo Choi /* 563792e7e9eSChanwoo Choi * Initialize the value of extcon property before setting 564792e7e9eSChanwoo Choi * the detached state for an external connector. 565792e7e9eSChanwoo Choi */ 566792e7e9eSChanwoo Choi if (!cable_state) 567792e7e9eSChanwoo Choi init_property(edev, id, index); 568792e7e9eSChanwoo Choi 569ab11af04SChanwoo Choi /* Update the state for a external connector. */ 570ab11af04SChanwoo Choi if (cable_state) 571ab11af04SChanwoo Choi edev->state |= BIT(index); 572ab11af04SChanwoo Choi else 573ab11af04SChanwoo Choi edev->state &= ~(BIT(index)); 574ab11af04SChanwoo Choi out: 575ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 576ab11af04SChanwoo Choi 577ab11af04SChanwoo Choi return ret; 578f68a8342SChanwoo Choi } 579575c2b86SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state); 580f68a8342SChanwoo Choi 581f68a8342SChanwoo Choi /** 582ab11af04SChanwoo Choi * extcon_set_state_sync() - Set the state of a external connector 583ab11af04SChanwoo Choi * with a notification. 584ab11af04SChanwoo Choi * @edev: the extcon device that has the cable. 585ab11af04SChanwoo Choi * @id: the unique id of each external connector 586ab11af04SChanwoo Choi * in extcon enumeration. 587ab11af04SChanwoo Choi * @state: the new cable status. The default semantics is 588ab11af04SChanwoo Choi * true: attached / false: detached. 589ab11af04SChanwoo Choi * 590ab11af04SChanwoo Choi * This function set the state of external connector and synchronize the data 591ab11af04SChanwoo Choi * by usning a notification. 592ab11af04SChanwoo Choi */ 593ab11af04SChanwoo Choi int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, 594ab11af04SChanwoo Choi bool cable_state) 595ab11af04SChanwoo Choi { 596ab11af04SChanwoo Choi int ret, index; 597ab11af04SChanwoo Choi unsigned long flags; 598ab11af04SChanwoo Choi 599ab11af04SChanwoo Choi index = find_cable_index_by_id(edev, id); 600ab11af04SChanwoo Choi if (index < 0) 601ab11af04SChanwoo Choi return index; 602ab11af04SChanwoo Choi 603ab11af04SChanwoo Choi /* Check whether the external connector's state is changed. */ 604ab11af04SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 605ab11af04SChanwoo Choi ret = is_extcon_changed(edev, index, cable_state); 606ab11af04SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 607ab11af04SChanwoo Choi if (!ret) 608ab11af04SChanwoo Choi return 0; 609ab11af04SChanwoo Choi 610ab11af04SChanwoo Choi ret = extcon_set_state(edev, id, cable_state); 611ab11af04SChanwoo Choi if (ret < 0) 612ab11af04SChanwoo Choi return ret; 613ab11af04SChanwoo Choi 614ab11af04SChanwoo Choi return extcon_sync(edev, id); 615ab11af04SChanwoo Choi } 616ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state_sync); 617ab11af04SChanwoo Choi 618ab11af04SChanwoo Choi /** 619792e7e9eSChanwoo Choi * extcon_get_property() - Get the property value of a specific cable. 620792e7e9eSChanwoo Choi * @edev: the extcon device that has the cable. 621792e7e9eSChanwoo Choi * @id: the unique id of each external connector 622792e7e9eSChanwoo Choi * in extcon enumeration. 623792e7e9eSChanwoo Choi * @prop: the property id among enum extcon_property. 624792e7e9eSChanwoo Choi * @prop_val: the pointer which store the value of property. 625792e7e9eSChanwoo Choi * 626792e7e9eSChanwoo Choi * When getting the property value of external connector, the external connector 627792e7e9eSChanwoo Choi * should be attached. If detached state, function just return 0 without 628792e7e9eSChanwoo Choi * property value. Also, the each property should be included in the list of 629792e7e9eSChanwoo Choi * supported properties according to the type of external connectors. 630792e7e9eSChanwoo Choi * 631792e7e9eSChanwoo Choi * Returns 0 if success or error number if fail 632792e7e9eSChanwoo Choi */ 633792e7e9eSChanwoo Choi int extcon_get_property(struct extcon_dev *edev, unsigned int id, 634792e7e9eSChanwoo Choi unsigned int prop, 635792e7e9eSChanwoo Choi union extcon_property_value *prop_val) 636792e7e9eSChanwoo Choi { 637792e7e9eSChanwoo Choi struct extcon_cable *cable; 638792e7e9eSChanwoo Choi unsigned long flags; 639792e7e9eSChanwoo Choi int index, ret = 0; 640792e7e9eSChanwoo Choi 641792e7e9eSChanwoo Choi *prop_val = (union extcon_property_value)(0); 642792e7e9eSChanwoo Choi 643792e7e9eSChanwoo Choi if (!edev) 644792e7e9eSChanwoo Choi return -EINVAL; 645792e7e9eSChanwoo Choi 646792e7e9eSChanwoo Choi /* Check whether the property is supported or not */ 647792e7e9eSChanwoo Choi if (!is_extcon_property_supported(id, prop)) 648792e7e9eSChanwoo Choi return -EINVAL; 649792e7e9eSChanwoo Choi 650792e7e9eSChanwoo Choi /* Find the cable index of external connector by using id */ 651792e7e9eSChanwoo Choi index = find_cable_index_by_id(edev, id); 652792e7e9eSChanwoo Choi if (index < 0) 653792e7e9eSChanwoo Choi return index; 654792e7e9eSChanwoo Choi 655792e7e9eSChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 656792e7e9eSChanwoo Choi 6577f2a0a16SChanwoo Choi /* Check whether the property is available or not. */ 6587f2a0a16SChanwoo Choi if (!is_extcon_property_capability(edev, id, index, prop)) { 6597f2a0a16SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 6607f2a0a16SChanwoo Choi return -EPERM; 6617f2a0a16SChanwoo Choi } 6627f2a0a16SChanwoo Choi 663792e7e9eSChanwoo Choi /* 664792e7e9eSChanwoo Choi * Check whether the external connector is attached. 665792e7e9eSChanwoo Choi * If external connector is detached, the user can not 666792e7e9eSChanwoo Choi * get the property value. 667792e7e9eSChanwoo Choi */ 668792e7e9eSChanwoo Choi if (!is_extcon_attached(edev, index)) { 669792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 670792e7e9eSChanwoo Choi return 0; 671792e7e9eSChanwoo Choi } 672792e7e9eSChanwoo Choi 673792e7e9eSChanwoo Choi cable = &edev->cables[index]; 674792e7e9eSChanwoo Choi 675792e7e9eSChanwoo Choi /* Get the property value according to extcon type */ 676792e7e9eSChanwoo Choi switch (prop) { 677792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 678792e7e9eSChanwoo Choi *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN]; 679792e7e9eSChanwoo Choi break; 680792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 681792e7e9eSChanwoo Choi *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN]; 682792e7e9eSChanwoo Choi break; 683792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 684792e7e9eSChanwoo Choi *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN]; 685792e7e9eSChanwoo Choi break; 686792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 687792e7e9eSChanwoo Choi *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN]; 688792e7e9eSChanwoo Choi break; 689792e7e9eSChanwoo Choi default: 690792e7e9eSChanwoo Choi ret = -EINVAL; 691792e7e9eSChanwoo Choi break; 692792e7e9eSChanwoo Choi } 693792e7e9eSChanwoo Choi 694792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 695792e7e9eSChanwoo Choi 696792e7e9eSChanwoo Choi return ret; 697792e7e9eSChanwoo Choi } 698792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property); 699792e7e9eSChanwoo Choi 700792e7e9eSChanwoo Choi /** 701792e7e9eSChanwoo Choi * extcon_set_property() - Set the property value of a specific cable. 702792e7e9eSChanwoo Choi * @edev: the extcon device that has the cable. 703792e7e9eSChanwoo Choi * @id: the unique id of each external connector 704792e7e9eSChanwoo Choi * in extcon enumeration. 705792e7e9eSChanwoo Choi * @prop: the property id among enum extcon_property. 706792e7e9eSChanwoo Choi * @prop_val: the pointer including the new value of property. 707792e7e9eSChanwoo Choi * 708792e7e9eSChanwoo Choi * The each property should be included in the list of supported properties 709792e7e9eSChanwoo Choi * according to the type of external connectors. 710792e7e9eSChanwoo Choi * 711792e7e9eSChanwoo Choi * Returns 0 if success or error number if fail 712792e7e9eSChanwoo Choi */ 713792e7e9eSChanwoo Choi int extcon_set_property(struct extcon_dev *edev, unsigned int id, 714792e7e9eSChanwoo Choi unsigned int prop, 715792e7e9eSChanwoo Choi union extcon_property_value prop_val) 716792e7e9eSChanwoo Choi { 717792e7e9eSChanwoo Choi struct extcon_cable *cable; 718792e7e9eSChanwoo Choi unsigned long flags; 719792e7e9eSChanwoo Choi int index, ret = 0; 720792e7e9eSChanwoo Choi 721792e7e9eSChanwoo Choi if (!edev) 722792e7e9eSChanwoo Choi return -EINVAL; 723792e7e9eSChanwoo Choi 724792e7e9eSChanwoo Choi /* Check whether the property is supported or not */ 725792e7e9eSChanwoo Choi if (!is_extcon_property_supported(id, prop)) 726792e7e9eSChanwoo Choi return -EINVAL; 727792e7e9eSChanwoo Choi 728792e7e9eSChanwoo Choi /* Find the cable index of external connector by using id */ 729792e7e9eSChanwoo Choi index = find_cable_index_by_id(edev, id); 730792e7e9eSChanwoo Choi if (index < 0) 731792e7e9eSChanwoo Choi return index; 732792e7e9eSChanwoo Choi 733792e7e9eSChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 734792e7e9eSChanwoo Choi 7357f2a0a16SChanwoo Choi /* Check whether the property is available or not. */ 7367f2a0a16SChanwoo Choi if (!is_extcon_property_capability(edev, id, index, prop)) { 7377f2a0a16SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 7387f2a0a16SChanwoo Choi return -EPERM; 7397f2a0a16SChanwoo Choi } 7407f2a0a16SChanwoo Choi 741792e7e9eSChanwoo Choi cable = &edev->cables[index]; 742792e7e9eSChanwoo Choi 743792e7e9eSChanwoo Choi /* Set the property value according to extcon type */ 744792e7e9eSChanwoo Choi switch (prop) { 745792e7e9eSChanwoo Choi case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: 746792e7e9eSChanwoo Choi cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val; 747792e7e9eSChanwoo Choi break; 748792e7e9eSChanwoo Choi case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: 749792e7e9eSChanwoo Choi cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val; 750792e7e9eSChanwoo Choi break; 751792e7e9eSChanwoo Choi case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: 752792e7e9eSChanwoo Choi cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val; 753792e7e9eSChanwoo Choi break; 754792e7e9eSChanwoo Choi case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: 755792e7e9eSChanwoo Choi cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val; 756792e7e9eSChanwoo Choi break; 757792e7e9eSChanwoo Choi default: 758792e7e9eSChanwoo Choi ret = -EINVAL; 759792e7e9eSChanwoo Choi break; 760792e7e9eSChanwoo Choi } 761792e7e9eSChanwoo Choi 762792e7e9eSChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 763792e7e9eSChanwoo Choi 764792e7e9eSChanwoo Choi return ret; 765792e7e9eSChanwoo Choi } 766792e7e9eSChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property); 767792e7e9eSChanwoo Choi 768792e7e9eSChanwoo Choi /** 769ab11af04SChanwoo Choi * extcon_set_property_sync() - Set the property value of a specific cable 770ab11af04SChanwoo Choi with a notification. 771ab11af04SChanwoo Choi * @prop_val: the pointer including the new value of property. 772ab11af04SChanwoo Choi * 773ab11af04SChanwoo Choi * When setting the property value of external connector, the external connector 774ab11af04SChanwoo Choi * should be attached. The each property should be included in the list of 775ab11af04SChanwoo Choi * supported properties according to the type of external connectors. 776ab11af04SChanwoo Choi * 777ab11af04SChanwoo Choi * Returns 0 if success or error number if fail 778ab11af04SChanwoo Choi */ 779ab11af04SChanwoo Choi int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id, 780ab11af04SChanwoo Choi unsigned int prop, 781ab11af04SChanwoo Choi union extcon_property_value prop_val) 782ab11af04SChanwoo Choi { 783ab11af04SChanwoo Choi int ret; 784ab11af04SChanwoo Choi 785ab11af04SChanwoo Choi ret = extcon_set_property(edev, id, prop, prop_val); 786ab11af04SChanwoo Choi if (ret < 0) 787ab11af04SChanwoo Choi return ret; 788ab11af04SChanwoo Choi 789ab11af04SChanwoo Choi return extcon_sync(edev, id); 790ab11af04SChanwoo Choi } 791ab11af04SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_sync); 792ab11af04SChanwoo Choi 793ab11af04SChanwoo Choi /** 7947f2a0a16SChanwoo Choi * extcon_get_property_capability() - Get the capability of property 7957f2a0a16SChanwoo Choi * of an external connector. 7967f2a0a16SChanwoo Choi * @edev: the extcon device that has the cable. 7977f2a0a16SChanwoo Choi * @id: the unique id of each external connector 7987f2a0a16SChanwoo Choi * in extcon enumeration. 7997f2a0a16SChanwoo Choi * @prop: the property id among enum extcon_property. 8007f2a0a16SChanwoo Choi * 8017f2a0a16SChanwoo Choi * Returns 1 if the property is available or 0 if not available. 8027f2a0a16SChanwoo Choi */ 8037f2a0a16SChanwoo Choi int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id, 8047f2a0a16SChanwoo Choi unsigned int prop) 8057f2a0a16SChanwoo Choi { 8067f2a0a16SChanwoo Choi int index; 8077f2a0a16SChanwoo Choi 8087f2a0a16SChanwoo Choi if (!edev) 8097f2a0a16SChanwoo Choi return -EINVAL; 8107f2a0a16SChanwoo Choi 8117f2a0a16SChanwoo Choi /* Check whether the property is supported or not */ 8127f2a0a16SChanwoo Choi if (!is_extcon_property_supported(id, prop)) 8137f2a0a16SChanwoo Choi return -EINVAL; 8147f2a0a16SChanwoo Choi 8157f2a0a16SChanwoo Choi /* Find the cable index of external connector by using id */ 8167f2a0a16SChanwoo Choi index = find_cable_index_by_id(edev, id); 8177f2a0a16SChanwoo Choi if (index < 0) 8187f2a0a16SChanwoo Choi return index; 8197f2a0a16SChanwoo Choi 8207f2a0a16SChanwoo Choi return is_extcon_property_capability(edev, id, index, prop); 8217f2a0a16SChanwoo Choi } 8227f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_property_capability); 8237f2a0a16SChanwoo Choi 8247f2a0a16SChanwoo Choi /** 8257f2a0a16SChanwoo Choi * extcon_set_property_capability() - Set the capability of a property 8267f2a0a16SChanwoo Choi * of an external connector. 8277f2a0a16SChanwoo Choi * @edev: the extcon device that has the cable. 8287f2a0a16SChanwoo Choi * @id: the unique id of each external connector 8297f2a0a16SChanwoo Choi * in extcon enumeration. 8307f2a0a16SChanwoo Choi * @prop: the property id among enum extcon_property. 8317f2a0a16SChanwoo Choi * 8327f2a0a16SChanwoo Choi * This function set the capability of a property for an external connector 8337f2a0a16SChanwoo Choi * to mark the bit in capability bitmap which mean the available state of 8347f2a0a16SChanwoo Choi * a property. 8357f2a0a16SChanwoo Choi * 8367f2a0a16SChanwoo Choi * Returns 0 if success or error number if fail 8377f2a0a16SChanwoo Choi */ 8387f2a0a16SChanwoo Choi int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id, 8397f2a0a16SChanwoo Choi unsigned int prop) 8407f2a0a16SChanwoo Choi { 8417f2a0a16SChanwoo Choi struct extcon_cable *cable; 8427f2a0a16SChanwoo Choi int index, type, ret = 0; 8437f2a0a16SChanwoo Choi 8447f2a0a16SChanwoo Choi if (!edev) 8457f2a0a16SChanwoo Choi return -EINVAL; 8467f2a0a16SChanwoo Choi 8477f2a0a16SChanwoo Choi /* Check whether the property is supported or not. */ 8487f2a0a16SChanwoo Choi if (!is_extcon_property_supported(id, prop)) 8497f2a0a16SChanwoo Choi return -EINVAL; 8507f2a0a16SChanwoo Choi 8517f2a0a16SChanwoo Choi /* Find the cable index of external connector by using id. */ 8527f2a0a16SChanwoo Choi index = find_cable_index_by_id(edev, id); 8537f2a0a16SChanwoo Choi if (index < 0) 8547f2a0a16SChanwoo Choi return index; 8557f2a0a16SChanwoo Choi 8567f2a0a16SChanwoo Choi type = get_extcon_type(prop); 8577f2a0a16SChanwoo Choi if (type < 0) 8587f2a0a16SChanwoo Choi return type; 8597f2a0a16SChanwoo Choi 8607f2a0a16SChanwoo Choi cable = &edev->cables[index]; 8617f2a0a16SChanwoo Choi 8627f2a0a16SChanwoo Choi switch (type) { 8637f2a0a16SChanwoo Choi case EXTCON_TYPE_USB: 8647f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); 8657f2a0a16SChanwoo Choi break; 8667f2a0a16SChanwoo Choi case EXTCON_TYPE_CHG: 8677f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); 8687f2a0a16SChanwoo Choi break; 8697f2a0a16SChanwoo Choi case EXTCON_TYPE_JACK: 8707f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); 8717f2a0a16SChanwoo Choi break; 8727f2a0a16SChanwoo Choi case EXTCON_TYPE_DISP: 8737f2a0a16SChanwoo Choi __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); 8747f2a0a16SChanwoo Choi break; 8757f2a0a16SChanwoo Choi default: 8767f2a0a16SChanwoo Choi ret = -EINVAL; 8777f2a0a16SChanwoo Choi } 8787f2a0a16SChanwoo Choi 8797f2a0a16SChanwoo Choi return ret; 8807f2a0a16SChanwoo Choi } 8817f2a0a16SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_property_capability); 8827f2a0a16SChanwoo Choi 8837f2a0a16SChanwoo Choi /** 884f68a8342SChanwoo Choi * extcon_get_extcon_dev() - Get the extcon device instance from the name 885f68a8342SChanwoo Choi * @extcon_name: The extcon name provided with extcon_dev_register() 886f68a8342SChanwoo Choi */ 887f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) 888f68a8342SChanwoo Choi { 889f68a8342SChanwoo Choi struct extcon_dev *sd; 890f68a8342SChanwoo Choi 8917eae43aeSChanwoo Choi if (!extcon_name) 8927eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 8937eae43aeSChanwoo Choi 894f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 895f68a8342SChanwoo Choi list_for_each_entry(sd, &extcon_dev_list, entry) { 896f68a8342SChanwoo Choi if (!strcmp(sd->name, extcon_name)) 897f68a8342SChanwoo Choi goto out; 898f68a8342SChanwoo Choi } 899f68a8342SChanwoo Choi sd = NULL; 900f68a8342SChanwoo Choi out: 901f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 902f68a8342SChanwoo Choi return sd; 903f68a8342SChanwoo Choi } 904f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); 905f68a8342SChanwoo Choi 906f68a8342SChanwoo Choi /** 907f68a8342SChanwoo Choi * extcon_register_notifier() - Register a notifiee to get notified by 908f68a8342SChanwoo Choi * any attach status changes from the extcon. 909046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 910046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 911f68a8342SChanwoo Choi * @nb: a notifier block to be registered. 912f68a8342SChanwoo Choi * 913f68a8342SChanwoo Choi * Note that the second parameter given to the callback of nb (val) is 914f68a8342SChanwoo Choi * "old_state", not the current state. The current state can be retrieved 915f68a8342SChanwoo Choi * by looking at the third pameter (edev pointer)'s state value. 916f68a8342SChanwoo Choi */ 91773b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, 918f68a8342SChanwoo Choi struct notifier_block *nb) 919f68a8342SChanwoo Choi { 92066bee35fSHans de Goede unsigned long flags; 9212c8116a1SManinder Singh int ret, idx = -EINVAL; 922046050f6SChanwoo Choi 92301b4c9a1SChanwoo Choi if (!edev || !nb) 9247eae43aeSChanwoo Choi return -EINVAL; 9257eae43aeSChanwoo Choi 926046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 927a05f44c8SStephen Boyd if (idx < 0) 928a05f44c8SStephen Boyd return idx; 92966bee35fSHans de Goede 93066bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 931046050f6SChanwoo Choi ret = raw_notifier_chain_register(&edev->nh[idx], nb); 93266bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 93366bee35fSHans de Goede 93466bee35fSHans de Goede return ret; 935f68a8342SChanwoo Choi } 936f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier); 937f68a8342SChanwoo Choi 938f68a8342SChanwoo Choi /** 939f68a8342SChanwoo Choi * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. 940046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 941046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 942046050f6SChanwoo Choi * @nb: a notifier block to be registered. 943f68a8342SChanwoo Choi */ 94473b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, 945f68a8342SChanwoo Choi struct notifier_block *nb) 946f68a8342SChanwoo Choi { 94766bee35fSHans de Goede unsigned long flags; 948046050f6SChanwoo Choi int ret, idx; 949046050f6SChanwoo Choi 9507eae43aeSChanwoo Choi if (!edev || !nb) 9517eae43aeSChanwoo Choi return -EINVAL; 9527eae43aeSChanwoo Choi 953046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 954a05f44c8SStephen Boyd if (idx < 0) 955a05f44c8SStephen Boyd return idx; 95666bee35fSHans de Goede 95766bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 958046050f6SChanwoo Choi ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); 95966bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 96066bee35fSHans de Goede 96166bee35fSHans de Goede return ret; 962f68a8342SChanwoo Choi } 963f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier); 964f68a8342SChanwoo Choi 965815429b3SChanwoo Choi /** 966815429b3SChanwoo Choi * extcon_register_notifier_all() - Register a notifier block for all connectors 967826a47e9SMarkus Elfring * @edev: the extcon device that has the external connector. 968815429b3SChanwoo Choi * @nb: a notifier block to be registered. 969815429b3SChanwoo Choi * 970826a47e9SMarkus Elfring * This function registers a notifier block in order to receive the state 971815429b3SChanwoo Choi * change of all supported external connectors from extcon device. 972826a47e9SMarkus Elfring * And the second parameter given to the callback of nb (val) is 973815429b3SChanwoo Choi * the current state and third parameter is the edev pointer. 974815429b3SChanwoo Choi * 975815429b3SChanwoo Choi * Returns 0 if success or error number if fail 976815429b3SChanwoo Choi */ 977815429b3SChanwoo Choi int extcon_register_notifier_all(struct extcon_dev *edev, 978815429b3SChanwoo Choi struct notifier_block *nb) 979815429b3SChanwoo Choi { 980815429b3SChanwoo Choi unsigned long flags; 981815429b3SChanwoo Choi int ret; 982815429b3SChanwoo Choi 983815429b3SChanwoo Choi if (!edev || !nb) 984815429b3SChanwoo Choi return -EINVAL; 985815429b3SChanwoo Choi 986815429b3SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 987815429b3SChanwoo Choi ret = raw_notifier_chain_register(&edev->nh_all, nb); 988815429b3SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 989815429b3SChanwoo Choi 990815429b3SChanwoo Choi return ret; 991815429b3SChanwoo Choi } 992815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier_all); 993815429b3SChanwoo Choi 994815429b3SChanwoo Choi /** 995815429b3SChanwoo Choi * extcon_unregister_notifier_all() - Unregister a notifier block from extcon. 996815429b3SChanwoo Choi * @edev: the extcon device that has the external connecotr. 997815429b3SChanwoo Choi * @nb: a notifier block to be registered. 998815429b3SChanwoo Choi * 999815429b3SChanwoo Choi * Returns 0 if success or error number if fail 1000815429b3SChanwoo Choi */ 1001815429b3SChanwoo Choi int extcon_unregister_notifier_all(struct extcon_dev *edev, 1002815429b3SChanwoo Choi struct notifier_block *nb) 1003815429b3SChanwoo Choi { 1004815429b3SChanwoo Choi unsigned long flags; 1005815429b3SChanwoo Choi int ret; 1006815429b3SChanwoo Choi 1007815429b3SChanwoo Choi if (!edev || !nb) 1008815429b3SChanwoo Choi return -EINVAL; 1009815429b3SChanwoo Choi 1010815429b3SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 1011815429b3SChanwoo Choi ret = raw_notifier_chain_unregister(&edev->nh_all, nb); 1012815429b3SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 1013815429b3SChanwoo Choi 1014815429b3SChanwoo Choi return ret; 1015815429b3SChanwoo Choi } 1016815429b3SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all); 1017815429b3SChanwoo Choi 1018f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = { 1019f68a8342SChanwoo Choi &dev_attr_state.attr, 1020f68a8342SChanwoo Choi &dev_attr_name.attr, 1021f68a8342SChanwoo Choi NULL, 1022f68a8342SChanwoo Choi }; 1023f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon); 1024f68a8342SChanwoo Choi 1025f68a8342SChanwoo Choi static int create_extcon_class(void) 1026f68a8342SChanwoo Choi { 1027f68a8342SChanwoo Choi if (!extcon_class) { 1028f68a8342SChanwoo Choi extcon_class = class_create(THIS_MODULE, "extcon"); 1029f68a8342SChanwoo Choi if (IS_ERR(extcon_class)) 1030f68a8342SChanwoo Choi return PTR_ERR(extcon_class); 1031f68a8342SChanwoo Choi extcon_class->dev_groups = extcon_groups; 1032f68a8342SChanwoo Choi } 1033f68a8342SChanwoo Choi 1034f68a8342SChanwoo Choi return 0; 1035f68a8342SChanwoo Choi } 1036f68a8342SChanwoo Choi 1037f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev) 1038f68a8342SChanwoo Choi { 1039f68a8342SChanwoo Choi } 1040f68a8342SChanwoo Choi 1041f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive"; 1042f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev) 1043f68a8342SChanwoo Choi { 1044f68a8342SChanwoo Choi } 1045f68a8342SChanwoo Choi 1046f68a8342SChanwoo Choi /* 1047f68a8342SChanwoo Choi * extcon_dev_allocate() - Allocate the memory of extcon device. 10482a9de9c0SChanwoo Choi * @supported_cable: Array of supported extcon ending with EXTCON_NONE. 1049f68a8342SChanwoo Choi * If supported_cable is NULL, cable name related APIs 1050f68a8342SChanwoo Choi * are disabled. 1051f68a8342SChanwoo Choi * 1052f68a8342SChanwoo Choi * This function allocates the memory for extcon device without allocating 1053f68a8342SChanwoo Choi * memory in each extcon provider driver and initialize default setting for 1054f68a8342SChanwoo Choi * extcon device. 1055f68a8342SChanwoo Choi * 1056f68a8342SChanwoo Choi * Return the pointer of extcon device if success or ERR_PTR(err) if fail 1057f68a8342SChanwoo Choi */ 105873b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) 1059f68a8342SChanwoo Choi { 1060f68a8342SChanwoo Choi struct extcon_dev *edev; 1061f68a8342SChanwoo Choi 10627eae43aeSChanwoo Choi if (!supported_cable) 10637eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 10647eae43aeSChanwoo Choi 1065f68a8342SChanwoo Choi edev = kzalloc(sizeof(*edev), GFP_KERNEL); 1066f68a8342SChanwoo Choi if (!edev) 1067f68a8342SChanwoo Choi return ERR_PTR(-ENOMEM); 1068f68a8342SChanwoo Choi 1069f68a8342SChanwoo Choi edev->max_supported = 0; 1070f68a8342SChanwoo Choi edev->supported_cable = supported_cable; 1071f68a8342SChanwoo Choi 1072f68a8342SChanwoo Choi return edev; 1073f68a8342SChanwoo Choi } 1074f68a8342SChanwoo Choi 1075f68a8342SChanwoo Choi /* 1076f68a8342SChanwoo Choi * extcon_dev_free() - Free the memory of extcon device. 1077f68a8342SChanwoo Choi * @edev: the extcon device to free 1078f68a8342SChanwoo Choi */ 1079f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev) 1080f68a8342SChanwoo Choi { 1081f68a8342SChanwoo Choi kfree(edev); 1082f68a8342SChanwoo Choi } 1083f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free); 1084f68a8342SChanwoo Choi 1085f68a8342SChanwoo Choi /** 1086f68a8342SChanwoo Choi * extcon_dev_register() - Register a new extcon device 1087f68a8342SChanwoo Choi * @edev : the new extcon device (should be allocated before calling) 1088f68a8342SChanwoo Choi * 1089f68a8342SChanwoo Choi * Among the members of edev struct, please set the "user initializing data" 1090f68a8342SChanwoo Choi * in any case and set the "optional callbacks" if required. However, please 1091f68a8342SChanwoo Choi * do not set the values of "internal data", which are initialized by 1092f68a8342SChanwoo Choi * this function. 1093f68a8342SChanwoo Choi */ 1094f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev) 1095f68a8342SChanwoo Choi { 1096f68a8342SChanwoo Choi int ret, index = 0; 109771c3ffa5SChanwoo Choi static atomic_t edev_no = ATOMIC_INIT(-1); 1098f68a8342SChanwoo Choi 1099f68a8342SChanwoo Choi if (!extcon_class) { 1100f68a8342SChanwoo Choi ret = create_extcon_class(); 1101f68a8342SChanwoo Choi if (ret < 0) 1102f68a8342SChanwoo Choi return ret; 1103f68a8342SChanwoo Choi } 1104f68a8342SChanwoo Choi 11057eae43aeSChanwoo Choi if (!edev || !edev->supported_cable) 11062a9de9c0SChanwoo Choi return -EINVAL; 1107f68a8342SChanwoo Choi 11082a9de9c0SChanwoo Choi for (; edev->supported_cable[index] != EXTCON_NONE; index++); 11092a9de9c0SChanwoo Choi 11102a9de9c0SChanwoo Choi edev->max_supported = index; 1111f68a8342SChanwoo Choi if (index > SUPPORTED_CABLE_MAX) { 11122a9de9c0SChanwoo Choi dev_err(&edev->dev, 11132a9de9c0SChanwoo Choi "exceed the maximum number of supported cables\n"); 1114f68a8342SChanwoo Choi return -EINVAL; 1115f68a8342SChanwoo Choi } 1116f68a8342SChanwoo Choi 1117f68a8342SChanwoo Choi edev->dev.class = extcon_class; 1118f68a8342SChanwoo Choi edev->dev.release = extcon_dev_release; 1119f68a8342SChanwoo Choi 112071c3ffa5SChanwoo Choi edev->name = dev_name(edev->dev.parent); 1121f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(edev->name)) { 1122f68a8342SChanwoo Choi dev_err(&edev->dev, 1123f68a8342SChanwoo Choi "extcon device name is null\n"); 1124f68a8342SChanwoo Choi return -EINVAL; 1125f68a8342SChanwoo Choi } 112671c3ffa5SChanwoo Choi dev_set_name(&edev->dev, "extcon%lu", 112771c3ffa5SChanwoo Choi (unsigned long)atomic_inc_return(&edev_no)); 1128f68a8342SChanwoo Choi 1129f68a8342SChanwoo Choi if (edev->max_supported) { 1130f68a8342SChanwoo Choi char buf[10]; 1131f68a8342SChanwoo Choi char *str; 1132f68a8342SChanwoo Choi struct extcon_cable *cable; 1133f68a8342SChanwoo Choi 1134f68a8342SChanwoo Choi edev->cables = kzalloc(sizeof(struct extcon_cable) * 1135f68a8342SChanwoo Choi edev->max_supported, GFP_KERNEL); 1136f68a8342SChanwoo Choi if (!edev->cables) { 1137f68a8342SChanwoo Choi ret = -ENOMEM; 1138f68a8342SChanwoo Choi goto err_sysfs_alloc; 1139f68a8342SChanwoo Choi } 1140f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) { 1141f68a8342SChanwoo Choi cable = &edev->cables[index]; 1142f68a8342SChanwoo Choi 1143f68a8342SChanwoo Choi snprintf(buf, 10, "cable.%d", index); 1144f68a8342SChanwoo Choi str = kzalloc(sizeof(char) * (strlen(buf) + 1), 1145f68a8342SChanwoo Choi GFP_KERNEL); 1146f68a8342SChanwoo Choi if (!str) { 1147f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 1148f68a8342SChanwoo Choi cable = &edev->cables[index]; 1149f68a8342SChanwoo Choi kfree(cable->attr_g.name); 1150f68a8342SChanwoo Choi } 1151f68a8342SChanwoo Choi ret = -ENOMEM; 1152f68a8342SChanwoo Choi 1153f68a8342SChanwoo Choi goto err_alloc_cables; 1154f68a8342SChanwoo Choi } 1155f68a8342SChanwoo Choi strcpy(str, buf); 1156f68a8342SChanwoo Choi 1157f68a8342SChanwoo Choi cable->edev = edev; 1158f68a8342SChanwoo Choi cable->cable_index = index; 1159f68a8342SChanwoo Choi cable->attrs[0] = &cable->attr_name.attr; 1160f68a8342SChanwoo Choi cable->attrs[1] = &cable->attr_state.attr; 1161f68a8342SChanwoo Choi cable->attrs[2] = NULL; 1162f68a8342SChanwoo Choi cable->attr_g.name = str; 1163f68a8342SChanwoo Choi cable->attr_g.attrs = cable->attrs; 1164f68a8342SChanwoo Choi 1165f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_name.attr); 1166f68a8342SChanwoo Choi cable->attr_name.attr.name = "name"; 1167f68a8342SChanwoo Choi cable->attr_name.attr.mode = 0444; 1168f68a8342SChanwoo Choi cable->attr_name.show = cable_name_show; 1169f68a8342SChanwoo Choi 1170f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_state.attr); 1171f68a8342SChanwoo Choi cable->attr_state.attr.name = "state"; 1172f68a8342SChanwoo Choi cable->attr_state.attr.mode = 0444; 1173f68a8342SChanwoo Choi cable->attr_state.show = cable_state_show; 1174f68a8342SChanwoo Choi } 1175f68a8342SChanwoo Choi } 1176f68a8342SChanwoo Choi 1177f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 1178f68a8342SChanwoo Choi char buf[80]; 1179f68a8342SChanwoo Choi char *name; 1180f68a8342SChanwoo Choi 1181f68a8342SChanwoo Choi /* Count the size of mutually_exclusive array */ 1182f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 1183f68a8342SChanwoo Choi ; 1184f68a8342SChanwoo Choi 1185f68a8342SChanwoo Choi edev->attrs_muex = kzalloc(sizeof(struct attribute *) * 1186f68a8342SChanwoo Choi (index + 1), GFP_KERNEL); 1187f68a8342SChanwoo Choi if (!edev->attrs_muex) { 1188f68a8342SChanwoo Choi ret = -ENOMEM; 1189f68a8342SChanwoo Choi goto err_muex; 1190f68a8342SChanwoo Choi } 1191f68a8342SChanwoo Choi 1192f68a8342SChanwoo Choi edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * 1193f68a8342SChanwoo Choi index, GFP_KERNEL); 1194f68a8342SChanwoo Choi if (!edev->d_attrs_muex) { 1195f68a8342SChanwoo Choi ret = -ENOMEM; 1196f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1197f68a8342SChanwoo Choi goto err_muex; 1198f68a8342SChanwoo Choi } 1199f68a8342SChanwoo Choi 1200f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) { 1201f68a8342SChanwoo Choi sprintf(buf, "0x%x", edev->mutually_exclusive[index]); 1202f68a8342SChanwoo Choi name = kzalloc(sizeof(char) * (strlen(buf) + 1), 1203f68a8342SChanwoo Choi GFP_KERNEL); 1204f68a8342SChanwoo Choi if (!name) { 1205f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 1206f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr. 1207f68a8342SChanwoo Choi name); 1208f68a8342SChanwoo Choi } 1209f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 1210f68a8342SChanwoo Choi kfree(edev->attrs_muex); 1211f68a8342SChanwoo Choi ret = -ENOMEM; 1212f68a8342SChanwoo Choi goto err_muex; 1213f68a8342SChanwoo Choi } 1214f68a8342SChanwoo Choi strcpy(name, buf); 1215f68a8342SChanwoo Choi sysfs_attr_init(&edev->d_attrs_muex[index].attr); 1216f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.name = name; 1217f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.mode = 0000; 1218f68a8342SChanwoo Choi edev->attrs_muex[index] = &edev->d_attrs_muex[index] 1219f68a8342SChanwoo Choi .attr; 1220f68a8342SChanwoo Choi } 1221f68a8342SChanwoo Choi edev->attr_g_muex.name = muex_name; 1222f68a8342SChanwoo Choi edev->attr_g_muex.attrs = edev->attrs_muex; 1223f68a8342SChanwoo Choi 1224f68a8342SChanwoo Choi } 1225f68a8342SChanwoo Choi 1226f68a8342SChanwoo Choi if (edev->max_supported) { 1227f68a8342SChanwoo Choi edev->extcon_dev_type.groups = 1228f68a8342SChanwoo Choi kzalloc(sizeof(struct attribute_group *) * 1229f68a8342SChanwoo Choi (edev->max_supported + 2), 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. 1299f68a8342SChanwoo Choi * @edev: the extcon device instance 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 1344f68a8342SChanwoo Choi /* 1345f68a8342SChanwoo Choi * extcon_get_edev_by_phandle - Get the extcon device from devicetree 1346f68a8342SChanwoo Choi * @dev - instance to the given device 1347f68a8342SChanwoo Choi * @index - index into list of extcon_dev 1348f68a8342SChanwoo Choi * 1349f68a8342SChanwoo Choi * return the instance of extcon device 1350f68a8342SChanwoo Choi */ 1351f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1352f68a8342SChanwoo Choi { 1353f68a8342SChanwoo Choi struct device_node *node; 1354f68a8342SChanwoo Choi struct extcon_dev *edev; 1355f68a8342SChanwoo Choi 13567eae43aeSChanwoo Choi if (!dev) 13577eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 13587eae43aeSChanwoo Choi 1359f68a8342SChanwoo Choi if (!dev->of_node) { 1360e8752b7aSStephen Boyd dev_dbg(dev, "device does not have a device node entry\n"); 1361f68a8342SChanwoo Choi return ERR_PTR(-EINVAL); 1362f68a8342SChanwoo Choi } 1363f68a8342SChanwoo Choi 1364f68a8342SChanwoo Choi node = of_parse_phandle(dev->of_node, "extcon", index); 1365f68a8342SChanwoo Choi if (!node) { 1366e8752b7aSStephen Boyd dev_dbg(dev, "failed to get phandle in %s node\n", 1367f68a8342SChanwoo Choi dev->of_node->full_name); 1368f68a8342SChanwoo Choi return ERR_PTR(-ENODEV); 1369f68a8342SChanwoo Choi } 1370f68a8342SChanwoo Choi 1371f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 1372f68a8342SChanwoo Choi list_for_each_entry(edev, &extcon_dev_list, entry) { 1373f68a8342SChanwoo Choi if (edev->dev.parent && edev->dev.parent->of_node == node) { 1374f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 13755d5c4c13SPeter Chen of_node_put(node); 1376f68a8342SChanwoo Choi return edev; 1377f68a8342SChanwoo Choi } 1378f68a8342SChanwoo Choi } 1379f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 13805d5c4c13SPeter Chen of_node_put(node); 1381f68a8342SChanwoo Choi 1382f68a8342SChanwoo Choi return ERR_PTR(-EPROBE_DEFER); 1383f68a8342SChanwoo Choi } 1384f68a8342SChanwoo Choi #else 1385f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1386f68a8342SChanwoo Choi { 1387f68a8342SChanwoo Choi return ERR_PTR(-ENOSYS); 1388f68a8342SChanwoo Choi } 1389f68a8342SChanwoo Choi #endif /* CONFIG_OF */ 1390f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); 1391f68a8342SChanwoo Choi 1392707d7550SChanwoo Choi /** 1393707d7550SChanwoo Choi * extcon_get_edev_name() - Get the name of the extcon device. 1394707d7550SChanwoo Choi * @edev: the extcon device 1395707d7550SChanwoo Choi */ 1396707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev) 1397707d7550SChanwoo Choi { 1398707d7550SChanwoo Choi return !edev ? NULL : edev->name; 1399707d7550SChanwoo Choi } 1400707d7550SChanwoo Choi 1401f68a8342SChanwoo Choi static int __init extcon_class_init(void) 1402f68a8342SChanwoo Choi { 1403f68a8342SChanwoo Choi return create_extcon_class(); 1404f68a8342SChanwoo Choi } 1405f68a8342SChanwoo Choi module_init(extcon_class_init); 1406f68a8342SChanwoo Choi 1407f68a8342SChanwoo Choi static void __exit extcon_class_exit(void) 1408f68a8342SChanwoo Choi { 1409f68a8342SChanwoo Choi class_destroy(extcon_class); 1410f68a8342SChanwoo Choi } 1411f68a8342SChanwoo Choi module_exit(extcon_class_exit); 1412f68a8342SChanwoo Choi 14132a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); 1414f68a8342SChanwoo Choi MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); 1415f68a8342SChanwoo Choi MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 1416f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 1417f68a8342SChanwoo Choi MODULE_DESCRIPTION("External connector (extcon) class driver"); 1418f68a8342SChanwoo Choi MODULE_LICENSE("GPL"); 1419