blob: 35973e7faac3c719b23f279b473a49997cfa085a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * descriptions + helper functions for simple dvb plls.
3 *
4 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/dvb/frontend.h>
23#include <asm/types.h>
24
25#include "dvb-pll.h"
26
27/* ----------------------------------------------------------- */
28/* descriptions */
29
Trent Piepho26aed922007-04-27 12:31:29 -030030/* Set AGC TOP value to 103 dBuV:
31 0x80 = Control Byte
32 0x40 = 250 uA charge pump (irrelevant)
33 0x18 = Aux Byte to follow
34 0x06 = 64.5 kHz divider (irrelevant)
35 0x01 = Disable Vt (aka sleep)
36
37 0x00 = AGC Time constant 2s Iagc = 300 nA (vs 0x80 = 9 nA)
38 0x50 = AGC Take over point = 103 dBuV */
39static u8 tua603x_agc103[] = { 2, 0x80|0x40|0x18|0x06|0x01, 0x00|0x50 };
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
42 .name = "Thomson dtt7579",
43 .min = 177000000,
44 .max = 858000000,
45 .count = 5,
46 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -030047 { 0, 36166667, 166667, 0xb4, 0x03 }, /* go sleep */
48 { 443250000, 36166667, 166667, 0xb4, 0x02 },
49 { 542000000, 36166667, 166667, 0xb4, 0x08 },
50 { 771000000, 36166667, 166667, 0xbc, 0x08 },
51 { 999999999, 36166667, 166667, 0xf4, 0x08 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 },
53};
54EXPORT_SYMBOL(dvb_pll_thomson_dtt7579);
55
56struct dvb_pll_desc dvb_pll_thomson_dtt7610 = {
57 .name = "Thomson dtt7610",
58 .min = 44000000,
59 .max = 958000000,
60 .count = 3,
61 .entries = {
62 { 157250000, 44000000, 62500, 0x8e, 0x39 },
63 { 454000000, 44000000, 62500, 0x8e, 0x3a },
64 { 999999999, 44000000, 62500, 0x8e, 0x3c },
65 },
66};
67EXPORT_SYMBOL(dvb_pll_thomson_dtt7610);
68
Patrick Boettcher49dc82f2005-07-07 17:58:09 -070069static void thomson_dtt759x_bw(u8 *buf, u32 freq, int bandwidth)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 if (BANDWIDTH_7_MHZ == bandwidth)
72 buf[3] |= 0x10;
73}
74
75struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
76 .name = "Thomson dtt759x",
77 .min = 177000000,
78 .max = 896000000,
79 .setbw = thomson_dtt759x_bw,
80 .count = 6,
81 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -030082 { 0, 36166667, 166667, 0x84, 0x03 },
83 { 264000000, 36166667, 166667, 0xb4, 0x02 },
84 { 470000000, 36166667, 166667, 0xbc, 0x02 },
85 { 735000000, 36166667, 166667, 0xbc, 0x08 },
86 { 835000000, 36166667, 166667, 0xf4, 0x08 },
87 { 999999999, 36166667, 166667, 0xfc, 0x08 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 },
89};
90EXPORT_SYMBOL(dvb_pll_thomson_dtt759x);
91
92struct dvb_pll_desc dvb_pll_lg_z201 = {
93 .name = "LG z201",
94 .min = 174000000,
95 .max = 862000000,
Gregory B Frost9e00e482005-07-27 11:45:48 -070096 .count = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -030098 { 0, 36166667, 166667, 0xbc, 0x03 },
99 { 157500000, 36166667, 166667, 0xbc, 0x01 },
100 { 443250000, 36166667, 166667, 0xbc, 0x02 },
101 { 542000000, 36166667, 166667, 0xbc, 0x04 },
102 { 830000000, 36166667, 166667, 0xf4, 0x04 },
103 { 999999999, 36166667, 166667, 0xfc, 0x04 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 },
105};
106EXPORT_SYMBOL(dvb_pll_lg_z201);
107
Mac Michaelsd8667cb2005-07-07 17:58:29 -0700108struct dvb_pll_desc dvb_pll_microtune_4042 = {
109 .name = "Microtune 4042 FI5",
110 .min = 57000000,
111 .max = 858000000,
112 .count = 3,
113 .entries = {
114 { 162000000, 44000000, 62500, 0x8e, 0xa1 },
115 { 457000000, 44000000, 62500, 0x8e, 0x91 },
116 { 999999999, 44000000, 62500, 0x8e, 0x31 },
117 },
118};
119EXPORT_SYMBOL(dvb_pll_microtune_4042);
120
Michael Krufky83ac87222006-01-09 15:25:29 -0200121struct dvb_pll_desc dvb_pll_thomson_dtt761x = {
122 /* DTT 7611 7611A 7612 7613 7613A 7614 7615 7615A */
123 .name = "Thomson dtt761x",
124 .min = 57000000,
125 .max = 863000000,
Michael Krufky0d723c02005-07-07 17:58:42 -0700126 .count = 3,
Trent Piepho26aed922007-04-27 12:31:29 -0300127 .initdata = tua603x_agc103,
Michael Krufky0d723c02005-07-07 17:58:42 -0700128 .entries = {
Michael Krufky83ac87222006-01-09 15:25:29 -0200129 { 147000000, 44000000, 62500, 0x8e, 0x39 },
130 { 417000000, 44000000, 62500, 0x8e, 0x3a },
Michael Krufky0d723c02005-07-07 17:58:42 -0700131 { 999999999, 44000000, 62500, 0x8e, 0x3c },
132 },
133};
Michael Krufky83ac87222006-01-09 15:25:29 -0200134EXPORT_SYMBOL(dvb_pll_thomson_dtt761x);
Michael Krufky0d723c02005-07-07 17:58:42 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct dvb_pll_desc dvb_pll_unknown_1 = {
137 .name = "unknown 1", /* used by dntv live dvb-t */
138 .min = 174000000,
139 .max = 862000000,
140 .count = 9,
141 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300142 { 150000000, 36166667, 166667, 0xb4, 0x01 },
143 { 173000000, 36166667, 166667, 0xbc, 0x01 },
144 { 250000000, 36166667, 166667, 0xb4, 0x02 },
145 { 400000000, 36166667, 166667, 0xbc, 0x02 },
146 { 420000000, 36166667, 166667, 0xf4, 0x02 },
147 { 470000000, 36166667, 166667, 0xfc, 0x02 },
148 { 600000000, 36166667, 166667, 0xbc, 0x08 },
149 { 730000000, 36166667, 166667, 0xf4, 0x08 },
150 { 999999999, 36166667, 166667, 0xfc, 0x08 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 },
152};
153EXPORT_SYMBOL(dvb_pll_unknown_1);
154
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700155/* Infineon TUA6010XS
156 * used in Thomson Cable Tuner
157 */
158struct dvb_pll_desc dvb_pll_tua6010xs = {
159 .name = "Infineon TUA6010XS",
160 .min = 44250000,
161 .max = 858000000,
162 .count = 3,
163 .entries = {
164 { 115750000, 36125000, 62500, 0x8e, 0x03 },
165 { 403250000, 36125000, 62500, 0x8e, 0x06 },
166 { 999999999, 36125000, 62500, 0x8e, 0x85 },
167 },
168};
169EXPORT_SYMBOL(dvb_pll_tua6010xs);
170
171/* Panasonic env57h1xd5 (some Philips PLL ?) */
172struct dvb_pll_desc dvb_pll_env57h1xd5 = {
173 .name = "Panasonic ENV57H1XD5",
174 .min = 44250000,
175 .max = 858000000,
176 .count = 4,
177 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300178 { 153000000, 36125000, 166667, 0xc2, 0x41 },
179 { 470000000, 36125000, 166667, 0xc2, 0x42 },
180 { 526000000, 36125000, 166667, 0xc2, 0x84 },
181 { 999999999, 36125000, 166667, 0xc2, 0xa4 },
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700182 },
183};
184EXPORT_SYMBOL(dvb_pll_env57h1xd5);
185
186/* Philips TDA6650/TDA6651
187 * used in Panasonic ENV77H11D5
188 */
Patrick Boettcher49dc82f2005-07-07 17:58:09 -0700189static void tda665x_bw(u8 *buf, u32 freq, int bandwidth)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700190{
191 if (bandwidth == BANDWIDTH_8_MHZ)
192 buf[3] |= 0x08;
193}
194
195struct dvb_pll_desc dvb_pll_tda665x = {
196 .name = "Philips TDA6650/TDA6651",
197 .min = 44250000,
198 .max = 858000000,
199 .setbw = tda665x_bw,
200 .count = 12,
201 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300202 { 93834000, 36166667, 166667, 0xca, 0x61 /* 011 0 0 0 01 */ },
203 { 123834000, 36166667, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
204 { 161000000, 36166667, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
205 { 163834000, 36166667, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
206 { 253834000, 36166667, 166667, 0xca, 0x62 /* 011 0 0 0 10 */ },
207 { 383834000, 36166667, 166667, 0xca, 0xa2 /* 101 0 0 0 10 */ },
208 { 443834000, 36166667, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
209 { 444000000, 36166667, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
210 { 583834000, 36166667, 166667, 0xca, 0x64 /* 011 0 0 1 00 */ },
211 { 793834000, 36166667, 166667, 0xca, 0xa4 /* 101 0 0 1 00 */ },
212 { 444834000, 36166667, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
213 { 861000000, 36166667, 166667, 0xca, 0xe4 /* 111 0 0 1 00 */ },
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700214 }
215};
216EXPORT_SYMBOL(dvb_pll_tda665x);
217
218/* Infineon TUA6034
219 * used in LG TDTP E102P
220 */
Patrick Boettcher49dc82f2005-07-07 17:58:09 -0700221static void tua6034_bw(u8 *buf, u32 freq, int bandwidth)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700222{
223 if (BANDWIDTH_7_MHZ != bandwidth)
224 buf[3] |= 0x08;
225}
226
227struct dvb_pll_desc dvb_pll_tua6034 = {
228 .name = "Infineon TUA6034",
229 .min = 44250000,
230 .max = 858000000,
231 .count = 3,
232 .setbw = tua6034_bw,
233 .entries = {
234 { 174500000, 36166667, 62500, 0xce, 0x01 },
235 { 230000000, 36166667, 62500, 0xce, 0x02 },
236 { 999999999, 36166667, 62500, 0xce, 0x04 },
237 },
238};
239EXPORT_SYMBOL(dvb_pll_tua6034);
240
Michael Krufky1963c902005-08-08 09:22:43 -0700241/* Infineon TUA6034
Michael Krufkyd9e12f22006-04-22 16:15:11 -0300242 * used in LG TDVS-H061F, LG TDVS-H062F and LG TDVS-H064F
Michael Krufky1963c902005-08-08 09:22:43 -0700243 */
Michael Krufky30fa61a2006-04-27 01:31:26 -0300244struct dvb_pll_desc dvb_pll_lg_tdvs_h06xf = {
Michael Krufkyd9e12f22006-04-22 16:15:11 -0300245 .name = "LG TDVS-H06xF",
Michael Krufky1963c902005-08-08 09:22:43 -0700246 .min = 54000000,
247 .max = 863000000,
Trent Piepho6bdcc6e2007-04-27 12:31:30 -0300248 .initdata = tua603x_agc103,
Michael Krufky1963c902005-08-08 09:22:43 -0700249 .count = 3,
250 .entries = {
Rusty Scottdd31d5ac2006-04-22 16:15:07 -0300251 { 165000000, 44000000, 62500, 0xce, 0x01 },
252 { 450000000, 44000000, 62500, 0xce, 0x02 },
Michael Krufky1963c902005-08-08 09:22:43 -0700253 { 999999999, 44000000, 62500, 0xce, 0x04 },
254 },
255};
Michael Krufky30fa61a2006-04-27 01:31:26 -0300256EXPORT_SYMBOL(dvb_pll_lg_tdvs_h06xf);
Michael Krufky1963c902005-08-08 09:22:43 -0700257
Patrick Boettcher49dc82f2005-07-07 17:58:09 -0700258/* Philips FMD1216ME
259 * used in Medion Hybrid PCMCIA card and USB Box
260 */
261static void fmd1216me_bw(u8 *buf, u32 freq, int bandwidth)
262{
263 if (bandwidth == BANDWIDTH_8_MHZ && freq >= 158870000)
264 buf[3] |= 0x08;
265}
266
267struct dvb_pll_desc dvb_pll_fmd1216me = {
Patrick Boettcher0589b8e2005-07-07 17:58:12 -0700268 .name = "Philips FMD1216ME",
Patrick Boettcher49dc82f2005-07-07 17:58:09 -0700269 .min = 50870000,
270 .max = 858000000,
271 .setbw = fmd1216me_bw,
272 .count = 7,
273 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300274 { 143870000, 36125000, 166667, 0xbc, 0x41 },
275 { 158870000, 36125000, 166667, 0xf4, 0x41 },
276 { 329870000, 36125000, 166667, 0xbc, 0x42 },
277 { 441870000, 36125000, 166667, 0xf4, 0x42 },
278 { 625870000, 36125000, 166667, 0xbc, 0x44 },
279 { 803870000, 36125000, 166667, 0xf4, 0x44 },
280 { 999999999, 36125000, 166667, 0xfc, 0x44 },
Patrick Boettcher49dc82f2005-07-07 17:58:09 -0700281 }
282};
283EXPORT_SYMBOL(dvb_pll_fmd1216me);
284
Patrick Boettcher0589b8e2005-07-07 17:58:12 -0700285/* ALPS TDED4
286 * used in Nebula-Cards and USB boxes
287 */
288static void tded4_bw(u8 *buf, u32 freq, int bandwidth)
289{
290 if (bandwidth == BANDWIDTH_8_MHZ)
291 buf[3] |= 0x04;
292}
293
294struct dvb_pll_desc dvb_pll_tded4 = {
295 .name = "ALPS TDED4",
296 .min = 47000000,
297 .max = 863000000,
298 .setbw = tded4_bw,
299 .count = 4,
300 .entries = {
301 { 153000000, 36166667, 166667, 0x85, 0x01 },
302 { 470000000, 36166667, 166667, 0x85, 0x02 },
303 { 823000000, 36166667, 166667, 0x85, 0x08 },
304 { 999999999, 36166667, 166667, 0x85, 0x88 },
305 }
306};
307EXPORT_SYMBOL(dvb_pll_tded4);
308
Kirk Lapray147418c2005-11-08 21:35:39 -0800309/* ALPS TDHU2
310 * used in AverTVHD MCE A180
311 */
312struct dvb_pll_desc dvb_pll_tdhu2 = {
313 .name = "ALPS TDHU2",
314 .min = 54000000,
315 .max = 864000000,
316 .count = 4,
317 .entries = {
318 { 162000000, 44000000, 62500, 0x85, 0x01 },
319 { 426000000, 44000000, 62500, 0x85, 0x02 },
320 { 782000000, 44000000, 62500, 0x85, 0x08 },
321 { 999999999, 44000000, 62500, 0x85, 0x88 },
322 }
323};
324EXPORT_SYMBOL(dvb_pll_tdhu2);
325
326/* Philips TUV1236D
327 * used in ATI HDTV Wonder
328 */
329struct dvb_pll_desc dvb_pll_tuv1236d = {
330 .name = "Philips TUV1236D",
Kirk Lapray04a45922005-11-08 21:35:46 -0800331 .min = 54000000,
Kirk Lapray147418c2005-11-08 21:35:39 -0800332 .max = 864000000,
333 .count = 3,
334 .entries = {
335 { 157250000, 44000000, 62500, 0xc6, 0x41 },
336 { 454000000, 44000000, 62500, 0xc6, 0x42 },
337 { 999999999, 44000000, 62500, 0xc6, 0x44 },
338 },
339};
340EXPORT_SYMBOL(dvb_pll_tuv1236d);
341
Michael Krufkyd76a6172006-01-23 17:11:06 -0200342/* Samsung TBMV30111IN / TBMV30712IN1
Kirk Lapray147418c2005-11-08 21:35:39 -0800343 * used in Air2PC ATSC - 2nd generation (nxt2002)
344 */
Michael Krufky28f3d4b2006-01-23 17:11:07 -0200345struct dvb_pll_desc dvb_pll_samsung_tbmv = {
346 .name = "Samsung TBMV30111IN / TBMV30712IN1",
Kirk Lapray147418c2005-11-08 21:35:39 -0800347 .min = 54000000,
348 .max = 860000000,
Michael Krufky17c37ef2006-01-15 19:04:04 -0200349 .count = 6,
Kirk Lapray147418c2005-11-08 21:35:39 -0800350 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300351 { 172000000, 44000000, 166667, 0xb4, 0x01 },
352 { 214000000, 44000000, 166667, 0xb4, 0x02 },
353 { 467000000, 44000000, 166667, 0xbc, 0x02 },
354 { 721000000, 44000000, 166667, 0xbc, 0x08 },
355 { 841000000, 44000000, 166667, 0xf4, 0x08 },
356 { 999999999, 44000000, 166667, 0xfc, 0x02 },
Kirk Lapray147418c2005-11-08 21:35:39 -0800357 }
358};
Michael Krufky28f3d4b2006-01-23 17:11:07 -0200359EXPORT_SYMBOL(dvb_pll_samsung_tbmv);
Kirk Lapray147418c2005-11-08 21:35:39 -0800360
Regis Prevotf8bf1342006-01-11 23:31:53 -0200361/*
362 * Philips SD1878 Tuner.
363 */
364struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
365 .name = "Philips SD1878",
366 .min = 950000,
367 .max = 2150000,
368 .count = 4,
369 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300370 /* zero-IF, offset 249 is to round up */
371 { 1250000, 249, 500, 0xc4, 0x00},
372 { 1550000, 249, 500, 0xc4, 0x40},
373 { 2050000, 249, 500, 0xc4, 0x80},
374 { 2150000, 249, 500, 0xc4, 0xc0},
Regis Prevotf8bf1342006-01-11 23:31:53 -0200375 },
376};
377EXPORT_SYMBOL(dvb_pll_philips_sd1878_tda8261);
378
Jose Alberto Regueroa78d0bf2006-02-07 06:25:14 -0200379/*
380 * Philips TD1316 Tuner.
381 */
382static void td1316_bw(u8 *buf, u32 freq, int bandwidth)
383{
384 u8 band;
385
386 /* determine band */
387 if (freq < 161000000)
388 band = 1;
389 else if (freq < 444000000)
390 band = 2;
391 else
392 band = 4;
393
394 buf[3] |= band;
395
396 /* setup PLL filter */
397 if (bandwidth == BANDWIDTH_8_MHZ)
398 buf[3] |= 1 << 3;
399}
400
401struct dvb_pll_desc dvb_pll_philips_td1316 = {
402 .name = "Philips TD1316",
403 .min = 87000000,
404 .max = 895000000,
405 .setbw = td1316_bw,
406 .count = 9,
407 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300408 { 93834000, 36166667, 166667, 0xca, 0x60},
409 { 123834000, 36166667, 166667, 0xca, 0xa0},
410 { 163834000, 36166667, 166667, 0xca, 0xc0},
411 { 253834000, 36166667, 166667, 0xca, 0x60},
412 { 383834000, 36166667, 166667, 0xca, 0xa0},
413 { 443834000, 36166667, 166667, 0xca, 0xc0},
414 { 583834000, 36166667, 166667, 0xca, 0x60},
415 { 793834000, 36166667, 166667, 0xca, 0xa0},
416 { 858834000, 36166667, 166667, 0xca, 0xe0},
Jose Alberto Regueroa78d0bf2006-02-07 06:25:14 -0200417 },
418};
419EXPORT_SYMBOL(dvb_pll_philips_td1316);
420
Chris Pascoe780dfef2006-02-28 08:34:59 -0300421/* FE6600 used on DViCO Hybrid */
Michael Krufky91ae3292006-03-01 00:04:42 -0300422struct dvb_pll_desc dvb_pll_thomson_fe6600 = {
423 .name = "Thomson FE6600",
Chris Pascoe780dfef2006-02-28 08:34:59 -0300424 .min = 44250000,
425 .max = 858000000,
426 .count = 4,
427 .entries = {
Trent Piepho0fd17d62007-03-19 02:23:59 -0300428 { 250000000, 36125000, 166667, 0xb4, 0x12 },
429 { 455000000, 36125000, 166667, 0xfe, 0x11 },
430 { 775500000, 36125000, 166667, 0xbc, 0x18 },
431 { 999999999, 36125000, 166667, 0xf4, 0x18 },
Chris Pascoe780dfef2006-02-28 08:34:59 -0300432 }
433};
Michael Krufky91ae3292006-03-01 00:04:42 -0300434EXPORT_SYMBOL(dvb_pll_thomson_fe6600);
Chris Pascoe780dfef2006-02-28 08:34:59 -0300435
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300436struct dvb_pll_priv {
437 /* i2c details */
438 int pll_i2c_address;
439 struct i2c_adapter *i2c;
440
441 /* the PLL descriptor */
442 struct dvb_pll_desc *pll_desc;
443
444 /* cached frequency/bandwidth */
445 u32 frequency;
446 u32 bandwidth;
447};
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* ----------------------------------------------------------- */
450/* code */
451
452static int debug = 0;
453module_param(debug, int, 0644);
454MODULE_PARM_DESC(debug, "enable verbose debug messages");
455
456int dvb_pll_configure(struct dvb_pll_desc *desc, u8 *buf,
457 u32 freq, int bandwidth)
458{
459 u32 div;
460 int i;
461
462 if (freq != 0 && (freq < desc->min || freq > desc->max))
463 return -EINVAL;
464
465 for (i = 0; i < desc->count; i++) {
466 if (freq > desc->entries[i].limit)
467 continue;
468 break;
469 }
470 if (debug)
471 printk("pll: %s: freq=%d bw=%d | i=%d/%d\n",
472 desc->name, freq, bandwidth, i, desc->count);
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300473 if (i == desc->count)
474 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Trent Piepho0fd17d62007-03-19 02:23:59 -0300476 div = (freq + desc->entries[i].offset + desc->entries[i].stepsize/2) /
477 desc->entries[i].stepsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 buf[0] = div >> 8;
479 buf[1] = div & 0xff;
Michael Krufkyab66b222006-01-23 17:11:11 -0200480 buf[2] = desc->entries[i].config;
481 buf[3] = desc->entries[i].cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (desc->setbw)
Patrick Boettcher49dc82f2005-07-07 17:58:09 -0700484 desc->setbw(buf, freq, bandwidth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (debug)
487 printk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
488 desc->name, div, buf[0], buf[1], buf[2], buf[3]);
489
Michael Krufky89faeef2006-11-20 16:45:29 -0300490 // calculate the frequency we set it to
491 return (div * desc->entries[i].stepsize) - desc->entries[i].offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493EXPORT_SYMBOL(dvb_pll_configure);
494
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300495static int dvb_pll_release(struct dvb_frontend *fe)
496{
Michael Krufky22139182006-11-19 19:49:11 -0300497 kfree(fe->tuner_priv);
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300498 fe->tuner_priv = NULL;
499 return 0;
500}
501
502static int dvb_pll_sleep(struct dvb_frontend *fe)
503{
504 struct dvb_pll_priv *priv = fe->tuner_priv;
505 u8 buf[4];
506 struct i2c_msg msg =
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300507 { .addr = priv->pll_i2c_address, .flags = 0,
508 .buf = buf, .len = sizeof(buf) };
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300509 int i;
510 int result;
511
Chris Pascoec162dff2006-08-08 15:48:08 -0300512 if (priv->i2c == NULL)
513 return -EINVAL;
514
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300515 for (i = 0; i < priv->pll_desc->count; i++) {
516 if (priv->pll_desc->entries[i].limit == 0)
517 break;
518 }
519 if (i == priv->pll_desc->count)
520 return 0;
521
522 buf[0] = 0;
523 buf[1] = 0;
524 buf[2] = priv->pll_desc->entries[i].config;
525 buf[3] = priv->pll_desc->entries[i].cb;
526
Patrick Boettcherdea74862006-05-14 05:01:31 -0300527 if (fe->ops.i2c_gate_ctrl)
528 fe->ops.i2c_gate_ctrl(fe, 1);
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300529 if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
530 return result;
531 }
532
533 return 0;
534}
535
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300536static int dvb_pll_set_params(struct dvb_frontend *fe,
537 struct dvb_frontend_parameters *params)
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300538{
539 struct dvb_pll_priv *priv = fe->tuner_priv;
540 u8 buf[4];
541 struct i2c_msg msg =
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300542 { .addr = priv->pll_i2c_address, .flags = 0,
543 .buf = buf, .len = sizeof(buf) };
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300544 int result;
Michael Krufky89faeef2006-11-20 16:45:29 -0300545 u32 bandwidth = 0, frequency = 0;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300546
547 if (priv->i2c == NULL)
548 return -EINVAL;
549
550 // DVBT bandwidth only just now
Patrick Boettcherdea74862006-05-14 05:01:31 -0300551 if (fe->ops.info.type == FE_OFDM) {
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300552 bandwidth = params->u.ofdm.bandwidth;
553 }
554
Michael Krufky89faeef2006-11-20 16:45:29 -0300555 if ((result = dvb_pll_configure(priv->pll_desc, buf,
556 params->frequency, bandwidth)) < 0)
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300557 return result;
Michael Krufky89faeef2006-11-20 16:45:29 -0300558 else
559 frequency = result;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300560
Patrick Boettcherdea74862006-05-14 05:01:31 -0300561 if (fe->ops.i2c_gate_ctrl)
562 fe->ops.i2c_gate_ctrl(fe, 1);
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300563 if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
564 return result;
565 }
566
Michael Krufky89faeef2006-11-20 16:45:29 -0300567 priv->frequency = frequency;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300568 priv->bandwidth = bandwidth;
569
570 return 0;
571}
572
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300573static int dvb_pll_calc_regs(struct dvb_frontend *fe,
574 struct dvb_frontend_parameters *params,
575 u8 *buf, int buf_len)
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300576{
577 struct dvb_pll_priv *priv = fe->tuner_priv;
578 int result;
Michael Krufky89faeef2006-11-20 16:45:29 -0300579 u32 bandwidth = 0, frequency = 0;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300580
581 if (buf_len < 5)
582 return -EINVAL;
583
584 // DVBT bandwidth only just now
Patrick Boettcherdea74862006-05-14 05:01:31 -0300585 if (fe->ops.info.type == FE_OFDM) {
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300586 bandwidth = params->u.ofdm.bandwidth;
587 }
588
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300589 if ((result = dvb_pll_configure(priv->pll_desc, buf+1,
Michael Krufky89faeef2006-11-20 16:45:29 -0300590 params->frequency, bandwidth)) < 0)
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300591 return result;
Michael Krufky89faeef2006-11-20 16:45:29 -0300592 else
593 frequency = result;
594
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300595 buf[0] = priv->pll_i2c_address;
596
Michael Krufky89faeef2006-11-20 16:45:29 -0300597 priv->frequency = frequency;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300598 priv->bandwidth = bandwidth;
599
600 return 5;
601}
602
603static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
604{
605 struct dvb_pll_priv *priv = fe->tuner_priv;
606 *frequency = priv->frequency;
607 return 0;
608}
609
610static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
611{
612 struct dvb_pll_priv *priv = fe->tuner_priv;
613 *bandwidth = priv->bandwidth;
614 return 0;
615}
616
Trent Piepho26aed922007-04-27 12:31:29 -0300617static int dvb_pll_init(struct dvb_frontend *fe)
618{
619 struct dvb_pll_priv *priv = fe->tuner_priv;
620
621 if (priv->i2c == NULL)
622 return -EINVAL;
623
624 if (priv->pll_desc->initdata) {
625 struct i2c_msg msg = { .flags = 0,
626 .addr = priv->pll_i2c_address,
627 .buf = priv->pll_desc->initdata + 1,
628 .len = priv->pll_desc->initdata[0] };
629
630 int result;
631 if (fe->ops.i2c_gate_ctrl)
632 fe->ops.i2c_gate_ctrl(fe, 1);
633 if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
634 return result;
635 }
636 return 0;
637 }
638 /* Shouldn't be called when initdata is NULL, maybe BUG()? */
639 return -EINVAL;
640}
641
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300642static struct dvb_tuner_ops dvb_pll_tuner_ops = {
643 .release = dvb_pll_release,
644 .sleep = dvb_pll_sleep,
645 .set_params = dvb_pll_set_params,
Andrew de Quinceybd4956b2006-04-18 21:38:49 -0300646 .calc_regs = dvb_pll_calc_regs,
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300647 .get_frequency = dvb_pll_get_frequency,
648 .get_bandwidth = dvb_pll_get_bandwidth,
649};
650
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300651struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
652 struct i2c_adapter *i2c,
653 struct dvb_pll_desc *desc)
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300654{
Andrew de Quincey061b6232006-07-10 03:34:14 -0300655 u8 b1 [] = { 0 };
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300656 struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
657 .buf = b1, .len = 1 };
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300658 struct dvb_pll_priv *priv = NULL;
Andrew de Quincey061b6232006-07-10 03:34:14 -0300659 int ret;
660
Andrew de Quincey55c05b62006-07-16 19:41:41 -0300661 if (i2c != NULL) {
662 if (fe->ops.i2c_gate_ctrl)
663 fe->ops.i2c_gate_ctrl(fe, 1);
Andrew de Quincey061b6232006-07-10 03:34:14 -0300664
Andrew de Quincey95faba22006-07-18 16:37:13 -0300665 ret = i2c_transfer (i2c, &msg, 1);
666 if (ret != 1)
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300667 return NULL;
Andrew de Quincey55c05b62006-07-16 19:41:41 -0300668 if (fe->ops.i2c_gate_ctrl)
669 fe->ops.i2c_gate_ctrl(fe, 0);
670 }
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300671
672 priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
673 if (priv == NULL)
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300674 return NULL;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300675
676 priv->pll_i2c_address = pll_addr;
677 priv->i2c = i2c;
678 priv->pll_desc = desc;
679
Michael Krufky47ae9ae2006-11-20 16:38:42 -0300680 memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
681 sizeof(struct dvb_tuner_ops));
682
Trent Piepho982dd1b2007-04-27 12:31:27 -0300683 strncpy(fe->ops.tuner_ops.info.name, desc->name,
684 sizeof(fe->ops.tuner_ops.info.name));
Patrick Boettcherdea74862006-05-14 05:01:31 -0300685 fe->ops.tuner_ops.info.frequency_min = desc->min;
686 fe->ops.tuner_ops.info.frequency_min = desc->max;
Trent Piepho26aed922007-04-27 12:31:29 -0300687 if (desc->initdata)
688 fe->ops.tuner_ops.init = dvb_pll_init;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300689
690 fe->tuner_priv = priv;
Andrew de Quincey2bfe0312006-08-08 09:10:08 -0300691 return fe;
Andrew de Quincey272bc4d2006-04-18 17:47:12 -0300692}
693EXPORT_SYMBOL(dvb_pll_attach);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695MODULE_DESCRIPTION("dvb pll library");
696MODULE_AUTHOR("Gerd Knorr");
697MODULE_LICENSE("GPL");