xref: /linux/drivers/media/pci/mantis/mantis_vp2033.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 	Mantis VP-2033 driver
4 
5 	Copyright (C) Manu Abraham (abraham.manu@gmail.com)
6 
7 */
8 
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/interrupt.h>
12 
13 #include <media/dmxdev.h>
14 #include <media/dvbdev.h>
15 #include <media/dvb_demux.h>
16 #include <media/dvb_frontend.h>
17 #include <media/dvb_net.h>
18 
19 #include "tda1002x.h"
20 #include "mantis_common.h"
21 #include "mantis_ioc.h"
22 #include "mantis_dvb.h"
23 #include "mantis_vp2033.h"
24 
25 #define MANTIS_MODEL_NAME	"VP-2033"
26 #define MANTIS_DEV_TYPE		"DVB-C"
27 
28 static struct tda1002x_config vp2033_tda1002x_cu1216_config = {
29 	.demod_address = 0x18 >> 1,
30 	.invert = 1,
31 };
32 
33 static struct tda10023_config vp2033_tda10023_cu1216_config = {
34 	.demod_address = 0x18 >> 1,
35 	.invert = 1,
36 };
37 
38 static u8 read_pwm(struct mantis_pci *mantis)
39 {
40 	struct i2c_adapter *adapter = &mantis->adapter;
41 
42 	u8 b = 0xff;
43 	u8 pwm;
44 	struct i2c_msg msg[] = {
45 		{.addr = 0x50, .flags = 0, .buf = &b, .len = 1},
46 		{.addr = 0x50, .flags = I2C_M_RD, .buf = &pwm, .len = 1}
47 	};
48 
49 	if ((i2c_transfer(adapter, msg, 2) != 2)
50 	    || (pwm == 0xff))
51 		pwm = 0x48;
52 
53 	return pwm;
54 }
55 
56 static int tda1002x_cu1216_tuner_set(struct dvb_frontend *fe)
57 {
58 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
59 	struct mantis_pci *mantis = fe->dvb->priv;
60 	struct i2c_adapter *adapter = &mantis->adapter;
61 
62 	u8 buf[6];
63 	struct i2c_msg msg = {.addr = 0x60, .flags = 0, .buf = buf, .len = sizeof(buf)};
64 	int i;
65 
66 #define CU1216_IF 36125000
67 #define TUNER_MUL 62500
68 
69 	u32 div = (p->frequency + CU1216_IF + TUNER_MUL / 2) / TUNER_MUL;
70 
71 	buf[0] = (div >> 8) & 0x7f;
72 	buf[1] = div & 0xff;
73 	buf[2] = 0xce;
74 	buf[3] = (p->frequency < 150000000 ? 0x01 :
75 		  p->frequency < 445000000 ? 0x02 : 0x04);
76 	buf[4] = 0xde;
77 	buf[5] = 0x20;
78 
79 	if (fe->ops.i2c_gate_ctrl)
80 		fe->ops.i2c_gate_ctrl(fe, 1);
81 
82 	if (i2c_transfer(adapter, &msg, 1) != 1)
83 		return -EIO;
84 
85 	/* wait for the pll lock */
86 	msg.flags = I2C_M_RD;
87 	msg.len = 1;
88 	for (i = 0; i < 20; i++) {
89 		if (fe->ops.i2c_gate_ctrl)
90 			fe->ops.i2c_gate_ctrl(fe, 1);
91 
92 		if (i2c_transfer(adapter, &msg, 1) == 1 && (buf[0] & 0x40))
93 			break;
94 
95 		msleep(10);
96 	}
97 
98 	/* switch the charge pump to the lower current */
99 	msg.flags = 0;
100 	msg.len = 2;
101 	msg.buf = &buf[2];
102 	buf[2] &= ~0x40;
103 	if (fe->ops.i2c_gate_ctrl)
104 		fe->ops.i2c_gate_ctrl(fe, 1);
105 
106 	if (i2c_transfer(adapter, &msg, 1) != 1)
107 		return -EIO;
108 
109 	return 0;
110 }
111 
112 static int vp2033_frontend_init(struct mantis_pci *mantis, struct dvb_frontend *fe)
113 {
114 	struct i2c_adapter *adapter = &mantis->adapter;
115 
116 	int err = 0;
117 
118 	err = mantis_frontend_power(mantis, POWER_ON);
119 	if (err == 0) {
120 		mantis_frontend_soft_reset(mantis);
121 		msleep(250);
122 
123 		dprintk(MANTIS_ERROR, 1, "Probing for CU1216 (DVB-C)");
124 		fe = dvb_attach(tda10021_attach, &vp2033_tda1002x_cu1216_config,
125 				     adapter,
126 				     read_pwm(mantis));
127 
128 		if (fe) {
129 			dprintk(MANTIS_ERROR, 1,
130 				"found Philips CU1216 DVB-C frontend (TDA10021) @ 0x%02x",
131 				vp2033_tda1002x_cu1216_config.demod_address);
132 		} else {
133 			fe = dvb_attach(tda10023_attach, &vp2033_tda10023_cu1216_config,
134 					     adapter,
135 					     read_pwm(mantis));
136 
137 			if (fe) {
138 				dprintk(MANTIS_ERROR, 1,
139 					"found Philips CU1216 DVB-C frontend (TDA10023) @ 0x%02x",
140 					vp2033_tda1002x_cu1216_config.demod_address);
141 			}
142 		}
143 
144 		if (fe) {
145 			fe->ops.tuner_ops.set_params = tda1002x_cu1216_tuner_set;
146 			dprintk(MANTIS_ERROR, 1, "Mantis DVB-C Philips CU1216 frontend attach success");
147 		} else {
148 			return -1;
149 		}
150 	} else {
151 		dprintk(MANTIS_ERROR, 1, "Frontend on <%s> POWER ON failed! <%d>",
152 			adapter->name,
153 			err);
154 
155 		return -EIO;
156 	}
157 
158 	mantis->fe = fe;
159 	dprintk(MANTIS_DEBUG, 1, "Done!");
160 
161 	return 0;
162 }
163 
164 struct mantis_hwconfig vp2033_config = {
165 	.model_name	= MANTIS_MODEL_NAME,
166 	.dev_type	= MANTIS_DEV_TYPE,
167 	.ts_size	= MANTIS_TS_204,
168 
169 	.baud_rate	= MANTIS_BAUD_9600,
170 	.parity		= MANTIS_PARITY_NONE,
171 	.bytes		= 0,
172 
173 	.frontend_init	= vp2033_frontend_init,
174 	.power		= GPIF_A12,
175 	.reset		= GPIF_A13,
176 };
177