drm_mipi_dbi.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * MIPI Display Bus Interface (DBI) LCD controller support
  4. *
  5. * Copyright 2016 Noralf Trønnes
  6. */
  7. #ifndef __LINUX_MIPI_DBI_H
  8. #define __LINUX_MIPI_DBI_H
  9. #include <linux/mutex.h>
  10. #include <drm/drm_device.h>
  11. #include <drm/drm_simple_kms_helper.h>
  12. struct drm_format_conv_state;
  13. struct drm_rect;
  14. struct gpio_desc;
  15. struct iosys_map;
  16. struct regulator;
  17. struct spi_device;
  18. /**
  19. * struct mipi_dbi - MIPI DBI interface
  20. */
  21. struct mipi_dbi {
  22. /**
  23. * @cmdlock: Command lock
  24. */
  25. struct mutex cmdlock;
  26. /**
  27. * @command: Bus specific callback executing commands.
  28. */
  29. int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num);
  30. /**
  31. * @read_commands: Array of read commands terminated by a zero entry.
  32. * Reading is disabled if this is NULL.
  33. */
  34. const u8 *read_commands;
  35. /**
  36. * @swap_bytes: Swap bytes in buffer before transfer
  37. */
  38. bool swap_bytes;
  39. /**
  40. * @reset: Optional reset gpio
  41. */
  42. struct gpio_desc *reset;
  43. /* Type C specific */
  44. /**
  45. * @spi: SPI device
  46. */
  47. struct spi_device *spi;
  48. /**
  49. * @write_memory_bpw: Bits per word used on a MIPI_DCS_WRITE_MEMORY_START transfer
  50. */
  51. unsigned int write_memory_bpw;
  52. /**
  53. * @dc: Optional D/C gpio.
  54. */
  55. struct gpio_desc *dc;
  56. /**
  57. * @tx_buf9: Buffer used for Option 1 9-bit conversion
  58. */
  59. void *tx_buf9;
  60. /**
  61. * @tx_buf9_len: Size of tx_buf9.
  62. */
  63. size_t tx_buf9_len;
  64. };
  65. /**
  66. * struct mipi_dbi_dev - MIPI DBI device
  67. */
  68. struct mipi_dbi_dev {
  69. /**
  70. * @drm: DRM device
  71. */
  72. struct drm_device drm;
  73. /**
  74. * @pipe: Display pipe structure
  75. */
  76. struct drm_simple_display_pipe pipe;
  77. /**
  78. * @connector: Connector
  79. */
  80. struct drm_connector connector;
  81. /**
  82. * @mode: Fixed display mode
  83. */
  84. struct drm_display_mode mode;
  85. /**
  86. * @pixel_format: Native pixel format (DRM_FORMAT\_\*)
  87. */
  88. u32 pixel_format;
  89. /**
  90. * @tx_buf: Buffer used for transfer (copy clip rect area)
  91. */
  92. u16 *tx_buf;
  93. /**
  94. * @rotation: initial rotation in degrees Counter Clock Wise
  95. */
  96. unsigned int rotation;
  97. /**
  98. * @left_offset: Horizontal offset of the display relative to the
  99. * controller's driver array
  100. */
  101. unsigned int left_offset;
  102. /**
  103. * @top_offset: Vertical offset of the display relative to the
  104. * controller's driver array
  105. */
  106. unsigned int top_offset;
  107. /**
  108. * @backlight: backlight device (optional)
  109. */
  110. struct backlight_device *backlight;
  111. /**
  112. * @regulator: power regulator (Vdd) (optional)
  113. */
  114. struct regulator *regulator;
  115. /**
  116. * @io_regulator: I/O power regulator (Vddi) (optional)
  117. */
  118. struct regulator *io_regulator;
  119. /**
  120. * @dbi: MIPI DBI interface
  121. */
  122. struct mipi_dbi dbi;
  123. /**
  124. * @driver_private: Driver private data.
  125. * Necessary for drivers with private data since devm_drm_dev_alloc()
  126. * can't allocate structures that embed a structure which then again
  127. * embeds drm_device.
  128. */
  129. void *driver_private;
  130. };
  131. static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm)
  132. {
  133. return container_of(drm, struct mipi_dbi_dev, drm);
  134. }
  135. int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi,
  136. struct gpio_desc *dc);
  137. int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev,
  138. const struct drm_simple_display_pipe_funcs *funcs,
  139. const uint32_t *formats, unsigned int format_count,
  140. const struct drm_display_mode *mode,
  141. unsigned int rotation, size_t tx_buf_size);
  142. int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev,
  143. const struct drm_simple_display_pipe_funcs *funcs,
  144. const struct drm_display_mode *mode, unsigned int rotation);
  145. enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
  146. const struct drm_display_mode *mode);
  147. void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe,
  148. struct drm_plane_state *old_state);
  149. void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev,
  150. struct drm_crtc_state *crtc_state,
  151. struct drm_plane_state *plan_state);
  152. void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe);
  153. int mipi_dbi_pipe_begin_fb_access(struct drm_simple_display_pipe *pipe,
  154. struct drm_plane_state *plane_state);
  155. void mipi_dbi_pipe_end_fb_access(struct drm_simple_display_pipe *pipe,
  156. struct drm_plane_state *plane_state);
  157. void mipi_dbi_pipe_reset_plane(struct drm_simple_display_pipe *pipe);
  158. struct drm_plane_state *mipi_dbi_pipe_duplicate_plane_state(struct drm_simple_display_pipe *pipe);
  159. void mipi_dbi_pipe_destroy_plane_state(struct drm_simple_display_pipe *pipe,
  160. struct drm_plane_state *plane_state);
  161. void mipi_dbi_hw_reset(struct mipi_dbi *dbi);
  162. bool mipi_dbi_display_is_on(struct mipi_dbi *dbi);
  163. int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev);
  164. int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev);
  165. u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len);
  166. int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
  167. u8 bpw, const void *buf, size_t len);
  168. int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val);
  169. int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len);
  170. int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data,
  171. size_t len);
  172. int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb,
  173. struct drm_rect *clip, bool swap,
  174. struct drm_format_conv_state *fmtcnv_state);
  175. /**
  176. * mipi_dbi_command - MIPI DCS command with optional parameter(s)
  177. * @dbi: MIPI DBI structure
  178. * @cmd: Command
  179. * @seq: Optional parameter(s)
  180. *
  181. * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for
  182. * get/read.
  183. *
  184. * Returns:
  185. * Zero on success, negative error code on failure.
  186. */
  187. #define mipi_dbi_command(dbi, cmd, seq...) \
  188. ({ \
  189. const u8 d[] = { seq }; \
  190. struct device *dev = &(dbi)->spi->dev; \
  191. int ret; \
  192. ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \
  193. if (ret) \
  194. dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \
  195. ret; \
  196. })
  197. #ifdef CONFIG_DEBUG_FS
  198. void mipi_dbi_debugfs_init(struct drm_minor *minor);
  199. #else
  200. static inline void mipi_dbi_debugfs_init(struct drm_minor *minor) {}
  201. #endif
  202. /**
  203. * DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS - Initializes struct drm_simple_display_pipe_funcs
  204. * for MIPI-DBI devices
  205. * @enable_: Enable-callback implementation
  206. *
  207. * This macro initializes struct drm_simple_display_pipe_funcs with default
  208. * values for MIPI-DBI-based devices. The only callback that depends on the
  209. * hardware is @enable, for which the driver has to provide an implementation.
  210. * MIPI-based drivers are encouraged to use this macro for initialization.
  211. */
  212. #define DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(enable_) \
  213. .mode_valid = mipi_dbi_pipe_mode_valid, \
  214. .enable = (enable_), \
  215. .disable = mipi_dbi_pipe_disable, \
  216. .update = mipi_dbi_pipe_update, \
  217. .begin_fb_access = mipi_dbi_pipe_begin_fb_access, \
  218. .end_fb_access = mipi_dbi_pipe_end_fb_access, \
  219. .reset_plane = mipi_dbi_pipe_reset_plane, \
  220. .duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state, \
  221. .destroy_plane_state = mipi_dbi_pipe_destroy_plane_state
  222. #endif /* __LINUX_MIPI_DBI_H */