phy-omap-otg.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OMAP OTG controller driver
  4. *
  5. * Based on code from tahvo-usb.c and isp1301_omap.c drivers.
  6. *
  7. * Copyright (C) 2005-2006 Nokia Corporation
  8. * Copyright (C) 2004 Texas Instruments
  9. * Copyright (C) 2004 David Brownell
  10. */
  11. #include <linux/io.h>
  12. #include <linux/err.h>
  13. #include <linux/extcon.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/platform_data/usb-omap1.h>
  19. struct otg_device {
  20. void __iomem *base;
  21. bool id;
  22. bool vbus;
  23. struct extcon_dev *extcon;
  24. struct notifier_block vbus_nb;
  25. struct notifier_block id_nb;
  26. };
  27. #define OMAP_OTG_CTRL 0x0c
  28. #define OMAP_OTG_ASESSVLD (1 << 20)
  29. #define OMAP_OTG_BSESSEND (1 << 19)
  30. #define OMAP_OTG_BSESSVLD (1 << 18)
  31. #define OMAP_OTG_VBUSVLD (1 << 17)
  32. #define OMAP_OTG_ID (1 << 16)
  33. #define OMAP_OTG_XCEIV_OUTPUTS \
  34. (OMAP_OTG_ASESSVLD | OMAP_OTG_BSESSEND | OMAP_OTG_BSESSVLD | \
  35. OMAP_OTG_VBUSVLD | OMAP_OTG_ID)
  36. static void omap_otg_ctrl(struct otg_device *otg_dev, u32 outputs)
  37. {
  38. u32 l;
  39. l = readl(otg_dev->base + OMAP_OTG_CTRL);
  40. l &= ~OMAP_OTG_XCEIV_OUTPUTS;
  41. l |= outputs;
  42. writel(l, otg_dev->base + OMAP_OTG_CTRL);
  43. }
  44. static void omap_otg_set_mode(struct otg_device *otg_dev)
  45. {
  46. if (!otg_dev->id && otg_dev->vbus)
  47. /* Set B-session valid. */
  48. omap_otg_ctrl(otg_dev, OMAP_OTG_ID | OMAP_OTG_BSESSVLD);
  49. else if (otg_dev->vbus)
  50. /* Set A-session valid. */
  51. omap_otg_ctrl(otg_dev, OMAP_OTG_ASESSVLD);
  52. else if (!otg_dev->id)
  53. /* Set B-session end to indicate no VBUS. */
  54. omap_otg_ctrl(otg_dev, OMAP_OTG_ID | OMAP_OTG_BSESSEND);
  55. }
  56. static int omap_otg_id_notifier(struct notifier_block *nb,
  57. unsigned long event, void *ptr)
  58. {
  59. struct otg_device *otg_dev = container_of(nb, struct otg_device, id_nb);
  60. otg_dev->id = event;
  61. omap_otg_set_mode(otg_dev);
  62. return NOTIFY_DONE;
  63. }
  64. static int omap_otg_vbus_notifier(struct notifier_block *nb,
  65. unsigned long event, void *ptr)
  66. {
  67. struct otg_device *otg_dev = container_of(nb, struct otg_device,
  68. vbus_nb);
  69. otg_dev->vbus = event;
  70. omap_otg_set_mode(otg_dev);
  71. return NOTIFY_DONE;
  72. }
  73. static int omap_otg_probe(struct platform_device *pdev)
  74. {
  75. const struct omap_usb_config *config = pdev->dev.platform_data;
  76. struct otg_device *otg_dev;
  77. struct extcon_dev *extcon;
  78. int ret;
  79. u32 rev;
  80. if (!config || !config->extcon)
  81. return -ENODEV;
  82. extcon = extcon_get_extcon_dev(config->extcon);
  83. if (!extcon)
  84. return -EPROBE_DEFER;
  85. otg_dev = devm_kzalloc(&pdev->dev, sizeof(*otg_dev), GFP_KERNEL);
  86. if (!otg_dev)
  87. return -ENOMEM;
  88. otg_dev->base = devm_ioremap_resource(&pdev->dev, &pdev->resource[0]);
  89. if (IS_ERR(otg_dev->base))
  90. return PTR_ERR(otg_dev->base);
  91. otg_dev->extcon = extcon;
  92. otg_dev->id_nb.notifier_call = omap_otg_id_notifier;
  93. otg_dev->vbus_nb.notifier_call = omap_otg_vbus_notifier;
  94. ret = devm_extcon_register_notifier(&pdev->dev, extcon,
  95. EXTCON_USB_HOST, &otg_dev->id_nb);
  96. if (ret)
  97. return ret;
  98. ret = devm_extcon_register_notifier(&pdev->dev, extcon,
  99. EXTCON_USB, &otg_dev->vbus_nb);
  100. if (ret) {
  101. return ret;
  102. }
  103. otg_dev->id = extcon_get_state(extcon, EXTCON_USB_HOST);
  104. otg_dev->vbus = extcon_get_state(extcon, EXTCON_USB);
  105. omap_otg_set_mode(otg_dev);
  106. rev = readl(otg_dev->base);
  107. dev_info(&pdev->dev,
  108. "OMAP USB OTG controller rev %d.%d (%s, id=%d, vbus=%d)\n",
  109. (rev >> 4) & 0xf, rev & 0xf, config->extcon, otg_dev->id,
  110. otg_dev->vbus);
  111. platform_set_drvdata(pdev, otg_dev);
  112. return 0;
  113. }
  114. static struct platform_driver omap_otg_driver = {
  115. .probe = omap_otg_probe,
  116. .driver = {
  117. .name = "omap_otg",
  118. },
  119. };
  120. module_platform_driver(omap_otg_driver);
  121. MODULE_DESCRIPTION("OMAP USB OTG controller driver");
  122. MODULE_LICENSE("GPL");
  123. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");