spi-pxa2xx-pci.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * CE4100's SPI device is more or less the same one as found on PXA
  3. *
  4. */
  5. #include <linux/pci.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/of_device.h>
  8. #include <linux/module.h>
  9. #include <linux/spi/pxa2xx_spi.h>
  10. struct ce4100_info {
  11. struct ssp_device ssp;
  12. struct platform_device *spi_pdev;
  13. };
  14. static DEFINE_MUTEX(ssp_lock);
  15. static LIST_HEAD(ssp_list);
  16. struct ssp_device *pxa_ssp_request(int port, const char *label)
  17. {
  18. struct ssp_device *ssp = NULL;
  19. mutex_lock(&ssp_lock);
  20. list_for_each_entry(ssp, &ssp_list, node) {
  21. if (ssp->port_id == port && ssp->use_count == 0) {
  22. ssp->use_count++;
  23. ssp->label = label;
  24. break;
  25. }
  26. }
  27. mutex_unlock(&ssp_lock);
  28. if (&ssp->node == &ssp_list)
  29. return NULL;
  30. return ssp;
  31. }
  32. EXPORT_SYMBOL_GPL(pxa_ssp_request);
  33. void pxa_ssp_free(struct ssp_device *ssp)
  34. {
  35. mutex_lock(&ssp_lock);
  36. if (ssp->use_count) {
  37. ssp->use_count--;
  38. ssp->label = NULL;
  39. } else
  40. dev_err(&ssp->pdev->dev, "device already free\n");
  41. mutex_unlock(&ssp_lock);
  42. }
  43. EXPORT_SYMBOL_GPL(pxa_ssp_free);
  44. static int __devinit ce4100_spi_probe(struct pci_dev *dev,
  45. const struct pci_device_id *ent)
  46. {
  47. int ret;
  48. resource_size_t phys_beg;
  49. resource_size_t phys_len;
  50. struct ce4100_info *spi_info;
  51. struct platform_device *pdev;
  52. struct pxa2xx_spi_master spi_pdata;
  53. struct ssp_device *ssp;
  54. ret = pci_enable_device(dev);
  55. if (ret)
  56. return ret;
  57. phys_beg = pci_resource_start(dev, 0);
  58. phys_len = pci_resource_len(dev, 0);
  59. if (!request_mem_region(phys_beg, phys_len,
  60. "CE4100 SPI")) {
  61. dev_err(&dev->dev, "Can't request register space.\n");
  62. ret = -EBUSY;
  63. return ret;
  64. }
  65. pdev = platform_device_alloc("pxa2xx-spi", dev->devfn);
  66. spi_info = kzalloc(sizeof(*spi_info), GFP_KERNEL);
  67. if (!pdev || !spi_info ) {
  68. ret = -ENOMEM;
  69. goto err_nomem;
  70. }
  71. memset(&spi_pdata, 0, sizeof(spi_pdata));
  72. spi_pdata.num_chipselect = dev->devfn;
  73. ret = platform_device_add_data(pdev, &spi_pdata, sizeof(spi_pdata));
  74. if (ret)
  75. goto err_nomem;
  76. pdev->dev.parent = &dev->dev;
  77. pdev->dev.of_node = dev->dev.of_node;
  78. ssp = &spi_info->ssp;
  79. ssp->phys_base = pci_resource_start(dev, 0);
  80. ssp->mmio_base = ioremap(phys_beg, phys_len);
  81. if (!ssp->mmio_base) {
  82. dev_err(&pdev->dev, "failed to ioremap() registers\n");
  83. ret = -EIO;
  84. goto err_nomem;
  85. }
  86. ssp->irq = dev->irq;
  87. ssp->port_id = pdev->id;
  88. ssp->type = PXA25x_SSP;
  89. mutex_lock(&ssp_lock);
  90. list_add(&ssp->node, &ssp_list);
  91. mutex_unlock(&ssp_lock);
  92. pci_set_drvdata(dev, spi_info);
  93. ret = platform_device_add(pdev);
  94. if (ret)
  95. goto err_dev_add;
  96. return ret;
  97. err_dev_add:
  98. pci_set_drvdata(dev, NULL);
  99. mutex_lock(&ssp_lock);
  100. list_del(&ssp->node);
  101. mutex_unlock(&ssp_lock);
  102. iounmap(ssp->mmio_base);
  103. err_nomem:
  104. release_mem_region(phys_beg, phys_len);
  105. platform_device_put(pdev);
  106. kfree(spi_info);
  107. return ret;
  108. }
  109. static void __devexit ce4100_spi_remove(struct pci_dev *dev)
  110. {
  111. struct ce4100_info *spi_info;
  112. struct ssp_device *ssp;
  113. spi_info = pci_get_drvdata(dev);
  114. ssp = &spi_info->ssp;
  115. platform_device_unregister(spi_info->spi_pdev);
  116. iounmap(ssp->mmio_base);
  117. release_mem_region(pci_resource_start(dev, 0),
  118. pci_resource_len(dev, 0));
  119. mutex_lock(&ssp_lock);
  120. list_del(&ssp->node);
  121. mutex_unlock(&ssp_lock);
  122. pci_set_drvdata(dev, NULL);
  123. pci_disable_device(dev);
  124. kfree(spi_info);
  125. }
  126. static DEFINE_PCI_DEVICE_TABLE(ce4100_spi_devices) = {
  127. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2e6a) },
  128. { },
  129. };
  130. MODULE_DEVICE_TABLE(pci, ce4100_spi_devices);
  131. static struct pci_driver ce4100_spi_driver = {
  132. .name = "ce4100_spi",
  133. .id_table = ce4100_spi_devices,
  134. .probe = ce4100_spi_probe,
  135. .remove = __devexit_p(ce4100_spi_remove),
  136. };
  137. static int __init ce4100_spi_init(void)
  138. {
  139. return pci_register_driver(&ce4100_spi_driver);
  140. }
  141. module_init(ce4100_spi_init);
  142. static void __exit ce4100_spi_exit(void)
  143. {
  144. pci_unregister_driver(&ce4100_spi_driver);
  145. }
  146. module_exit(ce4100_spi_exit);
  147. MODULE_DESCRIPTION("CE4100 PCI-SPI glue code for PXA's driver");
  148. MODULE_LICENSE("GPL v2");
  149. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");