blob: 8f8405b0d6080ddd41fb8285f86a9c74c236668a [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
Jon Hunter2e3dd4a2022-01-17 15:00:39 +000042 /*
43 * Some ULPI devices don't have a vendor id
44 * or provide an id_table so rely on OF match.
45 */
46 if (ulpi->id.vendor == 0 || !drv->id_table)
Stephen Boydef6a7bc2016-12-28 14:56:49 -080047 return of_driver_match_device(dev, driver);
48
Heikki Krogerus289fcff2015-05-13 15:26:42 +030049 for (id = drv->id_table; id->vendor; id++)
50 if (id->vendor == ulpi->id.vendor &&
51 id->product == ulpi->id.product)
52 return 1;
53
54 return 0;
55}
56
57static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
58{
59 struct ulpi *ulpi = to_ulpi_dev(dev);
Stephen Boydef6a7bc2016-12-28 14:56:49 -080060 int ret;
61
62 ret = of_device_uevent_modalias(dev, env);
63 if (ret != -ENODEV)
64 return ret;
Heikki Krogerus289fcff2015-05-13 15:26:42 +030065
66 if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
67 ulpi->id.vendor, ulpi->id.product))
68 return -ENOMEM;
69 return 0;
70}
71
72static int ulpi_probe(struct device *dev)
73{
74 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
Stephen Boydef6a7bc2016-12-28 14:56:49 -080075 int ret;
76
77 ret = of_clk_set_defaults(dev->of_node, false);
78 if (ret < 0)
79 return ret;
Heikki Krogerus289fcff2015-05-13 15:26:42 +030080
81 return drv->probe(to_ulpi_dev(dev));
82}
83
Uwe Kleine-Königfc7a6202021-07-13 21:35:22 +020084static void ulpi_remove(struct device *dev)
Heikki Krogerus289fcff2015-05-13 15:26:42 +030085{
86 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
87
88 if (drv->remove)
89 drv->remove(to_ulpi_dev(dev));
Heikki Krogerus289fcff2015-05-13 15:26:42 +030090}
91
92static struct bus_type ulpi_bus = {
93 .name = "ulpi",
94 .match = ulpi_match,
95 .uevent = ulpi_uevent,
96 .probe = ulpi_probe,
97 .remove = ulpi_remove,
98};
99
100/* -------------------------------------------------------------------------- */
101
102static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
103 char *buf)
104{
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800105 int len;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300106 struct ulpi *ulpi = to_ulpi_dev(dev);
107
Rob Herring0634c292017-03-22 09:16:27 -0500108 len = of_device_modalias(dev, buf, PAGE_SIZE);
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800109 if (len != -ENODEV)
110 return len;
111
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300112 return sprintf(buf, "ulpi:v%04xp%04x\n",
113 ulpi->id.vendor, ulpi->id.product);
114}
115static DEVICE_ATTR_RO(modalias);
116
117static struct attribute *ulpi_dev_attrs[] = {
118 &dev_attr_modalias.attr,
119 NULL
120};
121
Rikard Falkeborn52170e92020-11-25 17:25:00 +0100122static const struct attribute_group ulpi_dev_attr_group = {
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300123 .attrs = ulpi_dev_attrs,
124};
125
126static const struct attribute_group *ulpi_dev_attr_groups[] = {
127 &ulpi_dev_attr_group,
128 NULL
129};
130
131static void ulpi_dev_release(struct device *dev)
132{
133 kfree(to_ulpi_dev(dev));
134}
135
Bhumika Goyal53ec7f22017-08-19 13:52:26 +0530136static const struct device_type ulpi_dev_type = {
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300137 .name = "ulpi_device",
138 .groups = ulpi_dev_attr_groups,
139 .release = ulpi_dev_release,
140};
141
142/* -------------------------------------------------------------------------- */
143
144/**
Lee Jones826e9c42021-05-26 14:00:16 +0100145 * __ulpi_register_driver - register a driver with the ULPI bus
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300146 * @drv: driver being registered
Lee Jones246d7a12020-07-02 15:46:02 +0100147 * @module: ends up being THIS_MODULE
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300148 *
149 * Registers a driver with the ULPI bus.
150 */
Stephen Boyd1ebe88d2016-06-25 22:38:21 -0700151int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300152{
153 if (!drv->probe)
154 return -EINVAL;
155
Stephen Boyd1ebe88d2016-06-25 22:38:21 -0700156 drv->driver.owner = module;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300157 drv->driver.bus = &ulpi_bus;
158
159 return driver_register(&drv->driver);
160}
Stephen Boyd1ebe88d2016-06-25 22:38:21 -0700161EXPORT_SYMBOL_GPL(__ulpi_register_driver);
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300162
163/**
164 * ulpi_unregister_driver - unregister a driver with the ULPI bus
165 * @drv: driver to unregister
166 *
167 * Unregisters a driver with the ULPI bus.
168 */
169void ulpi_unregister_driver(struct ulpi_driver *drv)
170{
171 driver_unregister(&drv->driver);
172}
173EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
174
175/* -------------------------------------------------------------------------- */
176
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800177static int ulpi_of_register(struct ulpi *ulpi)
178{
179 struct device_node *np = NULL, *child;
180 struct device *parent;
181
182 /* Find a ulpi bus underneath the parent or the grandparent */
183 parent = ulpi->dev.parent;
184 if (parent->of_node)
Johan Hovold33c309e2017-11-11 16:31:18 +0100185 np = of_get_child_by_name(parent->of_node, "ulpi");
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800186 else if (parent->parent && parent->parent->of_node)
Johan Hovold33c309e2017-11-11 16:31:18 +0100187 np = of_get_child_by_name(parent->parent->of_node, "ulpi");
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800188 if (!np)
189 return 0;
190
191 child = of_get_next_available_child(np, NULL);
192 of_node_put(np);
193 if (!child)
194 return -EINVAL;
195
196 ulpi->dev.of_node = child;
197
198 return 0;
199}
200
201static int ulpi_read_id(struct ulpi *ulpi)
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300202{
203 int ret;
204
205 /* Test the interface */
206 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
207 if (ret < 0)
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800208 goto err;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300209
210 ret = ulpi_read(ulpi, ULPI_SCRATCH);
211 if (ret < 0)
212 return ret;
213
214 if (ret != 0xaa)
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800215 goto err;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300216
217 ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
218 ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
219
220 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
221 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
222
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800223 /* Some ULPI devices don't have a vendor id so rely on OF match */
224 if (ulpi->id.vendor == 0)
225 goto err;
226
227 request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
228 return 0;
229err:
230 of_device_request_module(&ulpi->dev);
231 return 0;
232}
233
234static int ulpi_register(struct device *dev, struct ulpi *ulpi)
235{
236 int ret;
237
238 ulpi->dev.parent = dev; /* needed early for ops */
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300239 ulpi->dev.bus = &ulpi_bus;
240 ulpi->dev.type = &ulpi_dev_type;
241 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
242
243 ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
244
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800245 ret = ulpi_of_register(ulpi);
246 if (ret)
247 return ret;
248
249 ret = ulpi_read_id(ulpi);
250 if (ret)
251 return ret;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300252
253 ret = device_register(&ulpi->dev);
254 if (ret)
255 return ret;
256
257 dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
258 ulpi->id.vendor, ulpi->id.product);
259
260 return 0;
261}
262
263/**
264 * ulpi_register_interface - instantiate new ULPI device
265 * @dev: USB controller's device interface
266 * @ops: ULPI register access
267 *
268 * Allocates and registers a ULPI device and an interface for it. Called from
269 * the USB controller that provides the ULPI interface.
270 */
Tal Shorerb9454f92016-08-16 19:04:52 +0300271struct ulpi *ulpi_register_interface(struct device *dev,
272 const struct ulpi_ops *ops)
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300273{
274 struct ulpi *ulpi;
275 int ret;
276
277 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
278 if (!ulpi)
279 return ERR_PTR(-ENOMEM);
280
281 ulpi->ops = ops;
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300282
283 ret = ulpi_register(dev, ulpi);
284 if (ret) {
285 kfree(ulpi);
286 return ERR_PTR(ret);
287 }
288
289 return ulpi;
290}
291EXPORT_SYMBOL_GPL(ulpi_register_interface);
292
293/**
294 * ulpi_unregister_interface - unregister ULPI interface
Lee Jones246d7a12020-07-02 15:46:02 +0100295 * @ulpi: struct ulpi_interface
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300296 *
297 * Unregisters a ULPI device and it's interface that was created with
298 * ulpi_create_interface().
299 */
300void ulpi_unregister_interface(struct ulpi *ulpi)
301{
Stephen Boydef6a7bc2016-12-28 14:56:49 -0800302 of_node_put(ulpi->dev.of_node);
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300303 device_unregister(&ulpi->dev);
304}
305EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
306
307/* -------------------------------------------------------------------------- */
308
309static int __init ulpi_init(void)
310{
311 return bus_register(&ulpi_bus);
312}
Lu Baolu4696b882015-06-04 20:44:14 +0800313subsys_initcall(ulpi_init);
Heikki Krogerus289fcff2015-05-13 15:26:42 +0300314
315static void __exit ulpi_exit(void)
316{
317 bus_unregister(&ulpi_bus);
318}
319module_exit(ulpi_exit);
320
321MODULE_AUTHOR("Intel Corporation");
322MODULE_LICENSE("GPL v2");
323MODULE_DESCRIPTION("USB ULPI PHY bus");