blob: 83a6240f64d3d17b1a486bb7dbddac50864ee490 [file] [log] [blame]
Antti Palosaarif9263742012-03-30 06:37:26 -03001/*
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 {
Antti Palosaarie6211c72012-09-11 22:27:10 -030042 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x\n",
43 KBUILD_MODNAME, ret, reg);
Antti Palosaarif9263742012-03-30 06:37:26 -030044 ret = -EREMOTEIO;
45 }
46
47 return ret;
48}
49
50static int tua9001_release(struct dvb_frontend *fe)
51{
Antti Palosaari89054e32012-09-11 22:27:07 -030052 struct tua9001_priv *priv = fe->tuner_priv;
53 int ret = 0;
54
Antti Palosaarie6211c72012-09-11 22:27:10 -030055 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
56
Antti Palosaari89054e32012-09-11 22:27:07 -030057 if (fe->callback)
58 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
59 TUA9001_CMD_CEN, 0);
60
Antti Palosaarif9263742012-03-30 06:37:26 -030061 kfree(fe->tuner_priv);
62 fe->tuner_priv = NULL;
63
Antti Palosaari89054e32012-09-11 22:27:07 -030064 return ret;
Antti Palosaarif9263742012-03-30 06:37:26 -030065}
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
Antti Palosaarie6211c72012-09-11 22:27:10 -030090 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
91
Antti Palosaari89054e32012-09-11 22:27:07 -030092 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
Antti Palosaarif9263742012-03-30 06:37:26 -030099 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);
Antti Palosaari89054e32012-09-11 22:27:07 -0300104 if (ret < 0)
105 goto err_i2c_gate_ctrl;
Antti Palosaarif9263742012-03-30 06:37:26 -0300106 }
107
Antti Palosaari89054e32012-09-11 22:27:07 -0300108err_i2c_gate_ctrl:
Antti Palosaarif9263742012-03-30 06:37:26 -0300109 if (fe->ops.i2c_gate_ctrl)
110 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
Antti Palosaari89054e32012-09-11 22:27:07 -0300111err:
112 if (ret < 0)
Antti Palosaarie6211c72012-09-11 22:27:10 -0300113 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari89054e32012-09-11 22:27:07 -0300114
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
Antti Palosaarie6211c72012-09-11 22:27:10 -0300123 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
124
Antti Palosaari89054e32012-09-11 22:27:07 -0300125 if (fe->callback)
126 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
127 TUA9001_CMD_RESETN, 1);
Antti Palosaarif9263742012-03-30 06:37:26 -0300128
129 if (ret < 0)
Antti Palosaarie6211c72012-09-11 22:27:10 -0300130 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif9263742012-03-30 06:37:26 -0300131
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;
Mauro Carvalho Chehab59e54052012-10-27 16:26:56 -0300139 int ret = 0, i;
Antti Palosaarif9263742012-03-30 06:37:26 -0300140 u16 val;
141 u32 frequency;
142 struct reg_val data[2];
143
Antti Palosaarie6211c72012-09-11 22:27:10 -0300144 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);
Antti Palosaarif9263742012-03-30 06:37:26 -0300147
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
Antti Palosaari89054e32012-09-11 22:27:07 -0300187 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
Antti Palosaarif9263742012-03-30 06:37:26 -0300194 for (i = 0; i < ARRAY_SIZE(data); i++) {
195 ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
196 if (ret < 0)
Antti Palosaari89054e32012-09-11 22:27:07 -0300197 goto err_i2c_gate_ctrl;
Antti Palosaarif9263742012-03-30 06:37:26 -0300198 }
199
Antti Palosaari89054e32012-09-11 22:27:07 -0300200 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:
Antti Palosaarif9263742012-03-30 06:37:26 -0300208 if (fe->ops.i2c_gate_ctrl)
209 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
Antti Palosaarif9263742012-03-30 06:37:26 -0300210err:
211 if (ret < 0)
Antti Palosaarie6211c72012-09-11 22:27:10 -0300212 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif9263742012-03-30 06:37:26 -0300213
214 return ret;
215}
216
217static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
218{
Antti Palosaarie6211c72012-09-11 22:27:10 -0300219 struct tua9001_priv *priv = fe->tuner_priv;
220
221 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
222
Antti Palosaarif9263742012-03-30 06:37:26 -0300223 *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,
Antti Palosaari89054e32012-09-11 22:27:07 -0300240 .sleep = tua9001_sleep,
Antti Palosaarif9263742012-03-30 06:37:26 -0300241 .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;
Antti Palosaari89054e32012-09-11 22:27:07 -0300250 int ret;
Antti Palosaarif9263742012-03-30 06:37:26 -0300251
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
Antti Palosaari89054e32012-09-11 22:27:07 -0300259 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;
Antti Palosaari69504792012-09-16 22:26:55 -0300264
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;
Antti Palosaari89054e32012-09-11 22:27:07 -0300274 }
275
Antti Palosaarie6211c72012-09-11 22:27:10 -0300276 dev_info(&priv->i2c->dev,
277 "%s: Infineon TUA 9001 successfully attached\n",
278 KBUILD_MODNAME);
Antti Palosaarif9263742012-03-30 06:37:26 -0300279
280 memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
281 sizeof(struct dvb_tuner_ops));
282
283 fe->tuner_priv = priv;
284 return fe;
Antti Palosaari89054e32012-09-11 22:27:07 -0300285err:
Antti Palosaarie6211c72012-09-11 22:27:10 -0300286 dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari89054e32012-09-11 22:27:07 -0300287 kfree(priv);
288 return NULL;
Antti Palosaarif9263742012-03-30 06:37:26 -0300289}
290EXPORT_SYMBOL(tua9001_attach);
291
292MODULE_DESCRIPTION("Infineon TUA 9001 silicon tuner driver");
293MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
294MODULE_LICENSE("GPL");