1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef I2C_CORE_H 10 #define I2C_CORE_H 11 12 #include "hdf_base.h" 13 #include "i2c_if.h" 14 #include "osal_mutex.h" 15 #include "platform_core.h" 16 17 #ifdef __cplusplus 18 #if __cplusplus 19 extern "C" { 20 #endif 21 #endif /* __cplusplus */ 22 23 #define I2C_BUS_MAX 16 24 25 struct I2cCntlr; 26 struct I2cMethod; 27 struct I2cLockMethod; 28 29 struct I2cCntlr { 30 struct OsalMutex lock; 31 void *owner; 32 int16_t busId; 33 void *priv; 34 const struct I2cMethod *ops; 35 const struct I2cLockMethod *lockOps; 36 }; 37 38 struct I2cMethod { 39 /** 40 * @brief Execute one or more I2C messages, which is implemented by a specific vendor driver. 41 * 42 * @param cntlr Indicates the I2C controller device. 43 * @param msgs Indicates the {@link I2cMsg} message array. 44 * @param count Indicates the length of the message array. 45 * 46 * @return Returns the number of transferred message structures if the operation is successful; 47 * returns a negative value otherwise. 48 * @see I2cMsg 49 * @since 1.0 50 */ 51 int32_t (*transfer)(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count); 52 }; 53 54 struct I2cLockMethod { 55 /** 56 * @brief Get exclusive access to an I2C controller. 57 * 58 * @param cntlr Indicates the I2C controller to access. 59 * 60 * @return Returns 0 on success; returns a negative value otherwise. 61 * @since 1.0 62 */ 63 int32_t (*lock)(struct I2cCntlr *cntlr); 64 /** 65 * @brief Release exclusive access to an I2C controller. 66 * 67 * @param cntlr Indicates the I2C controller to release. 68 * 69 * @since 1.0 70 */ 71 void (*unlock)(struct I2cCntlr *cntlr); 72 }; 73 74 /** 75 * @brief Bind to a HdfDeviceObject, and do some necessary check 76 * 77 * @param cntlr Indicates the I2C controller device. 78 * @param device The HdfDeviceObject of this I2cCntlr. 79 * 80 * @return Returns 0 on success; returns a negative value otherwise. 81 * @since 1.0 82 */ 83 int32_t I2cCntlrAdd(struct I2cCntlr *cntlr); 84 85 /** 86 * @brief Create a new I2cCntlr struct, and bind it to a HdfDeviceObject. 87 * 88 * @param cntlr Indicates the I2C controller device. 89 * 90 * @since 1.0 91 */ 92 void I2cCntlrRemove(struct I2cCntlr *cntlr); 93 94 /** 95 * @brief Find and return an i2c controller by number, with ref count. 96 * 97 * @param number Indicates the I2C controller to get. 98 * 99 * @since 1.0 100 */ 101 struct I2cCntlr *I2cCntlrGet(int16_t number); 102 103 /** 104 * @brief Find and return an i2c controller by number, with ref count. 105 * 106 * @param number Indicates the I2C controller to get. 107 * 108 * @since 1.0 109 */ 110 void I2cCntlrPut(struct I2cCntlr *cntlr); 111 112 /** 113 * @brief Execute one or more I2C messages. 114 * 115 * @param cntlr Indicates the I2C controller device. 116 * @param msgs Indicates the {@link I2cMsg} message array. 117 * @param count Indicates the length of the message array. 118 * 119 * @return Returns the number of transferred message structures if the operation is successful; 120 * returns a negative value otherwise. 121 * @see I2cMsg 122 * @since 1.0 123 */ 124 int32_t I2cCntlrTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count); 125 126 #ifdef __cplusplus 127 #if __cplusplus 128 } 129 #endif 130 #endif /* __cplusplus */ 131 132 #endif /* I2C_CORE_H */ 133