xilinx_spi_of.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /* Supports:
  20. * Xilinx SPI devices as OF devices
  21. *
  22. * Inspired by xilinx_spi.c, 2002-2007 (c) MontaVista Software, Inc.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_spi.h>
  32. #include <linux/spi/xilinx_spi.h>
  33. #include "xilinx_spi.h"
  34. static int __devinit xilinx_spi_of_probe(struct of_device *ofdev,
  35. const struct of_device_id *match)
  36. {
  37. struct spi_master *master;
  38. struct xspi_platform_data *pdata;
  39. struct resource r_mem;
  40. struct resource r_irq;
  41. int rc = 0;
  42. const u32 *prop;
  43. int len;
  44. rc = of_address_to_resource(ofdev->dev.of_node, 0, &r_mem);
  45. if (rc) {
  46. dev_warn(&ofdev->dev, "invalid address\n");
  47. return rc;
  48. }
  49. rc = of_irq_to_resource(ofdev->dev.of_node, 0, &r_irq);
  50. if (rc == NO_IRQ) {
  51. dev_warn(&ofdev->dev, "no IRQ found\n");
  52. return -ENODEV;
  53. }
  54. ofdev->dev.platform_data =
  55. kzalloc(sizeof(struct xspi_platform_data), GFP_KERNEL);
  56. pdata = ofdev->dev.platform_data;
  57. if (!pdata)
  58. return -ENOMEM;
  59. /* number of slave select bits is required */
  60. prop = of_get_property(ofdev->dev.of_node, "xlnx,num-ss-bits", &len);
  61. if (!prop || len < sizeof(*prop)) {
  62. dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n");
  63. return -EINVAL;
  64. }
  65. pdata->num_chipselect = *prop;
  66. pdata->bits_per_word = 8;
  67. master = xilinx_spi_init(&ofdev->dev, &r_mem, r_irq.start, -1);
  68. if (!master)
  69. return -ENODEV;
  70. dev_set_drvdata(&ofdev->dev, master);
  71. /* Add any subnodes on the SPI bus */
  72. of_register_spi_devices(master, ofdev->dev.of_node);
  73. return 0;
  74. }
  75. static int __devexit xilinx_spi_remove(struct of_device *ofdev)
  76. {
  77. xilinx_spi_deinit(dev_get_drvdata(&ofdev->dev));
  78. dev_set_drvdata(&ofdev->dev, 0);
  79. kfree(ofdev->dev.platform_data);
  80. ofdev->dev.platform_data = NULL;
  81. return 0;
  82. }
  83. static int __exit xilinx_spi_of_remove(struct of_device *op)
  84. {
  85. return xilinx_spi_remove(op);
  86. }
  87. static const struct of_device_id xilinx_spi_of_match[] = {
  88. { .compatible = "xlnx,xps-spi-2.00.a", },
  89. { .compatible = "xlnx,xps-spi-2.00.b", },
  90. {}
  91. };
  92. MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
  93. static struct of_platform_driver xilinx_spi_of_driver = {
  94. .probe = xilinx_spi_of_probe,
  95. .remove = __exit_p(xilinx_spi_of_remove),
  96. .driver = {
  97. .name = "xilinx-xps-spi",
  98. .owner = THIS_MODULE,
  99. .of_match_table = xilinx_spi_of_match,
  100. },
  101. };
  102. static int __init xilinx_spi_of_init(void)
  103. {
  104. return of_register_platform_driver(&xilinx_spi_of_driver);
  105. }
  106. module_init(xilinx_spi_of_init);
  107. static void __exit xilinx_spi_of_exit(void)
  108. {
  109. of_unregister_platform_driver(&xilinx_spi_of_driver);
  110. }
  111. module_exit(xilinx_spi_of_exit);
  112. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  113. MODULE_DESCRIPTION("Xilinx SPI platform driver");
  114. MODULE_LICENSE("GPL v2");