blob: 333e4a1da13bd5f673305f082c58f22325817c23 [file] [log] [blame]
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -03001/*
2 * Driver for Zarlink ZL10039 DVB-S tuner
3 *
4 * Copyright 2007 Jan D. Louw <jd.louw@mweb.co.za>
5 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -030016 */
17
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>
23
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050024#include <media/dvb_frontend.h>
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -030025#include "zl10039.h"
26
27static int debug;
28
Mauro Carvalho Chehab83937962013-11-02 05:05:18 -030029/* Max transfer size done by I2C transfer functions */
30#define MAX_XFER_SIZE 64
31
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -030032#define dprintk(args...) \
33 do { \
34 if (debug) \
35 printk(KERN_DEBUG args); \
36 } while (0)
37
38enum zl10039_model_id {
39 ID_ZL10039 = 1
40};
41
42struct zl10039_state {
43 struct i2c_adapter *i2c;
44 u8 i2c_addr;
45 u8 id;
46};
47
48enum 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
65};
66
67static int zl10039_read(const struct zl10039_state *state,
68 const enum zl10039_reg_addr reg, u8 *buf,
69 const size_t count)
70{
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,
83 },
84 };
85
86 dprintk("%s\n", __func__);
87
88 if (i2c_transfer(state->i2c, msg, 2) != 2) {
89 dprintk("%s: i2c read error\n", __func__);
90 return -EREMOTEIO;
91 }
92
93 return 0; /* Success */
94}
95
96static int zl10039_write(struct zl10039_state *state,
97 const enum zl10039_reg_addr reg, const u8 *src,
98 const size_t count)
99{
Mauro Carvalho Chehab83937962013-11-02 05:05:18 -0300100 u8 buf[MAX_XFER_SIZE];
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300101 struct i2c_msg msg = {
102 .addr = state->i2c_addr,
103 .flags = 0,
104 .buf = buf,
105 .len = count + 1,
106 };
107
Mauro Carvalho Chehab83937962013-11-02 05:05:18 -0300108 if (1 + count > sizeof(buf)) {
109 printk(KERN_WARNING
Mauro Carvalho Chehab35f30f32014-09-24 20:35:12 -0300110 "%s: i2c wr reg=%04x: len=%zu is too big!\n",
Mauro Carvalho Chehab83937962013-11-02 05:05:18 -0300111 KBUILD_MODNAME, reg, count);
112 return -EINVAL;
113 }
114
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300115 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;
122 }
123
124 return 0; /* Success */
125}
126
127static inline int zl10039_readreg(struct zl10039_state *state,
128 const enum zl10039_reg_addr reg, u8 *val)
129{
130 return zl10039_read(state, reg, val, 1);
131}
132
133static inline int zl10039_writereg(struct zl10039_state *state,
134 const enum zl10039_reg_addr reg,
135 const u8 val)
136{
Arnd Bergmann3cd890d2017-11-30 11:55:46 -0500137 const u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
138
139 return zl10039_write(state, reg, &tmp, 1);
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300140}
141
142static int zl10039_init(struct dvb_frontend *fe)
143{
144 struct zl10039_state *state = fe->tuner_priv;
145 int ret;
146
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) {
Mauro Carvalho Chehab4bd69e72016-10-18 17:44:22 -0200153 dprintk("Note: i2c write error normal when resetting the tuner\n");
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300154 }
155 /* Wake up */
156 ret = zl10039_writereg(state, GENERAL, 0x01);
157 if (ret < 0) {
158 dprintk("Tuner power up failed\n");
159 return ret;
160 }
161 if (fe->ops.i2c_gate_ctrl)
162 fe->ops.i2c_gate_ctrl(fe, 0);
163
164 return 0;
165}
166
167static int zl10039_sleep(struct dvb_frontend *fe)
168{
169 struct zl10039_state *state = fe->tuner_priv;
170 int ret;
171
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;
179 }
180 if (fe->ops.i2c_gate_ctrl)
181 fe->ops.i2c_gate_ctrl(fe, 0);
182
183 return 0;
184}
185
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300186static int zl10039_set_params(struct dvb_frontend *fe)
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300187{
Mauro Carvalho Chehab85798812011-12-22 21:20:31 -0300188 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300189 struct zl10039_state *state = fe->tuner_priv;
190 u8 buf[6];
191 u8 bf;
192 u32 fbw;
193 u32 div;
194 int ret;
195
196 dprintk("%s\n", __func__);
197 dprintk("Set frequency = %d, symbol rate = %d\n",
Mauro Carvalho Chehab85798812011-12-22 21:20:31 -0300198 c->frequency, c->symbol_rate);
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300199
200 /* Assumed 10.111 MHz crystal oscillator */
201 /* Cancelled num/den 80 to prevent overflow */
Mauro Carvalho Chehab85798812011-12-22 21:20:31 -0300202 div = (c->frequency * 1000) / 126387;
203 fbw = (c->symbol_rate * 27) / 32000;
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300204 /* Cancelled num/den 10 to prevent overflow */
205 bf = ((fbw * 5088) / 1011100) - 1;
206
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;
219
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;
235
236 /* Close i2c gate */
237 if (fe->ops.i2c_gate_ctrl)
238 fe->ops.i2c_gate_ctrl(fe, 0);
239 return 0;
240error:
241 dprintk("Error setting tuner\n");
242 return ret;
243}
244
Max Kellermann194ced72016-08-09 18:32:31 -0300245static void zl10039_release(struct dvb_frontend *fe)
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300246{
247 struct zl10039_state *state = fe->tuner_priv;
248
249 dprintk("%s\n", __func__);
250 kfree(state);
251 fe->tuner_priv = NULL;
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300252}
253
Julia Lawall14c4bf32016-09-11 11:44:12 -0300254static const struct dvb_tuner_ops zl10039_ops = {
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300255 .release = zl10039_release,
256 .init = zl10039_init,
257 .sleep = zl10039_sleep,
258 .set_params = zl10039_set_params,
259};
260
261struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
262 u8 i2c_addr, struct i2c_adapter *i2c)
263{
264 struct zl10039_state *state = NULL;
265
266 dprintk("%s\n", __func__);
267 state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
268 if (state == NULL)
269 goto error;
270
271 state->i2c = i2c;
272 state->i2c_addr = i2c_addr;
273
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;
283 }
284 /* Close i2c gate */
285 if (fe->ops.i2c_gate_ctrl)
286 fe->ops.i2c_gate_ctrl(fe, 0);
287
288 state->id = state->id & 0x0f;
289 switch (state->id) {
290 case ID_ZL10039:
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400291 strscpy(fe->ops.tuner_ops.info.name,
292 "Zarlink ZL10039 DVB-S tuner",
293 sizeof(fe->ops.tuner_ops.info.name));
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300294 break;
295 default:
296 dprintk("Chip ID=%x does not match a known type\n", state->id);
Igor M. Liplianin2ccf5a92009-06-20 09:51:48 -0300297 goto error;
298 }
299
300 memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops));
301 fe->tuner_priv = state;
302 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr);
303 return fe;
304error:
305 kfree(state);
306 return NULL;
307}
308EXPORT_SYMBOL(zl10039_attach);
309
310module_param(debug, int, 0644);
311MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
312MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
313MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
314MODULE_LICENSE("GPL");