aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c98
1 files changed, 95 insertions, 3 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 3d8f662e4fe9..1041cb83d67a 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -2,6 +2,7 @@
2 * SPI init/core code 2 * SPI init/core code
3 * 3 *
4 * Copyright (C) 2005 David Brownell 4 * Copyright (C) 2005 David Brownell
5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
5 * 6 *
6 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -19,15 +20,16 @@
19 */ 20 */
20 21
21#include <linux/kernel.h> 22#include <linux/kernel.h>
23#include <linux/kmod.h>
22#include <linux/device.h> 24#include <linux/device.h>
23#include <linux/init.h> 25#include <linux/init.h>
24#include <linux/cache.h> 26#include <linux/cache.h>
25#include <linux/mutex.h> 27#include <linux/mutex.h>
26#include <linux/of_device.h> 28#include <linux/of_device.h>
29#include <linux/of_irq.h>
27#include <linux/slab.h> 30#include <linux/slab.h>
28#include <linux/mod_devicetable.h> 31#include <linux/mod_devicetable.h>
29#include <linux/spi/spi.h> 32#include <linux/spi/spi.h>
30#include <linux/of_spi.h>
31#include <linux/pm_runtime.h> 33#include <linux/pm_runtime.h>
32#include <linux/export.h> 34#include <linux/export.h>
33#include <linux/sched.h> 35#include <linux/sched.h>
@@ -530,7 +532,7 @@ static void spi_pump_messages(struct kthread_work *work)
530 /* Lock queue and check for queue work */ 532 /* Lock queue and check for queue work */
531 spin_lock_irqsave(&master->queue_lock, flags); 533 spin_lock_irqsave(&master->queue_lock, flags);
532 if (list_empty(&master->queue) || !master->running) { 534 if (list_empty(&master->queue) || !master->running) {
533 if (master->busy) { 535 if (master->busy && master->unprepare_transfer_hardware) {
534 ret = master->unprepare_transfer_hardware(master); 536 ret = master->unprepare_transfer_hardware(master);
535 if (ret) { 537 if (ret) {
536 spin_unlock_irqrestore(&master->queue_lock, flags); 538 spin_unlock_irqrestore(&master->queue_lock, flags);
@@ -560,7 +562,7 @@ static void spi_pump_messages(struct kthread_work *work)
560 master->busy = true; 562 master->busy = true;
561 spin_unlock_irqrestore(&master->queue_lock, flags); 563 spin_unlock_irqrestore(&master->queue_lock, flags);
562 564
563 if (!was_busy) { 565 if (!was_busy && master->prepare_transfer_hardware) {
564 ret = master->prepare_transfer_hardware(master); 566 ret = master->prepare_transfer_hardware(master);
565 if (ret) { 567 if (ret) {
566 dev_err(&master->dev, 568 dev_err(&master->dev,
@@ -798,6 +800,94 @@ err_init_queue:
798 800
799/*-------------------------------------------------------------------------*/ 801/*-------------------------------------------------------------------------*/
800 802
803#if defined(CONFIG_OF) && !defined(CONFIG_SPARC)
804/**
805 * of_register_spi_devices() - Register child devices onto the SPI bus
806 * @master: Pointer to spi_master device
807 *
808 * Registers an spi_device for each child node of master node which has a 'reg'
809 * property.
810 */
811static void of_register_spi_devices(struct spi_master *master)
812{
813 struct spi_device *spi;
814 struct device_node *nc;
815 const __be32 *prop;
816 int rc;
817 int len;
818
819 if (!master->dev.of_node)
820 return;
821
822 for_each_child_of_node(master->dev.of_node, nc) {
823 /* Alloc an spi_device */
824 spi = spi_alloc_device(master);
825 if (!spi) {
826 dev_err(&master->dev, "spi_device alloc error for %s\n",
827 nc->full_name);
828 spi_dev_put(spi);
829 continue;
830 }
831
832 /* Select device driver */
833 if (of_modalias_node(nc, spi->modalias,
834 sizeof(spi->modalias)) < 0) {
835 dev_err(&master->dev, "cannot find modalias for %s\n",
836 nc->full_name);
837 spi_dev_put(spi);
838 continue;
839 }
840
841 /* Device address */
842 prop = of_get_property(nc, "reg", &len);
843 if (!prop || len < sizeof(*prop)) {
844 dev_err(&master->dev, "%s has no 'reg' property\n",
845 nc->full_name);
846 spi_dev_put(spi);
847 continue;
848 }
849 spi->chip_select = be32_to_cpup(prop);
850
851 /* Mode (clock phase/polarity/etc.) */
852 if (of_find_property(nc, "spi-cpha", NULL))
853 spi->mode |= SPI_CPHA;
854 if (of_find_property(nc, "spi-cpol", NULL))
855 spi->mode |= SPI_CPOL;
856 if (of_find_property(nc, "spi-cs-high", NULL))
857 spi->mode |= SPI_CS_HIGH;
858
859 /* Device speed */
860 prop = of_get_property(nc, "spi-max-frequency", &len);
861 if (!prop || len < sizeof(*prop)) {
862 dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
863 nc->full_name);
864 spi_dev_put(spi);
865 continue;
866 }
867 spi->max_speed_hz = be32_to_cpup(prop);
868
869 /* IRQ */
870 spi->irq = irq_of_parse_and_map(nc, 0);
871
872 /* Store a pointer to the node in the device structure */
873 of_node_get(nc);
874 spi->dev.of_node = nc;
875
876 /* Register the new device */
877 request_module(spi->modalias);
878 rc = spi_add_device(spi);
879 if (rc) {
880 dev_err(&master->dev, "spi_device register error %s\n",
881 nc->full_name);
882 spi_dev_put(spi);
883 }
884
885 }
886}
887#else
888static void of_register_spi_devices(struct spi_master *master) { }
889#endif
890
801static void spi_master_release(struct device *dev) 891static void spi_master_release(struct device *dev)
802{ 892{
803 struct spi_master *master; 893 struct spi_master *master;
@@ -846,6 +936,8 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
846 return NULL; 936 return NULL;
847 937
848 device_initialize(&master->dev); 938 device_initialize(&master->dev);
939 master->bus_num = -1;
940 master->num_chipselect = 1;
849 master->dev.class = &spi_master_class; 941 master->dev.class = &spi_master_class;
850 master->dev.parent = get_device(dev); 942 master->dev.parent = get_device(dev);
851 spi_master_set_devdata(master, &master[1]); 943 spi_master_set_devdata(master, &master[1]);