2 * Driver for the Solomon SSD1307 OLED controller
4 * Copyright 2012 Free Electrons
6 * Licensed under the GPLv2 or later.
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/i2c.h>
13 #include <linux/uaccess.h>
14 #include <linux/of_device.h>
15 #include <linux/of_gpio.h>
16 #include <linux/pwm.h>
17 #include <linux/delay.h>
19 #define SSD1307FB_DATA 0x40
20 #define SSD1307FB_COMMAND 0x80
22 #define SSD1307FB_SET_ADDRESS_MODE 0x20
23 #define SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL (0x00)
24 #define SSD1307FB_SET_ADDRESS_MODE_VERTICAL (0x01)
25 #define SSD1307FB_SET_ADDRESS_MODE_PAGE (0x02)
26 #define SSD1307FB_SET_COL_RANGE 0x21
27 #define SSD1307FB_SET_PAGE_RANGE 0x22
28 #define SSD1307FB_CONTRAST 0x81
29 #define SSD1307FB_CHARGE_PUMP 0x8d
30 #define SSD1307FB_SEG_REMAP_ON 0xa1
31 #define SSD1307FB_DISPLAY_OFF 0xae
32 #define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8
33 #define SSD1307FB_DISPLAY_ON 0xaf
34 #define SSD1307FB_START_PAGE_ADDRESS 0xb0
35 #define SSD1307FB_SET_DISPLAY_OFFSET 0xd3
36 #define SSD1307FB_SET_CLOCK_FREQ 0xd5
37 #define SSD1307FB_SET_PRECHARGE_PERIOD 0xd9
38 #define SSD1307FB_SET_COM_PINS_CONFIG 0xda
39 #define SSD1307FB_SET_VCOMH 0xdb
43 struct ssd1307fb_ops
{
44 int (*init
)(struct ssd1307fb_par
*);
45 int (*remove
)(struct ssd1307fb_par
*);
48 struct ssd1307fb_par
{
49 struct i2c_client
*client
;
52 struct ssd1307fb_ops
*ops
;
54 struct pwm_device
*pwm
;
60 struct ssd1307fb_array
{
65 static struct fb_fix_screeninfo ssd1307fb_fix
= {
66 .id
= "Solomon SSD1307",
67 .type
= FB_TYPE_PACKED_PIXELS
,
68 .visual
= FB_VISUAL_MONO10
,
72 .accel
= FB_ACCEL_NONE
,
75 static struct fb_var_screeninfo ssd1307fb_var
= {
79 static struct ssd1307fb_array
*ssd1307fb_alloc_array(u32 len
, u8 type
)
81 struct ssd1307fb_array
*array
;
83 array
= kzalloc(sizeof(struct ssd1307fb_array
) + len
, GFP_KERNEL
);
92 static int ssd1307fb_write_array(struct i2c_client
*client
,
93 struct ssd1307fb_array
*array
, u32 len
)
97 len
+= sizeof(struct ssd1307fb_array
);
99 ret
= i2c_master_send(client
, (u8
*)array
, len
);
101 dev_err(&client
->dev
, "Couldn't send I2C command.\n");
108 static inline int ssd1307fb_write_cmd(struct i2c_client
*client
, u8 cmd
)
110 struct ssd1307fb_array
*array
;
113 array
= ssd1307fb_alloc_array(1, SSD1307FB_COMMAND
);
117 array
->data
[0] = cmd
;
119 ret
= ssd1307fb_write_array(client
, array
, 1);
125 static inline int ssd1307fb_write_data(struct i2c_client
*client
, u8 data
)
127 struct ssd1307fb_array
*array
;
130 array
= ssd1307fb_alloc_array(1, SSD1307FB_DATA
);
134 array
->data
[0] = data
;
136 ret
= ssd1307fb_write_array(client
, array
, 1);
142 static void ssd1307fb_update_display(struct ssd1307fb_par
*par
)
144 struct ssd1307fb_array
*array
;
145 u8
*vmem
= par
->info
->screen_base
;
148 array
= ssd1307fb_alloc_array(par
->width
* par
->height
/ 8,
154 * The screen is divided in pages, each having a height of 8
155 * pixels, and the width of the screen. When sending a byte of
156 * data to the controller, it gives the 8 bits for the current
157 * column. I.e, the first byte are the 8 bits of the first
158 * column, then the 8 bits for the second column, etc.
161 * Representation of the screen, assuming it is 5 bits
162 * wide. Each letter-number combination is a bit that controls
174 * If you want to update this screen, you need to send 5 bytes:
175 * (1) A0 B0 C0 D0 E0 F0 G0 H0
176 * (2) A1 B1 C1 D1 E1 F1 G1 H1
177 * (3) A2 B2 C2 D2 E2 F2 G2 H2
178 * (4) A3 B3 C3 D3 E3 F3 G3 H3
179 * (5) A4 B4 C4 D4 E4 F4 G4 H4
182 for (i
= 0; i
< (par
->height
/ 8); i
++) {
183 for (j
= 0; j
< par
->width
; j
++) {
184 u32 array_idx
= i
* par
->width
+ j
;
185 array
->data
[array_idx
] = 0;
186 for (k
= 0; k
< 8; k
++) {
187 u32 page_length
= par
->width
* i
;
188 u32 index
= page_length
+ (par
->width
* k
+ j
) / 8;
189 u8 byte
= *(vmem
+ index
);
190 u8 bit
= byte
& (1 << (j
% 8));
191 bit
= bit
>> (j
% 8);
192 array
->data
[array_idx
] |= bit
<< k
;
197 ssd1307fb_write_array(par
->client
, array
, par
->width
* par
->height
/ 8);
202 static ssize_t
ssd1307fb_write(struct fb_info
*info
, const char __user
*buf
,
203 size_t count
, loff_t
*ppos
)
205 struct ssd1307fb_par
*par
= info
->par
;
206 unsigned long total_size
;
207 unsigned long p
= *ppos
;
210 total_size
= info
->fix
.smem_len
;
215 if (count
+ p
> total_size
)
216 count
= total_size
- p
;
221 dst
= (void __force
*) (info
->screen_base
+ p
);
223 if (copy_from_user(dst
, buf
, count
))
226 ssd1307fb_update_display(par
);
233 static void ssd1307fb_fillrect(struct fb_info
*info
, const struct fb_fillrect
*rect
)
235 struct ssd1307fb_par
*par
= info
->par
;
236 sys_fillrect(info
, rect
);
237 ssd1307fb_update_display(par
);
240 static void ssd1307fb_copyarea(struct fb_info
*info
, const struct fb_copyarea
*area
)
242 struct ssd1307fb_par
*par
= info
->par
;
243 sys_copyarea(info
, area
);
244 ssd1307fb_update_display(par
);
247 static void ssd1307fb_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
249 struct ssd1307fb_par
*par
= info
->par
;
250 sys_imageblit(info
, image
);
251 ssd1307fb_update_display(par
);
254 static struct fb_ops ssd1307fb_ops
= {
255 .owner
= THIS_MODULE
,
256 .fb_read
= fb_sys_read
,
257 .fb_write
= ssd1307fb_write
,
258 .fb_fillrect
= ssd1307fb_fillrect
,
259 .fb_copyarea
= ssd1307fb_copyarea
,
260 .fb_imageblit
= ssd1307fb_imageblit
,
263 static void ssd1307fb_deferred_io(struct fb_info
*info
,
264 struct list_head
*pagelist
)
266 ssd1307fb_update_display(info
->par
);
269 static struct fb_deferred_io ssd1307fb_defio
= {
271 .deferred_io
= ssd1307fb_deferred_io
,
274 static int ssd1307fb_ssd1307_init(struct ssd1307fb_par
*par
)
278 par
->pwm
= pwm_get(&par
->client
->dev
, NULL
);
279 if (IS_ERR(par
->pwm
)) {
280 dev_err(&par
->client
->dev
, "Could not get PWM from device tree!\n");
281 return PTR_ERR(par
->pwm
);
284 par
->pwm_period
= pwm_get_period(par
->pwm
);
286 pwm_config(par
->pwm
, par
->pwm_period
/ 2, par
->pwm_period
);
287 pwm_enable(par
->pwm
);
289 dev_dbg(&par
->client
->dev
, "Using PWM%d with a %dns period.\n",
290 par
->pwm
->pwm
, par
->pwm_period
);
292 /* Map column 127 of the OLED to segment 0 */
293 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SEG_REMAP_ON
);
297 /* Turn on the display */
298 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
305 static int ssd1307fb_ssd1307_remove(struct ssd1307fb_par
*par
)
307 pwm_disable(par
->pwm
);
312 static struct ssd1307fb_ops ssd1307fb_ssd1307_ops
= {
313 .init
= ssd1307fb_ssd1307_init
,
314 .remove
= ssd1307fb_ssd1307_remove
,
317 static int ssd1307fb_ssd1306_init(struct ssd1307fb_par
*par
)
321 /* Set initial contrast */
322 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
323 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0x7f);
327 /* Set COM direction */
328 ret
= ssd1307fb_write_cmd(par
->client
, 0xc8);
332 /* Set segment re-map */
333 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SEG_REMAP_ON
);
337 /* Set multiplex ratio value */
338 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_MULTIPLEX_RATIO
);
339 ret
= ret
& ssd1307fb_write_cmd(par
->client
, par
->height
- 1);
343 /* set display offset value */
344 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_DISPLAY_OFFSET
);
345 ret
= ssd1307fb_write_cmd(par
->client
, 0x20);
349 /* Set clock frequency */
350 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_CLOCK_FREQ
);
351 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0xf0);
355 /* Set precharge period in number of ticks from the internal clock */
356 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PRECHARGE_PERIOD
);
357 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0x22);
361 /* Set COM pins configuration */
362 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COM_PINS_CONFIG
);
363 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0x22);
368 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_VCOMH
);
369 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0x49);
373 /* Turn on the DC-DC Charge Pump */
374 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CHARGE_PUMP
);
375 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0x14);
379 /* Switch to horizontal addressing mode */
380 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_ADDRESS_MODE
);
381 ret
= ret
& ssd1307fb_write_cmd(par
->client
,
382 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL
);
386 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COL_RANGE
);
387 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0x0);
388 ret
= ret
& ssd1307fb_write_cmd(par
->client
, par
->width
- 1);
392 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PAGE_RANGE
);
393 ret
= ret
& ssd1307fb_write_cmd(par
->client
, 0x0);
394 ret
= ret
& ssd1307fb_write_cmd(par
->client
,
395 par
->page_offset
+ (par
->height
/ 8) - 1);
399 /* Turn on the display */
400 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
407 static struct ssd1307fb_ops ssd1307fb_ssd1306_ops
= {
408 .init
= ssd1307fb_ssd1306_init
,
411 static const struct of_device_id ssd1307fb_of_match
[] = {
413 .compatible
= "solomon,ssd1306fb-i2c",
414 .data
= (void *)&ssd1307fb_ssd1306_ops
,
417 .compatible
= "solomon,ssd1307fb-i2c",
418 .data
= (void *)&ssd1307fb_ssd1307_ops
,
422 MODULE_DEVICE_TABLE(of
, ssd1307fb_of_match
);
424 static int ssd1307fb_probe(struct i2c_client
*client
,
425 const struct i2c_device_id
*id
)
427 struct fb_info
*info
;
428 struct device_node
*node
= client
->dev
.of_node
;
430 struct ssd1307fb_par
*par
;
435 dev_err(&client
->dev
, "No device tree data found!\n");
439 info
= framebuffer_alloc(sizeof(struct ssd1307fb_par
), &client
->dev
);
441 dev_err(&client
->dev
, "Couldn't allocate framebuffer.\n");
447 par
->client
= client
;
449 par
->ops
= (struct ssd1307fb_ops
*)of_match_device(ssd1307fb_of_match
,
452 par
->reset
= of_get_named_gpio(client
->dev
.of_node
,
454 if (!gpio_is_valid(par
->reset
)) {
459 if (of_property_read_u32(node
, "solomon,width", &par
->width
))
462 if (of_property_read_u32(node
, "solomon,height", &par
->height
))
465 if (of_property_read_u32(node
, "solomon,page-offset", &par
->page_offset
))
466 par
->page_offset
= 1;
468 vmem_size
= par
->width
* par
->height
/ 8;
470 vmem
= devm_kzalloc(&client
->dev
, vmem_size
, GFP_KERNEL
);
472 dev_err(&client
->dev
, "Couldn't allocate graphical memory.\n");
477 info
->fbops
= &ssd1307fb_ops
;
478 info
->fix
= ssd1307fb_fix
;
479 info
->fix
.line_length
= par
->width
/ 8;
480 info
->fbdefio
= &ssd1307fb_defio
;
482 info
->var
= ssd1307fb_var
;
483 info
->var
.xres
= par
->width
;
484 info
->var
.xres_virtual
= par
->width
;
485 info
->var
.yres
= par
->height
;
486 info
->var
.yres_virtual
= par
->height
;
488 info
->var
.red
.length
= 1;
489 info
->var
.red
.offset
= 0;
490 info
->var
.green
.length
= 1;
491 info
->var
.green
.offset
= 0;
492 info
->var
.blue
.length
= 1;
493 info
->var
.blue
.offset
= 0;
495 info
->screen_base
= (u8 __force __iomem
*)vmem
;
496 info
->fix
.smem_start
= (unsigned long)vmem
;
497 info
->fix
.smem_len
= vmem_size
;
499 fb_deferred_io_init(info
);
501 ret
= devm_gpio_request_one(&client
->dev
, par
->reset
,
505 dev_err(&client
->dev
,
506 "failed to request gpio %d: %d\n",
508 goto reset_oled_error
;
511 i2c_set_clientdata(client
, info
);
513 /* Reset the screen */
514 gpio_set_value(par
->reset
, 0);
516 gpio_set_value(par
->reset
, 1);
519 if (par
->ops
->init
) {
520 ret
= par
->ops
->init(par
);
522 goto reset_oled_error
;
525 ret
= register_framebuffer(info
);
527 dev_err(&client
->dev
, "Couldn't register the framebuffer\n");
528 goto panel_init_error
;
531 dev_info(&client
->dev
, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info
->node
, info
->fix
.id
, vmem_size
);
536 if (par
->ops
->remove
)
537 par
->ops
->remove(par
);
539 fb_deferred_io_cleanup(info
);
541 framebuffer_release(info
);
545 static int ssd1307fb_remove(struct i2c_client
*client
)
547 struct fb_info
*info
= i2c_get_clientdata(client
);
548 struct ssd1307fb_par
*par
= info
->par
;
550 unregister_framebuffer(info
);
551 if (par
->ops
->remove
)
552 par
->ops
->remove(par
);
553 fb_deferred_io_cleanup(info
);
554 framebuffer_release(info
);
559 static const struct i2c_device_id ssd1307fb_i2c_id
[] = {
564 MODULE_DEVICE_TABLE(i2c
, ssd1307fb_i2c_id
);
566 static struct i2c_driver ssd1307fb_driver
= {
567 .probe
= ssd1307fb_probe
,
568 .remove
= ssd1307fb_remove
,
569 .id_table
= ssd1307fb_i2c_id
,
572 .of_match_table
= of_match_ptr(ssd1307fb_of_match
),
573 .owner
= THIS_MODULE
,
577 module_i2c_driver(ssd1307fb_driver
);
579 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
580 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
581 MODULE_LICENSE("GPL");