Linux Audio

Check our new training course

Loading...
v5.9
  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
 22int ulpi_read(struct ulpi *ulpi, u8 addr)
 23{
 24	return ulpi->ops->read(ulpi->dev.parent, addr);
 25}
 26EXPORT_SYMBOL_GPL(ulpi_read);
 27
 28int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
 29{
 30	return ulpi->ops->write(ulpi->dev.parent, addr, val);
 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
 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
 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);
 57	int ret;
 58
 59	ret = of_device_uevent_modalias(dev, env);
 60	if (ret != -ENODEV)
 61		return ret;
 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);
 72	int ret;
 73
 74	ret = of_clk_set_defaults(dev->of_node, false);
 75	if (ret < 0)
 76		return ret;
 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{
104	int len;
105	struct ulpi *ulpi = to_ulpi_dev(dev);
106
107	len = of_device_modalias(dev, buf, PAGE_SIZE);
108	if (len != -ENODEV)
109		return len;
110
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
135static const struct device_type ulpi_dev_type = {
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 * @module: ends up being THIS_MODULE
147 *
148 * Registers a driver with the ULPI bus.
149 */
150int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
151{
152	if (!drv->probe)
153		return -EINVAL;
154
155	drv->driver.owner = module;
156	drv->driver.bus = &ulpi_bus;
157
158	return driver_register(&drv->driver);
159}
160EXPORT_SYMBOL_GPL(__ulpi_register_driver);
161
162/**
163 * ulpi_unregister_driver - unregister a driver with the ULPI bus
164 * @drv: driver to unregister
165 *
166 * Unregisters a driver with the ULPI bus.
167 */
168void ulpi_unregister_driver(struct ulpi_driver *drv)
169{
170	driver_unregister(&drv->driver);
171}
172EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
173
174/* -------------------------------------------------------------------------- */
175
176static int ulpi_of_register(struct ulpi *ulpi)
177{
178	struct device_node *np = NULL, *child;
179	struct device *parent;
180
181	/* Find a ulpi bus underneath the parent or the grandparent */
182	parent = ulpi->dev.parent;
183	if (parent->of_node)
184		np = of_get_child_by_name(parent->of_node, "ulpi");
185	else if (parent->parent && parent->parent->of_node)
186		np = of_get_child_by_name(parent->parent->of_node, "ulpi");
187	if (!np)
188		return 0;
189
190	child = of_get_next_available_child(np, NULL);
191	of_node_put(np);
192	if (!child)
193		return -EINVAL;
194
195	ulpi->dev.of_node = child;
196
197	return 0;
198}
199
200static int ulpi_read_id(struct ulpi *ulpi)
201{
202	int ret;
203
204	/* Test the interface */
205	ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
206	if (ret < 0)
207		goto err;
208
209	ret = ulpi_read(ulpi, ULPI_SCRATCH);
210	if (ret < 0)
211		return ret;
212
213	if (ret != 0xaa)
214		goto err;
215
216	ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
217	ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
218
219	ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
220	ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
221
222	/* Some ULPI devices don't have a vendor id so rely on OF match */
223	if (ulpi->id.vendor == 0)
224		goto err;
225
226	request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
227	return 0;
228err:
229	of_device_request_module(&ulpi->dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230	return 0;
231}
 
 
 
232
233static int ulpi_register(struct device *dev, struct ulpi *ulpi)
234{
235	int ret;
 
236
237	ulpi->dev.parent = dev; /* needed early for ops */
238	ulpi->dev.bus = &ulpi_bus;
239	ulpi->dev.type = &ulpi_dev_type;
240	dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
241
242	ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
243
244	ret = ulpi_of_register(ulpi);
245	if (ret)
246		return ret;
247
248	ret = ulpi_read_id(ulpi);
249	if (ret)
 
250		return ret;
 
251
252	ret = device_register(&ulpi->dev);
253	if (ret)
 
254		return ret;
 
 
 
 
255
256	dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
257		ulpi->id.vendor, ulpi->id.product);
258
259	return 0;
260}
261
262/**
263 * ulpi_register_interface - instantiate new ULPI device
264 * @dev: USB controller's device interface
265 * @ops: ULPI register access
266 *
267 * Allocates and registers a ULPI device and an interface for it. Called from
268 * the USB controller that provides the ULPI interface.
269 */
270struct ulpi *ulpi_register_interface(struct device *dev,
271				     const struct ulpi_ops *ops)
272{
273	struct ulpi *ulpi;
274	int ret;
275
276	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
277	if (!ulpi)
278		return ERR_PTR(-ENOMEM);
279
280	ulpi->ops = ops;
281
282	ret = ulpi_register(dev, ulpi);
283	if (ret) {
284		kfree(ulpi);
285		return ERR_PTR(ret);
286	}
287
288	return ulpi;
289}
290EXPORT_SYMBOL_GPL(ulpi_register_interface);
291
292/**
293 * ulpi_unregister_interface - unregister ULPI interface
294 * @ulpi: struct ulpi_interface
295 *
296 * Unregisters a ULPI device and it's interface that was created with
297 * ulpi_create_interface().
298 */
299void ulpi_unregister_interface(struct ulpi *ulpi)
300{
301	of_node_put(ulpi->dev.of_node);
302	device_unregister(&ulpi->dev);
303}
304EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
305
306/* -------------------------------------------------------------------------- */
307
308static int __init ulpi_init(void)
309{
310	return bus_register(&ulpi_bus);
 
 
 
 
 
 
311}
312subsys_initcall(ulpi_init);
313
314static void __exit ulpi_exit(void)
315{
316	bus_unregister(&ulpi_bus);
 
317}
318module_exit(ulpi_exit);
319
320MODULE_AUTHOR("Intel Corporation");
321MODULE_LICENSE("GPL v2");
322MODULE_DESCRIPTION("USB ULPI PHY bus");
v6.8
  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/debugfs.h>
 17#include <linux/of.h>
 18#include <linux/of_device.h>
 19#include <linux/clk/clk-conf.h>
 20
 21/* -------------------------------------------------------------------------- */
 22
 23int ulpi_read(struct ulpi *ulpi, u8 addr)
 24{
 25	return ulpi->ops->read(ulpi->dev.parent, addr);
 26}
 27EXPORT_SYMBOL_GPL(ulpi_read);
 28
 29int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
 30{
 31	return ulpi->ops->write(ulpi->dev.parent, addr, val);
 32}
 33EXPORT_SYMBOL_GPL(ulpi_write);
 34
 35/* -------------------------------------------------------------------------- */
 36
 37static int ulpi_match(struct device *dev, struct device_driver *driver)
 38{
 39	struct ulpi_driver *drv = to_ulpi_driver(driver);
 40	struct ulpi *ulpi = to_ulpi_dev(dev);
 41	const struct ulpi_device_id *id;
 42
 43	/*
 44	 * Some ULPI devices don't have a vendor id
 45	 * or provide an id_table so rely on OF match.
 46	 */
 47	if (ulpi->id.vendor == 0 || !drv->id_table)
 48		return of_driver_match_device(dev, driver);
 49
 50	for (id = drv->id_table; id->vendor; id++)
 51		if (id->vendor == ulpi->id.vendor &&
 52		    id->product == ulpi->id.product)
 53			return 1;
 54
 55	return 0;
 56}
 57
 58static int ulpi_uevent(const struct device *dev, struct kobj_uevent_env *env)
 59{
 60	const struct ulpi *ulpi = to_ulpi_dev(dev);
 61	int ret;
 62
 63	ret = of_device_uevent_modalias(dev, env);
 64	if (ret != -ENODEV)
 65		return ret;
 66
 67	if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
 68			   ulpi->id.vendor, ulpi->id.product))
 69		return -ENOMEM;
 70	return 0;
 71}
 72
 73static int ulpi_probe(struct device *dev)
 74{
 75	struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
 76	int ret;
 77
 78	ret = of_clk_set_defaults(dev->of_node, false);
 79	if (ret < 0)
 80		return ret;
 81
 82	return drv->probe(to_ulpi_dev(dev));
 83}
 84
 85static void ulpi_remove(struct device *dev)
 86{
 87	struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
 88
 89	if (drv->remove)
 90		drv->remove(to_ulpi_dev(dev));
 
 
 91}
 92
 93static const struct bus_type ulpi_bus = {
 94	.name = "ulpi",
 95	.match = ulpi_match,
 96	.uevent = ulpi_uevent,
 97	.probe = ulpi_probe,
 98	.remove = ulpi_remove,
 99};
100
101/* -------------------------------------------------------------------------- */
102
103static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
104			     char *buf)
105{
106	int len;
107	struct ulpi *ulpi = to_ulpi_dev(dev);
108
109	len = of_device_modalias(dev, buf, PAGE_SIZE);
110	if (len != -ENODEV)
111		return len;
112
113	return sprintf(buf, "ulpi:v%04xp%04x\n",
114		       ulpi->id.vendor, ulpi->id.product);
115}
116static DEVICE_ATTR_RO(modalias);
117
118static struct attribute *ulpi_dev_attrs[] = {
119	&dev_attr_modalias.attr,
120	NULL
121};
122
123static const struct attribute_group ulpi_dev_attr_group = {
124	.attrs = ulpi_dev_attrs,
125};
126
127static const struct attribute_group *ulpi_dev_attr_groups[] = {
128	&ulpi_dev_attr_group,
129	NULL
130};
131
132static void ulpi_dev_release(struct device *dev)
133{
134	of_node_put(dev->of_node);
135	kfree(to_ulpi_dev(dev));
136}
137
138static 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 */
153int __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}
163EXPORT_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 */
171void ulpi_unregister_driver(struct ulpi_driver *drv)
172{
173	driver_unregister(&drv->driver);
174}
175EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
176
177/* -------------------------------------------------------------------------- */
178
179static 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
203static 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;
231err:
232	of_request_module(ulpi->dev.of_node);
233	return 0;
234}
235
236static int ulpi_regs_show(struct seq_file *seq, void *data)
237{
238	struct ulpi *ulpi = seq->private;
239
240#define ulpi_print(name, reg) do { \
241	int ret = ulpi_read(ulpi, reg); \
242	if (ret < 0) \
243		return ret; \
244	seq_printf(seq, name " %.02x\n", ret); \
245} while (0)
246
247	ulpi_print("Vendor ID Low               ", ULPI_VENDOR_ID_LOW);
248	ulpi_print("Vendor ID High              ", ULPI_VENDOR_ID_HIGH);
249	ulpi_print("Product ID Low              ", ULPI_PRODUCT_ID_LOW);
250	ulpi_print("Product ID High             ", ULPI_PRODUCT_ID_HIGH);
251	ulpi_print("Function Control            ", ULPI_FUNC_CTRL);
252	ulpi_print("Interface Control           ", ULPI_IFC_CTRL);
253	ulpi_print("OTG Control                 ", ULPI_OTG_CTRL);
254	ulpi_print("USB Interrupt Enable Rising ", ULPI_USB_INT_EN_RISE);
255	ulpi_print("USB Interrupt Enable Falling", ULPI_USB_INT_EN_FALL);
256	ulpi_print("USB Interrupt Status        ", ULPI_USB_INT_STS);
257	ulpi_print("USB Interrupt Latch         ", ULPI_USB_INT_LATCH);
258	ulpi_print("Debug                       ", ULPI_DEBUG);
259	ulpi_print("Scratch Register            ", ULPI_SCRATCH);
260	ulpi_print("Carkit Control              ", ULPI_CARKIT_CTRL);
261	ulpi_print("Carkit Interrupt Delay      ", ULPI_CARKIT_INT_DELAY);
262	ulpi_print("Carkit Interrupt Enable     ", ULPI_CARKIT_INT_EN);
263	ulpi_print("Carkit Interrupt Status     ", ULPI_CARKIT_INT_STS);
264	ulpi_print("Carkit Interrupt Latch      ", ULPI_CARKIT_INT_LATCH);
265	ulpi_print("Carkit Pulse Control        ", ULPI_CARKIT_PLS_CTRL);
266	ulpi_print("Transmit Positive Width     ", ULPI_TX_POS_WIDTH);
267	ulpi_print("Transmit Negative Width     ", ULPI_TX_NEG_WIDTH);
268	ulpi_print("Receive Polarity Recovery   ", ULPI_POLARITY_RECOVERY);
269
270	return 0;
271}
272DEFINE_SHOW_ATTRIBUTE(ulpi_regs);
273
274static struct dentry *ulpi_root;
275
276static int ulpi_register(struct device *dev, struct ulpi *ulpi)
277{
278	int ret;
279	struct dentry *root;
280
281	ulpi->dev.parent = dev; /* needed early for ops */
282	ulpi->dev.bus = &ulpi_bus;
283	ulpi->dev.type = &ulpi_dev_type;
284	dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
285
286	ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
287
288	ret = ulpi_of_register(ulpi);
289	if (ret)
290		return ret;
291
292	ret = ulpi_read_id(ulpi);
293	if (ret) {
294		of_node_put(ulpi->dev.of_node);
295		return ret;
296	}
297
298	ret = device_register(&ulpi->dev);
299	if (ret) {
300		put_device(&ulpi->dev);
301		return ret;
302	}
303
304	root = debugfs_create_dir(dev_name(&ulpi->dev), ulpi_root);
305	debugfs_create_file("regs", 0444, root, ulpi, &ulpi_regs_fops);
306
307	dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
308		ulpi->id.vendor, ulpi->id.product);
309
310	return 0;
311}
312
313/**
314 * ulpi_register_interface - instantiate new ULPI device
315 * @dev: USB controller's device interface
316 * @ops: ULPI register access
317 *
318 * Allocates and registers a ULPI device and an interface for it. Called from
319 * the USB controller that provides the ULPI interface.
320 */
321struct ulpi *ulpi_register_interface(struct device *dev,
322				     const struct ulpi_ops *ops)
323{
324	struct ulpi *ulpi;
325	int ret;
326
327	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
328	if (!ulpi)
329		return ERR_PTR(-ENOMEM);
330
331	ulpi->ops = ops;
332
333	ret = ulpi_register(dev, ulpi);
334	if (ret) {
335		kfree(ulpi);
336		return ERR_PTR(ret);
337	}
338
339	return ulpi;
340}
341EXPORT_SYMBOL_GPL(ulpi_register_interface);
342
343/**
344 * ulpi_unregister_interface - unregister ULPI interface
345 * @ulpi: struct ulpi_interface
346 *
347 * Unregisters a ULPI device and it's interface that was created with
348 * ulpi_create_interface().
349 */
350void ulpi_unregister_interface(struct ulpi *ulpi)
351{
352	debugfs_lookup_and_remove(dev_name(&ulpi->dev), ulpi_root);
353	device_unregister(&ulpi->dev);
354}
355EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
356
357/* -------------------------------------------------------------------------- */
358
359static int __init ulpi_init(void)
360{
361	int ret;
362
363	ulpi_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
364	ret = bus_register(&ulpi_bus);
365	if (ret)
366		debugfs_remove(ulpi_root);
367	return ret;
368}
369subsys_initcall(ulpi_init);
370
371static void __exit ulpi_exit(void)
372{
373	bus_unregister(&ulpi_bus);
374	debugfs_remove(ulpi_root);
375}
376module_exit(ulpi_exit);
377
378MODULE_AUTHOR("Intel Corporation");
379MODULE_LICENSE("GPL v2");
380MODULE_DESCRIPTION("USB ULPI PHY bus");