blob: 9ce929d0e5c2bc56364dd4c324600f2099bd57b5 [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);
305 if (!bl_ops) {
306 dev_err(par->info->device,
307 "%s: could not allocate memeory for backlight operations.\n",
308 __func__);
309 return;
310 }
311
312 bl_ops->get_brightness = fbtft_backlight_get_brightness;
313 bl_ops->update_status = fbtft_backlight_update_status;
314 bl_props.type = BACKLIGHT_RAW;
315 /* Assume backlight is off, get polarity from current state of pin */
316 bl_props.power = FB_BLANK_POWERDOWN;
317 if (!gpio_get_value(par->gpio.led[0]))
318 bl_props.state |= BL_CORE_DRIVER1;
319
320 bd = backlight_device_register(dev_driver_string(par->info->device),
321 par->info->device, par, bl_ops, &bl_props);
322 if (IS_ERR(bd)) {
323 dev_err(par->info->device,
324 "cannot register backlight device (%ld)\n",
325 PTR_ERR(bd));
326 return;
327 }
328 par->info->bl_dev = bd;
329
330 if (!par->fbtftops.unregister_backlight)
331 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
332}
333#else
334void fbtft_register_backlight(struct fbtft_par *par) { };
335void fbtft_unregister_backlight(struct fbtft_par *par) { };
336#endif
337EXPORT_SYMBOL(fbtft_register_backlight);
338EXPORT_SYMBOL(fbtft_unregister_backlight);
339
Krzysztof Adamskied208432015-01-22 19:08:58 +0100340static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
341 int ye)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100342{
343 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
344 "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
345
346 /* Column address set */
347 write_reg(par, 0x2A,
348 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
349
350 /* Row adress set */
351 write_reg(par, 0x2B,
352 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
353
354 /* Memory write */
355 write_reg(par, 0x2C);
356}
357
358
Krzysztof Adamskied208432015-01-22 19:08:58 +0100359static void fbtft_reset(struct fbtft_par *par)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100360{
361 if (par->gpio.reset == -1)
362 return;
363 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
364 gpio_set_value(par->gpio.reset, 0);
365 udelay(20);
366 gpio_set_value(par->gpio.reset, 1);
367 mdelay(120);
368}
369
370
Krzysztof Adamskied208432015-01-22 19:08:58 +0100371static void fbtft_update_display(struct fbtft_par *par, unsigned start_line,
372 unsigned end_line)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100373{
374 size_t offset, len;
375 struct timespec ts_start, ts_end, ts_fps, ts_duration;
376 long fps_ms, fps_us, duration_ms, duration_us;
377 long fps, throughput;
378 bool timeit = false;
379 int ret = 0;
380
381 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE | DEBUG_TIME_EACH_UPDATE))) {
Yeliz Taneroglu6ba67a52015-03-02 17:49:59 +0200382 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100383 ((par->debug & DEBUG_TIME_FIRST_UPDATE) && !par->first_update_done)) {
384 getnstimeofday(&ts_start);
385 timeit = true;
386 }
387 }
388
389 /* Sanity checks */
390 if (start_line > end_line) {
391 dev_warn(par->info->device,
392 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
393 __func__, start_line, end_line);
394 start_line = 0;
395 end_line = par->info->var.yres - 1;
396 }
397 if (start_line > par->info->var.yres - 1 || end_line > par->info->var.yres - 1) {
398 dev_warn(par->info->device,
399 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
400 __func__, start_line, end_line, par->info->var.yres - 1);
401 start_line = 0;
402 end_line = par->info->var.yres - 1;
403 }
404
405 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
406 __func__, start_line, end_line);
407
408 if (par->fbtftops.set_addr_win)
409 par->fbtftops.set_addr_win(par, 0, start_line,
410 par->info->var.xres-1, end_line);
411
412 offset = start_line * par->info->fix.line_length;
413 len = (end_line - start_line + 1) * par->info->fix.line_length;
414 ret = par->fbtftops.write_vmem(par, offset, len);
415 if (ret < 0)
416 dev_err(par->info->device,
417 "%s: write_vmem failed to update display buffer\n",
418 __func__);
419
420 if (unlikely(timeit)) {
421 getnstimeofday(&ts_end);
422 if (par->update_time.tv_nsec == 0 && par->update_time.tv_sec == 0) {
423 par->update_time.tv_sec = ts_start.tv_sec;
424 par->update_time.tv_nsec = ts_start.tv_nsec;
425 }
426 ts_fps = timespec_sub(ts_start, par->update_time);
427 par->update_time.tv_sec = ts_start.tv_sec;
428 par->update_time.tv_nsec = ts_start.tv_nsec;
429 fps_ms = (ts_fps.tv_sec * 1000) + ((ts_fps.tv_nsec / 1000000) % 1000);
430 fps_us = (ts_fps.tv_nsec / 1000) % 1000;
431 fps = fps_ms * 1000 + fps_us;
432 fps = fps ? 1000000 / fps : 0;
433
434 ts_duration = timespec_sub(ts_end, ts_start);
435 duration_ms = (ts_duration.tv_sec * 1000) + ((ts_duration.tv_nsec / 1000000) % 1000);
436 duration_us = (ts_duration.tv_nsec / 1000) % 1000;
437 throughput = duration_ms * 1000 + duration_us;
438 throughput = throughput ? (len * 1000) / throughput : 0;
439 throughput = throughput * 1000 / 1024;
440
441 dev_info(par->info->device,
442 "Display update: %ld kB/s (%ld.%.3ld ms), fps=%ld (%ld.%.3ld ms)\n",
443 throughput, duration_ms, duration_us,
444 fps, fps_ms, fps_us);
445 par->first_update_done = true;
446 }
447}
448
449
Krzysztof Adamskied208432015-01-22 19:08:58 +0100450static void fbtft_mkdirty(struct fb_info *info, int y, int height)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100451{
452 struct fbtft_par *par = info->par;
453 struct fb_deferred_io *fbdefio = info->fbdefio;
454
455 /* special case, needed ? */
456 if (y == -1) {
457 y = 0;
458 height = info->var.yres - 1;
459 }
460
461 /* Mark display lines/area as dirty */
462 spin_lock(&par->dirty_lock);
463 if (y < par->dirty_lines_start)
464 par->dirty_lines_start = y;
465 if (y + height - 1 > par->dirty_lines_end)
466 par->dirty_lines_end = y + height - 1;
467 spin_unlock(&par->dirty_lock);
468
469 /* Schedule deferred_io to update display (no-op if already on queue)*/
470 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
471}
472
Krzysztof Adamskied208432015-01-22 19:08:58 +0100473static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100474{
475 struct fbtft_par *par = info->par;
476 unsigned dirty_lines_start, dirty_lines_end;
477 struct page *page;
478 unsigned long index;
479 unsigned y_low = 0, y_high = 0;
480 int count = 0;
481
482 spin_lock(&par->dirty_lock);
483 dirty_lines_start = par->dirty_lines_start;
484 dirty_lines_end = par->dirty_lines_end;
485 /* set display line markers as clean */
486 par->dirty_lines_start = par->info->var.yres - 1;
487 par->dirty_lines_end = 0;
488 spin_unlock(&par->dirty_lock);
489
490 /* Mark display lines as dirty */
491 list_for_each_entry(page, pagelist, lru) {
492 count++;
493 index = page->index << PAGE_SHIFT;
494 y_low = index / info->fix.line_length;
495 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
496 fbtft_dev_dbg(DEBUG_DEFERRED_IO, par, info->device,
497 "page->index=%lu y_low=%d y_high=%d\n",
498 page->index, y_low, y_high);
499 if (y_high > info->var.yres - 1)
500 y_high = info->var.yres - 1;
501 if (y_low < dirty_lines_start)
502 dirty_lines_start = y_low;
503 if (y_high > dirty_lines_end)
504 dirty_lines_end = y_high;
505 }
506
507 par->fbtftops.update_display(info->par,
508 dirty_lines_start, dirty_lines_end);
509}
510
511
Krzysztof Adamskied208432015-01-22 19:08:58 +0100512static void fbtft_fb_fillrect(struct fb_info *info,
513 const struct fb_fillrect *rect)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100514{
515 struct fbtft_par *par = info->par;
516
517 fbtft_dev_dbg(DEBUG_FB_FILLRECT, par, info->dev,
518 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
519 __func__, rect->dx, rect->dy, rect->width, rect->height);
520 sys_fillrect(info, rect);
521
522 par->fbtftops.mkdirty(info, rect->dy, rect->height);
523}
524
Krzysztof Adamskied208432015-01-22 19:08:58 +0100525static void fbtft_fb_copyarea(struct fb_info *info,
526 const struct fb_copyarea *area)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100527{
528 struct fbtft_par *par = info->par;
529
530 fbtft_dev_dbg(DEBUG_FB_COPYAREA, par, info->dev,
531 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
532 __func__, area->dx, area->dy, area->width, area->height);
533 sys_copyarea(info, area);
534
535 par->fbtftops.mkdirty(info, area->dy, area->height);
536}
537
Krzysztof Adamskied208432015-01-22 19:08:58 +0100538static void fbtft_fb_imageblit(struct fb_info *info,
539 const struct fb_image *image)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100540{
541 struct fbtft_par *par = info->par;
542
543 fbtft_dev_dbg(DEBUG_FB_IMAGEBLIT, par, info->dev,
544 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
545 __func__, image->dx, image->dy, image->width, image->height);
546 sys_imageblit(info, image);
547
548 par->fbtftops.mkdirty(info, image->dy, image->height);
549}
550
Krzysztof Adamskied208432015-01-22 19:08:58 +0100551static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
552 size_t count, loff_t *ppos)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100553{
554 struct fbtft_par *par = info->par;
555 ssize_t res;
556
557 fbtft_dev_dbg(DEBUG_FB_WRITE, par, info->dev,
558 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
559 res = fb_sys_write(info, buf, count, ppos);
560
561 /* TODO: only mark changed area
562 update all for now */
563 par->fbtftops.mkdirty(info, -1, 0);
564
565 return res;
566}
567
568/* from pxafb.c */
Krzysztof Adamskied208432015-01-22 19:08:58 +0100569static unsigned int chan_to_field(unsigned chan, struct fb_bitfield *bf)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100570{
571 chan &= 0xffff;
572 chan >>= 16 - bf->length;
573 return chan << bf->offset;
574}
575
Krzysztof Adamskied208432015-01-22 19:08:58 +0100576static int fbtft_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
577 unsigned blue, unsigned transp,
578 struct fb_info *info)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100579{
580 struct fbtft_par *par = info->par;
581 unsigned val;
582 int ret = 1;
583
584 fbtft_dev_dbg(DEBUG_FB_SETCOLREG, par, info->dev,
585 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
586 __func__, regno, red, green, blue, transp);
587
588 switch (info->fix.visual) {
589 case FB_VISUAL_TRUECOLOR:
590 if (regno < 16) {
591 u32 *pal = info->pseudo_palette;
592
593 val = chan_to_field(red, &info->var.red);
594 val |= chan_to_field(green, &info->var.green);
595 val |= chan_to_field(blue, &info->var.blue);
596
597 pal[regno] = val;
598 ret = 0;
599 }
600 break;
601
602 }
603 return ret;
604}
605
Krzysztof Adamskied208432015-01-22 19:08:58 +0100606static int fbtft_fb_blank(int blank, struct fb_info *info)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100607{
608 struct fbtft_par *par = info->par;
609 int ret = -EINVAL;
610
611 fbtft_dev_dbg(DEBUG_FB_BLANK, par, info->dev, "%s(blank=%d)\n",
612 __func__, blank);
613
614 if (!par->fbtftops.blank)
615 return ret;
616
617 switch (blank) {
618 case FB_BLANK_POWERDOWN:
619 case FB_BLANK_VSYNC_SUSPEND:
620 case FB_BLANK_HSYNC_SUSPEND:
621 case FB_BLANK_NORMAL:
622 ret = par->fbtftops.blank(par, true);
623 break;
624 case FB_BLANK_UNBLANK:
625 ret = par->fbtftops.blank(par, false);
626 break;
627 }
628 return ret;
629}
630
Krzysztof Adamskied208432015-01-22 19:08:58 +0100631static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +0100632{
633 if (src->write)
634 dst->write = src->write;
635 if (src->read)
636 dst->read = src->read;
637 if (src->write_vmem)
638 dst->write_vmem = src->write_vmem;
639 if (src->write_register)
640 dst->write_register = src->write_register;
641 if (src->set_addr_win)
642 dst->set_addr_win = src->set_addr_win;
643 if (src->reset)
644 dst->reset = src->reset;
645 if (src->mkdirty)
646 dst->mkdirty = src->mkdirty;
647 if (src->update_display)
648 dst->update_display = src->update_display;
649 if (src->init_display)
650 dst->init_display = src->init_display;
651 if (src->blank)
652 dst->blank = src->blank;
653 if (src->request_gpios_match)
654 dst->request_gpios_match = src->request_gpios_match;
655 if (src->request_gpios)
656 dst->request_gpios = src->request_gpios;
657 if (src->verify_gpios)
658 dst->verify_gpios = src->verify_gpios;
659 if (src->register_backlight)
660 dst->register_backlight = src->register_backlight;
661 if (src->unregister_backlight)
662 dst->unregister_backlight = src->unregister_backlight;
663 if (src->set_var)
664 dst->set_var = src->set_var;
665 if (src->set_gamma)
666 dst->set_gamma = src->set_gamma;
667}
668
669/**
670 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
671 *
672 * @display: pointer to structure describing the display
673 * @dev: pointer to the device for this fb, this can be NULL
674 *
675 * Creates a new frame buffer info structure.
676 *
677 * Also creates and populates the following structures:
678 * info->fbops
679 * info->fbdefio
680 * info->pseudo_palette
681 * par->fbtftops
682 * par->txbuf
683 *
684 * Returns the new structure, or NULL if an error occurred.
685 *
686 */
687struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
688 struct device *dev)
689{
690 struct fb_info *info;
691 struct fbtft_par *par;
692 struct fb_ops *fbops = NULL;
693 struct fb_deferred_io *fbdefio = NULL;
694 struct fbtft_platform_data *pdata = dev->platform_data;
695 u8 *vmem = NULL;
696 void *txbuf = NULL;
697 void *buf = NULL;
698 unsigned width;
699 unsigned height;
700 int txbuflen = display->txbuflen;
701 unsigned bpp = display->bpp;
702 unsigned fps = display->fps;
703 int vmem_size, i;
704 int *init_sequence = display->init_sequence;
705 char *gamma = display->gamma;
706 unsigned long *gamma_curves = NULL;
707
708 /* sanity check */
709 if (display->gamma_num * display->gamma_len > FBTFT_GAMMA_MAX_VALUES_TOTAL) {
710 dev_err(dev,
711 "%s: FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
712 __func__, FBTFT_GAMMA_MAX_VALUES_TOTAL);
713 return NULL;
714 }
715
716 /* defaults */
717 if (!fps)
718 fps = 20;
719 if (!bpp)
720 bpp = 16;
721
722 if (!pdata) {
723 dev_err(dev, "platform data is missing\n");
724 return NULL;
725 }
726
727 /* override driver values? */
728 if (pdata->fps)
729 fps = pdata->fps;
730 if (pdata->txbuflen)
731 txbuflen = pdata->txbuflen;
732 if (pdata->display.init_sequence)
733 init_sequence = pdata->display.init_sequence;
734 if (pdata->gamma)
735 gamma = pdata->gamma;
736 if (pdata->display.debug)
737 display->debug = pdata->display.debug;
738 if (pdata->display.backlight)
739 display->backlight = pdata->display.backlight;
740 if (pdata->display.width)
741 display->width = pdata->display.width;
742 if (pdata->display.height)
743 display->height = pdata->display.height;
744 if (pdata->display.buswidth)
745 display->buswidth = pdata->display.buswidth;
746 if (pdata->display.regwidth)
747 display->regwidth = pdata->display.regwidth;
748
749 display->debug |= debug;
750 fbtft_expand_debug_value(&display->debug);
751
752 switch (pdata->rotate) {
753 case 90:
754 case 270:
755 width = display->height;
756 height = display->width;
757 break;
758 default:
759 width = display->width;
760 height = display->height;
761 }
762
763 vmem_size = display->width * display->height * bpp / 8;
764 vmem = vzalloc(vmem_size);
765 if (!vmem)
766 goto alloc_fail;
767
768 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
769 if (!fbops)
770 goto alloc_fail;
771
772 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
773 if (!fbdefio)
774 goto alloc_fail;
775
776 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
777 if (!buf)
778 goto alloc_fail;
779
780 if (display->gamma_num && display->gamma_len) {
781 gamma_curves = devm_kzalloc(dev, display->gamma_num * display->gamma_len * sizeof(gamma_curves[0]),
782 GFP_KERNEL);
783 if (!gamma_curves)
784 goto alloc_fail;
785 }
786
787 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
788 if (!info)
789 goto alloc_fail;
790
791 info->screen_base = (u8 __force __iomem *)vmem;
792 info->fbops = fbops;
793 info->fbdefio = fbdefio;
794
795 fbops->owner = dev->driver->owner;
796 fbops->fb_read = fb_sys_read;
797 fbops->fb_write = fbtft_fb_write;
798 fbops->fb_fillrect = fbtft_fb_fillrect;
799 fbops->fb_copyarea = fbtft_fb_copyarea;
800 fbops->fb_imageblit = fbtft_fb_imageblit;
801 fbops->fb_setcolreg = fbtft_fb_setcolreg;
802 fbops->fb_blank = fbtft_fb_blank;
803
804 fbdefio->delay = HZ/fps;
805 fbdefio->deferred_io = fbtft_deferred_io;
806 fb_deferred_io_init(info);
807
808 strncpy(info->fix.id, dev->driver->name, 16);
809 info->fix.type = FB_TYPE_PACKED_PIXELS;
810 info->fix.visual = FB_VISUAL_TRUECOLOR;
811 info->fix.xpanstep = 0;
812 info->fix.ypanstep = 0;
813 info->fix.ywrapstep = 0;
814 info->fix.line_length = width*bpp/8;
815 info->fix.accel = FB_ACCEL_NONE;
816 info->fix.smem_len = vmem_size;
817
818 info->var.rotate = pdata->rotate;
819 info->var.xres = width;
820 info->var.yres = height;
821 info->var.xres_virtual = info->var.xres;
822 info->var.yres_virtual = info->var.yres;
823 info->var.bits_per_pixel = bpp;
824 info->var.nonstd = 1;
825
826 /* RGB565 */
827 info->var.red.offset = 11;
828 info->var.red.length = 5;
829 info->var.green.offset = 5;
830 info->var.green.length = 6;
831 info->var.blue.offset = 0;
832 info->var.blue.length = 5;
833 info->var.transp.offset = 0;
834 info->var.transp.length = 0;
835
836 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
837
838 par = info->par;
839 par->info = info;
840 par->pdata = dev->platform_data;
841 par->debug = display->debug;
842 par->buf = buf;
843 spin_lock_init(&par->dirty_lock);
844 par->bgr = pdata->bgr;
845 par->startbyte = pdata->startbyte;
846 par->init_sequence = init_sequence;
847 par->gamma.curves = gamma_curves;
848 par->gamma.num_curves = display->gamma_num;
849 par->gamma.num_values = display->gamma_len;
850 mutex_init(&par->gamma.lock);
851 info->pseudo_palette = par->pseudo_palette;
852
853 if (par->gamma.curves && gamma) {
854 if (fbtft_gamma_parse_str(par,
855 par->gamma.curves, gamma, strlen(gamma)))
856 goto alloc_fail;
857 }
858
859 /* Transmit buffer */
860 if (txbuflen == -1)
861 txbuflen = vmem_size + 2; /* add in case startbyte is used */
862
863#ifdef __LITTLE_ENDIAN
864 if ((!txbuflen) && (bpp > 8))
865 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
866#endif
867
868 if (txbuflen > 0) {
869 if (dma) {
870 dev->coherent_dma_mask = ~0;
871 txbuf = dmam_alloc_coherent(dev, txbuflen, &par->txbuf.dma, GFP_DMA);
872 } else {
873 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
874 }
875 if (!txbuf)
876 goto alloc_fail;
877 par->txbuf.buf = txbuf;
878 par->txbuf.len = txbuflen;
879 }
880
881 /* Initialize gpios to disabled */
882 par->gpio.reset = -1;
883 par->gpio.dc = -1;
884 par->gpio.rd = -1;
885 par->gpio.wr = -1;
886 par->gpio.cs = -1;
887 par->gpio.latch = -1;
888 for (i = 0; i < 16; i++) {
889 par->gpio.db[i] = -1;
890 par->gpio.led[i] = -1;
891 par->gpio.aux[i] = -1;
892 }
893
894 /* default fbtft operations */
895 par->fbtftops.write = fbtft_write_spi;
896 par->fbtftops.read = fbtft_read_spi;
897 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
898 par->fbtftops.write_register = fbtft_write_reg8_bus8;
899 par->fbtftops.set_addr_win = fbtft_set_addr_win;
900 par->fbtftops.reset = fbtft_reset;
901 par->fbtftops.mkdirty = fbtft_mkdirty;
902 par->fbtftops.update_display = fbtft_update_display;
903 par->fbtftops.request_gpios = fbtft_request_gpios;
904 if (display->backlight)
905 par->fbtftops.register_backlight = fbtft_register_backlight;
906
907 /* use driver provided functions */
908 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
909
910 return info;
911
912alloc_fail:
913 vfree(vmem);
914
915 return NULL;
916}
917EXPORT_SYMBOL(fbtft_framebuffer_alloc);
918
919/**
920 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
921 *
922 * @info: frame buffer info structure
923 *
924 */
925void fbtft_framebuffer_release(struct fb_info *info)
926{
927 fb_deferred_io_cleanup(info);
928 vfree(info->screen_base);
929 framebuffer_release(info);
930}
931EXPORT_SYMBOL(fbtft_framebuffer_release);
932
933/**
934 * fbtft_register_framebuffer - registers a tft frame buffer device
935 * @fb_info: frame buffer info structure
936 *
937 * Sets SPI driverdata if needed
938 * Requests needed gpios.
939 * Initializes display
940 * Updates display.
941 * Registers a frame buffer device @fb_info.
942 *
943 * Returns negative errno on error, or zero for success.
944 *
945 */
946int fbtft_register_framebuffer(struct fb_info *fb_info)
947{
948 int ret;
949 char text1[50] = "";
950 char text2[50] = "";
951 struct fbtft_par *par = fb_info->par;
952 struct spi_device *spi = par->spi;
953
954 /* sanity checks */
955 if (!par->fbtftops.init_display) {
956 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
957 return -EINVAL;
958 }
959
960 if (spi)
961 spi_set_drvdata(spi, fb_info);
962 if (par->pdev)
963 platform_set_drvdata(par->pdev, fb_info);
964
965 ret = par->fbtftops.request_gpios(par);
966 if (ret < 0)
967 goto reg_fail;
968
969 if (par->fbtftops.verify_gpios) {
970 ret = par->fbtftops.verify_gpios(par);
971 if (ret < 0)
972 goto reg_fail;
973 }
974
975 ret = par->fbtftops.init_display(par);
976 if (ret < 0)
977 goto reg_fail;
978 if (par->fbtftops.set_var) {
979 ret = par->fbtftops.set_var(par);
980 if (ret < 0)
981 goto reg_fail;
982 }
983
984 /* update the entire display */
985 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
986
987 if (par->fbtftops.set_gamma && par->gamma.curves) {
988 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
989 if (ret)
990 goto reg_fail;
991 }
992
993 if (par->fbtftops.register_backlight)
994 par->fbtftops.register_backlight(par);
995
996 ret = register_framebuffer(fb_info);
997 if (ret < 0)
998 goto reg_fail;
999
1000 fbtft_sysfs_init(par);
1001
1002 if (par->txbuf.buf)
Fabio Estevam3fed5ba2015-02-19 10:12:11 -02001003 sprintf(text1, ", %zu KiB %sbuffer memory",
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001004 par->txbuf.len >> 10, par->txbuf.dma ? "DMA " : "");
1005 if (spi)
1006 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
1007 spi->chip_select, spi->max_speed_hz/1000000);
1008 dev_info(fb_info->dev,
1009 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
1010 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
1011 fb_info->fix.smem_len >> 10, text1,
1012 HZ/fb_info->fbdefio->delay, text2);
1013
1014#ifdef CONFIG_FB_BACKLIGHT
1015 /* Turn on backlight if available */
1016 if (fb_info->bl_dev) {
1017 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
1018 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
1019 }
1020#endif
1021
1022 return 0;
1023
1024reg_fail:
1025 if (par->fbtftops.unregister_backlight)
1026 par->fbtftops.unregister_backlight(par);
1027 if (spi)
1028 spi_set_drvdata(spi, NULL);
1029 if (par->pdev)
1030 platform_set_drvdata(par->pdev, NULL);
1031
1032 return ret;
1033}
1034EXPORT_SYMBOL(fbtft_register_framebuffer);
1035
1036/**
1037 * fbtft_unregister_framebuffer - releases a tft frame buffer device
1038 * @fb_info: frame buffer info structure
1039 *
1040 * Frees SPI driverdata if needed
1041 * Frees gpios.
1042 * Unregisters frame buffer device.
1043 *
1044 */
1045int fbtft_unregister_framebuffer(struct fb_info *fb_info)
1046{
1047 struct fbtft_par *par = fb_info->par;
1048 struct spi_device *spi = par->spi;
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001049
1050 if (spi)
1051 spi_set_drvdata(spi, NULL);
1052 if (par->pdev)
1053 platform_set_drvdata(par->pdev, NULL);
1054 if (par->fbtftops.unregister_backlight)
1055 par->fbtftops.unregister_backlight(par);
1056 fbtft_sysfs_exit(par);
Aya Mahfouz11107ff2015-02-27 15:10:30 +02001057 return unregister_framebuffer(fb_info);
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001058}
1059EXPORT_SYMBOL(fbtft_unregister_framebuffer);
1060
1061#ifdef CONFIG_OF
1062/**
1063 * fbtft_init_display_dt() - Device Tree init_display() function
1064 * @par: Driver data
1065 *
1066 * Return: 0 if successful, negative if error
1067 */
1068static int fbtft_init_display_dt(struct fbtft_par *par)
1069{
1070 struct device_node *node = par->info->device->of_node;
1071 struct property *prop;
1072 const __be32 *p;
1073 u32 val;
1074 int buf[64], i, j;
1075 char msg[128];
1076 char str[16];
1077
1078 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1079
1080 if (!node)
1081 return -EINVAL;
1082
1083 prop = of_find_property(node, "init", NULL);
1084 p = of_prop_next_u32(prop, NULL, &val);
1085 if (!p)
1086 return -EINVAL;
1087 while (p) {
1088 if (val & FBTFT_OF_INIT_CMD) {
1089 val &= 0xFFFF;
1090 i = 0;
1091 while (p && !(val & 0xFFFF0000)) {
1092 if (i > 63) {
1093 dev_err(par->info->device,
1094 "%s: Maximum register values exceeded\n",
1095 __func__);
1096 return -EINVAL;
1097 }
1098 buf[i++] = val;
1099 p = of_prop_next_u32(prop, p, &val);
1100 }
1101 /* make debug message */
1102 msg[0] = '\0';
1103 for (j = 0; j < i; j++) {
1104 snprintf(str, 128, " %02X", buf[j]);
1105 strcat(msg, str);
1106 }
1107 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1108 "init: write_register:%s\n", msg);
1109
1110 par->fbtftops.write_register(par, i,
1111 buf[0], buf[1], buf[2], buf[3],
1112 buf[4], buf[5], buf[6], buf[7],
1113 buf[8], buf[9], buf[10], buf[11],
1114 buf[12], buf[13], buf[14], buf[15],
1115 buf[16], buf[17], buf[18], buf[19],
1116 buf[20], buf[21], buf[22], buf[23],
1117 buf[24], buf[25], buf[26], buf[27],
1118 buf[28], buf[29], buf[30], buf[31],
1119 buf[32], buf[33], buf[34], buf[35],
1120 buf[36], buf[37], buf[38], buf[39],
1121 buf[40], buf[41], buf[42], buf[43],
1122 buf[44], buf[45], buf[46], buf[47],
1123 buf[48], buf[49], buf[50], buf[51],
1124 buf[52], buf[53], buf[54], buf[55],
1125 buf[56], buf[57], buf[58], buf[59],
1126 buf[60], buf[61], buf[62], buf[63]);
1127 } else if (val & FBTFT_OF_INIT_DELAY) {
1128 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1129 "init: msleep(%u)\n", val & 0xFFFF);
1130 msleep(val & 0xFFFF);
1131 p = of_prop_next_u32(prop, p, &val);
1132 } else {
1133 dev_err(par->info->device, "illegal init value 0x%X\n",
1134 val);
1135 return -EINVAL;
1136 }
1137 }
1138
1139 return 0;
1140}
1141#endif
1142
1143/**
1144 * fbtft_init_display() - Generic init_display() function
1145 * @par: Driver data
1146 *
1147 * Uses par->init_sequence to do the initialization
1148 *
1149 * Return: 0 if successful, negative if error
1150 */
1151int fbtft_init_display(struct fbtft_par *par)
1152{
1153 int buf[64];
1154 char msg[128];
1155 char str[16];
1156 int i = 0;
1157 int j;
1158
1159 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1160
1161 /* sanity check */
1162 if (!par->init_sequence) {
1163 dev_err(par->info->device,
1164 "error: init_sequence is not set\n");
1165 return -EINVAL;
1166 }
1167
1168 /* make sure stop marker exists */
1169 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1170 if (par->init_sequence[i] == -3)
1171 break;
1172 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1173 dev_err(par->info->device,
1174 "missing stop marker at end of init sequence\n");
1175 return -EINVAL;
1176 }
1177
1178 par->fbtftops.reset(par);
1179 if (par->gpio.cs != -1)
1180 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1181
1182 i = 0;
1183 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1184 if (par->init_sequence[i] == -3) {
1185 /* done */
1186 return 0;
1187 }
1188 if (par->init_sequence[i] >= 0) {
1189 dev_err(par->info->device,
1190 "missing delimiter at position %d\n", i);
1191 return -EINVAL;
1192 }
1193 if (par->init_sequence[i+1] < 0) {
1194 dev_err(par->info->device,
1195 "missing value after delimiter %d at position %d\n",
1196 par->init_sequence[i], i);
1197 return -EINVAL;
1198 }
1199 switch (par->init_sequence[i]) {
1200 case -1:
1201 i++;
1202 /* make debug message */
1203 strcpy(msg, "");
1204 j = i + 1;
1205 while (par->init_sequence[j] >= 0) {
1206 sprintf(str, "0x%02X ", par->init_sequence[j]);
1207 strcat(msg, str);
1208 j++;
1209 }
1210 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1211 "init: write(0x%02X) %s\n",
1212 par->init_sequence[i], msg);
1213
1214 /* Write */
1215 j = 0;
1216 while (par->init_sequence[i] >= 0) {
1217 if (j > 63) {
1218 dev_err(par->info->device,
1219 "%s: Maximum register values exceeded\n",
1220 __func__);
1221 return -EINVAL;
1222 }
1223 buf[j++] = par->init_sequence[i++];
1224 }
1225 par->fbtftops.write_register(par, j,
1226 buf[0], buf[1], buf[2], buf[3],
1227 buf[4], buf[5], buf[6], buf[7],
1228 buf[8], buf[9], buf[10], buf[11],
1229 buf[12], buf[13], buf[14], buf[15],
1230 buf[16], buf[17], buf[18], buf[19],
1231 buf[20], buf[21], buf[22], buf[23],
1232 buf[24], buf[25], buf[26], buf[27],
1233 buf[28], buf[29], buf[30], buf[31],
1234 buf[32], buf[33], buf[34], buf[35],
1235 buf[36], buf[37], buf[38], buf[39],
1236 buf[40], buf[41], buf[42], buf[43],
1237 buf[44], buf[45], buf[46], buf[47],
1238 buf[48], buf[49], buf[50], buf[51],
1239 buf[52], buf[53], buf[54], buf[55],
1240 buf[56], buf[57], buf[58], buf[59],
1241 buf[60], buf[61], buf[62], buf[63]);
1242 break;
1243 case -2:
1244 i++;
1245 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1246 "init: mdelay(%d)\n", par->init_sequence[i]);
1247 mdelay(par->init_sequence[i++]);
1248 break;
1249 default:
1250 dev_err(par->info->device,
1251 "unknown delimiter %d at position %d\n",
1252 par->init_sequence[i], i);
1253 return -EINVAL;
1254 }
1255 }
1256
1257 dev_err(par->info->device,
1258 "%s: something is wrong. Shouldn't get here.\n", __func__);
1259 return -EINVAL;
1260}
1261EXPORT_SYMBOL(fbtft_init_display);
1262
1263/**
1264 * fbtft_verify_gpios() - Generic verify_gpios() function
1265 * @par: Driver data
1266 *
1267 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1268 *
1269 * Return: 0 if successful, negative if error
1270 */
Krzysztof Adamskied208432015-01-22 19:08:58 +01001271static int fbtft_verify_gpios(struct fbtft_par *par)
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001272{
1273 struct fbtft_platform_data *pdata;
1274 int i;
1275
1276 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1277
1278 pdata = par->info->device->platform_data;
Yeliz Taneroglu6ba67a52015-03-02 17:49:59 +02001279 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
Thomas Petazzonic296d5f2014-12-31 10:11:09 +01001280 par->gpio.dc < 0) {
1281 dev_err(par->info->device,
1282 "Missing info about 'dc' gpio. Aborting.\n");
1283 return -EINVAL;
1284 }
1285
1286 if (!par->pdev)
1287 return 0;
1288
1289 if (par->gpio.wr < 0) {
1290 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1291 return -EINVAL;
1292 }
1293 for (i = 0; i < pdata->display.buswidth; i++) {
1294 if (par->gpio.db[i] < 0) {
1295 dev_err(par->info->device,
1296 "Missing 'db%02d' gpio. Aborting.\n", i);
1297 return -EINVAL;
1298 }
1299 }
1300
1301 return 0;
1302}
1303
1304#ifdef CONFIG_OF
1305/* returns 0 if the property is not present */
1306static u32 fbtft_of_value(struct device_node *node, const char *propname)
1307{
1308 int ret;
1309 u32 val = 0;
1310
1311 ret = of_property_read_u32(node, propname, &val);
1312 if (ret == 0)
1313 pr_info("%s: %s = %u\n", __func__, propname, val);
1314
1315 return val;
1316}
1317
1318static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1319{
1320 struct device_node *node = dev->of_node;
1321 struct fbtft_platform_data *pdata;
1322
1323 if (!node) {
1324 dev_err(dev, "Missing platform data or DT\n");
1325 return ERR_PTR(-EINVAL);
1326 }
1327
1328 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1329 if (!pdata)
1330 return ERR_PTR(-ENOMEM);
1331
1332 pdata->display.width = fbtft_of_value(node, "width");
1333 pdata->display.height = fbtft_of_value(node, "height");
1334 pdata->display.regwidth = fbtft_of_value(node, "regwidth");
1335 pdata->display.buswidth = fbtft_of_value(node, "buswidth");
1336 pdata->display.backlight = fbtft_of_value(node, "backlight");
1337 pdata->display.bpp = fbtft_of_value(node, "bpp");
1338 pdata->display.debug = fbtft_of_value(node, "debug");
1339 pdata->rotate = fbtft_of_value(node, "rotate");
1340 pdata->bgr = of_property_read_bool(node, "bgr");
1341 pdata->fps = fbtft_of_value(node, "fps");
1342 pdata->txbuflen = fbtft_of_value(node, "txbuflen");
1343 pdata->startbyte = fbtft_of_value(node, "startbyte");
1344 of_property_read_string(node, "gamma", (const char **)&pdata->gamma);
1345
1346 if (of_find_property(node, "led-gpios", NULL))
1347 pdata->display.backlight = 1;
1348 if (of_find_property(node, "init", NULL))
1349 pdata->display.fbtftops.init_display = fbtft_init_display_dt;
1350 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt;
1351
1352 return pdata;
1353}
1354#else
1355static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1356{
1357 dev_err(dev, "Missing platform data\n");
1358 return ERR_PTR(-EINVAL);
1359}
1360#endif
1361
1362/**
1363 * fbtft_probe_common() - Generic device probe() helper function
1364 * @display: Display properties
1365 * @sdev: SPI device
1366 * @pdev: Platform device
1367 *
1368 * Allocates, initializes and registers a framebuffer
1369 *
1370 * Either @sdev or @pdev should be NULL
1371 *
1372 * Return: 0 if successful, negative if error
1373 */
1374int fbtft_probe_common(struct fbtft_display *display,
1375 struct spi_device *sdev, struct platform_device *pdev)
1376{
1377 struct device *dev;
1378 struct fb_info *info;
1379 struct fbtft_par *par;
1380 struct fbtft_platform_data *pdata;
1381 int ret;
1382
1383 if (sdev)
1384 dev = &sdev->dev;
1385 else
1386 dev = &pdev->dev;
1387
1388 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1389 dev_info(dev, "%s()\n", __func__);
1390
1391 pdata = dev->platform_data;
1392 if (!pdata) {
1393 pdata = fbtft_probe_dt(dev);
1394 if (IS_ERR(pdata))
1395 return PTR_ERR(pdata);
1396 dev->platform_data = pdata;
1397 }
1398
1399 info = fbtft_framebuffer_alloc(display, dev);
1400 if (!info)
1401 return -ENOMEM;
1402
1403 par = info->par;
1404 par->spi = sdev;
1405 par->pdev = pdev;
1406
1407 if (display->buswidth == 0) {
1408 dev_err(dev, "buswidth is not set\n");
1409 return -EINVAL;
1410 }
1411
1412 /* write register functions */
1413 if (display->regwidth == 8 && display->buswidth == 8) {
1414 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1415 } else
1416 if (display->regwidth == 8 && display->buswidth == 9 && par->spi) {
1417 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1418 } else if (display->regwidth == 16 && display->buswidth == 8) {
1419 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1420 } else if (display->regwidth == 16 && display->buswidth == 16) {
1421 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1422 } else {
1423 dev_warn(dev,
1424 "no default functions for regwidth=%d and buswidth=%d\n",
1425 display->regwidth, display->buswidth);
1426 }
1427
1428 /* write_vmem() functions */
1429 if (display->buswidth == 8)
1430 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1431 else if (display->buswidth == 9)
1432 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1433 else if (display->buswidth == 16)
1434 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1435
1436 /* GPIO write() functions */
1437 if (par->pdev) {
1438 if (display->buswidth == 8)
1439 par->fbtftops.write = fbtft_write_gpio8_wr;
1440 else if (display->buswidth == 16)
1441 par->fbtftops.write = fbtft_write_gpio16_wr;
1442 }
1443
1444 /* 9-bit SPI setup */
1445 if (par->spi && display->buswidth == 9) {
1446 par->spi->bits_per_word = 9;
1447 ret = par->spi->master->setup(par->spi);
1448 if (ret) {
1449 dev_warn(&par->spi->dev,
1450 "9-bit SPI not available, emulating using 8-bit.\n");
1451 par->spi->bits_per_word = 8;
1452 ret = par->spi->master->setup(par->spi);
1453 if (ret)
1454 goto out_release;
1455 /* allocate buffer with room for dc bits */
1456 par->extra = devm_kzalloc(par->info->device,
1457 par->txbuf.len + (par->txbuf.len / 8) + 8,
1458 GFP_KERNEL);
1459 if (!par->extra) {
1460 ret = -ENOMEM;
1461 goto out_release;
1462 }
1463 par->fbtftops.write = fbtft_write_spi_emulate_9;
1464 }
1465 }
1466
1467 if (!par->fbtftops.verify_gpios)
1468 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1469
1470 /* make sure we still use the driver provided functions */
1471 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1472
1473 /* use init_sequence if provided */
1474 if (par->init_sequence)
1475 par->fbtftops.init_display = fbtft_init_display;
1476
1477 /* use platform_data provided functions above all */
1478 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1479
1480 ret = fbtft_register_framebuffer(info);
1481 if (ret < 0)
1482 goto out_release;
1483
1484 return 0;
1485
1486out_release:
1487 fbtft_framebuffer_release(info);
1488
1489 return ret;
1490}
1491EXPORT_SYMBOL(fbtft_probe_common);
1492
1493/**
1494 * fbtft_remove_common() - Generic device remove() helper function
1495 * @dev: Device
1496 * @info: Framebuffer
1497 *
1498 * Unregisters and releases the framebuffer
1499 *
1500 * Return: 0 if successful, negative if error
1501 */
1502int fbtft_remove_common(struct device *dev, struct fb_info *info)
1503{
1504 struct fbtft_par *par;
1505
1506 if (!info)
1507 return -EINVAL;
1508 par = info->par;
1509 if (par)
1510 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1511 "%s()\n", __func__);
1512 fbtft_unregister_framebuffer(info);
1513 fbtft_framebuffer_release(info);
1514
1515 return 0;
1516}
1517EXPORT_SYMBOL(fbtft_remove_common);
1518
1519MODULE_LICENSE("GPL");