1 // SPDX-License-Identifier: GPL-2.0 2 /** 3 * ulpi.c - USB ULPI PHY bus 4 * 5 * Copyright (C) 2015 Intel Corporation 6 * 7 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 */ 13 14 #include <linux/ulpi/interface.h> 15 #include <linux/ulpi/driver.h> 16 #include <linux/ulpi/regs.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 #include <linux/acpi.h> 20 #include <linux/of.h> 21 #include <linux/of_device.h> 22 #include <linux/clk/clk-conf.h> 23 24 /* -------------------------------------------------------------------------- */ 25 26 int ulpi_read(struct ulpi *ulpi, u8 addr) 27 { 28 return ulpi->ops->read(ulpi->dev.parent, addr); 29 } 30 EXPORT_SYMBOL_GPL(ulpi_read); 31 32 int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val) 33 { 34 return ulpi->ops->write(ulpi->dev.parent, addr, val); 35 } 36 EXPORT_SYMBOL_GPL(ulpi_write); 37 38 /* -------------------------------------------------------------------------- */ 39 40 static int ulpi_match(struct device *dev, struct device_driver *driver) 41 { 42 struct ulpi_driver *drv = to_ulpi_driver(driver); 43 struct ulpi *ulpi = to_ulpi_dev(dev); 44 const struct ulpi_device_id *id; 45 46 /* Some ULPI devices don't have a vendor id so rely on OF match */ 47 if (ulpi->id.vendor == 0) 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(struct device *dev, struct kobj_uevent_env *env) 59 { 60 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 int 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 return 0; 93 } 94 95 static struct bus_type ulpi_bus = { 96 .name = "ulpi", 97 .match = ulpi_match, 98 .uevent = ulpi_uevent, 99 .probe = ulpi_probe, 100 .remove = ulpi_remove, 101 }; 102 103 /* -------------------------------------------------------------------------- */ 104 105 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 106 char *buf) 107 { 108 int len; 109 struct ulpi *ulpi = to_ulpi_dev(dev); 110 111 len = of_device_modalias(dev, buf, PAGE_SIZE); 112 if (len != -ENODEV) 113 return len; 114 115 return sprintf(buf, "ulpi:v%04xp%04x\n", 116 ulpi->id.vendor, ulpi->id.product); 117 } 118 static DEVICE_ATTR_RO(modalias); 119 120 static struct attribute *ulpi_dev_attrs[] = { 121 &dev_attr_modalias.attr, 122 NULL 123 }; 124 125 static struct attribute_group ulpi_dev_attr_group = { 126 .attrs = ulpi_dev_attrs, 127 }; 128 129 static const struct attribute_group *ulpi_dev_attr_groups[] = { 130 &ulpi_dev_attr_group, 131 NULL 132 }; 133 134 static void ulpi_dev_release(struct device *dev) 135 { 136 kfree(to_ulpi_dev(dev)); 137 } 138 139 static const struct device_type ulpi_dev_type = { 140 .name = "ulpi_device", 141 .groups = ulpi_dev_attr_groups, 142 .release = ulpi_dev_release, 143 }; 144 145 /* -------------------------------------------------------------------------- */ 146 147 /** 148 * ulpi_register_driver - register a driver with the ULPI bus 149 * @drv: driver being registered 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 /* Find a ulpi bus underneath the parent or the grandparent */ 185 parent = ulpi->dev.parent; 186 if (parent->of_node) 187 np = of_find_node_by_name(parent->of_node, "ulpi"); 188 else if (parent->parent && parent->parent->of_node) 189 np = of_find_node_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 /* Test the interface */ 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 /* Some ULPI devices don't have a vendor id so rely on OF match */ 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_device_request_module(&ulpi->dev); 233 return 0; 234 } 235 236 static int ulpi_register(struct device *dev, struct ulpi *ulpi) 237 { 238 int ret; 239 240 ulpi->dev.parent = dev; /* needed early for ops */ 241 ulpi->dev.bus = &ulpi_bus; 242 ulpi->dev.type = &ulpi_dev_type; 243 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev)); 244 245 ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev)); 246 247 ret = ulpi_of_register(ulpi); 248 if (ret) 249 return ret; 250 251 ret = ulpi_read_id(ulpi); 252 if (ret) 253 return ret; 254 255 ret = device_register(&ulpi->dev); 256 if (ret) 257 return ret; 258 259 dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n", 260 ulpi->id.vendor, ulpi->id.product); 261 262 return 0; 263 } 264 265 /** 266 * ulpi_register_interface - instantiate new ULPI device 267 * @dev: USB controller's device interface 268 * @ops: ULPI register access 269 * 270 * Allocates and registers a ULPI device and an interface for it. Called from 271 * the USB controller that provides the ULPI interface. 272 */ 273 struct ulpi *ulpi_register_interface(struct device *dev, 274 const struct ulpi_ops *ops) 275 { 276 struct ulpi *ulpi; 277 int ret; 278 279 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL); 280 if (!ulpi) 281 return ERR_PTR(-ENOMEM); 282 283 ulpi->ops = ops; 284 285 ret = ulpi_register(dev, ulpi); 286 if (ret) { 287 kfree(ulpi); 288 return ERR_PTR(ret); 289 } 290 291 return ulpi; 292 } 293 EXPORT_SYMBOL_GPL(ulpi_register_interface); 294 295 /** 296 * ulpi_unregister_interface - unregister ULPI interface 297 * @intrf: struct ulpi_interface 298 * 299 * Unregisters a ULPI device and it's interface that was created with 300 * ulpi_create_interface(). 301 */ 302 void ulpi_unregister_interface(struct ulpi *ulpi) 303 { 304 of_node_put(ulpi->dev.of_node); 305 device_unregister(&ulpi->dev); 306 } 307 EXPORT_SYMBOL_GPL(ulpi_unregister_interface); 308 309 /* -------------------------------------------------------------------------- */ 310 311 static int __init ulpi_init(void) 312 { 313 return bus_register(&ulpi_bus); 314 } 315 subsys_initcall(ulpi_init); 316 317 static void __exit ulpi_exit(void) 318 { 319 bus_unregister(&ulpi_bus); 320 } 321 module_exit(ulpi_exit); 322 323 MODULE_AUTHOR("Intel Corporation"); 324 MODULE_LICENSE("GPL v2"); 325 MODULE_DESCRIPTION("USB ULPI PHY bus"); 326