timberdale.c 21.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/*
 * timberdale.c timberdale FPGA MFD driver
 * Copyright (c) 2009 Intel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* Supports:
 * Timberdale FPGA
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/msi.h>
#include <linux/mfd/core.h>
#include <linux/slab.h>

#include <linux/timb_gpio.h>

#include <linux/i2c.h>
#include <linux/i2c-ocores.h>
#include <linux/i2c-xiic.h>
#include <linux/i2c/tsc2007.h>

#include <linux/spi/spi.h>
#include <linux/spi/xilinx_spi.h>
#include <linux/spi/max7301.h>
#include <linux/spi/mc33880.h>

#include <media/timb_radio.h>
#include <media/timb_video.h>

#include <linux/timb_dma.h>

#include <linux/ks8842.h>

#include "timberdale.h"

#define DRIVER_NAME "timberdale"

struct timberdale_device {
	resource_size_t		ctl_mapbase;
	unsigned char __iomem   *ctl_membase;
	struct {
		u32 major;
		u32 minor;
		u32 config;
	} fw;
};

/*--------------------------------------------------------------------------*/

static struct tsc2007_platform_data timberdale_tsc2007_platform_data = {
	.model = 2003,
	.x_plate_ohms = 100
};

static struct i2c_board_info timberdale_i2c_board_info[] = {
	{
		I2C_BOARD_INFO("tsc2007", 0x48),
		.platform_data = &timberdale_tsc2007_platform_data,
		.irq = IRQ_TIMBERDALE_TSC_INT
	},
};

static struct xiic_i2c_platform_data
timberdale_xiic_platform_data = {
	.devices = timberdale_i2c_board_info,
	.num_devices = ARRAY_SIZE(timberdale_i2c_board_info)
};

static struct ocores_i2c_platform_data
timberdale_ocores_platform_data = {
	.reg_shift = 2,
	.clock_khz = 62500,
	.devices = timberdale_i2c_board_info,
	.num_devices = ARRAY_SIZE(timberdale_i2c_board_info)
};

static const struct resource timberdale_xiic_resources[] = {
	{
		.start	= XIICOFFSET,
		.end	= XIICEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_I2C,
		.end	= IRQ_TIMBERDALE_I2C,
		.flags	= IORESOURCE_IRQ,
	},
};

static const struct resource timberdale_ocores_resources[] = {
	{
		.start	= OCORESOFFSET,
		.end	= OCORESEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start 	= IRQ_TIMBERDALE_I2C,
		.end	= IRQ_TIMBERDALE_I2C,
		.flags	= IORESOURCE_IRQ,
	},
};

static const struct max7301_platform_data timberdale_max7301_platform_data = {
	.base = 200
};

static const struct mc33880_platform_data timberdale_mc33880_platform_data = {
	.base = 100
};

static struct spi_board_info timberdale_spi_16bit_board_info[] = {
	{
		.modalias = "max7301",
		.max_speed_hz = 26000,
		.chip_select = 2,
		.mode = SPI_MODE_0,
		.platform_data = &timberdale_max7301_platform_data
	},
};

static struct spi_board_info timberdale_spi_8bit_board_info[] = {
	{
		.modalias = "mc33880",
		.max_speed_hz = 4000,
		.chip_select = 1,
		.mode = SPI_MODE_1,
		.platform_data = &timberdale_mc33880_platform_data
	},
};

static struct xspi_platform_data timberdale_xspi_platform_data = {
	.num_chipselect = 3,
	/* bits per word and devices will be filled in runtime depending
	 * on the HW config
	 */
};

static const struct resource timberdale_spi_resources[] = {
	{
		.start 	= SPIOFFSET,
		.end	= SPIEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_SPI,
		.end	= IRQ_TIMBERDALE_SPI,
		.flags	= IORESOURCE_IRQ,
	},
};

static struct ks8842_platform_data
	timberdale_ks8842_platform_data = {
	.rx_dma_channel = DMA_ETH_RX,
	.tx_dma_channel = DMA_ETH_TX
};

static const struct resource timberdale_eth_resources[] = {
	{
		.start	= ETHOFFSET,
		.end	= ETHEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_ETHSW_IF,
		.end	= IRQ_TIMBERDALE_ETHSW_IF,
		.flags	= IORESOURCE_IRQ,
	},
};

static struct timbgpio_platform_data
	timberdale_gpio_platform_data = {
	.gpio_base = 0,
	.nr_pins = GPIO_NR_PINS,
	.irq_base = 200,
};

static const struct resource timberdale_gpio_resources[] = {
	{
		.start	= GPIOOFFSET,
		.end	= GPIOEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_GPIO,
		.end	= IRQ_TIMBERDALE_GPIO,
		.flags	= IORESOURCE_IRQ,
	},
};

static const struct resource timberdale_mlogicore_resources[] = {
	{
		.start	= MLCOREOFFSET,
		.end	= MLCOREEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_MLCORE,
		.end	= IRQ_TIMBERDALE_MLCORE,
		.flags	= IORESOURCE_IRQ,
	},
	{
		.start	= IRQ_TIMBERDALE_MLCORE_BUF,
		.end	= IRQ_TIMBERDALE_MLCORE_BUF,
		.flags	= IORESOURCE_IRQ,
	},
};

static const struct resource timberdale_uart_resources[] = {
	{
		.start	= UARTOFFSET,
		.end	= UARTEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_UART,
		.end	= IRQ_TIMBERDALE_UART,
		.flags	= IORESOURCE_IRQ,
	},
};

static const struct resource timberdale_uartlite_resources[] = {
	{
		.start	= UARTLITEOFFSET,
		.end	= UARTLITEEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_UARTLITE,
		.end	= IRQ_TIMBERDALE_UARTLITE,
		.flags	= IORESOURCE_IRQ,
	},
};

static struct i2c_board_info timberdale_adv7180_i2c_board_info = {
	/* Requires jumper JP9 to be off */
	I2C_BOARD_INFO("adv7180", 0x42 >> 1),
	.irq = IRQ_TIMBERDALE_ADV7180
};

static struct timb_video_platform_data
	timberdale_video_platform_data = {
	.dma_channel = DMA_VIDEO_RX,
	.i2c_adapter = 0,
	.encoder = {
		.info = &timberdale_adv7180_i2c_board_info
	}
};

static const struct resource
timberdale_radio_resources[] = {
	{
		.start	= RDSOFFSET,
		.end	= RDSEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_RDS,
		.end	= IRQ_TIMBERDALE_RDS,
		.flags	= IORESOURCE_IRQ,
	},
};

static struct i2c_board_info timberdale_tef6868_i2c_board_info = {
	I2C_BOARD_INFO("tef6862", 0x60)
};

static struct i2c_board_info timberdale_saa7706_i2c_board_info = {
	I2C_BOARD_INFO("saa7706h", 0x1C)
};

static struct timb_radio_platform_data
	timberdale_radio_platform_data = {
	.i2c_adapter = 0,
	.tuner = &timberdale_tef6868_i2c_board_info,
	.dsp = &timberdale_saa7706_i2c_board_info
};

static const struct resource timberdale_video_resources[] = {
	{
		.start	= LOGIWOFFSET,
		.end	= LOGIWEND,
		.flags	= IORESOURCE_MEM,
	},
	/*
	note that the "frame buffer" is located in DMA area
	starting at 0x1200000
	*/
};

static struct timb_dma_platform_data timb_dma_platform_data = {
	.nr_channels = 10,
	.channels = {
		{
			/* UART RX */
			.rx = true,
			.descriptors = 2,
			.descriptor_elements = 1
		},
		{
			/* UART TX */
			.rx = false,
			.descriptors = 2,
			.descriptor_elements = 1
		},
		{
			/* MLB RX */
			.rx = true,
			.descriptors = 2,
			.descriptor_elements = 1
		},
		{
			/* MLB TX */
			.rx = false,
			.descriptors = 2,
			.descriptor_elements = 1
		},
		{
			/* Video RX */
			.rx = true,
			.bytes_per_line = 1440,
			.descriptors = 2,
			.descriptor_elements = 16
		},
		{
			/* Video framedrop */
		},
		{
			/* SDHCI RX */
			.rx = true,
		},
		{
			/* SDHCI TX */
		},
		{
			/* ETH RX */
			.rx = true,
			.descriptors = 2,
			.descriptor_elements = 1
		},
		{
			/* ETH TX */
			.rx = false,
			.descriptors = 2,
			.descriptor_elements = 1
		},
	}
};

static const struct resource timberdale_dma_resources[] = {
	{
		.start	= DMAOFFSET,
		.end	= DMAEND,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_DMA,
		.end	= IRQ_TIMBERDALE_DMA,
		.flags	= IORESOURCE_IRQ,
	},
};

static const struct mfd_cell timberdale_cells_bar0_cfg0[] = {
	{
		.name = "timb-dma",
		.num_resources = ARRAY_SIZE(timberdale_dma_resources),
		.resources = timberdale_dma_resources,
		.platform_data = &timb_dma_platform_data,
		.pdata_size = sizeof(timb_dma_platform_data),
	},
	{
		.name = "timb-uart",
		.num_resources = ARRAY_SIZE(timberdale_uart_resources),
		.resources = timberdale_uart_resources,
	},
	{
		.name = "xiic-i2c",
		.num_resources = ARRAY_SIZE(timberdale_xiic_resources),
		.resources = timberdale_xiic_resources,
		.platform_data = &timberdale_xiic_platform_data,
		.pdata_size = sizeof(timberdale_xiic_platform_data),
	},
	{
		.name = "timb-gpio",
		.num_resources = ARRAY_SIZE(timberdale_gpio_resources),
		.resources = timberdale_gpio_resources,
		.platform_data = &timberdale_gpio_platform_data,
		.pdata_size = sizeof(timberdale_gpio_platform_data),
	},
	{
		.name = "timb-video",
		.num_resources = ARRAY_SIZE(timberdale_video_resources),
		.resources = timberdale_video_resources,
		.platform_data = &timberdale_video_platform_data,
		.pdata_size = sizeof(timberdale_video_platform_data),
	},
	{
		.name = "timb-radio",
		.num_resources = ARRAY_SIZE(timberdale_radio_resources),
		.resources = timberdale_radio_resources,
		.platform_data = &timberdale_radio_platform_data,
		.pdata_size = sizeof(timberdale_radio_platform_data),
	},
	{
		.name = "xilinx_spi",
		.num_resources = ARRAY_SIZE(timberdale_spi_resources),
		.resources = timberdale_spi_resources,
		.platform_data = &timberdale_xspi_platform_data,
		.pdata_size = sizeof(timberdale_xspi_platform_data),
	},
	{
		.name = "ks8842",
		.num_resources = ARRAY_SIZE(timberdale_eth_resources),
		.resources = timberdale_eth_resources,
		.platform_data = &timberdale_ks8842_platform_data,
		.pdata_size = sizeof(timberdale_ks8842_platform_data),
	},
};

static const struct mfd_cell timberdale_cells_bar0_cfg1[] = {
	{
		.name = "timb-dma",
		.num_resources = ARRAY_SIZE(timberdale_dma_resources),
		.resources = timberdale_dma_resources,
		.platform_data = &timb_dma_platform_data,
		.pdata_size = sizeof(timb_dma_platform_data),
	},
	{
		.name = "timb-uart",
		.num_resources = ARRAY_SIZE(timberdale_uart_resources),
		.resources = timberdale_uart_resources,
	},
	{
		.name = "uartlite",
		.num_resources = ARRAY_SIZE(timberdale_uartlite_resources),
		.resources = timberdale_uartlite_resources,
	},
	{
		.name = "xiic-i2c",
		.num_resources = ARRAY_SIZE(timberdale_xiic_resources),
		.resources = timberdale_xiic_resources,
		.platform_data = &timberdale_xiic_platform_data,
		.pdata_size = sizeof(timberdale_xiic_platform_data),
	},
	{
		.name = "timb-gpio",
		.num_resources = ARRAY_SIZE(timberdale_gpio_resources),
		.resources = timberdale_gpio_resources,
		.platform_data = &timberdale_gpio_platform_data,
		.pdata_size = sizeof(timberdale_gpio_platform_data),
	},
	{
		.name = "timb-mlogicore",
		.num_resources = ARRAY_SIZE(timberdale_mlogicore_resources),
		.resources = timberdale_mlogicore_resources,
	},
	{
		.name = "timb-video",
		.num_resources = ARRAY_SIZE(timberdale_video_resources),
		.resources = timberdale_video_resources,
		.platform_data = &timberdale_video_platform_data,
		.pdata_size = sizeof(timberdale_video_platform_data),
	},
	{
		.name = "timb-radio",
		.num_resources = ARRAY_SIZE(timberdale_radio_resources),
		.resources = timberdale_radio_resources,
		.platform_data = &timberdale_radio_platform_data,
		.pdata_size = sizeof(timberdale_radio_platform_data),
	},
	{
		.name = "xilinx_spi",
		.num_resources = ARRAY_SIZE(timberdale_spi_resources),
		.resources = timberdale_spi_resources,
		.platform_data = &timberdale_xspi_platform_data,
		.pdata_size = sizeof(timberdale_xspi_platform_data),
	},
	{
		.name = "ks8842",
		.num_resources = ARRAY_SIZE(timberdale_eth_resources),
		.resources = timberdale_eth_resources,
		.platform_data = &timberdale_ks8842_platform_data,
		.pdata_size = sizeof(timberdale_ks8842_platform_data),
	},
};

static const struct mfd_cell timberdale_cells_bar0_cfg2[] = {
	{
		.name = "timb-dma",
		.num_resources = ARRAY_SIZE(timberdale_dma_resources),
		.resources = timberdale_dma_resources,
		.platform_data = &timb_dma_platform_data,
		.pdata_size = sizeof(timb_dma_platform_data),
	},
	{
		.name = "timb-uart",
		.num_resources = ARRAY_SIZE(timberdale_uart_resources),
		.resources = timberdale_uart_resources,
	},
	{
		.name = "xiic-i2c",
		.num_resources = ARRAY_SIZE(timberdale_xiic_resources),
		.resources = timberdale_xiic_resources,
		.platform_data = &timberdale_xiic_platform_data,
		.pdata_size = sizeof(timberdale_xiic_platform_data),
	},
	{
		.name = "timb-gpio",
		.num_resources = ARRAY_SIZE(timberdale_gpio_resources),
		.resources = timberdale_gpio_resources,
		.platform_data = &timberdale_gpio_platform_data,
		.pdata_size = sizeof(timberdale_gpio_platform_data),
	},
	{
		.name = "timb-video",
		.num_resources = ARRAY_SIZE(timberdale_video_resources),
		.resources = timberdale_video_resources,
		.platform_data = &timberdale_video_platform_data,
		.pdata_size = sizeof(timberdale_video_platform_data),
	},
	{
		.name = "timb-radio",
		.num_resources = ARRAY_SIZE(timberdale_radio_resources),
		.resources = timberdale_radio_resources,
		.platform_data = &timberdale_radio_platform_data,
		.pdata_size = sizeof(timberdale_radio_platform_data),
	},
	{
		.name = "xilinx_spi",
		.num_resources = ARRAY_SIZE(timberdale_spi_resources),
		.resources = timberdale_spi_resources,
		.platform_data = &timberdale_xspi_platform_data,
		.pdata_size = sizeof(timberdale_xspi_platform_data),
	},
};

static const struct mfd_cell timberdale_cells_bar0_cfg3[] = {
	{
		.name = "timb-dma",
		.num_resources = ARRAY_SIZE(timberdale_dma_resources),
		.resources = timberdale_dma_resources,
		.platform_data = &timb_dma_platform_data,
		.pdata_size = sizeof(timb_dma_platform_data),
	},
	{
		.name = "timb-uart",
		.num_resources = ARRAY_SIZE(timberdale_uart_resources),
		.resources = timberdale_uart_resources,
	},
	{
		.name = "ocores-i2c",
		.num_resources = ARRAY_SIZE(timberdale_ocores_resources),
		.resources = timberdale_ocores_resources,
		.platform_data = &timberdale_ocores_platform_data,
		.pdata_size = sizeof(timberdale_ocores_platform_data),
	},
	{
		.name = "timb-gpio",
		.num_resources = ARRAY_SIZE(timberdale_gpio_resources),
		.resources = timberdale_gpio_resources,
		.platform_data = &timberdale_gpio_platform_data,
		.pdata_size = sizeof(timberdale_gpio_platform_data),
	},
	{
		.name = "timb-video",
		.num_resources = ARRAY_SIZE(timberdale_video_resources),
		.resources = timberdale_video_resources,
		.platform_data = &timberdale_video_platform_data,
		.pdata_size = sizeof(timberdale_video_platform_data),
	},
	{
		.name = "timb-radio",
		.num_resources = ARRAY_SIZE(timberdale_radio_resources),
		.resources = timberdale_radio_resources,
		.platform_data = &timberdale_radio_platform_data,
		.pdata_size = sizeof(timberdale_radio_platform_data),
	},
	{
		.name = "xilinx_spi",
		.num_resources = ARRAY_SIZE(timberdale_spi_resources),
		.resources = timberdale_spi_resources,
		.platform_data = &timberdale_xspi_platform_data,
		.pdata_size = sizeof(timberdale_xspi_platform_data),
	},
	{
		.name = "ks8842",
		.num_resources = ARRAY_SIZE(timberdale_eth_resources),
		.resources = timberdale_eth_resources,
		.platform_data = &timberdale_ks8842_platform_data,
		.pdata_size = sizeof(timberdale_ks8842_platform_data),
	},
};

static const struct resource timberdale_sdhc_resources[] = {
	/* located in bar 1 and bar 2 */
	{
		.start	= SDHC0OFFSET,
		.end	= SDHC0END,
		.flags	= IORESOURCE_MEM,
	},
	{
		.start	= IRQ_TIMBERDALE_SDHC,
		.end	= IRQ_TIMBERDALE_SDHC,
		.flags	= IORESOURCE_IRQ,
	},
};

static const struct mfd_cell timberdale_cells_bar1[] = {
	{
		.name = "sdhci",
		.num_resources = ARRAY_SIZE(timberdale_sdhc_resources),
		.resources = timberdale_sdhc_resources,
	},
};

static const struct mfd_cell timberdale_cells_bar2[] = {
	{
		.name = "sdhci",
		.num_resources = ARRAY_SIZE(timberdale_sdhc_resources),
		.resources = timberdale_sdhc_resources,
	},
};

static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
	char *buf)
{
	struct pci_dev *pdev = to_pci_dev(dev);
	struct timberdale_device *priv = pci_get_drvdata(pdev);

	return sprintf(buf, "%d.%d.%d\n", priv->fw.major, priv->fw.minor,
		priv->fw.config);
}

static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);

/*--------------------------------------------------------------------------*/

static int timb_probe(struct pci_dev *dev,
	const struct pci_device_id *id)
{
	struct timberdale_device *priv;
	int err, i;
	resource_size_t mapbase;
	struct msix_entry *msix_entries = NULL;
	u8 ip_setup;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	pci_set_drvdata(dev, priv);

	err = pci_enable_device(dev);
	if (err)
		goto err_enable;

	mapbase = pci_resource_start(dev, 0);
	if (!mapbase) {
		dev_err(&dev->dev, "No resource\n");
		goto err_start;
	}

	/* create a resource for the PCI master register */
	priv->ctl_mapbase = mapbase + CHIPCTLOFFSET;
	if (!request_mem_region(priv->ctl_mapbase, CHIPCTLSIZE, "timb-ctl")) {
		dev_err(&dev->dev, "Failed to request ctl mem\n");
		goto err_start;
	}

	priv->ctl_membase = ioremap(priv->ctl_mapbase, CHIPCTLSIZE);
	if (!priv->ctl_membase) {
		dev_err(&dev->dev, "ioremap failed for ctl mem\n");
		goto err_ioremap;
	}

	/* read the HW config */
	priv->fw.major = ioread32(priv->ctl_membase + TIMB_REV_MAJOR);
	priv->fw.minor = ioread32(priv->ctl_membase + TIMB_REV_MINOR);
	priv->fw.config = ioread32(priv->ctl_membase + TIMB_HW_CONFIG);

	if (priv->fw.major > TIMB_SUPPORTED_MAJOR) {
		dev_err(&dev->dev, "The driver supports an older "
			"version of the FPGA, please update the driver to "
			"support %d.%d\n", priv->fw.major, priv->fw.minor);
		goto err_config;
	}
	if (priv->fw.major < TIMB_SUPPORTED_MAJOR ||
		priv->fw.minor < TIMB_REQUIRED_MINOR) {
		dev_err(&dev->dev, "The FPGA image is too old (%d.%d), "
			"please upgrade the FPGA to at least: %d.%d\n",
			priv->fw.major, priv->fw.minor,
			TIMB_SUPPORTED_MAJOR, TIMB_REQUIRED_MINOR);
		goto err_config;
	}

	msix_entries = kzalloc(TIMBERDALE_NR_IRQS * sizeof(*msix_entries),
		GFP_KERNEL);
	if (!msix_entries)
		goto err_config;

	for (i = 0; i < TIMBERDALE_NR_IRQS; i++)
		msix_entries[i].entry = i;

	err = pci_enable_msix(dev, msix_entries, TIMBERDALE_NR_IRQS);
	if (err) {
		dev_err(&dev->dev,
			"MSI-X init failed: %d, expected entries: %d\n",
			err, TIMBERDALE_NR_IRQS);
		goto err_msix;
	}

	err = device_create_file(&dev->dev, &dev_attr_fw_ver);
	if (err)
		goto err_create_file;

	/* Reset all FPGA PLB peripherals */
	iowrite32(0x1, priv->ctl_membase + TIMB_SW_RST);

	/* update IRQ offsets in I2C board info */
	for (i = 0; i < ARRAY_SIZE(timberdale_i2c_board_info); i++)
		timberdale_i2c_board_info[i].irq =
			msix_entries[timberdale_i2c_board_info[i].irq].vector;

	/* Update the SPI configuration depending on the HW (8 or 16 bit) */
	if (priv->fw.config & TIMB_HW_CONFIG_SPI_8BIT) {
		timberdale_xspi_platform_data.bits_per_word = 8;
		timberdale_xspi_platform_data.devices =
			timberdale_spi_8bit_board_info;
		timberdale_xspi_platform_data.num_devices =
			ARRAY_SIZE(timberdale_spi_8bit_board_info);
	} else {
		timberdale_xspi_platform_data.bits_per_word = 16;
		timberdale_xspi_platform_data.devices =
			timberdale_spi_16bit_board_info;
		timberdale_xspi_platform_data.num_devices =
			ARRAY_SIZE(timberdale_spi_16bit_board_info);
	}

	ip_setup = priv->fw.config & TIMB_HW_VER_MASK;
	switch (ip_setup) {
	case TIMB_HW_VER0:
		err = mfd_add_devices(&dev->dev, -1,
			timberdale_cells_bar0_cfg0,
			ARRAY_SIZE(timberdale_cells_bar0_cfg0),
			&dev->resource[0], msix_entries[0].vector, NULL);
		break;
	case TIMB_HW_VER1:
		err = mfd_add_devices(&dev->dev, -1,
			timberdale_cells_bar0_cfg1,
			ARRAY_SIZE(timberdale_cells_bar0_cfg1),
			&dev->resource[0], msix_entries[0].vector, NULL);
		break;
	case TIMB_HW_VER2:
		err = mfd_add_devices(&dev->dev, -1,
			timberdale_cells_bar0_cfg2,
			ARRAY_SIZE(timberdale_cells_bar0_cfg2),
			&dev->resource[0], msix_entries[0].vector, NULL);
		break;
	case TIMB_HW_VER3:
		err = mfd_add_devices(&dev->dev, -1,
			timberdale_cells_bar0_cfg3,
			ARRAY_SIZE(timberdale_cells_bar0_cfg3),
			&dev->resource[0], msix_entries[0].vector, NULL);
		break;
	default:
		dev_err(&dev->dev, "Uknown IP setup: %d.%d.%d\n",
			priv->fw.major, priv->fw.minor, ip_setup);
		err = -ENODEV;
		goto err_mfd;
	}

	if (err) {
		dev_err(&dev->dev, "mfd_add_devices failed: %d\n", err);
		goto err_mfd;
	}

	err = mfd_add_devices(&dev->dev, 0,
		timberdale_cells_bar1, ARRAY_SIZE(timberdale_cells_bar1),
		&dev->resource[1], msix_entries[0].vector, NULL);
	if (err) {
		dev_err(&dev->dev, "mfd_add_devices failed: %d\n", err);
		goto err_mfd2;
	}

	/* only version 0 and 3 have the iNand routed to SDHCI */
	if (((priv->fw.config & TIMB_HW_VER_MASK) == TIMB_HW_VER0) ||
		((priv->fw.config & TIMB_HW_VER_MASK) == TIMB_HW_VER3)) {
		err = mfd_add_devices(&dev->dev, 1, timberdale_cells_bar2,
			ARRAY_SIZE(timberdale_cells_bar2),
			&dev->resource[2], msix_entries[0].vector, NULL);
		if (err) {
			dev_err(&dev->dev, "mfd_add_devices failed: %d\n", err);
			goto err_mfd2;
		}
	}

	kfree(msix_entries);

	dev_info(&dev->dev,
		"Found Timberdale Card. Rev: %d.%d, HW config: 0x%02x\n",
		priv->fw.major, priv->fw.minor, priv->fw.config);

	return 0;

err_mfd2:
	mfd_remove_devices(&dev->dev);
err_mfd:
	device_remove_file(&dev->dev, &dev_attr_fw_ver);
err_create_file:
	pci_disable_msix(dev);
err_msix:
	kfree(msix_entries);
err_config:
	iounmap(priv->ctl_membase);
err_ioremap:
	release_mem_region(priv->ctl_mapbase, CHIPCTLSIZE);
err_start:
	pci_disable_device(dev);
err_enable:
	kfree(priv);
	return -ENODEV;
}

static void timb_remove(struct pci_dev *dev)
{
	struct timberdale_device *priv = pci_get_drvdata(dev);

	mfd_remove_devices(&dev->dev);

	device_remove_file(&dev->dev, &dev_attr_fw_ver);

	iounmap(priv->ctl_membase);
	release_mem_region(priv->ctl_mapbase, CHIPCTLSIZE);

	pci_disable_msix(dev);
	pci_disable_device(dev);
	kfree(priv);
}

static const struct pci_device_id timberdale_pci_tbl[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_TIMB, PCI_DEVICE_ID_TIMB) },
	{ 0 }
};
MODULE_DEVICE_TABLE(pci, timberdale_pci_tbl);

static struct pci_driver timberdale_pci_driver = {
	.name = DRIVER_NAME,
	.id_table = timberdale_pci_tbl,
	.probe = timb_probe,
	.remove = timb_remove,
};

module_pci_driver(timberdale_pci_driver);

MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
MODULE_VERSION(DRV_VERSION);
MODULE_LICENSE("GPL v2");