dvb-pll.c 18 KB

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