ulpi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include <linux/ulpi/interface.h>
  10. #include <linux/ulpi/driver.h>
  11. #include <linux/ulpi/regs.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/acpi.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/clk/clk-conf.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. /* Some ULPI devices don't have a vendor id so rely on OF match */
  36. if (ulpi->id.vendor == 0)
  37. return of_driver_match_device(dev, driver);
  38. for (id = drv->id_table; id->vendor; id++)
  39. if (id->vendor == ulpi->id.vendor &&
  40. id->product == ulpi->id.product)
  41. return 1;
  42. return 0;
  43. }
  44. static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
  45. {
  46. struct ulpi *ulpi = to_ulpi_dev(dev);
  47. int ret;
  48. ret = of_device_uevent_modalias(dev, env);
  49. if (ret != -ENODEV)
  50. return ret;
  51. if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
  52. ulpi->id.vendor, ulpi->id.product))
  53. return -ENOMEM;
  54. return 0;
  55. }
  56. static int ulpi_probe(struct device *dev)
  57. {
  58. struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
  59. int ret;
  60. ret = of_clk_set_defaults(dev->of_node, false);
  61. if (ret < 0)
  62. return ret;
  63. return drv->probe(to_ulpi_dev(dev));
  64. }
  65. static int ulpi_remove(struct device *dev)
  66. {
  67. struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
  68. if (drv->remove)
  69. drv->remove(to_ulpi_dev(dev));
  70. return 0;
  71. }
  72. static struct bus_type ulpi_bus = {
  73. .name = "ulpi",
  74. .match = ulpi_match,
  75. .uevent = ulpi_uevent,
  76. .probe = ulpi_probe,
  77. .remove = ulpi_remove,
  78. };
  79. /* -------------------------------------------------------------------------- */
  80. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  81. char *buf)
  82. {
  83. int len;
  84. struct ulpi *ulpi = to_ulpi_dev(dev);
  85. len = of_device_modalias(dev, buf, PAGE_SIZE);
  86. if (len != -ENODEV)
  87. return len;
  88. return sprintf(buf, "ulpi:v%04xp%04x\n",
  89. ulpi->id.vendor, ulpi->id.product);
  90. }
  91. static DEVICE_ATTR_RO(modalias);
  92. static struct attribute *ulpi_dev_attrs[] = {
  93. &dev_attr_modalias.attr,
  94. NULL
  95. };
  96. static struct attribute_group ulpi_dev_attr_group = {
  97. .attrs = ulpi_dev_attrs,
  98. };
  99. static const struct attribute_group *ulpi_dev_attr_groups[] = {
  100. &ulpi_dev_attr_group,
  101. NULL
  102. };
  103. static void ulpi_dev_release(struct device *dev)
  104. {
  105. kfree(to_ulpi_dev(dev));
  106. }
  107. static const struct device_type ulpi_dev_type = {
  108. .name = "ulpi_device",
  109. .groups = ulpi_dev_attr_groups,
  110. .release = ulpi_dev_release,
  111. };
  112. /* -------------------------------------------------------------------------- */
  113. /**
  114. * ulpi_register_driver - register a driver with the ULPI bus
  115. * @drv: driver being registered
  116. *
  117. * Registers a driver with the ULPI bus.
  118. */
  119. int __ulpi_register_driver(struct ulpi_driver *drv, struct module *module)
  120. {
  121. if (!drv->probe)
  122. return -EINVAL;
  123. drv->driver.owner = module;
  124. drv->driver.bus = &ulpi_bus;
  125. return driver_register(&drv->driver);
  126. }
  127. EXPORT_SYMBOL_GPL(__ulpi_register_driver);
  128. /**
  129. * ulpi_unregister_driver - unregister a driver with the ULPI bus
  130. * @drv: driver to unregister
  131. *
  132. * Unregisters a driver with the ULPI bus.
  133. */
  134. void ulpi_unregister_driver(struct ulpi_driver *drv)
  135. {
  136. driver_unregister(&drv->driver);
  137. }
  138. EXPORT_SYMBOL_GPL(ulpi_unregister_driver);
  139. /* -------------------------------------------------------------------------- */
  140. static int ulpi_of_register(struct ulpi *ulpi)
  141. {
  142. struct device_node *np = NULL, *child;
  143. struct device *parent;
  144. /* Find a ulpi bus underneath the parent or the grandparent */
  145. parent = ulpi->dev.parent;
  146. if (parent->of_node)
  147. np = of_get_child_by_name(parent->of_node, "ulpi");
  148. else if (parent->parent && parent->parent->of_node)
  149. np = of_get_child_by_name(parent->parent->of_node, "ulpi");
  150. if (!np)
  151. return 0;
  152. child = of_get_next_available_child(np, NULL);
  153. of_node_put(np);
  154. if (!child)
  155. return -EINVAL;
  156. ulpi->dev.of_node = child;
  157. return 0;
  158. }
  159. static int ulpi_read_id(struct ulpi *ulpi)
  160. {
  161. int ret;
  162. /* Test the interface */
  163. ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa);
  164. if (ret < 0)
  165. goto err;
  166. ret = ulpi_read(ulpi, ULPI_SCRATCH);
  167. if (ret < 0)
  168. return ret;
  169. if (ret != 0xaa)
  170. goto err;
  171. ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW);
  172. ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8;
  173. ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW);
  174. ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8;
  175. /* Some ULPI devices don't have a vendor id so rely on OF match */
  176. if (ulpi->id.vendor == 0)
  177. goto err;
  178. request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product);
  179. return 0;
  180. err:
  181. of_device_request_module(&ulpi->dev);
  182. return 0;
  183. }
  184. static int ulpi_register(struct device *dev, struct ulpi *ulpi)
  185. {
  186. int ret;
  187. ulpi->dev.parent = dev; /* needed early for ops */
  188. ulpi->dev.bus = &ulpi_bus;
  189. ulpi->dev.type = &ulpi_dev_type;
  190. dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
  191. ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
  192. ret = ulpi_of_register(ulpi);
  193. if (ret)
  194. return ret;
  195. ret = ulpi_read_id(ulpi);
  196. if (ret)
  197. return ret;
  198. ret = device_register(&ulpi->dev);
  199. if (ret)
  200. return ret;
  201. dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
  202. ulpi->id.vendor, ulpi->id.product);
  203. return 0;
  204. }
  205. /**
  206. * ulpi_register_interface - instantiate new ULPI device
  207. * @dev: USB controller's device interface
  208. * @ops: ULPI register access
  209. *
  210. * Allocates and registers a ULPI device and an interface for it. Called from
  211. * the USB controller that provides the ULPI interface.
  212. */
  213. struct ulpi *ulpi_register_interface(struct device *dev,
  214. const struct ulpi_ops *ops)
  215. {
  216. struct ulpi *ulpi;
  217. int ret;
  218. ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
  219. if (!ulpi)
  220. return ERR_PTR(-ENOMEM);
  221. ulpi->ops = ops;
  222. ret = ulpi_register(dev, ulpi);
  223. if (ret) {
  224. kfree(ulpi);
  225. return ERR_PTR(ret);
  226. }
  227. return ulpi;
  228. }
  229. EXPORT_SYMBOL_GPL(ulpi_register_interface);
  230. /**
  231. * ulpi_unregister_interface - unregister ULPI interface
  232. * @intrf: struct ulpi_interface
  233. *
  234. * Unregisters a ULPI device and it's interface that was created with
  235. * ulpi_create_interface().
  236. */
  237. void ulpi_unregister_interface(struct ulpi *ulpi)
  238. {
  239. of_node_put(ulpi->dev.of_node);
  240. device_unregister(&ulpi->dev);
  241. }
  242. EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
  243. /* -------------------------------------------------------------------------- */
  244. static int __init ulpi_init(void)
  245. {
  246. return bus_register(&ulpi_bus);
  247. }
  248. subsys_initcall(ulpi_init);
  249. static void __exit ulpi_exit(void)
  250. {
  251. bus_unregister(&ulpi_bus);
  252. }
  253. module_exit(ulpi_exit);
  254. MODULE_AUTHOR("Intel Corporation");
  255. MODULE_LICENSE("GPL v2");
  256. MODULE_DESCRIPTION("USB ULPI PHY bus");