tda8290.c 25 KB

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