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