| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 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> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 31 |  | 
| Grant Likely | 22ae782 | 2010-07-29 11:49:01 -0600 | [diff] [blame] | 32 | #include <linux/of_address.h> | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 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 of_device *ofdev, | 
|  | 42 | const struct of_device_id *match) | 
|  | 43 | { | 
|  | 44 | struct spi_master *master; | 
|  | 45 | struct xspi_platform_data *pdata; | 
|  | 46 | struct resource r_mem; | 
|  | 47 | struct resource r_irq; | 
|  | 48 | int rc = 0; | 
|  | 49 | const u32 *prop; | 
|  | 50 | int len; | 
|  | 51 |  | 
| Grant Likely | bf6a67e | 2010-05-25 00:48:24 -0600 | [diff] [blame] | 52 | rc = of_address_to_resource(ofdev->dev.of_node, 0, &r_mem); | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 53 | if (rc) { | 
|  | 54 | dev_warn(&ofdev->dev, "invalid address\n"); | 
|  | 55 | return rc; | 
|  | 56 | } | 
|  | 57 |  | 
| Grant Likely | bf6a67e | 2010-05-25 00:48:24 -0600 | [diff] [blame] | 58 | rc = of_irq_to_resource(ofdev->dev.of_node, 0, &r_irq); | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 59 | if (rc == NO_IRQ) { | 
|  | 60 | dev_warn(&ofdev->dev, "no IRQ found\n"); | 
|  | 61 | return -ENODEV; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | ofdev->dev.platform_data = | 
|  | 65 | kzalloc(sizeof(struct xspi_platform_data), GFP_KERNEL); | 
|  | 66 | pdata = ofdev->dev.platform_data; | 
|  | 67 | if (!pdata) | 
|  | 68 | return -ENOMEM; | 
|  | 69 |  | 
|  | 70 | /* number of slave select bits is required */ | 
| Grant Likely | bf6a67e | 2010-05-25 00:48:24 -0600 | [diff] [blame] | 71 | prop = of_get_property(ofdev->dev.of_node, "xlnx,num-ss-bits", &len); | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 72 | if (!prop || len < sizeof(*prop)) { | 
|  | 73 | dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n"); | 
|  | 74 | return -EINVAL; | 
|  | 75 | } | 
|  | 76 | pdata->num_chipselect = *prop; | 
| Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 77 | pdata->bits_per_word = 8; | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 78 | master = xilinx_spi_init(&ofdev->dev, &r_mem, r_irq.start, -1); | 
|  | 79 | if (!master) | 
|  | 80 | return -ENODEV; | 
|  | 81 |  | 
|  | 82 | dev_set_drvdata(&ofdev->dev, master); | 
|  | 83 |  | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 84 | return 0; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | static int __devexit xilinx_spi_remove(struct of_device *ofdev) | 
|  | 88 | { | 
|  | 89 | xilinx_spi_deinit(dev_get_drvdata(&ofdev->dev)); | 
|  | 90 | dev_set_drvdata(&ofdev->dev, 0); | 
|  | 91 | kfree(ofdev->dev.platform_data); | 
|  | 92 | ofdev->dev.platform_data = NULL; | 
|  | 93 | return 0; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | static int __exit xilinx_spi_of_remove(struct of_device *op) | 
|  | 97 | { | 
|  | 98 | return xilinx_spi_remove(op); | 
|  | 99 | } | 
|  | 100 |  | 
| Márton Németh | 631e61b | 2010-01-20 13:49:44 -0700 | [diff] [blame] | 101 | static const struct of_device_id xilinx_spi_of_match[] = { | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 102 | { .compatible = "xlnx,xps-spi-2.00.a", }, | 
|  | 103 | { .compatible = "xlnx,xps-spi-2.00.b", }, | 
|  | 104 | {} | 
|  | 105 | }; | 
|  | 106 |  | 
|  | 107 | MODULE_DEVICE_TABLE(of, xilinx_spi_of_match); | 
|  | 108 |  | 
|  | 109 | static struct of_platform_driver xilinx_spi_of_driver = { | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 110 | .probe = xilinx_spi_of_probe, | 
|  | 111 | .remove = __exit_p(xilinx_spi_of_remove), | 
|  | 112 | .driver = { | 
|  | 113 | .name = "xilinx-xps-spi", | 
|  | 114 | .owner = THIS_MODULE, | 
| Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 115 | .of_match_table = xilinx_spi_of_match, | 
| Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 116 | }, | 
|  | 117 | }; | 
|  | 118 |  | 
|  | 119 | static int __init xilinx_spi_of_init(void) | 
|  | 120 | { | 
|  | 121 | return of_register_platform_driver(&xilinx_spi_of_driver); | 
|  | 122 | } | 
|  | 123 | module_init(xilinx_spi_of_init); | 
|  | 124 |  | 
|  | 125 | static void __exit xilinx_spi_of_exit(void) | 
|  | 126 | { | 
|  | 127 | of_unregister_platform_driver(&xilinx_spi_of_driver); | 
|  | 128 | } | 
|  | 129 | module_exit(xilinx_spi_of_exit); | 
|  | 130 |  | 
|  | 131 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); | 
|  | 132 | MODULE_DESCRIPTION("Xilinx SPI platform driver"); | 
|  | 133 | MODULE_LICENSE("GPL v2"); |