Commit | Line | Data |
---|---|---|
47220bc1 IL |
1 | /* |
2 | * stv6110.c | |
3 | * | |
4 | * Driver for ST STV6110 satellite tuner IC. | |
5 | * | |
6 | * Copyright (C) 2009 NetUP Inc. | |
7 | * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | */ | |
24 | ||
25 | #include <linux/module.h> | |
26 | #include <linux/dvb/frontend.h> | |
27 | ||
28 | #include <linux/types.h> | |
29 | ||
30 | #include "stv6110.h" | |
31 | ||
32 | static int debug; | |
33 | ||
34 | struct stv6110_priv { | |
35 | int i2c_address; | |
36 | struct i2c_adapter *i2c; | |
37 | ||
38 | u32 mclk; | |
92782bb0 | 39 | u8 clk_div; |
873688cd | 40 | u8 gain; |
47220bc1 IL |
41 | u8 regs[8]; |
42 | }; | |
43 | ||
44 | #define dprintk(args...) \ | |
45 | do { \ | |
46 | if (debug) \ | |
47 | printk(KERN_DEBUG args); \ | |
48 | } while (0) | |
49 | ||
50 | static s32 abssub(s32 a, s32 b) | |
51 | { | |
52 | if (a > b) | |
53 | return a - b; | |
54 | else | |
55 | return b - a; | |
56 | }; | |
57 | ||
58 | static int stv6110_release(struct dvb_frontend *fe) | |
59 | { | |
60 | kfree(fe->tuner_priv); | |
61 | fe->tuner_priv = NULL; | |
62 | return 0; | |
63 | } | |
64 | ||
65 | static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[], | |
66 | int start, int len) | |
67 | { | |
68 | struct stv6110_priv *priv = fe->tuner_priv; | |
69 | int rc; | |
70 | u8 cmdbuf[len + 1]; | |
71 | struct i2c_msg msg = { | |
72 | .addr = priv->i2c_address, | |
73 | .flags = 0, | |
74 | .buf = cmdbuf, | |
75 | .len = len + 1 | |
76 | }; | |
77 | ||
78 | dprintk("%s\n", __func__); | |
79 | ||
80 | if (start + len > 8) | |
81 | return -EINVAL; | |
82 | ||
83 | memcpy(&cmdbuf[1], buf, len); | |
84 | cmdbuf[0] = start; | |
85 | ||
86 | if (fe->ops.i2c_gate_ctrl) | |
87 | fe->ops.i2c_gate_ctrl(fe, 1); | |
88 | ||
89 | rc = i2c_transfer(priv->i2c, &msg, 1); | |
90 | if (rc != 1) | |
91 | dprintk("%s: i2c error\n", __func__); | |
92 | ||
93 | if (fe->ops.i2c_gate_ctrl) | |
94 | fe->ops.i2c_gate_ctrl(fe, 0); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[], | |
100 | int start, int len) | |
101 | { | |
102 | struct stv6110_priv *priv = fe->tuner_priv; | |
103 | int rc; | |
104 | u8 reg[] = { start }; | |
c8461210 IL |
105 | struct i2c_msg msg[] = { |
106 | { | |
107 | .addr = priv->i2c_address, | |
108 | .flags = 0, | |
109 | .buf = reg, | |
110 | .len = 1, | |
111 | }, { | |
112 | .addr = priv->i2c_address, | |
113 | .flags = I2C_M_RD, | |
114 | .buf = regs, | |
115 | .len = len, | |
116 | }, | |
47220bc1 IL |
117 | }; |
118 | ||
47220bc1 IL |
119 | if (fe->ops.i2c_gate_ctrl) |
120 | fe->ops.i2c_gate_ctrl(fe, 1); | |
121 | ||
c8461210 IL |
122 | rc = i2c_transfer(priv->i2c, msg, 2); |
123 | if (rc != 2) | |
47220bc1 IL |
124 | dprintk("%s: i2c error\n", __func__); |
125 | ||
126 | if (fe->ops.i2c_gate_ctrl) | |
127 | fe->ops.i2c_gate_ctrl(fe, 0); | |
128 | ||
129 | memcpy(&priv->regs[start], regs, len); | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
134 | static int stv6110_read_reg(struct dvb_frontend *fe, int start) | |
135 | { | |
136 | u8 buf[] = { 0 }; | |
137 | stv6110_read_regs(fe, buf, start, 1); | |
138 | ||
139 | return buf[0]; | |
140 | } | |
141 | ||
142 | static int stv6110_sleep(struct dvb_frontend *fe) | |
143 | { | |
144 | u8 reg[] = { 0 }; | |
145 | stv6110_write_regs(fe, reg, 0, 1); | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | static u32 carrier_width(u32 symbol_rate, fe_rolloff_t rolloff) | |
151 | { | |
152 | u32 rlf; | |
153 | ||
154 | switch (rolloff) { | |
155 | case ROLLOFF_20: | |
156 | rlf = 20; | |
157 | break; | |
158 | case ROLLOFF_25: | |
159 | rlf = 25; | |
160 | break; | |
161 | default: | |
162 | rlf = 35; | |
163 | break; | |
164 | } | |
165 | ||
166 | return symbol_rate + ((symbol_rate * rlf) / 100); | |
167 | } | |
168 | ||
169 | static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth) | |
170 | { | |
171 | struct stv6110_priv *priv = fe->tuner_priv; | |
172 | u8 r8, ret = 0x04; | |
173 | int i; | |
174 | ||
175 | if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/ | |
176 | r8 = 31; | |
177 | else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */ | |
178 | r8 = 0; | |
179 | else /*if 5 < BW/2 < 36*/ | |
180 | r8 = (bandwidth / 2) / 1000000 - 5; | |
181 | ||
182 | /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */ | |
183 | /* ctrl3, CF = r8 Set the LPF value */ | |
184 | priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f); | |
185 | priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f); | |
186 | stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1); | |
187 | /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/ | |
188 | priv->regs[RSTV6110_STAT1] |= 0x02; | |
189 | stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1); | |
190 | ||
191 | i = 0; | |
192 | /* Wait for CALRCSTRT == 0 */ | |
193 | while ((i < 10) && (ret != 0)) { | |
194 | ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02); | |
195 | mdelay(1); /* wait for LPF auto calibration */ | |
196 | i++; | |
197 | } | |
198 | ||
199 | /* RCCLKOFF = 1 calibration done, desactivate the calibration Clock */ | |
200 | priv->regs[RSTV6110_CTRL3] |= (1 << 6); | |
201 | stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1); | |
202 | return 0; | |
203 | } | |
204 | ||
205 | static int stv6110_init(struct dvb_frontend *fe) | |
206 | { | |
207 | struct stv6110_priv *priv = fe->tuner_priv; | |
208 | u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e }; | |
209 | ||
210 | memcpy(priv->regs, buf0, 8); | |
211 | /* K = (Reference / 1000000) - 16 */ | |
212 | priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3); | |
213 | priv->regs[RSTV6110_CTRL1] |= | |
214 | ((((priv->mclk / 1000000) - 16) & 0x1f) << 3); | |
215 | ||
92782bb0 IL |
216 | /* divisor value for the output clock */ |
217 | priv->regs[RSTV6110_CTRL2] &= ~0xc0; | |
218 | priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6); | |
219 | ||
47220bc1 IL |
220 | stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8); |
221 | msleep(1); | |
222 | stv6110_set_bandwidth(fe, 72000000); | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
227 | static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency) | |
228 | { | |
229 | struct stv6110_priv *priv = fe->tuner_priv; | |
230 | u32 nbsteps, divider, psd2, freq; | |
231 | u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
232 | ||
233 | stv6110_read_regs(fe, regs, 0, 8); | |
234 | /*N*/ | |
235 | divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8; | |
236 | divider += priv->regs[RSTV6110_TUNING1]; | |
237 | ||
238 | /*R*/ | |
239 | nbsteps = (priv->regs[RSTV6110_TUNING2] >> 6) & 3; | |
240 | /*p*/ | |
241 | psd2 = (priv->regs[RSTV6110_TUNING2] >> 4) & 1; | |
242 | ||
243 | freq = divider * (priv->mclk / 1000); | |
244 | freq /= (1 << (nbsteps + psd2)); | |
245 | freq /= 4; | |
246 | ||
247 | *frequency = freq; | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency) | |
253 | { | |
254 | struct stv6110_priv *priv = fe->tuner_priv; | |
255 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; | |
256 | u8 ret = 0x04; | |
257 | u32 divider, ref, p, presc, i, result_freq, vco_freq; | |
258 | s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val; | |
873688cd | 259 | s32 srate; |
47220bc1 IL |
260 | |
261 | dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__, | |
262 | frequency, priv->mclk); | |
263 | ||
264 | /* K = (Reference / 1000000) - 16 */ | |
265 | priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3); | |
266 | priv->regs[RSTV6110_CTRL1] |= | |
267 | ((((priv->mclk / 1000000) - 16) & 0x1f) << 3); | |
268 | ||
269 | /* BB_GAIN = db/2 */ | |
270 | if (fe->ops.set_property && fe->ops.get_property) { | |
271 | srate = c->symbol_rate; | |
272 | dprintk("%s: Get Frontend parameters: srate=%d\n", | |
273 | __func__, srate); | |
274 | } else | |
275 | srate = 15000000; | |
276 | ||
47220bc1 | 277 | priv->regs[RSTV6110_CTRL2] &= ~0x0f; |
873688cd | 278 | priv->regs[RSTV6110_CTRL2] |= (priv->gain & 0x0f); |
47220bc1 IL |
279 | |
280 | if (frequency <= 1023000) { | |
281 | p = 1; | |
282 | presc = 0; | |
283 | } else if (frequency <= 1300000) { | |
284 | p = 1; | |
285 | presc = 1; | |
286 | } else if (frequency <= 2046000) { | |
287 | p = 0; | |
288 | presc = 0; | |
289 | } else { | |
290 | p = 0; | |
291 | presc = 1; | |
292 | } | |
293 | /* DIV4SEL = p*/ | |
294 | priv->regs[RSTV6110_TUNING2] &= ~(1 << 4); | |
295 | priv->regs[RSTV6110_TUNING2] |= (p << 4); | |
296 | ||
297 | /* PRESC32ON = presc */ | |
298 | priv->regs[RSTV6110_TUNING2] &= ~(1 << 5); | |
299 | priv->regs[RSTV6110_TUNING2] |= (presc << 5); | |
300 | ||
301 | p_val = (int)(1 << (p + 1)) * 10;/* P = 2 or P = 4 */ | |
302 | for (r_div = 0; r_div <= 3; r_div++) { | |
303 | p_calc = (priv->mclk / 100000); | |
304 | p_calc /= (1 << (r_div + 1)); | |
305 | if ((abssub(p_calc, p_val)) < (abssub(p_calc_opt, p_val))) | |
306 | r_div_opt = r_div; | |
307 | ||
308 | p_calc_opt = (priv->mclk / 100000); | |
309 | p_calc_opt /= (1 << (r_div_opt + 1)); | |
310 | } | |
311 | ||
312 | ref = priv->mclk / ((1 << (r_div_opt + 1)) * (1 << (p + 1))); | |
313 | divider = (((frequency * 1000) + (ref >> 1)) / ref); | |
314 | ||
315 | /* RDIV = r_div_opt */ | |
316 | priv->regs[RSTV6110_TUNING2] &= ~(3 << 6); | |
317 | priv->regs[RSTV6110_TUNING2] |= (((r_div_opt) & 3) << 6); | |
318 | ||
319 | /* NDIV_MSB = MSB(divider) */ | |
320 | priv->regs[RSTV6110_TUNING2] &= ~0x0f; | |
321 | priv->regs[RSTV6110_TUNING2] |= (((divider) >> 8) & 0x0f); | |
322 | ||
323 | /* NDIV_LSB, LSB(divider) */ | |
324 | priv->regs[RSTV6110_TUNING1] = (divider & 0xff); | |
325 | ||
326 | /* CALVCOSTRT = 1 VCO Auto Calibration */ | |
327 | priv->regs[RSTV6110_STAT1] |= 0x04; | |
328 | stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], | |
329 | RSTV6110_CTRL1, 8); | |
330 | ||
331 | i = 0; | |
332 | /* Wait for CALVCOSTRT == 0 */ | |
333 | while ((i < 10) && (ret != 0)) { | |
334 | ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x04); | |
335 | msleep(1); /* wait for VCO auto calibration */ | |
336 | i++; | |
337 | } | |
338 | ||
339 | ret = stv6110_read_reg(fe, RSTV6110_STAT1); | |
340 | stv6110_get_frequency(fe, &result_freq); | |
341 | ||
342 | vco_freq = divider * ((priv->mclk / 1000) / ((1 << (r_div_opt + 1)))); | |
343 | dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__, | |
344 | ret, result_freq, vco_freq); | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | static int stv6110_set_params(struct dvb_frontend *fe, | |
350 | struct dvb_frontend_parameters *params) | |
351 | { | |
352 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; | |
353 | u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff); | |
354 | ||
355 | stv6110_set_frequency(fe, c->frequency); | |
356 | stv6110_set_bandwidth(fe, bandwidth); | |
357 | ||
358 | return 0; | |
359 | } | |
360 | ||
361 | static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) | |
362 | { | |
363 | struct stv6110_priv *priv = fe->tuner_priv; | |
364 | u8 r8 = 0; | |
365 | u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
366 | stv6110_read_regs(fe, regs, 0, 8); | |
367 | ||
368 | /* CF */ | |
369 | r8 = priv->regs[RSTV6110_CTRL3] & 0x1f; | |
370 | *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */ | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | static struct dvb_tuner_ops stv6110_tuner_ops = { | |
376 | .info = { | |
377 | .name = "ST STV6110", | |
378 | .frequency_min = 950000, | |
379 | .frequency_max = 2150000, | |
380 | .frequency_step = 1000, | |
381 | }, | |
382 | .init = stv6110_init, | |
383 | .release = stv6110_release, | |
384 | .sleep = stv6110_sleep, | |
385 | .set_params = stv6110_set_params, | |
386 | .get_frequency = stv6110_get_frequency, | |
387 | .set_frequency = stv6110_set_frequency, | |
388 | .get_bandwidth = stv6110_get_bandwidth, | |
389 | .set_bandwidth = stv6110_set_bandwidth, | |
390 | ||
391 | }; | |
392 | ||
393 | struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe, | |
394 | const struct stv6110_config *config, | |
395 | struct i2c_adapter *i2c) | |
396 | { | |
397 | struct stv6110_priv *priv = NULL; | |
398 | u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e }; | |
399 | ||
400 | struct i2c_msg msg[] = { | |
401 | { | |
402 | .addr = config->i2c_address, | |
403 | .flags = 0, | |
404 | .buf = reg0, | |
405 | .len = 9 | |
406 | } | |
407 | }; | |
408 | int ret; | |
409 | ||
92782bb0 IL |
410 | /* divisor value for the output clock */ |
411 | reg0[2] &= ~0xc0; | |
412 | reg0[2] |= (config->clk_div << 6); | |
413 | ||
47220bc1 IL |
414 | if (fe->ops.i2c_gate_ctrl) |
415 | fe->ops.i2c_gate_ctrl(fe, 1); | |
416 | ||
417 | ret = i2c_transfer(i2c, msg, 1); | |
418 | ||
419 | if (fe->ops.i2c_gate_ctrl) | |
420 | fe->ops.i2c_gate_ctrl(fe, 0); | |
421 | ||
422 | if (ret != 1) | |
423 | return NULL; | |
424 | ||
425 | priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL); | |
426 | if (priv == NULL) | |
427 | return NULL; | |
428 | ||
429 | priv->i2c_address = config->i2c_address; | |
430 | priv->i2c = i2c; | |
431 | priv->mclk = config->mclk; | |
92782bb0 | 432 | priv->clk_div = config->clk_div; |
873688cd | 433 | priv->gain = config->gain; |
47220bc1 IL |
434 | |
435 | memcpy(&priv->regs, ®0[1], 8); | |
436 | ||
437 | memcpy(&fe->ops.tuner_ops, &stv6110_tuner_ops, | |
438 | sizeof(struct dvb_tuner_ops)); | |
439 | fe->tuner_priv = priv; | |
440 | printk(KERN_INFO "STV6110 attached on addr=%x!\n", priv->i2c_address); | |
441 | ||
442 | return fe; | |
443 | } | |
444 | EXPORT_SYMBOL(stv6110_attach); | |
445 | ||
446 | module_param(debug, int, 0644); | |
447 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); | |
448 | ||
449 | MODULE_DESCRIPTION("ST STV6110 driver"); | |
450 | MODULE_AUTHOR("Igor M. Liplianin"); | |
451 | MODULE_LICENSE("GPL"); |