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
;
87 struct ssd1307fb_array
{
92 static const struct fb_fix_screeninfo ssd1307fb_fix
= {
93 .id
= "Solomon SSD1307",
94 .type
= FB_TYPE_PACKED_PIXELS
,
95 .visual
= FB_VISUAL_MONO10
,
99 .accel
= FB_ACCEL_NONE
,
102 static const struct fb_var_screeninfo ssd1307fb_var
= {
104 .red
= { .length
= 1 },
105 .green
= { .length
= 1 },
106 .blue
= { .length
= 1 },
109 static struct ssd1307fb_array
*ssd1307fb_alloc_array(u32 len
, u8 type
)
111 struct ssd1307fb_array
*array
;
113 array
= kzalloc(sizeof(struct ssd1307fb_array
) + len
, GFP_KERNEL
);
122 static int ssd1307fb_write_array(struct i2c_client
*client
,
123 struct ssd1307fb_array
*array
, u32 len
)
127 len
+= sizeof(struct ssd1307fb_array
);
129 ret
= i2c_master_send(client
, (u8
*)array
, len
);
131 dev_err(&client
->dev
, "Couldn't send I2C command.\n");
138 static inline int ssd1307fb_write_cmd(struct i2c_client
*client
, u8 cmd
)
140 struct ssd1307fb_array
*array
;
143 array
= ssd1307fb_alloc_array(1, SSD1307FB_COMMAND
);
147 array
->data
[0] = cmd
;
149 ret
= ssd1307fb_write_array(client
, array
, 1);
155 static void ssd1307fb_update_display(struct ssd1307fb_par
*par
)
157 struct ssd1307fb_array
*array
;
158 u8
*vmem
= par
->info
->screen_buffer
;
159 unsigned int line_length
= par
->info
->fix
.line_length
;
160 unsigned int pages
= DIV_ROUND_UP(par
->height
, 8);
163 array
= ssd1307fb_alloc_array(par
->width
* pages
, SSD1307FB_DATA
);
168 * The screen is divided in pages, each having a height of 8
169 * pixels, and the width of the screen. When sending a byte of
170 * data to the controller, it gives the 8 bits for the current
171 * column. I.e, the first byte are the 8 bits of the first
172 * column, then the 8 bits for the second column, etc.
175 * Representation of the screen, assuming it is 5 bits
176 * wide. Each letter-number combination is a bit that controls
188 * If you want to update this screen, you need to send 5 bytes:
189 * (1) A0 B0 C0 D0 E0 F0 G0 H0
190 * (2) A1 B1 C1 D1 E1 F1 G1 H1
191 * (3) A2 B2 C2 D2 E2 F2 G2 H2
192 * (4) A3 B3 C3 D3 E3 F3 G3 H3
193 * (5) A4 B4 C4 D4 E4 F4 G4 H4
196 for (i
= 0; i
< pages
; i
++) {
197 for (j
= 0; j
< par
->width
; j
++) {
199 u32 array_idx
= i
* par
->width
+ j
;
200 array
->data
[array_idx
] = 0;
201 /* Last page may be partial */
202 if (i
+ 1 == pages
&& par
->height
% 8)
204 for (k
= 0; k
< m
; k
++) {
205 u8 byte
= vmem
[(8 * i
+ k
) * line_length
+
207 u8 bit
= (byte
>> (j
% 8)) & 1;
208 array
->data
[array_idx
] |= bit
<< k
;
213 ssd1307fb_write_array(par
->client
, array
, par
->width
* pages
);
218 static ssize_t
ssd1307fb_write(struct fb_info
*info
, const char __user
*buf
,
219 size_t count
, loff_t
*ppos
)
221 struct ssd1307fb_par
*par
= info
->par
;
222 unsigned long total_size
;
223 unsigned long p
= *ppos
;
226 total_size
= info
->fix
.smem_len
;
231 if (count
+ p
> total_size
)
232 count
= total_size
- p
;
237 dst
= info
->screen_buffer
+ p
;
239 if (copy_from_user(dst
, buf
, count
))
242 ssd1307fb_update_display(par
);
249 static int ssd1307fb_blank(int blank_mode
, struct fb_info
*info
)
251 struct ssd1307fb_par
*par
= info
->par
;
253 if (blank_mode
!= FB_BLANK_UNBLANK
)
254 return ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_OFF
);
256 return ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
259 static void ssd1307fb_fillrect(struct fb_info
*info
, const struct fb_fillrect
*rect
)
261 struct ssd1307fb_par
*par
= info
->par
;
262 sys_fillrect(info
, rect
);
263 ssd1307fb_update_display(par
);
266 static void ssd1307fb_copyarea(struct fb_info
*info
, const struct fb_copyarea
*area
)
268 struct ssd1307fb_par
*par
= info
->par
;
269 sys_copyarea(info
, area
);
270 ssd1307fb_update_display(par
);
273 static void ssd1307fb_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
275 struct ssd1307fb_par
*par
= info
->par
;
276 sys_imageblit(info
, image
);
277 ssd1307fb_update_display(par
);
280 static const struct fb_ops ssd1307fb_ops
= {
281 .owner
= THIS_MODULE
,
282 .fb_read
= fb_sys_read
,
283 .fb_write
= ssd1307fb_write
,
284 .fb_blank
= ssd1307fb_blank
,
285 .fb_fillrect
= ssd1307fb_fillrect
,
286 .fb_copyarea
= ssd1307fb_copyarea
,
287 .fb_imageblit
= ssd1307fb_imageblit
,
290 static void ssd1307fb_deferred_io(struct fb_info
*info
,
291 struct list_head
*pagelist
)
293 ssd1307fb_update_display(info
->par
);
296 static int ssd1307fb_init(struct ssd1307fb_par
*par
)
298 struct pwm_state pwmstate
;
300 u32 precharge
, dclk
, com_invdir
, compins
;
302 if (par
->device_info
->need_pwm
) {
303 par
->pwm
= pwm_get(&par
->client
->dev
, NULL
);
304 if (IS_ERR(par
->pwm
)) {
305 dev_err(&par
->client
->dev
, "Could not get PWM from device tree!\n");
306 return PTR_ERR(par
->pwm
);
309 pwm_init_state(par
->pwm
, &pwmstate
);
310 pwm_set_relative_duty_cycle(&pwmstate
, 50, 100);
311 pwm_apply_state(par
->pwm
, &pwmstate
);
314 pwm_enable(par
->pwm
);
316 dev_dbg(&par
->client
->dev
, "Using PWM%d with a %lluns period.\n",
317 par
->pwm
->pwm
, pwm_get_period(par
->pwm
));
320 /* Set initial contrast */
321 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
325 ret
= ssd1307fb_write_cmd(par
->client
, par
->contrast
);
329 /* Set segment re-map */
330 if (par
->seg_remap
) {
331 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SEG_REMAP_ON
);
336 /* Set COM direction */
337 com_invdir
= 0xc0 | par
->com_invdir
<< 3;
338 ret
= ssd1307fb_write_cmd(par
->client
, com_invdir
);
342 /* Set multiplex ratio value */
343 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_MULTIPLEX_RATIO
);
347 ret
= ssd1307fb_write_cmd(par
->client
, par
->height
- 1);
351 /* set display offset value */
352 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_DISPLAY_OFFSET
);
356 ret
= ssd1307fb_write_cmd(par
->client
, par
->com_offset
);
360 /* Set clock frequency */
361 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_CLOCK_FREQ
);
365 dclk
= ((par
->dclk_div
- 1) & 0xf) | (par
->dclk_frq
& 0xf) << 4;
366 ret
= ssd1307fb_write_cmd(par
->client
, dclk
);
370 /* Set Set Area Color Mode ON/OFF & Low Power Display Mode */
371 if (par
->area_color_enable
|| par
->low_power
) {
374 ret
= ssd1307fb_write_cmd(par
->client
,
375 SSD1307FB_SET_AREA_COLOR_MODE
);
379 mode
= (par
->area_color_enable
? 0x30 : 0) |
380 (par
->low_power
? 5 : 0);
381 ret
= ssd1307fb_write_cmd(par
->client
, mode
);
386 /* Set precharge period in number of ticks from the internal clock */
387 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PRECHARGE_PERIOD
);
391 precharge
= (par
->prechargep1
& 0xf) | (par
->prechargep2
& 0xf) << 4;
392 ret
= ssd1307fb_write_cmd(par
->client
, precharge
);
396 /* Set COM pins configuration */
397 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COM_PINS_CONFIG
);
401 compins
= 0x02 | !par
->com_seq
<< 4 | par
->com_lrremap
<< 5;
402 ret
= ssd1307fb_write_cmd(par
->client
, compins
);
407 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_VCOMH
);
411 ret
= ssd1307fb_write_cmd(par
->client
, par
->vcomh
);
415 /* Turn on the DC-DC Charge Pump */
416 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CHARGE_PUMP
);
420 ret
= ssd1307fb_write_cmd(par
->client
,
421 BIT(4) | (par
->device_info
->need_chargepump
? BIT(2) : 0));
425 /* Set lookup table */
426 if (par
->lookup_table_set
) {
429 ret
= ssd1307fb_write_cmd(par
->client
,
430 SSD1307FB_SET_LOOKUP_TABLE
);
434 for (i
= 0; i
< ARRAY_SIZE(par
->lookup_table
); ++i
) {
435 u8 val
= par
->lookup_table
[i
];
437 if (val
< 31 || val
> 63)
438 dev_warn(&par
->client
->dev
,
439 "lookup table index %d value out of range 31 <= %d <= 63\n",
441 ret
= ssd1307fb_write_cmd(par
->client
, val
);
447 /* Switch to horizontal addressing mode */
448 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_ADDRESS_MODE
);
452 ret
= ssd1307fb_write_cmd(par
->client
,
453 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL
);
457 /* Set column range */
458 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COL_RANGE
);
462 ret
= ssd1307fb_write_cmd(par
->client
, par
->col_offset
);
466 ret
= ssd1307fb_write_cmd(par
->client
, par
->col_offset
+ par
->width
- 1);
471 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PAGE_RANGE
);
475 ret
= ssd1307fb_write_cmd(par
->client
, par
->page_offset
);
479 ret
= ssd1307fb_write_cmd(par
->client
,
481 DIV_ROUND_UP(par
->height
, 8) - 1);
485 /* Clear the screen */
486 ssd1307fb_update_display(par
);
488 /* Turn on the display */
489 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
496 static int ssd1307fb_update_bl(struct backlight_device
*bdev
)
498 struct ssd1307fb_par
*par
= bl_get_data(bdev
);
500 int brightness
= bdev
->props
.brightness
;
502 par
->contrast
= brightness
;
504 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
507 ret
= ssd1307fb_write_cmd(par
->client
, par
->contrast
);
513 static int ssd1307fb_get_brightness(struct backlight_device
*bdev
)
515 struct ssd1307fb_par
*par
= bl_get_data(bdev
);
517 return par
->contrast
;
520 static int ssd1307fb_check_fb(struct backlight_device
*bdev
,
521 struct fb_info
*info
)
523 return (info
->bl_dev
== bdev
);
526 static const struct backlight_ops ssd1307fb_bl_ops
= {
527 .options
= BL_CORE_SUSPENDRESUME
,
528 .update_status
= ssd1307fb_update_bl
,
529 .get_brightness
= ssd1307fb_get_brightness
,
530 .check_fb
= ssd1307fb_check_fb
,
533 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo
= {
534 .default_vcomh
= 0x34,
535 .default_dclk_div
= 1,
536 .default_dclk_frq
= 7,
539 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo
= {
540 .default_vcomh
= 0x20,
541 .default_dclk_div
= 1,
542 .default_dclk_frq
= 8,
543 .need_chargepump
= 1,
546 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo
= {
547 .default_vcomh
= 0x20,
548 .default_dclk_div
= 2,
549 .default_dclk_frq
= 12,
553 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo
= {
554 .default_vcomh
= 0x34,
555 .default_dclk_div
= 1,
556 .default_dclk_frq
= 10,
559 static const struct of_device_id ssd1307fb_of_match
[] = {
561 .compatible
= "solomon,ssd1305fb-i2c",
562 .data
= (void *)&ssd1307fb_ssd1305_deviceinfo
,
565 .compatible
= "solomon,ssd1306fb-i2c",
566 .data
= (void *)&ssd1307fb_ssd1306_deviceinfo
,
569 .compatible
= "solomon,ssd1307fb-i2c",
570 .data
= (void *)&ssd1307fb_ssd1307_deviceinfo
,
573 .compatible
= "solomon,ssd1309fb-i2c",
574 .data
= (void *)&ssd1307fb_ssd1309_deviceinfo
,
578 MODULE_DEVICE_TABLE(of
, ssd1307fb_of_match
);
580 static int ssd1307fb_probe(struct i2c_client
*client
)
582 struct device
*dev
= &client
->dev
;
583 struct backlight_device
*bl
;
585 struct fb_info
*info
;
586 struct fb_deferred_io
*ssd1307fb_defio
;
588 struct ssd1307fb_par
*par
;
592 info
= framebuffer_alloc(sizeof(struct ssd1307fb_par
), dev
);
598 par
->client
= client
;
600 par
->device_info
= device_get_match_data(dev
);
602 par
->reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
603 if (IS_ERR(par
->reset
)) {
604 dev_err(dev
, "failed to get reset gpio: %ld\n",
605 PTR_ERR(par
->reset
));
606 ret
= PTR_ERR(par
->reset
);
610 par
->vbat_reg
= devm_regulator_get_optional(dev
, "vbat");
611 if (IS_ERR(par
->vbat_reg
)) {
612 ret
= PTR_ERR(par
->vbat_reg
);
613 if (ret
== -ENODEV
) {
614 par
->vbat_reg
= NULL
;
616 dev_err(dev
, "failed to get VBAT regulator: %d\n", ret
);
621 if (device_property_read_u32(dev
, "solomon,width", &par
->width
))
624 if (device_property_read_u32(dev
, "solomon,height", &par
->height
))
627 if (device_property_read_u32(dev
, "solomon,page-offset", &par
->page_offset
))
628 par
->page_offset
= 1;
630 if (device_property_read_u32(dev
, "solomon,col-offset", &par
->col_offset
))
633 if (device_property_read_u32(dev
, "solomon,com-offset", &par
->com_offset
))
636 if (device_property_read_u32(dev
, "solomon,prechargep1", &par
->prechargep1
))
637 par
->prechargep1
= 2;
639 if (device_property_read_u32(dev
, "solomon,prechargep2", &par
->prechargep2
))
640 par
->prechargep2
= 2;
642 if (!device_property_read_u8_array(dev
, "solomon,lookup-table",
644 ARRAY_SIZE(par
->lookup_table
)))
645 par
->lookup_table_set
= 1;
647 par
->seg_remap
= !device_property_read_bool(dev
, "solomon,segment-no-remap");
648 par
->com_seq
= device_property_read_bool(dev
, "solomon,com-seq");
649 par
->com_lrremap
= device_property_read_bool(dev
, "solomon,com-lrremap");
650 par
->com_invdir
= device_property_read_bool(dev
, "solomon,com-invdir");
651 par
->area_color_enable
=
652 device_property_read_bool(dev
, "solomon,area-color-enable");
653 par
->low_power
= device_property_read_bool(dev
, "solomon,low-power");
656 par
->vcomh
= par
->device_info
->default_vcomh
;
658 /* Setup display timing */
659 if (device_property_read_u32(dev
, "solomon,dclk-div", &par
->dclk_div
))
660 par
->dclk_div
= par
->device_info
->default_dclk_div
;
661 if (device_property_read_u32(dev
, "solomon,dclk-frq", &par
->dclk_frq
))
662 par
->dclk_frq
= par
->device_info
->default_dclk_frq
;
664 vmem_size
= DIV_ROUND_UP(par
->width
, 8) * par
->height
;
666 vmem
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
667 get_order(vmem_size
));
669 dev_err(dev
, "Couldn't allocate graphical memory.\n");
674 ssd1307fb_defio
= devm_kzalloc(dev
, sizeof(*ssd1307fb_defio
),
676 if (!ssd1307fb_defio
) {
677 dev_err(dev
, "Couldn't allocate deferred io.\n");
682 ssd1307fb_defio
->delay
= HZ
/ refreshrate
;
683 ssd1307fb_defio
->deferred_io
= ssd1307fb_deferred_io
;
685 info
->fbops
= &ssd1307fb_ops
;
686 info
->fix
= ssd1307fb_fix
;
687 info
->fix
.line_length
= DIV_ROUND_UP(par
->width
, 8);
688 info
->fbdefio
= ssd1307fb_defio
;
690 info
->var
= ssd1307fb_var
;
691 info
->var
.xres
= par
->width
;
692 info
->var
.xres_virtual
= par
->width
;
693 info
->var
.yres
= par
->height
;
694 info
->var
.yres_virtual
= par
->height
;
696 info
->screen_buffer
= vmem
;
697 info
->fix
.smem_start
= __pa(vmem
);
698 info
->fix
.smem_len
= vmem_size
;
700 fb_deferred_io_init(info
);
702 i2c_set_clientdata(client
, info
);
705 /* Reset the screen */
706 gpiod_set_value_cansleep(par
->reset
, 1);
708 gpiod_set_value_cansleep(par
->reset
, 0);
713 ret
= regulator_enable(par
->vbat_reg
);
715 dev_err(dev
, "failed to enable VBAT: %d\n", ret
);
716 goto reset_oled_error
;
720 ret
= ssd1307fb_init(par
);
722 goto regulator_enable_error
;
724 ret
= register_framebuffer(info
);
726 dev_err(dev
, "Couldn't register the framebuffer\n");
727 goto panel_init_error
;
730 snprintf(bl_name
, sizeof(bl_name
), "ssd1307fb%d", info
->node
);
731 bl
= backlight_device_register(bl_name
, dev
, par
, &ssd1307fb_bl_ops
,
735 dev_err(dev
, "unable to register backlight device: %d\n", ret
);
739 bl
->props
.brightness
= par
->contrast
;
740 bl
->props
.max_brightness
= MAX_CONTRAST
;
743 dev_info(dev
, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info
->node
, info
->fix
.id
, vmem_size
);
748 unregister_framebuffer(info
);
750 if (par
->device_info
->need_pwm
) {
751 pwm_disable(par
->pwm
);
754 regulator_enable_error
:
756 regulator_disable(par
->vbat_reg
);
758 fb_deferred_io_cleanup(info
);
760 framebuffer_release(info
);
764 static int ssd1307fb_remove(struct i2c_client
*client
)
766 struct fb_info
*info
= i2c_get_clientdata(client
);
767 struct ssd1307fb_par
*par
= info
->par
;
769 ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_OFF
);
771 backlight_device_unregister(info
->bl_dev
);
773 unregister_framebuffer(info
);
774 if (par
->device_info
->need_pwm
) {
775 pwm_disable(par
->pwm
);
779 regulator_disable(par
->vbat_reg
);
780 fb_deferred_io_cleanup(info
);
781 __free_pages(__va(info
->fix
.smem_start
), get_order(info
->fix
.smem_len
));
782 framebuffer_release(info
);
787 static const struct i2c_device_id ssd1307fb_i2c_id
[] = {
794 MODULE_DEVICE_TABLE(i2c
, ssd1307fb_i2c_id
);
796 static struct i2c_driver ssd1307fb_driver
= {
797 .probe_new
= ssd1307fb_probe
,
798 .remove
= ssd1307fb_remove
,
799 .id_table
= ssd1307fb_i2c_id
,
802 .of_match_table
= ssd1307fb_of_match
,
806 module_i2c_driver(ssd1307fb_driver
);
808 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
809 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
810 MODULE_LICENSE("GPL");