dvb-pll.c 18 KB

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