dvb-pll.c 18 KB

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