blob: 5bd20e8800bcd6c1fbbf9299abc5e85a790c46f6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/amba-clcd.c
3 *
4 * Copyright (C) 2001 ARM Limited, by David A Rusling
5 * Updated to 2.5, Deep Blue Solutions Ltd.
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive
9 * for more details.
10 *
11 * ARM PrimeCell PL110 Color LCD Controller
12 */
Russell Kinge0a8ba22013-06-27 10:29:32 +010013#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/string.h>
18#include <linux/slab.h>
19#include <linux/delay.h>
20#include <linux/mm.h>
21#include <linux/fb.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
24#include <linux/list.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000025#include <linux/amba/bus.h>
26#include <linux/amba/clcd.h>
Jon Medhurst (Tixy)2b6c53b2014-08-20 13:41:04 +010027#include <linux/bitops.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000028#include <linux/clk.h>
Russell King934848d2009-01-08 09:58:51 +000029#include <linux/hardirq.h>
Pawel Molld10715be2014-06-24 12:55:11 +010030#include <linux/of.h>
31#include <linux/of_address.h>
32#include <linux/of_graph.h>
Linus Walleijc38162b2016-06-16 11:36:13 +020033#include <linux/backlight.h>
Pawel Molld10715be2014-06-24 12:55:11 +010034#include <video/display_timing.h>
35#include <video/of_display_timing.h>
36#include <video/videomode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define to_clcd(info) container_of(info, struct clcd_fb, fb)
39
40/* This is limited to 16 characters when displayed by X startup */
41static const char *clcd_name = "CLCD FB";
42
43/*
44 * Unfortunately, the enable/disable functions may be called either from
45 * process or IRQ context, and we _need_ to delay. This is _not_ good.
46 */
47static inline void clcdfb_sleep(unsigned int ms)
48{
49 if (in_atomic()) {
50 mdelay(ms);
51 } else {
52 msleep(ms);
53 }
54}
55
56static inline void clcdfb_set_start(struct clcd_fb *fb)
57{
58 unsigned long ustart = fb->fb.fix.smem_start;
59 unsigned long lstart;
60
61 ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
62 lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
63
64 writel(ustart, fb->regs + CLCD_UBAS);
65 writel(lstart, fb->regs + CLCD_LBAS);
66}
67
68static void clcdfb_disable(struct clcd_fb *fb)
69{
70 u32 val;
71
72 if (fb->board->disable)
73 fb->board->disable(fb);
74
Linus Walleijc38162b2016-06-16 11:36:13 +020075 if (fb->panel->backlight) {
76 fb->panel->backlight->props.power = FB_BLANK_POWERDOWN;
77 backlight_update_status(fb->panel->backlight);
78 }
79
Russell King3f175222010-02-12 14:32:01 +000080 val = readl(fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (val & CNTL_LCDPWR) {
82 val &= ~CNTL_LCDPWR;
Russell King3f175222010-02-12 14:32:01 +000083 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 clcdfb_sleep(20);
86 }
87 if (val & CNTL_LCDEN) {
88 val &= ~CNTL_LCDEN;
Russell King3f175222010-02-12 14:32:01 +000089 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
92 /*
93 * Disable CLCD clock source.
94 */
Russell King99c796d2010-08-17 22:13:22 +010095 if (fb->clk_enabled) {
96 fb->clk_enabled = false;
97 clk_disable(fb->clk);
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
102{
103 /*
104 * Enable the CLCD clock source.
105 */
Russell King99c796d2010-08-17 22:13:22 +0100106 if (!fb->clk_enabled) {
107 fb->clk_enabled = true;
108 clk_enable(fb->clk);
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /*
112 * Bring up by first enabling..
113 */
114 cntl |= CNTL_LCDEN;
Russell King3f175222010-02-12 14:32:01 +0000115 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 clcdfb_sleep(20);
118
119 /*
120 * and now apply power.
121 */
122 cntl |= CNTL_LCDPWR;
Russell King3f175222010-02-12 14:32:01 +0000123 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /*
Linus Walleijc38162b2016-06-16 11:36:13 +0200126 * Turn on backlight
127 */
128 if (fb->panel->backlight) {
129 fb->panel->backlight->props.power = FB_BLANK_UNBLANK;
130 backlight_update_status(fb->panel->backlight);
131 }
132
133 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * finally, enable the interface.
135 */
136 if (fb->board->enable)
137 fb->board->enable(fb);
138}
139
140static int
141clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
142{
Russell King7b4e9ce2011-01-21 14:03:28 +0000143 u32 caps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int ret = 0;
145
Russell King7b4e9ce2011-01-21 14:03:28 +0000146 if (fb->panel->caps && fb->board->caps)
147 caps = fb->panel->caps & fb->board->caps;
148 else {
149 /* Old way of specifying what can be used */
150 caps = fb->panel->cntl & CNTL_BGR ?
151 CLCD_CAP_BGR : CLCD_CAP_RGB;
152 /* But mask out 444 modes as they weren't supported */
153 caps &= ~CLCD_CAP_444;
154 }
155
156 /* Only TFT panels can do RGB888/BGR888 */
157 if (!(fb->panel->cntl & CNTL_LCDTFT))
158 caps &= ~CLCD_CAP_888;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 memset(&var->transp, 0, sizeof(var->transp));
Russell Kingc43e6f02006-01-26 14:12:06 +0000161
162 var->red.msb_right = 0;
163 var->green.msb_right = 0;
164 var->blue.msb_right = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 switch (var->bits_per_pixel) {
167 case 1:
168 case 2:
169 case 4:
170 case 8:
Russell King7b4e9ce2011-01-21 14:03:28 +0000171 /* If we can't do 5551, reject */
172 caps &= CLCD_CAP_5551;
173 if (!caps) {
174 ret = -EINVAL;
175 break;
176 }
177
Russell Kingc4d12b92005-04-28 10:38:19 +0100178 var->red.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 var->red.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100180 var->green.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 var->green.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100182 var->blue.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 var->blue.offset = 0;
184 break;
Russell King7b4e9ce2011-01-21 14:03:28 +0000185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 case 16:
Russell King7b4e9ce2011-01-21 14:03:28 +0000187 /* If we can't do 444, 5551 or 565, reject */
188 if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
189 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
191 }
Russell King7b4e9ce2011-01-21 14:03:28 +0000192
193 /*
194 * Green length can be 4, 5 or 6 depending whether
195 * we're operating in 444, 5551 or 565 mode.
196 */
197 if (var->green.length == 4 && caps & CLCD_CAP_444)
198 caps &= CLCD_CAP_444;
199 if (var->green.length == 5 && caps & CLCD_CAP_5551)
200 caps &= CLCD_CAP_5551;
201 else if (var->green.length == 6 && caps & CLCD_CAP_565)
202 caps &= CLCD_CAP_565;
203 else {
204 /*
205 * PL110 officially only supports RGB555,
206 * but may be wired up to allow RGB565.
207 */
208 if (caps & CLCD_CAP_565) {
209 var->green.length = 6;
210 caps &= CLCD_CAP_565;
211 } else if (caps & CLCD_CAP_5551) {
212 var->green.length = 5;
213 caps &= CLCD_CAP_5551;
214 } else {
215 var->green.length = 4;
216 caps &= CLCD_CAP_444;
217 }
218 }
219
220 if (var->green.length >= 5) {
221 var->red.length = 5;
222 var->blue.length = 5;
223 } else {
224 var->red.length = 4;
225 var->blue.length = 4;
226 }
227 break;
228 case 32:
229 /* If we can't do 888, reject */
230 caps &= CLCD_CAP_888;
231 if (!caps) {
232 ret = -EINVAL;
233 break;
234 }
235
236 var->red.length = 8;
237 var->green.length = 8;
238 var->blue.length = 8;
239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 default:
241 ret = -EINVAL;
242 break;
243 }
244
Russell Kingc43e6f02006-01-26 14:12:06 +0000245 /*
246 * >= 16bpp displays have separate colour component bitfields
247 * encoded in the pixel data. Calculate their position from
248 * the bitfield length defined above.
249 */
250 if (ret == 0 && var->bits_per_pixel >= 16) {
Russell King7b4e9ce2011-01-21 14:03:28 +0000251 bool bgr, rgb;
252
253 bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
254 rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
255
256 if (!bgr && !rgb)
257 /*
258 * The requested format was not possible, try just
259 * our capabilities. One of BGR or RGB must be
260 * supported.
261 */
262 bgr = caps & CLCD_CAP_BGR;
263
264 if (bgr) {
Russell Kingc43e6f02006-01-26 14:12:06 +0000265 var->blue.offset = 0;
266 var->green.offset = var->blue.offset + var->blue.length;
267 var->red.offset = var->green.offset + var->green.length;
268 } else {
269 var->red.offset = 0;
270 var->green.offset = var->red.offset + var->red.length;
271 var->blue.offset = var->green.offset + var->green.length;
272 }
273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return ret;
276}
277
278static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
279{
280 struct clcd_fb *fb = to_clcd(info);
281 int ret = -EINVAL;
282
283 if (fb->board->check)
284 ret = fb->board->check(fb, var);
Russell King82235e92005-04-28 10:43:52 +0100285
286 if (ret == 0 &&
287 var->xres_virtual * var->bits_per_pixel / 8 *
288 var->yres_virtual > fb->fb.fix.smem_len)
289 ret = -EINVAL;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (ret == 0)
292 ret = clcdfb_set_bitfields(fb, var);
293
294 return ret;
295}
296
297static int clcdfb_set_par(struct fb_info *info)
298{
299 struct clcd_fb *fb = to_clcd(info);
300 struct clcd_regs regs;
301
302 fb->fb.fix.line_length = fb->fb.var.xres_virtual *
303 fb->fb.var.bits_per_pixel / 8;
304
305 if (fb->fb.var.bits_per_pixel <= 8)
306 fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
307 else
308 fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
309
310 fb->board->decode(fb, &regs);
311
312 clcdfb_disable(fb);
313
314 writel(regs.tim0, fb->regs + CLCD_TIM0);
315 writel(regs.tim1, fb->regs + CLCD_TIM1);
316 writel(regs.tim2, fb->regs + CLCD_TIM2);
317 writel(regs.tim3, fb->regs + CLCD_TIM3);
318
319 clcdfb_set_start(fb);
320
321 clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
322
323 fb->clcd_cntl = regs.cntl;
324
325 clcdfb_enable(fb, regs.cntl);
326
327#ifdef DEBUG
Joe Perchesad361c92009-07-06 13:05:40 -0700328 printk(KERN_INFO
329 "CLCD: Registers set to\n"
330 " %08x %08x %08x %08x\n"
331 " %08x %08x %08x %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
333 readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
334 readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
Russell King3f175222010-02-12 14:32:01 +0000335 readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#endif
337
338 return 0;
339}
340
341static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
342{
343 unsigned int mask = (1 << bf->length) - 1;
344
345 return (val >> (16 - bf->length) & mask) << bf->offset;
346}
347
348/*
349 * Set a single color register. The values supplied have a 16 bit
350 * magnitude. Return != 0 for invalid regno.
351 */
352static int
353clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
354 unsigned int blue, unsigned int transp, struct fb_info *info)
355{
356 struct clcd_fb *fb = to_clcd(info);
357
358 if (regno < 16)
359 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
360 convert_bitfield(blue, &fb->fb.var.blue) |
361 convert_bitfield(green, &fb->fb.var.green) |
362 convert_bitfield(red, &fb->fb.var.red);
363
Russell King1ddb8a12005-04-30 22:39:51 +0100364 if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
366 u32 val, mask, newval;
367
368 newval = (red >> 11) & 0x001f;
369 newval |= (green >> 6) & 0x03e0;
370 newval |= (blue >> 1) & 0x7c00;
371
372 /*
373 * 3.2.11: if we're configured for big endian
374 * byte order, the palette entries are swapped.
375 */
376 if (fb->clcd_cntl & CNTL_BEBO)
377 regno ^= 1;
378
379 if (regno & 1) {
380 newval <<= 16;
381 mask = 0x0000ffff;
382 } else {
383 mask = 0xffff0000;
384 }
385
386 val = readl(fb->regs + hw_reg) & mask;
387 writel(val | newval, fb->regs + hw_reg);
388 }
389
390 return regno > 255;
391}
392
393/*
394 * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
395 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
396 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
397 * to e.g. a video mode which doesn't support it. Implements VESA suspend
398 * and powerdown modes on hardware that supports disabling hsync/vsync:
399 * blank_mode == 2: suspend vsync
400 * blank_mode == 3: suspend hsync
401 * blank_mode == 4: powerdown
402 */
403static int clcdfb_blank(int blank_mode, struct fb_info *info)
404{
405 struct clcd_fb *fb = to_clcd(info);
406
407 if (blank_mode != 0) {
408 clcdfb_disable(fb);
409 } else {
410 clcdfb_enable(fb, fb->clcd_cntl);
411 }
412 return 0;
413}
414
Christoph Hellwig216d5262006-01-14 13:21:25 -0800415static int clcdfb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct vm_area_struct *vma)
417{
418 struct clcd_fb *fb = to_clcd(info);
419 unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
420 int ret = -EINVAL;
421
422 len = info->fix.smem_len;
423
424 if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
425 fb->board->mmap)
426 ret = fb->board->mmap(fb, vma);
427
428 return ret;
429}
430
431static struct fb_ops clcdfb_ops = {
432 .owner = THIS_MODULE,
433 .fb_check_var = clcdfb_check_var,
434 .fb_set_par = clcdfb_set_par,
435 .fb_setcolreg = clcdfb_setcolreg,
436 .fb_blank = clcdfb_blank,
437 .fb_fillrect = cfb_fillrect,
438 .fb_copyarea = cfb_copyarea,
439 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 .fb_mmap = clcdfb_mmap,
441};
442
443static int clcdfb_register(struct clcd_fb *fb)
444{
445 int ret;
446
Russell King3f175222010-02-12 14:32:01 +0000447 /*
448 * ARM PL111 always has IENB at 0x1c; it's only PL110
449 * which is reversed on some platforms.
450 */
451 if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
452 fb->off_ienb = CLCD_PL111_IENB;
453 fb->off_cntl = CLCD_PL111_CNTL;
454 } else {
Linus Walleijf36fdac2016-02-23 11:01:38 +0100455 if (of_machine_is_compatible("arm,versatile-ab") ||
456 of_machine_is_compatible("arm,versatile-pb")) {
457 fb->off_ienb = CLCD_PL111_IENB;
458 fb->off_cntl = CLCD_PL111_CNTL;
459 } else {
460 fb->off_ienb = CLCD_PL110_IENB;
461 fb->off_cntl = CLCD_PL110_CNTL;
462 }
Russell King3f175222010-02-12 14:32:01 +0000463 }
464
Russell Kingee569c42008-11-30 17:38:14 +0000465 fb->clk = clk_get(&fb->dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (IS_ERR(fb->clk)) {
467 ret = PTR_ERR(fb->clk);
468 goto out;
469 }
470
Russell King99df4ee2011-09-22 12:34:31 +0100471 ret = clk_prepare(fb->clk);
472 if (ret)
473 goto free_clk;
474
Loïc Minier17e8c4e2011-06-20 20:44:17 +0000475 fb->fb.device = &fb->dev->dev;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 fb->fb.fix.mmio_start = fb->dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +0100478 fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
481 if (!fb->regs) {
482 printk(KERN_ERR "CLCD: unable to remap registers\n");
483 ret = -ENOMEM;
Russell King99df4ee2011-09-22 12:34:31 +0100484 goto clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 fb->fb.fbops = &clcdfb_ops;
488 fb->fb.flags = FBINFO_FLAG_DEFAULT;
489 fb->fb.pseudo_palette = fb->cmap;
490
491 strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
492 fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
493 fb->fb.fix.type_aux = 0;
494 fb->fb.fix.xpanstep = 0;
495 fb->fb.fix.ypanstep = 0;
496 fb->fb.fix.ywrapstep = 0;
497 fb->fb.fix.accel = FB_ACCEL_NONE;
498
499 fb->fb.var.xres = fb->panel->mode.xres;
500 fb->fb.var.yres = fb->panel->mode.yres;
501 fb->fb.var.xres_virtual = fb->panel->mode.xres;
502 fb->fb.var.yres_virtual = fb->panel->mode.yres;
503 fb->fb.var.bits_per_pixel = fb->panel->bpp;
504 fb->fb.var.grayscale = fb->panel->grayscale;
505 fb->fb.var.pixclock = fb->panel->mode.pixclock;
506 fb->fb.var.left_margin = fb->panel->mode.left_margin;
507 fb->fb.var.right_margin = fb->panel->mode.right_margin;
508 fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
509 fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
510 fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
511 fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
512 fb->fb.var.sync = fb->panel->mode.sync;
513 fb->fb.var.vmode = fb->panel->mode.vmode;
514 fb->fb.var.activate = FB_ACTIVATE_NOW;
515 fb->fb.var.nonstd = 0;
516 fb->fb.var.height = fb->panel->height;
517 fb->fb.var.width = fb->panel->width;
518 fb->fb.var.accel_flags = 0;
519
520 fb->fb.monspecs.hfmin = 0;
521 fb->fb.monspecs.hfmax = 100000;
522 fb->fb.monspecs.vfmin = 0;
523 fb->fb.monspecs.vfmax = 400;
524 fb->fb.monspecs.dclkmin = 1000000;
525 fb->fb.monspecs.dclkmax = 100000000;
526
527 /*
528 * Make sure that the bitfields are set appropriately.
529 */
530 clcdfb_set_bitfields(fb, &fb->fb.var);
531
532 /*
533 * Allocate colourmap.
534 */
Andres Salomon909baf02009-03-31 15:25:29 -0700535 ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
536 if (ret)
537 goto unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /*
540 * Ensure interrupts are disabled.
541 */
Russell King3f175222010-02-12 14:32:01 +0000542 writel(0, fb->regs + fb->off_ienb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 fb_set_var(&fb->fb, &fb->fb.var);
545
Russell Kingff643322011-01-19 21:10:24 +0000546 dev_info(&fb->dev->dev, "%s hardware, %s display\n",
547 fb->board->name, fb->panel->mode.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 ret = register_framebuffer(&fb->fb);
550 if (ret == 0)
551 goto out;
552
553 printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
554
Andres Salomon909baf02009-03-31 15:25:29 -0700555 fb_dealloc_cmap(&fb->fb.cmap);
556 unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +0100558 clk_unprep:
559 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 free_clk:
561 clk_put(fb->clk);
562 out:
563 return ret;
564}
565
Pawel Molld10715be2014-06-24 12:55:11 +0100566#ifdef CONFIG_OF
567static int clcdfb_of_get_dpi_panel_mode(struct device_node *node,
Linus Walleijaf298972016-06-16 11:36:14 +0200568 struct clcd_panel *clcd_panel)
Pawel Molld10715be2014-06-24 12:55:11 +0100569{
570 int err;
571 struct display_timing timing;
572 struct videomode video;
573
574 err = of_get_display_timing(node, "panel-timing", &timing);
575 if (err)
576 return err;
577
578 videomode_from_timing(&timing, &video);
579
Linus Walleijaf298972016-06-16 11:36:14 +0200580 err = fb_videomode_from_videomode(&video, &clcd_panel->mode);
Pawel Molld10715be2014-06-24 12:55:11 +0100581 if (err)
582 return err;
583
Linus Walleijaf298972016-06-16 11:36:14 +0200584 /* Set up some inversion flags */
585 if (timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
586 clcd_panel->tim2 |= TIM2_IPC;
587 else if (!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE))
588 /*
589 * To preserve backwards compatibility, the IPC (inverted
590 * pixel clock) flag needs to be set on any display that
591 * doesn't explicitly specify that the pixel clock is
592 * active on the negative or positive edge.
593 */
594 clcd_panel->tim2 |= TIM2_IPC;
595
596 if (timing.flags & DISPLAY_FLAGS_HSYNC_LOW)
597 clcd_panel->tim2 |= TIM2_IHS;
598
599 if (timing.flags & DISPLAY_FLAGS_VSYNC_LOW)
600 clcd_panel->tim2 |= TIM2_IVS;
601
602 if (timing.flags & DISPLAY_FLAGS_DE_LOW)
603 clcd_panel->tim2 |= TIM2_IOE;
604
Pawel Molld10715be2014-06-24 12:55:11 +0100605 return 0;
606}
607
608static int clcdfb_snprintf_mode(char *buf, int size, struct fb_videomode *mode)
609{
610 return snprintf(buf, size, "%ux%u@%u", mode->xres, mode->yres,
611 mode->refresh);
612}
613
Linus Walleijc38162b2016-06-16 11:36:13 +0200614static int clcdfb_of_get_backlight(struct device_node *endpoint,
615 struct clcd_panel *clcd_panel)
616{
617 struct device_node *panel;
618 struct device_node *backlight;
619
620 panel = of_graph_get_remote_port_parent(endpoint);
621 if (!panel)
622 return -ENODEV;
623
624 /* Look up the optional backlight phandle */
625 backlight = of_parse_phandle(panel, "backlight", 0);
626 if (backlight) {
627 clcd_panel->backlight = of_find_backlight_by_node(backlight);
628 of_node_put(backlight);
629
630 if (!clcd_panel->backlight)
631 return -EPROBE_DEFER;
632 }
633 return 0;
634}
635
Pawel Molld10715be2014-06-24 12:55:11 +0100636static int clcdfb_of_get_mode(struct device *dev, struct device_node *endpoint,
Linus Walleijaf298972016-06-16 11:36:14 +0200637 struct clcd_panel *clcd_panel)
Pawel Molld10715be2014-06-24 12:55:11 +0100638{
639 int err;
640 struct device_node *panel;
Linus Walleijaf298972016-06-16 11:36:14 +0200641 struct fb_videomode *mode;
Pawel Molld10715be2014-06-24 12:55:11 +0100642 char *name;
643 int len;
644
645 panel = of_graph_get_remote_port_parent(endpoint);
646 if (!panel)
647 return -ENODEV;
648
649 /* Only directly connected DPI panels supported for now */
650 if (of_device_is_compatible(panel, "panel-dpi"))
Linus Walleijaf298972016-06-16 11:36:14 +0200651 err = clcdfb_of_get_dpi_panel_mode(panel, clcd_panel);
Pawel Molld10715be2014-06-24 12:55:11 +0100652 else
653 err = -ENOENT;
654 if (err)
655 return err;
Linus Walleijaf298972016-06-16 11:36:14 +0200656 mode = &clcd_panel->mode;
Pawel Molld10715be2014-06-24 12:55:11 +0100657
658 len = clcdfb_snprintf_mode(NULL, 0, mode);
659 name = devm_kzalloc(dev, len + 1, GFP_KERNEL);
Kiran Padwal11f09e52015-02-11 15:06:45 +0530660 if (!name)
661 return -ENOMEM;
662
Pawel Molld10715be2014-06-24 12:55:11 +0100663 clcdfb_snprintf_mode(name, len + 1, mode);
664 mode->name = name;
665
666 return 0;
667}
668
669static int clcdfb_of_init_tft_panel(struct clcd_fb *fb, u32 r0, u32 g0, u32 b0)
670{
671 static struct {
672 unsigned int part;
673 u32 r0, g0, b0;
674 u32 caps;
675 } panels[] = {
676 { 0x110, 1, 7, 13, CLCD_CAP_5551 },
677 { 0x110, 0, 8, 16, CLCD_CAP_888 },
678 { 0x111, 4, 14, 20, CLCD_CAP_444 },
679 { 0x111, 3, 11, 19, CLCD_CAP_444 | CLCD_CAP_5551 },
680 { 0x111, 3, 10, 19, CLCD_CAP_444 | CLCD_CAP_5551 |
681 CLCD_CAP_565 },
682 { 0x111, 0, 8, 16, CLCD_CAP_444 | CLCD_CAP_5551 |
683 CLCD_CAP_565 | CLCD_CAP_888 },
684 };
685 int i;
686
Linus Walleijaf298972016-06-16 11:36:14 +0200687 /* Bypass pixel clock divider */
688 fb->panel->tim2 |= TIM2_BCD;
Pawel Molld10715be2014-06-24 12:55:11 +0100689
690 /* TFT display, vert. comp. interrupt at the start of the back porch */
691 fb->panel->cntl |= CNTL_LCDTFT | CNTL_LCDVCOMP(1);
692
693 fb->panel->caps = 0;
694
695 /* Match the setup with known variants */
696 for (i = 0; i < ARRAY_SIZE(panels) && !fb->panel->caps; i++) {
697 if (amba_part(fb->dev) != panels[i].part)
698 continue;
699 if (g0 != panels[i].g0)
700 continue;
701 if (r0 == panels[i].r0 && b0 == panels[i].b0)
Pawel Molle4cf39e2014-09-10 15:15:53 +0100702 fb->panel->caps = panels[i].caps;
Pawel Molld10715be2014-06-24 12:55:11 +0100703 }
704
705 return fb->panel->caps ? 0 : -EINVAL;
706}
707
708static int clcdfb_of_init_display(struct clcd_fb *fb)
709{
710 struct device_node *endpoint;
711 int err;
Jon Medhurst (Tixy)2b6c53b2014-08-20 13:41:04 +0100712 unsigned int bpp;
Pawel Molld10715be2014-06-24 12:55:11 +0100713 u32 max_bandwidth;
714 u32 tft_r0b0g0[3];
715
716 fb->panel = devm_kzalloc(&fb->dev->dev, sizeof(*fb->panel), GFP_KERNEL);
717 if (!fb->panel)
718 return -ENOMEM;
719
720 endpoint = of_graph_get_next_endpoint(fb->dev->dev.of_node, NULL);
721 if (!endpoint)
722 return -ENODEV;
723
Linus Walleijc38162b2016-06-16 11:36:13 +0200724 err = clcdfb_of_get_backlight(endpoint, fb->panel);
725 if (err)
726 return err;
727
Linus Walleijaf298972016-06-16 11:36:14 +0200728 err = clcdfb_of_get_mode(&fb->dev->dev, endpoint, fb->panel);
Pawel Molld10715be2014-06-24 12:55:11 +0100729 if (err)
730 return err;
731
732 err = of_property_read_u32(fb->dev->dev.of_node, "max-memory-bandwidth",
733 &max_bandwidth);
Jon Medhurst (Tixy)2b6c53b2014-08-20 13:41:04 +0100734 if (!err) {
735 /*
736 * max_bandwidth is in bytes per second and pixclock in
737 * pico-seconds, so the maximum allowed bits per pixel is
738 * 8 * max_bandwidth / (PICOS2KHZ(pixclock) * 1000)
739 * Rearrange this calculation to avoid overflow and then ensure
740 * result is a valid format.
741 */
742 bpp = max_bandwidth / (1000 / 8)
743 / PICOS2KHZ(fb->panel->mode.pixclock);
744 bpp = rounddown_pow_of_two(bpp);
745 if (bpp > 32)
746 bpp = 32;
747 } else
748 bpp = 32;
749 fb->panel->bpp = bpp;
Pawel Molld10715be2014-06-24 12:55:11 +0100750
751#ifdef CONFIG_CPU_BIG_ENDIAN
752 fb->panel->cntl |= CNTL_BEBO;
753#endif
754 fb->panel->width = -1;
755 fb->panel->height = -1;
756
757 if (of_property_read_u32_array(endpoint,
758 "arm,pl11x,tft-r0g0b0-pads",
759 tft_r0b0g0, ARRAY_SIZE(tft_r0b0g0)) == 0)
760 return clcdfb_of_init_tft_panel(fb, tft_r0b0g0[0],
761 tft_r0b0g0[1], tft_r0b0g0[2]);
762
763 return -ENOENT;
764}
765
766static int clcdfb_of_vram_setup(struct clcd_fb *fb)
767{
768 int err;
769 struct device_node *memory;
770 u64 size;
771
772 err = clcdfb_of_init_display(fb);
773 if (err)
774 return err;
775
776 memory = of_parse_phandle(fb->dev->dev.of_node, "memory-region", 0);
777 if (!memory)
778 return -ENODEV;
779
780 fb->fb.screen_base = of_iomap(memory, 0);
781 if (!fb->fb.screen_base)
782 return -ENOMEM;
783
784 fb->fb.fix.smem_start = of_translate_address(memory,
785 of_get_address(memory, 0, &size, NULL));
786 fb->fb.fix.smem_len = size;
787
788 return 0;
789}
790
791static int clcdfb_of_vram_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
792{
793 unsigned long off, user_size, kernel_size;
794
795
796 off = vma->vm_pgoff << PAGE_SHIFT;
797 user_size = vma->vm_end - vma->vm_start;
798 kernel_size = fb->fb.fix.smem_len;
799
800 if (off >= kernel_size || user_size > (kernel_size - off))
801 return -ENXIO;
802
803 return remap_pfn_range(vma, vma->vm_start,
804 __phys_to_pfn(fb->fb.fix.smem_start) + vma->vm_pgoff,
805 user_size,
806 pgprot_writecombine(vma->vm_page_prot));
807}
808
809static void clcdfb_of_vram_remove(struct clcd_fb *fb)
810{
811 iounmap(fb->fb.screen_base);
812}
813
814static int clcdfb_of_dma_setup(struct clcd_fb *fb)
815{
816 unsigned long framesize;
817 dma_addr_t dma;
818 int err;
819
820 err = clcdfb_of_init_display(fb);
821 if (err)
822 return err;
823
824 framesize = fb->panel->mode.xres * fb->panel->mode.yres *
825 fb->panel->bpp / 8;
826 fb->fb.screen_base = dma_alloc_coherent(&fb->dev->dev, framesize,
827 &dma, GFP_KERNEL);
828 if (!fb->fb.screen_base)
829 return -ENOMEM;
830
831 fb->fb.fix.smem_start = dma;
832 fb->fb.fix.smem_len = framesize;
833
834 return 0;
835}
836
837static int clcdfb_of_dma_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
838{
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800839 return dma_mmap_wc(&fb->dev->dev, vma, fb->fb.screen_base,
840 fb->fb.fix.smem_start, fb->fb.fix.smem_len);
Pawel Molld10715be2014-06-24 12:55:11 +0100841}
842
843static void clcdfb_of_dma_remove(struct clcd_fb *fb)
844{
845 dma_free_coherent(&fb->dev->dev, fb->fb.fix.smem_len,
846 fb->fb.screen_base, fb->fb.fix.smem_start);
847}
848
849static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
850{
851 struct clcd_board *board = devm_kzalloc(&dev->dev, sizeof(*board),
852 GFP_KERNEL);
853 struct device_node *node = dev->dev.of_node;
854
855 if (!board)
856 return NULL;
857
858 board->name = of_node_full_name(node);
859 board->caps = CLCD_CAP_ALL;
860 board->check = clcdfb_check;
861 board->decode = clcdfb_decode;
862 if (of_find_property(node, "memory-region", NULL)) {
863 board->setup = clcdfb_of_vram_setup;
864 board->mmap = clcdfb_of_vram_mmap;
865 board->remove = clcdfb_of_vram_remove;
866 } else {
867 board->setup = clcdfb_of_dma_setup;
868 board->mmap = clcdfb_of_dma_mmap;
869 board->remove = clcdfb_of_dma_remove;
870 }
871
872 return board;
873}
874#else
Pawel Moll1d5167b2014-08-01 15:43:34 +0100875static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
Pawel Molld10715be2014-06-24 12:55:11 +0100876{
877 return NULL;
878}
879#endif
880
Russell Kingaa25afa2011-02-19 15:55:00 +0000881static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jingoo Han46d2db82013-09-17 14:03:53 +0900883 struct clcd_board *board = dev_get_platdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct clcd_fb *fb;
885 int ret;
886
887 if (!board)
Pawel Molld10715be2014-06-24 12:55:11 +0100888 board = clcdfb_of_get_board(dev);
889
890 if (!board)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EINVAL;
892
Russell Kinge0a8ba22013-06-27 10:29:32 +0100893 ret = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
894 if (ret)
895 goto out;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 ret = amba_request_regions(dev, NULL);
898 if (ret) {
899 printk(KERN_ERR "CLCD: unable to reserve regs region\n");
900 goto out;
901 }
902
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700903 fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (!fb) {
905 printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
906 ret = -ENOMEM;
907 goto free_region;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 fb->dev = dev;
911 fb->board = board;
912
Russell Kingff643322011-01-19 21:10:24 +0000913 dev_info(&fb->dev->dev, "PL%03x rev%u at 0x%08llx\n",
914 amba_part(dev), amba_rev(dev),
915 (unsigned long long)dev->res.start);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 ret = fb->board->setup(fb);
918 if (ret)
919 goto free_fb;
920
921 ret = clcdfb_register(fb);
922 if (ret == 0) {
923 amba_set_drvdata(dev, fb);
924 goto out;
925 }
926
927 fb->board->remove(fb);
928 free_fb:
929 kfree(fb);
930 free_region:
931 amba_release_regions(dev);
932 out:
933 return ret;
934}
935
936static int clcdfb_remove(struct amba_device *dev)
937{
938 struct clcd_fb *fb = amba_get_drvdata(dev);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 clcdfb_disable(fb);
941 unregister_framebuffer(&fb->fb);
Andres Salomon909baf02009-03-31 15:25:29 -0700942 if (fb->fb.cmap.len)
943 fb_dealloc_cmap(&fb->fb.cmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +0100945 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 clk_put(fb->clk);
947
948 fb->board->remove(fb);
949
950 kfree(fb);
951
952 amba_release_regions(dev);
953
954 return 0;
955}
956
957static struct amba_id clcdfb_id_table[] = {
958 {
959 .id = 0x00041110,
Russell Kinge8315562005-11-02 14:40:35 +0000960 .mask = 0x000ffffe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 },
962 { 0, 0 },
963};
964
Dave Martin6054f9b2011-10-05 15:15:23 +0100965MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967static struct amba_driver clcd_driver = {
968 .drv = {
Russell Kinge8315562005-11-02 14:40:35 +0000969 .name = "clcd-pl11x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 },
971 .probe = clcdfb_probe,
972 .remove = clcdfb_remove,
973 .id_table = clcdfb_id_table,
974};
975
Russell King2c250132005-11-08 14:44:15 +0000976static int __init amba_clcdfb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 if (fb_get_options("ambafb", NULL))
979 return -ENODEV;
980
981 return amba_driver_register(&clcd_driver);
982}
983
984module_init(amba_clcdfb_init);
985
986static void __exit amba_clcdfb_exit(void)
987{
988 amba_driver_unregister(&clcd_driver);
989}
990
991module_exit(amba_clcdfb_exit);
992
993MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
994MODULE_LICENSE("GPL");