blob: 1267b93c03bd0765dc171bcb3a31284f6e3a190b [file] [log] [blame]
Maxime Riparda2ed00d2012-12-07 12:30:38 -08001/*
Masanari Iidaa895d572013-04-09 02:06:50 +09002 * Driver for the Solomon SSD1307 OLED controller
Maxime Riparda2ed00d2012-12-07 12:30:38 -08003 *
4 * Copyright 2012 Free Electrons
5 *
6 * Licensed under the GPLv2 or later.
7 */
8
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +02009#include <linux/backlight.h>
Olliver Schinagl48846ec2015-09-08 21:19:50 +020010#include <linux/delay.h>
Maxime Riparda2ed00d2012-12-07 12:30:38 -080011#include <linux/fb.h>
Olliver Schinagl48846ec2015-09-08 21:19:50 +020012#include <linux/i2c.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
Maxime Riparda2ed00d2012-12-07 12:30:38 -080015#include <linux/of_device.h>
16#include <linux/of_gpio.h>
17#include <linux/pwm.h>
Olliver Schinagl48846ec2015-09-08 21:19:50 +020018#include <linux/uaccess.h>
Maxime Riparda2ed00d2012-12-07 12:30:38 -080019
Maxime Riparda2ed00d2012-12-07 12:30:38 -080020#define SSD1307FB_DATA 0x40
21#define SSD1307FB_COMMAND 0x80
22
Maxime Ripard301bc062013-04-22 12:02:25 +020023#define SSD1307FB_SET_ADDRESS_MODE 0x20
24#define SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL (0x00)
25#define SSD1307FB_SET_ADDRESS_MODE_VERTICAL (0x01)
26#define SSD1307FB_SET_ADDRESS_MODE_PAGE (0x02)
27#define SSD1307FB_SET_COL_RANGE 0x21
28#define SSD1307FB_SET_PAGE_RANGE 0x22
Maxime Riparda2ed00d2012-12-07 12:30:38 -080029#define SSD1307FB_CONTRAST 0x81
Maxime Ripardbbc79082013-04-22 11:55:54 +020030#define SSD1307FB_CHARGE_PUMP 0x8d
Maxime Riparda2ed00d2012-12-07 12:30:38 -080031#define SSD1307FB_SEG_REMAP_ON 0xa1
32#define SSD1307FB_DISPLAY_OFF 0xae
Maxime Ripardbbc79082013-04-22 11:55:54 +020033#define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8
Maxime Riparda2ed00d2012-12-07 12:30:38 -080034#define SSD1307FB_DISPLAY_ON 0xaf
35#define SSD1307FB_START_PAGE_ADDRESS 0xb0
Maxime Ripardbbc79082013-04-22 11:55:54 +020036#define SSD1307FB_SET_DISPLAY_OFFSET 0xd3
37#define SSD1307FB_SET_CLOCK_FREQ 0xd5
38#define SSD1307FB_SET_PRECHARGE_PERIOD 0xd9
39#define SSD1307FB_SET_COM_PINS_CONFIG 0xda
40#define SSD1307FB_SET_VCOMH 0xdb
41
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +020042#define MAX_CONTRAST 255
43
Thomas Niederprüm3277e0b2015-03-31 20:27:13 +020044#define REFRESHRATE 1
45
46static u_int refreshrate = REFRESHRATE;
47module_param(refreshrate, uint, 0);
48
Maxime Ripardbbc79082013-04-22 11:55:54 +020049struct ssd1307fb_par;
50
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020051struct ssd1307fb_deviceinfo {
52 u32 default_vcomh;
53 u32 default_dclk_div;
54 u32 default_dclk_frq;
55 int need_pwm;
56 int need_chargepump;
Maxime Ripardbbc79082013-04-22 11:55:54 +020057};
Maxime Riparda2ed00d2012-12-07 12:30:38 -080058
59struct ssd1307fb_par {
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020060 u32 com_invdir;
61 u32 com_lrremap;
62 u32 com_offset;
63 u32 com_seq;
64 u32 contrast;
65 u32 dclk_div;
66 u32 dclk_frq;
LABBE Corentin60169c32016-08-16 11:27:17 +020067 const struct ssd1307fb_deviceinfo *device_info;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080068 struct i2c_client *client;
Maxime Ripardbbc79082013-04-22 11:55:54 +020069 u32 height;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080070 struct fb_info *info;
Maxime Ripardbbc79082013-04-22 11:55:54 +020071 u32 page_offset;
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020072 u32 prechargep1;
73 u32 prechargep2;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080074 struct pwm_device *pwm;
75 u32 pwm_period;
76 int reset;
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020077 u32 seg_remap;
78 u32 vcomh;
Maxime Ripardbbc79082013-04-22 11:55:54 +020079 u32 width;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080080};
81
Maxime Ripard9f7714d2013-04-22 12:02:23 +020082struct ssd1307fb_array {
83 u8 type;
84 u8 data[0];
85};
86
Julia Lawallca9384c2016-09-11 17:17:20 +020087static const struct fb_fix_screeninfo ssd1307fb_fix = {
Maxime Riparda2ed00d2012-12-07 12:30:38 -080088 .id = "Solomon SSD1307",
89 .type = FB_TYPE_PACKED_PIXELS,
90 .visual = FB_VISUAL_MONO10,
91 .xpanstep = 0,
92 .ypanstep = 0,
93 .ywrapstep = 0,
Maxime Riparda2ed00d2012-12-07 12:30:38 -080094 .accel = FB_ACCEL_NONE,
95};
96
Julia Lawallca9384c2016-09-11 17:17:20 +020097static const struct fb_var_screeninfo ssd1307fb_var = {
Maxime Riparda2ed00d2012-12-07 12:30:38 -080098 .bits_per_pixel = 1,
99};
100
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200101static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800102{
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200103 struct ssd1307fb_array *array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800104
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200105 array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL);
106 if (!array)
107 return NULL;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800108
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200109 array->type = type;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800110
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200111 return array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800112}
113
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200114static int ssd1307fb_write_array(struct i2c_client *client,
115 struct ssd1307fb_array *array, u32 len)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800116{
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200117 int ret;
118
119 len += sizeof(struct ssd1307fb_array);
120
121 ret = i2c_master_send(client, (u8 *)array, len);
122 if (ret != len) {
123 dev_err(&client->dev, "Couldn't send I2C command.\n");
124 return ret;
125 }
126
127 return 0;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800128}
129
130static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
131{
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200132 struct ssd1307fb_array *array;
133 int ret;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800134
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200135 array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND);
136 if (!array)
137 return -ENOMEM;
138
139 array->data[0] = cmd;
140
141 ret = ssd1307fb_write_array(client, array, 1);
142 kfree(array);
143
144 return ret;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800145}
146
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800147static void ssd1307fb_update_display(struct ssd1307fb_par *par)
148{
Maxime Ripard301bc062013-04-22 12:02:25 +0200149 struct ssd1307fb_array *array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800150 u8 *vmem = par->info->screen_base;
151 int i, j, k;
152
Maxime Ripard301bc062013-04-22 12:02:25 +0200153 array = ssd1307fb_alloc_array(par->width * par->height / 8,
154 SSD1307FB_DATA);
155 if (!array)
156 return;
157
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800158 /*
159 * The screen is divided in pages, each having a height of 8
160 * pixels, and the width of the screen. When sending a byte of
161 * data to the controller, it gives the 8 bits for the current
162 * column. I.e, the first byte are the 8 bits of the first
163 * column, then the 8 bits for the second column, etc.
164 *
165 *
166 * Representation of the screen, assuming it is 5 bits
167 * wide. Each letter-number combination is a bit that controls
168 * one pixel.
169 *
170 * A0 A1 A2 A3 A4
171 * B0 B1 B2 B3 B4
172 * C0 C1 C2 C3 C4
173 * D0 D1 D2 D3 D4
174 * E0 E1 E2 E3 E4
175 * F0 F1 F2 F3 F4
176 * G0 G1 G2 G3 G4
177 * H0 H1 H2 H3 H4
178 *
179 * If you want to update this screen, you need to send 5 bytes:
180 * (1) A0 B0 C0 D0 E0 F0 G0 H0
181 * (2) A1 B1 C1 D1 E1 F1 G1 H1
182 * (3) A2 B2 C2 D2 E2 F2 G2 H2
183 * (4) A3 B3 C3 D3 E3 F3 G3 H3
184 * (5) A4 B4 C4 D4 E4 F4 G4 H4
185 */
186
Maxime Ripardbbc79082013-04-22 11:55:54 +0200187 for (i = 0; i < (par->height / 8); i++) {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200188 for (j = 0; j < par->width; j++) {
Maxime Ripard301bc062013-04-22 12:02:25 +0200189 u32 array_idx = i * par->width + j;
190 array->data[array_idx] = 0;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800191 for (k = 0; k < 8; k++) {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200192 u32 page_length = par->width * i;
193 u32 index = page_length + (par->width * k + j) / 8;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800194 u8 byte = *(vmem + index);
Maxime Ripard552f0cc2013-01-11 14:31:43 -0800195 u8 bit = byte & (1 << (j % 8));
196 bit = bit >> (j % 8);
Maxime Ripard301bc062013-04-22 12:02:25 +0200197 array->data[array_idx] |= bit << k;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800198 }
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800199 }
200 }
Maxime Ripard301bc062013-04-22 12:02:25 +0200201
202 ssd1307fb_write_array(par->client, array, par->width * par->height / 8);
203 kfree(array);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800204}
205
206
207static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
208 size_t count, loff_t *ppos)
209{
210 struct ssd1307fb_par *par = info->par;
211 unsigned long total_size;
212 unsigned long p = *ppos;
213 u8 __iomem *dst;
214
215 total_size = info->fix.smem_len;
216
217 if (p > total_size)
218 return -EINVAL;
219
220 if (count + p > total_size)
221 count = total_size - p;
222
223 if (!count)
224 return -EINVAL;
225
226 dst = (void __force *) (info->screen_base + p);
227
228 if (copy_from_user(dst, buf, count))
229 return -EFAULT;
230
231 ssd1307fb_update_display(par);
232
233 *ppos += count;
234
235 return count;
236}
237
Thomas Niederprüm550e7682015-03-31 20:27:16 +0200238static int ssd1307fb_blank(int blank_mode, struct fb_info *info)
239{
240 struct ssd1307fb_par *par = info->par;
241
242 if (blank_mode != FB_BLANK_UNBLANK)
243 return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
244 else
245 return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
246}
247
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800248static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
249{
250 struct ssd1307fb_par *par = info->par;
251 sys_fillrect(info, rect);
252 ssd1307fb_update_display(par);
253}
254
255static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
256{
257 struct ssd1307fb_par *par = info->par;
258 sys_copyarea(info, area);
259 ssd1307fb_update_display(par);
260}
261
262static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
263{
264 struct ssd1307fb_par *par = info->par;
265 sys_imageblit(info, image);
266 ssd1307fb_update_display(par);
267}
268
269static struct fb_ops ssd1307fb_ops = {
270 .owner = THIS_MODULE,
271 .fb_read = fb_sys_read,
272 .fb_write = ssd1307fb_write,
Thomas Niederprüm550e7682015-03-31 20:27:16 +0200273 .fb_blank = ssd1307fb_blank,
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800274 .fb_fillrect = ssd1307fb_fillrect,
275 .fb_copyarea = ssd1307fb_copyarea,
276 .fb_imageblit = ssd1307fb_imageblit,
277};
278
279static void ssd1307fb_deferred_io(struct fb_info *info,
280 struct list_head *pagelist)
281{
282 ssd1307fb_update_display(info->par);
283}
284
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200285static int ssd1307fb_init(struct ssd1307fb_par *par)
Maxime Ripardbbc79082013-04-22 11:55:54 +0200286{
287 int ret;
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200288 u32 precharge, dclk, com_invdir, compins;
Boris Brezillon717c18f2016-04-14 21:17:28 +0200289 struct pwm_args pargs;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200290
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200291 if (par->device_info->need_pwm) {
292 par->pwm = pwm_get(&par->client->dev, NULL);
293 if (IS_ERR(par->pwm)) {
294 dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
295 return PTR_ERR(par->pwm);
296 }
Maxime Ripardbbc79082013-04-22 11:55:54 +0200297
Boris Brezillon717c18f2016-04-14 21:17:28 +0200298 /*
299 * FIXME: pwm_apply_args() should be removed when switching to
300 * the atomic PWM API.
301 */
302 pwm_apply_args(par->pwm);
303
304 pwm_get_args(par->pwm, &pargs);
305
306 par->pwm_period = pargs.period;
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200307 /* Enable the PWM */
308 pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
309 pwm_enable(par->pwm);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200310
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200311 dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
312 par->pwm->pwm, par->pwm_period);
313 };
Maxime Ripardbbc79082013-04-22 11:55:54 +0200314
315 /* Set initial contrast */
316 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000317 if (ret < 0)
318 return ret;
319
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200320 ret = ssd1307fb_write_cmd(par->client, par->contrast);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200321 if (ret < 0)
322 return ret;
323
324 /* Set segment re-map */
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200325 if (par->seg_remap) {
326 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
327 if (ret < 0)
328 return ret;
329 };
330
331 /* Set COM direction */
332 com_invdir = 0xc0 | (par->com_invdir & 0x1) << 3;
333 ret = ssd1307fb_write_cmd(par->client, com_invdir);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200334 if (ret < 0)
335 return ret;
336
337 /* Set multiplex ratio value */
338 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000339 if (ret < 0)
340 return ret;
341
342 ret = ssd1307fb_write_cmd(par->client, par->height - 1);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200343 if (ret < 0)
344 return ret;
345
346 /* set display offset value */
347 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000348 if (ret < 0)
349 return ret;
350
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200351 ret = ssd1307fb_write_cmd(par->client, par->com_offset);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200352 if (ret < 0)
353 return ret;
354
355 /* Set clock frequency */
356 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000357 if (ret < 0)
358 return ret;
359
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200360 dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4;
361 ret = ssd1307fb_write_cmd(par->client, dclk);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200362 if (ret < 0)
363 return ret;
364
365 /* Set precharge period in number of ticks from the internal clock */
366 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000367 if (ret < 0)
368 return ret;
369
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200370 precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4;
371 ret = ssd1307fb_write_cmd(par->client, precharge);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200372 if (ret < 0)
373 return ret;
374
375 /* Set COM pins configuration */
376 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000377 if (ret < 0)
378 return ret;
379
Thomas Niederprüm5e479322015-05-25 21:29:21 +0200380 compins = 0x02 | !(par->com_seq & 0x1) << 4
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200381 | (par->com_lrremap & 0x1) << 5;
382 ret = ssd1307fb_write_cmd(par->client, compins);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200383 if (ret < 0)
384 return ret;
385
386 /* Set VCOMH */
387 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000388 if (ret < 0)
389 return ret;
390
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200391 ret = ssd1307fb_write_cmd(par->client, par->vcomh);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200392 if (ret < 0)
393 return ret;
394
395 /* Turn on the DC-DC Charge Pump */
396 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000397 if (ret < 0)
398 return ret;
399
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200400 ret = ssd1307fb_write_cmd(par->client,
Julian Scheel80403b22016-03-24 22:14:23 +0100401 BIT(4) | (par->device_info->need_chargepump ? BIT(2) : 0));
Maxime Ripardbbc79082013-04-22 11:55:54 +0200402 if (ret < 0)
403 return ret;
404
Maxime Ripard301bc062013-04-22 12:02:25 +0200405 /* Switch to horizontal addressing mode */
406 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000407 if (ret < 0)
408 return ret;
409
410 ret = ssd1307fb_write_cmd(par->client,
411 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL);
Maxime Ripard301bc062013-04-22 12:02:25 +0200412 if (ret < 0)
413 return ret;
414
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200415 /* Set column range */
Maxime Ripard301bc062013-04-22 12:02:25 +0200416 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COL_RANGE);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000417 if (ret < 0)
418 return ret;
419
420 ret = ssd1307fb_write_cmd(par->client, 0x0);
421 if (ret < 0)
422 return ret;
423
424 ret = ssd1307fb_write_cmd(par->client, par->width - 1);
Maxime Ripard301bc062013-04-22 12:02:25 +0200425 if (ret < 0)
426 return ret;
427
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200428 /* Set page range */
Maxime Ripard301bc062013-04-22 12:02:25 +0200429 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PAGE_RANGE);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000430 if (ret < 0)
431 return ret;
432
Marko Kohtalae68cb862019-06-18 10:41:08 +0300433 ret = ssd1307fb_write_cmd(par->client, par->page_offset);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000434 if (ret < 0)
435 return ret;
436
437 ret = ssd1307fb_write_cmd(par->client,
438 par->page_offset + (par->height / 8) - 1);
Maxime Ripard301bc062013-04-22 12:02:25 +0200439 if (ret < 0)
440 return ret;
441
Maxime Ripardbbc79082013-04-22 11:55:54 +0200442 /* Turn on the display */
443 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
444 if (ret < 0)
445 return ret;
446
447 return 0;
448}
449
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +0200450static int ssd1307fb_update_bl(struct backlight_device *bdev)
451{
452 struct ssd1307fb_par *par = bl_get_data(bdev);
453 int ret;
454 int brightness = bdev->props.brightness;
455
456 par->contrast = brightness;
457
458 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
459 if (ret < 0)
460 return ret;
461 ret = ssd1307fb_write_cmd(par->client, par->contrast);
462 if (ret < 0)
463 return ret;
464 return 0;
465}
466
467static int ssd1307fb_get_brightness(struct backlight_device *bdev)
468{
469 struct ssd1307fb_par *par = bl_get_data(bdev);
470
471 return par->contrast;
472}
473
474static int ssd1307fb_check_fb(struct backlight_device *bdev,
475 struct fb_info *info)
476{
477 return (info->bl_dev == bdev);
478}
479
480static const struct backlight_ops ssd1307fb_bl_ops = {
481 .options = BL_CORE_SUSPENDRESUME,
482 .update_status = ssd1307fb_update_bl,
483 .get_brightness = ssd1307fb_get_brightness,
484 .check_fb = ssd1307fb_check_fb,
485};
486
Thomas Niederprüm5f2d36b2015-03-31 20:27:12 +0200487static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = {
488 .default_vcomh = 0x34,
489 .default_dclk_div = 1,
490 .default_dclk_frq = 7,
491};
492
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200493static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = {
494 .default_vcomh = 0x20,
495 .default_dclk_div = 1,
496 .default_dclk_frq = 8,
497 .need_chargepump = 1,
498};
499
500static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = {
501 .default_vcomh = 0x20,
502 .default_dclk_div = 2,
503 .default_dclk_frq = 12,
504 .need_pwm = 1,
Maxime Ripardbbc79082013-04-22 11:55:54 +0200505};
506
Olliver Schinagl3ac58d52015-09-08 21:19:51 +0200507static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo = {
508 .default_vcomh = 0x34,
509 .default_dclk_div = 1,
510 .default_dclk_frq = 10,
511};
512
Maxime Ripardbbc79082013-04-22 11:55:54 +0200513static const struct of_device_id ssd1307fb_of_match[] = {
514 {
Thomas Niederprüm5f2d36b2015-03-31 20:27:12 +0200515 .compatible = "solomon,ssd1305fb-i2c",
516 .data = (void *)&ssd1307fb_ssd1305_deviceinfo,
517 },
518 {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200519 .compatible = "solomon,ssd1306fb-i2c",
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200520 .data = (void *)&ssd1307fb_ssd1306_deviceinfo,
Maxime Ripardbbc79082013-04-22 11:55:54 +0200521 },
522 {
523 .compatible = "solomon,ssd1307fb-i2c",
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200524 .data = (void *)&ssd1307fb_ssd1307_deviceinfo,
Maxime Ripardbbc79082013-04-22 11:55:54 +0200525 },
Olliver Schinagl3ac58d52015-09-08 21:19:51 +0200526 {
527 .compatible = "solomon,ssd1309fb-i2c",
528 .data = (void *)&ssd1307fb_ssd1309_deviceinfo,
529 },
Maxime Ripardbbc79082013-04-22 11:55:54 +0200530 {},
531};
532MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
533
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800534static int ssd1307fb_probe(struct i2c_client *client,
535 const struct i2c_device_id *id)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800536{
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +0200537 struct backlight_device *bl;
538 char bl_name[12];
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800539 struct fb_info *info;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200540 struct device_node *node = client->dev.of_node;
Thomas Niederprüm3277e0b2015-03-31 20:27:13 +0200541 struct fb_deferred_io *ssd1307fb_defio;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200542 u32 vmem_size;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800543 struct ssd1307fb_par *par;
544 u8 *vmem;
545 int ret;
546
Maxime Ripardbbc79082013-04-22 11:55:54 +0200547 if (!node) {
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800548 dev_err(&client->dev, "No device tree data found!\n");
549 return -EINVAL;
550 }
551
552 info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
553 if (!info) {
554 dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
555 return -ENOMEM;
556 }
557
Maxime Ripardbbc79082013-04-22 11:55:54 +0200558 par = info->par;
559 par->info = info;
560 par->client = client;
561
LABBE Corentind8ed9e82016-08-16 11:27:18 +0200562 par->device_info = of_device_get_match_data(&client->dev);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200563
564 par->reset = of_get_named_gpio(client->dev.of_node,
565 "reset-gpios", 0);
566 if (!gpio_is_valid(par->reset)) {
567 ret = -EINVAL;
568 goto fb_alloc_error;
569 }
570
571 if (of_property_read_u32(node, "solomon,width", &par->width))
572 par->width = 96;
573
574 if (of_property_read_u32(node, "solomon,height", &par->height))
Lad, Prabhakar9561def2015-01-08 08:17:58 +0000575 par->height = 16;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200576
577 if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
578 par->page_offset = 1;
579
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200580 if (of_property_read_u32(node, "solomon,com-offset", &par->com_offset))
581 par->com_offset = 0;
582
583 if (of_property_read_u32(node, "solomon,prechargep1", &par->prechargep1))
584 par->prechargep1 = 2;
585
586 if (of_property_read_u32(node, "solomon,prechargep2", &par->prechargep2))
587 par->prechargep2 = 2;
588
589 par->seg_remap = !of_property_read_bool(node, "solomon,segment-no-remap");
590 par->com_seq = of_property_read_bool(node, "solomon,com-seq");
591 par->com_lrremap = of_property_read_bool(node, "solomon,com-lrremap");
592 par->com_invdir = of_property_read_bool(node, "solomon,com-invdir");
593
594 par->contrast = 127;
595 par->vcomh = par->device_info->default_vcomh;
596
597 /* Setup display timing */
598 par->dclk_div = par->device_info->default_dclk_div;
599 par->dclk_frq = par->device_info->default_dclk_frq;
600
Maxime Ripardbbc79082013-04-22 11:55:54 +0200601 vmem_size = par->width * par->height / 8;
602
Thomas Niederprümfacd94b2015-03-31 20:27:08 +0200603 vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
604 get_order(vmem_size));
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800605 if (!vmem) {
606 dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
607 ret = -ENOMEM;
608 goto fb_alloc_error;
609 }
610
Thomas Niederprüm3277e0b2015-03-31 20:27:13 +0200611 ssd1307fb_defio = devm_kzalloc(&client->dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
612 if (!ssd1307fb_defio) {
613 dev_err(&client->dev, "Couldn't allocate deferred io.\n");
614 ret = -ENOMEM;
615 goto fb_alloc_error;
616 }
617
618 ssd1307fb_defio->delay = HZ / refreshrate;
619 ssd1307fb_defio->deferred_io = ssd1307fb_deferred_io;
620
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800621 info->fbops = &ssd1307fb_ops;
622 info->fix = ssd1307fb_fix;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200623 info->fix.line_length = par->width / 8;
Thomas Niederprüm3277e0b2015-03-31 20:27:13 +0200624 info->fbdefio = ssd1307fb_defio;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800625
626 info->var = ssd1307fb_var;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200627 info->var.xres = par->width;
628 info->var.xres_virtual = par->width;
629 info->var.yres = par->height;
630 info->var.yres_virtual = par->height;
631
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800632 info->var.red.length = 1;
633 info->var.red.offset = 0;
634 info->var.green.length = 1;
635 info->var.green.offset = 0;
636 info->var.blue.length = 1;
637 info->var.blue.offset = 0;
638
639 info->screen_base = (u8 __force __iomem *)vmem;
Thomas Niederprüm258c0ea2015-03-31 20:27:07 +0200640 info->fix.smem_start = __pa(vmem);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800641 info->fix.smem_len = vmem_size;
642
643 fb_deferred_io_init(info);
644
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800645 ret = devm_gpio_request_one(&client->dev, par->reset,
646 GPIOF_OUT_INIT_HIGH,
647 "oled-reset");
648 if (ret) {
649 dev_err(&client->dev,
650 "failed to request gpio %d: %d\n",
651 par->reset, ret);
652 goto reset_oled_error;
653 }
654
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800655 i2c_set_clientdata(client, info);
656
657 /* Reset the screen */
658 gpio_set_value(par->reset, 0);
659 udelay(4);
660 gpio_set_value(par->reset, 1);
661 udelay(4);
662
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200663 ret = ssd1307fb_init(par);
664 if (ret)
665 goto reset_oled_error;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800666
Maxime Ripardbbc79082013-04-22 11:55:54 +0200667 ret = register_framebuffer(info);
668 if (ret) {
669 dev_err(&client->dev, "Couldn't register the framebuffer\n");
670 goto panel_init_error;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800671 }
672
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +0200673 snprintf(bl_name, sizeof(bl_name), "ssd1307fb%d", info->node);
674 bl = backlight_device_register(bl_name, &client->dev, par,
675 &ssd1307fb_bl_ops, NULL);
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +0200676 if (IS_ERR(bl)) {
Julia Lawallc4e67742015-08-23 02:11:15 +0200677 ret = PTR_ERR(bl);
678 dev_err(&client->dev, "unable to register backlight device: %d\n",
679 ret);
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +0200680 goto bl_init_error;
681 }
Dan Carpenterc2b00022015-05-23 20:32:35 +0300682
683 bl->props.brightness = par->contrast;
684 bl->props.max_brightness = MAX_CONTRAST;
685 info->bl_dev = bl;
686
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800687 dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
688
689 return 0;
690
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +0200691bl_init_error:
692 unregister_framebuffer(info);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200693panel_init_error:
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200694 if (par->device_info->need_pwm) {
695 pwm_disable(par->pwm);
696 pwm_put(par->pwm);
697 };
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800698reset_oled_error:
699 fb_deferred_io_cleanup(info);
700fb_alloc_error:
701 framebuffer_release(info);
702 return ret;
703}
704
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800705static int ssd1307fb_remove(struct i2c_client *client)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800706{
707 struct fb_info *info = i2c_get_clientdata(client);
708 struct ssd1307fb_par *par = info->par;
709
Thomas Niederprüm13bad592015-03-31 20:27:14 +0200710 ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
711
Thomas Niederprüm6ed5e2d2015-03-31 20:27:15 +0200712 backlight_device_unregister(info->bl_dev);
713
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800714 unregister_framebuffer(info);
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200715 if (par->device_info->need_pwm) {
716 pwm_disable(par->pwm);
717 pwm_put(par->pwm);
718 };
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800719 fb_deferred_io_cleanup(info);
Thomas Niederprümfacd94b2015-03-31 20:27:08 +0200720 __free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len));
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800721 framebuffer_release(info);
722
723 return 0;
724}
725
726static const struct i2c_device_id ssd1307fb_i2c_id[] = {
Thomas Niederprüm5f2d36b2015-03-31 20:27:12 +0200727 { "ssd1305fb", 0 },
Maxime Ripardbbc79082013-04-22 11:55:54 +0200728 { "ssd1306fb", 0 },
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800729 { "ssd1307fb", 0 },
Olliver Schinagl3ac58d52015-09-08 21:19:51 +0200730 { "ssd1309fb", 0 },
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800731 { }
732};
733MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
734
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800735static struct i2c_driver ssd1307fb_driver = {
736 .probe = ssd1307fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800737 .remove = ssd1307fb_remove,
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800738 .id_table = ssd1307fb_i2c_id,
739 .driver = {
740 .name = "ssd1307fb",
Sachin Kamatd71f8cd2013-09-30 14:15:31 +0530741 .of_match_table = ssd1307fb_of_match,
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800742 },
743};
744
745module_i2c_driver(ssd1307fb_driver);
746
Masanari Iidaa895d572013-04-09 02:06:50 +0900747MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800748MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
749MODULE_LICENSE("GPL");