2 * SPI OF support routines
3 * Copyright (C) 2008 Secret Lab Technologies Ltd.
5 * Support routines for deriving SPI device attachments from the device
10 #include <linux/device.h>
11 #include <linux/spi/spi.h>
12 #include <linux/of_irq.h>
13 #include <linux/of_spi.h>
16 * of_register_spi_devices - Register child devices onto the SPI bus
17 * @master: Pointer to spi_master device
18 * @np: parent node of SPI device nodes
20 * Registers an spi_device for each child node of 'np' which has a 'reg'
23 void of_register_spi_devices(struct spi_master *master, struct device_node *np)
25 struct spi_device *spi;
26 struct device_node *nc;
31 for_each_child_of_node(np, nc) {
32 /* Alloc an spi_device */
33 spi = spi_alloc_device(master);
35 dev_err(&master->dev, "spi_device alloc error for %s\n",
41 /* Select device driver */
42 if (of_modalias_node(nc, spi->modalias,
43 sizeof(spi->modalias)) < 0) {
44 dev_err(&master->dev, "cannot find modalias for %s\n",
51 prop = of_get_property(nc, "reg", &len);
52 if (!prop || len < sizeof(*prop)) {
53 dev_err(&master->dev, "%s has no 'reg' property\n",
58 spi->chip_select = be32_to_cpup(prop);
60 /* Mode (clock phase/polarity/etc.) */
61 if (of_find_property(nc, "spi-cpha", NULL))
62 spi->mode |= SPI_CPHA;
63 if (of_find_property(nc, "spi-cpol", NULL))
64 spi->mode |= SPI_CPOL;
65 if (of_find_property(nc, "spi-cs-high", NULL))
66 spi->mode |= SPI_CS_HIGH;
69 prop = of_get_property(nc, "spi-max-frequency", &len);
70 if (!prop || len < sizeof(*prop)) {
71 dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
76 spi->max_speed_hz = be32_to_cpup(prop);
79 spi->irq = irq_of_parse_and_map(nc, 0);
81 /* Store a pointer to the node in the device structure */
83 spi->dev.of_node = nc;
85 /* Register the new device */
86 request_module(spi->modalias);
87 rc = spi_add_device(spi);
89 dev_err(&master->dev, "spi_device register error %s\n",
96 EXPORT_SYMBOL(of_register_spi_devices);