Merge remote-tracking branches 'asoc/topic/mc13783', 'asoc/topic/msm8916', 'asoc...
[linux-block.git] / drivers / usb / common / ulpi.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
289fcff4
HK
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>
289fcff4
HK
8 */
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>
ef6a7bcf
SB
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/clk/clk-conf.h>
289fcff4
HK
19
20/* -------------------------------------------------------------------------- */
21
22int ulpi_read(struct ulpi *ulpi, u8 addr)
23{
e6f74849 24 return ulpi->ops->read(ulpi->dev.parent, addr);
289fcff4
HK
25}
26EXPORT_SYMBOL_GPL(ulpi_read);
27
28int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
29{
e6f74849 30 return ulpi->ops->write(ulpi->dev.parent, addr, val);
289fcff4
HK
31}
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
ef6a7bcf
SB
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);
45
289fcff4
HK
46 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);
ef6a7bcf
SB
57 int ret;
58
59 ret = of_device_uevent_modalias(dev, env);
60 if (ret != -ENODEV)
61 return ret;
289fcff4
HK
62
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);
ef6a7bcf
SB
72 int ret;
73
74 ret = of_clk_set_defaults(dev->of_node, false);
75 if (ret < 0)
76 return ret;
289fcff4
HK
77
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{
ef6a7bcf 104 int len;
289fcff4
HK
105 struct ulpi *ulpi = to_ulpi_dev(dev);
106
0634c295 107 len = of_device_modalias(dev, buf, PAGE_SIZE);
ef6a7bcf
SB
108 if (len != -ENODEV)
109 return len;
110
289fcff4
HK
111 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
121static struct attribute_group ulpi_dev_attr_group = {
122 .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
53ec7f27 135static const struct device_type ulpi_dev_type = {
289fcff4
HK
136 .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
146 *
147 * Registers a driver with the ULPI bus.
148 */
1ebe88d3 149int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
289fcff4
HK
150{
151 if (!drv->probe)
152 return -EINVAL;
153
1ebe88d3 154 drv->driver.owner = module;
289fcff4
HK
155 drv->driver.bus = &ulpi_bus;
156
157 return driver_register(&drv->driver);
158}
1ebe88d3 159EXPORT_SYMBOL_GPL(__ulpi_register_driver);
289fcff4
HK
160
161/**
162 * ulpi_unregister_driver - unregister a driver with the ULPI bus
163 * @drv: driver to unregister
164 *
165 * Unregisters a driver with the ULPI bus.
166 */
167void ulpi_unregister_driver(struct ulpi_driver *drv)
168{
169 driver_unregister(&drv->driver);
170}
171EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
172
173/* -------------------------------------------------------------------------- */
174
ef6a7bcf 175static int ulpi_of_register(struct ulpi *ulpi)
289fcff4 176{
ef6a7bcf
SB
177 struct device_node *np = NULL, *child;
178 struct device *parent;
179
180 /* Find a ulpi bus underneath the parent or the grandparent */
181 parent = ulpi->dev.parent;
182 if (parent->of_node)
33c309eb 183 np = of_get_child_by_name(parent->of_node, "ulpi");
ef6a7bcf 184 else if (parent->parent && parent->parent->of_node)
33c309eb 185 np = of_get_child_by_name(parent->parent->of_node, "ulpi");
ef6a7bcf
SB
186 if (!np)
187 return 0;
188
189 child = of_get_next_available_child(np, NULL);
190 of_node_put(np);
191 if (!child)
192 return -EINVAL;
289fcff4 193
ef6a7bcf
SB
194 ulpi->dev.of_node = child;
195
196 return 0;
197}
198
199static int ulpi_read_id(struct ulpi *ulpi)
200{
201 int ret;
51b0ce38 202
289fcff4
HK
203 /* Test the interface */
204 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
205 if (ret < 0)
ef6a7bcf 206 goto err;
289fcff4
HK
207
208 ret = ulpi_read(ulpi, ULPI_SCRATCH);
209 if (ret < 0)
210 return ret;
211
212 if (ret != 0xaa)
ef6a7bcf 213 goto err;
289fcff4
HK
214
215 ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
216 ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
217
218 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
219 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
220
ef6a7bcf
SB
221 /* Some ULPI devices don't have a vendor id so rely on OF match */
222 if (ulpi->id.vendor == 0)
223 goto err;
224
225 request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
226 return 0;
227err:
228 of_device_request_module(&ulpi->dev);
229 return 0;
230}
231
232static int ulpi_register(struct device *dev, struct ulpi *ulpi)
233{
234 int ret;
235
236 ulpi->dev.parent = dev; /* needed early for ops */
289fcff4
HK
237 ulpi->dev.bus = &ulpi_bus;
238 ulpi->dev.type = &ulpi_dev_type;
239 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
240
241 ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
242
ef6a7bcf
SB
243 ret = ulpi_of_register(ulpi);
244 if (ret)
245 return ret;
246
247 ret = ulpi_read_id(ulpi);
248 if (ret)
249 return ret;
289fcff4
HK
250
251 ret = device_register(&ulpi->dev);
252 if (ret)
253 return ret;
254
255 dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
256 ulpi->id.vendor, ulpi->id.product);
257
258 return 0;
259}
260
261/**
262 * ulpi_register_interface - instantiate new ULPI device
263 * @dev: USB controller's device interface
264 * @ops: ULPI register access
265 *
266 * Allocates and registers a ULPI device and an interface for it. Called from
267 * the USB controller that provides the ULPI interface.
268 */
b9454f90
TS
269struct ulpi *ulpi_register_interface(struct device *dev,
270 const struct ulpi_ops *ops)
289fcff4
HK
271{
272 struct ulpi *ulpi;
273 int ret;
274
275 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
276 if (!ulpi)
277 return ERR_PTR(-ENOMEM);
278
279 ulpi->ops = ops;
289fcff4
HK
280
281 ret = ulpi_register(dev, ulpi);
282 if (ret) {
283 kfree(ulpi);
284 return ERR_PTR(ret);
285 }
286
287 return ulpi;
288}
289EXPORT_SYMBOL_GPL(ulpi_register_interface);
290
291/**
292 * ulpi_unregister_interface - unregister ULPI interface
293 * @intrf: struct ulpi_interface
294 *
295 * Unregisters a ULPI device and it's interface that was created with
296 * ulpi_create_interface().
297 */
298void ulpi_unregister_interface(struct ulpi *ulpi)
299{
ef6a7bcf 300 of_node_put(ulpi->dev.of_node);
289fcff4
HK
301 device_unregister(&ulpi->dev);
302}
303EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
304
305/* -------------------------------------------------------------------------- */
306
307static int __init ulpi_init(void)
308{
309 return bus_register(&ulpi_bus);
310}
4696b887 311subsys_initcall(ulpi_init);
289fcff4
HK
312
313static void __exit ulpi_exit(void)
314{
315 bus_unregister(&ulpi_bus);
316}
317module_exit(ulpi_exit);
318
319MODULE_AUTHOR("Intel Corporation");
320MODULE_LICENSE("GPL v2");
321MODULE_DESCRIPTION("USB ULPI PHY bus");