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