blob: b6408f0156371e98ba7bd723a5342bac2b64487a [file] [log] [blame]
MyungJoo Hamde55d872012-04-20 14:16:22 +09001/*
Chanwoo Choib9ec23c2015-04-24 14:48:52 +09002 * drivers/extcon/extcon.c - External Connector (extcon) framework.
MyungJoo Hamde55d872012-04-20 14:16:22 +09003 *
4 * External connector (extcon) class driver
5 *
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09006 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 *
MyungJoo Hamde55d872012-04-20 14:16:22 +09009 * Copyright (C) 2012 Samsung Electronics
10 * Author: Donggeun Kim <dg77.kim@samsung.com>
11 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
12 *
13 * based on android/drivers/switch/switch_class.c
14 * Copyright (C) 2008 Google, Inc.
15 * Author: Mike Lockwood <lockwood@android.com>
16 *
17 * This software is licensed under the terms of the GNU General Public
18 * License version 2, as published by the Free Software Foundation, and
19 * may be copied, distributed, and modified under those terms.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
Chanwoo Choib9ec23c2015-04-24 14:48:52 +090025 */
MyungJoo Hamde55d872012-04-20 14:16:22 +090026
27#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/init.h>
30#include <linux/device.h>
31#include <linux/fs.h>
32#include <linux/err.h>
33#include <linux/extcon.h>
Tomasz Figaf841afb12014-10-16 15:11:44 +020034#include <linux/of.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090035#include <linux/slab.h>
Mark Brown9baf3222012-08-16 20:03:21 +010036#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090037
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090038#define SUPPORTED_CABLE_MAX 32
39#define CABLE_NAME_MAX 30
40
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
Mark Brownbe3a07f2012-06-05 16:14:38 +010080static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090081#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +090082static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090083#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +090084
Donggeun Kim74c5d092012-04-20 14:16:24 +090085static LIST_HEAD(extcon_dev_list);
86static DEFINE_MUTEX(extcon_dev_list_lock);
87
MyungJoo Hambde68e62012-04-20 14:16:26 +090088/**
89 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +090090 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +090091 * @edev: the extcon device
92 * @new_state: new cable attach status for @edev
93 *
94 * Returns 0 if nothing violates. Returns the index + 1 for the first
95 * violated condition.
96 */
97static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
98{
99 int i = 0;
100
101 if (!edev->mutually_exclusive)
102 return 0;
103
104 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530105 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900106 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900107
anish kumar28c0ada2012-08-30 00:35:10 +0530108 /* calculate the total number of bits set */
109 weight = hweight32(correspondants);
110 if (weight > 1)
111 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900112 }
113
114 return 0;
115}
116
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900117static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900118{
119 int i;
120
121 /* Find the the index of extcon cable in edev->supported_cable */
122 for (i = 0; i < edev->max_supported; i++) {
123 if (edev->supported_cable[i] == id)
124 return i;
125 }
126
127 return -EINVAL;
128}
129
Chanwoo Choi046050f2015-05-19 20:01:12 +0900130static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
131{
132 if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
Hans de Goedef4513b02015-08-24 00:35:36 +0200133 *attached = ((new >> idx) & 0x1) ? true : false;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900134 return true;
135 }
136
137 return false;
138}
139
MyungJoo Hamde55d872012-04-20 14:16:22 +0900140static ssize_t state_show(struct device *dev, struct device_attribute *attr,
141 char *buf)
142{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900143 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900144 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900145
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900146 if (edev->max_supported == 0)
147 return sprintf(buf, "%u\n", edev->state);
148
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900149 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900150 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900151 extcon_name[edev->supported_cable[i]],
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900152 !!(edev->state & (1 << i)));
153 }
154
155 return count;
156}
157
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900158static ssize_t state_store(struct device *dev, struct device_attribute *attr,
159 const char *buf, size_t count)
160{
161 u32 state;
162 ssize_t ret = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900163 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900164
165 ret = sscanf(buf, "0x%x", &state);
166 if (ret == 0)
167 ret = -EINVAL;
168 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900169 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900170
171 if (ret < 0)
172 return ret;
173
174 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900175}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700176static DEVICE_ATTR_RW(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900177
178static ssize_t name_show(struct device *dev, struct device_attribute *attr,
179 char *buf)
180{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900181 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900182
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900183 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900184}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700185static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900186
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900187static ssize_t cable_name_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
191 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900192 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900193
194 return sprintf(buf, "%s\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900195 extcon_name[cable->edev->supported_cable[i]]);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900196}
197
198static ssize_t cable_state_show(struct device *dev,
199 struct device_attribute *attr, char *buf)
200{
201 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
202 attr_state);
203
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300204 int i = cable->cable_index;
205
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900206 return sprintf(buf, "%d\n",
207 extcon_get_cable_state_(cable->edev,
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300208 cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900209}
210
MyungJoo Hamde55d872012-04-20 14:16:22 +0900211/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900212 * extcon_update_state() - Update the cable attach states of the extcon device
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900213 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900214 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900215 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900216 * @state: new cable attach status for @edev
217 *
218 * Changing the state sends uevent with environment variable containing
219 * the name of extcon device (envp[0]) and the state output (envp[1]).
220 * Tizen uses this format for extcon device to get events from ports.
221 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900222 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900223 * Note that the notifier provides which bits are changed in the state
224 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900225 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900226int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900227{
228 char name_buf[120];
229 char state_buf[120];
230 char *prop_buf;
231 char *envp[3];
232 int env_offset = 0;
233 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900234 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900235 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900236 bool attached;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900237
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900238 if (!edev)
239 return -EINVAL;
240
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900241 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900242
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900243 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
Roger Quadrosf7a89812015-07-06 17:46:58 +0300244 u32 old_state;
245
MyungJoo Hambde68e62012-04-20 14:16:26 +0900246 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
247 (state & mask))) {
248 spin_unlock_irqrestore(&edev->lock, flags);
249 return -EPERM;
250 }
251
Roger Quadrosf7a89812015-07-06 17:46:58 +0300252 old_state = edev->state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900253 edev->state &= ~mask;
254 edev->state |= state & mask;
255
Roger Quadrosf7a89812015-07-06 17:46:58 +0300256 for (index = 0; index < edev->max_supported; index++) {
257 if (is_extcon_changed(old_state, edev->state, index,
258 &attached))
259 raw_notifier_call_chain(&edev->nh[index],
260 attached, edev);
261 }
262
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900263 /* This could be in interrupt handler */
264 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900265 if (prop_buf) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900266 length = name_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900267 if (length > 0) {
268 if (prop_buf[length - 1] == '\n')
269 prop_buf[length - 1] = 0;
270 snprintf(name_buf, sizeof(name_buf),
271 "NAME=%s", prop_buf);
272 envp[env_offset++] = name_buf;
273 }
Chanwoo Choidae61652013-09-27 09:19:40 +0900274 length = state_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900275 if (length > 0) {
276 if (prop_buf[length - 1] == '\n')
277 prop_buf[length - 1] = 0;
278 snprintf(state_buf, sizeof(state_buf),
279 "STATE=%s", prop_buf);
280 envp[env_offset++] = state_buf;
281 }
282 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900283 /* Unlock early before uevent */
284 spin_unlock_irqrestore(&edev->lock, flags);
285
Chanwoo Choidae61652013-09-27 09:19:40 +0900286 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900287 free_page((unsigned long)prop_buf);
288 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900289 /* Unlock early before uevent */
290 spin_unlock_irqrestore(&edev->lock, flags);
291
Chanwoo Choidae61652013-09-27 09:19:40 +0900292 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
293 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900294 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900295 } else {
296 /* No changes */
297 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900298 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900299
300 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900301}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900302EXPORT_SYMBOL_GPL(extcon_update_state);
303
304/**
305 * extcon_set_state() - Set the cable attach states of the extcon device.
306 * @edev: the extcon device
307 * @state: new cable attach status for @edev
308 *
309 * Note that notifier provides which bits are changed in the state
310 * variable with the val parameter (second) to the callback.
311 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900312int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900313{
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900314 if (!edev)
315 return -EINVAL;
316
MyungJoo Hambde68e62012-04-20 14:16:26 +0900317 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900318}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900319EXPORT_SYMBOL_GPL(extcon_set_state);
320
Donggeun Kim74c5d092012-04-20 14:16:24 +0900321/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900322 * extcon_get_cable_state_() - Get the status of a specific cable.
323 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900324 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900325 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900326int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900327{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900328 int index;
329
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900330 if (!edev)
331 return -EINVAL;
332
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900333 index = find_cable_index_by_id(edev, id);
334 if (index < 0)
335 return index;
336
337 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900338 return -EINVAL;
339
340 return !!(edev->state & (1 << index));
341}
342EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
343
344/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900345 * extcon_set_cable_state_() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900346 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900347 * @id: the unique id of each external connector
348 * in extcon enumeration.
349 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900350 * true: attached / false: detached.
351 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900352int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900353 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900354{
355 u32 state;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900356 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900357
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900358 if (!edev)
359 return -EINVAL;
360
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900361 index = find_cable_index_by_id(edev, id);
362 if (index < 0)
363 return index;
364
365 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900366 return -EINVAL;
367
368 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900369 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900370}
371EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
372
373/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900374 * extcon_get_extcon_dev() - Get the extcon device instance from the name
375 * @extcon_name: The extcon name provided with extcon_dev_register()
376 */
377struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
378{
379 struct extcon_dev *sd;
380
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900381 if (!extcon_name)
382 return ERR_PTR(-EINVAL);
383
Donggeun Kim74c5d092012-04-20 14:16:24 +0900384 mutex_lock(&extcon_dev_list_lock);
385 list_for_each_entry(sd, &extcon_dev_list, entry) {
386 if (!strcmp(sd->name, extcon_name))
387 goto out;
388 }
389 sd = NULL;
390out:
391 mutex_unlock(&extcon_dev_list_lock);
392 return sd;
393}
394EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
395
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900396/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900397 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900398 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900399 * @edev: the extcon device that has the external connecotr.
400 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900401 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900402 *
403 * Note that the second parameter given to the callback of nb (val) is
404 * "old_state", not the current state. The current state can be retrieved
405 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900406 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900407int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900408 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900409{
Hans de Goede66bee352015-03-21 17:26:24 +0100410 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900411 int ret, idx;
412
Chanwoo Choi830ae442016-05-31 17:32:30 +0900413 if (!nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900414 return -EINVAL;
415
Chanwoo Choi830ae442016-05-31 17:32:30 +0900416 if (edev) {
417 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900418 if (idx < 0)
419 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100420
Chanwoo Choi830ae442016-05-31 17:32:30 +0900421 spin_lock_irqsave(&edev->lock, flags);
422 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
423 spin_unlock_irqrestore(&edev->lock, flags);
424 } else {
425 struct extcon_dev *extd;
426
427 mutex_lock(&extcon_dev_list_lock);
428 list_for_each_entry(extd, &extcon_dev_list, entry) {
429 idx = find_cable_index_by_id(extd, id);
430 if (idx >= 0)
431 break;
432 }
433 mutex_unlock(&extcon_dev_list_lock);
434
435 if (idx >= 0) {
436 edev = extd;
437 return extcon_register_notifier(extd, id, nb);
438 } else {
439 ret = -ENODEV;
440 }
441 }
Hans de Goede66bee352015-03-21 17:26:24 +0100442
443 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900444}
445EXPORT_SYMBOL_GPL(extcon_register_notifier);
446
447/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900448 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900449 * @edev: the extcon device that has the external connecotr.
450 * @id: the unique id of each external connector in extcon enumeration.
451 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900452 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900453int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900454 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900455{
Hans de Goede66bee352015-03-21 17:26:24 +0100456 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900457 int ret, idx;
458
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900459 if (!edev || !nb)
460 return -EINVAL;
461
Chanwoo Choi046050f2015-05-19 20:01:12 +0900462 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900463 if (idx < 0)
464 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100465
466 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900467 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100468 spin_unlock_irqrestore(&edev->lock, flags);
469
470 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900471}
472EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
473
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700474static struct attribute *extcon_attrs[] = {
475 &dev_attr_state.attr,
476 &dev_attr_name.attr,
477 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900478};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700479ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900480
481static int create_extcon_class(void)
482{
483 if (!extcon_class) {
484 extcon_class = class_create(THIS_MODULE, "extcon");
485 if (IS_ERR(extcon_class))
486 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700487 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900488
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900489#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900490 switch_class = class_compat_register("switch");
491 if (WARN(!switch_class, "cannot allocate"))
492 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900493#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900494 }
495
496 return 0;
497}
498
MyungJoo Hamde55d872012-04-20 14:16:22 +0900499static void extcon_dev_release(struct device *dev)
500{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900501}
502
MyungJoo Hambde68e62012-04-20 14:16:26 +0900503static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900504static void dummy_sysfs_dev_release(struct device *dev)
505{
506}
507
Chanwoo Choia9af6522014-04-24 19:46:49 +0900508/*
509 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900510 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900511 * If supported_cable is NULL, cable name related APIs
512 * are disabled.
513 *
514 * This function allocates the memory for extcon device without allocating
515 * memory in each extcon provider driver and initialize default setting for
516 * extcon device.
517 *
518 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
519 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900520struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +0900521{
522 struct extcon_dev *edev;
523
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900524 if (!supported_cable)
525 return ERR_PTR(-EINVAL);
526
Chanwoo Choia9af6522014-04-24 19:46:49 +0900527 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
528 if (!edev)
529 return ERR_PTR(-ENOMEM);
530
531 edev->max_supported = 0;
532 edev->supported_cable = supported_cable;
533
534 return edev;
535}
536
537/*
538 * extcon_dev_free() - Free the memory of extcon device.
539 * @edev: the extcon device to free
540 */
541void extcon_dev_free(struct extcon_dev *edev)
542{
543 kfree(edev);
544}
545EXPORT_SYMBOL_GPL(extcon_dev_free);
546
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900547static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
548{
549 struct extcon_dev **r = res;
550
551 if (WARN_ON(!r || !*r))
552 return 0;
553
554 return *r == data;
555}
556
557static void devm_extcon_dev_release(struct device *dev, void *res)
558{
559 extcon_dev_free(*(struct extcon_dev **)res);
560}
561
562/**
563 * devm_extcon_dev_allocate - Allocate managed extcon device
564 * @dev: device owning the extcon device being created
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900565 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900566 * If supported_cable is NULL, cable name related APIs
567 * are disabled.
568 *
569 * This function manages automatically the memory of extcon device using device
570 * resource management and simplify the control of freeing the memory of extcon
571 * device.
572 *
573 * Returns the pointer memory of allocated extcon_dev if success
574 * or ERR_PTR(err) if fail
575 */
576struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900577 const unsigned int *supported_cable)
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900578{
579 struct extcon_dev **ptr, *edev;
580
581 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
582 if (!ptr)
583 return ERR_PTR(-ENOMEM);
584
585 edev = extcon_dev_allocate(supported_cable);
586 if (IS_ERR(edev)) {
587 devres_free(ptr);
588 return edev;
589 }
590
Chanwoo Choiac65a622014-05-30 10:13:15 +0900591 edev->dev.parent = dev;
592
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900593 *ptr = edev;
594 devres_add(dev, ptr);
595
596 return edev;
597}
598EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
599
600void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
601{
602 WARN_ON(devres_release(dev, devm_extcon_dev_release,
603 devm_extcon_dev_match, edev));
604}
605EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
606
MyungJoo Hamde55d872012-04-20 14:16:22 +0900607/**
608 * extcon_dev_register() - Register a new extcon device
609 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900610 *
611 * Among the members of edev struct, please set the "user initializing data"
612 * in any case and set the "optional callbacks" if required. However, please
613 * do not set the values of "internal data", which are initialized by
614 * this function.
615 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900616int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900617{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900618 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900619 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900620
621 if (!extcon_class) {
622 ret = create_extcon_class();
623 if (ret < 0)
624 return ret;
625 }
626
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900627 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900628 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900629
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900630 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
631
632 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900633 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900634 dev_err(&edev->dev,
635 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900636 return -EINVAL;
637 }
638
Chanwoo Choidae61652013-09-27 09:19:40 +0900639 edev->dev.class = extcon_class;
640 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900641
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900642 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900643 if (IS_ERR_OR_NULL(edev->name)) {
644 dev_err(&edev->dev,
645 "extcon device name is null\n");
646 return -EINVAL;
647 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900648 dev_set_name(&edev->dev, "extcon%lu",
649 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900650
651 if (edev->max_supported) {
652 char buf[10];
653 char *str;
654 struct extcon_cable *cable;
655
656 edev->cables = kzalloc(sizeof(struct extcon_cable) *
657 edev->max_supported, GFP_KERNEL);
658 if (!edev->cables) {
659 ret = -ENOMEM;
660 goto err_sysfs_alloc;
661 }
662 for (index = 0; index < edev->max_supported; index++) {
663 cable = &edev->cables[index];
664
665 snprintf(buf, 10, "cable.%d", index);
666 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
667 GFP_KERNEL);
668 if (!str) {
669 for (index--; index >= 0; index--) {
670 cable = &edev->cables[index];
671 kfree(cable->attr_g.name);
672 }
673 ret = -ENOMEM;
674
675 goto err_alloc_cables;
676 }
677 strcpy(str, buf);
678
679 cable->edev = edev;
680 cable->cable_index = index;
681 cable->attrs[0] = &cable->attr_name.attr;
682 cable->attrs[1] = &cable->attr_state.attr;
683 cable->attrs[2] = NULL;
684 cable->attr_g.name = str;
685 cable->attr_g.attrs = cable->attrs;
686
Mark Brown9baf3222012-08-16 20:03:21 +0100687 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900688 cable->attr_name.attr.name = "name";
689 cable->attr_name.attr.mode = 0444;
690 cable->attr_name.show = cable_name_show;
691
Mark Brown9baf3222012-08-16 20:03:21 +0100692 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900693 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900694 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900695 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900696 }
697 }
698
MyungJoo Hambde68e62012-04-20 14:16:26 +0900699 if (edev->max_supported && edev->mutually_exclusive) {
700 char buf[80];
701 char *name;
702
703 /* Count the size of mutually_exclusive array */
704 for (index = 0; edev->mutually_exclusive[index]; index++)
705 ;
706
707 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
708 (index + 1), GFP_KERNEL);
709 if (!edev->attrs_muex) {
710 ret = -ENOMEM;
711 goto err_muex;
712 }
713
714 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
715 index, GFP_KERNEL);
716 if (!edev->d_attrs_muex) {
717 ret = -ENOMEM;
718 kfree(edev->attrs_muex);
719 goto err_muex;
720 }
721
722 for (index = 0; edev->mutually_exclusive[index]; index++) {
723 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
724 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
725 GFP_KERNEL);
726 if (!name) {
727 for (index--; index >= 0; index--) {
728 kfree(edev->d_attrs_muex[index].attr.
729 name);
730 }
731 kfree(edev->d_attrs_muex);
732 kfree(edev->attrs_muex);
733 ret = -ENOMEM;
734 goto err_muex;
735 }
736 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100737 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900738 edev->d_attrs_muex[index].attr.name = name;
739 edev->d_attrs_muex[index].attr.mode = 0000;
740 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
741 .attr;
742 }
743 edev->attr_g_muex.name = muex_name;
744 edev->attr_g_muex.attrs = edev->attrs_muex;
745
746 }
747
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900748 if (edev->max_supported) {
749 edev->extcon_dev_type.groups =
750 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900751 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900752 if (!edev->extcon_dev_type.groups) {
753 ret = -ENOMEM;
754 goto err_alloc_groups;
755 }
756
Chanwoo Choidae61652013-09-27 09:19:40 +0900757 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900758 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
759
760 for (index = 0; index < edev->max_supported; index++)
761 edev->extcon_dev_type.groups[index] =
762 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900763 if (edev->mutually_exclusive)
764 edev->extcon_dev_type.groups[index] =
765 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900766
Chanwoo Choidae61652013-09-27 09:19:40 +0900767 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900768 }
769
Chanwoo Choidae61652013-09-27 09:19:40 +0900770 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900771 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900772 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900773 goto err_dev;
774 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900775#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900776 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900777 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900778#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900779
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900780 spin_lock_init(&edev->lock);
781
Chanwoo Choi046050f2015-05-19 20:01:12 +0900782 edev->nh = devm_kzalloc(&edev->dev,
783 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
784 if (!edev->nh) {
785 ret = -ENOMEM;
786 goto err_dev;
787 }
788
789 for (index = 0; index < edev->max_supported; index++)
790 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900791
Chanwoo Choidae61652013-09-27 09:19:40 +0900792 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900793 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900794
795 mutex_lock(&extcon_dev_list_lock);
796 list_add(&edev->entry, &extcon_dev_list);
797 mutex_unlock(&extcon_dev_list_lock);
798
MyungJoo Hamde55d872012-04-20 14:16:22 +0900799 return 0;
800
801err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900802 if (edev->max_supported)
803 kfree(edev->extcon_dev_type.groups);
804err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900805 if (edev->max_supported && edev->mutually_exclusive) {
806 for (index = 0; edev->mutually_exclusive[index]; index++)
807 kfree(edev->d_attrs_muex[index].attr.name);
808 kfree(edev->d_attrs_muex);
809 kfree(edev->attrs_muex);
810 }
811err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900812 for (index = 0; index < edev->max_supported; index++)
813 kfree(edev->cables[index].attr_g.name);
814err_alloc_cables:
815 if (edev->max_supported)
816 kfree(edev->cables);
817err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900818 return ret;
819}
820EXPORT_SYMBOL_GPL(extcon_dev_register);
821
822/**
823 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900824 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900825 *
826 * Note that this does not call kfree(edev) because edev was not allocated
827 * by this class.
828 */
829void extcon_dev_unregister(struct extcon_dev *edev)
830{
anish kumar57e7cd32012-10-22 09:43:33 +0900831 int index;
832
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900833 if (!edev)
834 return;
835
anish kumar57e7cd32012-10-22 09:43:33 +0900836 mutex_lock(&extcon_dev_list_lock);
837 list_del(&edev->entry);
838 mutex_unlock(&extcon_dev_list_lock);
839
Chanwoo Choidae61652013-09-27 09:19:40 +0900840 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
841 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
842 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +0900843 return;
844 }
845
Wang, Xiaoming7585ca02013-11-01 18:48:14 -0400846 device_unregister(&edev->dev);
847
anish kumar57e7cd32012-10-22 09:43:33 +0900848 if (edev->mutually_exclusive && edev->max_supported) {
849 for (index = 0; edev->mutually_exclusive[index];
850 index++)
851 kfree(edev->d_attrs_muex[index].attr.name);
852 kfree(edev->d_attrs_muex);
853 kfree(edev->attrs_muex);
854 }
855
856 for (index = 0; index < edev->max_supported; index++)
857 kfree(edev->cables[index].attr_g.name);
858
859 if (edev->max_supported) {
860 kfree(edev->extcon_dev_type.groups);
861 kfree(edev->cables);
862 }
863
864#if defined(CONFIG_ANDROID)
865 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900866 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +0900867#endif
Chanwoo Choidae61652013-09-27 09:19:40 +0900868 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900869}
870EXPORT_SYMBOL_GPL(extcon_dev_unregister);
871
Sangjung Woo11112442014-04-21 19:10:08 +0900872static void devm_extcon_dev_unreg(struct device *dev, void *res)
873{
874 extcon_dev_unregister(*(struct extcon_dev **)res);
875}
876
Sangjung Woo11112442014-04-21 19:10:08 +0900877/**
878 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
879 * @dev: device to allocate extcon device
880 * @edev: the new extcon device to register
881 *
882 * Managed extcon_dev_register() function. If extcon device is attached with
883 * this function, that extcon device is automatically unregistered on driver
884 * detach. Internally this function calls extcon_dev_register() function.
885 * To get more information, refer that function.
886 *
887 * If extcon device is registered with this function and the device needs to be
888 * unregistered separately, devm_extcon_dev_unregister() should be used.
889 *
890 * Returns 0 if success or negaive error number if failure.
891 */
892int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
893{
894 struct extcon_dev **ptr;
895 int ret;
896
897 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
898 if (!ptr)
899 return -ENOMEM;
900
901 ret = extcon_dev_register(edev);
902 if (ret) {
903 devres_free(ptr);
904 return ret;
905 }
906
907 *ptr = edev;
908 devres_add(dev, ptr);
909
910 return 0;
911}
912EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
913
914/**
915 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
916 * @dev: device the extcon belongs to
917 * @edev: the extcon device to unregister
918 *
919 * Unregister extcon device that is registered with devm_extcon_dev_register()
920 * function.
921 */
922void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
923{
924 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
925 devm_extcon_dev_match, edev));
926}
927EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
928
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900929#ifdef CONFIG_OF
930/*
931 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
932 * @dev - instance to the given device
933 * @index - index into list of extcon_dev
934 *
935 * return the instance of extcon device
936 */
937struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
938{
939 struct device_node *node;
940 struct extcon_dev *edev;
941
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900942 if (!dev)
943 return ERR_PTR(-EINVAL);
944
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900945 if (!dev->of_node) {
946 dev_err(dev, "device does not have a device node entry\n");
947 return ERR_PTR(-EINVAL);
948 }
949
950 node = of_parse_phandle(dev->of_node, "extcon", index);
951 if (!node) {
952 dev_err(dev, "failed to get phandle in %s node\n",
953 dev->of_node->full_name);
954 return ERR_PTR(-ENODEV);
955 }
956
Tomasz Figaf841afb12014-10-16 15:11:44 +0200957 mutex_lock(&extcon_dev_list_lock);
958 list_for_each_entry(edev, &extcon_dev_list, entry) {
959 if (edev->dev.parent && edev->dev.parent->of_node == node) {
960 mutex_unlock(&extcon_dev_list_lock);
961 return edev;
962 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900963 }
Tomasz Figaf841afb12014-10-16 15:11:44 +0200964 mutex_unlock(&extcon_dev_list_lock);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900965
Tomasz Figaf841afb12014-10-16 15:11:44 +0200966 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +0900967}
968#else
969struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
970{
971 return ERR_PTR(-ENOSYS);
972}
973#endif /* CONFIG_OF */
974EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
975
Chanwoo Choi707d7552015-04-15 13:57:51 +0900976/**
977 * extcon_get_edev_name() - Get the name of the extcon device.
978 * @edev: the extcon device
979 */
980const char *extcon_get_edev_name(struct extcon_dev *edev)
981{
982 return !edev ? NULL : edev->name;
983}
984
MyungJoo Hamde55d872012-04-20 14:16:22 +0900985static int __init extcon_class_init(void)
986{
987 return create_extcon_class();
988}
989module_init(extcon_class_init);
990
991static void __exit extcon_class_exit(void)
992{
Peter Huewe0dc77b62012-09-24 15:32:31 +0900993#if defined(CONFIG_ANDROID)
994 class_compat_unregister(switch_class);
995#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +0900996 class_destroy(extcon_class);
997}
998module_exit(extcon_class_exit);
999
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001000MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001001MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1002MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1003MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1004MODULE_DESCRIPTION("External connector (extcon) class driver");
1005MODULE_LICENSE("GPL");