1 /*
2  * Copyright (c) 2016 - 2021, Broadcom
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef I2C_H
8 #define I2C_H
9 
10 #include <stdint.h>
11 
12 #define I2C_SPEED_100KHz	100000
13 #define I2C_SPEED_400KHz	400000
14 #define I2C_SPEED_DEFAULT	I2C_SPEED_100KHz
15 
16 /*
17  * Function Name:    i2c_probe
18  *
19  * Description:
20  *	This function probes the I2C bus for the existence of the specified
21  *	device.
22  *
23  * Parameters:
24  *	bus_id  - I2C bus ID
25  *	devaddr - Device Address
26  *
27  * Return:
28  *	0 on success, or -1 on failure.
29  */
30 int i2c_probe(uint32_t bus_id, uint8_t devaddr);
31 
32 /*
33  * Function Name:    i2c_init
34  *
35  * Description:
36  *	This function initializes the SMBUS.
37  *
38  * Parameters:
39  *	bus_id - I2C bus ID
40  *	speed  - I2C bus speed in Hz
41  *
42  * Return:
43  *	0 on success, or -1 on failure.
44  */
45 int i2c_init(uint32_t bus_id, int speed);
46 
47 /*
48  * Function Name:    i2c_set_bus_speed
49  *
50  * Description:
51  *	This function configures the SMBUS speed
52  *
53  * Parameters:
54  *	bus_id - I2C bus ID
55  *	speed  - I2C bus speed in Hz
56  *
57  * Return:
58  *	0 on success, or -1 on failure.
59  */
60 int i2c_set_bus_speed(uint32_t bus_id, uint32_t speed);
61 
62 /*
63  * Function Name:    i2c_get_bus_speed
64  *
65  * Description:
66  *	This function returns the SMBUS speed.
67  *
68  * Parameters:
69  *	bus_id - I2C bus ID
70  *
71  * Return:
72  *	Bus speed in Hz, 0 on failure
73  */
74 uint32_t i2c_get_bus_speed(uint32_t bus_id);
75 
76 /*
77  * Function Name:    i2c_recv_byte
78  *
79  * Description:
80  *	This function reads I2C data from a device without specifying
81  *	a command regsiter.
82  *
83  * Parameters:
84  *	bus_id  - I2C bus ID
85  *	devaddr - Device Address
86  *	value   - Data Read
87  *
88  * Return:
89  *	0 on success, or -1 on failure.
90  */
91 int i2c_recv_byte(uint32_t bus_id, uint8_t devaddr, uint8_t *value);
92 
93 /*
94  * Function Name:    i2c_send_byte
95  *
96  * Description:
97  *	This function send I2C data to a device without specifying
98  *	a command regsiter.
99  *
100  * Parameters:
101  *	bus_id  - I2C bus ID
102  *	devaddr - Device Address
103  *	value   - Data Send
104  *
105  * Return:
106  *	0 on success, or -1 on failure.
107  */
108 int i2c_send_byte(uint32_t bus_id, uint8_t devaddr, uint8_t value);
109 
110 /*
111  * Function Name:    i2c_read
112  *
113  * Description:
114  *	This function reads I2C data from a device with a designated
115  *	command register
116  *
117  * Parameters:
118  *	bus_id  - I2C bus ID
119  *	devaddr - Device Address
120  *	addr    - Register Offset
121  *	alen    - Address Length, 1 for byte, 2 for word (not supported)
122  *	buffer  - Data Buffer
123  *	len     - Data Length in bytes
124  *
125  * Return:
126  *	0 on success, or -1 on failure.
127  */
128 int i2c_read(uint32_t bus_id,
129 	     uint8_t devaddr,
130 	     uint32_t addr,
131 	     int alen,
132 	     uint8_t *buffer,
133 	     int len);
134 
135 /*
136  * Function Name:    i2c_write
137  *
138  * Description:
139  *	This function write I2C data to a device with a designated
140  *	command register
141  *
142  * Parameters:
143  *	bus_id  - I2C bus ID
144  *	devaddr - Device Address
145  *	addr    - Register Offset
146  *	alen    - Address Length, 1 for byte, 2 for word (not supported)
147  *	buffer  - Data Buffer
148  *	len     - Data Length in bytes
149  *
150  * Return:
151  *	0 on success, or -1 on failure.
152  */
153 int i2c_write(uint32_t bus_id,
154 	      uint8_t devaddr,
155 	      uint32_t addr,
156 	      int alen,
157 	      uint8_t *buffer,
158 	      int len);
159 
160 
161 #endif /* I2C_H */
162