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 |= (GPIO_MUX_30 | GPIO_MUX_31);
21 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(
89 unsigned char count, i;
90 unsigned int total_bytes = 0;
92 /* Set the Device Address */
93 POKE32(I2C_SLAVE_ADDRESS, addr & ~0x01);
97 * Only 16 byte can be accessed per i2c start instruction.
101 * Reset I2C by writing 0 to I2C_RESET register to
102 * clear the previous status.
104 POKE32(I2C_RESET, 0);
106 /* Set the number of bytes to be written */
107 if (length < MAX_HWI2C_FIFO)
110 count = MAX_HWI2C_FIFO - 1;
111 POKE32(I2C_BYTE_COUNT, count);
113 /* Move the data to the I2C data register */
114 for (i = 0; i <= count; i++)
115 POKE32(I2C_DATA0 + i, *buf++);
118 POKE32(I2C_CTRL, PEEK32(I2C_CTRL) | I2C_CTRL_CTRL);
120 /* Wait until the transfer is completed. */
121 if (hw_i2c_wait_tx_done() != 0)
124 /* Subtract length */
125 length -= (count + 1);
127 /* Total byte written */
128 total_bytes += (count + 1);
130 } while (length > 0);
136 * This function reads data from the slave device and stores them
137 * in the given buffer
140 * addr - i2c Slave device address
141 * length - Total number of bytes to be read
142 * buf - Pointer to a buffer to be filled with the data read
143 * from the slave device. It has to be the same size as the
144 * length to make sure that it can keep all the data read.
147 * Total number of actual bytes read from the slave device
149 static unsigned int hw_i2c_read_data(
155 unsigned char count, i;
156 unsigned int total_bytes = 0;
158 /* Set the Device Address */
159 POKE32(I2C_SLAVE_ADDRESS, addr | 0x01);
161 /* Read data and save them to the buffer.
163 * Only 16 byte can be accessed per i2c start instruction.
167 * Reset I2C by writing 0 to I2C_RESET register to
168 * clear all the status.
170 POKE32(I2C_RESET, 0);
172 /* Set the number of bytes to be read */
173 if (length <= MAX_HWI2C_FIFO)
176 count = MAX_HWI2C_FIFO - 1;
177 POKE32(I2C_BYTE_COUNT, count);
180 POKE32(I2C_CTRL, PEEK32(I2C_CTRL) | I2C_CTRL_CTRL);
182 /* Wait until transaction done. */
183 if (hw_i2c_wait_tx_done() != 0)
186 /* Save the data to the given buffer */
187 for (i = 0; i <= count; i++)
188 *buf++ = PEEK32(I2C_DATA0 + i);
190 /* Subtract length by 16 */
191 length -= (count + 1);
193 /* Number of bytes read. */
194 total_bytes += (count + 1);
196 } while (length > 0);
202 * This function reads the slave device's register
205 * deviceAddress - i2c Slave device address which register
207 * registerIndex - Slave device's register to be read
212 unsigned char sm750_hw_i2c_read_reg(
217 unsigned char value = (0xFF);
219 if (hw_i2c_write_data(addr, 1, ®) == 1)
220 hw_i2c_read_data(addr, 1, &value);
226 * This function writes a value to the slave device's register
229 * deviceAddress - i2c Slave device address which register
231 * registerIndex - Slave device's register to be written
232 * data - Data to be written to the register
238 int sm750_hw_i2c_write_reg(
244 unsigned char value[2];
248 if (hw_i2c_write_data(addr, 2, value) == 2)