Skip to content

Commit 7431798

Browse files
plagniojglikely
authored andcommitted
of_spi: add generic binding support to specify cs gpio
This will allow to use gpio for chip select with no modification in the driver binding When use the cs-gpios, the gpio number will be passed via the cs_gpio field and the number of chip select will automatically increased with max(hw cs, gpio cs). So if for example the controller has 2 CS lines, and the cs-gpios property looks like this: cs-gpios = <&gpio1 0 0> <0> <&gpio1 1 0> <&gpio1 2 0>; Then it should be configured so that num_chipselect = 4 with the following mapping: cs0 : &gpio1 0 0 cs1 : native cs2 : &gpio1 1 0 cs3 : &gpio1 2 0 Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Cc: devicetree-discuss@lists.ozlabs.org Cc: spi-devel-general@lists.sourceforge.net Signed-off-by: Richard Genoud <richard.genoud@gmail.com> [grant.likely: fixed up type of cs count so min() can do type checking] Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
1 parent 15d0983 commit 7431798

3 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎Documentation/devicetree/bindings/spi/spi-bus.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The SPI master node requires the following properties:
1212
- #size-cells - should be zero.
1313
- compatible - name of SPI bus controller following generic names
1414
recommended practice.
15+
- cs-gpios - (optional) gpios chip select.
1516
No other properties are required in the SPI bus node. It is assumed
1617
that a driver for an SPI bus device will understand that it is an SPI bus.
1718
However, the binding does not attempt to define the specific method for
@@ -24,6 +25,22 @@ support describing the chip select layout.
2425
Optional property:
2526
- num-cs : total number of chipselects
2627

28+
If cs-gpios is used the number of chip select will automatically increased
29+
with max(cs-gpios > hw cs)
30+
31+
So if for example the controller has 2 CS lines, and the cs-gpios
32+
property looks like this:
33+
34+
cs-gpios = <&gpio1 0 0> <0> <&gpio1 1 0> <&gpio1 2 0>;
35+
36+
Then it should be configured so that num_chipselect = 4 with the
37+
following mapping:
38+
39+
cs0 : &gpio1 0 0
40+
cs1 : native
41+
cs2 : &gpio1 1 0
42+
cs3 : &gpio1 2 0
43+
2744
SPI slave nodes must be children of the SPI master node and can
2845
contain the following properties.
2946
- reg - (required) chip select address of device.
@@ -37,6 +54,9 @@ contain the following properties.
3754
- spi-cs-high - (optional) Empty property indicating device requires
3855
chip select active high
3956

57+
If a gpio chipselect is used for the SPI slave the gpio number will be passed
58+
via the cs_gpio
59+
4060
SPI example for an MPC5200 SPI bus:
4161
spi@f00 {
4262
#address-cells = <1>;

‎drivers/spi/spi.c‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/slab.h>
3131
#include <linux/mod_devicetable.h>
3232
#include <linux/spi/spi.h>
33+
#include <linux/of_gpio.h>
3334
#include <linux/pm_runtime.h>
3435
#include <linux/export.h>
3536
#include <linux/sched.h>
@@ -327,6 +328,7 @@ struct spi_device *spi_alloc_device(struct spi_master *master)
327328
spi->dev.parent = &master->dev;
328329
spi->dev.bus = &spi_bus_type;
329330
spi->dev.release = spidev_release;
331+
spi->cs_gpio = -EINVAL;
330332
device_initialize(&spi->dev);
331333
return spi;
332334
}
@@ -344,15 +346,16 @@ EXPORT_SYMBOL_GPL(spi_alloc_device);
344346
int spi_add_device(struct spi_device *spi)
345347
{
346348
static DEFINE_MUTEX(spi_add_lock);
347-
struct device *dev = spi->master->dev.parent;
349+
struct spi_master *master = spi->master;
350+
struct device *dev = master->dev.parent;
348351
struct device *d;
349352
int status;
350353

351354
/* Chipselects are numbered 0..max; validate. */
352-
if (spi->chip_select >= spi->master->num_chipselect) {
355+
if (spi->chip_select >= master->num_chipselect) {
353356
dev_err(dev, "cs%d >= max %d\n",
354357
spi->chip_select,
355-
spi->master->num_chipselect);
358+
master->num_chipselect);
356359
return -EINVAL;
357360
}
358361

@@ -376,6 +379,9 @@ int spi_add_device(struct spi_device *spi)
376379
goto done;
377380
}
378381

382+
if (master->cs_gpios)
383+
spi->cs_gpio = master->cs_gpios[spi->chip_select];
384+
379385
/* Drivers may modify this initial i/o setup, but will
380386
* normally rely on the device being setup. Devices
381387
* using SPI_CS_HIGH can't coexist well otherwise...
@@ -946,6 +952,44 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
946952
}
947953
EXPORT_SYMBOL_GPL(spi_alloc_master);
948954

955+
#ifdef CONFIG_OF
956+
static int of_spi_register_master(struct spi_master *master)
957+
{
958+
u16 nb;
959+
int i, *cs;
960+
struct device_node *np = master->dev.of_node;
961+
962+
if (!np)
963+
return 0;
964+
965+
nb = of_gpio_named_count(np, "cs-gpios");
966+
master->num_chipselect = max(nb, master->num_chipselect);
967+
968+
if (nb < 1)
969+
return 0;
970+
971+
cs = devm_kzalloc(&master->dev,
972+
sizeof(int) * master->num_chipselect,
973+
GFP_KERNEL);
974+
master->cs_gpios = cs;
975+
976+
if (!master->cs_gpios)
977+
return -ENOMEM;
978+
979+
memset(cs, -EINVAL, master->num_chipselect);
980+
981+
for (i = 0; i < nb; i++)
982+
cs[i] = of_get_named_gpio(np, "cs-gpios", i);
983+
984+
return 0;
985+
}
986+
#else
987+
static int of_spi_register_master(struct spi_master *master)
988+
{
989+
return 0;
990+
}
991+
#endif
992+
949993
/**
950994
* spi_register_master - register SPI master controller
951995
* @master: initialized master, originally from spi_alloc_master()
@@ -977,6 +1021,10 @@ int spi_register_master(struct spi_master *master)
9771021
if (!dev)
9781022
return -ENODEV;
9791023

1024+
status = of_spi_register_master(master);
1025+
if (status)
1026+
return status;
1027+
9801028
/* even if it's just one always-selected device, there must
9811029
* be at least one chipselect
9821030
*/

‎include/linux/spi/spi.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct spi_device {
9090
void *controller_state;
9191
void *controller_data;
9292
char modalias[SPI_NAME_SIZE];
93+
int cs_gpio; /* chip select gpio */
9394

9495
/*
9596
* likely need more hooks for more protocol options affecting how
@@ -362,6 +363,8 @@ struct spi_master {
362363
int (*transfer_one_message)(struct spi_master *master,
363364
struct spi_message *mesg);
364365
int (*unprepare_transfer_hardware)(struct spi_master *master);
366+
/* gpio chip select */
367+
int *cs_gpios;
365368
};
366369

367370
static inline void *spi_master_get_devdata(struct spi_master *master)

0 commit comments

Comments
 (0)