1 /*
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 */
13 #include <linux/amba/bus.h>
14 #include <linux/amba/clcd.h>
15 #include <linux/backlight.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/fb.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/list.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25 #include <linux/of_address.h>
26 #include <linux/of_graph.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <video/display_timing.h>
30 #include <video/of_display_timing.h>
31 #include <video/videomode.h>
32
33 #define to_clcd(info) container_of(info, struct clcd_fb, fb)
34
35 /* This is limited to 16 characters when displayed by X startup */
36 static const char *clcd_name = "CLCD FB";
37
clcdfb_set_start(struct clcd_fb * fb)38 static inline void clcdfb_set_start(struct clcd_fb *fb)
39 {
40 unsigned long ustart = fb->fb.fix.smem_start;
41 unsigned long lstart;
42
43 ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
44 lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
45
46 writel(ustart, fb->regs + CLCD_UBAS);
47 writel(lstart, fb->regs + CLCD_LBAS);
48 }
49
clcdfb_disable(struct clcd_fb * fb)50 static void clcdfb_disable(struct clcd_fb *fb)
51 {
52 u32 val;
53
54 if (fb->board->disable)
55 fb->board->disable(fb);
56
57 if (fb->panel->backlight) {
58 fb->panel->backlight->props.power = FB_BLANK_POWERDOWN;
59 backlight_update_status(fb->panel->backlight);
60 }
61
62 val = readl(fb->regs + fb->off_cntl);
63 if (val & CNTL_LCDPWR) {
64 val &= ~CNTL_LCDPWR;
65 writel(val, fb->regs + fb->off_cntl);
66
67 msleep(20);
68 }
69 if (val & CNTL_LCDEN) {
70 val &= ~CNTL_LCDEN;
71 writel(val, fb->regs + fb->off_cntl);
72 }
73
74 /*
75 * Disable CLCD clock source.
76 */
77 if (fb->clk_enabled) {
78 fb->clk_enabled = false;
79 clk_disable(fb->clk);
80 }
81 }
82
clcdfb_enable(struct clcd_fb * fb,u32 cntl)83 static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
84 {
85 /*
86 * Enable the CLCD clock source.
87 */
88 if (!fb->clk_enabled) {
89 fb->clk_enabled = true;
90 clk_enable(fb->clk);
91 }
92
93 /*
94 * Bring up by first enabling..
95 */
96 cntl |= CNTL_LCDEN;
97 writel(cntl, fb->regs + fb->off_cntl);
98
99 msleep(20);
100
101 /*
102 * and now apply power.
103 */
104 cntl |= CNTL_LCDPWR;
105 writel(cntl, fb->regs + fb->off_cntl);
106
107 /*
108 * Turn on backlight
109 */
110 if (fb->panel->backlight) {
111 fb->panel->backlight->props.power = FB_BLANK_UNBLANK;
112 backlight_update_status(fb->panel->backlight);
113 }
114
115 /*
116 * finally, enable the interface.
117 */
118 if (fb->board->enable)
119 fb->board->enable(fb);
120 }
121
122 static int
clcdfb_set_bitfields(struct clcd_fb * fb,struct fb_var_screeninfo * var)123 clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
124 {
125 u32 caps;
126 int ret = 0;
127
128 if (fb->panel->caps && fb->board->caps)
129 caps = fb->panel->caps & fb->board->caps;
130 else {
131 /* Old way of specifying what can be used */
132 caps = fb->panel->cntl & CNTL_BGR ?
133 CLCD_CAP_BGR : CLCD_CAP_RGB;
134 /* But mask out 444 modes as they weren't supported */
135 caps &= ~CLCD_CAP_444;
136 }
137
138 /* Only TFT panels can do RGB888/BGR888 */
139 if (!(fb->panel->cntl & CNTL_LCDTFT))
140 caps &= ~CLCD_CAP_888;
141
142 memset(&var->transp, 0, sizeof(var->transp));
143
144 var->red.msb_right = 0;
145 var->green.msb_right = 0;
146 var->blue.msb_right = 0;
147
148 switch (var->bits_per_pixel) {
149 case 1:
150 case 2:
151 case 4:
152 case 8:
153 /* If we can't do 5551, reject */
154 caps &= CLCD_CAP_5551;
155 if (!caps) {
156 ret = -EINVAL;
157 break;
158 }
159
160 var->red.length = var->bits_per_pixel;
161 var->red.offset = 0;
162 var->green.length = var->bits_per_pixel;
163 var->green.offset = 0;
164 var->blue.length = var->bits_per_pixel;
165 var->blue.offset = 0;
166 break;
167
168 case 16:
169 /* If we can't do 444, 5551 or 565, reject */
170 if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
171 ret = -EINVAL;
172 break;
173 }
174
175 /*
176 * Green length can be 4, 5 or 6 depending whether
177 * we're operating in 444, 5551 or 565 mode.
178 */
179 if (var->green.length == 4 && caps & CLCD_CAP_444)
180 caps &= CLCD_CAP_444;
181 if (var->green.length == 5 && caps & CLCD_CAP_5551)
182 caps &= CLCD_CAP_5551;
183 else if (var->green.length == 6 && caps & CLCD_CAP_565)
184 caps &= CLCD_CAP_565;
185 else {
186 /*
187 * PL110 officially only supports RGB555,
188 * but may be wired up to allow RGB565.
189 */
190 if (caps & CLCD_CAP_565) {
191 var->green.length = 6;
192 caps &= CLCD_CAP_565;
193 } else if (caps & CLCD_CAP_5551) {
194 var->green.length = 5;
195 caps &= CLCD_CAP_5551;
196 } else {
197 var->green.length = 4;
198 caps &= CLCD_CAP_444;
199 }
200 }
201
202 if (var->green.length >= 5) {
203 var->red.length = 5;
204 var->blue.length = 5;
205 } else {
206 var->red.length = 4;
207 var->blue.length = 4;
208 }
209 break;
210 case 32:
211 /* If we can't do 888, reject */
212 caps &= CLCD_CAP_888;
213 if (!caps) {
214 ret = -EINVAL;
215 break;
216 }
217
218 var->red.length = 8;
219 var->green.length = 8;
220 var->blue.length = 8;
221 break;
222 default:
223 ret = -EINVAL;
224 break;
225 }
226
227 /*
228 * >= 16bpp displays have separate colour component bitfields
229 * encoded in the pixel data. Calculate their position from
230 * the bitfield length defined above.
231 */
232 if (ret == 0 && var->bits_per_pixel >= 16) {
233 bool bgr, rgb;
234
235 bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
236 rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
237
238 if (!bgr && !rgb)
239 /*
240 * The requested format was not possible, try just
241 * our capabilities. One of BGR or RGB must be
242 * supported.
243 */
244 bgr = caps & CLCD_CAP_BGR;
245
246 if (bgr) {
247 var->blue.offset = 0;
248 var->green.offset = var->blue.offset + var->blue.length;
249 var->red.offset = var->green.offset + var->green.length;
250 } else {
251 var->red.offset = 0;
252 var->green.offset = var->red.offset + var->red.length;
253 var->blue.offset = var->green.offset + var->green.length;
254 }
255 }
256
257 return ret;
258 }
259
clcdfb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)260 static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
261 {
262 struct clcd_fb *fb = to_clcd(info);
263 int ret = -EINVAL;
264
265 if (fb->board->check)
266 ret = fb->board->check(fb, var);
267
268 if (ret == 0 &&
269 var->xres_virtual * var->bits_per_pixel / 8 *
270 var->yres_virtual > fb->fb.fix.smem_len)
271 ret = -EINVAL;
272
273 if (ret == 0)
274 ret = clcdfb_set_bitfields(fb, var);
275
276 return ret;
277 }
278
clcdfb_set_par(struct fb_info * info)279 static int clcdfb_set_par(struct fb_info *info)
280 {
281 struct clcd_fb *fb = to_clcd(info);
282 struct clcd_regs regs;
283
284 fb->fb.fix.line_length = fb->fb.var.xres_virtual *
285 fb->fb.var.bits_per_pixel / 8;
286
287 if (fb->fb.var.bits_per_pixel <= 8)
288 fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
289 else
290 fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
291
292 fb->board->decode(fb, ®s);
293
294 clcdfb_disable(fb);
295
296 writel(regs.tim0, fb->regs + CLCD_TIM0);
297 writel(regs.tim1, fb->regs + CLCD_TIM1);
298 writel(regs.tim2, fb->regs + CLCD_TIM2);
299 writel(regs.tim3, fb->regs + CLCD_TIM3);
300
301 clcdfb_set_start(fb);
302
303 clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
304
305 fb->clcd_cntl = regs.cntl;
306
307 clcdfb_enable(fb, regs.cntl);
308
309 #ifdef DEBUG
310 printk(KERN_INFO
311 "CLCD: Registers set to\n"
312 " %08x %08x %08x %08x\n"
313 " %08x %08x %08x %08x\n",
314 readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
315 readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
316 readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
317 readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
318 #endif
319
320 return 0;
321 }
322
convert_bitfield(int val,struct fb_bitfield * bf)323 static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
324 {
325 unsigned int mask = (1 << bf->length) - 1;
326
327 return (val >> (16 - bf->length) & mask) << bf->offset;
328 }
329
330 /*
331 * Set a single color register. The values supplied have a 16 bit
332 * magnitude. Return != 0 for invalid regno.
333 */
334 static int
clcdfb_setcolreg(unsigned int regno,unsigned int red,unsigned int green,unsigned int blue,unsigned int transp,struct fb_info * info)335 clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
336 unsigned int blue, unsigned int transp, struct fb_info *info)
337 {
338 struct clcd_fb *fb = to_clcd(info);
339
340 if (regno < 16)
341 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
342 convert_bitfield(blue, &fb->fb.var.blue) |
343 convert_bitfield(green, &fb->fb.var.green) |
344 convert_bitfield(red, &fb->fb.var.red);
345
346 if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
347 int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
348 u32 val, mask, newval;
349
350 newval = (red >> 11) & 0x001f;
351 newval |= (green >> 6) & 0x03e0;
352 newval |= (blue >> 1) & 0x7c00;
353
354 /*
355 * 3.2.11: if we're configured for big endian
356 * byte order, the palette entries are swapped.
357 */
358 if (fb->clcd_cntl & CNTL_BEBO)
359 regno ^= 1;
360
361 if (regno & 1) {
362 newval <<= 16;
363 mask = 0x0000ffff;
364 } else {
365 mask = 0xffff0000;
366 }
367
368 val = readl(fb->regs + hw_reg) & mask;
369 writel(val | newval, fb->regs + hw_reg);
370 }
371
372 return regno > 255;
373 }
374
375 /*
376 * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
377 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
378 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
379 * to e.g. a video mode which doesn't support it. Implements VESA suspend
380 * and powerdown modes on hardware that supports disabling hsync/vsync:
381 * blank_mode == 2: suspend vsync
382 * blank_mode == 3: suspend hsync
383 * blank_mode == 4: powerdown
384 */
clcdfb_blank(int blank_mode,struct fb_info * info)385 static int clcdfb_blank(int blank_mode, struct fb_info *info)
386 {
387 struct clcd_fb *fb = to_clcd(info);
388
389 if (blank_mode != 0) {
390 clcdfb_disable(fb);
391 } else {
392 clcdfb_enable(fb, fb->clcd_cntl);
393 }
394 return 0;
395 }
396
clcdfb_mmap(struct fb_info * info,struct vm_area_struct * vma)397 static int clcdfb_mmap(struct fb_info *info,
398 struct vm_area_struct *vma)
399 {
400 struct clcd_fb *fb = to_clcd(info);
401 unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
402 int ret = -EINVAL;
403
404 len = info->fix.smem_len;
405
406 if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
407 fb->board->mmap)
408 ret = fb->board->mmap(fb, vma);
409
410 return ret;
411 }
412
413 static const struct fb_ops clcdfb_ops = {
414 .owner = THIS_MODULE,
415 .fb_check_var = clcdfb_check_var,
416 .fb_set_par = clcdfb_set_par,
417 .fb_setcolreg = clcdfb_setcolreg,
418 .fb_blank = clcdfb_blank,
419 .fb_fillrect = cfb_fillrect,
420 .fb_copyarea = cfb_copyarea,
421 .fb_imageblit = cfb_imageblit,
422 .fb_mmap = clcdfb_mmap,
423 };
424
clcdfb_register(struct clcd_fb * fb)425 static int clcdfb_register(struct clcd_fb *fb)
426 {
427 int ret;
428
429 /*
430 * ARM PL111 always has IENB at 0x1c; it's only PL110
431 * which is reversed on some platforms.
432 */
433 if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
434 fb->off_ienb = CLCD_PL111_IENB;
435 fb->off_cntl = CLCD_PL111_CNTL;
436 } else {
437 fb->off_ienb = CLCD_PL110_IENB;
438 fb->off_cntl = CLCD_PL110_CNTL;
439 }
440
441 fb->clk = clk_get(&fb->dev->dev, NULL);
442 if (IS_ERR(fb->clk)) {
443 ret = PTR_ERR(fb->clk);
444 goto out;
445 }
446
447 ret = clk_prepare(fb->clk);
448 if (ret)
449 goto free_clk;
450
451 fb->fb.device = &fb->dev->dev;
452
453 fb->fb.fix.mmio_start = fb->dev->res.start;
454 fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
455
456 fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
457 if (!fb->regs) {
458 printk(KERN_ERR "CLCD: unable to remap registers\n");
459 ret = -ENOMEM;
460 goto clk_unprep;
461 }
462
463 fb->fb.fbops = &clcdfb_ops;
464 fb->fb.flags = FBINFO_FLAG_DEFAULT;
465 fb->fb.pseudo_palette = fb->cmap;
466
467 strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
468 fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
469 fb->fb.fix.type_aux = 0;
470 fb->fb.fix.xpanstep = 0;
471 fb->fb.fix.ypanstep = 0;
472 fb->fb.fix.ywrapstep = 0;
473 fb->fb.fix.accel = FB_ACCEL_NONE;
474
475 fb->fb.var.xres = fb->panel->mode.xres;
476 fb->fb.var.yres = fb->panel->mode.yres;
477 fb->fb.var.xres_virtual = fb->panel->mode.xres;
478 fb->fb.var.yres_virtual = fb->panel->mode.yres;
479 fb->fb.var.bits_per_pixel = fb->panel->bpp;
480 fb->fb.var.grayscale = fb->panel->grayscale;
481 fb->fb.var.pixclock = fb->panel->mode.pixclock;
482 fb->fb.var.left_margin = fb->panel->mode.left_margin;
483 fb->fb.var.right_margin = fb->panel->mode.right_margin;
484 fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
485 fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
486 fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
487 fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
488 fb->fb.var.sync = fb->panel->mode.sync;
489 fb->fb.var.vmode = fb->panel->mode.vmode;
490 fb->fb.var.activate = FB_ACTIVATE_NOW;
491 fb->fb.var.nonstd = 0;
492 fb->fb.var.height = fb->panel->height;
493 fb->fb.var.width = fb->panel->width;
494 fb->fb.var.accel_flags = 0;
495
496 fb->fb.monspecs.hfmin = 0;
497 fb->fb.monspecs.hfmax = 100000;
498 fb->fb.monspecs.vfmin = 0;
499 fb->fb.monspecs.vfmax = 400;
500 fb->fb.monspecs.dclkmin = 1000000;
501 fb->fb.monspecs.dclkmax = 100000000;
502
503 /*
504 * Make sure that the bitfields are set appropriately.
505 */
506 clcdfb_set_bitfields(fb, &fb->fb.var);
507
508 /*
509 * Allocate colourmap.
510 */
511 ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
512 if (ret)
513 goto unmap;
514
515 /*
516 * Ensure interrupts are disabled.
517 */
518 writel(0, fb->regs + fb->off_ienb);
519
520 fb_set_var(&fb->fb, &fb->fb.var);
521
522 dev_info(&fb->dev->dev, "%s hardware, %s display\n",
523 fb->board->name, fb->panel->mode.name);
524
525 ret = register_framebuffer(&fb->fb);
526 if (ret == 0)
527 goto out;
528
529 printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
530
531 fb_dealloc_cmap(&fb->fb.cmap);
532 unmap:
533 iounmap(fb->regs);
534 clk_unprep:
535 clk_unprepare(fb->clk);
536 free_clk:
537 clk_put(fb->clk);
538 out:
539 return ret;
540 }
541
542 #ifdef CONFIG_OF
clcdfb_of_get_dpi_panel_mode(struct device_node * node,struct clcd_panel * clcd_panel)543 static int clcdfb_of_get_dpi_panel_mode(struct device_node *node,
544 struct clcd_panel *clcd_panel)
545 {
546 int err;
547 struct display_timing timing;
548 struct videomode video;
549
550 err = of_get_display_timing(node, "panel-timing", &timing);
551 if (err) {
552 pr_err("%pOF: problems parsing panel-timing (%d)\n", node, err);
553 return err;
554 }
555
556 videomode_from_timing(&timing, &video);
557
558 err = fb_videomode_from_videomode(&video, &clcd_panel->mode);
559 if (err)
560 return err;
561
562 /* Set up some inversion flags */
563 if (timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
564 clcd_panel->tim2 |= TIM2_IPC;
565 else if (!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE))
566 /*
567 * To preserve backwards compatibility, the IPC (inverted
568 * pixel clock) flag needs to be set on any display that
569 * doesn't explicitly specify that the pixel clock is
570 * active on the negative or positive edge.
571 */
572 clcd_panel->tim2 |= TIM2_IPC;
573
574 if (timing.flags & DISPLAY_FLAGS_HSYNC_LOW)
575 clcd_panel->tim2 |= TIM2_IHS;
576
577 if (timing.flags & DISPLAY_FLAGS_VSYNC_LOW)
578 clcd_panel->tim2 |= TIM2_IVS;
579
580 if (timing.flags & DISPLAY_FLAGS_DE_LOW)
581 clcd_panel->tim2 |= TIM2_IOE;
582
583 return 0;
584 }
585
clcdfb_snprintf_mode(char * buf,int size,struct fb_videomode * mode)586 static int clcdfb_snprintf_mode(char *buf, int size, struct fb_videomode *mode)
587 {
588 return snprintf(buf, size, "%ux%u@%u", mode->xres, mode->yres,
589 mode->refresh);
590 }
591
clcdfb_of_get_backlight(struct device * dev,struct clcd_panel * clcd_panel)592 static int clcdfb_of_get_backlight(struct device *dev,
593 struct clcd_panel *clcd_panel)
594 {
595 struct backlight_device *backlight;
596
597 /* Look up the optional backlight device */
598 backlight = devm_of_find_backlight(dev);
599 if (IS_ERR(backlight))
600 return PTR_ERR(backlight);
601
602 clcd_panel->backlight = backlight;
603 return 0;
604 }
605
clcdfb_of_get_mode(struct device * dev,struct device_node * panel,struct clcd_panel * clcd_panel)606 static int clcdfb_of_get_mode(struct device *dev, struct device_node *panel,
607 struct clcd_panel *clcd_panel)
608 {
609 int err;
610 struct fb_videomode *mode;
611 char *name;
612 int len;
613
614 /* Only directly connected DPI panels supported for now */
615 if (of_device_is_compatible(panel, "panel-dpi"))
616 err = clcdfb_of_get_dpi_panel_mode(panel, clcd_panel);
617 else
618 err = -ENOENT;
619 if (err)
620 return err;
621 mode = &clcd_panel->mode;
622
623 len = clcdfb_snprintf_mode(NULL, 0, mode);
624 name = devm_kzalloc(dev, len + 1, GFP_KERNEL);
625 if (!name)
626 return -ENOMEM;
627
628 clcdfb_snprintf_mode(name, len + 1, mode);
629 mode->name = name;
630
631 return 0;
632 }
633
clcdfb_of_init_tft_panel(struct clcd_fb * fb,u32 r0,u32 g0,u32 b0)634 static int clcdfb_of_init_tft_panel(struct clcd_fb *fb, u32 r0, u32 g0, u32 b0)
635 {
636 static struct {
637 unsigned int part;
638 u32 r0, g0, b0;
639 u32 caps;
640 } panels[] = {
641 { 0x110, 1, 7, 13, CLCD_CAP_5551 },
642 { 0x110, 0, 8, 16, CLCD_CAP_888 },
643 { 0x110, 16, 8, 0, CLCD_CAP_888 },
644 { 0x111, 4, 14, 20, CLCD_CAP_444 },
645 { 0x111, 3, 11, 19, CLCD_CAP_444 | CLCD_CAP_5551 },
646 { 0x111, 3, 10, 19, CLCD_CAP_444 | CLCD_CAP_5551 |
647 CLCD_CAP_565 },
648 { 0x111, 0, 8, 16, CLCD_CAP_444 | CLCD_CAP_5551 |
649 CLCD_CAP_565 | CLCD_CAP_888 },
650 };
651 int i;
652
653 /* Bypass pixel clock divider */
654 fb->panel->tim2 |= TIM2_BCD;
655
656 /* TFT display, vert. comp. interrupt at the start of the back porch */
657 fb->panel->cntl |= CNTL_LCDTFT | CNTL_LCDVCOMP(1);
658
659 fb->panel->caps = 0;
660
661 /* Match the setup with known variants */
662 for (i = 0; i < ARRAY_SIZE(panels) && !fb->panel->caps; i++) {
663 if (amba_part(fb->dev) != panels[i].part)
664 continue;
665 if (g0 != panels[i].g0)
666 continue;
667 if (r0 == panels[i].r0 && b0 == panels[i].b0)
668 fb->panel->caps = panels[i].caps;
669 }
670
671 /*
672 * If we actually physically connected the R lines to B and
673 * vice versa
674 */
675 if (r0 != 0 && b0 == 0)
676 fb->panel->bgr_connection = true;
677
678 return fb->panel->caps ? 0 : -EINVAL;
679 }
680
clcdfb_of_init_display(struct clcd_fb * fb)681 static int clcdfb_of_init_display(struct clcd_fb *fb)
682 {
683 struct device_node *endpoint, *panel;
684 int err;
685 unsigned int bpp;
686 u32 max_bandwidth;
687 u32 tft_r0b0g0[3];
688
689 fb->panel = devm_kzalloc(&fb->dev->dev, sizeof(*fb->panel), GFP_KERNEL);
690 if (!fb->panel)
691 return -ENOMEM;
692
693 /*
694 * Fetch the panel endpoint.
695 */
696 endpoint = of_graph_get_next_endpoint(fb->dev->dev.of_node, NULL);
697 if (!endpoint)
698 return -ENODEV;
699
700 panel = of_graph_get_remote_port_parent(endpoint);
701 if (!panel) {
702 err = -ENODEV;
703 goto out_endpoint_put;
704 }
705
706 err = clcdfb_of_get_backlight(&fb->dev->dev, fb->panel);
707 if (err)
708 goto out_panel_put;
709
710 err = clcdfb_of_get_mode(&fb->dev->dev, panel, fb->panel);
711 if (err)
712 goto out_panel_put;
713
714 err = of_property_read_u32(fb->dev->dev.of_node, "max-memory-bandwidth",
715 &max_bandwidth);
716 if (!err) {
717 /*
718 * max_bandwidth is in bytes per second and pixclock in
719 * pico-seconds, so the maximum allowed bits per pixel is
720 * 8 * max_bandwidth / (PICOS2KHZ(pixclock) * 1000)
721 * Rearrange this calculation to avoid overflow and then ensure
722 * result is a valid format.
723 */
724 bpp = max_bandwidth / (1000 / 8)
725 / PICOS2KHZ(fb->panel->mode.pixclock);
726 bpp = rounddown_pow_of_two(bpp);
727 if (bpp > 32)
728 bpp = 32;
729 } else
730 bpp = 32;
731 fb->panel->bpp = bpp;
732
733 #ifdef CONFIG_CPU_BIG_ENDIAN
734 fb->panel->cntl |= CNTL_BEBO;
735 #endif
736 fb->panel->width = -1;
737 fb->panel->height = -1;
738
739 if (of_property_read_u32_array(endpoint,
740 "arm,pl11x,tft-r0g0b0-pads",
741 tft_r0b0g0, ARRAY_SIZE(tft_r0b0g0)) != 0) {
742 err = -ENOENT;
743 goto out_panel_put;
744 }
745
746 of_node_put(panel);
747 of_node_put(endpoint);
748
749 return clcdfb_of_init_tft_panel(fb, tft_r0b0g0[0],
750 tft_r0b0g0[1], tft_r0b0g0[2]);
751 out_panel_put:
752 of_node_put(panel);
753 out_endpoint_put:
754 of_node_put(endpoint);
755 return err;
756 }
757
clcdfb_of_vram_setup(struct clcd_fb * fb)758 static int clcdfb_of_vram_setup(struct clcd_fb *fb)
759 {
760 int err;
761 struct device_node *memory;
762 u64 size;
763
764 err = clcdfb_of_init_display(fb);
765 if (err)
766 return err;
767
768 memory = of_parse_phandle(fb->dev->dev.of_node, "memory-region", 0);
769 if (!memory)
770 return -ENODEV;
771
772 fb->fb.screen_base = of_iomap(memory, 0);
773 if (!fb->fb.screen_base) {
774 of_node_put(memory);
775 return -ENOMEM;
776 }
777
778 fb->fb.fix.smem_start = of_translate_address(memory,
779 of_get_address(memory, 0, &size, NULL));
780 fb->fb.fix.smem_len = size;
781 of_node_put(memory);
782
783 return 0;
784 }
785
clcdfb_of_vram_mmap(struct clcd_fb * fb,struct vm_area_struct * vma)786 static int clcdfb_of_vram_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
787 {
788 unsigned long off, user_size, kernel_size;
789
790
791 off = vma->vm_pgoff << PAGE_SHIFT;
792 user_size = vma->vm_end - vma->vm_start;
793 kernel_size = fb->fb.fix.smem_len;
794
795 if (off >= kernel_size || user_size > (kernel_size - off))
796 return -ENXIO;
797
798 return remap_pfn_range(vma, vma->vm_start,
799 __phys_to_pfn(fb->fb.fix.smem_start) + vma->vm_pgoff,
800 user_size,
801 pgprot_writecombine(vma->vm_page_prot));
802 }
803
clcdfb_of_vram_remove(struct clcd_fb * fb)804 static void clcdfb_of_vram_remove(struct clcd_fb *fb)
805 {
806 iounmap(fb->fb.screen_base);
807 }
808
clcdfb_of_dma_setup(struct clcd_fb * fb)809 static int clcdfb_of_dma_setup(struct clcd_fb *fb)
810 {
811 unsigned long framesize;
812 dma_addr_t dma;
813 int err;
814
815 err = clcdfb_of_init_display(fb);
816 if (err)
817 return err;
818
819 framesize = PAGE_ALIGN(fb->panel->mode.xres * fb->panel->mode.yres *
820 fb->panel->bpp / 8);
821 fb->fb.screen_base = dma_alloc_coherent(&fb->dev->dev, framesize,
822 &dma, GFP_KERNEL);
823 if (!fb->fb.screen_base)
824 return -ENOMEM;
825
826 fb->fb.fix.smem_start = dma;
827 fb->fb.fix.smem_len = framesize;
828
829 return 0;
830 }
831
clcdfb_of_dma_mmap(struct clcd_fb * fb,struct vm_area_struct * vma)832 static int clcdfb_of_dma_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
833 {
834 return dma_mmap_wc(&fb->dev->dev, vma, fb->fb.screen_base,
835 fb->fb.fix.smem_start, fb->fb.fix.smem_len);
836 }
837
clcdfb_of_dma_remove(struct clcd_fb * fb)838 static void clcdfb_of_dma_remove(struct clcd_fb *fb)
839 {
840 dma_free_coherent(&fb->dev->dev, fb->fb.fix.smem_len,
841 fb->fb.screen_base, fb->fb.fix.smem_start);
842 }
843
clcdfb_of_get_board(struct amba_device * dev)844 static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
845 {
846 struct clcd_board *board = devm_kzalloc(&dev->dev, sizeof(*board),
847 GFP_KERNEL);
848 struct device_node *node = dev->dev.of_node;
849
850 if (!board)
851 return NULL;
852
853 board->name = of_node_full_name(node);
854 board->caps = CLCD_CAP_ALL;
855 board->check = clcdfb_check;
856 board->decode = clcdfb_decode;
857 if (of_find_property(node, "memory-region", NULL)) {
858 board->setup = clcdfb_of_vram_setup;
859 board->mmap = clcdfb_of_vram_mmap;
860 board->remove = clcdfb_of_vram_remove;
861 } else {
862 board->setup = clcdfb_of_dma_setup;
863 board->mmap = clcdfb_of_dma_mmap;
864 board->remove = clcdfb_of_dma_remove;
865 }
866
867 return board;
868 }
869 #else
clcdfb_of_get_board(struct amba_device * dev)870 static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
871 {
872 return NULL;
873 }
874 #endif
875
clcdfb_probe(struct amba_device * dev,const struct amba_id * id)876 static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
877 {
878 struct clcd_board *board = dev_get_platdata(&dev->dev);
879 struct clcd_fb *fb;
880 int ret;
881
882 if (!board)
883 board = clcdfb_of_get_board(dev);
884
885 if (!board)
886 return -EINVAL;
887
888 ret = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
889 if (ret)
890 goto out;
891
892 ret = amba_request_regions(dev, NULL);
893 if (ret) {
894 printk(KERN_ERR "CLCD: unable to reserve regs region\n");
895 goto out;
896 }
897
898 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
899 if (!fb) {
900 ret = -ENOMEM;
901 goto free_region;
902 }
903
904 fb->dev = dev;
905 fb->board = board;
906
907 dev_info(&fb->dev->dev, "PL%03x designer %02x rev%u at 0x%08llx\n",
908 amba_part(dev), amba_manf(dev), amba_rev(dev),
909 (unsigned long long)dev->res.start);
910
911 ret = fb->board->setup(fb);
912 if (ret)
913 goto free_fb;
914
915 ret = clcdfb_register(fb);
916 if (ret == 0) {
917 amba_set_drvdata(dev, fb);
918 goto out;
919 }
920
921 fb->board->remove(fb);
922 free_fb:
923 kfree(fb);
924 free_region:
925 amba_release_regions(dev);
926 out:
927 return ret;
928 }
929
clcdfb_remove(struct amba_device * dev)930 static void clcdfb_remove(struct amba_device *dev)
931 {
932 struct clcd_fb *fb = amba_get_drvdata(dev);
933
934 clcdfb_disable(fb);
935 unregister_framebuffer(&fb->fb);
936 if (fb->fb.cmap.len)
937 fb_dealloc_cmap(&fb->fb.cmap);
938 iounmap(fb->regs);
939 clk_unprepare(fb->clk);
940 clk_put(fb->clk);
941
942 fb->board->remove(fb);
943
944 kfree(fb);
945
946 amba_release_regions(dev);
947 }
948
949 static const struct amba_id clcdfb_id_table[] = {
950 {
951 .id = 0x00041110,
952 .mask = 0x000ffffe,
953 },
954 { 0, 0 },
955 };
956
957 MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
958
959 static struct amba_driver clcd_driver = {
960 .drv = {
961 .name = "clcd-pl11x",
962 },
963 .probe = clcdfb_probe,
964 .remove = clcdfb_remove,
965 .id_table = clcdfb_id_table,
966 };
967
amba_clcdfb_init(void)968 static int __init amba_clcdfb_init(void)
969 {
970 if (fb_get_options("ambafb", NULL))
971 return -ENODEV;
972
973 return amba_driver_register(&clcd_driver);
974 }
975
976 module_init(amba_clcdfb_init);
977
amba_clcdfb_exit(void)978 static void __exit amba_clcdfb_exit(void)
979 {
980 amba_driver_unregister(&clcd_driver);
981 }
982
983 module_exit(amba_clcdfb_exit);
984
985 MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
986 MODULE_LICENSE("GPL");
987