dvb-pll.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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, 166666, 0xb4, 0x03 }, /* go sleep */
  42. { 443250000, 36166667, 166666, 0xb4, 0x02 },
  43. { 542000000, 36166667, 166666, 0xb4, 0x08 },
  44. { 771000000, 36166667, 166666, 0xbc, 0x08 },
  45. { 999999999, 36166667, 166666, 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, 166666, 0x84, 0x03 },
  74. { 264000000, 36166667, 166666, 0xb4, 0x02 },
  75. { 470000000, 36166667, 166666, 0xbc, 0x02 },
  76. { 735000000, 36166667, 166666, 0xbc, 0x08 },
  77. { 835000000, 36166667, 166666, 0xf4, 0x08 },
  78. { 999999999, 36166667, 166666, 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, 166666, 0xbc, 0x03 },
  89. { 157500000, 36166667, 166666, 0xbc, 0x01 },
  90. { 443250000, 36166667, 166666, 0xbc, 0x02 },
  91. { 542000000, 36166667, 166666, 0xbc, 0x04 },
  92. { 830000000, 36166667, 166666, 0xf4, 0x04 },
  93. { 999999999, 36166667, 166666, 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, 166666, 0xb4, 0x01 },
  130. { 173000000, 36166667, 166666, 0xbc, 0x01 },
  131. { 250000000, 36166667, 166666, 0xb4, 0x02 },
  132. { 400000000, 36166667, 166666, 0xbc, 0x02 },
  133. { 420000000, 36166667, 166666, 0xf4, 0x02 },
  134. { 470000000, 36166667, 166666, 0xfc, 0x02 },
  135. { 600000000, 36166667, 166666, 0xbc, 0x08 },
  136. { 730000000, 36166667, 166666, 0xf4, 0x08 },
  137. { 999999999, 36166667, 166666, 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, 36291666, 166666, 0xc2, 0x41 },
  164. { 470000000, 36291666, 166666, 0xc2, 0x42 },
  165. { 526000000, 36291666, 166666, 0xc2, 0x84 },
  166. { 999999999, 36291666, 166666, 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, 36249333, 166667, 0xca, 0x61 /* 011 0 0 0 01 */ },
  186. { 123834000, 36249333, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
  187. { 161000000, 36249333, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
  188. { 163834000, 36249333, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
  189. { 253834000, 36249333, 166667, 0xca, 0x62 /* 011 0 0 0 10 */ },
  190. { 383834000, 36249333, 166667, 0xca, 0xa2 /* 101 0 0 0 10 */ },
  191. { 443834000, 36249333, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
  192. { 444000000, 36249333, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
  193. { 583834000, 36249333, 166667, 0xca, 0x64 /* 011 0 0 1 00 */ },
  194. { 793834000, 36249333, 166667, 0xca, 0xa4 /* 101 0 0 1 00 */ },
  195. { 444834000, 36249333, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
  196. { 861000000, 36249333, 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. .count = 3,
  229. .entries = {
  230. { 165000000, 44000000, 62500, 0xce, 0x01 },
  231. { 450000000, 44000000, 62500, 0xce, 0x02 },
  232. { 999999999, 44000000, 62500, 0xce, 0x04 },
  233. },
  234. };
  235. EXPORT_SYMBOL(dvb_pll_lg_tdvs_h06xf);
  236. /* Philips FMD1216ME
  237. * used in Medion Hybrid PCMCIA card and USB Box
  238. */
  239. static void fmd1216me_bw(u8 *buf, u32 freq, int bandwidth)
  240. {
  241. if (bandwidth == BANDWIDTH_8_MHZ && freq >= 158870000)
  242. buf[3] |= 0x08;
  243. }
  244. struct dvb_pll_desc dvb_pll_fmd1216me = {
  245. .name = "Philips FMD1216ME",
  246. .min = 50870000,
  247. .max = 858000000,
  248. .setbw = fmd1216me_bw,
  249. .count = 7,
  250. .entries = {
  251. { 143870000, 36213333, 166667, 0xbc, 0x41 },
  252. { 158870000, 36213333, 166667, 0xf4, 0x41 },
  253. { 329870000, 36213333, 166667, 0xbc, 0x42 },
  254. { 441870000, 36213333, 166667, 0xf4, 0x42 },
  255. { 625870000, 36213333, 166667, 0xbc, 0x44 },
  256. { 803870000, 36213333, 166667, 0xf4, 0x44 },
  257. { 999999999, 36213333, 166667, 0xfc, 0x44 },
  258. }
  259. };
  260. EXPORT_SYMBOL(dvb_pll_fmd1216me);
  261. /* ALPS TDED4
  262. * used in Nebula-Cards and USB boxes
  263. */
  264. static void tded4_bw(u8 *buf, u32 freq, int bandwidth)
  265. {
  266. if (bandwidth == BANDWIDTH_8_MHZ)
  267. buf[3] |= 0x04;
  268. }
  269. struct dvb_pll_desc dvb_pll_tded4 = {
  270. .name = "ALPS TDED4",
  271. .min = 47000000,
  272. .max = 863000000,
  273. .setbw = tded4_bw,
  274. .count = 4,
  275. .entries = {
  276. { 153000000, 36166667, 166667, 0x85, 0x01 },
  277. { 470000000, 36166667, 166667, 0x85, 0x02 },
  278. { 823000000, 36166667, 166667, 0x85, 0x08 },
  279. { 999999999, 36166667, 166667, 0x85, 0x88 },
  280. }
  281. };
  282. EXPORT_SYMBOL(dvb_pll_tded4);
  283. /* ALPS TDHU2
  284. * used in AverTVHD MCE A180
  285. */
  286. struct dvb_pll_desc dvb_pll_tdhu2 = {
  287. .name = "ALPS TDHU2",
  288. .min = 54000000,
  289. .max = 864000000,
  290. .count = 4,
  291. .entries = {
  292. { 162000000, 44000000, 62500, 0x85, 0x01 },
  293. { 426000000, 44000000, 62500, 0x85, 0x02 },
  294. { 782000000, 44000000, 62500, 0x85, 0x08 },
  295. { 999999999, 44000000, 62500, 0x85, 0x88 },
  296. }
  297. };
  298. EXPORT_SYMBOL(dvb_pll_tdhu2);
  299. /* Philips TUV1236D
  300. * used in ATI HDTV Wonder
  301. */
  302. struct dvb_pll_desc dvb_pll_tuv1236d = {
  303. .name = "Philips TUV1236D",
  304. .min = 54000000,
  305. .max = 864000000,
  306. .count = 3,
  307. .entries = {
  308. { 157250000, 44000000, 62500, 0xc6, 0x41 },
  309. { 454000000, 44000000, 62500, 0xc6, 0x42 },
  310. { 999999999, 44000000, 62500, 0xc6, 0x44 },
  311. },
  312. };
  313. EXPORT_SYMBOL(dvb_pll_tuv1236d);
  314. /* Samsung TBMV30111IN / TBMV30712IN1
  315. * used in Air2PC ATSC - 2nd generation (nxt2002)
  316. */
  317. struct dvb_pll_desc dvb_pll_samsung_tbmv = {
  318. .name = "Samsung TBMV30111IN / TBMV30712IN1",
  319. .min = 54000000,
  320. .max = 860000000,
  321. .count = 6,
  322. .entries = {
  323. { 172000000, 44000000, 166666, 0xb4, 0x01 },
  324. { 214000000, 44000000, 166666, 0xb4, 0x02 },
  325. { 467000000, 44000000, 166666, 0xbc, 0x02 },
  326. { 721000000, 44000000, 166666, 0xbc, 0x08 },
  327. { 841000000, 44000000, 166666, 0xf4, 0x08 },
  328. { 999999999, 44000000, 166666, 0xfc, 0x02 },
  329. }
  330. };
  331. EXPORT_SYMBOL(dvb_pll_samsung_tbmv);
  332. /*
  333. * Philips SD1878 Tuner.
  334. */
  335. struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
  336. .name = "Philips SD1878",
  337. .min = 950000,
  338. .max = 2150000,
  339. .count = 4,
  340. .entries = {
  341. { 1250000, 499, 500, 0xc4, 0x00},
  342. { 1550000, 499, 500, 0xc4, 0x40},
  343. { 2050000, 499, 500, 0xc4, 0x80},
  344. { 2150000, 499, 500, 0xc4, 0xc0},
  345. },
  346. };
  347. EXPORT_SYMBOL(dvb_pll_philips_sd1878_tda8261);
  348. /*
  349. * Philips TD1316 Tuner.
  350. */
  351. static void td1316_bw(u8 *buf, u32 freq, int bandwidth)
  352. {
  353. u8 band;
  354. /* determine band */
  355. if (freq < 161000000)
  356. band = 1;
  357. else if (freq < 444000000)
  358. band = 2;
  359. else
  360. band = 4;
  361. buf[3] |= band;
  362. /* setup PLL filter */
  363. if (bandwidth == BANDWIDTH_8_MHZ)
  364. buf[3] |= 1 << 3;
  365. }
  366. struct dvb_pll_desc dvb_pll_philips_td1316 = {
  367. .name = "Philips TD1316",
  368. .min = 87000000,
  369. .max = 895000000,
  370. .setbw = td1316_bw,
  371. .count = 9,
  372. .entries = {
  373. { 93834000, 36166000, 166666, 0xca, 0x60},
  374. { 123834000, 36166000, 166666, 0xca, 0xa0},
  375. { 163834000, 36166000, 166666, 0xca, 0xc0},
  376. { 253834000, 36166000, 166666, 0xca, 0x60},
  377. { 383834000, 36166000, 166666, 0xca, 0xa0},
  378. { 443834000, 36166000, 166666, 0xca, 0xc0},
  379. { 583834000, 36166000, 166666, 0xca, 0x60},
  380. { 793834000, 36166000, 166666, 0xca, 0xa0},
  381. { 858834000, 36166000, 166666, 0xca, 0xe0},
  382. },
  383. };
  384. EXPORT_SYMBOL(dvb_pll_philips_td1316);
  385. /* FE6600 used on DViCO Hybrid */
  386. struct dvb_pll_desc dvb_pll_thomson_fe6600 = {
  387. .name = "Thomson FE6600",
  388. .min = 44250000,
  389. .max = 858000000,
  390. .count = 4,
  391. .entries = {
  392. { 250000000, 36213333, 166667, 0xb4, 0x12 },
  393. { 455000000, 36213333, 166667, 0xfe, 0x11 },
  394. { 775500000, 36213333, 166667, 0xbc, 0x18 },
  395. { 999999999, 36213333, 166667, 0xf4, 0x18 },
  396. }
  397. };
  398. EXPORT_SYMBOL(dvb_pll_thomson_fe6600);
  399. struct dvb_pll_priv {
  400. /* i2c details */
  401. int pll_i2c_address;
  402. struct i2c_adapter *i2c;
  403. /* the PLL descriptor */
  404. struct dvb_pll_desc *pll_desc;
  405. /* cached frequency/bandwidth */
  406. u32 frequency;
  407. u32 bandwidth;
  408. };
  409. /* ----------------------------------------------------------- */
  410. /* code */
  411. static int debug = 0;
  412. module_param(debug, int, 0644);
  413. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  414. int dvb_pll_configure(struct dvb_pll_desc *desc, u8 *buf,
  415. u32 freq, int bandwidth)
  416. {
  417. u32 div;
  418. int i;
  419. if (freq != 0 && (freq < desc->min || freq > desc->max))
  420. return -EINVAL;
  421. for (i = 0; i < desc->count; i++) {
  422. if (freq > desc->entries[i].limit)
  423. continue;
  424. break;
  425. }
  426. if (debug)
  427. printk("pll: %s: freq=%d bw=%d | i=%d/%d\n",
  428. desc->name, freq, bandwidth, i, desc->count);
  429. if (i == desc->count)
  430. return -EINVAL;
  431. div = (freq + desc->entries[i].offset) / desc->entries[i].stepsize;
  432. buf[0] = div >> 8;
  433. buf[1] = div & 0xff;
  434. buf[2] = desc->entries[i].config;
  435. buf[3] = desc->entries[i].cb;
  436. if (desc->setbw)
  437. desc->setbw(buf, freq, bandwidth);
  438. if (debug)
  439. printk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  440. desc->name, div, buf[0], buf[1], buf[2], buf[3]);
  441. // calculate the frequency we set it to
  442. return (div * desc->entries[i].stepsize) - desc->entries[i].offset;
  443. }
  444. EXPORT_SYMBOL(dvb_pll_configure);
  445. static int dvb_pll_release(struct dvb_frontend *fe)
  446. {
  447. kfree(fe->tuner_priv);
  448. fe->tuner_priv = NULL;
  449. return 0;
  450. }
  451. static int dvb_pll_sleep(struct dvb_frontend *fe)
  452. {
  453. struct dvb_pll_priv *priv = fe->tuner_priv;
  454. u8 buf[4];
  455. struct i2c_msg msg =
  456. { .addr = priv->pll_i2c_address, .flags = 0,
  457. .buf = buf, .len = sizeof(buf) };
  458. int i;
  459. int result;
  460. if (priv->i2c == NULL)
  461. return -EINVAL;
  462. for (i = 0; i < priv->pll_desc->count; i++) {
  463. if (priv->pll_desc->entries[i].limit == 0)
  464. break;
  465. }
  466. if (i == priv->pll_desc->count)
  467. return 0;
  468. buf[0] = 0;
  469. buf[1] = 0;
  470. buf[2] = priv->pll_desc->entries[i].config;
  471. buf[3] = priv->pll_desc->entries[i].cb;
  472. if (fe->ops.i2c_gate_ctrl)
  473. fe->ops.i2c_gate_ctrl(fe, 1);
  474. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  475. return result;
  476. }
  477. return 0;
  478. }
  479. static int dvb_pll_set_params(struct dvb_frontend *fe,
  480. struct dvb_frontend_parameters *params)
  481. {
  482. struct dvb_pll_priv *priv = fe->tuner_priv;
  483. u8 buf[4];
  484. struct i2c_msg msg =
  485. { .addr = priv->pll_i2c_address, .flags = 0,
  486. .buf = buf, .len = sizeof(buf) };
  487. int result;
  488. u32 bandwidth = 0, frequency = 0;
  489. if (priv->i2c == NULL)
  490. return -EINVAL;
  491. // DVBT bandwidth only just now
  492. if (fe->ops.info.type == FE_OFDM) {
  493. bandwidth = params->u.ofdm.bandwidth;
  494. }
  495. if ((result = dvb_pll_configure(priv->pll_desc, buf,
  496. params->frequency, bandwidth)) < 0)
  497. return result;
  498. else
  499. frequency = result;
  500. if (fe->ops.i2c_gate_ctrl)
  501. fe->ops.i2c_gate_ctrl(fe, 1);
  502. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  503. return result;
  504. }
  505. priv->frequency = frequency;
  506. priv->bandwidth = bandwidth;
  507. return 0;
  508. }
  509. static int dvb_pll_calc_regs(struct dvb_frontend *fe,
  510. struct dvb_frontend_parameters *params,
  511. u8 *buf, int buf_len)
  512. {
  513. struct dvb_pll_priv *priv = fe->tuner_priv;
  514. int result;
  515. u32 bandwidth = 0, frequency = 0;
  516. if (buf_len < 5)
  517. return -EINVAL;
  518. // DVBT bandwidth only just now
  519. if (fe->ops.info.type == FE_OFDM) {
  520. bandwidth = params->u.ofdm.bandwidth;
  521. }
  522. if ((result = dvb_pll_configure(priv->pll_desc, buf+1,
  523. params->frequency, bandwidth)) < 0)
  524. return result;
  525. else
  526. frequency = result;
  527. buf[0] = priv->pll_i2c_address;
  528. priv->frequency = frequency;
  529. priv->bandwidth = bandwidth;
  530. return 5;
  531. }
  532. static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  533. {
  534. struct dvb_pll_priv *priv = fe->tuner_priv;
  535. *frequency = priv->frequency;
  536. return 0;
  537. }
  538. static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  539. {
  540. struct dvb_pll_priv *priv = fe->tuner_priv;
  541. *bandwidth = priv->bandwidth;
  542. return 0;
  543. }
  544. static int dvb_pll_init(struct dvb_frontend *fe)
  545. {
  546. struct dvb_pll_priv *priv = fe->tuner_priv;
  547. if (priv->i2c == NULL)
  548. return -EINVAL;
  549. if (priv->pll_desc->initdata) {
  550. struct i2c_msg msg = { .flags = 0,
  551. .addr = priv->pll_i2c_address,
  552. .buf = priv->pll_desc->initdata + 1,
  553. .len = priv->pll_desc->initdata[0] };
  554. int result;
  555. if (fe->ops.i2c_gate_ctrl)
  556. fe->ops.i2c_gate_ctrl(fe, 1);
  557. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  558. return result;
  559. }
  560. return 0;
  561. }
  562. /* Shouldn't be called when initdata is NULL, maybe BUG()? */
  563. return -EINVAL;
  564. }
  565. static struct dvb_tuner_ops dvb_pll_tuner_ops = {
  566. .release = dvb_pll_release,
  567. .sleep = dvb_pll_sleep,
  568. .set_params = dvb_pll_set_params,
  569. .calc_regs = dvb_pll_calc_regs,
  570. .get_frequency = dvb_pll_get_frequency,
  571. .get_bandwidth = dvb_pll_get_bandwidth,
  572. };
  573. struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
  574. struct i2c_adapter *i2c,
  575. struct dvb_pll_desc *desc)
  576. {
  577. u8 b1 [] = { 0 };
  578. struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
  579. .buf = b1, .len = 1 };
  580. struct dvb_pll_priv *priv = NULL;
  581. int ret;
  582. if (i2c != NULL) {
  583. if (fe->ops.i2c_gate_ctrl)
  584. fe->ops.i2c_gate_ctrl(fe, 1);
  585. ret = i2c_transfer (i2c, &msg, 1);
  586. if (ret != 1)
  587. return NULL;
  588. if (fe->ops.i2c_gate_ctrl)
  589. fe->ops.i2c_gate_ctrl(fe, 0);
  590. }
  591. priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
  592. if (priv == NULL)
  593. return NULL;
  594. priv->pll_i2c_address = pll_addr;
  595. priv->i2c = i2c;
  596. priv->pll_desc = desc;
  597. memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
  598. sizeof(struct dvb_tuner_ops));
  599. strncpy(fe->ops.tuner_ops.info.name, desc->name,
  600. sizeof(fe->ops.tuner_ops.info.name));
  601. fe->ops.tuner_ops.info.frequency_min = desc->min;
  602. fe->ops.tuner_ops.info.frequency_min = desc->max;
  603. if (desc->initdata)
  604. fe->ops.tuner_ops.init = dvb_pll_init;
  605. fe->tuner_priv = priv;
  606. return fe;
  607. }
  608. EXPORT_SYMBOL(dvb_pll_attach);
  609. MODULE_DESCRIPTION("dvb pll library");
  610. MODULE_AUTHOR("Gerd Knorr");
  611. MODULE_LICENSE("GPL");