xref: /openbmc/linux/drivers/video/fbdev/ssd1307fb.c (revision aaa746ad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for the Solomon SSD1307 OLED controller
4  *
5  * Copyright 2012 Free Electrons
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/fb.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/property.h>
16 #include <linux/pwm.h>
17 #include <linux/uaccess.h>
18 #include <linux/regulator/consumer.h>
19 
20 #define SSD1307FB_DATA			0x40
21 #define SSD1307FB_COMMAND		0x80
22 
23 #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
29 #define SSD1307FB_CONTRAST		0x81
30 #define SSD1307FB_SET_LOOKUP_TABLE	0x91
31 #define	SSD1307FB_CHARGE_PUMP		0x8d
32 #define SSD1307FB_SEG_REMAP_ON		0xa1
33 #define SSD1307FB_DISPLAY_OFF		0xae
34 #define SSD1307FB_SET_MULTIPLEX_RATIO	0xa8
35 #define SSD1307FB_DISPLAY_ON		0xaf
36 #define SSD1307FB_START_PAGE_ADDRESS	0xb0
37 #define SSD1307FB_SET_DISPLAY_OFFSET	0xd3
38 #define	SSD1307FB_SET_CLOCK_FREQ	0xd5
39 #define	SSD1307FB_SET_AREA_COLOR_MODE	0xd8
40 #define	SSD1307FB_SET_PRECHARGE_PERIOD	0xd9
41 #define	SSD1307FB_SET_COM_PINS_CONFIG	0xda
42 #define	SSD1307FB_SET_VCOMH		0xdb
43 
44 #define MAX_CONTRAST 255
45 
46 #define REFRESHRATE 1
47 
48 static u_int refreshrate = REFRESHRATE;
49 module_param(refreshrate, uint, 0);
50 
51 struct ssd1307fb_deviceinfo {
52 	u32 default_vcomh;
53 	u32 default_dclk_div;
54 	u32 default_dclk_frq;
55 	int need_pwm;
56 	int need_chargepump;
57 };
58 
59 struct ssd1307fb_par {
60 	unsigned area_color_enable : 1;
61 	unsigned com_invdir : 1;
62 	unsigned com_lrremap : 1;
63 	unsigned com_seq : 1;
64 	unsigned lookup_table_set : 1;
65 	unsigned low_power : 1;
66 	unsigned seg_remap : 1;
67 	u32 com_offset;
68 	u32 contrast;
69 	u32 dclk_div;
70 	u32 dclk_frq;
71 	const struct ssd1307fb_deviceinfo *device_info;
72 	struct i2c_client *client;
73 	u32 height;
74 	struct fb_info *info;
75 	u8 lookup_table[4];
76 	u32 page_offset;
77 	u32 col_offset;
78 	u32 prechargep1;
79 	u32 prechargep2;
80 	struct pwm_device *pwm;
81 	struct gpio_desc *reset;
82 	struct regulator *vbat_reg;
83 	u32 vcomh;
84 	u32 width;
85 	/* Cached address ranges */
86 	u8 col_start;
87 	u8 col_end;
88 	u8 page_start;
89 	u8 page_end;
90 };
91 
92 struct ssd1307fb_array {
93 	u8	type;
94 	u8	data[];
95 };
96 
97 static const struct fb_fix_screeninfo ssd1307fb_fix = {
98 	.id		= "Solomon SSD1307",
99 	.type		= FB_TYPE_PACKED_PIXELS,
100 	.visual		= FB_VISUAL_MONO10,
101 	.xpanstep	= 0,
102 	.ypanstep	= 0,
103 	.ywrapstep	= 0,
104 	.accel		= FB_ACCEL_NONE,
105 };
106 
107 static const struct fb_var_screeninfo ssd1307fb_var = {
108 	.bits_per_pixel	= 1,
109 	.red = { .length = 1 },
110 	.green = { .length = 1 },
111 	.blue = { .length = 1 },
112 };
113 
114 static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type)
115 {
116 	struct ssd1307fb_array *array;
117 
118 	array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL);
119 	if (!array)
120 		return NULL;
121 
122 	array->type = type;
123 
124 	return array;
125 }
126 
127 static int ssd1307fb_write_array(struct i2c_client *client,
128 				 struct ssd1307fb_array *array, u32 len)
129 {
130 	int ret;
131 
132 	len += sizeof(struct ssd1307fb_array);
133 
134 	ret = i2c_master_send(client, (u8 *)array, len);
135 	if (ret != len) {
136 		dev_err(&client->dev, "Couldn't send I2C command.\n");
137 		return ret;
138 	}
139 
140 	return 0;
141 }
142 
143 static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
144 {
145 	struct ssd1307fb_array *array;
146 	int ret;
147 
148 	array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND);
149 	if (!array)
150 		return -ENOMEM;
151 
152 	array->data[0] = cmd;
153 
154 	ret = ssd1307fb_write_array(client, array, 1);
155 	kfree(array);
156 
157 	return ret;
158 }
159 
160 static int ssd1307fb_set_col_range(struct ssd1307fb_par *par, u8 col_start,
161 				   u8 cols)
162 {
163 	u8 col_end = col_start + cols - 1;
164 	int ret;
165 
166 	if (col_start == par->col_start && col_end == par->col_end)
167 		return 0;
168 
169 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COL_RANGE);
170 	if (ret < 0)
171 		return ret;
172 
173 	ret = ssd1307fb_write_cmd(par->client, col_start);
174 	if (ret < 0)
175 		return ret;
176 
177 	ret = ssd1307fb_write_cmd(par->client, col_end);
178 	if (ret < 0)
179 		return ret;
180 
181 	par->col_start = col_start;
182 	par->col_end = col_end;
183 	return 0;
184 }
185 
186 static int ssd1307fb_set_page_range(struct ssd1307fb_par *par, u8 page_start,
187 				    u8 pages)
188 {
189 	u8 page_end = page_start + pages - 1;
190 	int ret;
191 
192 	if (page_start == par->page_start && page_end == par->page_end)
193 		return 0;
194 
195 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PAGE_RANGE);
196 	if (ret < 0)
197 		return ret;
198 
199 	ret = ssd1307fb_write_cmd(par->client, page_start);
200 	if (ret < 0)
201 		return ret;
202 
203 	ret = ssd1307fb_write_cmd(par->client, page_end);
204 	if (ret < 0)
205 		return ret;
206 
207 	par->page_start = page_start;
208 	par->page_end = page_end;
209 	return 0;
210 }
211 
212 static int ssd1307fb_update_rect(struct ssd1307fb_par *par, unsigned int x,
213 				 unsigned int y, unsigned int width,
214 				 unsigned int height)
215 {
216 	struct ssd1307fb_array *array;
217 	u8 *vmem = par->info->screen_buffer;
218 	unsigned int line_length = par->info->fix.line_length;
219 	unsigned int pages = DIV_ROUND_UP(y % 8 + height, 8);
220 	u32 array_idx = 0;
221 	int ret, i, j, k;
222 
223 	array = ssd1307fb_alloc_array(width * pages, SSD1307FB_DATA);
224 	if (!array)
225 		return -ENOMEM;
226 
227 	/*
228 	 * The screen is divided in pages, each having a height of 8
229 	 * pixels, and the width of the screen. When sending a byte of
230 	 * data to the controller, it gives the 8 bits for the current
231 	 * column. I.e, the first byte are the 8 bits of the first
232 	 * column, then the 8 bits for the second column, etc.
233 	 *
234 	 *
235 	 * Representation of the screen, assuming it is 5 bits
236 	 * wide. Each letter-number combination is a bit that controls
237 	 * one pixel.
238 	 *
239 	 * A0 A1 A2 A3 A4
240 	 * B0 B1 B2 B3 B4
241 	 * C0 C1 C2 C3 C4
242 	 * D0 D1 D2 D3 D4
243 	 * E0 E1 E2 E3 E4
244 	 * F0 F1 F2 F3 F4
245 	 * G0 G1 G2 G3 G4
246 	 * H0 H1 H2 H3 H4
247 	 *
248 	 * If you want to update this screen, you need to send 5 bytes:
249 	 *  (1) A0 B0 C0 D0 E0 F0 G0 H0
250 	 *  (2) A1 B1 C1 D1 E1 F1 G1 H1
251 	 *  (3) A2 B2 C2 D2 E2 F2 G2 H2
252 	 *  (4) A3 B3 C3 D3 E3 F3 G3 H3
253 	 *  (5) A4 B4 C4 D4 E4 F4 G4 H4
254 	 */
255 
256 	ret = ssd1307fb_set_col_range(par, par->col_offset + x, width);
257 	if (ret < 0)
258 		goto out_free;
259 
260 	ret = ssd1307fb_set_page_range(par, par->page_offset + y / 8, pages);
261 	if (ret < 0)
262 		goto out_free;
263 
264 	for (i = y / 8; i < y / 8 + pages; i++) {
265 		int m = 8;
266 
267 		/* Last page may be partial */
268 		if (8 * (i + 1) > par->height)
269 			m = par->height % 8;
270 		for (j = x; j < x + width; j++) {
271 			u8 data = 0;
272 
273 			for (k = 0; k < m; k++) {
274 				u8 byte = vmem[(8 * i + k) * line_length +
275 					       j / 8];
276 				u8 bit = (byte >> (j % 8)) & 1;
277 				data |= bit << k;
278 			}
279 			array->data[array_idx++] = data;
280 		}
281 	}
282 
283 	ret = ssd1307fb_write_array(par->client, array, width * pages);
284 
285 out_free:
286 	kfree(array);
287 	return ret;
288 }
289 
290 static int ssd1307fb_update_display(struct ssd1307fb_par *par)
291 {
292 	return ssd1307fb_update_rect(par, 0, 0, par->width, par->height);
293 }
294 
295 static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
296 		size_t count, loff_t *ppos)
297 {
298 	struct ssd1307fb_par *par = info->par;
299 	unsigned long total_size;
300 	unsigned long p = *ppos;
301 	void *dst;
302 	int ret;
303 
304 	total_size = info->fix.smem_len;
305 
306 	if (p > total_size)
307 		return -EINVAL;
308 
309 	if (count + p > total_size)
310 		count = total_size - p;
311 
312 	if (!count)
313 		return -EINVAL;
314 
315 	dst = info->screen_buffer + p;
316 
317 	if (copy_from_user(dst, buf, count))
318 		return -EFAULT;
319 
320 	ret = ssd1307fb_update_display(par);
321 	if (ret < 0)
322 		return ret;
323 
324 	*ppos += count;
325 
326 	return count;
327 }
328 
329 static int ssd1307fb_blank(int blank_mode, struct fb_info *info)
330 {
331 	struct ssd1307fb_par *par = info->par;
332 
333 	if (blank_mode != FB_BLANK_UNBLANK)
334 		return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
335 	else
336 		return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
337 }
338 
339 static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
340 {
341 	struct ssd1307fb_par *par = info->par;
342 	sys_fillrect(info, rect);
343 	ssd1307fb_update_rect(par, rect->dx, rect->dy, rect->width,
344 			      rect->height);
345 }
346 
347 static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
348 {
349 	struct ssd1307fb_par *par = info->par;
350 	sys_copyarea(info, area);
351 	ssd1307fb_update_rect(par, area->dx, area->dy, area->width,
352 			      area->height);
353 }
354 
355 static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
356 {
357 	struct ssd1307fb_par *par = info->par;
358 	sys_imageblit(info, image);
359 	ssd1307fb_update_rect(par, image->dx, image->dy, image->width,
360 			      image->height);
361 }
362 
363 static const struct fb_ops ssd1307fb_ops = {
364 	.owner		= THIS_MODULE,
365 	.fb_read	= fb_sys_read,
366 	.fb_write	= ssd1307fb_write,
367 	.fb_blank	= ssd1307fb_blank,
368 	.fb_fillrect	= ssd1307fb_fillrect,
369 	.fb_copyarea	= ssd1307fb_copyarea,
370 	.fb_imageblit	= ssd1307fb_imageblit,
371 	.fb_mmap	= fb_deferred_io_mmap,
372 };
373 
374 static void ssd1307fb_deferred_io(struct fb_info *info, struct list_head *pagereflist)
375 {
376 	ssd1307fb_update_display(info->par);
377 }
378 
379 static int ssd1307fb_init(struct ssd1307fb_par *par)
380 {
381 	struct pwm_state pwmstate;
382 	int ret;
383 	u32 precharge, dclk, com_invdir, compins;
384 
385 	if (par->device_info->need_pwm) {
386 		par->pwm = pwm_get(&par->client->dev, NULL);
387 		if (IS_ERR(par->pwm)) {
388 			dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
389 			return PTR_ERR(par->pwm);
390 		}
391 
392 		pwm_init_state(par->pwm, &pwmstate);
393 		pwm_set_relative_duty_cycle(&pwmstate, 50, 100);
394 		pwm_apply_state(par->pwm, &pwmstate);
395 
396 		/* Enable the PWM */
397 		pwm_enable(par->pwm);
398 
399 		dev_dbg(&par->client->dev, "Using PWM%d with a %lluns period.\n",
400 			par->pwm->pwm, pwm_get_period(par->pwm));
401 	}
402 
403 	/* Set initial contrast */
404 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
405 	if (ret < 0)
406 		return ret;
407 
408 	ret = ssd1307fb_write_cmd(par->client, par->contrast);
409 	if (ret < 0)
410 		return ret;
411 
412 	/* Set segment re-map */
413 	if (par->seg_remap) {
414 		ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
415 		if (ret < 0)
416 			return ret;
417 	}
418 
419 	/* Set COM direction */
420 	com_invdir = 0xc0 | par->com_invdir << 3;
421 	ret = ssd1307fb_write_cmd(par->client,  com_invdir);
422 	if (ret < 0)
423 		return ret;
424 
425 	/* Set multiplex ratio value */
426 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
427 	if (ret < 0)
428 		return ret;
429 
430 	ret = ssd1307fb_write_cmd(par->client, par->height - 1);
431 	if (ret < 0)
432 		return ret;
433 
434 	/* set display offset value */
435 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
436 	if (ret < 0)
437 		return ret;
438 
439 	ret = ssd1307fb_write_cmd(par->client, par->com_offset);
440 	if (ret < 0)
441 		return ret;
442 
443 	/* Set clock frequency */
444 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
445 	if (ret < 0)
446 		return ret;
447 
448 	dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4;
449 	ret = ssd1307fb_write_cmd(par->client, dclk);
450 	if (ret < 0)
451 		return ret;
452 
453 	/* Set Area Color Mode ON/OFF & Low Power Display Mode */
454 	if (par->area_color_enable || par->low_power) {
455 		u32 mode;
456 
457 		ret = ssd1307fb_write_cmd(par->client,
458 					  SSD1307FB_SET_AREA_COLOR_MODE);
459 		if (ret < 0)
460 			return ret;
461 
462 		mode = (par->area_color_enable ? 0x30 : 0) |
463 			(par->low_power ? 5 : 0);
464 		ret = ssd1307fb_write_cmd(par->client, mode);
465 		if (ret < 0)
466 			return ret;
467 	}
468 
469 	/* Set precharge period in number of ticks from the internal clock */
470 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
471 	if (ret < 0)
472 		return ret;
473 
474 	precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4;
475 	ret = ssd1307fb_write_cmd(par->client, precharge);
476 	if (ret < 0)
477 		return ret;
478 
479 	/* Set COM pins configuration */
480 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
481 	if (ret < 0)
482 		return ret;
483 
484 	compins = 0x02 | !par->com_seq << 4 | par->com_lrremap << 5;
485 	ret = ssd1307fb_write_cmd(par->client, compins);
486 	if (ret < 0)
487 		return ret;
488 
489 	/* Set VCOMH */
490 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
491 	if (ret < 0)
492 		return ret;
493 
494 	ret = ssd1307fb_write_cmd(par->client, par->vcomh);
495 	if (ret < 0)
496 		return ret;
497 
498 	/* Turn on the DC-DC Charge Pump */
499 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
500 	if (ret < 0)
501 		return ret;
502 
503 	ret = ssd1307fb_write_cmd(par->client,
504 		BIT(4) | (par->device_info->need_chargepump ? BIT(2) : 0));
505 	if (ret < 0)
506 		return ret;
507 
508 	/* Set lookup table */
509 	if (par->lookup_table_set) {
510 		int i;
511 
512 		ret = ssd1307fb_write_cmd(par->client,
513 					  SSD1307FB_SET_LOOKUP_TABLE);
514 		if (ret < 0)
515 			return ret;
516 
517 		for (i = 0; i < ARRAY_SIZE(par->lookup_table); ++i) {
518 			u8 val = par->lookup_table[i];
519 
520 			if (val < 31 || val > 63)
521 				dev_warn(&par->client->dev,
522 					 "lookup table index %d value out of range 31 <= %d <= 63\n",
523 					 i, val);
524 			ret = ssd1307fb_write_cmd(par->client, val);
525 			if (ret < 0)
526 				return ret;
527 		}
528 	}
529 
530 	/* Switch to horizontal addressing mode */
531 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE);
532 	if (ret < 0)
533 		return ret;
534 
535 	ret = ssd1307fb_write_cmd(par->client,
536 				  SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL);
537 	if (ret < 0)
538 		return ret;
539 
540 	/* Clear the screen */
541 	ret = ssd1307fb_update_display(par);
542 	if (ret < 0)
543 		return ret;
544 
545 	/* Turn on the display */
546 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
547 	if (ret < 0)
548 		return ret;
549 
550 	return 0;
551 }
552 
553 static int ssd1307fb_update_bl(struct backlight_device *bdev)
554 {
555 	struct ssd1307fb_par *par = bl_get_data(bdev);
556 	int ret;
557 	int brightness = bdev->props.brightness;
558 
559 	par->contrast = brightness;
560 
561 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
562 	if (ret < 0)
563 		return ret;
564 	ret = ssd1307fb_write_cmd(par->client, par->contrast);
565 	if (ret < 0)
566 		return ret;
567 	return 0;
568 }
569 
570 static int ssd1307fb_get_brightness(struct backlight_device *bdev)
571 {
572 	struct ssd1307fb_par *par = bl_get_data(bdev);
573 
574 	return par->contrast;
575 }
576 
577 static int ssd1307fb_check_fb(struct backlight_device *bdev,
578 				   struct fb_info *info)
579 {
580 	return (info->bl_dev == bdev);
581 }
582 
583 static const struct backlight_ops ssd1307fb_bl_ops = {
584 	.options	= BL_CORE_SUSPENDRESUME,
585 	.update_status	= ssd1307fb_update_bl,
586 	.get_brightness	= ssd1307fb_get_brightness,
587 	.check_fb	= ssd1307fb_check_fb,
588 };
589 
590 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = {
591 	.default_vcomh = 0x34,
592 	.default_dclk_div = 1,
593 	.default_dclk_frq = 7,
594 };
595 
596 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = {
597 	.default_vcomh = 0x20,
598 	.default_dclk_div = 1,
599 	.default_dclk_frq = 8,
600 	.need_chargepump = 1,
601 };
602 
603 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = {
604 	.default_vcomh = 0x20,
605 	.default_dclk_div = 2,
606 	.default_dclk_frq = 12,
607 	.need_pwm = 1,
608 };
609 
610 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo = {
611 	.default_vcomh = 0x34,
612 	.default_dclk_div = 1,
613 	.default_dclk_frq = 10,
614 };
615 
616 static const struct of_device_id ssd1307fb_of_match[] = {
617 	{
618 		.compatible = "solomon,ssd1305fb-i2c",
619 		.data = (void *)&ssd1307fb_ssd1305_deviceinfo,
620 	},
621 	{
622 		.compatible = "solomon,ssd1306fb-i2c",
623 		.data = (void *)&ssd1307fb_ssd1306_deviceinfo,
624 	},
625 	{
626 		.compatible = "solomon,ssd1307fb-i2c",
627 		.data = (void *)&ssd1307fb_ssd1307_deviceinfo,
628 	},
629 	{
630 		.compatible = "solomon,ssd1309fb-i2c",
631 		.data = (void *)&ssd1307fb_ssd1309_deviceinfo,
632 	},
633 	{},
634 };
635 MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
636 
637 static int ssd1307fb_probe(struct i2c_client *client)
638 {
639 	struct device *dev = &client->dev;
640 	struct backlight_device *bl;
641 	char bl_name[12];
642 	struct fb_info *info;
643 	struct fb_deferred_io *ssd1307fb_defio;
644 	u32 vmem_size;
645 	struct ssd1307fb_par *par;
646 	void *vmem;
647 	int ret;
648 
649 	info = framebuffer_alloc(sizeof(struct ssd1307fb_par), dev);
650 	if (!info)
651 		return -ENOMEM;
652 
653 	par = info->par;
654 	par->info = info;
655 	par->client = client;
656 
657 	par->device_info = device_get_match_data(dev);
658 
659 	par->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
660 	if (IS_ERR(par->reset)) {
661 		ret = dev_err_probe(dev, PTR_ERR(par->reset),
662 				    "failed to get reset gpio\n");
663 		goto fb_alloc_error;
664 	}
665 
666 	par->vbat_reg = devm_regulator_get_optional(dev, "vbat");
667 	if (IS_ERR(par->vbat_reg)) {
668 		ret = PTR_ERR(par->vbat_reg);
669 		if (ret == -ENODEV) {
670 			par->vbat_reg = NULL;
671 		} else {
672 			dev_err_probe(dev, ret, "failed to get VBAT regulator\n");
673 			goto fb_alloc_error;
674 		}
675 	}
676 
677 	if (device_property_read_u32(dev, "solomon,width", &par->width))
678 		par->width = 96;
679 
680 	if (device_property_read_u32(dev, "solomon,height", &par->height))
681 		par->height = 16;
682 
683 	if (device_property_read_u32(dev, "solomon,page-offset", &par->page_offset))
684 		par->page_offset = 1;
685 
686 	if (device_property_read_u32(dev, "solomon,col-offset", &par->col_offset))
687 		par->col_offset = 0;
688 
689 	if (device_property_read_u32(dev, "solomon,com-offset", &par->com_offset))
690 		par->com_offset = 0;
691 
692 	if (device_property_read_u32(dev, "solomon,prechargep1", &par->prechargep1))
693 		par->prechargep1 = 2;
694 
695 	if (device_property_read_u32(dev, "solomon,prechargep2", &par->prechargep2))
696 		par->prechargep2 = 2;
697 
698 	if (!device_property_read_u8_array(dev, "solomon,lookup-table",
699 					   par->lookup_table,
700 					   ARRAY_SIZE(par->lookup_table)))
701 		par->lookup_table_set = 1;
702 
703 	par->seg_remap = !device_property_read_bool(dev, "solomon,segment-no-remap");
704 	par->com_seq = device_property_read_bool(dev, "solomon,com-seq");
705 	par->com_lrremap = device_property_read_bool(dev, "solomon,com-lrremap");
706 	par->com_invdir = device_property_read_bool(dev, "solomon,com-invdir");
707 	par->area_color_enable =
708 		device_property_read_bool(dev, "solomon,area-color-enable");
709 	par->low_power = device_property_read_bool(dev, "solomon,low-power");
710 
711 	par->contrast = 127;
712 	par->vcomh = par->device_info->default_vcomh;
713 
714 	/* Setup display timing */
715 	if (device_property_read_u32(dev, "solomon,dclk-div", &par->dclk_div))
716 		par->dclk_div = par->device_info->default_dclk_div;
717 	if (device_property_read_u32(dev, "solomon,dclk-frq", &par->dclk_frq))
718 		par->dclk_frq = par->device_info->default_dclk_frq;
719 
720 	vmem_size = DIV_ROUND_UP(par->width, 8) * par->height;
721 
722 	vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
723 					get_order(vmem_size));
724 	if (!vmem) {
725 		dev_err(dev, "Couldn't allocate graphical memory.\n");
726 		ret = -ENOMEM;
727 		goto fb_alloc_error;
728 	}
729 
730 	ssd1307fb_defio = devm_kzalloc(dev, sizeof(*ssd1307fb_defio),
731 				       GFP_KERNEL);
732 	if (!ssd1307fb_defio) {
733 		dev_err(dev, "Couldn't allocate deferred io.\n");
734 		ret = -ENOMEM;
735 		goto fb_alloc_error;
736 	}
737 
738 	ssd1307fb_defio->delay = HZ / refreshrate;
739 	ssd1307fb_defio->deferred_io = ssd1307fb_deferred_io;
740 
741 	info->fbops = &ssd1307fb_ops;
742 	info->fix = ssd1307fb_fix;
743 	info->fix.line_length = DIV_ROUND_UP(par->width, 8);
744 	info->fbdefio = ssd1307fb_defio;
745 
746 	info->var = ssd1307fb_var;
747 	info->var.xres = par->width;
748 	info->var.xres_virtual = par->width;
749 	info->var.yres = par->height;
750 	info->var.yres_virtual = par->height;
751 
752 	info->screen_buffer = vmem;
753 	info->fix.smem_start = __pa(vmem);
754 	info->fix.smem_len = vmem_size;
755 
756 	fb_deferred_io_init(info);
757 
758 	i2c_set_clientdata(client, info);
759 
760 	if (par->reset) {
761 		/* Reset the screen */
762 		gpiod_set_value_cansleep(par->reset, 1);
763 		udelay(4);
764 		gpiod_set_value_cansleep(par->reset, 0);
765 		udelay(4);
766 	}
767 
768 	if (par->vbat_reg) {
769 		ret = regulator_enable(par->vbat_reg);
770 		if (ret) {
771 			dev_err(dev, "failed to enable VBAT: %d\n", ret);
772 			goto reset_oled_error;
773 		}
774 	}
775 
776 	ret = ssd1307fb_init(par);
777 	if (ret)
778 		goto regulator_enable_error;
779 
780 	ret = register_framebuffer(info);
781 	if (ret) {
782 		dev_err(dev, "Couldn't register the framebuffer\n");
783 		goto panel_init_error;
784 	}
785 
786 	snprintf(bl_name, sizeof(bl_name), "ssd1307fb%d", info->node);
787 	bl = backlight_device_register(bl_name, dev, par, &ssd1307fb_bl_ops,
788 				       NULL);
789 	if (IS_ERR(bl)) {
790 		ret = PTR_ERR(bl);
791 		dev_err(dev, "unable to register backlight device: %d\n", ret);
792 		goto bl_init_error;
793 	}
794 
795 	bl->props.brightness = par->contrast;
796 	bl->props.max_brightness = MAX_CONTRAST;
797 	info->bl_dev = bl;
798 
799 	dev_info(dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
800 
801 	return 0;
802 
803 bl_init_error:
804 	unregister_framebuffer(info);
805 panel_init_error:
806 	pwm_disable(par->pwm);
807 	pwm_put(par->pwm);
808 regulator_enable_error:
809 	if (par->vbat_reg)
810 		regulator_disable(par->vbat_reg);
811 reset_oled_error:
812 	fb_deferred_io_cleanup(info);
813 fb_alloc_error:
814 	framebuffer_release(info);
815 	return ret;
816 }
817 
818 static void ssd1307fb_remove(struct i2c_client *client)
819 {
820 	struct fb_info *info = i2c_get_clientdata(client);
821 	struct ssd1307fb_par *par = info->par;
822 
823 	ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
824 
825 	backlight_device_unregister(info->bl_dev);
826 
827 	unregister_framebuffer(info);
828 	pwm_disable(par->pwm);
829 	pwm_put(par->pwm);
830 	if (par->vbat_reg)
831 		regulator_disable(par->vbat_reg);
832 	fb_deferred_io_cleanup(info);
833 	__free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len));
834 	framebuffer_release(info);
835 }
836 
837 static const struct i2c_device_id ssd1307fb_i2c_id[] = {
838 	{ "ssd1305fb", 0 },
839 	{ "ssd1306fb", 0 },
840 	{ "ssd1307fb", 0 },
841 	{ "ssd1309fb", 0 },
842 	{ }
843 };
844 MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
845 
846 static struct i2c_driver ssd1307fb_driver = {
847 	.probe_new = ssd1307fb_probe,
848 	.remove = ssd1307fb_remove,
849 	.id_table = ssd1307fb_i2c_id,
850 	.driver = {
851 		.name = "ssd1307fb",
852 		.of_match_table = ssd1307fb_of_match,
853 	},
854 };
855 
856 module_i2c_driver(ssd1307fb_driver);
857 
858 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
859 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
860 MODULE_LICENSE("GPL");
861