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",
21 #define to_pdo(o) container_of(o, struct pdo, dev)
29 static void pdo_release(struct device *dev)
34 /* -------------------------------------------------------------------------- */
38 dual_role_power_show(struct device *dev, struct device_attribute *attr, char *buf)
40 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DUAL_ROLE));
42 static DEVICE_ATTR_RO(dual_role_power);
45 usb_suspend_supported_show(struct device *dev, struct device_attribute *attr, char *buf)
47 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_SUSPEND));
49 static DEVICE_ATTR_RO(usb_suspend_supported);
52 higher_capability_show(struct device *dev, struct device_attribute *attr, char *buf)
54 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_HIGHER_CAP));
56 static DEVICE_ATTR_RO(higher_capability);
59 unconstrained_power_show(struct device *dev, struct device_attribute *attr, char *buf)
61 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_EXTPOWER));
63 static DEVICE_ATTR_RO(unconstrained_power);
66 usb_communication_capable_show(struct device *dev, struct device_attribute *attr, char *buf)
68 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_USB_COMM));
70 static DEVICE_ATTR_RO(usb_communication_capable);
73 dual_role_data_show(struct device *dev, struct device_attribute *attr, char *buf)
75 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DATA_SWAP));
77 static DEVICE_ATTR_RO(dual_role_data);
80 unchunked_extended_messages_supported_show(struct device *dev,
81 struct device_attribute *attr, char *buf)
83 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_UNCHUNK_EXT));
85 static DEVICE_ATTR_RO(unchunked_extended_messages_supported);
88 * REVISIT: Peak Current requires access also to the RDO.
90 peak_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)
99 return sysfs_emit(buf, "%u\n", to_pdo(dev)->pdo >> PDO_FIXED_FRS_CURR_SHIFT) & 3;
101 static DEVICE_ATTR_RO(fast_role_swap_current);
103 static ssize_t voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
105 return sysfs_emit(buf, "%umV\n", pdo_fixed_voltage(to_pdo(dev)->pdo));
107 static DEVICE_ATTR_RO(voltage);
109 /* Shared with Variable supplies, both source and sink */
110 static ssize_t current_show(struct device *dev, struct device_attribute *attr, char *buf)
112 return sysfs_emit(buf, "%umA\n", pdo_max_current(to_pdo(dev)->pdo));
115 /* Shared with Variable type supplies */
116 static struct device_attribute maximum_current_attr = {
118 .name = "maximum_current",
121 .show = current_show,
124 static struct device_attribute operational_current_attr = {
126 .name = "operational_current",
129 .show = current_show,
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,
145 static umode_t fixed_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
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)
157 static const struct attribute_group source_fixed_supply_group = {
158 .is_visible = fixed_attr_is_visible,
159 .attrs = source_fixed_supply_attrs,
161 __ATTRIBUTE_GROUPS(source_fixed_supply);
163 static struct device_type source_fixed_supply_type = {
165 .release = pdo_release,
166 .groups = source_fixed_supply_groups,
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,
182 static const struct attribute_group sink_fixed_supply_group = {
183 .is_visible = fixed_attr_is_visible,
184 .attrs = sink_fixed_supply_attrs,
186 __ATTRIBUTE_GROUPS(sink_fixed_supply);
188 static struct device_type sink_fixed_supply_type = {
190 .release = pdo_release,
191 .groups = sink_fixed_supply_groups,
194 /* -------------------------------------------------------------------------- */
195 /* Variable Supply */
198 maximum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
200 return sysfs_emit(buf, "%umV\n", pdo_max_voltage(to_pdo(dev)->pdo));
202 static DEVICE_ATTR_RO(maximum_voltage);
205 minimum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
207 return sysfs_emit(buf, "%umV\n", pdo_min_voltage(to_pdo(dev)->pdo));
209 static DEVICE_ATTR_RO(minimum_voltage);
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,
217 ATTRIBUTE_GROUPS(source_variable_supply);
219 static struct device_type source_variable_supply_type = {
221 .release = pdo_release,
222 .groups = source_variable_supply_groups,
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,
231 ATTRIBUTE_GROUPS(sink_variable_supply);
233 static struct device_type sink_variable_supply_type = {
235 .release = pdo_release,
236 .groups = sink_variable_supply_groups,
239 /* -------------------------------------------------------------------------- */
243 maximum_power_show(struct device *dev, struct device_attribute *attr, char *buf)
245 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
247 static DEVICE_ATTR_RO(maximum_power);
250 operational_power_show(struct device *dev, struct device_attribute *attr, char *buf)
252 return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
254 static DEVICE_ATTR_RO(operational_power);
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,
262 ATTRIBUTE_GROUPS(source_battery);
264 static struct device_type source_battery_type = {
266 .release = pdo_release,
267 .groups = source_battery_groups,
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,
276 ATTRIBUTE_GROUPS(sink_battery);
278 static struct device_type sink_battery_type = {
280 .release = pdo_release,
281 .groups = sink_battery_groups,
284 /* -------------------------------------------------------------------------- */
285 /* Standard Power Range (SPR) Programmable Power Supply (PPS) */
288 pps_power_limited_show(struct device *dev, struct device_attribute *attr, char *buf)
290 return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & BIT(27)));
292 static DEVICE_ATTR_RO(pps_power_limited);
295 pps_max_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
297 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_max_voltage(to_pdo(dev)->pdo));
301 pps_min_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
303 return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_min_voltage(to_pdo(dev)->pdo));
307 pps_max_current_show(struct device *dev, struct device_attribute *attr, char *buf)
309 return sysfs_emit(buf, "%umA\n", pdo_pps_apdo_max_current(to_pdo(dev)->pdo));
312 static struct device_attribute pps_max_voltage_attr = {
314 .name = "maximum_voltage",
317 .show = pps_max_voltage_show,
320 static struct device_attribute pps_min_voltage_attr = {
322 .name = "minimum_voltage",
325 .show = pps_min_voltage_show,
328 static struct device_attribute pps_max_current_attr = {
330 .name = "maximum_current",
333 .show = pps_max_current_show,
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,
343 ATTRIBUTE_GROUPS(source_pps);
345 static struct device_type source_pps_type = {
347 .release = pdo_release,
348 .groups = source_pps_groups,
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,
357 ATTRIBUTE_GROUPS(sink_pps);
359 static struct device_type sink_pps_type = {
361 .release = pdo_release,
362 .groups = sink_pps_groups,
365 /* -------------------------------------------------------------------------- */
367 static const char * const supply_name[] = {
368 [PDO_TYPE_FIXED] = "fixed_supply",
369 [PDO_TYPE_BATT] = "battery",
370 [PDO_TYPE_VAR] = "variable_supply",
373 static const char * const apdo_supply_name[] = {
374 [APDO_TYPE_PPS] = "programmable_supply",
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,
383 static struct device_type *source_apdo_type[] = {
384 [APDO_TYPE_PPS] = &source_pps_type,
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,
393 static struct device_type *sink_apdo_type[] = {
394 [APDO_TYPE_PPS] = &sink_pps_type,
397 /* REVISIT: Export when EPR_*_Capabilities need to be supported. */
398 static int add_pdo(struct usb_power_delivery_capabilities *cap, u32 pdo, int position)
400 struct device_type *type;
405 p = kzalloc(sizeof(*p), GFP_KERNEL);
410 p->object_position = position;
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);
420 if (is_source(cap->role))
421 type = source_apdo_type[pdo_apdo_type(pdo)];
423 type = sink_apdo_type[pdo_apdo_type(pdo)];
425 name = apdo_supply_name[pdo_apdo_type(pdo)];
427 if (is_source(cap->role))
428 type = source_type[pdo_type(pdo)];
430 type = sink_type[pdo_type(pdo)];
432 name = supply_name[pdo_type(pdo)];
435 p->dev.parent = &cap->dev;
437 dev_set_name(&p->dev, "%u:%s", position + 1, name);
439 ret = device_register(&p->dev);
448 static int remove_pdo(struct device *dev, void *data)
450 device_unregister(dev);
454 /* -------------------------------------------------------------------------- */
456 static const char * const cap_name[] = {
457 [TYPEC_SINK] = "sink-capabilities",
458 [TYPEC_SOURCE] = "source-capabilities",
461 static void pd_capabilities_release(struct device *dev)
463 kfree(to_usb_power_delivery_capabilities(dev));
466 static struct device_type pd_capabilities_type = {
467 .name = "capabilities",
468 .release = pd_capabilities_release,
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.
476 * This function registers a Capabilities Message described in @desc. The
477 * capabilities will have their own sub-directory under @pd in sysfs.
479 * The function returns pointer to struct usb_power_delivery_capabilities, or
482 struct usb_power_delivery_capabilities *
483 usb_power_delivery_register_capabilities(struct usb_power_delivery *pd,
484 struct usb_power_delivery_capabilities_desc *desc)
486 struct usb_power_delivery_capabilities *cap;
490 cap = kzalloc(sizeof(*cap), GFP_KERNEL);
492 return ERR_PTR(-ENOMEM);
495 cap->role = desc->role;
497 cap->dev.parent = &pd->dev;
498 cap->dev.type = &pd_capabilities_type;
499 dev_set_name(&cap->dev, "%s", cap_name[cap->role]);
501 ret = device_register(&cap->dev);
503 put_device(&cap->dev);
507 for (i = 0; i < PDO_MAX_OBJECTS && desc->pdo[i]; i++) {
508 ret = add_pdo(cap, desc->pdo[i], i);
510 usb_power_delivery_unregister_capabilities(cap);
517 EXPORT_SYMBOL_GPL(usb_power_delivery_register_capabilities);
520 * usb_power_delivery_unregister_capabilities - Unregister a set of capabilities
521 * @cap: The capabilities
523 void usb_power_delivery_unregister_capabilities(struct usb_power_delivery_capabilities *cap)
528 device_for_each_child(&cap->dev, NULL, remove_pdo);
529 device_unregister(&cap->dev);
531 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister_capabilities);
533 /* -------------------------------------------------------------------------- */
535 static ssize_t revision_show(struct device *dev, struct device_attribute *attr, char *buf)
537 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
539 return sysfs_emit(buf, "%u.%u\n", (pd->revision >> 8) & 0xff, (pd->revision >> 4) & 0xf);
541 static DEVICE_ATTR_RO(revision);
543 static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
545 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
547 return sysfs_emit(buf, "%u.%u\n", (pd->version >> 8) & 0xff, (pd->version >> 4) & 0xf);
549 static DEVICE_ATTR_RO(version);
551 static struct attribute *pd_attrs[] = {
552 &dev_attr_revision.attr,
553 &dev_attr_version.attr,
557 static umode_t pd_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
559 struct usb_power_delivery *pd = to_usb_power_delivery(kobj_to_dev(kobj));
561 if (attr == &dev_attr_version.attr && !pd->version)
567 static const struct attribute_group pd_group = {
568 .is_visible = pd_attr_is_visible,
571 __ATTRIBUTE_GROUPS(pd);
573 static void pd_release(struct device *dev)
575 struct usb_power_delivery *pd = to_usb_power_delivery(dev);
577 ida_simple_remove(&pd_ida, pd->id);
581 static struct device_type pd_type = {
582 .name = "usb_power_delivery",
583 .release = pd_release,
587 struct usb_power_delivery *usb_power_delivery_find(const char *name)
591 dev = class_find_device_by_name(&pd_class, name);
593 return dev ? to_usb_power_delivery(dev) : NULL;
597 * usb_power_delivery_register - Register USB Power Delivery Support.
598 * @parent: Parent device.
599 * @desc: Description of the USB PD contract.
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.
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.
610 * Returns handle to struct usb_power_delivery or ERR_PTR.
612 struct usb_power_delivery *
613 usb_power_delivery_register(struct device *parent, struct usb_power_delivery_desc *desc)
615 struct usb_power_delivery *pd;
618 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
620 return ERR_PTR(-ENOMEM);
622 ret = ida_simple_get(&pd_ida, 0, 0, GFP_KERNEL);
629 pd->revision = desc->revision;
630 pd->version = desc->version;
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);
637 ret = device_register(&pd->dev);
639 put_device(&pd->dev);
645 EXPORT_SYMBOL_GPL(usb_power_delivery_register);
648 * usb_power_delivery_unregister - Unregister USB Power Delivery Support.
649 * @pd: The USB PD contract.
651 void usb_power_delivery_unregister(struct usb_power_delivery *pd)
653 if (IS_ERR_OR_NULL(pd))
656 device_unregister(&pd->dev);
658 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister);
661 * usb_power_delivery_link_device - Link device to its USB PD object.
662 * @pd: The USB PD instance.
665 * This function can be used to create a symlink named "usb_power_delivery" for
666 * @dev that points to @pd.
668 int usb_power_delivery_link_device(struct usb_power_delivery *pd, struct device *dev)
672 if (IS_ERR_OR_NULL(pd) || !dev)
675 ret = sysfs_create_link(&dev->kobj, &pd->dev.kobj, "usb_power_delivery");
679 get_device(&pd->dev);
684 EXPORT_SYMBOL_GPL(usb_power_delivery_link_device);
687 * usb_power_delivery_unlink_device - Unlink device from its USB PD object.
688 * @pd: The USB PD instance.
691 * Remove the symlink that was previously created with pd_link_device().
693 void usb_power_delivery_unlink_device(struct usb_power_delivery *pd, struct device *dev)
695 if (IS_ERR_OR_NULL(pd) || !dev)
698 sysfs_remove_link(&dev->kobj, "usb_power_delivery");
699 put_device(&pd->dev);
702 EXPORT_SYMBOL_GPL(usb_power_delivery_unlink_device);
704 /* -------------------------------------------------------------------------- */
706 int __init usb_power_delivery_init(void)
708 return class_register(&pd_class);
711 void __exit usb_power_delivery_exit(void)
713 ida_destroy(&pd_ida);
714 class_unregister(&pd_class);