blob: f3b872a6b38118396bab034a3b553fdc42b6fc28 [file] [log] [blame]
Antti Palosaaried85ada2012-09-01 21:09:21 -03001/*
2 * Elonics E4000 silicon tuner driver
3 *
4 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
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 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "e4000_priv.h"
22
23/* write multiple registers */
24static int e4000_wr_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
25{
26 int ret;
27 u8 buf[1 + len];
28 struct i2c_msg msg[1] = {
29 {
30 .addr = priv->cfg->i2c_addr,
31 .flags = 0,
32 .len = sizeof(buf),
33 .buf = buf,
34 }
35 };
36
37 buf[0] = reg;
38 memcpy(&buf[1], val, len);
39
40 ret = i2c_transfer(priv->i2c, msg, 1);
41 if (ret == 1) {
42 ret = 0;
43 } else {
44 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \
45 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
46 ret = -EREMOTEIO;
47 }
48 return ret;
49}
50
51/* read multiple registers */
52static int e4000_rd_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
53{
54 int ret;
55 u8 buf[len];
56 struct i2c_msg msg[2] = {
57 {
58 .addr = priv->cfg->i2c_addr,
59 .flags = 0,
60 .len = 1,
61 .buf = &reg,
62 }, {
63 .addr = priv->cfg->i2c_addr,
64 .flags = I2C_M_RD,
65 .len = sizeof(buf),
66 .buf = buf,
67 }
68 };
69
70 ret = i2c_transfer(priv->i2c, msg, 2);
71 if (ret == 2) {
72 memcpy(val, buf, len);
73 ret = 0;
74 } else {
75 dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \
76 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
77 ret = -EREMOTEIO;
78 }
79
80 return ret;
81}
82
83/* write single register */
84static int e4000_wr_reg(struct e4000_priv *priv, u8 reg, u8 val)
85{
86 return e4000_wr_regs(priv, reg, &val, 1);
87}
88
89/* read single register */
90static int e4000_rd_reg(struct e4000_priv *priv, u8 reg, u8 *val)
91{
92 return e4000_rd_regs(priv, reg, val, 1);
93}
94
95static int e4000_init(struct dvb_frontend *fe)
96{
97 struct e4000_priv *priv = fe->tuner_priv;
98 int ret;
99
100 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
101
102 if (fe->ops.i2c_gate_ctrl)
103 fe->ops.i2c_gate_ctrl(fe, 1);
104
105 /* dummy I2C to ensure I2C wakes up */
106 ret = e4000_wr_reg(priv, 0x02, 0x40);
107
108 /* reset */
109 ret = e4000_wr_reg(priv, 0x00, 0x01);
110 if (ret < 0)
111 goto err;
112
113 /* disable output clock */
114 ret = e4000_wr_reg(priv, 0x06, 0x00);
115 if (ret < 0)
116 goto err;
117
118 ret = e4000_wr_reg(priv, 0x7a, 0x96);
119 if (ret < 0)
120 goto err;
121
122 /* configure gains */
123 ret = e4000_wr_regs(priv, 0x7e, "\x01\xfe", 2);
124 if (ret < 0)
125 goto err;
126
127 ret = e4000_wr_reg(priv, 0x82, 0x00);
128 if (ret < 0)
129 goto err;
130
131 ret = e4000_wr_reg(priv, 0x24, 0x05);
132 if (ret < 0)
133 goto err;
134
135 ret = e4000_wr_regs(priv, 0x87, "\x20\x01", 2);
136 if (ret < 0)
137 goto err;
138
139 ret = e4000_wr_regs(priv, 0x9f, "\x7f\x07", 2);
140 if (ret < 0)
141 goto err;
142
Antti Palosaaried85ada2012-09-01 21:09:21 -0300143 /* DC offset control */
Antti Palosaari85146112013-07-24 02:04:12 -0300144 ret = e4000_wr_reg(priv, 0x2d, 0x1f);
145 if (ret < 0)
146 goto err;
147
148 ret = e4000_wr_regs(priv, 0x70, "\x01\x01", 2);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300149 if (ret < 0)
150 goto err;
151
152 /* gain control */
153 ret = e4000_wr_reg(priv, 0x1a, 0x17);
154 if (ret < 0)
155 goto err;
156
157 ret = e4000_wr_reg(priv, 0x1f, 0x1a);
158 if (ret < 0)
159 goto err;
160
161 if (fe->ops.i2c_gate_ctrl)
162 fe->ops.i2c_gate_ctrl(fe, 0);
163
164 return 0;
165err:
166 if (fe->ops.i2c_gate_ctrl)
167 fe->ops.i2c_gate_ctrl(fe, 0);
168
169 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
170 return ret;
171}
172
173static int e4000_sleep(struct dvb_frontend *fe)
174{
175 struct e4000_priv *priv = fe->tuner_priv;
176 int ret;
177
178 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
179
180 if (fe->ops.i2c_gate_ctrl)
181 fe->ops.i2c_gate_ctrl(fe, 1);
182
183 ret = e4000_wr_reg(priv, 0x00, 0x00);
184 if (ret < 0)
185 goto err;
186
187 if (fe->ops.i2c_gate_ctrl)
188 fe->ops.i2c_gate_ctrl(fe, 0);
189
190 return 0;
191err:
192 if (fe->ops.i2c_gate_ctrl)
193 fe->ops.i2c_gate_ctrl(fe, 0);
194
195 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
196 return ret;
197}
198
199static int e4000_set_params(struct dvb_frontend *fe)
200{
201 struct e4000_priv *priv = fe->tuner_priv;
202 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
203 int ret, i, sigma_delta;
204 unsigned int f_VCO;
Antti Palosaari85146112013-07-24 02:04:12 -0300205 u8 buf[5], i_data[4], q_data[4];
Antti Palosaaried85ada2012-09-01 21:09:21 -0300206
207 dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d frequency=%d " \
208 "bandwidth_hz=%d\n", __func__,
209 c->delivery_system, c->frequency, c->bandwidth_hz);
210
211 if (fe->ops.i2c_gate_ctrl)
212 fe->ops.i2c_gate_ctrl(fe, 1);
213
214 /* gain control manual */
215 ret = e4000_wr_reg(priv, 0x1a, 0x00);
216 if (ret < 0)
217 goto err;
218
219 /* PLL */
220 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
221 if (c->frequency <= e4000_pll_lut[i].freq)
222 break;
223 }
224
225 if (i == ARRAY_SIZE(e4000_pll_lut))
226 goto err;
227
228 /*
229 * Note: Currently f_VCO overflows when c->frequency is 1 073 741 824 Hz
230 * or more.
231 */
232 f_VCO = c->frequency * e4000_pll_lut[i].mul;
233 sigma_delta = 0x10000UL * (f_VCO % priv->cfg->clock) / priv->cfg->clock;
234 buf[0] = f_VCO / priv->cfg->clock;
235 buf[1] = (sigma_delta >> 0) & 0xff;
236 buf[2] = (sigma_delta >> 8) & 0xff;
237 buf[3] = 0x00;
238 buf[4] = e4000_pll_lut[i].div;
239
240 dev_dbg(&priv->i2c->dev, "%s: f_VCO=%u pll div=%d sigma_delta=%04x\n",
241 __func__, f_VCO, buf[0], sigma_delta);
242
243 ret = e4000_wr_regs(priv, 0x09, buf, 5);
244 if (ret < 0)
245 goto err;
246
247 /* LNA filter (RF filter) */
248 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
249 if (c->frequency <= e400_lna_filter_lut[i].freq)
250 break;
251 }
252
253 if (i == ARRAY_SIZE(e400_lna_filter_lut))
254 goto err;
255
256 ret = e4000_wr_reg(priv, 0x10, e400_lna_filter_lut[i].val);
257 if (ret < 0)
258 goto err;
259
260 /* IF filters */
261 for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
262 if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
263 break;
264 }
265
266 if (i == ARRAY_SIZE(e4000_if_filter_lut))
267 goto err;
268
269 buf[0] = e4000_if_filter_lut[i].reg11_val;
270 buf[1] = e4000_if_filter_lut[i].reg12_val;
271
272 ret = e4000_wr_regs(priv, 0x11, buf, 2);
273 if (ret < 0)
274 goto err;
275
276 /* frequency band */
277 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
278 if (c->frequency <= e4000_band_lut[i].freq)
279 break;
280 }
281
282 if (i == ARRAY_SIZE(e4000_band_lut))
283 goto err;
284
285 ret = e4000_wr_reg(priv, 0x07, e4000_band_lut[i].reg07_val);
286 if (ret < 0)
287 goto err;
288
289 ret = e4000_wr_reg(priv, 0x78, e4000_band_lut[i].reg78_val);
290 if (ret < 0)
291 goto err;
292
Antti Palosaari85146112013-07-24 02:04:12 -0300293 /* DC offset */
294 for (i = 0; i < 4; i++) {
295 if (i == 0)
296 ret = e4000_wr_regs(priv, 0x15, "\x00\x7e\x24", 3);
297 else if (i == 1)
298 ret = e4000_wr_regs(priv, 0x15, "\x00\x7f", 2);
299 else if (i == 2)
300 ret = e4000_wr_regs(priv, 0x15, "\x01", 1);
301 else
302 ret = e4000_wr_regs(priv, 0x16, "\x7e", 1);
303
304 if (ret < 0)
305 goto err;
306
307 ret = e4000_wr_reg(priv, 0x29, 0x01);
308 if (ret < 0)
309 goto err;
310
311 ret = e4000_rd_regs(priv, 0x2a, buf, 3);
312 if (ret < 0)
313 goto err;
314
315 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
316 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
317 }
318
Antti Palosaarid4992da2013-07-24 18:33:51 -0300319 swap(q_data[2], q_data[3]);
320 swap(i_data[2], i_data[3]);
321
322 ret = e4000_wr_regs(priv, 0x50, q_data, 4);
Antti Palosaari85146112013-07-24 02:04:12 -0300323 if (ret < 0)
324 goto err;
325
Antti Palosaarid4992da2013-07-24 18:33:51 -0300326 ret = e4000_wr_regs(priv, 0x60, i_data, 4);
Antti Palosaari85146112013-07-24 02:04:12 -0300327 if (ret < 0)
328 goto err;
329
Antti Palosaaried85ada2012-09-01 21:09:21 -0300330 /* gain control auto */
331 ret = e4000_wr_reg(priv, 0x1a, 0x17);
332 if (ret < 0)
333 goto err;
334
335 if (fe->ops.i2c_gate_ctrl)
336 fe->ops.i2c_gate_ctrl(fe, 0);
337
338 return 0;
339err:
340 if (fe->ops.i2c_gate_ctrl)
341 fe->ops.i2c_gate_ctrl(fe, 0);
342
343 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
344 return ret;
345}
346
347static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
348{
349 struct e4000_priv *priv = fe->tuner_priv;
350
351 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
352
353 *frequency = 0; /* Zero-IF */
354
355 return 0;
356}
357
358static int e4000_release(struct dvb_frontend *fe)
359{
360 struct e4000_priv *priv = fe->tuner_priv;
361
362 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
363
364 kfree(fe->tuner_priv);
365
366 return 0;
367}
368
369static const struct dvb_tuner_ops e4000_tuner_ops = {
370 .info = {
371 .name = "Elonics E4000",
372 .frequency_min = 174000000,
373 .frequency_max = 862000000,
374 },
375
376 .release = e4000_release,
377
378 .init = e4000_init,
379 .sleep = e4000_sleep,
380 .set_params = e4000_set_params,
381
382 .get_if_frequency = e4000_get_if_frequency,
383};
384
385struct dvb_frontend *e4000_attach(struct dvb_frontend *fe,
386 struct i2c_adapter *i2c, const struct e4000_config *cfg)
387{
388 struct e4000_priv *priv;
389 int ret;
390 u8 chip_id;
391
392 if (fe->ops.i2c_gate_ctrl)
393 fe->ops.i2c_gate_ctrl(fe, 1);
394
395 priv = kzalloc(sizeof(struct e4000_priv), GFP_KERNEL);
396 if (!priv) {
397 ret = -ENOMEM;
398 dev_err(&i2c->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
399 goto err;
400 }
401
402 priv->cfg = cfg;
403 priv->i2c = i2c;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300404
405 /* check if the tuner is there */
406 ret = e4000_rd_reg(priv, 0x02, &chip_id);
407 if (ret < 0)
408 goto err;
409
410 dev_dbg(&priv->i2c->dev, "%s: chip_id=%02x\n", __func__, chip_id);
411
412 if (chip_id != 0x40)
413 goto err;
414
415 /* put sleep as chip seems to be in normal mode by default */
416 ret = e4000_wr_reg(priv, 0x00, 0x00);
417 if (ret < 0)
418 goto err;
419
420 dev_info(&priv->i2c->dev,
421 "%s: Elonics E4000 successfully identified\n",
422 KBUILD_MODNAME);
423
Antti Palosaari36f647b2012-09-22 12:32:27 -0300424 fe->tuner_priv = priv;
425 memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
426 sizeof(struct dvb_tuner_ops));
427
Antti Palosaaried85ada2012-09-01 21:09:21 -0300428 if (fe->ops.i2c_gate_ctrl)
429 fe->ops.i2c_gate_ctrl(fe, 0);
430
431 return fe;
432err:
433 if (fe->ops.i2c_gate_ctrl)
434 fe->ops.i2c_gate_ctrl(fe, 0);
435
436 dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
437 kfree(priv);
438 return NULL;
439}
440EXPORT_SYMBOL(e4000_attach);
441
442MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
443MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
444MODULE_LICENSE("GPL");