1 /*
2  * Copyright (c) 2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_MIPI_DSI_DRV_H
9 #define HPM_MIPI_DSI_DRV_H
10 
11 /**
12  * @brief MIPI_DSI APIs
13  * @defgroup mipi_dsi_interface MIPI_DSI driver APIs
14  * @ingroup mipi_dsi_interfaces
15  * @{
16  */
17 
18 #include "hpm_common.h"
19 #include "hpm_soc.h"
20 #include "hpm_mipi_dsi_regs.h"
21 
22 
23 /* MIPI DSI Processor-to-Peripheral transaction types */
24 typedef enum mipi_dsi_tx_cmd {
25     MIPI_DSI_SHUTDOWN_PERIPHERAL = 0x22,
26     MIPI_DSI_TURN_ON_PERIPHERAL = 0x32,
27     MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM = 0x03,
28     MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM = 0x13,
29     MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM = 0x23,
30     MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM = 0x04,
31     MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM = 0x14,
32     MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM = 0x24,
33     MIPI_DSI_DCS_SHORT_WRITE = 0x05,
34     MIPI_DSI_DCS_SHORT_WRITE_PARAM = 0x15,
35     MIPI_DSI_DCS_READ = 0x06,
36     MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE = 0x37,
37     MIPI_DSI_GENERIC_LONG_WRITE = 0x29,
38     MIPI_DSI_DCS_LONG_WRITE	= 0x39
39 } mipi_dsi_tx_cmd_t;
40 
41 /* MIPI DCS commands */
42 typedef enum mipi_dcs_cmd {
43     MIPI_DCS_NOP = 0x00,
44     MIPI_DCS_SOFT_RESET = 0x01,
45     MIPI_DCS_GET_DISPLAY_ID = 0x04,
46     MIPI_DCS_GET_RED_CHANNEL = 0x06,
47     MIPI_DCS_GET_GREEN_CHANNEL = 0x07,
48     MIPI_DCS_GET_BLUE_CHANNEL = 0x08,
49     MIPI_DCS_GET_DISPLAY_STATUS = 0x09,
50     MIPI_DCS_GET_POWER_MODE = 0x0A,
51     MIPI_DCS_GET_ADDRESS_MODE = 0x0B,
52     MIPI_DCS_GET_PIXEL_FORMAT = 0x0C,
53     MIPI_DCS_GET_DISPLAY_MODE = 0x0D,
54     MIPI_DCS_GET_SIGNAL_MODE = 0x0E,
55     MIPI_DCS_GET_DIAGNOSTIC_RESULT = 0x0F,
56     MIPI_DCS_ENTER_SLEEP_MODE = 0x10,
57     MIPI_DCS_EXIT_SLEEP_MODE = 0x11,
58     MIPI_DCS_ENTER_PARTIAL_MODE = 0x12,
59     MIPI_DCS_ENTER_NORMAL_MODE = 0x13,
60     MIPI_DCS_EXIT_INVERT_MODE = 0x20,
61     MIPI_DCS_ENTER_INVERT_MODE = 0x21,
62     MIPI_DCS_SET_GAMMA_CURVE = 0x26,
63     MIPI_DCS_SET_DISPLAY_OFF = 0x28,
64     MIPI_DCS_SET_DISPLAY_ON = 0x29,
65     MIPI_DCS_SET_COLUMN_ADDRESS = 0x2A,
66     MIPI_DCS_SET_PAGE_ADDRESS = 0x2B,
67     MIPI_DCS_WRITE_MEMORY_START = 0x2C,
68     MIPI_DCS_WRITE_LUT = 0x2D,
69     MIPI_DCS_READ_MEMORY_START = 0x2E,
70     MIPI_DCS_SET_PARTIAL_AREA = 0x30,
71     MIPI_DCS_SET_SCROLL_AREA = 0x33,
72     MIPI_DCS_SET_TEAR_OFF = 0x34,
73     MIPI_DCS_SET_TEAR_ON = 0x35,
74     MIPI_DCS_SET_ADDRESS_MODE = 0x36,
75     MIPI_DCS_SET_SCROLL_START = 0x37,
76     MIPI_DCS_EXIT_IDLE_MODE = 0x38,
77     MIPI_DCS_ENTER_IDLE_MODE = 0x39,
78     MIPI_DCS_SET_PIXEL_FORMAT = 0x3A,
79     MIPI_DCS_WRITE_MEMORY_CONTINUE = 0x3C,
80     MIPI_DCS_READ_MEMORY_CONTINUE = 0x3E,
81     MIPI_DCS_SET_TEAR_SCANLINE = 0x44,
82     MIPI_DCS_GET_SCANLINE = 0x45,
83     MIPI_DCS_SET_DISPLAY_BRIGHTNESS = 0x51,		/* MIPI DCS 1.3 */
84     MIPI_DCS_GET_DISPLAY_BRIGHTNESS = 0x52,		/* MIPI DCS 1.3 */
85     MIPI_DCS_WRITE_CONTROL_DISPLAY = 0x53,		/* MIPI DCS 1.3 */
86     MIPI_DCS_GET_CONTROL_DISPLAY = 0x54,		/* MIPI DCS 1.3 */
87     MIPI_DCS_WRITE_POWER_SAVE = 0x55,		/* MIPI DCS 1.3 */
88     MIPI_DCS_GET_POWER_SAVE = 0x56,		/* MIPI DCS 1.3 */
89     MIPI_DCS_SET_CABC_MIN_BRIGHTNESS = 0x5E,	/* MIPI DCS 1.3 */
90     MIPI_DCS_GET_CABC_MIN_BRIGHTNESS = 0x5F,	/* MIPI DCS 1.3 */
91     MIPI_DCS_READ_DDB_START = 0xA1,
92     MIPI_DCS_READ_DDB_CONTINUE = 0xA8,
93 } mipi_dcs_cmd_t;
94 
95 typedef enum mipi_dsi_pixel_format {
96 	MIPI_DSI_FMT_RGB888,
97 	MIPI_DSI_FMT_RGB666,
98 	MIPI_DSI_FMT_RGB666_PACKED,
99 	MIPI_DSI_FMT_RGB565,
100 } mipi_dsi_pixel_format_t;
101 
102 typedef enum mipi_dsi_video_mode {
103     MIPI_DSI_VIDEO_MODE_SYNC_PULSE = 0x00,
104     MIPI_DSI_VIDEO_MODE_SYNC_EVENT = 0x01,
105 	MIPI_DSI_VIDEO_MODE_BURST = 0x02,
106 } mipi_dsi_video_mode_t;
107 
108 /**
109  * mipi_dsi_msg_t - read/write DSI buffer
110  */
111 typedef struct mipi_dsi_msg {
112     uint8_t channel; /*!< virtual channel id */
113     uint8_t type; /*!< payload data type */
114     uint16_t tx_len; /*!< length of tx_buf */
115     const void *tx_buf; /*!< data to be written */
116     uint16_t rx_len; /*!< length of rx_buf */
117     void *rx_buf; /*!< data to be read, or NULL */
118 } mipi_dsi_msg_t;
119 
120 typedef struct mipi_video_para {
121     uint32_t pixel_clock_khz;
122     uint32_t hactive;		/*!< hor. active video */
123 	uint32_t hfront_porch;	/*!< hor. front porch */
124 	uint32_t hback_porch;	/*!< hor. back porch */
125 	uint32_t hsync_len;		/*!< hor. sync len */
126 
127 	uint32_t vactive;		/*!< ver. active video */
128 	uint32_t vfront_porch;	/*!< ver. front porch */
129 	uint32_t vback_porch;	/*!< ver. back porch */
130 	uint32_t vsync_len;		/*!< ver. sync len */
131 } mipi_video_para_t;
132 
133 typedef struct mipi_dsi_config {
134     uint32_t lane_mbps;
135     uint8_t channel;
136     uint8_t lanes;
137     bool disable_eotp;
138     mipi_dsi_pixel_format_t pixel_format;
139     mipi_video_para_t video_para;
140     mipi_dsi_video_mode_t video_mode;
141 } mipi_dsi_config_t;
142 
143 
144 #ifdef __cplusplus
145 extern "C" {
146 #endif
147 
148 /**
149  * @brief get MIPI_DSI default config on video mode
150  *
151  * @param cfg: MIPI_DSI default config
152  */
153 void mipi_dsi_get_defconfig_on_video(mipi_dsi_config_t *cfg);
154 
155 /**
156  * @brief MIPI_DSI init
157  *
158  * @param ptr MIPI_DSI base address
159  * @param cfg config of MIPI_DSI
160  */
161 void mipi_dsi_init(MIPI_DSI_Type *ptr, mipi_dsi_config_t *cfg);
162 
163 /**
164  * @brief MIPI_DSI phy interface power on
165  *
166  * @param ptr MIPI_DSI base address
167  */
168 void mipi_dsi_phy_poweron(MIPI_DSI_Type *ptr);
169 
170 /**
171  * @brief MIPI_DSI phy interface power down
172  *
173  * @param ptr MIPI_DSI base address
174  */
175 void mipi_dsi_phy_powerdown(MIPI_DSI_Type *ptr);
176 
177 /**
178  * @brief MIPI_DSI hs transfer start
179  *
180  * @param ptr MIPI_DSI base address
181  */
182 void mipi_dsi_video_mode_hs_transfer_enable(MIPI_DSI_Type *ptr);
183 
184 /**
185  * @brief MIPI_DSI hs transfer stop
186  *
187  * @param ptr MIPI_DSI base address
188  */
189 void mipi_dsi_video_mode_hs_transfer_disable(MIPI_DSI_Type *ptr);
190 
191 /**
192  * @brief transmit data using mipi dsi message in low power mode
193  *
194  * @param ptr MIPI_DSI base address
195  * @param msg MPI_DSI message
196  *
197  * @return The number of bytes transmitted on success or a negative error code
198  * on failure.
199  */
200 int mipi_dsi_lp_cmd_transfer(MIPI_DSI_Type *ptr, const mipi_dsi_msg_t *msg);
201 
202 /**
203  * @brief specify the maximum size of the
204  *	the payload in a long packet transmitted from the peripheral back to the
205  *	host processor
206  *
207  * @param ptr: MIPI_DSI base address
208  * @param channel: virtual channel
209  * @param value: the maximum size of the payload
210  *
211  * @return: true on success or false on failure.
212  */
213 int mipi_dsi_set_maximum_return_packet_size(MIPI_DSI_Type *ptr, uint8_t channel, uint16_t value);
214 
215 /**
216  * @brief transmit data using a generic write packet
217  *
218  * @param ptr: MIPI_DSI base address
219  * @param channel: virtual channel
220  * @param payload: buffer containing the payload
221  * @param size: size of payload buffer
222  *
223  * This function will automatically choose the right data type depending on
224  * the payload length.
225  *
226  * @return: The number of bytes transmitted on success or a negative error code
227  * on failure.
228  */
229 int mipi_dsi_generic_write(MIPI_DSI_Type *ptr, uint8_t channel, const void *payload,
230 			       uint16_t size);
231 
232 /**
233  * @brief receive data using a generic read packet
234  *
235  * @param ptr: MIPI_DSI base address
236  * @param channel: virtual channel
237  * @param params: buffer containing the request parameters
238  * @param num_params: number of request parameters
239  * @param data: buffer in which to return the received data
240  * @param size: size of receive buffer
241  *
242  * This function will automatically choose the right data type depending on
243  * the number of parameters passed in.
244  *
245  * @return: The number of bytes successfully read or a negative error code on
246  * failure.
247  */
248 int mipi_dsi_generic_read(MIPI_DSI_Type *ptr, uint8_t channel, const void *params,
249 			      uint16_t num_params, void *data, uint16_t size);
250 
251 /**
252  * @brief transmit a DCS command with payload
253  *
254  * @param ptr: MIPI_DSI base address
255  * @param channel: virtual channel
256  * @param data: buffer containing data to be transmitted
257  * @param len: size of transmission buffer
258  *
259  * This function will automatically choose the right data type depending on
260  * the command payload length.
261  *
262  * @return: The number of bytes successfully transmitted or a negative error
263  * code on failure.
264  */
265 int mipi_dsi_dcs_write_buffer(MIPI_DSI_Type *ptr, uint8_t channel,
266 				  const void *data, uint16_t len);
267 
268 /**
269  * @brief send DCS write command
270  *
271  * @param ptr: MIPI_DSI base address
272  * @param channel: virtual channel
273  * @param cmd: DCS command
274  * @param data: buffer containing the command payload
275  * @param len: command payload length
276  *
277  * This function will automatically choose the right data type depending on
278  * the command payload length.
279  *
280  * @return: The number of bytes successfully transmitted or a negative error
281  * code on failure.
282  */
283 int mipi_dsi_dcs_write(MIPI_DSI_Type *ptr, uint8_t channel, uint8_t cmd,
284 			   const void *data, uint16_t len);
285 
286 /**
287  * @brief send DCS read request command
288  *
289  * @param ptr: MIPI_DSI base address
290  * @param channel: virtual channel
291  * @param cmd: DCS command
292  * @param data: buffer in which to receive data
293  * @param len: size of receive buffer
294  *
295  * @return: The number of bytes read or a negative error code on failure.
296  */
297 int mipi_dsi_dcs_read(MIPI_DSI_Type *ptr, uint8_t channel, uint8_t cmd, void *data, uint16_t len);
298 
299 
300 #ifdef __cplusplus
301 }
302 #endif
303 
304 /**
305  * @}
306  */
307 #endif /* HPM_MIPI_DSI_DRV_H */
308