tda18271-common.c 18 KB

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