1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Power Delivery sysfs entries
4 *
5 * Copyright (C) 2022, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
8
9 #include <linux/slab.h>
10 #include <linux/usb/pd.h>
11
12 #include "pd.h"
13
14 static DEFINE_IDA(pd_ida);
15
16 static struct class pd_class = {
17 .name = "usb_power_delivery",
18 .owner = THIS_MODULE,
19 };
20
21 #define to_pdo(o) container_of(o, struct pdo, dev)
22
23 struct pdo {
24 struct device dev;
25 int object_position;
26 u32 pdo;
27 };
28
pdo_release(struct device * dev)29 static void pdo_release(struct device *dev)
30 {
31 kfree(to_pdo(dev));
32 }
33
34 /* -------------------------------------------------------------------------- */
35 /* Fixed Supply */
36
37 static ssize_t
dual_role_power_show(struct device * dev,struct device_attribute * attr,char * buf)38 dual_role_power_show(struct device *dev, struct device_attribute *attr, char *buf)
39 {
40 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DUAL_ROLE));
41 }
42 static DEVICE_ATTR_RO(dual_role_power);
43
44 static ssize_t
usb_suspend_supported_show(struct device * dev,struct device_attribute * attr,char * buf)45 usb_suspend_supported_show(struct device *dev, struct device_attribute *attr, char *buf)
46 {
47 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_SUSPEND));
48 }
49 static DEVICE_ATTR_RO(usb_suspend_supported);
50
51 static ssize_t
higher_capability_show(struct device * dev,struct device_attribute * attr,char * buf)52 higher_capability_show(struct device *dev, struct device_attribute *attr, char *buf)
53 {
54 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_HIGHER_CAP));
55 }
56 static DEVICE_ATTR_RO(higher_capability);
57
58 static ssize_t
unconstrained_power_show(struct device * dev,struct device_attribute * attr,char * buf)59 unconstrained_power_show(struct device *dev, struct device_attribute *attr, char *buf)
60 {
61 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_EXTPOWER));
62 }
63 static DEVICE_ATTR_RO(unconstrained_power);
64
65 static ssize_t
usb_communication_capable_show(struct device * dev,struct device_attribute * attr,char * buf)66 usb_communication_capable_show(struct device *dev, struct device_attribute *attr, char *buf)
67 {
68 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_USB_COMM));
69 }
70 static DEVICE_ATTR_RO(usb_communication_capable);
71
72 static ssize_t
dual_role_data_show(struct device * dev,struct device_attribute * attr,char * buf)73 dual_role_data_show(struct device *dev, struct device_attribute *attr, char *buf)
74 {
75 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DATA_SWAP));
76 }
77 static DEVICE_ATTR_RO(dual_role_data);
78
79 static ssize_t
unchunked_extended_messages_supported_show(struct device * dev,struct device_attribute * attr,char * buf)80 unchunked_extended_messages_supported_show(struct device *dev,
81 struct device_attribute *attr, char *buf)
82 {
83 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_UNCHUNK_EXT));
84 }
85 static DEVICE_ATTR_RO(unchunked_extended_messages_supported);
86
87 /*
88 * REVISIT: Peak Current requires access also to the RDO.
89 static ssize_t
90 peak_current_show(struct device *dev, struct device_attribute *attr, char *buf)
91 {
92 ...
93 }
94 */
95
96 static ssize_t
fast_role_swap_current_show(struct device * dev,struct device_attribute * attr,char * buf)97 fast_role_swap_current_show(struct device *dev, struct device_attribute *attr, char *buf)
98 {
99 return sysfs_emit(buf, "%u\n", to_pdo(dev)->pdo >> PDO_FIXED_FRS_CURR_SHIFT) & 3;
100 }
101 static DEVICE_ATTR_RO(fast_role_swap_current);
102
voltage_show(struct device * dev,struct device_attribute * attr,char * buf)103 static ssize_t voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
104 {
105 return sysfs_emit(buf, "%umV\n", pdo_fixed_voltage(to_pdo(dev)->pdo));
106 }
107 static DEVICE_ATTR_RO(voltage);
108
109 /* Shared with Variable supplies, both source and sink */
current_show(struct device * dev,struct device_attribute * attr,char * buf)110 static ssize_t current_show(struct device *dev, struct device_attribute *attr, char *buf)
111 {
112 return sysfs_emit(buf, "%umA\n", pdo_max_current(to_pdo(dev)->pdo));
113 }
114
115 /* Shared with Variable type supplies */
116 static struct device_attribute maximum_current_attr = {
117 .attr = {
118 .name = "maximum_current",
119 .mode = 0444,
120 },
121 .show = current_show,
122 };
123
124 static struct device_attribute operational_current_attr = {
125 .attr = {
126 .name = "operational_current",
127 .mode = 0444,
128 },
129 .show = current_show,
130 };
131
132 static struct attribute *source_fixed_supply_attrs[] = {
133 &dev_attr_dual_role_power.attr,
134 &dev_attr_usb_suspend_supported.attr,
135 &dev_attr_unconstrained_power.attr,
136 &dev_attr_usb_communication_capable.attr,
137 &dev_attr_dual_role_data.attr,
138 &dev_attr_unchunked_extended_messages_supported.attr,
139 /*&dev_attr_peak_current.attr,*/
140 &dev_attr_voltage.attr,
141 &maximum_current_attr.attr,
142 NULL
143 };
144
fixed_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)145 static umode_t fixed_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
146 {
147 if (to_pdo(kobj_to_dev(kobj))->object_position &&
148 /*attr != &dev_attr_peak_current.attr &&*/
149 attr != &dev_attr_voltage.attr &&
150 attr != &maximum_current_attr.attr &&
151 attr != &operational_current_attr.attr)
152 return 0;
153
154 return attr->mode;
155 }
156
157 static const struct attribute_group source_fixed_supply_group = {
158 .is_visible = fixed_attr_is_visible,
159 .attrs = source_fixed_supply_attrs,
160 };
161 __ATTRIBUTE_GROUPS(source_fixed_supply);
162
163 static struct device_type source_fixed_supply_type = {
164 .name = "pdo",
165 .release = pdo_release,
166 .groups = source_fixed_supply_groups,
167 };
168
169 static struct attribute *sink_fixed_supply_attrs[] = {
170 &dev_attr_dual_role_power.attr,
171 &dev_attr_higher_capability.attr,
172 &dev_attr_unconstrained_power.attr,
173 &dev_attr_usb_communication_capable.attr,
174 &dev_attr_dual_role_data.attr,
175 &dev_attr_unchunked_extended_messages_supported.attr,
176 &dev_attr_fast_role_swap_current.attr,
177 &dev_attr_voltage.attr,
178 &operational_current_attr.attr,
179 NULL
180 };
181
182 static const struct attribute_group sink_fixed_supply_group = {
183 .is_visible = fixed_attr_is_visible,
184 .attrs = sink_fixed_supply_attrs,
185 };
186 __ATTRIBUTE_GROUPS(sink_fixed_supply);
187
188 static struct device_type sink_fixed_supply_type = {
189 .name = "pdo",
190 .release = pdo_release,
191 .groups = sink_fixed_supply_groups,
192 };
193
194 /* -------------------------------------------------------------------------- */
195 /* Variable Supply */
196
197 static ssize_t
maximum_voltage_show(struct device * dev,struct device_attribute * attr,char * buf)198 maximum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
199 {
200 return sysfs_emit(buf, "%umV\n", pdo_max_voltage(to_pdo(dev)->pdo));
201 }
202 static DEVICE_ATTR_RO(maximum_voltage);
203
204 static ssize_t
minimum_voltage_show(struct device * dev,struct device_attribute * attr,char * buf)205 minimum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
206 {
207 return sysfs_emit(buf, "%umV\n", pdo_min_voltage(to_pdo(dev)->pdo));
208 }
209 static DEVICE_ATTR_RO(minimum_voltage);
210
211 static struct attribute *source_variable_supply_attrs[] = {
212 &dev_attr_maximum_voltage.attr,
213 &dev_attr_minimum_voltage.attr,
214 &maximum_current_attr.attr,
215 NULL
216 };
217 ATTRIBUTE_GROUPS(source_variable_supply);
218
219 static struct device_type source_variable_supply_type = {
220 .name = "pdo",
221 .release = pdo_release,
222 .groups = source_variable_supply_groups,
223 };
224
225 static struct attribute *sink_variable_supply_attrs[] = {
226 &dev_attr_maximum_voltage.attr,
227 &dev_attr_minimum_voltage.attr,
228 &operational_current_attr.attr,
229 NULL
230 };
231 ATTRIBUTE_GROUPS(sink_variable_supply);
232
233 static struct device_type sink_variable_supply_type = {
234 .name = "pdo",
235 .release = pdo_release,
236 .groups = sink_variable_supply_groups,
237 };
238
239 /* -------------------------------------------------------------------------- */
240 /* Battery */
241
242 static ssize_t
maximum_power_show(struct device * dev,struct device_attribute * attr,char * buf)243 maximum_power_show(struct device *dev, struct device_attribute *attr, char *buf)
244 {
245 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
246 }
247 static DEVICE_ATTR_RO(maximum_power);
248
249 static ssize_t
operational_power_show(struct device * dev,struct device_attribute * attr,char * buf)250 operational_power_show(struct device *dev, struct device_attribute *attr, char *buf)
251 {
252 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
253 }
254 static DEVICE_ATTR_RO(operational_power);
255
256 static struct attribute *source_battery_attrs[] = {
257 &dev_attr_maximum_voltage.attr,
258 &dev_attr_minimum_voltage.attr,
259 &dev_attr_maximum_power.attr,
260 NULL
261 };
262 ATTRIBUTE_GROUPS(source_battery);
263
264 static struct device_type source_battery_type = {
265 .name = "pdo",
266 .release = pdo_release,
267 .groups = source_battery_groups,
268 };
269
270 static struct attribute *sink_battery_attrs[] = {
271 &dev_attr_maximum_voltage.attr,
272 &dev_attr_minimum_voltage.attr,
273 &dev_attr_operational_power.attr,
274 NULL
275 };
276 ATTRIBUTE_GROUPS(sink_battery);
277
278 static struct device_type sink_battery_type = {
279 .name = "pdo",
280 .release = pdo_release,
281 .groups = sink_battery_groups,
282 };
283
284 /* -------------------------------------------------------------------------- */
285 /* Standard Power Range (SPR) Programmable Power Supply (PPS) */
286
287 static ssize_t
pps_power_limited_show(struct device * dev,struct device_attribute * attr,char * buf)288 pps_power_limited_show(struct device *dev, struct device_attribute *attr, char *buf)
289 {
290 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & BIT(27)));
291 }
292 static DEVICE_ATTR_RO(pps_power_limited);
293
294 static ssize_t
pps_max_voltage_show(struct device * dev,struct device_attribute * attr,char * buf)295 pps_max_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
296 {
297 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_max_voltage(to_pdo(dev)->pdo));
298 }
299
300 static ssize_t
pps_min_voltage_show(struct device * dev,struct device_attribute * attr,char * buf)301 pps_min_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
302 {
303 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_min_voltage(to_pdo(dev)->pdo));
304 }
305
306 static ssize_t
pps_max_current_show(struct device * dev,struct device_attribute * attr,char * buf)307 pps_max_current_show(struct device *dev, struct device_attribute *attr, char *buf)
308 {
309 return sysfs_emit(buf, "%umA\n", pdo_pps_apdo_max_current(to_pdo(dev)->pdo));
310 }
311
312 static struct device_attribute pps_max_voltage_attr = {
313 .attr = {
314 .name = "maximum_voltage",
315 .mode = 0444,
316 },
317 .show = pps_max_voltage_show,
318 };
319
320 static struct device_attribute pps_min_voltage_attr = {
321 .attr = {
322 .name = "minimum_voltage",
323 .mode = 0444,
324 },
325 .show = pps_min_voltage_show,
326 };
327
328 static struct device_attribute pps_max_current_attr = {
329 .attr = {
330 .name = "maximum_current",
331 .mode = 0444,
332 },
333 .show = pps_max_current_show,
334 };
335
336 static struct attribute *source_pps_attrs[] = {
337 &dev_attr_pps_power_limited.attr,
338 &pps_max_voltage_attr.attr,
339 &pps_min_voltage_attr.attr,
340 &pps_max_current_attr.attr,
341 NULL
342 };
343 ATTRIBUTE_GROUPS(source_pps);
344
345 static struct device_type source_pps_type = {
346 .name = "pdo",
347 .release = pdo_release,
348 .groups = source_pps_groups,
349 };
350
351 static struct attribute *sink_pps_attrs[] = {
352 &pps_max_voltage_attr.attr,
353 &pps_min_voltage_attr.attr,
354 &pps_max_current_attr.attr,
355 NULL
356 };
357 ATTRIBUTE_GROUPS(sink_pps);
358
359 static struct device_type sink_pps_type = {
360 .name = "pdo",
361 .release = pdo_release,
362 .groups = sink_pps_groups,
363 };
364
365 /* -------------------------------------------------------------------------- */
366
367 static const char * const supply_name[] = {
368 [PDO_TYPE_FIXED] = "fixed_supply",
369 [PDO_TYPE_BATT] = "battery",
370 [PDO_TYPE_VAR] = "variable_supply",
371 };
372
373 static const char * const apdo_supply_name[] = {
374 [APDO_TYPE_PPS] = "programmable_supply",
375 };
376
377 static struct device_type *source_type[] = {
378 [PDO_TYPE_FIXED] = &source_fixed_supply_type,
379 [PDO_TYPE_BATT] = &source_battery_type,
380 [PDO_TYPE_VAR] = &source_variable_supply_type,
381 };
382
383 static struct device_type *source_apdo_type[] = {
384 [APDO_TYPE_PPS] = &source_pps_type,
385 };
386
387 static struct device_type *sink_type[] = {
388 [PDO_TYPE_FIXED] = &sink_fixed_supply_type,
389 [PDO_TYPE_BATT] = &sink_battery_type,
390 [PDO_TYPE_VAR] = &sink_variable_supply_type,
391 };
392
393 static struct device_type *sink_apdo_type[] = {
394 [APDO_TYPE_PPS] = &sink_pps_type,
395 };
396
397 /* REVISIT: Export when EPR_*_Capabilities need to be supported. */
add_pdo(struct usb_power_delivery_capabilities * cap,u32 pdo,int position)398 static int add_pdo(struct usb_power_delivery_capabilities *cap, u32 pdo, int position)
399 {
400 struct device_type *type;
401 const char *name;
402 struct pdo *p;
403 int ret;
404
405 p = kzalloc(sizeof(*p), GFP_KERNEL);
406 if (!p)
407 return -ENOMEM;
408
409 p->pdo = pdo;
410 p->object_position = position;
411
412 if (pdo_type(pdo) == PDO_TYPE_APDO) {
413 /* FIXME: Only PPS supported for now! Skipping others. */
414 if (pdo_apdo_type(pdo) > APDO_TYPE_PPS) {
415 dev_warn(&cap->dev, "Unknown APDO type. PDO 0x%08x\n", pdo);
416 kfree(p);
417 return 0;
418 }
419
420 if (is_source(cap->role))
421 type = source_apdo_type[pdo_apdo_type(pdo)];
422 else
423 type = sink_apdo_type[pdo_apdo_type(pdo)];
424
425 name = apdo_supply_name[pdo_apdo_type(pdo)];
426 } else {
427 if (is_source(cap->role))
428 type = source_type[pdo_type(pdo)];
429 else
430 type = sink_type[pdo_type(pdo)];
431
432 name = supply_name[pdo_type(pdo)];
433 }
434
435 p->dev.parent = &cap->dev;
436 p->dev.type = type;
437 dev_set_name(&p->dev, "%u:%s", position + 1, name);
438
439 ret = device_register(&p->dev);
440 if (ret) {
441 put_device(&p->dev);
442 return ret;
443 }
444
445 return 0;
446 }
447
remove_pdo(struct device * dev,void * data)448 static int remove_pdo(struct device *dev, void *data)
449 {
450 device_unregister(dev);
451 return 0;
452 }
453
454 /* -------------------------------------------------------------------------- */
455
456 static const char * const cap_name[] = {
457 [TYPEC_SINK] = "sink-capabilities",
458 [TYPEC_SOURCE] = "source-capabilities",
459 };
460
pd_capabilities_release(struct device * dev)461 static void pd_capabilities_release(struct device *dev)
462 {
463 kfree(to_usb_power_delivery_capabilities(dev));
464 }
465
466 static struct device_type pd_capabilities_type = {
467 .name = "capabilities",
468 .release = pd_capabilities_release,
469 };
470
471 /**
472 * usb_power_delivery_register_capabilities - Register a set of capabilities.
473 * @pd: The USB PD instance that the capabilities belong to.
474 * @desc: Description of the Capablities Message.
475 *
476 * This function registers a Capabilities Message described in @desc. The
477 * capabilities will have their own sub-directory under @pd in sysfs.
478 *
479 * The function returns pointer to struct usb_power_delivery_capabilities, or
480 * ERR_PRT(errno).
481 */
482 struct usb_power_delivery_capabilities *
usb_power_delivery_register_capabilities(struct usb_power_delivery * pd,struct usb_power_delivery_capabilities_desc * desc)483 usb_power_delivery_register_capabilities(struct usb_power_delivery *pd,
484 struct usb_power_delivery_capabilities_desc *desc)
485 {
486 struct usb_power_delivery_capabilities *cap;
487 int ret;
488 int i;
489
490 cap = kzalloc(sizeof(*cap), GFP_KERNEL);
491 if (!cap)
492 return ERR_PTR(-ENOMEM);
493
494 cap->pd = pd;
495 cap->role = desc->role;
496
497 cap->dev.parent = &pd->dev;
498 cap->dev.type = &pd_capabilities_type;
499 dev_set_name(&cap->dev, "%s", cap_name[cap->role]);
500
501 ret = device_register(&cap->dev);
502 if (ret) {
503 put_device(&cap->dev);
504 return ERR_PTR(ret);
505 }
506
507 for (i = 0; i < PDO_MAX_OBJECTS && desc->pdo[i]; i++) {
508 ret = add_pdo(cap, desc->pdo[i], i);
509 if (ret) {
510 usb_power_delivery_unregister_capabilities(cap);
511 return ERR_PTR(ret);
512 }
513 }
514
515 return cap;
516 }
517 EXPORT_SYMBOL_GPL(usb_power_delivery_register_capabilities);
518
519 /**
520 * usb_power_delivery_unregister_capabilities - Unregister a set of capabilities
521 * @cap: The capabilities
522 */
usb_power_delivery_unregister_capabilities(struct usb_power_delivery_capabilities * cap)523 void usb_power_delivery_unregister_capabilities(struct usb_power_delivery_capabilities *cap)
524 {
525 if (!cap)
526 return;
527
528 device_for_each_child(&cap->dev, NULL, remove_pdo);
529 device_unregister(&cap->dev);
530 }
531 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister_capabilities);
532
533 /* -------------------------------------------------------------------------- */
534
revision_show(struct device * dev,struct device_attribute * attr,char * buf)535 static ssize_t revision_show(struct device *dev, struct device_attribute *attr, char *buf)
536 {
537 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
538
539 return sysfs_emit(buf, "%u.%u\n", (pd->revision >> 8) & 0xff, (pd->revision >> 4) & 0xf);
540 }
541 static DEVICE_ATTR_RO(revision);
542
version_show(struct device * dev,struct device_attribute * attr,char * buf)543 static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
544 {
545 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
546
547 return sysfs_emit(buf, "%u.%u\n", (pd->version >> 8) & 0xff, (pd->version >> 4) & 0xf);
548 }
549 static DEVICE_ATTR_RO(version);
550
551 static struct attribute *pd_attrs[] = {
552 &dev_attr_revision.attr,
553 &dev_attr_version.attr,
554 NULL
555 };
556
pd_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)557 static umode_t pd_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
558 {
559 struct usb_power_delivery *pd = to_usb_power_delivery(kobj_to_dev(kobj));
560
561 if (attr == &dev_attr_version.attr && !pd->version)
562 return 0;
563
564 return attr->mode;
565 }
566
567 static const struct attribute_group pd_group = {
568 .is_visible = pd_attr_is_visible,
569 .attrs = pd_attrs,
570 };
571 __ATTRIBUTE_GROUPS(pd);
572
pd_release(struct device * dev)573 static void pd_release(struct device *dev)
574 {
575 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
576
577 ida_simple_remove(&pd_ida, pd->id);
578 kfree(pd);
579 }
580
581 static struct device_type pd_type = {
582 .name = "usb_power_delivery",
583 .release = pd_release,
584 .groups = pd_groups,
585 };
586
usb_power_delivery_find(const char * name)587 struct usb_power_delivery *usb_power_delivery_find(const char *name)
588 {
589 struct device *dev;
590
591 dev = class_find_device_by_name(&pd_class, name);
592
593 return dev ? to_usb_power_delivery(dev) : NULL;
594 }
595
596 /**
597 * usb_power_delivery_register - Register USB Power Delivery Support.
598 * @parent: Parent device.
599 * @desc: Description of the USB PD contract.
600 *
601 * This routine can be used to register USB Power Delivery capabilities that a
602 * device or devices can support. These capabilities represent all the
603 * capabilities that can be negotiated with a partner, so not only the Power
604 * Capabilities that are negotiated using the USB PD Capabilities Message.
605 *
606 * The USB Power Delivery Support object that this routine generates can be used
607 * as the parent object for all the actual USB Power Delivery Messages and
608 * objects that can be negotiated with the partner.
609 *
610 * Returns handle to struct usb_power_delivery or ERR_PTR.
611 */
612 struct usb_power_delivery *
usb_power_delivery_register(struct device * parent,struct usb_power_delivery_desc * desc)613 usb_power_delivery_register(struct device *parent, struct usb_power_delivery_desc *desc)
614 {
615 struct usb_power_delivery *pd;
616 int ret;
617
618 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
619 if (!pd)
620 return ERR_PTR(-ENOMEM);
621
622 ret = ida_simple_get(&pd_ida, 0, 0, GFP_KERNEL);
623 if (ret < 0) {
624 kfree(pd);
625 return ERR_PTR(ret);
626 }
627
628 pd->id = ret;
629 pd->revision = desc->revision;
630 pd->version = desc->version;
631
632 pd->dev.parent = parent;
633 pd->dev.type = &pd_type;
634 pd->dev.class = &pd_class;
635 dev_set_name(&pd->dev, "pd%d", pd->id);
636
637 ret = device_register(&pd->dev);
638 if (ret) {
639 put_device(&pd->dev);
640 return ERR_PTR(ret);
641 }
642
643 return pd;
644 }
645 EXPORT_SYMBOL_GPL(usb_power_delivery_register);
646
647 /**
648 * usb_power_delivery_unregister - Unregister USB Power Delivery Support.
649 * @pd: The USB PD contract.
650 */
usb_power_delivery_unregister(struct usb_power_delivery * pd)651 void usb_power_delivery_unregister(struct usb_power_delivery *pd)
652 {
653 if (IS_ERR_OR_NULL(pd))
654 return;
655
656 device_unregister(&pd->dev);
657 }
658 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister);
659
660 /**
661 * usb_power_delivery_link_device - Link device to its USB PD object.
662 * @pd: The USB PD instance.
663 * @dev: The device.
664 *
665 * This function can be used to create a symlink named "usb_power_delivery" for
666 * @dev that points to @pd.
667 */
usb_power_delivery_link_device(struct usb_power_delivery * pd,struct device * dev)668 int usb_power_delivery_link_device(struct usb_power_delivery *pd, struct device *dev)
669 {
670 int ret;
671
672 if (IS_ERR_OR_NULL(pd) || !dev)
673 return 0;
674
675 ret = sysfs_create_link(&dev->kobj, &pd->dev.kobj, "usb_power_delivery");
676 if (ret)
677 return ret;
678
679 get_device(&pd->dev);
680 get_device(dev);
681
682 return 0;
683 }
684 EXPORT_SYMBOL_GPL(usb_power_delivery_link_device);
685
686 /**
687 * usb_power_delivery_unlink_device - Unlink device from its USB PD object.
688 * @pd: The USB PD instance.
689 * @dev: The device.
690 *
691 * Remove the symlink that was previously created with pd_link_device().
692 */
usb_power_delivery_unlink_device(struct usb_power_delivery * pd,struct device * dev)693 void usb_power_delivery_unlink_device(struct usb_power_delivery *pd, struct device *dev)
694 {
695 if (IS_ERR_OR_NULL(pd) || !dev)
696 return;
697
698 sysfs_remove_link(&dev->kobj, "usb_power_delivery");
699 put_device(&pd->dev);
700 put_device(dev);
701 }
702 EXPORT_SYMBOL_GPL(usb_power_delivery_unlink_device);
703
704 /* -------------------------------------------------------------------------- */
705
usb_power_delivery_init(void)706 int __init usb_power_delivery_init(void)
707 {
708 return class_register(&pd_class);
709 }
710
usb_power_delivery_exit(void)711 void __exit usb_power_delivery_exit(void)
712 {
713 ida_destroy(&pd_ida);
714 class_unregister(&pd_class);
715 }
716