1f68a8342SChanwoo Choi /* 2b9ec23c0SChanwoo Choi * drivers/extcon/extcon.c - External Connector (extcon) framework. 3f68a8342SChanwoo Choi * 4f68a8342SChanwoo Choi * External connector (extcon) class driver 5f68a8342SChanwoo Choi * 62a9de9c0SChanwoo Choi * Copyright (C) 2015 Samsung Electronics 72a9de9c0SChanwoo Choi * Author: Chanwoo Choi <[email protected]> 82a9de9c0SChanwoo Choi * 9f68a8342SChanwoo Choi * Copyright (C) 2012 Samsung Electronics 10f68a8342SChanwoo Choi * Author: Donggeun Kim <[email protected]> 11f68a8342SChanwoo Choi * Author: MyungJoo Ham <[email protected]> 12f68a8342SChanwoo Choi * 13f68a8342SChanwoo Choi * based on android/drivers/switch/switch_class.c 14f68a8342SChanwoo Choi * Copyright (C) 2008 Google, Inc. 15f68a8342SChanwoo Choi * Author: Mike Lockwood <[email protected]> 16f68a8342SChanwoo Choi * 17f68a8342SChanwoo Choi * This software is licensed under the terms of the GNU General Public 18f68a8342SChanwoo Choi * License version 2, as published by the Free Software Foundation, and 19f68a8342SChanwoo Choi * may be copied, distributed, and modified under those terms. 20f68a8342SChanwoo Choi * 21f68a8342SChanwoo Choi * This program is distributed in the hope that it will be useful, 22f68a8342SChanwoo Choi * but WITHOUT ANY WARRANTY; without even the implied warranty of 23f68a8342SChanwoo Choi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24f68a8342SChanwoo Choi * GNU General Public License for more details. 25f68a8342SChanwoo Choi */ 26f68a8342SChanwoo Choi 27f68a8342SChanwoo Choi #include <linux/module.h> 28f68a8342SChanwoo Choi #include <linux/types.h> 29f68a8342SChanwoo Choi #include <linux/init.h> 30f68a8342SChanwoo Choi #include <linux/device.h> 31f68a8342SChanwoo Choi #include <linux/fs.h> 32f68a8342SChanwoo Choi #include <linux/err.h> 33f68a8342SChanwoo Choi #include <linux/extcon.h> 34f68a8342SChanwoo Choi #include <linux/of.h> 35f68a8342SChanwoo Choi #include <linux/slab.h> 36f68a8342SChanwoo Choi #include <linux/sysfs.h> 37f68a8342SChanwoo Choi 382a9de9c0SChanwoo Choi #define SUPPORTED_CABLE_MAX 32 392a9de9c0SChanwoo Choi #define CABLE_NAME_MAX 30 402a9de9c0SChanwoo Choi 41*55e4e2f1SChanwoo Choi struct __extcon_info { 42*55e4e2f1SChanwoo Choi unsigned int type; 43*55e4e2f1SChanwoo Choi unsigned int id; 44*55e4e2f1SChanwoo Choi const char *name; 45*55e4e2f1SChanwoo Choi 46*55e4e2f1SChanwoo Choi } extcon_info[] = { 47*55e4e2f1SChanwoo Choi [EXTCON_NONE] = { 48*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 49*55e4e2f1SChanwoo Choi .id = EXTCON_NONE, 50*55e4e2f1SChanwoo Choi .name = "NONE", 51*55e4e2f1SChanwoo Choi }, 5273b6ecdbSChanwoo Choi 538e9bc36dSChanwoo Choi /* USB external connector */ 54*55e4e2f1SChanwoo Choi [EXTCON_USB] = { 55*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 56*55e4e2f1SChanwoo Choi .id = EXTCON_USB, 57*55e4e2f1SChanwoo Choi .name = "USB", 58*55e4e2f1SChanwoo Choi }, 59*55e4e2f1SChanwoo Choi [EXTCON_USB_HOST] = { 60*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_USB, 61*55e4e2f1SChanwoo Choi .id = EXTCON_USB_HOST, 62*55e4e2f1SChanwoo Choi .name = "USB_HOST", 63*55e4e2f1SChanwoo Choi }, 648e9bc36dSChanwoo Choi 6511eecf91SChanwoo Choi /* Charging external connector */ 66*55e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SDP] = { 67*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 68*55e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SDP, 69*55e4e2f1SChanwoo Choi .name = "SDP", 70*55e4e2f1SChanwoo Choi }, 71*55e4e2f1SChanwoo Choi [EXTCON_CHG_USB_DCP] = { 72*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 73*55e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_DCP, 74*55e4e2f1SChanwoo Choi .name = "DCP", 75*55e4e2f1SChanwoo Choi }, 76*55e4e2f1SChanwoo Choi [EXTCON_CHG_USB_CDP] = { 77*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 78*55e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_CDP, 79*55e4e2f1SChanwoo Choi .name = "CDP", 80*55e4e2f1SChanwoo Choi }, 81*55e4e2f1SChanwoo Choi [EXTCON_CHG_USB_ACA] = { 82*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 83*55e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_ACA, 84*55e4e2f1SChanwoo Choi .name = "ACA", 85*55e4e2f1SChanwoo Choi }, 86*55e4e2f1SChanwoo Choi [EXTCON_CHG_USB_FAST] = { 87*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 88*55e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_FAST, 89*55e4e2f1SChanwoo Choi .name = "FAST-CHARGER", 90*55e4e2f1SChanwoo Choi }, 91*55e4e2f1SChanwoo Choi [EXTCON_CHG_USB_SLOW] = { 92*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, 93*55e4e2f1SChanwoo Choi .id = EXTCON_CHG_USB_SLOW, 94*55e4e2f1SChanwoo Choi .name = "SLOW-CHARGER", 95*55e4e2f1SChanwoo Choi }, 968e9bc36dSChanwoo Choi 9711eecf91SChanwoo Choi /* Jack external connector */ 98*55e4e2f1SChanwoo Choi [EXTCON_JACK_MICROPHONE] = { 99*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 100*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_MICROPHONE, 101*55e4e2f1SChanwoo Choi .name = "MICROPHONE", 102*55e4e2f1SChanwoo Choi }, 103*55e4e2f1SChanwoo Choi [EXTCON_JACK_HEADPHONE] = { 104*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 105*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_HEADPHONE, 106*55e4e2f1SChanwoo Choi .name = "HEADPHONE", 107*55e4e2f1SChanwoo Choi }, 108*55e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_IN] = { 109*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 110*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_IN, 111*55e4e2f1SChanwoo Choi .name = "LINE-IN", 112*55e4e2f1SChanwoo Choi }, 113*55e4e2f1SChanwoo Choi [EXTCON_JACK_LINE_OUT] = { 114*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 115*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_LINE_OUT, 116*55e4e2f1SChanwoo Choi .name = "LINE-OUT", 117*55e4e2f1SChanwoo Choi }, 118*55e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_IN] = { 119*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 120*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_IN, 121*55e4e2f1SChanwoo Choi .name = "VIDEO-IN", 122*55e4e2f1SChanwoo Choi }, 123*55e4e2f1SChanwoo Choi [EXTCON_JACK_VIDEO_OUT] = { 124*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 125*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_VIDEO_OUT, 126*55e4e2f1SChanwoo Choi .name = "VIDEO-OUT", 127*55e4e2f1SChanwoo Choi }, 128*55e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_IN] = { 129*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 130*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_IN, 131*55e4e2f1SChanwoo Choi .name = "SPDIF-IN", 132*55e4e2f1SChanwoo Choi }, 133*55e4e2f1SChanwoo Choi [EXTCON_JACK_SPDIF_OUT] = { 134*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_JACK, 135*55e4e2f1SChanwoo Choi .id = EXTCON_JACK_SPDIF_OUT, 136*55e4e2f1SChanwoo Choi .name = "SPDIF-OUT", 137*55e4e2f1SChanwoo Choi }, 1388e9bc36dSChanwoo Choi 13911eecf91SChanwoo Choi /* Display external connector */ 140*55e4e2f1SChanwoo Choi [EXTCON_DISP_HDMI] = { 141*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 142*55e4e2f1SChanwoo Choi .id = EXTCON_DISP_HDMI, 143*55e4e2f1SChanwoo Choi .name = "HDMI", 144*55e4e2f1SChanwoo Choi }, 145*55e4e2f1SChanwoo Choi [EXTCON_DISP_MHL] = { 146*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 147*55e4e2f1SChanwoo Choi .id = EXTCON_DISP_MHL, 148*55e4e2f1SChanwoo Choi .name = "MHL", 149*55e4e2f1SChanwoo Choi }, 150*55e4e2f1SChanwoo Choi [EXTCON_DISP_DVI] = { 151*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 152*55e4e2f1SChanwoo Choi .id = EXTCON_DISP_DVI, 153*55e4e2f1SChanwoo Choi .name = "DVI", 154*55e4e2f1SChanwoo Choi }, 155*55e4e2f1SChanwoo Choi [EXTCON_DISP_VGA] = { 156*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_DISP, 157*55e4e2f1SChanwoo Choi .id = EXTCON_DISP_VGA, 158*55e4e2f1SChanwoo Choi .name = "VGA", 159*55e4e2f1SChanwoo Choi }, 1608e9bc36dSChanwoo Choi 16111eecf91SChanwoo Choi /* Miscellaneous external connector */ 162*55e4e2f1SChanwoo Choi [EXTCON_DOCK] = { 163*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 164*55e4e2f1SChanwoo Choi .id = EXTCON_DOCK, 165*55e4e2f1SChanwoo Choi .name = "DOCK", 166*55e4e2f1SChanwoo Choi }, 167*55e4e2f1SChanwoo Choi [EXTCON_JIG] = { 168*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 169*55e4e2f1SChanwoo Choi .id = EXTCON_JIG, 170*55e4e2f1SChanwoo Choi .name = "JIG", 171*55e4e2f1SChanwoo Choi }, 172*55e4e2f1SChanwoo Choi [EXTCON_MECHANICAL] = { 173*55e4e2f1SChanwoo Choi .type = EXTCON_TYPE_MISC, 174*55e4e2f1SChanwoo Choi .id = EXTCON_MECHANICAL, 175*55e4e2f1SChanwoo Choi .name = "MECHANICAL", 176*55e4e2f1SChanwoo Choi }, 1778e9bc36dSChanwoo Choi 178*55e4e2f1SChanwoo Choi { /* sentinel */ } 179f68a8342SChanwoo Choi }; 180f68a8342SChanwoo Choi 18120f7b53dSChanwoo Choi /** 18220f7b53dSChanwoo Choi * struct extcon_cable - An internal data for each cable of extcon device. 18320f7b53dSChanwoo Choi * @edev: The extcon device 18420f7b53dSChanwoo Choi * @cable_index: Index of this cable in the edev 18520f7b53dSChanwoo Choi * @attr_g: Attribute group for the cable 18620f7b53dSChanwoo Choi * @attr_name: "name" sysfs entry 18720f7b53dSChanwoo Choi * @attr_state: "state" sysfs entry 18820f7b53dSChanwoo Choi * @attrs: Array pointing to attr_name and attr_state for attr_g 18920f7b53dSChanwoo Choi */ 19020f7b53dSChanwoo Choi struct extcon_cable { 19120f7b53dSChanwoo Choi struct extcon_dev *edev; 19220f7b53dSChanwoo Choi int cable_index; 19320f7b53dSChanwoo Choi 19420f7b53dSChanwoo Choi struct attribute_group attr_g; 19520f7b53dSChanwoo Choi struct device_attribute attr_name; 19620f7b53dSChanwoo Choi struct device_attribute attr_state; 19720f7b53dSChanwoo Choi 19820f7b53dSChanwoo Choi struct attribute *attrs[3]; /* to be fed to attr_g.attrs */ 19920f7b53dSChanwoo Choi }; 20020f7b53dSChanwoo Choi 201f68a8342SChanwoo Choi static struct class *extcon_class; 202f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 203f68a8342SChanwoo Choi static struct class_compat *switch_class; 204f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 205f68a8342SChanwoo Choi 206f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list); 207f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock); 208f68a8342SChanwoo Choi 209f68a8342SChanwoo Choi /** 210f68a8342SChanwoo Choi * check_mutually_exclusive - Check if new_state violates mutually_exclusive 211f68a8342SChanwoo Choi * condition. 212f68a8342SChanwoo Choi * @edev: the extcon device 213f68a8342SChanwoo Choi * @new_state: new cable attach status for @edev 214f68a8342SChanwoo Choi * 215f68a8342SChanwoo Choi * Returns 0 if nothing violates. Returns the index + 1 for the first 216f68a8342SChanwoo Choi * violated condition. 217f68a8342SChanwoo Choi */ 218f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) 219f68a8342SChanwoo Choi { 220f68a8342SChanwoo Choi int i = 0; 221f68a8342SChanwoo Choi 222f68a8342SChanwoo Choi if (!edev->mutually_exclusive) 223f68a8342SChanwoo Choi return 0; 224f68a8342SChanwoo Choi 225f68a8342SChanwoo Choi for (i = 0; edev->mutually_exclusive[i]; i++) { 226f68a8342SChanwoo Choi int weight; 227f68a8342SChanwoo Choi u32 correspondants = new_state & edev->mutually_exclusive[i]; 228f68a8342SChanwoo Choi 229f68a8342SChanwoo Choi /* calculate the total number of bits set */ 230f68a8342SChanwoo Choi weight = hweight32(correspondants); 231f68a8342SChanwoo Choi if (weight > 1) 232f68a8342SChanwoo Choi return i + 1; 233f68a8342SChanwoo Choi } 234f68a8342SChanwoo Choi 235f68a8342SChanwoo Choi return 0; 236f68a8342SChanwoo Choi } 237f68a8342SChanwoo Choi 23873b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id) 2392a9de9c0SChanwoo Choi { 2402a9de9c0SChanwoo Choi int i; 2412a9de9c0SChanwoo Choi 2422a9de9c0SChanwoo Choi /* Find the the index of extcon cable in edev->supported_cable */ 2432a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 2442a9de9c0SChanwoo Choi if (edev->supported_cable[i] == id) 2452a9de9c0SChanwoo Choi return i; 2462a9de9c0SChanwoo Choi } 2472a9de9c0SChanwoo Choi 2482a9de9c0SChanwoo Choi return -EINVAL; 2492a9de9c0SChanwoo Choi } 2502a9de9c0SChanwoo Choi 251046050f6SChanwoo Choi static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached) 252046050f6SChanwoo Choi { 253046050f6SChanwoo Choi if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) { 254f4513b06SHans de Goede *attached = ((new >> idx) & 0x1) ? true : false; 255046050f6SChanwoo Choi return true; 256046050f6SChanwoo Choi } 257046050f6SChanwoo Choi 258046050f6SChanwoo Choi return false; 259046050f6SChanwoo Choi } 260046050f6SChanwoo Choi 261f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr, 262f68a8342SChanwoo Choi char *buf) 263f68a8342SChanwoo Choi { 264f68a8342SChanwoo Choi int i, count = 0; 265f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 266f68a8342SChanwoo Choi 267f68a8342SChanwoo Choi if (edev->max_supported == 0) 268f68a8342SChanwoo Choi return sprintf(buf, "%u\n", edev->state); 269f68a8342SChanwoo Choi 2702a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 271f68a8342SChanwoo Choi count += sprintf(buf + count, "%s=%d\n", 272*55e4e2f1SChanwoo Choi extcon_info[edev->supported_cable[i]].name, 273f68a8342SChanwoo Choi !!(edev->state & (1 << i))); 274f68a8342SChanwoo Choi } 275f68a8342SChanwoo Choi 276f68a8342SChanwoo Choi return count; 277f68a8342SChanwoo Choi } 2785d5321e9SChanwoo Choi static DEVICE_ATTR_RO(state); 279f68a8342SChanwoo Choi 280f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr, 281f68a8342SChanwoo Choi char *buf) 282f68a8342SChanwoo Choi { 283f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 284f68a8342SChanwoo Choi 28571c3ffa5SChanwoo Choi return sprintf(buf, "%s\n", edev->name); 286f68a8342SChanwoo Choi } 287f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name); 288f68a8342SChanwoo Choi 289f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev, 290f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 291f68a8342SChanwoo Choi { 292f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 293f68a8342SChanwoo Choi attr_name); 2942a9de9c0SChanwoo Choi int i = cable->cable_index; 295f68a8342SChanwoo Choi 296f68a8342SChanwoo Choi return sprintf(buf, "%s\n", 297*55e4e2f1SChanwoo Choi extcon_info[cable->edev->supported_cable[i]].name); 298f68a8342SChanwoo Choi } 299f68a8342SChanwoo Choi 300f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev, 301f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 302f68a8342SChanwoo Choi { 303f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 304f68a8342SChanwoo Choi attr_state); 305f68a8342SChanwoo Choi 306be052cc8SRoger Quadros int i = cable->cable_index; 307be052cc8SRoger Quadros 308f68a8342SChanwoo Choi return sprintf(buf, "%d\n", 309f68a8342SChanwoo Choi extcon_get_cable_state_(cable->edev, 310be052cc8SRoger Quadros cable->edev->supported_cable[i])); 311f68a8342SChanwoo Choi } 312f68a8342SChanwoo Choi 313f68a8342SChanwoo Choi /** 314f68a8342SChanwoo Choi * extcon_update_state() - Update the cable attach states of the extcon device 315f68a8342SChanwoo Choi * only for the masked bits. 316f68a8342SChanwoo Choi * @edev: the extcon device 317f68a8342SChanwoo Choi * @mask: the bit mask to designate updated bits. 318f68a8342SChanwoo Choi * @state: new cable attach status for @edev 319f68a8342SChanwoo Choi * 320f68a8342SChanwoo Choi * Changing the state sends uevent with environment variable containing 321f68a8342SChanwoo Choi * the name of extcon device (envp[0]) and the state output (envp[1]). 322f68a8342SChanwoo Choi * Tizen uses this format for extcon device to get events from ports. 323f68a8342SChanwoo Choi * Android uses this format as well. 324f68a8342SChanwoo Choi * 325f68a8342SChanwoo Choi * Note that the notifier provides which bits are changed in the state 326f68a8342SChanwoo Choi * variable with the val parameter (second) to the callback. 327f68a8342SChanwoo Choi */ 328912465bcSChanwoo Choi static int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) 329f68a8342SChanwoo Choi { 330f68a8342SChanwoo Choi char name_buf[120]; 331f68a8342SChanwoo Choi char state_buf[120]; 332f68a8342SChanwoo Choi char *prop_buf; 333f68a8342SChanwoo Choi char *envp[3]; 334f68a8342SChanwoo Choi int env_offset = 0; 335f68a8342SChanwoo Choi int length; 336046050f6SChanwoo Choi int index; 337f68a8342SChanwoo Choi unsigned long flags; 338046050f6SChanwoo Choi bool attached; 339f68a8342SChanwoo Choi 3407eae43aeSChanwoo Choi if (!edev) 3417eae43aeSChanwoo Choi return -EINVAL; 3427eae43aeSChanwoo Choi 343f68a8342SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 344f68a8342SChanwoo Choi 345f68a8342SChanwoo Choi if (edev->state != ((edev->state & ~mask) | (state & mask))) { 346f7a89811SRoger Quadros u32 old_state; 347f7a89811SRoger Quadros 348f68a8342SChanwoo Choi if (check_mutually_exclusive(edev, (edev->state & ~mask) | 349f68a8342SChanwoo Choi (state & mask))) { 350f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 351f68a8342SChanwoo Choi return -EPERM; 352f68a8342SChanwoo Choi } 353f68a8342SChanwoo Choi 354f7a89811SRoger Quadros old_state = edev->state; 355f68a8342SChanwoo Choi edev->state &= ~mask; 356f68a8342SChanwoo Choi edev->state |= state & mask; 357f68a8342SChanwoo Choi 358f7a89811SRoger Quadros for (index = 0; index < edev->max_supported; index++) { 359f7a89811SRoger Quadros if (is_extcon_changed(old_state, edev->state, index, 360f7a89811SRoger Quadros &attached)) 361f7a89811SRoger Quadros raw_notifier_call_chain(&edev->nh[index], 362f7a89811SRoger Quadros attached, edev); 363f7a89811SRoger Quadros } 364f7a89811SRoger Quadros 365f68a8342SChanwoo Choi /* This could be in interrupt handler */ 366f68a8342SChanwoo Choi prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); 367f68a8342SChanwoo Choi if (prop_buf) { 368f68a8342SChanwoo Choi length = name_show(&edev->dev, NULL, prop_buf); 369f68a8342SChanwoo Choi if (length > 0) { 370f68a8342SChanwoo Choi if (prop_buf[length - 1] == '\n') 371f68a8342SChanwoo Choi prop_buf[length - 1] = 0; 372f68a8342SChanwoo Choi snprintf(name_buf, sizeof(name_buf), 373f68a8342SChanwoo Choi "NAME=%s", prop_buf); 374f68a8342SChanwoo Choi envp[env_offset++] = name_buf; 375f68a8342SChanwoo Choi } 376f68a8342SChanwoo Choi length = state_show(&edev->dev, NULL, prop_buf); 377f68a8342SChanwoo Choi if (length > 0) { 378f68a8342SChanwoo Choi if (prop_buf[length - 1] == '\n') 379f68a8342SChanwoo Choi prop_buf[length - 1] = 0; 380f68a8342SChanwoo Choi snprintf(state_buf, sizeof(state_buf), 381f68a8342SChanwoo Choi "STATE=%s", prop_buf); 382f68a8342SChanwoo Choi envp[env_offset++] = state_buf; 383f68a8342SChanwoo Choi } 384f68a8342SChanwoo Choi envp[env_offset] = NULL; 385f68a8342SChanwoo Choi /* Unlock early before uevent */ 386f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 387f68a8342SChanwoo Choi 388f68a8342SChanwoo Choi kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); 389f68a8342SChanwoo Choi free_page((unsigned long)prop_buf); 390f68a8342SChanwoo Choi } else { 391f68a8342SChanwoo Choi /* Unlock early before uevent */ 392f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 393f68a8342SChanwoo Choi 394f68a8342SChanwoo Choi dev_err(&edev->dev, "out of memory in extcon_set_state\n"); 395f68a8342SChanwoo Choi kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); 396f68a8342SChanwoo Choi } 397f68a8342SChanwoo Choi } else { 398f68a8342SChanwoo Choi /* No changes */ 399f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 400f68a8342SChanwoo Choi } 401f68a8342SChanwoo Choi 402f68a8342SChanwoo Choi return 0; 403f68a8342SChanwoo Choi } 404f68a8342SChanwoo Choi 405f68a8342SChanwoo Choi /** 406f68a8342SChanwoo Choi * extcon_get_cable_state_() - Get the status of a specific cable. 407f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 4082a9de9c0SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 409f68a8342SChanwoo Choi */ 41073b6ecdbSChanwoo Choi int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id) 411f68a8342SChanwoo Choi { 4122a9de9c0SChanwoo Choi int index; 4132a9de9c0SChanwoo Choi 4147eae43aeSChanwoo Choi if (!edev) 4157eae43aeSChanwoo Choi return -EINVAL; 4167eae43aeSChanwoo Choi 4172a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 4182a9de9c0SChanwoo Choi if (index < 0) 4192a9de9c0SChanwoo Choi return index; 4202a9de9c0SChanwoo Choi 4212a9de9c0SChanwoo Choi if (edev->max_supported && edev->max_supported <= index) 422f68a8342SChanwoo Choi return -EINVAL; 423f68a8342SChanwoo Choi 424f68a8342SChanwoo Choi return !!(edev->state & (1 << index)); 425f68a8342SChanwoo Choi } 426f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_cable_state_); 427f68a8342SChanwoo Choi 428f68a8342SChanwoo Choi /** 429f68a8342SChanwoo Choi * extcon_set_cable_state_() - Set the status of a specific cable. 430f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 4312a9de9c0SChanwoo Choi * @id: the unique id of each external connector 4322a9de9c0SChanwoo Choi * in extcon enumeration. 4332a9de9c0SChanwoo Choi * @state: the new cable status. The default semantics is 434f68a8342SChanwoo Choi * true: attached / false: detached. 435f68a8342SChanwoo Choi */ 43673b6ecdbSChanwoo Choi int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id, 4372a9de9c0SChanwoo Choi bool cable_state) 438f68a8342SChanwoo Choi { 439f68a8342SChanwoo Choi u32 state; 4402a9de9c0SChanwoo Choi int index; 441f68a8342SChanwoo Choi 4427eae43aeSChanwoo Choi if (!edev) 4437eae43aeSChanwoo Choi return -EINVAL; 4447eae43aeSChanwoo Choi 4452a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 4462a9de9c0SChanwoo Choi if (index < 0) 4472a9de9c0SChanwoo Choi return index; 4482a9de9c0SChanwoo Choi 4492a9de9c0SChanwoo Choi if (edev->max_supported && edev->max_supported <= index) 450f68a8342SChanwoo Choi return -EINVAL; 451f68a8342SChanwoo Choi 452f68a8342SChanwoo Choi state = cable_state ? (1 << index) : 0; 453f68a8342SChanwoo Choi return extcon_update_state(edev, 1 << index, state); 454f68a8342SChanwoo Choi } 455f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_cable_state_); 456f68a8342SChanwoo Choi 457f68a8342SChanwoo Choi /** 458f68a8342SChanwoo Choi * extcon_get_extcon_dev() - Get the extcon device instance from the name 459f68a8342SChanwoo Choi * @extcon_name: The extcon name provided with extcon_dev_register() 460f68a8342SChanwoo Choi */ 461f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) 462f68a8342SChanwoo Choi { 463f68a8342SChanwoo Choi struct extcon_dev *sd; 464f68a8342SChanwoo Choi 4657eae43aeSChanwoo Choi if (!extcon_name) 4667eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 4677eae43aeSChanwoo Choi 468f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 469f68a8342SChanwoo Choi list_for_each_entry(sd, &extcon_dev_list, entry) { 470f68a8342SChanwoo Choi if (!strcmp(sd->name, extcon_name)) 471f68a8342SChanwoo Choi goto out; 472f68a8342SChanwoo Choi } 473f68a8342SChanwoo Choi sd = NULL; 474f68a8342SChanwoo Choi out: 475f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 476f68a8342SChanwoo Choi return sd; 477f68a8342SChanwoo Choi } 478f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); 479f68a8342SChanwoo Choi 480f68a8342SChanwoo Choi /** 481f68a8342SChanwoo Choi * extcon_register_notifier() - Register a notifiee to get notified by 482f68a8342SChanwoo Choi * any attach status changes from the extcon. 483046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 484046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 485f68a8342SChanwoo Choi * @nb: a notifier block to be registered. 486f68a8342SChanwoo Choi * 487f68a8342SChanwoo Choi * Note that the second parameter given to the callback of nb (val) is 488f68a8342SChanwoo Choi * "old_state", not the current state. The current state can be retrieved 489f68a8342SChanwoo Choi * by looking at the third pameter (edev pointer)'s state value. 490f68a8342SChanwoo Choi */ 49173b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, 492f68a8342SChanwoo Choi struct notifier_block *nb) 493f68a8342SChanwoo Choi { 49466bee35fSHans de Goede unsigned long flags; 4952c8116a1SManinder Singh int ret, idx = -EINVAL; 496046050f6SChanwoo Choi 497830ae442SChanwoo Choi if (!nb) 4987eae43aeSChanwoo Choi return -EINVAL; 4997eae43aeSChanwoo Choi 500830ae442SChanwoo Choi if (edev) { 501046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 502a05f44c8SStephen Boyd if (idx < 0) 503a05f44c8SStephen Boyd return idx; 50466bee35fSHans de Goede 50566bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 506046050f6SChanwoo Choi ret = raw_notifier_chain_register(&edev->nh[idx], nb); 50766bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 508830ae442SChanwoo Choi } else { 509830ae442SChanwoo Choi struct extcon_dev *extd; 510830ae442SChanwoo Choi 511830ae442SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 512830ae442SChanwoo Choi list_for_each_entry(extd, &extcon_dev_list, entry) { 513830ae442SChanwoo Choi idx = find_cable_index_by_id(extd, id); 514830ae442SChanwoo Choi if (idx >= 0) 515830ae442SChanwoo Choi break; 516830ae442SChanwoo Choi } 517830ae442SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 518830ae442SChanwoo Choi 519830ae442SChanwoo Choi if (idx >= 0) { 520830ae442SChanwoo Choi edev = extd; 521830ae442SChanwoo Choi return extcon_register_notifier(extd, id, nb); 522830ae442SChanwoo Choi } else { 523830ae442SChanwoo Choi ret = -ENODEV; 524830ae442SChanwoo Choi } 525830ae442SChanwoo Choi } 52666bee35fSHans de Goede 52766bee35fSHans de Goede return ret; 528f68a8342SChanwoo Choi } 529f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier); 530f68a8342SChanwoo Choi 531f68a8342SChanwoo Choi /** 532f68a8342SChanwoo Choi * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. 533046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 534046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 535046050f6SChanwoo Choi * @nb: a notifier block to be registered. 536f68a8342SChanwoo Choi */ 53773b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, 538f68a8342SChanwoo Choi struct notifier_block *nb) 539f68a8342SChanwoo Choi { 54066bee35fSHans de Goede unsigned long flags; 541046050f6SChanwoo Choi int ret, idx; 542046050f6SChanwoo Choi 5437eae43aeSChanwoo Choi if (!edev || !nb) 5447eae43aeSChanwoo Choi return -EINVAL; 5457eae43aeSChanwoo Choi 546046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 547a05f44c8SStephen Boyd if (idx < 0) 548a05f44c8SStephen Boyd return idx; 54966bee35fSHans de Goede 55066bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 551046050f6SChanwoo Choi ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); 55266bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 55366bee35fSHans de Goede 55466bee35fSHans de Goede return ret; 555f68a8342SChanwoo Choi } 556f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier); 557f68a8342SChanwoo Choi 558f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = { 559f68a8342SChanwoo Choi &dev_attr_state.attr, 560f68a8342SChanwoo Choi &dev_attr_name.attr, 561f68a8342SChanwoo Choi NULL, 562f68a8342SChanwoo Choi }; 563f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon); 564f68a8342SChanwoo Choi 565f68a8342SChanwoo Choi static int create_extcon_class(void) 566f68a8342SChanwoo Choi { 567f68a8342SChanwoo Choi if (!extcon_class) { 568f68a8342SChanwoo Choi extcon_class = class_create(THIS_MODULE, "extcon"); 569f68a8342SChanwoo Choi if (IS_ERR(extcon_class)) 570f68a8342SChanwoo Choi return PTR_ERR(extcon_class); 571f68a8342SChanwoo Choi extcon_class->dev_groups = extcon_groups; 572f68a8342SChanwoo Choi 573f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 574f68a8342SChanwoo Choi switch_class = class_compat_register("switch"); 575f68a8342SChanwoo Choi if (WARN(!switch_class, "cannot allocate")) 576f68a8342SChanwoo Choi return -ENOMEM; 577f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 578f68a8342SChanwoo Choi } 579f68a8342SChanwoo Choi 580f68a8342SChanwoo Choi return 0; 581f68a8342SChanwoo Choi } 582f68a8342SChanwoo Choi 583f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev) 584f68a8342SChanwoo Choi { 585f68a8342SChanwoo Choi } 586f68a8342SChanwoo Choi 587f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive"; 588f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev) 589f68a8342SChanwoo Choi { 590f68a8342SChanwoo Choi } 591f68a8342SChanwoo Choi 592f68a8342SChanwoo Choi /* 593f68a8342SChanwoo Choi * extcon_dev_allocate() - Allocate the memory of extcon device. 5942a9de9c0SChanwoo Choi * @supported_cable: Array of supported extcon ending with EXTCON_NONE. 595f68a8342SChanwoo Choi * If supported_cable is NULL, cable name related APIs 596f68a8342SChanwoo Choi * are disabled. 597f68a8342SChanwoo Choi * 598f68a8342SChanwoo Choi * This function allocates the memory for extcon device without allocating 599f68a8342SChanwoo Choi * memory in each extcon provider driver and initialize default setting for 600f68a8342SChanwoo Choi * extcon device. 601f68a8342SChanwoo Choi * 602f68a8342SChanwoo Choi * Return the pointer of extcon device if success or ERR_PTR(err) if fail 603f68a8342SChanwoo Choi */ 60473b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) 605f68a8342SChanwoo Choi { 606f68a8342SChanwoo Choi struct extcon_dev *edev; 607f68a8342SChanwoo Choi 6087eae43aeSChanwoo Choi if (!supported_cable) 6097eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 6107eae43aeSChanwoo Choi 611f68a8342SChanwoo Choi edev = kzalloc(sizeof(*edev), GFP_KERNEL); 612f68a8342SChanwoo Choi if (!edev) 613f68a8342SChanwoo Choi return ERR_PTR(-ENOMEM); 614f68a8342SChanwoo Choi 615f68a8342SChanwoo Choi edev->max_supported = 0; 616f68a8342SChanwoo Choi edev->supported_cable = supported_cable; 617f68a8342SChanwoo Choi 618f68a8342SChanwoo Choi return edev; 619f68a8342SChanwoo Choi } 620f68a8342SChanwoo Choi 621f68a8342SChanwoo Choi /* 622f68a8342SChanwoo Choi * extcon_dev_free() - Free the memory of extcon device. 623f68a8342SChanwoo Choi * @edev: the extcon device to free 624f68a8342SChanwoo Choi */ 625f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev) 626f68a8342SChanwoo Choi { 627f68a8342SChanwoo Choi kfree(edev); 628f68a8342SChanwoo Choi } 629f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free); 630f68a8342SChanwoo Choi 631f68a8342SChanwoo Choi /** 632f68a8342SChanwoo Choi * extcon_dev_register() - Register a new extcon device 633f68a8342SChanwoo Choi * @edev : the new extcon device (should be allocated before calling) 634f68a8342SChanwoo Choi * 635f68a8342SChanwoo Choi * Among the members of edev struct, please set the "user initializing data" 636f68a8342SChanwoo Choi * in any case and set the "optional callbacks" if required. However, please 637f68a8342SChanwoo Choi * do not set the values of "internal data", which are initialized by 638f68a8342SChanwoo Choi * this function. 639f68a8342SChanwoo Choi */ 640f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev) 641f68a8342SChanwoo Choi { 642f68a8342SChanwoo Choi int ret, index = 0; 64371c3ffa5SChanwoo Choi static atomic_t edev_no = ATOMIC_INIT(-1); 644f68a8342SChanwoo Choi 645f68a8342SChanwoo Choi if (!extcon_class) { 646f68a8342SChanwoo Choi ret = create_extcon_class(); 647f68a8342SChanwoo Choi if (ret < 0) 648f68a8342SChanwoo Choi return ret; 649f68a8342SChanwoo Choi } 650f68a8342SChanwoo Choi 6517eae43aeSChanwoo Choi if (!edev || !edev->supported_cable) 6522a9de9c0SChanwoo Choi return -EINVAL; 653f68a8342SChanwoo Choi 6542a9de9c0SChanwoo Choi for (; edev->supported_cable[index] != EXTCON_NONE; index++); 6552a9de9c0SChanwoo Choi 6562a9de9c0SChanwoo Choi edev->max_supported = index; 657f68a8342SChanwoo Choi if (index > SUPPORTED_CABLE_MAX) { 6582a9de9c0SChanwoo Choi dev_err(&edev->dev, 6592a9de9c0SChanwoo Choi "exceed the maximum number of supported cables\n"); 660f68a8342SChanwoo Choi return -EINVAL; 661f68a8342SChanwoo Choi } 662f68a8342SChanwoo Choi 663f68a8342SChanwoo Choi edev->dev.class = extcon_class; 664f68a8342SChanwoo Choi edev->dev.release = extcon_dev_release; 665f68a8342SChanwoo Choi 66671c3ffa5SChanwoo Choi edev->name = dev_name(edev->dev.parent); 667f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(edev->name)) { 668f68a8342SChanwoo Choi dev_err(&edev->dev, 669f68a8342SChanwoo Choi "extcon device name is null\n"); 670f68a8342SChanwoo Choi return -EINVAL; 671f68a8342SChanwoo Choi } 67271c3ffa5SChanwoo Choi dev_set_name(&edev->dev, "extcon%lu", 67371c3ffa5SChanwoo Choi (unsigned long)atomic_inc_return(&edev_no)); 674f68a8342SChanwoo Choi 675f68a8342SChanwoo Choi if (edev->max_supported) { 676f68a8342SChanwoo Choi char buf[10]; 677f68a8342SChanwoo Choi char *str; 678f68a8342SChanwoo Choi struct extcon_cable *cable; 679f68a8342SChanwoo Choi 680f68a8342SChanwoo Choi edev->cables = kzalloc(sizeof(struct extcon_cable) * 681f68a8342SChanwoo Choi edev->max_supported, GFP_KERNEL); 682f68a8342SChanwoo Choi if (!edev->cables) { 683f68a8342SChanwoo Choi ret = -ENOMEM; 684f68a8342SChanwoo Choi goto err_sysfs_alloc; 685f68a8342SChanwoo Choi } 686f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) { 687f68a8342SChanwoo Choi cable = &edev->cables[index]; 688f68a8342SChanwoo Choi 689f68a8342SChanwoo Choi snprintf(buf, 10, "cable.%d", index); 690f68a8342SChanwoo Choi str = kzalloc(sizeof(char) * (strlen(buf) + 1), 691f68a8342SChanwoo Choi GFP_KERNEL); 692f68a8342SChanwoo Choi if (!str) { 693f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 694f68a8342SChanwoo Choi cable = &edev->cables[index]; 695f68a8342SChanwoo Choi kfree(cable->attr_g.name); 696f68a8342SChanwoo Choi } 697f68a8342SChanwoo Choi ret = -ENOMEM; 698f68a8342SChanwoo Choi 699f68a8342SChanwoo Choi goto err_alloc_cables; 700f68a8342SChanwoo Choi } 701f68a8342SChanwoo Choi strcpy(str, buf); 702f68a8342SChanwoo Choi 703f68a8342SChanwoo Choi cable->edev = edev; 704f68a8342SChanwoo Choi cable->cable_index = index; 705f68a8342SChanwoo Choi cable->attrs[0] = &cable->attr_name.attr; 706f68a8342SChanwoo Choi cable->attrs[1] = &cable->attr_state.attr; 707f68a8342SChanwoo Choi cable->attrs[2] = NULL; 708f68a8342SChanwoo Choi cable->attr_g.name = str; 709f68a8342SChanwoo Choi cable->attr_g.attrs = cable->attrs; 710f68a8342SChanwoo Choi 711f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_name.attr); 712f68a8342SChanwoo Choi cable->attr_name.attr.name = "name"; 713f68a8342SChanwoo Choi cable->attr_name.attr.mode = 0444; 714f68a8342SChanwoo Choi cable->attr_name.show = cable_name_show; 715f68a8342SChanwoo Choi 716f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_state.attr); 717f68a8342SChanwoo Choi cable->attr_state.attr.name = "state"; 718f68a8342SChanwoo Choi cable->attr_state.attr.mode = 0444; 719f68a8342SChanwoo Choi cable->attr_state.show = cable_state_show; 720f68a8342SChanwoo Choi } 721f68a8342SChanwoo Choi } 722f68a8342SChanwoo Choi 723f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 724f68a8342SChanwoo Choi char buf[80]; 725f68a8342SChanwoo Choi char *name; 726f68a8342SChanwoo Choi 727f68a8342SChanwoo Choi /* Count the size of mutually_exclusive array */ 728f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 729f68a8342SChanwoo Choi ; 730f68a8342SChanwoo Choi 731f68a8342SChanwoo Choi edev->attrs_muex = kzalloc(sizeof(struct attribute *) * 732f68a8342SChanwoo Choi (index + 1), GFP_KERNEL); 733f68a8342SChanwoo Choi if (!edev->attrs_muex) { 734f68a8342SChanwoo Choi ret = -ENOMEM; 735f68a8342SChanwoo Choi goto err_muex; 736f68a8342SChanwoo Choi } 737f68a8342SChanwoo Choi 738f68a8342SChanwoo Choi edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * 739f68a8342SChanwoo Choi index, GFP_KERNEL); 740f68a8342SChanwoo Choi if (!edev->d_attrs_muex) { 741f68a8342SChanwoo Choi ret = -ENOMEM; 742f68a8342SChanwoo Choi kfree(edev->attrs_muex); 743f68a8342SChanwoo Choi goto err_muex; 744f68a8342SChanwoo Choi } 745f68a8342SChanwoo Choi 746f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) { 747f68a8342SChanwoo Choi sprintf(buf, "0x%x", edev->mutually_exclusive[index]); 748f68a8342SChanwoo Choi name = kzalloc(sizeof(char) * (strlen(buf) + 1), 749f68a8342SChanwoo Choi GFP_KERNEL); 750f68a8342SChanwoo Choi if (!name) { 751f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 752f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr. 753f68a8342SChanwoo Choi name); 754f68a8342SChanwoo Choi } 755f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 756f68a8342SChanwoo Choi kfree(edev->attrs_muex); 757f68a8342SChanwoo Choi ret = -ENOMEM; 758f68a8342SChanwoo Choi goto err_muex; 759f68a8342SChanwoo Choi } 760f68a8342SChanwoo Choi strcpy(name, buf); 761f68a8342SChanwoo Choi sysfs_attr_init(&edev->d_attrs_muex[index].attr); 762f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.name = name; 763f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.mode = 0000; 764f68a8342SChanwoo Choi edev->attrs_muex[index] = &edev->d_attrs_muex[index] 765f68a8342SChanwoo Choi .attr; 766f68a8342SChanwoo Choi } 767f68a8342SChanwoo Choi edev->attr_g_muex.name = muex_name; 768f68a8342SChanwoo Choi edev->attr_g_muex.attrs = edev->attrs_muex; 769f68a8342SChanwoo Choi 770f68a8342SChanwoo Choi } 771f68a8342SChanwoo Choi 772f68a8342SChanwoo Choi if (edev->max_supported) { 773f68a8342SChanwoo Choi edev->extcon_dev_type.groups = 774f68a8342SChanwoo Choi kzalloc(sizeof(struct attribute_group *) * 775f68a8342SChanwoo Choi (edev->max_supported + 2), GFP_KERNEL); 776f68a8342SChanwoo Choi if (!edev->extcon_dev_type.groups) { 777f68a8342SChanwoo Choi ret = -ENOMEM; 778f68a8342SChanwoo Choi goto err_alloc_groups; 779f68a8342SChanwoo Choi } 780f68a8342SChanwoo Choi 781f68a8342SChanwoo Choi edev->extcon_dev_type.name = dev_name(&edev->dev); 782f68a8342SChanwoo Choi edev->extcon_dev_type.release = dummy_sysfs_dev_release; 783f68a8342SChanwoo Choi 784f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 785f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 786f68a8342SChanwoo Choi &edev->cables[index].attr_g; 787f68a8342SChanwoo Choi if (edev->mutually_exclusive) 788f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 789f68a8342SChanwoo Choi &edev->attr_g_muex; 790f68a8342SChanwoo Choi 791f68a8342SChanwoo Choi edev->dev.type = &edev->extcon_dev_type; 792f68a8342SChanwoo Choi } 793f68a8342SChanwoo Choi 794f68a8342SChanwoo Choi ret = device_register(&edev->dev); 795f68a8342SChanwoo Choi if (ret) { 796f68a8342SChanwoo Choi put_device(&edev->dev); 797f68a8342SChanwoo Choi goto err_dev; 798f68a8342SChanwoo Choi } 799f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 800f68a8342SChanwoo Choi if (switch_class) 801f68a8342SChanwoo Choi ret = class_compat_create_link(switch_class, &edev->dev, NULL); 802f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 803f68a8342SChanwoo Choi 804f68a8342SChanwoo Choi spin_lock_init(&edev->lock); 805f68a8342SChanwoo Choi 806046050f6SChanwoo Choi edev->nh = devm_kzalloc(&edev->dev, 807046050f6SChanwoo Choi sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL); 808046050f6SChanwoo Choi if (!edev->nh) { 809046050f6SChanwoo Choi ret = -ENOMEM; 810046050f6SChanwoo Choi goto err_dev; 811046050f6SChanwoo Choi } 812046050f6SChanwoo Choi 813046050f6SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 814046050f6SChanwoo Choi RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); 815f68a8342SChanwoo Choi 816f68a8342SChanwoo Choi dev_set_drvdata(&edev->dev, edev); 817f68a8342SChanwoo Choi edev->state = 0; 818f68a8342SChanwoo Choi 819f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 820f68a8342SChanwoo Choi list_add(&edev->entry, &extcon_dev_list); 821f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 822f68a8342SChanwoo Choi 823f68a8342SChanwoo Choi return 0; 824f68a8342SChanwoo Choi 825f68a8342SChanwoo Choi err_dev: 826f68a8342SChanwoo Choi if (edev->max_supported) 827f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 828f68a8342SChanwoo Choi err_alloc_groups: 829f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 830f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 831f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 832f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 833f68a8342SChanwoo Choi kfree(edev->attrs_muex); 834f68a8342SChanwoo Choi } 835f68a8342SChanwoo Choi err_muex: 836f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 837f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 838f68a8342SChanwoo Choi err_alloc_cables: 839f68a8342SChanwoo Choi if (edev->max_supported) 840f68a8342SChanwoo Choi kfree(edev->cables); 841f68a8342SChanwoo Choi err_sysfs_alloc: 842f68a8342SChanwoo Choi return ret; 843f68a8342SChanwoo Choi } 844f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register); 845f68a8342SChanwoo Choi 846f68a8342SChanwoo Choi /** 847f68a8342SChanwoo Choi * extcon_dev_unregister() - Unregister the extcon device. 848f68a8342SChanwoo Choi * @edev: the extcon device instance to be unregistered. 849f68a8342SChanwoo Choi * 850f68a8342SChanwoo Choi * Note that this does not call kfree(edev) because edev was not allocated 851f68a8342SChanwoo Choi * by this class. 852f68a8342SChanwoo Choi */ 853f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev) 854f68a8342SChanwoo Choi { 855f68a8342SChanwoo Choi int index; 856f68a8342SChanwoo Choi 8577eae43aeSChanwoo Choi if (!edev) 8587eae43aeSChanwoo Choi return; 8597eae43aeSChanwoo Choi 860f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 861f68a8342SChanwoo Choi list_del(&edev->entry); 862f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 863f68a8342SChanwoo Choi 864f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(get_device(&edev->dev))) { 865f68a8342SChanwoo Choi dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", 866f68a8342SChanwoo Choi dev_name(&edev->dev)); 867f68a8342SChanwoo Choi return; 868f68a8342SChanwoo Choi } 869f68a8342SChanwoo Choi 870f68a8342SChanwoo Choi device_unregister(&edev->dev); 871f68a8342SChanwoo Choi 872f68a8342SChanwoo Choi if (edev->mutually_exclusive && edev->max_supported) { 873f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; 874f68a8342SChanwoo Choi index++) 875f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 876f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 877f68a8342SChanwoo Choi kfree(edev->attrs_muex); 878f68a8342SChanwoo Choi } 879f68a8342SChanwoo Choi 880f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 881f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 882f68a8342SChanwoo Choi 883f68a8342SChanwoo Choi if (edev->max_supported) { 884f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 885f68a8342SChanwoo Choi kfree(edev->cables); 886f68a8342SChanwoo Choi } 887f68a8342SChanwoo Choi 888f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 889f68a8342SChanwoo Choi if (switch_class) 890f68a8342SChanwoo Choi class_compat_remove_link(switch_class, &edev->dev, NULL); 891f68a8342SChanwoo Choi #endif 892f68a8342SChanwoo Choi put_device(&edev->dev); 893f68a8342SChanwoo Choi } 894f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister); 895f68a8342SChanwoo Choi 896f68a8342SChanwoo Choi #ifdef CONFIG_OF 897f68a8342SChanwoo Choi /* 898f68a8342SChanwoo Choi * extcon_get_edev_by_phandle - Get the extcon device from devicetree 899f68a8342SChanwoo Choi * @dev - instance to the given device 900f68a8342SChanwoo Choi * @index - index into list of extcon_dev 901f68a8342SChanwoo Choi * 902f68a8342SChanwoo Choi * return the instance of extcon device 903f68a8342SChanwoo Choi */ 904f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 905f68a8342SChanwoo Choi { 906f68a8342SChanwoo Choi struct device_node *node; 907f68a8342SChanwoo Choi struct extcon_dev *edev; 908f68a8342SChanwoo Choi 9097eae43aeSChanwoo Choi if (!dev) 9107eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 9117eae43aeSChanwoo Choi 912f68a8342SChanwoo Choi if (!dev->of_node) { 913e8752b7aSStephen Boyd dev_dbg(dev, "device does not have a device node entry\n"); 914f68a8342SChanwoo Choi return ERR_PTR(-EINVAL); 915f68a8342SChanwoo Choi } 916f68a8342SChanwoo Choi 917f68a8342SChanwoo Choi node = of_parse_phandle(dev->of_node, "extcon", index); 918f68a8342SChanwoo Choi if (!node) { 919e8752b7aSStephen Boyd dev_dbg(dev, "failed to get phandle in %s node\n", 920f68a8342SChanwoo Choi dev->of_node->full_name); 921f68a8342SChanwoo Choi return ERR_PTR(-ENODEV); 922f68a8342SChanwoo Choi } 923f68a8342SChanwoo Choi 924f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 925f68a8342SChanwoo Choi list_for_each_entry(edev, &extcon_dev_list, entry) { 926f68a8342SChanwoo Choi if (edev->dev.parent && edev->dev.parent->of_node == node) { 927f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 9285d5c4c13SPeter Chen of_node_put(node); 929f68a8342SChanwoo Choi return edev; 930f68a8342SChanwoo Choi } 931f68a8342SChanwoo Choi } 932f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 9335d5c4c13SPeter Chen of_node_put(node); 934f68a8342SChanwoo Choi 935f68a8342SChanwoo Choi return ERR_PTR(-EPROBE_DEFER); 936f68a8342SChanwoo Choi } 937f68a8342SChanwoo Choi #else 938f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 939f68a8342SChanwoo Choi { 940f68a8342SChanwoo Choi return ERR_PTR(-ENOSYS); 941f68a8342SChanwoo Choi } 942f68a8342SChanwoo Choi #endif /* CONFIG_OF */ 943f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); 944f68a8342SChanwoo Choi 945707d7550SChanwoo Choi /** 946707d7550SChanwoo Choi * extcon_get_edev_name() - Get the name of the extcon device. 947707d7550SChanwoo Choi * @edev: the extcon device 948707d7550SChanwoo Choi */ 949707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev) 950707d7550SChanwoo Choi { 951707d7550SChanwoo Choi return !edev ? NULL : edev->name; 952707d7550SChanwoo Choi } 953707d7550SChanwoo Choi 954f68a8342SChanwoo Choi static int __init extcon_class_init(void) 955f68a8342SChanwoo Choi { 956f68a8342SChanwoo Choi return create_extcon_class(); 957f68a8342SChanwoo Choi } 958f68a8342SChanwoo Choi module_init(extcon_class_init); 959f68a8342SChanwoo Choi 960f68a8342SChanwoo Choi static void __exit extcon_class_exit(void) 961f68a8342SChanwoo Choi { 962f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 963f68a8342SChanwoo Choi class_compat_unregister(switch_class); 964f68a8342SChanwoo Choi #endif 965f68a8342SChanwoo Choi class_destroy(extcon_class); 966f68a8342SChanwoo Choi } 967f68a8342SChanwoo Choi module_exit(extcon_class_exit); 968f68a8342SChanwoo Choi 9692a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <[email protected]>"); 970f68a8342SChanwoo Choi MODULE_AUTHOR("Mike Lockwood <[email protected]>"); 971f68a8342SChanwoo Choi MODULE_AUTHOR("Donggeun Kim <[email protected]>"); 972f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <[email protected]>"); 973f68a8342SChanwoo Choi MODULE_DESCRIPTION("External connector (extcon) class driver"); 974f68a8342SChanwoo Choi MODULE_LICENSE("GPL"); 975