0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/ulpi/interface.h>
0011 #include <linux/ulpi/driver.h>
0012 #include <linux/ulpi/regs.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/acpi.h>
0016 #include <linux/debugfs.h>
0017 #include <linux/of.h>
0018 #include <linux/of_device.h>
0019 #include <linux/clk/clk-conf.h>
0020
0021
0022
0023 int ulpi_read(struct ulpi *ulpi, u8 addr)
0024 {
0025 return ulpi->ops->read(ulpi->dev.parent, addr);
0026 }
0027 EXPORT_SYMBOL_GPL(ulpi_read);
0028
0029 int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
0030 {
0031 return ulpi->ops->write(ulpi->dev.parent, addr, val);
0032 }
0033 EXPORT_SYMBOL_GPL(ulpi_write);
0034
0035
0036
0037 static int ulpi_match(struct device *dev, struct device_driver *driver)
0038 {
0039 struct ulpi_driver *drv = to_ulpi_driver(driver);
0040 struct ulpi *ulpi = to_ulpi_dev(dev);
0041 const struct ulpi_device_id *id;
0042
0043
0044
0045
0046
0047 if (ulpi->id.vendor == 0 || !drv->id_table)
0048 return of_driver_match_device(dev, driver);
0049
0050 for (id = drv->id_table; id->vendor; id++)
0051 if (id->vendor == ulpi->id.vendor &&
0052 id->product == ulpi->id.product)
0053 return 1;
0054
0055 return 0;
0056 }
0057
0058 static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
0059 {
0060 struct ulpi *ulpi = to_ulpi_dev(dev);
0061 int ret;
0062
0063 ret = of_device_uevent_modalias(dev, env);
0064 if (ret != -ENODEV)
0065 return ret;
0066
0067 if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
0068 ulpi->id.vendor, ulpi->id.product))
0069 return -ENOMEM;
0070 return 0;
0071 }
0072
0073 static int ulpi_probe(struct device *dev)
0074 {
0075 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
0076 int ret;
0077
0078 ret = of_clk_set_defaults(dev->of_node, false);
0079 if (ret < 0)
0080 return ret;
0081
0082 return drv->probe(to_ulpi_dev(dev));
0083 }
0084
0085 static void ulpi_remove(struct device *dev)
0086 {
0087 struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
0088
0089 if (drv->remove)
0090 drv->remove(to_ulpi_dev(dev));
0091 }
0092
0093 static struct bus_type ulpi_bus = {
0094 .name = "ulpi",
0095 .match = ulpi_match,
0096 .uevent = ulpi_uevent,
0097 .probe = ulpi_probe,
0098 .remove = ulpi_remove,
0099 };
0100
0101
0102
0103 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
0104 char *buf)
0105 {
0106 int len;
0107 struct ulpi *ulpi = to_ulpi_dev(dev);
0108
0109 len = of_device_modalias(dev, buf, PAGE_SIZE);
0110 if (len != -ENODEV)
0111 return len;
0112
0113 return sprintf(buf, "ulpi:v%04xp%04x\n",
0114 ulpi->id.vendor, ulpi->id.product);
0115 }
0116 static DEVICE_ATTR_RO(modalias);
0117
0118 static struct attribute *ulpi_dev_attrs[] = {
0119 &dev_attr_modalias.attr,
0120 NULL
0121 };
0122
0123 static const struct attribute_group ulpi_dev_attr_group = {
0124 .attrs = ulpi_dev_attrs,
0125 };
0126
0127 static const struct attribute_group *ulpi_dev_attr_groups[] = {
0128 &ulpi_dev_attr_group,
0129 NULL
0130 };
0131
0132 static void ulpi_dev_release(struct device *dev)
0133 {
0134 of_node_put(dev->of_node);
0135 kfree(to_ulpi_dev(dev));
0136 }
0137
0138 static const struct device_type ulpi_dev_type = {
0139 .name = "ulpi_device",
0140 .groups = ulpi_dev_attr_groups,
0141 .release = ulpi_dev_release,
0142 };
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
0154 {
0155 if (!drv->probe)
0156 return -EINVAL;
0157
0158 drv->driver.owner = module;
0159 drv->driver.bus = &ulpi_bus;
0160
0161 return driver_register(&drv->driver);
0162 }
0163 EXPORT_SYMBOL_GPL(__ulpi_register_driver);
0164
0165
0166
0167
0168
0169
0170
0171 void ulpi_unregister_driver(struct ulpi_driver *drv)
0172 {
0173 driver_unregister(&drv->driver);
0174 }
0175 EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
0176
0177
0178
0179 static int ulpi_of_register(struct ulpi *ulpi)
0180 {
0181 struct device_node *np = NULL, *child;
0182 struct device *parent;
0183
0184
0185 parent = ulpi->dev.parent;
0186 if (parent->of_node)
0187 np = of_get_child_by_name(parent->of_node, "ulpi");
0188 else if (parent->parent && parent->parent->of_node)
0189 np = of_get_child_by_name(parent->parent->of_node, "ulpi");
0190 if (!np)
0191 return 0;
0192
0193 child = of_get_next_available_child(np, NULL);
0194 of_node_put(np);
0195 if (!child)
0196 return -EINVAL;
0197
0198 ulpi->dev.of_node = child;
0199
0200 return 0;
0201 }
0202
0203 static int ulpi_read_id(struct ulpi *ulpi)
0204 {
0205 int ret;
0206
0207
0208 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
0209 if (ret < 0)
0210 goto err;
0211
0212 ret = ulpi_read(ulpi, ULPI_SCRATCH);
0213 if (ret < 0)
0214 return ret;
0215
0216 if (ret != 0xaa)
0217 goto err;
0218
0219 ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
0220 ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
0221
0222 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
0223 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
0224
0225
0226 if (ulpi->id.vendor == 0)
0227 goto err;
0228
0229 request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
0230 return 0;
0231 err:
0232 of_device_request_module(&ulpi->dev);
0233 return 0;
0234 }
0235
0236 static int ulpi_regs_read(struct seq_file *seq, void *data)
0237 {
0238 struct ulpi *ulpi = seq->private;
0239
0240 #define ulpi_print(name, reg) do { \
0241 int ret = ulpi_read(ulpi, reg); \
0242 if (ret < 0) \
0243 return ret; \
0244 seq_printf(seq, name " %.02x\n", ret); \
0245 } while (0)
0246
0247 ulpi_print("Vendor ID Low ", ULPI_VENDOR_ID_LOW);
0248 ulpi_print("Vendor ID High ", ULPI_VENDOR_ID_HIGH);
0249 ulpi_print("Product ID Low ", ULPI_PRODUCT_ID_LOW);
0250 ulpi_print("Product ID High ", ULPI_PRODUCT_ID_HIGH);
0251 ulpi_print("Function Control ", ULPI_FUNC_CTRL);
0252 ulpi_print("Interface Control ", ULPI_IFC_CTRL);
0253 ulpi_print("OTG Control ", ULPI_OTG_CTRL);
0254 ulpi_print("USB Interrupt Enable Rising ", ULPI_USB_INT_EN_RISE);
0255 ulpi_print("USB Interrupt Enable Falling", ULPI_USB_INT_EN_FALL);
0256 ulpi_print("USB Interrupt Status ", ULPI_USB_INT_STS);
0257 ulpi_print("USB Interrupt Latch ", ULPI_USB_INT_LATCH);
0258 ulpi_print("Debug ", ULPI_DEBUG);
0259 ulpi_print("Scratch Register ", ULPI_SCRATCH);
0260 ulpi_print("Carkit Control ", ULPI_CARKIT_CTRL);
0261 ulpi_print("Carkit Interrupt Delay ", ULPI_CARKIT_INT_DELAY);
0262 ulpi_print("Carkit Interrupt Enable ", ULPI_CARKIT_INT_EN);
0263 ulpi_print("Carkit Interrupt Status ", ULPI_CARKIT_INT_STS);
0264 ulpi_print("Carkit Interrupt Latch ", ULPI_CARKIT_INT_LATCH);
0265 ulpi_print("Carkit Pulse Control ", ULPI_CARKIT_PLS_CTRL);
0266 ulpi_print("Transmit Positive Width ", ULPI_TX_POS_WIDTH);
0267 ulpi_print("Transmit Negative Width ", ULPI_TX_NEG_WIDTH);
0268 ulpi_print("Receive Polarity Recovery ", ULPI_POLARITY_RECOVERY);
0269
0270 return 0;
0271 }
0272
0273 static int ulpi_regs_open(struct inode *inode, struct file *f)
0274 {
0275 struct ulpi *ulpi = inode->i_private;
0276
0277 return single_open(f, ulpi_regs_read, ulpi);
0278 }
0279
0280 static const struct file_operations ulpi_regs_ops = {
0281 .owner = THIS_MODULE,
0282 .open = ulpi_regs_open,
0283 .release = single_release,
0284 .read = seq_read,
0285 .llseek = seq_lseek
0286 };
0287
0288 #define ULPI_ROOT debugfs_lookup(KBUILD_MODNAME, NULL)
0289
0290 static int ulpi_register(struct device *dev, struct ulpi *ulpi)
0291 {
0292 int ret;
0293 struct dentry *root;
0294
0295 ulpi->dev.parent = dev;
0296 ulpi->dev.bus = &ulpi_bus;
0297 ulpi->dev.type = &ulpi_dev_type;
0298 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
0299
0300 ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
0301
0302 ret = ulpi_of_register(ulpi);
0303 if (ret)
0304 return ret;
0305
0306 ret = ulpi_read_id(ulpi);
0307 if (ret) {
0308 of_node_put(ulpi->dev.of_node);
0309 return ret;
0310 }
0311
0312 ret = device_register(&ulpi->dev);
0313 if (ret) {
0314 put_device(&ulpi->dev);
0315 return ret;
0316 }
0317
0318 root = debugfs_create_dir(dev_name(dev), ULPI_ROOT);
0319 debugfs_create_file("regs", 0444, root, ulpi, &ulpi_regs_ops);
0320
0321 dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
0322 ulpi->id.vendor, ulpi->id.product);
0323
0324 return 0;
0325 }
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 struct ulpi *ulpi_register_interface(struct device *dev,
0336 const struct ulpi_ops *ops)
0337 {
0338 struct ulpi *ulpi;
0339 int ret;
0340
0341 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
0342 if (!ulpi)
0343 return ERR_PTR(-ENOMEM);
0344
0345 ulpi->ops = ops;
0346
0347 ret = ulpi_register(dev, ulpi);
0348 if (ret) {
0349 kfree(ulpi);
0350 return ERR_PTR(ret);
0351 }
0352
0353 return ulpi;
0354 }
0355 EXPORT_SYMBOL_GPL(ulpi_register_interface);
0356
0357
0358
0359
0360
0361
0362
0363
0364 void ulpi_unregister_interface(struct ulpi *ulpi)
0365 {
0366 debugfs_remove_recursive(debugfs_lookup(dev_name(&ulpi->dev),
0367 ULPI_ROOT));
0368 device_unregister(&ulpi->dev);
0369 }
0370 EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
0371
0372
0373
0374 static int __init ulpi_init(void)
0375 {
0376 int ret;
0377 struct dentry *root;
0378
0379 root = debugfs_create_dir(KBUILD_MODNAME, NULL);
0380 ret = bus_register(&ulpi_bus);
0381 if (ret)
0382 debugfs_remove(root);
0383 return ret;
0384 }
0385 subsys_initcall(ulpi_init);
0386
0387 static void __exit ulpi_exit(void)
0388 {
0389 bus_unregister(&ulpi_bus);
0390 debugfs_remove_recursive(ULPI_ROOT);
0391 }
0392 module_exit(ulpi_exit);
0393
0394 MODULE_AUTHOR("Intel Corporation");
0395 MODULE_LICENSE("GPL v2");
0396 MODULE_DESCRIPTION("USB ULPI PHY bus");