3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
5 Copyright (C) ST Microelectronics
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
28 #include <media/dvb_frontend.h>
31 static unsigned int verbose
;
32 module_param(verbose
, int, 0644);
34 /* Max transfer size done by I2C transfer functions */
35 #define MAX_XFER_SIZE 64
42 #define dprintk(x, y, z, format, arg...) do { \
44 if ((x > FE_ERROR) && (x > y)) \
45 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
46 else if ((x > FE_NOTICE) && (x > y)) \
47 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
48 else if ((x > FE_INFO) && (x > y)) \
49 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
50 else if ((x > FE_DEBUG) && (x > y)) \
51 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
54 printk(format, ##arg); \
64 static void stb6100_release(struct dvb_frontend
*fe
);
66 static const struct stb6100_lkup lkup
[] = {
68 { 950000, 1000000, 0x0a },
69 { 1000000, 1075000, 0x0c },
70 { 1075000, 1200000, 0x00 },
71 { 1200000, 1300000, 0x01 },
72 { 1300000, 1370000, 0x02 },
73 { 1370000, 1470000, 0x04 },
74 { 1470000, 1530000, 0x05 },
75 { 1530000, 1650000, 0x06 },
76 { 1650000, 1800000, 0x08 },
77 { 1800000, 1950000, 0x0a },
78 { 1950000, 2150000, 0x0c },
79 { 2150000, 9999999, 0x0c },
83 /* Register names for easy debugging. */
84 static const char *stb6100_regnames
[] = {
86 [STB6100_VCO
] = "VCO",
88 [STB6100_NF_LSB
] = "NF",
92 [STB6100_DLB
] = "DLB",
93 [STB6100_TEST1
] = "TEST1",
94 [STB6100_FCCK
] = "FCCK",
95 [STB6100_LPEN
] = "LPEN",
96 [STB6100_TEST3
] = "TEST3",
99 /* Template for normalisation, i.e. setting unused or undocumented
100 * bits as required according to the documentation.
102 struct stb6100_regmask
{
107 static const struct stb6100_regmask stb6100_template
[] = {
108 [STB6100_LD
] = { 0xff, 0x00 },
109 [STB6100_VCO
] = { 0xff, 0x00 },
110 [STB6100_NI
] = { 0xff, 0x00 },
111 [STB6100_NF_LSB
] = { 0xff, 0x00 },
112 [STB6100_K
] = { 0xc7, 0x38 },
113 [STB6100_G
] = { 0xef, 0x10 },
114 [STB6100_F
] = { 0x1f, 0xc0 },
115 [STB6100_DLB
] = { 0x38, 0xc4 },
116 [STB6100_TEST1
] = { 0x00, 0x8f },
117 [STB6100_FCCK
] = { 0x40, 0x0d },
118 [STB6100_LPEN
] = { 0xf0, 0x0b },
119 [STB6100_TEST3
] = { 0x00, 0xde },
123 * Currently unused. Some boards might need it in the future
125 static inline void stb6100_normalise_regs(u8 regs
[])
129 for (i
= 0; i
< STB6100_NUMREGS
; i
++)
130 regs
[i
] = (regs
[i
] & stb6100_template
[i
].mask
) | stb6100_template
[i
].set
;
133 static int stb6100_read_regs(struct stb6100_state
*state
, u8 regs
[])
136 struct i2c_msg msg
= {
137 .addr
= state
->config
->tuner_address
,
140 .len
= STB6100_NUMREGS
143 rc
= i2c_transfer(state
->i2c
, &msg
, 1);
144 if (unlikely(rc
!= 1)) {
145 dprintk(verbose
, FE_ERROR
, 1, "Read (0x%x) err, rc=[%d]",
146 state
->config
->tuner_address
, rc
);
150 if (unlikely(verbose
> FE_DEBUG
)) {
153 dprintk(verbose
, FE_DEBUG
, 1, " Read from 0x%02x", state
->config
->tuner_address
);
154 for (i
= 0; i
< STB6100_NUMREGS
; i
++)
155 dprintk(verbose
, FE_DEBUG
, 1, " %s: 0x%02x", stb6100_regnames
[i
], regs
[i
]);
160 static int stb6100_read_reg(struct stb6100_state
*state
, u8 reg
)
162 u8 regs
[STB6100_NUMREGS
];
164 struct i2c_msg msg
= {
165 .addr
= state
->config
->tuner_address
+ reg
,
171 i2c_transfer(state
->i2c
, &msg
, 1);
173 if (unlikely(reg
>= STB6100_NUMREGS
)) {
174 dprintk(verbose
, FE_ERROR
, 1, "Invalid register offset 0x%x", reg
);
177 if (unlikely(verbose
> FE_DEBUG
)) {
178 dprintk(verbose
, FE_DEBUG
, 1, " Read from 0x%02x", state
->config
->tuner_address
);
179 dprintk(verbose
, FE_DEBUG
, 1, " %s: 0x%02x", stb6100_regnames
[reg
], regs
[0]);
182 return (unsigned int)regs
[0];
185 static int stb6100_write_reg_range(struct stb6100_state
*state
, u8 buf
[], int start
, int len
)
188 u8 cmdbuf
[MAX_XFER_SIZE
];
189 struct i2c_msg msg
= {
190 .addr
= state
->config
->tuner_address
,
196 if (1 + len
> sizeof(cmdbuf
)) {
198 "%s: i2c wr: len=%d is too big!\n",
199 KBUILD_MODNAME
, len
);
203 if (unlikely(start
< 1 || start
+ len
> STB6100_NUMREGS
)) {
204 dprintk(verbose
, FE_ERROR
, 1, "Invalid register range %d:%d",
208 memcpy(&cmdbuf
[1], buf
, len
);
211 if (unlikely(verbose
> FE_DEBUG
)) {
214 dprintk(verbose
, FE_DEBUG
, 1, " Write @ 0x%02x: [%d:%d]", state
->config
->tuner_address
, start
, len
);
215 for (i
= 0; i
< len
; i
++)
216 dprintk(verbose
, FE_DEBUG
, 1, " %s: 0x%02x", stb6100_regnames
[start
+ i
], buf
[i
]);
218 rc
= i2c_transfer(state
->i2c
, &msg
, 1);
219 if (unlikely(rc
!= 1)) {
220 dprintk(verbose
, FE_ERROR
, 1, "(0x%x) write err [%d:%d], rc=[%d]",
221 (unsigned int)state
->config
->tuner_address
, start
, len
, rc
);
227 static int stb6100_write_reg(struct stb6100_state
*state
, u8 reg
, u8 data
)
229 u8 tmp
= data
; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
231 if (unlikely(reg
>= STB6100_NUMREGS
)) {
232 dprintk(verbose
, FE_ERROR
, 1, "Invalid register offset 0x%x", reg
);
235 tmp
= (tmp
& stb6100_template
[reg
].mask
) | stb6100_template
[reg
].set
;
236 return stb6100_write_reg_range(state
, &tmp
, reg
, 1);
240 static int stb6100_get_status(struct dvb_frontend
*fe
, u32
*status
)
243 struct stb6100_state
*state
= fe
->tuner_priv
;
245 rc
= stb6100_read_reg(state
, STB6100_LD
);
247 dprintk(verbose
, FE_ERROR
, 1, "%s failed", __func__
);
250 return (rc
& STB6100_LD_LOCK
) ? TUNER_STATUS_LOCKED
: 0;
253 static int stb6100_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
258 struct stb6100_state
*state
= fe
->tuner_priv
;
260 rc
= stb6100_read_reg(state
, STB6100_F
);
263 f
= rc
& STB6100_F_F
;
265 bw
= (f
+ 5) * 2000; /* x2 for ZIF */
267 *bandwidth
= state
->bandwidth
= bw
* 1000;
268 dprintk(verbose
, FE_DEBUG
, 1, "bandwidth = %u Hz", state
->bandwidth
);
272 static int stb6100_set_bandwidth(struct dvb_frontend
*fe
, u32 bandwidth
)
276 struct stb6100_state
*state
= fe
->tuner_priv
;
278 dprintk(verbose
, FE_DEBUG
, 1, "set bandwidth to %u Hz", bandwidth
);
280 bandwidth
/= 2; /* ZIF */
282 if (bandwidth
>= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */
284 else if (bandwidth
<= 5000000) /* bw/2 min = 5Mhz for F=0 */
286 else /* if 5 < bw/2 < 36 */
287 tmp
= (bandwidth
+ 500000) / 1000000 - 5;
289 /* Turn on LPF bandwidth setting clock control,
290 * set bandwidth, wait 10ms, turn off.
292 rc
= stb6100_write_reg(state
, STB6100_FCCK
, 0x0d | STB6100_FCCK_FCCK
);
295 rc
= stb6100_write_reg(state
, STB6100_F
, 0xc0 | tmp
);
299 msleep(5); /* This is dangerous as another (related) thread may start */
301 rc
= stb6100_write_reg(state
, STB6100_FCCK
, 0x0d);
305 msleep(10); /* This is dangerous as another (related) thread may start */
310 static int stb6100_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
313 u32 nint
, nfrac
, fvco
;
315 struct stb6100_state
*state
= fe
->tuner_priv
;
316 u8 regs
[STB6100_NUMREGS
];
318 rc
= stb6100_read_regs(state
, regs
);
322 odiv
= (regs
[STB6100_VCO
] & STB6100_VCO_ODIV
) >> STB6100_VCO_ODIV_SHIFT
;
323 psd2
= (regs
[STB6100_K
] & STB6100_K_PSD2
) >> STB6100_K_PSD2_SHIFT
;
324 nint
= regs
[STB6100_NI
];
325 nfrac
= ((regs
[STB6100_K
] & STB6100_K_NF_MSB
) << 8) | regs
[STB6100_NF_LSB
];
326 fvco
= (nfrac
* state
->reference
>> (9 - psd2
)) + (nint
* state
->reference
<< psd2
);
327 *frequency
= state
->frequency
= fvco
>> (odiv
+ 1);
329 dprintk(verbose
, FE_DEBUG
, 1,
330 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
331 state
->frequency
, odiv
, psd2
, state
->reference
, fvco
, nint
, nfrac
);
336 static int stb6100_set_frequency(struct dvb_frontend
*fe
, u32 frequency
)
339 const struct stb6100_lkup
*ptr
;
340 struct stb6100_state
*state
= fe
->tuner_priv
;
341 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
343 u32 srate
= 0, fvco
, nint
, nfrac
;
344 u8 regs
[STB6100_NUMREGS
];
347 dprintk(verbose
, FE_DEBUG
, 1, "Version 2010-8-14 13:51");
349 if (fe
->ops
.get_frontend
) {
350 dprintk(verbose
, FE_DEBUG
, 1, "Get frontend parameters");
351 fe
->ops
.get_frontend(fe
, p
);
353 srate
= p
->symbol_rate
;
355 /* Set up tuner cleanly, LPF calibration on */
356 rc
= stb6100_write_reg(state
, STB6100_FCCK
, 0x4d | STB6100_FCCK_FCCK
);
358 return rc
; /* allow LPF calibration */
360 /* PLL Loop disabled, bias on, VCO on, synth on */
361 regs
[STB6100_LPEN
] = 0xeb;
362 rc
= stb6100_write_reg(state
, STB6100_LPEN
, regs
[STB6100_LPEN
]);
366 /* Program the registers with their data values */
368 /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */
369 if (frequency
<= 1075000)
374 /* VCO enabled, search clock off as per LL3.7, 3.4.1 */
375 regs
[STB6100_VCO
] = 0xe0 | (odiv
<< STB6100_VCO_ODIV_SHIFT
);
379 (ptr
->val_high
!= 0) && !CHKRANGE(frequency
, ptr
->val_low
, ptr
->val_high
);
382 if (ptr
->val_high
== 0) {
383 printk(KERN_ERR
"%s: frequency out of range: %u kHz\n", __func__
, frequency
);
386 regs
[STB6100_VCO
] = (regs
[STB6100_VCO
] & ~STB6100_VCO_OSM
) | ptr
->reg
;
387 rc
= stb6100_write_reg(state
, STB6100_VCO
, regs
[STB6100_VCO
]);
391 if ((frequency
> 1075000) && (frequency
<= 1325000))
395 /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */
396 fvco
= frequency
<< (1 + odiv
);
397 /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */
398 nint
= fvco
/ (state
->reference
<< psd2
);
399 /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */
400 nfrac
= DIV_ROUND_CLOSEST((fvco
- (nint
* state
->reference
<< psd2
))
401 << (9 - psd2
), state
->reference
);
404 regs
[STB6100_NI
] = nint
;
405 rc
= stb6100_write_reg(state
, STB6100_NI
, regs
[STB6100_NI
]);
410 regs
[STB6100_NF_LSB
] = nfrac
;
411 rc
= stb6100_write_reg(state
, STB6100_NF_LSB
, regs
[STB6100_NF_LSB
]);
416 regs
[STB6100_K
] = (0x38 & ~STB6100_K_PSD2
) | (psd2
<< STB6100_K_PSD2_SHIFT
);
417 regs
[STB6100_K
] = (regs
[STB6100_K
] & ~STB6100_K_NF_MSB
) | ((nfrac
>> 8) & STB6100_K_NF_MSB
);
418 rc
= stb6100_write_reg(state
, STB6100_K
, regs
[STB6100_K
]);
422 /* G Baseband gain. */
423 if (srate
>= 15000000)
425 else if (srate
>= 5000000)
430 regs
[STB6100_G
] = (0x10 & ~STB6100_G_G
) | g
;
431 regs
[STB6100_G
] &= ~STB6100_G_GCT
; /* mask GCT */
432 regs
[STB6100_G
] |= (1 << 5); /* 2Vp-p Mode */
433 rc
= stb6100_write_reg(state
, STB6100_G
, regs
[STB6100_G
]);
437 /* F we don't write as it is set up in BW set */
439 /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */
440 regs
[STB6100_DLB
] = 0xcc;
441 rc
= stb6100_write_reg(state
, STB6100_DLB
, regs
[STB6100_DLB
]);
445 dprintk(verbose
, FE_DEBUG
, 1,
446 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
447 frequency
, srate
, (unsigned int)g
, (unsigned int)odiv
,
448 (unsigned int)psd2
, state
->reference
,
449 ptr
->reg
, fvco
, nint
, nfrac
);
451 /* Set up the test registers */
452 regs
[STB6100_TEST1
] = 0x8f;
453 rc
= stb6100_write_reg(state
, STB6100_TEST1
, regs
[STB6100_TEST1
]);
456 regs
[STB6100_TEST3
] = 0xde;
457 rc
= stb6100_write_reg(state
, STB6100_TEST3
, regs
[STB6100_TEST3
]);
461 /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */
462 regs
[STB6100_LPEN
] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */
463 rc
= stb6100_write_reg(state
, STB6100_LPEN
, regs
[STB6100_LPEN
]);
469 /* Bring up tuner according to LLA 3.7 3.4.1, step 3 */
470 regs
[STB6100_VCO
] &= ~STB6100_VCO_OCK
; /* VCO fast search */
471 rc
= stb6100_write_reg(state
, STB6100_VCO
, regs
[STB6100_VCO
]);
475 msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */
477 regs
[STB6100_VCO
] &= ~STB6100_VCO_OSCH
; /* vco search disabled */
478 regs
[STB6100_VCO
] |= STB6100_VCO_OCK
; /* search clock off */
479 rc
= stb6100_write_reg(state
, STB6100_VCO
, regs
[STB6100_VCO
]);
483 rc
= stb6100_write_reg(state
, STB6100_FCCK
, 0x0d);
485 return rc
; /* Stop LPF calibration */
487 msleep(10); /* This is dangerous as another (related) thread may start */
488 /* wait for stabilisation, (should not be necessary) */
492 static int stb6100_sleep(struct dvb_frontend
*fe
)
494 /* TODO: power down */
498 static int stb6100_init(struct dvb_frontend
*fe
)
500 struct stb6100_state
*state
= fe
->tuner_priv
;
501 int refclk
= 27000000; /* Hz */
507 state
->bandwidth
= 36000000; /* Hz */
508 state
->reference
= refclk
/ 1000; /* kHz */
510 /* Set default bandwidth. Modified, PN 13-May-10 */
514 static int stb6100_set_params(struct dvb_frontend
*fe
)
516 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
518 if (c
->frequency
> 0)
519 stb6100_set_frequency(fe
, c
->frequency
);
521 if (c
->bandwidth_hz
> 0)
522 stb6100_set_bandwidth(fe
, c
->bandwidth_hz
);
527 static const struct dvb_tuner_ops stb6100_ops
= {
529 .name
= "STB6100 Silicon Tuner",
530 .frequency_min
= 950000,
531 .frequency_max
= 2150000,
535 .init
= stb6100_init
,
536 .sleep
= stb6100_sleep
,
537 .get_status
= stb6100_get_status
,
538 .set_params
= stb6100_set_params
,
539 .get_frequency
= stb6100_get_frequency
,
540 .get_bandwidth
= stb6100_get_bandwidth
,
541 .release
= stb6100_release
544 struct dvb_frontend
*stb6100_attach(struct dvb_frontend
*fe
,
545 const struct stb6100_config
*config
,
546 struct i2c_adapter
*i2c
)
548 struct stb6100_state
*state
= NULL
;
550 state
= kzalloc(sizeof (struct stb6100_state
), GFP_KERNEL
);
554 state
->config
= config
;
556 state
->frontend
= fe
;
557 state
->reference
= config
->refclock
/ 1000; /* kHz */
558 fe
->tuner_priv
= state
;
559 fe
->ops
.tuner_ops
= stb6100_ops
;
561 printk("%s: Attaching STB6100 \n", __func__
);
565 static void stb6100_release(struct dvb_frontend
*fe
)
567 struct stb6100_state
*state
= fe
->tuner_priv
;
569 fe
->tuner_priv
= NULL
;
573 EXPORT_SYMBOL(stb6100_attach
);
574 MODULE_PARM_DESC(verbose
, "Set Verbosity level");
576 MODULE_AUTHOR("Manu Abraham");
577 MODULE_DESCRIPTION("STB6100 Silicon tuner");
578 MODULE_LICENSE("GPL");