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