Files
android_kernel_xiaomi_sm8450/include/linux
Ernst Schwab cf32b71e98 spi/mmc_spi: SPI bus locking API, using mutex
SPI bus locking API to allow exclusive access to the SPI bus, especially, but
not limited to, for the mmc_spi driver.

Coded according to an outline from Grant Likely; here is his
specification (accidentally swapped function names corrected):

It requires 3 things to be added to struct spi_master.
- 1 Mutex
- 1 spin lock
- 1 flag.

The mutex protects spi_sync, and provides sleeping "for free"
The spinlock protects the atomic spi_async call.
The flag is set when the lock is obtained, and checked while holding
the spinlock in spi_async().  If the flag is checked, then spi_async()
must fail immediately.

The current runtime API looks like this:
spi_async(struct spi_device*, struct spi_message*);
spi_sync(struct spi_device*, struct spi_message*);

The API needs to be extended to this:
spi_async(struct spi_device*, struct spi_message*)
spi_sync(struct spi_device*, struct spi_message*)
spi_bus_lock(struct spi_master*)  /* although struct spi_device* might
be easier */
spi_bus_unlock(struct spi_master*)
spi_async_locked(struct spi_device*, struct spi_message*)
spi_sync_locked(struct spi_device*, struct spi_message*)

Drivers can only call the last two if they already hold the spi_master_lock().

spi_bus_lock() obtains the mutex, obtains the spin lock, sets the
flag, and releases the spin lock before returning.  It doesn't even
need to sleep while waiting for "in-flight" spi_transactions to
complete because its purpose is to guarantee no additional
transactions are added.  It does not guarantee that the bus is idle.

spi_bus_unlock() clears the flag and releases the mutex, which will
wake up any waiters.

The difference between spi_async() and spi_async_locked() is that the
locked version bypasses the check of the lock flag.  Both versions
need to obtain the spinlock.

The difference between spi_sync() and spi_sync_locked() is that
spi_sync() must hold the mutex while enqueuing a new transfer.
spi_sync_locked() doesn't because the mutex is already held.  Note
however that spi_sync must *not* continue to hold the mutex while
waiting for the transfer to complete, otherwise only one transfer
could be queued up at a time!

Almost no code needs to be written.  The current spi_async() and
spi_sync() can probably be renamed to __spi_async() and __spi_sync()
so that spi_async(), spi_sync(), spi_async_locked() and
spi_sync_locked() can just become wrappers around the common code.

spi_sync() is protected by a mutex because it can sleep
spi_async() needs to be protected with a flag and a spinlock because
it can be called atomically and must not sleep

Signed-off-by: Ernst Schwab <eschwab@online.de>
[grant.likely@secretlab.ca: use spin_lock_irqsave()]
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Tested-by: Matt Fleming <matt@console-pimps.org>
Tested-by: Antonio Ospite <ospite@studenti.unina.it>
2010-06-28 17:49:29 -07:00
..
2010-05-17 22:39:48 -07:00
2010-05-28 01:38:00 +02:00
2010-05-03 08:33:00 -04:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2010-03-12 15:53:10 -08:00
2009-06-17 00:36:36 -04:00
2010-05-14 17:08:01 -04:00
2010-05-27 09:12:53 -07:00
2009-09-08 17:42:50 -07:00
2010-05-19 13:38:54 -04:00
2009-11-20 20:13:39 +01:00
2009-11-20 20:13:39 +01:00
2009-11-20 20:13:39 +01:00
2010-02-05 07:35:05 -08:00
2009-07-12 12:22:34 -07:00
2010-05-19 22:41:57 -04:00
2010-05-10 16:08:01 -07:00
2009-12-10 23:52:01 +00:00
2010-05-17 16:30:58 -07:00
2009-11-04 09:50:58 -08:00
2010-05-21 19:30:44 +02:00
2009-10-01 21:17:49 +02:00
2010-06-01 11:12:28 +02:00
2009-09-14 17:41:42 -07:00
2009-12-15 08:53:33 -08:00
2010-04-09 08:36:07 +02:00
2009-11-04 09:50:58 -08:00
2009-12-23 13:33:54 +01:00
2010-05-27 22:05:02 -04:00
2009-06-18 13:04:05 -07:00
2009-11-04 09:50:58 -08:00
2010-03-16 08:55:32 +01:00
2010-05-25 08:07:01 -07:00
2010-05-27 09:12:42 -07:00
2010-05-21 09:34:29 -07:00
2009-09-01 01:13:31 -07:00
2010-02-09 11:13:56 +01:00
2010-03-07 22:17:09 +01:00
2010-03-02 12:23:42 +01:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2010-02-02 07:32:29 -08:00
2010-01-11 16:28:01 -08:00
2010-03-03 11:26:01 +01:00
2010-05-11 12:01:10 -07:00
2009-11-04 09:50:58 -08:00
2010-03-12 15:52:40 -08:00
2009-09-26 10:17:19 -07:00
2010-05-11 14:40:55 +02:00
2010-05-03 11:50:57 +02:00
2009-08-29 15:53:00 +02:00
2010-04-23 02:08:44 +02:00
2010-05-21 09:37:29 -07:00
2009-12-16 06:56:12 -08:00
2010-05-14 15:09:32 -04:00
2010-05-17 12:17:10 +03:00
2010-04-03 14:56:05 -07:00
2009-07-30 16:03:45 +09:30
2009-12-26 20:40:34 -08:00
2010-02-03 17:39:50 +11:00
2010-02-09 11:13:56 +01:00
2010-02-19 03:35:12 -05:00
2009-06-16 19:47:48 -07:00
2009-09-22 07:17:35 -07:00
2009-06-16 08:40:20 +02:00
2010-03-12 15:52:28 -08:00
2009-12-12 13:08:15 +01:00
2010-05-11 14:40:55 +02:00
2010-04-13 14:49:34 -07:00
2010-02-10 23:49:08 +09:00
2010-05-17 05:27:42 +02:00
2009-11-04 09:50:58 -08:00
2009-06-17 18:02:11 -07:00
2009-06-17 18:02:11 -07:00
2009-06-17 18:02:11 -07:00
2009-06-15 21:44:43 -07:00
2010-05-19 13:44:27 +10:00
2010-06-07 16:03:10 -04:00
2010-04-02 14:30:39 -07:00
2009-11-04 09:50:58 -08:00
2009-11-04 09:50:58 -08:00
2010-05-10 23:08:19 +02:00
2010-03-12 15:53:11 -08:00
2009-12-03 11:43:23 +00:00
2009-06-18 13:04:04 -07:00
2009-07-29 19:10:36 -07:00
2009-12-02 09:55:33 +01:00
2009-09-23 07:39:41 -07:00
2010-02-10 17:47:17 -08:00
2010-05-03 15:53:54 -07:00
2009-10-30 15:06:37 -07:00
2010-03-12 15:52:44 -08:00
2010-03-02 14:28:49 -05:00
2010-05-27 09:12:50 -07:00
2010-05-30 09:02:47 -07:00
2009-11-04 09:50:58 -08:00
2010-05-11 10:09:47 +02:00
2010-03-12 15:53:10 -08:00
2010-05-06 10:56:07 +10:00
2010-05-25 11:41:43 -04:00
2009-11-30 12:02:53 +09:00
2010-03-12 15:52:44 -08:00
2010-04-03 15:09:04 -07:00
2009-12-16 22:32:29 -05:00
2010-05-10 11:08:35 -07:00
2010-01-14 22:38:09 -05:00
2010-05-27 09:12:43 -07:00
2010-05-15 23:28:39 -07:00
2010-02-18 15:43:09 -08:00
2010-03-25 17:18:43 -07:00
2010-04-13 12:43:42 +02:00
2010-03-23 17:19:38 +01:00
2010-05-12 23:02:23 -07:00
2009-10-29 11:17:40 +11:00
2010-05-21 09:34:29 -07:00
2009-11-10 20:54:38 -08:00
2009-09-23 18:13:10 -07:00
2010-05-19 22:40:47 -04:00
2009-08-26 12:39:29 +01:00
2010-06-03 13:13:34 +10:00
2009-09-23 22:26:32 +09:30
2010-05-19 22:15:46 +09:30
2010-04-06 20:35:58 +04:00