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