1 /**
2  * @file    i2c_revb.h
3  * @brief   Inter-integrated circuit (I2C_REVB) communications interface driver.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 #ifndef LIBRARIES_PERIPHDRIVERS_SOURCE_I2C_I2C_REVB_H_
27 #define LIBRARIES_PERIPHDRIVERS_SOURCE_I2C_I2C_REVB_H_
28 
29 #include <stdint.h>
30 #include "i2c.h"
31 #include "i2c_reva.h"
32 #include "i2c_revb_regs.h"
33 #include "mxc_sys.h"
34 
35 /* **** Definitions **** */
36 #define MXC_I2C_REVB_MAX_ADDR_WIDTH 0x7F
37 #define MXC_I2C_REVB_STD_MODE 100000
38 #define MXC_I2C_REVB_FAST_SPEED 400000
39 #define MXC_I2C_REVB_FASTPLUS_SPEED 1000000
40 #define MXC_I2C_REVB_HS_MODE 3400000
41 
42 #define MXC_I2C_REVB_INTFL0_MASK 0x00FFFFFF
43 #define MXC_I2C_REVB_INTFL1_MASK 0x00000007
44 
45 #define MXC_I2C_REVB_MAX_FIFO_TRANSACTION 256
46 
47 #define MXC_I2C_REVB_ERROR                                            \
48     (MXC_F_I2C_REVB_INT_FL0_ARBERI | MXC_F_I2C_REVB_INT_FL0_TOERI |   \
49      MXC_F_I2C_REVB_INT_FL0_ADRERI | MXC_F_I2C_REVB_INT_FL0_DATAERI | \
50      MXC_F_I2C_REVB_INT_FL0_DNRERI | MXC_F_I2C_REVB_INT_FL0_STRTERI | \
51      MXC_F_I2C_REVB_INT_FL0_STOPERI)
52 
53 typedef struct _i2c_revb_req_t mxc_i2c_revb_req_t;
54 typedef int (*mxc_i2c_revb_getAck_t)(mxc_i2c_revb_regs_t *i2c, unsigned char byte);
55 typedef void (*mxc_i2c_revb_complete_cb_t)(mxc_i2c_revb_req_t *req, int result);
56 typedef void (*mxc_i2c_revb_dma_complete_cb_t)(int len, int result);
57 struct _i2c_revb_req_t {
58     mxc_i2c_revb_regs_t *i2c;
59     unsigned int addr;
60     unsigned char *tx_buf;
61     unsigned int tx_len;
62     unsigned char *rx_buf;
63     unsigned int rx_len;
64     int restart;
65     mxc_i2c_revb_complete_cb_t callback;
66 };
67 typedef enum {
68     MXC_I2C_REVB_EVT_MASTER_WR,
69     MXC_I2C_REVB_EVT_MASTER_RD,
70     MXC_I2C_REVB_EVT_RX_THRESH,
71     MXC_I2C_REVB_EVT_TX_THRESH,
72     MXC_I2C_REVB_EVT_TRANS_COMP,
73     MXC_I2C_REVB_EVT_UNDERFLOW,
74     MXC_I2C_REVB_EVT_OVERFLOW,
75 } mxc_i2c_revb_slave_event_t;
76 typedef int (*mxc_i2c_revb_slave_handler_t)(mxc_i2c_revb_regs_t *i2c,
77                                             mxc_i2c_revb_slave_event_t event, void *data);
78 /* **** Variable Declaration **** */
79 
80 /* **** Function Prototypes **** */
81 
82 /* ************************************************************************* */
83 /* Control/Configuration functions                                           */
84 /* ************************************************************************* */
85 int MXC_I2C_RevB_Init(mxc_i2c_revb_regs_t *i2c, int masterMode, unsigned int slaveAddr);
86 
87 int MXC_I2C_RevB_SetSlaveAddr(mxc_i2c_revb_regs_t *i2c, unsigned int slaveAddr, int idx);
88 int MXC_I2C_RevB_Shutdown(mxc_i2c_revb_regs_t *i2c);
89 int MXC_I2C_RevB_SetFrequency(mxc_i2c_revb_regs_t *i2c, unsigned int hz);
90 unsigned int MXC_I2C_RevB_GetFrequency(mxc_i2c_revb_regs_t *i2c);
91 int MXC_I2C_RevB_ReadyForSleep(mxc_i2c_revb_regs_t *i2c);
92 int MXC_I2C_RevB_SetClockStretching(mxc_i2c_revb_regs_t *i2c, int enable);
93 int MXC_I2C_RevB_GetClockStretching(mxc_i2c_revb_regs_t *i2c);
94 
95 /* ************************************************************************* */
96 /* Low-level functions                                                       */
97 /* ************************************************************************* */
98 int MXC_I2C_RevB_Start(mxc_i2c_revb_regs_t *i2c);
99 int MXC_I2C_RevB_Stop(mxc_i2c_revb_regs_t *i2c);
100 int MXC_I2C_RevB_WriteByte(mxc_i2c_revb_regs_t *i2c, unsigned char byte);
101 int MXC_I2C_RevB_ReadByte(mxc_i2c_revb_regs_t *i2c, unsigned char *byte, int ack);
102 int MXC_I2C_RevB_ReadByteInteractive(mxc_i2c_revb_regs_t *i2c, unsigned char *byte,
103                                      mxc_i2c_revb_getAck_t getAck);
104 int MXC_I2C_RevB_Write(mxc_i2c_revb_regs_t *i2c, unsigned char *bytes, unsigned int *len);
105 int MXC_I2C_RevB_Read(mxc_i2c_revb_regs_t *i2c, unsigned char *bytes, unsigned int *len, int ack);
106 int MXC_I2C_RevB_ReadRXFIFO(mxc_i2c_revb_regs_t *i2c, volatile unsigned char *bytes,
107                             unsigned int len);
108 int MXC_I2C_RevB_ReadRXFIFODMA(mxc_i2c_revb_regs_t *i2c, unsigned char *bytes, unsigned int len,
109                                mxc_dma_regs_t *dma);
110 int MXC_I2C_RevB_GetRXFIFOAvailable(mxc_i2c_revb_regs_t *i2c);
111 int MXC_I2C_RevB_WriteTXFIFO(mxc_i2c_revb_regs_t *i2c, volatile unsigned char *bytes,
112                              unsigned int len);
113 int MXC_I2C_RevB_WriteTXFIFODMA(mxc_i2c_revb_regs_t *i2c, unsigned char *bytes, unsigned int len,
114                                 mxc_dma_regs_t *dma);
115 int MXC_I2C_RevB_GetTXFIFOAvailable(mxc_i2c_revb_regs_t *i2c);
116 void MXC_I2C_RevB_ClearRXFIFO(mxc_i2c_revb_regs_t *i2c);
117 void MXC_I2C_RevB_ClearTXFIFO(mxc_i2c_revb_regs_t *i2c);
118 int MXC_I2C_RevB_GetFlags(mxc_i2c_revb_regs_t *i2c, unsigned int *flags0, unsigned int *flags1);
119 void MXC_I2C_RevB_ClearFlags(mxc_i2c_revb_regs_t *i2c, unsigned int flags0, unsigned int flags1);
120 void MXC_I2C_RevB_EnableInt(mxc_i2c_revb_regs_t *i2c, unsigned int flags0, unsigned int flags1);
121 void MXC_I2C_RevB_DisableInt(mxc_i2c_revb_regs_t *i2c, unsigned int flags0, unsigned int flags1);
122 void MXC_I2C_RevB_EnablePreload(mxc_i2c_revb_regs_t *i2c);
123 void MXC_I2C_RevB_DisablePreload(mxc_i2c_revb_regs_t *i2c);
124 void MXC_I2C_RevB_EnableGeneralCall(mxc_i2c_revb_regs_t *i2c);
125 void MXC_I2C_RevB_DisableGeneralCall(mxc_i2c_revb_regs_t *i2c);
126 void MXC_I2C_RevB_SetTimeout(mxc_i2c_revb_regs_t *i2c, unsigned int timeout);
127 unsigned int MXC_I2C_RevB_GetTimeout(mxc_i2c_revb_regs_t *i2c);
128 int MXC_I2C_RevB_Recover(mxc_i2c_revb_regs_t *i2c, unsigned int retries);
129 
130 /* ************************************************************************* */
131 /* Transaction level functions                                               */
132 /* ************************************************************************* */
133 int MXC_I2C_RevB_MasterTransaction(mxc_i2c_revb_req_t *req);
134 int MXC_I2C_RevB_MasterTransactionAsync(mxc_i2c_revb_req_t *req);
135 int MXC_I2C_RevB_MasterTransactionDMA(mxc_i2c_revb_req_t *req, mxc_dma_regs_t *dma);
136 int MXC_I2C_RevB_SlaveTransaction(mxc_i2c_revb_regs_t *i2c, mxc_i2c_revb_slave_handler_t callback,
137                                   uint32_t interruptCheck);
138 int MXC_I2C_RevB_SlaveTransactionAsync(mxc_i2c_revb_regs_t *i2c,
139                                        mxc_i2c_revb_slave_handler_t callback,
140                                        uint32_t interruptCheck);
141 int MXC_I2C_RevB_SetRXThreshold(mxc_i2c_revb_regs_t *i2c, unsigned int numBytes);
142 unsigned int MXC_I2C_RevB_GetRXThreshold(mxc_i2c_revb_regs_t *i2c);
143 int MXC_I2C_RevB_SetTXThreshold(mxc_i2c_revb_regs_t *i2c, unsigned int numBytes);
144 unsigned int MXC_I2C_RevB_GetTXThreshold(mxc_i2c_revb_regs_t *i2c);
145 void MXC_I2C_RevB_AsyncCallback(mxc_i2c_revb_regs_t *i2c, int retVal);
146 void MXC_I2C_RevB_AsyncStop(mxc_i2c_revb_regs_t *i2c);
147 void MXC_I2C_RevB_AbortAsync(mxc_i2c_revb_regs_t *i2c);
148 void MXC_I2C_RevB_MasterAsyncHandler(int i2cNum);
149 unsigned int MXC_I2C_RevB_SlaveAsyncHandler(mxc_i2c_revb_regs_t *i2c,
150                                             mxc_i2c_revb_slave_handler_t callback,
151                                             unsigned int interruptEnables, int *retVal);
152 void MXC_I2C_RevB_AsyncHandler(mxc_i2c_revb_regs_t *i2c, uint32_t interruptCheck);
153 void MXC_I2C_RevB_DMACallback(int ch, int error);
154 
155 #endif // LIBRARIES_PERIPHDRIVERS_SOURCE_I2C_I2C_REVB_H_
156