1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#include <linux/clk.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/errno.h>
48#include <linux/string.h>
49#include <linux/mm.h>
50#include <linux/fb.h>
51#include <linux/init.h>
52#include <linux/interrupt.h>
53#include <linux/ctype.h>
54#include <linux/dma-mapping.h>
55#include <linux/platform_device.h>
56#include <linux/slab.h>
57
58#include <asm/mach-au1x00/au1000.h>
59
60#define DEBUG 0
61
62#include "au1100fb.h"
63
64#define DRIVER_NAME "au1100fb"
65#define DRIVER_DESC "LCD controller driver for AU1100 processors"
66
67#define to_au1100fb_device(_info) \
68 (_info ? container_of(_info, struct au1100fb_device, info) : NULL);
69
70
71
72
73
74struct fb_bitfield rgb_bitfields[][4] =
75{
76
77 { { 10, 6, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
78 { { 11, 5, 0 }, { 5, 6, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
79 { { 11, 5, 0 }, { 6, 5, 0 }, { 0, 6, 0 }, { 0, 0, 0 } },
80 { { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 15, 1, 0 } },
81 { { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 1, 0 } },
82
83
84 { { 8, 4, 0 }, { 4, 4, 0 }, { 0, 4, 0 }, { 0, 0, 0 } },
85};
86
87static struct fb_fix_screeninfo au1100fb_fix = {
88 .id = "AU1100 FB",
89 .xpanstep = 1,
90 .ypanstep = 1,
91 .type = FB_TYPE_PACKED_PIXELS,
92 .accel = FB_ACCEL_NONE,
93};
94
95static struct fb_var_screeninfo au1100fb_var = {
96 .activate = FB_ACTIVATE_NOW,
97 .height = -1,
98 .width = -1,
99 .vmode = FB_VMODE_NONINTERLACED,
100};
101
102
103
104
105
106static int au1100fb_fb_blank(int blank_mode, struct fb_info *fbi)
107{
108 struct au1100fb_device *fbdev = to_au1100fb_device(fbi);
109
110 print_dbg("fb_blank %d %p", blank_mode, fbi);
111
112 switch (blank_mode) {
113
114 case VESA_NO_BLANKING:
115
116 fbdev->regs->lcd_control |= LCD_CONTROL_GO;
117 wmb();
118 break;
119
120 case VESA_VSYNC_SUSPEND:
121 case VESA_HSYNC_SUSPEND:
122 case VESA_POWERDOWN:
123
124 fbdev->regs->lcd_control &= ~LCD_CONTROL_GO;
125 wmb();
126 break;
127 default:
128 break;
129
130 }
131 return 0;
132}
133
134
135
136
137
138int au1100fb_setmode(struct au1100fb_device *fbdev)
139{
140 struct fb_info *info = &fbdev->info;
141 u32 words;
142 int index;
143
144 if (!fbdev)
145 return -EINVAL;
146
147
148 if (panel_is_active(fbdev->panel) || panel_is_color(fbdev->panel)) {
149 if (info->var.bits_per_pixel <= 8) {
150
151 info->var.red.offset = 0;
152 info->var.red.length = info->var.bits_per_pixel;
153 info->var.red.msb_right = 0;
154
155 info->var.green.offset = 0;
156 info->var.green.length = info->var.bits_per_pixel;
157 info->var.green.msb_right = 0;
158
159 info->var.blue.offset = 0;
160 info->var.blue.length = info->var.bits_per_pixel;
161 info->var.blue.msb_right = 0;
162
163 info->var.transp.offset = 0;
164 info->var.transp.length = 0;
165 info->var.transp.msb_right = 0;
166
167 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
168 info->fix.line_length = info->var.xres_virtual /
169 (8/info->var.bits_per_pixel);
170 } else {
171
172 index = (fbdev->panel->control_base & LCD_CONTROL_SBPPF_MASK) >> LCD_CONTROL_SBPPF_BIT;
173 info->var.red = rgb_bitfields[index][0];
174 info->var.green = rgb_bitfields[index][1];
175 info->var.blue = rgb_bitfields[index][2];
176 info->var.transp = rgb_bitfields[index][3];
177
178 info->fix.visual = FB_VISUAL_TRUECOLOR;
179 info->fix.line_length = info->var.xres_virtual << 1;
180 }
181 } else {
182
183 info->fix.visual = FB_VISUAL_MONO10;
184 info->fix.line_length = info->var.xres_virtual / 8;
185 }
186
187 info->screen_size = info->fix.line_length * info->var.yres_virtual;
188 info->var.rotate = ((fbdev->panel->control_base&LCD_CONTROL_SM_MASK) \
189 >> LCD_CONTROL_SM_BIT) * 90;
190
191
192 fbdev->regs->lcd_control = fbdev->panel->control_base;
193 fbdev->regs->lcd_horztiming = fbdev->panel->horztiming;
194 fbdev->regs->lcd_verttiming = fbdev->panel->verttiming;
195 fbdev->regs->lcd_clkcontrol = fbdev->panel->clkcontrol_base;
196 fbdev->regs->lcd_intenable = 0;
197 fbdev->regs->lcd_intstatus = 0;
198 fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(fbdev->fb_phys);
199
200 if (panel_is_dual(fbdev->panel)) {
201
202
203 if (info->var.yres_virtual >= (info->var.yres << 1)) {
204 fbdev->regs->lcd_dmaaddr1 = LCD_DMA_SA_N(fbdev->fb_phys +
205 (info->fix.line_length *
206 (info->var.yres_virtual >> 1)));
207 } else {
208 fbdev->regs->lcd_dmaaddr1 = LCD_DMA_SA_N(fbdev->fb_phys);
209 }
210 }
211
212 words = info->fix.line_length / sizeof(u32);
213 if (!info->var.rotate || (info->var.rotate == 180)) {
214 words *= info->var.yres_virtual;
215 if (info->var.rotate ) {
216 words -= (words % 8);
217 }
218 }
219 fbdev->regs->lcd_words = LCD_WRD_WRDS_N(words);
220
221 fbdev->regs->lcd_pwmdiv = 0;
222 fbdev->regs->lcd_pwmhi = 0;
223
224
225 fbdev->regs->lcd_control |= LCD_CONTROL_GO;
226 mdelay(10);
227 au1100fb_fb_blank(VESA_NO_BLANKING, info);
228
229 return 0;
230}
231
232
233
234
235int au1100fb_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info *fbi)
236{
237 struct au1100fb_device *fbdev;
238 u32 *palette;
239 u32 value;
240
241 fbdev = to_au1100fb_device(fbi);
242 palette = fbdev->regs->lcd_pallettebase;
243
244 if (regno > (AU1100_LCD_NBR_PALETTE_ENTRIES - 1))
245 return -EINVAL;
246
247 if (fbi->var.grayscale) {
248
249 red = green = blue =
250 (19595 * red + 38470 * green + 7471 * blue) >> 16;
251 }
252
253 if (fbi->fix.visual == FB_VISUAL_TRUECOLOR) {
254
255 if (regno > 16)
256 return -EINVAL;
257
258 palette = (u32*)fbi->pseudo_palette;
259
260 red >>= (16 - fbi->var.red.length);
261 green >>= (16 - fbi->var.green.length);
262 blue >>= (16 - fbi->var.blue.length);
263
264 value = (red << fbi->var.red.offset) |
265 (green << fbi->var.green.offset)|
266 (blue << fbi->var.blue.offset);
267 value &= 0xFFFF;
268
269 } else if (panel_is_active(fbdev->panel)) {
270
271 value = (red & 0xF800)|((green >> 5) & 0x07E0)|((blue >> 11) & 0x001F);
272 value &= 0xFFFF;
273
274 } else if (panel_is_color(fbdev->panel)) {
275
276 value = (((panel_swap_rgb(fbdev->panel) ? blue : red) >> 12) & 0x000F) |
277 ((green >> 8) & 0x00F0) |
278 (((panel_swap_rgb(fbdev->panel) ? red : blue) >> 4) & 0x0F00);
279 value &= 0xFFF;
280 } else {
281
282 value = (green >> 12) & 0x000F;
283 value &= 0xF;
284 }
285
286 palette[regno] = value;
287
288 return 0;
289}
290
291
292
293
294int au1100fb_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fbi)
295{
296 struct au1100fb_device *fbdev;
297 int dy;
298
299 fbdev = to_au1100fb_device(fbi);
300
301 print_dbg("fb_pan_display %p %p", var, fbi);
302
303 if (!var || !fbdev) {
304 return -EINVAL;
305 }
306
307 if (var->xoffset - fbi->var.xoffset) {
308
309 return -EINVAL;
310 }
311
312 print_dbg("fb_pan_display 2 %p %p", var, fbi);
313 dy = var->yoffset - fbi->var.yoffset;
314 if (dy) {
315
316 u32 dmaaddr;
317
318 print_dbg("Panning screen of %d lines", dy);
319
320 dmaaddr = fbdev->regs->lcd_dmaaddr0;
321 dmaaddr += (fbi->fix.line_length * dy);
322
323
324 fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(dmaaddr);
325
326 if (panel_is_dual(fbdev->panel)) {
327 dmaaddr = fbdev->regs->lcd_dmaaddr1;
328 dmaaddr += (fbi->fix.line_length * dy);
329 fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(dmaaddr);
330 }
331 }
332 print_dbg("fb_pan_display 3 %p %p", var, fbi);
333
334 return 0;
335}
336
337
338
339
340
341int au1100fb_fb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
342{
343 struct au1100fb_device *fbdev;
344
345 fbdev = to_au1100fb_device(fbi);
346
347 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
348 pgprot_val(vma->vm_page_prot) |= (6 << 9);
349
350 return vm_iomap_memory(vma, fbdev->fb_phys, fbdev->fb_len);
351}
352
353static struct fb_ops au1100fb_ops =
354{
355 .owner = THIS_MODULE,
356 .fb_setcolreg = au1100fb_fb_setcolreg,
357 .fb_blank = au1100fb_fb_blank,
358 .fb_pan_display = au1100fb_fb_pan_display,
359 .fb_fillrect = cfb_fillrect,
360 .fb_copyarea = cfb_copyarea,
361 .fb_imageblit = cfb_imageblit,
362 .fb_mmap = au1100fb_fb_mmap,
363};
364
365
366
367
368static int au1100fb_setup(struct au1100fb_device *fbdev)
369{
370 char *this_opt, *options;
371 int num_panels = ARRAY_SIZE(known_lcd_panels);
372
373 if (num_panels <= 0) {
374 print_err("No LCD panels supported by driver!");
375 return -ENODEV;
376 }
377
378 if (fb_get_options(DRIVER_NAME, &options))
379 return -ENODEV;
380 if (!options)
381 return -ENODEV;
382
383 while ((this_opt = strsep(&options, ",")) != NULL) {
384
385 if (!strncmp(this_opt, "panel:", 6)) {
386 int i;
387 this_opt += 6;
388 for (i = 0; i < num_panels; i++) {
389 if (!strncmp(this_opt, known_lcd_panels[i].name,
390 strlen(this_opt))) {
391 fbdev->panel = &known_lcd_panels[i];
392 fbdev->panel_idx = i;
393 break;
394 }
395 }
396 if (i >= num_panels) {
397 print_warn("Panel '%s' not supported!", this_opt);
398 return -ENODEV;
399 }
400 }
401
402 else
403 print_warn("Unsupported option \"%s\"", this_opt);
404 }
405
406 print_info("Panel=%s", fbdev->panel->name);
407
408 return 0;
409}
410
411static int au1100fb_drv_probe(struct platform_device *dev)
412{
413 struct au1100fb_device *fbdev = NULL;
414 struct resource *regs_res;
415 unsigned long page;
416 struct clk *c;
417
418
419 fbdev = devm_kzalloc(&dev->dev, sizeof(struct au1100fb_device),
420 GFP_KERNEL);
421 if (!fbdev) {
422 print_err("fail to allocate device private record");
423 return -ENOMEM;
424 }
425
426 if (au1100fb_setup(fbdev))
427 goto failed;
428
429 platform_set_drvdata(dev, (void *)fbdev);
430
431
432 regs_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
433 if (!regs_res) {
434 print_err("fail to retrieve registers resource");
435 return -EFAULT;
436 }
437
438 au1100fb_fix.mmio_start = regs_res->start;
439 au1100fb_fix.mmio_len = resource_size(regs_res);
440
441 if (!devm_request_mem_region(&dev->dev,
442 au1100fb_fix.mmio_start,
443 au1100fb_fix.mmio_len,
444 DRIVER_NAME)) {
445 print_err("fail to lock memory region at 0x%08lx",
446 au1100fb_fix.mmio_start);
447 return -EBUSY;
448 }
449
450 fbdev->regs = (struct au1100fb_regs*)KSEG1ADDR(au1100fb_fix.mmio_start);
451
452 print_dbg("Register memory map at %p", fbdev->regs);
453 print_dbg("phys=0x%08x, size=%d", fbdev->regs_phys, fbdev->regs_len);
454
455 c = clk_get(NULL, "lcd_intclk");
456 if (!IS_ERR(c)) {
457 fbdev->lcdclk = c;
458 clk_set_rate(c, 48000000);
459 clk_prepare_enable(c);
460 }
461
462
463 fbdev->fb_len = fbdev->panel->xres * fbdev->panel->yres *
464 (fbdev->panel->bpp >> 3) * AU1100FB_NBR_VIDEO_BUFFERS;
465
466 fbdev->fb_mem = dmam_alloc_coherent(&dev->dev,
467 PAGE_ALIGN(fbdev->fb_len),
468 &fbdev->fb_phys, GFP_KERNEL);
469 if (!fbdev->fb_mem) {
470 print_err("fail to allocate frambuffer (size: %dK))",
471 fbdev->fb_len / 1024);
472 return -ENOMEM;
473 }
474
475 au1100fb_fix.smem_start = fbdev->fb_phys;
476 au1100fb_fix.smem_len = fbdev->fb_len;
477
478
479
480
481
482 for (page = (unsigned long)fbdev->fb_mem;
483 page < PAGE_ALIGN((unsigned long)fbdev->fb_mem + fbdev->fb_len);
484 page += PAGE_SIZE) {
485#ifdef CONFIG_DMA_NONCOHERENT
486 SetPageReserved(virt_to_page(CAC_ADDR((void *)page)));
487#else
488 SetPageReserved(virt_to_page(page));
489#endif
490 }
491
492 print_dbg("Framebuffer memory map at %p", fbdev->fb_mem);
493 print_dbg("phys=0x%08x, size=%dK", fbdev->fb_phys, fbdev->fb_len / 1024);
494
495
496 au1100fb_var.bits_per_pixel = fbdev->panel->bpp;
497 au1100fb_var.xres = fbdev->panel->xres;
498 au1100fb_var.xres_virtual = au1100fb_var.xres;
499 au1100fb_var.yres = fbdev->panel->yres;
500 au1100fb_var.yres_virtual = au1100fb_var.yres;
501
502 fbdev->info.screen_base = fbdev->fb_mem;
503 fbdev->info.fbops = &au1100fb_ops;
504 fbdev->info.fix = au1100fb_fix;
505
506 fbdev->info.pseudo_palette =
507 devm_kzalloc(&dev->dev, sizeof(u32) * 16, GFP_KERNEL);
508 if (!fbdev->info.pseudo_palette)
509 return -ENOMEM;
510
511 if (fb_alloc_cmap(&fbdev->info.cmap, AU1100_LCD_NBR_PALETTE_ENTRIES, 0) < 0) {
512 print_err("Fail to allocate colormap (%d entries)",
513 AU1100_LCD_NBR_PALETTE_ENTRIES);
514 return -EFAULT;
515 }
516
517 fbdev->info.var = au1100fb_var;
518
519
520 au1100fb_setmode(fbdev);
521
522
523 if (register_framebuffer(&fbdev->info) < 0) {
524 print_err("cannot register new framebuffer");
525 goto failed;
526 }
527
528 return 0;
529
530failed:
531 if (fbdev->lcdclk) {
532 clk_disable_unprepare(fbdev->lcdclk);
533 clk_put(fbdev->lcdclk);
534 }
535 if (fbdev->fb_mem) {
536 dma_free_noncoherent(&dev->dev, fbdev->fb_len, fbdev->fb_mem,
537 fbdev->fb_phys);
538 }
539 if (fbdev->info.cmap.len != 0) {
540 fb_dealloc_cmap(&fbdev->info.cmap);
541 }
542
543 return -ENODEV;
544}
545
546int au1100fb_drv_remove(struct platform_device *dev)
547{
548 struct au1100fb_device *fbdev = NULL;
549
550 if (!dev)
551 return -ENODEV;
552
553 fbdev = platform_get_drvdata(dev);
554
555#if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
556 au1100fb_fb_blank(VESA_POWERDOWN, &fbdev->info);
557#endif
558 fbdev->regs->lcd_control &= ~LCD_CONTROL_GO;
559
560
561 unregister_framebuffer(&fbdev->info);
562
563 fb_dealloc_cmap(&fbdev->info.cmap);
564
565 if (fbdev->lcdclk) {
566 clk_disable_unprepare(fbdev->lcdclk);
567 clk_put(fbdev->lcdclk);
568 }
569
570 return 0;
571}
572
573#ifdef CONFIG_PM
574static struct au1100fb_regs fbregs;
575
576int au1100fb_drv_suspend(struct platform_device *dev, pm_message_t state)
577{
578 struct au1100fb_device *fbdev = platform_get_drvdata(dev);
579
580 if (!fbdev)
581 return 0;
582
583
584 au1100fb_fb_blank(VESA_POWERDOWN, &fbdev->info);
585
586 if (fbdev->lcdclk)
587 clk_disable(fbdev->lcdclk);
588
589 memcpy(&fbregs, fbdev->regs, sizeof(struct au1100fb_regs));
590
591 return 0;
592}
593
594int au1100fb_drv_resume(struct platform_device *dev)
595{
596 struct au1100fb_device *fbdev = platform_get_drvdata(dev);
597
598 if (!fbdev)
599 return 0;
600
601 memcpy(fbdev->regs, &fbregs, sizeof(struct au1100fb_regs));
602
603 if (fbdev->lcdclk)
604 clk_enable(fbdev->lcdclk);
605
606
607 au1100fb_fb_blank(VESA_NO_BLANKING, &fbdev->info);
608
609 return 0;
610}
611#else
612#define au1100fb_drv_suspend NULL
613#define au1100fb_drv_resume NULL
614#endif
615
616static struct platform_driver au1100fb_driver = {
617 .driver = {
618 .name = "au1100-lcd",
619 },
620 .probe = au1100fb_drv_probe,
621 .remove = au1100fb_drv_remove,
622 .suspend = au1100fb_drv_suspend,
623 .resume = au1100fb_drv_resume,
624};
625module_platform_driver(au1100fb_driver);
626
627MODULE_DESCRIPTION(DRIVER_DESC);
628MODULE_LICENSE("GPL");
629