1
2
3
4
5
6
7
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/gpio.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, len, "%s: ", __func__);
50
51 ret = par->fbtftops.write(par, par->buf, len);
52 if (ret < 0) {
53 dev_err(par->info->device,
54 "write() failed and returned %d\n", ret);
55 return;
56 }
57}
58
59static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
60{
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;
65 int i, j;
66 int ret = 0;
67
68 start_line = offset / par->info->fix.line_length;
69 end_line = start_line + (len / par->info->fix.line_length) - 1;
70
71
72 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
73 pos[0] = 0;
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;
77
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);
84 if (ret < 0)
85 return ret;
86 udelay(300);
87 }
88
89 return 0;
90}
91
92#define RGB565toRGB323(c) (((c&0xE000)>>8) | ((c&0600)>>6) | ((c&0x001C)>>2))
93#define RGB565toRGB332(c) (((c&0xE000)>>8) | ((c&0700)>>6) | ((c&0x0018)>>3))
94#define RGB565toRGB233(c) (((c&0xC000)>>8) | ((c&0700)>>5) | ((c&0x001C)>>2))
95
96static int write_vmem_8bit(struct fbtft_par *par, size_t offset, size_t len)
97{
98 unsigned int start_line, end_line;
99 u16 *vmem16 = (u16 *)(par->info->screen_buffer + offset);
100 __be16 *pos = par->txbuf.buf + 1;
101 u8 *buf8 = par->txbuf.buf + 10;
102 int i, j;
103 int ret = 0;
104
105 start_line = offset / par->info->fix.line_length;
106 end_line = start_line + (len / par->info->fix.line_length) - 1;
107
108
109 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
110 pos[0] = 0;
111 pos[2] = cpu_to_be16(par->info->var.xres);
112 pos[3] = cpu_to_be16(1);
113 ((u8 *)par->txbuf.buf)[9] = COLOR_RGB332;
114
115 for (i = start_line; i <= end_line; i++) {
116 pos[1] = cpu_to_be16(i);
117 for (j = 0; j < par->info->var.xres; j++) {
118 buf8[j] = RGB565toRGB332(*vmem16);
119 vmem16++;
120 }
121 ret = par->fbtftops.write(par,
122 par->txbuf.buf, 10 + par->info->var.xres);
123 if (ret < 0)
124 return ret;
125 udelay(700);
126 }
127
128 return 0;
129}
130
131static unsigned int firmware_version(struct fbtft_par *par)
132{
133 u8 rxbuf[4] = {0, };
134
135 write_reg(par, CMD_VERSION);
136 par->fbtftops.read(par, rxbuf, 4);
137 if (rxbuf[1] != '.')
138 return 0;
139
140 return (rxbuf[0] - '0') << 8 | (rxbuf[2] - '0') << 4 | (rxbuf[3] - '0');
141}
142
143static int init_display(struct fbtft_par *par)
144{
145 int ret;
146 unsigned int version;
147 u8 save_mode;
148
149
150 save_mode = par->spi->mode;
151 par->spi->mode |= SPI_CS_HIGH;
152 ret = spi_setup(par->spi);
153 if (ret) {
154 dev_err(par->info->device, "Could not set SPI_CS_HIGH\n");
155 return ret;
156 }
157 write_reg(par, 0x00);
158
159 mdelay(50);
160 par->fbtftops.reset(par);
161 mdelay(1000);
162 par->spi->mode = save_mode;
163 ret = spi_setup(par->spi);
164 if (ret) {
165 dev_err(par->info->device, "Could not restore SPI mode\n");
166 return ret;
167 }
168 write_reg(par, 0x00);
169
170 version = firmware_version(par);
171 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "Firmware version: %x.%02x\n",
172 version >> 8, version & 0xFF);
173
174 if (mode == 332)
175 par->fbtftops.write_vmem = write_vmem_8bit;
176 return 0;
177}
178
179static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
180{
181
182}
183
184static int set_var(struct fbtft_par *par)
185{
186 u8 rotate;
187
188
189 switch (par->info->var.rotate) {
190 case 90:
191 rotate = 27;
192 break;
193 case 180:
194 rotate = 18;
195 break;
196 case 270:
197 rotate = 9;
198 break;
199 default:
200 rotate = 0;
201 }
202 write_reg(par, CMD_LCD_ORIENTATION, rotate);
203
204 return 0;
205}
206
207static int verify_gpios(struct fbtft_par *par)
208{
209 if (par->gpio.reset < 0) {
210 dev_err(par->info->device, "Missing 'reset' gpio. Aborting.\n");
211 return -EINVAL;
212 }
213 return 0;
214}
215
216#ifdef CONFIG_FB_BACKLIGHT
217static int backlight_chip_update_status(struct backlight_device *bd)
218{
219 struct fbtft_par *par = bl_get_data(bd);
220 int brightness = bd->props.brightness;
221
222 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
223 "%s: brightness=%d, power=%d, fb_blank=%d\n",
224 __func__, bd->props.brightness, bd->props.power,
225 bd->props.fb_blank);
226
227 if (bd->props.power != FB_BLANK_UNBLANK)
228 brightness = 0;
229
230 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
231 brightness = 0;
232
233 write_reg(par, CMD_LCD_LED, brightness);
234
235 return 0;
236}
237
238static const struct backlight_ops bl_ops = {
239 .update_status = backlight_chip_update_status,
240};
241
242static void register_chip_backlight(struct fbtft_par *par)
243{
244 struct backlight_device *bd;
245 struct backlight_properties bl_props = { 0, };
246
247 bl_props.type = BACKLIGHT_RAW;
248 bl_props.power = FB_BLANK_POWERDOWN;
249 bl_props.max_brightness = 100;
250 bl_props.brightness = DEFAULT_BRIGHTNESS;
251
252 bd = backlight_device_register(dev_driver_string(par->info->device),
253 par->info->device, par, &bl_ops, &bl_props);
254 if (IS_ERR(bd)) {
255 dev_err(par->info->device,
256 "cannot register backlight device (%ld)\n",
257 PTR_ERR(bd));
258 return;
259 }
260 par->info->bl_dev = bd;
261
262 if (!par->fbtftops.unregister_backlight)
263 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
264}
265#else
266#define register_chip_backlight NULL
267#endif
268
269static struct fbtft_display display = {
270 .regwidth = 8,
271 .buswidth = 8,
272 .width = WIDTH,
273 .height = HEIGHT,
274 .fps = FPS,
275 .txbuflen = TXBUFLEN,
276 .fbtftops = {
277 .write_register = write_reg8_bus8,
278 .write_vmem = write_vmem,
279 .init_display = init_display,
280 .set_addr_win = set_addr_win,
281 .set_var = set_var,
282 .verify_gpios = verify_gpios,
283 .register_backlight = register_chip_backlight,
284 },
285};
286
287FBTFT_REGISTER_DRIVER(DRVNAME, "watterott,openlcd", &display);
288
289MODULE_ALIAS("spi:" DRVNAME);
290
291MODULE_DESCRIPTION("FB driver for the Watterott LCD Controller");
292MODULE_AUTHOR("Noralf Tronnes");
293MODULE_LICENSE("GPL");
294