ulpi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include <linux/ulpi/interface.h>
  13. #include <linux/ulpi/driver.h>
  14. #include <linux/ulpi/regs.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/acpi.h>
  18. /* -------------------------------------------------------------------------- */
  19. int ulpi_read(struct ulpi *ulpi, u8 addr)
  20. {
  21. return ulpi->ops->read(ulpi->dev.parent, addr);
  22. }
  23. EXPORT_SYMBOL_GPL(ulpi_read);
  24. int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
  25. {
  26. return ulpi->ops->write(ulpi->dev.parent, addr, val);
  27. }
  28. EXPORT_SYMBOL_GPL(ulpi_write);
  29. /* -------------------------------------------------------------------------- */
  30. static int ulpi_match(struct device *dev, struct device_driver *driver)
  31. {
  32. struct ulpi_driver *drv = to_ulpi_driver(driver);
  33. struct ulpi *ulpi = to_ulpi_dev(dev);
  34. const struct ulpi_device_id *id;
  35. for (id = drv->id_table; id->vendor; id++)
  36. if (id->vendor == ulpi->id.vendor &&
  37. id->product == ulpi->id.product)
  38. return 1;
  39. return 0;
  40. }
  41. static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
  42. {
  43. struct ulpi *ulpi = to_ulpi_dev(dev);
  44. if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
  45. ulpi->id.vendor, ulpi->id.product))
  46. return -ENOMEM;
  47. return 0;
  48. }
  49. static int ulpi_probe(struct device *dev)
  50. {
  51. struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
  52. return drv->probe(to_ulpi_dev(dev));
  53. }
  54. static int ulpi_remove(struct device *dev)
  55. {
  56. struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
  57. if (drv->remove)
  58. drv->remove(to_ulpi_dev(dev));
  59. return 0;
  60. }
  61. static struct bus_type ulpi_bus = {
  62. .name = "ulpi",
  63. .match = ulpi_match,
  64. .uevent = ulpi_uevent,
  65. .probe = ulpi_probe,
  66. .remove = ulpi_remove,
  67. };
  68. /* -------------------------------------------------------------------------- */
  69. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  70. char *buf)
  71. {
  72. struct ulpi *ulpi = to_ulpi_dev(dev);
  73. return sprintf(buf, "ulpi:v%04xp%04x\n",
  74. ulpi->id.vendor, ulpi->id.product);
  75. }
  76. static DEVICE_ATTR_RO(modalias);
  77. static struct attribute *ulpi_dev_attrs[] = {
  78. &dev_attr_modalias.attr,
  79. NULL
  80. };
  81. static struct attribute_group ulpi_dev_attr_group = {
  82. .attrs = ulpi_dev_attrs,
  83. };
  84. static const struct attribute_group *ulpi_dev_attr_groups[] = {
  85. &ulpi_dev_attr_group,
  86. NULL
  87. };
  88. static void ulpi_dev_release(struct device *dev)
  89. {
  90. kfree(to_ulpi_dev(dev));
  91. }
  92. static struct device_type ulpi_dev_type = {
  93. .name = "ulpi_device",
  94. .groups = ulpi_dev_attr_groups,
  95. .release = ulpi_dev_release,
  96. };
  97. /* -------------------------------------------------------------------------- */
  98. /**
  99. * ulpi_register_driver - register a driver with the ULPI bus
  100. * @drv: driver being registered
  101. *
  102. * Registers a driver with the ULPI bus.
  103. */
  104. int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
  105. {
  106. if (!drv->probe)
  107. return -EINVAL;
  108. drv->driver.owner = module;
  109. drv->driver.bus = &ulpi_bus;
  110. return driver_register(&drv->driver);
  111. }
  112. EXPORT_SYMBOL_GPL(__ulpi_register_driver);
  113. /**
  114. * ulpi_unregister_driver - unregister a driver with the ULPI bus
  115. * @drv: driver to unregister
  116. *
  117. * Unregisters a driver with the ULPI bus.
  118. */
  119. void ulpi_unregister_driver(struct ulpi_driver *drv)
  120. {
  121. driver_unregister(&drv->driver);
  122. }
  123. EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
  124. /* -------------------------------------------------------------------------- */
  125. static int ulpi_register(struct device *dev, struct ulpi *ulpi)
  126. {
  127. int ret;
  128. ulpi->dev.parent = dev; /* needed early for ops */
  129. /* Test the interface */
  130. ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
  131. if (ret < 0)
  132. return ret;
  133. ret = ulpi_read(ulpi, ULPI_SCRATCH);
  134. if (ret < 0)
  135. return ret;
  136. if (ret != 0xaa)
  137. return -ENODEV;
  138. ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
  139. ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
  140. ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
  141. ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
  142. ulpi->dev.bus = &ulpi_bus;
  143. ulpi->dev.type = &ulpi_dev_type;
  144. dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
  145. ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
  146. request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
  147. ret = device_register(&ulpi->dev);
  148. if (ret)
  149. return ret;
  150. dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
  151. ulpi->id.vendor, ulpi->id.product);
  152. return 0;
  153. }
  154. /**
  155. * ulpi_register_interface - instantiate new ULPI device
  156. * @dev: USB controller's device interface
  157. * @ops: ULPI register access
  158. *
  159. * Allocates and registers a ULPI device and an interface for it. Called from
  160. * the USB controller that provides the ULPI interface.
  161. */
  162. struct ulpi *ulpi_register_interface(struct device *dev,
  163. const struct ulpi_ops *ops)
  164. {
  165. struct ulpi *ulpi;
  166. int ret;
  167. ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
  168. if (!ulpi)
  169. return ERR_PTR(-ENOMEM);
  170. ulpi->ops = ops;
  171. ret = ulpi_register(dev, ulpi);
  172. if (ret) {
  173. kfree(ulpi);
  174. return ERR_PTR(ret);
  175. }
  176. return ulpi;
  177. }
  178. EXPORT_SYMBOL_GPL(ulpi_register_interface);
  179. /**
  180. * ulpi_unregister_interface - unregister ULPI interface
  181. * @intrf: struct ulpi_interface
  182. *
  183. * Unregisters a ULPI device and it's interface that was created with
  184. * ulpi_create_interface().
  185. */
  186. void ulpi_unregister_interface(struct ulpi *ulpi)
  187. {
  188. device_unregister(&ulpi->dev);
  189. }
  190. EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
  191. /* -------------------------------------------------------------------------- */
  192. static int __init ulpi_init(void)
  193. {
  194. return bus_register(&ulpi_bus);
  195. }
  196. subsys_initcall(ulpi_init);
  197. static void __exit ulpi_exit(void)
  198. {
  199. bus_unregister(&ulpi_bus);
  200. }
  201. module_exit(ulpi_exit);
  202. MODULE_AUTHOR("Intel Corporation");
  203. MODULE_LICENSE("GPL v2");
  204. MODULE_DESCRIPTION("USB ULPI PHY bus");