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);
51 struct ssd1307fb_deviceinfo
{
59 struct ssd1307fb_par
{
60 unsigned area_color_enable
: 1;
61 unsigned com_invdir
: 1;
62 unsigned com_lrremap
: 1;
64 unsigned lookup_table_set
: 1;
65 unsigned low_power
: 1;
66 unsigned seg_remap
: 1;
71 const struct ssd1307fb_deviceinfo
*device_info
;
72 struct i2c_client
*client
;
80 struct pwm_device
*pwm
;
81 struct gpio_desc
*reset
;
82 struct regulator
*vbat_reg
;
85 /* Cached address ranges */
92 struct ssd1307fb_array
{
97 static const struct fb_fix_screeninfo ssd1307fb_fix
= {
98 .id
= "Solomon SSD1307",
99 .type
= FB_TYPE_PACKED_PIXELS
,
100 .visual
= FB_VISUAL_MONO10
,
104 .accel
= FB_ACCEL_NONE
,
107 static const struct fb_var_screeninfo ssd1307fb_var
= {
109 .red
= { .length
= 1 },
110 .green
= { .length
= 1 },
111 .blue
= { .length
= 1 },
114 static struct ssd1307fb_array
*ssd1307fb_alloc_array(u32 len
, u8 type
)
116 struct ssd1307fb_array
*array
;
118 array
= kzalloc(sizeof(struct ssd1307fb_array
) + len
, GFP_KERNEL
);
127 static int ssd1307fb_write_array(struct i2c_client
*client
,
128 struct ssd1307fb_array
*array
, u32 len
)
132 len
+= sizeof(struct ssd1307fb_array
);
134 ret
= i2c_master_send(client
, (u8
*)array
, len
);
136 dev_err(&client
->dev
, "Couldn't send I2C command.\n");
143 static inline int ssd1307fb_write_cmd(struct i2c_client
*client
, u8 cmd
)
145 struct ssd1307fb_array
*array
;
148 array
= ssd1307fb_alloc_array(1, SSD1307FB_COMMAND
);
152 array
->data
[0] = cmd
;
154 ret
= ssd1307fb_write_array(client
, array
, 1);
160 static int ssd1307fb_set_col_range(struct ssd1307fb_par
*par
, u8 col_start
,
163 u8 col_end
= col_start
+ cols
- 1;
166 if (col_start
== par
->col_start
&& col_end
== par
->col_end
)
169 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COL_RANGE
);
173 ret
= ssd1307fb_write_cmd(par
->client
, col_start
);
177 ret
= ssd1307fb_write_cmd(par
->client
, col_end
);
181 par
->col_start
= col_start
;
182 par
->col_end
= col_end
;
186 static int ssd1307fb_set_page_range(struct ssd1307fb_par
*par
, u8 page_start
,
189 u8 page_end
= page_start
+ pages
- 1;
192 if (page_start
== par
->page_start
&& page_end
== par
->page_end
)
195 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PAGE_RANGE
);
199 ret
= ssd1307fb_write_cmd(par
->client
, page_start
);
203 ret
= ssd1307fb_write_cmd(par
->client
, page_end
);
207 par
->page_start
= page_start
;
208 par
->page_end
= page_end
;
212 static int ssd1307fb_update_rect(struct ssd1307fb_par
*par
, unsigned int x
,
213 unsigned int y
, unsigned int width
,
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);
223 array
= ssd1307fb_alloc_array(width
* pages
, SSD1307FB_DATA
);
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.
235 * Representation of the screen, assuming it is 5 bits
236 * wide. Each letter-number combination is a bit that controls
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
256 ret
= ssd1307fb_set_col_range(par
, par
->col_offset
+ x
, width
);
260 ret
= ssd1307fb_set_page_range(par
, par
->page_offset
+ y
/ 8, pages
);
264 for (i
= y
/ 8; i
< y
/ 8 + pages
; i
++) {
267 /* Last page may be partial */
268 if (8 * (i
+ 1) > par
->height
)
270 for (j
= x
; j
< x
+ width
; j
++) {
273 for (k
= 0; k
< m
; k
++) {
274 u8 byte
= vmem
[(8 * i
+ k
) * line_length
+
276 u8 bit
= (byte
>> (j
% 8)) & 1;
279 array
->data
[array_idx
++] = data
;
283 ret
= ssd1307fb_write_array(par
->client
, array
, width
* pages
);
290 static int ssd1307fb_update_display(struct ssd1307fb_par
*par
)
292 return ssd1307fb_update_rect(par
, 0, 0, par
->width
, par
->height
);
295 static int ssd1307fb_blank(int blank_mode
, struct fb_info
*info
)
297 struct ssd1307fb_par
*par
= info
->par
;
299 if (blank_mode
!= FB_BLANK_UNBLANK
)
300 return ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_OFF
);
302 return ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
305 static void ssd1307fb_defio_damage_range(struct fb_info
*info
, off_t off
, size_t len
)
307 struct ssd1307fb_par
*par
= info
->par
;
309 ssd1307fb_update_display(par
);
312 static void ssd1307fb_defio_damage_area(struct fb_info
*info
, u32 x
, u32 y
,
313 u32 width
, u32 height
)
315 struct ssd1307fb_par
*par
= info
->par
;
317 ssd1307fb_update_rect(par
, x
, y
, width
, height
);
320 FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(ssd1307fb
,
321 ssd1307fb_defio_damage_range
,
322 ssd1307fb_defio_damage_area
)
324 static const struct fb_ops ssd1307fb_ops
= {
325 .owner
= THIS_MODULE
,
326 FB_DEFAULT_DEFERRED_OPS(ssd1307fb
),
327 .fb_blank
= ssd1307fb_blank
,
330 static void ssd1307fb_deferred_io(struct fb_info
*info
, struct list_head
*pagereflist
)
332 ssd1307fb_update_display(info
->par
);
335 static int ssd1307fb_init(struct ssd1307fb_par
*par
)
337 struct pwm_state pwmstate
;
339 u32 precharge
, dclk
, com_invdir
, compins
;
341 if (par
->device_info
->need_pwm
) {
342 par
->pwm
= pwm_get(&par
->client
->dev
, NULL
);
343 if (IS_ERR(par
->pwm
)) {
344 dev_err(&par
->client
->dev
, "Could not get PWM from device tree!\n");
345 return PTR_ERR(par
->pwm
);
348 pwm_init_state(par
->pwm
, &pwmstate
);
349 pwm_set_relative_duty_cycle(&pwmstate
, 50, 100);
350 pwm_apply_might_sleep(par
->pwm
, &pwmstate
);
353 pwm_enable(par
->pwm
);
355 dev_dbg(&par
->client
->dev
, "Using PWM %s with a %lluns period.\n",
356 par
->pwm
->label
, pwm_get_period(par
->pwm
));
359 /* Set initial contrast */
360 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
364 ret
= ssd1307fb_write_cmd(par
->client
, par
->contrast
);
368 /* Set segment re-map */
369 if (par
->seg_remap
) {
370 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SEG_REMAP_ON
);
375 /* Set COM direction */
376 com_invdir
= 0xc0 | par
->com_invdir
<< 3;
377 ret
= ssd1307fb_write_cmd(par
->client
, com_invdir
);
381 /* Set multiplex ratio value */
382 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_MULTIPLEX_RATIO
);
386 ret
= ssd1307fb_write_cmd(par
->client
, par
->height
- 1);
390 /* set display offset value */
391 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_DISPLAY_OFFSET
);
395 ret
= ssd1307fb_write_cmd(par
->client
, par
->com_offset
);
399 /* Set clock frequency */
400 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_CLOCK_FREQ
);
404 dclk
= ((par
->dclk_div
- 1) & 0xf) | (par
->dclk_frq
& 0xf) << 4;
405 ret
= ssd1307fb_write_cmd(par
->client
, dclk
);
409 /* Set Area Color Mode ON/OFF & Low Power Display Mode */
410 if (par
->area_color_enable
|| par
->low_power
) {
413 ret
= ssd1307fb_write_cmd(par
->client
,
414 SSD1307FB_SET_AREA_COLOR_MODE
);
418 mode
= (par
->area_color_enable
? 0x30 : 0) |
419 (par
->low_power
? 5 : 0);
420 ret
= ssd1307fb_write_cmd(par
->client
, mode
);
425 /* Set precharge period in number of ticks from the internal clock */
426 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PRECHARGE_PERIOD
);
430 precharge
= (par
->prechargep1
& 0xf) | (par
->prechargep2
& 0xf) << 4;
431 ret
= ssd1307fb_write_cmd(par
->client
, precharge
);
435 /* Set COM pins configuration */
436 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COM_PINS_CONFIG
);
440 compins
= 0x02 | !par
->com_seq
<< 4 | par
->com_lrremap
<< 5;
441 ret
= ssd1307fb_write_cmd(par
->client
, compins
);
446 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_VCOMH
);
450 ret
= ssd1307fb_write_cmd(par
->client
, par
->vcomh
);
454 /* Turn on the DC-DC Charge Pump */
455 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CHARGE_PUMP
);
459 ret
= ssd1307fb_write_cmd(par
->client
,
460 BIT(4) | (par
->device_info
->need_chargepump
? BIT(2) : 0));
464 /* Set lookup table */
465 if (par
->lookup_table_set
) {
468 ret
= ssd1307fb_write_cmd(par
->client
,
469 SSD1307FB_SET_LOOKUP_TABLE
);
473 for (i
= 0; i
< ARRAY_SIZE(par
->lookup_table
); ++i
) {
474 u8 val
= par
->lookup_table
[i
];
476 if (val
< 31 || val
> 63)
477 dev_warn(&par
->client
->dev
,
478 "lookup table index %d value out of range 31 <= %d <= 63\n",
480 ret
= ssd1307fb_write_cmd(par
->client
, val
);
486 /* Switch to horizontal addressing mode */
487 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_ADDRESS_MODE
);
491 ret
= ssd1307fb_write_cmd(par
->client
,
492 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL
);
496 /* Clear the screen */
497 ret
= ssd1307fb_update_display(par
);
501 /* Turn on the display */
502 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
509 static int ssd1307fb_update_bl(struct backlight_device
*bdev
)
511 struct ssd1307fb_par
*par
= bl_get_data(bdev
);
513 int brightness
= bdev
->props
.brightness
;
515 par
->contrast
= brightness
;
517 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
520 ret
= ssd1307fb_write_cmd(par
->client
, par
->contrast
);
526 static int ssd1307fb_get_brightness(struct backlight_device
*bdev
)
528 struct ssd1307fb_par
*par
= bl_get_data(bdev
);
530 return par
->contrast
;
533 static const struct backlight_ops ssd1307fb_bl_ops
= {
534 .options
= BL_CORE_SUSPENDRESUME
,
535 .update_status
= ssd1307fb_update_bl
,
536 .get_brightness
= ssd1307fb_get_brightness
,
539 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo
= {
540 .default_vcomh
= 0x34,
541 .default_dclk_div
= 1,
542 .default_dclk_frq
= 7,
545 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo
= {
546 .default_vcomh
= 0x20,
547 .default_dclk_div
= 1,
548 .default_dclk_frq
= 8,
549 .need_chargepump
= 1,
552 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo
= {
553 .default_vcomh
= 0x20,
554 .default_dclk_div
= 2,
555 .default_dclk_frq
= 12,
559 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo
= {
560 .default_vcomh
= 0x34,
561 .default_dclk_div
= 1,
562 .default_dclk_frq
= 10,
565 static const struct of_device_id ssd1307fb_of_match
[] = {
567 .compatible
= "solomon,ssd1305fb-i2c",
568 .data
= (void *)&ssd1307fb_ssd1305_deviceinfo
,
571 .compatible
= "solomon,ssd1306fb-i2c",
572 .data
= (void *)&ssd1307fb_ssd1306_deviceinfo
,
575 .compatible
= "solomon,ssd1307fb-i2c",
576 .data
= (void *)&ssd1307fb_ssd1307_deviceinfo
,
579 .compatible
= "solomon,ssd1309fb-i2c",
580 .data
= (void *)&ssd1307fb_ssd1309_deviceinfo
,
584 MODULE_DEVICE_TABLE(of
, ssd1307fb_of_match
);
586 static int ssd1307fb_probe(struct i2c_client
*client
)
588 struct device
*dev
= &client
->dev
;
589 struct backlight_device
*bl
;
590 struct fb_info
*info
;
591 struct fb_deferred_io
*ssd1307fb_defio
;
593 struct ssd1307fb_par
*par
;
597 info
= framebuffer_alloc(sizeof(struct ssd1307fb_par
), dev
);
603 par
->client
= client
;
605 par
->device_info
= device_get_match_data(dev
);
607 par
->reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
608 if (IS_ERR(par
->reset
)) {
609 ret
= dev_err_probe(dev
, PTR_ERR(par
->reset
),
610 "failed to get reset gpio\n");
614 par
->vbat_reg
= devm_regulator_get_optional(dev
, "vbat");
615 if (IS_ERR(par
->vbat_reg
)) {
616 ret
= PTR_ERR(par
->vbat_reg
);
617 if (ret
== -ENODEV
) {
618 par
->vbat_reg
= NULL
;
620 dev_err_probe(dev
, ret
, "failed to get VBAT regulator\n");
625 if (device_property_read_u32(dev
, "solomon,width", &par
->width
))
628 if (device_property_read_u32(dev
, "solomon,height", &par
->height
))
631 if (device_property_read_u32(dev
, "solomon,page-offset", &par
->page_offset
))
632 par
->page_offset
= 1;
634 if (device_property_read_u32(dev
, "solomon,col-offset", &par
->col_offset
))
637 if (device_property_read_u32(dev
, "solomon,com-offset", &par
->com_offset
))
640 if (device_property_read_u32(dev
, "solomon,prechargep1", &par
->prechargep1
))
641 par
->prechargep1
= 2;
643 if (device_property_read_u32(dev
, "solomon,prechargep2", &par
->prechargep2
))
644 par
->prechargep2
= 2;
646 if (!device_property_read_u8_array(dev
, "solomon,lookup-table",
648 ARRAY_SIZE(par
->lookup_table
)))
649 par
->lookup_table_set
= 1;
651 par
->seg_remap
= !device_property_read_bool(dev
, "solomon,segment-no-remap");
652 par
->com_seq
= device_property_read_bool(dev
, "solomon,com-seq");
653 par
->com_lrremap
= device_property_read_bool(dev
, "solomon,com-lrremap");
654 par
->com_invdir
= device_property_read_bool(dev
, "solomon,com-invdir");
655 par
->area_color_enable
=
656 device_property_read_bool(dev
, "solomon,area-color-enable");
657 par
->low_power
= device_property_read_bool(dev
, "solomon,low-power");
660 par
->vcomh
= par
->device_info
->default_vcomh
;
662 /* Setup display timing */
663 if (device_property_read_u32(dev
, "solomon,dclk-div", &par
->dclk_div
))
664 par
->dclk_div
= par
->device_info
->default_dclk_div
;
665 if (device_property_read_u32(dev
, "solomon,dclk-frq", &par
->dclk_frq
))
666 par
->dclk_frq
= par
->device_info
->default_dclk_frq
;
668 vmem_size
= DIV_ROUND_UP(par
->width
, 8) * par
->height
;
670 vmem
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
671 get_order(vmem_size
));
673 dev_err(dev
, "Couldn't allocate graphical memory.\n");
678 ssd1307fb_defio
= devm_kzalloc(dev
, sizeof(*ssd1307fb_defio
),
680 if (!ssd1307fb_defio
) {
681 dev_err(dev
, "Couldn't allocate deferred io.\n");
686 ssd1307fb_defio
->delay
= HZ
/ refreshrate
;
687 ssd1307fb_defio
->deferred_io
= ssd1307fb_deferred_io
;
689 info
->fbops
= &ssd1307fb_ops
;
690 info
->fix
= ssd1307fb_fix
;
691 info
->fix
.line_length
= DIV_ROUND_UP(par
->width
, 8);
692 info
->fbdefio
= ssd1307fb_defio
;
694 info
->var
= ssd1307fb_var
;
695 info
->var
.xres
= par
->width
;
696 info
->var
.xres_virtual
= par
->width
;
697 info
->var
.yres
= par
->height
;
698 info
->var
.yres_virtual
= par
->height
;
700 info
->screen_buffer
= vmem
;
701 info
->fix
.smem_start
= __pa(vmem
);
702 info
->fix
.smem_len
= vmem_size
;
704 fb_deferred_io_init(info
);
706 i2c_set_clientdata(client
, info
);
709 /* Reset the screen */
710 gpiod_set_value_cansleep(par
->reset
, 1);
712 gpiod_set_value_cansleep(par
->reset
, 0);
717 ret
= regulator_enable(par
->vbat_reg
);
719 dev_err(dev
, "failed to enable VBAT: %d\n", ret
);
720 goto reset_oled_error
;
724 ret
= ssd1307fb_init(par
);
726 goto regulator_enable_error
;
728 bl
= backlight_device_register("ssd1307fb-bl", dev
, par
, &ssd1307fb_bl_ops
,
732 dev_err(dev
, "unable to register backlight device: %d\n", ret
);
733 goto panel_init_error
;
737 ret
= register_framebuffer(info
);
739 dev_err(dev
, "Couldn't register the framebuffer\n");
743 bl
->props
.brightness
= par
->contrast
;
744 bl
->props
.max_brightness
= MAX_CONTRAST
;
746 dev_info(dev
, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info
->node
, info
->fix
.id
, vmem_size
);
751 backlight_device_unregister(bl
);
753 pwm_disable(par
->pwm
);
755 regulator_enable_error
:
757 regulator_disable(par
->vbat_reg
);
759 fb_deferred_io_cleanup(info
);
761 framebuffer_release(info
);
765 static void ssd1307fb_remove(struct i2c_client
*client
)
767 struct fb_info
*info
= i2c_get_clientdata(client
);
768 struct ssd1307fb_par
*par
= info
->par
;
770 ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_OFF
);
772 backlight_device_unregister(info
->bl_dev
);
774 unregister_framebuffer(info
);
775 pwm_disable(par
->pwm
);
778 regulator_disable(par
->vbat_reg
);
779 fb_deferred_io_cleanup(info
);
780 __free_pages(__va(info
->fix
.smem_start
), get_order(info
->fix
.smem_len
));
781 framebuffer_release(info
);
784 static const struct i2c_device_id ssd1307fb_i2c_id
[] = {
791 MODULE_DEVICE_TABLE(i2c
, ssd1307fb_i2c_id
);
793 static struct i2c_driver ssd1307fb_driver
= {
794 .probe
= ssd1307fb_probe
,
795 .remove
= ssd1307fb_remove
,
796 .id_table
= ssd1307fb_i2c_id
,
799 .of_match_table
= ssd1307fb_of_match
,
803 module_i2c_driver(ssd1307fb_driver
);
805 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
806 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
807 MODULE_LICENSE("GPL");