Skip to content

Commit a020ed7

Browse files
David Brownellgregkh
authored andcommitted
[PATCH] SPI: busnum == 0 needs to work
We need to be able to have a "SPI bus 0" matching chip numbering; but that number was wrongly used to flag dynamic allocation of a bus number. This patch resolves that issue; now negative numbers trigger dynamic alloc. It also updates the how-to-write-a-controller-driver overview to mention this stuff. Signed-off-by: David Brownell <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent ccf77cc commit a020ed7

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Documentation/spi/spi-summary

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,33 @@ to get the driver-private data allocated for that device.
414414
The driver will initialize the fields of that spi_master, including the
415415
bus number (maybe the same as the platform device ID) and three methods
416416
used to interact with the SPI core and SPI protocol drivers. It will
417-
also initialize its own internal state.
417+
also initialize its own internal state. (See below about bus numbering
418+
and those methods.)
419+
420+
After you initialize the spi_master, then use spi_register_master() to
421+
publish it to the rest of the system. At that time, device nodes for
422+
the controller and any predeclared spi devices will be made available,
423+
and the driver model core will take care of binding them to drivers.
424+
425+
If you need to remove your SPI controller driver, spi_unregister_master()
426+
will reverse the effect of spi_register_master().
427+
428+
429+
BUS NUMBERING
430+
431+
Bus numbering is important, since that's how Linux identifies a given
432+
SPI bus (shared SCK, MOSI, MISO). Valid bus numbers start at zero. On
433+
SOC systems, the bus numbers should match the numbers defined by the chip
434+
manufacturer. For example, hardware controller SPI2 would be bus number 2,
435+
and spi_board_info for devices connected to it would use that number.
436+
437+
If you don't have such hardware-assigned bus number, and for some reason
438+
you can't just assign them, then provide a negative bus number. That will
439+
then be replaced by a dynamically assigned number. You'd then need to treat
440+
this as a non-static configuration (see above).
441+
442+
443+
SPI MASTER METHODS
418444

419445
master->setup(struct spi_device *spi)
420446
This sets up the device clock rate, SPI mode, and word sizes.
@@ -431,6 +457,9 @@ also initialize its own internal state.
431457
state it dynamically associates with that device. If you do that,
432458
be sure to provide the cleanup() method to free that state.
433459

460+
461+
SPI MESSAGE QUEUE
462+
434463
The bulk of the driver will be managing the I/O queue fed by transfer().
435464

436465
That queue could be purely conceptual. For example, a driver used only
@@ -440,6 +469,9 @@ But the queue will probably be very real, using message->queue, PIO,
440469
often DMA (especially if the root filesystem is in SPI flash), and
441470
execution contexts like IRQ handlers, tasklets, or workqueues (such
442471
as keventd). Your driver can be as fancy, or as simple, as you need.
472+
Such a transfer() method would normally just add the message to a
473+
queue, and then start some asynchronous transfer engine (unless it's
474+
already running).
443475

444476

445477
THANKS TO

drivers/spi/spi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ EXPORT_SYMBOL_GPL(spi_alloc_master);
395395
int __init_or_module
396396
spi_register_master(struct spi_master *master)
397397
{
398-
static atomic_t dyn_bus_id = ATOMIC_INIT(0);
398+
static atomic_t dyn_bus_id = ATOMIC_INIT((1<<16) - 1);
399399
struct device *dev = master->cdev.dev;
400400
int status = -ENODEV;
401401
int dynamic = 0;
@@ -404,7 +404,7 @@ spi_register_master(struct spi_master *master)
404404
return -ENODEV;
405405

406406
/* convention: dynamically assigned bus IDs count down from the max */
407-
if (master->bus_num == 0) {
407+
if (master->bus_num < 0) {
408408
master->bus_num = atomic_dec_return(&dyn_bus_id);
409409
dynamic = 1;
410410
}

include/linux/spi/spi.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
172172
struct spi_master {
173173
struct class_device cdev;
174174

175-
/* other than zero (== assign one dynamically), bus_num is fully
175+
/* other than negative (== assign one dynamically), bus_num is fully
176176
* board-specific. usually that simplifies to being SOC-specific.
177-
* example: one SOC has three SPI controllers, numbered 1..3,
177+
* example: one SOC has three SPI controllers, numbered 0..2,
178178
* and one board's schematics might show it using SPI-2. software
179179
* would normally use bus_num=2 for that controller.
180180
*/
181-
u16 bus_num;
181+
s16 bus_num;
182182

183183
/* chipselects will be integral to many controllers; some others
184184
* might use board-specific GPIOs.

0 commit comments

Comments
 (0)