blob: dec602c2307553f85b2faf00825877fecdaf35d3 [file] [log] [blame]
Andrei Konovalov147394c2007-05-08 00:40:18 -07001/*
2 * xilinxfb.c
3 *
4 * Xilinx TFT LCD frame buffer driver
5 *
6 * Author: MontaVista Software, Inc.
7 * source@mvista.com
8 *
Grant Likely31e8d462007-10-04 10:48:37 -06009 * 2002-2007 (c) MontaVista Software, Inc.
10 * 2007 (c) Secret Lab Technologies, Ltd.
11 *
12 * This file is licensed under the terms of the GNU General Public License
13 * version 2. This program is licensed "as is" without any warranty of any
14 * kind, whether express or implied.
Andrei Konovalov147394c2007-05-08 00:40:18 -070015 */
16
17/*
18 * This driver was based on au1100fb.c by MontaVista rewritten for 2.6
19 * by Embedded Alley Solutions <source@embeddedalley.com>, which in turn
20 * was based on skeletonfb.c, Skeleton for a frame buffer device by
21 * Geert Uytterhoeven.
22 */
23
Grant Likely3cb3ec22007-10-04 10:48:36 -060024#include <linux/device.h>
Andrei Konovalov147394c2007-05-08 00:40:18 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/version.h>
28#include <linux/errno.h>
29#include <linux/string.h>
30#include <linux/mm.h>
31#include <linux/fb.h>
32#include <linux/init.h>
33#include <linux/dma-mapping.h>
34#include <linux/platform_device.h>
Grant Likely31e8d462007-10-04 10:48:37 -060035#if defined(CONFIG_OF)
36#include <linux/of_device.h>
37#include <linux/of_platform.h>
38#endif
Andrei Konovalov147394c2007-05-08 00:40:18 -070039#include <asm/io.h>
Grant Likelydc8afdc2007-10-01 07:47:00 +100040#include <linux/xilinxfb.h>
Andrei Konovalov147394c2007-05-08 00:40:18 -070041
42#define DRIVER_NAME "xilinxfb"
43#define DRIVER_DESCRIPTION "Xilinx TFT LCD frame buffer driver"
44
45/*
46 * Xilinx calls it "PLB TFT LCD Controller" though it can also be used for
47 * the VGA port on the Xilinx ML40x board. This is a hardware display controller
48 * for a 640x480 resolution TFT or VGA screen.
49 *
50 * The interface to the framebuffer is nice and simple. There are two
51 * control registers. The first tells the LCD interface where in memory
52 * the frame buffer is (only the 11 most significant bits are used, so
53 * don't start thinking about scrolling). The second allows the LCD to
54 * be turned on or off as well as rotated 180 degrees.
55 */
56#define NUM_REGS 2
57#define REG_FB_ADDR 0
58#define REG_CTRL 1
59#define REG_CTRL_ENABLE 0x0001
60#define REG_CTRL_ROTATE 0x0002
61
62/*
63 * The hardware only handles a single mode: 640x480 24 bit true
64 * color. Each pixel gets a word (32 bits) of memory. Within each word,
65 * the 8 most significant bits are ignored, the next 8 bits are the red
66 * level, the next 8 bits are the green level and the 8 least
67 * significant bits are the blue level. Each row of the LCD uses 1024
68 * words, but only the first 640 pixels are displayed with the other 384
69 * words being ignored. There are 480 rows.
70 */
71#define BYTES_PER_PIXEL 4
72#define BITS_PER_PIXEL (BYTES_PER_PIXEL * 8)
73#define XRES 640
74#define YRES 480
75#define XRES_VIRTUAL 1024
76#define YRES_VIRTUAL YRES
77#define LINE_LENGTH (XRES_VIRTUAL * BYTES_PER_PIXEL)
78#define FB_SIZE (YRES_VIRTUAL * LINE_LENGTH)
79
80#define RED_SHIFT 16
81#define GREEN_SHIFT 8
82#define BLUE_SHIFT 0
83
84#define PALETTE_ENTRIES_NO 16 /* passed to fb_alloc_cmap() */
85
86/*
87 * Here are the default fb_fix_screeninfo and fb_var_screeninfo structures
88 */
Grant Likely3f5b85d2007-07-31 00:37:38 -070089static struct fb_fix_screeninfo xilinx_fb_fix = {
Andrei Konovalov147394c2007-05-08 00:40:18 -070090 .id = "Xilinx",
91 .type = FB_TYPE_PACKED_PIXELS,
92 .visual = FB_VISUAL_TRUECOLOR,
93 .smem_len = FB_SIZE,
94 .line_length = LINE_LENGTH,
95 .accel = FB_ACCEL_NONE
96};
97
Grant Likely3f5b85d2007-07-31 00:37:38 -070098static struct fb_var_screeninfo xilinx_fb_var = {
Andrei Konovalov147394c2007-05-08 00:40:18 -070099 .xres = XRES,
100 .yres = YRES,
101 .xres_virtual = XRES_VIRTUAL,
102 .yres_virtual = YRES_VIRTUAL,
103
104 .bits_per_pixel = BITS_PER_PIXEL,
105
106 .red = { RED_SHIFT, 8, 0 },
107 .green = { GREEN_SHIFT, 8, 0 },
108 .blue = { BLUE_SHIFT, 8, 0 },
109 .transp = { 0, 0, 0 },
110
111 .activate = FB_ACTIVATE_NOW
112};
113
114struct xilinxfb_drvdata {
115
116 struct fb_info info; /* FB driver info record */
117
118 u32 regs_phys; /* phys. address of the control registers */
119 u32 __iomem *regs; /* virt. address of the control registers */
120
Grant Likelyb9a22792007-10-04 10:48:37 -0600121 void *fb_virt; /* virt. address of the frame buffer */
Andrei Konovalov147394c2007-05-08 00:40:18 -0700122 dma_addr_t fb_phys; /* phys. address of the frame buffer */
123
124 u32 reg_ctrl_default;
125
126 u32 pseudo_palette[PALETTE_ENTRIES_NO];
127 /* Fake palette of 16 colors */
128};
129
130#define to_xilinxfb_drvdata(_info) \
131 container_of(_info, struct xilinxfb_drvdata, info)
132
133/*
134 * The LCD controller has DCR interface to its registers, but all
135 * the boards and configurations the driver has been tested with
136 * use opb2dcr bridge. So the registers are seen as memory mapped.
137 * This macro is to make it simple to add the direct DCR access
138 * when it's needed.
139 */
140#define xilinx_fb_out_be32(driverdata, offset, val) \
141 out_be32(driverdata->regs + offset, val)
142
143static int
144xilinx_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
145 unsigned transp, struct fb_info *fbi)
146{
147 u32 *palette = fbi->pseudo_palette;
148
149 if (regno >= PALETTE_ENTRIES_NO)
150 return -EINVAL;
151
152 if (fbi->var.grayscale) {
153 /* Convert color to grayscale.
154 * grayscale = 0.30*R + 0.59*G + 0.11*B */
155 red = green = blue =
156 (red * 77 + green * 151 + blue * 28 + 127) >> 8;
157 }
158
159 /* fbi->fix.visual is always FB_VISUAL_TRUECOLOR */
160
161 /* We only handle 8 bits of each color. */
162 red >>= 8;
163 green >>= 8;
164 blue >>= 8;
165 palette[regno] = (red << RED_SHIFT) | (green << GREEN_SHIFT) |
166 (blue << BLUE_SHIFT);
167
168 return 0;
169}
170
171static int
172xilinx_fb_blank(int blank_mode, struct fb_info *fbi)
173{
174 struct xilinxfb_drvdata *drvdata = to_xilinxfb_drvdata(fbi);
175
176 switch (blank_mode) {
177 case FB_BLANK_UNBLANK:
178 /* turn on panel */
179 xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
180 break;
181
182 case FB_BLANK_NORMAL:
183 case FB_BLANK_VSYNC_SUSPEND:
184 case FB_BLANK_HSYNC_SUSPEND:
185 case FB_BLANK_POWERDOWN:
186 /* turn off panel */
187 xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
188 default:
189 break;
190
191 }
192 return 0; /* success */
193}
194
195static struct fb_ops xilinxfb_ops =
196{
197 .owner = THIS_MODULE,
198 .fb_setcolreg = xilinx_fb_setcolreg,
199 .fb_blank = xilinx_fb_blank,
200 .fb_fillrect = cfb_fillrect,
201 .fb_copyarea = cfb_copyarea,
202 .fb_imageblit = cfb_imageblit,
203};
204
Grant Likely26477622007-10-04 10:48:37 -0600205/* ---------------------------------------------------------------------
206 * Bus independent setup/teardown
207 */
Andrei Konovalov147394c2007-05-08 00:40:18 -0700208
Grant Likely26477622007-10-04 10:48:37 -0600209static int xilinxfb_assign(struct device *dev, unsigned long physaddr,
210 int width_mm, int height_mm, int rotate)
Andrei Konovalov147394c2007-05-08 00:40:18 -0700211{
Andrei Konovalov147394c2007-05-08 00:40:18 -0700212 struct xilinxfb_drvdata *drvdata;
Grant Likely26477622007-10-04 10:48:37 -0600213 int rc;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700214
Grant Likely26477622007-10-04 10:48:37 -0600215 /* Allocate the driver data region */
Andrei Konovalov147394c2007-05-08 00:40:18 -0700216 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
217 if (!drvdata) {
Grant Likely3cb3ec22007-10-04 10:48:36 -0600218 dev_err(dev, "Couldn't allocate device private record\n");
Andrei Konovalov147394c2007-05-08 00:40:18 -0700219 return -ENOMEM;
220 }
221 dev_set_drvdata(dev, drvdata);
222
223 /* Map the control registers in */
Grant Likely26477622007-10-04 10:48:37 -0600224 if (!request_mem_region(physaddr, 8, DRIVER_NAME)) {
225 dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
226 physaddr);
227 rc = -ENODEV;
Grant Likely3fb99ce2007-10-04 10:48:37 -0600228 goto err_region;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700229 }
Grant Likely26477622007-10-04 10:48:37 -0600230 drvdata->regs_phys = physaddr;
231 drvdata->regs = ioremap(physaddr, 8);
232 if (!drvdata->regs) {
233 dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
234 physaddr);
235 rc = -ENODEV;
236 goto err_map;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700237 }
Andrei Konovalov147394c2007-05-08 00:40:18 -0700238
239 /* Allocate the framebuffer memory */
240 drvdata->fb_virt = dma_alloc_coherent(dev, PAGE_ALIGN(FB_SIZE),
241 &drvdata->fb_phys, GFP_KERNEL);
242 if (!drvdata->fb_virt) {
Grant Likely3cb3ec22007-10-04 10:48:36 -0600243 dev_err(dev, "Could not allocate frame buffer memory\n");
Grant Likely26477622007-10-04 10:48:37 -0600244 rc = -ENOMEM;
Grant Likely3fb99ce2007-10-04 10:48:37 -0600245 goto err_fbmem;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700246 }
247
248 /* Clear (turn to black) the framebuffer */
Grant Likelyb9a22792007-10-04 10:48:37 -0600249 memset_io((void __iomem *)drvdata->fb_virt, 0, FB_SIZE);
Andrei Konovalov147394c2007-05-08 00:40:18 -0700250
251 /* Tell the hardware where the frame buffer is */
252 xilinx_fb_out_be32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
253
254 /* Turn on the display */
Grant Likelyf53161d2007-07-31 00:37:39 -0700255 drvdata->reg_ctrl_default = REG_CTRL_ENABLE;
Grant Likely26477622007-10-04 10:48:37 -0600256 if (rotate)
Grant Likelyf53161d2007-07-31 00:37:39 -0700257 drvdata->reg_ctrl_default |= REG_CTRL_ROTATE;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700258 xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
259
260 /* Fill struct fb_info */
261 drvdata->info.device = dev;
Grant Likelyb9a22792007-10-04 10:48:37 -0600262 drvdata->info.screen_base = (void __iomem *)drvdata->fb_virt;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700263 drvdata->info.fbops = &xilinxfb_ops;
264 drvdata->info.fix = xilinx_fb_fix;
265 drvdata->info.fix.smem_start = drvdata->fb_phys;
266 drvdata->info.pseudo_palette = drvdata->pseudo_palette;
Grant Likely26477622007-10-04 10:48:37 -0600267 drvdata->info.flags = FBINFO_DEFAULT;
268 drvdata->info.var = xilinx_fb_var;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700269
Grant Likely26477622007-10-04 10:48:37 -0600270 xilinx_fb_var.height = height_mm;
271 xilinx_fb_var.width = width_mm;
272
273 /* Allocate a colour map */
274 rc = fb_alloc_cmap(&drvdata->info.cmap, PALETTE_ENTRIES_NO, 0);
275 if (rc) {
Grant Likely3cb3ec22007-10-04 10:48:36 -0600276 dev_err(dev, "Fail to allocate colormap (%d entries)\n",
Andrei Konovalov147394c2007-05-08 00:40:18 -0700277 PALETTE_ENTRIES_NO);
Grant Likely3fb99ce2007-10-04 10:48:37 -0600278 goto err_cmap;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700279 }
280
Andrei Konovalov147394c2007-05-08 00:40:18 -0700281 /* Register new frame buffer */
Grant Likely26477622007-10-04 10:48:37 -0600282 rc = register_framebuffer(&drvdata->info);
283 if (rc) {
Grant Likely3cb3ec22007-10-04 10:48:36 -0600284 dev_err(dev, "Could not register frame buffer\n");
Grant Likely3fb99ce2007-10-04 10:48:37 -0600285 goto err_regfb;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700286 }
287
Grant Likely258de4b2007-10-04 10:48:36 -0600288 /* Put a banner in the log (for DEBUG) */
Grant Likely26477622007-10-04 10:48:37 -0600289 dev_dbg(dev, "regs: phys=%lx, virt=%p\n", physaddr, drvdata->regs);
Grant Likely258de4b2007-10-04 10:48:36 -0600290 dev_dbg(dev, "fb: phys=%p, virt=%p, size=%x\n",
291 (void*)drvdata->fb_phys, drvdata->fb_virt, FB_SIZE);
Andrei Konovalov147394c2007-05-08 00:40:18 -0700292 return 0; /* success */
293
Grant Likely3fb99ce2007-10-04 10:48:37 -0600294err_regfb:
Andrei Konovalov147394c2007-05-08 00:40:18 -0700295 fb_dealloc_cmap(&drvdata->info.cmap);
296
Grant Likely3fb99ce2007-10-04 10:48:37 -0600297err_cmap:
Andrei Konovalov147394c2007-05-08 00:40:18 -0700298 dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
299 drvdata->fb_phys);
Andrei Konovalov147394c2007-05-08 00:40:18 -0700300 /* Turn off the display */
301 xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
Andrei Konovalov147394c2007-05-08 00:40:18 -0700302
Grant Likely3fb99ce2007-10-04 10:48:37 -0600303err_fbmem:
Grant Likely26477622007-10-04 10:48:37 -0600304 iounmap(drvdata->regs);
305
306err_map:
307 release_mem_region(physaddr, 8);
Andrei Konovalov147394c2007-05-08 00:40:18 -0700308
Grant Likely3fb99ce2007-10-04 10:48:37 -0600309err_region:
Andrei Konovalov147394c2007-05-08 00:40:18 -0700310 kfree(drvdata);
311 dev_set_drvdata(dev, NULL);
312
Grant Likely26477622007-10-04 10:48:37 -0600313 return rc;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700314}
315
Grant Likely26477622007-10-04 10:48:37 -0600316static int xilinxfb_release(struct device *dev)
Andrei Konovalov147394c2007-05-08 00:40:18 -0700317{
Grant Likely26477622007-10-04 10:48:37 -0600318 struct xilinxfb_drvdata *drvdata = dev_get_drvdata(dev);
Andrei Konovalov147394c2007-05-08 00:40:18 -0700319
320#if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
321 xilinx_fb_blank(VESA_POWERDOWN, &drvdata->info);
322#endif
323
324 unregister_framebuffer(&drvdata->info);
325
326 fb_dealloc_cmap(&drvdata->info.cmap);
327
328 dma_free_coherent(dev, PAGE_ALIGN(FB_SIZE), drvdata->fb_virt,
329 drvdata->fb_phys);
330
331 /* Turn off the display */
332 xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
333 iounmap(drvdata->regs);
334
335 release_mem_region(drvdata->regs_phys, 8);
336
337 kfree(drvdata);
338 dev_set_drvdata(dev, NULL);
339
340 return 0;
341}
342
Grant Likely26477622007-10-04 10:48:37 -0600343/* ---------------------------------------------------------------------
344 * Platform bus binding
345 */
346
347static int
Grant Likely47473e32007-10-04 10:48:37 -0600348xilinxfb_platform_probe(struct platform_device *pdev)
Grant Likely26477622007-10-04 10:48:37 -0600349{
Grant Likely26477622007-10-04 10:48:37 -0600350 struct xilinxfb_platform_data *pdata;
351 struct resource *res;
Grant Likelye3cec002007-10-04 15:44:52 -0600352 int width_mm = 0;
353 int height_mm = 0;
354 int rotate = 0;
Grant Likely26477622007-10-04 10:48:37 -0600355
356 /* Find the registers address */
357 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
358 if (!res) {
Grant Likely47473e32007-10-04 10:48:37 -0600359 dev_err(&pdev->dev, "Couldn't get registers resource\n");
Grant Likely26477622007-10-04 10:48:37 -0600360 return -ENODEV;
361 }
362
Grant Likelye3cec002007-10-04 15:44:52 -0600363 /* If a pdata structure is provided, then extract the parameters */
364 pdata = pdev->dev.platform_data;
365 if (pdata) {
366 height_mm = pdata->screen_height_mm;
367 width_mm = pdata->screen_width_mm;
368 rotate = pdata->rotate_screen ? 1 : 0;
369 }
Grant Likely26477622007-10-04 10:48:37 -0600370
Grant Likely47473e32007-10-04 10:48:37 -0600371 return xilinxfb_assign(&pdev->dev, res->start, width_mm, height_mm,
372 rotate);
Grant Likely26477622007-10-04 10:48:37 -0600373}
374
375static int
Grant Likely47473e32007-10-04 10:48:37 -0600376xilinxfb_platform_remove(struct platform_device *pdev)
Grant Likely26477622007-10-04 10:48:37 -0600377{
Grant Likely47473e32007-10-04 10:48:37 -0600378 return xilinxfb_release(&pdev->dev);
Grant Likely26477622007-10-04 10:48:37 -0600379}
380
Andrei Konovalov147394c2007-05-08 00:40:18 -0700381
Grant Likely47473e32007-10-04 10:48:37 -0600382static struct platform_driver xilinxfb_platform_driver = {
383 .probe = xilinxfb_platform_probe,
384 .remove = xilinxfb_platform_remove,
385 .driver = {
386 .owner = THIS_MODULE,
387 .name = DRIVER_NAME,
388 },
Andrei Konovalov147394c2007-05-08 00:40:18 -0700389};
390
Grant Likely31e8d462007-10-04 10:48:37 -0600391/* ---------------------------------------------------------------------
392 * OF bus binding
393 */
394
395#if defined(CONFIG_OF)
396static int __devinit
397xilinxfb_of_probe(struct of_device *op, const struct of_device_id *match)
398{
399 struct resource res;
400 const u32 *prop;
401 int width = 0, height = 0, rotate = 0;
402 int size, rc;
403
404 dev_dbg(&op->dev, "xilinxfb_of_probe(%p, %p)\n", op, match);
405
406 rc = of_address_to_resource(op->node, 0, &res);
407 if (rc) {
408 dev_err(&op->dev, "invalid address\n");
409 return rc;
410 }
411
412 prop = of_get_property(op->node, "display-number", &size);
413 if ((prop) && (size >= sizeof(u32)*2)) {
414 width = prop[0];
415 height = prop[1];
416 }
417
418 if (of_find_property(op->node, "rotate-display", NULL))
419 rotate = 1;
420
421 return xilinxfb_assign(&op->dev, res.start, width, height, rotate);
422}
423
424static int __devexit xilinxfb_of_remove(struct of_device *op)
425{
426 return xilinxfb_release(&op->dev);
427}
428
429/* Match table for of_platform binding */
430static struct of_device_id __devinit xilinxfb_of_match[] = {
431 { .compatible = "xilinx,ml300-fb", },
432 {},
433};
434MODULE_DEVICE_TABLE(of, xilinxfb_of_match);
435
436static struct of_platform_driver xilinxfb_of_driver = {
437 .owner = THIS_MODULE,
438 .name = DRIVER_NAME,
439 .match_table = xilinxfb_of_match,
440 .probe = xilinxfb_of_probe,
441 .remove = __devexit_p(xilinxfb_of_remove),
442 .driver = {
443 .name = DRIVER_NAME,
444 },
445};
446
447/* Registration helpers to keep the number of #ifdefs to a minimum */
448static inline int __init xilinxfb_of_register(void)
449{
450 pr_debug("xilinxfb: calling of_register_platform_driver()\n");
451 return of_register_platform_driver(&xilinxfb_of_driver);
452}
453
454static inline void __exit xilinxfb_of_unregister(void)
455{
456 of_unregister_platform_driver(&xilinxfb_of_driver);
457}
458#else /* CONFIG_OF */
459/* CONFIG_OF not enabled; do nothing helpers */
460static inline int __init xilinxfb_of_register(void) { return 0; }
461static inline void __exit xilinxfb_of_unregister(void) { }
462#endif /* CONFIG_OF */
463
464/* ---------------------------------------------------------------------
465 * Module setup and teardown
466 */
467
Andrei Konovalov147394c2007-05-08 00:40:18 -0700468static int __init
469xilinxfb_init(void)
470{
Grant Likely31e8d462007-10-04 10:48:37 -0600471 int rc;
472 rc = xilinxfb_of_register();
473 if (rc)
474 return rc;
475
476 rc = platform_driver_register(&xilinxfb_platform_driver);
477 if (rc)
478 xilinxfb_of_unregister();
479
480 return rc;
Andrei Konovalov147394c2007-05-08 00:40:18 -0700481}
482
483static void __exit
484xilinxfb_cleanup(void)
485{
Grant Likely47473e32007-10-04 10:48:37 -0600486 platform_driver_unregister(&xilinxfb_platform_driver);
Grant Likely31e8d462007-10-04 10:48:37 -0600487 xilinxfb_of_unregister();
Andrei Konovalov147394c2007-05-08 00:40:18 -0700488}
489
490module_init(xilinxfb_init);
491module_exit(xilinxfb_cleanup);
492
493MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
494MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
495MODULE_LICENSE("GPL");