tda18271-fe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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_calc_main_pll(struct dvb_frontend *fe, u32 freq)
  309. {
  310. /* Sets Main Post-Divider & Divider bytes, but does not write them */
  311. struct tda18271_priv *priv = fe->tuner_priv;
  312. unsigned char *regs = priv->tda18271_regs;
  313. u8 d, pd;
  314. u32 div;
  315. tda18271_lookup_main_pll(&freq, &pd, &d);
  316. regs[R_MPD] = (0x77 & pd);
  317. switch (priv->mode) {
  318. case TDA18271_ANALOG:
  319. regs[R_MPD] &= ~0x08;
  320. break;
  321. case TDA18271_DIGITAL:
  322. regs[R_MPD] |= 0x08;
  323. break;
  324. }
  325. div = ((d * (freq / 1000)) << 7) / 125;
  326. regs[R_MD1] = 0x7f & (div >> 16);
  327. regs[R_MD2] = 0xff & (div >> 8);
  328. regs[R_MD3] = 0xff & div;
  329. return 0;
  330. }
  331. static int tda18271_calc_cal_pll(struct dvb_frontend *fe, u32 freq)
  332. {
  333. /* Sets Cal Post-Divider & Divider bytes, but does not write them */
  334. struct tda18271_priv *priv = fe->tuner_priv;
  335. unsigned char *regs = priv->tda18271_regs;
  336. u8 d, pd;
  337. u32 div;
  338. tda18271_lookup_cal_pll(&freq, &pd, &d);
  339. regs[R_CPD] = pd;
  340. div = ((d * (freq / 1000)) << 7) / 125;
  341. regs[R_CD1] = 0x7f & (div >> 16);
  342. regs[R_CD2] = 0xff & (div >> 8);
  343. regs[R_CD3] = 0xff & div;
  344. return 0;
  345. }
  346. static int tda18271_tune(struct dvb_frontend *fe,
  347. u32 ifc, u32 freq, u32 bw, u8 std)
  348. {
  349. struct tda18271_priv *priv = fe->tuner_priv;
  350. unsigned char *regs = priv->tda18271_regs;
  351. u32 N = 0;
  352. u8 val;
  353. tda18271_init(fe);
  354. dbg_info("freq = %d, ifc = %d\n", freq, ifc);
  355. /* RF tracking filter calibration */
  356. /* calculate BP_Filter */
  357. tda18271_lookup_bp_filter(&freq, &val);
  358. regs[R_EP1] &= ~0x07; /* clear bp filter bits */
  359. regs[R_EP1] |= val;
  360. tda18271_write_regs(fe, R_EP1, 1);
  361. regs[R_EB4] &= 0x07;
  362. regs[R_EB4] |= 0x60;
  363. tda18271_write_regs(fe, R_EB4, 1);
  364. regs[R_EB7] = 0x60;
  365. tda18271_write_regs(fe, R_EB7, 1);
  366. regs[R_EB14] = 0x00;
  367. tda18271_write_regs(fe, R_EB14, 1);
  368. regs[R_EB20] = 0xcc;
  369. tda18271_write_regs(fe, R_EB20, 1);
  370. /* set CAL mode to RF tracking filter calibration */
  371. regs[R_EP4] |= 0x03;
  372. /* calculate CAL PLL */
  373. switch (priv->mode) {
  374. case TDA18271_ANALOG:
  375. N = freq - 1250000;
  376. break;
  377. case TDA18271_DIGITAL:
  378. N = freq + bw / 2;
  379. break;
  380. }
  381. tda18271_calc_cal_pll(fe, N);
  382. /* calculate MAIN PLL */
  383. switch (priv->mode) {
  384. case TDA18271_ANALOG:
  385. N = freq - 250000;
  386. break;
  387. case TDA18271_DIGITAL:
  388. N = freq + bw / 2 + 1000000;
  389. break;
  390. }
  391. tda18271_calc_main_pll(fe, N);
  392. tda18271_write_regs(fe, R_EP3, 11);
  393. msleep(5); /* RF tracking filter calibration initialization */
  394. /* search for K,M,CO for RF Calibration */
  395. tda18271_lookup_km(&freq, &val);
  396. regs[R_EB13] &= 0x83;
  397. regs[R_EB13] |= val;
  398. tda18271_write_regs(fe, R_EB13, 1);
  399. /* search for RF_BAND */
  400. tda18271_lookup_rf_band(&freq, &val);
  401. regs[R_EP2] &= ~0xe0; /* clear rf band bits */
  402. regs[R_EP2] |= (val << 5);
  403. /* search for Gain_Taper */
  404. tda18271_lookup_gain_taper(&freq, &val);
  405. regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
  406. regs[R_EP2] |= val;
  407. tda18271_write_regs(fe, R_EP2, 1);
  408. tda18271_write_regs(fe, R_EP1, 1);
  409. tda18271_write_regs(fe, R_EP2, 1);
  410. tda18271_write_regs(fe, R_EP1, 1);
  411. regs[R_EB4] &= 0x07;
  412. regs[R_EB4] |= 0x40;
  413. tda18271_write_regs(fe, R_EB4, 1);
  414. regs[R_EB7] = 0x40;
  415. tda18271_write_regs(fe, R_EB7, 1);
  416. msleep(10);
  417. regs[R_EB20] = 0xec;
  418. tda18271_write_regs(fe, R_EB20, 1);
  419. msleep(60); /* RF tracking filter calibration completion */
  420. regs[R_EP4] &= ~0x03; /* set cal mode to normal */
  421. tda18271_write_regs(fe, R_EP4, 1);
  422. tda18271_write_regs(fe, R_EP1, 1);
  423. /* RF tracking filer correction for VHF_Low band */
  424. tda18271_lookup_rf_cal(&freq, &val);
  425. /* VHF_Low band only */
  426. if (val != 0) {
  427. regs[R_EB14] = val;
  428. tda18271_write_regs(fe, R_EB14, 1);
  429. }
  430. /* Channel Configuration */
  431. switch (priv->mode) {
  432. case TDA18271_ANALOG:
  433. regs[R_EB22] = 0x2c;
  434. break;
  435. case TDA18271_DIGITAL:
  436. regs[R_EB22] = 0x37;
  437. break;
  438. }
  439. tda18271_write_regs(fe, R_EB22, 1);
  440. regs[R_EP1] |= 0x40; /* set dis power level on */
  441. /* set standard */
  442. regs[R_EP3] &= ~0x1f; /* clear std bits */
  443. /* see table 22 */
  444. regs[R_EP3] |= std;
  445. regs[R_EP4] &= ~0x03; /* set cal mode to normal */
  446. regs[R_EP4] &= ~0x1c; /* clear if level bits */
  447. switch (priv->mode) {
  448. case TDA18271_ANALOG:
  449. regs[R_MPD] &= ~0x80; /* IF notch = 0 */
  450. break;
  451. case TDA18271_DIGITAL:
  452. regs[R_EP4] |= 0x04;
  453. regs[R_MPD] |= 0x80;
  454. break;
  455. }
  456. regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
  457. /* image rejection validity EP5[2:0] */
  458. tda18271_lookup_ir_measure(&freq, &val);
  459. regs[R_EP5] &= ~0x07;
  460. regs[R_EP5] |= val;
  461. /* calculate MAIN PLL */
  462. N = freq + ifc;
  463. tda18271_calc_main_pll(fe, N);
  464. tda18271_write_regs(fe, R_TM, 15);
  465. msleep(5);
  466. return 0;
  467. }
  468. /* ------------------------------------------------------------------ */
  469. static int tda18271_set_params(struct dvb_frontend *fe,
  470. struct dvb_frontend_parameters *params)
  471. {
  472. struct tda18271_priv *priv = fe->tuner_priv;
  473. u8 std;
  474. u32 bw, sgIF = 0;
  475. u32 freq = params->frequency;
  476. priv->mode = TDA18271_DIGITAL;
  477. /* see table 22 */
  478. if (fe->ops.info.type == FE_ATSC) {
  479. switch (params->u.vsb.modulation) {
  480. case VSB_8:
  481. case VSB_16:
  482. std = 0x1b; /* device-specific (spec says 0x1c) */
  483. sgIF = 5380000;
  484. break;
  485. case QAM_64:
  486. case QAM_256:
  487. std = 0x18; /* device-specific (spec says 0x1d) */
  488. sgIF = 4000000;
  489. break;
  490. default:
  491. printk(KERN_WARNING "%s: modulation not set!\n",
  492. __FUNCTION__);
  493. return -EINVAL;
  494. }
  495. #if 0
  496. /* userspace request is already center adjusted */
  497. freq += 1750000; /* Adjust to center (+1.75MHZ) */
  498. #endif
  499. bw = 6000000;
  500. } else if (fe->ops.info.type == FE_OFDM) {
  501. switch (params->u.ofdm.bandwidth) {
  502. case BANDWIDTH_6_MHZ:
  503. std = 0x1b; /* device-specific (spec says 0x1c) */
  504. bw = 6000000;
  505. sgIF = 3300000;
  506. break;
  507. case BANDWIDTH_7_MHZ:
  508. std = 0x19; /* device-specific (spec says 0x1d) */
  509. bw = 7000000;
  510. sgIF = 3800000;
  511. break;
  512. case BANDWIDTH_8_MHZ:
  513. std = 0x1a; /* device-specific (spec says 0x1e) */
  514. bw = 8000000;
  515. sgIF = 4300000;
  516. break;
  517. default:
  518. printk(KERN_WARNING "%s: bandwidth not set!\n",
  519. __FUNCTION__);
  520. return -EINVAL;
  521. }
  522. } else {
  523. printk(KERN_WARNING "%s: modulation type not supported!\n",
  524. __FUNCTION__);
  525. return -EINVAL;
  526. }
  527. return tda18271_tune(fe, sgIF, freq, bw, std);
  528. }
  529. static int tda18271_set_analog_params(struct dvb_frontend *fe,
  530. struct analog_parameters *params)
  531. {
  532. struct tda18271_priv *priv = fe->tuner_priv;
  533. u8 std;
  534. unsigned int sgIF;
  535. char *mode;
  536. priv->mode = TDA18271_ANALOG;
  537. /* see table 22 */
  538. if (params->std & V4L2_STD_MN) {
  539. std = 0x0d;
  540. sgIF = 92;
  541. mode = "MN";
  542. } else if (params->std & V4L2_STD_B) {
  543. std = 0x0e;
  544. sgIF = 108;
  545. mode = "B";
  546. } else if (params->std & V4L2_STD_GH) {
  547. std = 0x0f;
  548. sgIF = 124;
  549. mode = "GH";
  550. } else if (params->std & V4L2_STD_PAL_I) {
  551. std = 0x0f;
  552. sgIF = 124;
  553. mode = "I";
  554. } else if (params->std & V4L2_STD_DK) {
  555. std = 0x0f;
  556. sgIF = 124;
  557. mode = "DK";
  558. } else if (params->std & V4L2_STD_SECAM_L) {
  559. std = 0x0f;
  560. sgIF = 124;
  561. mode = "L";
  562. } else if (params->std & V4L2_STD_SECAM_LC) {
  563. std = 0x0f;
  564. sgIF = 20;
  565. mode = "LC";
  566. } else {
  567. std = 0x0f;
  568. sgIF = 124;
  569. mode = "xx";
  570. }
  571. if (params->mode == V4L2_TUNER_RADIO)
  572. sgIF = 88; /* if frequency is 5.5 MHz */
  573. dbg_info("setting tda18271 to system %s\n", mode);
  574. return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500,
  575. 0, std);
  576. }
  577. static int tda18271_release(struct dvb_frontend *fe)
  578. {
  579. kfree(fe->tuner_priv);
  580. fe->tuner_priv = NULL;
  581. return 0;
  582. }
  583. static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  584. {
  585. struct tda18271_priv *priv = fe->tuner_priv;
  586. *frequency = priv->frequency;
  587. return 0;
  588. }
  589. static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  590. {
  591. struct tda18271_priv *priv = fe->tuner_priv;
  592. *bandwidth = priv->bandwidth;
  593. return 0;
  594. }
  595. static int tda18271_get_id(struct dvb_frontend *fe)
  596. {
  597. struct tda18271_priv *priv = fe->tuner_priv;
  598. unsigned char *regs = priv->tda18271_regs;
  599. char *name;
  600. int ret = 0;
  601. tda18271_read_regs(fe);
  602. switch (regs[R_ID] & 0x7f) {
  603. case 3:
  604. name = "TDA18271HD/C1";
  605. break;
  606. case 4:
  607. name = "TDA18271HD/C2";
  608. ret = -EPROTONOSUPPORT;
  609. break;
  610. default:
  611. name = "Unknown device";
  612. ret = -EINVAL;
  613. break;
  614. }
  615. dbg_info("%s detected @ %d-%04x%s\n", name,
  616. i2c_adapter_id(priv->i2c_adap), priv->i2c_addr,
  617. (0 == ret) ? "" : ", device not supported.");
  618. return ret;
  619. }
  620. static struct dvb_tuner_ops tda18271_tuner_ops = {
  621. .info = {
  622. .name = "NXP TDA18271HD",
  623. .frequency_min = 45000000,
  624. .frequency_max = 864000000,
  625. .frequency_step = 62500
  626. },
  627. .init = tda18271_init,
  628. .set_params = tda18271_set_params,
  629. .set_analog_params = tda18271_set_analog_params,
  630. .release = tda18271_release,
  631. .get_frequency = tda18271_get_frequency,
  632. .get_bandwidth = tda18271_get_bandwidth,
  633. };
  634. struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
  635. struct i2c_adapter *i2c,
  636. enum tda18271_i2c_gate gate)
  637. {
  638. struct tda18271_priv *priv = NULL;
  639. priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
  640. if (priv == NULL)
  641. return NULL;
  642. priv->i2c_addr = addr;
  643. priv->i2c_adap = i2c;
  644. priv->gate = gate;
  645. fe->tuner_priv = priv;
  646. if (tda18271_get_id(fe) < 0)
  647. goto fail;
  648. memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
  649. sizeof(struct dvb_tuner_ops));
  650. tda18271_init_regs(fe);
  651. return fe;
  652. fail:
  653. tda18271_release(fe);
  654. return NULL;
  655. }
  656. EXPORT_SYMBOL_GPL(tda18271_attach);
  657. MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
  658. MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
  659. MODULE_LICENSE("GPL");
  660. /*
  661. * Overrides for Emacs so that we follow Linus's tabbing style.
  662. * ---------------------------------------------------------------------------
  663. * Local variables:
  664. * c-basic-offset: 8
  665. * End:
  666. */