blob: 94a1998338dacb5dd726d44a9683338ef7185c95 [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 */
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
Jon Medhurst778a0242013-03-28 15:57:56 +000019#include <linux/dma-mapping.h>
20#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
Jon Medhurst778a0242013-03-28 15:57:56 +000022#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/fb.h>
24#include <linux/init.h>
25#include <linux/ioport.h>
26#include <linux/list.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000027#include <linux/amba/bus.h>
28#include <linux/amba/clcd.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000029#include <linux/clk.h>
Russell King934848d2009-01-08 09:58:51 +000030#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Russell Kingc6b8fda2005-10-28 14:05:16 +010032#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define to_clcd(info) container_of(info, struct clcd_fb, fb)
35
Catalin Marinas8df31022013-01-04 12:56:49 +000036#ifdef CONFIG_ARM
37#define clcdfb_dma_alloc dma_alloc_writecombine
38#define clcdfb_dma_free dma_free_writecombine
39#define clcdfb_dma_mmap dma_mmap_writecombine
40#else
41#define clcdfb_dma_alloc dma_alloc_coherent
42#define clcdfb_dma_free dma_free_coherent
43#define clcdfb_dma_mmap dma_mmap_coherent
44#endif
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* This is limited to 16 characters when displayed by X startup */
47static const char *clcd_name = "CLCD FB";
48
49/*
50 * Unfortunately, the enable/disable functions may be called either from
51 * process or IRQ context, and we _need_ to delay. This is _not_ good.
52 */
53static inline void clcdfb_sleep(unsigned int ms)
54{
55 if (in_atomic()) {
56 mdelay(ms);
57 } else {
58 msleep(ms);
59 }
60}
61
62static inline void clcdfb_set_start(struct clcd_fb *fb)
63{
64 unsigned long ustart = fb->fb.fix.smem_start;
65 unsigned long lstart;
66
67 ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
68 lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
69
70 writel(ustart, fb->regs + CLCD_UBAS);
71 writel(lstart, fb->regs + CLCD_LBAS);
72}
73
74static void clcdfb_disable(struct clcd_fb *fb)
75{
76 u32 val;
77
78 if (fb->board->disable)
79 fb->board->disable(fb);
80
Russell King3f175222010-02-12 14:32:01 +000081 val = readl(fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (val & CNTL_LCDPWR) {
83 val &= ~CNTL_LCDPWR;
Russell King3f175222010-02-12 14:32:01 +000084 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 clcdfb_sleep(20);
87 }
88 if (val & CNTL_LCDEN) {
89 val &= ~CNTL_LCDEN;
Russell King3f175222010-02-12 14:32:01 +000090 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 /*
94 * Disable CLCD clock source.
95 */
Russell King99c796d2010-08-17 22:13:22 +010096 if (fb->clk_enabled) {
97 fb->clk_enabled = false;
98 clk_disable(fb->clk);
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
103{
104 /*
105 * Enable the CLCD clock source.
106 */
Russell King99c796d2010-08-17 22:13:22 +0100107 if (!fb->clk_enabled) {
108 fb->clk_enabled = true;
109 clk_enable(fb->clk);
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /*
113 * Bring up by first enabling..
114 */
115 cntl |= CNTL_LCDEN;
Russell King3f175222010-02-12 14:32:01 +0000116 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 clcdfb_sleep(20);
119
120 /*
121 * and now apply power.
122 */
123 cntl |= CNTL_LCDPWR;
Russell King3f175222010-02-12 14:32:01 +0000124 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /*
127 * finally, enable the interface.
128 */
129 if (fb->board->enable)
130 fb->board->enable(fb);
131}
132
133static int
134clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
135{
Russell King7b4e9ce2011-01-21 14:03:28 +0000136 u32 caps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int ret = 0;
138
Russell King7b4e9ce2011-01-21 14:03:28 +0000139 if (fb->panel->caps && fb->board->caps)
140 caps = fb->panel->caps & fb->board->caps;
141 else {
142 /* Old way of specifying what can be used */
143 caps = fb->panel->cntl & CNTL_BGR ?
144 CLCD_CAP_BGR : CLCD_CAP_RGB;
145 /* But mask out 444 modes as they weren't supported */
146 caps &= ~CLCD_CAP_444;
147 }
148
149 /* Only TFT panels can do RGB888/BGR888 */
150 if (!(fb->panel->cntl & CNTL_LCDTFT))
151 caps &= ~CLCD_CAP_888;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 memset(&var->transp, 0, sizeof(var->transp));
Russell Kingc43e6f02006-01-26 14:12:06 +0000154
155 var->red.msb_right = 0;
156 var->green.msb_right = 0;
157 var->blue.msb_right = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 switch (var->bits_per_pixel) {
160 case 1:
161 case 2:
162 case 4:
163 case 8:
Russell King7b4e9ce2011-01-21 14:03:28 +0000164 /* If we can't do 5551, reject */
165 caps &= CLCD_CAP_5551;
166 if (!caps) {
167 ret = -EINVAL;
168 break;
169 }
170
Russell Kingc4d12b92005-04-28 10:38:19 +0100171 var->red.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 var->red.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100173 var->green.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 var->green.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100175 var->blue.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 var->blue.offset = 0;
177 break;
Russell King7b4e9ce2011-01-21 14:03:28 +0000178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 case 16:
Russell King7b4e9ce2011-01-21 14:03:28 +0000180 /* If we can't do 444, 5551 or 565, reject */
181 if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
182 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 }
Russell King7b4e9ce2011-01-21 14:03:28 +0000185
186 /*
187 * Green length can be 4, 5 or 6 depending whether
188 * we're operating in 444, 5551 or 565 mode.
189 */
190 if (var->green.length == 4 && caps & CLCD_CAP_444)
191 caps &= CLCD_CAP_444;
192 if (var->green.length == 5 && caps & CLCD_CAP_5551)
193 caps &= CLCD_CAP_5551;
194 else if (var->green.length == 6 && caps & CLCD_CAP_565)
195 caps &= CLCD_CAP_565;
196 else {
197 /*
198 * PL110 officially only supports RGB555,
199 * but may be wired up to allow RGB565.
200 */
201 if (caps & CLCD_CAP_565) {
202 var->green.length = 6;
203 caps &= CLCD_CAP_565;
204 } else if (caps & CLCD_CAP_5551) {
205 var->green.length = 5;
206 caps &= CLCD_CAP_5551;
207 } else {
208 var->green.length = 4;
209 caps &= CLCD_CAP_444;
210 }
211 }
212
213 if (var->green.length >= 5) {
214 var->red.length = 5;
215 var->blue.length = 5;
216 } else {
217 var->red.length = 4;
218 var->blue.length = 4;
219 }
220 break;
221 case 32:
222 /* If we can't do 888, reject */
223 caps &= CLCD_CAP_888;
224 if (!caps) {
225 ret = -EINVAL;
226 break;
227 }
228
229 var->red.length = 8;
230 var->green.length = 8;
231 var->blue.length = 8;
232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 default:
234 ret = -EINVAL;
235 break;
236 }
237
Russell Kingc43e6f02006-01-26 14:12:06 +0000238 /*
239 * >= 16bpp displays have separate colour component bitfields
240 * encoded in the pixel data. Calculate their position from
241 * the bitfield length defined above.
242 */
243 if (ret == 0 && var->bits_per_pixel >= 16) {
Russell King7b4e9ce2011-01-21 14:03:28 +0000244 bool bgr, rgb;
245
246 bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
247 rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
248
249 if (!bgr && !rgb)
250 /*
251 * The requested format was not possible, try just
252 * our capabilities. One of BGR or RGB must be
253 * supported.
254 */
255 bgr = caps & CLCD_CAP_BGR;
256
257 if (bgr) {
Russell Kingc43e6f02006-01-26 14:12:06 +0000258 var->blue.offset = 0;
259 var->green.offset = var->blue.offset + var->blue.length;
260 var->red.offset = var->green.offset + var->green.length;
261 } else {
262 var->red.offset = 0;
263 var->green.offset = var->red.offset + var->red.length;
264 var->blue.offset = var->green.offset + var->green.length;
265 }
266 }
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return ret;
269}
270
271static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
272{
273 struct clcd_fb *fb = to_clcd(info);
274 int ret = -EINVAL;
275
276 if (fb->board->check)
277 ret = fb->board->check(fb, var);
Russell King82235e92005-04-28 10:43:52 +0100278
279 if (ret == 0 &&
280 var->xres_virtual * var->bits_per_pixel / 8 *
281 var->yres_virtual > fb->fb.fix.smem_len)
282 ret = -EINVAL;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (ret == 0)
285 ret = clcdfb_set_bitfields(fb, var);
286
287 return ret;
288}
289
290static int clcdfb_set_par(struct fb_info *info)
291{
292 struct clcd_fb *fb = to_clcd(info);
293 struct clcd_regs regs;
294
295 fb->fb.fix.line_length = fb->fb.var.xres_virtual *
296 fb->fb.var.bits_per_pixel / 8;
297
298 if (fb->fb.var.bits_per_pixel <= 8)
299 fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
300 else
301 fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
302
303 fb->board->decode(fb, &regs);
304
305 clcdfb_disable(fb);
306
307 writel(regs.tim0, fb->regs + CLCD_TIM0);
308 writel(regs.tim1, fb->regs + CLCD_TIM1);
309 writel(regs.tim2, fb->regs + CLCD_TIM2);
310 writel(regs.tim3, fb->regs + CLCD_TIM3);
311
312 clcdfb_set_start(fb);
313
314 clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
315
316 fb->clcd_cntl = regs.cntl;
317
318 clcdfb_enable(fb, regs.cntl);
319
320#ifdef DEBUG
Joe Perchesad361c92009-07-06 13:05:40 -0700321 printk(KERN_INFO
322 "CLCD: Registers set to\n"
323 " %08x %08x %08x %08x\n"
324 " %08x %08x %08x %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
326 readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
327 readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
Russell King3f175222010-02-12 14:32:01 +0000328 readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#endif
330
331 return 0;
332}
333
334static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
335{
336 unsigned int mask = (1 << bf->length) - 1;
337
338 return (val >> (16 - bf->length) & mask) << bf->offset;
339}
340
341/*
342 * Set a single color register. The values supplied have a 16 bit
343 * magnitude. Return != 0 for invalid regno.
344 */
345static int
346clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
347 unsigned int blue, unsigned int transp, struct fb_info *info)
348{
349 struct clcd_fb *fb = to_clcd(info);
350
351 if (regno < 16)
352 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
353 convert_bitfield(blue, &fb->fb.var.blue) |
354 convert_bitfield(green, &fb->fb.var.green) |
355 convert_bitfield(red, &fb->fb.var.red);
356
Russell King1ddb8a12005-04-30 22:39:51 +0100357 if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
359 u32 val, mask, newval;
360
361 newval = (red >> 11) & 0x001f;
362 newval |= (green >> 6) & 0x03e0;
363 newval |= (blue >> 1) & 0x7c00;
364
365 /*
366 * 3.2.11: if we're configured for big endian
367 * byte order, the palette entries are swapped.
368 */
369 if (fb->clcd_cntl & CNTL_BEBO)
370 regno ^= 1;
371
372 if (regno & 1) {
373 newval <<= 16;
374 mask = 0x0000ffff;
375 } else {
376 mask = 0xffff0000;
377 }
378
379 val = readl(fb->regs + hw_reg) & mask;
380 writel(val | newval, fb->regs + hw_reg);
381 }
382
383 return regno > 255;
384}
385
386/*
387 * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
388 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
389 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
390 * to e.g. a video mode which doesn't support it. Implements VESA suspend
391 * and powerdown modes on hardware that supports disabling hsync/vsync:
392 * blank_mode == 2: suspend vsync
393 * blank_mode == 3: suspend hsync
394 * blank_mode == 4: powerdown
395 */
396static int clcdfb_blank(int blank_mode, struct fb_info *info)
397{
398 struct clcd_fb *fb = to_clcd(info);
399
400 if (blank_mode != 0) {
401 clcdfb_disable(fb);
402 } else {
403 clcdfb_enable(fb, fb->clcd_cntl);
404 }
405 return 0;
406}
Will Deacon50ad0eb2013-01-28 12:06:58 +0000407
Jon Medhurst778a0242013-03-28 15:57:56 +0000408int clcdfb_mmap_dma(struct clcd_fb *fb, struct vm_area_struct *vma)
409{
Will Deacon50ad0eb2013-01-28 12:06:58 +0000410 return clcdfb_dma_mmap(&fb->dev->dev, vma,
411 fb->fb.screen_base,
412 fb->fb.fix.smem_start,
413 fb->fb.fix.smem_len);
414}
415
416int clcdfb_mmap_io(struct clcd_fb *fb, struct vm_area_struct *vma)
417{
418 unsigned long user_count, count, pfn, off;
419
420 user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
421 count = PAGE_ALIGN(fb->fb.fix.smem_len) >> PAGE_SHIFT;
422 pfn = fb->fb.fix.smem_start >> PAGE_SHIFT;
423 off = vma->vm_pgoff;
424
425 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
426
427 if (off < count && user_count <= (count - off))
428 return remap_pfn_range(vma, vma->vm_start, pfn + off,
429 user_count << PAGE_SHIFT,
430 vma->vm_page_prot);
431
432 return -ENXIO;
Jon Medhurst778a0242013-03-28 15:57:56 +0000433}
434
435void clcdfb_remove_dma(struct clcd_fb *fb)
436{
Will Deacon50ad0eb2013-01-28 12:06:58 +0000437 clcdfb_dma_free(&fb->dev->dev, fb->fb.fix.smem_len,
438 fb->fb.screen_base, fb->fb.fix.smem_start);
439}
Catalin Marinas8df31022013-01-04 12:56:49 +0000440
Will Deacon50ad0eb2013-01-28 12:06:58 +0000441void clcdfb_remove_io(struct clcd_fb *fb)
442{
443 iounmap(fb->fb.screen_base);
Jon Medhurst778a0242013-03-28 15:57:56 +0000444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Christoph Hellwig216d5262006-01-14 13:21:25 -0800446static int clcdfb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct vm_area_struct *vma)
448{
449 struct clcd_fb *fb = to_clcd(info);
450 unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
451 int ret = -EINVAL;
452
453 len = info->fix.smem_len;
454
455 if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
456 fb->board->mmap)
457 ret = fb->board->mmap(fb, vma);
458
459 return ret;
460}
461
462static struct fb_ops clcdfb_ops = {
463 .owner = THIS_MODULE,
464 .fb_check_var = clcdfb_check_var,
465 .fb_set_par = clcdfb_set_par,
466 .fb_setcolreg = clcdfb_setcolreg,
467 .fb_blank = clcdfb_blank,
468 .fb_fillrect = cfb_fillrect,
469 .fb_copyarea = cfb_copyarea,
470 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 .fb_mmap = clcdfb_mmap,
472};
473
474static int clcdfb_register(struct clcd_fb *fb)
475{
476 int ret;
477
Russell King3f175222010-02-12 14:32:01 +0000478 /*
479 * ARM PL111 always has IENB at 0x1c; it's only PL110
480 * which is reversed on some platforms.
481 */
482 if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
483 fb->off_ienb = CLCD_PL111_IENB;
484 fb->off_cntl = CLCD_PL111_CNTL;
485 } else {
486#ifdef CONFIG_ARCH_VERSATILE
487 fb->off_ienb = CLCD_PL111_IENB;
488 fb->off_cntl = CLCD_PL111_CNTL;
489#else
490 fb->off_ienb = CLCD_PL110_IENB;
491 fb->off_cntl = CLCD_PL110_CNTL;
492#endif
493 }
494
Russell Kingee569c42008-11-30 17:38:14 +0000495 fb->clk = clk_get(&fb->dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (IS_ERR(fb->clk)) {
497 ret = PTR_ERR(fb->clk);
498 goto out;
499 }
500
Russell King99df4ee2011-09-22 12:34:31 +0100501 ret = clk_prepare(fb->clk);
502 if (ret)
503 goto free_clk;
504
Loïc Minier17e8c4e2011-06-20 20:44:17 +0000505 fb->fb.device = &fb->dev->dev;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 fb->fb.fix.mmio_start = fb->dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +0100508 fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
511 if (!fb->regs) {
512 printk(KERN_ERR "CLCD: unable to remap registers\n");
513 ret = -ENOMEM;
Russell King99df4ee2011-09-22 12:34:31 +0100514 goto clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 fb->fb.fbops = &clcdfb_ops;
518 fb->fb.flags = FBINFO_FLAG_DEFAULT;
519 fb->fb.pseudo_palette = fb->cmap;
520
521 strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
522 fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
523 fb->fb.fix.type_aux = 0;
524 fb->fb.fix.xpanstep = 0;
525 fb->fb.fix.ypanstep = 0;
526 fb->fb.fix.ywrapstep = 0;
527 fb->fb.fix.accel = FB_ACCEL_NONE;
528
529 fb->fb.var.xres = fb->panel->mode.xres;
530 fb->fb.var.yres = fb->panel->mode.yres;
531 fb->fb.var.xres_virtual = fb->panel->mode.xres;
532 fb->fb.var.yres_virtual = fb->panel->mode.yres;
533 fb->fb.var.bits_per_pixel = fb->panel->bpp;
534 fb->fb.var.grayscale = fb->panel->grayscale;
535 fb->fb.var.pixclock = fb->panel->mode.pixclock;
536 fb->fb.var.left_margin = fb->panel->mode.left_margin;
537 fb->fb.var.right_margin = fb->panel->mode.right_margin;
538 fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
539 fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
540 fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
541 fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
542 fb->fb.var.sync = fb->panel->mode.sync;
543 fb->fb.var.vmode = fb->panel->mode.vmode;
544 fb->fb.var.activate = FB_ACTIVATE_NOW;
545 fb->fb.var.nonstd = 0;
546 fb->fb.var.height = fb->panel->height;
547 fb->fb.var.width = fb->panel->width;
548 fb->fb.var.accel_flags = 0;
549
550 fb->fb.monspecs.hfmin = 0;
551 fb->fb.monspecs.hfmax = 100000;
552 fb->fb.monspecs.vfmin = 0;
553 fb->fb.monspecs.vfmax = 400;
554 fb->fb.monspecs.dclkmin = 1000000;
555 fb->fb.monspecs.dclkmax = 100000000;
556
557 /*
558 * Make sure that the bitfields are set appropriately.
559 */
560 clcdfb_set_bitfields(fb, &fb->fb.var);
561
562 /*
563 * Allocate colourmap.
564 */
Andres Salomon909baf02009-03-31 15:25:29 -0700565 ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
566 if (ret)
567 goto unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /*
570 * Ensure interrupts are disabled.
571 */
Russell King3f175222010-02-12 14:32:01 +0000572 writel(0, fb->regs + fb->off_ienb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 fb_set_var(&fb->fb, &fb->fb.var);
575
Russell Kingff643322011-01-19 21:10:24 +0000576 dev_info(&fb->dev->dev, "%s hardware, %s display\n",
577 fb->board->name, fb->panel->mode.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 ret = register_framebuffer(&fb->fb);
580 if (ret == 0)
581 goto out;
582
583 printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
584
Andres Salomon909baf02009-03-31 15:25:29 -0700585 fb_dealloc_cmap(&fb->fb.cmap);
586 unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +0100588 clk_unprep:
589 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 free_clk:
591 clk_put(fb->clk);
592 out:
593 return ret;
594}
595
Jon Medhurst778a0242013-03-28 15:57:56 +0000596struct string_lookup {
597 const char *string;
598 const u32 val;
599};
600
601static struct string_lookup vmode_lookups[] = {
602 { "FB_VMODE_NONINTERLACED", FB_VMODE_NONINTERLACED},
603 { "FB_VMODE_INTERLACED", FB_VMODE_INTERLACED},
604 { "FB_VMODE_DOUBLE", FB_VMODE_DOUBLE},
605 { "FB_VMODE_ODD_FLD_FIRST", FB_VMODE_ODD_FLD_FIRST},
606 { NULL, 0 },
607};
608
609static struct string_lookup tim2_lookups[] = {
610 { "TIM2_CLKSEL", TIM2_CLKSEL},
611 { "TIM2_IVS", TIM2_IVS},
612 { "TIM2_IHS", TIM2_IHS},
613 { "TIM2_IPC", TIM2_IPC},
614 { "TIM2_IOE", TIM2_IOE},
615 { "TIM2_BCD", TIM2_BCD},
616 { NULL, 0},
617};
618static struct string_lookup cntl_lookups[] = {
619 {"CNTL_LCDEN", CNTL_LCDEN},
620 {"CNTL_LCDBPP1", CNTL_LCDBPP1},
621 {"CNTL_LCDBPP2", CNTL_LCDBPP2},
622 {"CNTL_LCDBPP4", CNTL_LCDBPP4},
623 {"CNTL_LCDBPP8", CNTL_LCDBPP8},
624 {"CNTL_LCDBPP16", CNTL_LCDBPP16},
625 {"CNTL_LCDBPP16_565", CNTL_LCDBPP16_565},
626 {"CNTL_LCDBPP16_444", CNTL_LCDBPP16_444},
627 {"CNTL_LCDBPP24", CNTL_LCDBPP24},
628 {"CNTL_LCDBW", CNTL_LCDBW},
629 {"CNTL_LCDTFT", CNTL_LCDTFT},
630 {"CNTL_LCDMONO8", CNTL_LCDMONO8},
631 {"CNTL_LCDDUAL", CNTL_LCDDUAL},
632 {"CNTL_BGR", CNTL_BGR},
633 {"CNTL_BEBO", CNTL_BEBO},
634 {"CNTL_BEPO", CNTL_BEPO},
635 {"CNTL_LCDPWR", CNTL_LCDPWR},
636 {"CNTL_LCDVCOMP(1)", CNTL_LCDVCOMP(1)},
637 {"CNTL_LCDVCOMP(2)", CNTL_LCDVCOMP(2)},
638 {"CNTL_LCDVCOMP(3)", CNTL_LCDVCOMP(3)},
639 {"CNTL_LCDVCOMP(4)", CNTL_LCDVCOMP(4)},
640 {"CNTL_LCDVCOMP(5)", CNTL_LCDVCOMP(5)},
641 {"CNTL_LCDVCOMP(6)", CNTL_LCDVCOMP(6)},
642 {"CNTL_LCDVCOMP(7)", CNTL_LCDVCOMP(7)},
643 {"CNTL_LDMAFIFOTIME", CNTL_LDMAFIFOTIME},
644 {"CNTL_WATERMARK", CNTL_WATERMARK},
645 { NULL, 0},
646};
647static struct string_lookup caps_lookups[] = {
648 {"CLCD_CAP_RGB444", CLCD_CAP_RGB444},
649 {"CLCD_CAP_RGB5551", CLCD_CAP_RGB5551},
650 {"CLCD_CAP_RGB565", CLCD_CAP_RGB565},
651 {"CLCD_CAP_RGB888", CLCD_CAP_RGB888},
652 {"CLCD_CAP_BGR444", CLCD_CAP_BGR444},
653 {"CLCD_CAP_BGR5551", CLCD_CAP_BGR5551},
654 {"CLCD_CAP_BGR565", CLCD_CAP_BGR565},
655 {"CLCD_CAP_BGR888", CLCD_CAP_BGR888},
656 {"CLCD_CAP_444", CLCD_CAP_444},
657 {"CLCD_CAP_5551", CLCD_CAP_5551},
658 {"CLCD_CAP_565", CLCD_CAP_565},
659 {"CLCD_CAP_888", CLCD_CAP_888},
660 {"CLCD_CAP_RGB", CLCD_CAP_RGB},
661 {"CLCD_CAP_BGR", CLCD_CAP_BGR},
662 {"CLCD_CAP_ALL", CLCD_CAP_ALL},
663 { NULL, 0},
664};
665
666u32 parse_setting(struct string_lookup *lookup, const char *name)
667{
668 int i = 0;
669 while (lookup[i].string != NULL) {
670 if (strcmp(lookup[i].string, name) == 0)
671 return lookup[i].val;
672 ++i;
673 }
674 return -EINVAL;
675}
676
677u32 get_string_lookup(struct device_node *node, const char *name,
678 struct string_lookup *lookup)
679{
680 const char *string;
681 int count, i, ret = 0;
682
683 count = of_property_count_strings(node, name);
684 if (count >= 0)
685 for (i = 0; i < count; i++)
686 if (of_property_read_string_index(node, name, i,
687 &string) == 0)
688 ret |= parse_setting(lookup, string);
689 return ret;
690}
691
692int get_val(struct device_node *node, const char *string)
693{
694 u32 ret = 0;
695
696 if (of_property_read_u32(node, string, &ret))
697 ret = -1;
698 return ret;
699}
700
701struct clcd_panel *getPanel(struct device_node *node)
702{
703 static struct clcd_panel panel;
704
705 panel.mode.refresh = get_val(node, "refresh");
706 panel.mode.xres = get_val(node, "xres");
707 panel.mode.yres = get_val(node, "yres");
708 panel.mode.pixclock = get_val(node, "pixclock");
709 panel.mode.left_margin = get_val(node, "left_margin");
710 panel.mode.right_margin = get_val(node, "right_margin");
711 panel.mode.upper_margin = get_val(node, "upper_margin");
712 panel.mode.lower_margin = get_val(node, "lower_margin");
713 panel.mode.hsync_len = get_val(node, "hsync_len");
714 panel.mode.vsync_len = get_val(node, "vsync_len");
715 panel.mode.sync = get_val(node, "sync");
716 panel.bpp = get_val(node, "bpp");
717 panel.width = (signed short) get_val(node, "width");
718 panel.height = (signed short) get_val(node, "height");
719
720 panel.mode.vmode = get_string_lookup(node, "vmode", vmode_lookups);
721 panel.tim2 = get_string_lookup(node, "tim2", tim2_lookups);
722 panel.cntl = get_string_lookup(node, "cntl", cntl_lookups);
723 panel.caps = get_string_lookup(node, "caps", caps_lookups);
724
725 return &panel;
726}
727
728struct clcd_panel *clcdfb_get_panel(const char *name)
729{
730 struct device_node *node = NULL;
731 const char *mode;
732 struct clcd_panel *panel = NULL;
733
734 do {
735 node = of_find_compatible_node(node, NULL, "panel");
736 if (node)
737 if (of_property_read_string(node, "mode", &mode) == 0)
738 if (strcmp(mode, name) == 0) {
739 panel = getPanel(node);
740 panel->mode.name = name;
741 }
742 } while (node != NULL);
743
744 return panel;
745}
746
747#ifdef CONFIG_OF
748static int clcdfb_dt_init(struct clcd_fb *fb)
749{
750 int err = 0;
751 struct device_node *node;
752 const char *mode;
753 dma_addr_t dma;
754 u32 use_dma;
755 const __be32 *prop;
756 int len, na, ns;
757 phys_addr_t fb_base, fb_size;
758
759 node = fb->dev->dev.of_node;
760 if (!node)
761 return -ENODEV;
762
763 na = of_n_addr_cells(node);
764 ns = of_n_size_cells(node);
765
766 if (WARN_ON(of_property_read_string(node, "mode", &mode)))
767 return -ENODEV;
768
769 fb->panel = clcdfb_get_panel(mode);
770 if (!fb->panel)
771 return -EINVAL;
772 fb->fb.fix.smem_len = fb->panel->mode.xres * fb->panel->mode.yres * 2;
773
Will Deacon50ad0eb2013-01-28 12:06:58 +0000774 fb->board->name = "Device Tree CLCD PL111";
775 fb->board->caps = CLCD_CAP_5551 | CLCD_CAP_565;
776 fb->board->check = clcdfb_check;
777 fb->board->decode = clcdfb_decode;
778
Jon Medhurst778a0242013-03-28 15:57:56 +0000779 if (of_property_read_u32(node, "use_dma", &use_dma))
780 use_dma = 0;
Will Deacon50ad0eb2013-01-28 12:06:58 +0000781
Jon Medhurst778a0242013-03-28 15:57:56 +0000782 if (use_dma) {
Catalin Marinas8df31022013-01-04 12:56:49 +0000783 fb->fb.screen_base = clcdfb_dma_alloc(&fb->dev->dev,
Will Deacon50ad0eb2013-01-28 12:06:58 +0000784 fb->fb.fix.smem_len,
785 &dma, GFP_KERNEL);
Jon Medhurst778a0242013-03-28 15:57:56 +0000786 if (!fb->fb.screen_base) {
787 pr_err("CLCD: unable to map framebuffer\n");
Will Deacon50ad0eb2013-01-28 12:06:58 +0000788 return -ENOMEM;
789 }
790
791 fb->fb.fix.smem_start = dma;
792 fb->board->mmap = clcdfb_mmap_dma;
793 fb->board->remove = clcdfb_remove_dma;
Jon Medhurst778a0242013-03-28 15:57:56 +0000794 } else {
795 prop = of_get_property(node, "framebuffer", &len);
796 if (WARN_ON(!prop || len < (na + ns) * sizeof(*prop)))
797 return -EINVAL;
Will Deacon50ad0eb2013-01-28 12:06:58 +0000798
Jon Medhurst778a0242013-03-28 15:57:56 +0000799 fb_base = of_read_number(prop, na);
800 fb_size = of_read_number(prop + na, ns);
801
Will Deacon50ad0eb2013-01-28 12:06:58 +0000802 fb->fb.fix.smem_start = fb_base;
803 fb->fb.screen_base = ioremap_wc(fb_base, fb_size);
804 fb->board->mmap = clcdfb_mmap_io;
805 fb->board->remove = clcdfb_remove_io;
Jon Medhurst778a0242013-03-28 15:57:56 +0000806 }
Will Deacon50ad0eb2013-01-28 12:06:58 +0000807
Jon Medhurst778a0242013-03-28 15:57:56 +0000808 return err;
809}
810#endif /* CONFIG_OF */
811
Russell Kingaa25afa2011-02-19 15:55:00 +0000812static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct clcd_board *board = dev->dev.platform_data;
815 struct clcd_fb *fb;
816 int ret;
817
Will Deacon50ad0eb2013-01-28 12:06:58 +0000818 if (!board) {
Jon Medhurst778a0242013-03-28 15:57:56 +0000819#ifdef CONFIG_OF
Will Deacon50ad0eb2013-01-28 12:06:58 +0000820 if (dev->dev.of_node) {
Jon Medhurst778a0242013-03-28 15:57:56 +0000821 board = kzalloc(sizeof(struct clcd_board), GFP_KERNEL);
822 if (!board)
Will Deacon50ad0eb2013-01-28 12:06:58 +0000823 return -ENOMEM;
Jon Medhurst778a0242013-03-28 15:57:56 +0000824 board->setup = clcdfb_dt_init;
Will Deacon50ad0eb2013-01-28 12:06:58 +0000825 } else
826#endif
827 return -EINVAL;
Jon Medhurst778a0242013-03-28 15:57:56 +0000828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 ret = amba_request_regions(dev, NULL);
831 if (ret) {
832 printk(KERN_ERR "CLCD: unable to reserve regs region\n");
833 goto out;
834 }
835
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700836 fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!fb) {
838 printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
839 ret = -ENOMEM;
840 goto free_region;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 fb->dev = dev;
844 fb->board = board;
845
Russell Kingff643322011-01-19 21:10:24 +0000846 dev_info(&fb->dev->dev, "PL%03x rev%u at 0x%08llx\n",
847 amba_part(dev), amba_rev(dev),
848 (unsigned long long)dev->res.start);
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 ret = fb->board->setup(fb);
851 if (ret)
852 goto free_fb;
853
854 ret = clcdfb_register(fb);
855 if (ret == 0) {
856 amba_set_drvdata(dev, fb);
857 goto out;
858 }
859
860 fb->board->remove(fb);
861 free_fb:
862 kfree(fb);
863 free_region:
864 amba_release_regions(dev);
865 out:
866 return ret;
867}
868
869static int clcdfb_remove(struct amba_device *dev)
870{
871 struct clcd_fb *fb = amba_get_drvdata(dev);
872
873 amba_set_drvdata(dev, NULL);
874
875 clcdfb_disable(fb);
876 unregister_framebuffer(&fb->fb);
Andres Salomon909baf02009-03-31 15:25:29 -0700877 if (fb->fb.cmap.len)
878 fb_dealloc_cmap(&fb->fb.cmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +0100880 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 clk_put(fb->clk);
882
883 fb->board->remove(fb);
884
885 kfree(fb);
886
887 amba_release_regions(dev);
888
889 return 0;
890}
891
892static struct amba_id clcdfb_id_table[] = {
893 {
894 .id = 0x00041110,
Russell Kinge8315562005-11-02 14:40:35 +0000895 .mask = 0x000ffffe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 },
897 { 0, 0 },
898};
899
Dave Martin6054f9b2011-10-05 15:15:23 +0100900MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static struct amba_driver clcd_driver = {
903 .drv = {
Russell Kinge8315562005-11-02 14:40:35 +0000904 .name = "clcd-pl11x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 },
906 .probe = clcdfb_probe,
907 .remove = clcdfb_remove,
908 .id_table = clcdfb_id_table,
909};
910
Russell King2c250132005-11-08 14:44:15 +0000911static int __init amba_clcdfb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 if (fb_get_options("ambafb", NULL))
914 return -ENODEV;
915
916 return amba_driver_register(&clcd_driver);
917}
918
919module_init(amba_clcdfb_init);
920
921static void __exit amba_clcdfb_exit(void)
922{
923 amba_driver_unregister(&clcd_driver);
924}
925
926module_exit(amba_clcdfb_exit);
927
928MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
929MODULE_LICENSE("GPL");