dvb-pll.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. u8 *initdata;
  54. u8 *initdata2;
  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. {
  82. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  83. if (bw == 7000000)
  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. {
  169. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  170. if (bw == 8000000)
  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. {
  201. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  202. if (bw == 7000000)
  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. {
  223. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  224. if (bw == 8000000)
  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. {
  293. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  294. struct dvb_pll_priv *priv = fe->tuner_priv;
  295. u32 b_w = (c->symbol_rate * 27) / 32000;
  296. struct i2c_msg msg = {
  297. .addr = priv->pll_i2c_address,
  298. .flags = 0,
  299. .buf = buf,
  300. .len = 4
  301. };
  302. int result;
  303. u8 lpf;
  304. if (fe->ops.i2c_gate_ctrl)
  305. fe->ops.i2c_gate_ctrl(fe, 1);
  306. result = i2c_transfer(priv->i2c, &msg, 1);
  307. if (result != 1)
  308. printk(KERN_ERR "%s: i2c_transfer failed:%d",
  309. __func__, result);
  310. if (b_w <= 10000)
  311. lpf = 0xc;
  312. else if (b_w <= 12000)
  313. lpf = 0x2;
  314. else if (b_w <= 14000)
  315. lpf = 0xa;
  316. else if (b_w <= 16000)
  317. lpf = 0x6;
  318. else if (b_w <= 18000)
  319. lpf = 0xe;
  320. else if (b_w <= 20000)
  321. lpf = 0x1;
  322. else if (b_w <= 22000)
  323. lpf = 0x9;
  324. else if (b_w <= 24000)
  325. lpf = 0x5;
  326. else if (b_w <= 26000)
  327. lpf = 0xd;
  328. else if (b_w <= 28000)
  329. lpf = 0x3;
  330. else
  331. lpf = 0xb;
  332. buf[2] ^= 0x1c; /* Flip bits 3-5 */
  333. /* Set lpf */
  334. buf[2] |= ((lpf >> 2) & 0x3) << 3;
  335. buf[3] |= (lpf & 0x3) << 2;
  336. return;
  337. }
  338. static struct dvb_pll_desc dvb_pll_opera1 = {
  339. .name = "Opera Tuner",
  340. .min = 900000,
  341. .max = 2250000,
  342. .initdata = (u8[]){ 4, 0x08, 0xe5, 0xe1, 0x00 },
  343. .initdata2 = (u8[]){ 4, 0x08, 0xe5, 0xe5, 0x00 },
  344. .iffreq= 0,
  345. .set = opera1_bw,
  346. .count = 8,
  347. .entries = {
  348. { 1064000, 500, 0xf9, 0xc2 },
  349. { 1169000, 500, 0xf9, 0xe2 },
  350. { 1299000, 500, 0xf9, 0x20 },
  351. { 1444000, 500, 0xf9, 0x40 },
  352. { 1606000, 500, 0xf9, 0x60 },
  353. { 1777000, 500, 0xf9, 0x80 },
  354. { 1941000, 500, 0xf9, 0xa0 },
  355. { 2250000, 500, 0xf9, 0xc0 },
  356. }
  357. };
  358. static void samsung_dtos403ih102a_set(struct dvb_frontend *fe, u8 *buf)
  359. {
  360. struct dvb_pll_priv *priv = fe->tuner_priv;
  361. struct i2c_msg msg = {
  362. .addr = priv->pll_i2c_address,
  363. .flags = 0,
  364. .buf = buf,
  365. .len = 4
  366. };
  367. int result;
  368. if (fe->ops.i2c_gate_ctrl)
  369. fe->ops.i2c_gate_ctrl(fe, 1);
  370. result = i2c_transfer(priv->i2c, &msg, 1);
  371. if (result != 1)
  372. printk(KERN_ERR "%s: i2c_transfer failed:%d",
  373. __func__, result);
  374. buf[2] = 0x9e;
  375. buf[3] = 0x90;
  376. return;
  377. }
  378. /* unknown pll used in Samsung DTOS403IH102A DVB-C tuner */
  379. static struct dvb_pll_desc dvb_pll_samsung_dtos403ih102a = {
  380. .name = "Samsung DTOS403IH102A",
  381. .min = 44250000,
  382. .max = 858000000,
  383. .iffreq = 36125000,
  384. .count = 8,
  385. .set = samsung_dtos403ih102a_set,
  386. .entries = {
  387. { 135000000, 62500, 0xbe, 0x01 },
  388. { 177000000, 62500, 0xf6, 0x01 },
  389. { 370000000, 62500, 0xbe, 0x02 },
  390. { 450000000, 62500, 0xf6, 0x02 },
  391. { 466000000, 62500, 0xfe, 0x02 },
  392. { 538000000, 62500, 0xbe, 0x08 },
  393. { 826000000, 62500, 0xf6, 0x08 },
  394. { 999999999, 62500, 0xfe, 0x08 },
  395. }
  396. };
  397. /* Samsung TDTC9251DH0 DVB-T NIM, as used on AirStar 2 */
  398. static struct dvb_pll_desc dvb_pll_samsung_tdtc9251dh0 = {
  399. .name = "Samsung TDTC9251DH0",
  400. .min = 48000000,
  401. .max = 863000000,
  402. .iffreq = 36166667,
  403. .count = 3,
  404. .entries = {
  405. { 157500000, 166667, 0xcc, 0x09 },
  406. { 443000000, 166667, 0xcc, 0x0a },
  407. { 863000000, 166667, 0xcc, 0x08 },
  408. }
  409. };
  410. /* Samsung TBDU18132 DVB-S NIM with TSA5059 PLL, used in SkyStar2 DVB-S 2.3 */
  411. static struct dvb_pll_desc dvb_pll_samsung_tbdu18132 = {
  412. .name = "Samsung TBDU18132",
  413. .min = 950000,
  414. .max = 2150000, /* guesses */
  415. .iffreq = 0,
  416. .count = 2,
  417. .entries = {
  418. { 1550000, 125, 0x84, 0x82 },
  419. { 4095937, 125, 0x84, 0x80 },
  420. }
  421. /* TSA5059 PLL has a 17 bit divisor rather than the 15 bits supported
  422. * by this driver. The two extra bits are 0x60 in the third byte. 15
  423. * bits is enough for over 4 GHz, which is enough to cover the range
  424. * of this tuner. We could use the additional divisor bits by adding
  425. * more entries, e.g.
  426. { 0x0ffff * 125 + 125/2, 125, 0x84 | 0x20, },
  427. { 0x17fff * 125 + 125/2, 125, 0x84 | 0x40, },
  428. { 0x1ffff * 125 + 125/2, 125, 0x84 | 0x60, }, */
  429. };
  430. /* Samsung TBMU24112 DVB-S NIM with SL1935 zero-IF tuner */
  431. static struct dvb_pll_desc dvb_pll_samsung_tbmu24112 = {
  432. .name = "Samsung TBMU24112",
  433. .min = 950000,
  434. .max = 2150000, /* guesses */
  435. .iffreq = 0,
  436. .count = 2,
  437. .entries = {
  438. { 1500000, 125, 0x84, 0x18 },
  439. { 9999999, 125, 0x84, 0x08 },
  440. }
  441. };
  442. /* Alps TDEE4 DVB-C NIM, used on Cablestar 2 */
  443. /* byte 4 : 1 * * AGD R3 R2 R1 R0
  444. * byte 5 : C1 * RE RTS BS4 BS3 BS2 BS1
  445. * AGD = 1, R3 R2 R1 R0 = 0 1 0 1 => byte 4 = 1**10101 = 0x95
  446. * Range(MHz) C1 * RE RTS BS4 BS3 BS2 BS1 Byte 5
  447. * 47 - 153 0 * 0 0 0 0 0 1 0x01
  448. * 153 - 430 0 * 0 0 0 0 1 0 0x02
  449. * 430 - 822 0 * 0 0 1 0 0 0 0x08
  450. * 822 - 862 1 * 0 0 1 0 0 0 0x88 */
  451. static struct dvb_pll_desc dvb_pll_alps_tdee4 = {
  452. .name = "ALPS TDEE4",
  453. .min = 47000000,
  454. .max = 862000000,
  455. .iffreq = 36125000,
  456. .count = 4,
  457. .entries = {
  458. { 153000000, 62500, 0x95, 0x01 },
  459. { 430000000, 62500, 0x95, 0x02 },
  460. { 822000000, 62500, 0x95, 0x08 },
  461. { 999999999, 62500, 0x95, 0x88 },
  462. }
  463. };
  464. /* ----------------------------------------------------------- */
  465. static struct dvb_pll_desc *pll_list[] = {
  466. [DVB_PLL_UNDEFINED] = NULL,
  467. [DVB_PLL_THOMSON_DTT7579] = &dvb_pll_thomson_dtt7579,
  468. [DVB_PLL_THOMSON_DTT759X] = &dvb_pll_thomson_dtt759x,
  469. [DVB_PLL_LG_Z201] = &dvb_pll_lg_z201,
  470. [DVB_PLL_UNKNOWN_1] = &dvb_pll_unknown_1,
  471. [DVB_PLL_TUA6010XS] = &dvb_pll_tua6010xs,
  472. [DVB_PLL_ENV57H1XD5] = &dvb_pll_env57h1xd5,
  473. [DVB_PLL_TUA6034] = &dvb_pll_tua6034,
  474. [DVB_PLL_TDA665X] = &dvb_pll_tda665x,
  475. [DVB_PLL_TDED4] = &dvb_pll_tded4,
  476. [DVB_PLL_TDEE4] = &dvb_pll_alps_tdee4,
  477. [DVB_PLL_TDHU2] = &dvb_pll_tdhu2,
  478. [DVB_PLL_SAMSUNG_TBMV] = &dvb_pll_samsung_tbmv,
  479. [DVB_PLL_PHILIPS_SD1878_TDA8261] = &dvb_pll_philips_sd1878_tda8261,
  480. [DVB_PLL_OPERA1] = &dvb_pll_opera1,
  481. [DVB_PLL_SAMSUNG_DTOS403IH102A] = &dvb_pll_samsung_dtos403ih102a,
  482. [DVB_PLL_SAMSUNG_TDTC9251DH0] = &dvb_pll_samsung_tdtc9251dh0,
  483. [DVB_PLL_SAMSUNG_TBDU18132] = &dvb_pll_samsung_tbdu18132,
  484. [DVB_PLL_SAMSUNG_TBMU24112] = &dvb_pll_samsung_tbmu24112,
  485. };
  486. /* ----------------------------------------------------------- */
  487. /* code */
  488. static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
  489. const u32 frequency)
  490. {
  491. struct dvb_pll_priv *priv = fe->tuner_priv;
  492. struct dvb_pll_desc *desc = priv->pll_desc;
  493. u32 div;
  494. int i;
  495. if (frequency && (frequency < desc->min || frequency > desc->max))
  496. return -EINVAL;
  497. for (i = 0; i < desc->count; i++) {
  498. if (frequency > desc->entries[i].limit)
  499. continue;
  500. break;
  501. }
  502. if (debug)
  503. printk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
  504. frequency, i, desc->count);
  505. if (i == desc->count)
  506. return -EINVAL;
  507. div = (frequency + desc->iffreq +
  508. desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
  509. buf[0] = div >> 8;
  510. buf[1] = div & 0xff;
  511. buf[2] = desc->entries[i].config;
  512. buf[3] = desc->entries[i].cb;
  513. if (desc->set)
  514. desc->set(fe, buf);
  515. if (debug)
  516. printk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  517. desc->name, div, buf[0], buf[1], buf[2], buf[3]);
  518. // calculate the frequency we set it to
  519. return (div * desc->entries[i].stepsize) - desc->iffreq;
  520. }
  521. static int dvb_pll_release(struct dvb_frontend *fe)
  522. {
  523. kfree(fe->tuner_priv);
  524. fe->tuner_priv = NULL;
  525. return 0;
  526. }
  527. static int dvb_pll_sleep(struct dvb_frontend *fe)
  528. {
  529. struct dvb_pll_priv *priv = fe->tuner_priv;
  530. if (priv->i2c == NULL)
  531. return -EINVAL;
  532. if (priv->pll_desc->sleepdata) {
  533. struct i2c_msg msg = { .flags = 0,
  534. .addr = priv->pll_i2c_address,
  535. .buf = priv->pll_desc->sleepdata + 1,
  536. .len = priv->pll_desc->sleepdata[0] };
  537. int result;
  538. if (fe->ops.i2c_gate_ctrl)
  539. fe->ops.i2c_gate_ctrl(fe, 1);
  540. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  541. return result;
  542. }
  543. return 0;
  544. }
  545. /* Shouldn't be called when initdata is NULL, maybe BUG()? */
  546. return -EINVAL;
  547. }
  548. static int dvb_pll_set_params(struct dvb_frontend *fe)
  549. {
  550. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  551. struct dvb_pll_priv *priv = fe->tuner_priv;
  552. u8 buf[4];
  553. struct i2c_msg msg =
  554. { .addr = priv->pll_i2c_address, .flags = 0,
  555. .buf = buf, .len = sizeof(buf) };
  556. int result;
  557. u32 frequency = 0;
  558. if (priv->i2c == NULL)
  559. return -EINVAL;
  560. result = dvb_pll_configure(fe, buf, c->frequency);
  561. if (result < 0)
  562. return result;
  563. else
  564. frequency = result;
  565. if (fe->ops.i2c_gate_ctrl)
  566. fe->ops.i2c_gate_ctrl(fe, 1);
  567. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  568. return result;
  569. }
  570. priv->frequency = frequency;
  571. priv->bandwidth = c->bandwidth_hz;
  572. return 0;
  573. }
  574. static int dvb_pll_calc_regs(struct dvb_frontend *fe,
  575. u8 *buf, int buf_len)
  576. {
  577. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  578. struct dvb_pll_priv *priv = fe->tuner_priv;
  579. int result;
  580. u32 frequency = 0;
  581. if (buf_len < 5)
  582. return -EINVAL;
  583. result = dvb_pll_configure(fe, buf + 1, c->frequency);
  584. if (result < 0)
  585. return result;
  586. else
  587. frequency = result;
  588. buf[0] = priv->pll_i2c_address;
  589. priv->frequency = frequency;
  590. priv->bandwidth = c->bandwidth_hz;
  591. return 5;
  592. }
  593. static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  594. {
  595. struct dvb_pll_priv *priv = fe->tuner_priv;
  596. *frequency = priv->frequency;
  597. return 0;
  598. }
  599. static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  600. {
  601. struct dvb_pll_priv *priv = fe->tuner_priv;
  602. *bandwidth = priv->bandwidth;
  603. return 0;
  604. }
  605. static int dvb_pll_init(struct dvb_frontend *fe)
  606. {
  607. struct dvb_pll_priv *priv = fe->tuner_priv;
  608. if (priv->i2c == NULL)
  609. return -EINVAL;
  610. if (priv->pll_desc->initdata) {
  611. struct i2c_msg msg = { .flags = 0,
  612. .addr = priv->pll_i2c_address,
  613. .buf = priv->pll_desc->initdata + 1,
  614. .len = priv->pll_desc->initdata[0] };
  615. int result;
  616. if (fe->ops.i2c_gate_ctrl)
  617. fe->ops.i2c_gate_ctrl(fe, 1);
  618. result = i2c_transfer(priv->i2c, &msg, 1);
  619. if (result != 1)
  620. return result;
  621. if (priv->pll_desc->initdata2) {
  622. msg.buf = priv->pll_desc->initdata2 + 1;
  623. msg.len = priv->pll_desc->initdata2[0];
  624. if (fe->ops.i2c_gate_ctrl)
  625. fe->ops.i2c_gate_ctrl(fe, 1);
  626. result = i2c_transfer(priv->i2c, &msg, 1);
  627. if (result != 1)
  628. return result;
  629. }
  630. return 0;
  631. }
  632. /* Shouldn't be called when initdata is NULL, maybe BUG()? */
  633. return -EINVAL;
  634. }
  635. static struct dvb_tuner_ops dvb_pll_tuner_ops = {
  636. .release = dvb_pll_release,
  637. .sleep = dvb_pll_sleep,
  638. .init = dvb_pll_init,
  639. .set_params = dvb_pll_set_params,
  640. .calc_regs = dvb_pll_calc_regs,
  641. .get_frequency = dvb_pll_get_frequency,
  642. .get_bandwidth = dvb_pll_get_bandwidth,
  643. };
  644. struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
  645. struct i2c_adapter *i2c,
  646. unsigned int pll_desc_id)
  647. {
  648. u8 b1 [] = { 0 };
  649. struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
  650. .buf = b1, .len = 1 };
  651. struct dvb_pll_priv *priv = NULL;
  652. int ret;
  653. struct dvb_pll_desc *desc;
  654. if ((id[dvb_pll_devcount] > DVB_PLL_UNDEFINED) &&
  655. (id[dvb_pll_devcount] < ARRAY_SIZE(pll_list)))
  656. pll_desc_id = id[dvb_pll_devcount];
  657. BUG_ON(pll_desc_id < 1 || pll_desc_id >= ARRAY_SIZE(pll_list));
  658. desc = pll_list[pll_desc_id];
  659. if (i2c != NULL) {
  660. if (fe->ops.i2c_gate_ctrl)
  661. fe->ops.i2c_gate_ctrl(fe, 1);
  662. ret = i2c_transfer (i2c, &msg, 1);
  663. if (ret != 1)
  664. return NULL;
  665. if (fe->ops.i2c_gate_ctrl)
  666. fe->ops.i2c_gate_ctrl(fe, 0);
  667. }
  668. priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
  669. if (priv == NULL)
  670. return NULL;
  671. priv->pll_i2c_address = pll_addr;
  672. priv->i2c = i2c;
  673. priv->pll_desc = desc;
  674. priv->nr = dvb_pll_devcount++;
  675. memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
  676. sizeof(struct dvb_tuner_ops));
  677. strncpy(fe->ops.tuner_ops.info.name, desc->name,
  678. sizeof(fe->ops.tuner_ops.info.name));
  679. fe->ops.tuner_ops.info.frequency_min = desc->min;
  680. fe->ops.tuner_ops.info.frequency_max = desc->max;
  681. if (!desc->initdata)
  682. fe->ops.tuner_ops.init = NULL;
  683. if (!desc->sleepdata)
  684. fe->ops.tuner_ops.sleep = NULL;
  685. fe->tuner_priv = priv;
  686. if ((debug) || (id[priv->nr] == pll_desc_id)) {
  687. printk("dvb-pll[%d]", priv->nr);
  688. if (i2c != NULL)
  689. printk(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
  690. printk(": id# %d (%s) attached, %s\n", pll_desc_id, desc->name,
  691. id[priv->nr] == pll_desc_id ?
  692. "insmod option" : "autodetected");
  693. }
  694. return fe;
  695. }
  696. EXPORT_SYMBOL(dvb_pll_attach);
  697. MODULE_DESCRIPTION("dvb pll library");
  698. MODULE_AUTHOR("Gerd Knorr");
  699. MODULE_LICENSE("GPL");