xref: /linux/drivers/media/pci/mantis/mantis_vp2040.c (revision 8795a739e5c72abeec51caf36b6df2b37e5720c5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 	Mantis VP-2040 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_vp2040.h"
24 
25 #define MANTIS_MODEL_NAME	"VP-2040"
26 #define MANTIS_DEV_TYPE		"DVB-C"
27 
28 static struct tda1002x_config vp2040_tda1002x_cu1216_config = {
29 	.demod_address	= 0x18 >> 1,
30 	.invert		= 1,
31 };
32 
33 static struct tda10023_config vp2040_tda10023_cu1216_config = {
34 	.demod_address	= 0x18 >> 1,
35 	.invert		= 1,
36 };
37 
38 static int tda1002x_cu1216_tuner_set(struct dvb_frontend *fe)
39 {
40 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
41 	struct mantis_pci *mantis	= fe->dvb->priv;
42 	struct i2c_adapter *adapter	= &mantis->adapter;
43 
44 	u8 buf[6];
45 	struct i2c_msg msg = {.addr = 0x60, .flags = 0, .buf = buf, .len = sizeof(buf)};
46 	int i;
47 
48 #define CU1216_IF 36125000
49 #define TUNER_MUL 62500
50 
51 	u32 div = (p->frequency + CU1216_IF + TUNER_MUL / 2) / TUNER_MUL;
52 
53 	buf[0] = (div >> 8) & 0x7f;
54 	buf[1] = div & 0xff;
55 	buf[2] = 0xce;
56 	buf[3] = (p->frequency < 150000000 ? 0x01 :
57 		  p->frequency < 445000000 ? 0x02 : 0x04);
58 	buf[4] = 0xde;
59 	buf[5] = 0x20;
60 
61 	if (fe->ops.i2c_gate_ctrl)
62 		fe->ops.i2c_gate_ctrl(fe, 1);
63 
64 	if (i2c_transfer(adapter, &msg, 1) != 1)
65 		return -EIO;
66 
67 	/* wait for the pll lock */
68 	msg.flags = I2C_M_RD;
69 	msg.len = 1;
70 	for (i = 0; i < 20; i++) {
71 		if (fe->ops.i2c_gate_ctrl)
72 			fe->ops.i2c_gate_ctrl(fe, 1);
73 
74 		if (i2c_transfer(adapter, &msg, 1) == 1 && (buf[0] & 0x40))
75 			break;
76 
77 		msleep(10);
78 	}
79 
80 	/* switch the charge pump to the lower current */
81 	msg.flags = 0;
82 	msg.len = 2;
83 	msg.buf = &buf[2];
84 	buf[2] &= ~0x40;
85 	if (fe->ops.i2c_gate_ctrl)
86 		fe->ops.i2c_gate_ctrl(fe, 1);
87 
88 	if (i2c_transfer(adapter, &msg, 1) != 1)
89 		return -EIO;
90 
91 	return 0;
92 }
93 
94 static u8 read_pwm(struct mantis_pci *mantis)
95 {
96 	struct i2c_adapter *adapter = &mantis->adapter;
97 
98 	u8 b = 0xff;
99 	u8 pwm;
100 	struct i2c_msg msg[] = {
101 		{.addr = 0x50, .flags = 0, .buf = &b, .len = 1},
102 		{.addr = 0x50, .flags = I2C_M_RD, .buf = &pwm, .len = 1}
103 	};
104 
105 	if ((i2c_transfer(adapter, msg, 2) != 2)
106 	    || (pwm == 0xff))
107 		pwm = 0x48;
108 
109 	return pwm;
110 }
111 
112 static int vp2040_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, &vp2040_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 				vp2040_tda1002x_cu1216_config.demod_address);
132 		} else {
133 			fe = dvb_attach(tda10023_attach, &vp2040_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 					vp2040_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 	mantis->fe = fe;
158 	dprintk(MANTIS_DEBUG, 1, "Done!");
159 
160 	return 0;
161 }
162 
163 struct mantis_hwconfig vp2040_config = {
164 	.model_name	= MANTIS_MODEL_NAME,
165 	.dev_type	= MANTIS_DEV_TYPE,
166 	.ts_size	= MANTIS_TS_204,
167 
168 	.baud_rate	= MANTIS_BAUD_9600,
169 	.parity		= MANTIS_PARITY_NONE,
170 	.bytes		= 0,
171 
172 	.frontend_init	= vp2040_frontend_init,
173 	.power		= GPIF_A12,
174 	.reset		= GPIF_A13,
175 };
176