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 /* Max transfer size done by I2C transfer functions */
34 #define MAX_XFER_SIZE 64
36 #define dprintk(args...) \
39 printk(KERN_DEBUG args); \
42 enum zl10039_model_id
{
46 struct zl10039_state
{
47 struct i2c_adapter
*i2c
;
52 enum zl10039_reg_addr
{
71 static int zl10039_read(const struct zl10039_state
*state
,
72 const enum zl10039_reg_addr reg
, u8
*buf
,
75 u8 regbuf
[] = { reg
};
76 struct i2c_msg msg
[] = {
77 {/* Write register address */
78 .addr
= state
->i2c_addr
,
82 }, {/* Read count bytes */
83 .addr
= state
->i2c_addr
,
90 dprintk("%s\n", __func__
);
92 if (i2c_transfer(state
->i2c
, msg
, 2) != 2) {
93 dprintk("%s: i2c read error\n", __func__
);
97 return 0; /* Success */
100 static int zl10039_write(struct zl10039_state
*state
,
101 const enum zl10039_reg_addr reg
, const u8
*src
,
104 u8 buf
[MAX_XFER_SIZE
];
105 struct i2c_msg msg
= {
106 .addr
= state
->i2c_addr
,
112 if (1 + count
> sizeof(buf
)) {
114 "%s: i2c wr reg=%04x: len=%zu is too big!\n",
115 KBUILD_MODNAME
, reg
, count
);
119 dprintk("%s\n", __func__
);
120 /* Write register address and data in one go */
122 memcpy(&buf
[1], src
, count
);
123 if (i2c_transfer(state
->i2c
, &msg
, 1) != 1) {
124 dprintk("%s: i2c write error\n", __func__
);
128 return 0; /* Success */
131 static inline int zl10039_readreg(struct zl10039_state
*state
,
132 const enum zl10039_reg_addr reg
, u8
*val
)
134 return zl10039_read(state
, reg
, val
, 1);
137 static inline int zl10039_writereg(struct zl10039_state
*state
,
138 const enum zl10039_reg_addr reg
,
141 return zl10039_write(state
, reg
, &val
, 1);
144 static int zl10039_init(struct dvb_frontend
*fe
)
146 struct zl10039_state
*state
= fe
->tuner_priv
;
149 dprintk("%s\n", __func__
);
150 if (fe
->ops
.i2c_gate_ctrl
)
151 fe
->ops
.i2c_gate_ctrl(fe
, 1);
153 ret
= zl10039_writereg(state
, GENERAL
, 0x40);
155 dprintk("Note: i2c write error normal when resetting the tuner\n");
158 ret
= zl10039_writereg(state
, GENERAL
, 0x01);
160 dprintk("Tuner power up failed\n");
163 if (fe
->ops
.i2c_gate_ctrl
)
164 fe
->ops
.i2c_gate_ctrl(fe
, 0);
169 static int zl10039_sleep(struct dvb_frontend
*fe
)
171 struct zl10039_state
*state
= fe
->tuner_priv
;
174 dprintk("%s\n", __func__
);
175 if (fe
->ops
.i2c_gate_ctrl
)
176 fe
->ops
.i2c_gate_ctrl(fe
, 1);
177 ret
= zl10039_writereg(state
, GENERAL
, 0x80);
179 dprintk("Tuner sleep failed\n");
182 if (fe
->ops
.i2c_gate_ctrl
)
183 fe
->ops
.i2c_gate_ctrl(fe
, 0);
188 static int zl10039_set_params(struct dvb_frontend
*fe
)
190 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
191 struct zl10039_state
*state
= fe
->tuner_priv
;
198 dprintk("%s\n", __func__
);
199 dprintk("Set frequency = %d, symbol rate = %d\n",
200 c
->frequency
, c
->symbol_rate
);
202 /* Assumed 10.111 MHz crystal oscillator */
203 /* Cancelled num/den 80 to prevent overflow */
204 div
= (c
->frequency
* 1000) / 126387;
205 fbw
= (c
->symbol_rate
* 27) / 32000;
206 /* Cancelled num/den 10 to prevent overflow */
207 bf
= ((fbw
* 5088) / 1011100) - 1;
210 buf
[0] = (div
>> 8) & 0x7f;
211 buf
[1] = (div
>> 0) & 0xff;
212 /*Reference divider*/
213 /* Select reference ratio of 80 */
217 /*RF Control register*/
218 buf
[4] = 0x6E; /* Bypass enable */
219 /*Baseband filter cutoff */
223 if (fe
->ops
.i2c_gate_ctrl
)
224 fe
->ops
.i2c_gate_ctrl(fe
, 1);
225 /* BR = 10, Enable filter adjustment */
226 ret
= zl10039_writereg(state
, BASE1
, 0x0A);
229 /* Write new config values */
230 ret
= zl10039_write(state
, PLL0
, buf
, sizeof(buf
));
233 /* BR = 10, Disable filter adjustment */
234 ret
= zl10039_writereg(state
, BASE1
, 0x6A);
239 if (fe
->ops
.i2c_gate_ctrl
)
240 fe
->ops
.i2c_gate_ctrl(fe
, 0);
243 dprintk("Error setting tuner\n");
247 static void zl10039_release(struct dvb_frontend
*fe
)
249 struct zl10039_state
*state
= fe
->tuner_priv
;
251 dprintk("%s\n", __func__
);
253 fe
->tuner_priv
= NULL
;
256 static const struct dvb_tuner_ops zl10039_ops
= {
257 .release
= zl10039_release
,
258 .init
= zl10039_init
,
259 .sleep
= zl10039_sleep
,
260 .set_params
= zl10039_set_params
,
263 struct dvb_frontend
*zl10039_attach(struct dvb_frontend
*fe
,
264 u8 i2c_addr
, struct i2c_adapter
*i2c
)
266 struct zl10039_state
*state
= NULL
;
268 dprintk("%s\n", __func__
);
269 state
= kmalloc(sizeof(struct zl10039_state
), GFP_KERNEL
);
274 state
->i2c_addr
= i2c_addr
;
277 if (fe
->ops
.i2c_gate_ctrl
)
278 fe
->ops
.i2c_gate_ctrl(fe
, 1);
279 /* check if this is a valid tuner */
280 if (zl10039_readreg(state
, GENERAL
, &state
->id
) < 0) {
282 if (fe
->ops
.i2c_gate_ctrl
)
283 fe
->ops
.i2c_gate_ctrl(fe
, 0);
287 if (fe
->ops
.i2c_gate_ctrl
)
288 fe
->ops
.i2c_gate_ctrl(fe
, 0);
290 state
->id
= state
->id
& 0x0f;
293 strcpy(fe
->ops
.tuner_ops
.info
.name
,
294 "Zarlink ZL10039 DVB-S tuner");
297 dprintk("Chip ID=%x does not match a known type\n", state
->id
);
301 memcpy(&fe
->ops
.tuner_ops
, &zl10039_ops
, sizeof(struct dvb_tuner_ops
));
302 fe
->tuner_priv
= state
;
303 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr
);
309 EXPORT_SYMBOL(zl10039_attach
);
311 module_param(debug
, int, 0644);
312 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
313 MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
314 MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
315 MODULE_LICENSE("GPL");