tda8290.c 22 KB

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