blob: 09e3aef8073badc6c34ceaee9de3fd19bfc97317 [file] [log] [blame]
Manu Abrahame415c682009-04-06 15:45:20 -03001/*
2 STV6110(A) Silicon tuner driver
3
4 Copyright (C) Manu Abraham <abraham.manu@gmail.com>
5
6 Copyright (C) ST Microelectronics
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include <linux/init.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/string.h>
27
28#include "dvb_frontend.h"
29
30#include "stv6110x_reg.h"
31#include "stv6110x.h"
32#include "stv6110x_priv.h"
33
34static unsigned int verbose;
35module_param(verbose, int, 0644);
36MODULE_PARM_DESC(verbose, "Set Verbosity level");
37
38static u8 stv6110x_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
39
40static int stv6110x_read_reg(struct stv6110x_state *stv6110x, u8 reg, u8 *data)
41{
42 int ret;
43 const struct stv6110x_config *config = stv6110x->config;
44 u8 b0[] = { reg };
45 u8 b1[] = { 0 };
46 struct i2c_msg msg[] = {
47 { .addr = config->addr, .flags = 0, .buf = b0, .len = 1 },
48 { .addr = config->addr, .flags = I2C_M_RD, .buf = b1, .len = 1 }
49 };
50
51 ret = i2c_transfer(stv6110x->i2c, msg, 2);
52 if (ret != 2) {
53 dprintk(FE_ERROR, 1, "I/O Error");
54 return -EREMOTEIO;
55 }
Andreas Regelf2b2fd42009-04-16 08:38:46 -030056 *data = b1[0];
Manu Abrahame415c682009-04-06 15:45:20 -030057
58 return 0;
59}
60
Andreas Regel0c3f9fd2010-01-05 19:22:45 -030061static int stv6110x_write_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len)
Manu Abrahame415c682009-04-06 15:45:20 -030062{
63 int ret;
64 const struct stv6110x_config *config = stv6110x->config;
Andreas Regel0c3f9fd2010-01-05 19:22:45 -030065 u8 buf[len + 1];
66 struct i2c_msg msg = {
67 .addr = config->addr,
68 .flags = 0,
69 .buf = buf,
70 .len = len + 1
71 };
72
73 if (start + len > 8)
74 return -EINVAL;
75
76 buf[0] = start;
77 memcpy(&buf[1], data, len);
Manu Abrahame415c682009-04-06 15:45:20 -030078
79 ret = i2c_transfer(stv6110x->i2c, &msg, 1);
80 if (ret != 1) {
81 dprintk(FE_ERROR, 1, "I/O Error");
82 return -EREMOTEIO;
83 }
84
85 return 0;
86}
87
Andreas Regel0c3f9fd2010-01-05 19:22:45 -030088static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data)
89{
90 return stv6110x_write_regs(stv6110x, reg, &data, 1);
91}
92
Manu Abrahame415c682009-04-06 15:45:20 -030093static int stv6110x_init(struct dvb_frontend *fe)
94{
95 struct stv6110x_state *stv6110x = fe->tuner_priv;
96 int ret;
Manu Abrahame415c682009-04-06 15:45:20 -030097
Andreas Regel0c3f9fd2010-01-05 19:22:45 -030098 ret = stv6110x_write_regs(stv6110x, 0, stv6110x_regs,
99 ARRAY_SIZE(stv6110x_regs));
100 if (ret < 0) {
101 dprintk(FE_ERROR, 1, "Initialization failed");
102 return -1;
Manu Abrahame415c682009-04-06 15:45:20 -0300103 }
104
105 return 0;
106}
107
108static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
109{
110 struct stv6110x_state *stv6110x = fe->tuner_priv;
111 u32 rDiv, divider;
Andreas Regel7c236e32009-11-13 18:22:02 -0300112 s32 pVal, pCalc, rDivOpt = 0, pCalcOpt = 1000;
Manu Abrahame415c682009-04-06 15:45:20 -0300113 u8 i;
114
115 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16));
116
117 if (frequency <= 1023000) {
118 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
119 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
120 pVal = 40;
121 } else if (frequency <= 1300000) {
122 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
123 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
124 pVal = 40;
125 } else if (frequency <= 2046000) {
126 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
127 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
128 pVal = 20;
129 } else {
130 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
131 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
132 pVal = 20;
133 }
134
135 for (rDiv = 0; rDiv <= 3; rDiv++) {
136 pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv);
137
Andreas Regel7c236e32009-11-13 18:22:02 -0300138 if ((abs((s32)(pCalc - pVal))) < (abs((s32)(pCalcOpt - pVal))))
Manu Abrahame415c682009-04-06 15:45:20 -0300139 rDivOpt = rDiv;
Andreas Regel7c236e32009-11-13 18:22:02 -0300140
141 pCalcOpt = (REFCLOCK_kHz / 100) / R_DIV(rDivOpt);
Manu Abrahame415c682009-04-06 15:45:20 -0300142 }
143
144 divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
145 divider = (divider + 5) / 10;
146
147 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
148 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
149 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
150
151 /* VCO Auto calibration */
152 STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
153
154 stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
155 stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x_regs[STV6110x_TNG1]);
156 stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x_regs[STV6110x_TNG0]);
157 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
158
159 for (i = 0; i < TRIALS; i++) {
160 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
161 if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x_regs[STV6110x_STAT1]))
162 break;
163 msleep(1);
164 }
165
166 return 0;
167}
168
169static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
170{
171 struct stv6110x_state *stv6110x = fe->tuner_priv;
172
173 stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x_regs[STV6110x_TNG1]);
174 stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x_regs[STV6110x_TNG0]);
175
176 *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x_regs[STV6110x_TNG1]),
177 STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x_regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
178
179 *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x_regs[STV6110x_TNG1]) +
180 STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x_regs[STV6110x_TNG1])));
181
182 *frequency >>= 2;
183
184 return 0;
185}
186
187static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
188{
189 struct stv6110x_state *stv6110x = fe->tuner_priv;
190 u32 halfbw;
191 u8 i;
192
193 halfbw = bandwidth >> 1;
194
195 if (halfbw > 36000000)
196 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
197 else if (halfbw < 5000000)
198 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
199 else
200 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
201
202
203 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
204 STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
205
206 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
207 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
208
209 for (i = 0; i < TRIALS; i++) {
210 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
211 if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x_regs[STV6110x_STAT1]))
212 break;
213 msleep(1);
214 }
215 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
216 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
217
218 return 0;
219}
220
221static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
222{
223 struct stv6110x_state *stv6110x = fe->tuner_priv;
224
225 stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x_regs[STV6110x_CTRL3]);
226 *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x_regs[STV6110x_CTRL3]) + 5) * 2000000;
227
228 return 0;
229}
230
231static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
232{
233 struct stv6110x_state *stv6110x = fe->tuner_priv;
234
235 /* setup divider */
236 switch (refclock) {
237 default:
238 case 1:
239 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
240 break;
241 case 2:
242 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
243 break;
244 case 4:
245 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
246 break;
247 case 8:
248 case 0:
249 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
250 break;
251 }
252 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
253
254 return 0;
255}
256
257static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
258{
259 struct stv6110x_state *stv6110x = fe->tuner_priv;
260
261 stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x_regs[STV6110x_CTRL2]);
262 *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x_regs[STV6110x_CTRL2]);
263
264 return 0;
265}
266
267static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
268{
269 struct stv6110x_state *stv6110x = fe->tuner_priv;
270
271 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
272 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
273
274 return 0;
275}
276
277static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
278{
279 struct stv6110x_state *stv6110x = fe->tuner_priv;
280 int ret;
281
282 switch (mode) {
283 case TUNER_SLEEP:
284 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 0);
285 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 0);
286 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 0);
287 break;
288
289 case TUNER_WAKE:
290 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 1);
291 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 1);
292 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 1);
293 break;
294 }
295
296 ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
297 if (ret < 0) {
298 dprintk(FE_ERROR, 1, "I/O Error");
299 return -EIO;
300 }
301
302 return 0;
303}
304
305static int stv6110x_sleep(struct dvb_frontend *fe)
306{
307 return stv6110x_set_mode(fe, TUNER_SLEEP);
308}
309
310static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
311{
312 struct stv6110x_state *stv6110x = fe->tuner_priv;
313
314 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
315
316 if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x_regs[STV6110x_STAT1]))
317 *status = TUNER_PHASELOCKED;
318 else
319 *status = 0;
320
321 return 0;
322}
323
324
325static int stv6110x_release(struct dvb_frontend *fe)
326{
327 struct stv6110x_state *stv6110x = fe->tuner_priv;
328
329 fe->tuner_priv = NULL;
330 kfree(stv6110x);
331
332 return 0;
333}
334
335static struct dvb_tuner_ops stv6110x_ops = {
336 .info = {
337 .name = "STV6110(A) Silicon Tuner",
338 .frequency_min = 950000,
339 .frequency_max = 2150000,
340 .frequency_step = 0,
341 },
342
343 .init = stv6110x_init,
344 .sleep = stv6110x_sleep,
345 .release = stv6110x_release
346};
347
348static struct stv6110x_devctl stv6110x_ctl = {
349 .tuner_init = stv6110x_init,
350 .tuner_set_mode = stv6110x_set_mode,
351 .tuner_set_frequency = stv6110x_set_frequency,
352 .tuner_get_frequency = stv6110x_get_frequency,
353 .tuner_set_bandwidth = stv6110x_set_bandwidth,
354 .tuner_get_bandwidth = stv6110x_get_bandwidth,
355 .tuner_set_bbgain = stv6110x_set_bbgain,
356 .tuner_get_bbgain = stv6110x_get_bbgain,
357 .tuner_set_refclk = stv6110x_set_refclock,
358 .tuner_get_status = stv6110x_get_status,
359};
360
361struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
362 const struct stv6110x_config *config,
363 struct i2c_adapter *i2c)
364{
365 struct stv6110x_state *stv6110x;
366
367 stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL);
368 if (stv6110x == NULL)
369 goto error;
370
371 stv6110x->i2c = i2c;
372 stv6110x->config = config;
373 stv6110x->devctl = &stv6110x_ctl;
374
375 fe->tuner_priv = stv6110x;
376 fe->ops.tuner_ops = stv6110x_ops;
377
378 printk("%s: Attaching STV6110x \n", __func__);
379 return stv6110x->devctl;
380
381error:
382 kfree(stv6110x);
383 return NULL;
384}
385EXPORT_SYMBOL(stv6110x_attach);
386
387MODULE_AUTHOR("Manu Abraham");
388MODULE_DESCRIPTION("STV6110x Silicon tuner");
389MODULE_LICENSE("GPL");