dvb-pll.c 21 KB

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