drivers: display: elcdif: Implement set_format API

Implement the set_format API so that applications can change format
at runtime instead of using the predefined one in the device tree.

Signed-off-by: Phi Bang Nguyen <phibang.nguyen@nxp.com>
This commit is contained in:
Phi Bang Nguyen 2024-02-19 18:55:27 +01:00 committed by Fabio Baltieri
parent f5e970de3f
commit 080f26f4d4
2 changed files with 70 additions and 51 deletions

View file

@ -32,6 +32,16 @@ config MCUX_ELCDIF_FB_NUM
implications of this concern you, leave at least one driver
framebuffer enabled.
config MCUX_ELCDIF_FB_SIZE
int "Framebuffer size required by the eLCDIF driver"
default 3686400
help
eLCDIF driver allocates framebuffers to support partial display updates.
The framebuffer size is computed as : panel_width * panel_height * bpp.
The default value is set to afford for a default resolution of 1280x720 and
4-bytes pixel format, e.g. ARGB8888. Applications should change this value
according to the actual used resolution and format to optimize the heap size.
config MCUX_ELCDIF_PXP
bool "Use PXP for display rotation"
depends on MCUX_PXP

View file

@ -27,18 +27,17 @@
LOG_MODULE_REGISTER(display_mcux_elcdif, CONFIG_DISPLAY_LOG_LEVEL);
/* Define the heap size. 512 bytes of padding are included for kernel heap structures */
K_HEAP_DEFINE(display_heap, CONFIG_MCUX_ELCDIF_FB_NUM * CONFIG_MCUX_ELCDIF_FB_SIZE + 512);
static const uint32_t supported_fmts = PIXEL_FORMAT_BGR_565 | PIXEL_FORMAT_ARGB_8888;
struct mcux_elcdif_config {
LCDIF_Type *base;
void (*irq_config_func)(const struct device *dev);
elcdif_rgb_mode_config_t rgb_mode;
enum display_pixel_format pixel_format;
size_t pixel_bytes;
size_t fb_bytes;
const struct pinctrl_dev_config *pincfg;
const struct gpio_dt_spec backlight_gpio;
uint8_t *fb_ptr;
const struct device *pxp;
};
@ -47,6 +46,10 @@ struct mcux_elcdif_data {
const uint8_t *active_fb;
/* Pointers to driver allocated framebuffers */
uint8_t *fb[CONFIG_MCUX_ELCDIF_FB_NUM];
enum display_pixel_format pixel_format;
size_t pixel_bytes;
size_t fb_bytes;
elcdif_rgb_mode_config_t rgb_mode;
struct k_sem sem;
/* Tracks index of next active driver framebuffer */
uint8_t next_idx;
@ -77,7 +80,7 @@ static int mcux_elcdif_write(const struct device *dev, const uint16_t x, const u
int ret = 0;
bool full_fb = false;
__ASSERT((config->pixel_bytes * desc->pitch * desc->height) <= desc->buf_size,
__ASSERT((dev_data->pixel_bytes * desc->pitch * desc->height) <= desc->buf_size,
"Input buffer too small");
LOG_DBG("W=%d, H=%d, @%d,%d", desc->width, desc->height, x, y);
@ -110,17 +113,17 @@ static int mcux_elcdif_write(const struct device *dev, const uint16_t x, const u
*/
src = dev_data->active_fb;
dst = dev_data->fb[dev_data->next_idx];
memcpy(dst, src, config->fb_bytes);
memcpy(dst, src, dev_data->fb_bytes);
}
/* Now, write the display update into active framebuffer */
src = buf;
dst = dev_data->fb[dev_data->next_idx];
dst += config->pixel_bytes * (y * config->rgb_mode.panelWidth + x);
dst += dev_data->pixel_bytes * (y * config->rgb_mode.panelWidth + x);
for (h_idx = 0; h_idx < desc->height; h_idx++) {
memcpy(dst, src, config->pixel_bytes * desc->width);
src += config->pixel_bytes * desc->pitch;
dst += config->pixel_bytes * config->rgb_mode.panelWidth;
memcpy(dst, src, dev_data->pixel_bytes * desc->width);
src += dev_data->pixel_bytes * desc->pitch;
dst += dev_data->pixel_bytes * config->rgb_mode.panelWidth;
}
LOG_DBG("Setting FB from %p->%p", (void *)dev_data->active_fb,
@ -130,7 +133,7 @@ static int mcux_elcdif_write(const struct device *dev, const uint16_t x, const u
}
#ifdef CONFIG_HAS_MCUX_CACHE
DCACHE_CleanByRange((uint32_t)dev_data->active_fb, config->fb_bytes);
DCACHE_CleanByRange((uint32_t)dev_data->active_fb, dev_data->fb_bytes);
#endif
#ifdef CONFIG_MCUX_ELCDIF_PXP
@ -148,11 +151,11 @@ static int mcux_elcdif_write(const struct device *dev, const uint16_t x, const u
pxp_block.block_size = desc->buf_size;
/* DMA slot sets pixel format and rotation angle */
if (config->pixel_format == PIXEL_FORMAT_BGR_565) {
if (dev_data->pixel_format == PIXEL_FORMAT_BGR_565) {
pxp_dma.dma_slot = DMA_MCUX_PXP_FMT(DMA_MCUX_PXP_FMT_RGB565);
} else if (config->pixel_format == PIXEL_FORMAT_RGB_888) {
} else if (dev_data->pixel_format == PIXEL_FORMAT_RGB_888) {
pxp_dma.dma_slot = DMA_MCUX_PXP_FMT(DMA_MCUX_PXP_FMT_RGB888);
} else if (config->pixel_format == PIXEL_FORMAT_ARGB_8888) {
} else if (dev_data->pixel_format == PIXEL_FORMAT_ARGB_8888) {
pxp_dma.dma_slot = DMA_MCUX_PXP_FMT(DMA_MCUX_PXP_FMT_ARGB8888);
} else {
/* Cannot rotate */
@ -169,8 +172,8 @@ static int mcux_elcdif_write(const struct device *dev, const uint16_t x, const u
}
pxp_dma.channel_direction = MEMORY_TO_MEMORY;
pxp_dma.source_data_size = desc->width * config->pixel_bytes;
pxp_dma.dest_data_size = config->rgb_mode.panelWidth * config->pixel_bytes;
pxp_dma.source_data_size = desc->width * dev_data->pixel_bytes;
pxp_dma.dest_data_size = config->rgb_mode.panelWidth * dev_data->pixel_bytes;
/* Burst lengths are heights of source/dest buffer in pixels */
pxp_dma.source_burst_length = desc->height;
pxp_dma.dest_burst_length = config->rgb_mode.panelHeight;
@ -221,13 +224,42 @@ static int mcux_elcdif_display_blanking_on(const struct device *dev)
static int mcux_elcdif_set_pixel_format(const struct device *dev,
const enum display_pixel_format pixel_format)
{
struct mcux_elcdif_data *dev_data = dev->data;
const struct mcux_elcdif_config *config = dev->config;
if (pixel_format == config->pixel_format) {
return 0;
if (!(pixel_format & supported_fmts)) {
LOG_ERR("Unsupported pixel format");
return -ENOTSUP;
}
LOG_ERR("Pixel format change not implemented");
return -ENOTSUP;
dev_data->pixel_format = pixel_format;
dev_data->pixel_bytes = DISPLAY_BITS_PER_PIXEL(pixel_format) / 8;
dev_data->fb_bytes =
config->rgb_mode.panelWidth * config->rgb_mode.panelHeight * dev_data->pixel_bytes;
for (int i = 0; i < CONFIG_MCUX_ELCDIF_FB_NUM; i++) {
k_heap_free(&display_heap, dev_data->fb[i]);
dev_data->fb[i] =
k_heap_aligned_alloc(&display_heap, 64, dev_data->fb_bytes, K_FOREVER);
if (dev_data->fb[i] == NULL) {
LOG_ERR("Could not allocate memory for framebuffers");
return -ENOMEM;
}
memset(dev_data->fb[i], 0, dev_data->fb_bytes);
}
dev_data->rgb_mode = config->rgb_mode;
if (pixel_format == PIXEL_FORMAT_BGR_565) {
dev_data->rgb_mode.pixelFormat = kELCDIF_PixelFormatRGB565;
} else if (pixel_format == PIXEL_FORMAT_RGB_888) {
dev_data->rgb_mode.pixelFormat = kELCDIF_PixelFormatRGB888;
} else if (pixel_format == PIXEL_FORMAT_ARGB_8888) {
dev_data->rgb_mode.pixelFormat = kELCDIF_PixelFormatXRGB8888;
}
ELCDIF_RgbModeSetPixelFormat(config->base, dev_data->rgb_mode.pixelFormat);
return 0;
}
static int mcux_elcdif_set_orientation(const struct device *dev,
@ -249,7 +281,7 @@ static void mcux_elcdif_get_capabilities(const struct device *dev,
capabilities->x_resolution = config->rgb_mode.panelWidth;
capabilities->y_resolution = config->rgb_mode.panelHeight;
capabilities->supported_pixel_formats = supported_fmts;
capabilities->current_pixel_format = config->pixel_format;
capabilities->current_pixel_format = ((struct mcux_elcdif_data *)dev->data)->pixel_format;
capabilities->current_orientation = DISPLAY_ORIENTATION_NORMAL;
}
@ -286,25 +318,6 @@ static int mcux_elcdif_init(const struct device *dev)
return err;
}
elcdif_rgb_mode_config_t rgb_mode = config->rgb_mode;
/* Set the Pixel format */
if (config->pixel_format == PIXEL_FORMAT_BGR_565) {
rgb_mode.pixelFormat = kELCDIF_PixelFormatRGB565;
} else if (config->pixel_format == PIXEL_FORMAT_RGB_888) {
rgb_mode.pixelFormat = kELCDIF_PixelFormatRGB888;
} else if (config->pixel_format == PIXEL_FORMAT_ARGB_8888) {
rgb_mode.pixelFormat = kELCDIF_PixelFormatXRGB8888;
}
for (int i = 0; i < CONFIG_MCUX_ELCDIF_FB_NUM; i++) {
/* Record pointers to each driver framebuffer */
dev_data->fb[i] = config->fb_ptr + (config->fb_bytes * i);
}
rgb_mode.bufferAddr = (uint32_t)config->fb_ptr;
dev_data->active_fb = config->fb_ptr;
k_sem_init(&dev_data->sem, 0, 1);
#ifdef CONFIG_MCUX_ELCDIF_PXP
k_sem_init(&dev_data->pxp_done, 0, 1);
@ -316,7 +329,12 @@ static int mcux_elcdif_init(const struct device *dev)
config->irq_config_func(dev);
ELCDIF_RgbModeInit(config->base, &rgb_mode);
/* Set default pixel format obtained from device tree */
mcux_elcdif_set_pixel_format(dev, dev_data->pixel_format);
dev_data->active_fb = dev_data->fb[0];
ELCDIF_RgbModeInit(config->base, &dev_data->rgb_mode);
ELCDIF_RgbModeStart(config->base);
return 0;
@ -331,14 +349,9 @@ static const struct display_driver_api mcux_elcdif_api = {
.set_orientation = mcux_elcdif_set_orientation,
};
#define MCUX_ELCDIF_PIXEL_BYTES(id) (DISPLAY_BITS_PER_PIXEL(DT_INST_PROP(id, pixel_format)) / 8)
#define MCUX_ELCDIF_DEVICE_INIT(id) \
PINCTRL_DT_INST_DEFINE(id); \
static void mcux_elcdif_config_func_##id(const struct device *dev); \
static uint8_t __aligned(64) \
frame_buffer_##id[CONFIG_MCUX_ELCDIF_FB_NUM * DT_INST_PROP(id, width) * \
DT_INST_PROP(id, height) * MCUX_ELCDIF_PIXEL_BYTES(id)]; \
static const struct mcux_elcdif_config mcux_elcdif_config_##id = { \
.base = (LCDIF_Type *)DT_INST_REG_ADDR(id), \
.irq_config_func = mcux_elcdif_config_func_##id, \
@ -369,17 +382,13 @@ static const struct display_driver_api mcux_elcdif_api = {
.dataBus = LCDIF_CTRL_LCD_DATABUS_WIDTH( \
DT_INST_ENUM_IDX(id, data_bus_width)), \
}, \
.pixel_format = DT_INST_PROP(id, pixel_format), \
.pixel_bytes = MCUX_ELCDIF_PIXEL_BYTES(id), \
.fb_bytes = DT_INST_PROP(id, width) * DT_INST_PROP(id, height) * \
MCUX_ELCDIF_PIXEL_BYTES(id), \
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(id), \
.backlight_gpio = GPIO_DT_SPEC_INST_GET(id, backlight_gpios), \
.fb_ptr = frame_buffer_##id, \
IF_ENABLED(CONFIG_MCUX_ELCDIF_PXP, \
(.pxp = DEVICE_DT_GET(DT_INST_PHANDLE(id, nxp_pxp)),))}; \
static struct mcux_elcdif_data mcux_elcdif_data_##id = { \
.next_idx = 0, \
.pixel_format = DT_INST_PROP(id, pixel_format), \
}; \
DEVICE_DT_INST_DEFINE(id, &mcux_elcdif_init, NULL, &mcux_elcdif_data_##id, \
&mcux_elcdif_config_##id, POST_KERNEL, CONFIG_DISPLAY_INIT_PRIORITY, \