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