blob: 510239f80c0d9f5bb173a8f71d2c784de8b60201 [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
Antti Palosaaried85ada2012-09-01 21:09:21 -030024static int e4000_init(struct dvb_frontend *fe)
25{
Antti Palosaaric5f51b12014-02-10 22:52:51 -030026 struct e4000 *s = fe->tuner_priv;
Antti Palosaaried85ada2012-09-01 21:09:21 -030027 int ret;
28
Antti Palosaari13bd82d2014-08-24 23:35:48 -030029 dev_dbg(&s->client->dev, "\n");
Antti Palosaaried85ada2012-09-01 21:09:21 -030030
Antti Palosaaried85ada2012-09-01 21:09:21 -030031 /* dummy I2C to ensure I2C wakes up */
Antti Palosaaric5f51b12014-02-10 22:52:51 -030032 ret = regmap_write(s->regmap, 0x02, 0x40);
Antti Palosaaried85ada2012-09-01 21:09:21 -030033
34 /* reset */
Antti Palosaaric5f51b12014-02-10 22:52:51 -030035 ret = regmap_write(s->regmap, 0x00, 0x01);
36 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030037 goto err;
38
39 /* disable output clock */
Antti Palosaaric5f51b12014-02-10 22:52:51 -030040 ret = regmap_write(s->regmap, 0x06, 0x00);
41 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030042 goto err;
43
Antti Palosaaric5f51b12014-02-10 22:52:51 -030044 ret = regmap_write(s->regmap, 0x7a, 0x96);
45 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030046 goto err;
47
48 /* configure gains */
Antti Palosaaric5f51b12014-02-10 22:52:51 -030049 ret = regmap_bulk_write(s->regmap, 0x7e, "\x01\xfe", 2);
50 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030051 goto err;
52
Antti Palosaaric5f51b12014-02-10 22:52:51 -030053 ret = regmap_write(s->regmap, 0x82, 0x00);
54 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030055 goto err;
56
Antti Palosaaric5f51b12014-02-10 22:52:51 -030057 ret = regmap_write(s->regmap, 0x24, 0x05);
58 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030059 goto err;
60
Antti Palosaaric5f51b12014-02-10 22:52:51 -030061 ret = regmap_bulk_write(s->regmap, 0x87, "\x20\x01", 2);
62 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030063 goto err;
64
Antti Palosaaric5f51b12014-02-10 22:52:51 -030065 ret = regmap_bulk_write(s->regmap, 0x9f, "\x7f\x07", 2);
66 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030067 goto err;
68
Antti Palosaaried85ada2012-09-01 21:09:21 -030069 /* DC offset control */
Antti Palosaaric5f51b12014-02-10 22:52:51 -030070 ret = regmap_write(s->regmap, 0x2d, 0x1f);
71 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -030072 goto err;
73
Antti Palosaaric5f51b12014-02-10 22:52:51 -030074 ret = regmap_bulk_write(s->regmap, 0x70, "\x01\x01", 2);
75 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030076 goto err;
77
78 /* gain control */
Antti Palosaaric5f51b12014-02-10 22:52:51 -030079 ret = regmap_write(s->regmap, 0x1a, 0x17);
80 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030081 goto err;
82
Antti Palosaaric5f51b12014-02-10 22:52:51 -030083 ret = regmap_write(s->regmap, 0x1f, 0x1a);
84 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030085 goto err;
86
Antti Palosaaric5f51b12014-02-10 22:52:51 -030087 s->active = true;
Antti Palosaaried85ada2012-09-01 21:09:21 -030088err:
Antti Palosaari1c73fc62014-02-08 04:21:10 -030089 if (ret)
Antti Palosaari13bd82d2014-08-24 23:35:48 -030090 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -030091
Antti Palosaaried85ada2012-09-01 21:09:21 -030092 return ret;
93}
94
95static int e4000_sleep(struct dvb_frontend *fe)
96{
Antti Palosaaric5f51b12014-02-10 22:52:51 -030097 struct e4000 *s = fe->tuner_priv;
Antti Palosaaried85ada2012-09-01 21:09:21 -030098 int ret;
99
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300100 dev_dbg(&s->client->dev, "\n");
Antti Palosaaried85ada2012-09-01 21:09:21 -0300101
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300102 s->active = false;
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300103
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300104 ret = regmap_write(s->regmap, 0x00, 0x00);
105 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300106 goto err;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300107err:
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300108 if (ret)
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300109 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300110
Antti Palosaaried85ada2012-09-01 21:09:21 -0300111 return ret;
112}
113
114static int e4000_set_params(struct dvb_frontend *fe)
115{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300116 struct e4000 *s = fe->tuner_priv;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300117 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
118 int ret, i, sigma_delta;
Antti Palosaariac8f3922014-03-14 15:22:24 -0300119 unsigned int pll_n, pll_f;
Antti Palosaari0ed0b222014-01-27 03:13:19 -0300120 u64 f_vco;
Antti Palosaari85146112013-07-24 02:04:12 -0300121 u8 buf[5], i_data[4], q_data[4];
Antti Palosaaried85ada2012-09-01 21:09:21 -0300122
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300123 dev_dbg(&s->client->dev,
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300124 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
125 c->delivery_system, c->frequency, c->bandwidth_hz);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300126
Antti Palosaaried85ada2012-09-01 21:09:21 -0300127 /* gain control manual */
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300128 ret = regmap_write(s->regmap, 0x1a, 0x00);
129 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300130 goto err;
131
132 /* PLL */
133 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
134 if (c->frequency <= e4000_pll_lut[i].freq)
135 break;
136 }
137
Julia Lawall58f087c2013-12-29 19:47:35 -0300138 if (i == ARRAY_SIZE(e4000_pll_lut)) {
139 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300140 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300141 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300142
Antti Palosaari0ed0b222014-01-27 03:13:19 -0300143 f_vco = 1ull * c->frequency * e4000_pll_lut[i].mul;
Antti Palosaariac8f3922014-03-14 15:22:24 -0300144 pll_n = div_u64_rem(f_vco, s->clock, &pll_f);
145 sigma_delta = div_u64(0x10000ULL * pll_f, s->clock);
146 buf[0] = pll_n;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300147 buf[1] = (sigma_delta >> 0) & 0xff;
148 buf[2] = (sigma_delta >> 8) & 0xff;
149 buf[3] = 0x00;
150 buf[4] = e4000_pll_lut[i].div;
151
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300152 dev_dbg(&s->client->dev, "f_vco=%llu pll div=%d sigma_delta=%04x\n",
153 f_vco, buf[0], sigma_delta);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300154
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300155 ret = regmap_bulk_write(s->regmap, 0x09, buf, 5);
156 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300157 goto err;
158
159 /* LNA filter (RF filter) */
160 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
161 if (c->frequency <= e400_lna_filter_lut[i].freq)
162 break;
163 }
164
Julia Lawall58f087c2013-12-29 19:47:35 -0300165 if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
166 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300167 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300168 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300169
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300170 ret = regmap_write(s->regmap, 0x10, e400_lna_filter_lut[i].val);
171 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300172 goto err;
173
174 /* IF filters */
175 for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
176 if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
177 break;
178 }
179
Julia Lawall58f087c2013-12-29 19:47:35 -0300180 if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
181 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300182 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300183 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300184
185 buf[0] = e4000_if_filter_lut[i].reg11_val;
186 buf[1] = e4000_if_filter_lut[i].reg12_val;
187
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300188 ret = regmap_bulk_write(s->regmap, 0x11, buf, 2);
189 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300190 goto err;
191
192 /* frequency band */
193 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
194 if (c->frequency <= e4000_band_lut[i].freq)
195 break;
196 }
197
Julia Lawall58f087c2013-12-29 19:47:35 -0300198 if (i == ARRAY_SIZE(e4000_band_lut)) {
199 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300200 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300201 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300202
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300203 ret = regmap_write(s->regmap, 0x07, e4000_band_lut[i].reg07_val);
204 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300205 goto err;
206
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300207 ret = regmap_write(s->regmap, 0x78, e4000_band_lut[i].reg78_val);
208 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300209 goto err;
210
Antti Palosaari85146112013-07-24 02:04:12 -0300211 /* DC offset */
212 for (i = 0; i < 4; i++) {
213 if (i == 0)
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300214 ret = regmap_bulk_write(s->regmap, 0x15, "\x00\x7e\x24", 3);
Antti Palosaari85146112013-07-24 02:04:12 -0300215 else if (i == 1)
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300216 ret = regmap_bulk_write(s->regmap, 0x15, "\x00\x7f", 2);
Antti Palosaari85146112013-07-24 02:04:12 -0300217 else if (i == 2)
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300218 ret = regmap_bulk_write(s->regmap, 0x15, "\x01", 1);
Antti Palosaari85146112013-07-24 02:04:12 -0300219 else
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300220 ret = regmap_bulk_write(s->regmap, 0x16, "\x7e", 1);
Antti Palosaari85146112013-07-24 02:04:12 -0300221
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300222 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300223 goto err;
224
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300225 ret = regmap_write(s->regmap, 0x29, 0x01);
226 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300227 goto err;
228
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300229 ret = regmap_bulk_read(s->regmap, 0x2a, buf, 3);
230 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300231 goto err;
232
233 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
234 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
235 }
236
Antti Palosaarid4992da2013-07-24 18:33:51 -0300237 swap(q_data[2], q_data[3]);
238 swap(i_data[2], i_data[3]);
239
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300240 ret = regmap_bulk_write(s->regmap, 0x50, q_data, 4);
241 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300242 goto err;
243
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300244 ret = regmap_bulk_write(s->regmap, 0x60, i_data, 4);
245 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300246 goto err;
247
Antti Palosaaried85ada2012-09-01 21:09:21 -0300248 /* gain control auto */
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300249 ret = regmap_write(s->regmap, 0x1a, 0x17);
250 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300251 goto err;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300252err:
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300253 if (ret)
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300254 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300255
Antti Palosaaried85ada2012-09-01 21:09:21 -0300256 return ret;
257}
258
259static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
260{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300261 struct e4000 *s = fe->tuner_priv;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300262
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300263 dev_dbg(&s->client->dev, "\n");
Antti Palosaaried85ada2012-09-01 21:09:21 -0300264
265 *frequency = 0; /* Zero-IF */
266
267 return 0;
268}
269
Antti Palosaari320c6382014-03-16 18:13:05 -0300270#if IS_ENABLED(CONFIG_VIDEO_V4L2)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300271static int e4000_set_lna_gain(struct dvb_frontend *fe)
272{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300273 struct e4000 *s = fe->tuner_priv;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300274 int ret;
275 u8 u8tmp;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300276
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300277 dev_dbg(&s->client->dev, "lna auto=%d->%d val=%d->%d\n",
278 s->lna_gain_auto->cur.val, s->lna_gain_auto->val,
279 s->lna_gain->cur.val, s->lna_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300280
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300281 if (s->lna_gain_auto->val && s->if_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300282 u8tmp = 0x17;
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300283 else if (s->lna_gain_auto->val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300284 u8tmp = 0x19;
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300285 else if (s->if_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300286 u8tmp = 0x16;
287 else
288 u8tmp = 0x10;
289
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300290 ret = regmap_write(s->regmap, 0x1a, u8tmp);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300291 if (ret)
292 goto err;
293
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300294 if (s->lna_gain_auto->val == false) {
295 ret = regmap_write(s->regmap, 0x14, s->lna_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300296 if (ret)
297 goto err;
298 }
Antti Palosaariadaa6162014-01-26 21:02:53 -0300299err:
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300300 if (ret)
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300301 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300302
Antti Palosaariadaa6162014-01-26 21:02:53 -0300303 return ret;
304}
305
306static int e4000_set_mixer_gain(struct dvb_frontend *fe)
307{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300308 struct e4000 *s = fe->tuner_priv;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300309 int ret;
310 u8 u8tmp;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300311
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300312 dev_dbg(&s->client->dev, "mixer auto=%d->%d val=%d->%d\n",
313 s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val,
314 s->mixer_gain->cur.val, s->mixer_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300315
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300316 if (s->mixer_gain_auto->val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300317 u8tmp = 0x15;
318 else
319 u8tmp = 0x14;
320
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300321 ret = regmap_write(s->regmap, 0x20, u8tmp);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300322 if (ret)
323 goto err;
324
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300325 if (s->mixer_gain_auto->val == false) {
326 ret = regmap_write(s->regmap, 0x15, s->mixer_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300327 if (ret)
328 goto err;
329 }
Antti Palosaariadaa6162014-01-26 21:02:53 -0300330err:
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300331 if (ret)
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300332 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300333
Antti Palosaariadaa6162014-01-26 21:02:53 -0300334 return ret;
335}
336
337static int e4000_set_if_gain(struct dvb_frontend *fe)
338{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300339 struct e4000 *s = fe->tuner_priv;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300340 int ret;
341 u8 buf[2];
342 u8 u8tmp;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300343
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300344 dev_dbg(&s->client->dev, "if auto=%d->%d val=%d->%d\n",
345 s->if_gain_auto->cur.val, s->if_gain_auto->val,
346 s->if_gain->cur.val, s->if_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300347
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300348 if (s->if_gain_auto->val && s->lna_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300349 u8tmp = 0x17;
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300350 else if (s->lna_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300351 u8tmp = 0x19;
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300352 else if (s->if_gain_auto->val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300353 u8tmp = 0x16;
354 else
355 u8tmp = 0x10;
356
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300357 ret = regmap_write(s->regmap, 0x1a, u8tmp);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300358 if (ret)
359 goto err;
360
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300361 if (s->if_gain_auto->val == false) {
362 buf[0] = e4000_if_gain_lut[s->if_gain->val].reg16_val;
363 buf[1] = e4000_if_gain_lut[s->if_gain->val].reg17_val;
364 ret = regmap_bulk_write(s->regmap, 0x16, buf, 2);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300365 if (ret)
366 goto err;
367 }
Antti Palosaariadaa6162014-01-26 21:02:53 -0300368err:
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300369 if (ret)
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300370 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300371
Antti Palosaariadaa6162014-01-26 21:02:53 -0300372 return ret;
373}
374
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300375static int e4000_pll_lock(struct dvb_frontend *fe)
376{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300377 struct e4000 *s = fe->tuner_priv;
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300378 int ret;
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300379 unsigned int utmp;
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300380
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300381 ret = regmap_read(s->regmap, 0x07, &utmp);
382 if (ret)
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300383 goto err;
384
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300385 s->pll_lock->val = (utmp & 0x01);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300386err:
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300387 if (ret)
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300388 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300389
390 return ret;
391}
392
393static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
394{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300395 struct e4000 *s = container_of(ctrl->handler, struct e4000, hdl);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300396 int ret;
397
Mauro Carvalho Chehaba04557a2014-09-03 16:04:59 -0300398 if (!s->active)
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300399 return 0;
400
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300401 switch (ctrl->id) {
402 case V4L2_CID_RF_TUNER_PLL_LOCK:
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300403 ret = e4000_pll_lock(s->fe);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300404 break;
405 default:
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300406 dev_dbg(&s->client->dev, "unknown ctrl: id=%d name=%s\n",
407 ctrl->id, ctrl->name);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300408 ret = -EINVAL;
409 }
410
411 return ret;
412}
413
Antti Palosaariadaa6162014-01-26 21:02:53 -0300414static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
415{
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300416 struct e4000 *s = container_of(ctrl->handler, struct e4000, hdl);
417 struct dvb_frontend *fe = s->fe;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300418 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
419 int ret;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300420
Mauro Carvalho Chehaba04557a2014-09-03 16:04:59 -0300421 if (!s->active)
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300422 return 0;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300423
424 switch (ctrl->id) {
425 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
426 case V4L2_CID_RF_TUNER_BANDWIDTH:
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300427 c->bandwidth_hz = s->bandwidth->val;
428 ret = e4000_set_params(s->fe);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300429 break;
430 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
431 case V4L2_CID_RF_TUNER_LNA_GAIN:
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300432 ret = e4000_set_lna_gain(s->fe);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300433 break;
434 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
435 case V4L2_CID_RF_TUNER_MIXER_GAIN:
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300436 ret = e4000_set_mixer_gain(s->fe);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300437 break;
438 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
439 case V4L2_CID_RF_TUNER_IF_GAIN:
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300440 ret = e4000_set_if_gain(s->fe);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300441 break;
442 default:
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300443 dev_dbg(&s->client->dev, "unknown ctrl: id=%d name=%s\n",
444 ctrl->id, ctrl->name);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300445 ret = -EINVAL;
446 }
447
448 return ret;
449}
450
451static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300452 .g_volatile_ctrl = e4000_g_volatile_ctrl,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300453 .s_ctrl = e4000_s_ctrl,
454};
Antti Palosaari320c6382014-03-16 18:13:05 -0300455#endif
Antti Palosaariadaa6162014-01-26 21:02:53 -0300456
Antti Palosaaried85ada2012-09-01 21:09:21 -0300457static const struct dvb_tuner_ops e4000_tuner_ops = {
458 .info = {
459 .name = "Elonics E4000",
460 .frequency_min = 174000000,
461 .frequency_max = 862000000,
462 },
463
Antti Palosaaried85ada2012-09-01 21:09:21 -0300464 .init = e4000_init,
465 .sleep = e4000_sleep,
466 .set_params = e4000_set_params,
467
468 .get_if_frequency = e4000_get_if_frequency,
469};
470
Antti Palosaariadaa6162014-01-26 21:02:53 -0300471/*
472 * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
473 * subdev itself, just to avoid reinventing the wheel.
474 */
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300475static int e4000_probe(struct i2c_client *client,
476 const struct i2c_device_id *id)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300477{
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300478 struct e4000_config *cfg = client->dev.platform_data;
479 struct dvb_frontend *fe = cfg->fe;
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300480 struct e4000 *s;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300481 int ret;
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300482 unsigned int utmp;
483 static const struct regmap_config regmap_config = {
484 .reg_bits = 8,
485 .val_bits = 8,
486 .max_register = 0xff,
487 };
Antti Palosaaried85ada2012-09-01 21:09:21 -0300488
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300489 s = kzalloc(sizeof(struct e4000), GFP_KERNEL);
490 if (!s) {
Antti Palosaaried85ada2012-09-01 21:09:21 -0300491 ret = -ENOMEM;
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300492 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaaried85ada2012-09-01 21:09:21 -0300493 goto err;
494 }
495
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300496 s->clock = cfg->clock;
497 s->client = client;
498 s->fe = cfg->fe;
499 s->regmap = devm_regmap_init_i2c(client, &regmap_config);
500 if (IS_ERR(s->regmap)) {
501 ret = PTR_ERR(s->regmap);
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300502 goto err;
503 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300504
505 /* check if the tuner is there */
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300506 ret = regmap_read(s->regmap, 0x02, &utmp);
507 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300508 goto err;
509
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300510 dev_dbg(&s->client->dev, "chip id=%02x\n", utmp);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300511
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300512 if (utmp != 0x40) {
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300513 ret = -ENODEV;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300514 goto err;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300515 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300516
517 /* put sleep as chip seems to be in normal mode by default */
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300518 ret = regmap_write(s->regmap, 0x00, 0x00);
519 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300520 goto err;
521
Antti Palosaari320c6382014-03-16 18:13:05 -0300522#if IS_ENABLED(CONFIG_VIDEO_V4L2)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300523 /* Register controls */
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300524 v4l2_ctrl_handler_init(&s->hdl, 9);
525 s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300526 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300527 s->bandwidth = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300528 V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300529 v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
530 s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300531 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300532 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300533 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300534 v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false);
535 s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300536 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300537 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300538 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300539 v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false);
540 s->if_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300541 V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300542 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300543 V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300544 v4l2_ctrl_auto_cluster(2, &s->if_gain_auto, 0, false);
545 s->pll_lock = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300546 V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300547 if (s->hdl.error) {
548 ret = s->hdl.error;
549 dev_err(&s->client->dev, "Could not initialize controls\n");
550 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300551 goto err;
552 }
553
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300554 s->sd.ctrl_handler = &s->hdl;
Antti Palosaari320c6382014-03-16 18:13:05 -0300555#endif
Antti Palosaariadaa6162014-01-26 21:02:53 -0300556
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300557 dev_info(&s->client->dev, "Elonics E4000 successfully identified\n");
Antti Palosaaried85ada2012-09-01 21:09:21 -0300558
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300559 fe->tuner_priv = s;
Antti Palosaari36f647b2012-09-22 12:32:27 -0300560 memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
561 sizeof(struct dvb_tuner_ops));
562
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300563 v4l2_set_subdevdata(&s->sd, client);
564 i2c_set_clientdata(client, &s->sd);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300565
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300566 return 0;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300567err:
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300568 if (ret) {
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300569 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300570 kfree(s);
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300571 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300572
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300573 return ret;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300574}
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300575
576static int e4000_remove(struct i2c_client *client)
577{
Antti Palosaariadaa6162014-01-26 21:02:53 -0300578 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300579 struct e4000 *s = container_of(sd, struct e4000, sd);
580 struct dvb_frontend *fe = s->fe;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300581
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300582 dev_dbg(&client->dev, "\n");
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300583
Antti Palosaari320c6382014-03-16 18:13:05 -0300584#if IS_ENABLED(CONFIG_VIDEO_V4L2)
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300585 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari320c6382014-03-16 18:13:05 -0300586#endif
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300587 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
588 fe->tuner_priv = NULL;
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300589 kfree(s);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300590
591 return 0;
592}
593
594static const struct i2c_device_id e4000_id[] = {
595 {"e4000", 0},
596 {}
597};
598MODULE_DEVICE_TABLE(i2c, e4000_id);
599
600static struct i2c_driver e4000_driver = {
601 .driver = {
602 .owner = THIS_MODULE,
603 .name = "e4000",
604 },
605 .probe = e4000_probe,
606 .remove = e4000_remove,
607 .id_table = e4000_id,
608};
609
610module_i2c_driver(e4000_driver);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300611
612MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
613MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
614MODULE_LICENSE("GPL");