2 * SPI OF support routines
3 * Copyright (C) 2008 Secret Lab Technologies Ltd.
5 * Support routines for deriving SPI device attachments from the device
9 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/spi/spi.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_spi.h>
17 * of_register_spi_devices - Register child devices onto the SPI bus
18 * @master: Pointer to spi_master device
20 * Registers an spi_device for each child node of master node which has a 'reg'
23 void of_register_spi_devices(struct spi_master
*master
)
25 struct spi_device
*spi
;
26 struct device_node
*nc
;
31 if (!master
->dev
.of_node
)
34 for_each_child_of_node(master
->dev
.of_node
, nc
) {
35 /* Alloc an spi_device */
36 spi
= spi_alloc_device(master
);
38 dev_err(&master
->dev
, "spi_device alloc error for %s\n",
44 /* Select device driver */
45 if (of_modalias_node(nc
, spi
->modalias
,
46 sizeof(spi
->modalias
)) < 0) {
47 dev_err(&master
->dev
, "cannot find modalias for %s\n",
54 prop
= of_get_property(nc
, "reg", &len
);
55 if (!prop
|| len
< sizeof(*prop
)) {
56 dev_err(&master
->dev
, "%s has no 'reg' property\n",
61 spi
->chip_select
= be32_to_cpup(prop
);
63 /* Mode (clock phase/polarity/etc.) */
64 if (of_find_property(nc
, "spi-cpha", NULL
))
65 spi
->mode
|= SPI_CPHA
;
66 if (of_find_property(nc
, "spi-cpol", NULL
))
67 spi
->mode
|= SPI_CPOL
;
68 if (of_find_property(nc
, "spi-cs-high", NULL
))
69 spi
->mode
|= SPI_CS_HIGH
;
72 prop
= of_get_property(nc
, "spi-max-frequency", &len
);
73 if (!prop
|| len
< sizeof(*prop
)) {
74 dev_err(&master
->dev
, "%s has no 'spi-max-frequency' property\n",
79 spi
->max_speed_hz
= be32_to_cpup(prop
);
82 spi
->irq
= irq_of_parse_and_map(nc
, 0);
84 /* Store a pointer to the node in the device structure */
86 spi
->dev
.of_node
= nc
;
88 /* Register the new device */
89 request_module(spi
->modalias
);
90 rc
= spi_add_device(spi
);
92 dev_err(&master
->dev
, "spi_device register error %s\n",
99 EXPORT_SYMBOL(of_register_spi_devices
);