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