drivers/tty: make pty.c slightly more explicitly non-modular
[firefly-linux-kernel-4.4.55.git] / drivers / staging / sm750fb / ddk750_hwi2c.c
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
12 int hwI2CInit(
13 unsigned char busSpeedMode
14 )
15 {
16         unsigned int value;
17
18         /* Enable GPIO 30 & 31 as IIC clock & data */
19         value = PEEK32(GPIO_MUX);
20
21         value = FIELD_SET(value, GPIO_MUX, 30, I2C) |
22                           FIELD_SET(0, GPIO_MUX, 31, I2C);
23         POKE32(GPIO_MUX, value);
24
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;
40 }
41
42
43 void hwI2CClose(void)
44 {
45         unsigned int value;
46
47         /* Disable I2C controller */
48         value = PEEK32(I2C_CTRL);
49         value = FIELD_SET(value, I2C_CTRL, EN, DISABLE);
50         POKE32(I2C_CTRL, value);
51
52         /* Disable I2C Power */
53         enableI2C(0);
54
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);
60 }
61
62
63 static long hwI2CWaitTXDone(void)
64 {
65         unsigned int timeout;
66
67         /* Wait until the transfer is completed. */
68         timeout = HWI2C_WAIT_TIMEOUT;
69         while ((FIELD_GET(PEEK32(I2C_STATUS), I2C_STATUS, TX) != I2C_STATUS_TX_COMPLETED) &&
70                (timeout != 0))
71                 timeout--;
72
73         if (timeout == 0)
74                 return (-1);
75
76         return 0;
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  */
93 static unsigned int hwI2CWriteData(
94         unsigned char deviceAddress,
95         unsigned int length,
96         unsigned char *pBuffer
97 )
98 {
99         unsigned char count, i;
100         unsigned int totalBytes = 0;
101
102         /* Set the Device Address */
103         POKE32(I2C_SLAVE_ADDRESS, deviceAddress & ~0x01);
104
105         /* Write data.
106          * Note:
107          *      Only 16 byte can be accessed per i2c start instruction.
108          */
109         do {
110                 /* Reset I2C by writing 0 to I2C_RESET register to clear the previous status. */
111                 POKE32(I2C_RESET, 0);
112
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);
119
120                 /* Move the data to the I2C data register */
121                 for (i = 0; i <= count; i++)
122                         POKE32(I2C_DATA0 + i, *pBuffer++);
123
124                 /* Start the I2C */
125                 POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
126
127                 /* Wait until the transfer is completed. */
128                 if (hwI2CWaitTXDone() != 0)
129                         break;
130
131                 /* Substract length */
132                 length -= (count + 1);
133
134                 /* Total byte written */
135                 totalBytes += (count + 1);
136
137         } while (length > 0);
138
139         return totalBytes;
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  */
159 static unsigned int hwI2CReadData(
160         unsigned char deviceAddress,
161         unsigned int length,
162         unsigned char *pBuffer
163 )
164 {
165         unsigned char count, i;
166         unsigned int totalBytes = 0;
167
168         /* Set the Device Address */
169         POKE32(I2C_SLAVE_ADDRESS, deviceAddress | 0x01);
170
171         /* Read data and save them to the buffer.
172          * Note:
173          *      Only 16 byte can be accessed per i2c start instruction.
174          */
175         do {
176                 /* Reset I2C by writing 0 to I2C_RESET register to clear all the status. */
177                 POKE32(I2C_RESET, 0);
178
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);
185
186                 /* Start the I2C */
187                 POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
188
189                 /* Wait until transaction done. */
190                 if (hwI2CWaitTXDone() != 0)
191                         break;
192
193                 /* Save the data to the given buffer */
194                 for (i = 0; i <= count; i++)
195                         *pBuffer++ = PEEK32(I2C_DATA0 + i);
196
197                 /* Substract length by 16 */
198                 length -= (count + 1);
199
200                 /* Number of bytes read. */
201                 totalBytes += (count + 1);
202
203         } while (length > 0);
204
205         return totalBytes;
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  */
222 unsigned char hwI2CReadReg(
223         unsigned char deviceAddress,
224         unsigned char registerIndex
225 )
226 {
227         unsigned char value = (0xFF);
228
229         if (hwI2CWriteData(deviceAddress, 1, &registerIndex) == 1)
230                 hwI2CReadData(deviceAddress, 1, &value);
231
232         return value;
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  */
252 int hwI2CWriteReg(
253         unsigned char deviceAddress,
254         unsigned char registerIndex,
255         unsigned char data
256 )
257 {
258         unsigned char value[2];
259
260         value[0] = registerIndex;
261         value[1] = data;
262         if (hwI2CWriteData(deviceAddress, 2, value) == 2)
263                 return 0;
264
265         return (-1);
266 }
267
268
269 #endif