Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * MIPI DSI Bus
0004  *
0005  * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
0006  * Andrzej Hajda <a.hajda@samsung.com>
0007  */
0008 
0009 #ifndef __DRM_MIPI_DSI_H__
0010 #define __DRM_MIPI_DSI_H__
0011 
0012 #include <linux/device.h>
0013 
0014 struct mipi_dsi_host;
0015 struct mipi_dsi_device;
0016 struct drm_dsc_picture_parameter_set;
0017 
0018 /* request ACK from peripheral */
0019 #define MIPI_DSI_MSG_REQ_ACK    BIT(0)
0020 /* use Low Power Mode to transmit message */
0021 #define MIPI_DSI_MSG_USE_LPM    BIT(1)
0022 
0023 /**
0024  * struct mipi_dsi_msg - read/write DSI buffer
0025  * @channel: virtual channel id
0026  * @type: payload data type
0027  * @flags: flags controlling this message transmission
0028  * @tx_len: length of @tx_buf
0029  * @tx_buf: data to be written
0030  * @rx_len: length of @rx_buf
0031  * @rx_buf: data to be read, or NULL
0032  */
0033 struct mipi_dsi_msg {
0034     u8 channel;
0035     u8 type;
0036     u16 flags;
0037 
0038     size_t tx_len;
0039     const void *tx_buf;
0040 
0041     size_t rx_len;
0042     void *rx_buf;
0043 };
0044 
0045 bool mipi_dsi_packet_format_is_short(u8 type);
0046 bool mipi_dsi_packet_format_is_long(u8 type);
0047 
0048 /**
0049  * struct mipi_dsi_packet - represents a MIPI DSI packet in protocol format
0050  * @size: size (in bytes) of the packet
0051  * @header: the four bytes that make up the header (Data ID, Word Count or
0052  *     Packet Data, and ECC)
0053  * @payload_length: number of bytes in the payload
0054  * @payload: a pointer to a buffer containing the payload, if any
0055  */
0056 struct mipi_dsi_packet {
0057     size_t size;
0058     u8 header[4];
0059     size_t payload_length;
0060     const u8 *payload;
0061 };
0062 
0063 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
0064                const struct mipi_dsi_msg *msg);
0065 
0066 /**
0067  * struct mipi_dsi_host_ops - DSI bus operations
0068  * @attach: attach DSI device to DSI host
0069  * @detach: detach DSI device from DSI host
0070  * @transfer: transmit a DSI packet
0071  *
0072  * DSI packets transmitted by .transfer() are passed in as mipi_dsi_msg
0073  * structures. This structure contains information about the type of packet
0074  * being transmitted as well as the transmit and receive buffers. When an
0075  * error is encountered during transmission, this function will return a
0076  * negative error code. On success it shall return the number of bytes
0077  * transmitted for write packets or the number of bytes received for read
0078  * packets.
0079  *
0080  * Note that typically DSI packet transmission is atomic, so the .transfer()
0081  * function will seldomly return anything other than the number of bytes
0082  * contained in the transmit buffer on success.
0083  *
0084  * Also note that those callbacks can be called no matter the state the
0085  * host is in. Drivers that need the underlying device to be powered to
0086  * perform these operations will first need to make sure it's been
0087  * properly enabled.
0088  */
0089 struct mipi_dsi_host_ops {
0090     int (*attach)(struct mipi_dsi_host *host,
0091               struct mipi_dsi_device *dsi);
0092     int (*detach)(struct mipi_dsi_host *host,
0093               struct mipi_dsi_device *dsi);
0094     ssize_t (*transfer)(struct mipi_dsi_host *host,
0095                 const struct mipi_dsi_msg *msg);
0096 };
0097 
0098 /**
0099  * struct mipi_dsi_host - DSI host device
0100  * @dev: driver model device node for this DSI host
0101  * @ops: DSI host operations
0102  * @list: list management
0103  */
0104 struct mipi_dsi_host {
0105     struct device *dev;
0106     const struct mipi_dsi_host_ops *ops;
0107     struct list_head list;
0108 };
0109 
0110 int mipi_dsi_host_register(struct mipi_dsi_host *host);
0111 void mipi_dsi_host_unregister(struct mipi_dsi_host *host);
0112 struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node);
0113 
0114 /* DSI mode flags */
0115 
0116 /* video mode */
0117 #define MIPI_DSI_MODE_VIDEO     BIT(0)
0118 /* video burst mode */
0119 #define MIPI_DSI_MODE_VIDEO_BURST   BIT(1)
0120 /* video pulse mode */
0121 #define MIPI_DSI_MODE_VIDEO_SYNC_PULSE  BIT(2)
0122 /* enable auto vertical count mode */
0123 #define MIPI_DSI_MODE_VIDEO_AUTO_VERT   BIT(3)
0124 /* enable hsync-end packets in vsync-pulse and v-porch area */
0125 #define MIPI_DSI_MODE_VIDEO_HSE     BIT(4)
0126 /* disable hfront-porch area */
0127 #define MIPI_DSI_MODE_VIDEO_NO_HFP  BIT(5)
0128 /* disable hback-porch area */
0129 #define MIPI_DSI_MODE_VIDEO_NO_HBP  BIT(6)
0130 /* disable hsync-active area */
0131 #define MIPI_DSI_MODE_VIDEO_NO_HSA  BIT(7)
0132 /* flush display FIFO on vsync pulse */
0133 #define MIPI_DSI_MODE_VSYNC_FLUSH   BIT(8)
0134 /* disable EoT packets in HS mode */
0135 #define MIPI_DSI_MODE_NO_EOT_PACKET BIT(9)
0136 /* device supports non-continuous clock behavior (DSI spec 5.6.1) */
0137 #define MIPI_DSI_CLOCK_NON_CONTINUOUS   BIT(10)
0138 /* transmit data in low power */
0139 #define MIPI_DSI_MODE_LPM       BIT(11)
0140 /* transmit data ending at the same time for all lanes within one hsync */
0141 #define MIPI_DSI_HS_PKT_END_ALIGNED BIT(12)
0142 
0143 enum mipi_dsi_pixel_format {
0144     MIPI_DSI_FMT_RGB888,
0145     MIPI_DSI_FMT_RGB666,
0146     MIPI_DSI_FMT_RGB666_PACKED,
0147     MIPI_DSI_FMT_RGB565,
0148 };
0149 
0150 #define DSI_DEV_NAME_SIZE       20
0151 
0152 /**
0153  * struct mipi_dsi_device_info - template for creating a mipi_dsi_device
0154  * @type: DSI peripheral chip type
0155  * @channel: DSI virtual channel assigned to peripheral
0156  * @node: pointer to OF device node or NULL
0157  *
0158  * This is populated and passed to mipi_dsi_device_new to create a new
0159  * DSI device
0160  */
0161 struct mipi_dsi_device_info {
0162     char type[DSI_DEV_NAME_SIZE];
0163     u32 channel;
0164     struct device_node *node;
0165 };
0166 
0167 /**
0168  * struct mipi_dsi_device - DSI peripheral device
0169  * @host: DSI host for this peripheral
0170  * @dev: driver model device node for this peripheral
0171  * @name: DSI peripheral chip type
0172  * @channel: virtual channel assigned to the peripheral
0173  * @format: pixel format for video mode
0174  * @lanes: number of active data lanes
0175  * @mode_flags: DSI operation mode related flags
0176  * @hs_rate: maximum lane frequency for high speed mode in hertz, this should
0177  * be set to the real limits of the hardware, zero is only accepted for
0178  * legacy drivers
0179  * @lp_rate: maximum lane frequency for low power mode in hertz, this should
0180  * be set to the real limits of the hardware, zero is only accepted for
0181  * legacy drivers
0182  */
0183 struct mipi_dsi_device {
0184     struct mipi_dsi_host *host;
0185     struct device dev;
0186 
0187     char name[DSI_DEV_NAME_SIZE];
0188     unsigned int channel;
0189     unsigned int lanes;
0190     enum mipi_dsi_pixel_format format;
0191     unsigned long mode_flags;
0192     unsigned long hs_rate;
0193     unsigned long lp_rate;
0194 };
0195 
0196 #define MIPI_DSI_MODULE_PREFIX "mipi-dsi:"
0197 
0198 static inline struct mipi_dsi_device *to_mipi_dsi_device(struct device *dev)
0199 {
0200     return container_of(dev, struct mipi_dsi_device, dev);
0201 }
0202 
0203 /**
0204  * mipi_dsi_pixel_format_to_bpp - obtain the number of bits per pixel for any
0205  *                                given pixel format defined by the MIPI DSI
0206  *                                specification
0207  * @fmt: MIPI DSI pixel format
0208  *
0209  * Returns: The number of bits per pixel of the given pixel format.
0210  */
0211 static inline int mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)
0212 {
0213     switch (fmt) {
0214     case MIPI_DSI_FMT_RGB888:
0215     case MIPI_DSI_FMT_RGB666:
0216         return 24;
0217 
0218     case MIPI_DSI_FMT_RGB666_PACKED:
0219         return 18;
0220 
0221     case MIPI_DSI_FMT_RGB565:
0222         return 16;
0223     }
0224 
0225     return -EINVAL;
0226 }
0227 
0228 struct mipi_dsi_device *
0229 mipi_dsi_device_register_full(struct mipi_dsi_host *host,
0230                   const struct mipi_dsi_device_info *info);
0231 void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi);
0232 struct mipi_dsi_device *
0233 devm_mipi_dsi_device_register_full(struct device *dev, struct mipi_dsi_host *host,
0234                    const struct mipi_dsi_device_info *info);
0235 struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np);
0236 int mipi_dsi_attach(struct mipi_dsi_device *dsi);
0237 int mipi_dsi_detach(struct mipi_dsi_device *dsi);
0238 int devm_mipi_dsi_attach(struct device *dev, struct mipi_dsi_device *dsi);
0239 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi);
0240 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi);
0241 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
0242                         u16 value);
0243 ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable);
0244 ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
0245                        const struct drm_dsc_picture_parameter_set *pps);
0246 
0247 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
0248                    size_t size);
0249 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
0250                   size_t num_params, void *data, size_t size);
0251 
0252 /**
0253  * enum mipi_dsi_dcs_tear_mode - Tearing Effect Output Line mode
0254  * @MIPI_DSI_DCS_TEAR_MODE_VBLANK: the TE output line consists of V-Blanking
0255  *    information only
0256  * @MIPI_DSI_DCS_TEAR_MODE_VHBLANK : the TE output line consists of both
0257  *    V-Blanking and H-Blanking information
0258  */
0259 enum mipi_dsi_dcs_tear_mode {
0260     MIPI_DSI_DCS_TEAR_MODE_VBLANK,
0261     MIPI_DSI_DCS_TEAR_MODE_VHBLANK,
0262 };
0263 
0264 #define MIPI_DSI_DCS_POWER_MODE_DISPLAY (1 << 2)
0265 #define MIPI_DSI_DCS_POWER_MODE_NORMAL  (1 << 3)
0266 #define MIPI_DSI_DCS_POWER_MODE_SLEEP   (1 << 4)
0267 #define MIPI_DSI_DCS_POWER_MODE_PARTIAL (1 << 5)
0268 #define MIPI_DSI_DCS_POWER_MODE_IDLE    (1 << 6)
0269 
0270 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
0271                   const void *data, size_t len);
0272 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
0273                const void *data, size_t len);
0274 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
0275               size_t len);
0276 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi);
0277 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi);
0278 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode);
0279 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format);
0280 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi);
0281 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi);
0282 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi);
0283 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi);
0284 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
0285                     u16 end);
0286 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
0287                   u16 end);
0288 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi);
0289 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
0290                  enum mipi_dsi_dcs_tear_mode mode);
0291 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format);
0292 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline);
0293 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
0294                     u16 brightness);
0295 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
0296                     u16 *brightness);
0297 
0298 /**
0299  * mipi_dsi_dcs_write_seq - transmit a DCS command with payload
0300  * @dsi: DSI peripheral device
0301  * @cmd: Command
0302  * @seq: buffer containing data to be transmitted
0303  */
0304 #define mipi_dsi_dcs_write_seq(dsi, cmd, seq...) do {               \
0305         static const u8 d[] = { cmd, seq };             \
0306         struct device *dev = &dsi->dev; \
0307         int ret;                        \
0308         ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
0309         if (ret < 0) {                      \
0310             dev_err_ratelimited(dev, "sending command %#02x failed: %d\n", cmd, ret); \
0311             return ret;                     \
0312         }                       \
0313     } while (0)
0314 
0315 /**
0316  * struct mipi_dsi_driver - DSI driver
0317  * @driver: device driver model driver
0318  * @probe: callback for device binding
0319  * @remove: callback for device unbinding
0320  * @shutdown: called at shutdown time to quiesce the device
0321  */
0322 struct mipi_dsi_driver {
0323     struct device_driver driver;
0324     int(*probe)(struct mipi_dsi_device *dsi);
0325     int(*remove)(struct mipi_dsi_device *dsi);
0326     void (*shutdown)(struct mipi_dsi_device *dsi);
0327 };
0328 
0329 static inline struct mipi_dsi_driver *
0330 to_mipi_dsi_driver(struct device_driver *driver)
0331 {
0332     return container_of(driver, struct mipi_dsi_driver, driver);
0333 }
0334 
0335 static inline void *mipi_dsi_get_drvdata(const struct mipi_dsi_device *dsi)
0336 {
0337     return dev_get_drvdata(&dsi->dev);
0338 }
0339 
0340 static inline void mipi_dsi_set_drvdata(struct mipi_dsi_device *dsi, void *data)
0341 {
0342     dev_set_drvdata(&dsi->dev, data);
0343 }
0344 
0345 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *driver,
0346                   struct module *owner);
0347 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *driver);
0348 
0349 #define mipi_dsi_driver_register(driver) \
0350     mipi_dsi_driver_register_full(driver, THIS_MODULE)
0351 
0352 #define module_mipi_dsi_driver(__mipi_dsi_driver) \
0353     module_driver(__mipi_dsi_driver, mipi_dsi_driver_register, \
0354             mipi_dsi_driver_unregister)
0355 
0356 #endif /* __DRM_MIPI_DSI__ */