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