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