1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Zarlink ZL10039 DVB-S tuner
5 * Copyright 2007 Jan D. Louw <jd.louw@mweb.co.za>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/dvb/frontend.h>
14 #include <media/dvb_frontend.h>
19 /* Max transfer size done by I2C transfer functions */
20 #define MAX_XFER_SIZE 64
22 #define dprintk(args...) \
25 printk(KERN_DEBUG args); \
28 enum zl10039_model_id
{
32 struct zl10039_state
{
33 struct i2c_adapter
*i2c
;
38 enum zl10039_reg_addr
{
57 static int zl10039_read(const struct zl10039_state
*state
,
58 const enum zl10039_reg_addr reg
, u8
*buf
,
61 u8 regbuf
[] = { reg
};
62 struct i2c_msg msg
[] = {
63 {/* Write register address */
64 .addr
= state
->i2c_addr
,
68 }, {/* Read count bytes */
69 .addr
= state
->i2c_addr
,
76 dprintk("%s\n", __func__
);
78 if (i2c_transfer(state
->i2c
, msg
, 2) != 2) {
79 dprintk("%s: i2c read error\n", __func__
);
83 return 0; /* Success */
86 static int zl10039_write(struct zl10039_state
*state
,
87 const enum zl10039_reg_addr reg
, const u8
*src
,
90 u8 buf
[MAX_XFER_SIZE
];
91 struct i2c_msg msg
= {
92 .addr
= state
->i2c_addr
,
98 if (1 + count
> sizeof(buf
)) {
100 "%s: i2c wr reg=%04x: len=%zu is too big!\n",
101 KBUILD_MODNAME
, reg
, count
);
105 dprintk("%s\n", __func__
);
106 /* Write register address and data in one go */
108 memcpy(&buf
[1], src
, count
);
109 if (i2c_transfer(state
->i2c
, &msg
, 1) != 1) {
110 dprintk("%s: i2c write error\n", __func__
);
114 return 0; /* Success */
117 static inline int zl10039_readreg(struct zl10039_state
*state
,
118 const enum zl10039_reg_addr reg
, u8
*val
)
120 return zl10039_read(state
, reg
, val
, 1);
123 static inline int zl10039_writereg(struct zl10039_state
*state
,
124 const enum zl10039_reg_addr reg
,
127 const u8 tmp
= val
; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
129 return zl10039_write(state
, reg
, &tmp
, 1);
132 static int zl10039_init(struct dvb_frontend
*fe
)
134 struct zl10039_state
*state
= fe
->tuner_priv
;
137 dprintk("%s\n", __func__
);
138 if (fe
->ops
.i2c_gate_ctrl
)
139 fe
->ops
.i2c_gate_ctrl(fe
, 1);
141 ret
= zl10039_writereg(state
, GENERAL
, 0x40);
143 dprintk("Note: i2c write error normal when resetting the tuner\n");
146 ret
= zl10039_writereg(state
, GENERAL
, 0x01);
148 dprintk("Tuner power up failed\n");
151 if (fe
->ops
.i2c_gate_ctrl
)
152 fe
->ops
.i2c_gate_ctrl(fe
, 0);
157 static int zl10039_sleep(struct dvb_frontend
*fe
)
159 struct zl10039_state
*state
= fe
->tuner_priv
;
162 dprintk("%s\n", __func__
);
163 if (fe
->ops
.i2c_gate_ctrl
)
164 fe
->ops
.i2c_gate_ctrl(fe
, 1);
165 ret
= zl10039_writereg(state
, GENERAL
, 0x80);
167 dprintk("Tuner sleep failed\n");
170 if (fe
->ops
.i2c_gate_ctrl
)
171 fe
->ops
.i2c_gate_ctrl(fe
, 0);
176 static int zl10039_set_params(struct dvb_frontend
*fe
)
178 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
179 struct zl10039_state
*state
= fe
->tuner_priv
;
186 dprintk("%s\n", __func__
);
187 dprintk("Set frequency = %d, symbol rate = %d\n",
188 c
->frequency
, c
->symbol_rate
);
190 /* Assumed 10.111 MHz crystal oscillator */
191 /* Cancelled num/den 80 to prevent overflow */
192 div
= (c
->frequency
* 1000) / 126387;
193 fbw
= (c
->symbol_rate
* 27) / 32000;
194 /* Cancelled num/den 10 to prevent overflow */
195 bf
= ((fbw
* 5088) / 1011100) - 1;
198 buf
[0] = (div
>> 8) & 0x7f;
199 buf
[1] = (div
>> 0) & 0xff;
200 /*Reference divider*/
201 /* Select reference ratio of 80 */
205 /*RF Control register*/
206 buf
[4] = 0x6E; /* Bypass enable */
207 /*Baseband filter cutoff */
211 if (fe
->ops
.i2c_gate_ctrl
)
212 fe
->ops
.i2c_gate_ctrl(fe
, 1);
213 /* BR = 10, Enable filter adjustment */
214 ret
= zl10039_writereg(state
, BASE1
, 0x0A);
217 /* Write new config values */
218 ret
= zl10039_write(state
, PLL0
, buf
, sizeof(buf
));
221 /* BR = 10, Disable filter adjustment */
222 ret
= zl10039_writereg(state
, BASE1
, 0x6A);
227 if (fe
->ops
.i2c_gate_ctrl
)
228 fe
->ops
.i2c_gate_ctrl(fe
, 0);
231 dprintk("Error setting tuner\n");
235 static void zl10039_release(struct dvb_frontend
*fe
)
237 struct zl10039_state
*state
= fe
->tuner_priv
;
239 dprintk("%s\n", __func__
);
241 fe
->tuner_priv
= NULL
;
244 static const struct dvb_tuner_ops zl10039_ops
= {
245 .release
= zl10039_release
,
246 .init
= zl10039_init
,
247 .sleep
= zl10039_sleep
,
248 .set_params
= zl10039_set_params
,
251 struct dvb_frontend
*zl10039_attach(struct dvb_frontend
*fe
,
252 u8 i2c_addr
, struct i2c_adapter
*i2c
)
254 struct zl10039_state
*state
= NULL
;
256 dprintk("%s\n", __func__
);
257 state
= kmalloc(sizeof(struct zl10039_state
), GFP_KERNEL
);
262 state
->i2c_addr
= i2c_addr
;
265 if (fe
->ops
.i2c_gate_ctrl
)
266 fe
->ops
.i2c_gate_ctrl(fe
, 1);
267 /* check if this is a valid tuner */
268 if (zl10039_readreg(state
, GENERAL
, &state
->id
) < 0) {
270 if (fe
->ops
.i2c_gate_ctrl
)
271 fe
->ops
.i2c_gate_ctrl(fe
, 0);
275 if (fe
->ops
.i2c_gate_ctrl
)
276 fe
->ops
.i2c_gate_ctrl(fe
, 0);
278 state
->id
= state
->id
& 0x0f;
281 strscpy(fe
->ops
.tuner_ops
.info
.name
,
282 "Zarlink ZL10039 DVB-S tuner",
283 sizeof(fe
->ops
.tuner_ops
.info
.name
));
286 dprintk("Chip ID=%x does not match a known type\n", state
->id
);
290 memcpy(&fe
->ops
.tuner_ops
, &zl10039_ops
, sizeof(struct dvb_tuner_ops
));
291 fe
->tuner_priv
= state
;
292 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr
);
298 EXPORT_SYMBOL(zl10039_attach
);
300 module_param(debug
, int, 0644);
301 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
302 MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
303 MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
304 MODULE_LICENSE("GPL");