1f68a8342SChanwoo Choi /* 2f68a8342SChanwoo Choi * drivers/extcon/extcon_class.c 3f68a8342SChanwoo Choi * 4f68a8342SChanwoo Choi * External connector (extcon) class driver 5f68a8342SChanwoo Choi * 6f68a8342SChanwoo Choi * Copyright (C) 2012 Samsung Electronics 7f68a8342SChanwoo Choi * Author: Donggeun Kim <dg77.kim@samsung.com> 8f68a8342SChanwoo Choi * Author: MyungJoo Ham <myungjoo.ham@samsung.com> 9f68a8342SChanwoo Choi * 10f68a8342SChanwoo Choi * based on android/drivers/switch/switch_class.c 11f68a8342SChanwoo Choi * Copyright (C) 2008 Google, Inc. 12f68a8342SChanwoo Choi * Author: Mike Lockwood <lockwood@android.com> 13f68a8342SChanwoo Choi * 14f68a8342SChanwoo Choi * This software is licensed under the terms of the GNU General Public 15f68a8342SChanwoo Choi * License version 2, as published by the Free Software Foundation, and 16f68a8342SChanwoo Choi * may be copied, distributed, and modified under those terms. 17f68a8342SChanwoo Choi * 18f68a8342SChanwoo Choi * This program is distributed in the hope that it will be useful, 19f68a8342SChanwoo Choi * but WITHOUT ANY WARRANTY; without even the implied warranty of 20f68a8342SChanwoo Choi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21f68a8342SChanwoo Choi * GNU General Public License for more details. 22f68a8342SChanwoo Choi * 23f68a8342SChanwoo Choi */ 24f68a8342SChanwoo Choi 25f68a8342SChanwoo Choi #include <linux/module.h> 26f68a8342SChanwoo Choi #include <linux/types.h> 27f68a8342SChanwoo Choi #include <linux/init.h> 28f68a8342SChanwoo Choi #include <linux/device.h> 29f68a8342SChanwoo Choi #include <linux/fs.h> 30f68a8342SChanwoo Choi #include <linux/err.h> 31f68a8342SChanwoo Choi #include <linux/extcon.h> 32f68a8342SChanwoo Choi #include <linux/of.h> 33f68a8342SChanwoo Choi #include <linux/slab.h> 34f68a8342SChanwoo Choi #include <linux/sysfs.h> 35f68a8342SChanwoo Choi 36f68a8342SChanwoo Choi /* 37f68a8342SChanwoo Choi * extcon_cable_name suggests the standard cable names for commonly used 38f68a8342SChanwoo Choi * cable types. 39f68a8342SChanwoo Choi * 40f68a8342SChanwoo Choi * However, please do not use extcon_cable_name directly for extcon_dev 41f68a8342SChanwoo Choi * struct's supported_cable pointer unless your device really supports 42f68a8342SChanwoo Choi * every single port-type of the following cable names. Please choose cable 43f68a8342SChanwoo Choi * names that are actually used in your extcon device. 44f68a8342SChanwoo Choi */ 45f68a8342SChanwoo Choi const char extcon_cable_name[][CABLE_NAME_MAX + 1] = { 46f68a8342SChanwoo Choi [EXTCON_USB] = "USB", 47f68a8342SChanwoo Choi [EXTCON_USB_HOST] = "USB-Host", 48f68a8342SChanwoo Choi [EXTCON_TA] = "TA", 49f68a8342SChanwoo Choi [EXTCON_FAST_CHARGER] = "Fast-charger", 50f68a8342SChanwoo Choi [EXTCON_SLOW_CHARGER] = "Slow-charger", 51f68a8342SChanwoo Choi [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream", 52f68a8342SChanwoo Choi [EXTCON_HDMI] = "HDMI", 53f68a8342SChanwoo Choi [EXTCON_MHL] = "MHL", 54f68a8342SChanwoo Choi [EXTCON_DVI] = "DVI", 55f68a8342SChanwoo Choi [EXTCON_VGA] = "VGA", 56f68a8342SChanwoo Choi [EXTCON_DOCK] = "Dock", 57f68a8342SChanwoo Choi [EXTCON_LINE_IN] = "Line-in", 58f68a8342SChanwoo Choi [EXTCON_LINE_OUT] = "Line-out", 59f68a8342SChanwoo Choi [EXTCON_MIC_IN] = "Microphone", 60f68a8342SChanwoo Choi [EXTCON_HEADPHONE_OUT] = "Headphone", 61f68a8342SChanwoo Choi [EXTCON_SPDIF_IN] = "SPDIF-in", 62f68a8342SChanwoo Choi [EXTCON_SPDIF_OUT] = "SPDIF-out", 63f68a8342SChanwoo Choi [EXTCON_VIDEO_IN] = "Video-in", 64f68a8342SChanwoo Choi [EXTCON_VIDEO_OUT] = "Video-out", 65f68a8342SChanwoo Choi [EXTCON_MECHANICAL] = "Mechanical", 66f68a8342SChanwoo Choi }; 67f68a8342SChanwoo Choi 68f68a8342SChanwoo Choi static struct class *extcon_class; 69f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 70f68a8342SChanwoo Choi static struct class_compat *switch_class; 71f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 72f68a8342SChanwoo Choi 73f68a8342SChanwoo Choi static LIST_HEAD(extcon_dev_list); 74f68a8342SChanwoo Choi static DEFINE_MUTEX(extcon_dev_list_lock); 75f68a8342SChanwoo Choi 76f68a8342SChanwoo Choi /** 77f68a8342SChanwoo Choi * check_mutually_exclusive - Check if new_state violates mutually_exclusive 78f68a8342SChanwoo Choi * condition. 79f68a8342SChanwoo Choi * @edev: the extcon device 80f68a8342SChanwoo Choi * @new_state: new cable attach status for @edev 81f68a8342SChanwoo Choi * 82f68a8342SChanwoo Choi * Returns 0 if nothing violates. Returns the index + 1 for the first 83f68a8342SChanwoo Choi * violated condition. 84f68a8342SChanwoo Choi */ 85f68a8342SChanwoo Choi static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) 86f68a8342SChanwoo Choi { 87f68a8342SChanwoo Choi int i = 0; 88f68a8342SChanwoo Choi 89f68a8342SChanwoo Choi if (!edev->mutually_exclusive) 90f68a8342SChanwoo Choi return 0; 91f68a8342SChanwoo Choi 92f68a8342SChanwoo Choi for (i = 0; edev->mutually_exclusive[i]; i++) { 93f68a8342SChanwoo Choi int weight; 94f68a8342SChanwoo Choi u32 correspondants = new_state & edev->mutually_exclusive[i]; 95f68a8342SChanwoo Choi 96f68a8342SChanwoo Choi /* calculate the total number of bits set */ 97f68a8342SChanwoo Choi weight = hweight32(correspondants); 98f68a8342SChanwoo Choi if (weight > 1) 99f68a8342SChanwoo Choi return i + 1; 100f68a8342SChanwoo Choi } 101f68a8342SChanwoo Choi 102f68a8342SChanwoo Choi return 0; 103f68a8342SChanwoo Choi } 104f68a8342SChanwoo Choi 105f68a8342SChanwoo Choi static ssize_t state_show(struct device *dev, struct device_attribute *attr, 106f68a8342SChanwoo Choi char *buf) 107f68a8342SChanwoo Choi { 108f68a8342SChanwoo Choi int i, count = 0; 109f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 110f68a8342SChanwoo Choi 111f68a8342SChanwoo Choi if (edev->print_state) { 112f68a8342SChanwoo Choi int ret = edev->print_state(edev, buf); 113f68a8342SChanwoo Choi 114f68a8342SChanwoo Choi if (ret >= 0) 115f68a8342SChanwoo Choi return ret; 116f68a8342SChanwoo Choi /* Use default if failed */ 117f68a8342SChanwoo Choi } 118f68a8342SChanwoo Choi 119f68a8342SChanwoo Choi if (edev->max_supported == 0) 120f68a8342SChanwoo Choi return sprintf(buf, "%u\n", edev->state); 121f68a8342SChanwoo Choi 122f68a8342SChanwoo Choi for (i = 0; i < SUPPORTED_CABLE_MAX; i++) { 123f68a8342SChanwoo Choi if (!edev->supported_cable[i]) 124f68a8342SChanwoo Choi break; 125f68a8342SChanwoo Choi count += sprintf(buf + count, "%s=%d\n", 126f68a8342SChanwoo Choi edev->supported_cable[i], 127f68a8342SChanwoo Choi !!(edev->state & (1 << i))); 128f68a8342SChanwoo Choi } 129f68a8342SChanwoo Choi 130f68a8342SChanwoo Choi return count; 131f68a8342SChanwoo Choi } 132f68a8342SChanwoo Choi 133f68a8342SChanwoo Choi static ssize_t state_store(struct device *dev, struct device_attribute *attr, 134f68a8342SChanwoo Choi const char *buf, size_t count) 135f68a8342SChanwoo Choi { 136f68a8342SChanwoo Choi u32 state; 137f68a8342SChanwoo Choi ssize_t ret = 0; 138f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 139f68a8342SChanwoo Choi 140f68a8342SChanwoo Choi ret = sscanf(buf, "0x%x", &state); 141f68a8342SChanwoo Choi if (ret == 0) 142f68a8342SChanwoo Choi ret = -EINVAL; 143f68a8342SChanwoo Choi else 144f68a8342SChanwoo Choi ret = extcon_set_state(edev, state); 145f68a8342SChanwoo Choi 146f68a8342SChanwoo Choi if (ret < 0) 147f68a8342SChanwoo Choi return ret; 148f68a8342SChanwoo Choi 149f68a8342SChanwoo Choi return count; 150f68a8342SChanwoo Choi } 151f68a8342SChanwoo Choi static DEVICE_ATTR_RW(state); 152f68a8342SChanwoo Choi 153f68a8342SChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr, 154f68a8342SChanwoo Choi char *buf) 155f68a8342SChanwoo Choi { 156f68a8342SChanwoo Choi struct extcon_dev *edev = dev_get_drvdata(dev); 157f68a8342SChanwoo Choi 158f68a8342SChanwoo Choi /* Optional callback given by the user */ 159f68a8342SChanwoo Choi if (edev->print_name) { 160f68a8342SChanwoo Choi int ret = edev->print_name(edev, buf); 16134825e51SChanwoo Choi 162f68a8342SChanwoo Choi if (ret >= 0) 163f68a8342SChanwoo Choi return ret; 164f68a8342SChanwoo Choi } 165f68a8342SChanwoo Choi 16671c3ffa5SChanwoo Choi return sprintf(buf, "%s\n", edev->name); 167f68a8342SChanwoo Choi } 168f68a8342SChanwoo Choi static DEVICE_ATTR_RO(name); 169f68a8342SChanwoo Choi 170f68a8342SChanwoo Choi static ssize_t cable_name_show(struct device *dev, 171f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 172f68a8342SChanwoo Choi { 173f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 174f68a8342SChanwoo Choi attr_name); 175f68a8342SChanwoo Choi 176f68a8342SChanwoo Choi return sprintf(buf, "%s\n", 177f68a8342SChanwoo Choi cable->edev->supported_cable[cable->cable_index]); 178f68a8342SChanwoo Choi } 179f68a8342SChanwoo Choi 180f68a8342SChanwoo Choi static ssize_t cable_state_show(struct device *dev, 181f68a8342SChanwoo Choi struct device_attribute *attr, char *buf) 182f68a8342SChanwoo Choi { 183f68a8342SChanwoo Choi struct extcon_cable *cable = container_of(attr, struct extcon_cable, 184f68a8342SChanwoo Choi attr_state); 185f68a8342SChanwoo Choi 186f68a8342SChanwoo Choi return sprintf(buf, "%d\n", 187f68a8342SChanwoo Choi extcon_get_cable_state_(cable->edev, 188f68a8342SChanwoo Choi cable->cable_index)); 189f68a8342SChanwoo Choi } 190f68a8342SChanwoo Choi 191f68a8342SChanwoo Choi /** 192f68a8342SChanwoo Choi * extcon_update_state() - Update the cable attach states of the extcon device 193f68a8342SChanwoo Choi * only for the masked bits. 194f68a8342SChanwoo Choi * @edev: the extcon device 195f68a8342SChanwoo Choi * @mask: the bit mask to designate updated bits. 196f68a8342SChanwoo Choi * @state: new cable attach status for @edev 197f68a8342SChanwoo Choi * 198f68a8342SChanwoo Choi * Changing the state sends uevent with environment variable containing 199f68a8342SChanwoo Choi * the name of extcon device (envp[0]) and the state output (envp[1]). 200f68a8342SChanwoo Choi * Tizen uses this format for extcon device to get events from ports. 201f68a8342SChanwoo Choi * Android uses this format as well. 202f68a8342SChanwoo Choi * 203f68a8342SChanwoo Choi * Note that the notifier provides which bits are changed in the state 204f68a8342SChanwoo Choi * variable with the val parameter (second) to the callback. 205f68a8342SChanwoo Choi */ 206f68a8342SChanwoo Choi int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) 207f68a8342SChanwoo Choi { 208f68a8342SChanwoo Choi char name_buf[120]; 209f68a8342SChanwoo Choi char state_buf[120]; 210f68a8342SChanwoo Choi char *prop_buf; 211f68a8342SChanwoo Choi char *envp[3]; 212f68a8342SChanwoo Choi int env_offset = 0; 213f68a8342SChanwoo Choi int length; 214f68a8342SChanwoo Choi unsigned long flags; 215f68a8342SChanwoo Choi 216f68a8342SChanwoo Choi spin_lock_irqsave(&edev->lock, flags); 217f68a8342SChanwoo Choi 218f68a8342SChanwoo Choi if (edev->state != ((edev->state & ~mask) | (state & mask))) { 219f68a8342SChanwoo Choi u32 old_state = edev->state; 220f68a8342SChanwoo Choi 221f68a8342SChanwoo Choi if (check_mutually_exclusive(edev, (edev->state & ~mask) | 222f68a8342SChanwoo Choi (state & mask))) { 223f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 224f68a8342SChanwoo Choi return -EPERM; 225f68a8342SChanwoo Choi } 226f68a8342SChanwoo Choi 227f68a8342SChanwoo Choi edev->state &= ~mask; 228f68a8342SChanwoo Choi edev->state |= state & mask; 229f68a8342SChanwoo Choi 230f68a8342SChanwoo Choi raw_notifier_call_chain(&edev->nh, old_state, edev); 231f68a8342SChanwoo Choi /* This could be in interrupt handler */ 232f68a8342SChanwoo Choi prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); 233f68a8342SChanwoo Choi if (prop_buf) { 234f68a8342SChanwoo Choi length = name_show(&edev->dev, NULL, prop_buf); 235f68a8342SChanwoo Choi if (length > 0) { 236f68a8342SChanwoo Choi if (prop_buf[length - 1] == '\n') 237f68a8342SChanwoo Choi prop_buf[length - 1] = 0; 238f68a8342SChanwoo Choi snprintf(name_buf, sizeof(name_buf), 239f68a8342SChanwoo Choi "NAME=%s", prop_buf); 240f68a8342SChanwoo Choi envp[env_offset++] = name_buf; 241f68a8342SChanwoo Choi } 242f68a8342SChanwoo Choi length = state_show(&edev->dev, NULL, prop_buf); 243f68a8342SChanwoo Choi if (length > 0) { 244f68a8342SChanwoo Choi if (prop_buf[length - 1] == '\n') 245f68a8342SChanwoo Choi prop_buf[length - 1] = 0; 246f68a8342SChanwoo Choi snprintf(state_buf, sizeof(state_buf), 247f68a8342SChanwoo Choi "STATE=%s", prop_buf); 248f68a8342SChanwoo Choi envp[env_offset++] = state_buf; 249f68a8342SChanwoo Choi } 250f68a8342SChanwoo Choi envp[env_offset] = NULL; 251f68a8342SChanwoo Choi /* Unlock early before uevent */ 252f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 253f68a8342SChanwoo Choi 254f68a8342SChanwoo Choi kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); 255f68a8342SChanwoo Choi free_page((unsigned long)prop_buf); 256f68a8342SChanwoo Choi } else { 257f68a8342SChanwoo Choi /* Unlock early before uevent */ 258f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 259f68a8342SChanwoo Choi 260f68a8342SChanwoo Choi dev_err(&edev->dev, "out of memory in extcon_set_state\n"); 261f68a8342SChanwoo Choi kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); 262f68a8342SChanwoo Choi } 263f68a8342SChanwoo Choi } else { 264f68a8342SChanwoo Choi /* No changes */ 265f68a8342SChanwoo Choi spin_unlock_irqrestore(&edev->lock, flags); 266f68a8342SChanwoo Choi } 267f68a8342SChanwoo Choi 268f68a8342SChanwoo Choi return 0; 269f68a8342SChanwoo Choi } 270f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_update_state); 271f68a8342SChanwoo Choi 272f68a8342SChanwoo Choi /** 273f68a8342SChanwoo Choi * extcon_set_state() - Set the cable attach states of the extcon device. 274f68a8342SChanwoo Choi * @edev: the extcon device 275f68a8342SChanwoo Choi * @state: new cable attach status for @edev 276f68a8342SChanwoo Choi * 277f68a8342SChanwoo Choi * Note that notifier provides which bits are changed in the state 278f68a8342SChanwoo Choi * variable with the val parameter (second) to the callback. 279f68a8342SChanwoo Choi */ 280f68a8342SChanwoo Choi int extcon_set_state(struct extcon_dev *edev, u32 state) 281f68a8342SChanwoo Choi { 282f68a8342SChanwoo Choi return extcon_update_state(edev, 0xffffffff, state); 283f68a8342SChanwoo Choi } 284f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_state); 285f68a8342SChanwoo Choi 286f68a8342SChanwoo Choi /** 287f68a8342SChanwoo Choi * extcon_find_cable_index() - Get the cable index based on the cable name. 288f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 289f68a8342SChanwoo Choi * @cable_name: cable name to be searched. 290f68a8342SChanwoo Choi * 291f68a8342SChanwoo Choi * Note that accessing a cable state based on cable_index is faster than 292f68a8342SChanwoo Choi * cable_name because using cable_name induces a loop with strncmp(). 293f68a8342SChanwoo Choi * Thus, when get/set_cable_state is repeatedly used, using cable_index 294f68a8342SChanwoo Choi * is recommended. 295f68a8342SChanwoo Choi */ 296f68a8342SChanwoo Choi int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name) 297f68a8342SChanwoo Choi { 298f68a8342SChanwoo Choi int i; 299f68a8342SChanwoo Choi 300f68a8342SChanwoo Choi if (edev->supported_cable) { 301f68a8342SChanwoo Choi for (i = 0; edev->supported_cable[i]; i++) { 302f68a8342SChanwoo Choi if (!strncmp(edev->supported_cable[i], 303f68a8342SChanwoo Choi cable_name, CABLE_NAME_MAX)) 304f68a8342SChanwoo Choi return i; 305f68a8342SChanwoo Choi } 306f68a8342SChanwoo Choi } 307f68a8342SChanwoo Choi 308f68a8342SChanwoo Choi return -EINVAL; 309f68a8342SChanwoo Choi } 310f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_find_cable_index); 311f68a8342SChanwoo Choi 312f68a8342SChanwoo Choi /** 313f68a8342SChanwoo Choi * extcon_get_cable_state_() - Get the status of a specific cable. 314f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 315f68a8342SChanwoo Choi * @index: cable index that can be retrieved by extcon_find_cable_index(). 316f68a8342SChanwoo Choi */ 317f68a8342SChanwoo Choi int extcon_get_cable_state_(struct extcon_dev *edev, int index) 318f68a8342SChanwoo Choi { 319f68a8342SChanwoo Choi if (index < 0 || (edev->max_supported && edev->max_supported <= index)) 320f68a8342SChanwoo Choi return -EINVAL; 321f68a8342SChanwoo Choi 322f68a8342SChanwoo Choi return !!(edev->state & (1 << index)); 323f68a8342SChanwoo Choi } 324f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_cable_state_); 325f68a8342SChanwoo Choi 326f68a8342SChanwoo Choi /** 327f68a8342SChanwoo Choi * extcon_get_cable_state() - Get the status of a specific cable. 328f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 329f68a8342SChanwoo Choi * @cable_name: cable name. 330f68a8342SChanwoo Choi * 331f68a8342SChanwoo Choi * Note that this is slower than extcon_get_cable_state_. 332f68a8342SChanwoo Choi */ 333f68a8342SChanwoo Choi int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name) 334f68a8342SChanwoo Choi { 335f68a8342SChanwoo Choi return extcon_get_cable_state_(edev, extcon_find_cable_index 336f68a8342SChanwoo Choi (edev, cable_name)); 337f68a8342SChanwoo Choi } 338f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_cable_state); 339f68a8342SChanwoo Choi 340f68a8342SChanwoo Choi /** 341f68a8342SChanwoo Choi * extcon_set_cable_state_() - Set the status of a specific cable. 342f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 343f68a8342SChanwoo Choi * @index: cable index that can be retrieved by 344f68a8342SChanwoo Choi * extcon_find_cable_index(). 345f68a8342SChanwoo Choi * @cable_state: the new cable status. The default semantics is 346f68a8342SChanwoo Choi * true: attached / false: detached. 347f68a8342SChanwoo Choi */ 348f68a8342SChanwoo Choi int extcon_set_cable_state_(struct extcon_dev *edev, 349f68a8342SChanwoo Choi int index, bool cable_state) 350f68a8342SChanwoo Choi { 351f68a8342SChanwoo Choi u32 state; 352f68a8342SChanwoo Choi 353f68a8342SChanwoo Choi if (index < 0 || (edev->max_supported && edev->max_supported <= index)) 354f68a8342SChanwoo Choi return -EINVAL; 355f68a8342SChanwoo Choi 356f68a8342SChanwoo Choi state = cable_state ? (1 << index) : 0; 357f68a8342SChanwoo Choi return extcon_update_state(edev, 1 << index, state); 358f68a8342SChanwoo Choi } 359f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_cable_state_); 360f68a8342SChanwoo Choi 361f68a8342SChanwoo Choi /** 362f68a8342SChanwoo Choi * extcon_set_cable_state() - Set the status of a specific cable. 363f68a8342SChanwoo Choi * @edev: the extcon device that has the cable. 364f68a8342SChanwoo Choi * @cable_name: cable name. 365f68a8342SChanwoo Choi * @cable_state: the new cable status. The default semantics is 366f68a8342SChanwoo Choi * true: attached / false: detached. 367f68a8342SChanwoo Choi * 368f68a8342SChanwoo Choi * Note that this is slower than extcon_set_cable_state_. 369f68a8342SChanwoo Choi */ 370f68a8342SChanwoo Choi int extcon_set_cable_state(struct extcon_dev *edev, 371f68a8342SChanwoo Choi const char *cable_name, bool cable_state) 372f68a8342SChanwoo Choi { 373f68a8342SChanwoo Choi return extcon_set_cable_state_(edev, extcon_find_cable_index 374f68a8342SChanwoo Choi (edev, cable_name), cable_state); 375f68a8342SChanwoo Choi } 376f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_set_cable_state); 377f68a8342SChanwoo Choi 378f68a8342SChanwoo Choi /** 379f68a8342SChanwoo Choi * extcon_get_extcon_dev() - Get the extcon device instance from the name 380f68a8342SChanwoo Choi * @extcon_name: The extcon name provided with extcon_dev_register() 381f68a8342SChanwoo Choi */ 382f68a8342SChanwoo Choi struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) 383f68a8342SChanwoo Choi { 384f68a8342SChanwoo Choi struct extcon_dev *sd; 385f68a8342SChanwoo Choi 386f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 387f68a8342SChanwoo Choi list_for_each_entry(sd, &extcon_dev_list, entry) { 388f68a8342SChanwoo Choi if (!strcmp(sd->name, extcon_name)) 389f68a8342SChanwoo Choi goto out; 390f68a8342SChanwoo Choi } 391f68a8342SChanwoo Choi sd = NULL; 392f68a8342SChanwoo Choi out: 393f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 394f68a8342SChanwoo Choi return sd; 395f68a8342SChanwoo Choi } 396f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); 397f68a8342SChanwoo Choi 398f68a8342SChanwoo Choi static int _call_per_cable(struct notifier_block *nb, unsigned long val, 399f68a8342SChanwoo Choi void *ptr) 400f68a8342SChanwoo Choi { 401f68a8342SChanwoo Choi struct extcon_specific_cable_nb *obj = container_of(nb, 402f68a8342SChanwoo Choi struct extcon_specific_cable_nb, internal_nb); 403f68a8342SChanwoo Choi struct extcon_dev *edev = ptr; 404f68a8342SChanwoo Choi 405f68a8342SChanwoo Choi if ((val & (1 << obj->cable_index)) != 406f68a8342SChanwoo Choi (edev->state & (1 << obj->cable_index))) { 407f68a8342SChanwoo Choi bool cable_state = true; 408f68a8342SChanwoo Choi 409f68a8342SChanwoo Choi obj->previous_value = val; 410f68a8342SChanwoo Choi 411f68a8342SChanwoo Choi if (val & (1 << obj->cable_index)) 412f68a8342SChanwoo Choi cable_state = false; 413f68a8342SChanwoo Choi 414f68a8342SChanwoo Choi return obj->user_nb->notifier_call(obj->user_nb, 415f68a8342SChanwoo Choi cable_state, ptr); 416f68a8342SChanwoo Choi } 417f68a8342SChanwoo Choi 418f68a8342SChanwoo Choi return NOTIFY_OK; 419f68a8342SChanwoo Choi } 420f68a8342SChanwoo Choi 421f68a8342SChanwoo Choi /** 422f68a8342SChanwoo Choi * extcon_register_interest() - Register a notifier for a state change of a 423f68a8342SChanwoo Choi * specific cable, not an entier set of cables of a 424f68a8342SChanwoo Choi * extcon device. 425f68a8342SChanwoo Choi * @obj: an empty extcon_specific_cable_nb object to be returned. 426f68a8342SChanwoo Choi * @extcon_name: the name of extcon device. 427f68a8342SChanwoo Choi * if NULL, extcon_register_interest will register 428f68a8342SChanwoo Choi * every cable with the target cable_name given. 429f68a8342SChanwoo Choi * @cable_name: the target cable name. 430f68a8342SChanwoo Choi * @nb: the notifier block to get notified. 431f68a8342SChanwoo Choi * 432f68a8342SChanwoo Choi * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets 433f68a8342SChanwoo Choi * the struct for you. 434f68a8342SChanwoo Choi * 435f68a8342SChanwoo Choi * extcon_register_interest is a helper function for those who want to get 436f68a8342SChanwoo Choi * notification for a single specific cable's status change. If a user wants 437f68a8342SChanwoo Choi * to get notification for any changes of all cables of a extcon device, 438f68a8342SChanwoo Choi * he/she should use the general extcon_register_notifier(). 439f68a8342SChanwoo Choi * 440f68a8342SChanwoo Choi * Note that the second parameter given to the callback of nb (val) is 441f68a8342SChanwoo Choi * "old_state", not the current state. The current state can be retrieved 442f68a8342SChanwoo Choi * by looking at the third pameter (edev pointer)'s state value. 443f68a8342SChanwoo Choi */ 444f68a8342SChanwoo Choi int extcon_register_interest(struct extcon_specific_cable_nb *obj, 445f68a8342SChanwoo Choi const char *extcon_name, const char *cable_name, 446f68a8342SChanwoo Choi struct notifier_block *nb) 447f68a8342SChanwoo Choi { 44866bee35fSHans de Goede unsigned long flags; 44966bee35fSHans de Goede int ret; 45066bee35fSHans de Goede 451f68a8342SChanwoo Choi if (!obj || !cable_name || !nb) 452f68a8342SChanwoo Choi return -EINVAL; 453f68a8342SChanwoo Choi 454f68a8342SChanwoo Choi if (extcon_name) { 455f68a8342SChanwoo Choi obj->edev = extcon_get_extcon_dev(extcon_name); 456f68a8342SChanwoo Choi if (!obj->edev) 457f68a8342SChanwoo Choi return -ENODEV; 458f68a8342SChanwoo Choi 459f68a8342SChanwoo Choi obj->cable_index = extcon_find_cable_index(obj->edev, 460f68a8342SChanwoo Choi cable_name); 461f68a8342SChanwoo Choi if (obj->cable_index < 0) 462f68a8342SChanwoo Choi return obj->cable_index; 463f68a8342SChanwoo Choi 464f68a8342SChanwoo Choi obj->user_nb = nb; 465f68a8342SChanwoo Choi 466f68a8342SChanwoo Choi obj->internal_nb.notifier_call = _call_per_cable; 467f68a8342SChanwoo Choi 46866bee35fSHans de Goede spin_lock_irqsave(&obj->edev->lock, flags); 46966bee35fSHans de Goede ret = raw_notifier_chain_register(&obj->edev->nh, 470f68a8342SChanwoo Choi &obj->internal_nb); 47166bee35fSHans de Goede spin_unlock_irqrestore(&obj->edev->lock, flags); 47266bee35fSHans de Goede return ret; 473f68a8342SChanwoo Choi } else { 474f68a8342SChanwoo Choi struct class_dev_iter iter; 475f68a8342SChanwoo Choi struct extcon_dev *extd; 476f68a8342SChanwoo Choi struct device *dev; 477f68a8342SChanwoo Choi 478f68a8342SChanwoo Choi if (!extcon_class) 479f68a8342SChanwoo Choi return -ENODEV; 480f68a8342SChanwoo Choi class_dev_iter_init(&iter, extcon_class, NULL, NULL); 481f68a8342SChanwoo Choi while ((dev = class_dev_iter_next(&iter))) { 482f68a8342SChanwoo Choi extd = dev_get_drvdata(dev); 483f68a8342SChanwoo Choi 484f68a8342SChanwoo Choi if (extcon_find_cable_index(extd, cable_name) < 0) 485f68a8342SChanwoo Choi continue; 486f68a8342SChanwoo Choi 487f68a8342SChanwoo Choi class_dev_iter_exit(&iter); 488f68a8342SChanwoo Choi return extcon_register_interest(obj, extd->name, 489f68a8342SChanwoo Choi cable_name, nb); 490f68a8342SChanwoo Choi } 491f68a8342SChanwoo Choi 492f68a8342SChanwoo Choi return -ENODEV; 493f68a8342SChanwoo Choi } 494f68a8342SChanwoo Choi } 495f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_interest); 496f68a8342SChanwoo Choi 497f68a8342SChanwoo Choi /** 498f68a8342SChanwoo Choi * extcon_unregister_interest() - Unregister the notifier registered by 499f68a8342SChanwoo Choi * extcon_register_interest(). 500f68a8342SChanwoo Choi * @obj: the extcon_specific_cable_nb object returned by 501f68a8342SChanwoo Choi * extcon_register_interest(). 502f68a8342SChanwoo Choi */ 503f68a8342SChanwoo Choi int extcon_unregister_interest(struct extcon_specific_cable_nb *obj) 504f68a8342SChanwoo Choi { 50566bee35fSHans de Goede unsigned long flags; 50666bee35fSHans de Goede int ret; 50766bee35fSHans de Goede 508f68a8342SChanwoo Choi if (!obj) 509f68a8342SChanwoo Choi return -EINVAL; 510f68a8342SChanwoo Choi 51166bee35fSHans de Goede spin_lock_irqsave(&obj->edev->lock, flags); 51266bee35fSHans de Goede ret = raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb); 51366bee35fSHans de Goede spin_unlock_irqrestore(&obj->edev->lock, flags); 51466bee35fSHans de Goede 51566bee35fSHans de Goede return ret; 516f68a8342SChanwoo Choi } 517f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_interest); 518f68a8342SChanwoo Choi 519f68a8342SChanwoo Choi /** 520f68a8342SChanwoo Choi * extcon_register_notifier() - Register a notifiee to get notified by 521f68a8342SChanwoo Choi * any attach status changes from the extcon. 522f68a8342SChanwoo Choi * @edev: the extcon device. 523f68a8342SChanwoo Choi * @nb: a notifier block to be registered. 524f68a8342SChanwoo Choi * 525f68a8342SChanwoo Choi * Note that the second parameter given to the callback of nb (val) is 526f68a8342SChanwoo Choi * "old_state", not the current state. The current state can be retrieved 527f68a8342SChanwoo Choi * by looking at the third pameter (edev pointer)'s state value. 528f68a8342SChanwoo Choi */ 529f68a8342SChanwoo Choi int extcon_register_notifier(struct extcon_dev *edev, 530f68a8342SChanwoo Choi struct notifier_block *nb) 531f68a8342SChanwoo Choi { 53266bee35fSHans de Goede unsigned long flags; 53366bee35fSHans de Goede int ret; 53466bee35fSHans de Goede 53566bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 53666bee35fSHans de Goede ret = raw_notifier_chain_register(&edev->nh, nb); 53766bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 53866bee35fSHans de Goede 53966bee35fSHans de Goede return ret; 540f68a8342SChanwoo Choi } 541f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_register_notifier); 542f68a8342SChanwoo Choi 543f68a8342SChanwoo Choi /** 544f68a8342SChanwoo Choi * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. 545f68a8342SChanwoo Choi * @edev: the extcon device. 546f68a8342SChanwoo Choi * @nb: a registered notifier block to be unregistered. 547f68a8342SChanwoo Choi */ 548f68a8342SChanwoo Choi int extcon_unregister_notifier(struct extcon_dev *edev, 549f68a8342SChanwoo Choi struct notifier_block *nb) 550f68a8342SChanwoo Choi { 55166bee35fSHans de Goede unsigned long flags; 55266bee35fSHans de Goede int ret; 55366bee35fSHans de Goede 55466bee35fSHans de Goede spin_lock_irqsave(&edev->lock, flags); 55566bee35fSHans de Goede ret = raw_notifier_chain_unregister(&edev->nh, nb); 55666bee35fSHans de Goede spin_unlock_irqrestore(&edev->lock, flags); 55766bee35fSHans de Goede 55866bee35fSHans de Goede return ret; 559f68a8342SChanwoo Choi } 560f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_unregister_notifier); 561f68a8342SChanwoo Choi 562f68a8342SChanwoo Choi static struct attribute *extcon_attrs[] = { 563f68a8342SChanwoo Choi &dev_attr_state.attr, 564f68a8342SChanwoo Choi &dev_attr_name.attr, 565f68a8342SChanwoo Choi NULL, 566f68a8342SChanwoo Choi }; 567f68a8342SChanwoo Choi ATTRIBUTE_GROUPS(extcon); 568f68a8342SChanwoo Choi 569f68a8342SChanwoo Choi static int create_extcon_class(void) 570f68a8342SChanwoo Choi { 571f68a8342SChanwoo Choi if (!extcon_class) { 572f68a8342SChanwoo Choi extcon_class = class_create(THIS_MODULE, "extcon"); 573f68a8342SChanwoo Choi if (IS_ERR(extcon_class)) 574f68a8342SChanwoo Choi return PTR_ERR(extcon_class); 575f68a8342SChanwoo Choi extcon_class->dev_groups = extcon_groups; 576f68a8342SChanwoo Choi 577f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 578f68a8342SChanwoo Choi switch_class = class_compat_register("switch"); 579f68a8342SChanwoo Choi if (WARN(!switch_class, "cannot allocate")) 580f68a8342SChanwoo Choi return -ENOMEM; 581f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 582f68a8342SChanwoo Choi } 583f68a8342SChanwoo Choi 584f68a8342SChanwoo Choi return 0; 585f68a8342SChanwoo Choi } 586f68a8342SChanwoo Choi 587f68a8342SChanwoo Choi static void extcon_dev_release(struct device *dev) 588f68a8342SChanwoo Choi { 589f68a8342SChanwoo Choi } 590f68a8342SChanwoo Choi 591f68a8342SChanwoo Choi static const char *muex_name = "mutually_exclusive"; 592f68a8342SChanwoo Choi static void dummy_sysfs_dev_release(struct device *dev) 593f68a8342SChanwoo Choi { 594f68a8342SChanwoo Choi } 595f68a8342SChanwoo Choi 596f68a8342SChanwoo Choi /* 597f68a8342SChanwoo Choi * extcon_dev_allocate() - Allocate the memory of extcon device. 598f68a8342SChanwoo Choi * @supported_cable: Array of supported cable names ending with NULL. 599f68a8342SChanwoo Choi * If supported_cable is NULL, cable name related APIs 600f68a8342SChanwoo Choi * are disabled. 601f68a8342SChanwoo Choi * 602f68a8342SChanwoo Choi * This function allocates the memory for extcon device without allocating 603f68a8342SChanwoo Choi * memory in each extcon provider driver and initialize default setting for 604f68a8342SChanwoo Choi * extcon device. 605f68a8342SChanwoo Choi * 606f68a8342SChanwoo Choi * Return the pointer of extcon device if success or ERR_PTR(err) if fail 607f68a8342SChanwoo Choi */ 608f68a8342SChanwoo Choi struct extcon_dev *extcon_dev_allocate(const char **supported_cable) 609f68a8342SChanwoo Choi { 610f68a8342SChanwoo Choi struct extcon_dev *edev; 611f68a8342SChanwoo Choi 612f68a8342SChanwoo Choi edev = kzalloc(sizeof(*edev), GFP_KERNEL); 613f68a8342SChanwoo Choi if (!edev) 614f68a8342SChanwoo Choi return ERR_PTR(-ENOMEM); 615f68a8342SChanwoo Choi 616f68a8342SChanwoo Choi edev->max_supported = 0; 617f68a8342SChanwoo Choi edev->supported_cable = supported_cable; 618f68a8342SChanwoo Choi 619f68a8342SChanwoo Choi return edev; 620f68a8342SChanwoo Choi } 621f68a8342SChanwoo Choi 622f68a8342SChanwoo Choi /* 623f68a8342SChanwoo Choi * extcon_dev_free() - Free the memory of extcon device. 624f68a8342SChanwoo Choi * @edev: the extcon device to free 625f68a8342SChanwoo Choi */ 626f68a8342SChanwoo Choi void extcon_dev_free(struct extcon_dev *edev) 627f68a8342SChanwoo Choi { 628f68a8342SChanwoo Choi kfree(edev); 629f68a8342SChanwoo Choi } 630f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_free); 631f68a8342SChanwoo Choi 632f68a8342SChanwoo Choi static int devm_extcon_dev_match(struct device *dev, void *res, void *data) 633f68a8342SChanwoo Choi { 634f68a8342SChanwoo Choi struct extcon_dev **r = res; 635f68a8342SChanwoo Choi 636f68a8342SChanwoo Choi if (WARN_ON(!r || !*r)) 637f68a8342SChanwoo Choi return 0; 638f68a8342SChanwoo Choi 639f68a8342SChanwoo Choi return *r == data; 640f68a8342SChanwoo Choi } 641f68a8342SChanwoo Choi 642f68a8342SChanwoo Choi static void devm_extcon_dev_release(struct device *dev, void *res) 643f68a8342SChanwoo Choi { 644f68a8342SChanwoo Choi extcon_dev_free(*(struct extcon_dev **)res); 645f68a8342SChanwoo Choi } 646f68a8342SChanwoo Choi 647f68a8342SChanwoo Choi /** 648f68a8342SChanwoo Choi * devm_extcon_dev_allocate - Allocate managed extcon device 649f68a8342SChanwoo Choi * @dev: device owning the extcon device being created 650f68a8342SChanwoo Choi * @supported_cable: Array of supported cable names ending with NULL. 651f68a8342SChanwoo Choi * If supported_cable is NULL, cable name related APIs 652f68a8342SChanwoo Choi * are disabled. 653f68a8342SChanwoo Choi * 654f68a8342SChanwoo Choi * This function manages automatically the memory of extcon device using device 655f68a8342SChanwoo Choi * resource management and simplify the control of freeing the memory of extcon 656f68a8342SChanwoo Choi * device. 657f68a8342SChanwoo Choi * 658f68a8342SChanwoo Choi * Returns the pointer memory of allocated extcon_dev if success 659f68a8342SChanwoo Choi * or ERR_PTR(err) if fail 660f68a8342SChanwoo Choi */ 661f68a8342SChanwoo Choi struct extcon_dev *devm_extcon_dev_allocate(struct device *dev, 662f68a8342SChanwoo Choi const char **supported_cable) 663f68a8342SChanwoo Choi { 664f68a8342SChanwoo Choi struct extcon_dev **ptr, *edev; 665f68a8342SChanwoo Choi 666f68a8342SChanwoo Choi ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL); 667f68a8342SChanwoo Choi if (!ptr) 668f68a8342SChanwoo Choi return ERR_PTR(-ENOMEM); 669f68a8342SChanwoo Choi 670f68a8342SChanwoo Choi edev = extcon_dev_allocate(supported_cable); 671f68a8342SChanwoo Choi if (IS_ERR(edev)) { 672f68a8342SChanwoo Choi devres_free(ptr); 673f68a8342SChanwoo Choi return edev; 674f68a8342SChanwoo Choi } 675f68a8342SChanwoo Choi 676f68a8342SChanwoo Choi edev->dev.parent = dev; 677f68a8342SChanwoo Choi 678f68a8342SChanwoo Choi *ptr = edev; 679f68a8342SChanwoo Choi devres_add(dev, ptr); 680f68a8342SChanwoo Choi 681f68a8342SChanwoo Choi return edev; 682f68a8342SChanwoo Choi } 683f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate); 684f68a8342SChanwoo Choi 685f68a8342SChanwoo Choi void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev) 686f68a8342SChanwoo Choi { 687f68a8342SChanwoo Choi WARN_ON(devres_release(dev, devm_extcon_dev_release, 688f68a8342SChanwoo Choi devm_extcon_dev_match, edev)); 689f68a8342SChanwoo Choi } 690f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_free); 691f68a8342SChanwoo Choi 692f68a8342SChanwoo Choi /** 693f68a8342SChanwoo Choi * extcon_dev_register() - Register a new extcon device 694f68a8342SChanwoo Choi * @edev : the new extcon device (should be allocated before calling) 695f68a8342SChanwoo Choi * 696f68a8342SChanwoo Choi * Among the members of edev struct, please set the "user initializing data" 697f68a8342SChanwoo Choi * in any case and set the "optional callbacks" if required. However, please 698f68a8342SChanwoo Choi * do not set the values of "internal data", which are initialized by 699f68a8342SChanwoo Choi * this function. 700f68a8342SChanwoo Choi */ 701f68a8342SChanwoo Choi int extcon_dev_register(struct extcon_dev *edev) 702f68a8342SChanwoo Choi { 703f68a8342SChanwoo Choi int ret, index = 0; 70471c3ffa5SChanwoo Choi static atomic_t edev_no = ATOMIC_INIT(-1); 705f68a8342SChanwoo Choi 706f68a8342SChanwoo Choi if (!extcon_class) { 707f68a8342SChanwoo Choi ret = create_extcon_class(); 708f68a8342SChanwoo Choi if (ret < 0) 709f68a8342SChanwoo Choi return ret; 710f68a8342SChanwoo Choi } 711f68a8342SChanwoo Choi 712f68a8342SChanwoo Choi if (edev->supported_cable) { 713f68a8342SChanwoo Choi /* Get size of array */ 714f68a8342SChanwoo Choi for (index = 0; edev->supported_cable[index]; index++) 715f68a8342SChanwoo Choi ; 716f68a8342SChanwoo Choi edev->max_supported = index; 717f68a8342SChanwoo Choi } else { 718f68a8342SChanwoo Choi edev->max_supported = 0; 719f68a8342SChanwoo Choi } 720f68a8342SChanwoo Choi 721f68a8342SChanwoo Choi if (index > SUPPORTED_CABLE_MAX) { 722f68a8342SChanwoo Choi dev_err(&edev->dev, "extcon: maximum number of supported cables exceeded.\n"); 723f68a8342SChanwoo Choi return -EINVAL; 724f68a8342SChanwoo Choi } 725f68a8342SChanwoo Choi 726f68a8342SChanwoo Choi edev->dev.class = extcon_class; 727f68a8342SChanwoo Choi edev->dev.release = extcon_dev_release; 728f68a8342SChanwoo Choi 72971c3ffa5SChanwoo Choi edev->name = dev_name(edev->dev.parent); 730f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(edev->name)) { 731f68a8342SChanwoo Choi dev_err(&edev->dev, 732f68a8342SChanwoo Choi "extcon device name is null\n"); 733f68a8342SChanwoo Choi return -EINVAL; 734f68a8342SChanwoo Choi } 73571c3ffa5SChanwoo Choi dev_set_name(&edev->dev, "extcon%lu", 73671c3ffa5SChanwoo Choi (unsigned long)atomic_inc_return(&edev_no)); 737f68a8342SChanwoo Choi 738f68a8342SChanwoo Choi if (edev->max_supported) { 739f68a8342SChanwoo Choi char buf[10]; 740f68a8342SChanwoo Choi char *str; 741f68a8342SChanwoo Choi struct extcon_cable *cable; 742f68a8342SChanwoo Choi 743f68a8342SChanwoo Choi edev->cables = kzalloc(sizeof(struct extcon_cable) * 744f68a8342SChanwoo Choi edev->max_supported, GFP_KERNEL); 745f68a8342SChanwoo Choi if (!edev->cables) { 746f68a8342SChanwoo Choi ret = -ENOMEM; 747f68a8342SChanwoo Choi goto err_sysfs_alloc; 748f68a8342SChanwoo Choi } 749f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) { 750f68a8342SChanwoo Choi cable = &edev->cables[index]; 751f68a8342SChanwoo Choi 752f68a8342SChanwoo Choi snprintf(buf, 10, "cable.%d", index); 753f68a8342SChanwoo Choi str = kzalloc(sizeof(char) * (strlen(buf) + 1), 754f68a8342SChanwoo Choi GFP_KERNEL); 755f68a8342SChanwoo Choi if (!str) { 756f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 757f68a8342SChanwoo Choi cable = &edev->cables[index]; 758f68a8342SChanwoo Choi kfree(cable->attr_g.name); 759f68a8342SChanwoo Choi } 760f68a8342SChanwoo Choi ret = -ENOMEM; 761f68a8342SChanwoo Choi 762f68a8342SChanwoo Choi goto err_alloc_cables; 763f68a8342SChanwoo Choi } 764f68a8342SChanwoo Choi strcpy(str, buf); 765f68a8342SChanwoo Choi 766f68a8342SChanwoo Choi cable->edev = edev; 767f68a8342SChanwoo Choi cable->cable_index = index; 768f68a8342SChanwoo Choi cable->attrs[0] = &cable->attr_name.attr; 769f68a8342SChanwoo Choi cable->attrs[1] = &cable->attr_state.attr; 770f68a8342SChanwoo Choi cable->attrs[2] = NULL; 771f68a8342SChanwoo Choi cable->attr_g.name = str; 772f68a8342SChanwoo Choi cable->attr_g.attrs = cable->attrs; 773f68a8342SChanwoo Choi 774f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_name.attr); 775f68a8342SChanwoo Choi cable->attr_name.attr.name = "name"; 776f68a8342SChanwoo Choi cable->attr_name.attr.mode = 0444; 777f68a8342SChanwoo Choi cable->attr_name.show = cable_name_show; 778f68a8342SChanwoo Choi 779f68a8342SChanwoo Choi sysfs_attr_init(&cable->attr_state.attr); 780f68a8342SChanwoo Choi cable->attr_state.attr.name = "state"; 781f68a8342SChanwoo Choi cable->attr_state.attr.mode = 0444; 782f68a8342SChanwoo Choi cable->attr_state.show = cable_state_show; 783f68a8342SChanwoo Choi } 784f68a8342SChanwoo Choi } 785f68a8342SChanwoo Choi 786f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 787f68a8342SChanwoo Choi char buf[80]; 788f68a8342SChanwoo Choi char *name; 789f68a8342SChanwoo Choi 790f68a8342SChanwoo Choi /* Count the size of mutually_exclusive array */ 791f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 792f68a8342SChanwoo Choi ; 793f68a8342SChanwoo Choi 794f68a8342SChanwoo Choi edev->attrs_muex = kzalloc(sizeof(struct attribute *) * 795f68a8342SChanwoo Choi (index + 1), GFP_KERNEL); 796f68a8342SChanwoo Choi if (!edev->attrs_muex) { 797f68a8342SChanwoo Choi ret = -ENOMEM; 798f68a8342SChanwoo Choi goto err_muex; 799f68a8342SChanwoo Choi } 800f68a8342SChanwoo Choi 801f68a8342SChanwoo Choi edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * 802f68a8342SChanwoo Choi index, GFP_KERNEL); 803f68a8342SChanwoo Choi if (!edev->d_attrs_muex) { 804f68a8342SChanwoo Choi ret = -ENOMEM; 805f68a8342SChanwoo Choi kfree(edev->attrs_muex); 806f68a8342SChanwoo Choi goto err_muex; 807f68a8342SChanwoo Choi } 808f68a8342SChanwoo Choi 809f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) { 810f68a8342SChanwoo Choi sprintf(buf, "0x%x", edev->mutually_exclusive[index]); 811f68a8342SChanwoo Choi name = kzalloc(sizeof(char) * (strlen(buf) + 1), 812f68a8342SChanwoo Choi GFP_KERNEL); 813f68a8342SChanwoo Choi if (!name) { 814f68a8342SChanwoo Choi for (index--; index >= 0; index--) { 815f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr. 816f68a8342SChanwoo Choi name); 817f68a8342SChanwoo Choi } 818f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 819f68a8342SChanwoo Choi kfree(edev->attrs_muex); 820f68a8342SChanwoo Choi ret = -ENOMEM; 821f68a8342SChanwoo Choi goto err_muex; 822f68a8342SChanwoo Choi } 823f68a8342SChanwoo Choi strcpy(name, buf); 824f68a8342SChanwoo Choi sysfs_attr_init(&edev->d_attrs_muex[index].attr); 825f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.name = name; 826f68a8342SChanwoo Choi edev->d_attrs_muex[index].attr.mode = 0000; 827f68a8342SChanwoo Choi edev->attrs_muex[index] = &edev->d_attrs_muex[index] 828f68a8342SChanwoo Choi .attr; 829f68a8342SChanwoo Choi } 830f68a8342SChanwoo Choi edev->attr_g_muex.name = muex_name; 831f68a8342SChanwoo Choi edev->attr_g_muex.attrs = edev->attrs_muex; 832f68a8342SChanwoo Choi 833f68a8342SChanwoo Choi } 834f68a8342SChanwoo Choi 835f68a8342SChanwoo Choi if (edev->max_supported) { 836f68a8342SChanwoo Choi edev->extcon_dev_type.groups = 837f68a8342SChanwoo Choi kzalloc(sizeof(struct attribute_group *) * 838f68a8342SChanwoo Choi (edev->max_supported + 2), GFP_KERNEL); 839f68a8342SChanwoo Choi if (!edev->extcon_dev_type.groups) { 840f68a8342SChanwoo Choi ret = -ENOMEM; 841f68a8342SChanwoo Choi goto err_alloc_groups; 842f68a8342SChanwoo Choi } 843f68a8342SChanwoo Choi 844f68a8342SChanwoo Choi edev->extcon_dev_type.name = dev_name(&edev->dev); 845f68a8342SChanwoo Choi edev->extcon_dev_type.release = dummy_sysfs_dev_release; 846f68a8342SChanwoo Choi 847f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 848f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 849f68a8342SChanwoo Choi &edev->cables[index].attr_g; 850f68a8342SChanwoo Choi if (edev->mutually_exclusive) 851f68a8342SChanwoo Choi edev->extcon_dev_type.groups[index] = 852f68a8342SChanwoo Choi &edev->attr_g_muex; 853f68a8342SChanwoo Choi 854f68a8342SChanwoo Choi edev->dev.type = &edev->extcon_dev_type; 855f68a8342SChanwoo Choi } 856f68a8342SChanwoo Choi 857f68a8342SChanwoo Choi ret = device_register(&edev->dev); 858f68a8342SChanwoo Choi if (ret) { 859f68a8342SChanwoo Choi put_device(&edev->dev); 860f68a8342SChanwoo Choi goto err_dev; 861f68a8342SChanwoo Choi } 862f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 863f68a8342SChanwoo Choi if (switch_class) 864f68a8342SChanwoo Choi ret = class_compat_create_link(switch_class, &edev->dev, NULL); 865f68a8342SChanwoo Choi #endif /* CONFIG_ANDROID */ 866f68a8342SChanwoo Choi 867f68a8342SChanwoo Choi spin_lock_init(&edev->lock); 868f68a8342SChanwoo Choi 869f68a8342SChanwoo Choi RAW_INIT_NOTIFIER_HEAD(&edev->nh); 870f68a8342SChanwoo Choi 871f68a8342SChanwoo Choi dev_set_drvdata(&edev->dev, edev); 872f68a8342SChanwoo Choi edev->state = 0; 873f68a8342SChanwoo Choi 874f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 875f68a8342SChanwoo Choi list_add(&edev->entry, &extcon_dev_list); 876f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 877f68a8342SChanwoo Choi 878f68a8342SChanwoo Choi return 0; 879f68a8342SChanwoo Choi 880f68a8342SChanwoo Choi err_dev: 881f68a8342SChanwoo Choi if (edev->max_supported) 882f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 883f68a8342SChanwoo Choi err_alloc_groups: 884f68a8342SChanwoo Choi if (edev->max_supported && edev->mutually_exclusive) { 885f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; index++) 886f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 887f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 888f68a8342SChanwoo Choi kfree(edev->attrs_muex); 889f68a8342SChanwoo Choi } 890f68a8342SChanwoo Choi err_muex: 891f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 892f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 893f68a8342SChanwoo Choi err_alloc_cables: 894f68a8342SChanwoo Choi if (edev->max_supported) 895f68a8342SChanwoo Choi kfree(edev->cables); 896f68a8342SChanwoo Choi err_sysfs_alloc: 897f68a8342SChanwoo Choi return ret; 898f68a8342SChanwoo Choi } 899f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_register); 900f68a8342SChanwoo Choi 901f68a8342SChanwoo Choi /** 902f68a8342SChanwoo Choi * extcon_dev_unregister() - Unregister the extcon device. 903f68a8342SChanwoo Choi * @edev: the extcon device instance to be unregistered. 904f68a8342SChanwoo Choi * 905f68a8342SChanwoo Choi * Note that this does not call kfree(edev) because edev was not allocated 906f68a8342SChanwoo Choi * by this class. 907f68a8342SChanwoo Choi */ 908f68a8342SChanwoo Choi void extcon_dev_unregister(struct extcon_dev *edev) 909f68a8342SChanwoo Choi { 910f68a8342SChanwoo Choi int index; 911f68a8342SChanwoo Choi 912f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 913f68a8342SChanwoo Choi list_del(&edev->entry); 914f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 915f68a8342SChanwoo Choi 916f68a8342SChanwoo Choi if (IS_ERR_OR_NULL(get_device(&edev->dev))) { 917f68a8342SChanwoo Choi dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", 918f68a8342SChanwoo Choi dev_name(&edev->dev)); 919f68a8342SChanwoo Choi return; 920f68a8342SChanwoo Choi } 921f68a8342SChanwoo Choi 922f68a8342SChanwoo Choi device_unregister(&edev->dev); 923f68a8342SChanwoo Choi 924f68a8342SChanwoo Choi if (edev->mutually_exclusive && edev->max_supported) { 925f68a8342SChanwoo Choi for (index = 0; edev->mutually_exclusive[index]; 926f68a8342SChanwoo Choi index++) 927f68a8342SChanwoo Choi kfree(edev->d_attrs_muex[index].attr.name); 928f68a8342SChanwoo Choi kfree(edev->d_attrs_muex); 929f68a8342SChanwoo Choi kfree(edev->attrs_muex); 930f68a8342SChanwoo Choi } 931f68a8342SChanwoo Choi 932f68a8342SChanwoo Choi for (index = 0; index < edev->max_supported; index++) 933f68a8342SChanwoo Choi kfree(edev->cables[index].attr_g.name); 934f68a8342SChanwoo Choi 935f68a8342SChanwoo Choi if (edev->max_supported) { 936f68a8342SChanwoo Choi kfree(edev->extcon_dev_type.groups); 937f68a8342SChanwoo Choi kfree(edev->cables); 938f68a8342SChanwoo Choi } 939f68a8342SChanwoo Choi 940f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 941f68a8342SChanwoo Choi if (switch_class) 942f68a8342SChanwoo Choi class_compat_remove_link(switch_class, &edev->dev, NULL); 943f68a8342SChanwoo Choi #endif 944f68a8342SChanwoo Choi put_device(&edev->dev); 945f68a8342SChanwoo Choi } 946f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_dev_unregister); 947f68a8342SChanwoo Choi 948f68a8342SChanwoo Choi static void devm_extcon_dev_unreg(struct device *dev, void *res) 949f68a8342SChanwoo Choi { 950f68a8342SChanwoo Choi extcon_dev_unregister(*(struct extcon_dev **)res); 951f68a8342SChanwoo Choi } 952f68a8342SChanwoo Choi 953f68a8342SChanwoo Choi /** 954f68a8342SChanwoo Choi * devm_extcon_dev_register() - Resource-managed extcon_dev_register() 955f68a8342SChanwoo Choi * @dev: device to allocate extcon device 956f68a8342SChanwoo Choi * @edev: the new extcon device to register 957f68a8342SChanwoo Choi * 958f68a8342SChanwoo Choi * Managed extcon_dev_register() function. If extcon device is attached with 959f68a8342SChanwoo Choi * this function, that extcon device is automatically unregistered on driver 960f68a8342SChanwoo Choi * detach. Internally this function calls extcon_dev_register() function. 961f68a8342SChanwoo Choi * To get more information, refer that function. 962f68a8342SChanwoo Choi * 963f68a8342SChanwoo Choi * If extcon device is registered with this function and the device needs to be 964f68a8342SChanwoo Choi * unregistered separately, devm_extcon_dev_unregister() should be used. 965f68a8342SChanwoo Choi * 966f68a8342SChanwoo Choi * Returns 0 if success or negaive error number if failure. 967f68a8342SChanwoo Choi */ 968f68a8342SChanwoo Choi int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev) 969f68a8342SChanwoo Choi { 970f68a8342SChanwoo Choi struct extcon_dev **ptr; 971f68a8342SChanwoo Choi int ret; 972f68a8342SChanwoo Choi 973f68a8342SChanwoo Choi ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL); 974f68a8342SChanwoo Choi if (!ptr) 975f68a8342SChanwoo Choi return -ENOMEM; 976f68a8342SChanwoo Choi 977f68a8342SChanwoo Choi ret = extcon_dev_register(edev); 978f68a8342SChanwoo Choi if (ret) { 979f68a8342SChanwoo Choi devres_free(ptr); 980f68a8342SChanwoo Choi return ret; 981f68a8342SChanwoo Choi } 982f68a8342SChanwoo Choi 983f68a8342SChanwoo Choi *ptr = edev; 984f68a8342SChanwoo Choi devres_add(dev, ptr); 985f68a8342SChanwoo Choi 986f68a8342SChanwoo Choi return 0; 987f68a8342SChanwoo Choi } 988f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_register); 989f68a8342SChanwoo Choi 990f68a8342SChanwoo Choi /** 991f68a8342SChanwoo Choi * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister() 992f68a8342SChanwoo Choi * @dev: device the extcon belongs to 993f68a8342SChanwoo Choi * @edev: the extcon device to unregister 994f68a8342SChanwoo Choi * 995f68a8342SChanwoo Choi * Unregister extcon device that is registered with devm_extcon_dev_register() 996f68a8342SChanwoo Choi * function. 997f68a8342SChanwoo Choi */ 998f68a8342SChanwoo Choi void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev) 999f68a8342SChanwoo Choi { 1000f68a8342SChanwoo Choi WARN_ON(devres_release(dev, devm_extcon_dev_unreg, 1001f68a8342SChanwoo Choi devm_extcon_dev_match, edev)); 1002f68a8342SChanwoo Choi } 1003f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister); 1004f68a8342SChanwoo Choi 1005f68a8342SChanwoo Choi #ifdef CONFIG_OF 1006f68a8342SChanwoo Choi /* 1007f68a8342SChanwoo Choi * extcon_get_edev_by_phandle - Get the extcon device from devicetree 1008f68a8342SChanwoo Choi * @dev - instance to the given device 1009f68a8342SChanwoo Choi * @index - index into list of extcon_dev 1010f68a8342SChanwoo Choi * 1011f68a8342SChanwoo Choi * return the instance of extcon device 1012f68a8342SChanwoo Choi */ 1013f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1014f68a8342SChanwoo Choi { 1015f68a8342SChanwoo Choi struct device_node *node; 1016f68a8342SChanwoo Choi struct extcon_dev *edev; 1017f68a8342SChanwoo Choi 1018f68a8342SChanwoo Choi if (!dev->of_node) { 1019f68a8342SChanwoo Choi dev_err(dev, "device does not have a device node entry\n"); 1020f68a8342SChanwoo Choi return ERR_PTR(-EINVAL); 1021f68a8342SChanwoo Choi } 1022f68a8342SChanwoo Choi 1023f68a8342SChanwoo Choi node = of_parse_phandle(dev->of_node, "extcon", index); 1024f68a8342SChanwoo Choi if (!node) { 1025f68a8342SChanwoo Choi dev_err(dev, "failed to get phandle in %s node\n", 1026f68a8342SChanwoo Choi dev->of_node->full_name); 1027f68a8342SChanwoo Choi return ERR_PTR(-ENODEV); 1028f68a8342SChanwoo Choi } 1029f68a8342SChanwoo Choi 1030f68a8342SChanwoo Choi mutex_lock(&extcon_dev_list_lock); 1031f68a8342SChanwoo Choi list_for_each_entry(edev, &extcon_dev_list, entry) { 1032f68a8342SChanwoo Choi if (edev->dev.parent && edev->dev.parent->of_node == node) { 1033f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 1034f68a8342SChanwoo Choi return edev; 1035f68a8342SChanwoo Choi } 1036f68a8342SChanwoo Choi } 1037f68a8342SChanwoo Choi mutex_unlock(&extcon_dev_list_lock); 1038f68a8342SChanwoo Choi 1039f68a8342SChanwoo Choi return ERR_PTR(-EPROBE_DEFER); 1040f68a8342SChanwoo Choi } 1041f68a8342SChanwoo Choi #else 1042f68a8342SChanwoo Choi struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) 1043f68a8342SChanwoo Choi { 1044f68a8342SChanwoo Choi return ERR_PTR(-ENOSYS); 1045f68a8342SChanwoo Choi } 1046f68a8342SChanwoo Choi #endif /* CONFIG_OF */ 1047f68a8342SChanwoo Choi EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); 1048f68a8342SChanwoo Choi 1049707d7550SChanwoo Choi /** 1050707d7550SChanwoo Choi * extcon_get_edev_name() - Get the name of the extcon device. 1051707d7550SChanwoo Choi * @edev: the extcon device 1052707d7550SChanwoo Choi */ 1053707d7550SChanwoo Choi const char *extcon_get_edev_name(struct extcon_dev *edev) 1054707d7550SChanwoo Choi { 1055707d7550SChanwoo Choi return !edev ? NULL : edev->name; 1056707d7550SChanwoo Choi } 1057707d7550SChanwoo Choi 1058f68a8342SChanwoo Choi static int __init extcon_class_init(void) 1059f68a8342SChanwoo Choi { 1060f68a8342SChanwoo Choi return create_extcon_class(); 1061f68a8342SChanwoo Choi } 1062f68a8342SChanwoo Choi module_init(extcon_class_init); 1063f68a8342SChanwoo Choi 1064f68a8342SChanwoo Choi static void __exit extcon_class_exit(void) 1065f68a8342SChanwoo Choi { 1066f68a8342SChanwoo Choi #if defined(CONFIG_ANDROID) 1067f68a8342SChanwoo Choi class_compat_unregister(switch_class); 1068f68a8342SChanwoo Choi #endif 1069f68a8342SChanwoo Choi class_destroy(extcon_class); 1070f68a8342SChanwoo Choi } 1071f68a8342SChanwoo Choi module_exit(extcon_class_exit); 1072f68a8342SChanwoo Choi 1073f68a8342SChanwoo Choi MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); 1074f68a8342SChanwoo Choi MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 1075f68a8342SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 1076f68a8342SChanwoo Choi MODULE_DESCRIPTION("External connector (extcon) class driver"); 1077f68a8342SChanwoo Choi MODULE_LICENSE("GPL"); 1078