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