[media] DiBxxxx: get rid of DMA buffer on stack
[pandora-kernel.git] / drivers / media / dvb / frontends / dibx000_common.c
1 #include <linux/i2c.h>
2
3 #include "dibx000_common.h"
4
5 static int debug;
6 module_param(debug, int, 0644);
7 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
8
9 #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiBX000: "); printk(args); printk("\n"); } } while (0)
10
11 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
12 {
13         mst->i2c_write_buffer[0] = (reg >> 8) & 0xff;
14         mst->i2c_write_buffer[1] = reg & 0xff;
15         mst->i2c_write_buffer[2] = (val >> 8) & 0xff;
16         mst->i2c_write_buffer[3] = val & 0xff;
17
18         memset(mst->msg, 0, sizeof(struct i2c_msg));
19         mst->msg[0].addr = mst->i2c_addr;
20         mst->msg[0].flags = 0;
21         mst->msg[0].buf = mst->i2c_write_buffer;
22         mst->msg[0].len = 4;
23
24         return i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
25 }
26
27 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
28 {
29         mst->i2c_write_buffer[0] = reg >> 8;
30         mst->i2c_write_buffer[1] = reg & 0xff;
31
32         memset(mst->msg, 0, 2 * sizeof(struct i2c_msg));
33         mst->msg[0].addr = mst->i2c_addr;
34         mst->msg[0].flags = 0;
35         mst->msg[0].buf = mst->i2c_write_buffer;
36         mst->msg[0].len = 2;
37         mst->msg[1].addr = mst->i2c_addr;
38         mst->msg[1].flags = I2C_M_RD;
39         mst->msg[1].buf = mst->i2c_read_buffer;
40         mst->msg[1].len = 2;
41
42         if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
43                 dprintk("i2c read error on %d", reg);
44
45         return (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
46 }
47
48 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
49 {
50         int i = 100;
51         u16 status;
52
53         while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
54                 ;
55
56         /* i2c timed out */
57         if (i == 0)
58                 return -EREMOTEIO;
59
60         /* no acknowledge */
61         if ((status & 0x0080) == 0)
62                 return -EREMOTEIO;
63
64         return 0;
65 }
66
67 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
68 {
69         u16 data;
70         u16 da;
71         u16 i;
72         u16 txlen = msg->len, len;
73         const u8 *b = msg->buf;
74
75         while (txlen) {
76                 dibx000_read_word(mst, mst->base_reg + 2);
77
78                 len = txlen > 8 ? 8 : txlen;
79                 for (i = 0; i < len; i += 2) {
80                         data = *b++ << 8;
81                         if (i+1 < len)
82                                 data |= *b++;
83                         dibx000_write_word(mst, mst->base_reg, data);
84                 }
85                 da = (((u8) (msg->addr))  << 9) |
86                         (1           << 8) |
87                         (1           << 7) |
88                         (0           << 6) |
89                         (0           << 5) |
90                         ((len & 0x7) << 2) |
91                         (0           << 1) |
92                         (0           << 0);
93
94                 if (txlen == msg->len)
95                         da |= 1 << 5; /* start */
96
97                 if (txlen-len == 0 && stop)
98                         da |= 1 << 6; /* stop */
99
100                 dibx000_write_word(mst, mst->base_reg+1, da);
101
102                 if (dibx000_is_i2c_done(mst) != 0)
103                         return -EREMOTEIO;
104                 txlen -= len;
105         }
106
107         return 0;
108 }
109
110 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
111 {
112         u16 da;
113         u8 *b = msg->buf;
114         u16 rxlen = msg->len, len;
115
116         while (rxlen) {
117                 len = rxlen > 8 ? 8 : rxlen;
118                 da = (((u8) (msg->addr)) << 9) |
119                         (1           << 8) |
120                         (1           << 7) |
121                         (0           << 6) |
122                         (0           << 5) |
123                         ((len & 0x7) << 2) |
124                         (1           << 1) |
125                         (0           << 0);
126
127                 if (rxlen == msg->len)
128                         da |= 1 << 5; /* start */
129
130                 if (rxlen-len == 0)
131                         da |= 1 << 6; /* stop */
132                 dibx000_write_word(mst, mst->base_reg+1, da);
133
134                 if (dibx000_is_i2c_done(mst) != 0)
135                         return -EREMOTEIO;
136
137                 rxlen -= len;
138
139                 while (len) {
140                         da = dibx000_read_word(mst, mst->base_reg);
141                         *b++ = (da >> 8) & 0xff;
142                         len--;
143                         if (len >= 1) {
144                                 *b++ =  da   & 0xff;
145                                 len--;
146                         }
147                 }
148         }
149
150         return 0;
151 }
152
153 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
154 {
155         struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
156
157         if (mst->device_rev < DIB7000MC && speed < 235)
158                 speed = 235;
159         return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
160
161 }
162 EXPORT_SYMBOL(dibx000_i2c_set_speed);
163
164 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
165 {
166         return I2C_FUNC_I2C;
167 }
168
169 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
170                                         enum dibx000_i2c_interface intf)
171 {
172         if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
173                 dprintk("selecting interface: %d", intf);
174                 mst->selected_interface = intf;
175                 return dibx000_write_word(mst, mst->base_reg + 4, intf);
176         }
177         return 0;
178 }
179
180 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
181 {
182         struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
183         int msg_index;
184         int ret = 0;
185
186         dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
187         for (msg_index = 0; msg_index < num; msg_index++) {
188                 if (msg[msg_index].flags & I2C_M_RD) {
189                         ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
190                         if (ret != 0)
191                                 return 0;
192                 } else {
193                         ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
194                         if (ret != 0)
195                                 return 0;
196                 }
197         }
198
199         return num;
200 }
201
202 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
203 {
204         struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
205         int msg_index;
206         int ret = 0;
207
208         dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
209         for (msg_index = 0; msg_index < num; msg_index++) {
210                 if (msg[msg_index].flags & I2C_M_RD) {
211                         ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
212                         if (ret != 0)
213                                 return 0;
214                 } else {
215                         ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
216                         if (ret != 0)
217                                 return 0;
218                 }
219         }
220
221         return num;
222 }
223
224 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
225         .master_xfer = dibx000_i2c_master_xfer_gpio12,
226         .functionality = dibx000_i2c_func,
227 };
228
229 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
230         .master_xfer = dibx000_i2c_master_xfer_gpio34,
231         .functionality = dibx000_i2c_func,
232 };
233
234 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
235                                  u8 addr, int onoff)
236 {
237         u16 val;
238
239
240         if (onoff)
241                 val = addr << 8;        // bit 7 = use master or not, if 0, the gate is open
242         else
243                 val = 1 << 7;
244
245         if (mst->device_rev > DIB7000)
246                 val <<= 1;
247
248         tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
249         tx[1] = ((mst->base_reg + 1) & 0xff);
250         tx[2] = val >> 8;
251         tx[3] = val & 0xff;
252
253         return 0;
254 }
255
256 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
257                                         struct i2c_msg msg[], int num)
258 {
259         struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
260
261         if (num > 32) {
262                 dprintk("%s: too much I2C message to be transmitted (%i).\
263                                 Maximum is 32", __func__, num);
264                 return -ENOMEM;
265         }
266
267         memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
268
269         dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
270
271         /* open the gate */
272         dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
273         mst->msg[0].addr = mst->i2c_addr;
274         mst->msg[0].buf = &mst->i2c_write_buffer[0];
275         mst->msg[0].len = 4;
276
277         memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
278
279         /* close the gate */
280         dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
281         mst->msg[num + 1].addr = mst->i2c_addr;
282         mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
283         mst->msg[num + 1].len = 4;
284
285         return i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? num : -EIO;
286 }
287
288 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
289         .master_xfer = dibx000_i2c_gated_gpio67_xfer,
290         .functionality = dibx000_i2c_func,
291 };
292
293 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
294                                         struct i2c_msg msg[], int num)
295 {
296         struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
297
298         if (num > 32) {
299                 dprintk("%s: too much I2C message to be transmitted (%i).\
300                                 Maximum is 32", __func__, num);
301                 return -ENOMEM;
302         }
303
304         memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
305
306         dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
307
308         /* open the gate */
309         dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
310         mst->msg[0].addr = mst->i2c_addr;
311         mst->msg[0].buf = &mst->i2c_write_buffer[0];
312         mst->msg[0].len = 4;
313
314         memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
315
316         /* close the gate */
317         dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
318         mst->msg[num + 1].addr = mst->i2c_addr;
319         mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
320         mst->msg[num + 1].len = 4;
321
322         return i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? num : -EIO;
323 }
324
325 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
326         .master_xfer = dibx000_i2c_gated_tuner_xfer,
327         .functionality = dibx000_i2c_func,
328 };
329
330 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
331                                                 enum dibx000_i2c_interface intf,
332                                                 int gating)
333 {
334         struct i2c_adapter *i2c = NULL;
335
336         switch (intf) {
337         case DIBX000_I2C_INTERFACE_TUNER:
338                 if (gating)
339                         i2c = &mst->gated_tuner_i2c_adap;
340                 break;
341         case DIBX000_I2C_INTERFACE_GPIO_1_2:
342                 if (!gating)
343                         i2c = &mst->master_i2c_adap_gpio12;
344                 break;
345         case DIBX000_I2C_INTERFACE_GPIO_3_4:
346                 if (!gating)
347                         i2c = &mst->master_i2c_adap_gpio34;
348                 break;
349         case DIBX000_I2C_INTERFACE_GPIO_6_7:
350                 if (gating)
351                         i2c = &mst->master_i2c_adap_gpio67;
352                 break;
353         default:
354                 printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
355                 break;
356         }
357
358         return i2c;
359 }
360
361 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
362
363 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
364 {
365         /* initialize the i2c-master by closing the gate */
366         u8 tx[4];
367         struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
368
369         dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
370         i2c_transfer(mst->i2c_adap, &m, 1);
371         mst->selected_interface = 0xff; // the first time force a select of the I2C
372         dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
373 }
374
375 EXPORT_SYMBOL(dibx000_reset_i2c_master);
376
377 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
378                                 struct i2c_algorithm *algo, const char *name,
379                                 struct dibx000_i2c_master *mst)
380 {
381         strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
382         i2c_adap->algo = algo;
383         i2c_adap->algo_data = NULL;
384         i2c_set_adapdata(i2c_adap, mst);
385         if (i2c_add_adapter(i2c_adap) < 0)
386                 return -ENODEV;
387         return 0;
388 }
389
390 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
391                                 struct i2c_adapter *i2c_adap, u8 i2c_addr)
392 {
393         u8 tx[4];
394         struct i2c_msg m = {.addr = i2c_addr >> 1,.buf = tx,.len = 4 };
395
396         mst->device_rev = device_rev;
397         mst->i2c_adap = i2c_adap;
398         mst->i2c_addr = i2c_addr >> 1;
399
400         if (device_rev == DIB7000P || device_rev == DIB8000)
401                 mst->base_reg = 1024;
402         else
403                 mst->base_reg = 768;
404
405         mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
406         if (i2c_adapter_init
407                         (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
408                          "DiBX000 tuner I2C bus", mst) != 0)
409                 printk(KERN_ERR
410                                 "DiBX000: could not initialize the tuner i2c_adapter\n");
411
412         mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
413         if (i2c_adapter_init
414                         (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
415                          "DiBX000 master GPIO12 I2C bus", mst) != 0)
416                 printk(KERN_ERR
417                                 "DiBX000: could not initialize the master i2c_adapter\n");
418
419         mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
420         if (i2c_adapter_init
421                         (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
422                          "DiBX000 master GPIO34 I2C bus", mst) != 0)
423                 printk(KERN_ERR
424                                 "DiBX000: could not initialize the master i2c_adapter\n");
425
426         mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
427         if (i2c_adapter_init
428                         (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
429                          "DiBX000 master GPIO67 I2C bus", mst) != 0)
430                 printk(KERN_ERR
431                                 "DiBX000: could not initialize the master i2c_adapter\n");
432
433         /* initialize the i2c-master by closing the gate */
434         dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
435
436         return i2c_transfer(i2c_adap, &m, 1) == 1;
437 }
438
439 EXPORT_SYMBOL(dibx000_init_i2c_master);
440
441 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
442 {
443         i2c_del_adapter(&mst->gated_tuner_i2c_adap);
444         i2c_del_adapter(&mst->master_i2c_adap_gpio12);
445         i2c_del_adapter(&mst->master_i2c_adap_gpio34);
446         i2c_del_adapter(&mst->master_i2c_adap_gpio67);
447 }
448 EXPORT_SYMBOL(dibx000_exit_i2c_master);
449
450
451 u32 systime(void)
452 {
453         struct timespec t;
454
455         t = current_kernel_time();
456         return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
457 }
458 EXPORT_SYMBOL(systime);
459
460 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
461 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
462 MODULE_LICENSE("GPL");