1 /*
2 * Driver for Zarlink ZL10039 DVB-S tuner
3 *
4 * Copyright 2007 Jan D. Louw <jd.louw@mweb.co.za>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/slab.h>
26 #include <linux/dvb/frontend.h>
27
28 #include "dvb_frontend.h"
29 #include "zl10039.h"
30
31 static int debug;
32
33 /* Max transfer size done by I2C transfer functions */
34 #define MAX_XFER_SIZE 64
35
36 #define dprintk(args...) \
37 do { \
38 if (debug) \
39 printk(KERN_DEBUG args); \
40 } while (0)
41
42 enum zl10039_model_id {
43 ID_ZL10039 = 1
44 };
45
46 struct zl10039_state {
47 struct i2c_adapter *i2c;
48 u8 i2c_addr;
49 u8 id;
50 };
51
52 enum zl10039_reg_addr {
53 PLL0 = 0,
54 PLL1,
55 PLL2,
56 PLL3,
57 RFFE,
58 BASE0,
59 BASE1,
60 BASE2,
61 LO0,
62 LO1,
63 LO2,
64 LO3,
65 LO4,
66 LO5,
67 LO6,
68 GENERAL
69 };
70
zl10039_read(const struct zl10039_state * state,const enum zl10039_reg_addr reg,u8 * buf,const size_t count)71 static int zl10039_read(const struct zl10039_state *state,
72 const enum zl10039_reg_addr reg, u8 *buf,
73 const size_t count)
74 {
75 u8 regbuf[] = { reg };
76 struct i2c_msg msg[] = {
77 {/* Write register address */
78 .addr = state->i2c_addr,
79 .flags = 0,
80 .buf = regbuf,
81 .len = 1,
82 }, {/* Read count bytes */
83 .addr = state->i2c_addr,
84 .flags = I2C_M_RD,
85 .buf = buf,
86 .len = count,
87 },
88 };
89
90 dprintk("%s\n", __func__);
91
92 if (i2c_transfer(state->i2c, msg, 2) != 2) {
93 dprintk("%s: i2c read error\n", __func__);
94 return -EREMOTEIO;
95 }
96
97 return 0; /* Success */
98 }
99
zl10039_write(struct zl10039_state * state,const enum zl10039_reg_addr reg,const u8 * src,const size_t count)100 static int zl10039_write(struct zl10039_state *state,
101 const enum zl10039_reg_addr reg, const u8 *src,
102 const size_t count)
103 {
104 u8 buf[MAX_XFER_SIZE];
105 struct i2c_msg msg = {
106 .addr = state->i2c_addr,
107 .flags = 0,
108 .buf = buf,
109 .len = count + 1,
110 };
111
112 if (1 + count > sizeof(buf)) {
113 printk(KERN_WARNING
114 "%s: i2c wr reg=%04x: len=%zu is too big!\n",
115 KBUILD_MODNAME, reg, count);
116 return -EINVAL;
117 }
118
119 dprintk("%s\n", __func__);
120 /* Write register address and data in one go */
121 buf[0] = reg;
122 memcpy(&buf[1], src, count);
123 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
124 dprintk("%s: i2c write error\n", __func__);
125 return -EREMOTEIO;
126 }
127
128 return 0; /* Success */
129 }
130
zl10039_readreg(struct zl10039_state * state,const enum zl10039_reg_addr reg,u8 * val)131 static inline int zl10039_readreg(struct zl10039_state *state,
132 const enum zl10039_reg_addr reg, u8 *val)
133 {
134 return zl10039_read(state, reg, val, 1);
135 }
136
zl10039_writereg(struct zl10039_state * state,const enum zl10039_reg_addr reg,const u8 val)137 static inline int zl10039_writereg(struct zl10039_state *state,
138 const enum zl10039_reg_addr reg,
139 const u8 val)
140 {
141 const u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
142
143 return zl10039_write(state, reg, &tmp, 1);
144 }
145
zl10039_init(struct dvb_frontend * fe)146 static int zl10039_init(struct dvb_frontend *fe)
147 {
148 struct zl10039_state *state = fe->tuner_priv;
149 int ret;
150
151 dprintk("%s\n", __func__);
152 if (fe->ops.i2c_gate_ctrl)
153 fe->ops.i2c_gate_ctrl(fe, 1);
154 /* Reset logic */
155 ret = zl10039_writereg(state, GENERAL, 0x40);
156 if (ret < 0) {
157 dprintk("Note: i2c write error normal when resetting the "
158 "tuner\n");
159 }
160 /* Wake up */
161 ret = zl10039_writereg(state, GENERAL, 0x01);
162 if (ret < 0) {
163 dprintk("Tuner power up failed\n");
164 return ret;
165 }
166 if (fe->ops.i2c_gate_ctrl)
167 fe->ops.i2c_gate_ctrl(fe, 0);
168
169 return 0;
170 }
171
zl10039_sleep(struct dvb_frontend * fe)172 static int zl10039_sleep(struct dvb_frontend *fe)
173 {
174 struct zl10039_state *state = fe->tuner_priv;
175 int ret;
176
177 dprintk("%s\n", __func__);
178 if (fe->ops.i2c_gate_ctrl)
179 fe->ops.i2c_gate_ctrl(fe, 1);
180 ret = zl10039_writereg(state, GENERAL, 0x80);
181 if (ret < 0) {
182 dprintk("Tuner sleep failed\n");
183 return ret;
184 }
185 if (fe->ops.i2c_gate_ctrl)
186 fe->ops.i2c_gate_ctrl(fe, 0);
187
188 return 0;
189 }
190
zl10039_set_params(struct dvb_frontend * fe)191 static int zl10039_set_params(struct dvb_frontend *fe)
192 {
193 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
194 struct zl10039_state *state = fe->tuner_priv;
195 u8 buf[6];
196 u8 bf;
197 u32 fbw;
198 u32 div;
199 int ret;
200
201 dprintk("%s\n", __func__);
202 dprintk("Set frequency = %d, symbol rate = %d\n",
203 c->frequency, c->symbol_rate);
204
205 /* Assumed 10.111 MHz crystal oscillator */
206 /* Cancelled num/den 80 to prevent overflow */
207 div = (c->frequency * 1000) / 126387;
208 fbw = (c->symbol_rate * 27) / 32000;
209 /* Cancelled num/den 10 to prevent overflow */
210 bf = ((fbw * 5088) / 1011100) - 1;
211
212 /*PLL divider*/
213 buf[0] = (div >> 8) & 0x7f;
214 buf[1] = (div >> 0) & 0xff;
215 /*Reference divider*/
216 /* Select reference ratio of 80 */
217 buf[2] = 0x1D;
218 /*PLL test modes*/
219 buf[3] = 0x40;
220 /*RF Control register*/
221 buf[4] = 0x6E; /* Bypass enable */
222 /*Baseband filter cutoff */
223 buf[5] = bf;
224
225 /* Open i2c gate */
226 if (fe->ops.i2c_gate_ctrl)
227 fe->ops.i2c_gate_ctrl(fe, 1);
228 /* BR = 10, Enable filter adjustment */
229 ret = zl10039_writereg(state, BASE1, 0x0A);
230 if (ret < 0)
231 goto error;
232 /* Write new config values */
233 ret = zl10039_write(state, PLL0, buf, sizeof(buf));
234 if (ret < 0)
235 goto error;
236 /* BR = 10, Disable filter adjustment */
237 ret = zl10039_writereg(state, BASE1, 0x6A);
238 if (ret < 0)
239 goto error;
240
241 /* Close i2c gate */
242 if (fe->ops.i2c_gate_ctrl)
243 fe->ops.i2c_gate_ctrl(fe, 0);
244 return 0;
245 error:
246 dprintk("Error setting tuner\n");
247 return ret;
248 }
249
zl10039_release(struct dvb_frontend * fe)250 static int zl10039_release(struct dvb_frontend *fe)
251 {
252 struct zl10039_state *state = fe->tuner_priv;
253
254 dprintk("%s\n", __func__);
255 kfree(state);
256 fe->tuner_priv = NULL;
257 return 0;
258 }
259
260 static struct dvb_tuner_ops zl10039_ops = {
261 .release = zl10039_release,
262 .init = zl10039_init,
263 .sleep = zl10039_sleep,
264 .set_params = zl10039_set_params,
265 };
266
zl10039_attach(struct dvb_frontend * fe,u8 i2c_addr,struct i2c_adapter * i2c)267 struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
268 u8 i2c_addr, struct i2c_adapter *i2c)
269 {
270 struct zl10039_state *state = NULL;
271
272 dprintk("%s\n", __func__);
273 state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
274 if (state == NULL)
275 goto error;
276
277 state->i2c = i2c;
278 state->i2c_addr = i2c_addr;
279
280 /* Open i2c gate */
281 if (fe->ops.i2c_gate_ctrl)
282 fe->ops.i2c_gate_ctrl(fe, 1);
283 /* check if this is a valid tuner */
284 if (zl10039_readreg(state, GENERAL, &state->id) < 0) {
285 /* Close i2c gate */
286 if (fe->ops.i2c_gate_ctrl)
287 fe->ops.i2c_gate_ctrl(fe, 0);
288 goto error;
289 }
290 /* Close i2c gate */
291 if (fe->ops.i2c_gate_ctrl)
292 fe->ops.i2c_gate_ctrl(fe, 0);
293
294 state->id = state->id & 0x0f;
295 switch (state->id) {
296 case ID_ZL10039:
297 strcpy(fe->ops.tuner_ops.info.name,
298 "Zarlink ZL10039 DVB-S tuner");
299 break;
300 default:
301 dprintk("Chip ID=%x does not match a known type\n", state->id);
302 goto error;
303 }
304
305 memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops));
306 fe->tuner_priv = state;
307 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr);
308 return fe;
309 error:
310 kfree(state);
311 return NULL;
312 }
313 EXPORT_SYMBOL(zl10039_attach);
314
315 module_param(debug, int, 0644);
316 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
317 MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
318 MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
319 MODULE_LICENSE("GPL");
320