blob: a07addde297be4576a9306d170feb5e9b910a6d8 [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 Choi73b6ecd2015-06-12 11:10:06 +090042 [EXTCON_NONE] = "NONE",
43
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090044 /* USB external connector */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090045 [EXTCON_USB] = "USB",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090046 [EXTCON_USB_HOST] = "USB-HOST",
47
48 /* Charger external connector */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090049 [EXTCON_TA] = "TA",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090050 [EXTCON_FAST_CHARGER] = "FAST-CHARGER",
51 [EXTCON_SLOW_CHARGER] = "SLOW-CHARGER",
52 [EXTCON_CHARGE_DOWNSTREAM] = "CHARGE-DOWNSTREAM",
53
54 /* Audio/Video external connector */
55 [EXTCON_LINE_IN] = "LINE-IN",
56 [EXTCON_LINE_OUT] = "LINE-OUT",
57 [EXTCON_MICROPHONE] = "MICROPHONE",
58 [EXTCON_HEADPHONE] = "HEADPHONE",
59
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090060 [EXTCON_HDMI] = "HDMI",
61 [EXTCON_MHL] = "MHL",
62 [EXTCON_DVI] = "DVI",
63 [EXTCON_VGA] = "VGA",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090064 [EXTCON_SPDIF_IN] = "SPDIF-IN",
65 [EXTCON_SPDIF_OUT] = "SPDIF-OUT",
66 [EXTCON_VIDEO_IN] = "VIDEO-IN",
67 [EXTCON_VIDEO_OUT] = "VIDEO-OUT",
68
69 /* Etc external connector */
70 [EXTCON_DOCK] = "DOCK",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090071 [EXTCON_JIG] = "JIG",
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090072 [EXTCON_MECHANICAL] = "MECHANICAL",
73
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090074 NULL,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090075};
76
Mark Brownbe3a07f2012-06-05 16:14:38 +010077static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090078#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +090079static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090080#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +090081
Donggeun Kim74c5d092012-04-20 14:16:24 +090082static LIST_HEAD(extcon_dev_list);
83static DEFINE_MUTEX(extcon_dev_list_lock);
84
MyungJoo Hambde68e62012-04-20 14:16:26 +090085/**
86 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +090087 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +090088 * @edev: the extcon device
89 * @new_state: new cable attach status for @edev
90 *
91 * Returns 0 if nothing violates. Returns the index + 1 for the first
92 * violated condition.
93 */
94static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
95{
96 int i = 0;
97
98 if (!edev->mutually_exclusive)
99 return 0;
100
101 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530102 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900103 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900104
anish kumar28c0ada2012-08-30 00:35:10 +0530105 /* calculate the total number of bits set */
106 weight = hweight32(correspondants);
107 if (weight > 1)
108 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900109 }
110
111 return 0;
112}
113
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900114static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900115{
116 int i;
117
118 /* Find the the index of extcon cable in edev->supported_cable */
119 for (i = 0; i < edev->max_supported; i++) {
120 if (edev->supported_cable[i] == id)
121 return i;
122 }
123
124 return -EINVAL;
125}
126
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300127static int find_cable_id_by_name(struct extcon_dev *edev, const char *name)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900128{
Dan Carpentera598af72015-08-04 10:47:23 +0300129 int id = -EINVAL;
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900130 int i = 0;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900131
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300132 /* Find the id of extcon cable */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900133 while (extcon_name[i]) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900134 if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
135 id = i;
136 break;
137 }
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300138 i++;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900139 }
140
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300141 return id;
142}
143
144static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
145{
Dan Carpentera598af72015-08-04 10:47:23 +0300146 int id;
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300147
148 if (edev->max_supported == 0)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900149 return -EINVAL;
150
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300151 /* Find the the number of extcon cable */
152 id = find_cable_id_by_name(edev, name);
153 if (id < 0)
154 return id;
155
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900156 return find_cable_index_by_id(edev, id);
157}
158
Chanwoo Choi046050f2015-05-19 20:01:12 +0900159static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
160{
161 if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
162 *attached = new ? true : false;
163 return true;
164 }
165
166 return false;
167}
168
MyungJoo Hamde55d872012-04-20 14:16:22 +0900169static ssize_t state_show(struct device *dev, struct device_attribute *attr,
170 char *buf)
171{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900172 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900173 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900174
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900175 if (edev->max_supported == 0)
176 return sprintf(buf, "%u\n", edev->state);
177
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900178 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900179 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900180 extcon_name[edev->supported_cable[i]],
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900181 !!(edev->state & (1 << i)));
182 }
183
184 return count;
185}
186
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900187static ssize_t state_store(struct device *dev, struct device_attribute *attr,
188 const char *buf, size_t count)
189{
190 u32 state;
191 ssize_t ret = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900192 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900193
194 ret = sscanf(buf, "0x%x", &state);
195 if (ret == 0)
196 ret = -EINVAL;
197 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900198 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900199
200 if (ret < 0)
201 return ret;
202
203 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900204}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700205static DEVICE_ATTR_RW(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900206
207static ssize_t name_show(struct device *dev, struct device_attribute *attr,
208 char *buf)
209{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900210 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900211
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900212 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900213}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700214static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900215
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900216static ssize_t cable_name_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
218{
219 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
220 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900221 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900222
223 return sprintf(buf, "%s\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900224 extcon_name[cable->edev->supported_cable[i]]);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900225}
226
227static ssize_t cable_state_show(struct device *dev,
228 struct device_attribute *attr, char *buf)
229{
230 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
231 attr_state);
232
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300233 int i = cable->cable_index;
234
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900235 return sprintf(buf, "%d\n",
236 extcon_get_cable_state_(cable->edev,
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300237 cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900238}
239
MyungJoo Hamde55d872012-04-20 14:16:22 +0900240/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900241 * extcon_update_state() - Update the cable attach states of the extcon device
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900242 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900243 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900244 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900245 * @state: new cable attach status for @edev
246 *
247 * Changing the state sends uevent with environment variable containing
248 * the name of extcon device (envp[0]) and the state output (envp[1]).
249 * Tizen uses this format for extcon device to get events from ports.
250 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900251 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900252 * Note that the notifier provides which bits are changed in the state
253 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900254 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900255int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900256{
257 char name_buf[120];
258 char state_buf[120];
259 char *prop_buf;
260 char *envp[3];
261 int env_offset = 0;
262 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900263 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900264 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900265 bool attached;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900266
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900267 if (!edev)
268 return -EINVAL;
269
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900270 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900271
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900272 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
Roger Quadrosf7a89812015-07-06 17:46:58 +0300273 u32 old_state;
274
MyungJoo Hambde68e62012-04-20 14:16:26 +0900275 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
276 (state & mask))) {
277 spin_unlock_irqrestore(&edev->lock, flags);
278 return -EPERM;
279 }
280
Roger Quadrosf7a89812015-07-06 17:46:58 +0300281 old_state = edev->state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900282 edev->state &= ~mask;
283 edev->state |= state & mask;
284
Roger Quadrosf7a89812015-07-06 17:46:58 +0300285 for (index = 0; index < edev->max_supported; index++) {
286 if (is_extcon_changed(old_state, edev->state, index,
287 &attached))
288 raw_notifier_call_chain(&edev->nh[index],
289 attached, edev);
290 }
291
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900292 /* This could be in interrupt handler */
293 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900294 if (prop_buf) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900295 length = name_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900296 if (length > 0) {
297 if (prop_buf[length - 1] == '\n')
298 prop_buf[length - 1] = 0;
299 snprintf(name_buf, sizeof(name_buf),
300 "NAME=%s", prop_buf);
301 envp[env_offset++] = name_buf;
302 }
Chanwoo Choidae61652013-09-27 09:19:40 +0900303 length = state_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900304 if (length > 0) {
305 if (prop_buf[length - 1] == '\n')
306 prop_buf[length - 1] = 0;
307 snprintf(state_buf, sizeof(state_buf),
308 "STATE=%s", prop_buf);
309 envp[env_offset++] = state_buf;
310 }
311 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900312 /* Unlock early before uevent */
313 spin_unlock_irqrestore(&edev->lock, flags);
314
Chanwoo Choidae61652013-09-27 09:19:40 +0900315 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900316 free_page((unsigned long)prop_buf);
317 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900318 /* Unlock early before uevent */
319 spin_unlock_irqrestore(&edev->lock, flags);
320
Chanwoo Choidae61652013-09-27 09:19:40 +0900321 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
322 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900323 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900324 } else {
325 /* No changes */
326 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900327 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900328
329 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900330}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900331EXPORT_SYMBOL_GPL(extcon_update_state);
332
333/**
334 * extcon_set_state() - Set the cable attach states of the extcon device.
335 * @edev: the extcon device
336 * @state: new cable attach status for @edev
337 *
338 * Note that notifier provides which bits are changed in the state
339 * variable with the val parameter (second) to the callback.
340 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900341int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900342{
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900343 if (!edev)
344 return -EINVAL;
345
MyungJoo Hambde68e62012-04-20 14:16:26 +0900346 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900347}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900348EXPORT_SYMBOL_GPL(extcon_set_state);
349
Donggeun Kim74c5d092012-04-20 14:16:24 +0900350/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900351 * extcon_get_cable_state_() - Get the status of a specific cable.
352 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900353 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900354 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900355int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900356{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900357 int index;
358
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900359 if (!edev)
360 return -EINVAL;
361
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900362 index = find_cable_index_by_id(edev, id);
363 if (index < 0)
364 return index;
365
366 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900367 return -EINVAL;
368
369 return !!(edev->state & (1 << index));
370}
371EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
372
373/**
374 * extcon_get_cable_state() - Get the status of a specific cable.
375 * @edev: the extcon device that has the cable.
376 * @cable_name: cable name.
377 *
378 * Note that this is slower than extcon_get_cable_state_.
379 */
380int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
381{
Dan Carpentera598af72015-08-04 10:47:23 +0300382 int id;
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300383
384 id = find_cable_id_by_name(edev, cable_name);
385 if (id < 0)
386 return id;
387
388 return extcon_get_cable_state_(edev, id);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900389}
390EXPORT_SYMBOL_GPL(extcon_get_cable_state);
391
392/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900393 * extcon_set_cable_state_() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900394 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900395 * @id: the unique id of each external connector
396 * in extcon enumeration.
397 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900398 * true: attached / false: detached.
399 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900400int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900401 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900402{
403 u32 state;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900404 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900405
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900406 if (!edev)
407 return -EINVAL;
408
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900409 index = find_cable_index_by_id(edev, id);
410 if (index < 0)
411 return index;
412
413 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900414 return -EINVAL;
415
416 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900417 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900418}
419EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
420
421/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900422 * extcon_set_cable_state() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900423 * @edev: the extcon device that has the cable.
424 * @cable_name: cable name.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900425 * @cable_state: the new cable status. The default semantics is
426 * true: attached / false: detached.
427 *
428 * Note that this is slower than extcon_set_cable_state_.
429 */
430int extcon_set_cable_state(struct extcon_dev *edev,
431 const char *cable_name, bool cable_state)
432{
Dan Carpentera598af72015-08-04 10:47:23 +0300433 int id;
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300434
435 id = find_cable_id_by_name(edev, cable_name);
436 if (id < 0)
437 return id;
438
439 return extcon_set_cable_state_(edev, id, cable_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900440}
441EXPORT_SYMBOL_GPL(extcon_set_cable_state);
442
443/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900444 * extcon_get_extcon_dev() - Get the extcon device instance from the name
445 * @extcon_name: The extcon name provided with extcon_dev_register()
446 */
447struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
448{
449 struct extcon_dev *sd;
450
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900451 if (!extcon_name)
452 return ERR_PTR(-EINVAL);
453
Donggeun Kim74c5d092012-04-20 14:16:24 +0900454 mutex_lock(&extcon_dev_list_lock);
455 list_for_each_entry(sd, &extcon_dev_list, entry) {
456 if (!strcmp(sd->name, extcon_name))
457 goto out;
458 }
459 sd = NULL;
460out:
461 mutex_unlock(&extcon_dev_list_lock);
462 return sd;
463}
464EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
465
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900466/**
467 * extcon_register_interest() - Register a notifier for a state change of a
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900468 * specific cable, not an entier set of cables of a
469 * extcon device.
470 * @obj: an empty extcon_specific_cable_nb object to be returned.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900471 * @extcon_name: the name of extcon device.
Jenny TC4f2de3b2012-10-18 21:00:32 +0900472 * if NULL, extcon_register_interest will register
473 * every cable with the target cable_name given.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900474 * @cable_name: the target cable name.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900475 * @nb: the notifier block to get notified.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900476 *
477 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
478 * the struct for you.
479 *
480 * extcon_register_interest is a helper function for those who want to get
481 * notification for a single specific cable's status change. If a user wants
482 * to get notification for any changes of all cables of a extcon device,
483 * he/she should use the general extcon_register_notifier().
484 *
485 * Note that the second parameter given to the callback of nb (val) is
486 * "old_state", not the current state. The current state can be retrieved
487 * by looking at the third pameter (edev pointer)'s state value.
488 */
489int extcon_register_interest(struct extcon_specific_cable_nb *obj,
490 const char *extcon_name, const char *cable_name,
491 struct notifier_block *nb)
492{
Hans de Goede66bee352015-03-21 17:26:24 +0100493 unsigned long flags;
494 int ret;
495
Jenny TC4f2de3b2012-10-18 21:00:32 +0900496 if (!obj || !cable_name || !nb)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900497 return -EINVAL;
498
Jenny TC4f2de3b2012-10-18 21:00:32 +0900499 if (extcon_name) {
500 obj->edev = extcon_get_extcon_dev(extcon_name);
501 if (!obj->edev)
502 return -ENODEV;
503
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900504 obj->cable_index = find_cable_index_by_name(obj->edev,
505 cable_name);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900506 if (obj->cable_index < 0)
Sachin Kamatc0c078c2012-11-20 16:30:31 +0900507 return obj->cable_index;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900508
509 obj->user_nb = nb;
510
Hans de Goede66bee352015-03-21 17:26:24 +0100511 spin_lock_irqsave(&obj->edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900512 ret = raw_notifier_chain_register(
513 &obj->edev->nh[obj->cable_index],
514 obj->user_nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100515 spin_unlock_irqrestore(&obj->edev->lock, flags);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900516 } else {
517 struct class_dev_iter iter;
518 struct extcon_dev *extd;
519 struct device *dev;
520
521 if (!extcon_class)
522 return -ENODEV;
523 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
524 while ((dev = class_dev_iter_next(&iter))) {
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900525 extd = dev_get_drvdata(dev);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900526
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900527 if (find_cable_index_by_name(extd, cable_name) < 0)
Jenny TC4f2de3b2012-10-18 21:00:32 +0900528 continue;
529
530 class_dev_iter_exit(&iter);
531 return extcon_register_interest(obj, extd->name,
532 cable_name, nb);
533 }
534
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900535 ret = -ENODEV;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900536 }
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900537
538 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900539}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900540EXPORT_SYMBOL_GPL(extcon_register_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900541
542/**
543 * extcon_unregister_interest() - Unregister the notifier registered by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900544 * extcon_register_interest().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900545 * @obj: the extcon_specific_cable_nb object returned by
546 * extcon_register_interest().
547 */
548int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
549{
Hans de Goede66bee352015-03-21 17:26:24 +0100550 unsigned long flags;
551 int ret;
552
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900553 if (!obj)
554 return -EINVAL;
555
Hans de Goede66bee352015-03-21 17:26:24 +0100556 spin_lock_irqsave(&obj->edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900557 ret = raw_notifier_chain_unregister(
558 &obj->edev->nh[obj->cable_index], obj->user_nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100559 spin_unlock_irqrestore(&obj->edev->lock, flags);
560
561 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900562}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900563EXPORT_SYMBOL_GPL(extcon_unregister_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900564
Donggeun Kim74c5d092012-04-20 14:16:24 +0900565/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900566 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900567 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900568 * @edev: the extcon device that has the external connecotr.
569 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900570 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900571 *
572 * Note that the second parameter given to the callback of nb (val) is
573 * "old_state", not the current state. The current state can be retrieved
574 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900575 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900576int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900577 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900578{
Hans de Goede66bee352015-03-21 17:26:24 +0100579 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900580 int ret, idx;
581
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900582 if (!edev || !nb)
583 return -EINVAL;
584
Chanwoo Choi046050f2015-05-19 20:01:12 +0900585 idx = find_cable_index_by_id(edev, id);
Hans de Goede66bee352015-03-21 17:26:24 +0100586
587 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900588 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100589 spin_unlock_irqrestore(&edev->lock, flags);
590
591 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900592}
593EXPORT_SYMBOL_GPL(extcon_register_notifier);
594
595/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900596 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900597 * @edev: the extcon device that has the external connecotr.
598 * @id: the unique id of each external connector in extcon enumeration.
599 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900600 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900601int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900602 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900603{
Hans de Goede66bee352015-03-21 17:26:24 +0100604 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900605 int ret, idx;
606
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900607 if (!edev || !nb)
608 return -EINVAL;
609
Chanwoo Choi046050f2015-05-19 20:01:12 +0900610 idx = find_cable_index_by_id(edev, id);
Hans de Goede66bee352015-03-21 17:26:24 +0100611
612 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900613 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100614 spin_unlock_irqrestore(&edev->lock, flags);
615
616 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900617}
618EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
619
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700620static struct attribute *extcon_attrs[] = {
621 &dev_attr_state.attr,
622 &dev_attr_name.attr,
623 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900624};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700625ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900626
627static int create_extcon_class(void)
628{
629 if (!extcon_class) {
630 extcon_class = class_create(THIS_MODULE, "extcon");
631 if (IS_ERR(extcon_class))
632 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700633 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900634
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900635#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900636 switch_class = class_compat_register("switch");
637 if (WARN(!switch_class, "cannot allocate"))
638 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900639#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900640 }
641
642 return 0;
643}
644
MyungJoo Hamde55d872012-04-20 14:16:22 +0900645static void extcon_dev_release(struct device *dev)
646{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900647}
648
MyungJoo Hambde68e62012-04-20 14:16:26 +0900649static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900650static void dummy_sysfs_dev_release(struct device *dev)
651{
652}
653
Chanwoo Choia9af6522014-04-24 19:46:49 +0900654/*
655 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900656 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900657 * If supported_cable is NULL, cable name related APIs
658 * are disabled.
659 *
660 * This function allocates the memory for extcon device without allocating
661 * memory in each extcon provider driver and initialize default setting for
662 * extcon device.
663 *
664 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
665 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900666struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +0900667{
668 struct extcon_dev *edev;
669
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900670 if (!supported_cable)
671 return ERR_PTR(-EINVAL);
672
Chanwoo Choia9af6522014-04-24 19:46:49 +0900673 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
674 if (!edev)
675 return ERR_PTR(-ENOMEM);
676
677 edev->max_supported = 0;
678 edev->supported_cable = supported_cable;
679
680 return edev;
681}
682
683/*
684 * extcon_dev_free() - Free the memory of extcon device.
685 * @edev: the extcon device to free
686 */
687void extcon_dev_free(struct extcon_dev *edev)
688{
689 kfree(edev);
690}
691EXPORT_SYMBOL_GPL(extcon_dev_free);
692
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900693static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
694{
695 struct extcon_dev **r = res;
696
697 if (WARN_ON(!r || !*r))
698 return 0;
699
700 return *r == data;
701}
702
703static void devm_extcon_dev_release(struct device *dev, void *res)
704{
705 extcon_dev_free(*(struct extcon_dev **)res);
706}
707
708/**
709 * devm_extcon_dev_allocate - Allocate managed extcon device
710 * @dev: device owning the extcon device being created
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900711 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900712 * If supported_cable is NULL, cable name related APIs
713 * are disabled.
714 *
715 * This function manages automatically the memory of extcon device using device
716 * resource management and simplify the control of freeing the memory of extcon
717 * device.
718 *
719 * Returns the pointer memory of allocated extcon_dev if success
720 * or ERR_PTR(err) if fail
721 */
722struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900723 const unsigned int *supported_cable)
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900724{
725 struct extcon_dev **ptr, *edev;
726
727 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
728 if (!ptr)
729 return ERR_PTR(-ENOMEM);
730
731 edev = extcon_dev_allocate(supported_cable);
732 if (IS_ERR(edev)) {
733 devres_free(ptr);
734 return edev;
735 }
736
Chanwoo Choiac65a622014-05-30 10:13:15 +0900737 edev->dev.parent = dev;
738
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900739 *ptr = edev;
740 devres_add(dev, ptr);
741
742 return edev;
743}
744EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
745
746void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
747{
748 WARN_ON(devres_release(dev, devm_extcon_dev_release,
749 devm_extcon_dev_match, edev));
750}
751EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
752
MyungJoo Hamde55d872012-04-20 14:16:22 +0900753/**
754 * extcon_dev_register() - Register a new extcon device
755 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900756 *
757 * Among the members of edev struct, please set the "user initializing data"
758 * in any case and set the "optional callbacks" if required. However, please
759 * do not set the values of "internal data", which are initialized by
760 * this function.
761 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900762int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900763{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900764 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900765 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900766
767 if (!extcon_class) {
768 ret = create_extcon_class();
769 if (ret < 0)
770 return ret;
771 }
772
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900773 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900774 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900775
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900776 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
777
778 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900779 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900780 dev_err(&edev->dev,
781 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900782 return -EINVAL;
783 }
784
Chanwoo Choidae61652013-09-27 09:19:40 +0900785 edev->dev.class = extcon_class;
786 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900787
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900788 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900789 if (IS_ERR_OR_NULL(edev->name)) {
790 dev_err(&edev->dev,
791 "extcon device name is null\n");
792 return -EINVAL;
793 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900794 dev_set_name(&edev->dev, "extcon%lu",
795 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900796
797 if (edev->max_supported) {
798 char buf[10];
799 char *str;
800 struct extcon_cable *cable;
801
802 edev->cables = kzalloc(sizeof(struct extcon_cable) *
803 edev->max_supported, GFP_KERNEL);
804 if (!edev->cables) {
805 ret = -ENOMEM;
806 goto err_sysfs_alloc;
807 }
808 for (index = 0; index < edev->max_supported; index++) {
809 cable = &edev->cables[index];
810
811 snprintf(buf, 10, "cable.%d", index);
812 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
813 GFP_KERNEL);
814 if (!str) {
815 for (index--; index >= 0; index--) {
816 cable = &edev->cables[index];
817 kfree(cable->attr_g.name);
818 }
819 ret = -ENOMEM;
820
821 goto err_alloc_cables;
822 }
823 strcpy(str, buf);
824
825 cable->edev = edev;
826 cable->cable_index = index;
827 cable->attrs[0] = &cable->attr_name.attr;
828 cable->attrs[1] = &cable->attr_state.attr;
829 cable->attrs[2] = NULL;
830 cable->attr_g.name = str;
831 cable->attr_g.attrs = cable->attrs;
832
Mark Brown9baf3222012-08-16 20:03:21 +0100833 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900834 cable->attr_name.attr.name = "name";
835 cable->attr_name.attr.mode = 0444;
836 cable->attr_name.show = cable_name_show;
837
Mark Brown9baf3222012-08-16 20:03:21 +0100838 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900839 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900840 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900841 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900842 }
843 }
844
MyungJoo Hambde68e62012-04-20 14:16:26 +0900845 if (edev->max_supported && edev->mutually_exclusive) {
846 char buf[80];
847 char *name;
848
849 /* Count the size of mutually_exclusive array */
850 for (index = 0; edev->mutually_exclusive[index]; index++)
851 ;
852
853 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
854 (index + 1), GFP_KERNEL);
855 if (!edev->attrs_muex) {
856 ret = -ENOMEM;
857 goto err_muex;
858 }
859
860 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
861 index, GFP_KERNEL);
862 if (!edev->d_attrs_muex) {
863 ret = -ENOMEM;
864 kfree(edev->attrs_muex);
865 goto err_muex;
866 }
867
868 for (index = 0; edev->mutually_exclusive[index]; index++) {
869 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
870 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
871 GFP_KERNEL);
872 if (!name) {
873 for (index--; index >= 0; index--) {
874 kfree(edev->d_attrs_muex[index].attr.
875 name);
876 }
877 kfree(edev->d_attrs_muex);
878 kfree(edev->attrs_muex);
879 ret = -ENOMEM;
880 goto err_muex;
881 }
882 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100883 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900884 edev->d_attrs_muex[index].attr.name = name;
885 edev->d_attrs_muex[index].attr.mode = 0000;
886 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
887 .attr;
888 }
889 edev->attr_g_muex.name = muex_name;
890 edev->attr_g_muex.attrs = edev->attrs_muex;
891
892 }
893
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900894 if (edev->max_supported) {
895 edev->extcon_dev_type.groups =
896 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900897 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900898 if (!edev->extcon_dev_type.groups) {
899 ret = -ENOMEM;
900 goto err_alloc_groups;
901 }
902
Chanwoo Choidae61652013-09-27 09:19:40 +0900903 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900904 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
905
906 for (index = 0; index < edev->max_supported; index++)
907 edev->extcon_dev_type.groups[index] =
908 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900909 if (edev->mutually_exclusive)
910 edev->extcon_dev_type.groups[index] =
911 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900912
Chanwoo Choidae61652013-09-27 09:19:40 +0900913 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900914 }
915
Chanwoo Choidae61652013-09-27 09:19:40 +0900916 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900917 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900918 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900919 goto err_dev;
920 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900921#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900922 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900923 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900924#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900925
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900926 spin_lock_init(&edev->lock);
927
Chanwoo Choi046050f2015-05-19 20:01:12 +0900928 edev->nh = devm_kzalloc(&edev->dev,
929 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
930 if (!edev->nh) {
931 ret = -ENOMEM;
932 goto err_dev;
933 }
934
935 for (index = 0; index < edev->max_supported; index++)
936 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900937
Chanwoo Choidae61652013-09-27 09:19:40 +0900938 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900939 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900940
941 mutex_lock(&extcon_dev_list_lock);
942 list_add(&edev->entry, &extcon_dev_list);
943 mutex_unlock(&extcon_dev_list_lock);
944
MyungJoo Hamde55d872012-04-20 14:16:22 +0900945 return 0;
946
947err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900948 if (edev->max_supported)
949 kfree(edev->extcon_dev_type.groups);
950err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900951 if (edev->max_supported && edev->mutually_exclusive) {
952 for (index = 0; edev->mutually_exclusive[index]; index++)
953 kfree(edev->d_attrs_muex[index].attr.name);
954 kfree(edev->d_attrs_muex);
955 kfree(edev->attrs_muex);
956 }
957err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900958 for (index = 0; index < edev->max_supported; index++)
959 kfree(edev->cables[index].attr_g.name);
960err_alloc_cables:
961 if (edev->max_supported)
962 kfree(edev->cables);
963err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900964 return ret;
965}
966EXPORT_SYMBOL_GPL(extcon_dev_register);
967
968/**
969 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900970 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900971 *
972 * Note that this does not call kfree(edev) because edev was not allocated
973 * by this class.
974 */
975void extcon_dev_unregister(struct extcon_dev *edev)
976{
anish kumar57e7cd32012-10-22 09:43:33 +0900977 int index;
978
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900979 if (!edev)
980 return;
981
anish kumar57e7cd32012-10-22 09:43:33 +0900982 mutex_lock(&extcon_dev_list_lock);
983 list_del(&edev->entry);
984 mutex_unlock(&extcon_dev_list_lock);
985
Chanwoo Choidae61652013-09-27 09:19:40 +0900986 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
987 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
988 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +0900989 return;
990 }
991
Wang, Xiaoming7585ca02013-11-01 18:48:14 -0400992 device_unregister(&edev->dev);
993
anish kumar57e7cd32012-10-22 09:43:33 +0900994 if (edev->mutually_exclusive && edev->max_supported) {
995 for (index = 0; edev->mutually_exclusive[index];
996 index++)
997 kfree(edev->d_attrs_muex[index].attr.name);
998 kfree(edev->d_attrs_muex);
999 kfree(edev->attrs_muex);
1000 }
1001
1002 for (index = 0; index < edev->max_supported; index++)
1003 kfree(edev->cables[index].attr_g.name);
1004
1005 if (edev->max_supported) {
1006 kfree(edev->extcon_dev_type.groups);
1007 kfree(edev->cables);
1008 }
1009
1010#if defined(CONFIG_ANDROID)
1011 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001012 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +09001013#endif
Chanwoo Choidae61652013-09-27 09:19:40 +09001014 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001015}
1016EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1017
Sangjung Woo11112442014-04-21 19:10:08 +09001018static void devm_extcon_dev_unreg(struct device *dev, void *res)
1019{
1020 extcon_dev_unregister(*(struct extcon_dev **)res);
1021}
1022
Sangjung Woo11112442014-04-21 19:10:08 +09001023/**
1024 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
1025 * @dev: device to allocate extcon device
1026 * @edev: the new extcon device to register
1027 *
1028 * Managed extcon_dev_register() function. If extcon device is attached with
1029 * this function, that extcon device is automatically unregistered on driver
1030 * detach. Internally this function calls extcon_dev_register() function.
1031 * To get more information, refer that function.
1032 *
1033 * If extcon device is registered with this function and the device needs to be
1034 * unregistered separately, devm_extcon_dev_unregister() should be used.
1035 *
1036 * Returns 0 if success or negaive error number if failure.
1037 */
1038int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
1039{
1040 struct extcon_dev **ptr;
1041 int ret;
1042
1043 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
1044 if (!ptr)
1045 return -ENOMEM;
1046
1047 ret = extcon_dev_register(edev);
1048 if (ret) {
1049 devres_free(ptr);
1050 return ret;
1051 }
1052
1053 *ptr = edev;
1054 devres_add(dev, ptr);
1055
1056 return 0;
1057}
1058EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
1059
1060/**
1061 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1062 * @dev: device the extcon belongs to
1063 * @edev: the extcon device to unregister
1064 *
1065 * Unregister extcon device that is registered with devm_extcon_dev_register()
1066 * function.
1067 */
1068void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
1069{
1070 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1071 devm_extcon_dev_match, edev));
1072}
1073EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1074
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001075#ifdef CONFIG_OF
1076/*
1077 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1078 * @dev - instance to the given device
1079 * @index - index into list of extcon_dev
1080 *
1081 * return the instance of extcon device
1082 */
1083struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1084{
1085 struct device_node *node;
1086 struct extcon_dev *edev;
1087
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001088 if (!dev)
1089 return ERR_PTR(-EINVAL);
1090
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001091 if (!dev->of_node) {
1092 dev_err(dev, "device does not have a device node entry\n");
1093 return ERR_PTR(-EINVAL);
1094 }
1095
1096 node = of_parse_phandle(dev->of_node, "extcon", index);
1097 if (!node) {
1098 dev_err(dev, "failed to get phandle in %s node\n",
1099 dev->of_node->full_name);
1100 return ERR_PTR(-ENODEV);
1101 }
1102
Tomasz Figaf841afb2014-10-16 15:11:44 +02001103 mutex_lock(&extcon_dev_list_lock);
1104 list_for_each_entry(edev, &extcon_dev_list, entry) {
1105 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1106 mutex_unlock(&extcon_dev_list_lock);
1107 return edev;
1108 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001109 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001110 mutex_unlock(&extcon_dev_list_lock);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001111
Tomasz Figaf841afb2014-10-16 15:11:44 +02001112 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001113}
1114#else
1115struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1116{
1117 return ERR_PTR(-ENOSYS);
1118}
1119#endif /* CONFIG_OF */
1120EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1121
Chanwoo Choi707d7552015-04-15 13:57:51 +09001122/**
1123 * extcon_get_edev_name() - Get the name of the extcon device.
1124 * @edev: the extcon device
1125 */
1126const char *extcon_get_edev_name(struct extcon_dev *edev)
1127{
1128 return !edev ? NULL : edev->name;
1129}
1130
MyungJoo Hamde55d872012-04-20 14:16:22 +09001131static int __init extcon_class_init(void)
1132{
1133 return create_extcon_class();
1134}
1135module_init(extcon_class_init);
1136
1137static void __exit extcon_class_exit(void)
1138{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001139#if defined(CONFIG_ANDROID)
1140 class_compat_unregister(switch_class);
1141#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001142 class_destroy(extcon_class);
1143}
1144module_exit(extcon_class_exit);
1145
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001146MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001147MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1148MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1149MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1150MODULE_DESCRIPTION("External connector (extcon) class driver");
1151MODULE_LICENSE("GPL");