tda18271-fe.c 18 KB

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