blob: 8eccf7b1493700b055457ca8b43879fdab895311 [file] [log] [blame]
MyungJoo Hamde55d872012-04-20 14:16:22 +09001/*
Chanwoo Choib9ec23c2015-04-24 14:48:52 +09002 * drivers/extcon/extcon.c - External Connector (extcon) framework.
MyungJoo Hamde55d872012-04-20 14:16:22 +09003 *
4 * External connector (extcon) class driver
5 *
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09006 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 *
MyungJoo Hamde55d872012-04-20 14:16:22 +09009 * 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.
Chanwoo Choib9ec23c2015-04-24 14:48:52 +090025 */
MyungJoo Hamde55d872012-04-20 14:16:22 +090026
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>
Tomasz Figaf841afb2014-10-16 15:11:44 +020033#include <linux/of.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090034#include <linux/slab.h>
Mark Brown9baf3222012-08-16 20:03:21 +010035#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090036
Chanwoo Choie6cf0462016-12-26 20:37:38 +090037#include "extcon.h"
38
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090039#define SUPPORTED_CABLE_MAX 32
40#define CABLE_NAME_MAX 30
41
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090042struct __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 },
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090053
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090054 /* USB external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090055 [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,
Chanwoo Choi86d6cda2017-01-07 05:17:36 +090063 .name = "USB-HOST",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090064 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090065
Chanwoo Choi11eecf92015-10-03 14:15:13 +090066 /* Charging external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090067 [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 },
Chanwoo Choi7fe95fb2016-08-05 18:15:46 +090097 [EXTCON_CHG_WPT] = {
98 .type = EXTCON_TYPE_CHG,
99 .id = EXTCON_CHG_WPT,
100 .name = "WPT",
101 },
Chanwoo Choi3c5f0e02017-01-02 13:03:03 +0900102 [EXTCON_CHG_USB_PD] = {
103 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
104 .id = EXTCON_CHG_USB_PD,
105 .name = "PD",
106 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900107
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900108 /* Jack external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900109 [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 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900149
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900150 /* Display external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900151 [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 },
Chris Zhong21641882016-07-22 01:13:02 +0900171 [EXTCON_DISP_DP] = {
172 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
173 .id = EXTCON_DISP_DP,
174 .name = "DP",
175 },
Chanwoo Choi9c0595d2016-08-05 17:49:23 +0900176 [EXTCON_DISP_HMD] = {
177 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
178 .id = EXTCON_DISP_HMD,
179 .name = "HMD",
180 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900181
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900182 /* Miscellaneous external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900183 [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 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900198
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900199 { /* sentinel */ }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900200};
201
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900202/**
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 */
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900220
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];
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900225
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)];
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900230};
231
Mark Brownbe3a07f2012-06-05 16:14:38 +0100232static struct class *extcon_class;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900233
Donggeun Kim74c5d092012-04-20 14:16:24 +0900234static LIST_HEAD(extcon_dev_list);
235static DEFINE_MUTEX(extcon_dev_list_lock);
236
MyungJoo Hambde68e62012-04-20 14:16:26 +0900237/**
238 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900239 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +0900240 * @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++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530254 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900255 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900256
anish kumar28c0ada2012-08-30 00:35:10 +0530257 /* calculate the total number of bits set */
258 weight = hweight32(correspondants);
259 if (weight > 1)
260 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900261 }
262
263 return 0;
264}
265
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900266static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900267{
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
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900279static 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
Chanwoo Choiab11af042016-07-22 13:16:34 +0900300static bool is_extcon_changed(struct extcon_dev *edev, int index,
301 bool new_state)
Chanwoo Choi046050f2015-05-19 20:01:12 +0900302{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900303 int state = !!(edev->state & BIT(index));
304 return (state != new_state);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900305}
306
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900307static 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
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900320static 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
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900353static 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
MyungJoo Hamde55d872012-04-20 14:16:22 +0900368static ssize_t state_show(struct device *dev, struct device_attribute *attr,
369 char *buf)
370{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900371 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900372 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900373
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900374 if (edev->max_supported == 0)
375 return sprintf(buf, "%u\n", edev->state);
376
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900377 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900378 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900379 extcon_info[edev->supported_cable[i]].name,
Chanwoo Choi70641a02017-03-29 19:18:36 +0900380 !!(edev->state & BIT(i)));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900381 }
382
383 return count;
384}
Chanwoo Choi5d5321e2016-07-18 15:39:28 +0900385static DEVICE_ATTR_RO(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900386
387static ssize_t name_show(struct device *dev, struct device_attribute *attr,
388 char *buf)
389{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900390 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900391
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900392 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900393}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700394static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900395
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900396static 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);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900401 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900402
403 return sprintf(buf, "%s\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900404 extcon_info[cable->edev->supported_cable[i]].name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900405}
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
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300413 int i = cable->cable_index;
414
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900415 return sprintf(buf, "%d\n",
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900416 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900417}
418
MyungJoo Hamde55d872012-04-20 14:16:22 +0900419/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900420 * extcon_sync() - Synchronize the states for both the attached/detached
421 * @edev: the extcon device that has the cable.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900422 *
Chanwoo Choiab11af042016-07-22 13:16:34 +0900423 * This function send a notification to synchronize the all states of a
424 * specific external connector
MyungJoo Hamde55d872012-04-20 14:16:22 +0900425 */
Chanwoo Choiab11af042016-07-22 13:16:34 +0900426int extcon_sync(struct extcon_dev *edev, unsigned int id)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900427{
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;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900434 int index;
Chanwoo Choiab11af042016-07-22 13:16:34 +0900435 int state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900436 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900437
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900438 if (!edev)
439 return -EINVAL;
440
Chanwoo Choiab11af042016-07-22 13:16:34 +0900441 index = find_cable_index_by_id(edev, id);
442 if (index < 0)
443 return index;
444
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900445 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900446
Chanwoo Choiab11af042016-07-22 13:16:34 +0900447 state = !!(edev->state & BIT(index));
Chanwoo Choi815429b2017-03-29 19:30:17 +0900448
449 /*
450 * Call functions in a raw notifier chain for the specific one
451 * external connector.
452 */
Chanwoo Choiab11af042016-07-22 13:16:34 +0900453 raw_notifier_call_chain(&edev->nh[index], state, edev);
Roger Quadrosf7a89812015-07-06 17:46:58 +0300454
Chanwoo Choi815429b2017-03-29 19:30:17 +0900455 /*
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
Chanwoo Choiab11af042016-07-22 13:16:34 +0900461 /* This could be in interrupt handler */
462 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
463 if (!prop_buf) {
464 /* Unlock early before uevent */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900465 spin_unlock_irqrestore(&edev->lock, flags);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900466
467 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
468 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
469
Pan Biane7d9dd52016-12-03 16:56:49 +0800470 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900471 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900472
Chanwoo Choiab11af042016-07-22 13:16:34 +0900473 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;
487 }
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);
494
MyungJoo Hambde68e62012-04-20 14:16:26 +0900495 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900496}
Chanwoo Choiab11af042016-07-22 13:16:34 +0900497EXPORT_SYMBOL_GPL(extcon_sync);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900498
Donggeun Kim74c5d092012-04-20 14:16:24 +0900499/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900500 * extcon_get_state() - Get the state of a external connector.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900501 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900502 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900503 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900504int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900505{
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900506 int index, state;
507 unsigned long flags;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900508
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900509 if (!edev)
510 return -EINVAL;
511
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900512 index = find_cable_index_by_id(edev, id);
513 if (index < 0)
514 return index;
515
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900516 spin_lock_irqsave(&edev->lock, flags);
517 state = is_extcon_attached(edev, index);
518 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900519
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900520 return state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900521}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900522EXPORT_SYMBOL_GPL(extcon_get_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900523
524/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900525 * extcon_set_state() - Set the state of a external connector.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900526 * without a notification.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900527 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900528 * @id: the unique id of each external connector
529 * in extcon enumeration.
530 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900531 * true: attached / false: detached.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900532 *
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().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900536 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900537int extcon_set_state(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900538 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900539{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900540 unsigned long flags;
541 int index, ret = 0;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900542
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900543 if (!edev)
544 return -EINVAL;
545
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900546 index = find_cable_index_by_id(edev, id);
547 if (index < 0)
548 return index;
549
Chanwoo Choiab11af042016-07-22 13:16:34 +0900550 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
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900562 /*
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
Chanwoo Choiab11af042016-07-22 13:16:34 +0900569 /* 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;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900578}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900579EXPORT_SYMBOL_GPL(extcon_set_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900580
581/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900582 * 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
618/**
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900619 * 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
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900657 /* 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
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900663 /*
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
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900735 /* 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
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900741 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
768/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900769 * 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
793/**
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900794 * 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
883/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900884 * 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
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900891 if (!extcon_name)
892 return ERR_PTR(-EINVAL);
893
Donggeun Kim74c5d092012-04-20 14:16:24 +0900894 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
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900906/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900907 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900908 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900909 * @edev: the extcon device that has the external connecotr.
910 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900911 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900912 *
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.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900916 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900917int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900918 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900919{
Hans de Goede66bee352015-03-21 17:26:24 +0100920 unsigned long flags;
Maninder Singh2c8116a2016-08-01 14:51:30 +0530921 int ret, idx = -EINVAL;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900922
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900923 if (!edev || !nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900924 return -EINVAL;
925
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900926 idx = find_cable_index_by_id(edev, id);
927 if (idx < 0)
928 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100929
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900930 spin_lock_irqsave(&edev->lock, flags);
931 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
932 spin_unlock_irqrestore(&edev->lock, flags);
Hans de Goede66bee352015-03-21 17:26:24 +0100933
934 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900935}
936EXPORT_SYMBOL_GPL(extcon_register_notifier);
937
938/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900939 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900940 * @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.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900943 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900944int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900945 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900946{
Hans de Goede66bee352015-03-21 17:26:24 +0100947 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900948 int ret, idx;
949
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900950 if (!edev || !nb)
951 return -EINVAL;
952
Chanwoo Choi046050f2015-05-19 20:01:12 +0900953 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900954 if (idx < 0)
955 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100956
957 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900958 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100959 spin_unlock_irqrestore(&edev->lock, flags);
960
961 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900962}
963EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
964
Chanwoo Choi815429b2017-03-29 19:30:17 +0900965/**
966 * extcon_register_notifier_all() - Register a notifier block for all connectors
Markus Elfring826a47e2017-04-23 22:15:20 +0200967 * @edev: the extcon device that has the external connector.
Chanwoo Choi815429b2017-03-29 19:30:17 +0900968 * @nb: a notifier block to be registered.
969 *
Markus Elfring826a47e2017-04-23 22:15:20 +0200970 * This function registers a notifier block in order to receive the state
Chanwoo Choi815429b2017-03-29 19:30:17 +0900971 * change of all supported external connectors from extcon device.
Markus Elfring826a47e2017-04-23 22:15:20 +0200972 * And the second parameter given to the callback of nb (val) is
Chanwoo Choi815429b2017-03-29 19:30:17 +0900973 * 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
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001018static struct attribute *extcon_attrs[] = {
1019 &dev_attr_state.attr,
1020 &dev_attr_name.attr,
1021 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +09001022};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001023ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001024
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);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001031 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001032 }
1033
1034 return 0;
1035}
1036
MyungJoo Hamde55d872012-04-20 14:16:22 +09001037static void extcon_dev_release(struct device *dev)
1038{
MyungJoo Hamde55d872012-04-20 14:16:22 +09001039}
1040
MyungJoo Hambde68e62012-04-20 14:16:26 +09001041static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001042static void dummy_sysfs_dev_release(struct device *dev)
1043{
1044}
1045
Chanwoo Choia9af6522014-04-24 19:46:49 +09001046/*
1047 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001048 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +09001049 * 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 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +09001058struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +09001059{
1060 struct extcon_dev *edev;
1061
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001062 if (!supported_cable)
1063 return ERR_PTR(-EINVAL);
1064
Chanwoo Choia9af6522014-04-24 19:46:49 +09001065 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
MyungJoo Hamde55d872012-04-20 14:16:22 +09001085/**
1086 * extcon_dev_register() - Register a new extcon device
1087 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001088 *
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 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001094int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001095{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001096 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001097 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001098
1099 if (!extcon_class) {
1100 ret = create_extcon_class();
1101 if (ret < 0)
1102 return ret;
1103 }
1104
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001105 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001106 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001107
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001108 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1109
1110 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001111 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001112 dev_err(&edev->dev,
1113 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001114 return -EINVAL;
1115 }
1116
Chanwoo Choidae61652013-09-27 09:19:40 +09001117 edev->dev.class = extcon_class;
1118 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001119
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001120 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001121 if (IS_ERR_OR_NULL(edev->name)) {
1122 dev_err(&edev->dev,
1123 "extcon device name is null\n");
1124 return -EINVAL;
1125 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001126 dev_set_name(&edev->dev, "extcon%lu",
1127 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001128
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
Mark Brown9baf3222012-08-16 20:03:21 +01001165 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001166 cable->attr_name.attr.name = "name";
1167 cable->attr_name.attr.mode = 0444;
1168 cable->attr_name.show = cable_name_show;
1169
Mark Brown9baf3222012-08-16 20:03:21 +01001170 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001171 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +09001172 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001173 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001174 }
1175 }
1176
MyungJoo Hambde68e62012-04-20 14:16:26 +09001177 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);
Mark Brown9baf3222012-08-16 20:03:21 +01001215 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001216 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
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001226 if (edev->max_supported) {
1227 edev->extcon_dev_type.groups =
1228 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +09001229 (edev->max_supported + 2), 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 Choi815429b2017-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.
Peter Meerwaldc338bb02012-08-23 09:11:54 +09001299 * @edev: the extcon device instance 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
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
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001356 if (!dev)
1357 return ERR_PTR(-EINVAL);
1358
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001359 if (!dev->of_node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001360 dev_dbg(dev, "device does not have a device node entry\n");
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001361 return ERR_PTR(-EINVAL);
1362 }
1363
1364 node = of_parse_phandle(dev->of_node, "extcon", index);
1365 if (!node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001366 dev_dbg(dev, "failed to get phandle in %s node\n",
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001367 dev->of_node->full_name);
1368 return ERR_PTR(-ENODEV);
1369 }
1370
Tomasz Figaf841afb2014-10-16 15:11:44 +02001371 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);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001375 of_node_put(node);
Tomasz Figaf841afb2014-10-16 15:11:44 +02001376 return edev;
1377 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001378 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001379 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001380 of_node_put(node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001381
Tomasz Figaf841afb2014-10-16 15:11:44 +02001382 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001383}
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
Chanwoo Choi707d7552015-04-15 13:57:51 +09001392/**
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
MyungJoo Hamde55d872012-04-20 14:16:22 +09001401static 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{
MyungJoo Hamde55d872012-04-20 14:16:22 +09001409 class_destroy(extcon_class);
1410}
1411module_exit(extcon_class_exit);
1412
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001413MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001414MODULE_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");