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