1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/string.h>
21#include <linux/slab.h>
22#include <linux/dvb/frontend.h>
23
24#include "dvb_frontend.h"
25#include "zl10039.h"
26
27static int debug;
28
29
30#define MAX_XFER_SIZE 64
31
32#define dprintk(args...) \
33 do { \
34 if (debug) \
35 printk(KERN_DEBUG args); \
36 } while (0)
37
38enum zl10039_model_id {
39 ID_ZL10039 = 1
40};
41
42struct zl10039_state {
43 struct i2c_adapter *i2c;
44 u8 i2c_addr;
45 u8 id;
46};
47
48enum zl10039_reg_addr {
49 PLL0 = 0,
50 PLL1,
51 PLL2,
52 PLL3,
53 RFFE,
54 BASE0,
55 BASE1,
56 BASE2,
57 LO0,
58 LO1,
59 LO2,
60 LO3,
61 LO4,
62 LO5,
63 LO6,
64 GENERAL
65};
66
67static int zl10039_read(const struct zl10039_state *state,
68 const enum zl10039_reg_addr reg, u8 *buf,
69 const size_t count)
70{
71 u8 regbuf[] = { reg };
72 struct i2c_msg msg[] = {
73 {
74 .addr = state->i2c_addr,
75 .flags = 0,
76 .buf = regbuf,
77 .len = 1,
78 }, {
79 .addr = state->i2c_addr,
80 .flags = I2C_M_RD,
81 .buf = buf,
82 .len = count,
83 },
84 };
85
86 dprintk("%s\n", __func__);
87
88 if (i2c_transfer(state->i2c, msg, 2) != 2) {
89 dprintk("%s: i2c read error\n", __func__);
90 return -EREMOTEIO;
91 }
92
93 return 0;
94}
95
96static int zl10039_write(struct zl10039_state *state,
97 const enum zl10039_reg_addr reg, const u8 *src,
98 const size_t count)
99{
100 u8 buf[MAX_XFER_SIZE];
101 struct i2c_msg msg = {
102 .addr = state->i2c_addr,
103 .flags = 0,
104 .buf = buf,
105 .len = count + 1,
106 };
107
108 if (1 + count > sizeof(buf)) {
109 printk(KERN_WARNING
110 "%s: i2c wr reg=%04x: len=%zu is too big!\n",
111 KBUILD_MODNAME, reg, count);
112 return -EINVAL;
113 }
114
115 dprintk("%s\n", __func__);
116
117 buf[0] = reg;
118 memcpy(&buf[1], src, count);
119 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
120 dprintk("%s: i2c write error\n", __func__);
121 return -EREMOTEIO;
122 }
123
124 return 0;
125}
126
127static inline int zl10039_readreg(struct zl10039_state *state,
128 const enum zl10039_reg_addr reg, u8 *val)
129{
130 return zl10039_read(state, reg, val, 1);
131}
132
133static inline int zl10039_writereg(struct zl10039_state *state,
134 const enum zl10039_reg_addr reg,
135 const u8 val)
136{
137 return zl10039_write(state, reg, &val, 1);
138}
139
140static int zl10039_init(struct dvb_frontend *fe)
141{
142 struct zl10039_state *state = fe->tuner_priv;
143 int ret;
144
145 dprintk("%s\n", __func__);
146 if (fe->ops.i2c_gate_ctrl)
147 fe->ops.i2c_gate_ctrl(fe, 1);
148
149 ret = zl10039_writereg(state, GENERAL, 0x40);
150 if (ret < 0) {
151 dprintk("Note: i2c write error normal when resetting the tuner\n");
152 }
153
154 ret = zl10039_writereg(state, GENERAL, 0x01);
155 if (ret < 0) {
156 dprintk("Tuner power up failed\n");
157 return ret;
158 }
159 if (fe->ops.i2c_gate_ctrl)
160 fe->ops.i2c_gate_ctrl(fe, 0);
161
162 return 0;
163}
164
165static int zl10039_sleep(struct dvb_frontend *fe)
166{
167 struct zl10039_state *state = fe->tuner_priv;
168 int ret;
169
170 dprintk("%s\n", __func__);
171 if (fe->ops.i2c_gate_ctrl)
172 fe->ops.i2c_gate_ctrl(fe, 1);
173 ret = zl10039_writereg(state, GENERAL, 0x80);
174 if (ret < 0) {
175 dprintk("Tuner sleep failed\n");
176 return ret;
177 }
178 if (fe->ops.i2c_gate_ctrl)
179 fe->ops.i2c_gate_ctrl(fe, 0);
180
181 return 0;
182}
183
184static int zl10039_set_params(struct dvb_frontend *fe)
185{
186 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
187 struct zl10039_state *state = fe->tuner_priv;
188 u8 buf[6];
189 u8 bf;
190 u32 fbw;
191 u32 div;
192 int ret;
193
194 dprintk("%s\n", __func__);
195 dprintk("Set frequency = %d, symbol rate = %d\n",
196 c->frequency, c->symbol_rate);
197
198
199
200 div = (c->frequency * 1000) / 126387;
201 fbw = (c->symbol_rate * 27) / 32000;
202
203 bf = ((fbw * 5088) / 1011100) - 1;
204
205
206 buf[0] = (div >> 8) & 0x7f;
207 buf[1] = (div >> 0) & 0xff;
208
209
210 buf[2] = 0x1D;
211
212 buf[3] = 0x40;
213
214 buf[4] = 0x6E;
215
216 buf[5] = bf;
217
218
219 if (fe->ops.i2c_gate_ctrl)
220 fe->ops.i2c_gate_ctrl(fe, 1);
221
222 ret = zl10039_writereg(state, BASE1, 0x0A);
223 if (ret < 0)
224 goto error;
225
226 ret = zl10039_write(state, PLL0, buf, sizeof(buf));
227 if (ret < 0)
228 goto error;
229
230 ret = zl10039_writereg(state, BASE1, 0x6A);
231 if (ret < 0)
232 goto error;
233
234
235 if (fe->ops.i2c_gate_ctrl)
236 fe->ops.i2c_gate_ctrl(fe, 0);
237 return 0;
238error:
239 dprintk("Error setting tuner\n");
240 return ret;
241}
242
243static void zl10039_release(struct dvb_frontend *fe)
244{
245 struct zl10039_state *state = fe->tuner_priv;
246
247 dprintk("%s\n", __func__);
248 kfree(state);
249 fe->tuner_priv = NULL;
250}
251
252static const struct dvb_tuner_ops zl10039_ops = {
253 .release = zl10039_release,
254 .init = zl10039_init,
255 .sleep = zl10039_sleep,
256 .set_params = zl10039_set_params,
257};
258
259struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
260 u8 i2c_addr, struct i2c_adapter *i2c)
261{
262 struct zl10039_state *state = NULL;
263
264 dprintk("%s\n", __func__);
265 state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
266 if (state == NULL)
267 goto error;
268
269 state->i2c = i2c;
270 state->i2c_addr = i2c_addr;
271
272
273 if (fe->ops.i2c_gate_ctrl)
274 fe->ops.i2c_gate_ctrl(fe, 1);
275
276 if (zl10039_readreg(state, GENERAL, &state->id) < 0) {
277
278 if (fe->ops.i2c_gate_ctrl)
279 fe->ops.i2c_gate_ctrl(fe, 0);
280 goto error;
281 }
282
283 if (fe->ops.i2c_gate_ctrl)
284 fe->ops.i2c_gate_ctrl(fe, 0);
285
286 state->id = state->id & 0x0f;
287 switch (state->id) {
288 case ID_ZL10039:
289 strcpy(fe->ops.tuner_ops.info.name,
290 "Zarlink ZL10039 DVB-S tuner");
291 break;
292 default:
293 dprintk("Chip ID=%x does not match a known type\n", state->id);
294 goto error;
295 }
296
297 memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops));
298 fe->tuner_priv = state;
299 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr);
300 return fe;
301error:
302 kfree(state);
303 return NULL;
304}
305EXPORT_SYMBOL(zl10039_attach);
306
307module_param(debug, int, 0644);
308MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
309MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
310MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
311MODULE_LICENSE("GPL");
312