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/string.h>
27 #include "dvb_frontend.h"
30 static unsigned int verbose
;
31 module_param(verbose
, int, 0644);
39 #define dprintk(x, y, z, format, arg...) do { \
41 if ((x > FE_ERROR) && (x > y)) \
42 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
43 else if ((x > FE_NOTICE) && (x > y)) \
44 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
45 else if ((x > FE_INFO) && (x > y)) \
46 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
47 else if ((x > FE_DEBUG) && (x > y)) \
48 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
51 printk(format, ##arg); \
61 static int stb6100_release(struct dvb_frontend
*fe
);
63 static const struct stb6100_lkup lkup
[] = {
65 { 950000, 1000000, 0x0a },
66 { 1000000, 1075000, 0x0c },
67 { 1075000, 1200000, 0x00 },
68 { 1200000, 1300000, 0x01 },
69 { 1300000, 1370000, 0x02 },
70 { 1370000, 1470000, 0x04 },
71 { 1470000, 1530000, 0x05 },
72 { 1530000, 1650000, 0x06 },
73 { 1650000, 1800000, 0x08 },
74 { 1800000, 1950000, 0x0a },
75 { 1950000, 2150000, 0x0c },
76 { 2150000, 9999999, 0x0c },
80 /* Register names for easy debugging. */
81 static const char *stb6100_regnames
[] = {
83 [STB6100_VCO
] = "VCO",
85 [STB6100_NF_LSB
] = "NF",
89 [STB6100_DLB
] = "DLB",
90 [STB6100_TEST1
] = "TEST1",
91 [STB6100_FCCK
] = "FCCK",
92 [STB6100_LPEN
] = "LPEN",
93 [STB6100_TEST3
] = "TEST3",
96 /* Template for normalisation, i.e. setting unused or undocumented
97 * bits as required according to the documentation.
99 struct stb6100_regmask
{
104 static const struct stb6100_regmask stb6100_template
[] = {
105 [STB6100_LD
] = { 0xff, 0x00 },
106 [STB6100_VCO
] = { 0xff, 0x00 },
107 [STB6100_NI
] = { 0xff, 0x00 },
108 [STB6100_NF_LSB
] = { 0xff, 0x00 },
109 [STB6100_K
] = { 0xc7, 0x38 },
110 [STB6100_G
] = { 0xef, 0x10 },
111 [STB6100_F
] = { 0x1f, 0xc0 },
112 [STB6100_DLB
] = { 0x38, 0xc4 },
113 [STB6100_TEST1
] = { 0x00, 0x8f },
114 [STB6100_FCCK
] = { 0x40, 0x0d },
115 [STB6100_LPEN
] = { 0xf0, 0x0b },
116 [STB6100_TEST3
] = { 0x00, 0xde },
119 static void stb6100_normalise_regs(u8 regs
[])
123 for (i
= 0; i
< STB6100_NUMREGS
; i
++)
124 regs
[i
] = (regs
[i
] & stb6100_template
[i
].mask
) | stb6100_template
[i
].set
;
127 static int stb6100_read_regs(struct stb6100_state
*state
, u8 regs
[])
130 struct i2c_msg msg
= {
131 .addr
= state
->config
->tuner_address
,
134 .len
= STB6100_NUMREGS
137 rc
= i2c_transfer(state
->i2c
, &msg
, 1);
138 if (unlikely(rc
!= 1)) {
139 dprintk(verbose
, FE_ERROR
, 1, "Read (0x%x) err, rc=[%d]",
140 state
->config
->tuner_address
, rc
);
144 if (unlikely(verbose
> FE_DEBUG
)) {
147 dprintk(verbose
, FE_DEBUG
, 1, " Read from 0x%02x", state
->config
->tuner_address
);
148 for (i
= 0; i
< STB6100_NUMREGS
; i
++)
149 dprintk(verbose
, FE_DEBUG
, 1, " %s: 0x%02x", stb6100_regnames
[i
], regs
[i
]);
154 static int stb6100_read_reg(struct stb6100_state
*state
, u8 reg
)
156 u8 regs
[STB6100_NUMREGS
];
159 if (unlikely(reg
>= STB6100_NUMREGS
)) {
160 dprintk(verbose
, FE_ERROR
, 1, "Invalid register offset 0x%x", reg
);
163 if ((rc
= stb6100_read_regs(state
, regs
)) < 0)
165 return (unsigned int)regs
[reg
];
168 static int stb6100_write_reg_range(struct stb6100_state
*state
, u8 buf
[], int start
, int len
)
172 struct i2c_msg msg
= {
173 .addr
= state
->config
->tuner_address
,
179 if (unlikely(start
< 1 || start
+ len
> STB6100_NUMREGS
)) {
180 dprintk(verbose
, FE_ERROR
, 1, "Invalid register range %d:%d",
184 memcpy(&cmdbuf
[1], buf
, len
);
187 if (unlikely(verbose
> FE_DEBUG
)) {
190 dprintk(verbose
, FE_DEBUG
, 1, " Write @ 0x%02x: [%d:%d]", state
->config
->tuner_address
, start
, len
);
191 for (i
= 0; i
< len
; i
++)
192 dprintk(verbose
, FE_DEBUG
, 1, " %s: 0x%02x", stb6100_regnames
[start
+ i
], buf
[i
]);
194 rc
= i2c_transfer(state
->i2c
, &msg
, 1);
195 if (unlikely(rc
!= 1)) {
196 dprintk(verbose
, FE_ERROR
, 1, "(0x%x) write err [%d:%d], rc=[%d]",
197 (unsigned int)state
->config
->tuner_address
, start
, len
, rc
);
203 static int stb6100_write_reg(struct stb6100_state
*state
, u8 reg
, u8 data
)
205 if (unlikely(reg
>= STB6100_NUMREGS
)) {
206 dprintk(verbose
, FE_ERROR
, 1, "Invalid register offset 0x%x", reg
);
209 data
= (data
& stb6100_template
[reg
].mask
) | stb6100_template
[reg
].set
;
210 return stb6100_write_reg_range(state
, &data
, reg
, 1);
213 static int stb6100_write_regs(struct stb6100_state
*state
, u8 regs
[])
215 stb6100_normalise_regs(regs
);
216 return stb6100_write_reg_range(state
, ®s
[1], 1, STB6100_NUMREGS
- 1);
219 static int stb6100_get_status(struct dvb_frontend
*fe
, u32
*status
)
222 struct stb6100_state
*state
= fe
->tuner_priv
;
224 if ((rc
= stb6100_read_reg(state
, STB6100_LD
)) < 0)
227 return (rc
& STB6100_LD_LOCK
) ? TUNER_STATUS_LOCKED
: 0;
230 static int stb6100_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
234 struct stb6100_state
*state
= fe
->tuner_priv
;
236 if ((rc
= stb6100_read_reg(state
, STB6100_F
)) < 0)
238 f
= rc
& STB6100_F_F
;
240 state
->status
.bandwidth
= (f
+ 5) * 2000; /* x2 for ZIF */
242 *bandwidth
= state
->bandwidth
= state
->status
.bandwidth
* 1000;
243 dprintk(verbose
, FE_DEBUG
, 1, "bandwidth = %u Hz", state
->bandwidth
);
247 static int stb6100_set_bandwidth(struct dvb_frontend
*fe
, u32 bandwidth
)
251 struct stb6100_state
*state
= fe
->tuner_priv
;
253 dprintk(verbose
, FE_DEBUG
, 1, "set bandwidth to %u Hz", bandwidth
);
255 bandwidth
/= 2; /* ZIF */
257 if (bandwidth
>= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */
259 else if (bandwidth
<= 5000000) /* bw/2 min = 5Mhz for F=0 */
261 else /* if 5 < bw/2 < 36 */
262 tmp
= (bandwidth
+ 500000) / 1000000 - 5;
264 /* Turn on LPF bandwidth setting clock control,
265 * set bandwidth, wait 10ms, turn off.
267 if ((rc
= stb6100_write_reg(state
, STB6100_FCCK
, 0x0d | STB6100_FCCK_FCCK
)) < 0)
269 if ((rc
= stb6100_write_reg(state
, STB6100_F
, 0xc0 | tmp
)) < 0)
272 if ((rc
= stb6100_write_reg(state
, STB6100_FCCK
, 0x0d)) < 0)
278 static int stb6100_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
281 u32 nint
, nfrac
, fvco
;
283 struct stb6100_state
*state
= fe
->tuner_priv
;
284 u8 regs
[STB6100_NUMREGS
];
286 if ((rc
= stb6100_read_regs(state
, regs
)) < 0)
289 odiv
= (regs
[STB6100_VCO
] & STB6100_VCO_ODIV
) >> STB6100_VCO_ODIV_SHIFT
;
290 psd2
= (regs
[STB6100_K
] & STB6100_K_PSD2
) >> STB6100_K_PSD2_SHIFT
;
291 nint
= regs
[STB6100_NI
];
292 nfrac
= ((regs
[STB6100_K
] & STB6100_K_NF_MSB
) << 8) | regs
[STB6100_NF_LSB
];
293 fvco
= (nfrac
* state
->reference
>> (9 - psd2
)) + (nint
* state
->reference
<< psd2
);
294 *frequency
= state
->frequency
= fvco
>> (odiv
+ 1);
296 dprintk(verbose
, FE_DEBUG
, 1,
297 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
298 state
->frequency
, odiv
, psd2
, state
->reference
, fvco
, nint
, nfrac
);
303 static int stb6100_set_frequency(struct dvb_frontend
*fe
, u32 frequency
)
306 const struct stb6100_lkup
*ptr
;
307 struct stb6100_state
*state
= fe
->tuner_priv
;
308 struct dvb_frontend_parameters p
;
310 u32 srate
= 0, fvco
, nint
, nfrac
;
311 u8 regs
[STB6100_NUMREGS
];
314 if ((rc
= stb6100_read_regs(state
, regs
)) < 0)
317 if (fe
->ops
.get_frontend
) {
318 dprintk(verbose
, FE_DEBUG
, 1, "Get frontend parameters");
319 fe
->ops
.get_frontend(fe
, &p
);
321 srate
= p
.u
.qpsk
.symbol_rate
;
323 regs
[STB6100_DLB
] = 0xdc;
325 regs
[STB6100_LPEN
] &= ~STB6100_LPEN_LPEN
; /* PLL Loop disabled */
327 if ((rc
= stb6100_write_regs(state
, regs
)) < 0)
331 if (srate
>= 15000000)
333 else if (srate
>= 5000000)
338 regs
[STB6100_G
] = (regs
[STB6100_G
] & ~STB6100_G_G
) | g
;
339 regs
[STB6100_G
] &= ~STB6100_G_GCT
; /* mask GCT */
340 regs
[STB6100_G
] |= (1 << 5); /* 2Vp-p Mode */
342 /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */
343 if (frequency
<= 1075000)
347 regs
[STB6100_VCO
] = (regs
[STB6100_VCO
] & ~STB6100_VCO_ODIV
) | (odiv
<< STB6100_VCO_ODIV_SHIFT
);
349 if ((frequency
> 1075000) && (frequency
<= 1325000))
353 regs
[STB6100_K
] = (regs
[STB6100_K
] & ~STB6100_K_PSD2
) | (psd2
<< STB6100_K_PSD2_SHIFT
);
357 (ptr
->val_high
!= 0) && !CHKRANGE(frequency
, ptr
->val_low
, ptr
->val_high
);
359 if (ptr
->val_high
== 0) {
360 printk(KERN_ERR
"%s: frequency out of range: %u kHz\n", __func__
, frequency
);
363 regs
[STB6100_VCO
] = (regs
[STB6100_VCO
] & ~STB6100_VCO_OSM
) | ptr
->reg
;
365 /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */
366 fvco
= frequency
<< (1 + odiv
);
367 /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */
368 nint
= fvco
/ (state
->reference
<< psd2
);
369 /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */
370 nfrac
= (((fvco
- (nint
* state
->reference
<< psd2
)) << (9 - psd2
)) + state
->reference
/ 2) / state
->reference
;
371 dprintk(verbose
, FE_DEBUG
, 1,
372 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
373 frequency
, srate
, (unsigned int)g
, (unsigned int)odiv
,
374 (unsigned int)psd2
, state
->reference
,
375 ptr
->reg
, fvco
, nint
, nfrac
);
376 regs
[STB6100_NI
] = nint
;
377 regs
[STB6100_NF_LSB
] = nfrac
;
378 regs
[STB6100_K
] = (regs
[STB6100_K
] & ~STB6100_K_NF_MSB
) | ((nfrac
>> 8) & STB6100_K_NF_MSB
);
379 regs
[STB6100_VCO
] |= STB6100_VCO_OSCH
; /* VCO search enabled */
380 regs
[STB6100_VCO
] |= STB6100_VCO_OCK
; /* VCO search clock off */
381 regs
[STB6100_FCCK
] |= STB6100_FCCK_FCCK
; /* LPF BW setting clock enabled */
382 regs
[STB6100_LPEN
] &= ~STB6100_LPEN_LPEN
; /* PLL loop disabled */
384 regs
[STB6100_LPEN
] |= STB6100_LPEN_SYNP
| STB6100_LPEN_OSCP
| STB6100_LPEN_BEN
;
387 if ((rc
= stb6100_write_regs(state
, regs
)) < 0)
391 regs
[STB6100_LPEN
] |= STB6100_LPEN_LPEN
; /* PLL loop enabled */
392 if ((rc
= stb6100_write_reg(state
, STB6100_LPEN
, regs
[STB6100_LPEN
])) < 0)
395 regs
[STB6100_VCO
] &= ~STB6100_VCO_OCK
; /* VCO fast search */
396 if ((rc
= stb6100_write_reg(state
, STB6100_VCO
, regs
[STB6100_VCO
])) < 0)
399 msleep(10); /* wait for LO to lock */
400 regs
[STB6100_VCO
] &= ~STB6100_VCO_OSCH
; /* vco search disabled */
401 regs
[STB6100_VCO
] |= STB6100_VCO_OCK
; /* search clock off */
402 if ((rc
= stb6100_write_reg(state
, STB6100_VCO
, regs
[STB6100_VCO
])) < 0)
404 regs
[STB6100_FCCK
] &= ~STB6100_FCCK_FCCK
; /* LPF BW clock disabled */
405 stb6100_normalise_regs(regs
);
406 if ((rc
= stb6100_write_reg_range(state
, ®s
[1], 1, STB6100_NUMREGS
- 3)) < 0)
414 static int stb6100_sleep(struct dvb_frontend
*fe
)
416 /* TODO: power down */
420 static int stb6100_init(struct dvb_frontend
*fe
)
422 struct stb6100_state
*state
= fe
->tuner_priv
;
423 struct tuner_state
*status
= &state
->status
;
425 status
->tunerstep
= 125000;
427 status
->refclock
= 27000000; /* Hz */
429 status
->bandwidth
= 36000; /* kHz */
430 state
->bandwidth
= status
->bandwidth
* 1000; /* Hz */
431 state
->reference
= status
->refclock
/ 1000; /* kHz */
433 /* Set default bandwidth. */
434 return stb6100_set_bandwidth(fe
, state
->bandwidth
);
437 static int stb6100_get_state(struct dvb_frontend
*fe
,
438 enum tuner_param param
,
439 struct tuner_state
*state
)
442 case DVBFE_TUNER_FREQUENCY
:
443 stb6100_get_frequency(fe
, &state
->frequency
);
445 case DVBFE_TUNER_TUNERSTEP
:
447 case DVBFE_TUNER_IFFREQ
:
449 case DVBFE_TUNER_BANDWIDTH
:
450 stb6100_get_bandwidth(fe
, &state
->bandwidth
);
452 case DVBFE_TUNER_REFCLOCK
:
461 static int stb6100_set_state(struct dvb_frontend
*fe
,
462 enum tuner_param param
,
463 struct tuner_state
*state
)
465 struct stb6100_state
*tstate
= fe
->tuner_priv
;
468 case DVBFE_TUNER_FREQUENCY
:
469 stb6100_set_frequency(fe
, state
->frequency
);
470 tstate
->frequency
= state
->frequency
;
472 case DVBFE_TUNER_TUNERSTEP
:
474 case DVBFE_TUNER_IFFREQ
:
476 case DVBFE_TUNER_BANDWIDTH
:
477 stb6100_set_bandwidth(fe
, state
->bandwidth
);
478 tstate
->bandwidth
= state
->bandwidth
;
480 case DVBFE_TUNER_REFCLOCK
:
489 static struct dvb_tuner_ops stb6100_ops
= {
491 .name
= "STB6100 Silicon Tuner",
492 .frequency_min
= 950000,
493 .frequency_max
= 2150000,
497 .init
= stb6100_init
,
498 .sleep
= stb6100_sleep
,
499 .get_status
= stb6100_get_status
,
500 .get_state
= stb6100_get_state
,
501 .set_state
= stb6100_set_state
,
502 .release
= stb6100_release
505 struct dvb_frontend
*stb6100_attach(struct dvb_frontend
*fe
,
506 struct stb6100_config
*config
,
507 struct i2c_adapter
*i2c
)
509 struct stb6100_state
*state
= NULL
;
511 state
= kzalloc(sizeof (struct stb6100_state
), GFP_KERNEL
);
515 state
->config
= config
;
517 state
->frontend
= fe
;
518 state
->reference
= config
->refclock
/ 1000; /* kHz */
519 fe
->tuner_priv
= state
;
520 fe
->ops
.tuner_ops
= stb6100_ops
;
522 printk("%s: Attaching STB6100 \n", __func__
);
530 static int stb6100_release(struct dvb_frontend
*fe
)
532 struct stb6100_state
*state
= fe
->tuner_priv
;
534 fe
->tuner_priv
= NULL
;
540 EXPORT_SYMBOL(stb6100_attach
);
541 MODULE_PARM_DESC(verbose
, "Set Verbosity level");
543 MODULE_AUTHOR("Manu Abraham");
544 MODULE_DESCRIPTION("STB6100 Silicon tuner");
545 MODULE_LICENSE("GPL");