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 <media/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 const u8 tmp
= val
; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
139 return zl10039_write(state
, reg
, &tmp
, 1);
142 static int zl10039_init(struct dvb_frontend
*fe
)
144 struct zl10039_state
*state
= fe
->tuner_priv
;
147 dprintk("%s\n", __func__
);
148 if (fe
->ops
.i2c_gate_ctrl
)
149 fe
->ops
.i2c_gate_ctrl(fe
, 1);
151 ret
= zl10039_writereg(state
, GENERAL
, 0x40);
153 dprintk("Note: i2c write error normal when resetting the tuner\n");
156 ret
= zl10039_writereg(state
, GENERAL
, 0x01);
158 dprintk("Tuner power up failed\n");
161 if (fe
->ops
.i2c_gate_ctrl
)
162 fe
->ops
.i2c_gate_ctrl(fe
, 0);
167 static int zl10039_sleep(struct dvb_frontend
*fe
)
169 struct zl10039_state
*state
= fe
->tuner_priv
;
172 dprintk("%s\n", __func__
);
173 if (fe
->ops
.i2c_gate_ctrl
)
174 fe
->ops
.i2c_gate_ctrl(fe
, 1);
175 ret
= zl10039_writereg(state
, GENERAL
, 0x80);
177 dprintk("Tuner sleep failed\n");
180 if (fe
->ops
.i2c_gate_ctrl
)
181 fe
->ops
.i2c_gate_ctrl(fe
, 0);
186 static int zl10039_set_params(struct dvb_frontend
*fe
)
188 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
189 struct zl10039_state
*state
= fe
->tuner_priv
;
196 dprintk("%s\n", __func__
);
197 dprintk("Set frequency = %d, symbol rate = %d\n",
198 c
->frequency
, c
->symbol_rate
);
200 /* Assumed 10.111 MHz crystal oscillator */
201 /* Cancelled num/den 80 to prevent overflow */
202 div
= (c
->frequency
* 1000) / 126387;
203 fbw
= (c
->symbol_rate
* 27) / 32000;
204 /* Cancelled num/den 10 to prevent overflow */
205 bf
= ((fbw
* 5088) / 1011100) - 1;
208 buf
[0] = (div
>> 8) & 0x7f;
209 buf
[1] = (div
>> 0) & 0xff;
210 /*Reference divider*/
211 /* Select reference ratio of 80 */
215 /*RF Control register*/
216 buf
[4] = 0x6E; /* Bypass enable */
217 /*Baseband filter cutoff */
221 if (fe
->ops
.i2c_gate_ctrl
)
222 fe
->ops
.i2c_gate_ctrl(fe
, 1);
223 /* BR = 10, Enable filter adjustment */
224 ret
= zl10039_writereg(state
, BASE1
, 0x0A);
227 /* Write new config values */
228 ret
= zl10039_write(state
, PLL0
, buf
, sizeof(buf
));
231 /* BR = 10, Disable filter adjustment */
232 ret
= zl10039_writereg(state
, BASE1
, 0x6A);
237 if (fe
->ops
.i2c_gate_ctrl
)
238 fe
->ops
.i2c_gate_ctrl(fe
, 0);
241 dprintk("Error setting tuner\n");
245 static void zl10039_release(struct dvb_frontend
*fe
)
247 struct zl10039_state
*state
= fe
->tuner_priv
;
249 dprintk("%s\n", __func__
);
251 fe
->tuner_priv
= NULL
;
254 static const struct dvb_tuner_ops zl10039_ops
= {
255 .release
= zl10039_release
,
256 .init
= zl10039_init
,
257 .sleep
= zl10039_sleep
,
258 .set_params
= zl10039_set_params
,
261 struct dvb_frontend
*zl10039_attach(struct dvb_frontend
*fe
,
262 u8 i2c_addr
, struct i2c_adapter
*i2c
)
264 struct zl10039_state
*state
= NULL
;
266 dprintk("%s\n", __func__
);
267 state
= kmalloc(sizeof(struct zl10039_state
), GFP_KERNEL
);
272 state
->i2c_addr
= i2c_addr
;
275 if (fe
->ops
.i2c_gate_ctrl
)
276 fe
->ops
.i2c_gate_ctrl(fe
, 1);
277 /* check if this is a valid tuner */
278 if (zl10039_readreg(state
, GENERAL
, &state
->id
) < 0) {
280 if (fe
->ops
.i2c_gate_ctrl
)
281 fe
->ops
.i2c_gate_ctrl(fe
, 0);
285 if (fe
->ops
.i2c_gate_ctrl
)
286 fe
->ops
.i2c_gate_ctrl(fe
, 0);
288 state
->id
= state
->id
& 0x0f;
291 strcpy(fe
->ops
.tuner_ops
.info
.name
,
292 "Zarlink ZL10039 DVB-S tuner");
295 dprintk("Chip ID=%x does not match a known type\n", state
->id
);
299 memcpy(&fe
->ops
.tuner_ops
, &zl10039_ops
, sizeof(struct dvb_tuner_ops
));
300 fe
->tuner_priv
= state
;
301 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr
);
307 EXPORT_SYMBOL(zl10039_attach
);
309 module_param(debug
, int, 0644);
310 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
311 MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
312 MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
313 MODULE_LICENSE("GPL");