1 // SPDX-License-Identifier: GPL-2.0+
3 * FB driver for the Watterott LCD Controller
5 * Copyright (C) 2013 Noralf Tronnes
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
16 #define DRVNAME "fb_watterott"
21 #define DEFAULT_BRIGHTNESS 50
23 #define CMD_VERSION 0x01
24 #define CMD_LCD_LED 0x10
25 #define CMD_LCD_RESET 0x11
26 #define CMD_LCD_ORIENTATION 0x20
27 #define CMD_LCD_DRAWIMAGE 0x27
28 #define COLOR_RGB323 8
29 #define COLOR_RGB332 9
30 #define COLOR_RGB233 10
31 #define COLOR_RGB565 16
33 static short mode = 565;
34 module_param(mode, short, 0000);
35 MODULE_PARM_DESC(mode, "RGB color transfer mode: 332, 565 (default)");
37 static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
44 for (i = 0; i < len; i++)
45 *buf++ = (u8)va_arg(args, unsigned int);
48 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
49 par->info->device, u8, par->buf,
50 len, "%s: ", __func__);
52 ret = par->fbtftops.write(par, par->buf, len);
54 dev_err(par->info->device,
55 "write() failed and returned %d\n", ret);
60 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
62 unsigned int start_line, end_line;
63 u16 *vmem16 = (u16 *)(par->info->screen_buffer + offset);
64 __be16 *pos = par->txbuf.buf + 1;
65 __be16 *buf16 = par->txbuf.buf + 10;
69 start_line = offset / par->info->fix.line_length;
70 end_line = start_line + (len / par->info->fix.line_length) - 1;
72 /* Set command header. pos: x, y, w, h */
73 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
75 pos[2] = cpu_to_be16(par->info->var.xres);
76 pos[3] = cpu_to_be16(1);
77 ((u8 *)par->txbuf.buf)[9] = COLOR_RGB565;
79 for (i = start_line; i <= end_line; i++) {
80 pos[1] = cpu_to_be16(i);
81 for (j = 0; j < par->info->var.xres; j++)
82 buf16[j] = cpu_to_be16(*vmem16++);
83 ret = par->fbtftops.write(par,
84 par->txbuf.buf, 10 + par->info->fix.line_length);
93 #define RGB565toRGB323(c) ((((c) & 0xE000) >> 8) |\
94 (((c) & 000600) >> 6) |\
95 (((c) & 0x001C) >> 2))
96 #define RGB565toRGB332(c) ((((c) & 0xE000) >> 8) |\
97 (((c) & 000700) >> 6) |\
98 (((c) & 0x0018) >> 3))
99 #define RGB565toRGB233(c) ((((c) & 0xC000) >> 8) |\
100 (((c) & 000700) >> 5) |\
101 (((c) & 0x001C) >> 2))
103 static int write_vmem_8bit(struct fbtft_par *par, size_t offset, size_t len)
105 unsigned int start_line, end_line;
106 u16 *vmem16 = (u16 *)(par->info->screen_buffer + offset);
107 __be16 *pos = par->txbuf.buf + 1;
108 u8 *buf8 = par->txbuf.buf + 10;
112 start_line = offset / par->info->fix.line_length;
113 end_line = start_line + (len / par->info->fix.line_length) - 1;
115 /* Set command header. pos: x, y, w, h */
116 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
118 pos[2] = cpu_to_be16(par->info->var.xres);
119 pos[3] = cpu_to_be16(1);
120 ((u8 *)par->txbuf.buf)[9] = COLOR_RGB332;
122 for (i = start_line; i <= end_line; i++) {
123 pos[1] = cpu_to_be16(i);
124 for (j = 0; j < par->info->var.xres; j++) {
125 buf8[j] = RGB565toRGB332(*vmem16);
128 ret = par->fbtftops.write(par,
129 par->txbuf.buf, 10 + par->info->var.xres);
138 static unsigned int firmware_version(struct fbtft_par *par)
142 write_reg(par, CMD_VERSION);
143 par->fbtftops.read(par, rxbuf, 4);
147 return (rxbuf[0] - '0') << 8 | (rxbuf[2] - '0') << 4 | (rxbuf[3] - '0');
150 static int init_display(struct fbtft_par *par)
153 unsigned int version;
156 /* enable SPI interface by having CS and MOSI low during reset */
157 save_mode = par->spi->mode;
158 par->spi->mode |= SPI_CS_HIGH;
159 ret = spi_setup(par->spi); /* set CS inactive low */
161 dev_err(par->info->device, "Could not set SPI_CS_HIGH\n");
164 write_reg(par, 0x00); /* make sure mode is set */
167 par->fbtftops.reset(par);
169 par->spi->mode = save_mode;
170 ret = spi_setup(par->spi);
172 dev_err(par->info->device, "Could not restore SPI mode\n");
175 write_reg(par, 0x00);
177 version = firmware_version(par);
178 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "Firmware version: %x.%02x\n",
179 version >> 8, version & 0xFF);
182 par->fbtftops.write_vmem = write_vmem_8bit;
186 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
188 /* not used on this controller */
191 static int set_var(struct fbtft_par *par)
195 /* this controller rotates clock wise */
196 switch (par->info->var.rotate) {
209 write_reg(par, CMD_LCD_ORIENTATION, rotate);
214 static int verify_gpios(struct fbtft_par *par)
216 if (par->gpio.reset < 0) {
217 dev_err(par->info->device, "Missing 'reset' gpio. Aborting.\n");
223 #ifdef CONFIG_FB_BACKLIGHT
224 static int backlight_chip_update_status(struct backlight_device *bd)
226 struct fbtft_par *par = bl_get_data(bd);
227 int brightness = bd->props.brightness;
229 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
230 "%s: brightness=%d, power=%d, fb_blank=%d\n", __func__,
231 bd->props.brightness, bd->props.power,
234 if (bd->props.power != FB_BLANK_UNBLANK)
237 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
240 write_reg(par, CMD_LCD_LED, brightness);
245 static const struct backlight_ops bl_ops = {
246 .update_status = backlight_chip_update_status,
249 static void register_chip_backlight(struct fbtft_par *par)
251 struct backlight_device *bd;
252 struct backlight_properties bl_props = { 0, };
254 bl_props.type = BACKLIGHT_RAW;
255 bl_props.power = FB_BLANK_POWERDOWN;
256 bl_props.max_brightness = 100;
257 bl_props.brightness = DEFAULT_BRIGHTNESS;
259 bd = backlight_device_register(dev_driver_string(par->info->device),
260 par->info->device, par, &bl_ops,
263 dev_err(par->info->device,
264 "cannot register backlight device (%ld)\n",
268 par->info->bl_dev = bd;
270 if (!par->fbtftops.unregister_backlight)
271 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
274 #define register_chip_backlight NULL
277 static struct fbtft_display display = {
283 .txbuflen = TXBUFLEN,
285 .write_register = write_reg8_bus8,
286 .write_vmem = write_vmem,
287 .init_display = init_display,
288 .set_addr_win = set_addr_win,
290 .verify_gpios = verify_gpios,
291 .register_backlight = register_chip_backlight,
295 FBTFT_REGISTER_DRIVER(DRVNAME, "watterott,openlcd", &display);
297 MODULE_ALIAS("spi:" DRVNAME);
299 MODULE_DESCRIPTION("FB driver for the Watterott LCD Controller");
300 MODULE_AUTHOR("Noralf Tronnes");
301 MODULE_LICENSE("GPL");