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