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/delay.h>
12
13#include "fbtft.h"
14
15#define DRVNAME "fb_watterott"
16#define WIDTH 320
17#define HEIGHT 240
18#define FPS 5
19#define TXBUFLEN 1024
20#define DEFAULT_BRIGHTNESS 50
21
22#define CMD_VERSION 0x01
23#define CMD_LCD_LED 0x10
24#define CMD_LCD_RESET 0x11
25#define CMD_LCD_ORIENTATION 0x20
26#define CMD_LCD_DRAWIMAGE 0x27
27#define COLOR_RGB323 8
28#define COLOR_RGB332 9
29#define COLOR_RGB233 10
30#define COLOR_RGB565 16
31
32static short mode = 565;
33module_param(mode, short, 0000);
34MODULE_PARM_DESC(mode, "RGB color transfer mode: 332, 565 (default)");
35
36static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
37{
38 va_list args;
39 int i, ret;
40 u8 *buf = par->buf;
41
42 va_start(args, len);
43 for (i = 0; i < len; i++)
44 *buf++ = (u8)va_arg(args, unsigned int);
45 va_end(args);
46
47 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
48 par->info->device, u8, par->buf,
49 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
92static inline int rgb565_to_rgb332(u16 c)
93{
94 return ((c & 0xE000) >> 8) | ((c & 000700) >> 6) | ((c & 0x0018) >> 3);
95}
96
97static int write_vmem_8bit(struct fbtft_par *par, size_t offset, size_t len)
98{
99 unsigned int start_line, end_line;
100 u16 *vmem16 = (u16 *)(par->info->screen_buffer + offset);
101 __be16 *pos = par->txbuf.buf + 1;
102 u8 *buf8 = par->txbuf.buf + 10;
103 int i, j;
104 int ret = 0;
105
106 start_line = offset / par->info->fix.line_length;
107 end_line = start_line + (len / par->info->fix.line_length) - 1;
108
109
110 ((u8 *)par->txbuf.buf)[0] = CMD_LCD_DRAWIMAGE;
111 pos[0] = 0;
112 pos[2] = cpu_to_be16(par->info->var.xres);
113 pos[3] = cpu_to_be16(1);
114 ((u8 *)par->txbuf.buf)[9] = COLOR_RGB332;
115
116 for (i = start_line; i <= end_line; i++) {
117 pos[1] = cpu_to_be16(i);
118 for (j = 0; j < par->info->var.xres; j++) {
119 buf8[j] = rgb565_to_rgb332(*vmem16);
120 vmem16++;
121 }
122 ret = par->fbtftops.write(par,
123 par->txbuf.buf, 10 + par->info->var.xres);
124 if (ret < 0)
125 return ret;
126 udelay(700);
127 }
128
129 return 0;
130}
131
132static unsigned int firmware_version(struct fbtft_par *par)
133{
134 u8 rxbuf[4] = {0, };
135
136 write_reg(par, CMD_VERSION);
137 par->fbtftops.read(par, rxbuf, 4);
138 if (rxbuf[1] != '.')
139 return 0;
140
141 return (rxbuf[0] - '0') << 8 | (rxbuf[2] - '0') << 4 | (rxbuf[3] - '0');
142}
143
144static int init_display(struct fbtft_par *par)
145{
146 int ret;
147 unsigned int version;
148 u8 save_mode;
149
150
151 save_mode = par->spi->mode;
152
153
154
155
156
157
158 par->spi->mode ^= SPI_CS_HIGH;
159 ret = spi_setup(par->spi);
160 if (ret) {
161 dev_err(par->info->device,
162 "Could not set inverse CS polarity\n");
163 return ret;
164 }
165 write_reg(par, 0x00);
166
167 mdelay(50);
168 par->fbtftops.reset(par);
169 mdelay(1000);
170 par->spi->mode = save_mode;
171 ret = spi_setup(par->spi);
172 if (ret) {
173 dev_err(par->info->device, "Could not restore SPI mode\n");
174 return ret;
175 }
176 write_reg(par, 0x00);
177
178 version = firmware_version(par);
179 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "Firmware version: %x.%02x\n",
180 version >> 8, version & 0xFF);
181
182 if (mode == 332)
183 par->fbtftops.write_vmem = write_vmem_8bit;
184 return 0;
185}
186
187static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
188{
189
190}
191
192static int set_var(struct fbtft_par *par)
193{
194 u8 rotate;
195
196
197 switch (par->info->var.rotate) {
198 case 90:
199 rotate = 27;
200 break;
201 case 180:
202 rotate = 18;
203 break;
204 case 270:
205 rotate = 9;
206 break;
207 default:
208 rotate = 0;
209 }
210 write_reg(par, CMD_LCD_ORIENTATION, rotate);
211
212 return 0;
213}
214
215static int verify_gpios(struct fbtft_par *par)
216{
217 if (!par->gpio.reset) {
218 dev_err(par->info->device, "Missing 'reset' gpio. Aborting.\n");
219 return -EINVAL;
220 }
221 return 0;
222}
223
224#ifdef CONFIG_FB_BACKLIGHT
225static int backlight_chip_update_status(struct backlight_device *bd)
226{
227 struct fbtft_par *par = bl_get_data(bd);
228 int brightness = bd->props.brightness;
229
230 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
231 "%s: brightness=%d, power=%d, fb_blank=%d\n", __func__,
232 bd->props.brightness, bd->props.power,
233 bd->props.fb_blank);
234
235 if (bd->props.power != FB_BLANK_UNBLANK)
236 brightness = 0;
237
238 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
239 brightness = 0;
240
241 write_reg(par, CMD_LCD_LED, brightness);
242
243 return 0;
244}
245
246static const struct backlight_ops bl_ops = {
247 .update_status = backlight_chip_update_status,
248};
249
250static void register_chip_backlight(struct fbtft_par *par)
251{
252 struct backlight_device *bd;
253 struct backlight_properties bl_props = { 0, };
254
255 bl_props.type = BACKLIGHT_RAW;
256 bl_props.power = FB_BLANK_POWERDOWN;
257 bl_props.max_brightness = 100;
258 bl_props.brightness = DEFAULT_BRIGHTNESS;
259
260 bd = backlight_device_register(dev_driver_string(par->info->device),
261 par->info->device, par, &bl_ops,
262 &bl_props);
263 if (IS_ERR(bd)) {
264 dev_err(par->info->device,
265 "cannot register backlight device (%ld)\n",
266 PTR_ERR(bd));
267 return;
268 }
269 par->info->bl_dev = bd;
270
271 if (!par->fbtftops.unregister_backlight)
272 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
273}
274#else
275#define register_chip_backlight NULL
276#endif
277
278static struct fbtft_display display = {
279 .regwidth = 8,
280 .buswidth = 8,
281 .width = WIDTH,
282 .height = HEIGHT,
283 .fps = FPS,
284 .txbuflen = TXBUFLEN,
285 .fbtftops = {
286 .write_register = write_reg8_bus8,
287 .write_vmem = write_vmem,
288 .init_display = init_display,
289 .set_addr_win = set_addr_win,
290 .set_var = set_var,
291 .verify_gpios = verify_gpios,
292 .register_backlight = register_chip_backlight,
293 },
294};
295
296FBTFT_REGISTER_DRIVER(DRVNAME, "watterott,openlcd", &display);
297
298MODULE_ALIAS("spi:" DRVNAME);
299
300MODULE_DESCRIPTION("FB driver for the Watterott LCD Controller");
301MODULE_AUTHOR("Noralf Tronnes");
302MODULE_LICENSE("GPL");
303