blob: 2fb5f757e8114b3d8ec37fb314c61f9db23aca03 [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 *
6 * Copyright (C) 2012 Samsung Electronics
7 * Author: Donggeun Kim <dg77.kim@samsung.com>
8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
9 *
10 * based on android/drivers/switch/switch_class.c
11 * Copyright (C) 2008 Google, Inc.
12 * Author: Mike Lockwood <lockwood@android.com>
13 *
14 * This software is licensed under the terms of the GNU General Public
15 * License version 2, as published by the Free Software Foundation, and
16 * may be copied, distributed, and modified under those terms.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
Chanwoo Choib9ec23c2015-04-24 14:48:52 +090022 */
MyungJoo Hamde55d872012-04-20 14:16:22 +090023
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/init.h>
27#include <linux/device.h>
28#include <linux/fs.h>
29#include <linux/err.h>
30#include <linux/extcon.h>
Tomasz Figaf841afb2014-10-16 15:11:44 +020031#include <linux/of.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090032#include <linux/slab.h>
Mark Brown9baf3222012-08-16 20:03:21 +010033#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090034
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090035/*
36 * extcon_cable_name suggests the standard cable names for commonly used
37 * cable types.
38 *
39 * However, please do not use extcon_cable_name directly for extcon_dev
40 * struct's supported_cable pointer unless your device really supports
41 * every single port-type of the following cable names. Please choose cable
42 * names that are actually used in your extcon device.
43 */
anish kumar0cf6ad82012-08-30 00:35:09 +053044const char extcon_cable_name[][CABLE_NAME_MAX + 1] = {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090045 [EXTCON_USB] = "USB",
46 [EXTCON_USB_HOST] = "USB-Host",
47 [EXTCON_TA] = "TA",
48 [EXTCON_FAST_CHARGER] = "Fast-charger",
49 [EXTCON_SLOW_CHARGER] = "Slow-charger",
50 [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
51 [EXTCON_HDMI] = "HDMI",
52 [EXTCON_MHL] = "MHL",
53 [EXTCON_DVI] = "DVI",
54 [EXTCON_VGA] = "VGA",
55 [EXTCON_DOCK] = "Dock",
56 [EXTCON_LINE_IN] = "Line-in",
57 [EXTCON_LINE_OUT] = "Line-out",
58 [EXTCON_MIC_IN] = "Microphone",
59 [EXTCON_HEADPHONE_OUT] = "Headphone",
60 [EXTCON_SPDIF_IN] = "SPDIF-in",
61 [EXTCON_SPDIF_OUT] = "SPDIF-out",
62 [EXTCON_VIDEO_IN] = "Video-in",
63 [EXTCON_VIDEO_OUT] = "Video-out",
Mark Brown0e1507c2012-05-02 10:38:51 +010064 [EXTCON_MECHANICAL] = "Mechanical",
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090065};
66
Mark Brownbe3a07f2012-06-05 16:14:38 +010067static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090068#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +090069static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090070#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +090071
Donggeun Kim74c5d092012-04-20 14:16:24 +090072static LIST_HEAD(extcon_dev_list);
73static DEFINE_MUTEX(extcon_dev_list_lock);
74
MyungJoo Hambde68e62012-04-20 14:16:26 +090075/**
76 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +090077 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +090078 * @edev: the extcon device
79 * @new_state: new cable attach status for @edev
80 *
81 * Returns 0 if nothing violates. Returns the index + 1 for the first
82 * violated condition.
83 */
84static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
85{
86 int i = 0;
87
88 if (!edev->mutually_exclusive)
89 return 0;
90
91 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +053092 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +090093 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +090094
anish kumar28c0ada2012-08-30 00:35:10 +053095 /* calculate the total number of bits set */
96 weight = hweight32(correspondants);
97 if (weight > 1)
98 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +090099 }
100
101 return 0;
102}
103
MyungJoo Hamde55d872012-04-20 14:16:22 +0900104static ssize_t state_show(struct device *dev, struct device_attribute *attr,
105 char *buf)
106{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900107 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900108 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900109
110 if (edev->print_state) {
111 int ret = edev->print_state(edev, buf);
112
113 if (ret >= 0)
114 return ret;
115 /* Use default if failed */
116 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900117
118 if (edev->max_supported == 0)
119 return sprintf(buf, "%u\n", edev->state);
120
121 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
122 if (!edev->supported_cable[i])
123 break;
124 count += sprintf(buf + count, "%s=%d\n",
125 edev->supported_cable[i],
126 !!(edev->state & (1 << i)));
127 }
128
129 return count;
130}
131
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900132static ssize_t state_store(struct device *dev, struct device_attribute *attr,
133 const char *buf, size_t count)
134{
135 u32 state;
136 ssize_t ret = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900137 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900138
139 ret = sscanf(buf, "0x%x", &state);
140 if (ret == 0)
141 ret = -EINVAL;
142 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900143 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900144
145 if (ret < 0)
146 return ret;
147
148 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900149}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700150static DEVICE_ATTR_RW(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900151
152static ssize_t name_show(struct device *dev, struct device_attribute *attr,
153 char *buf)
154{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900155 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900156
157 /* Optional callback given by the user */
158 if (edev->print_name) {
159 int ret = edev->print_name(edev, buf);
Chanwoo Choi34825e52015-03-07 01:41:36 +0900160
MyungJoo Hamde55d872012-04-20 14:16:22 +0900161 if (ret >= 0)
162 return ret;
163 }
164
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900165 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900166}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700167static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900168
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900169static ssize_t cable_name_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
172 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
173 attr_name);
174
175 return sprintf(buf, "%s\n",
176 cable->edev->supported_cable[cable->cable_index]);
177}
178
179static ssize_t cable_state_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
183 attr_state);
184
185 return sprintf(buf, "%d\n",
186 extcon_get_cable_state_(cable->edev,
187 cable->cable_index));
188}
189
MyungJoo Hamde55d872012-04-20 14:16:22 +0900190/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900191 * extcon_update_state() - Update the cable attach states of the extcon device
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900192 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900193 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900194 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900195 * @state: new cable attach status for @edev
196 *
197 * Changing the state sends uevent with environment variable containing
198 * the name of extcon device (envp[0]) and the state output (envp[1]).
199 * Tizen uses this format for extcon device to get events from ports.
200 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900201 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900202 * Note that the notifier provides which bits are changed in the state
203 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900204 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900205int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900206{
207 char name_buf[120];
208 char state_buf[120];
209 char *prop_buf;
210 char *envp[3];
211 int env_offset = 0;
212 int length;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900213 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900214
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900215 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900216
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900217 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
218 u32 old_state = edev->state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900219
MyungJoo Hambde68e62012-04-20 14:16:26 +0900220 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
221 (state & mask))) {
222 spin_unlock_irqrestore(&edev->lock, flags);
223 return -EPERM;
224 }
225
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900226 edev->state &= ~mask;
227 edev->state |= state & mask;
228
229 raw_notifier_call_chain(&edev->nh, old_state, edev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900230 /* This could be in interrupt handler */
231 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900232 if (prop_buf) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900233 length = name_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900234 if (length > 0) {
235 if (prop_buf[length - 1] == '\n')
236 prop_buf[length - 1] = 0;
237 snprintf(name_buf, sizeof(name_buf),
238 "NAME=%s", prop_buf);
239 envp[env_offset++] = name_buf;
240 }
Chanwoo Choidae61652013-09-27 09:19:40 +0900241 length = state_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900242 if (length > 0) {
243 if (prop_buf[length - 1] == '\n')
244 prop_buf[length - 1] = 0;
245 snprintf(state_buf, sizeof(state_buf),
246 "STATE=%s", prop_buf);
247 envp[env_offset++] = state_buf;
248 }
249 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900250 /* Unlock early before uevent */
251 spin_unlock_irqrestore(&edev->lock, flags);
252
Chanwoo Choidae61652013-09-27 09:19:40 +0900253 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900254 free_page((unsigned long)prop_buf);
255 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900256 /* Unlock early before uevent */
257 spin_unlock_irqrestore(&edev->lock, flags);
258
Chanwoo Choidae61652013-09-27 09:19:40 +0900259 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
260 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900261 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900262 } else {
263 /* No changes */
264 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900265 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900266
267 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900268}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900269EXPORT_SYMBOL_GPL(extcon_update_state);
270
271/**
272 * extcon_set_state() - Set the cable attach states of the extcon device.
273 * @edev: the extcon device
274 * @state: new cable attach status for @edev
275 *
276 * Note that notifier provides which bits are changed in the state
277 * variable with the val parameter (second) to the callback.
278 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900279int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900280{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900281 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900282}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900283EXPORT_SYMBOL_GPL(extcon_set_state);
284
Donggeun Kim74c5d092012-04-20 14:16:24 +0900285/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900286 * extcon_find_cable_index() - Get the cable index based on the cable name.
287 * @edev: the extcon device that has the cable.
288 * @cable_name: cable name to be searched.
289 *
290 * Note that accessing a cable state based on cable_index is faster than
291 * cable_name because using cable_name induces a loop with strncmp().
292 * Thus, when get/set_cable_state is repeatedly used, using cable_index
293 * is recommended.
294 */
295int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
296{
297 int i;
298
299 if (edev->supported_cable) {
300 for (i = 0; edev->supported_cable[i]; i++) {
301 if (!strncmp(edev->supported_cable[i],
302 cable_name, CABLE_NAME_MAX))
303 return i;
304 }
305 }
306
307 return -EINVAL;
308}
309EXPORT_SYMBOL_GPL(extcon_find_cable_index);
310
311/**
312 * extcon_get_cable_state_() - Get the status of a specific cable.
313 * @edev: the extcon device that has the cable.
314 * @index: cable index that can be retrieved by extcon_find_cable_index().
315 */
316int extcon_get_cable_state_(struct extcon_dev *edev, int index)
317{
318 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
319 return -EINVAL;
320
321 return !!(edev->state & (1 << index));
322}
323EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
324
325/**
326 * extcon_get_cable_state() - Get the status of a specific cable.
327 * @edev: the extcon device that has the cable.
328 * @cable_name: cable name.
329 *
330 * Note that this is slower than extcon_get_cable_state_.
331 */
332int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
333{
334 return extcon_get_cable_state_(edev, extcon_find_cable_index
335 (edev, cable_name));
336}
337EXPORT_SYMBOL_GPL(extcon_get_cable_state);
338
339/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900340 * extcon_set_cable_state_() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900341 * @edev: the extcon device that has the cable.
342 * @index: cable index that can be retrieved by
343 * extcon_find_cable_index().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900344 * @cable_state: the new cable status. The default semantics is
345 * true: attached / false: detached.
346 */
347int extcon_set_cable_state_(struct extcon_dev *edev,
348 int index, bool cable_state)
349{
350 u32 state;
351
352 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
353 return -EINVAL;
354
355 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900356 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900357}
358EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
359
360/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900361 * extcon_set_cable_state() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900362 * @edev: the extcon device that has the cable.
363 * @cable_name: cable name.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900364 * @cable_state: the new cable status. The default semantics is
365 * true: attached / false: detached.
366 *
367 * Note that this is slower than extcon_set_cable_state_.
368 */
369int extcon_set_cable_state(struct extcon_dev *edev,
370 const char *cable_name, bool cable_state)
371{
372 return extcon_set_cable_state_(edev, extcon_find_cable_index
373 (edev, cable_name), cable_state);
374}
375EXPORT_SYMBOL_GPL(extcon_set_cable_state);
376
377/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900378 * extcon_get_extcon_dev() - Get the extcon device instance from the name
379 * @extcon_name: The extcon name provided with extcon_dev_register()
380 */
381struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
382{
383 struct extcon_dev *sd;
384
385 mutex_lock(&extcon_dev_list_lock);
386 list_for_each_entry(sd, &extcon_dev_list, entry) {
387 if (!strcmp(sd->name, extcon_name))
388 goto out;
389 }
390 sd = NULL;
391out:
392 mutex_unlock(&extcon_dev_list_lock);
393 return sd;
394}
395EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
396
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900397static int _call_per_cable(struct notifier_block *nb, unsigned long val,
398 void *ptr)
399{
400 struct extcon_specific_cable_nb *obj = container_of(nb,
401 struct extcon_specific_cable_nb, internal_nb);
402 struct extcon_dev *edev = ptr;
403
404 if ((val & (1 << obj->cable_index)) !=
405 (edev->state & (1 << obj->cable_index))) {
Chanwoo Choif4cce692012-04-27 15:17:28 +0900406 bool cable_state = true;
407
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900408 obj->previous_value = val;
Chanwoo Choif4cce692012-04-27 15:17:28 +0900409
410 if (val & (1 << obj->cable_index))
411 cable_state = false;
412
413 return obj->user_nb->notifier_call(obj->user_nb,
414 cable_state, ptr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900415 }
416
417 return NOTIFY_OK;
418}
419
420/**
421 * extcon_register_interest() - Register a notifier for a state change of a
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900422 * specific cable, not an entier set of cables of a
423 * extcon device.
424 * @obj: an empty extcon_specific_cable_nb object to be returned.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900425 * @extcon_name: the name of extcon device.
Jenny TC4f2de3b2012-10-18 21:00:32 +0900426 * if NULL, extcon_register_interest will register
427 * every cable with the target cable_name given.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900428 * @cable_name: the target cable name.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900429 * @nb: the notifier block to get notified.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900430 *
431 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
432 * the struct for you.
433 *
434 * extcon_register_interest is a helper function for those who want to get
435 * notification for a single specific cable's status change. If a user wants
436 * to get notification for any changes of all cables of a extcon device,
437 * he/she should use the general extcon_register_notifier().
438 *
439 * Note that the second parameter given to the callback of nb (val) is
440 * "old_state", not the current state. The current state can be retrieved
441 * by looking at the third pameter (edev pointer)'s state value.
442 */
443int extcon_register_interest(struct extcon_specific_cable_nb *obj,
444 const char *extcon_name, const char *cable_name,
445 struct notifier_block *nb)
446{
Hans de Goede66bee352015-03-21 17:26:24 +0100447 unsigned long flags;
448 int ret;
449
Jenny TC4f2de3b2012-10-18 21:00:32 +0900450 if (!obj || !cable_name || !nb)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900451 return -EINVAL;
452
Jenny TC4f2de3b2012-10-18 21:00:32 +0900453 if (extcon_name) {
454 obj->edev = extcon_get_extcon_dev(extcon_name);
455 if (!obj->edev)
456 return -ENODEV;
457
Chanwoo Choic2275d22013-08-23 10:21:37 +0900458 obj->cable_index = extcon_find_cable_index(obj->edev,
459 cable_name);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900460 if (obj->cable_index < 0)
Sachin Kamatc0c078c2012-11-20 16:30:31 +0900461 return obj->cable_index;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900462
463 obj->user_nb = nb;
464
465 obj->internal_nb.notifier_call = _call_per_cable;
466
Hans de Goede66bee352015-03-21 17:26:24 +0100467 spin_lock_irqsave(&obj->edev->lock, flags);
468 ret = raw_notifier_chain_register(&obj->edev->nh,
Chanwoo Choic2275d22013-08-23 10:21:37 +0900469 &obj->internal_nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100470 spin_unlock_irqrestore(&obj->edev->lock, flags);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900471 } else {
472 struct class_dev_iter iter;
473 struct extcon_dev *extd;
474 struct device *dev;
475
476 if (!extcon_class)
477 return -ENODEV;
478 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
479 while ((dev = class_dev_iter_next(&iter))) {
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900480 extd = dev_get_drvdata(dev);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900481
482 if (extcon_find_cable_index(extd, cable_name) < 0)
483 continue;
484
485 class_dev_iter_exit(&iter);
486 return extcon_register_interest(obj, extd->name,
487 cable_name, nb);
488 }
489
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900490 ret = -ENODEV;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900491 }
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900492
493 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900494}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900495EXPORT_SYMBOL_GPL(extcon_register_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900496
497/**
498 * extcon_unregister_interest() - Unregister the notifier registered by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900499 * extcon_register_interest().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900500 * @obj: the extcon_specific_cable_nb object returned by
501 * extcon_register_interest().
502 */
503int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
504{
Hans de Goede66bee352015-03-21 17:26:24 +0100505 unsigned long flags;
506 int ret;
507
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900508 if (!obj)
509 return -EINVAL;
510
Hans de Goede66bee352015-03-21 17:26:24 +0100511 spin_lock_irqsave(&obj->edev->lock, flags);
512 ret = raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
513 spin_unlock_irqrestore(&obj->edev->lock, flags);
514
515 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900516}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900517EXPORT_SYMBOL_GPL(extcon_unregister_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900518
Donggeun Kim74c5d092012-04-20 14:16:24 +0900519/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900520 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900521 * any attach status changes from the extcon.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900522 * @edev: the extcon device.
523 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900524 *
525 * Note that the second parameter given to the callback of nb (val) is
526 * "old_state", not the current state. The current state can be retrieved
527 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900528 */
529int extcon_register_notifier(struct extcon_dev *edev,
530 struct notifier_block *nb)
531{
Hans de Goede66bee352015-03-21 17:26:24 +0100532 unsigned long flags;
533 int ret;
534
535 spin_lock_irqsave(&edev->lock, flags);
536 ret = raw_notifier_chain_register(&edev->nh, nb);
537 spin_unlock_irqrestore(&edev->lock, flags);
538
539 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900540}
541EXPORT_SYMBOL_GPL(extcon_register_notifier);
542
543/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900544 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900545 * @edev: the extcon device.
546 * @nb: a registered notifier block to be unregistered.
547 */
548int extcon_unregister_notifier(struct extcon_dev *edev,
549 struct notifier_block *nb)
550{
Hans de Goede66bee352015-03-21 17:26:24 +0100551 unsigned long flags;
552 int ret;
553
554 spin_lock_irqsave(&edev->lock, flags);
555 ret = raw_notifier_chain_unregister(&edev->nh, nb);
556 spin_unlock_irqrestore(&edev->lock, flags);
557
558 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900559}
560EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
561
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700562static struct attribute *extcon_attrs[] = {
563 &dev_attr_state.attr,
564 &dev_attr_name.attr,
565 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900566};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700567ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900568
569static int create_extcon_class(void)
570{
571 if (!extcon_class) {
572 extcon_class = class_create(THIS_MODULE, "extcon");
573 if (IS_ERR(extcon_class))
574 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700575 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900576
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900577#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900578 switch_class = class_compat_register("switch");
579 if (WARN(!switch_class, "cannot allocate"))
580 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900581#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900582 }
583
584 return 0;
585}
586
MyungJoo Hamde55d872012-04-20 14:16:22 +0900587static void extcon_dev_release(struct device *dev)
588{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900589}
590
MyungJoo Hambde68e62012-04-20 14:16:26 +0900591static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900592static void dummy_sysfs_dev_release(struct device *dev)
593{
594}
595
Chanwoo Choia9af6522014-04-24 19:46:49 +0900596/*
597 * extcon_dev_allocate() - Allocate the memory of extcon device.
598 * @supported_cable: Array of supported cable names ending with NULL.
599 * If supported_cable is NULL, cable name related APIs
600 * are disabled.
601 *
602 * This function allocates the memory for extcon device without allocating
603 * memory in each extcon provider driver and initialize default setting for
604 * extcon device.
605 *
606 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
607 */
608struct extcon_dev *extcon_dev_allocate(const char **supported_cable)
609{
610 struct extcon_dev *edev;
611
612 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
613 if (!edev)
614 return ERR_PTR(-ENOMEM);
615
616 edev->max_supported = 0;
617 edev->supported_cable = supported_cable;
618
619 return edev;
620}
621
622/*
623 * extcon_dev_free() - Free the memory of extcon device.
624 * @edev: the extcon device to free
625 */
626void extcon_dev_free(struct extcon_dev *edev)
627{
628 kfree(edev);
629}
630EXPORT_SYMBOL_GPL(extcon_dev_free);
631
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900632static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
633{
634 struct extcon_dev **r = res;
635
636 if (WARN_ON(!r || !*r))
637 return 0;
638
639 return *r == data;
640}
641
642static void devm_extcon_dev_release(struct device *dev, void *res)
643{
644 extcon_dev_free(*(struct extcon_dev **)res);
645}
646
647/**
648 * devm_extcon_dev_allocate - Allocate managed extcon device
649 * @dev: device owning the extcon device being created
650 * @supported_cable: Array of supported cable names ending with NULL.
651 * If supported_cable is NULL, cable name related APIs
652 * are disabled.
653 *
654 * This function manages automatically the memory of extcon device using device
655 * resource management and simplify the control of freeing the memory of extcon
656 * device.
657 *
658 * Returns the pointer memory of allocated extcon_dev if success
659 * or ERR_PTR(err) if fail
660 */
661struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
662 const char **supported_cable)
663{
664 struct extcon_dev **ptr, *edev;
665
666 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
667 if (!ptr)
668 return ERR_PTR(-ENOMEM);
669
670 edev = extcon_dev_allocate(supported_cable);
671 if (IS_ERR(edev)) {
672 devres_free(ptr);
673 return edev;
674 }
675
Chanwoo Choiac65a622014-05-30 10:13:15 +0900676 edev->dev.parent = dev;
677
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900678 *ptr = edev;
679 devres_add(dev, ptr);
680
681 return edev;
682}
683EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
684
685void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
686{
687 WARN_ON(devres_release(dev, devm_extcon_dev_release,
688 devm_extcon_dev_match, edev));
689}
690EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
691
MyungJoo Hamde55d872012-04-20 14:16:22 +0900692/**
693 * extcon_dev_register() - Register a new extcon device
694 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900695 *
696 * Among the members of edev struct, please set the "user initializing data"
697 * in any case and set the "optional callbacks" if required. However, please
698 * do not set the values of "internal data", which are initialized by
699 * this function.
700 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900701int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900702{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900703 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900704 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900705
706 if (!extcon_class) {
707 ret = create_extcon_class();
708 if (ret < 0)
709 return ret;
710 }
711
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900712 if (edev->supported_cable) {
713 /* Get size of array */
714 for (index = 0; edev->supported_cable[index]; index++)
715 ;
716 edev->max_supported = index;
717 } else {
718 edev->max_supported = 0;
719 }
720
721 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900722 dev_err(&edev->dev, "extcon: maximum number of supported cables exceeded.\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900723 return -EINVAL;
724 }
725
Chanwoo Choidae61652013-09-27 09:19:40 +0900726 edev->dev.class = extcon_class;
727 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900728
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900729 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900730 if (IS_ERR_OR_NULL(edev->name)) {
731 dev_err(&edev->dev,
732 "extcon device name is null\n");
733 return -EINVAL;
734 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900735 dev_set_name(&edev->dev, "extcon%lu",
736 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900737
738 if (edev->max_supported) {
739 char buf[10];
740 char *str;
741 struct extcon_cable *cable;
742
743 edev->cables = kzalloc(sizeof(struct extcon_cable) *
744 edev->max_supported, GFP_KERNEL);
745 if (!edev->cables) {
746 ret = -ENOMEM;
747 goto err_sysfs_alloc;
748 }
749 for (index = 0; index < edev->max_supported; index++) {
750 cable = &edev->cables[index];
751
752 snprintf(buf, 10, "cable.%d", index);
753 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
754 GFP_KERNEL);
755 if (!str) {
756 for (index--; index >= 0; index--) {
757 cable = &edev->cables[index];
758 kfree(cable->attr_g.name);
759 }
760 ret = -ENOMEM;
761
762 goto err_alloc_cables;
763 }
764 strcpy(str, buf);
765
766 cable->edev = edev;
767 cable->cable_index = index;
768 cable->attrs[0] = &cable->attr_name.attr;
769 cable->attrs[1] = &cable->attr_state.attr;
770 cable->attrs[2] = NULL;
771 cable->attr_g.name = str;
772 cable->attr_g.attrs = cable->attrs;
773
Mark Brown9baf3222012-08-16 20:03:21 +0100774 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900775 cable->attr_name.attr.name = "name";
776 cable->attr_name.attr.mode = 0444;
777 cable->attr_name.show = cable_name_show;
778
Mark Brown9baf3222012-08-16 20:03:21 +0100779 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900780 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900781 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900782 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900783 }
784 }
785
MyungJoo Hambde68e62012-04-20 14:16:26 +0900786 if (edev->max_supported && edev->mutually_exclusive) {
787 char buf[80];
788 char *name;
789
790 /* Count the size of mutually_exclusive array */
791 for (index = 0; edev->mutually_exclusive[index]; index++)
792 ;
793
794 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
795 (index + 1), GFP_KERNEL);
796 if (!edev->attrs_muex) {
797 ret = -ENOMEM;
798 goto err_muex;
799 }
800
801 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
802 index, GFP_KERNEL);
803 if (!edev->d_attrs_muex) {
804 ret = -ENOMEM;
805 kfree(edev->attrs_muex);
806 goto err_muex;
807 }
808
809 for (index = 0; edev->mutually_exclusive[index]; index++) {
810 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
811 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
812 GFP_KERNEL);
813 if (!name) {
814 for (index--; index >= 0; index--) {
815 kfree(edev->d_attrs_muex[index].attr.
816 name);
817 }
818 kfree(edev->d_attrs_muex);
819 kfree(edev->attrs_muex);
820 ret = -ENOMEM;
821 goto err_muex;
822 }
823 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100824 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900825 edev->d_attrs_muex[index].attr.name = name;
826 edev->d_attrs_muex[index].attr.mode = 0000;
827 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
828 .attr;
829 }
830 edev->attr_g_muex.name = muex_name;
831 edev->attr_g_muex.attrs = edev->attrs_muex;
832
833 }
834
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900835 if (edev->max_supported) {
836 edev->extcon_dev_type.groups =
837 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900838 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900839 if (!edev->extcon_dev_type.groups) {
840 ret = -ENOMEM;
841 goto err_alloc_groups;
842 }
843
Chanwoo Choidae61652013-09-27 09:19:40 +0900844 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900845 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
846
847 for (index = 0; index < edev->max_supported; index++)
848 edev->extcon_dev_type.groups[index] =
849 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900850 if (edev->mutually_exclusive)
851 edev->extcon_dev_type.groups[index] =
852 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900853
Chanwoo Choidae61652013-09-27 09:19:40 +0900854 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900855 }
856
Chanwoo Choidae61652013-09-27 09:19:40 +0900857 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900858 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900859 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900860 goto err_dev;
861 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900862#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900863 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900864 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900865#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900866
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900867 spin_lock_init(&edev->lock);
868
Donggeun Kim74c5d092012-04-20 14:16:24 +0900869 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
870
Chanwoo Choidae61652013-09-27 09:19:40 +0900871 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900872 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900873
874 mutex_lock(&extcon_dev_list_lock);
875 list_add(&edev->entry, &extcon_dev_list);
876 mutex_unlock(&extcon_dev_list_lock);
877
MyungJoo Hamde55d872012-04-20 14:16:22 +0900878 return 0;
879
880err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900881 if (edev->max_supported)
882 kfree(edev->extcon_dev_type.groups);
883err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900884 if (edev->max_supported && edev->mutually_exclusive) {
885 for (index = 0; edev->mutually_exclusive[index]; index++)
886 kfree(edev->d_attrs_muex[index].attr.name);
887 kfree(edev->d_attrs_muex);
888 kfree(edev->attrs_muex);
889 }
890err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900891 for (index = 0; index < edev->max_supported; index++)
892 kfree(edev->cables[index].attr_g.name);
893err_alloc_cables:
894 if (edev->max_supported)
895 kfree(edev->cables);
896err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900897 return ret;
898}
899EXPORT_SYMBOL_GPL(extcon_dev_register);
900
901/**
902 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900903 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900904 *
905 * Note that this does not call kfree(edev) because edev was not allocated
906 * by this class.
907 */
908void extcon_dev_unregister(struct extcon_dev *edev)
909{
anish kumar57e7cd32012-10-22 09:43:33 +0900910 int index;
911
912 mutex_lock(&extcon_dev_list_lock);
913 list_del(&edev->entry);
914 mutex_unlock(&extcon_dev_list_lock);
915
Chanwoo Choidae61652013-09-27 09:19:40 +0900916 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
917 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
918 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +0900919 return;
920 }
921
Wang, Xiaoming7585ca02013-11-01 18:48:14 -0400922 device_unregister(&edev->dev);
923
anish kumar57e7cd32012-10-22 09:43:33 +0900924 if (edev->mutually_exclusive && edev->max_supported) {
925 for (index = 0; edev->mutually_exclusive[index];
926 index++)
927 kfree(edev->d_attrs_muex[index].attr.name);
928 kfree(edev->d_attrs_muex);
929 kfree(edev->attrs_muex);
930 }
931
932 for (index = 0; index < edev->max_supported; index++)
933 kfree(edev->cables[index].attr_g.name);
934
935 if (edev->max_supported) {
936 kfree(edev->extcon_dev_type.groups);
937 kfree(edev->cables);
938 }
939
940#if defined(CONFIG_ANDROID)
941 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900942 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +0900943#endif
Chanwoo Choidae61652013-09-27 09:19:40 +0900944 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900945}
946EXPORT_SYMBOL_GPL(extcon_dev_unregister);
947
Sangjung Woo11112442014-04-21 19:10:08 +0900948static void devm_extcon_dev_unreg(struct device *dev, void *res)
949{
950 extcon_dev_unregister(*(struct extcon_dev **)res);
951}
952
Sangjung Woo11112442014-04-21 19:10:08 +0900953/**
954 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
955 * @dev: device to allocate extcon device
956 * @edev: the new extcon device to register
957 *
958 * Managed extcon_dev_register() function. If extcon device is attached with
959 * this function, that extcon device is automatically unregistered on driver
960 * detach. Internally this function calls extcon_dev_register() function.
961 * To get more information, refer that function.
962 *
963 * If extcon device is registered with this function and the device needs to be
964 * unregistered separately, devm_extcon_dev_unregister() should be used.
965 *
966 * Returns 0 if success or negaive error number if failure.
967 */
968int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
969{
970 struct extcon_dev **ptr;
971 int ret;
972
973 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
974 if (!ptr)
975 return -ENOMEM;
976
977 ret = extcon_dev_register(edev);
978 if (ret) {
979 devres_free(ptr);
980 return ret;
981 }
982
983 *ptr = edev;
984 devres_add(dev, ptr);
985
986 return 0;
987}
988EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
989
990/**
991 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
992 * @dev: device the extcon belongs to
993 * @edev: the extcon device to unregister
994 *
995 * Unregister extcon device that is registered with devm_extcon_dev_register()
996 * function.
997 */
998void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
999{
1000 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1001 devm_extcon_dev_match, edev));
1002}
1003EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1004
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001005#ifdef CONFIG_OF
1006/*
1007 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1008 * @dev - instance to the given device
1009 * @index - index into list of extcon_dev
1010 *
1011 * return the instance of extcon device
1012 */
1013struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1014{
1015 struct device_node *node;
1016 struct extcon_dev *edev;
1017
1018 if (!dev->of_node) {
1019 dev_err(dev, "device does not have a device node entry\n");
1020 return ERR_PTR(-EINVAL);
1021 }
1022
1023 node = of_parse_phandle(dev->of_node, "extcon", index);
1024 if (!node) {
1025 dev_err(dev, "failed to get phandle in %s node\n",
1026 dev->of_node->full_name);
1027 return ERR_PTR(-ENODEV);
1028 }
1029
Tomasz Figaf841afb2014-10-16 15:11:44 +02001030 mutex_lock(&extcon_dev_list_lock);
1031 list_for_each_entry(edev, &extcon_dev_list, entry) {
1032 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1033 mutex_unlock(&extcon_dev_list_lock);
1034 return edev;
1035 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001036 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001037 mutex_unlock(&extcon_dev_list_lock);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001038
Tomasz Figaf841afb2014-10-16 15:11:44 +02001039 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001040}
1041#else
1042struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1043{
1044 return ERR_PTR(-ENOSYS);
1045}
1046#endif /* CONFIG_OF */
1047EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1048
Chanwoo Choi707d7552015-04-15 13:57:51 +09001049/**
1050 * extcon_get_edev_name() - Get the name of the extcon device.
1051 * @edev: the extcon device
1052 */
1053const char *extcon_get_edev_name(struct extcon_dev *edev)
1054{
1055 return !edev ? NULL : edev->name;
1056}
1057
MyungJoo Hamde55d872012-04-20 14:16:22 +09001058static int __init extcon_class_init(void)
1059{
1060 return create_extcon_class();
1061}
1062module_init(extcon_class_init);
1063
1064static void __exit extcon_class_exit(void)
1065{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001066#if defined(CONFIG_ANDROID)
1067 class_compat_unregister(switch_class);
1068#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001069 class_destroy(extcon_class);
1070}
1071module_exit(extcon_class_exit);
1072
1073MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1074MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1075MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1076MODULE_DESCRIPTION("External connector (extcon) class driver");
1077MODULE_LICENSE("GPL");