tda827x.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. *
  3. * (c) 2005 Hartmut Hackmann
  4. * (c) 2007 Michael Krufky
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <asm/types.h>
  23. #include <linux/dvb/frontend.h>
  24. #include <linux/videodev2.h>
  25. #include "tda827x.h"
  26. static int debug;
  27. module_param(debug, int, 0644);
  28. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  29. #define dprintk(args...) \
  30. do { \
  31. if (debug) printk(KERN_DEBUG "tda827x: " args); \
  32. } while (0)
  33. struct tda827x_priv {
  34. int i2c_addr;
  35. struct i2c_adapter *i2c_adap;
  36. struct tda827x_config *cfg;
  37. unsigned int sgIF;
  38. unsigned char lpsel;
  39. u32 frequency;
  40. u32 bandwidth;
  41. };
  42. static void tda827x_set_std(struct dvb_frontend *fe,
  43. struct analog_parameters *params)
  44. {
  45. struct tda827x_priv *priv = fe->tuner_priv;
  46. char *mode;
  47. priv->lpsel = 0;
  48. if (params->std & V4L2_STD_MN) {
  49. priv->sgIF = 92;
  50. priv->lpsel = 1;
  51. mode = "MN";
  52. } else if (params->std & V4L2_STD_B) {
  53. priv->sgIF = 108;
  54. mode = "B";
  55. } else if (params->std & V4L2_STD_GH) {
  56. priv->sgIF = 124;
  57. mode = "GH";
  58. } else if (params->std & V4L2_STD_PAL_I) {
  59. priv->sgIF = 124;
  60. mode = "I";
  61. } else if (params->std & V4L2_STD_DK) {
  62. priv->sgIF = 124;
  63. mode = "DK";
  64. } else if (params->std & V4L2_STD_SECAM_L) {
  65. priv->sgIF = 124;
  66. mode = "L";
  67. } else if (params->std & V4L2_STD_SECAM_LC) {
  68. priv->sgIF = 20;
  69. mode = "LC";
  70. } else {
  71. priv->sgIF = 124;
  72. mode = "xx";
  73. }
  74. if (params->mode == V4L2_TUNER_RADIO) {
  75. priv->sgIF = 88; /* if frequency is 5.5 MHz */
  76. dprintk("setting tda827x to radio FM\n");
  77. } else
  78. dprintk("setting tda827x to system %s\n", mode);
  79. }
  80. /* ------------------------------------------------------------------ */
  81. struct tda827x_data {
  82. u32 lomax;
  83. u8 spd;
  84. u8 bs;
  85. u8 bp;
  86. u8 cp;
  87. u8 gc3;
  88. u8 div1p5;
  89. };
  90. static const struct tda827x_data tda827x_table[] = {
  91. { .lomax = 62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
  92. { .lomax = 66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
  93. { .lomax = 76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
  94. { .lomax = 84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
  95. { .lomax = 93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
  96. { .lomax = 98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
  97. { .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
  98. { .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
  99. { .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
  100. { .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
  101. { .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
  102. { .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
  103. { .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
  104. { .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
  105. { .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
  106. { .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
  107. { .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
  108. { .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
  109. { .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
  110. { .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
  111. { .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
  112. { .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
  113. { .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
  114. { .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
  115. { .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
  116. { .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
  117. { .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
  118. { .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
  119. { .lomax = 0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
  120. };
  121. static int tuner_transfer(struct dvb_frontend *fe,
  122. struct i2c_msg *msg,
  123. const int size)
  124. {
  125. int rc;
  126. struct tda827x_priv *priv = fe->tuner_priv;
  127. if (fe->ops.i2c_gate_ctrl)
  128. fe->ops.i2c_gate_ctrl(fe, 1);
  129. rc = i2c_transfer(priv->i2c_adap, msg, size);
  130. if (fe->ops.i2c_gate_ctrl)
  131. fe->ops.i2c_gate_ctrl(fe, 0);
  132. if (rc >= 0 && rc != size)
  133. return -EIO;
  134. return rc;
  135. }
  136. static int tda827xo_set_params(struct dvb_frontend *fe)
  137. {
  138. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  139. struct tda827x_priv *priv = fe->tuner_priv;
  140. u8 buf[14];
  141. int rc;
  142. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
  143. .buf = buf, .len = sizeof(buf) };
  144. int i, tuner_freq, if_freq;
  145. u32 N;
  146. dprintk("%s:\n", __func__);
  147. if (c->bandwidth_hz == 0) {
  148. if_freq = 5000000;
  149. } else if (c->bandwidth_hz <= 6000000) {
  150. if_freq = 4000000;
  151. } else if (c->bandwidth_hz <= 7000000) {
  152. if_freq = 4500000;
  153. } else { /* 8 MHz */
  154. if_freq = 5000000;
  155. }
  156. tuner_freq = c->frequency;
  157. i = 0;
  158. while (tda827x_table[i].lomax < tuner_freq) {
  159. if (tda827x_table[i + 1].lomax == 0)
  160. break;
  161. i++;
  162. }
  163. tuner_freq += if_freq;
  164. N = ((tuner_freq + 125000) / 250000) << (tda827x_table[i].spd + 2);
  165. buf[0] = 0;
  166. buf[1] = (N>>8) | 0x40;
  167. buf[2] = N & 0xff;
  168. buf[3] = 0;
  169. buf[4] = 0x52;
  170. buf[5] = (tda827x_table[i].spd << 6) + (tda827x_table[i].div1p5 << 5) +
  171. (tda827x_table[i].bs << 3) +
  172. tda827x_table[i].bp;
  173. buf[6] = (tda827x_table[i].gc3 << 4) + 0x8f;
  174. buf[7] = 0xbf;
  175. buf[8] = 0x2a;
  176. buf[9] = 0x05;
  177. buf[10] = 0xff;
  178. buf[11] = 0x00;
  179. buf[12] = 0x00;
  180. buf[13] = 0x40;
  181. msg.len = 14;
  182. rc = tuner_transfer(fe, &msg, 1);
  183. if (rc < 0)
  184. goto err;
  185. msleep(500);
  186. /* correct CP value */
  187. buf[0] = 0x30;
  188. buf[1] = 0x50 + tda827x_table[i].cp;
  189. msg.len = 2;
  190. rc = tuner_transfer(fe, &msg, 1);
  191. if (rc < 0)
  192. goto err;
  193. priv->frequency = c->frequency;
  194. priv->bandwidth = c->bandwidth_hz;
  195. return 0;
  196. err:
  197. printk(KERN_ERR "%s: could not write to tuner at addr: 0x%02x\n",
  198. __func__, priv->i2c_addr << 1);
  199. return rc;
  200. }
  201. static int tda827xo_sleep(struct dvb_frontend *fe)
  202. {
  203. struct tda827x_priv *priv = fe->tuner_priv;
  204. static u8 buf[] = { 0x30, 0xd0 };
  205. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
  206. .buf = buf, .len = sizeof(buf) };
  207. dprintk("%s:\n", __func__);
  208. tuner_transfer(fe, &msg, 1);
  209. if (priv->cfg && priv->cfg->sleep)
  210. priv->cfg->sleep(fe);
  211. return 0;
  212. }
  213. /* ------------------------------------------------------------------ */
  214. static int tda827xo_set_analog_params(struct dvb_frontend *fe,
  215. struct analog_parameters *params)
  216. {
  217. unsigned char tuner_reg[8];
  218. unsigned char reg2[2];
  219. u32 N;
  220. int i;
  221. struct tda827x_priv *priv = fe->tuner_priv;
  222. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0 };
  223. unsigned int freq = params->frequency;
  224. tda827x_set_std(fe, params);
  225. if (params->mode == V4L2_TUNER_RADIO)
  226. freq = freq / 1000;
  227. N = freq + priv->sgIF;
  228. i = 0;
  229. while (tda827x_table[i].lomax < N * 62500) {
  230. if (tda827x_table[i + 1].lomax == 0)
  231. break;
  232. i++;
  233. }
  234. N = N << tda827x_table[i].spd;
  235. tuner_reg[0] = 0;
  236. tuner_reg[1] = (unsigned char)(N>>8);
  237. tuner_reg[2] = (unsigned char) N;
  238. tuner_reg[3] = 0x40;
  239. tuner_reg[4] = 0x52 + (priv->lpsel << 5);
  240. tuner_reg[5] = (tda827x_table[i].spd << 6) +
  241. (tda827x_table[i].div1p5 << 5) +
  242. (tda827x_table[i].bs << 3) + tda827x_table[i].bp;
  243. tuner_reg[6] = 0x8f + (tda827x_table[i].gc3 << 4);
  244. tuner_reg[7] = 0x8f;
  245. msg.buf = tuner_reg;
  246. msg.len = 8;
  247. tuner_transfer(fe, &msg, 1);
  248. msg.buf = reg2;
  249. msg.len = 2;
  250. reg2[0] = 0x80;
  251. reg2[1] = 0;
  252. tuner_transfer(fe, &msg, 1);
  253. reg2[0] = 0x60;
  254. reg2[1] = 0xbf;
  255. tuner_transfer(fe, &msg, 1);
  256. reg2[0] = 0x30;
  257. reg2[1] = tuner_reg[4] + 0x80;
  258. tuner_transfer(fe, &msg, 1);
  259. msleep(1);
  260. reg2[0] = 0x30;
  261. reg2[1] = tuner_reg[4] + 4;
  262. tuner_transfer(fe, &msg, 1);
  263. msleep(1);
  264. reg2[0] = 0x30;
  265. reg2[1] = tuner_reg[4];
  266. tuner_transfer(fe, &msg, 1);
  267. msleep(550);
  268. reg2[0] = 0x30;
  269. reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_table[i].cp;
  270. tuner_transfer(fe, &msg, 1);
  271. reg2[0] = 0x60;
  272. reg2[1] = 0x3f;
  273. tuner_transfer(fe, &msg, 1);
  274. reg2[0] = 0x80;
  275. reg2[1] = 0x08; /* Vsync en */
  276. tuner_transfer(fe, &msg, 1);
  277. priv->frequency = params->frequency;
  278. return 0;
  279. }
  280. static void tda827xo_agcf(struct dvb_frontend *fe)
  281. {
  282. struct tda827x_priv *priv = fe->tuner_priv;
  283. unsigned char data[] = { 0x80, 0x0c };
  284. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
  285. .buf = data, .len = 2};
  286. tuner_transfer(fe, &msg, 1);
  287. }
  288. /* ------------------------------------------------------------------ */
  289. struct tda827xa_data {
  290. u32 lomax;
  291. u8 svco;
  292. u8 spd;
  293. u8 scr;
  294. u8 sbs;
  295. u8 gc3;
  296. };
  297. static struct tda827xa_data tda827xa_dvbt[] = {
  298. { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
  299. { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
  300. { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
  301. { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
  302. { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
  303. { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  304. { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  305. { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  306. { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  307. { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
  308. { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
  309. { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
  310. { .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
  311. { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
  312. { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
  313. { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
  314. { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
  315. { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
  316. { .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
  317. { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
  318. { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
  319. { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
  320. { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
  321. { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
  322. { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
  323. { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
  324. { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
  325. };
  326. static struct tda827xa_data tda827xa_dvbc[] = {
  327. { .lomax = 50125000, .svco = 2, .spd = 4, .scr = 2, .sbs = 0, .gc3 = 3},
  328. { .lomax = 58500000, .svco = 3, .spd = 4, .scr = 2, .sbs = 0, .gc3 = 3},
  329. { .lomax = 69250000, .svco = 0, .spd = 3, .scr = 2, .sbs = 0, .gc3 = 3},
  330. { .lomax = 83625000, .svco = 1, .spd = 3, .scr = 2, .sbs = 0, .gc3 = 3},
  331. { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 2, .sbs = 0, .gc3 = 3},
  332. { .lomax = 100250000, .svco = 2, .spd = 3, .scr = 2, .sbs = 1, .gc3 = 1},
  333. { .lomax = 117000000, .svco = 3, .spd = 3, .scr = 2, .sbs = 1, .gc3 = 1},
  334. { .lomax = 138500000, .svco = 0, .spd = 2, .scr = 2, .sbs = 1, .gc3 = 1},
  335. { .lomax = 167250000, .svco = 1, .spd = 2, .scr = 2, .sbs = 1, .gc3 = 1},
  336. { .lomax = 187000000, .svco = 2, .spd = 2, .scr = 2, .sbs = 1, .gc3 = 1},
  337. { .lomax = 200500000, .svco = 2, .spd = 2, .scr = 2, .sbs = 2, .gc3 = 1},
  338. { .lomax = 234000000, .svco = 3, .spd = 2, .scr = 2, .sbs = 2, .gc3 = 3},
  339. { .lomax = 277000000, .svco = 0, .spd = 1, .scr = 2, .sbs = 2, .gc3 = 3},
  340. { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 2, .sbs = 2, .gc3 = 1},
  341. { .lomax = 334500000, .svco = 1, .spd = 1, .scr = 2, .sbs = 3, .gc3 = 3},
  342. { .lomax = 401000000, .svco = 2, .spd = 1, .scr = 2, .sbs = 3, .gc3 = 3},
  343. { .lomax = 468000000, .svco = 3, .spd = 1, .scr = 2, .sbs = 3, .gc3 = 1},
  344. { .lomax = 535000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
  345. { .lomax = 554000000, .svco = 0, .spd = 0, .scr = 2, .sbs = 3, .gc3 = 1},
  346. { .lomax = 638000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
  347. { .lomax = 669000000, .svco = 1, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 1},
  348. { .lomax = 720000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
  349. { .lomax = 802000000, .svco = 2, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 1},
  350. { .lomax = 835000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
  351. { .lomax = 885000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
  352. { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 1},
  353. { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
  354. };
  355. static struct tda827xa_data tda827xa_analog[] = {
  356. { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3},
  357. { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
  358. { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
  359. { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
  360. { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
  361. { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  362. { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  363. { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  364. { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
  365. { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
  366. { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3},
  367. { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3},
  368. { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
  369. { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
  370. { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
  371. { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
  372. { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
  373. { .lomax = 554000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
  374. { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
  375. { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
  376. { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
  377. { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
  378. { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
  379. { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
  380. { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
  381. { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
  382. };
  383. static int tda827xa_sleep(struct dvb_frontend *fe)
  384. {
  385. struct tda827x_priv *priv = fe->tuner_priv;
  386. static u8 buf[] = { 0x30, 0x90 };
  387. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
  388. .buf = buf, .len = sizeof(buf) };
  389. dprintk("%s:\n", __func__);
  390. tuner_transfer(fe, &msg, 1);
  391. if (priv->cfg && priv->cfg->sleep)
  392. priv->cfg->sleep(fe);
  393. return 0;
  394. }
  395. static void tda827xa_lna_gain(struct dvb_frontend *fe, int high,
  396. struct analog_parameters *params)
  397. {
  398. struct tda827x_priv *priv = fe->tuner_priv;
  399. unsigned char buf[] = {0x22, 0x01};
  400. int arg;
  401. int gp_func;
  402. struct i2c_msg msg = { .flags = 0, .buf = buf, .len = sizeof(buf) };
  403. if (NULL == priv->cfg) {
  404. dprintk("tda827x_config not defined, cannot set LNA gain!\n");
  405. return;
  406. }
  407. msg.addr = priv->cfg->switch_addr;
  408. if (priv->cfg->config) {
  409. if (high)
  410. dprintk("setting LNA to high gain\n");
  411. else
  412. dprintk("setting LNA to low gain\n");
  413. }
  414. switch (priv->cfg->config) {
  415. case TDA8290_LNA_OFF: /* no LNA */
  416. break;
  417. case TDA8290_LNA_GP0_HIGH_ON: /* switch is GPIO 0 of tda8290 */
  418. case TDA8290_LNA_GP0_HIGH_OFF:
  419. if (params == NULL) {
  420. gp_func = 0;
  421. arg = 0;
  422. } else {
  423. /* turn Vsync on */
  424. gp_func = 1;
  425. if (params->std & V4L2_STD_MN)
  426. arg = 1;
  427. else
  428. arg = 0;
  429. }
  430. if (fe->callback)
  431. fe->callback(priv->i2c_adap->algo_data,
  432. DVB_FRONTEND_COMPONENT_TUNER,
  433. gp_func, arg);
  434. buf[1] = high ? 0 : 1;
  435. if (priv->cfg->config == TDA8290_LNA_GP0_HIGH_OFF)
  436. buf[1] = high ? 1 : 0;
  437. tuner_transfer(fe, &msg, 1);
  438. break;
  439. case TDA8290_LNA_ON_BRIDGE: /* switch with GPIO of saa713x */
  440. if (fe->callback)
  441. fe->callback(priv->i2c_adap->algo_data,
  442. DVB_FRONTEND_COMPONENT_TUNER, 0, high);
  443. break;
  444. }
  445. }
  446. static int tda827xa_set_params(struct dvb_frontend *fe)
  447. {
  448. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  449. struct tda827x_priv *priv = fe->tuner_priv;
  450. struct tda827xa_data *frequency_map = tda827xa_dvbt;
  451. u8 buf[11];
  452. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
  453. .buf = buf, .len = sizeof(buf) };
  454. int i, tuner_freq, if_freq, rc;
  455. u32 N;
  456. dprintk("%s:\n", __func__);
  457. tda827xa_lna_gain(fe, 1, NULL);
  458. msleep(20);
  459. if (c->bandwidth_hz == 0) {
  460. if_freq = 5000000;
  461. } else if (c->bandwidth_hz <= 6000000) {
  462. if_freq = 4000000;
  463. } else if (c->bandwidth_hz <= 7000000) {
  464. if_freq = 4500000;
  465. } else { /* 8 MHz */
  466. if_freq = 5000000;
  467. }
  468. tuner_freq = c->frequency;
  469. switch (c->delivery_system) {
  470. case SYS_DVBC_ANNEX_A:
  471. case SYS_DVBC_ANNEX_C:
  472. dprintk("%s select tda827xa_dvbc\n", __func__);
  473. frequency_map = tda827xa_dvbc;
  474. break;
  475. default:
  476. break;
  477. }
  478. i = 0;
  479. while (frequency_map[i].lomax < tuner_freq) {
  480. if (frequency_map[i + 1].lomax == 0)
  481. break;
  482. i++;
  483. }
  484. tuner_freq += if_freq;
  485. N = ((tuner_freq + 31250) / 62500) << frequency_map[i].spd;
  486. buf[0] = 0; // subaddress
  487. buf[1] = N >> 8;
  488. buf[2] = N & 0xff;
  489. buf[3] = 0;
  490. buf[4] = 0x16;
  491. buf[5] = (frequency_map[i].spd << 5) + (frequency_map[i].svco << 3) +
  492. frequency_map[i].sbs;
  493. buf[6] = 0x4b + (frequency_map[i].gc3 << 4);
  494. buf[7] = 0x1c;
  495. buf[8] = 0x06;
  496. buf[9] = 0x24;
  497. buf[10] = 0x00;
  498. msg.len = 11;
  499. rc = tuner_transfer(fe, &msg, 1);
  500. if (rc < 0)
  501. goto err;
  502. buf[0] = 0x90;
  503. buf[1] = 0xff;
  504. buf[2] = 0x60;
  505. buf[3] = 0x00;
  506. buf[4] = 0x59; // lpsel, for 6MHz + 2
  507. msg.len = 5;
  508. rc = tuner_transfer(fe, &msg, 1);
  509. if (rc < 0)
  510. goto err;
  511. buf[0] = 0xa0;
  512. buf[1] = 0x40;
  513. msg.len = 2;
  514. rc = tuner_transfer(fe, &msg, 1);
  515. if (rc < 0)
  516. goto err;
  517. msleep(11);
  518. msg.flags = I2C_M_RD;
  519. rc = tuner_transfer(fe, &msg, 1);
  520. if (rc < 0)
  521. goto err;
  522. msg.flags = 0;
  523. buf[1] >>= 4;
  524. dprintk("tda8275a AGC2 gain is: %d\n", buf[1]);
  525. if ((buf[1]) < 2) {
  526. tda827xa_lna_gain(fe, 0, NULL);
  527. buf[0] = 0x60;
  528. buf[1] = 0x0c;
  529. rc = tuner_transfer(fe, &msg, 1);
  530. if (rc < 0)
  531. goto err;
  532. }
  533. buf[0] = 0xc0;
  534. buf[1] = 0x99; // lpsel, for 6MHz + 2
  535. rc = tuner_transfer(fe, &msg, 1);
  536. if (rc < 0)
  537. goto err;
  538. buf[0] = 0x60;
  539. buf[1] = 0x3c;
  540. rc = tuner_transfer(fe, &msg, 1);
  541. if (rc < 0)
  542. goto err;
  543. /* correct CP value */
  544. buf[0] = 0x30;
  545. buf[1] = 0x10 + frequency_map[i].scr;
  546. rc = tuner_transfer(fe, &msg, 1);
  547. if (rc < 0)
  548. goto err;
  549. msleep(163);
  550. buf[0] = 0xc0;
  551. buf[1] = 0x39; // lpsel, for 6MHz + 2
  552. rc = tuner_transfer(fe, &msg, 1);
  553. if (rc < 0)
  554. goto err;
  555. msleep(3);
  556. /* freeze AGC1 */
  557. buf[0] = 0x50;
  558. buf[1] = 0x4f + (frequency_map[i].gc3 << 4);
  559. rc = tuner_transfer(fe, &msg, 1);
  560. if (rc < 0)
  561. goto err;
  562. priv->frequency = c->frequency;
  563. priv->bandwidth = c->bandwidth_hz;
  564. return 0;
  565. err:
  566. printk(KERN_ERR "%s: could not write to tuner at addr: 0x%02x\n",
  567. __func__, priv->i2c_addr << 1);
  568. return rc;
  569. }
  570. static int tda827xa_set_analog_params(struct dvb_frontend *fe,
  571. struct analog_parameters *params)
  572. {
  573. unsigned char tuner_reg[11];
  574. u32 N;
  575. int i;
  576. struct tda827x_priv *priv = fe->tuner_priv;
  577. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
  578. .buf = tuner_reg, .len = sizeof(tuner_reg) };
  579. unsigned int freq = params->frequency;
  580. tda827x_set_std(fe, params);
  581. tda827xa_lna_gain(fe, 1, params);
  582. msleep(10);
  583. if (params->mode == V4L2_TUNER_RADIO)
  584. freq = freq / 1000;
  585. N = freq + priv->sgIF;
  586. i = 0;
  587. while (tda827xa_analog[i].lomax < N * 62500) {
  588. if (tda827xa_analog[i + 1].lomax == 0)
  589. break;
  590. i++;
  591. }
  592. N = N << tda827xa_analog[i].spd;
  593. tuner_reg[0] = 0;
  594. tuner_reg[1] = (unsigned char)(N>>8);
  595. tuner_reg[2] = (unsigned char) N;
  596. tuner_reg[3] = 0;
  597. tuner_reg[4] = 0x16;
  598. tuner_reg[5] = (tda827xa_analog[i].spd << 5) +
  599. (tda827xa_analog[i].svco << 3) +
  600. tda827xa_analog[i].sbs;
  601. tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
  602. tuner_reg[7] = 0x1c;
  603. tuner_reg[8] = 4;
  604. tuner_reg[9] = 0x20;
  605. tuner_reg[10] = 0x00;
  606. msg.len = 11;
  607. tuner_transfer(fe, &msg, 1);
  608. tuner_reg[0] = 0x90;
  609. tuner_reg[1] = 0xff;
  610. tuner_reg[2] = 0xe0;
  611. tuner_reg[3] = 0;
  612. tuner_reg[4] = 0x99 + (priv->lpsel << 1);
  613. msg.len = 5;
  614. tuner_transfer(fe, &msg, 1);
  615. tuner_reg[0] = 0xa0;
  616. tuner_reg[1] = 0xc0;
  617. msg.len = 2;
  618. tuner_transfer(fe, &msg, 1);
  619. tuner_reg[0] = 0x30;
  620. tuner_reg[1] = 0x10 + tda827xa_analog[i].scr;
  621. tuner_transfer(fe, &msg, 1);
  622. msg.flags = I2C_M_RD;
  623. tuner_transfer(fe, &msg, 1);
  624. msg.flags = 0;
  625. tuner_reg[1] >>= 4;
  626. dprintk("AGC2 gain is: %d\n", tuner_reg[1]);
  627. if (tuner_reg[1] < 1)
  628. tda827xa_lna_gain(fe, 0, params);
  629. msleep(100);
  630. tuner_reg[0] = 0x60;
  631. tuner_reg[1] = 0x3c;
  632. tuner_transfer(fe, &msg, 1);
  633. msleep(163);
  634. tuner_reg[0] = 0x50;
  635. tuner_reg[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
  636. tuner_transfer(fe, &msg, 1);
  637. tuner_reg[0] = 0x80;
  638. tuner_reg[1] = 0x28;
  639. tuner_transfer(fe, &msg, 1);
  640. tuner_reg[0] = 0xb0;
  641. tuner_reg[1] = 0x01;
  642. tuner_transfer(fe, &msg, 1);
  643. tuner_reg[0] = 0xc0;
  644. tuner_reg[1] = 0x19 + (priv->lpsel << 1);
  645. tuner_transfer(fe, &msg, 1);
  646. priv->frequency = params->frequency;
  647. return 0;
  648. }
  649. static void tda827xa_agcf(struct dvb_frontend *fe)
  650. {
  651. struct tda827x_priv *priv = fe->tuner_priv;
  652. unsigned char data[] = {0x80, 0x2c};
  653. struct i2c_msg msg = {.addr = priv->i2c_addr, .flags = 0,
  654. .buf = data, .len = 2};
  655. tuner_transfer(fe, &msg, 1);
  656. }
  657. /* ------------------------------------------------------------------ */
  658. static int tda827x_release(struct dvb_frontend *fe)
  659. {
  660. kfree(fe->tuner_priv);
  661. fe->tuner_priv = NULL;
  662. return 0;
  663. }
  664. static int tda827x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  665. {
  666. struct tda827x_priv *priv = fe->tuner_priv;
  667. *frequency = priv->frequency;
  668. return 0;
  669. }
  670. static int tda827x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  671. {
  672. struct tda827x_priv *priv = fe->tuner_priv;
  673. *bandwidth = priv->bandwidth;
  674. return 0;
  675. }
  676. static int tda827x_init(struct dvb_frontend *fe)
  677. {
  678. struct tda827x_priv *priv = fe->tuner_priv;
  679. dprintk("%s:\n", __func__);
  680. if (priv->cfg && priv->cfg->init)
  681. priv->cfg->init(fe);
  682. return 0;
  683. }
  684. static int tda827x_probe_version(struct dvb_frontend *fe);
  685. static int tda827x_initial_init(struct dvb_frontend *fe)
  686. {
  687. int ret;
  688. ret = tda827x_probe_version(fe);
  689. if (ret)
  690. return ret;
  691. return fe->ops.tuner_ops.init(fe);
  692. }
  693. static int tda827x_initial_sleep(struct dvb_frontend *fe)
  694. {
  695. int ret;
  696. ret = tda827x_probe_version(fe);
  697. if (ret)
  698. return ret;
  699. return fe->ops.tuner_ops.sleep(fe);
  700. }
  701. static struct dvb_tuner_ops tda827xo_tuner_ops = {
  702. .info = {
  703. .name = "Philips TDA827X",
  704. .frequency_min = 55000000,
  705. .frequency_max = 860000000,
  706. .frequency_step = 250000
  707. },
  708. .release = tda827x_release,
  709. .init = tda827x_initial_init,
  710. .sleep = tda827x_initial_sleep,
  711. .set_params = tda827xo_set_params,
  712. .set_analog_params = tda827xo_set_analog_params,
  713. .get_frequency = tda827x_get_frequency,
  714. .get_bandwidth = tda827x_get_bandwidth,
  715. };
  716. static struct dvb_tuner_ops tda827xa_tuner_ops = {
  717. .info = {
  718. .name = "Philips TDA827XA",
  719. .frequency_min = 44000000,
  720. .frequency_max = 906000000,
  721. .frequency_step = 62500
  722. },
  723. .release = tda827x_release,
  724. .init = tda827x_init,
  725. .sleep = tda827xa_sleep,
  726. .set_params = tda827xa_set_params,
  727. .set_analog_params = tda827xa_set_analog_params,
  728. .get_frequency = tda827x_get_frequency,
  729. .get_bandwidth = tda827x_get_bandwidth,
  730. };
  731. static int tda827x_probe_version(struct dvb_frontend *fe)
  732. {
  733. u8 data;
  734. int rc;
  735. struct tda827x_priv *priv = fe->tuner_priv;
  736. struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = I2C_M_RD,
  737. .buf = &data, .len = 1 };
  738. rc = tuner_transfer(fe, &msg, 1);
  739. if (rc < 0) {
  740. printk("%s: could not read from tuner at addr: 0x%02x\n",
  741. __func__, msg.addr << 1);
  742. return rc;
  743. }
  744. if ((data & 0x3c) == 0) {
  745. dprintk("tda827x tuner found\n");
  746. fe->ops.tuner_ops.init = tda827x_init;
  747. fe->ops.tuner_ops.sleep = tda827xo_sleep;
  748. if (priv->cfg)
  749. priv->cfg->agcf = tda827xo_agcf;
  750. } else {
  751. dprintk("tda827xa tuner found\n");
  752. memcpy(&fe->ops.tuner_ops, &tda827xa_tuner_ops, sizeof(struct dvb_tuner_ops));
  753. if (priv->cfg)
  754. priv->cfg->agcf = tda827xa_agcf;
  755. }
  756. return 0;
  757. }
  758. struct dvb_frontend *tda827x_attach(struct dvb_frontend *fe, int addr,
  759. struct i2c_adapter *i2c,
  760. struct tda827x_config *cfg)
  761. {
  762. struct tda827x_priv *priv = NULL;
  763. dprintk("%s:\n", __func__);
  764. priv = kzalloc(sizeof(struct tda827x_priv), GFP_KERNEL);
  765. if (priv == NULL)
  766. return NULL;
  767. priv->i2c_addr = addr;
  768. priv->i2c_adap = i2c;
  769. priv->cfg = cfg;
  770. memcpy(&fe->ops.tuner_ops, &tda827xo_tuner_ops, sizeof(struct dvb_tuner_ops));
  771. fe->tuner_priv = priv;
  772. dprintk("type set to %s\n", fe->ops.tuner_ops.info.name);
  773. return fe;
  774. }
  775. EXPORT_SYMBOL_GPL(tda827x_attach);
  776. MODULE_DESCRIPTION("DVB TDA827x driver");
  777. MODULE_AUTHOR("Hartmut Hackmann <hartmut.hackmann@t-online.de>");
  778. MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
  779. MODULE_LICENSE("GPL");