Merge branch 'ib-extcon-powersupply-4.8' into extcon-next
[deliverable/linux.git] / drivers / extcon / extcon.c
1 /*
2 * drivers/extcon/extcon.c - External Connector (extcon) framework.
3 *
4 * External connector (extcon) class driver
5 *
6 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 *
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.
25 */
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>
34 #include <linux/of.h>
35 #include <linux/slab.h>
36 #include <linux/sysfs.h>
37
38 #define SUPPORTED_CABLE_MAX 32
39 #define CABLE_NAME_MAX 30
40
41 static const char *extcon_name[] = {
42 [EXTCON_NONE] = "NONE",
43
44 /* USB external connector */
45 [EXTCON_USB] = "USB",
46 [EXTCON_USB_HOST] = "USB-HOST",
47
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",
55
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",
65
66 /* Display external connector */
67 [EXTCON_DISP_HDMI] = "HDMI",
68 [EXTCON_DISP_MHL] = "MHL",
69 [EXTCON_DISP_DVI] = "DVI",
70 [EXTCON_DISP_VGA] = "VGA",
71
72 /* Miscellaneous external connector */
73 [EXTCON_DOCK] = "DOCK",
74 [EXTCON_JIG] = "JIG",
75 [EXTCON_MECHANICAL] = "MECHANICAL",
76
77 NULL,
78 };
79
80 static struct class *extcon_class;
81 #if defined(CONFIG_ANDROID)
82 static struct class_compat *switch_class;
83 #endif /* CONFIG_ANDROID */
84
85 static LIST_HEAD(extcon_dev_list);
86 static DEFINE_MUTEX(extcon_dev_list_lock);
87
88 /**
89 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
90 * condition.
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++) {
105 int weight;
106 u32 correspondants = new_state & edev->mutually_exclusive[i];
107
108 /* calculate the total number of bits set */
109 weight = hweight32(correspondants);
110 if (weight > 1)
111 return i + 1;
112 }
113
114 return 0;
115 }
116
117 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
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
130 static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
131 {
132 if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
133 *attached = ((new >> idx) & 0x1) ? true : false;
134 return true;
135 }
136
137 return false;
138 }
139
140 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
141 char *buf)
142 {
143 int i, count = 0;
144 struct extcon_dev *edev = dev_get_drvdata(dev);
145
146 if (edev->max_supported == 0)
147 return sprintf(buf, "%u\n", edev->state);
148
149 for (i = 0; i < edev->max_supported; i++) {
150 count += sprintf(buf + count, "%s=%d\n",
151 extcon_name[edev->supported_cable[i]],
152 !!(edev->state & (1 << i)));
153 }
154
155 return count;
156 }
157
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;
163 struct extcon_dev *edev = dev_get_drvdata(dev);
164
165 ret = sscanf(buf, "0x%x", &state);
166 if (ret == 0)
167 ret = -EINVAL;
168 else
169 ret = extcon_set_state(edev, state);
170
171 if (ret < 0)
172 return ret;
173
174 return count;
175 }
176 static DEVICE_ATTR_RW(state);
177
178 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
179 char *buf)
180 {
181 struct extcon_dev *edev = dev_get_drvdata(dev);
182
183 return sprintf(buf, "%s\n", edev->name);
184 }
185 static DEVICE_ATTR_RO(name);
186
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);
192 int i = cable->cable_index;
193
194 return sprintf(buf, "%s\n",
195 extcon_name[cable->edev->supported_cable[i]]);
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
204 int i = cable->cable_index;
205
206 return sprintf(buf, "%d\n",
207 extcon_get_cable_state_(cable->edev,
208 cable->edev->supported_cable[i]));
209 }
210
211 /**
212 * extcon_update_state() - Update the cable attach states of the extcon device
213 * only for the masked bits.
214 * @edev: the extcon device
215 * @mask: the bit mask to designate updated bits.
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.
222 *
223 * Note that the notifier provides which bits are changed in the state
224 * variable with the val parameter (second) to the callback.
225 */
226 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
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;
234 int index;
235 unsigned long flags;
236 bool attached;
237
238 if (!edev)
239 return -EINVAL;
240
241 spin_lock_irqsave(&edev->lock, flags);
242
243 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
244 u32 old_state;
245
246 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
247 (state & mask))) {
248 spin_unlock_irqrestore(&edev->lock, flags);
249 return -EPERM;
250 }
251
252 old_state = edev->state;
253 edev->state &= ~mask;
254 edev->state |= state & mask;
255
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
263 /* This could be in interrupt handler */
264 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
265 if (prop_buf) {
266 length = name_show(&edev->dev, NULL, prop_buf);
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 }
274 length = state_show(&edev->dev, NULL, prop_buf);
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;
283 /* Unlock early before uevent */
284 spin_unlock_irqrestore(&edev->lock, flags);
285
286 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
287 free_page((unsigned long)prop_buf);
288 } else {
289 /* Unlock early before uevent */
290 spin_unlock_irqrestore(&edev->lock, flags);
291
292 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
293 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
294 }
295 } else {
296 /* No changes */
297 spin_unlock_irqrestore(&edev->lock, flags);
298 }
299
300 return 0;
301 }
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 */
312 int extcon_set_state(struct extcon_dev *edev, u32 state)
313 {
314 if (!edev)
315 return -EINVAL;
316
317 return extcon_update_state(edev, 0xffffffff, state);
318 }
319 EXPORT_SYMBOL_GPL(extcon_set_state);
320
321 /**
322 * extcon_get_cable_state_() - Get the status of a specific cable.
323 * @edev: the extcon device that has the cable.
324 * @id: the unique id of each external connector in extcon enumeration.
325 */
326 int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
327 {
328 int index;
329
330 if (!edev)
331 return -EINVAL;
332
333 index = find_cable_index_by_id(edev, id);
334 if (index < 0)
335 return index;
336
337 if (edev->max_supported && edev->max_supported <= index)
338 return -EINVAL;
339
340 return !!(edev->state & (1 << index));
341 }
342 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
343
344 /**
345 * extcon_set_cable_state_() - Set the status of a specific cable.
346 * @edev: the extcon device that has the cable.
347 * @id: the unique id of each external connector
348 * in extcon enumeration.
349 * @state: the new cable status. The default semantics is
350 * true: attached / false: detached.
351 */
352 int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
353 bool cable_state)
354 {
355 u32 state;
356 int index;
357
358 if (!edev)
359 return -EINVAL;
360
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)
366 return -EINVAL;
367
368 state = cable_state ? (1 << index) : 0;
369 return extcon_update_state(edev, 1 << index, state);
370 }
371 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
372
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
381 if (!extcon_name)
382 return ERR_PTR(-EINVAL);
383
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 /**
397 * extcon_register_notifier() - Register a notifiee to get notified by
398 * any attach status changes from the extcon.
399 * @edev: the extcon device that has the external connecotr.
400 * @id: the unique id of each external connector in extcon enumeration.
401 * @nb: a notifier block to be registered.
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.
406 */
407 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
408 struct notifier_block *nb)
409 {
410 unsigned long flags;
411 int ret, idx;
412
413 if (!nb)
414 return -EINVAL;
415
416 if (edev) {
417 idx = find_cable_index_by_id(edev, id);
418
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 }
440
441 return ret;
442 }
443 EXPORT_SYMBOL_GPL(extcon_register_notifier);
444
445 /**
446 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
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.
450 */
451 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
452 struct notifier_block *nb)
453 {
454 unsigned long flags;
455 int ret, idx;
456
457 if (!edev || !nb)
458 return -EINVAL;
459
460 idx = find_cable_index_by_id(edev, id);
461
462 spin_lock_irqsave(&edev->lock, flags);
463 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
464 spin_unlock_irqrestore(&edev->lock, flags);
465
466 return ret;
467 }
468 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
469
470 static struct attribute *extcon_attrs[] = {
471 &dev_attr_state.attr,
472 &dev_attr_name.attr,
473 NULL,
474 };
475 ATTRIBUTE_GROUPS(extcon);
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);
483 extcon_class->dev_groups = extcon_groups;
484
485 #if defined(CONFIG_ANDROID)
486 switch_class = class_compat_register("switch");
487 if (WARN(!switch_class, "cannot allocate"))
488 return -ENOMEM;
489 #endif /* CONFIG_ANDROID */
490 }
491
492 return 0;
493 }
494
495 static void extcon_dev_release(struct device *dev)
496 {
497 }
498
499 static const char *muex_name = "mutually_exclusive";
500 static void dummy_sysfs_dev_release(struct device *dev)
501 {
502 }
503
504 /*
505 * extcon_dev_allocate() - Allocate the memory of extcon device.
506 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
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 */
516 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
517 {
518 struct extcon_dev *edev;
519
520 if (!supported_cable)
521 return ERR_PTR(-EINVAL);
522
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
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
561 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
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,
573 const unsigned int *supported_cable)
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
587 edev->dev.parent = dev;
588
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
603 /**
604 * extcon_dev_register() - Register a new extcon device
605 * @edev : the new extcon device (should be allocated before calling)
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 */
612 int extcon_dev_register(struct extcon_dev *edev)
613 {
614 int ret, index = 0;
615 static atomic_t edev_no = ATOMIC_INIT(-1);
616
617 if (!extcon_class) {
618 ret = create_extcon_class();
619 if (ret < 0)
620 return ret;
621 }
622
623 if (!edev || !edev->supported_cable)
624 return -EINVAL;
625
626 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
627
628 edev->max_supported = index;
629 if (index > SUPPORTED_CABLE_MAX) {
630 dev_err(&edev->dev,
631 "exceed the maximum number of supported cables\n");
632 return -EINVAL;
633 }
634
635 edev->dev.class = extcon_class;
636 edev->dev.release = extcon_dev_release;
637
638 edev->name = dev_name(edev->dev.parent);
639 if (IS_ERR_OR_NULL(edev->name)) {
640 dev_err(&edev->dev,
641 "extcon device name is null\n");
642 return -EINVAL;
643 }
644 dev_set_name(&edev->dev, "extcon%lu",
645 (unsigned long)atomic_inc_return(&edev_no));
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
683 sysfs_attr_init(&cable->attr_name.attr);
684 cable->attr_name.attr.name = "name";
685 cable->attr_name.attr.mode = 0444;
686 cable->attr_name.show = cable_name_show;
687
688 sysfs_attr_init(&cable->attr_state.attr);
689 cable->attr_state.attr.name = "state";
690 cable->attr_state.attr.mode = 0444;
691 cable->attr_state.show = cable_state_show;
692 }
693 }
694
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);
733 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
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
744 if (edev->max_supported) {
745 edev->extcon_dev_type.groups =
746 kzalloc(sizeof(struct attribute_group *) *
747 (edev->max_supported + 2), GFP_KERNEL);
748 if (!edev->extcon_dev_type.groups) {
749 ret = -ENOMEM;
750 goto err_alloc_groups;
751 }
752
753 edev->extcon_dev_type.name = dev_name(&edev->dev);
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;
759 if (edev->mutually_exclusive)
760 edev->extcon_dev_type.groups[index] =
761 &edev->attr_g_muex;
762
763 edev->dev.type = &edev->extcon_dev_type;
764 }
765
766 ret = device_register(&edev->dev);
767 if (ret) {
768 put_device(&edev->dev);
769 goto err_dev;
770 }
771 #if defined(CONFIG_ANDROID)
772 if (switch_class)
773 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
774 #endif /* CONFIG_ANDROID */
775
776 spin_lock_init(&edev->lock);
777
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]);
787
788 dev_set_drvdata(&edev->dev, edev);
789 edev->state = 0;
790
791 mutex_lock(&extcon_dev_list_lock);
792 list_add(&edev->entry, &extcon_dev_list);
793 mutex_unlock(&extcon_dev_list_lock);
794
795 return 0;
796
797 err_dev:
798 if (edev->max_supported)
799 kfree(edev->extcon_dev_type.groups);
800 err_alloc_groups:
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:
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:
814 return ret;
815 }
816 EXPORT_SYMBOL_GPL(extcon_dev_register);
817
818 /**
819 * extcon_dev_unregister() - Unregister the extcon device.
820 * @edev: the extcon device instance to be unregistered.
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 {
827 int index;
828
829 if (!edev)
830 return;
831
832 mutex_lock(&extcon_dev_list_lock);
833 list_del(&edev->entry);
834 mutex_unlock(&extcon_dev_list_lock);
835
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));
839 return;
840 }
841
842 device_unregister(&edev->dev);
843
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)
862 class_compat_remove_link(switch_class, &edev->dev, NULL);
863 #endif
864 put_device(&edev->dev);
865 }
866 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
867
868 static void devm_extcon_dev_unreg(struct device *dev, void *res)
869 {
870 extcon_dev_unregister(*(struct extcon_dev **)res);
871 }
872
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
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
938 if (!dev)
939 return ERR_PTR(-EINVAL);
940
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
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 }
959 }
960 mutex_unlock(&extcon_dev_list_lock);
961
962 return ERR_PTR(-EPROBE_DEFER);
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
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
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 {
989 #if defined(CONFIG_ANDROID)
990 class_compat_unregister(switch_class);
991 #endif
992 class_destroy(extcon_class);
993 }
994 module_exit(extcon_class_exit);
995
996 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
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");
This page took 0.080098 seconds and 6 git commands to generate.