blob: 7db908ed87f6d6b3d2bb30d8343a19a3d314c849 [file] [log] [blame]
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -03001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Patrick Boettcher42afd062006-07-29 17:33:44 -03003#include <linux/i2c.h>
Patrick Boettcher79fcce32011-08-03 12:08:21 -03004#include <linux/mutex.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -04005#include <linux/module.h>
Patrick Boettcher42afd062006-07-29 17:33:44 -03006
7#include "dibx000_common.h"
8
9static int debug;
10module_param(debug, int, 0644);
11MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
12
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -030013#define dprintk(fmt, arg...) do { \
14 if (debug) \
15 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
16 __func__, ##arg); \
17} while (0)
Patrick Boettcher42afd062006-07-29 17:33:44 -030018
19static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
20{
Patrick Boettcher79fcce32011-08-03 12:08:21 -030021 int ret;
22
23 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -030024 dprintk("could not acquire lock\n");
Patrick Boettcher79fcce32011-08-03 12:08:21 -030025 return -EINVAL;
26 }
27
Olivier Grenie5a0deee2011-05-03 12:27:33 -030028 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;
Olivier Grenieb994d192011-01-03 15:39:35 -030032
Olivier Grenie5a0deee2011-05-03 12:27:33 -030033 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;
38
Patrick Boettcher79fcce32011-08-03 12:08:21 -030039 ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
40 mutex_unlock(&mst->i2c_buffer_lock);
41
42 return ret;
Patrick Boettcher42afd062006-07-29 17:33:44 -030043}
44
Olivier Grenieb994d192011-01-03 15:39:35 -030045static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
46{
Patrick Boettcher79fcce32011-08-03 12:08:21 -030047 u16 ret;
48
49 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -030050 dprintk("could not acquire lock\n");
Patrick Boettcher79fcce32011-08-03 12:08:21 -030051 return 0;
52 }
53
Olivier Grenie5a0deee2011-05-03 12:27:33 -030054 mst->i2c_write_buffer[0] = reg >> 8;
55 mst->i2c_write_buffer[1] = reg & 0xff;
Olivier Grenieb994d192011-01-03 15:39:35 -030056
Olivier Grenie5a0deee2011-05-03 12:27:33 -030057 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;
66
67 if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -030068 dprintk("i2c read error on %d\n", reg);
Olivier Grenieb994d192011-01-03 15:39:35 -030069
Patrick Boettcher79fcce32011-08-03 12:08:21 -030070 ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
71 mutex_unlock(&mst->i2c_buffer_lock);
72
73 return ret;
Olivier Grenieb994d192011-01-03 15:39:35 -030074}
75
76static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
77{
Olivier Grenieb4d6046e2011-01-04 13:08:14 -030078 int i = 100;
Olivier Grenieb994d192011-01-03 15:39:35 -030079 u16 status;
80
Olivier Grenieb4d6046e2011-01-04 13:08:14 -030081 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
82 ;
Olivier Grenieb994d192011-01-03 15:39:35 -030083
84 /* i2c timed out */
85 if (i == 0)
86 return -EREMOTEIO;
87
88 /* no acknowledge */
89 if ((status & 0x0080) == 0)
90 return -EREMOTEIO;
91
92 return 0;
93}
94
95static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
96{
97 u16 data;
98 u16 da;
99 u16 i;
100 u16 txlen = msg->len, len;
101 const u8 *b = msg->buf;
102
103 while (txlen) {
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300104 dibx000_read_word(mst, mst->base_reg + 2);
Olivier Grenieb994d192011-01-03 15:39:35 -0300105
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);
112 }
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300113 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);
Olivier Grenieb994d192011-01-03 15:39:35 -0300121
122 if (txlen == msg->len)
123 da |= 1 << 5; /* start */
124
125 if (txlen-len == 0 && stop)
126 da |= 1 << 6; /* stop */
127
128 dibx000_write_word(mst, mst->base_reg+1, da);
129
130 if (dibx000_is_i2c_done(mst) != 0)
131 return -EREMOTEIO;
132 txlen -= len;
133 }
134
135 return 0;
136}
137
138static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
139{
140 u16 da;
141 u8 *b = msg->buf;
142 u16 rxlen = msg->len, len;
143
144 while (rxlen) {
145 len = rxlen > 8 ? 8 : rxlen;
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300146 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);
Olivier Grenieb994d192011-01-03 15:39:35 -0300154
155 if (rxlen == msg->len)
156 da |= 1 << 5; /* start */
157
158 if (rxlen-len == 0)
159 da |= 1 << 6; /* stop */
160 dibx000_write_word(mst, mst->base_reg+1, da);
161
162 if (dibx000_is_i2c_done(mst) != 0)
163 return -EREMOTEIO;
164
165 rxlen -= len;
166
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--;
174 }
175 }
176 }
177
178 return 0;
179}
180
181int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
182{
183 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
184
185 if (mst->device_rev < DIB7000MC && speed < 235)
186 speed = 235;
187 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
188
189}
190EXPORT_SYMBOL(dibx000_i2c_set_speed);
191
192static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
193{
194 return I2C_FUNC_I2C;
195}
Patrick Boettcher42afd062006-07-29 17:33:44 -0300196
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300197static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
198 enum dibx000_i2c_interface intf)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300199{
200 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300201 dprintk("selecting interface: %d\n", intf);
Patrick Boettcher42afd062006-07-29 17:33:44 -0300202 mst->selected_interface = intf;
203 return dibx000_write_word(mst, mst->base_reg + 4, intf);
204 }
205 return 0;
206}
207
Olivier Grenieb994d192011-01-03 15:39:35 -0300208static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
209{
210 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
211 int msg_index;
212 int ret = 0;
213
214 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300215 for (msg_index = 0; msg_index < num; msg_index++) {
216 if (msg[msg_index].flags & I2C_M_RD) {
Olivier Grenieb994d192011-01-03 15:39:35 -0300217 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
218 if (ret != 0)
219 return 0;
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300220 } else {
Olivier Grenieb994d192011-01-03 15:39:35 -0300221 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
222 if (ret != 0)
223 return 0;
224 }
225 }
226
227 return num;
228}
229
230static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
231{
232 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
233 int msg_index;
234 int ret = 0;
235
236 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300237 for (msg_index = 0; msg_index < num; msg_index++) {
238 if (msg[msg_index].flags & I2C_M_RD) {
Olivier Grenieb994d192011-01-03 15:39:35 -0300239 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
240 if (ret != 0)
241 return 0;
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300242 } else {
Olivier Grenieb994d192011-01-03 15:39:35 -0300243 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
244 if (ret != 0)
245 return 0;
246 }
247 }
248
249 return num;
250}
251
252static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
253 .master_xfer = dibx000_i2c_master_xfer_gpio12,
254 .functionality = dibx000_i2c_func,
255};
256
257static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
258 .master_xfer = dibx000_i2c_master_xfer_gpio34,
259 .functionality = dibx000_i2c_func,
260};
261
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300262static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
263 u8 addr, int onoff)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300264{
265 u16 val;
266
267
268 if (onoff)
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300269 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
Patrick Boettcher42afd062006-07-29 17:33:44 -0300270 else
271 val = 1 << 7;
272
273 if (mst->device_rev > DIB7000)
274 val <<= 1;
275
276 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300277 tx[1] = ((mst->base_reg + 1) & 0xff);
Patrick Boettcher42afd062006-07-29 17:33:44 -0300278 tx[2] = val >> 8;
279 tx[3] = val & 0xff;
280
281 return 0;
282}
283
Olivier Grenieb994d192011-01-03 15:39:35 -0300284static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
285 struct i2c_msg msg[], int num)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300286{
Olivier Grenieb994d192011-01-03 15:39:35 -0300287 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300288 int ret;
Olivier Grenieb994d192011-01-03 15:39:35 -0300289
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300290 if (num > 32) {
291 dprintk("%s: too much I2C message to be transmitted (%i).\
292 Maximum is 32", __func__, num);
293 return -ENOMEM;
294 }
295
Olivier Grenieb994d192011-01-03 15:39:35 -0300296 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
297
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300298 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300299 dprintk("could not acquire lock\n");
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300300 return -EINVAL;
301 }
302
303 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
304
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300305 /* 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;
Olivier Grenieb994d192011-01-03 15:39:35 -0300310
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300311 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
Olivier Grenieb994d192011-01-03 15:39:35 -0300312
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300313 /* 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;
Olivier Grenieb994d192011-01-03 15:39:35 -0300318
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300319 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
320 num : -EIO);
321
322 mutex_unlock(&mst->i2c_buffer_lock);
323 return ret;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300324}
325
Olivier Grenieb994d192011-01-03 15:39:35 -0300326static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
327 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
328 .functionality = dibx000_i2c_func,
329};
330
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300331static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
332 struct i2c_msg msg[], int num)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300333{
334 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300335 int ret;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300336
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300337 if (num > 32) {
338 dprintk("%s: too much I2C message to be transmitted (%i).\
339 Maximum is 32", __func__, num);
340 return -ENOMEM;
341 }
342
Patrick Boettcher42afd062006-07-29 17:33:44 -0300343 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
344
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300345 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300346 dprintk("could not acquire lock\n");
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300347 return -EINVAL;
348 }
349 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
350
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300351 /* 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;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300356
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300357 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
Patrick Boettcher42afd062006-07-29 17:33:44 -0300358
Olivier Grenie5a0deee2011-05-03 12:27:33 -0300359 /* 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;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300364
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300365 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;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300369}
370
371static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300372 .master_xfer = dibx000_i2c_gated_tuner_xfer,
Patrick Boettcher42afd062006-07-29 17:33:44 -0300373 .functionality = dibx000_i2c_func,
374};
375
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300376struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
Olivier Grenieb994d192011-01-03 15:39:35 -0300377 enum dibx000_i2c_interface intf,
378 int gating)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300379{
380 struct i2c_adapter *i2c = NULL;
381
382 switch (intf) {
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300383 case DIBX000_I2C_INTERFACE_TUNER:
384 if (gating)
385 i2c = &mst->gated_tuner_i2c_adap;
386 break;
Olivier Grenieb994d192011-01-03 15:39:35 -0300387 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;
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300399 default:
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300400 pr_err("incorrect I2C interface selected\n");
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300401 break;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300402 }
403
404 return i2c;
405}
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300406
Patrick Boettcher42afd062006-07-29 17:33:44 -0300407EXPORT_SYMBOL(dibx000_get_i2c_adapter);
408
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300409void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
410{
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 };
414
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);
419}
420
421EXPORT_SYMBOL(dibx000_reset_i2c_master);
422
423static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
Olivier Grenieb994d192011-01-03 15:39:35 -0300424 struct i2c_algorithm *algo, const char *name,
425 struct dibx000_i2c_master *mst)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300426{
David Brownell2096b952007-05-01 23:26:28 +0200427 strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
Jean Delvare2c2742d2010-11-05 07:35:35 -0300428 i2c_adap->algo = algo;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300429 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;
434}
435
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300436int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
Olivier Grenieb994d192011-01-03 15:39:35 -0300437 struct i2c_adapter *i2c_adap, u8 i2c_addr)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300438{
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300439 int ret;
440
441 mutex_init(&mst->i2c_buffer_lock);
442 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300443 dprintk("could not acquire lock\n");
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300444 return -EINVAL;
445 }
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;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300451
452 mst->device_rev = device_rev;
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300453 mst->i2c_adap = i2c_adap;
454 mst->i2c_addr = i2c_addr >> 1;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300455
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300456 if (device_rev == DIB7000P || device_rev == DIB8000)
Patrick Boettcher42afd062006-07-29 17:33:44 -0300457 mst->base_reg = 1024;
458 else
459 mst->base_reg = 768;
460
Olivier Grenieb994d192011-01-03 15:39:35 -0300461 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300462 if (i2c_adapter_init
Olivier Grenieb994d192011-01-03 15:39:35 -0300463 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
464 "DiBX000 tuner I2C bus", mst) != 0)
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300465 pr_err("could not initialize the tuner i2c_adapter\n");
Olivier Grenieb994d192011-01-03 15:39:35 -0300466
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)
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300471 pr_err("could not initialize the master i2c_adapter\n");
Olivier Grenieb994d192011-01-03 15:39:35 -0300472
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)
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300477 pr_err("could not initialize the master i2c_adapter\n");
Olivier Grenieb994d192011-01-03 15:39:35 -0300478
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)
Mauro Carvalho Chehab0a6dc892016-10-14 10:28:42 -0300483 pr_err("could not initialize the master i2c_adapter\n");
Patrick Boettcher42afd062006-07-29 17:33:44 -0300484
485 /* initialize the i2c-master by closing the gate */
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300486 dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0);
Patrick Boettcher42afd062006-07-29 17:33:44 -0300487
Patrick Boettcher79fcce32011-08-03 12:08:21 -0300488 ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1);
489 mutex_unlock(&mst->i2c_buffer_lock);
490
491 return ret;
Patrick Boettcher42afd062006-07-29 17:33:44 -0300492}
Patrick Boettcher77e2c0f2009-08-17 07:01:10 -0300493
Patrick Boettcher42afd062006-07-29 17:33:44 -0300494EXPORT_SYMBOL(dibx000_init_i2c_master);
495
496void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
497{
498 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
Olivier Grenieb994d192011-01-03 15:39:35 -0300499 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);
Patrick Boettcher42afd062006-07-29 17:33:44 -0300502}
503EXPORT_SYMBOL(dibx000_exit_i2c_master);
Patrick Boettcherc6d74c22006-08-06 08:49:09 -0300504
Olivier Grenie03245a52009-12-04 13:27:57 -0300505
Randy Dunlap7ccf1ee2010-02-14 23:39:32 -0300506u32 systime(void)
Olivier Grenie03245a52009-12-04 13:27:57 -0300507{
Olivier Grenieb994d192011-01-03 15:39:35 -0300508 struct timespec t;
Olivier Grenie03245a52009-12-04 13:27:57 -0300509
Olivier Grenieb994d192011-01-03 15:39:35 -0300510 t = current_kernel_time();
511 return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
Olivier Grenie03245a52009-12-04 13:27:57 -0300512}
513EXPORT_SYMBOL(systime);
514
Patrick Boettcher99e44da2016-01-24 12:56:58 -0200515MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
Patrick Boettcherc6d74c22006-08-06 08:49:09 -0300516MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
517MODULE_LICENSE("GPL");