3 #include "ddk750_chip.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(unsigned char bus_speed_mode)
15 /* Enable GPIO 30 & 31 as IIC clock & data */
16 value = peek32(GPIO_MUX);
18 value |= (GPIO_MUX_30 | GPIO_MUX_31);
19 poke32(GPIO_MUX, value);
22 * Enable Hardware I2C power.
23 * TODO: Check if we need to enable GPIO power?
27 /* Enable the I2C Controller and set the bus speed mode */
28 value = peek32(I2C_CTRL) & ~(I2C_CTRL_MODE | I2C_CTRL_EN);
30 value |= I2C_CTRL_MODE;
32 poke32(I2C_CTRL, value);
37 void sm750_hw_i2c_close(void)
41 /* Disable I2C controller */
42 value = peek32(I2C_CTRL) & ~I2C_CTRL_EN;
43 poke32(I2C_CTRL, value);
45 /* Disable I2C Power */
48 /* Set GPIO 30 & 31 back as GPIO pins */
49 value = peek32(GPIO_MUX);
50 value &= ~GPIO_MUX_30;
51 value &= ~GPIO_MUX_31;
52 poke32(GPIO_MUX, value);
55 static long hw_i2c_wait_tx_done(void)
59 /* Wait until the transfer is completed. */
60 timeout = HWI2C_WAIT_TIMEOUT;
61 while (!(peek32(I2C_STATUS) & I2C_STATUS_TX) && (timeout != 0))
71 * This function writes data to the i2c slave device registers.
74 * addr - i2c Slave device address
75 * length - Total number of bytes to be written to the device
76 * buf - The buffer that contains the data to be written to the
80 * Total number of bytes those are actually written.
82 static unsigned int hw_i2c_write_data(unsigned char addr,
86 unsigned char count, i;
87 unsigned int total_bytes = 0;
89 /* Set the Device Address */
90 poke32(I2C_SLAVE_ADDRESS, addr & ~0x01);
95 * Only 16 byte can be accessed per i2c start instruction.
99 * Reset I2C by writing 0 to I2C_RESET register to
100 * clear the previous status.
102 poke32(I2C_RESET, 0);
104 /* Set the number of bytes to be written */
105 if (length < MAX_HWI2C_FIFO)
108 count = MAX_HWI2C_FIFO - 1;
109 poke32(I2C_BYTE_COUNT, count);
111 /* Move the data to the I2C data register */
112 for (i = 0; i <= count; i++)
113 poke32(I2C_DATA0 + i, *buf++);
116 poke32(I2C_CTRL, peek32(I2C_CTRL) | I2C_CTRL_CTRL);
118 /* Wait until the transfer is completed. */
119 if (hw_i2c_wait_tx_done() != 0)
122 /* Subtract length */
123 length -= (count + 1);
125 /* Total byte written */
126 total_bytes += (count + 1);
128 } while (length > 0);
134 * This function reads data from the slave device and stores them
135 * in the given buffer
138 * addr - i2c Slave device address
139 * length - Total number of bytes to be read
140 * buf - Pointer to a buffer to be filled with the data read
141 * from the slave device. It has to be the same size as the
142 * length to make sure that it can keep all the data read.
145 * Total number of actual bytes read from the slave device
147 static unsigned int hw_i2c_read_data(unsigned char addr,
151 unsigned char count, i;
152 unsigned int total_bytes = 0;
154 /* Set the Device Address */
155 poke32(I2C_SLAVE_ADDRESS, addr | 0x01);
158 * Read data and save them to the buffer.
160 * Only 16 byte can be accessed per i2c start instruction.
164 * Reset I2C by writing 0 to I2C_RESET register to
165 * clear all the status.
167 poke32(I2C_RESET, 0);
169 /* Set the number of bytes to be read */
170 if (length <= MAX_HWI2C_FIFO)
173 count = MAX_HWI2C_FIFO - 1;
174 poke32(I2C_BYTE_COUNT, count);
177 poke32(I2C_CTRL, peek32(I2C_CTRL) | I2C_CTRL_CTRL);
179 /* Wait until transaction done. */
180 if (hw_i2c_wait_tx_done() != 0)
183 /* Save the data to the given buffer */
184 for (i = 0; i <= count; i++)
185 *buf++ = peek32(I2C_DATA0 + i);
187 /* Subtract length by 16 */
188 length -= (count + 1);
190 /* Number of bytes read. */
191 total_bytes += (count + 1);
193 } while (length > 0);
199 * This function reads the slave device's register
202 * deviceAddress - i2c Slave device address which register
204 * registerIndex - Slave device's register to be read
209 unsigned char sm750_hw_i2c_read_reg(unsigned char addr, unsigned char reg)
211 unsigned char value = 0xFF;
213 if (hw_i2c_write_data(addr, 1, ®) == 1)
214 hw_i2c_read_data(addr, 1, &value);
220 * This function writes a value to the slave device's register
223 * deviceAddress - i2c Slave device address which register
225 * registerIndex - Slave device's register to be written
226 * data - Data to be written to the register
232 int sm750_hw_i2c_write_reg(unsigned char addr,
236 unsigned char value[2];
240 if (hw_i2c_write_data(addr, 2, value) == 2)