1 // SPDX-License-Identifier: GPL-2.0
3 * USB Power Delivery sysfs entries
5 * Copyright (C) 2022, Intel Corporation
9 #include <linux/slab.h>
10 #include <linux/usb/pd.h>
14 static DEFINE_IDA(pd_ida);
16 static struct class pd_class = {
17 .name = "usb_power_delivery",
20 #define to_pdo(o) container_of(o, struct pdo, dev)
28 static void pdo_release(struct device *dev)
33 /* -------------------------------------------------------------------------- */
37 dual_role_power_show(struct device *dev, struct device_attribute *attr, char *buf)
39 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DUAL_ROLE));
41 static DEVICE_ATTR_RO(dual_role_power);
44 usb_suspend_supported_show(struct device *dev, struct device_attribute *attr, char *buf)
46 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_SUSPEND));
48 static DEVICE_ATTR_RO(usb_suspend_supported);
51 higher_capability_show(struct device *dev, struct device_attribute *attr, char *buf)
53 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_HIGHER_CAP));
55 static DEVICE_ATTR_RO(higher_capability);
58 unconstrained_power_show(struct device *dev, struct device_attribute *attr, char *buf)
60 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_EXTPOWER));
62 static DEVICE_ATTR_RO(unconstrained_power);
65 usb_communication_capable_show(struct device *dev, struct device_attribute *attr, char *buf)
67 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_USB_COMM));
69 static DEVICE_ATTR_RO(usb_communication_capable);
72 dual_role_data_show(struct device *dev, struct device_attribute *attr, char *buf)
74 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DATA_SWAP));
76 static DEVICE_ATTR_RO(dual_role_data);
79 unchunked_extended_messages_supported_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
82 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_UNCHUNK_EXT));
84 static DEVICE_ATTR_RO(unchunked_extended_messages_supported);
87 * REVISIT: Peak Current requires access also to the RDO.
89 peak_current_show(struct device *dev, struct device_attribute *attr, char *buf)
96 fast_role_swap_current_show(struct device *dev, struct device_attribute *attr, char *buf)
98 return sysfs_emit(buf, "%u\n", (to_pdo(dev)->pdo >> PDO_FIXED_FRS_CURR_SHIFT) & 3);
100 static DEVICE_ATTR_RO(fast_role_swap_current);
102 static ssize_t voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
104 return sysfs_emit(buf, "%umV\n", pdo_fixed_voltage(to_pdo(dev)->pdo));
106 static DEVICE_ATTR_RO(voltage);
108 /* Shared with Variable supplies, both source and sink */
109 static ssize_t current_show(struct device *dev, struct device_attribute *attr, char *buf)
111 return sysfs_emit(buf, "%umA\n", pdo_max_current(to_pdo(dev)->pdo));
114 /* Shared with Variable type supplies */
115 static struct device_attribute maximum_current_attr = {
117 .name = "maximum_current",
120 .show = current_show,
123 static struct device_attribute operational_current_attr = {
125 .name = "operational_current",
128 .show = current_show,
131 static struct attribute *source_fixed_supply_attrs[] = {
132 &dev_attr_dual_role_power.attr,
133 &dev_attr_usb_suspend_supported.attr,
134 &dev_attr_unconstrained_power.attr,
135 &dev_attr_usb_communication_capable.attr,
136 &dev_attr_dual_role_data.attr,
137 &dev_attr_unchunked_extended_messages_supported.attr,
138 /*&dev_attr_peak_current.attr,*/
139 &dev_attr_voltage.attr,
140 &maximum_current_attr.attr,
144 static umode_t fixed_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
146 if (to_pdo(kobj_to_dev(kobj))->object_position &&
147 /*attr != &dev_attr_peak_current.attr &&*/
148 attr != &dev_attr_voltage.attr &&
149 attr != &maximum_current_attr.attr &&
150 attr != &operational_current_attr.attr)
156 static const struct attribute_group source_fixed_supply_group = {
157 .is_visible = fixed_attr_is_visible,
158 .attrs = source_fixed_supply_attrs,
160 __ATTRIBUTE_GROUPS(source_fixed_supply);
162 static struct device_type source_fixed_supply_type = {
164 .release = pdo_release,
165 .groups = source_fixed_supply_groups,
168 static struct attribute *sink_fixed_supply_attrs[] = {
169 &dev_attr_dual_role_power.attr,
170 &dev_attr_higher_capability.attr,
171 &dev_attr_unconstrained_power.attr,
172 &dev_attr_usb_communication_capable.attr,
173 &dev_attr_dual_role_data.attr,
174 &dev_attr_unchunked_extended_messages_supported.attr,
175 &dev_attr_fast_role_swap_current.attr,
176 &dev_attr_voltage.attr,
177 &operational_current_attr.attr,
181 static const struct attribute_group sink_fixed_supply_group = {
182 .is_visible = fixed_attr_is_visible,
183 .attrs = sink_fixed_supply_attrs,
185 __ATTRIBUTE_GROUPS(sink_fixed_supply);
187 static struct device_type sink_fixed_supply_type = {
189 .release = pdo_release,
190 .groups = sink_fixed_supply_groups,
193 /* -------------------------------------------------------------------------- */
194 /* Variable Supply */
197 maximum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
199 return sysfs_emit(buf, "%umV\n", pdo_max_voltage(to_pdo(dev)->pdo));
201 static DEVICE_ATTR_RO(maximum_voltage);
204 minimum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
206 return sysfs_emit(buf, "%umV\n", pdo_min_voltage(to_pdo(dev)->pdo));
208 static DEVICE_ATTR_RO(minimum_voltage);
210 static struct attribute *source_variable_supply_attrs[] = {
211 &dev_attr_maximum_voltage.attr,
212 &dev_attr_minimum_voltage.attr,
213 &maximum_current_attr.attr,
216 ATTRIBUTE_GROUPS(source_variable_supply);
218 static struct device_type source_variable_supply_type = {
220 .release = pdo_release,
221 .groups = source_variable_supply_groups,
224 static struct attribute *sink_variable_supply_attrs[] = {
225 &dev_attr_maximum_voltage.attr,
226 &dev_attr_minimum_voltage.attr,
227 &operational_current_attr.attr,
230 ATTRIBUTE_GROUPS(sink_variable_supply);
232 static struct device_type sink_variable_supply_type = {
234 .release = pdo_release,
235 .groups = sink_variable_supply_groups,
238 /* -------------------------------------------------------------------------- */
242 maximum_power_show(struct device *dev, struct device_attribute *attr, char *buf)
244 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
246 static DEVICE_ATTR_RO(maximum_power);
249 operational_power_show(struct device *dev, struct device_attribute *attr, char *buf)
251 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
253 static DEVICE_ATTR_RO(operational_power);
255 static struct attribute *source_battery_attrs[] = {
256 &dev_attr_maximum_voltage.attr,
257 &dev_attr_minimum_voltage.attr,
258 &dev_attr_maximum_power.attr,
261 ATTRIBUTE_GROUPS(source_battery);
263 static struct device_type source_battery_type = {
265 .release = pdo_release,
266 .groups = source_battery_groups,
269 static struct attribute *sink_battery_attrs[] = {
270 &dev_attr_maximum_voltage.attr,
271 &dev_attr_minimum_voltage.attr,
272 &dev_attr_operational_power.attr,
275 ATTRIBUTE_GROUPS(sink_battery);
277 static struct device_type sink_battery_type = {
279 .release = pdo_release,
280 .groups = sink_battery_groups,
283 /* -------------------------------------------------------------------------- */
284 /* Standard Power Range (SPR) Programmable Power Supply (PPS) */
287 pps_power_limited_show(struct device *dev, struct device_attribute *attr, char *buf)
289 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & BIT(27)));
291 static DEVICE_ATTR_RO(pps_power_limited);
294 pps_max_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
296 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_max_voltage(to_pdo(dev)->pdo));
300 pps_min_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
302 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_min_voltage(to_pdo(dev)->pdo));
306 pps_max_current_show(struct device *dev, struct device_attribute *attr, char *buf)
308 return sysfs_emit(buf, "%umA\n", pdo_pps_apdo_max_current(to_pdo(dev)->pdo));
311 static struct device_attribute pps_max_voltage_attr = {
313 .name = "maximum_voltage",
316 .show = pps_max_voltage_show,
319 static struct device_attribute pps_min_voltage_attr = {
321 .name = "minimum_voltage",
324 .show = pps_min_voltage_show,
327 static struct device_attribute pps_max_current_attr = {
329 .name = "maximum_current",
332 .show = pps_max_current_show,
335 static struct attribute *source_pps_attrs[] = {
336 &dev_attr_pps_power_limited.attr,
337 &pps_max_voltage_attr.attr,
338 &pps_min_voltage_attr.attr,
339 &pps_max_current_attr.attr,
342 ATTRIBUTE_GROUPS(source_pps);
344 static struct device_type source_pps_type = {
346 .release = pdo_release,
347 .groups = source_pps_groups,
350 static struct attribute *sink_pps_attrs[] = {
351 &pps_max_voltage_attr.attr,
352 &pps_min_voltage_attr.attr,
353 &pps_max_current_attr.attr,
356 ATTRIBUTE_GROUPS(sink_pps);
358 static struct device_type sink_pps_type = {
360 .release = pdo_release,
361 .groups = sink_pps_groups,
364 /* -------------------------------------------------------------------------- */
366 static const char * const supply_name[] = {
367 [PDO_TYPE_FIXED] = "fixed_supply",
368 [PDO_TYPE_BATT] = "battery",
369 [PDO_TYPE_VAR] = "variable_supply",
372 static const char * const apdo_supply_name[] = {
373 [APDO_TYPE_PPS] = "programmable_supply",
376 static struct device_type *source_type[] = {
377 [PDO_TYPE_FIXED] = &source_fixed_supply_type,
378 [PDO_TYPE_BATT] = &source_battery_type,
379 [PDO_TYPE_VAR] = &source_variable_supply_type,
382 static struct device_type *source_apdo_type[] = {
383 [APDO_TYPE_PPS] = &source_pps_type,
386 static struct device_type *sink_type[] = {
387 [PDO_TYPE_FIXED] = &sink_fixed_supply_type,
388 [PDO_TYPE_BATT] = &sink_battery_type,
389 [PDO_TYPE_VAR] = &sink_variable_supply_type,
392 static struct device_type *sink_apdo_type[] = {
393 [APDO_TYPE_PPS] = &sink_pps_type,
396 /* REVISIT: Export when EPR_*_Capabilities need to be supported. */
397 static int add_pdo(struct usb_power_delivery_capabilities *cap, u32 pdo, int position)
399 struct device_type *type;
404 p = kzalloc(sizeof(*p), GFP_KERNEL);
409 p->object_position = position;
411 if (pdo_type(pdo) == PDO_TYPE_APDO) {
412 /* FIXME: Only PPS supported for now! Skipping others. */
413 if (pdo_apdo_type(pdo) > APDO_TYPE_PPS) {
414 dev_warn(&cap->dev, "Unknown APDO type. PDO 0x%08x\n", pdo);
419 if (is_source(cap->role))
420 type = source_apdo_type[pdo_apdo_type(pdo)];
422 type = sink_apdo_type[pdo_apdo_type(pdo)];
424 name = apdo_supply_name[pdo_apdo_type(pdo)];
426 if (is_source(cap->role))
427 type = source_type[pdo_type(pdo)];
429 type = sink_type[pdo_type(pdo)];
431 name = supply_name[pdo_type(pdo)];
434 p->dev.parent = &cap->dev;
436 dev_set_name(&p->dev, "%u:%s", position + 1, name);
438 ret = device_register(&p->dev);
447 static int remove_pdo(struct device *dev, void *data)
449 device_unregister(dev);
453 /* -------------------------------------------------------------------------- */
455 static const char * const cap_name[] = {
456 [TYPEC_SINK] = "sink-capabilities",
457 [TYPEC_SOURCE] = "source-capabilities",
460 static void pd_capabilities_release(struct device *dev)
462 kfree(to_usb_power_delivery_capabilities(dev));
465 static struct device_type pd_capabilities_type = {
466 .name = "capabilities",
467 .release = pd_capabilities_release,
471 * usb_power_delivery_register_capabilities - Register a set of capabilities.
472 * @pd: The USB PD instance that the capabilities belong to.
473 * @desc: Description of the Capablities Message.
475 * This function registers a Capabilities Message described in @desc. The
476 * capabilities will have their own sub-directory under @pd in sysfs.
478 * The function returns pointer to struct usb_power_delivery_capabilities, or
481 struct usb_power_delivery_capabilities *
482 usb_power_delivery_register_capabilities(struct usb_power_delivery *pd,
483 struct usb_power_delivery_capabilities_desc *desc)
485 struct usb_power_delivery_capabilities *cap;
489 cap = kzalloc(sizeof(*cap), GFP_KERNEL);
491 return ERR_PTR(-ENOMEM);
494 cap->role = desc->role;
496 cap->dev.parent = &pd->dev;
497 cap->dev.type = &pd_capabilities_type;
498 dev_set_name(&cap->dev, "%s", cap_name[cap->role]);
500 ret = device_register(&cap->dev);
502 put_device(&cap->dev);
506 for (i = 0; i < PDO_MAX_OBJECTS && desc->pdo[i]; i++) {
507 ret = add_pdo(cap, desc->pdo[i], i);
509 usb_power_delivery_unregister_capabilities(cap);
516 EXPORT_SYMBOL_GPL(usb_power_delivery_register_capabilities);
519 * usb_power_delivery_unregister_capabilities - Unregister a set of capabilities
520 * @cap: The capabilities
522 void usb_power_delivery_unregister_capabilities(struct usb_power_delivery_capabilities *cap)
527 device_for_each_child(&cap->dev, NULL, remove_pdo);
528 device_unregister(&cap->dev);
530 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister_capabilities);
532 /* -------------------------------------------------------------------------- */
534 static ssize_t revision_show(struct device *dev, struct device_attribute *attr, char *buf)
536 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
538 return sysfs_emit(buf, "%u.%u\n", (pd->revision >> 8) & 0xff, (pd->revision >> 4) & 0xf);
540 static DEVICE_ATTR_RO(revision);
542 static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
544 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
546 return sysfs_emit(buf, "%u.%u\n", (pd->version >> 8) & 0xff, (pd->version >> 4) & 0xf);
548 static DEVICE_ATTR_RO(version);
550 static struct attribute *pd_attrs[] = {
551 &dev_attr_revision.attr,
552 &dev_attr_version.attr,
556 static umode_t pd_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
558 struct usb_power_delivery *pd = to_usb_power_delivery(kobj_to_dev(kobj));
560 if (attr == &dev_attr_version.attr && !pd->version)
566 static const struct attribute_group pd_group = {
567 .is_visible = pd_attr_is_visible,
570 __ATTRIBUTE_GROUPS(pd);
572 static void pd_release(struct device *dev)
574 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
576 ida_simple_remove(&pd_ida, pd->id);
580 static struct device_type pd_type = {
581 .name = "usb_power_delivery",
582 .release = pd_release,
586 struct usb_power_delivery *usb_power_delivery_find(const char *name)
590 dev = class_find_device_by_name(&pd_class, name);
592 return dev ? to_usb_power_delivery(dev) : NULL;
596 * usb_power_delivery_register - Register USB Power Delivery Support.
597 * @parent: Parent device.
598 * @desc: Description of the USB PD contract.
600 * This routine can be used to register USB Power Delivery capabilities that a
601 * device or devices can support. These capabilities represent all the
602 * capabilities that can be negotiated with a partner, so not only the Power
603 * Capabilities that are negotiated using the USB PD Capabilities Message.
605 * The USB Power Delivery Support object that this routine generates can be used
606 * as the parent object for all the actual USB Power Delivery Messages and
607 * objects that can be negotiated with the partner.
609 * Returns handle to struct usb_power_delivery or ERR_PTR.
611 struct usb_power_delivery *
612 usb_power_delivery_register(struct device *parent, struct usb_power_delivery_desc *desc)
614 struct usb_power_delivery *pd;
617 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
619 return ERR_PTR(-ENOMEM);
621 ret = ida_simple_get(&pd_ida, 0, 0, GFP_KERNEL);
628 pd->revision = desc->revision;
629 pd->version = desc->version;
631 pd->dev.parent = parent;
632 pd->dev.type = &pd_type;
633 pd->dev.class = &pd_class;
634 dev_set_name(&pd->dev, "pd%d", pd->id);
636 ret = device_register(&pd->dev);
638 put_device(&pd->dev);
644 EXPORT_SYMBOL_GPL(usb_power_delivery_register);
647 * usb_power_delivery_unregister - Unregister USB Power Delivery Support.
648 * @pd: The USB PD contract.
650 void usb_power_delivery_unregister(struct usb_power_delivery *pd)
652 if (IS_ERR_OR_NULL(pd))
655 device_unregister(&pd->dev);
657 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister);
660 * usb_power_delivery_link_device - Link device to its USB PD object.
661 * @pd: The USB PD instance.
664 * This function can be used to create a symlink named "usb_power_delivery" for
665 * @dev that points to @pd.
667 int usb_power_delivery_link_device(struct usb_power_delivery *pd, struct device *dev)
671 if (IS_ERR_OR_NULL(pd) || !dev)
674 ret = sysfs_create_link(&dev->kobj, &pd->dev.kobj, "usb_power_delivery");
678 get_device(&pd->dev);
683 EXPORT_SYMBOL_GPL(usb_power_delivery_link_device);
686 * usb_power_delivery_unlink_device - Unlink device from its USB PD object.
687 * @pd: The USB PD instance.
690 * Remove the symlink that was previously created with pd_link_device().
692 void usb_power_delivery_unlink_device(struct usb_power_delivery *pd, struct device *dev)
694 if (IS_ERR_OR_NULL(pd) || !dev)
697 sysfs_remove_link(&dev->kobj, "usb_power_delivery");
698 put_device(&pd->dev);
701 EXPORT_SYMBOL_GPL(usb_power_delivery_unlink_device);
703 /* -------------------------------------------------------------------------- */
705 int __init usb_power_delivery_init(void)
707 return class_register(&pd_class);
710 void __exit usb_power_delivery_exit(void)
712 ida_destroy(&pd_ida);
713 class_unregister(&pd_class);