Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / dvb-frontends / zl10039.c
blob6293bd920fa613aae8f7f629688a71283ca299d2
1 /*
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>
25 #include "zl10039.h"
27 static int debug;
29 /* Max transfer size done by I2C transfer functions */
30 #define MAX_XFER_SIZE 64
32 #define dprintk(args...) \
33 do { \
34 if (debug) \
35 printk(KERN_DEBUG args); \
36 } while (0)
38 enum zl10039_model_id {
39 ID_ZL10039 = 1
42 struct zl10039_state {
43 struct i2c_adapter *i2c;
44 u8 i2c_addr;
45 u8 id;
48 enum zl10039_reg_addr {
49 PLL0 = 0,
50 PLL1,
51 PLL2,
52 PLL3,
53 RFFE,
54 BASE0,
55 BASE1,
56 BASE2,
57 LO0,
58 LO1,
59 LO2,
60 LO3,
61 LO4,
62 LO5,
63 LO6,
64 GENERAL
67 static int zl10039_read(const struct zl10039_state *state,
68 const enum zl10039_reg_addr reg, u8 *buf,
69 const size_t count)
71 u8 regbuf[] = { reg };
72 struct i2c_msg msg[] = {
73 {/* Write register address */
74 .addr = state->i2c_addr,
75 .flags = 0,
76 .buf = regbuf,
77 .len = 1,
78 }, {/* Read count bytes */
79 .addr = state->i2c_addr,
80 .flags = I2C_M_RD,
81 .buf = buf,
82 .len = count,
86 dprintk("%s\n", __func__);
88 if (i2c_transfer(state->i2c, msg, 2) != 2) {
89 dprintk("%s: i2c read error\n", __func__);
90 return -EREMOTEIO;
93 return 0; /* Success */
96 static int zl10039_write(struct zl10039_state *state,
97 const enum zl10039_reg_addr reg, const u8 *src,
98 const size_t count)
100 u8 buf[MAX_XFER_SIZE];
101 struct i2c_msg msg = {
102 .addr = state->i2c_addr,
103 .flags = 0,
104 .buf = buf,
105 .len = count + 1,
108 if (1 + count > sizeof(buf)) {
109 printk(KERN_WARNING
110 "%s: i2c wr reg=%04x: len=%zu is too big!\n",
111 KBUILD_MODNAME, reg, count);
112 return -EINVAL;
115 dprintk("%s\n", __func__);
116 /* Write register address and data in one go */
117 buf[0] = reg;
118 memcpy(&buf[1], src, count);
119 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
120 dprintk("%s: i2c write error\n", __func__);
121 return -EREMOTEIO;
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,
135 const u8 val)
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;
145 int ret;
147 dprintk("%s\n", __func__);
148 if (fe->ops.i2c_gate_ctrl)
149 fe->ops.i2c_gate_ctrl(fe, 1);
150 /* Reset logic */
151 ret = zl10039_writereg(state, GENERAL, 0x40);
152 if (ret < 0) {
153 dprintk("Note: i2c write error normal when resetting the tuner\n");
155 /* Wake up */
156 ret = zl10039_writereg(state, GENERAL, 0x01);
157 if (ret < 0) {
158 dprintk("Tuner power up failed\n");
159 return ret;
161 if (fe->ops.i2c_gate_ctrl)
162 fe->ops.i2c_gate_ctrl(fe, 0);
164 return 0;
167 static int zl10039_sleep(struct dvb_frontend *fe)
169 struct zl10039_state *state = fe->tuner_priv;
170 int ret;
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);
176 if (ret < 0) {
177 dprintk("Tuner sleep failed\n");
178 return ret;
180 if (fe->ops.i2c_gate_ctrl)
181 fe->ops.i2c_gate_ctrl(fe, 0);
183 return 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;
190 u8 buf[6];
191 u8 bf;
192 u32 fbw;
193 u32 div;
194 int ret;
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;
207 /*PLL divider*/
208 buf[0] = (div >> 8) & 0x7f;
209 buf[1] = (div >> 0) & 0xff;
210 /*Reference divider*/
211 /* Select reference ratio of 80 */
212 buf[2] = 0x1D;
213 /*PLL test modes*/
214 buf[3] = 0x40;
215 /*RF Control register*/
216 buf[4] = 0x6E; /* Bypass enable */
217 /*Baseband filter cutoff */
218 buf[5] = bf;
220 /* Open i2c gate */
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);
225 if (ret < 0)
226 goto error;
227 /* Write new config values */
228 ret = zl10039_write(state, PLL0, buf, sizeof(buf));
229 if (ret < 0)
230 goto error;
231 /* BR = 10, Disable filter adjustment */
232 ret = zl10039_writereg(state, BASE1, 0x6A);
233 if (ret < 0)
234 goto error;
236 /* Close i2c gate */
237 if (fe->ops.i2c_gate_ctrl)
238 fe->ops.i2c_gate_ctrl(fe, 0);
239 return 0;
240 error:
241 dprintk("Error setting tuner\n");
242 return ret;
245 static void zl10039_release(struct dvb_frontend *fe)
247 struct zl10039_state *state = fe->tuner_priv;
249 dprintk("%s\n", __func__);
250 kfree(state);
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);
268 if (state == NULL)
269 goto error;
271 state->i2c = i2c;
272 state->i2c_addr = i2c_addr;
274 /* Open i2c gate */
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) {
279 /* Close i2c gate */
280 if (fe->ops.i2c_gate_ctrl)
281 fe->ops.i2c_gate_ctrl(fe, 0);
282 goto error;
284 /* Close i2c gate */
285 if (fe->ops.i2c_gate_ctrl)
286 fe->ops.i2c_gate_ctrl(fe, 0);
288 state->id = state->id & 0x0f;
289 switch (state->id) {
290 case ID_ZL10039:
291 strcpy(fe->ops.tuner_ops.info.name,
292 "Zarlink ZL10039 DVB-S tuner");
293 break;
294 default:
295 dprintk("Chip ID=%x does not match a known type\n", state->id);
296 goto error;
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);
302 return fe;
303 error:
304 kfree(state);
305 return NULL;
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");