blob: a0a1eea1872798f7acfcfa94d043f400dcf11c91 [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 Figaf841afb2014-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 Choi505cf012016-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 Choi505cf012016-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 Choi505cf012016-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 Choi505cf012016-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 Choi505cf012016-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 },
Chris Zhong2f251402016-07-22 01:13:02 +0900160 [EXTCON_DISP_DP] = {
161 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
162 .id = EXTCON_DISP_DP,
163 .name = "DP",
164 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900165
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900166 /* Miscellaneous external connector */
Chanwoo Choi505cf012016-07-11 16:34:52 +0900167 [EXTCON_DOCK] = {
168 .type = EXTCON_TYPE_MISC,
169 .id = EXTCON_DOCK,
170 .name = "DOCK",
171 },
172 [EXTCON_JIG] = {
173 .type = EXTCON_TYPE_MISC,
174 .id = EXTCON_JIG,
175 .name = "JIG",
176 },
177 [EXTCON_MECHANICAL] = {
178 .type = EXTCON_TYPE_MISC,
179 .id = EXTCON_MECHANICAL,
180 .name = "MECHANICAL",
181 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900182
Chanwoo Choi505cf012016-07-11 16:34:52 +0900183 { /* sentinel */ }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900184};
185
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900186/**
187 * struct extcon_cable - An internal data for each cable of extcon device.
188 * @edev: The extcon device
189 * @cable_index: Index of this cable in the edev
190 * @attr_g: Attribute group for the cable
191 * @attr_name: "name" sysfs entry
192 * @attr_state: "state" sysfs entry
193 * @attrs: Array pointing to attr_name and attr_state for attr_g
194 */
195struct extcon_cable {
196 struct extcon_dev *edev;
197 int cable_index;
198
199 struct attribute_group attr_g;
200 struct device_attribute attr_name;
201 struct device_attribute attr_state;
202
203 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
Chanwoo Choi067c1652016-07-11 19:30:43 +0900204
205 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
206 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
207 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
208 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
Chanwoo Choiceaa98f2016-07-25 21:15:19 +0900209
210 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
211 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
212 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
213 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900214};
215
Mark Brownbe3a07f2012-06-05 16:14:38 +0100216static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900217#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900218static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900219#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900220
Donggeun Kim74c5d092012-04-20 14:16:24 +0900221static LIST_HEAD(extcon_dev_list);
222static DEFINE_MUTEX(extcon_dev_list_lock);
223
MyungJoo Hambde68e62012-04-20 14:16:26 +0900224/**
225 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900226 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +0900227 * @edev: the extcon device
228 * @new_state: new cable attach status for @edev
229 *
230 * Returns 0 if nothing violates. Returns the index + 1 for the first
231 * violated condition.
232 */
233static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
234{
235 int i = 0;
236
237 if (!edev->mutually_exclusive)
238 return 0;
239
240 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530241 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900242 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900243
anish kumar28c0ada2012-08-30 00:35:10 +0530244 /* calculate the total number of bits set */
245 weight = hweight32(correspondants);
246 if (weight > 1)
247 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900248 }
249
250 return 0;
251}
252
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900253static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900254{
255 int i;
256
257 /* Find the the index of extcon cable in edev->supported_cable */
258 for (i = 0; i < edev->max_supported; i++) {
259 if (edev->supported_cable[i] == id)
260 return i;
261 }
262
263 return -EINVAL;
264}
265
Chanwoo Choi067c1652016-07-11 19:30:43 +0900266static int get_extcon_type(unsigned int prop)
267{
268 switch (prop) {
269 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
270 return EXTCON_TYPE_USB;
271 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
272 return EXTCON_TYPE_CHG;
273 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
274 return EXTCON_TYPE_JACK;
275 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
276 return EXTCON_TYPE_DISP;
277 default:
278 return -EINVAL;
279 }
280}
281
282static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
283{
284 return !!(edev->state & BIT(index));
285}
286
Chanwoo Choia5809822016-07-22 13:16:34 +0900287static bool is_extcon_changed(struct extcon_dev *edev, int index,
288 bool new_state)
Chanwoo Choi046050f2015-05-19 20:01:12 +0900289{
Chanwoo Choia5809822016-07-22 13:16:34 +0900290 int state = !!(edev->state & BIT(index));
291 return (state != new_state);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900292}
293
Chanwoo Choi067c1652016-07-11 19:30:43 +0900294static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
295{
296 int type;
297
298 /* Check whether the property is supported or not. */
299 type = get_extcon_type(prop);
300 if (type < 0)
301 return false;
302
303 /* Check whether a specific extcon id supports the property or not. */
304 return !!(extcon_info[id].type & type);
305}
306
Chanwoo Choiceaa98f2016-07-25 21:15:19 +0900307static int is_extcon_property_capability(struct extcon_dev *edev,
308 unsigned int id, int index,unsigned int prop)
309{
310 struct extcon_cable *cable;
311 int type, ret;
312
313 /* Check whether the property is supported or not. */
314 type = get_extcon_type(prop);
315 if (type < 0)
316 return type;
317
318 cable = &edev->cables[index];
319
320 switch (type) {
321 case EXTCON_TYPE_USB:
322 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
323 break;
324 case EXTCON_TYPE_CHG:
325 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
326 break;
327 case EXTCON_TYPE_JACK:
328 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
329 break;
330 case EXTCON_TYPE_DISP:
331 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
332 break;
333 default:
334 ret = -EINVAL;
335 }
336
337 return ret;
338}
339
Chanwoo Choi067c1652016-07-11 19:30:43 +0900340static void init_property(struct extcon_dev *edev, unsigned int id, int index)
341{
342 unsigned int type = extcon_info[id].type;
343 struct extcon_cable *cable = &edev->cables[index];
344
345 if (EXTCON_TYPE_USB & type)
346 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
347 if (EXTCON_TYPE_CHG & type)
348 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
349 if (EXTCON_TYPE_JACK & type)
350 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
351 if (EXTCON_TYPE_DISP & type)
352 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
353}
354
MyungJoo Hamde55d872012-04-20 14:16:22 +0900355static ssize_t state_show(struct device *dev, struct device_attribute *attr,
356 char *buf)
357{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900358 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900359 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900360
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900361 if (edev->max_supported == 0)
362 return sprintf(buf, "%u\n", edev->state);
363
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900364 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900365 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi505cf012016-07-11 16:34:52 +0900366 extcon_info[edev->supported_cable[i]].name,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900367 !!(edev->state & (1 << i)));
368 }
369
370 return count;
371}
Chanwoo Choi0143f592016-07-18 15:39:28 +0900372static DEVICE_ATTR_RO(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900373
374static ssize_t name_show(struct device *dev, struct device_attribute *attr,
375 char *buf)
376{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900377 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900378
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900379 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900380}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700381static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900382
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900383static ssize_t cable_name_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
385{
386 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
387 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900388 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900389
390 return sprintf(buf, "%s\n",
Chanwoo Choi505cf012016-07-11 16:34:52 +0900391 extcon_info[cable->edev->supported_cable[i]].name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900392}
393
394static ssize_t cable_state_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
396{
397 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
398 attr_state);
399
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300400 int i = cable->cable_index;
401
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900402 return sprintf(buf, "%d\n",
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900403 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900404}
405
MyungJoo Hamde55d872012-04-20 14:16:22 +0900406/**
Chanwoo Choia5809822016-07-22 13:16:34 +0900407 * extcon_sync() - Synchronize the states for both the attached/detached
408 * @edev: the extcon device that has the cable.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900409 *
Chanwoo Choia5809822016-07-22 13:16:34 +0900410 * This function send a notification to synchronize the all states of a
411 * specific external connector
MyungJoo Hamde55d872012-04-20 14:16:22 +0900412 */
Chanwoo Choia5809822016-07-22 13:16:34 +0900413int extcon_sync(struct extcon_dev *edev, unsigned int id)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900414{
415 char name_buf[120];
416 char state_buf[120];
417 char *prop_buf;
418 char *envp[3];
419 int env_offset = 0;
420 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900421 int index;
Chanwoo Choia5809822016-07-22 13:16:34 +0900422 int state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900423 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900424
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900425 if (!edev)
426 return -EINVAL;
427
Chanwoo Choia5809822016-07-22 13:16:34 +0900428 index = find_cable_index_by_id(edev, id);
429 if (index < 0)
430 return index;
431
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900432 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900433
Chanwoo Choia5809822016-07-22 13:16:34 +0900434 state = !!(edev->state & BIT(index));
435 raw_notifier_call_chain(&edev->nh[index], state, edev);
Roger Quadrosf7a89812015-07-06 17:46:58 +0300436
Chanwoo Choia5809822016-07-22 13:16:34 +0900437 /* This could be in interrupt handler */
438 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
439 if (!prop_buf) {
440 /* Unlock early before uevent */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900441 spin_unlock_irqrestore(&edev->lock, flags);
Chanwoo Choia5809822016-07-22 13:16:34 +0900442
443 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
444 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
445
446 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900447 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900448
Chanwoo Choia5809822016-07-22 13:16:34 +0900449 length = name_show(&edev->dev, NULL, prop_buf);
450 if (length > 0) {
451 if (prop_buf[length - 1] == '\n')
452 prop_buf[length - 1] = 0;
453 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
454 envp[env_offset++] = name_buf;
455 }
456
457 length = state_show(&edev->dev, NULL, prop_buf);
458 if (length > 0) {
459 if (prop_buf[length - 1] == '\n')
460 prop_buf[length - 1] = 0;
461 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
462 envp[env_offset++] = state_buf;
463 }
464 envp[env_offset] = NULL;
465
466 /* Unlock early before uevent */
467 spin_unlock_irqrestore(&edev->lock, flags);
468 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
469 free_page((unsigned long)prop_buf);
470
MyungJoo Hambde68e62012-04-20 14:16:26 +0900471 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900472}
Chanwoo Choia5809822016-07-22 13:16:34 +0900473EXPORT_SYMBOL_GPL(extcon_sync);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900474
Donggeun Kim74c5d092012-04-20 14:16:24 +0900475/**
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900476 * extcon_get_state() - Get the state of a external connector.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900477 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900478 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900479 */
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900480int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900481{
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900482 int index, state;
483 unsigned long flags;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900484
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900485 if (!edev)
486 return -EINVAL;
487
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900488 index = find_cable_index_by_id(edev, id);
489 if (index < 0)
490 return index;
491
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900492 spin_lock_irqsave(&edev->lock, flags);
493 state = is_extcon_attached(edev, index);
494 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900495
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900496 return state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900497}
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900498EXPORT_SYMBOL_GPL(extcon_get_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900499
500/**
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900501 * extcon_set_state() - Set the state of a external connector.
Chanwoo Choia5809822016-07-22 13:16:34 +0900502 * without a notification.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900503 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900504 * @id: the unique id of each external connector
505 * in extcon enumeration.
506 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900507 * true: attached / false: detached.
Chanwoo Choia5809822016-07-22 13:16:34 +0900508 *
509 * This function only set the state of a external connector without
510 * a notification. To synchronize the data of a external connector,
511 * use extcon_set_state_sync() and extcon_sync().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900512 */
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900513int extcon_set_state(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900514 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900515{
Chanwoo Choia5809822016-07-22 13:16:34 +0900516 unsigned long flags;
517 int index, ret = 0;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900518
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900519 if (!edev)
520 return -EINVAL;
521
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900522 index = find_cable_index_by_id(edev, id);
523 if (index < 0)
524 return index;
525
Chanwoo Choia5809822016-07-22 13:16:34 +0900526 spin_lock_irqsave(&edev->lock, flags);
527
528 /* Check whether the external connector's state is changed. */
529 if (!is_extcon_changed(edev, index, cable_state))
530 goto out;
531
532 if (check_mutually_exclusive(edev,
533 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
534 ret = -EPERM;
535 goto out;
536 }
537
Chanwoo Choi067c1652016-07-11 19:30:43 +0900538 /*
539 * Initialize the value of extcon property before setting
540 * the detached state for an external connector.
541 */
542 if (!cable_state)
543 init_property(edev, id, index);
544
Chanwoo Choia5809822016-07-22 13:16:34 +0900545 /* Update the state for a external connector. */
546 if (cable_state)
547 edev->state |= BIT(index);
548 else
549 edev->state &= ~(BIT(index));
550out:
551 spin_unlock_irqrestore(&edev->lock, flags);
552
553 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900554}
Chanwoo Choi35872fd2016-07-22 13:03:17 +0900555EXPORT_SYMBOL_GPL(extcon_set_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900556
557/**
Chanwoo Choia5809822016-07-22 13:16:34 +0900558 * extcon_set_state_sync() - Set the state of a external connector
559 * with a notification.
560 * @edev: the extcon device that has the cable.
561 * @id: the unique id of each external connector
562 * in extcon enumeration.
563 * @state: the new cable status. The default semantics is
564 * true: attached / false: detached.
565 *
566 * This function set the state of external connector and synchronize the data
567 * by usning a notification.
568 */
569int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
570 bool cable_state)
571{
572 int ret, index;
573 unsigned long flags;
574
575 index = find_cable_index_by_id(edev, id);
576 if (index < 0)
577 return index;
578
579 /* Check whether the external connector's state is changed. */
580 spin_lock_irqsave(&edev->lock, flags);
581 ret = is_extcon_changed(edev, index, cable_state);
582 spin_unlock_irqrestore(&edev->lock, flags);
583 if (!ret)
584 return 0;
585
586 ret = extcon_set_state(edev, id, cable_state);
587 if (ret < 0)
588 return ret;
589
590 return extcon_sync(edev, id);
591}
592EXPORT_SYMBOL_GPL(extcon_set_state_sync);
593
594/**
Chanwoo Choi067c1652016-07-11 19:30:43 +0900595 * extcon_get_property() - Get the property value of a specific cable.
596 * @edev: the extcon device that has the cable.
597 * @id: the unique id of each external connector
598 * in extcon enumeration.
599 * @prop: the property id among enum extcon_property.
600 * @prop_val: the pointer which store the value of property.
601 *
602 * When getting the property value of external connector, the external connector
603 * should be attached. If detached state, function just return 0 without
604 * property value. Also, the each property should be included in the list of
605 * supported properties according to the type of external connectors.
606 *
607 * Returns 0 if success or error number if fail
608 */
609int extcon_get_property(struct extcon_dev *edev, unsigned int id,
610 unsigned int prop,
611 union extcon_property_value *prop_val)
612{
613 struct extcon_cable *cable;
614 unsigned long flags;
615 int index, ret = 0;
616
617 *prop_val = (union extcon_property_value)(0);
618
619 if (!edev)
620 return -EINVAL;
621
622 /* Check whether the property is supported or not */
623 if (!is_extcon_property_supported(id, prop))
624 return -EINVAL;
625
626 /* Find the cable index of external connector by using id */
627 index = find_cable_index_by_id(edev, id);
628 if (index < 0)
629 return index;
630
631 spin_lock_irqsave(&edev->lock, flags);
632
Chanwoo Choiceaa98f2016-07-25 21:15:19 +0900633 /* Check whether the property is available or not. */
634 if (!is_extcon_property_capability(edev, id, index, prop)) {
635 spin_unlock_irqrestore(&edev->lock, flags);
636 return -EPERM;
637 }
638
Chanwoo Choi067c1652016-07-11 19:30:43 +0900639 /*
640 * Check whether the external connector is attached.
641 * If external connector is detached, the user can not
642 * get the property value.
643 */
644 if (!is_extcon_attached(edev, index)) {
645 spin_unlock_irqrestore(&edev->lock, flags);
646 return 0;
647 }
648
649 cable = &edev->cables[index];
650
651 /* Get the property value according to extcon type */
652 switch (prop) {
653 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
654 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
655 break;
656 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
657 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
658 break;
659 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
660 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
661 break;
662 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
663 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
664 break;
665 default:
666 ret = -EINVAL;
667 break;
668 }
669
670 spin_unlock_irqrestore(&edev->lock, flags);
671
672 return ret;
673}
674EXPORT_SYMBOL_GPL(extcon_get_property);
675
676/**
677 * extcon_set_property() - Set the property value of a specific cable.
678 * @edev: the extcon device that has the cable.
679 * @id: the unique id of each external connector
680 * in extcon enumeration.
681 * @prop: the property id among enum extcon_property.
682 * @prop_val: the pointer including the new value of property.
683 *
684 * The each property should be included in the list of supported properties
685 * according to the type of external connectors.
686 *
687 * Returns 0 if success or error number if fail
688 */
689int extcon_set_property(struct extcon_dev *edev, unsigned int id,
690 unsigned int prop,
691 union extcon_property_value prop_val)
692{
693 struct extcon_cable *cable;
694 unsigned long flags;
695 int index, ret = 0;
696
697 if (!edev)
698 return -EINVAL;
699
700 /* Check whether the property is supported or not */
701 if (!is_extcon_property_supported(id, prop))
702 return -EINVAL;
703
704 /* Find the cable index of external connector by using id */
705 index = find_cable_index_by_id(edev, id);
706 if (index < 0)
707 return index;
708
709 spin_lock_irqsave(&edev->lock, flags);
710
Chanwoo Choiceaa98f2016-07-25 21:15:19 +0900711 /* Check whether the property is available or not. */
712 if (!is_extcon_property_capability(edev, id, index, prop)) {
713 spin_unlock_irqrestore(&edev->lock, flags);
714 return -EPERM;
715 }
716
Chanwoo Choi067c1652016-07-11 19:30:43 +0900717 cable = &edev->cables[index];
718
719 /* Set the property value according to extcon type */
720 switch (prop) {
721 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
722 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
723 break;
724 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
725 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
726 break;
727 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
728 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
729 break;
730 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
731 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
732 break;
733 default:
734 ret = -EINVAL;
735 break;
736 }
737
738 spin_unlock_irqrestore(&edev->lock, flags);
739
740 return ret;
741}
742EXPORT_SYMBOL_GPL(extcon_set_property);
743
744/**
Chanwoo Choia5809822016-07-22 13:16:34 +0900745 * extcon_set_property_sync() - Set the property value of a specific cable
746 with a notification.
747 * @prop_val: the pointer including the new value of property.
748 *
749 * When setting the property value of external connector, the external connector
750 * should be attached. The each property should be included in the list of
751 * supported properties according to the type of external connectors.
752 *
753 * Returns 0 if success or error number if fail
754 */
755int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
756 unsigned int prop,
757 union extcon_property_value prop_val)
758{
759 int ret;
760
761 ret = extcon_set_property(edev, id, prop, prop_val);
762 if (ret < 0)
763 return ret;
764
765 return extcon_sync(edev, id);
766}
767EXPORT_SYMBOL_GPL(extcon_set_property_sync);
768
769/**
Chanwoo Choiceaa98f2016-07-25 21:15:19 +0900770 * extcon_get_property_capability() - Get the capability of property
771 * of an external connector.
772 * @edev: the extcon device that has the cable.
773 * @id: the unique id of each external connector
774 * in extcon enumeration.
775 * @prop: the property id among enum extcon_property.
776 *
777 * Returns 1 if the property is available or 0 if not available.
778 */
779int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
780 unsigned int prop)
781{
782 int index;
783
784 if (!edev)
785 return -EINVAL;
786
787 /* Check whether the property is supported or not */
788 if (!is_extcon_property_supported(id, prop))
789 return -EINVAL;
790
791 /* Find the cable index of external connector by using id */
792 index = find_cable_index_by_id(edev, id);
793 if (index < 0)
794 return index;
795
796 return is_extcon_property_capability(edev, id, index, prop);
797}
798EXPORT_SYMBOL_GPL(extcon_get_property_capability);
799
800/**
801 * extcon_set_property_capability() - Set the capability of a property
802 * of an external connector.
803 * @edev: the extcon device that has the cable.
804 * @id: the unique id of each external connector
805 * in extcon enumeration.
806 * @prop: the property id among enum extcon_property.
807 *
808 * This function set the capability of a property for an external connector
809 * to mark the bit in capability bitmap which mean the available state of
810 * a property.
811 *
812 * Returns 0 if success or error number if fail
813 */
814int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
815 unsigned int prop)
816{
817 struct extcon_cable *cable;
818 int index, type, ret = 0;
819
820 if (!edev)
821 return -EINVAL;
822
823 /* Check whether the property is supported or not. */
824 if (!is_extcon_property_supported(id, prop))
825 return -EINVAL;
826
827 /* Find the cable index of external connector by using id. */
828 index = find_cable_index_by_id(edev, id);
829 if (index < 0)
830 return index;
831
832 type = get_extcon_type(prop);
833 if (type < 0)
834 return type;
835
836 cable = &edev->cables[index];
837
838 switch (type) {
839 case EXTCON_TYPE_USB:
840 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
841 break;
842 case EXTCON_TYPE_CHG:
843 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
844 break;
845 case EXTCON_TYPE_JACK:
846 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
847 break;
848 case EXTCON_TYPE_DISP:
849 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
850 break;
851 default:
852 ret = -EINVAL;
853 }
854
855 return ret;
856}
857EXPORT_SYMBOL_GPL(extcon_set_property_capability);
858
859/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900860 * extcon_get_extcon_dev() - Get the extcon device instance from the name
861 * @extcon_name: The extcon name provided with extcon_dev_register()
862 */
863struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
864{
865 struct extcon_dev *sd;
866
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900867 if (!extcon_name)
868 return ERR_PTR(-EINVAL);
869
Donggeun Kim74c5d092012-04-20 14:16:24 +0900870 mutex_lock(&extcon_dev_list_lock);
871 list_for_each_entry(sd, &extcon_dev_list, entry) {
872 if (!strcmp(sd->name, extcon_name))
873 goto out;
874 }
875 sd = NULL;
876out:
877 mutex_unlock(&extcon_dev_list_lock);
878 return sd;
879}
880EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
881
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900882/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900883 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900884 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900885 * @edev: the extcon device that has the external connecotr.
886 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900887 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900888 *
889 * Note that the second parameter given to the callback of nb (val) is
890 * "old_state", not the current state. The current state can be retrieved
891 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900892 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900893int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900894 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900895{
Hans de Goede66bee352015-03-21 17:26:24 +0100896 unsigned long flags;
Maninder Singh6e25baa2016-08-01 14:51:30 +0530897 int ret, idx = -EINVAL;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900898
Chanwoo Choi830ae442016-05-31 17:32:30 +0900899 if (!nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900900 return -EINVAL;
901
Chanwoo Choi830ae442016-05-31 17:32:30 +0900902 if (edev) {
903 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900904 if (idx < 0)
905 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100906
Chanwoo Choi830ae442016-05-31 17:32:30 +0900907 spin_lock_irqsave(&edev->lock, flags);
908 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
909 spin_unlock_irqrestore(&edev->lock, flags);
910 } else {
911 struct extcon_dev *extd;
912
913 mutex_lock(&extcon_dev_list_lock);
914 list_for_each_entry(extd, &extcon_dev_list, entry) {
915 idx = find_cable_index_by_id(extd, id);
916 if (idx >= 0)
917 break;
918 }
919 mutex_unlock(&extcon_dev_list_lock);
920
921 if (idx >= 0) {
922 edev = extd;
923 return extcon_register_notifier(extd, id, nb);
924 } else {
925 ret = -ENODEV;
926 }
927 }
Hans de Goede66bee352015-03-21 17:26:24 +0100928
929 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900930}
931EXPORT_SYMBOL_GPL(extcon_register_notifier);
932
933/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900934 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900935 * @edev: the extcon device that has the external connecotr.
936 * @id: the unique id of each external connector in extcon enumeration.
937 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900938 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900939int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900940 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900941{
Hans de Goede66bee352015-03-21 17:26:24 +0100942 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900943 int ret, idx;
944
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900945 if (!edev || !nb)
946 return -EINVAL;
947
Chanwoo Choi046050f2015-05-19 20:01:12 +0900948 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900949 if (idx < 0)
950 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100951
952 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900953 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100954 spin_unlock_irqrestore(&edev->lock, flags);
955
956 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900957}
958EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
959
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700960static struct attribute *extcon_attrs[] = {
961 &dev_attr_state.attr,
962 &dev_attr_name.attr,
963 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900964};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700965ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900966
967static int create_extcon_class(void)
968{
969 if (!extcon_class) {
970 extcon_class = class_create(THIS_MODULE, "extcon");
971 if (IS_ERR(extcon_class))
972 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700973 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900974
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900975#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900976 switch_class = class_compat_register("switch");
977 if (WARN(!switch_class, "cannot allocate"))
978 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900979#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900980 }
981
982 return 0;
983}
984
MyungJoo Hamde55d872012-04-20 14:16:22 +0900985static void extcon_dev_release(struct device *dev)
986{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900987}
988
MyungJoo Hambde68e62012-04-20 14:16:26 +0900989static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900990static void dummy_sysfs_dev_release(struct device *dev)
991{
992}
993
Chanwoo Choia9af6522014-04-24 19:46:49 +0900994/*
995 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900996 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900997 * If supported_cable is NULL, cable name related APIs
998 * are disabled.
999 *
1000 * This function allocates the memory for extcon device without allocating
1001 * memory in each extcon provider driver and initialize default setting for
1002 * extcon device.
1003 *
1004 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
1005 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +09001006struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +09001007{
1008 struct extcon_dev *edev;
1009
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001010 if (!supported_cable)
1011 return ERR_PTR(-EINVAL);
1012
Chanwoo Choia9af6522014-04-24 19:46:49 +09001013 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1014 if (!edev)
1015 return ERR_PTR(-ENOMEM);
1016
1017 edev->max_supported = 0;
1018 edev->supported_cable = supported_cable;
1019
1020 return edev;
1021}
1022
1023/*
1024 * extcon_dev_free() - Free the memory of extcon device.
1025 * @edev: the extcon device to free
1026 */
1027void extcon_dev_free(struct extcon_dev *edev)
1028{
1029 kfree(edev);
1030}
1031EXPORT_SYMBOL_GPL(extcon_dev_free);
1032
MyungJoo Hamde55d872012-04-20 14:16:22 +09001033/**
1034 * extcon_dev_register() - Register a new extcon device
1035 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001036 *
1037 * Among the members of edev struct, please set the "user initializing data"
1038 * in any case and set the "optional callbacks" if required. However, please
1039 * do not set the values of "internal data", which are initialized by
1040 * this function.
1041 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001042int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001043{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001044 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001045 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001046
1047 if (!extcon_class) {
1048 ret = create_extcon_class();
1049 if (ret < 0)
1050 return ret;
1051 }
1052
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001053 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001054 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001055
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001056 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1057
1058 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001059 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001060 dev_err(&edev->dev,
1061 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001062 return -EINVAL;
1063 }
1064
Chanwoo Choidae61652013-09-27 09:19:40 +09001065 edev->dev.class = extcon_class;
1066 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001067
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001068 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001069 if (IS_ERR_OR_NULL(edev->name)) {
1070 dev_err(&edev->dev,
1071 "extcon device name is null\n");
1072 return -EINVAL;
1073 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001074 dev_set_name(&edev->dev, "extcon%lu",
1075 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001076
1077 if (edev->max_supported) {
1078 char buf[10];
1079 char *str;
1080 struct extcon_cable *cable;
1081
1082 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1083 edev->max_supported, GFP_KERNEL);
1084 if (!edev->cables) {
1085 ret = -ENOMEM;
1086 goto err_sysfs_alloc;
1087 }
1088 for (index = 0; index < edev->max_supported; index++) {
1089 cable = &edev->cables[index];
1090
1091 snprintf(buf, 10, "cable.%d", index);
1092 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1093 GFP_KERNEL);
1094 if (!str) {
1095 for (index--; index >= 0; index--) {
1096 cable = &edev->cables[index];
1097 kfree(cable->attr_g.name);
1098 }
1099 ret = -ENOMEM;
1100
1101 goto err_alloc_cables;
1102 }
1103 strcpy(str, buf);
1104
1105 cable->edev = edev;
1106 cable->cable_index = index;
1107 cable->attrs[0] = &cable->attr_name.attr;
1108 cable->attrs[1] = &cable->attr_state.attr;
1109 cable->attrs[2] = NULL;
1110 cable->attr_g.name = str;
1111 cable->attr_g.attrs = cable->attrs;
1112
Mark Brown9baf3222012-08-16 20:03:21 +01001113 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001114 cable->attr_name.attr.name = "name";
1115 cable->attr_name.attr.mode = 0444;
1116 cable->attr_name.show = cable_name_show;
1117
Mark Brown9baf3222012-08-16 20:03:21 +01001118 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001119 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +09001120 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001121 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001122 }
1123 }
1124
MyungJoo Hambde68e62012-04-20 14:16:26 +09001125 if (edev->max_supported && edev->mutually_exclusive) {
1126 char buf[80];
1127 char *name;
1128
1129 /* Count the size of mutually_exclusive array */
1130 for (index = 0; edev->mutually_exclusive[index]; index++)
1131 ;
1132
1133 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1134 (index + 1), GFP_KERNEL);
1135 if (!edev->attrs_muex) {
1136 ret = -ENOMEM;
1137 goto err_muex;
1138 }
1139
1140 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1141 index, GFP_KERNEL);
1142 if (!edev->d_attrs_muex) {
1143 ret = -ENOMEM;
1144 kfree(edev->attrs_muex);
1145 goto err_muex;
1146 }
1147
1148 for (index = 0; edev->mutually_exclusive[index]; index++) {
1149 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1150 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1151 GFP_KERNEL);
1152 if (!name) {
1153 for (index--; index >= 0; index--) {
1154 kfree(edev->d_attrs_muex[index].attr.
1155 name);
1156 }
1157 kfree(edev->d_attrs_muex);
1158 kfree(edev->attrs_muex);
1159 ret = -ENOMEM;
1160 goto err_muex;
1161 }
1162 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +01001163 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001164 edev->d_attrs_muex[index].attr.name = name;
1165 edev->d_attrs_muex[index].attr.mode = 0000;
1166 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1167 .attr;
1168 }
1169 edev->attr_g_muex.name = muex_name;
1170 edev->attr_g_muex.attrs = edev->attrs_muex;
1171
1172 }
1173
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001174 if (edev->max_supported) {
1175 edev->extcon_dev_type.groups =
1176 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +09001177 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001178 if (!edev->extcon_dev_type.groups) {
1179 ret = -ENOMEM;
1180 goto err_alloc_groups;
1181 }
1182
Chanwoo Choidae61652013-09-27 09:19:40 +09001183 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001184 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1185
1186 for (index = 0; index < edev->max_supported; index++)
1187 edev->extcon_dev_type.groups[index] =
1188 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +09001189 if (edev->mutually_exclusive)
1190 edev->extcon_dev_type.groups[index] =
1191 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001192
Chanwoo Choidae61652013-09-27 09:19:40 +09001193 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001194 }
1195
Chanwoo Choidae61652013-09-27 09:19:40 +09001196 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001197 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +09001198 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001199 goto err_dev;
1200 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001201#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001202 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001203 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001204#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +09001205
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001206 spin_lock_init(&edev->lock);
1207
Chanwoo Choi046050f2015-05-19 20:01:12 +09001208 edev->nh = devm_kzalloc(&edev->dev,
1209 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1210 if (!edev->nh) {
1211 ret = -ENOMEM;
1212 goto err_dev;
1213 }
1214
1215 for (index = 0; index < edev->max_supported; index++)
1216 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +09001217
Chanwoo Choidae61652013-09-27 09:19:40 +09001218 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001219 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +09001220
1221 mutex_lock(&extcon_dev_list_lock);
1222 list_add(&edev->entry, &extcon_dev_list);
1223 mutex_unlock(&extcon_dev_list_lock);
1224
MyungJoo Hamde55d872012-04-20 14:16:22 +09001225 return 0;
1226
1227err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001228 if (edev->max_supported)
1229 kfree(edev->extcon_dev_type.groups);
1230err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +09001231 if (edev->max_supported && edev->mutually_exclusive) {
1232 for (index = 0; edev->mutually_exclusive[index]; index++)
1233 kfree(edev->d_attrs_muex[index].attr.name);
1234 kfree(edev->d_attrs_muex);
1235 kfree(edev->attrs_muex);
1236 }
1237err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001238 for (index = 0; index < edev->max_supported; index++)
1239 kfree(edev->cables[index].attr_g.name);
1240err_alloc_cables:
1241 if (edev->max_supported)
1242 kfree(edev->cables);
1243err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +09001244 return ret;
1245}
1246EXPORT_SYMBOL_GPL(extcon_dev_register);
1247
1248/**
1249 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +09001250 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +09001251 *
1252 * Note that this does not call kfree(edev) because edev was not allocated
1253 * by this class.
1254 */
1255void extcon_dev_unregister(struct extcon_dev *edev)
1256{
anish kumar57e7cd32012-10-22 09:43:33 +09001257 int index;
1258
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001259 if (!edev)
1260 return;
1261
anish kumar57e7cd32012-10-22 09:43:33 +09001262 mutex_lock(&extcon_dev_list_lock);
1263 list_del(&edev->entry);
1264 mutex_unlock(&extcon_dev_list_lock);
1265
Chanwoo Choidae61652013-09-27 09:19:40 +09001266 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1267 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1268 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +09001269 return;
1270 }
1271
Wang, Xiaoming7585ca02013-11-01 18:48:14 -04001272 device_unregister(&edev->dev);
1273
anish kumar57e7cd32012-10-22 09:43:33 +09001274 if (edev->mutually_exclusive && edev->max_supported) {
1275 for (index = 0; edev->mutually_exclusive[index];
1276 index++)
1277 kfree(edev->d_attrs_muex[index].attr.name);
1278 kfree(edev->d_attrs_muex);
1279 kfree(edev->attrs_muex);
1280 }
1281
1282 for (index = 0; index < edev->max_supported; index++)
1283 kfree(edev->cables[index].attr_g.name);
1284
1285 if (edev->max_supported) {
1286 kfree(edev->extcon_dev_type.groups);
1287 kfree(edev->cables);
1288 }
1289
1290#if defined(CONFIG_ANDROID)
1291 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001292 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +09001293#endif
Chanwoo Choidae61652013-09-27 09:19:40 +09001294 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001295}
1296EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1297
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001298#ifdef CONFIG_OF
1299/*
1300 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1301 * @dev - instance to the given device
1302 * @index - index into list of extcon_dev
1303 *
1304 * return the instance of extcon device
1305 */
1306struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1307{
1308 struct device_node *node;
1309 struct extcon_dev *edev;
1310
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001311 if (!dev)
1312 return ERR_PTR(-EINVAL);
1313
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001314 if (!dev->of_node) {
Stephen Boyd97536d12016-07-05 11:57:05 -07001315 dev_dbg(dev, "device does not have a device node entry\n");
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001316 return ERR_PTR(-EINVAL);
1317 }
1318
1319 node = of_parse_phandle(dev->of_node, "extcon", index);
1320 if (!node) {
Stephen Boyd97536d12016-07-05 11:57:05 -07001321 dev_dbg(dev, "failed to get phandle in %s node\n",
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001322 dev->of_node->full_name);
1323 return ERR_PTR(-ENODEV);
1324 }
1325
Tomasz Figaf841afb2014-10-16 15:11:44 +02001326 mutex_lock(&extcon_dev_list_lock);
1327 list_for_each_entry(edev, &extcon_dev_list, entry) {
1328 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1329 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001330 of_node_put(node);
Tomasz Figaf841afb2014-10-16 15:11:44 +02001331 return edev;
1332 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001333 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001334 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001335 of_node_put(node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001336
Tomasz Figaf841afb2014-10-16 15:11:44 +02001337 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001338}
1339#else
1340struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1341{
1342 return ERR_PTR(-ENOSYS);
1343}
1344#endif /* CONFIG_OF */
1345EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1346
Chanwoo Choi707d7552015-04-15 13:57:51 +09001347/**
1348 * extcon_get_edev_name() - Get the name of the extcon device.
1349 * @edev: the extcon device
1350 */
1351const char *extcon_get_edev_name(struct extcon_dev *edev)
1352{
1353 return !edev ? NULL : edev->name;
1354}
1355
MyungJoo Hamde55d872012-04-20 14:16:22 +09001356static int __init extcon_class_init(void)
1357{
1358 return create_extcon_class();
1359}
1360module_init(extcon_class_init);
1361
1362static void __exit extcon_class_exit(void)
1363{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001364#if defined(CONFIG_ANDROID)
1365 class_compat_unregister(switch_class);
1366#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001367 class_destroy(extcon_class);
1368}
1369module_exit(extcon_class_exit);
1370
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001371MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001372MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1373MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1374MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1375MODULE_DESCRIPTION("External connector (extcon) class driver");
1376MODULE_LICENSE("GPL");