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 | |
Paul Gortmaker | 48a9e41 | 2011-07-17 15:58:06 -0400 | [diff] [blame] | 9 | #include <linux/module.h> |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 10 | #include <linux/of.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/spi/spi.h> |
Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 13 | #include <linux/of_irq.h> |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 14 | #include <linux/of_spi.h> |
| 15 | |
| 16 | /** |
| 17 | * of_register_spi_devices - Register child devices onto the SPI bus |
| 18 | * @master: Pointer to spi_master device |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 19 | * |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 20 | * 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] | 21 | * property. |
| 22 | */ |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 23 | void of_register_spi_devices(struct spi_master *master) |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 24 | { |
| 25 | struct spi_device *spi; |
| 26 | struct device_node *nc; |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 27 | const __be32 *prop; |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 28 | int rc; |
| 29 | int len; |
| 30 | |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 31 | if (!master->dev.of_node) |
| 32 | return; |
| 33 | |
| 34 | for_each_child_of_node(master->dev.of_node, nc) { |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 35 | /* Alloc an spi_device */ |
| 36 | spi = spi_alloc_device(master); |
| 37 | if (!spi) { |
| 38 | dev_err(&master->dev, "spi_device alloc error for %s\n", |
| 39 | nc->full_name); |
| 40 | spi_dev_put(spi); |
| 41 | continue; |
| 42 | } |
| 43 | |
| 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", |
| 48 | nc->full_name); |
| 49 | spi_dev_put(spi); |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | /* Device address */ |
| 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", |
| 57 | nc->full_name); |
| 58 | spi_dev_put(spi); |
| 59 | continue; |
| 60 | } |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 61 | spi->chip_select = be32_to_cpup(prop); |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 62 | |
| 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; |
Wolfgang Ocker | f618ebf | 2008-10-15 15:00:47 +0200 | [diff] [blame] | 68 | if (of_find_property(nc, "spi-cs-high", NULL)) |
| 69 | spi->mode |= SPI_CS_HIGH; |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 70 | |
| 71 | /* Device speed */ |
| 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", |
| 75 | nc->full_name); |
| 76 | spi_dev_put(spi); |
| 77 | continue; |
| 78 | } |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 79 | spi->max_speed_hz = be32_to_cpup(prop); |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 80 | |
| 81 | /* IRQ */ |
| 82 | spi->irq = irq_of_parse_and_map(nc, 0); |
| 83 | |
| 84 | /* Store a pointer to the node in the device structure */ |
| 85 | of_node_get(nc); |
Grant Likely | d706c1b | 2010-04-13 16:12:28 -0700 | [diff] [blame] | 86 | spi->dev.of_node = nc; |
Grant Likely | 284b018 | 2008-05-16 11:37:09 -0600 | [diff] [blame] | 87 | |
| 88 | /* Register the new device */ |
| 89 | request_module(spi->modalias); |
| 90 | rc = spi_add_device(spi); |
| 91 | if (rc) { |
| 92 | dev_err(&master->dev, "spi_device register error %s\n", |
| 93 | nc->full_name); |
| 94 | spi_dev_put(spi); |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | } |
| 99 | EXPORT_SYMBOL(of_register_spi_devices); |