1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2 
3 #include <linux/i2c.h>
4 #include <linux/mutex.h>
5 #include <linux/module.h>
6 
7 #include "dibx000_common.h"
8 
9 static int debug;
10 module_param(debug, int, 0644);
11 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
12 
13 #define dprintk(fmt, arg...) do {					\
14 	if (debug)							\
15 		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
16 		       __func__, ##arg);				\
17 } while (0)
18 
19 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
20 {
21 	int ret;
22 
23 	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
24 		dprintk("could not acquire lock\n");
25 		return -EINVAL;
26 	}
27 
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;
32 
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;
38 
39 	ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
40 	mutex_unlock(&mst->i2c_buffer_lock);
41 
42 	return ret;
43 }
44 
45 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
46 {
47 	u16 ret;
48 
49 	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
50 		dprintk("could not acquire lock\n");
51 		return 0;
52 	}
53 
54 	mst->i2c_write_buffer[0] = reg >> 8;
55 	mst->i2c_write_buffer[1] = reg & 0xff;
56 
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;
66 
67 	if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
68 		dprintk("i2c read error on %d\n", reg);
69 
70 	ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
71 	mutex_unlock(&mst->i2c_buffer_lock);
72 
73 	return ret;
74 }
75 
76 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
77 {
78 	int i = 100;
79 	u16 status;
80 
81 	while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
82 		;
83 
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 
95 static 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) {
104 		dibx000_read_word(mst, mst->base_reg + 2);
105 
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 		}
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);
121 
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 
138 static 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;
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);
154 
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 
181 int 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 }
190 EXPORT_SYMBOL(dibx000_i2c_set_speed);
191 
192 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
193 {
194 	return I2C_FUNC_I2C;
195 }
196 
197 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
198 					enum dibx000_i2c_interface intf)
199 {
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);
204 	}
205 	return 0;
206 }
207 
208 static 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);
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;
224 		}
225 	}
226 
227 	return num;
228 }
229 
230 static 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);
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;
246 		}
247 	}
248 
249 	return num;
250 }
251 
252 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
253 	.master_xfer = dibx000_i2c_master_xfer_gpio12,
254 	.functionality = dibx000_i2c_func,
255 };
256 
257 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
258 	.master_xfer = dibx000_i2c_master_xfer_gpio34,
259 	.functionality = dibx000_i2c_func,
260 };
261 
262 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
263 				 u8 addr, int onoff)
264 {
265 	u16 val;
266 
267 
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;
272 
273 	if (mst->device_rev > DIB7000)
274 		val <<= 1;
275 
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;
280 
281 	return 0;
282 }
283 
284 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
285 					struct i2c_msg msg[], int num)
286 {
287 	struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
288 	int ret;
289 
290 	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 
296 	dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
297 
298 	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
299 		dprintk("could not acquire lock\n");
300 		return -EINVAL;
301 	}
302 
303 	memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
304 
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;
310 
311 	memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
312 
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;
318 
319 	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;
324 }
325 
326 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
327 	.master_xfer = dibx000_i2c_gated_gpio67_xfer,
328 	.functionality = dibx000_i2c_func,
329 };
330 
331 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
332 					struct i2c_msg msg[], int num)
333 {
334 	struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
335 	int ret;
336 
337 	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 
343 	dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
344 
345 	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
346 		dprintk("could not acquire lock\n");
347 		return -EINVAL;
348 	}
349 	memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
350 
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;
356 
357 	memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
358 
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;
364 
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;
369 }
370 
371 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
372 	.master_xfer = dibx000_i2c_gated_tuner_xfer,
373 	.functionality = dibx000_i2c_func,
374 };
375 
376 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
377 						enum dibx000_i2c_interface intf,
378 						int gating)
379 {
380 	struct i2c_adapter *i2c = NULL;
381 
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;
402 	}
403 
404 	return i2c;
405 }
406 
407 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
408 
409 void 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 
421 EXPORT_SYMBOL(dibx000_reset_i2c_master);
422 
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)
426 {
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;
434 }
435 
436 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
437 				struct i2c_adapter *i2c_adap, u8 i2c_addr)
438 {
439 	int ret;
440 
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;
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;
451 
452 	mst->device_rev = device_rev;
453 	mst->i2c_adap = i2c_adap;
454 	mst->i2c_addr = i2c_addr >> 1;
455 
456 	if (device_rev == DIB7000P || device_rev == DIB8000)
457 		mst->base_reg = 1024;
458 	else
459 		mst->base_reg = 768;
460 
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");
466 
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");
472 
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");
478 
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");
484 
485 	/* initialize the i2c-master by closing the gate */
486 	dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0);
487 
488 	ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1);
489 	mutex_unlock(&mst->i2c_buffer_lock);
490 
491 	return ret;
492 }
493 
494 EXPORT_SYMBOL(dibx000_init_i2c_master);
495 
496 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
497 {
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);
502 }
503 EXPORT_SYMBOL(dibx000_exit_i2c_master);
504 
505 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
506 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
507 MODULE_LICENSE("GPL");
508