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