1/*
2 * FB driver for the SSD1289 LCD Controller
3 *
4 * Copyright (C) 2013 Noralf Tronnes
5 *
6 * Init sequence taken from ITDB02_Graph16.cpp - (C)2010-2011 Henning Karlsen
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/gpio.h>
27
28#include "fbtft.h"
29
30#define DRVNAME		"fb_ssd1289"
31#define WIDTH		240
32#define HEIGHT		320
33#define DEFAULT_GAMMA	"02 03 2 5 7 7 4 2 4 2\n" \
34			"02 03 2 5 7 5 4 2 4 2"
35
36static unsigned reg11 = 0x6040;
37module_param(reg11, uint, 0);
38MODULE_PARM_DESC(reg11, "Register 11h value");
39
40
41static int init_display(struct fbtft_par *par)
42{
43	fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
44
45	par->fbtftops.reset(par);
46
47	if (par->gpio.cs != -1)
48		gpio_set_value(par->gpio.cs, 0);  /* Activate chip */
49
50	write_reg(par, 0x00, 0x0001);
51	write_reg(par, 0x03, 0xA8A4);
52	write_reg(par, 0x0C, 0x0000);
53	write_reg(par, 0x0D, 0x080C);
54	write_reg(par, 0x0E, 0x2B00);
55	write_reg(par, 0x1E, 0x00B7);
56	write_reg(par, 0x01,
57		(1 << 13) | (par->bgr << 11) | (1 << 9) | (HEIGHT - 1));
58	write_reg(par, 0x02, 0x0600);
59	write_reg(par, 0x10, 0x0000);
60	write_reg(par, 0x05, 0x0000);
61	write_reg(par, 0x06, 0x0000);
62	write_reg(par, 0x16, 0xEF1C);
63	write_reg(par, 0x17, 0x0003);
64	write_reg(par, 0x07, 0x0233);
65	write_reg(par, 0x0B, 0x0000);
66	write_reg(par, 0x0F, 0x0000);
67	write_reg(par, 0x41, 0x0000);
68	write_reg(par, 0x42, 0x0000);
69	write_reg(par, 0x48, 0x0000);
70	write_reg(par, 0x49, 0x013F);
71	write_reg(par, 0x4A, 0x0000);
72	write_reg(par, 0x4B, 0x0000);
73	write_reg(par, 0x44, 0xEF00);
74	write_reg(par, 0x45, 0x0000);
75	write_reg(par, 0x46, 0x013F);
76	write_reg(par, 0x23, 0x0000);
77	write_reg(par, 0x24, 0x0000);
78	write_reg(par, 0x25, 0x8000);
79	write_reg(par, 0x4f, 0x0000);
80	write_reg(par, 0x4e, 0x0000);
81	write_reg(par, 0x22);
82	return 0;
83}
84
85static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
86{
87	fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
88		"%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
89
90	switch (par->info->var.rotate) {
91	/* R4Eh - Set GDDRAM X address counter */
92	/* R4Fh - Set GDDRAM Y address counter */
93	case 0:
94		write_reg(par, 0x4e, xs);
95		write_reg(par, 0x4f, ys);
96		break;
97	case 180:
98		write_reg(par, 0x4e, par->info->var.xres - 1 - xs);
99		write_reg(par, 0x4f, par->info->var.yres - 1 - ys);
100		break;
101	case 270:
102		write_reg(par, 0x4e, par->info->var.yres - 1 - ys);
103		write_reg(par, 0x4f, xs);
104		break;
105	case 90:
106		write_reg(par, 0x4e, ys);
107		write_reg(par, 0x4f, par->info->var.xres - 1 - xs);
108		break;
109	}
110
111	/* R22h - RAM data write */
112	write_reg(par, 0x22);
113}
114
115static int set_var(struct fbtft_par *par)
116{
117	fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
118
119	if (par->fbtftops.init_display != init_display) {
120		/* don't risk messing up register 11h */
121		fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
122			"%s: skipping since custom init_display() is used\n",
123			__func__);
124		return 0;
125	}
126
127	switch (par->info->var.rotate) {
128	case 0:
129		write_reg(par, 0x11, reg11 | 0x30);
130		break;
131	case 270:
132		write_reg(par, 0x11, reg11 | 0x28);
133		break;
134	case 180:
135		write_reg(par, 0x11, reg11 | 0x00);
136		break;
137	case 90:
138		write_reg(par, 0x11, reg11 | 0x18);
139		break;
140	}
141
142	return 0;
143}
144
145/*
146  Gamma string format:
147    VRP0 VRP1 PRP0 PRP1 PKP0 PKP1 PKP2 PKP3 PKP4 PKP5
148    VRN0 VRN1 PRN0 PRN1 PKN0 PKN1 PKN2 PKN3 PKN4 PKN5
149*/
150#define CURVE(num, idx)  curves[num*par->gamma.num_values + idx]
151static int set_gamma(struct fbtft_par *par, unsigned long *curves)
152{
153	unsigned long mask[] = {
154		0x1f, 0x1f, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
155		0x1f, 0x1f, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
156	};
157	int i, j;
158
159	fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
160
161	/* apply mask */
162	for (i = 0; i < 2; i++)
163		for (j = 0; j < 10; j++)
164			CURVE(i, j) &= mask[i*par->gamma.num_values + j];
165
166	write_reg(par, 0x0030, CURVE(0, 5) << 8 | CURVE(0, 4));
167	write_reg(par, 0x0031, CURVE(0, 7) << 8 | CURVE(0, 6));
168	write_reg(par, 0x0032, CURVE(0, 9) << 8 | CURVE(0, 8));
169	write_reg(par, 0x0033, CURVE(0, 3) << 8 | CURVE(0, 2));
170	write_reg(par, 0x0034, CURVE(1, 5) << 8 | CURVE(1, 4));
171	write_reg(par, 0x0035, CURVE(1, 7) << 8 | CURVE(1, 6));
172	write_reg(par, 0x0036, CURVE(1, 9) << 8 | CURVE(1, 8));
173	write_reg(par, 0x0037, CURVE(1, 3) << 8 | CURVE(1, 2));
174	write_reg(par, 0x003A, CURVE(0, 1) << 8 | CURVE(0, 0));
175	write_reg(par, 0x003B, CURVE(1, 1) << 8 | CURVE(1, 0));
176
177	return 0;
178}
179#undef CURVE
180
181
182static struct fbtft_display display = {
183	.regwidth = 16,
184	.width = WIDTH,
185	.height = HEIGHT,
186	.gamma_num = 2,
187	.gamma_len = 10,
188	.gamma = DEFAULT_GAMMA,
189	.fbtftops = {
190		.init_display = init_display,
191		.set_addr_win = set_addr_win,
192		.set_var = set_var,
193		.set_gamma = set_gamma,
194	},
195};
196FBTFT_REGISTER_DRIVER(DRVNAME, "solomon,ssd1289", &display);
197
198MODULE_ALIAS("spi:" DRVNAME);
199MODULE_ALIAS("platform:" DRVNAME);
200MODULE_ALIAS("spi:ssd1289");
201MODULE_ALIAS("platform:ssd1289");
202
203MODULE_DESCRIPTION("FB driver for the SSD1289 LCD Controller");
204MODULE_AUTHOR("Noralf Tronnes");
205MODULE_LICENSE("GPL");
206