tda18271-fe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
  3. Copyright (C) 2007 Michael Krufky (mkrufky@linuxtv.org)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/videodev2.h>
  18. #include "tda18271.h"
  19. #include "tda18271-priv.h"
  20. int tda18271_debug;
  21. module_param_named(debug, tda18271_debug, int, 0644);
  22. MODULE_PARM_DESC(debug, "set debug level (info=1, map=2, reg=4 (or-able))");
  23. /*---------------------------------------------------------------------*/
  24. enum tda18271_mode {
  25. TDA18271_ANALOG,
  26. TDA18271_DIGITAL,
  27. };
  28. struct tda18271_priv {
  29. u8 i2c_addr;
  30. struct i2c_adapter *i2c_adap;
  31. unsigned char tda18271_regs[TDA18271_NUM_REGS];
  32. enum tda18271_mode mode;
  33. enum tda18271_i2c_gate gate;
  34. u32 frequency;
  35. u32 bandwidth;
  36. };
  37. static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  38. {
  39. struct tda18271_priv *priv = fe->tuner_priv;
  40. struct analog_tuner_ops *ops = fe->ops.analog_demod_ops;
  41. enum tda18271_i2c_gate gate;
  42. int ret = 0;
  43. switch (priv->gate) {
  44. case TDA18271_GATE_DIGITAL:
  45. case TDA18271_GATE_ANALOG:
  46. gate = priv->gate;
  47. break;
  48. case TDA18271_GATE_AUTO:
  49. default:
  50. switch (priv->mode) {
  51. case TDA18271_DIGITAL:
  52. gate = TDA18271_GATE_DIGITAL;
  53. break;
  54. case TDA18271_ANALOG:
  55. default:
  56. gate = TDA18271_GATE_ANALOG;
  57. break;
  58. }
  59. }
  60. switch (gate) {
  61. case TDA18271_GATE_ANALOG:
  62. if (ops && ops->i2c_gate_ctrl)
  63. ret = ops->i2c_gate_ctrl(fe, enable);
  64. break;
  65. case TDA18271_GATE_DIGITAL:
  66. if (fe->ops.i2c_gate_ctrl)
  67. ret = fe->ops.i2c_gate_ctrl(fe, enable);
  68. break;
  69. default:
  70. ret = -EINVAL;
  71. break;
  72. }
  73. return ret;
  74. };
  75. /*---------------------------------------------------------------------*/
  76. static void tda18271_dump_regs(struct dvb_frontend *fe)
  77. {
  78. struct tda18271_priv *priv = fe->tuner_priv;
  79. unsigned char *regs = priv->tda18271_regs;
  80. dbg_reg("=== TDA18271 REG DUMP ===\n");
  81. dbg_reg("ID_BYTE = 0x%02x\n", 0xff & regs[R_ID]);
  82. dbg_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs[R_TM]);
  83. dbg_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs[R_PL]);
  84. dbg_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs[R_EP1]);
  85. dbg_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs[R_EP2]);
  86. dbg_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs[R_EP3]);
  87. dbg_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs[R_EP4]);
  88. dbg_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs[R_EP5]);
  89. dbg_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_CPD]);
  90. dbg_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_CD1]);
  91. dbg_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_CD2]);
  92. dbg_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_CD3]);
  93. dbg_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_MPD]);
  94. dbg_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_MD1]);
  95. dbg_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_MD2]);
  96. dbg_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_MD3]);
  97. }
  98. static void tda18271_read_regs(struct dvb_frontend *fe)
  99. {
  100. struct tda18271_priv *priv = fe->tuner_priv;
  101. unsigned char *regs = priv->tda18271_regs;
  102. unsigned char buf = 0x00;
  103. int ret;
  104. struct i2c_msg msg[] = {
  105. { .addr = priv->i2c_addr, .flags = 0,
  106. .buf = &buf, .len = 1 },
  107. { .addr = priv->i2c_addr, .flags = I2C_M_RD,
  108. .buf = regs, .len = 16 }
  109. };
  110. tda18271_i2c_gate_ctrl(fe, 1);
  111. /* read all registers */
  112. ret = i2c_transfer(priv->i2c_adap, msg, 2);
  113. tda18271_i2c_gate_ctrl(fe, 0);
  114. if (ret != 2)
  115. printk("ERROR: %s: i2c_transfer returned: %d\n",
  116. __FUNCTION__, ret);
  117. if (tda18271_debug & DBG_REG)
  118. tda18271_dump_regs(fe);
  119. }
  120. static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
  121. {
  122. struct tda18271_priv *priv = fe->tuner_priv;
  123. unsigned char *regs = priv->tda18271_regs;
  124. unsigned char buf[TDA18271_NUM_REGS+1];
  125. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
  126. .buf = buf, .len = len+1 };
  127. int i, ret;
  128. BUG_ON((len == 0) || (idx+len > sizeof(buf)));
  129. buf[0] = idx;
  130. for (i = 1; i <= len; i++) {
  131. buf[i] = regs[idx-1+i];
  132. }
  133. tda18271_i2c_gate_ctrl(fe, 1);
  134. /* write registers */
  135. ret = i2c_transfer(priv->i2c_adap, &msg, 1);
  136. tda18271_i2c_gate_ctrl(fe, 0);
  137. if (ret != 1)
  138. printk(KERN_WARNING "ERROR: %s: i2c_transfer returned: %d\n",
  139. __FUNCTION__, ret);
  140. }
  141. /*---------------------------------------------------------------------*/
  142. static int tda18271_init_regs(struct dvb_frontend *fe)
  143. {
  144. struct tda18271_priv *priv = fe->tuner_priv;
  145. unsigned char *regs = priv->tda18271_regs;
  146. printk(KERN_INFO "tda18271: initializing registers\n");
  147. /* initialize registers */
  148. regs[R_ID] = 0x83;
  149. regs[R_TM] = 0x08;
  150. regs[R_PL] = 0x80;
  151. regs[R_EP1] = 0xc6;
  152. regs[R_EP2] = 0xdf;
  153. regs[R_EP3] = 0x16;
  154. regs[R_EP4] = 0x60;
  155. regs[R_EP5] = 0x80;
  156. regs[R_CPD] = 0x80;
  157. regs[R_CD1] = 0x00;
  158. regs[R_CD2] = 0x00;
  159. regs[R_CD3] = 0x00;
  160. regs[R_MPD] = 0x00;
  161. regs[R_MD1] = 0x00;
  162. regs[R_MD2] = 0x00;
  163. regs[R_MD3] = 0x00;
  164. regs[R_EB1] = 0xff;
  165. regs[R_EB2] = 0x01;
  166. regs[R_EB3] = 0x84;
  167. regs[R_EB4] = 0x41;
  168. regs[R_EB5] = 0x01;
  169. regs[R_EB6] = 0x84;
  170. regs[R_EB7] = 0x40;
  171. regs[R_EB8] = 0x07;
  172. regs[R_EB9] = 0x00;
  173. regs[R_EB10] = 0x00;
  174. regs[R_EB11] = 0x96;
  175. regs[R_EB12] = 0x0f;
  176. regs[R_EB13] = 0xc1;
  177. regs[R_EB14] = 0x00;
  178. regs[R_EB15] = 0x8f;
  179. regs[R_EB16] = 0x00;
  180. regs[R_EB17] = 0x00;
  181. regs[R_EB18] = 0x00;
  182. regs[R_EB19] = 0x00;
  183. regs[R_EB20] = 0x20;
  184. regs[R_EB21] = 0x33;
  185. regs[R_EB22] = 0x48;
  186. regs[R_EB23] = 0xb0;
  187. tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
  188. /* setup AGC1 & AGC2 */
  189. regs[R_EB17] = 0x00;
  190. tda18271_write_regs(fe, R_EB17, 1);
  191. regs[R_EB17] = 0x03;
  192. tda18271_write_regs(fe, R_EB17, 1);
  193. regs[R_EB17] = 0x43;
  194. tda18271_write_regs(fe, R_EB17, 1);
  195. regs[R_EB17] = 0x4c;
  196. tda18271_write_regs(fe, R_EB17, 1);
  197. regs[R_EB20] = 0xa0;
  198. tda18271_write_regs(fe, R_EB20, 1);
  199. regs[R_EB20] = 0xa7;
  200. tda18271_write_regs(fe, R_EB20, 1);
  201. regs[R_EB20] = 0xe7;
  202. tda18271_write_regs(fe, R_EB20, 1);
  203. regs[R_EB20] = 0xec;
  204. tda18271_write_regs(fe, R_EB20, 1);
  205. /* image rejection calibration */
  206. /* low-band */
  207. regs[R_EP3] = 0x1f;
  208. regs[R_EP4] = 0x66;
  209. regs[R_EP5] = 0x81;
  210. regs[R_CPD] = 0xcc;
  211. regs[R_CD1] = 0x6c;
  212. regs[R_CD2] = 0x00;
  213. regs[R_CD3] = 0x00;
  214. regs[R_MPD] = 0xcd;
  215. regs[R_MD1] = 0x77;
  216. regs[R_MD2] = 0x08;
  217. regs[R_MD3] = 0x00;
  218. tda18271_write_regs(fe, R_EP3, 11);
  219. msleep(5); /* pll locking */
  220. regs[R_EP1] = 0xc6;
  221. tda18271_write_regs(fe, R_EP1, 1);
  222. msleep(5); /* wanted low measurement */
  223. regs[R_EP3] = 0x1f;
  224. regs[R_EP4] = 0x66;
  225. regs[R_EP5] = 0x85;
  226. regs[R_CPD] = 0xcb;
  227. regs[R_CD1] = 0x66;
  228. regs[R_CD2] = 0x70;
  229. regs[R_CD3] = 0x00;
  230. tda18271_write_regs(fe, R_EP3, 7);
  231. msleep(5); /* pll locking */
  232. regs[R_EP2] = 0xdf;
  233. tda18271_write_regs(fe, R_EP2, 1);
  234. msleep(30); /* image low optimization completion */
  235. /* mid-band */
  236. regs[R_EP3] = 0x1f;
  237. regs[R_EP4] = 0x66;
  238. regs[R_EP5] = 0x82;
  239. regs[R_CPD] = 0xa8;
  240. regs[R_CD1] = 0x66;
  241. regs[R_CD2] = 0x00;
  242. regs[R_CD3] = 0x00;
  243. regs[R_MPD] = 0xa9;
  244. regs[R_MD1] = 0x73;
  245. regs[R_MD2] = 0x1a;
  246. regs[R_MD3] = 0x00;
  247. tda18271_write_regs(fe, R_EP3, 11);
  248. msleep(5); /* pll locking */
  249. regs[R_EP1] = 0xc6;
  250. tda18271_write_regs(fe, R_EP1, 1);
  251. msleep(5); /* wanted mid measurement */
  252. regs[R_EP3] = 0x1f;
  253. regs[R_EP4] = 0x66;
  254. regs[R_EP5] = 0x86;
  255. regs[R_CPD] = 0xa8;
  256. regs[R_CD1] = 0x66;
  257. regs[R_CD2] = 0xa0;
  258. regs[R_CD3] = 0x00;
  259. tda18271_write_regs(fe, R_EP3, 7);
  260. msleep(5); /* pll locking */
  261. regs[R_EP2] = 0xdf;
  262. tda18271_write_regs(fe, R_EP2, 1);
  263. msleep(30); /* image mid optimization completion */
  264. /* high-band */
  265. regs[R_EP3] = 0x1f;
  266. regs[R_EP4] = 0x66;
  267. regs[R_EP5] = 0x83;
  268. regs[R_CPD] = 0x98;
  269. regs[R_CD1] = 0x65;
  270. regs[R_CD2] = 0x00;
  271. regs[R_CD3] = 0x00;
  272. regs[R_MPD] = 0x99;
  273. regs[R_MD1] = 0x71;
  274. regs[R_MD2] = 0xcd;
  275. regs[R_MD3] = 0x00;
  276. tda18271_write_regs(fe, R_EP3, 11);
  277. msleep(5); /* pll locking */
  278. regs[R_EP1] = 0xc6;
  279. tda18271_write_regs(fe, R_EP1, 1);
  280. msleep(5); /* wanted high measurement */
  281. regs[R_EP3] = 0x1f;
  282. regs[R_EP4] = 0x66;
  283. regs[R_EP5] = 0x87;
  284. regs[R_CPD] = 0x98;
  285. regs[R_CD1] = 0x65;
  286. regs[R_CD2] = 0x50;
  287. regs[R_CD3] = 0x00;
  288. tda18271_write_regs(fe, R_EP3, 7);
  289. msleep(5); /* pll locking */
  290. regs[R_EP2] = 0xdf;
  291. tda18271_write_regs(fe, R_EP2, 1);
  292. msleep(30); /* image high optimization completion */
  293. regs[R_EP4] = 0x64;
  294. tda18271_write_regs(fe, R_EP4, 1);
  295. regs[R_EP1] = 0xc6;
  296. tda18271_write_regs(fe, R_EP1, 1);
  297. return 0;
  298. }
  299. static int tda18271_init(struct dvb_frontend *fe)
  300. {
  301. struct tda18271_priv *priv = fe->tuner_priv;
  302. unsigned char *regs = priv->tda18271_regs;
  303. tda18271_read_regs(fe);
  304. /* test IR_CAL_OK to see if we need init */
  305. if ((regs[R_EP1] & 0x08) == 0)
  306. tda18271_init_regs(fe);
  307. return 0;
  308. }
  309. static int tda18271_tune(struct dvb_frontend *fe,
  310. u32 ifc, u32 freq, u32 bw, u8 std)
  311. {
  312. struct tda18271_priv *priv = fe->tuner_priv;
  313. unsigned char *regs = priv->tda18271_regs;
  314. u32 div, N = 0;
  315. u8 d, pd, val;
  316. tda18271_init(fe);
  317. dbg_info("freq = %d, ifc = %d\n", freq, ifc);
  318. /* RF tracking filter calibration */
  319. /* calculate BP_Filter */
  320. tda18271_calc_bp_filter(&freq, &val);
  321. regs[R_EP1] &= ~0x07; /* clear bp filter bits */
  322. regs[R_EP1] |= val;
  323. tda18271_write_regs(fe, R_EP1, 1);
  324. regs[R_EB4] &= 0x07;
  325. regs[R_EB4] |= 0x60;
  326. tda18271_write_regs(fe, R_EB4, 1);
  327. regs[R_EB7] = 0x60;
  328. tda18271_write_regs(fe, R_EB7, 1);
  329. regs[R_EB14] = 0x00;
  330. tda18271_write_regs(fe, R_EB14, 1);
  331. regs[R_EB20] = 0xcc;
  332. tda18271_write_regs(fe, R_EB20, 1);
  333. /* set CAL mode to RF tracking filter calibration */
  334. regs[R_EB4] |= 0x03;
  335. /* calculate CAL PLL */
  336. switch (priv->mode) {
  337. case TDA18271_ANALOG:
  338. N = freq - 1250000;
  339. break;
  340. case TDA18271_DIGITAL:
  341. N = freq + bw / 2;
  342. break;
  343. }
  344. tda18271_calc_cal_pll(&N, &pd, &d);
  345. regs[R_CPD] = pd;
  346. div = ((d * (N / 1000)) << 7) / 125;
  347. regs[R_CD1] = 0xff & (div >> 16);
  348. regs[R_CD2] = 0xff & (div >> 8);
  349. regs[R_CD3] = 0xff & div;
  350. /* calculate MAIN PLL */
  351. switch (priv->mode) {
  352. case TDA18271_ANALOG:
  353. N = freq - 250000;
  354. break;
  355. case TDA18271_DIGITAL:
  356. N = freq + bw / 2 + 1000000;
  357. break;
  358. }
  359. tda18271_calc_main_pll(&N, &pd, &d);
  360. regs[R_MPD] = (0x7f & pd);
  361. switch (priv->mode) {
  362. case TDA18271_ANALOG:
  363. regs[R_MPD] &= ~0x08;
  364. break;
  365. case TDA18271_DIGITAL:
  366. regs[R_MPD] |= 0x08;
  367. break;
  368. }
  369. div = ((d * (N / 1000)) << 7) / 125;
  370. regs[R_MD1] = 0xff & (div >> 16);
  371. regs[R_MD2] = 0xff & (div >> 8);
  372. regs[R_MD3] = 0xff & div;
  373. tda18271_write_regs(fe, R_EP3, 11);
  374. msleep(5); /* RF tracking filter calibration initialization */
  375. /* search for K,M,CO for RF Calibration */
  376. tda18271_calc_km(&freq, &val);
  377. regs[R_EB13] &= 0x83;
  378. regs[R_EB13] |= val;
  379. tda18271_write_regs(fe, R_EB13, 1);
  380. /* search for RF_BAND */
  381. tda18271_calc_rf_band(&freq, &val);
  382. regs[R_EP2] &= ~0xe0; /* clear rf band bits */
  383. regs[R_EP2] |= (val << 5);
  384. /* search for Gain_Taper */
  385. tda18271_calc_gain_taper(&freq, &val);
  386. regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
  387. regs[R_EP2] |= val;
  388. tda18271_write_regs(fe, R_EP2, 1);
  389. tda18271_write_regs(fe, R_EP1, 1);
  390. tda18271_write_regs(fe, R_EP2, 1);
  391. tda18271_write_regs(fe, R_EP1, 1);
  392. regs[R_EB4] &= 0x07;
  393. regs[R_EB4] |= 0x40;
  394. tda18271_write_regs(fe, R_EB4, 1);
  395. regs[R_EB7] = 0x40;
  396. tda18271_write_regs(fe, R_EB7, 1);
  397. msleep(10);
  398. regs[R_EB20] = 0xec;
  399. tda18271_write_regs(fe, R_EB20, 1);
  400. msleep(60); /* RF tracking filter calibration completion */
  401. regs[R_EP4] &= ~0x03; /* set cal mode to normal */
  402. tda18271_write_regs(fe, R_EP4, 1);
  403. tda18271_write_regs(fe, R_EP1, 1);
  404. /* RF tracking filer correction for VHF_Low band */
  405. tda18271_calc_rf_cal(&freq, &val);
  406. /* VHF_Low band only */
  407. if (val != 0) {
  408. regs[R_EB14] = val;
  409. tda18271_write_regs(fe, R_EB14, 1);
  410. }
  411. /* Channel Configuration */
  412. switch (priv->mode) {
  413. case TDA18271_ANALOG:
  414. regs[R_EB22] = 0x2c;
  415. break;
  416. case TDA18271_DIGITAL:
  417. regs[R_EB22] = 0x37;
  418. break;
  419. }
  420. tda18271_write_regs(fe, R_EB22, 1);
  421. regs[R_EP1] |= 0x40; /* set dis power level on */
  422. /* set standard */
  423. regs[R_EP3] &= ~0x1f; /* clear std bits */
  424. /* see table 22 */
  425. regs[R_EP3] |= std;
  426. regs[R_EP4] &= ~0x03; /* set cal mode to normal */
  427. regs[R_EP4] &= ~0x1c; /* clear if level bits */
  428. switch (priv->mode) {
  429. case TDA18271_ANALOG:
  430. regs[R_MPD] &= ~0x80; /* IF notch = 0 */
  431. break;
  432. case TDA18271_DIGITAL:
  433. regs[R_EP4] |= 0x04;
  434. regs[R_MPD] |= 0x80;
  435. break;
  436. }
  437. regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
  438. /* image rejection validity EP5[2:0] */
  439. tda18271_calc_ir_measure(&freq, &val);
  440. regs[R_EP5] &= ~0x07;
  441. regs[R_EP5] |= val;
  442. /* calculate MAIN PLL */
  443. N = freq + ifc;
  444. tda18271_calc_main_pll(&N, &pd, &d);
  445. regs[R_MPD] = (0x7f & pd);
  446. switch (priv->mode) {
  447. case TDA18271_ANALOG:
  448. regs[R_MPD] &= ~0x08;
  449. break;
  450. case TDA18271_DIGITAL:
  451. regs[R_MPD] |= 0x08;
  452. break;
  453. }
  454. div = ((d * (N / 1000)) << 7) / 125;
  455. regs[R_MD1] = 0xff & (div >> 16);
  456. regs[R_MD2] = 0xff & (div >> 8);
  457. regs[R_MD3] = 0xff & div;
  458. tda18271_write_regs(fe, R_TM, 15);
  459. msleep(5);
  460. return 0;
  461. }
  462. /* ------------------------------------------------------------------ */
  463. static int tda18271_set_params(struct dvb_frontend *fe,
  464. struct dvb_frontend_parameters *params)
  465. {
  466. struct tda18271_priv *priv = fe->tuner_priv;
  467. u8 std;
  468. u32 bw, sgIF = 0;
  469. u32 freq = params->frequency;
  470. priv->mode = TDA18271_DIGITAL;
  471. /* see table 22 */
  472. if (fe->ops.info.type == FE_ATSC) {
  473. switch (params->u.vsb.modulation) {
  474. case VSB_8:
  475. case VSB_16:
  476. std = 0x1b; /* device-specific (spec says 0x1c) */
  477. sgIF = 5380000;
  478. break;
  479. case QAM_64:
  480. case QAM_256:
  481. std = 0x18; /* device-specific (spec says 0x1d) */
  482. sgIF = 4000000;
  483. break;
  484. default:
  485. printk(KERN_WARNING "%s: modulation not set!\n",
  486. __FUNCTION__);
  487. return -EINVAL;
  488. }
  489. #if 0
  490. /* userspace request is already center adjusted */
  491. freq += 1750000; /* Adjust to center (+1.75MHZ) */
  492. #endif
  493. bw = 6000000;
  494. } else if (fe->ops.info.type == FE_OFDM) {
  495. switch (params->u.ofdm.bandwidth) {
  496. case BANDWIDTH_6_MHZ:
  497. std = 0x1b; /* device-specific (spec says 0x1c) */
  498. bw = 6000000;
  499. sgIF = 3300000;
  500. break;
  501. case BANDWIDTH_7_MHZ:
  502. std = 0x19; /* device-specific (spec says 0x1d) */
  503. bw = 7000000;
  504. sgIF = 3800000;
  505. break;
  506. case BANDWIDTH_8_MHZ:
  507. std = 0x1a; /* device-specific (spec says 0x1e) */
  508. bw = 8000000;
  509. sgIF = 4300000;
  510. break;
  511. default:
  512. printk(KERN_WARNING "%s: bandwidth not set!\n",
  513. __FUNCTION__);
  514. return -EINVAL;
  515. }
  516. } else {
  517. printk(KERN_WARNING "%s: modulation type not supported!\n",
  518. __FUNCTION__);
  519. return -EINVAL;
  520. }
  521. return tda18271_tune(fe, sgIF, freq, bw, std);
  522. }
  523. static int tda18271_set_analog_params(struct dvb_frontend *fe,
  524. struct analog_parameters *params)
  525. {
  526. struct tda18271_priv *priv = fe->tuner_priv;
  527. u8 std;
  528. unsigned int sgIF;
  529. char *mode;
  530. priv->mode = TDA18271_ANALOG;
  531. /* see table 22 */
  532. if (params->std & V4L2_STD_MN) {
  533. std = 0x0d;
  534. sgIF = 92;
  535. mode = "MN";
  536. } else if (params->std & V4L2_STD_B) {
  537. std = 0x0e;
  538. sgIF = 108;
  539. mode = "B";
  540. } else if (params->std & V4L2_STD_GH) {
  541. std = 0x0f;
  542. sgIF = 124;
  543. mode = "GH";
  544. } else if (params->std & V4L2_STD_PAL_I) {
  545. std = 0x0f;
  546. sgIF = 124;
  547. mode = "I";
  548. } else if (params->std & V4L2_STD_DK) {
  549. std = 0x0f;
  550. sgIF = 124;
  551. mode = "DK";
  552. } else if (params->std & V4L2_STD_SECAM_L) {
  553. std = 0x0f;
  554. sgIF = 124;
  555. mode = "L";
  556. } else if (params->std & V4L2_STD_SECAM_LC) {
  557. std = 0x0f;
  558. sgIF = 20;
  559. mode = "LC";
  560. } else {
  561. std = 0x0f;
  562. sgIF = 124;
  563. mode = "xx";
  564. }
  565. if (params->mode == V4L2_TUNER_RADIO)
  566. sgIF = 88; /* if frequency is 5.5 MHz */
  567. dbg_info("setting tda18271 to system %s\n", mode);
  568. return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500,
  569. 0, std);
  570. }
  571. static int tda18271_release(struct dvb_frontend *fe)
  572. {
  573. kfree(fe->tuner_priv);
  574. fe->tuner_priv = NULL;
  575. return 0;
  576. }
  577. static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  578. {
  579. struct tda18271_priv *priv = fe->tuner_priv;
  580. *frequency = priv->frequency;
  581. return 0;
  582. }
  583. static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  584. {
  585. struct tda18271_priv *priv = fe->tuner_priv;
  586. *bandwidth = priv->bandwidth;
  587. return 0;
  588. }
  589. static struct dvb_tuner_ops tda18271_tuner_ops = {
  590. .info = {
  591. .name = "NXP TDA18271HD",
  592. .frequency_min = 45000000,
  593. .frequency_max = 864000000,
  594. .frequency_step = 62500
  595. },
  596. .init = tda18271_init,
  597. .set_params = tda18271_set_params,
  598. .set_analog_params = tda18271_set_analog_params,
  599. .release = tda18271_release,
  600. .get_frequency = tda18271_get_frequency,
  601. .get_bandwidth = tda18271_get_bandwidth,
  602. };
  603. struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
  604. struct i2c_adapter *i2c,
  605. enum tda18271_i2c_gate gate)
  606. {
  607. struct tda18271_priv *priv = NULL;
  608. dbg_info("@ %d-%04x\n", i2c_adapter_id(i2c), addr);
  609. priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
  610. if (priv == NULL)
  611. return NULL;
  612. priv->i2c_addr = addr;
  613. priv->i2c_adap = i2c;
  614. priv->gate = gate;
  615. memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
  616. sizeof(struct dvb_tuner_ops));
  617. fe->tuner_priv = priv;
  618. tda18271_init_regs(fe);
  619. return fe;
  620. }
  621. EXPORT_SYMBOL_GPL(tda18271_attach);
  622. MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
  623. MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
  624. MODULE_LICENSE("GPL");
  625. /*
  626. * Overrides for Emacs so that we follow Linus's tabbing style.
  627. * ---------------------------------------------------------------------------
  628. * Local variables:
  629. * c-basic-offset: 8
  630. * End:
  631. */