blob: 25b26b8780e0b6fd7878021cdeafc050b4964389 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Carlos Pizano-Uribe <cpu@chromium.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23/*
24 * COPYRIGHT(c) 2015 STMicroelectronics
25 *
26 * Redistribution and use in source and binary forms, with or without modification,
27 * are permitted provided that the following conditions are met:
28 * 1. Redistributions of source code must retain the above copyright notice,
29 * this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright notice,
31 * this list of conditions and the following disclaimer in the documentation
32 * and/or other materials provided with the distribution.
33 * 3. Neither the name of STMicroelectronics nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 ******************************************************************************
49 */
50
51#include <err.h>
52#include <debug.h>
53#include <trace.h>
54#include <target.h>
55#include <compiler.h>
56#include <string.h>
57#include <lib/gfx.h>
58#include <dev/gpio.h>
59#include <dev/display.h>
60#include <kernel/novm.h>
61#include <platform/stm32.h>
62
63/*
64 * lcd initialization sequence, taken from
65 * STM32Cube_FW_F7_V1.1.0/Drivers/BSP/STM32746G_Disco/stm32746g_discovery_lcd.[ch]
66 *
67 * This code only applies to The RK043FN48H LCD 480x272.
68 */
69
70/**
71 * @brief RK043FN48H Size
72 */
73#define RK043FN48H_WIDTH ((uint16_t)480) /* LCD PIXEL WIDTH */
74#define RK043FN48H_HEIGHT ((uint16_t)272) /* LCD PIXEL HEIGHT */
75
76/**
77 * @brief RK043FN48H Timing
78 */
79#define RK043FN48H_HSYNC ((uint16_t)41) /* Horizontal synchronization */
80#define RK043FN48H_HBP ((uint16_t)13) /* Horizontal back porch */
81#define RK043FN48H_HFP ((uint16_t)32) /* Horizontal front porch */
82#define RK043FN48H_VSYNC ((uint16_t)10) /* Vertical synchronization */
83#define RK043FN48H_VBP ((uint16_t)2) /* Vertical back porch */
84#define RK043FN48H_VFP ((uint16_t)2) /* Vertical front porch */
85
86
87/**
88 * @brief LCD special pins
89 */
90/* Display enable pin */
91#define LCD_DISP_PIN GPIO_PIN_12
92#define LCD_DISP_GPIO_PORT GPIOI
93/* Backlight control pin */
94#define LCD_BL_CTRL_PIN GPIO_PIN_3
95#define LCD_BL_CTRL_GPIO_PORT GPIOK
96
97#define LCD_DISP_GPIO_CLK_ENABLE() __HAL_RCC_GPIOI_CLK_ENABLE()
98#define LCD_DISP_GPIO_CLK_DISABLE() __HAL_RCC_GPIOI_CLK_DISABLE()
99#define LCD_BL_CTRL_GPIO_CLK_ENABLE() __HAL_RCC_GPIOK_CLK_ENABLE()
100#define LCD_BL_CTRL_GPIO_CLK_DISABLE() __HAL_RCC_GPIOK_CLK_DISABLE()
101
102/**
103 * @brief RK043FN48H frequency divider
104 */
105#define RK043FN48H_FREQUENCY_DIVIDER 5 /* LCD Frequency divider */
106
107/** @defgroup STM32746G_DISCOVERY_LCD_Exported_Constants
108 * @{
109 */
110#define MAX_LAYER_NUMBER ((uint32_t)2)
111
112#define LCD_LayerCfgTypeDef LTDC_LayerCfgTypeDef
113
114#define LTDC_ACTIVE_LAYER ((uint32_t)1) /* Layer 1 */
115/**
116 * @brief LCD status structure definition
117 */
118#define LCD_OK ((uint8_t)0x00)
119#define LCD_ERROR ((uint8_t)0x01)
120#define LCD_TIMEOUT ((uint8_t)0x02)
121
122
123static LTDC_HandleTypeDef ltdc_handle;
124
125/* Default LCD configuration with LCD Layer 1 */
126static uint32_t active_layer = 0;
127
128/**
129 * @brief Gets the LCD X size.
130 * @retval Used LCD X size
131 */
132static uint32_t BSP_LCD_GetXSize(void)
133{
134 return ltdc_handle.LayerCfg[active_layer].ImageWidth;
135}
136
137/**
138 * @brief Gets the LCD Y size.
139 * @retval Used LCD Y size
140 */
141static uint32_t BSP_LCD_GetYSize(void)
142{
143 return ltdc_handle.LayerCfg[active_layer].ImageHeight;
144}
145
146static size_t BSP_LCD_PixelSize(void)
147{
148 return (ltdc_handle.LayerCfg[active_layer].PixelFormat ==
149 LTDC_PIXEL_FORMAT_ARGB8888) ? 4 : 2;
150}
151
152/**
153 * @brief Initializes the LCD layer in ARGB8888 format (32 bits per pixel).
154 * @param LayerIndex: Layer foreground or background
155 * @param FB_Address: Layer frame buffer
156 * @retval None
157 */
158static void BSP_LCD_LayerDefaultInit(uint16_t LayerIndex, uint32_t FB_Address)
159{
160 LCD_LayerCfgTypeDef layer_cfg;
161
162 /* Layer Init */
163 layer_cfg.WindowX0 = 0;
164 layer_cfg.WindowX1 = BSP_LCD_GetXSize();
165 layer_cfg.WindowY0 = 0;
166 layer_cfg.WindowY1 = BSP_LCD_GetYSize();
167 layer_cfg.PixelFormat = LTDC_PIXEL_FORMAT_ARGB8888;
168 layer_cfg.FBStartAdress = FB_Address;
169 layer_cfg.Alpha = 255;
170 layer_cfg.Alpha0 = 0;
171 layer_cfg.Backcolor.Blue = 0;
172 layer_cfg.Backcolor.Green = 0;
173 layer_cfg.Backcolor.Red = 0;
174 layer_cfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
175 layer_cfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
176 layer_cfg.ImageWidth = BSP_LCD_GetXSize();
177 layer_cfg.ImageHeight = BSP_LCD_GetYSize();
178
179 HAL_LTDC_ConfigLayer(&ltdc_handle, &layer_cfg, LayerIndex);
180}
181
182/**
183 * @brief Selects the LCD Layer.
184 * @param LayerIndex: Layer foreground or background
185 * @retval None
186 */
187void BSP_LCD_SelectLayer(uint32_t LayerIndex)
188{
189 active_layer = LayerIndex;
190}
191
192/**
193 * @brief Sets an LCD Layer visible
194 * @param LayerIndex: Visible Layer
195 * @param State: New state of the specified layer
196 * This parameter can be one of the following values:
197 * @arg ENABLE
198 * @arg DISABLE
199 * @retval None
200 */
201void BSP_LCD_SetLayerVisible(uint32_t LayerIndex, FunctionalState State)
202{
203 if(State == ENABLE) {
204 __HAL_LTDC_LAYER_ENABLE(&ltdc_handle, LayerIndex);
205 } else {
206 __HAL_LTDC_LAYER_DISABLE(&ltdc_handle, LayerIndex);
207 }
208 __HAL_LTDC_RELOAD_CONFIG(&ltdc_handle);
209}
210
211/**
212 * @brief Sets an LCD layer frame buffer address.
213 * @param LayerIndex: Layer foreground or background
214 * @param Address: New LCD frame buffer value
215 * @retval None
216 */
217void BSP_LCD_SetLayerAddress(uint32_t LayerIndex, uint32_t Address)
218{
219 HAL_LTDC_SetAddress(&ltdc_handle, Address, LayerIndex);
220}
221
222/**
223 * @brief Enables the display.
224 * @retval None
225 */
226void BSP_LCD_DisplayOn(void)
227{
228 /* Display On */
229 __HAL_LTDC_ENABLE(&ltdc_handle);
230 HAL_GPIO_WritePin(LCD_DISP_GPIO_PORT, LCD_DISP_PIN, GPIO_PIN_SET);
231 HAL_GPIO_WritePin(LCD_BL_CTRL_GPIO_PORT, LCD_BL_CTRL_PIN, GPIO_PIN_SET);
232}
233
234/**
235 * @brief Disables the display.
236 * @retval None
237 */
238void BSP_LCD_DisplayOff(void)
239{
240 /* Display Off */
241 __HAL_LTDC_DISABLE(&ltdc_handle);
242 HAL_GPIO_WritePin(LCD_DISP_GPIO_PORT, LCD_DISP_PIN, GPIO_PIN_RESET);
243 HAL_GPIO_WritePin(LCD_BL_CTRL_GPIO_PORT, LCD_BL_CTRL_PIN, GPIO_PIN_RESET);
244}
245
246/**
247 * @brief Initializes the LTDC MSP.
248 * @param hltdc: LTDC handle
249 * @param Params
250 * @retval None
251 */
252static void BSP_LCD_MspInit(LTDC_HandleTypeDef *hltdc, void *Params)
253{
254 GPIO_InitTypeDef gpio_init_structure;
255
256 /* Enable the LTDC and DMA2D clocks */
257 __HAL_RCC_LTDC_CLK_ENABLE();
258 __HAL_RCC_DMA2D_CLK_ENABLE();
259
260 /* Enable GPIOs clock */
261 __HAL_RCC_GPIOE_CLK_ENABLE();
262 __HAL_RCC_GPIOG_CLK_ENABLE();
263 __HAL_RCC_GPIOI_CLK_ENABLE();
264 __HAL_RCC_GPIOJ_CLK_ENABLE();
265 __HAL_RCC_GPIOK_CLK_ENABLE();
266 LCD_DISP_GPIO_CLK_ENABLE();
267 LCD_BL_CTRL_GPIO_CLK_ENABLE();
268
269 /*** LTDC Pins configuration ***/
270 /* GPIOE configuration */
271 gpio_init_structure.Pin = GPIO_PIN_4;
272 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
273 gpio_init_structure.Pull = GPIO_NOPULL;
274 gpio_init_structure.Speed = GPIO_SPEED_FAST;
275 gpio_init_structure.Alternate = GPIO_AF14_LTDC;
276 HAL_GPIO_Init(GPIOE, &gpio_init_structure);
277
278 /* GPIOG configuration */
279 gpio_init_structure.Pin = GPIO_PIN_12;
280 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
281 gpio_init_structure.Alternate = GPIO_AF9_LTDC;
282 HAL_GPIO_Init(GPIOG, &gpio_init_structure);
283
284 /* GPIOI LTDC alternate configuration */
285 gpio_init_structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | \
286 GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
287 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
288 gpio_init_structure.Alternate = GPIO_AF14_LTDC;
289 HAL_GPIO_Init(GPIOI, &gpio_init_structure);
290
291 /* GPIOJ configuration */
292 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | \
293 GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | \
294 GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | \
295 GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
296 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
297 gpio_init_structure.Alternate = GPIO_AF14_LTDC;
298 HAL_GPIO_Init(GPIOJ, &gpio_init_structure);
299
300 /* GPIOK configuration */
301 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 | \
302 GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
303 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
304 gpio_init_structure.Alternate = GPIO_AF14_LTDC;
305 HAL_GPIO_Init(GPIOK, &gpio_init_structure);
306
307 /* LCD_DISP GPIO configuration
308 Note that LCD_DISP pin has to be manually controlled.
309 */
310 gpio_init_structure.Pin = LCD_DISP_PIN;
311 gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
312 HAL_GPIO_Init(LCD_DISP_GPIO_PORT, &gpio_init_structure);
313
314 /* LCD_BL_CTRL GPIO configuration
315 Note that LCD_BL_CTRL pin has to be manually controlled.
316 */
317 gpio_init_structure.Pin = LCD_BL_CTRL_PIN;
318 gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
319 HAL_GPIO_Init(LCD_BL_CTRL_GPIO_PORT, &gpio_init_structure);
320}
321
322/**
323 * @brief Clock Config.
324 * @param hltdc: LTDC handle
325 * @param Params
326 * @note This API is called by BSP_LCD_Init()
327 * @retval None
328 */
329static void BSP_LCD_ClockConfig(LTDC_HandleTypeDef *hltdc, void *Params)
330{
331 static RCC_PeriphCLKInitTypeDef periph_clk_init_struct;
332
333 /* RK043FN48H LCD clock configuration */
334 /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
335 /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN = 192 Mhz */
336 /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAIR = 192/5 = 38.4 Mhz */
337 /* LTDC clock frequency = PLLLCDCLK / LTDC_PLLSAI_DIVR_4 = 38.4/4 = 9.6Mhz */
338 periph_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_LTDC;
339 periph_clk_init_struct.PLLSAI.PLLSAIN = 192;
340 periph_clk_init_struct.PLLSAI.PLLSAIR = RK043FN48H_FREQUENCY_DIVIDER;
341 periph_clk_init_struct.PLLSAIDivR = RCC_PLLSAIDIVR_4;
342 HAL_RCCEx_PeriphCLKConfig(&periph_clk_init_struct);
343}
344
345/**
346 * @brief Initializes the LCD.
347 * @retval LCD state
348 */
349uint8_t BSP_LCD_Init(void)
350{
351 /* Timing Configuration */
352 ltdc_handle.Init.HorizontalSync = (RK043FN48H_HSYNC - 1);
353 ltdc_handle.Init.VerticalSync = (RK043FN48H_VSYNC - 1);
354 ltdc_handle.Init.AccumulatedHBP = (RK043FN48H_HSYNC + RK043FN48H_HBP - 1);
355 ltdc_handle.Init.AccumulatedVBP = (RK043FN48H_VSYNC + RK043FN48H_VBP - 1);
356 ltdc_handle.Init.AccumulatedActiveH = (RK043FN48H_HEIGHT + RK043FN48H_VSYNC + RK043FN48H_VBP - 1);
357 ltdc_handle.Init.AccumulatedActiveW = (RK043FN48H_WIDTH + RK043FN48H_HSYNC + RK043FN48H_HBP - 1);
358 ltdc_handle.Init.TotalHeigh = (RK043FN48H_HEIGHT + RK043FN48H_VSYNC + RK043FN48H_VBP + RK043FN48H_VFP - 1);
359 ltdc_handle.Init.TotalWidth = (RK043FN48H_WIDTH + RK043FN48H_HSYNC + RK043FN48H_HBP + RK043FN48H_HFP - 1);
360
361 /* LCD clock configuration */
362 BSP_LCD_ClockConfig(&ltdc_handle, NULL);
363
364 /* Initialize the LCD pixel width and pixel height */
365 ltdc_handle.LayerCfg->ImageWidth = RK043FN48H_WIDTH;
366 ltdc_handle.LayerCfg->ImageHeight = RK043FN48H_HEIGHT;
367
368 /* Background value */
369 ltdc_handle.Init.Backcolor.Blue = 0;
370 ltdc_handle.Init.Backcolor.Green = 0;
371 ltdc_handle.Init.Backcolor.Red = 0;
372
373 /* Polarity */
374 ltdc_handle.Init.HSPolarity = LTDC_HSPOLARITY_AL;
375 ltdc_handle.Init.VSPolarity = LTDC_VSPOLARITY_AL;
376 ltdc_handle.Init.DEPolarity = LTDC_DEPOLARITY_AL;
377 ltdc_handle.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
378 ltdc_handle.Instance = LTDC;
379
380 if (HAL_LTDC_GetState(&ltdc_handle) == HAL_LTDC_STATE_RESET) {
381 BSP_LCD_MspInit(&ltdc_handle, NULL);
382 }
383
384 HAL_LTDC_Init(&ltdc_handle);
385
386 /* allocate the framebuffer */
387 size_t fb_size_pages = PAGE_ALIGN(RK043FN48H_WIDTH * RK043FN48H_HEIGHT * 4) / PAGE_SIZE;
388 void *fb_address = novm_alloc_pages(fb_size_pages, NOVM_ARENA_SECONDARY);
389 if (!fb_address)
390 panic("failed to allocate framebuffer for LCD\n");
391
392 BSP_LCD_LayerDefaultInit(0, (uint32_t)fb_address);
393 BSP_LCD_SelectLayer(0);
394
395 /* clear framebuffer */
396 memset((void *)ltdc_handle.LayerCfg[active_layer].FBStartAdress, 0,
397 BSP_LCD_GetXSize() * BSP_LCD_GetYSize() * BSP_LCD_PixelSize());
398
399 /* turn the display on */
400 BSP_LCD_DisplayOn();
401 return LCD_OK;
402}
403
404/* LK display (lib/gfx.h) calls this function */
405status_t display_get_info(struct display_info *info)
406{
407 info->framebuffer = (void *)ltdc_handle.LayerCfg[active_layer].FBStartAdress;
408
409 if (ltdc_handle.LayerCfg[active_layer].PixelFormat == LTDC_PIXEL_FORMAT_ARGB8888) {
410 info->format = GFX_FORMAT_ARGB_8888;
411 } else if (ltdc_handle.LayerCfg[active_layer].PixelFormat == LTDC_PIXEL_FORMAT_RGB565) {
412 info->format = GFX_FORMAT_RGB_565;
413 } else {
414 panic("unhandled pixel format\n");
415 return ERR_NOT_FOUND;
416 }
417
418 info->width = BSP_LCD_GetXSize();
419 info->height = BSP_LCD_GetYSize();
420 info->stride = BSP_LCD_GetXSize();
421 info->flush = NULL;
422
423 return NO_ERROR;
424}
425