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