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