tda8290.c 24 KB

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