dvb-pll.c 19 KB

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