qcom_eud.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/err.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/usb/role.h>
  17. #define EUD_REG_INT1_EN_MASK 0x0024
  18. #define EUD_REG_INT_STATUS_1 0x0044
  19. #define EUD_REG_CTL_OUT_1 0x0074
  20. #define EUD_REG_VBUS_INT_CLR 0x0080
  21. #define EUD_REG_CSR_EUD_EN 0x1014
  22. #define EUD_REG_SW_ATTACH_DET 0x1018
  23. #define EUD_REG_EUD_EN2 0x0000
  24. #define EUD_ENABLE BIT(0)
  25. #define EUD_INT_PET_EUD BIT(0)
  26. #define EUD_INT_VBUS BIT(2)
  27. #define EUD_INT_SAFE_MODE BIT(4)
  28. #define EUD_INT_ALL (EUD_INT_VBUS | EUD_INT_SAFE_MODE)
  29. struct eud_chip {
  30. struct device *dev;
  31. struct usb_role_switch *role_sw;
  32. void __iomem *base;
  33. void __iomem *mode_mgr;
  34. unsigned int int_status;
  35. int irq;
  36. bool enabled;
  37. bool usb_attached;
  38. };
  39. static int enable_eud(struct eud_chip *priv)
  40. {
  41. writel(EUD_ENABLE, priv->base + EUD_REG_CSR_EUD_EN);
  42. writel(EUD_INT_VBUS | EUD_INT_SAFE_MODE,
  43. priv->base + EUD_REG_INT1_EN_MASK);
  44. writel(1, priv->mode_mgr + EUD_REG_EUD_EN2);
  45. return usb_role_switch_set_role(priv->role_sw, USB_ROLE_DEVICE);
  46. }
  47. static void disable_eud(struct eud_chip *priv)
  48. {
  49. writel(0, priv->base + EUD_REG_CSR_EUD_EN);
  50. writel(0, priv->mode_mgr + EUD_REG_EUD_EN2);
  51. }
  52. static ssize_t enable_show(struct device *dev,
  53. struct device_attribute *attr, char *buf)
  54. {
  55. struct eud_chip *chip = dev_get_drvdata(dev);
  56. return sysfs_emit(buf, "%d\n", chip->enabled);
  57. }
  58. static ssize_t enable_store(struct device *dev,
  59. struct device_attribute *attr,
  60. const char *buf, size_t count)
  61. {
  62. struct eud_chip *chip = dev_get_drvdata(dev);
  63. bool enable;
  64. int ret;
  65. if (kstrtobool(buf, &enable))
  66. return -EINVAL;
  67. if (enable) {
  68. ret = enable_eud(chip);
  69. if (!ret)
  70. chip->enabled = enable;
  71. else
  72. disable_eud(chip);
  73. } else {
  74. disable_eud(chip);
  75. }
  76. return count;
  77. }
  78. static DEVICE_ATTR_RW(enable);
  79. static struct attribute *eud_attrs[] = {
  80. &dev_attr_enable.attr,
  81. NULL,
  82. };
  83. ATTRIBUTE_GROUPS(eud);
  84. static void usb_attach_detach(struct eud_chip *chip)
  85. {
  86. u32 reg;
  87. /* read ctl_out_1[4] to find USB attach or detach event */
  88. reg = readl(chip->base + EUD_REG_CTL_OUT_1);
  89. chip->usb_attached = reg & EUD_INT_SAFE_MODE;
  90. }
  91. static void pet_eud(struct eud_chip *chip)
  92. {
  93. u32 reg;
  94. int ret;
  95. /* When the EUD_INT_PET_EUD in SW_ATTACH_DET is set, the cable has been
  96. * disconnected and we need to detach the pet to check if EUD is in safe
  97. * mode before attaching again.
  98. */
  99. reg = readl(chip->base + EUD_REG_SW_ATTACH_DET);
  100. if (reg & EUD_INT_PET_EUD) {
  101. /* Detach & Attach pet for EUD */
  102. writel(0, chip->base + EUD_REG_SW_ATTACH_DET);
  103. /* Delay to make sure detach pet is done before attach pet */
  104. ret = readl_poll_timeout(chip->base + EUD_REG_SW_ATTACH_DET,
  105. reg, (reg == 0), 1, 100);
  106. if (ret) {
  107. dev_err(chip->dev, "Detach pet failed\n");
  108. return;
  109. }
  110. }
  111. /* Attach pet for EUD */
  112. writel(EUD_INT_PET_EUD, chip->base + EUD_REG_SW_ATTACH_DET);
  113. }
  114. static irqreturn_t handle_eud_irq(int irq, void *data)
  115. {
  116. struct eud_chip *chip = data;
  117. u32 reg;
  118. reg = readl(chip->base + EUD_REG_INT_STATUS_1);
  119. switch (reg & EUD_INT_ALL) {
  120. case EUD_INT_VBUS:
  121. usb_attach_detach(chip);
  122. return IRQ_WAKE_THREAD;
  123. case EUD_INT_SAFE_MODE:
  124. pet_eud(chip);
  125. return IRQ_HANDLED;
  126. default:
  127. return IRQ_NONE;
  128. }
  129. }
  130. static irqreturn_t handle_eud_irq_thread(int irq, void *data)
  131. {
  132. struct eud_chip *chip = data;
  133. int ret;
  134. if (chip->usb_attached)
  135. ret = usb_role_switch_set_role(chip->role_sw, USB_ROLE_DEVICE);
  136. else
  137. ret = usb_role_switch_set_role(chip->role_sw, USB_ROLE_HOST);
  138. if (ret)
  139. dev_err(chip->dev, "failed to set role switch\n");
  140. /* set and clear vbus_int_clr[0] to clear interrupt */
  141. writel(BIT(0), chip->base + EUD_REG_VBUS_INT_CLR);
  142. writel(0, chip->base + EUD_REG_VBUS_INT_CLR);
  143. return IRQ_HANDLED;
  144. }
  145. static void eud_role_switch_release(void *data)
  146. {
  147. struct eud_chip *chip = data;
  148. usb_role_switch_put(chip->role_sw);
  149. }
  150. static int eud_probe(struct platform_device *pdev)
  151. {
  152. struct eud_chip *chip;
  153. int ret;
  154. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  155. if (!chip)
  156. return -ENOMEM;
  157. chip->dev = &pdev->dev;
  158. chip->role_sw = usb_role_switch_get(&pdev->dev);
  159. if (IS_ERR(chip->role_sw))
  160. return dev_err_probe(chip->dev, PTR_ERR(chip->role_sw),
  161. "failed to get role switch\n");
  162. ret = devm_add_action_or_reset(chip->dev, eud_role_switch_release, chip);
  163. if (ret)
  164. return dev_err_probe(chip->dev, ret,
  165. "failed to add role switch release action\n");
  166. chip->base = devm_platform_ioremap_resource(pdev, 0);
  167. if (IS_ERR(chip->base))
  168. return PTR_ERR(chip->base);
  169. chip->mode_mgr = devm_platform_ioremap_resource(pdev, 1);
  170. if (IS_ERR(chip->mode_mgr))
  171. return PTR_ERR(chip->mode_mgr);
  172. chip->irq = platform_get_irq(pdev, 0);
  173. ret = devm_request_threaded_irq(&pdev->dev, chip->irq, handle_eud_irq,
  174. handle_eud_irq_thread, IRQF_ONESHOT, NULL, chip);
  175. if (ret)
  176. return dev_err_probe(chip->dev, ret, "failed to allocate irq\n");
  177. enable_irq_wake(chip->irq);
  178. platform_set_drvdata(pdev, chip);
  179. return 0;
  180. }
  181. static int eud_remove(struct platform_device *pdev)
  182. {
  183. struct eud_chip *chip = platform_get_drvdata(pdev);
  184. if (chip->enabled)
  185. disable_eud(chip);
  186. device_init_wakeup(&pdev->dev, false);
  187. disable_irq_wake(chip->irq);
  188. return 0;
  189. }
  190. static const struct of_device_id eud_dt_match[] = {
  191. { .compatible = "qcom,sc7280-eud" },
  192. { }
  193. };
  194. MODULE_DEVICE_TABLE(of, eud_dt_match);
  195. static struct platform_driver eud_driver = {
  196. .probe = eud_probe,
  197. .remove = eud_remove,
  198. .driver = {
  199. .name = "qcom_eud",
  200. .dev_groups = eud_groups,
  201. .of_match_table = eud_dt_match,
  202. },
  203. };
  204. module_platform_driver(eud_driver);
  205. MODULE_DESCRIPTION("QTI EUD driver");
  206. MODULE_LICENSE("GPL v2");