xref: /openbmc/linux/drivers/video/fbdev/ssd1307fb.c (revision d6e2d652)
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 	if (!info->screen_buffer)
305 		return -ENODEV;
306 
307 	total_size = info->fix.smem_len;
308 
309 	if (p > total_size)
310 		return -EINVAL;
311 
312 	if (count + p > total_size)
313 		count = total_size - p;
314 
315 	if (!count)
316 		return -EINVAL;
317 
318 	dst = info->screen_buffer + p;
319 
320 	if (copy_from_user(dst, buf, count))
321 		return -EFAULT;
322 
323 	ret = ssd1307fb_update_display(par);
324 	if (ret < 0)
325 		return ret;
326 
327 	*ppos += count;
328 
329 	return count;
330 }
331 
332 static int ssd1307fb_blank(int blank_mode, struct fb_info *info)
333 {
334 	struct ssd1307fb_par *par = info->par;
335 
336 	if (blank_mode != FB_BLANK_UNBLANK)
337 		return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
338 	else
339 		return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
340 }
341 
342 static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
343 {
344 	struct ssd1307fb_par *par = info->par;
345 	sys_fillrect(info, rect);
346 	ssd1307fb_update_rect(par, rect->dx, rect->dy, rect->width,
347 			      rect->height);
348 }
349 
350 static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
351 {
352 	struct ssd1307fb_par *par = info->par;
353 	sys_copyarea(info, area);
354 	ssd1307fb_update_rect(par, area->dx, area->dy, area->width,
355 			      area->height);
356 }
357 
358 static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
359 {
360 	struct ssd1307fb_par *par = info->par;
361 	sys_imageblit(info, image);
362 	ssd1307fb_update_rect(par, image->dx, image->dy, image->width,
363 			      image->height);
364 }
365 
366 static const struct fb_ops ssd1307fb_ops = {
367 	.owner		= THIS_MODULE,
368 	.fb_read	= fb_sys_read,
369 	.fb_write	= ssd1307fb_write,
370 	.fb_blank	= ssd1307fb_blank,
371 	.fb_fillrect	= ssd1307fb_fillrect,
372 	.fb_copyarea	= ssd1307fb_copyarea,
373 	.fb_imageblit	= ssd1307fb_imageblit,
374 	.fb_mmap	= fb_deferred_io_mmap,
375 };
376 
377 static void ssd1307fb_deferred_io(struct fb_info *info, struct list_head *pagereflist)
378 {
379 	ssd1307fb_update_display(info->par);
380 }
381 
382 static int ssd1307fb_init(struct ssd1307fb_par *par)
383 {
384 	struct pwm_state pwmstate;
385 	int ret;
386 	u32 precharge, dclk, com_invdir, compins;
387 
388 	if (par->device_info->need_pwm) {
389 		par->pwm = pwm_get(&par->client->dev, NULL);
390 		if (IS_ERR(par->pwm)) {
391 			dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
392 			return PTR_ERR(par->pwm);
393 		}
394 
395 		pwm_init_state(par->pwm, &pwmstate);
396 		pwm_set_relative_duty_cycle(&pwmstate, 50, 100);
397 		pwm_apply_state(par->pwm, &pwmstate);
398 
399 		/* Enable the PWM */
400 		pwm_enable(par->pwm);
401 
402 		dev_dbg(&par->client->dev, "Using PWM%d with a %lluns period.\n",
403 			par->pwm->pwm, pwm_get_period(par->pwm));
404 	}
405 
406 	/* Set initial contrast */
407 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
408 	if (ret < 0)
409 		return ret;
410 
411 	ret = ssd1307fb_write_cmd(par->client, par->contrast);
412 	if (ret < 0)
413 		return ret;
414 
415 	/* Set segment re-map */
416 	if (par->seg_remap) {
417 		ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
418 		if (ret < 0)
419 			return ret;
420 	}
421 
422 	/* Set COM direction */
423 	com_invdir = 0xc0 | par->com_invdir << 3;
424 	ret = ssd1307fb_write_cmd(par->client,  com_invdir);
425 	if (ret < 0)
426 		return ret;
427 
428 	/* Set multiplex ratio value */
429 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
430 	if (ret < 0)
431 		return ret;
432 
433 	ret = ssd1307fb_write_cmd(par->client, par->height - 1);
434 	if (ret < 0)
435 		return ret;
436 
437 	/* set display offset value */
438 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
439 	if (ret < 0)
440 		return ret;
441 
442 	ret = ssd1307fb_write_cmd(par->client, par->com_offset);
443 	if (ret < 0)
444 		return ret;
445 
446 	/* Set clock frequency */
447 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
448 	if (ret < 0)
449 		return ret;
450 
451 	dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4;
452 	ret = ssd1307fb_write_cmd(par->client, dclk);
453 	if (ret < 0)
454 		return ret;
455 
456 	/* Set Area Color Mode ON/OFF & Low Power Display Mode */
457 	if (par->area_color_enable || par->low_power) {
458 		u32 mode;
459 
460 		ret = ssd1307fb_write_cmd(par->client,
461 					  SSD1307FB_SET_AREA_COLOR_MODE);
462 		if (ret < 0)
463 			return ret;
464 
465 		mode = (par->area_color_enable ? 0x30 : 0) |
466 			(par->low_power ? 5 : 0);
467 		ret = ssd1307fb_write_cmd(par->client, mode);
468 		if (ret < 0)
469 			return ret;
470 	}
471 
472 	/* Set precharge period in number of ticks from the internal clock */
473 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
474 	if (ret < 0)
475 		return ret;
476 
477 	precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4;
478 	ret = ssd1307fb_write_cmd(par->client, precharge);
479 	if (ret < 0)
480 		return ret;
481 
482 	/* Set COM pins configuration */
483 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
484 	if (ret < 0)
485 		return ret;
486 
487 	compins = 0x02 | !par->com_seq << 4 | par->com_lrremap << 5;
488 	ret = ssd1307fb_write_cmd(par->client, compins);
489 	if (ret < 0)
490 		return ret;
491 
492 	/* Set VCOMH */
493 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
494 	if (ret < 0)
495 		return ret;
496 
497 	ret = ssd1307fb_write_cmd(par->client, par->vcomh);
498 	if (ret < 0)
499 		return ret;
500 
501 	/* Turn on the DC-DC Charge Pump */
502 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
503 	if (ret < 0)
504 		return ret;
505 
506 	ret = ssd1307fb_write_cmd(par->client,
507 		BIT(4) | (par->device_info->need_chargepump ? BIT(2) : 0));
508 	if (ret < 0)
509 		return ret;
510 
511 	/* Set lookup table */
512 	if (par->lookup_table_set) {
513 		int i;
514 
515 		ret = ssd1307fb_write_cmd(par->client,
516 					  SSD1307FB_SET_LOOKUP_TABLE);
517 		if (ret < 0)
518 			return ret;
519 
520 		for (i = 0; i < ARRAY_SIZE(par->lookup_table); ++i) {
521 			u8 val = par->lookup_table[i];
522 
523 			if (val < 31 || val > 63)
524 				dev_warn(&par->client->dev,
525 					 "lookup table index %d value out of range 31 <= %d <= 63\n",
526 					 i, val);
527 			ret = ssd1307fb_write_cmd(par->client, val);
528 			if (ret < 0)
529 				return ret;
530 		}
531 	}
532 
533 	/* Switch to horizontal addressing mode */
534 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE);
535 	if (ret < 0)
536 		return ret;
537 
538 	ret = ssd1307fb_write_cmd(par->client,
539 				  SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL);
540 	if (ret < 0)
541 		return ret;
542 
543 	/* Clear the screen */
544 	ret = ssd1307fb_update_display(par);
545 	if (ret < 0)
546 		return ret;
547 
548 	/* Turn on the display */
549 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
550 	if (ret < 0)
551 		return ret;
552 
553 	return 0;
554 }
555 
556 static int ssd1307fb_update_bl(struct backlight_device *bdev)
557 {
558 	struct ssd1307fb_par *par = bl_get_data(bdev);
559 	int ret;
560 	int brightness = bdev->props.brightness;
561 
562 	par->contrast = brightness;
563 
564 	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
565 	if (ret < 0)
566 		return ret;
567 	ret = ssd1307fb_write_cmd(par->client, par->contrast);
568 	if (ret < 0)
569 		return ret;
570 	return 0;
571 }
572 
573 static int ssd1307fb_get_brightness(struct backlight_device *bdev)
574 {
575 	struct ssd1307fb_par *par = bl_get_data(bdev);
576 
577 	return par->contrast;
578 }
579 
580 static int ssd1307fb_check_fb(struct backlight_device *bdev,
581 				   struct fb_info *info)
582 {
583 	return (info->bl_dev == bdev);
584 }
585 
586 static const struct backlight_ops ssd1307fb_bl_ops = {
587 	.options	= BL_CORE_SUSPENDRESUME,
588 	.update_status	= ssd1307fb_update_bl,
589 	.get_brightness	= ssd1307fb_get_brightness,
590 	.check_fb	= ssd1307fb_check_fb,
591 };
592 
593 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = {
594 	.default_vcomh = 0x34,
595 	.default_dclk_div = 1,
596 	.default_dclk_frq = 7,
597 };
598 
599 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = {
600 	.default_vcomh = 0x20,
601 	.default_dclk_div = 1,
602 	.default_dclk_frq = 8,
603 	.need_chargepump = 1,
604 };
605 
606 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = {
607 	.default_vcomh = 0x20,
608 	.default_dclk_div = 2,
609 	.default_dclk_frq = 12,
610 	.need_pwm = 1,
611 };
612 
613 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo = {
614 	.default_vcomh = 0x34,
615 	.default_dclk_div = 1,
616 	.default_dclk_frq = 10,
617 };
618 
619 static const struct of_device_id ssd1307fb_of_match[] = {
620 	{
621 		.compatible = "solomon,ssd1305fb-i2c",
622 		.data = (void *)&ssd1307fb_ssd1305_deviceinfo,
623 	},
624 	{
625 		.compatible = "solomon,ssd1306fb-i2c",
626 		.data = (void *)&ssd1307fb_ssd1306_deviceinfo,
627 	},
628 	{
629 		.compatible = "solomon,ssd1307fb-i2c",
630 		.data = (void *)&ssd1307fb_ssd1307_deviceinfo,
631 	},
632 	{
633 		.compatible = "solomon,ssd1309fb-i2c",
634 		.data = (void *)&ssd1307fb_ssd1309_deviceinfo,
635 	},
636 	{},
637 };
638 MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
639 
640 static int ssd1307fb_probe(struct i2c_client *client)
641 {
642 	struct device *dev = &client->dev;
643 	struct backlight_device *bl;
644 	char bl_name[12];
645 	struct fb_info *info;
646 	struct fb_deferred_io *ssd1307fb_defio;
647 	u32 vmem_size;
648 	struct ssd1307fb_par *par;
649 	void *vmem;
650 	int ret;
651 
652 	info = framebuffer_alloc(sizeof(struct ssd1307fb_par), dev);
653 	if (!info)
654 		return -ENOMEM;
655 
656 	par = info->par;
657 	par->info = info;
658 	par->client = client;
659 
660 	par->device_info = device_get_match_data(dev);
661 
662 	par->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
663 	if (IS_ERR(par->reset)) {
664 		ret = dev_err_probe(dev, PTR_ERR(par->reset),
665 				    "failed to get reset gpio\n");
666 		goto fb_alloc_error;
667 	}
668 
669 	par->vbat_reg = devm_regulator_get_optional(dev, "vbat");
670 	if (IS_ERR(par->vbat_reg)) {
671 		ret = PTR_ERR(par->vbat_reg);
672 		if (ret == -ENODEV) {
673 			par->vbat_reg = NULL;
674 		} else {
675 			dev_err_probe(dev, ret, "failed to get VBAT regulator\n");
676 			goto fb_alloc_error;
677 		}
678 	}
679 
680 	if (device_property_read_u32(dev, "solomon,width", &par->width))
681 		par->width = 96;
682 
683 	if (device_property_read_u32(dev, "solomon,height", &par->height))
684 		par->height = 16;
685 
686 	if (device_property_read_u32(dev, "solomon,page-offset", &par->page_offset))
687 		par->page_offset = 1;
688 
689 	if (device_property_read_u32(dev, "solomon,col-offset", &par->col_offset))
690 		par->col_offset = 0;
691 
692 	if (device_property_read_u32(dev, "solomon,com-offset", &par->com_offset))
693 		par->com_offset = 0;
694 
695 	if (device_property_read_u32(dev, "solomon,prechargep1", &par->prechargep1))
696 		par->prechargep1 = 2;
697 
698 	if (device_property_read_u32(dev, "solomon,prechargep2", &par->prechargep2))
699 		par->prechargep2 = 2;
700 
701 	if (!device_property_read_u8_array(dev, "solomon,lookup-table",
702 					   par->lookup_table,
703 					   ARRAY_SIZE(par->lookup_table)))
704 		par->lookup_table_set = 1;
705 
706 	par->seg_remap = !device_property_read_bool(dev, "solomon,segment-no-remap");
707 	par->com_seq = device_property_read_bool(dev, "solomon,com-seq");
708 	par->com_lrremap = device_property_read_bool(dev, "solomon,com-lrremap");
709 	par->com_invdir = device_property_read_bool(dev, "solomon,com-invdir");
710 	par->area_color_enable =
711 		device_property_read_bool(dev, "solomon,area-color-enable");
712 	par->low_power = device_property_read_bool(dev, "solomon,low-power");
713 
714 	par->contrast = 127;
715 	par->vcomh = par->device_info->default_vcomh;
716 
717 	/* Setup display timing */
718 	if (device_property_read_u32(dev, "solomon,dclk-div", &par->dclk_div))
719 		par->dclk_div = par->device_info->default_dclk_div;
720 	if (device_property_read_u32(dev, "solomon,dclk-frq", &par->dclk_frq))
721 		par->dclk_frq = par->device_info->default_dclk_frq;
722 
723 	vmem_size = DIV_ROUND_UP(par->width, 8) * par->height;
724 
725 	vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
726 					get_order(vmem_size));
727 	if (!vmem) {
728 		dev_err(dev, "Couldn't allocate graphical memory.\n");
729 		ret = -ENOMEM;
730 		goto fb_alloc_error;
731 	}
732 
733 	ssd1307fb_defio = devm_kzalloc(dev, sizeof(*ssd1307fb_defio),
734 				       GFP_KERNEL);
735 	if (!ssd1307fb_defio) {
736 		dev_err(dev, "Couldn't allocate deferred io.\n");
737 		ret = -ENOMEM;
738 		goto fb_alloc_error;
739 	}
740 
741 	ssd1307fb_defio->delay = HZ / refreshrate;
742 	ssd1307fb_defio->deferred_io = ssd1307fb_deferred_io;
743 
744 	info->fbops = &ssd1307fb_ops;
745 	info->fix = ssd1307fb_fix;
746 	info->fix.line_length = DIV_ROUND_UP(par->width, 8);
747 	info->fbdefio = ssd1307fb_defio;
748 
749 	info->var = ssd1307fb_var;
750 	info->var.xres = par->width;
751 	info->var.xres_virtual = par->width;
752 	info->var.yres = par->height;
753 	info->var.yres_virtual = par->height;
754 
755 	info->screen_buffer = vmem;
756 	info->fix.smem_start = __pa(vmem);
757 	info->fix.smem_len = vmem_size;
758 
759 	fb_deferred_io_init(info);
760 
761 	i2c_set_clientdata(client, info);
762 
763 	if (par->reset) {
764 		/* Reset the screen */
765 		gpiod_set_value_cansleep(par->reset, 1);
766 		udelay(4);
767 		gpiod_set_value_cansleep(par->reset, 0);
768 		udelay(4);
769 	}
770 
771 	if (par->vbat_reg) {
772 		ret = regulator_enable(par->vbat_reg);
773 		if (ret) {
774 			dev_err(dev, "failed to enable VBAT: %d\n", ret);
775 			goto reset_oled_error;
776 		}
777 	}
778 
779 	ret = ssd1307fb_init(par);
780 	if (ret)
781 		goto regulator_enable_error;
782 
783 	ret = register_framebuffer(info);
784 	if (ret) {
785 		dev_err(dev, "Couldn't register the framebuffer\n");
786 		goto panel_init_error;
787 	}
788 
789 	snprintf(bl_name, sizeof(bl_name), "ssd1307fb%d", info->node);
790 	bl = backlight_device_register(bl_name, dev, par, &ssd1307fb_bl_ops,
791 				       NULL);
792 	if (IS_ERR(bl)) {
793 		ret = PTR_ERR(bl);
794 		dev_err(dev, "unable to register backlight device: %d\n", ret);
795 		goto bl_init_error;
796 	}
797 
798 	bl->props.brightness = par->contrast;
799 	bl->props.max_brightness = MAX_CONTRAST;
800 	info->bl_dev = bl;
801 
802 	dev_info(dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
803 
804 	return 0;
805 
806 bl_init_error:
807 	unregister_framebuffer(info);
808 panel_init_error:
809 	pwm_disable(par->pwm);
810 	pwm_put(par->pwm);
811 regulator_enable_error:
812 	if (par->vbat_reg)
813 		regulator_disable(par->vbat_reg);
814 reset_oled_error:
815 	fb_deferred_io_cleanup(info);
816 fb_alloc_error:
817 	framebuffer_release(info);
818 	return ret;
819 }
820 
821 static void ssd1307fb_remove(struct i2c_client *client)
822 {
823 	struct fb_info *info = i2c_get_clientdata(client);
824 	struct ssd1307fb_par *par = info->par;
825 
826 	ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
827 
828 	backlight_device_unregister(info->bl_dev);
829 
830 	unregister_framebuffer(info);
831 	pwm_disable(par->pwm);
832 	pwm_put(par->pwm);
833 	if (par->vbat_reg)
834 		regulator_disable(par->vbat_reg);
835 	fb_deferred_io_cleanup(info);
836 	__free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len));
837 	framebuffer_release(info);
838 }
839 
840 static const struct i2c_device_id ssd1307fb_i2c_id[] = {
841 	{ "ssd1305fb", 0 },
842 	{ "ssd1306fb", 0 },
843 	{ "ssd1307fb", 0 },
844 	{ "ssd1309fb", 0 },
845 	{ }
846 };
847 MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
848 
849 static struct i2c_driver ssd1307fb_driver = {
850 	.probe = ssd1307fb_probe,
851 	.remove = ssd1307fb_remove,
852 	.id_table = ssd1307fb_i2c_id,
853 	.driver = {
854 		.name = "ssd1307fb",
855 		.of_match_table = ssd1307fb_of_match,
856 	},
857 };
858 
859 module_i2c_driver(ssd1307fb_driver);
860 
861 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
862 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
863 MODULE_LICENSE("GPL");
864