* drivers/lcd/mio283qt9a.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/spi/spi.h>
#include <nuttx/lcd/lcd.h>
#include <nuttx/lcd/mio283qt9a.h>
#ifdef CONFIG_LCD_MIO283QT9A
* Pre-processor Definitions
****************************************************************************/
#if !defined(CONFIG_LCD_MAXCONTRAST)
# define CONFIG_LCD_MAXCONTRAST 1
#endif
#if !defined(CONFIG_LCD_MAXPOWER) || CONFIG_LCD_MAXPOWER < 1
# define CONFIG_LCD_MAXPOWER 1
#endif
#if CONFIG_LCD_MAXPOWER > 255
# error "CONFIG_LCD_MAXPOWER must be less than 256 to fit in uint8_t"
#endif
#if defined(CONFIG_LCD_PORTRAIT)
# if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE) || defined(CONFIG_LCD_RPORTRAIT)
# error "Cannot define both portrait and any other orientations"
# endif
#elif defined(CONFIG_LCD_RPORTRAIT)
# if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE)
# error "Cannot define both rportrait and any other orientations"
# endif
#elif defined(CONFIG_LCD_LANDSCAPE)
# ifdef CONFIG_LCD_RLANDSCAPE
# error "Cannot define both landscape and any other orientations"
# endif
#elif !defined(CONFIG_LCD_RLANDSCAPE)
# define CONFIG_LCD_LANDSCAPE 1
#endif
#if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE)
# define MIO283QT9A_XRES 320
# define MIO283QT9A_YRES 240
#else
# define MIO283QT9A_XRES 240
# define MIO283QT9A_YRES 320
#endif
#define MIO283QT9A_BPP 16
#define MIO283QT9A_COLORFMT FB_FMT_RGB16_565
* and values. Some recent experiences have shown me that during LCD bringup,
* it is more important to know the binary values rather than nice, people
* friendly names. Sad, but true.
*/
#define ILI9341_ID_1 0x93
#define ILI9341_ID_2 0x41
* Private Type Definition
****************************************************************************/
struct mio283qt9a_dev_s
{
struct lcd_dev_s dev;
FAR struct mio283qt9a_lcd_s *lcd;
uint8_t power;
* and for each color plane.
* This memory will hold one raster line of data.
* The size of the allocated run buffer must therefore be at least
* (bpp * xres / 8). Actual alignment of the buffer must conform to the
* bitwidth of the underlying pixel type.
*
* If there are multiple planes, they may share the same working buffer
* because different planes will not be operate on concurrently.
* However, if there are multiple LCD devices, they must each have unique
* run buffers.
*/
uint16_t runbuffer[MIO283QT9A_XRES];
};
* Private Function Prototypes
****************************************************************************/
static void mio283qt9a_putreg(FAR struct mio283qt9a_lcd_s *lcd,
uint8_t regaddr,
uint16_t regval);
#ifndef CONFIG_LCD_NOGETRUN
static uint16_t mio283qt9a_readreg(FAR struct mio283qt9a_lcd_s *lcd,
uint8_t regaddr);
#endif
static inline void mio283qt9a_gramwrite(FAR struct mio283qt9a_lcd_s *lcd,
uint16_t rgbcolor);
#ifndef CONFIG_LCD_NOGETRUN
static inline void mio283qt9a_readsetup(FAR struct mio283qt9a_lcd_s *lcd,
FAR uint16_t *accum);
static inline uint16_t mio283qt9a_gramread(FAR struct mio283qt9a_lcd_s *lcd,
FAR uint16_t *accum);
#endif
static void mio283qt9a_setarea(FAR struct mio283qt9a_lcd_s *lcd,
uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
static int mio283qt9a_putrun(FAR struct lcd_dev_s *dev,
fb_coord_t row, fb_coord_t col,
FAR const uint8_t *buffer,
size_t npixels);
static int mio283qt9a_getrun(FAR struct lcd_dev_s *dev,
fb_coord_t row, fb_coord_t col,
FAR uint8_t *buffer,
size_t npixels);
static int mio283qt9a_getvideoinfo(FAR struct lcd_dev_s *dev,
FAR struct fb_videoinfo_s *vinfo);
static int mio283qt9a_getplaneinfo(FAR struct lcd_dev_s *dev,
unsigned int planeno,
FAR struct lcd_planeinfo_s *pinfo);
#ifdef CONFIG_FB_CMAP
# error "RGB color mapping not supported by this driver"
#endif
#ifdef CONFIG_FB_HWCURSOR
# error "Cursor control not supported by this driver"
#endif
static int mio283qt9a_getpower(FAR struct lcd_dev_s *dev);
static int mio283qt9a_setpower(FAR struct lcd_dev_s *dev, int power);
static int mio283qt9a_getcontrast(FAR struct lcd_dev_s *dev);
static int mio283qt9a_setcontrast(FAR struct lcd_dev_s *dev,
unsigned int contrast);
static inline int mio283qt9a_hwinitialize(
FAR struct mio283qt9a_dev_s *priv);
* Private Data
****************************************************************************/
* The following is the single MIO283QT9A driver state instance:
*/
static struct mio283qt9a_dev_s g_lcddev;
* Private Functions
****************************************************************************/
* Name: mio283qt9a_putreg
*
* Description:
* Write to an LCD register
*
****************************************************************************/
static void mio283qt9a_putreg(FAR struct mio283qt9a_lcd_s *lcd,
uint8_t regaddr, uint16_t regval)
{
* contents
*/
lcd->index(lcd, regaddr);
lcd->write(lcd, regval);
}
* Name: mio283qt9a_readreg
*
* Description:
* Read from an LCD register
*
****************************************************************************/
#ifndef CONFIG_LCD_NOGETRUN
static uint16_t mio283qt9a_readreg(FAR struct mio283qt9a_lcd_s *lcd,
uint8_t regaddr)
{
* contents.
*/
lcd->index(lcd, regaddr);
return lcd->read(lcd);
}
#endif
* Name: mio283qt9a_gramselect_write
*
* Description:
* Setup to write multiple pixels to the GRAM memory
*
****************************************************************************/
static inline void mio283qt9a_gramselect_write(
FAR struct mio283qt9a_lcd_s *lcd)
{
lcd->index(lcd, 0x2c);
}
* Name: mio283qt9a_gramselect_read
*
* Description:
* Setup to read multiple pixels to the GRAM memory
*
****************************************************************************/
static inline void mio283qt9a_gramselect_read(
FAR struct mio283qt9a_lcd_s *lcd)
{
lcd->index(lcd, 0x2e);
lcd->readgram(lcd);
}
* Name: mio283qt9a_gramwrite
*
* Description:
* Setup to read or write multiple pixels to the GRAM memory
*
****************************************************************************/
static inline void mio283qt9a_gramwrite(
FAR struct mio283qt9a_lcd_s *lcd, uint16_t data)
{
lcd->write(lcd, data);
}
* Name: mio283qt9a_readsetup
*
* Description:
* Prime the operation by reading one pixel from the GRAM memory if
* necessary for this LCD type.
* When reading 16-bit gram data, there may be some shifts in the
* returned data:
*
* - ILI932x: Discard first dummy read; no shift in the return data
*
****************************************************************************/
#ifndef CONFIG_LCD_NOGETRUN
static inline void mio283qt9a_readsetup(FAR struct mio283qt9a_lcd_s *lcd,
FAR uint16_t *accum)
{
#if 0
*accum = lcd->read(lcd);
#endif
}
#endif
* Name: mio283qt9a_gramread
*
* Description:
* Read one correctly aligned pixel from the GRAM memory.
* Possibly shifting the data and possibly swapping red and green
* components.
*
* - ILI932x: Unknown -- assuming colors are in the color order
*
****************************************************************************/
#ifndef CONFIG_LCD_NOGETRUN
static inline uint16_t mio283qt9a_gramread(FAR struct mio283qt9a_lcd_s *lcd,
FAR uint16_t *accum)
{
return lcd->readgram(lcd);
}
#endif
* Name: mio283qt9a_setarea
*
* Description:
* Set the cursor position.
* In landscape mode, the "column" is actually the physical
* Y position and the "row" is the physical X position.
*
****************************************************************************/
static void mio283qt9a_setarea(FAR struct mio283qt9a_lcd_s *lcd,
uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
mio283qt9a_putreg(lcd, 0x2a, (x0 >> 8));
lcd->write(lcd, (x0 & 0xff));
lcd->write(lcd, (x1 >> 8));
lcd->write(lcd, (x1 & 0xff));
mio283qt9a_putreg(lcd, 0x2b, (y0 >> 8));
lcd->write(lcd, (y0 & 0xff));
lcd->write(lcd, (y1 >> 8));
lcd->write(lcd, (y1 & 0xff));
}
* Name: mio283qt9a_putrun
*
* Description:
* This method can be used to write a partial raster line to the LCD:
*
* dev - The lcd device
* row - Starting row to write to (range: 0 <= row < yres)
* col - Starting column to write to (range: 0 <= col <= xres-npixels)
* buffer - The buffer containing the run to be written to the LCD
* npixels - The number of pixels to write to the LCD
* (range: 0 < npixels <= xres-col)
*
****************************************************************************/
static int mio283qt9a_putrun(FAR struct lcd_dev_s *dev,
fb_coord_t row, fb_coord_t col,
FAR const uint8_t *buffer,
size_t npixels)
{
FAR struct mio283qt9a_dev_s *priv = (FAR struct mio283qt9a_dev_s *)dev;
FAR struct mio283qt9a_lcd_s *lcd = priv->lcd;
FAR const uint16_t *src = (FAR const uint16_t *)buffer;
int i;
DEBUGASSERT(buffer && ((uintptr_t)buffer & 1) == 0);
lcd->select(lcd);
mio283qt9a_setarea(lcd, col, row, col + npixels - 1, row);
mio283qt9a_gramselect_write(lcd);
for (i = 0; i < npixels; i++)
{
mio283qt9a_gramwrite(lcd, *src);
src++;
}
lcd->deselect(lcd);
return OK;
}
* Name: mio283qt9a_getrun
*
* Description:
* This method can be used to read a partial raster line from the LCD:
*
* dev - The lcd device
* row - Starting row to read from (range: 0 <= row < yres)
* col - Starting column to read read (range: 0 <= col <= xres-npixels)
* buffer - The buffer in which to return the run read from the LCD
* npixels - The number of pixels to read from the LCD
* (range: 0 < npixels <= xres-col)
*
****************************************************************************/
static int mio283qt9a_getrun(FAR struct lcd_dev_s *dev,
fb_coord_t row, fb_coord_t col,
FAR uint8_t *buffer,
size_t npixels)
{
#ifndef CONFIG_LCD_NOGETRUN
FAR struct mio283qt9a_dev_s *priv = (FAR struct mio283qt9a_dev_s *)dev;
FAR struct mio283qt9a_lcd_s *lcd = priv->lcd;
FAR uint16_t *dest = (FAR uint16_t *)buffer;
uint16_t accum;
uint16_t test;
int i;
lcdinfo("mio283qt9a_getrun row: %d col: %d npixels: %d\n",
row, col, npixels);
DEBUGASSERT(buffer && ((uintptr_t)buffer & 1) == 0);
lcd->select(lcd);
mio283qt9a_setarea(lcd, col, row, col + npixels - 1, row);
mio283qt9a_gramselect_read(lcd);
mio283qt9a_readsetup(lcd, &accum);
for (i = 0; i < npixels; i++)
{
test = mio283qt9a_gramread(lcd, &accum);
*dest++ = test;
}
lcd->deselect(lcd);
return OK;
#else
return -ENOSYS;
#endif
}
* Name: mio283qt9a_getvideoinfo
*
* Description:
* Get information about the LCD video controller configuration.
*
****************************************************************************/
static int mio283qt9a_getvideoinfo(FAR struct lcd_dev_s *dev,
FAR struct fb_videoinfo_s *vinfo)
{
DEBUGASSERT(dev && vinfo);
lcdinfo("fmt: %d xres: %d yres: %d nplanes: 1\n",
MIO283QT9A_COLORFMT, MIO283QT9A_XRES, MIO283QT9A_YRES);
vinfo->fmt = MIO283QT9A_COLORFMT;
vinfo->xres = MIO283QT9A_XRES;
vinfo->yres = MIO283QT9A_YRES;
vinfo->nplanes = 1;
return OK;
}
* Name: mio283qt9a_getplaneinfo
*
* Description:
* Get information about the configuration of each LCD color plane.
*
****************************************************************************/
static int mio283qt9a_getplaneinfo(FAR struct lcd_dev_s *dev,
unsigned int planeno,
FAR struct lcd_planeinfo_s *pinfo)
{
FAR struct mio283qt9a_dev_s *priv = (FAR struct mio283qt9a_dev_s *)dev;
DEBUGASSERT(dev && pinfo && planeno == 0);
lcdinfo("planeno: %d bpp: %d\n", planeno, MIO283QT9A_BPP);
pinfo->putrun = mio283qt9a_putrun;
pinfo->getrun = mio283qt9a_getrun;
pinfo->buffer = (FAR uint8_t *)priv->runbuffer;
pinfo->bpp = MIO283QT9A_BPP;
pinfo->dev = dev;
return OK;
}
* Name: mio283qt9a_getpower
*
* Description:
* Get the LCD panel power status
* (0: full off - CONFIG_LCD_MAXPOWER: full on).
* On backlit LCDs, this setting may correspond to the backlight setting.
*
****************************************************************************/
static int mio283qt9a_getpower(FAR struct lcd_dev_s *dev)
{
lcdinfo("getpower: %d\n", 0);
return g_lcddev.power;
}
* Name: mio283qt9a_poweroff
*
* Description:
* Enable/disable LCD panel power
* (0: full off - CONFIG_LCD_MAXPOWER: full on).
* On backlit LCDs, this setting may correspond to the backlight setting.
*
****************************************************************************/
static int mio283qt9a_poweroff(FAR struct mio283qt9a_lcd_s *lcd)
{
lcdinfo("mio283qt9a_poweroff\n");
lcd->select(lcd);
lcd->backlight(lcd, 0);
mio283qt9a_putreg(lcd, 0x28, 0x0000);
lcd->deselect(lcd);
g_lcddev.power = 0;
return OK;
}
* Name: mio283qt9a_setpower
*
* Description:
* Enable/disable LCD panel power
* (0: full off - CONFIG_LCD_MAXPOWER: full on).
* On backlit LCDs, this setting may correspond to the backlight setting.
*
****************************************************************************/
static int mio283qt9a_setpower(FAR struct lcd_dev_s *dev, int power)
{
FAR struct mio283qt9a_dev_s *priv = (FAR struct mio283qt9a_dev_s *)dev;
FAR struct mio283qt9a_lcd_s *lcd = priv->lcd;
lcdinfo("setpower: %d\n", power);
DEBUGASSERT((unsigned)power <= CONFIG_LCD_MAXPOWER);
if (power > 0)
{
lcd->select(lcd);
lcd->backlight(lcd, power);
mio283qt9a_putreg(lcd, 0x29, 0x00);
lcd->deselect(lcd);
g_lcddev.power = power;
}
else
{
mio283qt9a_poweroff(lcd);
}
return OK;
}
* Name: mio283qt9a_getcontrast
*
* Description:
* Get the current contrast setting (0-CONFIG_LCD_MAXCONTRAST).
*
****************************************************************************/
static int mio283qt9a_getcontrast(FAR struct lcd_dev_s *dev)
{
lcdinfo("Not implemented\n");
return -ENOSYS;
}
* Name: mio283qt9a_setcontrast
*
* Description:
* Set LCD panel contrast (0-CONFIG_LCD_MAXCONTRAST).
*
****************************************************************************/
static int mio283qt9a_setcontrast(FAR struct lcd_dev_s *dev,
unsigned int contrast)
{
lcdinfo("contrast: %d\n", contrast);
return -ENOSYS;
}
* Name: mio283qt9a_hwinitialize
*
* Description:
* Initialize the LCD hardware.
*
****************************************************************************/
static inline int mio283qt9a_hwinitialize(FAR struct mio283qt9a_dev_s *priv)
{
FAR struct mio283qt9a_lcd_s *lcd = priv->lcd;
#if !defined(CONFIG_LCD_NOGETRUN) || defined(CONFIG_DEBUG_LCD)
uint16_t id_a;
uint16_t id_b;
uint16_t id_c;
uint16_t id_d;
#endif
#ifdef CONFIG_DEBUG_LCD
uint16_t id_e;
#endif
int ret;
lcd->select(lcd);
#ifndef CONFIG_LCD_NOGETRUN
id_a = mio283qt9a_readreg(lcd, 0xd3);
id_b = lcd->read(lcd);
id_c = lcd->read(lcd);
id_d = lcd->read(lcd);
lcdinfo("LCD ID: %04x %04x %04x %04x\n", id_a, id_b, id_c, id_d);
UNUSED(id_a);
UNUSED(id_b);
if (id_c == ILI9341_ID_1 && id_d == ILI9341_ID_2)
#endif
{
mio283qt9a_putreg(lcd, 0x29, 0);
mio283qt9a_putreg(lcd, 0x13, 0);
mio283qt9a_putreg(lcd, 0xc0, 0x28);
mio283qt9a_putreg(lcd, 0xc1, 0x01);
mio283qt9a_putreg(lcd, 0xc5, 0x24);
lcd->write(lcd, 0x25);
mio283qt9a_putreg(lcd, 0x26, 0x01);
mio283qt9a_putreg(lcd, 0x3a, 0x55);
#if defined(CONFIG_LCD_LANDSCAPE)
mio283qt9a_putreg(lcd, 0x36, 0x0028);
#elif defined(CONFIG_LCD_PORTRAIT)
mio283qt9a_putreg(lcd, 0x36, 0x0008);
#elif defined(CONFIG_LCD_RLANDSCAPE)
mio283qt9a_putreg(lcd, 0x36, 0x0068);
#elif defined(CONFIG_LCD_RPORTRAIT)
mio283qt9a_putreg(lcd, 0x36, 0x00c8);
#endif
mio283qt9a_setarea(lcd, 0, 0, (MIO283QT9A_XRES - 1),
(MIO283QT9A_YRES - 1));
mio283qt9a_putreg(lcd, 0x11, 0);
up_mdelay(25);
#ifdef CONFIG_DEBUG_LCD
id_a = mio283qt9a_readreg(lcd, 0x04);
id_b = lcd->read(lcd);
id_c = lcd->read(lcd);
id_d = lcd->read(lcd);
lcdinfo("LCD man ID: %02x, version: %02x, driver ID: %02x\n",
id_b, id_c, id_d);
id_a = mio283qt9a_readreg(lcd, 0x09);
id_b = lcd->read(lcd);
id_c = lcd->read(lcd);
id_d = lcd->read(lcd);
id_e = lcd->read(lcd);
lcdinfo("Display status %02x, %02x, %02x, %02x, %02x\n",
id_a, id_b, id_c, id_d, id_e);
id_a = mio283qt9a_readreg(lcd, 0x0a);
id_b = lcd->read(lcd);
lcdinfo("Power status %02x, %02x\n", id_a, id_b);
id_a = mio283qt9a_readreg(lcd, 0x0b);
id_b = lcd->read(lcd);
lcdinfo("MADCTL %02x, %02x\n", id_a, id_b);
id_a = mio283qt9a_readreg(lcd, 0x0c);
id_b = lcd->read(lcd);
lcdinfo("Pixel format %02x, %02x\n", id_a, id_b);
id_a = mio283qt9a_readreg(lcd, 0x0d);
id_b = lcd->read(lcd);
lcdinfo("Image format %02x, %02x\n", id_a, id_b);
id_a = mio283qt9a_readreg(lcd, 0x0e);
id_b = lcd->read(lcd);
lcdinfo("Signal mode %02x, %02x\n", id_a, id_b);
id_a = mio283qt9a_readreg(lcd, 0x0f);
id_b = lcd->read(lcd);
lcdinfo("Self diag %02x, %02x\n", id_a, id_b);
#endif
ret = OK;
}
#ifndef CONFIG_LCD_NOGETRUN
else
{
lcderr("ERROR: Unsupported LCD type\n");
ret = -ENODEV;
}
#endif
lcd->deselect(lcd);
return ret;
}
* Public Functions
****************************************************************************/
* Name: mio283qt9a_lcdinitialize
*
* Description:
* Initialize the LCD video hardware.
* The initial state of the LCD is fully initialized, display memory
* cleared, and the LCD ready to use, but with the power setting at 0
* (full off).
*
****************************************************************************/
FAR struct lcd_dev_s *mio283qt9a_lcdinitialize(
FAR struct mio283qt9a_lcd_s *lcd)
{
FAR struct mio283qt9a_dev_s *priv;
int ret;
lcdinfo("Initializing\n");
* allocate a new driver data structure.
*/
priv = &g_lcddev;
priv->dev.getvideoinfo = mio283qt9a_getvideoinfo;
priv->dev.getplaneinfo = mio283qt9a_getplaneinfo;
priv->dev.getpower = mio283qt9a_getpower;
priv->dev.setpower = mio283qt9a_setpower;
priv->dev.getcontrast = mio283qt9a_getcontrast;
priv->dev.setcontrast = mio283qt9a_setcontrast;
priv->lcd = lcd;
ret = mio283qt9a_hwinitialize(priv);
if (ret == OK)
{
mio283qt9a_clear(&priv->dev, 0x0000);
mio283qt9a_poweroff(lcd);
return &g_lcddev.dev;
}
return NULL;
}
* Name: mio283qt9a_clear
*
* Description:
* This is a non-standard LCD interface just for the stm3240g-EVAL board.
* Because of the various rotations, clearing the display in the normal
* way by writing a sequences of runs that covers the entire display can
* be very slow. Here the display is cleared by simply setting all GRAM
* memory to the specified color.
*
****************************************************************************/
void mio283qt9a_clear(FAR struct lcd_dev_s *dev, uint16_t color)
{
FAR struct mio283qt9a_dev_s *priv = (FAR struct mio283qt9a_dev_s *)dev;
FAR struct mio283qt9a_lcd_s *lcd = priv->lcd;
uint32_t i;
lcd->select(lcd);
mio283qt9a_setarea(lcd, 0, 0, (MIO283QT9A_XRES - 1),
(MIO283QT9A_YRES - 1));
mio283qt9a_gramselect_write(lcd);
* Orientation does not matter in this case.
*/
for (i = 0; i < MIO283QT9A_XRES * MIO283QT9A_YRES; i++)
{
mio283qt9a_gramwrite(lcd, color);
}
lcd->deselect(lcd);
}
#endif