Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 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 | |
| 20 | /* -------------------------------------------------------------------------- */ |
| 21 | |
| 22 | int ulpi_read(struct ulpi *ulpi, u8 addr) |
| 23 | { |
Tal Shorer | e6f7484 | 2016-08-16 19:04:50 +0300 | [diff] [blame^] | 24 | return ulpi->ops->read(ulpi->dev.parent, addr); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 25 | } |
| 26 | EXPORT_SYMBOL_GPL(ulpi_read); |
| 27 | |
| 28 | int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val) |
| 29 | { |
Tal Shorer | e6f7484 | 2016-08-16 19:04:50 +0300 | [diff] [blame^] | 30 | return ulpi->ops->write(ulpi->dev.parent, addr, val); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 31 | } |
| 32 | EXPORT_SYMBOL_GPL(ulpi_write); |
| 33 | |
| 34 | /* -------------------------------------------------------------------------- */ |
| 35 | |
| 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 | for (id = drv->id_table; id->vendor; id++) |
| 43 | if (id->vendor == ulpi->id.vendor && |
| 44 | id->product == ulpi->id.product) |
| 45 | return 1; |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 51 | { |
| 52 | struct ulpi *ulpi = to_ulpi_dev(dev); |
| 53 | |
| 54 | if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x", |
| 55 | ulpi->id.vendor, ulpi->id.product)) |
| 56 | return -ENOMEM; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int ulpi_probe(struct device *dev) |
| 61 | { |
| 62 | struct ulpi_driver *drv = to_ulpi_driver(dev->driver); |
| 63 | |
| 64 | return drv->probe(to_ulpi_dev(dev)); |
| 65 | } |
| 66 | |
| 67 | static int ulpi_remove(struct device *dev) |
| 68 | { |
| 69 | struct ulpi_driver *drv = to_ulpi_driver(dev->driver); |
| 70 | |
| 71 | if (drv->remove) |
| 72 | drv->remove(to_ulpi_dev(dev)); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static struct bus_type ulpi_bus = { |
| 78 | .name = "ulpi", |
| 79 | .match = ulpi_match, |
| 80 | .uevent = ulpi_uevent, |
| 81 | .probe = ulpi_probe, |
| 82 | .remove = ulpi_remove, |
| 83 | }; |
| 84 | |
| 85 | /* -------------------------------------------------------------------------- */ |
| 86 | |
| 87 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 88 | char *buf) |
| 89 | { |
| 90 | struct ulpi *ulpi = to_ulpi_dev(dev); |
| 91 | |
| 92 | return sprintf(buf, "ulpi:v%04xp%04x\n", |
| 93 | ulpi->id.vendor, ulpi->id.product); |
| 94 | } |
| 95 | static DEVICE_ATTR_RO(modalias); |
| 96 | |
| 97 | static struct attribute *ulpi_dev_attrs[] = { |
| 98 | &dev_attr_modalias.attr, |
| 99 | NULL |
| 100 | }; |
| 101 | |
| 102 | static struct attribute_group ulpi_dev_attr_group = { |
| 103 | .attrs = ulpi_dev_attrs, |
| 104 | }; |
| 105 | |
| 106 | static const struct attribute_group *ulpi_dev_attr_groups[] = { |
| 107 | &ulpi_dev_attr_group, |
| 108 | NULL |
| 109 | }; |
| 110 | |
| 111 | static void ulpi_dev_release(struct device *dev) |
| 112 | { |
| 113 | kfree(to_ulpi_dev(dev)); |
| 114 | } |
| 115 | |
| 116 | static struct device_type ulpi_dev_type = { |
| 117 | .name = "ulpi_device", |
| 118 | .groups = ulpi_dev_attr_groups, |
| 119 | .release = ulpi_dev_release, |
| 120 | }; |
| 121 | |
| 122 | /* -------------------------------------------------------------------------- */ |
| 123 | |
| 124 | /** |
| 125 | * ulpi_register_driver - register a driver with the ULPI bus |
| 126 | * @drv: driver being registered |
| 127 | * |
| 128 | * Registers a driver with the ULPI bus. |
| 129 | */ |
| 130 | int ulpi_register_driver(struct ulpi_driver *drv) |
| 131 | { |
| 132 | if (!drv->probe) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | drv->driver.bus = &ulpi_bus; |
| 136 | |
| 137 | return driver_register(&drv->driver); |
| 138 | } |
| 139 | EXPORT_SYMBOL_GPL(ulpi_register_driver); |
| 140 | |
| 141 | /** |
| 142 | * ulpi_unregister_driver - unregister a driver with the ULPI bus |
| 143 | * @drv: driver to unregister |
| 144 | * |
| 145 | * Unregisters a driver with the ULPI bus. |
| 146 | */ |
| 147 | void ulpi_unregister_driver(struct ulpi_driver *drv) |
| 148 | { |
| 149 | driver_unregister(&drv->driver); |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(ulpi_unregister_driver); |
| 152 | |
| 153 | /* -------------------------------------------------------------------------- */ |
| 154 | |
| 155 | static int ulpi_register(struct device *dev, struct ulpi *ulpi) |
| 156 | { |
| 157 | int ret; |
| 158 | |
Tal Shorer | 51b0ce3 | 2016-08-16 19:04:46 +0300 | [diff] [blame] | 159 | ulpi->dev.parent = dev; /* needed early for ops */ |
| 160 | |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 161 | /* Test the interface */ |
| 162 | ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa); |
| 163 | if (ret < 0) |
| 164 | return ret; |
| 165 | |
| 166 | ret = ulpi_read(ulpi, ULPI_SCRATCH); |
| 167 | if (ret < 0) |
| 168 | return ret; |
| 169 | |
| 170 | if (ret != 0xaa) |
| 171 | return -ENODEV; |
| 172 | |
| 173 | ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW); |
| 174 | ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8; |
| 175 | |
| 176 | ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW); |
| 177 | ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8; |
| 178 | |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 179 | ulpi->dev.bus = &ulpi_bus; |
| 180 | ulpi->dev.type = &ulpi_dev_type; |
| 181 | dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev)); |
| 182 | |
| 183 | ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev)); |
| 184 | |
| 185 | request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product); |
| 186 | |
| 187 | ret = device_register(&ulpi->dev); |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | |
| 191 | dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n", |
| 192 | ulpi->id.vendor, ulpi->id.product); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * ulpi_register_interface - instantiate new ULPI device |
| 199 | * @dev: USB controller's device interface |
| 200 | * @ops: ULPI register access |
| 201 | * |
| 202 | * Allocates and registers a ULPI device and an interface for it. Called from |
| 203 | * the USB controller that provides the ULPI interface. |
| 204 | */ |
| 205 | struct ulpi *ulpi_register_interface(struct device *dev, struct ulpi_ops *ops) |
| 206 | { |
| 207 | struct ulpi *ulpi; |
| 208 | int ret; |
| 209 | |
| 210 | ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL); |
| 211 | if (!ulpi) |
| 212 | return ERR_PTR(-ENOMEM); |
| 213 | |
| 214 | ulpi->ops = ops; |
| 215 | ops->dev = dev; |
| 216 | |
| 217 | ret = ulpi_register(dev, ulpi); |
| 218 | if (ret) { |
| 219 | kfree(ulpi); |
| 220 | return ERR_PTR(ret); |
| 221 | } |
| 222 | |
| 223 | return ulpi; |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(ulpi_register_interface); |
| 226 | |
| 227 | /** |
| 228 | * ulpi_unregister_interface - unregister ULPI interface |
| 229 | * @intrf: struct ulpi_interface |
| 230 | * |
| 231 | * Unregisters a ULPI device and it's interface that was created with |
| 232 | * ulpi_create_interface(). |
| 233 | */ |
| 234 | void ulpi_unregister_interface(struct ulpi *ulpi) |
| 235 | { |
| 236 | device_unregister(&ulpi->dev); |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(ulpi_unregister_interface); |
| 239 | |
| 240 | /* -------------------------------------------------------------------------- */ |
| 241 | |
| 242 | static int __init ulpi_init(void) |
| 243 | { |
| 244 | return bus_register(&ulpi_bus); |
| 245 | } |
Lu Baolu | 4696b88 | 2015-06-04 20:44:14 +0800 | [diff] [blame] | 246 | subsys_initcall(ulpi_init); |
Heikki Krogerus | 289fcff | 2015-05-13 15:26:42 +0300 | [diff] [blame] | 247 | |
| 248 | static void __exit ulpi_exit(void) |
| 249 | { |
| 250 | bus_unregister(&ulpi_bus); |
| 251 | } |
| 252 | module_exit(ulpi_exit); |
| 253 | |
| 254 | MODULE_AUTHOR("Intel Corporation"); |
| 255 | MODULE_LICENSE("GPL v2"); |
| 256 | MODULE_DESCRIPTION("USB ULPI PHY bus"); |