Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Lee Jones | 246d7a1 | 2020-07-02 15:46:02 +0100 | [diff] [blame] | 2 | /* |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 3 | * ulpi.c - USB ULPI PHY bus |
| 4 | * |
| 5 | * Copyright (C) 2015 Intel Corporation |
| 6 | * |
| 7 | * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/ulpi/interface.h> |
| 11 | #include <linux/ulpi/driver.h> |
| 12 | #include <linux/ulpi/regs.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/acpi.h> |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/clk/clk-conf.h> |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 19 | |
| 20 | /* -------------------------------------------------------------------------- */ |
| 21 | |
| 22 | int ulpi_read(struct ulpi *ulpi, u8 addr) |
| 23 | { |
Tal Shorer | e6f7484 | 2016-08-16 19:04:50 +0300 | [diff] [blame] | 24 | return ulpi->ops->read(ulpi->dev.parent, addr); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 25 | } |
| 26 | EXPORT_SYMBOL_GPL(ulpi_read); |
| 27 | |
| 28 | int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val) |
| 29 | { |
Tal Shorer | e6f7484 | 2016-08-16 19:04:50 +0300 | [diff] [blame] | 30 | return ulpi->ops->write(ulpi->dev.parent, addr, val); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 31 | } |
| 32 | EXPORT_SYMBOL_GPL(ulpi_write); |
| 33 | |
| 34 | /* -------------------------------------------------------------------------- */ |
| 35 | |
| 36 | static int ulpi_match(struct device *dev, struct device_driver *driver) |
| 37 | { |
| 38 | struct ulpi_driver *drv = to_ulpi_driver(driver); |
| 39 | struct ulpi *ulpi = to_ulpi_dev(dev); |
| 40 | const struct ulpi_device_id *id; |
| 41 | |
Jon Hunter | 2e3dd4a | 2022-01-17 15:00:39 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Some ULPI devices don't have a vendor id |
| 44 | * or provide an id_table so rely on OF match. |
| 45 | */ |
| 46 | if (ulpi->id.vendor == 0 || !drv->id_table) |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 47 | return of_driver_match_device(dev, driver); |
| 48 | |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 49 | for (id = drv->id_table; id->vendor; id++) |
| 50 | if (id->vendor == ulpi->id.vendor && |
| 51 | id->product == ulpi->id.product) |
| 52 | return 1; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 58 | { |
| 59 | struct ulpi *ulpi = to_ulpi_dev(dev); |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 60 | int ret; |
| 61 | |
| 62 | ret = of_device_uevent_modalias(dev, env); |
| 63 | if (ret != -ENODEV) |
| 64 | return ret; |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 65 | |
| 66 | if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x", |
| 67 | ulpi->id.vendor, ulpi->id.product)) |
| 68 | return -ENOMEM; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int ulpi_probe(struct device *dev) |
| 73 | { |
| 74 | struct ulpi_driver *drv = to_ulpi_driver(dev->driver); |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 75 | int ret; |
| 76 | |
| 77 | ret = of_clk_set_defaults(dev->of_node, false); |
| 78 | if (ret < 0) |
| 79 | return ret; |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 80 | |
| 81 | return drv->probe(to_ulpi_dev(dev)); |
| 82 | } |
| 83 | |
Uwe Kleine-König | fc7a620 | 2021-07-13 21:35:22 +0200 | [diff] [blame] | 84 | static void ulpi_remove(struct device *dev) |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 85 | { |
| 86 | struct ulpi_driver *drv = to_ulpi_driver(dev->driver); |
| 87 | |
| 88 | if (drv->remove) |
| 89 | drv->remove(to_ulpi_dev(dev)); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static struct bus_type ulpi_bus = { |
| 93 | .name = "ulpi", |
| 94 | .match = ulpi_match, |
| 95 | .uevent = ulpi_uevent, |
| 96 | .probe = ulpi_probe, |
| 97 | .remove = ulpi_remove, |
| 98 | }; |
| 99 | |
| 100 | /* -------------------------------------------------------------------------- */ |
| 101 | |
| 102 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 103 | char *buf) |
| 104 | { |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 105 | int len; |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 106 | struct ulpi *ulpi = to_ulpi_dev(dev); |
| 107 | |
Rob Herring | 0634c29 | 2017-03-22 09:16:27 -0500 | [diff] [blame] | 108 | len = of_device_modalias(dev, buf, PAGE_SIZE); |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 109 | if (len != -ENODEV) |
| 110 | return len; |
| 111 | |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 112 | return sprintf(buf, "ulpi:v%04xp%04x\n", |
| 113 | ulpi->id.vendor, ulpi->id.product); |
| 114 | } |
| 115 | static DEVICE_ATTR_RO(modalias); |
| 116 | |
| 117 | static struct attribute *ulpi_dev_attrs[] = { |
| 118 | &dev_attr_modalias.attr, |
| 119 | NULL |
| 120 | }; |
| 121 | |
Rikard Falkeborn | 52170e9 | 2020-11-25 17:25:00 +0100 | [diff] [blame] | 122 | static const struct attribute_group ulpi_dev_attr_group = { |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 123 | .attrs = ulpi_dev_attrs, |
| 124 | }; |
| 125 | |
| 126 | static const struct attribute_group *ulpi_dev_attr_groups[] = { |
| 127 | &ulpi_dev_attr_group, |
| 128 | NULL |
| 129 | }; |
| 130 | |
| 131 | static void ulpi_dev_release(struct device *dev) |
| 132 | { |
Sean Anderson | 092f45b | 2022-01-27 14:00:02 -0500 | [diff] [blame] | 133 | of_node_put(dev->of_node); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 134 | kfree(to_ulpi_dev(dev)); |
| 135 | } |
| 136 | |
Bhumika Goyal | 53ec7f2 | 2017-08-19 13:52:26 +0530 | [diff] [blame] | 137 | static const struct device_type ulpi_dev_type = { |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 138 | .name = "ulpi_device", |
| 139 | .groups = ulpi_dev_attr_groups, |
| 140 | .release = ulpi_dev_release, |
| 141 | }; |
| 142 | |
| 143 | /* -------------------------------------------------------------------------- */ |
| 144 | |
| 145 | /** |
Lee Jones | 826e9c4 | 2021-05-26 14:00:16 +0100 | [diff] [blame] | 146 | * __ulpi_register_driver - register a driver with the ULPI bus |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 147 | * @drv: driver being registered |
Lee Jones | 246d7a1 | 2020-07-02 15:46:02 +0100 | [diff] [blame] | 148 | * @module: ends up being THIS_MODULE |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 149 | * |
| 150 | * Registers a driver with the ULPI bus. |
| 151 | */ |
Stephen Boyd | 1ebe88d | 2016-06-25 22:38:21 -0700 | [diff] [blame] | 152 | int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module) |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 153 | { |
| 154 | if (!drv->probe) |
| 155 | return -EINVAL; |
| 156 | |
Stephen Boyd | 1ebe88d | 2016-06-25 22:38:21 -0700 | [diff] [blame] | 157 | drv->driver.owner = module; |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 158 | drv->driver.bus = &ulpi_bus; |
| 159 | |
| 160 | return driver_register(&drv->driver); |
| 161 | } |
Stephen Boyd | 1ebe88d | 2016-06-25 22:38:21 -0700 | [diff] [blame] | 162 | EXPORT_SYMBOL_GPL(__ulpi_register_driver); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * ulpi_unregister_driver - unregister a driver with the ULPI bus |
| 166 | * @drv: driver to unregister |
| 167 | * |
| 168 | * Unregisters a driver with the ULPI bus. |
| 169 | */ |
| 170 | void ulpi_unregister_driver(struct ulpi_driver *drv) |
| 171 | { |
| 172 | driver_unregister(&drv->driver); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(ulpi_unregister_driver); |
| 175 | |
| 176 | /* -------------------------------------------------------------------------- */ |
| 177 | |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 178 | static int ulpi_of_register(struct ulpi *ulpi) |
| 179 | { |
| 180 | struct device_node *np = NULL, *child; |
| 181 | struct device *parent; |
| 182 | |
| 183 | /* Find a ulpi bus underneath the parent or the grandparent */ |
| 184 | parent = ulpi->dev.parent; |
| 185 | if (parent->of_node) |
Johan Hovold | 33c309e | 2017-11-11 16:31:18 +0100 | [diff] [blame] | 186 | np = of_get_child_by_name(parent->of_node, "ulpi"); |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 187 | else if (parent->parent && parent->parent->of_node) |
Johan Hovold | 33c309e | 2017-11-11 16:31:18 +0100 | [diff] [blame] | 188 | np = of_get_child_by_name(parent->parent->of_node, "ulpi"); |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 189 | if (!np) |
| 190 | return 0; |
| 191 | |
| 192 | child = of_get_next_available_child(np, NULL); |
| 193 | of_node_put(np); |
| 194 | if (!child) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | ulpi->dev.of_node = child; |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int ulpi_read_id(struct ulpi *ulpi) |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 203 | { |
| 204 | int ret; |
| 205 | |
| 206 | /* Test the interface */ |
| 207 | ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa); |
| 208 | if (ret < 0) |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 209 | goto err; |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 210 | |
| 211 | ret = ulpi_read(ulpi, ULPI_SCRATCH); |
| 212 | if (ret < 0) |
| 213 | return ret; |
| 214 | |
| 215 | if (ret != 0xaa) |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 216 | goto err; |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 217 | |
| 218 | ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW); |
| 219 | ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8; |
| 220 | |
| 221 | ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW); |
| 222 | ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8; |
| 223 | |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 224 | /* Some ULPI devices don't have a vendor id so rely on OF match */ |
| 225 | if (ulpi->id.vendor == 0) |
| 226 | goto err; |
| 227 | |
| 228 | request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product); |
| 229 | return 0; |
| 230 | err: |
| 231 | of_device_request_module(&ulpi->dev); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int ulpi_register(struct device *dev, struct ulpi *ulpi) |
| 236 | { |
| 237 | int ret; |
| 238 | |
| 239 | ulpi->dev.parent = dev; /* needed early for ops */ |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 240 | ulpi->dev.bus = &ulpi_bus; |
| 241 | ulpi->dev.type = &ulpi_dev_type; |
| 242 | dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev)); |
| 243 | |
| 244 | ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev)); |
| 245 | |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 246 | ret = ulpi_of_register(ulpi); |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | |
| 250 | ret = ulpi_read_id(ulpi); |
Sean Anderson | 0a907ee | 2022-01-27 14:00:03 -0500 | [diff] [blame] | 251 | if (ret) { |
| 252 | of_node_put(ulpi->dev.of_node); |
Stephen Boyd | ef6a7bc | 2016-12-28 14:56:49 -0800 | [diff] [blame] | 253 | return ret; |
Sean Anderson | 0a907ee | 2022-01-27 14:00:03 -0500 | [diff] [blame] | 254 | } |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 255 | |
| 256 | ret = device_register(&ulpi->dev); |
Sean Anderson | 0a907ee | 2022-01-27 14:00:03 -0500 | [diff] [blame] | 257 | if (ret) { |
| 258 | put_device(&ulpi->dev); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 259 | return ret; |
Sean Anderson | 0a907ee | 2022-01-27 14:00:03 -0500 | [diff] [blame] | 260 | } |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 261 | |
| 262 | dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n", |
| 263 | ulpi->id.vendor, ulpi->id.product); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * ulpi_register_interface - instantiate new ULPI device |
| 270 | * @dev: USB controller's device interface |
| 271 | * @ops: ULPI register access |
| 272 | * |
| 273 | * Allocates and registers a ULPI device and an interface for it. Called from |
| 274 | * the USB controller that provides the ULPI interface. |
| 275 | */ |
Tal Shorer | b9454f9 | 2016-08-16 19:04:52 +0300 | [diff] [blame] | 276 | struct ulpi *ulpi_register_interface(struct device *dev, |
| 277 | const struct ulpi_ops *ops) |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 278 | { |
| 279 | struct ulpi *ulpi; |
| 280 | int ret; |
| 281 | |
| 282 | ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL); |
| 283 | if (!ulpi) |
| 284 | return ERR_PTR(-ENOMEM); |
| 285 | |
| 286 | ulpi->ops = ops; |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 287 | |
| 288 | ret = ulpi_register(dev, ulpi); |
| 289 | if (ret) { |
| 290 | kfree(ulpi); |
| 291 | return ERR_PTR(ret); |
| 292 | } |
| 293 | |
| 294 | return ulpi; |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(ulpi_register_interface); |
| 297 | |
| 298 | /** |
| 299 | * ulpi_unregister_interface - unregister ULPI interface |
Lee Jones | 246d7a1 | 2020-07-02 15:46:02 +0100 | [diff] [blame] | 300 | * @ulpi: struct ulpi_interface |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 301 | * |
| 302 | * Unregisters a ULPI device and it's interface that was created with |
| 303 | * ulpi_create_interface(). |
| 304 | */ |
| 305 | void ulpi_unregister_interface(struct ulpi *ulpi) |
| 306 | { |
| 307 | device_unregister(&ulpi->dev); |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(ulpi_unregister_interface); |
| 310 | |
| 311 | /* -------------------------------------------------------------------------- */ |
| 312 | |
| 313 | static int __init ulpi_init(void) |
| 314 | { |
| 315 | return bus_register(&ulpi_bus); |
| 316 | } |
Lu Baolu | 4696b88 | 2015-06-04 20:44:14 +0800 | [diff] [blame] | 317 | subsys_initcall(ulpi_init); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 318 | |
| 319 | static void __exit ulpi_exit(void) |
| 320 | { |
| 321 | bus_unregister(&ulpi_bus); |
| 322 | } |
| 323 | module_exit(ulpi_exit); |
| 324 | |
| 325 | MODULE_AUTHOR("Intel Corporation"); |
| 326 | MODULE_LICENSE("GPL v2"); |
| 327 | MODULE_DESCRIPTION("USB ULPI PHY bus"); |