blob: fce2687bde8e9657cc8dc41d16f3137875e389f1 [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 Choi8e9bc362015-05-19 19:58:49 +090042 /* USB external connector */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090043 [EXTCON_USB] = "USB",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090044 [EXTCON_USB_HOST] = "USB-HOST",
45
46 /* Charger external connector */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090047 [EXTCON_TA] = "TA",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090048 [EXTCON_FAST_CHARGER] = "FAST-CHARGER",
49 [EXTCON_SLOW_CHARGER] = "SLOW-CHARGER",
50 [EXTCON_CHARGE_DOWNSTREAM] = "CHARGE-DOWNSTREAM",
51
52 /* Audio/Video external connector */
53 [EXTCON_LINE_IN] = "LINE-IN",
54 [EXTCON_LINE_OUT] = "LINE-OUT",
55 [EXTCON_MICROPHONE] = "MICROPHONE",
56 [EXTCON_HEADPHONE] = "HEADPHONE",
57
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090058 [EXTCON_HDMI] = "HDMI",
59 [EXTCON_MHL] = "MHL",
60 [EXTCON_DVI] = "DVI",
61 [EXTCON_VGA] = "VGA",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090062 [EXTCON_SPDIF_IN] = "SPDIF-IN",
63 [EXTCON_SPDIF_OUT] = "SPDIF-OUT",
64 [EXTCON_VIDEO_IN] = "VIDEO-IN",
65 [EXTCON_VIDEO_OUT] = "VIDEO-OUT",
66
67 /* Etc external connector */
68 [EXTCON_DOCK] = "DOCK",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090069 [EXTCON_JIG] = "JIG",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090070 [EXTCON_MECHANICAL] = "MECHANICAL",
71
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090072 NULL,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090073};
74
Mark Brownbe3a07f2012-06-05 16:14:38 +010075static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090076#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +090077static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090078#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +090079
Donggeun Kim74c5d092012-04-20 14:16:24 +090080static LIST_HEAD(extcon_dev_list);
81static DEFINE_MUTEX(extcon_dev_list_lock);
82
MyungJoo Hambde68e62012-04-20 14:16:26 +090083/**
84 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +090085 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +090086 * @edev: the extcon device
87 * @new_state: new cable attach status for @edev
88 *
89 * Returns 0 if nothing violates. Returns the index + 1 for the first
90 * violated condition.
91 */
92static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
93{
94 int i = 0;
95
96 if (!edev->mutually_exclusive)
97 return 0;
98
99 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530100 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900101 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900102
anish kumar28c0ada2012-08-30 00:35:10 +0530103 /* calculate the total number of bits set */
104 weight = hweight32(correspondants);
105 if (weight > 1)
106 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900107 }
108
109 return 0;
110}
111
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900112static int find_cable_index_by_id(struct extcon_dev *edev, const enum extcon id)
113{
114 int i;
115
116 /* Find the the index of extcon cable in edev->supported_cable */
117 for (i = 0; i < edev->max_supported; i++) {
118 if (edev->supported_cable[i] == id)
119 return i;
120 }
121
122 return -EINVAL;
123}
124
125static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
126{
127 enum extcon id = EXTCON_NONE;
128 int i;
129
130 if (edev->max_supported == 0)
131 return -EINVAL;
132
133 /* Find the the number of extcon cable */
134 for (i = 0; i < EXTCON_END; i++) {
135 if (!extcon_name[i])
136 continue;
137 if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
138 id = i;
139 break;
140 }
141 }
142
143 if (id == EXTCON_NONE)
144 return -EINVAL;
145
146 return find_cable_index_by_id(edev, id);
147}
148
MyungJoo Hamde55d872012-04-20 14:16:22 +0900149static ssize_t state_show(struct device *dev, struct device_attribute *attr,
150 char *buf)
151{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900152 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900153 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900154
155 if (edev->print_state) {
156 int ret = edev->print_state(edev, buf);
157
158 if (ret >= 0)
159 return ret;
160 /* Use default if failed */
161 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900162
163 if (edev->max_supported == 0)
164 return sprintf(buf, "%u\n", edev->state);
165
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900166 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900167 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900168 extcon_name[edev->supported_cable[i]],
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900169 !!(edev->state & (1 << i)));
170 }
171
172 return count;
173}
174
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900175static ssize_t state_store(struct device *dev, struct device_attribute *attr,
176 const char *buf, size_t count)
177{
178 u32 state;
179 ssize_t ret = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900180 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900181
182 ret = sscanf(buf, "0x%x", &state);
183 if (ret == 0)
184 ret = -EINVAL;
185 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900186 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900187
188 if (ret < 0)
189 return ret;
190
191 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900192}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700193static DEVICE_ATTR_RW(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900194
195static ssize_t name_show(struct device *dev, struct device_attribute *attr,
196 char *buf)
197{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900198 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900199
200 /* Optional callback given by the user */
201 if (edev->print_name) {
202 int ret = edev->print_name(edev, buf);
Chanwoo Choi34825e52015-03-07 01:41:36 +0900203
MyungJoo Hamde55d872012-04-20 14:16:22 +0900204 if (ret >= 0)
205 return ret;
206 }
207
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900208 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900209}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700210static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900211
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900212static ssize_t cable_name_show(struct device *dev,
213 struct device_attribute *attr, char *buf)
214{
215 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
216 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900217 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900218
219 return sprintf(buf, "%s\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900220 extcon_name[cable->edev->supported_cable[i]]);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900221}
222
223static ssize_t cable_state_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
227 attr_state);
228
229 return sprintf(buf, "%d\n",
230 extcon_get_cable_state_(cable->edev,
231 cable->cable_index));
232}
233
MyungJoo Hamde55d872012-04-20 14:16:22 +0900234/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900235 * extcon_update_state() - Update the cable attach states of the extcon device
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900236 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900237 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900238 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900239 * @state: new cable attach status for @edev
240 *
241 * Changing the state sends uevent with environment variable containing
242 * the name of extcon device (envp[0]) and the state output (envp[1]).
243 * Tizen uses this format for extcon device to get events from ports.
244 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900245 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900246 * Note that the notifier provides which bits are changed in the state
247 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900248 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900249int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900250{
251 char name_buf[120];
252 char state_buf[120];
253 char *prop_buf;
254 char *envp[3];
255 int env_offset = 0;
256 int length;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900257 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900258
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900259 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900260
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900261 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
262 u32 old_state = edev->state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900263
MyungJoo Hambde68e62012-04-20 14:16:26 +0900264 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
265 (state & mask))) {
266 spin_unlock_irqrestore(&edev->lock, flags);
267 return -EPERM;
268 }
269
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900270 edev->state &= ~mask;
271 edev->state |= state & mask;
272
273 raw_notifier_call_chain(&edev->nh, old_state, edev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900274 /* This could be in interrupt handler */
275 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900276 if (prop_buf) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900277 length = name_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900278 if (length > 0) {
279 if (prop_buf[length - 1] == '\n')
280 prop_buf[length - 1] = 0;
281 snprintf(name_buf, sizeof(name_buf),
282 "NAME=%s", prop_buf);
283 envp[env_offset++] = name_buf;
284 }
Chanwoo Choidae61652013-09-27 09:19:40 +0900285 length = state_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900286 if (length > 0) {
287 if (prop_buf[length - 1] == '\n')
288 prop_buf[length - 1] = 0;
289 snprintf(state_buf, sizeof(state_buf),
290 "STATE=%s", prop_buf);
291 envp[env_offset++] = state_buf;
292 }
293 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900294 /* Unlock early before uevent */
295 spin_unlock_irqrestore(&edev->lock, flags);
296
Chanwoo Choidae61652013-09-27 09:19:40 +0900297 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900298 free_page((unsigned long)prop_buf);
299 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900300 /* Unlock early before uevent */
301 spin_unlock_irqrestore(&edev->lock, flags);
302
Chanwoo Choidae61652013-09-27 09:19:40 +0900303 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
304 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900305 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900306 } else {
307 /* No changes */
308 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900309 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900310
311 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900312}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900313EXPORT_SYMBOL_GPL(extcon_update_state);
314
315/**
316 * extcon_set_state() - Set the cable attach states of the extcon device.
317 * @edev: the extcon device
318 * @state: new cable attach status for @edev
319 *
320 * Note that notifier provides which bits are changed in the state
321 * variable with the val parameter (second) to the callback.
322 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900323int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900324{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900325 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900326}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900327EXPORT_SYMBOL_GPL(extcon_set_state);
328
Donggeun Kim74c5d092012-04-20 14:16:24 +0900329/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900330 * extcon_get_cable_state_() - Get the status of a specific cable.
331 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900332 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900333 */
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900334int extcon_get_cable_state_(struct extcon_dev *edev, const enum extcon id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900335{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900336 int index;
337
338 index = find_cable_index_by_id(edev, id);
339 if (index < 0)
340 return index;
341
342 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900343 return -EINVAL;
344
345 return !!(edev->state & (1 << index));
346}
347EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
348
349/**
350 * extcon_get_cable_state() - Get the status of a specific cable.
351 * @edev: the extcon device that has the cable.
352 * @cable_name: cable name.
353 *
354 * Note that this is slower than extcon_get_cable_state_.
355 */
356int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
357{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900358 return extcon_get_cable_state_(edev, find_cable_index_by_name
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900359 (edev, cable_name));
360}
361EXPORT_SYMBOL_GPL(extcon_get_cable_state);
362
363/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900364 * extcon_set_cable_state_() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900365 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900366 * @id: the unique id of each external connector
367 * in extcon enumeration.
368 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900369 * true: attached / false: detached.
370 */
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900371int extcon_set_cable_state_(struct extcon_dev *edev, enum extcon id,
372 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900373{
374 u32 state;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900375 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900376
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900377 index = find_cable_index_by_id(edev, id);
378 if (index < 0)
379 return index;
380
381 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900382 return -EINVAL;
383
384 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900385 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900386}
387EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
388
389/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900390 * extcon_set_cable_state() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900391 * @edev: the extcon device that has the cable.
392 * @cable_name: cable name.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900393 * @cable_state: the new cable status. The default semantics is
394 * true: attached / false: detached.
395 *
396 * Note that this is slower than extcon_set_cable_state_.
397 */
398int extcon_set_cable_state(struct extcon_dev *edev,
399 const char *cable_name, bool cable_state)
400{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900401 return extcon_set_cable_state_(edev, find_cable_index_by_name
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900402 (edev, cable_name), cable_state);
403}
404EXPORT_SYMBOL_GPL(extcon_set_cable_state);
405
406/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900407 * extcon_get_extcon_dev() - Get the extcon device instance from the name
408 * @extcon_name: The extcon name provided with extcon_dev_register()
409 */
410struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
411{
412 struct extcon_dev *sd;
413
414 mutex_lock(&extcon_dev_list_lock);
415 list_for_each_entry(sd, &extcon_dev_list, entry) {
416 if (!strcmp(sd->name, extcon_name))
417 goto out;
418 }
419 sd = NULL;
420out:
421 mutex_unlock(&extcon_dev_list_lock);
422 return sd;
423}
424EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
425
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900426static int _call_per_cable(struct notifier_block *nb, unsigned long val,
427 void *ptr)
428{
429 struct extcon_specific_cable_nb *obj = container_of(nb,
430 struct extcon_specific_cable_nb, internal_nb);
431 struct extcon_dev *edev = ptr;
432
433 if ((val & (1 << obj->cable_index)) !=
434 (edev->state & (1 << obj->cable_index))) {
Chanwoo Choif4cce692012-04-27 15:17:28 +0900435 bool cable_state = true;
436
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900437 obj->previous_value = val;
Chanwoo Choif4cce692012-04-27 15:17:28 +0900438
439 if (val & (1 << obj->cable_index))
440 cable_state = false;
441
442 return obj->user_nb->notifier_call(obj->user_nb,
443 cable_state, ptr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900444 }
445
446 return NOTIFY_OK;
447}
448
449/**
450 * extcon_register_interest() - Register a notifier for a state change of a
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900451 * specific cable, not an entier set of cables of a
452 * extcon device.
453 * @obj: an empty extcon_specific_cable_nb object to be returned.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900454 * @extcon_name: the name of extcon device.
Jenny TC4f2de3b2012-10-18 21:00:32 +0900455 * if NULL, extcon_register_interest will register
456 * every cable with the target cable_name given.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900457 * @cable_name: the target cable name.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900458 * @nb: the notifier block to get notified.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900459 *
460 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
461 * the struct for you.
462 *
463 * extcon_register_interest is a helper function for those who want to get
464 * notification for a single specific cable's status change. If a user wants
465 * to get notification for any changes of all cables of a extcon device,
466 * he/she should use the general extcon_register_notifier().
467 *
468 * Note that the second parameter given to the callback of nb (val) is
469 * "old_state", not the current state. The current state can be retrieved
470 * by looking at the third pameter (edev pointer)'s state value.
471 */
472int extcon_register_interest(struct extcon_specific_cable_nb *obj,
473 const char *extcon_name, const char *cable_name,
474 struct notifier_block *nb)
475{
Hans de Goede66bee352015-03-21 17:26:24 +0100476 unsigned long flags;
477 int ret;
478
Jenny TC4f2de3b2012-10-18 21:00:32 +0900479 if (!obj || !cable_name || !nb)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900480 return -EINVAL;
481
Jenny TC4f2de3b2012-10-18 21:00:32 +0900482 if (extcon_name) {
483 obj->edev = extcon_get_extcon_dev(extcon_name);
484 if (!obj->edev)
485 return -ENODEV;
486
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900487 obj->cable_index = find_cable_index_by_name(obj->edev,
488 cable_name);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900489 if (obj->cable_index < 0)
Sachin Kamatc0c078c2012-11-20 16:30:31 +0900490 return obj->cable_index;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900491
492 obj->user_nb = nb;
493
494 obj->internal_nb.notifier_call = _call_per_cable;
495
Hans de Goede66bee352015-03-21 17:26:24 +0100496 spin_lock_irqsave(&obj->edev->lock, flags);
497 ret = raw_notifier_chain_register(&obj->edev->nh,
Chanwoo Choic2275d22013-08-23 10:21:37 +0900498 &obj->internal_nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100499 spin_unlock_irqrestore(&obj->edev->lock, flags);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900500 } else {
501 struct class_dev_iter iter;
502 struct extcon_dev *extd;
503 struct device *dev;
504
505 if (!extcon_class)
506 return -ENODEV;
507 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
508 while ((dev = class_dev_iter_next(&iter))) {
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900509 extd = dev_get_drvdata(dev);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900510
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900511 if (find_cable_index_by_name(extd, cable_name) < 0)
Jenny TC4f2de3b2012-10-18 21:00:32 +0900512 continue;
513
514 class_dev_iter_exit(&iter);
515 return extcon_register_interest(obj, extd->name,
516 cable_name, nb);
517 }
518
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900519 ret = -ENODEV;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900520 }
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900521
522 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900523}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900524EXPORT_SYMBOL_GPL(extcon_register_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900525
526/**
527 * extcon_unregister_interest() - Unregister the notifier registered by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900528 * extcon_register_interest().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900529 * @obj: the extcon_specific_cable_nb object returned by
530 * extcon_register_interest().
531 */
532int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
533{
Hans de Goede66bee352015-03-21 17:26:24 +0100534 unsigned long flags;
535 int ret;
536
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900537 if (!obj)
538 return -EINVAL;
539
Hans de Goede66bee352015-03-21 17:26:24 +0100540 spin_lock_irqsave(&obj->edev->lock, flags);
541 ret = raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
542 spin_unlock_irqrestore(&obj->edev->lock, flags);
543
544 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900545}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900546EXPORT_SYMBOL_GPL(extcon_unregister_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900547
Donggeun Kim74c5d092012-04-20 14:16:24 +0900548/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900549 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900550 * any attach status changes from the extcon.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900551 * @edev: the extcon device.
552 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900553 *
554 * Note that the second parameter given to the callback of nb (val) is
555 * "old_state", not the current state. The current state can be retrieved
556 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900557 */
558int extcon_register_notifier(struct extcon_dev *edev,
559 struct notifier_block *nb)
560{
Hans de Goede66bee352015-03-21 17:26:24 +0100561 unsigned long flags;
562 int ret;
563
564 spin_lock_irqsave(&edev->lock, flags);
565 ret = raw_notifier_chain_register(&edev->nh, nb);
566 spin_unlock_irqrestore(&edev->lock, flags);
567
568 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900569}
570EXPORT_SYMBOL_GPL(extcon_register_notifier);
571
572/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900573 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900574 * @edev: the extcon device.
575 * @nb: a registered notifier block to be unregistered.
576 */
577int extcon_unregister_notifier(struct extcon_dev *edev,
578 struct notifier_block *nb)
579{
Hans de Goede66bee352015-03-21 17:26:24 +0100580 unsigned long flags;
581 int ret;
582
583 spin_lock_irqsave(&edev->lock, flags);
584 ret = raw_notifier_chain_unregister(&edev->nh, nb);
585 spin_unlock_irqrestore(&edev->lock, flags);
586
587 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900588}
589EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
590
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700591static struct attribute *extcon_attrs[] = {
592 &dev_attr_state.attr,
593 &dev_attr_name.attr,
594 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900595};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700596ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900597
598static int create_extcon_class(void)
599{
600 if (!extcon_class) {
601 extcon_class = class_create(THIS_MODULE, "extcon");
602 if (IS_ERR(extcon_class))
603 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700604 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900605
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900606#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900607 switch_class = class_compat_register("switch");
608 if (WARN(!switch_class, "cannot allocate"))
609 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900610#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900611 }
612
613 return 0;
614}
615
MyungJoo Hamde55d872012-04-20 14:16:22 +0900616static void extcon_dev_release(struct device *dev)
617{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900618}
619
MyungJoo Hambde68e62012-04-20 14:16:26 +0900620static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900621static void dummy_sysfs_dev_release(struct device *dev)
622{
623}
624
Chanwoo Choia9af6522014-04-24 19:46:49 +0900625/*
626 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900627 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900628 * If supported_cable is NULL, cable name related APIs
629 * are disabled.
630 *
631 * This function allocates the memory for extcon device without allocating
632 * memory in each extcon provider driver and initialize default setting for
633 * extcon device.
634 *
635 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
636 */
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900637struct extcon_dev *extcon_dev_allocate(const enum extcon *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +0900638{
639 struct extcon_dev *edev;
640
641 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
642 if (!edev)
643 return ERR_PTR(-ENOMEM);
644
645 edev->max_supported = 0;
646 edev->supported_cable = supported_cable;
647
648 return edev;
649}
650
651/*
652 * extcon_dev_free() - Free the memory of extcon device.
653 * @edev: the extcon device to free
654 */
655void extcon_dev_free(struct extcon_dev *edev)
656{
657 kfree(edev);
658}
659EXPORT_SYMBOL_GPL(extcon_dev_free);
660
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900661static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
662{
663 struct extcon_dev **r = res;
664
665 if (WARN_ON(!r || !*r))
666 return 0;
667
668 return *r == data;
669}
670
671static void devm_extcon_dev_release(struct device *dev, void *res)
672{
673 extcon_dev_free(*(struct extcon_dev **)res);
674}
675
676/**
677 * devm_extcon_dev_allocate - Allocate managed extcon device
678 * @dev: device owning the extcon device being created
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900679 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900680 * If supported_cable is NULL, cable name related APIs
681 * are disabled.
682 *
683 * This function manages automatically the memory of extcon device using device
684 * resource management and simplify the control of freeing the memory of extcon
685 * device.
686 *
687 * Returns the pointer memory of allocated extcon_dev if success
688 * or ERR_PTR(err) if fail
689 */
690struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900691 const enum extcon *supported_cable)
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900692{
693 struct extcon_dev **ptr, *edev;
694
695 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
696 if (!ptr)
697 return ERR_PTR(-ENOMEM);
698
699 edev = extcon_dev_allocate(supported_cable);
700 if (IS_ERR(edev)) {
701 devres_free(ptr);
702 return edev;
703 }
704
Chanwoo Choiac65a622014-05-30 10:13:15 +0900705 edev->dev.parent = dev;
706
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900707 *ptr = edev;
708 devres_add(dev, ptr);
709
710 return edev;
711}
712EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
713
714void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
715{
716 WARN_ON(devres_release(dev, devm_extcon_dev_release,
717 devm_extcon_dev_match, edev));
718}
719EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
720
MyungJoo Hamde55d872012-04-20 14:16:22 +0900721/**
722 * extcon_dev_register() - Register a new extcon device
723 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900724 *
725 * Among the members of edev struct, please set the "user initializing data"
726 * in any case and set the "optional callbacks" if required. However, please
727 * do not set the values of "internal data", which are initialized by
728 * this function.
729 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900730int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900731{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900732 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900733 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900734
735 if (!extcon_class) {
736 ret = create_extcon_class();
737 if (ret < 0)
738 return ret;
739 }
740
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900741 if (!edev->supported_cable)
742 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900743
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900744 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
745
746 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900747 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900748 dev_err(&edev->dev,
749 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900750 return -EINVAL;
751 }
752
Chanwoo Choidae61652013-09-27 09:19:40 +0900753 edev->dev.class = extcon_class;
754 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900755
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900756 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900757 if (IS_ERR_OR_NULL(edev->name)) {
758 dev_err(&edev->dev,
759 "extcon device name is null\n");
760 return -EINVAL;
761 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900762 dev_set_name(&edev->dev, "extcon%lu",
763 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900764
765 if (edev->max_supported) {
766 char buf[10];
767 char *str;
768 struct extcon_cable *cable;
769
770 edev->cables = kzalloc(sizeof(struct extcon_cable) *
771 edev->max_supported, GFP_KERNEL);
772 if (!edev->cables) {
773 ret = -ENOMEM;
774 goto err_sysfs_alloc;
775 }
776 for (index = 0; index < edev->max_supported; index++) {
777 cable = &edev->cables[index];
778
779 snprintf(buf, 10, "cable.%d", index);
780 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
781 GFP_KERNEL);
782 if (!str) {
783 for (index--; index >= 0; index--) {
784 cable = &edev->cables[index];
785 kfree(cable->attr_g.name);
786 }
787 ret = -ENOMEM;
788
789 goto err_alloc_cables;
790 }
791 strcpy(str, buf);
792
793 cable->edev = edev;
794 cable->cable_index = index;
795 cable->attrs[0] = &cable->attr_name.attr;
796 cable->attrs[1] = &cable->attr_state.attr;
797 cable->attrs[2] = NULL;
798 cable->attr_g.name = str;
799 cable->attr_g.attrs = cable->attrs;
800
Mark Brown9baf3222012-08-16 20:03:21 +0100801 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900802 cable->attr_name.attr.name = "name";
803 cable->attr_name.attr.mode = 0444;
804 cable->attr_name.show = cable_name_show;
805
Mark Brown9baf3222012-08-16 20:03:21 +0100806 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900807 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900808 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900809 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900810 }
811 }
812
MyungJoo Hambde68e62012-04-20 14:16:26 +0900813 if (edev->max_supported && edev->mutually_exclusive) {
814 char buf[80];
815 char *name;
816
817 /* Count the size of mutually_exclusive array */
818 for (index = 0; edev->mutually_exclusive[index]; index++)
819 ;
820
821 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
822 (index + 1), GFP_KERNEL);
823 if (!edev->attrs_muex) {
824 ret = -ENOMEM;
825 goto err_muex;
826 }
827
828 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
829 index, GFP_KERNEL);
830 if (!edev->d_attrs_muex) {
831 ret = -ENOMEM;
832 kfree(edev->attrs_muex);
833 goto err_muex;
834 }
835
836 for (index = 0; edev->mutually_exclusive[index]; index++) {
837 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
838 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
839 GFP_KERNEL);
840 if (!name) {
841 for (index--; index >= 0; index--) {
842 kfree(edev->d_attrs_muex[index].attr.
843 name);
844 }
845 kfree(edev->d_attrs_muex);
846 kfree(edev->attrs_muex);
847 ret = -ENOMEM;
848 goto err_muex;
849 }
850 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100851 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900852 edev->d_attrs_muex[index].attr.name = name;
853 edev->d_attrs_muex[index].attr.mode = 0000;
854 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
855 .attr;
856 }
857 edev->attr_g_muex.name = muex_name;
858 edev->attr_g_muex.attrs = edev->attrs_muex;
859
860 }
861
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900862 if (edev->max_supported) {
863 edev->extcon_dev_type.groups =
864 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900865 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900866 if (!edev->extcon_dev_type.groups) {
867 ret = -ENOMEM;
868 goto err_alloc_groups;
869 }
870
Chanwoo Choidae61652013-09-27 09:19:40 +0900871 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900872 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
873
874 for (index = 0; index < edev->max_supported; index++)
875 edev->extcon_dev_type.groups[index] =
876 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900877 if (edev->mutually_exclusive)
878 edev->extcon_dev_type.groups[index] =
879 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900880
Chanwoo Choidae61652013-09-27 09:19:40 +0900881 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900882 }
883
Chanwoo Choidae61652013-09-27 09:19:40 +0900884 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900885 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900886 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900887 goto err_dev;
888 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900889#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900890 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900891 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900892#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900893
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900894 spin_lock_init(&edev->lock);
895
Donggeun Kim74c5d092012-04-20 14:16:24 +0900896 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
897
Chanwoo Choidae61652013-09-27 09:19:40 +0900898 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900899 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900900
901 mutex_lock(&extcon_dev_list_lock);
902 list_add(&edev->entry, &extcon_dev_list);
903 mutex_unlock(&extcon_dev_list_lock);
904
MyungJoo Hamde55d872012-04-20 14:16:22 +0900905 return 0;
906
907err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900908 if (edev->max_supported)
909 kfree(edev->extcon_dev_type.groups);
910err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900911 if (edev->max_supported && edev->mutually_exclusive) {
912 for (index = 0; edev->mutually_exclusive[index]; index++)
913 kfree(edev->d_attrs_muex[index].attr.name);
914 kfree(edev->d_attrs_muex);
915 kfree(edev->attrs_muex);
916 }
917err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900918 for (index = 0; index < edev->max_supported; index++)
919 kfree(edev->cables[index].attr_g.name);
920err_alloc_cables:
921 if (edev->max_supported)
922 kfree(edev->cables);
923err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900924 return ret;
925}
926EXPORT_SYMBOL_GPL(extcon_dev_register);
927
928/**
929 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900930 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900931 *
932 * Note that this does not call kfree(edev) because edev was not allocated
933 * by this class.
934 */
935void extcon_dev_unregister(struct extcon_dev *edev)
936{
anish kumar57e7cd32012-10-22 09:43:33 +0900937 int index;
938
939 mutex_lock(&extcon_dev_list_lock);
940 list_del(&edev->entry);
941 mutex_unlock(&extcon_dev_list_lock);
942
Chanwoo Choidae61652013-09-27 09:19:40 +0900943 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
944 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
945 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +0900946 return;
947 }
948
Wang, Xiaoming7585ca02013-11-01 18:48:14 -0400949 device_unregister(&edev->dev);
950
anish kumar57e7cd32012-10-22 09:43:33 +0900951 if (edev->mutually_exclusive && edev->max_supported) {
952 for (index = 0; edev->mutually_exclusive[index];
953 index++)
954 kfree(edev->d_attrs_muex[index].attr.name);
955 kfree(edev->d_attrs_muex);
956 kfree(edev->attrs_muex);
957 }
958
959 for (index = 0; index < edev->max_supported; index++)
960 kfree(edev->cables[index].attr_g.name);
961
962 if (edev->max_supported) {
963 kfree(edev->extcon_dev_type.groups);
964 kfree(edev->cables);
965 }
966
967#if defined(CONFIG_ANDROID)
968 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900969 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +0900970#endif
Chanwoo Choidae61652013-09-27 09:19:40 +0900971 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900972}
973EXPORT_SYMBOL_GPL(extcon_dev_unregister);
974
Sangjung Woo11112442014-04-21 19:10:08 +0900975static void devm_extcon_dev_unreg(struct device *dev, void *res)
976{
977 extcon_dev_unregister(*(struct extcon_dev **)res);
978}
979
Sangjung Woo11112442014-04-21 19:10:08 +0900980/**
981 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
982 * @dev: device to allocate extcon device
983 * @edev: the new extcon device to register
984 *
985 * Managed extcon_dev_register() function. If extcon device is attached with
986 * this function, that extcon device is automatically unregistered on driver
987 * detach. Internally this function calls extcon_dev_register() function.
988 * To get more information, refer that function.
989 *
990 * If extcon device is registered with this function and the device needs to be
991 * unregistered separately, devm_extcon_dev_unregister() should be used.
992 *
993 * Returns 0 if success or negaive error number if failure.
994 */
995int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
996{
997 struct extcon_dev **ptr;
998 int ret;
999
1000 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
1001 if (!ptr)
1002 return -ENOMEM;
1003
1004 ret = extcon_dev_register(edev);
1005 if (ret) {
1006 devres_free(ptr);
1007 return ret;
1008 }
1009
1010 *ptr = edev;
1011 devres_add(dev, ptr);
1012
1013 return 0;
1014}
1015EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
1016
1017/**
1018 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1019 * @dev: device the extcon belongs to
1020 * @edev: the extcon device to unregister
1021 *
1022 * Unregister extcon device that is registered with devm_extcon_dev_register()
1023 * function.
1024 */
1025void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
1026{
1027 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1028 devm_extcon_dev_match, edev));
1029}
1030EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1031
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001032#ifdef CONFIG_OF
1033/*
1034 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1035 * @dev - instance to the given device
1036 * @index - index into list of extcon_dev
1037 *
1038 * return the instance of extcon device
1039 */
1040struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1041{
1042 struct device_node *node;
1043 struct extcon_dev *edev;
1044
1045 if (!dev->of_node) {
1046 dev_err(dev, "device does not have a device node entry\n");
1047 return ERR_PTR(-EINVAL);
1048 }
1049
1050 node = of_parse_phandle(dev->of_node, "extcon", index);
1051 if (!node) {
1052 dev_err(dev, "failed to get phandle in %s node\n",
1053 dev->of_node->full_name);
1054 return ERR_PTR(-ENODEV);
1055 }
1056
Tomasz Figaf841afb2014-10-16 15:11:44 +02001057 mutex_lock(&extcon_dev_list_lock);
1058 list_for_each_entry(edev, &extcon_dev_list, entry) {
1059 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1060 mutex_unlock(&extcon_dev_list_lock);
1061 return edev;
1062 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001063 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001064 mutex_unlock(&extcon_dev_list_lock);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001065
Tomasz Figaf841afb2014-10-16 15:11:44 +02001066 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001067}
1068#else
1069struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1070{
1071 return ERR_PTR(-ENOSYS);
1072}
1073#endif /* CONFIG_OF */
1074EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1075
Chanwoo Choi707d7552015-04-15 13:57:51 +09001076/**
1077 * extcon_get_edev_name() - Get the name of the extcon device.
1078 * @edev: the extcon device
1079 */
1080const char *extcon_get_edev_name(struct extcon_dev *edev)
1081{
1082 return !edev ? NULL : edev->name;
1083}
1084
MyungJoo Hamde55d872012-04-20 14:16:22 +09001085static int __init extcon_class_init(void)
1086{
1087 return create_extcon_class();
1088}
1089module_init(extcon_class_init);
1090
1091static void __exit extcon_class_exit(void)
1092{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001093#if defined(CONFIG_ANDROID)
1094 class_compat_unregister(switch_class);
1095#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001096 class_destroy(extcon_class);
1097}
1098module_exit(extcon_class_exit);
1099
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001100MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001101MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1102MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1103MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1104MODULE_DESCRIPTION("External connector (extcon) class driver");
1105MODULE_LICENSE("GPL");