tda8290.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. i2c tv tuner chip device driver
  3. controls the philips tda8290+75 tuner chip combo.
  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. This "tda8290" module was split apart from the original "tuner" module.
  16. */
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/videodev2.h>
  21. #include "tuner-i2c.h"
  22. #include "tda8290.h"
  23. #include "tda827x.h"
  24. #include "tda18271.h"
  25. static int debug;
  26. module_param(debug, int, 0644);
  27. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  28. static int deemphasis_50;
  29. module_param(deemphasis_50, int, 0644);
  30. MODULE_PARM_DESC(deemphasis_50, "0 - 75us deemphasis; 1 - 50us deemphasis");
  31. /* ---------------------------------------------------------------------- */
  32. struct tda8290_priv {
  33. struct tuner_i2c_props i2c_props;
  34. unsigned char tda8290_easy_mode;
  35. unsigned char tda827x_addr;
  36. unsigned char ver;
  37. #define TDA8290 1
  38. #define TDA8295 2
  39. #define TDA8275 4
  40. #define TDA8275A 8
  41. #define TDA18271 16
  42. struct tda827x_config cfg;
  43. };
  44. /*---------------------------------------------------------------------*/
  45. static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
  46. {
  47. struct tda8290_priv *priv = fe->analog_demod_priv;
  48. unsigned char enable[2] = { 0x21, 0xC0 };
  49. unsigned char disable[2] = { 0x21, 0x00 };
  50. unsigned char *msg;
  51. if (close) {
  52. msg = enable;
  53. tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
  54. /* let the bridge stabilize */
  55. msleep(20);
  56. } else {
  57. msg = disable;
  58. tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
  59. }
  60. return 0;
  61. }
  62. static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
  63. {
  64. struct tda8290_priv *priv = fe->analog_demod_priv;
  65. unsigned char enable[2] = { 0x45, 0xc1 };
  66. unsigned char disable[2] = { 0x46, 0x00 };
  67. unsigned char buf[3] = { 0x45, 0x01, 0x00 };
  68. unsigned char *msg;
  69. if (close) {
  70. msg = enable;
  71. tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
  72. /* let the bridge stabilize */
  73. msleep(20);
  74. } else {
  75. msg = disable;
  76. tuner_i2c_xfer_send(&priv->i2c_props, msg, 1);
  77. tuner_i2c_xfer_recv(&priv->i2c_props, &msg[1], 1);
  78. buf[2] = msg[1];
  79. buf[2] &= ~0x04;
  80. tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
  81. msleep(5);
  82. msg[1] |= 0x04;
  83. tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
  84. }
  85. return 0;
  86. }
  87. /*---------------------------------------------------------------------*/
  88. static void set_audio(struct dvb_frontend *fe,
  89. struct analog_parameters *params)
  90. {
  91. struct tda8290_priv *priv = fe->analog_demod_priv;
  92. char* mode;
  93. if (params->std & V4L2_STD_MN) {
  94. priv->tda8290_easy_mode = 0x01;
  95. mode = "MN";
  96. } else if (params->std & V4L2_STD_B) {
  97. priv->tda8290_easy_mode = 0x02;
  98. mode = "B";
  99. } else if (params->std & V4L2_STD_GH) {
  100. priv->tda8290_easy_mode = 0x04;
  101. mode = "GH";
  102. } else if (params->std & V4L2_STD_PAL_I) {
  103. priv->tda8290_easy_mode = 0x08;
  104. mode = "I";
  105. } else if (params->std & V4L2_STD_DK) {
  106. priv->tda8290_easy_mode = 0x10;
  107. mode = "DK";
  108. } else if (params->std & V4L2_STD_SECAM_L) {
  109. priv->tda8290_easy_mode = 0x20;
  110. mode = "L";
  111. } else if (params->std & V4L2_STD_SECAM_LC) {
  112. priv->tda8290_easy_mode = 0x40;
  113. mode = "LC";
  114. } else {
  115. priv->tda8290_easy_mode = 0x10;
  116. mode = "xx";
  117. }
  118. if (params->mode == V4L2_TUNER_RADIO) {
  119. /* Set TDA8295 to FM radio; Start TDA8290 with MN values */
  120. priv->tda8290_easy_mode = (priv->ver & TDA8295) ? 0x80 : 0x01;
  121. tuner_dbg("setting to radio FM\n");
  122. } else {
  123. tuner_dbg("setting tda829x to system %s\n", mode);
  124. }
  125. }
  126. static struct {
  127. unsigned char seq[2];
  128. } fm_mode[] = {
  129. { { 0x01, 0x81} }, /* Put device into expert mode */
  130. { { 0x03, 0x48} }, /* Disable NOTCH and VIDEO filters */
  131. { { 0x04, 0x04} }, /* Disable color carrier filter (SSIF) */
  132. { { 0x05, 0x04} }, /* ADC headroom */
  133. { { 0x06, 0x10} }, /* group delay flat */
  134. { { 0x07, 0x00} }, /* use the same radio DTO values as a tda8295 */
  135. { { 0x08, 0x00} },
  136. { { 0x09, 0x80} },
  137. { { 0x0a, 0xda} },
  138. { { 0x0b, 0x4b} },
  139. { { 0x0c, 0x68} },
  140. { { 0x0d, 0x00} }, /* PLL off, no video carrier detect */
  141. { { 0x14, 0x00} }, /* disable auto mute if no video */
  142. };
  143. static void tda8290_set_params(struct dvb_frontend *fe,
  144. struct analog_parameters *params)
  145. {
  146. struct tda8290_priv *priv = fe->analog_demod_priv;
  147. unsigned char soft_reset[] = { 0x00, 0x00 };
  148. unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
  149. unsigned char expert_mode[] = { 0x01, 0x80 };
  150. unsigned char agc_out_on[] = { 0x02, 0x00 };
  151. unsigned char gainset_off[] = { 0x28, 0x14 };
  152. unsigned char if_agc_spd[] = { 0x0f, 0x88 };
  153. unsigned char adc_head_6[] = { 0x05, 0x04 };
  154. unsigned char adc_head_9[] = { 0x05, 0x02 };
  155. unsigned char adc_head_12[] = { 0x05, 0x01 };
  156. unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
  157. unsigned char pll_bw_low[] = { 0x0d, 0x27 };
  158. unsigned char gainset_2[] = { 0x28, 0x64 };
  159. unsigned char agc_rst_on[] = { 0x0e, 0x0b };
  160. unsigned char agc_rst_off[] = { 0x0e, 0x09 };
  161. unsigned char if_agc_set[] = { 0x0f, 0x81 };
  162. unsigned char addr_adc_sat = 0x1a;
  163. unsigned char addr_agc_stat = 0x1d;
  164. unsigned char addr_pll_stat = 0x1b;
  165. unsigned char adc_sat, agc_stat,
  166. pll_stat;
  167. int i;
  168. set_audio(fe, params);
  169. if (priv->cfg.config)
  170. tuner_dbg("tda827xa config is 0x%02x\n", priv->cfg.config);
  171. tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
  172. tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
  173. tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
  174. msleep(1);
  175. if (params->mode == V4L2_TUNER_RADIO) {
  176. unsigned char deemphasis[] = { 0x13, 1 };
  177. /* FIXME: allow using a different deemphasis */
  178. if (deemphasis_50)
  179. deemphasis[1] = 2;
  180. for (i = 0; i < ARRAY_SIZE(fm_mode); i++)
  181. tuner_i2c_xfer_send(&priv->i2c_props, fm_mode[i].seq, 2);
  182. tuner_i2c_xfer_send(&priv->i2c_props, deemphasis, 2);
  183. } else {
  184. expert_mode[1] = priv->tda8290_easy_mode + 0x80;
  185. tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
  186. tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
  187. tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
  188. if (priv->tda8290_easy_mode & 0x60)
  189. tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
  190. else
  191. tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
  192. tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
  193. }
  194. tda8290_i2c_bridge(fe, 1);
  195. if (fe->ops.tuner_ops.set_analog_params)
  196. fe->ops.tuner_ops.set_analog_params(fe, params);
  197. for (i = 0; i < 3; i++) {
  198. tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
  199. tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
  200. if (pll_stat & 0x80) {
  201. tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
  202. tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
  203. tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
  204. tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
  205. tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
  206. break;
  207. } else {
  208. tuner_dbg("tda8290 not locked, no signal?\n");
  209. msleep(100);
  210. }
  211. }
  212. /* adjust headroom resp. gain */
  213. if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
  214. tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
  215. agc_stat, adc_sat, pll_stat & 0x80);
  216. tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
  217. msleep(100);
  218. tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
  219. tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
  220. tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
  221. tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
  222. if ((agc_stat > 115) || !(pll_stat & 0x80)) {
  223. tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
  224. agc_stat, pll_stat & 0x80);
  225. if (priv->cfg.agcf)
  226. priv->cfg.agcf(fe);
  227. msleep(100);
  228. tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
  229. tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
  230. tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
  231. tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
  232. if((agc_stat > 115) || !(pll_stat & 0x80)) {
  233. tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
  234. tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
  235. tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
  236. msleep(100);
  237. }
  238. }
  239. }
  240. /* l/ l' deadlock? */
  241. if(priv->tda8290_easy_mode & 0x60) {
  242. tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
  243. tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
  244. tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
  245. tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
  246. if ((adc_sat > 20) || !(pll_stat & 0x80)) {
  247. tuner_dbg("trying to resolve SECAM L deadlock\n");
  248. tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
  249. msleep(40);
  250. tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
  251. }
  252. }
  253. tda8290_i2c_bridge(fe, 0);
  254. tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
  255. }
  256. /*---------------------------------------------------------------------*/
  257. static void tda8295_power(struct dvb_frontend *fe, int enable)
  258. {
  259. struct tda8290_priv *priv = fe->analog_demod_priv;
  260. unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
  261. tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
  262. tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
  263. if (enable)
  264. buf[1] = 0x01;
  265. else
  266. buf[1] = 0x03;
  267. tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
  268. }
  269. static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
  270. {
  271. struct tda8290_priv *priv = fe->analog_demod_priv;
  272. unsigned char buf[] = { 0x01, 0x00 };
  273. tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
  274. tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
  275. if (enable)
  276. buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
  277. else
  278. buf[1] = 0x00; /* reset active bit */
  279. tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
  280. }
  281. static void tda8295_set_video_std(struct dvb_frontend *fe)
  282. {
  283. struct tda8290_priv *priv = fe->analog_demod_priv;
  284. unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
  285. tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
  286. tda8295_set_easy_mode(fe, 1);
  287. msleep(20);
  288. tda8295_set_easy_mode(fe, 0);
  289. }
  290. /*---------------------------------------------------------------------*/
  291. static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
  292. {
  293. struct tda8290_priv *priv = fe->analog_demod_priv;
  294. unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
  295. tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
  296. tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
  297. if (enable)
  298. buf[1] &= ~0x40;
  299. else
  300. buf[1] |= 0x40;
  301. tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
  302. }
  303. static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
  304. {
  305. struct tda8290_priv *priv = fe->analog_demod_priv;
  306. unsigned char set_gpio_cf[] = { 0x44, 0x00 };
  307. unsigned char set_gpio_val[] = { 0x46, 0x00 };
  308. tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_cf[0], 1);
  309. tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_cf[1], 1);
  310. tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_val[0], 1);
  311. tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_val[1], 1);
  312. set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
  313. if (enable) {
  314. set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
  315. set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
  316. }
  317. tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
  318. tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
  319. }
  320. static int tda8295_has_signal(struct dvb_frontend *fe)
  321. {
  322. struct tda8290_priv *priv = fe->analog_demod_priv;
  323. unsigned char hvpll_stat = 0x26;
  324. unsigned char ret;
  325. tuner_i2c_xfer_send(&priv->i2c_props, &hvpll_stat, 1);
  326. tuner_i2c_xfer_recv(&priv->i2c_props, &ret, 1);
  327. return (ret & 0x01) ? 65535 : 0;
  328. }
  329. /*---------------------------------------------------------------------*/
  330. static void tda8295_set_params(struct dvb_frontend *fe,
  331. struct analog_parameters *params)
  332. {
  333. struct tda8290_priv *priv = fe->analog_demod_priv;
  334. unsigned char blanking_mode[] = { 0x1d, 0x00 };
  335. set_audio(fe, params);
  336. tuner_dbg("%s: freq = %d\n", __func__, params->frequency);
  337. tda8295_power(fe, 1);
  338. tda8295_agc1_out(fe, 1);
  339. tuner_i2c_xfer_send(&priv->i2c_props, &blanking_mode[0], 1);
  340. tuner_i2c_xfer_recv(&priv->i2c_props, &blanking_mode[1], 1);
  341. tda8295_set_video_std(fe);
  342. blanking_mode[1] = 0x03;
  343. tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
  344. msleep(20);
  345. tda8295_i2c_bridge(fe, 1);
  346. if (fe->ops.tuner_ops.set_analog_params)
  347. fe->ops.tuner_ops.set_analog_params(fe, params);
  348. if (priv->cfg.agcf)
  349. priv->cfg.agcf(fe);
  350. if (tda8295_has_signal(fe))
  351. tuner_dbg("tda8295 is locked\n");
  352. else
  353. tuner_dbg("tda8295 not locked, no signal?\n");
  354. tda8295_i2c_bridge(fe, 0);
  355. }
  356. /*---------------------------------------------------------------------*/
  357. static int tda8290_has_signal(struct dvb_frontend *fe)
  358. {
  359. struct tda8290_priv *priv = fe->analog_demod_priv;
  360. unsigned char i2c_get_afc[1] = { 0x1B };
  361. unsigned char afc = 0;
  362. tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
  363. tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1);
  364. return (afc & 0x80)? 65535:0;
  365. }
  366. /*---------------------------------------------------------------------*/
  367. static void tda8290_standby(struct dvb_frontend *fe)
  368. {
  369. struct tda8290_priv *priv = fe->analog_demod_priv;
  370. unsigned char cb1[] = { 0x30, 0xD0 };
  371. unsigned char tda8290_standby[] = { 0x00, 0x02 };
  372. unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
  373. struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
  374. tda8290_i2c_bridge(fe, 1);
  375. if (priv->ver & TDA8275A)
  376. cb1[1] = 0x90;
  377. i2c_transfer(priv->i2c_props.adap, &msg, 1);
  378. tda8290_i2c_bridge(fe, 0);
  379. tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
  380. tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
  381. }
  382. static void tda8295_standby(struct dvb_frontend *fe)
  383. {
  384. tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
  385. tda8295_power(fe, 0);
  386. }
  387. static void tda8290_init_if(struct dvb_frontend *fe)
  388. {
  389. struct tda8290_priv *priv = fe->analog_demod_priv;
  390. unsigned char set_VS[] = { 0x30, 0x6F };
  391. unsigned char set_GP00_CF[] = { 0x20, 0x01 };
  392. unsigned char set_GP01_CF[] = { 0x20, 0x0B };
  393. if ((priv->cfg.config == 1) || (priv->cfg.config == 2))
  394. tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
  395. else
  396. tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
  397. tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
  398. }
  399. static void tda8295_init_if(struct dvb_frontend *fe)
  400. {
  401. struct tda8290_priv *priv = fe->analog_demod_priv;
  402. static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
  403. static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
  404. static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
  405. static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
  406. static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
  407. static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
  408. static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
  409. tda8295_power(fe, 1);
  410. tda8295_set_easy_mode(fe, 0);
  411. tda8295_set_video_std(fe);
  412. tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
  413. tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
  414. tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
  415. tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
  416. tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
  417. tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
  418. tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
  419. tda8295_agc1_out(fe, 0);
  420. tda8295_agc2_out(fe, 0);
  421. }
  422. static void tda8290_init_tuner(struct dvb_frontend *fe)
  423. {
  424. struct tda8290_priv *priv = fe->analog_demod_priv;
  425. unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
  426. 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
  427. unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
  428. 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
  429. struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
  430. .buf=tda8275_init, .len = 14};
  431. if (priv->ver & TDA8275A)
  432. msg.buf = tda8275a_init;
  433. tda8290_i2c_bridge(fe, 1);
  434. i2c_transfer(priv->i2c_props.adap, &msg, 1);
  435. tda8290_i2c_bridge(fe, 0);
  436. }
  437. /*---------------------------------------------------------------------*/
  438. static void tda829x_release(struct dvb_frontend *fe)
  439. {
  440. struct tda8290_priv *priv = fe->analog_demod_priv;
  441. /* only try to release the tuner if we've
  442. * attached it from within this module */
  443. if (priv->ver & (TDA18271 | TDA8275 | TDA8275A))
  444. if (fe->ops.tuner_ops.release)
  445. fe->ops.tuner_ops.release(fe);
  446. kfree(fe->analog_demod_priv);
  447. fe->analog_demod_priv = NULL;
  448. }
  449. static struct tda18271_config tda829x_tda18271_config = {
  450. .gate = TDA18271_GATE_ANALOG,
  451. };
  452. static int tda829x_find_tuner(struct dvb_frontend *fe)
  453. {
  454. struct tda8290_priv *priv = fe->analog_demod_priv;
  455. struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
  456. int i, ret, tuners_found;
  457. u32 tuner_addrs;
  458. u8 data;
  459. struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
  460. if (!analog_ops->i2c_gate_ctrl) {
  461. printk(KERN_ERR "tda8290: no gate control were provided!\n");
  462. return -EINVAL;
  463. }
  464. analog_ops->i2c_gate_ctrl(fe, 1);
  465. /* probe for tuner chip */
  466. tuners_found = 0;
  467. tuner_addrs = 0;
  468. for (i = 0x60; i <= 0x63; i++) {
  469. msg.addr = i;
  470. ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
  471. if (ret == 1) {
  472. tuners_found++;
  473. tuner_addrs = (tuner_addrs << 8) + i;
  474. }
  475. }
  476. /* if there is more than one tuner, we expect the right one is
  477. behind the bridge and we choose the highest address that doesn't
  478. give a response now
  479. */
  480. analog_ops->i2c_gate_ctrl(fe, 0);
  481. if (tuners_found > 1)
  482. for (i = 0; i < tuners_found; i++) {
  483. msg.addr = tuner_addrs & 0xff;
  484. ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
  485. if (ret == 1)
  486. tuner_addrs = tuner_addrs >> 8;
  487. else
  488. break;
  489. }
  490. if (tuner_addrs == 0) {
  491. tuner_addrs = 0x60;
  492. tuner_info("could not clearly identify tuner address, "
  493. "defaulting to %x\n", tuner_addrs);
  494. } else {
  495. tuner_addrs = tuner_addrs & 0xff;
  496. tuner_info("setting tuner address to %x\n", tuner_addrs);
  497. }
  498. priv->tda827x_addr = tuner_addrs;
  499. msg.addr = tuner_addrs;
  500. analog_ops->i2c_gate_ctrl(fe, 1);
  501. ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
  502. if (ret != 1) {
  503. tuner_warn("tuner access failed!\n");
  504. analog_ops->i2c_gate_ctrl(fe, 0);
  505. return -EREMOTEIO;
  506. }
  507. if ((data == 0x83) || (data == 0x84)) {
  508. priv->ver |= TDA18271;
  509. tda829x_tda18271_config.config = priv->cfg.config;
  510. dvb_attach(tda18271_attach, fe, priv->tda827x_addr,
  511. priv->i2c_props.adap, &tda829x_tda18271_config);
  512. } else {
  513. if ((data & 0x3c) == 0)
  514. priv->ver |= TDA8275;
  515. else
  516. priv->ver |= TDA8275A;
  517. dvb_attach(tda827x_attach, fe, priv->tda827x_addr,
  518. priv->i2c_props.adap, &priv->cfg);
  519. priv->cfg.switch_addr = priv->i2c_props.addr;
  520. }
  521. if (fe->ops.tuner_ops.init)
  522. fe->ops.tuner_ops.init(fe);
  523. if (fe->ops.tuner_ops.sleep)
  524. fe->ops.tuner_ops.sleep(fe);
  525. analog_ops->i2c_gate_ctrl(fe, 0);
  526. return 0;
  527. }
  528. static int tda8290_probe(struct tuner_i2c_props *i2c_props)
  529. {
  530. #define TDA8290_ID 0x89
  531. unsigned char tda8290_id[] = { 0x1f, 0x00 };
  532. /* detect tda8290 */
  533. tuner_i2c_xfer_send(i2c_props, &tda8290_id[0], 1);
  534. tuner_i2c_xfer_recv(i2c_props, &tda8290_id[1], 1);
  535. if (tda8290_id[1] == TDA8290_ID) {
  536. if (debug)
  537. printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
  538. __func__, i2c_adapter_id(i2c_props->adap),
  539. i2c_props->addr);
  540. return 0;
  541. }
  542. return -ENODEV;
  543. }
  544. static int tda8295_probe(struct tuner_i2c_props *i2c_props)
  545. {
  546. #define TDA8295_ID 0x8a
  547. #define TDA8295C2_ID 0x8b
  548. unsigned char tda8295_id[] = { 0x2f, 0x00 };
  549. /* detect tda8295 */
  550. tuner_i2c_xfer_send(i2c_props, &tda8295_id[0], 1);
  551. tuner_i2c_xfer_recv(i2c_props, &tda8295_id[1], 1);
  552. if ((tda8295_id[1] & 0xfe) == TDA8295_ID) {
  553. if (debug)
  554. printk(KERN_DEBUG "%s: %s detected @ %d-%04x\n",
  555. __func__, (tda8295_id[1] == TDA8295_ID) ?
  556. "tda8295c1" : "tda8295c2",
  557. i2c_adapter_id(i2c_props->adap),
  558. i2c_props->addr);
  559. return 0;
  560. }
  561. return -ENODEV;
  562. }
  563. static struct analog_demod_ops tda8290_ops = {
  564. .set_params = tda8290_set_params,
  565. .has_signal = tda8290_has_signal,
  566. .standby = tda8290_standby,
  567. .release = tda829x_release,
  568. .i2c_gate_ctrl = tda8290_i2c_bridge,
  569. };
  570. static struct analog_demod_ops tda8295_ops = {
  571. .set_params = tda8295_set_params,
  572. .has_signal = tda8295_has_signal,
  573. .standby = tda8295_standby,
  574. .release = tda829x_release,
  575. .i2c_gate_ctrl = tda8295_i2c_bridge,
  576. };
  577. struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
  578. struct i2c_adapter *i2c_adap, u8 i2c_addr,
  579. struct tda829x_config *cfg)
  580. {
  581. struct tda8290_priv *priv = NULL;
  582. char *name;
  583. priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
  584. if (priv == NULL)
  585. return NULL;
  586. fe->analog_demod_priv = priv;
  587. priv->i2c_props.addr = i2c_addr;
  588. priv->i2c_props.adap = i2c_adap;
  589. priv->i2c_props.name = "tda829x";
  590. if (cfg)
  591. priv->cfg.config = cfg->lna_cfg;
  592. if (tda8290_probe(&priv->i2c_props) == 0) {
  593. priv->ver = TDA8290;
  594. memcpy(&fe->ops.analog_ops, &tda8290_ops,
  595. sizeof(struct analog_demod_ops));
  596. }
  597. if (tda8295_probe(&priv->i2c_props) == 0) {
  598. priv->ver = TDA8295;
  599. memcpy(&fe->ops.analog_ops, &tda8295_ops,
  600. sizeof(struct analog_demod_ops));
  601. }
  602. if ((!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) &&
  603. (tda829x_find_tuner(fe) < 0))
  604. goto fail;
  605. switch (priv->ver) {
  606. case TDA8290:
  607. name = "tda8290";
  608. break;
  609. case TDA8295:
  610. name = "tda8295";
  611. break;
  612. case TDA8290 | TDA8275:
  613. name = "tda8290+75";
  614. break;
  615. case TDA8295 | TDA8275:
  616. name = "tda8295+75";
  617. break;
  618. case TDA8290 | TDA8275A:
  619. name = "tda8290+75a";
  620. break;
  621. case TDA8295 | TDA8275A:
  622. name = "tda8295+75a";
  623. break;
  624. case TDA8290 | TDA18271:
  625. name = "tda8290+18271";
  626. break;
  627. case TDA8295 | TDA18271:
  628. name = "tda8295+18271";
  629. break;
  630. default:
  631. goto fail;
  632. }
  633. tuner_info("type set to %s\n", name);
  634. fe->ops.analog_ops.info.name = name;
  635. if (priv->ver & TDA8290) {
  636. if (priv->ver & (TDA8275 | TDA8275A))
  637. tda8290_init_tuner(fe);
  638. tda8290_init_if(fe);
  639. } else if (priv->ver & TDA8295)
  640. tda8295_init_if(fe);
  641. return fe;
  642. fail:
  643. tda829x_release(fe);
  644. return NULL;
  645. }
  646. EXPORT_SYMBOL_GPL(tda829x_attach);
  647. int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
  648. {
  649. struct tuner_i2c_props i2c_props = {
  650. .adap = i2c_adap,
  651. .addr = i2c_addr,
  652. };
  653. unsigned char soft_reset[] = { 0x00, 0x00 };
  654. unsigned char easy_mode_b[] = { 0x01, 0x02 };
  655. unsigned char easy_mode_g[] = { 0x01, 0x04 };
  656. unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
  657. unsigned char addr_dto_lsb = 0x07;
  658. unsigned char data;
  659. #define PROBE_BUFFER_SIZE 8
  660. unsigned char buf[PROBE_BUFFER_SIZE];
  661. int i;
  662. /* rule out tda9887, which would return the same byte repeatedly */
  663. tuner_i2c_xfer_send(&i2c_props, soft_reset, 1);
  664. tuner_i2c_xfer_recv(&i2c_props, buf, PROBE_BUFFER_SIZE);
  665. for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
  666. if (buf[i] != buf[0])
  667. break;
  668. }
  669. /* all bytes are equal, not a tda829x - probably a tda9887 */
  670. if (i == PROBE_BUFFER_SIZE)
  671. return -ENODEV;
  672. if ((tda8290_probe(&i2c_props) == 0) ||
  673. (tda8295_probe(&i2c_props) == 0))
  674. return 0;
  675. /* fall back to old probing method */
  676. tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
  677. tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
  678. tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
  679. tuner_i2c_xfer_recv(&i2c_props, &data, 1);
  680. if (data == 0) {
  681. tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
  682. tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
  683. tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
  684. tuner_i2c_xfer_recv(&i2c_props, &data, 1);
  685. if (data == 0x7b) {
  686. return 0;
  687. }
  688. }
  689. tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
  690. return -ENODEV;
  691. }
  692. EXPORT_SYMBOL_GPL(tda829x_probe);
  693. MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
  694. MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
  695. MODULE_LICENSE("GPL");
  696. /*
  697. * Overrides for Emacs so that we follow Linus's tabbing style.
  698. * ---------------------------------------------------------------------------
  699. * Local variables:
  700. * c-basic-offset: 8
  701. * End:
  702. */