• 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 	kfree(to_ulpi_dev(dev));
136 }
137 
138 static const struct device_type ulpi_dev_type = {
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  * @module: ends up being THIS_MODULE
150  *
151  * Registers a driver with the ULPI bus.
152  */
__ulpi_register_driver(struct ulpi_driver * drv,struct module * module)153 int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
154 {
155 	if (!drv->probe)
156 		return -EINVAL;
157 
158 	drv->driver.owner = module;
159 	drv->driver.bus = &ulpi_bus;
160 
161 	return driver_register(&drv->driver);
162 }
163 EXPORT_SYMBOL_GPL(__ulpi_register_driver);
164 
165 /**
166  * ulpi_unregister_driver - unregister a driver with the ULPI bus
167  * @drv: driver to unregister
168  *
169  * Unregisters a driver with the ULPI bus.
170  */
ulpi_unregister_driver(struct ulpi_driver * drv)171 void ulpi_unregister_driver(struct ulpi_driver *drv)
172 {
173 	driver_unregister(&drv->driver);
174 }
175 EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
176 
177 /* -------------------------------------------------------------------------- */
178 
ulpi_of_register(struct ulpi * ulpi)179 static int ulpi_of_register(struct ulpi *ulpi)
180 {
181 	struct device_node *np = NULL, *child;
182 	struct device *parent;
183 
184 	/* Find a ulpi bus underneath the parent or the grandparent */
185 	parent = ulpi->dev.parent;
186 	if (parent->of_node)
187 		np = of_get_child_by_name(parent->of_node, "ulpi");
188 	else if (parent->parent && parent->parent->of_node)
189 		np = of_get_child_by_name(parent->parent->of_node, "ulpi");
190 	if (!np)
191 		return 0;
192 
193 	child = of_get_next_available_child(np, NULL);
194 	of_node_put(np);
195 	if (!child)
196 		return -EINVAL;
197 
198 	ulpi->dev.of_node = child;
199 
200 	return 0;
201 }
202 
ulpi_read_id(struct ulpi * ulpi)203 static int ulpi_read_id(struct ulpi *ulpi)
204 {
205 	int ret;
206 
207 	/* Test the interface */
208 	ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
209 	if (ret < 0)
210 		goto err;
211 
212 	ret = ulpi_read(ulpi, ULPI_SCRATCH);
213 	if (ret < 0)
214 		return ret;
215 
216 	if (ret != 0xaa)
217 		goto err;
218 
219 	ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
220 	ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
221 
222 	ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
223 	ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
224 
225 	/* Some ULPI devices don't have a vendor id so rely on OF match */
226 	if (ulpi->id.vendor == 0)
227 		goto err;
228 
229 	request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
230 	return 0;
231 err:
232 	of_device_request_module(&ulpi->dev);
233 	return 0;
234 }
235 
ulpi_register(struct device * dev,struct ulpi * ulpi)236 static int ulpi_register(struct device *dev, struct ulpi *ulpi)
237 {
238 	int ret;
239 
240 	ulpi->dev.parent = dev; /* needed early for ops */
241 	ulpi->dev.bus = &ulpi_bus;
242 	ulpi->dev.type = &ulpi_dev_type;
243 	dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
244 
245 	ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
246 
247 	ret = ulpi_of_register(ulpi);
248 	if (ret)
249 		return ret;
250 
251 	ret = ulpi_read_id(ulpi);
252 	if (ret)
253 		return ret;
254 
255 	ret = device_register(&ulpi->dev);
256 	if (ret)
257 		return ret;
258 
259 	dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
260 		ulpi->id.vendor, ulpi->id.product);
261 
262 	return 0;
263 }
264 
265 /**
266  * ulpi_register_interface - instantiate new ULPI device
267  * @dev: USB controller's device interface
268  * @ops: ULPI register access
269  *
270  * Allocates and registers a ULPI device and an interface for it. Called from
271  * the USB controller that provides the ULPI interface.
272  */
ulpi_register_interface(struct device * dev,const struct ulpi_ops * ops)273 struct ulpi *ulpi_register_interface(struct device *dev,
274 				     const struct ulpi_ops *ops)
275 {
276 	struct ulpi *ulpi;
277 	int ret;
278 
279 	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
280 	if (!ulpi)
281 		return ERR_PTR(-ENOMEM);
282 
283 	ulpi->ops = ops;
284 
285 	ret = ulpi_register(dev, ulpi);
286 	if (ret) {
287 		kfree(ulpi);
288 		return ERR_PTR(ret);
289 	}
290 
291 	return ulpi;
292 }
293 EXPORT_SYMBOL_GPL(ulpi_register_interface);
294 
295 /**
296  * ulpi_unregister_interface - unregister ULPI interface
297  * @ulpi: struct ulpi_interface
298  *
299  * Unregisters a ULPI device and it's interface that was created with
300  * ulpi_create_interface().
301  */
ulpi_unregister_interface(struct ulpi * ulpi)302 void ulpi_unregister_interface(struct ulpi *ulpi)
303 {
304 	of_node_put(ulpi->dev.of_node);
305 	device_unregister(&ulpi->dev);
306 }
307 EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
308 
309 /* -------------------------------------------------------------------------- */
310 
ulpi_init(void)311 static int __init ulpi_init(void)
312 {
313 	return bus_register(&ulpi_bus);
314 }
315 subsys_initcall(ulpi_init);
316 
ulpi_exit(void)317 static void __exit ulpi_exit(void)
318 {
319 	bus_unregister(&ulpi_bus);
320 }
321 module_exit(ulpi_exit);
322 
323 MODULE_AUTHOR("Intel Corporation");
324 MODULE_LICENSE("GPL v2");
325 MODULE_DESCRIPTION("USB ULPI PHY bus");
326