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