2 * Frame buffer driver for ADV7393/2 video encoder
4 * Copyright 2006-2009 Analog Devices Inc.
5 * Licensed under the GPL-2 or late.
13 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
20 #include <linux/tty.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
24 #include <linux/ioport.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
29 #include <asm/blackfin.h>
32 #include <linux/uaccess.h>
33 #include <linux/gpio.h>
34 #include <asm/portmux.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/proc_fs.h>
38 #include <linux/platform_device.h>
39 #include <linux/i2c.h>
41 #include "bfin_adv7393fb.h"
43 static int mode = VMODE;
44 static int mem = VMEM;
45 static int nocursor = 1;
47 static const unsigned short ppi_pins[] = {
48 P_PPI0_CLK, P_PPI0_FS1, P_PPI0_FS2,
49 P_PPI0_D0, P_PPI0_D1, P_PPI0_D2, P_PPI0_D3,
50 P_PPI0_D4, P_PPI0_D5, P_PPI0_D6, P_PPI0_D7,
51 P_PPI0_D8, P_PPI0_D9, P_PPI0_D10, P_PPI0_D11,
52 P_PPI0_D12, P_PPI0_D13, P_PPI0_D14, P_PPI0_D15,
60 static struct bfin_adv7393_fb_par {
61 /* structure holding blackfin / adv7393 paramters when
64 u8 Mode; /* ntsc/pal/? */
69 /* --------------------------------------------------------------------- */
71 static struct fb_var_screeninfo bfin_adv7393_fb_defined = {
77 .activate = FB_ACTIVATE_TEST,
84 .vmode = FB_VMODE_INTERLACED,
91 static struct fb_fix_screeninfo bfin_adv7393_fb_fix __devinitdata = {
93 .smem_len = 720 * 480 * 2,
94 .type = FB_TYPE_PACKED_PIXELS,
95 .visual = FB_VISUAL_TRUECOLOR,
98 .line_length = 720 * 2,
99 .accel = FB_ACCEL_NONE
102 static struct fb_ops bfin_adv7393_fb_ops = {
103 .owner = THIS_MODULE,
104 .fb_open = bfin_adv7393_fb_open,
105 .fb_release = bfin_adv7393_fb_release,
106 .fb_check_var = bfin_adv7393_fb_check_var,
107 .fb_pan_display = bfin_adv7393_fb_pan_display,
108 .fb_blank = bfin_adv7393_fb_blank,
109 .fb_fillrect = cfb_fillrect,
110 .fb_copyarea = cfb_copyarea,
111 .fb_imageblit = cfb_imageblit,
112 .fb_cursor = bfin_adv7393_fb_cursor,
113 .fb_setcolreg = bfin_adv7393_fb_setcolreg,
116 static int dma_desc_list(struct adv7393fb_device *fbdev, u16 arg)
118 if (arg == BUILD) { /* Build */
119 fbdev->vb1 = l1_data_sram_zalloc(sizeof(struct dmasg));
120 if (fbdev->vb1 == NULL)
123 fbdev->av1 = l1_data_sram_zalloc(sizeof(struct dmasg));
124 if (fbdev->av1 == NULL)
127 fbdev->vb2 = l1_data_sram_zalloc(sizeof(struct dmasg));
128 if (fbdev->vb2 == NULL)
131 fbdev->av2 = l1_data_sram_zalloc(sizeof(struct dmasg));
132 if (fbdev->av2 == NULL)
135 /* Build linked DMA descriptor list */
136 fbdev->vb1->next_desc_addr = fbdev->av1;
137 fbdev->av1->next_desc_addr = fbdev->vb2;
138 fbdev->vb2->next_desc_addr = fbdev->av2;
139 fbdev->av2->next_desc_addr = fbdev->vb1;
142 fbdev->descriptor_list_head = fbdev->av2;
144 /* Vertical Blanking Field 1 */
145 fbdev->vb1->start_addr = VB_DUMMY_MEMORY_SOURCE;
146 fbdev->vb1->cfg = DMA_CFG_VAL;
148 fbdev->vb1->x_count =
149 fbdev->modes[mode].xres + fbdev->modes[mode].boeft_blank;
151 fbdev->vb1->x_modify = 0;
152 fbdev->vb1->y_count = fbdev->modes[mode].vb1_lines;
153 fbdev->vb1->y_modify = 0;
155 /* Active Video Field 1 */
157 fbdev->av1->start_addr = (unsigned long)fbdev->fb_mem;
158 fbdev->av1->cfg = DMA_CFG_VAL;
159 fbdev->av1->x_count =
160 fbdev->modes[mode].xres + fbdev->modes[mode].boeft_blank;
161 fbdev->av1->x_modify = fbdev->modes[mode].bpp / 8;
162 fbdev->av1->y_count = fbdev->modes[mode].a_lines;
163 fbdev->av1->y_modify =
164 (fbdev->modes[mode].xres - fbdev->modes[mode].boeft_blank +
165 1) * (fbdev->modes[mode].bpp / 8);
167 /* Vertical Blanking Field 2 */
169 fbdev->vb2->start_addr = VB_DUMMY_MEMORY_SOURCE;
170 fbdev->vb2->cfg = DMA_CFG_VAL;
171 fbdev->vb2->x_count =
172 fbdev->modes[mode].xres + fbdev->modes[mode].boeft_blank;
174 fbdev->vb2->x_modify = 0;
175 fbdev->vb2->y_count = fbdev->modes[mode].vb2_lines;
176 fbdev->vb2->y_modify = 0;
178 /* Active Video Field 2 */
180 fbdev->av2->start_addr =
181 (unsigned long)fbdev->fb_mem + fbdev->line_len;
183 fbdev->av2->cfg = DMA_CFG_VAL;
185 fbdev->av2->x_count =
186 fbdev->modes[mode].xres + fbdev->modes[mode].boeft_blank;
188 fbdev->av2->x_modify = (fbdev->modes[mode].bpp / 8);
189 fbdev->av2->y_count = fbdev->modes[mode].a_lines;
191 fbdev->av2->y_modify =
192 (fbdev->modes[mode].xres - fbdev->modes[mode].boeft_blank +
193 1) * (fbdev->modes[mode].bpp / 8);
199 l1_data_sram_free(fbdev->vb1);
200 l1_data_sram_free(fbdev->av1);
201 l1_data_sram_free(fbdev->vb2);
202 l1_data_sram_free(fbdev->av2);
207 static int bfin_config_dma(struct adv7393fb_device *fbdev)
209 BUG_ON(!(fbdev->fb_mem));
211 set_dma_x_count(CH_PPI, fbdev->descriptor_list_head->x_count);
212 set_dma_x_modify(CH_PPI, fbdev->descriptor_list_head->x_modify);
213 set_dma_y_count(CH_PPI, fbdev->descriptor_list_head->y_count);
214 set_dma_y_modify(CH_PPI, fbdev->descriptor_list_head->y_modify);
215 set_dma_start_addr(CH_PPI, fbdev->descriptor_list_head->start_addr);
216 set_dma_next_desc_addr(CH_PPI,
217 fbdev->descriptor_list_head->next_desc_addr);
218 set_dma_config(CH_PPI, fbdev->descriptor_list_head->cfg);
223 static void bfin_disable_dma(void)
225 bfin_write_DMA0_CONFIG(bfin_read_DMA0_CONFIG() & ~DMAEN);
228 static void bfin_config_ppi(struct adv7393fb_device *fbdev)
230 if (ANOMALY_05000183) {
231 bfin_write_TIMER2_CONFIG(WDTH_CAP);
232 bfin_write_TIMER_ENABLE(TIMEN2);
235 bfin_write_PPI_CONTROL(0x381E);
236 bfin_write_PPI_FRAME(fbdev->modes[mode].tot_lines);
237 bfin_write_PPI_COUNT(fbdev->modes[mode].xres +
238 fbdev->modes[mode].boeft_blank - 1);
239 bfin_write_PPI_DELAY(fbdev->modes[mode].aoeft_blank - 1);
242 static void bfin_enable_ppi(void)
244 bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN);
247 static void bfin_disable_ppi(void)
249 bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() & ~PORT_EN);
252 static inline int adv7393_write(struct i2c_client *client, u8 reg, u8 value)
254 return i2c_smbus_write_byte_data(client, reg, value);
257 static inline int adv7393_read(struct i2c_client *client, u8 reg)
259 return i2c_smbus_read_byte_data(client, reg);
263 adv7393_write_block(struct i2c_client *client,
264 const u8 *data, unsigned int len)
271 ret = adv7393_write(client, reg, *data++);
280 static int adv7393_mode(struct i2c_client *client, u16 mode)
283 case POWER_ON: /* ADV7393 Sleep mode OFF */
284 adv7393_write(client, 0x00, 0x1E);
286 case POWER_DOWN: /* ADV7393 Sleep mode ON */
287 adv7393_write(client, 0x00, 0x1F);
289 case BLANK_OFF: /* Pixel Data Valid */
290 adv7393_write(client, 0x82, 0xCB);
292 case BLANK_ON: /* Pixel Data Invalid */
293 adv7393_write(client, 0x82, 0x8B);
302 static irqreturn_t ppi_irq_error(int irq, void *dev_id)
305 struct adv7393fb_device *fbdev = (struct adv7393fb_device *)dev_id;
307 u16 status = bfin_read_PPI_STATUS();
309 pr_debug("%s: PPI Status = 0x%X\n", __func__, status);
312 bfin_disable_dma(); /* TODO: Check Sequence */
314 bfin_clear_PPI_STATUS();
315 bfin_config_dma(fbdev);
323 static int proc_output(char *buf)
329 "echo 0x[REG][Value] > adv7393\n"
330 "example: echo 0x1234 >adv7393\n"
331 "writes 0x34 into Register 0x12\n");
337 adv7393_read_proc(char *page, char **start, off_t off,
338 int count, int *eof, void *data)
342 len = proc_output(page);
343 if (len <= off + count)
355 adv7393_write_proc(struct file *file, const char __user * buffer,
356 unsigned long count, void *data)
358 struct adv7393fb_device *fbdev = data;
363 ret = copy_from_user(line, buffer, count);
367 val = simple_strtoul(line, NULL, 0);
368 adv7393_write(fbdev->client, val >> 8, val & 0xff);
373 static int __devinit bfin_adv7393_fb_probe(struct i2c_client *client,
374 const struct i2c_device_id *id)
377 struct proc_dir_entry *entry;
378 int num_modes = ARRAY_SIZE(known_modes);
380 struct adv7393fb_device *fbdev = NULL;
383 dev_err(&client->dev, "mem out of allowed range [1;2]\n");
387 if (mode > num_modes) {
388 dev_err(&client->dev, "mode %d: not supported", mode);
392 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
394 dev_err(&client->dev, "failed to allocate device private record");
398 i2c_set_clientdata(client, fbdev);
400 fbdev->modes = known_modes;
401 fbdev->client = client;
404 mem * fbdev->modes[mode].xres * fbdev->modes[mode].xres *
405 (fbdev->modes[mode].bpp / 8);
408 fbdev->modes[mode].xres * (fbdev->modes[mode].bpp / 8);
410 /* Workaround "PPI Does Not Start Properly In Specific Mode" */
411 if (ANOMALY_05000400) {
412 ret = gpio_request_one(P_IDENT(P_PPI0_FS3), GPIOF_OUT_INIT_LOW,
415 dev_err(&client->dev, "PPI0_FS3 GPIO request failed\n");
421 if (peripheral_request_list(ppi_pins, DRIVER_NAME)) {
422 dev_err(&client->dev, "requesting PPI peripheral failed\n");
428 dma_alloc_coherent(NULL, fbdev->fb_len, &fbdev->dma_handle,
431 if (NULL == fbdev->fb_mem) {
432 dev_err(&client->dev, "couldn't allocate dma buffer (%d bytes)\n",
433 (u32) fbdev->fb_len);
438 fbdev->info.screen_base = (void *)fbdev->fb_mem;
439 bfin_adv7393_fb_fix.smem_start = (int)fbdev->fb_mem;
441 bfin_adv7393_fb_fix.smem_len = fbdev->fb_len;
442 bfin_adv7393_fb_fix.line_length = fbdev->line_len;
445 bfin_adv7393_fb_fix.ypanstep = 1;
447 bfin_adv7393_fb_defined.red.length = 5;
448 bfin_adv7393_fb_defined.green.length = 6;
449 bfin_adv7393_fb_defined.blue.length = 5;
451 bfin_adv7393_fb_defined.xres = fbdev->modes[mode].xres;
452 bfin_adv7393_fb_defined.yres = fbdev->modes[mode].yres;
453 bfin_adv7393_fb_defined.xres_virtual = fbdev->modes[mode].xres;
454 bfin_adv7393_fb_defined.yres_virtual = mem * fbdev->modes[mode].yres;
455 bfin_adv7393_fb_defined.bits_per_pixel = fbdev->modes[mode].bpp;
457 fbdev->info.fbops = &bfin_adv7393_fb_ops;
458 fbdev->info.var = bfin_adv7393_fb_defined;
459 fbdev->info.fix = bfin_adv7393_fb_fix;
460 fbdev->info.par = &bfin_par;
461 fbdev->info.flags = FBINFO_DEFAULT;
463 fbdev->info.pseudo_palette = kzalloc(sizeof(u32) * 16, GFP_KERNEL);
464 if (!fbdev->info.pseudo_palette) {
465 dev_err(&client->dev, "failed to allocate pseudo_palette\n");
470 if (fb_alloc_cmap(&fbdev->info.cmap, BFIN_LCD_NBR_PALETTE_ENTRIES, 0) < 0) {
471 dev_err(&client->dev, "failed to allocate colormap (%d entries)\n",
472 BFIN_LCD_NBR_PALETTE_ENTRIES);
477 if (request_dma(CH_PPI, "BF5xx_PPI_DMA") < 0) {
478 dev_err(&client->dev, "unable to request PPI DMA\n");
483 if (request_irq(IRQ_PPI_ERROR, ppi_irq_error, 0,
484 "PPI ERROR", fbdev) < 0) {
485 dev_err(&client->dev, "unable to request PPI ERROR IRQ\n");
492 ret = adv7393_write_block(client, fbdev->modes[mode].adv7393_i2c_initd,
493 fbdev->modes[mode].adv7393_i2c_initd_len);
496 dev_err(&client->dev, "i2c attach: init error\n");
501 if (register_framebuffer(&fbdev->info) < 0) {
502 dev_err(&client->dev, "unable to register framebuffer\n");
507 dev_info(&client->dev, "fb%d: %s frame buffer device\n",
508 fbdev->info.node, fbdev->info.fix.id);
509 dev_info(&client->dev, "fb memory address : 0x%p\n", fbdev->fb_mem);
511 entry = create_proc_entry("driver/adv7393", 0, NULL);
513 dev_err(&client->dev, "unable to create /proc entry\n");
518 entry->read_proc = adv7393_read_proc;
519 entry->write_proc = adv7393_write_proc;
525 unregister_framebuffer(&fbdev->info);
527 free_irq(IRQ_PPI_ERROR, fbdev);
531 fb_dealloc_cmap(&fbdev->info.cmap);
533 kfree(fbdev->info.pseudo_palette);
535 dma_free_coherent(NULL, fbdev->fb_len, fbdev->fb_mem,
538 peripheral_free_list(ppi_pins);
540 if (ANOMALY_05000400)
541 gpio_free(P_IDENT(P_PPI0_FS3));
548 static int bfin_adv7393_fb_open(struct fb_info *info, int user)
550 struct adv7393fb_device *fbdev = to_adv7393fb_device(info);
552 fbdev->info.screen_base = (void *)fbdev->fb_mem;
553 if (!fbdev->info.screen_base) {
554 dev_err(&fbdev->client->dev, "unable to map device\n");
559 dma_desc_list(fbdev, BUILD);
560 adv7393_mode(fbdev->client, BLANK_OFF);
561 bfin_config_ppi(fbdev);
562 bfin_config_dma(fbdev);
568 static int bfin_adv7393_fb_release(struct fb_info *info, int user)
570 struct adv7393fb_device *fbdev = to_adv7393fb_device(info);
572 adv7393_mode(fbdev->client, BLANK_ON);
575 dma_desc_list(fbdev, DESTRUCT);
581 bfin_adv7393_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
584 switch (var->bits_per_pixel) {
585 case 16:/* DIRECTCOLOUR, 64k */
586 var->red.offset = info->var.red.offset;
587 var->green.offset = info->var.green.offset;
588 var->blue.offset = info->var.blue.offset;
589 var->red.length = info->var.red.length;
590 var->green.length = info->var.green.length;
591 var->blue.length = info->var.blue.length;
592 var->transp.offset = 0;
593 var->transp.length = 0;
594 var->transp.msb_right = 0;
595 var->red.msb_right = 0;
596 var->green.msb_right = 0;
597 var->blue.msb_right = 0;
600 pr_debug("%s: depth not supported: %u BPP\n", __func__,
601 var->bits_per_pixel);
605 if (info->var.xres != var->xres ||
606 info->var.yres != var->yres ||
607 info->var.xres_virtual != var->xres_virtual ||
608 info->var.yres_virtual != var->yres_virtual) {
609 pr_debug("%s: Resolution not supported: X%u x Y%u\n",
610 __func__, var->xres, var->yres);
618 if ((info->fix.line_length * var->yres_virtual) > info->fix.smem_len) {
619 pr_debug("%s: Memory Limit requested yres_virtual = %u\n",
620 __func__, var->yres_virtual);
628 bfin_adv7393_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
632 struct adv7393fb_device *fbdev = to_adv7393fb_device(info);
637 if (var->xoffset - info->var.xoffset) {
638 /* No support for X panning for now! */
641 dy = var->yoffset - info->var.yoffset;
644 pr_debug("%s: Panning screen of %d lines\n", __func__, dy);
646 dmaaddr = fbdev->av1->start_addr;
647 dmaaddr += (info->fix.line_length * dy);
648 /* TODO: Wait for current frame to finished */
650 fbdev->av1->start_addr = (unsigned long)dmaaddr;
651 fbdev->av2->start_addr = (unsigned long)dmaaddr + fbdev->line_len;
658 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
659 static int bfin_adv7393_fb_blank(int blank, struct fb_info *info)
661 struct adv7393fb_device *fbdev = to_adv7393fb_device(info);
665 case VESA_NO_BLANKING:
667 adv7393_mode(fbdev->client, BLANK_OFF);
670 case VESA_VSYNC_SUSPEND:
671 case VESA_HSYNC_SUSPEND:
674 adv7393_mode(fbdev->client, BLANK_ON);
684 int bfin_adv7393_fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
689 return -EINVAL; /* just to force soft_cursor() call */
692 static int bfin_adv7393_fb_setcolreg(u_int regno, u_int red, u_int green,
693 u_int blue, u_int transp,
694 struct fb_info *info)
696 if (regno >= BFIN_LCD_NBR_PALETTE_ENTRIES)
699 if (info->var.grayscale)
700 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
701 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
703 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
705 /* Place color in the pseudopalette */
709 red >>= (16 - info->var.red.length);
710 green >>= (16 - info->var.green.length);
711 blue >>= (16 - info->var.blue.length);
713 value = (red << info->var.red.offset) |
714 (green << info->var.green.offset)|
715 (blue << info->var.blue.offset);
718 ((u32 *) (info->pseudo_palette))[regno] = value;
724 static int __devexit bfin_adv7393_fb_remove(struct i2c_client *client)
726 struct adv7393fb_device *fbdev = i2c_get_clientdata(client);
728 adv7393_mode(client, POWER_DOWN);
731 dma_free_coherent(NULL, fbdev->fb_len, fbdev->fb_mem, fbdev->dma_handle);
733 free_irq(IRQ_PPI_ERROR, fbdev);
734 unregister_framebuffer(&fbdev->info);
735 remove_proc_entry("driver/adv7393", NULL);
736 fb_dealloc_cmap(&fbdev->info.cmap);
737 kfree(fbdev->info.pseudo_palette);
739 if (ANOMALY_05000400)
740 gpio_free(P_IDENT(P_PPI0_FS3)); /* FS3 */
741 peripheral_free_list(ppi_pins);
748 static int bfin_adv7393_fb_suspend(struct device *dev)
750 struct adv7393fb_device *fbdev = dev_get_drvdata(dev);
755 dma_desc_list(fbdev, DESTRUCT);
757 adv7393_mode(fbdev->client, POWER_DOWN);
762 static int bfin_adv7393_fb_resume(struct device *dev)
764 struct adv7393fb_device *fbdev = dev_get_drvdata(dev);
766 adv7393_mode(fbdev->client, POWER_ON);
769 dma_desc_list(fbdev, BUILD);
770 bfin_config_ppi(fbdev);
771 bfin_config_dma(fbdev);
778 static const struct dev_pm_ops bfin_adv7393_dev_pm_ops = {
779 .suspend = bfin_adv7393_fb_suspend,
780 .resume = bfin_adv7393_fb_resume,
784 static const struct i2c_device_id bfin_adv7393_id[] = {
789 MODULE_DEVICE_TABLE(i2c, bfin_adv7393_id);
791 static struct i2c_driver bfin_adv7393_fb_driver = {
795 .pm = &bfin_adv7393_dev_pm_ops,
798 .probe = bfin_adv7393_fb_probe,
799 .remove = __devexit_p(bfin_adv7393_fb_remove),
800 .id_table = bfin_adv7393_id,
803 static int __init bfin_adv7393_fb_driver_init(void)
805 #if defined(CONFIG_I2C_BLACKFIN_TWI) || defined(CONFIG_I2C_BLACKFIN_TWI_MODULE)
806 request_module("i2c-bfin-twi");
808 request_module("i2c-gpio");
811 return i2c_add_driver(&bfin_adv7393_fb_driver);
813 module_init(bfin_adv7393_fb_driver_init);
815 static void __exit bfin_adv7393_fb_driver_cleanup(void)
817 i2c_del_driver(&bfin_adv7393_fb_driver);
819 module_exit(bfin_adv7393_fb_driver_cleanup);
821 MODULE_LICENSE("GPL");
822 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
823 MODULE_DESCRIPTION("Frame buffer driver for ADV7393/2 Video Encoder");
825 module_param(mode, int, 0);
826 MODULE_PARM_DESC(mode,
827 "Video Mode (0=NTSC,1=PAL,2=NTSC 640x480,3=PAL 640x480,4=NTSC YCbCr input,5=PAL YCbCr input)");
829 module_param(mem, int, 0);
830 MODULE_PARM_DESC(mem,
831 "Size of frame buffer memory 1=Single 2=Double Size (allows y-panning / frame stacking)");
833 module_param(nocursor, int, 0644);
834 MODULE_PARM_DESC(nocursor, "cursor enable/disable");