blob: 591582b0d2b7e95d058ac4cc4cd91a6f19ffba38 [file] [log] [blame]
MyungJoo Hamde55d872012-04-20 14:16:22 +09001/*
Chanwoo Choib9ec23c2015-04-24 14:48:52 +09002 * drivers/extcon/extcon.c - External Connector (extcon) framework.
MyungJoo Hamde55d872012-04-20 14:16:22 +09003 *
4 * External connector (extcon) class driver
5 *
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09006 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 *
MyungJoo Hamde55d872012-04-20 14:16:22 +09009 * Copyright (C) 2012 Samsung Electronics
10 * Author: Donggeun Kim <dg77.kim@samsung.com>
11 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
12 *
13 * based on android/drivers/switch/switch_class.c
14 * Copyright (C) 2008 Google, Inc.
15 * Author: Mike Lockwood <lockwood@android.com>
16 *
17 * This software is licensed under the terms of the GNU General Public
18 * License version 2, as published by the Free Software Foundation, and
19 * may be copied, distributed, and modified under those terms.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
Chanwoo Choib9ec23c2015-04-24 14:48:52 +090025 */
MyungJoo Hamde55d872012-04-20 14:16:22 +090026
27#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/init.h>
30#include <linux/device.h>
31#include <linux/fs.h>
32#include <linux/err.h>
Tomasz Figaf841afb2014-10-16 15:11:44 +020033#include <linux/of.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090034#include <linux/slab.h>
Mark Brown9baf3222012-08-16 20:03:21 +010035#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090036
Chanwoo Choie6cf0462016-12-26 20:37:38 +090037#include "extcon.h"
38
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090039#define SUPPORTED_CABLE_MAX 32
40#define CABLE_NAME_MAX 30
41
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090042struct __extcon_info {
43 unsigned int type;
44 unsigned int id;
45 const char *name;
46
47} extcon_info[] = {
48 [EXTCON_NONE] = {
49 .type = EXTCON_TYPE_MISC,
50 .id = EXTCON_NONE,
51 .name = "NONE",
52 },
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090053
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090054 /* USB external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090055 [EXTCON_USB] = {
56 .type = EXTCON_TYPE_USB,
57 .id = EXTCON_USB,
58 .name = "USB",
59 },
60 [EXTCON_USB_HOST] = {
61 .type = EXTCON_TYPE_USB,
62 .id = EXTCON_USB_HOST,
63 .name = "USB_HOST",
64 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090065
Chanwoo Choi11eecf92015-10-03 14:15:13 +090066 /* Charging external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090067 [EXTCON_CHG_USB_SDP] = {
68 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
69 .id = EXTCON_CHG_USB_SDP,
70 .name = "SDP",
71 },
72 [EXTCON_CHG_USB_DCP] = {
73 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
74 .id = EXTCON_CHG_USB_DCP,
75 .name = "DCP",
76 },
77 [EXTCON_CHG_USB_CDP] = {
78 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
79 .id = EXTCON_CHG_USB_CDP,
80 .name = "CDP",
81 },
82 [EXTCON_CHG_USB_ACA] = {
83 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
84 .id = EXTCON_CHG_USB_ACA,
85 .name = "ACA",
86 },
87 [EXTCON_CHG_USB_FAST] = {
88 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
89 .id = EXTCON_CHG_USB_FAST,
90 .name = "FAST-CHARGER",
91 },
92 [EXTCON_CHG_USB_SLOW] = {
93 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
94 .id = EXTCON_CHG_USB_SLOW,
95 .name = "SLOW-CHARGER",
96 },
Chanwoo Choi7fe95fb2016-08-05 18:15:46 +090097 [EXTCON_CHG_WPT] = {
98 .type = EXTCON_TYPE_CHG,
99 .id = EXTCON_CHG_WPT,
100 .name = "WPT",
101 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900102
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900103 /* Jack external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900104 [EXTCON_JACK_MICROPHONE] = {
105 .type = EXTCON_TYPE_JACK,
106 .id = EXTCON_JACK_MICROPHONE,
107 .name = "MICROPHONE",
108 },
109 [EXTCON_JACK_HEADPHONE] = {
110 .type = EXTCON_TYPE_JACK,
111 .id = EXTCON_JACK_HEADPHONE,
112 .name = "HEADPHONE",
113 },
114 [EXTCON_JACK_LINE_IN] = {
115 .type = EXTCON_TYPE_JACK,
116 .id = EXTCON_JACK_LINE_IN,
117 .name = "LINE-IN",
118 },
119 [EXTCON_JACK_LINE_OUT] = {
120 .type = EXTCON_TYPE_JACK,
121 .id = EXTCON_JACK_LINE_OUT,
122 .name = "LINE-OUT",
123 },
124 [EXTCON_JACK_VIDEO_IN] = {
125 .type = EXTCON_TYPE_JACK,
126 .id = EXTCON_JACK_VIDEO_IN,
127 .name = "VIDEO-IN",
128 },
129 [EXTCON_JACK_VIDEO_OUT] = {
130 .type = EXTCON_TYPE_JACK,
131 .id = EXTCON_JACK_VIDEO_OUT,
132 .name = "VIDEO-OUT",
133 },
134 [EXTCON_JACK_SPDIF_IN] = {
135 .type = EXTCON_TYPE_JACK,
136 .id = EXTCON_JACK_SPDIF_IN,
137 .name = "SPDIF-IN",
138 },
139 [EXTCON_JACK_SPDIF_OUT] = {
140 .type = EXTCON_TYPE_JACK,
141 .id = EXTCON_JACK_SPDIF_OUT,
142 .name = "SPDIF-OUT",
143 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900144
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900145 /* Display external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900146 [EXTCON_DISP_HDMI] = {
147 .type = EXTCON_TYPE_DISP,
148 .id = EXTCON_DISP_HDMI,
149 .name = "HDMI",
150 },
151 [EXTCON_DISP_MHL] = {
152 .type = EXTCON_TYPE_DISP,
153 .id = EXTCON_DISP_MHL,
154 .name = "MHL",
155 },
156 [EXTCON_DISP_DVI] = {
157 .type = EXTCON_TYPE_DISP,
158 .id = EXTCON_DISP_DVI,
159 .name = "DVI",
160 },
161 [EXTCON_DISP_VGA] = {
162 .type = EXTCON_TYPE_DISP,
163 .id = EXTCON_DISP_VGA,
164 .name = "VGA",
165 },
Chris Zhong21641882016-07-22 01:13:02 +0900166 [EXTCON_DISP_DP] = {
167 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
168 .id = EXTCON_DISP_DP,
169 .name = "DP",
170 },
Chanwoo Choi9c0595d2016-08-05 17:49:23 +0900171 [EXTCON_DISP_HMD] = {
172 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
173 .id = EXTCON_DISP_HMD,
174 .name = "HMD",
175 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900176
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900177 /* Miscellaneous external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900178 [EXTCON_DOCK] = {
179 .type = EXTCON_TYPE_MISC,
180 .id = EXTCON_DOCK,
181 .name = "DOCK",
182 },
183 [EXTCON_JIG] = {
184 .type = EXTCON_TYPE_MISC,
185 .id = EXTCON_JIG,
186 .name = "JIG",
187 },
188 [EXTCON_MECHANICAL] = {
189 .type = EXTCON_TYPE_MISC,
190 .id = EXTCON_MECHANICAL,
191 .name = "MECHANICAL",
192 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900193
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900194 { /* sentinel */ }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900195};
196
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900197/**
198 * struct extcon_cable - An internal data for each cable of extcon device.
199 * @edev: The extcon device
200 * @cable_index: Index of this cable in the edev
201 * @attr_g: Attribute group for the cable
202 * @attr_name: "name" sysfs entry
203 * @attr_state: "state" sysfs entry
204 * @attrs: Array pointing to attr_name and attr_state for attr_g
205 */
206struct extcon_cable {
207 struct extcon_dev *edev;
208 int cable_index;
209
210 struct attribute_group attr_g;
211 struct device_attribute attr_name;
212 struct device_attribute attr_state;
213
214 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900215
216 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
217 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
218 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
219 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900220
221 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
222 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
223 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
224 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900225};
226
Mark Brownbe3a07f2012-06-05 16:14:38 +0100227static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900228#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900229static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900230#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900231
Donggeun Kim74c5d092012-04-20 14:16:24 +0900232static LIST_HEAD(extcon_dev_list);
233static DEFINE_MUTEX(extcon_dev_list_lock);
234
MyungJoo Hambde68e62012-04-20 14:16:26 +0900235/**
236 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900237 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +0900238 * @edev: the extcon device
239 * @new_state: new cable attach status for @edev
240 *
241 * Returns 0 if nothing violates. Returns the index + 1 for the first
242 * violated condition.
243 */
244static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
245{
246 int i = 0;
247
248 if (!edev->mutually_exclusive)
249 return 0;
250
251 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530252 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900253 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900254
anish kumar28c0ada2012-08-30 00:35:10 +0530255 /* calculate the total number of bits set */
256 weight = hweight32(correspondants);
257 if (weight > 1)
258 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900259 }
260
261 return 0;
262}
263
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900264static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900265{
266 int i;
267
268 /* Find the the index of extcon cable in edev->supported_cable */
269 for (i = 0; i < edev->max_supported; i++) {
270 if (edev->supported_cable[i] == id)
271 return i;
272 }
273
274 return -EINVAL;
275}
276
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900277static int get_extcon_type(unsigned int prop)
278{
279 switch (prop) {
280 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
281 return EXTCON_TYPE_USB;
282 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
283 return EXTCON_TYPE_CHG;
284 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
285 return EXTCON_TYPE_JACK;
286 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
287 return EXTCON_TYPE_DISP;
288 default:
289 return -EINVAL;
290 }
291}
292
293static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
294{
295 return !!(edev->state & BIT(index));
296}
297
Chanwoo Choiab11af042016-07-22 13:16:34 +0900298static bool is_extcon_changed(struct extcon_dev *edev, int index,
299 bool new_state)
Chanwoo Choi046050f2015-05-19 20:01:12 +0900300{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900301 int state = !!(edev->state & BIT(index));
302 return (state != new_state);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900303}
304
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900305static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
306{
307 int type;
308
309 /* Check whether the property is supported or not. */
310 type = get_extcon_type(prop);
311 if (type < 0)
312 return false;
313
314 /* Check whether a specific extcon id supports the property or not. */
315 return !!(extcon_info[id].type & type);
316}
317
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900318static int is_extcon_property_capability(struct extcon_dev *edev,
319 unsigned int id, int index,unsigned int prop)
320{
321 struct extcon_cable *cable;
322 int type, ret;
323
324 /* Check whether the property is supported or not. */
325 type = get_extcon_type(prop);
326 if (type < 0)
327 return type;
328
329 cable = &edev->cables[index];
330
331 switch (type) {
332 case EXTCON_TYPE_USB:
333 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
334 break;
335 case EXTCON_TYPE_CHG:
336 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
337 break;
338 case EXTCON_TYPE_JACK:
339 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
340 break;
341 case EXTCON_TYPE_DISP:
342 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
343 break;
344 default:
345 ret = -EINVAL;
346 }
347
348 return ret;
349}
350
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900351static void init_property(struct extcon_dev *edev, unsigned int id, int index)
352{
353 unsigned int type = extcon_info[id].type;
354 struct extcon_cable *cable = &edev->cables[index];
355
356 if (EXTCON_TYPE_USB & type)
357 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
358 if (EXTCON_TYPE_CHG & type)
359 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
360 if (EXTCON_TYPE_JACK & type)
361 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
362 if (EXTCON_TYPE_DISP & type)
363 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
364}
365
MyungJoo Hamde55d872012-04-20 14:16:22 +0900366static ssize_t state_show(struct device *dev, struct device_attribute *attr,
367 char *buf)
368{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900369 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900370 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900371
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900372 if (edev->max_supported == 0)
373 return sprintf(buf, "%u\n", edev->state);
374
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900375 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900376 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900377 extcon_info[edev->supported_cable[i]].name,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900378 !!(edev->state & (1 << i)));
379 }
380
381 return count;
382}
Chanwoo Choi5d5321e2016-07-18 15:39:28 +0900383static DEVICE_ATTR_RO(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900384
385static ssize_t name_show(struct device *dev, struct device_attribute *attr,
386 char *buf)
387{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900388 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900389
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900390 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900391}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700392static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900393
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900394static ssize_t cable_name_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_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900399 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900400
401 return sprintf(buf, "%s\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900402 extcon_info[cable->edev->supported_cable[i]].name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900403}
404
405static ssize_t cable_state_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
409 attr_state);
410
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300411 int i = cable->cable_index;
412
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900413 return sprintf(buf, "%d\n",
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900414 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900415}
416
MyungJoo Hamde55d872012-04-20 14:16:22 +0900417/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900418 * extcon_sync() - Synchronize the states for both the attached/detached
419 * @edev: the extcon device that has the cable.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900420 *
Chanwoo Choiab11af042016-07-22 13:16:34 +0900421 * This function send a notification to synchronize the all states of a
422 * specific external connector
MyungJoo Hamde55d872012-04-20 14:16:22 +0900423 */
Chanwoo Choiab11af042016-07-22 13:16:34 +0900424int extcon_sync(struct extcon_dev *edev, unsigned int id)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900425{
426 char name_buf[120];
427 char state_buf[120];
428 char *prop_buf;
429 char *envp[3];
430 int env_offset = 0;
431 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900432 int index;
Chanwoo Choiab11af042016-07-22 13:16:34 +0900433 int state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900434 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900435
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900436 if (!edev)
437 return -EINVAL;
438
Chanwoo Choiab11af042016-07-22 13:16:34 +0900439 index = find_cable_index_by_id(edev, id);
440 if (index < 0)
441 return index;
442
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900443 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900444
Chanwoo Choiab11af042016-07-22 13:16:34 +0900445 state = !!(edev->state & BIT(index));
446 raw_notifier_call_chain(&edev->nh[index], state, edev);
Roger Quadrosf7a89812015-07-06 17:46:58 +0300447
Chanwoo Choiab11af042016-07-22 13:16:34 +0900448 /* This could be in interrupt handler */
449 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
450 if (!prop_buf) {
451 /* Unlock early before uevent */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900452 spin_unlock_irqrestore(&edev->lock, flags);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900453
454 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
455 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
456
Pan Biane7d9dd52016-12-03 16:56:49 +0800457 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900458 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900459
Chanwoo Choiab11af042016-07-22 13:16:34 +0900460 length = name_show(&edev->dev, NULL, prop_buf);
461 if (length > 0) {
462 if (prop_buf[length - 1] == '\n')
463 prop_buf[length - 1] = 0;
464 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
465 envp[env_offset++] = name_buf;
466 }
467
468 length = state_show(&edev->dev, NULL, prop_buf);
469 if (length > 0) {
470 if (prop_buf[length - 1] == '\n')
471 prop_buf[length - 1] = 0;
472 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
473 envp[env_offset++] = state_buf;
474 }
475 envp[env_offset] = NULL;
476
477 /* Unlock early before uevent */
478 spin_unlock_irqrestore(&edev->lock, flags);
479 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
480 free_page((unsigned long)prop_buf);
481
MyungJoo Hambde68e62012-04-20 14:16:26 +0900482 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900483}
Chanwoo Choiab11af042016-07-22 13:16:34 +0900484EXPORT_SYMBOL_GPL(extcon_sync);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900485
Donggeun Kim74c5d092012-04-20 14:16:24 +0900486/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900487 * extcon_get_state() - Get the state of a external connector.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900488 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900489 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900490 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900491int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900492{
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900493 int index, state;
494 unsigned long flags;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900495
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900496 if (!edev)
497 return -EINVAL;
498
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900499 index = find_cable_index_by_id(edev, id);
500 if (index < 0)
501 return index;
502
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900503 spin_lock_irqsave(&edev->lock, flags);
504 state = is_extcon_attached(edev, index);
505 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900506
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900507 return state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900508}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900509EXPORT_SYMBOL_GPL(extcon_get_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900510
511/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900512 * extcon_set_state() - Set the state of a external connector.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900513 * without a notification.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900514 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900515 * @id: the unique id of each external connector
516 * in extcon enumeration.
517 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900518 * true: attached / false: detached.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900519 *
520 * This function only set the state of a external connector without
521 * a notification. To synchronize the data of a external connector,
522 * use extcon_set_state_sync() and extcon_sync().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900523 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900524int extcon_set_state(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900525 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900526{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900527 unsigned long flags;
528 int index, ret = 0;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900529
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900530 if (!edev)
531 return -EINVAL;
532
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900533 index = find_cable_index_by_id(edev, id);
534 if (index < 0)
535 return index;
536
Chanwoo Choiab11af042016-07-22 13:16:34 +0900537 spin_lock_irqsave(&edev->lock, flags);
538
539 /* Check whether the external connector's state is changed. */
540 if (!is_extcon_changed(edev, index, cable_state))
541 goto out;
542
543 if (check_mutually_exclusive(edev,
544 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
545 ret = -EPERM;
546 goto out;
547 }
548
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900549 /*
550 * Initialize the value of extcon property before setting
551 * the detached state for an external connector.
552 */
553 if (!cable_state)
554 init_property(edev, id, index);
555
Chanwoo Choiab11af042016-07-22 13:16:34 +0900556 /* Update the state for a external connector. */
557 if (cable_state)
558 edev->state |= BIT(index);
559 else
560 edev->state &= ~(BIT(index));
561out:
562 spin_unlock_irqrestore(&edev->lock, flags);
563
564 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900565}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900566EXPORT_SYMBOL_GPL(extcon_set_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900567
568/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900569 * extcon_set_state_sync() - Set the state of a external connector
570 * with a notification.
571 * @edev: the extcon device that has the cable.
572 * @id: the unique id of each external connector
573 * in extcon enumeration.
574 * @state: the new cable status. The default semantics is
575 * true: attached / false: detached.
576 *
577 * This function set the state of external connector and synchronize the data
578 * by usning a notification.
579 */
580int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
581 bool cable_state)
582{
583 int ret, index;
584 unsigned long flags;
585
586 index = find_cable_index_by_id(edev, id);
587 if (index < 0)
588 return index;
589
590 /* Check whether the external connector's state is changed. */
591 spin_lock_irqsave(&edev->lock, flags);
592 ret = is_extcon_changed(edev, index, cable_state);
593 spin_unlock_irqrestore(&edev->lock, flags);
594 if (!ret)
595 return 0;
596
597 ret = extcon_set_state(edev, id, cable_state);
598 if (ret < 0)
599 return ret;
600
601 return extcon_sync(edev, id);
602}
603EXPORT_SYMBOL_GPL(extcon_set_state_sync);
604
605/**
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900606 * extcon_get_property() - Get the property value of a specific cable.
607 * @edev: the extcon device that has the cable.
608 * @id: the unique id of each external connector
609 * in extcon enumeration.
610 * @prop: the property id among enum extcon_property.
611 * @prop_val: the pointer which store the value of property.
612 *
613 * When getting the property value of external connector, the external connector
614 * should be attached. If detached state, function just return 0 without
615 * property value. Also, the each property should be included in the list of
616 * supported properties according to the type of external connectors.
617 *
618 * Returns 0 if success or error number if fail
619 */
620int extcon_get_property(struct extcon_dev *edev, unsigned int id,
621 unsigned int prop,
622 union extcon_property_value *prop_val)
623{
624 struct extcon_cable *cable;
625 unsigned long flags;
626 int index, ret = 0;
627
628 *prop_val = (union extcon_property_value)(0);
629
630 if (!edev)
631 return -EINVAL;
632
633 /* Check whether the property is supported or not */
634 if (!is_extcon_property_supported(id, prop))
635 return -EINVAL;
636
637 /* Find the cable index of external connector by using id */
638 index = find_cable_index_by_id(edev, id);
639 if (index < 0)
640 return index;
641
642 spin_lock_irqsave(&edev->lock, flags);
643
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900644 /* Check whether the property is available or not. */
645 if (!is_extcon_property_capability(edev, id, index, prop)) {
646 spin_unlock_irqrestore(&edev->lock, flags);
647 return -EPERM;
648 }
649
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900650 /*
651 * Check whether the external connector is attached.
652 * If external connector is detached, the user can not
653 * get the property value.
654 */
655 if (!is_extcon_attached(edev, index)) {
656 spin_unlock_irqrestore(&edev->lock, flags);
657 return 0;
658 }
659
660 cable = &edev->cables[index];
661
662 /* Get the property value according to extcon type */
663 switch (prop) {
664 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
665 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
666 break;
667 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
668 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
669 break;
670 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
671 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
672 break;
673 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
674 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
675 break;
676 default:
677 ret = -EINVAL;
678 break;
679 }
680
681 spin_unlock_irqrestore(&edev->lock, flags);
682
683 return ret;
684}
685EXPORT_SYMBOL_GPL(extcon_get_property);
686
687/**
688 * extcon_set_property() - Set the property value of a specific cable.
689 * @edev: the extcon device that has the cable.
690 * @id: the unique id of each external connector
691 * in extcon enumeration.
692 * @prop: the property id among enum extcon_property.
693 * @prop_val: the pointer including the new value of property.
694 *
695 * The each property should be included in the list of supported properties
696 * according to the type of external connectors.
697 *
698 * Returns 0 if success or error number if fail
699 */
700int extcon_set_property(struct extcon_dev *edev, unsigned int id,
701 unsigned int prop,
702 union extcon_property_value prop_val)
703{
704 struct extcon_cable *cable;
705 unsigned long flags;
706 int index, ret = 0;
707
708 if (!edev)
709 return -EINVAL;
710
711 /* Check whether the property is supported or not */
712 if (!is_extcon_property_supported(id, prop))
713 return -EINVAL;
714
715 /* Find the cable index of external connector by using id */
716 index = find_cable_index_by_id(edev, id);
717 if (index < 0)
718 return index;
719
720 spin_lock_irqsave(&edev->lock, flags);
721
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900722 /* Check whether the property is available or not. */
723 if (!is_extcon_property_capability(edev, id, index, prop)) {
724 spin_unlock_irqrestore(&edev->lock, flags);
725 return -EPERM;
726 }
727
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900728 cable = &edev->cables[index];
729
730 /* Set the property value according to extcon type */
731 switch (prop) {
732 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
733 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
734 break;
735 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
736 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
737 break;
738 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
739 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
740 break;
741 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
742 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
743 break;
744 default:
745 ret = -EINVAL;
746 break;
747 }
748
749 spin_unlock_irqrestore(&edev->lock, flags);
750
751 return ret;
752}
753EXPORT_SYMBOL_GPL(extcon_set_property);
754
755/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900756 * extcon_set_property_sync() - Set the property value of a specific cable
757 with a notification.
758 * @prop_val: the pointer including the new value of property.
759 *
760 * When setting the property value of external connector, the external connector
761 * should be attached. The each property should be included in the list of
762 * supported properties according to the type of external connectors.
763 *
764 * Returns 0 if success or error number if fail
765 */
766int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
767 unsigned int prop,
768 union extcon_property_value prop_val)
769{
770 int ret;
771
772 ret = extcon_set_property(edev, id, prop, prop_val);
773 if (ret < 0)
774 return ret;
775
776 return extcon_sync(edev, id);
777}
778EXPORT_SYMBOL_GPL(extcon_set_property_sync);
779
780/**
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900781 * extcon_get_property_capability() - Get the capability of property
782 * of an external connector.
783 * @edev: the extcon device that has the cable.
784 * @id: the unique id of each external connector
785 * in extcon enumeration.
786 * @prop: the property id among enum extcon_property.
787 *
788 * Returns 1 if the property is available or 0 if not available.
789 */
790int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
791 unsigned int prop)
792{
793 int index;
794
795 if (!edev)
796 return -EINVAL;
797
798 /* Check whether the property is supported or not */
799 if (!is_extcon_property_supported(id, prop))
800 return -EINVAL;
801
802 /* Find the cable index of external connector by using id */
803 index = find_cable_index_by_id(edev, id);
804 if (index < 0)
805 return index;
806
807 return is_extcon_property_capability(edev, id, index, prop);
808}
809EXPORT_SYMBOL_GPL(extcon_get_property_capability);
810
811/**
812 * extcon_set_property_capability() - Set the capability of a property
813 * of an external connector.
814 * @edev: the extcon device that has the cable.
815 * @id: the unique id of each external connector
816 * in extcon enumeration.
817 * @prop: the property id among enum extcon_property.
818 *
819 * This function set the capability of a property for an external connector
820 * to mark the bit in capability bitmap which mean the available state of
821 * a property.
822 *
823 * Returns 0 if success or error number if fail
824 */
825int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
826 unsigned int prop)
827{
828 struct extcon_cable *cable;
829 int index, type, ret = 0;
830
831 if (!edev)
832 return -EINVAL;
833
834 /* Check whether the property is supported or not. */
835 if (!is_extcon_property_supported(id, prop))
836 return -EINVAL;
837
838 /* Find the cable index of external connector by using id. */
839 index = find_cable_index_by_id(edev, id);
840 if (index < 0)
841 return index;
842
843 type = get_extcon_type(prop);
844 if (type < 0)
845 return type;
846
847 cable = &edev->cables[index];
848
849 switch (type) {
850 case EXTCON_TYPE_USB:
851 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
852 break;
853 case EXTCON_TYPE_CHG:
854 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
855 break;
856 case EXTCON_TYPE_JACK:
857 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
858 break;
859 case EXTCON_TYPE_DISP:
860 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
861 break;
862 default:
863 ret = -EINVAL;
864 }
865
866 return ret;
867}
868EXPORT_SYMBOL_GPL(extcon_set_property_capability);
869
870/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900871 * extcon_get_extcon_dev() - Get the extcon device instance from the name
872 * @extcon_name: The extcon name provided with extcon_dev_register()
873 */
874struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
875{
876 struct extcon_dev *sd;
877
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900878 if (!extcon_name)
879 return ERR_PTR(-EINVAL);
880
Donggeun Kim74c5d092012-04-20 14:16:24 +0900881 mutex_lock(&extcon_dev_list_lock);
882 list_for_each_entry(sd, &extcon_dev_list, entry) {
883 if (!strcmp(sd->name, extcon_name))
884 goto out;
885 }
886 sd = NULL;
887out:
888 mutex_unlock(&extcon_dev_list_lock);
889 return sd;
890}
891EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
892
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900893/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900894 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900895 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900896 * @edev: the extcon device that has the external connecotr.
897 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900898 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900899 *
900 * Note that the second parameter given to the callback of nb (val) is
901 * "old_state", not the current state. The current state can be retrieved
902 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900903 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900904int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900905 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900906{
Hans de Goede66bee352015-03-21 17:26:24 +0100907 unsigned long flags;
Maninder Singh2c8116a2016-08-01 14:51:30 +0530908 int ret, idx = -EINVAL;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900909
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900910 if (!edev || !nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900911 return -EINVAL;
912
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900913 idx = find_cable_index_by_id(edev, id);
914 if (idx < 0)
915 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100916
Chanwoo Choi01b4c9a2016-12-19 21:02:33 +0900917 spin_lock_irqsave(&edev->lock, flags);
918 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
919 spin_unlock_irqrestore(&edev->lock, flags);
Hans de Goede66bee352015-03-21 17:26:24 +0100920
921 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900922}
923EXPORT_SYMBOL_GPL(extcon_register_notifier);
924
925/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900926 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900927 * @edev: the extcon device that has the external connecotr.
928 * @id: the unique id of each external connector in extcon enumeration.
929 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900930 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900931int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900932 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900933{
Hans de Goede66bee352015-03-21 17:26:24 +0100934 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900935 int ret, idx;
936
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900937 if (!edev || !nb)
938 return -EINVAL;
939
Chanwoo Choi046050f2015-05-19 20:01:12 +0900940 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900941 if (idx < 0)
942 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100943
944 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900945 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100946 spin_unlock_irqrestore(&edev->lock, flags);
947
948 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900949}
950EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
951
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700952static struct attribute *extcon_attrs[] = {
953 &dev_attr_state.attr,
954 &dev_attr_name.attr,
955 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900956};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700957ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900958
959static int create_extcon_class(void)
960{
961 if (!extcon_class) {
962 extcon_class = class_create(THIS_MODULE, "extcon");
963 if (IS_ERR(extcon_class))
964 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700965 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900966
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900967#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900968 switch_class = class_compat_register("switch");
969 if (WARN(!switch_class, "cannot allocate"))
970 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900971#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900972 }
973
974 return 0;
975}
976
MyungJoo Hamde55d872012-04-20 14:16:22 +0900977static void extcon_dev_release(struct device *dev)
978{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900979}
980
MyungJoo Hambde68e62012-04-20 14:16:26 +0900981static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900982static void dummy_sysfs_dev_release(struct device *dev)
983{
984}
985
Chanwoo Choia9af6522014-04-24 19:46:49 +0900986/*
987 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900988 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900989 * If supported_cable is NULL, cable name related APIs
990 * are disabled.
991 *
992 * This function allocates the memory for extcon device without allocating
993 * memory in each extcon provider driver and initialize default setting for
994 * extcon device.
995 *
996 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
997 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900998struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +0900999{
1000 struct extcon_dev *edev;
1001
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001002 if (!supported_cable)
1003 return ERR_PTR(-EINVAL);
1004
Chanwoo Choia9af6522014-04-24 19:46:49 +09001005 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1006 if (!edev)
1007 return ERR_PTR(-ENOMEM);
1008
1009 edev->max_supported = 0;
1010 edev->supported_cable = supported_cable;
1011
1012 return edev;
1013}
1014
1015/*
1016 * extcon_dev_free() - Free the memory of extcon device.
1017 * @edev: the extcon device to free
1018 */
1019void extcon_dev_free(struct extcon_dev *edev)
1020{
1021 kfree(edev);
1022}
1023EXPORT_SYMBOL_GPL(extcon_dev_free);
1024
MyungJoo Hamde55d872012-04-20 14:16:22 +09001025/**
1026 * extcon_dev_register() - Register a new extcon device
1027 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001028 *
1029 * Among the members of edev struct, please set the "user initializing data"
1030 * in any case and set the "optional callbacks" if required. However, please
1031 * do not set the values of "internal data", which are initialized by
1032 * this function.
1033 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001034int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001035{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001036 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001037 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001038
1039 if (!extcon_class) {
1040 ret = create_extcon_class();
1041 if (ret < 0)
1042 return ret;
1043 }
1044
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001045 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001046 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001047
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001048 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1049
1050 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001051 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001052 dev_err(&edev->dev,
1053 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001054 return -EINVAL;
1055 }
1056
Chanwoo Choidae61652013-09-27 09:19:40 +09001057 edev->dev.class = extcon_class;
1058 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001059
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001060 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001061 if (IS_ERR_OR_NULL(edev->name)) {
1062 dev_err(&edev->dev,
1063 "extcon device name is null\n");
1064 return -EINVAL;
1065 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001066 dev_set_name(&edev->dev, "extcon%lu",
1067 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001068
1069 if (edev->max_supported) {
1070 char buf[10];
1071 char *str;
1072 struct extcon_cable *cable;
1073
1074 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1075 edev->max_supported, GFP_KERNEL);
1076 if (!edev->cables) {
1077 ret = -ENOMEM;
1078 goto err_sysfs_alloc;
1079 }
1080 for (index = 0; index < edev->max_supported; index++) {
1081 cable = &edev->cables[index];
1082
1083 snprintf(buf, 10, "cable.%d", index);
1084 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1085 GFP_KERNEL);
1086 if (!str) {
1087 for (index--; index >= 0; index--) {
1088 cable = &edev->cables[index];
1089 kfree(cable->attr_g.name);
1090 }
1091 ret = -ENOMEM;
1092
1093 goto err_alloc_cables;
1094 }
1095 strcpy(str, buf);
1096
1097 cable->edev = edev;
1098 cable->cable_index = index;
1099 cable->attrs[0] = &cable->attr_name.attr;
1100 cable->attrs[1] = &cable->attr_state.attr;
1101 cable->attrs[2] = NULL;
1102 cable->attr_g.name = str;
1103 cable->attr_g.attrs = cable->attrs;
1104
Mark Brown9baf3222012-08-16 20:03:21 +01001105 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001106 cable->attr_name.attr.name = "name";
1107 cable->attr_name.attr.mode = 0444;
1108 cable->attr_name.show = cable_name_show;
1109
Mark Brown9baf3222012-08-16 20:03:21 +01001110 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001111 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +09001112 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001113 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001114 }
1115 }
1116
MyungJoo Hambde68e62012-04-20 14:16:26 +09001117 if (edev->max_supported && edev->mutually_exclusive) {
1118 char buf[80];
1119 char *name;
1120
1121 /* Count the size of mutually_exclusive array */
1122 for (index = 0; edev->mutually_exclusive[index]; index++)
1123 ;
1124
1125 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1126 (index + 1), GFP_KERNEL);
1127 if (!edev->attrs_muex) {
1128 ret = -ENOMEM;
1129 goto err_muex;
1130 }
1131
1132 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1133 index, GFP_KERNEL);
1134 if (!edev->d_attrs_muex) {
1135 ret = -ENOMEM;
1136 kfree(edev->attrs_muex);
1137 goto err_muex;
1138 }
1139
1140 for (index = 0; edev->mutually_exclusive[index]; index++) {
1141 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1142 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1143 GFP_KERNEL);
1144 if (!name) {
1145 for (index--; index >= 0; index--) {
1146 kfree(edev->d_attrs_muex[index].attr.
1147 name);
1148 }
1149 kfree(edev->d_attrs_muex);
1150 kfree(edev->attrs_muex);
1151 ret = -ENOMEM;
1152 goto err_muex;
1153 }
1154 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +01001155 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001156 edev->d_attrs_muex[index].attr.name = name;
1157 edev->d_attrs_muex[index].attr.mode = 0000;
1158 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1159 .attr;
1160 }
1161 edev->attr_g_muex.name = muex_name;
1162 edev->attr_g_muex.attrs = edev->attrs_muex;
1163
1164 }
1165
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001166 if (edev->max_supported) {
1167 edev->extcon_dev_type.groups =
1168 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +09001169 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001170 if (!edev->extcon_dev_type.groups) {
1171 ret = -ENOMEM;
1172 goto err_alloc_groups;
1173 }
1174
Chanwoo Choidae61652013-09-27 09:19:40 +09001175 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001176 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1177
1178 for (index = 0; index < edev->max_supported; index++)
1179 edev->extcon_dev_type.groups[index] =
1180 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +09001181 if (edev->mutually_exclusive)
1182 edev->extcon_dev_type.groups[index] =
1183 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001184
Chanwoo Choidae61652013-09-27 09:19:40 +09001185 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001186 }
1187
Chanwoo Choidae61652013-09-27 09:19:40 +09001188 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001189 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +09001190 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001191 goto err_dev;
1192 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001193#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001194 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001195 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001196#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +09001197
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001198 spin_lock_init(&edev->lock);
1199
Chanwoo Choi046050f2015-05-19 20:01:12 +09001200 edev->nh = devm_kzalloc(&edev->dev,
1201 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1202 if (!edev->nh) {
1203 ret = -ENOMEM;
1204 goto err_dev;
1205 }
1206
1207 for (index = 0; index < edev->max_supported; index++)
1208 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +09001209
Chanwoo Choidae61652013-09-27 09:19:40 +09001210 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001211 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +09001212
1213 mutex_lock(&extcon_dev_list_lock);
1214 list_add(&edev->entry, &extcon_dev_list);
1215 mutex_unlock(&extcon_dev_list_lock);
1216
MyungJoo Hamde55d872012-04-20 14:16:22 +09001217 return 0;
1218
1219err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001220 if (edev->max_supported)
1221 kfree(edev->extcon_dev_type.groups);
1222err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +09001223 if (edev->max_supported && edev->mutually_exclusive) {
1224 for (index = 0; edev->mutually_exclusive[index]; index++)
1225 kfree(edev->d_attrs_muex[index].attr.name);
1226 kfree(edev->d_attrs_muex);
1227 kfree(edev->attrs_muex);
1228 }
1229err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001230 for (index = 0; index < edev->max_supported; index++)
1231 kfree(edev->cables[index].attr_g.name);
1232err_alloc_cables:
1233 if (edev->max_supported)
1234 kfree(edev->cables);
1235err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +09001236 return ret;
1237}
1238EXPORT_SYMBOL_GPL(extcon_dev_register);
1239
1240/**
1241 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +09001242 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +09001243 *
1244 * Note that this does not call kfree(edev) because edev was not allocated
1245 * by this class.
1246 */
1247void extcon_dev_unregister(struct extcon_dev *edev)
1248{
anish kumar57e7cd32012-10-22 09:43:33 +09001249 int index;
1250
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001251 if (!edev)
1252 return;
1253
anish kumar57e7cd32012-10-22 09:43:33 +09001254 mutex_lock(&extcon_dev_list_lock);
1255 list_del(&edev->entry);
1256 mutex_unlock(&extcon_dev_list_lock);
1257
Chanwoo Choidae61652013-09-27 09:19:40 +09001258 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1259 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1260 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +09001261 return;
1262 }
1263
Wang, Xiaoming7585ca02013-11-01 18:48:14 -04001264 device_unregister(&edev->dev);
1265
anish kumar57e7cd32012-10-22 09:43:33 +09001266 if (edev->mutually_exclusive && edev->max_supported) {
1267 for (index = 0; edev->mutually_exclusive[index];
1268 index++)
1269 kfree(edev->d_attrs_muex[index].attr.name);
1270 kfree(edev->d_attrs_muex);
1271 kfree(edev->attrs_muex);
1272 }
1273
1274 for (index = 0; index < edev->max_supported; index++)
1275 kfree(edev->cables[index].attr_g.name);
1276
1277 if (edev->max_supported) {
1278 kfree(edev->extcon_dev_type.groups);
1279 kfree(edev->cables);
1280 }
1281
1282#if defined(CONFIG_ANDROID)
1283 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001284 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +09001285#endif
Chanwoo Choidae61652013-09-27 09:19:40 +09001286 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001287}
1288EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1289
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001290#ifdef CONFIG_OF
1291/*
1292 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1293 * @dev - instance to the given device
1294 * @index - index into list of extcon_dev
1295 *
1296 * return the instance of extcon device
1297 */
1298struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1299{
1300 struct device_node *node;
1301 struct extcon_dev *edev;
1302
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001303 if (!dev)
1304 return ERR_PTR(-EINVAL);
1305
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001306 if (!dev->of_node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001307 dev_dbg(dev, "device does not have a device node entry\n");
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001308 return ERR_PTR(-EINVAL);
1309 }
1310
1311 node = of_parse_phandle(dev->of_node, "extcon", index);
1312 if (!node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001313 dev_dbg(dev, "failed to get phandle in %s node\n",
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001314 dev->of_node->full_name);
1315 return ERR_PTR(-ENODEV);
1316 }
1317
Tomasz Figaf841afb2014-10-16 15:11:44 +02001318 mutex_lock(&extcon_dev_list_lock);
1319 list_for_each_entry(edev, &extcon_dev_list, entry) {
1320 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1321 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001322 of_node_put(node);
Tomasz Figaf841afb2014-10-16 15:11:44 +02001323 return edev;
1324 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001325 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001326 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001327 of_node_put(node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001328
Tomasz Figaf841afb2014-10-16 15:11:44 +02001329 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001330}
1331#else
1332struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1333{
1334 return ERR_PTR(-ENOSYS);
1335}
1336#endif /* CONFIG_OF */
1337EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1338
Chanwoo Choi707d7552015-04-15 13:57:51 +09001339/**
1340 * extcon_get_edev_name() - Get the name of the extcon device.
1341 * @edev: the extcon device
1342 */
1343const char *extcon_get_edev_name(struct extcon_dev *edev)
1344{
1345 return !edev ? NULL : edev->name;
1346}
1347
MyungJoo Hamde55d872012-04-20 14:16:22 +09001348static int __init extcon_class_init(void)
1349{
1350 return create_extcon_class();
1351}
1352module_init(extcon_class_init);
1353
1354static void __exit extcon_class_exit(void)
1355{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001356#if defined(CONFIG_ANDROID)
1357 class_compat_unregister(switch_class);
1358#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001359 class_destroy(extcon_class);
1360}
1361module_exit(extcon_class_exit);
1362
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001363MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001364MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1365MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1366MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1367MODULE_DESCRIPTION("External connector (extcon) class driver");
1368MODULE_LICENSE("GPL");