Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved. |
| 3 | * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved. |
| 4 | * Copyright 2009 Jonathan Corbet <corbet@lwn.net> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Core code for the Via multifunction framebuffer device. |
| 9 | */ |
Jonathan Corbet | ec66841 | 2010-05-05 14:44:55 -0600 | [diff] [blame] | 10 | #include <linux/via-core.h> |
| 11 | #include <linux/via_i2c.h> |
| 12 | #include <linux/via-gpio.h> |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 13 | #include "global.h" |
| 14 | |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Jonathan Corbet | 3d28eb4 | 2010-04-23 10:04:12 -0600 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * The default port config. |
| 21 | */ |
| 22 | static struct via_port_cfg adap_configs[] = { |
| 23 | [VIA_PORT_26] = { VIA_PORT_I2C, VIA_MODE_OFF, VIASR, 0x26 }, |
| 24 | [VIA_PORT_31] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x31 }, |
| 25 | [VIA_PORT_25] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x25 }, |
| 26 | [VIA_PORT_2C] = { VIA_PORT_GPIO, VIA_MODE_I2C, VIASR, 0x2c }, |
| 27 | [VIA_PORT_3D] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x3d }, |
| 28 | { 0, 0, 0, 0 } |
| 29 | }; |
| 30 | |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 31 | /* |
| 32 | * We currently only support one viafb device (will there ever be |
| 33 | * more than one?), so just declare it globally here. |
| 34 | */ |
| 35 | static struct viafb_dev global_dev; |
| 36 | |
| 37 | |
| 38 | /* |
Jonathan Corbet | 94dd1a8 | 2010-04-23 09:46:59 -0600 | [diff] [blame] | 39 | * Basic register access; spinlock required. |
| 40 | */ |
| 41 | static inline void viafb_mmio_write(int reg, u32 v) |
| 42 | { |
| 43 | iowrite32(v, global_dev.engine_mmio + reg); |
| 44 | } |
| 45 | |
| 46 | static inline int viafb_mmio_read(int reg) |
| 47 | { |
| 48 | return ioread32(global_dev.engine_mmio + reg); |
| 49 | } |
| 50 | |
| 51 | /* ---------------------------------------------------------------------- */ |
| 52 | /* |
| 53 | * Interrupt management. We have a single IRQ line for a lot of |
| 54 | * different functions, so we need to share it. The design here |
| 55 | * is that we don't want to reimplement the shared IRQ code here; |
| 56 | * we also want to avoid having contention for a single handler thread. |
| 57 | * So each subdev driver which needs interrupts just requests |
| 58 | * them directly from the kernel. We just have what's needed for |
| 59 | * overall access to the interrupt control register. |
| 60 | */ |
| 61 | |
| 62 | /* |
| 63 | * Which interrupts are enabled now? |
| 64 | */ |
| 65 | static u32 viafb_enabled_ints; |
| 66 | |
Henrik Kretzschmar | eca9c47 | 2010-06-17 12:01:24 +0200 | [diff] [blame] | 67 | static void __devinit viafb_int_init(void) |
Jonathan Corbet | 94dd1a8 | 2010-04-23 09:46:59 -0600 | [diff] [blame] | 68 | { |
| 69 | viafb_enabled_ints = 0; |
| 70 | |
| 71 | viafb_mmio_write(VDE_INTERRUPT, 0); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Allow subdevs to ask for specific interrupts to be enabled. These |
| 76 | * functions must be called with reg_lock held |
| 77 | */ |
| 78 | void viafb_irq_enable(u32 mask) |
| 79 | { |
| 80 | viafb_enabled_ints |= mask; |
| 81 | viafb_mmio_write(VDE_INTERRUPT, viafb_enabled_ints | VDE_I_ENABLE); |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(viafb_irq_enable); |
| 84 | |
| 85 | void viafb_irq_disable(u32 mask) |
| 86 | { |
| 87 | viafb_enabled_ints &= ~mask; |
| 88 | if (viafb_enabled_ints == 0) |
| 89 | viafb_mmio_write(VDE_INTERRUPT, 0); /* Disable entirely */ |
| 90 | else |
| 91 | viafb_mmio_write(VDE_INTERRUPT, |
| 92 | viafb_enabled_ints | VDE_I_ENABLE); |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(viafb_irq_disable); |
| 95 | |
Jonathan Corbet | 3d28eb4 | 2010-04-23 10:04:12 -0600 | [diff] [blame] | 96 | /* ---------------------------------------------------------------------- */ |
| 97 | /* |
Jonathan Corbet | 024fafbac | 2010-10-19 21:32:11 -0300 | [diff] [blame^] | 98 | * Currently, the camera driver is the only user of the DMA code, so we |
| 99 | * only compile it in if the camera driver is being built. Chances are, |
| 100 | * most viafb systems will not need to have this extra code for a while. |
| 101 | * As soon as another user comes long, the ifdef can be removed. |
| 102 | */ |
| 103 | #if defined(CONFIG_VIDEO_VIA_CAMERA) || defined(CONFIG_VIDEO_VIA_CAMERA_MODULE) |
| 104 | /* |
Jonathan Corbet | 3d28eb4 | 2010-04-23 10:04:12 -0600 | [diff] [blame] | 105 | * Access to the DMA engine. This currently provides what the camera |
| 106 | * driver needs (i.e. outgoing only) but is easily expandable if need |
| 107 | * be. |
| 108 | */ |
Jonathan Corbet | 94dd1a8 | 2010-04-23 09:46:59 -0600 | [diff] [blame] | 109 | |
| 110 | /* |
Jonathan Corbet | 3d28eb4 | 2010-04-23 10:04:12 -0600 | [diff] [blame] | 111 | * There are four DMA channels in the vx855. For now, we only |
| 112 | * use one of them, though. Most of the time, the DMA channel |
| 113 | * will be idle, so we keep the IRQ handler unregistered except |
| 114 | * when some subsystem has indicated an interest. |
| 115 | */ |
| 116 | static int viafb_dma_users; |
| 117 | static DECLARE_COMPLETION(viafb_dma_completion); |
| 118 | /* |
| 119 | * This mutex protects viafb_dma_users and our global interrupt |
| 120 | * registration state; it also serializes access to the DMA |
| 121 | * engine. |
| 122 | */ |
| 123 | static DEFINE_MUTEX(viafb_dma_lock); |
| 124 | |
| 125 | /* |
| 126 | * The VX855 DMA descriptor (used for s/g transfers) looks |
| 127 | * like this. |
| 128 | */ |
| 129 | struct viafb_vx855_dma_descr { |
| 130 | u32 addr_low; /* Low part of phys addr */ |
| 131 | u32 addr_high; /* High 12 bits of addr */ |
| 132 | u32 fb_offset; /* Offset into FB memory */ |
| 133 | u32 seg_size; /* Size, 16-byte units */ |
| 134 | u32 tile_mode; /* "tile mode" setting */ |
| 135 | u32 next_desc_low; /* Next descriptor addr */ |
| 136 | u32 next_desc_high; |
| 137 | u32 pad; /* Fill out to 64 bytes */ |
| 138 | }; |
| 139 | |
| 140 | /* |
| 141 | * Flags added to the "next descriptor low" pointers |
| 142 | */ |
| 143 | #define VIAFB_DMA_MAGIC 0x01 /* ??? Just has to be there */ |
| 144 | #define VIAFB_DMA_FINAL_SEGMENT 0x02 /* Final segment */ |
| 145 | |
| 146 | /* |
| 147 | * The completion IRQ handler. |
| 148 | */ |
| 149 | static irqreturn_t viafb_dma_irq(int irq, void *data) |
| 150 | { |
| 151 | int csr; |
| 152 | irqreturn_t ret = IRQ_NONE; |
| 153 | |
| 154 | spin_lock(&global_dev.reg_lock); |
| 155 | csr = viafb_mmio_read(VDMA_CSR0); |
| 156 | if (csr & VDMA_C_DONE) { |
| 157 | viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE); |
| 158 | complete(&viafb_dma_completion); |
| 159 | ret = IRQ_HANDLED; |
| 160 | } |
| 161 | spin_unlock(&global_dev.reg_lock); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Indicate a need for DMA functionality. |
| 167 | */ |
| 168 | int viafb_request_dma(void) |
| 169 | { |
| 170 | int ret = 0; |
| 171 | |
| 172 | /* |
| 173 | * Only VX855 is supported currently. |
| 174 | */ |
| 175 | if (global_dev.chip_type != UNICHROME_VX855) |
| 176 | return -ENODEV; |
| 177 | /* |
| 178 | * Note the new user and set up our interrupt handler |
| 179 | * if need be. |
| 180 | */ |
| 181 | mutex_lock(&viafb_dma_lock); |
| 182 | viafb_dma_users++; |
| 183 | if (viafb_dma_users == 1) { |
| 184 | ret = request_irq(global_dev.pdev->irq, viafb_dma_irq, |
| 185 | IRQF_SHARED, "via-dma", &viafb_dma_users); |
| 186 | if (ret) |
| 187 | viafb_dma_users--; |
| 188 | else |
| 189 | viafb_irq_enable(VDE_I_DMA0TDEN); |
| 190 | } |
| 191 | mutex_unlock(&viafb_dma_lock); |
| 192 | return ret; |
| 193 | } |
| 194 | EXPORT_SYMBOL_GPL(viafb_request_dma); |
| 195 | |
| 196 | void viafb_release_dma(void) |
| 197 | { |
| 198 | mutex_lock(&viafb_dma_lock); |
| 199 | viafb_dma_users--; |
| 200 | if (viafb_dma_users == 0) { |
| 201 | viafb_irq_disable(VDE_I_DMA0TDEN); |
| 202 | free_irq(global_dev.pdev->irq, &viafb_dma_users); |
| 203 | } |
| 204 | mutex_unlock(&viafb_dma_lock); |
| 205 | } |
| 206 | EXPORT_SYMBOL_GPL(viafb_release_dma); |
| 207 | |
| 208 | |
| 209 | #if 0 |
| 210 | /* |
| 211 | * Copy a single buffer from FB memory, synchronously. This code works |
| 212 | * but is not currently used. |
| 213 | */ |
| 214 | void viafb_dma_copy_out(unsigned int offset, dma_addr_t paddr, int len) |
| 215 | { |
| 216 | unsigned long flags; |
| 217 | int csr; |
| 218 | |
| 219 | mutex_lock(&viafb_dma_lock); |
| 220 | init_completion(&viafb_dma_completion); |
| 221 | /* |
| 222 | * Program the controller. |
| 223 | */ |
| 224 | spin_lock_irqsave(&global_dev.reg_lock, flags); |
| 225 | viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE); |
| 226 | /* Enable ints; must happen after CSR0 write! */ |
| 227 | viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE); |
| 228 | viafb_mmio_write(VDMA_MARL0, (int) (paddr & 0xfffffff0)); |
| 229 | viafb_mmio_write(VDMA_MARH0, (int) ((paddr >> 28) & 0xfff)); |
| 230 | /* Data sheet suggests DAR0 should be <<4, but it lies */ |
| 231 | viafb_mmio_write(VDMA_DAR0, offset); |
| 232 | viafb_mmio_write(VDMA_DQWCR0, len >> 4); |
| 233 | viafb_mmio_write(VDMA_TMR0, 0); |
| 234 | viafb_mmio_write(VDMA_DPRL0, 0); |
| 235 | viafb_mmio_write(VDMA_DPRH0, 0); |
| 236 | viafb_mmio_write(VDMA_PMR0, 0); |
| 237 | csr = viafb_mmio_read(VDMA_CSR0); |
| 238 | viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START); |
| 239 | spin_unlock_irqrestore(&global_dev.reg_lock, flags); |
| 240 | /* |
| 241 | * Now we just wait until the interrupt handler says |
| 242 | * we're done. |
| 243 | */ |
| 244 | wait_for_completion_interruptible(&viafb_dma_completion); |
| 245 | viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */ |
| 246 | mutex_unlock(&viafb_dma_lock); |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(viafb_dma_copy_out); |
| 249 | #endif |
| 250 | |
| 251 | /* |
| 252 | * Do a scatter/gather DMA copy from FB memory. You must have done |
| 253 | * a successful call to viafb_request_dma() first. |
| 254 | */ |
| 255 | int viafb_dma_copy_out_sg(unsigned int offset, struct scatterlist *sg, int nsg) |
| 256 | { |
| 257 | struct viafb_vx855_dma_descr *descr; |
| 258 | void *descrpages; |
| 259 | dma_addr_t descr_handle; |
| 260 | unsigned long flags; |
| 261 | int i; |
| 262 | struct scatterlist *sgentry; |
| 263 | dma_addr_t nextdesc; |
| 264 | |
| 265 | /* |
| 266 | * Get a place to put the descriptors. |
| 267 | */ |
| 268 | descrpages = dma_alloc_coherent(&global_dev.pdev->dev, |
| 269 | nsg*sizeof(struct viafb_vx855_dma_descr), |
| 270 | &descr_handle, GFP_KERNEL); |
| 271 | if (descrpages == NULL) { |
| 272 | dev_err(&global_dev.pdev->dev, "Unable to get descr page.\n"); |
| 273 | return -ENOMEM; |
| 274 | } |
| 275 | mutex_lock(&viafb_dma_lock); |
| 276 | /* |
| 277 | * Fill them in. |
| 278 | */ |
| 279 | descr = descrpages; |
| 280 | nextdesc = descr_handle + sizeof(struct viafb_vx855_dma_descr); |
| 281 | for_each_sg(sg, sgentry, nsg, i) { |
| 282 | dma_addr_t paddr = sg_dma_address(sgentry); |
| 283 | descr->addr_low = paddr & 0xfffffff0; |
| 284 | descr->addr_high = ((u64) paddr >> 32) & 0x0fff; |
| 285 | descr->fb_offset = offset; |
| 286 | descr->seg_size = sg_dma_len(sgentry) >> 4; |
| 287 | descr->tile_mode = 0; |
| 288 | descr->next_desc_low = (nextdesc&0xfffffff0) | VIAFB_DMA_MAGIC; |
| 289 | descr->next_desc_high = ((u64) nextdesc >> 32) & 0x0fff; |
| 290 | descr->pad = 0xffffffff; /* VIA driver does this */ |
| 291 | offset += sg_dma_len(sgentry); |
| 292 | nextdesc += sizeof(struct viafb_vx855_dma_descr); |
| 293 | descr++; |
| 294 | } |
| 295 | descr[-1].next_desc_low = VIAFB_DMA_FINAL_SEGMENT|VIAFB_DMA_MAGIC; |
| 296 | /* |
| 297 | * Program the engine. |
| 298 | */ |
| 299 | spin_lock_irqsave(&global_dev.reg_lock, flags); |
| 300 | init_completion(&viafb_dma_completion); |
| 301 | viafb_mmio_write(VDMA_DQWCR0, 0); |
| 302 | viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE); |
| 303 | viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE | VDMA_MR_CHAIN); |
| 304 | viafb_mmio_write(VDMA_DPRL0, descr_handle | VIAFB_DMA_MAGIC); |
| 305 | viafb_mmio_write(VDMA_DPRH0, |
| 306 | (((u64)descr_handle >> 32) & 0x0fff) | 0xf0000); |
| 307 | (void) viafb_mmio_read(VDMA_CSR0); |
| 308 | viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START); |
| 309 | spin_unlock_irqrestore(&global_dev.reg_lock, flags); |
| 310 | /* |
| 311 | * Now we just wait until the interrupt handler says |
| 312 | * we're done. Except that, actually, we need to wait a little |
| 313 | * longer: the interrupts seem to jump the gun a little and we |
| 314 | * get corrupted frames sometimes. |
| 315 | */ |
| 316 | wait_for_completion_timeout(&viafb_dma_completion, 1); |
| 317 | msleep(1); |
| 318 | if ((viafb_mmio_read(VDMA_CSR0)&VDMA_C_DONE) == 0) |
| 319 | printk(KERN_ERR "VIA DMA timeout!\n"); |
| 320 | /* |
| 321 | * Clean up and we're done. |
| 322 | */ |
| 323 | viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE); |
| 324 | viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */ |
| 325 | mutex_unlock(&viafb_dma_lock); |
| 326 | dma_free_coherent(&global_dev.pdev->dev, |
| 327 | nsg*sizeof(struct viafb_vx855_dma_descr), descrpages, |
| 328 | descr_handle); |
| 329 | return 0; |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(viafb_dma_copy_out_sg); |
Jonathan Corbet | 024fafbac | 2010-10-19 21:32:11 -0300 | [diff] [blame^] | 332 | #endif /* CONFIG_VIDEO_VIA_CAMERA */ |
Jonathan Corbet | 3d28eb4 | 2010-04-23 10:04:12 -0600 | [diff] [blame] | 333 | |
| 334 | /* ---------------------------------------------------------------------- */ |
| 335 | /* |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 336 | * Figure out how big our framebuffer memory is. Kind of ugly, |
| 337 | * but evidently we can't trust the information found in the |
| 338 | * fbdev configuration area. |
| 339 | */ |
| 340 | static u16 via_function3[] = { |
| 341 | CLE266_FUNCTION3, KM400_FUNCTION3, CN400_FUNCTION3, CN700_FUNCTION3, |
| 342 | CX700_FUNCTION3, KM800_FUNCTION3, KM890_FUNCTION3, P4M890_FUNCTION3, |
| 343 | P4M900_FUNCTION3, VX800_FUNCTION3, VX855_FUNCTION3, |
| 344 | }; |
| 345 | |
| 346 | /* Get the BIOS-configured framebuffer size from PCI configuration space |
| 347 | * of function 3 in the respective chipset */ |
| 348 | static int viafb_get_fb_size_from_pci(int chip_type) |
| 349 | { |
| 350 | int i; |
| 351 | u8 offset = 0; |
| 352 | u32 FBSize; |
| 353 | u32 VideoMemSize; |
| 354 | |
| 355 | /* search for the "FUNCTION3" device in this chipset */ |
| 356 | for (i = 0; i < ARRAY_SIZE(via_function3); i++) { |
| 357 | struct pci_dev *pdev; |
| 358 | |
| 359 | pdev = pci_get_device(PCI_VENDOR_ID_VIA, via_function3[i], |
| 360 | NULL); |
| 361 | if (!pdev) |
| 362 | continue; |
| 363 | |
| 364 | DEBUG_MSG(KERN_INFO "Device ID = %x\n", pdev->device); |
| 365 | |
| 366 | switch (pdev->device) { |
| 367 | case CLE266_FUNCTION3: |
| 368 | case KM400_FUNCTION3: |
| 369 | offset = 0xE0; |
| 370 | break; |
| 371 | case CN400_FUNCTION3: |
| 372 | case CN700_FUNCTION3: |
| 373 | case CX700_FUNCTION3: |
| 374 | case KM800_FUNCTION3: |
| 375 | case KM890_FUNCTION3: |
| 376 | case P4M890_FUNCTION3: |
| 377 | case P4M900_FUNCTION3: |
| 378 | case VX800_FUNCTION3: |
| 379 | case VX855_FUNCTION3: |
| 380 | /*case CN750_FUNCTION3: */ |
| 381 | offset = 0xA0; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | if (!offset) |
| 386 | break; |
| 387 | |
| 388 | pci_read_config_dword(pdev, offset, &FBSize); |
| 389 | pci_dev_put(pdev); |
| 390 | } |
| 391 | |
| 392 | if (!offset) { |
| 393 | printk(KERN_ERR "cannot determine framebuffer size\n"); |
| 394 | return -EIO; |
| 395 | } |
| 396 | |
| 397 | FBSize = FBSize & 0x00007000; |
| 398 | DEBUG_MSG(KERN_INFO "FB Size = %x\n", FBSize); |
| 399 | |
| 400 | if (chip_type < UNICHROME_CX700) { |
| 401 | switch (FBSize) { |
| 402 | case 0x00004000: |
| 403 | VideoMemSize = (16 << 20); /*16M */ |
| 404 | break; |
| 405 | |
| 406 | case 0x00005000: |
| 407 | VideoMemSize = (32 << 20); /*32M */ |
| 408 | break; |
| 409 | |
| 410 | case 0x00006000: |
| 411 | VideoMemSize = (64 << 20); /*64M */ |
| 412 | break; |
| 413 | |
| 414 | default: |
| 415 | VideoMemSize = (32 << 20); /*32M */ |
| 416 | break; |
| 417 | } |
| 418 | } else { |
| 419 | switch (FBSize) { |
| 420 | case 0x00001000: |
| 421 | VideoMemSize = (8 << 20); /*8M */ |
| 422 | break; |
| 423 | |
| 424 | case 0x00002000: |
| 425 | VideoMemSize = (16 << 20); /*16M */ |
| 426 | break; |
| 427 | |
| 428 | case 0x00003000: |
| 429 | VideoMemSize = (32 << 20); /*32M */ |
| 430 | break; |
| 431 | |
| 432 | case 0x00004000: |
| 433 | VideoMemSize = (64 << 20); /*64M */ |
| 434 | break; |
| 435 | |
| 436 | case 0x00005000: |
| 437 | VideoMemSize = (128 << 20); /*128M */ |
| 438 | break; |
| 439 | |
| 440 | case 0x00006000: |
| 441 | VideoMemSize = (256 << 20); /*256M */ |
| 442 | break; |
| 443 | |
| 444 | case 0x00007000: /* Only on VX855/875 */ |
| 445 | VideoMemSize = (512 << 20); /*512M */ |
| 446 | break; |
| 447 | |
| 448 | default: |
| 449 | VideoMemSize = (32 << 20); /*32M */ |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return VideoMemSize; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /* |
| 459 | * Figure out and map our MMIO regions. |
| 460 | */ |
| 461 | static int __devinit via_pci_setup_mmio(struct viafb_dev *vdev) |
| 462 | { |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 463 | int ret; |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 464 | /* |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 465 | * Hook up to the device registers. Note that we soldier |
| 466 | * on if it fails; the framebuffer can operate (without |
| 467 | * acceleration) without this region. |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 468 | */ |
| 469 | vdev->engine_start = pci_resource_start(vdev->pdev, 1); |
| 470 | vdev->engine_len = pci_resource_len(vdev->pdev, 1); |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 471 | vdev->engine_mmio = ioremap_nocache(vdev->engine_start, |
| 472 | vdev->engine_len); |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 473 | if (vdev->engine_mmio == NULL) |
| 474 | dev_err(&vdev->pdev->dev, |
| 475 | "Unable to map engine MMIO; operation will be " |
| 476 | "slow and crippled.\n"); |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 477 | /* |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 478 | * Map in framebuffer memory. For now, failure here is |
| 479 | * fatal. Unfortunately, in the absence of significant |
| 480 | * vmalloc space, failure here is also entirely plausible. |
| 481 | * Eventually we want to move away from mapping this |
| 482 | * entire region. |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 483 | */ |
| 484 | vdev->fbmem_start = pci_resource_start(vdev->pdev, 0); |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 485 | ret = vdev->fbmem_len = viafb_get_fb_size_from_pci(vdev->chip_type); |
| 486 | if (ret < 0) |
| 487 | goto out_unmap; |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 488 | vdev->fbmem = ioremap_nocache(vdev->fbmem_start, vdev->fbmem_len); |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 489 | if (vdev->fbmem == NULL) { |
| 490 | ret = -ENOMEM; |
| 491 | goto out_unmap; |
| 492 | } |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 493 | return 0; |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 494 | out_unmap: |
| 495 | iounmap(vdev->engine_mmio); |
| 496 | return ret; |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 497 | } |
| 498 | |
Henrik Kretzschmar | eca9c47 | 2010-06-17 12:01:24 +0200 | [diff] [blame] | 499 | static void via_pci_teardown_mmio(struct viafb_dev *vdev) |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 500 | { |
| 501 | iounmap(vdev->fbmem); |
| 502 | iounmap(vdev->engine_mmio); |
| 503 | } |
| 504 | |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 505 | /* |
| 506 | * Create our subsidiary devices. |
| 507 | */ |
| 508 | static struct viafb_subdev_info { |
| 509 | char *name; |
| 510 | struct platform_device *platdev; |
| 511 | } viafb_subdevs[] = { |
| 512 | { |
| 513 | .name = "viafb-gpio", |
| 514 | }, |
| 515 | { |
| 516 | .name = "viafb-i2c", |
Jonathan Corbet | 024fafbac | 2010-10-19 21:32:11 -0300 | [diff] [blame^] | 517 | }, |
| 518 | #if defined(CONFIG_VIDEO_VIA_CAMERA) || defined(CONFIG_VIDEO_VIA_CAMERA_MODULE) |
| 519 | { |
| 520 | .name = "viafb-camera", |
| 521 | }, |
| 522 | #endif |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 523 | }; |
| 524 | #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs) |
| 525 | |
| 526 | static int __devinit via_create_subdev(struct viafb_dev *vdev, |
| 527 | struct viafb_subdev_info *info) |
| 528 | { |
| 529 | int ret; |
| 530 | |
| 531 | info->platdev = platform_device_alloc(info->name, -1); |
| 532 | if (!info->platdev) { |
| 533 | dev_err(&vdev->pdev->dev, "Unable to allocate pdev %s\n", |
| 534 | info->name); |
| 535 | return -ENOMEM; |
| 536 | } |
| 537 | info->platdev->dev.parent = &vdev->pdev->dev; |
| 538 | info->platdev->dev.platform_data = vdev; |
| 539 | ret = platform_device_add(info->platdev); |
| 540 | if (ret) { |
| 541 | dev_err(&vdev->pdev->dev, "Unable to add pdev %s\n", |
| 542 | info->name); |
| 543 | platform_device_put(info->platdev); |
| 544 | info->platdev = NULL; |
| 545 | } |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | static int __devinit via_setup_subdevs(struct viafb_dev *vdev) |
| 550 | { |
| 551 | int i; |
| 552 | |
| 553 | /* |
| 554 | * Ignore return values. Even if some of the devices |
| 555 | * fail to be created, we'll still be able to use some |
| 556 | * of the rest. |
| 557 | */ |
| 558 | for (i = 0; i < N_SUBDEVS; i++) |
| 559 | via_create_subdev(vdev, viafb_subdevs + i); |
| 560 | return 0; |
| 561 | } |
| 562 | |
Henrik Kretzschmar | eca9c47 | 2010-06-17 12:01:24 +0200 | [diff] [blame] | 563 | static void via_teardown_subdevs(void) |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 564 | { |
| 565 | int i; |
| 566 | |
| 567 | for (i = 0; i < N_SUBDEVS; i++) |
| 568 | if (viafb_subdevs[i].platdev) { |
| 569 | viafb_subdevs[i].platdev->dev.platform_data = NULL; |
| 570 | platform_device_unregister(viafb_subdevs[i].platdev); |
| 571 | } |
| 572 | } |
| 573 | |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 574 | |
| 575 | static int __devinit via_pci_probe(struct pci_dev *pdev, |
| 576 | const struct pci_device_id *ent) |
| 577 | { |
| 578 | int ret; |
| 579 | |
| 580 | ret = pci_enable_device(pdev); |
| 581 | if (ret) |
| 582 | return ret; |
| 583 | /* |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 584 | * Global device initialization. |
| 585 | */ |
| 586 | memset(&global_dev, 0, sizeof(global_dev)); |
| 587 | global_dev.pdev = pdev; |
| 588 | global_dev.chip_type = ent->driver_data; |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 589 | global_dev.port_cfg = adap_configs; |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 590 | spin_lock_init(&global_dev.reg_lock); |
| 591 | ret = via_pci_setup_mmio(&global_dev); |
| 592 | if (ret) |
| 593 | goto out_disable; |
| 594 | /* |
Jonathan Corbet | 94dd1a8 | 2010-04-23 09:46:59 -0600 | [diff] [blame] | 595 | * Set up interrupts and create our subdevices. Continue even if |
| 596 | * some things fail. |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 597 | */ |
Jonathan Corbet | 94dd1a8 | 2010-04-23 09:46:59 -0600 | [diff] [blame] | 598 | viafb_int_init(); |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 599 | via_setup_subdevs(&global_dev); |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 600 | /* |
Jonathan Corbet | 8bbf50f | 2010-04-30 10:03:57 -0600 | [diff] [blame] | 601 | * Set up the framebuffer device |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 602 | */ |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 603 | ret = via_fb_pci_probe(&global_dev); |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 604 | if (ret) |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 605 | goto out_subdevs; |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 606 | return 0; |
| 607 | |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 608 | out_subdevs: |
| 609 | via_teardown_subdevs(); |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 610 | via_pci_teardown_mmio(&global_dev); |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 611 | out_disable: |
| 612 | pci_disable_device(pdev); |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | static void __devexit via_pci_remove(struct pci_dev *pdev) |
| 617 | { |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 618 | via_teardown_subdevs(); |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 619 | via_fb_pci_remove(pdev); |
Jonathan Corbet | 24b4d82 | 2010-04-22 13:48:09 -0600 | [diff] [blame] | 620 | via_pci_teardown_mmio(&global_dev); |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 621 | pci_disable_device(pdev); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | static struct pci_device_id via_pci_table[] __devinitdata = { |
| 626 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID), |
| 627 | .driver_data = UNICHROME_CLE266 }, |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 628 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID), |
| 629 | .driver_data = UNICHROME_K400 }, |
| 630 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID), |
| 631 | .driver_data = UNICHROME_K800 }, |
Florian Tobias Schandinat | ad0676c | 2010-05-22 22:53:06 +0000 | [diff] [blame] | 632 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID), |
| 633 | .driver_data = UNICHROME_PM800 }, |
| 634 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN700_DID), |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 635 | .driver_data = UNICHROME_CN700 }, |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 636 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID), |
| 637 | .driver_data = UNICHROME_CX700 }, |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 638 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID), |
| 639 | .driver_data = UNICHROME_CN750 }, |
Florian Tobias Schandinat | ad0676c | 2010-05-22 22:53:06 +0000 | [diff] [blame] | 640 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID), |
| 641 | .driver_data = UNICHROME_K8M890 }, |
| 642 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID), |
| 643 | .driver_data = UNICHROME_P4M890 }, |
| 644 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID), |
| 645 | .driver_data = UNICHROME_P4M900 }, |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 646 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID), |
| 647 | .driver_data = UNICHROME_VX800 }, |
| 648 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID), |
| 649 | .driver_data = UNICHROME_VX855 }, |
| 650 | { } |
| 651 | }; |
| 652 | MODULE_DEVICE_TABLE(pci, via_pci_table); |
| 653 | |
| 654 | static struct pci_driver via_driver = { |
| 655 | .name = "viafb", |
| 656 | .id_table = via_pci_table, |
| 657 | .probe = via_pci_probe, |
| 658 | .remove = __devexit_p(via_pci_remove), |
| 659 | }; |
| 660 | |
| 661 | static int __init via_core_init(void) |
| 662 | { |
| 663 | int ret; |
| 664 | |
| 665 | ret = viafb_init(); |
| 666 | if (ret) |
| 667 | return ret; |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 668 | viafb_i2c_init(); |
| 669 | viafb_gpio_init(); |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 670 | return pci_register_driver(&via_driver); |
| 671 | } |
| 672 | |
| 673 | static void __exit via_core_exit(void) |
| 674 | { |
| 675 | pci_unregister_driver(&via_driver); |
Jonathan Corbet | 7582eb9 | 2010-04-22 17:39:34 -0600 | [diff] [blame] | 676 | viafb_gpio_exit(); |
| 677 | viafb_i2c_exit(); |
Jonathan Corbet | f045f77 | 2009-12-01 20:29:39 -0700 | [diff] [blame] | 678 | viafb_exit(); |
| 679 | } |
| 680 | |
| 681 | module_init(via_core_init); |
| 682 | module_exit(via_core_exit); |