1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Driver for Philips tda8262/tda8263 DVBS Silicon tuners
4
5 (c) 2006 Andrew de Quincey
6
7
8 */
9
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/dvb/frontend.h>
13 #include <asm/types.h>
14
15 #include "tda826x.h"
16
17 static int debug;
18 #define dprintk(args...) \
19 do { \
20 if (debug) printk(KERN_DEBUG "tda826x: " args); \
21 } while (0)
22
23 struct tda826x_priv {
24 /* i2c details */
25 int i2c_address;
26 struct i2c_adapter *i2c;
27 u8 has_loopthrough:1;
28 u32 frequency;
29 };
30
tda826x_release(struct dvb_frontend * fe)31 static void tda826x_release(struct dvb_frontend *fe)
32 {
33 kfree(fe->tuner_priv);
34 fe->tuner_priv = NULL;
35 }
36
tda826x_sleep(struct dvb_frontend * fe)37 static int tda826x_sleep(struct dvb_frontend *fe)
38 {
39 struct tda826x_priv *priv = fe->tuner_priv;
40 int ret;
41 u8 buf [] = { 0x00, 0x8d };
42 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 };
43
44 dprintk("%s:\n", __func__);
45
46 if (!priv->has_loopthrough)
47 buf[1] = 0xad;
48
49 if (fe->ops.i2c_gate_ctrl)
50 fe->ops.i2c_gate_ctrl(fe, 1);
51 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
52 dprintk("%s: i2c error\n", __func__);
53 }
54 if (fe->ops.i2c_gate_ctrl)
55 fe->ops.i2c_gate_ctrl(fe, 0);
56
57 return (ret == 1) ? 0 : ret;
58 }
59
tda826x_set_params(struct dvb_frontend * fe)60 static int tda826x_set_params(struct dvb_frontend *fe)
61 {
62 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
63 struct tda826x_priv *priv = fe->tuner_priv;
64 int ret;
65 u32 div;
66 u32 ksyms;
67 u32 bandwidth;
68 u8 buf [11];
69 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
70
71 dprintk("%s:\n", __func__);
72
73 div = (p->frequency + (1000-1)) / 1000;
74
75 /* BW = ((1 + RO) * SR/2 + 5) * 1.3 [SR in MSPS, BW in MHz] */
76 /* with R0 = 0.35 and some transformations: */
77 ksyms = p->symbol_rate / 1000;
78 bandwidth = (878 * ksyms + 6500000) / 1000000 + 1;
79 if (bandwidth < 5)
80 bandwidth = 5;
81 else if (bandwidth > 36)
82 bandwidth = 36;
83
84 buf[0] = 0x00; // subaddress
85 buf[1] = 0x09; // powerdown RSSI + the magic value 1
86 if (!priv->has_loopthrough)
87 buf[1] |= 0x20; // power down loopthrough if not needed
88 buf[2] = (1<<5) | 0x0b; // 1Mhz + 0.45 VCO
89 buf[3] = div >> 7;
90 buf[4] = div << 1;
91 buf[5] = ((bandwidth - 5) << 3) | 7; /* baseband cut-off */
92 buf[6] = 0xfe; // baseband gain 9 db + no RF attenuation
93 buf[7] = 0x83; // charge pumps at high, tests off
94 buf[8] = 0x80; // recommended value 4 for AMPVCO + disable ports.
95 buf[9] = 0x1a; // normal caltime + recommended values for SELTH + SELVTL
96 buf[10] = 0xd4; // recommended value 13 for BBIAS + unknown bit set on
97
98 if (fe->ops.i2c_gate_ctrl)
99 fe->ops.i2c_gate_ctrl(fe, 1);
100 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
101 dprintk("%s: i2c error\n", __func__);
102 }
103 if (fe->ops.i2c_gate_ctrl)
104 fe->ops.i2c_gate_ctrl(fe, 0);
105
106 priv->frequency = div * 1000;
107
108 return (ret == 1) ? 0 : ret;
109 }
110
tda826x_get_frequency(struct dvb_frontend * fe,u32 * frequency)111 static int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
112 {
113 struct tda826x_priv *priv = fe->tuner_priv;
114 *frequency = priv->frequency;
115 return 0;
116 }
117
118 static const struct dvb_tuner_ops tda826x_tuner_ops = {
119 .info = {
120 .name = "Philips TDA826X",
121 .frequency_min_hz = 950 * MHz,
122 .frequency_max_hz = 2175 * MHz
123 },
124 .release = tda826x_release,
125 .sleep = tda826x_sleep,
126 .set_params = tda826x_set_params,
127 .get_frequency = tda826x_get_frequency,
128 };
129
tda826x_attach(struct dvb_frontend * fe,int addr,struct i2c_adapter * i2c,int has_loopthrough)130 struct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough)
131 {
132 struct tda826x_priv *priv = NULL;
133 u8 b1 [] = { 0, 0 };
134 struct i2c_msg msg[2] = {
135 { .addr = addr, .flags = 0, .buf = NULL, .len = 0 },
136 { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 }
137 };
138 int ret;
139
140 dprintk("%s:\n", __func__);
141
142 if (fe->ops.i2c_gate_ctrl)
143 fe->ops.i2c_gate_ctrl(fe, 1);
144 ret = i2c_transfer (i2c, msg, 2);
145 if (fe->ops.i2c_gate_ctrl)
146 fe->ops.i2c_gate_ctrl(fe, 0);
147
148 if (ret != 2)
149 return NULL;
150 if (!(b1[1] & 0x80))
151 return NULL;
152
153 priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL);
154 if (priv == NULL)
155 return NULL;
156
157 priv->i2c_address = addr;
158 priv->i2c = i2c;
159 priv->has_loopthrough = has_loopthrough;
160
161 memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops));
162
163 fe->tuner_priv = priv;
164
165 return fe;
166 }
167 EXPORT_SYMBOL_GPL(tda826x_attach);
168
169 module_param(debug, int, 0644);
170 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
171
172 MODULE_DESCRIPTION("DVB TDA826x driver");
173 MODULE_AUTHOR("Andrew de Quincey");
174 MODULE_LICENSE("GPL");
175