1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kernel.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"
18 TDA9886 (PAL, SECAM, NTSC)
19 TDA9887 (PAL, SECAM, NTSC, FM Radio)
21 Used as part of several tuners
25 module_param(debug
, int, 0644);
26 MODULE_PARM_DESC(debug
, "enable verbose debug messages");
28 static DEFINE_MUTEX(tda9887_list_mutex
);
29 static LIST_HEAD(hybrid_tuner_instance_list
);
32 struct tuner_i2c_props i2c_props
;
33 struct list_head hybrid_tuner_instance_list
;
35 unsigned char data
[4];
44 /* ---------------------------------------------------------------------- */
56 /* ---------------------------------------------------------------------- */
63 #define cVideoTrapBypassOFF 0x00 // bit b0
64 #define cVideoTrapBypassON 0x01 // bit b0
66 #define cAutoMuteFmInactive 0x00 // bit b1
67 #define cAutoMuteFmActive 0x02 // bit b1
69 #define cIntercarrier 0x00 // bit b2
70 #define cQSS 0x04 // bit b2
72 #define cPositiveAmTV 0x00 // bit b3:4
73 #define cFmRadio 0x08 // bit b3:4
74 #define cNegativeFmTV 0x10 // bit b3:4
77 #define cForcedMuteAudioON 0x20 // bit b5
78 #define cForcedMuteAudioOFF 0x00 // bit b5
80 #define cOutputPort1Active 0x00 // bit b6
81 #define cOutputPort1Inactive 0x40 // bit b6
83 #define cOutputPort2Active 0x00 // bit b7
84 #define cOutputPort2Inactive 0x80 // bit b7
88 #define cDeemphasisOFF 0x00 // bit c5
89 #define cDeemphasisON 0x20 // bit c5
91 #define cDeemphasis75 0x00 // bit c6
92 #define cDeemphasis50 0x40 // bit c6
94 #define cAudioGain0 0x00 // bit c7
95 #define cAudioGain6 0x80 // bit c7
97 #define cTopMask 0x1f // bit c0:4
98 #define cTopDefault 0x10 // bit c0:4
101 #define cAudioIF_4_5 0x00 // bit e0:1
102 #define cAudioIF_5_5 0x01 // bit e0:1
103 #define cAudioIF_6_0 0x02 // bit e0:1
104 #define cAudioIF_6_5 0x03 // bit e0:1
107 #define cVideoIFMask 0x1c // bit e2:4
108 /* Video IF selection in TV Mode (bit B3=0) */
109 #define cVideoIF_58_75 0x00 // bit e2:4
110 #define cVideoIF_45_75 0x04 // bit e2:4
111 #define cVideoIF_38_90 0x08 // bit e2:4
112 #define cVideoIF_38_00 0x0C // bit e2:4
113 #define cVideoIF_33_90 0x10 // bit e2:4
114 #define cVideoIF_33_40 0x14 // bit e2:4
115 #define cRadioIF_45_75 0x18 // bit e2:4
116 #define cRadioIF_38_90 0x1C // bit e2:4
118 /* IF1 selection in Radio Mode (bit B3=1) */
119 #define cRadioIF_33_30 0x00 // bit e2,4 (also 0x10,0x14)
120 #define cRadioIF_41_30 0x04 // bit e2,4
122 /* Output of AFC pin in radio mode when bit E7=1 */
123 #define cRadioAGC_SIF 0x00 // bit e3
124 #define cRadioAGC_FM 0x08 // bit e3
126 #define cTunerGainNormal 0x00 // bit e5
127 #define cTunerGainLow 0x20 // bit e5
129 #define cGating_18 0x00 // bit e6
130 #define cGating_36 0x40 // bit e6
132 #define cAgcOutON 0x80 // bit e7
133 #define cAgcOutOFF 0x00 // bit e7
135 /* ---------------------------------------------------------------------- */
137 static struct tvnorm tvnorms
[] = {
139 .std
= V4L2_STD_PAL_BG
| V4L2_STD_PAL_H
| V4L2_STD_PAL_N
,
141 .b
= ( cNegativeFmTV
|
143 .c
= ( cDeemphasisON
|
150 .std
= V4L2_STD_PAL_I
,
152 .b
= ( cNegativeFmTV
|
154 .c
= ( cDeemphasisON
|
161 .std
= V4L2_STD_PAL_DK
,
163 .b
= ( cNegativeFmTV
|
165 .c
= ( cDeemphasisON
|
172 .std
= V4L2_STD_PAL_M
| V4L2_STD_PAL_Nc
,
174 .b
= ( cNegativeFmTV
|
176 .c
= ( cDeemphasisON
|
183 .std
= V4L2_STD_SECAM_B
| V4L2_STD_SECAM_G
| V4L2_STD_SECAM_H
,
185 .b
= ( cNegativeFmTV
|
188 .e
= ( cAudioIF_5_5
|
191 .std
= V4L2_STD_SECAM_L
,
193 .b
= ( cPositiveAmTV
|
200 .std
= V4L2_STD_SECAM_LC
,
202 .b
= ( cOutputPort2Inactive
|
210 .std
= V4L2_STD_SECAM_DK
,
212 .b
= ( cNegativeFmTV
|
214 .c
= ( cDeemphasisON
|
221 .std
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
223 .b
= ( cNegativeFmTV
|
225 .c
= ( cDeemphasisON
|
232 .std
= V4L2_STD_NTSC_M_JP
,
234 .b
= ( cNegativeFmTV
|
236 .c
= ( cDeemphasisON
|
245 static struct tvnorm radio_stereo
= {
246 .name
= "Radio Stereo",
249 .c
= ( cDeemphasisOFF
|
252 .e
= ( cTunerGainLow
|
257 static struct tvnorm radio_mono
= {
258 .name
= "Radio Mono",
261 .c
= ( cDeemphasisON
|
264 .e
= ( cTunerGainLow
|
269 /* ---------------------------------------------------------------------- */
271 static void dump_read_message(struct dvb_frontend
*fe
, unsigned char *buf
)
273 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
275 static char *afc
[16] = {
293 tuner_info("read: 0x%2x\n", buf
[0]);
294 tuner_info(" after power on : %s\n", (buf
[0] & 0x01) ? "yes" : "no");
295 tuner_info(" afc : %s\n", afc
[(buf
[0] >> 1) & 0x0f]);
296 tuner_info(" fmif level : %s\n", (buf
[0] & 0x20) ? "high" : "low");
297 tuner_info(" afc window : %s\n", (buf
[0] & 0x40) ? "in" : "out");
298 tuner_info(" vfi level : %s\n", (buf
[0] & 0x80) ? "high" : "low");
301 static void dump_write_message(struct dvb_frontend
*fe
, unsigned char *buf
)
303 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
305 static char *sound
[4] = {
311 static char *adjust
[32] = {
312 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
313 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
314 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
315 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
317 static char *deemph
[4] = {
318 "no", "no", "75", "50"
320 static char *carrier
[4] = {
326 static char *vif
[8] = {
336 static char *rif
[4] = {
343 tuner_info("write: byte B 0x%02x\n", buf
[1]);
344 tuner_info(" B0 video mode : %s\n",
345 (buf
[1] & 0x01) ? "video trap" : "sound trap");
346 tuner_info(" B1 auto mute fm : %s\n",
347 (buf
[1] & 0x02) ? "yes" : "no");
348 tuner_info(" B2 carrier mode : %s\n",
349 (buf
[1] & 0x04) ? "QSS" : "Intercarrier");
350 tuner_info(" B3-4 tv sound/radio : %s\n",
351 sound
[(buf
[1] & 0x18) >> 3]);
352 tuner_info(" B5 force mute audio: %s\n",
353 (buf
[1] & 0x20) ? "yes" : "no");
354 tuner_info(" B6 output port 1 : %s\n",
355 (buf
[1] & 0x40) ? "high (inactive)" : "low (active)");
356 tuner_info(" B7 output port 2 : %s\n",
357 (buf
[1] & 0x80) ? "high (inactive)" : "low (active)");
359 tuner_info("write: byte C 0x%02x\n", buf
[2]);
360 tuner_info(" C0-4 top adjustment : %s dB\n",
361 adjust
[buf
[2] & 0x1f]);
362 tuner_info(" C5-6 de-emphasis : %s\n",
363 deemph
[(buf
[2] & 0x60) >> 5]);
364 tuner_info(" C7 audio gain : %s\n",
365 (buf
[2] & 0x80) ? "-6" : "0");
367 tuner_info("write: byte E 0x%02x\n", buf
[3]);
368 tuner_info(" E0-1 sound carrier : %s\n",
369 carrier
[(buf
[3] & 0x03)]);
370 tuner_info(" E6 l pll gating : %s\n",
371 (buf
[3] & 0x40) ? "36" : "13");
375 tuner_info(" E2-4 video if : %s\n",
376 rif
[(buf
[3] & 0x0c) >> 2]);
377 tuner_info(" E7 vif agc output : %s\n",
379 ? ((buf
[3] & 0x10) ? "fm-agc radio" :
381 : "fm radio carrier afc");
384 tuner_info(" E2-4 video if : %s\n",
385 vif
[(buf
[3] & 0x1c) >> 2]);
386 tuner_info(" E5 tuner gain : %s\n",
388 ? ((buf
[3] & 0x20) ? "external" : "normal")
389 : ((buf
[3] & 0x20) ? "minimum" : "normal"));
390 tuner_info(" E7 vif agc output : %s\n",
391 (buf
[3] & 0x80) ? ((buf
[3] & 0x20)
392 ? "pin3 port, pin22 vif agc out"
393 : "pin22 port, pin3 vif acg ext in")
394 : "pin3+pin22 port");
399 /* ---------------------------------------------------------------------- */
401 static int tda9887_set_tvnorm(struct dvb_frontend
*fe
)
403 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
404 struct tvnorm
*norm
= NULL
;
405 char *buf
= priv
->data
;
408 if (priv
->mode
== V4L2_TUNER_RADIO
) {
409 if (priv
->audmode
== V4L2_TUNER_MODE_MONO
)
412 norm
= &radio_stereo
;
414 for (i
= 0; i
< ARRAY_SIZE(tvnorms
); i
++) {
415 if (tvnorms
[i
].std
& priv
->std
) {
422 tuner_dbg("Unsupported tvnorm entry - audio muted\n");
426 tuner_dbg("configure for: %s\n", norm
->name
);
433 static unsigned int port1
= UNSET
;
434 static unsigned int port2
= UNSET
;
435 static unsigned int qss
= UNSET
;
436 static unsigned int adjust
= UNSET
;
438 module_param(port1
, int, 0644);
439 module_param(port2
, int, 0644);
440 module_param(qss
, int, 0644);
441 module_param(adjust
, int, 0644);
443 static int tda9887_set_insmod(struct dvb_frontend
*fe
)
445 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
446 char *buf
= priv
->data
;
448 if (UNSET
!= port1
) {
450 buf
[1] |= cOutputPort1Inactive
;
452 buf
[1] &= ~cOutputPort1Inactive
;
454 if (UNSET
!= port2
) {
456 buf
[1] |= cOutputPort2Inactive
;
458 buf
[1] &= ~cOutputPort2Inactive
;
475 static int tda9887_do_config(struct dvb_frontend
*fe
)
477 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
478 char *buf
= priv
->data
;
480 if (priv
->config
& TDA9887_PORT1_ACTIVE
)
481 buf
[1] &= ~cOutputPort1Inactive
;
482 if (priv
->config
& TDA9887_PORT1_INACTIVE
)
483 buf
[1] |= cOutputPort1Inactive
;
484 if (priv
->config
& TDA9887_PORT2_ACTIVE
)
485 buf
[1] &= ~cOutputPort2Inactive
;
486 if (priv
->config
& TDA9887_PORT2_INACTIVE
)
487 buf
[1] |= cOutputPort2Inactive
;
489 if (priv
->config
& TDA9887_QSS
)
491 if (priv
->config
& TDA9887_INTERCARRIER
)
494 if (priv
->config
& TDA9887_AUTOMUTE
)
495 buf
[1] |= cAutoMuteFmActive
;
496 if (priv
->config
& TDA9887_DEEMPHASIS_MASK
) {
498 switch (priv
->config
& TDA9887_DEEMPHASIS_MASK
) {
499 case TDA9887_DEEMPHASIS_NONE
:
500 buf
[2] |= cDeemphasisOFF
;
502 case TDA9887_DEEMPHASIS_50
:
503 buf
[2] |= cDeemphasisON
| cDeemphasis50
;
505 case TDA9887_DEEMPHASIS_75
:
506 buf
[2] |= cDeemphasisON
| cDeemphasis75
;
510 if (priv
->config
& TDA9887_TOP_SET
) {
512 buf
[2] |= (priv
->config
>> 8) & cTopMask
;
514 if ((priv
->config
& TDA9887_INTERCARRIER_NTSC
) &&
515 (priv
->std
& V4L2_STD_NTSC
))
517 if (priv
->config
& TDA9887_GATING_18
)
518 buf
[3] &= ~cGating_36
;
520 if (priv
->mode
== V4L2_TUNER_RADIO
) {
521 if (priv
->config
& TDA9887_RIF_41_3
) {
522 buf
[3] &= ~cVideoIFMask
;
523 buf
[3] |= cRadioIF_41_30
;
525 if (priv
->config
& TDA9887_GAIN_NORMAL
)
526 buf
[3] &= ~cTunerGainLow
;
532 /* ---------------------------------------------------------------------- */
534 static int tda9887_status(struct dvb_frontend
*fe
)
536 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
537 unsigned char buf
[1];
540 rc
= tuner_i2c_xfer_recv(&priv
->i2c_props
, buf
, 1);
542 tuner_info("i2c i/o error: rc == %d (should be 1)\n", rc
);
543 dump_read_message(fe
, buf
);
547 static void tda9887_configure(struct dvb_frontend
*fe
)
549 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
552 memset(priv
->data
,0,sizeof(priv
->data
));
553 tda9887_set_tvnorm(fe
);
555 /* A note on the port settings:
556 These settings tend to depend on the specifics of the board.
557 By default they are set to inactive (bit value 1) by this driver,
558 overwriting any changes made by the tvnorm. This means that it
559 is the responsibility of the module using the tda9887 to set
560 these values in case of changes in the tvnorm.
561 In many cases port 2 should be made active (0) when selecting
562 SECAM-L, and port 2 should remain inactive (1) for SECAM-L'.
564 For the other standards the tda9887 application note says that
565 the ports should be set to active (0), but, again, that may
566 differ depending on the precise hardware configuration.
568 priv
->data
[1] |= cOutputPort1Inactive
;
569 priv
->data
[1] |= cOutputPort2Inactive
;
571 tda9887_do_config(fe
);
572 tda9887_set_insmod(fe
);
575 priv
->data
[1] |= cForcedMuteAudioON
;
577 tuner_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n",
578 priv
->data
[1], priv
->data
[2], priv
->data
[3]);
580 dump_write_message(fe
, priv
->data
);
582 if (4 != (rc
= tuner_i2c_xfer_send(&priv
->i2c_props
,priv
->data
,4)))
583 tuner_info("i2c i/o error: rc == %d (should be 4)\n", rc
);
586 msleep_interruptible(1000);
591 /* ---------------------------------------------------------------------- */
593 static void tda9887_tuner_status(struct dvb_frontend
*fe
)
595 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
596 tuner_info("Data bytes: b=0x%02x c=0x%02x e=0x%02x\n",
597 priv
->data
[1], priv
->data
[2], priv
->data
[3]);
600 static int tda9887_get_afc(struct dvb_frontend
*fe
, s32
*afc
)
602 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
603 static const int AFC_BITS_2_kHz
[] = {
604 -12500, -37500, -62500, -97500,
605 -112500, -137500, -162500, -187500,
606 187500, 162500, 137500, 112500,
607 97500 , 62500, 37500 , 12500
611 if (priv
->mode
!= V4L2_TUNER_RADIO
)
613 if (1 == tuner_i2c_xfer_recv(&priv
->i2c_props
, ®
, 1))
614 *afc
= AFC_BITS_2_kHz
[(reg
>> 1) & 0x0f];
618 static void tda9887_standby(struct dvb_frontend
*fe
)
620 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
622 priv
->standby
= true;
624 tda9887_configure(fe
);
627 static void tda9887_set_params(struct dvb_frontend
*fe
,
628 struct analog_parameters
*params
)
630 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
632 priv
->standby
= false;
633 priv
->mode
= params
->mode
;
634 priv
->audmode
= params
->audmode
;
635 priv
->std
= params
->std
;
636 tda9887_configure(fe
);
639 static int tda9887_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
641 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
643 priv
->config
= *(unsigned int *)priv_cfg
;
644 tda9887_configure(fe
);
649 static void tda9887_release(struct dvb_frontend
*fe
)
651 struct tda9887_priv
*priv
= fe
->analog_demod_priv
;
653 mutex_lock(&tda9887_list_mutex
);
656 hybrid_tuner_release_state(priv
);
658 mutex_unlock(&tda9887_list_mutex
);
660 fe
->analog_demod_priv
= NULL
;
663 static const struct analog_demod_ops tda9887_ops
= {
667 .set_params
= tda9887_set_params
,
668 .standby
= tda9887_standby
,
669 .tuner_status
= tda9887_tuner_status
,
670 .get_afc
= tda9887_get_afc
,
671 .release
= tda9887_release
,
672 .set_config
= tda9887_set_config
,
675 struct dvb_frontend
*tda9887_attach(struct dvb_frontend
*fe
,
676 struct i2c_adapter
*i2c_adap
,
679 struct tda9887_priv
*priv
= NULL
;
682 mutex_lock(&tda9887_list_mutex
);
684 instance
= hybrid_tuner_request_state(struct tda9887_priv
, priv
,
685 hybrid_tuner_instance_list
,
686 i2c_adap
, i2c_addr
, "tda9887");
689 mutex_unlock(&tda9887_list_mutex
);
692 fe
->analog_demod_priv
= priv
;
693 priv
->standby
= true;
694 tuner_info("tda988[5/6/7] found\n");
697 fe
->analog_demod_priv
= priv
;
701 mutex_unlock(&tda9887_list_mutex
);
703 memcpy(&fe
->ops
.analog_ops
, &tda9887_ops
,
704 sizeof(struct analog_demod_ops
));
708 EXPORT_SYMBOL_GPL(tda9887_attach
);
710 MODULE_LICENSE("GPL");