staging: sm750fb: rename hwI2CInit to sm750_hw_i2c_init
[deliverable/linux.git] / drivers / staging / sm750fb / ddk750_hwi2c.c
CommitLineData
81dee67e
SM
1#define USE_HW_I2C
2#ifdef USE_HW_I2C
3#include "ddk750_help.h"
4#include "ddk750_reg.h"
5#include "ddk750_hwi2c.h"
6#include "ddk750_power.h"
7
8#define MAX_HWI2C_FIFO 16
9#define HWI2C_WAIT_TIMEOUT 0xF0000
10
11
19f70eae 12int sm750_hw_i2c_init(
78376535 13unsigned char busSpeedMode
81dee67e
SM
14)
15{
78376535 16 unsigned int value;
81dee67e 17
78376535 18 /* Enable GPIO 30 & 31 as IIC clock & data */
81dee67e
SM
19 value = PEEK32(GPIO_MUX);
20
78376535
JL
21 value = FIELD_SET(value, GPIO_MUX, 30, I2C) |
22 FIELD_SET(0, GPIO_MUX, 31, I2C);
81dee67e
SM
23 POKE32(GPIO_MUX, value);
24
78376535
JL
25 /* Enable Hardware I2C power.
26 TODO: Check if we need to enable GPIO power?
27 */
28 enableI2C(1);
29
30 /* Enable the I2C Controller and set the bus speed mode */
31 value = PEEK32(I2C_CTRL);
32 if (busSpeedMode == 0)
33 value = FIELD_SET(value, I2C_CTRL, MODE, STANDARD);
34 else
35 value = FIELD_SET(value, I2C_CTRL, MODE, FAST);
36 value = FIELD_SET(value, I2C_CTRL, EN, ENABLE);
37 POKE32(I2C_CTRL, value);
38
39 return 0;
81dee67e
SM
40}
41
42
43void hwI2CClose(void)
44{
78376535 45 unsigned int value;
81dee67e 46
78376535
JL
47 /* Disable I2C controller */
48 value = PEEK32(I2C_CTRL);
49 value = FIELD_SET(value, I2C_CTRL, EN, DISABLE);
50 POKE32(I2C_CTRL, value);
81dee67e 51
78376535
JL
52 /* Disable I2C Power */
53 enableI2C(0);
81dee67e 54
78376535
JL
55 /* Set GPIO 30 & 31 back as GPIO pins */
56 value = PEEK32(GPIO_MUX);
57 value = FIELD_SET(value, GPIO_MUX, 30, GPIO);
58 value = FIELD_SET(value, GPIO_MUX, 31, GPIO);
59 POKE32(GPIO_MUX, value);
81dee67e
SM
60}
61
62
d93abd15 63static long hwI2CWaitTXDone(void)
81dee67e 64{
78376535 65 unsigned int timeout;
81dee67e 66
78376535
JL
67 /* Wait until the transfer is completed. */
68 timeout = HWI2C_WAIT_TIMEOUT;
81dee67e 69 while ((FIELD_GET(PEEK32(I2C_STATUS), I2C_STATUS, TX) != I2C_STATUS_TX_COMPLETED) &&
78376535 70 (timeout != 0))
81dee67e
SM
71 timeout--;
72
73 if (timeout == 0)
78376535 74 return (-1);
81dee67e 75
78376535 76 return 0;
81dee67e
SM
77}
78
79
80
81/*
82 * This function writes data to the i2c slave device registers.
83 *
84 * Parameters:
85 * deviceAddress - i2c Slave device address
86 * length - Total number of bytes to be written to the device
87 * pBuffer - The buffer that contains the data to be written to the
88 * i2c device.
89 *
90 * Return Value:
91 * Total number of bytes those are actually written.
92 */
d93abd15 93static unsigned int hwI2CWriteData(
78376535
JL
94 unsigned char deviceAddress,
95 unsigned int length,
96 unsigned char *pBuffer
81dee67e
SM
97)
98{
78376535
JL
99 unsigned char count, i;
100 unsigned int totalBytes = 0;
81dee67e 101
78376535
JL
102 /* Set the Device Address */
103 POKE32(I2C_SLAVE_ADDRESS, deviceAddress & ~0x01);
81dee67e 104
78376535
JL
105 /* Write data.
106 * Note:
107 * Only 16 byte can be accessed per i2c start instruction.
108 */
259fef35 109 do {
78376535
JL
110 /* Reset I2C by writing 0 to I2C_RESET register to clear the previous status. */
111 POKE32(I2C_RESET, 0);
81dee67e 112
78376535
JL
113 /* Set the number of bytes to be written */
114 if (length < MAX_HWI2C_FIFO)
115 count = length - 1;
116 else
117 count = MAX_HWI2C_FIFO - 1;
118 POKE32(I2C_BYTE_COUNT, count);
81dee67e 119
78376535
JL
120 /* Move the data to the I2C data register */
121 for (i = 0; i <= count; i++)
122 POKE32(I2C_DATA0 + i, *pBuffer++);
81dee67e 123
78376535
JL
124 /* Start the I2C */
125 POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
81dee67e 126
78376535
JL
127 /* Wait until the transfer is completed. */
128 if (hwI2CWaitTXDone() != 0)
129 break;
81dee67e 130
78376535
JL
131 /* Substract length */
132 length -= (count + 1);
81dee67e 133
78376535
JL
134 /* Total byte written */
135 totalBytes += (count + 1);
81dee67e 136
78376535 137 } while (length > 0);
81dee67e 138
78376535 139 return totalBytes;
81dee67e
SM
140}
141
142
143
144
145/*
146 * This function reads data from the slave device and stores them
147 * in the given buffer
148 *
149 * Parameters:
150 * deviceAddress - i2c Slave device address
151 * length - Total number of bytes to be read
152 * pBuffer - Pointer to a buffer to be filled with the data read
153 * from the slave device. It has to be the same size as the
154 * length to make sure that it can keep all the data read.
155 *
156 * Return Value:
157 * Total number of actual bytes read from the slave device
158 */
d93abd15 159static unsigned int hwI2CReadData(
78376535
JL
160 unsigned char deviceAddress,
161 unsigned int length,
162 unsigned char *pBuffer
81dee67e
SM
163)
164{
78376535
JL
165 unsigned char count, i;
166 unsigned int totalBytes = 0;
81dee67e 167
78376535
JL
168 /* Set the Device Address */
169 POKE32(I2C_SLAVE_ADDRESS, deviceAddress | 0x01);
81dee67e 170
78376535
JL
171 /* Read data and save them to the buffer.
172 * Note:
173 * Only 16 byte can be accessed per i2c start instruction.
174 */
259fef35 175 do {
78376535
JL
176 /* Reset I2C by writing 0 to I2C_RESET register to clear all the status. */
177 POKE32(I2C_RESET, 0);
81dee67e 178
78376535
JL
179 /* Set the number of bytes to be read */
180 if (length <= MAX_HWI2C_FIFO)
181 count = length - 1;
182 else
183 count = MAX_HWI2C_FIFO - 1;
184 POKE32(I2C_BYTE_COUNT, count);
81dee67e 185
78376535
JL
186 /* Start the I2C */
187 POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
81dee67e 188
78376535
JL
189 /* Wait until transaction done. */
190 if (hwI2CWaitTXDone() != 0)
191 break;
81dee67e 192
78376535
JL
193 /* Save the data to the given buffer */
194 for (i = 0; i <= count; i++)
195 *pBuffer++ = PEEK32(I2C_DATA0 + i);
81dee67e 196
78376535
JL
197 /* Substract length by 16 */
198 length -= (count + 1);
81dee67e 199
78376535
JL
200 /* Number of bytes read. */
201 totalBytes += (count + 1);
81dee67e 202
78376535 203 } while (length > 0);
81dee67e 204
78376535 205 return totalBytes;
81dee67e
SM
206}
207
208
209
210
211/*
212 * This function reads the slave device's register
213 *
214 * Parameters:
215 * deviceAddress - i2c Slave device address which register
216 * to be read from
217 * registerIndex - Slave device's register to be read
218 *
219 * Return Value:
220 * Register value
221 */
222unsigned char hwI2CReadReg(
78376535
JL
223 unsigned char deviceAddress,
224 unsigned char registerIndex
81dee67e
SM
225)
226{
78376535 227 unsigned char value = (0xFF);
81dee67e 228
78376535
JL
229 if (hwI2CWriteData(deviceAddress, 1, &registerIndex) == 1)
230 hwI2CReadData(deviceAddress, 1, &value);
81dee67e 231
78376535 232 return value;
81dee67e
SM
233}
234
235
236
237
238
239/*
240 * This function writes a value to the slave device's register
241 *
242 * Parameters:
243 * deviceAddress - i2c Slave device address which register
244 * to be written
245 * registerIndex - Slave device's register to be written
246 * data - Data to be written to the register
247 *
248 * Result:
249 * 0 - Success
250 * -1 - Fail
251 */
252int hwI2CWriteReg(
78376535
JL
253 unsigned char deviceAddress,
254 unsigned char registerIndex,
255 unsigned char data
81dee67e
SM
256)
257{
78376535 258 unsigned char value[2];
81dee67e 259
78376535
JL
260 value[0] = registerIndex;
261 value[1] = data;
262 if (hwI2CWriteData(deviceAddress, 2, value) == 2)
263 return 0;
81dee67e 264
78376535 265 return (-1);
81dee67e
SM
266}
267
268
269#endif
This page took 0.090868 seconds and 5 git commands to generate.