summaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb-frontends/dibx000_common.c
blob: 63a4c6a4afb584e7b8f8e68cba761ec2ff180258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// SPDX-License-Identifier: GPL-2.0-only
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/module.h>

#include "dibx000_common.h"

static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");

#define dprintk(fmt, arg...) do {					\
	if (debug)							\
		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
		       __func__, ##arg);				\
} while (0)

static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
{
	int ret;

	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
		dprintk("could not acquire lock\n");
		return -EINVAL;
	}

	mst->i2c_write_buffer[0] = (reg >> 8) & 0xff;
	mst->i2c_write_buffer[1] = reg & 0xff;
	mst->i2c_write_buffer[2] = (val >> 8) & 0xff;
	mst->i2c_write_buffer[3] = val & 0xff;

	memset(mst->msg, 0, sizeof(struct i2c_msg));
	mst->msg[0].addr = mst->i2c_addr;
	mst->msg[0].flags = 0;
	mst->msg[0].buf = mst->i2c_write_buffer;
	mst->msg[0].len = 4;

	ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
	mutex_unlock(&mst->i2c_buffer_lock);

	return ret;
}

static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
{
	u16 ret;

	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
		dprintk("could not acquire lock\n");
		return 0;
	}

	mst->i2c_write_buffer[0] = reg >> 8;
	mst->i2c_write_buffer[1] = reg & 0xff;

	memset(mst->msg, 0, 2 * sizeof(struct i2c_msg));
	mst->msg[0].addr = mst->i2c_addr;
	mst->msg[0].flags = 0;
	mst->msg[0].buf = mst->i2c_write_buffer;
	mst->msg[0].len = 2;
	mst->msg[1].addr = mst->i2c_addr;
	mst->msg[1].flags = I2C_M_RD;
	mst->msg[1].buf = mst->i2c_read_buffer;
	mst->msg[1].len = 2;

	if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
		dprintk("i2c read error on %d\n", reg);

	ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
	mutex_unlock(&mst->i2c_buffer_lock);

	return ret;
}

static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
{
	int i = 100;
	u16 status;

	while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
		;

	/* i2c timed out */
	if (i == 0)
		return -EREMOTEIO;

	/* no acknowledge */
	if ((status & 0x0080) == 0)
		return -EREMOTEIO;

	return 0;
}

static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
{
	u16 data;
	u16 da;
	u16 i;
	u16 txlen = msg->len, len;
	const u8 *b = msg->buf;

	while (txlen) {
		dibx000_read_word(mst, mst->base_reg + 2);

		len = txlen > 8 ? 8 : txlen;
		for (i = 0; i < len; i += 2) {
			data = *b++ << 8;
			if (i+1 < len)
				data |= *b++;
			dibx000_write_word(mst, mst->base_reg, data);
		}
		da = (((u8) (msg->addr))  << 9) |
			(1           << 8) |
			(1           << 7) |
			(0           << 6) |
			(0           << 5) |
			((len & 0x7) << 2) |
			(0           << 1) |
			(0           << 0);

		if (txlen == msg->len)
			da |= 1 << 5; /* start */

		if (txlen-len == 0 && stop)
			da |= 1 << 6; /* stop */

		dibx000_write_word(mst, mst->base_reg+1, da);

		if (dibx000_is_i2c_done(mst) != 0)
			return -EREMOTEIO;
		txlen -= len;
	}

	return 0;
}

static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
{
	u16 da;
	u8 *b = msg->buf;
	u16 rxlen = msg->len, len;

	while (rxlen) {
		len = rxlen > 8 ? 8 : rxlen;
		da = (((u8) (msg->addr)) << 9) |
			(1           << 8) |
			(1           << 7) |
			(0           << 6) |
			(0           << 5) |
			((len & 0x7) << 2) |
			(1           << 1) |
			(0           << 0);

		if (rxlen == msg->len)
			da |= 1 << 5; /* start */

		if (rxlen-len == 0)
			da |= 1 << 6; /* stop */
		dibx000_write_word(mst, mst->base_reg+1, da);

		if (dibx000_is_i2c_done(mst) != 0)
			return -EREMOTEIO;

		rxlen -= len;

		while (len) {
			da = dibx000_read_word(mst, mst->base_reg);
			*b++ = (da >> 8) & 0xff;
			len--;
			if (len >= 1) {
				*b++ =  da   & 0xff;
				len--;
			}
		}
	}

	return 0;
}

int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
{
	struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);

	if (mst->device_rev < DIB7000MC && speed < 235)
		speed = 235;
	return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));

}
EXPORT_SYMBOL(dibx000_i2c_set_speed);

static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
					enum dibx000_i2c_interface intf)
{
	if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
		dprintk("selecting interface: %d\n", intf);
		mst->selected_interface = intf;
		return dibx000_write_word(mst, mst->base_reg + 4, intf);
	}
	return 0;
}

static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
{
	struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
	int msg_index;
	int ret = 0;

	dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
	for (msg_index = 0; msg_index < num; msg_index++) {
		if (msg[msg_index].flags & I2C_M_RD) {
			ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
			if (ret != 0)
				return 0;
		} else {
			ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
			if (ret != 0)
				return 0;
		}
	}

	return num;
}

static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
{
	struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
	int msg_index;
	int ret = 0;

	dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
	for (msg_index = 0; msg_index < num; msg_index++) {
		if (msg[msg_index].flags & I2C_M_RD) {
			ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
			if (ret != 0)
				return 0;
		} else {
			ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
			if (ret != 0)
				return 0;
		}
	}

	return num;
}

static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
	.master_xfer = dibx000_i2c_master_xfer_gpio12,
	.functionality = dibx000_i2c_func,
};

static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
	.master_xfer = dibx000_i2c_master_xfer_gpio34,
	.functionality = dibx000_i2c_func,
};

static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
				 u8 addr, int onoff)
{
	u16 val;


	if (onoff)
		val = addr << 8;	// bit 7 = use master or not, if 0, the gate is open
	else
		val = 1 << 7;

	if (mst->device_rev > DIB7000)
		val <<= 1;

	tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
	tx[1] = ((mst->base_reg + 1) & 0xff);
	tx[2] = val >> 8;
	tx[3] = val & 0xff;

	return 0;
}

static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
					struct i2c_msg msg[], int num)
{
	struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
	int ret;

	if (num > 32) {
		dprintk("%s: too much I2C message to be transmitted (%i). Maximum is 32",
			__func__, num);
		return -ENOMEM;
	}

	dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);

	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
		dprintk("could not acquire lock\n");
		return -EINVAL;
	}

	memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));

	/* open the gate */
	dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
	mst->msg[0].addr = mst->i2c_addr;
	mst->msg[0].buf = &mst->i2c_write_buffer[0];
	mst->msg[0].len = 4;

	memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);

	/* close the gate */
	dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
	mst->msg[num + 1].addr = mst->i2c_addr;
	mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
	mst->msg[num + 1].len = 4;

	ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
			num : -EIO);

	mutex_unlock(&mst->i2c_buffer_lock);
	return ret;
}

static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
	.master_xfer = dibx000_i2c_gated_gpio67_xfer,
	.functionality = dibx000_i2c_func,
};

static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
					struct i2c_msg msg[], int num)
{
	struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
	int ret;

	if (num > 32) {
		dprintk("%s: too much I2C message to be transmitted (%i). Maximum is 32",
			__func__, num);
		return -ENOMEM;
	}

	dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);

	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
		dprintk("could not acquire lock\n");
		return -EINVAL;
	}
	memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));

	/* open the gate */
	dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
	mst->msg[0].addr = mst->i2c_addr;
	mst->msg[0].buf = &mst->i2c_write_buffer[0];
	mst->msg[0].len = 4;

	memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);

	/* close the gate */
	dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
	mst->msg[num + 1].addr = mst->i2c_addr;
	mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
	mst->msg[num + 1].len = 4;

	ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
			num : -EIO);
	mutex_unlock(&mst->i2c_buffer_lock);
	return ret;
}

static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
	.master_xfer = dibx000_i2c_gated_tuner_xfer,
	.functionality = dibx000_i2c_func,
};

struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
						enum dibx000_i2c_interface intf,
						int gating)
{
	struct i2c_adapter *i2c = NULL;

	switch (intf) {
	case DIBX000_I2C_INTERFACE_TUNER:
		if (gating)
			i2c = &mst->gated_tuner_i2c_adap;
		break;
	case DIBX000_I2C_INTERFACE_GPIO_1_2:
		if (!gating)
			i2c = &mst->master_i2c_adap_gpio12;
		break;
	case DIBX000_I2C_INTERFACE_GPIO_3_4:
		if (!gating)
			i2c = &mst->master_i2c_adap_gpio34;
		break;
	case DIBX000_I2C_INTERFACE_GPIO_6_7:
		if (gating)
			i2c = &mst->master_i2c_adap_gpio67;
		break;
	default:
		pr_err("incorrect I2C interface selected\n");
		break;
	}

	return i2c;
}

EXPORT_SYMBOL(dibx000_get_i2c_adapter);

void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
{
	/* initialize the i2c-master by closing the gate */
	u8 tx[4];
	struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };

	dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
	i2c_transfer(mst->i2c_adap, &m, 1);
	mst->selected_interface = 0xff;	// the first time force a select of the I2C
	dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
}

EXPORT_SYMBOL(dibx000_reset_i2c_master);

static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
				struct i2c_algorithm *algo, const char *name,
				struct dibx000_i2c_master *mst)
{
	strscpy(i2c_adap->name, name, sizeof(i2c_adap->name));
	i2c_adap->algo = algo;
	i2c_adap->algo_data = NULL;
	i2c_set_adapdata(i2c_adap, mst);
	if (i2c_add_adapter(i2c_adap) < 0)
		return -ENODEV;
	return 0;
}

int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
				struct i2c_adapter *i2c_adap, u8 i2c_addr)
{
	int ret;

	mutex_init(&mst->i2c_buffer_lock);
	if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
		dprintk("could not acquire lock\n");
		return -EINVAL;
	}
	memset(mst->msg, 0, sizeof(struct i2c_msg));
	mst->msg[0].addr = i2c_addr >> 1;
	mst->msg[0].flags = 0;
	mst->msg[0].buf = mst->i2c_write_buffer;
	mst->msg[0].len = 4;

	mst->device_rev = device_rev;
	mst->i2c_adap = i2c_adap;
	mst->i2c_addr = i2c_addr >> 1;

	if (device_rev == DIB7000P || device_rev == DIB8000)
		mst->base_reg = 1024;
	else
		mst->base_reg = 768;

	mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
	if (i2c_adapter_init
			(&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
			 "DiBX000 tuner I2C bus", mst) != 0)
		pr_err("could not initialize the tuner i2c_adapter\n");

	mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
	if (i2c_adapter_init
			(&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
			 "DiBX000 master GPIO12 I2C bus", mst) != 0)
		pr_err("could not initialize the master i2c_adapter\n");

	mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
	if (i2c_adapter_init
			(&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
			 "DiBX000 master GPIO34 I2C bus", mst) != 0)
		pr_err("could not initialize the master i2c_adapter\n");

	mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
	if (i2c_adapter_init
			(&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
			 "DiBX000 master GPIO67 I2C bus", mst) != 0)
		pr_err("could not initialize the master i2c_adapter\n");

	/* initialize the i2c-master by closing the gate */
	dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0);

	ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1);
	mutex_unlock(&mst->i2c_buffer_lock);

	return ret;
}

EXPORT_SYMBOL(dibx000_init_i2c_master);

void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
{
	i2c_del_adapter(&mst->gated_tuner_i2c_adap);
	i2c_del_adapter(&mst->master_i2c_adap_gpio12);
	i2c_del_adapter(&mst->master_i2c_adap_gpio34);
	i2c_del_adapter(&mst->master_i2c_adap_gpio67);
}
EXPORT_SYMBOL(dibx000_exit_i2c_master);

MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
MODULE_LICENSE("GPL");