blob: 3c4769aab6781a1d218ba1748fc379b70172bbb8 [file] [log] [blame]
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001/*
2 * Copyright (C) 2013 Noralf Tronnes
3 *
4 * This driver is inspired by:
5 * st7735fb.c, Copyright (C) 2011, Matt Porter
6 * broadsheetfb.c, Copyright (C) 2008, Jaya Kumar
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/errno.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/vmalloc.h>
29#include <linux/slab.h>
30#include <linux/init.h>
31#include <linux/fb.h>
32#include <linux/gpio.h>
33#include <linux/spi/spi.h>
34#include <linux/delay.h>
35#include <linux/uaccess.h>
36#include <linux/backlight.h>
37#include <linux/platform_device.h>
38#include <linux/spinlock.h>
39#include <linux/dma-mapping.h>
40#include <linux/of.h>
41#include <linux/of_gpio.h>
42
43#include "fbtft.h"
44
45extern void fbtft_sysfs_init(struct fbtft_par *par);
46extern void fbtft_sysfs_exit(struct fbtft_par *par);
47extern void fbtft_expand_debug_value(unsigned long *debug);
48extern int fbtft_gamma_parse_str(struct fbtft_par *par, unsigned long *curves,
49 const char *str, int size);
50
51static unsigned long debug;
Aya Mahfouz7e059db2015-02-19 04:56:50 +020052module_param(debug, ulong, 0);
Thomas Petazzonic296d5f2014-12-31 10:11:09 +010053MODULE_PARM_DESC(debug, "override device debug level");
54
55static bool dma = true;
56module_param(dma, bool, 0);
57MODULE_PARM_DESC(dma, "Use DMA buffer");
58
59
60void fbtft_dbg_hex(const struct device *dev, int groupsize,
61 void *buf, size_t len, const char *fmt, ...)
62{
63 va_list args;
64 static char textbuf[512];
65 char *text = textbuf;
66 size_t text_len;
67
68 va_start(args, fmt);
69 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
70 va_end(args);
71
72 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
73 512 - text_len, false);
74
75 if (len > 32)
76 dev_info(dev, "%s ...\n", text);
77 else
78 dev_info(dev, "%s\n", text);
79}
80EXPORT_SYMBOL(fbtft_dbg_hex);
81
Krzysztof Adamskied208432015-01-22 19:08:58 +010082static unsigned long fbtft_request_gpios_match(struct fbtft_par *par,
Thomas Petazzonic296d5f2014-12-31 10:11:09 +010083 const struct fbtft_gpio *gpio)
84{
85 int ret;
86 long val;
87
88 fbtft_par_dbg(DEBUG_REQUEST_GPIOS_MATCH, par, "%s('%s')\n",
89 __func__, gpio->name);
90
91 if (strcasecmp(gpio->name, "reset") == 0) {
92 par->gpio.reset = gpio->gpio;
93 return GPIOF_OUT_INIT_HIGH;
94 } else if (strcasecmp(gpio->name, "dc") == 0) {
95 par->gpio.dc = gpio->gpio;
96 return GPIOF_OUT_INIT_LOW;
97 } else if (strcasecmp(gpio->name, "cs") == 0) {
98 par->gpio.cs = gpio->gpio;
99 return GPIOF_OUT_INIT_HIGH;
100 } else if (strcasecmp(gpio->name, "wr") == 0) {
101 par->gpio.wr = gpio->gpio;
102 return GPIOF_OUT_INIT_HIGH;
103 } else if (strcasecmp(gpio->name, "rd") == 0) {
104 par->gpio.rd = gpio->gpio;
105 return GPIOF_OUT_INIT_HIGH;
106 } else if (strcasecmp(gpio->name, "latch") == 0) {
107 par->gpio.latch = gpio->gpio;
108 return GPIOF_OUT_INIT_LOW;
109 } else if (gpio->name[0] == 'd' && gpio->name[1] == 'b') {
110 ret = kstrtol(&gpio->name[2], 10, &val);
111 if (ret == 0 && val < 16) {
112 par->gpio.db[val] = gpio->gpio;
113 return GPIOF_OUT_INIT_LOW;
114 }
115 } else if (strcasecmp(gpio->name, "led") == 0) {
116 par->gpio.led[0] = gpio->gpio;
117 return GPIOF_OUT_INIT_LOW;
118 } else if (strcasecmp(gpio->name, "led_") == 0) {
119 par->gpio.led[0] = gpio->gpio;
120 return GPIOF_OUT_INIT_HIGH;
121 }
122
123 return FBTFT_GPIO_NO_MATCH;
124}
125
Krzysztof Adamskied208432015-01-22 19:08:58 +0100126static int fbtft_request_gpios(struct fbtft_par *par)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100127{
128 struct fbtft_platform_data *pdata = par->pdata;
129 const struct fbtft_gpio *gpio;
130 unsigned long flags;
131 int ret;
132
133 if (pdata && pdata->gpios) {
134 gpio = pdata->gpios;
135 while (gpio->name[0]) {
136 flags = FBTFT_GPIO_NO_MATCH;
137 /* if driver provides match function, try it first,
138 if no match use our own */
139 if (par->fbtftops.request_gpios_match)
140 flags = par->fbtftops.request_gpios_match(par, gpio);
141 if (flags == FBTFT_GPIO_NO_MATCH)
142 flags = fbtft_request_gpios_match(par, gpio);
143 if (flags != FBTFT_GPIO_NO_MATCH) {
144 ret = devm_gpio_request_one(par->info->device,
145 gpio->gpio, flags,
146 par->info->device->driver->name);
147 if (ret < 0) {
148 dev_err(par->info->device,
149 "%s: gpio_request_one('%s'=%d) failed with %d\n",
150 __func__, gpio->name,
151 gpio->gpio, ret);
152 return ret;
153 }
154 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par,
155 "%s: '%s' = GPIO%d\n",
156 __func__, gpio->name, gpio->gpio);
157 }
158 gpio++;
159 }
160 }
161
162 return 0;
163}
164
165#ifdef CONFIG_OF
166static int fbtft_request_one_gpio(struct fbtft_par *par,
167 const char *name, int index, int *gpiop)
168{
169 struct device *dev = par->info->device;
170 struct device_node *node = dev->of_node;
171 int gpio, flags, ret = 0;
172 enum of_gpio_flags of_flags;
173
174 if (of_find_property(node, name, NULL)) {
175 gpio = of_get_named_gpio_flags(node, name, index, &of_flags);
176 if (gpio == -ENOENT)
177 return 0;
178 if (gpio == -EPROBE_DEFER)
179 return gpio;
180 if (gpio < 0) {
181 dev_err(dev,
182 "failed to get '%s' from DT\n", name);
183 return gpio;
184 }
185
186 /* active low translates to initially low */
187 flags = (of_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW :
188 GPIOF_OUT_INIT_HIGH;
189 ret = devm_gpio_request_one(dev, gpio, flags,
190 dev->driver->name);
191 if (ret) {
192 dev_err(dev,
193 "gpio_request_one('%s'=%d) failed with %d\n",
194 name, gpio, ret);
195 return ret;
196 }
197 if (gpiop)
198 *gpiop = gpio;
199 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' = GPIO%d\n",
200 __func__, name, gpio);
201 }
202
203 return ret;
204}
205
206static int fbtft_request_gpios_dt(struct fbtft_par *par)
207{
208 int i;
209 int ret;
210
211 if (!par->info->device->of_node)
212 return -EINVAL;
213
214 ret = fbtft_request_one_gpio(par, "reset-gpios", 0, &par->gpio.reset);
215 if (ret)
216 return ret;
217 ret = fbtft_request_one_gpio(par, "dc-gpios", 0, &par->gpio.dc);
218 if (ret)
219 return ret;
220 ret = fbtft_request_one_gpio(par, "rd-gpios", 0, &par->gpio.rd);
221 if (ret)
222 return ret;
223 ret = fbtft_request_one_gpio(par, "wr-gpios", 0, &par->gpio.wr);
224 if (ret)
225 return ret;
226 ret = fbtft_request_one_gpio(par, "cs-gpios", 0, &par->gpio.cs);
227 if (ret)
228 return ret;
229 ret = fbtft_request_one_gpio(par, "latch-gpios", 0, &par->gpio.latch);
230 if (ret)
231 return ret;
232 for (i = 0; i < 16; i++) {
233 ret = fbtft_request_one_gpio(par, "db-gpios", i,
234 &par->gpio.db[i]);
235 if (ret)
236 return ret;
237 ret = fbtft_request_one_gpio(par, "led-gpios", i,
238 &par->gpio.led[i]);
239 if (ret)
240 return ret;
241 ret = fbtft_request_one_gpio(par, "aux-gpios", i,
242 &par->gpio.aux[i]);
243 if (ret)
244 return ret;
245 }
246
247 return 0;
248}
249#endif
250
251#ifdef CONFIG_FB_BACKLIGHT
Krzysztof Adamskied208432015-01-22 19:08:58 +0100252static int fbtft_backlight_update_status(struct backlight_device *bd)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100253{
254 struct fbtft_par *par = bl_get_data(bd);
255 bool polarity = !!(bd->props.state & BL_CORE_DRIVER1);
256
257 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
258 "%s: polarity=%d, power=%d, fb_blank=%d\n",
259 __func__, polarity, bd->props.power, bd->props.fb_blank);
260
261 if ((bd->props.power == FB_BLANK_UNBLANK) && (bd->props.fb_blank == FB_BLANK_UNBLANK))
262 gpio_set_value(par->gpio.led[0], polarity);
263 else
264 gpio_set_value(par->gpio.led[0], !polarity);
265
266 return 0;
267}
268
Krzysztof Adamskied208432015-01-22 19:08:58 +0100269static int fbtft_backlight_get_brightness(struct backlight_device *bd)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100270{
271 return bd->props.brightness;
272}
273
274void fbtft_unregister_backlight(struct fbtft_par *par)
275{
276 const struct backlight_ops *bl_ops;
277
278 fbtft_par_dbg(DEBUG_BACKLIGHT, par, "%s()\n", __func__);
279
280 if (par->info->bl_dev) {
281 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
282 backlight_update_status(par->info->bl_dev);
283 bl_ops = par->info->bl_dev->ops;
284 backlight_device_unregister(par->info->bl_dev);
285 par->info->bl_dev = NULL;
286 }
287}
288
289void fbtft_register_backlight(struct fbtft_par *par)
290{
291 struct backlight_device *bd;
292 struct backlight_properties bl_props = { 0, };
293 struct backlight_ops *bl_ops;
294
295 fbtft_par_dbg(DEBUG_BACKLIGHT, par, "%s()\n", __func__);
296
297 if (par->gpio.led[0] == -1) {
298 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
299 "%s(): led pin not set, exiting.\n", __func__);
300 return;
301 }
302
303 bl_ops = devm_kzalloc(par->info->device, sizeof(struct backlight_ops),
304 GFP_KERNEL);
Dilek Uzulmez6cb624e2015-03-02 22:56:53 +0200305 if (!bl_ops)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100306 return;
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100307
308 bl_ops->get_brightness = fbtft_backlight_get_brightness;
309 bl_ops->update_status = fbtft_backlight_update_status;
310 bl_props.type = BACKLIGHT_RAW;
311 /* Assume backlight is off, get polarity from current state of pin */
312 bl_props.power = FB_BLANK_POWERDOWN;
313 if (!gpio_get_value(par->gpio.led[0]))
314 bl_props.state |= BL_CORE_DRIVER1;
315
316 bd = backlight_device_register(dev_driver_string(par->info->device),
317 par->info->device, par, bl_ops, &bl_props);
318 if (IS_ERR(bd)) {
319 dev_err(par->info->device,
320 "cannot register backlight device (%ld)\n",
321 PTR_ERR(bd));
322 return;
323 }
324 par->info->bl_dev = bd;
325
326 if (!par->fbtftops.unregister_backlight)
327 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
328}
329#else
330void fbtft_register_backlight(struct fbtft_par *par) { };
331void fbtft_unregister_backlight(struct fbtft_par *par) { };
332#endif
333EXPORT_SYMBOL(fbtft_register_backlight);
334EXPORT_SYMBOL(fbtft_unregister_backlight);
335
Krzysztof Adamskied208432015-01-22 19:08:58 +0100336static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
337 int ye)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100338{
339 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
340 "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
341
342 /* Column address set */
343 write_reg(par, 0x2A,
344 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
345
346 /* Row adress set */
347 write_reg(par, 0x2B,
348 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
349
350 /* Memory write */
351 write_reg(par, 0x2C);
352}
353
354
Krzysztof Adamskied208432015-01-22 19:08:58 +0100355static void fbtft_reset(struct fbtft_par *par)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100356{
357 if (par->gpio.reset == -1)
358 return;
359 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
360 gpio_set_value(par->gpio.reset, 0);
361 udelay(20);
362 gpio_set_value(par->gpio.reset, 1);
363 mdelay(120);
364}
365
366
Krzysztof Adamskied208432015-01-22 19:08:58 +0100367static void fbtft_update_display(struct fbtft_par *par, unsigned start_line,
368 unsigned end_line)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100369{
370 size_t offset, len;
371 struct timespec ts_start, ts_end, ts_fps, ts_duration;
372 long fps_ms, fps_us, duration_ms, duration_us;
373 long fps, throughput;
374 bool timeit = false;
375 int ret = 0;
376
377 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE | DEBUG_TIME_EACH_UPDATE))) {
Yeliz Taneroglu6ba67a52015-03-02 17:49:59 +0200378 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100379 ((par->debug & DEBUG_TIME_FIRST_UPDATE) && !par->first_update_done)) {
380 getnstimeofday(&ts_start);
381 timeit = true;
382 }
383 }
384
385 /* Sanity checks */
386 if (start_line > end_line) {
387 dev_warn(par->info->device,
388 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
389 __func__, start_line, end_line);
390 start_line = 0;
391 end_line = par->info->var.yres - 1;
392 }
393 if (start_line > par->info->var.yres - 1 || end_line > par->info->var.yres - 1) {
394 dev_warn(par->info->device,
395 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
396 __func__, start_line, end_line, par->info->var.yres - 1);
397 start_line = 0;
398 end_line = par->info->var.yres - 1;
399 }
400
401 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
402 __func__, start_line, end_line);
403
404 if (par->fbtftops.set_addr_win)
405 par->fbtftops.set_addr_win(par, 0, start_line,
406 par->info->var.xres-1, end_line);
407
408 offset = start_line * par->info->fix.line_length;
409 len = (end_line - start_line + 1) * par->info->fix.line_length;
410 ret = par->fbtftops.write_vmem(par, offset, len);
411 if (ret < 0)
412 dev_err(par->info->device,
413 "%s: write_vmem failed to update display buffer\n",
414 __func__);
415
416 if (unlikely(timeit)) {
417 getnstimeofday(&ts_end);
418 if (par->update_time.tv_nsec == 0 && par->update_time.tv_sec == 0) {
419 par->update_time.tv_sec = ts_start.tv_sec;
420 par->update_time.tv_nsec = ts_start.tv_nsec;
421 }
422 ts_fps = timespec_sub(ts_start, par->update_time);
423 par->update_time.tv_sec = ts_start.tv_sec;
424 par->update_time.tv_nsec = ts_start.tv_nsec;
425 fps_ms = (ts_fps.tv_sec * 1000) + ((ts_fps.tv_nsec / 1000000) % 1000);
426 fps_us = (ts_fps.tv_nsec / 1000) % 1000;
427 fps = fps_ms * 1000 + fps_us;
428 fps = fps ? 1000000 / fps : 0;
429
430 ts_duration = timespec_sub(ts_end, ts_start);
431 duration_ms = (ts_duration.tv_sec * 1000) + ((ts_duration.tv_nsec / 1000000) % 1000);
432 duration_us = (ts_duration.tv_nsec / 1000) % 1000;
433 throughput = duration_ms * 1000 + duration_us;
434 throughput = throughput ? (len * 1000) / throughput : 0;
435 throughput = throughput * 1000 / 1024;
436
437 dev_info(par->info->device,
438 "Display update: %ld kB/s (%ld.%.3ld ms), fps=%ld (%ld.%.3ld ms)\n",
439 throughput, duration_ms, duration_us,
440 fps, fps_ms, fps_us);
441 par->first_update_done = true;
442 }
443}
444
445
Krzysztof Adamskied208432015-01-22 19:08:58 +0100446static void fbtft_mkdirty(struct fb_info *info, int y, int height)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100447{
448 struct fbtft_par *par = info->par;
449 struct fb_deferred_io *fbdefio = info->fbdefio;
450
451 /* special case, needed ? */
452 if (y == -1) {
453 y = 0;
454 height = info->var.yres - 1;
455 }
456
457 /* Mark display lines/area as dirty */
458 spin_lock(&par->dirty_lock);
459 if (y < par->dirty_lines_start)
460 par->dirty_lines_start = y;
461 if (y + height - 1 > par->dirty_lines_end)
462 par->dirty_lines_end = y + height - 1;
463 spin_unlock(&par->dirty_lock);
464
465 /* Schedule deferred_io to update display (no-op if already on queue)*/
466 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
467}
468
Krzysztof Adamskied208432015-01-22 19:08:58 +0100469static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100470{
471 struct fbtft_par *par = info->par;
472 unsigned dirty_lines_start, dirty_lines_end;
473 struct page *page;
474 unsigned long index;
475 unsigned y_low = 0, y_high = 0;
476 int count = 0;
477
478 spin_lock(&par->dirty_lock);
479 dirty_lines_start = par->dirty_lines_start;
480 dirty_lines_end = par->dirty_lines_end;
481 /* set display line markers as clean */
482 par->dirty_lines_start = par->info->var.yres - 1;
483 par->dirty_lines_end = 0;
484 spin_unlock(&par->dirty_lock);
485
486 /* Mark display lines as dirty */
487 list_for_each_entry(page, pagelist, lru) {
488 count++;
489 index = page->index << PAGE_SHIFT;
490 y_low = index / info->fix.line_length;
491 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
492 fbtft_dev_dbg(DEBUG_DEFERRED_IO, par, info->device,
493 "page->index=%lu y_low=%d y_high=%d\n",
494 page->index, y_low, y_high);
495 if (y_high > info->var.yres - 1)
496 y_high = info->var.yres - 1;
497 if (y_low < dirty_lines_start)
498 dirty_lines_start = y_low;
499 if (y_high > dirty_lines_end)
500 dirty_lines_end = y_high;
501 }
502
503 par->fbtftops.update_display(info->par,
504 dirty_lines_start, dirty_lines_end);
505}
506
507
Krzysztof Adamskied208432015-01-22 19:08:58 +0100508static void fbtft_fb_fillrect(struct fb_info *info,
509 const struct fb_fillrect *rect)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100510{
511 struct fbtft_par *par = info->par;
512
513 fbtft_dev_dbg(DEBUG_FB_FILLRECT, par, info->dev,
514 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
515 __func__, rect->dx, rect->dy, rect->width, rect->height);
516 sys_fillrect(info, rect);
517
518 par->fbtftops.mkdirty(info, rect->dy, rect->height);
519}
520
Krzysztof Adamskied208432015-01-22 19:08:58 +0100521static void fbtft_fb_copyarea(struct fb_info *info,
522 const struct fb_copyarea *area)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100523{
524 struct fbtft_par *par = info->par;
525
526 fbtft_dev_dbg(DEBUG_FB_COPYAREA, par, info->dev,
527 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
528 __func__, area->dx, area->dy, area->width, area->height);
529 sys_copyarea(info, area);
530
531 par->fbtftops.mkdirty(info, area->dy, area->height);
532}
533
Krzysztof Adamskied208432015-01-22 19:08:58 +0100534static void fbtft_fb_imageblit(struct fb_info *info,
535 const struct fb_image *image)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100536{
537 struct fbtft_par *par = info->par;
538
539 fbtft_dev_dbg(DEBUG_FB_IMAGEBLIT, par, info->dev,
540 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
541 __func__, image->dx, image->dy, image->width, image->height);
542 sys_imageblit(info, image);
543
544 par->fbtftops.mkdirty(info, image->dy, image->height);
545}
546
Krzysztof Adamskied208432015-01-22 19:08:58 +0100547static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
548 size_t count, loff_t *ppos)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100549{
550 struct fbtft_par *par = info->par;
551 ssize_t res;
552
553 fbtft_dev_dbg(DEBUG_FB_WRITE, par, info->dev,
554 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
555 res = fb_sys_write(info, buf, count, ppos);
556
557 /* TODO: only mark changed area
558 update all for now */
559 par->fbtftops.mkdirty(info, -1, 0);
560
561 return res;
562}
563
564/* from pxafb.c */
Krzysztof Adamskied208432015-01-22 19:08:58 +0100565static unsigned int chan_to_field(unsigned chan, struct fb_bitfield *bf)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100566{
567 chan &= 0xffff;
568 chan >>= 16 - bf->length;
569 return chan << bf->offset;
570}
571
Krzysztof Adamskied208432015-01-22 19:08:58 +0100572static int fbtft_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
573 unsigned blue, unsigned transp,
574 struct fb_info *info)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100575{
576 struct fbtft_par *par = info->par;
577 unsigned val;
578 int ret = 1;
579
580 fbtft_dev_dbg(DEBUG_FB_SETCOLREG, par, info->dev,
581 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
582 __func__, regno, red, green, blue, transp);
583
584 switch (info->fix.visual) {
585 case FB_VISUAL_TRUECOLOR:
586 if (regno < 16) {
587 u32 *pal = info->pseudo_palette;
588
589 val = chan_to_field(red, &info->var.red);
590 val |= chan_to_field(green, &info->var.green);
591 val |= chan_to_field(blue, &info->var.blue);
592
593 pal[regno] = val;
594 ret = 0;
595 }
596 break;
597
598 }
599 return ret;
600}
601
Krzysztof Adamskied208432015-01-22 19:08:58 +0100602static int fbtft_fb_blank(int blank, struct fb_info *info)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100603{
604 struct fbtft_par *par = info->par;
605 int ret = -EINVAL;
606
607 fbtft_dev_dbg(DEBUG_FB_BLANK, par, info->dev, "%s(blank=%d)\n",
608 __func__, blank);
609
610 if (!par->fbtftops.blank)
611 return ret;
612
613 switch (blank) {
614 case FB_BLANK_POWERDOWN:
615 case FB_BLANK_VSYNC_SUSPEND:
616 case FB_BLANK_HSYNC_SUSPEND:
617 case FB_BLANK_NORMAL:
618 ret = par->fbtftops.blank(par, true);
619 break;
620 case FB_BLANK_UNBLANK:
621 ret = par->fbtftops.blank(par, false);
622 break;
623 }
624 return ret;
625}
626
Krzysztof Adamskied208432015-01-22 19:08:58 +0100627static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100628{
629 if (src->write)
630 dst->write = src->write;
631 if (src->read)
632 dst->read = src->read;
633 if (src->write_vmem)
634 dst->write_vmem = src->write_vmem;
635 if (src->write_register)
636 dst->write_register = src->write_register;
637 if (src->set_addr_win)
638 dst->set_addr_win = src->set_addr_win;
639 if (src->reset)
640 dst->reset = src->reset;
641 if (src->mkdirty)
642 dst->mkdirty = src->mkdirty;
643 if (src->update_display)
644 dst->update_display = src->update_display;
645 if (src->init_display)
646 dst->init_display = src->init_display;
647 if (src->blank)
648 dst->blank = src->blank;
649 if (src->request_gpios_match)
650 dst->request_gpios_match = src->request_gpios_match;
651 if (src->request_gpios)
652 dst->request_gpios = src->request_gpios;
653 if (src->verify_gpios)
654 dst->verify_gpios = src->verify_gpios;
655 if (src->register_backlight)
656 dst->register_backlight = src->register_backlight;
657 if (src->unregister_backlight)
658 dst->unregister_backlight = src->unregister_backlight;
659 if (src->set_var)
660 dst->set_var = src->set_var;
661 if (src->set_gamma)
662 dst->set_gamma = src->set_gamma;
663}
664
665/**
666 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
667 *
668 * @display: pointer to structure describing the display
669 * @dev: pointer to the device for this fb, this can be NULL
670 *
671 * Creates a new frame buffer info structure.
672 *
673 * Also creates and populates the following structures:
674 * info->fbops
675 * info->fbdefio
676 * info->pseudo_palette
677 * par->fbtftops
678 * par->txbuf
679 *
680 * Returns the new structure, or NULL if an error occurred.
681 *
682 */
683struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
684 struct device *dev)
685{
686 struct fb_info *info;
687 struct fbtft_par *par;
688 struct fb_ops *fbops = NULL;
689 struct fb_deferred_io *fbdefio = NULL;
690 struct fbtft_platform_data *pdata = dev->platform_data;
691 u8 *vmem = NULL;
692 void *txbuf = NULL;
693 void *buf = NULL;
694 unsigned width;
695 unsigned height;
696 int txbuflen = display->txbuflen;
697 unsigned bpp = display->bpp;
698 unsigned fps = display->fps;
699 int vmem_size, i;
700 int *init_sequence = display->init_sequence;
701 char *gamma = display->gamma;
702 unsigned long *gamma_curves = NULL;
703
704 /* sanity check */
705 if (display->gamma_num * display->gamma_len > FBTFT_GAMMA_MAX_VALUES_TOTAL) {
706 dev_err(dev,
707 "%s: FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
708 __func__, FBTFT_GAMMA_MAX_VALUES_TOTAL);
709 return NULL;
710 }
711
712 /* defaults */
713 if (!fps)
714 fps = 20;
715 if (!bpp)
716 bpp = 16;
717
718 if (!pdata) {
719 dev_err(dev, "platform data is missing\n");
720 return NULL;
721 }
722
723 /* override driver values? */
724 if (pdata->fps)
725 fps = pdata->fps;
726 if (pdata->txbuflen)
727 txbuflen = pdata->txbuflen;
728 if (pdata->display.init_sequence)
729 init_sequence = pdata->display.init_sequence;
730 if (pdata->gamma)
731 gamma = pdata->gamma;
732 if (pdata->display.debug)
733 display->debug = pdata->display.debug;
734 if (pdata->display.backlight)
735 display->backlight = pdata->display.backlight;
736 if (pdata->display.width)
737 display->width = pdata->display.width;
738 if (pdata->display.height)
739 display->height = pdata->display.height;
740 if (pdata->display.buswidth)
741 display->buswidth = pdata->display.buswidth;
742 if (pdata->display.regwidth)
743 display->regwidth = pdata->display.regwidth;
744
745 display->debug |= debug;
746 fbtft_expand_debug_value(&display->debug);
747
748 switch (pdata->rotate) {
749 case 90:
750 case 270:
751 width = display->height;
752 height = display->width;
753 break;
754 default:
755 width = display->width;
756 height = display->height;
757 }
758
759 vmem_size = display->width * display->height * bpp / 8;
760 vmem = vzalloc(vmem_size);
761 if (!vmem)
762 goto alloc_fail;
763
764 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
765 if (!fbops)
766 goto alloc_fail;
767
768 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
769 if (!fbdefio)
770 goto alloc_fail;
771
772 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
773 if (!buf)
774 goto alloc_fail;
775
776 if (display->gamma_num && display->gamma_len) {
777 gamma_curves = devm_kzalloc(dev, display->gamma_num * display->gamma_len * sizeof(gamma_curves[0]),
778 GFP_KERNEL);
779 if (!gamma_curves)
780 goto alloc_fail;
781 }
782
783 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
784 if (!info)
785 goto alloc_fail;
786
787 info->screen_base = (u8 __force __iomem *)vmem;
788 info->fbops = fbops;
789 info->fbdefio = fbdefio;
790
791 fbops->owner = dev->driver->owner;
792 fbops->fb_read = fb_sys_read;
793 fbops->fb_write = fbtft_fb_write;
794 fbops->fb_fillrect = fbtft_fb_fillrect;
795 fbops->fb_copyarea = fbtft_fb_copyarea;
796 fbops->fb_imageblit = fbtft_fb_imageblit;
797 fbops->fb_setcolreg = fbtft_fb_setcolreg;
798 fbops->fb_blank = fbtft_fb_blank;
799
800 fbdefio->delay = HZ/fps;
801 fbdefio->deferred_io = fbtft_deferred_io;
802 fb_deferred_io_init(info);
803
804 strncpy(info->fix.id, dev->driver->name, 16);
805 info->fix.type = FB_TYPE_PACKED_PIXELS;
806 info->fix.visual = FB_VISUAL_TRUECOLOR;
807 info->fix.xpanstep = 0;
808 info->fix.ypanstep = 0;
809 info->fix.ywrapstep = 0;
810 info->fix.line_length = width*bpp/8;
811 info->fix.accel = FB_ACCEL_NONE;
812 info->fix.smem_len = vmem_size;
813
814 info->var.rotate = pdata->rotate;
815 info->var.xres = width;
816 info->var.yres = height;
817 info->var.xres_virtual = info->var.xres;
818 info->var.yres_virtual = info->var.yres;
819 info->var.bits_per_pixel = bpp;
820 info->var.nonstd = 1;
821
822 /* RGB565 */
823 info->var.red.offset = 11;
824 info->var.red.length = 5;
825 info->var.green.offset = 5;
826 info->var.green.length = 6;
827 info->var.blue.offset = 0;
828 info->var.blue.length = 5;
829 info->var.transp.offset = 0;
830 info->var.transp.length = 0;
831
832 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
833
834 par = info->par;
835 par->info = info;
836 par->pdata = dev->platform_data;
837 par->debug = display->debug;
838 par->buf = buf;
839 spin_lock_init(&par->dirty_lock);
840 par->bgr = pdata->bgr;
841 par->startbyte = pdata->startbyte;
842 par->init_sequence = init_sequence;
843 par->gamma.curves = gamma_curves;
844 par->gamma.num_curves = display->gamma_num;
845 par->gamma.num_values = display->gamma_len;
846 mutex_init(&par->gamma.lock);
847 info->pseudo_palette = par->pseudo_palette;
848
849 if (par->gamma.curves && gamma) {
850 if (fbtft_gamma_parse_str(par,
851 par->gamma.curves, gamma, strlen(gamma)))
852 goto alloc_fail;
853 }
854
855 /* Transmit buffer */
856 if (txbuflen == -1)
857 txbuflen = vmem_size + 2; /* add in case startbyte is used */
858
859#ifdef __LITTLE_ENDIAN
860 if ((!txbuflen) && (bpp > 8))
861 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
862#endif
863
864 if (txbuflen > 0) {
865 if (dma) {
866 dev->coherent_dma_mask = ~0;
867 txbuf = dmam_alloc_coherent(dev, txbuflen, &par->txbuf.dma, GFP_DMA);
868 } else {
869 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
870 }
871 if (!txbuf)
872 goto alloc_fail;
873 par->txbuf.buf = txbuf;
874 par->txbuf.len = txbuflen;
875 }
876
877 /* Initialize gpios to disabled */
878 par->gpio.reset = -1;
879 par->gpio.dc = -1;
880 par->gpio.rd = -1;
881 par->gpio.wr = -1;
882 par->gpio.cs = -1;
883 par->gpio.latch = -1;
884 for (i = 0; i < 16; i++) {
885 par->gpio.db[i] = -1;
886 par->gpio.led[i] = -1;
887 par->gpio.aux[i] = -1;
888 }
889
890 /* default fbtft operations */
891 par->fbtftops.write = fbtft_write_spi;
892 par->fbtftops.read = fbtft_read_spi;
893 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
894 par->fbtftops.write_register = fbtft_write_reg8_bus8;
895 par->fbtftops.set_addr_win = fbtft_set_addr_win;
896 par->fbtftops.reset = fbtft_reset;
897 par->fbtftops.mkdirty = fbtft_mkdirty;
898 par->fbtftops.update_display = fbtft_update_display;
899 par->fbtftops.request_gpios = fbtft_request_gpios;
900 if (display->backlight)
901 par->fbtftops.register_backlight = fbtft_register_backlight;
902
903 /* use driver provided functions */
904 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
905
906 return info;
907
908alloc_fail:
909 vfree(vmem);
910
911 return NULL;
912}
913EXPORT_SYMBOL(fbtft_framebuffer_alloc);
914
915/**
916 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
917 *
918 * @info: frame buffer info structure
919 *
920 */
921void fbtft_framebuffer_release(struct fb_info *info)
922{
923 fb_deferred_io_cleanup(info);
924 vfree(info->screen_base);
925 framebuffer_release(info);
926}
927EXPORT_SYMBOL(fbtft_framebuffer_release);
928
929/**
930 * fbtft_register_framebuffer - registers a tft frame buffer device
931 * @fb_info: frame buffer info structure
932 *
933 * Sets SPI driverdata if needed
934 * Requests needed gpios.
935 * Initializes display
936 * Updates display.
937 * Registers a frame buffer device @fb_info.
938 *
939 * Returns negative errno on error, or zero for success.
940 *
941 */
942int fbtft_register_framebuffer(struct fb_info *fb_info)
943{
944 int ret;
945 char text1[50] = "";
946 char text2[50] = "";
947 struct fbtft_par *par = fb_info->par;
948 struct spi_device *spi = par->spi;
949
950 /* sanity checks */
951 if (!par->fbtftops.init_display) {
952 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
953 return -EINVAL;
954 }
955
956 if (spi)
957 spi_set_drvdata(spi, fb_info);
958 if (par->pdev)
959 platform_set_drvdata(par->pdev, fb_info);
960
961 ret = par->fbtftops.request_gpios(par);
962 if (ret < 0)
963 goto reg_fail;
964
965 if (par->fbtftops.verify_gpios) {
966 ret = par->fbtftops.verify_gpios(par);
967 if (ret < 0)
968 goto reg_fail;
969 }
970
971 ret = par->fbtftops.init_display(par);
972 if (ret < 0)
973 goto reg_fail;
974 if (par->fbtftops.set_var) {
975 ret = par->fbtftops.set_var(par);
976 if (ret < 0)
977 goto reg_fail;
978 }
979
980 /* update the entire display */
981 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
982
983 if (par->fbtftops.set_gamma && par->gamma.curves) {
984 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
985 if (ret)
986 goto reg_fail;
987 }
988
989 if (par->fbtftops.register_backlight)
990 par->fbtftops.register_backlight(par);
991
992 ret = register_framebuffer(fb_info);
993 if (ret < 0)
994 goto reg_fail;
995
996 fbtft_sysfs_init(par);
997
998 if (par->txbuf.buf)
Fabio Estevam3fed5ba2015-02-19 10:12:11 -0200999 sprintf(text1, ", %zu KiB %sbuffer memory",
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001000 par->txbuf.len >> 10, par->txbuf.dma ? "DMA " : "");
1001 if (spi)
1002 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
1003 spi->chip_select, spi->max_speed_hz/1000000);
1004 dev_info(fb_info->dev,
1005 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
1006 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
1007 fb_info->fix.smem_len >> 10, text1,
1008 HZ/fb_info->fbdefio->delay, text2);
1009
1010#ifdef CONFIG_FB_BACKLIGHT
1011 /* Turn on backlight if available */
1012 if (fb_info->bl_dev) {
1013 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
1014 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
1015 }
1016#endif
1017
1018 return 0;
1019
1020reg_fail:
1021 if (par->fbtftops.unregister_backlight)
1022 par->fbtftops.unregister_backlight(par);
1023 if (spi)
1024 spi_set_drvdata(spi, NULL);
1025 if (par->pdev)
1026 platform_set_drvdata(par->pdev, NULL);
1027
1028 return ret;
1029}
1030EXPORT_SYMBOL(fbtft_register_framebuffer);
1031
1032/**
1033 * fbtft_unregister_framebuffer - releases a tft frame buffer device
1034 * @fb_info: frame buffer info structure
1035 *
1036 * Frees SPI driverdata if needed
1037 * Frees gpios.
1038 * Unregisters frame buffer device.
1039 *
1040 */
1041int fbtft_unregister_framebuffer(struct fb_info *fb_info)
1042{
1043 struct fbtft_par *par = fb_info->par;
1044 struct spi_device *spi = par->spi;
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001045
1046 if (spi)
1047 spi_set_drvdata(spi, NULL);
1048 if (par->pdev)
1049 platform_set_drvdata(par->pdev, NULL);
1050 if (par->fbtftops.unregister_backlight)
1051 par->fbtftops.unregister_backlight(par);
1052 fbtft_sysfs_exit(par);
Aya Mahfouz11107ff2015-02-27 15:10:30 +02001053 return unregister_framebuffer(fb_info);
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001054}
1055EXPORT_SYMBOL(fbtft_unregister_framebuffer);
1056
1057#ifdef CONFIG_OF
1058/**
1059 * fbtft_init_display_dt() - Device Tree init_display() function
1060 * @par: Driver data
1061 *
1062 * Return: 0 if successful, negative if error
1063 */
1064static int fbtft_init_display_dt(struct fbtft_par *par)
1065{
1066 struct device_node *node = par->info->device->of_node;
1067 struct property *prop;
1068 const __be32 *p;
1069 u32 val;
1070 int buf[64], i, j;
1071 char msg[128];
1072 char str[16];
1073
1074 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1075
1076 if (!node)
1077 return -EINVAL;
1078
1079 prop = of_find_property(node, "init", NULL);
1080 p = of_prop_next_u32(prop, NULL, &val);
1081 if (!p)
1082 return -EINVAL;
1083 while (p) {
1084 if (val & FBTFT_OF_INIT_CMD) {
1085 val &= 0xFFFF;
1086 i = 0;
1087 while (p && !(val & 0xFFFF0000)) {
1088 if (i > 63) {
1089 dev_err(par->info->device,
1090 "%s: Maximum register values exceeded\n",
1091 __func__);
1092 return -EINVAL;
1093 }
1094 buf[i++] = val;
1095 p = of_prop_next_u32(prop, p, &val);
1096 }
1097 /* make debug message */
1098 msg[0] = '\0';
1099 for (j = 0; j < i; j++) {
1100 snprintf(str, 128, " %02X", buf[j]);
1101 strcat(msg, str);
1102 }
1103 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1104 "init: write_register:%s\n", msg);
1105
1106 par->fbtftops.write_register(par, i,
1107 buf[0], buf[1], buf[2], buf[3],
1108 buf[4], buf[5], buf[6], buf[7],
1109 buf[8], buf[9], buf[10], buf[11],
1110 buf[12], buf[13], buf[14], buf[15],
1111 buf[16], buf[17], buf[18], buf[19],
1112 buf[20], buf[21], buf[22], buf[23],
1113 buf[24], buf[25], buf[26], buf[27],
1114 buf[28], buf[29], buf[30], buf[31],
1115 buf[32], buf[33], buf[34], buf[35],
1116 buf[36], buf[37], buf[38], buf[39],
1117 buf[40], buf[41], buf[42], buf[43],
1118 buf[44], buf[45], buf[46], buf[47],
1119 buf[48], buf[49], buf[50], buf[51],
1120 buf[52], buf[53], buf[54], buf[55],
1121 buf[56], buf[57], buf[58], buf[59],
1122 buf[60], buf[61], buf[62], buf[63]);
1123 } else if (val & FBTFT_OF_INIT_DELAY) {
1124 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1125 "init: msleep(%u)\n", val & 0xFFFF);
1126 msleep(val & 0xFFFF);
1127 p = of_prop_next_u32(prop, p, &val);
1128 } else {
1129 dev_err(par->info->device, "illegal init value 0x%X\n",
1130 val);
1131 return -EINVAL;
1132 }
1133 }
1134
1135 return 0;
1136}
1137#endif
1138
1139/**
1140 * fbtft_init_display() - Generic init_display() function
1141 * @par: Driver data
1142 *
1143 * Uses par->init_sequence to do the initialization
1144 *
1145 * Return: 0 if successful, negative if error
1146 */
1147int fbtft_init_display(struct fbtft_par *par)
1148{
1149 int buf[64];
1150 char msg[128];
1151 char str[16];
1152 int i = 0;
1153 int j;
1154
1155 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1156
1157 /* sanity check */
1158 if (!par->init_sequence) {
1159 dev_err(par->info->device,
1160 "error: init_sequence is not set\n");
1161 return -EINVAL;
1162 }
1163
1164 /* make sure stop marker exists */
1165 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1166 if (par->init_sequence[i] == -3)
1167 break;
1168 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1169 dev_err(par->info->device,
1170 "missing stop marker at end of init sequence\n");
1171 return -EINVAL;
1172 }
1173
1174 par->fbtftops.reset(par);
1175 if (par->gpio.cs != -1)
1176 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1177
1178 i = 0;
1179 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1180 if (par->init_sequence[i] == -3) {
1181 /* done */
1182 return 0;
1183 }
1184 if (par->init_sequence[i] >= 0) {
1185 dev_err(par->info->device,
1186 "missing delimiter at position %d\n", i);
1187 return -EINVAL;
1188 }
1189 if (par->init_sequence[i+1] < 0) {
1190 dev_err(par->info->device,
1191 "missing value after delimiter %d at position %d\n",
1192 par->init_sequence[i], i);
1193 return -EINVAL;
1194 }
1195 switch (par->init_sequence[i]) {
1196 case -1:
1197 i++;
1198 /* make debug message */
1199 strcpy(msg, "");
1200 j = i + 1;
1201 while (par->init_sequence[j] >= 0) {
1202 sprintf(str, "0x%02X ", par->init_sequence[j]);
1203 strcat(msg, str);
1204 j++;
1205 }
1206 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1207 "init: write(0x%02X) %s\n",
1208 par->init_sequence[i], msg);
1209
1210 /* Write */
1211 j = 0;
1212 while (par->init_sequence[i] >= 0) {
1213 if (j > 63) {
1214 dev_err(par->info->device,
1215 "%s: Maximum register values exceeded\n",
1216 __func__);
1217 return -EINVAL;
1218 }
1219 buf[j++] = par->init_sequence[i++];
1220 }
1221 par->fbtftops.write_register(par, j,
1222 buf[0], buf[1], buf[2], buf[3],
1223 buf[4], buf[5], buf[6], buf[7],
1224 buf[8], buf[9], buf[10], buf[11],
1225 buf[12], buf[13], buf[14], buf[15],
1226 buf[16], buf[17], buf[18], buf[19],
1227 buf[20], buf[21], buf[22], buf[23],
1228 buf[24], buf[25], buf[26], buf[27],
1229 buf[28], buf[29], buf[30], buf[31],
1230 buf[32], buf[33], buf[34], buf[35],
1231 buf[36], buf[37], buf[38], buf[39],
1232 buf[40], buf[41], buf[42], buf[43],
1233 buf[44], buf[45], buf[46], buf[47],
1234 buf[48], buf[49], buf[50], buf[51],
1235 buf[52], buf[53], buf[54], buf[55],
1236 buf[56], buf[57], buf[58], buf[59],
1237 buf[60], buf[61], buf[62], buf[63]);
1238 break;
1239 case -2:
1240 i++;
1241 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1242 "init: mdelay(%d)\n", par->init_sequence[i]);
1243 mdelay(par->init_sequence[i++]);
1244 break;
1245 default:
1246 dev_err(par->info->device,
1247 "unknown delimiter %d at position %d\n",
1248 par->init_sequence[i], i);
1249 return -EINVAL;
1250 }
1251 }
1252
1253 dev_err(par->info->device,
1254 "%s: something is wrong. Shouldn't get here.\n", __func__);
1255 return -EINVAL;
1256}
1257EXPORT_SYMBOL(fbtft_init_display);
1258
1259/**
1260 * fbtft_verify_gpios() - Generic verify_gpios() function
1261 * @par: Driver data
1262 *
1263 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1264 *
1265 * Return: 0 if successful, negative if error
1266 */
Krzysztof Adamskied208432015-01-22 19:08:58 +01001267static int fbtft_verify_gpios(struct fbtft_par *par)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001268{
1269 struct fbtft_platform_data *pdata;
1270 int i;
1271
1272 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1273
1274 pdata = par->info->device->platform_data;
Yeliz Taneroglu6ba67a52015-03-02 17:49:59 +02001275 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001276 par->gpio.dc < 0) {
1277 dev_err(par->info->device,
1278 "Missing info about 'dc' gpio. Aborting.\n");
1279 return -EINVAL;
1280 }
1281
1282 if (!par->pdev)
1283 return 0;
1284
1285 if (par->gpio.wr < 0) {
1286 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1287 return -EINVAL;
1288 }
1289 for (i = 0; i < pdata->display.buswidth; i++) {
1290 if (par->gpio.db[i] < 0) {
1291 dev_err(par->info->device,
1292 "Missing 'db%02d' gpio. Aborting.\n", i);
1293 return -EINVAL;
1294 }
1295 }
1296
1297 return 0;
1298}
1299
1300#ifdef CONFIG_OF
1301/* returns 0 if the property is not present */
1302static u32 fbtft_of_value(struct device_node *node, const char *propname)
1303{
1304 int ret;
1305 u32 val = 0;
1306
1307 ret = of_property_read_u32(node, propname, &val);
1308 if (ret == 0)
1309 pr_info("%s: %s = %u\n", __func__, propname, val);
1310
1311 return val;
1312}
1313
1314static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1315{
1316 struct device_node *node = dev->of_node;
1317 struct fbtft_platform_data *pdata;
1318
1319 if (!node) {
1320 dev_err(dev, "Missing platform data or DT\n");
1321 return ERR_PTR(-EINVAL);
1322 }
1323
1324 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1325 if (!pdata)
1326 return ERR_PTR(-ENOMEM);
1327
1328 pdata->display.width = fbtft_of_value(node, "width");
1329 pdata->display.height = fbtft_of_value(node, "height");
1330 pdata->display.regwidth = fbtft_of_value(node, "regwidth");
1331 pdata->display.buswidth = fbtft_of_value(node, "buswidth");
1332 pdata->display.backlight = fbtft_of_value(node, "backlight");
1333 pdata->display.bpp = fbtft_of_value(node, "bpp");
1334 pdata->display.debug = fbtft_of_value(node, "debug");
1335 pdata->rotate = fbtft_of_value(node, "rotate");
1336 pdata->bgr = of_property_read_bool(node, "bgr");
1337 pdata->fps = fbtft_of_value(node, "fps");
1338 pdata->txbuflen = fbtft_of_value(node, "txbuflen");
1339 pdata->startbyte = fbtft_of_value(node, "startbyte");
1340 of_property_read_string(node, "gamma", (const char **)&pdata->gamma);
1341
1342 if (of_find_property(node, "led-gpios", NULL))
1343 pdata->display.backlight = 1;
1344 if (of_find_property(node, "init", NULL))
1345 pdata->display.fbtftops.init_display = fbtft_init_display_dt;
1346 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt;
1347
1348 return pdata;
1349}
1350#else
1351static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1352{
1353 dev_err(dev, "Missing platform data\n");
1354 return ERR_PTR(-EINVAL);
1355}
1356#endif
1357
1358/**
1359 * fbtft_probe_common() - Generic device probe() helper function
1360 * @display: Display properties
1361 * @sdev: SPI device
1362 * @pdev: Platform device
1363 *
1364 * Allocates, initializes and registers a framebuffer
1365 *
1366 * Either @sdev or @pdev should be NULL
1367 *
1368 * Return: 0 if successful, negative if error
1369 */
1370int fbtft_probe_common(struct fbtft_display *display,
1371 struct spi_device *sdev, struct platform_device *pdev)
1372{
1373 struct device *dev;
1374 struct fb_info *info;
1375 struct fbtft_par *par;
1376 struct fbtft_platform_data *pdata;
1377 int ret;
1378
1379 if (sdev)
1380 dev = &sdev->dev;
1381 else
1382 dev = &pdev->dev;
1383
1384 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1385 dev_info(dev, "%s()\n", __func__);
1386
1387 pdata = dev->platform_data;
1388 if (!pdata) {
1389 pdata = fbtft_probe_dt(dev);
1390 if (IS_ERR(pdata))
1391 return PTR_ERR(pdata);
1392 dev->platform_data = pdata;
1393 }
1394
1395 info = fbtft_framebuffer_alloc(display, dev);
1396 if (!info)
1397 return -ENOMEM;
1398
1399 par = info->par;
1400 par->spi = sdev;
1401 par->pdev = pdev;
1402
1403 if (display->buswidth == 0) {
1404 dev_err(dev, "buswidth is not set\n");
1405 return -EINVAL;
1406 }
1407
1408 /* write register functions */
1409 if (display->regwidth == 8 && display->buswidth == 8) {
1410 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1411 } else
1412 if (display->regwidth == 8 && display->buswidth == 9 && par->spi) {
1413 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1414 } else if (display->regwidth == 16 && display->buswidth == 8) {
1415 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1416 } else if (display->regwidth == 16 && display->buswidth == 16) {
1417 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1418 } else {
1419 dev_warn(dev,
1420 "no default functions for regwidth=%d and buswidth=%d\n",
1421 display->regwidth, display->buswidth);
1422 }
1423
1424 /* write_vmem() functions */
1425 if (display->buswidth == 8)
1426 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1427 else if (display->buswidth == 9)
1428 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1429 else if (display->buswidth == 16)
1430 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1431
1432 /* GPIO write() functions */
1433 if (par->pdev) {
1434 if (display->buswidth == 8)
1435 par->fbtftops.write = fbtft_write_gpio8_wr;
1436 else if (display->buswidth == 16)
1437 par->fbtftops.write = fbtft_write_gpio16_wr;
1438 }
1439
1440 /* 9-bit SPI setup */
1441 if (par->spi && display->buswidth == 9) {
1442 par->spi->bits_per_word = 9;
1443 ret = par->spi->master->setup(par->spi);
1444 if (ret) {
1445 dev_warn(&par->spi->dev,
1446 "9-bit SPI not available, emulating using 8-bit.\n");
1447 par->spi->bits_per_word = 8;
1448 ret = par->spi->master->setup(par->spi);
1449 if (ret)
1450 goto out_release;
1451 /* allocate buffer with room for dc bits */
1452 par->extra = devm_kzalloc(par->info->device,
1453 par->txbuf.len + (par->txbuf.len / 8) + 8,
1454 GFP_KERNEL);
1455 if (!par->extra) {
1456 ret = -ENOMEM;
1457 goto out_release;
1458 }
1459 par->fbtftops.write = fbtft_write_spi_emulate_9;
1460 }
1461 }
1462
1463 if (!par->fbtftops.verify_gpios)
1464 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1465
1466 /* make sure we still use the driver provided functions */
1467 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1468
1469 /* use init_sequence if provided */
1470 if (par->init_sequence)
1471 par->fbtftops.init_display = fbtft_init_display;
1472
1473 /* use platform_data provided functions above all */
1474 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1475
1476 ret = fbtft_register_framebuffer(info);
1477 if (ret < 0)
1478 goto out_release;
1479
1480 return 0;
1481
1482out_release:
1483 fbtft_framebuffer_release(info);
1484
1485 return ret;
1486}
1487EXPORT_SYMBOL(fbtft_probe_common);
1488
1489/**
1490 * fbtft_remove_common() - Generic device remove() helper function
1491 * @dev: Device
1492 * @info: Framebuffer
1493 *
1494 * Unregisters and releases the framebuffer
1495 *
1496 * Return: 0 if successful, negative if error
1497 */
1498int fbtft_remove_common(struct device *dev, struct fb_info *info)
1499{
1500 struct fbtft_par *par;
1501
1502 if (!info)
1503 return -EINVAL;
1504 par = info->par;
1505 if (par)
1506 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1507 "%s()\n", __func__);
1508 fbtft_unregister_framebuffer(info);
1509 fbtft_framebuffer_release(info);
1510
1511 return 0;
1512}
1513EXPORT_SYMBOL(fbtft_remove_common);
1514
1515MODULE_LICENSE("GPL");