Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | // SPDX-License-Identifier: GPL-2.0 /* * Copyright 2022 Google LLC * * USB Type-C Retimer support. * Author: Prashant Malani <pmalani@chromium.org> * */ #include <linux/device.h> #include <linux/list.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/property.h> #include <linux/slab.h> #include "class.h" #include "retimer.h" static int retimer_fwnode_match(struct device *dev, const void *fwnode) { return is_typec_retimer(dev) && device_match_fwnode(dev, fwnode); } static void *typec_retimer_match(const struct fwnode_handle *fwnode, const char *id, void *data) { struct device *dev; if (id && !fwnode_property_present(fwnode, id)) return NULL; dev = class_find_device(&retimer_class, NULL, fwnode, retimer_fwnode_match); return dev ? to_typec_retimer(dev) : ERR_PTR(-EPROBE_DEFER); } /** * fwnode_typec_retimer_get - Find USB Type-C retimer. * @fwnode: The caller device node. * * Finds a retimer linked to the caller. This function is primarily meant for the * Type-C drivers. Returns a reference to the retimer on success, NULL if no * matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a connection * was found but the retimer has not been enumerated yet. */ struct typec_retimer *fwnode_typec_retimer_get(struct fwnode_handle *fwnode) { struct typec_retimer *retimer; retimer = fwnode_connection_find_match(fwnode, "retimer-switch", NULL, typec_retimer_match); if (!IS_ERR_OR_NULL(retimer)) WARN_ON(!try_module_get(retimer->dev.parent->driver->owner)); return retimer; } EXPORT_SYMBOL_GPL(fwnode_typec_retimer_get); /** * typec_retimer_put - Release handle to a retimer. * @retimer: USB Type-C Connector Retimer. * * Decrements reference count for @retimer. */ void typec_retimer_put(struct typec_retimer *retimer) { if (!IS_ERR_OR_NULL(retimer)) { module_put(retimer->dev.parent->driver->owner); put_device(&retimer->dev); } } EXPORT_SYMBOL_GPL(typec_retimer_put); int typec_retimer_set(struct typec_retimer *retimer, struct typec_retimer_state *state) { if (IS_ERR_OR_NULL(retimer)) return 0; return retimer->set(retimer, state); } EXPORT_SYMBOL_GPL(typec_retimer_set); static void typec_retimer_release(struct device *dev) { kfree(to_typec_retimer(dev)); } const struct device_type typec_retimer_dev_type = { .name = "typec_retimer", .release = typec_retimer_release, }; /** * typec_retimer_register - Register a retimer device. * @parent: Parent device. * @desc: Retimer description. * * Some USB Type-C connectors have their physical lines routed through retimers before they * reach muxes or host controllers. In some cases (for example: using alternate modes) * these retimers need to be reconfigured appropriately. This function registers retimer * switches which route and potentially modify the signals on the Type C physical lines * enroute to the host controllers. */ struct typec_retimer * typec_retimer_register(struct device *parent, const struct typec_retimer_desc *desc) { struct typec_retimer *retimer; int ret; if (!desc || !desc->set) return ERR_PTR(-EINVAL); retimer = kzalloc(sizeof(*retimer), GFP_KERNEL); if (!retimer) return ERR_PTR(-ENOMEM); retimer->set = desc->set; device_initialize(&retimer->dev); retimer->dev.parent = parent; retimer->dev.fwnode = desc->fwnode; retimer->dev.class = &retimer_class; retimer->dev.type = &typec_retimer_dev_type; retimer->dev.driver_data = desc->drvdata; dev_set_name(&retimer->dev, "%s-retimer", desc->name ? desc->name : dev_name(parent)); ret = device_add(&retimer->dev); if (ret) { dev_err(parent, "failed to register retimer (%d)\n", ret); put_device(&retimer->dev); return ERR_PTR(ret); } return retimer; } EXPORT_SYMBOL_GPL(typec_retimer_register); /** * typec_retimer_unregister - Unregister retimer device. * @retimer: USB Type-C Connector retimer. * * Unregister retimer that was registered with typec_retimer_register(). */ void typec_retimer_unregister(struct typec_retimer *retimer) { if (!IS_ERR_OR_NULL(retimer)) device_unregister(&retimer->dev); } EXPORT_SYMBOL_GPL(typec_retimer_unregister); void *typec_retimer_get_drvdata(struct typec_retimer *retimer) { return dev_get_drvdata(&retimer->dev); } EXPORT_SYMBOL_GPL(typec_retimer_get_drvdata); struct class retimer_class = { .name = "retimer", }; |