blob: f685d244fa2a6362d7a408f3e73e2419bf8e1e2b [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
Maxime Ripard301bc062013-04-22 12:02:25 +020022#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
Maxime Riparda2ed00d2012-12-07 12:30:38 -080028#define SSD1307FB_CONTRAST 0x81
Maxime Ripardbbc79082013-04-22 11:55:54 +020029#define SSD1307FB_CHARGE_PUMP 0x8d
Maxime Riparda2ed00d2012-12-07 12:30:38 -080030#define SSD1307FB_SEG_REMAP_ON 0xa1
31#define SSD1307FB_DISPLAY_OFF 0xae
Maxime Ripardbbc79082013-04-22 11:55:54 +020032#define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8
Maxime Riparda2ed00d2012-12-07 12:30:38 -080033#define SSD1307FB_DISPLAY_ON 0xaf
34#define SSD1307FB_START_PAGE_ADDRESS 0xb0
Maxime Ripardbbc79082013-04-22 11:55:54 +020035#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
40
41struct ssd1307fb_par;
42
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020043struct ssd1307fb_deviceinfo {
44 u32 default_vcomh;
45 u32 default_dclk_div;
46 u32 default_dclk_frq;
47 int need_pwm;
48 int need_chargepump;
Maxime Ripardbbc79082013-04-22 11:55:54 +020049};
Maxime Riparda2ed00d2012-12-07 12:30:38 -080050
51struct ssd1307fb_par {
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020052 u32 com_invdir;
53 u32 com_lrremap;
54 u32 com_offset;
55 u32 com_seq;
56 u32 contrast;
57 u32 dclk_div;
58 u32 dclk_frq;
59 struct ssd1307fb_deviceinfo *device_info;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080060 struct i2c_client *client;
Maxime Ripardbbc79082013-04-22 11:55:54 +020061 u32 height;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080062 struct fb_info *info;
Maxime Ripardbbc79082013-04-22 11:55:54 +020063 u32 page_offset;
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020064 u32 prechargep1;
65 u32 prechargep2;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080066 struct pwm_device *pwm;
67 u32 pwm_period;
68 int reset;
Thomas Niederprümc89eacf2015-03-31 20:27:10 +020069 u32 seg_remap;
70 u32 vcomh;
Maxime Ripardbbc79082013-04-22 11:55:54 +020071 u32 width;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080072};
73
Maxime Ripard9f7714d2013-04-22 12:02:23 +020074struct ssd1307fb_array {
75 u8 type;
76 u8 data[0];
77};
78
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080079static struct fb_fix_screeninfo ssd1307fb_fix = {
Maxime Riparda2ed00d2012-12-07 12:30:38 -080080 .id = "Solomon SSD1307",
81 .type = FB_TYPE_PACKED_PIXELS,
82 .visual = FB_VISUAL_MONO10,
83 .xpanstep = 0,
84 .ypanstep = 0,
85 .ywrapstep = 0,
Maxime Riparda2ed00d2012-12-07 12:30:38 -080086 .accel = FB_ACCEL_NONE,
87};
88
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080089static struct fb_var_screeninfo ssd1307fb_var = {
Maxime Riparda2ed00d2012-12-07 12:30:38 -080090 .bits_per_pixel = 1,
91};
92
Maxime Ripard9f7714d2013-04-22 12:02:23 +020093static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type)
Maxime Riparda2ed00d2012-12-07 12:30:38 -080094{
Maxime Ripard9f7714d2013-04-22 12:02:23 +020095 struct ssd1307fb_array *array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -080096
Maxime Ripard9f7714d2013-04-22 12:02:23 +020097 array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL);
98 if (!array)
99 return NULL;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800100
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200101 array->type = type;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800102
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200103 return array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800104}
105
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200106static int ssd1307fb_write_array(struct i2c_client *client,
107 struct ssd1307fb_array *array, u32 len)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800108{
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200109 int ret;
110
111 len += sizeof(struct ssd1307fb_array);
112
113 ret = i2c_master_send(client, (u8 *)array, len);
114 if (ret != len) {
115 dev_err(&client->dev, "Couldn't send I2C command.\n");
116 return ret;
117 }
118
119 return 0;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800120}
121
122static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
123{
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200124 struct ssd1307fb_array *array;
125 int ret;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800126
Maxime Ripard9f7714d2013-04-22 12:02:23 +0200127 array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND);
128 if (!array)
129 return -ENOMEM;
130
131 array->data[0] = cmd;
132
133 ret = ssd1307fb_write_array(client, array, 1);
134 kfree(array);
135
136 return ret;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800137}
138
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800139static void ssd1307fb_update_display(struct ssd1307fb_par *par)
140{
Maxime Ripard301bc062013-04-22 12:02:25 +0200141 struct ssd1307fb_array *array;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800142 u8 *vmem = par->info->screen_base;
143 int i, j, k;
144
Maxime Ripard301bc062013-04-22 12:02:25 +0200145 array = ssd1307fb_alloc_array(par->width * par->height / 8,
146 SSD1307FB_DATA);
147 if (!array)
148 return;
149
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800150 /*
151 * The screen is divided in pages, each having a height of 8
152 * pixels, and the width of the screen. When sending a byte of
153 * data to the controller, it gives the 8 bits for the current
154 * column. I.e, the first byte are the 8 bits of the first
155 * column, then the 8 bits for the second column, etc.
156 *
157 *
158 * Representation of the screen, assuming it is 5 bits
159 * wide. Each letter-number combination is a bit that controls
160 * one pixel.
161 *
162 * A0 A1 A2 A3 A4
163 * B0 B1 B2 B3 B4
164 * C0 C1 C2 C3 C4
165 * D0 D1 D2 D3 D4
166 * E0 E1 E2 E3 E4
167 * F0 F1 F2 F3 F4
168 * G0 G1 G2 G3 G4
169 * H0 H1 H2 H3 H4
170 *
171 * If you want to update this screen, you need to send 5 bytes:
172 * (1) A0 B0 C0 D0 E0 F0 G0 H0
173 * (2) A1 B1 C1 D1 E1 F1 G1 H1
174 * (3) A2 B2 C2 D2 E2 F2 G2 H2
175 * (4) A3 B3 C3 D3 E3 F3 G3 H3
176 * (5) A4 B4 C4 D4 E4 F4 G4 H4
177 */
178
Maxime Ripardbbc79082013-04-22 11:55:54 +0200179 for (i = 0; i < (par->height / 8); i++) {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200180 for (j = 0; j < par->width; j++) {
Maxime Ripard301bc062013-04-22 12:02:25 +0200181 u32 array_idx = i * par->width + j;
182 array->data[array_idx] = 0;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800183 for (k = 0; k < 8; k++) {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200184 u32 page_length = par->width * i;
185 u32 index = page_length + (par->width * k + j) / 8;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800186 u8 byte = *(vmem + index);
Maxime Ripard552f0cc2013-01-11 14:31:43 -0800187 u8 bit = byte & (1 << (j % 8));
188 bit = bit >> (j % 8);
Maxime Ripard301bc062013-04-22 12:02:25 +0200189 array->data[array_idx] |= bit << k;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800190 }
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800191 }
192 }
Maxime Ripard301bc062013-04-22 12:02:25 +0200193
194 ssd1307fb_write_array(par->client, array, par->width * par->height / 8);
195 kfree(array);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800196}
197
198
199static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
200 size_t count, loff_t *ppos)
201{
202 struct ssd1307fb_par *par = info->par;
203 unsigned long total_size;
204 unsigned long p = *ppos;
205 u8 __iomem *dst;
206
207 total_size = info->fix.smem_len;
208
209 if (p > total_size)
210 return -EINVAL;
211
212 if (count + p > total_size)
213 count = total_size - p;
214
215 if (!count)
216 return -EINVAL;
217
218 dst = (void __force *) (info->screen_base + p);
219
220 if (copy_from_user(dst, buf, count))
221 return -EFAULT;
222
223 ssd1307fb_update_display(par);
224
225 *ppos += count;
226
227 return count;
228}
229
230static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
231{
232 struct ssd1307fb_par *par = info->par;
233 sys_fillrect(info, rect);
234 ssd1307fb_update_display(par);
235}
236
237static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
238{
239 struct ssd1307fb_par *par = info->par;
240 sys_copyarea(info, area);
241 ssd1307fb_update_display(par);
242}
243
244static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
245{
246 struct ssd1307fb_par *par = info->par;
247 sys_imageblit(info, image);
248 ssd1307fb_update_display(par);
249}
250
251static struct fb_ops ssd1307fb_ops = {
252 .owner = THIS_MODULE,
253 .fb_read = fb_sys_read,
254 .fb_write = ssd1307fb_write,
255 .fb_fillrect = ssd1307fb_fillrect,
256 .fb_copyarea = ssd1307fb_copyarea,
257 .fb_imageblit = ssd1307fb_imageblit,
258};
259
260static void ssd1307fb_deferred_io(struct fb_info *info,
261 struct list_head *pagelist)
262{
263 ssd1307fb_update_display(info->par);
264}
265
266static struct fb_deferred_io ssd1307fb_defio = {
267 .delay = HZ,
268 .deferred_io = ssd1307fb_deferred_io,
269};
270
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200271static int ssd1307fb_init(struct ssd1307fb_par *par)
Maxime Ripardbbc79082013-04-22 11:55:54 +0200272{
273 int ret;
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200274 u32 precharge, dclk, com_invdir, compins;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200275
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200276 if (par->device_info->need_pwm) {
277 par->pwm = pwm_get(&par->client->dev, NULL);
278 if (IS_ERR(par->pwm)) {
279 dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
280 return PTR_ERR(par->pwm);
281 }
Maxime Ripardbbc79082013-04-22 11:55:54 +0200282
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200283 par->pwm_period = pwm_get_period(par->pwm);
284 /* Enable the PWM */
285 pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
286 pwm_enable(par->pwm);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200287
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200288 dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
289 par->pwm->pwm, par->pwm_period);
290 };
Maxime Ripardbbc79082013-04-22 11:55:54 +0200291
292 /* Set initial contrast */
293 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000294 if (ret < 0)
295 return ret;
296
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200297 ret = ssd1307fb_write_cmd(par->client, par->contrast);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200298 if (ret < 0)
299 return ret;
300
301 /* Set segment re-map */
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200302 if (par->seg_remap) {
303 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
304 if (ret < 0)
305 return ret;
306 };
307
308 /* Set COM direction */
309 com_invdir = 0xc0 | (par->com_invdir & 0x1) << 3;
310 ret = ssd1307fb_write_cmd(par->client, com_invdir);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200311 if (ret < 0)
312 return ret;
313
314 /* Set multiplex ratio value */
315 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000316 if (ret < 0)
317 return ret;
318
319 ret = ssd1307fb_write_cmd(par->client, par->height - 1);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200320 if (ret < 0)
321 return ret;
322
323 /* set display offset value */
324 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000325 if (ret < 0)
326 return ret;
327
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200328 ret = ssd1307fb_write_cmd(par->client, par->com_offset);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200329 if (ret < 0)
330 return ret;
331
332 /* Set clock frequency */
333 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000334 if (ret < 0)
335 return ret;
336
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200337 dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4;
338 ret = ssd1307fb_write_cmd(par->client, dclk);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200339 if (ret < 0)
340 return ret;
341
342 /* Set precharge period in number of ticks from the internal clock */
343 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000344 if (ret < 0)
345 return ret;
346
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200347 precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4;
348 ret = ssd1307fb_write_cmd(par->client, precharge);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200349 if (ret < 0)
350 return ret;
351
352 /* Set COM pins configuration */
353 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000354 if (ret < 0)
355 return ret;
356
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200357 compins = 0x02 | (!par->com_seq & 0x1) << 4
358 | (par->com_lrremap & 0x1) << 5;
359 ret = ssd1307fb_write_cmd(par->client, compins);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200360 if (ret < 0)
361 return ret;
362
363 /* Set VCOMH */
364 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000365 if (ret < 0)
366 return ret;
367
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200368 ret = ssd1307fb_write_cmd(par->client, par->vcomh);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200369 if (ret < 0)
370 return ret;
371
372 /* Turn on the DC-DC Charge Pump */
373 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000374 if (ret < 0)
375 return ret;
376
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200377 ret = ssd1307fb_write_cmd(par->client,
378 (par->device_info->need_chargepump & 0x1 << 2) & 0x14);
Maxime Ripardbbc79082013-04-22 11:55:54 +0200379 if (ret < 0)
380 return ret;
381
Maxime Ripard301bc062013-04-22 12:02:25 +0200382 /* Switch to horizontal addressing mode */
383 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000384 if (ret < 0)
385 return ret;
386
387 ret = ssd1307fb_write_cmd(par->client,
388 SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL);
Maxime Ripard301bc062013-04-22 12:02:25 +0200389 if (ret < 0)
390 return ret;
391
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200392 /* Set column range */
Maxime Ripard301bc062013-04-22 12:02:25 +0200393 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COL_RANGE);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000394 if (ret < 0)
395 return ret;
396
397 ret = ssd1307fb_write_cmd(par->client, 0x0);
398 if (ret < 0)
399 return ret;
400
401 ret = ssd1307fb_write_cmd(par->client, par->width - 1);
Maxime Ripard301bc062013-04-22 12:02:25 +0200402 if (ret < 0)
403 return ret;
404
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200405 /* Set page range */
Maxime Ripard301bc062013-04-22 12:02:25 +0200406 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PAGE_RANGE);
Prabhakar Lad5b72ae92015-01-15 19:05:37 +0000407 if (ret < 0)
408 return ret;
409
410 ret = ssd1307fb_write_cmd(par->client, 0x0);
411 if (ret < 0)
412 return ret;
413
414 ret = ssd1307fb_write_cmd(par->client,
415 par->page_offset + (par->height / 8) - 1);
Maxime Ripard301bc062013-04-22 12:02:25 +0200416 if (ret < 0)
417 return ret;
418
Maxime Ripardbbc79082013-04-22 11:55:54 +0200419 /* Turn on the display */
420 ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
421 if (ret < 0)
422 return ret;
423
424 return 0;
425}
426
Thomas Niederprüm5f2d36b2015-03-31 20:27:12 +0200427static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = {
428 .default_vcomh = 0x34,
429 .default_dclk_div = 1,
430 .default_dclk_frq = 7,
431};
432
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200433static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = {
434 .default_vcomh = 0x20,
435 .default_dclk_div = 1,
436 .default_dclk_frq = 8,
437 .need_chargepump = 1,
438};
439
440static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = {
441 .default_vcomh = 0x20,
442 .default_dclk_div = 2,
443 .default_dclk_frq = 12,
444 .need_pwm = 1,
Maxime Ripardbbc79082013-04-22 11:55:54 +0200445};
446
447static const struct of_device_id ssd1307fb_of_match[] = {
448 {
Thomas Niederprüm5f2d36b2015-03-31 20:27:12 +0200449 .compatible = "solomon,ssd1305fb-i2c",
450 .data = (void *)&ssd1307fb_ssd1305_deviceinfo,
451 },
452 {
Maxime Ripardbbc79082013-04-22 11:55:54 +0200453 .compatible = "solomon,ssd1306fb-i2c",
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200454 .data = (void *)&ssd1307fb_ssd1306_deviceinfo,
Maxime Ripardbbc79082013-04-22 11:55:54 +0200455 },
456 {
457 .compatible = "solomon,ssd1307fb-i2c",
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200458 .data = (void *)&ssd1307fb_ssd1307_deviceinfo,
Maxime Ripardbbc79082013-04-22 11:55:54 +0200459 },
460 {},
461};
462MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
463
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800464static int ssd1307fb_probe(struct i2c_client *client,
465 const struct i2c_device_id *id)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800466{
467 struct fb_info *info;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200468 struct device_node *node = client->dev.of_node;
469 u32 vmem_size;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800470 struct ssd1307fb_par *par;
471 u8 *vmem;
472 int ret;
473
Maxime Ripardbbc79082013-04-22 11:55:54 +0200474 if (!node) {
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800475 dev_err(&client->dev, "No device tree data found!\n");
476 return -EINVAL;
477 }
478
479 info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
480 if (!info) {
481 dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
482 return -ENOMEM;
483 }
484
Maxime Ripardbbc79082013-04-22 11:55:54 +0200485 par = info->par;
486 par->info = info;
487 par->client = client;
488
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200489 par->device_info = (struct ssd1307fb_deviceinfo *)of_match_device(
490 ssd1307fb_of_match, &client->dev)->data;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200491
492 par->reset = of_get_named_gpio(client->dev.of_node,
493 "reset-gpios", 0);
494 if (!gpio_is_valid(par->reset)) {
495 ret = -EINVAL;
496 goto fb_alloc_error;
497 }
498
499 if (of_property_read_u32(node, "solomon,width", &par->width))
500 par->width = 96;
501
502 if (of_property_read_u32(node, "solomon,height", &par->height))
Lad, Prabhakar9561def2015-01-08 08:17:58 +0000503 par->height = 16;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200504
505 if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
506 par->page_offset = 1;
507
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200508 if (of_property_read_u32(node, "solomon,com-offset", &par->com_offset))
509 par->com_offset = 0;
510
511 if (of_property_read_u32(node, "solomon,prechargep1", &par->prechargep1))
512 par->prechargep1 = 2;
513
514 if (of_property_read_u32(node, "solomon,prechargep2", &par->prechargep2))
515 par->prechargep2 = 2;
516
517 par->seg_remap = !of_property_read_bool(node, "solomon,segment-no-remap");
518 par->com_seq = of_property_read_bool(node, "solomon,com-seq");
519 par->com_lrremap = of_property_read_bool(node, "solomon,com-lrremap");
520 par->com_invdir = of_property_read_bool(node, "solomon,com-invdir");
521
522 par->contrast = 127;
523 par->vcomh = par->device_info->default_vcomh;
524
525 /* Setup display timing */
526 par->dclk_div = par->device_info->default_dclk_div;
527 par->dclk_frq = par->device_info->default_dclk_frq;
528
Maxime Ripardbbc79082013-04-22 11:55:54 +0200529 vmem_size = par->width * par->height / 8;
530
Thomas Niederprümfacd94b2015-03-31 20:27:08 +0200531 vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
532 get_order(vmem_size));
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800533 if (!vmem) {
534 dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
535 ret = -ENOMEM;
536 goto fb_alloc_error;
537 }
538
539 info->fbops = &ssd1307fb_ops;
540 info->fix = ssd1307fb_fix;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200541 info->fix.line_length = par->width / 8;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800542 info->fbdefio = &ssd1307fb_defio;
543
544 info->var = ssd1307fb_var;
Maxime Ripardbbc79082013-04-22 11:55:54 +0200545 info->var.xres = par->width;
546 info->var.xres_virtual = par->width;
547 info->var.yres = par->height;
548 info->var.yres_virtual = par->height;
549
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800550 info->var.red.length = 1;
551 info->var.red.offset = 0;
552 info->var.green.length = 1;
553 info->var.green.offset = 0;
554 info->var.blue.length = 1;
555 info->var.blue.offset = 0;
556
557 info->screen_base = (u8 __force __iomem *)vmem;
Thomas Niederprüm258c0ea2015-03-31 20:27:07 +0200558 info->fix.smem_start = __pa(vmem);
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800559 info->fix.smem_len = vmem_size;
560
561 fb_deferred_io_init(info);
562
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800563 ret = devm_gpio_request_one(&client->dev, par->reset,
564 GPIOF_OUT_INIT_HIGH,
565 "oled-reset");
566 if (ret) {
567 dev_err(&client->dev,
568 "failed to request gpio %d: %d\n",
569 par->reset, ret);
570 goto reset_oled_error;
571 }
572
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800573 i2c_set_clientdata(client, info);
574
575 /* Reset the screen */
576 gpio_set_value(par->reset, 0);
577 udelay(4);
578 gpio_set_value(par->reset, 1);
579 udelay(4);
580
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200581 ret = ssd1307fb_init(par);
582 if (ret)
583 goto reset_oled_error;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800584
Maxime Ripardbbc79082013-04-22 11:55:54 +0200585 ret = register_framebuffer(info);
586 if (ret) {
587 dev_err(&client->dev, "Couldn't register the framebuffer\n");
588 goto panel_init_error;
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800589 }
590
591 dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
592
593 return 0;
594
Maxime Ripardbbc79082013-04-22 11:55:54 +0200595panel_init_error:
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200596 if (par->device_info->need_pwm) {
597 pwm_disable(par->pwm);
598 pwm_put(par->pwm);
599 };
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800600reset_oled_error:
601 fb_deferred_io_cleanup(info);
602fb_alloc_error:
603 framebuffer_release(info);
604 return ret;
605}
606
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800607static int ssd1307fb_remove(struct i2c_client *client)
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800608{
609 struct fb_info *info = i2c_get_clientdata(client);
610 struct ssd1307fb_par *par = info->par;
611
612 unregister_framebuffer(info);
Thomas Niederprümc89eacf2015-03-31 20:27:10 +0200613 if (par->device_info->need_pwm) {
614 pwm_disable(par->pwm);
615 pwm_put(par->pwm);
616 };
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800617 fb_deferred_io_cleanup(info);
Thomas Niederprümfacd94b2015-03-31 20:27:08 +0200618 __free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len));
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800619 framebuffer_release(info);
620
621 return 0;
622}
623
624static const struct i2c_device_id ssd1307fb_i2c_id[] = {
Thomas Niederprüm5f2d36b2015-03-31 20:27:12 +0200625 { "ssd1305fb", 0 },
Maxime Ripardbbc79082013-04-22 11:55:54 +0200626 { "ssd1306fb", 0 },
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800627 { "ssd1307fb", 0 },
628 { }
629};
630MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
631
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800632static struct i2c_driver ssd1307fb_driver = {
633 .probe = ssd1307fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800634 .remove = ssd1307fb_remove,
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800635 .id_table = ssd1307fb_i2c_id,
636 .driver = {
637 .name = "ssd1307fb",
Sachin Kamatd71f8cd2013-09-30 14:15:31 +0530638 .of_match_table = ssd1307fb_of_match,
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800639 .owner = THIS_MODULE,
640 },
641};
642
643module_i2c_driver(ssd1307fb_driver);
644
Masanari Iidaa895d572013-04-09 02:06:50 +0900645MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
Maxime Riparda2ed00d2012-12-07 12:30:38 -0800646MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
647MODULE_LICENSE("GPL");