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