dvb-pll.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * descriptions + helper functions for simple dvb plls.
  4. *
  5. * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/idr.h>
  11. #include <linux/dvb/frontend.h>
  12. #include <asm/types.h>
  13. #include "dvb-pll.h"
  14. #define dprintk(fmt, arg...) \
  15. printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
  16. struct dvb_pll_priv {
  17. /* pll number */
  18. int nr;
  19. /* i2c details */
  20. int pll_i2c_address;
  21. struct i2c_adapter *i2c;
  22. /* the PLL descriptor */
  23. const struct dvb_pll_desc *pll_desc;
  24. /* cached frequency/bandwidth */
  25. u32 frequency;
  26. u32 bandwidth;
  27. };
  28. #define DVB_PLL_MAX 64
  29. static DEFINE_IDA(pll_ida);
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  33. static unsigned int id[DVB_PLL_MAX] =
  34. { [ 0 ... (DVB_PLL_MAX-1) ] = DVB_PLL_UNDEFINED };
  35. module_param_array(id, int, NULL, 0644);
  36. MODULE_PARM_DESC(id, "force pll id to use (DEBUG ONLY)");
  37. /* ----------------------------------------------------------- */
  38. struct dvb_pll_desc {
  39. const char *name;
  40. u32 min;
  41. u32 max;
  42. u32 iffreq;
  43. void (*set)(struct dvb_frontend *fe, u8 *buf);
  44. u8 *initdata;
  45. u8 *initdata2;
  46. u8 *sleepdata;
  47. int count;
  48. struct {
  49. u32 limit;
  50. u32 stepsize;
  51. u8 config;
  52. u8 cb;
  53. } entries[];
  54. };
  55. /* ----------------------------------------------------------- */
  56. /* descriptions */
  57. static const struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
  58. .name = "Thomson dtt7579",
  59. .min = 177 * MHz,
  60. .max = 858 * MHz,
  61. .iffreq= 36166667,
  62. .sleepdata = (u8[]){ 2, 0xb4, 0x03 },
  63. .count = 4,
  64. .entries = {
  65. { 443250000, 166667, 0xb4, 0x02 },
  66. { 542000000, 166667, 0xb4, 0x08 },
  67. { 771000000, 166667, 0xbc, 0x08 },
  68. { 999999999, 166667, 0xf4, 0x08 },
  69. },
  70. };
  71. static void thomson_dtt759x_bw(struct dvb_frontend *fe, u8 *buf)
  72. {
  73. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  74. if (bw == 7000000)
  75. buf[3] |= 0x10;
  76. }
  77. static const struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
  78. .name = "Thomson dtt759x",
  79. .min = 177 * MHz,
  80. .max = 896 * MHz,
  81. .set = thomson_dtt759x_bw,
  82. .iffreq= 36166667,
  83. .sleepdata = (u8[]){ 2, 0x84, 0x03 },
  84. .count = 5,
  85. .entries = {
  86. { 264000000, 166667, 0xb4, 0x02 },
  87. { 470000000, 166667, 0xbc, 0x02 },
  88. { 735000000, 166667, 0xbc, 0x08 },
  89. { 835000000, 166667, 0xf4, 0x08 },
  90. { 999999999, 166667, 0xfc, 0x08 },
  91. },
  92. };
  93. static void thomson_dtt7520x_bw(struct dvb_frontend *fe, u8 *buf)
  94. {
  95. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  96. if (bw == 8000000)
  97. buf[3] ^= 0x10;
  98. }
  99. static const struct dvb_pll_desc dvb_pll_thomson_dtt7520x = {
  100. .name = "Thomson dtt7520x",
  101. .min = 185 * MHz,
  102. .max = 900 * MHz,
  103. .set = thomson_dtt7520x_bw,
  104. .iffreq = 36166667,
  105. .count = 7,
  106. .entries = {
  107. { 305000000, 166667, 0xb4, 0x12 },
  108. { 405000000, 166667, 0xbc, 0x12 },
  109. { 445000000, 166667, 0xbc, 0x12 },
  110. { 465000000, 166667, 0xf4, 0x18 },
  111. { 735000000, 166667, 0xfc, 0x18 },
  112. { 835000000, 166667, 0xbc, 0x18 },
  113. { 999999999, 166667, 0xfc, 0x18 },
  114. },
  115. };
  116. static const struct dvb_pll_desc dvb_pll_lg_z201 = {
  117. .name = "LG z201",
  118. .min = 174 * MHz,
  119. .max = 862 * MHz,
  120. .iffreq= 36166667,
  121. .sleepdata = (u8[]){ 2, 0xbc, 0x03 },
  122. .count = 5,
  123. .entries = {
  124. { 157500000, 166667, 0xbc, 0x01 },
  125. { 443250000, 166667, 0xbc, 0x02 },
  126. { 542000000, 166667, 0xbc, 0x04 },
  127. { 830000000, 166667, 0xf4, 0x04 },
  128. { 999999999, 166667, 0xfc, 0x04 },
  129. },
  130. };
  131. static const struct dvb_pll_desc dvb_pll_unknown_1 = {
  132. .name = "unknown 1", /* used by dntv live dvb-t */
  133. .min = 174 * MHz,
  134. .max = 862 * MHz,
  135. .iffreq= 36166667,
  136. .count = 9,
  137. .entries = {
  138. { 150000000, 166667, 0xb4, 0x01 },
  139. { 173000000, 166667, 0xbc, 0x01 },
  140. { 250000000, 166667, 0xb4, 0x02 },
  141. { 400000000, 166667, 0xbc, 0x02 },
  142. { 420000000, 166667, 0xf4, 0x02 },
  143. { 470000000, 166667, 0xfc, 0x02 },
  144. { 600000000, 166667, 0xbc, 0x08 },
  145. { 730000000, 166667, 0xf4, 0x08 },
  146. { 999999999, 166667, 0xfc, 0x08 },
  147. },
  148. };
  149. /* Infineon TUA6010XS
  150. * used in Thomson Cable Tuner
  151. */
  152. static const struct dvb_pll_desc dvb_pll_tua6010xs = {
  153. .name = "Infineon TUA6010XS",
  154. .min = 44250 * kHz,
  155. .max = 858 * MHz,
  156. .iffreq= 36125000,
  157. .count = 3,
  158. .entries = {
  159. { 115750000, 62500, 0x8e, 0x03 },
  160. { 403250000, 62500, 0x8e, 0x06 },
  161. { 999999999, 62500, 0x8e, 0x85 },
  162. },
  163. };
  164. /* Panasonic env57h1xd5 (some Philips PLL ?) */
  165. static const struct dvb_pll_desc dvb_pll_env57h1xd5 = {
  166. .name = "Panasonic ENV57H1XD5",
  167. .min = 44250 * kHz,
  168. .max = 858 * MHz,
  169. .iffreq= 36125000,
  170. .count = 4,
  171. .entries = {
  172. { 153000000, 166667, 0xc2, 0x41 },
  173. { 470000000, 166667, 0xc2, 0x42 },
  174. { 526000000, 166667, 0xc2, 0x84 },
  175. { 999999999, 166667, 0xc2, 0xa4 },
  176. },
  177. };
  178. /* Philips TDA6650/TDA6651
  179. * used in Panasonic ENV77H11D5
  180. */
  181. static void tda665x_bw(struct dvb_frontend *fe, u8 *buf)
  182. {
  183. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  184. if (bw == 8000000)
  185. buf[3] |= 0x08;
  186. }
  187. static const struct dvb_pll_desc dvb_pll_tda665x = {
  188. .name = "Philips TDA6650/TDA6651",
  189. .min = 44250 * kHz,
  190. .max = 858 * MHz,
  191. .set = tda665x_bw,
  192. .iffreq= 36166667,
  193. .initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
  194. .count = 12,
  195. .entries = {
  196. { 93834000, 166667, 0xca, 0x61 /* 011 0 0 0 01 */ },
  197. { 123834000, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
  198. { 161000000, 166667, 0xca, 0xa1 /* 101 0 0 0 01 */ },
  199. { 163834000, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
  200. { 253834000, 166667, 0xca, 0x62 /* 011 0 0 0 10 */ },
  201. { 383834000, 166667, 0xca, 0xa2 /* 101 0 0 0 10 */ },
  202. { 443834000, 166667, 0xca, 0xc2 /* 110 0 0 0 10 */ },
  203. { 444000000, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
  204. { 583834000, 166667, 0xca, 0x64 /* 011 0 0 1 00 */ },
  205. { 793834000, 166667, 0xca, 0xa4 /* 101 0 0 1 00 */ },
  206. { 444834000, 166667, 0xca, 0xc4 /* 110 0 0 1 00 */ },
  207. { 861000000, 166667, 0xca, 0xe4 /* 111 0 0 1 00 */ },
  208. }
  209. };
  210. /* Infineon TUA6034
  211. * used in LG TDTP E102P
  212. */
  213. static void tua6034_bw(struct dvb_frontend *fe, u8 *buf)
  214. {
  215. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  216. if (bw == 7000000)
  217. buf[3] |= 0x08;
  218. }
  219. static const struct dvb_pll_desc dvb_pll_tua6034 = {
  220. .name = "Infineon TUA6034",
  221. .min = 44250 * kHz,
  222. .max = 858 * MHz,
  223. .iffreq= 36166667,
  224. .count = 3,
  225. .set = tua6034_bw,
  226. .entries = {
  227. { 174500000, 62500, 0xce, 0x01 },
  228. { 230000000, 62500, 0xce, 0x02 },
  229. { 999999999, 62500, 0xce, 0x04 },
  230. },
  231. };
  232. /* ALPS TDED4
  233. * used in Nebula-Cards and USB boxes
  234. */
  235. static void tded4_bw(struct dvb_frontend *fe, u8 *buf)
  236. {
  237. u32 bw = fe->dtv_property_cache.bandwidth_hz;
  238. if (bw == 8000000)
  239. buf[3] |= 0x04;
  240. }
  241. static const struct dvb_pll_desc dvb_pll_tded4 = {
  242. .name = "ALPS TDED4",
  243. .min = 47 * MHz,
  244. .max = 863 * MHz,
  245. .iffreq= 36166667,
  246. .set = tded4_bw,
  247. .count = 4,
  248. .entries = {
  249. { 153000000, 166667, 0x85, 0x01 },
  250. { 470000000, 166667, 0x85, 0x02 },
  251. { 823000000, 166667, 0x85, 0x08 },
  252. { 999999999, 166667, 0x85, 0x88 },
  253. }
  254. };
  255. /* ALPS TDHU2
  256. * used in AverTVHD MCE A180
  257. */
  258. static const struct dvb_pll_desc dvb_pll_tdhu2 = {
  259. .name = "ALPS TDHU2",
  260. .min = 54 * MHz,
  261. .max = 864 * MHz,
  262. .iffreq= 44000000,
  263. .count = 4,
  264. .entries = {
  265. { 162000000, 62500, 0x85, 0x01 },
  266. { 426000000, 62500, 0x85, 0x02 },
  267. { 782000000, 62500, 0x85, 0x08 },
  268. { 999999999, 62500, 0x85, 0x88 },
  269. }
  270. };
  271. /* Samsung TBMV30111IN / TBMV30712IN1
  272. * used in Air2PC ATSC - 2nd generation (nxt2002)
  273. */
  274. static const struct dvb_pll_desc dvb_pll_samsung_tbmv = {
  275. .name = "Samsung TBMV30111IN / TBMV30712IN1",
  276. .min = 54 * MHz,
  277. .max = 860 * MHz,
  278. .iffreq= 44000000,
  279. .count = 6,
  280. .entries = {
  281. { 172000000, 166667, 0xb4, 0x01 },
  282. { 214000000, 166667, 0xb4, 0x02 },
  283. { 467000000, 166667, 0xbc, 0x02 },
  284. { 721000000, 166667, 0xbc, 0x08 },
  285. { 841000000, 166667, 0xf4, 0x08 },
  286. { 999999999, 166667, 0xfc, 0x02 },
  287. }
  288. };
  289. /*
  290. * Philips SD1878 Tuner.
  291. */
  292. static const struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
  293. .name = "Philips SD1878",
  294. .min = 950 * MHz,
  295. .max = 2150 * MHz,
  296. .iffreq= 249, /* zero-IF, offset 249 is to round up */
  297. .count = 4,
  298. .entries = {
  299. { 1250000, 500, 0xc4, 0x00},
  300. { 1450000, 500, 0xc4, 0x40},
  301. { 2050000, 500, 0xc4, 0x80},
  302. { 2150000, 500, 0xc4, 0xc0},
  303. },
  304. };
  305. static void opera1_bw(struct dvb_frontend *fe, u8 *buf)
  306. {
  307. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  308. struct dvb_pll_priv *priv = fe->tuner_priv;
  309. u32 b_w = (c->symbol_rate * 27) / 32000;
  310. struct i2c_msg msg = {
  311. .addr = priv->pll_i2c_address,
  312. .flags = 0,
  313. .buf = buf,
  314. .len = 4
  315. };
  316. int result;
  317. u8 lpf;
  318. if (fe->ops.i2c_gate_ctrl)
  319. fe->ops.i2c_gate_ctrl(fe, 1);
  320. result = i2c_transfer(priv->i2c, &msg, 1);
  321. if (result != 1)
  322. pr_err("%s: i2c_transfer failed:%d",
  323. __func__, result);
  324. if (b_w <= 10000)
  325. lpf = 0xc;
  326. else if (b_w <= 12000)
  327. lpf = 0x2;
  328. else if (b_w <= 14000)
  329. lpf = 0xa;
  330. else if (b_w <= 16000)
  331. lpf = 0x6;
  332. else if (b_w <= 18000)
  333. lpf = 0xe;
  334. else if (b_w <= 20000)
  335. lpf = 0x1;
  336. else if (b_w <= 22000)
  337. lpf = 0x9;
  338. else if (b_w <= 24000)
  339. lpf = 0x5;
  340. else if (b_w <= 26000)
  341. lpf = 0xd;
  342. else if (b_w <= 28000)
  343. lpf = 0x3;
  344. else
  345. lpf = 0xb;
  346. buf[2] ^= 0x1c; /* Flip bits 3-5 */
  347. /* Set lpf */
  348. buf[2] |= ((lpf >> 2) & 0x3) << 3;
  349. buf[3] |= (lpf & 0x3) << 2;
  350. return;
  351. }
  352. static const struct dvb_pll_desc dvb_pll_opera1 = {
  353. .name = "Opera Tuner",
  354. .min = 900 * MHz,
  355. .max = 2250 * MHz,
  356. .initdata = (u8[]){ 4, 0x08, 0xe5, 0xe1, 0x00 },
  357. .initdata2 = (u8[]){ 4, 0x08, 0xe5, 0xe5, 0x00 },
  358. .iffreq= 0,
  359. .set = opera1_bw,
  360. .count = 8,
  361. .entries = {
  362. { 1064000, 500, 0xf9, 0xc2 },
  363. { 1169000, 500, 0xf9, 0xe2 },
  364. { 1299000, 500, 0xf9, 0x20 },
  365. { 1444000, 500, 0xf9, 0x40 },
  366. { 1606000, 500, 0xf9, 0x60 },
  367. { 1777000, 500, 0xf9, 0x80 },
  368. { 1941000, 500, 0xf9, 0xa0 },
  369. { 2250000, 500, 0xf9, 0xc0 },
  370. }
  371. };
  372. static void samsung_dtos403ih102a_set(struct dvb_frontend *fe, u8 *buf)
  373. {
  374. struct dvb_pll_priv *priv = fe->tuner_priv;
  375. struct i2c_msg msg = {
  376. .addr = priv->pll_i2c_address,
  377. .flags = 0,
  378. .buf = buf,
  379. .len = 4
  380. };
  381. int result;
  382. if (fe->ops.i2c_gate_ctrl)
  383. fe->ops.i2c_gate_ctrl(fe, 1);
  384. result = i2c_transfer(priv->i2c, &msg, 1);
  385. if (result != 1)
  386. pr_err("%s: i2c_transfer failed:%d",
  387. __func__, result);
  388. buf[2] = 0x9e;
  389. buf[3] = 0x90;
  390. return;
  391. }
  392. /* unknown pll used in Samsung DTOS403IH102A DVB-C tuner */
  393. static const struct dvb_pll_desc dvb_pll_samsung_dtos403ih102a = {
  394. .name = "Samsung DTOS403IH102A",
  395. .min = 44250 * kHz,
  396. .max = 858 * MHz,
  397. .iffreq = 36125000,
  398. .count = 8,
  399. .set = samsung_dtos403ih102a_set,
  400. .entries = {
  401. { 135000000, 62500, 0xbe, 0x01 },
  402. { 177000000, 62500, 0xf6, 0x01 },
  403. { 370000000, 62500, 0xbe, 0x02 },
  404. { 450000000, 62500, 0xf6, 0x02 },
  405. { 466000000, 62500, 0xfe, 0x02 },
  406. { 538000000, 62500, 0xbe, 0x08 },
  407. { 826000000, 62500, 0xf6, 0x08 },
  408. { 999999999, 62500, 0xfe, 0x08 },
  409. }
  410. };
  411. /* Samsung TDTC9251DH0 DVB-T NIM, as used on AirStar 2 */
  412. static const struct dvb_pll_desc dvb_pll_samsung_tdtc9251dh0 = {
  413. .name = "Samsung TDTC9251DH0",
  414. .min = 48 * MHz,
  415. .max = 863 * MHz,
  416. .iffreq = 36166667,
  417. .count = 3,
  418. .entries = {
  419. { 157500000, 166667, 0xcc, 0x09 },
  420. { 443000000, 166667, 0xcc, 0x0a },
  421. { 863000000, 166667, 0xcc, 0x08 },
  422. }
  423. };
  424. /* Samsung TBDU18132 DVB-S NIM with TSA5059 PLL, used in SkyStar2 DVB-S 2.3 */
  425. static const struct dvb_pll_desc dvb_pll_samsung_tbdu18132 = {
  426. .name = "Samsung TBDU18132",
  427. .min = 950 * MHz,
  428. .max = 2150 * MHz, /* guesses */
  429. .iffreq = 0,
  430. .count = 2,
  431. .entries = {
  432. { 1550000, 125, 0x84, 0x82 },
  433. { 4095937, 125, 0x84, 0x80 },
  434. }
  435. /* TSA5059 PLL has a 17 bit divisor rather than the 15 bits supported
  436. * by this driver. The two extra bits are 0x60 in the third byte. 15
  437. * bits is enough for over 4 GHz, which is enough to cover the range
  438. * of this tuner. We could use the additional divisor bits by adding
  439. * more entries, e.g.
  440. { 0x0ffff * 125 + 125/2, 125, 0x84 | 0x20, },
  441. { 0x17fff * 125 + 125/2, 125, 0x84 | 0x40, },
  442. { 0x1ffff * 125 + 125/2, 125, 0x84 | 0x60, }, */
  443. };
  444. /* Samsung TBMU24112 DVB-S NIM with SL1935 zero-IF tuner */
  445. static const struct dvb_pll_desc dvb_pll_samsung_tbmu24112 = {
  446. .name = "Samsung TBMU24112",
  447. .min = 950 * MHz,
  448. .max = 2150 * MHz, /* guesses */
  449. .iffreq = 0,
  450. .count = 2,
  451. .entries = {
  452. { 1500000, 125, 0x84, 0x18 },
  453. { 9999999, 125, 0x84, 0x08 },
  454. }
  455. };
  456. /* Alps TDEE4 DVB-C NIM, used on Cablestar 2 */
  457. /* byte 4 : 1 * * AGD R3 R2 R1 R0
  458. * byte 5 : C1 * RE RTS BS4 BS3 BS2 BS1
  459. * AGD = 1, R3 R2 R1 R0 = 0 1 0 1 => byte 4 = 1**10101 = 0x95
  460. * Range(MHz) C1 * RE RTS BS4 BS3 BS2 BS1 Byte 5
  461. * 47 - 153 0 * 0 0 0 0 0 1 0x01
  462. * 153 - 430 0 * 0 0 0 0 1 0 0x02
  463. * 430 - 822 0 * 0 0 1 0 0 0 0x08
  464. * 822 - 862 1 * 0 0 1 0 0 0 0x88 */
  465. static const struct dvb_pll_desc dvb_pll_alps_tdee4 = {
  466. .name = "ALPS TDEE4",
  467. .min = 47 * MHz,
  468. .max = 862 * MHz,
  469. .iffreq = 36125000,
  470. .count = 4,
  471. .entries = {
  472. { 153000000, 62500, 0x95, 0x01 },
  473. { 430000000, 62500, 0x95, 0x02 },
  474. { 822000000, 62500, 0x95, 0x08 },
  475. { 999999999, 62500, 0x95, 0x88 },
  476. }
  477. };
  478. /* Infineon TUA6034 ISDB-T, used in Friio */
  479. /* CP cur. 50uA, AGC takeover: 103dBuV, PORT3 on */
  480. static const struct dvb_pll_desc dvb_pll_tua6034_friio = {
  481. .name = "Infineon TUA6034 ISDB-T (Friio)",
  482. .min = 90 * MHz,
  483. .max = 770 * MHz,
  484. .iffreq = 57000000,
  485. .initdata = (u8[]){ 4, 0x9a, 0x50, 0xb2, 0x08 },
  486. .sleepdata = (u8[]){ 4, 0x9a, 0x70, 0xb3, 0x0b },
  487. .count = 3,
  488. .entries = {
  489. { 170000000, 142857, 0xba, 0x09 },
  490. { 470000000, 142857, 0xba, 0x0a },
  491. { 770000000, 142857, 0xb2, 0x08 },
  492. }
  493. };
  494. /* Philips TDA6651 ISDB-T, used in Earthsoft PT1 */
  495. static const struct dvb_pll_desc dvb_pll_tda665x_earth_pt1 = {
  496. .name = "Philips TDA6651 ISDB-T (EarthSoft PT1)",
  497. .min = 90 * MHz,
  498. .max = 770 * MHz,
  499. .iffreq = 57000000,
  500. .initdata = (u8[]){ 5, 0x0e, 0x7f, 0xc1, 0x80, 0x80 },
  501. .count = 10,
  502. .entries = {
  503. { 140000000, 142857, 0xc1, 0x81 },
  504. { 170000000, 142857, 0xc1, 0xa1 },
  505. { 220000000, 142857, 0xc1, 0x62 },
  506. { 330000000, 142857, 0xc1, 0xa2 },
  507. { 402000000, 142857, 0xc1, 0xe2 },
  508. { 450000000, 142857, 0xc1, 0x64 },
  509. { 550000000, 142857, 0xc1, 0x84 },
  510. { 600000000, 142857, 0xc1, 0xa4 },
  511. { 700000000, 142857, 0xc1, 0xc4 },
  512. { 770000000, 142857, 0xc1, 0xe4 },
  513. }
  514. };
  515. /* ----------------------------------------------------------- */
  516. static const struct dvb_pll_desc *pll_list[] = {
  517. [DVB_PLL_UNDEFINED] = NULL,
  518. [DVB_PLL_THOMSON_DTT7579] = &dvb_pll_thomson_dtt7579,
  519. [DVB_PLL_THOMSON_DTT759X] = &dvb_pll_thomson_dtt759x,
  520. [DVB_PLL_THOMSON_DTT7520X] = &dvb_pll_thomson_dtt7520x,
  521. [DVB_PLL_LG_Z201] = &dvb_pll_lg_z201,
  522. [DVB_PLL_UNKNOWN_1] = &dvb_pll_unknown_1,
  523. [DVB_PLL_TUA6010XS] = &dvb_pll_tua6010xs,
  524. [DVB_PLL_ENV57H1XD5] = &dvb_pll_env57h1xd5,
  525. [DVB_PLL_TUA6034] = &dvb_pll_tua6034,
  526. [DVB_PLL_TDA665X] = &dvb_pll_tda665x,
  527. [DVB_PLL_TDED4] = &dvb_pll_tded4,
  528. [DVB_PLL_TDEE4] = &dvb_pll_alps_tdee4,
  529. [DVB_PLL_TDHU2] = &dvb_pll_tdhu2,
  530. [DVB_PLL_SAMSUNG_TBMV] = &dvb_pll_samsung_tbmv,
  531. [DVB_PLL_PHILIPS_SD1878_TDA8261] = &dvb_pll_philips_sd1878_tda8261,
  532. [DVB_PLL_OPERA1] = &dvb_pll_opera1,
  533. [DVB_PLL_SAMSUNG_DTOS403IH102A] = &dvb_pll_samsung_dtos403ih102a,
  534. [DVB_PLL_SAMSUNG_TDTC9251DH0] = &dvb_pll_samsung_tdtc9251dh0,
  535. [DVB_PLL_SAMSUNG_TBDU18132] = &dvb_pll_samsung_tbdu18132,
  536. [DVB_PLL_SAMSUNG_TBMU24112] = &dvb_pll_samsung_tbmu24112,
  537. [DVB_PLL_TUA6034_FRIIO] = &dvb_pll_tua6034_friio,
  538. [DVB_PLL_TDA665X_EARTH_PT1] = &dvb_pll_tda665x_earth_pt1,
  539. };
  540. /* ----------------------------------------------------------- */
  541. /* code */
  542. static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
  543. const u32 frequency)
  544. {
  545. struct dvb_pll_priv *priv = fe->tuner_priv;
  546. const struct dvb_pll_desc *desc = priv->pll_desc;
  547. u32 div;
  548. int i;
  549. for (i = 0; i < desc->count; i++) {
  550. if (frequency > desc->entries[i].limit)
  551. continue;
  552. break;
  553. }
  554. if (debug)
  555. dprintk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
  556. frequency, i, desc->count);
  557. if (i == desc->count)
  558. return -EINVAL;
  559. div = (frequency + desc->iffreq +
  560. desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
  561. buf[0] = div >> 8;
  562. buf[1] = div & 0xff;
  563. buf[2] = desc->entries[i].config;
  564. buf[3] = desc->entries[i].cb;
  565. if (desc->set)
  566. desc->set(fe, buf);
  567. if (debug)
  568. dprintk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  569. desc->name, div, buf[0], buf[1], buf[2], buf[3]);
  570. // calculate the frequency we set it to
  571. return (div * desc->entries[i].stepsize) - desc->iffreq;
  572. }
  573. static void dvb_pll_release(struct dvb_frontend *fe)
  574. {
  575. kfree(fe->tuner_priv);
  576. fe->tuner_priv = NULL;
  577. }
  578. static int dvb_pll_sleep(struct dvb_frontend *fe)
  579. {
  580. struct dvb_pll_priv *priv = fe->tuner_priv;
  581. if (priv->i2c == NULL)
  582. return -EINVAL;
  583. if (priv->pll_desc->sleepdata) {
  584. struct i2c_msg msg = { .flags = 0,
  585. .addr = priv->pll_i2c_address,
  586. .buf = priv->pll_desc->sleepdata + 1,
  587. .len = priv->pll_desc->sleepdata[0] };
  588. int 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. return 0;
  595. }
  596. /* Shouldn't be called when initdata is NULL, maybe BUG()? */
  597. return -EINVAL;
  598. }
  599. static int dvb_pll_set_params(struct dvb_frontend *fe)
  600. {
  601. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  602. struct dvb_pll_priv *priv = fe->tuner_priv;
  603. u8 buf[4];
  604. struct i2c_msg msg =
  605. { .addr = priv->pll_i2c_address, .flags = 0,
  606. .buf = buf, .len = sizeof(buf) };
  607. int result;
  608. u32 frequency = 0;
  609. if (priv->i2c == NULL)
  610. return -EINVAL;
  611. result = dvb_pll_configure(fe, buf, c->frequency);
  612. if (result < 0)
  613. return result;
  614. else
  615. frequency = result;
  616. if (fe->ops.i2c_gate_ctrl)
  617. fe->ops.i2c_gate_ctrl(fe, 1);
  618. if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
  619. return result;
  620. }
  621. priv->frequency = frequency;
  622. priv->bandwidth = c->bandwidth_hz;
  623. return 0;
  624. }
  625. static int dvb_pll_calc_regs(struct dvb_frontend *fe,
  626. u8 *buf, int buf_len)
  627. {
  628. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  629. struct dvb_pll_priv *priv = fe->tuner_priv;
  630. int result;
  631. u32 frequency = 0;
  632. if (buf_len < 5)
  633. return -EINVAL;
  634. result = dvb_pll_configure(fe, buf + 1, c->frequency);
  635. if (result < 0)
  636. return result;
  637. else
  638. frequency = result;
  639. buf[0] = priv->pll_i2c_address;
  640. priv->frequency = frequency;
  641. priv->bandwidth = c->bandwidth_hz;
  642. return 5;
  643. }
  644. static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  645. {
  646. struct dvb_pll_priv *priv = fe->tuner_priv;
  647. *frequency = priv->frequency;
  648. return 0;
  649. }
  650. static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  651. {
  652. struct dvb_pll_priv *priv = fe->tuner_priv;
  653. *bandwidth = priv->bandwidth;
  654. return 0;
  655. }
  656. static int dvb_pll_init(struct dvb_frontend *fe)
  657. {
  658. struct dvb_pll_priv *priv = fe->tuner_priv;
  659. if (priv->i2c == NULL)
  660. return -EINVAL;
  661. if (priv->pll_desc->initdata) {
  662. struct i2c_msg msg = { .flags = 0,
  663. .addr = priv->pll_i2c_address,
  664. .buf = priv->pll_desc->initdata + 1,
  665. .len = priv->pll_desc->initdata[0] };
  666. int result;
  667. if (fe->ops.i2c_gate_ctrl)
  668. fe->ops.i2c_gate_ctrl(fe, 1);
  669. result = i2c_transfer(priv->i2c, &msg, 1);
  670. if (result != 1)
  671. return result;
  672. if (priv->pll_desc->initdata2) {
  673. msg.buf = priv->pll_desc->initdata2 + 1;
  674. msg.len = priv->pll_desc->initdata2[0];
  675. if (fe->ops.i2c_gate_ctrl)
  676. fe->ops.i2c_gate_ctrl(fe, 1);
  677. result = i2c_transfer(priv->i2c, &msg, 1);
  678. if (result != 1)
  679. return result;
  680. }
  681. return 0;
  682. }
  683. /* Shouldn't be called when initdata is NULL, maybe BUG()? */
  684. return -EINVAL;
  685. }
  686. static const struct dvb_tuner_ops dvb_pll_tuner_ops = {
  687. .release = dvb_pll_release,
  688. .sleep = dvb_pll_sleep,
  689. .init = dvb_pll_init,
  690. .set_params = dvb_pll_set_params,
  691. .calc_regs = dvb_pll_calc_regs,
  692. .get_frequency = dvb_pll_get_frequency,
  693. .get_bandwidth = dvb_pll_get_bandwidth,
  694. };
  695. struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
  696. struct i2c_adapter *i2c,
  697. unsigned int pll_desc_id)
  698. {
  699. u8 *b1;
  700. struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD, .len = 1 };
  701. struct dvb_pll_priv *priv = NULL;
  702. int ret;
  703. const struct dvb_pll_desc *desc;
  704. int nr;
  705. b1 = kmalloc(1, GFP_KERNEL);
  706. if (!b1)
  707. return NULL;
  708. b1[0] = 0;
  709. msg.buf = b1;
  710. nr = ida_simple_get(&pll_ida, 0, DVB_PLL_MAX, GFP_KERNEL);
  711. if (nr < 0) {
  712. kfree(b1);
  713. return NULL;
  714. }
  715. if (id[nr] > DVB_PLL_UNDEFINED && id[nr] < ARRAY_SIZE(pll_list))
  716. pll_desc_id = id[nr];
  717. BUG_ON(pll_desc_id < 1 || pll_desc_id >= ARRAY_SIZE(pll_list));
  718. desc = pll_list[pll_desc_id];
  719. if (i2c != NULL) {
  720. if (fe->ops.i2c_gate_ctrl)
  721. fe->ops.i2c_gate_ctrl(fe, 1);
  722. ret = i2c_transfer (i2c, &msg, 1);
  723. if (ret != 1)
  724. goto out;
  725. if (fe->ops.i2c_gate_ctrl)
  726. fe->ops.i2c_gate_ctrl(fe, 0);
  727. }
  728. priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
  729. if (!priv)
  730. goto out;
  731. priv->pll_i2c_address = pll_addr;
  732. priv->i2c = i2c;
  733. priv->pll_desc = desc;
  734. priv->nr = nr;
  735. memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
  736. sizeof(struct dvb_tuner_ops));
  737. strscpy(fe->ops.tuner_ops.info.name, desc->name,
  738. sizeof(fe->ops.tuner_ops.info.name));
  739. fe->ops.tuner_ops.info.frequency_min_hz = desc->min;
  740. fe->ops.tuner_ops.info.frequency_max_hz = desc->max;
  741. dprintk("%s tuner, frequency range: %u...%u\n",
  742. desc->name, desc->min, desc->max);
  743. if (!desc->initdata)
  744. fe->ops.tuner_ops.init = NULL;
  745. if (!desc->sleepdata)
  746. fe->ops.tuner_ops.sleep = NULL;
  747. fe->tuner_priv = priv;
  748. if ((debug) || (id[priv->nr] == pll_desc_id)) {
  749. dprintk("dvb-pll[%d]", priv->nr);
  750. if (i2c != NULL)
  751. pr_cont(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
  752. pr_cont(": id# %d (%s) attached, %s\n", pll_desc_id, desc->name,
  753. id[priv->nr] == pll_desc_id ?
  754. "insmod option" : "autodetected");
  755. }
  756. kfree(b1);
  757. return fe;
  758. out:
  759. kfree(b1);
  760. ida_simple_remove(&pll_ida, nr);
  761. return NULL;
  762. }
  763. EXPORT_SYMBOL(dvb_pll_attach);
  764. static int
  765. dvb_pll_probe(struct i2c_client *client, const struct i2c_device_id *id)
  766. {
  767. struct dvb_pll_config *cfg;
  768. struct dvb_frontend *fe;
  769. unsigned int desc_id;
  770. cfg = client->dev.platform_data;
  771. fe = cfg->fe;
  772. i2c_set_clientdata(client, fe);
  773. desc_id = (unsigned int) id->driver_data;
  774. if (!dvb_pll_attach(fe, client->addr, client->adapter, desc_id))
  775. return -ENOMEM;
  776. /*
  777. * Unset tuner_ops.release (== dvb_pll_release)
  778. * which has been just set in the above dvb_pll_attach(),
  779. * because if tuner_ops.release was left defined,
  780. * this module would be 'put' twice on exit:
  781. * once by dvb_frontend_detach() and another by dvb_module_release().
  782. *
  783. * dvb_pll_release is instead executed in the i2c driver's .remove(),
  784. * keeping dvb_pll_attach untouched for legacy (dvb_attach) drivers.
  785. */
  786. fe->ops.tuner_ops.release = NULL;
  787. dev_info(&client->dev, "DVB Simple Tuner attached.\n");
  788. return 0;
  789. }
  790. static int dvb_pll_remove(struct i2c_client *client)
  791. {
  792. struct dvb_frontend *fe = i2c_get_clientdata(client);
  793. struct dvb_pll_priv *priv = fe->tuner_priv;
  794. ida_simple_remove(&pll_ida, priv->nr);
  795. dvb_pll_release(fe);
  796. return 0;
  797. }
  798. static const struct i2c_device_id dvb_pll_id[] = {
  799. {"dtt7579", DVB_PLL_THOMSON_DTT7579},
  800. {"dtt759x", DVB_PLL_THOMSON_DTT759X},
  801. {"z201", DVB_PLL_LG_Z201},
  802. {"unknown_1", DVB_PLL_UNKNOWN_1},
  803. {"tua6010xs", DVB_PLL_TUA6010XS},
  804. {"env57h1xd5", DVB_PLL_ENV57H1XD5},
  805. {"tua6034", DVB_PLL_TUA6034},
  806. {"tda665x", DVB_PLL_TDA665X},
  807. {"tded4", DVB_PLL_TDED4},
  808. {"tdhu2", DVB_PLL_TDHU2},
  809. {"tbmv", DVB_PLL_SAMSUNG_TBMV},
  810. {"sd1878_tda8261", DVB_PLL_PHILIPS_SD1878_TDA8261},
  811. {"opera1", DVB_PLL_OPERA1},
  812. {"dtos403ih102a", DVB_PLL_SAMSUNG_DTOS403IH102A},
  813. {"tdtc9251dh0", DVB_PLL_SAMSUNG_TDTC9251DH0},
  814. {"tbdu18132", DVB_PLL_SAMSUNG_TBDU18132},
  815. {"tbmu24112", DVB_PLL_SAMSUNG_TBMU24112},
  816. {"tdee4", DVB_PLL_TDEE4},
  817. {"dtt7520x", DVB_PLL_THOMSON_DTT7520X},
  818. {"tua6034_friio", DVB_PLL_TUA6034_FRIIO},
  819. {"tda665x_earthpt1", DVB_PLL_TDA665X_EARTH_PT1},
  820. {}
  821. };
  822. MODULE_DEVICE_TABLE(i2c, dvb_pll_id);
  823. static struct i2c_driver dvb_pll_driver = {
  824. .driver = {
  825. .name = "dvb_pll",
  826. },
  827. .probe = dvb_pll_probe,
  828. .remove = dvb_pll_remove,
  829. .id_table = dvb_pll_id,
  830. };
  831. module_i2c_driver(dvb_pll_driver);
  832. MODULE_DESCRIPTION("dvb pll library");
  833. MODULE_AUTHOR("Gerd Knorr");
  834. MODULE_LICENSE("GPL");