• 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,void * buf,size_t len,const char * fmt,...)51 void fbtft_dbg_hex(const struct device *dev, int groupsize,
52 		   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 * pagelist)325 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
326 {
327 	struct fbtft_par *par = info->par;
328 	unsigned int dirty_lines_start, dirty_lines_end;
329 	struct page *page;
330 	unsigned long index;
331 	unsigned int y_low = 0, y_high = 0;
332 	int count = 0;
333 
334 	spin_lock(&par->dirty_lock);
335 	dirty_lines_start = par->dirty_lines_start;
336 	dirty_lines_end = par->dirty_lines_end;
337 	/* set display line markers as clean */
338 	par->dirty_lines_start = par->info->var.yres - 1;
339 	par->dirty_lines_end = 0;
340 	spin_unlock(&par->dirty_lock);
341 
342 	/* Mark display lines as dirty */
343 	list_for_each_entry(page, pagelist, lru) {
344 		count++;
345 		index = page->index << PAGE_SHIFT;
346 		y_low = index / info->fix.line_length;
347 		y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
348 		dev_dbg(info->device,
349 			"page->index=%lu y_low=%d y_high=%d\n",
350 			page->index, y_low, y_high);
351 		if (y_high > info->var.yres - 1)
352 			y_high = info->var.yres - 1;
353 		if (y_low < dirty_lines_start)
354 			dirty_lines_start = y_low;
355 		if (y_high > dirty_lines_end)
356 			dirty_lines_end = y_high;
357 	}
358 
359 	par->fbtftops.update_display(info->par,
360 					dirty_lines_start, dirty_lines_end);
361 }
362 
fbtft_fb_fillrect(struct fb_info * info,const struct fb_fillrect * rect)363 static void fbtft_fb_fillrect(struct fb_info *info,
364 			      const struct fb_fillrect *rect)
365 {
366 	struct fbtft_par *par = info->par;
367 
368 	dev_dbg(info->dev,
369 		"%s: dx=%d, dy=%d, width=%d, height=%d\n",
370 		__func__, rect->dx, rect->dy, rect->width, rect->height);
371 	sys_fillrect(info, rect);
372 
373 	par->fbtftops.mkdirty(info, rect->dy, rect->height);
374 }
375 
fbtft_fb_copyarea(struct fb_info * info,const struct fb_copyarea * area)376 static void fbtft_fb_copyarea(struct fb_info *info,
377 			      const struct fb_copyarea *area)
378 {
379 	struct fbtft_par *par = info->par;
380 
381 	dev_dbg(info->dev,
382 		"%s: dx=%d, dy=%d, width=%d, height=%d\n",
383 		__func__,  area->dx, area->dy, area->width, area->height);
384 	sys_copyarea(info, area);
385 
386 	par->fbtftops.mkdirty(info, area->dy, area->height);
387 }
388 
fbtft_fb_imageblit(struct fb_info * info,const struct fb_image * image)389 static void fbtft_fb_imageblit(struct fb_info *info,
390 			       const struct fb_image *image)
391 {
392 	struct fbtft_par *par = info->par;
393 
394 	dev_dbg(info->dev,
395 		"%s: dx=%d, dy=%d, width=%d, height=%d\n",
396 		__func__,  image->dx, image->dy, image->width, image->height);
397 	sys_imageblit(info, image);
398 
399 	par->fbtftops.mkdirty(info, image->dy, image->height);
400 }
401 
fbtft_fb_write(struct fb_info * info,const char __user * buf,size_t count,loff_t * ppos)402 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
403 			      size_t count, loff_t *ppos)
404 {
405 	struct fbtft_par *par = info->par;
406 	ssize_t res;
407 
408 	dev_dbg(info->dev,
409 		"%s: count=%zd, ppos=%llu\n", __func__,  count, *ppos);
410 	res = fb_sys_write(info, buf, count, ppos);
411 
412 	/* TODO: only mark changed area update all for now */
413 	par->fbtftops.mkdirty(info, -1, 0);
414 
415 	return res;
416 }
417 
418 /* from pxafb.c */
chan_to_field(unsigned int chan,struct fb_bitfield * bf)419 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
420 {
421 	chan &= 0xffff;
422 	chan >>= 16 - bf->length;
423 	return chan << bf->offset;
424 }
425 
fbtft_fb_setcolreg(unsigned int regno,unsigned int red,unsigned int green,unsigned int blue,unsigned int transp,struct fb_info * info)426 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red,
427 			      unsigned int green, unsigned int blue,
428 			      unsigned int transp, struct fb_info *info)
429 {
430 	unsigned int val;
431 	int ret = 1;
432 
433 	dev_dbg(info->dev,
434 		"%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
435 		__func__, regno, red, green, blue, transp);
436 
437 	switch (info->fix.visual) {
438 	case FB_VISUAL_TRUECOLOR:
439 		if (regno < 16) {
440 			u32 *pal = info->pseudo_palette;
441 
442 			val  = chan_to_field(red,   &info->var.red);
443 			val |= chan_to_field(green, &info->var.green);
444 			val |= chan_to_field(blue,  &info->var.blue);
445 
446 			pal[regno] = val;
447 			ret = 0;
448 		}
449 		break;
450 	}
451 	return ret;
452 }
453 
fbtft_fb_blank(int blank,struct fb_info * info)454 static int fbtft_fb_blank(int blank, struct fb_info *info)
455 {
456 	struct fbtft_par *par = info->par;
457 	int ret = -EINVAL;
458 
459 	dev_dbg(info->dev, "%s(blank=%d)\n",
460 		__func__, blank);
461 
462 	if (!par->fbtftops.blank)
463 		return ret;
464 
465 	switch (blank) {
466 	case FB_BLANK_POWERDOWN:
467 	case FB_BLANK_VSYNC_SUSPEND:
468 	case FB_BLANK_HSYNC_SUSPEND:
469 	case FB_BLANK_NORMAL:
470 		ret = par->fbtftops.blank(par, true);
471 		break;
472 	case FB_BLANK_UNBLANK:
473 		ret = par->fbtftops.blank(par, false);
474 		break;
475 	}
476 	return ret;
477 }
478 
fbtft_merge_fbtftops(struct fbtft_ops * dst,struct fbtft_ops * src)479 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
480 {
481 	if (src->write)
482 		dst->write = src->write;
483 	if (src->read)
484 		dst->read = src->read;
485 	if (src->write_vmem)
486 		dst->write_vmem = src->write_vmem;
487 	if (src->write_register)
488 		dst->write_register = src->write_register;
489 	if (src->set_addr_win)
490 		dst->set_addr_win = src->set_addr_win;
491 	if (src->reset)
492 		dst->reset = src->reset;
493 	if (src->mkdirty)
494 		dst->mkdirty = src->mkdirty;
495 	if (src->update_display)
496 		dst->update_display = src->update_display;
497 	if (src->init_display)
498 		dst->init_display = src->init_display;
499 	if (src->blank)
500 		dst->blank = src->blank;
501 	if (src->request_gpios_match)
502 		dst->request_gpios_match = src->request_gpios_match;
503 	if (src->request_gpios)
504 		dst->request_gpios = src->request_gpios;
505 	if (src->verify_gpios)
506 		dst->verify_gpios = src->verify_gpios;
507 	if (src->register_backlight)
508 		dst->register_backlight = src->register_backlight;
509 	if (src->unregister_backlight)
510 		dst->unregister_backlight = src->unregister_backlight;
511 	if (src->set_var)
512 		dst->set_var = src->set_var;
513 	if (src->set_gamma)
514 		dst->set_gamma = src->set_gamma;
515 }
516 
517 /**
518  * fbtft_framebuffer_alloc - creates a new frame buffer info structure
519  *
520  * @display: pointer to structure describing the display
521  * @dev: pointer to the device for this fb, this can be NULL
522  * @pdata: platform data for the display in use
523  *
524  * Creates a new frame buffer info structure.
525  *
526  * Also creates and populates the following structures:
527  *   info->fbops
528  *   info->fbdefio
529  *   info->pseudo_palette
530  *   par->fbtftops
531  *   par->txbuf
532  *
533  * Returns the new structure, or NULL if an error occurred.
534  *
535  */
fbtft_framebuffer_alloc(struct fbtft_display * display,struct device * dev,struct fbtft_platform_data * pdata)536 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
537 					struct device *dev,
538 					struct fbtft_platform_data *pdata)
539 {
540 	struct fb_info *info;
541 	struct fbtft_par *par;
542 	struct fb_ops *fbops = NULL;
543 	struct fb_deferred_io *fbdefio = NULL;
544 	u8 *vmem = NULL;
545 	void *txbuf = NULL;
546 	void *buf = NULL;
547 	unsigned int width;
548 	unsigned int height;
549 	int txbuflen = display->txbuflen;
550 	unsigned int bpp = display->bpp;
551 	unsigned int fps = display->fps;
552 	int vmem_size;
553 	const s16 *init_sequence = display->init_sequence;
554 	char *gamma = display->gamma;
555 	u32 *gamma_curves = NULL;
556 
557 	/* sanity check */
558 	if (display->gamma_num * display->gamma_len >
559 			FBTFT_GAMMA_MAX_VALUES_TOTAL) {
560 		dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
561 			FBTFT_GAMMA_MAX_VALUES_TOTAL);
562 		return NULL;
563 	}
564 
565 	/* defaults */
566 	if (!fps)
567 		fps = 20;
568 	if (!bpp)
569 		bpp = 16;
570 
571 	if (!pdata) {
572 		dev_err(dev, "platform data is missing\n");
573 		return NULL;
574 	}
575 
576 	/* override driver values? */
577 	if (pdata->fps)
578 		fps = pdata->fps;
579 	if (pdata->txbuflen)
580 		txbuflen = pdata->txbuflen;
581 	if (pdata->display.init_sequence)
582 		init_sequence = pdata->display.init_sequence;
583 	if (pdata->gamma)
584 		gamma = pdata->gamma;
585 	if (pdata->display.debug)
586 		display->debug = pdata->display.debug;
587 	if (pdata->display.backlight)
588 		display->backlight = pdata->display.backlight;
589 	if (pdata->display.width)
590 		display->width = pdata->display.width;
591 	if (pdata->display.height)
592 		display->height = pdata->display.height;
593 	if (pdata->display.buswidth)
594 		display->buswidth = pdata->display.buswidth;
595 	if (pdata->display.regwidth)
596 		display->regwidth = pdata->display.regwidth;
597 
598 	display->debug |= debug;
599 	fbtft_expand_debug_value(&display->debug);
600 
601 	switch (pdata->rotate) {
602 	case 90:
603 	case 270:
604 		width =  display->height;
605 		height = display->width;
606 		break;
607 	default:
608 		width =  display->width;
609 		height = display->height;
610 	}
611 
612 	vmem_size = display->width * display->height * bpp / 8;
613 	vmem = vzalloc(vmem_size);
614 	if (!vmem)
615 		goto alloc_fail;
616 
617 	fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
618 	if (!fbops)
619 		goto alloc_fail;
620 
621 	fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
622 	if (!fbdefio)
623 		goto alloc_fail;
624 
625 	buf = devm_kzalloc(dev, 128, GFP_KERNEL);
626 	if (!buf)
627 		goto alloc_fail;
628 
629 	if (display->gamma_num && display->gamma_len) {
630 		gamma_curves = devm_kcalloc(dev,
631 					    display->gamma_num *
632 					    display->gamma_len,
633 					    sizeof(gamma_curves[0]),
634 					    GFP_KERNEL);
635 		if (!gamma_curves)
636 			goto alloc_fail;
637 	}
638 
639 	info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
640 	if (!info)
641 		goto alloc_fail;
642 
643 	info->screen_buffer = vmem;
644 	info->fbops = fbops;
645 	info->fbdefio = fbdefio;
646 
647 	fbops->owner        =      dev->driver->owner;
648 	fbops->fb_read      =      fb_sys_read;
649 	fbops->fb_write     =      fbtft_fb_write;
650 	fbops->fb_fillrect  =      fbtft_fb_fillrect;
651 	fbops->fb_copyarea  =      fbtft_fb_copyarea;
652 	fbops->fb_imageblit =      fbtft_fb_imageblit;
653 	fbops->fb_setcolreg =      fbtft_fb_setcolreg;
654 	fbops->fb_blank     =      fbtft_fb_blank;
655 
656 	fbdefio->delay =           HZ / fps;
657 	fbdefio->deferred_io =     fbtft_deferred_io;
658 	fb_deferred_io_init(info);
659 
660 	snprintf(info->fix.id, sizeof(info->fix.id), "%s", dev->driver->name);
661 	info->fix.type =           FB_TYPE_PACKED_PIXELS;
662 	info->fix.visual =         FB_VISUAL_TRUECOLOR;
663 	info->fix.xpanstep =	   0;
664 	info->fix.ypanstep =	   0;
665 	info->fix.ywrapstep =	   0;
666 	info->fix.line_length =    width * bpp / 8;
667 	info->fix.accel =          FB_ACCEL_NONE;
668 	info->fix.smem_len =       vmem_size;
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 	char msg[128];
989 	char str[16];
990 	int i = 0;
991 	int j;
992 
993 	/* sanity check */
994 	if (!par->init_sequence) {
995 		dev_err(par->info->device,
996 			"error: init_sequence is not set\n");
997 		return -EINVAL;
998 	}
999 
1000 	/* make sure stop marker exists */
1001 	for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1002 		if (par->init_sequence[i] == -3)
1003 			break;
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 			/* make debug message */
1033 			strcpy(msg, "");
1034 			j = i + 1;
1035 			while (par->init_sequence[j] >= 0) {
1036 				sprintf(str, "0x%02X ", par->init_sequence[j]);
1037 				strcat(msg, str);
1038 				j++;
1039 			}
1040 			fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1041 				      "init: write(0x%02X) %s\n",
1042 				      par->init_sequence[i], msg);
1043 
1044 			/* Write */
1045 			j = 0;
1046 			while (par->init_sequence[i] >= 0) {
1047 				if (j > 63) {
1048 					dev_err(par->info->device,
1049 						"%s: Maximum register values exceeded\n",
1050 						__func__);
1051 					return -EINVAL;
1052 				}
1053 				buf[j++] = par->init_sequence[i++];
1054 			}
1055 			par->fbtftops.write_register(par, j,
1056 				buf[0], buf[1], buf[2], buf[3],
1057 				buf[4], buf[5], buf[6], buf[7],
1058 				buf[8], buf[9], buf[10], buf[11],
1059 				buf[12], buf[13], buf[14], buf[15],
1060 				buf[16], buf[17], buf[18], buf[19],
1061 				buf[20], buf[21], buf[22], buf[23],
1062 				buf[24], buf[25], buf[26], buf[27],
1063 				buf[28], buf[29], buf[30], buf[31],
1064 				buf[32], buf[33], buf[34], buf[35],
1065 				buf[36], buf[37], buf[38], buf[39],
1066 				buf[40], buf[41], buf[42], buf[43],
1067 				buf[44], buf[45], buf[46], buf[47],
1068 				buf[48], buf[49], buf[50], buf[51],
1069 				buf[52], buf[53], buf[54], buf[55],
1070 				buf[56], buf[57], buf[58], buf[59],
1071 				buf[60], buf[61], buf[62], buf[63]);
1072 			break;
1073 		case -2:
1074 			i++;
1075 			fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1076 				      "init: mdelay(%d)\n",
1077 				      par->init_sequence[i]);
1078 			mdelay(par->init_sequence[i++]);
1079 			break;
1080 		default:
1081 			dev_err(par->info->device,
1082 				"unknown delimiter %d at position %d\n",
1083 				par->init_sequence[i], i);
1084 			return -EINVAL;
1085 		}
1086 	}
1087 
1088 	dev_err(par->info->device,
1089 		"%s: something is wrong. Shouldn't get here.\n", __func__);
1090 	return -EINVAL;
1091 }
1092 EXPORT_SYMBOL(fbtft_init_display);
1093 
1094 /**
1095  * fbtft_verify_gpios() - Generic verify_gpios() function
1096  * @par: Driver data
1097  *
1098  * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1099  *
1100  * Return: 0 if successful, negative if error
1101  */
fbtft_verify_gpios(struct fbtft_par * par)1102 static int fbtft_verify_gpios(struct fbtft_par *par)
1103 {
1104 	struct fbtft_platform_data *pdata = par->pdata;
1105 	int i;
1106 
1107 	fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1108 
1109 	if (pdata->display.buswidth != 9 &&  par->startbyte == 0 &&
1110 	    !par->gpio.dc) {
1111 		dev_err(par->info->device,
1112 			"Missing info about 'dc' gpio. Aborting.\n");
1113 		return -EINVAL;
1114 	}
1115 
1116 	if (!par->pdev)
1117 		return 0;
1118 
1119 	if (!par->gpio.wr) {
1120 		dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1121 		return -EINVAL;
1122 	}
1123 	for (i = 0; i < pdata->display.buswidth; i++) {
1124 		if (!par->gpio.db[i]) {
1125 			dev_err(par->info->device,
1126 				"Missing 'db%02d' gpio. Aborting.\n", i);
1127 			return -EINVAL;
1128 		}
1129 	}
1130 
1131 	return 0;
1132 }
1133 
1134 /* returns 0 if the property is not present */
fbtft_property_value(struct device * dev,const char * propname)1135 static u32 fbtft_property_value(struct device *dev, const char *propname)
1136 {
1137 	int ret;
1138 	u32 val = 0;
1139 
1140 	ret = device_property_read_u32(dev, propname, &val);
1141 	if (ret == 0)
1142 		dev_info(dev, "%s: %s = %u\n", __func__, propname, val);
1143 
1144 	return val;
1145 }
1146 
fbtft_properties_read(struct device * dev)1147 static struct fbtft_platform_data *fbtft_properties_read(struct device *dev)
1148 {
1149 	struct fbtft_platform_data *pdata;
1150 
1151 	if (!dev_fwnode(dev)) {
1152 		dev_err(dev, "Missing platform data or properties\n");
1153 		return ERR_PTR(-EINVAL);
1154 	}
1155 
1156 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1157 	if (!pdata)
1158 		return ERR_PTR(-ENOMEM);
1159 
1160 	pdata->display.width = fbtft_property_value(dev, "width");
1161 	pdata->display.height = fbtft_property_value(dev, "height");
1162 	pdata->display.regwidth = fbtft_property_value(dev, "regwidth");
1163 	pdata->display.buswidth = fbtft_property_value(dev, "buswidth");
1164 	pdata->display.backlight = fbtft_property_value(dev, "backlight");
1165 	pdata->display.bpp = fbtft_property_value(dev, "bpp");
1166 	pdata->display.debug = fbtft_property_value(dev, "debug");
1167 	pdata->rotate = fbtft_property_value(dev, "rotate");
1168 	pdata->bgr = device_property_read_bool(dev, "bgr");
1169 	pdata->fps = fbtft_property_value(dev, "fps");
1170 	pdata->txbuflen = fbtft_property_value(dev, "txbuflen");
1171 	pdata->startbyte = fbtft_property_value(dev, "startbyte");
1172 	device_property_read_string(dev, "gamma", (const char **)&pdata->gamma);
1173 
1174 	if (device_property_present(dev, "led-gpios"))
1175 		pdata->display.backlight = 1;
1176 	if (device_property_present(dev, "init"))
1177 		pdata->display.fbtftops.init_display =
1178 			fbtft_init_display_from_property;
1179 
1180 	pdata->display.fbtftops.request_gpios = fbtft_request_gpios;
1181 
1182 	return pdata;
1183 }
1184 
1185 /**
1186  * fbtft_probe_common() - Generic device probe() helper function
1187  * @display: Display properties
1188  * @sdev: SPI device
1189  * @pdev: Platform device
1190  *
1191  * Allocates, initializes and registers a framebuffer
1192  *
1193  * Either @sdev or @pdev should be NULL
1194  *
1195  * Return: 0 if successful, negative if error
1196  */
fbtft_probe_common(struct fbtft_display * display,struct spi_device * sdev,struct platform_device * pdev)1197 int fbtft_probe_common(struct fbtft_display *display,
1198 		       struct spi_device *sdev,
1199 		       struct platform_device *pdev)
1200 {
1201 	struct device *dev;
1202 	struct fb_info *info;
1203 	struct fbtft_par *par;
1204 	struct fbtft_platform_data *pdata;
1205 	int ret;
1206 
1207 	if (sdev)
1208 		dev = &sdev->dev;
1209 	else
1210 		dev = &pdev->dev;
1211 
1212 	if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1213 		dev_info(dev, "%s()\n", __func__);
1214 
1215 	pdata = dev->platform_data;
1216 	if (!pdata) {
1217 		pdata = fbtft_properties_read(dev);
1218 		if (IS_ERR(pdata))
1219 			return PTR_ERR(pdata);
1220 	}
1221 
1222 	info = fbtft_framebuffer_alloc(display, dev, pdata);
1223 	if (!info)
1224 		return -ENOMEM;
1225 
1226 	par = info->par;
1227 	par->spi = sdev;
1228 	par->pdev = pdev;
1229 
1230 	if (display->buswidth == 0) {
1231 		dev_err(dev, "buswidth is not set\n");
1232 		return -EINVAL;
1233 	}
1234 
1235 	/* write register functions */
1236 	if (display->regwidth == 8 && display->buswidth == 8)
1237 		par->fbtftops.write_register = fbtft_write_reg8_bus8;
1238 	else if (display->regwidth == 8 && display->buswidth == 9 && par->spi)
1239 		par->fbtftops.write_register = fbtft_write_reg8_bus9;
1240 	else if (display->regwidth == 16 && display->buswidth == 8)
1241 		par->fbtftops.write_register = fbtft_write_reg16_bus8;
1242 	else if (display->regwidth == 16 && display->buswidth == 16)
1243 		par->fbtftops.write_register = fbtft_write_reg16_bus16;
1244 	else
1245 		dev_warn(dev,
1246 			 "no default functions for regwidth=%d and buswidth=%d\n",
1247 			 display->regwidth, display->buswidth);
1248 
1249 	/* write_vmem() functions */
1250 	if (display->buswidth == 8)
1251 		par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1252 	else if (display->buswidth == 9)
1253 		par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1254 	else if (display->buswidth == 16)
1255 		par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1256 
1257 	/* GPIO write() functions */
1258 	if (par->pdev) {
1259 		if (display->buswidth == 8)
1260 			par->fbtftops.write = fbtft_write_gpio8_wr;
1261 		else if (display->buswidth == 16)
1262 			par->fbtftops.write = fbtft_write_gpio16_wr;
1263 	}
1264 
1265 	/* 9-bit SPI setup */
1266 	if (par->spi && display->buswidth == 9) {
1267 		if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1268 			par->spi->bits_per_word = 9;
1269 		} else {
1270 			dev_warn(&par->spi->dev,
1271 				 "9-bit SPI not available, emulating using 8-bit.\n");
1272 			/* allocate buffer with room for dc bits */
1273 			par->extra = devm_kzalloc(par->info->device,
1274 						  par->txbuf.len +
1275 						  (par->txbuf.len / 8) + 8,
1276 						  GFP_KERNEL);
1277 			if (!par->extra) {
1278 				ret = -ENOMEM;
1279 				goto out_release;
1280 			}
1281 			par->fbtftops.write = fbtft_write_spi_emulate_9;
1282 		}
1283 	}
1284 
1285 	if (!par->fbtftops.verify_gpios)
1286 		par->fbtftops.verify_gpios = fbtft_verify_gpios;
1287 
1288 	/* make sure we still use the driver provided functions */
1289 	fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1290 
1291 	/* use init_sequence if provided */
1292 	if (par->init_sequence)
1293 		par->fbtftops.init_display = fbtft_init_display;
1294 
1295 	/* use platform_data provided functions above all */
1296 	fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1297 
1298 	ret = fbtft_register_framebuffer(info);
1299 	if (ret < 0)
1300 		goto out_release;
1301 
1302 	return 0;
1303 
1304 out_release:
1305 	fbtft_framebuffer_release(info);
1306 
1307 	return ret;
1308 }
1309 EXPORT_SYMBOL(fbtft_probe_common);
1310 
1311 /**
1312  * fbtft_remove_common() - Generic device remove() helper function
1313  * @dev: Device
1314  * @info: Framebuffer
1315  *
1316  * Unregisters and releases the framebuffer
1317  *
1318  * Return: 0 if successful, negative if error
1319  */
fbtft_remove_common(struct device * dev,struct fb_info * info)1320 int fbtft_remove_common(struct device *dev, struct fb_info *info)
1321 {
1322 	struct fbtft_par *par;
1323 
1324 	if (!info)
1325 		return -EINVAL;
1326 	par = info->par;
1327 	if (par)
1328 		fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1329 			      "%s()\n", __func__);
1330 	fbtft_unregister_framebuffer(info);
1331 	fbtft_framebuffer_release(info);
1332 
1333 	return 0;
1334 }
1335 EXPORT_SYMBOL(fbtft_remove_common);
1336 
1337 MODULE_LICENSE("GPL");
1338