]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/dvb/frontends/stv6110x.c
V4L/DVB (11579): Initial go at TT S2-1600
[karo-tx-linux.git] / drivers / media / dvb / frontends / stv6110x.c
1 /*
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
34 static unsigned int verbose;
35 module_param(verbose, int, 0644);
36 MODULE_PARM_DESC(verbose, "Set Verbosity level");
37
38 static u8 stv6110x_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
39
40 static 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         }
56
57         return 0;
58 }
59
60 static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data)
61 {
62         int ret;
63         const struct stv6110x_config *config = stv6110x->config;
64         u8 buf[] = { reg, data };
65         struct i2c_msg msg = { .addr = config->addr, .flags = 0, . buf = buf, .len = 2 };
66
67         ret = i2c_transfer(stv6110x->i2c, &msg, 1);
68         if (ret != 1) {
69                 dprintk(FE_ERROR, 1, "I/O Error");
70                 return -EREMOTEIO;
71         }
72
73         return 0;
74 }
75
76 static int stv6110x_init(struct dvb_frontend *fe)
77 {
78         struct stv6110x_state *stv6110x = fe->tuner_priv;
79         int ret;
80         u8 i;
81
82         for (i = 0; i < ARRAY_SIZE(stv6110x_regs); i++) {
83                 ret = stv6110x_write_reg(stv6110x, i, stv6110x_regs[i]);
84                 if (ret < 0) {
85                         dprintk(FE_ERROR, 1, "Initialization failed");
86                         return -1;
87                 }
88         }
89
90         return 0;
91 }
92
93 static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
94 {
95         struct stv6110x_state *stv6110x = fe->tuner_priv;
96         u32 rDiv, divider;
97         s32 pVal, pCalc, rDivOpt = 0;
98         u8 i;
99
100         STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16));
101
102         if (frequency <= 1023000) {
103                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
104                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
105                 pVal = 40;
106         } else if (frequency <= 1300000) {
107                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
108                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
109                 pVal = 40;
110         } else if (frequency <= 2046000) {
111                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
112                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
113                 pVal = 20;
114         } else {
115                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
116                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
117                 pVal = 20;
118         }
119
120         for (rDiv = 0; rDiv <= 3; rDiv++) {
121                 pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv);
122
123                 if ((abs((s32)(pCalc - pVal))) < (abs((s32)(1000 - pVal))))
124                         rDivOpt = rDiv;
125         }
126
127         divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
128         divider = (divider + 5) / 10;
129
130         STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
131         STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
132         STV6110x_SETFIELD(stv6110x_regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
133
134         /* VCO Auto calibration */
135         STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
136
137         stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
138         stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x_regs[STV6110x_TNG1]);
139         stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x_regs[STV6110x_TNG0]);
140         stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
141
142         for (i = 0; i < TRIALS; i++) {
143                 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
144                 if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x_regs[STV6110x_STAT1]))
145                                 break;
146                 msleep(1);
147         }
148
149         return 0;
150 }
151
152 static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
153 {
154         struct stv6110x_state *stv6110x = fe->tuner_priv;
155
156         stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x_regs[STV6110x_TNG1]);
157         stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x_regs[STV6110x_TNG0]);
158
159         *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x_regs[STV6110x_TNG1]),
160                                  STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x_regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
161
162         *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x_regs[STV6110x_TNG1]) +
163                              STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x_regs[STV6110x_TNG1])));
164
165         *frequency >>= 2;
166
167         return 0;
168 }
169
170 static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
171 {
172         struct stv6110x_state *stv6110x = fe->tuner_priv;
173         u32 halfbw;
174         u8 i;
175
176         halfbw = bandwidth >> 1;
177
178         if (halfbw > 36000000)
179                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
180         else if (halfbw < 5000000)
181                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
182         else
183                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
184
185
186         STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
187         STV6110x_SETFIELD(stv6110x_regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
188
189         stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
190         stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x_regs[STV6110x_STAT1]);
191
192         for (i = 0; i < TRIALS; i++) {
193                 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
194                 if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x_regs[STV6110x_STAT1]))
195                         break;
196                 msleep(1);
197         }
198         STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
199         stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x_regs[STV6110x_CTRL3]);
200
201         return 0;
202 }
203
204 static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
205 {
206         struct stv6110x_state *stv6110x = fe->tuner_priv;
207
208         stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x_regs[STV6110x_CTRL3]);
209         *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x_regs[STV6110x_CTRL3]) + 5) * 2000000;
210
211         return 0;
212 }
213
214 static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
215 {
216         struct stv6110x_state *stv6110x = fe->tuner_priv;
217
218         /* setup divider */
219         switch (refclock) {
220         default:
221         case 1:
222                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
223                 break;
224         case 2:
225                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
226                 break;
227         case 4:
228                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
229                 break;
230         case 8:
231         case 0:
232                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
233                 break;
234         }
235         stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
236
237         return 0;
238 }
239
240 static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
241 {
242         struct stv6110x_state *stv6110x = fe->tuner_priv;
243
244         stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x_regs[STV6110x_CTRL2]);
245         *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x_regs[STV6110x_CTRL2]);
246
247         return 0;
248 }
249
250 static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
251 {
252         struct stv6110x_state *stv6110x = fe->tuner_priv;
253
254         STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
255         stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x_regs[STV6110x_CTRL2]);
256
257         return 0;
258 }
259
260 static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
261 {
262         struct stv6110x_state *stv6110x = fe->tuner_priv;
263         int ret;
264
265         switch (mode) {
266         case TUNER_SLEEP:
267                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 0);
268                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 0);
269                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 0);
270                 break;
271
272         case TUNER_WAKE:
273                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_SYN, 1);
274                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_RX, 1);
275                 STV6110x_SETFIELD(stv6110x_regs[STV6110x_CTRL1], CTRL1_LPT, 1);
276                 break;
277         }
278
279         ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x_regs[STV6110x_CTRL1]);
280         if (ret < 0) {
281                 dprintk(FE_ERROR, 1, "I/O Error");
282                 return -EIO;
283         }
284
285         return 0;
286 }
287
288 static int stv6110x_sleep(struct dvb_frontend *fe)
289 {
290         return stv6110x_set_mode(fe, TUNER_SLEEP);
291 }
292
293 static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
294 {
295         struct stv6110x_state *stv6110x = fe->tuner_priv;
296
297         stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x_regs[STV6110x_STAT1]);
298
299         if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x_regs[STV6110x_STAT1]))
300                 *status = TUNER_PHASELOCKED;
301         else
302                 *status = 0;
303
304         return 0;
305 }
306
307
308 static int stv6110x_release(struct dvb_frontend *fe)
309 {
310         struct stv6110x_state *stv6110x = fe->tuner_priv;
311
312         fe->tuner_priv = NULL;
313         kfree(stv6110x);
314
315         return 0;
316 }
317
318 static struct dvb_tuner_ops stv6110x_ops = {
319         .info = {
320                 .name           = "STV6110(A) Silicon Tuner",
321                 .frequency_min  =  950000,
322                 .frequency_max  = 2150000,
323                 .frequency_step = 0,
324         },
325
326         .init                   = stv6110x_init,
327         .sleep                  = stv6110x_sleep,
328         .release                = stv6110x_release
329 };
330
331 static struct stv6110x_devctl stv6110x_ctl = {
332         .tuner_init             = stv6110x_init,
333         .tuner_set_mode         = stv6110x_set_mode,
334         .tuner_set_frequency    = stv6110x_set_frequency,
335         .tuner_get_frequency    = stv6110x_get_frequency,
336         .tuner_set_bandwidth    = stv6110x_set_bandwidth,
337         .tuner_get_bandwidth    = stv6110x_get_bandwidth,
338         .tuner_set_bbgain       = stv6110x_set_bbgain,
339         .tuner_get_bbgain       = stv6110x_get_bbgain,
340         .tuner_set_refclk       = stv6110x_set_refclock,
341         .tuner_get_status       = stv6110x_get_status,
342 };
343
344 struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
345                                         const struct stv6110x_config *config,
346                                         struct i2c_adapter *i2c)
347 {
348         struct stv6110x_state *stv6110x;
349
350         stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL);
351         if (stv6110x == NULL)
352                 goto error;
353
354         stv6110x->i2c           = i2c;
355         stv6110x->config        = config;
356         stv6110x->devctl        = &stv6110x_ctl;
357
358         fe->tuner_priv          = stv6110x;
359         fe->ops.tuner_ops       = stv6110x_ops;
360
361         printk("%s: Attaching STV6110x \n", __func__);
362         return stv6110x->devctl;
363
364 error:
365         kfree(stv6110x);
366         return NULL;
367 }
368 EXPORT_SYMBOL(stv6110x_attach);
369
370 MODULE_AUTHOR("Manu Abraham");
371 MODULE_DESCRIPTION("STV6110x Silicon tuner");
372 MODULE_LICENSE("GPL");