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 412a9de9c0SChanwoo Choi static const char *extcon_name[] = { 4273b6ecdbSChanwoo Choi [EXTCON_NONE] = "NONE", 4373b6ecdbSChanwoo Choi 448e9bc36dSChanwoo Choi /* USB external connector */ 45f68a8342SChanwoo Choi [EXTCON_USB] = "USB", 468e9bc36dSChanwoo Choi [EXTCON_USB_HOST] = "USB-HOST", 478e9bc36dSChanwoo Choi 4811eecf91SChanwoo Choi /* Charging external connector */ 4911eecf91SChanwoo Choi [EXTCON_CHG_USB_SDP] = "SDP", 5011eecf91SChanwoo Choi [EXTCON_CHG_USB_DCP] = "DCP", 5111eecf91SChanwoo Choi [EXTCON_CHG_USB_CDP] = "CDP", 5211eecf91SChanwoo Choi [EXTCON_CHG_USB_ACA] = "ACA", 5311eecf91SChanwoo Choi [EXTCON_CHG_USB_FAST] = "FAST-CHARGER", 5411eecf91SChanwoo Choi [EXTCON_CHG_USB_SLOW] = "SLOW-CHARGER", 558e9bc36dSChanwoo Choi 5611eecf91SChanwoo Choi /* Jack external connector */ 5711eecf91SChanwoo Choi [EXTCON_JACK_MICROPHONE] = "MICROPHONE", 5811eecf91SChanwoo Choi [EXTCON_JACK_HEADPHONE] = "HEADPHONE", 5911eecf91SChanwoo Choi [EXTCON_JACK_LINE_IN] = "LINE-IN", 6011eecf91SChanwoo Choi [EXTCON_JACK_LINE_OUT] = "LINE-OUT", 6111eecf91SChanwoo Choi [EXTCON_JACK_VIDEO_IN] = "VIDEO-IN", 6211eecf91SChanwoo Choi [EXTCON_JACK_VIDEO_OUT] = "VIDEO-OUT", 6311eecf91SChanwoo Choi [EXTCON_JACK_SPDIF_IN] = "SPDIF-IN", 6411eecf91SChanwoo Choi [EXTCON_JACK_SPDIF_OUT] = "SPDIF-OUT", 658e9bc36dSChanwoo Choi 6611eecf91SChanwoo Choi /* Display external connector */ 6711eecf91SChanwoo Choi [EXTCON_DISP_HDMI] = "HDMI", 6811eecf91SChanwoo Choi [EXTCON_DISP_MHL] = "MHL", 6911eecf91SChanwoo Choi [EXTCON_DISP_DVI] = "DVI", 7011eecf91SChanwoo Choi [EXTCON_DISP_VGA] = "VGA", 718e9bc36dSChanwoo Choi 7211eecf91SChanwoo Choi /* Miscellaneous external connector */ 738e9bc36dSChanwoo Choi [EXTCON_DOCK] = "DOCK", 742a9de9c0SChanwoo Choi [EXTCON_JIG] = "JIG", 758e9bc36dSChanwoo Choi [EXTCON_MECHANICAL] = "MECHANICAL", 768e9bc36dSChanwoo Choi 772a9de9c0SChanwoo Choi NULL, 78f68a8342SChanwoo Choi }; 79f68a8342SChanwoo Choi 8020f7b53dSChanwoo Choi /** 8120f7b53dSChanwoo Choi * struct extcon_cable - An internal data for each cable of extcon device. 8220f7b53dSChanwoo Choi * @edev: The extcon device 8320f7b53dSChanwoo Choi * @cable_index: Index of this cable in the edev 8420f7b53dSChanwoo Choi * @attr_g: Attribute group for the cable 8520f7b53dSChanwoo Choi * @attr_name: "name" sysfs entry 8620f7b53dSChanwoo Choi * @attr_state: "state" sysfs entry 8720f7b53dSChanwoo Choi * @attrs: Array pointing to attr_name and attr_state for attr_g 8820f7b53dSChanwoo Choi */ 8920f7b53dSChanwoo Choi struct extcon_cable { 9020f7b53dSChanwoo Choi struct extcon_dev *edev; 9120f7b53dSChanwoo Choi int cable_index; 9220f7b53dSChanwoo Choi 9320f7b53dSChanwoo Choi struct attribute_group attr_g; 9420f7b53dSChanwoo Choi struct device_attribute attr_name; 9520f7b53dSChanwoo Choi struct device_attribute attr_state; 9620f7b53dSChanwoo Choi 9720f7b53dSChanwoo Choi struct attribute *attrs[3]; /* to be fed to attr_g.attrs */ 9820f7b53dSChanwoo Choi }; 9920f7b53dSChanwoo Choi 100f68a8342SChanwoo Choi static struct class *extcon_class; 101f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 102f68a8342SChanwoo Choi static struct class_compat *switch_class; 103f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 104f68a8342SChanwoo Choi 105f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list); 106f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock); 107f68a8342SChanwoo Choi 108f68a8342SChanwoo Choi /** 109f68a8342SChanwoo Choi * check_mutually_exclusive - Check if new_state violates mutually_exclusive 110f68a8342SChanwoo Choi * condition. 111f68a8342SChanwoo Choi * @edev: the extcon device 112f68a8342SChanwoo Choi * @new_state: new cable attach status for @edev 113f68a8342SChanwoo Choi * 114f68a8342SChanwoo Choi * Returns 0 if nothing violates. Returns the index + 1 for the first 115f68a8342SChanwoo Choi * violated condition. 116f68a8342SChanwoo Choi */ 117f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) 118f68a8342SChanwoo Choi { 119f68a8342SChanwoo Choi int i = 0; 120f68a8342SChanwoo Choi 121f68a8342SChanwoo Choi if (!edev->mutually_exclusive) 122f68a8342SChanwoo Choi return 0; 123f68a8342SChanwoo Choi 124f68a8342SChanwoo Choi for (i = 0; edev->mutually_exclusive[i]; i++) { 125f68a8342SChanwoo Choi int weight; 126f68a8342SChanwoo Choi u32 correspondants = new_state & edev->mutually_exclusive[i]; 127f68a8342SChanwoo Choi 128f68a8342SChanwoo Choi /* calculate the total number of bits set */ 129f68a8342SChanwoo Choi weight = hweight32(correspondants); 130f68a8342SChanwoo Choi if (weight > 1) 131f68a8342SChanwoo Choi return i + 1; 132f68a8342SChanwoo Choi } 133f68a8342SChanwoo Choi 134f68a8342SChanwoo Choi return 0; 135f68a8342SChanwoo Choi } 136f68a8342SChanwoo Choi 13773b6ecdbSChanwoo Choi static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id) 1382a9de9c0SChanwoo Choi { 1392a9de9c0SChanwoo Choi int i; 1402a9de9c0SChanwoo Choi 1412a9de9c0SChanwoo Choi /* Find the the index of extcon cable in edev->supported_cable */ 1422a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 1432a9de9c0SChanwoo Choi if (edev->supported_cable[i] == id) 1442a9de9c0SChanwoo Choi return i; 1452a9de9c0SChanwoo Choi } 1462a9de9c0SChanwoo Choi 1472a9de9c0SChanwoo Choi return -EINVAL; 1482a9de9c0SChanwoo Choi } 1492a9de9c0SChanwoo Choi 150046050f6SChanwoo Choi static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached) 151046050f6SChanwoo Choi { 152046050f6SChanwoo Choi if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) { 153f4513b06SHans de Goede *attached = ((new >> idx) & 0x1) ? true : false; 154046050f6SChanwoo Choi return true; 155046050f6SChanwoo Choi } 156046050f6SChanwoo Choi 157046050f6SChanwoo Choi return false; 158046050f6SChanwoo Choi } 159046050f6SChanwoo Choi 160f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr, 161f68a8342SChanwoo Choi char *buf) 162f68a8342SChanwoo Choi { 163f68a8342SChanwoo Choi int i, count = 0; 164f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 165f68a8342SChanwoo Choi 166f68a8342SChanwoo Choi if (edev->max_supported == 0) 167f68a8342SChanwoo Choi return sprintf(buf, "%u\n", edev->state); 168f68a8342SChanwoo Choi 1692a9de9c0SChanwoo Choi for (i = 0; i < edev->max_supported; i++) { 170f68a8342SChanwoo Choi count += sprintf(buf + count, "%s=%d\n", 1712a9de9c0SChanwoo Choi extcon_name[edev->supported_cable[i]], 172f68a8342SChanwoo Choi !!(edev->state & (1 << i))); 173f68a8342SChanwoo Choi } 174f68a8342SChanwoo Choi 175f68a8342SChanwoo Choi return count; 176f68a8342SChanwoo Choi } 177f68a8342SChanwoo Choi 178f68a8342SChanwoo Choi static ssize_t state_store(struct device *dev, struct device_attribute *attr, 179f68a8342SChanwoo Choi const char *buf, size_t count) 180f68a8342SChanwoo Choi { 181f68a8342SChanwoo Choi u32 state; 182f68a8342SChanwoo Choi ssize_t ret = 0; 183f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 184f68a8342SChanwoo Choi 185f68a8342SChanwoo Choi ret = sscanf(buf, "0x%x", &state); 186f68a8342SChanwoo Choi if (ret == 0) 187f68a8342SChanwoo Choi ret = -EINVAL; 188f68a8342SChanwoo Choi else 189f68a8342SChanwoo Choi ret = extcon_set_state(edev, state); 190f68a8342SChanwoo Choi 191f68a8342SChanwoo Choi if (ret < 0) 192f68a8342SChanwoo Choi return ret; 193f68a8342SChanwoo Choi 194f68a8342SChanwoo Choi return count; 195f68a8342SChanwoo Choi } 196f68a8342SChanwoo Choi static DEVICE_ATTR_RW(state); 197f68a8342SChanwoo Choi 198f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr, 199f68a8342SChanwoo Choi char *buf) 200f68a8342SChanwoo Choi { 201f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 202f68a8342SChanwoo Choi 20371c3ffa5SChanwoo Choi return sprintf(buf, "%s\n", edev->name); 204f68a8342SChanwoo Choi } 205f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name); 206f68a8342SChanwoo Choi 207f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev, 208f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 209f68a8342SChanwoo Choi { 210f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 211f68a8342SChanwoo Choi attr_name); 2122a9de9c0SChanwoo Choi int i = cable->cable_index; 213f68a8342SChanwoo Choi 214f68a8342SChanwoo Choi return sprintf(buf, "%s\n", 2152a9de9c0SChanwoo Choi extcon_name[cable->edev->supported_cable[i]]); 216f68a8342SChanwoo Choi } 217f68a8342SChanwoo Choi 218f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev, 219f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 220f68a8342SChanwoo Choi { 221f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 222f68a8342SChanwoo Choi attr_state); 223f68a8342SChanwoo Choi 224be052cc8SRoger Quadros int i = cable->cable_index; 225be052cc8SRoger Quadros 226f68a8342SChanwoo Choi return sprintf(buf, "%d\n", 227f68a8342SChanwoo Choi extcon_get_cable_state_(cable->edev, 228be052cc8SRoger Quadros cable->edev->supported_cable[i])); 229f68a8342SChanwoo Choi } 230f68a8342SChanwoo Choi 231f68a8342SChanwoo Choi /** 232f68a8342SChanwoo Choi * extcon_update_state() - Update the cable attach states of the extcon device 233f68a8342SChanwoo Choi * only for the masked bits. 234f68a8342SChanwoo Choi * @edev: the extcon device 235f68a8342SChanwoo Choi * @mask: the bit mask to designate updated bits. 236f68a8342SChanwoo Choi * @state: new cable attach status for @edev 237f68a8342SChanwoo Choi * 238f68a8342SChanwoo Choi * Changing the state sends uevent with environment variable containing 239f68a8342SChanwoo Choi * the name of extcon device (envp[0]) and the state output (envp[1]). 240f68a8342SChanwoo Choi * Tizen uses this format for extcon device to get events from ports. 241f68a8342SChanwoo Choi * Android uses this format as well. 242f68a8342SChanwoo Choi * 243f68a8342SChanwoo Choi * Note that the notifier provides which bits are changed in the state 244f68a8342SChanwoo Choi * variable with the val parameter (second) to the callback. 245f68a8342SChanwoo Choi */ 246f68a8342SChanwoo Choi int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) 247f68a8342SChanwoo Choi { 248f68a8342SChanwoo Choi char name_buf[120]; 249f68a8342SChanwoo Choi char state_buf[120]; 250f68a8342SChanwoo Choi char *prop_buf; 251f68a8342SChanwoo Choi char *envp[3]; 252f68a8342SChanwoo Choi int env_offset = 0; 253f68a8342SChanwoo Choi int length; 254046050f6SChanwoo Choi int index; 255f68a8342SChanwoo Choi unsigned long flags; 256046050f6SChanwoo Choi bool attached; 257f68a8342SChanwoo Choi 2587eae43aeSChanwoo Choi if (!edev) 2597eae43aeSChanwoo Choi return -EINVAL; 2607eae43aeSChanwoo Choi 261f68a8342SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 262f68a8342SChanwoo Choi 263f68a8342SChanwoo Choi if (edev->state != ((edev->state & ~mask) | (state & mask))) { 264f7a89811SRoger Quadros u32 old_state; 265f7a89811SRoger Quadros 266f68a8342SChanwoo Choi if (check_mutually_exclusive(edev, (edev->state & ~mask) | 267f68a8342SChanwoo Choi (state & mask))) { 268f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 269f68a8342SChanwoo Choi return -EPERM; 270f68a8342SChanwoo Choi } 271f68a8342SChanwoo Choi 272f7a89811SRoger Quadros old_state = edev->state; 273f68a8342SChanwoo Choi edev->state &= ~mask; 274f68a8342SChanwoo Choi edev->state |= state & mask; 275f68a8342SChanwoo Choi 276f7a89811SRoger Quadros for (index = 0; index < edev->max_supported; index++) { 277f7a89811SRoger Quadros if (is_extcon_changed(old_state, edev->state, index, 278f7a89811SRoger Quadros &attached)) 279f7a89811SRoger Quadros raw_notifier_call_chain(&edev->nh[index], 280f7a89811SRoger Quadros attached, edev); 281f7a89811SRoger Quadros } 282f7a89811SRoger Quadros 283f68a8342SChanwoo Choi /* This could be in interrupt handler */ 284f68a8342SChanwoo Choi prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); 285f68a8342SChanwoo Choi if (prop_buf) { 286f68a8342SChanwoo Choi length = name_show(&edev->dev, NULL, prop_buf); 287f68a8342SChanwoo Choi if (length > 0) { 288f68a8342SChanwoo Choi if (prop_buf[length - 1] == '\n') 289f68a8342SChanwoo Choi prop_buf[length - 1] = 0; 290f68a8342SChanwoo Choi snprintf(name_buf, sizeof(name_buf), 291f68a8342SChanwoo Choi "NAME=%s", prop_buf); 292f68a8342SChanwoo Choi envp[env_offset++] = name_buf; 293f68a8342SChanwoo Choi } 294f68a8342SChanwoo Choi length = state_show(&edev->dev, NULL, prop_buf); 295f68a8342SChanwoo Choi if (length > 0) { 296f68a8342SChanwoo Choi if (prop_buf[length - 1] == '\n') 297f68a8342SChanwoo Choi prop_buf[length - 1] = 0; 298f68a8342SChanwoo Choi snprintf(state_buf, sizeof(state_buf), 299f68a8342SChanwoo Choi "STATE=%s", prop_buf); 300f68a8342SChanwoo Choi envp[env_offset++] = state_buf; 301f68a8342SChanwoo Choi } 302f68a8342SChanwoo Choi envp[env_offset] = NULL; 303f68a8342SChanwoo Choi /* Unlock early before uevent */ 304f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 305f68a8342SChanwoo Choi 306f68a8342SChanwoo Choi kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); 307f68a8342SChanwoo Choi free_page((unsigned long)prop_buf); 308f68a8342SChanwoo Choi } else { 309f68a8342SChanwoo Choi /* Unlock early before uevent */ 310f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 311f68a8342SChanwoo Choi 312f68a8342SChanwoo Choi dev_err(&edev->dev, "out of memory in extcon_set_state\n"); 313f68a8342SChanwoo Choi kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); 314f68a8342SChanwoo Choi } 315f68a8342SChanwoo Choi } else { 316f68a8342SChanwoo Choi /* No changes */ 317f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 318f68a8342SChanwoo Choi } 319f68a8342SChanwoo Choi 320f68a8342SChanwoo Choi return 0; 321f68a8342SChanwoo Choi } 322f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_update_state); 323f68a8342SChanwoo Choi 324f68a8342SChanwoo Choi /** 325f68a8342SChanwoo Choi * extcon_set_state() - Set the cable attach states of the extcon device. 326f68a8342SChanwoo Choi * @edev: the extcon device 327f68a8342SChanwoo Choi * @state: new cable attach status for @edev 328f68a8342SChanwoo Choi * 329f68a8342SChanwoo Choi * Note that notifier provides which bits are changed in the state 330f68a8342SChanwoo Choi * variable with the val parameter (second) to the callback. 331f68a8342SChanwoo Choi */ 332f68a8342SChanwoo Choi int extcon_set_state(struct extcon_dev *edev, u32 state) 333f68a8342SChanwoo Choi { 3347eae43aeSChanwoo Choi if (!edev) 3357eae43aeSChanwoo Choi return -EINVAL; 3367eae43aeSChanwoo Choi 337f68a8342SChanwoo Choi return extcon_update_state(edev, 0xffffffff, state); 338f68a8342SChanwoo Choi } 339f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state); 340f68a8342SChanwoo Choi 341f68a8342SChanwoo Choi /** 342f68a8342SChanwoo Choi * extcon_get_cable_state_() - Get the status of a specific cable. 343f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 3442a9de9c0SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 345f68a8342SChanwoo Choi */ 34673b6ecdbSChanwoo Choi int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id) 347f68a8342SChanwoo Choi { 3482a9de9c0SChanwoo Choi int index; 3492a9de9c0SChanwoo Choi 3507eae43aeSChanwoo Choi if (!edev) 3517eae43aeSChanwoo Choi return -EINVAL; 3527eae43aeSChanwoo Choi 3532a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 3542a9de9c0SChanwoo Choi if (index < 0) 3552a9de9c0SChanwoo Choi return index; 3562a9de9c0SChanwoo Choi 3572a9de9c0SChanwoo Choi if (edev->max_supported && edev->max_supported <= index) 358f68a8342SChanwoo Choi return -EINVAL; 359f68a8342SChanwoo Choi 360f68a8342SChanwoo Choi return !!(edev->state & (1 << index)); 361f68a8342SChanwoo Choi } 362f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_cable_state_); 363f68a8342SChanwoo Choi 364f68a8342SChanwoo Choi /** 365f68a8342SChanwoo Choi * extcon_set_cable_state_() - Set the status of a specific cable. 366f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 3672a9de9c0SChanwoo Choi * @id: the unique id of each external connector 3682a9de9c0SChanwoo Choi * in extcon enumeration. 3692a9de9c0SChanwoo Choi * @state: the new cable status. The default semantics is 370f68a8342SChanwoo Choi * true: attached / false: detached. 371f68a8342SChanwoo Choi */ 37273b6ecdbSChanwoo Choi int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id, 3732a9de9c0SChanwoo Choi bool cable_state) 374f68a8342SChanwoo Choi { 375f68a8342SChanwoo Choi u32 state; 3762a9de9c0SChanwoo Choi int index; 377f68a8342SChanwoo Choi 3787eae43aeSChanwoo Choi if (!edev) 3797eae43aeSChanwoo Choi return -EINVAL; 3807eae43aeSChanwoo Choi 3812a9de9c0SChanwoo Choi index = find_cable_index_by_id(edev, id); 3822a9de9c0SChanwoo Choi if (index < 0) 3832a9de9c0SChanwoo Choi return index; 3842a9de9c0SChanwoo Choi 3852a9de9c0SChanwoo Choi if (edev->max_supported && edev->max_supported <= index) 386f68a8342SChanwoo Choi return -EINVAL; 387f68a8342SChanwoo Choi 388f68a8342SChanwoo Choi state = cable_state ? (1 << index) : 0; 389f68a8342SChanwoo Choi return extcon_update_state(edev, 1 << index, state); 390f68a8342SChanwoo Choi } 391f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_cable_state_); 392f68a8342SChanwoo Choi 393f68a8342SChanwoo Choi /** 394f68a8342SChanwoo Choi * extcon_get_extcon_dev() - Get the extcon device instance from the name 395f68a8342SChanwoo Choi * @extcon_name: The extcon name provided with extcon_dev_register() 396f68a8342SChanwoo Choi */ 397f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) 398f68a8342SChanwoo Choi { 399f68a8342SChanwoo Choi struct extcon_dev *sd; 400f68a8342SChanwoo Choi 4017eae43aeSChanwoo Choi if (!extcon_name) 4027eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 4037eae43aeSChanwoo Choi 404f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 405f68a8342SChanwoo Choi list_for_each_entry(sd, &extcon_dev_list, entry) { 406f68a8342SChanwoo Choi if (!strcmp(sd->name, extcon_name)) 407f68a8342SChanwoo Choi goto out; 408f68a8342SChanwoo Choi } 409f68a8342SChanwoo Choi sd = NULL; 410f68a8342SChanwoo Choi out: 411f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 412f68a8342SChanwoo Choi return sd; 413f68a8342SChanwoo Choi } 414f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); 415f68a8342SChanwoo Choi 416f68a8342SChanwoo Choi /** 417f68a8342SChanwoo Choi * extcon_register_notifier() - Register a notifiee to get notified by 418f68a8342SChanwoo Choi * any attach status changes from the extcon. 419046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 420046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 421f68a8342SChanwoo Choi * @nb: a notifier block to be registered. 422f68a8342SChanwoo Choi * 423f68a8342SChanwoo Choi * Note that the second parameter given to the callback of nb (val) is 424f68a8342SChanwoo Choi * "old_state", not the current state. The current state can be retrieved 425f68a8342SChanwoo Choi * by looking at the third pameter (edev pointer)'s state value. 426f68a8342SChanwoo Choi */ 42773b6ecdbSChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, 428f68a8342SChanwoo Choi struct notifier_block *nb) 429f68a8342SChanwoo Choi { 43066bee35fSHans de Goede unsigned long flags; 431046050f6SChanwoo Choi int ret, idx; 432046050f6SChanwoo Choi 433830ae442SChanwoo Choi if (!nb) 4347eae43aeSChanwoo Choi return -EINVAL; 4357eae43aeSChanwoo Choi 436830ae442SChanwoo Choi if (edev) { 437046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 438a05f44c8SStephen Boyd if (idx < 0) 439a05f44c8SStephen Boyd return idx; 44066bee35fSHans de Goede 44166bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 442046050f6SChanwoo Choi ret = raw_notifier_chain_register(&edev->nh[idx], nb); 44366bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 444830ae442SChanwoo Choi } else { 445830ae442SChanwoo Choi struct extcon_dev *extd; 446830ae442SChanwoo Choi 447830ae442SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 448830ae442SChanwoo Choi list_for_each_entry(extd, &extcon_dev_list, entry) { 449830ae442SChanwoo Choi idx = find_cable_index_by_id(extd, id); 450830ae442SChanwoo Choi if (idx >= 0) 451830ae442SChanwoo Choi break; 452830ae442SChanwoo Choi } 453830ae442SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 454830ae442SChanwoo Choi 455830ae442SChanwoo Choi if (idx >= 0) { 456830ae442SChanwoo Choi edev = extd; 457830ae442SChanwoo Choi return extcon_register_notifier(extd, id, nb); 458830ae442SChanwoo Choi } else { 459830ae442SChanwoo Choi ret = -ENODEV; 460830ae442SChanwoo Choi } 461830ae442SChanwoo Choi } 46266bee35fSHans de Goede 46366bee35fSHans de Goede return ret; 464f68a8342SChanwoo Choi } 465f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier); 466f68a8342SChanwoo Choi 467f68a8342SChanwoo Choi /** 468f68a8342SChanwoo Choi * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. 469046050f6SChanwoo Choi * @edev: the extcon device that has the external connecotr. 470046050f6SChanwoo Choi * @id: the unique id of each external connector in extcon enumeration. 471046050f6SChanwoo Choi * @nb: a notifier block to be registered. 472f68a8342SChanwoo Choi */ 47373b6ecdbSChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, 474f68a8342SChanwoo Choi struct notifier_block *nb) 475f68a8342SChanwoo Choi { 47666bee35fSHans de Goede unsigned long flags; 477046050f6SChanwoo Choi int ret, idx; 478046050f6SChanwoo Choi 4797eae43aeSChanwoo Choi if (!edev || !nb) 4807eae43aeSChanwoo Choi return -EINVAL; 4817eae43aeSChanwoo Choi 482046050f6SChanwoo Choi idx = find_cable_index_by_id(edev, id); 483a05f44c8SStephen Boyd if (idx < 0) 484a05f44c8SStephen Boyd return idx; 48566bee35fSHans de Goede 48666bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 487046050f6SChanwoo Choi ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); 48866bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 48966bee35fSHans de Goede 49066bee35fSHans de Goede return ret; 491f68a8342SChanwoo Choi } 492f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier); 493f68a8342SChanwoo Choi 494f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = { 495f68a8342SChanwoo Choi &dev_attr_state.attr, 496f68a8342SChanwoo Choi &dev_attr_name.attr, 497f68a8342SChanwoo Choi NULL, 498f68a8342SChanwoo Choi }; 499f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon); 500f68a8342SChanwoo Choi 501f68a8342SChanwoo Choi static int create_extcon_class(void) 502f68a8342SChanwoo Choi { 503f68a8342SChanwoo Choi if (!extcon_class) { 504f68a8342SChanwoo Choi extcon_class = class_create(THIS_MODULE, "extcon"); 505f68a8342SChanwoo Choi if (IS_ERR(extcon_class)) 506f68a8342SChanwoo Choi return PTR_ERR(extcon_class); 507f68a8342SChanwoo Choi extcon_class->dev_groups = extcon_groups; 508f68a8342SChanwoo Choi 509f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 510f68a8342SChanwoo Choi switch_class = class_compat_register("switch"); 511f68a8342SChanwoo Choi if (WARN(!switch_class, "cannot allocate")) 512f68a8342SChanwoo Choi return -ENOMEM; 513f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 514f68a8342SChanwoo Choi } 515f68a8342SChanwoo Choi 516f68a8342SChanwoo Choi return 0; 517f68a8342SChanwoo Choi } 518f68a8342SChanwoo Choi 519f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev) 520f68a8342SChanwoo Choi { 521f68a8342SChanwoo Choi } 522f68a8342SChanwoo Choi 523f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive"; 524f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev) 525f68a8342SChanwoo Choi { 526f68a8342SChanwoo Choi } 527f68a8342SChanwoo Choi 528f68a8342SChanwoo Choi /* 529f68a8342SChanwoo Choi * extcon_dev_allocate() - Allocate the memory of extcon device. 5302a9de9c0SChanwoo Choi * @supported_cable: Array of supported extcon ending with EXTCON_NONE. 531f68a8342SChanwoo Choi * If supported_cable is NULL, cable name related APIs 532f68a8342SChanwoo Choi * are disabled. 533f68a8342SChanwoo Choi * 534f68a8342SChanwoo Choi * This function allocates the memory for extcon device without allocating 535f68a8342SChanwoo Choi * memory in each extcon provider driver and initialize default setting for 536f68a8342SChanwoo Choi * extcon device. 537f68a8342SChanwoo Choi * 538f68a8342SChanwoo Choi * Return the pointer of extcon device if success or ERR_PTR(err) if fail 539f68a8342SChanwoo Choi */ 54073b6ecdbSChanwoo Choi struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) 541f68a8342SChanwoo Choi { 542f68a8342SChanwoo Choi struct extcon_dev *edev; 543f68a8342SChanwoo Choi 5447eae43aeSChanwoo Choi if (!supported_cable) 5457eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 5467eae43aeSChanwoo Choi 547f68a8342SChanwoo Choi edev = kzalloc(sizeof(*edev), GFP_KERNEL); 548f68a8342SChanwoo Choi if (!edev) 549f68a8342SChanwoo Choi return ERR_PTR(-ENOMEM); 550f68a8342SChanwoo Choi 551f68a8342SChanwoo Choi edev->max_supported = 0; 552f68a8342SChanwoo Choi edev->supported_cable = supported_cable; 553f68a8342SChanwoo Choi 554f68a8342SChanwoo Choi return edev; 555f68a8342SChanwoo Choi } 556f68a8342SChanwoo Choi 557f68a8342SChanwoo Choi /* 558f68a8342SChanwoo Choi * extcon_dev_free() - Free the memory of extcon device. 559f68a8342SChanwoo Choi * @edev: the extcon device to free 560f68a8342SChanwoo Choi */ 561f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev) 562f68a8342SChanwoo Choi { 563f68a8342SChanwoo Choi kfree(edev); 564f68a8342SChanwoo Choi } 565f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free); 566f68a8342SChanwoo Choi 567f68a8342SChanwoo Choi /** 568f68a8342SChanwoo Choi * extcon_dev_register() - Register a new extcon device 569f68a8342SChanwoo Choi * @edev : the new extcon device (should be allocated before calling) 570f68a8342SChanwoo Choi * 571f68a8342SChanwoo Choi * Among the members of edev struct, please set the "user initializing data" 572f68a8342SChanwoo Choi * in any case and set the "optional callbacks" if required. However, please 573f68a8342SChanwoo Choi * do not set the values of "internal data", which are initialized by 574f68a8342SChanwoo Choi * this function. 575f68a8342SChanwoo Choi */ 576f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev) 577f68a8342SChanwoo Choi { 578f68a8342SChanwoo Choi int ret, index = 0; 57971c3ffa5SChanwoo Choi static atomic_t edev_no = ATOMIC_INIT(-1); 580f68a8342SChanwoo Choi 581f68a8342SChanwoo Choi if (!extcon_class) { 582f68a8342SChanwoo Choi ret = create_extcon_class(); 583f68a8342SChanwoo Choi if (ret < 0) 584f68a8342SChanwoo Choi return ret; 585f68a8342SChanwoo Choi } 586f68a8342SChanwoo Choi 5877eae43aeSChanwoo Choi if (!edev || !edev->supported_cable) 5882a9de9c0SChanwoo Choi return -EINVAL; 589f68a8342SChanwoo Choi 5902a9de9c0SChanwoo Choi for (; edev->supported_cable[index] != EXTCON_NONE; index++); 5912a9de9c0SChanwoo Choi 5922a9de9c0SChanwoo Choi edev->max_supported = index; 593f68a8342SChanwoo Choi if (index > SUPPORTED_CABLE_MAX) { 5942a9de9c0SChanwoo Choi dev_err(&edev->dev, 5952a9de9c0SChanwoo Choi "exceed the maximum number of supported cables\n"); 596f68a8342SChanwoo Choi return -EINVAL; 597f68a8342SChanwoo Choi } 598f68a8342SChanwoo Choi 599f68a8342SChanwoo Choi edev->dev.class = extcon_class; 600f68a8342SChanwoo Choi edev->dev.release = extcon_dev_release; 601f68a8342SChanwoo Choi 60271c3ffa5SChanwoo Choi edev->name = dev_name(edev->dev.parent); 603f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(edev->name)) { 604f68a8342SChanwoo Choi dev_err(&edev->dev, 605f68a8342SChanwoo Choi "extcon device name is null\n"); 606f68a8342SChanwoo Choi return -EINVAL; 607f68a8342SChanwoo Choi } 60871c3ffa5SChanwoo Choi dev_set_name(&edev->dev, "extcon%lu", 60971c3ffa5SChanwoo Choi (unsigned long)atomic_inc_return(&edev_no)); 610f68a8342SChanwoo Choi 611f68a8342SChanwoo Choi if (edev->max_supported) { 612f68a8342SChanwoo Choi char buf[10]; 613f68a8342SChanwoo Choi char *str; 614f68a8342SChanwoo Choi struct extcon_cable *cable; 615f68a8342SChanwoo Choi 616f68a8342SChanwoo Choi edev->cables = kzalloc(sizeof(struct extcon_cable) * 617f68a8342SChanwoo Choi edev->max_supported, GFP_KERNEL); 618f68a8342SChanwoo Choi if (!edev->cables) { 619f68a8342SChanwoo Choi ret = -ENOMEM; 620f68a8342SChanwoo Choi goto err_sysfs_alloc; 621f68a8342SChanwoo Choi } 622f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) { 623f68a8342SChanwoo Choi cable = &edev->cables[index]; 624f68a8342SChanwoo Choi 625f68a8342SChanwoo Choi snprintf(buf, 10, "cable.%d", index); 626f68a8342SChanwoo Choi str = kzalloc(sizeof(char) * (strlen(buf) + 1), 627f68a8342SChanwoo Choi GFP_KERNEL); 628f68a8342SChanwoo Choi if (!str) { 629f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 630f68a8342SChanwoo Choi cable = &edev->cables[index]; 631f68a8342SChanwoo Choi kfree(cable->attr_g.name); 632f68a8342SChanwoo Choi } 633f68a8342SChanwoo Choi ret = -ENOMEM; 634f68a8342SChanwoo Choi 635f68a8342SChanwoo Choi goto err_alloc_cables; 636f68a8342SChanwoo Choi } 637f68a8342SChanwoo Choi strcpy(str, buf); 638f68a8342SChanwoo Choi 639f68a8342SChanwoo Choi cable->edev = edev; 640f68a8342SChanwoo Choi cable->cable_index = index; 641f68a8342SChanwoo Choi cable->attrs[0] = &cable->attr_name.attr; 642f68a8342SChanwoo Choi cable->attrs[1] = &cable->attr_state.attr; 643f68a8342SChanwoo Choi cable->attrs[2] = NULL; 644f68a8342SChanwoo Choi cable->attr_g.name = str; 645f68a8342SChanwoo Choi cable->attr_g.attrs = cable->attrs; 646f68a8342SChanwoo Choi 647f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_name.attr); 648f68a8342SChanwoo Choi cable->attr_name.attr.name = "name"; 649f68a8342SChanwoo Choi cable->attr_name.attr.mode = 0444; 650f68a8342SChanwoo Choi cable->attr_name.show = cable_name_show; 651f68a8342SChanwoo Choi 652f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_state.attr); 653f68a8342SChanwoo Choi cable->attr_state.attr.name = "state"; 654f68a8342SChanwoo Choi cable->attr_state.attr.mode = 0444; 655f68a8342SChanwoo Choi cable->attr_state.show = cable_state_show; 656f68a8342SChanwoo Choi } 657f68a8342SChanwoo Choi } 658f68a8342SChanwoo Choi 659f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 660f68a8342SChanwoo Choi char buf[80]; 661f68a8342SChanwoo Choi char *name; 662f68a8342SChanwoo Choi 663f68a8342SChanwoo Choi /* Count the size of mutually_exclusive array */ 664f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 665f68a8342SChanwoo Choi ; 666f68a8342SChanwoo Choi 667f68a8342SChanwoo Choi edev->attrs_muex = kzalloc(sizeof(struct attribute *) * 668f68a8342SChanwoo Choi (index + 1), GFP_KERNEL); 669f68a8342SChanwoo Choi if (!edev->attrs_muex) { 670f68a8342SChanwoo Choi ret = -ENOMEM; 671f68a8342SChanwoo Choi goto err_muex; 672f68a8342SChanwoo Choi } 673f68a8342SChanwoo Choi 674f68a8342SChanwoo Choi edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * 675f68a8342SChanwoo Choi index, GFP_KERNEL); 676f68a8342SChanwoo Choi if (!edev->d_attrs_muex) { 677f68a8342SChanwoo Choi ret = -ENOMEM; 678f68a8342SChanwoo Choi kfree(edev->attrs_muex); 679f68a8342SChanwoo Choi goto err_muex; 680f68a8342SChanwoo Choi } 681f68a8342SChanwoo Choi 682f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) { 683f68a8342SChanwoo Choi sprintf(buf, "0x%x", edev->mutually_exclusive[index]); 684f68a8342SChanwoo Choi name = kzalloc(sizeof(char) * (strlen(buf) + 1), 685f68a8342SChanwoo Choi GFP_KERNEL); 686f68a8342SChanwoo Choi if (!name) { 687f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 688f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr. 689f68a8342SChanwoo Choi name); 690f68a8342SChanwoo Choi } 691f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 692f68a8342SChanwoo Choi kfree(edev->attrs_muex); 693f68a8342SChanwoo Choi ret = -ENOMEM; 694f68a8342SChanwoo Choi goto err_muex; 695f68a8342SChanwoo Choi } 696f68a8342SChanwoo Choi strcpy(name, buf); 697f68a8342SChanwoo Choi sysfs_attr_init(&edev->d_attrs_muex[index].attr); 698f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.name = name; 699f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.mode = 0000; 700f68a8342SChanwoo Choi edev->attrs_muex[index] = &edev->d_attrs_muex[index] 701f68a8342SChanwoo Choi .attr; 702f68a8342SChanwoo Choi } 703f68a8342SChanwoo Choi edev->attr_g_muex.name = muex_name; 704f68a8342SChanwoo Choi edev->attr_g_muex.attrs = edev->attrs_muex; 705f68a8342SChanwoo Choi 706f68a8342SChanwoo Choi } 707f68a8342SChanwoo Choi 708f68a8342SChanwoo Choi if (edev->max_supported) { 709f68a8342SChanwoo Choi edev->extcon_dev_type.groups = 710f68a8342SChanwoo Choi kzalloc(sizeof(struct attribute_group *) * 711f68a8342SChanwoo Choi (edev->max_supported + 2), GFP_KERNEL); 712f68a8342SChanwoo Choi if (!edev->extcon_dev_type.groups) { 713f68a8342SChanwoo Choi ret = -ENOMEM; 714f68a8342SChanwoo Choi goto err_alloc_groups; 715f68a8342SChanwoo Choi } 716f68a8342SChanwoo Choi 717f68a8342SChanwoo Choi edev->extcon_dev_type.name = dev_name(&edev->dev); 718f68a8342SChanwoo Choi edev->extcon_dev_type.release = dummy_sysfs_dev_release; 719f68a8342SChanwoo Choi 720f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 721f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 722f68a8342SChanwoo Choi &edev->cables[index].attr_g; 723f68a8342SChanwoo Choi if (edev->mutually_exclusive) 724f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 725f68a8342SChanwoo Choi &edev->attr_g_muex; 726f68a8342SChanwoo Choi 727f68a8342SChanwoo Choi edev->dev.type = &edev->extcon_dev_type; 728f68a8342SChanwoo Choi } 729f68a8342SChanwoo Choi 730f68a8342SChanwoo Choi ret = device_register(&edev->dev); 731f68a8342SChanwoo Choi if (ret) { 732f68a8342SChanwoo Choi put_device(&edev->dev); 733f68a8342SChanwoo Choi goto err_dev; 734f68a8342SChanwoo Choi } 735f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 736f68a8342SChanwoo Choi if (switch_class) 737f68a8342SChanwoo Choi ret = class_compat_create_link(switch_class, &edev->dev, NULL); 738f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 739f68a8342SChanwoo Choi 740f68a8342SChanwoo Choi spin_lock_init(&edev->lock); 741f68a8342SChanwoo Choi 742046050f6SChanwoo Choi edev->nh = devm_kzalloc(&edev->dev, 743046050f6SChanwoo Choi sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL); 744046050f6SChanwoo Choi if (!edev->nh) { 745046050f6SChanwoo Choi ret = -ENOMEM; 746046050f6SChanwoo Choi goto err_dev; 747046050f6SChanwoo Choi } 748046050f6SChanwoo Choi 749046050f6SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 750046050f6SChanwoo Choi RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); 751f68a8342SChanwoo Choi 752f68a8342SChanwoo Choi dev_set_drvdata(&edev->dev, edev); 753f68a8342SChanwoo Choi edev->state = 0; 754f68a8342SChanwoo Choi 755f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 756f68a8342SChanwoo Choi list_add(&edev->entry, &extcon_dev_list); 757f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 758f68a8342SChanwoo Choi 759f68a8342SChanwoo Choi return 0; 760f68a8342SChanwoo Choi 761f68a8342SChanwoo Choi err_dev: 762f68a8342SChanwoo Choi if (edev->max_supported) 763f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 764f68a8342SChanwoo Choi err_alloc_groups: 765f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 766f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 767f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 768f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 769f68a8342SChanwoo Choi kfree(edev->attrs_muex); 770f68a8342SChanwoo Choi } 771f68a8342SChanwoo Choi err_muex: 772f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 773f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 774f68a8342SChanwoo Choi err_alloc_cables: 775f68a8342SChanwoo Choi if (edev->max_supported) 776f68a8342SChanwoo Choi kfree(edev->cables); 777f68a8342SChanwoo Choi err_sysfs_alloc: 778f68a8342SChanwoo Choi return ret; 779f68a8342SChanwoo Choi } 780f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register); 781f68a8342SChanwoo Choi 782f68a8342SChanwoo Choi /** 783f68a8342SChanwoo Choi * extcon_dev_unregister() - Unregister the extcon device. 784f68a8342SChanwoo Choi * @edev: the extcon device instance to be unregistered. 785f68a8342SChanwoo Choi * 786f68a8342SChanwoo Choi * Note that this does not call kfree(edev) because edev was not allocated 787f68a8342SChanwoo Choi * by this class. 788f68a8342SChanwoo Choi */ 789f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev) 790f68a8342SChanwoo Choi { 791f68a8342SChanwoo Choi int index; 792f68a8342SChanwoo Choi 7937eae43aeSChanwoo Choi if (!edev) 7947eae43aeSChanwoo Choi return; 7957eae43aeSChanwoo Choi 796f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 797f68a8342SChanwoo Choi list_del(&edev->entry); 798f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 799f68a8342SChanwoo Choi 800f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(get_device(&edev->dev))) { 801f68a8342SChanwoo Choi dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", 802f68a8342SChanwoo Choi dev_name(&edev->dev)); 803f68a8342SChanwoo Choi return; 804f68a8342SChanwoo Choi } 805f68a8342SChanwoo Choi 806f68a8342SChanwoo Choi device_unregister(&edev->dev); 807f68a8342SChanwoo Choi 808f68a8342SChanwoo Choi if (edev->mutually_exclusive && edev->max_supported) { 809f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; 810f68a8342SChanwoo Choi index++) 811f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 812f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 813f68a8342SChanwoo Choi kfree(edev->attrs_muex); 814f68a8342SChanwoo Choi } 815f68a8342SChanwoo Choi 816f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 817f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 818f68a8342SChanwoo Choi 819f68a8342SChanwoo Choi if (edev->max_supported) { 820f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 821f68a8342SChanwoo Choi kfree(edev->cables); 822f68a8342SChanwoo Choi } 823f68a8342SChanwoo Choi 824f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 825f68a8342SChanwoo Choi if (switch_class) 826f68a8342SChanwoo Choi class_compat_remove_link(switch_class, &edev->dev, NULL); 827f68a8342SChanwoo Choi #endif 828f68a8342SChanwoo Choi put_device(&edev->dev); 829f68a8342SChanwoo Choi } 830f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister); 831f68a8342SChanwoo Choi 832f68a8342SChanwoo Choi #ifdef CONFIG_OF 833f68a8342SChanwoo Choi /* 834f68a8342SChanwoo Choi * extcon_get_edev_by_phandle - Get the extcon device from devicetree 835f68a8342SChanwoo Choi * @dev - instance to the given device 836f68a8342SChanwoo Choi * @index - index into list of extcon_dev 837f68a8342SChanwoo Choi * 838f68a8342SChanwoo Choi * return the instance of extcon device 839f68a8342SChanwoo Choi */ 840f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 841f68a8342SChanwoo Choi { 842f68a8342SChanwoo Choi struct device_node *node; 843f68a8342SChanwoo Choi struct extcon_dev *edev; 844f68a8342SChanwoo Choi 8457eae43aeSChanwoo Choi if (!dev) 8467eae43aeSChanwoo Choi return ERR_PTR(-EINVAL); 8477eae43aeSChanwoo Choi 848f68a8342SChanwoo Choi if (!dev->of_node) { 849f68a8342SChanwoo Choi dev_err(dev, "device does not have a device node entry\n"); 850f68a8342SChanwoo Choi return ERR_PTR(-EINVAL); 851f68a8342SChanwoo Choi } 852f68a8342SChanwoo Choi 853f68a8342SChanwoo Choi node = of_parse_phandle(dev->of_node, "extcon", index); 854f68a8342SChanwoo Choi if (!node) { 855f68a8342SChanwoo Choi dev_err(dev, "failed to get phandle in %s node\n", 856f68a8342SChanwoo Choi dev->of_node->full_name); 857f68a8342SChanwoo Choi return ERR_PTR(-ENODEV); 858f68a8342SChanwoo Choi } 859f68a8342SChanwoo Choi 860f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 861f68a8342SChanwoo Choi list_for_each_entry(edev, &extcon_dev_list, entry) { 862f68a8342SChanwoo Choi if (edev->dev.parent && edev->dev.parent->of_node == node) { 863f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 864*5d5c4c13SPeter Chen of_node_put(node); 865f68a8342SChanwoo Choi return edev; 866f68a8342SChanwoo Choi } 867f68a8342SChanwoo Choi } 868f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 869*5d5c4c13SPeter Chen of_node_put(node); 870f68a8342SChanwoo Choi 871f68a8342SChanwoo Choi return ERR_PTR(-EPROBE_DEFER); 872f68a8342SChanwoo Choi } 873f68a8342SChanwoo Choi #else 874f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 875f68a8342SChanwoo Choi { 876f68a8342SChanwoo Choi return ERR_PTR(-ENOSYS); 877f68a8342SChanwoo Choi } 878f68a8342SChanwoo Choi #endif /* CONFIG_OF */ 879f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); 880f68a8342SChanwoo Choi 881707d7550SChanwoo Choi /** 882707d7550SChanwoo Choi * extcon_get_edev_name() - Get the name of the extcon device. 883707d7550SChanwoo Choi * @edev: the extcon device 884707d7550SChanwoo Choi */ 885707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev) 886707d7550SChanwoo Choi { 887707d7550SChanwoo Choi return !edev ? NULL : edev->name; 888707d7550SChanwoo Choi } 889707d7550SChanwoo Choi 890f68a8342SChanwoo Choi static int __init extcon_class_init(void) 891f68a8342SChanwoo Choi { 892f68a8342SChanwoo Choi return create_extcon_class(); 893f68a8342SChanwoo Choi } 894f68a8342SChanwoo Choi module_init(extcon_class_init); 895f68a8342SChanwoo Choi 896f68a8342SChanwoo Choi static void __exit extcon_class_exit(void) 897f68a8342SChanwoo Choi { 898f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 899f68a8342SChanwoo Choi class_compat_unregister(switch_class); 900f68a8342SChanwoo Choi #endif 901f68a8342SChanwoo Choi class_destroy(extcon_class); 902f68a8342SChanwoo Choi } 903f68a8342SChanwoo Choi module_exit(extcon_class_exit); 904f68a8342SChanwoo Choi 9052a9de9c0SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <[email protected]>"); 906f68a8342SChanwoo Choi MODULE_AUTHOR("Mike Lockwood <[email protected]>"); 907f68a8342SChanwoo Choi MODULE_AUTHOR("Donggeun Kim <[email protected]>"); 908f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <[email protected]>"); 909f68a8342SChanwoo Choi MODULE_DESCRIPTION("External connector (extcon) class driver"); 910f68a8342SChanwoo Choi MODULE_LICENSE("GPL"); 911