blob: 3a03b026eb0c1636f8f207362fb4f9771be47b10 [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"
Antti Palosaari35c00c92013-09-02 13:06:13 -030022#include <linux/math64.h>
Antti Palosaaried85ada2012-09-01 21:09:21 -030023
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030024/* Max transfer size done by I2C transfer functions */
25#define MAX_XFER_SIZE 64
26
Antti Palosaaried85ada2012-09-01 21:09:21 -030027/* write multiple registers */
28static int e4000_wr_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
29{
30 int ret;
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030031 u8 buf[MAX_XFER_SIZE];
Antti Palosaaried85ada2012-09-01 21:09:21 -030032 struct i2c_msg msg[1] = {
33 {
Antti Palosaari28fd31f2013-10-15 19:22:45 -030034 .addr = priv->client->addr,
Antti Palosaaried85ada2012-09-01 21:09:21 -030035 .flags = 0,
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030036 .len = 1 + len,
Antti Palosaaried85ada2012-09-01 21:09:21 -030037 .buf = buf,
38 }
39 };
40
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030041 if (1 + len > sizeof(buf)) {
Antti Palosaari28fd31f2013-10-15 19:22:45 -030042 dev_warn(&priv->client->dev,
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030043 "%s: i2c wr reg=%04x: len=%d is too big!\n",
44 KBUILD_MODNAME, reg, len);
45 return -EINVAL;
46 }
47
Antti Palosaaried85ada2012-09-01 21:09:21 -030048 buf[0] = reg;
49 memcpy(&buf[1], val, len);
50
Antti Palosaari28fd31f2013-10-15 19:22:45 -030051 ret = i2c_transfer(priv->client->adapter, msg, 1);
Antti Palosaaried85ada2012-09-01 21:09:21 -030052 if (ret == 1) {
53 ret = 0;
54 } else {
Antti Palosaari28fd31f2013-10-15 19:22:45 -030055 dev_warn(&priv->client->dev,
Antti Palosaari4a337d52013-07-24 18:38:29 -030056 "%s: i2c wr failed=%d reg=%02x len=%d\n",
57 KBUILD_MODNAME, ret, reg, len);
Antti Palosaaried85ada2012-09-01 21:09:21 -030058 ret = -EREMOTEIO;
59 }
60 return ret;
61}
62
63/* read multiple registers */
64static int e4000_rd_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
65{
66 int ret;
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030067 u8 buf[MAX_XFER_SIZE];
Antti Palosaaried85ada2012-09-01 21:09:21 -030068 struct i2c_msg msg[2] = {
69 {
Antti Palosaari28fd31f2013-10-15 19:22:45 -030070 .addr = priv->client->addr,
Antti Palosaaried85ada2012-09-01 21:09:21 -030071 .flags = 0,
72 .len = 1,
73 .buf = &reg,
74 }, {
Antti Palosaari28fd31f2013-10-15 19:22:45 -030075 .addr = priv->client->addr,
Antti Palosaaried85ada2012-09-01 21:09:21 -030076 .flags = I2C_M_RD,
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030077 .len = len,
Antti Palosaaried85ada2012-09-01 21:09:21 -030078 .buf = buf,
79 }
80 };
81
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030082 if (len > sizeof(buf)) {
Antti Palosaari28fd31f2013-10-15 19:22:45 -030083 dev_warn(&priv->client->dev,
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030084 "%s: i2c rd reg=%04x: len=%d is too big!\n",
85 KBUILD_MODNAME, reg, len);
86 return -EINVAL;
87 }
88
Antti Palosaari28fd31f2013-10-15 19:22:45 -030089 ret = i2c_transfer(priv->client->adapter, msg, 2);
Antti Palosaaried85ada2012-09-01 21:09:21 -030090 if (ret == 2) {
91 memcpy(val, buf, len);
92 ret = 0;
93 } else {
Antti Palosaari28fd31f2013-10-15 19:22:45 -030094 dev_warn(&priv->client->dev,
Antti Palosaari4a337d52013-07-24 18:38:29 -030095 "%s: i2c rd failed=%d reg=%02x len=%d\n",
96 KBUILD_MODNAME, ret, reg, len);
Antti Palosaaried85ada2012-09-01 21:09:21 -030097 ret = -EREMOTEIO;
98 }
99
100 return ret;
101}
102
103/* write single register */
104static int e4000_wr_reg(struct e4000_priv *priv, u8 reg, u8 val)
105{
106 return e4000_wr_regs(priv, reg, &val, 1);
107}
108
109/* read single register */
110static int e4000_rd_reg(struct e4000_priv *priv, u8 reg, u8 *val)
111{
112 return e4000_rd_regs(priv, reg, val, 1);
113}
114
115static int e4000_init(struct dvb_frontend *fe)
116{
117 struct e4000_priv *priv = fe->tuner_priv;
118 int ret;
119
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300120 dev_dbg(&priv->client->dev, "%s:\n", __func__);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300121
122 if (fe->ops.i2c_gate_ctrl)
123 fe->ops.i2c_gate_ctrl(fe, 1);
124
125 /* dummy I2C to ensure I2C wakes up */
126 ret = e4000_wr_reg(priv, 0x02, 0x40);
127
128 /* reset */
129 ret = e4000_wr_reg(priv, 0x00, 0x01);
130 if (ret < 0)
131 goto err;
132
133 /* disable output clock */
134 ret = e4000_wr_reg(priv, 0x06, 0x00);
135 if (ret < 0)
136 goto err;
137
138 ret = e4000_wr_reg(priv, 0x7a, 0x96);
139 if (ret < 0)
140 goto err;
141
142 /* configure gains */
143 ret = e4000_wr_regs(priv, 0x7e, "\x01\xfe", 2);
144 if (ret < 0)
145 goto err;
146
147 ret = e4000_wr_reg(priv, 0x82, 0x00);
148 if (ret < 0)
149 goto err;
150
151 ret = e4000_wr_reg(priv, 0x24, 0x05);
152 if (ret < 0)
153 goto err;
154
155 ret = e4000_wr_regs(priv, 0x87, "\x20\x01", 2);
156 if (ret < 0)
157 goto err;
158
159 ret = e4000_wr_regs(priv, 0x9f, "\x7f\x07", 2);
160 if (ret < 0)
161 goto err;
162
Antti Palosaaried85ada2012-09-01 21:09:21 -0300163 /* DC offset control */
Antti Palosaari85146112013-07-24 02:04:12 -0300164 ret = e4000_wr_reg(priv, 0x2d, 0x1f);
165 if (ret < 0)
166 goto err;
167
168 ret = e4000_wr_regs(priv, 0x70, "\x01\x01", 2);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300169 if (ret < 0)
170 goto err;
171
172 /* gain control */
173 ret = e4000_wr_reg(priv, 0x1a, 0x17);
174 if (ret < 0)
175 goto err;
176
177 ret = e4000_wr_reg(priv, 0x1f, 0x1a);
178 if (ret < 0)
179 goto err;
180
181 if (fe->ops.i2c_gate_ctrl)
182 fe->ops.i2c_gate_ctrl(fe, 0);
183
184 return 0;
185err:
186 if (fe->ops.i2c_gate_ctrl)
187 fe->ops.i2c_gate_ctrl(fe, 0);
188
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300189 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300190 return ret;
191}
192
193static int e4000_sleep(struct dvb_frontend *fe)
194{
195 struct e4000_priv *priv = fe->tuner_priv;
196 int ret;
197
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300198 dev_dbg(&priv->client->dev, "%s:\n", __func__);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300199
200 if (fe->ops.i2c_gate_ctrl)
201 fe->ops.i2c_gate_ctrl(fe, 1);
202
203 ret = e4000_wr_reg(priv, 0x00, 0x00);
204 if (ret < 0)
205 goto err;
206
207 if (fe->ops.i2c_gate_ctrl)
208 fe->ops.i2c_gate_ctrl(fe, 0);
209
210 return 0;
211err:
212 if (fe->ops.i2c_gate_ctrl)
213 fe->ops.i2c_gate_ctrl(fe, 0);
214
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300215 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300216 return ret;
217}
218
219static int e4000_set_params(struct dvb_frontend *fe)
220{
221 struct e4000_priv *priv = fe->tuner_priv;
222 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
223 int ret, i, sigma_delta;
Antti Palosaari4a337d52013-07-24 18:38:29 -0300224 unsigned int f_vco;
Antti Palosaari85146112013-07-24 02:04:12 -0300225 u8 buf[5], i_data[4], q_data[4];
Antti Palosaaried85ada2012-09-01 21:09:21 -0300226
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300227 dev_dbg(&priv->client->dev,
Antti Palosaari4a337d52013-07-24 18:38:29 -0300228 "%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
229 __func__, c->delivery_system, c->frequency,
230 c->bandwidth_hz);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300231
232 if (fe->ops.i2c_gate_ctrl)
233 fe->ops.i2c_gate_ctrl(fe, 1);
234
235 /* gain control manual */
236 ret = e4000_wr_reg(priv, 0x1a, 0x00);
237 if (ret < 0)
238 goto err;
239
240 /* PLL */
241 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
242 if (c->frequency <= e4000_pll_lut[i].freq)
243 break;
244 }
245
Julia Lawall58f087c2013-12-29 19:47:35 -0300246 if (i == ARRAY_SIZE(e4000_pll_lut)) {
247 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300248 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300249 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300250
251 /*
Antti Palosaari4a337d52013-07-24 18:38:29 -0300252 * Note: Currently f_vco overflows when c->frequency is 1 073 741 824 Hz
Antti Palosaaried85ada2012-09-01 21:09:21 -0300253 * or more.
254 */
Antti Palosaari4a337d52013-07-24 18:38:29 -0300255 f_vco = c->frequency * e4000_pll_lut[i].mul;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300256 sigma_delta = div_u64(0x10000ULL * (f_vco % priv->clock), priv->clock);
257 buf[0] = f_vco / priv->clock;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300258 buf[1] = (sigma_delta >> 0) & 0xff;
259 buf[2] = (sigma_delta >> 8) & 0xff;
260 buf[3] = 0x00;
261 buf[4] = e4000_pll_lut[i].div;
262
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300263 dev_dbg(&priv->client->dev,
264 "%s: f_vco=%u pll div=%d sigma_delta=%04x\n",
Antti Palosaari4a337d52013-07-24 18:38:29 -0300265 __func__, f_vco, buf[0], sigma_delta);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300266
267 ret = e4000_wr_regs(priv, 0x09, buf, 5);
268 if (ret < 0)
269 goto err;
270
271 /* LNA filter (RF filter) */
272 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
273 if (c->frequency <= e400_lna_filter_lut[i].freq)
274 break;
275 }
276
Julia Lawall58f087c2013-12-29 19:47:35 -0300277 if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
278 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300279 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300280 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300281
282 ret = e4000_wr_reg(priv, 0x10, e400_lna_filter_lut[i].val);
283 if (ret < 0)
284 goto err;
285
286 /* IF filters */
287 for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
288 if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
289 break;
290 }
291
Julia Lawall58f087c2013-12-29 19:47:35 -0300292 if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
293 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300294 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300295 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300296
297 buf[0] = e4000_if_filter_lut[i].reg11_val;
298 buf[1] = e4000_if_filter_lut[i].reg12_val;
299
300 ret = e4000_wr_regs(priv, 0x11, buf, 2);
301 if (ret < 0)
302 goto err;
303
304 /* frequency band */
305 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
306 if (c->frequency <= e4000_band_lut[i].freq)
307 break;
308 }
309
Julia Lawall58f087c2013-12-29 19:47:35 -0300310 if (i == ARRAY_SIZE(e4000_band_lut)) {
311 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300312 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300313 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300314
315 ret = e4000_wr_reg(priv, 0x07, e4000_band_lut[i].reg07_val);
316 if (ret < 0)
317 goto err;
318
319 ret = e4000_wr_reg(priv, 0x78, e4000_band_lut[i].reg78_val);
320 if (ret < 0)
321 goto err;
322
Antti Palosaari85146112013-07-24 02:04:12 -0300323 /* DC offset */
324 for (i = 0; i < 4; i++) {
325 if (i == 0)
326 ret = e4000_wr_regs(priv, 0x15, "\x00\x7e\x24", 3);
327 else if (i == 1)
328 ret = e4000_wr_regs(priv, 0x15, "\x00\x7f", 2);
329 else if (i == 2)
330 ret = e4000_wr_regs(priv, 0x15, "\x01", 1);
331 else
332 ret = e4000_wr_regs(priv, 0x16, "\x7e", 1);
333
334 if (ret < 0)
335 goto err;
336
337 ret = e4000_wr_reg(priv, 0x29, 0x01);
338 if (ret < 0)
339 goto err;
340
341 ret = e4000_rd_regs(priv, 0x2a, buf, 3);
342 if (ret < 0)
343 goto err;
344
345 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
346 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
347 }
348
Antti Palosaarid4992da2013-07-24 18:33:51 -0300349 swap(q_data[2], q_data[3]);
350 swap(i_data[2], i_data[3]);
351
352 ret = e4000_wr_regs(priv, 0x50, q_data, 4);
Antti Palosaari85146112013-07-24 02:04:12 -0300353 if (ret < 0)
354 goto err;
355
Antti Palosaarid4992da2013-07-24 18:33:51 -0300356 ret = e4000_wr_regs(priv, 0x60, i_data, 4);
Antti Palosaari85146112013-07-24 02:04:12 -0300357 if (ret < 0)
358 goto err;
359
Antti Palosaaried85ada2012-09-01 21:09:21 -0300360 /* gain control auto */
361 ret = e4000_wr_reg(priv, 0x1a, 0x17);
362 if (ret < 0)
363 goto err;
364
365 if (fe->ops.i2c_gate_ctrl)
366 fe->ops.i2c_gate_ctrl(fe, 0);
367
368 return 0;
369err:
370 if (fe->ops.i2c_gate_ctrl)
371 fe->ops.i2c_gate_ctrl(fe, 0);
372
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300373 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300374 return ret;
375}
376
377static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
378{
379 struct e4000_priv *priv = fe->tuner_priv;
380
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300381 dev_dbg(&priv->client->dev, "%s:\n", __func__);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300382
383 *frequency = 0; /* Zero-IF */
384
385 return 0;
386}
387
Antti Palosaariadaa6162014-01-26 21:02:53 -0300388static int e4000_set_lna_gain(struct dvb_frontend *fe)
389{
390 struct e4000_priv *priv = fe->tuner_priv;
391 int ret;
392 u8 u8tmp;
393 dev_dbg(&priv->client->dev, "%s: lna auto=%d->%d val=%d->%d\n",
394 __func__, priv->lna_gain_auto->cur.val,
395 priv->lna_gain_auto->val, priv->lna_gain->cur.val,
396 priv->lna_gain->val);
397
398 if (fe->ops.i2c_gate_ctrl)
399 fe->ops.i2c_gate_ctrl(fe, 1);
400
401 if (priv->lna_gain_auto->val && priv->if_gain_auto->cur.val)
402 u8tmp = 0x17;
403 else if (priv->lna_gain_auto->val)
404 u8tmp = 0x19;
405 else if (priv->if_gain_auto->cur.val)
406 u8tmp = 0x16;
407 else
408 u8tmp = 0x10;
409
410 ret = e4000_wr_reg(priv, 0x1a, u8tmp);
411 if (ret)
412 goto err;
413
414 if (priv->lna_gain_auto->val == false) {
415 ret = e4000_wr_reg(priv, 0x14, priv->lna_gain->val);
416 if (ret)
417 goto err;
418 }
419
420 if (fe->ops.i2c_gate_ctrl)
421 fe->ops.i2c_gate_ctrl(fe, 0);
422
423 return 0;
424err:
425 if (fe->ops.i2c_gate_ctrl)
426 fe->ops.i2c_gate_ctrl(fe, 0);
427
428 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
429 return ret;
430}
431
432static int e4000_set_mixer_gain(struct dvb_frontend *fe)
433{
434 struct e4000_priv *priv = fe->tuner_priv;
435 int ret;
436 u8 u8tmp;
437 dev_dbg(&priv->client->dev, "%s: mixer auto=%d->%d val=%d->%d\n",
438 __func__, priv->mixer_gain_auto->cur.val,
439 priv->mixer_gain_auto->val, priv->mixer_gain->cur.val,
440 priv->mixer_gain->val);
441
442 if (fe->ops.i2c_gate_ctrl)
443 fe->ops.i2c_gate_ctrl(fe, 1);
444
445 if (priv->mixer_gain_auto->val)
446 u8tmp = 0x15;
447 else
448 u8tmp = 0x14;
449
450 ret = e4000_wr_reg(priv, 0x20, u8tmp);
451 if (ret)
452 goto err;
453
454 if (priv->mixer_gain_auto->val == false) {
455 ret = e4000_wr_reg(priv, 0x15, priv->mixer_gain->val);
456 if (ret)
457 goto err;
458 }
459
460 if (fe->ops.i2c_gate_ctrl)
461 fe->ops.i2c_gate_ctrl(fe, 0);
462
463 return 0;
464err:
465 if (fe->ops.i2c_gate_ctrl)
466 fe->ops.i2c_gate_ctrl(fe, 0);
467
468 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
469 return ret;
470}
471
472static int e4000_set_if_gain(struct dvb_frontend *fe)
473{
474 struct e4000_priv *priv = fe->tuner_priv;
475 int ret;
476 u8 buf[2];
477 u8 u8tmp;
478 dev_dbg(&priv->client->dev, "%s: if auto=%d->%d val=%d->%d\n",
479 __func__, priv->if_gain_auto->cur.val,
480 priv->if_gain_auto->val, priv->if_gain->cur.val,
481 priv->if_gain->val);
482
483 if (fe->ops.i2c_gate_ctrl)
484 fe->ops.i2c_gate_ctrl(fe, 1);
485
486 if (priv->if_gain_auto->val && priv->lna_gain_auto->cur.val)
487 u8tmp = 0x17;
488 else if (priv->lna_gain_auto->cur.val)
489 u8tmp = 0x19;
490 else if (priv->if_gain_auto->val)
491 u8tmp = 0x16;
492 else
493 u8tmp = 0x10;
494
495 ret = e4000_wr_reg(priv, 0x1a, u8tmp);
496 if (ret)
497 goto err;
498
499 if (priv->if_gain_auto->val == false) {
500 buf[0] = e4000_if_gain_lut[priv->if_gain->val].reg16_val;
501 buf[1] = e4000_if_gain_lut[priv->if_gain->val].reg17_val;
502 ret = e4000_wr_regs(priv, 0x16, buf, 2);
503 if (ret)
504 goto err;
505 }
506
507 if (fe->ops.i2c_gate_ctrl)
508 fe->ops.i2c_gate_ctrl(fe, 0);
509
510 return 0;
511err:
512 if (fe->ops.i2c_gate_ctrl)
513 fe->ops.i2c_gate_ctrl(fe, 0);
514
515 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
516 return ret;
517}
518
519static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
520{
521 struct e4000_priv *priv =
522 container_of(ctrl->handler, struct e4000_priv, hdl);
523 struct dvb_frontend *fe = priv->fe;
524 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
525 int ret;
526 dev_dbg(&priv->client->dev,
527 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
528 __func__, ctrl->id, ctrl->name, ctrl->val,
529 ctrl->minimum, ctrl->maximum, ctrl->step);
530
531 switch (ctrl->id) {
532 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
533 case V4L2_CID_RF_TUNER_BANDWIDTH:
534 c->bandwidth_hz = priv->bandwidth->val;
535 ret = e4000_set_params(priv->fe);
536 break;
537 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
538 case V4L2_CID_RF_TUNER_LNA_GAIN:
539 ret = e4000_set_lna_gain(priv->fe);
540 break;
541 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
542 case V4L2_CID_RF_TUNER_MIXER_GAIN:
543 ret = e4000_set_mixer_gain(priv->fe);
544 break;
545 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
546 case V4L2_CID_RF_TUNER_IF_GAIN:
547 ret = e4000_set_if_gain(priv->fe);
548 break;
549 default:
550 ret = -EINVAL;
551 }
552
553 return ret;
554}
555
556static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
557 .s_ctrl = e4000_s_ctrl,
558};
559
Antti Palosaaried85ada2012-09-01 21:09:21 -0300560static const struct dvb_tuner_ops e4000_tuner_ops = {
561 .info = {
562 .name = "Elonics E4000",
563 .frequency_min = 174000000,
564 .frequency_max = 862000000,
565 },
566
Antti Palosaaried85ada2012-09-01 21:09:21 -0300567 .init = e4000_init,
568 .sleep = e4000_sleep,
569 .set_params = e4000_set_params,
570
571 .get_if_frequency = e4000_get_if_frequency,
572};
573
Antti Palosaariadaa6162014-01-26 21:02:53 -0300574/*
575 * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
576 * subdev itself, just to avoid reinventing the wheel.
577 */
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300578static int e4000_probe(struct i2c_client *client,
579 const struct i2c_device_id *id)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300580{
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300581 struct e4000_config *cfg = client->dev.platform_data;
582 struct dvb_frontend *fe = cfg->fe;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300583 struct e4000_priv *priv;
584 int ret;
585 u8 chip_id;
586
587 if (fe->ops.i2c_gate_ctrl)
588 fe->ops.i2c_gate_ctrl(fe, 1);
589
590 priv = kzalloc(sizeof(struct e4000_priv), GFP_KERNEL);
591 if (!priv) {
592 ret = -ENOMEM;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300593 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300594 goto err;
595 }
596
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300597 priv->clock = cfg->clock;
598 priv->client = client;
599 priv->fe = cfg->fe;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300600
601 /* check if the tuner is there */
602 ret = e4000_rd_reg(priv, 0x02, &chip_id);
603 if (ret < 0)
604 goto err;
605
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300606 dev_dbg(&priv->client->dev,
607 "%s: chip_id=%02x\n", __func__, chip_id);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300608
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300609 if (chip_id != 0x40) {
610 ret = -ENODEV;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300611 goto err;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300612 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300613
614 /* put sleep as chip seems to be in normal mode by default */
615 ret = e4000_wr_reg(priv, 0x00, 0x00);
616 if (ret < 0)
617 goto err;
618
Antti Palosaariadaa6162014-01-26 21:02:53 -0300619 /* Register controls */
620 v4l2_ctrl_handler_init(&priv->hdl, 8);
621 priv->bandwidth_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
622 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
623 priv->bandwidth = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
624 V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
625 v4l2_ctrl_auto_cluster(2, &priv->bandwidth_auto, 0, false);
626 priv->lna_gain_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
627 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
628 priv->lna_gain = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
629 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
630 v4l2_ctrl_auto_cluster(2, &priv->lna_gain_auto, 0, false);
631 priv->mixer_gain_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
632 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
633 priv->mixer_gain = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
634 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
635 v4l2_ctrl_auto_cluster(2, &priv->mixer_gain_auto, 0, false);
636 priv->if_gain_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
637 V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
638 priv->if_gain = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
639 V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
640 v4l2_ctrl_auto_cluster(2, &priv->if_gain_auto, 0, false);
641 if (priv->hdl.error) {
642 ret = priv->hdl.error;
643 dev_err(&priv->client->dev, "Could not initialize controls\n");
644 v4l2_ctrl_handler_free(&priv->hdl);
645 goto err;
646 }
647
648 priv->sd.ctrl_handler = &priv->hdl;
649
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300650 dev_info(&priv->client->dev,
Antti Palosaaried85ada2012-09-01 21:09:21 -0300651 "%s: Elonics E4000 successfully identified\n",
652 KBUILD_MODNAME);
653
Antti Palosaari36f647b2012-09-22 12:32:27 -0300654 fe->tuner_priv = priv;
655 memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
656 sizeof(struct dvb_tuner_ops));
657
Antti Palosaariadaa6162014-01-26 21:02:53 -0300658 v4l2_set_subdevdata(&priv->sd, client);
659 i2c_set_clientdata(client, &priv->sd);
660
Antti Palosaaried85ada2012-09-01 21:09:21 -0300661 if (fe->ops.i2c_gate_ctrl)
662 fe->ops.i2c_gate_ctrl(fe, 0);
663
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300664 return 0;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300665err:
666 if (fe->ops.i2c_gate_ctrl)
667 fe->ops.i2c_gate_ctrl(fe, 0);
668
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300669 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300670 kfree(priv);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300671 return ret;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300672}
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300673
674static int e4000_remove(struct i2c_client *client)
675{
Antti Palosaariadaa6162014-01-26 21:02:53 -0300676 struct v4l2_subdev *sd = i2c_get_clientdata(client);
677 struct e4000_priv *priv = container_of(sd, struct e4000_priv, sd);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300678 struct dvb_frontend *fe = priv->fe;
679
680 dev_dbg(&client->dev, "%s:\n", __func__);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300681 v4l2_ctrl_handler_free(&priv->hdl);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300682 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
683 fe->tuner_priv = NULL;
684 kfree(priv);
685
686 return 0;
687}
688
689static const struct i2c_device_id e4000_id[] = {
690 {"e4000", 0},
691 {}
692};
693MODULE_DEVICE_TABLE(i2c, e4000_id);
694
695static struct i2c_driver e4000_driver = {
696 .driver = {
697 .owner = THIS_MODULE,
698 .name = "e4000",
699 },
700 .probe = e4000_probe,
701 .remove = e4000_remove,
702 .id_table = e4000_id,
703};
704
705module_i2c_driver(e4000_driver);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300706
707MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
708MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
709MODULE_LICENSE("GPL");