gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / media / dvb-frontends / tda8083.c
blob5be11fd65e3b153d8e31f8507a3b0654f487be24
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Driver for Philips TDA8083 based QPSK Demodulator
5 Copyright (C) 2001 Convergence Integrated Media GmbH
7 written by Ralph Metzler <ralph@convergence.de>
9 adoption to the new DVB frontend API and diagnostic ioctl's
10 by Holger Waechtler <holger@convergence.de>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <media/dvb_frontend.h>
22 #include "tda8083.h"
25 struct tda8083_state {
26 struct i2c_adapter* i2c;
27 /* configuration settings */
28 const struct tda8083_config* config;
29 struct dvb_frontend frontend;
32 static int debug;
33 #define dprintk(args...) \
34 do { \
35 if (debug) printk(KERN_DEBUG "tda8083: " args); \
36 } while (0)
39 static u8 tda8083_init_tab [] = {
40 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
41 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
42 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
43 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
44 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00
49 static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
51 int ret;
52 u8 buf [] = { reg, data };
53 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
55 ret = i2c_transfer(state->i2c, &msg, 1);
57 if (ret != 1)
58 dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
59 __func__, reg, ret);
61 return (ret != 1) ? -1 : 0;
64 static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
66 int ret;
67 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
68 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
70 ret = i2c_transfer(state->i2c, msg, 2);
72 if (ret != 2)
73 dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
74 __func__, reg1, ret);
76 return ret == 2 ? 0 : -1;
79 static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
81 u8 val;
83 tda8083_readregs (state, reg, &val, 1);
85 return val;
88 static int tda8083_set_inversion(struct tda8083_state *state,
89 enum fe_spectral_inversion inversion)
91 /* XXX FIXME: implement other modes than FEC_AUTO */
92 if (inversion == INVERSION_AUTO)
93 return 0;
95 return -EINVAL;
98 static int tda8083_set_fec(struct tda8083_state *state, enum fe_code_rate fec)
100 if (fec == FEC_AUTO)
101 return tda8083_writereg (state, 0x07, 0xff);
103 if (fec >= FEC_1_2 && fec <= FEC_8_9)
104 return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
106 return -EINVAL;
109 static enum fe_code_rate tda8083_get_fec(struct tda8083_state *state)
111 u8 index;
112 static enum fe_code_rate fec_tab[] = {
113 FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
114 FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8
117 index = tda8083_readreg(state, 0x0e) & 0x07;
119 return fec_tab [index];
122 static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
124 u32 ratio;
125 u32 tmp;
126 u8 filter;
128 if (srate > 32000000)
129 srate = 32000000;
130 if (srate < 500000)
131 srate = 500000;
133 filter = 0;
134 if (srate < 24000000)
135 filter = 2;
136 if (srate < 16000000)
137 filter = 3;
139 tmp = 31250 << 16;
140 ratio = tmp / srate;
142 tmp = (tmp % srate) << 8;
143 ratio = (ratio << 8) + tmp / srate;
145 tmp = (tmp % srate) << 8;
146 ratio = (ratio << 8) + tmp / srate;
148 dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
150 tda8083_writereg (state, 0x05, filter);
151 tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff);
152 tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff);
153 tda8083_writereg (state, 0x04, (ratio ) & 0xff);
155 tda8083_writereg (state, 0x00, 0x3c);
156 tda8083_writereg (state, 0x00, 0x04);
158 return 1;
161 static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
163 unsigned long start = jiffies;
165 while (jiffies - start < timeout &&
166 !(tda8083_readreg(state, 0x02) & 0x80))
168 msleep(50);
172 static int tda8083_set_tone(struct tda8083_state *state,
173 enum fe_sec_tone_mode tone)
175 tda8083_writereg (state, 0x26, 0xf1);
177 switch (tone) {
178 case SEC_TONE_OFF:
179 return tda8083_writereg (state, 0x29, 0x00);
180 case SEC_TONE_ON:
181 return tda8083_writereg (state, 0x29, 0x80);
182 default:
183 return -EINVAL;
187 static int tda8083_set_voltage(struct tda8083_state *state,
188 enum fe_sec_voltage voltage)
190 switch (voltage) {
191 case SEC_VOLTAGE_13:
192 return tda8083_writereg (state, 0x20, 0x00);
193 case SEC_VOLTAGE_18:
194 return tda8083_writereg (state, 0x20, 0x11);
195 default:
196 return -EINVAL;
200 static int tda8083_send_diseqc_burst(struct tda8083_state *state,
201 enum fe_sec_mini_cmd burst)
203 switch (burst) {
204 case SEC_MINI_A:
205 tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */
206 break;
207 case SEC_MINI_B:
208 tda8083_writereg (state, 0x29, (7 << 2)); /* send B */
209 break;
210 default:
211 return -EINVAL;
214 tda8083_wait_diseqc_fifo (state, 100);
216 return 0;
219 static int tda8083_send_diseqc_msg(struct dvb_frontend *fe,
220 struct dvb_diseqc_master_cmd *m)
222 struct tda8083_state* state = fe->demodulator_priv;
223 int i;
225 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
227 for (i=0; i<m->msg_len; i++)
228 tda8083_writereg (state, 0x23 + i, m->msg[i]);
230 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
232 tda8083_wait_diseqc_fifo (state, 100);
234 return 0;
237 static int tda8083_read_status(struct dvb_frontend *fe,
238 enum fe_status *status)
240 struct tda8083_state* state = fe->demodulator_priv;
242 u8 signal = ~tda8083_readreg (state, 0x01);
243 u8 sync = tda8083_readreg (state, 0x02);
245 *status = 0;
247 if (signal > 10)
248 *status |= FE_HAS_SIGNAL;
250 if (sync & 0x01)
251 *status |= FE_HAS_CARRIER;
253 if (sync & 0x02)
254 *status |= FE_HAS_VITERBI;
256 if (sync & 0x10)
257 *status |= FE_HAS_SYNC;
259 if (sync & 0x20) /* frontend can not lock */
260 *status |= FE_TIMEDOUT;
262 if ((sync & 0x1f) == 0x1f)
263 *status |= FE_HAS_LOCK;
265 return 0;
268 static int tda8083_read_ber(struct dvb_frontend* fe, u32* ber)
270 struct tda8083_state* state = fe->demodulator_priv;
271 int ret;
272 u8 buf[3];
274 if ((ret = tda8083_readregs(state, 0x0b, buf, sizeof(buf))))
275 return ret;
277 *ber = ((buf[0] & 0x1f) << 16) | (buf[1] << 8) | buf[2];
279 return 0;
282 static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
284 struct tda8083_state* state = fe->demodulator_priv;
286 u8 signal = ~tda8083_readreg (state, 0x01);
287 *strength = (signal << 8) | signal;
289 return 0;
292 static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
294 struct tda8083_state* state = fe->demodulator_priv;
296 u8 _snr = tda8083_readreg (state, 0x08);
297 *snr = (_snr << 8) | _snr;
299 return 0;
302 static int tda8083_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
304 struct tda8083_state* state = fe->demodulator_priv;
306 *ucblocks = tda8083_readreg(state, 0x0f);
307 if (*ucblocks == 0xff)
308 *ucblocks = 0xffffffff;
310 return 0;
313 static int tda8083_set_frontend(struct dvb_frontend *fe)
315 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
316 struct tda8083_state* state = fe->demodulator_priv;
318 if (fe->ops.tuner_ops.set_params) {
319 fe->ops.tuner_ops.set_params(fe);
320 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
323 tda8083_set_inversion (state, p->inversion);
324 tda8083_set_fec(state, p->fec_inner);
325 tda8083_set_symbolrate(state, p->symbol_rate);
327 tda8083_writereg (state, 0x00, 0x3c);
328 tda8083_writereg (state, 0x00, 0x04);
330 return 0;
333 static int tda8083_get_frontend(struct dvb_frontend *fe,
334 struct dtv_frontend_properties *p)
336 struct tda8083_state* state = fe->demodulator_priv;
338 /* FIXME: get symbolrate & frequency offset...*/
339 /*p->frequency = ???;*/
340 p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
341 INVERSION_ON : INVERSION_OFF;
342 p->fec_inner = tda8083_get_fec(state);
343 /*p->symbol_rate = tda8083_get_symbolrate (state);*/
345 return 0;
348 static int tda8083_sleep(struct dvb_frontend* fe)
350 struct tda8083_state* state = fe->demodulator_priv;
352 tda8083_writereg (state, 0x00, 0x02);
353 return 0;
356 static int tda8083_init(struct dvb_frontend* fe)
358 struct tda8083_state* state = fe->demodulator_priv;
359 int i;
361 for (i=0; i<44; i++)
362 tda8083_writereg (state, i, tda8083_init_tab[i]);
364 tda8083_writereg (state, 0x00, 0x3c);
365 tda8083_writereg (state, 0x00, 0x04);
367 return 0;
370 static int tda8083_diseqc_send_burst(struct dvb_frontend *fe,
371 enum fe_sec_mini_cmd burst)
373 struct tda8083_state* state = fe->demodulator_priv;
375 tda8083_send_diseqc_burst (state, burst);
376 tda8083_writereg (state, 0x00, 0x3c);
377 tda8083_writereg (state, 0x00, 0x04);
379 return 0;
382 static int tda8083_diseqc_set_tone(struct dvb_frontend *fe,
383 enum fe_sec_tone_mode tone)
385 struct tda8083_state* state = fe->demodulator_priv;
387 tda8083_set_tone (state, tone);
388 tda8083_writereg (state, 0x00, 0x3c);
389 tda8083_writereg (state, 0x00, 0x04);
391 return 0;
394 static int tda8083_diseqc_set_voltage(struct dvb_frontend *fe,
395 enum fe_sec_voltage voltage)
397 struct tda8083_state* state = fe->demodulator_priv;
399 tda8083_set_voltage (state, voltage);
400 tda8083_writereg (state, 0x00, 0x3c);
401 tda8083_writereg (state, 0x00, 0x04);
403 return 0;
406 static void tda8083_release(struct dvb_frontend* fe)
408 struct tda8083_state* state = fe->demodulator_priv;
409 kfree(state);
412 static const struct dvb_frontend_ops tda8083_ops;
414 struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
415 struct i2c_adapter* i2c)
417 struct tda8083_state* state = NULL;
419 /* allocate memory for the internal state */
420 state = kzalloc(sizeof(struct tda8083_state), GFP_KERNEL);
421 if (state == NULL) goto error;
423 /* setup the state */
424 state->config = config;
425 state->i2c = i2c;
427 /* check if the demod is there */
428 if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
430 /* create dvb_frontend */
431 memcpy(&state->frontend.ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
432 state->frontend.demodulator_priv = state;
433 return &state->frontend;
435 error:
436 kfree(state);
437 return NULL;
440 static const struct dvb_frontend_ops tda8083_ops = {
441 .delsys = { SYS_DVBS },
442 .info = {
443 .name = "Philips TDA8083 DVB-S",
444 .frequency_min_hz = 920 * MHz, /* TDA8060 */
445 .frequency_max_hz = 2200 * MHz, /* TDA8060 */
446 .frequency_stepsize_hz = 125 * kHz,
447 .symbol_rate_min = 12000000,
448 .symbol_rate_max = 30000000,
449 /* .symbol_rate_tolerance = ???,*/
450 .caps = FE_CAN_INVERSION_AUTO |
451 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
452 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
453 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
454 FE_CAN_QPSK | FE_CAN_MUTE_TS
457 .release = tda8083_release,
459 .init = tda8083_init,
460 .sleep = tda8083_sleep,
462 .set_frontend = tda8083_set_frontend,
463 .get_frontend = tda8083_get_frontend,
465 .read_status = tda8083_read_status,
466 .read_signal_strength = tda8083_read_signal_strength,
467 .read_snr = tda8083_read_snr,
468 .read_ber = tda8083_read_ber,
469 .read_ucblocks = tda8083_read_ucblocks,
471 .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
472 .diseqc_send_burst = tda8083_diseqc_send_burst,
473 .set_tone = tda8083_diseqc_set_tone,
474 .set_voltage = tda8083_diseqc_set_voltage,
477 module_param(debug, int, 0644);
478 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
480 MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
481 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
482 MODULE_LICENSE("GPL");
484 EXPORT_SYMBOL(tda8083_attach);