GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / staging / fbtft / flexfb.c
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
29 static char *chip;
30 module_param(chip, charp, 0000);
31 MODULE_PARM_DESC(chip, "LCD controller");
32
33 static unsigned int width;
34 module_param(width, uint, 0000);
35 MODULE_PARM_DESC(width, "Display width");
36
37 static unsigned int height;
38 module_param(height, uint, 0000);
39 MODULE_PARM_DESC(height, "Display height");
40
41 static s16 init[512];
42 static int init_num;
43 module_param_array(init, short, &init_num, 0000);
44 MODULE_PARM_DESC(init, "Init sequence");
45
46 static unsigned int setaddrwin;
47 module_param(setaddrwin, uint, 0000);
48 MODULE_PARM_DESC(setaddrwin, "Which set_addr_win() implementation to use");
49
50 static unsigned int buswidth = 8;
51 module_param(buswidth, uint, 0000);
52 MODULE_PARM_DESC(buswidth, "Width of databus (default: 8)");
53
54 static unsigned int regwidth = 8;
55 module_param(regwidth, uint, 0000);
56 MODULE_PARM_DESC(regwidth, "Width of controller register (default: 8)");
57
58 static bool nobacklight;
59 module_param(nobacklight, bool, 0000);
60 MODULE_PARM_DESC(nobacklight, "Turn off backlight functionality.");
61
62 static bool latched;
63 module_param(latched, bool, 0000);
64 MODULE_PARM_DESC(latched, "Use with latched 16-bit databus");
65
66 static const s16 *initp;
67 static int initp_num;
68
69 /* default init sequences */
70 static const s16 st7735r_init[] = {
71         -1, 0x01,
72         -2, 150,
73         -1, 0x11,
74         -2, 500,
75         -1, 0xB1, 0x01, 0x2C, 0x2D,
76         -1, 0xB2, 0x01, 0x2C, 0x2D,
77         -1, 0xB3, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D,
78         -1, 0xB4, 0x07,
79         -1, 0xC0, 0xA2, 0x02, 0x84,
80         -1, 0xC1, 0xC5,
81         -1, 0xC2, 0x0A, 0x00,
82         -1, 0xC3, 0x8A, 0x2A,
83         -1, 0xC4, 0x8A, 0xEE,
84         -1, 0xC5, 0x0E,
85         -1, 0x20,
86         -1, 0x36, 0xC0,
87         -1, 0x3A, 0x05,
88         -1, 0xE0, 0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22,
89             0x1f, 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10,
90         -1, 0xE1, 0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e,
91             0x30, 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10,
92         -1, 0x29,
93         -2, 100,
94         -1, 0x13,
95         -2, 10,
96         -3
97 };
98
99 static const s16 ssd1289_init[] = {
100         -1, 0x00, 0x0001,
101         -1, 0x03, 0xA8A4,
102         -1, 0x0C, 0x0000,
103         -1, 0x0D, 0x080C,
104         -1, 0x0E, 0x2B00,
105         -1, 0x1E, 0x00B7,
106         -1, 0x01, 0x2B3F,
107         -1, 0x02, 0x0600,
108         -1, 0x10, 0x0000,
109         -1, 0x11, 0x6070,
110         -1, 0x05, 0x0000,
111         -1, 0x06, 0x0000,
112         -1, 0x16, 0xEF1C,
113         -1, 0x17, 0x0003,
114         -1, 0x07, 0x0233,
115         -1, 0x0B, 0x0000,
116         -1, 0x0F, 0x0000,
117         -1, 0x41, 0x0000,
118         -1, 0x42, 0x0000,
119         -1, 0x48, 0x0000,
120         -1, 0x49, 0x013F,
121         -1, 0x4A, 0x0000,
122         -1, 0x4B, 0x0000,
123         -1, 0x44, 0xEF00,
124         -1, 0x45, 0x0000,
125         -1, 0x46, 0x013F,
126         -1, 0x30, 0x0707,
127         -1, 0x31, 0x0204,
128         -1, 0x32, 0x0204,
129         -1, 0x33, 0x0502,
130         -1, 0x34, 0x0507,
131         -1, 0x35, 0x0204,
132         -1, 0x36, 0x0204,
133         -1, 0x37, 0x0502,
134         -1, 0x3A, 0x0302,
135         -1, 0x3B, 0x0302,
136         -1, 0x23, 0x0000,
137         -1, 0x24, 0x0000,
138         -1, 0x25, 0x8000,
139         -1, 0x4f, 0x0000,
140         -1, 0x4e, 0x0000,
141         -1, 0x22,
142         -3
143 };
144
145 static const s16 hx8340bn_init[] = {
146         -1, 0xC1, 0xFF, 0x83, 0x40,
147         -1, 0x11,
148         -2, 150,
149         -1, 0xCA, 0x70, 0x00, 0xD9,
150         -1, 0xB0, 0x01, 0x11,
151         -1, 0xC9, 0x90, 0x49, 0x10, 0x28, 0x28, 0x10, 0x00, 0x06,
152         -2, 20,
153         -1, 0xC2, 0x60, 0x71, 0x01, 0x0E, 0x05, 0x02, 0x09, 0x31, 0x0A,
154         -1, 0xC3, 0x67, 0x30, 0x61, 0x17, 0x48, 0x07, 0x05, 0x33,
155         -2, 10,
156         -1, 0xB5, 0x35, 0x20, 0x45,
157         -1, 0xB4, 0x33, 0x25, 0x4C,
158         -2, 10,
159         -1, 0x3A, 0x05,
160         -1, 0x29,
161         -2, 10,
162         -3
163 };
164
165 static const s16 ili9225_init[] = {
166         -1, 0x0001, 0x011C,
167         -1, 0x0002, 0x0100,
168         -1, 0x0003, 0x1030,
169         -1, 0x0008, 0x0808,
170         -1, 0x000C, 0x0000,
171         -1, 0x000F, 0x0A01,
172         -1, 0x0020, 0x0000,
173         -1, 0x0021, 0x0000,
174         -2, 50,
175         -1, 0x0010, 0x0A00,
176         -1, 0x0011, 0x1038,
177         -2, 50,
178         -1, 0x0012, 0x1121,
179         -1, 0x0013, 0x004E,
180         -1, 0x0014, 0x676F,
181         -1, 0x0030, 0x0000,
182         -1, 0x0031, 0x00DB,
183         -1, 0x0032, 0x0000,
184         -1, 0x0033, 0x0000,
185         -1, 0x0034, 0x00DB,
186         -1, 0x0035, 0x0000,
187         -1, 0x0036, 0x00AF,
188         -1, 0x0037, 0x0000,
189         -1, 0x0038, 0x00DB,
190         -1, 0x0039, 0x0000,
191         -1, 0x0050, 0x0000,
192         -1, 0x0051, 0x060A,
193         -1, 0x0052, 0x0D0A,
194         -1, 0x0053, 0x0303,
195         -1, 0x0054, 0x0A0D,
196         -1, 0x0055, 0x0A06,
197         -1, 0x0056, 0x0000,
198         -1, 0x0057, 0x0303,
199         -1, 0x0058, 0x0000,
200         -1, 0x0059, 0x0000,
201         -2, 50,
202         -1, 0x0007, 0x1017,
203         -2, 50,
204         -3
205 };
206
207 static const s16 ili9320_init[] = {
208         -1, 0x00E5, 0x8000,
209         -1, 0x0000, 0x0001,
210         -1, 0x0001, 0x0100,
211         -1, 0x0002, 0x0700,
212         -1, 0x0003, 0x1030,
213         -1, 0x0004, 0x0000,
214         -1, 0x0008, 0x0202,
215         -1, 0x0009, 0x0000,
216         -1, 0x000A, 0x0000,
217         -1, 0x000C, 0x0000,
218         -1, 0x000D, 0x0000,
219         -1, 0x000F, 0x0000,
220         -1, 0x0010, 0x0000,
221         -1, 0x0011, 0x0007,
222         -1, 0x0012, 0x0000,
223         -1, 0x0013, 0x0000,
224         -2, 200,
225         -1, 0x0010, 0x17B0,
226         -1, 0x0011, 0x0031,
227         -2, 50,
228         -1, 0x0012, 0x0138,
229         -2, 50,
230         -1, 0x0013, 0x1800,
231         -1, 0x0029, 0x0008,
232         -2, 50,
233         -1, 0x0020, 0x0000,
234         -1, 0x0021, 0x0000,
235         -1, 0x0030, 0x0000,
236         -1, 0x0031, 0x0505,
237         -1, 0x0032, 0x0004,
238         -1, 0x0035, 0x0006,
239         -1, 0x0036, 0x0707,
240         -1, 0x0037, 0x0105,
241         -1, 0x0038, 0x0002,
242         -1, 0x0039, 0x0707,
243         -1, 0x003C, 0x0704,
244         -1, 0x003D, 0x0807,
245         -1, 0x0050, 0x0000,
246         -1, 0x0051, 0x00EF,
247         -1, 0x0052, 0x0000,
248         -1, 0x0053, 0x013F,
249         -1, 0x0060, 0x2700,
250         -1, 0x0061, 0x0001,
251         -1, 0x006A, 0x0000,
252         -1, 0x0080, 0x0000,
253         -1, 0x0081, 0x0000,
254         -1, 0x0082, 0x0000,
255         -1, 0x0083, 0x0000,
256         -1, 0x0084, 0x0000,
257         -1, 0x0085, 0x0000,
258         -1, 0x0090, 0x0010,
259         -1, 0x0092, 0x0000,
260         -1, 0x0093, 0x0003,
261         -1, 0x0095, 0x0110,
262         -1, 0x0097, 0x0000,
263         -1, 0x0098, 0x0000,
264         -1, 0x0007, 0x0173,
265         -3
266 };
267
268 static const s16 ili9325_init[] = {
269         -1, 0x00E3, 0x3008,
270         -1, 0x00E7, 0x0012,
271         -1, 0x00EF, 0x1231,
272         -1, 0x0001, 0x0100,
273         -1, 0x0002, 0x0700,
274         -1, 0x0003, 0x1030,
275         -1, 0x0004, 0x0000,
276         -1, 0x0008, 0x0207,
277         -1, 0x0009, 0x0000,
278         -1, 0x000A, 0x0000,
279         -1, 0x000C, 0x0000,
280         -1, 0x000D, 0x0000,
281         -1, 0x000F, 0x0000,
282         -1, 0x0010, 0x0000,
283         -1, 0x0011, 0x0007,
284         -1, 0x0012, 0x0000,
285         -1, 0x0013, 0x0000,
286         -2, 200,
287         -1, 0x0010, 0x1690,
288         -1, 0x0011, 0x0223,
289         -2, 50,
290         -1, 0x0012, 0x000D,
291         -2, 50,
292         -1, 0x0013, 0x1200,
293         -1, 0x0029, 0x000A,
294         -1, 0x002B, 0x000C,
295         -2, 50,
296         -1, 0x0020, 0x0000,
297         -1, 0x0021, 0x0000,
298         -1, 0x0030, 0x0000,
299         -1, 0x0031, 0x0506,
300         -1, 0x0032, 0x0104,
301         -1, 0x0035, 0x0207,
302         -1, 0x0036, 0x000F,
303         -1, 0x0037, 0x0306,
304         -1, 0x0038, 0x0102,
305         -1, 0x0039, 0x0707,
306         -1, 0x003C, 0x0702,
307         -1, 0x003D, 0x1604,
308         -1, 0x0050, 0x0000,
309         -1, 0x0051, 0x00EF,
310         -1, 0x0052, 0x0000,
311         -1, 0x0053, 0x013F,
312         -1, 0x0060, 0xA700,
313         -1, 0x0061, 0x0001,
314         -1, 0x006A, 0x0000,
315         -1, 0x0080, 0x0000,
316         -1, 0x0081, 0x0000,
317         -1, 0x0082, 0x0000,
318         -1, 0x0083, 0x0000,
319         -1, 0x0084, 0x0000,
320         -1, 0x0085, 0x0000,
321         -1, 0x0090, 0x0010,
322         -1, 0x0092, 0x0600,
323         -1, 0x0007, 0x0133,
324         -3
325 };
326
327 static const s16 ili9341_init[] = {
328         -1, 0x28,
329         -2, 20,
330         -1, 0xCF, 0x00, 0x83, 0x30,
331         -1, 0xED, 0x64, 0x03, 0x12, 0x81,
332         -1, 0xE8, 0x85, 0x01, 0x79,
333         -1, 0xCB, 0x39, 0x2c, 0x00, 0x34, 0x02,
334         -1, 0xF7, 0x20,
335         -1, 0xEA, 0x00, 0x00,
336         -1, 0xC0, 0x26,
337         -1, 0xC1, 0x11,
338         -1, 0xC5, 0x35, 0x3E,
339         -1, 0xC7, 0xBE,
340         -1, 0xB1, 0x00, 0x1B,
341         -1, 0xB6, 0x0a, 0x82, 0x27, 0x00,
342         -1, 0xB7, 0x07,
343         -1, 0x3A, 0x55,
344         -1, 0x36, 0x48,
345         -1, 0x11,
346         -2, 120,
347         -1, 0x29,
348         -2, 20,
349         -3
350 };
351
352 static const s16 ssd1351_init[] = {
353         -1, 0xfd, 0x12,
354         -1, 0xfd, 0xb1,
355         -1, 0xae,
356         -1, 0xb3, 0xf1,
357         -1, 0xca, 0x7f,
358         -1, 0xa0, 0x74,
359         -1, 0x15, 0x00, 0x7f,
360         -1, 0x75, 0x00, 0x7f,
361         -1, 0xa1, 0x00,
362         -1, 0xa2, 0x00,
363         -1, 0xb5, 0x00,
364         -1, 0xab, 0x01,
365         -1, 0xb1, 0x32,
366         -1, 0xb4, 0xa0, 0xb5, 0x55,
367         -1, 0xbb, 0x17,
368         -1, 0xbe, 0x05,
369         -1, 0xc1, 0xc8, 0x80, 0xc8,
370         -1, 0xc7, 0x0f,
371         -1, 0xb6, 0x01,
372         -1, 0xa6,
373         -1, 0xaf,
374         -3
375 };
376
377 /**
378  * struct flexfb_lcd_controller - Describes the LCD controller properties
379  * @name: Model name of the chip
380  * @width: Width of display in pixels
381  * @height: Height of display in pixels
382  * @setaddrwin: Which set_addr_win() implementation to use
383  * @regwidth: LCD Controller Register width in bits
384  * @init_seq: LCD initialization sequence
385  * @init_seq_sz: Size of LCD initialization sequence
386  */
387 struct flexfb_lcd_controller {
388         const char *name;
389         unsigned int width;
390         unsigned int height;
391         unsigned int setaddrwin;
392         unsigned int regwidth;
393         const s16 *init_seq;
394         int init_seq_sz;
395 };
396
397 static const struct flexfb_lcd_controller flexfb_chip_table[] = {
398         {
399                 .name = "st7735r",
400                 .width = 120,
401                 .height = 160,
402                 .init_seq = st7735r_init,
403                 .init_seq_sz = ARRAY_SIZE(st7735r_init),
404         },
405         {
406                 .name = "hx8340bn",
407                 .width = 176,
408                 .height = 220,
409                 .init_seq = hx8340bn_init,
410                 .init_seq_sz = ARRAY_SIZE(hx8340bn_init),
411         },
412         {
413                 .name = "ili9225",
414                 .width = 176,
415                 .height = 220,
416                 .regwidth = 16,
417                 .init_seq = ili9225_init,
418                 .init_seq_sz = ARRAY_SIZE(ili9225_init),
419         },
420         {
421                 .name = "ili9320",
422                 .width = 240,
423                 .height = 320,
424                 .setaddrwin = 1,
425                 .regwidth = 16,
426                 .init_seq = ili9320_init,
427                 .init_seq_sz = ARRAY_SIZE(ili9320_init),
428         },
429         {
430                 .name = "ili9325",
431                 .width = 240,
432                 .height = 320,
433                 .setaddrwin = 1,
434                 .regwidth = 16,
435                 .init_seq = ili9325_init,
436                 .init_seq_sz = ARRAY_SIZE(ili9325_init),
437         },
438         {
439                 .name = "ili9341",
440                 .width = 240,
441                 .height = 320,
442                 .init_seq = ili9341_init,
443                 .init_seq_sz = ARRAY_SIZE(ili9341_init),
444         },
445         {
446                 .name = "ssd1289",
447                 .width = 240,
448                 .height = 320,
449                 .setaddrwin = 2,
450                 .regwidth = 16,
451                 .init_seq = ssd1289_init,
452                 .init_seq_sz = ARRAY_SIZE(ssd1289_init),
453         },
454         {
455                 .name = "ssd1351",
456                 .width = 128,
457                 .height = 128,
458                 .setaddrwin = 3,
459                 .init_seq = ssd1351_init,
460                 .init_seq_sz = ARRAY_SIZE(ssd1351_init),
461         },
462 };
463
464 /* ili9320, ili9325 */
465 static void flexfb_set_addr_win_1(struct fbtft_par *par,
466                                   int xs, int ys, int xe, int ye)
467 {
468         switch (par->info->var.rotate) {
469         /* R20h = Horizontal GRAM Start Address */
470         /* R21h = Vertical GRAM Start Address */
471         case 0:
472                 write_reg(par, 0x0020, xs);
473                 write_reg(par, 0x0021, ys);
474                 break;
475         case 180:
476                 write_reg(par, 0x0020, width - 1 - xs);
477                 write_reg(par, 0x0021, height - 1 - ys);
478                 break;
479         case 270:
480                 write_reg(par, 0x0020, width - 1 - ys);
481                 write_reg(par, 0x0021, xs);
482                 break;
483         case 90:
484                 write_reg(par, 0x0020, ys);
485                 write_reg(par, 0x0021, height - 1 - xs);
486                 break;
487         }
488         write_reg(par, 0x0022); /* Write Data to GRAM */
489 }
490
491 /* ssd1289 */
492 static void flexfb_set_addr_win_2(struct fbtft_par *par,
493                                   int xs, int ys, int xe, int ye)
494 {
495         switch (par->info->var.rotate) {
496         /* R4Eh - Set GDDRAM X address counter */
497         /* R4Fh - Set GDDRAM Y address counter */
498         case 0:
499                 write_reg(par, 0x4e, xs);
500                 write_reg(par, 0x4f, ys);
501                 break;
502         case 180:
503                 write_reg(par, 0x4e, par->info->var.xres - 1 - xs);
504                 write_reg(par, 0x4f, par->info->var.yres - 1 - ys);
505                 break;
506         case 270:
507                 write_reg(par, 0x4e, par->info->var.yres - 1 - ys);
508                 write_reg(par, 0x4f, xs);
509                 break;
510         case 90:
511                 write_reg(par, 0x4e, ys);
512                 write_reg(par, 0x4f, par->info->var.xres - 1 - xs);
513                 break;
514         }
515
516         /* R22h - RAM data write */
517         write_reg(par, 0x22, 0);
518 }
519
520 /* ssd1351 */
521 static void set_addr_win_3(struct fbtft_par *par,
522                            int xs, int ys, int xe, int ye)
523 {
524         write_reg(par, 0x15, xs, xe);
525         write_reg(par, 0x75, ys, ye);
526         write_reg(par, 0x5C);
527 }
528
529 static int flexfb_verify_gpios_dc(struct fbtft_par *par)
530 {
531         fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
532
533         if (par->gpio.dc < 0) {
534                 dev_err(par->info->device,
535                         "Missing info about 'dc' gpio. Aborting.\n");
536                 return -EINVAL;
537         }
538
539         return 0;
540 }
541
542 static int flexfb_verify_gpios_db(struct fbtft_par *par)
543 {
544         int i;
545         int num_db = buswidth;
546
547         fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
548
549         if (par->gpio.dc < 0) {
550                 dev_err(par->info->device, "Missing info about 'dc' gpio. Aborting.\n");
551                 return -EINVAL;
552         }
553         if (par->gpio.wr < 0) {
554                 dev_err(par->info->device, "Missing info about 'wr' gpio. Aborting.\n");
555                 return -EINVAL;
556         }
557         if (latched && (par->gpio.latch < 0)) {
558                 dev_err(par->info->device, "Missing info about 'latch' gpio. Aborting.\n");
559                 return -EINVAL;
560         }
561         if (latched)
562                 num_db = buswidth / 2;
563         for (i = 0; i < num_db; i++) {
564                 if (par->gpio.db[i] < 0) {
565                         dev_err(par->info->device,
566                                 "Missing info about 'db%02d' gpio. Aborting.\n",
567                                 i);
568                         return -EINVAL;
569                 }
570         }
571
572         return 0;
573 }
574
575 static void flexfb_chip_load_param(const struct flexfb_lcd_controller *chip)
576 {
577         if (!width)
578                 width = chip->width;
579         if (!height)
580                 height = chip->height;
581         setaddrwin = chip->setaddrwin;
582         if (chip->regwidth)
583                 regwidth = chip->regwidth;
584         if (!init_num) {
585                 initp = chip->init_seq;
586                 initp_num = chip->init_seq_sz;
587         }
588 }
589
590 static struct fbtft_display flex_display = { };
591
592 static int flexfb_chip_init(const struct device *dev)
593 {
594         int i;
595
596         for (i = 0; i < ARRAY_SIZE(flexfb_chip_table); i++)
597                 if (!strcmp(chip, flexfb_chip_table[i].name)) {
598                         flexfb_chip_load_param(&flexfb_chip_table[i]);
599                         return 0;
600                 }
601
602         dev_err(dev, "chip=%s is not supported\n", chip);
603
604         return -EINVAL;
605 }
606
607 static int flexfb_probe_common(struct spi_device *sdev,
608                                struct platform_device *pdev)
609 {
610         struct device *dev;
611         struct fb_info *info;
612         struct fbtft_par *par;
613         int ret;
614
615         initp = init;
616         initp_num = init_num;
617
618         if (sdev)
619                 dev = &sdev->dev;
620         else
621                 dev = &pdev->dev;
622
623         fbtft_init_dbg(dev, "%s(%s)\n", __func__,
624                        sdev ? "'SPI device'" : "'Platform device'");
625
626         if (chip) {
627                 ret = flexfb_chip_init(dev);
628                 if (ret)
629                         return ret;
630         }
631
632         if (width == 0 || height == 0) {
633                 dev_err(dev, "argument(s) missing: width and height has to be set.\n");
634                 return -EINVAL;
635         }
636         flex_display.width = width;
637         flex_display.height = height;
638         fbtft_init_dbg(dev, "Display resolution: %dx%d\n", width, height);
639         fbtft_init_dbg(dev, "chip = %s\n", chip ? chip : "not set");
640         fbtft_init_dbg(dev, "setaddrwin = %d\n", setaddrwin);
641         fbtft_init_dbg(dev, "regwidth = %d\n", regwidth);
642         fbtft_init_dbg(dev, "buswidth = %d\n", buswidth);
643
644         info = fbtft_framebuffer_alloc(&flex_display, dev, dev->platform_data);
645         if (!info)
646                 return -ENOMEM;
647
648         par = info->par;
649         if (sdev)
650                 par->spi = sdev;
651         else
652                 par->pdev = pdev;
653         if (!par->init_sequence)
654                 par->init_sequence = initp;
655         par->fbtftops.init_display = fbtft_init_display;
656
657         /* registerwrite functions */
658         switch (regwidth) {
659         case 8:
660                 par->fbtftops.write_register = fbtft_write_reg8_bus8;
661                 break;
662         case 16:
663                 par->fbtftops.write_register = fbtft_write_reg16_bus8;
664                 break;
665         default:
666                 dev_err(dev,
667                         "argument 'regwidth': %d is not supported.\n",
668                         regwidth);
669                 return -EINVAL;
670         }
671
672         /* bus functions */
673         if (sdev) {
674                 par->fbtftops.write = fbtft_write_spi;
675                 switch (buswidth) {
676                 case 8:
677                         par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
678                         if (!par->startbyte)
679                                 par->fbtftops.verify_gpios = flexfb_verify_gpios_dc;
680                         break;
681                 case 9:
682                         if (regwidth == 16) {
683                                 dev_err(dev, "argument 'regwidth': %d is not supported with buswidth=%d and SPI.\n", regwidth, buswidth);
684                                 return -EINVAL;
685                         }
686                         par->fbtftops.write_register = fbtft_write_reg8_bus9;
687                         par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
688                         if (par->spi->master->bits_per_word_mask
689                             & SPI_BPW_MASK(9)) {
690                                 par->spi->bits_per_word = 9;
691                         } else {
692                                 dev_warn(dev,
693                                         "9-bit SPI not available, emulating using 8-bit.\n");
694                                 /* allocate buffer with room for dc bits */
695                                 par->extra = devm_kzalloc(par->info->device,
696                                                 par->txbuf.len + (par->txbuf.len / 8) + 8,
697                                                 GFP_KERNEL);
698                                 if (!par->extra) {
699                                         ret = -ENOMEM;
700                                         goto out_release;
701                                 }
702                                 par->fbtftops.write = fbtft_write_spi_emulate_9;
703                         }
704                         break;
705                 default:
706                         dev_err(dev, "argument 'buswidth': %d is not supported with SPI.\n", buswidth);
707                         return -EINVAL;
708                 }
709         } else {
710                 par->fbtftops.verify_gpios = flexfb_verify_gpios_db;
711                 switch (buswidth) {
712                 case 8:
713                         par->fbtftops.write = fbtft_write_gpio8_wr;
714                         par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
715                         break;
716                 case 16:
717                         par->fbtftops.write_register = fbtft_write_reg16_bus16;
718                         if (latched)
719                                 par->fbtftops.write = fbtft_write_gpio16_wr_latched;
720                         else
721                                 par->fbtftops.write = fbtft_write_gpio16_wr;
722                         par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
723                         break;
724                 default:
725                         dev_err(dev, "argument 'buswidth': %d is not supported with parallel.\n", buswidth);
726                         return -EINVAL;
727                 }
728         }
729
730         /* set_addr_win function */
731         switch (setaddrwin) {
732         case 0:
733                 /* use default */
734                 break;
735         case 1:
736                 par->fbtftops.set_addr_win = flexfb_set_addr_win_1;
737                 break;
738         case 2:
739                 par->fbtftops.set_addr_win = flexfb_set_addr_win_2;
740                 break;
741         case 3:
742                 par->fbtftops.set_addr_win = set_addr_win_3;
743                 break;
744         default:
745                 dev_err(dev, "argument 'setaddrwin': unknown value %d.\n",
746                         setaddrwin);
747                 return -EINVAL;
748         }
749
750         if (!nobacklight)
751                 par->fbtftops.register_backlight = fbtft_register_backlight;
752
753         ret = fbtft_register_framebuffer(info);
754         if (ret < 0)
755                 goto out_release;
756
757         return 0;
758
759 out_release:
760         fbtft_framebuffer_release(info);
761
762         return ret;
763 }
764
765 static int flexfb_remove_common(struct device *dev, struct fb_info *info)
766 {
767         struct fbtft_par *par;
768
769         if (!info)
770                 return -EINVAL;
771         par = info->par;
772         if (par)
773                 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par, "%s()\n",
774                               __func__);
775         fbtft_unregister_framebuffer(info);
776         fbtft_framebuffer_release(info);
777
778         return 0;
779 }
780
781 static int flexfb_probe_spi(struct spi_device *spi)
782 {
783         return flexfb_probe_common(spi, NULL);
784 }
785
786 static int flexfb_remove_spi(struct spi_device *spi)
787 {
788         struct fb_info *info = spi_get_drvdata(spi);
789
790         return flexfb_remove_common(&spi->dev, info);
791 }
792
793 static int flexfb_probe_pdev(struct platform_device *pdev)
794 {
795         return flexfb_probe_common(NULL, pdev);
796 }
797
798 static int flexfb_remove_pdev(struct platform_device *pdev)
799 {
800         struct fb_info *info = platform_get_drvdata(pdev);
801
802         return flexfb_remove_common(&pdev->dev, info);
803 }
804
805 static struct spi_driver flexfb_spi_driver = {
806         .driver = {
807                 .name   = DRVNAME,
808         },
809         .probe  = flexfb_probe_spi,
810         .remove = flexfb_remove_spi,
811 };
812
813 static const struct platform_device_id flexfb_platform_ids[] = {
814         { "flexpfb", 0 },
815         { },
816 };
817 MODULE_DEVICE_TABLE(platform, flexfb_platform_ids);
818
819 static struct platform_driver flexfb_platform_driver = {
820         .driver = {
821                 .name   = DRVNAME,
822         },
823         .id_table = flexfb_platform_ids,
824         .probe  = flexfb_probe_pdev,
825         .remove = flexfb_remove_pdev,
826 };
827
828 static int __init flexfb_init(void)
829 {
830         int ret, ret2;
831
832         ret = spi_register_driver(&flexfb_spi_driver);
833         ret2 = platform_driver_register(&flexfb_platform_driver);
834         if (ret < 0)
835                 return ret;
836         return ret2;
837 }
838
839 static void __exit flexfb_exit(void)
840 {
841         spi_unregister_driver(&flexfb_spi_driver);
842         platform_driver_unregister(&flexfb_platform_driver);
843 }
844
845 /* ------------------------------------------------------------------------- */
846
847 module_init(flexfb_init);
848 module_exit(flexfb_exit);
849
850 MODULE_DESCRIPTION("Generic FB driver for TFT LCD displays");
851 MODULE_AUTHOR("Noralf Tronnes");
852 MODULE_LICENSE("GPL");