Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:11

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @brief Serial Peripheral Interface (SPI) Driver API
0007  *
0008  * @ingroup SPI
0009  */
0010 
0011 /*
0012  * Copyright (C) 2016, 2017 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifndef _DEV_SPI_SPI_H
0037 #define _DEV_SPI_SPI_H
0038 
0039 #include <linux/spi/spidev.h>
0040 
0041 #include <rtems.h>
0042 #include <rtems/seterr.h>
0043 #include <rtems/thread.h>
0044 
0045 #ifdef __cplusplus
0046 extern "C" {
0047 #endif /* __cplusplus */
0048 
0049 typedef struct spi_ioc_transfer spi_ioc_transfer;
0050 
0051 typedef struct spi_bus spi_bus;
0052 
0053 /**
0054  * @defgroup SPI Serial Peripheral Interface (SPI) Driver
0055  *
0056  * @ingroup RTEMSDeviceDrivers
0057  *
0058  * @brief Serial Peripheral Interface (SPI) bus driver support.
0059  *
0060  * @{
0061  */
0062 
0063 /**
0064  * @brief Obtains the bus.
0065  *
0066  * This command has no argument.
0067  */
0068 #define SPI_BUS_OBTAIN _IO(SPI_IOC_MAGIC, 13)
0069 
0070 /**
0071  * @brief Releases the bus.
0072  *
0073  * This command has no argument.
0074  */
0075 #define SPI_BUS_RELEASE _IO(SPI_IOC_MAGIC, 23)
0076 
0077 /**
0078  * @brief SPI bus control.
0079  */
0080 struct spi_bus {
0081   /**
0082    * @brief Transfers SPI messages.
0083    *
0084    * @param[in] bus The bus control.
0085    * @param[in] msgs The messages to transfer.
0086    * @param[in] msg_count The count of messages to transfer.  It must be
0087    * positive.
0088    *
0089    * @retval 0 Successful operation.
0090    * @retval negative Negative error number in case of an error.
0091    */
0092   int (*transfer)(spi_bus *bus, const spi_ioc_transfer *msgs, uint32_t msg_count);
0093 
0094   /**
0095    * @brief Checks if maximum speed and bits per word are in a valid range
0096    * for the device
0097    *
0098    * @param[in] bus The bus control.
0099    *
0100    * @retval 0 Successful operation.
0101    * @retval negative Negative error number in case of an error.
0102    */
0103   int (*setup)(spi_bus *bus);
0104 
0105   /**
0106    * @brief Destroys the bus.
0107    *
0108    * @param[in] bus The bus control.
0109    */
0110   void (*destroy)(spi_bus *bus);
0111 
0112   /**
0113    * @brief Mutex to protect the bus access.
0114    */
0115   rtems_recursive_mutex mutex;
0116 
0117   /**
0118    * @brief Maximum Speed in Hz
0119    */
0120   uint32_t max_speed_hz;
0121 
0122   /**
0123    * @brief Indicates the speed of the current device message.
0124    */
0125   uint32_t speed_hz;
0126 
0127   /**
0128    * @brief Indicates if chip select must be set high after transfer.
0129    */
0130   bool cs_change;
0131 
0132   /**
0133    * @brief Indicates which device is selected by chip select
0134    */
0135   uint8_t cs;
0136 
0137   /**
0138    * @brief Indicates the bits per word used on the device.
0139    */
0140   uint8_t bits_per_word;
0141 
0142   /**
0143    * @brief Indicates if LSB is supposed to be transmitted first.
0144    */
0145   bool lsb_first;
0146 
0147   /**
0148    * @brief Current mode.
0149    */
0150   uint32_t mode;
0151 
0152   /**
0153    * @brief Indicates the delay between transfers on different chip select
0154    * devices.
0155    */
0156   uint16_t delay_usecs;
0157 
0158   /**
0159    * @brief Driver specific ioctl.
0160    *
0161    * @param[in] bus The bus control.
0162    */
0163   int (*ioctl)(spi_bus *bus, ioctl_command_t command, void *arg);
0164 };
0165 
0166 /**
0167  * @brief Initializes a bus control.
0168  *
0169  * After a sucessful initialization the bus control must be destroyed via
0170  * spi_bus_destroy().  A registered bus control will be automatically destroyed
0171  * in case the device file is unlinked.  Make sure to call spi_bus_destroy() in
0172  * a custom destruction handler.
0173  *
0174  * @param[in] bus The bus control.
0175  *
0176  * @retval 0 Successful operation.
0177  * @retval -1 An error occurred.  The errno is set to indicate the error.
0178  *
0179  * @see spi_bus_register()
0180  */
0181 int spi_bus_init(spi_bus *bus);
0182 
0183 /**
0184  * @brief Allocates a bus control from the heap and initializes it.
0185  *
0186  * After a sucessful allocation and initialization the bus control must be
0187  * destroyed via spi_bus_destroy_and_free().  A registered bus control will be
0188  * automatically destroyed in case the device file is unlinked.  Make sure to
0189  * call spi_bus_destroy_and_free() in a custom destruction handler.
0190  *
0191  * @param[in] size The size of the bus control.  This enables the addition of
0192  * bus controller specific data to the base bus control.  The bus control is
0193  * zero initialized.
0194  *
0195  * @retval non-NULL The new bus control.
0196  * @retval NULL An error occurred.  The errno is set to indicate the error.
0197  *
0198  * @see spi_bus_register()
0199  */
0200 spi_bus *spi_bus_alloc_and_init(size_t size);
0201 
0202 /**
0203  * @brief Destroys a bus control.
0204  *
0205  * @param[in] bus The bus control.
0206  */
0207 void spi_bus_destroy(spi_bus *bus);
0208 
0209 /**
0210  * @brief Destroys a bus control and frees its memory.
0211  *
0212  * @param[in] bus The bus control.
0213  */
0214 void spi_bus_destroy_and_free(spi_bus *bus);
0215 
0216 /**
0217  * @brief Registers a bus control.
0218  *
0219  * This function claims ownership of the bus control regardless if the
0220  * registration is successful or not.
0221  *
0222  * @param[in] bus The bus control.
0223  * @param[in] bus_path The path to the bus device file.
0224  *
0225  * @retval 0 Successful operation.
0226  * @retval -1 An error occurred.  The errno is set to indicate the error.
0227  */
0228 int spi_bus_register(
0229   spi_bus *bus,
0230   const char *bus_path
0231 );
0232 
0233 /** @} */
0234 
0235 #ifdef __cplusplus
0236 }
0237 #endif /* __cplusplus */
0238 
0239 #endif /* _DEV_SPI_SPI_H */