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