2 * drivers/video/tegrafb.c
4 * Copyright (C) 2010 Google, Inc.
5 * Author: Colin Cross <ccross@android.com>
6 * Travis Geiselbrecht <travis@palm.com>
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
28 #include <linux/interrupt.h>
29 #include <linux/clk.h>
30 #include <linux/wait.h>
31 #include <asm/cacheflush.h>
32 #include <mach/tegra_fb.h>
34 #define DC_CMD_GENERAL_INCR_SYNCPT 0x000
35 #define DC_CMD_GENERAL_INCR_SYNCPT_CNTRL 0x001
36 #define DC_CMD_GENERAL_INCR_SYNCPT_ERROR 0x002
37 #define DC_CMD_WIN_A_INCR_SYNCPT 0x008
38 #define DC_CMD_WIN_A_INCR_SYNCPT_CNTRL 0x009
39 #define DC_CMD_WIN_A_INCR_SYNCPT_ERROR 0x00a
40 #define DC_CMD_WIN_B_INCR_SYNCPT 0x010
41 #define DC_CMD_WIN_B_INCR_SYNCPT_CNTRL 0x011
42 #define DC_CMD_WIN_B_INCR_SYNCPT_ERROR 0x012
43 #define DC_CMD_WIN_C_INCR_SYNCPT 0x018
44 #define DC_CMD_WIN_C_INCR_SYNCPT_CNTRL 0x019
45 #define DC_CMD_WIN_C_INCR_SYNCPT_ERROR 0x01a
46 #define DC_CMD_CONT_SYNCPT_VSYNC 0x028
47 #define DC_CMD_DISPLAY_COMMAND_OPTION0 0x031
48 #define DC_CMD_DISPLAY_COMMAND 0x032
49 #define DC_CMD_SIGNAL_RAISE 0x033
50 #define DC_CMD_INT_STATUS 0x037
51 #define DC_CMD_INT_MASK 0x038
52 #define DC_CMD_INT_ENABLE 0x039
53 #define DC_CMD_INT_TYPE 0x03a
54 #define DC_CMD_INT_POLARITY 0x03b
55 #define DC_CMD_SIGNAL_RAISE1 0x03c
56 #define DC_CMD_SIGNAL_RAISE2 0x03d
57 #define DC_CMD_SIGNAL_RAISE3 0x03e
58 #define DC_CMD_STATE_ACCESS 0x040
59 #define DC_CMD_STATE_CONTROL 0x041
60 #define DC_CMD_DISPLAY_WINDOW_HEADER 0x042
61 #define DC_CMD_REG_ACT_CONTROL 0x043
62 #define DC_WINC_A_COLOR_PALETTE 0x500
63 #define DC_WINC_A_PALETTE_COLOR_EXT 0x600
64 #define DC_WIN_A_WIN_OPTIONS 0x700
65 #define DC_WIN_A_BYTE_SWAP 0x701
66 #define DC_WIN_A_BUFFER_CONTROL 0x702
67 #define DC_WIN_A_COLOR_DEPTH 0x703
68 #define DC_WIN_A_POSITION 0x704
69 #define DC_WIN_A_SIZE 0x705
70 #define DC_WIN_A_PRESCALED_SIZE 0x706
71 #define DC_WIN_A_H_INITIAL_DDA 0x707
72 #define DC_WIN_A_V_INITIAL_DDA 0x708
73 #define DC_WIN_A_DDA_INCREMENT 0x709
74 #define DC_WIN_A_LINE_STRIDE 0x70a
75 #define DC_WIN_A_BUF_STRIDE 0x70b
76 #define DC_WINBUF_A_START_ADDR 0x800
77 #define DC_WINBUF_A_ADDR_H_OFFSET 0x806
78 #define DC_WINBUF_A_ADDR_V_OFFSET 0x808
81 #define DC_INT_CTXSW (1<<0)
82 #define DC_INT_FRAME_END (1<<1)
83 #define DC_INT_V_BLANK (1<<2)
84 #define DC_INT_H_BLANK (1<<3)
85 #define DC_INT_V_PULSE3 (1<<4)
87 #define DC_COLOR_DEPTH_P1 0
88 #define DC_COLOR_DEPTH_P2 1
89 #define DC_COLOR_DEPTH_P4 2
90 #define DC_COLOR_DEPTH_P8 3
91 #define DC_COLOR_DEPTH_B4G4R4A4 4
92 #define DC_COLOR_DEPTH_B5G5R5A 5
93 #define DC_COLOR_DEPTH_B5G6R5 6
94 #define DC_COLOR_DEPTH_AB5G5R5 7
95 #define DC_COLOR_DEPTH_B8G8R8A8 12
96 #define DC_COLOR_DEPTH_R8G8B8A8 13
97 #define DC_COLOR_DEPTH_B6x2G6x2R6x2A8 14
98 #define DC_COLOR_DEPTH_R6x2G6x2B6x2A8 15
99 #define DC_COLOR_DEPTH_YCbCr422 16
100 #define DC_COLOR_DEPTH_YUV422 17
101 #define DC_COLOR_DEPTH_YCbCr420P 18
102 #define DC_COLOR_DEPTH_YUV420P 19
103 #define DC_COLOR_DEPTH_YCbCr422P 20
104 #define DC_COLOR_DEPTH_YUV422P 21
105 #define DC_COLOR_DEPTH_YCbCr422R 22
106 #define DC_COLOR_DEPTH_YUV422R 23
107 #define DC_COLOR_DEPTH_YCbCr422RA 24
108 #define DC_COLOR_DEPTH_YUV422RA 25
110 struct tegra_fb_info {
112 struct resource *reg_mem;
113 struct resource *fb_mem;
114 void __iomem *reg_base;
115 wait_queue_head_t event_wq;
116 unsigned int wait_condition;
117 /* Resolution of the output to the LCD. If different from the
118 framebuffer resolution, the Tegra display block will scale it */
124 static void tegra_fb_writel(struct tegra_fb_info *tegra_fb, u32 val, unsigned long reg)
126 writel(val, tegra_fb->reg_base + reg*sizeof(u32));
129 static u32 tegra_fb_readl(struct tegra_fb_info *tegra_fb, unsigned long reg)
131 return readl(tegra_fb->reg_base + reg*sizeof(u32));
134 /* palette attary used by the fbcon */
135 u32 pseudo_palette[16];
137 irqreturn_t tegra_fb_irq(int irq, void *ptr)
139 struct fb_info *info = ptr;
140 struct tegra_fb_info *tegra_fb = info->par;
142 u32 reg = tegra_fb_readl(tegra_fb, DC_CMD_INT_STATUS);
143 tegra_fb_writel(tegra_fb, reg, DC_CMD_INT_STATUS);
145 tegra_fb->wait_condition = 1;
146 wake_up(&tegra_fb->event_wq);
150 static int tegra_fb_wait_for_event(struct tegra_fb_info *tegra_fb, unsigned long timeout, u32 irq_mask)
154 tegra_fb->wait_condition = 0;
156 reg = tegra_fb_readl(tegra_fb, DC_CMD_INT_MASK);
158 tegra_fb_writel(tegra_fb, reg, DC_CMD_INT_MASK);
160 /* Clear any pending interrupt */
161 tegra_fb_writel(tegra_fb, irq_mask, DC_CMD_INT_STATUS);
163 tegra_fb_writel(tegra_fb, irq_mask, DC_CMD_INT_ENABLE);
165 /* Wait for the irq to fire */
166 wait_event_interruptible_timeout(tegra_fb->event_wq, tegra_fb->wait_condition, timeout);
168 tegra_fb_writel(tegra_fb, 0, DC_CMD_INT_ENABLE);
170 if (!tegra_fb->wait_condition) {
171 pr_warning("%s: wait for vsync timed out\n", __func__);
177 /* Writes to many registers in the display block are buffered.
178 * tegra_fb_activate requests an update of the main registers from their
179 * double-buffered registers, which takes effect at the end of the next frame,
180 * and then waits for the frame_end IRQ.
182 static void tegra_fb_activate(struct tegra_fb_info *tegra_fb)
186 if (unlikely(tegra_fb_readl(tegra_fb, DC_CMD_STATE_CONTROL) & 3)) {
187 pr_warning("%s: update already activated!\n", __func__);
191 tegra_fb_writel(tegra_fb, (1 << 8) | (1 << 9), DC_CMD_STATE_CONTROL);
192 tegra_fb_writel(tegra_fb, (1 << 0) | (1 << 1), DC_CMD_STATE_CONTROL);
193 while (tegra_fb_readl(tegra_fb, DC_CMD_STATE_CONTROL) & 3) {
195 if (tegra_fb_wait_for_event(tegra_fb, HZ/10, DC_INT_FRAME_END))
198 if (unlikely(vsync_count > 1))
199 pr_warning("%s: waited for %d vsyncs\n", __func__, vsync_count);
203 static int tegra_fb_open(struct fb_info *info, int user)
208 static int tegra_fb_release(struct fb_info *info, int user)
213 static int tegra_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
215 if ((var->xres != info->var.xres) ||
216 (var->yres != info->var.yres) ||
217 (var->xres_virtual != info->var.xres_virtual) ||
218 (var->yres_virtual != info->var.yres_virtual) ||
219 (var->grayscale != info->var.grayscale))
224 static int tegra_fb_set_par(struct fb_info *info)
226 struct tegra_fb_info *tegra_fb = info->par;
227 struct fb_var_screeninfo *var = &info->var;
228 struct fb_fix_screeninfo *fix = &info->fix;
233 /* we only support RGB ordering for now */
234 switch (var->bits_per_pixel) {
239 var->green.offset = 8;
240 var->green.length = 8;
241 var->blue.offset = 16;
242 var->blue.length = 8;
243 color_depth = DC_COLOR_DEPTH_B8G8R8A8;
246 var->red.offset = 11;
248 var->green.offset = 5;
249 var->green.length = 6;
250 var->blue.offset = 0;
251 var->blue.length = 5;
252 color_depth = DC_COLOR_DEPTH_B5G6R5;
257 fix->line_length = var->xres * var->bits_per_pixel / 8;
259 h_dda = (var->xres * 0x1000) / (tegra_fb->lcd_xres - 1);
260 v_dda = (var->yres * 0x1000) / (tegra_fb->lcd_yres - 1);
262 tegra_fb_writel(tegra_fb, tegra_fb->lcd_yres<<16 | tegra_fb->lcd_xres,
264 tegra_fb_writel(tegra_fb, var->yres<<16 | fix->line_length,
265 DC_WIN_A_PRESCALED_SIZE);
266 tegra_fb_writel(tegra_fb, 0, DC_WIN_A_H_INITIAL_DDA);
267 tegra_fb_writel(tegra_fb, 0, DC_WIN_A_V_INITIAL_DDA);
268 tegra_fb_writel(tegra_fb, v_dda << 16 | h_dda, DC_WIN_A_DDA_INCREMENT);
269 tegra_fb_writel(tegra_fb, color_depth, DC_WIN_A_COLOR_DEPTH);
270 tegra_fb_writel(tegra_fb, fix->line_length, DC_WIN_A_LINE_STRIDE);
274 static int tegra_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
275 unsigned blue, unsigned transp, struct fb_info *info)
277 struct fb_var_screeninfo *var = &info->var;
279 if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
280 info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
286 v = (red << var->red.offset) |
287 (green << var->green.offset) |
288 (blue << var->blue.offset);
290 ((u32 *)info->pseudo_palette)[regno] = v;
296 static int tegra_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
298 struct tegra_fb_info *tegra_fb = info->par;
299 char __iomem *flush_start;
300 char __iomem *flush_end;
303 flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
304 flush_end = flush_start + (var->yres * info->fix.line_length);
306 info->var.xoffset = var->xoffset;
307 info->var.yoffset = var->yoffset;
309 addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
310 (var->xoffset * (var->bits_per_pixel/8));
312 tegra_fb_writel(tegra_fb, addr, DC_WINBUF_A_START_ADDR);
313 tegra_fb_activate(tegra_fb);
317 static void tegra_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
319 cfb_fillrect(info, rect);
322 static void tegra_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
324 cfb_copyarea(info, region);
327 static void tegra_fb_imageblit(struct fb_info *info, const struct fb_image *image)
329 cfb_imageblit(info, image);
333 #define DUMP_REG(a) dev_dbg(info->dev, "%-32s\t%03x\t%08x\n", #a, a, tegra_fb_readl(tegra_fb, a));
334 static void dump_regs(struct fb_info *info)
336 struct tegra_fb_info *tegra_fb = info->par;
338 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
339 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
340 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
341 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
342 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
343 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
344 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
345 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
346 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
347 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
348 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
349 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
350 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
351 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
352 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
353 DUMP_REG(DC_CMD_SIGNAL_RAISE);
354 DUMP_REG(DC_CMD_INT_STATUS);
355 DUMP_REG(DC_CMD_INT_MASK);
356 DUMP_REG(DC_CMD_INT_ENABLE);
357 DUMP_REG(DC_CMD_INT_TYPE);
358 DUMP_REG(DC_CMD_INT_POLARITY);
359 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
360 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
361 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
362 DUMP_REG(DC_CMD_STATE_ACCESS);
363 DUMP_REG(DC_CMD_STATE_CONTROL);
364 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
365 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
366 tegra_fb_writel(tegra_fb, 1<<4, DC_CMD_DISPLAY_WINDOW_HEADER);
367 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
368 DUMP_REG(DC_WINC_A_COLOR_PALETTE);
369 DUMP_REG(DC_WINC_A_PALETTE_COLOR_EXT);
370 DUMP_REG(DC_WIN_A_WIN_OPTIONS);
371 DUMP_REG(DC_WIN_A_BYTE_SWAP);
372 DUMP_REG(DC_WIN_A_BUFFER_CONTROL);
373 DUMP_REG(DC_WIN_A_COLOR_DEPTH);
374 DUMP_REG(DC_WIN_A_POSITION);
375 DUMP_REG(DC_WIN_A_SIZE);
376 DUMP_REG(DC_WIN_A_PRESCALED_SIZE);
377 DUMP_REG(DC_WIN_A_H_INITIAL_DDA);
378 DUMP_REG(DC_WIN_A_V_INITIAL_DDA);
379 DUMP_REG(DC_WIN_A_DDA_INCREMENT);
380 DUMP_REG(DC_WIN_A_LINE_STRIDE);
381 DUMP_REG(DC_WIN_A_BUF_STRIDE);
382 DUMP_REG(DC_WINBUF_A_START_ADDR);
383 DUMP_REG(DC_WINBUF_A_ADDR_H_OFFSET);
384 DUMP_REG(DC_WINBUF_A_ADDR_V_OFFSET);
388 static struct fb_ops tegra_fb_ops = {
389 .owner = THIS_MODULE,
390 .fb_open = tegra_fb_open,
391 .fb_release = tegra_fb_release,
392 .fb_check_var = tegra_fb_check_var,
393 .fb_set_par = tegra_fb_set_par,
394 .fb_setcolreg = tegra_fb_setcolreg,
395 .fb_pan_display = tegra_fb_pan_display,
396 .fb_fillrect = tegra_fb_fillrect,
397 .fb_copyarea = tegra_fb_copyarea,
398 .fb_imageblit = tegra_fb_imageblit,
401 static int tegra_plat_probe(struct platform_device *pdev)
403 struct fb_info *info;
404 struct tegra_fb_info *tegra_fb;
406 struct resource *res;
407 struct resource *reg_mem;
408 struct resource *fb_mem;
410 void __iomem *reg_base;
411 void __iomem *fb_base;
412 unsigned long fb_size;
413 unsigned long fb_phys;
415 const struct tegra_fb_lcd_data *lcd_data = pdev->dev.platform_data;
417 info = framebuffer_alloc(sizeof(struct tegra_fb_info), &pdev->dev);
423 irq = platform_get_irq(pdev, 0);
425 dev_err(&pdev->dev, "%s: no irq\n", pdev->name);
430 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
432 dev_err(&pdev->dev, "%s: no mem resource\n", pdev->name);
437 reg_mem = request_mem_region(res->start, resource_size(res), pdev->name);
439 dev_err(&pdev->dev, "%s: request_mem_region failed\n", pdev->name);
444 reg_base = ioremap(res->start, resource_size(res));
446 dev_err(&pdev->dev, "%s: registers can't be mapped\n", pdev->name);
448 goto err_release_resource_reg;
451 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
453 dev_err(&pdev->dev, "%s: no mem resource\n", pdev->name);
455 goto err_iounmap_reg;
458 fb_mem = request_mem_region(res->start, resource_size(res), pdev->name);
460 dev_err(&pdev->dev, "%s: request_mem_region failed\n", pdev->name);
462 goto err_iounmap_reg;
465 fb_size = resource_size(res);
466 fb_phys = res->start;
467 fb_base = ioremap_nocache(fb_phys, fb_size);
469 dev_err(&pdev->dev, "%s: fb can't be mapped\n", pdev->name);
471 goto err_release_resource_fb;
474 clk = clk_get(&pdev->dev, NULL);
476 dev_err(&pdev->dev, "%s: can't get clock\n", pdev->name);
482 tegra_fb = info->par;
484 tegra_fb->fb_mem = fb_mem;
485 tegra_fb->reg_mem = reg_mem;
486 tegra_fb->reg_base = reg_base;
488 tegra_fb->lcd_xres = lcd_data->lcd_xres;
489 tegra_fb->lcd_yres = lcd_data->lcd_yres;
491 info->fbops = &tegra_fb_ops;
492 info->pseudo_palette = pseudo_palette;
493 info->screen_base = fb_base;
494 info->screen_size = fb_size;
496 strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
497 info->fix.type = FB_TYPE_PACKED_PIXELS;
498 info->fix.visual = FB_VISUAL_TRUECOLOR;
499 info->fix.xpanstep = 1;
500 info->fix.ypanstep = 1;
501 info->fix.accel = FB_ACCEL_NONE;
502 info->fix.smem_start = fb_phys;
503 info->fix.smem_len = fb_size;
505 info->var.xres = lcd_data->fb_xres;
506 info->var.yres = lcd_data->fb_yres;
507 info->var.xres_virtual = lcd_data->fb_xres;
508 info->var.yres_virtual = lcd_data->fb_yres*2;
509 info->var.bits_per_pixel = lcd_data->bits_per_pixel;
510 info->var.activate = FB_ACTIVATE_NOW;
511 info->var.height = -1;
512 info->var.width = -1;
513 info->var.pixclock = 24500;
514 info->var.left_margin = 0;
515 info->var.right_margin = 0;
516 info->var.upper_margin = 0;
517 info->var.lower_margin = 0;
518 info->var.hsync_len = 0;
519 info->var.vsync_len = 0;
520 info->var.vmode = FB_VMODE_NONINTERLACED;
522 if (request_irq(irq, tegra_fb_irq, IRQF_DISABLED,
523 dev_name(&pdev->dev), info)) {
524 pr_debug("%s: request_irq %d failed\n",
527 goto err_clk_disable;
530 init_waitqueue_head(&tegra_fb->event_wq);
532 /* Enable writes to Window A */
533 tegra_fb_writel(tegra_fb, 1<<4, DC_CMD_DISPLAY_WINDOW_HEADER);
536 info->dev = &pdev->dev;
537 dev_dbg(info->dev, "Framebuffer registers before init:\n");
541 tegra_fb_set_par(info);
544 dev_dbg(info->dev, "Framebuffer registers after init:\n");
548 dev_info(&pdev->dev, "base address: %08x (%08x)\n",
549 (unsigned int)info->fix.smem_start,
550 (unsigned int)info->screen_base);
552 if (register_framebuffer(info)) {
553 dev_err(&pdev->dev, "failed to register framebuffer\n");
557 platform_set_drvdata(pdev, info);
566 err_release_resource_fb:
567 release_resource(fb_mem);
570 err_release_resource_reg:
571 release_resource(reg_mem);
573 framebuffer_release(info);
578 static int tegra_plat_remove(struct platform_device *pdev)
580 struct fb_info *info = platform_get_drvdata(pdev);
581 struct tegra_fb_info *tegra_fb = info->par;
582 unregister_framebuffer(info);
583 free_irq(tegra_fb->irq, info);
584 clk_disable(tegra_fb->clk);
585 iounmap(info->screen_base);
586 release_resource(tegra_fb->fb_mem);
587 iounmap(tegra_fb->reg_base);
588 release_resource(tegra_fb->reg_mem);
589 framebuffer_release(info);
593 struct platform_driver tegra_platform_driver = {
594 .probe = tegra_plat_probe,
595 .remove = tegra_plat_remove,
598 .owner = THIS_MODULE,
602 static int __init tegra_fb_init(void)
605 e = platform_driver_register(&tegra_platform_driver);
607 pr_info("tegrafb: platform_driver_register failed\n");
613 static void __exit tegra_exit(void)
615 platform_driver_unregister(&tegra_platform_driver);
618 module_exit(tegra_exit);
619 module_init(tegra_fb_init);