Initial commit
[kernel/linux-3.0.git] / drivers / media / isdbt / fc8150 / fc8150_i2c.c
1 /*****************************************************************************
2  Copyright(c) 2012 FCI Inc. All Rights Reserved
3
4  File name : fc8150_i2c.c
5
6  Description : fc8150 host interface
7
8 *******************************************************************************/
9
10 #include "fci_types.h"
11 #include "fc8150_regs.h"
12 #include "fci_oal.h"
13 #include "fci_hal.h"
14
15 #define HPIC_READ           0x01 /* read command */
16 #define HPIC_WRITE          0x02 /* write command */
17 #define HPIC_AINC           0x04 /* address increment */
18 #define HPIC_BMODE          0x00 /* byte mode */
19 #define HPIC_WMODE          0x10 /* word mode */
20 #define HPIC_LMODE          0x20 /* long mode */
21 #define HPIC_ENDIAN         0x00 /* little endian */
22 #define HPIC_CLEAR          0x80 /* currently not used */
23
24 #define CHIP_ADDR           0x58
25
26 static int i2c_bulkread(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
27 {
28         /* Write your own i2c driver code here for read operation. */
29
30         return BBM_OK;
31 }
32
33 static int i2c_bulkwrite(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
34 {
35
36         /* Write your own i2c driver code here for Write operation. */
37
38         return BBM_OK;
39 }
40
41 static int i2c_dataread(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u32 length)
42 {
43         return i2c_bulkread(hDevice, chip, addr, data, length);
44 }
45
46 int fc8150_bypass_read(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
47 {
48         int res;
49         u8 bypass_addr = 0x03;
50         u8 bypass_data = 1;
51         u8 bypass_len  = 1;
52
53         OAL_OBTAIN_SEMAPHORE();
54         res = i2c_bulkwrite(hDevice, CHIP_ADDR, bypass_addr, &bypass_data, \
55                 bypass_len);
56         res |= i2c_bulkread(hDevice, chip, addr, data, length);
57         OAL_RELEASE_SEMAPHORE();
58
59         return res;
60 }
61
62 int fc8150_bypass_write(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
63 {
64         int res;
65         u8 bypass_addr = 0x03;
66         u8 bypass_data = 1;
67         u8 bypass_len  = 1;
68
69         OAL_OBTAIN_SEMAPHORE();
70         res = i2c_bulkwrite(hDevice, CHIP_ADDR, bypass_addr, &bypass_data, \
71                 bypass_len);
72         res |= i2c_bulkwrite(hDevice, chip, addr, data, length);
73         OAL_RELEASE_SEMAPHORE();
74
75         return res;
76 }
77
78 int fc8150_i2c_init(HANDLE hDevice, u16 param1, u16 param2)
79 {
80         OAL_CREATE_SEMAPHORE();
81
82         /* for TSIF, you can call here your own TSIF initialization function. */
83         /* tsif_initialize(); */
84
85         bbm_write(hDevice, BBM_TS_CLK_DIV, 0x04);
86         bbm_write(hDevice, BBM_TS_PAUSE, 0x80);
87
88         bbm_write(hDevice, BBM_TS_CTRL, 0x02);
89         bbm_write(hDevice, BBM_TS_SEL, 0x84);
90
91         return BBM_OK;
92 }
93
94 int fc8150_i2c_byteread(HANDLE hDevice, u16 addr, u8 *data)
95 {
96         int res;
97         u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
98
99         OAL_OBTAIN_SEMAPHORE();
100         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
101                 (u8 *)&addr, 2);
102         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
103         res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, 1);
104         OAL_RELEASE_SEMAPHORE();
105
106         return res;
107 }
108
109 int fc8150_i2c_wordread(HANDLE hDevice, u16 addr, u16 *data)
110 {
111         int res;
112         u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
113
114         OAL_OBTAIN_SEMAPHORE();
115         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
116                 (u8 *)&addr, 2);
117         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
118         res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)data, 2);
119         OAL_RELEASE_SEMAPHORE();
120
121         return res;
122 }
123
124 int fc8150_i2c_longread(HANDLE hDevice, u16 addr, u32 *data)
125 {
126         int res;
127         u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
128
129         OAL_OBTAIN_SEMAPHORE();
130         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
131                 (u8 *)&addr, 2);
132         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
133         res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)data, 4);
134         OAL_RELEASE_SEMAPHORE();
135
136         return res;
137 }
138
139 int fc8150_i2c_bulkread(HANDLE hDevice, u16 addr, u8 *data, u16 length)
140 {
141         int res;
142         u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
143
144         OAL_OBTAIN_SEMAPHORE();
145         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
146                 (u8 *)&addr, 2);
147         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
148         res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
149         OAL_RELEASE_SEMAPHORE();
150
151         return res;
152 }
153
154 int fc8150_i2c_bytewrite(HANDLE hDevice, u16 addr, u8 data)
155 {
156         int res;
157         u8 command = HPIC_WRITE | HPIC_BMODE | HPIC_ENDIAN;
158
159         OAL_OBTAIN_SEMAPHORE();
160         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
161                 (u8 *)&addr, 2);
162         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
163         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 1);
164         OAL_RELEASE_SEMAPHORE();
165
166         return res;
167 }
168
169 int fc8150_i2c_wordwrite(HANDLE hDevice, u16 addr, u16 data)
170 {
171         int res;
172         u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
173
174         OAL_OBTAIN_SEMAPHORE();
175         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
176                 (u8 *)&addr, 2);
177         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
178         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 2);
179         OAL_RELEASE_SEMAPHORE();
180
181         return res;
182 }
183
184 int fc8150_i2c_longwrite(HANDLE hDevice, u16 addr, u32 data)
185 {
186         int res;
187         u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
188
189         OAL_OBTAIN_SEMAPHORE();
190         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
191                 (u8 *)&addr, 2);
192         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
193         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 4);
194         OAL_RELEASE_SEMAPHORE();
195
196         return res;
197 }
198
199 int fc8150_i2c_bulkwrite(HANDLE hDevice, u16 addr, u8 *data, u16 length)
200 {
201         int res;
202         u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
203
204         OAL_OBTAIN_SEMAPHORE();
205         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
206                 (u8 *)&addr, 2);
207         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
208         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
209         OAL_RELEASE_SEMAPHORE();
210
211         return res;
212 }
213
214 int fc8150_i2c_dataread(HANDLE hDevice, u16 addr, u8 *data, u32 length)
215 {
216         int res;
217         u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
218
219         OAL_OBTAIN_SEMAPHORE();
220         res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG,
221                 (u8 *)&addr, 2);
222         res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
223         res |= i2c_dataread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
224         OAL_RELEASE_SEMAPHORE();
225
226         return res;
227 }
228
229 int fc8150_i2c_deinit(HANDLE hDevice)
230 {
231         bbm_write(hDevice, BBM_TS_SEL, 0x00);
232
233         /*tsif_disable(); */
234
235         OAL_DELETE_SEMAPHORE();
236
237         return BBM_OK;
238 }