2008-05-16 11:37:09 -06:00
|
|
|
/*
|
|
|
|
* SPI OF support routines
|
|
|
|
* Copyright (C) 2008 Secret Lab Technologies Ltd.
|
|
|
|
*
|
|
|
|
* Support routines for deriving SPI device attachments from the device
|
|
|
|
* tree.
|
|
|
|
*/
|
|
|
|
|
2011-07-17 13:58:06 -06:00
|
|
|
#include <linux/module.h>
|
2008-05-16 11:37:09 -06:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/spi/spi.h>
|
2010-06-18 11:09:59 -06:00
|
|
|
#include <linux/of_irq.h>
|
2008-05-16 11:37:09 -06:00
|
|
|
#include <linux/of_spi.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_register_spi_devices - Register child devices onto the SPI bus
|
|
|
|
* @master: Pointer to spi_master device
|
|
|
|
*
|
2010-07-27 14:35:58 -06:00
|
|
|
* Registers an spi_device for each child node of master node which has a 'reg'
|
2008-05-16 11:37:09 -06:00
|
|
|
* property.
|
|
|
|
*/
|
2010-07-27 14:35:58 -06:00
|
|
|
void of_register_spi_devices(struct spi_master *master)
|
2008-05-16 11:37:09 -06:00
|
|
|
{
|
|
|
|
struct spi_device *spi;
|
|
|
|
struct device_node *nc;
|
2010-01-30 01:45:26 -07:00
|
|
|
const __be32 *prop;
|
2008-05-16 11:37:09 -06:00
|
|
|
int rc;
|
|
|
|
int len;
|
|
|
|
|
2010-07-27 14:35:58 -06:00
|
|
|
if (!master->dev.of_node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for_each_child_of_node(master->dev.of_node, nc) {
|
2008-05-16 11:37:09 -06:00
|
|
|
/* Alloc an spi_device */
|
|
|
|
spi = spi_alloc_device(master);
|
|
|
|
if (!spi) {
|
|
|
|
dev_err(&master->dev, "spi_device alloc error for %s\n",
|
|
|
|
nc->full_name);
|
|
|
|
spi_dev_put(spi);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select device driver */
|
|
|
|
if (of_modalias_node(nc, spi->modalias,
|
|
|
|
sizeof(spi->modalias)) < 0) {
|
|
|
|
dev_err(&master->dev, "cannot find modalias for %s\n",
|
|
|
|
nc->full_name);
|
|
|
|
spi_dev_put(spi);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Device address */
|
|
|
|
prop = of_get_property(nc, "reg", &len);
|
|
|
|
if (!prop || len < sizeof(*prop)) {
|
|
|
|
dev_err(&master->dev, "%s has no 'reg' property\n",
|
|
|
|
nc->full_name);
|
|
|
|
spi_dev_put(spi);
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-30 01:45:26 -07:00
|
|
|
spi->chip_select = be32_to_cpup(prop);
|
2008-05-16 11:37:09 -06:00
|
|
|
|
|
|
|
/* Mode (clock phase/polarity/etc.) */
|
|
|
|
if (of_find_property(nc, "spi-cpha", NULL))
|
|
|
|
spi->mode |= SPI_CPHA;
|
|
|
|
if (of_find_property(nc, "spi-cpol", NULL))
|
|
|
|
spi->mode |= SPI_CPOL;
|
2008-10-15 07:00:47 -06:00
|
|
|
if (of_find_property(nc, "spi-cs-high", NULL))
|
|
|
|
spi->mode |= SPI_CS_HIGH;
|
2008-05-16 11:37:09 -06:00
|
|
|
|
|
|
|
/* Device speed */
|
|
|
|
prop = of_get_property(nc, "spi-max-frequency", &len);
|
|
|
|
if (!prop || len < sizeof(*prop)) {
|
|
|
|
dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
|
|
|
|
nc->full_name);
|
|
|
|
spi_dev_put(spi);
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-30 01:45:26 -07:00
|
|
|
spi->max_speed_hz = be32_to_cpup(prop);
|
2008-05-16 11:37:09 -06:00
|
|
|
|
|
|
|
/* IRQ */
|
|
|
|
spi->irq = irq_of_parse_and_map(nc, 0);
|
|
|
|
|
|
|
|
/* Store a pointer to the node in the device structure */
|
|
|
|
of_node_get(nc);
|
2010-04-13 17:12:28 -06:00
|
|
|
spi->dev.of_node = nc;
|
2008-05-16 11:37:09 -06:00
|
|
|
|
|
|
|
/* Register the new device */
|
|
|
|
request_module(spi->modalias);
|
|
|
|
rc = spi_add_device(spi);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(&master->dev, "spi_device register error %s\n",
|
|
|
|
nc->full_name);
|
|
|
|
spi_dev_put(spi);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_register_spi_devices);
|