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