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.
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>
24 #include "dvb_frontend.h"
29 /* Max transfer size done by I2C transfer functions */
30 #define MAX_XFER_SIZE 64
32 #define dprintk(args...) \
35 printk(KERN_DEBUG args); \
38 enum zl10039_model_id {
42 struct zl10039_state {
43 struct i2c_adapter *i2c;
48 enum zl10039_reg_addr {
67 static int zl10039_read(const struct zl10039_state *state,
68 const enum zl10039_reg_addr reg, u8 *buf,
71 u8 regbuf[] = { reg };
72 struct i2c_msg msg[] = {
73 {/* Write register address */
74 .addr = state->i2c_addr,
78 }, {/* Read count bytes */
79 .addr = state->i2c_addr,
86 dprintk("%s\n", __func__);
88 if (i2c_transfer(state->i2c, msg, 2) != 2) {
89 dprintk("%s: i2c read error\n", __func__);
93 return 0; /* Success */
96 static int zl10039_write(struct zl10039_state *state,
97 const enum zl10039_reg_addr reg, const u8 *src,
100 u8 buf[MAX_XFER_SIZE];
101 struct i2c_msg msg = {
102 .addr = state->i2c_addr,
108 if (1 + count > sizeof(buf)) {
110 "%s: i2c wr reg=%04x: len=%zu is too big!\n",
111 KBUILD_MODNAME, reg, count);
115 dprintk("%s\n", __func__);
116 /* Write register address and data in one go */
118 memcpy(&buf[1], src, count);
119 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
120 dprintk("%s: i2c write error\n", __func__);
124 return 0; /* Success */
127 static inline int zl10039_readreg(struct zl10039_state *state,
128 const enum zl10039_reg_addr reg, u8 *val)
130 return zl10039_read(state, reg, val, 1);
133 static inline int zl10039_writereg(struct zl10039_state *state,
134 const enum zl10039_reg_addr reg,
137 return zl10039_write(state, reg, &val, 1);
140 static int zl10039_init(struct dvb_frontend *fe)
142 struct zl10039_state *state = fe->tuner_priv;
145 dprintk("%s\n", __func__);
146 if (fe->ops.i2c_gate_ctrl)
147 fe->ops.i2c_gate_ctrl(fe, 1);
149 ret = zl10039_writereg(state, GENERAL, 0x40);
151 dprintk("Note: i2c write error normal when resetting the tuner\n");
154 ret = zl10039_writereg(state, GENERAL, 0x01);
156 dprintk("Tuner power up failed\n");
159 if (fe->ops.i2c_gate_ctrl)
160 fe->ops.i2c_gate_ctrl(fe, 0);
165 static int zl10039_sleep(struct dvb_frontend *fe)
167 struct zl10039_state *state = fe->tuner_priv;
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);
175 dprintk("Tuner sleep failed\n");
178 if (fe->ops.i2c_gate_ctrl)
179 fe->ops.i2c_gate_ctrl(fe, 0);
184 static int zl10039_set_params(struct dvb_frontend *fe)
186 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
187 struct zl10039_state *state = fe->tuner_priv;
194 dprintk("%s\n", __func__);
195 dprintk("Set frequency = %d, symbol rate = %d\n",
196 c->frequency, c->symbol_rate);
198 /* Assumed 10.111 MHz crystal oscillator */
199 /* Cancelled num/den 80 to prevent overflow */
200 div = (c->frequency * 1000) / 126387;
201 fbw = (c->symbol_rate * 27) / 32000;
202 /* Cancelled num/den 10 to prevent overflow */
203 bf = ((fbw * 5088) / 1011100) - 1;
206 buf[0] = (div >> 8) & 0x7f;
207 buf[1] = (div >> 0) & 0xff;
208 /*Reference divider*/
209 /* Select reference ratio of 80 */
213 /*RF Control register*/
214 buf[4] = 0x6E; /* Bypass enable */
215 /*Baseband filter cutoff */
219 if (fe->ops.i2c_gate_ctrl)
220 fe->ops.i2c_gate_ctrl(fe, 1);
221 /* BR = 10, Enable filter adjustment */
222 ret = zl10039_writereg(state, BASE1, 0x0A);
225 /* Write new config values */
226 ret = zl10039_write(state, PLL0, buf, sizeof(buf));
229 /* BR = 10, Disable filter adjustment */
230 ret = zl10039_writereg(state, BASE1, 0x6A);
235 if (fe->ops.i2c_gate_ctrl)
236 fe->ops.i2c_gate_ctrl(fe, 0);
239 dprintk("Error setting tuner\n");
243 static void zl10039_release(struct dvb_frontend *fe)
245 struct zl10039_state *state = fe->tuner_priv;
247 dprintk("%s\n", __func__);
249 fe->tuner_priv = NULL;
252 static 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,
259 struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
260 u8 i2c_addr, struct i2c_adapter *i2c)
262 struct zl10039_state *state = NULL;
264 dprintk("%s\n", __func__);
265 state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
270 state->i2c_addr = i2c_addr;
273 if (fe->ops.i2c_gate_ctrl)
274 fe->ops.i2c_gate_ctrl(fe, 1);
275 /* check if this is a valid tuner */
276 if (zl10039_readreg(state, GENERAL, &state->id) < 0) {
278 if (fe->ops.i2c_gate_ctrl)
279 fe->ops.i2c_gate_ctrl(fe, 0);
283 if (fe->ops.i2c_gate_ctrl)
284 fe->ops.i2c_gate_ctrl(fe, 0);
286 state->id = state->id & 0x0f;
289 strcpy(fe->ops.tuner_ops.info.name,
290 "Zarlink ZL10039 DVB-S tuner");
293 dprintk("Chip ID=%x does not match a known type\n", state->id);
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);
305 EXPORT_SYMBOL(zl10039_attach);
307 module_param(debug, int, 0644);
308 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
309 MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
310 MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
311 MODULE_LICENSE("GPL");