1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/dvb/frontend.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25
26#include "ix2505v.h"
27
28static int ix2505v_debug;
29#define dprintk(level, args...) do { \
30 if (ix2505v_debug & level) \
31 printk(KERN_DEBUG "ix2505v: " args); \
32} while (0)
33
34#define deb_info(args...) dprintk(0x01, args)
35#define deb_i2c(args...) dprintk(0x02, args)
36
37struct ix2505v_state {
38 struct i2c_adapter *i2c;
39 const struct ix2505v_config *config;
40 u32 frequency;
41};
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62static int ix2505v_read_status_reg(struct ix2505v_state *state)
63{
64 u8 addr = state->config->tuner_address;
65 u8 b2[] = {0};
66 int ret;
67
68 struct i2c_msg msg[1] = {
69 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 }
70 };
71
72 ret = i2c_transfer(state->i2c, msg, 1);
73 deb_i2c("Read %s ", __func__);
74
75 return (ret == 1) ? (int) b2[0] : -1;
76}
77
78static int ix2505v_write(struct ix2505v_state *state, u8 buf[], u8 count)
79{
80 struct i2c_msg msg[1] = {
81 { .addr = state->config->tuner_address, .flags = 0,
82 .buf = buf, .len = count },
83 };
84
85 int ret;
86
87 ret = i2c_transfer(state->i2c, msg, 1);
88
89 if (ret != 1) {
90 deb_i2c("%s: i2c error, ret=%d\n", __func__, ret);
91 return -EIO;
92 }
93
94 return 0;
95}
96
97static void ix2505v_release(struct dvb_frontend *fe)
98{
99 struct ix2505v_state *state = fe->tuner_priv;
100
101 fe->tuner_priv = NULL;
102 kfree(state);
103
104}
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131static int ix2505v_set_params(struct dvb_frontend *fe)
132{
133 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
134 struct ix2505v_state *state = fe->tuner_priv;
135 u32 frequency = c->frequency;
136 u32 b_w = (c->symbol_rate * 27) / 32000;
137 u32 div_factor, N , A, x;
138 int ret = 0, len;
139 u8 gain, cc, ref, psc, local_osc, lpf;
140 u8 data[4] = {0};
141
142 if ((frequency < fe->ops.info.frequency_min)
143 || (frequency > fe->ops.info.frequency_max))
144 return -EINVAL;
145
146 if (state->config->tuner_gain)
147 gain = (state->config->tuner_gain < 4)
148 ? state->config->tuner_gain : 0;
149 else
150 gain = 0x0;
151
152 if (state->config->tuner_chargepump)
153 cc = state->config->tuner_chargepump;
154 else
155 cc = 0x3;
156
157 ref = 8;
158 psc = 32;
159
160 div_factor = (frequency * ref) / 40;
161 x = div_factor / psc;
162 N = x/100;
163 A = ((x - (N * 100)) * psc) / 100;
164
165 data[0] = ((gain & 0x3) << 5) | (N >> 3);
166 data[1] = (N << 5) | (A & 0x1f);
167 data[2] = 0x81 | ((cc & 0x3) << 5) ;
168
169 deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A);
170
171 if (frequency <= 1065000)
172 local_osc = (6 << 5) | 2;
173 else if (frequency <= 1170000)
174 local_osc = (7 << 5) | 2;
175 else if (frequency <= 1300000)
176 local_osc = (1 << 5);
177 else if (frequency <= 1445000)
178 local_osc = (2 << 5);
179 else if (frequency <= 1607000)
180 local_osc = (3 << 5);
181 else if (frequency <= 1778000)
182 local_osc = (4 << 5);
183 else if (frequency <= 1942000)
184 local_osc = (5 << 5);
185 else
186 local_osc = (6 << 5);
187
188 data[3] = local_osc;
189
190 if (b_w <= 10000)
191 lpf = 0xc;
192 else if (b_w <= 12000)
193 lpf = 0x2;
194 else if (b_w <= 14000)
195 lpf = 0xa;
196 else if (b_w <= 16000)
197 lpf = 0x6;
198 else if (b_w <= 18000)
199 lpf = 0xe;
200 else if (b_w <= 20000)
201 lpf = 0x1;
202 else if (b_w <= 22000)
203 lpf = 0x9;
204 else if (b_w <= 24000)
205 lpf = 0x5;
206 else if (b_w <= 26000)
207 lpf = 0xd;
208 else if (b_w <= 28000)
209 lpf = 0x3;
210 else
211 lpf = 0xb;
212
213 deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf);
214 deb_info("Data 0=[%4phN]\n", data);
215
216 if (fe->ops.i2c_gate_ctrl)
217 fe->ops.i2c_gate_ctrl(fe, 1);
218
219 len = sizeof(data);
220 ret |= ix2505v_write(state, data, len);
221
222 data[2] |= 0x4;
223
224 if (fe->ops.i2c_gate_ctrl)
225 fe->ops.i2c_gate_ctrl(fe, 1);
226
227 len = 1;
228 ret |= ix2505v_write(state, &data[2], len);
229
230 msleep(10);
231
232 data[2] |= ((lpf >> 2) & 0x3) << 3;
233 data[3] |= (lpf & 0x3) << 2;
234
235 deb_info("Data 2=[%x%x]\n", data[2], data[3]);
236
237 if (fe->ops.i2c_gate_ctrl)
238 fe->ops.i2c_gate_ctrl(fe, 1);
239
240 len = 2;
241 ret |= ix2505v_write(state, &data[2], len);
242
243 if (state->config->min_delay_ms)
244 msleep(state->config->min_delay_ms);
245
246 state->frequency = frequency;
247
248 return ret;
249}
250
251static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency)
252{
253 struct ix2505v_state *state = fe->tuner_priv;
254
255 *frequency = state->frequency;
256
257 return 0;
258}
259
260static const struct dvb_tuner_ops ix2505v_tuner_ops = {
261 .info = {
262 .name = "Sharp IX2505V (B0017)",
263 .frequency_min = 950000,
264 .frequency_max = 2175000
265 },
266 .release = ix2505v_release,
267 .set_params = ix2505v_set_params,
268 .get_frequency = ix2505v_get_frequency,
269};
270
271struct dvb_frontend *ix2505v_attach(struct dvb_frontend *fe,
272 const struct ix2505v_config *config,
273 struct i2c_adapter *i2c)
274{
275 struct ix2505v_state *state = NULL;
276 int ret;
277
278 if (NULL == config) {
279 deb_i2c("%s: no config ", __func__);
280 goto error;
281 }
282
283 state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL);
284 if (NULL == state)
285 return NULL;
286
287 state->config = config;
288 state->i2c = i2c;
289
290 if (state->config->tuner_write_only) {
291 if (fe->ops.i2c_gate_ctrl)
292 fe->ops.i2c_gate_ctrl(fe, 1);
293
294 ret = ix2505v_read_status_reg(state);
295
296 if (ret & 0x80) {
297 deb_i2c("%s: No IX2505V found\n", __func__);
298 goto error;
299 }
300
301 if (fe->ops.i2c_gate_ctrl)
302 fe->ops.i2c_gate_ctrl(fe, 0);
303 }
304
305 fe->tuner_priv = state;
306
307 memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops,
308 sizeof(struct dvb_tuner_ops));
309 deb_i2c("%s: initialization (%s addr=0x%02x) ok\n",
310 __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
311
312 return fe;
313
314error:
315 kfree(state);
316 return NULL;
317}
318EXPORT_SYMBOL(ix2505v_attach);
319
320module_param_named(debug, ix2505v_debug, int, 0644);
321MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
322MODULE_DESCRIPTION("DVB IX2505V tuner driver");
323MODULE_AUTHOR("Malcolm Priestley");
324MODULE_LICENSE("GPL");
325