ssd1307fb.c 18.4 KB
Newer Older
1
/*
M
Masanari Iida 已提交
2
 * Driver for the Solomon SSD1307 OLED controller
3 4 5 6 7 8
 *
 * Copyright 2012 Free Electrons
 *
 * Licensed under the GPLv2 or later.
 */

9
#include <linux/backlight.h>
10
#include <linux/delay.h>
11
#include <linux/fb.h>
12
#include <linux/gpio/consumer.h>
13 14 15
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
16 17 18
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/pwm.h>
19
#include <linux/uaccess.h>
20
#include <linux/regulator/consumer.h>
21 22 23 24

#define SSD1307FB_DATA			0x40
#define SSD1307FB_COMMAND		0x80

25 26 27 28 29 30
#define SSD1307FB_SET_ADDRESS_MODE	0x20
#define SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL	(0x00)
#define SSD1307FB_SET_ADDRESS_MODE_VERTICAL	(0x01)
#define SSD1307FB_SET_ADDRESS_MODE_PAGE		(0x02)
#define SSD1307FB_SET_COL_RANGE		0x21
#define SSD1307FB_SET_PAGE_RANGE	0x22
31
#define SSD1307FB_CONTRAST		0x81
32
#define	SSD1307FB_CHARGE_PUMP		0x8d
33 34
#define SSD1307FB_SEG_REMAP_ON		0xa1
#define SSD1307FB_DISPLAY_OFF		0xae
35
#define SSD1307FB_SET_MULTIPLEX_RATIO	0xa8
36 37
#define SSD1307FB_DISPLAY_ON		0xaf
#define SSD1307FB_START_PAGE_ADDRESS	0xb0
38 39 40 41 42 43
#define SSD1307FB_SET_DISPLAY_OFFSET	0xd3
#define	SSD1307FB_SET_CLOCK_FREQ	0xd5
#define	SSD1307FB_SET_PRECHARGE_PERIOD	0xd9
#define	SSD1307FB_SET_COM_PINS_CONFIG	0xda
#define	SSD1307FB_SET_VCOMH		0xdb

44 45
#define MAX_CONTRAST 255

46 47 48 49 50
#define REFRESHRATE 1

static u_int refreshrate = REFRESHRATE;
module_param(refreshrate, uint, 0);

51 52
struct ssd1307fb_par;

53 54 55 56 57 58
struct ssd1307fb_deviceinfo {
	u32 default_vcomh;
	u32 default_dclk_div;
	u32 default_dclk_frq;
	int need_pwm;
	int need_chargepump;
59
};
60 61

struct ssd1307fb_par {
62 63 64 65 66 67 68
	u32 com_invdir;
	u32 com_lrremap;
	u32 com_offset;
	u32 com_seq;
	u32 contrast;
	u32 dclk_div;
	u32 dclk_frq;
69
	const struct ssd1307fb_deviceinfo *device_info;
70
	struct i2c_client *client;
71
	u32 height;
72
	struct fb_info *info;
73
	u32 page_offset;
74 75
	u32 prechargep1;
	u32 prechargep2;
76 77
	struct pwm_device *pwm;
	u32 pwm_period;
78
	struct gpio_desc *reset;
79
	struct regulator *vbat_reg;
80 81
	u32 seg_remap;
	u32 vcomh;
82
	u32 width;
83 84
};

85 86 87 88 89
struct ssd1307fb_array {
	u8	type;
	u8	data[0];
};

90
static const struct fb_fix_screeninfo ssd1307fb_fix = {
91 92 93 94 95 96 97 98 99
	.id		= "Solomon SSD1307",
	.type		= FB_TYPE_PACKED_PIXELS,
	.visual		= FB_VISUAL_MONO10,
	.xpanstep	= 0,
	.ypanstep	= 0,
	.ywrapstep	= 0,
	.accel		= FB_ACCEL_NONE,
};

100
static const struct fb_var_screeninfo ssd1307fb_var = {
101 102 103
	.bits_per_pixel	= 1,
};

104
static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type)
105
{
106
	struct ssd1307fb_array *array;
107

108 109 110
	array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL);
	if (!array)
		return NULL;
111

112
	array->type = type;
113

114
	return array;
115 116
}

117 118
static int ssd1307fb_write_array(struct i2c_client *client,
				 struct ssd1307fb_array *array, u32 len)
119
{
120 121 122 123 124 125 126 127 128 129 130
	int ret;

	len += sizeof(struct ssd1307fb_array);

	ret = i2c_master_send(client, (u8 *)array, len);
	if (ret != len) {
		dev_err(&client->dev, "Couldn't send I2C command.\n");
		return ret;
	}

	return 0;
131 132 133 134
}

static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
{
135 136
	struct ssd1307fb_array *array;
	int ret;
137

138 139 140 141 142 143 144 145 146 147
	array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND);
	if (!array)
		return -ENOMEM;

	array->data[0] = cmd;

	ret = ssd1307fb_write_array(client, array, 1);
	kfree(array);

	return ret;
148 149 150 151
}

static void ssd1307fb_update_display(struct ssd1307fb_par *par)
{
152
	struct ssd1307fb_array *array;
153 154 155
	u8 *vmem = par->info->screen_base;
	int i, j, k;

156 157 158 159 160
	array = ssd1307fb_alloc_array(par->width * par->height / 8,
				      SSD1307FB_DATA);
	if (!array)
		return;

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	/*
	 * The screen is divided in pages, each having a height of 8
	 * pixels, and the width of the screen. When sending a byte of
	 * data to the controller, it gives the 8 bits for the current
	 * column. I.e, the first byte are the 8 bits of the first
	 * column, then the 8 bits for the second column, etc.
	 *
	 *
	 * Representation of the screen, assuming it is 5 bits
	 * wide. Each letter-number combination is a bit that controls
	 * one pixel.
	 *
	 * A0 A1 A2 A3 A4
	 * B0 B1 B2 B3 B4
	 * C0 C1 C2 C3 C4
	 * D0 D1 D2 D3 D4
	 * E0 E1 E2 E3 E4
	 * F0 F1 F2 F3 F4
	 * G0 G1 G2 G3 G4
	 * H0 H1 H2 H3 H4
	 *
	 * If you want to update this screen, you need to send 5 bytes:
	 *  (1) A0 B0 C0 D0 E0 F0 G0 H0
	 *  (2) A1 B1 C1 D1 E1 F1 G1 H1
	 *  (3) A2 B2 C2 D2 E2 F2 G2 H2
	 *  (4) A3 B3 C3 D3 E3 F3 G3 H3
	 *  (5) A4 B4 C4 D4 E4 F4 G4 H4
	 */

190 191
	for (i = 0; i < (par->height / 8); i++) {
		for (j = 0; j < par->width; j++) {
192 193
			u32 array_idx = i * par->width + j;
			array->data[array_idx] = 0;
194
			for (k = 0; k < 8; k++) {
195 196
				u32 page_length = par->width * i;
				u32 index = page_length + (par->width * k + j) / 8;
197
				u8 byte = *(vmem + index);
198 199
				u8 bit = byte & (1 << (j % 8));
				bit = bit >> (j % 8);
200
				array->data[array_idx] |= bit << k;
201 202 203
			}
		}
	}
204 205 206

	ssd1307fb_write_array(par->client, array, par->width * par->height / 8);
	kfree(array);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}


static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
		size_t count, loff_t *ppos)
{
	struct ssd1307fb_par *par = info->par;
	unsigned long total_size;
	unsigned long p = *ppos;
	u8 __iomem *dst;

	total_size = info->fix.smem_len;

	if (p > total_size)
		return -EINVAL;

	if (count + p > total_size)
		count = total_size - p;

	if (!count)
		return -EINVAL;

	dst = (void __force *) (info->screen_base + p);

	if (copy_from_user(dst, buf, count))
		return -EFAULT;

	ssd1307fb_update_display(par);

	*ppos += count;

	return count;
}

241 242 243 244 245 246 247 248 249 250
static int ssd1307fb_blank(int blank_mode, struct fb_info *info)
{
	struct ssd1307fb_par *par = info->par;

	if (blank_mode != FB_BLANK_UNBLANK)
		return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
	else
		return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
{
	struct ssd1307fb_par *par = info->par;
	sys_fillrect(info, rect);
	ssd1307fb_update_display(par);
}

static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
{
	struct ssd1307fb_par *par = info->par;
	sys_copyarea(info, area);
	ssd1307fb_update_display(par);
}

static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
{
	struct ssd1307fb_par *par = info->par;
	sys_imageblit(info, image);
	ssd1307fb_update_display(par);
}

static struct fb_ops ssd1307fb_ops = {
	.owner		= THIS_MODULE,
	.fb_read	= fb_sys_read,
	.fb_write	= ssd1307fb_write,
276
	.fb_blank	= ssd1307fb_blank,
277 278 279 280 281 282 283 284 285 286 287
	.fb_fillrect	= ssd1307fb_fillrect,
	.fb_copyarea	= ssd1307fb_copyarea,
	.fb_imageblit	= ssd1307fb_imageblit,
};

static void ssd1307fb_deferred_io(struct fb_info *info,
				struct list_head *pagelist)
{
	ssd1307fb_update_display(info->par);
}

288
static int ssd1307fb_init(struct ssd1307fb_par *par)
289 290
{
	int ret;
291
	u32 precharge, dclk, com_invdir, compins;
292
	struct pwm_args pargs;
293

294 295 296 297 298 299
	if (par->device_info->need_pwm) {
		par->pwm = pwm_get(&par->client->dev, NULL);
		if (IS_ERR(par->pwm)) {
			dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
			return PTR_ERR(par->pwm);
		}
300

301 302 303 304 305 306 307 308 309
		/*
		 * FIXME: pwm_apply_args() should be removed when switching to
		 * the atomic PWM API.
		 */
		pwm_apply_args(par->pwm);

		pwm_get_args(par->pwm, &pargs);

		par->pwm_period = pargs.period;
310 311 312
		/* Enable the PWM */
		pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
		pwm_enable(par->pwm);
313

314 315 316
		dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
			par->pwm->pwm, par->pwm_period);
	};
317 318 319

	/* Set initial contrast */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
320 321 322
	if (ret < 0)
		return ret;

323
	ret = ssd1307fb_write_cmd(par->client, par->contrast);
324 325 326 327
	if (ret < 0)
		return ret;

	/* Set segment re-map */
328 329 330 331 332 333 334 335 336
	if (par->seg_remap) {
		ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
		if (ret < 0)
			return ret;
	};

	/* Set COM direction */
	com_invdir = 0xc0 | (par->com_invdir & 0x1) << 3;
	ret = ssd1307fb_write_cmd(par->client,  com_invdir);
337 338 339 340 341
	if (ret < 0)
		return ret;

	/* Set multiplex ratio value */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
342 343 344 345
	if (ret < 0)
		return ret;

	ret = ssd1307fb_write_cmd(par->client, par->height - 1);
346 347 348 349 350
	if (ret < 0)
		return ret;

	/* set display offset value */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
351 352 353
	if (ret < 0)
		return ret;

354
	ret = ssd1307fb_write_cmd(par->client, par->com_offset);
355 356 357 358 359
	if (ret < 0)
		return ret;

	/* Set clock frequency */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
360 361 362
	if (ret < 0)
		return ret;

363 364
	dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4;
	ret = ssd1307fb_write_cmd(par->client, dclk);
365 366 367 368 369
	if (ret < 0)
		return ret;

	/* Set precharge period in number of ticks from the internal clock */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
370 371 372
	if (ret < 0)
		return ret;

373 374
	precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4;
	ret = ssd1307fb_write_cmd(par->client, precharge);
375 376 377 378 379
	if (ret < 0)
		return ret;

	/* Set COM pins configuration */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
380 381 382
	if (ret < 0)
		return ret;

383
	compins = 0x02 | !(par->com_seq & 0x1) << 4
384 385
				   | (par->com_lrremap & 0x1) << 5;
	ret = ssd1307fb_write_cmd(par->client, compins);
386 387 388 389 390
	if (ret < 0)
		return ret;

	/* Set VCOMH */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
391 392 393
	if (ret < 0)
		return ret;

394
	ret = ssd1307fb_write_cmd(par->client, par->vcomh);
395 396 397 398 399
	if (ret < 0)
		return ret;

	/* Turn on the DC-DC Charge Pump */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
400 401 402
	if (ret < 0)
		return ret;

403
	ret = ssd1307fb_write_cmd(par->client,
404
		BIT(4) | (par->device_info->need_chargepump ? BIT(2) : 0));
405 406 407
	if (ret < 0)
		return ret;

408 409
	/* Switch to horizontal addressing mode */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE);
410 411 412 413 414
	if (ret < 0)
		return ret;

	ret = ssd1307fb_write_cmd(par->client,
				  SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL);
415 416 417
	if (ret < 0)
		return ret;

418
	/* Set column range */
419
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COL_RANGE);
420 421 422 423 424 425 426 427
	if (ret < 0)
		return ret;

	ret = ssd1307fb_write_cmd(par->client, 0x0);
	if (ret < 0)
		return ret;

	ret = ssd1307fb_write_cmd(par->client, par->width - 1);
428 429 430
	if (ret < 0)
		return ret;

431
	/* Set page range */
432
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PAGE_RANGE);
433 434 435 436 437 438 439 440 441
	if (ret < 0)
		return ret;

	ret = ssd1307fb_write_cmd(par->client, 0x0);
	if (ret < 0)
		return ret;

	ret = ssd1307fb_write_cmd(par->client,
				  par->page_offset + (par->height / 8) - 1);
442 443 444
	if (ret < 0)
		return ret;

445 446
	/* Clear the screen */
	ssd1307fb_update_display(par);
447

448 449 450 451 452 453 454 455
	/* Turn on the display */
	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
	if (ret < 0)
		return ret;

	return 0;
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
static int ssd1307fb_update_bl(struct backlight_device *bdev)
{
	struct ssd1307fb_par *par = bl_get_data(bdev);
	int ret;
	int brightness = bdev->props.brightness;

	par->contrast = brightness;

	ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
	if (ret < 0)
		return ret;
	ret = ssd1307fb_write_cmd(par->client, par->contrast);
	if (ret < 0)
		return ret;
	return 0;
}

static int ssd1307fb_get_brightness(struct backlight_device *bdev)
{
	struct ssd1307fb_par *par = bl_get_data(bdev);

	return par->contrast;
}

static int ssd1307fb_check_fb(struct backlight_device *bdev,
				   struct fb_info *info)
{
	return (info->bl_dev == bdev);
}

static const struct backlight_ops ssd1307fb_bl_ops = {
	.options	= BL_CORE_SUSPENDRESUME,
	.update_status	= ssd1307fb_update_bl,
	.get_brightness	= ssd1307fb_get_brightness,
	.check_fb	= ssd1307fb_check_fb,
};

493 494 495 496 497 498
static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = {
	.default_vcomh = 0x34,
	.default_dclk_div = 1,
	.default_dclk_frq = 7,
};

499 500 501 502 503 504 505 506 507 508 509 510
static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = {
	.default_vcomh = 0x20,
	.default_dclk_div = 1,
	.default_dclk_frq = 8,
	.need_chargepump = 1,
};

static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = {
	.default_vcomh = 0x20,
	.default_dclk_div = 2,
	.default_dclk_frq = 12,
	.need_pwm = 1,
511 512
};

513 514 515 516 517 518
static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo = {
	.default_vcomh = 0x34,
	.default_dclk_div = 1,
	.default_dclk_frq = 10,
};

519
static const struct of_device_id ssd1307fb_of_match[] = {
520 521 522 523
	{
		.compatible = "solomon,ssd1305fb-i2c",
		.data = (void *)&ssd1307fb_ssd1305_deviceinfo,
	},
524 525
	{
		.compatible = "solomon,ssd1306fb-i2c",
526
		.data = (void *)&ssd1307fb_ssd1306_deviceinfo,
527 528 529
	},
	{
		.compatible = "solomon,ssd1307fb-i2c",
530
		.data = (void *)&ssd1307fb_ssd1307_deviceinfo,
531
	},
532 533 534 535
	{
		.compatible = "solomon,ssd1309fb-i2c",
		.data = (void *)&ssd1307fb_ssd1309_deviceinfo,
	},
536 537 538 539
	{},
};
MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);

540 541
static int ssd1307fb_probe(struct i2c_client *client,
			   const struct i2c_device_id *id)
542
{
543 544
	struct backlight_device *bl;
	char bl_name[12];
545
	struct fb_info *info;
546
	struct device_node *node = client->dev.of_node;
547
	struct fb_deferred_io *ssd1307fb_defio;
548
	u32 vmem_size;
549 550 551 552
	struct ssd1307fb_par *par;
	u8 *vmem;
	int ret;

553
	if (!node) {
554 555 556 557 558 559 560 561 562 563
		dev_err(&client->dev, "No device tree data found!\n");
		return -EINVAL;
	}

	info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
	if (!info) {
		dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
		return -ENOMEM;
	}

564 565 566 567
	par = info->par;
	par->info = info;
	par->client = client;

568
	par->device_info = of_device_get_match_data(&client->dev);
569

570 571
	par->reset = devm_gpiod_get_optional(&client->dev, "reset",
					     GPIOD_OUT_LOW);
572 573 574 575
	if (IS_ERR(par->reset)) {
		dev_err(&client->dev, "failed to get reset gpio: %ld\n",
			PTR_ERR(par->reset));
		ret = PTR_ERR(par->reset);
576 577 578
		goto fb_alloc_error;
	}

579 580 581
	par->vbat_reg = devm_regulator_get_optional(&client->dev, "vbat");
	if (IS_ERR(par->vbat_reg)) {
		ret = PTR_ERR(par->vbat_reg);
582 583 584 585 586 587 588
		if (ret == -ENODEV) {
			par->vbat_reg = NULL;
		} else {
			dev_err(&client->dev, "failed to get VBAT regulator: %d\n",
				ret);
			goto fb_alloc_error;
		}
589 590
	}

591 592 593 594
	if (of_property_read_u32(node, "solomon,width", &par->width))
		par->width = 96;

	if (of_property_read_u32(node, "solomon,height", &par->height))
595
		par->height = 16;
596 597 598 599

	if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
		par->page_offset = 1;

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	if (of_property_read_u32(node, "solomon,com-offset", &par->com_offset))
		par->com_offset = 0;

	if (of_property_read_u32(node, "solomon,prechargep1", &par->prechargep1))
		par->prechargep1 = 2;

	if (of_property_read_u32(node, "solomon,prechargep2", &par->prechargep2))
		par->prechargep2 = 2;

	par->seg_remap = !of_property_read_bool(node, "solomon,segment-no-remap");
	par->com_seq = of_property_read_bool(node, "solomon,com-seq");
	par->com_lrremap = of_property_read_bool(node, "solomon,com-lrremap");
	par->com_invdir = of_property_read_bool(node, "solomon,com-invdir");

	par->contrast = 127;
	par->vcomh = par->device_info->default_vcomh;

	/* Setup display timing */
	par->dclk_div = par->device_info->default_dclk_div;
	par->dclk_frq = par->device_info->default_dclk_frq;

621 622
	vmem_size = par->width * par->height / 8;

623 624
	vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
					get_order(vmem_size));
625 626 627 628 629 630
	if (!vmem) {
		dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
		ret = -ENOMEM;
		goto fb_alloc_error;
	}

631 632 633 634 635 636 637 638 639 640
	ssd1307fb_defio = devm_kzalloc(&client->dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
	if (!ssd1307fb_defio) {
		dev_err(&client->dev, "Couldn't allocate deferred io.\n");
		ret = -ENOMEM;
		goto fb_alloc_error;
	}

	ssd1307fb_defio->delay = HZ / refreshrate;
	ssd1307fb_defio->deferred_io = ssd1307fb_deferred_io;

641 642
	info->fbops = &ssd1307fb_ops;
	info->fix = ssd1307fb_fix;
643
	info->fix.line_length = par->width / 8;
644
	info->fbdefio = ssd1307fb_defio;
645 646

	info->var = ssd1307fb_var;
647 648 649 650 651
	info->var.xres = par->width;
	info->var.xres_virtual = par->width;
	info->var.yres = par->height;
	info->var.yres_virtual = par->height;

652 653 654 655 656 657 658 659
	info->var.red.length = 1;
	info->var.red.offset = 0;
	info->var.green.length = 1;
	info->var.green.offset = 0;
	info->var.blue.length = 1;
	info->var.blue.offset = 0;

	info->screen_base = (u8 __force __iomem *)vmem;
660
	info->fix.smem_start = __pa(vmem);
661 662 663 664 665 666
	info->fix.smem_len = vmem_size;

	fb_deferred_io_init(info);

	i2c_set_clientdata(client, info);

667 668 669 670 671 672 673
	if (par->reset) {
		/* Reset the screen */
		gpiod_set_value(par->reset, 0);
		udelay(4);
		gpiod_set_value(par->reset, 1);
		udelay(4);
	}
674

675 676 677 678 679 680 681
	if (par->vbat_reg) {
		ret = regulator_enable(par->vbat_reg);
		if (ret) {
			dev_err(&client->dev, "failed to enable VBAT: %d\n",
				ret);
			goto reset_oled_error;
		}
682 683
	}

684 685
	ret = ssd1307fb_init(par);
	if (ret)
686
		goto regulator_enable_error;
687

688 689 690 691
	ret = register_framebuffer(info);
	if (ret) {
		dev_err(&client->dev, "Couldn't register the framebuffer\n");
		goto panel_init_error;
692 693
	}

694 695 696 697
	snprintf(bl_name, sizeof(bl_name), "ssd1307fb%d", info->node);
	bl = backlight_device_register(bl_name, &client->dev, par,
				       &ssd1307fb_bl_ops, NULL);
	if (IS_ERR(bl)) {
698 699 700
		ret = PTR_ERR(bl);
		dev_err(&client->dev, "unable to register backlight device: %d\n",
			ret);
701 702
		goto bl_init_error;
	}
703 704 705 706 707

	bl->props.brightness = par->contrast;
	bl->props.max_brightness = MAX_CONTRAST;
	info->bl_dev = bl;

708 709 710 711
	dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);

	return 0;

712 713
bl_init_error:
	unregister_framebuffer(info);
714
panel_init_error:
715 716 717 718
	if (par->device_info->need_pwm) {
		pwm_disable(par->pwm);
		pwm_put(par->pwm);
	};
719
regulator_enable_error:
720 721
	if (par->vbat_reg)
		regulator_disable(par->vbat_reg);
722 723 724 725 726 727 728
reset_oled_error:
	fb_deferred_io_cleanup(info);
fb_alloc_error:
	framebuffer_release(info);
	return ret;
}

729
static int ssd1307fb_remove(struct i2c_client *client)
730 731 732 733
{
	struct fb_info *info = i2c_get_clientdata(client);
	struct ssd1307fb_par *par = info->par;

734 735
	ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);

736 737
	backlight_device_unregister(info->bl_dev);

738
	unregister_framebuffer(info);
739 740 741 742
	if (par->device_info->need_pwm) {
		pwm_disable(par->pwm);
		pwm_put(par->pwm);
	};
743
	fb_deferred_io_cleanup(info);
744
	__free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len));
745 746 747 748 749 750
	framebuffer_release(info);

	return 0;
}

static const struct i2c_device_id ssd1307fb_i2c_id[] = {
751
	{ "ssd1305fb", 0 },
752
	{ "ssd1306fb", 0 },
753
	{ "ssd1307fb", 0 },
754
	{ "ssd1309fb", 0 },
755 756 757 758 759 760
	{ }
};
MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);

static struct i2c_driver ssd1307fb_driver = {
	.probe = ssd1307fb_probe,
761
	.remove = ssd1307fb_remove,
762 763 764
	.id_table = ssd1307fb_i2c_id,
	.driver = {
		.name = "ssd1307fb",
765
		.of_match_table = ssd1307fb_of_match,
766 767 768 769 770
	},
};

module_i2c_driver(ssd1307fb_driver);

M
Masanari Iida 已提交
771
MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
772 773
MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
MODULE_LICENSE("GPL");