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 void ssd1307fb_update_display(struct ssd1307fb_par
*par
)
127 struct ssd1307fb_array
*array
;
128 u8
*vmem
= par
->info
->screen_base
;
131 array
= ssd1307fb_alloc_array(par
->width
* par
->height
/ 8,
137 * The screen is divided in pages, each having a height of 8
138 * pixels, and the width of the screen. When sending a byte of
139 * data to the controller, it gives the 8 bits for the current
140 * column. I.e, the first byte are the 8 bits of the first
141 * column, then the 8 bits for the second column, etc.
144 * Representation of the screen, assuming it is 5 bits
145 * wide. Each letter-number combination is a bit that controls
157 * If you want to update this screen, you need to send 5 bytes:
158 * (1) A0 B0 C0 D0 E0 F0 G0 H0
159 * (2) A1 B1 C1 D1 E1 F1 G1 H1
160 * (3) A2 B2 C2 D2 E2 F2 G2 H2
161 * (4) A3 B3 C3 D3 E3 F3 G3 H3
162 * (5) A4 B4 C4 D4 E4 F4 G4 H4
165 for (i
= 0; i
< (par
->height
/ 8); i
++) {
166 for (j
= 0; j
< par
->width
; j
++) {
167 u32 array_idx
= i
* par
->width
+ j
;
168 array
->data
[array_idx
] = 0;
169 for (k
= 0; k
< 8; k
++) {
170 u32 page_length
= par
->width
* i
;
171 u32 index
= page_length
+ (par
->width
* k
+ j
) / 8;
172 u8 byte
= *(vmem
+ index
);
173 u8 bit
= byte
& (1 << (j
% 8));
174 bit
= bit
>> (j
% 8);
175 array
->data
[array_idx
] |= bit
<< k
;
180 ssd1307fb_write_array(par
->client
, array
, par
->width
* par
->height
/ 8);
185 static ssize_t
ssd1307fb_write(struct fb_info
*info
, const char __user
*buf
,
186 size_t count
, loff_t
*ppos
)
188 struct ssd1307fb_par
*par
= info
->par
;
189 unsigned long total_size
;
190 unsigned long p
= *ppos
;
193 total_size
= info
->fix
.smem_len
;
198 if (count
+ p
> total_size
)
199 count
= total_size
- p
;
204 dst
= (void __force
*) (info
->screen_base
+ p
);
206 if (copy_from_user(dst
, buf
, count
))
209 ssd1307fb_update_display(par
);
216 static void ssd1307fb_fillrect(struct fb_info
*info
, const struct fb_fillrect
*rect
)
218 struct ssd1307fb_par
*par
= info
->par
;
219 sys_fillrect(info
, rect
);
220 ssd1307fb_update_display(par
);
223 static void ssd1307fb_copyarea(struct fb_info
*info
, const struct fb_copyarea
*area
)
225 struct ssd1307fb_par
*par
= info
->par
;
226 sys_copyarea(info
, area
);
227 ssd1307fb_update_display(par
);
230 static void ssd1307fb_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
232 struct ssd1307fb_par
*par
= info
->par
;
233 sys_imageblit(info
, image
);
234 ssd1307fb_update_display(par
);
237 static struct fb_ops ssd1307fb_ops
= {
238 .owner
= THIS_MODULE
,
239 .fb_read
= fb_sys_read
,
240 .fb_write
= ssd1307fb_write
,
241 .fb_fillrect
= ssd1307fb_fillrect
,
242 .fb_copyarea
= ssd1307fb_copyarea
,
243 .fb_imageblit
= ssd1307fb_imageblit
,
246 static void ssd1307fb_deferred_io(struct fb_info
*info
,
247 struct list_head
*pagelist
)
249 ssd1307fb_update_display(info
->par
);
252 static struct fb_deferred_io ssd1307fb_defio
= {
254 .deferred_io
= ssd1307fb_deferred_io
,
257 static int ssd1307fb_ssd1307_init(struct ssd1307fb_par
*par
)
261 par
->pwm
= pwm_get(&par
->client
->dev
, NULL
);
262 if (IS_ERR(par
->pwm
)) {
263 dev_err(&par
->client
->dev
, "Could not get PWM from device tree!\n");
264 return PTR_ERR(par
->pwm
);
267 par
->pwm_period
= pwm_get_period(par
->pwm
);
269 pwm_config(par
->pwm
, par
->pwm_period
/ 2, par
->pwm_period
);
270 pwm_enable(par
->pwm
);
272 dev_dbg(&par
->client
->dev
, "Using PWM%d with a %dns period.\n",
273 par
->pwm
->pwm
, par
->pwm_period
);
275 /* Map column 127 of the OLED to segment 0 */
276 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SEG_REMAP_ON
);
280 /* Turn on the display */
281 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
288 static int ssd1307fb_ssd1307_remove(struct ssd1307fb_par
*par
)
290 pwm_disable(par
->pwm
);
295 static struct ssd1307fb_ops ssd1307fb_ssd1307_ops
= {
296 .init
= ssd1307fb_ssd1307_init
,
297 .remove
= ssd1307fb_ssd1307_remove
,
300 static int ssd1307fb_ssd1306_init(struct ssd1307fb_par
*par
)
304 /* Set initial contrast */
305 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CONTRAST
);
309 ret
= ssd1307fb_write_cmd(par
->client
, 0x7f);
313 /* Set COM direction */
314 ret
= ssd1307fb_write_cmd(par
->client
, 0xc8);
318 /* Set segment re-map */
319 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SEG_REMAP_ON
);
323 /* Set multiplex ratio value */
324 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_MULTIPLEX_RATIO
);
328 ret
= ssd1307fb_write_cmd(par
->client
, par
->height
- 1);
332 /* set display offset value */
333 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_DISPLAY_OFFSET
);
337 ret
= ssd1307fb_write_cmd(par
->client
, 0x20);
341 /* Set clock frequency */
342 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_CLOCK_FREQ
);
346 ret
= ssd1307fb_write_cmd(par
->client
, 0xf0);
350 /* Set precharge period in number of ticks from the internal clock */
351 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PRECHARGE_PERIOD
);
355 ret
= ssd1307fb_write_cmd(par
->client
, 0x22);
359 /* Set COM pins configuration */
360 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COM_PINS_CONFIG
);
364 ret
= ssd1307fb_write_cmd(par
->client
, 0x22);
369 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_VCOMH
);
373 ret
= ssd1307fb_write_cmd(par
->client
, 0x49);
377 /* Turn on the DC-DC Charge Pump */
378 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_CHARGE_PUMP
);
382 ret
= ssd1307fb_write_cmd(par
->client
, 0x14);
386 /* Switch to horizontal addressing mode */
387 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_ADDRESS_MODE
);
391 ret
= ssd1307fb_write_cmd(par
->client
,
392 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL
);
396 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_COL_RANGE
);
400 ret
= ssd1307fb_write_cmd(par
->client
, 0x0);
404 ret
= ssd1307fb_write_cmd(par
->client
, par
->width
- 1);
408 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_SET_PAGE_RANGE
);
412 ret
= ssd1307fb_write_cmd(par
->client
, 0x0);
416 ret
= ssd1307fb_write_cmd(par
->client
,
417 par
->page_offset
+ (par
->height
/ 8) - 1);
421 /* Turn on the display */
422 ret
= ssd1307fb_write_cmd(par
->client
, SSD1307FB_DISPLAY_ON
);
429 static struct ssd1307fb_ops ssd1307fb_ssd1306_ops
= {
430 .init
= ssd1307fb_ssd1306_init
,
433 static const struct of_device_id ssd1307fb_of_match
[] = {
435 .compatible
= "solomon,ssd1306fb-i2c",
436 .data
= (void *)&ssd1307fb_ssd1306_ops
,
439 .compatible
= "solomon,ssd1307fb-i2c",
440 .data
= (void *)&ssd1307fb_ssd1307_ops
,
444 MODULE_DEVICE_TABLE(of
, ssd1307fb_of_match
);
446 static int ssd1307fb_probe(struct i2c_client
*client
,
447 const struct i2c_device_id
*id
)
449 struct fb_info
*info
;
450 struct device_node
*node
= client
->dev
.of_node
;
452 struct ssd1307fb_par
*par
;
457 dev_err(&client
->dev
, "No device tree data found!\n");
461 info
= framebuffer_alloc(sizeof(struct ssd1307fb_par
), &client
->dev
);
463 dev_err(&client
->dev
, "Couldn't allocate framebuffer.\n");
469 par
->client
= client
;
471 par
->ops
= (struct ssd1307fb_ops
*)of_match_device(ssd1307fb_of_match
,
474 par
->reset
= of_get_named_gpio(client
->dev
.of_node
,
476 if (!gpio_is_valid(par
->reset
)) {
481 if (of_property_read_u32(node
, "solomon,width", &par
->width
))
484 if (of_property_read_u32(node
, "solomon,height", &par
->height
))
487 if (of_property_read_u32(node
, "solomon,page-offset", &par
->page_offset
))
488 par
->page_offset
= 1;
490 vmem_size
= par
->width
* par
->height
/ 8;
492 vmem
= devm_kzalloc(&client
->dev
, vmem_size
, GFP_KERNEL
);
494 dev_err(&client
->dev
, "Couldn't allocate graphical memory.\n");
499 info
->fbops
= &ssd1307fb_ops
;
500 info
->fix
= ssd1307fb_fix
;
501 info
->fix
.line_length
= par
->width
/ 8;
502 info
->fbdefio
= &ssd1307fb_defio
;
504 info
->var
= ssd1307fb_var
;
505 info
->var
.xres
= par
->width
;
506 info
->var
.xres_virtual
= par
->width
;
507 info
->var
.yres
= par
->height
;
508 info
->var
.yres_virtual
= par
->height
;
510 info
->var
.red
.length
= 1;
511 info
->var
.red
.offset
= 0;
512 info
->var
.green
.length
= 1;
513 info
->var
.green
.offset
= 0;
514 info
->var
.blue
.length
= 1;
515 info
->var
.blue
.offset
= 0;
517 info
->screen_base
= (u8 __force __iomem
*)vmem
;
518 info
->fix
.smem_start
= (unsigned long)vmem
;
519 info
->fix
.smem_len
= vmem_size
;
521 fb_deferred_io_init(info
);
523 ret
= devm_gpio_request_one(&client
->dev
, par
->reset
,
527 dev_err(&client
->dev
,
528 "failed to request gpio %d: %d\n",
530 goto reset_oled_error
;
533 i2c_set_clientdata(client
, info
);
535 /* Reset the screen */
536 gpio_set_value(par
->reset
, 0);
538 gpio_set_value(par
->reset
, 1);
541 if (par
->ops
->init
) {
542 ret
= par
->ops
->init(par
);
544 goto reset_oled_error
;
547 ret
= register_framebuffer(info
);
549 dev_err(&client
->dev
, "Couldn't register the framebuffer\n");
550 goto panel_init_error
;
553 dev_info(&client
->dev
, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info
->node
, info
->fix
.id
, vmem_size
);
558 if (par
->ops
->remove
)
559 par
->ops
->remove(par
);
561 fb_deferred_io_cleanup(info
);
563 framebuffer_release(info
);
567 static int ssd1307fb_remove(struct i2c_client
*client
)
569 struct fb_info
*info
= i2c_get_clientdata(client
);
570 struct ssd1307fb_par
*par
= info
->par
;
572 unregister_framebuffer(info
);
573 if (par
->ops
->remove
)
574 par
->ops
->remove(par
);
575 fb_deferred_io_cleanup(info
);
576 framebuffer_release(info
);
581 static const struct i2c_device_id ssd1307fb_i2c_id
[] = {
586 MODULE_DEVICE_TABLE(i2c
, ssd1307fb_i2c_id
);
588 static struct i2c_driver ssd1307fb_driver
= {
589 .probe
= ssd1307fb_probe
,
590 .remove
= ssd1307fb_remove
,
591 .id_table
= ssd1307fb_i2c_id
,
594 .of_match_table
= ssd1307fb_of_match
,
595 .owner
= THIS_MODULE
,
599 module_i2c_driver(ssd1307fb_driver
);
601 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
602 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
603 MODULE_LICENSE("GPL");