blob: 2b45c7be4815708bf3a54a48a4a5707ed9264d99 [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 Molld10715b2014-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 Molld10715b2014-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 Walleij1d3f0cb2016-06-16 11:36:17 +020038#include "amba-clcd-nomadik.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define to_clcd(info) container_of(info, struct clcd_fb, fb)
41
42/* This is limited to 16 characters when displayed by X startup */
43static const char *clcd_name = "CLCD FB";
44
45/*
46 * Unfortunately, the enable/disable functions may be called either from
47 * process or IRQ context, and we _need_ to delay. This is _not_ good.
48 */
49static inline void clcdfb_sleep(unsigned int ms)
50{
51 if (in_atomic()) {
52 mdelay(ms);
53 } else {
54 msleep(ms);
55 }
56}
57
58static inline void clcdfb_set_start(struct clcd_fb *fb)
59{
60 unsigned long ustart = fb->fb.fix.smem_start;
61 unsigned long lstart;
62
63 ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
64 lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
65
66 writel(ustart, fb->regs + CLCD_UBAS);
67 writel(lstart, fb->regs + CLCD_LBAS);
68}
69
70static void clcdfb_disable(struct clcd_fb *fb)
71{
72 u32 val;
73
74 if (fb->board->disable)
75 fb->board->disable(fb);
76
Linus Walleijc38162b2016-06-16 11:36:13 +020077 if (fb->panel->backlight) {
78 fb->panel->backlight->props.power = FB_BLANK_POWERDOWN;
79 backlight_update_status(fb->panel->backlight);
80 }
81
Russell King3f17522c2010-02-12 14:32:01 +000082 val = readl(fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (val & CNTL_LCDPWR) {
84 val &= ~CNTL_LCDPWR;
Russell King3f17522c2010-02-12 14:32:01 +000085 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 clcdfb_sleep(20);
88 }
89 if (val & CNTL_LCDEN) {
90 val &= ~CNTL_LCDEN;
Russell King3f17522c2010-02-12 14:32:01 +000091 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
94 /*
95 * Disable CLCD clock source.
96 */
Russell King99c796d2010-08-17 22:13:22 +010097 if (fb->clk_enabled) {
98 fb->clk_enabled = false;
99 clk_disable(fb->clk);
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
104{
105 /*
106 * Enable the CLCD clock source.
107 */
Russell King99c796d2010-08-17 22:13:22 +0100108 if (!fb->clk_enabled) {
109 fb->clk_enabled = true;
110 clk_enable(fb->clk);
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /*
114 * Bring up by first enabling..
115 */
116 cntl |= CNTL_LCDEN;
Russell King3f17522c2010-02-12 14:32:01 +0000117 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 clcdfb_sleep(20);
120
121 /*
122 * and now apply power.
123 */
124 cntl |= CNTL_LCDPWR;
Russell King3f17522c2010-02-12 14:32:01 +0000125 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 /*
Linus Walleijc38162b2016-06-16 11:36:13 +0200128 * Turn on backlight
129 */
130 if (fb->panel->backlight) {
131 fb->panel->backlight->props.power = FB_BLANK_UNBLANK;
132 backlight_update_status(fb->panel->backlight);
133 }
134
135 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * finally, enable the interface.
137 */
138 if (fb->board->enable)
139 fb->board->enable(fb);
140}
141
142static int
143clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
144{
Russell King7b4e9ce2011-01-21 14:03:28 +0000145 u32 caps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int ret = 0;
147
Russell King7b4e9ce2011-01-21 14:03:28 +0000148 if (fb->panel->caps && fb->board->caps)
149 caps = fb->panel->caps & fb->board->caps;
150 else {
151 /* Old way of specifying what can be used */
152 caps = fb->panel->cntl & CNTL_BGR ?
153 CLCD_CAP_BGR : CLCD_CAP_RGB;
154 /* But mask out 444 modes as they weren't supported */
155 caps &= ~CLCD_CAP_444;
156 }
157
158 /* Only TFT panels can do RGB888/BGR888 */
159 if (!(fb->panel->cntl & CNTL_LCDTFT))
160 caps &= ~CLCD_CAP_888;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memset(&var->transp, 0, sizeof(var->transp));
Russell Kingc43e6f02006-01-26 14:12:06 +0000163
164 var->red.msb_right = 0;
165 var->green.msb_right = 0;
166 var->blue.msb_right = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 switch (var->bits_per_pixel) {
169 case 1:
170 case 2:
171 case 4:
172 case 8:
Russell King7b4e9ce2011-01-21 14:03:28 +0000173 /* If we can't do 5551, reject */
174 caps &= CLCD_CAP_5551;
175 if (!caps) {
176 ret = -EINVAL;
177 break;
178 }
179
Russell Kingc4d12b92005-04-28 10:38:19 +0100180 var->red.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 var->red.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100182 var->green.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 var->green.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100184 var->blue.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 var->blue.offset = 0;
186 break;
Russell King7b4e9ce2011-01-21 14:03:28 +0000187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 case 16:
Russell King7b4e9ce2011-01-21 14:03:28 +0000189 /* If we can't do 444, 5551 or 565, reject */
190 if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
191 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193 }
Russell King7b4e9ce2011-01-21 14:03:28 +0000194
195 /*
196 * Green length can be 4, 5 or 6 depending whether
197 * we're operating in 444, 5551 or 565 mode.
198 */
199 if (var->green.length == 4 && caps & CLCD_CAP_444)
200 caps &= CLCD_CAP_444;
201 if (var->green.length == 5 && caps & CLCD_CAP_5551)
202 caps &= CLCD_CAP_5551;
203 else if (var->green.length == 6 && caps & CLCD_CAP_565)
204 caps &= CLCD_CAP_565;
205 else {
206 /*
207 * PL110 officially only supports RGB555,
208 * but may be wired up to allow RGB565.
209 */
210 if (caps & CLCD_CAP_565) {
211 var->green.length = 6;
212 caps &= CLCD_CAP_565;
213 } else if (caps & CLCD_CAP_5551) {
214 var->green.length = 5;
215 caps &= CLCD_CAP_5551;
216 } else {
217 var->green.length = 4;
218 caps &= CLCD_CAP_444;
219 }
220 }
221
222 if (var->green.length >= 5) {
223 var->red.length = 5;
224 var->blue.length = 5;
225 } else {
226 var->red.length = 4;
227 var->blue.length = 4;
228 }
229 break;
Linus Walleij046ad6c2016-06-16 11:36:16 +0200230 case 24:
231 if (fb->vendor->packed_24_bit_pixels) {
232 var->red.length = 8;
233 var->green.length = 8;
234 var->blue.length = 8;
235 } else {
236 ret = -EINVAL;
237 }
238 break;
Russell King7b4e9ce2011-01-21 14:03:28 +0000239 case 32:
240 /* If we can't do 888, reject */
241 caps &= CLCD_CAP_888;
242 if (!caps) {
243 ret = -EINVAL;
244 break;
245 }
246
247 var->red.length = 8;
248 var->green.length = 8;
249 var->blue.length = 8;
250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 default:
252 ret = -EINVAL;
253 break;
254 }
255
Russell Kingc43e6f02006-01-26 14:12:06 +0000256 /*
257 * >= 16bpp displays have separate colour component bitfields
258 * encoded in the pixel data. Calculate their position from
259 * the bitfield length defined above.
260 */
261 if (ret == 0 && var->bits_per_pixel >= 16) {
Russell King7b4e9ce2011-01-21 14:03:28 +0000262 bool bgr, rgb;
263
264 bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
265 rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
266
267 if (!bgr && !rgb)
268 /*
269 * The requested format was not possible, try just
270 * our capabilities. One of BGR or RGB must be
271 * supported.
272 */
273 bgr = caps & CLCD_CAP_BGR;
274
275 if (bgr) {
Russell Kingc43e6f02006-01-26 14:12:06 +0000276 var->blue.offset = 0;
277 var->green.offset = var->blue.offset + var->blue.length;
278 var->red.offset = var->green.offset + var->green.length;
279 } else {
280 var->red.offset = 0;
281 var->green.offset = var->red.offset + var->red.length;
282 var->blue.offset = var->green.offset + var->green.length;
283 }
284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return ret;
287}
288
289static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
290{
291 struct clcd_fb *fb = to_clcd(info);
292 int ret = -EINVAL;
293
294 if (fb->board->check)
295 ret = fb->board->check(fb, var);
Russell King82235e92005-04-28 10:43:52 +0100296
297 if (ret == 0 &&
298 var->xres_virtual * var->bits_per_pixel / 8 *
299 var->yres_virtual > fb->fb.fix.smem_len)
300 ret = -EINVAL;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (ret == 0)
303 ret = clcdfb_set_bitfields(fb, var);
304
305 return ret;
306}
307
308static int clcdfb_set_par(struct fb_info *info)
309{
310 struct clcd_fb *fb = to_clcd(info);
311 struct clcd_regs regs;
312
313 fb->fb.fix.line_length = fb->fb.var.xres_virtual *
314 fb->fb.var.bits_per_pixel / 8;
315
316 if (fb->fb.var.bits_per_pixel <= 8)
317 fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
318 else
319 fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
320
321 fb->board->decode(fb, &regs);
322
323 clcdfb_disable(fb);
324
Linus Walleij046ad6c2016-06-16 11:36:16 +0200325 /* Some variants must be clocked here */
326 if (fb->vendor->clock_timregs && !fb->clk_enabled) {
327 fb->clk_enabled = true;
328 clk_enable(fb->clk);
329 }
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 writel(regs.tim0, fb->regs + CLCD_TIM0);
332 writel(regs.tim1, fb->regs + CLCD_TIM1);
333 writel(regs.tim2, fb->regs + CLCD_TIM2);
334 writel(regs.tim3, fb->regs + CLCD_TIM3);
335
336 clcdfb_set_start(fb);
337
338 clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
339
340 fb->clcd_cntl = regs.cntl;
341
342 clcdfb_enable(fb, regs.cntl);
343
344#ifdef DEBUG
Joe Perchesad361c92009-07-06 13:05:40 -0700345 printk(KERN_INFO
346 "CLCD: Registers set to\n"
347 " %08x %08x %08x %08x\n"
348 " %08x %08x %08x %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
350 readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
351 readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
Russell King3f17522c2010-02-12 14:32:01 +0000352 readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353#endif
354
355 return 0;
356}
357
358static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
359{
360 unsigned int mask = (1 << bf->length) - 1;
361
362 return (val >> (16 - bf->length) & mask) << bf->offset;
363}
364
365/*
366 * Set a single color register. The values supplied have a 16 bit
367 * magnitude. Return != 0 for invalid regno.
368 */
369static int
370clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
371 unsigned int blue, unsigned int transp, struct fb_info *info)
372{
373 struct clcd_fb *fb = to_clcd(info);
374
375 if (regno < 16)
376 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
377 convert_bitfield(blue, &fb->fb.var.blue) |
378 convert_bitfield(green, &fb->fb.var.green) |
379 convert_bitfield(red, &fb->fb.var.red);
380
Russell King1ddb8a12005-04-30 22:39:51 +0100381 if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
383 u32 val, mask, newval;
384
385 newval = (red >> 11) & 0x001f;
386 newval |= (green >> 6) & 0x03e0;
387 newval |= (blue >> 1) & 0x7c00;
388
389 /*
390 * 3.2.11: if we're configured for big endian
391 * byte order, the palette entries are swapped.
392 */
393 if (fb->clcd_cntl & CNTL_BEBO)
394 regno ^= 1;
395
396 if (regno & 1) {
397 newval <<= 16;
398 mask = 0x0000ffff;
399 } else {
400 mask = 0xffff0000;
401 }
402
403 val = readl(fb->regs + hw_reg) & mask;
404 writel(val | newval, fb->regs + hw_reg);
405 }
406
407 return regno > 255;
408}
409
410/*
411 * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
412 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
413 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
414 * to e.g. a video mode which doesn't support it. Implements VESA suspend
415 * and powerdown modes on hardware that supports disabling hsync/vsync:
416 * blank_mode == 2: suspend vsync
417 * blank_mode == 3: suspend hsync
418 * blank_mode == 4: powerdown
419 */
420static int clcdfb_blank(int blank_mode, struct fb_info *info)
421{
422 struct clcd_fb *fb = to_clcd(info);
423
424 if (blank_mode != 0) {
425 clcdfb_disable(fb);
426 } else {
427 clcdfb_enable(fb, fb->clcd_cntl);
428 }
429 return 0;
430}
431
Christoph Hellwig216d5262006-01-14 13:21:25 -0800432static int clcdfb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct vm_area_struct *vma)
434{
435 struct clcd_fb *fb = to_clcd(info);
436 unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
437 int ret = -EINVAL;
438
439 len = info->fix.smem_len;
440
441 if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
442 fb->board->mmap)
443 ret = fb->board->mmap(fb, vma);
444
445 return ret;
446}
447
448static struct fb_ops clcdfb_ops = {
449 .owner = THIS_MODULE,
450 .fb_check_var = clcdfb_check_var,
451 .fb_set_par = clcdfb_set_par,
452 .fb_setcolreg = clcdfb_setcolreg,
453 .fb_blank = clcdfb_blank,
454 .fb_fillrect = cfb_fillrect,
455 .fb_copyarea = cfb_copyarea,
456 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .fb_mmap = clcdfb_mmap,
458};
459
460static int clcdfb_register(struct clcd_fb *fb)
461{
462 int ret;
463
Russell King3f17522c2010-02-12 14:32:01 +0000464 /*
465 * ARM PL111 always has IENB at 0x1c; it's only PL110
466 * which is reversed on some platforms.
467 */
468 if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
469 fb->off_ienb = CLCD_PL111_IENB;
470 fb->off_cntl = CLCD_PL111_CNTL;
471 } else {
Linus Walleijf36fdac2016-02-23 11:01:38 +0100472 if (of_machine_is_compatible("arm,versatile-ab") ||
473 of_machine_is_compatible("arm,versatile-pb")) {
474 fb->off_ienb = CLCD_PL111_IENB;
475 fb->off_cntl = CLCD_PL111_CNTL;
476 } else {
477 fb->off_ienb = CLCD_PL110_IENB;
478 fb->off_cntl = CLCD_PL110_CNTL;
479 }
Russell King3f17522c2010-02-12 14:32:01 +0000480 }
481
Russell Kingee569c42008-11-30 17:38:14 +0000482 fb->clk = clk_get(&fb->dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (IS_ERR(fb->clk)) {
484 ret = PTR_ERR(fb->clk);
485 goto out;
486 }
487
Russell King99df4ee2011-09-22 12:34:31 +0100488 ret = clk_prepare(fb->clk);
489 if (ret)
490 goto free_clk;
491
Loïc Minier17e8c4e2011-06-20 20:44:17 +0000492 fb->fb.device = &fb->dev->dev;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 fb->fb.fix.mmio_start = fb->dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +0100495 fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
498 if (!fb->regs) {
499 printk(KERN_ERR "CLCD: unable to remap registers\n");
500 ret = -ENOMEM;
Russell King99df4ee2011-09-22 12:34:31 +0100501 goto clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
504 fb->fb.fbops = &clcdfb_ops;
505 fb->fb.flags = FBINFO_FLAG_DEFAULT;
506 fb->fb.pseudo_palette = fb->cmap;
507
508 strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
509 fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
510 fb->fb.fix.type_aux = 0;
511 fb->fb.fix.xpanstep = 0;
512 fb->fb.fix.ypanstep = 0;
513 fb->fb.fix.ywrapstep = 0;
514 fb->fb.fix.accel = FB_ACCEL_NONE;
515
516 fb->fb.var.xres = fb->panel->mode.xres;
517 fb->fb.var.yres = fb->panel->mode.yres;
518 fb->fb.var.xres_virtual = fb->panel->mode.xres;
519 fb->fb.var.yres_virtual = fb->panel->mode.yres;
520 fb->fb.var.bits_per_pixel = fb->panel->bpp;
521 fb->fb.var.grayscale = fb->panel->grayscale;
522 fb->fb.var.pixclock = fb->panel->mode.pixclock;
523 fb->fb.var.left_margin = fb->panel->mode.left_margin;
524 fb->fb.var.right_margin = fb->panel->mode.right_margin;
525 fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
526 fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
527 fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
528 fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
529 fb->fb.var.sync = fb->panel->mode.sync;
530 fb->fb.var.vmode = fb->panel->mode.vmode;
531 fb->fb.var.activate = FB_ACTIVATE_NOW;
532 fb->fb.var.nonstd = 0;
533 fb->fb.var.height = fb->panel->height;
534 fb->fb.var.width = fb->panel->width;
535 fb->fb.var.accel_flags = 0;
536
537 fb->fb.monspecs.hfmin = 0;
538 fb->fb.monspecs.hfmax = 100000;
539 fb->fb.monspecs.vfmin = 0;
540 fb->fb.monspecs.vfmax = 400;
541 fb->fb.monspecs.dclkmin = 1000000;
542 fb->fb.monspecs.dclkmax = 100000000;
543
544 /*
545 * Make sure that the bitfields are set appropriately.
546 */
547 clcdfb_set_bitfields(fb, &fb->fb.var);
548
549 /*
550 * Allocate colourmap.
551 */
Andres Salomon909baf02009-03-31 15:25:29 -0700552 ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
553 if (ret)
554 goto unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /*
557 * Ensure interrupts are disabled.
558 */
Russell King3f17522c2010-02-12 14:32:01 +0000559 writel(0, fb->regs + fb->off_ienb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 fb_set_var(&fb->fb, &fb->fb.var);
562
Russell Kingff643322011-01-19 21:10:24 +0000563 dev_info(&fb->dev->dev, "%s hardware, %s display\n",
564 fb->board->name, fb->panel->mode.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 ret = register_framebuffer(&fb->fb);
567 if (ret == 0)
568 goto out;
569
570 printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
571
Andres Salomon909baf02009-03-31 15:25:29 -0700572 fb_dealloc_cmap(&fb->fb.cmap);
573 unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +0100575 clk_unprep:
576 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 free_clk:
578 clk_put(fb->clk);
579 out:
580 return ret;
581}
582
Pawel Molld10715b2014-06-24 12:55:11 +0100583#ifdef CONFIG_OF
584static int clcdfb_of_get_dpi_panel_mode(struct device_node *node,
Linus Walleijaf298972016-06-16 11:36:14 +0200585 struct clcd_panel *clcd_panel)
Pawel Molld10715b2014-06-24 12:55:11 +0100586{
587 int err;
588 struct display_timing timing;
589 struct videomode video;
590
591 err = of_get_display_timing(node, "panel-timing", &timing);
592 if (err)
593 return err;
594
595 videomode_from_timing(&timing, &video);
596
Linus Walleijaf298972016-06-16 11:36:14 +0200597 err = fb_videomode_from_videomode(&video, &clcd_panel->mode);
Pawel Molld10715b2014-06-24 12:55:11 +0100598 if (err)
599 return err;
600
Linus Walleijaf298972016-06-16 11:36:14 +0200601 /* Set up some inversion flags */
602 if (timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
603 clcd_panel->tim2 |= TIM2_IPC;
604 else if (!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE))
605 /*
606 * To preserve backwards compatibility, the IPC (inverted
607 * pixel clock) flag needs to be set on any display that
608 * doesn't explicitly specify that the pixel clock is
609 * active on the negative or positive edge.
610 */
611 clcd_panel->tim2 |= TIM2_IPC;
612
613 if (timing.flags & DISPLAY_FLAGS_HSYNC_LOW)
614 clcd_panel->tim2 |= TIM2_IHS;
615
616 if (timing.flags & DISPLAY_FLAGS_VSYNC_LOW)
617 clcd_panel->tim2 |= TIM2_IVS;
618
619 if (timing.flags & DISPLAY_FLAGS_DE_LOW)
620 clcd_panel->tim2 |= TIM2_IOE;
621
Pawel Molld10715b2014-06-24 12:55:11 +0100622 return 0;
623}
624
625static int clcdfb_snprintf_mode(char *buf, int size, struct fb_videomode *mode)
626{
627 return snprintf(buf, size, "%ux%u@%u", mode->xres, mode->yres,
628 mode->refresh);
629}
630
Linus Walleijc38162b2016-06-16 11:36:13 +0200631static int clcdfb_of_get_backlight(struct device_node *endpoint,
632 struct clcd_panel *clcd_panel)
633{
634 struct device_node *panel;
635 struct device_node *backlight;
636
637 panel = of_graph_get_remote_port_parent(endpoint);
638 if (!panel)
639 return -ENODEV;
640
641 /* Look up the optional backlight phandle */
642 backlight = of_parse_phandle(panel, "backlight", 0);
643 if (backlight) {
644 clcd_panel->backlight = of_find_backlight_by_node(backlight);
645 of_node_put(backlight);
646
647 if (!clcd_panel->backlight)
648 return -EPROBE_DEFER;
649 }
650 return 0;
651}
652
Pawel Molld10715b2014-06-24 12:55:11 +0100653static int clcdfb_of_get_mode(struct device *dev, struct device_node *endpoint,
Linus Walleijaf298972016-06-16 11:36:14 +0200654 struct clcd_panel *clcd_panel)
Pawel Molld10715b2014-06-24 12:55:11 +0100655{
656 int err;
657 struct device_node *panel;
Linus Walleijaf298972016-06-16 11:36:14 +0200658 struct fb_videomode *mode;
Pawel Molld10715b2014-06-24 12:55:11 +0100659 char *name;
660 int len;
661
662 panel = of_graph_get_remote_port_parent(endpoint);
663 if (!panel)
664 return -ENODEV;
665
666 /* Only directly connected DPI panels supported for now */
667 if (of_device_is_compatible(panel, "panel-dpi"))
Linus Walleijaf298972016-06-16 11:36:14 +0200668 err = clcdfb_of_get_dpi_panel_mode(panel, clcd_panel);
Pawel Molld10715b2014-06-24 12:55:11 +0100669 else
670 err = -ENOENT;
671 if (err)
672 return err;
Linus Walleijaf298972016-06-16 11:36:14 +0200673 mode = &clcd_panel->mode;
Pawel Molld10715b2014-06-24 12:55:11 +0100674
675 len = clcdfb_snprintf_mode(NULL, 0, mode);
676 name = devm_kzalloc(dev, len + 1, GFP_KERNEL);
Kiran Padwal11f09e52015-02-11 15:06:45 +0530677 if (!name)
678 return -ENOMEM;
679
Pawel Molld10715b2014-06-24 12:55:11 +0100680 clcdfb_snprintf_mode(name, len + 1, mode);
681 mode->name = name;
682
683 return 0;
684}
685
686static int clcdfb_of_init_tft_panel(struct clcd_fb *fb, u32 r0, u32 g0, u32 b0)
687{
688 static struct {
689 unsigned int part;
690 u32 r0, g0, b0;
691 u32 caps;
692 } panels[] = {
693 { 0x110, 1, 7, 13, CLCD_CAP_5551 },
694 { 0x110, 0, 8, 16, CLCD_CAP_888 },
Linus Walleij03d14c32016-06-16 11:36:15 +0200695 { 0x110, 16, 8, 0, CLCD_CAP_888 },
Pawel Molld10715b2014-06-24 12:55:11 +0100696 { 0x111, 4, 14, 20, CLCD_CAP_444 },
697 { 0x111, 3, 11, 19, CLCD_CAP_444 | CLCD_CAP_5551 },
698 { 0x111, 3, 10, 19, CLCD_CAP_444 | CLCD_CAP_5551 |
699 CLCD_CAP_565 },
700 { 0x111, 0, 8, 16, CLCD_CAP_444 | CLCD_CAP_5551 |
701 CLCD_CAP_565 | CLCD_CAP_888 },
702 };
703 int i;
704
Linus Walleijaf298972016-06-16 11:36:14 +0200705 /* Bypass pixel clock divider */
706 fb->panel->tim2 |= TIM2_BCD;
Pawel Molld10715b2014-06-24 12:55:11 +0100707
708 /* TFT display, vert. comp. interrupt at the start of the back porch */
709 fb->panel->cntl |= CNTL_LCDTFT | CNTL_LCDVCOMP(1);
710
711 fb->panel->caps = 0;
712
713 /* Match the setup with known variants */
714 for (i = 0; i < ARRAY_SIZE(panels) && !fb->panel->caps; i++) {
715 if (amba_part(fb->dev) != panels[i].part)
716 continue;
717 if (g0 != panels[i].g0)
718 continue;
719 if (r0 == panels[i].r0 && b0 == panels[i].b0)
Pawel Molle4cf39e2014-09-10 15:15:53 +0100720 fb->panel->caps = panels[i].caps;
Pawel Molld10715b2014-06-24 12:55:11 +0100721 }
722
Linus Walleij03d14c32016-06-16 11:36:15 +0200723 /*
724 * If we actually physically connected the R lines to B and
725 * vice versa
726 */
727 if (r0 != 0 && b0 == 0)
728 fb->panel->bgr_connection = true;
729
Linus Walleij046ad6c2016-06-16 11:36:16 +0200730 if (fb->panel->caps && fb->vendor->st_bitmux_control) {
731 /*
732 * Set up the special bits for the Nomadik control register
733 * (other platforms tend to do this through an external
734 * register).
735 */
736
737 /* Offset of the highest used color */
738 int maxoff = max3(r0, g0, b0);
739 /* Most significant bit out, highest used bit */
740 int msb = 0;
741
742 if (fb->panel->caps & CLCD_CAP_888) {
743 msb = maxoff + 8 - 1;
744 } else if (fb->panel->caps & CLCD_CAP_565) {
745 msb = maxoff + 5 - 1;
746 fb->panel->cntl |= CNTL_ST_1XBPP_565;
747 } else if (fb->panel->caps & CLCD_CAP_5551) {
748 msb = maxoff + 5 - 1;
749 fb->panel->cntl |= CNTL_ST_1XBPP_5551;
750 } else if (fb->panel->caps & CLCD_CAP_444) {
751 msb = maxoff + 4 - 1;
752 fb->panel->cntl |= CNTL_ST_1XBPP_444;
753 }
754
755 /* Send out as many bits as we need */
756 if (msb > 17)
757 fb->panel->cntl |= CNTL_ST_CDWID_24;
758 else if (msb > 15)
759 fb->panel->cntl |= CNTL_ST_CDWID_18;
760 else if (msb > 11)
761 fb->panel->cntl |= CNTL_ST_CDWID_16;
762 else
763 fb->panel->cntl |= CNTL_ST_CDWID_12;
764 }
765
Pawel Molld10715b2014-06-24 12:55:11 +0100766 return fb->panel->caps ? 0 : -EINVAL;
767}
768
769static int clcdfb_of_init_display(struct clcd_fb *fb)
770{
771 struct device_node *endpoint;
772 int err;
Jon Medhurst (Tixy)2b6c53b2014-08-20 13:41:04 +0100773 unsigned int bpp;
Pawel Molld10715b2014-06-24 12:55:11 +0100774 u32 max_bandwidth;
775 u32 tft_r0b0g0[3];
776
777 fb->panel = devm_kzalloc(&fb->dev->dev, sizeof(*fb->panel), GFP_KERNEL);
778 if (!fb->panel)
779 return -ENOMEM;
780
Linus Walleij046ad6c2016-06-16 11:36:16 +0200781 /*
782 * Fetch the panel endpoint.
783 */
784 if (!endpoint) {
785 endpoint = of_graph_get_next_endpoint(fb->dev->dev.of_node,
786 NULL);
787 if (!endpoint)
788 return -ENODEV;
789 }
790
791 if (fb->vendor->init_panel) {
792 err = fb->vendor->init_panel(fb, endpoint);
793 if (err)
794 return err;
795 }
Pawel Molld10715b2014-06-24 12:55:11 +0100796
Linus Walleijc38162b2016-06-16 11:36:13 +0200797 err = clcdfb_of_get_backlight(endpoint, fb->panel);
798 if (err)
799 return err;
800
Linus Walleijaf298972016-06-16 11:36:14 +0200801 err = clcdfb_of_get_mode(&fb->dev->dev, endpoint, fb->panel);
Pawel Molld10715b2014-06-24 12:55:11 +0100802 if (err)
803 return err;
804
805 err = of_property_read_u32(fb->dev->dev.of_node, "max-memory-bandwidth",
806 &max_bandwidth);
Jon Medhurst (Tixy)2b6c53b2014-08-20 13:41:04 +0100807 if (!err) {
808 /*
809 * max_bandwidth is in bytes per second and pixclock in
810 * pico-seconds, so the maximum allowed bits per pixel is
811 * 8 * max_bandwidth / (PICOS2KHZ(pixclock) * 1000)
812 * Rearrange this calculation to avoid overflow and then ensure
813 * result is a valid format.
814 */
815 bpp = max_bandwidth / (1000 / 8)
816 / PICOS2KHZ(fb->panel->mode.pixclock);
817 bpp = rounddown_pow_of_two(bpp);
818 if (bpp > 32)
819 bpp = 32;
820 } else
821 bpp = 32;
822 fb->panel->bpp = bpp;
Pawel Molld10715b2014-06-24 12:55:11 +0100823
824#ifdef CONFIG_CPU_BIG_ENDIAN
825 fb->panel->cntl |= CNTL_BEBO;
826#endif
827 fb->panel->width = -1;
828 fb->panel->height = -1;
829
830 if (of_property_read_u32_array(endpoint,
831 "arm,pl11x,tft-r0g0b0-pads",
Linus Walleij046ad6c2016-06-16 11:36:16 +0200832 tft_r0b0g0, ARRAY_SIZE(tft_r0b0g0)) != 0)
833 return -ENOENT;
Pawel Molld10715b2014-06-24 12:55:11 +0100834
Linus Walleij046ad6c2016-06-16 11:36:16 +0200835 return clcdfb_of_init_tft_panel(fb, tft_r0b0g0[0],
836 tft_r0b0g0[1], tft_r0b0g0[2]);
Pawel Molld10715b2014-06-24 12:55:11 +0100837}
838
839static int clcdfb_of_vram_setup(struct clcd_fb *fb)
840{
841 int err;
842 struct device_node *memory;
843 u64 size;
844
845 err = clcdfb_of_init_display(fb);
846 if (err)
847 return err;
848
849 memory = of_parse_phandle(fb->dev->dev.of_node, "memory-region", 0);
850 if (!memory)
851 return -ENODEV;
852
853 fb->fb.screen_base = of_iomap(memory, 0);
854 if (!fb->fb.screen_base)
855 return -ENOMEM;
856
857 fb->fb.fix.smem_start = of_translate_address(memory,
858 of_get_address(memory, 0, &size, NULL));
859 fb->fb.fix.smem_len = size;
860
861 return 0;
862}
863
864static int clcdfb_of_vram_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
865{
866 unsigned long off, user_size, kernel_size;
867
868
869 off = vma->vm_pgoff << PAGE_SHIFT;
870 user_size = vma->vm_end - vma->vm_start;
871 kernel_size = fb->fb.fix.smem_len;
872
873 if (off >= kernel_size || user_size > (kernel_size - off))
874 return -ENXIO;
875
876 return remap_pfn_range(vma, vma->vm_start,
877 __phys_to_pfn(fb->fb.fix.smem_start) + vma->vm_pgoff,
878 user_size,
879 pgprot_writecombine(vma->vm_page_prot));
880}
881
882static void clcdfb_of_vram_remove(struct clcd_fb *fb)
883{
884 iounmap(fb->fb.screen_base);
885}
886
887static int clcdfb_of_dma_setup(struct clcd_fb *fb)
888{
889 unsigned long framesize;
890 dma_addr_t dma;
891 int err;
892
893 err = clcdfb_of_init_display(fb);
894 if (err)
895 return err;
896
897 framesize = fb->panel->mode.xres * fb->panel->mode.yres *
898 fb->panel->bpp / 8;
899 fb->fb.screen_base = dma_alloc_coherent(&fb->dev->dev, framesize,
900 &dma, GFP_KERNEL);
901 if (!fb->fb.screen_base)
902 return -ENOMEM;
903
904 fb->fb.fix.smem_start = dma;
905 fb->fb.fix.smem_len = framesize;
906
907 return 0;
908}
909
910static int clcdfb_of_dma_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
911{
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800912 return dma_mmap_wc(&fb->dev->dev, vma, fb->fb.screen_base,
913 fb->fb.fix.smem_start, fb->fb.fix.smem_len);
Pawel Molld10715b2014-06-24 12:55:11 +0100914}
915
916static void clcdfb_of_dma_remove(struct clcd_fb *fb)
917{
918 dma_free_coherent(&fb->dev->dev, fb->fb.fix.smem_len,
919 fb->fb.screen_base, fb->fb.fix.smem_start);
920}
921
922static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
923{
924 struct clcd_board *board = devm_kzalloc(&dev->dev, sizeof(*board),
925 GFP_KERNEL);
926 struct device_node *node = dev->dev.of_node;
927
928 if (!board)
929 return NULL;
930
931 board->name = of_node_full_name(node);
932 board->caps = CLCD_CAP_ALL;
933 board->check = clcdfb_check;
934 board->decode = clcdfb_decode;
935 if (of_find_property(node, "memory-region", NULL)) {
936 board->setup = clcdfb_of_vram_setup;
937 board->mmap = clcdfb_of_vram_mmap;
938 board->remove = clcdfb_of_vram_remove;
939 } else {
940 board->setup = clcdfb_of_dma_setup;
941 board->mmap = clcdfb_of_dma_mmap;
942 board->remove = clcdfb_of_dma_remove;
943 }
944
945 return board;
946}
947#else
Pawel Moll1d5167b2014-08-01 15:43:34 +0100948static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
Pawel Molld10715b2014-06-24 12:55:11 +0100949{
950 return NULL;
951}
952#endif
953
Russell Kingaa25afa2011-02-19 15:55:00 +0000954static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Jingoo Han46d2db82013-09-17 14:03:53 +0900956 struct clcd_board *board = dev_get_platdata(&dev->dev);
Linus Walleij046ad6c2016-06-16 11:36:16 +0200957 struct clcd_vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct clcd_fb *fb;
959 int ret;
960
961 if (!board)
Pawel Molld10715b2014-06-24 12:55:11 +0100962 board = clcdfb_of_get_board(dev);
963
964 if (!board)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EINVAL;
966
Linus Walleij046ad6c2016-06-16 11:36:16 +0200967 if (vendor->init_board) {
968 ret = vendor->init_board(dev, board);
969 if (ret)
970 return ret;
971 }
972
Russell Kinge0a8ba22013-06-27 10:29:32 +0100973 ret = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
974 if (ret)
975 goto out;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 ret = amba_request_regions(dev, NULL);
978 if (ret) {
979 printk(KERN_ERR "CLCD: unable to reserve regs region\n");
980 goto out;
981 }
982
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700983 fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (!fb) {
985 printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
986 ret = -ENOMEM;
987 goto free_region;
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 fb->dev = dev;
Linus Walleij046ad6c2016-06-16 11:36:16 +0200991 fb->vendor = vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 fb->board = board;
993
Linus Walleij046ad6c2016-06-16 11:36:16 +0200994 dev_info(&fb->dev->dev, "PL%03x designer %02x rev%u at 0x%08llx\n",
995 amba_part(dev), amba_manf(dev), amba_rev(dev),
Russell Kingff643322011-01-19 21:10:24 +0000996 (unsigned long long)dev->res.start);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 ret = fb->board->setup(fb);
999 if (ret)
1000 goto free_fb;
1001
Linus Walleij1d3f0cb2016-06-16 11:36:17 +02001002 ret = clcdfb_register(fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (ret == 0) {
1004 amba_set_drvdata(dev, fb);
1005 goto out;
1006 }
1007
1008 fb->board->remove(fb);
1009 free_fb:
1010 kfree(fb);
1011 free_region:
1012 amba_release_regions(dev);
1013 out:
1014 return ret;
1015}
1016
1017static int clcdfb_remove(struct amba_device *dev)
1018{
1019 struct clcd_fb *fb = amba_get_drvdata(dev);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 clcdfb_disable(fb);
1022 unregister_framebuffer(&fb->fb);
Andres Salomon909baf02009-03-31 15:25:29 -07001023 if (fb->fb.cmap.len)
1024 fb_dealloc_cmap(&fb->fb.cmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +01001026 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 clk_put(fb->clk);
1028
1029 fb->board->remove(fb);
1030
1031 kfree(fb);
1032
1033 amba_release_regions(dev);
1034
1035 return 0;
1036}
1037
Linus Walleij046ad6c2016-06-16 11:36:16 +02001038static struct clcd_vendor_data vendor_arm = {
1039 /* No special business */
1040};
1041
1042static struct clcd_vendor_data vendor_nomadik = {
1043 .clock_timregs = true,
1044 .packed_24_bit_pixels = true,
1045 .st_bitmux_control = true,
Linus Walleij1d3f0cb2016-06-16 11:36:17 +02001046 .init_board = nomadik_clcd_init_board,
1047 .init_panel = nomadik_clcd_init_panel,
Linus Walleij046ad6c2016-06-16 11:36:16 +02001048};
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050static struct amba_id clcdfb_id_table[] = {
1051 {
1052 .id = 0x00041110,
Russell Kinge8315562005-11-02 14:40:35 +00001053 .mask = 0x000ffffe,
Linus Walleij046ad6c2016-06-16 11:36:16 +02001054 .data = &vendor_arm,
1055 },
1056 /* ST Electronics Nomadik variant */
1057 {
1058 .id = 0x00180110,
1059 .mask = 0x00fffffe,
1060 .data = &vendor_nomadik,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 },
1062 { 0, 0 },
1063};
1064
Dave Martin6054f9b2011-10-05 15:15:23 +01001065MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static struct amba_driver clcd_driver = {
1068 .drv = {
Russell Kinge8315562005-11-02 14:40:35 +00001069 .name = "clcd-pl11x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 },
1071 .probe = clcdfb_probe,
1072 .remove = clcdfb_remove,
1073 .id_table = clcdfb_id_table,
1074};
1075
Russell King2c250132005-11-08 14:44:15 +00001076static int __init amba_clcdfb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 if (fb_get_options("ambafb", NULL))
1079 return -ENODEV;
1080
1081 return amba_driver_register(&clcd_driver);
1082}
1083
1084module_init(amba_clcdfb_init);
1085
1086static void __exit amba_clcdfb_exit(void)
1087{
1088 amba_driver_unregister(&clcd_driver);
1089}
1090
1091module_exit(amba_clcdfb_exit);
1092
1093MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
1094MODULE_LICENSE("GPL");