of_spi.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SPI OF support routines
  3. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  4. *
  5. * Support routines for deriving SPI device attachments from the device
  6. * tree.
  7. */
  8. #include <linux/of.h>
  9. #include <linux/device.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_spi.h>
  13. /**
  14. * of_register_spi_devices - Register child devices onto the SPI bus
  15. * @master: Pointer to spi_master device
  16. *
  17. * Registers an spi_device for each child node of master node which has a 'reg'
  18. * property.
  19. */
  20. void of_register_spi_devices(struct spi_master *master)
  21. {
  22. struct spi_device *spi;
  23. struct device_node *nc;
  24. const __be32 *prop;
  25. int rc;
  26. int len;
  27. if (!master->dev.of_node)
  28. return;
  29. for_each_child_of_node(master->dev.of_node, nc) {
  30. /* Alloc an spi_device */
  31. spi = spi_alloc_device(master);
  32. if (!spi) {
  33. dev_err(&master->dev, "spi_device alloc error for %s\n",
  34. nc->full_name);
  35. spi_dev_put(spi);
  36. continue;
  37. }
  38. /* Select device driver */
  39. if (of_modalias_node(nc, spi->modalias,
  40. sizeof(spi->modalias)) < 0) {
  41. dev_err(&master->dev, "cannot find modalias for %s\n",
  42. nc->full_name);
  43. spi_dev_put(spi);
  44. continue;
  45. }
  46. /* Device address */
  47. prop = of_get_property(nc, "reg", &len);
  48. if (!prop || len < sizeof(*prop)) {
  49. dev_err(&master->dev, "%s has no 'reg' property\n",
  50. nc->full_name);
  51. spi_dev_put(spi);
  52. continue;
  53. }
  54. spi->chip_select = be32_to_cpup(prop);
  55. /* Mode (clock phase/polarity/etc.) */
  56. if (of_find_property(nc, "spi-cpha", NULL))
  57. spi->mode |= SPI_CPHA;
  58. if (of_find_property(nc, "spi-cpol", NULL))
  59. spi->mode |= SPI_CPOL;
  60. if (of_find_property(nc, "spi-cs-high", NULL))
  61. spi->mode |= SPI_CS_HIGH;
  62. /* Device speed */
  63. prop = of_get_property(nc, "spi-max-frequency", &len);
  64. if (!prop || len < sizeof(*prop)) {
  65. dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
  66. nc->full_name);
  67. spi_dev_put(spi);
  68. continue;
  69. }
  70. spi->max_speed_hz = be32_to_cpup(prop);
  71. /* IRQ */
  72. spi->irq = irq_of_parse_and_map(nc, 0);
  73. /* Store a pointer to the node in the device structure */
  74. of_node_get(nc);
  75. spi->dev.of_node = nc;
  76. /* Register the new device */
  77. request_module(spi->modalias);
  78. rc = spi_add_device(spi);
  79. if (rc) {
  80. dev_err(&master->dev, "spi_device register error %s\n",
  81. nc->full_name);
  82. spi_dev_put(spi);
  83. }
  84. }
  85. }
  86. EXPORT_SYMBOL(of_register_spi_devices);