Merge tag 'mediatek-drm-fixes-5.3' of https://github.com/ckhu-mediatek/linux.git...
[linux-2.6-block.git] / drivers / media / dvb-frontends / tda826x.c
CommitLineData
74ba9207 1// SPDX-License-Identifier: GPL-2.0-or-later
6bca3580
AQ
2 /*
3 Driver for Philips tda8262/tda8263 DVBS Silicon tuners
4
5 (c) 2006 Andrew de Quincey
6
6bca3580
AQ
7
8 */
9
5a0e3ad6 10#include <linux/slab.h>
6bca3580
AQ
11#include <linux/module.h>
12#include <linux/dvb/frontend.h>
13#include <asm/types.h>
14
15#include "tda826x.h"
16
ff699e6b 17static int debug;
8b9a4664
AQ
18#define dprintk(args...) \
19 do { \
20 if (debug) printk(KERN_DEBUG "tda826x: " args); \
21 } while (0)
22
6bca3580
AQ
23struct tda826x_priv {
24 /* i2c details */
25 int i2c_address;
26 struct i2c_adapter *i2c;
27 u8 has_loopthrough:1;
28 u32 frequency;
29};
30
f2709c20
MCC
31static void tda826x_release(struct dvb_frontend *fe)
32{
33 kfree(fe->tuner_priv);
34 fe->tuner_priv = NULL;
35}
36
6bca3580
AQ
37static 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
271ddbf7 44 dprintk("%s:\n", __func__);
8b9a4664 45
6bca3580
AQ
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) {
271ddbf7 52 dprintk("%s: i2c error\n", __func__);
6bca3580
AQ
53 }
54 if (fe->ops.i2c_gate_ctrl)
55 fe->ops.i2c_gate_ctrl(fe, 0);
6bca3580
AQ
56
57 return (ret == 1) ? 0 : ret;
58}
59
14d24d14 60static int tda826x_set_params(struct dvb_frontend *fe)
6bca3580 61{
66e6cd59 62 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6bca3580
AQ
63 struct tda826x_priv *priv = fe->tuner_priv;
64 int ret;
65 u32 div;
3ff9a81b
HH
66 u32 ksyms;
67 u32 bandwidth;
6bca3580
AQ
68 u8 buf [11];
69 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
70
271ddbf7 71 dprintk("%s:\n", __func__);
8b9a4664 72
66e6cd59 73 div = (p->frequency + (1000-1)) / 1000;
6bca3580 74
3ff9a81b
HH
75 /* BW = ((1 + RO) * SR/2 + 5) * 1.3 [SR in MSPS, BW in MHz] */
76 /* with R0 = 0.35 and some transformations: */
66e6cd59 77 ksyms = p->symbol_rate / 1000;
3ff9a81b
HH
78 bandwidth = (878 * ksyms + 6500000) / 1000000 + 1;
79 if (bandwidth < 5)
80 bandwidth = 5;
81 else if (bandwidth > 36)
82 bandwidth = 36;
83
6bca3580
AQ
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;
3ff9a81b 91 buf[5] = ((bandwidth - 5) << 3) | 7; /* baseband cut-off */
c6604150 92 buf[6] = 0xfe; // baseband gain 9 db + no RF attenuation
6bca3580
AQ
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) {
271ddbf7 101 dprintk("%s: i2c error\n", __func__);
6bca3580
AQ
102 }
103 if (fe->ops.i2c_gate_ctrl)
104 fe->ops.i2c_gate_ctrl(fe, 0);
105
106 priv->frequency = div * 1000;
6bca3580
AQ
107
108 return (ret == 1) ? 0 : ret;
109}
110
111static 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
14c4bf3c 118static const struct dvb_tuner_ops tda826x_tuner_ops = {
ef09c071
TP
119 .info = {
120 .name = "Philips TDA826X",
a3f90c75
MCC
121 .frequency_min_hz = 950 * MHz,
122 .frequency_max_hz = 2175 * MHz
ef09c071 123 },
f2709c20 124 .release = tda826x_release,
6bca3580
AQ
125 .sleep = tda826x_sleep,
126 .set_params = tda826x_set_params,
127 .get_frequency = tda826x_get_frequency,
128};
129
130struct 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 };
8949f1a2
PB
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 };
6bca3580
AQ
138 int ret;
139
271ddbf7 140 dprintk("%s:\n", __func__);
8b9a4664 141
6bca3580
AQ
142 if (fe->ops.i2c_gate_ctrl)
143 fe->ops.i2c_gate_ctrl(fe, 1);
8949f1a2 144 ret = i2c_transfer (i2c, msg, 2);
6bca3580
AQ
145 if (fe->ops.i2c_gate_ctrl)
146 fe->ops.i2c_gate_ctrl(fe, 0);
147
8949f1a2 148 if (ret != 2)
6bca3580
AQ
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));
6bca3580
AQ
162
163 fe->tuner_priv = priv;
6bca3580
AQ
164
165 return fe;
166}
167EXPORT_SYMBOL(tda826x_attach);
168
8b9a4664
AQ
169module_param(debug, int, 0644);
170MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
171
6bca3580
AQ
172MODULE_DESCRIPTION("DVB TDA826x driver");
173MODULE_AUTHOR("Andrew de Quincey");
174MODULE_LICENSE("GPL");