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 RTEMS Port of Linux I2C API
0007  *
0008  * @ingroup I2CLinux
0009  */
0010 
0011 /*
0012  * Copyright (c) 2014 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 _UAPI_LINUX_I2C_H
0037 #define _UAPI_LINUX_I2C_H
0038 
0039 #include <stdint.h>
0040 
0041 /**
0042  * @defgroup I2CLinux Linux I2C User-Space API
0043  *
0044  * @ingroup I2C
0045  *
0046  * @brief RTEMS port of Linux I2C user-space API.
0047  *
0048  * Additional documentation is available through the Linux sources, see:
0049  *
0050  * - /usr/src/linux/include/uapi/linux/i2c.h,
0051  * - /usr/src/linux/include/uapi/linux/i2c-dev.h
0052  * - https://www.kernel.org/doc/Documentation/i2c/i2c-protocol
0053  * - https://www.kernel.org/doc/Documentation/i2c/dev-interface
0054  *
0055  * @{
0056  */
0057 
0058 /**
0059  * @name I2C Message Flags
0060  *
0061  * @{
0062  */
0063 
0064 /**
0065  * @brief I2C message flag to indicate a 10-bit address.
0066  *
0067  * The controller must support this as indicated by the I2C_FUNC_10BIT_ADDR
0068  * functionality.
0069  *
0070  * @see i2c_msg.
0071  */
0072 #define I2C_M_TEN 0x0010
0073 
0074 /**
0075  * @brief I2C message flag to indicate a read transfer (from slave to master).
0076  *
0077  * @see i2c_msg.
0078  */
0079 #define I2C_M_RD 0x0001
0080 
0081 /**
0082  * @brief I2C message flag to signal a stop condition even if this is not the
0083  * last message.
0084  *
0085  * The controller must support this as indicated by the
0086  * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
0087  *
0088  * @see i2c_msg.
0089  */
0090 #define I2C_M_STOP 0x8000
0091 
0092 /**
0093  * @brief I2C message flag to omit start condition and slave address.
0094  *
0095  * The controller must support this as indicated by the
0096  * @ref I2C_FUNC_NOSTART functionality.
0097  *
0098  * @see i2c_msg.
0099  */
0100 #define I2C_M_NOSTART 0x4000
0101 
0102 /**
0103  * @brief I2C message flag to reverse the direction flag.
0104  *
0105  * The controller must support this as indicated by the
0106  * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
0107  *
0108  * @see i2c_msg.
0109  */
0110 #define I2C_M_REV_DIR_ADDR 0x2000
0111 
0112 /**
0113  * @brief I2C message flag to ignore a non-acknowledge.
0114  *
0115  * The controller must support this as indicated by the
0116  * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
0117  *
0118  * @see i2c_msg.
0119  */
0120 #define I2C_M_IGNORE_NAK 0x1000
0121 
0122 /**
0123  * @brief I2C message flag to omit a master acknowledge/non-acknowledge in a
0124  * read transfer.
0125  *
0126  * The controller must support this as indicated by the
0127  * @ref I2C_FUNC_PROTOCOL_MANGLING functionality.
0128  *
0129  * @see i2c_msg.
0130  */
0131 #define I2C_M_NO_RD_ACK 0x0800
0132 
0133 /**
0134  * @brief I2C message flag to indicate that the message data length is the
0135  * first received byte.
0136  *
0137  * The message data buffer must be large enough to store up to 32 bytes, the
0138  * initial length byte and the SMBus PEC (if used).  Initialize the message
0139  * length to one.  The message length is incremented by the count of received
0140  * data bytes.
0141  *
0142  * @see i2c_msg.
0143  */
0144 #define I2C_M_RECV_LEN 0x0400
0145 
0146 /** @} */
0147 
0148 /**
0149  * @brief I2C transfer message.
0150  */
0151 struct i2c_msg {
0152   /**
0153    * @brief The slave address.
0154    *
0155    * In case the @ref I2C_M_TEN flag is set, then this is a 10-bit address,
0156    * otherwise it is a 7-bit address.
0157    */
0158   uint16_t addr;
0159 
0160   /**
0161    * @brief The message flags.
0162    *
0163    * Valid flags are
0164    * - @ref I2C_M_TEN,
0165    * - @ref I2C_M_RD,
0166    * - @ref I2C_M_STOP,
0167    * - @ref I2C_M_NOSTART,
0168    * - @ref I2C_M_REV_DIR_ADDR,
0169    * - @ref I2C_M_IGNORE_NAK,
0170    * - @ref I2C_M_NO_RD_ACK, and
0171    * - @ref I2C_M_RECV_LEN.
0172    */
0173   uint16_t flags;
0174 
0175   /**
0176    * @brief The message data length in bytes.
0177    */
0178   uint16_t len;
0179 
0180   /**
0181    * @brief Pointer to the message data.
0182    */
0183   uint8_t *buf;
0184 };
0185 
0186 /**
0187  * @name I2C Controller Functionality
0188  *
0189  * @{
0190  */
0191 
0192 #define I2C_FUNC_I2C 0x00000001
0193 #define I2C_FUNC_10BIT_ADDR 0x00000002
0194 #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004
0195 #define I2C_FUNC_SMBUS_PEC 0x00000008
0196 #define I2C_FUNC_NOSTART 0x00000010
0197 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000
0198 #define I2C_FUNC_SMBUS_QUICK 0x00010000
0199 #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
0200 #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
0201 #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
0202 #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
0203 #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
0204 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
0205 #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
0206 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
0207 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
0208 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000
0209 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000
0210 
0211 #define I2C_FUNC_SMBUS_BYTE \
0212   (I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE)
0213 
0214 #define I2C_FUNC_SMBUS_BYTE_DATA \
0215   (I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
0216 
0217 #define I2C_FUNC_SMBUS_WORD_DATA \
0218   (I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA)
0219 
0220 #define I2C_FUNC_SMBUS_BLOCK_DATA \
0221   (I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
0222 
0223 #define I2C_FUNC_SMBUS_I2C_BLOCK \
0224   (I2C_FUNC_SMBUS_READ_I2C_BLOCK | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
0225 
0226 #define I2C_FUNC_SMBUS_EMUL \
0227   (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA \
0228     | I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_PROC_CALL \
0229     | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK \
0230     | I2C_FUNC_SMBUS_PEC)
0231 
0232 /** @} */
0233 
0234 /**
0235  * @brief Maximum SMBus data block count.
0236  */
0237 #define I2C_SMBUS_BLOCK_MAX 32
0238 
0239 /**
0240  * @brief SMBus data.
0241  */
0242 union i2c_smbus_data {
0243   uint8_t byte;
0244   uint16_t word;
0245   uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
0246 };
0247 
0248 /**
0249  * @name SMBus Transfer Read and Write Markers
0250  *
0251  * @{
0252  */
0253 
0254 #define I2C_SMBUS_READ 1
0255 
0256 #define I2C_SMBUS_WRITE 0
0257 
0258 /** @} */
0259 
0260 /**
0261  * @name SMBus Transaction Types
0262  *
0263  * @{
0264  */
0265 
0266 #define I2C_SMBUS_QUICK 0
0267 
0268 #define I2C_SMBUS_BYTE 1
0269 
0270 #define I2C_SMBUS_BYTE_DATA 2
0271 
0272 #define I2C_SMBUS_WORD_DATA 3
0273 
0274 #define I2C_SMBUS_PROC_CALL 4
0275 
0276 #define I2C_SMBUS_BLOCK_DATA 5
0277 
0278 #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
0279 
0280 #define I2C_SMBUS_BLOCK_PROC_CALL 7
0281 
0282 #define I2C_SMBUS_I2C_BLOCK_DATA 8
0283 
0284 /** @} */
0285 
0286 /** @} */
0287 
0288 #endif /* _UAPI_LINUX_I2C_H */