tda18271-common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. tda18271-common.c - driver for the Philips / NXP TDA18271 silicon tuner
  3. Copyright (C) 2007, 2008 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 "tda18271-priv.h"
  17. static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  18. {
  19. struct tda18271_priv *priv = fe->tuner_priv;
  20. enum tda18271_i2c_gate gate;
  21. int ret = 0;
  22. switch (priv->gate) {
  23. case TDA18271_GATE_DIGITAL:
  24. case TDA18271_GATE_ANALOG:
  25. gate = priv->gate;
  26. break;
  27. case TDA18271_GATE_AUTO:
  28. default:
  29. switch (priv->mode) {
  30. case TDA18271_DIGITAL:
  31. gate = TDA18271_GATE_DIGITAL;
  32. break;
  33. case TDA18271_ANALOG:
  34. default:
  35. gate = TDA18271_GATE_ANALOG;
  36. break;
  37. }
  38. }
  39. switch (gate) {
  40. case TDA18271_GATE_ANALOG:
  41. if (fe->ops.analog_ops.i2c_gate_ctrl)
  42. ret = fe->ops.analog_ops.i2c_gate_ctrl(fe, enable);
  43. break;
  44. case TDA18271_GATE_DIGITAL:
  45. if (fe->ops.i2c_gate_ctrl)
  46. ret = fe->ops.i2c_gate_ctrl(fe, enable);
  47. break;
  48. default:
  49. ret = -EINVAL;
  50. break;
  51. }
  52. return ret;
  53. };
  54. /*---------------------------------------------------------------------*/
  55. static void tda18271_dump_regs(struct dvb_frontend *fe, int extended)
  56. {
  57. struct tda18271_priv *priv = fe->tuner_priv;
  58. unsigned char *regs = priv->tda18271_regs;
  59. tda_reg("=== TDA18271 REG DUMP ===\n");
  60. tda_reg("ID_BYTE = 0x%02x\n", 0xff & regs[R_ID]);
  61. tda_reg("THERMO_BYTE = 0x%02x\n", 0xff & regs[R_TM]);
  62. tda_reg("POWER_LEVEL_BYTE = 0x%02x\n", 0xff & regs[R_PL]);
  63. tda_reg("EASY_PROG_BYTE_1 = 0x%02x\n", 0xff & regs[R_EP1]);
  64. tda_reg("EASY_PROG_BYTE_2 = 0x%02x\n", 0xff & regs[R_EP2]);
  65. tda_reg("EASY_PROG_BYTE_3 = 0x%02x\n", 0xff & regs[R_EP3]);
  66. tda_reg("EASY_PROG_BYTE_4 = 0x%02x\n", 0xff & regs[R_EP4]);
  67. tda_reg("EASY_PROG_BYTE_5 = 0x%02x\n", 0xff & regs[R_EP5]);
  68. tda_reg("CAL_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_CPD]);
  69. tda_reg("CAL_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_CD1]);
  70. tda_reg("CAL_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_CD2]);
  71. tda_reg("CAL_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_CD3]);
  72. tda_reg("MAIN_POST_DIV_BYTE = 0x%02x\n", 0xff & regs[R_MPD]);
  73. tda_reg("MAIN_DIV_BYTE_1 = 0x%02x\n", 0xff & regs[R_MD1]);
  74. tda_reg("MAIN_DIV_BYTE_2 = 0x%02x\n", 0xff & regs[R_MD2]);
  75. tda_reg("MAIN_DIV_BYTE_3 = 0x%02x\n", 0xff & regs[R_MD3]);
  76. /* only dump extended regs if DBG_ADV is set */
  77. if (!(tda18271_debug & DBG_ADV))
  78. return;
  79. /* W indicates write-only registers.
  80. * Register dump for write-only registers shows last value written. */
  81. tda_reg("EXTENDED_BYTE_1 = 0x%02x\n", 0xff & regs[R_EB1]);
  82. tda_reg("EXTENDED_BYTE_2 = 0x%02x\n", 0xff & regs[R_EB2]);
  83. tda_reg("EXTENDED_BYTE_3 = 0x%02x\n", 0xff & regs[R_EB3]);
  84. tda_reg("EXTENDED_BYTE_4 = 0x%02x\n", 0xff & regs[R_EB4]);
  85. tda_reg("EXTENDED_BYTE_5 = 0x%02x\n", 0xff & regs[R_EB5]);
  86. tda_reg("EXTENDED_BYTE_6 = 0x%02x\n", 0xff & regs[R_EB6]);
  87. tda_reg("EXTENDED_BYTE_7 = 0x%02x\n", 0xff & regs[R_EB7]);
  88. tda_reg("EXTENDED_BYTE_8 = 0x%02x\n", 0xff & regs[R_EB8]);
  89. tda_reg("EXTENDED_BYTE_9 W = 0x%02x\n", 0xff & regs[R_EB9]);
  90. tda_reg("EXTENDED_BYTE_10 = 0x%02x\n", 0xff & regs[R_EB10]);
  91. tda_reg("EXTENDED_BYTE_11 = 0x%02x\n", 0xff & regs[R_EB11]);
  92. tda_reg("EXTENDED_BYTE_12 = 0x%02x\n", 0xff & regs[R_EB12]);
  93. tda_reg("EXTENDED_BYTE_13 = 0x%02x\n", 0xff & regs[R_EB13]);
  94. tda_reg("EXTENDED_BYTE_14 = 0x%02x\n", 0xff & regs[R_EB14]);
  95. tda_reg("EXTENDED_BYTE_15 = 0x%02x\n", 0xff & regs[R_EB15]);
  96. tda_reg("EXTENDED_BYTE_16 W = 0x%02x\n", 0xff & regs[R_EB16]);
  97. tda_reg("EXTENDED_BYTE_17 W = 0x%02x\n", 0xff & regs[R_EB17]);
  98. tda_reg("EXTENDED_BYTE_18 = 0x%02x\n", 0xff & regs[R_EB18]);
  99. tda_reg("EXTENDED_BYTE_19 W = 0x%02x\n", 0xff & regs[R_EB19]);
  100. tda_reg("EXTENDED_BYTE_20 W = 0x%02x\n", 0xff & regs[R_EB20]);
  101. tda_reg("EXTENDED_BYTE_21 = 0x%02x\n", 0xff & regs[R_EB21]);
  102. tda_reg("EXTENDED_BYTE_22 = 0x%02x\n", 0xff & regs[R_EB22]);
  103. tda_reg("EXTENDED_BYTE_23 = 0x%02x\n", 0xff & regs[R_EB23]);
  104. }
  105. int tda18271_read_regs(struct dvb_frontend *fe)
  106. {
  107. struct tda18271_priv *priv = fe->tuner_priv;
  108. unsigned char *regs = priv->tda18271_regs;
  109. unsigned char buf = 0x00;
  110. int ret;
  111. struct i2c_msg msg[] = {
  112. { .addr = priv->i2c_props.addr, .flags = 0,
  113. .buf = &buf, .len = 1 },
  114. { .addr = priv->i2c_props.addr, .flags = I2C_M_RD,
  115. .buf = regs, .len = 16 }
  116. };
  117. tda18271_i2c_gate_ctrl(fe, 1);
  118. /* read all registers */
  119. ret = i2c_transfer(priv->i2c_props.adap, msg, 2);
  120. tda18271_i2c_gate_ctrl(fe, 0);
  121. if (ret != 2)
  122. tda_err("ERROR: i2c_transfer returned: %d\n", ret);
  123. if (tda18271_debug & DBG_REG)
  124. tda18271_dump_regs(fe, 0);
  125. return (ret == 2 ? 0 : ret);
  126. }
  127. int tda18271_read_extended(struct dvb_frontend *fe)
  128. {
  129. struct tda18271_priv *priv = fe->tuner_priv;
  130. unsigned char *regs = priv->tda18271_regs;
  131. unsigned char regdump[TDA18271_NUM_REGS];
  132. unsigned char buf = 0x00;
  133. int ret, i;
  134. struct i2c_msg msg[] = {
  135. { .addr = priv->i2c_props.addr, .flags = 0,
  136. .buf = &buf, .len = 1 },
  137. { .addr = priv->i2c_props.addr, .flags = I2C_M_RD,
  138. .buf = regdump, .len = TDA18271_NUM_REGS }
  139. };
  140. tda18271_i2c_gate_ctrl(fe, 1);
  141. /* read all registers */
  142. ret = i2c_transfer(priv->i2c_props.adap, msg, 2);
  143. tda18271_i2c_gate_ctrl(fe, 0);
  144. if (ret != 2)
  145. tda_err("ERROR: i2c_transfer returned: %d\n", ret);
  146. for (i = 0; i < TDA18271_NUM_REGS; i++) {
  147. /* don't update write-only registers */
  148. if ((i != R_EB9) &&
  149. (i != R_EB16) &&
  150. (i != R_EB17) &&
  151. (i != R_EB19) &&
  152. (i != R_EB20))
  153. regs[i] = regdump[i];
  154. }
  155. if (tda18271_debug & DBG_REG)
  156. tda18271_dump_regs(fe, 1);
  157. return (ret == 2 ? 0 : ret);
  158. }
  159. static int __tda18271_write_regs(struct dvb_frontend *fe, int idx, int len,
  160. bool lock_i2c)
  161. {
  162. struct tda18271_priv *priv = fe->tuner_priv;
  163. unsigned char *regs = priv->tda18271_regs;
  164. unsigned char buf[TDA18271_NUM_REGS + 1];
  165. struct i2c_msg msg = { .addr = priv->i2c_props.addr, .flags = 0,
  166. .buf = buf };
  167. int i, ret = 1, max;
  168. BUG_ON((len == 0) || (idx + len > sizeof(buf)));
  169. switch (priv->small_i2c) {
  170. case TDA18271_03_BYTE_CHUNK_INIT:
  171. max = 3;
  172. break;
  173. case TDA18271_08_BYTE_CHUNK_INIT:
  174. max = 8;
  175. break;
  176. case TDA18271_16_BYTE_CHUNK_INIT:
  177. max = 16;
  178. break;
  179. case TDA18271_39_BYTE_CHUNK_INIT:
  180. default:
  181. max = 39;
  182. }
  183. /*
  184. * If lock_i2c is true, it will take the I2C bus for tda18271 private
  185. * usage during the entire write ops, as otherwise, bad things could
  186. * happen.
  187. * During device init, several write operations will happen. So,
  188. * tda18271_init_regs controls the I2C lock directly,
  189. * disabling lock_i2c here.
  190. */
  191. if (lock_i2c) {
  192. tda18271_i2c_gate_ctrl(fe, 1);
  193. i2c_lock_adapter(priv->i2c_props.adap);
  194. }
  195. while (len) {
  196. if (max > len)
  197. max = len;
  198. buf[0] = idx;
  199. for (i = 1; i <= max; i++)
  200. buf[i] = regs[idx - 1 + i];
  201. msg.len = max + 1;
  202. /* write registers */
  203. ret = __i2c_transfer(priv->i2c_props.adap, &msg, 1);
  204. if (ret != 1)
  205. break;
  206. idx += max;
  207. len -= max;
  208. }
  209. if (lock_i2c) {
  210. i2c_unlock_adapter(priv->i2c_props.adap);
  211. tda18271_i2c_gate_ctrl(fe, 0);
  212. }
  213. if (ret != 1)
  214. tda_err("ERROR: idx = 0x%x, len = %d, "
  215. "i2c_transfer returned: %d\n", idx, max, ret);
  216. return (ret == 1 ? 0 : ret);
  217. }
  218. int tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
  219. {
  220. return __tda18271_write_regs(fe, idx, len, true);
  221. }
  222. /*---------------------------------------------------------------------*/
  223. static int __tda18271_charge_pump_source(struct dvb_frontend *fe,
  224. enum tda18271_pll pll, int force,
  225. bool lock_i2c)
  226. {
  227. struct tda18271_priv *priv = fe->tuner_priv;
  228. unsigned char *regs = priv->tda18271_regs;
  229. int r_cp = (pll == TDA18271_CAL_PLL) ? R_EB7 : R_EB4;
  230. regs[r_cp] &= ~0x20;
  231. regs[r_cp] |= ((force & 1) << 5);
  232. return __tda18271_write_regs(fe, r_cp, 1, lock_i2c);
  233. }
  234. int tda18271_charge_pump_source(struct dvb_frontend *fe,
  235. enum tda18271_pll pll, int force)
  236. {
  237. return __tda18271_charge_pump_source(fe, pll, force, true);
  238. }
  239. int tda18271_init_regs(struct dvb_frontend *fe)
  240. {
  241. struct tda18271_priv *priv = fe->tuner_priv;
  242. unsigned char *regs = priv->tda18271_regs;
  243. tda_dbg("initializing registers for device @ %d-%04x\n",
  244. i2c_adapter_id(priv->i2c_props.adap),
  245. priv->i2c_props.addr);
  246. /*
  247. * Don't let any other I2C transfer to happen at adapter during init,
  248. * as those could cause bad things
  249. */
  250. tda18271_i2c_gate_ctrl(fe, 1);
  251. i2c_lock_adapter(priv->i2c_props.adap);
  252. /* initialize registers */
  253. switch (priv->id) {
  254. case TDA18271HDC1:
  255. regs[R_ID] = 0x83;
  256. break;
  257. case TDA18271HDC2:
  258. regs[R_ID] = 0x84;
  259. break;
  260. }
  261. regs[R_TM] = 0x08;
  262. regs[R_PL] = 0x80;
  263. regs[R_EP1] = 0xc6;
  264. regs[R_EP2] = 0xdf;
  265. regs[R_EP3] = 0x16;
  266. regs[R_EP4] = 0x60;
  267. regs[R_EP5] = 0x80;
  268. regs[R_CPD] = 0x80;
  269. regs[R_CD1] = 0x00;
  270. regs[R_CD2] = 0x00;
  271. regs[R_CD3] = 0x00;
  272. regs[R_MPD] = 0x00;
  273. regs[R_MD1] = 0x00;
  274. regs[R_MD2] = 0x00;
  275. regs[R_MD3] = 0x00;
  276. switch (priv->id) {
  277. case TDA18271HDC1:
  278. regs[R_EB1] = 0xff;
  279. break;
  280. case TDA18271HDC2:
  281. regs[R_EB1] = 0xfc;
  282. break;
  283. }
  284. regs[R_EB2] = 0x01;
  285. regs[R_EB3] = 0x84;
  286. regs[R_EB4] = 0x41;
  287. regs[R_EB5] = 0x01;
  288. regs[R_EB6] = 0x84;
  289. regs[R_EB7] = 0x40;
  290. regs[R_EB8] = 0x07;
  291. regs[R_EB9] = 0x00;
  292. regs[R_EB10] = 0x00;
  293. regs[R_EB11] = 0x96;
  294. switch (priv->id) {
  295. case TDA18271HDC1:
  296. regs[R_EB12] = 0x0f;
  297. break;
  298. case TDA18271HDC2:
  299. regs[R_EB12] = 0x33;
  300. break;
  301. }
  302. regs[R_EB13] = 0xc1;
  303. regs[R_EB14] = 0x00;
  304. regs[R_EB15] = 0x8f;
  305. regs[R_EB16] = 0x00;
  306. regs[R_EB17] = 0x00;
  307. switch (priv->id) {
  308. case TDA18271HDC1:
  309. regs[R_EB18] = 0x00;
  310. break;
  311. case TDA18271HDC2:
  312. regs[R_EB18] = 0x8c;
  313. break;
  314. }
  315. regs[R_EB19] = 0x00;
  316. regs[R_EB20] = 0x20;
  317. switch (priv->id) {
  318. case TDA18271HDC1:
  319. regs[R_EB21] = 0x33;
  320. break;
  321. case TDA18271HDC2:
  322. regs[R_EB21] = 0xb3;
  323. break;
  324. }
  325. regs[R_EB22] = 0x48;
  326. regs[R_EB23] = 0xb0;
  327. __tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS, false);
  328. /* setup agc1 gain */
  329. regs[R_EB17] = 0x00;
  330. __tda18271_write_regs(fe, R_EB17, 1, false);
  331. regs[R_EB17] = 0x03;
  332. __tda18271_write_regs(fe, R_EB17, 1, false);
  333. regs[R_EB17] = 0x43;
  334. __tda18271_write_regs(fe, R_EB17, 1, false);
  335. regs[R_EB17] = 0x4c;
  336. __tda18271_write_regs(fe, R_EB17, 1, false);
  337. /* setup agc2 gain */
  338. if ((priv->id) == TDA18271HDC1) {
  339. regs[R_EB20] = 0xa0;
  340. __tda18271_write_regs(fe, R_EB20, 1, false);
  341. regs[R_EB20] = 0xa7;
  342. __tda18271_write_regs(fe, R_EB20, 1, false);
  343. regs[R_EB20] = 0xe7;
  344. __tda18271_write_regs(fe, R_EB20, 1, false);
  345. regs[R_EB20] = 0xec;
  346. __tda18271_write_regs(fe, R_EB20, 1, false);
  347. }
  348. /* image rejection calibration */
  349. /* low-band */
  350. regs[R_EP3] = 0x1f;
  351. regs[R_EP4] = 0x66;
  352. regs[R_EP5] = 0x81;
  353. regs[R_CPD] = 0xcc;
  354. regs[R_CD1] = 0x6c;
  355. regs[R_CD2] = 0x00;
  356. regs[R_CD3] = 0x00;
  357. regs[R_MPD] = 0xcd;
  358. regs[R_MD1] = 0x77;
  359. regs[R_MD2] = 0x08;
  360. regs[R_MD3] = 0x00;
  361. __tda18271_write_regs(fe, R_EP3, 11, false);
  362. if ((priv->id) == TDA18271HDC2) {
  363. /* main pll cp source on */
  364. __tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1, false);
  365. msleep(1);
  366. /* main pll cp source off */
  367. __tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0, false);
  368. }
  369. msleep(5); /* pll locking */
  370. /* launch detector */
  371. __tda18271_write_regs(fe, R_EP1, 1, false);
  372. msleep(5); /* wanted low measurement */
  373. regs[R_EP5] = 0x85;
  374. regs[R_CPD] = 0xcb;
  375. regs[R_CD1] = 0x66;
  376. regs[R_CD2] = 0x70;
  377. __tda18271_write_regs(fe, R_EP3, 7, false);
  378. msleep(5); /* pll locking */
  379. /* launch optimization algorithm */
  380. __tda18271_write_regs(fe, R_EP2, 1, false);
  381. msleep(30); /* image low optimization completion */
  382. /* mid-band */
  383. regs[R_EP5] = 0x82;
  384. regs[R_CPD] = 0xa8;
  385. regs[R_CD2] = 0x00;
  386. regs[R_MPD] = 0xa9;
  387. regs[R_MD1] = 0x73;
  388. regs[R_MD2] = 0x1a;
  389. __tda18271_write_regs(fe, R_EP3, 11, false);
  390. msleep(5); /* pll locking */
  391. /* launch detector */
  392. __tda18271_write_regs(fe, R_EP1, 1, false);
  393. msleep(5); /* wanted mid measurement */
  394. regs[R_EP5] = 0x86;
  395. regs[R_CPD] = 0xa8;
  396. regs[R_CD1] = 0x66;
  397. regs[R_CD2] = 0xa0;
  398. __tda18271_write_regs(fe, R_EP3, 7, false);
  399. msleep(5); /* pll locking */
  400. /* launch optimization algorithm */
  401. __tda18271_write_regs(fe, R_EP2, 1, false);
  402. msleep(30); /* image mid optimization completion */
  403. /* high-band */
  404. regs[R_EP5] = 0x83;
  405. regs[R_CPD] = 0x98;
  406. regs[R_CD1] = 0x65;
  407. regs[R_CD2] = 0x00;
  408. regs[R_MPD] = 0x99;
  409. regs[R_MD1] = 0x71;
  410. regs[R_MD2] = 0xcd;
  411. __tda18271_write_regs(fe, R_EP3, 11, false);
  412. msleep(5); /* pll locking */
  413. /* launch detector */
  414. __tda18271_write_regs(fe, R_EP1, 1, false);
  415. msleep(5); /* wanted high measurement */
  416. regs[R_EP5] = 0x87;
  417. regs[R_CD1] = 0x65;
  418. regs[R_CD2] = 0x50;
  419. __tda18271_write_regs(fe, R_EP3, 7, false);
  420. msleep(5); /* pll locking */
  421. /* launch optimization algorithm */
  422. __tda18271_write_regs(fe, R_EP2, 1, false);
  423. msleep(30); /* image high optimization completion */
  424. /* return to normal mode */
  425. regs[R_EP4] = 0x64;
  426. __tda18271_write_regs(fe, R_EP4, 1, false);
  427. /* synchronize */
  428. __tda18271_write_regs(fe, R_EP1, 1, false);
  429. i2c_unlock_adapter(priv->i2c_props.adap);
  430. tda18271_i2c_gate_ctrl(fe, 0);
  431. return 0;
  432. }
  433. /*---------------------------------------------------------------------*/
  434. /*
  435. * Standby modes, EP3 [7:5]
  436. *
  437. * | SM || SM_LT || SM_XT || mode description
  438. * |=====\\=======\\=======\\===================================
  439. * | 0 || 0 || 0 || normal mode
  440. * |-----||-------||-------||-----------------------------------
  441. * | || || || standby mode w/ slave tuner output
  442. * | 1 || 0 || 0 || & loop thru & xtal oscillator on
  443. * |-----||-------||-------||-----------------------------------
  444. * | 1 || 1 || 0 || standby mode w/ xtal oscillator on
  445. * |-----||-------||-------||-----------------------------------
  446. * | 1 || 1 || 1 || power off
  447. *
  448. */
  449. int tda18271_set_standby_mode(struct dvb_frontend *fe,
  450. int sm, int sm_lt, int sm_xt)
  451. {
  452. struct tda18271_priv *priv = fe->tuner_priv;
  453. unsigned char *regs = priv->tda18271_regs;
  454. if (tda18271_debug & DBG_ADV)
  455. tda_dbg("sm = %d, sm_lt = %d, sm_xt = %d\n", sm, sm_lt, sm_xt);
  456. regs[R_EP3] &= ~0xe0; /* clear sm, sm_lt, sm_xt */
  457. regs[R_EP3] |= (sm ? (1 << 7) : 0) |
  458. (sm_lt ? (1 << 6) : 0) |
  459. (sm_xt ? (1 << 5) : 0);
  460. return tda18271_write_regs(fe, R_EP3, 1);
  461. }
  462. /*---------------------------------------------------------------------*/
  463. int tda18271_calc_main_pll(struct dvb_frontend *fe, u32 freq)
  464. {
  465. /* sets main post divider & divider bytes, but does not write them */
  466. struct tda18271_priv *priv = fe->tuner_priv;
  467. unsigned char *regs = priv->tda18271_regs;
  468. u8 d, pd;
  469. u32 div;
  470. int ret = tda18271_lookup_pll_map(fe, MAIN_PLL, &freq, &pd, &d);
  471. if (tda_fail(ret))
  472. goto fail;
  473. regs[R_MPD] = (0x7f & pd);
  474. div = ((d * (freq / 1000)) << 7) / 125;
  475. regs[R_MD1] = 0x7f & (div >> 16);
  476. regs[R_MD2] = 0xff & (div >> 8);
  477. regs[R_MD3] = 0xff & div;
  478. fail:
  479. return ret;
  480. }
  481. int tda18271_calc_cal_pll(struct dvb_frontend *fe, u32 freq)
  482. {
  483. /* sets cal post divider & divider bytes, but does not write them */
  484. struct tda18271_priv *priv = fe->tuner_priv;
  485. unsigned char *regs = priv->tda18271_regs;
  486. u8 d, pd;
  487. u32 div;
  488. int ret = tda18271_lookup_pll_map(fe, CAL_PLL, &freq, &pd, &d);
  489. if (tda_fail(ret))
  490. goto fail;
  491. regs[R_CPD] = pd;
  492. div = ((d * (freq / 1000)) << 7) / 125;
  493. regs[R_CD1] = 0x7f & (div >> 16);
  494. regs[R_CD2] = 0xff & (div >> 8);
  495. regs[R_CD3] = 0xff & div;
  496. fail:
  497. return ret;
  498. }
  499. /*---------------------------------------------------------------------*/
  500. int tda18271_calc_bp_filter(struct dvb_frontend *fe, u32 *freq)
  501. {
  502. /* sets bp filter bits, but does not write them */
  503. struct tda18271_priv *priv = fe->tuner_priv;
  504. unsigned char *regs = priv->tda18271_regs;
  505. u8 val;
  506. int ret = tda18271_lookup_map(fe, BP_FILTER, freq, &val);
  507. if (tda_fail(ret))
  508. goto fail;
  509. regs[R_EP1] &= ~0x07; /* clear bp filter bits */
  510. regs[R_EP1] |= (0x07 & val);
  511. fail:
  512. return ret;
  513. }
  514. int tda18271_calc_km(struct dvb_frontend *fe, u32 *freq)
  515. {
  516. /* sets K & M bits, but does not write them */
  517. struct tda18271_priv *priv = fe->tuner_priv;
  518. unsigned char *regs = priv->tda18271_regs;
  519. u8 val;
  520. int ret = tda18271_lookup_map(fe, RF_CAL_KMCO, freq, &val);
  521. if (tda_fail(ret))
  522. goto fail;
  523. regs[R_EB13] &= ~0x7c; /* clear k & m bits */
  524. regs[R_EB13] |= (0x7c & val);
  525. fail:
  526. return ret;
  527. }
  528. int tda18271_calc_rf_band(struct dvb_frontend *fe, u32 *freq)
  529. {
  530. /* sets rf band bits, but does not write them */
  531. struct tda18271_priv *priv = fe->tuner_priv;
  532. unsigned char *regs = priv->tda18271_regs;
  533. u8 val;
  534. int ret = tda18271_lookup_map(fe, RF_BAND, freq, &val);
  535. if (tda_fail(ret))
  536. goto fail;
  537. regs[R_EP2] &= ~0xe0; /* clear rf band bits */
  538. regs[R_EP2] |= (0xe0 & (val << 5));
  539. fail:
  540. return ret;
  541. }
  542. int tda18271_calc_gain_taper(struct dvb_frontend *fe, u32 *freq)
  543. {
  544. /* sets gain taper bits, but does not write them */
  545. struct tda18271_priv *priv = fe->tuner_priv;
  546. unsigned char *regs = priv->tda18271_regs;
  547. u8 val;
  548. int ret = tda18271_lookup_map(fe, GAIN_TAPER, freq, &val);
  549. if (tda_fail(ret))
  550. goto fail;
  551. regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
  552. regs[R_EP2] |= (0x1f & val);
  553. fail:
  554. return ret;
  555. }
  556. int tda18271_calc_ir_measure(struct dvb_frontend *fe, u32 *freq)
  557. {
  558. /* sets IR Meas bits, but does not write them */
  559. struct tda18271_priv *priv = fe->tuner_priv;
  560. unsigned char *regs = priv->tda18271_regs;
  561. u8 val;
  562. int ret = tda18271_lookup_map(fe, IR_MEASURE, freq, &val);
  563. if (tda_fail(ret))
  564. goto fail;
  565. regs[R_EP5] &= ~0x07;
  566. regs[R_EP5] |= (0x07 & val);
  567. fail:
  568. return ret;
  569. }
  570. int tda18271_calc_rf_cal(struct dvb_frontend *fe, u32 *freq)
  571. {
  572. /* sets rf cal byte (RFC_Cprog), but does not write it */
  573. struct tda18271_priv *priv = fe->tuner_priv;
  574. unsigned char *regs = priv->tda18271_regs;
  575. u8 val;
  576. int ret = tda18271_lookup_map(fe, RF_CAL, freq, &val);
  577. /* The TDA18271HD/C1 rf_cal map lookup is expected to go out of range
  578. * for frequencies above 61.1 MHz. In these cases, the internal RF
  579. * tracking filters calibration mechanism is used.
  580. *
  581. * There is no need to warn the user about this.
  582. */
  583. if (ret < 0)
  584. goto fail;
  585. regs[R_EB14] = val;
  586. fail:
  587. return ret;
  588. }
  589. void _tda_printk(struct tda18271_priv *state, const char *level,
  590. const char *func, const char *fmt, ...)
  591. {
  592. struct va_format vaf;
  593. va_list args;
  594. va_start(args, fmt);
  595. vaf.fmt = fmt;
  596. vaf.va = &args;
  597. if (state)
  598. printk("%s%s: [%d-%04x|%c] %pV",
  599. level, func, i2c_adapter_id(state->i2c_props.adap),
  600. state->i2c_props.addr,
  601. (state->role == TDA18271_MASTER) ? 'M' : 'S',
  602. &vaf);
  603. else
  604. printk("%s%s: %pV", level, func, &vaf);
  605. va_end(args);
  606. }