2 * Driver for the Solomon SSD1307 OLED controller
4 * Copyright 2012 Free Electrons
6 * Licensed under the GPLv2 or later.
9 #include <linux/backlight.h>
10 #include <linux/delay.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>
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_CHARGE_PUMP 0x8d
31 #define SSD1307FB_SEG_REMAP_ON 0xa1
32 #define SSD1307FB_DISPLAY_OFF 0xae
33 #define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8
34 #define SSD1307FB_DISPLAY_ON 0xaf
35 #define SSD1307FB_START_PAGE_ADDRESS 0xb0
36 #define SSD1307FB_SET_DISPLAY_OFFSET 0xd3
37 #define SSD1307FB_SET_CLOCK_FREQ 0xd5
38 #define SSD1307FB_SET_PRECHARGE_PERIOD 0xd9
39 #define SSD1307FB_SET_COM_PINS_CONFIG 0xda
40 #define SSD1307FB_SET_VCOMH 0xdb
42 #define MAX_CONTRAST 255
46 static u_int refreshrate
= REFRESHRATE
;
47 module_param(refreshrate
, uint
, 0);
51 struct ssd1307fb_deviceinfo
{
59 struct ssd1307fb_par
{
67 const struct ssd1307fb_deviceinfo
*device_info
;
68 struct i2c_client
*client
;
74 struct pwm_device
*pwm
;
82 struct ssd1307fb_array
{
87 static const struct fb_fix_screeninfo ssd1307fb_fix
= {
88 .id
= "Solomon SSD1307",
89 .type
= FB_TYPE_PACKED_PIXELS
,
90 .visual
= FB_VISUAL_MONO10
,
94 .accel
= FB_ACCEL_NONE
,
97 static const struct fb_var_screeninfo ssd1307fb_var
= {
101 static struct ssd1307fb_array
*ssd1307fb_alloc_array(u32 len
, u8 type
)
103 struct ssd1307fb_array
*array
;
105 array
= kzalloc(sizeof(struct ssd1307fb_array
) + len
, GFP_KERNEL
);
114 static int ssd1307fb_write_array(struct i2c_client
*client
,
115 struct ssd1307fb_array
*array
, u32 len
)
119 len
+= sizeof(struct ssd1307fb_array
);
121 ret
= i2c_master_send(client
, (u8
*)array
, len
);
123 dev_err(&client
->dev
, "Couldn't send I2C command.\n");
130 static inline int ssd1307fb_write_cmd(struct i2c_client
*client
, u8 cmd
)
132 struct ssd1307fb_array
*array
;
135 array
= ssd1307fb_alloc_array(1, SSD1307FB_COMMAND
);
139 array
->data
[0] = cmd
;
141 ret
= ssd1307fb_write_array(client
, array
, 1);
147 static void ssd1307fb_update_display(struct ssd1307fb_par
*par
)
149 struct ssd1307fb_array
*array
;
150 u8
*vmem
= par
->info
->screen_base
;
153 array
= ssd1307fb_alloc_array(par
->width
* par
->height
/ 8,
159 * The screen is divided in pages, each having a height of 8
160 * pixels, and the width of the screen. When sending a byte of
161 * data to the controller, it gives the 8 bits for the current
162 * column. I.e, the first byte are the 8 bits of the first
163 * column, then the 8 bits for the second column, etc.
166 * Representation of the screen, assuming it is 5 bits
167 * wide. Each letter-number combination is a bit that controls
179 * If you want to update this screen, you need to send 5 bytes:
180 * (1) A0 B0 C0 D0 E0 F0 G0 H0
181 * (2) A1 B1 C1 D1 E1 F1 G1 H1
182 * (3) A2 B2 C2 D2 E2 F2 G2 H2
183 * (4) A3 B3 C3 D3 E3 F3 G3 H3
184 * (5) A4 B4 C4 D4 E4 F4 G4 H4
187 for (i
= 0; i
< (par
->height
/ 8); i
++) {
188 for (j
= 0; j
< par
->width
; j
++) {
189 u32 array_idx
= i
* par
->width
+ j
;
190 array
->data
[array_idx
] = 0;
191 for (k
= 0; k
< 8; k
++) {
192 u32 page_length
= par
->width
* i
;
193 u32 index
= page_length
+ (par
->width
* k
+ j
) / 8;
194 u8 byte
= *(vmem
+ index
);
195 u8 bit
= byte
& (1 << (j
% 8));
196 bit
= bit
>> (j
% 8);
197 array
->data
[array_idx
] |= bit
<< k
;
202 ssd1307fb_write_array(par
->client
, array
, par
->width
* par
->height
/ 8);
207 static ssize_t
ssd1307fb_write(struct fb_info
*info
, const char __user
*buf
,
208 size_t count
, loff_t
*ppos
)
210 struct ssd1307fb_par
*par
= info
->par
;
211 unsigned long total_size
;
212 unsigned long p
= *ppos
;
215 total_size
= info
->fix
.smem_len
;
220 if (count
+ p
> total_size
)
221 count
= total_size
- p
;
226 dst
= (void __force
*) (info
->screen_base
+ p
);
228 if (copy_from_user(dst
, buf
, count
))
231 ssd1307fb_update_display(par
);
238 static int ssd1307fb_blank(int blank_mode
, struct fb_info
*info
)
240 struct ssd1307fb_par
*par
= info
->par
;
242 if (blank_mode
!= FB_BLANK_UNBLANK
)
243 return ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_OFF
);
245 return ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
248 static void ssd1307fb_fillrect(struct fb_info
*info
, const struct fb_fillrect
*rect
)
250 struct ssd1307fb_par
*par
= info
->par
;
251 sys_fillrect(info
, rect
);
252 ssd1307fb_update_display(par
);
255 static void ssd1307fb_copyarea(struct fb_info
*info
, const struct fb_copyarea
*area
)
257 struct ssd1307fb_par
*par
= info
->par
;
258 sys_copyarea(info
, area
);
259 ssd1307fb_update_display(par
);
262 static void ssd1307fb_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
264 struct ssd1307fb_par
*par
= info
->par
;
265 sys_imageblit(info
, image
);
266 ssd1307fb_update_display(par
);
269 static struct fb_ops ssd1307fb_ops
= {
270 .owner
= THIS_MODULE
,
271 .fb_read
= fb_sys_read
,
272 .fb_write
= ssd1307fb_write
,
273 .fb_blank
= ssd1307fb_blank
,
274 .fb_fillrect
= ssd1307fb_fillrect
,
275 .fb_copyarea
= ssd1307fb_copyarea
,
276 .fb_imageblit
= ssd1307fb_imageblit
,
279 static void ssd1307fb_deferred_io(struct fb_info
*info
,
280 struct list_head
*pagelist
)
282 ssd1307fb_update_display(info
->par
);
285 static int ssd1307fb_init(struct ssd1307fb_par
*par
)
288 u32 precharge
, dclk
, com_invdir
, compins
;
289 struct pwm_args pargs
;
291 if (par
->device_info
->need_pwm
) {
292 par
->pwm
= pwm_get(&par
->client
->dev
, NULL
);
293 if (IS_ERR(par
->pwm
)) {
294 dev_err(&par
->client
->dev
, "Could not get PWM from device tree!\n");
295 return PTR_ERR(par
->pwm
);
299 * FIXME: pwm_apply_args() should be removed when switching to
300 * the atomic PWM API.
302 pwm_apply_args(par
->pwm
);
304 pwm_get_args(par
->pwm
, &pargs
);
306 par
->pwm_period
= pargs
.period
;
308 pwm_config(par
->pwm
, par
->pwm_period
/ 2, par
->pwm_period
);
309 pwm_enable(par
->pwm
);
311 dev_dbg(&par
->client
->dev
, "Using PWM%d with a %dns period.\n",
312 par
->pwm
->pwm
, par
->pwm_period
);
315 /* Set initial contrast */
316 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
320 ret
= ssd1307fb_write_cmd(par
->client
, par
->contrast
);
324 /* Set segment re-map */
325 if (par
->seg_remap
) {
326 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SEG_REMAP_ON
);
331 /* Set COM direction */
332 com_invdir
= 0xc0 | (par
->com_invdir
& 0x1) << 3;
333 ret
= ssd1307fb_write_cmd(par
->client
, com_invdir
);
337 /* Set multiplex ratio value */
338 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_MULTIPLEX_RATIO
);
342 ret
= ssd1307fb_write_cmd(par
->client
, par
->height
- 1);
346 /* set display offset value */
347 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_DISPLAY_OFFSET
);
351 ret
= ssd1307fb_write_cmd(par
->client
, par
->com_offset
);
355 /* Set clock frequency */
356 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_CLOCK_FREQ
);
360 dclk
= ((par
->dclk_div
- 1) & 0xf) | (par
->dclk_frq
& 0xf) << 4;
361 ret
= ssd1307fb_write_cmd(par
->client
, dclk
);
365 /* Set precharge period in number of ticks from the internal clock */
366 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PRECHARGE_PERIOD
);
370 precharge
= (par
->prechargep1
& 0xf) | (par
->prechargep2
& 0xf) << 4;
371 ret
= ssd1307fb_write_cmd(par
->client
, precharge
);
375 /* Set COM pins configuration */
376 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COM_PINS_CONFIG
);
380 compins
= 0x02 | !(par
->com_seq
& 0x1) << 4
381 | (par
->com_lrremap
& 0x1) << 5;
382 ret
= ssd1307fb_write_cmd(par
->client
, compins
);
387 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_VCOMH
);
391 ret
= ssd1307fb_write_cmd(par
->client
, par
->vcomh
);
395 /* Turn on the DC-DC Charge Pump */
396 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CHARGE_PUMP
);
400 ret
= ssd1307fb_write_cmd(par
->client
,
401 BIT(4) | (par
->device_info
->need_chargepump
? BIT(2) : 0));
405 /* Switch to horizontal addressing mode */
406 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_ADDRESS_MODE
);
410 ret
= ssd1307fb_write_cmd(par
->client
,
411 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL
);
415 /* Set column range */
416 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COL_RANGE
);
420 ret
= ssd1307fb_write_cmd(par
->client
, 0x0);
424 ret
= ssd1307fb_write_cmd(par
->client
, par
->width
- 1);
429 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PAGE_RANGE
);
433 ret
= ssd1307fb_write_cmd(par
->client
, 0x0);
437 ret
= ssd1307fb_write_cmd(par
->client
,
438 par
->page_offset
+ (par
->height
/ 8) - 1);
442 /* Turn on the display */
443 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
450 static int ssd1307fb_update_bl(struct backlight_device
*bdev
)
452 struct ssd1307fb_par
*par
= bl_get_data(bdev
);
454 int brightness
= bdev
->props
.brightness
;
456 par
->contrast
= brightness
;
458 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
461 ret
= ssd1307fb_write_cmd(par
->client
, par
->contrast
);
467 static int ssd1307fb_get_brightness(struct backlight_device
*bdev
)
469 struct ssd1307fb_par
*par
= bl_get_data(bdev
);
471 return par
->contrast
;
474 static int ssd1307fb_check_fb(struct backlight_device
*bdev
,
475 struct fb_info
*info
)
477 return (info
->bl_dev
== bdev
);
480 static const struct backlight_ops ssd1307fb_bl_ops
= {
481 .options
= BL_CORE_SUSPENDRESUME
,
482 .update_status
= ssd1307fb_update_bl
,
483 .get_brightness
= ssd1307fb_get_brightness
,
484 .check_fb
= ssd1307fb_check_fb
,
487 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo
= {
488 .default_vcomh
= 0x34,
489 .default_dclk_div
= 1,
490 .default_dclk_frq
= 7,
493 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo
= {
494 .default_vcomh
= 0x20,
495 .default_dclk_div
= 1,
496 .default_dclk_frq
= 8,
497 .need_chargepump
= 1,
500 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo
= {
501 .default_vcomh
= 0x20,
502 .default_dclk_div
= 2,
503 .default_dclk_frq
= 12,
507 static struct ssd1307fb_deviceinfo ssd1307fb_ssd1309_deviceinfo
= {
508 .default_vcomh
= 0x34,
509 .default_dclk_div
= 1,
510 .default_dclk_frq
= 10,
513 static const struct of_device_id ssd1307fb_of_match
[] = {
515 .compatible
= "solomon,ssd1305fb-i2c",
516 .data
= (void *)&ssd1307fb_ssd1305_deviceinfo
,
519 .compatible
= "solomon,ssd1306fb-i2c",
520 .data
= (void *)&ssd1307fb_ssd1306_deviceinfo
,
523 .compatible
= "solomon,ssd1307fb-i2c",
524 .data
= (void *)&ssd1307fb_ssd1307_deviceinfo
,
527 .compatible
= "solomon,ssd1309fb-i2c",
528 .data
= (void *)&ssd1307fb_ssd1309_deviceinfo
,
532 MODULE_DEVICE_TABLE(of
, ssd1307fb_of_match
);
534 static int ssd1307fb_probe(struct i2c_client
*client
,
535 const struct i2c_device_id
*id
)
537 struct backlight_device
*bl
;
539 struct fb_info
*info
;
540 struct device_node
*node
= client
->dev
.of_node
;
541 struct fb_deferred_io
*ssd1307fb_defio
;
543 struct ssd1307fb_par
*par
;
548 dev_err(&client
->dev
, "No device tree data found!\n");
552 info
= framebuffer_alloc(sizeof(struct ssd1307fb_par
), &client
->dev
);
554 dev_err(&client
->dev
, "Couldn't allocate framebuffer.\n");
560 par
->client
= client
;
562 par
->device_info
= of_device_get_match_data(&client
->dev
);
564 par
->reset
= of_get_named_gpio(client
->dev
.of_node
,
566 if (!gpio_is_valid(par
->reset
)) {
571 if (of_property_read_u32(node
, "solomon,width", &par
->width
))
574 if (of_property_read_u32(node
, "solomon,height", &par
->height
))
577 if (of_property_read_u32(node
, "solomon,page-offset", &par
->page_offset
))
578 par
->page_offset
= 1;
580 if (of_property_read_u32(node
, "solomon,com-offset", &par
->com_offset
))
583 if (of_property_read_u32(node
, "solomon,prechargep1", &par
->prechargep1
))
584 par
->prechargep1
= 2;
586 if (of_property_read_u32(node
, "solomon,prechargep2", &par
->prechargep2
))
587 par
->prechargep2
= 2;
589 par
->seg_remap
= !of_property_read_bool(node
, "solomon,segment-no-remap");
590 par
->com_seq
= of_property_read_bool(node
, "solomon,com-seq");
591 par
->com_lrremap
= of_property_read_bool(node
, "solomon,com-lrremap");
592 par
->com_invdir
= of_property_read_bool(node
, "solomon,com-invdir");
595 par
->vcomh
= par
->device_info
->default_vcomh
;
597 /* Setup display timing */
598 par
->dclk_div
= par
->device_info
->default_dclk_div
;
599 par
->dclk_frq
= par
->device_info
->default_dclk_frq
;
601 vmem_size
= par
->width
* par
->height
/ 8;
603 vmem
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
604 get_order(vmem_size
));
606 dev_err(&client
->dev
, "Couldn't allocate graphical memory.\n");
611 ssd1307fb_defio
= devm_kzalloc(&client
->dev
, sizeof(struct fb_deferred_io
), GFP_KERNEL
);
612 if (!ssd1307fb_defio
) {
613 dev_err(&client
->dev
, "Couldn't allocate deferred io.\n");
618 ssd1307fb_defio
->delay
= HZ
/ refreshrate
;
619 ssd1307fb_defio
->deferred_io
= ssd1307fb_deferred_io
;
621 info
->fbops
= &ssd1307fb_ops
;
622 info
->fix
= ssd1307fb_fix
;
623 info
->fix
.line_length
= par
->width
/ 8;
624 info
->fbdefio
= ssd1307fb_defio
;
626 info
->var
= ssd1307fb_var
;
627 info
->var
.xres
= par
->width
;
628 info
->var
.xres_virtual
= par
->width
;
629 info
->var
.yres
= par
->height
;
630 info
->var
.yres_virtual
= par
->height
;
632 info
->var
.red
.length
= 1;
633 info
->var
.red
.offset
= 0;
634 info
->var
.green
.length
= 1;
635 info
->var
.green
.offset
= 0;
636 info
->var
.blue
.length
= 1;
637 info
->var
.blue
.offset
= 0;
639 info
->screen_base
= (u8 __force __iomem
*)vmem
;
640 info
->fix
.smem_start
= __pa(vmem
);
641 info
->fix
.smem_len
= vmem_size
;
643 fb_deferred_io_init(info
);
645 ret
= devm_gpio_request_one(&client
->dev
, par
->reset
,
649 dev_err(&client
->dev
,
650 "failed to request gpio %d: %d\n",
652 goto reset_oled_error
;
655 i2c_set_clientdata(client
, info
);
657 /* Reset the screen */
658 gpio_set_value(par
->reset
, 0);
660 gpio_set_value(par
->reset
, 1);
663 ret
= ssd1307fb_init(par
);
665 goto reset_oled_error
;
667 ret
= register_framebuffer(info
);
669 dev_err(&client
->dev
, "Couldn't register the framebuffer\n");
670 goto panel_init_error
;
673 snprintf(bl_name
, sizeof(bl_name
), "ssd1307fb%d", info
->node
);
674 bl
= backlight_device_register(bl_name
, &client
->dev
, par
,
675 &ssd1307fb_bl_ops
, NULL
);
678 dev_err(&client
->dev
, "unable to register backlight device: %d\n",
683 bl
->props
.brightness
= par
->contrast
;
684 bl
->props
.max_brightness
= MAX_CONTRAST
;
687 dev_info(&client
->dev
, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info
->node
, info
->fix
.id
, vmem_size
);
692 unregister_framebuffer(info
);
694 if (par
->device_info
->need_pwm
) {
695 pwm_disable(par
->pwm
);
699 fb_deferred_io_cleanup(info
);
701 framebuffer_release(info
);
705 static int ssd1307fb_remove(struct i2c_client
*client
)
707 struct fb_info
*info
= i2c_get_clientdata(client
);
708 struct ssd1307fb_par
*par
= info
->par
;
710 ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_OFF
);
712 backlight_device_unregister(info
->bl_dev
);
714 unregister_framebuffer(info
);
715 if (par
->device_info
->need_pwm
) {
716 pwm_disable(par
->pwm
);
719 fb_deferred_io_cleanup(info
);
720 __free_pages(__va(info
->fix
.smem_start
), get_order(info
->fix
.smem_len
));
721 framebuffer_release(info
);
726 static const struct i2c_device_id ssd1307fb_i2c_id
[] = {
733 MODULE_DEVICE_TABLE(i2c
, ssd1307fb_i2c_id
);
735 static struct i2c_driver ssd1307fb_driver
= {
736 .probe
= ssd1307fb_probe
,
737 .remove
= ssd1307fb_remove
,
738 .id_table
= ssd1307fb_i2c_id
,
741 .of_match_table
= ssd1307fb_of_match
,
745 module_i2c_driver(ssd1307fb_driver
);
747 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
748 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
749 MODULE_LICENSE("GPL");