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