video: ssd1307fb: Add support for SSD1306 OLED controller
[cascardo/linux.git] / drivers / video / ssd1307fb.c
1 /*
2  * Driver for the Solomon SSD1307 OLED controller
3  *
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
19 #define SSD1307FB_DATA                  0x40
20 #define SSD1307FB_COMMAND               0x80
21
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
34
35 struct ssd1307fb_par;
36
37 struct ssd1307fb_ops {
38         int (*init)(struct ssd1307fb_par *);
39         int (*remove)(struct ssd1307fb_par *);
40 };
41
42 struct ssd1307fb_par {
43         struct i2c_client *client;
44         u32 height;
45         struct fb_info *info;
46         struct ssd1307fb_ops *ops;
47         u32 page_offset;
48         struct pwm_device *pwm;
49         u32 pwm_period;
50         int reset;
51         u32 width;
52 };
53
54 static struct fb_fix_screeninfo ssd1307fb_fix = {
55         .id             = "Solomon SSD1307",
56         .type           = FB_TYPE_PACKED_PIXELS,
57         .visual         = FB_VISUAL_MONO10,
58         .xpanstep       = 0,
59         .ypanstep       = 0,
60         .ywrapstep      = 0,
61         .accel          = FB_ACCEL_NONE,
62 };
63
64 static struct fb_var_screeninfo ssd1307fb_var = {
65         .bits_per_pixel = 1,
66 };
67
68 static int ssd1307fb_write_array(struct i2c_client *client, u8 type, u8 *cmd, u32 len)
69 {
70         u8 *buf;
71         int ret = 0;
72
73         buf = kzalloc(len + 1, GFP_KERNEL);
74         if (!buf) {
75                 dev_err(&client->dev, "Couldn't allocate sending buffer.\n");
76                 return -ENOMEM;
77         }
78
79         buf[0] = type;
80         memcpy(buf + 1, cmd, len);
81
82         ret = i2c_master_send(client, buf, len + 1);
83         if (ret != len + 1) {
84                 dev_err(&client->dev, "Couldn't send I2C command.\n");
85                 goto error;
86         }
87
88 error:
89         kfree(buf);
90         return ret;
91 }
92
93 static inline int ssd1307fb_write_cmd_array(struct i2c_client *client, u8 *cmd, u32 len)
94 {
95         return ssd1307fb_write_array(client, SSD1307FB_COMMAND, cmd, len);
96 }
97
98 static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
99 {
100         return ssd1307fb_write_cmd_array(client, &cmd, 1);
101 }
102
103 static inline int ssd1307fb_write_data_array(struct i2c_client *client, u8 *cmd, u32 len)
104 {
105         return ssd1307fb_write_array(client, SSD1307FB_DATA, cmd, len);
106 }
107
108 static inline int ssd1307fb_write_data(struct i2c_client *client, u8 data)
109 {
110         return ssd1307fb_write_data_array(client, &data, 1);
111 }
112
113 static void ssd1307fb_update_display(struct ssd1307fb_par *par)
114 {
115         u8 *vmem = par->info->screen_base;
116         int i, j, k;
117
118         /*
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.
124          *
125          *
126          * Representation of the screen, assuming it is 5 bits
127          * wide. Each letter-number combination is a bit that controls
128          * one pixel.
129          *
130          * A0 A1 A2 A3 A4
131          * B0 B1 B2 B3 B4
132          * C0 C1 C2 C3 C4
133          * D0 D1 D2 D3 D4
134          * E0 E1 E2 E3 E4
135          * F0 F1 F2 F3 F4
136          * G0 G1 G2 G3 G4
137          * H0 H1 H2 H3 H4
138          *
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
145          */
146
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);
152
153                 for (j = 0; j < par->width; j++) {
154                         u8 buf = 0;
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);
161                                 buf |= bit << k;
162                         }
163                         ssd1307fb_write_data(par->client, buf);
164                 }
165         }
166 }
167
168
169 static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
170                 size_t count, loff_t *ppos)
171 {
172         struct ssd1307fb_par *par = info->par;
173         unsigned long total_size;
174         unsigned long p = *ppos;
175         u8 __iomem *dst;
176
177         total_size = info->fix.smem_len;
178
179         if (p > total_size)
180                 return -EINVAL;
181
182         if (count + p > total_size)
183                 count = total_size - p;
184
185         if (!count)
186                 return -EINVAL;
187
188         dst = (void __force *) (info->screen_base + p);
189
190         if (copy_from_user(dst, buf, count))
191                 return -EFAULT;
192
193         ssd1307fb_update_display(par);
194
195         *ppos += count;
196
197         return count;
198 }
199
200 static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
201 {
202         struct ssd1307fb_par *par = info->par;
203         sys_fillrect(info, rect);
204         ssd1307fb_update_display(par);
205 }
206
207 static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
208 {
209         struct ssd1307fb_par *par = info->par;
210         sys_copyarea(info, area);
211         ssd1307fb_update_display(par);
212 }
213
214 static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
215 {
216         struct ssd1307fb_par *par = info->par;
217         sys_imageblit(info, image);
218         ssd1307fb_update_display(par);
219 }
220
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,
228 };
229
230 static void ssd1307fb_deferred_io(struct fb_info *info,
231                                 struct list_head *pagelist)
232 {
233         ssd1307fb_update_display(info->par);
234 }
235
236 static struct fb_deferred_io ssd1307fb_defio = {
237         .delay          = HZ,
238         .deferred_io    = ssd1307fb_deferred_io,
239 };
240
241 static int ssd1307fb_ssd1307_init(struct ssd1307fb_par *par)
242 {
243         int ret;
244
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);
249         }
250
251         par->pwm_period = pwm_get_period(par->pwm);
252         /* Enable the PWM */
253         pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
254         pwm_enable(par->pwm);
255
256         dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
257                 par->pwm->pwm, par->pwm_period);
258
259         /* Map column 127 of the OLED to segment 0 */
260         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
261         if (ret < 0)
262                 return ret;
263
264         /* Turn on the display */
265         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
266         if (ret < 0)
267                 return ret;
268
269         return 0;
270 }
271
272 static int ssd1307fb_ssd1307_remove(struct ssd1307fb_par *par)
273 {
274         pwm_disable(par->pwm);
275         pwm_put(par->pwm);
276         return 0;
277 }
278
279 static struct ssd1307fb_ops ssd1307fb_ssd1307_ops = {
280         .init   = ssd1307fb_ssd1307_init,
281         .remove = ssd1307fb_ssd1307_remove,
282 };
283
284 static int ssd1307fb_ssd1306_init(struct ssd1307fb_par *par)
285 {
286         int ret;
287
288         /* Set initial contrast */
289         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
290         ret = ret & ssd1307fb_write_cmd(par->client, 0x7f);
291         if (ret < 0)
292                 return ret;
293
294         /* Set COM direction */
295         ret = ssd1307fb_write_cmd(par->client, 0xc8);
296         if (ret < 0)
297                 return ret;
298
299         /* Set segment re-map */
300         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
301         if (ret < 0)
302                 return ret;
303
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);
307         if (ret < 0)
308                 return ret;
309
310         /* set display offset value */
311         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
312         ret = ssd1307fb_write_cmd(par->client, 0x20);
313         if (ret < 0)
314                 return ret;
315
316         /* Set clock frequency */
317         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
318         ret = ret & ssd1307fb_write_cmd(par->client, 0xf0);
319         if (ret < 0)
320                 return ret;
321
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);
325         if (ret < 0)
326                 return ret;
327
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);
331         if (ret < 0)
332                 return ret;
333
334         /* Set VCOMH */
335         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
336         ret = ret & ssd1307fb_write_cmd(par->client, 0x49);
337         if (ret < 0)
338                 return ret;
339
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);
343         if (ret < 0)
344                 return ret;
345
346         /* Turn on the display */
347         ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
348         if (ret < 0)
349                 return ret;
350
351         return 0;
352 }
353
354 static struct ssd1307fb_ops ssd1307fb_ssd1306_ops = {
355         .init   = ssd1307fb_ssd1306_init,
356 };
357
358 static const struct of_device_id ssd1307fb_of_match[] = {
359         {
360                 .compatible = "solomon,ssd1306fb-i2c",
361                 .data = (void *)&ssd1307fb_ssd1306_ops,
362         },
363         {
364                 .compatible = "solomon,ssd1307fb-i2c",
365                 .data = (void *)&ssd1307fb_ssd1307_ops,
366         },
367         {},
368 };
369 MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
370
371 static int ssd1307fb_probe(struct i2c_client *client,
372                            const struct i2c_device_id *id)
373 {
374         struct fb_info *info;
375         struct device_node *node = client->dev.of_node;
376         u32 vmem_size;
377         struct ssd1307fb_par *par;
378         u8 *vmem;
379         int ret;
380
381         if (!node) {
382                 dev_err(&client->dev, "No device tree data found!\n");
383                 return -EINVAL;
384         }
385
386         info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
387         if (!info) {
388                 dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
389                 return -ENOMEM;
390         }
391
392         par = info->par;
393         par->info = info;
394         par->client = client;
395
396         par->ops = (struct ssd1307fb_ops *)of_match_device(ssd1307fb_of_match,
397                                                            &client->dev)->data;
398
399         par->reset = of_get_named_gpio(client->dev.of_node,
400                                          "reset-gpios", 0);
401         if (!gpio_is_valid(par->reset)) {
402                 ret = -EINVAL;
403                 goto fb_alloc_error;
404         }
405
406         if (of_property_read_u32(node, "solomon,width", &par->width))
407                 par->width = 96;
408
409         if (of_property_read_u32(node, "solomon,height", &par->height))
410                 par->width = 16;
411
412         if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
413                 par->page_offset = 1;
414
415         vmem_size = par->width * par->height / 8;
416
417         vmem = devm_kzalloc(&client->dev, vmem_size, GFP_KERNEL);
418         if (!vmem) {
419                 dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
420                 ret = -ENOMEM;
421                 goto fb_alloc_error;
422         }
423
424         info->fbops = &ssd1307fb_ops;
425         info->fix = ssd1307fb_fix;
426         info->fix.line_length = par->width / 8;
427         info->fbdefio = &ssd1307fb_defio;
428
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;
434
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;
441
442         info->screen_base = (u8 __force __iomem *)vmem;
443         info->fix.smem_start = (unsigned long)vmem;
444         info->fix.smem_len = vmem_size;
445
446         fb_deferred_io_init(info);
447
448         ret = devm_gpio_request_one(&client->dev, par->reset,
449                                     GPIOF_OUT_INIT_HIGH,
450                                     "oled-reset");
451         if (ret) {
452                 dev_err(&client->dev,
453                         "failed to request gpio %d: %d\n",
454                         par->reset, ret);
455                 goto reset_oled_error;
456         }
457
458         i2c_set_clientdata(client, info);
459
460         /* Reset the screen */
461         gpio_set_value(par->reset, 0);
462         udelay(4);
463         gpio_set_value(par->reset, 1);
464         udelay(4);
465
466         if (par->ops->init) {
467                 ret = par->ops->init(par);
468                 if (ret)
469                         goto reset_oled_error;
470         }
471
472         ret = register_framebuffer(info);
473         if (ret) {
474                 dev_err(&client->dev, "Couldn't register the framebuffer\n");
475                 goto panel_init_error;
476         }
477
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);
479
480         return 0;
481
482 panel_init_error:
483         if (par->ops->remove)
484                 par->ops->remove(par);
485 reset_oled_error:
486         fb_deferred_io_cleanup(info);
487 fb_alloc_error:
488         framebuffer_release(info);
489         return ret;
490 }
491
492 static int ssd1307fb_remove(struct i2c_client *client)
493 {
494         struct fb_info *info = i2c_get_clientdata(client);
495         struct ssd1307fb_par *par = info->par;
496
497         unregister_framebuffer(info);
498         if (par->ops->remove)
499                 par->ops->remove(par);
500         fb_deferred_io_cleanup(info);
501         framebuffer_release(info);
502
503         return 0;
504 }
505
506 static const struct i2c_device_id ssd1307fb_i2c_id[] = {
507         { "ssd1306fb", 0 },
508         { "ssd1307fb", 0 },
509         { }
510 };
511 MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
512
513 static struct i2c_driver ssd1307fb_driver = {
514         .probe = ssd1307fb_probe,
515         .remove = ssd1307fb_remove,
516         .id_table = ssd1307fb_i2c_id,
517         .driver = {
518                 .name = "ssd1307fb",
519                 .of_match_table = of_match_ptr(ssd1307fb_of_match),
520                 .owner = THIS_MODULE,
521         },
522 };
523
524 module_i2c_driver(ssd1307fb_driver);
525
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");