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