1 // SPDX-License-Identifier: GPL-2.0
3 * USB Power Delivery sysfs entries
5 * Copyright (C) 2022, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
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 peak_current_show(struct device *dev, struct device_attribute *attr, char *buf)
89 return sysfs_emit(buf, "%u\n", (to_pdo(dev)->pdo >> PDO_FIXED_PEAK_CURR_SHIFT) & 3);
91 static DEVICE_ATTR_RO(peak_current);
94 fast_role_swap_current_show(struct device *dev, struct device_attribute *attr, char *buf)
96 return sysfs_emit(buf, "%u\n", (to_pdo(dev)->pdo >> PDO_FIXED_FRS_CURR_SHIFT) & 3);
98 static DEVICE_ATTR_RO(fast_role_swap_current);
100 static ssize_t voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
102 return sysfs_emit(buf, "%umV\n", pdo_fixed_voltage(to_pdo(dev)->pdo));
104 static DEVICE_ATTR_RO(voltage);
106 /* Shared with Variable supplies, both source and sink */
107 static ssize_t current_show(struct device *dev, struct device_attribute *attr, char *buf)
109 return sysfs_emit(buf, "%umA\n", pdo_max_current(to_pdo(dev)->pdo));
112 /* Shared with Variable type supplies */
113 static struct device_attribute maximum_current_attr = {
115 .name = "maximum_current",
118 .show = current_show,
121 static struct device_attribute operational_current_attr = {
123 .name = "operational_current",
126 .show = current_show,
129 static struct attribute *source_fixed_supply_attrs[] = {
130 &dev_attr_dual_role_power.attr,
131 &dev_attr_usb_suspend_supported.attr,
132 &dev_attr_unconstrained_power.attr,
133 &dev_attr_usb_communication_capable.attr,
134 &dev_attr_dual_role_data.attr,
135 &dev_attr_unchunked_extended_messages_supported.attr,
136 &dev_attr_peak_current.attr,
137 &dev_attr_voltage.attr,
138 &maximum_current_attr.attr,
142 static umode_t fixed_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
144 if (to_pdo(kobj_to_dev(kobj))->object_position &&
145 attr != &dev_attr_peak_current.attr &&
146 attr != &dev_attr_voltage.attr &&
147 attr != &maximum_current_attr.attr &&
148 attr != &operational_current_attr.attr)
154 static const struct attribute_group source_fixed_supply_group = {
155 .is_visible = fixed_attr_is_visible,
156 .attrs = source_fixed_supply_attrs,
158 __ATTRIBUTE_GROUPS(source_fixed_supply);
160 static struct device_type source_fixed_supply_type = {
162 .release = pdo_release,
163 .groups = source_fixed_supply_groups,
166 static struct attribute *sink_fixed_supply_attrs[] = {
167 &dev_attr_dual_role_power.attr,
168 &dev_attr_higher_capability.attr,
169 &dev_attr_unconstrained_power.attr,
170 &dev_attr_usb_communication_capable.attr,
171 &dev_attr_dual_role_data.attr,
172 &dev_attr_unchunked_extended_messages_supported.attr,
173 &dev_attr_fast_role_swap_current.attr,
174 &dev_attr_voltage.attr,
175 &operational_current_attr.attr,
179 static const struct attribute_group sink_fixed_supply_group = {
180 .is_visible = fixed_attr_is_visible,
181 .attrs = sink_fixed_supply_attrs,
183 __ATTRIBUTE_GROUPS(sink_fixed_supply);
185 static struct device_type sink_fixed_supply_type = {
187 .release = pdo_release,
188 .groups = sink_fixed_supply_groups,
191 /* -------------------------------------------------------------------------- */
192 /* Variable Supply */
195 maximum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
197 return sysfs_emit(buf, "%umV\n", pdo_max_voltage(to_pdo(dev)->pdo));
199 static DEVICE_ATTR_RO(maximum_voltage);
202 minimum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
204 return sysfs_emit(buf, "%umV\n", pdo_min_voltage(to_pdo(dev)->pdo));
206 static DEVICE_ATTR_RO(minimum_voltage);
208 static struct attribute *source_variable_supply_attrs[] = {
209 &dev_attr_maximum_voltage.attr,
210 &dev_attr_minimum_voltage.attr,
211 &maximum_current_attr.attr,
214 ATTRIBUTE_GROUPS(source_variable_supply);
216 static struct device_type source_variable_supply_type = {
218 .release = pdo_release,
219 .groups = source_variable_supply_groups,
222 static struct attribute *sink_variable_supply_attrs[] = {
223 &dev_attr_maximum_voltage.attr,
224 &dev_attr_minimum_voltage.attr,
225 &operational_current_attr.attr,
228 ATTRIBUTE_GROUPS(sink_variable_supply);
230 static struct device_type sink_variable_supply_type = {
232 .release = pdo_release,
233 .groups = sink_variable_supply_groups,
236 /* -------------------------------------------------------------------------- */
240 maximum_power_show(struct device *dev, struct device_attribute *attr, char *buf)
242 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
244 static DEVICE_ATTR_RO(maximum_power);
247 operational_power_show(struct device *dev, struct device_attribute *attr, char *buf)
249 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
251 static DEVICE_ATTR_RO(operational_power);
253 static struct attribute *source_battery_attrs[] = {
254 &dev_attr_maximum_voltage.attr,
255 &dev_attr_minimum_voltage.attr,
256 &dev_attr_maximum_power.attr,
259 ATTRIBUTE_GROUPS(source_battery);
261 static struct device_type source_battery_type = {
263 .release = pdo_release,
264 .groups = source_battery_groups,
267 static struct attribute *sink_battery_attrs[] = {
268 &dev_attr_maximum_voltage.attr,
269 &dev_attr_minimum_voltage.attr,
270 &dev_attr_operational_power.attr,
273 ATTRIBUTE_GROUPS(sink_battery);
275 static struct device_type sink_battery_type = {
277 .release = pdo_release,
278 .groups = sink_battery_groups,
281 /* -------------------------------------------------------------------------- */
282 /* Standard Power Range (SPR) Programmable Power Supply (PPS) */
285 pps_power_limited_show(struct device *dev, struct device_attribute *attr, char *buf)
287 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & BIT(27)));
289 static DEVICE_ATTR_RO(pps_power_limited);
292 pps_max_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
294 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_max_voltage(to_pdo(dev)->pdo));
298 pps_min_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
300 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_min_voltage(to_pdo(dev)->pdo));
304 pps_max_current_show(struct device *dev, struct device_attribute *attr, char *buf)
306 return sysfs_emit(buf, "%umA\n", pdo_pps_apdo_max_current(to_pdo(dev)->pdo));
309 static struct device_attribute pps_max_voltage_attr = {
311 .name = "maximum_voltage",
314 .show = pps_max_voltage_show,
317 static struct device_attribute pps_min_voltage_attr = {
319 .name = "minimum_voltage",
322 .show = pps_min_voltage_show,
325 static struct device_attribute pps_max_current_attr = {
327 .name = "maximum_current",
330 .show = pps_max_current_show,
333 static struct attribute *source_pps_attrs[] = {
334 &dev_attr_pps_power_limited.attr,
335 &pps_max_voltage_attr.attr,
336 &pps_min_voltage_attr.attr,
337 &pps_max_current_attr.attr,
340 ATTRIBUTE_GROUPS(source_pps);
342 static struct device_type source_pps_type = {
344 .release = pdo_release,
345 .groups = source_pps_groups,
348 static struct attribute *sink_pps_attrs[] = {
349 &pps_max_voltage_attr.attr,
350 &pps_min_voltage_attr.attr,
351 &pps_max_current_attr.attr,
354 ATTRIBUTE_GROUPS(sink_pps);
356 static struct device_type sink_pps_type = {
358 .release = pdo_release,
359 .groups = sink_pps_groups,
362 /* -------------------------------------------------------------------------- */
364 static const char * const supply_name[] = {
365 [PDO_TYPE_FIXED] = "fixed_supply",
366 [PDO_TYPE_BATT] = "battery",
367 [PDO_TYPE_VAR] = "variable_supply",
370 static const char * const apdo_supply_name[] = {
371 [APDO_TYPE_PPS] = "programmable_supply",
374 static struct device_type *source_type[] = {
375 [PDO_TYPE_FIXED] = &source_fixed_supply_type,
376 [PDO_TYPE_BATT] = &source_battery_type,
377 [PDO_TYPE_VAR] = &source_variable_supply_type,
380 static struct device_type *source_apdo_type[] = {
381 [APDO_TYPE_PPS] = &source_pps_type,
384 static struct device_type *sink_type[] = {
385 [PDO_TYPE_FIXED] = &sink_fixed_supply_type,
386 [PDO_TYPE_BATT] = &sink_battery_type,
387 [PDO_TYPE_VAR] = &sink_variable_supply_type,
390 static struct device_type *sink_apdo_type[] = {
391 [APDO_TYPE_PPS] = &sink_pps_type,
394 /* REVISIT: Export when EPR_*_Capabilities need to be supported. */
395 static int add_pdo(struct usb_power_delivery_capabilities *cap, u32 pdo, int position)
397 struct device_type *type;
402 p = kzalloc(sizeof(*p), GFP_KERNEL);
407 p->object_position = position;
409 if (pdo_type(pdo) == PDO_TYPE_APDO) {
410 /* FIXME: Only PPS supported for now! Skipping others. */
411 if (pdo_apdo_type(pdo) > APDO_TYPE_PPS) {
412 dev_warn(&cap->dev, "Unknown APDO type. PDO 0x%08x\n", pdo);
417 if (is_source(cap->role))
418 type = source_apdo_type[pdo_apdo_type(pdo)];
420 type = sink_apdo_type[pdo_apdo_type(pdo)];
422 name = apdo_supply_name[pdo_apdo_type(pdo)];
424 if (is_source(cap->role))
425 type = source_type[pdo_type(pdo)];
427 type = sink_type[pdo_type(pdo)];
429 name = supply_name[pdo_type(pdo)];
432 p->dev.parent = &cap->dev;
434 dev_set_name(&p->dev, "%u:%s", position + 1, name);
436 ret = device_register(&p->dev);
445 static int remove_pdo(struct device *dev, void *data)
447 device_unregister(dev);
451 /* -------------------------------------------------------------------------- */
453 static const char * const cap_name[] = {
454 [TYPEC_SINK] = "sink-capabilities",
455 [TYPEC_SOURCE] = "source-capabilities",
458 static void pd_capabilities_release(struct device *dev)
460 kfree(to_usb_power_delivery_capabilities(dev));
463 static struct device_type pd_capabilities_type = {
464 .name = "capabilities",
465 .release = pd_capabilities_release,
469 * usb_power_delivery_register_capabilities - Register a set of capabilities.
470 * @pd: The USB PD instance that the capabilities belong to.
471 * @desc: Description of the Capabilities Message.
473 * This function registers a Capabilities Message described in @desc. The
474 * capabilities will have their own sub-directory under @pd in sysfs.
476 * The function returns pointer to struct usb_power_delivery_capabilities, or
479 struct usb_power_delivery_capabilities *
480 usb_power_delivery_register_capabilities(struct usb_power_delivery *pd,
481 struct usb_power_delivery_capabilities_desc *desc)
483 struct usb_power_delivery_capabilities *cap;
487 cap = kzalloc(sizeof(*cap), GFP_KERNEL);
489 return ERR_PTR(-ENOMEM);
492 cap->role = desc->role;
494 cap->dev.parent = &pd->dev;
495 cap->dev.type = &pd_capabilities_type;
496 dev_set_name(&cap->dev, "%s", cap_name[cap->role]);
498 ret = device_register(&cap->dev);
500 put_device(&cap->dev);
504 for (i = 0; i < PDO_MAX_OBJECTS && desc->pdo[i]; i++) {
505 ret = add_pdo(cap, desc->pdo[i], i);
507 usb_power_delivery_unregister_capabilities(cap);
514 EXPORT_SYMBOL_GPL(usb_power_delivery_register_capabilities);
517 * usb_power_delivery_unregister_capabilities - Unregister a set of capabilities
518 * @cap: The capabilities
520 void usb_power_delivery_unregister_capabilities(struct usb_power_delivery_capabilities *cap)
525 device_for_each_child(&cap->dev, NULL, remove_pdo);
526 device_unregister(&cap->dev);
528 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister_capabilities);
530 /* -------------------------------------------------------------------------- */
532 static ssize_t revision_show(struct device *dev, struct device_attribute *attr, char *buf)
534 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
536 return sysfs_emit(buf, "%u.%u\n", (pd->revision >> 8) & 0xff, (pd->revision >> 4) & 0xf);
538 static DEVICE_ATTR_RO(revision);
540 static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
542 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
544 return sysfs_emit(buf, "%u.%u\n", (pd->version >> 8) & 0xff, (pd->version >> 4) & 0xf);
546 static DEVICE_ATTR_RO(version);
548 static struct attribute *pd_attrs[] = {
549 &dev_attr_revision.attr,
550 &dev_attr_version.attr,
554 static umode_t pd_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
556 struct usb_power_delivery *pd = to_usb_power_delivery(kobj_to_dev(kobj));
558 if (attr == &dev_attr_version.attr && !pd->version)
564 static const struct attribute_group pd_group = {
565 .is_visible = pd_attr_is_visible,
568 __ATTRIBUTE_GROUPS(pd);
570 static void pd_release(struct device *dev)
572 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
574 ida_free(&pd_ida, pd->id);
578 static struct device_type pd_type = {
579 .name = "usb_power_delivery",
580 .release = pd_release,
584 struct usb_power_delivery *usb_power_delivery_find(const char *name)
588 dev = class_find_device_by_name(&pd_class, name);
590 return dev ? to_usb_power_delivery(dev) : NULL;
594 * usb_power_delivery_register - Register USB Power Delivery Support.
595 * @parent: Parent device.
596 * @desc: Description of the USB PD contract.
598 * This routine can be used to register USB Power Delivery capabilities that a
599 * device or devices can support. These capabilities represent all the
600 * capabilities that can be negotiated with a partner, so not only the Power
601 * Capabilities that are negotiated using the USB PD Capabilities Message.
603 * The USB Power Delivery Support object that this routine generates can be used
604 * as the parent object for all the actual USB Power Delivery Messages and
605 * objects that can be negotiated with the partner.
607 * Returns handle to struct usb_power_delivery or ERR_PTR.
609 struct usb_power_delivery *
610 usb_power_delivery_register(struct device *parent, struct usb_power_delivery_desc *desc)
612 struct usb_power_delivery *pd;
615 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
617 return ERR_PTR(-ENOMEM);
619 ret = ida_alloc(&pd_ida, GFP_KERNEL);
626 pd->revision = desc->revision;
627 pd->version = desc->version;
629 pd->dev.parent = parent;
630 pd->dev.type = &pd_type;
631 pd->dev.class = &pd_class;
632 dev_set_name(&pd->dev, "pd%d", pd->id);
634 ret = device_register(&pd->dev);
636 put_device(&pd->dev);
642 EXPORT_SYMBOL_GPL(usb_power_delivery_register);
645 * usb_power_delivery_unregister - Unregister USB Power Delivery Support.
646 * @pd: The USB PD contract.
648 void usb_power_delivery_unregister(struct usb_power_delivery *pd)
650 if (IS_ERR_OR_NULL(pd))
653 device_unregister(&pd->dev);
655 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister);
658 * usb_power_delivery_link_device - Link device to its USB PD object.
659 * @pd: The USB PD instance.
662 * This function can be used to create a symlink named "usb_power_delivery" for
663 * @dev that points to @pd.
665 int usb_power_delivery_link_device(struct usb_power_delivery *pd, struct device *dev)
669 if (IS_ERR_OR_NULL(pd) || !dev)
672 ret = sysfs_create_link(&dev->kobj, &pd->dev.kobj, "usb_power_delivery");
676 get_device(&pd->dev);
681 EXPORT_SYMBOL_GPL(usb_power_delivery_link_device);
684 * usb_power_delivery_unlink_device - Unlink device from its USB PD object.
685 * @pd: The USB PD instance.
688 * Remove the symlink that was previously created with pd_link_device().
690 void usb_power_delivery_unlink_device(struct usb_power_delivery *pd, struct device *dev)
692 if (IS_ERR_OR_NULL(pd) || !dev)
695 sysfs_remove_link(&dev->kobj, "usb_power_delivery");
696 put_device(&pd->dev);
699 EXPORT_SYMBOL_GPL(usb_power_delivery_unlink_device);
701 /* -------------------------------------------------------------------------- */
703 int __init usb_power_delivery_init(void)
705 return class_register(&pd_class);
708 void __exit usb_power_delivery_exit(void)
710 ida_destroy(&pd_ida);
711 class_unregister(&pd_class);