blob: 873e2c72a4d72d82cb456b653184c9149f931a0c [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;
52module_param(debug, ulong , 0);
53MODULE_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
82unsigned long fbtft_request_gpios_match(struct fbtft_par *par,
83 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
126int fbtft_request_gpios(struct fbtft_par *par)
127{
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
252int fbtft_backlight_update_status(struct backlight_device *bd)
253{
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
269int fbtft_backlight_get_brightness(struct backlight_device *bd)
270{
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);
305 if (!bl_ops) {
306 dev_err(par->info->device,
307 "%s: could not allocate memeory for backlight operations.\n",
308 __func__);
309 return;
310 }
311
312 bl_ops->get_brightness = fbtft_backlight_get_brightness;
313 bl_ops->update_status = fbtft_backlight_update_status;
314 bl_props.type = BACKLIGHT_RAW;
315 /* Assume backlight is off, get polarity from current state of pin */
316 bl_props.power = FB_BLANK_POWERDOWN;
317 if (!gpio_get_value(par->gpio.led[0]))
318 bl_props.state |= BL_CORE_DRIVER1;
319
320 bd = backlight_device_register(dev_driver_string(par->info->device),
321 par->info->device, par, bl_ops, &bl_props);
322 if (IS_ERR(bd)) {
323 dev_err(par->info->device,
324 "cannot register backlight device (%ld)\n",
325 PTR_ERR(bd));
326 return;
327 }
328 par->info->bl_dev = bd;
329
330 if (!par->fbtftops.unregister_backlight)
331 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
332}
333#else
334void fbtft_register_backlight(struct fbtft_par *par) { };
335void fbtft_unregister_backlight(struct fbtft_par *par) { };
336#endif
337EXPORT_SYMBOL(fbtft_register_backlight);
338EXPORT_SYMBOL(fbtft_unregister_backlight);
339
340void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
341{
342 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
343 "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
344
345 /* Column address set */
346 write_reg(par, 0x2A,
347 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
348
349 /* Row adress set */
350 write_reg(par, 0x2B,
351 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
352
353 /* Memory write */
354 write_reg(par, 0x2C);
355}
356
357
358void fbtft_reset(struct fbtft_par *par)
359{
360 if (par->gpio.reset == -1)
361 return;
362 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
363 gpio_set_value(par->gpio.reset, 0);
364 udelay(20);
365 gpio_set_value(par->gpio.reset, 1);
366 mdelay(120);
367}
368
369
370void fbtft_update_display(struct fbtft_par *par, unsigned start_line, unsigned end_line)
371{
372 size_t offset, len;
373 struct timespec ts_start, ts_end, ts_fps, ts_duration;
374 long fps_ms, fps_us, duration_ms, duration_us;
375 long fps, throughput;
376 bool timeit = false;
377 int ret = 0;
378
379 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE | DEBUG_TIME_EACH_UPDATE))) {
380 if ((par->debug & DEBUG_TIME_EACH_UPDATE) || \
381 ((par->debug & DEBUG_TIME_FIRST_UPDATE) && !par->first_update_done)) {
382 getnstimeofday(&ts_start);
383 timeit = true;
384 }
385 }
386
387 /* Sanity checks */
388 if (start_line > end_line) {
389 dev_warn(par->info->device,
390 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
391 __func__, start_line, end_line);
392 start_line = 0;
393 end_line = par->info->var.yres - 1;
394 }
395 if (start_line > par->info->var.yres - 1 || end_line > par->info->var.yres - 1) {
396 dev_warn(par->info->device,
397 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
398 __func__, start_line, end_line, par->info->var.yres - 1);
399 start_line = 0;
400 end_line = par->info->var.yres - 1;
401 }
402
403 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
404 __func__, start_line, end_line);
405
406 if (par->fbtftops.set_addr_win)
407 par->fbtftops.set_addr_win(par, 0, start_line,
408 par->info->var.xres-1, end_line);
409
410 offset = start_line * par->info->fix.line_length;
411 len = (end_line - start_line + 1) * par->info->fix.line_length;
412 ret = par->fbtftops.write_vmem(par, offset, len);
413 if (ret < 0)
414 dev_err(par->info->device,
415 "%s: write_vmem failed to update display buffer\n",
416 __func__);
417
418 if (unlikely(timeit)) {
419 getnstimeofday(&ts_end);
420 if (par->update_time.tv_nsec == 0 && par->update_time.tv_sec == 0) {
421 par->update_time.tv_sec = ts_start.tv_sec;
422 par->update_time.tv_nsec = ts_start.tv_nsec;
423 }
424 ts_fps = timespec_sub(ts_start, par->update_time);
425 par->update_time.tv_sec = ts_start.tv_sec;
426 par->update_time.tv_nsec = ts_start.tv_nsec;
427 fps_ms = (ts_fps.tv_sec * 1000) + ((ts_fps.tv_nsec / 1000000) % 1000);
428 fps_us = (ts_fps.tv_nsec / 1000) % 1000;
429 fps = fps_ms * 1000 + fps_us;
430 fps = fps ? 1000000 / fps : 0;
431
432 ts_duration = timespec_sub(ts_end, ts_start);
433 duration_ms = (ts_duration.tv_sec * 1000) + ((ts_duration.tv_nsec / 1000000) % 1000);
434 duration_us = (ts_duration.tv_nsec / 1000) % 1000;
435 throughput = duration_ms * 1000 + duration_us;
436 throughput = throughput ? (len * 1000) / throughput : 0;
437 throughput = throughput * 1000 / 1024;
438
439 dev_info(par->info->device,
440 "Display update: %ld kB/s (%ld.%.3ld ms), fps=%ld (%ld.%.3ld ms)\n",
441 throughput, duration_ms, duration_us,
442 fps, fps_ms, fps_us);
443 par->first_update_done = true;
444 }
445}
446
447
448void fbtft_mkdirty(struct fb_info *info, int y, int height)
449{
450 struct fbtft_par *par = info->par;
451 struct fb_deferred_io *fbdefio = info->fbdefio;
452
453 /* special case, needed ? */
454 if (y == -1) {
455 y = 0;
456 height = info->var.yres - 1;
457 }
458
459 /* Mark display lines/area as dirty */
460 spin_lock(&par->dirty_lock);
461 if (y < par->dirty_lines_start)
462 par->dirty_lines_start = y;
463 if (y + height - 1 > par->dirty_lines_end)
464 par->dirty_lines_end = y + height - 1;
465 spin_unlock(&par->dirty_lock);
466
467 /* Schedule deferred_io to update display (no-op if already on queue)*/
468 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
469}
470
471void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
472{
473 struct fbtft_par *par = info->par;
474 unsigned dirty_lines_start, dirty_lines_end;
475 struct page *page;
476 unsigned long index;
477 unsigned y_low = 0, y_high = 0;
478 int count = 0;
479
480 spin_lock(&par->dirty_lock);
481 dirty_lines_start = par->dirty_lines_start;
482 dirty_lines_end = par->dirty_lines_end;
483 /* set display line markers as clean */
484 par->dirty_lines_start = par->info->var.yres - 1;
485 par->dirty_lines_end = 0;
486 spin_unlock(&par->dirty_lock);
487
488 /* Mark display lines as dirty */
489 list_for_each_entry(page, pagelist, lru) {
490 count++;
491 index = page->index << PAGE_SHIFT;
492 y_low = index / info->fix.line_length;
493 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
494 fbtft_dev_dbg(DEBUG_DEFERRED_IO, par, info->device,
495 "page->index=%lu y_low=%d y_high=%d\n",
496 page->index, y_low, y_high);
497 if (y_high > info->var.yres - 1)
498 y_high = info->var.yres - 1;
499 if (y_low < dirty_lines_start)
500 dirty_lines_start = y_low;
501 if (y_high > dirty_lines_end)
502 dirty_lines_end = y_high;
503 }
504
505 par->fbtftops.update_display(info->par,
506 dirty_lines_start, dirty_lines_end);
507}
508
509
510void fbtft_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
511{
512 struct fbtft_par *par = info->par;
513
514 fbtft_dev_dbg(DEBUG_FB_FILLRECT, par, info->dev,
515 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
516 __func__, rect->dx, rect->dy, rect->width, rect->height);
517 sys_fillrect(info, rect);
518
519 par->fbtftops.mkdirty(info, rect->dy, rect->height);
520}
521
522void fbtft_fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
523{
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
534void fbtft_fb_imageblit(struct fb_info *info, const struct fb_image *image)
535{
536 struct fbtft_par *par = info->par;
537
538 fbtft_dev_dbg(DEBUG_FB_IMAGEBLIT, par, info->dev,
539 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
540 __func__, image->dx, image->dy, image->width, image->height);
541 sys_imageblit(info, image);
542
543 par->fbtftops.mkdirty(info, image->dy, image->height);
544}
545
546ssize_t fbtft_fb_write(struct fb_info *info,
547 const char __user *buf, size_t count, loff_t *ppos)
548{
549 struct fbtft_par *par = info->par;
550 ssize_t res;
551
552 fbtft_dev_dbg(DEBUG_FB_WRITE, par, info->dev,
553 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
554 res = fb_sys_write(info, buf, count, ppos);
555
556 /* TODO: only mark changed area
557 update all for now */
558 par->fbtftops.mkdirty(info, -1, 0);
559
560 return res;
561}
562
563/* from pxafb.c */
564unsigned int chan_to_field(unsigned chan, struct fb_bitfield *bf)
565{
566 chan &= 0xffff;
567 chan >>= 16 - bf->length;
568 return chan << bf->offset;
569}
570
571int fbtft_fb_setcolreg(unsigned regno,
572 unsigned red, unsigned green, unsigned blue,
573 unsigned transp, struct fb_info *info)
574{
575 struct fbtft_par *par = info->par;
576 unsigned val;
577 int ret = 1;
578
579 fbtft_dev_dbg(DEBUG_FB_SETCOLREG, par, info->dev,
580 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
581 __func__, regno, red, green, blue, transp);
582
583 switch (info->fix.visual) {
584 case FB_VISUAL_TRUECOLOR:
585 if (regno < 16) {
586 u32 *pal = info->pseudo_palette;
587
588 val = chan_to_field(red, &info->var.red);
589 val |= chan_to_field(green, &info->var.green);
590 val |= chan_to_field(blue, &info->var.blue);
591
592 pal[regno] = val;
593 ret = 0;
594 }
595 break;
596
597 }
598 return ret;
599}
600
601int fbtft_fb_blank(int blank, struct fb_info *info)
602{
603 struct fbtft_par *par = info->par;
604 int ret = -EINVAL;
605
606 fbtft_dev_dbg(DEBUG_FB_BLANK, par, info->dev, "%s(blank=%d)\n",
607 __func__, blank);
608
609 if (!par->fbtftops.blank)
610 return ret;
611
612 switch (blank) {
613 case FB_BLANK_POWERDOWN:
614 case FB_BLANK_VSYNC_SUSPEND:
615 case FB_BLANK_HSYNC_SUSPEND:
616 case FB_BLANK_NORMAL:
617 ret = par->fbtftops.blank(par, true);
618 break;
619 case FB_BLANK_UNBLANK:
620 ret = par->fbtftops.blank(par, false);
621 break;
622 }
623 return ret;
624}
625
626void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
627{
628 if (src->write)
629 dst->write = src->write;
630 if (src->read)
631 dst->read = src->read;
632 if (src->write_vmem)
633 dst->write_vmem = src->write_vmem;
634 if (src->write_register)
635 dst->write_register = src->write_register;
636 if (src->set_addr_win)
637 dst->set_addr_win = src->set_addr_win;
638 if (src->reset)
639 dst->reset = src->reset;
640 if (src->mkdirty)
641 dst->mkdirty = src->mkdirty;
642 if (src->update_display)
643 dst->update_display = src->update_display;
644 if (src->init_display)
645 dst->init_display = src->init_display;
646 if (src->blank)
647 dst->blank = src->blank;
648 if (src->request_gpios_match)
649 dst->request_gpios_match = src->request_gpios_match;
650 if (src->request_gpios)
651 dst->request_gpios = src->request_gpios;
652 if (src->verify_gpios)
653 dst->verify_gpios = src->verify_gpios;
654 if (src->register_backlight)
655 dst->register_backlight = src->register_backlight;
656 if (src->unregister_backlight)
657 dst->unregister_backlight = src->unregister_backlight;
658 if (src->set_var)
659 dst->set_var = src->set_var;
660 if (src->set_gamma)
661 dst->set_gamma = src->set_gamma;
662}
663
664/**
665 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
666 *
667 * @display: pointer to structure describing the display
668 * @dev: pointer to the device for this fb, this can be NULL
669 *
670 * Creates a new frame buffer info structure.
671 *
672 * Also creates and populates the following structures:
673 * info->fbops
674 * info->fbdefio
675 * info->pseudo_palette
676 * par->fbtftops
677 * par->txbuf
678 *
679 * Returns the new structure, or NULL if an error occurred.
680 *
681 */
682struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
683 struct device *dev)
684{
685 struct fb_info *info;
686 struct fbtft_par *par;
687 struct fb_ops *fbops = NULL;
688 struct fb_deferred_io *fbdefio = NULL;
689 struct fbtft_platform_data *pdata = dev->platform_data;
690 u8 *vmem = NULL;
691 void *txbuf = NULL;
692 void *buf = NULL;
693 unsigned width;
694 unsigned height;
695 int txbuflen = display->txbuflen;
696 unsigned bpp = display->bpp;
697 unsigned fps = display->fps;
698 int vmem_size, i;
699 int *init_sequence = display->init_sequence;
700 char *gamma = display->gamma;
701 unsigned long *gamma_curves = NULL;
702
703 /* sanity check */
704 if (display->gamma_num * display->gamma_len > FBTFT_GAMMA_MAX_VALUES_TOTAL) {
705 dev_err(dev,
706 "%s: FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
707 __func__, FBTFT_GAMMA_MAX_VALUES_TOTAL);
708 return NULL;
709 }
710
711 /* defaults */
712 if (!fps)
713 fps = 20;
714 if (!bpp)
715 bpp = 16;
716
717 if (!pdata) {
718 dev_err(dev, "platform data is missing\n");
719 return NULL;
720 }
721
722 /* override driver values? */
723 if (pdata->fps)
724 fps = pdata->fps;
725 if (pdata->txbuflen)
726 txbuflen = pdata->txbuflen;
727 if (pdata->display.init_sequence)
728 init_sequence = pdata->display.init_sequence;
729 if (pdata->gamma)
730 gamma = pdata->gamma;
731 if (pdata->display.debug)
732 display->debug = pdata->display.debug;
733 if (pdata->display.backlight)
734 display->backlight = pdata->display.backlight;
735 if (pdata->display.width)
736 display->width = pdata->display.width;
737 if (pdata->display.height)
738 display->height = pdata->display.height;
739 if (pdata->display.buswidth)
740 display->buswidth = pdata->display.buswidth;
741 if (pdata->display.regwidth)
742 display->regwidth = pdata->display.regwidth;
743
744 display->debug |= debug;
745 fbtft_expand_debug_value(&display->debug);
746
747 switch (pdata->rotate) {
748 case 90:
749 case 270:
750 width = display->height;
751 height = display->width;
752 break;
753 default:
754 width = display->width;
755 height = display->height;
756 }
757
758 vmem_size = display->width * display->height * bpp / 8;
759 vmem = vzalloc(vmem_size);
760 if (!vmem)
761 goto alloc_fail;
762
763 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
764 if (!fbops)
765 goto alloc_fail;
766
767 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
768 if (!fbdefio)
769 goto alloc_fail;
770
771 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
772 if (!buf)
773 goto alloc_fail;
774
775 if (display->gamma_num && display->gamma_len) {
776 gamma_curves = devm_kzalloc(dev, display->gamma_num * display->gamma_len * sizeof(gamma_curves[0]),
777 GFP_KERNEL);
778 if (!gamma_curves)
779 goto alloc_fail;
780 }
781
782 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
783 if (!info)
784 goto alloc_fail;
785
786 info->screen_base = (u8 __force __iomem *)vmem;
787 info->fbops = fbops;
788 info->fbdefio = fbdefio;
789
790 fbops->owner = dev->driver->owner;
791 fbops->fb_read = fb_sys_read;
792 fbops->fb_write = fbtft_fb_write;
793 fbops->fb_fillrect = fbtft_fb_fillrect;
794 fbops->fb_copyarea = fbtft_fb_copyarea;
795 fbops->fb_imageblit = fbtft_fb_imageblit;
796 fbops->fb_setcolreg = fbtft_fb_setcolreg;
797 fbops->fb_blank = fbtft_fb_blank;
798
799 fbdefio->delay = HZ/fps;
800 fbdefio->deferred_io = fbtft_deferred_io;
801 fb_deferred_io_init(info);
802
803 strncpy(info->fix.id, dev->driver->name, 16);
804 info->fix.type = FB_TYPE_PACKED_PIXELS;
805 info->fix.visual = FB_VISUAL_TRUECOLOR;
806 info->fix.xpanstep = 0;
807 info->fix.ypanstep = 0;
808 info->fix.ywrapstep = 0;
809 info->fix.line_length = width*bpp/8;
810 info->fix.accel = FB_ACCEL_NONE;
811 info->fix.smem_len = vmem_size;
812
813 info->var.rotate = pdata->rotate;
814 info->var.xres = width;
815 info->var.yres = height;
816 info->var.xres_virtual = info->var.xres;
817 info->var.yres_virtual = info->var.yres;
818 info->var.bits_per_pixel = bpp;
819 info->var.nonstd = 1;
820
821 /* RGB565 */
822 info->var.red.offset = 11;
823 info->var.red.length = 5;
824 info->var.green.offset = 5;
825 info->var.green.length = 6;
826 info->var.blue.offset = 0;
827 info->var.blue.length = 5;
828 info->var.transp.offset = 0;
829 info->var.transp.length = 0;
830
831 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
832
833 par = info->par;
834 par->info = info;
835 par->pdata = dev->platform_data;
836 par->debug = display->debug;
837 par->buf = buf;
838 spin_lock_init(&par->dirty_lock);
839 par->bgr = pdata->bgr;
840 par->startbyte = pdata->startbyte;
841 par->init_sequence = init_sequence;
842 par->gamma.curves = gamma_curves;
843 par->gamma.num_curves = display->gamma_num;
844 par->gamma.num_values = display->gamma_len;
845 mutex_init(&par->gamma.lock);
846 info->pseudo_palette = par->pseudo_palette;
847
848 if (par->gamma.curves && gamma) {
849 if (fbtft_gamma_parse_str(par,
850 par->gamma.curves, gamma, strlen(gamma)))
851 goto alloc_fail;
852 }
853
854 /* Transmit buffer */
855 if (txbuflen == -1)
856 txbuflen = vmem_size + 2; /* add in case startbyte is used */
857
858#ifdef __LITTLE_ENDIAN
859 if ((!txbuflen) && (bpp > 8))
860 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
861#endif
862
863 if (txbuflen > 0) {
864 if (dma) {
865 dev->coherent_dma_mask = ~0;
866 txbuf = dmam_alloc_coherent(dev, txbuflen, &par->txbuf.dma, GFP_DMA);
867 } else {
868 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
869 }
870 if (!txbuf)
871 goto alloc_fail;
872 par->txbuf.buf = txbuf;
873 par->txbuf.len = txbuflen;
874 }
875
876 /* Initialize gpios to disabled */
877 par->gpio.reset = -1;
878 par->gpio.dc = -1;
879 par->gpio.rd = -1;
880 par->gpio.wr = -1;
881 par->gpio.cs = -1;
882 par->gpio.latch = -1;
883 for (i = 0; i < 16; i++) {
884 par->gpio.db[i] = -1;
885 par->gpio.led[i] = -1;
886 par->gpio.aux[i] = -1;
887 }
888
889 /* default fbtft operations */
890 par->fbtftops.write = fbtft_write_spi;
891 par->fbtftops.read = fbtft_read_spi;
892 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
893 par->fbtftops.write_register = fbtft_write_reg8_bus8;
894 par->fbtftops.set_addr_win = fbtft_set_addr_win;
895 par->fbtftops.reset = fbtft_reset;
896 par->fbtftops.mkdirty = fbtft_mkdirty;
897 par->fbtftops.update_display = fbtft_update_display;
898 par->fbtftops.request_gpios = fbtft_request_gpios;
899 if (display->backlight)
900 par->fbtftops.register_backlight = fbtft_register_backlight;
901
902 /* use driver provided functions */
903 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
904
905 return info;
906
907alloc_fail:
908 vfree(vmem);
909
910 return NULL;
911}
912EXPORT_SYMBOL(fbtft_framebuffer_alloc);
913
914/**
915 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
916 *
917 * @info: frame buffer info structure
918 *
919 */
920void fbtft_framebuffer_release(struct fb_info *info)
921{
922 fb_deferred_io_cleanup(info);
923 vfree(info->screen_base);
924 framebuffer_release(info);
925}
926EXPORT_SYMBOL(fbtft_framebuffer_release);
927
928/**
929 * fbtft_register_framebuffer - registers a tft frame buffer device
930 * @fb_info: frame buffer info structure
931 *
932 * Sets SPI driverdata if needed
933 * Requests needed gpios.
934 * Initializes display
935 * Updates display.
936 * Registers a frame buffer device @fb_info.
937 *
938 * Returns negative errno on error, or zero for success.
939 *
940 */
941int fbtft_register_framebuffer(struct fb_info *fb_info)
942{
943 int ret;
944 char text1[50] = "";
945 char text2[50] = "";
946 struct fbtft_par *par = fb_info->par;
947 struct spi_device *spi = par->spi;
948
949 /* sanity checks */
950 if (!par->fbtftops.init_display) {
951 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
952 return -EINVAL;
953 }
954
955 if (spi)
956 spi_set_drvdata(spi, fb_info);
957 if (par->pdev)
958 platform_set_drvdata(par->pdev, fb_info);
959
960 ret = par->fbtftops.request_gpios(par);
961 if (ret < 0)
962 goto reg_fail;
963
964 if (par->fbtftops.verify_gpios) {
965 ret = par->fbtftops.verify_gpios(par);
966 if (ret < 0)
967 goto reg_fail;
968 }
969
970 ret = par->fbtftops.init_display(par);
971 if (ret < 0)
972 goto reg_fail;
973 if (par->fbtftops.set_var) {
974 ret = par->fbtftops.set_var(par);
975 if (ret < 0)
976 goto reg_fail;
977 }
978
979 /* update the entire display */
980 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
981
982 if (par->fbtftops.set_gamma && par->gamma.curves) {
983 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
984 if (ret)
985 goto reg_fail;
986 }
987
988 if (par->fbtftops.register_backlight)
989 par->fbtftops.register_backlight(par);
990
991 ret = register_framebuffer(fb_info);
992 if (ret < 0)
993 goto reg_fail;
994
995 fbtft_sysfs_init(par);
996
997 if (par->txbuf.buf)
998 sprintf(text1, ", %d KiB %sbuffer memory",
999 par->txbuf.len >> 10, par->txbuf.dma ? "DMA " : "");
1000 if (spi)
1001 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
1002 spi->chip_select, spi->max_speed_hz/1000000);
1003 dev_info(fb_info->dev,
1004 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
1005 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
1006 fb_info->fix.smem_len >> 10, text1,
1007 HZ/fb_info->fbdefio->delay, text2);
1008
1009#ifdef CONFIG_FB_BACKLIGHT
1010 /* Turn on backlight if available */
1011 if (fb_info->bl_dev) {
1012 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
1013 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
1014 }
1015#endif
1016
1017 return 0;
1018
1019reg_fail:
1020 if (par->fbtftops.unregister_backlight)
1021 par->fbtftops.unregister_backlight(par);
1022 if (spi)
1023 spi_set_drvdata(spi, NULL);
1024 if (par->pdev)
1025 platform_set_drvdata(par->pdev, NULL);
1026
1027 return ret;
1028}
1029EXPORT_SYMBOL(fbtft_register_framebuffer);
1030
1031/**
1032 * fbtft_unregister_framebuffer - releases a tft frame buffer device
1033 * @fb_info: frame buffer info structure
1034 *
1035 * Frees SPI driverdata if needed
1036 * Frees gpios.
1037 * Unregisters frame buffer device.
1038 *
1039 */
1040int fbtft_unregister_framebuffer(struct fb_info *fb_info)
1041{
1042 struct fbtft_par *par = fb_info->par;
1043 struct spi_device *spi = par->spi;
1044 int ret;
1045
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);
1053 ret = unregister_framebuffer(fb_info);
1054 return ret;
1055}
1056EXPORT_SYMBOL(fbtft_unregister_framebuffer);
1057
1058#ifdef CONFIG_OF
1059/**
1060 * fbtft_init_display_dt() - Device Tree init_display() function
1061 * @par: Driver data
1062 *
1063 * Return: 0 if successful, negative if error
1064 */
1065static int fbtft_init_display_dt(struct fbtft_par *par)
1066{
1067 struct device_node *node = par->info->device->of_node;
1068 struct property *prop;
1069 const __be32 *p;
1070 u32 val;
1071 int buf[64], i, j;
1072 char msg[128];
1073 char str[16];
1074
1075 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1076
1077 if (!node)
1078 return -EINVAL;
1079
1080 prop = of_find_property(node, "init", NULL);
1081 p = of_prop_next_u32(prop, NULL, &val);
1082 if (!p)
1083 return -EINVAL;
1084 while (p) {
1085 if (val & FBTFT_OF_INIT_CMD) {
1086 val &= 0xFFFF;
1087 i = 0;
1088 while (p && !(val & 0xFFFF0000)) {
1089 if (i > 63) {
1090 dev_err(par->info->device,
1091 "%s: Maximum register values exceeded\n",
1092 __func__);
1093 return -EINVAL;
1094 }
1095 buf[i++] = val;
1096 p = of_prop_next_u32(prop, p, &val);
1097 }
1098 /* make debug message */
1099 msg[0] = '\0';
1100 for (j = 0; j < i; j++) {
1101 snprintf(str, 128, " %02X", buf[j]);
1102 strcat(msg, str);
1103 }
1104 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1105 "init: write_register:%s\n", msg);
1106
1107 par->fbtftops.write_register(par, i,
1108 buf[0], buf[1], buf[2], buf[3],
1109 buf[4], buf[5], buf[6], buf[7],
1110 buf[8], buf[9], buf[10], buf[11],
1111 buf[12], buf[13], buf[14], buf[15],
1112 buf[16], buf[17], buf[18], buf[19],
1113 buf[20], buf[21], buf[22], buf[23],
1114 buf[24], buf[25], buf[26], buf[27],
1115 buf[28], buf[29], buf[30], buf[31],
1116 buf[32], buf[33], buf[34], buf[35],
1117 buf[36], buf[37], buf[38], buf[39],
1118 buf[40], buf[41], buf[42], buf[43],
1119 buf[44], buf[45], buf[46], buf[47],
1120 buf[48], buf[49], buf[50], buf[51],
1121 buf[52], buf[53], buf[54], buf[55],
1122 buf[56], buf[57], buf[58], buf[59],
1123 buf[60], buf[61], buf[62], buf[63]);
1124 } else if (val & FBTFT_OF_INIT_DELAY) {
1125 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1126 "init: msleep(%u)\n", val & 0xFFFF);
1127 msleep(val & 0xFFFF);
1128 p = of_prop_next_u32(prop, p, &val);
1129 } else {
1130 dev_err(par->info->device, "illegal init value 0x%X\n",
1131 val);
1132 return -EINVAL;
1133 }
1134 }
1135
1136 return 0;
1137}
1138#endif
1139
1140/**
1141 * fbtft_init_display() - Generic init_display() function
1142 * @par: Driver data
1143 *
1144 * Uses par->init_sequence to do the initialization
1145 *
1146 * Return: 0 if successful, negative if error
1147 */
1148int fbtft_init_display(struct fbtft_par *par)
1149{
1150 int buf[64];
1151 char msg[128];
1152 char str[16];
1153 int i = 0;
1154 int j;
1155
1156 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1157
1158 /* sanity check */
1159 if (!par->init_sequence) {
1160 dev_err(par->info->device,
1161 "error: init_sequence is not set\n");
1162 return -EINVAL;
1163 }
1164
1165 /* make sure stop marker exists */
1166 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1167 if (par->init_sequence[i] == -3)
1168 break;
1169 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1170 dev_err(par->info->device,
1171 "missing stop marker at end of init sequence\n");
1172 return -EINVAL;
1173 }
1174
1175 par->fbtftops.reset(par);
1176 if (par->gpio.cs != -1)
1177 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1178
1179 i = 0;
1180 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1181 if (par->init_sequence[i] == -3) {
1182 /* done */
1183 return 0;
1184 }
1185 if (par->init_sequence[i] >= 0) {
1186 dev_err(par->info->device,
1187 "missing delimiter at position %d\n", i);
1188 return -EINVAL;
1189 }
1190 if (par->init_sequence[i+1] < 0) {
1191 dev_err(par->info->device,
1192 "missing value after delimiter %d at position %d\n",
1193 par->init_sequence[i], i);
1194 return -EINVAL;
1195 }
1196 switch (par->init_sequence[i]) {
1197 case -1:
1198 i++;
1199 /* make debug message */
1200 strcpy(msg, "");
1201 j = i + 1;
1202 while (par->init_sequence[j] >= 0) {
1203 sprintf(str, "0x%02X ", par->init_sequence[j]);
1204 strcat(msg, str);
1205 j++;
1206 }
1207 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1208 "init: write(0x%02X) %s\n",
1209 par->init_sequence[i], msg);
1210
1211 /* Write */
1212 j = 0;
1213 while (par->init_sequence[i] >= 0) {
1214 if (j > 63) {
1215 dev_err(par->info->device,
1216 "%s: Maximum register values exceeded\n",
1217 __func__);
1218 return -EINVAL;
1219 }
1220 buf[j++] = par->init_sequence[i++];
1221 }
1222 par->fbtftops.write_register(par, j,
1223 buf[0], buf[1], buf[2], buf[3],
1224 buf[4], buf[5], buf[6], buf[7],
1225 buf[8], buf[9], buf[10], buf[11],
1226 buf[12], buf[13], buf[14], buf[15],
1227 buf[16], buf[17], buf[18], buf[19],
1228 buf[20], buf[21], buf[22], buf[23],
1229 buf[24], buf[25], buf[26], buf[27],
1230 buf[28], buf[29], buf[30], buf[31],
1231 buf[32], buf[33], buf[34], buf[35],
1232 buf[36], buf[37], buf[38], buf[39],
1233 buf[40], buf[41], buf[42], buf[43],
1234 buf[44], buf[45], buf[46], buf[47],
1235 buf[48], buf[49], buf[50], buf[51],
1236 buf[52], buf[53], buf[54], buf[55],
1237 buf[56], buf[57], buf[58], buf[59],
1238 buf[60], buf[61], buf[62], buf[63]);
1239 break;
1240 case -2:
1241 i++;
1242 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1243 "init: mdelay(%d)\n", par->init_sequence[i]);
1244 mdelay(par->init_sequence[i++]);
1245 break;
1246 default:
1247 dev_err(par->info->device,
1248 "unknown delimiter %d at position %d\n",
1249 par->init_sequence[i], i);
1250 return -EINVAL;
1251 }
1252 }
1253
1254 dev_err(par->info->device,
1255 "%s: something is wrong. Shouldn't get here.\n", __func__);
1256 return -EINVAL;
1257}
1258EXPORT_SYMBOL(fbtft_init_display);
1259
1260/**
1261 * fbtft_verify_gpios() - Generic verify_gpios() function
1262 * @par: Driver data
1263 *
1264 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1265 *
1266 * Return: 0 if successful, negative if error
1267 */
1268int fbtft_verify_gpios(struct fbtft_par *par)
1269{
1270 struct fbtft_platform_data *pdata;
1271 int i;
1272
1273 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1274
1275 pdata = par->info->device->platform_data;
1276 if (pdata->display.buswidth != 9 && par->startbyte == 0 && \
1277 par->gpio.dc < 0) {
1278 dev_err(par->info->device,
1279 "Missing info about 'dc' gpio. Aborting.\n");
1280 return -EINVAL;
1281 }
1282
1283 if (!par->pdev)
1284 return 0;
1285
1286 if (par->gpio.wr < 0) {
1287 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1288 return -EINVAL;
1289 }
1290 for (i = 0; i < pdata->display.buswidth; i++) {
1291 if (par->gpio.db[i] < 0) {
1292 dev_err(par->info->device,
1293 "Missing 'db%02d' gpio. Aborting.\n", i);
1294 return -EINVAL;
1295 }
1296 }
1297
1298 return 0;
1299}
1300
1301#ifdef CONFIG_OF
1302/* returns 0 if the property is not present */
1303static u32 fbtft_of_value(struct device_node *node, const char *propname)
1304{
1305 int ret;
1306 u32 val = 0;
1307
1308 ret = of_property_read_u32(node, propname, &val);
1309 if (ret == 0)
1310 pr_info("%s: %s = %u\n", __func__, propname, val);
1311
1312 return val;
1313}
1314
1315static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1316{
1317 struct device_node *node = dev->of_node;
1318 struct fbtft_platform_data *pdata;
1319
1320 if (!node) {
1321 dev_err(dev, "Missing platform data or DT\n");
1322 return ERR_PTR(-EINVAL);
1323 }
1324
1325 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1326 if (!pdata)
1327 return ERR_PTR(-ENOMEM);
1328
1329 pdata->display.width = fbtft_of_value(node, "width");
1330 pdata->display.height = fbtft_of_value(node, "height");
1331 pdata->display.regwidth = fbtft_of_value(node, "regwidth");
1332 pdata->display.buswidth = fbtft_of_value(node, "buswidth");
1333 pdata->display.backlight = fbtft_of_value(node, "backlight");
1334 pdata->display.bpp = fbtft_of_value(node, "bpp");
1335 pdata->display.debug = fbtft_of_value(node, "debug");
1336 pdata->rotate = fbtft_of_value(node, "rotate");
1337 pdata->bgr = of_property_read_bool(node, "bgr");
1338 pdata->fps = fbtft_of_value(node, "fps");
1339 pdata->txbuflen = fbtft_of_value(node, "txbuflen");
1340 pdata->startbyte = fbtft_of_value(node, "startbyte");
1341 of_property_read_string(node, "gamma", (const char **)&pdata->gamma);
1342
1343 if (of_find_property(node, "led-gpios", NULL))
1344 pdata->display.backlight = 1;
1345 if (of_find_property(node, "init", NULL))
1346 pdata->display.fbtftops.init_display = fbtft_init_display_dt;
1347 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt;
1348
1349 return pdata;
1350}
1351#else
1352static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1353{
1354 dev_err(dev, "Missing platform data\n");
1355 return ERR_PTR(-EINVAL);
1356}
1357#endif
1358
1359/**
1360 * fbtft_probe_common() - Generic device probe() helper function
1361 * @display: Display properties
1362 * @sdev: SPI device
1363 * @pdev: Platform device
1364 *
1365 * Allocates, initializes and registers a framebuffer
1366 *
1367 * Either @sdev or @pdev should be NULL
1368 *
1369 * Return: 0 if successful, negative if error
1370 */
1371int fbtft_probe_common(struct fbtft_display *display,
1372 struct spi_device *sdev, struct platform_device *pdev)
1373{
1374 struct device *dev;
1375 struct fb_info *info;
1376 struct fbtft_par *par;
1377 struct fbtft_platform_data *pdata;
1378 int ret;
1379
1380 if (sdev)
1381 dev = &sdev->dev;
1382 else
1383 dev = &pdev->dev;
1384
1385 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1386 dev_info(dev, "%s()\n", __func__);
1387
1388 pdata = dev->platform_data;
1389 if (!pdata) {
1390 pdata = fbtft_probe_dt(dev);
1391 if (IS_ERR(pdata))
1392 return PTR_ERR(pdata);
1393 dev->platform_data = pdata;
1394 }
1395
1396 info = fbtft_framebuffer_alloc(display, dev);
1397 if (!info)
1398 return -ENOMEM;
1399
1400 par = info->par;
1401 par->spi = sdev;
1402 par->pdev = pdev;
1403
1404 if (display->buswidth == 0) {
1405 dev_err(dev, "buswidth is not set\n");
1406 return -EINVAL;
1407 }
1408
1409 /* write register functions */
1410 if (display->regwidth == 8 && display->buswidth == 8) {
1411 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1412 } else
1413 if (display->regwidth == 8 && display->buswidth == 9 && par->spi) {
1414 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1415 } else if (display->regwidth == 16 && display->buswidth == 8) {
1416 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1417 } else if (display->regwidth == 16 && display->buswidth == 16) {
1418 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1419 } else {
1420 dev_warn(dev,
1421 "no default functions for regwidth=%d and buswidth=%d\n",
1422 display->regwidth, display->buswidth);
1423 }
1424
1425 /* write_vmem() functions */
1426 if (display->buswidth == 8)
1427 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1428 else if (display->buswidth == 9)
1429 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1430 else if (display->buswidth == 16)
1431 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1432
1433 /* GPIO write() functions */
1434 if (par->pdev) {
1435 if (display->buswidth == 8)
1436 par->fbtftops.write = fbtft_write_gpio8_wr;
1437 else if (display->buswidth == 16)
1438 par->fbtftops.write = fbtft_write_gpio16_wr;
1439 }
1440
1441 /* 9-bit SPI setup */
1442 if (par->spi && display->buswidth == 9) {
1443 par->spi->bits_per_word = 9;
1444 ret = par->spi->master->setup(par->spi);
1445 if (ret) {
1446 dev_warn(&par->spi->dev,
1447 "9-bit SPI not available, emulating using 8-bit.\n");
1448 par->spi->bits_per_word = 8;
1449 ret = par->spi->master->setup(par->spi);
1450 if (ret)
1451 goto out_release;
1452 /* allocate buffer with room for dc bits */
1453 par->extra = devm_kzalloc(par->info->device,
1454 par->txbuf.len + (par->txbuf.len / 8) + 8,
1455 GFP_KERNEL);
1456 if (!par->extra) {
1457 ret = -ENOMEM;
1458 goto out_release;
1459 }
1460 par->fbtftops.write = fbtft_write_spi_emulate_9;
1461 }
1462 }
1463
1464 if (!par->fbtftops.verify_gpios)
1465 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1466
1467 /* make sure we still use the driver provided functions */
1468 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1469
1470 /* use init_sequence if provided */
1471 if (par->init_sequence)
1472 par->fbtftops.init_display = fbtft_init_display;
1473
1474 /* use platform_data provided functions above all */
1475 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1476
1477 ret = fbtft_register_framebuffer(info);
1478 if (ret < 0)
1479 goto out_release;
1480
1481 return 0;
1482
1483out_release:
1484 fbtft_framebuffer_release(info);
1485
1486 return ret;
1487}
1488EXPORT_SYMBOL(fbtft_probe_common);
1489
1490/**
1491 * fbtft_remove_common() - Generic device remove() helper function
1492 * @dev: Device
1493 * @info: Framebuffer
1494 *
1495 * Unregisters and releases the framebuffer
1496 *
1497 * Return: 0 if successful, negative if error
1498 */
1499int fbtft_remove_common(struct device *dev, struct fb_info *info)
1500{
1501 struct fbtft_par *par;
1502
1503 if (!info)
1504 return -EINVAL;
1505 par = info->par;
1506 if (par)
1507 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1508 "%s()\n", __func__);
1509 fbtft_unregister_framebuffer(info);
1510 fbtft_framebuffer_release(info);
1511
1512 return 0;
1513}
1514EXPORT_SYMBOL(fbtft_remove_common);
1515
1516MODULE_LICENSE("GPL");