OpenBIC
OpenSource Bridge-IC
hal_i2c.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef HAL_I2C_H
18#define HAL_I2C_H
19
20#include <drivers/i2c.h>
21#include <drivers/i2c/slave/ipmb.h>
22
23#if defined(CONFIG_I2C_ASPEED)
24#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c0), okay)
25#define DEV_I2C_0
26#endif
27
28#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1), okay)
29#define DEV_I2C_1
30#endif
31
32#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c2), okay)
33#define DEV_I2C_2
34#endif
35
36#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c3), okay)
37#define DEV_I2C_3
38#endif
39
40#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c4), okay)
41#define DEV_I2C_4
42#endif
43
44#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c5), okay)
45#define DEV_I2C_5
46#endif
47
48#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c6), okay)
49#define DEV_I2C_6
50#endif
51
52#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c7), okay)
53#define DEV_I2C_7
54#endif
55
56#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c8), okay)
57#define DEV_I2C_8
58#endif
59
60#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c9), okay)
61#define DEV_I2C_9
62#endif
63
64#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c10), okay)
65#define DEV_I2C_10
66#endif
67
68#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c11), okay)
69#define DEV_I2C_11
70#endif
71
72#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c12), okay)
73#define DEV_I2C_12
74#endif
75
76#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c13), okay)
77#define DEV_I2C_13
78#endif
79
80#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c14), okay)
81#define DEV_I2C_14
82#endif
83
84#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c15), okay)
85#define DEV_I2C_15
86#endif
87
88#elif (CONFIG_I2C_NPCM4XX)
89#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1a), okay)
90#define DEV_I2C_0
91#endif
92
93#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c1b), okay)
94#define DEV_I2C_0
95#endif
96
97#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c2a), okay)
98#define DEV_I2C_1
99#endif
100
101#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c3a), okay)
102#define DEV_I2C_2
103#endif
104
105#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c4a), okay)
106#define DEV_I2C_3
107#endif
108
109#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c4b), okay)
110#define DEV_I2C_3
111#endif
112
113#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c5a), okay)
114#define DEV_I2C_4
115#endif
116
117#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c6a), okay)
118#define DEV_I2C_5
119#endif
120
121#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c6b), okay)
122#define DEV_I2C_5
123#endif
124
125#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c7a), okay)
126#define DEV_I2C_6
127#endif
128
129#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c8a), okay)
130#define DEV_I2C_7
131#endif
132
133#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c9a), okay)
134#define DEV_I2C_8
135#endif
136
137#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c10a), okay)
138#define DEV_I2C_9
139#endif
140
141#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c11a), okay)
142#define DEV_I2C_10
143#endif
144
145#if DT_NODE_HAS_STATUS(DT_NODELABEL(i2c12a), okay)
146#define DEV_I2C_11
147#endif
148
149#else /* defined(CONFIG_GPIO_ASPEED) */
150#endif /* defined(CONFIG_GPIO_ASPEED) */
151
152#define DEV_I2C(n) DEV_I2C_##n
153
154#define I2C_BUFF_SIZE 256
155#define MUTEX_LOCK_ENABLE true
156#define MUTEX_LOCK_DISENABLE false
157
161};
162
163typedef struct _I2C_MSG_ {
164 uint8_t bus;
165 uint8_t target_addr;
166 uint8_t rx_len;
167 uint8_t tx_len;
169 struct k_mutex lock;
171
172int i2c_freq_set(uint8_t i2c_bus, uint8_t i2c_speed_mode, uint8_t en_slave);
173int i2c_addr_set(uint8_t i2c_bus, uint8_t i2c_addr);
174int i2c_master_read(I2C_MSG *msg, uint8_t retry);
175int i2c_master_read_without_mutex(I2C_MSG *msg, uint8_t retry);
176int i2c_master_write(I2C_MSG *msg, uint8_t retry);
177int i2c_master_write_without_mutex(I2C_MSG *msg, uint8_t retry);
178void i2c_scan(uint8_t bus, uint8_t *target_addr, uint8_t *target_addr_len);
179void util_init_I2C(void);
180int check_i2c_bus_valid(uint8_t bus);
181
182#endif
int i2c_master_write(I2C_MSG *msg, uint8_t retry)
Definition: hal_i2c.c:187
int i2c_master_read(I2C_MSG *msg, uint8_t retry)
Definition: hal_i2c.c:114
int i2c_freq_set(uint8_t i2c_bus, uint8_t i2c_speed_mode, uint8_t en_slave)
Definition: hal_i2c.c:54
I2C_TRANSFER_TYPE
Definition: hal_i2c.h:158
@ I2C_READ
Definition: hal_i2c.h:159
@ I2C_WRITE
Definition: hal_i2c.h:160
void i2c_scan(uint8_t bus, uint8_t *target_addr, uint8_t *target_addr_len)
Definition: hal_i2c.c:344
int i2c_master_read_without_mutex(I2C_MSG *msg, uint8_t retry)
Definition: hal_i2c.c:240
struct _I2C_MSG_ I2C_MSG
void util_init_I2C(void)
Definition: hal_i2c.c:378
int check_i2c_bus_valid(uint8_t bus)
Definition: hal_i2c.c:482
int i2c_addr_set(uint8_t i2c_bus, uint8_t i2c_addr)
Definition: hal_i2c.c:84
int i2c_master_write_without_mutex(I2C_MSG *msg, uint8_t retry)
Definition: hal_i2c.c:302
#define I2C_BUFF_SIZE
Definition: hal_i2c.h:154
Definition: hal_i2c.h:163
struct k_mutex lock
Definition: hal_i2c.h:169
uint8_t tx_len
Definition: hal_i2c.h:167
uint8_t data[I2C_BUFF_SIZE]
Definition: hal_i2c.h:168
uint8_t rx_len
Definition: hal_i2c.h:166
uint8_t target_addr
Definition: hal_i2c.h:165
uint8_t bus
Definition: hal_i2c.h:164