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/consumer.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 static inline int rgb565_to_rgb332(u16 c)
95 return ((c & 0xE000) >> 8) | ((c & 000700) >> 6) | ((c & 0x0018) >> 3);
98 static int write_vmem_8bit(struct fbtft_par *par, size_t offset, size_t len)
100 unsigned int start_line, end_line;
101 u16 *vmem16 = (u16 *)(par->info->screen_buffer + offset);
102 __be16 *pos = par->txbuf.buf + 1;
103 u8 *buf8 = par->txbuf.buf + 10;
107 start_line = offset / par->info->fix.line_length;
108 end_line = start_line + (len / par->info->fix.line_length) - 1;
110 /* Set command header. pos: x, y, w, h */
111 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
113 pos[2] = cpu_to_be16(par->info->var.xres);
114 pos[3] = cpu_to_be16(1);
115 ((u8 *)par->txbuf.buf)[9] = COLOR_RGB332;
117 for (i = start_line; i <= end_line; i++) {
118 pos[1] = cpu_to_be16(i);
119 for (j = 0; j < par->info->var.xres; j++) {
120 buf8[j] = rgb565_to_rgb332(*vmem16);
123 ret = par->fbtftops.write(par,
124 par->txbuf.buf, 10 + par->info->var.xres);
133 static unsigned int firmware_version(struct fbtft_par *par)
137 write_reg(par, CMD_VERSION);
138 par->fbtftops.read(par, rxbuf, 4);
142 return (rxbuf[0] - '0') << 8 | (rxbuf[2] - '0') << 4 | (rxbuf[3] - '0');
145 static int init_display(struct fbtft_par *par)
148 unsigned int version;
151 /* enable SPI interface by having CS and MOSI low during reset */
152 save_mode = par->spi->mode;
153 par->spi->mode |= SPI_CS_HIGH;
154 ret = spi_setup(par->spi); /* set CS inactive low */
156 dev_err(par->info->device, "Could not set SPI_CS_HIGH\n");
159 write_reg(par, 0x00); /* make sure mode is set */
162 par->fbtftops.reset(par);
164 par->spi->mode = save_mode;
165 ret = spi_setup(par->spi);
167 dev_err(par->info->device, "Could not restore SPI mode\n");
170 write_reg(par, 0x00);
172 version = firmware_version(par);
173 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "Firmware version: %x.%02x\n",
174 version >> 8, version & 0xFF);
177 par->fbtftops.write_vmem = write_vmem_8bit;
181 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
183 /* not used on this controller */
186 static int set_var(struct fbtft_par *par)
190 /* this controller rotates clock wise */
191 switch (par->info->var.rotate) {
204 write_reg(par, CMD_LCD_ORIENTATION, rotate);
209 static int verify_gpios(struct fbtft_par *par)
211 if (!par->gpio.reset) {
212 dev_err(par->info->device, "Missing 'reset' gpio. Aborting.\n");
218 #ifdef CONFIG_FB_BACKLIGHT
219 static int backlight_chip_update_status(struct backlight_device *bd)
221 struct fbtft_par *par = bl_get_data(bd);
222 int brightness = bd->props.brightness;
224 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
225 "%s: brightness=%d, power=%d, fb_blank=%d\n", __func__,
226 bd->props.brightness, bd->props.power,
229 if (bd->props.power != FB_BLANK_UNBLANK)
232 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
235 write_reg(par, CMD_LCD_LED, brightness);
240 static const struct backlight_ops bl_ops = {
241 .update_status = backlight_chip_update_status,
244 static void register_chip_backlight(struct fbtft_par *par)
246 struct backlight_device *bd;
247 struct backlight_properties bl_props = { 0, };
249 bl_props.type = BACKLIGHT_RAW;
250 bl_props.power = FB_BLANK_POWERDOWN;
251 bl_props.max_brightness = 100;
252 bl_props.brightness = DEFAULT_BRIGHTNESS;
254 bd = backlight_device_register(dev_driver_string(par->info->device),
255 par->info->device, par, &bl_ops,
258 dev_err(par->info->device,
259 "cannot register backlight device (%ld)\n",
263 par->info->bl_dev = bd;
265 if (!par->fbtftops.unregister_backlight)
266 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
269 #define register_chip_backlight NULL
272 static struct fbtft_display display = {
278 .txbuflen = TXBUFLEN,
280 .write_register = write_reg8_bus8,
281 .write_vmem = write_vmem,
282 .init_display = init_display,
283 .set_addr_win = set_addr_win,
285 .verify_gpios = verify_gpios,
286 .register_backlight = register_chip_backlight,
290 FBTFT_REGISTER_DRIVER(DRVNAME, "watterott,openlcd", &display);
292 MODULE_ALIAS("spi:" DRVNAME);
294 MODULE_DESCRIPTION("FB driver for the Watterott LCD Controller");
295 MODULE_AUTHOR("Noralf Tronnes");
296 MODULE_LICENSE("GPL");