blob: c21650a9268901948c44f321199ca8958746ffba [file] [log] [blame]
MyungJoo Hamde55d872012-04-20 14:16:22 +09001/*
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09002 * drivers/extcon/extcon.c - External Connector (extcon) framework.
MyungJoo Hamde55d872012-04-20 14:16:22 +09003 *
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09004 * Copyright (C) 2015 Samsung Electronics
5 * Author: Chanwoo Choi <cw00.choi@samsung.com>
6 *
MyungJoo Hamde55d872012-04-20 14:16:22 +09007 * Copyright (C) 2012 Samsung Electronics
8 * Author: Donggeun Kim <dg77.kim@samsung.com>
9 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
10 *
11 * based on android/drivers/switch/switch_class.c
12 * Copyright (C) 2008 Google, Inc.
13 * Author: Mike Lockwood <lockwood@android.com>
14 *
15 * This software is licensed under the terms of the GNU General Public
16 * License version 2, as published by the Free Software Foundation, and
17 * may be copied, distributed, and modified under those terms.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
Chanwoo Choib9ec23c2015-04-24 14:48:52 +090023 */
MyungJoo Hamde55d872012-04-20 14:16:22 +090024
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/fs.h>
30#include <linux/err.h>
Tomasz Figaf841afb12014-10-16 15:11:44 +020031#include <linux/of.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090032#include <linux/slab.h>
Mark Brown9baf3222012-08-16 20:03:21 +010033#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090034
Chanwoo Choie6cf0462016-12-26 20:37:38 +090035#include "extcon.h"
36
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090037#define SUPPORTED_CABLE_MAX 32
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090038
Colin Ian King51832402017-09-09 17:51:00 +010039static const struct __extcon_info {
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090040 unsigned int type;
41 unsigned int id;
42 const char *name;
43
44} extcon_info[] = {
45 [EXTCON_NONE] = {
46 .type = EXTCON_TYPE_MISC,
47 .id = EXTCON_NONE,
48 .name = "NONE",
49 },
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090050
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090051 /* USB external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090052 [EXTCON_USB] = {
53 .type = EXTCON_TYPE_USB,
54 .id = EXTCON_USB,
55 .name = "USB",
56 },
57 [EXTCON_USB_HOST] = {
58 .type = EXTCON_TYPE_USB,
59 .id = EXTCON_USB_HOST,
Chanwoo Choi86d6cda2017-01-07 05:17:36 +090060 .name = "USB-HOST",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090061 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090062
Chanwoo Choi11eecf92015-10-03 14:15:13 +090063 /* Charging external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090064 [EXTCON_CHG_USB_SDP] = {
65 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
66 .id = EXTCON_CHG_USB_SDP,
67 .name = "SDP",
68 },
69 [EXTCON_CHG_USB_DCP] = {
70 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
71 .id = EXTCON_CHG_USB_DCP,
72 .name = "DCP",
73 },
74 [EXTCON_CHG_USB_CDP] = {
75 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
76 .id = EXTCON_CHG_USB_CDP,
77 .name = "CDP",
78 },
79 [EXTCON_CHG_USB_ACA] = {
80 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
81 .id = EXTCON_CHG_USB_ACA,
82 .name = "ACA",
83 },
84 [EXTCON_CHG_USB_FAST] = {
85 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
86 .id = EXTCON_CHG_USB_FAST,
87 .name = "FAST-CHARGER",
88 },
89 [EXTCON_CHG_USB_SLOW] = {
90 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
91 .id = EXTCON_CHG_USB_SLOW,
92 .name = "SLOW-CHARGER",
93 },
Chanwoo Choi7fe95fb2016-08-05 18:15:46 +090094 [EXTCON_CHG_WPT] = {
95 .type = EXTCON_TYPE_CHG,
96 .id = EXTCON_CHG_WPT,
97 .name = "WPT",
98 },
Chanwoo Choi3c5f0e02017-01-02 13:03:03 +090099 [EXTCON_CHG_USB_PD] = {
100 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
101 .id = EXTCON_CHG_USB_PD,
102 .name = "PD",
103 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900104
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900105 /* Jack external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900106 [EXTCON_JACK_MICROPHONE] = {
107 .type = EXTCON_TYPE_JACK,
108 .id = EXTCON_JACK_MICROPHONE,
109 .name = "MICROPHONE",
110 },
111 [EXTCON_JACK_HEADPHONE] = {
112 .type = EXTCON_TYPE_JACK,
113 .id = EXTCON_JACK_HEADPHONE,
114 .name = "HEADPHONE",
115 },
116 [EXTCON_JACK_LINE_IN] = {
117 .type = EXTCON_TYPE_JACK,
118 .id = EXTCON_JACK_LINE_IN,
119 .name = "LINE-IN",
120 },
121 [EXTCON_JACK_LINE_OUT] = {
122 .type = EXTCON_TYPE_JACK,
123 .id = EXTCON_JACK_LINE_OUT,
124 .name = "LINE-OUT",
125 },
126 [EXTCON_JACK_VIDEO_IN] = {
127 .type = EXTCON_TYPE_JACK,
128 .id = EXTCON_JACK_VIDEO_IN,
129 .name = "VIDEO-IN",
130 },
131 [EXTCON_JACK_VIDEO_OUT] = {
132 .type = EXTCON_TYPE_JACK,
133 .id = EXTCON_JACK_VIDEO_OUT,
134 .name = "VIDEO-OUT",
135 },
136 [EXTCON_JACK_SPDIF_IN] = {
137 .type = EXTCON_TYPE_JACK,
138 .id = EXTCON_JACK_SPDIF_IN,
139 .name = "SPDIF-IN",
140 },
141 [EXTCON_JACK_SPDIF_OUT] = {
142 .type = EXTCON_TYPE_JACK,
143 .id = EXTCON_JACK_SPDIF_OUT,
144 .name = "SPDIF-OUT",
145 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900146
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900147 /* Display external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900148 [EXTCON_DISP_HDMI] = {
149 .type = EXTCON_TYPE_DISP,
150 .id = EXTCON_DISP_HDMI,
151 .name = "HDMI",
152 },
153 [EXTCON_DISP_MHL] = {
154 .type = EXTCON_TYPE_DISP,
155 .id = EXTCON_DISP_MHL,
156 .name = "MHL",
157 },
158 [EXTCON_DISP_DVI] = {
159 .type = EXTCON_TYPE_DISP,
160 .id = EXTCON_DISP_DVI,
161 .name = "DVI",
162 },
163 [EXTCON_DISP_VGA] = {
164 .type = EXTCON_TYPE_DISP,
165 .id = EXTCON_DISP_VGA,
166 .name = "VGA",
167 },
Chris Zhong21641882016-07-22 01:13:02 +0900168 [EXTCON_DISP_DP] = {
169 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
170 .id = EXTCON_DISP_DP,
171 .name = "DP",
172 },
Chanwoo Choi9c0595d2016-08-05 17:49:23 +0900173 [EXTCON_DISP_HMD] = {
174 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
175 .id = EXTCON_DISP_HMD,
176 .name = "HMD",
177 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900178
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900179 /* Miscellaneous external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900180 [EXTCON_DOCK] = {
181 .type = EXTCON_TYPE_MISC,
182 .id = EXTCON_DOCK,
183 .name = "DOCK",
184 },
185 [EXTCON_JIG] = {
186 .type = EXTCON_TYPE_MISC,
187 .id = EXTCON_JIG,
188 .name = "JIG",
189 },
190 [EXTCON_MECHANICAL] = {
191 .type = EXTCON_TYPE_MISC,
192 .id = EXTCON_MECHANICAL,
193 .name = "MECHANICAL",
194 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900195
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900196 { /* sentinel */ }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900197};
198
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900199/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900200 * struct extcon_cable - An internal data for an external connector.
201 * @edev: the extcon device
202 * @cable_index: the index of this cable in the edev
203 * @attr_g: the attribute group for the cable
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900204 * @attr_name: "name" sysfs entry
205 * @attr_state: "state" sysfs entry
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900206 * @attrs: the array pointing to attr_name and attr_state for attr_g
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900207 */
208struct extcon_cable {
209 struct extcon_dev *edev;
210 int cable_index;
211
212 struct attribute_group attr_g;
213 struct device_attribute attr_name;
214 struct device_attribute attr_state;
215
216 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900217
218 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
219 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
220 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
221 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900222
223 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
224 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
225 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
226 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900227};
228
Mark Brownbe3a07f2012-06-05 16:14:38 +0100229static struct class *extcon_class;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900230
Donggeun Kim74c5d092012-04-20 14:16:24 +0900231static LIST_HEAD(extcon_dev_list);
232static DEFINE_MUTEX(extcon_dev_list_lock);
233
MyungJoo Hambde68e62012-04-20 14:16:26 +0900234static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
235{
236 int i = 0;
237
238 if (!edev->mutually_exclusive)
239 return 0;
240
241 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530242 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900243 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900244
anish kumar28c0ada2012-08-30 00:35:10 +0530245 /* calculate the total number of bits set */
246 weight = hweight32(correspondants);
247 if (weight > 1)
248 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900249 }
250
251 return 0;
252}
253
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900254static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900255{
256 int i;
257
258 /* Find the the index of extcon cable in edev->supported_cable */
259 for (i = 0; i < edev->max_supported; i++) {
260 if (edev->supported_cable[i] == id)
261 return i;
262 }
263
264 return -EINVAL;
265}
266
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900267static int get_extcon_type(unsigned int prop)
268{
269 switch (prop) {
270 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
271 return EXTCON_TYPE_USB;
272 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
273 return EXTCON_TYPE_CHG;
274 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
275 return EXTCON_TYPE_JACK;
276 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
277 return EXTCON_TYPE_DISP;
278 default:
279 return -EINVAL;
280 }
281}
282
283static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
284{
285 return !!(edev->state & BIT(index));
286}
287
Chanwoo Choiab11af042016-07-22 13:16:34 +0900288static bool is_extcon_changed(struct extcon_dev *edev, int index,
289 bool new_state)
Chanwoo Choi046050f2015-05-19 20:01:12 +0900290{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900291 int state = !!(edev->state & BIT(index));
292 return (state != new_state);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900293}
294
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900295static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
296{
297 int type;
298
299 /* Check whether the property is supported or not. */
300 type = get_extcon_type(prop);
301 if (type < 0)
302 return false;
303
304 /* Check whether a specific extcon id supports the property or not. */
305 return !!(extcon_info[id].type & type);
306}
307
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900308static int is_extcon_property_capability(struct extcon_dev *edev,
309 unsigned int id, int index,unsigned int prop)
310{
311 struct extcon_cable *cable;
312 int type, ret;
313
314 /* Check whether the property is supported or not. */
315 type = get_extcon_type(prop);
316 if (type < 0)
317 return type;
318
319 cable = &edev->cables[index];
320
321 switch (type) {
322 case EXTCON_TYPE_USB:
323 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
324 break;
325 case EXTCON_TYPE_CHG:
326 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
327 break;
328 case EXTCON_TYPE_JACK:
329 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
330 break;
331 case EXTCON_TYPE_DISP:
332 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
333 break;
334 default:
335 ret = -EINVAL;
336 }
337
338 return ret;
339}
340
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900341static void init_property(struct extcon_dev *edev, unsigned int id, int index)
342{
343 unsigned int type = extcon_info[id].type;
344 struct extcon_cable *cable = &edev->cables[index];
345
346 if (EXTCON_TYPE_USB & type)
347 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
348 if (EXTCON_TYPE_CHG & type)
349 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
350 if (EXTCON_TYPE_JACK & type)
351 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
352 if (EXTCON_TYPE_DISP & type)
353 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
354}
355
MyungJoo Hamde55d872012-04-20 14:16:22 +0900356static ssize_t state_show(struct device *dev, struct device_attribute *attr,
357 char *buf)
358{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900359 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900360 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900361
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900362 if (edev->max_supported == 0)
363 return sprintf(buf, "%u\n", edev->state);
364
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900365 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900366 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900367 extcon_info[edev->supported_cable[i]].name,
Chanwoo Choi70641a02017-03-29 19:18:36 +0900368 !!(edev->state & BIT(i)));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900369 }
370
371 return count;
372}
Chanwoo Choi5d5321e2016-07-18 15:39:28 +0900373static DEVICE_ATTR_RO(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900374
375static ssize_t name_show(struct device *dev, struct device_attribute *attr,
376 char *buf)
377{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900378 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900379
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900380 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900381}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700382static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900383
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900384static ssize_t cable_name_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
387 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
388 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900389 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900390
391 return sprintf(buf, "%s\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900392 extcon_info[cable->edev->supported_cable[i]].name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900393}
394
395static ssize_t cable_state_show(struct device *dev,
396 struct device_attribute *attr, char *buf)
397{
398 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
399 attr_state);
400
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300401 int i = cable->cable_index;
402
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900403 return sprintf(buf, "%d\n",
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900404 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900405}
406
MyungJoo Hamde55d872012-04-20 14:16:22 +0900407/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900408 * extcon_sync() - Synchronize the state for an external connector.
409 * @edev: the extcon device
MyungJoo Hamde55d872012-04-20 14:16:22 +0900410 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900411 * Note that this function send a notification in order to synchronize
412 * the state and property of an external connector.
413 *
414 * Returns 0 if success or error number if fail.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900415 */
Chanwoo Choiab11af042016-07-22 13:16:34 +0900416int extcon_sync(struct extcon_dev *edev, unsigned int id)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900417{
418 char name_buf[120];
419 char state_buf[120];
420 char *prop_buf;
421 char *envp[3];
422 int env_offset = 0;
423 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900424 int index;
Chanwoo Choiab11af042016-07-22 13:16:34 +0900425 int state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900426 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900427
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900428 if (!edev)
429 return -EINVAL;
430
Chanwoo Choiab11af042016-07-22 13:16:34 +0900431 index = find_cable_index_by_id(edev, id);
432 if (index < 0)
433 return index;
434
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900435 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900436 state = !!(edev->state & BIT(index));
Chanwoo Choi8a9dbb72018-06-14 11:16:29 +0900437 spin_unlock_irqrestore(&edev->lock, flags);
Chanwoo Choi815429b32017-03-29 19:30:17 +0900438
439 /*
440 * Call functions in a raw notifier chain for the specific one
441 * external connector.
442 */
Chanwoo Choiab11af042016-07-22 13:16:34 +0900443 raw_notifier_call_chain(&edev->nh[index], state, edev);
Roger Quadrosf7a89812015-07-06 17:46:58 +0300444
Chanwoo Choi815429b32017-03-29 19:30:17 +0900445 /*
446 * Call functions in a raw notifier chain for the all supported
447 * external connectors.
448 */
449 raw_notifier_call_chain(&edev->nh_all, state, edev);
450
Chanwoo Choi8a9dbb72018-06-14 11:16:29 +0900451 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900452 /* This could be in interrupt handler */
453 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
454 if (!prop_buf) {
455 /* Unlock early before uevent */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900456 spin_unlock_irqrestore(&edev->lock, flags);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900457
458 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
459 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
460
Pan Biane7d9dd52016-12-03 16:56:49 +0800461 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900462 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900463
Chanwoo Choiab11af042016-07-22 13:16:34 +0900464 length = name_show(&edev->dev, NULL, prop_buf);
465 if (length > 0) {
466 if (prop_buf[length - 1] == '\n')
467 prop_buf[length - 1] = 0;
468 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
469 envp[env_offset++] = name_buf;
470 }
471
472 length = state_show(&edev->dev, NULL, prop_buf);
473 if (length > 0) {
474 if (prop_buf[length - 1] == '\n')
475 prop_buf[length - 1] = 0;
476 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
477 envp[env_offset++] = state_buf;
478 }
479 envp[env_offset] = NULL;
480
481 /* Unlock early before uevent */
482 spin_unlock_irqrestore(&edev->lock, flags);
483 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
484 free_page((unsigned long)prop_buf);
485
MyungJoo Hambde68e62012-04-20 14:16:26 +0900486 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900487}
Chanwoo Choiab11af042016-07-22 13:16:34 +0900488EXPORT_SYMBOL_GPL(extcon_sync);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900489
Donggeun Kim74c5d092012-04-20 14:16:24 +0900490/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900491 * extcon_get_state() - Get the state of an external connector.
492 * @edev: the extcon device
493 * @id: the unique id indicating an external connector
494 *
495 * Returns 0 if success or error number if fail.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900496 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900497int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900498{
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900499 int index, state;
500 unsigned long flags;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900501
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900502 if (!edev)
503 return -EINVAL;
504
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900505 index = find_cable_index_by_id(edev, id);
506 if (index < 0)
507 return index;
508
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900509 spin_lock_irqsave(&edev->lock, flags);
510 state = is_extcon_attached(edev, index);
511 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900512
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900513 return state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900514}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900515EXPORT_SYMBOL_GPL(extcon_get_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900516
517/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900518 * extcon_set_state() - Set the state of an external connector.
519 * @edev: the extcon device
520 * @id: the unique id indicating an external connector
521 * @state: the new state of an external connector.
522 * the default semantics is true: attached / false: detached.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900523 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900524 * Note that this function set the state of an external connector without
525 * a notification. To synchronize the state of an external connector,
526 * have to use extcon_set_state_sync() and extcon_sync().
527 *
528 * Returns 0 if success or error number if fail.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900529 */
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900530int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900531{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900532 unsigned long flags;
533 int index, ret = 0;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900534
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900535 if (!edev)
536 return -EINVAL;
537
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900538 index = find_cable_index_by_id(edev, id);
539 if (index < 0)
540 return index;
541
Chanwoo Choiab11af042016-07-22 13:16:34 +0900542 spin_lock_irqsave(&edev->lock, flags);
543
544 /* Check whether the external connector's state is changed. */
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900545 if (!is_extcon_changed(edev, index, state))
Chanwoo Choiab11af042016-07-22 13:16:34 +0900546 goto out;
547
548 if (check_mutually_exclusive(edev,
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900549 (edev->state & ~BIT(index)) | (state & BIT(index)))) {
Chanwoo Choiab11af042016-07-22 13:16:34 +0900550 ret = -EPERM;
551 goto out;
552 }
553
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900554 /*
555 * Initialize the value of extcon property before setting
556 * the detached state for an external connector.
557 */
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900558 if (!state)
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900559 init_property(edev, id, index);
560
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900561 /* Update the state for an external connector. */
562 if (state)
Chanwoo Choiab11af042016-07-22 13:16:34 +0900563 edev->state |= BIT(index);
564 else
565 edev->state &= ~(BIT(index));
566out:
567 spin_unlock_irqrestore(&edev->lock, flags);
568
569 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900570}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900571EXPORT_SYMBOL_GPL(extcon_set_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900572
573/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900574 * extcon_set_state_sync() - Set the state of an external connector with sync.
575 * @edev: the extcon device
576 * @id: the unique id indicating an external connector
577 * @state: the new state of external connector.
578 * the default semantics is true: attached / false: detached.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900579 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900580 * Note that this function set the state of external connector
581 * and synchronize the state by sending a notification.
582 *
583 * Returns 0 if success or error number if fail.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900584 */
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900585int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
Chanwoo Choiab11af042016-07-22 13:16:34 +0900586{
587 int ret, index;
588 unsigned long flags;
589
590 index = find_cable_index_by_id(edev, id);
591 if (index < 0)
592 return index;
593
594 /* Check whether the external connector's state is changed. */
595 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900596 ret = is_extcon_changed(edev, index, state);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900597 spin_unlock_irqrestore(&edev->lock, flags);
598 if (!ret)
599 return 0;
600
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900601 ret = extcon_set_state(edev, id, state);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900602 if (ret < 0)
603 return ret;
604
605 return extcon_sync(edev, id);
606}
607EXPORT_SYMBOL_GPL(extcon_set_state_sync);
608
609/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900610 * extcon_get_property() - Get the property value of an external connector.
611 * @edev: the extcon device
612 * @id: the unique id indicating an external connector
613 * @prop: the property id indicating an extcon property
614 * @prop_val: the pointer which store the value of extcon property
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900615 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900616 * Note that when getting the property value of external connector,
617 * the external connector should be attached. If detached state, function
618 * return 0 without property value. Also, the each property should be
619 * included in the list of supported properties according to extcon type.
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900620 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900621 * Returns 0 if success or error number if fail.
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900622 */
623int extcon_get_property(struct extcon_dev *edev, unsigned int id,
624 unsigned int prop,
625 union extcon_property_value *prop_val)
626{
627 struct extcon_cable *cable;
628 unsigned long flags;
629 int index, ret = 0;
630
Andy Shevchenkocff74992018-08-27 18:35:51 +0300631 *prop_val = (union extcon_property_value){0};
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900632
633 if (!edev)
634 return -EINVAL;
635
636 /* Check whether the property is supported or not */
637 if (!is_extcon_property_supported(id, prop))
638 return -EINVAL;
639
640 /* Find the cable index of external connector by using id */
641 index = find_cable_index_by_id(edev, id);
642 if (index < 0)
643 return index;
644
645 spin_lock_irqsave(&edev->lock, flags);
646
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900647 /* Check whether the property is available or not. */
648 if (!is_extcon_property_capability(edev, id, index, prop)) {
649 spin_unlock_irqrestore(&edev->lock, flags);
650 return -EPERM;
651 }
652
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900653 /*
654 * Check whether the external connector is attached.
655 * If external connector is detached, the user can not
656 * get the property value.
657 */
658 if (!is_extcon_attached(edev, index)) {
659 spin_unlock_irqrestore(&edev->lock, flags);
660 return 0;
661 }
662
663 cable = &edev->cables[index];
664
665 /* Get the property value according to extcon type */
666 switch (prop) {
667 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
668 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
669 break;
670 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
671 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
672 break;
673 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
674 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
675 break;
676 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
677 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
678 break;
679 default:
680 ret = -EINVAL;
681 break;
682 }
683
684 spin_unlock_irqrestore(&edev->lock, flags);
685
686 return ret;
687}
688EXPORT_SYMBOL_GPL(extcon_get_property);
689
690/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900691 * extcon_set_property() - Set the property value of an external connector.
692 * @edev: the extcon device
693 * @id: the unique id indicating an external connector
694 * @prop: the property id indicating an extcon property
695 * @prop_val: the pointer including the new value of extcon property
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900696 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900697 * Note that each property should be included in the list of supported
698 * properties according to the extcon type.
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900699 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900700 * Returns 0 if success or error number if fail.
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900701 */
702int extcon_set_property(struct extcon_dev *edev, unsigned int id,
703 unsigned int prop,
704 union extcon_property_value prop_val)
705{
706 struct extcon_cable *cable;
707 unsigned long flags;
708 int index, ret = 0;
709
710 if (!edev)
711 return -EINVAL;
712
713 /* Check whether the property is supported or not */
714 if (!is_extcon_property_supported(id, prop))
715 return -EINVAL;
716
717 /* Find the cable index of external connector by using id */
718 index = find_cable_index_by_id(edev, id);
719 if (index < 0)
720 return index;
721
722 spin_lock_irqsave(&edev->lock, flags);
723
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900724 /* Check whether the property is available or not. */
725 if (!is_extcon_property_capability(edev, id, index, prop)) {
726 spin_unlock_irqrestore(&edev->lock, flags);
727 return -EPERM;
728 }
729
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900730 cable = &edev->cables[index];
731
732 /* Set the property value according to extcon type */
733 switch (prop) {
734 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
735 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
736 break;
737 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
738 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
739 break;
740 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
741 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
742 break;
743 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
744 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
745 break;
746 default:
747 ret = -EINVAL;
748 break;
749 }
750
751 spin_unlock_irqrestore(&edev->lock, flags);
752
753 return ret;
754}
755EXPORT_SYMBOL_GPL(extcon_set_property);
756
757/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900758 * extcon_set_property_sync() - Set property of an external connector with sync.
759 * @prop_val: the pointer including the new value of extcon property
Chanwoo Choiab11af042016-07-22 13:16:34 +0900760 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900761 * Note that when setting the property value of external connector,
762 * the external connector should be attached. The each property should
763 * be included in the list of supported properties according to extcon type.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900764 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900765 * Returns 0 if success or error number if fail.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900766 */
767int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
768 unsigned int prop,
769 union extcon_property_value prop_val)
770{
771 int ret;
772
773 ret = extcon_set_property(edev, id, prop, prop_val);
774 if (ret < 0)
775 return ret;
776
777 return extcon_sync(edev, id);
778}
779EXPORT_SYMBOL_GPL(extcon_set_property_sync);
780
781/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900782 * extcon_get_property_capability() - Get the capability of the property
783 * for an external connector.
784 * @edev: the extcon device
785 * @id: the unique id indicating an external connector
786 * @prop: the property id indicating an extcon property
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900787 *
788 * Returns 1 if the property is available or 0 if not available.
789 */
790int 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}
809EXPORT_SYMBOL_GPL(extcon_get_property_capability);
810
811/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900812 * extcon_set_property_capability() - Set the capability of the property
813 * for an external connector.
814 * @edev: the extcon device
815 * @id: the unique id indicating an external connector
816 * @prop: the property id indicating an extcon property
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900817 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900818 * Note that this function set the capability of the property
819 * for an external connector in order to mark the bit in capability
820 * bitmap which mean the available state of the property.
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900821 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900822 * Returns 0 if success or error number if fail.
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900823 */
824int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
825 unsigned int prop)
826{
827 struct extcon_cable *cable;
828 int index, type, ret = 0;
829
830 if (!edev)
831 return -EINVAL;
832
833 /* Check whether the property is supported or not. */
834 if (!is_extcon_property_supported(id, prop))
835 return -EINVAL;
836
837 /* Find the cable index of external connector by using id. */
838 index = find_cable_index_by_id(edev, id);
839 if (index < 0)
840 return index;
841
842 type = get_extcon_type(prop);
843 if (type < 0)
844 return type;
845
846 cable = &edev->cables[index];
847
848 switch (type) {
849 case EXTCON_TYPE_USB:
850 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
851 break;
852 case EXTCON_TYPE_CHG:
853 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
854 break;
855 case EXTCON_TYPE_JACK:
856 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
857 break;
858 case EXTCON_TYPE_DISP:
859 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
860 break;
861 default:
862 ret = -EINVAL;
863 }
864
865 return ret;
866}
867EXPORT_SYMBOL_GPL(extcon_set_property_capability);
868
869/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900870 * extcon_get_extcon_dev() - Get the extcon device instance from the name.
871 * @extcon_name: the extcon name provided with extcon_dev_register()
872 *
873 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900874 */
875struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
876{
877 struct extcon_dev *sd;
878
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900879 if (!extcon_name)
880 return ERR_PTR(-EINVAL);
881
Donggeun Kim74c5d092012-04-20 14:16:24 +0900882 mutex_lock(&extcon_dev_list_lock);
883 list_for_each_entry(sd, &extcon_dev_list, entry) {
884 if (!strcmp(sd->name, extcon_name))
885 goto out;
886 }
887 sd = NULL;
888out:
889 mutex_unlock(&extcon_dev_list_lock);
890 return sd;
891}
892EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
893
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900894/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900895 * extcon_register_notifier() - Register a notifier block to get notified by
896 * any state changes from the extcon.
897 * @edev: the extcon device
898 * @id: the unique id indicating an external connector
899 * @nb: a notifier block to be registered
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900900 *
901 * Note that the second parameter given to the callback of nb (val) is
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900902 * the current state of an external connector and the third pameter
903 * is the pointer of extcon device.
904 *
905 * Returns 0 if success or error number if fail.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900906 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900907int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900908 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900909{
Hans de Goede66bee352015-03-21 17:26:24 +0100910 unsigned long flags;
Maninder Singh2c8116a2016-08-01 14:51:30 +0530911 int ret, idx = -EINVAL;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900912
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900913 if (!edev || !nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900914 return -EINVAL;
915
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900916 idx = find_cable_index_by_id(edev, id);
917 if (idx < 0)
918 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100919
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900920 spin_lock_irqsave(&edev->lock, flags);
921 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
922 spin_unlock_irqrestore(&edev->lock, flags);
Hans de Goede66bee352015-03-21 17:26:24 +0100923
924 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900925}
926EXPORT_SYMBOL_GPL(extcon_register_notifier);
927
928/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900929 * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
930 * @edev: the extcon device
931 * @id: the unique id indicating an external connector
932 * @nb: a notifier block to be registered
933 *
934 * Returns 0 if success or error number if fail.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900935 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900936int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900937 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900938{
Hans de Goede66bee352015-03-21 17:26:24 +0100939 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900940 int ret, idx;
941
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900942 if (!edev || !nb)
943 return -EINVAL;
944
Chanwoo Choi046050f2015-05-19 20:01:12 +0900945 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900946 if (idx < 0)
947 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100948
949 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900950 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100951 spin_unlock_irqrestore(&edev->lock, flags);
952
953 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900954}
955EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
956
Chanwoo Choi815429b32017-03-29 19:30:17 +0900957/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900958 * extcon_register_notifier_all() - Register a notifier block for all connectors.
959 * @edev: the extcon device
960 * @nb: a notifier block to be registered
Chanwoo Choi815429b32017-03-29 19:30:17 +0900961 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900962 * Note that this function registers a notifier block in order to receive
963 * the state change of all supported external connectors from extcon device.
Markus Elfring826a47e2017-04-23 22:15:20 +0200964 * And the second parameter given to the callback of nb (val) is
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900965 * the current state and the third pameter is the pointer of extcon device.
Chanwoo Choi815429b32017-03-29 19:30:17 +0900966 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900967 * Returns 0 if success or error number if fail.
Chanwoo Choi815429b32017-03-29 19:30:17 +0900968 */
969int extcon_register_notifier_all(struct extcon_dev *edev,
970 struct notifier_block *nb)
971{
972 unsigned long flags;
973 int ret;
974
975 if (!edev || !nb)
976 return -EINVAL;
977
978 spin_lock_irqsave(&edev->lock, flags);
979 ret = raw_notifier_chain_register(&edev->nh_all, nb);
980 spin_unlock_irqrestore(&edev->lock, flags);
981
982 return ret;
983}
984EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
985
986/**
987 * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900988 * @edev: the extcon device
989 * @nb: a notifier block to be registered
Chanwoo Choi815429b32017-03-29 19:30:17 +0900990 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +0900991 * Returns 0 if success or error number if fail.
Chanwoo Choi815429b32017-03-29 19:30:17 +0900992 */
993int extcon_unregister_notifier_all(struct extcon_dev *edev,
994 struct notifier_block *nb)
995{
996 unsigned long flags;
997 int ret;
998
999 if (!edev || !nb)
1000 return -EINVAL;
1001
1002 spin_lock_irqsave(&edev->lock, flags);
1003 ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
1004 spin_unlock_irqrestore(&edev->lock, flags);
1005
1006 return ret;
1007}
1008EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
1009
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001010static struct attribute *extcon_attrs[] = {
1011 &dev_attr_state.attr,
1012 &dev_attr_name.attr,
1013 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +09001014};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001015ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001016
1017static int create_extcon_class(void)
1018{
1019 if (!extcon_class) {
1020 extcon_class = class_create(THIS_MODULE, "extcon");
1021 if (IS_ERR(extcon_class))
1022 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001023 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001024 }
1025
1026 return 0;
1027}
1028
MyungJoo Hamde55d872012-04-20 14:16:22 +09001029static void extcon_dev_release(struct device *dev)
1030{
MyungJoo Hamde55d872012-04-20 14:16:22 +09001031}
1032
MyungJoo Hambde68e62012-04-20 14:16:26 +09001033static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001034static void dummy_sysfs_dev_release(struct device *dev)
1035{
1036}
1037
Chanwoo Choia9af6522014-04-24 19:46:49 +09001038/*
1039 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001040 * @supported_cable: the array of the supported external connectors
1041 * ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +09001042 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001043 * Note that this function allocates the memory for extcon device
1044 * and initialize default setting for the extcon device.
Chanwoo Choia9af6522014-04-24 19:46:49 +09001045 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001046 * Returns the pointer memory of allocated extcon_dev if success
1047 * or ERR_PTR(err) if fail.
Chanwoo Choia9af6522014-04-24 19:46:49 +09001048 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +09001049struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +09001050{
1051 struct extcon_dev *edev;
1052
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001053 if (!supported_cable)
1054 return ERR_PTR(-EINVAL);
1055
Chanwoo Choia9af6522014-04-24 19:46:49 +09001056 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1057 if (!edev)
1058 return ERR_PTR(-ENOMEM);
1059
1060 edev->max_supported = 0;
1061 edev->supported_cable = supported_cable;
1062
1063 return edev;
1064}
1065
1066/*
1067 * extcon_dev_free() - Free the memory of extcon device.
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001068 * @edev: the extcon device
Chanwoo Choia9af6522014-04-24 19:46:49 +09001069 */
1070void extcon_dev_free(struct extcon_dev *edev)
1071{
1072 kfree(edev);
1073}
1074EXPORT_SYMBOL_GPL(extcon_dev_free);
1075
MyungJoo Hamde55d872012-04-20 14:16:22 +09001076/**
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001077 * extcon_dev_register() - Register an new extcon device
1078 * @edev: the extcon device to be registered
MyungJoo Hamde55d872012-04-20 14:16:22 +09001079 *
1080 * Among the members of edev struct, please set the "user initializing data"
MyungJoo Hamde55d872012-04-20 14:16:22 +09001081 * do not set the values of "internal data", which are initialized by
1082 * this function.
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001083 *
1084 * Note that before calling this funciton, have to allocate the memory
1085 * of an extcon device by using the extcon_dev_allocate(). And the extcon
1086 * dev should include the supported_cable information.
1087 *
1088 * Returns 0 if success or error number if fail.
MyungJoo Hamde55d872012-04-20 14:16:22 +09001089 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001090int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001091{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001092 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001093 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001094
1095 if (!extcon_class) {
1096 ret = create_extcon_class();
1097 if (ret < 0)
1098 return ret;
1099 }
1100
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001101 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001102 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001103
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001104 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1105
1106 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001107 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001108 dev_err(&edev->dev,
1109 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001110 return -EINVAL;
1111 }
1112
Chanwoo Choidae61652013-09-27 09:19:40 +09001113 edev->dev.class = extcon_class;
1114 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001115
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001116 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001117 if (IS_ERR_OR_NULL(edev->name)) {
1118 dev_err(&edev->dev,
1119 "extcon device name is null\n");
1120 return -EINVAL;
1121 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001122 dev_set_name(&edev->dev, "extcon%lu",
1123 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001124
1125 if (edev->max_supported) {
1126 char buf[10];
1127 char *str;
1128 struct extcon_cable *cable;
1129
Kees Cook6396bb22018-06-12 14:03:40 -07001130 edev->cables = kcalloc(edev->max_supported,
1131 sizeof(struct extcon_cable),
1132 GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001133 if (!edev->cables) {
1134 ret = -ENOMEM;
1135 goto err_sysfs_alloc;
1136 }
1137 for (index = 0; index < edev->max_supported; index++) {
1138 cable = &edev->cables[index];
1139
1140 snprintf(buf, 10, "cable.%d", index);
Kees Cook6396bb22018-06-12 14:03:40 -07001141 str = kzalloc(strlen(buf) + 1,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001142 GFP_KERNEL);
1143 if (!str) {
1144 for (index--; index >= 0; index--) {
1145 cable = &edev->cables[index];
1146 kfree(cable->attr_g.name);
1147 }
1148 ret = -ENOMEM;
1149
1150 goto err_alloc_cables;
1151 }
1152 strcpy(str, buf);
1153
1154 cable->edev = edev;
1155 cable->cable_index = index;
1156 cable->attrs[0] = &cable->attr_name.attr;
1157 cable->attrs[1] = &cable->attr_state.attr;
1158 cable->attrs[2] = NULL;
1159 cable->attr_g.name = str;
1160 cable->attr_g.attrs = cable->attrs;
1161
Mark Brown9baf3222012-08-16 20:03:21 +01001162 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001163 cable->attr_name.attr.name = "name";
1164 cable->attr_name.attr.mode = 0444;
1165 cable->attr_name.show = cable_name_show;
1166
Mark Brown9baf3222012-08-16 20:03:21 +01001167 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001168 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +09001169 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001170 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001171 }
1172 }
1173
MyungJoo Hambde68e62012-04-20 14:16:26 +09001174 if (edev->max_supported && edev->mutually_exclusive) {
1175 char buf[80];
1176 char *name;
1177
1178 /* Count the size of mutually_exclusive array */
1179 for (index = 0; edev->mutually_exclusive[index]; index++)
1180 ;
1181
Kees Cook6396bb22018-06-12 14:03:40 -07001182 edev->attrs_muex = kcalloc(index + 1,
1183 sizeof(struct attribute *),
1184 GFP_KERNEL);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001185 if (!edev->attrs_muex) {
1186 ret = -ENOMEM;
1187 goto err_muex;
1188 }
1189
Kees Cook6396bb22018-06-12 14:03:40 -07001190 edev->d_attrs_muex = kcalloc(index,
1191 sizeof(struct device_attribute),
1192 GFP_KERNEL);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001193 if (!edev->d_attrs_muex) {
1194 ret = -ENOMEM;
1195 kfree(edev->attrs_muex);
1196 goto err_muex;
1197 }
1198
1199 for (index = 0; edev->mutually_exclusive[index]; index++) {
1200 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
Kees Cook6396bb22018-06-12 14:03:40 -07001201 name = kzalloc(strlen(buf) + 1,
MyungJoo Hambde68e62012-04-20 14:16:26 +09001202 GFP_KERNEL);
1203 if (!name) {
1204 for (index--; index >= 0; index--) {
1205 kfree(edev->d_attrs_muex[index].attr.
1206 name);
1207 }
1208 kfree(edev->d_attrs_muex);
1209 kfree(edev->attrs_muex);
1210 ret = -ENOMEM;
1211 goto err_muex;
1212 }
1213 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +01001214 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001215 edev->d_attrs_muex[index].attr.name = name;
1216 edev->d_attrs_muex[index].attr.mode = 0000;
1217 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1218 .attr;
1219 }
1220 edev->attr_g_muex.name = muex_name;
1221 edev->attr_g_muex.attrs = edev->attrs_muex;
1222
1223 }
1224
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001225 if (edev->max_supported) {
1226 edev->extcon_dev_type.groups =
Kees Cook6396bb22018-06-12 14:03:40 -07001227 kcalloc(edev->max_supported + 2,
1228 sizeof(struct attribute_group *),
1229 GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001230 if (!edev->extcon_dev_type.groups) {
1231 ret = -ENOMEM;
1232 goto err_alloc_groups;
1233 }
1234
Chanwoo Choidae61652013-09-27 09:19:40 +09001235 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001236 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;
MyungJoo Hambde68e62012-04-20 14:16:26 +09001241 if (edev->mutually_exclusive)
1242 edev->extcon_dev_type.groups[index] =
1243 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001244
Chanwoo Choidae61652013-09-27 09:19:40 +09001245 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001246 }
1247
Chanwoo Choidae61652013-09-27 09:19:40 +09001248 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001249 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +09001250 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001251 goto err_dev;
1252 }
MyungJoo Hamde55d872012-04-20 14:16:22 +09001253
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001254 spin_lock_init(&edev->lock);
Markus Elfring3f5071a2017-04-23 20:54:11 +02001255 edev->nh = devm_kcalloc(&edev->dev, edev->max_supported,
1256 sizeof(*edev->nh), GFP_KERNEL);
Chanwoo Choi046050f2015-05-19 20:01:12 +09001257 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]);
Donggeun Kim74c5d092012-04-20 14:16:24 +09001264
Chanwoo Choi815429b32017-03-29 19:30:17 +09001265 RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1266
Chanwoo Choidae61652013-09-27 09:19:40 +09001267 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001268 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +09001269
1270 mutex_lock(&extcon_dev_list_lock);
1271 list_add(&edev->entry, &extcon_dev_list);
1272 mutex_unlock(&extcon_dev_list_lock);
1273
MyungJoo Hamde55d872012-04-20 14:16:22 +09001274 return 0;
1275
1276err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001277 if (edev->max_supported)
1278 kfree(edev->extcon_dev_type.groups);
1279err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +09001280 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:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001287 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:
MyungJoo Hamde55d872012-04-20 14:16:22 +09001293 return ret;
1294}
1295EXPORT_SYMBOL_GPL(extcon_dev_register);
1296
1297/**
1298 * extcon_dev_unregister() - Unregister the extcon device.
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001299 * @edev: the extcon device to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +09001300 *
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{
anish kumar57e7cd32012-10-22 09:43:33 +09001306 int index;
1307
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001308 if (!edev)
1309 return;
1310
anish kumar57e7cd32012-10-22 09:43:33 +09001311 mutex_lock(&extcon_dev_list_lock);
1312 list_del(&edev->entry);
1313 mutex_unlock(&extcon_dev_list_lock);
1314
Chanwoo Choidae61652013-09-27 09:19:40 +09001315 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));
anish kumar57e7cd32012-10-22 09:43:33 +09001318 return;
1319 }
1320
Wang, Xiaoming7585ca02013-11-01 18:48:14 -04001321 device_unregister(&edev->dev);
1322
anish kumar57e7cd32012-10-22 09:43:33 +09001323 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
Chanwoo Choidae61652013-09-27 09:19:40 +09001339 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001340}
1341EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1342
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001343#ifdef CONFIG_OF
Andrzej Hajda370ed7a2018-02-27 13:22:07 +01001344
1345/*
1346 * extcon_find_edev_by_node - Find the extcon device from devicetree.
1347 * @node : OF node identifying edev
1348 *
1349 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1350 */
1351struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1352{
1353 struct extcon_dev *edev;
1354
1355 mutex_lock(&extcon_dev_list_lock);
1356 list_for_each_entry(edev, &extcon_dev_list, entry)
1357 if (edev->dev.parent && edev->dev.parent->of_node == node)
1358 goto out;
1359 edev = ERR_PTR(-EPROBE_DEFER);
1360out:
1361 mutex_unlock(&extcon_dev_list_lock);
1362
1363 return edev;
1364}
1365
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001366/*
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001367 * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
1368 * @dev : the instance to the given device
1369 * @index : the index into list of extcon_dev
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001370 *
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001371 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001372 */
1373struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1374{
1375 struct device_node *node;
1376 struct extcon_dev *edev;
1377
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001378 if (!dev)
1379 return ERR_PTR(-EINVAL);
1380
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001381 if (!dev->of_node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001382 dev_dbg(dev, "device does not have a device node entry\n");
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001383 return ERR_PTR(-EINVAL);
1384 }
1385
1386 node = of_parse_phandle(dev->of_node, "extcon", index);
1387 if (!node) {
Rob Herring5c270362017-07-18 16:43:00 -05001388 dev_dbg(dev, "failed to get phandle in %pOF node\n",
1389 dev->of_node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001390 return ERR_PTR(-ENODEV);
1391 }
1392
Andrzej Hajda370ed7a2018-02-27 13:22:07 +01001393 edev = extcon_find_edev_by_node(node);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001394 of_node_put(node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001395
Andrzej Hajda370ed7a2018-02-27 13:22:07 +01001396 return edev;
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001397}
Andrzej Hajda370ed7a2018-02-27 13:22:07 +01001398
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001399#else
Andrzej Hajda370ed7a2018-02-27 13:22:07 +01001400
1401struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1402{
1403 return ERR_PTR(-ENOSYS);
1404}
1405
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001406struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1407{
1408 return ERR_PTR(-ENOSYS);
1409}
Andrzej Hajda370ed7a2018-02-27 13:22:07 +01001410
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001411#endif /* CONFIG_OF */
Andrzej Hajda370ed7a2018-02-27 13:22:07 +01001412
1413EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001414EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1415
Chanwoo Choi707d7552015-04-15 13:57:51 +09001416/**
1417 * extcon_get_edev_name() - Get the name of the extcon device.
1418 * @edev: the extcon device
1419 */
1420const char *extcon_get_edev_name(struct extcon_dev *edev)
1421{
1422 return !edev ? NULL : edev->name;
1423}
1424
MyungJoo Hamde55d872012-04-20 14:16:22 +09001425static int __init extcon_class_init(void)
1426{
1427 return create_extcon_class();
1428}
1429module_init(extcon_class_init);
1430
1431static void __exit extcon_class_exit(void)
1432{
MyungJoo Hamde55d872012-04-20 14:16:22 +09001433 class_destroy(extcon_class);
1434}
1435module_exit(extcon_class_exit);
1436
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001437MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001438MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
Chanwoo Choi6ab6094f2017-04-03 19:45:48 +09001439MODULE_DESCRIPTION("External Connector (extcon) framework");
1440MODULE_LICENSE("GPL v2");