1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB Type-C Multiplexer/DeMultiplexer Switch support 4 * 5 * Copyright (C) 2018 Intel Corporation 6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 * Hans de Goede <hdegoede@redhat.com> 8 */ 9 10 #include <linux/device.h> 11 #include <linux/list.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/property.h> 15 #include <linux/slab.h> 16 #include <linux/usb/typec_mux.h> 17 18 #include "bus.h" 19 20 static bool dev_name_ends_with(struct device *dev, const char *suffix) 21 { 22 const char *name = dev_name(dev); 23 const int name_len = strlen(name); 24 const int suffix_len = strlen(suffix); 25 26 if (suffix_len > name_len) 27 return false; 28 29 return strcmp(name + (name_len - suffix_len), suffix) == 0; 30 } 31 32 static int switch_fwnode_match(struct device *dev, const void *fwnode) 33 { 34 return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-switch"); 35 } 36 37 static void *typec_switch_match(struct fwnode_handle *fwnode, const char *id, 38 void *data) 39 { 40 struct device *dev; 41 42 if (id && !fwnode_property_present(fwnode, id)) 43 return NULL; 44 45 dev = class_find_device(&typec_mux_class, NULL, fwnode, 46 switch_fwnode_match); 47 48 return dev ? to_typec_switch(dev) : ERR_PTR(-EPROBE_DEFER); 49 } 50 51 /** 52 * fwnode_typec_switch_get - Find USB Type-C orientation switch 53 * @fwnode: The caller device node 54 * 55 * Finds a switch linked with @dev. Returns a reference to the switch on 56 * success, NULL if no matching connection was found, or 57 * ERR_PTR(-EPROBE_DEFER) when a connection was found but the switch 58 * has not been enumerated yet. 59 */ 60 struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode) 61 { 62 struct typec_switch *sw; 63 64 sw = fwnode_connection_find_match(fwnode, "orientation-switch", NULL, 65 typec_switch_match); 66 if (!IS_ERR_OR_NULL(sw)) 67 WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); 68 69 return sw; 70 } 71 EXPORT_SYMBOL_GPL(fwnode_typec_switch_get); 72 73 /** 74 * typec_switch_put - Release USB Type-C orientation switch 75 * @sw: USB Type-C orientation switch 76 * 77 * Decrement reference count for @sw. 78 */ 79 void typec_switch_put(struct typec_switch *sw) 80 { 81 if (!IS_ERR_OR_NULL(sw)) { 82 module_put(sw->dev.parent->driver->owner); 83 put_device(&sw->dev); 84 } 85 } 86 EXPORT_SYMBOL_GPL(typec_switch_put); 87 88 static void typec_switch_release(struct device *dev) 89 { 90 kfree(to_typec_switch(dev)); 91 } 92 93 static const struct device_type typec_switch_dev_type = { 94 .name = "orientation_switch", 95 .release = typec_switch_release, 96 }; 97 98 /** 99 * typec_switch_register - Register USB Type-C orientation switch 100 * @parent: Parent device 101 * @desc: Orientation switch description 102 * 103 * This function registers a switch that can be used for routing the correct 104 * data pairs depending on the cable plug orientation from the USB Type-C 105 * connector to the USB controllers. USB Type-C plugs can be inserted 106 * right-side-up or upside-down. 107 */ 108 struct typec_switch * 109 typec_switch_register(struct device *parent, 110 const struct typec_switch_desc *desc) 111 { 112 struct typec_switch *sw; 113 int ret; 114 115 if (!desc || !desc->set) 116 return ERR_PTR(-EINVAL); 117 118 sw = kzalloc(sizeof(*sw), GFP_KERNEL); 119 if (!sw) 120 return ERR_PTR(-ENOMEM); 121 122 sw->set = desc->set; 123 124 device_initialize(&sw->dev); 125 sw->dev.parent = parent; 126 sw->dev.fwnode = desc->fwnode; 127 sw->dev.class = &typec_mux_class; 128 sw->dev.type = &typec_switch_dev_type; 129 sw->dev.driver_data = desc->drvdata; 130 dev_set_name(&sw->dev, "%s-switch", 131 desc->name ? desc->name : dev_name(parent)); 132 133 ret = device_add(&sw->dev); 134 if (ret) { 135 dev_err(parent, "failed to register switch (%d)\n", ret); 136 put_device(&sw->dev); 137 return ERR_PTR(ret); 138 } 139 140 return sw; 141 } 142 EXPORT_SYMBOL_GPL(typec_switch_register); 143 144 int typec_switch_set(struct typec_switch *sw, 145 enum typec_orientation orientation) 146 { 147 if (IS_ERR_OR_NULL(sw)) 148 return 0; 149 150 return sw->set(sw, orientation); 151 } 152 EXPORT_SYMBOL_GPL(typec_switch_set); 153 154 /** 155 * typec_switch_unregister - Unregister USB Type-C orientation switch 156 * @sw: USB Type-C orientation switch 157 * 158 * Unregister switch that was registered with typec_switch_register(). 159 */ 160 void typec_switch_unregister(struct typec_switch *sw) 161 { 162 if (!IS_ERR_OR_NULL(sw)) 163 device_unregister(&sw->dev); 164 } 165 EXPORT_SYMBOL_GPL(typec_switch_unregister); 166 167 void typec_switch_set_drvdata(struct typec_switch *sw, void *data) 168 { 169 dev_set_drvdata(&sw->dev, data); 170 } 171 EXPORT_SYMBOL_GPL(typec_switch_set_drvdata); 172 173 void *typec_switch_get_drvdata(struct typec_switch *sw) 174 { 175 return dev_get_drvdata(&sw->dev); 176 } 177 EXPORT_SYMBOL_GPL(typec_switch_get_drvdata); 178 179 /* ------------------------------------------------------------------------- */ 180 181 static int mux_fwnode_match(struct device *dev, const void *fwnode) 182 { 183 return dev_fwnode(dev) == fwnode && dev_name_ends_with(dev, "-mux"); 184 } 185 186 static void *typec_mux_match(struct fwnode_handle *fwnode, const char *id, 187 void *data) 188 { 189 const struct typec_altmode_desc *desc = data; 190 struct device *dev; 191 bool match; 192 int nval; 193 u16 *val; 194 int ret; 195 int i; 196 197 /* 198 * Check has the identifier already been "consumed". If it 199 * has, no need to do any extra connection identification. 200 */ 201 match = !id; 202 if (match) 203 goto find_mux; 204 205 /* Accessory Mode muxes */ 206 if (!desc) { 207 match = fwnode_property_present(fwnode, "accessory"); 208 if (match) 209 goto find_mux; 210 return NULL; 211 } 212 213 /* Alternate Mode muxes */ 214 nval = fwnode_property_count_u16(fwnode, "svid"); 215 if (nval <= 0) 216 return NULL; 217 218 val = kcalloc(nval, sizeof(*val), GFP_KERNEL); 219 if (!val) 220 return ERR_PTR(-ENOMEM); 221 222 ret = fwnode_property_read_u16_array(fwnode, "svid", val, nval); 223 if (ret < 0) { 224 kfree(val); 225 return ERR_PTR(ret); 226 } 227 228 for (i = 0; i < nval; i++) { 229 match = val[i] == desc->svid; 230 if (match) { 231 kfree(val); 232 goto find_mux; 233 } 234 } 235 kfree(val); 236 return NULL; 237 238 find_mux: 239 dev = class_find_device(&typec_mux_class, NULL, fwnode, 240 mux_fwnode_match); 241 242 return dev ? to_typec_mux(dev) : ERR_PTR(-EPROBE_DEFER); 243 } 244 245 /** 246 * fwnode_typec_mux_get - Find USB Type-C Multiplexer 247 * @fwnode: The caller device node 248 * @desc: Alt Mode description 249 * 250 * Finds a mux linked to the caller. This function is primarily meant for the 251 * Type-C drivers. Returns a reference to the mux on success, NULL if no 252 * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection 253 * was found but the mux has not been enumerated yet. 254 */ 255 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode, 256 const struct typec_altmode_desc *desc) 257 { 258 struct typec_mux *mux; 259 260 mux = fwnode_connection_find_match(fwnode, "mode-switch", (void *)desc, 261 typec_mux_match); 262 if (!IS_ERR_OR_NULL(mux)) 263 WARN_ON(!try_module_get(mux->dev.parent->driver->owner)); 264 265 return mux; 266 } 267 EXPORT_SYMBOL_GPL(fwnode_typec_mux_get); 268 269 /** 270 * typec_mux_put - Release handle to a Multiplexer 271 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer 272 * 273 * Decrements reference count for @mux. 274 */ 275 void typec_mux_put(struct typec_mux *mux) 276 { 277 if (!IS_ERR_OR_NULL(mux)) { 278 module_put(mux->dev.parent->driver->owner); 279 put_device(&mux->dev); 280 } 281 } 282 EXPORT_SYMBOL_GPL(typec_mux_put); 283 284 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state) 285 { 286 if (IS_ERR_OR_NULL(mux)) 287 return 0; 288 289 return mux->set(mux, state); 290 } 291 EXPORT_SYMBOL_GPL(typec_mux_set); 292 293 static void typec_mux_release(struct device *dev) 294 { 295 kfree(to_typec_mux(dev)); 296 } 297 298 static const struct device_type typec_mux_dev_type = { 299 .name = "mode_switch", 300 .release = typec_mux_release, 301 }; 302 303 /** 304 * typec_mux_register - Register Multiplexer routing USB Type-C pins 305 * @parent: Parent device 306 * @desc: Multiplexer description 307 * 308 * USB Type-C connectors can be used for alternate modes of operation besides 309 * USB when Accessory/Alternate Modes are supported. With some of those modes, 310 * the pins on the connector need to be reconfigured. This function registers 311 * multiplexer switches routing the pins on the connector. 312 */ 313 struct typec_mux * 314 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc) 315 { 316 struct typec_mux *mux; 317 int ret; 318 319 if (!desc || !desc->set) 320 return ERR_PTR(-EINVAL); 321 322 mux = kzalloc(sizeof(*mux), GFP_KERNEL); 323 if (!mux) 324 return ERR_PTR(-ENOMEM); 325 326 mux->set = desc->set; 327 328 device_initialize(&mux->dev); 329 mux->dev.parent = parent; 330 mux->dev.fwnode = desc->fwnode; 331 mux->dev.class = &typec_mux_class; 332 mux->dev.type = &typec_mux_dev_type; 333 mux->dev.driver_data = desc->drvdata; 334 dev_set_name(&mux->dev, "%s-mux", 335 desc->name ? desc->name : dev_name(parent)); 336 337 ret = device_add(&mux->dev); 338 if (ret) { 339 dev_err(parent, "failed to register mux (%d)\n", ret); 340 put_device(&mux->dev); 341 return ERR_PTR(ret); 342 } 343 344 return mux; 345 } 346 EXPORT_SYMBOL_GPL(typec_mux_register); 347 348 /** 349 * typec_mux_unregister - Unregister Multiplexer Switch 350 * @mux: USB Type-C Connector Multiplexer/DeMultiplexer 351 * 352 * Unregister mux that was registered with typec_mux_register(). 353 */ 354 void typec_mux_unregister(struct typec_mux *mux) 355 { 356 if (!IS_ERR_OR_NULL(mux)) 357 device_unregister(&mux->dev); 358 } 359 EXPORT_SYMBOL_GPL(typec_mux_unregister); 360 361 void typec_mux_set_drvdata(struct typec_mux *mux, void *data) 362 { 363 dev_set_drvdata(&mux->dev, data); 364 } 365 EXPORT_SYMBOL_GPL(typec_mux_set_drvdata); 366 367 void *typec_mux_get_drvdata(struct typec_mux *mux) 368 { 369 return dev_get_drvdata(&mux->dev); 370 } 371 EXPORT_SYMBOL_GPL(typec_mux_get_drvdata); 372 373 struct class typec_mux_class = { 374 .name = "typec_mux", 375 .owner = THIS_MODULE, 376 }; 377