2 * FB driver for the UltraChip UC1611 LCD controller
4 * The display is 4-bit grayscale (16 shades) 240x160.
6 * Copyright (C) 2015 Henri Chain
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.
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.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/gpio.h>
23 #include <linux/spi/spi.h>
24 #include <linux/delay.h>
28 #define DRVNAME "fb_uc1611"
35 * LCD voltage is a combination of ratio, gain, pot and temp
37 * V_LCD = V_BIAS * ratio
38 * V_LCD = (C_V0 + C_PM × pot) * (1 + (T - 25) * temp)
39 * C_V0 and C_PM depend on ratio and gain
40 * T is ambient temperature
43 /* BR -> actual ratio: 0-3 -> 5, 10, 11, 13 */
44 static unsigned ratio = 2;
45 module_param(ratio, uint, 0);
46 MODULE_PARM_DESC(ratio, "BR[1:0] Bias voltage ratio: 0-3 (default: 2)");
48 static unsigned gain = 3;
49 module_param(gain, uint, 0);
50 MODULE_PARM_DESC(gain, "GN[1:0] Bias voltage gain: 0-3 (default: 3)");
52 static unsigned pot = 16;
53 module_param(pot, uint, 0);
54 MODULE_PARM_DESC(pot, "PM[6:0] Bias voltage pot.: 0-63 (default: 16)");
56 /* TC -> % compensation per deg C: 0-3 -> -.05, -.10, -.015, -.20 */
58 module_param(temp, uint, 0);
59 MODULE_PARM_DESC(temp, "TC[1:0] Temperature compensation: 0-3 (default: 0)");
61 /* PC[1:0] -> LCD capacitance: 0-3 -> <20nF, 20-28 nF, 29-40 nF, 40-56 nF */
62 static unsigned load = 1;
63 module_param(load, uint, 0);
64 MODULE_PARM_DESC(load, "PC[1:0] Panel Loading: 0-3 (default: 1)");
66 /* PC[3:2] -> V_LCD: 0, 1, 3 -> ext., int. with ratio = 5, int. standard */
67 static unsigned pump = 3;
68 module_param(pump, uint, 0);
69 MODULE_PARM_DESC(pump, "PC[3:2] Pump control: 0,1,3 (default: 3)");
71 static int init_display(struct fbtft_par *par)
75 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
77 /* Set CS active high */
78 par->spi->mode |= SPI_CS_HIGH;
79 ret = spi_setup(par->spi);
81 dev_err(par->info->device, "Could not set SPI_CS_HIGH\n");
85 /* Reset controller */
89 write_reg(par, 0xE8 | (ratio & 0x03));
91 /* Set bias gain and potentiometer */
93 write_reg(par, (gain & 0x03) << 6 | (pot & 0x3F));
95 /* Set temperature compensation */
96 write_reg(par, 0x24 | (temp & 0x03));
98 /* Set panel loading */
99 write_reg(par, 0x28 | (load & 0x03));
101 /* Set pump control */
102 write_reg(par, 0x2C | (pump & 0x03));
104 /* Set inverse display */
105 write_reg(par, 0xA6 | (0x01 & 0x01));
107 /* Set 4-bit grayscale mode */
108 write_reg(par, 0xD0 | (0x02 & 0x03));
110 /* Set Display enable */
111 write_reg(par, 0xA8 | 0x07);
116 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
118 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
119 "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n",
120 __func__, xs, ys, xe, ye);
122 switch (par->info->var.rotate) {
125 /* Set column address */
126 write_reg(par, ys & 0x0F);
127 write_reg(par, 0x10 | (ys >> 4));
129 /* Set page address (divide xs by 2) (not used by driver) */
130 write_reg(par, 0x60 | ((xs >> 1) & 0x0F));
131 write_reg(par, 0x70 | (xs >> 5));
134 /* Set column address (not used by driver) */
135 write_reg(par, xs & 0x0F);
136 write_reg(par, 0x10 | (xs >> 4));
138 /* Set page address (divide ys by 2) */
139 write_reg(par, 0x60 | ((ys >> 1) & 0x0F));
140 write_reg(par, 0x70 | (ys >> 5));
145 static int blank(struct fbtft_par *par, bool on)
147 fbtft_par_dbg(DEBUG_BLANK, par, "%s(blank=%s)\n",
148 __func__, on ? "true" : "false");
151 write_reg(par, 0xA8 | 0x00);
153 write_reg(par, 0xA8 | 0x07);
157 static int set_var(struct fbtft_par *par)
159 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
161 /* par->info->fix.visual = FB_VISUAL_PSEUDOCOLOR; */
162 par->info->var.grayscale = 1;
163 par->info->var.red.offset = 0;
164 par->info->var.red.length = 8;
165 par->info->var.green.offset = 0;
166 par->info->var.green.length = 8;
167 par->info->var.blue.offset = 0;
168 par->info->var.blue.length = 8;
169 par->info->var.transp.offset = 0;
170 par->info->var.transp.length = 0;
172 switch (par->info->var.rotate) {
174 /* Set RAM address control */
176 | (0x0 & 0x1) << 2 /* Increment positively */
177 | (0x1 & 0x1) << 1 /* Increment page first */
178 | (0x1 & 0x1)); /* Wrap around (default) */
180 /* Set LCD mapping */
182 | (0x0 & 0x1) << 2 /* Mirror Y OFF */
183 | (0x0 & 0x1) << 1 /* Mirror X OFF */
184 | (0x0 & 0x1)); /* MS nibble last (default) */
187 /* Set RAM address control */
189 | (0x0 & 0x1) << 2 /* Increment positively */
190 | (0x0 & 0x1) << 1 /* Increment column first */
191 | (0x1 & 0x1)); /* Wrap around (default) */
193 /* Set LCD mapping */
195 | (0x1 & 0x1) << 2 /* Mirror Y ON */
196 | (0x0 & 0x1) << 1 /* Mirror X OFF */
197 | (0x0 & 0x1)); /* MS nibble last (default) */
200 /* Set RAM address control */
202 | (0x0 & 0x1) << 2 /* Increment positively */
203 | (0x1 & 0x1) << 1 /* Increment page first */
204 | (0x1 & 0x1)); /* Wrap around (default) */
206 /* Set LCD mapping */
208 | (0x1 & 0x1) << 2 /* Mirror Y ON */
209 | (0x1 & 0x1) << 1 /* Mirror X ON */
210 | (0x0 & 0x1)); /* MS nibble last (default) */
213 /* Set RAM address control */
215 | (0x0 & 0x1) << 2 /* Increment positively */
216 | (0x0 & 0x1) << 1 /* Increment column first */
217 | (0x1 & 0x1)); /* Wrap around (default) */
219 /* Set LCD mapping */
221 | (0x0 & 0x1) << 2 /* Mirror Y OFF */
222 | (0x1 & 0x1) << 1 /* Mirror X ON */
223 | (0x0 & 0x1)); /* MS nibble last (default) */
230 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
232 u8 *vmem8 = (u8 *)(par->info->screen_base);
233 u8 *buf8 = (u8 *)(par->txbuf.buf);
234 u16 *buf16 = (u16 *)(par->txbuf.buf);
235 int line_length = par->info->fix.line_length;
236 int y_start = (offset / line_length);
237 int y_end = (offset + len - 1) / line_length;
241 fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s()\n", __func__);
243 switch (par->pdata->display.buswidth) {
245 switch (par->info->var.rotate) {
248 i = y_start * line_length;
249 for (y = y_start; y <= y_end; y++) {
250 for (x = 0; x < line_length; x += 2) {
251 *buf8 = vmem8[i] >> 4;
252 *buf8 |= vmem8[i + 1] & 0xF0;
259 /* Must be even because pages are two lines */
261 i = y_start * line_length;
262 for (y = y_start; y <= y_end; y += 2) {
263 for (x = 0; x < line_length; x++) {
264 *buf8 = vmem8[i] >> 4;
265 *buf8 |= vmem8[i + line_length] & 0xF0;
273 gpio_set_value(par->gpio.dc, 1);
276 ret = par->fbtftops.write(par, par->txbuf.buf, len / 2);
279 switch (par->info->var.rotate) {
282 i = y_start * line_length;
283 for (y = y_start; y <= y_end; y++) {
284 for (x = 0; x < line_length; x += 2) {
286 *buf16 |= vmem8[i] >> 4;
287 *buf16 |= vmem8[i + 1] & 0xF0;
294 /* Must be even because pages are two lines */
296 i = y_start * line_length;
297 for (y = y_start; y <= y_end; y += 2) {
298 for (x = 0; x < line_length; x++) {
300 *buf16 |= vmem8[i] >> 4;
301 *buf16 |= vmem8[i + line_length] & 0xF0;
311 ret = par->fbtftops.write(par, par->txbuf.buf, len);
314 dev_err(par->info->device, "unsupported buswidth %d\n",
315 par->pdata->display.buswidth);
319 dev_err(par->info->device, "write failed and returned: %d\n",
325 static struct fbtft_display display = {
333 .write_vmem = write_vmem,
334 .init_display = init_display,
335 .set_addr_win = set_addr_win,
341 FBTFT_REGISTER_DRIVER(DRVNAME, "ultrachip,uc1611", &display);
343 MODULE_ALIAS("spi:" DRVNAME);
344 MODULE_ALIAS("platform:" DRVNAME);
345 MODULE_ALIAS("spi:uc1611");
346 MODULE_ALIAS("platform:uc1611");
348 MODULE_DESCRIPTION("FB driver for the UC1611 LCD controller");
349 MODULE_AUTHOR("Henri Chain");
350 MODULE_LICENSE("GPL");