dvb-pll.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * 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. #include <linux/module.h>
  21. #include <linux/dvb/frontend.h>
  22. #include <asm/types.h>
  23. #include "dvb-pll.h"
  24. struct dvb_pll_priv {
  25. /* pll number */
  26. int nr;
  27. /* i2c details */
  28. int pll_i2c_address;
  29. struct i2c_adapter *i2c;
  30. /* the PLL descriptor */
  31. struct dvb_pll_desc *pll_desc;
  32. /* cached frequency/bandwidth */
  33. u32 frequency;
  34. u32 bandwidth;
  35. };
  36. #define DVB_PLL_MAX 64
  37. static unsigned int dvb_pll_devcount;
  38. static int debug;
  39. module_param(debug, int, 0644);
  40. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  41. static unsigned int input[DVB_PLL_MAX] = { [ 0 ... (DVB_PLL_MAX-1) ] = 0 };
  42. module_param_array(input, int, NULL, 0644);
  43. MODULE_PARM_DESC(input,"specify rf input choice, 0 for autoselect (default)");
  44. static unsigned int id[DVB_PLL_MAX] =
  45. { [ 0 ... (DVB_PLL_MAX-1) ] = DVB_PLL_UNDEFINED };
  46. module_param_array(id, int, NULL, 0644);
  47. MODULE_PARM_DESC(id, "force pll id to use (DEBUG ONLY)");
  48. /* ----------------------------------------------------------- */
  49. struct dvb_pll_desc {
  50. char *name;
  51. u32 min;
  52. u32 max;
  53. u32 iffreq;
  54. void (*set)(struct dvb_frontend *fe, u8 *buf,
  55. const struct dvb_frontend_parameters *params);
  56. u8 *initdata;
  57. u8 *sleepdata;
  58. int count;
  59. struct {
  60. u32 limit;
  61. u32 stepsize;
  62. u8 config;
  63. u8 cb;
  64. } entries[12];
  65. };
  66. /* ----------------------------------------------------------- */
  67. /* descriptions */
  68. /* Set AGC TOP value to 103 dBuV:
  69. 0x80 = Control Byte
  70. 0x40 = 250 uA charge pump (irrelevant)
  71. 0x18 = Aux Byte to follow
  72. 0x06 = 64.5 kHz divider (irrelevant)
  73. 0x01 = Disable Vt (aka sleep)
  74. 0x00 = AGC Time constant 2s Iagc = 300 nA (vs 0x80 = 9 nA)
  75. 0x50 = AGC Take over point = 103 dBuV */
  76. static u8 tua603x_agc103[] = { 2, 0x80|0x40|0x18|0x06|0x01, 0x00|0x50 };
  77. /* 0x04 = 166.67 kHz divider
  78. 0x80 = AGC Time constant 50ms Iagc = 9 uA
  79. 0x20 = AGC Take over point = 112 dBuV */
  80. static u8 tua603x_agc112[] = { 2, 0x80|0x40|0x18|0x04|0x01, 0x80|0x20 };
  81. static struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
  82. .name = "Thomson dtt7579",
  83. .min = 177000000,
  84. .max = 858000000,
  85. .iffreq= 36166667,
  86. .sleepdata = (u8[]){ 2, 0xb4, 0x03 },
  87. .count = 4,
  88. .entries = {
  89. { 443250000, 166667, 0xb4, 0x02 },
  90. { 542000000, 166667, 0xb4, 0x08 },
  91. { 771000000, 166667, 0xbc, 0x08 },
  92. { 999999999, 166667, 0xf4, 0x08 },
  93. },
  94. };
  95. static void thomson_dtt759x_bw(struct dvb_frontend *fe, u8 *buf,
  96. const struct dvb_frontend_parameters *params)
  97. {
  98. if (BANDWIDTH_7_MHZ == params->u.ofdm.bandwidth)
  99. buf[3] |= 0x10;
  100. }
  101. static struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
  102. .name = "Thomson dtt759x",
  103. .min = 177000000,
  104. .max = 896000000,
  105. .set = thomson_dtt759x_bw,
  106. .iffreq= 36166667,
  107. .sleepdata = (u8[]){ 2, 0x84, 0x03 },
  108. .count = 5,
  109. .entries = {
  110. { 264000000, 166667, 0xb4, 0x02 },
  111. { 470000000, 166667, 0xbc, 0x02 },
  112. { 735000000, 166667, 0xbc, 0x08 },
  113. { 835000000, 166667, 0xf4, 0x08 },
  114. { 999999999, 166667, 0xfc, 0x08 },
  115. },
  116. };
  117. static struct dvb_pll_desc dvb_pll_lg_z201 = {
  118. .name = "LG z201",
  119. .min = 174000000,
  120. .max = 862000000,
  121. .iffreq= 36166667,
  122. .sleepdata = (u8[]){ 2, 0xbc, 0x03 },
  123. .count = 5,
  124. .entries = {
  125. { 157500000, 166667, 0xbc, 0x01 },
  126. { 443250000, 166667, 0xbc, 0x02 },
  127. { 542000000, 166667, 0xbc, 0x04 },
  128. { 830000000, 166667, 0xf4, 0x04 },
  129. { 999999999, 166667, 0xfc, 0x04 },
  130. },
  131. };
  132. static struct dvb_pll_desc dvb_pll_unknown_1 = {
  133. .name = "unknown 1", /* used by dntv live dvb-t */
  134. .min = 174000000,
  135. .max = 862000000,
  136. .iffreq= 36166667,
  137. .count = 9,
  138. .entries = {
  139. { 150000000, 166667, 0xb4, 0x01 },
  140. { 173000000, 166667, 0xbc, 0x01 },
  141. { 250000000, 166667, 0xb4, 0x02 },
  142. { 400000000, 166667, 0xbc, 0x02 },
  143. { 420000000, 166667, 0xf4, 0x02 },
  144. { 470000000, 166667, 0xfc, 0x02 },
  145. { 600000000, 166667, 0xbc, 0x08 },
  146. { 730000000, 166667, 0xf4, 0x08 },
  147. { 999999999, 166667, 0xfc, 0x08 },
  148. },
  149. };
  150. /* Infineon TUA6010XS
  151. * used in Thomson Cable Tuner
  152. */
  153. static struct dvb_pll_desc dvb_pll_tua6010xs = {
  154. .name = "Infineon TUA6010XS",
  155. .min = 44250000,
  156. .max = 858000000,
  157. .iffreq= 36125000,
  158. .count = 3,
  159. .entries = {
  160. { 115750000, 62500, 0x8e, 0x03 },
  161. { 403250000, 62500, 0x8e, 0x06 },
  162. { 999999999, 62500, 0x8e, 0x85 },
  163. },
  164. };
  165. /* Panasonic env57h1xd5 (some Philips PLL ?) */
  166. static struct dvb_pll_desc dvb_pll_env57h1xd5 = {
  167. .name = "Panasonic ENV57H1XD5",
  168. .min = 44250000,
  169. .max = 858000000,
  170. .iffreq= 36125000,
  171. .count = 4,
  172. .entries = {
  173. { 153000000, 166667, 0xc2, 0x41 },
  174. { 470000000, 166667, 0xc2, 0x42 },
  175. { 526000000, 166667, 0xc2, 0x84 },
  176. { 999999999, 166667, 0xc2, 0xa4 },
  177. },
  178. };
  179. /* Philips TDA6650/TDA6651
  180. * used in Panasonic ENV77H11D5
  181. */
  182. static void tda665x_bw(struct dvb_frontend *fe, u8 *buf,
  183. const struct dvb_frontend_parameters *params)
  184. {
  185. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  186. buf[3] |= 0x08;
  187. }
  188. static struct dvb_pll_desc dvb_pll_tda665x = {
  189. .name = "Philips TDA6650/TDA6651",
  190. .min = 44250000,
  191. .max = 858000000,
  192. .set = tda665x_bw,
  193. .iffreq= 36166667,
  194. .initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
  195. .count = 12,
  196. .entries = {
  197. { 93834000, 166667, 0xca, 0x61 /* 011 0 0 0 01 */ },
  198. { 123834000, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
  199. { 161000000, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
  200. { 163834000, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
  201. { 253834000, 166667, 0xca, 0x62 /* 011 0 0 0 10 */ },
  202. { 383834000, 166667, 0xca, 0xa2 /* 101 0 0 0 10 */ },
  203. { 443834000, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
  204. { 444000000, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
  205. { 583834000, 166667, 0xca, 0x64 /* 011 0 0 1 00 */ },
  206. { 793834000, 166667, 0xca, 0xa4 /* 101 0 0 1 00 */ },
  207. { 444834000, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
  208. { 861000000, 166667, 0xca, 0xe4 /* 111 0 0 1 00 */ },
  209. }
  210. };
  211. /* Infineon TUA6034
  212. * used in LG TDTP E102P
  213. */
  214. static void tua6034_bw(struct dvb_frontend *fe, u8 *buf,
  215. const struct dvb_frontend_parameters *params)
  216. {
  217. if (BANDWIDTH_7_MHZ != params->u.ofdm.bandwidth)
  218. buf[3] |= 0x08;
  219. }
  220. static struct dvb_pll_desc dvb_pll_tua6034 = {
  221. .name = "Infineon TUA6034",
  222. .min = 44250000,
  223. .max = 858000000,
  224. .iffreq= 36166667,
  225. .count = 3,
  226. .set = tua6034_bw,
  227. .entries = {
  228. { 174500000, 62500, 0xce, 0x01 },
  229. { 230000000, 62500, 0xce, 0x02 },
  230. { 999999999, 62500, 0xce, 0x04 },
  231. },
  232. };
  233. /* Infineon TUA6034
  234. * used in LG TDVS-H061F, LG TDVS-H062F and LG TDVS-H064F
  235. */
  236. static struct dvb_pll_desc dvb_pll_lg_tdvs_h06xf = {
  237. .name = "LG TDVS-H06xF",
  238. .min = 54000000,
  239. .max = 863000000,
  240. .iffreq= 44000000,
  241. .initdata = tua603x_agc103,
  242. .count = 3,
  243. .entries = {
  244. { 165000000, 62500, 0xce, 0x01 },
  245. { 450000000, 62500, 0xce, 0x02 },
  246. { 999999999, 62500, 0xce, 0x04 },
  247. },
  248. };
  249. /* Philips FMD1216ME
  250. * used in Medion Hybrid PCMCIA card and USB Box
  251. */
  252. static void fmd1216me_bw(struct dvb_frontend *fe, u8 *buf,
  253. const struct dvb_frontend_parameters *params)
  254. {
  255. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
  256. params->frequency >= 158870000)
  257. buf[3] |= 0x08;
  258. }
  259. static struct dvb_pll_desc dvb_pll_fmd1216me = {
  260. .name = "Philips FMD1216ME",
  261. .min = 50870000,
  262. .max = 858000000,
  263. .iffreq= 36125000,
  264. .set = fmd1216me_bw,
  265. .initdata = tua603x_agc112,
  266. .sleepdata = (u8[]){ 4, 0x9c, 0x60, 0x85, 0x54 },
  267. .count = 7,
  268. .entries = {
  269. { 143870000, 166667, 0xbc, 0x41 },
  270. { 158870000, 166667, 0xf4, 0x41 },
  271. { 329870000, 166667, 0xbc, 0x42 },
  272. { 441870000, 166667, 0xf4, 0x42 },
  273. { 625870000, 166667, 0xbc, 0x44 },
  274. { 803870000, 166667, 0xf4, 0x44 },
  275. { 999999999, 166667, 0xfc, 0x44 },
  276. }
  277. };
  278. /* ALPS TDED4
  279. * used in Nebula-Cards and USB boxes
  280. */
  281. static void tded4_bw(struct dvb_frontend *fe, u8 *buf,
  282. const struct dvb_frontend_parameters *params)
  283. {
  284. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  285. buf[3] |= 0x04;
  286. }
  287. static struct dvb_pll_desc dvb_pll_tded4 = {
  288. .name = "ALPS TDED4",
  289. .min = 47000000,
  290. .max = 863000000,
  291. .iffreq= 36166667,
  292. .set = tded4_bw,
  293. .count = 4,
  294. .entries = {
  295. { 153000000, 166667, 0x85, 0x01 },
  296. { 470000000, 166667, 0x85, 0x02 },
  297. { 823000000, 166667, 0x85, 0x08 },
  298. { 999999999, 166667, 0x85, 0x88 },
  299. }
  300. };
  301. /* ALPS TDHU2
  302. * used in AverTVHD MCE A180
  303. */
  304. static struct dvb_pll_desc dvb_pll_tdhu2 = {
  305. .name = "ALPS TDHU2",
  306. .min = 54000000,
  307. .max = 864000000,
  308. .iffreq= 44000000,
  309. .count = 4,
  310. .entries = {
  311. { 162000000, 62500, 0x85, 0x01 },
  312. { 426000000, 62500, 0x85, 0x02 },
  313. { 782000000, 62500, 0x85, 0x08 },
  314. { 999999999, 62500, 0x85, 0x88 },
  315. }
  316. };
  317. /* Philips TUV1236D
  318. * used in ATI HDTV Wonder
  319. */
  320. static void tuv1236d_rf(struct dvb_frontend *fe, u8 *buf,
  321. const struct dvb_frontend_parameters *params)
  322. {
  323. struct dvb_pll_priv *priv = fe->tuner_priv;
  324. unsigned int new_rf = input[priv->nr];
  325. if ((new_rf == 0) || (new_rf > 2)) {
  326. switch (params->u.vsb.modulation) {
  327. case QAM_64:
  328. case QAM_256:
  329. new_rf = 1;
  330. break;
  331. case VSB_8:
  332. default:
  333. new_rf = 2;
  334. }
  335. }
  336. switch (new_rf) {
  337. case 1:
  338. buf[3] |= 0x08;
  339. break;
  340. case 2:
  341. buf[3] &= ~0x08;
  342. break;
  343. default:
  344. printk(KERN_WARNING
  345. "%s: unhandled rf input selection: %d",
  346. __FUNCTION__, new_rf);
  347. }
  348. }
  349. static struct dvb_pll_desc dvb_pll_tuv1236d = {
  350. .name = "Philips TUV1236D",
  351. .min = 54000000,
  352. .max = 864000000,
  353. .iffreq= 44000000,
  354. .set = tuv1236d_rf,
  355. .count = 3,
  356. .entries = {
  357. { 157250000, 62500, 0xc6, 0x41 },
  358. { 454000000, 62500, 0xc6, 0x42 },
  359. { 999999999, 62500, 0xc6, 0x44 },
  360. },
  361. };
  362. /* Samsung TBMV30111IN / TBMV30712IN1
  363. * used in Air2PC ATSC - 2nd generation (nxt2002)
  364. */
  365. static struct dvb_pll_desc dvb_pll_samsung_tbmv = {
  366. .name = "Samsung TBMV30111IN / TBMV30712IN1",
  367. .min = 54000000,
  368. .max = 860000000,
  369. .iffreq= 44000000,
  370. .count = 6,
  371. .entries = {
  372. { 172000000, 166667, 0xb4, 0x01 },
  373. { 214000000, 166667, 0xb4, 0x02 },
  374. { 467000000, 166667, 0xbc, 0x02 },
  375. { 721000000, 166667, 0xbc, 0x08 },
  376. { 841000000, 166667, 0xf4, 0x08 },
  377. { 999999999, 166667, 0xfc, 0x02 },
  378. }
  379. };
  380. /*
  381. * Philips SD1878 Tuner.
  382. */
  383. static struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
  384. .name = "Philips SD1878",
  385. .min = 950000,
  386. .max = 2150000,
  387. .iffreq= 249, /* zero-IF, offset 249 is to round up */
  388. .count = 4,
  389. .entries = {
  390. { 1250000, 500, 0xc4, 0x00},
  391. { 1550000, 500, 0xc4, 0x40},
  392. { 2050000, 500, 0xc4, 0x80},
  393. { 2150000, 500, 0xc4, 0xc0},
  394. },
  395. };
  396. /*
  397. * Philips TD1316 Tuner.
  398. */
  399. static void td1316_bw(struct dvb_frontend *fe, u8 *buf,
  400. const struct dvb_frontend_parameters *params)
  401. {
  402. u8 band;
  403. /* determine band */
  404. if (params->frequency < 161000000)
  405. band = 1;
  406. else if (params->frequency < 444000000)
  407. band = 2;
  408. else
  409. band = 4;
  410. buf[3] |= band;
  411. /* setup PLL filter */
  412. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  413. buf[3] |= 1 << 3;
  414. }
  415. static struct dvb_pll_desc dvb_pll_philips_td1316 = {
  416. .name = "Philips TD1316",
  417. .min = 87000000,
  418. .max = 895000000,
  419. .iffreq= 36166667,
  420. .set = td1316_bw,
  421. .count = 9,
  422. .entries = {
  423. { 93834000, 166667, 0xca, 0x60},
  424. { 123834000, 166667, 0xca, 0xa0},
  425. { 163834000, 166667, 0xca, 0xc0},
  426. { 253834000, 166667, 0xca, 0x60},
  427. { 383834000, 166667, 0xca, 0xa0},
  428. { 443834000, 166667, 0xca, 0xc0},
  429. { 583834000, 166667, 0xca, 0x60},
  430. { 793834000, 166667, 0xca, 0xa0},
  431. { 858834000, 166667, 0xca, 0xe0},
  432. },
  433. };
  434. static void opera1_bw(struct dvb_frontend *fe, u8 *buf,
  435. const struct dvb_frontend_parameters *params)
  436. {
  437. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  438. buf[2] |= 0x08;
  439. }
  440. static struct dvb_pll_desc dvb_pll_opera1 = {
  441. .name = "Opera Tuner",
  442. .min = 900000,
  443. .max = 2250000,
  444. .iffreq= 0,
  445. .set = opera1_bw,
  446. .count = 8,
  447. .entries = {
  448. { 1064000, 500, 0xe5, 0xc6 },
  449. { 1169000, 500, 0xe5, 0xe6 },
  450. { 1299000, 500, 0xe5, 0x24 },
  451. { 1444000, 500, 0xe5, 0x44 },
  452. { 1606000, 500, 0xe5, 0x64 },
  453. { 1777000, 500, 0xe5, 0x84 },
  454. { 1941000, 500, 0xe5, 0xa4 },
  455. { 2250000, 500, 0xe5, 0xc4 },
  456. }
  457. };
  458. /* ----------------------------------------------------------- */
  459. static struct dvb_pll_desc *pll_list[] = {
  460. [DVB_PLL_UNDEFINED] = NULL,
  461. [DVB_PLL_THOMSON_DTT7579] = &dvb_pll_thomson_dtt7579,
  462. [DVB_PLL_THOMSON_DTT759X] = &dvb_pll_thomson_dtt759x,
  463. [DVB_PLL_LG_Z201] = &dvb_pll_lg_z201,
  464. [DVB_PLL_UNKNOWN_1] = &dvb_pll_unknown_1,
  465. [DVB_PLL_TUA6010XS] = &dvb_pll_tua6010xs,
  466. [DVB_PLL_ENV57H1XD5] = &dvb_pll_env57h1xd5,
  467. [DVB_PLL_TUA6034] = &dvb_pll_tua6034,
  468. [DVB_PLL_LG_TDVS_H06XF] = &dvb_pll_lg_tdvs_h06xf,
  469. [DVB_PLL_TDA665X] = &dvb_pll_tda665x,
  470. [DVB_PLL_FMD1216ME] = &dvb_pll_fmd1216me,
  471. [DVB_PLL_TDED4] = &dvb_pll_tded4,
  472. [DVB_PLL_TUV1236D] = &dvb_pll_tuv1236d,
  473. [DVB_PLL_TDHU2] = &dvb_pll_tdhu2,
  474. [DVB_PLL_SAMSUNG_TBMV] = &dvb_pll_samsung_tbmv,
  475. [DVB_PLL_PHILIPS_SD1878_TDA8261] = &dvb_pll_philips_sd1878_tda8261,
  476. [DVB_PLL_PHILIPS_TD1316] = &dvb_pll_philips_td1316,
  477. [DVB_PLL_OPERA1] = &dvb_pll_opera1,
  478. };
  479. /* ----------------------------------------------------------- */
  480. /* code */
  481. static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
  482. const struct dvb_frontend_parameters *params)
  483. {
  484. struct dvb_pll_priv *priv = fe->tuner_priv;
  485. struct dvb_pll_desc *desc = priv->pll_desc;
  486. u32 div;
  487. int i;
  488. if (params->frequency != 0 && (params->frequency < desc->min ||
  489. params->frequency > desc->max))
  490. return -EINVAL;
  491. for (i = 0; i < desc->count; i++) {
  492. if (params->frequency > desc->entries[i].limit)
  493. continue;
  494. break;
  495. }
  496. if (debug)
  497. printk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
  498. params->frequency, i, desc->count);
  499. if (i == desc->count)
  500. return -EINVAL;
  501. div = (params->frequency + desc->iffreq +
  502. desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
  503. buf[0] = div >> 8;
  504. buf[1] = div & 0xff;
  505. buf[2] = desc->entries[i].config;
  506. buf[3] = desc->entries[i].cb;
  507. if (desc->set)
  508. desc->set(fe, buf, params);
  509. if (debug)
  510. printk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  511. desc->name, div, buf[0], buf[1], buf[2], buf[3]);
  512. // calculate the frequency we set it to
  513. return (div * desc->entries[i].stepsize) - desc->iffreq;
  514. }
  515. static int dvb_pll_release(struct dvb_frontend *fe)
  516. {
  517. kfree(fe->tuner_priv);
  518. fe->tuner_priv = NULL;
  519. return 0;
  520. }
  521. static int dvb_pll_sleep(struct dvb_frontend *fe)
  522. {
  523. struct dvb_pll_priv *priv = fe->tuner_priv;
  524. if (priv->i2c == NULL)
  525. return -EINVAL;
  526. if (priv->pll_desc->sleepdata) {
  527. struct i2c_msg msg = { .flags = 0,
  528. .addr = priv->pll_i2c_address,
  529. .buf = priv->pll_desc->sleepdata + 1,
  530. .len = priv->pll_desc->sleepdata[0] };
  531. int result;
  532. if (fe->ops.i2c_gate_ctrl)
  533. fe->ops.i2c_gate_ctrl(fe, 1);
  534. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  535. return result;
  536. }
  537. return 0;
  538. }
  539. /* Shouldn't be called when initdata is NULL, maybe BUG()? */
  540. return -EINVAL;
  541. }
  542. static int dvb_pll_set_params(struct dvb_frontend *fe,
  543. struct dvb_frontend_parameters *params)
  544. {
  545. struct dvb_pll_priv *priv = fe->tuner_priv;
  546. u8 buf[4];
  547. struct i2c_msg msg =
  548. { .addr = priv->pll_i2c_address, .flags = 0,
  549. .buf = buf, .len = sizeof(buf) };
  550. int result;
  551. u32 frequency = 0;
  552. if (priv->i2c == NULL)
  553. return -EINVAL;
  554. if ((result = dvb_pll_configure(fe, buf, params)) < 0)
  555. return result;
  556. else
  557. frequency = result;
  558. if (fe->ops.i2c_gate_ctrl)
  559. fe->ops.i2c_gate_ctrl(fe, 1);
  560. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  561. return result;
  562. }
  563. priv->frequency = frequency;
  564. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
  565. return 0;
  566. }
  567. static int dvb_pll_calc_regs(struct dvb_frontend *fe,
  568. struct dvb_frontend_parameters *params,
  569. u8 *buf, int buf_len)
  570. {
  571. struct dvb_pll_priv *priv = fe->tuner_priv;
  572. int result;
  573. u32 frequency = 0;
  574. if (buf_len < 5)
  575. return -EINVAL;
  576. if ((result = dvb_pll_configure(fe, buf+1, params)) < 0)
  577. return result;
  578. else
  579. frequency = result;
  580. buf[0] = priv->pll_i2c_address;
  581. priv->frequency = frequency;
  582. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
  583. return 5;
  584. }
  585. static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  586. {
  587. struct dvb_pll_priv *priv = fe->tuner_priv;
  588. *frequency = priv->frequency;
  589. return 0;
  590. }
  591. static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  592. {
  593. struct dvb_pll_priv *priv = fe->tuner_priv;
  594. *bandwidth = priv->bandwidth;
  595. return 0;
  596. }
  597. static int dvb_pll_init(struct dvb_frontend *fe)
  598. {
  599. struct dvb_pll_priv *priv = fe->tuner_priv;
  600. if (priv->i2c == NULL)
  601. return -EINVAL;
  602. if (priv->pll_desc->initdata) {
  603. struct i2c_msg msg = { .flags = 0,
  604. .addr = priv->pll_i2c_address,
  605. .buf = priv->pll_desc->initdata + 1,
  606. .len = priv->pll_desc->initdata[0] };
  607. int result;
  608. if (fe->ops.i2c_gate_ctrl)
  609. fe->ops.i2c_gate_ctrl(fe, 1);
  610. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  611. return result;
  612. }
  613. return 0;
  614. }
  615. /* Shouldn't be called when initdata is NULL, maybe BUG()? */
  616. return -EINVAL;
  617. }
  618. static struct dvb_tuner_ops dvb_pll_tuner_ops = {
  619. .release = dvb_pll_release,
  620. .sleep = dvb_pll_sleep,
  621. .init = dvb_pll_init,
  622. .set_params = dvb_pll_set_params,
  623. .calc_regs = dvb_pll_calc_regs,
  624. .get_frequency = dvb_pll_get_frequency,
  625. .get_bandwidth = dvb_pll_get_bandwidth,
  626. };
  627. struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
  628. struct i2c_adapter *i2c,
  629. unsigned int pll_desc_id)
  630. {
  631. u8 b1 [] = { 0 };
  632. struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
  633. .buf = b1, .len = 1 };
  634. struct dvb_pll_priv *priv = NULL;
  635. int ret;
  636. struct dvb_pll_desc *desc;
  637. if ((id[dvb_pll_devcount] > DVB_PLL_UNDEFINED) &&
  638. (id[dvb_pll_devcount] < ARRAY_SIZE(pll_list)))
  639. pll_desc_id = id[dvb_pll_devcount];
  640. BUG_ON(pll_desc_id < 1 || pll_desc_id >= ARRAY_SIZE(pll_list));
  641. desc = pll_list[pll_desc_id];
  642. if (i2c != NULL) {
  643. if (fe->ops.i2c_gate_ctrl)
  644. fe->ops.i2c_gate_ctrl(fe, 1);
  645. ret = i2c_transfer (i2c, &msg, 1);
  646. if (ret != 1)
  647. return NULL;
  648. if (fe->ops.i2c_gate_ctrl)
  649. fe->ops.i2c_gate_ctrl(fe, 0);
  650. }
  651. priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
  652. if (priv == NULL)
  653. return NULL;
  654. priv->pll_i2c_address = pll_addr;
  655. priv->i2c = i2c;
  656. priv->pll_desc = desc;
  657. priv->nr = dvb_pll_devcount++;
  658. memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
  659. sizeof(struct dvb_tuner_ops));
  660. strncpy(fe->ops.tuner_ops.info.name, desc->name,
  661. sizeof(fe->ops.tuner_ops.info.name));
  662. fe->ops.tuner_ops.info.frequency_min = desc->min;
  663. fe->ops.tuner_ops.info.frequency_max = desc->max;
  664. if (!desc->initdata)
  665. fe->ops.tuner_ops.init = NULL;
  666. if (!desc->sleepdata)
  667. fe->ops.tuner_ops.sleep = NULL;
  668. fe->tuner_priv = priv;
  669. if ((debug) || (id[priv->nr] == pll_desc_id)) {
  670. printk("dvb-pll[%d]", priv->nr);
  671. if (i2c != NULL)
  672. printk(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
  673. printk(": id# %d (%s) attached, %s\n", pll_desc_id, desc->name,
  674. id[priv->nr] == pll_desc_id ?
  675. "insmod option" : "autodetected");
  676. }
  677. if ((debug) || (input[priv->nr] > 0)) {
  678. printk("dvb-pll[%d]", priv->nr);
  679. if (i2c != NULL)
  680. printk(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
  681. printk(": tuner rf input will be ");
  682. switch (input[priv->nr]) {
  683. case 0:
  684. printk("autoselected\n");
  685. break;
  686. default:
  687. printk("set to input %d (insmod option)\n",
  688. input[priv->nr]);
  689. }
  690. }
  691. return fe;
  692. }
  693. EXPORT_SYMBOL(dvb_pll_attach);
  694. MODULE_DESCRIPTION("dvb pll library");
  695. MODULE_AUTHOR("Gerd Knorr");
  696. MODULE_LICENSE("GPL");