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_spi.h>
15 * of_register_spi_devices - Register child devices onto the SPI bus
16 * @master: Pointer to spi_master device
17 * @np: parent node of SPI device nodes
19 * Registers an spi_device for each child node of 'np' which has a 'reg'
22 void of_register_spi_devices(struct spi_master *master, struct device_node *np)
24 struct spi_device *spi;
25 struct device_node *nc;
30 for_each_child_of_node(np, nc) {
31 /* Alloc an spi_device */
32 spi = spi_alloc_device(master);
34 dev_err(&master->dev, "spi_device alloc error for %s\n",
40 /* Select device driver */
41 if (of_modalias_node(nc, spi->modalias,
42 sizeof(spi->modalias)) < 0) {
43 dev_err(&master->dev, "cannot find modalias for %s\n",
50 prop = of_get_property(nc, "reg", &len);
51 if (!prop || len < sizeof(*prop)) {
52 dev_err(&master->dev, "%s has no 'reg' property\n",
57 spi->chip_select = *prop;
59 /* Mode (clock phase/polarity/etc.) */
60 if (of_find_property(nc, "spi-cpha", NULL))
61 spi->mode |= SPI_CPHA;
62 if (of_find_property(nc, "spi-cpol", NULL))
63 spi->mode |= SPI_CPOL;
64 if (of_find_property(nc, "spi-cs-high", NULL))
65 spi->mode |= SPI_CS_HIGH;
68 prop = of_get_property(nc, "spi-max-frequency", &len);
69 if (!prop || len < sizeof(*prop)) {
70 dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
75 spi->max_speed_hz = *prop;
78 spi->irq = irq_of_parse_and_map(nc, 0);
80 /* Store a pointer to the node in the device structure */
82 spi->dev.archdata.of_node = nc;
84 /* Register the new device */
85 request_module(spi->modalias);
86 rc = spi_add_device(spi);
88 dev_err(&master->dev, "spi_device register error %s\n",
95 EXPORT_SYMBOL(of_register_spi_devices);