regulator: Introduce property to flag set-load support
[cascardo/linux.git] / drivers / staging / fbtft / fb_tls8204.c
1 /*
2  * FB driver for the TLS8204 LCD Controller
3  *
4  * The display is monochrome and the video memory is RGB565.
5  * Any pixel value except 0 turns the pixel on.
6  *
7  * Copyright (C) 2013 Noralf Tronnes
8  * Copyright (C) 2014 Michael Hope (adapted for the TLS8204)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/gpio.h>
29 #include <linux/spi/spi.h>
30 #include <linux/delay.h>
31
32 #include "fbtft.h"
33
34 #define DRVNAME         "fb_tls8204"
35 #define WIDTH           84
36 #define HEIGHT          48
37 #define TXBUFLEN        WIDTH
38
39 /* gamma is used to control contrast in this driver */
40 #define DEFAULT_GAMMA   "40"
41
42 static unsigned bs = 4;
43 module_param(bs, uint, 0);
44 MODULE_PARM_DESC(bs, "BS[2:0] Bias voltage level: 0-7 (default: 4)");
45
46 static int init_display(struct fbtft_par *par)
47 {
48         fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
49
50         par->fbtftops.reset(par);
51
52         /* Enter extended command mode */
53         write_reg(par, 0x21); /* 5:1  1
54                                  2:0  PD - Powerdown control: chip is active
55                                  1:0  V  - Entry mode: horizontal addressing
56                                  0:1  H  - Extended instruction set control:
57                                                 extended
58                               */
59
60         /* H=1 Bias system */
61         write_reg(par, 0x10 | (bs & 0x7)); /*
62                                  4:1  1
63                                  3:0  0
64                                  2:x  BS2 - Bias System
65                                  1:x  BS1
66                                  0:x  BS0
67                               */
68
69         /* Set the address of the first display line. */
70         write_reg(par, 0x04 | (64 >> 6));
71         write_reg(par, 0x40 | (64 & 0x3F));
72
73         /* Enter H=0 standard command mode */
74         write_reg(par, 0x20);
75
76         /* H=0 Display control */
77         write_reg(par, 0x08 | 4); /*
78                                  3:1  1
79                                  2:1  D  - DE: 10=normal mode
80                                  1:0  0
81                                  0:0  E
82                               */
83
84         return 0;
85 }
86
87 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
88 {
89         fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
90                       "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n",
91                       __func__, xs, ys, xe, ye);
92
93         /* H=0 Set X address of RAM */
94         write_reg(par, 0x80); /* 7:1  1
95                                  6-0: X[6:0] - 0x00
96                               */
97
98         /* H=0 Set Y address of RAM */
99         write_reg(par, 0x40); /* 7:0  0
100                                  6:1  1
101                                  2-0: Y[2:0] - 0x0
102                               */
103 }
104
105 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
106 {
107         u16 *vmem16 = (u16 *)par->info->screen_base;
108         int x, y, i;
109         int ret = 0;
110
111         fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s()\n", __func__);
112
113         for (y = 0; y < HEIGHT/8; y++) {
114                 u8 *buf = par->txbuf.buf;
115                 /* The display is 102x68 but the LCD is 84x48.  Set
116                    the write pointer at the start of each row. */
117                 gpio_set_value(par->gpio.dc, 0);
118                 write_reg(par, 0x80 | 0);
119                 write_reg(par, 0x40 | y);
120
121                 for (x = 0; x < WIDTH; x++) {
122                         u8 ch = 0;
123
124                         for (i = 0; i < 8*WIDTH; i += WIDTH) {
125                                 ch >>= 1;
126                                 if (vmem16[(y*8*WIDTH)+i+x])
127                                         ch |= 0x80;
128                         }
129                         *buf++ = ch;
130                 }
131                 /* Write the row */
132                 gpio_set_value(par->gpio.dc, 1);
133                 ret = par->fbtftops.write(par, par->txbuf.buf, WIDTH);
134                 if (ret < 0) {
135                         dev_err(par->info->device,
136                                 "write failed and returned: %d\n", ret);
137                         break;
138                 }
139         }
140
141         return ret;
142 }
143
144 static int set_gamma(struct fbtft_par *par, unsigned long *curves)
145 {
146         fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
147
148         /* apply mask */
149         curves[0] &= 0x7F;
150
151         write_reg(par, 0x21); /* turn on extended instruction set */
152         write_reg(par, 0x80 | curves[0]);
153         write_reg(par, 0x20); /* turn off extended instruction set */
154
155         return 0;
156 }
157
158
159 static struct fbtft_display display = {
160         .regwidth = 8,
161         .width = WIDTH,
162         .height = HEIGHT,
163         .txbuflen = TXBUFLEN,
164         .gamma_num = 1,
165         .gamma_len = 1,
166         .gamma = DEFAULT_GAMMA,
167         .fbtftops = {
168                 .init_display = init_display,
169                 .set_addr_win = set_addr_win,
170                 .write_vmem = write_vmem,
171                 .set_gamma = set_gamma,
172         },
173         .backlight = 1,
174 };
175 FBTFT_REGISTER_DRIVER(DRVNAME, "teralane,tls8204", &display);
176
177 MODULE_ALIAS("spi:" DRVNAME);
178 MODULE_ALIAS("spi:tls8204");
179
180 MODULE_DESCRIPTION("FB driver for the TLS8204 LCD Controller");
181 MODULE_AUTHOR("Michael Hope");
182 MODULE_LICENSE("GPL");