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