diff options
author | Grant Likely <grant.likely@secretlab.ca> | 2008-05-16 13:37:09 -0400 |
---|---|---|
committer | Grant Likely <grant.likely@secretlab.ca> | 2008-07-25 22:34:40 -0400 |
commit | 284b01897340974000bcc84de87a4e1becc8a83d (patch) | |
tree | 9d2f38c9d2d39601d615f738c337c5af14a6b344 | |
parent | dc87c98e8f635a718f1abb2c3e15fc77c0001651 (diff) |
spi: Add OF binding support for SPI busses
This patch adds support for populating an SPI bus based on data in the
OF device tree. This is useful for powerpc platforms which use the
device tree instead of discrete code for describing platform layout.
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
-rw-r--r-- | drivers/of/Kconfig | 6 | ||||
-rw-r--r-- | drivers/of/Makefile | 1 | ||||
-rw-r--r-- | drivers/of/of_spi.c | 93 | ||||
-rw-r--r-- | include/linux/of_spi.h | 18 |
4 files changed, 118 insertions, 0 deletions
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index 1d7ec3129349..f821dbc952a4 100644 --- a/drivers/of/Kconfig +++ b/drivers/of/Kconfig | |||
@@ -13,3 +13,9 @@ config OF_I2C | |||
13 | depends on PPC_OF && I2C | 13 | depends on PPC_OF && I2C |
14 | help | 14 | help |
15 | OpenFirmware I2C accessors | 15 | OpenFirmware I2C accessors |
16 | |||
17 | config OF_SPI | ||
18 | def_tristate SPI | ||
19 | depends on OF && PPC_OF && SPI | ||
20 | help | ||
21 | OpenFirmware SPI accessors | ||
diff --git a/drivers/of/Makefile b/drivers/of/Makefile index 548772e871fd..4c3c6f8e36f5 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile | |||
@@ -2,3 +2,4 @@ obj-y = base.o | |||
2 | obj-$(CONFIG_OF_DEVICE) += device.o platform.o | 2 | obj-$(CONFIG_OF_DEVICE) += device.o platform.o |
3 | obj-$(CONFIG_OF_GPIO) += gpio.o | 3 | obj-$(CONFIG_OF_GPIO) += gpio.o |
4 | obj-$(CONFIG_OF_I2C) += of_i2c.o | 4 | obj-$(CONFIG_OF_I2C) += of_i2c.o |
5 | obj-$(CONFIG_OF_SPI) += of_spi.o | ||
diff --git a/drivers/of/of_spi.c b/drivers/of/of_spi.c new file mode 100644 index 000000000000..b01eec026f68 --- /dev/null +++ b/drivers/of/of_spi.c | |||
@@ -0,0 +1,93 @@ | |||
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 | |||
9 | #include <linux/of.h> | ||
10 | #include <linux/device.h> | ||
11 | #include <linux/spi/spi.h> | ||
12 | #include <linux/of_spi.h> | ||
13 | |||
14 | /** | ||
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 | ||
18 | * | ||
19 | * Registers an spi_device for each child node of 'np' which has a 'reg' | ||
20 | * property. | ||
21 | */ | ||
22 | void of_register_spi_devices(struct spi_master *master, struct device_node *np) | ||
23 | { | ||
24 | struct spi_device *spi; | ||
25 | struct device_node *nc; | ||
26 | const u32 *prop; | ||
27 | int rc; | ||
28 | int len; | ||
29 | |||
30 | for_each_child_of_node(np, 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 | |||
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", | ||
44 | nc->full_name); | ||
45 | spi_dev_put(spi); | ||
46 | continue; | ||
47 | } | ||
48 | |||
49 | /* Device address */ | ||
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", | ||
53 | nc->full_name); | ||
54 | spi_dev_put(spi); | ||
55 | continue; | ||
56 | } | ||
57 | spi->chip_select = *prop; | ||
58 | |||
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 | |||
65 | /* Device speed */ | ||
66 | prop = of_get_property(nc, "spi-max-frequency", &len); | ||
67 | if (!prop || len < sizeof(*prop)) { | ||
68 | dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n", | ||
69 | nc->full_name); | ||
70 | spi_dev_put(spi); | ||
71 | continue; | ||
72 | } | ||
73 | spi->max_speed_hz = *prop; | ||
74 | |||
75 | /* IRQ */ | ||
76 | spi->irq = irq_of_parse_and_map(nc, 0); | ||
77 | |||
78 | /* Store a pointer to the node in the device structure */ | ||
79 | of_node_get(nc); | ||
80 | spi->dev.archdata.of_node = nc; | ||
81 | |||
82 | /* Register the new device */ | ||
83 | request_module(spi->modalias); | ||
84 | rc = spi_add_device(spi); | ||
85 | if (rc) { | ||
86 | dev_err(&master->dev, "spi_device register error %s\n", | ||
87 | nc->full_name); | ||
88 | spi_dev_put(spi); | ||
89 | } | ||
90 | |||
91 | } | ||
92 | } | ||
93 | EXPORT_SYMBOL(of_register_spi_devices); | ||
diff --git a/include/linux/of_spi.h b/include/linux/of_spi.h new file mode 100644 index 000000000000..5f71ee8c0868 --- /dev/null +++ b/include/linux/of_spi.h | |||
@@ -0,0 +1,18 @@ | |||
1 | /* | ||
2 | * OpenFirmware SPI 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 | |||
9 | #ifndef __LINUX_OF_SPI_H | ||
10 | #define __LINUX_OF_SPI_H | ||
11 | |||
12 | #include <linux/of.h> | ||
13 | #include <linux/spi/spi.h> | ||
14 | |||
15 | extern void of_register_spi_devices(struct spi_master *master, | ||
16 | struct device_node *np); | ||
17 | |||
18 | #endif /* __LINUX_OF_SPI */ | ||