dvb-pll.c 18 KB

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