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