1// SPDX-License-Identifier: GPL-2.0-only
2/* Common methods for dibusb-based-receivers.
3 *
4 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
5 *
6 * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
7 */
8
9#include "dibusb.h"
10
11MODULE_LICENSE("GPL");
12
13/* 3000MC/P stuff */
14// Config Adjacent channels  Perf -cal22
15static struct dibx000_agc_config dib3000p_mt2060_agc_config = {
16	.band_caps = BAND_VHF | BAND_UHF,
17	.setup     = (1 << 8) | (5 << 5) | (1 << 4) | (1 << 3) | (0 << 2) | (2 << 0),
18
19	.agc1_max = 48497,
20	.agc1_min = 23593,
21	.agc2_max = 46531,
22	.agc2_min = 24904,
23
24	.agc1_pt1 = 0x65,
25	.agc1_pt2 = 0x69,
26
27	.agc1_slope1 = 0x51,
28	.agc1_slope2 = 0x27,
29
30	.agc2_pt1 = 0,
31	.agc2_pt2 = 0x33,
32
33	.agc2_slope1 = 0x35,
34	.agc2_slope2 = 0x37,
35};
36
37static struct dib3000mc_config stk3000p_dib3000p_config = {
38	&dib3000p_mt2060_agc_config,
39
40	.max_time     = 0x196,
41	.ln_adc_level = 0x1cc7,
42
43	.output_mpeg2_in_188_bytes = 1,
44
45	.agc_command1 = 1,
46	.agc_command2 = 1,
47};
48
49static struct dibx000_agc_config dib3000p_panasonic_agc_config = {
50	.band_caps = BAND_VHF | BAND_UHF,
51	.setup     = (1 << 8) | (5 << 5) | (1 << 4) | (1 << 3) | (0 << 2) | (2 << 0),
52
53	.agc1_max = 56361,
54	.agc1_min = 22282,
55	.agc2_max = 47841,
56	.agc2_min = 36045,
57
58	.agc1_pt1 = 0x3b,
59	.agc1_pt2 = 0x6b,
60
61	.agc1_slope1 = 0x55,
62	.agc1_slope2 = 0x1d,
63
64	.agc2_pt1 = 0,
65	.agc2_pt2 = 0x0a,
66
67	.agc2_slope1 = 0x95,
68	.agc2_slope2 = 0x1e,
69};
70
71static struct dib3000mc_config mod3000p_dib3000p_config = {
72	&dib3000p_panasonic_agc_config,
73
74	.max_time     = 0x51,
75	.ln_adc_level = 0x1cc7,
76
77	.output_mpeg2_in_188_bytes = 1,
78
79	.agc_command1 = 1,
80	.agc_command2 = 1,
81};
82
83int dibusb_dib3000mc_frontend_attach(struct dvb_usb_adapter *adap)
84{
85	if (le16_to_cpu(adap->dev->udev->descriptor.idVendor) == USB_VID_LITEON &&
86	    le16_to_cpu(adap->dev->udev->descriptor.idProduct) ==
87			USB_PID_LITEON_DVB_T_WARM) {
88		msleep(1000);
89	}
90
91	adap->fe_adap[0].fe = dvb_attach(dib3000mc_attach,
92					 &adap->dev->i2c_adap,
93					 DEFAULT_DIB3000P_I2C_ADDRESS,
94					 &mod3000p_dib3000p_config);
95	if ((adap->fe_adap[0].fe) == NULL)
96		adap->fe_adap[0].fe = dvb_attach(dib3000mc_attach,
97						 &adap->dev->i2c_adap,
98						 DEFAULT_DIB3000MC_I2C_ADDRESS,
99						 &mod3000p_dib3000p_config);
100	if ((adap->fe_adap[0].fe) != NULL) {
101		if (adap->priv != NULL) {
102			struct dibusb_state *st = adap->priv;
103			st->ops.pid_parse = dib3000mc_pid_parse;
104			st->ops.pid_ctrl  = dib3000mc_pid_control;
105		}
106		return 0;
107	}
108	return -ENODEV;
109}
110EXPORT_SYMBOL(dibusb_dib3000mc_frontend_attach);
111
112static struct mt2060_config stk3000p_mt2060_config = {
113	0x60
114};
115
116int dibusb_dib3000mc_tuner_attach(struct dvb_usb_adapter *adap)
117{
118	struct dibusb_state *st = adap->priv;
119	u8 a,b;
120	u16 if1 = 1220;
121	struct i2c_adapter *tun_i2c;
122
123	// First IF calibration for Liteon Sticks
124	if (le16_to_cpu(adap->dev->udev->descriptor.idVendor) == USB_VID_LITEON &&
125	    le16_to_cpu(adap->dev->udev->descriptor.idProduct) == USB_PID_LITEON_DVB_T_WARM) {
126
127		dibusb_read_eeprom_byte(adap->dev,0x7E,&a);
128		dibusb_read_eeprom_byte(adap->dev,0x7F,&b);
129
130		if (a == 0x00)
131			if1 += b;
132		else if (a == 0x80)
133			if1 -= b;
134		else
135			warn("LITE-ON DVB-T: Strange IF1 calibration :%2X %2X\n", a, b);
136
137	} else if (le16_to_cpu(adap->dev->udev->descriptor.idVendor) == USB_VID_DIBCOM &&
138		   le16_to_cpu(adap->dev->udev->descriptor.idProduct) == USB_PID_DIBCOM_MOD3001_WARM) {
139		u8 desc;
140		dibusb_read_eeprom_byte(adap->dev, 7, &desc);
141		if (desc == 2) {
142			a = 127;
143			do {
144				dibusb_read_eeprom_byte(adap->dev, a, &desc);
145				a--;
146			} while (a > 7 && (desc == 0xff || desc == 0x00));
147			if (desc & 0x80)
148				if1 -= (0xff - desc);
149			else
150				if1 += desc;
151		}
152	}
153
154	tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe_adap[0].fe, 1);
155	if (dvb_attach(mt2060_attach, adap->fe_adap[0].fe, tun_i2c, &stk3000p_mt2060_config, if1) == NULL) {
156		/* not found - use panasonic pll parameters */
157		if (dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60, tun_i2c, DVB_PLL_ENV57H1XD5) == NULL)
158			return -ENOMEM;
159	} else {
160		st->mt2060_present = 1;
161		/* set the correct parameters for the dib3000p */
162		dib3000mc_set_config(adap->fe_adap[0].fe, &stk3000p_dib3000p_config);
163	}
164	return 0;
165}
166EXPORT_SYMBOL(dibusb_dib3000mc_tuner_attach);
167