blob: 43b57b02d050d197fe7994ea744231b7a580eb23 [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{
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300129 unsigned 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{
146 unsigned int id;
147
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
175 if (edev->print_state) {
176 int ret = edev->print_state(edev, buf);
177
178 if (ret >= 0)
179 return ret;
180 /* Use default if failed */
181 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900182
183 if (edev->max_supported == 0)
184 return sprintf(buf, "%u\n", edev->state);
185
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900186 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900187 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900188 extcon_name[edev->supported_cable[i]],
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900189 !!(edev->state & (1 << i)));
190 }
191
192 return count;
193}
194
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900195static ssize_t state_store(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
197{
198 u32 state;
199 ssize_t ret = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900200 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900201
202 ret = sscanf(buf, "0x%x", &state);
203 if (ret == 0)
204 ret = -EINVAL;
205 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900206 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900207
208 if (ret < 0)
209 return ret;
210
211 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900212}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700213static DEVICE_ATTR_RW(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900214
215static ssize_t name_show(struct device *dev, struct device_attribute *attr,
216 char *buf)
217{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900218 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900219
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900220 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900221}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700222static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900223
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900224static ssize_t cable_name_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
226{
227 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
228 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900229 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900230
231 return sprintf(buf, "%s\n",
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900232 extcon_name[cable->edev->supported_cable[i]]);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900233}
234
235static ssize_t cable_state_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
237{
238 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
239 attr_state);
240
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300241 int i = cable->cable_index;
242
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900243 return sprintf(buf, "%d\n",
244 extcon_get_cable_state_(cable->edev,
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300245 cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900246}
247
MyungJoo Hamde55d872012-04-20 14:16:22 +0900248/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900249 * extcon_update_state() - Update the cable attach states of the extcon device
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900250 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900251 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900252 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900253 * @state: new cable attach status for @edev
254 *
255 * Changing the state sends uevent with environment variable containing
256 * the name of extcon device (envp[0]) and the state output (envp[1]).
257 * Tizen uses this format for extcon device to get events from ports.
258 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900259 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900260 * Note that the notifier provides which bits are changed in the state
261 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900262 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900263int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900264{
265 char name_buf[120];
266 char state_buf[120];
267 char *prop_buf;
268 char *envp[3];
269 int env_offset = 0;
270 int length;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900271 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900272 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900273 bool attached;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900274
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900275 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900276
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900277 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
Roger Quadrosf7a89812015-07-06 17:46:58 +0300278 u32 old_state;
279
MyungJoo Hambde68e62012-04-20 14:16:26 +0900280 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
281 (state & mask))) {
282 spin_unlock_irqrestore(&edev->lock, flags);
283 return -EPERM;
284 }
285
Roger Quadrosf7a89812015-07-06 17:46:58 +0300286 old_state = edev->state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900287 edev->state &= ~mask;
288 edev->state |= state & mask;
289
Roger Quadrosf7a89812015-07-06 17:46:58 +0300290 for (index = 0; index < edev->max_supported; index++) {
291 if (is_extcon_changed(old_state, edev->state, index,
292 &attached))
293 raw_notifier_call_chain(&edev->nh[index],
294 attached, edev);
295 }
296
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900297 /* This could be in interrupt handler */
298 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900299 if (prop_buf) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900300 length = name_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900301 if (length > 0) {
302 if (prop_buf[length - 1] == '\n')
303 prop_buf[length - 1] = 0;
304 snprintf(name_buf, sizeof(name_buf),
305 "NAME=%s", prop_buf);
306 envp[env_offset++] = name_buf;
307 }
Chanwoo Choidae61652013-09-27 09:19:40 +0900308 length = state_show(&edev->dev, NULL, prop_buf);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900309 if (length > 0) {
310 if (prop_buf[length - 1] == '\n')
311 prop_buf[length - 1] = 0;
312 snprintf(state_buf, sizeof(state_buf),
313 "STATE=%s", prop_buf);
314 envp[env_offset++] = state_buf;
315 }
316 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900317 /* Unlock early before uevent */
318 spin_unlock_irqrestore(&edev->lock, flags);
319
Chanwoo Choidae61652013-09-27 09:19:40 +0900320 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900321 free_page((unsigned long)prop_buf);
322 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900323 /* Unlock early before uevent */
324 spin_unlock_irqrestore(&edev->lock, flags);
325
Chanwoo Choidae61652013-09-27 09:19:40 +0900326 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
327 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900328 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900329 } else {
330 /* No changes */
331 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900332 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900333
334 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900335}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900336EXPORT_SYMBOL_GPL(extcon_update_state);
337
338/**
339 * extcon_set_state() - Set the cable attach states of the extcon device.
340 * @edev: the extcon device
341 * @state: new cable attach status for @edev
342 *
343 * Note that notifier provides which bits are changed in the state
344 * variable with the val parameter (second) to the callback.
345 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900346int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900347{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900348 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900349}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900350EXPORT_SYMBOL_GPL(extcon_set_state);
351
Donggeun Kim74c5d092012-04-20 14:16:24 +0900352/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900353 * extcon_get_cable_state_() - Get the status of a specific cable.
354 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900355 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900356 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900357int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900358{
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900359 int index;
360
361 index = find_cable_index_by_id(edev, id);
362 if (index < 0)
363 return index;
364
365 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900366 return -EINVAL;
367
368 return !!(edev->state & (1 << index));
369}
370EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
371
372/**
373 * extcon_get_cable_state() - Get the status of a specific cable.
374 * @edev: the extcon device that has the cable.
375 * @cable_name: cable name.
376 *
377 * Note that this is slower than extcon_get_cable_state_.
378 */
379int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
380{
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300381 unsigned int id;
382
383 id = find_cable_id_by_name(edev, cable_name);
384 if (id < 0)
385 return id;
386
387 return extcon_get_cable_state_(edev, id);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900388}
389EXPORT_SYMBOL_GPL(extcon_get_cable_state);
390
391/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900392 * extcon_set_cable_state_() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900393 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900394 * @id: the unique id of each external connector
395 * in extcon enumeration.
396 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900397 * true: attached / false: detached.
398 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900399int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900400 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900401{
402 u32 state;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900403 int index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900404
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900405 index = find_cable_index_by_id(edev, id);
406 if (index < 0)
407 return index;
408
409 if (edev->max_supported && edev->max_supported <= index)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900410 return -EINVAL;
411
412 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900413 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900414}
415EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
416
417/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900418 * extcon_set_cable_state() - Set the status of a specific cable.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900419 * @edev: the extcon device that has the cable.
420 * @cable_name: cable name.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900421 * @cable_state: the new cable status. The default semantics is
422 * true: attached / false: detached.
423 *
424 * Note that this is slower than extcon_set_cable_state_.
425 */
426int extcon_set_cable_state(struct extcon_dev *edev,
427 const char *cable_name, bool cable_state)
428{
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300429 unsigned int id;
430
431 id = find_cable_id_by_name(edev, cable_name);
432 if (id < 0)
433 return id;
434
435 return extcon_set_cable_state_(edev, id, cable_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900436}
437EXPORT_SYMBOL_GPL(extcon_set_cable_state);
438
439/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900440 * extcon_get_extcon_dev() - Get the extcon device instance from the name
441 * @extcon_name: The extcon name provided with extcon_dev_register()
442 */
443struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
444{
445 struct extcon_dev *sd;
446
447 mutex_lock(&extcon_dev_list_lock);
448 list_for_each_entry(sd, &extcon_dev_list, entry) {
449 if (!strcmp(sd->name, extcon_name))
450 goto out;
451 }
452 sd = NULL;
453out:
454 mutex_unlock(&extcon_dev_list_lock);
455 return sd;
456}
457EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
458
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900459/**
460 * extcon_register_interest() - Register a notifier for a state change of a
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900461 * specific cable, not an entier set of cables of a
462 * extcon device.
463 * @obj: an empty extcon_specific_cable_nb object to be returned.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900464 * @extcon_name: the name of extcon device.
Jenny TC4f2de3b2012-10-18 21:00:32 +0900465 * if NULL, extcon_register_interest will register
466 * every cable with the target cable_name given.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900467 * @cable_name: the target cable name.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900468 * @nb: the notifier block to get notified.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900469 *
470 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
471 * the struct for you.
472 *
473 * extcon_register_interest is a helper function for those who want to get
474 * notification for a single specific cable's status change. If a user wants
475 * to get notification for any changes of all cables of a extcon device,
476 * he/she should use the general extcon_register_notifier().
477 *
478 * Note that the second parameter given to the callback of nb (val) is
479 * "old_state", not the current state. The current state can be retrieved
480 * by looking at the third pameter (edev pointer)'s state value.
481 */
482int extcon_register_interest(struct extcon_specific_cable_nb *obj,
483 const char *extcon_name, const char *cable_name,
484 struct notifier_block *nb)
485{
Hans de Goede66bee352015-03-21 17:26:24 +0100486 unsigned long flags;
487 int ret;
488
Jenny TC4f2de3b2012-10-18 21:00:32 +0900489 if (!obj || !cable_name || !nb)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900490 return -EINVAL;
491
Jenny TC4f2de3b2012-10-18 21:00:32 +0900492 if (extcon_name) {
493 obj->edev = extcon_get_extcon_dev(extcon_name);
494 if (!obj->edev)
495 return -ENODEV;
496
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900497 obj->cable_index = find_cable_index_by_name(obj->edev,
498 cable_name);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900499 if (obj->cable_index < 0)
Sachin Kamatc0c078c2012-11-20 16:30:31 +0900500 return obj->cable_index;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900501
502 obj->user_nb = nb;
503
Hans de Goede66bee352015-03-21 17:26:24 +0100504 spin_lock_irqsave(&obj->edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900505 ret = raw_notifier_chain_register(
506 &obj->edev->nh[obj->cable_index],
507 obj->user_nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100508 spin_unlock_irqrestore(&obj->edev->lock, flags);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900509 } else {
510 struct class_dev_iter iter;
511 struct extcon_dev *extd;
512 struct device *dev;
513
514 if (!extcon_class)
515 return -ENODEV;
516 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
517 while ((dev = class_dev_iter_next(&iter))) {
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900518 extd = dev_get_drvdata(dev);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900519
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900520 if (find_cable_index_by_name(extd, cable_name) < 0)
Jenny TC4f2de3b2012-10-18 21:00:32 +0900521 continue;
522
523 class_dev_iter_exit(&iter);
524 return extcon_register_interest(obj, extd->name,
525 cable_name, nb);
526 }
527
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900528 ret = -ENODEV;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900529 }
Chanwoo Choib9ec23c2015-04-24 14:48:52 +0900530
531 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900532}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900533EXPORT_SYMBOL_GPL(extcon_register_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900534
535/**
536 * extcon_unregister_interest() - Unregister the notifier registered by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900537 * extcon_register_interest().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900538 * @obj: the extcon_specific_cable_nb object returned by
539 * extcon_register_interest().
540 */
541int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
542{
Hans de Goede66bee352015-03-21 17:26:24 +0100543 unsigned long flags;
544 int ret;
545
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900546 if (!obj)
547 return -EINVAL;
548
Hans de Goede66bee352015-03-21 17:26:24 +0100549 spin_lock_irqsave(&obj->edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900550 ret = raw_notifier_chain_unregister(
551 &obj->edev->nh[obj->cable_index], obj->user_nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100552 spin_unlock_irqrestore(&obj->edev->lock, flags);
553
554 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900555}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900556EXPORT_SYMBOL_GPL(extcon_unregister_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900557
Donggeun Kim74c5d092012-04-20 14:16:24 +0900558/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900559 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900560 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900561 * @edev: the extcon device that has the external connecotr.
562 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900563 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900564 *
565 * Note that the second parameter given to the callback of nb (val) is
566 * "old_state", not the current state. The current state can be retrieved
567 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900568 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900569int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900570 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900571{
Hans de Goede66bee352015-03-21 17:26:24 +0100572 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900573 int ret, idx;
574
575 idx = find_cable_index_by_id(edev, id);
Hans de Goede66bee352015-03-21 17:26:24 +0100576
577 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900578 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100579 spin_unlock_irqrestore(&edev->lock, flags);
580
581 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900582}
583EXPORT_SYMBOL_GPL(extcon_register_notifier);
584
585/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900586 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900587 * @edev: the extcon device that has the external connecotr.
588 * @id: the unique id of each external connector in extcon enumeration.
589 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900590 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900591int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900592 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900593{
Hans de Goede66bee352015-03-21 17:26:24 +0100594 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900595 int ret, idx;
596
597 idx = find_cable_index_by_id(edev, id);
Hans de Goede66bee352015-03-21 17:26:24 +0100598
599 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900600 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100601 spin_unlock_irqrestore(&edev->lock, flags);
602
603 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900604}
605EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
606
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700607static struct attribute *extcon_attrs[] = {
608 &dev_attr_state.attr,
609 &dev_attr_name.attr,
610 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900611};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700612ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900613
614static int create_extcon_class(void)
615{
616 if (!extcon_class) {
617 extcon_class = class_create(THIS_MODULE, "extcon");
618 if (IS_ERR(extcon_class))
619 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700620 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900621
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900622#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900623 switch_class = class_compat_register("switch");
624 if (WARN(!switch_class, "cannot allocate"))
625 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900626#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900627 }
628
629 return 0;
630}
631
MyungJoo Hamde55d872012-04-20 14:16:22 +0900632static void extcon_dev_release(struct device *dev)
633{
MyungJoo Hamde55d872012-04-20 14:16:22 +0900634}
635
MyungJoo Hambde68e62012-04-20 14:16:26 +0900636static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900637static void dummy_sysfs_dev_release(struct device *dev)
638{
639}
640
Chanwoo Choia9af6522014-04-24 19:46:49 +0900641/*
642 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900643 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +0900644 * If supported_cable is NULL, cable name related APIs
645 * are disabled.
646 *
647 * This function allocates the memory for extcon device without allocating
648 * memory in each extcon provider driver and initialize default setting for
649 * extcon device.
650 *
651 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
652 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900653struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +0900654{
655 struct extcon_dev *edev;
656
657 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
658 if (!edev)
659 return ERR_PTR(-ENOMEM);
660
661 edev->max_supported = 0;
662 edev->supported_cable = supported_cable;
663
664 return edev;
665}
666
667/*
668 * extcon_dev_free() - Free the memory of extcon device.
669 * @edev: the extcon device to free
670 */
671void extcon_dev_free(struct extcon_dev *edev)
672{
673 kfree(edev);
674}
675EXPORT_SYMBOL_GPL(extcon_dev_free);
676
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900677static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
678{
679 struct extcon_dev **r = res;
680
681 if (WARN_ON(!r || !*r))
682 return 0;
683
684 return *r == data;
685}
686
687static void devm_extcon_dev_release(struct device *dev, void *res)
688{
689 extcon_dev_free(*(struct extcon_dev **)res);
690}
691
692/**
693 * devm_extcon_dev_allocate - Allocate managed extcon device
694 * @dev: device owning the extcon device being created
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900695 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900696 * If supported_cable is NULL, cable name related APIs
697 * are disabled.
698 *
699 * This function manages automatically the memory of extcon device using device
700 * resource management and simplify the control of freeing the memory of extcon
701 * device.
702 *
703 * Returns the pointer memory of allocated extcon_dev if success
704 * or ERR_PTR(err) if fail
705 */
706struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900707 const unsigned int *supported_cable)
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900708{
709 struct extcon_dev **ptr, *edev;
710
711 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
712 if (!ptr)
713 return ERR_PTR(-ENOMEM);
714
715 edev = extcon_dev_allocate(supported_cable);
716 if (IS_ERR(edev)) {
717 devres_free(ptr);
718 return edev;
719 }
720
Chanwoo Choiac65a622014-05-30 10:13:15 +0900721 edev->dev.parent = dev;
722
Chanwoo Choi739ba1b2014-04-24 20:12:15 +0900723 *ptr = edev;
724 devres_add(dev, ptr);
725
726 return edev;
727}
728EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
729
730void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
731{
732 WARN_ON(devres_release(dev, devm_extcon_dev_release,
733 devm_extcon_dev_match, edev));
734}
735EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
736
MyungJoo Hamde55d872012-04-20 14:16:22 +0900737/**
738 * extcon_dev_register() - Register a new extcon device
739 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900740 *
741 * Among the members of edev struct, please set the "user initializing data"
742 * in any case and set the "optional callbacks" if required. However, please
743 * do not set the values of "internal data", which are initialized by
744 * this function.
745 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900746int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900747{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900748 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900749 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900750
751 if (!extcon_class) {
752 ret = create_extcon_class();
753 if (ret < 0)
754 return ret;
755 }
756
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900757 if (!edev->supported_cable)
758 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900759
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900760 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
761
762 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900763 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900764 dev_err(&edev->dev,
765 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900766 return -EINVAL;
767 }
768
Chanwoo Choidae61652013-09-27 09:19:40 +0900769 edev->dev.class = extcon_class;
770 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900771
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900772 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900773 if (IS_ERR_OR_NULL(edev->name)) {
774 dev_err(&edev->dev,
775 "extcon device name is null\n");
776 return -EINVAL;
777 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900778 dev_set_name(&edev->dev, "extcon%lu",
779 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900780
781 if (edev->max_supported) {
782 char buf[10];
783 char *str;
784 struct extcon_cable *cable;
785
786 edev->cables = kzalloc(sizeof(struct extcon_cable) *
787 edev->max_supported, GFP_KERNEL);
788 if (!edev->cables) {
789 ret = -ENOMEM;
790 goto err_sysfs_alloc;
791 }
792 for (index = 0; index < edev->max_supported; index++) {
793 cable = &edev->cables[index];
794
795 snprintf(buf, 10, "cable.%d", index);
796 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
797 GFP_KERNEL);
798 if (!str) {
799 for (index--; index >= 0; index--) {
800 cable = &edev->cables[index];
801 kfree(cable->attr_g.name);
802 }
803 ret = -ENOMEM;
804
805 goto err_alloc_cables;
806 }
807 strcpy(str, buf);
808
809 cable->edev = edev;
810 cable->cable_index = index;
811 cable->attrs[0] = &cable->attr_name.attr;
812 cable->attrs[1] = &cable->attr_state.attr;
813 cable->attrs[2] = NULL;
814 cable->attr_g.name = str;
815 cable->attr_g.attrs = cable->attrs;
816
Mark Brown9baf3222012-08-16 20:03:21 +0100817 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900818 cable->attr_name.attr.name = "name";
819 cable->attr_name.attr.mode = 0444;
820 cable->attr_name.show = cable_name_show;
821
Mark Brown9baf3222012-08-16 20:03:21 +0100822 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900823 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900824 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900825 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900826 }
827 }
828
MyungJoo Hambde68e62012-04-20 14:16:26 +0900829 if (edev->max_supported && edev->mutually_exclusive) {
830 char buf[80];
831 char *name;
832
833 /* Count the size of mutually_exclusive array */
834 for (index = 0; edev->mutually_exclusive[index]; index++)
835 ;
836
837 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
838 (index + 1), GFP_KERNEL);
839 if (!edev->attrs_muex) {
840 ret = -ENOMEM;
841 goto err_muex;
842 }
843
844 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
845 index, GFP_KERNEL);
846 if (!edev->d_attrs_muex) {
847 ret = -ENOMEM;
848 kfree(edev->attrs_muex);
849 goto err_muex;
850 }
851
852 for (index = 0; edev->mutually_exclusive[index]; index++) {
853 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
854 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
855 GFP_KERNEL);
856 if (!name) {
857 for (index--; index >= 0; index--) {
858 kfree(edev->d_attrs_muex[index].attr.
859 name);
860 }
861 kfree(edev->d_attrs_muex);
862 kfree(edev->attrs_muex);
863 ret = -ENOMEM;
864 goto err_muex;
865 }
866 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100867 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900868 edev->d_attrs_muex[index].attr.name = name;
869 edev->d_attrs_muex[index].attr.mode = 0000;
870 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
871 .attr;
872 }
873 edev->attr_g_muex.name = muex_name;
874 edev->attr_g_muex.attrs = edev->attrs_muex;
875
876 }
877
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900878 if (edev->max_supported) {
879 edev->extcon_dev_type.groups =
880 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900881 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900882 if (!edev->extcon_dev_type.groups) {
883 ret = -ENOMEM;
884 goto err_alloc_groups;
885 }
886
Chanwoo Choidae61652013-09-27 09:19:40 +0900887 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900888 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
889
890 for (index = 0; index < edev->max_supported; index++)
891 edev->extcon_dev_type.groups[index] =
892 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900893 if (edev->mutually_exclusive)
894 edev->extcon_dev_type.groups[index] =
895 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900896
Chanwoo Choidae61652013-09-27 09:19:40 +0900897 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900898 }
899
Chanwoo Choidae61652013-09-27 09:19:40 +0900900 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900901 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +0900902 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900903 goto err_dev;
904 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900905#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900906 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900907 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900908#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900909
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900910 spin_lock_init(&edev->lock);
911
Chanwoo Choi046050f2015-05-19 20:01:12 +0900912 edev->nh = devm_kzalloc(&edev->dev,
913 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
914 if (!edev->nh) {
915 ret = -ENOMEM;
916 goto err_dev;
917 }
918
919 for (index = 0; index < edev->max_supported; index++)
920 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900921
Chanwoo Choidae61652013-09-27 09:19:40 +0900922 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900923 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900924
925 mutex_lock(&extcon_dev_list_lock);
926 list_add(&edev->entry, &extcon_dev_list);
927 mutex_unlock(&extcon_dev_list_lock);
928
MyungJoo Hamde55d872012-04-20 14:16:22 +0900929 return 0;
930
931err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900932 if (edev->max_supported)
933 kfree(edev->extcon_dev_type.groups);
934err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900935 if (edev->max_supported && edev->mutually_exclusive) {
936 for (index = 0; edev->mutually_exclusive[index]; index++)
937 kfree(edev->d_attrs_muex[index].attr.name);
938 kfree(edev->d_attrs_muex);
939 kfree(edev->attrs_muex);
940 }
941err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900942 for (index = 0; index < edev->max_supported; index++)
943 kfree(edev->cables[index].attr_g.name);
944err_alloc_cables:
945 if (edev->max_supported)
946 kfree(edev->cables);
947err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900948 return ret;
949}
950EXPORT_SYMBOL_GPL(extcon_dev_register);
951
952/**
953 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900954 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900955 *
956 * Note that this does not call kfree(edev) because edev was not allocated
957 * by this class.
958 */
959void extcon_dev_unregister(struct extcon_dev *edev)
960{
anish kumar57e7cd32012-10-22 09:43:33 +0900961 int index;
962
963 mutex_lock(&extcon_dev_list_lock);
964 list_del(&edev->entry);
965 mutex_unlock(&extcon_dev_list_lock);
966
Chanwoo Choidae61652013-09-27 09:19:40 +0900967 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
968 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
969 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +0900970 return;
971 }
972
Wang, Xiaoming7585ca02013-11-01 18:48:14 -0400973 device_unregister(&edev->dev);
974
anish kumar57e7cd32012-10-22 09:43:33 +0900975 if (edev->mutually_exclusive && edev->max_supported) {
976 for (index = 0; edev->mutually_exclusive[index];
977 index++)
978 kfree(edev->d_attrs_muex[index].attr.name);
979 kfree(edev->d_attrs_muex);
980 kfree(edev->attrs_muex);
981 }
982
983 for (index = 0; index < edev->max_supported; index++)
984 kfree(edev->cables[index].attr_g.name);
985
986 if (edev->max_supported) {
987 kfree(edev->extcon_dev_type.groups);
988 kfree(edev->cables);
989 }
990
991#if defined(CONFIG_ANDROID)
992 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +0900993 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +0900994#endif
Chanwoo Choidae61652013-09-27 09:19:40 +0900995 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900996}
997EXPORT_SYMBOL_GPL(extcon_dev_unregister);
998
Sangjung Woo11112442014-04-21 19:10:08 +0900999static void devm_extcon_dev_unreg(struct device *dev, void *res)
1000{
1001 extcon_dev_unregister(*(struct extcon_dev **)res);
1002}
1003
Sangjung Woo11112442014-04-21 19:10:08 +09001004/**
1005 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
1006 * @dev: device to allocate extcon device
1007 * @edev: the new extcon device to register
1008 *
1009 * Managed extcon_dev_register() function. If extcon device is attached with
1010 * this function, that extcon device is automatically unregistered on driver
1011 * detach. Internally this function calls extcon_dev_register() function.
1012 * To get more information, refer that function.
1013 *
1014 * If extcon device is registered with this function and the device needs to be
1015 * unregistered separately, devm_extcon_dev_unregister() should be used.
1016 *
1017 * Returns 0 if success or negaive error number if failure.
1018 */
1019int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
1020{
1021 struct extcon_dev **ptr;
1022 int ret;
1023
1024 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
1025 if (!ptr)
1026 return -ENOMEM;
1027
1028 ret = extcon_dev_register(edev);
1029 if (ret) {
1030 devres_free(ptr);
1031 return ret;
1032 }
1033
1034 *ptr = edev;
1035 devres_add(dev, ptr);
1036
1037 return 0;
1038}
1039EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
1040
1041/**
1042 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1043 * @dev: device the extcon belongs to
1044 * @edev: the extcon device to unregister
1045 *
1046 * Unregister extcon device that is registered with devm_extcon_dev_register()
1047 * function.
1048 */
1049void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
1050{
1051 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1052 devm_extcon_dev_match, edev));
1053}
1054EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1055
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001056#ifdef CONFIG_OF
1057/*
1058 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1059 * @dev - instance to the given device
1060 * @index - index into list of extcon_dev
1061 *
1062 * return the instance of extcon device
1063 */
1064struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1065{
1066 struct device_node *node;
1067 struct extcon_dev *edev;
1068
1069 if (!dev->of_node) {
1070 dev_err(dev, "device does not have a device node entry\n");
1071 return ERR_PTR(-EINVAL);
1072 }
1073
1074 node = of_parse_phandle(dev->of_node, "extcon", index);
1075 if (!node) {
1076 dev_err(dev, "failed to get phandle in %s node\n",
1077 dev->of_node->full_name);
1078 return ERR_PTR(-ENODEV);
1079 }
1080
Tomasz Figaf841afb2014-10-16 15:11:44 +02001081 mutex_lock(&extcon_dev_list_lock);
1082 list_for_each_entry(edev, &extcon_dev_list, entry) {
1083 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1084 mutex_unlock(&extcon_dev_list_lock);
1085 return edev;
1086 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001087 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001088 mutex_unlock(&extcon_dev_list_lock);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001089
Tomasz Figaf841afb2014-10-16 15:11:44 +02001090 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001091}
1092#else
1093struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1094{
1095 return ERR_PTR(-ENOSYS);
1096}
1097#endif /* CONFIG_OF */
1098EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1099
Chanwoo Choi707d7552015-04-15 13:57:51 +09001100/**
1101 * extcon_get_edev_name() - Get the name of the extcon device.
1102 * @edev: the extcon device
1103 */
1104const char *extcon_get_edev_name(struct extcon_dev *edev)
1105{
1106 return !edev ? NULL : edev->name;
1107}
1108
MyungJoo Hamde55d872012-04-20 14:16:22 +09001109static int __init extcon_class_init(void)
1110{
1111 return create_extcon_class();
1112}
1113module_init(extcon_class_init);
1114
1115static void __exit extcon_class_exit(void)
1116{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001117#if defined(CONFIG_ANDROID)
1118 class_compat_unregister(switch_class);
1119#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001120 class_destroy(extcon_class);
1121}
1122module_exit(extcon_class_exit);
1123
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001124MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001125MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1126MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1127MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1128MODULE_DESCRIPTION("External connector (extcon) class driver");
1129MODULE_LICENSE("GPL");