tda8290.c 22 KB

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