of_spi.c 2.3 KB

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