2015-07-02 19:17:16 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation
|
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-07-02 19:17:16 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-07-02 19:17:16 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-07-02 19:17:16 +08:00
|
|
|
*/
|
|
|
|
|
2016-02-08 04:29:17 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Public API for SPI drivers
|
|
|
|
*/
|
|
|
|
|
2015-07-02 19:17:16 +08:00
|
|
|
#ifndef __SPI_H__
|
|
|
|
#define __SPI_H__
|
|
|
|
|
2015-10-26 18:18:44 +08:00
|
|
|
/**
|
|
|
|
* @brief SPI Interface
|
|
|
|
* @defgroup spi_interface SPI Interface
|
|
|
|
* @ingroup io_interfaces
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-07-13 21:36:31 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <device.h>
|
|
|
|
|
2015-07-02 19:17:16 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-02-08 04:29:17 +08:00
|
|
|
/**
|
|
|
|
* @brief SPI Polarity & Phase Modes
|
|
|
|
*/
|
2015-10-07 03:57:10 +08:00
|
|
|
#define SPI_MODE_CPOL 0x1
|
|
|
|
#define SPI_MODE_CPHA 0x2
|
|
|
|
#define SPI_MODE_LOOP 0x4
|
2015-07-02 19:17:16 +08:00
|
|
|
|
2015-10-07 03:57:10 +08:00
|
|
|
#define SPI_MODE_MASK (0x7)
|
|
|
|
#define SPI_MODE(_in_) ((_in_) & SPI_MODE_MASK)
|
2015-07-02 19:17:16 +08:00
|
|
|
|
2016-02-08 04:29:17 +08:00
|
|
|
/**
|
|
|
|
* @brief SPI Transfer modes (host controller dependent)
|
|
|
|
*/
|
2015-10-07 03:57:10 +08:00
|
|
|
#define SPI_TRANSFER_MSB (0 << 3)
|
|
|
|
#define SPI_TRANSFER_LSB (1 << 3)
|
2015-07-02 19:17:16 +08:00
|
|
|
|
2015-10-07 03:57:10 +08:00
|
|
|
#define SPI_TRANSFER_MASK (0x8)
|
2015-07-02 19:17:16 +08:00
|
|
|
|
2015-10-07 03:57:10 +08:00
|
|
|
#define SPI_WORD_SIZE_MASK (0xFF << 4)
|
|
|
|
#define SPI_WORD_SIZE_GET(_in_) (((_in_) & SPI_WORD_SIZE_MASK) >> 4)
|
|
|
|
#define SPI_WORD(_in_) ((_in_) << 4)
|
2015-07-02 19:17:16 +08:00
|
|
|
|
2016-02-08 04:29:17 +08:00
|
|
|
/**
|
|
|
|
* @brief SPI configuration structure.
|
|
|
|
*
|
2015-07-02 19:17:16 +08:00
|
|
|
* config is a bit field with the following parts:
|
2016-02-08 04:29:17 +08:00
|
|
|
* mode [ 0 : 2 ] - Polarity, phase and loop mode.
|
|
|
|
* transfer_mode [ 3 ] - LSB or MSB first transfer mode.
|
|
|
|
* word_size [ 4 : 11 ] - Size of a data frame in bits.
|
2016-02-19 03:38:32 +08:00
|
|
|
* RESERVED [ 12 : 31 ] - Undefined or device-specific usage.
|
2015-07-02 19:17:16 +08:00
|
|
|
*
|
|
|
|
* max_sys_freq is the maximum frequency supported by the slave it
|
2016-02-08 04:29:17 +08:00
|
|
|
* will deal with. This value depends on the host controller. The driver
|
|
|
|
* can present a specific format for setting it.
|
2015-07-02 19:17:16 +08:00
|
|
|
*/
|
|
|
|
struct spi_config {
|
|
|
|
uint32_t config;
|
|
|
|
uint32_t max_sys_freq;
|
|
|
|
};
|
|
|
|
|
2016-06-16 05:18:38 +08:00
|
|
|
/**
|
|
|
|
* @typedef spi_api_configure
|
|
|
|
* @brief Callback API upon configuring the const controller
|
|
|
|
* See spi_configure() for argument description
|
|
|
|
*/
|
2015-10-05 23:02:16 +08:00
|
|
|
typedef int (*spi_api_configure)(struct device *dev,
|
2015-12-08 20:30:41 +08:00
|
|
|
struct spi_config *config);
|
2016-06-16 05:18:38 +08:00
|
|
|
/**
|
|
|
|
* @typedef spi_api_slave_select
|
|
|
|
* @brief Callback API upon selecting a slave
|
|
|
|
* See spi_slave_select() for argument description
|
|
|
|
*/
|
2015-08-21 17:48:54 +08:00
|
|
|
typedef int (*spi_api_slave_select)(struct device *dev, uint32_t slave);
|
2016-06-16 05:18:38 +08:00
|
|
|
/**
|
|
|
|
* @typedef spi_api_io
|
|
|
|
* @brief Callback API for I/O
|
|
|
|
* See spi_read() and spi_write() for argument descriptions
|
|
|
|
*/
|
2015-08-21 17:48:54 +08:00
|
|
|
typedef int (*spi_api_io)(struct device *dev,
|
2016-03-04 15:07:53 +08:00
|
|
|
const void *tx_buf, uint32_t tx_buf_len,
|
|
|
|
void *rx_buf, uint32_t rx_buf_len);
|
2016-06-16 05:18:38 +08:00
|
|
|
/**
|
|
|
|
* @typedef spi_api_control
|
|
|
|
* @brief Callback API upon for suspend/resume
|
|
|
|
* See spi_suspend() and spi_resume() for argument description
|
|
|
|
*/
|
2015-07-02 19:17:16 +08:00
|
|
|
typedef int (*spi_api_control)(struct device *dev);
|
|
|
|
|
|
|
|
struct spi_driver_api {
|
|
|
|
spi_api_configure configure;
|
2015-08-21 17:48:54 +08:00
|
|
|
spi_api_slave_select slave_select;
|
|
|
|
spi_api_io transceive;
|
2015-07-02 19:17:16 +08:00
|
|
|
spi_api_control suspend;
|
|
|
|
spi_api_control resume;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-02-08 04:29:17 +08:00
|
|
|
* @brief Configure a host controller for operating against slaves.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
* @param config Pointer to the configuration provided by the application.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-03-10 01:01:20 +08:00
|
|
|
* @retval 0 If successful.
|
2016-03-10 22:47:06 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2015-07-02 19:17:16 +08:00
|
|
|
*/
|
2015-10-05 23:02:16 +08:00
|
|
|
static inline int spi_configure(struct device *dev,
|
2015-12-08 20:30:41 +08:00
|
|
|
struct spi_config *config)
|
2015-07-02 19:17:16 +08:00
|
|
|
{
|
|
|
|
struct spi_driver_api *api = (struct spi_driver_api *)dev->driver_api;
|
2015-10-21 00:42:34 +08:00
|
|
|
|
2015-12-08 20:30:41 +08:00
|
|
|
return api->configure(dev, config);
|
2015-07-02 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 17:48:54 +08:00
|
|
|
/**
|
|
|
|
* @brief Select a slave to deal with.
|
|
|
|
*
|
2016-02-08 04:29:17 +08:00
|
|
|
* This routine is meaningful only if the controller supports per-slave
|
|
|
|
* addressing: One SS line per-slave. If not, this routine has no effect
|
|
|
|
* and daisy-chaining should be considered to deal with multiple slaves
|
2015-08-21 17:48:54 +08:00
|
|
|
* on the same line.
|
|
|
|
*
|
|
|
|
* @param dev Pointer to the device structure for the driver instance
|
|
|
|
* @param slave An integer identifying the slave
|
|
|
|
*
|
2016-03-10 01:01:20 +08:00
|
|
|
* @retval 0 If successful.
|
2016-03-10 22:47:06 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2015-08-21 17:48:54 +08:00
|
|
|
*/
|
2015-10-20 00:36:13 +08:00
|
|
|
static inline int spi_slave_select(struct device *dev, uint32_t slave)
|
2015-08-21 17:48:54 +08:00
|
|
|
{
|
|
|
|
struct spi_driver_api *api = (struct spi_driver_api *)dev->driver_api;
|
|
|
|
|
|
|
|
if (!api->slave_select) {
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2015-08-21 17:48:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return api->slave_select(dev, slave);
|
|
|
|
}
|
|
|
|
|
2015-07-02 19:17:16 +08:00
|
|
|
/**
|
2016-02-08 04:29:17 +08:00
|
|
|
* @brief Read the specified amount of data from the SPI driver.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
* @param buf Memory buffer where data will be transferred.
|
|
|
|
* @param len Size of the memory buffer available for writing.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-03-10 01:01:20 +08:00
|
|
|
* @retval 0 If successful.
|
2016-03-10 22:47:06 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2015-07-02 19:17:16 +08:00
|
|
|
*/
|
2016-03-04 15:07:53 +08:00
|
|
|
static inline int spi_read(struct device *dev, void *buf, uint32_t len)
|
2015-07-02 19:17:16 +08:00
|
|
|
{
|
|
|
|
struct spi_driver_api *api = (struct spi_driver_api *)dev->driver_api;
|
2015-10-21 00:42:34 +08:00
|
|
|
|
2015-08-21 17:48:54 +08:00
|
|
|
return api->transceive(dev, NULL, 0, buf, len);
|
2015-07-02 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-08 04:29:17 +08:00
|
|
|
* @brief Write the specified amount of data from the SPI driver.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
* @param buf Memory buffer from where data is transferred.
|
|
|
|
* @param len Size of the memory buffer available for reading.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-03-10 01:01:20 +08:00
|
|
|
* @retval 0 If successful.
|
2016-03-10 22:47:06 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2015-07-02 19:17:16 +08:00
|
|
|
*/
|
2016-03-04 15:07:53 +08:00
|
|
|
static inline int spi_write(struct device *dev, const void *buf, uint32_t len)
|
2015-07-02 19:17:16 +08:00
|
|
|
{
|
|
|
|
struct spi_driver_api *api = (struct spi_driver_api *)dev->driver_api;
|
2015-10-21 00:42:34 +08:00
|
|
|
|
2015-08-21 17:48:54 +08:00
|
|
|
return api->transceive(dev, buf, len, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-08 04:29:17 +08:00
|
|
|
* @brief Read and write the specified amount of data from the SPI driver.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-02-08 04:29:17 +08:00
|
|
|
* This routine is meant for full-duplex transmission.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-02-08 04:29:17 +08:00
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
* @param tx_buf Memory buffer where data originates
|
|
|
|
* @param tx_buf_len Size of the memory buffer available for reading.
|
|
|
|
* @param rx_buf Memory buffer where data is transferred.
|
|
|
|
* @param rx_buf_len Size of the memory buffer available for writing.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-03-10 01:01:20 +08:00
|
|
|
* @retval 0 If successful.
|
2016-03-10 22:47:06 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2015-08-21 17:48:54 +08:00
|
|
|
*/
|
2015-09-08 15:53:03 +08:00
|
|
|
static inline int spi_transceive(struct device *dev,
|
2016-03-04 15:07:53 +08:00
|
|
|
const void *tx_buf, uint32_t tx_buf_len,
|
|
|
|
void *rx_buf, uint32_t rx_buf_len)
|
2015-08-21 17:48:54 +08:00
|
|
|
{
|
|
|
|
struct spi_driver_api *api = (struct spi_driver_api *)dev->driver_api;
|
2015-10-21 00:42:34 +08:00
|
|
|
|
2015-08-21 17:48:54 +08:00
|
|
|
return api->transceive(dev, tx_buf, tx_buf_len, rx_buf, rx_buf_len);
|
2015-07-02 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-08 04:29:17 +08:00
|
|
|
* @brief Suspend the SPI host controller operations.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-03-10 01:01:20 +08:00
|
|
|
* @retval 0 If successful.
|
2016-03-10 22:47:06 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2015-07-02 19:17:16 +08:00
|
|
|
*/
|
2015-08-25 01:00:05 +08:00
|
|
|
static inline int spi_suspend(struct device *dev)
|
2015-07-02 19:17:16 +08:00
|
|
|
{
|
|
|
|
struct spi_driver_api *api = (struct spi_driver_api *)dev->driver_api;
|
2015-10-21 00:42:34 +08:00
|
|
|
|
2015-07-13 21:36:31 +08:00
|
|
|
return api->suspend(dev);
|
2015-07-02 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-08 04:29:17 +08:00
|
|
|
* @brief Resume the SPI host controller operations.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
2015-08-21 17:48:54 +08:00
|
|
|
*
|
2016-03-10 01:01:20 +08:00
|
|
|
* @retval 0 If successful.
|
2016-03-10 22:47:06 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2015-07-02 19:17:16 +08:00
|
|
|
*/
|
2015-08-25 01:00:05 +08:00
|
|
|
static inline int spi_resume(struct device *dev)
|
2015-07-02 19:17:16 +08:00
|
|
|
{
|
|
|
|
struct spi_driver_api *api = (struct spi_driver_api *)dev->driver_api;
|
2015-10-21 00:42:34 +08:00
|
|
|
|
2015-07-13 21:36:31 +08:00
|
|
|
return api->resume(dev);
|
2015-07-02 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-26 18:18:44 +08:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2015-07-02 19:17:16 +08:00
|
|
|
#endif /* __SPI_H__ */
|