treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb / vp7045-fe.c
blobd253307a35f8a92205ea239d65d5ace2cea76da0
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.
5 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@posteo.de)
7 * Thanks to Twinhan who kindly provided hardware and information.
9 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
11 #include "vp7045.h"
13 /* It is a Zarlink MT352 within a Samsung Tuner (DNOS404ZH102A) - 040929 - AAT
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.
20 struct vp7045_fe_state {
21 struct dvb_frontend fe;
22 struct dvb_usb_device *d;
25 static int vp7045_fe_read_status(struct dvb_frontend *fe,
26 enum fe_status *status)
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);
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;
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;
49 return 0;
52 static int vp7045_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
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;
61 static int vp7045_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
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;
69 static int vp7045_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
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);
75 *strength = ~signal;
76 return 0;
79 static int vp7045_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
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;
87 static int vp7045_fe_init(struct dvb_frontend* fe)
89 return 0;
92 static int vp7045_fe_sleep(struct dvb_frontend* fe)
94 return 0;
97 static int vp7045_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
99 tune->min_delay_ms = 800;
100 return 0;
103 static int vp7045_fe_set_frontend(struct dvb_frontend *fe)
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;
110 buf[0] = (freq >> 16) & 0xff;
111 buf[1] = (freq >> 8) & 0xff;
112 buf[2] = freq & 0xff;
113 buf[3] = 0;
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;
129 vp7045_usb_op(state->d,LOCK_TUNER_COMMAND,buf,5,NULL,0,200);
130 return 0;
133 static void vp7045_fe_release(struct dvb_frontend* fe)
135 struct vp7045_fe_state *state = fe->demodulator_priv;
136 kfree(state);
139 static const struct dvb_frontend_ops vp7045_fe_ops;
141 struct dvb_frontend * vp7045_fe_attach(struct dvb_usb_device *d)
143 struct vp7045_fe_state *s = kzalloc(sizeof(struct vp7045_fe_state), GFP_KERNEL);
144 if (s == NULL)
145 goto error;
147 s->d = d;
148 memcpy(&s->fe.ops, &vp7045_fe_ops, sizeof(struct dvb_frontend_ops));
149 s->fe.demodulator_priv = s;
151 return &s->fe;
152 error:
153 return NULL;
157 static 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,
174 .release = vp7045_fe_release,
176 .init = vp7045_fe_init,
177 .sleep = vp7045_fe_sleep,
179 .set_frontend = vp7045_fe_set_frontend,
180 .get_tune_settings = vp7045_fe_get_tune_settings,
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,