Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 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> |
Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 12 | #include <linux/of_irq.h> |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 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 |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 18 | * |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 19 | * Registers an spi_device for each child node of master node which has a 'reg' |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 20 | * property. |
| 21 | */ |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 22 | void of_register_spi_devices(struct spi_master *master) |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 23 | { |
| 24 | struct spi_device *spi; |
| 25 | struct device_node *nc; |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 26 | const __be32 *prop; |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 27 | int rc; |
| 28 | int len; |
| 29 | |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 30 | if (!master->dev.of_node) |
| 31 | return; |
| 32 | |
| 33 | for_each_child_of_node(master->dev.of_node, nc) { |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 34 | /* Alloc an spi_device */ |
| 35 | spi = spi_alloc_device(master); |
| 36 | if (!spi) { |
| 37 | dev_err(&master->dev, "spi_device alloc error for %s\n", |
| 38 | nc->full_name); |
| 39 | spi_dev_put(spi); |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | /* Select device driver */ |
| 44 | if (of_modalias_node(nc, spi->modalias, |
| 45 | sizeof(spi->modalias)) < 0) { |
| 46 | dev_err(&master->dev, "cannot find modalias for %s\n", |
| 47 | nc->full_name); |
| 48 | spi_dev_put(spi); |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | /* Device address */ |
| 53 | prop = of_get_property(nc, "reg", &len); |
| 54 | if (!prop || len < sizeof(*prop)) { |
| 55 | dev_err(&master->dev, "%s has no 'reg' property\n", |
| 56 | nc->full_name); |
| 57 | spi_dev_put(spi); |
| 58 | continue; |
| 59 | } |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 60 | spi->chip_select = be32_to_cpup(prop); |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 61 | |
| 62 | /* Mode (clock phase/polarity/etc.) */ |
| 63 | if (of_find_property(nc, "spi-cpha", NULL)) |
| 64 | spi->mode |= SPI_CPHA; |
| 65 | if (of_find_property(nc, "spi-cpol", NULL)) |
| 66 | spi->mode |= SPI_CPOL; |
Wolfgang Ocker | f618ebf | 2008-10-15 15:00:47 +0200 | [diff] [blame] | 67 | if (of_find_property(nc, "spi-cs-high", NULL)) |
| 68 | spi->mode |= SPI_CS_HIGH; |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 69 | |
| 70 | /* Device speed */ |
| 71 | prop = of_get_property(nc, "spi-max-frequency", &len); |
| 72 | if (!prop || len < sizeof(*prop)) { |
| 73 | dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n", |
| 74 | nc->full_name); |
| 75 | spi_dev_put(spi); |
| 76 | continue; |
| 77 | } |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 78 | spi->max_speed_hz = be32_to_cpup(prop); |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 79 | |
| 80 | /* IRQ */ |
| 81 | spi->irq = irq_of_parse_and_map(nc, 0); |
| 82 | |
| 83 | /* Store a pointer to the node in the device structure */ |
| 84 | of_node_get(nc); |
Grant Likely | d706c1b | 2010-04-13 16:12:28 -0700 | [diff] [blame] | 85 | spi->dev.of_node = nc; |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 86 | |
| 87 | /* Register the new device */ |
| 88 | request_module(spi->modalias); |
| 89 | rc = spi_add_device(spi); |
| 90 | if (rc) { |
| 91 | dev_err(&master->dev, "spi_device register error %s\n", |
| 92 | nc->full_name); |
| 93 | spi_dev_put(spi); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | } |
| 98 | EXPORT_SYMBOL(of_register_spi_devices); |