blob: b5fdb5d9dbadd86bc81eff3174a9f517be09196d [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
41static const char *extcon_name[] = {
Chanwoo Choi11eecf92015-10-03 14:15:13 +090042 [EXTCON_NONE] = "NONE",
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090043
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090044 /* USB external connector */
Chanwoo Choi11eecf92015-10-03 14:15:13 +090045 [EXTCON_USB] = "USB",
46 [EXTCON_USB_HOST] = "USB-HOST",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090047
Chanwoo Choi11eecf92015-10-03 14:15:13 +090048 /* Charging external connector */
49 [EXTCON_CHG_USB_SDP] = "SDP",
50 [EXTCON_CHG_USB_DCP] = "DCP",
51 [EXTCON_CHG_USB_CDP] = "CDP",
52 [EXTCON_CHG_USB_ACA] = "ACA",
53 [EXTCON_CHG_USB_FAST] = "FAST-CHARGER",
54 [EXTCON_CHG_USB_SLOW] = "SLOW-CHARGER",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090055
Chanwoo Choi11eecf92015-10-03 14:15:13 +090056 /* Jack external connector */
57 [EXTCON_JACK_MICROPHONE] = "MICROPHONE",
58 [EXTCON_JACK_HEADPHONE] = "HEADPHONE",
59 [EXTCON_JACK_LINE_IN] = "LINE-IN",
60 [EXTCON_JACK_LINE_OUT] = "LINE-OUT",
61 [EXTCON_JACK_VIDEO_IN] = "VIDEO-IN",
62 [EXTCON_JACK_VIDEO_OUT] = "VIDEO-OUT",
63 [EXTCON_JACK_SPDIF_IN] = "SPDIF-IN",
64 [EXTCON_JACK_SPDIF_OUT] = "SPDIF-OUT",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090065
Chanwoo Choi11eecf92015-10-03 14:15:13 +090066 /* Display external connector */
67 [EXTCON_DISP_HDMI] = "HDMI",
68 [EXTCON_DISP_MHL] = "MHL",
69 [EXTCON_DISP_DVI] = "DVI",
70 [EXTCON_DISP_VGA] = "VGA",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090071
Chanwoo Choi11eecf92015-10-03 14:15:13 +090072 /* Miscellaneous external connector */
73 [EXTCON_DOCK] = "DOCK",
74 [EXTCON_JIG] = "JIG",
75 [EXTCON_MECHANICAL] = "MECHANICAL",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090076
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090077 NULL,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090078};
79
Chanwoo Choi20f7b532016-06-27 19:17:06 +090080/**
81 * struct extcon_cable - An internal data for each cable of extcon device.
82 * @edev: The extcon device
83 * @cable_index: Index of this cable in the edev
84 * @attr_g: Attribute group for the cable
85 * @attr_name: "name" sysfs entry
86 * @attr_state: "state" sysfs entry
87 * @attrs: Array pointing to attr_name and attr_state for attr_g
88 */
89struct extcon_cable {
90 struct extcon_dev *edev;
91 int cable_index;
92
93 struct attribute_group attr_g;
94 struct device_attribute attr_name;
95 struct device_attribute attr_state;
96
97 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
98};
99
Mark Brownbe3a07f2012-06-05 16:14:38 +0100100static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900101#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900102static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900103#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900104
Donggeun Kim74c5d092012-04-20 14:16:24 +0900105static LIST_HEAD(extcon_dev_list);
106static DEFINE_MUTEX(extcon_dev_list_lock);
107
MyungJoo Hambde68e62012-04-20 14:16:26 +0900108/**
109 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900110 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +0900111 * @edev: the extcon device
112 * @new_state: new cable attach status for @edev
113 *
114 * Returns 0 if nothing violates. Returns the index + 1 for the first
115 * violated condition.
116 */
117static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
118{
119 int i = 0;
120
121 if (!edev->mutually_exclusive)
122 return 0;
123
124 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530125 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900126 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900127
anish kumar28c0ada2012-08-30 00:35:10 +0530128 /* calculate the total number of bits set */
129 weight = hweight32(correspondants);
130 if (weight > 1)
131 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900132 }
133
134 return 0;
135}
136
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900137static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900138{
139 int i;
140
141 /* Find the the index of extcon cable in edev->supported_cable */
142 for (i = 0; i < edev->max_supported; i++) {
143 if (edev->supported_cable[i] == id)
144 return i;
145 }
146
147 return -EINVAL;
148}
149
Chanwoo Choi046050f2015-05-19 20:01:12 +0900150static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
151{
152 if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
Hans de Goedef4513b02015-08-24 00:35:36 +0200153 *attached = ((new >> idx) & 0x1) ? true : false;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900154 return true;
155 }
156
157 return false;
158}
159
MyungJoo Hamde55d872012-04-20 14:16:22 +0900160static ssize_t state_show(struct device *dev, struct device_attribute *attr,
161 char *buf)
162{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900163 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900164 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900165
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900166 if (edev->max_supported == 0)
167 return sprintf(buf, "%u\n", edev->state);
168
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900169 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900170 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900171 extcon_name[edev->supported_cable[i]],
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900172 !!(edev->state & (1 << i)));
173 }
174
175 return count;
176}
177
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900178static ssize_t state_store(struct device *dev, struct device_attribute *attr,
179 const char *buf, size_t count)
180{
181 u32 state;
182 ssize_t ret = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900183 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900184
185 ret = sscanf(buf, "0x%x", &state);
186 if (ret == 0)
187 ret = -EINVAL;
188 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900189 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900190
191 if (ret < 0)
192 return ret;
193
194 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900195}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700196static DEVICE_ATTR_RW(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900197
198static ssize_t name_show(struct device *dev, struct device_attribute *attr,
199 char *buf)
200{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900201 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900202
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900203 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900204}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700205static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900206
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900207static ssize_t cable_name_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
211 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900212 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900213
214 return sprintf(buf, "%s\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900215 extcon_name[cable->edev->supported_cable[i]]);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900216}
217
218static ssize_t cable_state_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
220{
221 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
222 attr_state);
223
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300224 int i = cable->cable_index;
225
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900226 return sprintf(buf, "%d\n",
227 extcon_get_cable_state_(cable->edev,
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300228 cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900229}
230
MyungJoo Hamde55d872012-04-20 14:16:22 +0900231/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900232 * extcon_update_state() - Update the cable attach states of the extcon device
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900233 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900234 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900235 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900236 * @state: new cable attach status for @edev
237 *
238 * Changing the state sends uevent with environment variable containing
239 * the name of extcon device (envp[0]) and the state output (envp[1]).
240 * Tizen uses this format for extcon device to get events from ports.
241 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900242 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900243 * Note that the notifier provides which bits are changed in the state
244 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900245 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900246int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900247{
248 char name_buf[120];
249 char state_buf[120];
250 char *prop_buf;
251 char *envp[3];
252 int env_offset = 0;
253 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900254 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900255 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900256 bool attached;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900257
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900258 if (!edev)
259 return -EINVAL;
260
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900261 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900262
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900263 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
Roger Quadrosf7a89812015-07-06 17:46:58 +0300264 u32 old_state;
265
MyungJoo Hambde68e62012-04-20 14:16:26 +0900266 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
267 (state & mask))) {
268 spin_unlock_irqrestore(&edev->lock, flags);
269 return -EPERM;
270 }
271
Roger Quadrosf7a89812015-07-06 17:46:58 +0300272 old_state = edev->state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900273 edev->state &= ~mask;
274 edev->state |= state & mask;
275
Roger Quadrosf7a89812015-07-06 17:46:58 +0300276 for (index = 0; index < edev->max_supported; index++) {
277 if (is_extcon_changed(old_state, edev->state, index,
278 &attached))
279 raw_notifier_call_chain(&edev->nh[index],
280 attached, edev);
281 }
282
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900283 /* This could be in interrupt handler */
284 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900285 if (prop_buf) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900286 length = name_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900287 if (length > 0) {
288 if (prop_buf[length - 1] == '\n')
289 prop_buf[length - 1] = 0;
290 snprintf(name_buf, sizeof(name_buf),
291 "NAME=%s", prop_buf);
292 envp[env_offset++] = name_buf;
293 }
Chanwoo Choidae61652013-09-27 09:19:40 +0900294 length = state_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900295 if (length > 0) {
296 if (prop_buf[length - 1] == '\n')
297 prop_buf[length - 1] = 0;
298 snprintf(state_buf, sizeof(state_buf),
299 "STATE=%s", prop_buf);
300 envp[env_offset++] = state_buf;
301 }
302 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900303 /* Unlock early before uevent */
304 spin_unlock_irqrestore(&edev->lock, flags);
305
Chanwoo Choidae61652013-09-27 09:19:40 +0900306 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900307 free_page((unsigned long)prop_buf);
308 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900309 /* Unlock early before uevent */
310 spin_unlock_irqrestore(&edev->lock, flags);
311
Chanwoo Choidae61652013-09-27 09:19:40 +0900312 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
313 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900314 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900315 } else {
316 /* No changes */
317 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900318 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900319
320 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900321}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900322EXPORT_SYMBOL_GPL(extcon_update_state);
323
324/**
325 * extcon_set_state() - Set the cable attach states of the extcon device.
326 * @edev: the extcon device
327 * @state: new cable attach status for @edev
328 *
329 * Note that notifier provides which bits are changed in the state
330 * variable with the val parameter (second) to the callback.
331 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900332int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900333{
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900334 if (!edev)
335 return -EINVAL;
336
MyungJoo Hambde68e62012-04-20 14:16:26 +0900337 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900338}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900339EXPORT_SYMBOL_GPL(extcon_set_state);
340
Donggeun Kim74c5d092012-04-20 14:16:24 +0900341/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900342 * extcon_get_cable_state_() - Get the status of a specific cable.
343 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900344 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900345 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900346int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900347{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900348 int index;
349
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900350 if (!edev)
351 return -EINVAL;
352
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900353 index = find_cable_index_by_id(edev, id);
354 if (index < 0)
355 return index;
356
357 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900358 return -EINVAL;
359
360 return !!(edev->state & (1 << index));
361}
362EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
363
364/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900365 * extcon_set_cable_state_() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900366 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900367 * @id: the unique id of each external connector
368 * in extcon enumeration.
369 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900370 * true: attached / false: detached.
371 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900372int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900373 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900374{
375 u32 state;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900376 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900377
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900378 if (!edev)
379 return -EINVAL;
380
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900381 index = find_cable_index_by_id(edev, id);
382 if (index < 0)
383 return index;
384
385 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900386 return -EINVAL;
387
388 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900389 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900390}
391EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
392
393/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900394 * extcon_get_extcon_dev() - Get the extcon device instance from the name
395 * @extcon_name: The extcon name provided with extcon_dev_register()
396 */
397struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
398{
399 struct extcon_dev *sd;
400
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900401 if (!extcon_name)
402 return ERR_PTR(-EINVAL);
403
Donggeun Kim74c5d092012-04-20 14:16:24 +0900404 mutex_lock(&extcon_dev_list_lock);
405 list_for_each_entry(sd, &extcon_dev_list, entry) {
406 if (!strcmp(sd->name, extcon_name))
407 goto out;
408 }
409 sd = NULL;
410out:
411 mutex_unlock(&extcon_dev_list_lock);
412 return sd;
413}
414EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
415
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900416/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900417 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900418 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900419 * @edev: the extcon device that has the external connecotr.
420 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900421 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900422 *
423 * Note that the second parameter given to the callback of nb (val) is
424 * "old_state", not the current state. The current state can be retrieved
425 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900426 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900427int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900428 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900429{
Hans de Goede66bee352015-03-21 17:26:24 +0100430 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900431 int ret, idx;
432
Chanwoo Choi830ae442016-05-31 17:32:30 +0900433 if (!nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900434 return -EINVAL;
435
Chanwoo Choi830ae442016-05-31 17:32:30 +0900436 if (edev) {
437 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900438 if (idx < 0)
439 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100440
Chanwoo Choi830ae442016-05-31 17:32:30 +0900441 spin_lock_irqsave(&edev->lock, flags);
442 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
443 spin_unlock_irqrestore(&edev->lock, flags);
444 } else {
445 struct extcon_dev *extd;
446
447 mutex_lock(&extcon_dev_list_lock);
448 list_for_each_entry(extd, &extcon_dev_list, entry) {
449 idx = find_cable_index_by_id(extd, id);
450 if (idx >= 0)
451 break;
452 }
453 mutex_unlock(&extcon_dev_list_lock);
454
455 if (idx >= 0) {
456 edev = extd;
457 return extcon_register_notifier(extd, id, nb);
458 } else {
459 ret = -ENODEV;
460 }
461 }
Hans de Goede66bee352015-03-21 17:26:24 +0100462
463 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900464}
465EXPORT_SYMBOL_GPL(extcon_register_notifier);
466
467/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900468 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900469 * @edev: the extcon device that has the external connecotr.
470 * @id: the unique id of each external connector in extcon enumeration.
471 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900472 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900473int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900474 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900475{
Hans de Goede66bee352015-03-21 17:26:24 +0100476 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900477 int ret, idx;
478
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900479 if (!edev || !nb)
480 return -EINVAL;
481
Chanwoo Choi046050f2015-05-19 20:01:12 +0900482 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900483 if (idx < 0)
484 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100485
486 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900487 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100488 spin_unlock_irqrestore(&edev->lock, flags);
489
490 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900491}
492EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
493
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700494static struct attribute *extcon_attrs[] = {
495 &dev_attr_state.attr,
496 &dev_attr_name.attr,
497 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900498};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700499ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900500
501static int create_extcon_class(void)
502{
503 if (!extcon_class) {
504 extcon_class = class_create(THIS_MODULE, "extcon");
505 if (IS_ERR(extcon_class))
506 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700507 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900508
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900509#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900510 switch_class = class_compat_register("switch");
511 if (WARN(!switch_class, "cannot allocate"))
512 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900513#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900514 }
515
516 return 0;
517}
518
MyungJoo Hamde55d872012-04-20 14:16:22 +0900519static void extcon_dev_release(struct device *dev)
520{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900521}
522
MyungJoo Hambde68e62012-04-20 14:16:26 +0900523static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900524static void dummy_sysfs_dev_release(struct device *dev)
525{
526}
527
Chanwoo Choia9af6522014-04-24 19:46:49 +0900528/*
529 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900530 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900531 * If supported_cable is NULL, cable name related APIs
532 * are disabled.
533 *
534 * This function allocates the memory for extcon device without allocating
535 * memory in each extcon provider driver and initialize default setting for
536 * extcon device.
537 *
538 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
539 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900540struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +0900541{
542 struct extcon_dev *edev;
543
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900544 if (!supported_cable)
545 return ERR_PTR(-EINVAL);
546
Chanwoo Choia9af6522014-04-24 19:46:49 +0900547 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
548 if (!edev)
549 return ERR_PTR(-ENOMEM);
550
551 edev->max_supported = 0;
552 edev->supported_cable = supported_cable;
553
554 return edev;
555}
556
557/*
558 * extcon_dev_free() - Free the memory of extcon device.
559 * @edev: the extcon device to free
560 */
561void extcon_dev_free(struct extcon_dev *edev)
562{
563 kfree(edev);
564}
565EXPORT_SYMBOL_GPL(extcon_dev_free);
566
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900567static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
568{
569 struct extcon_dev **r = res;
570
571 if (WARN_ON(!r || !*r))
572 return 0;
573
574 return *r == data;
575}
576
577static void devm_extcon_dev_release(struct device *dev, void *res)
578{
579 extcon_dev_free(*(struct extcon_dev **)res);
580}
581
582/**
583 * devm_extcon_dev_allocate - Allocate managed extcon device
584 * @dev: device owning the extcon device being created
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900585 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900586 * If supported_cable is NULL, cable name related APIs
587 * are disabled.
588 *
589 * This function manages automatically the memory of extcon device using device
590 * resource management and simplify the control of freeing the memory of extcon
591 * device.
592 *
593 * Returns the pointer memory of allocated extcon_dev if success
594 * or ERR_PTR(err) if fail
595 */
596struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900597 const unsigned int *supported_cable)
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900598{
599 struct extcon_dev **ptr, *edev;
600
601 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
602 if (!ptr)
603 return ERR_PTR(-ENOMEM);
604
605 edev = extcon_dev_allocate(supported_cable);
606 if (IS_ERR(edev)) {
607 devres_free(ptr);
608 return edev;
609 }
610
Chanwoo Choiac65a622014-05-30 10:13:15 +0900611 edev->dev.parent = dev;
612
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900613 *ptr = edev;
614 devres_add(dev, ptr);
615
616 return edev;
617}
618EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
619
620void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
621{
622 WARN_ON(devres_release(dev, devm_extcon_dev_release,
623 devm_extcon_dev_match, edev));
624}
625EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
626
MyungJoo Hamde55d872012-04-20 14:16:22 +0900627/**
628 * extcon_dev_register() - Register a new extcon device
629 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900630 *
631 * Among the members of edev struct, please set the "user initializing data"
632 * in any case and set the "optional callbacks" if required. However, please
633 * do not set the values of "internal data", which are initialized by
634 * this function.
635 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900636int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900637{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900638 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900639 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900640
641 if (!extcon_class) {
642 ret = create_extcon_class();
643 if (ret < 0)
644 return ret;
645 }
646
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900647 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900648 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900649
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900650 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
651
652 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900653 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900654 dev_err(&edev->dev,
655 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900656 return -EINVAL;
657 }
658
Chanwoo Choidae61652013-09-27 09:19:40 +0900659 edev->dev.class = extcon_class;
660 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900661
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900662 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900663 if (IS_ERR_OR_NULL(edev->name)) {
664 dev_err(&edev->dev,
665 "extcon device name is null\n");
666 return -EINVAL;
667 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900668 dev_set_name(&edev->dev, "extcon%lu",
669 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900670
671 if (edev->max_supported) {
672 char buf[10];
673 char *str;
674 struct extcon_cable *cable;
675
676 edev->cables = kzalloc(sizeof(struct extcon_cable) *
677 edev->max_supported, GFP_KERNEL);
678 if (!edev->cables) {
679 ret = -ENOMEM;
680 goto err_sysfs_alloc;
681 }
682 for (index = 0; index < edev->max_supported; index++) {
683 cable = &edev->cables[index];
684
685 snprintf(buf, 10, "cable.%d", index);
686 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
687 GFP_KERNEL);
688 if (!str) {
689 for (index--; index >= 0; index--) {
690 cable = &edev->cables[index];
691 kfree(cable->attr_g.name);
692 }
693 ret = -ENOMEM;
694
695 goto err_alloc_cables;
696 }
697 strcpy(str, buf);
698
699 cable->edev = edev;
700 cable->cable_index = index;
701 cable->attrs[0] = &cable->attr_name.attr;
702 cable->attrs[1] = &cable->attr_state.attr;
703 cable->attrs[2] = NULL;
704 cable->attr_g.name = str;
705 cable->attr_g.attrs = cable->attrs;
706
Mark Brown9baf3222012-08-16 20:03:21 +0100707 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900708 cable->attr_name.attr.name = "name";
709 cable->attr_name.attr.mode = 0444;
710 cable->attr_name.show = cable_name_show;
711
Mark Brown9baf3222012-08-16 20:03:21 +0100712 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900713 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900714 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900715 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900716 }
717 }
718
MyungJoo Hambde68e62012-04-20 14:16:26 +0900719 if (edev->max_supported && edev->mutually_exclusive) {
720 char buf[80];
721 char *name;
722
723 /* Count the size of mutually_exclusive array */
724 for (index = 0; edev->mutually_exclusive[index]; index++)
725 ;
726
727 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
728 (index + 1), GFP_KERNEL);
729 if (!edev->attrs_muex) {
730 ret = -ENOMEM;
731 goto err_muex;
732 }
733
734 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
735 index, GFP_KERNEL);
736 if (!edev->d_attrs_muex) {
737 ret = -ENOMEM;
738 kfree(edev->attrs_muex);
739 goto err_muex;
740 }
741
742 for (index = 0; edev->mutually_exclusive[index]; index++) {
743 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
744 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
745 GFP_KERNEL);
746 if (!name) {
747 for (index--; index >= 0; index--) {
748 kfree(edev->d_attrs_muex[index].attr.
749 name);
750 }
751 kfree(edev->d_attrs_muex);
752 kfree(edev->attrs_muex);
753 ret = -ENOMEM;
754 goto err_muex;
755 }
756 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100757 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900758 edev->d_attrs_muex[index].attr.name = name;
759 edev->d_attrs_muex[index].attr.mode = 0000;
760 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
761 .attr;
762 }
763 edev->attr_g_muex.name = muex_name;
764 edev->attr_g_muex.attrs = edev->attrs_muex;
765
766 }
767
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900768 if (edev->max_supported) {
769 edev->extcon_dev_type.groups =
770 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900771 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900772 if (!edev->extcon_dev_type.groups) {
773 ret = -ENOMEM;
774 goto err_alloc_groups;
775 }
776
Chanwoo Choidae61652013-09-27 09:19:40 +0900777 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900778 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
779
780 for (index = 0; index < edev->max_supported; index++)
781 edev->extcon_dev_type.groups[index] =
782 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900783 if (edev->mutually_exclusive)
784 edev->extcon_dev_type.groups[index] =
785 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900786
Chanwoo Choidae61652013-09-27 09:19:40 +0900787 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900788 }
789
Chanwoo Choidae61652013-09-27 09:19:40 +0900790 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900791 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900792 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900793 goto err_dev;
794 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900795#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900796 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900797 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900798#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900799
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900800 spin_lock_init(&edev->lock);
801
Chanwoo Choi046050f2015-05-19 20:01:12 +0900802 edev->nh = devm_kzalloc(&edev->dev,
803 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
804 if (!edev->nh) {
805 ret = -ENOMEM;
806 goto err_dev;
807 }
808
809 for (index = 0; index < edev->max_supported; index++)
810 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900811
Chanwoo Choidae61652013-09-27 09:19:40 +0900812 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900813 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900814
815 mutex_lock(&extcon_dev_list_lock);
816 list_add(&edev->entry, &extcon_dev_list);
817 mutex_unlock(&extcon_dev_list_lock);
818
MyungJoo Hamde55d872012-04-20 14:16:22 +0900819 return 0;
820
821err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900822 if (edev->max_supported)
823 kfree(edev->extcon_dev_type.groups);
824err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900825 if (edev->max_supported && edev->mutually_exclusive) {
826 for (index = 0; edev->mutually_exclusive[index]; index++)
827 kfree(edev->d_attrs_muex[index].attr.name);
828 kfree(edev->d_attrs_muex);
829 kfree(edev->attrs_muex);
830 }
831err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900832 for (index = 0; index < edev->max_supported; index++)
833 kfree(edev->cables[index].attr_g.name);
834err_alloc_cables:
835 if (edev->max_supported)
836 kfree(edev->cables);
837err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900838 return ret;
839}
840EXPORT_SYMBOL_GPL(extcon_dev_register);
841
842/**
843 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900844 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900845 *
846 * Note that this does not call kfree(edev) because edev was not allocated
847 * by this class.
848 */
849void extcon_dev_unregister(struct extcon_dev *edev)
850{
anish kumar57e7cd32012-10-22 09:43:33 +0900851 int index;
852
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900853 if (!edev)
854 return;
855
anish kumar57e7cd32012-10-22 09:43:33 +0900856 mutex_lock(&extcon_dev_list_lock);
857 list_del(&edev->entry);
858 mutex_unlock(&extcon_dev_list_lock);
859
Chanwoo Choidae61652013-09-27 09:19:40 +0900860 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
861 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
862 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +0900863 return;
864 }
865
Wang, Xiaoming7585ca02013-11-01 18:48:14 -0400866 device_unregister(&edev->dev);
867
anish kumar57e7cd32012-10-22 09:43:33 +0900868 if (edev->mutually_exclusive && edev->max_supported) {
869 for (index = 0; edev->mutually_exclusive[index];
870 index++)
871 kfree(edev->d_attrs_muex[index].attr.name);
872 kfree(edev->d_attrs_muex);
873 kfree(edev->attrs_muex);
874 }
875
876 for (index = 0; index < edev->max_supported; index++)
877 kfree(edev->cables[index].attr_g.name);
878
879 if (edev->max_supported) {
880 kfree(edev->extcon_dev_type.groups);
881 kfree(edev->cables);
882 }
883
884#if defined(CONFIG_ANDROID)
885 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900886 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +0900887#endif
Chanwoo Choidae61652013-09-27 09:19:40 +0900888 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900889}
890EXPORT_SYMBOL_GPL(extcon_dev_unregister);
891
Sangjung Woo11112442014-04-21 19:10:08 +0900892static void devm_extcon_dev_unreg(struct device *dev, void *res)
893{
894 extcon_dev_unregister(*(struct extcon_dev **)res);
895}
896
Sangjung Woo11112442014-04-21 19:10:08 +0900897/**
898 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
899 * @dev: device to allocate extcon device
900 * @edev: the new extcon device to register
901 *
902 * Managed extcon_dev_register() function. If extcon device is attached with
903 * this function, that extcon device is automatically unregistered on driver
904 * detach. Internally this function calls extcon_dev_register() function.
905 * To get more information, refer that function.
906 *
907 * If extcon device is registered with this function and the device needs to be
908 * unregistered separately, devm_extcon_dev_unregister() should be used.
909 *
910 * Returns 0 if success or negaive error number if failure.
911 */
912int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
913{
914 struct extcon_dev **ptr;
915 int ret;
916
917 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
918 if (!ptr)
919 return -ENOMEM;
920
921 ret = extcon_dev_register(edev);
922 if (ret) {
923 devres_free(ptr);
924 return ret;
925 }
926
927 *ptr = edev;
928 devres_add(dev, ptr);
929
930 return 0;
931}
932EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
933
934/**
935 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
936 * @dev: device the extcon belongs to
937 * @edev: the extcon device to unregister
938 *
939 * Unregister extcon device that is registered with devm_extcon_dev_register()
940 * function.
941 */
942void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
943{
944 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
945 devm_extcon_dev_match, edev));
946}
947EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
948
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900949#ifdef CONFIG_OF
950/*
951 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
952 * @dev - instance to the given device
953 * @index - index into list of extcon_dev
954 *
955 * return the instance of extcon device
956 */
957struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
958{
959 struct device_node *node;
960 struct extcon_dev *edev;
961
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900962 if (!dev)
963 return ERR_PTR(-EINVAL);
964
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900965 if (!dev->of_node) {
966 dev_err(dev, "device does not have a device node entry\n");
967 return ERR_PTR(-EINVAL);
968 }
969
970 node = of_parse_phandle(dev->of_node, "extcon", index);
971 if (!node) {
972 dev_err(dev, "failed to get phandle in %s node\n",
973 dev->of_node->full_name);
974 return ERR_PTR(-ENODEV);
975 }
976
Tomasz Figaf841afb2014-10-16 15:11:44 +0200977 mutex_lock(&extcon_dev_list_lock);
978 list_for_each_entry(edev, &extcon_dev_list, entry) {
979 if (edev->dev.parent && edev->dev.parent->of_node == node) {
980 mutex_unlock(&extcon_dev_list_lock);
981 return edev;
982 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900983 }
Tomasz Figaf841afb2014-10-16 15:11:44 +0200984 mutex_unlock(&extcon_dev_list_lock);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900985
Tomasz Figaf841afb2014-10-16 15:11:44 +0200986 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900987}
988#else
989struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
990{
991 return ERR_PTR(-ENOSYS);
992}
993#endif /* CONFIG_OF */
994EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
995
Chanwoo Choi707d7552015-04-15 13:57:51 +0900996/**
997 * extcon_get_edev_name() - Get the name of the extcon device.
998 * @edev: the extcon device
999 */
1000const char *extcon_get_edev_name(struct extcon_dev *edev)
1001{
1002 return !edev ? NULL : edev->name;
1003}
1004
MyungJoo Hamde55d872012-04-20 14:16:22 +09001005static int __init extcon_class_init(void)
1006{
1007 return create_extcon_class();
1008}
1009module_init(extcon_class_init);
1010
1011static void __exit extcon_class_exit(void)
1012{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001013#if defined(CONFIG_ANDROID)
1014 class_compat_unregister(switch_class);
1015#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001016 class_destroy(extcon_class);
1017}
1018module_exit(extcon_class_exit);
1019
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001020MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001021MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1022MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1023MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1024MODULE_DESCRIPTION("External connector (extcon) class driver");
1025MODULE_LICENSE("GPL");