tda9887.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/i2c.h>
  5. #include <linux/types.h>
  6. #include <linux/init.h>
  7. #include <linux/errno.h>
  8. #include <linux/delay.h>
  9. #include <linux/videodev2.h>
  10. #include <media/v4l2-common.h>
  11. #include <media/tuner.h>
  12. #include "tuner-i2c.h"
  13. #include "tda9887.h"
  14. /* Chips:
  15. TDA9885 (PAL, NTSC)
  16. TDA9886 (PAL, SECAM, NTSC)
  17. TDA9887 (PAL, SECAM, NTSC, FM Radio)
  18. Used as part of several tuners
  19. */
  20. static int debug;
  21. module_param(debug, int, 0644);
  22. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  23. static DEFINE_MUTEX(tda9887_list_mutex);
  24. static LIST_HEAD(hybrid_tuner_instance_list);
  25. struct tda9887_priv {
  26. struct tuner_i2c_props i2c_props;
  27. struct list_head hybrid_tuner_instance_list;
  28. unsigned char data[4];
  29. unsigned int config;
  30. unsigned int mode;
  31. unsigned int audmode;
  32. v4l2_std_id std;
  33. bool standby;
  34. };
  35. /* ---------------------------------------------------------------------- */
  36. #define UNSET (-1U)
  37. struct tvnorm {
  38. v4l2_std_id std;
  39. char *name;
  40. unsigned char b;
  41. unsigned char c;
  42. unsigned char e;
  43. };
  44. /* ---------------------------------------------------------------------- */
  45. //
  46. // TDA defines
  47. //
  48. //// first reg (b)
  49. #define cVideoTrapBypassOFF 0x00 // bit b0
  50. #define cVideoTrapBypassON 0x01 // bit b0
  51. #define cAutoMuteFmInactive 0x00 // bit b1
  52. #define cAutoMuteFmActive 0x02 // bit b1
  53. #define cIntercarrier 0x00 // bit b2
  54. #define cQSS 0x04 // bit b2
  55. #define cPositiveAmTV 0x00 // bit b3:4
  56. #define cFmRadio 0x08 // bit b3:4
  57. #define cNegativeFmTV 0x10 // bit b3:4
  58. #define cForcedMuteAudioON 0x20 // bit b5
  59. #define cForcedMuteAudioOFF 0x00 // bit b5
  60. #define cOutputPort1Active 0x00 // bit b6
  61. #define cOutputPort1Inactive 0x40 // bit b6
  62. #define cOutputPort2Active 0x00 // bit b7
  63. #define cOutputPort2Inactive 0x80 // bit b7
  64. //// second reg (c)
  65. #define cDeemphasisOFF 0x00 // bit c5
  66. #define cDeemphasisON 0x20 // bit c5
  67. #define cDeemphasis75 0x00 // bit c6
  68. #define cDeemphasis50 0x40 // bit c6
  69. #define cAudioGain0 0x00 // bit c7
  70. #define cAudioGain6 0x80 // bit c7
  71. #define cTopMask 0x1f // bit c0:4
  72. #define cTopDefault 0x10 // bit c0:4
  73. //// third reg (e)
  74. #define cAudioIF_4_5 0x00 // bit e0:1
  75. #define cAudioIF_5_5 0x01 // bit e0:1
  76. #define cAudioIF_6_0 0x02 // bit e0:1
  77. #define cAudioIF_6_5 0x03 // bit e0:1
  78. #define cVideoIFMask 0x1c // bit e2:4
  79. /* Video IF selection in TV Mode (bit B3=0) */
  80. #define cVideoIF_58_75 0x00 // bit e2:4
  81. #define cVideoIF_45_75 0x04 // bit e2:4
  82. #define cVideoIF_38_90 0x08 // bit e2:4
  83. #define cVideoIF_38_00 0x0C // bit e2:4
  84. #define cVideoIF_33_90 0x10 // bit e2:4
  85. #define cVideoIF_33_40 0x14 // bit e2:4
  86. #define cRadioIF_45_75 0x18 // bit e2:4
  87. #define cRadioIF_38_90 0x1C // bit e2:4
  88. /* IF1 selection in Radio Mode (bit B3=1) */
  89. #define cRadioIF_33_30 0x00 // bit e2,4 (also 0x10,0x14)
  90. #define cRadioIF_41_30 0x04 // bit e2,4
  91. /* Output of AFC pin in radio mode when bit E7=1 */
  92. #define cRadioAGC_SIF 0x00 // bit e3
  93. #define cRadioAGC_FM 0x08 // bit e3
  94. #define cTunerGainNormal 0x00 // bit e5
  95. #define cTunerGainLow 0x20 // bit e5
  96. #define cGating_18 0x00 // bit e6
  97. #define cGating_36 0x40 // bit e6
  98. #define cAgcOutON 0x80 // bit e7
  99. #define cAgcOutOFF 0x00 // bit e7
  100. /* ---------------------------------------------------------------------- */
  101. static struct tvnorm tvnorms[] = {
  102. {
  103. .std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H | V4L2_STD_PAL_N,
  104. .name = "PAL-BGHN",
  105. .b = ( cNegativeFmTV |
  106. cQSS ),
  107. .c = ( cDeemphasisON |
  108. cDeemphasis50 |
  109. cTopDefault),
  110. .e = ( cGating_36 |
  111. cAudioIF_5_5 |
  112. cVideoIF_38_90 ),
  113. },{
  114. .std = V4L2_STD_PAL_I,
  115. .name = "PAL-I",
  116. .b = ( cNegativeFmTV |
  117. cQSS ),
  118. .c = ( cDeemphasisON |
  119. cDeemphasis50 |
  120. cTopDefault),
  121. .e = ( cGating_36 |
  122. cAudioIF_6_0 |
  123. cVideoIF_38_90 ),
  124. },{
  125. .std = V4L2_STD_PAL_DK,
  126. .name = "PAL-DK",
  127. .b = ( cNegativeFmTV |
  128. cQSS ),
  129. .c = ( cDeemphasisON |
  130. cDeemphasis50 |
  131. cTopDefault),
  132. .e = ( cGating_36 |
  133. cAudioIF_6_5 |
  134. cVideoIF_38_90 ),
  135. },{
  136. .std = V4L2_STD_PAL_M | V4L2_STD_PAL_Nc,
  137. .name = "PAL-M/Nc",
  138. .b = ( cNegativeFmTV |
  139. cQSS ),
  140. .c = ( cDeemphasisON |
  141. cDeemphasis75 |
  142. cTopDefault),
  143. .e = ( cGating_36 |
  144. cAudioIF_4_5 |
  145. cVideoIF_45_75 ),
  146. },{
  147. .std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
  148. .name = "SECAM-BGH",
  149. .b = ( cNegativeFmTV |
  150. cQSS ),
  151. .c = ( cTopDefault),
  152. .e = ( cAudioIF_5_5 |
  153. cVideoIF_38_90 ),
  154. },{
  155. .std = V4L2_STD_SECAM_L,
  156. .name = "SECAM-L",
  157. .b = ( cPositiveAmTV |
  158. cQSS ),
  159. .c = ( cTopDefault),
  160. .e = ( cGating_36 |
  161. cAudioIF_6_5 |
  162. cVideoIF_38_90 ),
  163. },{
  164. .std = V4L2_STD_SECAM_LC,
  165. .name = "SECAM-L'",
  166. .b = ( cOutputPort2Inactive |
  167. cPositiveAmTV |
  168. cQSS ),
  169. .c = ( cTopDefault),
  170. .e = ( cGating_36 |
  171. cAudioIF_6_5 |
  172. cVideoIF_33_90 ),
  173. },{
  174. .std = V4L2_STD_SECAM_DK,
  175. .name = "SECAM-DK",
  176. .b = ( cNegativeFmTV |
  177. cQSS ),
  178. .c = ( cDeemphasisON |
  179. cDeemphasis50 |
  180. cTopDefault),
  181. .e = ( cGating_36 |
  182. cAudioIF_6_5 |
  183. cVideoIF_38_90 ),
  184. },{
  185. .std = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
  186. .name = "NTSC-M",
  187. .b = ( cNegativeFmTV |
  188. cQSS ),
  189. .c = ( cDeemphasisON |
  190. cDeemphasis75 |
  191. cTopDefault),
  192. .e = ( cGating_36 |
  193. cAudioIF_4_5 |
  194. cVideoIF_45_75 ),
  195. },{
  196. .std = V4L2_STD_NTSC_M_JP,
  197. .name = "NTSC-M-JP",
  198. .b = ( cNegativeFmTV |
  199. cQSS ),
  200. .c = ( cDeemphasisON |
  201. cDeemphasis50 |
  202. cTopDefault),
  203. .e = ( cGating_36 |
  204. cAudioIF_4_5 |
  205. cVideoIF_58_75 ),
  206. }
  207. };
  208. static struct tvnorm radio_stereo = {
  209. .name = "Radio Stereo",
  210. .b = ( cFmRadio |
  211. cQSS ),
  212. .c = ( cDeemphasisOFF |
  213. cAudioGain6 |
  214. cTopDefault),
  215. .e = ( cTunerGainLow |
  216. cAudioIF_5_5 |
  217. cRadioIF_38_90 ),
  218. };
  219. static struct tvnorm radio_mono = {
  220. .name = "Radio Mono",
  221. .b = ( cFmRadio |
  222. cQSS ),
  223. .c = ( cDeemphasisON |
  224. cDeemphasis75 |
  225. cTopDefault),
  226. .e = ( cTunerGainLow |
  227. cAudioIF_5_5 |
  228. cRadioIF_38_90 ),
  229. };
  230. /* ---------------------------------------------------------------------- */
  231. static void dump_read_message(struct dvb_frontend *fe, unsigned char *buf)
  232. {
  233. struct tda9887_priv *priv = fe->analog_demod_priv;
  234. static char *afc[16] = {
  235. "- 12.5 kHz",
  236. "- 37.5 kHz",
  237. "- 62.5 kHz",
  238. "- 87.5 kHz",
  239. "-112.5 kHz",
  240. "-137.5 kHz",
  241. "-162.5 kHz",
  242. "-187.5 kHz [min]",
  243. "+187.5 kHz [max]",
  244. "+162.5 kHz",
  245. "+137.5 kHz",
  246. "+112.5 kHz",
  247. "+ 87.5 kHz",
  248. "+ 62.5 kHz",
  249. "+ 37.5 kHz",
  250. "+ 12.5 kHz",
  251. };
  252. tuner_info("read: 0x%2x\n", buf[0]);
  253. tuner_info(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
  254. tuner_info(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
  255. tuner_info(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
  256. tuner_info(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
  257. tuner_info(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
  258. }
  259. static void dump_write_message(struct dvb_frontend *fe, unsigned char *buf)
  260. {
  261. struct tda9887_priv *priv = fe->analog_demod_priv;
  262. static char *sound[4] = {
  263. "AM/TV",
  264. "FM/radio",
  265. "FM/TV",
  266. "FM/radio"
  267. };
  268. static char *adjust[32] = {
  269. "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
  270. "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
  271. "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
  272. "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
  273. };
  274. static char *deemph[4] = {
  275. "no", "no", "75", "50"
  276. };
  277. static char *carrier[4] = {
  278. "4.5 MHz",
  279. "5.5 MHz",
  280. "6.0 MHz",
  281. "6.5 MHz / AM"
  282. };
  283. static char *vif[8] = {
  284. "58.75 MHz",
  285. "45.75 MHz",
  286. "38.9 MHz",
  287. "38.0 MHz",
  288. "33.9 MHz",
  289. "33.4 MHz",
  290. "45.75 MHz + pin13",
  291. "38.9 MHz + pin13",
  292. };
  293. static char *rif[4] = {
  294. "44 MHz",
  295. "52 MHz",
  296. "52 MHz",
  297. "44 MHz",
  298. };
  299. tuner_info("write: byte B 0x%02x\n", buf[1]);
  300. tuner_info(" B0 video mode : %s\n",
  301. (buf[1] & 0x01) ? "video trap" : "sound trap");
  302. tuner_info(" B1 auto mute fm : %s\n",
  303. (buf[1] & 0x02) ? "yes" : "no");
  304. tuner_info(" B2 carrier mode : %s\n",
  305. (buf[1] & 0x04) ? "QSS" : "Intercarrier");
  306. tuner_info(" B3-4 tv sound/radio : %s\n",
  307. sound[(buf[1] & 0x18) >> 3]);
  308. tuner_info(" B5 force mute audio: %s\n",
  309. (buf[1] & 0x20) ? "yes" : "no");
  310. tuner_info(" B6 output port 1 : %s\n",
  311. (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
  312. tuner_info(" B7 output port 2 : %s\n",
  313. (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
  314. tuner_info("write: byte C 0x%02x\n", buf[2]);
  315. tuner_info(" C0-4 top adjustment : %s dB\n",
  316. adjust[buf[2] & 0x1f]);
  317. tuner_info(" C5-6 de-emphasis : %s\n",
  318. deemph[(buf[2] & 0x60) >> 5]);
  319. tuner_info(" C7 audio gain : %s\n",
  320. (buf[2] & 0x80) ? "-6" : "0");
  321. tuner_info("write: byte E 0x%02x\n", buf[3]);
  322. tuner_info(" E0-1 sound carrier : %s\n",
  323. carrier[(buf[3] & 0x03)]);
  324. tuner_info(" E6 l pll gating : %s\n",
  325. (buf[3] & 0x40) ? "36" : "13");
  326. if (buf[1] & 0x08) {
  327. /* radio */
  328. tuner_info(" E2-4 video if : %s\n",
  329. rif[(buf[3] & 0x0c) >> 2]);
  330. tuner_info(" E7 vif agc output : %s\n",
  331. (buf[3] & 0x80)
  332. ? ((buf[3] & 0x10) ? "fm-agc radio" :
  333. "sif-agc radio")
  334. : "fm radio carrier afc");
  335. } else {
  336. /* video */
  337. tuner_info(" E2-4 video if : %s\n",
  338. vif[(buf[3] & 0x1c) >> 2]);
  339. tuner_info(" E5 tuner gain : %s\n",
  340. (buf[3] & 0x80)
  341. ? ((buf[3] & 0x20) ? "external" : "normal")
  342. : ((buf[3] & 0x20) ? "minimum" : "normal"));
  343. tuner_info(" E7 vif agc output : %s\n",
  344. (buf[3] & 0x80) ? ((buf[3] & 0x20)
  345. ? "pin3 port, pin22 vif agc out"
  346. : "pin22 port, pin3 vif acg ext in")
  347. : "pin3+pin22 port");
  348. }
  349. tuner_info("--\n");
  350. }
  351. /* ---------------------------------------------------------------------- */
  352. static int tda9887_set_tvnorm(struct dvb_frontend *fe)
  353. {
  354. struct tda9887_priv *priv = fe->analog_demod_priv;
  355. struct tvnorm *norm = NULL;
  356. char *buf = priv->data;
  357. int i;
  358. if (priv->mode == V4L2_TUNER_RADIO) {
  359. if (priv->audmode == V4L2_TUNER_MODE_MONO)
  360. norm = &radio_mono;
  361. else
  362. norm = &radio_stereo;
  363. } else {
  364. for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
  365. if (tvnorms[i].std & priv->std) {
  366. norm = tvnorms+i;
  367. break;
  368. }
  369. }
  370. }
  371. if (NULL == norm) {
  372. tuner_dbg("Unsupported tvnorm entry - audio muted\n");
  373. return -1;
  374. }
  375. tuner_dbg("configure for: %s\n", norm->name);
  376. buf[1] = norm->b;
  377. buf[2] = norm->c;
  378. buf[3] = norm->e;
  379. return 0;
  380. }
  381. static unsigned int port1 = UNSET;
  382. static unsigned int port2 = UNSET;
  383. static unsigned int qss = UNSET;
  384. static unsigned int adjust = UNSET;
  385. module_param(port1, int, 0644);
  386. module_param(port2, int, 0644);
  387. module_param(qss, int, 0644);
  388. module_param(adjust, int, 0644);
  389. static int tda9887_set_insmod(struct dvb_frontend *fe)
  390. {
  391. struct tda9887_priv *priv = fe->analog_demod_priv;
  392. char *buf = priv->data;
  393. if (UNSET != port1) {
  394. if (port1)
  395. buf[1] |= cOutputPort1Inactive;
  396. else
  397. buf[1] &= ~cOutputPort1Inactive;
  398. }
  399. if (UNSET != port2) {
  400. if (port2)
  401. buf[1] |= cOutputPort2Inactive;
  402. else
  403. buf[1] &= ~cOutputPort2Inactive;
  404. }
  405. if (UNSET != qss) {
  406. if (qss)
  407. buf[1] |= cQSS;
  408. else
  409. buf[1] &= ~cQSS;
  410. }
  411. if (adjust < 0x20) {
  412. buf[2] &= ~cTopMask;
  413. buf[2] |= adjust;
  414. }
  415. return 0;
  416. }
  417. static int tda9887_do_config(struct dvb_frontend *fe)
  418. {
  419. struct tda9887_priv *priv = fe->analog_demod_priv;
  420. char *buf = priv->data;
  421. if (priv->config & TDA9887_PORT1_ACTIVE)
  422. buf[1] &= ~cOutputPort1Inactive;
  423. if (priv->config & TDA9887_PORT1_INACTIVE)
  424. buf[1] |= cOutputPort1Inactive;
  425. if (priv->config & TDA9887_PORT2_ACTIVE)
  426. buf[1] &= ~cOutputPort2Inactive;
  427. if (priv->config & TDA9887_PORT2_INACTIVE)
  428. buf[1] |= cOutputPort2Inactive;
  429. if (priv->config & TDA9887_QSS)
  430. buf[1] |= cQSS;
  431. if (priv->config & TDA9887_INTERCARRIER)
  432. buf[1] &= ~cQSS;
  433. if (priv->config & TDA9887_AUTOMUTE)
  434. buf[1] |= cAutoMuteFmActive;
  435. if (priv->config & TDA9887_DEEMPHASIS_MASK) {
  436. buf[2] &= ~0x60;
  437. switch (priv->config & TDA9887_DEEMPHASIS_MASK) {
  438. case TDA9887_DEEMPHASIS_NONE:
  439. buf[2] |= cDeemphasisOFF;
  440. break;
  441. case TDA9887_DEEMPHASIS_50:
  442. buf[2] |= cDeemphasisON | cDeemphasis50;
  443. break;
  444. case TDA9887_DEEMPHASIS_75:
  445. buf[2] |= cDeemphasisON | cDeemphasis75;
  446. break;
  447. }
  448. }
  449. if (priv->config & TDA9887_TOP_SET) {
  450. buf[2] &= ~cTopMask;
  451. buf[2] |= (priv->config >> 8) & cTopMask;
  452. }
  453. if ((priv->config & TDA9887_INTERCARRIER_NTSC) &&
  454. (priv->std & V4L2_STD_NTSC))
  455. buf[1] &= ~cQSS;
  456. if (priv->config & TDA9887_GATING_18)
  457. buf[3] &= ~cGating_36;
  458. if (priv->mode == V4L2_TUNER_RADIO) {
  459. if (priv->config & TDA9887_RIF_41_3) {
  460. buf[3] &= ~cVideoIFMask;
  461. buf[3] |= cRadioIF_41_30;
  462. }
  463. if (priv->config & TDA9887_GAIN_NORMAL)
  464. buf[3] &= ~cTunerGainLow;
  465. }
  466. return 0;
  467. }
  468. /* ---------------------------------------------------------------------- */
  469. static int tda9887_status(struct dvb_frontend *fe)
  470. {
  471. struct tda9887_priv *priv = fe->analog_demod_priv;
  472. unsigned char buf[1];
  473. int rc;
  474. rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, 1);
  475. if (rc != 1)
  476. tuner_info("i2c i/o error: rc == %d (should be 1)\n", rc);
  477. dump_read_message(fe, buf);
  478. return 0;
  479. }
  480. static void tda9887_configure(struct dvb_frontend *fe)
  481. {
  482. struct tda9887_priv *priv = fe->analog_demod_priv;
  483. int rc;
  484. memset(priv->data,0,sizeof(priv->data));
  485. tda9887_set_tvnorm(fe);
  486. /* A note on the port settings:
  487. These settings tend to depend on the specifics of the board.
  488. By default they are set to inactive (bit value 1) by this driver,
  489. overwriting any changes made by the tvnorm. This means that it
  490. is the responsibility of the module using the tda9887 to set
  491. these values in case of changes in the tvnorm.
  492. In many cases port 2 should be made active (0) when selecting
  493. SECAM-L, and port 2 should remain inactive (1) for SECAM-L'.
  494. For the other standards the tda9887 application note says that
  495. the ports should be set to active (0), but, again, that may
  496. differ depending on the precise hardware configuration.
  497. */
  498. priv->data[1] |= cOutputPort1Inactive;
  499. priv->data[1] |= cOutputPort2Inactive;
  500. tda9887_do_config(fe);
  501. tda9887_set_insmod(fe);
  502. if (priv->standby)
  503. priv->data[1] |= cForcedMuteAudioON;
  504. tuner_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n",
  505. priv->data[1], priv->data[2], priv->data[3]);
  506. if (debug > 1)
  507. dump_write_message(fe, priv->data);
  508. if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,priv->data,4)))
  509. tuner_info("i2c i/o error: rc == %d (should be 4)\n", rc);
  510. if (debug > 2) {
  511. msleep_interruptible(1000);
  512. tda9887_status(fe);
  513. }
  514. }
  515. /* ---------------------------------------------------------------------- */
  516. static void tda9887_tuner_status(struct dvb_frontend *fe)
  517. {
  518. struct tda9887_priv *priv = fe->analog_demod_priv;
  519. tuner_info("Data bytes: b=0x%02x c=0x%02x e=0x%02x\n",
  520. priv->data[1], priv->data[2], priv->data[3]);
  521. }
  522. static int tda9887_get_afc(struct dvb_frontend *fe, s32 *afc)
  523. {
  524. struct tda9887_priv *priv = fe->analog_demod_priv;
  525. static const int AFC_BITS_2_kHz[] = {
  526. -12500, -37500, -62500, -97500,
  527. -112500, -137500, -162500, -187500,
  528. 187500, 162500, 137500, 112500,
  529. 97500 , 62500, 37500 , 12500
  530. };
  531. __u8 reg = 0;
  532. if (priv->mode != V4L2_TUNER_RADIO)
  533. return 0;
  534. if (1 == tuner_i2c_xfer_recv(&priv->i2c_props, &reg, 1))
  535. *afc = AFC_BITS_2_kHz[(reg >> 1) & 0x0f];
  536. return 0;
  537. }
  538. static void tda9887_standby(struct dvb_frontend *fe)
  539. {
  540. struct tda9887_priv *priv = fe->analog_demod_priv;
  541. priv->standby = true;
  542. tda9887_configure(fe);
  543. }
  544. static void tda9887_set_params(struct dvb_frontend *fe,
  545. struct analog_parameters *params)
  546. {
  547. struct tda9887_priv *priv = fe->analog_demod_priv;
  548. priv->standby = false;
  549. priv->mode = params->mode;
  550. priv->audmode = params->audmode;
  551. priv->std = params->std;
  552. tda9887_configure(fe);
  553. }
  554. static int tda9887_set_config(struct dvb_frontend *fe, void *priv_cfg)
  555. {
  556. struct tda9887_priv *priv = fe->analog_demod_priv;
  557. priv->config = *(unsigned int *)priv_cfg;
  558. tda9887_configure(fe);
  559. return 0;
  560. }
  561. static void tda9887_release(struct dvb_frontend *fe)
  562. {
  563. struct tda9887_priv *priv = fe->analog_demod_priv;
  564. mutex_lock(&tda9887_list_mutex);
  565. if (priv)
  566. hybrid_tuner_release_state(priv);
  567. mutex_unlock(&tda9887_list_mutex);
  568. fe->analog_demod_priv = NULL;
  569. }
  570. static const struct analog_demod_ops tda9887_ops = {
  571. .info = {
  572. .name = "tda9887",
  573. },
  574. .set_params = tda9887_set_params,
  575. .standby = tda9887_standby,
  576. .tuner_status = tda9887_tuner_status,
  577. .get_afc = tda9887_get_afc,
  578. .release = tda9887_release,
  579. .set_config = tda9887_set_config,
  580. };
  581. struct dvb_frontend *tda9887_attach(struct dvb_frontend *fe,
  582. struct i2c_adapter *i2c_adap,
  583. u8 i2c_addr)
  584. {
  585. struct tda9887_priv *priv = NULL;
  586. int instance;
  587. mutex_lock(&tda9887_list_mutex);
  588. instance = hybrid_tuner_request_state(struct tda9887_priv, priv,
  589. hybrid_tuner_instance_list,
  590. i2c_adap, i2c_addr, "tda9887");
  591. switch (instance) {
  592. case 0:
  593. mutex_unlock(&tda9887_list_mutex);
  594. return NULL;
  595. case 1:
  596. fe->analog_demod_priv = priv;
  597. priv->standby = true;
  598. tuner_info("tda988[5/6/7] found\n");
  599. break;
  600. default:
  601. fe->analog_demod_priv = priv;
  602. break;
  603. }
  604. mutex_unlock(&tda9887_list_mutex);
  605. memcpy(&fe->ops.analog_ops, &tda9887_ops,
  606. sizeof(struct analog_demod_ops));
  607. return fe;
  608. }
  609. EXPORT_SYMBOL_GPL(tda9887_attach);
  610. MODULE_LICENSE("GPL");