of_spi.c 2.4 KB

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