blob: f209a6959fededa24a745a8554a6252b9a98bbe6 [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 Choi55e4e2f2016-07-11 16:34:52 +090041struct __extcon_info {
42 unsigned int type;
43 unsigned int id;
44 const char *name;
45
46} extcon_info[] = {
47 [EXTCON_NONE] = {
48 .type = EXTCON_TYPE_MISC,
49 .id = EXTCON_NONE,
50 .name = "NONE",
51 },
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090052
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090053 /* USB external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090054 [EXTCON_USB] = {
55 .type = EXTCON_TYPE_USB,
56 .id = EXTCON_USB,
57 .name = "USB",
58 },
59 [EXTCON_USB_HOST] = {
60 .type = EXTCON_TYPE_USB,
61 .id = EXTCON_USB_HOST,
62 .name = "USB_HOST",
63 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090064
Chanwoo Choi11eecf92015-10-03 14:15:13 +090065 /* Charging external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090066 [EXTCON_CHG_USB_SDP] = {
67 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
68 .id = EXTCON_CHG_USB_SDP,
69 .name = "SDP",
70 },
71 [EXTCON_CHG_USB_DCP] = {
72 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
73 .id = EXTCON_CHG_USB_DCP,
74 .name = "DCP",
75 },
76 [EXTCON_CHG_USB_CDP] = {
77 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
78 .id = EXTCON_CHG_USB_CDP,
79 .name = "CDP",
80 },
81 [EXTCON_CHG_USB_ACA] = {
82 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
83 .id = EXTCON_CHG_USB_ACA,
84 .name = "ACA",
85 },
86 [EXTCON_CHG_USB_FAST] = {
87 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
88 .id = EXTCON_CHG_USB_FAST,
89 .name = "FAST-CHARGER",
90 },
91 [EXTCON_CHG_USB_SLOW] = {
92 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
93 .id = EXTCON_CHG_USB_SLOW,
94 .name = "SLOW-CHARGER",
95 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090096
Chanwoo Choi11eecf92015-10-03 14:15:13 +090097 /* Jack external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090098 [EXTCON_JACK_MICROPHONE] = {
99 .type = EXTCON_TYPE_JACK,
100 .id = EXTCON_JACK_MICROPHONE,
101 .name = "MICROPHONE",
102 },
103 [EXTCON_JACK_HEADPHONE] = {
104 .type = EXTCON_TYPE_JACK,
105 .id = EXTCON_JACK_HEADPHONE,
106 .name = "HEADPHONE",
107 },
108 [EXTCON_JACK_LINE_IN] = {
109 .type = EXTCON_TYPE_JACK,
110 .id = EXTCON_JACK_LINE_IN,
111 .name = "LINE-IN",
112 },
113 [EXTCON_JACK_LINE_OUT] = {
114 .type = EXTCON_TYPE_JACK,
115 .id = EXTCON_JACK_LINE_OUT,
116 .name = "LINE-OUT",
117 },
118 [EXTCON_JACK_VIDEO_IN] = {
119 .type = EXTCON_TYPE_JACK,
120 .id = EXTCON_JACK_VIDEO_IN,
121 .name = "VIDEO-IN",
122 },
123 [EXTCON_JACK_VIDEO_OUT] = {
124 .type = EXTCON_TYPE_JACK,
125 .id = EXTCON_JACK_VIDEO_OUT,
126 .name = "VIDEO-OUT",
127 },
128 [EXTCON_JACK_SPDIF_IN] = {
129 .type = EXTCON_TYPE_JACK,
130 .id = EXTCON_JACK_SPDIF_IN,
131 .name = "SPDIF-IN",
132 },
133 [EXTCON_JACK_SPDIF_OUT] = {
134 .type = EXTCON_TYPE_JACK,
135 .id = EXTCON_JACK_SPDIF_OUT,
136 .name = "SPDIF-OUT",
137 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900138
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900139 /* Display external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900140 [EXTCON_DISP_HDMI] = {
141 .type = EXTCON_TYPE_DISP,
142 .id = EXTCON_DISP_HDMI,
143 .name = "HDMI",
144 },
145 [EXTCON_DISP_MHL] = {
146 .type = EXTCON_TYPE_DISP,
147 .id = EXTCON_DISP_MHL,
148 .name = "MHL",
149 },
150 [EXTCON_DISP_DVI] = {
151 .type = EXTCON_TYPE_DISP,
152 .id = EXTCON_DISP_DVI,
153 .name = "DVI",
154 },
155 [EXTCON_DISP_VGA] = {
156 .type = EXTCON_TYPE_DISP,
157 .id = EXTCON_DISP_VGA,
158 .name = "VGA",
159 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900160
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900161 /* Miscellaneous external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900162 [EXTCON_DOCK] = {
163 .type = EXTCON_TYPE_MISC,
164 .id = EXTCON_DOCK,
165 .name = "DOCK",
166 },
167 [EXTCON_JIG] = {
168 .type = EXTCON_TYPE_MISC,
169 .id = EXTCON_JIG,
170 .name = "JIG",
171 },
172 [EXTCON_MECHANICAL] = {
173 .type = EXTCON_TYPE_MISC,
174 .id = EXTCON_MECHANICAL,
175 .name = "MECHANICAL",
176 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900177
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900178 { /* sentinel */ }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900179};
180
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900181/**
182 * struct extcon_cable - An internal data for each cable of extcon device.
183 * @edev: The extcon device
184 * @cable_index: Index of this cable in the edev
185 * @attr_g: Attribute group for the cable
186 * @attr_name: "name" sysfs entry
187 * @attr_state: "state" sysfs entry
188 * @attrs: Array pointing to attr_name and attr_state for attr_g
189 */
190struct extcon_cable {
191 struct extcon_dev *edev;
192 int cable_index;
193
194 struct attribute_group attr_g;
195 struct device_attribute attr_name;
196 struct device_attribute attr_state;
197
198 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
199};
200
Mark Brownbe3a07f2012-06-05 16:14:38 +0100201static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900202#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900203static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900204#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900205
Donggeun Kim74c5d092012-04-20 14:16:24 +0900206static LIST_HEAD(extcon_dev_list);
207static DEFINE_MUTEX(extcon_dev_list_lock);
208
MyungJoo Hambde68e62012-04-20 14:16:26 +0900209/**
210 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900211 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +0900212 * @edev: the extcon device
213 * @new_state: new cable attach status for @edev
214 *
215 * Returns 0 if nothing violates. Returns the index + 1 for the first
216 * violated condition.
217 */
218static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
219{
220 int i = 0;
221
222 if (!edev->mutually_exclusive)
223 return 0;
224
225 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530226 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900227 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900228
anish kumar28c0ada2012-08-30 00:35:10 +0530229 /* calculate the total number of bits set */
230 weight = hweight32(correspondants);
231 if (weight > 1)
232 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900233 }
234
235 return 0;
236}
237
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900238static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900239{
240 int i;
241
242 /* Find the the index of extcon cable in edev->supported_cable */
243 for (i = 0; i < edev->max_supported; i++) {
244 if (edev->supported_cable[i] == id)
245 return i;
246 }
247
248 return -EINVAL;
249}
250
Chanwoo Choi046050f2015-05-19 20:01:12 +0900251static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
252{
253 if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
Hans de Goedef4513b02015-08-24 00:35:36 +0200254 *attached = ((new >> idx) & 0x1) ? true : false;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900255 return true;
256 }
257
258 return false;
259}
260
MyungJoo Hamde55d872012-04-20 14:16:22 +0900261static ssize_t state_show(struct device *dev, struct device_attribute *attr,
262 char *buf)
263{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900264 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900265 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900266
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900267 if (edev->max_supported == 0)
268 return sprintf(buf, "%u\n", edev->state);
269
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900270 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900271 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900272 extcon_info[edev->supported_cable[i]].name,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900273 !!(edev->state & (1 << i)));
274 }
275
276 return count;
277}
Chanwoo Choi5d5321e2016-07-18 15:39:28 +0900278static DEVICE_ATTR_RO(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900279
280static ssize_t name_show(struct device *dev, struct device_attribute *attr,
281 char *buf)
282{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900283 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900284
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900285 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900286}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700287static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900288
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900289static ssize_t cable_name_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
292 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
293 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900294 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900295
296 return sprintf(buf, "%s\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900297 extcon_info[cable->edev->supported_cable[i]].name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900298}
299
300static ssize_t cable_state_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
302{
303 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
304 attr_state);
305
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300306 int i = cable->cable_index;
307
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900308 return sprintf(buf, "%d\n",
309 extcon_get_cable_state_(cable->edev,
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300310 cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900311}
312
MyungJoo Hamde55d872012-04-20 14:16:22 +0900313/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900314 * extcon_update_state() - Update the cable attach states of the extcon device
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900315 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900316 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900317 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900318 * @state: new cable attach status for @edev
319 *
320 * Changing the state sends uevent with environment variable containing
321 * the name of extcon device (envp[0]) and the state output (envp[1]).
322 * Tizen uses this format for extcon device to get events from ports.
323 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900324 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900325 * Note that the notifier provides which bits are changed in the state
326 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900327 */
Chanwoo Choi912465b2016-07-01 02:41:18 +0900328static int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900329{
330 char name_buf[120];
331 char state_buf[120];
332 char *prop_buf;
333 char *envp[3];
334 int env_offset = 0;
335 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900336 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900337 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900338 bool attached;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900339
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900340 if (!edev)
341 return -EINVAL;
342
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900343 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900344
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900345 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
Roger Quadrosf7a89812015-07-06 17:46:58 +0300346 u32 old_state;
347
MyungJoo Hambde68e62012-04-20 14:16:26 +0900348 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
349 (state & mask))) {
350 spin_unlock_irqrestore(&edev->lock, flags);
351 return -EPERM;
352 }
353
Roger Quadrosf7a89812015-07-06 17:46:58 +0300354 old_state = edev->state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900355 edev->state &= ~mask;
356 edev->state |= state & mask;
357
Roger Quadrosf7a89812015-07-06 17:46:58 +0300358 for (index = 0; index < edev->max_supported; index++) {
359 if (is_extcon_changed(old_state, edev->state, index,
360 &attached))
361 raw_notifier_call_chain(&edev->nh[index],
362 attached, edev);
363 }
364
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900365 /* This could be in interrupt handler */
366 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900367 if (prop_buf) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900368 length = name_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900369 if (length > 0) {
370 if (prop_buf[length - 1] == '\n')
371 prop_buf[length - 1] = 0;
372 snprintf(name_buf, sizeof(name_buf),
373 "NAME=%s", prop_buf);
374 envp[env_offset++] = name_buf;
375 }
Chanwoo Choidae61652013-09-27 09:19:40 +0900376 length = state_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900377 if (length > 0) {
378 if (prop_buf[length - 1] == '\n')
379 prop_buf[length - 1] = 0;
380 snprintf(state_buf, sizeof(state_buf),
381 "STATE=%s", prop_buf);
382 envp[env_offset++] = state_buf;
383 }
384 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900385 /* Unlock early before uevent */
386 spin_unlock_irqrestore(&edev->lock, flags);
387
Chanwoo Choidae61652013-09-27 09:19:40 +0900388 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900389 free_page((unsigned long)prop_buf);
390 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900391 /* Unlock early before uevent */
392 spin_unlock_irqrestore(&edev->lock, flags);
393
Chanwoo Choidae61652013-09-27 09:19:40 +0900394 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
395 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900396 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900397 } else {
398 /* No changes */
399 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900400 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900401
402 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900403}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900404
Donggeun Kim74c5d092012-04-20 14:16:24 +0900405/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900406 * extcon_get_cable_state_() - Get the status of a specific cable.
407 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900408 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900409 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900410int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900411{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900412 int index;
413
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900414 if (!edev)
415 return -EINVAL;
416
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900417 index = find_cable_index_by_id(edev, id);
418 if (index < 0)
419 return index;
420
421 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900422 return -EINVAL;
423
424 return !!(edev->state & (1 << index));
425}
426EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
427
428/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900429 * extcon_set_cable_state_() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900430 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900431 * @id: the unique id of each external connector
432 * in extcon enumeration.
433 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900434 * true: attached / false: detached.
435 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900436int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900437 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900438{
439 u32 state;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900440 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900441
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900442 if (!edev)
443 return -EINVAL;
444
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900445 index = find_cable_index_by_id(edev, id);
446 if (index < 0)
447 return index;
448
449 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900450 return -EINVAL;
451
452 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900453 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900454}
455EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
456
457/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900458 * extcon_get_extcon_dev() - Get the extcon device instance from the name
459 * @extcon_name: The extcon name provided with extcon_dev_register()
460 */
461struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
462{
463 struct extcon_dev *sd;
464
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900465 if (!extcon_name)
466 return ERR_PTR(-EINVAL);
467
Donggeun Kim74c5d092012-04-20 14:16:24 +0900468 mutex_lock(&extcon_dev_list_lock);
469 list_for_each_entry(sd, &extcon_dev_list, entry) {
470 if (!strcmp(sd->name, extcon_name))
471 goto out;
472 }
473 sd = NULL;
474out:
475 mutex_unlock(&extcon_dev_list_lock);
476 return sd;
477}
478EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
479
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900480/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900481 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900482 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900483 * @edev: the extcon device that has the external connecotr.
484 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900485 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900486 *
487 * Note that the second parameter given to the callback of nb (val) is
488 * "old_state", not the current state. The current state can be retrieved
489 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900490 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900491int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900492 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900493{
Hans de Goede66bee352015-03-21 17:26:24 +0100494 unsigned long flags;
Maninder Singh2c8116a2016-08-01 14:51:30 +0530495 int ret, idx = -EINVAL;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900496
Chanwoo Choi830ae442016-05-31 17:32:30 +0900497 if (!nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900498 return -EINVAL;
499
Chanwoo Choi830ae442016-05-31 17:32:30 +0900500 if (edev) {
501 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900502 if (idx < 0)
503 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100504
Chanwoo Choi830ae442016-05-31 17:32:30 +0900505 spin_lock_irqsave(&edev->lock, flags);
506 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
507 spin_unlock_irqrestore(&edev->lock, flags);
508 } else {
509 struct extcon_dev *extd;
510
511 mutex_lock(&extcon_dev_list_lock);
512 list_for_each_entry(extd, &extcon_dev_list, entry) {
513 idx = find_cable_index_by_id(extd, id);
514 if (idx >= 0)
515 break;
516 }
517 mutex_unlock(&extcon_dev_list_lock);
518
519 if (idx >= 0) {
520 edev = extd;
521 return extcon_register_notifier(extd, id, nb);
522 } else {
523 ret = -ENODEV;
524 }
525 }
Hans de Goede66bee352015-03-21 17:26:24 +0100526
527 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900528}
529EXPORT_SYMBOL_GPL(extcon_register_notifier);
530
531/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900532 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900533 * @edev: the extcon device that has the external connecotr.
534 * @id: the unique id of each external connector in extcon enumeration.
535 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900536 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900537int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900538 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900539{
Hans de Goede66bee352015-03-21 17:26:24 +0100540 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900541 int ret, idx;
542
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900543 if (!edev || !nb)
544 return -EINVAL;
545
Chanwoo Choi046050f2015-05-19 20:01:12 +0900546 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900547 if (idx < 0)
548 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100549
550 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900551 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100552 spin_unlock_irqrestore(&edev->lock, flags);
553
554 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900555}
556EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
557
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700558static struct attribute *extcon_attrs[] = {
559 &dev_attr_state.attr,
560 &dev_attr_name.attr,
561 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900562};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700563ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900564
565static int create_extcon_class(void)
566{
567 if (!extcon_class) {
568 extcon_class = class_create(THIS_MODULE, "extcon");
569 if (IS_ERR(extcon_class))
570 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700571 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900572
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900573#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900574 switch_class = class_compat_register("switch");
575 if (WARN(!switch_class, "cannot allocate"))
576 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900577#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900578 }
579
580 return 0;
581}
582
MyungJoo Hamde55d872012-04-20 14:16:22 +0900583static void extcon_dev_release(struct device *dev)
584{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900585}
586
MyungJoo Hambde68e62012-04-20 14:16:26 +0900587static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900588static void dummy_sysfs_dev_release(struct device *dev)
589{
590}
591
Chanwoo Choia9af6522014-04-24 19:46:49 +0900592/*
593 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900594 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900595 * If supported_cable is NULL, cable name related APIs
596 * are disabled.
597 *
598 * This function allocates the memory for extcon device without allocating
599 * memory in each extcon provider driver and initialize default setting for
600 * extcon device.
601 *
602 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
603 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900604struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +0900605{
606 struct extcon_dev *edev;
607
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900608 if (!supported_cable)
609 return ERR_PTR(-EINVAL);
610
Chanwoo Choia9af6522014-04-24 19:46:49 +0900611 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
612 if (!edev)
613 return ERR_PTR(-ENOMEM);
614
615 edev->max_supported = 0;
616 edev->supported_cable = supported_cable;
617
618 return edev;
619}
620
621/*
622 * extcon_dev_free() - Free the memory of extcon device.
623 * @edev: the extcon device to free
624 */
625void extcon_dev_free(struct extcon_dev *edev)
626{
627 kfree(edev);
628}
629EXPORT_SYMBOL_GPL(extcon_dev_free);
630
MyungJoo Hamde55d872012-04-20 14:16:22 +0900631/**
632 * extcon_dev_register() - Register a new extcon device
633 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900634 *
635 * Among the members of edev struct, please set the "user initializing data"
636 * in any case and set the "optional callbacks" if required. However, please
637 * do not set the values of "internal data", which are initialized by
638 * this function.
639 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900640int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900641{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900642 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900643 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900644
645 if (!extcon_class) {
646 ret = create_extcon_class();
647 if (ret < 0)
648 return ret;
649 }
650
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900651 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900652 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900653
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900654 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
655
656 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900657 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900658 dev_err(&edev->dev,
659 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900660 return -EINVAL;
661 }
662
Chanwoo Choidae61652013-09-27 09:19:40 +0900663 edev->dev.class = extcon_class;
664 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900665
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900666 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900667 if (IS_ERR_OR_NULL(edev->name)) {
668 dev_err(&edev->dev,
669 "extcon device name is null\n");
670 return -EINVAL;
671 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900672 dev_set_name(&edev->dev, "extcon%lu",
673 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900674
675 if (edev->max_supported) {
676 char buf[10];
677 char *str;
678 struct extcon_cable *cable;
679
680 edev->cables = kzalloc(sizeof(struct extcon_cable) *
681 edev->max_supported, GFP_KERNEL);
682 if (!edev->cables) {
683 ret = -ENOMEM;
684 goto err_sysfs_alloc;
685 }
686 for (index = 0; index < edev->max_supported; index++) {
687 cable = &edev->cables[index];
688
689 snprintf(buf, 10, "cable.%d", index);
690 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
691 GFP_KERNEL);
692 if (!str) {
693 for (index--; index >= 0; index--) {
694 cable = &edev->cables[index];
695 kfree(cable->attr_g.name);
696 }
697 ret = -ENOMEM;
698
699 goto err_alloc_cables;
700 }
701 strcpy(str, buf);
702
703 cable->edev = edev;
704 cable->cable_index = index;
705 cable->attrs[0] = &cable->attr_name.attr;
706 cable->attrs[1] = &cable->attr_state.attr;
707 cable->attrs[2] = NULL;
708 cable->attr_g.name = str;
709 cable->attr_g.attrs = cable->attrs;
710
Mark Brown9baf3222012-08-16 20:03:21 +0100711 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900712 cable->attr_name.attr.name = "name";
713 cable->attr_name.attr.mode = 0444;
714 cable->attr_name.show = cable_name_show;
715
Mark Brown9baf3222012-08-16 20:03:21 +0100716 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900717 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900718 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900719 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900720 }
721 }
722
MyungJoo Hambde68e62012-04-20 14:16:26 +0900723 if (edev->max_supported && edev->mutually_exclusive) {
724 char buf[80];
725 char *name;
726
727 /* Count the size of mutually_exclusive array */
728 for (index = 0; edev->mutually_exclusive[index]; index++)
729 ;
730
731 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
732 (index + 1), GFP_KERNEL);
733 if (!edev->attrs_muex) {
734 ret = -ENOMEM;
735 goto err_muex;
736 }
737
738 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
739 index, GFP_KERNEL);
740 if (!edev->d_attrs_muex) {
741 ret = -ENOMEM;
742 kfree(edev->attrs_muex);
743 goto err_muex;
744 }
745
746 for (index = 0; edev->mutually_exclusive[index]; index++) {
747 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
748 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
749 GFP_KERNEL);
750 if (!name) {
751 for (index--; index >= 0; index--) {
752 kfree(edev->d_attrs_muex[index].attr.
753 name);
754 }
755 kfree(edev->d_attrs_muex);
756 kfree(edev->attrs_muex);
757 ret = -ENOMEM;
758 goto err_muex;
759 }
760 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100761 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900762 edev->d_attrs_muex[index].attr.name = name;
763 edev->d_attrs_muex[index].attr.mode = 0000;
764 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
765 .attr;
766 }
767 edev->attr_g_muex.name = muex_name;
768 edev->attr_g_muex.attrs = edev->attrs_muex;
769
770 }
771
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900772 if (edev->max_supported) {
773 edev->extcon_dev_type.groups =
774 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900775 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900776 if (!edev->extcon_dev_type.groups) {
777 ret = -ENOMEM;
778 goto err_alloc_groups;
779 }
780
Chanwoo Choidae61652013-09-27 09:19:40 +0900781 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900782 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
783
784 for (index = 0; index < edev->max_supported; index++)
785 edev->extcon_dev_type.groups[index] =
786 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900787 if (edev->mutually_exclusive)
788 edev->extcon_dev_type.groups[index] =
789 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900790
Chanwoo Choidae61652013-09-27 09:19:40 +0900791 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900792 }
793
Chanwoo Choidae61652013-09-27 09:19:40 +0900794 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900795 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900796 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900797 goto err_dev;
798 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900799#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900800 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900801 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900802#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900803
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900804 spin_lock_init(&edev->lock);
805
Chanwoo Choi046050f2015-05-19 20:01:12 +0900806 edev->nh = devm_kzalloc(&edev->dev,
807 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
808 if (!edev->nh) {
809 ret = -ENOMEM;
810 goto err_dev;
811 }
812
813 for (index = 0; index < edev->max_supported; index++)
814 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900815
Chanwoo Choidae61652013-09-27 09:19:40 +0900816 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900817 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900818
819 mutex_lock(&extcon_dev_list_lock);
820 list_add(&edev->entry, &extcon_dev_list);
821 mutex_unlock(&extcon_dev_list_lock);
822
MyungJoo Hamde55d872012-04-20 14:16:22 +0900823 return 0;
824
825err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900826 if (edev->max_supported)
827 kfree(edev->extcon_dev_type.groups);
828err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900829 if (edev->max_supported && edev->mutually_exclusive) {
830 for (index = 0; edev->mutually_exclusive[index]; index++)
831 kfree(edev->d_attrs_muex[index].attr.name);
832 kfree(edev->d_attrs_muex);
833 kfree(edev->attrs_muex);
834 }
835err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900836 for (index = 0; index < edev->max_supported; index++)
837 kfree(edev->cables[index].attr_g.name);
838err_alloc_cables:
839 if (edev->max_supported)
840 kfree(edev->cables);
841err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900842 return ret;
843}
844EXPORT_SYMBOL_GPL(extcon_dev_register);
845
846/**
847 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900848 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900849 *
850 * Note that this does not call kfree(edev) because edev was not allocated
851 * by this class.
852 */
853void extcon_dev_unregister(struct extcon_dev *edev)
854{
anish kumar57e7cd32012-10-22 09:43:33 +0900855 int index;
856
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900857 if (!edev)
858 return;
859
anish kumar57e7cd32012-10-22 09:43:33 +0900860 mutex_lock(&extcon_dev_list_lock);
861 list_del(&edev->entry);
862 mutex_unlock(&extcon_dev_list_lock);
863
Chanwoo Choidae61652013-09-27 09:19:40 +0900864 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
865 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
866 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +0900867 return;
868 }
869
Wang, Xiaoming7585ca02013-11-01 18:48:14 -0400870 device_unregister(&edev->dev);
871
anish kumar57e7cd32012-10-22 09:43:33 +0900872 if (edev->mutually_exclusive && edev->max_supported) {
873 for (index = 0; edev->mutually_exclusive[index];
874 index++)
875 kfree(edev->d_attrs_muex[index].attr.name);
876 kfree(edev->d_attrs_muex);
877 kfree(edev->attrs_muex);
878 }
879
880 for (index = 0; index < edev->max_supported; index++)
881 kfree(edev->cables[index].attr_g.name);
882
883 if (edev->max_supported) {
884 kfree(edev->extcon_dev_type.groups);
885 kfree(edev->cables);
886 }
887
888#if defined(CONFIG_ANDROID)
889 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900890 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +0900891#endif
Chanwoo Choidae61652013-09-27 09:19:40 +0900892 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900893}
894EXPORT_SYMBOL_GPL(extcon_dev_unregister);
895
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900896#ifdef CONFIG_OF
897/*
898 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
899 * @dev - instance to the given device
900 * @index - index into list of extcon_dev
901 *
902 * return the instance of extcon device
903 */
904struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
905{
906 struct device_node *node;
907 struct extcon_dev *edev;
908
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900909 if (!dev)
910 return ERR_PTR(-EINVAL);
911
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900912 if (!dev->of_node) {
Stephen Boyde8752b72016-07-05 11:57:05 -0700913 dev_dbg(dev, "device does not have a device node entry\n");
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900914 return ERR_PTR(-EINVAL);
915 }
916
917 node = of_parse_phandle(dev->of_node, "extcon", index);
918 if (!node) {
Stephen Boyde8752b72016-07-05 11:57:05 -0700919 dev_dbg(dev, "failed to get phandle in %s node\n",
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900920 dev->of_node->full_name);
921 return ERR_PTR(-ENODEV);
922 }
923
Tomasz Figaf841afb2014-10-16 15:11:44 +0200924 mutex_lock(&extcon_dev_list_lock);
925 list_for_each_entry(edev, &extcon_dev_list, entry) {
926 if (edev->dev.parent && edev->dev.parent->of_node == node) {
927 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +0900928 of_node_put(node);
Tomasz Figaf841afb2014-10-16 15:11:44 +0200929 return edev;
930 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900931 }
Tomasz Figaf841afb2014-10-16 15:11:44 +0200932 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +0900933 of_node_put(node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900934
Tomasz Figaf841afb2014-10-16 15:11:44 +0200935 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900936}
937#else
938struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
939{
940 return ERR_PTR(-ENOSYS);
941}
942#endif /* CONFIG_OF */
943EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
944
Chanwoo Choi707d7552015-04-15 13:57:51 +0900945/**
946 * extcon_get_edev_name() - Get the name of the extcon device.
947 * @edev: the extcon device
948 */
949const char *extcon_get_edev_name(struct extcon_dev *edev)
950{
951 return !edev ? NULL : edev->name;
952}
953
MyungJoo Hamde55d872012-04-20 14:16:22 +0900954static int __init extcon_class_init(void)
955{
956 return create_extcon_class();
957}
958module_init(extcon_class_init);
959
960static void __exit extcon_class_exit(void)
961{
Peter Huewe0dc77b62012-09-24 15:32:31 +0900962#if defined(CONFIG_ANDROID)
963 class_compat_unregister(switch_class);
964#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +0900965 class_destroy(extcon_class);
966}
967module_exit(extcon_class_exit);
968
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900969MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +0900970MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
971MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
972MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
973MODULE_DESCRIPTION("External connector (extcon) class driver");
974MODULE_LICENSE("GPL");