extcon: Move defintion of struct extcon_dev to driver/extcon directory
[platform/kernel/linux-rpi.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/of.h>
34 #include <linux/slab.h>
35 #include <linux/sysfs.h>
36
37 #include "extcon.h"
38
39 #define SUPPORTED_CABLE_MAX     32
40 #define CABLE_NAME_MAX          30
41
42 struct __extcon_info {
43         unsigned int type;
44         unsigned int id;
45         const char *name;
46
47 } extcon_info[] = {
48         [EXTCON_NONE] = {
49                 .type = EXTCON_TYPE_MISC,
50                 .id = EXTCON_NONE,
51                 .name = "NONE",
52         },
53
54         /* USB external connector */
55         [EXTCON_USB] = {
56                 .type = EXTCON_TYPE_USB,
57                 .id = EXTCON_USB,
58                 .name = "USB",
59         },
60         [EXTCON_USB_HOST] = {
61                 .type = EXTCON_TYPE_USB,
62                 .id = EXTCON_USB_HOST,
63                 .name = "USB_HOST",
64         },
65
66         /* Charging external connector */
67         [EXTCON_CHG_USB_SDP] = {
68                 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
69                 .id = EXTCON_CHG_USB_SDP,
70                 .name = "SDP",
71         },
72         [EXTCON_CHG_USB_DCP] = {
73                 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
74                 .id = EXTCON_CHG_USB_DCP,
75                 .name = "DCP",
76         },
77         [EXTCON_CHG_USB_CDP] = {
78                 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
79                 .id = EXTCON_CHG_USB_CDP,
80                 .name = "CDP",
81         },
82         [EXTCON_CHG_USB_ACA] = {
83                 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
84                 .id = EXTCON_CHG_USB_ACA,
85                 .name = "ACA",
86         },
87         [EXTCON_CHG_USB_FAST] = {
88                 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
89                 .id = EXTCON_CHG_USB_FAST,
90                 .name = "FAST-CHARGER",
91         },
92         [EXTCON_CHG_USB_SLOW] = {
93                 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
94                 .id = EXTCON_CHG_USB_SLOW,
95                 .name = "SLOW-CHARGER",
96         },
97         [EXTCON_CHG_WPT] = {
98                 .type = EXTCON_TYPE_CHG,
99                 .id = EXTCON_CHG_WPT,
100                 .name = "WPT",
101         },
102
103         /* Jack external connector */
104         [EXTCON_JACK_MICROPHONE] = {
105                 .type = EXTCON_TYPE_JACK,
106                 .id = EXTCON_JACK_MICROPHONE,
107                 .name = "MICROPHONE",
108         },
109         [EXTCON_JACK_HEADPHONE] = {
110                 .type = EXTCON_TYPE_JACK,
111                 .id = EXTCON_JACK_HEADPHONE,
112                 .name = "HEADPHONE",
113         },
114         [EXTCON_JACK_LINE_IN] = {
115                 .type = EXTCON_TYPE_JACK,
116                 .id = EXTCON_JACK_LINE_IN,
117                 .name = "LINE-IN",
118         },
119         [EXTCON_JACK_LINE_OUT] = {
120                 .type = EXTCON_TYPE_JACK,
121                 .id = EXTCON_JACK_LINE_OUT,
122                 .name = "LINE-OUT",
123         },
124         [EXTCON_JACK_VIDEO_IN] = {
125                 .type = EXTCON_TYPE_JACK,
126                 .id = EXTCON_JACK_VIDEO_IN,
127                 .name = "VIDEO-IN",
128         },
129         [EXTCON_JACK_VIDEO_OUT] = {
130                 .type = EXTCON_TYPE_JACK,
131                 .id = EXTCON_JACK_VIDEO_OUT,
132                 .name = "VIDEO-OUT",
133         },
134         [EXTCON_JACK_SPDIF_IN] = {
135                 .type = EXTCON_TYPE_JACK,
136                 .id = EXTCON_JACK_SPDIF_IN,
137                 .name = "SPDIF-IN",
138         },
139         [EXTCON_JACK_SPDIF_OUT] = {
140                 .type = EXTCON_TYPE_JACK,
141                 .id = EXTCON_JACK_SPDIF_OUT,
142                 .name = "SPDIF-OUT",
143         },
144
145         /* Display external connector */
146         [EXTCON_DISP_HDMI] = {
147                 .type = EXTCON_TYPE_DISP,
148                 .id = EXTCON_DISP_HDMI,
149                 .name = "HDMI",
150         },
151         [EXTCON_DISP_MHL] = {
152                 .type = EXTCON_TYPE_DISP,
153                 .id = EXTCON_DISP_MHL,
154                 .name = "MHL",
155         },
156         [EXTCON_DISP_DVI] = {
157                 .type = EXTCON_TYPE_DISP,
158                 .id = EXTCON_DISP_DVI,
159                 .name = "DVI",
160         },
161         [EXTCON_DISP_VGA] = {
162                 .type = EXTCON_TYPE_DISP,
163                 .id = EXTCON_DISP_VGA,
164                 .name = "VGA",
165         },
166         [EXTCON_DISP_DP] = {
167                 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
168                 .id = EXTCON_DISP_DP,
169                 .name = "DP",
170         },
171         [EXTCON_DISP_HMD] = {
172                 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
173                 .id = EXTCON_DISP_HMD,
174                 .name = "HMD",
175         },
176
177         /* Miscellaneous external connector */
178         [EXTCON_DOCK] = {
179                 .type = EXTCON_TYPE_MISC,
180                 .id = EXTCON_DOCK,
181                 .name = "DOCK",
182         },
183         [EXTCON_JIG] = {
184                 .type = EXTCON_TYPE_MISC,
185                 .id = EXTCON_JIG,
186                 .name = "JIG",
187         },
188         [EXTCON_MECHANICAL] = {
189                 .type = EXTCON_TYPE_MISC,
190                 .id = EXTCON_MECHANICAL,
191                 .name = "MECHANICAL",
192         },
193
194         { /* sentinel */ }
195 };
196
197 /**
198  * struct extcon_cable - An internal data for each cable of extcon device.
199  * @edev:               The extcon device
200  * @cable_index:        Index of this cable in the edev
201  * @attr_g:             Attribute group for the cable
202  * @attr_name:          "name" sysfs entry
203  * @attr_state:         "state" sysfs entry
204  * @attrs:              Array pointing to attr_name and attr_state for attr_g
205  */
206 struct extcon_cable {
207         struct extcon_dev *edev;
208         int cable_index;
209
210         struct attribute_group attr_g;
211         struct device_attribute attr_name;
212         struct device_attribute attr_state;
213
214         struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
215
216         union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
217         union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
218         union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
219         union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
220
221         unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
222         unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
223         unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
224         unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
225 };
226
227 static struct class *extcon_class;
228 #if defined(CONFIG_ANDROID)
229 static struct class_compat *switch_class;
230 #endif /* CONFIG_ANDROID */
231
232 static LIST_HEAD(extcon_dev_list);
233 static DEFINE_MUTEX(extcon_dev_list_lock);
234
235 /**
236  * check_mutually_exclusive - Check if new_state violates mutually_exclusive
237  *                            condition.
238  * @edev:       the extcon device
239  * @new_state:  new cable attach status for @edev
240  *
241  * Returns 0 if nothing violates. Returns the index + 1 for the first
242  * violated condition.
243  */
244 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
245 {
246         int i = 0;
247
248         if (!edev->mutually_exclusive)
249                 return 0;
250
251         for (i = 0; edev->mutually_exclusive[i]; i++) {
252                 int weight;
253                 u32 correspondants = new_state & edev->mutually_exclusive[i];
254
255                 /* calculate the total number of bits set */
256                 weight = hweight32(correspondants);
257                 if (weight > 1)
258                         return i + 1;
259         }
260
261         return 0;
262 }
263
264 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
265 {
266         int i;
267
268         /* Find the the index of extcon cable in edev->supported_cable */
269         for (i = 0; i < edev->max_supported; i++) {
270                 if (edev->supported_cable[i] == id)
271                         return i;
272         }
273
274         return -EINVAL;
275 }
276
277 static int get_extcon_type(unsigned int prop)
278 {
279         switch (prop) {
280         case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
281                 return EXTCON_TYPE_USB;
282         case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
283                 return EXTCON_TYPE_CHG;
284         case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
285                 return EXTCON_TYPE_JACK;
286         case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
287                 return EXTCON_TYPE_DISP;
288         default:
289                 return -EINVAL;
290         }
291 }
292
293 static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
294 {
295         return !!(edev->state & BIT(index));
296 }
297
298 static bool is_extcon_changed(struct extcon_dev *edev, int index,
299                                 bool new_state)
300 {
301         int state = !!(edev->state & BIT(index));
302         return (state != new_state);
303 }
304
305 static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
306 {
307         int type;
308
309         /* Check whether the property is supported or not. */
310         type = get_extcon_type(prop);
311         if (type < 0)
312                 return false;
313
314         /* Check whether a specific extcon id supports the property or not. */
315         return !!(extcon_info[id].type & type);
316 }
317
318 static int is_extcon_property_capability(struct extcon_dev *edev,
319                                 unsigned int id, int index,unsigned int prop)
320 {
321         struct extcon_cable *cable;
322         int type, ret;
323
324         /* Check whether the property is supported or not. */
325         type = get_extcon_type(prop);
326         if (type < 0)
327                 return type;
328
329         cable = &edev->cables[index];
330
331         switch (type) {
332         case EXTCON_TYPE_USB:
333                 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
334                 break;
335         case EXTCON_TYPE_CHG:
336                 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
337                 break;
338         case EXTCON_TYPE_JACK:
339                 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
340                 break;
341         case EXTCON_TYPE_DISP:
342                 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
343                 break;
344         default:
345                 ret = -EINVAL;
346         }
347
348         return ret;
349 }
350
351 static void init_property(struct extcon_dev *edev, unsigned int id, int index)
352 {
353         unsigned int type = extcon_info[id].type;
354         struct extcon_cable *cable = &edev->cables[index];
355
356         if (EXTCON_TYPE_USB & type)
357                 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
358         if (EXTCON_TYPE_CHG & type)
359                 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
360         if (EXTCON_TYPE_JACK & type)
361                 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
362         if (EXTCON_TYPE_DISP & type)
363                 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
364 }
365
366 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
367                           char *buf)
368 {
369         int i, count = 0;
370         struct extcon_dev *edev = dev_get_drvdata(dev);
371
372         if (edev->max_supported == 0)
373                 return sprintf(buf, "%u\n", edev->state);
374
375         for (i = 0; i < edev->max_supported; i++) {
376                 count += sprintf(buf + count, "%s=%d\n",
377                                 extcon_info[edev->supported_cable[i]].name,
378                                  !!(edev->state & (1 << i)));
379         }
380
381         return count;
382 }
383 static DEVICE_ATTR_RO(state);
384
385 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
386                 char *buf)
387 {
388         struct extcon_dev *edev = dev_get_drvdata(dev);
389
390         return sprintf(buf, "%s\n", edev->name);
391 }
392 static DEVICE_ATTR_RO(name);
393
394 static ssize_t cable_name_show(struct device *dev,
395                                struct device_attribute *attr, char *buf)
396 {
397         struct extcon_cable *cable = container_of(attr, struct extcon_cable,
398                                                   attr_name);
399         int i = cable->cable_index;
400
401         return sprintf(buf, "%s\n",
402                         extcon_info[cable->edev->supported_cable[i]].name);
403 }
404
405 static ssize_t cable_state_show(struct device *dev,
406                                 struct device_attribute *attr, char *buf)
407 {
408         struct extcon_cable *cable = container_of(attr, struct extcon_cable,
409                                                   attr_state);
410
411         int i = cable->cable_index;
412
413         return sprintf(buf, "%d\n",
414                 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
415 }
416
417 /**
418  * extcon_sync()        - Synchronize the states for both the attached/detached
419  * @edev:               the extcon device that has the cable.
420  *
421  * This function send a notification to synchronize the all states of a
422  * specific external connector
423  */
424 int extcon_sync(struct extcon_dev *edev, unsigned int id)
425 {
426         char name_buf[120];
427         char state_buf[120];
428         char *prop_buf;
429         char *envp[3];
430         int env_offset = 0;
431         int length;
432         int index;
433         int state;
434         unsigned long flags;
435
436         if (!edev)
437                 return -EINVAL;
438
439         index = find_cable_index_by_id(edev, id);
440         if (index < 0)
441                 return index;
442
443         spin_lock_irqsave(&edev->lock, flags);
444
445         state = !!(edev->state & BIT(index));
446         raw_notifier_call_chain(&edev->nh[index], state, edev);
447
448         /* This could be in interrupt handler */
449         prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
450         if (!prop_buf) {
451                 /* Unlock early before uevent */
452                 spin_unlock_irqrestore(&edev->lock, flags);
453
454                 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
455                 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
456
457                 return -ENOMEM;
458         }
459
460         length = name_show(&edev->dev, NULL, prop_buf);
461         if (length > 0) {
462                 if (prop_buf[length - 1] == '\n')
463                         prop_buf[length - 1] = 0;
464                 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
465                 envp[env_offset++] = name_buf;
466         }
467
468         length = state_show(&edev->dev, NULL, prop_buf);
469         if (length > 0) {
470                 if (prop_buf[length - 1] == '\n')
471                         prop_buf[length - 1] = 0;
472                 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
473                 envp[env_offset++] = state_buf;
474         }
475         envp[env_offset] = NULL;
476
477         /* Unlock early before uevent */
478         spin_unlock_irqrestore(&edev->lock, flags);
479         kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
480         free_page((unsigned long)prop_buf);
481
482         return 0;
483 }
484 EXPORT_SYMBOL_GPL(extcon_sync);
485
486 /**
487  * extcon_get_state() - Get the state of a external connector.
488  * @edev:       the extcon device that has the cable.
489  * @id:         the unique id of each external connector in extcon enumeration.
490  */
491 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
492 {
493         int index, state;
494         unsigned long flags;
495
496         if (!edev)
497                 return -EINVAL;
498
499         index = find_cable_index_by_id(edev, id);
500         if (index < 0)
501                 return index;
502
503         spin_lock_irqsave(&edev->lock, flags);
504         state = is_extcon_attached(edev, index);
505         spin_unlock_irqrestore(&edev->lock, flags);
506
507         return state;
508 }
509 EXPORT_SYMBOL_GPL(extcon_get_state);
510
511 /**
512  * extcon_set_state() - Set the state of a external connector.
513  *                      without a notification.
514  * @edev:               the extcon device that has the cable.
515  * @id:                 the unique id of each external connector
516  *                      in extcon enumeration.
517  * @state:              the new cable status. The default semantics is
518  *                      true: attached / false: detached.
519  *
520  * This function only set the state of a external connector without
521  * a notification. To synchronize the data of a external connector,
522  * use extcon_set_state_sync() and extcon_sync().
523  */
524 int extcon_set_state(struct extcon_dev *edev, unsigned int id,
525                                 bool cable_state)
526 {
527         unsigned long flags;
528         int index, ret = 0;
529
530         if (!edev)
531                 return -EINVAL;
532
533         index = find_cable_index_by_id(edev, id);
534         if (index < 0)
535                 return index;
536
537         spin_lock_irqsave(&edev->lock, flags);
538
539         /* Check whether the external connector's state is changed. */
540         if (!is_extcon_changed(edev, index, cable_state))
541                 goto out;
542
543         if (check_mutually_exclusive(edev,
544                 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
545                 ret = -EPERM;
546                 goto out;
547         }
548
549         /*
550          * Initialize the value of extcon property before setting
551          * the detached state for an external connector.
552          */
553         if (!cable_state)
554                 init_property(edev, id, index);
555
556         /* Update the state for a external connector. */
557         if (cable_state)
558                 edev->state |= BIT(index);
559         else
560                 edev->state &= ~(BIT(index));
561 out:
562         spin_unlock_irqrestore(&edev->lock, flags);
563
564         return ret;
565 }
566 EXPORT_SYMBOL_GPL(extcon_set_state);
567
568 /**
569  * extcon_set_state_sync() - Set the state of a external connector
570  *                      with a notification.
571  * @edev:               the extcon device that has the cable.
572  * @id:                 the unique id of each external connector
573  *                      in extcon enumeration.
574  * @state:              the new cable status. The default semantics is
575  *                      true: attached / false: detached.
576  *
577  * This function set the state of external connector and synchronize the data
578  * by usning a notification.
579  */
580 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
581                                 bool cable_state)
582 {
583         int ret, index;
584         unsigned long flags;
585
586         index = find_cable_index_by_id(edev, id);
587         if (index < 0)
588                 return index;
589
590         /* Check whether the external connector's state is changed. */
591         spin_lock_irqsave(&edev->lock, flags);
592         ret = is_extcon_changed(edev, index, cable_state);
593         spin_unlock_irqrestore(&edev->lock, flags);
594         if (!ret)
595                 return 0;
596
597         ret = extcon_set_state(edev, id, cable_state);
598         if (ret < 0)
599                 return ret;
600
601         return extcon_sync(edev, id);
602 }
603 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
604
605 /**
606  * extcon_get_property() - Get the property value of a specific cable.
607  * @edev:               the extcon device that has the cable.
608  * @id:                 the unique id of each external connector
609  *                      in extcon enumeration.
610  * @prop:               the property id among enum extcon_property.
611  * @prop_val:           the pointer which store the value of property.
612  *
613  * When getting the property value of external connector, the external connector
614  * should be attached. If detached state, function just return 0 without
615  * property value. Also, the each property should be included in the list of
616  * supported properties according to the type of external connectors.
617  *
618  * Returns 0 if success or error number if fail
619  */
620 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
621                                 unsigned int prop,
622                                 union extcon_property_value *prop_val)
623 {
624         struct extcon_cable *cable;
625         unsigned long flags;
626         int index, ret = 0;
627
628         *prop_val = (union extcon_property_value)(0);
629
630         if (!edev)
631                 return -EINVAL;
632
633         /* Check whether the property is supported or not */
634         if (!is_extcon_property_supported(id, prop))
635                 return -EINVAL;
636
637         /* Find the cable index of external connector by using id */
638         index = find_cable_index_by_id(edev, id);
639         if (index < 0)
640                 return index;
641
642         spin_lock_irqsave(&edev->lock, flags);
643
644         /* Check whether the property is available or not. */
645         if (!is_extcon_property_capability(edev, id, index, prop)) {
646                 spin_unlock_irqrestore(&edev->lock, flags);
647                 return -EPERM;
648         }
649
650         /*
651          * Check whether the external connector is attached.
652          * If external connector is detached, the user can not
653          * get the property value.
654          */
655         if (!is_extcon_attached(edev, index)) {
656                 spin_unlock_irqrestore(&edev->lock, flags);
657                 return 0;
658         }
659
660         cable = &edev->cables[index];
661
662         /* Get the property value according to extcon type */
663         switch (prop) {
664         case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
665                 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
666                 break;
667         case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
668                 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
669                 break;
670         case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
671                 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
672                 break;
673         case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
674                 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
675                 break;
676         default:
677                 ret = -EINVAL;
678                 break;
679         }
680
681         spin_unlock_irqrestore(&edev->lock, flags);
682
683         return ret;
684 }
685 EXPORT_SYMBOL_GPL(extcon_get_property);
686
687 /**
688  * extcon_set_property() - Set the property value of a specific cable.
689  * @edev:               the extcon device that has the cable.
690  * @id:                 the unique id of each external connector
691  *                      in extcon enumeration.
692  * @prop:               the property id among enum extcon_property.
693  * @prop_val:           the pointer including the new value of property.
694  *
695  * The each property should be included in the list of supported properties
696  * according to the type of external connectors.
697  *
698  * Returns 0 if success or error number if fail
699  */
700 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
701                                 unsigned int prop,
702                                 union extcon_property_value prop_val)
703 {
704         struct extcon_cable *cable;
705         unsigned long flags;
706         int index, ret = 0;
707
708         if (!edev)
709                 return -EINVAL;
710
711         /* Check whether the property is supported or not */
712         if (!is_extcon_property_supported(id, prop))
713                 return -EINVAL;
714
715         /* Find the cable index of external connector by using id */
716         index = find_cable_index_by_id(edev, id);
717         if (index < 0)
718                 return index;
719
720         spin_lock_irqsave(&edev->lock, flags);
721
722         /* Check whether the property is available or not. */
723         if (!is_extcon_property_capability(edev, id, index, prop)) {
724                 spin_unlock_irqrestore(&edev->lock, flags);
725                 return -EPERM;
726         }
727
728         cable = &edev->cables[index];
729
730         /* Set the property value according to extcon type */
731         switch (prop) {
732         case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
733                 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
734                 break;
735         case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
736                 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
737                 break;
738         case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
739                 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
740                 break;
741         case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
742                 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
743                 break;
744         default:
745                 ret = -EINVAL;
746                 break;
747         }
748
749         spin_unlock_irqrestore(&edev->lock, flags);
750
751         return ret;
752 }
753 EXPORT_SYMBOL_GPL(extcon_set_property);
754
755 /**
756  * extcon_set_property_sync() - Set the property value of a specific cable
757                         with a notification.
758  * @prop_val:           the pointer including the new value of property.
759  *
760  * When setting the property value of external connector, the external connector
761  * should be attached. The each property should be included in the list of
762  * supported properties according to the type of external connectors.
763  *
764  * Returns 0 if success or error number if fail
765  */
766 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
767                                 unsigned int prop,
768                                 union extcon_property_value prop_val)
769 {
770         int ret;
771
772         ret = extcon_set_property(edev, id, prop, prop_val);
773         if (ret < 0)
774                 return ret;
775
776         return extcon_sync(edev, id);
777 }
778 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
779
780 /**
781  * extcon_get_property_capability() - Get the capability of property
782  *                      of an external connector.
783  * @edev:               the extcon device that has the cable.
784  * @id:                 the unique id of each external connector
785  *                      in extcon enumeration.
786  * @prop:               the property id among enum extcon_property.
787  *
788  * Returns 1 if the property is available or 0 if not available.
789  */
790 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
791                                         unsigned int prop)
792 {
793         int index;
794
795         if (!edev)
796                 return -EINVAL;
797
798         /* Check whether the property is supported or not */
799         if (!is_extcon_property_supported(id, prop))
800                 return -EINVAL;
801
802         /* Find the cable index of external connector by using id */
803         index = find_cable_index_by_id(edev, id);
804         if (index < 0)
805                 return index;
806
807         return is_extcon_property_capability(edev, id, index, prop);
808 }
809 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
810
811 /**
812  * extcon_set_property_capability() - Set the capability of a property
813  *                      of an external connector.
814  * @edev:               the extcon device that has the cable.
815  * @id:                 the unique id of each external connector
816  *                      in extcon enumeration.
817  * @prop:               the property id among enum extcon_property.
818  *
819  * This function set the capability of a property for an external connector
820  * to mark the bit in capability bitmap which mean the available state of
821  * a property.
822  *
823  * Returns 0 if success or error number if fail
824  */
825 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
826                                         unsigned int prop)
827 {
828         struct extcon_cable *cable;
829         int index, type, ret = 0;
830
831         if (!edev)
832                 return -EINVAL;
833
834         /* Check whether the property is supported or not. */
835         if (!is_extcon_property_supported(id, prop))
836                 return -EINVAL;
837
838         /* Find the cable index of external connector by using id. */
839         index = find_cable_index_by_id(edev, id);
840         if (index < 0)
841                 return index;
842
843         type = get_extcon_type(prop);
844         if (type < 0)
845                 return type;
846
847         cable = &edev->cables[index];
848
849         switch (type) {
850         case EXTCON_TYPE_USB:
851                 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
852                 break;
853         case EXTCON_TYPE_CHG:
854                 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
855                 break;
856         case EXTCON_TYPE_JACK:
857                 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
858                 break;
859         case EXTCON_TYPE_DISP:
860                 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
861                 break;
862         default:
863                 ret = -EINVAL;
864         }
865
866         return ret;
867 }
868 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
869
870 /**
871  * extcon_get_extcon_dev() - Get the extcon device instance from the name
872  * @extcon_name:        The extcon name provided with extcon_dev_register()
873  */
874 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
875 {
876         struct extcon_dev *sd;
877
878         if (!extcon_name)
879                 return ERR_PTR(-EINVAL);
880
881         mutex_lock(&extcon_dev_list_lock);
882         list_for_each_entry(sd, &extcon_dev_list, entry) {
883                 if (!strcmp(sd->name, extcon_name))
884                         goto out;
885         }
886         sd = NULL;
887 out:
888         mutex_unlock(&extcon_dev_list_lock);
889         return sd;
890 }
891 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
892
893 /**
894  * extcon_register_notifier() - Register a notifiee to get notified by
895  *                              any attach status changes from the extcon.
896  * @edev:       the extcon device that has the external connecotr.
897  * @id:         the unique id of each external connector in extcon enumeration.
898  * @nb:         a notifier block to be registered.
899  *
900  * Note that the second parameter given to the callback of nb (val) is
901  * "old_state", not the current state. The current state can be retrieved
902  * by looking at the third pameter (edev pointer)'s state value.
903  */
904 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
905                              struct notifier_block *nb)
906 {
907         unsigned long flags;
908         int ret, idx = -EINVAL;
909
910         if (!edev || !nb)
911                 return -EINVAL;
912
913         idx = find_cable_index_by_id(edev, id);
914         if (idx < 0)
915                 return idx;
916
917         spin_lock_irqsave(&edev->lock, flags);
918         ret = raw_notifier_chain_register(&edev->nh[idx], nb);
919         spin_unlock_irqrestore(&edev->lock, flags);
920
921         return ret;
922 }
923 EXPORT_SYMBOL_GPL(extcon_register_notifier);
924
925 /**
926  * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
927  * @edev:       the extcon device that has the external connecotr.
928  * @id:         the unique id of each external connector in extcon enumeration.
929  * @nb:         a notifier block to be registered.
930  */
931 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
932                                 struct notifier_block *nb)
933 {
934         unsigned long flags;
935         int ret, idx;
936
937         if (!edev || !nb)
938                 return -EINVAL;
939
940         idx = find_cable_index_by_id(edev, id);
941         if (idx < 0)
942                 return idx;
943
944         spin_lock_irqsave(&edev->lock, flags);
945         ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
946         spin_unlock_irqrestore(&edev->lock, flags);
947
948         return ret;
949 }
950 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
951
952 static struct attribute *extcon_attrs[] = {
953         &dev_attr_state.attr,
954         &dev_attr_name.attr,
955         NULL,
956 };
957 ATTRIBUTE_GROUPS(extcon);
958
959 static int create_extcon_class(void)
960 {
961         if (!extcon_class) {
962                 extcon_class = class_create(THIS_MODULE, "extcon");
963                 if (IS_ERR(extcon_class))
964                         return PTR_ERR(extcon_class);
965                 extcon_class->dev_groups = extcon_groups;
966
967 #if defined(CONFIG_ANDROID)
968                 switch_class = class_compat_register("switch");
969                 if (WARN(!switch_class, "cannot allocate"))
970                         return -ENOMEM;
971 #endif /* CONFIG_ANDROID */
972         }
973
974         return 0;
975 }
976
977 static void extcon_dev_release(struct device *dev)
978 {
979 }
980
981 static const char *muex_name = "mutually_exclusive";
982 static void dummy_sysfs_dev_release(struct device *dev)
983 {
984 }
985
986 /*
987  * extcon_dev_allocate() - Allocate the memory of extcon device.
988  * @supported_cable:    Array of supported extcon ending with EXTCON_NONE.
989  *                      If supported_cable is NULL, cable name related APIs
990  *                      are disabled.
991  *
992  * This function allocates the memory for extcon device without allocating
993  * memory in each extcon provider driver and initialize default setting for
994  * extcon device.
995  *
996  * Return the pointer of extcon device if success or ERR_PTR(err) if fail
997  */
998 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
999 {
1000         struct extcon_dev *edev;
1001
1002         if (!supported_cable)
1003                 return ERR_PTR(-EINVAL);
1004
1005         edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1006         if (!edev)
1007                 return ERR_PTR(-ENOMEM);
1008
1009         edev->max_supported = 0;
1010         edev->supported_cable = supported_cable;
1011
1012         return edev;
1013 }
1014
1015 /*
1016  * extcon_dev_free() - Free the memory of extcon device.
1017  * @edev:       the extcon device to free
1018  */
1019 void extcon_dev_free(struct extcon_dev *edev)
1020 {
1021         kfree(edev);
1022 }
1023 EXPORT_SYMBOL_GPL(extcon_dev_free);
1024
1025 /**
1026  * extcon_dev_register() - Register a new extcon device
1027  * @edev        : the new extcon device (should be allocated before calling)
1028  *
1029  * Among the members of edev struct, please set the "user initializing data"
1030  * in any case and set the "optional callbacks" if required. However, please
1031  * do not set the values of "internal data", which are initialized by
1032  * this function.
1033  */
1034 int extcon_dev_register(struct extcon_dev *edev)
1035 {
1036         int ret, index = 0;
1037         static atomic_t edev_no = ATOMIC_INIT(-1);
1038
1039         if (!extcon_class) {
1040                 ret = create_extcon_class();
1041                 if (ret < 0)
1042                         return ret;
1043         }
1044
1045         if (!edev || !edev->supported_cable)
1046                 return -EINVAL;
1047
1048         for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1049
1050         edev->max_supported = index;
1051         if (index > SUPPORTED_CABLE_MAX) {
1052                 dev_err(&edev->dev,
1053                         "exceed the maximum number of supported cables\n");
1054                 return -EINVAL;
1055         }
1056
1057         edev->dev.class = extcon_class;
1058         edev->dev.release = extcon_dev_release;
1059
1060         edev->name = dev_name(edev->dev.parent);
1061         if (IS_ERR_OR_NULL(edev->name)) {
1062                 dev_err(&edev->dev,
1063                         "extcon device name is null\n");
1064                 return -EINVAL;
1065         }
1066         dev_set_name(&edev->dev, "extcon%lu",
1067                         (unsigned long)atomic_inc_return(&edev_no));
1068
1069         if (edev->max_supported) {
1070                 char buf[10];
1071                 char *str;
1072                 struct extcon_cable *cable;
1073
1074                 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1075                                        edev->max_supported, GFP_KERNEL);
1076                 if (!edev->cables) {
1077                         ret = -ENOMEM;
1078                         goto err_sysfs_alloc;
1079                 }
1080                 for (index = 0; index < edev->max_supported; index++) {
1081                         cable = &edev->cables[index];
1082
1083                         snprintf(buf, 10, "cable.%d", index);
1084                         str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1085                                       GFP_KERNEL);
1086                         if (!str) {
1087                                 for (index--; index >= 0; index--) {
1088                                         cable = &edev->cables[index];
1089                                         kfree(cable->attr_g.name);
1090                                 }
1091                                 ret = -ENOMEM;
1092
1093                                 goto err_alloc_cables;
1094                         }
1095                         strcpy(str, buf);
1096
1097                         cable->edev = edev;
1098                         cable->cable_index = index;
1099                         cable->attrs[0] = &cable->attr_name.attr;
1100                         cable->attrs[1] = &cable->attr_state.attr;
1101                         cable->attrs[2] = NULL;
1102                         cable->attr_g.name = str;
1103                         cable->attr_g.attrs = cable->attrs;
1104
1105                         sysfs_attr_init(&cable->attr_name.attr);
1106                         cable->attr_name.attr.name = "name";
1107                         cable->attr_name.attr.mode = 0444;
1108                         cable->attr_name.show = cable_name_show;
1109
1110                         sysfs_attr_init(&cable->attr_state.attr);
1111                         cable->attr_state.attr.name = "state";
1112                         cable->attr_state.attr.mode = 0444;
1113                         cable->attr_state.show = cable_state_show;
1114                 }
1115         }
1116
1117         if (edev->max_supported && edev->mutually_exclusive) {
1118                 char buf[80];
1119                 char *name;
1120
1121                 /* Count the size of mutually_exclusive array */
1122                 for (index = 0; edev->mutually_exclusive[index]; index++)
1123                         ;
1124
1125                 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1126                                            (index + 1), GFP_KERNEL);
1127                 if (!edev->attrs_muex) {
1128                         ret = -ENOMEM;
1129                         goto err_muex;
1130                 }
1131
1132                 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1133                                              index, GFP_KERNEL);
1134                 if (!edev->d_attrs_muex) {
1135                         ret = -ENOMEM;
1136                         kfree(edev->attrs_muex);
1137                         goto err_muex;
1138                 }
1139
1140                 for (index = 0; edev->mutually_exclusive[index]; index++) {
1141                         sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1142                         name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1143                                        GFP_KERNEL);
1144                         if (!name) {
1145                                 for (index--; index >= 0; index--) {
1146                                         kfree(edev->d_attrs_muex[index].attr.
1147                                               name);
1148                                 }
1149                                 kfree(edev->d_attrs_muex);
1150                                 kfree(edev->attrs_muex);
1151                                 ret = -ENOMEM;
1152                                 goto err_muex;
1153                         }
1154                         strcpy(name, buf);
1155                         sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1156                         edev->d_attrs_muex[index].attr.name = name;
1157                         edev->d_attrs_muex[index].attr.mode = 0000;
1158                         edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1159                                                         .attr;
1160                 }
1161                 edev->attr_g_muex.name = muex_name;
1162                 edev->attr_g_muex.attrs = edev->attrs_muex;
1163
1164         }
1165
1166         if (edev->max_supported) {
1167                 edev->extcon_dev_type.groups =
1168                         kzalloc(sizeof(struct attribute_group *) *
1169                                 (edev->max_supported + 2), GFP_KERNEL);
1170                 if (!edev->extcon_dev_type.groups) {
1171                         ret = -ENOMEM;
1172                         goto err_alloc_groups;
1173                 }
1174
1175                 edev->extcon_dev_type.name = dev_name(&edev->dev);
1176                 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1177
1178                 for (index = 0; index < edev->max_supported; index++)
1179                         edev->extcon_dev_type.groups[index] =
1180                                 &edev->cables[index].attr_g;
1181                 if (edev->mutually_exclusive)
1182                         edev->extcon_dev_type.groups[index] =
1183                                 &edev->attr_g_muex;
1184
1185                 edev->dev.type = &edev->extcon_dev_type;
1186         }
1187
1188         ret = device_register(&edev->dev);
1189         if (ret) {
1190                 put_device(&edev->dev);
1191                 goto err_dev;
1192         }
1193 #if defined(CONFIG_ANDROID)
1194         if (switch_class)
1195                 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
1196 #endif /* CONFIG_ANDROID */
1197
1198         spin_lock_init(&edev->lock);
1199
1200         edev->nh = devm_kzalloc(&edev->dev,
1201                         sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1202         if (!edev->nh) {
1203                 ret = -ENOMEM;
1204                 goto err_dev;
1205         }
1206
1207         for (index = 0; index < edev->max_supported; index++)
1208                 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1209
1210         dev_set_drvdata(&edev->dev, edev);
1211         edev->state = 0;
1212
1213         mutex_lock(&extcon_dev_list_lock);
1214         list_add(&edev->entry, &extcon_dev_list);
1215         mutex_unlock(&extcon_dev_list_lock);
1216
1217         return 0;
1218
1219 err_dev:
1220         if (edev->max_supported)
1221                 kfree(edev->extcon_dev_type.groups);
1222 err_alloc_groups:
1223         if (edev->max_supported && edev->mutually_exclusive) {
1224                 for (index = 0; edev->mutually_exclusive[index]; index++)
1225                         kfree(edev->d_attrs_muex[index].attr.name);
1226                 kfree(edev->d_attrs_muex);
1227                 kfree(edev->attrs_muex);
1228         }
1229 err_muex:
1230         for (index = 0; index < edev->max_supported; index++)
1231                 kfree(edev->cables[index].attr_g.name);
1232 err_alloc_cables:
1233         if (edev->max_supported)
1234                 kfree(edev->cables);
1235 err_sysfs_alloc:
1236         return ret;
1237 }
1238 EXPORT_SYMBOL_GPL(extcon_dev_register);
1239
1240 /**
1241  * extcon_dev_unregister() - Unregister the extcon device.
1242  * @edev:       the extcon device instance to be unregistered.
1243  *
1244  * Note that this does not call kfree(edev) because edev was not allocated
1245  * by this class.
1246  */
1247 void extcon_dev_unregister(struct extcon_dev *edev)
1248 {
1249         int index;
1250
1251         if (!edev)
1252                 return;
1253
1254         mutex_lock(&extcon_dev_list_lock);
1255         list_del(&edev->entry);
1256         mutex_unlock(&extcon_dev_list_lock);
1257
1258         if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1259                 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1260                                 dev_name(&edev->dev));
1261                 return;
1262         }
1263
1264         device_unregister(&edev->dev);
1265
1266         if (edev->mutually_exclusive && edev->max_supported) {
1267                 for (index = 0; edev->mutually_exclusive[index];
1268                                 index++)
1269                         kfree(edev->d_attrs_muex[index].attr.name);
1270                 kfree(edev->d_attrs_muex);
1271                 kfree(edev->attrs_muex);
1272         }
1273
1274         for (index = 0; index < edev->max_supported; index++)
1275                 kfree(edev->cables[index].attr_g.name);
1276
1277         if (edev->max_supported) {
1278                 kfree(edev->extcon_dev_type.groups);
1279                 kfree(edev->cables);
1280         }
1281
1282 #if defined(CONFIG_ANDROID)
1283         if (switch_class)
1284                 class_compat_remove_link(switch_class, &edev->dev, NULL);
1285 #endif
1286         put_device(&edev->dev);
1287 }
1288 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1289
1290 #ifdef CONFIG_OF
1291 /*
1292  * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1293  * @dev - instance to the given device
1294  * @index - index into list of extcon_dev
1295  *
1296  * return the instance of extcon device
1297  */
1298 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1299 {
1300         struct device_node *node;
1301         struct extcon_dev *edev;
1302
1303         if (!dev)
1304                 return ERR_PTR(-EINVAL);
1305
1306         if (!dev->of_node) {
1307                 dev_dbg(dev, "device does not have a device node entry\n");
1308                 return ERR_PTR(-EINVAL);
1309         }
1310
1311         node = of_parse_phandle(dev->of_node, "extcon", index);
1312         if (!node) {
1313                 dev_dbg(dev, "failed to get phandle in %s node\n",
1314                         dev->of_node->full_name);
1315                 return ERR_PTR(-ENODEV);
1316         }
1317
1318         mutex_lock(&extcon_dev_list_lock);
1319         list_for_each_entry(edev, &extcon_dev_list, entry) {
1320                 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1321                         mutex_unlock(&extcon_dev_list_lock);
1322                         of_node_put(node);
1323                         return edev;
1324                 }
1325         }
1326         mutex_unlock(&extcon_dev_list_lock);
1327         of_node_put(node);
1328
1329         return ERR_PTR(-EPROBE_DEFER);
1330 }
1331 #else
1332 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1333 {
1334         return ERR_PTR(-ENOSYS);
1335 }
1336 #endif /* CONFIG_OF */
1337 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1338
1339 /**
1340  * extcon_get_edev_name() - Get the name of the extcon device.
1341  * @edev:       the extcon device
1342  */
1343 const char *extcon_get_edev_name(struct extcon_dev *edev)
1344 {
1345         return !edev ? NULL : edev->name;
1346 }
1347
1348 static int __init extcon_class_init(void)
1349 {
1350         return create_extcon_class();
1351 }
1352 module_init(extcon_class_init);
1353
1354 static void __exit extcon_class_exit(void)
1355 {
1356 #if defined(CONFIG_ANDROID)
1357         class_compat_unregister(switch_class);
1358 #endif
1359         class_destroy(extcon_class);
1360 }
1361 module_exit(extcon_class_exit);
1362
1363 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1364 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1365 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1366 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1367 MODULE_DESCRIPTION("External connector (extcon) class driver");
1368 MODULE_LICENSE("GPL");