2 * Driver for Zarlink ZL10039 DVB-S tuner
4 * Copyright 2007 Jan D. Louw <jd.louw@mweb.co.za>
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.
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
15 * GNU General Public License for more details.
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.
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>
28 #include "dvb_frontend.h"
33 #define dprintk(args...) \
36 printk(KERN_DEBUG args); \
39 enum zl10039_model_id {
43 struct zl10039_state {
44 struct i2c_adapter *i2c;
49 enum zl10039_reg_addr {
68 static int zl10039_read(const struct zl10039_state *state,
69 const enum zl10039_reg_addr reg, u8 *buf,
72 u8 regbuf[] = { reg };
73 struct i2c_msg msg[] = {
74 {/* Write register address */
75 .addr = state->i2c_addr,
79 }, {/* Read count bytes */
80 .addr = state->i2c_addr,
87 dprintk("%s\n", __func__);
89 if (i2c_transfer(state->i2c, msg, 2) != 2) {
90 dprintk("%s: i2c read error\n", __func__);
94 return 0; /* Success */
97 static int zl10039_write(struct zl10039_state *state,
98 const enum zl10039_reg_addr reg, const u8 *src,
102 struct i2c_msg msg = {
103 .addr = state->i2c_addr,
109 dprintk("%s\n", __func__);
110 /* Write register address and data in one go */
112 memcpy(&buf[1], src, count);
113 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
114 dprintk("%s: i2c write error\n", __func__);
118 return 0; /* Success */
121 static inline int zl10039_readreg(struct zl10039_state *state,
122 const enum zl10039_reg_addr reg, u8 *val)
124 return zl10039_read(state, reg, val, 1);
127 static inline int zl10039_writereg(struct zl10039_state *state,
128 const enum zl10039_reg_addr reg,
131 return zl10039_write(state, reg, &val, 1);
134 static int zl10039_init(struct dvb_frontend *fe)
136 struct zl10039_state *state = fe->tuner_priv;
139 dprintk("%s\n", __func__);
140 if (fe->ops.i2c_gate_ctrl)
141 fe->ops.i2c_gate_ctrl(fe, 1);
143 ret = zl10039_writereg(state, GENERAL, 0x40);
145 dprintk("Note: i2c write error normal when resetting the "
149 ret = zl10039_writereg(state, GENERAL, 0x01);
151 dprintk("Tuner power up failed\n");
154 if (fe->ops.i2c_gate_ctrl)
155 fe->ops.i2c_gate_ctrl(fe, 0);
160 static int zl10039_sleep(struct dvb_frontend *fe)
162 struct zl10039_state *state = fe->tuner_priv;
165 dprintk("%s\n", __func__);
166 if (fe->ops.i2c_gate_ctrl)
167 fe->ops.i2c_gate_ctrl(fe, 1);
168 ret = zl10039_writereg(state, GENERAL, 0x80);
170 dprintk("Tuner sleep failed\n");
173 if (fe->ops.i2c_gate_ctrl)
174 fe->ops.i2c_gate_ctrl(fe, 0);
179 static int zl10039_set_params(struct dvb_frontend *fe)
181 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
182 struct zl10039_state *state = fe->tuner_priv;
189 dprintk("%s\n", __func__);
190 dprintk("Set frequency = %d, symbol rate = %d\n",
191 c->frequency, c->symbol_rate);
193 /* Assumed 10.111 MHz crystal oscillator */
194 /* Cancelled num/den 80 to prevent overflow */
195 div = (c->frequency * 1000) / 126387;
196 fbw = (c->symbol_rate * 27) / 32000;
197 /* Cancelled num/den 10 to prevent overflow */
198 bf = ((fbw * 5088) / 1011100) - 1;
201 buf[0] = (div >> 8) & 0x7f;
202 buf[1] = (div >> 0) & 0xff;
203 /*Reference divider*/
204 /* Select reference ratio of 80 */
208 /*RF Control register*/
209 buf[4] = 0x6E; /* Bypass enable */
210 /*Baseband filter cutoff */
214 if (fe->ops.i2c_gate_ctrl)
215 fe->ops.i2c_gate_ctrl(fe, 1);
216 /* BR = 10, Enable filter adjustment */
217 ret = zl10039_writereg(state, BASE1, 0x0A);
220 /* Write new config values */
221 ret = zl10039_write(state, PLL0, buf, sizeof(buf));
224 /* BR = 10, Disable filter adjustment */
225 ret = zl10039_writereg(state, BASE1, 0x6A);
230 if (fe->ops.i2c_gate_ctrl)
231 fe->ops.i2c_gate_ctrl(fe, 0);
234 dprintk("Error setting tuner\n");
238 static int zl10039_release(struct dvb_frontend *fe)
240 struct zl10039_state *state = fe->tuner_priv;
242 dprintk("%s\n", __func__);
244 fe->tuner_priv = NULL;
248 static struct dvb_tuner_ops zl10039_ops = {
249 .release = zl10039_release,
250 .init = zl10039_init,
251 .sleep = zl10039_sleep,
252 .set_params = zl10039_set_params,
255 struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
256 u8 i2c_addr, struct i2c_adapter *i2c)
258 struct zl10039_state *state = NULL;
260 dprintk("%s\n", __func__);
261 state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
266 state->i2c_addr = i2c_addr;
269 if (fe->ops.i2c_gate_ctrl)
270 fe->ops.i2c_gate_ctrl(fe, 1);
271 /* check if this is a valid tuner */
272 if (zl10039_readreg(state, GENERAL, &state->id) < 0) {
274 if (fe->ops.i2c_gate_ctrl)
275 fe->ops.i2c_gate_ctrl(fe, 0);
279 if (fe->ops.i2c_gate_ctrl)
280 fe->ops.i2c_gate_ctrl(fe, 0);
282 state->id = state->id & 0x0f;
285 strcpy(fe->ops.tuner_ops.info.name,
286 "Zarlink ZL10039 DVB-S tuner");
289 dprintk("Chip ID=%x does not match a known type\n", state->id);
293 memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops));
294 fe->tuner_priv = state;
295 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr);
301 EXPORT_SYMBOL(zl10039_attach);
303 module_param(debug, int, 0644);
304 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
305 MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
306 MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
307 MODULE_LICENSE("GPL");