blob: 53af3d18a0c6dfb11af2212c5d87ed47e239d7a8 [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
36/* This is limited to 16 characters when displayed by X startup */
37static const char *clcd_name = "CLCD FB";
38
39/*
40 * Unfortunately, the enable/disable functions may be called either from
41 * process or IRQ context, and we _need_ to delay. This is _not_ good.
42 */
43static inline void clcdfb_sleep(unsigned int ms)
44{
45 if (in_atomic()) {
46 mdelay(ms);
47 } else {
48 msleep(ms);
49 }
50}
51
52static inline void clcdfb_set_start(struct clcd_fb *fb)
53{
54 unsigned long ustart = fb->fb.fix.smem_start;
55 unsigned long lstart;
56
57 ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
58 lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
59
60 writel(ustart, fb->regs + CLCD_UBAS);
61 writel(lstart, fb->regs + CLCD_LBAS);
62}
63
64static void clcdfb_disable(struct clcd_fb *fb)
65{
66 u32 val;
67
68 if (fb->board->disable)
69 fb->board->disable(fb);
70
Russell King3f175222010-02-12 14:32:01 +000071 val = readl(fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (val & CNTL_LCDPWR) {
73 val &= ~CNTL_LCDPWR;
Russell King3f175222010-02-12 14:32:01 +000074 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 clcdfb_sleep(20);
77 }
78 if (val & CNTL_LCDEN) {
79 val &= ~CNTL_LCDEN;
Russell King3f175222010-02-12 14:32:01 +000080 writel(val, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
83 /*
84 * Disable CLCD clock source.
85 */
Russell King99c796d2010-08-17 22:13:22 +010086 if (fb->clk_enabled) {
87 fb->clk_enabled = false;
88 clk_disable(fb->clk);
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
93{
94 /*
95 * Enable the CLCD clock source.
96 */
Russell King99c796d2010-08-17 22:13:22 +010097 if (!fb->clk_enabled) {
98 fb->clk_enabled = true;
99 clk_enable(fb->clk);
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /*
103 * Bring up by first enabling..
104 */
105 cntl |= CNTL_LCDEN;
Russell King3f175222010-02-12 14:32:01 +0000106 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 clcdfb_sleep(20);
109
110 /*
111 * and now apply power.
112 */
113 cntl |= CNTL_LCDPWR;
Russell King3f175222010-02-12 14:32:01 +0000114 writel(cntl, fb->regs + fb->off_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /*
117 * finally, enable the interface.
118 */
119 if (fb->board->enable)
120 fb->board->enable(fb);
121}
122
123static int
124clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
125{
Russell King7b4e9ce2011-01-21 14:03:28 +0000126 u32 caps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int ret = 0;
128
Russell King7b4e9ce2011-01-21 14:03:28 +0000129 if (fb->panel->caps && fb->board->caps)
130 caps = fb->panel->caps & fb->board->caps;
131 else {
132 /* Old way of specifying what can be used */
133 caps = fb->panel->cntl & CNTL_BGR ?
134 CLCD_CAP_BGR : CLCD_CAP_RGB;
135 /* But mask out 444 modes as they weren't supported */
136 caps &= ~CLCD_CAP_444;
137 }
138
139 /* Only TFT panels can do RGB888/BGR888 */
140 if (!(fb->panel->cntl & CNTL_LCDTFT))
141 caps &= ~CLCD_CAP_888;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 memset(&var->transp, 0, sizeof(var->transp));
Russell Kingc43e6f02006-01-26 14:12:06 +0000144
145 var->red.msb_right = 0;
146 var->green.msb_right = 0;
147 var->blue.msb_right = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 switch (var->bits_per_pixel) {
150 case 1:
151 case 2:
152 case 4:
153 case 8:
Russell King7b4e9ce2011-01-21 14:03:28 +0000154 /* If we can't do 5551, reject */
155 caps &= CLCD_CAP_5551;
156 if (!caps) {
157 ret = -EINVAL;
158 break;
159 }
160
Russell Kingc4d12b92005-04-28 10:38:19 +0100161 var->red.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 var->red.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100163 var->green.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 var->green.offset = 0;
Russell Kingc4d12b92005-04-28 10:38:19 +0100165 var->blue.length = var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 var->blue.offset = 0;
167 break;
Russell King7b4e9ce2011-01-21 14:03:28 +0000168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 case 16:
Russell King7b4e9ce2011-01-21 14:03:28 +0000170 /* If we can't do 444, 5551 or 565, reject */
171 if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
172 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174 }
Russell King7b4e9ce2011-01-21 14:03:28 +0000175
176 /*
177 * Green length can be 4, 5 or 6 depending whether
178 * we're operating in 444, 5551 or 565 mode.
179 */
180 if (var->green.length == 4 && caps & CLCD_CAP_444)
181 caps &= CLCD_CAP_444;
182 if (var->green.length == 5 && caps & CLCD_CAP_5551)
183 caps &= CLCD_CAP_5551;
184 else if (var->green.length == 6 && caps & CLCD_CAP_565)
185 caps &= CLCD_CAP_565;
186 else {
187 /*
188 * PL110 officially only supports RGB555,
189 * but may be wired up to allow RGB565.
190 */
191 if (caps & CLCD_CAP_565) {
192 var->green.length = 6;
193 caps &= CLCD_CAP_565;
194 } else if (caps & CLCD_CAP_5551) {
195 var->green.length = 5;
196 caps &= CLCD_CAP_5551;
197 } else {
198 var->green.length = 4;
199 caps &= CLCD_CAP_444;
200 }
201 }
202
203 if (var->green.length >= 5) {
204 var->red.length = 5;
205 var->blue.length = 5;
206 } else {
207 var->red.length = 4;
208 var->blue.length = 4;
209 }
210 break;
211 case 32:
212 /* If we can't do 888, reject */
213 caps &= CLCD_CAP_888;
214 if (!caps) {
215 ret = -EINVAL;
216 break;
217 }
218
219 var->red.length = 8;
220 var->green.length = 8;
221 var->blue.length = 8;
222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 default:
224 ret = -EINVAL;
225 break;
226 }
227
Russell Kingc43e6f02006-01-26 14:12:06 +0000228 /*
229 * >= 16bpp displays have separate colour component bitfields
230 * encoded in the pixel data. Calculate their position from
231 * the bitfield length defined above.
232 */
233 if (ret == 0 && var->bits_per_pixel >= 16) {
Russell King7b4e9ce2011-01-21 14:03:28 +0000234 bool bgr, rgb;
235
236 bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
237 rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
238
239 if (!bgr && !rgb)
240 /*
241 * The requested format was not possible, try just
242 * our capabilities. One of BGR or RGB must be
243 * supported.
244 */
245 bgr = caps & CLCD_CAP_BGR;
246
247 if (bgr) {
Russell Kingc43e6f02006-01-26 14:12:06 +0000248 var->blue.offset = 0;
249 var->green.offset = var->blue.offset + var->blue.length;
250 var->red.offset = var->green.offset + var->green.length;
251 } else {
252 var->red.offset = 0;
253 var->green.offset = var->red.offset + var->red.length;
254 var->blue.offset = var->green.offset + var->green.length;
255 }
256 }
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return ret;
259}
260
261static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
262{
263 struct clcd_fb *fb = to_clcd(info);
264 int ret = -EINVAL;
265
266 if (fb->board->check)
267 ret = fb->board->check(fb, var);
Russell King82235e92005-04-28 10:43:52 +0100268
269 if (ret == 0 &&
270 var->xres_virtual * var->bits_per_pixel / 8 *
271 var->yres_virtual > fb->fb.fix.smem_len)
272 ret = -EINVAL;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (ret == 0)
275 ret = clcdfb_set_bitfields(fb, var);
276
277 return ret;
278}
279
280static int clcdfb_set_par(struct fb_info *info)
281{
282 struct clcd_fb *fb = to_clcd(info);
283 struct clcd_regs regs;
284
285 fb->fb.fix.line_length = fb->fb.var.xres_virtual *
286 fb->fb.var.bits_per_pixel / 8;
287
288 if (fb->fb.var.bits_per_pixel <= 8)
289 fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
290 else
291 fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
292
293 fb->board->decode(fb, &regs);
294
295 clcdfb_disable(fb);
296
297 writel(regs.tim0, fb->regs + CLCD_TIM0);
298 writel(regs.tim1, fb->regs + CLCD_TIM1);
299 writel(regs.tim2, fb->regs + CLCD_TIM2);
300 writel(regs.tim3, fb->regs + CLCD_TIM3);
301
302 clcdfb_set_start(fb);
303
304 clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
305
306 fb->clcd_cntl = regs.cntl;
307
308 clcdfb_enable(fb, regs.cntl);
309
310#ifdef DEBUG
Joe Perchesad361c92009-07-06 13:05:40 -0700311 printk(KERN_INFO
312 "CLCD: Registers set to\n"
313 " %08x %08x %08x %08x\n"
314 " %08x %08x %08x %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
316 readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
317 readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
Russell King3f175222010-02-12 14:32:01 +0000318 readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#endif
320
321 return 0;
322}
323
324static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
325{
326 unsigned int mask = (1 << bf->length) - 1;
327
328 return (val >> (16 - bf->length) & mask) << bf->offset;
329}
330
331/*
332 * Set a single color register. The values supplied have a 16 bit
333 * magnitude. Return != 0 for invalid regno.
334 */
335static int
336clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
337 unsigned int blue, unsigned int transp, struct fb_info *info)
338{
339 struct clcd_fb *fb = to_clcd(info);
340
341 if (regno < 16)
342 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
343 convert_bitfield(blue, &fb->fb.var.blue) |
344 convert_bitfield(green, &fb->fb.var.green) |
345 convert_bitfield(red, &fb->fb.var.red);
346
Russell King1ddb8a12005-04-30 22:39:51 +0100347 if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
349 u32 val, mask, newval;
350
351 newval = (red >> 11) & 0x001f;
352 newval |= (green >> 6) & 0x03e0;
353 newval |= (blue >> 1) & 0x7c00;
354
355 /*
356 * 3.2.11: if we're configured for big endian
357 * byte order, the palette entries are swapped.
358 */
359 if (fb->clcd_cntl & CNTL_BEBO)
360 regno ^= 1;
361
362 if (regno & 1) {
363 newval <<= 16;
364 mask = 0x0000ffff;
365 } else {
366 mask = 0xffff0000;
367 }
368
369 val = readl(fb->regs + hw_reg) & mask;
370 writel(val | newval, fb->regs + hw_reg);
371 }
372
373 return regno > 255;
374}
375
376/*
377 * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
378 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
379 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
380 * to e.g. a video mode which doesn't support it. Implements VESA suspend
381 * and powerdown modes on hardware that supports disabling hsync/vsync:
382 * blank_mode == 2: suspend vsync
383 * blank_mode == 3: suspend hsync
384 * blank_mode == 4: powerdown
385 */
386static int clcdfb_blank(int blank_mode, struct fb_info *info)
387{
388 struct clcd_fb *fb = to_clcd(info);
389
390 if (blank_mode != 0) {
391 clcdfb_disable(fb);
392 } else {
393 clcdfb_enable(fb, fb->clcd_cntl);
394 }
395 return 0;
396}
Jon Medhurst778a0242013-03-28 15:57:56 +0000397int clcdfb_mmap_dma(struct clcd_fb *fb, struct vm_area_struct *vma)
398{
399 return dma_mmap_writecombine(&fb->dev->dev, vma,
400 fb->fb.screen_base,
401 fb->fb.fix.smem_start,
402 fb->fb.fix.smem_len);
403}
404
405void clcdfb_remove_dma(struct clcd_fb *fb)
406{
407 dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
408 fb->fb.screen_base, fb->fb.fix.smem_start);
409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Christoph Hellwig216d5262006-01-14 13:21:25 -0800411static int clcdfb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct vm_area_struct *vma)
413{
414 struct clcd_fb *fb = to_clcd(info);
415 unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
416 int ret = -EINVAL;
417
418 len = info->fix.smem_len;
419
420 if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
421 fb->board->mmap)
422 ret = fb->board->mmap(fb, vma);
423
424 return ret;
425}
426
427static struct fb_ops clcdfb_ops = {
428 .owner = THIS_MODULE,
429 .fb_check_var = clcdfb_check_var,
430 .fb_set_par = clcdfb_set_par,
431 .fb_setcolreg = clcdfb_setcolreg,
432 .fb_blank = clcdfb_blank,
433 .fb_fillrect = cfb_fillrect,
434 .fb_copyarea = cfb_copyarea,
435 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 .fb_mmap = clcdfb_mmap,
437};
438
439static int clcdfb_register(struct clcd_fb *fb)
440{
441 int ret;
442
Russell King3f175222010-02-12 14:32:01 +0000443 /*
444 * ARM PL111 always has IENB at 0x1c; it's only PL110
445 * which is reversed on some platforms.
446 */
447 if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
448 fb->off_ienb = CLCD_PL111_IENB;
449 fb->off_cntl = CLCD_PL111_CNTL;
450 } else {
451#ifdef CONFIG_ARCH_VERSATILE
452 fb->off_ienb = CLCD_PL111_IENB;
453 fb->off_cntl = CLCD_PL111_CNTL;
454#else
455 fb->off_ienb = CLCD_PL110_IENB;
456 fb->off_cntl = CLCD_PL110_CNTL;
457#endif
458 }
459
Russell Kingee569c42008-11-30 17:38:14 +0000460 fb->clk = clk_get(&fb->dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (IS_ERR(fb->clk)) {
462 ret = PTR_ERR(fb->clk);
463 goto out;
464 }
465
Russell King99df4ee2011-09-22 12:34:31 +0100466 ret = clk_prepare(fb->clk);
467 if (ret)
468 goto free_clk;
469
Loïc Minier17e8c4e2011-06-20 20:44:17 +0000470 fb->fb.device = &fb->dev->dev;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 fb->fb.fix.mmio_start = fb->dev->res.start;
Linus Walleijdc890c22009-06-07 23:27:31 +0100473 fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
476 if (!fb->regs) {
477 printk(KERN_ERR "CLCD: unable to remap registers\n");
478 ret = -ENOMEM;
Russell King99df4ee2011-09-22 12:34:31 +0100479 goto clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 fb->fb.fbops = &clcdfb_ops;
483 fb->fb.flags = FBINFO_FLAG_DEFAULT;
484 fb->fb.pseudo_palette = fb->cmap;
485
486 strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
487 fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
488 fb->fb.fix.type_aux = 0;
489 fb->fb.fix.xpanstep = 0;
490 fb->fb.fix.ypanstep = 0;
491 fb->fb.fix.ywrapstep = 0;
492 fb->fb.fix.accel = FB_ACCEL_NONE;
493
494 fb->fb.var.xres = fb->panel->mode.xres;
495 fb->fb.var.yres = fb->panel->mode.yres;
496 fb->fb.var.xres_virtual = fb->panel->mode.xres;
497 fb->fb.var.yres_virtual = fb->panel->mode.yres;
498 fb->fb.var.bits_per_pixel = fb->panel->bpp;
499 fb->fb.var.grayscale = fb->panel->grayscale;
500 fb->fb.var.pixclock = fb->panel->mode.pixclock;
501 fb->fb.var.left_margin = fb->panel->mode.left_margin;
502 fb->fb.var.right_margin = fb->panel->mode.right_margin;
503 fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
504 fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
505 fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
506 fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
507 fb->fb.var.sync = fb->panel->mode.sync;
508 fb->fb.var.vmode = fb->panel->mode.vmode;
509 fb->fb.var.activate = FB_ACTIVATE_NOW;
510 fb->fb.var.nonstd = 0;
511 fb->fb.var.height = fb->panel->height;
512 fb->fb.var.width = fb->panel->width;
513 fb->fb.var.accel_flags = 0;
514
515 fb->fb.monspecs.hfmin = 0;
516 fb->fb.monspecs.hfmax = 100000;
517 fb->fb.monspecs.vfmin = 0;
518 fb->fb.monspecs.vfmax = 400;
519 fb->fb.monspecs.dclkmin = 1000000;
520 fb->fb.monspecs.dclkmax = 100000000;
521
522 /*
523 * Make sure that the bitfields are set appropriately.
524 */
525 clcdfb_set_bitfields(fb, &fb->fb.var);
526
527 /*
528 * Allocate colourmap.
529 */
Andres Salomon909baf02009-03-31 15:25:29 -0700530 ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
531 if (ret)
532 goto unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /*
535 * Ensure interrupts are disabled.
536 */
Russell King3f175222010-02-12 14:32:01 +0000537 writel(0, fb->regs + fb->off_ienb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 fb_set_var(&fb->fb, &fb->fb.var);
540
Russell Kingff643322011-01-19 21:10:24 +0000541 dev_info(&fb->dev->dev, "%s hardware, %s display\n",
542 fb->board->name, fb->panel->mode.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 ret = register_framebuffer(&fb->fb);
545 if (ret == 0)
546 goto out;
547
548 printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
549
Andres Salomon909baf02009-03-31 15:25:29 -0700550 fb_dealloc_cmap(&fb->fb.cmap);
551 unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +0100553 clk_unprep:
554 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 free_clk:
556 clk_put(fb->clk);
557 out:
558 return ret;
559}
560
Jon Medhurst778a0242013-03-28 15:57:56 +0000561struct string_lookup {
562 const char *string;
563 const u32 val;
564};
565
566static struct string_lookup vmode_lookups[] = {
567 { "FB_VMODE_NONINTERLACED", FB_VMODE_NONINTERLACED},
568 { "FB_VMODE_INTERLACED", FB_VMODE_INTERLACED},
569 { "FB_VMODE_DOUBLE", FB_VMODE_DOUBLE},
570 { "FB_VMODE_ODD_FLD_FIRST", FB_VMODE_ODD_FLD_FIRST},
571 { NULL, 0 },
572};
573
574static struct string_lookup tim2_lookups[] = {
575 { "TIM2_CLKSEL", TIM2_CLKSEL},
576 { "TIM2_IVS", TIM2_IVS},
577 { "TIM2_IHS", TIM2_IHS},
578 { "TIM2_IPC", TIM2_IPC},
579 { "TIM2_IOE", TIM2_IOE},
580 { "TIM2_BCD", TIM2_BCD},
581 { NULL, 0},
582};
583static struct string_lookup cntl_lookups[] = {
584 {"CNTL_LCDEN", CNTL_LCDEN},
585 {"CNTL_LCDBPP1", CNTL_LCDBPP1},
586 {"CNTL_LCDBPP2", CNTL_LCDBPP2},
587 {"CNTL_LCDBPP4", CNTL_LCDBPP4},
588 {"CNTL_LCDBPP8", CNTL_LCDBPP8},
589 {"CNTL_LCDBPP16", CNTL_LCDBPP16},
590 {"CNTL_LCDBPP16_565", CNTL_LCDBPP16_565},
591 {"CNTL_LCDBPP16_444", CNTL_LCDBPP16_444},
592 {"CNTL_LCDBPP24", CNTL_LCDBPP24},
593 {"CNTL_LCDBW", CNTL_LCDBW},
594 {"CNTL_LCDTFT", CNTL_LCDTFT},
595 {"CNTL_LCDMONO8", CNTL_LCDMONO8},
596 {"CNTL_LCDDUAL", CNTL_LCDDUAL},
597 {"CNTL_BGR", CNTL_BGR},
598 {"CNTL_BEBO", CNTL_BEBO},
599 {"CNTL_BEPO", CNTL_BEPO},
600 {"CNTL_LCDPWR", CNTL_LCDPWR},
601 {"CNTL_LCDVCOMP(1)", CNTL_LCDVCOMP(1)},
602 {"CNTL_LCDVCOMP(2)", CNTL_LCDVCOMP(2)},
603 {"CNTL_LCDVCOMP(3)", CNTL_LCDVCOMP(3)},
604 {"CNTL_LCDVCOMP(4)", CNTL_LCDVCOMP(4)},
605 {"CNTL_LCDVCOMP(5)", CNTL_LCDVCOMP(5)},
606 {"CNTL_LCDVCOMP(6)", CNTL_LCDVCOMP(6)},
607 {"CNTL_LCDVCOMP(7)", CNTL_LCDVCOMP(7)},
608 {"CNTL_LDMAFIFOTIME", CNTL_LDMAFIFOTIME},
609 {"CNTL_WATERMARK", CNTL_WATERMARK},
610 { NULL, 0},
611};
612static struct string_lookup caps_lookups[] = {
613 {"CLCD_CAP_RGB444", CLCD_CAP_RGB444},
614 {"CLCD_CAP_RGB5551", CLCD_CAP_RGB5551},
615 {"CLCD_CAP_RGB565", CLCD_CAP_RGB565},
616 {"CLCD_CAP_RGB888", CLCD_CAP_RGB888},
617 {"CLCD_CAP_BGR444", CLCD_CAP_BGR444},
618 {"CLCD_CAP_BGR5551", CLCD_CAP_BGR5551},
619 {"CLCD_CAP_BGR565", CLCD_CAP_BGR565},
620 {"CLCD_CAP_BGR888", CLCD_CAP_BGR888},
621 {"CLCD_CAP_444", CLCD_CAP_444},
622 {"CLCD_CAP_5551", CLCD_CAP_5551},
623 {"CLCD_CAP_565", CLCD_CAP_565},
624 {"CLCD_CAP_888", CLCD_CAP_888},
625 {"CLCD_CAP_RGB", CLCD_CAP_RGB},
626 {"CLCD_CAP_BGR", CLCD_CAP_BGR},
627 {"CLCD_CAP_ALL", CLCD_CAP_ALL},
628 { NULL, 0},
629};
630
631u32 parse_setting(struct string_lookup *lookup, const char *name)
632{
633 int i = 0;
634 while (lookup[i].string != NULL) {
635 if (strcmp(lookup[i].string, name) == 0)
636 return lookup[i].val;
637 ++i;
638 }
639 return -EINVAL;
640}
641
642u32 get_string_lookup(struct device_node *node, const char *name,
643 struct string_lookup *lookup)
644{
645 const char *string;
646 int count, i, ret = 0;
647
648 count = of_property_count_strings(node, name);
649 if (count >= 0)
650 for (i = 0; i < count; i++)
651 if (of_property_read_string_index(node, name, i,
652 &string) == 0)
653 ret |= parse_setting(lookup, string);
654 return ret;
655}
656
657int get_val(struct device_node *node, const char *string)
658{
659 u32 ret = 0;
660
661 if (of_property_read_u32(node, string, &ret))
662 ret = -1;
663 return ret;
664}
665
666struct clcd_panel *getPanel(struct device_node *node)
667{
668 static struct clcd_panel panel;
669
670 panel.mode.refresh = get_val(node, "refresh");
671 panel.mode.xres = get_val(node, "xres");
672 panel.mode.yres = get_val(node, "yres");
673 panel.mode.pixclock = get_val(node, "pixclock");
674 panel.mode.left_margin = get_val(node, "left_margin");
675 panel.mode.right_margin = get_val(node, "right_margin");
676 panel.mode.upper_margin = get_val(node, "upper_margin");
677 panel.mode.lower_margin = get_val(node, "lower_margin");
678 panel.mode.hsync_len = get_val(node, "hsync_len");
679 panel.mode.vsync_len = get_val(node, "vsync_len");
680 panel.mode.sync = get_val(node, "sync");
681 panel.bpp = get_val(node, "bpp");
682 panel.width = (signed short) get_val(node, "width");
683 panel.height = (signed short) get_val(node, "height");
684
685 panel.mode.vmode = get_string_lookup(node, "vmode", vmode_lookups);
686 panel.tim2 = get_string_lookup(node, "tim2", tim2_lookups);
687 panel.cntl = get_string_lookup(node, "cntl", cntl_lookups);
688 panel.caps = get_string_lookup(node, "caps", caps_lookups);
689
690 return &panel;
691}
692
693struct clcd_panel *clcdfb_get_panel(const char *name)
694{
695 struct device_node *node = NULL;
696 const char *mode;
697 struct clcd_panel *panel = NULL;
698
699 do {
700 node = of_find_compatible_node(node, NULL, "panel");
701 if (node)
702 if (of_property_read_string(node, "mode", &mode) == 0)
703 if (strcmp(mode, name) == 0) {
704 panel = getPanel(node);
705 panel->mode.name = name;
706 }
707 } while (node != NULL);
708
709 return panel;
710}
711
712#ifdef CONFIG_OF
713static int clcdfb_dt_init(struct clcd_fb *fb)
714{
715 int err = 0;
716 struct device_node *node;
717 const char *mode;
718 dma_addr_t dma;
719 u32 use_dma;
720 const __be32 *prop;
721 int len, na, ns;
722 phys_addr_t fb_base, fb_size;
723
724 node = fb->dev->dev.of_node;
725 if (!node)
726 return -ENODEV;
727
728 na = of_n_addr_cells(node);
729 ns = of_n_size_cells(node);
730
731 if (WARN_ON(of_property_read_string(node, "mode", &mode)))
732 return -ENODEV;
733
734 fb->panel = clcdfb_get_panel(mode);
735 if (!fb->panel)
736 return -EINVAL;
737 fb->fb.fix.smem_len = fb->panel->mode.xres * fb->panel->mode.yres * 2;
738
739 if (of_property_read_u32(node, "use_dma", &use_dma))
740 use_dma = 0;
741 if (use_dma) {
742 fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev,
743 fb->fb.fix.smem_len, &dma, GFP_KERNEL);
744 if (!fb->fb.screen_base) {
745 pr_err("CLCD: unable to map framebuffer\n");
746 err = -ENOMEM;
747 } else
748 fb->fb.fix.smem_start = dma;
749 } else {
750 prop = of_get_property(node, "framebuffer", &len);
751 if (WARN_ON(!prop || len < (na + ns) * sizeof(*prop)))
752 return -EINVAL;
753 fb_base = of_read_number(prop, na);
754 fb_size = of_read_number(prop + na, ns);
755
756 fb->fb.fix.smem_start = fb_base;
757 fb->fb.screen_base = ioremap_wc(fb->fb.fix.smem_start, fb_size);
758 }
759 return err;
760}
761#endif /* CONFIG_OF */
762
Russell Kingaa25afa2011-02-19 15:55:00 +0000763static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct clcd_board *board = dev->dev.platform_data;
766 struct clcd_fb *fb;
767 int ret;
768
Jon Medhurst778a0242013-03-28 15:57:56 +0000769#ifdef CONFIG_OF
770 if (dev->dev.of_node) {
Jon Medhurst778a0242013-03-28 15:57:56 +0000771 if (!board) {
772 board = kzalloc(sizeof(struct clcd_board), GFP_KERNEL);
773 if (!board)
774 return -EINVAL;
775 board->name = "Device Tree CLCD PL111";
776 board->caps = CLCD_CAP_5551 | CLCD_CAP_565;
777 board->check = clcdfb_check;
778 board->decode = clcdfb_decode;
779 board->setup = clcdfb_dt_init;
780 board->mmap = clcdfb_mmap_dma;
781 board->remove = clcdfb_remove_dma;
782 }
783 }
784#endif /* CONFIG_OF */
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!board)
787 return -EINVAL;
788
789 ret = amba_request_regions(dev, NULL);
790 if (ret) {
791 printk(KERN_ERR "CLCD: unable to reserve regs region\n");
792 goto out;
793 }
794
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700795 fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!fb) {
797 printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
798 ret = -ENOMEM;
799 goto free_region;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 fb->dev = dev;
803 fb->board = board;
804
Russell Kingff643322011-01-19 21:10:24 +0000805 dev_info(&fb->dev->dev, "PL%03x rev%u at 0x%08llx\n",
806 amba_part(dev), amba_rev(dev),
807 (unsigned long long)dev->res.start);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 ret = fb->board->setup(fb);
810 if (ret)
811 goto free_fb;
812
813 ret = clcdfb_register(fb);
814 if (ret == 0) {
815 amba_set_drvdata(dev, fb);
816 goto out;
817 }
818
819 fb->board->remove(fb);
820 free_fb:
821 kfree(fb);
822 free_region:
823 amba_release_regions(dev);
824 out:
825 return ret;
826}
827
828static int clcdfb_remove(struct amba_device *dev)
829{
830 struct clcd_fb *fb = amba_get_drvdata(dev);
831
832 amba_set_drvdata(dev, NULL);
833
834 clcdfb_disable(fb);
835 unregister_framebuffer(&fb->fb);
Andres Salomon909baf02009-03-31 15:25:29 -0700836 if (fb->fb.cmap.len)
837 fb_dealloc_cmap(&fb->fb.cmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 iounmap(fb->regs);
Russell King99df4ee2011-09-22 12:34:31 +0100839 clk_unprepare(fb->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 clk_put(fb->clk);
841
842 fb->board->remove(fb);
843
844 kfree(fb);
845
846 amba_release_regions(dev);
847
848 return 0;
849}
850
851static struct amba_id clcdfb_id_table[] = {
852 {
853 .id = 0x00041110,
Russell Kinge8315562005-11-02 14:40:35 +0000854 .mask = 0x000ffffe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 },
856 { 0, 0 },
857};
858
Dave Martin6054f9b82011-10-05 15:15:23 +0100859MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861static struct amba_driver clcd_driver = {
862 .drv = {
Russell Kinge8315562005-11-02 14:40:35 +0000863 .name = "clcd-pl11x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 },
865 .probe = clcdfb_probe,
866 .remove = clcdfb_remove,
867 .id_table = clcdfb_id_table,
868};
869
Russell King2c250132005-11-08 14:44:15 +0000870static int __init amba_clcdfb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 if (fb_get_options("ambafb", NULL))
873 return -ENODEV;
874
875 return amba_driver_register(&clcd_driver);
876}
877
878module_init(amba_clcdfb_init);
879
880static void __exit amba_clcdfb_exit(void)
881{
882 amba_driver_unregister(&clcd_driver);
883}
884
885module_exit(amba_clcdfb_exit);
886
887MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
888MODULE_LICENSE("GPL");