Merge pull request #157 from makermelissa/driver-fix

ST7789 Driver fix
This commit is contained in:
Melissa LeBlanc-Williams 2021-02-26 11:00:19 -08:00 committed by GitHub
commit f9704fc473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 358 additions and 625 deletions

View file

@ -16,7 +16,7 @@ except ImportError:
shell = Shell()
shell.group = 'PITFT'
__version__ = "3.0.0"
__version__ = "3.1.0"
"""
This is the main configuration. Displays should be placed in the order
@ -117,9 +117,15 @@ dtoverlay=pitft28-capacitive,{rotation}""",
"kernel_upgrade": True,
"overlay_src": "overlays/minipitft13-overlay.dts",
"overlay_dest": "/boot/overlays/drm-minipitft13.dtbo",
"overlay": "dtoverlay=drm-minipitft13,rotation={pitftrot}",
"overlay": "dtoverlay=drm-minipitft13,rotate={pitftrot}",
"width": 240,
"height": 240,
"fbcp_rotations": {
"0": "0",
"90": "1",
"180": "2",
"270": "3",
},
},
{
"type": "st7789_240x320",
@ -131,7 +137,6 @@ dtoverlay=pitft28-capacitive,{rotation}""",
"overlay": "dtoverlay=drm-st7789v_240x320,rotate={pitftrot}",
"width": 320,
"height": 240,
"rotate": False,
},
{
"type": "st7789_240x135",
@ -140,20 +145,38 @@ dtoverlay=pitft28-capacitive,{rotation}""",
"kernel_upgrade": True,
"overlay_src": "overlays/minipitft114-overlay.dts",
"overlay_dest": "/boot/overlays/drm-minipitft114.dtbo",
"overlay": "dtoverlay=drm-minipitft114,rotation={pitftrot}",
"overlay": "dtoverlay=drm-minipitft114,rotate={pitftrot}",
"rotations": {
"0": None,
"90": "90",
"180": None,
"270": "270",
},
"width": 240,
"height": 135,
},
"fbcp_rotations": {
"0": "3",
"90": "2",
"180": "1",
"270": "0",
},
},
]
# default rotations
fbcp_rotations = {
"0": "1",
"90": "0",
"180": "3",
"270": "2",
}
PITFT_ROTATIONS = ("90", "180", "270", "0")
UPDATE_DB = False
SYSTEMD = None
pitft_config = None
pitftrot = None
auto_reboot = None
force_kernel = None
force_kernel_release = "1.20201126-1"
def warn_exit(message):
shell.warn(message)
@ -197,7 +220,7 @@ def softwareinstall():
if not shell.run_command("apt-get install -y libts0", True):
if not shell.run_command("apt-get install -y tslib"):
warn_exit("Apt failed to install TSLIB!")
if not shell.run_command("apt-get install -y bc fbi git python-dev python-pip python-smbus python-spidev evtest libts-bin device-tree-compiler"):
if not shell.run_command("apt-get install -y bc fbi git python3-dev python3-pip python3-smbus python3-spidev evtest libts-bin device-tree-compiler"):
warn_exit("Apt failed to install software!")
if not shell.run_command("pip3 install evdev"):
warn_exit("Pip failed to install software!")
@ -220,6 +243,34 @@ def uninstall_etc_modules():
shell.pattern_replace("/etc/modules", 'fbtft_device')
return True
def install_drivers():
"""Compile display driver and overlay if required"""
if "overlay_src" in pitft_config and "overlay_dest" in pitft_config:
print("Compiling Device Tree Overlay")
shell.run_command("dtc -@ -I dts -O dtb -o {dest} {src}".format(dest=pitft_config['overlay_dest'], src=pitft_config['overlay_src']))
if pitft_config['kernel_upgrade']:
print("############# UPGRADING KERNEL ###############")
print("Updating packages...")
if not shell.run_command("sudo apt-get update", True):
warn_exit("Apt failed to update itself!")
print("Upgrading packages...")
if not shell.run_command("sudo apt-get -y upgrade", False):
warn_exit("Apt failed to install software!")
print("Installing Kernel Headers. This may take a few minutes...")
if not shell.run_command("apt-get install -y raspberrypi-kernel-headers", True):
warn_exit("Apt failed to install software!")
if not shell.isdir("/lib/modules/{}/build".format(shell.release())):
warn_exit("Kernel was updated, but needs to be loaded. Please reboot now and re-run script!")
print("Compiling and installing display driver...")
shell.pushd("st7789_module")
if not shell.run_command("make"):
warn_exit("Apt failed to compile ST7789V drivers!")
shell.run_command("mv /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.BACK".format(rel=shell.release()))
shell.run_command("mv fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko".format(rel=shell.release()))
shell.popd()
return True
def update_configtxt(rotation_override=None):
"""update /boot/config.txt with appropriate values"""
uninstall_bootconfigtxt()
@ -228,35 +279,8 @@ def update_configtxt(rotation_override=None):
if "{pitftrot}" in overlay:
rotation = str(rotation_override) if rotation_override is not None else pitftrot
overlay = overlay.format(pitftrot=rotation)
if "{rotation}" in overlay and isinstance(pitft_config['rotations'], dict):
if "{rotation}" in overlay and isinstance(pitft_config['rotations'], dict) and pitft_config['rotations'][pitftrot] is not None:
overlay = overlay.format(rotation=pitft_config['rotations'][pitftrot])
if "overlay_src" in pitft_config and "overlay_dest" in pitft_config:
shell.run_command("dtc -@ -I dts -O dtb -o {dest} {src}".format(dest=pitft_config['overlay_dest'], src=pitft_config['overlay_src']))
print("############# UPGRADING KERNEL ###############")
print("Updating packages...")
if not shell.run_command("sudo apt-get update", True):
warn_exit("Apt failed to update itself!")
print("Upgrading packages...")
if not shell.run_command("sudo apt-get -y upgrade", False):
warn_exit("Apt failed to install software!")
if force_kernel and force_kernel + '.' not in shell.release():
print("Pinning kernel to version {}. This may take a while...".format(force_kernel))
if not shell.run_command("sudo sh rpi-pin-kernel-firmware.sh {}".format(force_kernel_release), False):
warn_exit("Failed to pin all kernel files!")
print("Installing Kernel Headers...")
if not shell.run_command("apt-get install -y raspberrypi-kernel-headers", True):
warn_exit("Apt failed to install software!")
if not shell.isdir("/lib/modules/{}/build".format(shell.release())):
warn_exit("Kernel was updated, please reboot now and re-run script!")
shell.pushd("st7789_module")
if not shell.run_command("make -C /lib/modules/$(uname -r)/build M=$(pwd) modules"):
warn_exit("Apt failed to compile ST7789V drivers!")
shell.run_command("mv /lib/modules/{rel}/kernel/drivers/gpu/drm/tiny/mi0283qt.ko /lib/modules/{rel}/kernel/drivers/gpu/drm/tiny/mi0283qt.BACK".format(rel=shell.release()))
shell.run_command("mv /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/gpu/drm/tiny/mi0283qt.BACK".format(rel=shell.release()))
shell.run_command("mv st7789v_ada.ko /lib/modules/{rel}/kernel/drivers/gpu/drm/tiny/mi0283qt.ko".format(rel=shell.release()))
shell.run_command("mv fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko".format(rel=shell.release()))
shell.popd()
shell.write_text_file("/boot/config.txt", """
# --- added by adafruit-pitft-helper {date} ---
@ -356,6 +380,9 @@ def install_fbcp():
shell.popd()
shell.run_command("rm -rf /tmp/rpi-fbcp-master")
if "fbcp_rotations" in pitft_config:
fbcp_rotations = pitft_config['fbcp_rotations']
# Start fbcp in the appropriate place, depending on init system:
if SYSTEMD:
# Add fbcp to /etc/rc.local:
@ -400,20 +427,19 @@ def install_fbcp():
shell.reconfig("/boot/config.txt", "^.*hdmi_cvt.*$", "hdmi_cvt={} {} 60 1 0 0 0".format(WIDTH, HEIGHT))
if pitftrot == "90" or ("rotate" in pitft_config and not pitft_config['rotate']):
# dont rotate HDMI on 90
shell.reconfig("/boot/config.txt", "^.*display_hdmi_rotate.*$", "")
try:
default_orientation = int(list(fbcp_rotations.keys())[list(fbcp_rotations.values()).index("0")])
except ValueError:
default_orientation = 90
if pitftrot in ("0", "180", "270"):
if pitftrot == "180":
display_rotate = "3"
elif pitftrot == "270":
display_rotate = "2"
else:
display_rotate = "1"
if fbcp_rotations[pitftrot] == "0":
# dont rotate HDMI on default orientation
shell.reconfig("/boot/config.txt", "^.*display_hdmi_rotate.*$", "")
else:
display_rotate = fbcp_rotations[pitftrot]
shell.reconfig("/boot/config.txt", "^.*display_hdmi_rotate.*$", "display_hdmi_rotate={}".format(display_rotate))
# Because we rotate HDMI we have to 'unrotate' the TFT by overriding pitftrot!
if not update_configtxt(90):
if not update_configtxt(default_orientation):
shell.bail("Unable to update /boot/config.txt")
return True
@ -581,6 +607,17 @@ Run time of up to 5 minutes. Reboot required!
))
pitftrot = PITFT_ROTATIONS[PITFT_ROTATE - 1]
if 'rotations' in pitft_config and isinstance(pitft_config['rotations'], dict) and pitftrot in pitft_config['rotations'] and pitft_config['rotations'][pitftrot] is None:
shell.bail("""Unfortunately {rotation} degrees for the {display} is not working at this time. Please
restart the script and choose a different orientation.""".format(rotation=pitftrot, display=pitft_config["menulabel"]))
# Checking if kernel is pinned
if shell.exists('/etc/apt/preferences.d/99-adafruit-pin-kernel'):
shell.warn("WARNING! Script detected your system is currently pinned to an older kernel. The pin will be removed and your system will be updated.")
if not shell.prompt("Continue?"):
shell.exit()
shell.remove('/etc/apt/preferences.d/99-adafruit-pin-kernel')
# check init system (technique borrowed from raspi-config):
shell.info('Checking init system...')
if shell.run_command("which systemctl", True) and shell.run_command("systemctl | grep '\-\.mount'", True):
@ -612,6 +649,11 @@ Run time of up to 5 minutes. Reboot required!
if not softwareinstall():
shell.bail("Unable to install software")
if "overlay_src" in pitft_config and "overlay_dest" in pitft_config:
shell.info("Installing display drivers and device tree overlay...")
if not install_drivers():
shell.bail("Unable to install display drivers")
shell.info("Updating /boot/config.txt...")
if not update_configtxt():
shell.bail("Unable to update /boot/config.txt")

View file

@ -1,5 +1,5 @@
/*
* Device Tree overlay for Adafruit Mini PiTFT 1.14" Display
* Device Tree overlay for Adafruit Mini PiTFT 1.14" 135x240 Display
*
*/
@ -7,72 +7,65 @@
/plugin/;
/ {
compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
fragment@0 {
target = <&spi0>;
__overlay__ {
status = "okay";
fragment@0 {
target = <&spi0>;
__overlay__ {
status = "okay";
spidev@0{
status = "disabled";
};
spidev@0{
status = "disabled";
};
};
};
spidev@1{
status = "disabled";
};
};
};
fragment@1 {
target = <&gpio>;
__overlay__ {
pitft_pins: pitft_pins {
brcm,pins = <25>; /* dc pin */
brcm,function = <1>; /* out */
brcm,pull = <0>; /* no pull */
};
};
};
fragment@1 {
target = <&gpio>;
__overlay__ {
pitft_pins: pitft_pins {
brcm,pins = <25>;
brcm,function = <1>; /* out */
brcm,pull = <0>; /* none */
};
};
};
fragment@2 {
target = <&spi0>;
__overlay__ {
/* needed to avoid dtc warning */
#address-cells = <1>;
#size-cells = <0>;
fragment@2 {
target = <&spi0>;
__overlay__ {
/* needed to avoid dtc warning */
#address-cells = <1>;
#size-cells = <0>;
pitft: pitft@0{
compatible = "multi-inno,mi0283qt";
reg = <0>;
pinctrl-names = "default";
pinctrl-0 = <&pitft_pins>;
pitft: pitft@0{
compatible = "sitronix,st7789v";
reg = <0>;
pinctrl-names = "default";
pinctrl-0 = <&pitft_pins>;
spi-max-frequency = <32000000>;
rotate = <270>;
width = <135>;
height = <240>;
buswidth = <8>;
dc-gpios = <&gpio 25 0>;
led-gpios = <&gpio 26 0>;
debug = <0>;
};
};
};
spi-max-frequency = <32000000>;
rotation = <90>;
width = <136>;
height = <240>;
col_offset = <53>;
row_offset = <40>;
dc-gpios = <&gpio 25 0>;
backlight = <&backlight>;
};
};
};
fragment@3 {
target-path = "/soc";
__overlay__ {
backlight: backlight {
compatible = "gpio-backlight";
gpios = <&gpio 22 0>;
};
};
};
__overrides__ {
speed = <&pitft>,"spi-max-frequency:0";
rotation = <&pitft>,"rotation:0";
width = <&pitft>,"width:0";
height = <&pitft>,"height:0";
col_offset = <&pitft>,"col_offset:0";
row_offset = <&pitft>,"row_offset:0";
};
__overrides__ {
speed = <&pitft>,"spi-max-frequency:0";
rotate = <&pitft>,"rotate:0";
width = <&pitft>,"width:0";
height = <&pitft>,"height:0";
fps = <&pitft>,"fps:0";
debug = <&pitft>,"debug:0";
};
};

View file

@ -43,7 +43,7 @@
#size-cells = <0>;
pitft: pitft@0{
compatible = "sitronix,st7789v";
compatible = "sitronix,st7789v";
reg = <0>;
pinctrl-names = "default";
pinctrl-0 = <&pitft_pins>;

View file

@ -43,7 +43,7 @@
#size-cells = <0>;
pitft: pitft@0{
compatible = "sitronix,st7789v";
compatible = "sitronix,st7789v";
reg = <0>;
pinctrl-names = "default";
pinctrl-0 = <&pitft_pins>;

View file

@ -1,15 +1,13 @@
obj-m += st7789v_ada.o
obj-m += fb_st7789v.o
KDIR ?= /lib/modules/`uname -r`/build
default:
$(MAKE) -C $(KDIR) M=$(pwd) modules
$(MAKE) -C $(KDIR) M=$(shell pwd) modules
install:
$(MAKE) -C $(KDIR) M=$(PWD) modules_install
$(MAKE) -C $(KDIR) M=$(shell pwd) modules_install
$(DEPMOD)
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
$(MAKE) -C $(KDIR) M=$(shell pwd) clean

View file

@ -16,31 +16,16 @@
#define DRVNAME "fb_st7789v"
static unsigned int width;
module_param(width, uint, 0000);
MODULE_PARM_DESC(width, "Display width");
static unsigned int height;
module_param(height, uint, 0000);
MODULE_PARM_DESC(height, "Display height");
static u32 col_offset = 0;
static u32 row_offset = 0;
static u8 col_hack_fix_offset = 0;
static short x_offset = 0;
static short y_offset = 0;
#define ST77XX_MADCTL_MY 0x80
#define ST77XX_MADCTL_MX 0x40
#define ST77XX_MADCTL_MV 0x20
#define ST77XX_MADCTL_ML 0x10
#define ST77XX_MADCTL_BGR 0x08
#define ST77XX_MADCTL_RGB 0x00
#define DEFAULT_GAMMA \
"70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25\n" \
"70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25"
#define HSD20_IPS_GAMMA \
"D0 05 0A 09 08 05 2E 44 45 0F 17 16 2B 33\n" \
"D0 05 0A 09 08 05 2E 43 45 0F 16 16 2B 33"
#define HSD20_IPS 1
/**
* enum st7789v_command - ST7789V display controller commands
*
@ -76,6 +61,18 @@ enum st7789v_command {
NVGAMCTRL = 0xE1,
};
#define MADCTL_BGR BIT(3) /* bitmask for RGB/BGR order */
#define MADCTL_MV BIT(5) /* bitmask for page/column order */
#define MADCTL_MX BIT(6) /* bitmask for column address order */
#define MADCTL_MY BIT(7) /* bitmask for page address order */
static u32 col1_offset = 0;
static u32 col2_offset = 0;
static u32 row1_offset = 0;
static u32 row2_offset = 0;
static short x_offset = 0;
static short y_offset = 0;
/**
* init_display() - initialize the display controller
*
@ -92,44 +89,85 @@ enum st7789v_command {
*/
static int init_display(struct fbtft_par *par)
{
printk(KERN_INFO "ST7789 adafruit fbtft driver\n");
width = par->info->var.xres;
height = par->info->var.yres;
if ((width == 0) || (width > 240)) {
width = 240;
}
if ((height == 0) || (height > 320)) {
height = 320;
}
printk(KERN_INFO "Size: (%d, %d)\n", width, height);
// Go to sleep
write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
// Soft reset
write_reg(par, MIPI_DCS_SOFT_RESET);
mdelay(150);
/* turn off sleep mode */
write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
mdelay(10);
mdelay(120);
/* set pixel format to RGB-565 */
write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
if (HSD20_IPS)
write_reg(par, PORCTRL, 0x05, 0x05, 0x00, 0x33, 0x33);
write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, 0);
write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS, 0, 0, 0, 240);
write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS, 0, 0, 320>>8, 320&0xFF);
write_reg(par, MIPI_DCS_ENTER_INVERT_MODE); // odd hack, displays are inverted
write_reg(par, MIPI_DCS_ENTER_NORMAL_MODE);
mdelay(10);
else
write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
/*
* VGH = 13.26V
* VGL = -10.43V
*/
if (HSD20_IPS)
write_reg(par, GCTRL, 0x75);
else
write_reg(par, GCTRL, 0x35);
/*
* VDV and VRH register values come from command write
* (instead of NVM)
*/
write_reg(par, VDVVRHEN, 0x01, 0xFF);
/*
* VAP = 4.1V + (VCOM + VCOM offset + 0.5 * VDV)
* VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
*/
if (HSD20_IPS)
write_reg(par, VRHS, 0x13);
else
write_reg(par, VRHS, 0x0B);
/* VDV = 0V */
write_reg(par, VDVS, 0x20);
/* VCOM = 0.9V */
if (HSD20_IPS)
write_reg(par, VCOMS, 0x22);
else
write_reg(par, VCOMS, 0x20);
/* VCOM offset = 0V */
write_reg(par, VCMOFSET, 0x20);
/*
* AVDD = 6.8V
* AVCL = -4.8V
* VDS = 2.3V
*/
write_reg(par, PWCTRL1, 0xA4, 0xA1);
write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
if (HSD20_IPS)
write_reg(par, MIPI_DCS_ENTER_INVERT_MODE);
return 0;
}
static void minipitft_set_addr_win(struct fbtft_par *par, int xs, int ys,
int xe, int ye)
{
xs += x_offset;
xe += x_offset;
ys += y_offset;
ye += y_offset;
write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
xs >> 8, xs & 0xFF, xe >> 8, xe & 0xFF);
write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
ys >> 8, ys & 0xFF, ye >> 8, ye & 0xFF);
write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
}
/**
* set_var() - apply LCD properties like rotation and BGR mode
*
@ -139,36 +177,104 @@ static int init_display(struct fbtft_par *par)
*/
static int set_var(struct fbtft_par *par)
{
u8 addr_mode = 0;
u8 madctl_par = 0;
struct fbtft_display *display = &par->pdata->display;
u32 width = display->width;
u32 height = display->height;
if (par->bgr)
madctl_par |= MADCTL_BGR;
if (width < 240) {
// Display is centered
row1_offset = row2_offset = (int)((320 - height + 1) / 2);
col1_offset = (int)((240 - width) / 2);
col2_offset = (int)((240 - width + 1) / 2);
} else {
row1_offset = 0;
row2_offset = (320 - height);
col1_offset = col2_offset = (240 - width);
}
switch (par->info->var.rotate) {
case 0:
addr_mode = 0;
x_offset = col_offset;
y_offset = row_offset;
x_offset = col1_offset;
y_offset = row1_offset;
break;
case 90:
addr_mode = ST77XX_MADCTL_MV | ST77XX_MADCTL_MX;
x_offset = row_offset;
y_offset = col_offset;
break;
madctl_par |= (MADCTL_MV | MADCTL_MY);
x_offset = row1_offset;
y_offset = col1_offset;
break;
case 180:
addr_mode = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY;
x_offset = (240 - width) - col_offset + col_hack_fix_offset;
// hack tweak to account for extra pixel width to make even
y_offset = (320 - height) - row_offset;
madctl_par |= (MADCTL_MX | MADCTL_MY);
x_offset = col2_offset;
y_offset = row2_offset;
break;
case 270:
addr_mode = ST77XX_MADCTL_MV | ST77XX_MADCTL_MY;
x_offset = (320 - height) - row_offset;
y_offset = (240 - width) - col_offset;
madctl_par |= (MADCTL_MV | MADCTL_MX);
x_offset = row2_offset;
y_offset = col2_offset;
break;
default:
return -EINVAL;
}
printk(KERN_INFO "Rotation %d offsets %d %d\n", par->info->var.rotate, x_offset, y_offset);
write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
return 0;
}
write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
/**
* set_gamma() - set gamma curves
*
* @par: FBTFT parameter object
* @curves: gamma curves
*
* Before the gamma curves are applied, they are preprocessed with a bitmask
* to ensure syntactically correct input for the display controller.
* This implies that the curves input parameter might be changed by this
* function and that illegal gamma values are auto-corrected and not
* reported as errors.
*
* Return: 0 on success, < 0 if error occurred.
*/
static int set_gamma(struct fbtft_par *par, u32 *curves)
{
int i;
int j;
int c; /* curve index offset */
/*
* Bitmasks for gamma curve command parameters.
* The masks are the same for both positive and negative voltage
* gamma curves.
*/
static const u8 gamma_par_mask[] = {
0xFF, /* V63[3:0], V0[3:0]*/
0x3F, /* V1[5:0] */
0x3F, /* V2[5:0] */
0x1F, /* V4[4:0] */
0x1F, /* V6[4:0] */
0x3F, /* J0[1:0], V13[3:0] */
0x7F, /* V20[6:0] */
0x77, /* V36[2:0], V27[2:0] */
0x7F, /* V43[6:0] */
0x3F, /* J1[1:0], V50[3:0] */
0x1F, /* V57[4:0] */
0x1F, /* V59[4:0] */
0x3F, /* V61[5:0] */
0x3F, /* V62[5:0] */
};
for (i = 0; i < par->gamma.num_curves; i++) {
c = i * par->gamma.num_values;
for (j = 0; j < par->gamma.num_values; j++)
curves[c + j] &= gamma_par_mask[j];
write_reg(par, PVGAMCTRL + i,
curves[c + 0], curves[c + 1], curves[c + 2],
curves[c + 3], curves[c + 4], curves[c + 5],
curves[c + 6], curves[c + 7], curves[c + 8],
curves[c + 9], curves[c + 10], curves[c + 11],
curves[c + 12], curves[c + 13]);
}
return 0;
}
@ -195,11 +301,13 @@ static struct fbtft_display display = {
.height = 320,
.gamma_num = 2,
.gamma_len = 14,
.gamma = DEFAULT_GAMMA,
.gamma = HSD20_IPS_GAMMA,
.fbtftops = {
.init_display = init_display,
.set_var = set_var,
.set_gamma = set_gamma,
.blank = blank,
.set_addr_win = minipitft_set_addr_win,
},
};

View file

@ -251,7 +251,8 @@ void fbtft_register_backlight(struct fbtft_par *par);
void fbtft_unregister_backlight(struct fbtft_par *par);
int fbtft_init_display(struct fbtft_par *par);
int fbtft_probe_common(struct fbtft_display *display, struct spi_device *sdev,
struct platform_device *pdev);
struct platform_device *pdev,
const struct of_device_id *dt_ids);
int fbtft_remove_common(struct device *dev, struct fb_info *info);
/* fbtft-io.c */
@ -272,11 +273,13 @@ void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...);
void fbtft_write_reg16_bus8(struct fbtft_par *par, int len, ...);
void fbtft_write_reg16_bus16(struct fbtft_par *par, int len, ...);
#define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \
#define FBTFT_REGISTER_DRIVER_START(_display) \
\
static const struct of_device_id dt_ids[]; \
\
static int fbtft_driver_probe_spi(struct spi_device *spi) \
{ \
return fbtft_probe_common(_display, spi, NULL); \
return fbtft_probe_common(_display, spi, NULL, dt_ids); \
} \
\
static int fbtft_driver_remove_spi(struct spi_device *spi) \
@ -288,7 +291,7 @@ static int fbtft_driver_remove_spi(struct spi_device *spi) \
\
static int fbtft_driver_probe_pdev(struct platform_device *pdev) \
{ \
return fbtft_probe_common(_display, NULL, pdev); \
return fbtft_probe_common(_display, NULL, pdev, dt_ids); \
} \
\
static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
@ -298,8 +301,16 @@ static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
return fbtft_remove_common(&pdev->dev, info); \
} \
\
static const struct of_device_id dt_ids[] = { \
{ .compatible = _compatible }, \
static const struct of_device_id dt_ids[] = {
#define FBTFT_COMPATIBLE(_compatible) \
{ .compatible = _compatible },
#define FBTFT_VARIANT_COMPATIBLE(_compatible, _variant) \
{ .compatible = _compatible, .data = _variant },
#define FBTFT_REGISTER_DRIVER_END(_name, _display) \
\
{}, \
}; \
\
@ -309,7 +320,7 @@ MODULE_DEVICE_TABLE(of, dt_ids); \
static struct spi_driver fbtft_driver_spi_driver = { \
.driver = { \
.name = _name, \
.of_match_table = of_match_ptr(dt_ids), \
.of_match_table = dt_ids, \
}, \
.probe = fbtft_driver_probe_spi, \
.remove = fbtft_driver_remove_spi, \
@ -319,7 +330,7 @@ static struct platform_driver fbtft_driver_platform_driver = { \
.driver = { \
.name = _name, \
.owner = THIS_MODULE, \
.of_match_table = of_match_ptr(dt_ids), \
.of_match_table = dt_ids, \
}, \
.probe = fbtft_driver_probe_pdev, \
.remove = fbtft_driver_remove_pdev, \
@ -344,13 +355,26 @@ static void __exit fbtft_driver_module_exit(void) \
module_init(fbtft_driver_module_init); \
module_exit(fbtft_driver_module_exit);
#define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \
FBTFT_REGISTER_DRIVER_START(_display) \
FBTFT_COMPATIBLE(_compatible) \
FBTFT_REGISTER_DRIVER_END(_name, _display)
/* Debug macros */
/* shorthand debug levels */
#define DEBUG_LEVEL_1 DEBUG_REQUEST_GPIOS
#define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS | DEBUG_TIME_FIRST_UPDATE)
#define DEBUG_LEVEL_3 (DEBUG_LEVEL_2 | DEBUG_RESET | DEBUG_INIT_DISPLAY | DEBUG_BLANK | DEBUG_REQUEST_GPIOS | DEBUG_FREE_GPIOS | DEBUG_VERIFY_GPIOS | DEBUG_BACKLIGHT | DEBUG_SYSFS)
#define DEBUG_LEVEL_4 (DEBUG_LEVEL_2 | DEBUG_FB_READ | DEBUG_FB_WRITE | DEBUG_FB_FILLRECT | DEBUG_FB_COPYAREA | DEBUG_FB_IMAGEBLIT | DEBUG_FB_BLANK)
#define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS \
| DEBUG_TIME_FIRST_UPDATE)
#define DEBUG_LEVEL_3 (DEBUG_LEVEL_2 | DEBUG_RESET | DEBUG_INIT_DISPLAY \
| DEBUG_BLANK | DEBUG_REQUEST_GPIOS \
| DEBUG_FREE_GPIOS \
| DEBUG_VERIFY_GPIOS \
| DEBUG_BACKLIGHT | DEBUG_SYSFS)
#define DEBUG_LEVEL_4 (DEBUG_LEVEL_2 | DEBUG_FB_READ | DEBUG_FB_WRITE \
| DEBUG_FB_FILLRECT \
| DEBUG_FB_COPYAREA \
| DEBUG_FB_IMAGEBLIT | DEBUG_FB_BLANK)
#define DEBUG_LEVEL_5 (DEBUG_LEVEL_3 | DEBUG_UPDATE_DISPLAY)
#define DEBUG_LEVEL_6 (DEBUG_LEVEL_4 | DEBUG_LEVEL_5)
#define DEBUG_LEVEL_7 0xFFFFFFFF
@ -398,8 +422,8 @@ do { \
#define fbtft_par_dbg(level, par, format, arg...) \
do { \
if (unlikely(par->debug & level)) \
dev_info(par->info->device, format, ##arg); \
if (unlikely((par)->debug & (level))) \
dev_info((par)->info->device, format, ##arg); \
} while (0)
#define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \

View file

@ -1,432 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* DRM driver for ST7789V panels with flexible config
*
* Copyright 2021 Melissa LeBlanc-Williams
* Copyright 2019 Limor Fried
* Copyright 2016 Noralf Trønnes
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/backlight.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/regulator/consumer.h>
#include <linux/spi/spi.h>
#include <linux/version.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_damage_helper.h>
#include <drm/drm_drv.h>
#include <drm/drm_fb_cma_helper.h>
#include <drm/drm_fb_helper.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_gem_cma_helper.h>
#include <drm/drm_gem_framebuffer_helper.h>
#include <drm/drm_mipi_dbi.h>
#include <drm/drm_rect.h>
#include <drm/drm_vblank.h>
#include <drm/drm_modeset_helper.h>
#include <video/mipi_display.h>
#define ST77XX_MADCTL_MY 0x80
#define ST77XX_MADCTL_MX 0x40
#define ST77XX_MADCTL_MV 0x20
#define ST77XX_MADCTL_ML 0x10
#define ST77XX_MADCTL_BGR 0x08
#define ST77XX_MADCTL_RGB 0x00
static u32 col_offset = 0;
static u32 row_offset = 0;
static u8 col_hack_fix_offset = 0;
static short x_offset = 0;
static short y_offset = 0;
static void st7789vada_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
{
struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
unsigned int height = rect->y2 - rect->y1;
unsigned int width = rect->x2 - rect->x1;
struct mipi_dbi *dbi = &dbidev->dbi;
bool swap = dbi->swap_bytes;
u16 x1, x2, y1, y2;
int idx, ret = 0;
bool full;
void *tr;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(5,8,0)
if (!dbidev->enabled)
return;
#endif
if (!drm_dev_enter(fb->dev, &idx))
return;
full = width == fb->width && height == fb->height;
DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
if (!dbi->dc || !full || swap ||
fb->format->format == DRM_FORMAT_XRGB8888) {
tr = dbidev->tx_buf;
ret = mipi_dbi_buf_copy(dbidev->tx_buf, fb, rect, swap);
if (ret)
goto err_msg;
} else {
tr = cma_obj->vaddr;
}
x1 = rect->x1 + x_offset;
x2 = rect->x2 - 1 + x_offset;
y1 = rect->y1 + y_offset;
y2 = rect->y2 - 1 + y_offset;
//printk(KERN_INFO "setaddrwin (%d, %d) -> (%d, %d) offsets: %d & %d \n", x1, y1, x2, y2, x_offset, y_offset);
mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS,
(x1 >> 8) & 0xFF, x1 & 0xFF,
(x2 >> 8) & 0xFF, x2 & 0xFF);
mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS,
(y1 >> 8) & 0xFF, y1 & 0xFF,
(y2 >> 8) & 0xFF, y2 & 0xFF);
ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START, tr,
width*height * 2);
err_msg:
if (ret)
dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
drm_dev_exit(idx);
}
static void st7789vada_pipe_update(struct drm_simple_display_pipe *pipe,
struct drm_plane_state *old_state)
{
struct drm_plane_state *state = pipe->plane.state;
struct drm_crtc *crtc = &pipe->crtc;
struct drm_rect rect;
if (drm_atomic_helper_damage_merged(old_state, state, &rect))
st7789vada_fb_dirty(state->fb, &rect);
if (crtc->state->event) {
spin_lock_irq(&crtc->dev->event_lock);
drm_crtc_send_vblank_event(crtc, crtc->state->event);
spin_unlock_irq(&crtc->dev->event_lock);
crtc->state->event = NULL;
}
}
static struct drm_display_mode st7789vada_mode = {
DRM_SIMPLE_MODE(240, 320, 58, 43),
};
DEFINE_DRM_GEM_CMA_FOPS(st7789vada_fops);
#if LINUX_VERSION_CODE <= KERNEL_VERSION(5,8,0)
static struct drm_driver st7789vada_driver = {
.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
.fops = &st7789vada_fops,
.release = mipi_dbi_release,
DRM_GEM_CMA_VMAP_DRIVER_OPS,
.debugfs_init = mipi_dbi_debugfs_init,
.name = "st7789vada",
.desc = "ST7789V Adafruit",
.date = "20200727",
.major = 1,
.minor = 0,
};
#else
static struct drm_driver st7789vada_driver = {
.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
.fops = &st7789vada_fops,
DRM_GEM_CMA_DRIVER_OPS_VMAP,
.debugfs_init = mipi_dbi_debugfs_init,
.name = "st7789vada",
.desc = "ST7789V Adafruit",
.date = "20200727",
.major = 1,
.minor = 0,
};
#endif
static const struct of_device_id st7789vada_of_match[] = {
{ .compatible = "multi-inno,mi0283qt" },
{},
};
MODULE_DEVICE_TABLE(of, st7789vada_of_match);
static const struct spi_device_id st7789vada_id[] = {
{ "st7789vada", 0 },
{ },
};
MODULE_DEVICE_TABLE(spi, st7789vada_id);
static void st7789vada_enable(struct drm_simple_display_pipe *pipe,
struct drm_crtc_state *crtc_state,
struct drm_plane_state *plane_state)
{
struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
struct mipi_dbi *dbi = &dbidev->dbi;
u8 addr_mode;
u16 width = st7789vada_mode.htotal;
u16 height = st7789vada_mode.vtotal;
int ret, idx;
//printk(KERN_INFO "w/h %d %d\n", width, height);
if (!drm_dev_enter(pipe->crtc.dev, &idx))
return;
DRM_DEBUG_KMS("\n");
ret = mipi_dbi_poweron_conditional_reset(dbidev);
if (ret < 0)
goto out_exit;
if (ret == 1)
goto out_enable;
mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
mipi_dbi_command(dbi, MIPI_DCS_SOFT_RESET);
msleep(150);
mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
msleep(10);
mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55); // 16 bit color
msleep(10);
mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, 0);
mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS, 0, 0, 0, 240);
mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS, 0, 0, 320>>8, 320&0xFF);
mipi_dbi_command(dbi, MIPI_DCS_ENTER_INVERT_MODE); // odd hack, displays are inverted
mipi_dbi_command(dbi, MIPI_DCS_ENTER_NORMAL_MODE);
msleep(10);
mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
msleep(10);
out_enable:
/* The PiTFT has a hardware reset circuit that
* resets only on power-on and not on each reboot through
* a gpio like the rpi-display does.
* As a result, we need to always apply the rotation value
* regardless of the display "on/off" state.
*/
switch (dbidev->rotation) {
default:
addr_mode = 0;
x_offset = col_offset;
y_offset = row_offset;
break;
case 90:
addr_mode = ST77XX_MADCTL_MV | ST77XX_MADCTL_MX;
x_offset = row_offset;
y_offset = col_offset;
break;
case 180:
addr_mode = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY;
x_offset = (240 - width) - col_offset + col_hack_fix_offset;
// hack tweak to account for extra pixel width to make even
y_offset = (320 - height) - row_offset;
break;
case 270:
addr_mode = ST77XX_MADCTL_MV | ST77XX_MADCTL_MY;
x_offset = (320 - height) - row_offset;
y_offset = (240 - width) - col_offset;
break;
}
//printk(KERN_INFO "Rotation offsets %d %d\n", x_offset, y_offset);
mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
out_exit:
drm_dev_exit(idx);
}
static const struct drm_simple_display_pipe_funcs st7789vada_pipe_funcs = {
.enable = st7789vada_enable,
.disable = mipi_dbi_pipe_disable,
.update = st7789vada_pipe_update,
.prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
};
static int st7789vada_probe(struct spi_device *spi)
{
struct device *dev = &spi->dev;
struct mipi_dbi_dev *dbidev;
struct drm_device *drm;
struct mipi_dbi *dbi;
struct gpio_desc *dc;
u32 rotation = 0;
u32 width = 240;
u32 height = 320;
int ret;
dbidev = kzalloc(sizeof(*dbidev), GFP_KERNEL);
if (!dbidev)
return -ENOMEM;
//printk(KERN_INFO "ST7789 fake driver\n");
dbi = &dbidev->dbi;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(5,8,0)
drm = &dbidev->drm;
ret = devm_drm_dev_init(dev, drm, &st7789vada_driver);
#else
drm = drm_dev_alloc(&st7789vada_driver, dev);
ret = drm_dev_register(drm, 0);
#endif
if (ret) {
kfree(dbidev);
return ret;
}
drm_mode_config_init(drm);
dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
if (IS_ERR(dbi->reset)) {
DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
return PTR_ERR(dbi->reset);
}
dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
if (IS_ERR(dc)) {
DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
return PTR_ERR(dc);
}
dbidev->regulator = devm_regulator_get(dev, "power");
if (IS_ERR(dbidev->regulator))
return PTR_ERR(dbidev->regulator);
dbidev->backlight = devm_of_find_backlight(dev);
if (IS_ERR(dbidev->backlight))
return PTR_ERR(dbidev->backlight);
device_property_read_u32(dev, "rotation", &rotation);
//printk(KERN_INFO "Rotation %d\n", rotation);
device_property_read_u32(dev, "width", &width);
if (width % 2) {
width +=1; // odd width will cause a kernel panic
col_hack_fix_offset = 3;
} else {
col_hack_fix_offset = 0;
}
//printk(KERN_INFO "Width %d\n", width);
if ((width == 0) || (width > 240)) {
width = 240; // default to full framebuff;
}
device_property_read_u32(dev, "height", &height);
//printk(KERN_INFO "Height %d\n", height);
if ((height == 0) || (height > 320)) {
height = 320; // default to full framebuff;
}
st7789vada_mode.hdisplay = st7789vada_mode.hsync_start =
st7789vada_mode.hsync_end = st7789vada_mode.htotal = width;
st7789vada_mode.vdisplay = st7789vada_mode.vsync_start =
st7789vada_mode.vsync_end = st7789vada_mode.vtotal = height;
device_property_read_u32(dev, "col_offset", &col_offset);
//printk(KERN_INFO "Column offset %d\n", col_offset);
device_property_read_u32(dev, "row_offset", &row_offset);
//printk(KERN_INFO "Row offset %d\n", row_offset);
ret = mipi_dbi_spi_init(spi, dbi, dc);
if (ret)
return ret;
/* Cannot read from this controller via SPI */
dbi->read_commands = NULL;
ret = mipi_dbi_dev_init(dbidev, &st7789vada_pipe_funcs, &st7789vada_mode, rotation);
if (ret)
return ret;
drm_mode_config_reset(drm);
ret = drm_dev_register(drm, 0);
if (ret)
return ret;
spi_set_drvdata(spi, drm);
drm_fbdev_generic_setup(drm, 0);
return 0;
}
static int st7789vada_remove(struct spi_device *spi)
{
struct drm_device *drm = spi_get_drvdata(spi);
drm_dev_unplug(drm);
drm_atomic_helper_shutdown(drm);
return 0;
}
static void st7789vada_shutdown(struct spi_device *spi)
{
drm_atomic_helper_shutdown(spi_get_drvdata(spi));
}
static int __maybe_unused st7789vada_pm_suspend(struct device *dev)
{
return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
}
static int __maybe_unused st7789vada_pm_resume(struct device *dev)
{
drm_mode_config_helper_resume(dev_get_drvdata(dev));
return 0;
}
static const struct dev_pm_ops st7789vada_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(st7789vada_pm_suspend, st7789vada_pm_resume)
};
static struct spi_driver st7789vada_spi_driver = {
.driver = {
.name = "st7789vada",
.owner = THIS_MODULE,
.of_match_table = st7789vada_of_match,
.pm = &st7789vada_pm_ops,
},
.id_table = st7789vada_id,
.probe = st7789vada_probe,
.remove = st7789vada_remove,
.shutdown = st7789vada_shutdown,
};
module_spi_driver(st7789vada_spi_driver);
MODULE_DESCRIPTION("Sitronix ST7789V Flexible DRM driver");
MODULE_AUTHOR("Noralf Trønnes + Limor Fried + Melissa LeBlanc-Williams");
MODULE_LICENSE("GPL");