blob: 378e504f89ebdf8dd7b2b503e6c3a79d8820f269 [file] [log] [blame]
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +01001/*
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/spi/pxa2xx_spi.h>
9
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053010struct ce4100_info {
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010011 struct ssp_device ssp;
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053012 struct platform_device *spi_pdev;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010013};
14
15static DEFINE_MUTEX(ssp_lock);
16static LIST_HEAD(ssp_list);
17
18struct ssp_device *pxa_ssp_request(int port, const char *label)
19{
20 struct ssp_device *ssp = NULL;
21
22 mutex_lock(&ssp_lock);
23
24 list_for_each_entry(ssp, &ssp_list, node) {
25 if (ssp->port_id == port && ssp->use_count == 0) {
26 ssp->use_count++;
27 ssp->label = label;
28 break;
29 }
30 }
31
32 mutex_unlock(&ssp_lock);
33
34 if (&ssp->node == &ssp_list)
35 return NULL;
36
37 return ssp;
38}
39EXPORT_SYMBOL_GPL(pxa_ssp_request);
40
41void pxa_ssp_free(struct ssp_device *ssp)
42{
43 mutex_lock(&ssp_lock);
44 if (ssp->use_count) {
45 ssp->use_count--;
46 ssp->label = NULL;
47 } else
48 dev_err(&ssp->pdev->dev, "device already free\n");
49 mutex_unlock(&ssp_lock);
50}
51EXPORT_SYMBOL_GPL(pxa_ssp_free);
52
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010053static int __devinit ce4100_spi_probe(struct pci_dev *dev,
54 const struct pci_device_id *ent)
55{
56 int ret;
57 resource_size_t phys_beg;
58 resource_size_t phys_len;
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053059 struct ce4100_info *spi_info;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010060 struct platform_device *pdev;
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053061 struct pxa2xx_spi_master spi_pdata;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010062 struct ssp_device *ssp;
63
64 ret = pci_enable_device(dev);
65 if (ret)
66 return ret;
67
68 phys_beg = pci_resource_start(dev, 0);
69 phys_len = pci_resource_len(dev, 0);
70
71 if (!request_mem_region(phys_beg, phys_len,
72 "CE4100 SPI")) {
73 dev_err(&dev->dev, "Can't request register space.\n");
74 ret = -EBUSY;
75 return ret;
76 }
77
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053078 pdev = platform_device_alloc("pxa2xx-spi", dev->devfn);
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010079 spi_info = kzalloc(sizeof(*spi_info), GFP_KERNEL);
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053080 if (!pdev || !spi_info ) {
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010081 ret = -ENOMEM;
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053082 goto err_nomem;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010083 }
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053084 memset(&spi_pdata, 0, sizeof(spi_pdata));
85 spi_pdata.num_chipselect = dev->devfn;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010086
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053087 ret = platform_device_add_data(pdev, &spi_pdata, sizeof(spi_pdata));
88 if (ret)
89 goto err_nomem;
90
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010091 pdev->dev.parent = &dev->dev;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010092 pdev->dev.of_node = dev->dev.of_node;
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053093 ssp = &spi_info->ssp;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +010094 ssp->phys_base = pci_resource_start(dev, 0);
95 ssp->mmio_base = ioremap(phys_beg, phys_len);
96 if (!ssp->mmio_base) {
97 dev_err(&pdev->dev, "failed to ioremap() registers\n");
98 ret = -EIO;
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +053099 goto err_nomem;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100100 }
101 ssp->irq = dev->irq;
102 ssp->port_id = pdev->id;
103 ssp->type = PXA25x_SSP;
104
105 mutex_lock(&ssp_lock);
106 list_add(&ssp->node, &ssp_list);
107 mutex_unlock(&ssp_lock);
108
109 pci_set_drvdata(dev, spi_info);
110
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +0530111 ret = platform_device_add(pdev);
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100112 if (ret)
113 goto err_dev_add;
114
115 return ret;
116
117err_dev_add:
118 pci_set_drvdata(dev, NULL);
119 mutex_lock(&ssp_lock);
120 list_del(&ssp->node);
121 mutex_unlock(&ssp_lock);
122 iounmap(ssp->mmio_base);
123
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +0530124err_nomem:
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100125 release_mem_region(phys_beg, phys_len);
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +0530126 platform_device_put(pdev);
127 kfree(spi_info);
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100128 return ret;
129}
130
131static void __devexit ce4100_spi_remove(struct pci_dev *dev)
132{
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +0530133 struct ce4100_info *spi_info;
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100134 struct ssp_device *ssp;
135
136 spi_info = pci_get_drvdata(dev);
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100137 ssp = &spi_info->ssp;
Sebastian Andrzej Siewior0f3e1d22011-02-03 00:31:21 +0530138 platform_device_unregister(spi_info->spi_pdev);
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100139
140 iounmap(ssp->mmio_base);
141 release_mem_region(pci_resource_start(dev, 0),
142 pci_resource_len(dev, 0));
143
144 mutex_lock(&ssp_lock);
145 list_del(&ssp->node);
146 mutex_unlock(&ssp_lock);
147
148 pci_set_drvdata(dev, NULL);
149 pci_disable_device(dev);
150 kfree(spi_info);
151}
152
153static struct pci_device_id ce4100_spi_devices[] __devinitdata = {
Sebastian Andrzej Siewiord6ea3df2010-11-24 10:17:14 +0100154 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2e6a) },
155 { },
156};
157MODULE_DEVICE_TABLE(pci, ce4100_spi_devices);
158
159static struct pci_driver ce4100_spi_driver = {
160 .name = "ce4100_spi",
161 .id_table = ce4100_spi_devices,
162 .probe = ce4100_spi_probe,
163 .remove = __devexit_p(ce4100_spi_remove),
164};
165
166static int __init ce4100_spi_init(void)
167{
168 return pci_register_driver(&ce4100_spi_driver);
169}
170module_init(ce4100_spi_init);
171
172static void __exit ce4100_spi_exit(void)
173{
174 pci_unregister_driver(&ce4100_spi_driver);
175}
176module_exit(ce4100_spi_exit);
177
178MODULE_DESCRIPTION("CE4100 PCI-SPI glue code for PXA's driver");
179MODULE_LICENSE("GPL v2");
180MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");