Commit | Line | Data |
---|---|---|
c46b6562 MA |
1 | /* |
2 | STB6100 Silicon Tuner | |
3 | Copyright (C) Manu Abraham (abraham.manu@gmail.com) | |
4 | ||
5 | Copyright (C) ST Microelectronics | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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. | |
20 | */ | |
21 | ||
22 | #include <linux/init.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/module.h> | |
5a0e3ad6 | 25 | #include <linux/slab.h> |
c46b6562 MA |
26 | #include <linux/string.h> |
27 | ||
28 | #include "dvb_frontend.h" | |
29 | #include "stb6100.h" | |
30 | ||
31 | static unsigned int verbose; | |
32 | module_param(verbose, int, 0644); | |
33 | ||
34 | ||
35 | #define FE_ERROR 0 | |
36 | #define FE_NOTICE 1 | |
37 | #define FE_INFO 2 | |
38 | #define FE_DEBUG 3 | |
39 | ||
40 | #define dprintk(x, y, z, format, arg...) do { \ | |
41 | if (z) { \ | |
42 | if ((x > FE_ERROR) && (x > y)) \ | |
43 | printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \ | |
44 | else if ((x > FE_NOTICE) && (x > y)) \ | |
45 | printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \ | |
46 | else if ((x > FE_INFO) && (x > y)) \ | |
47 | printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \ | |
48 | else if ((x > FE_DEBUG) && (x > y)) \ | |
49 | printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \ | |
50 | } else { \ | |
51 | if (x > y) \ | |
52 | printk(format, ##arg); \ | |
53 | } \ | |
f14bfe94 | 54 | } while (0) |
c46b6562 MA |
55 | |
56 | struct stb6100_lkup { | |
57 | u32 val_low; | |
58 | u32 val_high; | |
59 | u8 reg; | |
60 | }; | |
61 | ||
62 | static int stb6100_release(struct dvb_frontend *fe); | |
63 | ||
64 | static const struct stb6100_lkup lkup[] = { | |
65 | { 0, 950000, 0x0a }, | |
66 | { 950000, 1000000, 0x0a }, | |
67 | { 1000000, 1075000, 0x0c }, | |
68 | { 1075000, 1200000, 0x00 }, | |
69 | { 1200000, 1300000, 0x01 }, | |
70 | { 1300000, 1370000, 0x02 }, | |
71 | { 1370000, 1470000, 0x04 }, | |
72 | { 1470000, 1530000, 0x05 }, | |
73 | { 1530000, 1650000, 0x06 }, | |
74 | { 1650000, 1800000, 0x08 }, | |
75 | { 1800000, 1950000, 0x0a }, | |
76 | { 1950000, 2150000, 0x0c }, | |
77 | { 2150000, 9999999, 0x0c }, | |
78 | { 0, 0, 0x00 } | |
79 | }; | |
80 | ||
81 | /* Register names for easy debugging. */ | |
82 | static const char *stb6100_regnames[] = { | |
83 | [STB6100_LD] = "LD", | |
84 | [STB6100_VCO] = "VCO", | |
85 | [STB6100_NI] = "NI", | |
86 | [STB6100_NF_LSB] = "NF", | |
87 | [STB6100_K] = "K", | |
88 | [STB6100_G] = "G", | |
89 | [STB6100_F] = "F", | |
90 | [STB6100_DLB] = "DLB", | |
91 | [STB6100_TEST1] = "TEST1", | |
92 | [STB6100_FCCK] = "FCCK", | |
93 | [STB6100_LPEN] = "LPEN", | |
94 | [STB6100_TEST3] = "TEST3", | |
95 | }; | |
96 | ||
97 | /* Template for normalisation, i.e. setting unused or undocumented | |
98 | * bits as required according to the documentation. | |
99 | */ | |
100 | struct stb6100_regmask { | |
101 | u8 mask; | |
102 | u8 set; | |
103 | }; | |
104 | ||
105 | static const struct stb6100_regmask stb6100_template[] = { | |
106 | [STB6100_LD] = { 0xff, 0x00 }, | |
107 | [STB6100_VCO] = { 0xff, 0x00 }, | |
108 | [STB6100_NI] = { 0xff, 0x00 }, | |
109 | [STB6100_NF_LSB] = { 0xff, 0x00 }, | |
110 | [STB6100_K] = { 0xc7, 0x38 }, | |
111 | [STB6100_G] = { 0xef, 0x10 }, | |
112 | [STB6100_F] = { 0x1f, 0xc0 }, | |
113 | [STB6100_DLB] = { 0x38, 0xc4 }, | |
114 | [STB6100_TEST1] = { 0x00, 0x8f }, | |
115 | [STB6100_FCCK] = { 0x40, 0x0d }, | |
116 | [STB6100_LPEN] = { 0xf0, 0x0b }, | |
117 | [STB6100_TEST3] = { 0x00, 0xde }, | |
118 | }; | |
119 | ||
120 | static void stb6100_normalise_regs(u8 regs[]) | |
121 | { | |
122 | int i; | |
123 | ||
124 | for (i = 0; i < STB6100_NUMREGS; i++) | |
125 | regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set; | |
126 | } | |
127 | ||
128 | static int stb6100_read_regs(struct stb6100_state *state, u8 regs[]) | |
129 | { | |
130 | int rc; | |
131 | struct i2c_msg msg = { | |
132 | .addr = state->config->tuner_address, | |
133 | .flags = I2C_M_RD, | |
134 | .buf = regs, | |
135 | .len = STB6100_NUMREGS | |
136 | }; | |
137 | ||
c46b6562 | 138 | rc = i2c_transfer(state->i2c, &msg, 1); |
c46b6562 MA |
139 | if (unlikely(rc != 1)) { |
140 | dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]", | |
141 | state->config->tuner_address, rc); | |
142 | ||
143 | return -EREMOTEIO; | |
144 | } | |
145 | if (unlikely(verbose > FE_DEBUG)) { | |
146 | int i; | |
147 | ||
148 | dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address); | |
149 | for (i = 0; i < STB6100_NUMREGS; i++) | |
150 | dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[i], regs[i]); | |
151 | } | |
152 | return 0; | |
153 | } | |
154 | ||
155 | static int stb6100_read_reg(struct stb6100_state *state, u8 reg) | |
156 | { | |
157 | u8 regs[STB6100_NUMREGS]; | |
158 | int rc; | |
159 | ||
f14bfe94 MA |
160 | struct i2c_msg msg = { |
161 | .addr = state->config->tuner_address + reg, | |
162 | .flags = I2C_M_RD, | |
163 | .buf = regs, | |
164 | .len = 1 | |
165 | }; | |
166 | ||
167 | rc = i2c_transfer(state->i2c, &msg, 1); | |
168 | ||
c46b6562 MA |
169 | if (unlikely(reg >= STB6100_NUMREGS)) { |
170 | dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg); | |
171 | return -EINVAL; | |
172 | } | |
f14bfe94 MA |
173 | if (unlikely(verbose > FE_DEBUG)) { |
174 | dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address); | |
175 | dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[reg], regs[0]); | |
176 | } | |
177 | ||
178 | return (unsigned int)regs[0]; | |
c46b6562 MA |
179 | } |
180 | ||
181 | static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len) | |
182 | { | |
183 | int rc; | |
184 | u8 cmdbuf[len + 1]; | |
185 | struct i2c_msg msg = { | |
186 | .addr = state->config->tuner_address, | |
187 | .flags = 0, | |
188 | .buf = cmdbuf, | |
189 | .len = len + 1 | |
190 | }; | |
191 | ||
192 | if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) { | |
193 | dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d", | |
194 | start, len); | |
195 | return -EINVAL; | |
196 | } | |
197 | memcpy(&cmdbuf[1], buf, len); | |
198 | cmdbuf[0] = start; | |
199 | ||
200 | if (unlikely(verbose > FE_DEBUG)) { | |
201 | int i; | |
202 | ||
203 | dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len); | |
204 | for (i = 0; i < len; i++) | |
205 | dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]); | |
206 | } | |
c46b6562 | 207 | rc = i2c_transfer(state->i2c, &msg, 1); |
c46b6562 MA |
208 | if (unlikely(rc != 1)) { |
209 | dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]", | |
210 | (unsigned int)state->config->tuner_address, start, len, rc); | |
211 | return -EREMOTEIO; | |
212 | } | |
213 | return 0; | |
214 | } | |
215 | ||
216 | static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data) | |
217 | { | |
218 | if (unlikely(reg >= STB6100_NUMREGS)) { | |
219 | dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg); | |
220 | return -EREMOTEIO; | |
221 | } | |
222 | data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set; | |
223 | return stb6100_write_reg_range(state, &data, reg, 1); | |
224 | } | |
225 | ||
c46b6562 MA |
226 | |
227 | static int stb6100_get_status(struct dvb_frontend *fe, u32 *status) | |
228 | { | |
229 | int rc; | |
230 | struct stb6100_state *state = fe->tuner_priv; | |
231 | ||
f14bfe94 MA |
232 | rc = stb6100_read_reg(state, STB6100_LD); |
233 | if (rc < 0) { | |
234 | dprintk(verbose, FE_ERROR, 1, "%s failed", __func__); | |
c46b6562 | 235 | return rc; |
f14bfe94 | 236 | } |
c46b6562 MA |
237 | return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0; |
238 | } | |
239 | ||
240 | static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) | |
241 | { | |
242 | int rc; | |
243 | u8 f; | |
244 | struct stb6100_state *state = fe->tuner_priv; | |
245 | ||
f14bfe94 MA |
246 | rc = stb6100_read_reg(state, STB6100_F); |
247 | if (rc < 0) | |
c46b6562 MA |
248 | return rc; |
249 | f = rc & STB6100_F_F; | |
250 | ||
251 | state->status.bandwidth = (f + 5) * 2000; /* x2 for ZIF */ | |
252 | ||
253 | *bandwidth = state->bandwidth = state->status.bandwidth * 1000; | |
254 | dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth) | |
259 | { | |
260 | u32 tmp; | |
261 | int rc; | |
262 | struct stb6100_state *state = fe->tuner_priv; | |
263 | ||
89693b7d | 264 | dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth); |
c46b6562 | 265 | |
763fbaf6 | 266 | bandwidth /= 2; /* ZIF */ |
c46b6562 | 267 | |
89693b7d | 268 | if (bandwidth >= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */ |
c46b6562 | 269 | tmp = 31; |
89693b7d | 270 | else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */ |
c46b6562 MA |
271 | tmp = 0; |
272 | else /* if 5 < bw/2 < 36 */ | |
6f6c268b | 273 | tmp = (bandwidth + 500000) / 1000000 - 5; |
c46b6562 MA |
274 | |
275 | /* Turn on LPF bandwidth setting clock control, | |
276 | * set bandwidth, wait 10ms, turn off. | |
277 | */ | |
f14bfe94 MA |
278 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK); |
279 | if (rc < 0) | |
c46b6562 | 280 | return rc; |
f14bfe94 MA |
281 | rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp); |
282 | if (rc < 0) | |
c46b6562 | 283 | return rc; |
f14bfe94 MA |
284 | |
285 | msleep(5); /* This is dangerous as another (related) thread may start */ | |
286 | ||
287 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d); | |
288 | if (rc < 0) | |
c46b6562 MA |
289 | return rc; |
290 | ||
f14bfe94 MA |
291 | msleep(10); /* This is dangerous as another (related) thread may start */ |
292 | ||
c46b6562 MA |
293 | return 0; |
294 | } | |
295 | ||
296 | static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency) | |
297 | { | |
298 | int rc; | |
299 | u32 nint, nfrac, fvco; | |
300 | int psd2, odiv; | |
301 | struct stb6100_state *state = fe->tuner_priv; | |
302 | u8 regs[STB6100_NUMREGS]; | |
303 | ||
f14bfe94 MA |
304 | rc = stb6100_read_regs(state, regs); |
305 | if (rc < 0) | |
c46b6562 MA |
306 | return rc; |
307 | ||
308 | odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT; | |
309 | psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT; | |
310 | nint = regs[STB6100_NI]; | |
311 | nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB]; | |
312 | fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2); | |
313 | *frequency = state->frequency = fvco >> (odiv + 1); | |
314 | ||
315 | dprintk(verbose, FE_DEBUG, 1, | |
316 | "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u", | |
317 | state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | ||
322 | static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency) | |
323 | { | |
324 | int rc; | |
325 | const struct stb6100_lkup *ptr; | |
326 | struct stb6100_state *state = fe->tuner_priv; | |
3f400925 | 327 | struct dvb_frontend_parameters p; |
c46b6562 MA |
328 | |
329 | u32 srate = 0, fvco, nint, nfrac; | |
330 | u8 regs[STB6100_NUMREGS]; | |
331 | u8 g, psd2, odiv; | |
332 | ||
f14bfe94 | 333 | dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51"); |
3f400925 MA |
334 | |
335 | if (fe->ops.get_frontend) { | |
336 | dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters"); | |
337 | fe->ops.get_frontend(fe, &p); | |
c46b6562 | 338 | } |
3f400925 | 339 | srate = p.u.qpsk.symbol_rate; |
c46b6562 | 340 | |
f14bfe94 MA |
341 | /* Set up tuner cleanly, LPF calibration on */ |
342 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK); | |
343 | if (rc < 0) | |
344 | return rc; /* allow LPF calibration */ | |
20dafb3b | 345 | |
f14bfe94 MA |
346 | /* PLL Loop disabled, bias on, VCO on, synth on */ |
347 | regs[STB6100_LPEN] = 0xeb; | |
348 | rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]); | |
349 | if (rc < 0) | |
20dafb3b AJ |
350 | return rc; |
351 | ||
f14bfe94 | 352 | /* Program the registers with their data values */ |
c46b6562 MA |
353 | |
354 | /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */ | |
355 | if (frequency <= 1075000) | |
356 | odiv = 1; | |
357 | else | |
358 | odiv = 0; | |
c46b6562 | 359 | |
f14bfe94 MA |
360 | /* VCO enabled, seach clock off as per LL3.7, 3.4.1 */ |
361 | regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT); | |
c46b6562 MA |
362 | |
363 | /* OSM */ | |
364 | for (ptr = lkup; | |
365 | (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high); | |
366 | ptr++); | |
f14bfe94 | 367 | |
c46b6562 MA |
368 | if (ptr->val_high == 0) { |
369 | printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency); | |
370 | return -EINVAL; | |
371 | } | |
372 | regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg; | |
f14bfe94 MA |
373 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
374 | if (rc < 0) | |
375 | return rc; | |
c46b6562 | 376 | |
f14bfe94 MA |
377 | if ((frequency > 1075000) && (frequency <= 1325000)) |
378 | psd2 = 0; | |
379 | else | |
380 | psd2 = 1; | |
c46b6562 MA |
381 | /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */ |
382 | fvco = frequency << (1 + odiv); | |
383 | /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */ | |
384 | nint = fvco / (state->reference << psd2); | |
385 | /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */ | |
75b697f7 | 386 | nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2)) |
f14bfe94 MA |
387 | << (9 - psd2), state->reference); |
388 | ||
389 | /* NI */ | |
390 | regs[STB6100_NI] = nint; | |
391 | rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]); | |
392 | if (rc < 0) | |
393 | return rc; | |
394 | ||
395 | /* NF */ | |
396 | regs[STB6100_NF_LSB] = nfrac; | |
397 | rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]); | |
398 | if (rc < 0) | |
399 | return rc; | |
400 | ||
401 | /* K */ | |
402 | regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT); | |
403 | regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB); | |
404 | rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]); | |
405 | if (rc < 0) | |
406 | return rc; | |
407 | ||
408 | /* G Baseband gain. */ | |
409 | if (srate >= 15000000) | |
410 | g = 9; /* +4 dB */ | |
411 | else if (srate >= 5000000) | |
412 | g = 11; /* +8 dB */ | |
413 | else | |
414 | g = 14; /* +14 dB */ | |
415 | ||
416 | regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g; | |
417 | regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */ | |
418 | regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */ | |
419 | rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]); | |
420 | if (rc < 0) | |
421 | return rc; | |
422 | ||
423 | /* F we don't write as it is set up in BW set */ | |
424 | ||
425 | /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */ | |
426 | regs[STB6100_DLB] = 0xcc; | |
427 | rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]); | |
428 | if (rc < 0) | |
429 | return rc; | |
430 | ||
c46b6562 MA |
431 | dprintk(verbose, FE_DEBUG, 1, |
432 | "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u", | |
433 | frequency, srate, (unsigned int)g, (unsigned int)odiv, | |
434 | (unsigned int)psd2, state->reference, | |
435 | ptr->reg, fvco, nint, nfrac); | |
c46b6562 | 436 | |
f14bfe94 MA |
437 | /* Set up the test registers */ |
438 | regs[STB6100_TEST1] = 0x8f; | |
439 | rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]); | |
440 | if (rc < 0) | |
441 | return rc; | |
442 | regs[STB6100_TEST3] = 0xde; | |
443 | rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]); | |
444 | if (rc < 0) | |
c46b6562 MA |
445 | return rc; |
446 | ||
f14bfe94 MA |
447 | /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */ |
448 | regs[STB6100_LPEN] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */ | |
449 | rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]); | |
450 | if (rc < 0) | |
c46b6562 MA |
451 | return rc; |
452 | ||
f14bfe94 MA |
453 | msleep(2); |
454 | ||
455 | /* Bring up tuner according to LLA 3.7 3.4.1, step 3 */ | |
c46b6562 | 456 | regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */ |
f14bfe94 MA |
457 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
458 | if (rc < 0) | |
c46b6562 MA |
459 | return rc; |
460 | ||
f14bfe94 MA |
461 | msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */ |
462 | ||
c46b6562 MA |
463 | regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */ |
464 | regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */ | |
f14bfe94 MA |
465 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
466 | if (rc < 0) | |
c46b6562 MA |
467 | return rc; |
468 | ||
f14bfe94 MA |
469 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d); |
470 | if (rc < 0) | |
471 | return rc; /* Stop LPF calibration */ | |
7e4e8c52 | 472 | |
f14bfe94 MA |
473 | msleep(10); /* This is dangerous as another (related) thread may start */ |
474 | /* wait for stabilisation, (should not be necessary) */ | |
c46b6562 MA |
475 | return 0; |
476 | } | |
477 | ||
478 | static int stb6100_sleep(struct dvb_frontend *fe) | |
479 | { | |
480 | /* TODO: power down */ | |
481 | return 0; | |
482 | } | |
483 | ||
484 | static int stb6100_init(struct dvb_frontend *fe) | |
485 | { | |
486 | struct stb6100_state *state = fe->tuner_priv; | |
487 | struct tuner_state *status = &state->status; | |
488 | ||
489 | status->tunerstep = 125000; | |
490 | status->ifreq = 0; | |
491 | status->refclock = 27000000; /* Hz */ | |
492 | status->iqsense = 1; | |
493 | status->bandwidth = 36000; /* kHz */ | |
26f26fa8 | 494 | state->bandwidth = status->bandwidth * 1000; /* Hz */ |
c46b6562 MA |
495 | state->reference = status->refclock / 1000; /* kHz */ |
496 | ||
f14bfe94 MA |
497 | /* Set default bandwidth. Modified, PN 13-May-10 */ |
498 | return 0; | |
c46b6562 MA |
499 | } |
500 | ||
501 | static int stb6100_get_state(struct dvb_frontend *fe, | |
502 | enum tuner_param param, | |
503 | struct tuner_state *state) | |
504 | { | |
505 | switch (param) { | |
506 | case DVBFE_TUNER_FREQUENCY: | |
507 | stb6100_get_frequency(fe, &state->frequency); | |
508 | break; | |
509 | case DVBFE_TUNER_TUNERSTEP: | |
510 | break; | |
511 | case DVBFE_TUNER_IFFREQ: | |
512 | break; | |
513 | case DVBFE_TUNER_BANDWIDTH: | |
514 | stb6100_get_bandwidth(fe, &state->bandwidth); | |
515 | break; | |
516 | case DVBFE_TUNER_REFCLOCK: | |
517 | break; | |
518 | default: | |
519 | break; | |
520 | } | |
521 | ||
522 | return 0; | |
523 | } | |
524 | ||
525 | static int stb6100_set_state(struct dvb_frontend *fe, | |
526 | enum tuner_param param, | |
527 | struct tuner_state *state) | |
528 | { | |
529 | struct stb6100_state *tstate = fe->tuner_priv; | |
530 | ||
531 | switch (param) { | |
532 | case DVBFE_TUNER_FREQUENCY: | |
533 | stb6100_set_frequency(fe, state->frequency); | |
7d8f1e57 | 534 | tstate->frequency = state->frequency; |
c46b6562 MA |
535 | break; |
536 | case DVBFE_TUNER_TUNERSTEP: | |
537 | break; | |
538 | case DVBFE_TUNER_IFFREQ: | |
539 | break; | |
540 | case DVBFE_TUNER_BANDWIDTH: | |
541 | stb6100_set_bandwidth(fe, state->bandwidth); | |
7d8f1e57 | 542 | tstate->bandwidth = state->bandwidth; |
c46b6562 MA |
543 | break; |
544 | case DVBFE_TUNER_REFCLOCK: | |
545 | break; | |
546 | default: | |
547 | break; | |
548 | } | |
549 | ||
550 | return 0; | |
551 | } | |
552 | ||
553 | static struct dvb_tuner_ops stb6100_ops = { | |
554 | .info = { | |
555 | .name = "STB6100 Silicon Tuner", | |
556 | .frequency_min = 950000, | |
557 | .frequency_max = 2150000, | |
558 | .frequency_step = 0, | |
559 | }, | |
560 | ||
561 | .init = stb6100_init, | |
562 | .sleep = stb6100_sleep, | |
563 | .get_status = stb6100_get_status, | |
564 | .get_state = stb6100_get_state, | |
565 | .set_state = stb6100_set_state, | |
c46b6562 MA |
566 | .release = stb6100_release |
567 | }; | |
568 | ||
569 | struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe, | |
2e4e98e7 | 570 | const struct stb6100_config *config, |
c46b6562 MA |
571 | struct i2c_adapter *i2c) |
572 | { | |
573 | struct stb6100_state *state = NULL; | |
574 | ||
a18d4315 | 575 | state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL); |
c46b6562 MA |
576 | if (state == NULL) |
577 | goto error; | |
578 | ||
579 | state->config = config; | |
580 | state->i2c = i2c; | |
581 | state->frontend = fe; | |
3e3263e6 | 582 | state->reference = config->refclock / 1000; /* kHz */ |
c46b6562 MA |
583 | fe->tuner_priv = state; |
584 | fe->ops.tuner_ops = stb6100_ops; | |
585 | ||
3e3263e6 | 586 | printk("%s: Attaching STB6100 \n", __func__); |
c46b6562 MA |
587 | return fe; |
588 | ||
589 | error: | |
590 | kfree(state); | |
c46b6562 MA |
591 | return NULL; |
592 | } | |
593 | ||
594 | static int stb6100_release(struct dvb_frontend *fe) | |
595 | { | |
596 | struct stb6100_state *state = fe->tuner_priv; | |
597 | ||
598 | fe->tuner_priv = NULL; | |
c46b6562 MA |
599 | kfree(state); |
600 | ||
601 | return 0; | |
602 | } | |
603 | ||
604 | EXPORT_SYMBOL(stb6100_attach); | |
605 | MODULE_PARM_DESC(verbose, "Set Verbosity level"); | |
606 | ||
607 | MODULE_AUTHOR("Manu Abraham"); | |
608 | MODULE_DESCRIPTION("STB6100 Silicon tuner"); | |
609 | MODULE_LICENSE("GPL"); |