Commit | Line | Data |
---|---|---|
de55d871 | 1 | /* |
b9ec23c0 | 2 | * drivers/extcon/extcon.c - External Connector (extcon) framework. |
de55d871 MH |
3 | * |
4 | * External connector (extcon) class driver | |
5 | * | |
2a9de9c0 CC |
6 | * Copyright (C) 2015 Samsung Electronics |
7 | * Author: Chanwoo Choi <cw00.choi@samsung.com> | |
8 | * | |
de55d871 MH |
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. | |
b9ec23c0 | 25 | */ |
de55d871 MH |
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> | |
f841afb1 | 34 | #include <linux/of.h> |
de55d871 | 35 | #include <linux/slab.h> |
9baf3220 | 36 | #include <linux/sysfs.h> |
de55d871 | 37 | |
2a9de9c0 CC |
38 | #define SUPPORTED_CABLE_MAX 32 |
39 | #define CABLE_NAME_MAX 30 | |
40 | ||
41 | static const char *extcon_name[] = { | |
11eecf91 | 42 | [EXTCON_NONE] = "NONE", |
73b6ecdb | 43 | |
8e9bc36d | 44 | /* USB external connector */ |
11eecf91 CC |
45 | [EXTCON_USB] = "USB", |
46 | [EXTCON_USB_HOST] = "USB-HOST", | |
8e9bc36d | 47 | |
11eecf91 CC |
48 | /* Charging external connector */ |
49 | [EXTCON_CHG_USB_SDP] = "SDP", | |
50 | [EXTCON_CHG_USB_DCP] = "DCP", | |
51 | [EXTCON_CHG_USB_CDP] = "CDP", | |
52 | [EXTCON_CHG_USB_ACA] = "ACA", | |
53 | [EXTCON_CHG_USB_FAST] = "FAST-CHARGER", | |
54 | [EXTCON_CHG_USB_SLOW] = "SLOW-CHARGER", | |
8e9bc36d | 55 | |
11eecf91 CC |
56 | /* Jack external connector */ |
57 | [EXTCON_JACK_MICROPHONE] = "MICROPHONE", | |
58 | [EXTCON_JACK_HEADPHONE] = "HEADPHONE", | |
59 | [EXTCON_JACK_LINE_IN] = "LINE-IN", | |
60 | [EXTCON_JACK_LINE_OUT] = "LINE-OUT", | |
61 | [EXTCON_JACK_VIDEO_IN] = "VIDEO-IN", | |
62 | [EXTCON_JACK_VIDEO_OUT] = "VIDEO-OUT", | |
63 | [EXTCON_JACK_SPDIF_IN] = "SPDIF-IN", | |
64 | [EXTCON_JACK_SPDIF_OUT] = "SPDIF-OUT", | |
8e9bc36d | 65 | |
11eecf91 CC |
66 | /* Display external connector */ |
67 | [EXTCON_DISP_HDMI] = "HDMI", | |
68 | [EXTCON_DISP_MHL] = "MHL", | |
69 | [EXTCON_DISP_DVI] = "DVI", | |
70 | [EXTCON_DISP_VGA] = "VGA", | |
8e9bc36d | 71 | |
11eecf91 CC |
72 | /* Miscellaneous external connector */ |
73 | [EXTCON_DOCK] = "DOCK", | |
74 | [EXTCON_JIG] = "JIG", | |
75 | [EXTCON_MECHANICAL] = "MECHANICAL", | |
8e9bc36d | 76 | |
2a9de9c0 | 77 | NULL, |
806d9dd7 MH |
78 | }; |
79 | ||
be3a07f7 | 80 | static struct class *extcon_class; |
449a2bf5 | 81 | #if defined(CONFIG_ANDROID) |
de55d871 | 82 | static struct class_compat *switch_class; |
449a2bf5 | 83 | #endif /* CONFIG_ANDROID */ |
de55d871 | 84 | |
74c5d09b DK |
85 | static LIST_HEAD(extcon_dev_list); |
86 | static DEFINE_MUTEX(extcon_dev_list_lock); | |
87 | ||
bde68e60 MH |
88 | /** |
89 | * check_mutually_exclusive - Check if new_state violates mutually_exclusive | |
a75e1c73 | 90 | * condition. |
bde68e60 MH |
91 | * @edev: the extcon device |
92 | * @new_state: new cable attach status for @edev | |
93 | * | |
94 | * Returns 0 if nothing violates. Returns the index + 1 for the first | |
95 | * violated condition. | |
96 | */ | |
97 | static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) | |
98 | { | |
99 | int i = 0; | |
100 | ||
101 | if (!edev->mutually_exclusive) | |
102 | return 0; | |
103 | ||
104 | for (i = 0; edev->mutually_exclusive[i]; i++) { | |
28c0ada6 | 105 | int weight; |
bde68e60 | 106 | u32 correspondants = new_state & edev->mutually_exclusive[i]; |
bde68e60 | 107 | |
28c0ada6 | 108 | /* calculate the total number of bits set */ |
109 | weight = hweight32(correspondants); | |
110 | if (weight > 1) | |
111 | return i + 1; | |
bde68e60 MH |
112 | } |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
73b6ecdb | 117 | static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id) |
2a9de9c0 CC |
118 | { |
119 | int i; | |
120 | ||
121 | /* Find the the index of extcon cable in edev->supported_cable */ | |
122 | for (i = 0; i < edev->max_supported; i++) { | |
123 | if (edev->supported_cable[i] == id) | |
124 | return i; | |
125 | } | |
126 | ||
127 | return -EINVAL; | |
128 | } | |
129 | ||
046050f6 CC |
130 | static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached) |
131 | { | |
132 | if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) { | |
f4513b06 | 133 | *attached = ((new >> idx) & 0x1) ? true : false; |
046050f6 CC |
134 | return true; |
135 | } | |
136 | ||
137 | return false; | |
138 | } | |
139 | ||
de55d871 MH |
140 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
141 | char *buf) | |
142 | { | |
806d9dd7 | 143 | int i, count = 0; |
cb8bb3a7 | 144 | struct extcon_dev *edev = dev_get_drvdata(dev); |
de55d871 | 145 | |
806d9dd7 MH |
146 | if (edev->max_supported == 0) |
147 | return sprintf(buf, "%u\n", edev->state); | |
148 | ||
2a9de9c0 | 149 | for (i = 0; i < edev->max_supported; i++) { |
806d9dd7 | 150 | count += sprintf(buf + count, "%s=%d\n", |
2a9de9c0 | 151 | extcon_name[edev->supported_cable[i]], |
806d9dd7 MH |
152 | !!(edev->state & (1 << i))); |
153 | } | |
154 | ||
155 | return count; | |
156 | } | |
157 | ||
806d9dd7 MH |
158 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
159 | const char *buf, size_t count) | |
160 | { | |
161 | u32 state; | |
162 | ssize_t ret = 0; | |
cb8bb3a7 | 163 | struct extcon_dev *edev = dev_get_drvdata(dev); |
806d9dd7 MH |
164 | |
165 | ret = sscanf(buf, "0x%x", &state); | |
166 | if (ret == 0) | |
167 | ret = -EINVAL; | |
168 | else | |
bde68e60 | 169 | ret = extcon_set_state(edev, state); |
806d9dd7 MH |
170 | |
171 | if (ret < 0) | |
172 | return ret; | |
173 | ||
174 | return count; | |
de55d871 | 175 | } |
af01da0e | 176 | static DEVICE_ATTR_RW(state); |
de55d871 MH |
177 | |
178 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, | |
179 | char *buf) | |
180 | { | |
cb8bb3a7 | 181 | struct extcon_dev *edev = dev_get_drvdata(dev); |
de55d871 | 182 | |
71c3ffa5 | 183 | return sprintf(buf, "%s\n", edev->name); |
de55d871 | 184 | } |
af01da0e | 185 | static DEVICE_ATTR_RO(name); |
de55d871 | 186 | |
806d9dd7 MH |
187 | static ssize_t cable_name_show(struct device *dev, |
188 | struct device_attribute *attr, char *buf) | |
189 | { | |
190 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, | |
191 | attr_name); | |
2a9de9c0 | 192 | int i = cable->cable_index; |
806d9dd7 MH |
193 | |
194 | return sprintf(buf, "%s\n", | |
2a9de9c0 | 195 | extcon_name[cable->edev->supported_cable[i]]); |
806d9dd7 MH |
196 | } |
197 | ||
198 | static ssize_t cable_state_show(struct device *dev, | |
199 | struct device_attribute *attr, char *buf) | |
200 | { | |
201 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, | |
202 | attr_state); | |
203 | ||
be052cc8 RQ |
204 | int i = cable->cable_index; |
205 | ||
806d9dd7 MH |
206 | return sprintf(buf, "%d\n", |
207 | extcon_get_cable_state_(cable->edev, | |
be052cc8 | 208 | cable->edev->supported_cable[i])); |
806d9dd7 MH |
209 | } |
210 | ||
de55d871 | 211 | /** |
806d9dd7 | 212 | * extcon_update_state() - Update the cable attach states of the extcon device |
a75e1c73 | 213 | * only for the masked bits. |
de55d871 | 214 | * @edev: the extcon device |
806d9dd7 | 215 | * @mask: the bit mask to designate updated bits. |
de55d871 MH |
216 | * @state: new cable attach status for @edev |
217 | * | |
218 | * Changing the state sends uevent with environment variable containing | |
219 | * the name of extcon device (envp[0]) and the state output (envp[1]). | |
220 | * Tizen uses this format for extcon device to get events from ports. | |
221 | * Android uses this format as well. | |
74c5d09b | 222 | * |
806d9dd7 MH |
223 | * Note that the notifier provides which bits are changed in the state |
224 | * variable with the val parameter (second) to the callback. | |
de55d871 | 225 | */ |
bde68e60 | 226 | int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) |
de55d871 MH |
227 | { |
228 | char name_buf[120]; | |
229 | char state_buf[120]; | |
230 | char *prop_buf; | |
231 | char *envp[3]; | |
232 | int env_offset = 0; | |
233 | int length; | |
046050f6 | 234 | int index; |
806d9dd7 | 235 | unsigned long flags; |
046050f6 | 236 | bool attached; |
de55d871 | 237 | |
7eae43ae CC |
238 | if (!edev) |
239 | return -EINVAL; | |
240 | ||
806d9dd7 | 241 | spin_lock_irqsave(&edev->lock, flags); |
de55d871 | 242 | |
806d9dd7 | 243 | if (edev->state != ((edev->state & ~mask) | (state & mask))) { |
f7a89811 RQ |
244 | u32 old_state; |
245 | ||
bde68e60 MH |
246 | if (check_mutually_exclusive(edev, (edev->state & ~mask) | |
247 | (state & mask))) { | |
248 | spin_unlock_irqrestore(&edev->lock, flags); | |
249 | return -EPERM; | |
250 | } | |
251 | ||
f7a89811 | 252 | old_state = edev->state; |
806d9dd7 MH |
253 | edev->state &= ~mask; |
254 | edev->state |= state & mask; | |
255 | ||
f7a89811 RQ |
256 | for (index = 0; index < edev->max_supported; index++) { |
257 | if (is_extcon_changed(old_state, edev->state, index, | |
258 | &attached)) | |
259 | raw_notifier_call_chain(&edev->nh[index], | |
260 | attached, edev); | |
261 | } | |
262 | ||
806d9dd7 MH |
263 | /* This could be in interrupt handler */ |
264 | prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); | |
de55d871 | 265 | if (prop_buf) { |
dae61651 | 266 | length = name_show(&edev->dev, NULL, prop_buf); |
de55d871 MH |
267 | if (length > 0) { |
268 | if (prop_buf[length - 1] == '\n') | |
269 | prop_buf[length - 1] = 0; | |
270 | snprintf(name_buf, sizeof(name_buf), | |
271 | "NAME=%s", prop_buf); | |
272 | envp[env_offset++] = name_buf; | |
273 | } | |
dae61651 | 274 | length = state_show(&edev->dev, NULL, prop_buf); |
de55d871 MH |
275 | if (length > 0) { |
276 | if (prop_buf[length - 1] == '\n') | |
277 | prop_buf[length - 1] = 0; | |
278 | snprintf(state_buf, sizeof(state_buf), | |
279 | "STATE=%s", prop_buf); | |
280 | envp[env_offset++] = state_buf; | |
281 | } | |
282 | envp[env_offset] = NULL; | |
806d9dd7 MH |
283 | /* Unlock early before uevent */ |
284 | spin_unlock_irqrestore(&edev->lock, flags); | |
285 | ||
dae61651 | 286 | kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); |
de55d871 MH |
287 | free_page((unsigned long)prop_buf); |
288 | } else { | |
806d9dd7 MH |
289 | /* Unlock early before uevent */ |
290 | spin_unlock_irqrestore(&edev->lock, flags); | |
291 | ||
dae61651 CC |
292 | dev_err(&edev->dev, "out of memory in extcon_set_state\n"); |
293 | kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); | |
de55d871 | 294 | } |
806d9dd7 MH |
295 | } else { |
296 | /* No changes */ | |
297 | spin_unlock_irqrestore(&edev->lock, flags); | |
de55d871 | 298 | } |
bde68e60 MH |
299 | |
300 | return 0; | |
de55d871 | 301 | } |
806d9dd7 MH |
302 | EXPORT_SYMBOL_GPL(extcon_update_state); |
303 | ||
304 | /** | |
305 | * extcon_set_state() - Set the cable attach states of the extcon device. | |
306 | * @edev: the extcon device | |
307 | * @state: new cable attach status for @edev | |
308 | * | |
309 | * Note that notifier provides which bits are changed in the state | |
310 | * variable with the val parameter (second) to the callback. | |
311 | */ | |
bde68e60 | 312 | int extcon_set_state(struct extcon_dev *edev, u32 state) |
806d9dd7 | 313 | { |
7eae43ae CC |
314 | if (!edev) |
315 | return -EINVAL; | |
316 | ||
bde68e60 | 317 | return extcon_update_state(edev, 0xffffffff, state); |
806d9dd7 | 318 | } |
de55d871 MH |
319 | EXPORT_SYMBOL_GPL(extcon_set_state); |
320 | ||
806d9dd7 | 321 | /** |
2a9de9c0 | 322 | * extcon_get_cable_state_() - Get the status of a specific cable. |
806d9dd7 | 323 | * @edev: the extcon device that has the cable. |
2a9de9c0 | 324 | * @id: the unique id of each external connector in extcon enumeration. |
806d9dd7 | 325 | */ |
73b6ecdb | 326 | int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id) |
806d9dd7 | 327 | { |
2a9de9c0 | 328 | int index; |
806d9dd7 | 329 | |
7eae43ae CC |
330 | if (!edev) |
331 | return -EINVAL; | |
332 | ||
2a9de9c0 CC |
333 | index = find_cable_index_by_id(edev, id); |
334 | if (index < 0) | |
335 | return index; | |
806d9dd7 | 336 | |
2a9de9c0 | 337 | if (edev->max_supported && edev->max_supported <= index) |
806d9dd7 MH |
338 | return -EINVAL; |
339 | ||
340 | return !!(edev->state & (1 << index)); | |
341 | } | |
342 | EXPORT_SYMBOL_GPL(extcon_get_cable_state_); | |
343 | ||
806d9dd7 | 344 | /** |
909f9ec0 | 345 | * extcon_set_cable_state_() - Set the status of a specific cable. |
a75e1c73 | 346 | * @edev: the extcon device that has the cable. |
2a9de9c0 CC |
347 | * @id: the unique id of each external connector |
348 | * in extcon enumeration. | |
349 | * @state: the new cable status. The default semantics is | |
806d9dd7 MH |
350 | * true: attached / false: detached. |
351 | */ | |
73b6ecdb | 352 | int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id, |
2a9de9c0 | 353 | bool cable_state) |
806d9dd7 MH |
354 | { |
355 | u32 state; | |
2a9de9c0 | 356 | int index; |
806d9dd7 | 357 | |
7eae43ae CC |
358 | if (!edev) |
359 | return -EINVAL; | |
360 | ||
2a9de9c0 CC |
361 | index = find_cable_index_by_id(edev, id); |
362 | if (index < 0) | |
363 | return index; | |
364 | ||
365 | if (edev->max_supported && edev->max_supported <= index) | |
806d9dd7 MH |
366 | return -EINVAL; |
367 | ||
368 | state = cable_state ? (1 << index) : 0; | |
bde68e60 | 369 | return extcon_update_state(edev, 1 << index, state); |
806d9dd7 MH |
370 | } |
371 | EXPORT_SYMBOL_GPL(extcon_set_cable_state_); | |
372 | ||
74c5d09b DK |
373 | /** |
374 | * extcon_get_extcon_dev() - Get the extcon device instance from the name | |
375 | * @extcon_name: The extcon name provided with extcon_dev_register() | |
376 | */ | |
377 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) | |
378 | { | |
379 | struct extcon_dev *sd; | |
380 | ||
7eae43ae CC |
381 | if (!extcon_name) |
382 | return ERR_PTR(-EINVAL); | |
383 | ||
74c5d09b DK |
384 | mutex_lock(&extcon_dev_list_lock); |
385 | list_for_each_entry(sd, &extcon_dev_list, entry) { | |
386 | if (!strcmp(sd->name, extcon_name)) | |
387 | goto out; | |
388 | } | |
389 | sd = NULL; | |
390 | out: | |
391 | mutex_unlock(&extcon_dev_list_lock); | |
392 | return sd; | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); | |
395 | ||
396 | /** | |
c338bb03 | 397 | * extcon_register_notifier() - Register a notifiee to get notified by |
a75e1c73 | 398 | * any attach status changes from the extcon. |
046050f6 CC |
399 | * @edev: the extcon device that has the external connecotr. |
400 | * @id: the unique id of each external connector in extcon enumeration. | |
74c5d09b | 401 | * @nb: a notifier block to be registered. |
806d9dd7 MH |
402 | * |
403 | * Note that the second parameter given to the callback of nb (val) is | |
404 | * "old_state", not the current state. The current state can be retrieved | |
405 | * by looking at the third pameter (edev pointer)'s state value. | |
74c5d09b | 406 | */ |
73b6ecdb | 407 | int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, |
046050f6 | 408 | struct notifier_block *nb) |
74c5d09b | 409 | { |
66bee35f | 410 | unsigned long flags; |
046050f6 CC |
411 | int ret, idx; |
412 | ||
830ae442 | 413 | if (!nb) |
7eae43ae CC |
414 | return -EINVAL; |
415 | ||
830ae442 CC |
416 | if (edev) { |
417 | idx = find_cable_index_by_id(edev, id); | |
66bee35f | 418 | |
830ae442 CC |
419 | spin_lock_irqsave(&edev->lock, flags); |
420 | ret = raw_notifier_chain_register(&edev->nh[idx], nb); | |
421 | spin_unlock_irqrestore(&edev->lock, flags); | |
422 | } else { | |
423 | struct extcon_dev *extd; | |
424 | ||
425 | mutex_lock(&extcon_dev_list_lock); | |
426 | list_for_each_entry(extd, &extcon_dev_list, entry) { | |
427 | idx = find_cable_index_by_id(extd, id); | |
428 | if (idx >= 0) | |
429 | break; | |
430 | } | |
431 | mutex_unlock(&extcon_dev_list_lock); | |
432 | ||
433 | if (idx >= 0) { | |
434 | edev = extd; | |
435 | return extcon_register_notifier(extd, id, nb); | |
436 | } else { | |
437 | ret = -ENODEV; | |
438 | } | |
439 | } | |
66bee35f HG |
440 | |
441 | return ret; | |
74c5d09b DK |
442 | } |
443 | EXPORT_SYMBOL_GPL(extcon_register_notifier); | |
444 | ||
445 | /** | |
c338bb03 | 446 | * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. |
046050f6 CC |
447 | * @edev: the extcon device that has the external connecotr. |
448 | * @id: the unique id of each external connector in extcon enumeration. | |
449 | * @nb: a notifier block to be registered. | |
74c5d09b | 450 | */ |
73b6ecdb | 451 | int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, |
046050f6 | 452 | struct notifier_block *nb) |
74c5d09b | 453 | { |
66bee35f | 454 | unsigned long flags; |
046050f6 CC |
455 | int ret, idx; |
456 | ||
7eae43ae CC |
457 | if (!edev || !nb) |
458 | return -EINVAL; | |
459 | ||
046050f6 | 460 | idx = find_cable_index_by_id(edev, id); |
66bee35f HG |
461 | |
462 | spin_lock_irqsave(&edev->lock, flags); | |
046050f6 | 463 | ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); |
66bee35f HG |
464 | spin_unlock_irqrestore(&edev->lock, flags); |
465 | ||
466 | return ret; | |
74c5d09b DK |
467 | } |
468 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); | |
469 | ||
af01da0e GKH |
470 | static struct attribute *extcon_attrs[] = { |
471 | &dev_attr_state.attr, | |
472 | &dev_attr_name.attr, | |
473 | NULL, | |
de55d871 | 474 | }; |
af01da0e | 475 | ATTRIBUTE_GROUPS(extcon); |
de55d871 MH |
476 | |
477 | static int create_extcon_class(void) | |
478 | { | |
479 | if (!extcon_class) { | |
480 | extcon_class = class_create(THIS_MODULE, "extcon"); | |
481 | if (IS_ERR(extcon_class)) | |
482 | return PTR_ERR(extcon_class); | |
af01da0e | 483 | extcon_class->dev_groups = extcon_groups; |
de55d871 | 484 | |
449a2bf5 | 485 | #if defined(CONFIG_ANDROID) |
de55d871 MH |
486 | switch_class = class_compat_register("switch"); |
487 | if (WARN(!switch_class, "cannot allocate")) | |
488 | return -ENOMEM; | |
449a2bf5 | 489 | #endif /* CONFIG_ANDROID */ |
de55d871 MH |
490 | } |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
de55d871 MH |
495 | static void extcon_dev_release(struct device *dev) |
496 | { | |
de55d871 MH |
497 | } |
498 | ||
bde68e60 | 499 | static const char *muex_name = "mutually_exclusive"; |
806d9dd7 MH |
500 | static void dummy_sysfs_dev_release(struct device *dev) |
501 | { | |
502 | } | |
503 | ||
a9af6522 CC |
504 | /* |
505 | * extcon_dev_allocate() - Allocate the memory of extcon device. | |
2a9de9c0 | 506 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. |
a9af6522 CC |
507 | * If supported_cable is NULL, cable name related APIs |
508 | * are disabled. | |
509 | * | |
510 | * This function allocates the memory for extcon device without allocating | |
511 | * memory in each extcon provider driver and initialize default setting for | |
512 | * extcon device. | |
513 | * | |
514 | * Return the pointer of extcon device if success or ERR_PTR(err) if fail | |
515 | */ | |
73b6ecdb | 516 | struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) |
a9af6522 CC |
517 | { |
518 | struct extcon_dev *edev; | |
519 | ||
7eae43ae CC |
520 | if (!supported_cable) |
521 | return ERR_PTR(-EINVAL); | |
522 | ||
a9af6522 CC |
523 | edev = kzalloc(sizeof(*edev), GFP_KERNEL); |
524 | if (!edev) | |
525 | return ERR_PTR(-ENOMEM); | |
526 | ||
527 | edev->max_supported = 0; | |
528 | edev->supported_cable = supported_cable; | |
529 | ||
530 | return edev; | |
531 | } | |
532 | ||
533 | /* | |
534 | * extcon_dev_free() - Free the memory of extcon device. | |
535 | * @edev: the extcon device to free | |
536 | */ | |
537 | void extcon_dev_free(struct extcon_dev *edev) | |
538 | { | |
539 | kfree(edev); | |
540 | } | |
541 | EXPORT_SYMBOL_GPL(extcon_dev_free); | |
542 | ||
739ba1bf CC |
543 | static int devm_extcon_dev_match(struct device *dev, void *res, void *data) |
544 | { | |
545 | struct extcon_dev **r = res; | |
546 | ||
547 | if (WARN_ON(!r || !*r)) | |
548 | return 0; | |
549 | ||
550 | return *r == data; | |
551 | } | |
552 | ||
553 | static void devm_extcon_dev_release(struct device *dev, void *res) | |
554 | { | |
555 | extcon_dev_free(*(struct extcon_dev **)res); | |
556 | } | |
557 | ||
558 | /** | |
559 | * devm_extcon_dev_allocate - Allocate managed extcon device | |
560 | * @dev: device owning the extcon device being created | |
2a9de9c0 | 561 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. |
739ba1bf CC |
562 | * If supported_cable is NULL, cable name related APIs |
563 | * are disabled. | |
564 | * | |
565 | * This function manages automatically the memory of extcon device using device | |
566 | * resource management and simplify the control of freeing the memory of extcon | |
567 | * device. | |
568 | * | |
569 | * Returns the pointer memory of allocated extcon_dev if success | |
570 | * or ERR_PTR(err) if fail | |
571 | */ | |
572 | struct extcon_dev *devm_extcon_dev_allocate(struct device *dev, | |
73b6ecdb | 573 | const unsigned int *supported_cable) |
739ba1bf CC |
574 | { |
575 | struct extcon_dev **ptr, *edev; | |
576 | ||
577 | ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL); | |
578 | if (!ptr) | |
579 | return ERR_PTR(-ENOMEM); | |
580 | ||
581 | edev = extcon_dev_allocate(supported_cable); | |
582 | if (IS_ERR(edev)) { | |
583 | devres_free(ptr); | |
584 | return edev; | |
585 | } | |
586 | ||
ac65a625 CC |
587 | edev->dev.parent = dev; |
588 | ||
739ba1bf CC |
589 | *ptr = edev; |
590 | devres_add(dev, ptr); | |
591 | ||
592 | return edev; | |
593 | } | |
594 | EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate); | |
595 | ||
596 | void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev) | |
597 | { | |
598 | WARN_ON(devres_release(dev, devm_extcon_dev_release, | |
599 | devm_extcon_dev_match, edev)); | |
600 | } | |
601 | EXPORT_SYMBOL_GPL(devm_extcon_dev_free); | |
602 | ||
de55d871 MH |
603 | /** |
604 | * extcon_dev_register() - Register a new extcon device | |
605 | * @edev : the new extcon device (should be allocated before calling) | |
de55d871 MH |
606 | * |
607 | * Among the members of edev struct, please set the "user initializing data" | |
608 | * in any case and set the "optional callbacks" if required. However, please | |
609 | * do not set the values of "internal data", which are initialized by | |
610 | * this function. | |
611 | */ | |
42d7d753 | 612 | int extcon_dev_register(struct extcon_dev *edev) |
de55d871 | 613 | { |
806d9dd7 | 614 | int ret, index = 0; |
71c3ffa5 | 615 | static atomic_t edev_no = ATOMIC_INIT(-1); |
de55d871 MH |
616 | |
617 | if (!extcon_class) { | |
618 | ret = create_extcon_class(); | |
619 | if (ret < 0) | |
620 | return ret; | |
621 | } | |
622 | ||
7eae43ae | 623 | if (!edev || !edev->supported_cable) |
2a9de9c0 CC |
624 | return -EINVAL; |
625 | ||
626 | for (; edev->supported_cable[index] != EXTCON_NONE; index++); | |
806d9dd7 | 627 | |
2a9de9c0 | 628 | edev->max_supported = index; |
806d9dd7 | 629 | if (index > SUPPORTED_CABLE_MAX) { |
2a9de9c0 CC |
630 | dev_err(&edev->dev, |
631 | "exceed the maximum number of supported cables\n"); | |
806d9dd7 MH |
632 | return -EINVAL; |
633 | } | |
634 | ||
dae61651 CC |
635 | edev->dev.class = extcon_class; |
636 | edev->dev.release = extcon_dev_release; | |
de55d871 | 637 | |
71c3ffa5 | 638 | edev->name = dev_name(edev->dev.parent); |
42d7d753 CC |
639 | if (IS_ERR_OR_NULL(edev->name)) { |
640 | dev_err(&edev->dev, | |
641 | "extcon device name is null\n"); | |
642 | return -EINVAL; | |
643 | } | |
71c3ffa5 CC |
644 | dev_set_name(&edev->dev, "extcon%lu", |
645 | (unsigned long)atomic_inc_return(&edev_no)); | |
806d9dd7 MH |
646 | |
647 | if (edev->max_supported) { | |
648 | char buf[10]; | |
649 | char *str; | |
650 | struct extcon_cable *cable; | |
651 | ||
652 | edev->cables = kzalloc(sizeof(struct extcon_cable) * | |
653 | edev->max_supported, GFP_KERNEL); | |
654 | if (!edev->cables) { | |
655 | ret = -ENOMEM; | |
656 | goto err_sysfs_alloc; | |
657 | } | |
658 | for (index = 0; index < edev->max_supported; index++) { | |
659 | cable = &edev->cables[index]; | |
660 | ||
661 | snprintf(buf, 10, "cable.%d", index); | |
662 | str = kzalloc(sizeof(char) * (strlen(buf) + 1), | |
663 | GFP_KERNEL); | |
664 | if (!str) { | |
665 | for (index--; index >= 0; index--) { | |
666 | cable = &edev->cables[index]; | |
667 | kfree(cable->attr_g.name); | |
668 | } | |
669 | ret = -ENOMEM; | |
670 | ||
671 | goto err_alloc_cables; | |
672 | } | |
673 | strcpy(str, buf); | |
674 | ||
675 | cable->edev = edev; | |
676 | cable->cable_index = index; | |
677 | cable->attrs[0] = &cable->attr_name.attr; | |
678 | cable->attrs[1] = &cable->attr_state.attr; | |
679 | cable->attrs[2] = NULL; | |
680 | cable->attr_g.name = str; | |
681 | cable->attr_g.attrs = cable->attrs; | |
682 | ||
9baf3220 | 683 | sysfs_attr_init(&cable->attr_name.attr); |
806d9dd7 MH |
684 | cable->attr_name.attr.name = "name"; |
685 | cable->attr_name.attr.mode = 0444; | |
686 | cable->attr_name.show = cable_name_show; | |
687 | ||
9baf3220 | 688 | sysfs_attr_init(&cable->attr_state.attr); |
806d9dd7 | 689 | cable->attr_state.attr.name = "state"; |
ea9dd9d6 | 690 | cable->attr_state.attr.mode = 0444; |
806d9dd7 | 691 | cable->attr_state.show = cable_state_show; |
806d9dd7 MH |
692 | } |
693 | } | |
694 | ||
bde68e60 MH |
695 | if (edev->max_supported && edev->mutually_exclusive) { |
696 | char buf[80]; | |
697 | char *name; | |
698 | ||
699 | /* Count the size of mutually_exclusive array */ | |
700 | for (index = 0; edev->mutually_exclusive[index]; index++) | |
701 | ; | |
702 | ||
703 | edev->attrs_muex = kzalloc(sizeof(struct attribute *) * | |
704 | (index + 1), GFP_KERNEL); | |
705 | if (!edev->attrs_muex) { | |
706 | ret = -ENOMEM; | |
707 | goto err_muex; | |
708 | } | |
709 | ||
710 | edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * | |
711 | index, GFP_KERNEL); | |
712 | if (!edev->d_attrs_muex) { | |
713 | ret = -ENOMEM; | |
714 | kfree(edev->attrs_muex); | |
715 | goto err_muex; | |
716 | } | |
717 | ||
718 | for (index = 0; edev->mutually_exclusive[index]; index++) { | |
719 | sprintf(buf, "0x%x", edev->mutually_exclusive[index]); | |
720 | name = kzalloc(sizeof(char) * (strlen(buf) + 1), | |
721 | GFP_KERNEL); | |
722 | if (!name) { | |
723 | for (index--; index >= 0; index--) { | |
724 | kfree(edev->d_attrs_muex[index].attr. | |
725 | name); | |
726 | } | |
727 | kfree(edev->d_attrs_muex); | |
728 | kfree(edev->attrs_muex); | |
729 | ret = -ENOMEM; | |
730 | goto err_muex; | |
731 | } | |
732 | strcpy(name, buf); | |
9baf3220 | 733 | sysfs_attr_init(&edev->d_attrs_muex[index].attr); |
bde68e60 MH |
734 | edev->d_attrs_muex[index].attr.name = name; |
735 | edev->d_attrs_muex[index].attr.mode = 0000; | |
736 | edev->attrs_muex[index] = &edev->d_attrs_muex[index] | |
737 | .attr; | |
738 | } | |
739 | edev->attr_g_muex.name = muex_name; | |
740 | edev->attr_g_muex.attrs = edev->attrs_muex; | |
741 | ||
742 | } | |
743 | ||
806d9dd7 MH |
744 | if (edev->max_supported) { |
745 | edev->extcon_dev_type.groups = | |
746 | kzalloc(sizeof(struct attribute_group *) * | |
bde68e60 | 747 | (edev->max_supported + 2), GFP_KERNEL); |
806d9dd7 MH |
748 | if (!edev->extcon_dev_type.groups) { |
749 | ret = -ENOMEM; | |
750 | goto err_alloc_groups; | |
751 | } | |
752 | ||
dae61651 | 753 | edev->extcon_dev_type.name = dev_name(&edev->dev); |
806d9dd7 MH |
754 | edev->extcon_dev_type.release = dummy_sysfs_dev_release; |
755 | ||
756 | for (index = 0; index < edev->max_supported; index++) | |
757 | edev->extcon_dev_type.groups[index] = | |
758 | &edev->cables[index].attr_g; | |
bde68e60 MH |
759 | if (edev->mutually_exclusive) |
760 | edev->extcon_dev_type.groups[index] = | |
761 | &edev->attr_g_muex; | |
806d9dd7 | 762 | |
dae61651 | 763 | edev->dev.type = &edev->extcon_dev_type; |
806d9dd7 MH |
764 | } |
765 | ||
dae61651 | 766 | ret = device_register(&edev->dev); |
de55d871 | 767 | if (ret) { |
dae61651 | 768 | put_device(&edev->dev); |
de55d871 MH |
769 | goto err_dev; |
770 | } | |
449a2bf5 | 771 | #if defined(CONFIG_ANDROID) |
de55d871 | 772 | if (switch_class) |
dae61651 | 773 | ret = class_compat_create_link(switch_class, &edev->dev, NULL); |
449a2bf5 | 774 | #endif /* CONFIG_ANDROID */ |
de55d871 | 775 | |
806d9dd7 MH |
776 | spin_lock_init(&edev->lock); |
777 | ||
046050f6 CC |
778 | edev->nh = devm_kzalloc(&edev->dev, |
779 | sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL); | |
780 | if (!edev->nh) { | |
781 | ret = -ENOMEM; | |
782 | goto err_dev; | |
783 | } | |
784 | ||
785 | for (index = 0; index < edev->max_supported; index++) | |
786 | RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); | |
74c5d09b | 787 | |
dae61651 | 788 | dev_set_drvdata(&edev->dev, edev); |
de55d871 | 789 | edev->state = 0; |
74c5d09b DK |
790 | |
791 | mutex_lock(&extcon_dev_list_lock); | |
792 | list_add(&edev->entry, &extcon_dev_list); | |
793 | mutex_unlock(&extcon_dev_list_lock); | |
794 | ||
de55d871 MH |
795 | return 0; |
796 | ||
797 | err_dev: | |
806d9dd7 MH |
798 | if (edev->max_supported) |
799 | kfree(edev->extcon_dev_type.groups); | |
800 | err_alloc_groups: | |
bde68e60 MH |
801 | if (edev->max_supported && edev->mutually_exclusive) { |
802 | for (index = 0; edev->mutually_exclusive[index]; index++) | |
803 | kfree(edev->d_attrs_muex[index].attr.name); | |
804 | kfree(edev->d_attrs_muex); | |
805 | kfree(edev->attrs_muex); | |
806 | } | |
807 | err_muex: | |
806d9dd7 MH |
808 | for (index = 0; index < edev->max_supported; index++) |
809 | kfree(edev->cables[index].attr_g.name); | |
810 | err_alloc_cables: | |
811 | if (edev->max_supported) | |
812 | kfree(edev->cables); | |
813 | err_sysfs_alloc: | |
de55d871 MH |
814 | return ret; |
815 | } | |
816 | EXPORT_SYMBOL_GPL(extcon_dev_register); | |
817 | ||
818 | /** | |
819 | * extcon_dev_unregister() - Unregister the extcon device. | |
c338bb03 | 820 | * @edev: the extcon device instance to be unregistered. |
de55d871 MH |
821 | * |
822 | * Note that this does not call kfree(edev) because edev was not allocated | |
823 | * by this class. | |
824 | */ | |
825 | void extcon_dev_unregister(struct extcon_dev *edev) | |
826 | { | |
57e7cd37 | 827 | int index; |
828 | ||
7eae43ae CC |
829 | if (!edev) |
830 | return; | |
831 | ||
57e7cd37 | 832 | mutex_lock(&extcon_dev_list_lock); |
833 | list_del(&edev->entry); | |
834 | mutex_unlock(&extcon_dev_list_lock); | |
835 | ||
dae61651 CC |
836 | if (IS_ERR_OR_NULL(get_device(&edev->dev))) { |
837 | dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", | |
838 | dev_name(&edev->dev)); | |
57e7cd37 | 839 | return; |
840 | } | |
841 | ||
7585ca0d WX |
842 | device_unregister(&edev->dev); |
843 | ||
57e7cd37 | 844 | if (edev->mutually_exclusive && edev->max_supported) { |
845 | for (index = 0; edev->mutually_exclusive[index]; | |
846 | index++) | |
847 | kfree(edev->d_attrs_muex[index].attr.name); | |
848 | kfree(edev->d_attrs_muex); | |
849 | kfree(edev->attrs_muex); | |
850 | } | |
851 | ||
852 | for (index = 0; index < edev->max_supported; index++) | |
853 | kfree(edev->cables[index].attr_g.name); | |
854 | ||
855 | if (edev->max_supported) { | |
856 | kfree(edev->extcon_dev_type.groups); | |
857 | kfree(edev->cables); | |
858 | } | |
859 | ||
860 | #if defined(CONFIG_ANDROID) | |
861 | if (switch_class) | |
dae61651 | 862 | class_compat_remove_link(switch_class, &edev->dev, NULL); |
57e7cd37 | 863 | #endif |
dae61651 | 864 | put_device(&edev->dev); |
de55d871 MH |
865 | } |
866 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); | |
867 | ||
1111244f SW |
868 | static void devm_extcon_dev_unreg(struct device *dev, void *res) |
869 | { | |
870 | extcon_dev_unregister(*(struct extcon_dev **)res); | |
871 | } | |
872 | ||
1111244f SW |
873 | /** |
874 | * devm_extcon_dev_register() - Resource-managed extcon_dev_register() | |
875 | * @dev: device to allocate extcon device | |
876 | * @edev: the new extcon device to register | |
877 | * | |
878 | * Managed extcon_dev_register() function. If extcon device is attached with | |
879 | * this function, that extcon device is automatically unregistered on driver | |
880 | * detach. Internally this function calls extcon_dev_register() function. | |
881 | * To get more information, refer that function. | |
882 | * | |
883 | * If extcon device is registered with this function and the device needs to be | |
884 | * unregistered separately, devm_extcon_dev_unregister() should be used. | |
885 | * | |
886 | * Returns 0 if success or negaive error number if failure. | |
887 | */ | |
888 | int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev) | |
889 | { | |
890 | struct extcon_dev **ptr; | |
891 | int ret; | |
892 | ||
893 | ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL); | |
894 | if (!ptr) | |
895 | return -ENOMEM; | |
896 | ||
897 | ret = extcon_dev_register(edev); | |
898 | if (ret) { | |
899 | devres_free(ptr); | |
900 | return ret; | |
901 | } | |
902 | ||
903 | *ptr = edev; | |
904 | devres_add(dev, ptr); | |
905 | ||
906 | return 0; | |
907 | } | |
908 | EXPORT_SYMBOL_GPL(devm_extcon_dev_register); | |
909 | ||
910 | /** | |
911 | * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister() | |
912 | * @dev: device the extcon belongs to | |
913 | * @edev: the extcon device to unregister | |
914 | * | |
915 | * Unregister extcon device that is registered with devm_extcon_dev_register() | |
916 | * function. | |
917 | */ | |
918 | void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev) | |
919 | { | |
920 | WARN_ON(devres_release(dev, devm_extcon_dev_unreg, | |
921 | devm_extcon_dev_match, edev)); | |
922 | } | |
923 | EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister); | |
924 | ||
1ad94ffe CC |
925 | #ifdef CONFIG_OF |
926 | /* | |
927 | * extcon_get_edev_by_phandle - Get the extcon device from devicetree | |
928 | * @dev - instance to the given device | |
929 | * @index - index into list of extcon_dev | |
930 | * | |
931 | * return the instance of extcon device | |
932 | */ | |
933 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) | |
934 | { | |
935 | struct device_node *node; | |
936 | struct extcon_dev *edev; | |
937 | ||
7eae43ae CC |
938 | if (!dev) |
939 | return ERR_PTR(-EINVAL); | |
940 | ||
1ad94ffe CC |
941 | if (!dev->of_node) { |
942 | dev_err(dev, "device does not have a device node entry\n"); | |
943 | return ERR_PTR(-EINVAL); | |
944 | } | |
945 | ||
946 | node = of_parse_phandle(dev->of_node, "extcon", index); | |
947 | if (!node) { | |
948 | dev_err(dev, "failed to get phandle in %s node\n", | |
949 | dev->of_node->full_name); | |
950 | return ERR_PTR(-ENODEV); | |
951 | } | |
952 | ||
f841afb1 TF |
953 | mutex_lock(&extcon_dev_list_lock); |
954 | list_for_each_entry(edev, &extcon_dev_list, entry) { | |
955 | if (edev->dev.parent && edev->dev.parent->of_node == node) { | |
956 | mutex_unlock(&extcon_dev_list_lock); | |
957 | return edev; | |
958 | } | |
1ad94ffe | 959 | } |
f841afb1 | 960 | mutex_unlock(&extcon_dev_list_lock); |
1ad94ffe | 961 | |
f841afb1 | 962 | return ERR_PTR(-EPROBE_DEFER); |
1ad94ffe CC |
963 | } |
964 | #else | |
965 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) | |
966 | { | |
967 | return ERR_PTR(-ENOSYS); | |
968 | } | |
969 | #endif /* CONFIG_OF */ | |
970 | EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); | |
971 | ||
707d7550 CC |
972 | /** |
973 | * extcon_get_edev_name() - Get the name of the extcon device. | |
974 | * @edev: the extcon device | |
975 | */ | |
976 | const char *extcon_get_edev_name(struct extcon_dev *edev) | |
977 | { | |
978 | return !edev ? NULL : edev->name; | |
979 | } | |
980 | ||
de55d871 MH |
981 | static int __init extcon_class_init(void) |
982 | { | |
983 | return create_extcon_class(); | |
984 | } | |
985 | module_init(extcon_class_init); | |
986 | ||
987 | static void __exit extcon_class_exit(void) | |
988 | { | |
0dc77b6d PH |
989 | #if defined(CONFIG_ANDROID) |
990 | class_compat_unregister(switch_class); | |
991 | #endif | |
de55d871 MH |
992 | class_destroy(extcon_class); |
993 | } | |
994 | module_exit(extcon_class_exit); | |
995 | ||
2a9de9c0 | 996 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
de55d871 MH |
997 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
998 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); | |
999 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
1000 | MODULE_DESCRIPTION("External connector (extcon) class driver"); | |
1001 | MODULE_LICENSE("GPL"); |