forked from RobertCNelson/linux-dev
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path0003-video-st7735fb-add-st7735-framebuffer-driver.patch
803 lines (796 loc) · 19.4 KB
/
0003-video-st7735fb-add-st7735-framebuffer-driver.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
From 9f56f6df5491cceb02019776830af3062ac3dcc4 Mon Sep 17 00:00:00 2001
From: Matt Porter <mporter@ti.com>
Date: Tue, 11 Sep 2012 15:30:10 -0400
Subject: [PATCH 03/35] video: st7735fb: add st7735 framebuffer driver
Add driver for the SPI-connected ST7735 display controller.
This driver requires that the platform support DT booting.
Signed-off-by: Matt Porter <mporter@ti.com>
---
.../devicetree/bindings/vendor-prefixes.txt | 2 +
drivers/video/Kconfig | 11 +
drivers/video/Makefile | 1 +
drivers/video/st7735fb.c | 631 ++++++++++++++++++++
drivers/video/st7735fb.h | 84 +++
5 files changed, 729 insertions(+)
create mode 100644 drivers/video/st7735fb.c
create mode 100644 drivers/video/st7735fb.h
diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index 902b1b1..2e000ea 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -4,6 +4,7 @@ This isn't an exhaustive list, but you should add new prefixes to it before
using them to avoid name-space collisions.
ad Avionic Design GmbH
+adafruit Adafruit Industries
adi Analog Devices, Inc.
ak Asahi Kasei Corp.
amcc Applied Micro Circuits Corporation (APM, formally AMCC)
@@ -48,6 +49,7 @@ schindler Schindler
sil Silicon Image
simtek
sirf SiRF Technology, Inc.
+sitronix Sitronix Technology Corp.
snps Synopsys, Inc.
st STMicroelectronics
stericsson ST-Ericsson
diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
index e7068c5..b532f02 100644
--- a/drivers/video/Kconfig
+++ b/drivers/video/Kconfig
@@ -2419,6 +2419,17 @@ config FB_PUV3_UNIGFX
Choose this option if you want to use the Unigfx device as a
framebuffer device. Without the support of PCI & AGP.
+config FB_ST7735
+ tristate "ST7735 framebuffer support"
+ depends on FB && SPI
+ select FB_SYS_FILLRECT
+ select FB_SYS_COPYAREA
+ select FB_SYS_IMAGEBLIT
+ select FB_SYS_FOPS
+ select FB_DEFERRED_IO
+ help
+ Framebuffer support for the ST7735 display controller in SPI mode.
+
source "drivers/video/omap/Kconfig"
source "drivers/video/omap2/Kconfig"
source "drivers/video/exynos/Kconfig"
diff --git a/drivers/video/Makefile b/drivers/video/Makefile
index 768a137..58dbeaf 100644
--- a/drivers/video/Makefile
+++ b/drivers/video/Makefile
@@ -146,6 +146,7 @@ obj-$(CONFIG_FB_MSM) += msm/
obj-$(CONFIG_FB_NUC900) += nuc900fb.o
obj-$(CONFIG_FB_JZ4740) += jz4740_fb.o
obj-$(CONFIG_FB_PUV3_UNIGFX) += fb-puv3.o
+obj-$(CONFIG_FB_ST7735) += st7735fb.o
# Platform or fallback drivers go here
obj-$(CONFIG_FB_UVESA) += uvesafb.o
diff --git a/drivers/video/st7735fb.c b/drivers/video/st7735fb.c
new file mode 100644
index 0000000..319653b
--- /dev/null
+++ b/drivers/video/st7735fb.c
@@ -0,0 +1,631 @@
+/*
+ * linux/drivers/video/st7735fb.c -- FB driver for ST7735 LCD controller
+ * Layout is based on skeletonfb.c by James Simmons and Geert Uytterhoeven.
+ *
+ * Copyright (C) 2012, Matt Porter <matt@ohporter.com>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file COPYING in the main directory of this archive for
+ * more details.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/string.h>
+#include <linux/mm.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/init.h>
+#include <linux/fb.h>
+#include <linux/gpio.h>
+#include <linux/spi/spi.h>
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/uaccess.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/of_gpio.h>
+#include <linux/pinctrl/consumer.h>
+
+#include "st7735fb.h"
+
+static struct st7735_function st7735_cfg_script[] = {
+ { ST7735_START, ST7735_START},
+ { ST7735_CMD, ST7735_SWRESET},
+ { ST7735_DELAY, 150},
+ { ST7735_CMD, ST7735_SLPOUT},
+ { ST7735_DELAY, 500},
+ { ST7735_CMD, ST7735_FRMCTR1},
+ { ST7735_DATA, 0x01},
+ { ST7735_DATA, 0x2c},
+ { ST7735_DATA, 0x2d},
+ { ST7735_CMD, ST7735_FRMCTR2},
+ { ST7735_DATA, 0x01},
+ { ST7735_DATA, 0x2c},
+ { ST7735_DATA, 0x2d},
+ { ST7735_CMD, ST7735_FRMCTR3},
+ { ST7735_DATA, 0x01},
+ { ST7735_DATA, 0x2c},
+ { ST7735_DATA, 0x2d},
+ { ST7735_DATA, 0x01},
+ { ST7735_DATA, 0x2c},
+ { ST7735_DATA, 0x2d},
+ { ST7735_CMD, ST7735_INVCTR},
+ { ST7735_DATA, 0x07},
+ { ST7735_CMD, ST7735_PWCTR1},
+ { ST7735_DATA, 0xa2},
+ { ST7735_DATA, 0x02},
+ { ST7735_DATA, 0x84},
+ { ST7735_CMD, ST7735_PWCTR2},
+ { ST7735_DATA, 0xc5},
+ { ST7735_CMD, ST7735_PWCTR3},
+ { ST7735_DATA, 0x0a},
+ { ST7735_DATA, 0x00},
+ { ST7735_CMD, ST7735_PWCTR4},
+ { ST7735_DATA, 0x8a},
+ { ST7735_DATA, 0x2a},
+ { ST7735_CMD, ST7735_PWCTR5},
+ { ST7735_DATA, 0x8a},
+ { ST7735_DATA, 0xee},
+ { ST7735_CMD, ST7735_VMCTR1},
+ { ST7735_DATA, 0x0e},
+ { ST7735_CMD, ST7735_INVOFF},
+ { ST7735_CMD, ST7735_MADCTL},
+ { ST7735_DATA, 0xc8},
+ { ST7735_CMD, ST7735_COLMOD},
+ { ST7735_DATA, 0x05},
+ { ST7735_CMD, ST7735_CASET},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x7f},
+ { ST7735_CMD, ST7735_RASET},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x9f},
+ { ST7735_CMD, ST7735_GMCTRP1},
+ { ST7735_DATA, 0x02},
+ { ST7735_DATA, 0x1c},
+ { ST7735_DATA, 0x07},
+ { ST7735_DATA, 0x12},
+ { ST7735_DATA, 0x37},
+ { ST7735_DATA, 0x32},
+ { ST7735_DATA, 0x29},
+ { ST7735_DATA, 0x2d},
+ { ST7735_DATA, 0x29},
+ { ST7735_DATA, 0x25},
+ { ST7735_DATA, 0x2b},
+ { ST7735_DATA, 0x39},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x01},
+ { ST7735_DATA, 0x03},
+ { ST7735_DATA, 0x10},
+ { ST7735_CMD, ST7735_GMCTRN1},
+ { ST7735_DATA, 0x03},
+ { ST7735_DATA, 0x1d},
+ { ST7735_DATA, 0x07},
+ { ST7735_DATA, 0x06},
+ { ST7735_DATA, 0x2e},
+ { ST7735_DATA, 0x2c},
+ { ST7735_DATA, 0x29},
+ { ST7735_DATA, 0x2d},
+ { ST7735_DATA, 0x2e},
+ { ST7735_DATA, 0x2e},
+ { ST7735_DATA, 0x37},
+ { ST7735_DATA, 0x3f},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x00},
+ { ST7735_DATA, 0x02},
+ { ST7735_DATA, 0x10},
+ { ST7735_CMD, ST7735_DISPON},
+ { ST7735_DELAY, 100},
+ { ST7735_CMD, ST7735_NORON},
+ { ST7735_DELAY, 10},
+ { ST7735_END, ST7735_END},
+};
+
+static struct fb_fix_screeninfo st7735fb_fix __devinitdata = {
+ .id = "ST7735",
+ .type = FB_TYPE_PACKED_PIXELS,
+ .visual = FB_VISUAL_DIRECTCOLOR,
+ .xpanstep = 0,
+ .ypanstep = 0,
+ .ywrapstep = 0,
+ .line_length = WIDTH*BPP/8,
+ .accel = FB_ACCEL_NONE,
+};
+
+static struct fb_var_screeninfo st7735fb_var __devinitdata = {
+ .xres = WIDTH,
+ .yres = HEIGHT,
+ .xres_virtual = WIDTH,
+ .yres_virtual = HEIGHT,
+ .bits_per_pixel = BPP,
+ .nonstd = 0,
+};
+
+static int st7735_write(struct st7735fb_par *par, u8 data)
+{
+ par->buf[0] = data;
+
+ return spi_write(par->spi, par->buf, 1);
+}
+
+static void st7735_write_data(struct st7735fb_par *par, u8 data)
+{
+ int ret = 0;
+
+ /* Set data mode */
+ gpio_set_value(par->dc, 1);
+
+ ret = st7735_write(par, data);
+ if (ret < 0)
+ pr_err("%s: write data %02x failed with status %d\n",
+ par->info->fix.id, data, ret);
+}
+
+static int st7735_write_data_buf(struct st7735fb_par *par,
+ u8 *txbuf, int size)
+{
+ /* Set data mode */
+ gpio_set_value(par->dc, 1);
+
+ /* Write entire buffer */
+ return spi_write(par->spi, txbuf, size);
+}
+
+static void st7735_write_cmd(struct st7735fb_par *par, u8 data)
+{
+ int ret = 0;
+
+ /* Set command mode */
+ gpio_set_value(par->dc, 0);
+
+ ret = st7735_write(par, data);
+ if (ret < 0)
+ pr_err("%s: write command %02x failed with status %d\n",
+ par->info->fix.id, data, ret);
+}
+
+static void st7735_run_cfg_script(struct st7735fb_par *par)
+{
+ int i = 0;
+ int end_script = 0;
+
+ do {
+ switch (st7735_cfg_script[i].cmd)
+ {
+ case ST7735_START:
+ break;
+ case ST7735_CMD:
+ st7735_write_cmd(par,
+ st7735_cfg_script[i].data & 0xff);
+ break;
+ case ST7735_DATA:
+ st7735_write_data(par,
+ st7735_cfg_script[i].data & 0xff);
+ break;
+ case ST7735_DELAY:
+ mdelay(st7735_cfg_script[i].data);
+ break;
+ case ST7735_END:
+ end_script = 1;
+ }
+ i++;
+ } while (!end_script);
+}
+
+static void st7735_set_addr_win(struct st7735fb_par *par,
+ int xs, int ys, int xe, int ye)
+{
+ st7735_write_cmd(par, ST7735_CASET);
+ st7735_write_data(par, 0x00);
+ st7735_write_data(par, xs + par->xoff);
+ st7735_write_data(par, 0x00);
+ st7735_write_data(par, xe + par->xoff);
+ st7735_write_cmd(par, ST7735_RASET);
+ st7735_write_data(par, 0x00);
+ st7735_write_data(par, ys + par->yoff);
+ st7735_write_data(par, 0x00);
+ st7735_write_data(par, ye + par->yoff);
+}
+
+static void st7735_reset(struct st7735fb_par *par)
+{
+ /* Reset controller */
+ gpio_set_value(par->rst, 0);
+ udelay(10);
+ gpio_set_value(par->rst, 1);
+ mdelay(120);
+}
+
+static void st7735fb_update_display(struct st7735fb_par *par)
+{
+ int ret = 0;
+ u16 *vmem;
+#ifdef __LITTLE_ENDIAN
+ int i;
+ u16 *vmem16 = (u16 *)par->info->screen_base;
+ vmem = par->ssbuf;
+
+ for (i=0; i<WIDTH*HEIGHT*BPP/8/2; i++)
+ vmem[i] = swab16(vmem16[i]);
+#else
+ vmem = (u16 *)par->info->screen_base;
+#endif
+
+ mutex_lock(&(par->io_lock));
+
+ /* Set row/column data window */
+ st7735_set_addr_win(par, 0, 0, WIDTH-1, HEIGHT-1);
+
+ /* Internal RAM write command */
+ st7735_write_cmd(par, ST7735_RAMWR);
+
+ /* Blast framebuffer to ST7735 internal display RAM */
+ ret = st7735_write_data_buf(par, (u8 *)vmem, WIDTH*HEIGHT*BPP/8);
+ if (ret < 0)
+ pr_err("%s: spi_write failed to update display buffer\n",
+ par->info->fix.id);
+
+ mutex_unlock(&(par->io_lock));
+}
+
+static int st7735fb_init_display(struct st7735fb_par *par)
+{
+ int ret = 0;
+
+ /* Request GPIOs and initialize to default values */
+ ret = gpio_request_one(par->rst, GPIOF_OUT_INIT_HIGH,
+ "ST7735 Reset Pin");
+ if (ret < 0) {
+ pr_err("%s: failed to claim reset pin\n", par->info->fix.id);
+ goto out;
+ }
+ ret = gpio_request_one(par->dc, GPIOF_OUT_INIT_LOW,
+ "ST7735 Data/Command Pin");
+ if (ret < 0) {
+ pr_err("%s: failed to claim data/command pin\n", par->info->fix.id);
+ goto out;
+ }
+
+ st7735_reset(par);
+
+ st7735_run_cfg_script(par);
+
+out:
+ return ret;
+}
+
+static void st7735fb_deferred_io(struct fb_info *info,
+ struct list_head *pagelist)
+{
+ st7735fb_update_display(info->par);
+}
+
+static void st7735fb_update_display_deferred(struct fb_info *info)
+{
+ struct fb_deferred_io *fbdefio = info->fbdefio;
+
+ schedule_delayed_work(&info->deferred_work, fbdefio->delay);
+}
+
+static void st7735fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
+{
+ sys_fillrect(info, rect);
+
+ st7735fb_update_display_deferred(info);
+}
+
+static void st7735fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
+{
+ sys_copyarea(info, area);
+
+ st7735fb_update_display_deferred(info);
+}
+
+static void st7735fb_imageblit(struct fb_info *info, const struct fb_image *image)
+{
+ sys_imageblit(info, image);
+
+ st7735fb_update_display_deferred(info);
+}
+
+static ssize_t st7735fb_write(struct fb_info *info, const char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ unsigned long p = *ppos;
+ void *dst;
+ int err = 0;
+ unsigned long total_size;
+
+ if (info->state != FBINFO_STATE_RUNNING)
+ return -EPERM;
+
+ total_size = info->fix.smem_len;
+
+ if (p > total_size)
+ return -EFBIG;
+
+ if (count > total_size) {
+ err = -EFBIG;
+ count = total_size;
+ }
+
+ if (count + p > total_size) {
+ if (!err)
+ err = -ENOSPC;
+
+ count = total_size - p;
+ }
+
+ dst = (void __force *) (info->screen_base + p);
+
+ if (copy_from_user(dst, buf, count))
+ err = -EFAULT;
+
+ if (!err)
+ *ppos += count;
+
+ st7735fb_update_display_deferred(info);
+
+ return (err) ? err : count;
+}
+
+static int st7735fb_setcolreg(unsigned regno, unsigned red, unsigned green,
+ unsigned blue, unsigned transp,
+ struct fb_info *info)
+{
+ if (regno >= MAX_PALETTE)
+ return -EINVAL;
+
+ /* RGB565 */
+ ((u32*)(info->pseudo_palette))[regno] =
+ ((red & 0xf800) |
+ ((green & 0xfc00) >> 5) |
+ ((blue & 0xf800) >> 11));
+
+ return 0;
+}
+
+static struct fb_ops st7735fb_ops = {
+ .owner = THIS_MODULE,
+ .fb_read = fb_sys_read,
+ .fb_write = st7735fb_write,
+ .fb_fillrect = st7735fb_fillrect,
+ .fb_copyarea = st7735fb_copyarea,
+ .fb_imageblit = st7735fb_imageblit,
+ .fb_setcolreg = st7735fb_setcolreg,
+};
+
+static struct fb_deferred_io st7735fb_defio = {
+ .delay = HZ/20,
+ .deferred_io = st7735fb_deferred_io,
+};
+
+static const struct spi_device_id st7735fb_device_id[] = {
+ {
+ .name = "tft-lcd-1.8-green",
+ .driver_data = ST7735_AF_TFT18_GREEN,
+ }, {
+ .name = "tft-lcd-1.8-red",
+ .driver_data = ST7735_AF_TFT18_RED,
+ }, {
+ /* sentinel */
+ }
+};
+MODULE_DEVICE_TABLE(spi, st7735fb_device_id);
+
+static const struct of_device_id st7735fb_dt_ids[] = {
+ { .compatible = "adafruit,tft-lcd-1.8-green", .data = (void *) ST7735_AF_TFT18_GREEN, },
+ { .compatible = "adafruit,tft-lcd-1.8-red", .data = (void *) ST7735_AF_TFT18_RED, },
+};
+MODULE_DEVICE_TABLE(of, st7735fb_dt_ids);
+
+static int __devinit st7735fb_probe (struct spi_device *spi)
+{
+ int vmem_size = WIDTH*HEIGHT*BPP/8;
+ u8 *vmem;
+ struct fb_info *info;
+ struct st7735fb_par *par;
+ int retval = -ENOMEM;
+ struct device_node *np = spi->dev.of_node;
+ const struct spi_device_id *spi_id = spi_get_device_id(spi);
+ struct pinctrl *pinctrl;
+
+ if (!spi_id) {
+ dev_err(&spi->dev,
+ "device id not supported!\n");
+ return -EINVAL;
+ }
+
+ pinctrl = devm_pinctrl_get_select_default(&spi->dev);
+ if (IS_ERR(pinctrl))
+ dev_warn(&spi->dev,
+ "pins are not configured from the driver\n");
+
+#ifdef __LITTLE_ENDIAN
+ vmem = (u8 *)vmalloc(vmem_size);
+#else
+ vmem = (u8 *)kmalloc(vmem_size, GFP_KERNEL);
+#endif
+ if (!vmem)
+ return retval;
+
+ info = framebuffer_alloc(sizeof(struct st7735fb_par), &spi->dev);
+ if (!info)
+ goto fballoc_fail;
+
+ info->pseudo_palette = kmalloc(sizeof(u32)*MAX_PALETTE, GFP_KERNEL);
+ if (!info->pseudo_palette) {
+ goto palette_fail;
+ }
+
+ info->screen_base = (u8 __force __iomem *)vmem;
+ info->fbops = &st7735fb_ops;
+ info->fix = st7735fb_fix;
+ info->fix.smem_len = vmem_size;
+ info->var = st7735fb_var;
+ /* Choose any packed pixel format as long as it's RGB565 */
+ info->var.red.offset = 11;
+ info->var.red.length = 5;
+ info->var.green.offset = 5;
+ info->var.green.length = 6;
+ info->var.blue.offset = 0;
+ info->var.blue.length = 5;
+ info->var.transp.offset = 0;
+ info->var.transp.length = 0;
+ info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
+
+ info->fbdefio = &st7735fb_defio;
+ fb_deferred_io_init(info);
+
+ retval = fb_alloc_cmap(&info->cmap, MAX_PALETTE, 0);
+ if (retval < 0)
+ goto cmap_fail;
+ info->cmap.len = MAX_PALETTE;
+
+ par = info->par;
+ par->info = info;
+ par->spi = spi;
+
+ mutex_init(&par->io_lock);
+
+ if (spi_id->driver_data == ST7735_AF_TFT18_GREEN) {
+ par->xoff = 2;
+ par->yoff = 1;
+ } else {
+ par->xoff = 0;
+ par->yoff = 0;
+ }
+
+ /* TODO: fix all exit paths for cleanup */
+ par->rst = of_get_named_gpio(np, "st7735-rst", 0);
+ if (par->rst < 0) {
+ printk("failed to find st7735-rst node!\n");
+ return -EINVAL;
+ }
+
+ par->dc = of_get_named_gpio(np, "st7735-dc", 0);
+ if (par->dc < 0) {
+ printk("failed to find st7735-dc node!\n");
+ return -EINVAL;
+ }
+
+ par->buf = kmalloc(1, GFP_KERNEL);
+ if (!par->buf) {
+ retval = -ENOMEM;
+ goto buf_fail;
+ }
+
+#ifdef __LITTLE_ENDIAN
+ /* Allocated swapped shadow buffer */
+ par->ssbuf = kmalloc(vmem_size, GFP_KERNEL);
+ if (!par->ssbuf) {
+ retval = -ENOMEM;
+ goto ssbuf_fail;
+ }
+#endif
+
+ retval = st7735fb_init_display(par);
+ if (retval < 0)
+ goto init_fail;
+
+ retval = register_framebuffer(info);
+ if (retval < 0)
+ goto fbreg_fail;
+
+ spi_set_drvdata(spi, info);
+
+ printk(KERN_INFO
+ "fb%d: %s frame buffer device,\n\tusing %d KiB of video memory\n",
+ info->node, info->fix.id, vmem_size);
+
+ return 0;
+
+
+ spi_set_drvdata(spi, NULL);
+
+fbreg_fail:
+ /* TODO: release gpios on fail */
+ /* TODO: and unwind everything in init */
+
+init_fail:
+#ifdef __LITTLE_ENDIAN
+ kfree(par->ssbuf);
+#endif
+
+ssbuf_fail:
+ kfree(par->buf);
+
+buf_fail:
+ fb_dealloc_cmap(&info->cmap);
+
+cmap_fail:
+ kfree(info->pseudo_palette);
+
+palette_fail:
+ framebuffer_release(info);
+
+fballoc_fail:
+#ifdef __LITTLE_ENDIAN
+ vfree(vmem);
+#else
+ kfree(vmem);
+#endif
+
+ return retval;
+}
+
+static int __devexit st7735fb_remove(struct spi_device *spi)
+{
+ struct fb_info *info = spi_get_drvdata(spi);
+
+ spi_set_drvdata(spi, NULL);
+
+ if (info) {
+ unregister_framebuffer(info);
+ fb_dealloc_cmap(&info->cmap);
+ kfree(info->pseudo_palette);
+ vfree(info->screen_base);
+ framebuffer_release(info);
+ }
+
+ /* TODO: release gpios */
+
+ return 0;
+}
+
+static struct spi_driver st7735fb_driver = {
+ .id_table = st7735fb_device_id,
+ .driver = {
+ .name = "st7735",
+ .owner = THIS_MODULE,
+ .of_match_table = st7735fb_dt_ids,
+ },
+ .probe = st7735fb_probe,
+ .remove = __devexit_p(st7735fb_remove),
+};
+
+static int __init st7735fb_init(void)
+{
+ return spi_register_driver(&st7735fb_driver);
+}
+
+static void __exit st7735fb_exit(void)
+{
+ spi_unregister_driver(&st7735fb_driver);
+}
+
+/* ------------------------------------------------------------------------- */
+
+module_init(st7735fb_init);
+module_exit(st7735fb_exit);
+
+MODULE_DESCRIPTION("FB driver for ST7735 display controller");
+MODULE_AUTHOR("Matt Porter <matt@ohporter.com>");
+MODULE_LICENSE("GPL");
diff --git a/drivers/video/st7735fb.h b/drivers/video/st7735fb.h
new file mode 100644
index 0000000..0ce5c11
--- /dev/null
+++ b/drivers/video/st7735fb.h
@@ -0,0 +1,84 @@
+/*
+ * linux/include/video/st7735fb.h -- FB driver for ST7735 LCD controller
+ *
+ * Copyright (C) 2012, Matt Porter
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file COPYING in the main directory of this archive for
+ * more details.
+ */
+
+#define DRVNAME "st7735fb"
+#define WIDTH 128
+#define HEIGHT 160
+#define BPP 16
+#define MAX_PALETTE 16
+
+/* Supported display modules */
+#define ST7735_AF_TFT18_GREEN 0 /* Adafruit SPI TFT 1.8" - green tab */
+#define ST7735_AF_TFT18_RED 1 /* Adafruit SPI TFT 1.8" - red tab */
+
+/* Init script function */
+struct st7735_function {
+ u16 cmd;
+ u16 data;
+};
+
+/* Init script commands */
+enum st7735_cmd {
+ ST7735_START,
+ ST7735_END,
+ ST7735_CMD,
+ ST7735_DATA,
+ ST7735_DELAY
+};
+
+struct st7735fb_par {
+ struct spi_device *spi;
+ struct fb_info *info;
+ struct mutex io_lock;
+ int xoff;
+ int yoff;
+ int rst;
+ int dc;
+ u16 *ssbuf;
+ u8 *buf;
+};
+
+/* ST7735 Commands */
+#define ST7735_NOP 0x0
+#define ST7735_SWRESET 0x01
+#define ST7735_RDDID 0x04
+#define ST7735_RDDST 0x09
+#define ST7735_SLPIN 0x10
+#define ST7735_SLPOUT 0x11
+#define ST7735_PTLON 0x12
+#define ST7735_NORON 0x13
+#define ST7735_INVOFF 0x20
+#define ST7735_INVON 0x21
+#define ST7735_DISPOFF 0x28
+#define ST7735_DISPON 0x29
+#define ST7735_CASET 0x2A
+#define ST7735_RASET 0x2B
+#define ST7735_RAMWR 0x2C
+#define ST7735_RAMRD 0x2E
+#define ST7735_COLMOD 0x3A
+#define ST7735_MADCTL 0x36
+#define ST7735_FRMCTR1 0xB1
+#define ST7735_FRMCTR2 0xB2
+#define ST7735_FRMCTR3 0xB3
+#define ST7735_INVCTR 0xB4
+#define ST7735_DISSET5 0xB6
+#define ST7735_PWCTR1 0xC0
+#define ST7735_PWCTR2 0xC1
+#define ST7735_PWCTR3 0xC2
+#define ST7735_PWCTR4 0xC3
+#define ST7735_PWCTR5 0xC4
+#define ST7735_VMCTR1 0xC5
+#define ST7735_RDID1 0xDA
+#define ST7735_RDID2 0xDB
+#define ST7735_RDID3 0xDC
+#define ST7735_RDID4 0xDD
+#define ST7735_GMCTRP1 0xE0
+#define ST7735_GMCTRN1 0xE1
+#define ST7735_PWCTR6 0xFC
--
1.7.10.4