sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / dvb-frontends / dibx000_common.c
blobbc28184c7fb0855d0cdb1d23167a95ac2cf5b98f
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/i2c.h>
4 #include <linux/mutex.h>
5 #include <linux/module.h>
7 #include "dibx000_common.h"
9 static int debug;
10 module_param(debug, int, 0644);
11 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
13 #define dprintk(fmt, arg...) do { \
14 if (debug) \
15 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
16 __func__, ##arg); \
17 } while (0)
19 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
21 int ret;
23 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
24 dprintk("could not acquire lock\n");
25 return -EINVAL;
28 mst->i2c_write_buffer[0] = (reg >> 8) & 0xff;
29 mst->i2c_write_buffer[1] = reg & 0xff;
30 mst->i2c_write_buffer[2] = (val >> 8) & 0xff;
31 mst->i2c_write_buffer[3] = val & 0xff;
33 memset(mst->msg, 0, 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 = 4;
39 ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
40 mutex_unlock(&mst->i2c_buffer_lock);
42 return ret;
45 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
47 u16 ret;
49 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
50 dprintk("could not acquire lock\n");
51 return 0;
54 mst->i2c_write_buffer[0] = reg >> 8;
55 mst->i2c_write_buffer[1] = reg & 0xff;
57 memset(mst->msg, 0, 2 * sizeof(struct i2c_msg));
58 mst->msg[0].addr = mst->i2c_addr;
59 mst->msg[0].flags = 0;
60 mst->msg[0].buf = mst->i2c_write_buffer;
61 mst->msg[0].len = 2;
62 mst->msg[1].addr = mst->i2c_addr;
63 mst->msg[1].flags = I2C_M_RD;
64 mst->msg[1].buf = mst->i2c_read_buffer;
65 mst->msg[1].len = 2;
67 if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
68 dprintk("i2c read error on %d\n", reg);
70 ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
71 mutex_unlock(&mst->i2c_buffer_lock);
73 return ret;
76 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
78 int i = 100;
79 u16 status;
81 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
84 /* i2c timed out */
85 if (i == 0)
86 return -EREMOTEIO;
88 /* no acknowledge */
89 if ((status & 0x0080) == 0)
90 return -EREMOTEIO;
92 return 0;
95 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
97 u16 data;
98 u16 da;
99 u16 i;
100 u16 txlen = msg->len, len;
101 const u8 *b = msg->buf;
103 while (txlen) {
104 dibx000_read_word(mst, mst->base_reg + 2);
106 len = txlen > 8 ? 8 : txlen;
107 for (i = 0; i < len; i += 2) {
108 data = *b++ << 8;
109 if (i+1 < len)
110 data |= *b++;
111 dibx000_write_word(mst, mst->base_reg, data);
113 da = (((u8) (msg->addr)) << 9) |
114 (1 << 8) |
115 (1 << 7) |
116 (0 << 6) |
117 (0 << 5) |
118 ((len & 0x7) << 2) |
119 (0 << 1) |
120 (0 << 0);
122 if (txlen == msg->len)
123 da |= 1 << 5; /* start */
125 if (txlen-len == 0 && stop)
126 da |= 1 << 6; /* stop */
128 dibx000_write_word(mst, mst->base_reg+1, da);
130 if (dibx000_is_i2c_done(mst) != 0)
131 return -EREMOTEIO;
132 txlen -= len;
135 return 0;
138 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
140 u16 da;
141 u8 *b = msg->buf;
142 u16 rxlen = msg->len, len;
144 while (rxlen) {
145 len = rxlen > 8 ? 8 : rxlen;
146 da = (((u8) (msg->addr)) << 9) |
147 (1 << 8) |
148 (1 << 7) |
149 (0 << 6) |
150 (0 << 5) |
151 ((len & 0x7) << 2) |
152 (1 << 1) |
153 (0 << 0);
155 if (rxlen == msg->len)
156 da |= 1 << 5; /* start */
158 if (rxlen-len == 0)
159 da |= 1 << 6; /* stop */
160 dibx000_write_word(mst, mst->base_reg+1, da);
162 if (dibx000_is_i2c_done(mst) != 0)
163 return -EREMOTEIO;
165 rxlen -= len;
167 while (len) {
168 da = dibx000_read_word(mst, mst->base_reg);
169 *b++ = (da >> 8) & 0xff;
170 len--;
171 if (len >= 1) {
172 *b++ = da & 0xff;
173 len--;
178 return 0;
181 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
183 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
185 if (mst->device_rev < DIB7000MC && speed < 235)
186 speed = 235;
187 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
190 EXPORT_SYMBOL(dibx000_i2c_set_speed);
192 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
194 return I2C_FUNC_I2C;
197 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
198 enum dibx000_i2c_interface intf)
200 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
201 dprintk("selecting interface: %d\n", intf);
202 mst->selected_interface = intf;
203 return dibx000_write_word(mst, mst->base_reg + 4, intf);
205 return 0;
208 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
210 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
211 int msg_index;
212 int ret = 0;
214 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
215 for (msg_index = 0; msg_index < num; msg_index++) {
216 if (msg[msg_index].flags & I2C_M_RD) {
217 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
218 if (ret != 0)
219 return 0;
220 } else {
221 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
222 if (ret != 0)
223 return 0;
227 return num;
230 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
232 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
233 int msg_index;
234 int ret = 0;
236 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
237 for (msg_index = 0; msg_index < num; msg_index++) {
238 if (msg[msg_index].flags & I2C_M_RD) {
239 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
240 if (ret != 0)
241 return 0;
242 } else {
243 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
244 if (ret != 0)
245 return 0;
249 return num;
252 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
253 .master_xfer = dibx000_i2c_master_xfer_gpio12,
254 .functionality = dibx000_i2c_func,
257 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
258 .master_xfer = dibx000_i2c_master_xfer_gpio34,
259 .functionality = dibx000_i2c_func,
262 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
263 u8 addr, int onoff)
265 u16 val;
268 if (onoff)
269 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
270 else
271 val = 1 << 7;
273 if (mst->device_rev > DIB7000)
274 val <<= 1;
276 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
277 tx[1] = ((mst->base_reg + 1) & 0xff);
278 tx[2] = val >> 8;
279 tx[3] = val & 0xff;
281 return 0;
284 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
285 struct i2c_msg msg[], int num)
287 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
288 int ret;
290 if (num > 32) {
291 dprintk("%s: too much I2C message to be transmitted (%i).\
292 Maximum is 32", __func__, num);
293 return -ENOMEM;
296 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
298 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
299 dprintk("could not acquire lock\n");
300 return -EINVAL;
303 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
305 /* open the gate */
306 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
307 mst->msg[0].addr = mst->i2c_addr;
308 mst->msg[0].buf = &mst->i2c_write_buffer[0];
309 mst->msg[0].len = 4;
311 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
313 /* close the gate */
314 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
315 mst->msg[num + 1].addr = mst->i2c_addr;
316 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
317 mst->msg[num + 1].len = 4;
319 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
320 num : -EIO);
322 mutex_unlock(&mst->i2c_buffer_lock);
323 return ret;
326 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
327 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
328 .functionality = dibx000_i2c_func,
331 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
332 struct i2c_msg msg[], int num)
334 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
335 int ret;
337 if (num > 32) {
338 dprintk("%s: too much I2C message to be transmitted (%i).\
339 Maximum is 32", __func__, num);
340 return -ENOMEM;
343 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
345 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
346 dprintk("could not acquire lock\n");
347 return -EINVAL;
349 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
351 /* open the gate */
352 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
353 mst->msg[0].addr = mst->i2c_addr;
354 mst->msg[0].buf = &mst->i2c_write_buffer[0];
355 mst->msg[0].len = 4;
357 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
359 /* close the gate */
360 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
361 mst->msg[num + 1].addr = mst->i2c_addr;
362 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
363 mst->msg[num + 1].len = 4;
365 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
366 num : -EIO);
367 mutex_unlock(&mst->i2c_buffer_lock);
368 return ret;
371 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
372 .master_xfer = dibx000_i2c_gated_tuner_xfer,
373 .functionality = dibx000_i2c_func,
376 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
377 enum dibx000_i2c_interface intf,
378 int gating)
380 struct i2c_adapter *i2c = NULL;
382 switch (intf) {
383 case DIBX000_I2C_INTERFACE_TUNER:
384 if (gating)
385 i2c = &mst->gated_tuner_i2c_adap;
386 break;
387 case DIBX000_I2C_INTERFACE_GPIO_1_2:
388 if (!gating)
389 i2c = &mst->master_i2c_adap_gpio12;
390 break;
391 case DIBX000_I2C_INTERFACE_GPIO_3_4:
392 if (!gating)
393 i2c = &mst->master_i2c_adap_gpio34;
394 break;
395 case DIBX000_I2C_INTERFACE_GPIO_6_7:
396 if (gating)
397 i2c = &mst->master_i2c_adap_gpio67;
398 break;
399 default:
400 pr_err("incorrect I2C interface selected\n");
401 break;
404 return i2c;
407 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
409 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
411 /* initialize the i2c-master by closing the gate */
412 u8 tx[4];
413 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
415 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
416 i2c_transfer(mst->i2c_adap, &m, 1);
417 mst->selected_interface = 0xff; // the first time force a select of the I2C
418 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
421 EXPORT_SYMBOL(dibx000_reset_i2c_master);
423 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
424 struct i2c_algorithm *algo, const char *name,
425 struct dibx000_i2c_master *mst)
427 strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
428 i2c_adap->algo = algo;
429 i2c_adap->algo_data = NULL;
430 i2c_set_adapdata(i2c_adap, mst);
431 if (i2c_add_adapter(i2c_adap) < 0)
432 return -ENODEV;
433 return 0;
436 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
437 struct i2c_adapter *i2c_adap, u8 i2c_addr)
439 int ret;
441 mutex_init(&mst->i2c_buffer_lock);
442 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
443 dprintk("could not acquire lock\n");
444 return -EINVAL;
446 memset(mst->msg, 0, sizeof(struct i2c_msg));
447 mst->msg[0].addr = i2c_addr >> 1;
448 mst->msg[0].flags = 0;
449 mst->msg[0].buf = mst->i2c_write_buffer;
450 mst->msg[0].len = 4;
452 mst->device_rev = device_rev;
453 mst->i2c_adap = i2c_adap;
454 mst->i2c_addr = i2c_addr >> 1;
456 if (device_rev == DIB7000P || device_rev == DIB8000)
457 mst->base_reg = 1024;
458 else
459 mst->base_reg = 768;
461 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
462 if (i2c_adapter_init
463 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
464 "DiBX000 tuner I2C bus", mst) != 0)
465 pr_err("could not initialize the tuner i2c_adapter\n");
467 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
468 if (i2c_adapter_init
469 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
470 "DiBX000 master GPIO12 I2C bus", mst) != 0)
471 pr_err("could not initialize the master i2c_adapter\n");
473 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
474 if (i2c_adapter_init
475 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
476 "DiBX000 master GPIO34 I2C bus", mst) != 0)
477 pr_err("could not initialize the master i2c_adapter\n");
479 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
480 if (i2c_adapter_init
481 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
482 "DiBX000 master GPIO67 I2C bus", mst) != 0)
483 pr_err("could not initialize the master i2c_adapter\n");
485 /* initialize the i2c-master by closing the gate */
486 dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0);
488 ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1);
489 mutex_unlock(&mst->i2c_buffer_lock);
491 return ret;
494 EXPORT_SYMBOL(dibx000_init_i2c_master);
496 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
498 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
499 i2c_del_adapter(&mst->master_i2c_adap_gpio12);
500 i2c_del_adapter(&mst->master_i2c_adap_gpio34);
501 i2c_del_adapter(&mst->master_i2c_adap_gpio67);
503 EXPORT_SYMBOL(dibx000_exit_i2c_master);
505 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
506 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
507 MODULE_LICENSE("GPL");