ulpi.c 7.3 KB

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