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/delay.h>
15 #define DRVNAME "fb_watterott"
20 #define DEFAULT_BRIGHTNESS 50
22 #define CMD_VERSION 0x01
23 #define CMD_LCD_LED 0x10
24 #define CMD_LCD_RESET 0x11
25 #define CMD_LCD_ORIENTATION 0x20
26 #define CMD_LCD_DRAWIMAGE 0x27
27 #define COLOR_RGB323 8
28 #define COLOR_RGB332 9
29 #define COLOR_RGB233 10
30 #define COLOR_RGB565 16
32 static short mode = 565;
33 module_param(mode, short, 0000);
34 MODULE_PARM_DESC(mode, "RGB color transfer mode: 332, 565 (default)");
36 static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
43 for (i = 0; i < len; i++)
44 *buf++ = (u8)va_arg(args, unsigned int);
47 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
48 par->info->device, u8, par->buf,
49 len, "%s: ", __func__);
51 ret = par->fbtftops.write(par, par->buf, len);
53 dev_err(par->info->device,
54 "write() failed and returned %d\n", ret);
59 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
61 unsigned int start_line, end_line;
62 u16 *vmem16 = (u16 *)(par->info->screen_buffer + offset);
63 __be16 *pos = par->txbuf.buf + 1;
64 __be16 *buf16 = par->txbuf.buf + 10;
68 start_line = offset / par->info->fix.line_length;
69 end_line = start_line + (len / par->info->fix.line_length) - 1;
71 /* Set command header. pos: x, y, w, h */
72 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
74 pos[2] = cpu_to_be16(par->info->var.xres);
75 pos[3] = cpu_to_be16(1);
76 ((u8 *)par->txbuf.buf)[9] = COLOR_RGB565;
78 for (i = start_line; i <= end_line; i++) {
79 pos[1] = cpu_to_be16(i);
80 for (j = 0; j < par->info->var.xres; j++)
81 buf16[j] = cpu_to_be16(*vmem16++);
82 ret = par->fbtftops.write(par,
83 par->txbuf.buf, 10 + par->info->fix.line_length);
92 static inline int rgb565_to_rgb332(u16 c)
94 return ((c & 0xE000) >> 8) | ((c & 000700) >> 6) | ((c & 0x0018) >> 3);
97 static int write_vmem_8bit(struct fbtft_par *par, size_t offset, size_t len)
99 unsigned int start_line, end_line;
100 u16 *vmem16 = (u16 *)(par->info->screen_buffer + offset);
101 __be16 *pos = par->txbuf.buf + 1;
102 u8 *buf8 = par->txbuf.buf + 10;
106 start_line = offset / par->info->fix.line_length;
107 end_line = start_line + (len / par->info->fix.line_length) - 1;
109 /* Set command header. pos: x, y, w, h */
110 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
112 pos[2] = cpu_to_be16(par->info->var.xres);
113 pos[3] = cpu_to_be16(1);
114 ((u8 *)par->txbuf.buf)[9] = COLOR_RGB332;
116 for (i = start_line; i <= end_line; i++) {
117 pos[1] = cpu_to_be16(i);
118 for (j = 0; j < par->info->var.xres; j++) {
119 buf8[j] = rgb565_to_rgb332(*vmem16);
122 ret = par->fbtftops.write(par,
123 par->txbuf.buf, 10 + par->info->var.xres);
132 static unsigned int firmware_version(struct fbtft_par *par)
136 write_reg(par, CMD_VERSION);
137 par->fbtftops.read(par, rxbuf, 4);
141 return (rxbuf[0] - '0') << 8 | (rxbuf[2] - '0') << 4 | (rxbuf[3] - '0');
144 static int init_display(struct fbtft_par *par)
147 unsigned int version;
150 /* enable SPI interface by having CS and MOSI low during reset */
151 save_mode = par->spi->mode;
153 * Set CS active inverse polarity: just setting SPI_CS_HIGH does not
154 * work with GPIO based chip selects that are logically active high
155 * but inverted inside the GPIO library, so enforce inverted
158 par->spi->mode ^= SPI_CS_HIGH;
159 ret = spi_setup(par->spi);
161 dev_err(par->info->device,
162 "Could not set inverse CS polarity\n");
165 write_reg(par, 0x00); /* make sure mode is set */
168 par->fbtftops.reset(par);
170 par->spi->mode = save_mode;
171 ret = spi_setup(par->spi);
173 dev_err(par->info->device, "Could not restore SPI mode\n");
176 write_reg(par, 0x00);
178 version = firmware_version(par);
179 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "Firmware version: %x.%02x\n",
180 version >> 8, version & 0xFF);
183 par->fbtftops.write_vmem = write_vmem_8bit;
187 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
189 /* not used on this controller */
192 static int set_var(struct fbtft_par *par)
196 /* this controller rotates clock wise */
197 switch (par->info->var.rotate) {
210 write_reg(par, CMD_LCD_ORIENTATION, rotate);
215 static int verify_gpios(struct fbtft_par *par)
217 if (!par->gpio.reset) {
218 dev_err(par->info->device, "Missing 'reset' gpio. Aborting.\n");
224 #ifdef CONFIG_FB_BACKLIGHT
225 static int backlight_chip_update_status(struct backlight_device *bd)
227 struct fbtft_par *par = bl_get_data(bd);
228 int brightness = bd->props.brightness;
230 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
231 "%s: brightness=%d, power=%d, fb_blank=%d\n", __func__,
232 bd->props.brightness, bd->props.power,
235 if (bd->props.power != FB_BLANK_UNBLANK)
238 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
241 write_reg(par, CMD_LCD_LED, brightness);
246 static const struct backlight_ops bl_ops = {
247 .update_status = backlight_chip_update_status,
250 static void register_chip_backlight(struct fbtft_par *par)
252 struct backlight_device *bd;
253 struct backlight_properties bl_props = { 0, };
255 bl_props.type = BACKLIGHT_RAW;
256 bl_props.power = FB_BLANK_POWERDOWN;
257 bl_props.max_brightness = 100;
258 bl_props.brightness = DEFAULT_BRIGHTNESS;
260 bd = backlight_device_register(dev_driver_string(par->info->device),
261 par->info->device, par, &bl_ops,
264 dev_err(par->info->device,
265 "cannot register backlight device (%ld)\n",
269 par->info->bl_dev = bd;
271 if (!par->fbtftops.unregister_backlight)
272 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
275 #define register_chip_backlight NULL
278 static struct fbtft_display display = {
284 .txbuflen = TXBUFLEN,
286 .write_register = write_reg8_bus8,
287 .write_vmem = write_vmem,
288 .init_display = init_display,
289 .set_addr_win = set_addr_win,
291 .verify_gpios = verify_gpios,
292 .register_backlight = register_chip_backlight,
296 FBTFT_REGISTER_DRIVER(DRVNAME, "watterott,openlcd", &display);
298 MODULE_ALIAS("spi:" DRVNAME);
300 MODULE_DESCRIPTION("FB driver for the Watterott LCD Controller");
301 MODULE_AUTHOR("Noralf Tronnes");
302 MODULE_LICENSE("GPL");