usbpd.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #ifndef __LINUX_USB_USBPD_H
  6. #define __LINUX_USB_USBPD_H
  7. #include <linux/list.h>
  8. struct usbpd;
  9. /* Standard IDs */
  10. #define USBPD_SID 0xff00
  11. /* Structured VDM Command Type */
  12. enum usbpd_svdm_cmd_type {
  13. SVDM_CMD_TYPE_INITIATOR,
  14. SVDM_CMD_TYPE_RESP_ACK,
  15. SVDM_CMD_TYPE_RESP_NAK,
  16. SVDM_CMD_TYPE_RESP_BUSY,
  17. };
  18. /* Structured VDM Commands */
  19. #define USBPD_SVDM_DISCOVER_IDENTITY 0x1
  20. #define USBPD_SVDM_DISCOVER_SVIDS 0x2
  21. #define USBPD_SVDM_DISCOVER_MODES 0x3
  22. #define USBPD_SVDM_ENTER_MODE 0x4
  23. #define USBPD_SVDM_EXIT_MODE 0x5
  24. #define USBPD_SVDM_ATTENTION 0x6
  25. /*
  26. * Implemented by client
  27. */
  28. struct usbpd_svid_handler {
  29. u16 svid;
  30. /* Notified when VDM session established/reset; must be implemented */
  31. void (*connect)(struct usbpd_svid_handler *hdlr,
  32. bool supports_usb_comm);
  33. void (*disconnect)(struct usbpd_svid_handler *hdlr);
  34. /* DP driver -> PE driver for requesting USB SS lanes */
  35. int (*request_usb_ss_lane)(struct usbpd *pd,
  36. struct usbpd_svid_handler *hdlr);
  37. /* Unstructured VDM */
  38. void (*vdm_received)(struct usbpd_svid_handler *hdlr, u32 vdm_hdr,
  39. const u32 *vdos, int num_vdos);
  40. /* Structured VDM */
  41. void (*svdm_received)(struct usbpd_svid_handler *hdlr, u8 cmd,
  42. enum usbpd_svdm_cmd_type cmd_type, const u32 *vdos,
  43. int num_vdos);
  44. /* client should leave these blank; private members used by PD driver */
  45. struct list_head entry;
  46. bool discovered;
  47. };
  48. enum plug_orientation {
  49. ORIENTATION_NONE,
  50. ORIENTATION_CC1,
  51. ORIENTATION_CC2,
  52. };
  53. #if IS_ENABLED(CONFIG_USB_PD_POLICY)
  54. /*
  55. * Obtains an instance of usbpd from a DT phandle
  56. */
  57. struct usbpd *devm_usbpd_get_by_phandle(struct device *dev,
  58. const char *phandle);
  59. /*
  60. * Called by client to handle specific SVID messages.
  61. * Specify callback functions in the usbpd_svid_handler argument
  62. */
  63. int usbpd_register_svid(struct usbpd *pd, struct usbpd_svid_handler *hdlr);
  64. void usbpd_unregister_svid(struct usbpd *pd, struct usbpd_svid_handler *hdlr);
  65. /*
  66. * Transmit a VDM message.
  67. */
  68. int usbpd_send_vdm(struct usbpd *pd, u32 vdm_hdr, const u32 *vdos,
  69. int num_vdos);
  70. /*
  71. * Transmit a Structured VDM message.
  72. */
  73. int usbpd_send_svdm(struct usbpd *pd, u16 svid, u8 cmd,
  74. enum usbpd_svdm_cmd_type cmd_type, int obj_pos,
  75. const u32 *vdos, int num_vdos);
  76. /*
  77. * Get current status of CC pin orientation.
  78. *
  79. * Return: ORIENTATION_CC1 or ORIENTATION_CC2 if attached,
  80. * otherwise ORIENTATION_NONE if not attached
  81. */
  82. enum plug_orientation usbpd_get_plug_orientation(struct usbpd *pd);
  83. void usbpd_vdm_in_suspend(struct usbpd *pd, bool in_suspend);
  84. #else
  85. static inline struct usbpd *devm_usbpd_get_by_phandle(struct device *dev,
  86. const char *phandle)
  87. {
  88. return ERR_PTR(-ENODEV);
  89. }
  90. static inline int usbpd_register_svid(struct usbpd *pd,
  91. struct usbpd_svid_handler *hdlr)
  92. {
  93. return -EINVAL;
  94. }
  95. static inline void usbpd_unregister_svid(struct usbpd *pd,
  96. struct usbpd_svid_handler *hdlr)
  97. {
  98. }
  99. static inline int usbpd_send_vdm(struct usbpd *pd, u32 vdm_hdr, const u32 *vdos,
  100. int num_vdos)
  101. {
  102. return -EINVAL;
  103. }
  104. static inline int usbpd_send_svdm(struct usbpd *pd, u16 svid, u8 cmd,
  105. enum usbpd_svdm_cmd_type cmd_type, int obj_pos,
  106. const u32 *vdos, int num_vdos)
  107. {
  108. return -EINVAL;
  109. }
  110. static inline enum plug_orientation usbpd_get_plug_orientation(struct usbpd *pd)
  111. {
  112. return ORIENTATION_NONE;
  113. }
  114. static inline void usbpd_vdm_in_suspend(struct usbpd *pd, bool in_suspend) { }
  115. #endif /* IS_ENABLED(CONFIG_USB_PD_POLICY) */
  116. /*
  117. * Additional helpers for Enter/Exit Mode commands
  118. */
  119. static inline int usbpd_enter_mode(struct usbpd *pd, u16 svid, int mode,
  120. const u32 *vdo)
  121. {
  122. return usbpd_send_svdm(pd, svid, USBPD_SVDM_ENTER_MODE,
  123. SVDM_CMD_TYPE_INITIATOR, mode, vdo, vdo ? 1 : 0);
  124. }
  125. static inline int usbpd_exit_mode(struct usbpd *pd, u16 svid, int mode,
  126. const u32 *vdo)
  127. {
  128. return usbpd_send_svdm(pd, svid, USBPD_SVDM_EXIT_MODE,
  129. SVDM_CMD_TYPE_INITIATOR, mode, vdo, vdo ? 1 : 0);
  130. }
  131. #endif /* __LINUX_USB_USBPD_H */