spi/xilinx: Eliminate pdata references from common code.
[platform/kernel/linux-starfive.git] / drivers / spi / xilinx_spi_of.c
1 /*
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>
30 #include <linux/slab.h>
31
32 #include <linux/of_address.h>
33 #include <linux/of_platform.h>
34 #include <linux/of_device.h>
35 #include <linux/of_spi.h>
36
37 #include <linux/spi/xilinx_spi.h>
38 #include "xilinx_spi.h"
39
40
41 static int __devinit xilinx_spi_of_probe(struct platform_device *ofdev,
42         const struct of_device_id *match)
43 {
44         struct spi_master *master;
45         struct resource r_mem;
46         struct resource r_irq;
47         int rc = 0;
48         const u32 *prop;
49         int len, num_cs;
50
51         rc = of_address_to_resource(ofdev->dev.of_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->dev.of_node, 0, &r_irq);
58         if (rc == NO_IRQ) {
59                 dev_warn(&ofdev->dev, "no IRQ found\n");
60                 return -ENODEV;
61         }
62
63         /* number of slave select bits is required */
64         prop = of_get_property(ofdev->dev.of_node, "xlnx,num-ss-bits", &len);
65         if (!prop || len < sizeof(*prop)) {
66                 dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n");
67                 return -EINVAL;
68         }
69         num_cs = __be32_to_cpup(prop);
70         master = xilinx_spi_init(&ofdev->dev, &r_mem, r_irq.start, -1,
71                                  num_cs, 0, 8);
72         if (!master)
73                 return -ENODEV;
74
75         dev_set_drvdata(&ofdev->dev, master);
76
77         return 0;
78 }
79
80 static int __devexit xilinx_spi_remove(struct platform_device *ofdev)
81 {
82         xilinx_spi_deinit(dev_get_drvdata(&ofdev->dev));
83         dev_set_drvdata(&ofdev->dev, 0);
84         return 0;
85 }
86
87 static int __exit xilinx_spi_of_remove(struct platform_device *op)
88 {
89         return xilinx_spi_remove(op);
90 }
91
92 static const struct of_device_id xilinx_spi_of_match[] = {
93         { .compatible = "xlnx,xps-spi-2.00.a", },
94         { .compatible = "xlnx,xps-spi-2.00.b", },
95         {}
96 };
97
98 MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
99
100 static struct of_platform_driver xilinx_spi_of_driver = {
101         .probe = xilinx_spi_of_probe,
102         .remove = __exit_p(xilinx_spi_of_remove),
103         .driver = {
104                 .name = "xilinx-xps-spi",
105                 .owner = THIS_MODULE,
106                 .of_match_table = xilinx_spi_of_match,
107         },
108 };
109
110 static int __init xilinx_spi_of_init(void)
111 {
112         return of_register_platform_driver(&xilinx_spi_of_driver);
113 }
114 module_init(xilinx_spi_of_init);
115
116 static void __exit xilinx_spi_of_exit(void)
117 {
118         of_unregister_platform_driver(&xilinx_spi_of_driver);
119 }
120 module_exit(xilinx_spi_of_exit);
121
122 MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
123 MODULE_DESCRIPTION("Xilinx SPI platform driver");
124 MODULE_LICENSE("GPL v2");