1/*
2 * Infineon TUA 9001 silicon tuner driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 *
6 *    This program is free software; you can redistribute it and/or modify
7 *    it under the terms of the GNU General Public License as published by
8 *    the Free Software Foundation; either version 2 of the License, or
9 *    (at your option) any later version.
10 *
11 *    This program is distributed in the hope that it will be useful,
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *    GNU General Public License for more details.
15 *
16 *    You should have received a copy of the GNU General Public License along
17 *    with this program; if not, write to the Free Software Foundation, Inc.,
18 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "tua9001.h"
22#include "tua9001_priv.h"
23
24/* write register */
25static int tua9001_wr_reg(struct tua9001_priv *priv, u8 reg, u16 val)
26{
27	int ret;
28	u8 buf[3] = { reg, (val >> 8) & 0xff, (val >> 0) & 0xff };
29	struct i2c_msg msg[1] = {
30		{
31			.addr = priv->cfg->i2c_addr,
32			.flags = 0,
33			.len = sizeof(buf),
34			.buf = buf,
35		}
36	};
37
38	ret = i2c_transfer(priv->i2c, msg, 1);
39	if (ret == 1) {
40		ret = 0;
41	} else {
42		dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x\n",
43				KBUILD_MODNAME, ret, reg);
44		ret = -EREMOTEIO;
45	}
46
47	return ret;
48}
49
50static int tua9001_release(struct dvb_frontend *fe)
51{
52	struct tua9001_priv *priv = fe->tuner_priv;
53	int ret = 0;
54
55	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
56
57	if (fe->callback)
58		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
59				TUA9001_CMD_CEN, 0);
60
61	kfree(fe->tuner_priv);
62	fe->tuner_priv = NULL;
63
64	return ret;
65}
66
67static int tua9001_init(struct dvb_frontend *fe)
68{
69	struct tua9001_priv *priv = fe->tuner_priv;
70	int ret = 0;
71	u8 i;
72	struct reg_val data[] = {
73		{ 0x1e, 0x6512 },
74		{ 0x25, 0xb888 },
75		{ 0x39, 0x5460 },
76		{ 0x3b, 0x00c0 },
77		{ 0x3a, 0xf000 },
78		{ 0x08, 0x0000 },
79		{ 0x32, 0x0030 },
80		{ 0x41, 0x703a },
81		{ 0x40, 0x1c78 },
82		{ 0x2c, 0x1c00 },
83		{ 0x36, 0xc013 },
84		{ 0x37, 0x6f18 },
85		{ 0x27, 0x0008 },
86		{ 0x2a, 0x0001 },
87		{ 0x34, 0x0a40 },
88	};
89
90	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
91
92	if (fe->callback) {
93		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
94				TUA9001_CMD_RESETN, 0);
95		if (ret < 0)
96			goto err;
97	}
98
99	if (fe->ops.i2c_gate_ctrl)
100		fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
101
102	for (i = 0; i < ARRAY_SIZE(data); i++) {
103		ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
104		if (ret < 0)
105			goto err_i2c_gate_ctrl;
106	}
107
108err_i2c_gate_ctrl:
109	if (fe->ops.i2c_gate_ctrl)
110		fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
111err:
112	if (ret < 0)
113		dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
114
115	return ret;
116}
117
118static int tua9001_sleep(struct dvb_frontend *fe)
119{
120	struct tua9001_priv *priv = fe->tuner_priv;
121	int ret = 0;
122
123	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
124
125	if (fe->callback)
126		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
127				TUA9001_CMD_RESETN, 1);
128
129	if (ret < 0)
130		dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
131
132	return ret;
133}
134
135static int tua9001_set_params(struct dvb_frontend *fe)
136{
137	struct tua9001_priv *priv = fe->tuner_priv;
138	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
139	int ret = 0, i;
140	u16 val;
141	u32 frequency;
142	struct reg_val data[2];
143
144	dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d frequency=%d " \
145			"bandwidth_hz=%d\n", __func__,
146			c->delivery_system, c->frequency, c->bandwidth_hz);
147
148	switch (c->delivery_system) {
149	case SYS_DVBT:
150		switch (c->bandwidth_hz) {
151		case 8000000:
152			val  = 0x0000;
153			break;
154		case 7000000:
155			val  = 0x1000;
156			break;
157		case 6000000:
158			val  = 0x2000;
159			break;
160		case 5000000:
161			val  = 0x3000;
162			break;
163		default:
164			ret = -EINVAL;
165			goto err;
166		}
167		break;
168	default:
169		ret = -EINVAL;
170		goto err;
171	}
172
173	data[0].reg = 0x04;
174	data[0].val = val;
175
176	frequency = (c->frequency - 150000000);
177	frequency /= 100;
178	frequency *= 48;
179	frequency /= 10000;
180
181	data[1].reg = 0x1f;
182	data[1].val = frequency;
183
184	if (fe->ops.i2c_gate_ctrl)
185		fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
186
187	if (fe->callback) {
188		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
189				TUA9001_CMD_RXEN, 0);
190		if (ret < 0)
191			goto err_i2c_gate_ctrl;
192	}
193
194	for (i = 0; i < ARRAY_SIZE(data); i++) {
195		ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
196		if (ret < 0)
197			goto err_i2c_gate_ctrl;
198	}
199
200	if (fe->callback) {
201		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
202				TUA9001_CMD_RXEN, 1);
203		if (ret < 0)
204			goto err_i2c_gate_ctrl;
205	}
206
207err_i2c_gate_ctrl:
208	if (fe->ops.i2c_gate_ctrl)
209		fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
210err:
211	if (ret < 0)
212		dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
213
214	return ret;
215}
216
217static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
218{
219	struct tua9001_priv *priv = fe->tuner_priv;
220
221	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
222
223	*frequency = 0; /* Zero-IF */
224
225	return 0;
226}
227
228static const struct dvb_tuner_ops tua9001_tuner_ops = {
229	.info = {
230		.name           = "Infineon TUA 9001",
231
232		.frequency_min  = 170000000,
233		.frequency_max  = 862000000,
234		.frequency_step = 0,
235	},
236
237	.release = tua9001_release,
238
239	.init = tua9001_init,
240	.sleep = tua9001_sleep,
241	.set_params = tua9001_set_params,
242
243	.get_if_frequency = tua9001_get_if_frequency,
244};
245
246struct dvb_frontend *tua9001_attach(struct dvb_frontend *fe,
247		struct i2c_adapter *i2c, struct tua9001_config *cfg)
248{
249	struct tua9001_priv *priv = NULL;
250	int ret;
251
252	priv = kzalloc(sizeof(struct tua9001_priv), GFP_KERNEL);
253	if (priv == NULL)
254		return NULL;
255
256	priv->cfg = cfg;
257	priv->i2c = i2c;
258
259	if (fe->callback) {
260		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
261				TUA9001_CMD_CEN, 1);
262		if (ret < 0)
263			goto err;
264
265		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
266				TUA9001_CMD_RXEN, 0);
267		if (ret < 0)
268			goto err;
269
270		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
271				TUA9001_CMD_RESETN, 1);
272		if (ret < 0)
273			goto err;
274	}
275
276	dev_info(&priv->i2c->dev,
277			"%s: Infineon TUA 9001 successfully attached\n",
278			KBUILD_MODNAME);
279
280	memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
281			sizeof(struct dvb_tuner_ops));
282
283	fe->tuner_priv = priv;
284	return fe;
285err:
286	dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
287	kfree(priv);
288	return NULL;
289}
290EXPORT_SYMBOL(tua9001_attach);
291
292MODULE_DESCRIPTION("Infineon TUA 9001 silicon tuner driver");
293MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
294MODULE_LICENSE("GPL");
295