Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Frame buffer driver for ADV7393/2 video encoder |
| 3 | * |
| 4 | * Copyright 2006-2009 Analog Devices Inc. |
| 5 | * Licensed under the GPL-2 or late. |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * TODO: Remove Globals |
| 10 | * TODO: Code Cleanup |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) DRIVER_NAME ": " fmt |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/tty.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/fb.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> |
| 30 | #include <asm/irq.h> |
| 31 | #include <asm/dma.h> |
| 32 | #include <linux/uaccess.h> |
| 33 | #include <linux/gpio.h> |
| 34 | #include <asm/portmux.h> |
| 35 | |
| 36 | #include <linux/dma-mapping.h> |
| 37 | #include <linux/proc_fs.h> |
| 38 | #include <linux/platform_device.h> |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 39 | #include <linux/i2c.h> |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 40 | |
| 41 | #include "bfin_adv7393fb.h" |
| 42 | |
| 43 | static int mode = VMODE; |
| 44 | static int mem = VMEM; |
| 45 | static int nocursor = 1; |
| 46 | |
| 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, |
| 53 | 0 |
| 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * card parameters |
| 58 | */ |
| 59 | |
| 60 | static struct bfin_adv7393_fb_par { |
| 61 | /* structure holding blackfin / adv7393 paramters when |
| 62 | screen is blanked */ |
| 63 | struct { |
| 64 | u8 Mode; /* ntsc/pal/? */ |
| 65 | } vga_state; |
| 66 | atomic_t ref_count; |
| 67 | } bfin_par; |
| 68 | |
| 69 | /* --------------------------------------------------------------------- */ |
| 70 | |
| 71 | static struct fb_var_screeninfo bfin_adv7393_fb_defined = { |
| 72 | .xres = 720, |
| 73 | .yres = 480, |
| 74 | .xres_virtual = 720, |
| 75 | .yres_virtual = 480, |
| 76 | .bits_per_pixel = 16, |
| 77 | .activate = FB_ACTIVATE_TEST, |
| 78 | .height = -1, |
| 79 | .width = -1, |
| 80 | .left_margin = 0, |
| 81 | .right_margin = 0, |
| 82 | .upper_margin = 0, |
| 83 | .lower_margin = 0, |
| 84 | .vmode = FB_VMODE_INTERLACED, |
| 85 | .red = {11, 5, 0}, |
| 86 | .green = {5, 6, 0}, |
| 87 | .blue = {0, 5, 0}, |
| 88 | .transp = {0, 0, 0}, |
| 89 | }; |
| 90 | |
| 91 | static struct fb_fix_screeninfo bfin_adv7393_fb_fix __devinitdata = { |
| 92 | .id = "BFIN ADV7393", |
| 93 | .smem_len = 720 * 480 * 2, |
| 94 | .type = FB_TYPE_PACKED_PIXELS, |
| 95 | .visual = FB_VISUAL_TRUECOLOR, |
| 96 | .xpanstep = 0, |
| 97 | .ypanstep = 0, |
| 98 | .line_length = 720 * 2, |
| 99 | .accel = FB_ACCEL_NONE |
| 100 | }; |
| 101 | |
| 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, |
| 114 | }; |
| 115 | |
| 116 | static int dma_desc_list(struct adv7393fb_device *fbdev, u16 arg) |
| 117 | { |
| 118 | if (arg == BUILD) { /* Build */ |
| 119 | fbdev->vb1 = l1_data_sram_zalloc(sizeof(struct dmasg)); |
| 120 | if (fbdev->vb1 == NULL) |
| 121 | goto error; |
| 122 | |
| 123 | fbdev->av1 = l1_data_sram_zalloc(sizeof(struct dmasg)); |
| 124 | if (fbdev->av1 == NULL) |
| 125 | goto error; |
| 126 | |
| 127 | fbdev->vb2 = l1_data_sram_zalloc(sizeof(struct dmasg)); |
| 128 | if (fbdev->vb2 == NULL) |
| 129 | goto error; |
| 130 | |
| 131 | fbdev->av2 = l1_data_sram_zalloc(sizeof(struct dmasg)); |
| 132 | if (fbdev->av2 == NULL) |
| 133 | goto error; |
| 134 | |
| 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; |
| 140 | |
| 141 | /* Save list head */ |
| 142 | fbdev->descriptor_list_head = fbdev->av2; |
| 143 | |
| 144 | /* Vertical Blanking Field 1 */ |
| 145 | fbdev->vb1->start_addr = VB_DUMMY_MEMORY_SOURCE; |
| 146 | fbdev->vb1->cfg = DMA_CFG_VAL; |
| 147 | |
| 148 | fbdev->vb1->x_count = |
| 149 | fbdev->modes[mode].xres + fbdev->modes[mode].boeft_blank; |
| 150 | |
| 151 | fbdev->vb1->x_modify = 0; |
| 152 | fbdev->vb1->y_count = fbdev->modes[mode].vb1_lines; |
| 153 | fbdev->vb1->y_modify = 0; |
| 154 | |
| 155 | /* Active Video Field 1 */ |
| 156 | |
| 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); |
| 166 | |
| 167 | /* Vertical Blanking Field 2 */ |
| 168 | |
| 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; |
| 173 | |
| 174 | fbdev->vb2->x_modify = 0; |
| 175 | fbdev->vb2->y_count = fbdev->modes[mode].vb2_lines; |
| 176 | fbdev->vb2->y_modify = 0; |
| 177 | |
| 178 | /* Active Video Field 2 */ |
| 179 | |
| 180 | fbdev->av2->start_addr = |
| 181 | (unsigned long)fbdev->fb_mem + fbdev->line_len; |
| 182 | |
| 183 | fbdev->av2->cfg = DMA_CFG_VAL; |
| 184 | |
| 185 | fbdev->av2->x_count = |
| 186 | fbdev->modes[mode].xres + fbdev->modes[mode].boeft_blank; |
| 187 | |
| 188 | fbdev->av2->x_modify = (fbdev->modes[mode].bpp / 8); |
| 189 | fbdev->av2->y_count = fbdev->modes[mode].a_lines; |
| 190 | |
| 191 | fbdev->av2->y_modify = |
| 192 | (fbdev->modes[mode].xres - fbdev->modes[mode].boeft_blank + |
| 193 | 1) * (fbdev->modes[mode].bpp / 8); |
| 194 | |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | error: |
| 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); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int bfin_config_dma(struct adv7393fb_device *fbdev) |
| 208 | { |
| 209 | BUG_ON(!(fbdev->fb_mem)); |
| 210 | |
| 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); |
| 219 | |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | static void bfin_disable_dma(void) |
| 224 | { |
| 225 | bfin_write_DMA0_CONFIG(bfin_read_DMA0_CONFIG() & ~DMAEN); |
| 226 | } |
| 227 | |
| 228 | static void bfin_config_ppi(struct adv7393fb_device *fbdev) |
| 229 | { |
| 230 | if (ANOMALY_05000183) { |
| 231 | bfin_write_TIMER2_CONFIG(WDTH_CAP); |
| 232 | bfin_write_TIMER_ENABLE(TIMEN2); |
| 233 | } |
| 234 | |
| 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); |
| 240 | } |
| 241 | |
| 242 | static void bfin_enable_ppi(void) |
| 243 | { |
| 244 | bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN); |
| 245 | } |
| 246 | |
| 247 | static void bfin_disable_ppi(void) |
| 248 | { |
| 249 | bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() & ~PORT_EN); |
| 250 | } |
| 251 | |
| 252 | static inline int adv7393_write(struct i2c_client *client, u8 reg, u8 value) |
| 253 | { |
| 254 | return i2c_smbus_write_byte_data(client, reg, value); |
| 255 | } |
| 256 | |
| 257 | static inline int adv7393_read(struct i2c_client *client, u8 reg) |
| 258 | { |
| 259 | return i2c_smbus_read_byte_data(client, reg); |
| 260 | } |
| 261 | |
| 262 | static int |
| 263 | adv7393_write_block(struct i2c_client *client, |
| 264 | const u8 *data, unsigned int len) |
| 265 | { |
| 266 | int ret = -1; |
| 267 | u8 reg; |
| 268 | |
| 269 | while (len >= 2) { |
| 270 | reg = *data++; |
| 271 | ret = adv7393_write(client, reg, *data++); |
| 272 | if (ret < 0) |
| 273 | break; |
| 274 | len -= 2; |
| 275 | } |
| 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static int adv7393_mode(struct i2c_client *client, u16 mode) |
| 281 | { |
| 282 | switch (mode) { |
| 283 | case POWER_ON: /* ADV7393 Sleep mode OFF */ |
| 284 | adv7393_write(client, 0x00, 0x1E); |
| 285 | break; |
| 286 | case POWER_DOWN: /* ADV7393 Sleep mode ON */ |
| 287 | adv7393_write(client, 0x00, 0x1F); |
| 288 | break; |
| 289 | case BLANK_OFF: /* Pixel Data Valid */ |
| 290 | adv7393_write(client, 0x82, 0xCB); |
| 291 | break; |
| 292 | case BLANK_ON: /* Pixel Data Invalid */ |
| 293 | adv7393_write(client, 0x82, 0x8B); |
| 294 | break; |
| 295 | default: |
| 296 | return -EINVAL; |
| 297 | break; |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static irqreturn_t ppi_irq_error(int irq, void *dev_id) |
| 303 | { |
| 304 | |
| 305 | struct adv7393fb_device *fbdev = (struct adv7393fb_device *)dev_id; |
| 306 | |
| 307 | u16 status = bfin_read_PPI_STATUS(); |
| 308 | |
| 309 | pr_debug("%s: PPI Status = 0x%X\n", __func__, status); |
| 310 | |
| 311 | if (status) { |
| 312 | bfin_disable_dma(); /* TODO: Check Sequence */ |
| 313 | bfin_disable_ppi(); |
| 314 | bfin_clear_PPI_STATUS(); |
| 315 | bfin_config_dma(fbdev); |
| 316 | bfin_enable_ppi(); |
| 317 | } |
| 318 | |
| 319 | return IRQ_HANDLED; |
| 320 | |
| 321 | } |
| 322 | |
| 323 | static int proc_output(char *buf) |
| 324 | { |
| 325 | char *p = buf; |
| 326 | |
| 327 | p += sprintf(p, |
| 328 | "Usage:\n" |
| 329 | "echo 0x[REG][Value] > adv7393\n" |
| 330 | "example: echo 0x1234 >adv7393\n" |
| 331 | "writes 0x34 into Register 0x12\n"); |
| 332 | |
| 333 | return p - buf; |
| 334 | } |
| 335 | |
| 336 | static int |
| 337 | adv7393_read_proc(char *page, char **start, off_t off, |
| 338 | int count, int *eof, void *data) |
| 339 | { |
| 340 | int len; |
| 341 | |
| 342 | len = proc_output(page); |
| 343 | if (len <= off + count) |
| 344 | *eof = 1; |
| 345 | *start = page + off; |
| 346 | len -= off; |
| 347 | if (len > count) |
| 348 | len = count; |
| 349 | if (len < 0) |
| 350 | len = 0; |
| 351 | return len; |
| 352 | } |
| 353 | |
| 354 | static int |
| 355 | adv7393_write_proc(struct file *file, const char __user * buffer, |
Emil Goode | 5c888aa | 2012-05-29 18:57:00 +0200 | [diff] [blame] | 356 | size_t count, void *data) |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 357 | { |
| 358 | struct adv7393fb_device *fbdev = data; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 359 | unsigned int val; |
| 360 | int ret; |
| 361 | |
Emil Goode | 5c888aa | 2012-05-29 18:57:00 +0200 | [diff] [blame] | 362 | ret = kstrtouint_from_user(buffer, count, 0, &val); |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 363 | if (ret) |
| 364 | return -EFAULT; |
| 365 | |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 366 | adv7393_write(fbdev->client, val >> 8, val & 0xff); |
| 367 | |
| 368 | return count; |
| 369 | } |
| 370 | |
| 371 | static int __devinit bfin_adv7393_fb_probe(struct i2c_client *client, |
| 372 | const struct i2c_device_id *id) |
| 373 | { |
| 374 | int ret = 0; |
| 375 | struct proc_dir_entry *entry; |
| 376 | int num_modes = ARRAY_SIZE(known_modes); |
| 377 | |
| 378 | struct adv7393fb_device *fbdev = NULL; |
| 379 | |
| 380 | if (mem > 2) { |
| 381 | dev_err(&client->dev, "mem out of allowed range [1;2]\n"); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | if (mode > num_modes) { |
| 386 | dev_err(&client->dev, "mode %d: not supported", mode); |
| 387 | return -EFAULT; |
| 388 | } |
| 389 | |
| 390 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); |
| 391 | if (!fbdev) { |
| 392 | dev_err(&client->dev, "failed to allocate device private record"); |
| 393 | return -ENOMEM; |
| 394 | } |
| 395 | |
| 396 | i2c_set_clientdata(client, fbdev); |
| 397 | |
| 398 | fbdev->modes = known_modes; |
| 399 | fbdev->client = client; |
| 400 | |
| 401 | fbdev->fb_len = |
| 402 | mem * fbdev->modes[mode].xres * fbdev->modes[mode].xres * |
| 403 | (fbdev->modes[mode].bpp / 8); |
| 404 | |
| 405 | fbdev->line_len = |
| 406 | fbdev->modes[mode].xres * (fbdev->modes[mode].bpp / 8); |
| 407 | |
| 408 | /* Workaround "PPI Does Not Start Properly In Specific Mode" */ |
| 409 | if (ANOMALY_05000400) { |
Jingoo Han | f8bd493 | 2012-01-26 19:32:34 +0900 | [diff] [blame] | 410 | ret = gpio_request_one(P_IDENT(P_PPI0_FS3), GPIOF_OUT_INIT_LOW, |
| 411 | "PPI0_FS3") |
| 412 | if (ret) { |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 413 | dev_err(&client->dev, "PPI0_FS3 GPIO request failed\n"); |
| 414 | ret = -EBUSY; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 415 | goto free_fbdev; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 416 | } |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (peripheral_request_list(ppi_pins, DRIVER_NAME)) { |
| 420 | dev_err(&client->dev, "requesting PPI peripheral failed\n"); |
| 421 | ret = -EFAULT; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 422 | goto free_gpio; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | fbdev->fb_mem = |
| 426 | dma_alloc_coherent(NULL, fbdev->fb_len, &fbdev->dma_handle, |
| 427 | GFP_KERNEL); |
| 428 | |
| 429 | if (NULL == fbdev->fb_mem) { |
| 430 | dev_err(&client->dev, "couldn't allocate dma buffer (%d bytes)\n", |
| 431 | (u32) fbdev->fb_len); |
| 432 | ret = -ENOMEM; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 433 | goto free_ppi_pins; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | fbdev->info.screen_base = (void *)fbdev->fb_mem; |
| 437 | bfin_adv7393_fb_fix.smem_start = (int)fbdev->fb_mem; |
| 438 | |
| 439 | bfin_adv7393_fb_fix.smem_len = fbdev->fb_len; |
| 440 | bfin_adv7393_fb_fix.line_length = fbdev->line_len; |
| 441 | |
| 442 | if (mem > 1) |
| 443 | bfin_adv7393_fb_fix.ypanstep = 1; |
| 444 | |
| 445 | bfin_adv7393_fb_defined.red.length = 5; |
| 446 | bfin_adv7393_fb_defined.green.length = 6; |
| 447 | bfin_adv7393_fb_defined.blue.length = 5; |
| 448 | |
| 449 | bfin_adv7393_fb_defined.xres = fbdev->modes[mode].xres; |
| 450 | bfin_adv7393_fb_defined.yres = fbdev->modes[mode].yres; |
| 451 | bfin_adv7393_fb_defined.xres_virtual = fbdev->modes[mode].xres; |
| 452 | bfin_adv7393_fb_defined.yres_virtual = mem * fbdev->modes[mode].yres; |
| 453 | bfin_adv7393_fb_defined.bits_per_pixel = fbdev->modes[mode].bpp; |
| 454 | |
| 455 | fbdev->info.fbops = &bfin_adv7393_fb_ops; |
| 456 | fbdev->info.var = bfin_adv7393_fb_defined; |
| 457 | fbdev->info.fix = bfin_adv7393_fb_fix; |
| 458 | fbdev->info.par = &bfin_par; |
| 459 | fbdev->info.flags = FBINFO_DEFAULT; |
| 460 | |
| 461 | fbdev->info.pseudo_palette = kzalloc(sizeof(u32) * 16, GFP_KERNEL); |
| 462 | if (!fbdev->info.pseudo_palette) { |
| 463 | dev_err(&client->dev, "failed to allocate pseudo_palette\n"); |
| 464 | ret = -ENOMEM; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 465 | goto free_fb_mem; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | if (fb_alloc_cmap(&fbdev->info.cmap, BFIN_LCD_NBR_PALETTE_ENTRIES, 0) < 0) { |
| 469 | dev_err(&client->dev, "failed to allocate colormap (%d entries)\n", |
| 470 | BFIN_LCD_NBR_PALETTE_ENTRIES); |
| 471 | ret = -EFAULT; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 472 | goto free_palette; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | if (request_dma(CH_PPI, "BF5xx_PPI_DMA") < 0) { |
| 476 | dev_err(&client->dev, "unable to request PPI DMA\n"); |
| 477 | ret = -EFAULT; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 478 | goto free_cmap; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Yong Zhang | f8798cc | 2011-09-22 16:59:16 +0800 | [diff] [blame] | 481 | if (request_irq(IRQ_PPI_ERROR, ppi_irq_error, 0, |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 482 | "PPI ERROR", fbdev) < 0) { |
| 483 | dev_err(&client->dev, "unable to request PPI ERROR IRQ\n"); |
| 484 | ret = -EFAULT; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 485 | goto free_ch_ppi; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | fbdev->open = 0; |
| 489 | |
| 490 | ret = adv7393_write_block(client, fbdev->modes[mode].adv7393_i2c_initd, |
| 491 | fbdev->modes[mode].adv7393_i2c_initd_len); |
| 492 | |
| 493 | if (ret) { |
| 494 | dev_err(&client->dev, "i2c attach: init error\n"); |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 495 | goto free_irq_ppi; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
| 499 | if (register_framebuffer(&fbdev->info) < 0) { |
| 500 | dev_err(&client->dev, "unable to register framebuffer\n"); |
| 501 | ret = -EFAULT; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 502 | goto free_irq_ppi; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | dev_info(&client->dev, "fb%d: %s frame buffer device\n", |
| 506 | fbdev->info.node, fbdev->info.fix.id); |
| 507 | dev_info(&client->dev, "fb memory address : 0x%p\n", fbdev->fb_mem); |
| 508 | |
| 509 | entry = create_proc_entry("driver/adv7393", 0, NULL); |
| 510 | if (!entry) { |
| 511 | dev_err(&client->dev, "unable to create /proc entry\n"); |
| 512 | ret = -EFAULT; |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 513 | goto free_fb; |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | entry->read_proc = adv7393_read_proc; |
| 517 | entry->write_proc = adv7393_write_proc; |
| 518 | entry->data = fbdev; |
| 519 | |
| 520 | return 0; |
| 521 | |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 522 | free_fb: |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 523 | unregister_framebuffer(&fbdev->info); |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 524 | free_irq_ppi: |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 525 | free_irq(IRQ_PPI_ERROR, fbdev); |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 526 | free_ch_ppi: |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 527 | free_dma(CH_PPI); |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 528 | free_cmap: |
| 529 | fb_dealloc_cmap(&fbdev->info.cmap); |
| 530 | free_palette: |
| 531 | kfree(fbdev->info.pseudo_palette); |
| 532 | free_fb_mem: |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 533 | dma_free_coherent(NULL, fbdev->fb_len, fbdev->fb_mem, |
| 534 | fbdev->dma_handle); |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 535 | free_ppi_pins: |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 536 | peripheral_free_list(ppi_pins); |
Emil Goode | c895305e | 2012-05-28 18:54:51 +0200 | [diff] [blame] | 537 | free_gpio: |
| 538 | if (ANOMALY_05000400) |
| 539 | gpio_free(P_IDENT(P_PPI0_FS3)); |
| 540 | free_fbdev: |
Michael Hennerich | cffd934 | 2010-11-24 09:33:59 +0000 | [diff] [blame] | 541 | kfree(fbdev); |
| 542 | |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | static int bfin_adv7393_fb_open(struct fb_info *info, int user) |
| 547 | { |
| 548 | struct adv7393fb_device *fbdev = to_adv7393fb_device(info); |
| 549 | |
| 550 | fbdev->info.screen_base = (void *)fbdev->fb_mem; |
| 551 | if (!fbdev->info.screen_base) { |
| 552 | dev_err(&fbdev->client->dev, "unable to map device\n"); |
| 553 | return -ENOMEM; |
| 554 | } |
| 555 | |
| 556 | fbdev->open = 1; |
| 557 | dma_desc_list(fbdev, BUILD); |
| 558 | adv7393_mode(fbdev->client, BLANK_OFF); |
| 559 | bfin_config_ppi(fbdev); |
| 560 | bfin_config_dma(fbdev); |
| 561 | bfin_enable_ppi(); |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int bfin_adv7393_fb_release(struct fb_info *info, int user) |
| 567 | { |
| 568 | struct adv7393fb_device *fbdev = to_adv7393fb_device(info); |
| 569 | |
| 570 | adv7393_mode(fbdev->client, BLANK_ON); |
| 571 | bfin_disable_dma(); |
| 572 | bfin_disable_ppi(); |
| 573 | dma_desc_list(fbdev, DESTRUCT); |
| 574 | fbdev->open = 0; |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int |
| 579 | bfin_adv7393_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) |
| 580 | { |
| 581 | |
| 582 | switch (var->bits_per_pixel) { |
| 583 | case 16:/* DIRECTCOLOUR, 64k */ |
| 584 | var->red.offset = info->var.red.offset; |
| 585 | var->green.offset = info->var.green.offset; |
| 586 | var->blue.offset = info->var.blue.offset; |
| 587 | var->red.length = info->var.red.length; |
| 588 | var->green.length = info->var.green.length; |
| 589 | var->blue.length = info->var.blue.length; |
| 590 | var->transp.offset = 0; |
| 591 | var->transp.length = 0; |
| 592 | var->transp.msb_right = 0; |
| 593 | var->red.msb_right = 0; |
| 594 | var->green.msb_right = 0; |
| 595 | var->blue.msb_right = 0; |
| 596 | break; |
| 597 | default: |
| 598 | pr_debug("%s: depth not supported: %u BPP\n", __func__, |
| 599 | var->bits_per_pixel); |
| 600 | return -EINVAL; |
| 601 | } |
| 602 | |
| 603 | if (info->var.xres != var->xres || |
| 604 | info->var.yres != var->yres || |
| 605 | info->var.xres_virtual != var->xres_virtual || |
| 606 | info->var.yres_virtual != var->yres_virtual) { |
| 607 | pr_debug("%s: Resolution not supported: X%u x Y%u\n", |
| 608 | __func__, var->xres, var->yres); |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Memory limit |
| 614 | */ |
| 615 | |
| 616 | if ((info->fix.line_length * var->yres_virtual) > info->fix.smem_len) { |
| 617 | pr_debug("%s: Memory Limit requested yres_virtual = %u\n", |
| 618 | __func__, var->yres_virtual); |
| 619 | return -ENOMEM; |
| 620 | } |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int |
| 626 | bfin_adv7393_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info) |
| 627 | { |
| 628 | int dy; |
| 629 | u32 dmaaddr; |
| 630 | struct adv7393fb_device *fbdev = to_adv7393fb_device(info); |
| 631 | |
| 632 | if (!var || !info) |
| 633 | return -EINVAL; |
| 634 | |
| 635 | if (var->xoffset - info->var.xoffset) { |
| 636 | /* No support for X panning for now! */ |
| 637 | return -EINVAL; |
| 638 | } |
| 639 | dy = var->yoffset - info->var.yoffset; |
| 640 | |
| 641 | if (dy) { |
| 642 | pr_debug("%s: Panning screen of %d lines\n", __func__, dy); |
| 643 | |
| 644 | dmaaddr = fbdev->av1->start_addr; |
| 645 | dmaaddr += (info->fix.line_length * dy); |
| 646 | /* TODO: Wait for current frame to finished */ |
| 647 | |
| 648 | fbdev->av1->start_addr = (unsigned long)dmaaddr; |
| 649 | fbdev->av2->start_addr = (unsigned long)dmaaddr + fbdev->line_len; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | |
| 654 | } |
| 655 | |
| 656 | /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */ |
| 657 | static int bfin_adv7393_fb_blank(int blank, struct fb_info *info) |
| 658 | { |
| 659 | struct adv7393fb_device *fbdev = to_adv7393fb_device(info); |
| 660 | |
| 661 | switch (blank) { |
| 662 | |
| 663 | case VESA_NO_BLANKING: |
| 664 | /* Turn on panel */ |
| 665 | adv7393_mode(fbdev->client, BLANK_OFF); |
| 666 | break; |
| 667 | |
| 668 | case VESA_VSYNC_SUSPEND: |
| 669 | case VESA_HSYNC_SUSPEND: |
| 670 | case VESA_POWERDOWN: |
| 671 | /* Turn off panel */ |
| 672 | adv7393_mode(fbdev->client, BLANK_ON); |
| 673 | break; |
| 674 | |
| 675 | default: |
| 676 | return -EINVAL; |
| 677 | break; |
| 678 | } |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | int bfin_adv7393_fb_cursor(struct fb_info *info, struct fb_cursor *cursor) |
| 683 | { |
| 684 | if (nocursor) |
| 685 | return 0; |
| 686 | else |
| 687 | return -EINVAL; /* just to force soft_cursor() call */ |
| 688 | } |
| 689 | |
| 690 | static int bfin_adv7393_fb_setcolreg(u_int regno, u_int red, u_int green, |
| 691 | u_int blue, u_int transp, |
| 692 | struct fb_info *info) |
| 693 | { |
| 694 | if (regno >= BFIN_LCD_NBR_PALETTE_ENTRIES) |
| 695 | return -EINVAL; |
| 696 | |
| 697 | if (info->var.grayscale) |
| 698 | /* grayscale = 0.30*R + 0.59*G + 0.11*B */ |
| 699 | red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8; |
| 700 | |
| 701 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) { |
| 702 | u32 value; |
| 703 | /* Place color in the pseudopalette */ |
| 704 | if (regno > 16) |
| 705 | return -EINVAL; |
| 706 | |
| 707 | red >>= (16 - info->var.red.length); |
| 708 | green >>= (16 - info->var.green.length); |
| 709 | blue >>= (16 - info->var.blue.length); |
| 710 | |
| 711 | value = (red << info->var.red.offset) | |
| 712 | (green << info->var.green.offset)| |
| 713 | (blue << info->var.blue.offset); |
| 714 | value &= 0xFFFF; |
| 715 | |
| 716 | ((u32 *) (info->pseudo_palette))[regno] = value; |
| 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int __devexit bfin_adv7393_fb_remove(struct i2c_client *client) |
| 723 | { |
| 724 | struct adv7393fb_device *fbdev = i2c_get_clientdata(client); |
| 725 | |
| 726 | adv7393_mode(client, POWER_DOWN); |
| 727 | |
| 728 | if (fbdev->fb_mem) |
| 729 | dma_free_coherent(NULL, fbdev->fb_len, fbdev->fb_mem, fbdev->dma_handle); |
| 730 | free_dma(CH_PPI); |
| 731 | free_irq(IRQ_PPI_ERROR, fbdev); |
| 732 | unregister_framebuffer(&fbdev->info); |
| 733 | remove_proc_entry("driver/adv7393", NULL); |
| 734 | fb_dealloc_cmap(&fbdev->info.cmap); |
| 735 | kfree(fbdev->info.pseudo_palette); |
| 736 | |
| 737 | if (ANOMALY_05000400) |
| 738 | gpio_free(P_IDENT(P_PPI0_FS3)); /* FS3 */ |
| 739 | peripheral_free_list(ppi_pins); |
| 740 | kfree(fbdev); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | #ifdef CONFIG_PM |
| 746 | static int bfin_adv7393_fb_suspend(struct device *dev) |
| 747 | { |
| 748 | struct adv7393fb_device *fbdev = dev_get_drvdata(dev); |
| 749 | |
| 750 | if (fbdev->open) { |
| 751 | bfin_disable_dma(); |
| 752 | bfin_disable_ppi(); |
| 753 | dma_desc_list(fbdev, DESTRUCT); |
| 754 | } |
| 755 | adv7393_mode(fbdev->client, POWER_DOWN); |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static int bfin_adv7393_fb_resume(struct device *dev) |
| 761 | { |
| 762 | struct adv7393fb_device *fbdev = dev_get_drvdata(dev); |
| 763 | |
| 764 | adv7393_mode(fbdev->client, POWER_ON); |
| 765 | |
| 766 | if (fbdev->open) { |
| 767 | dma_desc_list(fbdev, BUILD); |
| 768 | bfin_config_ppi(fbdev); |
| 769 | bfin_config_dma(fbdev); |
| 770 | bfin_enable_ppi(); |
| 771 | } |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static const struct dev_pm_ops bfin_adv7393_dev_pm_ops = { |
| 777 | .suspend = bfin_adv7393_fb_suspend, |
| 778 | .resume = bfin_adv7393_fb_resume, |
| 779 | }; |
| 780 | #endif |
| 781 | |
| 782 | static const struct i2c_device_id bfin_adv7393_id[] = { |
| 783 | {DRIVER_NAME, 0}, |
| 784 | {} |
| 785 | }; |
| 786 | |
| 787 | MODULE_DEVICE_TABLE(i2c, bfin_adv7393_id); |
| 788 | |
| 789 | static struct i2c_driver bfin_adv7393_fb_driver = { |
| 790 | .driver = { |
| 791 | .name = DRIVER_NAME, |
| 792 | #ifdef CONFIG_PM |
| 793 | .pm = &bfin_adv7393_dev_pm_ops, |
| 794 | #endif |
| 795 | }, |
| 796 | .probe = bfin_adv7393_fb_probe, |
| 797 | .remove = __devexit_p(bfin_adv7393_fb_remove), |
| 798 | .id_table = bfin_adv7393_id, |
| 799 | }; |
| 800 | |
| 801 | static int __init bfin_adv7393_fb_driver_init(void) |
| 802 | { |
| 803 | #if defined(CONFIG_I2C_BLACKFIN_TWI) || defined(CONFIG_I2C_BLACKFIN_TWI_MODULE) |
| 804 | request_module("i2c-bfin-twi"); |
| 805 | #else |
| 806 | request_module("i2c-gpio"); |
| 807 | #endif |
| 808 | |
| 809 | return i2c_add_driver(&bfin_adv7393_fb_driver); |
| 810 | } |
| 811 | module_init(bfin_adv7393_fb_driver_init); |
| 812 | |
| 813 | static void __exit bfin_adv7393_fb_driver_cleanup(void) |
| 814 | { |
| 815 | i2c_del_driver(&bfin_adv7393_fb_driver); |
| 816 | } |
| 817 | module_exit(bfin_adv7393_fb_driver_cleanup); |
| 818 | |
| 819 | MODULE_LICENSE("GPL"); |
| 820 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 821 | MODULE_DESCRIPTION("Frame buffer driver for ADV7393/2 Video Encoder"); |
| 822 | |
| 823 | module_param(mode, int, 0); |
| 824 | MODULE_PARM_DESC(mode, |
| 825 | "Video Mode (0=NTSC,1=PAL,2=NTSC 640x480,3=PAL 640x480,4=NTSC YCbCr input,5=PAL YCbCr input)"); |
| 826 | |
| 827 | module_param(mem, int, 0); |
| 828 | MODULE_PARM_DESC(mem, |
| 829 | "Size of frame buffer memory 1=Single 2=Double Size (allows y-panning / frame stacking)"); |
| 830 | |
| 831 | module_param(nocursor, int, 0644); |
| 832 | MODULE_PARM_DESC(nocursor, "cursor enable/disable"); |