blob: 55c58012a028392e0c2b6cfbcdb06e1f3ccb86d1 [file] [log] [blame]
Richard Röjforsd5af91a2009-11-13 12:28:39 +01001/*
2 * Xilinx SPI OF device driver
3 *
4 * Copyright (c) 2009 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20/* Supports:
21 * Xilinx SPI devices as OF devices
22 *
23 * Inspired by xilinx_spi.c, 2002-2007 (c) MontaVista Software, Inc.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/interrupt.h>
29#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Richard Röjforsd5af91a2009-11-13 12:28:39 +010031
32#include <linux/of_platform.h>
33#include <linux/of_device.h>
34#include <linux/of_spi.h>
35
36#include <linux/spi/xilinx_spi.h>
37#include "xilinx_spi.h"
38
39
40static int __devinit xilinx_spi_of_probe(struct of_device *ofdev,
41 const struct of_device_id *match)
42{
43 struct spi_master *master;
44 struct xspi_platform_data *pdata;
45 struct resource r_mem;
46 struct resource r_irq;
47 int rc = 0;
48 const u32 *prop;
49 int len;
50
51 rc = of_address_to_resource(ofdev->node, 0, &r_mem);
52 if (rc) {
53 dev_warn(&ofdev->dev, "invalid address\n");
54 return rc;
55 }
56
57 rc = of_irq_to_resource(ofdev->node, 0, &r_irq);
58 if (rc == NO_IRQ) {
59 dev_warn(&ofdev->dev, "no IRQ found\n");
60 return -ENODEV;
61 }
62
63 ofdev->dev.platform_data =
64 kzalloc(sizeof(struct xspi_platform_data), GFP_KERNEL);
65 pdata = ofdev->dev.platform_data;
66 if (!pdata)
67 return -ENOMEM;
68
69 /* number of slave select bits is required */
70 prop = of_get_property(ofdev->node, "xlnx,num-ss-bits", &len);
71 if (!prop || len < sizeof(*prop)) {
72 dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n");
73 return -EINVAL;
74 }
75 pdata->num_chipselect = *prop;
Richard Röjforsc9da2e12009-11-13 12:28:55 +010076 pdata->bits_per_word = 8;
Richard Röjforsd5af91a2009-11-13 12:28:39 +010077 master = xilinx_spi_init(&ofdev->dev, &r_mem, r_irq.start, -1);
78 if (!master)
79 return -ENODEV;
80
81 dev_set_drvdata(&ofdev->dev, master);
82
83 /* Add any subnodes on the SPI bus */
84 of_register_spi_devices(master, ofdev->node);
85
86 return 0;
87}
88
89static int __devexit xilinx_spi_remove(struct of_device *ofdev)
90{
91 xilinx_spi_deinit(dev_get_drvdata(&ofdev->dev));
92 dev_set_drvdata(&ofdev->dev, 0);
93 kfree(ofdev->dev.platform_data);
94 ofdev->dev.platform_data = NULL;
95 return 0;
96}
97
98static int __exit xilinx_spi_of_remove(struct of_device *op)
99{
100 return xilinx_spi_remove(op);
101}
102
Márton Németh631e61b2010-01-20 13:49:44 -0700103static const struct of_device_id xilinx_spi_of_match[] = {
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100104 { .compatible = "xlnx,xps-spi-2.00.a", },
105 { .compatible = "xlnx,xps-spi-2.00.b", },
106 {}
107};
108
109MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
110
111static struct of_platform_driver xilinx_spi_of_driver = {
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100112 .probe = xilinx_spi_of_probe,
113 .remove = __exit_p(xilinx_spi_of_remove),
114 .driver = {
115 .name = "xilinx-xps-spi",
116 .owner = THIS_MODULE,
Grant Likely40182942010-04-13 16:13:02 -0700117 .of_match_table = xilinx_spi_of_match,
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100118 },
119};
120
121static int __init xilinx_spi_of_init(void)
122{
123 return of_register_platform_driver(&xilinx_spi_of_driver);
124}
125module_init(xilinx_spi_of_init);
126
127static void __exit xilinx_spi_of_exit(void)
128{
129 of_unregister_platform_driver(&xilinx_spi_of_driver);
130}
131module_exit(xilinx_spi_of_exit);
132
133MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
134MODULE_DESCRIPTION("Xilinx SPI platform driver");
135MODULE_LICENSE("GPL v2");