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