| .. | .. |
|---|
| 1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
|---|
| 2 | | -/** |
|---|
| 2 | +/* |
|---|
| 3 | 3 | * USB Type-C Multiplexer/DeMultiplexer Switch support |
|---|
| 4 | 4 | * |
|---|
| 5 | 5 | * Copyright (C) 2018 Intel Corporation |
|---|
| .. | .. |
|---|
| 16 | 16 | #include <linux/usb/typec_mux.h> |
|---|
| 17 | 17 | |
|---|
| 18 | 18 | #include "bus.h" |
|---|
| 19 | | - |
|---|
| 20 | | -static int name_match(struct device *dev, const void *name) |
|---|
| 21 | | -{ |
|---|
| 22 | | - return !strcmp((const char *)name, dev_name(dev)); |
|---|
| 23 | | -} |
|---|
| 24 | 19 | |
|---|
| 25 | 20 | static bool dev_name_ends_with(struct device *dev, const char *suffix) |
|---|
| 26 | 21 | { |
|---|
| .. | .. |
|---|
| 39 | 34 | return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-switch"); |
|---|
| 40 | 35 | } |
|---|
| 41 | 36 | |
|---|
| 42 | | -static void *typec_switch_match(struct device_connection *con, int ep, |
|---|
| 37 | +static void *typec_switch_match(struct fwnode_handle *fwnode, const char *id, |
|---|
| 43 | 38 | void *data) |
|---|
| 44 | 39 | { |
|---|
| 45 | 40 | struct device *dev; |
|---|
| 46 | 41 | |
|---|
| 47 | | - if (con->fwnode) { |
|---|
| 48 | | - if (con->id && !fwnode_property_present(con->fwnode, con->id)) |
|---|
| 49 | | - return NULL; |
|---|
| 42 | + if (id && !fwnode_property_present(fwnode, id)) |
|---|
| 43 | + return NULL; |
|---|
| 50 | 44 | |
|---|
| 51 | | - dev = class_find_device(&typec_mux_class, NULL, con->fwnode, |
|---|
| 52 | | - switch_fwnode_match); |
|---|
| 53 | | - } else { |
|---|
| 54 | | - dev = class_find_device(&typec_mux_class, NULL, |
|---|
| 55 | | - con->endpoint[ep], name_match); |
|---|
| 56 | | - } |
|---|
| 45 | + dev = class_find_device(&typec_mux_class, NULL, fwnode, |
|---|
| 46 | + switch_fwnode_match); |
|---|
| 57 | 47 | |
|---|
| 58 | 48 | return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER); |
|---|
| 59 | 49 | } |
|---|
| 60 | 50 | |
|---|
| 61 | 51 | /** |
|---|
| 62 | | - * typec_switch_get - Find USB Type-C orientation switch |
|---|
| 63 | | - * @dev: The caller device |
|---|
| 52 | + * fwnode_typec_switch_get - Find USB Type-C orientation switch |
|---|
| 53 | + * @fwnode: The caller device node |
|---|
| 64 | 54 | * |
|---|
| 65 | 55 | * Finds a switch linked with @dev. Returns a reference to the switch on |
|---|
| 66 | 56 | * success, NULL if no matching connection was found, or |
|---|
| 67 | 57 | * ERR_PTR(-EPROBE_DEFER) when a connection was found but the switch |
|---|
| 68 | 58 | * has not been enumerated yet. |
|---|
| 69 | 59 | */ |
|---|
| 70 | | -struct typec_switch *typec_switch_get(struct device *dev) |
|---|
| 60 | +struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode) |
|---|
| 71 | 61 | { |
|---|
| 72 | 62 | struct typec_switch *sw; |
|---|
| 73 | 63 | |
|---|
| 74 | | - sw = device_connection_find_match(dev, "orientation-switch", NULL, |
|---|
| 64 | + sw = fwnode_connection_find_match(fwnode, "orientation-switch", NULL, |
|---|
| 75 | 65 | typec_switch_match); |
|---|
| 76 | 66 | if (!IS_ERR_OR_NULL(sw)) |
|---|
| 77 | 67 | WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); |
|---|
| 78 | 68 | |
|---|
| 79 | 69 | return sw; |
|---|
| 80 | 70 | } |
|---|
| 81 | | -EXPORT_SYMBOL_GPL(typec_switch_get); |
|---|
| 71 | +EXPORT_SYMBOL_GPL(fwnode_typec_switch_get); |
|---|
| 82 | 72 | |
|---|
| 83 | 73 | /** |
|---|
| 84 | | - * typec_put_switch - Release USB Type-C orientation switch |
|---|
| 74 | + * typec_switch_put - Release USB Type-C orientation switch |
|---|
| 85 | 75 | * @sw: USB Type-C orientation switch |
|---|
| 86 | 76 | * |
|---|
| 87 | 77 | * Decrement reference count for @sw. |
|---|
| .. | .. |
|---|
| 137 | 127 | sw->dev.class = &typec_mux_class; |
|---|
| 138 | 128 | sw->dev.type = &typec_switch_dev_type; |
|---|
| 139 | 129 | sw->dev.driver_data = desc->drvdata; |
|---|
| 140 | | - dev_set_name(&sw->dev, "%s-switch", dev_name(parent)); |
|---|
| 130 | + ret = dev_set_name(&sw->dev, "%s-switch", desc->name ? desc->name : dev_name(parent)); |
|---|
| 131 | + if (ret) { |
|---|
| 132 | + put_device(&sw->dev); |
|---|
| 133 | + return ERR_PTR(ret); |
|---|
| 134 | + } |
|---|
| 141 | 135 | |
|---|
| 142 | 136 | ret = device_add(&sw->dev); |
|---|
| 143 | 137 | if (ret) { |
|---|
| .. | .. |
|---|
| 149 | 143 | return sw; |
|---|
| 150 | 144 | } |
|---|
| 151 | 145 | EXPORT_SYMBOL_GPL(typec_switch_register); |
|---|
| 146 | + |
|---|
| 147 | +int typec_switch_set(struct typec_switch *sw, |
|---|
| 148 | + enum typec_orientation orientation) |
|---|
| 149 | +{ |
|---|
| 150 | + if (IS_ERR_OR_NULL(sw)) |
|---|
| 151 | + return 0; |
|---|
| 152 | + |
|---|
| 153 | + return sw->set(sw, orientation); |
|---|
| 154 | +} |
|---|
| 155 | +EXPORT_SYMBOL_GPL(typec_switch_set); |
|---|
| 152 | 156 | |
|---|
| 153 | 157 | /** |
|---|
| 154 | 158 | * typec_switch_unregister - Unregister USB Type-C orientation switch |
|---|
| .. | .. |
|---|
| 182 | 186 | return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-mux"); |
|---|
| 183 | 187 | } |
|---|
| 184 | 188 | |
|---|
| 185 | | -static void *typec_mux_match(struct device_connection *con, int ep, void *data) |
|---|
| 189 | +static void *typec_mux_match(struct fwnode_handle *fwnode, const char *id, |
|---|
| 190 | + void *data) |
|---|
| 186 | 191 | { |
|---|
| 187 | 192 | const struct typec_altmode_desc *desc = data; |
|---|
| 188 | 193 | struct device *dev; |
|---|
| 189 | 194 | bool match; |
|---|
| 190 | 195 | int nval; |
|---|
| 191 | 196 | u16 *val; |
|---|
| 197 | + int ret; |
|---|
| 192 | 198 | int i; |
|---|
| 193 | | - |
|---|
| 194 | | - if (!con->fwnode) { |
|---|
| 195 | | - dev = class_find_device(&typec_mux_class, NULL, |
|---|
| 196 | | - con->endpoint[ep], name_match); |
|---|
| 197 | | - |
|---|
| 198 | | - return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER); |
|---|
| 199 | | - } |
|---|
| 200 | 199 | |
|---|
| 201 | 200 | /* |
|---|
| 202 | 201 | * Check has the identifier already been "consumed". If it |
|---|
| 203 | 202 | * has, no need to do any extra connection identification. |
|---|
| 204 | 203 | */ |
|---|
| 205 | | - match = !con->id; |
|---|
| 204 | + match = !id; |
|---|
| 206 | 205 | if (match) |
|---|
| 207 | 206 | goto find_mux; |
|---|
| 208 | 207 | |
|---|
| 209 | 208 | /* Accessory Mode muxes */ |
|---|
| 210 | 209 | if (!desc) { |
|---|
| 211 | | - match = fwnode_property_present(con->fwnode, "accessory"); |
|---|
| 210 | + match = fwnode_property_present(fwnode, "accessory"); |
|---|
| 212 | 211 | if (match) |
|---|
| 213 | 212 | goto find_mux; |
|---|
| 214 | 213 | return NULL; |
|---|
| 215 | 214 | } |
|---|
| 216 | 215 | |
|---|
| 217 | 216 | /* Alternate Mode muxes */ |
|---|
| 218 | | - nval = fwnode_property_count_u16(con->fwnode, "svid"); |
|---|
| 217 | + nval = fwnode_property_count_u16(fwnode, "svid"); |
|---|
| 219 | 218 | if (nval <= 0) |
|---|
| 220 | 219 | return NULL; |
|---|
| 221 | 220 | |
|---|
| .. | .. |
|---|
| 223 | 222 | if (!val) |
|---|
| 224 | 223 | return ERR_PTR(-ENOMEM); |
|---|
| 225 | 224 | |
|---|
| 226 | | - nval = fwnode_property_read_u16_array(con->fwnode, "svid", val, nval); |
|---|
| 227 | | - if (nval < 0) { |
|---|
| 225 | + ret = fwnode_property_read_u16_array(fwnode, "svid", val, nval); |
|---|
| 226 | + if (ret < 0) { |
|---|
| 228 | 227 | kfree(val); |
|---|
| 229 | | - return ERR_PTR(nval); |
|---|
| 228 | + return ERR_PTR(ret); |
|---|
| 230 | 229 | } |
|---|
| 231 | 230 | |
|---|
| 232 | 231 | for (i = 0; i < nval; i++) { |
|---|
| .. | .. |
|---|
| 240 | 239 | return NULL; |
|---|
| 241 | 240 | |
|---|
| 242 | 241 | find_mux: |
|---|
| 243 | | - dev = class_find_device(&typec_mux_class, NULL, con->fwnode, |
|---|
| 242 | + dev = class_find_device(&typec_mux_class, NULL, fwnode, |
|---|
| 244 | 243 | mux_fwnode_match); |
|---|
| 245 | 244 | |
|---|
| 246 | | - return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER); |
|---|
| 245 | + return dev ? to_typec_mux(dev) : ERR_PTR(-EPROBE_DEFER); |
|---|
| 247 | 246 | } |
|---|
| 248 | 247 | |
|---|
| 249 | 248 | /** |
|---|
| 250 | | - * typec_mux_get - Find USB Type-C Multiplexer |
|---|
| 251 | | - * @dev: The caller device |
|---|
| 249 | + * fwnode_typec_mux_get - Find USB Type-C Multiplexer |
|---|
| 250 | + * @fwnode: The caller device node |
|---|
| 252 | 251 | * @desc: Alt Mode description |
|---|
| 253 | 252 | * |
|---|
| 254 | 253 | * Finds a mux linked to the caller. This function is primarily meant for the |
|---|
| .. | .. |
|---|
| 256 | 255 | * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection |
|---|
| 257 | 256 | * was found but the mux has not been enumerated yet. |
|---|
| 258 | 257 | */ |
|---|
| 259 | | -struct typec_mux *typec_mux_get(struct device *dev, |
|---|
| 260 | | - const struct typec_altmode_desc *desc) |
|---|
| 258 | +struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode, |
|---|
| 259 | + const struct typec_altmode_desc *desc) |
|---|
| 261 | 260 | { |
|---|
| 262 | 261 | struct typec_mux *mux; |
|---|
| 263 | 262 | |
|---|
| 264 | | - mux = device_connection_find_match(dev, "mode-switch", (void *)desc, |
|---|
| 263 | + mux = fwnode_connection_find_match(fwnode, "mode-switch", (void *)desc, |
|---|
| 265 | 264 | typec_mux_match); |
|---|
| 266 | 265 | if (!IS_ERR_OR_NULL(mux)) |
|---|
| 267 | 266 | WARN_ON(!try_module_get(mux->dev.parent->driver->owner)); |
|---|
| 268 | 267 | |
|---|
| 269 | 268 | return mux; |
|---|
| 270 | 269 | } |
|---|
| 271 | | -EXPORT_SYMBOL_GPL(typec_mux_get); |
|---|
| 270 | +EXPORT_SYMBOL_GPL(fwnode_typec_mux_get); |
|---|
| 272 | 271 | |
|---|
| 273 | 272 | /** |
|---|
| 274 | 273 | * typec_mux_put - Release handle to a Multiplexer |
|---|
| .. | .. |
|---|
| 284 | 283 | } |
|---|
| 285 | 284 | } |
|---|
| 286 | 285 | EXPORT_SYMBOL_GPL(typec_mux_put); |
|---|
| 286 | + |
|---|
| 287 | +int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state) |
|---|
| 288 | +{ |
|---|
| 289 | + if (IS_ERR_OR_NULL(mux)) |
|---|
| 290 | + return 0; |
|---|
| 291 | + |
|---|
| 292 | + return mux->set(mux, state); |
|---|
| 293 | +} |
|---|
| 294 | +EXPORT_SYMBOL_GPL(typec_mux_set); |
|---|
| 287 | 295 | |
|---|
| 288 | 296 | static void typec_mux_release(struct device *dev) |
|---|
| 289 | 297 | { |
|---|
| .. | .. |
|---|
| 326 | 334 | mux->dev.class = &typec_mux_class; |
|---|
| 327 | 335 | mux->dev.type = &typec_mux_dev_type; |
|---|
| 328 | 336 | mux->dev.driver_data = desc->drvdata; |
|---|
| 329 | | - dev_set_name(&mux->dev, "%s-mux", dev_name(parent)); |
|---|
| 337 | + ret = dev_set_name(&mux->dev, "%s-mux", desc->name ? desc->name : dev_name(parent)); |
|---|
| 338 | + if (ret) { |
|---|
| 339 | + put_device(&mux->dev); |
|---|
| 340 | + return ERR_PTR(ret); |
|---|
| 341 | + } |
|---|
| 330 | 342 | |
|---|
| 331 | 343 | ret = device_add(&mux->dev); |
|---|
| 332 | 344 | if (ret) { |
|---|