1/*
2 * FB driver for the HX8347D LCD Controller
3 *
4 * Copyright (C) 2013 Christian Vogelgsang
5 *
6 * Based on driver code found here: https://github.com/watterott/r61505u-Adapter
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/delay.h>
27
28#include "fbtft.h"
29
30#define DRVNAME		"fb_hx8347d"
31#define WIDTH		320
32#define HEIGHT		240
33#define DEFAULT_GAMMA	"0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" \
34			"0 0 0 0 0 0 0 0 0 0 0 0 0 0"
35
36
37static int init_display(struct fbtft_par *par)
38{
39	fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
40
41	par->fbtftops.reset(par);
42
43	/* driving ability */
44	write_reg(par, 0xEA, 0x00);
45	write_reg(par, 0xEB, 0x20);
46	write_reg(par, 0xEC, 0x0C);
47	write_reg(par, 0xED, 0xC4);
48	write_reg(par, 0xE8, 0x40);
49	write_reg(par, 0xE9, 0x38);
50	write_reg(par, 0xF1, 0x01);
51	write_reg(par, 0xF2, 0x10);
52	write_reg(par, 0x27, 0xA3);
53
54	/* power voltage */
55	write_reg(par, 0x1B, 0x1B);
56	write_reg(par, 0x1A, 0x01);
57	write_reg(par, 0x24, 0x2F);
58	write_reg(par, 0x25, 0x57);
59
60	/* VCOM offset */
61	write_reg(par, 0x23, 0x8D); /* for flicker adjust */
62
63	/* power on */
64	write_reg(par, 0x18, 0x36);
65	write_reg(par, 0x19, 0x01); /* start osc */
66	write_reg(par, 0x01, 0x00); /* wakeup */
67	write_reg(par, 0x1F, 0x88);
68	mdelay(5);
69	write_reg(par, 0x1F, 0x80);
70	mdelay(5);
71	write_reg(par, 0x1F, 0x90);
72	mdelay(5);
73	write_reg(par, 0x1F, 0xD0);
74	mdelay(5);
75
76	/* color selection */
77	write_reg(par, 0x17, 0x05); /* 65k */
78
79	/*panel characteristic */
80	write_reg(par, 0x36, 0x00);
81
82	/*display on */
83	write_reg(par, 0x28, 0x38);
84	mdelay(40);
85	write_reg(par, 0x28, 0x3C);
86
87	/* orientation */
88	write_reg(par, 0x16, 0x60 | (par->bgr << 3));
89
90	return 0;
91}
92
93static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
94{
95	fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
96		"%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
97
98	write_reg(par, 0x02, (xs >> 8) & 0xFF);
99	write_reg(par, 0x03, xs & 0xFF);
100	write_reg(par, 0x04, (xe >> 8) & 0xFF);
101	write_reg(par, 0x05, xe & 0xFF);
102	write_reg(par, 0x06, (ys >> 8) & 0xFF);
103	write_reg(par, 0x07, ys & 0xFF);
104	write_reg(par, 0x08, (ye >> 8) & 0xFF);
105	write_reg(par, 0x09, ye & 0xFF);
106	write_reg(par, 0x22);
107}
108
109/*
110  Gamma string format:
111    VRP0 VRP1 VRP2 VRP3 VRP4 VRP5 PRP0 PRP1 PKP0 PKP1 PKP2 PKP3 PKP4 CGM
112    VRN0 VRN1 VRN2 VRN3 VRN4 VRN5 PRN0 PRN1 PKN0 PKN1 PKN2 PKN3 PKN4 CGM
113*/
114#define CURVE(num, idx)  curves[num*par->gamma.num_values + idx]
115static int set_gamma(struct fbtft_par *par, unsigned long *curves)
116{
117	unsigned long mask[] = {
118		0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x7f, 0x7f, 0x1f, 0x1f,
119		0x1f, 0x1f, 0x1f, 0x0f,
120	};
121	int i, j;
122	int acc = 0;
123
124	fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
125
126	/* apply mask */
127	for (i = 0; i < par->gamma.num_curves; i++)
128		for (j = 0; j < par->gamma.num_values; j++) {
129			acc += CURVE(i, j);
130			CURVE(i, j) &= mask[j];
131		}
132
133	if (acc == 0) /* skip if all values are zero */
134		return 0;
135
136	for (i = 0; i < par->gamma.num_curves; i++) {
137		write_reg(par, 0x40 + (i * 0x10), CURVE(i, 0));
138		write_reg(par, 0x41 + (i * 0x10), CURVE(i, 1));
139		write_reg(par, 0x42 + (i * 0x10), CURVE(i, 2));
140		write_reg(par, 0x43 + (i * 0x10), CURVE(i, 3));
141		write_reg(par, 0x44 + (i * 0x10), CURVE(i, 4));
142		write_reg(par, 0x45 + (i * 0x10), CURVE(i, 5));
143		write_reg(par, 0x46 + (i * 0x10), CURVE(i, 6));
144		write_reg(par, 0x47 + (i * 0x10), CURVE(i, 7));
145		write_reg(par, 0x48 + (i * 0x10), CURVE(i, 8));
146		write_reg(par, 0x49 + (i * 0x10), CURVE(i, 9));
147		write_reg(par, 0x4A + (i * 0x10), CURVE(i, 10));
148		write_reg(par, 0x4B + (i * 0x10), CURVE(i, 11));
149		write_reg(par, 0x4C + (i * 0x10), CURVE(i, 12));
150	}
151	write_reg(par, 0x5D, (CURVE(1, 0) << 4) | CURVE(0, 0));
152
153	return 0;
154}
155#undef CURVE
156
157
158static struct fbtft_display display = {
159	.regwidth = 8,
160	.width = WIDTH,
161	.height = HEIGHT,
162	.gamma_num = 2,
163	.gamma_len = 14,
164	.gamma = DEFAULT_GAMMA,
165	.fbtftops = {
166		.init_display = init_display,
167		.set_addr_win = set_addr_win,
168		.set_gamma = set_gamma,
169	},
170};
171FBTFT_REGISTER_DRIVER(DRVNAME, "himax,hx8347d", &display);
172
173MODULE_ALIAS("spi:" DRVNAME);
174MODULE_ALIAS("platform:" DRVNAME);
175MODULE_ALIAS("spi:hx8347d");
176MODULE_ALIAS("platform:hx8347d");
177
178MODULE_DESCRIPTION("FB driver for the HX8347D LCD Controller");
179MODULE_AUTHOR("Christian Vogelgsang");
180MODULE_LICENSE("GPL");
181