drm_mipi_dsi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * MIPI DSI Bus
  4. *
  5. * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
  6. * Andrzej Hajda <[email protected]>
  7. */
  8. #ifndef __DRM_MIPI_DSI_H__
  9. #define __DRM_MIPI_DSI_H__
  10. #include <linux/device.h>
  11. struct mipi_dsi_host;
  12. struct mipi_dsi_device;
  13. struct drm_dsc_picture_parameter_set;
  14. /* request ACK from peripheral */
  15. #define MIPI_DSI_MSG_REQ_ACK BIT(0)
  16. /* use Low Power Mode to transmit message */
  17. #define MIPI_DSI_MSG_USE_LPM BIT(1)
  18. /**
  19. * struct mipi_dsi_msg - read/write DSI buffer
  20. * @channel: virtual channel id
  21. * @type: payload data type
  22. * @flags: flags controlling this message transmission
  23. * @tx_len: length of @tx_buf
  24. * @tx_buf: data to be written
  25. * @rx_len: length of @rx_buf
  26. * @rx_buf: data to be read, or NULL
  27. */
  28. struct mipi_dsi_msg {
  29. u8 channel;
  30. u8 type;
  31. u16 flags;
  32. size_t tx_len;
  33. const void *tx_buf;
  34. size_t rx_len;
  35. void *rx_buf;
  36. };
  37. bool mipi_dsi_packet_format_is_short(u8 type);
  38. bool mipi_dsi_packet_format_is_long(u8 type);
  39. /**
  40. * struct mipi_dsi_packet - represents a MIPI DSI packet in protocol format
  41. * @size: size (in bytes) of the packet
  42. * @header: the four bytes that make up the header (Data ID, Word Count or
  43. * Packet Data, and ECC)
  44. * @payload_length: number of bytes in the payload
  45. * @payload: a pointer to a buffer containing the payload, if any
  46. */
  47. struct mipi_dsi_packet {
  48. size_t size;
  49. u8 header[4];
  50. size_t payload_length;
  51. const u8 *payload;
  52. };
  53. int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
  54. const struct mipi_dsi_msg *msg);
  55. /**
  56. * struct mipi_dsi_host_ops - DSI bus operations
  57. * @attach: attach DSI device to DSI host
  58. * @detach: detach DSI device from DSI host
  59. * @transfer: transmit a DSI packet
  60. *
  61. * DSI packets transmitted by .transfer() are passed in as mipi_dsi_msg
  62. * structures. This structure contains information about the type of packet
  63. * being transmitted as well as the transmit and receive buffers. When an
  64. * error is encountered during transmission, this function will return a
  65. * negative error code. On success it shall return the number of bytes
  66. * transmitted for write packets or the number of bytes received for read
  67. * packets.
  68. *
  69. * Note that typically DSI packet transmission is atomic, so the .transfer()
  70. * function will seldomly return anything other than the number of bytes
  71. * contained in the transmit buffer on success.
  72. *
  73. * Also note that those callbacks can be called no matter the state the
  74. * host is in. Drivers that need the underlying device to be powered to
  75. * perform these operations will first need to make sure it's been
  76. * properly enabled.
  77. */
  78. struct mipi_dsi_host_ops {
  79. int (*attach)(struct mipi_dsi_host *host,
  80. struct mipi_dsi_device *dsi);
  81. int (*detach)(struct mipi_dsi_host *host,
  82. struct mipi_dsi_device *dsi);
  83. ssize_t (*transfer)(struct mipi_dsi_host *host,
  84. const struct mipi_dsi_msg *msg);
  85. };
  86. /**
  87. * struct mipi_dsi_host - DSI host device
  88. * @dev: driver model device node for this DSI host
  89. * @ops: DSI host operations
  90. * @list: list management
  91. */
  92. struct mipi_dsi_host {
  93. struct device *dev;
  94. const struct mipi_dsi_host_ops *ops;
  95. struct list_head list;
  96. };
  97. int mipi_dsi_host_register(struct mipi_dsi_host *host);
  98. void mipi_dsi_host_unregister(struct mipi_dsi_host *host);
  99. struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node);
  100. /* DSI mode flags */
  101. /* video mode */
  102. #define MIPI_DSI_MODE_VIDEO BIT(0)
  103. /* video burst mode */
  104. #define MIPI_DSI_MODE_VIDEO_BURST BIT(1)
  105. /* video pulse mode */
  106. #define MIPI_DSI_MODE_VIDEO_SYNC_PULSE BIT(2)
  107. /* enable auto vertical count mode */
  108. #define MIPI_DSI_MODE_VIDEO_AUTO_VERT BIT(3)
  109. /* enable hsync-end packets in vsync-pulse and v-porch area */
  110. #define MIPI_DSI_MODE_VIDEO_HSE BIT(4)
  111. /* disable hfront-porch area */
  112. #define MIPI_DSI_MODE_VIDEO_NO_HFP BIT(5)
  113. /* disable hback-porch area */
  114. #define MIPI_DSI_MODE_VIDEO_NO_HBP BIT(6)
  115. /* disable hsync-active area */
  116. #define MIPI_DSI_MODE_VIDEO_NO_HSA BIT(7)
  117. /* flush display FIFO on vsync pulse */
  118. #define MIPI_DSI_MODE_VSYNC_FLUSH BIT(8)
  119. /* disable EoT packets in HS mode */
  120. #define MIPI_DSI_MODE_NO_EOT_PACKET BIT(9)
  121. /* device supports non-continuous clock behavior (DSI spec 5.6.1) */
  122. #define MIPI_DSI_CLOCK_NON_CONTINUOUS BIT(10)
  123. /* transmit data in low power */
  124. #define MIPI_DSI_MODE_LPM BIT(11)
  125. /* transmit data ending at the same time for all lanes within one hsync */
  126. #define MIPI_DSI_HS_PKT_END_ALIGNED BIT(12)
  127. enum mipi_dsi_pixel_format {
  128. MIPI_DSI_FMT_RGB888,
  129. MIPI_DSI_FMT_RGB666,
  130. MIPI_DSI_FMT_RGB666_PACKED,
  131. MIPI_DSI_FMT_RGB565,
  132. };
  133. #define DSI_DEV_NAME_SIZE 20
  134. /**
  135. * struct mipi_dsi_device_info - template for creating a mipi_dsi_device
  136. * @type: DSI peripheral chip type
  137. * @channel: DSI virtual channel assigned to peripheral
  138. * @node: pointer to OF device node or NULL
  139. *
  140. * This is populated and passed to mipi_dsi_device_new to create a new
  141. * DSI device
  142. */
  143. struct mipi_dsi_device_info {
  144. char type[DSI_DEV_NAME_SIZE];
  145. u32 channel;
  146. struct device_node *node;
  147. };
  148. /**
  149. * struct mipi_dsi_device - DSI peripheral device
  150. * @host: DSI host for this peripheral
  151. * @dev: driver model device node for this peripheral
  152. * @name: DSI peripheral chip type
  153. * @channel: virtual channel assigned to the peripheral
  154. * @format: pixel format for video mode
  155. * @lanes: number of active data lanes
  156. * @mode_flags: DSI operation mode related flags
  157. * @hs_rate: maximum lane frequency for high speed mode in hertz, this should
  158. * be set to the real limits of the hardware, zero is only accepted for
  159. * legacy drivers
  160. * @lp_rate: maximum lane frequency for low power mode in hertz, this should
  161. * be set to the real limits of the hardware, zero is only accepted for
  162. * legacy drivers
  163. * @dsc: panel/bridge DSC pps payload to be sent
  164. */
  165. struct mipi_dsi_device {
  166. struct mipi_dsi_host *host;
  167. struct device dev;
  168. char name[DSI_DEV_NAME_SIZE];
  169. unsigned int channel;
  170. unsigned int lanes;
  171. enum mipi_dsi_pixel_format format;
  172. unsigned long mode_flags;
  173. unsigned long hs_rate;
  174. unsigned long lp_rate;
  175. struct drm_dsc_config *dsc;
  176. };
  177. #define MIPI_DSI_MODULE_PREFIX "mipi-dsi:"
  178. static inline struct mipi_dsi_device *to_mipi_dsi_device(struct device *dev)
  179. {
  180. return container_of(dev, struct mipi_dsi_device, dev);
  181. }
  182. /**
  183. * mipi_dsi_pixel_format_to_bpp - obtain the number of bits per pixel for any
  184. * given pixel format defined by the MIPI DSI
  185. * specification
  186. * @fmt: MIPI DSI pixel format
  187. *
  188. * Returns: The number of bits per pixel of the given pixel format.
  189. */
  190. static inline int mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)
  191. {
  192. switch (fmt) {
  193. case MIPI_DSI_FMT_RGB888:
  194. case MIPI_DSI_FMT_RGB666:
  195. return 24;
  196. case MIPI_DSI_FMT_RGB666_PACKED:
  197. return 18;
  198. case MIPI_DSI_FMT_RGB565:
  199. return 16;
  200. }
  201. return -EINVAL;
  202. }
  203. struct mipi_dsi_device *
  204. mipi_dsi_device_register_full(struct mipi_dsi_host *host,
  205. const struct mipi_dsi_device_info *info);
  206. void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi);
  207. struct mipi_dsi_device *
  208. devm_mipi_dsi_device_register_full(struct device *dev, struct mipi_dsi_host *host,
  209. const struct mipi_dsi_device_info *info);
  210. struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np);
  211. int mipi_dsi_attach(struct mipi_dsi_device *dsi);
  212. int mipi_dsi_detach(struct mipi_dsi_device *dsi);
  213. int devm_mipi_dsi_attach(struct device *dev, struct mipi_dsi_device *dsi);
  214. int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi);
  215. int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi);
  216. int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
  217. u16 value);
  218. ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable);
  219. ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
  220. const struct drm_dsc_picture_parameter_set *pps);
  221. ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
  222. size_t size);
  223. ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
  224. size_t num_params, void *data, size_t size);
  225. /**
  226. * enum mipi_dsi_dcs_tear_mode - Tearing Effect Output Line mode
  227. * @MIPI_DSI_DCS_TEAR_MODE_VBLANK: the TE output line consists of V-Blanking
  228. * information only
  229. * @MIPI_DSI_DCS_TEAR_MODE_VHBLANK : the TE output line consists of both
  230. * V-Blanking and H-Blanking information
  231. */
  232. enum mipi_dsi_dcs_tear_mode {
  233. MIPI_DSI_DCS_TEAR_MODE_VBLANK,
  234. MIPI_DSI_DCS_TEAR_MODE_VHBLANK,
  235. };
  236. #define MIPI_DSI_DCS_POWER_MODE_DISPLAY (1 << 2)
  237. #define MIPI_DSI_DCS_POWER_MODE_NORMAL (1 << 3)
  238. #define MIPI_DSI_DCS_POWER_MODE_SLEEP (1 << 4)
  239. #define MIPI_DSI_DCS_POWER_MODE_PARTIAL (1 << 5)
  240. #define MIPI_DSI_DCS_POWER_MODE_IDLE (1 << 6)
  241. ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
  242. const void *data, size_t len);
  243. ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
  244. const void *data, size_t len);
  245. ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
  246. size_t len);
  247. int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi);
  248. int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi);
  249. int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode);
  250. int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format);
  251. int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi);
  252. int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi);
  253. int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi);
  254. int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi);
  255. int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
  256. u16 end);
  257. int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
  258. u16 end);
  259. int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi);
  260. int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
  261. enum mipi_dsi_dcs_tear_mode mode);
  262. int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format);
  263. int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline);
  264. int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
  265. u16 brightness);
  266. int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
  267. u16 *brightness);
  268. int mipi_dsi_dcs_set_display_brightness_large(struct mipi_dsi_device *dsi,
  269. u16 brightness);
  270. int mipi_dsi_dcs_get_display_brightness_large(struct mipi_dsi_device *dsi,
  271. u16 *brightness);
  272. /**
  273. * mipi_dsi_dcs_write_seq - transmit a DCS command with payload
  274. * @dsi: DSI peripheral device
  275. * @cmd: Command
  276. * @seq: buffer containing data to be transmitted
  277. */
  278. #define mipi_dsi_dcs_write_seq(dsi, cmd, seq...) do { \
  279. static const u8 d[] = { cmd, seq }; \
  280. struct device *dev = &dsi->dev; \
  281. int ret; \
  282. ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
  283. if (ret < 0) { \
  284. dev_err_ratelimited(dev, "sending command %#02x failed: %d\n", cmd, ret); \
  285. return ret; \
  286. } \
  287. } while (0)
  288. /**
  289. * struct mipi_dsi_driver - DSI driver
  290. * @driver: device driver model driver
  291. * @probe: callback for device binding
  292. * @remove: callback for device unbinding
  293. * @shutdown: called at shutdown time to quiesce the device
  294. */
  295. struct mipi_dsi_driver {
  296. struct device_driver driver;
  297. int(*probe)(struct mipi_dsi_device *dsi);
  298. void (*remove)(struct mipi_dsi_device *dsi);
  299. void (*shutdown)(struct mipi_dsi_device *dsi);
  300. };
  301. static inline struct mipi_dsi_driver *
  302. to_mipi_dsi_driver(struct device_driver *driver)
  303. {
  304. return container_of(driver, struct mipi_dsi_driver, driver);
  305. }
  306. static inline void *mipi_dsi_get_drvdata(const struct mipi_dsi_device *dsi)
  307. {
  308. return dev_get_drvdata(&dsi->dev);
  309. }
  310. static inline void mipi_dsi_set_drvdata(struct mipi_dsi_device *dsi, void *data)
  311. {
  312. dev_set_drvdata(&dsi->dev, data);
  313. }
  314. int mipi_dsi_driver_register_full(struct mipi_dsi_driver *driver,
  315. struct module *owner);
  316. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *driver);
  317. #define mipi_dsi_driver_register(driver) \
  318. mipi_dsi_driver_register_full(driver, THIS_MODULE)
  319. #define module_mipi_dsi_driver(__mipi_dsi_driver) \
  320. module_driver(__mipi_dsi_driver, mipi_dsi_driver_register, \
  321. mipi_dsi_driver_unregister)
  322. #endif /* __DRM_MIPI_DSI__ */