linux/drivers/staging/fbtft/fb_watterott.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * FB driver for the Watterott LCD Controller
   4 *
   5 * Copyright (C) 2013 Noralf Tronnes
   6 */
   7
   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>
  13
  14#include "fbtft.h"
  15
  16#define DRVNAME                 "fb_watterott"
  17#define WIDTH                   320
  18#define HEIGHT                  240
  19#define FPS                     5
  20#define TXBUFLEN                1024
  21#define DEFAULT_BRIGHTNESS      50
  22
  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
  32
  33static short mode = 565;
  34module_param(mode, short, 0000);
  35MODULE_PARM_DESC(mode, "RGB color transfer mode: 332, 565 (default)");
  36
  37static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
  38{
  39        va_list args;
  40        int i, ret;
  41        u8 *buf = par->buf;
  42
  43        va_start(args, len);
  44        for (i = 0; i < len; i++)
  45                *buf++ = (u8)va_arg(args, unsigned int);
  46        va_end(args);
  47
  48        fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
  49                          par->info->device, u8, par->buf,
  50                          len, "%s: ", __func__);
  51
  52        ret = par->fbtftops.write(par, par->buf, len);
  53        if (ret < 0) {
  54                dev_err(par->info->device,
  55                        "write() failed and returned %d\n", ret);
  56                return;
  57        }
  58}
  59
  60static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
  61{
  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;
  66        int i, j;
  67        int ret = 0;
  68
  69        start_line = offset / par->info->fix.line_length;
  70        end_line = start_line + (len / par->info->fix.line_length) - 1;
  71
  72        /* Set command header. pos: x, y, w, h */
  73        ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
  74        pos[0] = 0;
  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;
  78
  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);
  85                if (ret < 0)
  86                        return ret;
  87                udelay(300);
  88        }
  89
  90        return 0;
  91}
  92
  93static inline int rgb565_to_rgb332(u16 c)
  94{
  95        return ((c & 0xE000) >> 8) | ((c & 000700) >> 6) | ((c & 0x0018) >> 3);
  96}
  97
  98static int write_vmem_8bit(struct fbtft_par *par, size_t offset, size_t len)
  99{
 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;
 104        int i, j;
 105        int ret = 0;
 106
 107        start_line = offset / par->info->fix.line_length;
 108        end_line = start_line + (len / par->info->fix.line_length) - 1;
 109
 110        /* Set command header. pos: x, y, w, h */
 111        ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
 112        pos[0] = 0;
 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;
 116
 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);
 121                        vmem16++;
 122                }
 123                ret = par->fbtftops.write(par,
 124                        par->txbuf.buf, 10 + par->info->var.xres);
 125                if (ret < 0)
 126                        return ret;
 127                udelay(700);
 128        }
 129
 130        return 0;
 131}
 132
 133static unsigned int firmware_version(struct fbtft_par *par)
 134{
 135        u8 rxbuf[4] = {0, };
 136
 137        write_reg(par, CMD_VERSION);
 138        par->fbtftops.read(par, rxbuf, 4);
 139        if (rxbuf[1] != '.')
 140                return 0;
 141
 142        return (rxbuf[0] - '0') << 8 | (rxbuf[2] - '0') << 4 | (rxbuf[3] - '0');
 143}
 144
 145static int init_display(struct fbtft_par *par)
 146{
 147        int ret;
 148        unsigned int version;
 149        u8 save_mode;
 150
 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 */
 155        if (ret) {
 156                dev_err(par->info->device, "Could not set SPI_CS_HIGH\n");
 157                return ret;
 158        }
 159        write_reg(par, 0x00); /* make sure mode is set */
 160
 161        mdelay(50);
 162        par->fbtftops.reset(par);
 163        mdelay(1000);
 164        par->spi->mode = save_mode;
 165        ret = spi_setup(par->spi);
 166        if (ret) {
 167                dev_err(par->info->device, "Could not restore SPI mode\n");
 168                return ret;
 169        }
 170        write_reg(par, 0x00);
 171
 172        version = firmware_version(par);
 173        fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "Firmware version: %x.%02x\n",
 174                      version >> 8, version & 0xFF);
 175
 176        if (mode == 332)
 177                par->fbtftops.write_vmem = write_vmem_8bit;
 178        return 0;
 179}
 180
 181static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
 182{
 183        /* not used on this controller */
 184}
 185
 186static int set_var(struct fbtft_par *par)
 187{
 188        u8 rotate;
 189
 190        /* this controller rotates clock wise */
 191        switch (par->info->var.rotate) {
 192        case 90:
 193                rotate = 27;
 194                break;
 195        case 180:
 196                rotate = 18;
 197                break;
 198        case 270:
 199                rotate = 9;
 200                break;
 201        default:
 202                rotate = 0;
 203        }
 204        write_reg(par, CMD_LCD_ORIENTATION, rotate);
 205
 206        return 0;
 207}
 208
 209static int verify_gpios(struct fbtft_par *par)
 210{
 211        if (!par->gpio.reset) {
 212                dev_err(par->info->device, "Missing 'reset' gpio. Aborting.\n");
 213                return -EINVAL;
 214        }
 215        return 0;
 216}
 217
 218#ifdef CONFIG_FB_BACKLIGHT
 219static int backlight_chip_update_status(struct backlight_device *bd)
 220{
 221        struct fbtft_par *par = bl_get_data(bd);
 222        int brightness = bd->props.brightness;
 223
 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,
 227                      bd->props.fb_blank);
 228
 229        if (bd->props.power != FB_BLANK_UNBLANK)
 230                brightness = 0;
 231
 232        if (bd->props.fb_blank != FB_BLANK_UNBLANK)
 233                brightness = 0;
 234
 235        write_reg(par, CMD_LCD_LED, brightness);
 236
 237        return 0;
 238}
 239
 240static const struct backlight_ops bl_ops = {
 241        .update_status = backlight_chip_update_status,
 242};
 243
 244static void register_chip_backlight(struct fbtft_par *par)
 245{
 246        struct backlight_device *bd;
 247        struct backlight_properties bl_props = { 0, };
 248
 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;
 253
 254        bd = backlight_device_register(dev_driver_string(par->info->device),
 255                                       par->info->device, par, &bl_ops,
 256                                       &bl_props);
 257        if (IS_ERR(bd)) {
 258                dev_err(par->info->device,
 259                        "cannot register backlight device (%ld)\n",
 260                        PTR_ERR(bd));
 261                return;
 262        }
 263        par->info->bl_dev = bd;
 264
 265        if (!par->fbtftops.unregister_backlight)
 266                par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
 267}
 268#else
 269#define register_chip_backlight NULL
 270#endif
 271
 272static struct fbtft_display display = {
 273        .regwidth = 8,
 274        .buswidth = 8,
 275        .width = WIDTH,
 276        .height = HEIGHT,
 277        .fps = FPS,
 278        .txbuflen = TXBUFLEN,
 279        .fbtftops = {
 280                .write_register = write_reg8_bus8,
 281                .write_vmem = write_vmem,
 282                .init_display = init_display,
 283                .set_addr_win = set_addr_win,
 284                .set_var = set_var,
 285                .verify_gpios = verify_gpios,
 286                .register_backlight = register_chip_backlight,
 287        },
 288};
 289
 290FBTFT_REGISTER_DRIVER(DRVNAME, "watterott,openlcd", &display);
 291
 292MODULE_ALIAS("spi:" DRVNAME);
 293
 294MODULE_DESCRIPTION("FB driver for the Watterott LCD Controller");
 295MODULE_AUTHOR("Noralf Tronnes");
 296MODULE_LICENSE("GPL");
 297