1*74ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
225aee3deSMauro Carvalho Chehab /*
325aee3deSMauro Carvalho Chehab Mantis VP-2040 driver
425aee3deSMauro Carvalho Chehab
525aee3deSMauro Carvalho Chehab Copyright (C) Manu Abraham (abraham.manu@gmail.com)
625aee3deSMauro Carvalho Chehab
725aee3deSMauro Carvalho Chehab */
825aee3deSMauro Carvalho Chehab
925aee3deSMauro Carvalho Chehab #include <linux/signal.h>
1025aee3deSMauro Carvalho Chehab #include <linux/sched.h>
1125aee3deSMauro Carvalho Chehab #include <linux/interrupt.h>
1225aee3deSMauro Carvalho Chehab
13fada1935SMauro Carvalho Chehab #include <media/dmxdev.h>
14fada1935SMauro Carvalho Chehab #include <media/dvbdev.h>
15fada1935SMauro Carvalho Chehab #include <media/dvb_demux.h>
16fada1935SMauro Carvalho Chehab #include <media/dvb_frontend.h>
17fada1935SMauro Carvalho Chehab #include <media/dvb_net.h>
1825aee3deSMauro Carvalho Chehab
1925aee3deSMauro Carvalho Chehab #include "tda1002x.h"
2025aee3deSMauro Carvalho Chehab #include "mantis_common.h"
2125aee3deSMauro Carvalho Chehab #include "mantis_ioc.h"
2225aee3deSMauro Carvalho Chehab #include "mantis_dvb.h"
2325aee3deSMauro Carvalho Chehab #include "mantis_vp2040.h"
2425aee3deSMauro Carvalho Chehab
2525aee3deSMauro Carvalho Chehab #define MANTIS_MODEL_NAME "VP-2040"
2625aee3deSMauro Carvalho Chehab #define MANTIS_DEV_TYPE "DVB-C"
2725aee3deSMauro Carvalho Chehab
28967a3783SHans Verkuil static struct tda1002x_config vp2040_tda1002x_cu1216_config = {
2925aee3deSMauro Carvalho Chehab .demod_address = 0x18 >> 1,
3025aee3deSMauro Carvalho Chehab .invert = 1,
3125aee3deSMauro Carvalho Chehab };
3225aee3deSMauro Carvalho Chehab
33967a3783SHans Verkuil static struct tda10023_config vp2040_tda10023_cu1216_config = {
3425aee3deSMauro Carvalho Chehab .demod_address = 0x18 >> 1,
3525aee3deSMauro Carvalho Chehab .invert = 1,
3625aee3deSMauro Carvalho Chehab };
3725aee3deSMauro Carvalho Chehab
tda1002x_cu1216_tuner_set(struct dvb_frontend * fe)3825aee3deSMauro Carvalho Chehab static int tda1002x_cu1216_tuner_set(struct dvb_frontend *fe)
3925aee3deSMauro Carvalho Chehab {
4025aee3deSMauro Carvalho Chehab struct dtv_frontend_properties *p = &fe->dtv_property_cache;
4125aee3deSMauro Carvalho Chehab struct mantis_pci *mantis = fe->dvb->priv;
4225aee3deSMauro Carvalho Chehab struct i2c_adapter *adapter = &mantis->adapter;
4325aee3deSMauro Carvalho Chehab
4425aee3deSMauro Carvalho Chehab u8 buf[6];
4525aee3deSMauro Carvalho Chehab struct i2c_msg msg = {.addr = 0x60, .flags = 0, .buf = buf, .len = sizeof(buf)};
4625aee3deSMauro Carvalho Chehab int i;
4725aee3deSMauro Carvalho Chehab
4825aee3deSMauro Carvalho Chehab #define CU1216_IF 36125000
4925aee3deSMauro Carvalho Chehab #define TUNER_MUL 62500
5025aee3deSMauro Carvalho Chehab
5125aee3deSMauro Carvalho Chehab u32 div = (p->frequency + CU1216_IF + TUNER_MUL / 2) / TUNER_MUL;
5225aee3deSMauro Carvalho Chehab
5325aee3deSMauro Carvalho Chehab buf[0] = (div >> 8) & 0x7f;
5425aee3deSMauro Carvalho Chehab buf[1] = div & 0xff;
5525aee3deSMauro Carvalho Chehab buf[2] = 0xce;
5625aee3deSMauro Carvalho Chehab buf[3] = (p->frequency < 150000000 ? 0x01 :
5725aee3deSMauro Carvalho Chehab p->frequency < 445000000 ? 0x02 : 0x04);
5825aee3deSMauro Carvalho Chehab buf[4] = 0xde;
5925aee3deSMauro Carvalho Chehab buf[5] = 0x20;
6025aee3deSMauro Carvalho Chehab
6125aee3deSMauro Carvalho Chehab if (fe->ops.i2c_gate_ctrl)
6225aee3deSMauro Carvalho Chehab fe->ops.i2c_gate_ctrl(fe, 1);
6325aee3deSMauro Carvalho Chehab
6425aee3deSMauro Carvalho Chehab if (i2c_transfer(adapter, &msg, 1) != 1)
6525aee3deSMauro Carvalho Chehab return -EIO;
6625aee3deSMauro Carvalho Chehab
6725aee3deSMauro Carvalho Chehab /* wait for the pll lock */
6825aee3deSMauro Carvalho Chehab msg.flags = I2C_M_RD;
6925aee3deSMauro Carvalho Chehab msg.len = 1;
7025aee3deSMauro Carvalho Chehab for (i = 0; i < 20; i++) {
7125aee3deSMauro Carvalho Chehab if (fe->ops.i2c_gate_ctrl)
7225aee3deSMauro Carvalho Chehab fe->ops.i2c_gate_ctrl(fe, 1);
7325aee3deSMauro Carvalho Chehab
7425aee3deSMauro Carvalho Chehab if (i2c_transfer(adapter, &msg, 1) == 1 && (buf[0] & 0x40))
7525aee3deSMauro Carvalho Chehab break;
7625aee3deSMauro Carvalho Chehab
7725aee3deSMauro Carvalho Chehab msleep(10);
7825aee3deSMauro Carvalho Chehab }
7925aee3deSMauro Carvalho Chehab
8025aee3deSMauro Carvalho Chehab /* switch the charge pump to the lower current */
8125aee3deSMauro Carvalho Chehab msg.flags = 0;
8225aee3deSMauro Carvalho Chehab msg.len = 2;
8325aee3deSMauro Carvalho Chehab msg.buf = &buf[2];
8425aee3deSMauro Carvalho Chehab buf[2] &= ~0x40;
8525aee3deSMauro Carvalho Chehab if (fe->ops.i2c_gate_ctrl)
8625aee3deSMauro Carvalho Chehab fe->ops.i2c_gate_ctrl(fe, 1);
8725aee3deSMauro Carvalho Chehab
8825aee3deSMauro Carvalho Chehab if (i2c_transfer(adapter, &msg, 1) != 1)
8925aee3deSMauro Carvalho Chehab return -EIO;
9025aee3deSMauro Carvalho Chehab
9125aee3deSMauro Carvalho Chehab return 0;
9225aee3deSMauro Carvalho Chehab }
9325aee3deSMauro Carvalho Chehab
read_pwm(struct mantis_pci * mantis)9425aee3deSMauro Carvalho Chehab static u8 read_pwm(struct mantis_pci *mantis)
9525aee3deSMauro Carvalho Chehab {
9625aee3deSMauro Carvalho Chehab struct i2c_adapter *adapter = &mantis->adapter;
9725aee3deSMauro Carvalho Chehab
9825aee3deSMauro Carvalho Chehab u8 b = 0xff;
9925aee3deSMauro Carvalho Chehab u8 pwm;
10025aee3deSMauro Carvalho Chehab struct i2c_msg msg[] = {
10125aee3deSMauro Carvalho Chehab {.addr = 0x50, .flags = 0, .buf = &b, .len = 1},
10225aee3deSMauro Carvalho Chehab {.addr = 0x50, .flags = I2C_M_RD, .buf = &pwm, .len = 1}
10325aee3deSMauro Carvalho Chehab };
10425aee3deSMauro Carvalho Chehab
10525aee3deSMauro Carvalho Chehab if ((i2c_transfer(adapter, msg, 2) != 2)
10625aee3deSMauro Carvalho Chehab || (pwm == 0xff))
10725aee3deSMauro Carvalho Chehab pwm = 0x48;
10825aee3deSMauro Carvalho Chehab
10925aee3deSMauro Carvalho Chehab return pwm;
11025aee3deSMauro Carvalho Chehab }
11125aee3deSMauro Carvalho Chehab
vp2040_frontend_init(struct mantis_pci * mantis,struct dvb_frontend * fe)11225aee3deSMauro Carvalho Chehab static int vp2040_frontend_init(struct mantis_pci *mantis, struct dvb_frontend *fe)
11325aee3deSMauro Carvalho Chehab {
11425aee3deSMauro Carvalho Chehab struct i2c_adapter *adapter = &mantis->adapter;
11525aee3deSMauro Carvalho Chehab
11625aee3deSMauro Carvalho Chehab int err = 0;
11725aee3deSMauro Carvalho Chehab
11825aee3deSMauro Carvalho Chehab err = mantis_frontend_power(mantis, POWER_ON);
11925aee3deSMauro Carvalho Chehab if (err == 0) {
12025aee3deSMauro Carvalho Chehab mantis_frontend_soft_reset(mantis);
12125aee3deSMauro Carvalho Chehab msleep(250);
12225aee3deSMauro Carvalho Chehab
12325aee3deSMauro Carvalho Chehab dprintk(MANTIS_ERROR, 1, "Probing for CU1216 (DVB-C)");
12425aee3deSMauro Carvalho Chehab fe = dvb_attach(tda10021_attach, &vp2040_tda1002x_cu1216_config,
12525aee3deSMauro Carvalho Chehab adapter,
12625aee3deSMauro Carvalho Chehab read_pwm(mantis));
12725aee3deSMauro Carvalho Chehab
12825aee3deSMauro Carvalho Chehab if (fe) {
12925aee3deSMauro Carvalho Chehab dprintk(MANTIS_ERROR, 1,
13025aee3deSMauro Carvalho Chehab "found Philips CU1216 DVB-C frontend (TDA10021) @ 0x%02x",
13125aee3deSMauro Carvalho Chehab vp2040_tda1002x_cu1216_config.demod_address);
13225aee3deSMauro Carvalho Chehab } else {
13325aee3deSMauro Carvalho Chehab fe = dvb_attach(tda10023_attach, &vp2040_tda10023_cu1216_config,
13425aee3deSMauro Carvalho Chehab adapter,
13525aee3deSMauro Carvalho Chehab read_pwm(mantis));
13625aee3deSMauro Carvalho Chehab
13725aee3deSMauro Carvalho Chehab if (fe) {
13825aee3deSMauro Carvalho Chehab dprintk(MANTIS_ERROR, 1,
13925aee3deSMauro Carvalho Chehab "found Philips CU1216 DVB-C frontend (TDA10023) @ 0x%02x",
14025aee3deSMauro Carvalho Chehab vp2040_tda1002x_cu1216_config.demod_address);
14125aee3deSMauro Carvalho Chehab }
14225aee3deSMauro Carvalho Chehab }
14325aee3deSMauro Carvalho Chehab
14425aee3deSMauro Carvalho Chehab if (fe) {
14525aee3deSMauro Carvalho Chehab fe->ops.tuner_ops.set_params = tda1002x_cu1216_tuner_set;
14625aee3deSMauro Carvalho Chehab dprintk(MANTIS_ERROR, 1, "Mantis DVB-C Philips CU1216 frontend attach success");
14725aee3deSMauro Carvalho Chehab } else {
14825aee3deSMauro Carvalho Chehab return -1;
14925aee3deSMauro Carvalho Chehab }
15025aee3deSMauro Carvalho Chehab } else {
15125aee3deSMauro Carvalho Chehab dprintk(MANTIS_ERROR, 1, "Frontend on <%s> POWER ON failed! <%d>",
15225aee3deSMauro Carvalho Chehab adapter->name,
15325aee3deSMauro Carvalho Chehab err);
15425aee3deSMauro Carvalho Chehab
15525aee3deSMauro Carvalho Chehab return -EIO;
15625aee3deSMauro Carvalho Chehab }
15725aee3deSMauro Carvalho Chehab mantis->fe = fe;
15825aee3deSMauro Carvalho Chehab dprintk(MANTIS_DEBUG, 1, "Done!");
15925aee3deSMauro Carvalho Chehab
16025aee3deSMauro Carvalho Chehab return 0;
16125aee3deSMauro Carvalho Chehab }
16225aee3deSMauro Carvalho Chehab
16325aee3deSMauro Carvalho Chehab struct mantis_hwconfig vp2040_config = {
16425aee3deSMauro Carvalho Chehab .model_name = MANTIS_MODEL_NAME,
16525aee3deSMauro Carvalho Chehab .dev_type = MANTIS_DEV_TYPE,
16625aee3deSMauro Carvalho Chehab .ts_size = MANTIS_TS_204,
16725aee3deSMauro Carvalho Chehab
16825aee3deSMauro Carvalho Chehab .baud_rate = MANTIS_BAUD_9600,
16925aee3deSMauro Carvalho Chehab .parity = MANTIS_PARITY_NONE,
17025aee3deSMauro Carvalho Chehab .bytes = 0,
17125aee3deSMauro Carvalho Chehab
17225aee3deSMauro Carvalho Chehab .frontend_init = vp2040_frontend_init,
17325aee3deSMauro Carvalho Chehab .power = GPIF_A12,
17425aee3deSMauro Carvalho Chehab .reset = GPIF_A13,
17525aee3deSMauro Carvalho Chehab };
176