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