1 // SPDX-License-Identifier: GPL-2.0
4 #include "ddk750_chip.h"
5 #include "ddk750_reg.h"
6 #include "ddk750_hwi2c.h"
7 #include "ddk750_power.h"
9 #define MAX_HWI2C_FIFO 16
10 #define HWI2C_WAIT_TIMEOUT 0xF0000
12 int sm750_hw_i2c_init(unsigned char bus_speed_mode)
16 /* Enable GPIO 30 & 31 as IIC clock & data */
17 value = peek32(GPIO_MUX);
19 value |= (GPIO_MUX_30 | GPIO_MUX_31);
20 poke32(GPIO_MUX, value);
23 * Enable Hardware I2C power.
24 * TODO: Check if we need to enable GPIO power?
28 /* Enable the I2C Controller and set the bus speed mode */
29 value = peek32(I2C_CTRL) & ~(I2C_CTRL_MODE | I2C_CTRL_EN);
31 value |= I2C_CTRL_MODE;
33 poke32(I2C_CTRL, value);
38 void sm750_hw_i2c_close(void)
42 /* Disable I2C controller */
43 value = peek32(I2C_CTRL) & ~I2C_CTRL_EN;
44 poke32(I2C_CTRL, value);
46 /* Disable I2C Power */
49 /* Set GPIO 30 & 31 back as GPIO pins */
50 value = peek32(GPIO_MUX);
51 value &= ~GPIO_MUX_30;
52 value &= ~GPIO_MUX_31;
53 poke32(GPIO_MUX, value);
56 static long hw_i2c_wait_tx_done(void)
60 /* Wait until the transfer is completed. */
61 timeout = HWI2C_WAIT_TIMEOUT;
62 while (!(peek32(I2C_STATUS) & I2C_STATUS_TX) && (timeout != 0))
72 * This function writes data to the i2c slave device registers.
75 * addr - i2c Slave device address
76 * length - Total number of bytes to be written to the device
77 * buf - The buffer that contains the data to be written to the
81 * Total number of bytes those are actually written.
83 static unsigned int hw_i2c_write_data(unsigned char addr,
87 unsigned char count, i;
88 unsigned int total_bytes = 0;
90 /* Set the Device Address */
91 poke32(I2C_SLAVE_ADDRESS, addr & ~0x01);
96 * Only 16 byte can be accessed per i2c start instruction.
100 * Reset I2C by writing 0 to I2C_RESET register to
101 * clear the previous status.
103 poke32(I2C_RESET, 0);
105 /* Set the number of bytes to be written */
106 if (length < MAX_HWI2C_FIFO)
109 count = MAX_HWI2C_FIFO - 1;
110 poke32(I2C_BYTE_COUNT, count);
112 /* Move the data to the I2C data register */
113 for (i = 0; i <= count; i++)
114 poke32(I2C_DATA0 + i, *buf++);
117 poke32(I2C_CTRL, peek32(I2C_CTRL) | I2C_CTRL_CTRL);
119 /* Wait until the transfer is completed. */
120 if (hw_i2c_wait_tx_done() != 0)
123 /* Subtract length */
124 length -= (count + 1);
126 /* Total byte written */
127 total_bytes += (count + 1);
129 } while (length > 0);
135 * This function reads data from the slave device and stores them
136 * in the given buffer
139 * addr - i2c Slave device address
140 * length - Total number of bytes to be read
141 * buf - Pointer to a buffer to be filled with the data read
142 * from the slave device. It has to be the same size as the
143 * length to make sure that it can keep all the data read.
146 * Total number of actual bytes read from the slave device
148 static unsigned int hw_i2c_read_data(unsigned char addr,
152 unsigned char count, i;
153 unsigned int total_bytes = 0;
155 /* Set the Device Address */
156 poke32(I2C_SLAVE_ADDRESS, addr | 0x01);
159 * Read data and save them to the buffer.
161 * Only 16 byte can be accessed per i2c start instruction.
165 * Reset I2C by writing 0 to I2C_RESET register to
166 * clear all the status.
168 poke32(I2C_RESET, 0);
170 /* Set the number of bytes to be read */
171 if (length <= MAX_HWI2C_FIFO)
174 count = MAX_HWI2C_FIFO - 1;
175 poke32(I2C_BYTE_COUNT, count);
178 poke32(I2C_CTRL, peek32(I2C_CTRL) | I2C_CTRL_CTRL);
180 /* Wait until transaction done. */
181 if (hw_i2c_wait_tx_done() != 0)
184 /* Save the data to the given buffer */
185 for (i = 0; i <= count; i++)
186 *buf++ = peek32(I2C_DATA0 + i);
188 /* Subtract length by 16 */
189 length -= (count + 1);
191 /* Number of bytes read. */
192 total_bytes += (count + 1);
194 } while (length > 0);
200 * This function reads the slave device's register
203 * deviceAddress - i2c Slave device address which register
205 * registerIndex - Slave device's register to be read
210 unsigned char sm750_hw_i2c_read_reg(unsigned char addr, unsigned char reg)
212 unsigned char value = 0xFF;
214 if (hw_i2c_write_data(addr, 1, ®) == 1)
215 hw_i2c_read_data(addr, 1, &value);
221 * This function writes a value to the slave device's register
224 * deviceAddress - i2c Slave device address which register
226 * registerIndex - Slave device's register to be written
227 * data - Data to be written to the register
233 int sm750_hw_i2c_write_reg(unsigned char addr,
237 unsigned char value[2];
241 if (hw_i2c_write_data(addr, 2, value) == 2)