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>
16 #include <linux/debugfs.h>
18 #include <linux/of_device.h>
19 #include <linux/clk/clk-conf.h>
21 /* -------------------------------------------------------------------------- */
23 int ulpi_read(struct ulpi
*ulpi
, u8 addr
)
25 return ulpi
->ops
->read(ulpi
->dev
.parent
, addr
);
27 EXPORT_SYMBOL_GPL(ulpi_read
);
29 int ulpi_write(struct ulpi
*ulpi
, u8 addr
, u8 val
)
31 return ulpi
->ops
->write(ulpi
->dev
.parent
, addr
, val
);
33 EXPORT_SYMBOL_GPL(ulpi_write
);
35 /* -------------------------------------------------------------------------- */
37 static int ulpi_match(struct device
*dev
, const struct device_driver
*driver
)
39 struct ulpi_driver
*drv
= to_ulpi_driver(driver
);
40 struct ulpi
*ulpi
= to_ulpi_dev(dev
);
41 const struct ulpi_device_id
*id
;
44 * Some ULPI devices don't have a vendor id
45 * or provide an id_table so rely on OF match.
47 if (ulpi
->id
.vendor
== 0 || !drv
->id_table
)
48 return of_driver_match_device(dev
, driver
);
50 for (id
= drv
->id_table
; id
->vendor
; id
++)
51 if (id
->vendor
== ulpi
->id
.vendor
&&
52 id
->product
== ulpi
->id
.product
)
58 static int ulpi_uevent(const struct device
*dev
, struct kobj_uevent_env
*env
)
60 const struct ulpi
*ulpi
= to_ulpi_dev(dev
);
63 ret
= of_device_uevent_modalias(dev
, env
);
67 if (add_uevent_var(env
, "MODALIAS=ulpi:v%04xp%04x",
68 ulpi
->id
.vendor
, ulpi
->id
.product
))
73 static int ulpi_probe(struct device
*dev
)
75 struct ulpi_driver
*drv
= to_ulpi_driver(dev
->driver
);
78 ret
= of_clk_set_defaults(dev
->of_node
, false);
82 return drv
->probe(to_ulpi_dev(dev
));
85 static void ulpi_remove(struct device
*dev
)
87 struct ulpi_driver
*drv
= to_ulpi_driver(dev
->driver
);
90 drv
->remove(to_ulpi_dev(dev
));
93 static const struct bus_type ulpi_bus
= {
96 .uevent
= ulpi_uevent
,
98 .remove
= ulpi_remove
,
101 /* -------------------------------------------------------------------------- */
103 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
107 struct ulpi
*ulpi
= to_ulpi_dev(dev
);
109 len
= of_device_modalias(dev
, buf
, PAGE_SIZE
);
113 return sprintf(buf
, "ulpi:v%04xp%04x\n",
114 ulpi
->id
.vendor
, ulpi
->id
.product
);
116 static DEVICE_ATTR_RO(modalias
);
118 static struct attribute
*ulpi_dev_attrs
[] = {
119 &dev_attr_modalias
.attr
,
123 static const struct attribute_group ulpi_dev_attr_group
= {
124 .attrs
= ulpi_dev_attrs
,
127 static const struct attribute_group
*ulpi_dev_attr_groups
[] = {
128 &ulpi_dev_attr_group
,
132 static void ulpi_dev_release(struct device
*dev
)
134 of_node_put(dev
->of_node
);
135 kfree(to_ulpi_dev(dev
));
138 static const struct device_type ulpi_dev_type
= {
139 .name
= "ulpi_device",
140 .groups
= ulpi_dev_attr_groups
,
141 .release
= ulpi_dev_release
,
144 /* -------------------------------------------------------------------------- */
147 * __ulpi_register_driver - register a driver with the ULPI bus
148 * @drv: driver being registered
149 * @module: ends up being THIS_MODULE
151 * Registers a driver with the ULPI bus.
153 int __ulpi_register_driver(struct ulpi_driver
*drv
, struct module
*module
)
158 drv
->driver
.owner
= module
;
159 drv
->driver
.bus
= &ulpi_bus
;
161 return driver_register(&drv
->driver
);
163 EXPORT_SYMBOL_GPL(__ulpi_register_driver
);
166 * ulpi_unregister_driver - unregister a driver with the ULPI bus
167 * @drv: driver to unregister
169 * Unregisters a driver with the ULPI bus.
171 void ulpi_unregister_driver(struct ulpi_driver
*drv
)
173 driver_unregister(&drv
->driver
);
175 EXPORT_SYMBOL_GPL(ulpi_unregister_driver
);
177 /* -------------------------------------------------------------------------- */
179 static int ulpi_of_register(struct ulpi
*ulpi
)
181 struct device_node
*np
= NULL
, *child
;
182 struct device
*parent
;
184 /* Find a ulpi bus underneath the parent or the grandparent */
185 parent
= ulpi
->dev
.parent
;
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");
193 child
= of_get_next_available_child(np
, NULL
);
198 ulpi
->dev
.of_node
= child
;
203 static int ulpi_read_id(struct ulpi
*ulpi
)
207 /* Test the interface */
208 ret
= ulpi_write(ulpi
, ULPI_SCRATCH
, 0xaa);
212 ret
= ulpi_read(ulpi
, ULPI_SCRATCH
);
219 ulpi
->id
.vendor
= ulpi_read(ulpi
, ULPI_VENDOR_ID_LOW
);
220 ulpi
->id
.vendor
|= ulpi_read(ulpi
, ULPI_VENDOR_ID_HIGH
) << 8;
222 ulpi
->id
.product
= ulpi_read(ulpi
, ULPI_PRODUCT_ID_LOW
);
223 ulpi
->id
.product
|= ulpi_read(ulpi
, ULPI_PRODUCT_ID_HIGH
) << 8;
225 /* Some ULPI devices don't have a vendor id so rely on OF match */
226 if (ulpi
->id
.vendor
== 0)
229 request_module("ulpi:v%04xp%04x", ulpi
->id
.vendor
, ulpi
->id
.product
);
232 of_request_module(ulpi
->dev
.of_node
);
236 static int ulpi_regs_show(struct seq_file
*seq
, void *data
)
238 struct ulpi
*ulpi
= seq
->private;
240 #define ulpi_print(name, reg) do { \
241 int ret = ulpi_read(ulpi, reg); \
244 seq_printf(seq, name " %.02x\n", ret); \
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
);
272 DEFINE_SHOW_ATTRIBUTE(ulpi_regs
);
274 static struct dentry
*ulpi_root
;
276 static int ulpi_register(struct device
*dev
, struct ulpi
*ulpi
)
281 ulpi
->dev
.parent
= dev
; /* needed early for ops */
282 ulpi
->dev
.bus
= &ulpi_bus
;
283 ulpi
->dev
.type
= &ulpi_dev_type
;
284 dev_set_name(&ulpi
->dev
, "%s.ulpi", dev_name(dev
));
286 ACPI_COMPANION_SET(&ulpi
->dev
, ACPI_COMPANION(dev
));
288 ret
= ulpi_of_register(ulpi
);
292 ret
= ulpi_read_id(ulpi
);
294 of_node_put(ulpi
->dev
.of_node
);
298 ret
= device_register(&ulpi
->dev
);
300 put_device(&ulpi
->dev
);
304 root
= debugfs_create_dir(dev_name(&ulpi
->dev
), ulpi_root
);
305 debugfs_create_file("regs", 0444, root
, ulpi
, &ulpi_regs_fops
);
307 dev_dbg(&ulpi
->dev
, "registered ULPI PHY: vendor %04x, product %04x\n",
308 ulpi
->id
.vendor
, ulpi
->id
.product
);
314 * ulpi_register_interface - instantiate new ULPI device
315 * @dev: USB controller's device interface
316 * @ops: ULPI register access
318 * Allocates and registers a ULPI device and an interface for it. Called from
319 * the USB controller that provides the ULPI interface.
321 struct ulpi
*ulpi_register_interface(struct device
*dev
,
322 const struct ulpi_ops
*ops
)
327 ulpi
= kzalloc(sizeof(*ulpi
), GFP_KERNEL
);
329 return ERR_PTR(-ENOMEM
);
333 ret
= ulpi_register(dev
, ulpi
);
341 EXPORT_SYMBOL_GPL(ulpi_register_interface
);
344 * ulpi_unregister_interface - unregister ULPI interface
345 * @ulpi: struct ulpi_interface
347 * Unregisters a ULPI device and it's interface that was created with
348 * ulpi_create_interface().
350 void ulpi_unregister_interface(struct ulpi
*ulpi
)
352 debugfs_lookup_and_remove(dev_name(&ulpi
->dev
), ulpi_root
);
353 device_unregister(&ulpi
->dev
);
355 EXPORT_SYMBOL_GPL(ulpi_unregister_interface
);
357 /* -------------------------------------------------------------------------- */
359 static int __init
ulpi_init(void)
363 ulpi_root
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
364 ret
= bus_register(&ulpi_bus
);
366 debugfs_remove(ulpi_root
);
369 subsys_initcall(ulpi_init
);
371 static void __exit
ulpi_exit(void)
373 bus_unregister(&ulpi_bus
);
374 debugfs_remove(ulpi_root
);
376 module_exit(ulpi_exit
);
378 MODULE_AUTHOR("Intel Corporation");
379 MODULE_LICENSE("GPL v2");
380 MODULE_DESCRIPTION("USB ULPI PHY bus");