ulpi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <[email protected]>
  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/debugfs.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/clk/clk-conf.h>
  19. /* -------------------------------------------------------------------------- */
  20. int ulpi_read(struct ulpi *ulpi, u8 addr)
  21. {
  22. return ulpi->ops->read(ulpi->dev.parent, addr);
  23. }
  24. EXPORT_SYMBOL_GPL(ulpi_read);
  25. int ulpi_write(struct ulpi *ulpi, u8 addr, u8 val)
  26. {
  27. return ulpi->ops->write(ulpi->dev.parent, addr, val);
  28. }
  29. EXPORT_SYMBOL_GPL(ulpi_write);
  30. /* -------------------------------------------------------------------------- */
  31. static int ulpi_match(struct device *dev, struct device_driver *driver)
  32. {
  33. struct ulpi_driver *drv = to_ulpi_driver(driver);
  34. struct ulpi *ulpi = to_ulpi_dev(dev);
  35. const struct ulpi_device_id *id;
  36. /*
  37. * Some ULPI devices don't have a vendor id
  38. * or provide an id_table so rely on OF match.
  39. */
  40. if (ulpi->id.vendor == 0 || !drv->id_table)
  41. return of_driver_match_device(dev, driver);
  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. return 0;
  47. }
  48. static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env)
  49. {
  50. struct ulpi *ulpi = to_ulpi_dev(dev);
  51. int ret;
  52. ret = of_device_uevent_modalias(dev, env);
  53. if (ret != -ENODEV)
  54. return ret;
  55. if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x",
  56. ulpi->id.vendor, ulpi->id.product))
  57. return -ENOMEM;
  58. return 0;
  59. }
  60. static int ulpi_probe(struct device *dev)
  61. {
  62. struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
  63. int ret;
  64. ret = of_clk_set_defaults(dev->of_node, false);
  65. if (ret < 0)
  66. return ret;
  67. return drv->probe(to_ulpi_dev(dev));
  68. }
  69. static void ulpi_remove(struct device *dev)
  70. {
  71. struct ulpi_driver *drv = to_ulpi_driver(dev->driver);
  72. if (drv->remove)
  73. drv->remove(to_ulpi_dev(dev));
  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 const 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_regs_show(struct seq_file *seq, void *data)
  190. {
  191. struct ulpi *ulpi = seq->private;
  192. #define ulpi_print(name, reg) do { \
  193. int ret = ulpi_read(ulpi, reg); \
  194. if (ret < 0) \
  195. return ret; \
  196. seq_printf(seq, name " %.02x\n", ret); \
  197. } while (0)
  198. ulpi_print("Vendor ID Low ", ULPI_VENDOR_ID_LOW);
  199. ulpi_print("Vendor ID High ", ULPI_VENDOR_ID_HIGH);
  200. ulpi_print("Product ID Low ", ULPI_PRODUCT_ID_LOW);
  201. ulpi_print("Product ID High ", ULPI_PRODUCT_ID_HIGH);
  202. ulpi_print("Function Control ", ULPI_FUNC_CTRL);
  203. ulpi_print("Interface Control ", ULPI_IFC_CTRL);
  204. ulpi_print("OTG Control ", ULPI_OTG_CTRL);
  205. ulpi_print("USB Interrupt Enable Rising ", ULPI_USB_INT_EN_RISE);
  206. ulpi_print("USB Interrupt Enable Falling", ULPI_USB_INT_EN_FALL);
  207. ulpi_print("USB Interrupt Status ", ULPI_USB_INT_STS);
  208. ulpi_print("USB Interrupt Latch ", ULPI_USB_INT_LATCH);
  209. ulpi_print("Debug ", ULPI_DEBUG);
  210. ulpi_print("Scratch Register ", ULPI_SCRATCH);
  211. ulpi_print("Carkit Control ", ULPI_CARKIT_CTRL);
  212. ulpi_print("Carkit Interrupt Delay ", ULPI_CARKIT_INT_DELAY);
  213. ulpi_print("Carkit Interrupt Enable ", ULPI_CARKIT_INT_EN);
  214. ulpi_print("Carkit Interrupt Status ", ULPI_CARKIT_INT_STS);
  215. ulpi_print("Carkit Interrupt Latch ", ULPI_CARKIT_INT_LATCH);
  216. ulpi_print("Carkit Pulse Control ", ULPI_CARKIT_PLS_CTRL);
  217. ulpi_print("Transmit Positive Width ", ULPI_TX_POS_WIDTH);
  218. ulpi_print("Transmit Negative Width ", ULPI_TX_NEG_WIDTH);
  219. ulpi_print("Receive Polarity Recovery ", ULPI_POLARITY_RECOVERY);
  220. return 0;
  221. }
  222. DEFINE_SHOW_ATTRIBUTE(ulpi_regs);
  223. static struct dentry *ulpi_root;
  224. static int ulpi_register(struct device *dev, struct ulpi *ulpi)
  225. {
  226. int ret;
  227. struct dentry *root;
  228. ulpi->dev.parent = dev; /* needed early for ops */
  229. ulpi->dev.bus = &ulpi_bus;
  230. ulpi->dev.type = &ulpi_dev_type;
  231. dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev));
  232. ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev));
  233. ret = ulpi_of_register(ulpi);
  234. if (ret)
  235. return ret;
  236. ret = ulpi_read_id(ulpi);
  237. if (ret) {
  238. of_node_put(ulpi->dev.of_node);
  239. return ret;
  240. }
  241. ret = device_register(&ulpi->dev);
  242. if (ret) {
  243. put_device(&ulpi->dev);
  244. return ret;
  245. }
  246. root = debugfs_create_dir(dev_name(dev), ulpi_root);
  247. debugfs_create_file("regs", 0444, root, ulpi, &ulpi_regs_fops);
  248. dev_dbg(&ulpi->dev, "registered ULPI PHY: vendor %04x, product %04x\n",
  249. ulpi->id.vendor, ulpi->id.product);
  250. return 0;
  251. }
  252. /**
  253. * ulpi_register_interface - instantiate new ULPI device
  254. * @dev: USB controller's device interface
  255. * @ops: ULPI register access
  256. *
  257. * Allocates and registers a ULPI device and an interface for it. Called from
  258. * the USB controller that provides the ULPI interface.
  259. */
  260. struct ulpi *ulpi_register_interface(struct device *dev,
  261. const struct ulpi_ops *ops)
  262. {
  263. struct ulpi *ulpi;
  264. int ret;
  265. ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
  266. if (!ulpi)
  267. return ERR_PTR(-ENOMEM);
  268. ulpi->ops = ops;
  269. ret = ulpi_register(dev, ulpi);
  270. if (ret) {
  271. kfree(ulpi);
  272. return ERR_PTR(ret);
  273. }
  274. return ulpi;
  275. }
  276. EXPORT_SYMBOL_GPL(ulpi_register_interface);
  277. /**
  278. * ulpi_unregister_interface - unregister ULPI interface
  279. * @ulpi: struct ulpi_interface
  280. *
  281. * Unregisters a ULPI device and it's interface that was created with
  282. * ulpi_create_interface().
  283. */
  284. void ulpi_unregister_interface(struct ulpi *ulpi)
  285. {
  286. debugfs_lookup_and_remove(dev_name(&ulpi->dev), ulpi_root);
  287. device_unregister(&ulpi->dev);
  288. }
  289. EXPORT_SYMBOL_GPL(ulpi_unregister_interface);
  290. /* -------------------------------------------------------------------------- */
  291. static int __init ulpi_init(void)
  292. {
  293. int ret;
  294. ulpi_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  295. ret = bus_register(&ulpi_bus);
  296. if (ret)
  297. debugfs_remove(ulpi_root);
  298. return ret;
  299. }
  300. subsys_initcall(ulpi_init);
  301. static void __exit ulpi_exit(void)
  302. {
  303. bus_unregister(&ulpi_bus);
  304. debugfs_remove(ulpi_root);
  305. }
  306. module_exit(ulpi_exit);
  307. MODULE_AUTHOR("Intel Corporation");
  308. MODULE_LICENSE("GPL v2");
  309. MODULE_DESCRIPTION("USB ULPI PHY bus");