3 #include "ddk750_help.h"
4 #include "ddk750_reg.h"
5 #include "ddk750_hwi2c.h"
6 #include "ddk750_power.h"
8 #define MAX_HWI2C_FIFO 16
9 #define HWI2C_WAIT_TIMEOUT 0xF0000
11 int sm750_hw_i2c_init(
12 unsigned char bus_speed_mode
17 /* Enable GPIO 30 & 31 as IIC clock & data */
18 value = PEEK32(GPIO_MUX);
20 value = FIELD_SET(value, GPIO_MUX, 30, I2C) |
21 FIELD_SET(0, GPIO_MUX, 31, I2C);
22 POKE32(GPIO_MUX, value);
24 /* Enable Hardware I2C power.
25 TODO: Check if we need to enable GPIO power?
29 /* Enable the I2C Controller and set the bus speed mode */
30 value = PEEK32(I2C_CTRL);
31 if (bus_speed_mode == 0)
32 value = FIELD_SET(value, I2C_CTRL, MODE, STANDARD);
34 value = FIELD_SET(value, I2C_CTRL, MODE, FAST);
35 value = FIELD_SET(value, I2C_CTRL, EN, ENABLE);
36 POKE32(I2C_CTRL, value);
41 void sm750_hw_i2c_close(void)
45 /* Disable I2C controller */
46 value = PEEK32(I2C_CTRL);
47 value = FIELD_SET(value, I2C_CTRL, EN, DISABLE);
48 POKE32(I2C_CTRL, value);
50 /* Disable I2C Power */
53 /* Set GPIO 30 & 31 back as GPIO pins */
54 value = PEEK32(GPIO_MUX);
55 value = FIELD_SET(value, GPIO_MUX, 30, GPIO);
56 value = FIELD_SET(value, GPIO_MUX, 31, GPIO);
57 POKE32(GPIO_MUX, value);
60 static long hw_i2c_wait_tx_done(void)
64 /* Wait until the transfer is completed. */
65 timeout = HWI2C_WAIT_TIMEOUT;
66 while ((FIELD_GET(PEEK32(I2C_STATUS),
67 I2C_STATUS, TX) != I2C_STATUS_TX_COMPLETED) &&
78 * This function writes data to the i2c slave device registers.
81 * addr - i2c Slave device address
82 * length - Total number of bytes to be written to the device
83 * buf - The buffer that contains the data to be written to the
87 * Total number of bytes those are actually written.
89 static unsigned int hw_i2c_write_data(
95 unsigned char count, i;
96 unsigned int total_bytes = 0;
98 /* Set the Device Address */
99 POKE32(I2C_SLAVE_ADDRESS, addr & ~0x01);
103 * Only 16 byte can be accessed per i2c start instruction.
107 * Reset I2C by writing 0 to I2C_RESET register to
108 * clear the previous status.
110 POKE32(I2C_RESET, 0);
112 /* Set the number of bytes to be written */
113 if (length < MAX_HWI2C_FIFO)
116 count = MAX_HWI2C_FIFO - 1;
117 POKE32(I2C_BYTE_COUNT, count);
119 /* Move the data to the I2C data register */
120 for (i = 0; i <= count; i++)
121 POKE32(I2C_DATA0 + i, *buf++);
125 FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
127 /* Wait until the transfer is completed. */
128 if (hw_i2c_wait_tx_done() != 0)
131 /* Substract length */
132 length -= (count + 1);
134 /* Total byte written */
135 total_bytes += (count + 1);
137 } while (length > 0);
143 * This function reads data from the slave device and stores them
144 * in the given buffer
147 * addr - i2c Slave device address
148 * length - Total number of bytes to be read
149 * buf - Pointer to a buffer to be filled with the data read
150 * from the slave device. It has to be the same size as the
151 * length to make sure that it can keep all the data read.
154 * Total number of actual bytes read from the slave device
156 static unsigned int hw_i2c_read_data(
162 unsigned char count, i;
163 unsigned int total_bytes = 0;
165 /* Set the Device Address */
166 POKE32(I2C_SLAVE_ADDRESS, addr | 0x01);
168 /* Read data and save them to the buffer.
170 * Only 16 byte can be accessed per i2c start instruction.
174 * Reset I2C by writing 0 to I2C_RESET register to
175 * clear all the status.
177 POKE32(I2C_RESET, 0);
179 /* Set the number of bytes to be read */
180 if (length <= MAX_HWI2C_FIFO)
183 count = MAX_HWI2C_FIFO - 1;
184 POKE32(I2C_BYTE_COUNT, count);
188 FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
190 /* Wait until transaction done. */
191 if (hw_i2c_wait_tx_done() != 0)
194 /* Save the data to the given buffer */
195 for (i = 0; i <= count; i++)
196 *buf++ = PEEK32(I2C_DATA0 + i);
198 /* Substract length by 16 */
199 length -= (count + 1);
201 /* Number of bytes read. */
202 total_bytes += (count + 1);
204 } while (length > 0);
210 * This function reads the slave device's register
213 * deviceAddress - i2c Slave device address which register
215 * registerIndex - Slave device's register to be read
220 unsigned char sm750_hw_i2c_read_reg(
225 unsigned char value = (0xFF);
227 if (hw_i2c_write_data(addr, 1, ®) == 1)
228 hw_i2c_read_data(addr, 1, &value);
234 * This function writes a value to the slave device's register
237 * deviceAddress - i2c Slave device address which register
239 * registerIndex - Slave device's register to be written
240 * data - Data to be written to the register
246 int sm750_hw_i2c_write_reg(
252 unsigned char value[2];
256 if (hw_i2c_write_data(addr, 2, value) == 2)