}
static DEVICE_ATTR_RO(svid);
+static int increment_duplicated_priority(struct device *dev, void *data)
+{
+ if (is_typec_port_altmode(dev)) {
+ struct typec_altmode **alt_target = (struct typec_altmode **)data;
+ struct typec_altmode *alt = to_typec_altmode(dev);
+
+ if (alt != *alt_target && alt->priority == (*alt_target)->priority) {
+ alt->priority++;
+ *alt_target = alt;
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int find_duplicated_priority(struct device *dev, void *data)
+{
+ if (is_typec_port_altmode(dev)) {
+ struct typec_altmode **alt_target = (struct typec_altmode **)data;
+ struct typec_altmode *alt = to_typec_altmode(dev);
+
+ if (alt != *alt_target && alt->priority == (*alt_target)->priority)
+ return 1;
+ }
+ return 0;
+}
+
+static int typec_mode_set_priority(struct typec_altmode *alt, const u8 priority)
+{
+ struct typec_port *port = to_typec_port(alt->dev.parent);
+ const u8 old_priority = alt->priority;
+ int res = 1;
+
+ alt->priority = priority;
+ while (res) {
+ res = device_for_each_child(&port->dev, &alt, find_duplicated_priority);
+ if (res) {
+ alt->priority++;
+ if (alt->priority == 0) {
+ alt->priority = old_priority;
+ return -EOVERFLOW;
+ }
+ }
+ }
+
+ res = 1;
+ alt->priority = priority;
+ while (res)
+ res = device_for_each_child(&port->dev, &alt,
+ increment_duplicated_priority);
+
+ return 0;
+}
+
+static ssize_t priority_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ u8 val;
+ int err = kstrtou8(buf, 10, &val);
+
+ if (!err)
+ err = typec_mode_set_priority(to_typec_altmode(dev), val);
+
+ if (!err)
+ return size;
+ return err;
+}
+
+static ssize_t priority_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ return sysfs_emit(buf, "%u\n", to_typec_altmode(dev)->priority);
+}
+static DEVICE_ATTR_RW(priority);
+
static struct attribute *typec_altmode_attrs[] = {
&dev_attr_active.attr,
&dev_attr_mode.attr,
&dev_attr_svid.attr,
&dev_attr_vdo.attr,
+ &dev_attr_priority.attr,
NULL
};
struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
struct typec_port *port = typec_altmode2port(adev);
- if (attr == &dev_attr_active.attr)
+ if (attr == &dev_attr_active.attr) {
if (!is_typec_port(adev->dev.parent)) {
if (!port->mode_control || !adev->ops || !adev->ops->activate)
return 0444;
}
+ } else if (attr == &dev_attr_priority.attr) {
+ if (!is_typec_port(adev->dev.parent) || !port->mode_control)
+ return 0;
+ }
return attr->mode;
}
struct typec_altmode *adev;
struct typec_mux *mux;
struct typec_retimer *retimer;
+ int ret;
mux = typec_mux_get(&port->dev);
if (IS_ERR(mux))
} else {
to_altmode(adev)->mux = mux;
to_altmode(adev)->retimer = retimer;
+
+ ret = typec_mode_set_priority(adev, 0);
+ if (ret) {
+ typec_unregister_altmode(adev);
+ return ERR_PTR(ret);
+ }
}
return adev;