blob: 9daf058917a7522aab15a3387bec9fa0b71656a4 [file] [log] [blame]
Maxime Riparda2ed00d2012-12-07 12:30:38 -08001/*
Masanari Iidaa895d572013-04-09 02:06:50 +09002 * Driver for the Solomon SSD1307 OLED controller
Maxime Riparda2ed00d2012-12-07 12:30:38 -08003 *
4 * Copyright 2012 Free Electrons
5 *
6 * Licensed under the GPLv2 or later.
7 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/i2c.h>
12#include <linux/fb.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>
18
Maxime Riparda2ed00d2012-12-07 12:30:38 -080019#define SSD1307FB_DATA 0x40
20#define SSD1307FB_COMMAND 0x80
21
22#define SSD1307FB_CONTRAST 0x81
Maxime Ripardbbc79082013-04-22 11:55:54 +020023#define SSD1307FB_CHARGE_PUMP 0x8d
Maxime Riparda2ed00d2012-12-07 12:30:38 -080024#define SSD1307FB_SEG_REMAP_ON 0xa1
25#define SSD1307FB_DISPLAY_OFF 0xae
Maxime Ripardbbc79082013-04-22 11:55:54 +020026#define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8
Maxime Riparda2ed00d2012-12-07 12:30:38 -080027#define SSD1307FB_DISPLAY_ON 0xaf
28#define SSD1307FB_START_PAGE_ADDRESS 0xb0
Maxime Ripardbbc79082013-04-22 11:55:54 +020029#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
34
35struct ssd1307fb_par;
36
37struct ssd1307fb_ops {
38 int (*init)(struct ssd1307fb_par *);
39 int (*remove)(struct ssd1307fb_par *);
40};
Maxime Riparda2ed00d2012-12-07 12:30:38 -080041
42struct ssd1307fb_par {
43 struct i2c_client *client;
Maxime Ripardbbc79082013-04-22 11:55:54 +020044 u32 height;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080045 struct fb_info *info;
Maxime Ripardbbc79082013-04-22 11:55:54 +020046 struct ssd1307fb_ops *ops;
47 u32 page_offset;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080048 struct pwm_device *pwm;
49 u32 pwm_period;
50 int reset;
Maxime Ripardbbc79082013-04-22 11:55:54 +020051 u32 width;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080052};
53
Maxime Ripard9f7714d2013-04-22 12:02:23 +020054struct ssd1307fb_array {
55 u8 type;
56 u8 data[0];
57};
58
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080059static struct fb_fix_screeninfo ssd1307fb_fix = {
Maxime Riparda2ed00d2012-12-07 12:30:38 -080060 .id = "Solomon SSD1307",
61 .type = FB_TYPE_PACKED_PIXELS,
62 .visual = FB_VISUAL_MONO10,
63 .xpanstep = 0,
64 .ypanstep = 0,
65 .ywrapstep = 0,
Maxime Riparda2ed00d2012-12-07 12:30:38 -080066 .accel = FB_ACCEL_NONE,
67};
68
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080069static struct fb_var_screeninfo ssd1307fb_var = {
Maxime Riparda2ed00d2012-12-07 12:30:38 -080070 .bits_per_pixel = 1,
71};
72
Maxime Ripard9f7714d2013-04-22 12:02:23 +020073static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type)
Maxime Riparda2ed00d2012-12-07 12:30:38 -080074{
Maxime Ripard9f7714d2013-04-22 12:02:23 +020075 struct ssd1307fb_array *array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080076
Maxime Ripard9f7714d2013-04-22 12:02:23 +020077 array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL);
78 if (!array)
79 return NULL;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080080
Maxime Ripard9f7714d2013-04-22 12:02:23 +020081 array->type = type;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080082
Maxime Ripard9f7714d2013-04-22 12:02:23 +020083 return array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080084}
85
Maxime Ripard9f7714d2013-04-22 12:02:23 +020086static int ssd1307fb_write_array(struct i2c_client *client,
87 struct ssd1307fb_array *array, u32 len)
Maxime Riparda2ed00d2012-12-07 12:30:38 -080088{
Maxime Ripard9f7714d2013-04-22 12:02:23 +020089 int ret;
90
91 len += sizeof(struct ssd1307fb_array);
92
93 ret = i2c_master_send(client, (u8 *)array, len);
94 if (ret != len) {
95 dev_err(&client->dev, "Couldn't send I2C command.\n");
96 return ret;
97 }
98
99 return 0;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800100}
101
102static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
103{
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200104 struct ssd1307fb_array *array;
105 int ret;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800106
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200107 array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND);
108 if (!array)
109 return -ENOMEM;
110
111 array->data[0] = cmd;
112
113 ret = ssd1307fb_write_array(client, array, 1);
114 kfree(array);
115
116 return ret;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800117}
118
119static inline int ssd1307fb_write_data(struct i2c_client *client, u8 data)
120{
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200121 struct ssd1307fb_array *array;
122 int ret;
123
124 array = ssd1307fb_alloc_array(1, SSD1307FB_DATA);
125 if (!array)
126 return -ENOMEM;
127
128 array->data[0] = data;
129
130 ret = ssd1307fb_write_array(client, array, 1);
131 kfree(array);
132
133 return ret;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800134}
135
136static void ssd1307fb_update_display(struct ssd1307fb_par *par)
137{
138 u8 *vmem = par->info->screen_base;
139 int i, j, k;
140
141 /*
142 * The screen is divided in pages, each having a height of 8
143 * pixels, and the width of the screen. When sending a byte of
144 * data to the controller, it gives the 8 bits for the current
145 * column. I.e, the first byte are the 8 bits of the first
146 * column, then the 8 bits for the second column, etc.
147 *
148 *
149 * Representation of the screen, assuming it is 5 bits
150 * wide. Each letter-number combination is a bit that controls
151 * one pixel.
152 *
153 * A0 A1 A2 A3 A4
154 * B0 B1 B2 B3 B4
155 * C0 C1 C2 C3 C4
156 * D0 D1 D2 D3 D4
157 * E0 E1 E2 E3 E4
158 * F0 F1 F2 F3 F4
159 * G0 G1 G2 G3 G4
160 * H0 H1 H2 H3 H4
161 *
162 * If you want to update this screen, you need to send 5 bytes:
163 * (1) A0 B0 C0 D0 E0 F0 G0 H0
164 * (2) A1 B1 C1 D1 E1 F1 G1 H1
165 * (3) A2 B2 C2 D2 E2 F2 G2 H2
166 * (4) A3 B3 C3 D3 E3 F3 G3 H3
167 * (5) A4 B4 C4 D4 E4 F4 G4 H4
168 */
169
Maxime Ripardbbc79082013-04-22 11:55:54 +0200170 for (i = 0; i < (par->height / 8); i++) {
171 ssd1307fb_write_cmd(par->client,
172 SSD1307FB_START_PAGE_ADDRESS + i + par->page_offset);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800173 ssd1307fb_write_cmd(par->client, 0x00);
174 ssd1307fb_write_cmd(par->client, 0x10);
175
Maxime Ripardbbc79082013-04-22 11:55:54 +0200176 for (j = 0; j < par->width; j++) {
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800177 u8 buf = 0;
178 for (k = 0; k < 8; k++) {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200179 u32 page_length = par->width * i;
180 u32 index = page_length + (par->width * k + j) / 8;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800181 u8 byte = *(vmem + index);
Maxime Ripard552f0cc2013-01-11 14:31:43 -0800182 u8 bit = byte & (1 << (j % 8));
183 bit = bit >> (j % 8);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800184 buf |= bit << k;
185 }
186 ssd1307fb_write_data(par->client, buf);
187 }
188 }
189}
190
191
192static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
193 size_t count, loff_t *ppos)
194{
195 struct ssd1307fb_par *par = info->par;
196 unsigned long total_size;
197 unsigned long p = *ppos;
198 u8 __iomem *dst;
199
200 total_size = info->fix.smem_len;
201
202 if (p > total_size)
203 return -EINVAL;
204
205 if (count + p > total_size)
206 count = total_size - p;
207
208 if (!count)
209 return -EINVAL;
210
211 dst = (void __force *) (info->screen_base + p);
212
213 if (copy_from_user(dst, buf, count))
214 return -EFAULT;
215
216 ssd1307fb_update_display(par);
217
218 *ppos += count;
219
220 return count;
221}
222
223static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
224{
225 struct ssd1307fb_par *par = info->par;
226 sys_fillrect(info, rect);
227 ssd1307fb_update_display(par);
228}
229
230static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
231{
232 struct ssd1307fb_par *par = info->par;
233 sys_copyarea(info, area);
234 ssd1307fb_update_display(par);
235}
236
237static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
238{
239 struct ssd1307fb_par *par = info->par;
240 sys_imageblit(info, image);
241 ssd1307fb_update_display(par);
242}
243
244static struct fb_ops ssd1307fb_ops = {
245 .owner = THIS_MODULE,
246 .fb_read = fb_sys_read,
247 .fb_write = ssd1307fb_write,
248 .fb_fillrect = ssd1307fb_fillrect,
249 .fb_copyarea = ssd1307fb_copyarea,
250 .fb_imageblit = ssd1307fb_imageblit,
251};
252
253static void ssd1307fb_deferred_io(struct fb_info *info,
254 struct list_head *pagelist)
255{
256 ssd1307fb_update_display(info->par);
257}
258
259static struct fb_deferred_io ssd1307fb_defio = {
260 .delay = HZ,
261 .deferred_io = ssd1307fb_deferred_io,
262};
263
Maxime Ripardbbc79082013-04-22 11:55:54 +0200264static int ssd1307fb_ssd1307_init(struct ssd1307fb_par *par)
265{
266 int ret;
267
268 par->pwm = pwm_get(&par->client->dev, NULL);
269 if (IS_ERR(par->pwm)) {
270 dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
271 return PTR_ERR(par->pwm);
272 }
273
274 par->pwm_period = pwm_get_period(par->pwm);
275 /* Enable the PWM */
276 pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
277 pwm_enable(par->pwm);
278
279 dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
280 par->pwm->pwm, par->pwm_period);
281
282 /* Map column 127 of the OLED to segment 0 */
283 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
284 if (ret < 0)
285 return ret;
286
287 /* Turn on the display */
288 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
289 if (ret < 0)
290 return ret;
291
292 return 0;
293}
294
295static int ssd1307fb_ssd1307_remove(struct ssd1307fb_par *par)
296{
297 pwm_disable(par->pwm);
298 pwm_put(par->pwm);
299 return 0;
300}
301
302static struct ssd1307fb_ops ssd1307fb_ssd1307_ops = {
303 .init = ssd1307fb_ssd1307_init,
304 .remove = ssd1307fb_ssd1307_remove,
305};
306
307static int ssd1307fb_ssd1306_init(struct ssd1307fb_par *par)
308{
309 int ret;
310
311 /* Set initial contrast */
312 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
313 ret = ret & ssd1307fb_write_cmd(par->client, 0x7f);
314 if (ret < 0)
315 return ret;
316
317 /* Set COM direction */
318 ret = ssd1307fb_write_cmd(par->client, 0xc8);
319 if (ret < 0)
320 return ret;
321
322 /* Set segment re-map */
323 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
324 if (ret < 0)
325 return ret;
326
327 /* Set multiplex ratio value */
328 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
329 ret = ret & ssd1307fb_write_cmd(par->client, par->height - 1);
330 if (ret < 0)
331 return ret;
332
333 /* set display offset value */
334 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
335 ret = ssd1307fb_write_cmd(par->client, 0x20);
336 if (ret < 0)
337 return ret;
338
339 /* Set clock frequency */
340 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
341 ret = ret & ssd1307fb_write_cmd(par->client, 0xf0);
342 if (ret < 0)
343 return ret;
344
345 /* Set precharge period in number of ticks from the internal clock */
346 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
347 ret = ret & ssd1307fb_write_cmd(par->client, 0x22);
348 if (ret < 0)
349 return ret;
350
351 /* Set COM pins configuration */
352 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
353 ret = ret & ssd1307fb_write_cmd(par->client, 0x22);
354 if (ret < 0)
355 return ret;
356
357 /* Set VCOMH */
358 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
359 ret = ret & ssd1307fb_write_cmd(par->client, 0x49);
360 if (ret < 0)
361 return ret;
362
363 /* Turn on the DC-DC Charge Pump */
364 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
365 ret = ret & ssd1307fb_write_cmd(par->client, 0x14);
366 if (ret < 0)
367 return ret;
368
369 /* Turn on the display */
370 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
371 if (ret < 0)
372 return ret;
373
374 return 0;
375}
376
377static struct ssd1307fb_ops ssd1307fb_ssd1306_ops = {
378 .init = ssd1307fb_ssd1306_init,
379};
380
381static const struct of_device_id ssd1307fb_of_match[] = {
382 {
383 .compatible = "solomon,ssd1306fb-i2c",
384 .data = (void *)&ssd1307fb_ssd1306_ops,
385 },
386 {
387 .compatible = "solomon,ssd1307fb-i2c",
388 .data = (void *)&ssd1307fb_ssd1307_ops,
389 },
390 {},
391};
392MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
393
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800394static int ssd1307fb_probe(struct i2c_client *client,
395 const struct i2c_device_id *id)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800396{
397 struct fb_info *info;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200398 struct device_node *node = client->dev.of_node;
399 u32 vmem_size;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800400 struct ssd1307fb_par *par;
401 u8 *vmem;
402 int ret;
403
Maxime Ripardbbc79082013-04-22 11:55:54 +0200404 if (!node) {
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800405 dev_err(&client->dev, "No device tree data found!\n");
406 return -EINVAL;
407 }
408
409 info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
410 if (!info) {
411 dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
412 return -ENOMEM;
413 }
414
Maxime Ripardbbc79082013-04-22 11:55:54 +0200415 par = info->par;
416 par->info = info;
417 par->client = client;
418
419 par->ops = (struct ssd1307fb_ops *)of_match_device(ssd1307fb_of_match,
420 &client->dev)->data;
421
422 par->reset = of_get_named_gpio(client->dev.of_node,
423 "reset-gpios", 0);
424 if (!gpio_is_valid(par->reset)) {
425 ret = -EINVAL;
426 goto fb_alloc_error;
427 }
428
429 if (of_property_read_u32(node, "solomon,width", &par->width))
430 par->width = 96;
431
432 if (of_property_read_u32(node, "solomon,height", &par->height))
433 par->width = 16;
434
435 if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
436 par->page_offset = 1;
437
438 vmem_size = par->width * par->height / 8;
439
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800440 vmem = devm_kzalloc(&client->dev, vmem_size, GFP_KERNEL);
441 if (!vmem) {
442 dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
443 ret = -ENOMEM;
444 goto fb_alloc_error;
445 }
446
447 info->fbops = &ssd1307fb_ops;
448 info->fix = ssd1307fb_fix;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200449 info->fix.line_length = par->width / 8;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800450 info->fbdefio = &ssd1307fb_defio;
451
452 info->var = ssd1307fb_var;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200453 info->var.xres = par->width;
454 info->var.xres_virtual = par->width;
455 info->var.yres = par->height;
456 info->var.yres_virtual = par->height;
457
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800458 info->var.red.length = 1;
459 info->var.red.offset = 0;
460 info->var.green.length = 1;
461 info->var.green.offset = 0;
462 info->var.blue.length = 1;
463 info->var.blue.offset = 0;
464
465 info->screen_base = (u8 __force __iomem *)vmem;
466 info->fix.smem_start = (unsigned long)vmem;
467 info->fix.smem_len = vmem_size;
468
469 fb_deferred_io_init(info);
470
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800471 ret = devm_gpio_request_one(&client->dev, par->reset,
472 GPIOF_OUT_INIT_HIGH,
473 "oled-reset");
474 if (ret) {
475 dev_err(&client->dev,
476 "failed to request gpio %d: %d\n",
477 par->reset, ret);
478 goto reset_oled_error;
479 }
480
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800481 i2c_set_clientdata(client, info);
482
483 /* Reset the screen */
484 gpio_set_value(par->reset, 0);
485 udelay(4);
486 gpio_set_value(par->reset, 1);
487 udelay(4);
488
Maxime Ripardbbc79082013-04-22 11:55:54 +0200489 if (par->ops->init) {
490 ret = par->ops->init(par);
491 if (ret)
492 goto reset_oled_error;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800493 }
494
Maxime Ripardbbc79082013-04-22 11:55:54 +0200495 ret = register_framebuffer(info);
496 if (ret) {
497 dev_err(&client->dev, "Couldn't register the framebuffer\n");
498 goto panel_init_error;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800499 }
500
501 dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
502
503 return 0;
504
Maxime Ripardbbc79082013-04-22 11:55:54 +0200505panel_init_error:
506 if (par->ops->remove)
507 par->ops->remove(par);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800508reset_oled_error:
509 fb_deferred_io_cleanup(info);
510fb_alloc_error:
511 framebuffer_release(info);
512 return ret;
513}
514
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800515static int ssd1307fb_remove(struct i2c_client *client)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800516{
517 struct fb_info *info = i2c_get_clientdata(client);
518 struct ssd1307fb_par *par = info->par;
519
520 unregister_framebuffer(info);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200521 if (par->ops->remove)
522 par->ops->remove(par);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800523 fb_deferred_io_cleanup(info);
524 framebuffer_release(info);
525
526 return 0;
527}
528
529static const struct i2c_device_id ssd1307fb_i2c_id[] = {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200530 { "ssd1306fb", 0 },
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800531 { "ssd1307fb", 0 },
532 { }
533};
534MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
535
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800536static struct i2c_driver ssd1307fb_driver = {
537 .probe = ssd1307fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800538 .remove = ssd1307fb_remove,
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800539 .id_table = ssd1307fb_i2c_id,
540 .driver = {
541 .name = "ssd1307fb",
542 .of_match_table = of_match_ptr(ssd1307fb_of_match),
543 .owner = THIS_MODULE,
544 },
545};
546
547module_i2c_driver(ssd1307fb_driver);
548
Masanari Iidaa895d572013-04-09 02:06:50 +0900549MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800550MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
551MODULE_LICENSE("GPL");