1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/module.h>
31#include <linux/dvb/frontend.h>
32#include <linux/slab.h>
33#include <linux/types.h>
34
35#include "zl10036.h"
36
37static int zl10036_debug;
38#define dprintk(level, args...) \
39 do { if (zl10036_debug & level) printk(KERN_DEBUG "zl10036: " args); \
40 } while (0)
41
42#define deb_info(args...) dprintk(0x01, args)
43#define deb_i2c(args...) dprintk(0x02, args)
44
45struct zl10036_state {
46 struct i2c_adapter *i2c;
47 const struct zl10036_config *config;
48 u32 frequency;
49 u8 br, bf;
50};
51
52
53
54#define _XTAL 10111
55
56
57
58
59
60
61
62#define _RDIV 10
63#define _RDIV_REG 0x0a
64#define _FR (_XTAL/_RDIV)
65
66#define STATUS_POR 0x80
67#define STATUS_FL 0x40
68
69
70
71static int zl10036_read_status_reg(struct zl10036_state *state)
72{
73 u8 status;
74 struct i2c_msg msg[1] = {
75 { .addr = state->config->tuner_address, .flags = I2C_M_RD,
76 .buf = &status, .len = sizeof(status) },
77 };
78
79 if (i2c_transfer(state->i2c, msg, 1) != 1) {
80 printk(KERN_ERR "%s: i2c read failed at addr=%02x\n",
81 __func__, state->config->tuner_address);
82 return -EIO;
83 }
84
85 deb_i2c("R(status): %02x [FL=%d]\n", status,
86 (status & STATUS_FL) ? 1 : 0);
87 if (status & STATUS_POR)
88 deb_info("%s: Power-On-Reset bit enabled - "
89 "need to initialize the tuner\n", __func__);
90
91 return status;
92}
93
94static int zl10036_write(struct zl10036_state *state, u8 buf[], u8 count)
95{
96 struct i2c_msg msg[1] = {
97 { .addr = state->config->tuner_address, .flags = 0,
98 .buf = buf, .len = count },
99 };
100 u8 reg = 0;
101 int ret;
102
103 if (zl10036_debug & 0x02) {
104
105
106 if ((buf[0] & 0x80) == 0x00)
107 reg = 2;
108 else if ((buf[0] & 0xc0) == 0x80)
109 reg = 4;
110 else if ((buf[0] & 0xf0) == 0xc0)
111 reg = 6;
112 else if ((buf[0] & 0xf0) == 0xd0)
113 reg = 8;
114 else if ((buf[0] & 0xf0) == 0xe0)
115 reg = 10;
116 else if ((buf[0] & 0xf0) == 0xf0)
117 reg = 12;
118
119 deb_i2c("W(%d):", reg);
120 {
121 int i;
122 for (i = 0; i < count; i++)
123 printk(KERN_CONT " %02x", buf[i]);
124 printk(KERN_CONT "\n");
125 }
126 }
127
128 ret = i2c_transfer(state->i2c, msg, 1);
129 if (ret != 1) {
130 printk(KERN_ERR "%s: i2c error, ret=%d\n", __func__, ret);
131 return -EIO;
132 }
133
134 return 0;
135}
136
137static int zl10036_release(struct dvb_frontend *fe)
138{
139 struct zl10036_state *state = fe->tuner_priv;
140
141 fe->tuner_priv = NULL;
142 kfree(state);
143
144 return 0;
145}
146
147static int zl10036_sleep(struct dvb_frontend *fe)
148{
149 struct zl10036_state *state = fe->tuner_priv;
150 u8 buf[] = { 0xf0, 0x80 };
151 int ret;
152
153 deb_info("%s\n", __func__);
154
155 if (fe->ops.i2c_gate_ctrl)
156 fe->ops.i2c_gate_ctrl(fe, 1);
157
158 ret = zl10036_write(state, buf, sizeof(buf));
159
160 if (fe->ops.i2c_gate_ctrl)
161 fe->ops.i2c_gate_ctrl(fe, 0);
162
163 return ret;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184static int zl10036_set_frequency(struct zl10036_state *state, u32 frequency)
185{
186 u8 buf[2];
187 u32 div, foffset;
188
189 div = (frequency + _FR/2) / _FR;
190 state->frequency = div * _FR;
191
192 foffset = frequency - state->frequency;
193
194 buf[0] = (div >> 8) & 0x7f;
195 buf[1] = (div >> 0) & 0xff;
196
197 deb_info("%s: ftodo=%u fpriv=%u ferr=%d div=%u\n", __func__,
198 frequency, state->frequency, foffset, div);
199
200 return zl10036_write(state, buf, sizeof(buf));
201}
202
203static int zl10036_set_bandwidth(struct zl10036_state *state, u32 fbw)
204{
205
206 u8 br, bf;
207 int ret;
208 u8 buf_bf[] = {
209 0xc0, 0x00,
210 };
211 u8 buf_br[] = {
212 0xf0, 0x00,
213 };
214 u8 zl10036_rsd_off[] = { 0xc8 };
215
216
217 if (fbw > 35000)
218 fbw = 35000;
219 if (fbw < 8000)
220 fbw = 8000;
221
222#define _BR_MAXIMUM (_XTAL/575)
223
224
225 if (fbw <= 28820) {
226 br = _BR_MAXIMUM;
227 } else {
228
229
230
231
232 br = ((_XTAL * 21 * 1000) / (fbw * 419));
233 }
234
235
236 if (br < 4)
237 br = 4;
238 if (br > _BR_MAXIMUM)
239 br = _BR_MAXIMUM;
240
241
242
243
244
245 bf = (fbw * br * 1257) / (_XTAL * 1000) - 1;
246
247
248 if (bf > 62)
249 bf = 62;
250
251 buf_bf[1] = (bf << 1) & 0x7e;
252 buf_br[1] = (br << 2) & 0x7c;
253 deb_info("%s: BW=%d br=%u bf=%u\n", __func__, fbw, br, bf);
254
255 if (br != state->br) {
256 ret = zl10036_write(state, buf_br, sizeof(buf_br));
257 if (ret < 0)
258 return ret;
259 }
260
261 if (bf != state->bf) {
262 ret = zl10036_write(state, buf_bf, sizeof(buf_bf));
263 if (ret < 0)
264 return ret;
265
266
267
268
269 msleep(1);
270
271 ret = zl10036_write(state, zl10036_rsd_off,
272 sizeof(zl10036_rsd_off));
273 if (ret < 0)
274 return ret;
275 }
276
277 state->br = br;
278 state->bf = bf;
279
280 return 0;
281}
282
283static int zl10036_set_gain_params(struct zl10036_state *state,
284 int c)
285{
286 u8 buf[2];
287 u8 rfg, ba, bg;
288
289
290 rfg = 0;
291 ba = 1;
292 bg = 1;
293
294
295 buf[0] = 0x80 | ((rfg << 5) & 0x20)
296 | ((ba << 3) & 0x18) | ((bg << 1) & 0x06);
297
298 if (!state->config->rf_loop_enable)
299 buf[0] |= 0x01;
300
301
302 buf[1] = _RDIV_REG | ((c << 5) & 0x60);
303
304 deb_info("%s: c=%u rfg=%u ba=%u bg=%u\n", __func__, c, rfg, ba, bg);
305 return zl10036_write(state, buf, sizeof(buf));
306}
307
308static int zl10036_set_params(struct dvb_frontend *fe)
309{
310 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
311 struct zl10036_state *state = fe->tuner_priv;
312 int ret = 0;
313 u32 frequency = p->frequency;
314 u32 fbw;
315 int i;
316 u8 c;
317
318
319
320 if ((frequency < fe->ops.info.frequency_min)
321 || (frequency > fe->ops.info.frequency_max))
322 return -EINVAL;
323
324
325
326
327
328
329 fbw = (27 * p->symbol_rate) / 32;
330
331
332 fbw /= 1000;
333
334
335 fbw += 3000;
336
337
338 if (frequency < 950000)
339 return -EINVAL;
340 else if (frequency < 1250000)
341 c = 0;
342 else if (frequency < 1750000)
343 c = 1;
344 else if (frequency < 2175000)
345 c = 2;
346 else
347 return -EINVAL;
348
349 if (fe->ops.i2c_gate_ctrl)
350 fe->ops.i2c_gate_ctrl(fe, 1);
351
352 ret = zl10036_set_gain_params(state, c);
353 if (ret < 0)
354 goto error;
355
356 ret = zl10036_set_frequency(state, p->frequency);
357 if (ret < 0)
358 goto error;
359
360 ret = zl10036_set_bandwidth(state, fbw);
361 if (ret < 0)
362 goto error;
363
364
365 for (i = 0; i < 20; i++) {
366 ret = zl10036_read_status_reg(state);
367 if (ret < 0)
368 goto error;
369
370
371 if (ret & STATUS_FL)
372 break;
373
374 msleep(10);
375 }
376
377error:
378 if (fe->ops.i2c_gate_ctrl)
379 fe->ops.i2c_gate_ctrl(fe, 0);
380
381 return ret;
382}
383
384static int zl10036_get_frequency(struct dvb_frontend *fe, u32 *frequency)
385{
386 struct zl10036_state *state = fe->tuner_priv;
387
388 *frequency = state->frequency;
389
390 return 0;
391}
392
393static int zl10036_init_regs(struct zl10036_state *state)
394{
395 int ret;
396 int i;
397
398
399 u8 zl10036_init_tab[][2] = {
400 { 0x04, 0x00 },
401 { 0x8b, _RDIV_REG },
402
403 { 0xc0, 0x20 },
404 { 0xd3, 0x40 },
405 { 0xe3, 0x5b },
406 { 0xf0, 0x28 },
407 { 0xe3, 0xf9 },
408 };
409
410
411 state->br = 0xff;
412 state->bf = 0xff;
413
414 if (!state->config->rf_loop_enable)
415 zl10036_init_tab[1][0] |= 0x01;
416
417 deb_info("%s\n", __func__);
418
419 for (i = 0; i < ARRAY_SIZE(zl10036_init_tab); i++) {
420 ret = zl10036_write(state, zl10036_init_tab[i], 2);
421 if (ret < 0)
422 return ret;
423 }
424
425 return 0;
426}
427
428static int zl10036_init(struct dvb_frontend *fe)
429{
430 struct zl10036_state *state = fe->tuner_priv;
431 int ret = 0;
432
433 if (fe->ops.i2c_gate_ctrl)
434 fe->ops.i2c_gate_ctrl(fe, 1);
435
436 ret = zl10036_read_status_reg(state);
437 if (ret < 0)
438 return ret;
439
440
441 ret = zl10036_init_regs(state);
442
443 if (fe->ops.i2c_gate_ctrl)
444 fe->ops.i2c_gate_ctrl(fe, 0);
445
446 return ret;
447}
448
449static struct dvb_tuner_ops zl10036_tuner_ops = {
450 .info = {
451 .name = "Zarlink ZL10036",
452 .frequency_min = 950000,
453 .frequency_max = 2175000
454 },
455 .init = zl10036_init,
456 .release = zl10036_release,
457 .sleep = zl10036_sleep,
458 .set_params = zl10036_set_params,
459 .get_frequency = zl10036_get_frequency,
460};
461
462struct dvb_frontend *zl10036_attach(struct dvb_frontend *fe,
463 const struct zl10036_config *config,
464 struct i2c_adapter *i2c)
465{
466 struct zl10036_state *state;
467 int ret;
468
469 if (!config) {
470 printk(KERN_ERR "%s: no config specified", __func__);
471 return NULL;
472 }
473
474 state = kzalloc(sizeof(struct zl10036_state), GFP_KERNEL);
475 if (!state)
476 return NULL;
477
478 state->config = config;
479 state->i2c = i2c;
480
481 if (fe->ops.i2c_gate_ctrl)
482 fe->ops.i2c_gate_ctrl(fe, 1);
483
484 ret = zl10036_read_status_reg(state);
485 if (ret < 0) {
486 printk(KERN_ERR "%s: No zl10036 found\n", __func__);
487 goto error;
488 }
489
490 ret = zl10036_init_regs(state);
491 if (ret < 0) {
492 printk(KERN_ERR "%s: tuner initialization failed\n",
493 __func__);
494 goto error;
495 }
496
497 if (fe->ops.i2c_gate_ctrl)
498 fe->ops.i2c_gate_ctrl(fe, 0);
499
500 fe->tuner_priv = state;
501
502 memcpy(&fe->ops.tuner_ops, &zl10036_tuner_ops,
503 sizeof(struct dvb_tuner_ops));
504 printk(KERN_INFO "%s: tuner initialization (%s addr=0x%02x) ok\n",
505 __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
506
507 return fe;
508
509error:
510 kfree(state);
511 return NULL;
512}
513EXPORT_SYMBOL(zl10036_attach);
514
515module_param_named(debug, zl10036_debug, int, 0644);
516MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
517MODULE_DESCRIPTION("DVB ZL10036 driver");
518MODULE_AUTHOR("Tino Reichardt");
519MODULE_AUTHOR("Matthias Schwarzott");
520MODULE_LICENSE("GPL");
521