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