]> Pileus Git - ~andy/linux/blob - drivers/of/of_spi.c
of/gpio: add default of_xlate function if device has a node pointer
[~andy/linux] / drivers / of / of_spi.c
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_irq.h>
13 #include <linux/of_spi.h>
14
15 /**
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
19  *
20  * Registers an spi_device for each child node of 'np' which has a 'reg'
21  * property.
22  */
23 void of_register_spi_devices(struct spi_master *master, struct device_node *np)
24 {
25         struct spi_device *spi;
26         struct device_node *nc;
27         const __be32 *prop;
28         int rc;
29         int len;
30
31         for_each_child_of_node(np, nc) {
32                 /* Alloc an spi_device */
33                 spi = spi_alloc_device(master);
34                 if (!spi) {
35                         dev_err(&master->dev, "spi_device alloc error for %s\n",
36                                 nc->full_name);
37                         spi_dev_put(spi);
38                         continue;
39                 }
40
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",
45                                 nc->full_name);
46                         spi_dev_put(spi);
47                         continue;
48                 }
49
50                 /* Device address */
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",
54                                 nc->full_name);
55                         spi_dev_put(spi);
56                         continue;
57                 }
58                 spi->chip_select = be32_to_cpup(prop);
59
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;
67
68                 /* Device speed */
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",
72                                 nc->full_name);
73                         spi_dev_put(spi);
74                         continue;
75                 }
76                 spi->max_speed_hz = be32_to_cpup(prop);
77
78                 /* IRQ */
79                 spi->irq = irq_of_parse_and_map(nc, 0);
80
81                 /* Store a pointer to the node in the device structure */
82                 of_node_get(nc);
83                 spi->dev.of_node = nc;
84
85                 /* Register the new device */
86                 request_module(spi->modalias);
87                 rc = spi_add_device(spi);
88                 if (rc) {
89                         dev_err(&master->dev, "spi_device register error %s\n",
90                                 nc->full_name);
91                         spi_dev_put(spi);
92                 }
93
94         }
95 }
96 EXPORT_SYMBOL(of_register_spi_devices);