1// SPDX-License-Identifier: GPL-2.0-only
2/* DVB frontend part of the Linux driver for TwinhanDTV Alpha/MagicBoxII USB2.0
3 * DVB-T receiver.
4 *
5 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@posteo.de)
6 *
7 * Thanks to Twinhan who kindly provided hardware and information.
8 *
9 * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
10 */
11#include "vp7045.h"
12
13/* It is a Zarlink MT352 within a Samsung Tuner (DNOS404ZH102A) - 040929 - AAT
14 *
15 * Programming is hidden inside the firmware, so set_frontend is very easy.
16 * Even though there is a Firmware command that one can use to access the demod
17 * via its registers. This is used for status information.
18 */
19
20struct vp7045_fe_state {
21	struct dvb_frontend fe;
22	struct dvb_usb_device *d;
23};
24
25static int vp7045_fe_read_status(struct dvb_frontend *fe,
26				 enum fe_status *status)
27{
28	struct vp7045_fe_state *state = fe->demodulator_priv;
29	u8 s0 = vp7045_read_reg(state->d,0x00),
30	   s1 = vp7045_read_reg(state->d,0x01),
31	   s3 = vp7045_read_reg(state->d,0x03);
32
33	*status = 0;
34	if (s0 & (1 << 4))
35		*status |= FE_HAS_CARRIER;
36	if (s0 & (1 << 1))
37		*status |= FE_HAS_VITERBI;
38	if (s0 & (1 << 5))
39		*status |= FE_HAS_LOCK;
40	if (s1 & (1 << 1))
41		*status |= FE_HAS_SYNC;
42	if (s3 & (1 << 6))
43		*status |= FE_HAS_SIGNAL;
44
45	if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
46			(FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
47		*status &= ~FE_HAS_LOCK;
48
49	return 0;
50}
51
52static int vp7045_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
53{
54	struct vp7045_fe_state *state = fe->demodulator_priv;
55	*ber = (vp7045_read_reg(state->d, 0x0D) << 16) |
56	       (vp7045_read_reg(state->d, 0x0E) << 8) |
57		vp7045_read_reg(state->d, 0x0F);
58	return 0;
59}
60
61static int vp7045_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
62{
63	struct vp7045_fe_state *state = fe->demodulator_priv;
64	*unc = (vp7045_read_reg(state->d, 0x10) << 8) |
65		    vp7045_read_reg(state->d, 0x11);
66	return 0;
67}
68
69static int vp7045_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
70{
71	struct vp7045_fe_state *state = fe->demodulator_priv;
72	u16 signal = (vp7045_read_reg(state->d, 0x14) << 8) |
73		vp7045_read_reg(state->d, 0x15);
74
75	*strength = ~signal;
76	return 0;
77}
78
79static int vp7045_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
80{
81	struct vp7045_fe_state *state = fe->demodulator_priv;
82	u8 _snr = vp7045_read_reg(state->d, 0x09);
83	*snr = (_snr << 8) | _snr;
84	return 0;
85}
86
87static int vp7045_fe_init(struct dvb_frontend* fe)
88{
89	return 0;
90}
91
92static int vp7045_fe_sleep(struct dvb_frontend* fe)
93{
94	return 0;
95}
96
97static int vp7045_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
98{
99	tune->min_delay_ms = 800;
100	return 0;
101}
102
103static int vp7045_fe_set_frontend(struct dvb_frontend *fe)
104{
105	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
106	struct vp7045_fe_state *state = fe->demodulator_priv;
107	u8 buf[5];
108	u32 freq = fep->frequency / 1000;
109
110	buf[0] = (freq >> 16) & 0xff;
111	buf[1] = (freq >>  8) & 0xff;
112	buf[2] =  freq        & 0xff;
113	buf[3] = 0;
114
115	switch (fep->bandwidth_hz) {
116	case 8000000:
117		buf[4] = 8;
118		break;
119	case 7000000:
120		buf[4] = 7;
121		break;
122	case 6000000:
123		buf[4] = 6;
124		break;
125	default:
126		return -EINVAL;
127	}
128
129	vp7045_usb_op(state->d,LOCK_TUNER_COMMAND,buf,5,NULL,0,200);
130	return 0;
131}
132
133static void vp7045_fe_release(struct dvb_frontend* fe)
134{
135	struct vp7045_fe_state *state = fe->demodulator_priv;
136	kfree(state);
137}
138
139static const struct dvb_frontend_ops vp7045_fe_ops;
140
141struct dvb_frontend * vp7045_fe_attach(struct dvb_usb_device *d)
142{
143	struct vp7045_fe_state *s = kzalloc(sizeof(struct vp7045_fe_state), GFP_KERNEL);
144	if (s == NULL)
145		goto error;
146
147	s->d = d;
148	memcpy(&s->fe.ops, &vp7045_fe_ops, sizeof(struct dvb_frontend_ops));
149	s->fe.demodulator_priv = s;
150
151	return &s->fe;
152error:
153	return NULL;
154}
155
156
157static const struct dvb_frontend_ops vp7045_fe_ops = {
158	.delsys = { SYS_DVBT },
159	.info = {
160		.name			= "Twinhan VP7045/46 USB DVB-T",
161		.frequency_min_hz	=  44250 * kHz,
162		.frequency_max_hz	= 867250 * kHz,
163		.frequency_stepsize_hz	=      1 * kHz,
164		.caps = FE_CAN_INVERSION_AUTO |
165				FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
166				FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
167				FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
168				FE_CAN_TRANSMISSION_MODE_AUTO |
169				FE_CAN_GUARD_INTERVAL_AUTO |
170				FE_CAN_RECOVER |
171				FE_CAN_HIERARCHY_AUTO,
172	},
173
174	.release = vp7045_fe_release,
175
176	.init = vp7045_fe_init,
177	.sleep = vp7045_fe_sleep,
178
179	.set_frontend = vp7045_fe_set_frontend,
180	.get_tune_settings = vp7045_fe_get_tune_settings,
181
182	.read_status = vp7045_fe_read_status,
183	.read_ber = vp7045_fe_read_ber,
184	.read_signal_strength = vp7045_fe_read_signal_strength,
185	.read_snr = vp7045_fe_read_snr,
186	.read_ucblocks = vp7045_fe_read_unc_blocks,
187};
188