tda8290.c 24 KB

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