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     uint32_t stride;                                    /**< stride of lines in bytes. stride is calculated by driver if stride == 0. */
105 } lcdc_layer_config_t;
106 
107 #ifdef __cplusplus
108 extern "C" {
109 #endif
110 
111 /**
112  *
113  * @brief Layer config
114  *
115  * @param[in] ptr LCD base address
116  */
lcdc_software_reset(LCDC_Type * ptr)117 static inline void lcdc_software_reset(LCDC_Type *ptr)
118 {
119     ptr->CTRL |= LCDC_CTRL_SW_RST_MASK;
120     ptr->CTRL &= ~LCDC_CTRL_SW_RST_MASK;
121 }
122 
123 /**
124  *
125  * @brief Enable interrupt according to the given mask
126  *
127  * @param[in] ptr LCD base address
128  * @param[in] interrupt_mask Mask of interrupts to be enabled
129  */
lcdc_enable_interrupt(LCDC_Type * ptr,uint32_t interrupt_mask)130 static inline void lcdc_enable_interrupt(LCDC_Type *ptr, uint32_t interrupt_mask)
131 {
132     ptr->INT_EN |= interrupt_mask;
133 }
134 
135 /**
136  *
137  * @brief Disable interrupt according to the given mask
138  *
139  * @param[in] ptr LCD base address
140  * @param[in] interrupt_mask Mask of interrupts to be disabled
141  */
lcdc_disable_interrupt(LCDC_Type * ptr,uint32_t interrupt_mask)142 static inline void lcdc_disable_interrupt(LCDC_Type *ptr, uint32_t interrupt_mask)
143 {
144     ptr->INT_EN &= ~interrupt_mask;
145 }
146 
147 /**
148  *
149  * @brief Clear specific status according to the given mask
150  *
151  * @param[in] ptr LCD base address
152  * @param[in] mask Status mask of status to be cleared
153  */
lcdc_clear_status(LCDC_Type * ptr,uint32_t mask)154 static inline void lcdc_clear_status(LCDC_Type *ptr, uint32_t mask)
155 {
156     ptr->ST = mask;
157 }
158 
159 /**
160  *
161  * @brief Make layer control shadow registers take effect
162  *
163  * @param[in] ptr LCD base address
164  * @param[in] layer_index Index of layer to be controlled
165  */
lcdc_layer_control_shadow_loaded(LCDC_Type * ptr,uint8_t layer_index)166 static inline bool lcdc_layer_control_shadow_loaded(LCDC_Type *ptr, uint8_t layer_index)
167 {
168     return !(ptr->LAYER[layer_index].LAYCTRL & LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK);
169 }
170 
171 /**
172  *
173  * @brief Get DMA status
174  *
175  * @param[in] ptr LCD base address
176  * @retval DMA status
177  */
lcdc_get_dma_status(LCDC_Type * ptr)178 static inline uint32_t lcdc_get_dma_status(LCDC_Type *ptr)
179 {
180     return ptr->DMA_ST;
181 }
182 
183 /**
184  *
185  * @brief Check DMA status against the given mask
186  *
187  * @param[in] ptr LCD base address
188  * @param[in] mask Mask of expected DMA status
189  * @retval true if all bits set to 1 in mask are set
190  * @retval false if any bit set to 1 in mask is not set
191  */
lcdc_check_dma_status(LCDC_Type * ptr,uint32_t mask)192 static inline bool lcdc_check_dma_status(LCDC_Type *ptr, uint32_t mask)
193 {
194     return ((ptr->DMA_ST & mask) == mask);
195 }
196 
197 /**
198  *
199  * @brief Clear DMA status according to the given mask
200  *
201  * @param[in] ptr LCD base address
202  * @param[in] mask Mask of expected DMA status
203  */
lcdc_clear_dma_status(LCDC_Type * ptr,uint32_t mask)204 static inline void lcdc_clear_dma_status(LCDC_Type *ptr, uint32_t mask)
205 {
206     ptr->DMA_ST = mask;
207 }
208 
209 /**
210  *
211  * @brief Get status
212  *
213  * @param[in] ptr LCD base address
214  * @retval current status
215  */
lcdc_get_status(LCDC_Type * ptr)216 static inline uint32_t lcdc_get_status(LCDC_Type *ptr)
217 {
218     return ptr->ST;
219 }
220 
221 /**
222  *
223  * @brief Check status against the given mask
224  *
225  * @param[in] ptr LCD base address
226  * @param[in] mask Mask of expected status
227  * @retval true if all bits set to 1 in mask are set
228  * @retval false if any bit set to 1 in mask is not set
229  */
lcdc_check_status(LCDC_Type * ptr,uint32_t mask)230 static inline bool lcdc_check_status(LCDC_Type *ptr, uint32_t mask)
231 {
232     return (ptr->ST & mask) == mask;
233 }
234 
235 /**
236  *
237  * @brief Set next buffer for certain layer
238  *
239  * @param[in] ptr LCD base address
240  * @param[in] layer_index target layer to be configured
241  * @param[in] buffer display buffer to be set
242  */
lcdc_layer_set_next_buffer(LCDC_Type * ptr,uint32_t layer_index,uint32_t buffer)243 static inline void lcdc_layer_set_next_buffer(LCDC_Type *ptr, uint32_t layer_index, uint32_t buffer)
244 {
245     ptr->LAYER[layer_index].START0 = LCDC_LAYER_START0_ADDR0_SET(buffer);
246     ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
247 }
248 
249 /**
250  *
251  * @brief Update specific layer background
252  *
253  * @param[in] ptr LCD base address
254  * @param[in] layer_index target layer to be configured
255  * @param[in] background color to be set as background
256  */
lcdc_layer_update_background(LCDC_Type * ptr,uint8_t layer_index,display_color_32b_t background)257 static inline void lcdc_layer_update_background(LCDC_Type *ptr,
258                 uint8_t layer_index, display_color_32b_t background)
259 {
260     ptr->LAYER[layer_index].BG_CL = LCDC_LAYER_BG_CL_ARGB_SET(background.u);
261     ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
262 }
263 
264 /**
265  *
266  * @brief Update specific layer position
267  *
268  * @param[in] ptr LCD base address
269  * @param[in] layer_index target layer to be configured
270  * @param[in] x Position X coord
271  * @param[in] y Position Y coord
272  */
lcdc_layer_update_position(LCDC_Type * ptr,uint8_t layer_index,uint16_t x,uint32_t y)273 static inline void lcdc_layer_update_position(LCDC_Type *ptr,
274         uint8_t layer_index, uint16_t x, uint32_t y)
275 {
276     ptr->LAYER[layer_index].LAYPOS = LCDC_LAYER_LAYPOS_X_SET(x)
277         | LCDC_LAYER_LAYPOS_Y_SET(y);
278     ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
279 }
280 
281 /**
282  *
283  * @brief Update specific layer dimension
284  *
285  * @param[in] ptr LCD base address
286  * @param[in] layer_index target layer to be configured
287  * @param[in] width Width in pixel
288  * @param[in] height Height in pixel
289  */
lcdc_layer_update_dimension(LCDC_Type * ptr,uint8_t layer_index,uint8_t width,uint8_t height)290 static inline void lcdc_layer_update_dimension(LCDC_Type *ptr,
291         uint8_t layer_index, uint8_t width, uint8_t height)
292 {
293     ptr->LAYER[layer_index].LAYSIZE = LCDC_LAYER_LAYSIZE_WIDTH_SET(width)
294         | LCDC_LAYER_LAYSIZE_HEIGHT_SET(height);
295     ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
296 }
297 
298 /**
299  *
300  * @brief Update specific layer region
301  *
302  * @param[in] ptr LCD base address
303  * @param[in] layer_index target layer to be configured
304  * @param[in] x1 X coord of the top left pixel
305  * @param[in] y1 Y coord of the top left pixel
306  * @param[in] x2 X coord of the bottom right pixel
307  * @param[in] y2 Y coord of the bottom right pixel
308  */
lcdc_layer_set_region(LCDC_Type * ptr,uint8_t layer_index,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)309 static inline void lcdc_layer_set_region(LCDC_Type *ptr, uint8_t layer_index,
310                         uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
311 {
312     ptr->LAYER[layer_index].LAYPOS = LCDC_LAYER_LAYPOS_X_SET(x1)
313         | LCDC_LAYER_LAYPOS_Y_SET(y1);
314     ptr->LAYER[layer_index].LAYSIZE = LCDC_LAYER_LAYSIZE_WIDTH_SET(x2 - x1 + 1)
315         | LCDC_LAYER_LAYSIZE_HEIGHT_SET(y2 - y1 + 1);
316     ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
317 }
318 
319 /**
320  *
321  * @brief Update specific layer configuration
322  *
323  * @param[in] ptr LCD base address
324  * @param[in] layer_index target layer to be configured
325  */
lcdc_layer_update(LCDC_Type * ptr,uint8_t layer_index)326 static inline void lcdc_layer_update(LCDC_Type *ptr, uint8_t layer_index)
327 {
328     ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
329 }
330 
331 /**
332  *
333  * @brief Enable specific layer
334  *
335  * @param[in] ptr LCD base address
336  * @param[in] layer_index target layer to be configured
337  */
lcdc_layer_enable(LCDC_Type * ptr,uint32_t layer_index)338 static inline void lcdc_layer_enable(LCDC_Type *ptr, uint32_t layer_index)
339 {
340     ptr->LAYER[layer_index].LAYCTRL |=
341         (LCDC_LAYER_LAYCTRL_EN_MASK | LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK);
342 }
343 
344 /**
345  *
346  * @brief Disable specific layer
347  *
348  * @param[in] ptr LCD base address
349  * @param[in] layer_index target layer to be configured
350  */
lcdc_layer_disable(LCDC_Type * ptr,uint32_t layer_index)351 static inline void lcdc_layer_disable(LCDC_Type *ptr, uint32_t layer_index)
352 {
353     ptr->LAYER[layer_index].LAYCTRL =
354         (ptr->LAYER[layer_index].LAYCTRL & (~LCDC_LAYER_LAYCTRL_EN_MASK))
355         | LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
356 }
357 
358 /**
359  *
360  * @brief Set test mode
361  *
362  * @param[in] ptr LCD base address
363  * @param[in] test_mode target test mode to be enabled
364  */
lcdc_set_testmode(LCDC_Type * ptr,uint8_t test_mode)365 static inline void lcdc_set_testmode(LCDC_Type *ptr, uint8_t test_mode)
366 {
367     ptr->CTRL = ((ptr->CTRL & ~LCDC_CTRL_DISP_MODE_MASK))
368         | LCDC_CTRL_DISP_MODE_SET(test_mode)
369         | LCDC_CTRL_DISP_ON_MASK;
370 }
371 
372 /**
373  *
374  * @brief Set background
375  *
376  * @param[in] ptr LCD base address
377  * @param[in] color background color
378  */
lcdc_set_background(LCDC_Type * ptr,display_color_32b_t color)379 static inline void lcdc_set_background(LCDC_Type *ptr,
380                                        display_color_32b_t color)
381 {
382     ptr->BGND_CL = LCDC_BGND_CL_R_SET(color.r)
383         | LCDC_BGND_CL_G_SET(color.g)
384         | LCDC_BGND_CL_B_SET(color.b);
385 }
386 
387 /**
388  *
389  * @brief enable background on alpha blender
390  *
391  * @note it not depend the background color of the layer itself. it can be used with lcdc_set_background API
392  *
393  * @param[in] ptr LCD base address
394  */
lcdc_enable_background_in_alpha_blender(LCDC_Type * ptr)395 static inline void lcdc_enable_background_in_alpha_blender(LCDC_Type *ptr)
396 {
397     ptr->CTRL |= LCDC_CTRL_BGDCL4CLR_MASK;
398 }
399 
400 /**
401  *
402  * @brief disable background on alpha blender
403  *
404  * @note if not use background but want depend the the background color of the layer itself, can be use the API
405  *
406  * @param[in] ptr LCD base address
407  */
lcdc_disable_background_in_alpha_blender(LCDC_Type * ptr)408 static inline void lcdc_disable_background_in_alpha_blender(LCDC_Type *ptr)
409 {
410     ptr->CTRL &= ~LCDC_CTRL_BGDCL4CLR_MASK;
411 }
412 /**
413  *
414  * @brief Get default layer configuration value
415  *
416  * @param[in] ptr LCD base address
417  * @param[out] layer Pointer of layer configuration struct buffer
418  * @param[in] pixel_format Pixel format to be used for this layer
419  * @param[in] layer_index target layer to be configured
420  */
421 void lcdc_get_default_layer_config(LCDC_Type *ptr,
422                                    lcdc_layer_config_t *layer, display_pixel_format_t pixel_format, uint8_t layer_index);
423 
424 /**
425  *
426  * @brief Get default configuration value
427  *
428  * @param[in] ptr LCD base address
429  * @param[out] config Pointer of configuration struct buffer
430  */
431 void lcdc_get_default_config(LCDC_Type *ptr, lcdc_config_t *config);
432 
433 /**
434  *
435  * @brief Initialize LCD controller
436  *
437  * @param[in] ptr LCD base address
438  * @param[in] config Pointer of configuration struct buffer
439  */
440 void lcdc_init(LCDC_Type *ptr, lcdc_config_t *config);
441 
442 /**
443  *
444  * @brief Configure specific layer
445  *
446  * @param[in] ptr LCD base address
447  * @param[in] layer_index target layer to be configured
448  * @param[in] layer_config Pointer of layer configuration struct buffer
449  * @param[in] enable_layer Set true if the layer needs to be enabled right after being configured
450  */
451 hpm_stat_t lcdc_config_layer(LCDC_Type *ptr, uint8_t layer_index,
452                        lcdc_layer_config_t *layer_config, bool enable_layer);
453 
454 /**
455  *
456  * @brief Turn on display
457  *
458  * @param[in] ptr LCD base address
459  */
460 void lcdc_turn_on_display(LCDC_Type *ptr);
461 
462 /**
463  *
464  * @brief Turn off display
465  *
466  * @param[in] ptr LCD base address
467  */
468 void lcdc_turn_off_display(LCDC_Type *ptr);
469 
470 #ifdef __cplusplus
471 }
472 #endif
473 /**
474  * @}
475  */
476 
477 #endif /* HPM_LCDC_DRV_H */
478