drm_mipi_dsi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 <a.hajda@samsung.com>
  7. */
  8. #ifndef __DRM_MIPI_DSI_H__
  9. #define __DRM_MIPI_DSI_H__
  10. #include <linux/device.h>
  11. #include <linux/delay.h>
  12. struct mipi_dsi_host;
  13. struct mipi_dsi_device;
  14. struct drm_dsc_picture_parameter_set;
  15. /* request ACK from peripheral */
  16. #define MIPI_DSI_MSG_REQ_ACK BIT(0)
  17. /* use Low Power Mode to transmit message */
  18. #define MIPI_DSI_MSG_USE_LPM BIT(1)
  19. /**
  20. * struct mipi_dsi_msg - read/write DSI buffer
  21. * @channel: virtual channel id
  22. * @type: payload data type
  23. * @flags: flags controlling this message transmission
  24. * @tx_len: length of @tx_buf
  25. * @tx_buf: data to be written
  26. * @rx_len: length of @rx_buf
  27. * @rx_buf: data to be read, or NULL
  28. */
  29. struct mipi_dsi_msg {
  30. u8 channel;
  31. u8 type;
  32. u16 flags;
  33. size_t tx_len;
  34. const void *tx_buf;
  35. size_t rx_len;
  36. void *rx_buf;
  37. };
  38. bool mipi_dsi_packet_format_is_short(u8 type);
  39. bool mipi_dsi_packet_format_is_long(u8 type);
  40. /**
  41. * struct mipi_dsi_packet - represents a MIPI DSI packet in protocol format
  42. * @size: size (in bytes) of the packet
  43. * @header: the four bytes that make up the header (Data ID, Word Count or
  44. * Packet Data, and ECC)
  45. * @payload_length: number of bytes in the payload
  46. * @payload: a pointer to a buffer containing the payload, if any
  47. */
  48. struct mipi_dsi_packet {
  49. size_t size;
  50. u8 header[4];
  51. size_t payload_length;
  52. const u8 *payload;
  53. };
  54. int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
  55. const struct mipi_dsi_msg *msg);
  56. /**
  57. * struct mipi_dsi_host_ops - DSI bus operations
  58. * @attach: attach DSI device to DSI host
  59. * @detach: detach DSI device from DSI host
  60. * @transfer: transmit a DSI packet
  61. *
  62. * DSI packets transmitted by .transfer() are passed in as mipi_dsi_msg
  63. * structures. This structure contains information about the type of packet
  64. * being transmitted as well as the transmit and receive buffers. When an
  65. * error is encountered during transmission, this function will return a
  66. * negative error code. On success it shall return the number of bytes
  67. * transmitted for write packets or the number of bytes received for read
  68. * packets.
  69. *
  70. * Note that typically DSI packet transmission is atomic, so the .transfer()
  71. * function will seldomly return anything other than the number of bytes
  72. * contained in the transmit buffer on success.
  73. *
  74. * Also note that those callbacks can be called no matter the state the
  75. * host is in. Drivers that need the underlying device to be powered to
  76. * perform these operations will first need to make sure it's been
  77. * properly enabled.
  78. */
  79. struct mipi_dsi_host_ops {
  80. int (*attach)(struct mipi_dsi_host *host,
  81. struct mipi_dsi_device *dsi);
  82. int (*detach)(struct mipi_dsi_host *host,
  83. struct mipi_dsi_device *dsi);
  84. ssize_t (*transfer)(struct mipi_dsi_host *host,
  85. const struct mipi_dsi_msg *msg);
  86. };
  87. /**
  88. * struct mipi_dsi_host - DSI host device
  89. * @dev: driver model device node for this DSI host
  90. * @ops: DSI host operations
  91. * @list: list management
  92. */
  93. struct mipi_dsi_host {
  94. struct device *dev;
  95. const struct mipi_dsi_host_ops *ops;
  96. struct list_head list;
  97. };
  98. int mipi_dsi_host_register(struct mipi_dsi_host *host);
  99. void mipi_dsi_host_unregister(struct mipi_dsi_host *host);
  100. struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node);
  101. /* DSI mode flags */
  102. /* video mode */
  103. #define MIPI_DSI_MODE_VIDEO BIT(0)
  104. /* video burst mode */
  105. #define MIPI_DSI_MODE_VIDEO_BURST BIT(1)
  106. /* video pulse mode */
  107. #define MIPI_DSI_MODE_VIDEO_SYNC_PULSE BIT(2)
  108. /* enable auto vertical count mode */
  109. #define MIPI_DSI_MODE_VIDEO_AUTO_VERT BIT(3)
  110. /* enable hsync-end packets in vsync-pulse and v-porch area */
  111. #define MIPI_DSI_MODE_VIDEO_HSE BIT(4)
  112. /* disable hfront-porch area */
  113. #define MIPI_DSI_MODE_VIDEO_NO_HFP BIT(5)
  114. /* disable hback-porch area */
  115. #define MIPI_DSI_MODE_VIDEO_NO_HBP BIT(6)
  116. /* disable hsync-active area */
  117. #define MIPI_DSI_MODE_VIDEO_NO_HSA BIT(7)
  118. /* flush display FIFO on vsync pulse */
  119. #define MIPI_DSI_MODE_VSYNC_FLUSH BIT(8)
  120. /* disable EoT packets in HS mode */
  121. #define MIPI_DSI_MODE_NO_EOT_PACKET BIT(9)
  122. /* device supports non-continuous clock behavior (DSI spec 5.6.1) */
  123. #define MIPI_DSI_CLOCK_NON_CONTINUOUS BIT(10)
  124. /* transmit data in low power */
  125. #define MIPI_DSI_MODE_LPM BIT(11)
  126. /* transmit data ending at the same time for all lanes within one hsync */
  127. #define MIPI_DSI_HS_PKT_END_ALIGNED BIT(12)
  128. enum mipi_dsi_pixel_format {
  129. MIPI_DSI_FMT_RGB888,
  130. MIPI_DSI_FMT_RGB666,
  131. MIPI_DSI_FMT_RGB666_PACKED,
  132. MIPI_DSI_FMT_RGB565,
  133. };
  134. #define DSI_DEV_NAME_SIZE 20
  135. /**
  136. * struct mipi_dsi_device_info - template for creating a mipi_dsi_device
  137. * @type: DSI peripheral chip type
  138. * @channel: DSI virtual channel assigned to peripheral
  139. * @node: pointer to OF device node or NULL
  140. *
  141. * This is populated and passed to mipi_dsi_device_new to create a new
  142. * DSI device
  143. */
  144. struct mipi_dsi_device_info {
  145. char type[DSI_DEV_NAME_SIZE];
  146. u32 channel;
  147. struct device_node *node;
  148. };
  149. /**
  150. * struct mipi_dsi_device - DSI peripheral device
  151. * @host: DSI host for this peripheral
  152. * @dev: driver model device node for this peripheral
  153. * @attached: the DSI device has been successfully attached
  154. * @name: DSI peripheral chip type
  155. * @channel: virtual channel assigned to the peripheral
  156. * @format: pixel format for video mode
  157. * @lanes: number of active data lanes
  158. * @mode_flags: DSI operation mode related flags
  159. * @hs_rate: maximum lane frequency for high speed mode in hertz, this should
  160. * be set to the real limits of the hardware, zero is only accepted for
  161. * legacy drivers
  162. * @lp_rate: maximum lane frequency for low power mode in hertz, this should
  163. * be set to the real limits of the hardware, zero is only accepted for
  164. * legacy drivers
  165. * @dsc: panel/bridge DSC pps payload to be sent
  166. */
  167. struct mipi_dsi_device {
  168. struct mipi_dsi_host *host;
  169. struct device dev;
  170. bool attached;
  171. char name[DSI_DEV_NAME_SIZE];
  172. unsigned int channel;
  173. unsigned int lanes;
  174. enum mipi_dsi_pixel_format format;
  175. unsigned long mode_flags;
  176. unsigned long hs_rate;
  177. unsigned long lp_rate;
  178. struct drm_dsc_config *dsc;
  179. };
  180. /**
  181. * struct mipi_dsi_multi_context - Context to call multiple MIPI DSI funcs in a row
  182. */
  183. struct mipi_dsi_multi_context {
  184. /**
  185. * @dsi: Pointer to the MIPI DSI device
  186. */
  187. struct mipi_dsi_device *dsi;
  188. /**
  189. * @accum_err: Storage for the accumulated error over the multiple calls
  190. *
  191. * Init to 0. If a function encounters an error then the error code
  192. * will be stored here. If you call a function and this points to a
  193. * non-zero value then the function will be a noop. This allows calling
  194. * a function many times in a row and just checking the error at the
  195. * end to see if any of them failed.
  196. */
  197. int accum_err;
  198. };
  199. #define MIPI_DSI_MODULE_PREFIX "mipi-dsi:"
  200. #define to_mipi_dsi_device(__dev) container_of_const(__dev, struct mipi_dsi_device, dev)
  201. /**
  202. * mipi_dsi_pixel_format_to_bpp - obtain the number of bits per pixel for any
  203. * given pixel format defined by the MIPI DSI
  204. * specification
  205. * @fmt: MIPI DSI pixel format
  206. *
  207. * Returns: The number of bits per pixel of the given pixel format.
  208. */
  209. static inline int mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)
  210. {
  211. switch (fmt) {
  212. case MIPI_DSI_FMT_RGB888:
  213. case MIPI_DSI_FMT_RGB666:
  214. return 24;
  215. case MIPI_DSI_FMT_RGB666_PACKED:
  216. return 18;
  217. case MIPI_DSI_FMT_RGB565:
  218. return 16;
  219. }
  220. return -EINVAL;
  221. }
  222. enum mipi_dsi_compression_algo {
  223. MIPI_DSI_COMPRESSION_DSC = 0,
  224. MIPI_DSI_COMPRESSION_VENDOR = 3,
  225. /* other two values are reserved, DSI 1.3 */
  226. };
  227. struct mipi_dsi_device *
  228. mipi_dsi_device_register_full(struct mipi_dsi_host *host,
  229. const struct mipi_dsi_device_info *info);
  230. void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi);
  231. struct mipi_dsi_device *
  232. devm_mipi_dsi_device_register_full(struct device *dev, struct mipi_dsi_host *host,
  233. const struct mipi_dsi_device_info *info);
  234. struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np);
  235. int mipi_dsi_attach(struct mipi_dsi_device *dsi);
  236. int mipi_dsi_detach(struct mipi_dsi_device *dsi);
  237. int devm_mipi_dsi_attach(struct device *dev, struct mipi_dsi_device *dsi);
  238. int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi);
  239. int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi);
  240. int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
  241. u16 value);
  242. int mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable);
  243. int mipi_dsi_compression_mode_ext(struct mipi_dsi_device *dsi, bool enable,
  244. enum mipi_dsi_compression_algo algo,
  245. unsigned int pps_selector);
  246. int mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
  247. const struct drm_dsc_picture_parameter_set *pps);
  248. void mipi_dsi_compression_mode_ext_multi(struct mipi_dsi_multi_context *ctx,
  249. bool enable,
  250. enum mipi_dsi_compression_algo algo,
  251. unsigned int pps_selector);
  252. void mipi_dsi_picture_parameter_set_multi(struct mipi_dsi_multi_context *ctx,
  253. const struct drm_dsc_picture_parameter_set *pps);
  254. ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
  255. size_t size);
  256. int mipi_dsi_generic_write_chatty(struct mipi_dsi_device *dsi,
  257. const void *payload, size_t size);
  258. void mipi_dsi_generic_write_multi(struct mipi_dsi_multi_context *ctx,
  259. const void *payload, size_t size);
  260. ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
  261. size_t num_params, void *data, size_t size);
  262. #define mipi_dsi_msleep(ctx, delay) \
  263. do { \
  264. if (!(ctx)->accum_err) \
  265. msleep(delay); \
  266. } while (0)
  267. #define mipi_dsi_usleep_range(ctx, min, max) \
  268. do { \
  269. if (!(ctx)->accum_err) \
  270. usleep_range(min, max); \
  271. } while (0)
  272. /**
  273. * enum mipi_dsi_dcs_tear_mode - Tearing Effect Output Line mode
  274. * @MIPI_DSI_DCS_TEAR_MODE_VBLANK: the TE output line consists of V-Blanking
  275. * information only
  276. * @MIPI_DSI_DCS_TEAR_MODE_VHBLANK : the TE output line consists of both
  277. * V-Blanking and H-Blanking information
  278. */
  279. enum mipi_dsi_dcs_tear_mode {
  280. MIPI_DSI_DCS_TEAR_MODE_VBLANK,
  281. MIPI_DSI_DCS_TEAR_MODE_VHBLANK,
  282. };
  283. #define MIPI_DSI_DCS_POWER_MODE_DISPLAY (1 << 2)
  284. #define MIPI_DSI_DCS_POWER_MODE_NORMAL (1 << 3)
  285. #define MIPI_DSI_DCS_POWER_MODE_SLEEP (1 << 4)
  286. #define MIPI_DSI_DCS_POWER_MODE_PARTIAL (1 << 5)
  287. #define MIPI_DSI_DCS_POWER_MODE_IDLE (1 << 6)
  288. ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
  289. const void *data, size_t len);
  290. int mipi_dsi_dcs_write_buffer_chatty(struct mipi_dsi_device *dsi,
  291. const void *data, size_t len);
  292. void mipi_dsi_dcs_write_buffer_multi(struct mipi_dsi_multi_context *ctx,
  293. const void *data, size_t len);
  294. ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
  295. const void *data, size_t len);
  296. ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
  297. size_t len);
  298. int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi);
  299. int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi);
  300. int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode);
  301. int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format);
  302. int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi);
  303. int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi);
  304. int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi);
  305. int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi);
  306. int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
  307. u16 end);
  308. int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
  309. u16 end);
  310. int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi);
  311. int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
  312. enum mipi_dsi_dcs_tear_mode mode);
  313. int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format);
  314. int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline);
  315. int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
  316. u16 brightness);
  317. int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
  318. u16 *brightness);
  319. int mipi_dsi_dcs_set_display_brightness_large(struct mipi_dsi_device *dsi,
  320. u16 brightness);
  321. int mipi_dsi_dcs_get_display_brightness_large(struct mipi_dsi_device *dsi,
  322. u16 *brightness);
  323. void mipi_dsi_dcs_nop_multi(struct mipi_dsi_multi_context *ctx);
  324. void mipi_dsi_dcs_enter_sleep_mode_multi(struct mipi_dsi_multi_context *ctx);
  325. void mipi_dsi_dcs_exit_sleep_mode_multi(struct mipi_dsi_multi_context *ctx);
  326. void mipi_dsi_dcs_set_display_off_multi(struct mipi_dsi_multi_context *ctx);
  327. void mipi_dsi_dcs_set_display_on_multi(struct mipi_dsi_multi_context *ctx);
  328. void mipi_dsi_dcs_set_tear_on_multi(struct mipi_dsi_multi_context *ctx,
  329. enum mipi_dsi_dcs_tear_mode mode);
  330. void mipi_dsi_turn_on_peripheral_multi(struct mipi_dsi_multi_context *ctx);
  331. void mipi_dsi_dcs_soft_reset_multi(struct mipi_dsi_multi_context *ctx);
  332. void mipi_dsi_dcs_set_display_brightness_multi(struct mipi_dsi_multi_context *ctx,
  333. u16 brightness);
  334. void mipi_dsi_dcs_set_pixel_format_multi(struct mipi_dsi_multi_context *ctx,
  335. u8 format);
  336. void mipi_dsi_dcs_set_column_address_multi(struct mipi_dsi_multi_context *ctx,
  337. u16 start, u16 end);
  338. void mipi_dsi_dcs_set_page_address_multi(struct mipi_dsi_multi_context *ctx,
  339. u16 start, u16 end);
  340. void mipi_dsi_dcs_set_tear_scanline_multi(struct mipi_dsi_multi_context *ctx,
  341. u16 scanline);
  342. /**
  343. * mipi_dsi_generic_write_seq - transmit data using a generic write packet
  344. *
  345. * This macro will print errors for you and will RETURN FROM THE CALLING
  346. * FUNCTION (yes this is non-intuitive) upon error.
  347. *
  348. * Because of the non-intuitive return behavior, THIS MACRO IS DEPRECATED.
  349. * Please replace calls of it with mipi_dsi_generic_write_seq_multi().
  350. *
  351. * @dsi: DSI peripheral device
  352. * @seq: buffer containing the payload
  353. */
  354. #define mipi_dsi_generic_write_seq(dsi, seq...) \
  355. do { \
  356. static const u8 d[] = { seq }; \
  357. int ret; \
  358. ret = mipi_dsi_generic_write_chatty(dsi, d, ARRAY_SIZE(d)); \
  359. if (ret < 0) \
  360. return ret; \
  361. } while (0)
  362. /**
  363. * mipi_dsi_generic_write_seq_multi - transmit data using a generic write packet
  364. *
  365. * This macro will print errors for you and error handling is optimized for
  366. * callers that call this multiple times in a row.
  367. *
  368. * @ctx: Context for multiple DSI transactions
  369. * @seq: buffer containing the payload
  370. */
  371. #define mipi_dsi_generic_write_seq_multi(ctx, seq...) \
  372. do { \
  373. static const u8 d[] = { seq }; \
  374. mipi_dsi_generic_write_multi(ctx, d, ARRAY_SIZE(d)); \
  375. } while (0)
  376. /**
  377. * mipi_dsi_dcs_write_seq - transmit a DCS command with payload
  378. *
  379. * This macro will print errors for you and will RETURN FROM THE CALLING
  380. * FUNCTION (yes this is non-intuitive) upon error.
  381. *
  382. * Because of the non-intuitive return behavior, THIS MACRO IS DEPRECATED.
  383. * Please replace calls of it with mipi_dsi_dcs_write_seq_multi().
  384. *
  385. * @dsi: DSI peripheral device
  386. * @cmd: Command
  387. * @seq: buffer containing data to be transmitted
  388. */
  389. #define mipi_dsi_dcs_write_seq(dsi, cmd, seq...) \
  390. do { \
  391. static const u8 d[] = { cmd, seq }; \
  392. int ret; \
  393. ret = mipi_dsi_dcs_write_buffer_chatty(dsi, d, ARRAY_SIZE(d)); \
  394. if (ret < 0) \
  395. return ret; \
  396. } while (0)
  397. /**
  398. * mipi_dsi_dcs_write_seq_multi - transmit a DCS command with payload
  399. *
  400. * This macro will print errors for you and error handling is optimized for
  401. * callers that call this multiple times in a row.
  402. *
  403. * @ctx: Context for multiple DSI transactions
  404. * @cmd: Command
  405. * @seq: buffer containing data to be transmitted
  406. */
  407. #define mipi_dsi_dcs_write_seq_multi(ctx, cmd, seq...) \
  408. do { \
  409. static const u8 d[] = { cmd, seq }; \
  410. mipi_dsi_dcs_write_buffer_multi(ctx, d, ARRAY_SIZE(d)); \
  411. } while (0)
  412. /**
  413. * struct mipi_dsi_driver - DSI driver
  414. * @driver: device driver model driver
  415. * @probe: callback for device binding
  416. * @remove: callback for device unbinding
  417. * @shutdown: called at shutdown time to quiesce the device
  418. */
  419. struct mipi_dsi_driver {
  420. struct device_driver driver;
  421. int(*probe)(struct mipi_dsi_device *dsi);
  422. void (*remove)(struct mipi_dsi_device *dsi);
  423. void (*shutdown)(struct mipi_dsi_device *dsi);
  424. };
  425. static inline struct mipi_dsi_driver *
  426. to_mipi_dsi_driver(struct device_driver *driver)
  427. {
  428. return container_of(driver, struct mipi_dsi_driver, driver);
  429. }
  430. static inline void *mipi_dsi_get_drvdata(const struct mipi_dsi_device *dsi)
  431. {
  432. return dev_get_drvdata(&dsi->dev);
  433. }
  434. static inline void mipi_dsi_set_drvdata(struct mipi_dsi_device *dsi, void *data)
  435. {
  436. dev_set_drvdata(&dsi->dev, data);
  437. }
  438. int mipi_dsi_driver_register_full(struct mipi_dsi_driver *driver,
  439. struct module *owner);
  440. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *driver);
  441. #define mipi_dsi_driver_register(driver) \
  442. mipi_dsi_driver_register_full(driver, THIS_MODULE)
  443. #define module_mipi_dsi_driver(__mipi_dsi_driver) \
  444. module_driver(__mipi_dsi_driver, mipi_dsi_driver_register, \
  445. mipi_dsi_driver_unregister)
  446. #endif /* __DRM_MIPI_DSI__ */