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/of_device.h>
16 #include <linux/of_gpio.h>
17 #include <linux/pwm.h>
18 #include <linux/uaccess.h>
19 #include <linux/regulator/consumer.h>
21 #define SSD1307FB_DATA 0x40
22 #define SSD1307FB_COMMAND 0x80
24 #define SSD1307FB_SET_ADDRESS_MODE 0x20
25 #define SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL (0x00)
26 #define SSD1307FB_SET_ADDRESS_MODE_VERTICAL (0x01)
27 #define SSD1307FB_SET_ADDRESS_MODE_PAGE (0x02)
28 #define SSD1307FB_SET_COL_RANGE 0x21
29 #define SSD1307FB_SET_PAGE_RANGE 0x22
30 #define SSD1307FB_CONTRAST 0x81
31 #define SSD1307FB_SET_LOOKUP_TABLE 0x91
32 #define SSD1307FB_CHARGE_PUMP 0x8d
33 #define SSD1307FB_SEG_REMAP_ON 0xa1
34 #define SSD1307FB_DISPLAY_OFF 0xae
35 #define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8
36 #define SSD1307FB_DISPLAY_ON 0xaf
37 #define SSD1307FB_START_PAGE_ADDRESS 0xb0
38 #define SSD1307FB_SET_DISPLAY_OFFSET 0xd3
39 #define SSD1307FB_SET_CLOCK_FREQ 0xd5
40 #define SSD1307FB_SET_AREA_COLOR_MODE 0xd8
41 #define SSD1307FB_SET_PRECHARGE_PERIOD 0xd9
42 #define SSD1307FB_SET_COM_PINS_CONFIG 0xda
43 #define SSD1307FB_SET_VCOMH 0xdb
45 #define MAX_CONTRAST 255
49 static u_int refreshrate = REFRESHRATE;
50 module_param(refreshrate, uint, 0);
54 struct ssd1307fb_deviceinfo {
62 struct ssd1307fb_par {
63 unsigned area_color_enable : 1;
64 unsigned com_invdir : 1;
65 unsigned com_lrremap : 1;
67 unsigned lookup_table_set : 1;
68 unsigned low_power : 1;
69 unsigned seg_remap : 1;
74 const struct ssd1307fb_deviceinfo *device_info;
75 struct i2c_client *client;
82 struct pwm_device *pwm;
84 struct gpio_desc *reset;
85 struct regulator *vbat_reg;
90 struct ssd1307fb_array {
95 static const struct fb_fix_screeninfo ssd1307fb_fix = {
96 .id = "Solomon SSD1307",
97 .type = FB_TYPE_PACKED_PIXELS,
98 .visual = FB_VISUAL_MONO10,
102 .accel = FB_ACCEL_NONE,
105 static const struct fb_var_screeninfo ssd1307fb_var = {
107 .red = { .length = 1 },
108 .green = { .length = 1 },
109 .blue = { .length = 1 },
112 static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type)
114 struct ssd1307fb_array *array;
116 array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL);
125 static int ssd1307fb_write_array(struct i2c_client *client,
126 struct ssd1307fb_array *array, u32 len)
130 len += sizeof(struct ssd1307fb_array);
132 ret = i2c_master_send(client, (u8 *)array, len);
134 dev_err(&client->dev, "Couldn't send I2C command.\n");
141 static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
143 struct ssd1307fb_array *array;
146 array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND);
150 array->data[0] = cmd;
152 ret = ssd1307fb_write_array(client, array, 1);
158 static void ssd1307fb_update_display(struct ssd1307fb_par *par)
160 struct ssd1307fb_array *array;
161 u8 *vmem = par->info->screen_buffer;
162 unsigned int line_length = par->info->fix.line_length;
163 unsigned int pages = DIV_ROUND_UP(par->height, 8);
166 array = ssd1307fb_alloc_array(par->width * pages, SSD1307FB_DATA);
171 * The screen is divided in pages, each having a height of 8
172 * pixels, and the width of the screen. When sending a byte of
173 * data to the controller, it gives the 8 bits for the current
174 * column. I.e, the first byte are the 8 bits of the first
175 * column, then the 8 bits for the second column, etc.
178 * Representation of the screen, assuming it is 5 bits
179 * wide. Each letter-number combination is a bit that controls
191 * If you want to update this screen, you need to send 5 bytes:
192 * (1) A0 B0 C0 D0 E0 F0 G0 H0
193 * (2) A1 B1 C1 D1 E1 F1 G1 H1
194 * (3) A2 B2 C2 D2 E2 F2 G2 H2
195 * (4) A3 B3 C3 D3 E3 F3 G3 H3
196 * (5) A4 B4 C4 D4 E4 F4 G4 H4
199 for (i = 0; i < pages; i++) {
200 for (j = 0; j < par->width; j++) {
202 u32 array_idx = i * par->width + j;
203 array->data[array_idx] = 0;
204 /* Last page may be partial */
205 if (i + 1 == pages && par->height % 8)
207 for (k = 0; k < m; k++) {
208 u8 byte = vmem[(8 * i + k) * line_length +
210 u8 bit = (byte >> (j % 8)) & 1;
211 array->data[array_idx] |= bit << k;
216 ssd1307fb_write_array(par->client, array, par->width * pages);
221 static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
222 size_t count, loff_t *ppos)
224 struct ssd1307fb_par *par = info->par;
225 unsigned long total_size;
226 unsigned long p = *ppos;
229 total_size = info->fix.smem_len;
234 if (count + p > total_size)
235 count = total_size - p;
240 dst = info->screen_buffer + p;
242 if (copy_from_user(dst, buf, count))
245 ssd1307fb_update_display(par);
252 static int ssd1307fb_blank(int blank_mode, struct fb_info *info)
254 struct ssd1307fb_par *par = info->par;
256 if (blank_mode != FB_BLANK_UNBLANK)
257 return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
259 return ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
262 static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
264 struct ssd1307fb_par *par = info->par;
265 sys_fillrect(info, rect);
266 ssd1307fb_update_display(par);
269 static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
271 struct ssd1307fb_par *par = info->par;
272 sys_copyarea(info, area);
273 ssd1307fb_update_display(par);
276 static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
278 struct ssd1307fb_par *par = info->par;
279 sys_imageblit(info, image);
280 ssd1307fb_update_display(par);
283 static struct fb_ops ssd1307fb_ops = {
284 .owner = THIS_MODULE,
285 .fb_read = fb_sys_read,
286 .fb_write = ssd1307fb_write,
287 .fb_blank = ssd1307fb_blank,
288 .fb_fillrect = ssd1307fb_fillrect,
289 .fb_copyarea = ssd1307fb_copyarea,
290 .fb_imageblit = ssd1307fb_imageblit,
293 static void ssd1307fb_deferred_io(struct fb_info *info,
294 struct list_head *pagelist)
296 ssd1307fb_update_display(info->par);
299 static int ssd1307fb_init(struct ssd1307fb_par *par)
302 u32 precharge, dclk, com_invdir, compins;
303 struct pwm_args pargs;
305 if (par->device_info->need_pwm) {
306 par->pwm = pwm_get(&par->client->dev, NULL);
307 if (IS_ERR(par->pwm)) {
308 dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
309 return PTR_ERR(par->pwm);
313 * FIXME: pwm_apply_args() should be removed when switching to
314 * the atomic PWM API.
316 pwm_apply_args(par->pwm);
318 pwm_get_args(par->pwm, &pargs);
320 par->pwm_period = pargs.period;
322 pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
323 pwm_enable(par->pwm);
325 dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
326 par->pwm->pwm, par->pwm_period);
329 /* Set initial contrast */
330 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
334 ret = ssd1307fb_write_cmd(par->client, par->contrast);
338 /* Set segment re-map */
339 if (par->seg_remap) {
340 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
345 /* Set COM direction */
346 com_invdir = 0xc0 | par->com_invdir << 3;
347 ret = ssd1307fb_write_cmd(par->client, com_invdir);
351 /* Set multiplex ratio value */
352 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
356 ret = ssd1307fb_write_cmd(par->client, par->height - 1);
360 /* set display offset value */
361 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
365 ret = ssd1307fb_write_cmd(par->client, par->com_offset);
369 /* Set clock frequency */
370 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
374 dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4;
375 ret = ssd1307fb_write_cmd(par->client, dclk);
379 /* Set Set Area Color Mode ON/OFF & Low Power Display Mode */
380 if (par->area_color_enable || par->low_power) {
383 ret = ssd1307fb_write_cmd(par->client,
384 SSD1307FB_SET_AREA_COLOR_MODE);
388 mode = (par->area_color_enable ? 0x30 : 0) |
389 (par->low_power ? 5 : 0);
390 ret = ssd1307fb_write_cmd(par->client, mode);
395 /* Set precharge period in number of ticks from the internal clock */
396 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
400 precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4;
401 ret = ssd1307fb_write_cmd(par->client, precharge);
405 /* Set COM pins configuration */
406 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
410 compins = 0x02 | !par->com_seq << 4 | par->com_lrremap << 5;
411 ret = ssd1307fb_write_cmd(par->client, compins);
416 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
420 ret = ssd1307fb_write_cmd(par->client, par->vcomh);
424 /* Turn on the DC-DC Charge Pump */
425 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
429 ret = ssd1307fb_write_cmd(par->client,
430 BIT(4) | (par->device_info->need_chargepump ? BIT(2) : 0));
434 /* Set lookup table */
435 if (par->lookup_table_set) {
438 ret = ssd1307fb_write_cmd(par->client,
439 SSD1307FB_SET_LOOKUP_TABLE);
443 for (i = 0; i < ARRAY_SIZE(par->lookup_table); ++i) {
444 u8 val = par->lookup_table[i];
446 if (val < 31 || val > 63)
447 dev_warn(&par->client->dev,
448 "lookup table index %d value out of range 31 <= %d <= 63\n",
450 ret = ssd1307fb_write_cmd(par->client, val);
456 /* Switch to horizontal addressing mode */
457 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE);
461 ret = ssd1307fb_write_cmd(par->client,
462 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL);
466 /* Set column range */
467 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COL_RANGE);
471 ret = ssd1307fb_write_cmd(par->client, 0x0);
475 ret = ssd1307fb_write_cmd(par->client, par->width - 1);
480 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PAGE_RANGE);
484 ret = ssd1307fb_write_cmd(par->client, par->page_offset);
488 ret = ssd1307fb_write_cmd(par->client,
490 DIV_ROUND_UP(par->height, 8) - 1);
494 /* Clear the screen */
495 ssd1307fb_update_display(par);
497 /* Turn on the display */
498 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
505 static int ssd1307fb_update_bl(struct backlight_device *bdev)
507 struct ssd1307fb_par *par = bl_get_data(bdev);
509 int brightness = bdev->props.brightness;
511 par->contrast = brightness;
513 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
516 ret = ssd1307fb_write_cmd(par->client, par->contrast);
522 static int ssd1307fb_get_brightness(struct backlight_device *bdev)
524 struct ssd1307fb_par *par = bl_get_data(bdev);
526 return par->contrast;
529 static int ssd1307fb_check_fb(struct backlight_device *bdev,
530 struct fb_info *info)
532 return (info->bl_dev == bdev);
535 static const struct backlight_ops ssd1307fb_bl_ops = {
536 .options = BL_CORE_SUSPENDRESUME,
537 .update_status = ssd1307fb_update_bl,
538 .get_brightness = ssd1307fb_get_brightness,
539 .check_fb = ssd1307fb_check_fb,
542 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = {
543 .default_vcomh = 0x34,
544 .default_dclk_div = 1,
545 .default_dclk_frq = 7,
548 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = {
549 .default_vcomh = 0x20,
550 .default_dclk_div = 1,
551 .default_dclk_frq = 8,
552 .need_chargepump = 1,
555 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = {
556 .default_vcomh = 0x20,
557 .default_dclk_div = 2,
558 .default_dclk_frq = 12,
562 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo = {
563 .default_vcomh = 0x34,
564 .default_dclk_div = 1,
565 .default_dclk_frq = 10,
568 static const struct of_device_id ssd1307fb_of_match[] = {
570 .compatible = "solomon,ssd1305fb-i2c",
571 .data = (void *)&ssd1307fb_ssd1305_deviceinfo,
574 .compatible = "solomon,ssd1306fb-i2c",
575 .data = (void *)&ssd1307fb_ssd1306_deviceinfo,
578 .compatible = "solomon,ssd1307fb-i2c",
579 .data = (void *)&ssd1307fb_ssd1307_deviceinfo,
582 .compatible = "solomon,ssd1309fb-i2c",
583 .data = (void *)&ssd1307fb_ssd1309_deviceinfo,
587 MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
589 static int ssd1307fb_probe(struct i2c_client *client,
590 const struct i2c_device_id *id)
592 struct backlight_device *bl;
594 struct fb_info *info;
595 struct device_node *node = client->dev.of_node;
596 struct fb_deferred_io *ssd1307fb_defio;
598 struct ssd1307fb_par *par;
603 dev_err(&client->dev, "No device tree data found!\n");
607 info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
613 par->client = client;
615 par->device_info = of_device_get_match_data(&client->dev);
617 par->reset = devm_gpiod_get_optional(&client->dev, "reset",
619 if (IS_ERR(par->reset)) {
620 dev_err(&client->dev, "failed to get reset gpio: %ld\n",
621 PTR_ERR(par->reset));
622 ret = PTR_ERR(par->reset);
626 par->vbat_reg = devm_regulator_get_optional(&client->dev, "vbat");
627 if (IS_ERR(par->vbat_reg)) {
628 ret = PTR_ERR(par->vbat_reg);
629 if (ret == -ENODEV) {
630 par->vbat_reg = NULL;
632 dev_err(&client->dev, "failed to get VBAT regulator: %d\n",
638 if (of_property_read_u32(node, "solomon,width", &par->width))
641 if (of_property_read_u32(node, "solomon,height", &par->height))
644 if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
645 par->page_offset = 1;
647 if (of_property_read_u32(node, "solomon,com-offset", &par->com_offset))
650 if (of_property_read_u32(node, "solomon,prechargep1", &par->prechargep1))
651 par->prechargep1 = 2;
653 if (of_property_read_u32(node, "solomon,prechargep2", &par->prechargep2))
654 par->prechargep2 = 2;
656 if (!of_property_read_u8_array(node, "solomon,lookup-table",
658 ARRAY_SIZE(par->lookup_table)))
659 par->lookup_table_set = 1;
661 par->seg_remap = !of_property_read_bool(node, "solomon,segment-no-remap");
662 par->com_seq = of_property_read_bool(node, "solomon,com-seq");
663 par->com_lrremap = of_property_read_bool(node, "solomon,com-lrremap");
664 par->com_invdir = of_property_read_bool(node, "solomon,com-invdir");
665 par->area_color_enable =
666 of_property_read_bool(node, "solomon,area-color-enable");
667 par->low_power = of_property_read_bool(node, "solomon,low-power");
670 par->vcomh = par->device_info->default_vcomh;
672 /* Setup display timing */
673 if (of_property_read_u32(node, "solomon,dclk-div", &par->dclk_div))
674 par->dclk_div = par->device_info->default_dclk_div;
675 if (of_property_read_u32(node, "solomon,dclk-frq", &par->dclk_frq))
676 par->dclk_frq = par->device_info->default_dclk_frq;
678 vmem_size = DIV_ROUND_UP(par->width, 8) * par->height;
680 vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
681 get_order(vmem_size));
683 dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
688 ssd1307fb_defio = devm_kzalloc(&client->dev, sizeof(*ssd1307fb_defio),
690 if (!ssd1307fb_defio) {
691 dev_err(&client->dev, "Couldn't allocate deferred io.\n");
696 ssd1307fb_defio->delay = HZ / refreshrate;
697 ssd1307fb_defio->deferred_io = ssd1307fb_deferred_io;
699 info->fbops = &ssd1307fb_ops;
700 info->fix = ssd1307fb_fix;
701 info->fix.line_length = DIV_ROUND_UP(par->width, 8);
702 info->fbdefio = ssd1307fb_defio;
704 info->var = ssd1307fb_var;
705 info->var.xres = par->width;
706 info->var.xres_virtual = par->width;
707 info->var.yres = par->height;
708 info->var.yres_virtual = par->height;
710 info->screen_buffer = vmem;
711 info->fix.smem_start = __pa(vmem);
712 info->fix.smem_len = vmem_size;
714 fb_deferred_io_init(info);
716 i2c_set_clientdata(client, info);
719 /* Reset the screen */
720 gpiod_set_value_cansleep(par->reset, 1);
722 gpiod_set_value_cansleep(par->reset, 0);
727 ret = regulator_enable(par->vbat_reg);
729 dev_err(&client->dev, "failed to enable VBAT: %d\n",
731 goto reset_oled_error;
735 ret = ssd1307fb_init(par);
737 goto regulator_enable_error;
739 ret = register_framebuffer(info);
741 dev_err(&client->dev, "Couldn't register the framebuffer\n");
742 goto panel_init_error;
745 snprintf(bl_name, sizeof(bl_name), "ssd1307fb%d", info->node);
746 bl = backlight_device_register(bl_name, &client->dev, par,
747 &ssd1307fb_bl_ops, NULL);
750 dev_err(&client->dev, "unable to register backlight device: %d\n",
755 bl->props.brightness = par->contrast;
756 bl->props.max_brightness = MAX_CONTRAST;
759 dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
764 unregister_framebuffer(info);
766 if (par->device_info->need_pwm) {
767 pwm_disable(par->pwm);
770 regulator_enable_error:
772 regulator_disable(par->vbat_reg);
774 fb_deferred_io_cleanup(info);
776 framebuffer_release(info);
780 static int ssd1307fb_remove(struct i2c_client *client)
782 struct fb_info *info = i2c_get_clientdata(client);
783 struct ssd1307fb_par *par = info->par;
785 ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_OFF);
787 backlight_device_unregister(info->bl_dev);
789 unregister_framebuffer(info);
790 if (par->device_info->need_pwm) {
791 pwm_disable(par->pwm);
794 fb_deferred_io_cleanup(info);
795 __free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len));
796 framebuffer_release(info);
801 static const struct i2c_device_id ssd1307fb_i2c_id[] = {
808 MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
810 static struct i2c_driver ssd1307fb_driver = {
811 .probe = ssd1307fb_probe,
812 .remove = ssd1307fb_remove,
813 .id_table = ssd1307fb_i2c_id,
816 .of_match_table = ssd1307fb_of_match,
820 module_i2c_driver(ssd1307fb_driver);
822 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
823 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
824 MODULE_LICENSE("GPL");