1 /*
2 * Copyright (c) 2021 HPMicro
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8
9 #ifndef HPM_LCDC_DRV_H
10 #define HPM_LCDC_DRV_H
11 #include "hpm_display_common.h"
12 #include "hpm_soc_feature.h"
13 #include "hpm_lcdc_regs.h"
14
15 /**
16 *
17 * @brief LCD driver APIs
18 * @defgroup lcd_interface LCD driver APIs
19 * @ingroup io_interfaces
20 * @{
21 */
22
23 #define LCDC_TEST_MODE_DISABLE (0U)
24 #define LCDC_TEST_MODE_BACKGROUND (1U)
25 #define LCDC_TEST_MODE_COLOR_BAR_COL (2U)
26 #define LCDC_TEST_MODE_COLOR_BAR_ROW (3U)
27
28 /* @brief LCD driver specific status */
29 enum {
30 status_lcdc_no_active_layer_yet = MAKE_STATUS(status_group_lcdc, 1),
31 status_lcdc_layer_not_supported = MAKE_STATUS(status_group_lcdc, 2),
32 };
33
34 /* @brief LCD line pattern */
35 typedef enum lcdc_line_pattern {
36 lcdc_line_pattern_rgb = 0,
37 lcdc_line_pattern_rbg,
38 lcdc_line_pattern_gbr,
39 lcdc_line_pattern_grb,
40 lcdc_line_pattern_brg,
41 lcdc_line_pattern_bgr,
42 } lcdc_line_pattern_t;
43
44 /* @brief LCD display mode */
45 typedef enum lcdc_display_mode {
46 lcdc_display_mode_normal = 0,
47 lcdc_display_mode_test_mode_1,
48 lcdc_display_mode_test_mode_2,
49 lcdc_display_mode_test_mode_3,
50 } lcdc_display_mode_t;
51
52 /* @brief LCD layer transfer max bytes */
53 typedef enum lcdc_layer_max_bytes_per_transfer {
54 lcdc_layer_max_bytes_64 = 0,
55 lcdc_layer_max_bytes_128,
56 lcdc_layer_max_bytes_256,
57 lcdc_layer_max_bytes_512,
58 lcdc_layer_max_bytes_1024,
59 } lcdc_layer_max_bytes_per_transfer_t;
60
61 /* @brief LCD control */
62 typedef struct lcdc_control {
63 lcdc_line_pattern_t line_pattern; /**< Line pattern setting */
64 lcdc_display_mode_t display_mode; /**< Display mode setting */
65 bool invert_pixel_data; /**< Invert pixel data level */
66 bool invert_pixel_clock; /**< Invert pixel clock level */
67 bool invert_href; /**< Invert href level */
68 bool invert_vsync; /**< Invert vsync level */
69 bool invert_hsync; /**< Invert hsync level */
70 } lcdc_control_t;
71
72 /* @brief LCD hsync/vsync config */
73 typedef struct lcdc_xsync_config {
74 uint16_t front_porch_pulse; /**< Front porch pulse */
75 uint16_t back_porch_pulse; /**< Back porch pulse */
76 uint16_t pulse_width; /**< Pulse width */
77 } lcdc_xsync_config_t;
78
79 /* @brief LCD config */
80 typedef struct lcdc_config {
81 uint16_t resolution_x; /**< Horizontal resolution in pixel */
82 uint16_t resolution_y; /**< Vertial resolution in pixel */
83 lcdc_xsync_config_t hsync; /**< Hsync config */
84 lcdc_xsync_config_t vsync; /**< Vsync config */
85 display_color_32b_t background; /**< Background color */
86 lcdc_control_t control; /**< LCD control */
87 } lcdc_config_t;
88
89 /* @brief LCD layer config */
90 typedef struct lcdc_layer_config {
91 uint8_t max_ot; /**< Maximum outstanding transfer */
92 display_byteorder_t byteorder; /**< Byte order */
93 display_yuv_format_t yuv; /**< YUV format */
94 display_pixel_format_t pixel_format; /**< Pixel format */
95 display_alphablend_option_t alphablend; /**< Alphablending option */
96 display_yuv2rgb_config_t csc_config; /**< Color space conversion config */
97 lcdc_layer_max_bytes_per_transfer_t max_bytes; /**< Layer max transfer bytes */
98 uint16_t height; /**< Layer height in pixel */
99 uint16_t width; /**< Layer width in pixel */
100 uint16_t position_x; /**< Layer output position X coord */
101 uint16_t position_y; /**< Layer output position Y coord */
102 display_color_32b_t background; /**< Background color */
103 uint32_t buffer; /**< Pointer of layer display buffer */
104 } lcdc_layer_config_t;
105
106 #ifdef __cplusplus
107 extern "C" {
108 #endif
109
110 /**
111 *
112 * @brief Layer config
113 *
114 * @param[in] ptr LCD base address
115 */
lcdc_software_reset(LCDC_Type * ptr)116 static inline void lcdc_software_reset(LCDC_Type *ptr)
117 {
118 ptr->CTRL |= LCDC_CTRL_SW_RST_MASK;
119 ptr->CTRL &= ~LCDC_CTRL_SW_RST_MASK;
120 }
121
122 /**
123 *
124 * @brief Enable interrupt according to the given mask
125 *
126 * @param[in] ptr LCD base address
127 * @param[in] interrupt_mask Mask of interrupts to be enabled
128 */
lcdc_enable_interrupt(LCDC_Type * ptr,uint32_t interrupt_mask)129 static inline void lcdc_enable_interrupt(LCDC_Type *ptr, uint32_t interrupt_mask)
130 {
131 ptr->INT_EN |= interrupt_mask;
132 }
133
134 /**
135 *
136 * @brief Disable interrupt according to the given mask
137 *
138 * @param[in] ptr LCD base address
139 * @param[in] interrupt_mask Mask of interrupts to be disabled
140 */
lcdc_disable_interrupt(LCDC_Type * ptr,uint32_t interrupt_mask)141 static inline void lcdc_disable_interrupt(LCDC_Type *ptr, uint32_t interrupt_mask)
142 {
143 ptr->INT_EN &= ~interrupt_mask;
144 }
145
146 /**
147 *
148 * @brief Clear specific status according to the given mask
149 *
150 * @param[in] ptr LCD base address
151 * @param[in] mask Status mask of status to be cleared
152 */
lcdc_clear_status(LCDC_Type * ptr,uint32_t mask)153 static inline void lcdc_clear_status(LCDC_Type *ptr, uint32_t mask)
154 {
155 ptr->ST |= mask;
156 }
157
158 /**
159 *
160 * @brief Make layer control shadow registers take effect
161 *
162 * @param[in] ptr LCD base address
163 * @param[in] layer_index Index of layer to be controlled
164 */
lcdc_layer_control_shadow_loaded(LCDC_Type * ptr,uint8_t layer_index)165 static inline bool lcdc_layer_control_shadow_loaded(LCDC_Type *ptr, uint8_t layer_index)
166 {
167 return !(ptr->LAYER[layer_index].LAYCTRL & LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK);
168 }
169
170 /**
171 *
172 * @brief Get DMA status
173 *
174 * @param[in] ptr LCD base address
175 * @retval DMA status
176 */
lcdc_get_dma_status(LCDC_Type * ptr)177 static inline uint32_t lcdc_get_dma_status(LCDC_Type *ptr)
178 {
179 return ptr->DMA_ST;
180 }
181
182 /**
183 *
184 * @brief Check DMA status against the given mask
185 *
186 * @param[in] ptr LCD base address
187 * @param[in] mask Mask of expected DMA status
188 * @retval true if all bits set to 1 in mask are set
189 * @retval false if any bit set to 1 in mask is not set
190 */
lcdc_check_dma_status(LCDC_Type * ptr,uint32_t mask)191 static inline bool lcdc_check_dma_status(LCDC_Type *ptr, uint32_t mask)
192 {
193 return ((ptr->DMA_ST & mask) == mask);
194 }
195
196 /**
197 *
198 * @brief Clear DMA status according to the given mask
199 *
200 * @param[in] ptr LCD base address
201 * @param[in] mask Mask of expected DMA status
202 */
lcdc_clear_dma_status(LCDC_Type * ptr,uint32_t mask)203 static inline void lcdc_clear_dma_status(LCDC_Type *ptr, uint32_t mask)
204 {
205 ptr->DMA_ST |= mask;
206 }
207
208 /**
209 *
210 * @brief Get status
211 *
212 * @param[in] ptr LCD base address
213 * @retval current status
214 */
lcdc_get_status(LCDC_Type * ptr)215 static inline uint32_t lcdc_get_status(LCDC_Type *ptr)
216 {
217 return ptr->ST;
218 }
219
220 /**
221 *
222 * @brief Check status against the given mask
223 *
224 * @param[in] ptr LCD base address
225 * @param[in] mask Mask of expected status
226 * @retval true if all bits set to 1 in mask are set
227 * @retval false if any bit set to 1 in mask is not set
228 */
lcdc_check_status(LCDC_Type * ptr,uint32_t mask)229 static inline bool lcdc_check_status(LCDC_Type *ptr, uint32_t mask)
230 {
231 return (ptr->ST & mask) == mask;
232 }
233
234 /**
235 *
236 * @brief Set next buffer for certain layer
237 *
238 * @param[in] ptr LCD base address
239 * @param[in] layer_index target layer to be configured
240 * @param[in] buffer display buffer to be set
241 */
lcdc_layer_set_next_buffer(LCDC_Type * ptr,uint32_t layer_index,uint32_t buffer)242 static inline void lcdc_layer_set_next_buffer(LCDC_Type *ptr, uint32_t layer_index, uint32_t buffer)
243 {
244 ptr->LAYER[layer_index].START0 = LCDC_LAYER_START0_ADDR0_SET(buffer);
245 ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
246 }
247
248 /**
249 *
250 * @brief Update specific layer background
251 *
252 * @param[in] ptr LCD base address
253 * @param[in] layer_index target layer to be configured
254 * @param[in] background color to be set as background
255 */
lcdc_layer_update_background(LCDC_Type * ptr,uint8_t layer_index,display_color_32b_t background)256 static inline void lcdc_layer_update_background(LCDC_Type *ptr,
257 uint8_t layer_index, display_color_32b_t background)
258 {
259 ptr->LAYER[layer_index].BG_CL = LCDC_LAYER_BG_CL_ARGB_SET(background.u);
260 ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
261 }
262
263 /**
264 *
265 * @brief Update specific layer position
266 *
267 * @param[in] ptr LCD base address
268 * @param[in] layer_index target layer to be configured
269 * @param[in] x Position X coord
270 * @param[in] y Position Y coord
271 */
lcdc_layer_update_position(LCDC_Type * ptr,uint8_t layer_index,uint16_t x,uint32_t y)272 static inline void lcdc_layer_update_position(LCDC_Type *ptr,
273 uint8_t layer_index, uint16_t x, uint32_t y)
274 {
275 ptr->LAYER[layer_index].LAYPOS = LCDC_LAYER_LAYPOS_X_SET(x)
276 | LCDC_LAYER_LAYPOS_Y_SET(y);
277 ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
278 }
279
280 /**
281 *
282 * @brief Update specific layer dimension
283 *
284 * @param[in] ptr LCD base address
285 * @param[in] layer_index target layer to be configured
286 * @param[in] width Width in pixel
287 * @param[in] height Height in pixel
288 */
lcdc_layer_update_dimension(LCDC_Type * ptr,uint8_t layer_index,uint8_t width,uint8_t height)289 static inline void lcdc_layer_update_dimension(LCDC_Type *ptr,
290 uint8_t layer_index, uint8_t width, uint8_t height)
291 {
292 ptr->LAYER[layer_index].LAYSIZE = LCDC_LAYER_LAYSIZE_WIDTH_SET(width)
293 | LCDC_LAYER_LAYSIZE_HEIGHT_SET(height);
294 ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
295 }
296
297 /**
298 *
299 * @brief Update specific layer region
300 *
301 * @param[in] ptr LCD base address
302 * @param[in] layer_index target layer to be configured
303 * @param[in] x1 X coord of the top left pixel
304 * @param[in] y1 Y coord of the top left pixel
305 * @param[in] x2 X coord of the bottom right pixel
306 * @param[in] y2 Y coord of the bottom right pixel
307 */
lcdc_layer_set_region(LCDC_Type * ptr,uint8_t layer_index,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)308 static inline void lcdc_layer_set_region(LCDC_Type *ptr, uint8_t layer_index,
309 uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
310 {
311 ptr->LAYER[layer_index].LAYPOS = LCDC_LAYER_LAYPOS_X_SET(x1)
312 | LCDC_LAYER_LAYPOS_Y_SET(y1);
313 ptr->LAYER[layer_index].LAYSIZE = LCDC_LAYER_LAYSIZE_WIDTH_SET(x2 - x1 + 1)
314 | LCDC_LAYER_LAYSIZE_HEIGHT_SET(y2 - y1 + 1);
315 ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
316 }
317
318 /**
319 *
320 * @brief Update specific layer configuration
321 *
322 * @param[in] ptr LCD base address
323 * @param[in] layer_index target layer to be configured
324 */
lcdc_layer_update(LCDC_Type * ptr,uint8_t layer_index)325 static inline void lcdc_layer_update(LCDC_Type *ptr, uint8_t layer_index)
326 {
327 ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
328 }
329
330 /**
331 *
332 * @brief Enable specific layer
333 *
334 * @param[in] ptr LCD base address
335 * @param[in] layer_index target layer to be configured
336 */
lcdc_layer_enable(LCDC_Type * ptr,uint32_t layer_index)337 static inline void lcdc_layer_enable(LCDC_Type *ptr, uint32_t layer_index)
338 {
339 ptr->LAYER[layer_index].LAYCTRL |=
340 (LCDC_LAYER_LAYCTRL_EN_MASK | LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK);
341 }
342
343 /**
344 *
345 * @brief Disable specific layer
346 *
347 * @param[in] ptr LCD base address
348 * @param[in] layer_index target layer to be configured
349 */
lcdc_layer_disable(LCDC_Type * ptr,uint32_t layer_index)350 static inline void lcdc_layer_disable(LCDC_Type *ptr, uint32_t layer_index)
351 {
352 ptr->LAYER[layer_index].LAYCTRL =
353 (ptr->LAYER[layer_index].LAYCTRL & (~LCDC_LAYER_LAYCTRL_EN_MASK))
354 | LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
355 }
356
357 /**
358 *
359 * @brief Set test mode
360 *
361 * @param[in] ptr LCD base address
362 * @param[in] test_mode target test mode to be enabled
363 */
lcdc_set_testmode(LCDC_Type * ptr,uint8_t test_mode)364 static inline void lcdc_set_testmode(LCDC_Type *ptr, uint8_t test_mode)
365 {
366 ptr->CTRL = ((ptr->CTRL & ~LCDC_CTRL_DISP_MODE_MASK))
367 | LCDC_CTRL_DISP_MODE_SET(test_mode)
368 | LCDC_CTRL_DISP_ON_MASK;
369 }
370
371 /**
372 *
373 * @brief Set background
374 *
375 * @param[in] ptr LCD base address
376 * @param[in] color background color
377 */
lcdc_set_background(LCDC_Type * ptr,display_color_32b_t color)378 static inline void lcdc_set_background(LCDC_Type *ptr,
379 display_color_32b_t color)
380 {
381 ptr->BGND_CL = LCDC_BGND_CL_R_SET(color.r)
382 | LCDC_BGND_CL_G_SET(color.g)
383 | LCDC_BGND_CL_B_SET(color.b);
384 }
385
386 /**
387 *
388 * @brief enable background on alpha blender
389 *
390 * @note it not depend the background color of the layer itself. it can be used with lcdc_set_background API
391 *
392 * @param[in] ptr LCD base address
393 */
lcdc_enable_background_in_alpha_blender(LCDC_Type * ptr)394 static inline void lcdc_enable_background_in_alpha_blender(LCDC_Type *ptr)
395 {
396 ptr->CTRL |= LCDC_CTRL_BGDCL4CLR_MASK;
397 }
398
399 /**
400 *
401 * @brief disable background on alpha blender
402 *
403 * @note if not use background but want depend the the background color of the layer itself, can be use the API
404 *
405 * @param[in] ptr LCD base address
406 */
lcdc_disable_background_in_alpha_blender(LCDC_Type * ptr)407 static inline void lcdc_disable_background_in_alpha_blender(LCDC_Type *ptr)
408 {
409 ptr->CTRL &= ~LCDC_CTRL_BGDCL4CLR_MASK;
410 }
411 /**
412 *
413 * @brief Get default layer configuration value
414 *
415 * @param[in] ptr LCD base address
416 * @param[out] layer Pointer of layer configuration struct buffer
417 * @param[in] pixel_format Pixel format to be used for this layer
418 * @param[in] layer_index target layer to be configured
419 */
420 void lcdc_get_default_layer_config(LCDC_Type *ptr,
421 lcdc_layer_config_t *layer, display_pixel_format_t pixel_format, uint8_t layer_index);
422
423 /**
424 *
425 * @brief Get default configuration value
426 *
427 * @param[in] ptr LCD base address
428 * @param[out] config Pointer of configuration struct buffer
429 */
430 void lcdc_get_default_config(LCDC_Type *ptr, lcdc_config_t *config);
431
432 /**
433 *
434 * @brief Initialize LCD controller
435 *
436 * @param[in] ptr LCD base address
437 * @param[in] config Pointer of configuration struct buffer
438 */
439 void lcdc_init(LCDC_Type *ptr, lcdc_config_t *config);
440
441 /**
442 *
443 * @brief Configure specific layer
444 *
445 * @param[in] ptr LCD base address
446 * @param[in] layer_index target layer to be configured
447 * @param[in] layer_config Pointer of layer configuration struct buffer
448 * @param[in] enable_layer Set true if the layer needs to be enabled right after being configured
449 */
450 hpm_stat_t lcdc_config_layer(LCDC_Type *ptr, uint8_t layer_index,
451 lcdc_layer_config_t *layer_config, bool enable_layer);
452
453 /**
454 *
455 * @brief Turn on display
456 *
457 * @param[in] ptr LCD base address
458 */
459 void lcdc_turn_on_display(LCDC_Type *ptr);
460
461 /**
462 *
463 * @brief Turn off display
464 *
465 * @param[in] ptr LCD base address
466 */
467 void lcdc_turn_off_display(LCDC_Type *ptr);
468
469 #ifdef __cplusplus
470 }
471 #endif
472 /**
473 * @}
474 */
475
476 #endif /* HPM_LCDC_DRV_H */
477