Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * polling/bitbanging SPI master controller driver utilities
0004  */
0005 
0006 #include <linux/spinlock.h>
0007 #include <linux/workqueue.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/module.h>
0010 #include <linux/delay.h>
0011 #include <linux/errno.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/slab.h>
0014 
0015 #include <linux/spi/spi.h>
0016 #include <linux/spi/spi_bitbang.h>
0017 
0018 #define SPI_BITBANG_CS_DELAY    100
0019 
0020 
0021 /*----------------------------------------------------------------------*/
0022 
0023 /*
0024  * FIRST PART (OPTIONAL):  word-at-a-time spi_transfer support.
0025  * Use this for GPIO or shift-register level hardware APIs.
0026  *
0027  * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
0028  * to glue code.  These bitbang setup() and cleanup() routines are always
0029  * used, though maybe they're called from controller-aware code.
0030  *
0031  * chipselect() and friends may use spi_device->controller_data and
0032  * controller registers as appropriate.
0033  *
0034  *
0035  * NOTE:  SPI controller pins can often be used as GPIO pins instead,
0036  * which means you could use a bitbang driver either to get hardware
0037  * working quickly, or testing for differences that aren't speed related.
0038  */
0039 
0040 struct spi_bitbang_cs {
0041     unsigned    nsecs;  /* (clock cycle time)/2 */
0042     u32     (*txrx_word)(struct spi_device *spi, unsigned nsecs,
0043                     u32 word, u8 bits, unsigned flags);
0044     unsigned    (*txrx_bufs)(struct spi_device *,
0045                     u32 (*txrx_word)(
0046                         struct spi_device *spi,
0047                         unsigned nsecs,
0048                         u32 word, u8 bits,
0049                         unsigned flags),
0050                     unsigned, struct spi_transfer *,
0051                     unsigned);
0052 };
0053 
0054 static unsigned bitbang_txrx_8(
0055     struct spi_device   *spi,
0056     u32         (*txrx_word)(struct spi_device *spi,
0057                     unsigned nsecs,
0058                     u32 word, u8 bits,
0059                     unsigned flags),
0060     unsigned        ns,
0061     struct spi_transfer *t,
0062     unsigned flags
0063 )
0064 {
0065     unsigned        bits = t->bits_per_word;
0066     unsigned        count = t->len;
0067     const u8        *tx = t->tx_buf;
0068     u8          *rx = t->rx_buf;
0069 
0070     while (likely(count > 0)) {
0071         u8      word = 0;
0072 
0073         if (tx)
0074             word = *tx++;
0075         word = txrx_word(spi, ns, word, bits, flags);
0076         if (rx)
0077             *rx++ = word;
0078         count -= 1;
0079     }
0080     return t->len - count;
0081 }
0082 
0083 static unsigned bitbang_txrx_16(
0084     struct spi_device   *spi,
0085     u32         (*txrx_word)(struct spi_device *spi,
0086                     unsigned nsecs,
0087                     u32 word, u8 bits,
0088                     unsigned flags),
0089     unsigned        ns,
0090     struct spi_transfer *t,
0091     unsigned flags
0092 )
0093 {
0094     unsigned        bits = t->bits_per_word;
0095     unsigned        count = t->len;
0096     const u16       *tx = t->tx_buf;
0097     u16         *rx = t->rx_buf;
0098 
0099     while (likely(count > 1)) {
0100         u16     word = 0;
0101 
0102         if (tx)
0103             word = *tx++;
0104         word = txrx_word(spi, ns, word, bits, flags);
0105         if (rx)
0106             *rx++ = word;
0107         count -= 2;
0108     }
0109     return t->len - count;
0110 }
0111 
0112 static unsigned bitbang_txrx_32(
0113     struct spi_device   *spi,
0114     u32         (*txrx_word)(struct spi_device *spi,
0115                     unsigned nsecs,
0116                     u32 word, u8 bits,
0117                     unsigned flags),
0118     unsigned        ns,
0119     struct spi_transfer *t,
0120     unsigned flags
0121 )
0122 {
0123     unsigned        bits = t->bits_per_word;
0124     unsigned        count = t->len;
0125     const u32       *tx = t->tx_buf;
0126     u32         *rx = t->rx_buf;
0127 
0128     while (likely(count > 3)) {
0129         u32     word = 0;
0130 
0131         if (tx)
0132             word = *tx++;
0133         word = txrx_word(spi, ns, word, bits, flags);
0134         if (rx)
0135             *rx++ = word;
0136         count -= 4;
0137     }
0138     return t->len - count;
0139 }
0140 
0141 int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
0142 {
0143     struct spi_bitbang_cs   *cs = spi->controller_state;
0144     u8          bits_per_word;
0145     u32         hz;
0146 
0147     if (t) {
0148         bits_per_word = t->bits_per_word;
0149         hz = t->speed_hz;
0150     } else {
0151         bits_per_word = 0;
0152         hz = 0;
0153     }
0154 
0155     /* spi_transfer level calls that work per-word */
0156     if (!bits_per_word)
0157         bits_per_word = spi->bits_per_word;
0158     if (bits_per_word <= 8)
0159         cs->txrx_bufs = bitbang_txrx_8;
0160     else if (bits_per_word <= 16)
0161         cs->txrx_bufs = bitbang_txrx_16;
0162     else if (bits_per_word <= 32)
0163         cs->txrx_bufs = bitbang_txrx_32;
0164     else
0165         return -EINVAL;
0166 
0167     /* nsecs = (clock period)/2 */
0168     if (!hz)
0169         hz = spi->max_speed_hz;
0170     if (hz) {
0171         cs->nsecs = (1000000000/2) / hz;
0172         if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
0173             return -EINVAL;
0174     }
0175 
0176     return 0;
0177 }
0178 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
0179 
0180 /*
0181  * spi_bitbang_setup - default setup for per-word I/O loops
0182  */
0183 int spi_bitbang_setup(struct spi_device *spi)
0184 {
0185     struct spi_bitbang_cs   *cs = spi->controller_state;
0186     struct spi_bitbang  *bitbang;
0187     bool            initial_setup = false;
0188     int         retval;
0189 
0190     bitbang = spi_master_get_devdata(spi->master);
0191 
0192     if (!cs) {
0193         cs = kzalloc(sizeof(*cs), GFP_KERNEL);
0194         if (!cs)
0195             return -ENOMEM;
0196         spi->controller_state = cs;
0197         initial_setup = true;
0198     }
0199 
0200     /* per-word shift register access, in hardware or bitbanging */
0201     cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
0202     if (!cs->txrx_word) {
0203         retval = -EINVAL;
0204         goto err_free;
0205     }
0206 
0207     if (bitbang->setup_transfer) {
0208         retval = bitbang->setup_transfer(spi, NULL);
0209         if (retval < 0)
0210             goto err_free;
0211     }
0212 
0213     dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
0214 
0215     return 0;
0216 
0217 err_free:
0218     if (initial_setup)
0219         kfree(cs);
0220     return retval;
0221 }
0222 EXPORT_SYMBOL_GPL(spi_bitbang_setup);
0223 
0224 /*
0225  * spi_bitbang_cleanup - default cleanup for per-word I/O loops
0226  */
0227 void spi_bitbang_cleanup(struct spi_device *spi)
0228 {
0229     kfree(spi->controller_state);
0230 }
0231 EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
0232 
0233 static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
0234 {
0235     struct spi_bitbang_cs   *cs = spi->controller_state;
0236     unsigned        nsecs = cs->nsecs;
0237     struct spi_bitbang  *bitbang;
0238 
0239     bitbang = spi_master_get_devdata(spi->master);
0240     if (bitbang->set_line_direction) {
0241         int err;
0242 
0243         err = bitbang->set_line_direction(spi, !!(t->tx_buf));
0244         if (err < 0)
0245             return err;
0246     }
0247 
0248     if (spi->mode & SPI_3WIRE) {
0249         unsigned flags;
0250 
0251         flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX;
0252         return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
0253     }
0254     return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
0255 }
0256 
0257 /*----------------------------------------------------------------------*/
0258 
0259 /*
0260  * SECOND PART ... simple transfer queue runner.
0261  *
0262  * This costs a task context per controller, running the queue by
0263  * performing each transfer in sequence.  Smarter hardware can queue
0264  * several DMA transfers at once, and process several controller queues
0265  * in parallel; this driver doesn't match such hardware very well.
0266  *
0267  * Drivers can provide word-at-a-time i/o primitives, or provide
0268  * transfer-at-a-time ones to leverage dma or fifo hardware.
0269  */
0270 
0271 static int spi_bitbang_prepare_hardware(struct spi_master *spi)
0272 {
0273     struct spi_bitbang  *bitbang;
0274 
0275     bitbang = spi_master_get_devdata(spi);
0276 
0277     mutex_lock(&bitbang->lock);
0278     bitbang->busy = 1;
0279     mutex_unlock(&bitbang->lock);
0280 
0281     return 0;
0282 }
0283 
0284 static int spi_bitbang_transfer_one(struct spi_master *master,
0285                     struct spi_device *spi,
0286                     struct spi_transfer *transfer)
0287 {
0288     struct spi_bitbang *bitbang = spi_master_get_devdata(master);
0289     int status = 0;
0290 
0291     if (bitbang->setup_transfer) {
0292         status = bitbang->setup_transfer(spi, transfer);
0293         if (status < 0)
0294             goto out;
0295     }
0296 
0297     if (transfer->len)
0298         status = bitbang->txrx_bufs(spi, transfer);
0299 
0300     if (status == transfer->len)
0301         status = 0;
0302     else if (status >= 0)
0303         status = -EREMOTEIO;
0304 
0305 out:
0306     spi_finalize_current_transfer(master);
0307 
0308     return status;
0309 }
0310 
0311 static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
0312 {
0313     struct spi_bitbang  *bitbang;
0314 
0315     bitbang = spi_master_get_devdata(spi);
0316 
0317     mutex_lock(&bitbang->lock);
0318     bitbang->busy = 0;
0319     mutex_unlock(&bitbang->lock);
0320 
0321     return 0;
0322 }
0323 
0324 static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
0325 {
0326     struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
0327 
0328     /* SPI core provides CS high / low, but bitbang driver
0329      * expects CS active
0330      * spi device driver takes care of handling SPI_CS_HIGH
0331      */
0332     enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
0333 
0334     ndelay(SPI_BITBANG_CS_DELAY);
0335     bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
0336                 BITBANG_CS_INACTIVE);
0337     ndelay(SPI_BITBANG_CS_DELAY);
0338 }
0339 
0340 /*----------------------------------------------------------------------*/
0341 
0342 int spi_bitbang_init(struct spi_bitbang *bitbang)
0343 {
0344     struct spi_master *master = bitbang->master;
0345     bool custom_cs;
0346 
0347     if (!master)
0348         return -EINVAL;
0349     /*
0350      * We only need the chipselect callback if we are actually using it.
0351      * If we just use GPIO descriptors, it is surplus. If the
0352      * SPI_MASTER_GPIO_SS flag is set, we always need to call the
0353      * driver-specific chipselect routine.
0354      */
0355     custom_cs = (!master->use_gpio_descriptors ||
0356              (master->flags & SPI_MASTER_GPIO_SS));
0357 
0358     if (custom_cs && !bitbang->chipselect)
0359         return -EINVAL;
0360 
0361     mutex_init(&bitbang->lock);
0362 
0363     if (!master->mode_bits)
0364         master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
0365 
0366     if (master->transfer || master->transfer_one_message)
0367         return -EINVAL;
0368 
0369     master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
0370     master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
0371     master->transfer_one = spi_bitbang_transfer_one;
0372     /*
0373      * When using GPIO descriptors, the ->set_cs() callback doesn't even
0374      * get called unless SPI_MASTER_GPIO_SS is set.
0375      */
0376     if (custom_cs)
0377         master->set_cs = spi_bitbang_set_cs;
0378 
0379     if (!bitbang->txrx_bufs) {
0380         bitbang->use_dma = 0;
0381         bitbang->txrx_bufs = spi_bitbang_bufs;
0382         if (!master->setup) {
0383             if (!bitbang->setup_transfer)
0384                 bitbang->setup_transfer =
0385                      spi_bitbang_setup_transfer;
0386             master->setup = spi_bitbang_setup;
0387             master->cleanup = spi_bitbang_cleanup;
0388         }
0389     }
0390 
0391     return 0;
0392 }
0393 EXPORT_SYMBOL_GPL(spi_bitbang_init);
0394 
0395 /**
0396  * spi_bitbang_start - start up a polled/bitbanging SPI master driver
0397  * @bitbang: driver handle
0398  *
0399  * Caller should have zero-initialized all parts of the structure, and then
0400  * provided callbacks for chip selection and I/O loops.  If the master has
0401  * a transfer method, its final step should call spi_bitbang_transfer; or,
0402  * that's the default if the transfer routine is not initialized.  It should
0403  * also set up the bus number and number of chipselects.
0404  *
0405  * For i/o loops, provide callbacks either per-word (for bitbanging, or for
0406  * hardware that basically exposes a shift register) or per-spi_transfer
0407  * (which takes better advantage of hardware like fifos or DMA engines).
0408  *
0409  * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
0410  * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
0411  * master methods.  Those methods are the defaults if the bitbang->txrx_bufs
0412  * routine isn't initialized.
0413  *
0414  * This routine registers the spi_master, which will process requests in a
0415  * dedicated task, keeping IRQs unblocked most of the time.  To stop
0416  * processing those requests, call spi_bitbang_stop().
0417  *
0418  * On success, this routine will take a reference to master. The caller is
0419  * responsible for calling spi_bitbang_stop() to decrement the reference and
0420  * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
0421  * leak.
0422  */
0423 int spi_bitbang_start(struct spi_bitbang *bitbang)
0424 {
0425     struct spi_master *master = bitbang->master;
0426     int ret;
0427 
0428     ret = spi_bitbang_init(bitbang);
0429     if (ret)
0430         return ret;
0431 
0432     /* driver may get busy before register() returns, especially
0433      * if someone registered boardinfo for devices
0434      */
0435     ret = spi_register_master(spi_master_get(master));
0436     if (ret)
0437         spi_master_put(master);
0438 
0439     return ret;
0440 }
0441 EXPORT_SYMBOL_GPL(spi_bitbang_start);
0442 
0443 /*
0444  * spi_bitbang_stop - stops the task providing spi communication
0445  */
0446 void spi_bitbang_stop(struct spi_bitbang *bitbang)
0447 {
0448     spi_unregister_master(bitbang->master);
0449 }
0450 EXPORT_SYMBOL_GPL(spi_bitbang_stop);
0451 
0452 MODULE_LICENSE("GPL");
0453