// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the Himax HX - 8357 LCD Controller
*
* Copyright 2012 Free Electrons
*/
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/lcd.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/spi/spi.h>
#define HX8357_NUM_IM_PINS 3
#define HX8357_SWRESET 0 x01
#define HX8357_GET_RED_CHANNEL 0 x06
#define HX8357_GET_GREEN_CHANNEL 0 x07
#define HX8357_GET_BLUE_CHANNEL 0 x08
#define HX8357_GET_POWER_MODE 0 x0a
#define HX8357_GET_MADCTL 0 x0b
#define HX8357_GET_PIXEL_FORMAT 0 x0c
#define HX8357_GET_DISPLAY_MODE 0 x0d
#define HX8357_GET_SIGNAL_MODE 0 x0e
#define HX8357_GET_DIAGNOSTIC_RESULT 0 x0f
#define HX8357_ENTER_SLEEP_MODE 0 x10
#define HX8357_EXIT_SLEEP_MODE 0 x11
#define HX8357_ENTER_PARTIAL_MODE 0 x12
#define HX8357_ENTER_NORMAL_MODE 0 x13
#define HX8357_EXIT_INVERSION_MODE 0 x20
#define HX8357_ENTER_INVERSION_MODE 0 x21
#define HX8357_SET_DISPLAY_OFF 0 x28
#define HX8357_SET_DISPLAY_ON 0 x29
#define HX8357_SET_COLUMN_ADDRESS 0 x2a
#define HX8357_SET_PAGE_ADDRESS 0 x2b
#define HX8357_WRITE_MEMORY_START 0 x2c
#define HX8357_READ_MEMORY_START 0 x2e
#define HX8357_SET_PARTIAL_AREA 0 x30
#define HX8357_SET_SCROLL_AREA 0 x33
#define HX8357_SET_TEAR_OFF 0 x34
#define HX8357_SET_TEAR_ON 0 x35
#define HX8357_SET_ADDRESS_MODE 0 x36
#define HX8357_SET_SCROLL_START 0 x37
#define HX8357_EXIT_IDLE_MODE 0 x38
#define HX8357_ENTER_IDLE_MODE 0 x39
#define HX8357_SET_PIXEL_FORMAT 0 x3a
#define HX8357_SET_PIXEL_FORMAT_DBI_3BIT (0 x1)
#define HX8357_SET_PIXEL_FORMAT_DBI_16BIT (0 x5)
#define HX8357_SET_PIXEL_FORMAT_DBI_18BIT (0 x6)
#define HX8357_SET_PIXEL_FORMAT_DPI_3BIT (0 x1 << 4 )
#define HX8357_SET_PIXEL_FORMAT_DPI_16BIT (0 x5 << 4 )
#define HX8357_SET_PIXEL_FORMAT_DPI_18BIT (0 x6 << 4 )
#define HX8357_WRITE_MEMORY_CONTINUE 0 x3c
#define HX8357_READ_MEMORY_CONTINUE 0 x3e
#define HX8357_SET_TEAR_SCAN_LINES 0 x44
#define HX8357_GET_SCAN_LINES 0 x45
#define HX8357_READ_DDB_START 0 xa1
#define HX8357_SET_DISPLAY_MODE 0 xb4
#define HX8357_SET_DISPLAY_MODE_RGB_THROUGH (0 x3)
#define HX8357_SET_DISPLAY_MODE_RGB_INTERFACE (1 << 4 )
#define HX8357_SET_PANEL_DRIVING 0 xc0
#define HX8357_SET_DISPLAY_FRAME 0 xc5
#define HX8357_SET_RGB 0 xc6
#define HX8357_SET_RGB_ENABLE_HIGH (1 << 1 )
#define HX8357_SET_GAMMA 0 xc8
#define HX8357_SET_POWER 0 xd0
#define HX8357_SET_VCOM 0 xd1
#define HX8357_SET_POWER_NORMAL 0 xd2
#define HX8357_SET_PANEL_RELATED 0 xe9
#define HX8369_SET_DISPLAY_BRIGHTNESS 0 x51
#define HX8369_WRITE_CABC_DISPLAY_VALUE 0 x53
#define HX8369_WRITE_CABC_BRIGHT_CTRL 0 x55
#define HX8369_WRITE_CABC_MIN_BRIGHTNESS 0 x5e
#define HX8369_SET_POWER 0 xb1
#define HX8369_SET_DISPLAY_MODE 0 xb2
#define HX8369_SET_DISPLAY_WAVEFORM_CYC 0 xb4
#define HX8369_SET_VCOM 0 xb6
#define HX8369_SET_EXTENSION_COMMAND 0 xb9
#define HX8369_SET_GIP 0 xd5
#define HX8369_SET_GAMMA_CURVE_RELATED 0 xe0
struct hx8357_data {
struct gpio_descs *im_pins;
struct gpio_desc *reset;
struct spi_device *spi;
int state;
};
static u8 hx8357_seq_power[] = {
HX8357_SET_POWER, 0 x44, 0 x41, 0 x06,
};
static u8 hx8357_seq_vcom[] = {
HX8357_SET_VCOM, 0 x40, 0 x10,
};
static u8 hx8357_seq_power_normal[] = {
HX8357_SET_POWER_NORMAL, 0 x05, 0 x12,
};
static u8 hx8357_seq_panel_driving[] = {
HX8357_SET_PANEL_DRIVING, 0 x14, 0 x3b, 0 x00, 0 x02, 0 x11,
};
static u8 hx8357_seq_display_frame[] = {
HX8357_SET_DISPLAY_FRAME, 0 x0c,
};
static u8 hx8357_seq_panel_related[] = {
HX8357_SET_PANEL_RELATED, 0 x01,
};
static u8 hx8357_seq_undefined1[] = {
0 xea, 0 x03, 0 x00, 0 x00,
};
static u8 hx8357_seq_undefined2[] = {
0 xeb, 0 x40, 0 x54, 0 x26, 0 xdb,
};
static u8 hx8357_seq_gamma[] = {
HX8357_SET_GAMMA, 0 x00, 0 x15, 0 x00, 0 x22, 0 x00,
0 x08, 0 x77, 0 x26, 0 x77, 0 x22, 0 x04, 0 x00,
};
static u8 hx8357_seq_address_mode[] = {
HX8357_SET_ADDRESS_MODE, 0 xc0,
};
static u8 hx8357_seq_pixel_format[] = {
HX8357_SET_PIXEL_FORMAT,
HX8357_SET_PIXEL_FORMAT_DPI_18BIT |
HX8357_SET_PIXEL_FORMAT_DBI_18BIT,
};
static u8 hx8357_seq_column_address[] = {
HX8357_SET_COLUMN_ADDRESS, 0 x00, 0 x00, 0 x01, 0 x3f,
};
static u8 hx8357_seq_page_address[] = {
HX8357_SET_PAGE_ADDRESS, 0 x00, 0 x00, 0 x01, 0 xdf,
};
static u8 hx8357_seq_rgb[] = {
HX8357_SET_RGB, 0 x02,
};
static u8 hx8357_seq_display_mode[] = {
HX8357_SET_DISPLAY_MODE,
HX8357_SET_DISPLAY_MODE_RGB_THROUGH |
HX8357_SET_DISPLAY_MODE_RGB_INTERFACE,
};
static u8 hx8369_seq_write_CABC_min_brightness[] = {
HX8369_WRITE_CABC_MIN_BRIGHTNESS, 0 x00,
};
static u8 hx8369_seq_write_CABC_control[] = {
HX8369_WRITE_CABC_DISPLAY_VALUE, 0 x24,
};
static u8 hx8369_seq_set_display_brightness[] = {
HX8369_SET_DISPLAY_BRIGHTNESS, 0 xFF,
};
static u8 hx8369_seq_write_CABC_control_setting[] = {
HX8369_WRITE_CABC_BRIGHT_CTRL, 0 x02,
};
static u8 hx8369_seq_extension_command[] = {
HX8369_SET_EXTENSION_COMMAND, 0 xff, 0 x83, 0 x69,
};
static u8 hx8369_seq_display_related[] = {
HX8369_SET_DISPLAY_MODE, 0 x00, 0 x2b, 0 x03, 0 x03, 0 x70, 0 x00,
0 xff, 0 x00, 0 x00, 0 x00, 0 x00, 0 x03, 0 x03, 0 x00, 0 x01,
};
static u8 hx8369_seq_panel_waveform_cycle[] = {
HX8369_SET_DISPLAY_WAVEFORM_CYC, 0 x0a, 0 x1d, 0 x80, 0 x06, 0 x02,
};
static u8 hx8369_seq_set_address_mode[] = {
HX8357_SET_ADDRESS_MODE, 0 x00,
};
static u8 hx8369_seq_vcom[] = {
HX8369_SET_VCOM, 0 x3e, 0 x3e,
};
static u8 hx8369_seq_gip[] = {
HX8369_SET_GIP, 0 x00, 0 x01, 0 x03, 0 x25, 0 x01, 0 x02, 0 x28, 0 x70,
0 x11, 0 x13, 0 x00, 0 x00, 0 x40, 0 x26, 0 x51, 0 x37, 0 x00, 0 x00, 0 x71,
0 x35, 0 x60, 0 x24, 0 x07, 0 x0f, 0 x04, 0 x04,
};
static u8 hx8369_seq_power[] = {
HX8369_SET_POWER, 0 x01, 0 x00, 0 x34, 0 x03, 0 x00, 0 x11, 0 x11, 0 x32,
0 x2f, 0 x3f, 0 x3f, 0 x01, 0 x3a, 0 x01, 0 xe6, 0 xe6, 0 xe6, 0 xe6, 0 xe6,
};
static u8 hx8369_seq_gamma_curve_related[] = {
HX8369_SET_GAMMA_CURVE_RELATED, 0 x00, 0 x0d, 0 x19, 0 x2f, 0 x3b, 0 x3d,
0 x2e, 0 x4a, 0 x08, 0 x0e, 0 x0f, 0 x14, 0 x16, 0 x14, 0 x14, 0 x14, 0 x1e,
0 x00, 0 x0d, 0 x19, 0 x2f, 0 x3b, 0 x3d, 0 x2e, 0 x4a, 0 x08, 0 x0e, 0 x0f,
0 x14, 0 x16, 0 x14, 0 x14, 0 x14, 0 x1e,
};
static int hx8357_spi_write_then_read(struct lcd_device *lcdev,
u8 *txbuf, u16 txlen,
u8 *rxbuf, u16 rxlen)
{
struct hx8357_data *lcd = lcd_get_data(lcdev);
struct spi_message msg;
struct spi_transfer xfer[2 ];
u16 *local_txbuf = NULL;
int ret = 0 ;
memset(xfer, 0 , sizeof (xfer));
spi_message_init(&msg);
if (txlen) {
int i;
local_txbuf = kcalloc(txlen, sizeof (*local_txbuf), GFP_KERNEL);
if (!local_txbuf)
return -ENOMEM;
for (i = 0 ; i < txlen; i++) {
local_txbuf[i] = txbuf[i];
if (i > 0 )
local_txbuf[i] |= 1 << 8 ;
}
xfer[0 ].len = 2 * txlen;
xfer[0 ].bits_per_word = 9 ;
xfer[0 ].tx_buf = local_txbuf;
spi_message_add_tail(&xfer[0 ], &msg);
}
if (rxlen) {
xfer[1 ].len = rxlen;
xfer[1 ].bits_per_word = 8 ;
xfer[1 ].rx_buf = rxbuf;
spi_message_add_tail(&xfer[1 ], &msg);
}
ret = spi_sync(lcd->spi, &msg);
if (ret < 0 )
dev_err(&lcdev->dev, "Couldn't send SPI data\n" );
if (txlen)
kfree(local_txbuf);
return ret;
}
static inline int hx8357_spi_write_array(struct lcd_device *lcdev,
u8 *value, u8 len)
{
return hx8357_spi_write_then_read(lcdev, value, len, NULL, 0 );
}
static inline int hx8357_spi_write_byte(struct lcd_device *lcdev,
u8 value)
{
return hx8357_spi_write_then_read(lcdev, &value, 1 , NULL, 0 );
}
static int hx8357_enter_standby(struct lcd_device *lcdev)
{
int ret;
ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_OFF);
if (ret < 0 )
return ret;
usleep_range(10000 , 12000 );
ret = hx8357_spi_write_byte(lcdev, HX8357_ENTER_SLEEP_MODE);
if (ret < 0 )
return ret;
/*
* The controller needs 120 ms when entering in sleep mode before we can
* send the command to go off sleep mode
*/
msleep(120 );
return 0 ;
}
static int hx8357_exit_standby(struct lcd_device *lcdev)
{
int ret;
ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
if (ret < 0 )
return ret;
/*
* The controller needs 120 ms when exiting from sleep mode before we
* can send the command to enter in sleep mode
*/
msleep(120 );
ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
if (ret < 0 )
return ret;
return 0 ;
}
static void hx8357_lcd_reset(struct lcd_device *lcdev)
{
struct hx8357_data *lcd = lcd_get_data(lcdev);
/* Reset the screen */
gpiod_set_value(lcd->reset, 0 );
usleep_range(10000 , 12000 );
gpiod_set_value(lcd->reset, 1 );
usleep_range(10000 , 12000 );
gpiod_set_value(lcd->reset, 0 );
/* The controller needs 120ms to recover from reset */
msleep(120 );
}
static int hx8357_lcd_init(struct lcd_device *lcdev)
{
struct hx8357_data *lcd = lcd_get_data(lcdev);
int ret;
/*
* Set the interface selection pins to SPI mode , with three
* wires
*/
if (lcd->im_pins) {
gpiod_set_value_cansleep(lcd->im_pins->desc[0 ], 1 );
gpiod_set_value_cansleep(lcd->im_pins->desc[1 ], 0 );
gpiod_set_value_cansleep(lcd->im_pins->desc[2 ], 1 );
}
ret = hx8357_spi_write_array(lcdev, hx8357_seq_power,
ARRAY_SIZE(hx8357_seq_power));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_vcom,
ARRAY_SIZE(hx8357_seq_vcom));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_power_normal,
ARRAY_SIZE(hx8357_seq_power_normal));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_panel_driving,
ARRAY_SIZE(hx8357_seq_panel_driving));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_display_frame,
ARRAY_SIZE(hx8357_seq_display_frame));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_panel_related,
ARRAY_SIZE(hx8357_seq_panel_related));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_undefined1,
ARRAY_SIZE(hx8357_seq_undefined1));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_undefined2,
ARRAY_SIZE(hx8357_seq_undefined2));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_gamma,
ARRAY_SIZE(hx8357_seq_gamma));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_address_mode,
ARRAY_SIZE(hx8357_seq_address_mode));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_pixel_format,
ARRAY_SIZE(hx8357_seq_pixel_format));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_column_address,
ARRAY_SIZE(hx8357_seq_column_address));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_page_address,
ARRAY_SIZE(hx8357_seq_page_address));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_rgb,
ARRAY_SIZE(hx8357_seq_rgb));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8357_seq_display_mode,
ARRAY_SIZE(hx8357_seq_display_mode));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
if (ret < 0 )
return ret;
/*
* The controller needs 120 ms to fully recover from exiting sleep mode
*/
msleep(120 );
ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
if (ret < 0 )
return ret;
usleep_range(5000 , 7000 );
ret = hx8357_spi_write_byte(lcdev, HX8357_WRITE_MEMORY_START);
if (ret < 0 )
return ret;
return 0 ;
}
static int hx8369_lcd_init(struct lcd_device *lcdev)
{
int ret;
ret = hx8357_spi_write_array(lcdev, hx8369_seq_extension_command,
ARRAY_SIZE(hx8369_seq_extension_command));
if (ret < 0 )
return ret;
usleep_range(10000 , 12000 );
ret = hx8357_spi_write_array(lcdev, hx8369_seq_display_related,
ARRAY_SIZE(hx8369_seq_display_related));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8369_seq_panel_waveform_cycle,
ARRAY_SIZE(hx8369_seq_panel_waveform_cycle));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8369_seq_set_address_mode,
ARRAY_SIZE(hx8369_seq_set_address_mode));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8369_seq_vcom,
ARRAY_SIZE(hx8369_seq_vcom));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8369_seq_gip,
ARRAY_SIZE(hx8369_seq_gip));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev, hx8369_seq_power,
ARRAY_SIZE(hx8369_seq_power));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
if (ret < 0 )
return ret;
/*
* The controller needs 120 ms to fully recover from exiting sleep mode
*/
msleep(120 );
ret = hx8357_spi_write_array(lcdev, hx8369_seq_gamma_curve_related,
ARRAY_SIZE(hx8369_seq_gamma_curve_related));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
if (ret < 0 )
return ret;
usleep_range(1000 , 1200 );
ret = hx8357_spi_write_array(lcdev, hx8369_seq_write_CABC_control,
ARRAY_SIZE(hx8369_seq_write_CABC_control));
if (ret < 0 )
return ret;
usleep_range(10000 , 12000 );
ret = hx8357_spi_write_array(lcdev,
hx8369_seq_write_CABC_control_setting,
ARRAY_SIZE(hx8369_seq_write_CABC_control_setting));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_array(lcdev,
hx8369_seq_write_CABC_min_brightness,
ARRAY_SIZE(hx8369_seq_write_CABC_min_brightness));
if (ret < 0 )
return ret;
usleep_range(10000 , 12000 );
ret = hx8357_spi_write_array(lcdev, hx8369_seq_set_display_brightness,
ARRAY_SIZE(hx8369_seq_set_display_brightness));
if (ret < 0 )
return ret;
ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
if (ret < 0 )
return ret;
return 0 ;
}
#define POWER_IS_ON(pwr) ((pwr) <= LCD_POWER_REDUCED)
static int hx8357_set_power(struct lcd_device *lcdev, int power)
{
struct hx8357_data *lcd = lcd_get_data(lcdev);
int ret = 0 ;
if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->state))
ret = hx8357_exit_standby(lcdev);
else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->state))
ret = hx8357_enter_standby(lcdev);
if (ret == 0 )
lcd->state = power;
else
dev_warn(&lcdev->dev, "failed to set power mode %d\n" , power);
return ret;
}
static int hx8357_get_power(struct lcd_device *lcdev)
{
struct hx8357_data *lcd = lcd_get_data(lcdev);
return lcd->state;
}
static const struct lcd_ops hx8357_ops = {
.set_power = hx8357_set_power,
.get_power = hx8357_get_power,
};
typedef int (*hx8357_init_fn)(struct lcd_device *);
static int hx8357_probe(struct spi_device *spi)
{
struct device *dev = &spi->dev;
struct lcd_device *lcdev;
struct hx8357_data *lcd;
hx8357_init_fn init_fn;
int i, ret;
lcd = devm_kzalloc(dev, sizeof (*lcd), GFP_KERNEL);
if (!lcd)
return -ENOMEM;
ret = spi_setup(spi);
if (ret < 0 )
return dev_err_probe(dev, ret, "SPI setup failed.\n" );
lcd->spi = spi;
init_fn = device_get_match_data(dev);
if (!init_fn)
return -EINVAL;
lcd->reset = devm_gpiod_get(dev, "reset" , GPIOD_OUT_LOW);
if (IS_ERR(lcd->reset))
return dev_err_probe(dev, PTR_ERR(lcd->reset), "failed to request reset GPIO\n" );
gpiod_set_consumer_name(lcd->reset, "hx8357-reset" );
lcd->im_pins = devm_gpiod_get_array_optional(dev, "im" , GPIOD_OUT_LOW);
if (IS_ERR(lcd->im_pins))
return dev_err_probe(dev, PTR_ERR(lcd->im_pins), "failed to request im GPIOs\n" );
if (lcd->im_pins) {
if (lcd->im_pins->ndescs < HX8357_NUM_IM_PINS)
return dev_err_probe(dev, -EINVAL, "not enough im GPIOs\n" );
for (i = 0 ; i < HX8357_NUM_IM_PINS; i++)
gpiod_set_consumer_name(lcd->im_pins->desc[i], "im_pins" );
}
lcdev = devm_lcd_device_register(dev, "mxsfb" , dev, lcd, &hx8357_ops);
if (IS_ERR(lcdev)) {
ret = PTR_ERR(lcdev);
return ret;
}
spi_set_drvdata(spi, lcdev);
hx8357_lcd_reset(lcdev);
ret = init_fn(lcdev);
if (ret)
return dev_err_probe(dev, ret, "Couldn't initialize panel\n" );
dev_info(dev, "Panel probed\n" );
return 0 ;
}
static const struct of_device_id hx8357_dt_ids[] = {
{
.compatible = "himax,hx8357" ,
.data = hx8357_lcd_init,
},
{
.compatible = "himax,hx8369" ,
.data = hx8369_lcd_init,
},
{}
};
MODULE_DEVICE_TABLE(of, hx8357_dt_ids);
static struct spi_driver hx8357_driver = {
.probe = hx8357_probe,
.driver = {
.name = "hx8357" ,
.of_match_table = hx8357_dt_ids,
},
};
module_spi_driver(hx8357_driver);
MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>" );
MODULE_DESCRIPTION("Himax HX-8357 LCD Driver" );
MODULE_LICENSE("GPL" );
Messung V0.5 in Prozent C=97 H=99 G=97
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-09-27)
¤
*© Formatika GbR, Deutschland