1 // SPDX-License-Identifier: GPL-2.0
3 * ulpi.c - USB ULPI PHY bus
5 * Copyright (C) 2015 Intel Corporation
7 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
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>
17 #include <linux/of_device.h>
18 #include <linux/clk/clk-conf.h>
20 /* -------------------------------------------------------------------------- */
22 int ulpi_read(struct ulpi *ulpi, u8 addr)
24 return ulpi->ops->read(ulpi->dev.parent, addr);
26 EXPORT_SYMBOL_GPL(ulpi_read);
28 int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
30 return ulpi->ops->write(ulpi->dev.parent, addr, val);
32 EXPORT_SYMBOL_GPL(ulpi_write);
34 /* -------------------------------------------------------------------------- */
36 static int ulpi_match(struct device *dev, struct device_driver *driver)
38 struct ulpi_driver *drv = to_ulpi_driver(driver);
39 struct ulpi *ulpi = to_ulpi_dev(dev);
40 const struct ulpi_device_id *id;
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);
46 for (id = drv->id_table; id->vendor; id++)
47 if (id->vendor == ulpi->id.vendor &&
48 id->product == ulpi->id.product)
54 static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
56 struct ulpi *ulpi = to_ulpi_dev(dev);
59 ret = of_device_uevent_modalias(dev, env);
63 if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
64 ulpi->id.vendor, ulpi->id.product))
69 static int ulpi_probe(struct device *dev)
71 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
74 ret = of_clk_set_defaults(dev->of_node, false);
78 return drv->probe(to_ulpi_dev(dev));
81 static int ulpi_remove(struct device *dev)
83 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
86 drv->remove(to_ulpi_dev(dev));
91 static struct bus_type ulpi_bus = {
94 .uevent = ulpi_uevent,
96 .remove = ulpi_remove,
99 /* -------------------------------------------------------------------------- */
101 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
105 struct ulpi *ulpi = to_ulpi_dev(dev);
107 len = of_device_modalias(dev, buf, PAGE_SIZE);
111 return sprintf(buf, "ulpi:v%04xp%04x\n",
112 ulpi->id.vendor, ulpi->id.product);
114 static DEVICE_ATTR_RO(modalias);
116 static struct attribute *ulpi_dev_attrs[] = {
117 &dev_attr_modalias.attr,
121 static struct attribute_group ulpi_dev_attr_group = {
122 .attrs = ulpi_dev_attrs,
125 static const struct attribute_group *ulpi_dev_attr_groups[] = {
126 &ulpi_dev_attr_group,
130 static void ulpi_dev_release(struct device *dev)
132 kfree(to_ulpi_dev(dev));
135 static const struct device_type ulpi_dev_type = {
136 .name = "ulpi_device",
137 .groups = ulpi_dev_attr_groups,
138 .release = ulpi_dev_release,
141 /* -------------------------------------------------------------------------- */
144 * ulpi_register_driver - register a driver with the ULPI bus
145 * @drv: driver being registered
146 * @module: ends up being THIS_MODULE
148 * Registers a driver with the ULPI bus.
150 int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
155 drv->driver.owner = module;
156 drv->driver.bus = &ulpi_bus;
158 return driver_register(&drv->driver);
160 EXPORT_SYMBOL_GPL(__ulpi_register_driver);
163 * ulpi_unregister_driver - unregister a driver with the ULPI bus
164 * @drv: driver to unregister
166 * Unregisters a driver with the ULPI bus.
168 void ulpi_unregister_driver(struct ulpi_driver *drv)
170 driver_unregister(&drv->driver);
172 EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
174 /* -------------------------------------------------------------------------- */
176 static int ulpi_of_register(struct ulpi *ulpi)
178 struct device_node *np = NULL, *child;
179 struct device *parent;
181 /* Find a ulpi bus underneath the parent or the grandparent */
182 parent = ulpi->dev.parent;
184 np = of_get_child_by_name(parent->of_node, "ulpi");
185 else if (parent->parent && parent->parent->of_node)
186 np = of_get_child_by_name(parent->parent->of_node, "ulpi");
190 child = of_get_next_available_child(np, NULL);
195 ulpi->dev.of_node = child;
200 static int ulpi_read_id(struct ulpi *ulpi)
204 /* Test the interface */
205 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
209 ret = ulpi_read(ulpi, ULPI_SCRATCH);
216 ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
217 ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
219 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
220 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
222 /* Some ULPI devices don't have a vendor id so rely on OF match */
223 if (ulpi->id.vendor == 0)
226 request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
229 of_device_request_module(&ulpi->dev);
233 static int ulpi_register(struct device *dev, struct ulpi *ulpi)
237 ulpi->dev.parent = dev; /* needed early for ops */
238 ulpi->dev.bus = &ulpi_bus;
239 ulpi->dev.type = &ulpi_dev_type;
240 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
242 ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
244 ret = ulpi_of_register(ulpi);
248 ret = ulpi_read_id(ulpi);
252 ret = device_register(&ulpi->dev);
256 dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
257 ulpi->id.vendor, ulpi->id.product);
263 * ulpi_register_interface - instantiate new ULPI device
264 * @dev: USB controller's device interface
265 * @ops: ULPI register access
267 * Allocates and registers a ULPI device and an interface for it. Called from
268 * the USB controller that provides the ULPI interface.
270 struct ulpi *ulpi_register_interface(struct device *dev,
271 const struct ulpi_ops *ops)
276 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
278 return ERR_PTR(-ENOMEM);
282 ret = ulpi_register(dev, ulpi);
290 EXPORT_SYMBOL_GPL(ulpi_register_interface);
293 * ulpi_unregister_interface - unregister ULPI interface
294 * @ulpi: struct ulpi_interface
296 * Unregisters a ULPI device and it's interface that was created with
297 * ulpi_create_interface().
299 void ulpi_unregister_interface(struct ulpi *ulpi)
301 of_node_put(ulpi->dev.of_node);
302 device_unregister(&ulpi->dev);
304 EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
306 /* -------------------------------------------------------------------------- */
308 static int __init ulpi_init(void)
310 return bus_register(&ulpi_bus);
312 subsys_initcall(ulpi_init);
314 static void __exit ulpi_exit(void)
316 bus_unregister(&ulpi_bus);
318 module_exit(ulpi_exit);
320 MODULE_AUTHOR("Intel Corporation");
321 MODULE_LICENSE("GPL v2");
322 MODULE_DESCRIPTION("USB ULPI PHY bus");