blob: ce5e6f6711f795e49c1c78ccac0770f5790ae9c0 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Lee Jones246d7a12020-07-02 15:46:02 +01002/*
Heikki Krogerus289fcff2015-05-13 15:26:42 +03003 * ulpi.c - USB ULPI PHY bus
4 *
5 * Copyright (C) 2015 Intel Corporation
6 *
7 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Heikki Krogerus289fcff2015-05-13 15:26:42 +03008 */
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 Boydef6a7bc2016-12-28 14:56:49 -080016#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/clk/clk-conf.h>
Heikki Krogerus289fcff2015-05-13 15:26:42 +030019
20/* -------------------------------------------------------------------------- */
21
22int ulpi_read(struct ulpi *ulpi, u8 addr)
23{
Tal Shorere6f74842016-08-16 19:04:50 +030024 return ulpi->ops->read(ulpi->dev.parent, addr);
Heikki Krogerus289fcff2015-05-13 15:26:42 +030025}
26EXPORT_SYMBOL_GPL(ulpi_read);
27
28int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
29{
Tal Shorere6f74842016-08-16 19:04:50 +030030 return ulpi->ops->write(ulpi->dev.parent, addr, val);
Heikki Krogerus289fcff2015-05-13 15:26:42 +030031}
32EXPORT_SYMBOL_GPL(ulpi_write);
33
34/* -------------------------------------------------------------------------- */
35
36static 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 Boydef6a7bc2016-12-28 14:56:49 -080042 /* 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 Krogerus289fcff2015-05-13 15:26:42 +030046 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
54static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
55{
56 struct ulpi *ulpi = to_ulpi_dev(dev);
Stephen Boydef6a7bc2016-12-28 14:56:49 -080057 int ret;
58
59 ret = of_device_uevent_modalias(dev, env);
60 if (ret != -ENODEV)
61 return ret;
Heikki Krogerus289fcff2015-05-13 15:26:42 +030062
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
69static int ulpi_probe(struct device *dev)
70{
71 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
Stephen Boydef6a7bc2016-12-28 14:56:49 -080072 int ret;
73
74 ret = of_clk_set_defaults(dev->of_node, false);
75 if (ret < 0)
76 return ret;
Heikki Krogerus289fcff2015-05-13 15:26:42 +030077
78 return drv->probe(to_ulpi_dev(dev));
79}
80
81static int ulpi_remove(struct device *dev)
82{
83 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
84
85 if (drv->remove)
86 drv->remove(to_ulpi_dev(dev));
87
88 return 0;
89}
90
91static struct bus_type ulpi_bus = {
92 .name = "ulpi",
93 .match = ulpi_match,
94 .uevent = ulpi_uevent,
95 .probe = ulpi_probe,
96 .remove = ulpi_remove,
97};
98
99/* -------------------------------------------------------------------------- */
100
101static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
102 char *buf)
103{
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800104 int len;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300105 struct ulpi *ulpi = to_ulpi_dev(dev);
106
Rob Herring0634c292017-03-22 09:16:27 -0500107 len = of_device_modalias(dev, buf, PAGE_SIZE);
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800108 if (len != -ENODEV)
109 return len;
110
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300111 return sprintf(buf, "ulpi:v%04xp%04x\n",
112 ulpi->id.vendor, ulpi->id.product);
113}
114static DEVICE_ATTR_RO(modalias);
115
116static struct attribute *ulpi_dev_attrs[] = {
117 &dev_attr_modalias.attr,
118 NULL
119};
120
Rikard Falkeborn52170e92020-11-25 17:25:00 +0100121static const struct attribute_group ulpi_dev_attr_group = {
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300122 .attrs = ulpi_dev_attrs,
123};
124
125static const struct attribute_group *ulpi_dev_attr_groups[] = {
126 &ulpi_dev_attr_group,
127 NULL
128};
129
130static void ulpi_dev_release(struct device *dev)
131{
132 kfree(to_ulpi_dev(dev));
133}
134
Bhumika Goyal53ec7f22017-08-19 13:52:26 +0530135static const struct device_type ulpi_dev_type = {
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300136 .name = "ulpi_device",
137 .groups = ulpi_dev_attr_groups,
138 .release = ulpi_dev_release,
139};
140
141/* -------------------------------------------------------------------------- */
142
143/**
144 * ulpi_register_driver - register a driver with the ULPI bus
145 * @drv: driver being registered
Lee Jones246d7a12020-07-02 15:46:02 +0100146 * @module: ends up being THIS_MODULE
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300147 *
148 * Registers a driver with the ULPI bus.
149 */
Stephen Boyd1ebe88d2016-06-25 22:38:21 -0700150int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300151{
152 if (!drv->probe)
153 return -EINVAL;
154
Stephen Boyd1ebe88d2016-06-25 22:38:21 -0700155 drv->driver.owner = module;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300156 drv->driver.bus = &ulpi_bus;
157
158 return driver_register(&drv->driver);
159}
Stephen Boyd1ebe88d2016-06-25 22:38:21 -0700160EXPORT_SYMBOL_GPL(__ulpi_register_driver);
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300161
162/**
163 * ulpi_unregister_driver - unregister a driver with the ULPI bus
164 * @drv: driver to unregister
165 *
166 * Unregisters a driver with the ULPI bus.
167 */
168void ulpi_unregister_driver(struct ulpi_driver *drv)
169{
170 driver_unregister(&drv->driver);
171}
172EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
173
174/* -------------------------------------------------------------------------- */
175
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800176static int ulpi_of_register(struct ulpi *ulpi)
177{
178 struct device_node *np = NULL, *child;
179 struct device *parent;
180
181 /* Find a ulpi bus underneath the parent or the grandparent */
182 parent = ulpi->dev.parent;
183 if (parent->of_node)
Johan Hovold33c309e2017-11-11 16:31:18 +0100184 np = of_get_child_by_name(parent->of_node, "ulpi");
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800185 else if (parent->parent && parent->parent->of_node)
Johan Hovold33c309e2017-11-11 16:31:18 +0100186 np = of_get_child_by_name(parent->parent->of_node, "ulpi");
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800187 if (!np)
188 return 0;
189
190 child = of_get_next_available_child(np, NULL);
191 of_node_put(np);
192 if (!child)
193 return -EINVAL;
194
195 ulpi->dev.of_node = child;
196
197 return 0;
198}
199
200static int ulpi_read_id(struct ulpi *ulpi)
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300201{
202 int ret;
203
204 /* Test the interface */
205 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
206 if (ret < 0)
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800207 goto err;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300208
209 ret = ulpi_read(ulpi, ULPI_SCRATCH);
210 if (ret < 0)
211 return ret;
212
213 if (ret != 0xaa)
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800214 goto err;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300215
216 ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
217 ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
218
219 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
220 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
221
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800222 /* Some ULPI devices don't have a vendor id so rely on OF match */
223 if (ulpi->id.vendor == 0)
224 goto err;
225
226 request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
227 return 0;
228err:
229 of_device_request_module(&ulpi->dev);
230 return 0;
231}
232
233static int ulpi_register(struct device *dev, struct ulpi *ulpi)
234{
235 int ret;
236
237 ulpi->dev.parent = dev; /* needed early for ops */
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300238 ulpi->dev.bus = &ulpi_bus;
239 ulpi->dev.type = &ulpi_dev_type;
240 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
241
242 ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
243
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800244 ret = ulpi_of_register(ulpi);
245 if (ret)
246 return ret;
247
248 ret = ulpi_read_id(ulpi);
249 if (ret)
250 return ret;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300251
252 ret = device_register(&ulpi->dev);
253 if (ret)
254 return ret;
255
256 dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
257 ulpi->id.vendor, ulpi->id.product);
258
259 return 0;
260}
261
262/**
263 * ulpi_register_interface - instantiate new ULPI device
264 * @dev: USB controller's device interface
265 * @ops: ULPI register access
266 *
267 * Allocates and registers a ULPI device and an interface for it. Called from
268 * the USB controller that provides the ULPI interface.
269 */
Tal Shorerb9454f92016-08-16 19:04:52 +0300270struct ulpi *ulpi_register_interface(struct device *dev,
271 const struct ulpi_ops *ops)
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300272{
273 struct ulpi *ulpi;
274 int ret;
275
276 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
277 if (!ulpi)
278 return ERR_PTR(-ENOMEM);
279
280 ulpi->ops = ops;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300281
282 ret = ulpi_register(dev, ulpi);
283 if (ret) {
284 kfree(ulpi);
285 return ERR_PTR(ret);
286 }
287
288 return ulpi;
289}
290EXPORT_SYMBOL_GPL(ulpi_register_interface);
291
292/**
293 * ulpi_unregister_interface - unregister ULPI interface
Lee Jones246d7a12020-07-02 15:46:02 +0100294 * @ulpi: struct ulpi_interface
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300295 *
296 * Unregisters a ULPI device and it's interface that was created with
297 * ulpi_create_interface().
298 */
299void ulpi_unregister_interface(struct ulpi *ulpi)
300{
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800301 of_node_put(ulpi->dev.of_node);
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300302 device_unregister(&ulpi->dev);
303}
304EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
305
306/* -------------------------------------------------------------------------- */
307
308static int __init ulpi_init(void)
309{
310 return bus_register(&ulpi_bus);
311}
Lu Baolu4696b882015-06-04 20:44:14 +0800312subsys_initcall(ulpi_init);
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300313
314static void __exit ulpi_exit(void)
315{
316 bus_unregister(&ulpi_bus);
317}
318module_exit(ulpi_exit);
319
320MODULE_AUTHOR("Intel Corporation");
321MODULE_LICENSE("GPL v2");
322MODULE_DESCRIPTION("USB ULPI PHY bus");