blob: 9bcd7a0aeed4b035caac6d00a0e2dc195fc4c834 [file] [log] [blame]
Thomas Petazzoni883daf02014-12-31 10:11:28 +01001#include <linux/module.h>
2#include <linux/kernel.h>
3#include <linux/init.h>
4#include <linux/gpio.h>
5#include <linux/spi/spi.h>
6#include <linux/delay.h>
7
8#include "fbtft.h"
9
10#define DRVNAME "fb_ssd1351"
11#define WIDTH 128
12#define HEIGHT 128
13#define GAMMA_NUM 1
14#define GAMMA_LEN 63
15#define DEFAULT_GAMMA "0 2 2 2 2 2 2 2 " \
16 "2 2 2 2 2 2 2 2 " \
17 "2 2 2 2 2 2 2 2 " \
18 "2 2 2 2 2 2 2 2 " \
19 "2 2 2 2 2 2 2 2 " \
20 "2 2 2 2 2 2 2 2 " \
21 "2 2 2 2 2 2 2 2 " \
22 "2 2 2 2 2 2 2" \
23
24static void register_onboard_backlight(struct fbtft_par *par);
25
26static int init_display(struct fbtft_par *par)
27{
28 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
29
30 if (par->pdata
31 && par->pdata->display.backlight == FBTFT_ONBOARD_BACKLIGHT) {
32 /* module uses onboard GPIO for panel power */
33 par->fbtftops.register_backlight = register_onboard_backlight;
34 }
35
36 par->fbtftops.reset(par);
37
38 write_reg(par, 0xfd, 0x12); /* Command Lock */
39 write_reg(par, 0xfd, 0xb1); /* Command Lock */
40 write_reg(par, 0xae); /* Display Off */
41 write_reg(par, 0xb3, 0xf1); /* Front Clock Div */
42 write_reg(par, 0xca, 0x7f); /* Set Mux Ratio */
43 write_reg(par, 0x15, 0x00, 0x7f); /* Set Column Address */
44 write_reg(par, 0x75, 0x00, 0x7f); /* Set Row Address */
45 write_reg(par, 0xa1, 0x00); /* Set Display Start Line */
46 write_reg(par, 0xa2, 0x00); /* Set Display Offset */
47 write_reg(par, 0xb5, 0x00); /* Set GPIO */
48 write_reg(par, 0xab, 0x01); /* Set Function Selection */
49 write_reg(par, 0xb1, 0x32); /* Set Phase Length */
50 write_reg(par, 0xb4, 0xa0, 0xb5, 0x55); /* Set Segment Low Voltage */
51 write_reg(par, 0xbb, 0x17); /* Set Precharge Voltage */
52 write_reg(par, 0xbe, 0x05); /* Set VComH Voltage */
53 write_reg(par, 0xc1, 0xc8, 0x80, 0xc8); /* Set Contrast */
54 write_reg(par, 0xc7, 0x0f); /* Set Master Contrast */
55 write_reg(par, 0xb6, 0x01); /* Set Second Precharge Period */
56 write_reg(par, 0xa6); /* Set Display Mode Reset */
57 write_reg(par, 0xaf); /* Set Sleep Mode Display On */
58
59 return 0;
60}
61
62static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
63{
64 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
65 "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
66
67 write_reg(par, 0x15, xs, xe);
68 write_reg(par, 0x75, ys, ye);
69 write_reg(par, 0x5c);
70}
71
72static int set_var(struct fbtft_par *par)
73{
74 unsigned remap;
aybuke ozdemir54a8de12015-02-19 20:42:43 +020075
Thomas Petazzoni883daf02014-12-31 10:11:28 +010076 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
77
78 if (par->fbtftops.init_display != init_display) {
79 /* don't risk messing up register A0h */
80 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
81 "%s: skipping since custom init_display() is used\n",
82 __func__);
83 return 0;
84 }
85
86 remap = 0x60 | (par->bgr << 2); /* Set Colour Depth */
87
88 switch (par->info->var.rotate) {
89 case 0:
Geert Uytterhoeven153fe942015-03-20 16:21:58 +010090 write_reg(par, 0xA0, remap | 0x00 | 1<<4);
Thomas Petazzoni883daf02014-12-31 10:11:28 +010091 break;
92 case 270:
Geert Uytterhoeven153fe942015-03-20 16:21:58 +010093 write_reg(par, 0xA0, remap | 0x03 | 1<<4);
Thomas Petazzoni883daf02014-12-31 10:11:28 +010094 break;
95 case 180:
Geert Uytterhoeven153fe942015-03-20 16:21:58 +010096 write_reg(par, 0xA0, remap | 0x02);
Thomas Petazzoni883daf02014-12-31 10:11:28 +010097 break;
98 case 90:
Geert Uytterhoeven153fe942015-03-20 16:21:58 +010099 write_reg(par, 0xA0, remap | 0x01);
Thomas Petazzoni883daf02014-12-31 10:11:28 +0100100 break;
101 }
102
103 return 0;
104}
105
106/*
107 Grayscale Lookup Table
108 GS1 - GS63
109 The driver Gamma curve contains the relative values between the entries
110 in the Lookup table.
111
112 From datasheet:
113 8.8 Gray Scale Decoder
114
115 there are total 180 Gamma Settings (Setting 0 to Setting 180)
116 available for the Gray Scale table.
117
118 The gray scale is defined in incremental way, with reference
119 to the length of previous table entry:
120 Setting of GS1 has to be >= 0
121 Setting of GS2 has to be > Setting of GS1 +1
122 Setting of GS3 has to be > Setting of GS2 +1
123 :
124 Setting of GS63 has to be > Setting of GS62 +1
125
126
127*/
128static int set_gamma(struct fbtft_par *par, unsigned long *curves)
129{
130 unsigned long tmp[GAMMA_NUM * GAMMA_LEN];
131 int i, acc = 0;
132
133 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
134
135 for (i = 0; i < 63; i++) {
136 if (i > 0 && curves[i] < 2) {
137 dev_err(par->info->device,
138 "Illegal value in Grayscale Lookup Table at index %d. " \
139 "Must be greater than 1\n", i);
140 return -EINVAL;
141 }
142 acc += curves[i];
143 tmp[i] = acc;
144 if (acc > 180) {
145 dev_err(par->info->device,
146 "Illegal value(s) in Grayscale Lookup Table. " \
147 "At index=%d, the accumulated value has exceeded 180\n", i);
148 return -EINVAL;
149 }
150 }
151
152 write_reg(par, 0xB8,
153 tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7],
154 tmp[8], tmp[9], tmp[10], tmp[11], tmp[12], tmp[13], tmp[14], tmp[15],
155 tmp[16], tmp[17], tmp[18], tmp[19], tmp[20], tmp[21], tmp[22], tmp[23],
156 tmp[24], tmp[25], tmp[26], tmp[27], tmp[28], tmp[29], tmp[30], tmp[31],
157 tmp[32], tmp[33], tmp[34], tmp[35], tmp[36], tmp[37], tmp[38], tmp[39],
158 tmp[40], tmp[41], tmp[42], tmp[43], tmp[44], tmp[45], tmp[46], tmp[47],
159 tmp[48], tmp[49], tmp[50], tmp[51], tmp[52], tmp[53], tmp[54], tmp[55],
160 tmp[56], tmp[57], tmp[58], tmp[59], tmp[60], tmp[61], tmp[62]);
161
162 return 0;
163}
164
165static int blank(struct fbtft_par *par, bool on)
166{
167 fbtft_par_dbg(DEBUG_BLANK, par, "%s(blank=%s)\n",
168 __func__, on ? "true" : "false");
169 if (on)
170 write_reg(par, 0xAE);
171 else
172 write_reg(par, 0xAF);
173 return 0;
174}
175
176
177static struct fbtft_display display = {
178 .regwidth = 8,
179 .width = WIDTH,
180 .height = HEIGHT,
181 .gamma_num = GAMMA_NUM,
182 .gamma_len = GAMMA_LEN,
183 .gamma = DEFAULT_GAMMA,
184 .fbtftops = {
185 .init_display = init_display,
186 .set_addr_win = set_addr_win,
187 .set_var = set_var,
188 .set_gamma = set_gamma,
189 .blank = blank,
190 },
191};
192
193#ifdef CONFIG_FB_BACKLIGHT
194static int update_onboard_backlight(struct backlight_device *bd)
195{
196 struct fbtft_par *par = bl_get_data(bd);
197 bool on;
198
199 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
200 "%s: power=%d, fb_blank=%d\n",
201 __func__, bd->props.power, bd->props.fb_blank);
202
203 on = (bd->props.power == FB_BLANK_UNBLANK)
204 && (bd->props.fb_blank == FB_BLANK_UNBLANK);
205 /* Onboard backlight connected to GPIO0 on SSD1351, GPIO1 unused */
206 write_reg(par, 0xB5, on ? 0x03 : 0x02);
207
208 return 0;
209}
210
211static void register_onboard_backlight(struct fbtft_par *par)
212{
213 struct backlight_device *bd;
214 struct backlight_properties bl_props = { 0, };
215 struct backlight_ops *bl_ops;
216
217 fbtft_par_dbg(DEBUG_BACKLIGHT, par, "%s()\n", __func__);
218
219 bl_ops = devm_kzalloc(par->info->device, sizeof(struct backlight_ops),
220 GFP_KERNEL);
Dilek Uzulmezfad0d052015-03-02 23:28:14 +0200221 if (!bl_ops)
Thomas Petazzoni883daf02014-12-31 10:11:28 +0100222 return;
Thomas Petazzoni883daf02014-12-31 10:11:28 +0100223
224 bl_ops->update_status = update_onboard_backlight;
225 bl_props.type = BACKLIGHT_RAW;
226 bl_props.power = FB_BLANK_POWERDOWN;
227
228 bd = backlight_device_register(dev_driver_string(par->info->device),
229 par->info->device, par, bl_ops, &bl_props);
230 if (IS_ERR(bd)) {
231 dev_err(par->info->device,
232 "cannot register backlight device (%ld)\n",
233 PTR_ERR(bd));
234 return;
235 }
236 par->info->bl_dev = bd;
237
238 if (!par->fbtftops.unregister_backlight)
239 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
240}
241#else
242static void register_onboard_backlight(struct fbtft_par *par) { };
243#endif
244
245
246FBTFT_REGISTER_DRIVER(DRVNAME, "solomon,ssd1351", &display);
247
248MODULE_ALIAS("spi:" DRVNAME);
249MODULE_ALIAS("platform:" DRVNAME);
250MODULE_ALIAS("spi:ssd1351");
251MODULE_ALIAS("platform:ssd1351");
252
253MODULE_DESCRIPTION("SSD1351 OLED Driver");
254MODULE_AUTHOR("James Davies");
255MODULE_LICENSE("GPL");