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_CONTRAST 0x81
23 #define SSD1307FB_CHARGE_PUMP 0x8d
24 #define SSD1307FB_SEG_REMAP_ON 0xa1
25 #define SSD1307FB_DISPLAY_OFF 0xae
26 #define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8
27 #define SSD1307FB_DISPLAY_ON 0xaf
28 #define SSD1307FB_START_PAGE_ADDRESS 0xb0
29 #define SSD1307FB_SET_DISPLAY_OFFSET 0xd3
30 #define SSD1307FB_SET_CLOCK_FREQ 0xd5
31 #define SSD1307FB_SET_PRECHARGE_PERIOD 0xd9
32 #define SSD1307FB_SET_COM_PINS_CONFIG 0xda
33 #define SSD1307FB_SET_VCOMH 0xdb
37 struct ssd1307fb_ops {
38 int (*init)(struct ssd1307fb_par *);
39 int (*remove)(struct ssd1307fb_par *);
42 struct ssd1307fb_par {
43 struct i2c_client *client;
46 struct ssd1307fb_ops *ops;
48 struct pwm_device *pwm;
54 static struct fb_fix_screeninfo ssd1307fb_fix = {
55 .id = "Solomon SSD1307",
56 .type = FB_TYPE_PACKED_PIXELS,
57 .visual = FB_VISUAL_MONO10,
61 .accel = FB_ACCEL_NONE,
64 static struct fb_var_screeninfo ssd1307fb_var = {
68 static int ssd1307fb_write_array(struct i2c_client *client, u8 type, u8 *cmd, u32 len)
73 buf = kzalloc(len + 1, GFP_KERNEL);
75 dev_err(&client->dev, "Couldn't allocate sending buffer.\n");
80 memcpy(buf + 1, cmd, len);
82 ret = i2c_master_send(client, buf, len + 1);
84 dev_err(&client->dev, "Couldn't send I2C command.\n");
93 static inline int ssd1307fb_write_cmd_array(struct i2c_client *client, u8 *cmd, u32 len)
95 return ssd1307fb_write_array(client, SSD1307FB_COMMAND, cmd, len);
98 static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
100 return ssd1307fb_write_cmd_array(client, &cmd, 1);
103 static inline int ssd1307fb_write_data_array(struct i2c_client *client, u8 *cmd, u32 len)
105 return ssd1307fb_write_array(client, SSD1307FB_DATA, cmd, len);
108 static inline int ssd1307fb_write_data(struct i2c_client *client, u8 data)
110 return ssd1307fb_write_data_array(client, &data, 1);
113 static void ssd1307fb_update_display(struct ssd1307fb_par *par)
115 u8 *vmem = par->info->screen_base;
119 * The screen is divided in pages, each having a height of 8
120 * pixels, and the width of the screen. When sending a byte of
121 * data to the controller, it gives the 8 bits for the current
122 * column. I.e, the first byte are the 8 bits of the first
123 * column, then the 8 bits for the second column, etc.
126 * Representation of the screen, assuming it is 5 bits
127 * wide. Each letter-number combination is a bit that controls
139 * If you want to update this screen, you need to send 5 bytes:
140 * (1) A0 B0 C0 D0 E0 F0 G0 H0
141 * (2) A1 B1 C1 D1 E1 F1 G1 H1
142 * (3) A2 B2 C2 D2 E2 F2 G2 H2
143 * (4) A3 B3 C3 D3 E3 F3 G3 H3
144 * (5) A4 B4 C4 D4 E4 F4 G4 H4
147 for (i = 0; i < (par->height / 8); i++) {
148 ssd1307fb_write_cmd(par->client,
149 SSD1307FB_START_PAGE_ADDRESS + i + par->page_offset);
150 ssd1307fb_write_cmd(par->client, 0x00);
151 ssd1307fb_write_cmd(par->client, 0x10);
153 for (j = 0; j < par->width; j++) {
155 for (k = 0; k < 8; k++) {
156 u32 page_length = par->width * i;
157 u32 index = page_length + (par->width * k + j) / 8;
158 u8 byte = *(vmem + index);
159 u8 bit = byte & (1 << (j % 8));
160 bit = bit >> (j % 8);
163 ssd1307fb_write_data(par->client, buf);
169 static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
170 size_t count, loff_t *ppos)
172 struct ssd1307fb_par *par = info->par;
173 unsigned long total_size;
174 unsigned long p = *ppos;
177 total_size = info->fix.smem_len;
182 if (count + p > total_size)
183 count = total_size - p;
188 dst = (void __force *) (info->screen_base + p);
190 if (copy_from_user(dst, buf, count))
193 ssd1307fb_update_display(par);
200 static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
202 struct ssd1307fb_par *par = info->par;
203 sys_fillrect(info, rect);
204 ssd1307fb_update_display(par);
207 static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
209 struct ssd1307fb_par *par = info->par;
210 sys_copyarea(info, area);
211 ssd1307fb_update_display(par);
214 static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
216 struct ssd1307fb_par *par = info->par;
217 sys_imageblit(info, image);
218 ssd1307fb_update_display(par);
221 static struct fb_ops ssd1307fb_ops = {
222 .owner = THIS_MODULE,
223 .fb_read = fb_sys_read,
224 .fb_write = ssd1307fb_write,
225 .fb_fillrect = ssd1307fb_fillrect,
226 .fb_copyarea = ssd1307fb_copyarea,
227 .fb_imageblit = ssd1307fb_imageblit,
230 static void ssd1307fb_deferred_io(struct fb_info *info,
231 struct list_head *pagelist)
233 ssd1307fb_update_display(info->par);
236 static struct fb_deferred_io ssd1307fb_defio = {
238 .deferred_io = ssd1307fb_deferred_io,
241 static int ssd1307fb_ssd1307_init(struct ssd1307fb_par *par)
245 par->pwm = pwm_get(&par->client->dev, NULL);
246 if (IS_ERR(par->pwm)) {
247 dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
248 return PTR_ERR(par->pwm);
251 par->pwm_period = pwm_get_period(par->pwm);
253 pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
254 pwm_enable(par->pwm);
256 dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
257 par->pwm->pwm, par->pwm_period);
259 /* Map column 127 of the OLED to segment 0 */
260 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
264 /* Turn on the display */
265 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
272 static int ssd1307fb_ssd1307_remove(struct ssd1307fb_par *par)
274 pwm_disable(par->pwm);
279 static struct ssd1307fb_ops ssd1307fb_ssd1307_ops = {
280 .init = ssd1307fb_ssd1307_init,
281 .remove = ssd1307fb_ssd1307_remove,
284 static int ssd1307fb_ssd1306_init(struct ssd1307fb_par *par)
288 /* Set initial contrast */
289 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
290 ret = ret & ssd1307fb_write_cmd(par->client, 0x7f);
294 /* Set COM direction */
295 ret = ssd1307fb_write_cmd(par->client, 0xc8);
299 /* Set segment re-map */
300 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
304 /* Set multiplex ratio value */
305 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
306 ret = ret & ssd1307fb_write_cmd(par->client, par->height - 1);
310 /* set display offset value */
311 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
312 ret = ssd1307fb_write_cmd(par->client, 0x20);
316 /* Set clock frequency */
317 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
318 ret = ret & ssd1307fb_write_cmd(par->client, 0xf0);
322 /* Set precharge period in number of ticks from the internal clock */
323 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
324 ret = ret & ssd1307fb_write_cmd(par->client, 0x22);
328 /* Set COM pins configuration */
329 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
330 ret = ret & ssd1307fb_write_cmd(par->client, 0x22);
335 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
336 ret = ret & ssd1307fb_write_cmd(par->client, 0x49);
340 /* Turn on the DC-DC Charge Pump */
341 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
342 ret = ret & ssd1307fb_write_cmd(par->client, 0x14);
346 /* Turn on the display */
347 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
354 static struct ssd1307fb_ops ssd1307fb_ssd1306_ops = {
355 .init = ssd1307fb_ssd1306_init,
358 static const struct of_device_id ssd1307fb_of_match[] = {
360 .compatible = "solomon,ssd1306fb-i2c",
361 .data = (void *)&ssd1307fb_ssd1306_ops,
364 .compatible = "solomon,ssd1307fb-i2c",
365 .data = (void *)&ssd1307fb_ssd1307_ops,
369 MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
371 static int ssd1307fb_probe(struct i2c_client *client,
372 const struct i2c_device_id *id)
374 struct fb_info *info;
375 struct device_node *node = client->dev.of_node;
377 struct ssd1307fb_par *par;
382 dev_err(&client->dev, "No device tree data found!\n");
386 info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
388 dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
394 par->client = client;
396 par->ops = (struct ssd1307fb_ops *)of_match_device(ssd1307fb_of_match,
399 par->reset = of_get_named_gpio(client->dev.of_node,
401 if (!gpio_is_valid(par->reset)) {
406 if (of_property_read_u32(node, "solomon,width", &par->width))
409 if (of_property_read_u32(node, "solomon,height", &par->height))
412 if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
413 par->page_offset = 1;
415 vmem_size = par->width * par->height / 8;
417 vmem = devm_kzalloc(&client->dev, vmem_size, GFP_KERNEL);
419 dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
424 info->fbops = &ssd1307fb_ops;
425 info->fix = ssd1307fb_fix;
426 info->fix.line_length = par->width / 8;
427 info->fbdefio = &ssd1307fb_defio;
429 info->var = ssd1307fb_var;
430 info->var.xres = par->width;
431 info->var.xres_virtual = par->width;
432 info->var.yres = par->height;
433 info->var.yres_virtual = par->height;
435 info->var.red.length = 1;
436 info->var.red.offset = 0;
437 info->var.green.length = 1;
438 info->var.green.offset = 0;
439 info->var.blue.length = 1;
440 info->var.blue.offset = 0;
442 info->screen_base = (u8 __force __iomem *)vmem;
443 info->fix.smem_start = (unsigned long)vmem;
444 info->fix.smem_len = vmem_size;
446 fb_deferred_io_init(info);
448 ret = devm_gpio_request_one(&client->dev, par->reset,
452 dev_err(&client->dev,
453 "failed to request gpio %d: %d\n",
455 goto reset_oled_error;
458 i2c_set_clientdata(client, info);
460 /* Reset the screen */
461 gpio_set_value(par->reset, 0);
463 gpio_set_value(par->reset, 1);
466 if (par->ops->init) {
467 ret = par->ops->init(par);
469 goto reset_oled_error;
472 ret = register_framebuffer(info);
474 dev_err(&client->dev, "Couldn't register the framebuffer\n");
475 goto panel_init_error;
478 dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
483 if (par->ops->remove)
484 par->ops->remove(par);
486 fb_deferred_io_cleanup(info);
488 framebuffer_release(info);
492 static int ssd1307fb_remove(struct i2c_client *client)
494 struct fb_info *info = i2c_get_clientdata(client);
495 struct ssd1307fb_par *par = info->par;
497 unregister_framebuffer(info);
498 if (par->ops->remove)
499 par->ops->remove(par);
500 fb_deferred_io_cleanup(info);
501 framebuffer_release(info);
506 static const struct i2c_device_id ssd1307fb_i2c_id[] = {
511 MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
513 static struct i2c_driver ssd1307fb_driver = {
514 .probe = ssd1307fb_probe,
515 .remove = ssd1307fb_remove,
516 .id_table = ssd1307fb_i2c_id,
519 .of_match_table = of_match_ptr(ssd1307fb_of_match),
520 .owner = THIS_MODULE,
524 module_i2c_driver(ssd1307fb_driver);
526 MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
527 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
528 MODULE_LICENSE("GPL");