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        /*
 154         * Set CS active inverse polarity: just setting SPI_CS_HIGH does not
 155         * work with GPIO based chip selects that are logically active high
 156         * but inverted inside the GPIO library, so enforce inverted
 157         * semantics.
 158         */
 159        par->spi->mode ^= SPI_CS_HIGH;
 160        ret = spi_setup(par->spi);
 161        if (ret) {
 162                dev_err(par->info->device,
 163                        "Could not set inverse CS polarity\n");
 164                return ret;
 165        }
 166        write_reg(par, 0x00); /* make sure mode is set */
 167
 168        mdelay(50);
 169        par->fbtftops.reset(par);
 170        mdelay(1000);
 171        par->spi->mode = save_mode;
 172        ret = spi_setup(par->spi);
 173        if (ret) {
 174                dev_err(par->info->device, "Could not restore SPI mode\n");
 175                return ret;
 176        }
 177        write_reg(par, 0x00);
 178
 179        version = firmware_version(par);
 180        fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "Firmware version: %x.%02x\n",
 181                      version >> 8, version & 0xFF);
 182
 183        if (mode == 332)
 184                par->fbtftops.write_vmem = write_vmem_8bit;
 185        return 0;
 186}
 187
 188static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
 189{
 190        /* not used on this controller */
 191}
 192
 193static int set_var(struct fbtft_par *par)
 194{
 195        u8 rotate;
 196
 197        /* this controller rotates clock wise */
 198        switch (par->info->var.rotate) {
 199        case 90:
 200                rotate = 27;
 201                break;
 202        case 180:
 203                rotate = 18;
 204                break;
 205        case 270:
 206                rotate = 9;
 207                break;
 208        default:
 209                rotate = 0;
 210        }
 211        write_reg(par, CMD_LCD_ORIENTATION, rotate);
 212
 213        return 0;
 214}
 215
 216static int verify_gpios(struct fbtft_par *par)
 217{
 218        if (!par->gpio.reset) {
 219                dev_err(par->info->device, "Missing 'reset' gpio. Aborting.\n");
 220                return -EINVAL;
 221        }
 222        return 0;
 223}
 224
 225#ifdef CONFIG_FB_BACKLIGHT
 226static int backlight_chip_update_status(struct backlight_device *bd)
 227{
 228        struct fbtft_par *par = bl_get_data(bd);
 229        int brightness = bd->props.brightness;
 230
 231        fbtft_par_dbg(DEBUG_BACKLIGHT, par,
 232                      "%s: brightness=%d, power=%d, fb_blank=%d\n", __func__,
 233                      bd->props.brightness, bd->props.power,
 234                      bd->props.fb_blank);
 235
 236        if (bd->props.power != FB_BLANK_UNBLANK)
 237                brightness = 0;
 238
 239        if (bd->props.fb_blank != FB_BLANK_UNBLANK)
 240                brightness = 0;
 241
 242        write_reg(par, CMD_LCD_LED, brightness);
 243
 244        return 0;
 245}
 246
 247static const struct backlight_ops bl_ops = {
 248        .update_status = backlight_chip_update_status,
 249};
 250
 251static void register_chip_backlight(struct fbtft_par *par)
 252{
 253        struct backlight_device *bd;
 254        struct backlight_properties bl_props = { 0, };
 255
 256        bl_props.type = BACKLIGHT_RAW;
 257        bl_props.power = FB_BLANK_POWERDOWN;
 258        bl_props.max_brightness = 100;
 259        bl_props.brightness = DEFAULT_BRIGHTNESS;
 260
 261        bd = backlight_device_register(dev_driver_string(par->info->device),
 262                                       par->info->device, par, &bl_ops,
 263                                       &bl_props);
 264        if (IS_ERR(bd)) {
 265                dev_err(par->info->device,
 266                        "cannot register backlight device (%ld)\n",
 267                        PTR_ERR(bd));
 268                return;
 269        }
 270        par->info->bl_dev = bd;
 271
 272        if (!par->fbtftops.unregister_backlight)
 273                par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
 274}
 275#else
 276#define register_chip_backlight NULL
 277#endif
 278
 279static struct fbtft_display display = {
 280        .regwidth = 8,
 281        .buswidth = 8,
 282        .width = WIDTH,
 283        .height = HEIGHT,
 284        .fps = FPS,
 285        .txbuflen = TXBUFLEN,
 286        .fbtftops = {
 287                .write_register = write_reg8_bus8,
 288                .write_vmem = write_vmem,
 289                .init_display = init_display,
 290                .set_addr_win = set_addr_win,
 291                .set_var = set_var,
 292                .verify_gpios = verify_gpios,
 293                .register_backlight = register_chip_backlight,
 294        },
 295};
 296
 297FBTFT_REGISTER_DRIVER(DRVNAME, "watterott,openlcd", &display);
 298
 299MODULE_ALIAS("spi:" DRVNAME);
 300
 301MODULE_DESCRIPTION("FB driver for the Watterott LCD Controller");
 302MODULE_AUTHOR("Noralf Tronnes");
 303MODULE_LICENSE("GPL");
 304