tda18271-fe.c 18 KB

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