| 1 | |
| 2 | |
| 3 | * ulpi.c - USB ULPI PHY bus
|
| 4 | *
|
| 5 | * Copyright (C) 2015 Intel Corporation
|
| 6 | *
|
| 7 | * Author: Heikki Krogerus <[email protected]>
|
| 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>
|
| 16 | #include <linux/debugfs.h>
|
| 17 | #include <linux/of.h>
|
| 18 | #include <linux/of_device.h>
|
| 19 | #include <linux/clk/clk-conf.h>
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | int ulpi_read(struct ulpi *ulpi, u8 addr)
|
| 24 | {
|
| 25 | return ulpi->ops->read(ulpi->dev.parent, addr);
|
| 26 | }
|
| 27 | EXPORT_SYMBOL_GPL(ulpi_read);
|
| 28 |
|
| 29 | int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
|
| 30 | {
|
| 31 | return ulpi->ops->write(ulpi->dev.parent, addr, val);
|
| 32 | }
|
| 33 | EXPORT_SYMBOL_GPL(ulpi_write);
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 | static int ulpi_match(struct device *dev, const struct device_driver *driver)
|
| 38 | {
|
| 39 | struct ulpi_driver *drv = to_ulpi_driver(driver);
|
| 40 | struct ulpi *ulpi = to_ulpi_dev(dev);
|
| 41 | const struct ulpi_device_id *id;
|
| 42 |
|
| 43 | |
| 44 | * Some ULPI devices don't have a vendor id
|
| 45 | * or provide an id_table so rely on OF match.
|
| 46 |
|
| 47 | if (ulpi->id.vendor == 0 || !drv->id_table)
|
| 48 | return of_driver_match_device(dev, driver);
|
| 49 |
|
| 50 | for (id = drv->id_table; id->vendor; id++)
|
| 51 | if (id->vendor == ulpi->id.vendor &&
|
| 52 | id->product == ulpi->id.product)
|
| 53 | return 1;
|
| 54 |
|
| 55 | return 0;
|
| 56 | }
|
| 57 |
|
| 58 | static int ulpi_uevent(const struct device *dev, struct kobj_uevent_env *env)
|
| 59 | {
|
| 60 | const struct ulpi *ulpi = to_ulpi_dev(dev);
|
| 61 | int ret;
|
| 62 |
|
| 63 | ret = of_device_uevent_modalias(dev, env);
|
| 64 | if (ret != -ENODEV)
|
| 65 | return ret;
|
| 66 |
|
| 67 | if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
|
| 68 | ulpi->id.vendor, ulpi->id.product))
|
| 69 | return -ENOMEM;
|
| 70 | return 0;
|
| 71 | }
|
| 72 |
|
| 73 | static int ulpi_probe(struct device *dev)
|
| 74 | {
|
| 75 | struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
|
| 76 | int ret;
|
| 77 |
|
| 78 | ret = of_clk_set_defaults(dev->of_node, false);
|
| 79 | if (ret < 0)
|
| 80 | return ret;
|
| 81 |
|
| 82 | return drv->probe(to_ulpi_dev(dev));
|
| 83 | }
|
| 84 |
|
| 85 | static void ulpi_remove(struct device *dev)
|
| 86 | {
|
| 87 | struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
|
| 88 |
|
| 89 | if (drv->remove)
|
| 90 | drv->remove(to_ulpi_dev(dev));
|
| 91 | }
|
| 92 |
|
| 93 | static const struct bus_type ulpi_bus = {
|
| 94 | .name = "ulpi",
|
| 95 | .match = ulpi_match,
|
| 96 | .uevent = ulpi_uevent,
|
| 97 | .probe = ulpi_probe,
|
| 98 | .remove = ulpi_remove,
|
| 99 | };
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
|
| 104 | char *buf)
|
| 105 | {
|
| 106 | int len;
|
| 107 | struct ulpi *ulpi = to_ulpi_dev(dev);
|
| 108 |
|
| 109 | len = of_device_modalias(dev, buf, PAGE_SIZE);
|
| 110 | if (len != -ENODEV)
|
| 111 | return len;
|
| 112 |
|
| 113 | return sprintf(buf, "ulpi:v%04xp%04x\n",
|
| 114 | ulpi->id.vendor, ulpi->id.product);
|
| 115 | }
|
| 116 | static DEVICE_ATTR_RO(modalias);
|
| 117 |
|
| 118 | static struct attribute *ulpi_dev_attrs[] = {
|
| 119 | &dev_attr_modalias.attr,
|
| 120 | NULL
|
| 121 | };
|
| 122 |
|
| 123 | static const struct attribute_group ulpi_dev_attr_group = {
|
| 124 | .attrs = ulpi_dev_attrs,
|
| 125 | };
|
| 126 |
|
| 127 | static const struct attribute_group *ulpi_dev_attr_groups[] = {
|
| 128 | &ulpi_dev_attr_group,
|
| 129 | NULL
|
| 130 | };
|
| 131 |
|
| 132 | static void ulpi_dev_release(struct device *dev)
|
| 133 | {
|
| 134 | of_node_put(dev->of_node);
|
| 135 | kfree(to_ulpi_dev(dev));
|
| 136 | }
|
| 137 |
|
| 138 | static const struct device_type ulpi_dev_type = {
|
| 139 | .name = "ulpi_device",
|
| 140 | .groups = ulpi_dev_attr_groups,
|
| 141 | .release = ulpi_dev_release,
|
| 142 | };
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 | |
| 147 | * __ulpi_register_driver - register a driver with the ULPI bus
|
| 148 | * @drv: driver being registered
|
| 149 | * @module: ends up being THIS_MODULE
|
| 150 | *
|
| 151 | * Registers a driver with the ULPI bus.
|
| 152 |
|
| 153 | int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
|
| 154 | {
|
| 155 | if (!drv->probe)
|
| 156 | return -EINVAL;
|
| 157 |
|
| 158 | drv->driver.owner = module;
|
| 159 | drv->driver.bus = &ulpi_bus;
|
| 160 |
|
| 161 | return driver_register(&drv->driver);
|
| 162 | }
|
| 163 | EXPORT_SYMBOL_GPL(__ulpi_register_driver);
|
| 164 |
|
| 165 | |
| 166 | * ulpi_unregister_driver - unregister a driver with the ULPI bus
|
| 167 | * @drv: driver to unregister
|
| 168 | *
|
| 169 | * Unregisters a driver with the ULPI bus.
|
| 170 |
|
| 171 | void ulpi_unregister_driver(struct ulpi_driver *drv)
|
| 172 | {
|
| 173 | driver_unregister(&drv->driver);
|
| 174 | }
|
| 175 | EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 | static int ulpi_of_register(struct ulpi *ulpi)
|
| 180 | {
|
| 181 | struct device_node *np = NULL, *child;
|
| 182 | struct device *parent;
|
| 183 |
|
| 184 |
|
| 185 | parent = ulpi->dev.parent;
|
| 186 | if (parent->of_node)
|
| 187 | np = of_get_child_by_name(parent->of_node, "ulpi");
|
| 188 | else if (parent->parent && parent->parent->of_node)
|
| 189 | np = of_get_child_by_name(parent->parent->of_node, "ulpi");
|
| 190 | if (!np)
|
| 191 | return 0;
|
| 192 |
|
| 193 | child = of_get_next_available_child(np, NULL);
|
| 194 | of_node_put(np);
|
| 195 | if (!child)
|
| 196 | return -EINVAL;
|
| 197 |
|
| 198 | ulpi->dev.of_node = child;
|
| 199 |
|
| 200 | return 0;
|
| 201 | }
|
| 202 |
|
| 203 | static int ulpi_read_id(struct ulpi *ulpi)
|
| 204 | {
|
| 205 | int ret;
|
| 206 |
|
| 207 |
|
| 208 | ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
|
| 209 | if (ret < 0)
|
| 210 | goto err;
|
| 211 |
|
| 212 | ret = ulpi_read(ulpi, ULPI_SCRATCH);
|
| 213 | if (ret < 0)
|
| 214 | return ret;
|
| 215 |
|
| 216 | if (ret != 0xaa)
|
| 217 | goto err;
|
| 218 |
|
| 219 | ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
|
| 220 | ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
|
| 221 |
|
| 222 | ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
|
| 223 | ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
|
| 224 |
|
| 225 |
|
| 226 | if (ulpi->id.vendor == 0)
|
| 227 | goto err;
|
| 228 |
|
| 229 | request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
|
| 230 | return 0;
|
| 231 | err:
|
| 232 | of_request_module(ulpi->dev.of_node);
|
| 233 | return 0;
|
| 234 | }
|
| 235 |
|
| 236 | static int ulpi_regs_show(struct seq_file *seq, void *data)
|
| 237 | {
|
| 238 | struct ulpi *ulpi = seq->private;
|
| 239 |
|
| 240 | #define ulpi_print(name, reg) do { \
|
| 241 | int ret = ulpi_read(ulpi, reg); \
|
| 242 | if (ret < 0) \
|
| 243 | return ret; \
|
| 244 | seq_printf(seq, name " %.02x\n", ret); \
|
| 245 | } while (0)
|
| 246 |
|
| 247 | ulpi_print("Vendor ID Low ", ULPI_VENDOR_ID_LOW);
|
| 248 | ulpi_print("Vendor ID High ", ULPI_VENDOR_ID_HIGH);
|
| 249 | ulpi_print("Product ID Low ", ULPI_PRODUCT_ID_LOW);
|
| 250 | ulpi_print("Product ID High ", ULPI_PRODUCT_ID_HIGH);
|
| 251 | ulpi_print("Function Control ", ULPI_FUNC_CTRL);
|
| 252 | ulpi_print("Interface Control ", ULPI_IFC_CTRL);
|
| 253 | ulpi_print("OTG Control ", ULPI_OTG_CTRL);
|
| 254 | ulpi_print("USB Interrupt Enable Rising ", ULPI_USB_INT_EN_RISE);
|
| 255 | ulpi_print("USB Interrupt Enable Falling", ULPI_USB_INT_EN_FALL);
|
| 256 | ulpi_print("USB Interrupt Status ", ULPI_USB_INT_STS);
|
| 257 | ulpi_print("USB Interrupt Latch ", ULPI_USB_INT_LATCH);
|
| 258 | ulpi_print("Debug ", ULPI_DEBUG);
|
| 259 | ulpi_print("Scratch Register ", ULPI_SCRATCH);
|
| 260 | ulpi_print("Carkit Control ", ULPI_CARKIT_CTRL);
|
| 261 | ulpi_print("Carkit Interrupt Delay ", ULPI_CARKIT_INT_DELAY);
|
| 262 | ulpi_print("Carkit Interrupt Enable ", ULPI_CARKIT_INT_EN);
|
| 263 | ulpi_print("Carkit Interrupt Status ", ULPI_CARKIT_INT_STS);
|
| 264 | ulpi_print("Carkit Interrupt Latch ", ULPI_CARKIT_INT_LATCH);
|
| 265 | ulpi_print("Carkit Pulse Control ", ULPI_CARKIT_PLS_CTRL);
|
| 266 | ulpi_print("Transmit Positive Width ", ULPI_TX_POS_WIDTH);
|
| 267 | ulpi_print("Transmit Negative Width ", ULPI_TX_NEG_WIDTH);
|
| 268 | ulpi_print("Receive Polarity Recovery ", ULPI_POLARITY_RECOVERY);
|
| 269 |
|
| 270 | return 0;
|
| 271 | }
|
| 272 | DEFINE_SHOW_ATTRIBUTE(ulpi_regs);
|
| 273 |
|
| 274 | static struct dentry *ulpi_root;
|
| 275 |
|
| 276 | static int ulpi_register(struct device *dev, struct ulpi *ulpi)
|
| 277 | {
|
| 278 | int ret;
|
| 279 | struct dentry *root;
|
| 280 |
|
| 281 | ulpi->dev.parent = dev;
|
| 282 | ulpi->dev.bus = &ulpi_bus;
|
| 283 | ulpi->dev.type = &ulpi_dev_type;
|
| 284 |
|
| 285 | device_initialize(&ulpi->dev);
|
| 286 |
|
| 287 | dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
|
| 288 |
|
| 289 | ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
|
| 290 |
|
| 291 | ret = ulpi_of_register(ulpi);
|
| 292 | if (ret)
|
| 293 | return ret;
|
| 294 |
|
| 295 | ret = ulpi_read_id(ulpi);
|
| 296 | if (ret)
|
| 297 | return ret;
|
| 298 |
|
| 299 | ret = device_add(&ulpi->dev);
|
| 300 | if (ret)
|
| 301 | return ret;
|
| 302 |
|
| 303 | root = debugfs_create_dir(dev_name(&ulpi->dev), ulpi_root);
|
| 304 | debugfs_create_file("regs", 0444, root, ulpi, &ulpi_regs_fops);
|
| 305 |
|
| 306 | dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
|
| 307 | ulpi->id.vendor, ulpi->id.product);
|
| 308 |
|
| 309 | return 0;
|
| 310 | }
|
| 311 |
|
| 312 | |
| 313 | * ulpi_register_interface - instantiate new ULPI device
|
| 314 | * @dev: USB controller's device interface
|
| 315 | * @ops: ULPI register access
|
| 316 | *
|
| 317 | * Allocates and registers a ULPI device and an interface for it. Called from
|
| 318 | * the USB controller that provides the ULPI interface.
|
| 319 |
|
| 320 | struct ulpi *ulpi_register_interface(struct device *dev,
|
| 321 | const struct ulpi_ops *ops)
|
| 322 | {
|
| 323 | struct ulpi *ulpi;
|
| 324 | int ret;
|
| 325 |
|
| 326 | ulpi = kzalloc_obj(*ulpi);
|
| 327 | if (!ulpi)
|
| 328 | return ERR_PTR(-ENOMEM);
|
| 329 |
|
| 330 | ulpi->ops = ops;
|
| 331 |
|
| 332 | ret = ulpi_register(dev, ulpi);
|
| 333 | if (ret) {
|
| 334 | put_device(&ulpi->dev);
|
| 335 | return ERR_PTR(ret);
|
| 336 | }
|
| 337 |
|
| 338 | return ulpi;
|
| 339 | }
|
| 340 | EXPORT_SYMBOL_GPL(ulpi_register_interface);
|
| 341 |
|
| 342 | |
| 343 | * ulpi_unregister_interface - unregister ULPI interface
|
| 344 | * @ulpi: struct ulpi_interface
|
| 345 | *
|
| 346 | * Unregisters a ULPI device and it's interface that was created with
|
| 347 | * ulpi_create_interface().
|
| 348 |
|
| 349 | void ulpi_unregister_interface(struct ulpi *ulpi)
|
| 350 | {
|
| 351 | debugfs_lookup_and_remove(dev_name(&ulpi->dev), ulpi_root);
|
| 352 | device_unregister(&ulpi->dev);
|
| 353 | }
|
| 354 | EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 | static int __init ulpi_init(void)
|
| 359 | {
|
| 360 | int ret;
|
| 361 |
|
| 362 | ulpi_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
|
| 363 | ret = bus_register(&ulpi_bus);
|
| 364 | if (ret)
|
| 365 | debugfs_remove(ulpi_root);
|
| 366 | return ret;
|
| 367 | }
|
| 368 | subsys_initcall(ulpi_init);
|
| 369 |
|
| 370 | static void __exit ulpi_exit(void)
|
| 371 | {
|
| 372 | bus_unregister(&ulpi_bus);
|
| 373 | debugfs_remove(ulpi_root);
|
| 374 | }
|
| 375 | module_exit(ulpi_exit);
|
| 376 |
|
| 377 | MODULE_AUTHOR("Intel Corporation");
|
| 378 | MODULE_LICENSE("GPL v2");
|
| 379 | MODULE_DESCRIPTION("USB ULPI PHY bus");
|