Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: typec: mux: Allow multiple mux_devs per mux

In the Qualcomm platforms the USB/DP PHY handles muxing and orientation
switching of the SuperSpeed lines, but the SBU lines needs to be
connected and switched by external (to the SoC) hardware.

It's therefor necessary to be able to have the TypeC controller operate
multiple TypeC muxes and switches. Use the newly introduced indirection
object to handle this, to avoid having to taint the TypeC controllers
with knowledge about the downstream hardware configuration.

The max number of devs per indirection is set to 3, which account for
being able to mux/switch the USB HS, SS and SBU lines, as per defined
defined in the usb-c-connector binding. This number could be grown if
need arrises at a later point in time.

Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Link: https://lore.kernel.org/r/20220422222351.1297276-6-bjorn.andersson@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Bjorn Andersson and committed by
Greg Kroah-Hartman
71793b57 713fd49b

+102 -26
+102 -26
drivers/usb/typec/mux.c
··· 17 17 #include "class.h" 18 18 #include "mux.h" 19 19 20 + #define TYPEC_MUX_MAX_DEVS 3 21 + 20 22 struct typec_switch { 21 - struct typec_switch_dev *sw_dev; 23 + struct typec_switch_dev *sw_devs[TYPEC_MUX_MAX_DEVS]; 24 + unsigned int num_sw_devs; 22 25 }; 23 26 24 27 static int switch_fwnode_match(struct device *dev, const void *fwnode) ··· 70 67 */ 71 68 struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode) 72 69 { 73 - struct typec_switch_dev *sw_dev; 70 + struct typec_switch_dev *sw_devs[TYPEC_MUX_MAX_DEVS]; 74 71 struct typec_switch *sw; 72 + int count; 73 + int err; 74 + int i; 75 75 76 76 sw = kzalloc(sizeof(*sw), GFP_KERNEL); 77 77 if (!sw) 78 78 return ERR_PTR(-ENOMEM); 79 79 80 - sw_dev = fwnode_connection_find_match(fwnode, "orientation-switch", NULL, 81 - typec_switch_match); 82 - if (IS_ERR_OR_NULL(sw_dev)) { 80 + count = fwnode_connection_find_matches(fwnode, "orientation-switch", NULL, 81 + typec_switch_match, 82 + (void **)sw_devs, 83 + ARRAY_SIZE(sw_devs)); 84 + if (count <= 0) { 83 85 kfree(sw); 84 - return ERR_CAST(sw_dev); 86 + return NULL; 85 87 } 86 88 87 - WARN_ON(!try_module_get(sw_dev->dev.parent->driver->owner)); 89 + for (i = 0; i < count; i++) { 90 + if (IS_ERR(sw_devs[i])) { 91 + err = PTR_ERR(sw_devs[i]); 92 + goto put_sw_devs; 93 + } 94 + } 88 95 89 - sw->sw_dev = sw_dev; 96 + for (i = 0; i < count; i++) { 97 + WARN_ON(!try_module_get(sw_devs[i]->dev.parent->driver->owner)); 98 + sw->sw_devs[i] = sw_devs[i]; 99 + } 100 + 101 + sw->num_sw_devs = count; 90 102 91 103 return sw; 104 + 105 + put_sw_devs: 106 + for (i = 0; i < count; i++) { 107 + if (!IS_ERR(sw_devs[i])) 108 + put_device(&sw_devs[i]->dev); 109 + } 110 + 111 + kfree(sw); 112 + 113 + return ERR_PTR(err); 92 114 } 93 115 EXPORT_SYMBOL_GPL(fwnode_typec_switch_get); 94 116 ··· 126 98 void typec_switch_put(struct typec_switch *sw) 127 99 { 128 100 struct typec_switch_dev *sw_dev; 101 + unsigned int i; 129 102 130 103 if (IS_ERR_OR_NULL(sw)) 131 104 return; 132 105 133 - sw_dev = sw->sw_dev; 106 + for (i = 0; i < sw->num_sw_devs; i++) { 107 + sw_dev = sw->sw_devs[i]; 134 108 135 - module_put(sw_dev->dev.parent->driver->owner); 136 - put_device(&sw_dev->dev); 109 + module_put(sw_dev->dev.parent->driver->owner); 110 + put_device(&sw_dev->dev); 111 + } 137 112 kfree(sw); 138 113 } 139 114 EXPORT_SYMBOL_GPL(typec_switch_put); ··· 204 173 enum typec_orientation orientation) 205 174 { 206 175 struct typec_switch_dev *sw_dev; 176 + unsigned int i; 177 + int ret; 207 178 208 179 if (IS_ERR_OR_NULL(sw)) 209 180 return 0; 210 181 211 - sw_dev = sw->sw_dev; 182 + for (i = 0; i < sw->num_sw_devs; i++) { 183 + sw_dev = sw->sw_devs[i]; 212 184 213 - return sw_dev->set(sw_dev, orientation); 185 + ret = sw_dev->set(sw_dev, orientation); 186 + if (ret) 187 + return ret; 188 + } 189 + 190 + return 0; 214 191 } 215 192 EXPORT_SYMBOL_GPL(typec_switch_set); 216 193 ··· 250 211 /* ------------------------------------------------------------------------- */ 251 212 252 213 struct typec_mux { 253 - struct typec_mux_dev *mux_dev; 214 + struct typec_mux_dev *mux_devs[TYPEC_MUX_MAX_DEVS]; 215 + unsigned int num_mux_devs; 254 216 }; 255 217 256 218 static int mux_fwnode_match(struct device *dev, const void *fwnode) ··· 334 294 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode, 335 295 const struct typec_altmode_desc *desc) 336 296 { 337 - struct typec_mux_dev *mux_dev; 297 + struct typec_mux_dev *mux_devs[TYPEC_MUX_MAX_DEVS]; 338 298 struct typec_mux *mux; 299 + int count; 300 + int err; 301 + int i; 339 302 340 303 mux = kzalloc(sizeof(*mux), GFP_KERNEL); 341 304 if (!mux) 342 305 return ERR_PTR(-ENOMEM); 343 306 344 - mux_dev = fwnode_connection_find_match(fwnode, "mode-switch", (void *)desc, 345 - typec_mux_match); 346 - if (IS_ERR_OR_NULL(mux_dev)) { 307 + count = fwnode_connection_find_matches(fwnode, "mode-switch", 308 + (void *)desc, typec_mux_match, 309 + (void **)mux_devs, 310 + ARRAY_SIZE(mux_devs)); 311 + if (count <= 0) { 347 312 kfree(mux); 348 - return ERR_CAST(mux_dev); 313 + return NULL; 349 314 } 350 315 351 - WARN_ON(!try_module_get(mux_dev->dev.parent->driver->owner)); 316 + for (i = 0; i < count; i++) { 317 + if (IS_ERR(mux_devs[i])) { 318 + err = PTR_ERR(mux_devs[i]); 319 + goto put_mux_devs; 320 + } 321 + } 352 322 353 - mux->mux_dev = mux_dev; 323 + for (i = 0; i < count; i++) { 324 + WARN_ON(!try_module_get(mux_devs[i]->dev.parent->driver->owner)); 325 + mux->mux_devs[i] = mux_devs[i]; 326 + } 327 + 328 + mux->num_mux_devs = count; 354 329 355 330 return mux; 331 + 332 + put_mux_devs: 333 + for (i = 0; i < count; i++) { 334 + if (!IS_ERR(mux_devs[i])) 335 + put_device(&mux_devs[i]->dev); 336 + } 337 + 338 + kfree(mux); 339 + 340 + return ERR_PTR(err); 356 341 } 357 342 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get); 358 343 ··· 390 325 void typec_mux_put(struct typec_mux *mux) 391 326 { 392 327 struct typec_mux_dev *mux_dev; 328 + unsigned int i; 393 329 394 330 if (IS_ERR_OR_NULL(mux)) 395 331 return; 396 332 397 - mux_dev = mux->mux_dev; 398 - module_put(mux_dev->dev.parent->driver->owner); 399 - put_device(&mux_dev->dev); 333 + for (i = 0; i < mux->num_mux_devs; i++) { 334 + mux_dev = mux->mux_devs[i]; 335 + module_put(mux_dev->dev.parent->driver->owner); 336 + put_device(&mux_dev->dev); 337 + } 400 338 kfree(mux); 401 339 } 402 340 EXPORT_SYMBOL_GPL(typec_mux_put); ··· 407 339 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state) 408 340 { 409 341 struct typec_mux_dev *mux_dev; 342 + unsigned int i; 343 + int ret; 410 344 411 345 if (IS_ERR_OR_NULL(mux)) 412 346 return 0; 413 347 414 - mux_dev = mux->mux_dev; 348 + for (i = 0; i < mux->num_mux_devs; i++) { 349 + mux_dev = mux->mux_devs[i]; 415 350 416 - return mux_dev->set(mux_dev, state); 351 + ret = mux_dev->set(mux_dev, state); 352 + if (ret) 353 + return ret; 354 + } 355 + 356 + return 0; 417 357 } 418 358 EXPORT_SYMBOL_GPL(typec_mux_set); 419 359