blob: 8fde4befaa5142895560a1871b6528770a28c9de [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>
33#include <linux/extcon.h>
Tomasz Figaf841afb12014-10-16 15:11:44 +020034#include <linux/of.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090035#include <linux/slab.h>
Mark Brown9baf3222012-08-16 20:03:21 +010036#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090037
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090038#define SUPPORTED_CABLE_MAX 32
39#define CABLE_NAME_MAX 30
40
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090041struct __extcon_info {
42 unsigned int type;
43 unsigned int id;
44 const char *name;
45
46} extcon_info[] = {
47 [EXTCON_NONE] = {
48 .type = EXTCON_TYPE_MISC,
49 .id = EXTCON_NONE,
50 .name = "NONE",
51 },
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090052
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090053 /* USB external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090054 [EXTCON_USB] = {
55 .type = EXTCON_TYPE_USB,
56 .id = EXTCON_USB,
57 .name = "USB",
58 },
59 [EXTCON_USB_HOST] = {
60 .type = EXTCON_TYPE_USB,
61 .id = EXTCON_USB_HOST,
62 .name = "USB_HOST",
63 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090064
Chanwoo Choi11eecf92015-10-03 14:15:13 +090065 /* Charging external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090066 [EXTCON_CHG_USB_SDP] = {
67 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
68 .id = EXTCON_CHG_USB_SDP,
69 .name = "SDP",
70 },
71 [EXTCON_CHG_USB_DCP] = {
72 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
73 .id = EXTCON_CHG_USB_DCP,
74 .name = "DCP",
75 },
76 [EXTCON_CHG_USB_CDP] = {
77 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
78 .id = EXTCON_CHG_USB_CDP,
79 .name = "CDP",
80 },
81 [EXTCON_CHG_USB_ACA] = {
82 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
83 .id = EXTCON_CHG_USB_ACA,
84 .name = "ACA",
85 },
86 [EXTCON_CHG_USB_FAST] = {
87 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
88 .id = EXTCON_CHG_USB_FAST,
89 .name = "FAST-CHARGER",
90 },
91 [EXTCON_CHG_USB_SLOW] = {
92 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
93 .id = EXTCON_CHG_USB_SLOW,
94 .name = "SLOW-CHARGER",
95 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090096
Chanwoo Choi11eecf92015-10-03 14:15:13 +090097 /* Jack external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090098 [EXTCON_JACK_MICROPHONE] = {
99 .type = EXTCON_TYPE_JACK,
100 .id = EXTCON_JACK_MICROPHONE,
101 .name = "MICROPHONE",
102 },
103 [EXTCON_JACK_HEADPHONE] = {
104 .type = EXTCON_TYPE_JACK,
105 .id = EXTCON_JACK_HEADPHONE,
106 .name = "HEADPHONE",
107 },
108 [EXTCON_JACK_LINE_IN] = {
109 .type = EXTCON_TYPE_JACK,
110 .id = EXTCON_JACK_LINE_IN,
111 .name = "LINE-IN",
112 },
113 [EXTCON_JACK_LINE_OUT] = {
114 .type = EXTCON_TYPE_JACK,
115 .id = EXTCON_JACK_LINE_OUT,
116 .name = "LINE-OUT",
117 },
118 [EXTCON_JACK_VIDEO_IN] = {
119 .type = EXTCON_TYPE_JACK,
120 .id = EXTCON_JACK_VIDEO_IN,
121 .name = "VIDEO-IN",
122 },
123 [EXTCON_JACK_VIDEO_OUT] = {
124 .type = EXTCON_TYPE_JACK,
125 .id = EXTCON_JACK_VIDEO_OUT,
126 .name = "VIDEO-OUT",
127 },
128 [EXTCON_JACK_SPDIF_IN] = {
129 .type = EXTCON_TYPE_JACK,
130 .id = EXTCON_JACK_SPDIF_IN,
131 .name = "SPDIF-IN",
132 },
133 [EXTCON_JACK_SPDIF_OUT] = {
134 .type = EXTCON_TYPE_JACK,
135 .id = EXTCON_JACK_SPDIF_OUT,
136 .name = "SPDIF-OUT",
137 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900138
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900139 /* Display external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900140 [EXTCON_DISP_HDMI] = {
141 .type = EXTCON_TYPE_DISP,
142 .id = EXTCON_DISP_HDMI,
143 .name = "HDMI",
144 },
145 [EXTCON_DISP_MHL] = {
146 .type = EXTCON_TYPE_DISP,
147 .id = EXTCON_DISP_MHL,
148 .name = "MHL",
149 },
150 [EXTCON_DISP_DVI] = {
151 .type = EXTCON_TYPE_DISP,
152 .id = EXTCON_DISP_DVI,
153 .name = "DVI",
154 },
155 [EXTCON_DISP_VGA] = {
156 .type = EXTCON_TYPE_DISP,
157 .id = EXTCON_DISP_VGA,
158 .name = "VGA",
159 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900160
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900161 /* Miscellaneous external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900162 [EXTCON_DOCK] = {
163 .type = EXTCON_TYPE_MISC,
164 .id = EXTCON_DOCK,
165 .name = "DOCK",
166 },
167 [EXTCON_JIG] = {
168 .type = EXTCON_TYPE_MISC,
169 .id = EXTCON_JIG,
170 .name = "JIG",
171 },
172 [EXTCON_MECHANICAL] = {
173 .type = EXTCON_TYPE_MISC,
174 .id = EXTCON_MECHANICAL,
175 .name = "MECHANICAL",
176 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900177
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900178 { /* sentinel */ }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900179};
180
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900181/**
182 * struct extcon_cable - An internal data for each cable of extcon device.
183 * @edev: The extcon device
184 * @cable_index: Index of this cable in the edev
185 * @attr_g: Attribute group for the cable
186 * @attr_name: "name" sysfs entry
187 * @attr_state: "state" sysfs entry
188 * @attrs: Array pointing to attr_name and attr_state for attr_g
189 */
190struct extcon_cable {
191 struct extcon_dev *edev;
192 int cable_index;
193
194 struct attribute_group attr_g;
195 struct device_attribute attr_name;
196 struct device_attribute attr_state;
197
198 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900199
200 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
201 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
202 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
203 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900204
205 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
206 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
207 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
208 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900209};
210
Mark Brownbe3a07f2012-06-05 16:14:38 +0100211static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900212#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900213static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900214#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900215
Donggeun Kim74c5d092012-04-20 14:16:24 +0900216static LIST_HEAD(extcon_dev_list);
217static DEFINE_MUTEX(extcon_dev_list_lock);
218
MyungJoo Hambde68e62012-04-20 14:16:26 +0900219/**
220 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900221 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +0900222 * @edev: the extcon device
223 * @new_state: new cable attach status for @edev
224 *
225 * Returns 0 if nothing violates. Returns the index + 1 for the first
226 * violated condition.
227 */
228static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
229{
230 int i = 0;
231
232 if (!edev->mutually_exclusive)
233 return 0;
234
235 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530236 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900237 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900238
anish kumar28c0ada2012-08-30 00:35:10 +0530239 /* calculate the total number of bits set */
240 weight = hweight32(correspondants);
241 if (weight > 1)
242 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900243 }
244
245 return 0;
246}
247
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900248static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900249{
250 int i;
251
252 /* Find the the index of extcon cable in edev->supported_cable */
253 for (i = 0; i < edev->max_supported; i++) {
254 if (edev->supported_cable[i] == id)
255 return i;
256 }
257
258 return -EINVAL;
259}
260
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900261static int get_extcon_type(unsigned int prop)
262{
263 switch (prop) {
264 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
265 return EXTCON_TYPE_USB;
266 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
267 return EXTCON_TYPE_CHG;
268 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
269 return EXTCON_TYPE_JACK;
270 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
271 return EXTCON_TYPE_DISP;
272 default:
273 return -EINVAL;
274 }
275}
276
277static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
278{
279 return !!(edev->state & BIT(index));
280}
281
Chanwoo Choiab11af042016-07-22 13:16:34 +0900282static bool is_extcon_changed(struct extcon_dev *edev, int index,
283 bool new_state)
Chanwoo Choi046050f2015-05-19 20:01:12 +0900284{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900285 int state = !!(edev->state & BIT(index));
286 return (state != new_state);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900287}
288
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900289static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
290{
291 int type;
292
293 /* Check whether the property is supported or not. */
294 type = get_extcon_type(prop);
295 if (type < 0)
296 return false;
297
298 /* Check whether a specific extcon id supports the property or not. */
299 return !!(extcon_info[id].type & type);
300}
301
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900302static int is_extcon_property_capability(struct extcon_dev *edev,
303 unsigned int id, int index,unsigned int prop)
304{
305 struct extcon_cable *cable;
306 int type, ret;
307
308 /* Check whether the property is supported or not. */
309 type = get_extcon_type(prop);
310 if (type < 0)
311 return type;
312
313 cable = &edev->cables[index];
314
315 switch (type) {
316 case EXTCON_TYPE_USB:
317 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
318 break;
319 case EXTCON_TYPE_CHG:
320 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
321 break;
322 case EXTCON_TYPE_JACK:
323 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
324 break;
325 case EXTCON_TYPE_DISP:
326 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
327 break;
328 default:
329 ret = -EINVAL;
330 }
331
332 return ret;
333}
334
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900335static void init_property(struct extcon_dev *edev, unsigned int id, int index)
336{
337 unsigned int type = extcon_info[id].type;
338 struct extcon_cable *cable = &edev->cables[index];
339
340 if (EXTCON_TYPE_USB & type)
341 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
342 if (EXTCON_TYPE_CHG & type)
343 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
344 if (EXTCON_TYPE_JACK & type)
345 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
346 if (EXTCON_TYPE_DISP & type)
347 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
348}
349
MyungJoo Hamde55d872012-04-20 14:16:22 +0900350static ssize_t state_show(struct device *dev, struct device_attribute *attr,
351 char *buf)
352{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900353 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900354 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900355
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900356 if (edev->max_supported == 0)
357 return sprintf(buf, "%u\n", edev->state);
358
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900359 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900360 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900361 extcon_info[edev->supported_cable[i]].name,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900362 !!(edev->state & (1 << i)));
363 }
364
365 return count;
366}
Chanwoo Choi5d5321e2016-07-18 15:39:28 +0900367static DEVICE_ATTR_RO(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900368
369static ssize_t name_show(struct device *dev, struct device_attribute *attr,
370 char *buf)
371{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900372 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900373
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900374 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900375}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700376static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900377
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900378static ssize_t cable_name_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
381 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
382 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900383 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900384
385 return sprintf(buf, "%s\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900386 extcon_info[cable->edev->supported_cable[i]].name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900387}
388
389static ssize_t cable_state_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
392 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
393 attr_state);
394
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300395 int i = cable->cable_index;
396
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900397 return sprintf(buf, "%d\n",
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900398 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900399}
400
MyungJoo Hamde55d872012-04-20 14:16:22 +0900401/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900402 * extcon_sync() - Synchronize the states for both the attached/detached
403 * @edev: the extcon device that has the cable.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900404 *
Chanwoo Choiab11af042016-07-22 13:16:34 +0900405 * This function send a notification to synchronize the all states of a
406 * specific external connector
MyungJoo Hamde55d872012-04-20 14:16:22 +0900407 */
Chanwoo Choiab11af042016-07-22 13:16:34 +0900408int extcon_sync(struct extcon_dev *edev, unsigned int id)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900409{
410 char name_buf[120];
411 char state_buf[120];
412 char *prop_buf;
413 char *envp[3];
414 int env_offset = 0;
415 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900416 int index;
Chanwoo Choiab11af042016-07-22 13:16:34 +0900417 int state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900418 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900419
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900420 if (!edev)
421 return -EINVAL;
422
Chanwoo Choiab11af042016-07-22 13:16:34 +0900423 index = find_cable_index_by_id(edev, id);
424 if (index < 0)
425 return index;
426
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900427 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900428
Chanwoo Choiab11af042016-07-22 13:16:34 +0900429 state = !!(edev->state & BIT(index));
430 raw_notifier_call_chain(&edev->nh[index], state, edev);
Roger Quadrosf7a89812015-07-06 17:46:58 +0300431
Chanwoo Choiab11af042016-07-22 13:16:34 +0900432 /* This could be in interrupt handler */
433 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
434 if (!prop_buf) {
435 /* Unlock early before uevent */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900436 spin_unlock_irqrestore(&edev->lock, flags);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900437
438 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
439 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
440
441 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900442 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900443
Chanwoo Choiab11af042016-07-22 13:16:34 +0900444 length = name_show(&edev->dev, NULL, prop_buf);
445 if (length > 0) {
446 if (prop_buf[length - 1] == '\n')
447 prop_buf[length - 1] = 0;
448 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
449 envp[env_offset++] = name_buf;
450 }
451
452 length = state_show(&edev->dev, NULL, prop_buf);
453 if (length > 0) {
454 if (prop_buf[length - 1] == '\n')
455 prop_buf[length - 1] = 0;
456 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
457 envp[env_offset++] = state_buf;
458 }
459 envp[env_offset] = NULL;
460
461 /* Unlock early before uevent */
462 spin_unlock_irqrestore(&edev->lock, flags);
463 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
464 free_page((unsigned long)prop_buf);
465
MyungJoo Hambde68e62012-04-20 14:16:26 +0900466 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900467}
Chanwoo Choiab11af042016-07-22 13:16:34 +0900468EXPORT_SYMBOL_GPL(extcon_sync);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900469
Donggeun Kim74c5d092012-04-20 14:16:24 +0900470/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900471 * extcon_get_state() - Get the state of a external connector.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900472 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900473 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900474 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900475int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900476{
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900477 int index, state;
478 unsigned long flags;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900479
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900480 if (!edev)
481 return -EINVAL;
482
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900483 index = find_cable_index_by_id(edev, id);
484 if (index < 0)
485 return index;
486
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900487 spin_lock_irqsave(&edev->lock, flags);
488 state = is_extcon_attached(edev, index);
489 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900490
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900491 return state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900492}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900493EXPORT_SYMBOL_GPL(extcon_get_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900494
495/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900496 * extcon_set_state() - Set the state of a external connector.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900497 * without a notification.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900498 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900499 * @id: the unique id of each external connector
500 * in extcon enumeration.
501 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900502 * true: attached / false: detached.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900503 *
504 * This function only set the state of a external connector without
505 * a notification. To synchronize the data of a external connector,
506 * use extcon_set_state_sync() and extcon_sync().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900507 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900508int extcon_set_state(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900509 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900510{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900511 unsigned long flags;
512 int index, ret = 0;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900513
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900514 if (!edev)
515 return -EINVAL;
516
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900517 index = find_cable_index_by_id(edev, id);
518 if (index < 0)
519 return index;
520
Chanwoo Choiab11af042016-07-22 13:16:34 +0900521 spin_lock_irqsave(&edev->lock, flags);
522
523 /* Check whether the external connector's state is changed. */
524 if (!is_extcon_changed(edev, index, cable_state))
525 goto out;
526
527 if (check_mutually_exclusive(edev,
528 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
529 ret = -EPERM;
530 goto out;
531 }
532
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900533 /*
534 * Initialize the value of extcon property before setting
535 * the detached state for an external connector.
536 */
537 if (!cable_state)
538 init_property(edev, id, index);
539
Chanwoo Choiab11af042016-07-22 13:16:34 +0900540 /* Update the state for a external connector. */
541 if (cable_state)
542 edev->state |= BIT(index);
543 else
544 edev->state &= ~(BIT(index));
545out:
546 spin_unlock_irqrestore(&edev->lock, flags);
547
548 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900549}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900550EXPORT_SYMBOL_GPL(extcon_set_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900551
552/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900553 * extcon_set_state_sync() - Set the state of a external connector
554 * with a notification.
555 * @edev: the extcon device that has the cable.
556 * @id: the unique id of each external connector
557 * in extcon enumeration.
558 * @state: the new cable status. The default semantics is
559 * true: attached / false: detached.
560 *
561 * This function set the state of external connector and synchronize the data
562 * by usning a notification.
563 */
564int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
565 bool cable_state)
566{
567 int ret, index;
568 unsigned long flags;
569
570 index = find_cable_index_by_id(edev, id);
571 if (index < 0)
572 return index;
573
574 /* Check whether the external connector's state is changed. */
575 spin_lock_irqsave(&edev->lock, flags);
576 ret = is_extcon_changed(edev, index, cable_state);
577 spin_unlock_irqrestore(&edev->lock, flags);
578 if (!ret)
579 return 0;
580
581 ret = extcon_set_state(edev, id, cable_state);
582 if (ret < 0)
583 return ret;
584
585 return extcon_sync(edev, id);
586}
587EXPORT_SYMBOL_GPL(extcon_set_state_sync);
588
589/**
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900590 * extcon_get_property() - Get the property value of a specific cable.
591 * @edev: the extcon device that has the cable.
592 * @id: the unique id of each external connector
593 * in extcon enumeration.
594 * @prop: the property id among enum extcon_property.
595 * @prop_val: the pointer which store the value of property.
596 *
597 * When getting the property value of external connector, the external connector
598 * should be attached. If detached state, function just return 0 without
599 * property value. Also, the each property should be included in the list of
600 * supported properties according to the type of external connectors.
601 *
602 * Returns 0 if success or error number if fail
603 */
604int extcon_get_property(struct extcon_dev *edev, unsigned int id,
605 unsigned int prop,
606 union extcon_property_value *prop_val)
607{
608 struct extcon_cable *cable;
609 unsigned long flags;
610 int index, ret = 0;
611
612 *prop_val = (union extcon_property_value)(0);
613
614 if (!edev)
615 return -EINVAL;
616
617 /* Check whether the property is supported or not */
618 if (!is_extcon_property_supported(id, prop))
619 return -EINVAL;
620
621 /* Find the cable index of external connector by using id */
622 index = find_cable_index_by_id(edev, id);
623 if (index < 0)
624 return index;
625
626 spin_lock_irqsave(&edev->lock, flags);
627
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900628 /* Check whether the property is available or not. */
629 if (!is_extcon_property_capability(edev, id, index, prop)) {
630 spin_unlock_irqrestore(&edev->lock, flags);
631 return -EPERM;
632 }
633
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900634 /*
635 * Check whether the external connector is attached.
636 * If external connector is detached, the user can not
637 * get the property value.
638 */
639 if (!is_extcon_attached(edev, index)) {
640 spin_unlock_irqrestore(&edev->lock, flags);
641 return 0;
642 }
643
644 cable = &edev->cables[index];
645
646 /* Get the property value according to extcon type */
647 switch (prop) {
648 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
649 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
650 break;
651 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
652 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
653 break;
654 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
655 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
656 break;
657 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
658 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
659 break;
660 default:
661 ret = -EINVAL;
662 break;
663 }
664
665 spin_unlock_irqrestore(&edev->lock, flags);
666
667 return ret;
668}
669EXPORT_SYMBOL_GPL(extcon_get_property);
670
671/**
672 * extcon_set_property() - Set the property value of a specific cable.
673 * @edev: the extcon device that has the cable.
674 * @id: the unique id of each external connector
675 * in extcon enumeration.
676 * @prop: the property id among enum extcon_property.
677 * @prop_val: the pointer including the new value of property.
678 *
679 * The each property should be included in the list of supported properties
680 * according to the type of external connectors.
681 *
682 * Returns 0 if success or error number if fail
683 */
684int extcon_set_property(struct extcon_dev *edev, unsigned int id,
685 unsigned int prop,
686 union extcon_property_value prop_val)
687{
688 struct extcon_cable *cable;
689 unsigned long flags;
690 int index, ret = 0;
691
692 if (!edev)
693 return -EINVAL;
694
695 /* Check whether the property is supported or not */
696 if (!is_extcon_property_supported(id, prop))
697 return -EINVAL;
698
699 /* Find the cable index of external connector by using id */
700 index = find_cable_index_by_id(edev, id);
701 if (index < 0)
702 return index;
703
704 spin_lock_irqsave(&edev->lock, flags);
705
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900706 /* Check whether the property is available or not. */
707 if (!is_extcon_property_capability(edev, id, index, prop)) {
708 spin_unlock_irqrestore(&edev->lock, flags);
709 return -EPERM;
710 }
711
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900712 cable = &edev->cables[index];
713
714 /* Set the property value according to extcon type */
715 switch (prop) {
716 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
717 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
718 break;
719 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
720 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
721 break;
722 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
723 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
724 break;
725 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
726 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
727 break;
728 default:
729 ret = -EINVAL;
730 break;
731 }
732
733 spin_unlock_irqrestore(&edev->lock, flags);
734
735 return ret;
736}
737EXPORT_SYMBOL_GPL(extcon_set_property);
738
739/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900740 * extcon_set_property_sync() - Set the property value of a specific cable
741 with a notification.
742 * @prop_val: the pointer including the new value of property.
743 *
744 * When setting the property value of external connector, the external connector
745 * should be attached. The each property should be included in the list of
746 * supported properties according to the type of external connectors.
747 *
748 * Returns 0 if success or error number if fail
749 */
750int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
751 unsigned int prop,
752 union extcon_property_value prop_val)
753{
754 int ret;
755
756 ret = extcon_set_property(edev, id, prop, prop_val);
757 if (ret < 0)
758 return ret;
759
760 return extcon_sync(edev, id);
761}
762EXPORT_SYMBOL_GPL(extcon_set_property_sync);
763
764/**
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900765 * extcon_get_property_capability() - Get the capability of property
766 * of an external connector.
767 * @edev: the extcon device that has the cable.
768 * @id: the unique id of each external connector
769 * in extcon enumeration.
770 * @prop: the property id among enum extcon_property.
771 *
772 * Returns 1 if the property is available or 0 if not available.
773 */
774int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
775 unsigned int prop)
776{
777 int index;
778
779 if (!edev)
780 return -EINVAL;
781
782 /* Check whether the property is supported or not */
783 if (!is_extcon_property_supported(id, prop))
784 return -EINVAL;
785
786 /* Find the cable index of external connector by using id */
787 index = find_cable_index_by_id(edev, id);
788 if (index < 0)
789 return index;
790
791 return is_extcon_property_capability(edev, id, index, prop);
792}
793EXPORT_SYMBOL_GPL(extcon_get_property_capability);
794
795/**
796 * extcon_set_property_capability() - Set the capability of a property
797 * of an external connector.
798 * @edev: the extcon device that has the cable.
799 * @id: the unique id of each external connector
800 * in extcon enumeration.
801 * @prop: the property id among enum extcon_property.
802 *
803 * This function set the capability of a property for an external connector
804 * to mark the bit in capability bitmap which mean the available state of
805 * a property.
806 *
807 * Returns 0 if success or error number if fail
808 */
809int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
810 unsigned int prop)
811{
812 struct extcon_cable *cable;
813 int index, type, ret = 0;
814
815 if (!edev)
816 return -EINVAL;
817
818 /* Check whether the property is supported or not. */
819 if (!is_extcon_property_supported(id, prop))
820 return -EINVAL;
821
822 /* Find the cable index of external connector by using id. */
823 index = find_cable_index_by_id(edev, id);
824 if (index < 0)
825 return index;
826
827 type = get_extcon_type(prop);
828 if (type < 0)
829 return type;
830
831 cable = &edev->cables[index];
832
833 switch (type) {
834 case EXTCON_TYPE_USB:
835 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
836 break;
837 case EXTCON_TYPE_CHG:
838 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
839 break;
840 case EXTCON_TYPE_JACK:
841 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
842 break;
843 case EXTCON_TYPE_DISP:
844 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
845 break;
846 default:
847 ret = -EINVAL;
848 }
849
850 return ret;
851}
852EXPORT_SYMBOL_GPL(extcon_set_property_capability);
853
854/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900855 * extcon_get_extcon_dev() - Get the extcon device instance from the name
856 * @extcon_name: The extcon name provided with extcon_dev_register()
857 */
858struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
859{
860 struct extcon_dev *sd;
861
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900862 if (!extcon_name)
863 return ERR_PTR(-EINVAL);
864
Donggeun Kim74c5d092012-04-20 14:16:24 +0900865 mutex_lock(&extcon_dev_list_lock);
866 list_for_each_entry(sd, &extcon_dev_list, entry) {
867 if (!strcmp(sd->name, extcon_name))
868 goto out;
869 }
870 sd = NULL;
871out:
872 mutex_unlock(&extcon_dev_list_lock);
873 return sd;
874}
875EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
876
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900877/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900878 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900879 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900880 * @edev: the extcon device that has the external connecotr.
881 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900882 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900883 *
884 * Note that the second parameter given to the callback of nb (val) is
885 * "old_state", not the current state. The current state can be retrieved
886 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900887 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900888int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900889 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900890{
Hans de Goede66bee352015-03-21 17:26:24 +0100891 unsigned long flags;
Maninder Singh2c8116a2016-08-01 14:51:30 +0530892 int ret, idx = -EINVAL;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900893
Chanwoo Choi830ae442016-05-31 17:32:30 +0900894 if (!nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900895 return -EINVAL;
896
Chanwoo Choi830ae442016-05-31 17:32:30 +0900897 if (edev) {
898 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900899 if (idx < 0)
900 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100901
Chanwoo Choi830ae442016-05-31 17:32:30 +0900902 spin_lock_irqsave(&edev->lock, flags);
903 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
904 spin_unlock_irqrestore(&edev->lock, flags);
905 } else {
906 struct extcon_dev *extd;
907
908 mutex_lock(&extcon_dev_list_lock);
909 list_for_each_entry(extd, &extcon_dev_list, entry) {
910 idx = find_cable_index_by_id(extd, id);
911 if (idx >= 0)
912 break;
913 }
914 mutex_unlock(&extcon_dev_list_lock);
915
916 if (idx >= 0) {
917 edev = extd;
918 return extcon_register_notifier(extd, id, nb);
919 } else {
920 ret = -ENODEV;
921 }
922 }
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/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900929 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900930 * @edev: the extcon device that has the external connecotr.
931 * @id: the unique id of each external connector in extcon enumeration.
932 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900933 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900934int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900935 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900936{
Hans de Goede66bee352015-03-21 17:26:24 +0100937 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900938 int ret, idx;
939
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900940 if (!edev || !nb)
941 return -EINVAL;
942
Chanwoo Choi046050f2015-05-19 20:01:12 +0900943 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900944 if (idx < 0)
945 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100946
947 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900948 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100949 spin_unlock_irqrestore(&edev->lock, flags);
950
951 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900952}
953EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
954
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700955static struct attribute *extcon_attrs[] = {
956 &dev_attr_state.attr,
957 &dev_attr_name.attr,
958 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900959};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700960ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900961
962static int create_extcon_class(void)
963{
964 if (!extcon_class) {
965 extcon_class = class_create(THIS_MODULE, "extcon");
966 if (IS_ERR(extcon_class))
967 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700968 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900969
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900970#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900971 switch_class = class_compat_register("switch");
972 if (WARN(!switch_class, "cannot allocate"))
973 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900974#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900975 }
976
977 return 0;
978}
979
MyungJoo Hamde55d872012-04-20 14:16:22 +0900980static void extcon_dev_release(struct device *dev)
981{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900982}
983
MyungJoo Hambde68e62012-04-20 14:16:26 +0900984static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900985static void dummy_sysfs_dev_release(struct device *dev)
986{
987}
988
Chanwoo Choia9af6522014-04-24 19:46:49 +0900989/*
990 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900991 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900992 * If supported_cable is NULL, cable name related APIs
993 * are disabled.
994 *
995 * This function allocates the memory for extcon device without allocating
996 * memory in each extcon provider driver and initialize default setting for
997 * extcon device.
998 *
999 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
1000 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +09001001struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +09001002{
1003 struct extcon_dev *edev;
1004
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001005 if (!supported_cable)
1006 return ERR_PTR(-EINVAL);
1007
Chanwoo Choia9af6522014-04-24 19:46:49 +09001008 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1009 if (!edev)
1010 return ERR_PTR(-ENOMEM);
1011
1012 edev->max_supported = 0;
1013 edev->supported_cable = supported_cable;
1014
1015 return edev;
1016}
1017
1018/*
1019 * extcon_dev_free() - Free the memory of extcon device.
1020 * @edev: the extcon device to free
1021 */
1022void extcon_dev_free(struct extcon_dev *edev)
1023{
1024 kfree(edev);
1025}
1026EXPORT_SYMBOL_GPL(extcon_dev_free);
1027
MyungJoo Hamde55d872012-04-20 14:16:22 +09001028/**
1029 * extcon_dev_register() - Register a new extcon device
1030 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001031 *
1032 * Among the members of edev struct, please set the "user initializing data"
1033 * in any case and set the "optional callbacks" if required. However, please
1034 * do not set the values of "internal data", which are initialized by
1035 * this function.
1036 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001037int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001038{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001039 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001040 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001041
1042 if (!extcon_class) {
1043 ret = create_extcon_class();
1044 if (ret < 0)
1045 return ret;
1046 }
1047
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001048 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001049 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001050
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001051 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1052
1053 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001054 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001055 dev_err(&edev->dev,
1056 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001057 return -EINVAL;
1058 }
1059
Chanwoo Choidae61652013-09-27 09:19:40 +09001060 edev->dev.class = extcon_class;
1061 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001062
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001063 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001064 if (IS_ERR_OR_NULL(edev->name)) {
1065 dev_err(&edev->dev,
1066 "extcon device name is null\n");
1067 return -EINVAL;
1068 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001069 dev_set_name(&edev->dev, "extcon%lu",
1070 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001071
1072 if (edev->max_supported) {
1073 char buf[10];
1074 char *str;
1075 struct extcon_cable *cable;
1076
1077 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1078 edev->max_supported, GFP_KERNEL);
1079 if (!edev->cables) {
1080 ret = -ENOMEM;
1081 goto err_sysfs_alloc;
1082 }
1083 for (index = 0; index < edev->max_supported; index++) {
1084 cable = &edev->cables[index];
1085
1086 snprintf(buf, 10, "cable.%d", index);
1087 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1088 GFP_KERNEL);
1089 if (!str) {
1090 for (index--; index >= 0; index--) {
1091 cable = &edev->cables[index];
1092 kfree(cable->attr_g.name);
1093 }
1094 ret = -ENOMEM;
1095
1096 goto err_alloc_cables;
1097 }
1098 strcpy(str, buf);
1099
1100 cable->edev = edev;
1101 cable->cable_index = index;
1102 cable->attrs[0] = &cable->attr_name.attr;
1103 cable->attrs[1] = &cable->attr_state.attr;
1104 cable->attrs[2] = NULL;
1105 cable->attr_g.name = str;
1106 cable->attr_g.attrs = cable->attrs;
1107
Mark Brown9baf3222012-08-16 20:03:21 +01001108 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001109 cable->attr_name.attr.name = "name";
1110 cable->attr_name.attr.mode = 0444;
1111 cable->attr_name.show = cable_name_show;
1112
Mark Brown9baf3222012-08-16 20:03:21 +01001113 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001114 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +09001115 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001116 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001117 }
1118 }
1119
MyungJoo Hambde68e62012-04-20 14:16:26 +09001120 if (edev->max_supported && edev->mutually_exclusive) {
1121 char buf[80];
1122 char *name;
1123
1124 /* Count the size of mutually_exclusive array */
1125 for (index = 0; edev->mutually_exclusive[index]; index++)
1126 ;
1127
1128 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1129 (index + 1), GFP_KERNEL);
1130 if (!edev->attrs_muex) {
1131 ret = -ENOMEM;
1132 goto err_muex;
1133 }
1134
1135 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1136 index, GFP_KERNEL);
1137 if (!edev->d_attrs_muex) {
1138 ret = -ENOMEM;
1139 kfree(edev->attrs_muex);
1140 goto err_muex;
1141 }
1142
1143 for (index = 0; edev->mutually_exclusive[index]; index++) {
1144 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1145 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1146 GFP_KERNEL);
1147 if (!name) {
1148 for (index--; index >= 0; index--) {
1149 kfree(edev->d_attrs_muex[index].attr.
1150 name);
1151 }
1152 kfree(edev->d_attrs_muex);
1153 kfree(edev->attrs_muex);
1154 ret = -ENOMEM;
1155 goto err_muex;
1156 }
1157 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +01001158 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001159 edev->d_attrs_muex[index].attr.name = name;
1160 edev->d_attrs_muex[index].attr.mode = 0000;
1161 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1162 .attr;
1163 }
1164 edev->attr_g_muex.name = muex_name;
1165 edev->attr_g_muex.attrs = edev->attrs_muex;
1166
1167 }
1168
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001169 if (edev->max_supported) {
1170 edev->extcon_dev_type.groups =
1171 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +09001172 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001173 if (!edev->extcon_dev_type.groups) {
1174 ret = -ENOMEM;
1175 goto err_alloc_groups;
1176 }
1177
Chanwoo Choidae61652013-09-27 09:19:40 +09001178 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001179 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1180
1181 for (index = 0; index < edev->max_supported; index++)
1182 edev->extcon_dev_type.groups[index] =
1183 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +09001184 if (edev->mutually_exclusive)
1185 edev->extcon_dev_type.groups[index] =
1186 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001187
Chanwoo Choidae61652013-09-27 09:19:40 +09001188 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001189 }
1190
Chanwoo Choidae61652013-09-27 09:19:40 +09001191 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001192 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +09001193 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001194 goto err_dev;
1195 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001196#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001197 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001198 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001199#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +09001200
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001201 spin_lock_init(&edev->lock);
1202
Chanwoo Choi046050f2015-05-19 20:01:12 +09001203 edev->nh = devm_kzalloc(&edev->dev,
1204 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1205 if (!edev->nh) {
1206 ret = -ENOMEM;
1207 goto err_dev;
1208 }
1209
1210 for (index = 0; index < edev->max_supported; index++)
1211 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +09001212
Chanwoo Choidae61652013-09-27 09:19:40 +09001213 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001214 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +09001215
1216 mutex_lock(&extcon_dev_list_lock);
1217 list_add(&edev->entry, &extcon_dev_list);
1218 mutex_unlock(&extcon_dev_list_lock);
1219
MyungJoo Hamde55d872012-04-20 14:16:22 +09001220 return 0;
1221
1222err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001223 if (edev->max_supported)
1224 kfree(edev->extcon_dev_type.groups);
1225err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +09001226 if (edev->max_supported && edev->mutually_exclusive) {
1227 for (index = 0; edev->mutually_exclusive[index]; index++)
1228 kfree(edev->d_attrs_muex[index].attr.name);
1229 kfree(edev->d_attrs_muex);
1230 kfree(edev->attrs_muex);
1231 }
1232err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001233 for (index = 0; index < edev->max_supported; index++)
1234 kfree(edev->cables[index].attr_g.name);
1235err_alloc_cables:
1236 if (edev->max_supported)
1237 kfree(edev->cables);
1238err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +09001239 return ret;
1240}
1241EXPORT_SYMBOL_GPL(extcon_dev_register);
1242
1243/**
1244 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +09001245 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +09001246 *
1247 * Note that this does not call kfree(edev) because edev was not allocated
1248 * by this class.
1249 */
1250void extcon_dev_unregister(struct extcon_dev *edev)
1251{
anish kumar57e7cd32012-10-22 09:43:33 +09001252 int index;
1253
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001254 if (!edev)
1255 return;
1256
anish kumar57e7cd32012-10-22 09:43:33 +09001257 mutex_lock(&extcon_dev_list_lock);
1258 list_del(&edev->entry);
1259 mutex_unlock(&extcon_dev_list_lock);
1260
Chanwoo Choidae61652013-09-27 09:19:40 +09001261 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1262 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1263 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +09001264 return;
1265 }
1266
Wang, Xiaoming7585ca02013-11-01 18:48:14 -04001267 device_unregister(&edev->dev);
1268
anish kumar57e7cd32012-10-22 09:43:33 +09001269 if (edev->mutually_exclusive && edev->max_supported) {
1270 for (index = 0; edev->mutually_exclusive[index];
1271 index++)
1272 kfree(edev->d_attrs_muex[index].attr.name);
1273 kfree(edev->d_attrs_muex);
1274 kfree(edev->attrs_muex);
1275 }
1276
1277 for (index = 0; index < edev->max_supported; index++)
1278 kfree(edev->cables[index].attr_g.name);
1279
1280 if (edev->max_supported) {
1281 kfree(edev->extcon_dev_type.groups);
1282 kfree(edev->cables);
1283 }
1284
1285#if defined(CONFIG_ANDROID)
1286 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001287 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +09001288#endif
Chanwoo Choidae61652013-09-27 09:19:40 +09001289 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001290}
1291EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1292
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001293#ifdef CONFIG_OF
1294/*
1295 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1296 * @dev - instance to the given device
1297 * @index - index into list of extcon_dev
1298 *
1299 * return the instance of extcon device
1300 */
1301struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1302{
1303 struct device_node *node;
1304 struct extcon_dev *edev;
1305
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001306 if (!dev)
1307 return ERR_PTR(-EINVAL);
1308
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001309 if (!dev->of_node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001310 dev_dbg(dev, "device does not have a device node entry\n");
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001311 return ERR_PTR(-EINVAL);
1312 }
1313
1314 node = of_parse_phandle(dev->of_node, "extcon", index);
1315 if (!node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001316 dev_dbg(dev, "failed to get phandle in %s node\n",
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001317 dev->of_node->full_name);
1318 return ERR_PTR(-ENODEV);
1319 }
1320
Tomasz Figaf841afb12014-10-16 15:11:44 +02001321 mutex_lock(&extcon_dev_list_lock);
1322 list_for_each_entry(edev, &extcon_dev_list, entry) {
1323 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1324 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001325 of_node_put(node);
Tomasz Figaf841afb12014-10-16 15:11:44 +02001326 return edev;
1327 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001328 }
Tomasz Figaf841afb12014-10-16 15:11:44 +02001329 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001330 of_node_put(node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001331
Tomasz Figaf841afb12014-10-16 15:11:44 +02001332 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001333}
1334#else
1335struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1336{
1337 return ERR_PTR(-ENOSYS);
1338}
1339#endif /* CONFIG_OF */
1340EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1341
Chanwoo Choi707d7552015-04-15 13:57:51 +09001342/**
1343 * extcon_get_edev_name() - Get the name of the extcon device.
1344 * @edev: the extcon device
1345 */
1346const char *extcon_get_edev_name(struct extcon_dev *edev)
1347{
1348 return !edev ? NULL : edev->name;
1349}
1350
MyungJoo Hamde55d872012-04-20 14:16:22 +09001351static int __init extcon_class_init(void)
1352{
1353 return create_extcon_class();
1354}
1355module_init(extcon_class_init);
1356
1357static void __exit extcon_class_exit(void)
1358{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001359#if defined(CONFIG_ANDROID)
1360 class_compat_unregister(switch_class);
1361#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001362 class_destroy(extcon_class);
1363}
1364module_exit(extcon_class_exit);
1365
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001366MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001367MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1368MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1369MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1370MODULE_DESCRIPTION("External connector (extcon) class driver");
1371MODULE_LICENSE("GPL");