1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Solomon SSD1307 OLED controller
5 * Copyright 2012 Free Electrons
8 #include <linux/backlight.h>
9 #include <linux/delay.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>
20 #define SSD1307FB_DATA 0x40
21 #define SSD1307FB_COMMAND 0x80
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
44 #define MAX_CONTRAST 255
48 static u_int refreshrate = REFRESHRATE;
49 module_param(refreshrate, uint, 0);
53 struct ssd1307fb_deviceinfo {
61 struct ssd1307fb_par {
62 unsigned area_color_enable : 1;
63 unsigned com_invdir : 1;
64 unsigned com_lrremap : 1;
66 unsigned lookup_table_set : 1;
67 unsigned low_power : 1;
68 unsigned seg_remap : 1;
73 const struct ssd1307fb_deviceinfo *device_info;
74 struct i2c_client *client;
81 struct pwm_device *pwm;
82 struct gpio_desc *reset;
83 struct regulator *vbat_reg;
88 struct ssd1307fb_array {
93 static const struct fb_fix_screeninfo ssd1307fb_fix = {
94 .id = "Solomon SSD1307",
95 .type = FB_TYPE_PACKED_PIXELS,
96 .visual = FB_VISUAL_MONO10,
100 .accel = FB_ACCEL_NONE,
103 static const struct fb_var_screeninfo ssd1307fb_var = {
105 .red = { .length = 1 },
106 .green = { .length = 1 },
107 .blue = { .length = 1 },
110 static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type)
112 struct ssd1307fb_array *array;
114 array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL);
123 static int ssd1307fb_write_array(struct i2c_client *client,
124 struct ssd1307fb_array *array, u32 len)
128 len += sizeof(struct ssd1307fb_array);
130 ret = i2c_master_send(client, (u8 *)array, len);
132 dev_err(&client->dev, "Couldn't send I2C command.\n");
139 static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
141 struct ssd1307fb_array *array;
144 array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND);
148 array->data[0] = cmd;
150 ret = ssd1307fb_write_array(client, array, 1);
156 static void ssd1307fb_update_display(struct ssd1307fb_par *par)
158 struct ssd1307fb_array *array;
159 u8 *vmem = par->info->screen_buffer;
160 unsigned int line_length = par->info->fix.line_length;
161 unsigned int pages = DIV_ROUND_UP(par->height, 8);
164 array = ssd1307fb_alloc_array(par->width * pages, SSD1307FB_DATA);
169 * The screen is divided in pages, each having a height of 8
170 * pixels, and the width of the screen. When sending a byte of
171 * data to the controller, it gives the 8 bits for the current
172 * column. I.e, the first byte are the 8 bits of the first
173 * column, then the 8 bits for the second column, etc.
176 * Representation of the screen, assuming it is 5 bits
177 * wide. Each letter-number combination is a bit that controls
189 * If you want to update this screen, you need to send 5 bytes:
190 * (1) A0 B0 C0 D0 E0 F0 G0 H0
191 * (2) A1 B1 C1 D1 E1 F1 G1 H1
192 * (3) A2 B2 C2 D2 E2 F2 G2 H2
193 * (4) A3 B3 C3 D3 E3 F3 G3 H3
194 * (5) A4 B4 C4 D4 E4 F4 G4 H4
197 for (i = 0; i < pages; i++) {
198 for (j = 0; j < par->width; j++) {
200 u32 array_idx = i * par->width + j;
201 array->data[array_idx] = 0;
202 /* Last page may be partial */
203 if (i + 1 == pages && par->height % 8)
205 for (k = 0; k < m; k++) {
206 u8 byte = vmem[(8 * i + k) * line_length +
208 u8 bit = (byte >> (j % 8)) & 1;
209 array->data[array_idx] |= bit << k;
214 ssd1307fb_write_array(par->client, array, par->width * pages);
219 static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
220 size_t count, loff_t *ppos)
222 struct ssd1307fb_par *par = info->par;
223 unsigned long total_size;
224 unsigned long p = *ppos;
227 total_size = info->fix.smem_len;
232 if (count + p > total_size)
233 count = total_size - p;
238 dst = info->screen_buffer + p;
240 if (copy_from_user(dst, buf, count))
243 ssd1307fb_update_display(par);
250 static int ssd1307fb_blank(int blank_mode, struct fb_info *info)
252 struct ssd1307fb_par *par = info->par;
254 if (blank_mode != FB_BLANK_UNBLANK)
255 return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
257 return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
260 static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
262 struct ssd1307fb_par *par = info->par;
263 sys_fillrect(info, rect);
264 ssd1307fb_update_display(par);
267 static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
269 struct ssd1307fb_par *par = info->par;
270 sys_copyarea(info, area);
271 ssd1307fb_update_display(par);
274 static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
276 struct ssd1307fb_par *par = info->par;
277 sys_imageblit(info, image);
278 ssd1307fb_update_display(par);
281 static const struct fb_ops ssd1307fb_ops = {
282 .owner = THIS_MODULE,
283 .fb_read = fb_sys_read,
284 .fb_write = ssd1307fb_write,
285 .fb_blank = ssd1307fb_blank,
286 .fb_fillrect = ssd1307fb_fillrect,
287 .fb_copyarea = ssd1307fb_copyarea,
288 .fb_imageblit = ssd1307fb_imageblit,
291 static void ssd1307fb_deferred_io(struct fb_info *info,
292 struct list_head *pagelist)
294 ssd1307fb_update_display(info->par);
297 static int ssd1307fb_init(struct ssd1307fb_par *par)
299 struct pwm_state pwmstate;
301 u32 precharge, dclk, com_invdir, compins;
303 if (par->device_info->need_pwm) {
304 par->pwm = pwm_get(&par->client->dev, NULL);
305 if (IS_ERR(par->pwm)) {
306 dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
307 return PTR_ERR(par->pwm);
310 pwm_init_state(par->pwm, &pwmstate);
311 pwm_set_relative_duty_cycle(&pwmstate, 50, 100);
312 pwm_apply_state(par->pwm, &pwmstate);
315 pwm_enable(par->pwm);
317 dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
318 par->pwm->pwm, pwm_get_period(par->pwm));
321 /* Set initial contrast */
322 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
326 ret = ssd1307fb_write_cmd(par->client, par->contrast);
330 /* Set segment re-map */
331 if (par->seg_remap) {
332 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
337 /* Set COM direction */
338 com_invdir = 0xc0 | par->com_invdir << 3;
339 ret = ssd1307fb_write_cmd(par->client, com_invdir);
343 /* Set multiplex ratio value */
344 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
348 ret = ssd1307fb_write_cmd(par->client, par->height - 1);
352 /* set display offset value */
353 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
357 ret = ssd1307fb_write_cmd(par->client, par->com_offset);
361 /* Set clock frequency */
362 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
366 dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4;
367 ret = ssd1307fb_write_cmd(par->client, dclk);
371 /* Set Set Area Color Mode ON/OFF & Low Power Display Mode */
372 if (par->area_color_enable || par->low_power) {
375 ret = ssd1307fb_write_cmd(par->client,
376 SSD1307FB_SET_AREA_COLOR_MODE);
380 mode = (par->area_color_enable ? 0x30 : 0) |
381 (par->low_power ? 5 : 0);
382 ret = ssd1307fb_write_cmd(par->client, mode);
387 /* Set precharge period in number of ticks from the internal clock */
388 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
392 precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4;
393 ret = ssd1307fb_write_cmd(par->client, precharge);
397 /* Set COM pins configuration */
398 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
402 compins = 0x02 | !par->com_seq << 4 | par->com_lrremap << 5;
403 ret = ssd1307fb_write_cmd(par->client, compins);
408 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
412 ret = ssd1307fb_write_cmd(par->client, par->vcomh);
416 /* Turn on the DC-DC Charge Pump */
417 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
421 ret = ssd1307fb_write_cmd(par->client,
422 BIT(4) | (par->device_info->need_chargepump ? BIT(2) : 0));
426 /* Set lookup table */
427 if (par->lookup_table_set) {
430 ret = ssd1307fb_write_cmd(par->client,
431 SSD1307FB_SET_LOOKUP_TABLE);
435 for (i = 0; i < ARRAY_SIZE(par->lookup_table); ++i) {
436 u8 val = par->lookup_table[i];
438 if (val < 31 || val > 63)
439 dev_warn(&par->client->dev,
440 "lookup table index %d value out of range 31 <= %d <= 63\n",
442 ret = ssd1307fb_write_cmd(par->client, val);
448 /* Switch to horizontal addressing mode */
449 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE);
453 ret = ssd1307fb_write_cmd(par->client,
454 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL);
458 /* Set column range */
459 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COL_RANGE);
463 ret = ssd1307fb_write_cmd(par->client, 0x0);
467 ret = ssd1307fb_write_cmd(par->client, par->width - 1);
472 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PAGE_RANGE);
476 ret = ssd1307fb_write_cmd(par->client, par->page_offset);
480 ret = ssd1307fb_write_cmd(par->client,
482 DIV_ROUND_UP(par->height, 8) - 1);
486 /* Clear the screen */
487 ssd1307fb_update_display(par);
489 /* Turn on the display */
490 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
497 static int ssd1307fb_update_bl(struct backlight_device *bdev)
499 struct ssd1307fb_par *par = bl_get_data(bdev);
501 int brightness = bdev->props.brightness;
503 par->contrast = brightness;
505 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
508 ret = ssd1307fb_write_cmd(par->client, par->contrast);
514 static int ssd1307fb_get_brightness(struct backlight_device *bdev)
516 struct ssd1307fb_par *par = bl_get_data(bdev);
518 return par->contrast;
521 static int ssd1307fb_check_fb(struct backlight_device *bdev,
522 struct fb_info *info)
524 return (info->bl_dev == bdev);
527 static const struct backlight_ops ssd1307fb_bl_ops = {
528 .options = BL_CORE_SUSPENDRESUME,
529 .update_status = ssd1307fb_update_bl,
530 .get_brightness = ssd1307fb_get_brightness,
531 .check_fb = ssd1307fb_check_fb,
534 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = {
535 .default_vcomh = 0x34,
536 .default_dclk_div = 1,
537 .default_dclk_frq = 7,
540 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = {
541 .default_vcomh = 0x20,
542 .default_dclk_div = 1,
543 .default_dclk_frq = 8,
544 .need_chargepump = 1,
547 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = {
548 .default_vcomh = 0x20,
549 .default_dclk_div = 2,
550 .default_dclk_frq = 12,
554 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo = {
555 .default_vcomh = 0x34,
556 .default_dclk_div = 1,
557 .default_dclk_frq = 10,
560 static const struct of_device_id ssd1307fb_of_match[] = {
562 .compatible = "solomon,ssd1305fb-i2c",
563 .data = (void *)&ssd1307fb_ssd1305_deviceinfo,
566 .compatible = "solomon,ssd1306fb-i2c",
567 .data = (void *)&ssd1307fb_ssd1306_deviceinfo,
570 .compatible = "solomon,ssd1307fb-i2c",
571 .data = (void *)&ssd1307fb_ssd1307_deviceinfo,
574 .compatible = "solomon,ssd1309fb-i2c",
575 .data = (void *)&ssd1307fb_ssd1309_deviceinfo,
579 MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
581 static int ssd1307fb_probe(struct i2c_client *client)
583 struct device *dev = &client->dev;
584 struct backlight_device *bl;
586 struct fb_info *info;
587 struct fb_deferred_io *ssd1307fb_defio;
589 struct ssd1307fb_par *par;
593 info = framebuffer_alloc(sizeof(struct ssd1307fb_par), dev);
599 par->client = client;
601 par->device_info = device_get_match_data(dev);
603 par->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
604 if (IS_ERR(par->reset)) {
605 dev_err(dev, "failed to get reset gpio: %ld\n",
606 PTR_ERR(par->reset));
607 ret = PTR_ERR(par->reset);
611 par->vbat_reg = devm_regulator_get_optional(dev, "vbat");
612 if (IS_ERR(par->vbat_reg)) {
613 ret = PTR_ERR(par->vbat_reg);
614 if (ret == -ENODEV) {
615 par->vbat_reg = NULL;
617 dev_err(dev, "failed to get VBAT regulator: %d\n", ret);
622 if (device_property_read_u32(dev, "solomon,width", &par->width))
625 if (device_property_read_u32(dev, "solomon,height", &par->height))
628 if (device_property_read_u32(dev, "solomon,page-offset", &par->page_offset))
629 par->page_offset = 1;
631 if (device_property_read_u32(dev, "solomon,com-offset", &par->com_offset))
634 if (device_property_read_u32(dev, "solomon,prechargep1", &par->prechargep1))
635 par->prechargep1 = 2;
637 if (device_property_read_u32(dev, "solomon,prechargep2", &par->prechargep2))
638 par->prechargep2 = 2;
640 if (!device_property_read_u8_array(dev, "solomon,lookup-table",
642 ARRAY_SIZE(par->lookup_table)))
643 par->lookup_table_set = 1;
645 par->seg_remap = !device_property_read_bool(dev, "solomon,segment-no-remap");
646 par->com_seq = device_property_read_bool(dev, "solomon,com-seq");
647 par->com_lrremap = device_property_read_bool(dev, "solomon,com-lrremap");
648 par->com_invdir = device_property_read_bool(dev, "solomon,com-invdir");
649 par->area_color_enable =
650 device_property_read_bool(dev, "solomon,area-color-enable");
651 par->low_power = device_property_read_bool(dev, "solomon,low-power");
654 par->vcomh = par->device_info->default_vcomh;
656 /* Setup display timing */
657 if (device_property_read_u32(dev, "solomon,dclk-div", &par->dclk_div))
658 par->dclk_div = par->device_info->default_dclk_div;
659 if (device_property_read_u32(dev, "solomon,dclk-frq", &par->dclk_frq))
660 par->dclk_frq = par->device_info->default_dclk_frq;
662 vmem_size = DIV_ROUND_UP(par->width, 8) * par->height;
664 vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
665 get_order(vmem_size));
667 dev_err(dev, "Couldn't allocate graphical memory.\n");
672 ssd1307fb_defio = devm_kzalloc(dev, sizeof(*ssd1307fb_defio),
674 if (!ssd1307fb_defio) {
675 dev_err(dev, "Couldn't allocate deferred io.\n");
680 ssd1307fb_defio->delay = HZ / refreshrate;
681 ssd1307fb_defio->deferred_io = ssd1307fb_deferred_io;
683 info->fbops = &ssd1307fb_ops;
684 info->fix = ssd1307fb_fix;
685 info->fix.line_length = DIV_ROUND_UP(par->width, 8);
686 info->fbdefio = ssd1307fb_defio;
688 info->var = ssd1307fb_var;
689 info->var.xres = par->width;
690 info->var.xres_virtual = par->width;
691 info->var.yres = par->height;
692 info->var.yres_virtual = par->height;
694 info->screen_buffer = vmem;
695 info->fix.smem_start = __pa(vmem);
696 info->fix.smem_len = vmem_size;
698 fb_deferred_io_init(info);
700 i2c_set_clientdata(client, info);
703 /* Reset the screen */
704 gpiod_set_value_cansleep(par->reset, 1);
706 gpiod_set_value_cansleep(par->reset, 0);
711 ret = regulator_enable(par->vbat_reg);
713 dev_err(dev, "failed to enable VBAT: %d\n", ret);
714 goto reset_oled_error;
718 ret = ssd1307fb_init(par);
720 goto regulator_enable_error;
722 ret = register_framebuffer(info);
724 dev_err(dev, "Couldn't register the framebuffer\n");
725 goto panel_init_error;
728 snprintf(bl_name, sizeof(bl_name), "ssd1307fb%d", info->node);
729 bl = backlight_device_register(bl_name, dev, par, &ssd1307fb_bl_ops,
733 dev_err(dev, "unable to register backlight device: %d\n", ret);
737 bl->props.brightness = par->contrast;
738 bl->props.max_brightness = MAX_CONTRAST;
741 dev_info(dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
746 unregister_framebuffer(info);
748 if (par->device_info->need_pwm) {
749 pwm_disable(par->pwm);
752 regulator_enable_error:
754 regulator_disable(par->vbat_reg);
756 fb_deferred_io_cleanup(info);
758 framebuffer_release(info);
762 static int ssd1307fb_remove(struct i2c_client *client)
764 struct fb_info *info = i2c_get_clientdata(client);
765 struct ssd1307fb_par *par = info->par;
767 ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
769 backlight_device_unregister(info->bl_dev);
771 unregister_framebuffer(info);
772 if (par->device_info->need_pwm) {
773 pwm_disable(par->pwm);
777 regulator_disable(par->vbat_reg);
778 fb_deferred_io_cleanup(info);
779 __free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len));
780 framebuffer_release(info);
785 static const struct i2c_device_id ssd1307fb_i2c_id[] = {
792 MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
794 static struct i2c_driver ssd1307fb_driver = {
795 .probe_new = ssd1307fb_probe,
796 .remove = ssd1307fb_remove,
797 .id_table = ssd1307fb_i2c_id,
800 .of_match_table = ssd1307fb_of_match,
804 module_i2c_driver(ssd1307fb_driver);
806 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
807 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
808 MODULE_LICENSE("GPL");