linux/drivers/staging/fbtft/flexfb.c
<<
>>
Prefs
   1/*
   2 * Generic FB driver for TFT LCD displays
   3 *
   4 * Copyright (C) 2013 Noralf Tronnes
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 *
  11 * This program is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 */
  16
  17#include <linux/module.h>
  18#include <linux/kernel.h>
  19#include <linux/init.h>
  20#include <linux/vmalloc.h>
  21#include <linux/gpio.h>
  22#include <linux/spi/spi.h>
  23#include <linux/delay.h>
  24
  25#include "fbtft.h"
  26
  27#define DRVNAME     "flexfb"
  28
  29static char *chip;
  30module_param(chip, charp, 0);
  31MODULE_PARM_DESC(chip, "LCD controller");
  32
  33static unsigned int width;
  34module_param(width, uint, 0);
  35MODULE_PARM_DESC(width, "Display width");
  36
  37static unsigned int height;
  38module_param(height, uint, 0);
  39MODULE_PARM_DESC(height, "Display height");
  40
  41static int init[512];
  42static int init_num;
  43module_param_array(init, int, &init_num, 0);
  44MODULE_PARM_DESC(init, "Init sequence");
  45
  46static unsigned int setaddrwin;
  47module_param(setaddrwin, uint, 0);
  48MODULE_PARM_DESC(setaddrwin, "Which set_addr_win() implementation to use");
  49
  50static unsigned int buswidth = 8;
  51module_param(buswidth, uint, 0);
  52MODULE_PARM_DESC(buswidth, "Width of databus (default: 8)");
  53
  54static unsigned int regwidth = 8;
  55module_param(regwidth, uint, 0);
  56MODULE_PARM_DESC(regwidth, "Width of controller register (default: 8)");
  57
  58static bool nobacklight;
  59module_param(nobacklight, bool, 0);
  60MODULE_PARM_DESC(nobacklight, "Turn off backlight functionality.");
  61
  62static bool latched;
  63module_param(latched, bool, 0);
  64MODULE_PARM_DESC(latched, "Use with latched 16-bit databus");
  65
  66static int *initp;
  67static int initp_num;
  68
  69/* default init sequences */
  70static int st7735r_init[] = {
  71-1, 0x01, -2, 150, -1, 0x11, -2, 500, -1, 0xB1, 0x01, 0x2C, 0x2D, -1, 0xB2, 0x01, 0x2C, 0x2D, -1, 0xB3, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D,
  72-1, 0xB4, 0x07, -1, 0xC0, 0xA2, 0x02, 0x84, -1, 0xC1, 0xC5, -1, 0xC2, 0x0A, 0x00, -1, 0xC3, 0x8A, 0x2A, -1, 0xC4, 0x8A, 0xEE, -1, 0xC5, 0x0E,
  73-1, 0x20, -1, 0x36, 0xC0, -1, 0x3A, 0x05, -1, 0xE0, 0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22, 0x1f, 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10,
  74-1, 0xE1, 0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30, 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10, -1, 0x29, -2, 100, -1, 0x13, -2, 10, -3 };
  75
  76static int ssd1289_init[] = {
  77-1, 0x00, 0x0001, -1, 0x03, 0xA8A4, -1, 0x0C, 0x0000, -1, 0x0D, 0x080C, -1, 0x0E, 0x2B00, -1, 0x1E, 0x00B7, -1, 0x01, 0x2B3F, -1, 0x02, 0x0600,
  78-1, 0x10, 0x0000, -1, 0x11, 0x6070, -1, 0x05, 0x0000, -1, 0x06, 0x0000, -1, 0x16, 0xEF1C, -1, 0x17, 0x0003, -1, 0x07, 0x0233, -1, 0x0B, 0x0000,
  79-1, 0x0F, 0x0000, -1, 0x41, 0x0000, -1, 0x42, 0x0000, -1, 0x48, 0x0000, -1, 0x49, 0x013F, -1, 0x4A, 0x0000, -1, 0x4B, 0x0000, -1, 0x44, 0xEF00,
  80-1, 0x45, 0x0000, -1, 0x46, 0x013F, -1, 0x30, 0x0707, -1, 0x31, 0x0204, -1, 0x32, 0x0204, -1, 0x33, 0x0502, -1, 0x34, 0x0507, -1, 0x35, 0x0204,
  81-1, 0x36, 0x0204, -1, 0x37, 0x0502, -1, 0x3A, 0x0302, -1, 0x3B, 0x0302, -1, 0x23, 0x0000, -1, 0x24, 0x0000, -1, 0x25, 0x8000, -1, 0x4f, 0x0000,
  82-1, 0x4e, 0x0000, -1, 0x22, -3 };
  83
  84static int hx8340bn_init[] = {
  85-1, 0xC1, 0xFF, 0x83, 0x40, -1, 0x11, -2, 150, -1, 0xCA, 0x70, 0x00, 0xD9, -1, 0xB0, 0x01, 0x11,
  86-1, 0xC9, 0x90, 0x49, 0x10, 0x28, 0x28, 0x10, 0x00, 0x06, -2, 20, -1, 0xC2, 0x60, 0x71, 0x01, 0x0E, 0x05, 0x02, 0x09, 0x31, 0x0A,
  87-1, 0xC3, 0x67, 0x30, 0x61, 0x17, 0x48, 0x07, 0x05, 0x33, -2, 10, -1, 0xB5, 0x35, 0x20, 0x45, -1, 0xB4, 0x33, 0x25, 0x4C, -2, 10,
  88-1, 0x3A, 0x05, -1, 0x29, -2, 10, -3 };
  89
  90static int ili9225_init[] = {
  91-1, 0x0001, 0x011C, -1, 0x0002, 0x0100, -1, 0x0003, 0x1030, -1, 0x0008, 0x0808, -1, 0x000C, 0x0000, -1, 0x000F, 0x0A01, -1, 0x0020, 0x0000,
  92-1, 0x0021, 0x0000, -2, 50, -1, 0x0010, 0x0A00, -1, 0x0011, 0x1038, -2, 50, -1, 0x0012, 0x1121, -1, 0x0013, 0x004E, -1, 0x0014, 0x676F,
  93-1, 0x0030, 0x0000, -1, 0x0031, 0x00DB, -1, 0x0032, 0x0000, -1, 0x0033, 0x0000, -1, 0x0034, 0x00DB, -1, 0x0035, 0x0000, -1, 0x0036, 0x00AF,
  94-1, 0x0037, 0x0000, -1, 0x0038, 0x00DB, -1, 0x0039, 0x0000, -1, 0x0050, 0x0000, -1, 0x0051, 0x060A, -1, 0x0052, 0x0D0A, -1, 0x0053, 0x0303,
  95-1, 0x0054, 0x0A0D, -1, 0x0055, 0x0A06, -1, 0x0056, 0x0000, -1, 0x0057, 0x0303, -1, 0x0058, 0x0000, -1, 0x0059, 0x0000, -2, 50,
  96-1, 0x0007, 0x1017, -2, 50, -3 };
  97
  98static int ili9320_init[] = {
  99-1, 0x00E5, 0x8000, -1, 0x0000, 0x0001, -1, 0x0001, 0x0100, -1, 0x0002, 0x0700, -1, 0x0003, 0x1030, -1, 0x0004, 0x0000, -1, 0x0008, 0x0202,
 100-1, 0x0009, 0x0000, -1, 0x000A, 0x0000, -1, 0x000C, 0x0000, -1, 0x000D, 0x0000, -1, 0x000F, 0x0000, -1, 0x0010, 0x0000, -1, 0x0011, 0x0007,
 101-1, 0x0012, 0x0000, -1, 0x0013, 0x0000, -2, 200, -1, 0x0010, 0x17B0, -1, 0x0011, 0x0031, -2, 50, -1, 0x0012, 0x0138, -2, 50, -1, 0x0013, 0x1800,
 102-1, 0x0029, 0x0008, -2, 50, -1, 0x0020, 0x0000, -1, 0x0021, 0x0000, -1, 0x0030, 0x0000, -1, 0x0031, 0x0505, -1, 0x0032, 0x0004,
 103-1, 0x0035, 0x0006, -1, 0x0036, 0x0707, -1, 0x0037, 0x0105, -1, 0x0038, 0x0002, -1, 0x0039, 0x0707, -1, 0x003C, 0x0704, -1, 0x003D, 0x0807,
 104-1, 0x0050, 0x0000, -1, 0x0051, 0x00EF, -1, 0x0052, 0x0000, -1, 0x0053, 0x013F, -1, 0x0060, 0x2700, -1, 0x0061, 0x0001, -1, 0x006A, 0x0000,
 105-1, 0x0080, 0x0000, -1, 0x0081, 0x0000, -1, 0x0082, 0x0000, -1, 0x0083, 0x0000, -1, 0x0084, 0x0000, -1, 0x0085, 0x0000, -1, 0x0090, 0x0010,
 106-1, 0x0092, 0x0000, -1, 0x0093, 0x0003, -1, 0x0095, 0x0110, -1, 0x0097, 0x0000, -1, 0x0098, 0x0000, -1, 0x0007, 0x0173, -3 };
 107
 108static int ili9325_init[] = {
 109-1, 0x00E3, 0x3008, -1, 0x00E7, 0x0012, -1, 0x00EF, 0x1231, -1, 0x0001, 0x0100, -1, 0x0002, 0x0700, -1, 0x0003, 0x1030, -1, 0x0004, 0x0000,
 110-1, 0x0008, 0x0207, -1, 0x0009, 0x0000, -1, 0x000A, 0x0000, -1, 0x000C, 0x0000, -1, 0x000D, 0x0000, -1, 0x000F, 0x0000, -1, 0x0010, 0x0000,
 111-1, 0x0011, 0x0007, -1, 0x0012, 0x0000, -1, 0x0013, 0x0000, -2, 200, -1, 0x0010, 0x1690, -1, 0x0011, 0x0223, -2, 50, -1, 0x0012, 0x000D, -2, 50,
 112-1, 0x0013, 0x1200, -1, 0x0029, 0x000A, -1, 0x002B, 0x000C, -2, 50, -1, 0x0020, 0x0000, -1, 0x0021, 0x0000, -1, 0x0030, 0x0000,
 113-1, 0x0031, 0x0506, -1, 0x0032, 0x0104, -1, 0x0035, 0x0207, -1, 0x0036, 0x000F, -1, 0x0037, 0x0306, -1, 0x0038, 0x0102, -1, 0x0039, 0x0707,
 114-1, 0x003C, 0x0702, -1, 0x003D, 0x1604, -1, 0x0050, 0x0000, -1, 0x0051, 0x00EF, -1, 0x0052, 0x0000, -1, 0x0053, 0x013F, -1, 0x0060, 0xA700,
 115-1, 0x0061, 0x0001, -1, 0x006A, 0x0000, -1, 0x0080, 0x0000, -1, 0x0081, 0x0000, -1, 0x0082, 0x0000, -1, 0x0083, 0x0000, -1, 0x0084, 0x0000,
 116-1, 0x0085, 0x0000, -1, 0x0090, 0x0010, -1, 0x0092, 0x0600, -1, 0x0007, 0x0133, -3 };
 117
 118static int ili9341_init[] = {
 119-1, 0x28, -2, 20, -1, 0xCF, 0x00, 0x83, 0x30, -1, 0xED, 0x64, 0x03, 0x12, 0x81, -1, 0xE8, 0x85, 0x01, 0x79,
 120-1, 0xCB, 0x39, 0x2c, 0x00, 0x34, 0x02, -1, 0xF7, 0x20, -1, 0xEA, 0x00, 0x00, -1, 0xC0, 0x26, -1, 0xC1, 0x11,
 121-1, 0xC5, 0x35, 0x3E, -1, 0xC7, 0xBE, -1, 0xB1, 0x00, 0x1B, -1, 0xB6, 0x0a, 0x82, 0x27, 0x00, -1, 0xB7, 0x07,
 122-1, 0x3A, 0x55, -1, 0x36, 0x48, -1, 0x11, -2, 120, -1, 0x29, -2, 20, -3 };
 123
 124static int ssd1351_init[] = { -1, 0xfd, 0x12, -1, 0xfd, 0xb1, -1, 0xae, -1, 0xb3, 0xf1, -1, 0xca, 0x7f, -1, 0xa0, 0x74,
 125                              -1, 0x15, 0x00, 0x7f, -1, 0x75, 0x00, 0x7f, -1, 0xa1, 0x00, -1, 0xa2, 0x00, -1, 0xb5, 0x00,
 126                              -1, 0xab, 0x01, -1, 0xb1, 0x32, -1, 0xb4, 0xa0, 0xb5, 0x55, -1, 0xbb, 0x17, -1, 0xbe, 0x05,
 127                              -1, 0xc1, 0xc8, 0x80, 0xc8, -1, 0xc7, 0x0f, -1, 0xb6, 0x01, -1, 0xa6, -1, 0xaf, -3 };
 128
 129/**
 130 * struct flexfb_lcd_controller - Describes the LCD controller properties
 131 * @name: Model name of the chip
 132 * @width: Width of display in pixels
 133 * @height: Height of display in pixels
 134 * @setaddrwin: Which set_addr_win() implementation to use
 135 * @regwidth: LCD Controller Register width in bits
 136 * @init_seq: LCD initialization sequence
 137 * @init_seq_sz: Size of LCD initialization sequence
 138 */
 139struct flexfb_lcd_controller {
 140        const char *name;
 141        unsigned int width;
 142        unsigned int height;
 143        unsigned int setaddrwin;
 144        unsigned int regwidth;
 145        int *init_seq;
 146        int init_seq_sz;
 147};
 148
 149static const struct flexfb_lcd_controller flexfb_chip_table[] = {
 150        {
 151                .name = "st7735r",
 152                .width = 120,
 153                .height = 160,
 154                .init_seq = st7735r_init,
 155                .init_seq_sz = ARRAY_SIZE(st7735r_init),
 156        },
 157        {
 158                .name = "hx8340bn",
 159                .width = 176,
 160                .height = 220,
 161                .init_seq = hx8340bn_init,
 162                .init_seq_sz = ARRAY_SIZE(hx8340bn_init),
 163        },
 164        {
 165                .name = "ili9225",
 166                .width = 176,
 167                .height = 220,
 168                .regwidth = 16,
 169                .init_seq = ili9225_init,
 170                .init_seq_sz = ARRAY_SIZE(ili9225_init),
 171        },
 172        {
 173                .name = "ili9225",
 174                .width = 176,
 175                .height = 220,
 176                .regwidth = 16,
 177                .init_seq = ili9225_init,
 178                .init_seq_sz = ARRAY_SIZE(ili9225_init),
 179        },
 180        {
 181                .name = "ili9225",
 182                .width = 176,
 183                .height = 220,
 184                .regwidth = 16,
 185                .init_seq = ili9225_init,
 186                .init_seq_sz = ARRAY_SIZE(ili9225_init),
 187        },
 188        {
 189                .name = "ili9320",
 190                .width = 240,
 191                .height = 320,
 192                .setaddrwin = 1,
 193                .regwidth = 16,
 194                .init_seq = ili9320_init,
 195                .init_seq_sz = ARRAY_SIZE(ili9320_init),
 196        },
 197        {
 198                .name = "ili9325",
 199                .width = 240,
 200                .height = 320,
 201                .setaddrwin = 1,
 202                .regwidth = 16,
 203                .init_seq = ili9325_init,
 204                .init_seq_sz = ARRAY_SIZE(ili9325_init),
 205        },
 206        {
 207                .name = "ili9341",
 208                .width = 240,
 209                .height = 320,
 210                .init_seq = ili9341_init,
 211                .init_seq_sz = ARRAY_SIZE(ili9341_init),
 212        },
 213        {
 214                .name = "ssd1289",
 215                .width = 240,
 216                .height = 320,
 217                .setaddrwin = 2,
 218                .regwidth = 16,
 219                .init_seq = ssd1289_init,
 220                .init_seq_sz = ARRAY_SIZE(ssd1289_init),
 221        },
 222        {
 223                .name = "ssd1351",
 224                .width = 128,
 225                .height = 128,
 226                .setaddrwin = 3,
 227                .init_seq = ssd1351_init,
 228                .init_seq_sz = ARRAY_SIZE(ssd1351_init),
 229        },
 230};
 231
 232/* ili9320, ili9325 */
 233static void flexfb_set_addr_win_1(struct fbtft_par *par,
 234                                  int xs, int ys, int xe, int ye)
 235{
 236        fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par, "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n",
 237                      __func__, xs, ys, xe, ye);
 238        switch (par->info->var.rotate) {
 239        /* R20h = Horizontal GRAM Start Address */
 240        /* R21h = Vertical GRAM Start Address */
 241        case 0:
 242                write_reg(par, 0x0020, xs);
 243                write_reg(par, 0x0021, ys);
 244                break;
 245        case 180:
 246                write_reg(par, 0x0020, width - 1 - xs);
 247                write_reg(par, 0x0021, height - 1 - ys);
 248                break;
 249        case 270:
 250                write_reg(par, 0x0020, width - 1 - ys);
 251                write_reg(par, 0x0021, xs);
 252                break;
 253        case 90:
 254                write_reg(par, 0x0020, ys);
 255                write_reg(par, 0x0021, height - 1 - xs);
 256                break;
 257        }
 258        write_reg(par, 0x0022); /* Write Data to GRAM */
 259}
 260
 261/* ssd1289 */
 262static void flexfb_set_addr_win_2(struct fbtft_par *par,
 263                                  int xs, int ys, int xe, int ye)
 264{
 265        fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
 266                      "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n",
 267                      __func__, xs, ys, xe, ye);
 268
 269        switch (par->info->var.rotate) {
 270        /* R4Eh - Set GDDRAM X address counter */
 271        /* R4Fh - Set GDDRAM Y address counter */
 272        case 0:
 273                write_reg(par, 0x4e, xs);
 274                write_reg(par, 0x4f, ys);
 275                break;
 276        case 180:
 277                write_reg(par, 0x4e, par->info->var.xres - 1 - xs);
 278                write_reg(par, 0x4f, par->info->var.yres - 1 - ys);
 279                break;
 280        case 270:
 281                write_reg(par, 0x4e, par->info->var.yres - 1 - ys);
 282                write_reg(par, 0x4f, xs);
 283                break;
 284        case 90:
 285                write_reg(par, 0x4e, ys);
 286                write_reg(par, 0x4f, par->info->var.xres - 1 - xs);
 287                break;
 288        }
 289
 290        /* R22h - RAM data write */
 291        write_reg(par, 0x22, 0);
 292}
 293
 294/* ssd1351 */
 295static void set_addr_win_3(struct fbtft_par *par,
 296                           int xs, int ys, int xe, int ye)
 297{
 298        fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
 299                      "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__,
 300                      xs, ys, xe, ye);
 301
 302        write_reg(par, 0x15, xs, xe);
 303        write_reg(par, 0x75, ys, ye);
 304        write_reg(par, 0x5C);
 305}
 306
 307static int flexfb_verify_gpios_dc(struct fbtft_par *par)
 308{
 309        fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
 310
 311        if (par->gpio.dc < 0) {
 312                dev_err(par->info->device,
 313                        "Missing info about 'dc' gpio. Aborting.\n");
 314                return -EINVAL;
 315        }
 316
 317        return 0;
 318}
 319
 320static int flexfb_verify_gpios_db(struct fbtft_par *par)
 321{
 322        int i;
 323        int num_db = buswidth;
 324
 325        fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
 326
 327        if (par->gpio.dc < 0) {
 328                dev_err(par->info->device, "Missing info about 'dc' gpio. Aborting.\n");
 329                return -EINVAL;
 330        }
 331        if (par->gpio.wr < 0) {
 332                dev_err(par->info->device, "Missing info about 'wr' gpio. Aborting.\n");
 333                return -EINVAL;
 334        }
 335        if (latched && (par->gpio.latch < 0)) {
 336                dev_err(par->info->device, "Missing info about 'latch' gpio. Aborting.\n");
 337                return -EINVAL;
 338        }
 339        if (latched)
 340                num_db = buswidth / 2;
 341        for (i = 0; i < num_db; i++) {
 342                if (par->gpio.db[i] < 0) {
 343                        dev_err(par->info->device,
 344                                "Missing info about 'db%02d' gpio. Aborting.\n",
 345                                i);
 346                        return -EINVAL;
 347                }
 348        }
 349
 350        return 0;
 351}
 352
 353static void flexfb_chip_load_param(const struct flexfb_lcd_controller *chip)
 354{
 355        if (!width)
 356                width = chip->width;
 357        if (!height)
 358                height = chip->height;
 359        setaddrwin = chip->setaddrwin;
 360        if (chip->regwidth)
 361                regwidth = chip->regwidth;
 362        if (!init_num) {
 363                initp = chip->init_seq;
 364                initp_num = chip->init_seq_sz;
 365        }
 366}
 367
 368static struct fbtft_display flex_display = { };
 369
 370static int flexfb_chip_init(const struct device *dev)
 371{
 372        int i;
 373
 374        for (i = 0; i < ARRAY_SIZE(flexfb_chip_table); i++)
 375                if (!strcmp(chip, flexfb_chip_table[i].name)) {
 376                        flexfb_chip_load_param(&flexfb_chip_table[i]);
 377                        return 0;
 378                }
 379
 380        dev_err(dev, "chip=%s is not supported\n", chip);
 381
 382        return -EINVAL;
 383}
 384
 385static int flexfb_probe_common(struct spi_device *sdev,
 386                               struct platform_device *pdev)
 387{
 388        struct device *dev;
 389        struct fb_info *info;
 390        struct fbtft_par *par;
 391        int ret;
 392
 393        initp = init;
 394        initp_num = init_num;
 395
 396        if (sdev)
 397                dev = &sdev->dev;
 398        else
 399                dev = &pdev->dev;
 400
 401        fbtft_init_dbg(dev, "%s(%s)\n", __func__,
 402                       sdev ? "'SPI device'" : "'Platform device'");
 403
 404        if (chip) {
 405                ret = flexfb_chip_init(dev);
 406                if (ret)
 407                        return ret;
 408        }
 409
 410        if (width == 0 || height == 0) {
 411                dev_err(dev, "argument(s) missing: width and height has to be set.\n");
 412                return -EINVAL;
 413        }
 414        flex_display.width = width;
 415        flex_display.height = height;
 416        fbtft_init_dbg(dev, "Display resolution: %dx%d\n", width, height);
 417        fbtft_init_dbg(dev, "chip = %s\n", chip ? chip : "not set");
 418        fbtft_init_dbg(dev, "setaddrwin = %d\n", setaddrwin);
 419        fbtft_init_dbg(dev, "regwidth = %d\n", regwidth);
 420        fbtft_init_dbg(dev, "buswidth = %d\n", buswidth);
 421
 422        info = fbtft_framebuffer_alloc(&flex_display, dev, dev->platform_data);
 423        if (!info)
 424                return -ENOMEM;
 425
 426        par = info->par;
 427        if (sdev)
 428                par->spi = sdev;
 429        else
 430                par->pdev = pdev;
 431        if (!par->init_sequence)
 432                par->init_sequence = initp;
 433        par->fbtftops.init_display = fbtft_init_display;
 434
 435        /* registerwrite functions */
 436        switch (regwidth) {
 437        case 8:
 438                par->fbtftops.write_register = fbtft_write_reg8_bus8;
 439                break;
 440        case 16:
 441                par->fbtftops.write_register = fbtft_write_reg16_bus8;
 442                break;
 443        default:
 444                dev_err(dev,
 445                        "argument 'regwidth': %d is not supported.\n",
 446                        regwidth);
 447                return -EINVAL;
 448        }
 449
 450        /* bus functions */
 451        if (sdev) {
 452                par->fbtftops.write = fbtft_write_spi;
 453                switch (buswidth) {
 454                case 8:
 455                        par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
 456                        if (!par->startbyte)
 457                                par->fbtftops.verify_gpios = flexfb_verify_gpios_dc;
 458                        break;
 459                case 9:
 460                        if (regwidth == 16) {
 461                                dev_err(dev, "argument 'regwidth': %d is not supported with buswidth=%d and SPI.\n", regwidth, buswidth);
 462                                return -EINVAL;
 463                        }
 464                        par->fbtftops.write_register = fbtft_write_reg8_bus9;
 465                        par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
 466                        if (par->spi->master->bits_per_word_mask
 467                            & SPI_BPW_MASK(9)) {
 468                                par->spi->bits_per_word = 9;
 469                        } else {
 470                                dev_warn(dev,
 471                                        "9-bit SPI not available, emulating using 8-bit.\n");
 472                                /* allocate buffer with room for dc bits */
 473                                par->extra = devm_kzalloc(par->info->device,
 474                                                par->txbuf.len + (par->txbuf.len / 8) + 8,
 475                                                GFP_KERNEL);
 476                                if (!par->extra) {
 477                                        ret = -ENOMEM;
 478                                        goto out_release;
 479                                }
 480                                par->fbtftops.write = fbtft_write_spi_emulate_9;
 481                        }
 482                        break;
 483                default:
 484                        dev_err(dev, "argument 'buswidth': %d is not supported with SPI.\n", buswidth);
 485                        return -EINVAL;
 486                }
 487        } else {
 488                par->fbtftops.verify_gpios = flexfb_verify_gpios_db;
 489                switch (buswidth) {
 490                case 8:
 491                        par->fbtftops.write = fbtft_write_gpio8_wr;
 492                        par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
 493                        break;
 494                case 16:
 495                        par->fbtftops.write_register = fbtft_write_reg16_bus16;
 496                        if (latched)
 497                                par->fbtftops.write = fbtft_write_gpio16_wr_latched;
 498                        else
 499                                par->fbtftops.write = fbtft_write_gpio16_wr;
 500                        par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
 501                        break;
 502                default:
 503                        dev_err(dev, "argument 'buswidth': %d is not supported with parallel.\n", buswidth);
 504                        return -EINVAL;
 505                }
 506        }
 507
 508        /* set_addr_win function */
 509        switch (setaddrwin) {
 510        case 0:
 511                /* use default */
 512                break;
 513        case 1:
 514                par->fbtftops.set_addr_win = flexfb_set_addr_win_1;
 515                break;
 516        case 2:
 517                par->fbtftops.set_addr_win = flexfb_set_addr_win_2;
 518                break;
 519        case 3:
 520                par->fbtftops.set_addr_win = set_addr_win_3;
 521                break;
 522        default:
 523                dev_err(dev, "argument 'setaddrwin': unknown value %d.\n",
 524                        setaddrwin);
 525                return -EINVAL;
 526        }
 527
 528        if (!nobacklight)
 529                par->fbtftops.register_backlight = fbtft_register_backlight;
 530
 531        ret = fbtft_register_framebuffer(info);
 532        if (ret < 0)
 533                goto out_release;
 534
 535        return 0;
 536
 537out_release:
 538        fbtft_framebuffer_release(info);
 539
 540        return ret;
 541}
 542
 543static int flexfb_remove_common(struct device *dev, struct fb_info *info)
 544{
 545        struct fbtft_par *par;
 546
 547        if (!info)
 548                return -EINVAL;
 549        par = info->par;
 550        if (par)
 551                fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par, "%s()\n",
 552                              __func__);
 553        fbtft_unregister_framebuffer(info);
 554        fbtft_framebuffer_release(info);
 555
 556        return 0;
 557}
 558
 559static int flexfb_probe_spi(struct spi_device *spi)
 560{
 561        return flexfb_probe_common(spi, NULL);
 562}
 563
 564static int flexfb_remove_spi(struct spi_device *spi)
 565{
 566        struct fb_info *info = spi_get_drvdata(spi);
 567
 568        return flexfb_remove_common(&spi->dev, info);
 569}
 570
 571static int flexfb_probe_pdev(struct platform_device *pdev)
 572{
 573        return flexfb_probe_common(NULL, pdev);
 574}
 575
 576static int flexfb_remove_pdev(struct platform_device *pdev)
 577{
 578        struct fb_info *info = platform_get_drvdata(pdev);
 579
 580        return flexfb_remove_common(&pdev->dev, info);
 581}
 582
 583static struct spi_driver flexfb_spi_driver = {
 584        .driver = {
 585                .name   = DRVNAME,
 586                .owner  = THIS_MODULE,
 587        },
 588        .probe  = flexfb_probe_spi,
 589        .remove = flexfb_remove_spi,
 590};
 591
 592static const struct platform_device_id flexfb_platform_ids[] = {
 593        { "flexpfb", 0 },
 594        { },
 595};
 596
 597static struct platform_driver flexfb_platform_driver = {
 598        .driver = {
 599                .name   = DRVNAME,
 600        },
 601        .id_table = flexfb_platform_ids,
 602        .probe  = flexfb_probe_pdev,
 603        .remove = flexfb_remove_pdev,
 604};
 605
 606static int __init flexfb_init(void)
 607{
 608        int ret, ret2;
 609
 610        ret = spi_register_driver(&flexfb_spi_driver);
 611        ret2 = platform_driver_register(&flexfb_platform_driver);
 612        if (ret < 0)
 613                return ret;
 614        return ret2;
 615}
 616
 617static void __exit flexfb_exit(void)
 618{
 619        spi_unregister_driver(&flexfb_spi_driver);
 620        platform_driver_unregister(&flexfb_platform_driver);
 621}
 622
 623/* ------------------------------------------------------------------------- */
 624
 625module_init(flexfb_init);
 626module_exit(flexfb_exit);
 627
 628MODULE_DESCRIPTION("Generic FB driver for TFT LCD displays");
 629MODULE_AUTHOR("Noralf Tronnes");
 630MODULE_LICENSE("GPL");
 631