1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/dvb/frontend.h>
26#include <asm/types.h>
27
28#include "tda826x.h"
29
30static int debug;
31#define dprintk(args...) \
32 do { \
33 if (debug) printk(KERN_DEBUG "tda826x: " args); \
34 } while (0)
35
36struct tda826x_priv {
37
38 int i2c_address;
39 struct i2c_adapter *i2c;
40 u8 has_loopthrough:1;
41 u32 frequency;
42};
43
44static void tda826x_release(struct dvb_frontend *fe)
45{
46 kfree(fe->tuner_priv);
47 fe->tuner_priv = NULL;
48}
49
50static int tda826x_sleep(struct dvb_frontend *fe)
51{
52 struct tda826x_priv *priv = fe->tuner_priv;
53 int ret;
54 u8 buf [] = { 0x00, 0x8d };
55 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 };
56
57 dprintk("%s:\n", __func__);
58
59 if (!priv->has_loopthrough)
60 buf[1] = 0xad;
61
62 if (fe->ops.i2c_gate_ctrl)
63 fe->ops.i2c_gate_ctrl(fe, 1);
64 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
65 dprintk("%s: i2c error\n", __func__);
66 }
67 if (fe->ops.i2c_gate_ctrl)
68 fe->ops.i2c_gate_ctrl(fe, 0);
69
70 return (ret == 1) ? 0 : ret;
71}
72
73static int tda826x_set_params(struct dvb_frontend *fe)
74{
75 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
76 struct tda826x_priv *priv = fe->tuner_priv;
77 int ret;
78 u32 div;
79 u32 ksyms;
80 u32 bandwidth;
81 u8 buf [11];
82 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
83
84 dprintk("%s:\n", __func__);
85
86 div = (p->frequency + (1000-1)) / 1000;
87
88
89
90 ksyms = p->symbol_rate / 1000;
91 bandwidth = (878 * ksyms + 6500000) / 1000000 + 1;
92 if (bandwidth < 5)
93 bandwidth = 5;
94 else if (bandwidth > 36)
95 bandwidth = 36;
96
97 buf[0] = 0x00;
98 buf[1] = 0x09;
99 if (!priv->has_loopthrough)
100 buf[1] |= 0x20;
101 buf[2] = (1<<5) | 0x0b;
102 buf[3] = div >> 7;
103 buf[4] = div << 1;
104 buf[5] = ((bandwidth - 5) << 3) | 7;
105 buf[6] = 0xfe;
106 buf[7] = 0x83;
107 buf[8] = 0x80;
108 buf[9] = 0x1a;
109 buf[10] = 0xd4;
110
111 if (fe->ops.i2c_gate_ctrl)
112 fe->ops.i2c_gate_ctrl(fe, 1);
113 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
114 dprintk("%s: i2c error\n", __func__);
115 }
116 if (fe->ops.i2c_gate_ctrl)
117 fe->ops.i2c_gate_ctrl(fe, 0);
118
119 priv->frequency = div * 1000;
120
121 return (ret == 1) ? 0 : ret;
122}
123
124static int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
125{
126 struct tda826x_priv *priv = fe->tuner_priv;
127 *frequency = priv->frequency;
128 return 0;
129}
130
131static const struct dvb_tuner_ops tda826x_tuner_ops = {
132 .info = {
133 .name = "Philips TDA826X",
134 .frequency_min_hz = 950 * MHz,
135 .frequency_max_hz = 2175 * MHz
136 },
137 .release = tda826x_release,
138 .sleep = tda826x_sleep,
139 .set_params = tda826x_set_params,
140 .get_frequency = tda826x_get_frequency,
141};
142
143struct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough)
144{
145 struct tda826x_priv *priv = NULL;
146 u8 b1 [] = { 0, 0 };
147 struct i2c_msg msg[2] = {
148 { .addr = addr, .flags = 0, .buf = NULL, .len = 0 },
149 { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 }
150 };
151 int ret;
152
153 dprintk("%s:\n", __func__);
154
155 if (fe->ops.i2c_gate_ctrl)
156 fe->ops.i2c_gate_ctrl(fe, 1);
157 ret = i2c_transfer (i2c, msg, 2);
158 if (fe->ops.i2c_gate_ctrl)
159 fe->ops.i2c_gate_ctrl(fe, 0);
160
161 if (ret != 2)
162 return NULL;
163 if (!(b1[1] & 0x80))
164 return NULL;
165
166 priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL);
167 if (priv == NULL)
168 return NULL;
169
170 priv->i2c_address = addr;
171 priv->i2c = i2c;
172 priv->has_loopthrough = has_loopthrough;
173
174 memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops));
175
176 fe->tuner_priv = priv;
177
178 return fe;
179}
180EXPORT_SYMBOL(tda826x_attach);
181
182module_param(debug, int, 0644);
183MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
184
185MODULE_DESCRIPTION("DVB TDA826x driver");
186MODULE_AUTHOR("Andrew de Quincey");
187MODULE_LICENSE("GPL");
188