tda8290.c 24 KB

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