V4L/DVB (11310): cx18: remove intermediate 'ioctl' step
[linux-2.6] / 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); } } while (0)
10
11 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
12 {
13         u8 b[4] = {
14                 (reg >> 8) & 0xff, reg & 0xff,
15                 (val >> 8) & 0xff, val & 0xff,
16         };
17         struct i2c_msg msg = {
18                 .addr = mst->i2c_addr, .flags = 0, .buf = b, .len = 4
19         };
20         return i2c_transfer(mst->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
21 }
22
23
24 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst, enum dibx000_i2c_interface intf)
25 {
26         if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
27                 dprintk("selecting interface: %d\n",intf);
28                 mst->selected_interface = intf;
29                 return dibx000_write_word(mst, mst->base_reg + 4, intf);
30         }
31         return 0;
32 }
33
34 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4], u8 addr, int onoff)
35 {
36         u16 val;
37
38
39         if (onoff)
40                 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
41         else
42                 val = 1 << 7;
43
44         if (mst->device_rev > DIB7000)
45                 val <<= 1;
46
47         tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
48         tx[1] = ( (mst->base_reg + 1)       & 0xff);
49         tx[2] = val >> 8;
50         tx[3] = val & 0xff;
51
52         return 0;
53 }
54
55 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
56 {
57         return I2C_FUNC_I2C;
58 }
59
60 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
61 {
62         struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
63         struct i2c_msg m[2 + num];
64         u8 tx_open[4], tx_close[4];
65
66         memset(m,0, sizeof(struct i2c_msg) * (2 + num));
67
68         dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
69
70         dibx000_i2c_gate_ctrl(mst, tx_open,  msg[0].addr, 1);
71         m[0].addr = mst->i2c_addr;
72         m[0].buf  = tx_open;
73         m[0].len  = 4;
74
75         memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
76
77         dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0);
78         m[num+1].addr = mst->i2c_addr;
79         m[num+1].buf  = tx_close;
80         m[num+1].len  = 4;
81
82         return i2c_transfer(mst->i2c_adap, m, 2+num) == 2 + num ? num : -EIO;
83 }
84
85 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
86         .master_xfer   = dibx000_i2c_gated_tuner_xfer,
87         .functionality = dibx000_i2c_func,
88 };
89
90 struct i2c_adapter * dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst, enum dibx000_i2c_interface intf, int gating)
91 {
92         struct i2c_adapter *i2c = NULL;
93
94         switch (intf) {
95                 case DIBX000_I2C_INTERFACE_TUNER:
96                         if (gating)
97                                 i2c = &mst->gated_tuner_i2c_adap;
98                         break;
99                 default:
100                         printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
101                         break;
102         }
103
104         return i2c;
105 }
106 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
107
108 static int i2c_adapter_init(struct i2c_adapter *i2c_adap, struct i2c_algorithm *algo, const char *name, struct dibx000_i2c_master *mst)
109 {
110         strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
111         i2c_adap->class     = I2C_CLASS_TV_DIGITAL,
112         i2c_adap->algo      = algo;
113         i2c_adap->algo_data = NULL;
114         i2c_set_adapdata(i2c_adap, mst);
115         if (i2c_add_adapter(i2c_adap) < 0)
116                 return -ENODEV;
117         return 0;
118 }
119
120 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev, struct i2c_adapter *i2c_adap, u8 i2c_addr)
121 {
122         u8 tx[4];
123         struct i2c_msg m = { .addr = i2c_addr >> 1, .buf = tx, .len = 4 };
124
125         mst->device_rev = device_rev;
126         mst->i2c_adap   = i2c_adap;
127         mst->i2c_addr   = i2c_addr >> 1;
128
129         if (device_rev == DIB7000P)
130                 mst->base_reg = 1024;
131         else
132                 mst->base_reg = 768;
133
134     if (i2c_adapter_init(&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo, "DiBX000 tuner I2C bus", mst) != 0)
135                 printk(KERN_ERR "DiBX000: could not initialize the tuner i2c_adapter\n");
136
137         /* initialize the i2c-master by closing the gate */
138         dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
139
140         return i2c_transfer(i2c_adap, &m, 1) == 1;
141 }
142 EXPORT_SYMBOL(dibx000_init_i2c_master);
143
144 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
145 {
146         i2c_del_adapter(&mst->gated_tuner_i2c_adap);
147 }
148 EXPORT_SYMBOL(dibx000_exit_i2c_master);
149
150 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
151 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
152 MODULE_LICENSE("GPL");