GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / usb / common / ulpi.c
1 /**
2  * ulpi.c - USB ULPI PHY bus
3  *
4  * Copyright (C) 2015 Intel Corporation
5  *
6  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/ulpi/interface.h>
14 #include <linux/ulpi/driver.h>
15 #include <linux/ulpi/regs.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/acpi.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/clk/clk-conf.h>
22
23 /* -------------------------------------------------------------------------- */
24
25 int ulpi_read(struct ulpi *ulpi, u8 addr)
26 {
27         return ulpi->ops->read(ulpi->dev.parent, addr);
28 }
29 EXPORT_SYMBOL_GPL(ulpi_read);
30
31 int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
32 {
33         return ulpi->ops->write(ulpi->dev.parent, addr, val);
34 }
35 EXPORT_SYMBOL_GPL(ulpi_write);
36
37 /* -------------------------------------------------------------------------- */
38
39 static int ulpi_match(struct device *dev, struct device_driver *driver)
40 {
41         struct ulpi_driver *drv = to_ulpi_driver(driver);
42         struct ulpi *ulpi = to_ulpi_dev(dev);
43         const struct ulpi_device_id *id;
44
45         /*
46          * Some ULPI devices don't have a vendor id
47          * or provide an id_table so rely on OF match.
48          */
49         if (ulpi->id.vendor == 0 || !drv->id_table)
50                 return of_driver_match_device(dev, driver);
51
52         for (id = drv->id_table; id->vendor; id++)
53                 if (id->vendor == ulpi->id.vendor &&
54                     id->product == ulpi->id.product)
55                         return 1;
56
57         return 0;
58 }
59
60 static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
61 {
62         struct ulpi *ulpi = to_ulpi_dev(dev);
63         int ret;
64
65         ret = of_device_uevent_modalias(dev, env);
66         if (ret != -ENODEV)
67                 return ret;
68
69         if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
70                            ulpi->id.vendor, ulpi->id.product))
71                 return -ENOMEM;
72         return 0;
73 }
74
75 static int ulpi_probe(struct device *dev)
76 {
77         struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
78         int ret;
79
80         ret = of_clk_set_defaults(dev->of_node, false);
81         if (ret < 0)
82                 return ret;
83
84         return drv->probe(to_ulpi_dev(dev));
85 }
86
87 static int ulpi_remove(struct device *dev)
88 {
89         struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
90
91         if (drv->remove)
92                 drv->remove(to_ulpi_dev(dev));
93
94         return 0;
95 }
96
97 static struct bus_type ulpi_bus = {
98         .name = "ulpi",
99         .match = ulpi_match,
100         .uevent = ulpi_uevent,
101         .probe = ulpi_probe,
102         .remove = ulpi_remove,
103 };
104
105 /* -------------------------------------------------------------------------- */
106
107 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
108                              char *buf)
109 {
110         int len;
111         struct ulpi *ulpi = to_ulpi_dev(dev);
112
113         len = of_device_modalias(dev, buf, PAGE_SIZE);
114         if (len != -ENODEV)
115                 return len;
116
117         return sprintf(buf, "ulpi:v%04xp%04x\n",
118                        ulpi->id.vendor, ulpi->id.product);
119 }
120 static DEVICE_ATTR_RO(modalias);
121
122 static struct attribute *ulpi_dev_attrs[] = {
123         &dev_attr_modalias.attr,
124         NULL
125 };
126
127 static struct attribute_group ulpi_dev_attr_group = {
128         .attrs = ulpi_dev_attrs,
129 };
130
131 static const struct attribute_group *ulpi_dev_attr_groups[] = {
132         &ulpi_dev_attr_group,
133         NULL
134 };
135
136 static void ulpi_dev_release(struct device *dev)
137 {
138         of_node_put(dev->of_node);
139         kfree(to_ulpi_dev(dev));
140 }
141
142 static const struct device_type ulpi_dev_type = {
143         .name = "ulpi_device",
144         .groups = ulpi_dev_attr_groups,
145         .release = ulpi_dev_release,
146 };
147
148 /* -------------------------------------------------------------------------- */
149
150 /**
151  * ulpi_register_driver - register a driver with the ULPI bus
152  * @drv: driver being registered
153  *
154  * Registers a driver with the ULPI bus.
155  */
156 int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
157 {
158         if (!drv->probe)
159                 return -EINVAL;
160
161         drv->driver.owner = module;
162         drv->driver.bus = &ulpi_bus;
163
164         return driver_register(&drv->driver);
165 }
166 EXPORT_SYMBOL_GPL(__ulpi_register_driver);
167
168 /**
169  * ulpi_unregister_driver - unregister a driver with the ULPI bus
170  * @drv: driver to unregister
171  *
172  * Unregisters a driver with the ULPI bus.
173  */
174 void ulpi_unregister_driver(struct ulpi_driver *drv)
175 {
176         driver_unregister(&drv->driver);
177 }
178 EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
179
180 /* -------------------------------------------------------------------------- */
181
182 static int ulpi_of_register(struct ulpi *ulpi)
183 {
184         struct device_node *np = NULL, *child;
185         struct device *parent;
186
187         /* Find a ulpi bus underneath the parent or the grandparent */
188         parent = ulpi->dev.parent;
189         if (parent->of_node)
190                 np = of_get_child_by_name(parent->of_node, "ulpi");
191         else if (parent->parent && parent->parent->of_node)
192                 np = of_get_child_by_name(parent->parent->of_node, "ulpi");
193         if (!np)
194                 return 0;
195
196         child = of_get_next_available_child(np, NULL);
197         of_node_put(np);
198         if (!child)
199                 return -EINVAL;
200
201         ulpi->dev.of_node = child;
202
203         return 0;
204 }
205
206 static int ulpi_read_id(struct ulpi *ulpi)
207 {
208         int ret;
209
210         /* Test the interface */
211         ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
212         if (ret < 0)
213                 goto err;
214
215         ret = ulpi_read(ulpi, ULPI_SCRATCH);
216         if (ret < 0)
217                 goto err;
218
219         if (ret != 0xaa)
220                 goto err;
221
222         ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
223         ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
224
225         ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
226         ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
227
228         /* Some ULPI devices don't have a vendor id so rely on OF match */
229         if (ulpi->id.vendor == 0)
230                 goto err;
231
232         request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
233         return 0;
234 err:
235         of_device_request_module(&ulpi->dev);
236         return 0;
237 }
238
239 static int ulpi_register(struct device *dev, struct ulpi *ulpi)
240 {
241         int ret;
242
243         ulpi->dev.parent = dev; /* needed early for ops */
244         ulpi->dev.bus = &ulpi_bus;
245         ulpi->dev.type = &ulpi_dev_type;
246         dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
247
248         ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
249
250         ret = ulpi_of_register(ulpi);
251         if (ret)
252                 return ret;
253
254         ret = ulpi_read_id(ulpi);
255         if (ret) {
256                 of_node_put(ulpi->dev.of_node);
257                 return ret;
258         }
259
260         ret = device_register(&ulpi->dev);
261         if (ret) {
262                 put_device(&ulpi->dev);
263                 return ret;
264         }
265
266         dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
267                 ulpi->id.vendor, ulpi->id.product);
268
269         return 0;
270 }
271
272 /**
273  * ulpi_register_interface - instantiate new ULPI device
274  * @dev: USB controller's device interface
275  * @ops: ULPI register access
276  *
277  * Allocates and registers a ULPI device and an interface for it. Called from
278  * the USB controller that provides the ULPI interface.
279  */
280 struct ulpi *ulpi_register_interface(struct device *dev,
281                                      const struct ulpi_ops *ops)
282 {
283         struct ulpi *ulpi;
284         int ret;
285
286         ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
287         if (!ulpi)
288                 return ERR_PTR(-ENOMEM);
289
290         ulpi->ops = ops;
291
292         ret = ulpi_register(dev, ulpi);
293         if (ret) {
294                 kfree(ulpi);
295                 return ERR_PTR(ret);
296         }
297
298         return ulpi;
299 }
300 EXPORT_SYMBOL_GPL(ulpi_register_interface);
301
302 /**
303  * ulpi_unregister_interface - unregister ULPI interface
304  * @intrf: struct ulpi_interface
305  *
306  * Unregisters a ULPI device and it's interface that was created with
307  * ulpi_create_interface().
308  */
309 void ulpi_unregister_interface(struct ulpi *ulpi)
310 {
311         device_unregister(&ulpi->dev);
312 }
313 EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
314
315 /* -------------------------------------------------------------------------- */
316
317 static int __init ulpi_init(void)
318 {
319         return bus_register(&ulpi_bus);
320 }
321 subsys_initcall(ulpi_init);
322
323 static void __exit ulpi_exit(void)
324 {
325         bus_unregister(&ulpi_bus);
326 }
327 module_exit(ulpi_exit);
328
329 MODULE_AUTHOR("Intel Corporation");
330 MODULE_LICENSE("GPL v2");
331 MODULE_DESCRIPTION("USB ULPI PHY bus");