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