1/*
2 * FB driver for the ILI9340 LCD Controller
3 *
4 * Copyright (C) 2013 Noralf Tronnes
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/gpio.h>
21#include <linux/delay.h>
22
23#include "fbtft.h"
24
25#define DRVNAME		"fb_ili9340"
26#define WIDTH		240
27#define HEIGHT		320
28
29/* Init sequence taken from: Arduino Library for the Adafruit 2.2" display */
30static int init_display(struct fbtft_par *par)
31{
32	par->fbtftops.reset(par);
33
34	write_reg(par, 0xEF, 0x03, 0x80, 0x02);
35	write_reg(par, 0xCF, 0x00, 0XC1, 0X30);
36	write_reg(par, 0xED, 0x64, 0x03, 0X12, 0X81);
37	write_reg(par, 0xE8, 0x85, 0x00, 0x78);
38	write_reg(par, 0xCB, 0x39, 0x2C, 0x00, 0x34, 0x02);
39	write_reg(par, 0xF7, 0x20);
40	write_reg(par, 0xEA, 0x00, 0x00);
41
42	/* Power Control 1 */
43	write_reg(par, 0xC0, 0x23);
44
45	/* Power Control 2 */
46	write_reg(par, 0xC1, 0x10);
47
48	/* VCOM Control 1 */
49	write_reg(par, 0xC5, 0x3e, 0x28);
50
51	/* VCOM Control 2 */
52	write_reg(par, 0xC7, 0x86);
53
54	/* COLMOD: Pixel Format Set */
55	/* 16 bits/pixel */
56	write_reg(par, 0x3A, 0x55);
57
58	/* Frame Rate Control */
59	/* Division ratio = fosc, Frame Rate = 79Hz */
60	write_reg(par, 0xB1, 0x00, 0x18);
61
62	/* Display Function Control */
63	write_reg(par, 0xB6, 0x08, 0x82, 0x27);
64
65	/* Gamma Function Disable */
66	write_reg(par, 0xF2, 0x00);
67
68	/* Gamma curve selected  */
69	write_reg(par, 0x26, 0x01);
70
71	/* Positive Gamma Correction */
72	write_reg(par, 0xE0,
73		0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1,
74		0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00);
75
76	/* Negative Gamma Correction */
77	write_reg(par, 0xE1,
78		0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1,
79		0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F);
80
81	/* Sleep OUT */
82	write_reg(par, 0x11);
83
84	mdelay(120);
85
86	/* Display ON */
87	write_reg(par, 0x29);
88
89	return 0;
90}
91
92static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
93{
94	/* Column address */
95	write_reg(par, 0x2A, xs >> 8, xs & 0xFF, xe >> 8, xe & 0xFF);
96
97	/* Row address */
98	write_reg(par, 0x2B, ys >> 8, ys & 0xFF, ye >> 8, ye & 0xFF);
99
100	/* Memory write */
101	write_reg(par, 0x2C);
102}
103
104#define ILI9340_MADCTL_MV  0x20
105#define ILI9340_MADCTL_MX  0x40
106#define ILI9340_MADCTL_MY  0x80
107static int set_var(struct fbtft_par *par)
108{
109	u8 val;
110
111	switch (par->info->var.rotate) {
112	case 270:
113		val = ILI9340_MADCTL_MV;
114		break;
115	case 180:
116		val = ILI9340_MADCTL_MY;
117		break;
118	case 90:
119		val = ILI9340_MADCTL_MV | ILI9340_MADCTL_MY | ILI9340_MADCTL_MX;
120		break;
121	default:
122		val = ILI9340_MADCTL_MX;
123		break;
124	}
125	/* Memory Access Control  */
126	write_reg(par, 0x36, val | (par->bgr << 3));
127
128	return 0;
129}
130
131static struct fbtft_display display = {
132	.regwidth = 8,
133	.width = WIDTH,
134	.height = HEIGHT,
135	.fbtftops = {
136		.init_display = init_display,
137		.set_addr_win = set_addr_win,
138		.set_var = set_var,
139	},
140};
141
142FBTFT_REGISTER_DRIVER(DRVNAME, "ilitek,ili9340", &display);
143
144MODULE_ALIAS("spi:" DRVNAME);
145MODULE_ALIAS("platform:" DRVNAME);
146MODULE_ALIAS("spi:ili9340");
147MODULE_ALIAS("platform:ili9340");
148
149MODULE_DESCRIPTION("FB driver for the ILI9340 LCD Controller");
150MODULE_AUTHOR("Noralf Tronnes");
151MODULE_LICENSE("GPL");
152