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