summaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb-frontends/tua6100.c
blob: 2483f614d0e7df2cdf95ac3fd9d8a1e69db3d1da (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Driver for Infineon tua6100 pll.
 *
 * (c) 2006 Andrew de Quincey
 *
 * Based on code found in budget-av.c, which has the following:
 * Compiled from various sources by Michael Hunold <michael@mihu.de>
 *
 * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
 *                               Andrew de Quincey <adq_dvb@lidskialf.net>
 *
 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
 *
 * Copyright (C) 1999-2002 Ralph  Metzler
 *                       & Marcus Metzler for convergence integrated media GmbH
 */

#include <linux/slab.h>
#include <linux/module.h>
#include <linux/dvb/frontend.h>
#include <asm/types.h>

#include "tua6100.h"

struct tua6100_priv {
	/* i2c details */
	int i2c_address;
	struct i2c_adapter *i2c;
	u32 frequency;
};

static void tua6100_release(struct dvb_frontend *fe)
{
	kfree(fe->tuner_priv);
	fe->tuner_priv = NULL;
}

static int tua6100_sleep(struct dvb_frontend *fe)
{
	struct tua6100_priv *priv = fe->tuner_priv;
	int ret;
	u8 reg0[] = { 0x00, 0x00 };
	struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
	if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
		printk("%s: i2c error\n", __func__);
	}
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 0);

	return (ret == 1) ? 0 : ret;
}

static int tua6100_set_params(struct dvb_frontend *fe)
{
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
	struct tua6100_priv *priv = fe->tuner_priv;
	u32 div;
	u32 prediv;
	u8 reg0[] = { 0x00, 0x00 };
	u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
	u8 reg2[] = { 0x02, 0x00, 0x00 };
	struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
	struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
	struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };

#define _R_VAL 4
#define _P_VAL 32
#define _ri 4000000

	// setup register 0
	if (c->frequency < 2000000)
		reg0[1] = 0x03;
	else
		reg0[1] = 0x07;

	// setup register 1
	if (c->frequency < 1630000)
		reg1[1] = 0x2c;
	else
		reg1[1] = 0x0c;

	if (_P_VAL == 64)
		reg1[1] |= 0x40;
	if (c->frequency >= 1525000)
		reg1[1] |= 0x80;

	// register 2
	reg2[1] = (_R_VAL >> 8) & 0x03;
	reg2[2] = _R_VAL;
	if (c->frequency < 1455000)
		reg2[1] |= 0x1c;
	else if (c->frequency < 1630000)
		reg2[1] |= 0x0c;
	else
		reg2[1] |= 0x1c;

	/*
	 * The N divisor ratio (note: c->frequency is in kHz, but we
	 * need it in Hz)
	 */
	prediv = (c->frequency * _R_VAL) / (_ri / 1000);
	div = prediv / _P_VAL;
	reg1[1] |= (div >> 9) & 0x03;
	reg1[2] = div >> 1;
	reg1[3] = (div << 7);
	priv->frequency = ((div * _P_VAL) * (_ri / 1000)) / _R_VAL;

	// Finally, calculate and store the value for A
	reg1[3] |= (prediv - (div*_P_VAL)) & 0x7f;

#undef _R_VAL
#undef _P_VAL
#undef _ri

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
	if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
		return -EIO;

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
	if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
		return -EIO;

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
	if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
		return -EIO;

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 0);

	return 0;
}

static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
{
	struct tua6100_priv *priv = fe->tuner_priv;
	*frequency = priv->frequency;
	return 0;
}

static const struct dvb_tuner_ops tua6100_tuner_ops = {
	.info = {
		.name = "Infineon TUA6100",
		.frequency_min_hz  =  950 * MHz,
		.frequency_max_hz  = 2150 * MHz,
		.frequency_step_hz =    1 * MHz,
	},
	.release = tua6100_release,
	.sleep = tua6100_sleep,
	.set_params = tua6100_set_params,
	.get_frequency = tua6100_get_frequency,
};

struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
{
	struct tua6100_priv *priv = NULL;
	u8 b1 [] = { 0x80 };
	u8 b2 [] = { 0x00 };
	struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
				  { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
	int ret;

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
	ret = i2c_transfer (i2c, msg, 2);
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 0);

	if (ret != 2)
		return NULL;

	priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
	if (priv == NULL)
		return NULL;

	priv->i2c_address = addr;
	priv->i2c = i2c;

	memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
	fe->tuner_priv = priv;
	return fe;
}
EXPORT_SYMBOL(tua6100_attach);

MODULE_DESCRIPTION("DVB tua6100 driver");
MODULE_AUTHOR("Andrew de Quincey");
MODULE_LICENSE("GPL");