blob: 3f1f9af92bc9eb27ec9649e9a1b3fcd7810192ae [file] [log] [blame]
Thomas Gleixner16216332019-05-19 15:51:31 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Antti Palosaaried85ada2012-09-01 21:09:21 -03002/*
3 * Elonics E4000 silicon tuner driver
4 *
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
Antti Palosaaried85ada2012-09-01 21:09:21 -03006 */
7
8#include "e4000_priv.h"
9
Antti Palosaaric7861bb2015-05-12 14:26:07 -030010static int e4000_init(struct e4000_dev *dev)
Antti Palosaaried85ada2012-09-01 21:09:21 -030011{
Antti Palosaarif8b9b872015-04-21 17:13:39 -030012 struct i2c_client *client = dev->client;
Antti Palosaaried85ada2012-09-01 21:09:21 -030013 int ret;
14
Antti Palosaarif8b9b872015-04-21 17:13:39 -030015 dev_dbg(&client->dev, "\n");
Antti Palosaaried85ada2012-09-01 21:09:21 -030016
17 /* reset */
Antti Palosaarif8b9b872015-04-21 17:13:39 -030018 ret = regmap_write(dev->regmap, 0x00, 0x01);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030019 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030020 goto err;
21
22 /* disable output clock */
Antti Palosaarif8b9b872015-04-21 17:13:39 -030023 ret = regmap_write(dev->regmap, 0x06, 0x00);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030024 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030025 goto err;
26
Antti Palosaarif8b9b872015-04-21 17:13:39 -030027 ret = regmap_write(dev->regmap, 0x7a, 0x96);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030028 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030029 goto err;
30
31 /* configure gains */
Antti Palosaarif8b9b872015-04-21 17:13:39 -030032 ret = regmap_bulk_write(dev->regmap, 0x7e, "\x01\xfe", 2);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030033 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030034 goto err;
35
Antti Palosaarif8b9b872015-04-21 17:13:39 -030036 ret = regmap_write(dev->regmap, 0x82, 0x00);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030037 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030038 goto err;
39
Antti Palosaarif8b9b872015-04-21 17:13:39 -030040 ret = regmap_write(dev->regmap, 0x24, 0x05);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030041 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030042 goto err;
43
Antti Palosaarif8b9b872015-04-21 17:13:39 -030044 ret = regmap_bulk_write(dev->regmap, 0x87, "\x20\x01", 2);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030045 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030046 goto err;
47
Antti Palosaarif8b9b872015-04-21 17:13:39 -030048 ret = regmap_bulk_write(dev->regmap, 0x9f, "\x7f\x07", 2);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030049 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030050 goto err;
51
Antti Palosaaried85ada2012-09-01 21:09:21 -030052 /* DC offset control */
Antti Palosaarif8b9b872015-04-21 17:13:39 -030053 ret = regmap_write(dev->regmap, 0x2d, 0x1f);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030054 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -030055 goto err;
56
Antti Palosaarif8b9b872015-04-21 17:13:39 -030057 ret = regmap_bulk_write(dev->regmap, 0x70, "\x01\x01", 2);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030058 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030059 goto err;
60
61 /* gain control */
Antti Palosaarif8b9b872015-04-21 17:13:39 -030062 ret = regmap_write(dev->regmap, 0x1a, 0x17);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030063 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030064 goto err;
65
Antti Palosaarif8b9b872015-04-21 17:13:39 -030066 ret = regmap_write(dev->regmap, 0x1f, 0x1a);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030067 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030068 goto err;
69
Antti Palosaarif8b9b872015-04-21 17:13:39 -030070 dev->active = true;
71
72 return 0;
Antti Palosaaried85ada2012-09-01 21:09:21 -030073err:
Antti Palosaarif8b9b872015-04-21 17:13:39 -030074 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -030075 return ret;
76}
77
Antti Palosaaric7861bb2015-05-12 14:26:07 -030078static int e4000_sleep(struct e4000_dev *dev)
Antti Palosaaried85ada2012-09-01 21:09:21 -030079{
Antti Palosaarif8b9b872015-04-21 17:13:39 -030080 struct i2c_client *client = dev->client;
Antti Palosaaried85ada2012-09-01 21:09:21 -030081 int ret;
82
Antti Palosaarif8b9b872015-04-21 17:13:39 -030083 dev_dbg(&client->dev, "\n");
Antti Palosaaried85ada2012-09-01 21:09:21 -030084
Antti Palosaarif8b9b872015-04-21 17:13:39 -030085 dev->active = false;
Antti Palosaariecfb7ca2014-02-07 02:55:57 -030086
Antti Palosaarif8b9b872015-04-21 17:13:39 -030087 ret = regmap_write(dev->regmap, 0x00, 0x00);
Antti Palosaaric5f51b12014-02-10 22:52:51 -030088 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -030089 goto err;
Antti Palosaaried85ada2012-09-01 21:09:21 -030090
Antti Palosaarif8b9b872015-04-21 17:13:39 -030091 return 0;
92err:
93 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -030094 return ret;
95}
96
Antti Palosaaric7861bb2015-05-12 14:26:07 -030097static int e4000_set_params(struct e4000_dev *dev)
Antti Palosaaried85ada2012-09-01 21:09:21 -030098{
Antti Palosaarif8b9b872015-04-21 17:13:39 -030099 struct i2c_client *client = dev->client;
Antti Palosaari0e3a71c2015-04-21 21:18:45 -0300100 int ret, i;
101 unsigned int div_n, k, k_cw, div_out;
Antti Palosaari0ed0b222014-01-27 03:13:19 -0300102 u64 f_vco;
Antti Palosaari85146112013-07-24 02:04:12 -0300103 u8 buf[5], i_data[4], q_data[4];
Antti Palosaaried85ada2012-09-01 21:09:21 -0300104
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300105 if (!dev->active) {
106 dev_dbg(&client->dev, "tuner is sleeping\n");
107 return 0;
108 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300109
Antti Palosaaried85ada2012-09-01 21:09:21 -0300110 /* gain control manual */
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300111 ret = regmap_write(dev->regmap, 0x1a, 0x00);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300112 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300113 goto err;
114
Antti Palosaari0e3a71c2015-04-21 21:18:45 -0300115 /*
116 * Fractional-N synthesizer
117 *
118 * +----------------------------+
119 * v |
120 * Fref +----+ +-------+ +------+ +---+
121 * ------> | PD | --> | VCO | ------> | /N.F | <-- | K |
122 * +----+ +-------+ +------+ +---+
123 * |
124 * |
125 * v
126 * +-------+ Fout
127 * | /Rout | ------>
128 * +-------+
129 */
Antti Palosaaried85ada2012-09-01 21:09:21 -0300130 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300131 if (dev->f_frequency <= e4000_pll_lut[i].freq)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300132 break;
133 }
Julia Lawall58f087c2013-12-29 19:47:35 -0300134 if (i == ARRAY_SIZE(e4000_pll_lut)) {
135 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300136 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300137 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300138
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300139 #define F_REF dev->clk
Antti Palosaari0e3a71c2015-04-21 21:18:45 -0300140 div_out = e4000_pll_lut[i].div_out;
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300141 f_vco = (u64) dev->f_frequency * div_out;
Antti Palosaari0e3a71c2015-04-21 21:18:45 -0300142 /* calculate PLL integer and fractional control word */
143 div_n = div_u64_rem(f_vco, F_REF, &k);
144 k_cw = div_u64((u64) k * 0x10000, F_REF);
145
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300146 dev_dbg(&client->dev,
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300147 "frequency=%u bandwidth=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n",
148 dev->f_frequency, dev->f_bandwidth, f_vco, F_REF, div_n, k,
149 k_cw, div_out);
Antti Palosaari0e3a71c2015-04-21 21:18:45 -0300150
151 buf[0] = div_n;
152 buf[1] = (k_cw >> 0) & 0xff;
153 buf[2] = (k_cw >> 8) & 0xff;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300154 buf[3] = 0x00;
Antti Palosaari0e3a71c2015-04-21 21:18:45 -0300155 buf[4] = e4000_pll_lut[i].div_out_reg;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300156 ret = regmap_bulk_write(dev->regmap, 0x09, buf, 5);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300157 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300158 goto err;
159
160 /* LNA filter (RF filter) */
161 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300162 if (dev->f_frequency <= e400_lna_filter_lut[i].freq)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300163 break;
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 Palosaarif8b9b872015-04-21 17:13:39 -0300170 ret = regmap_write(dev->regmap, 0x10, e400_lna_filter_lut[i].val);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300171 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++) {
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300176 if (dev->f_bandwidth <= e4000_if_filter_lut[i].freq)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300177 break;
178 }
Julia Lawall58f087c2013-12-29 19:47:35 -0300179 if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
180 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300181 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300182 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300183
184 buf[0] = e4000_if_filter_lut[i].reg11_val;
185 buf[1] = e4000_if_filter_lut[i].reg12_val;
186
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300187 ret = regmap_bulk_write(dev->regmap, 0x11, buf, 2);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300188 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300189 goto err;
190
191 /* frequency band */
192 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300193 if (dev->f_frequency <= e4000_band_lut[i].freq)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300194 break;
195 }
Julia Lawall58f087c2013-12-29 19:47:35 -0300196 if (i == ARRAY_SIZE(e4000_band_lut)) {
197 ret = -EINVAL;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300198 goto err;
Julia Lawall58f087c2013-12-29 19:47:35 -0300199 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300200
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300201 ret = regmap_write(dev->regmap, 0x07, e4000_band_lut[i].reg07_val);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300202 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300203 goto err;
204
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300205 ret = regmap_write(dev->regmap, 0x78, e4000_band_lut[i].reg78_val);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300206 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300207 goto err;
208
Antti Palosaari85146112013-07-24 02:04:12 -0300209 /* DC offset */
210 for (i = 0; i < 4; i++) {
211 if (i == 0)
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300212 ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7e\x24", 3);
Antti Palosaari85146112013-07-24 02:04:12 -0300213 else if (i == 1)
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300214 ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7f", 2);
Antti Palosaari85146112013-07-24 02:04:12 -0300215 else if (i == 2)
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300216 ret = regmap_bulk_write(dev->regmap, 0x15, "\x01", 1);
Antti Palosaari85146112013-07-24 02:04:12 -0300217 else
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300218 ret = regmap_bulk_write(dev->regmap, 0x16, "\x7e", 1);
Antti Palosaari85146112013-07-24 02:04:12 -0300219
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300220 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300221 goto err;
222
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300223 ret = regmap_write(dev->regmap, 0x29, 0x01);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300224 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300225 goto err;
226
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300227 ret = regmap_bulk_read(dev->regmap, 0x2a, buf, 3);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300228 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300229 goto err;
230
231 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
232 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
233 }
234
Antti Palosaarid4992da2013-07-24 18:33:51 -0300235 swap(q_data[2], q_data[3]);
236 swap(i_data[2], i_data[3]);
237
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300238 ret = regmap_bulk_write(dev->regmap, 0x50, q_data, 4);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300239 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300240 goto err;
241
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300242 ret = regmap_bulk_write(dev->regmap, 0x60, i_data, 4);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300243 if (ret)
Antti Palosaari85146112013-07-24 02:04:12 -0300244 goto err;
245
Antti Palosaaried85ada2012-09-01 21:09:21 -0300246 /* gain control auto */
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300247 ret = regmap_write(dev->regmap, 0x1a, 0x17);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300248 if (ret)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300249 goto err;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300250
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300251 return 0;
252err:
253 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300254 return ret;
255}
256
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300257/*
258 * V4L2 API
259 */
260#if IS_ENABLED(CONFIG_VIDEO_V4L2)
261static const struct v4l2_frequency_band bands[] = {
262 {
263 .type = V4L2_TUNER_RF,
264 .index = 0,
265 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
266 .rangelow = 59000000,
267 .rangehigh = 1105000000,
268 },
269 {
270 .type = V4L2_TUNER_RF,
271 .index = 1,
272 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
273 .rangelow = 1249000000,
Hans Verkuil1ba904922015-05-22 04:39:54 -0300274 .rangehigh = 2208000000UL,
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300275 },
276};
277
278static inline struct e4000_dev *e4000_subdev_to_dev(struct v4l2_subdev *sd)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300279{
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300280 return container_of(sd, struct e4000_dev, sd);
281}
282
Hans Verkuil3aab15a2018-02-21 02:49:25 -0500283static int e4000_standby(struct v4l2_subdev *sd)
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300284{
285 struct e4000_dev *dev = e4000_subdev_to_dev(sd);
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300286 int ret;
287
Hans Verkuil3aab15a2018-02-21 02:49:25 -0500288 ret = e4000_sleep(dev);
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300289 if (ret)
290 return ret;
291
292 return e4000_set_params(dev);
293}
294
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300295static int e4000_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
296{
297 struct e4000_dev *dev = e4000_subdev_to_dev(sd);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300298 struct i2c_client *client = dev->client;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300299
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300300 dev_dbg(&client->dev, "index=%d\n", v->index);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300301
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400302 strscpy(v->name, "Elonics E4000", sizeof(v->name));
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300303 v->type = V4L2_TUNER_RF;
304 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
305 v->rangelow = bands[0].rangelow;
306 v->rangehigh = bands[1].rangehigh;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300307 return 0;
308}
309
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300310static int e4000_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
311{
312 struct e4000_dev *dev = e4000_subdev_to_dev(sd);
313 struct i2c_client *client = dev->client;
314
315 dev_dbg(&client->dev, "index=%d\n", v->index);
316 return 0;
317}
318
319static int e4000_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
320{
321 struct e4000_dev *dev = e4000_subdev_to_dev(sd);
322 struct i2c_client *client = dev->client;
323
324 dev_dbg(&client->dev, "tuner=%d\n", f->tuner);
325 f->frequency = dev->f_frequency;
326 return 0;
327}
328
329static int e4000_s_frequency(struct v4l2_subdev *sd,
330 const struct v4l2_frequency *f)
331{
332 struct e4000_dev *dev = e4000_subdev_to_dev(sd);
333 struct i2c_client *client = dev->client;
334
335 dev_dbg(&client->dev, "tuner=%d type=%d frequency=%u\n",
336 f->tuner, f->type, f->frequency);
337
338 dev->f_frequency = clamp_t(unsigned int, f->frequency,
339 bands[0].rangelow, bands[1].rangehigh);
340 return e4000_set_params(dev);
341}
342
343static int e4000_enum_freq_bands(struct v4l2_subdev *sd,
344 struct v4l2_frequency_band *band)
345{
346 struct e4000_dev *dev = e4000_subdev_to_dev(sd);
347 struct i2c_client *client = dev->client;
348
349 dev_dbg(&client->dev, "tuner=%d type=%d index=%d\n",
350 band->tuner, band->type, band->index);
351
352 if (band->index >= ARRAY_SIZE(bands))
353 return -EINVAL;
354
355 band->capability = bands[band->index].capability;
356 band->rangelow = bands[band->index].rangelow;
357 band->rangehigh = bands[band->index].rangehigh;
358 return 0;
359}
360
361static const struct v4l2_subdev_tuner_ops e4000_subdev_tuner_ops = {
Hans Verkuil3aab15a2018-02-21 02:49:25 -0500362 .standby = e4000_standby,
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300363 .g_tuner = e4000_g_tuner,
364 .s_tuner = e4000_s_tuner,
365 .g_frequency = e4000_g_frequency,
366 .s_frequency = e4000_s_frequency,
367 .enum_freq_bands = e4000_enum_freq_bands,
368};
369
370static const struct v4l2_subdev_ops e4000_subdev_ops = {
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300371 .tuner = &e4000_subdev_tuner_ops,
372};
373
Antti Palosaariadaa6162014-01-26 21:02:53 -0300374static int e4000_set_lna_gain(struct dvb_frontend *fe)
375{
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300376 struct e4000_dev *dev = fe->tuner_priv;
377 struct i2c_client *client = dev->client;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300378 int ret;
379 u8 u8tmp;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300380
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300381 dev_dbg(&client->dev, "lna auto=%d->%d val=%d->%d\n",
382 dev->lna_gain_auto->cur.val, dev->lna_gain_auto->val,
383 dev->lna_gain->cur.val, dev->lna_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300384
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300385 if (dev->lna_gain_auto->val && dev->if_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300386 u8tmp = 0x17;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300387 else if (dev->lna_gain_auto->val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300388 u8tmp = 0x19;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300389 else if (dev->if_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300390 u8tmp = 0x16;
391 else
392 u8tmp = 0x10;
393
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300394 ret = regmap_write(dev->regmap, 0x1a, u8tmp);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300395 if (ret)
396 goto err;
397
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300398 if (dev->lna_gain_auto->val == false) {
399 ret = regmap_write(dev->regmap, 0x14, dev->lna_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300400 if (ret)
401 goto err;
402 }
Antti Palosaariadaa6162014-01-26 21:02:53 -0300403
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300404 return 0;
405err:
406 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300407 return ret;
408}
409
410static int e4000_set_mixer_gain(struct dvb_frontend *fe)
411{
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300412 struct e4000_dev *dev = fe->tuner_priv;
413 struct i2c_client *client = dev->client;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300414 int ret;
415 u8 u8tmp;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300416
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300417 dev_dbg(&client->dev, "mixer auto=%d->%d val=%d->%d\n",
418 dev->mixer_gain_auto->cur.val, dev->mixer_gain_auto->val,
419 dev->mixer_gain->cur.val, dev->mixer_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300420
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300421 if (dev->mixer_gain_auto->val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300422 u8tmp = 0x15;
423 else
424 u8tmp = 0x14;
425
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300426 ret = regmap_write(dev->regmap, 0x20, u8tmp);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300427 if (ret)
428 goto err;
429
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300430 if (dev->mixer_gain_auto->val == false) {
431 ret = regmap_write(dev->regmap, 0x15, dev->mixer_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300432 if (ret)
433 goto err;
434 }
Antti Palosaariadaa6162014-01-26 21:02:53 -0300435
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300436 return 0;
437err:
438 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300439 return ret;
440}
441
442static int e4000_set_if_gain(struct dvb_frontend *fe)
443{
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300444 struct e4000_dev *dev = fe->tuner_priv;
445 struct i2c_client *client = dev->client;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300446 int ret;
447 u8 buf[2];
448 u8 u8tmp;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300449
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300450 dev_dbg(&client->dev, "if auto=%d->%d val=%d->%d\n",
451 dev->if_gain_auto->cur.val, dev->if_gain_auto->val,
452 dev->if_gain->cur.val, dev->if_gain->val);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300453
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300454 if (dev->if_gain_auto->val && dev->lna_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300455 u8tmp = 0x17;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300456 else if (dev->lna_gain_auto->cur.val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300457 u8tmp = 0x19;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300458 else if (dev->if_gain_auto->val)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300459 u8tmp = 0x16;
460 else
461 u8tmp = 0x10;
462
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300463 ret = regmap_write(dev->regmap, 0x1a, u8tmp);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300464 if (ret)
465 goto err;
466
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300467 if (dev->if_gain_auto->val == false) {
468 buf[0] = e4000_if_gain_lut[dev->if_gain->val].reg16_val;
469 buf[1] = e4000_if_gain_lut[dev->if_gain->val].reg17_val;
470 ret = regmap_bulk_write(dev->regmap, 0x16, buf, 2);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300471 if (ret)
472 goto err;
473 }
Antti Palosaariadaa6162014-01-26 21:02:53 -0300474
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300475 return 0;
476err:
477 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300478 return ret;
479}
480
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300481static int e4000_pll_lock(struct dvb_frontend *fe)
482{
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300483 struct e4000_dev *dev = fe->tuner_priv;
484 struct i2c_client *client = dev->client;
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300485 int ret;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300486 unsigned int uitmp;
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300487
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300488 ret = regmap_read(dev->regmap, 0x07, &uitmp);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300489 if (ret)
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300490 goto err;
491
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300492 dev->pll_lock->val = (uitmp & 0x01);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300493
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300494 return 0;
495err:
496 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300497 return ret;
498}
499
500static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
501{
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300502 struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
503 struct i2c_client *client = dev->client;
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300504 int ret;
505
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300506 if (!dev->active)
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300507 return 0;
508
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300509 switch (ctrl->id) {
510 case V4L2_CID_RF_TUNER_PLL_LOCK:
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300511 ret = e4000_pll_lock(dev->fe);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300512 break;
513 default:
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300514 dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
515 ctrl->id, ctrl->name);
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300516 ret = -EINVAL;
517 }
518
519 return ret;
520}
521
Antti Palosaariadaa6162014-01-26 21:02:53 -0300522static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
523{
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300524 struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
525 struct i2c_client *client = dev->client;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300526 int ret;
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300527
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300528 if (!dev->active)
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300529 return 0;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300530
531 switch (ctrl->id) {
532 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
533 case V4L2_CID_RF_TUNER_BANDWIDTH:
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300534 /*
535 * TODO: Auto logic does not work 100% correctly as tuner driver
536 * do not have information to calculate maximum suitable
537 * bandwidth. Calculating it is responsible of master driver.
538 */
539 dev->f_bandwidth = dev->bandwidth->val;
540 ret = e4000_set_params(dev);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300541 break;
542 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
543 case V4L2_CID_RF_TUNER_LNA_GAIN:
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300544 ret = e4000_set_lna_gain(dev->fe);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300545 break;
546 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
547 case V4L2_CID_RF_TUNER_MIXER_GAIN:
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300548 ret = e4000_set_mixer_gain(dev->fe);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300549 break;
550 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
551 case V4L2_CID_RF_TUNER_IF_GAIN:
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300552 ret = e4000_set_if_gain(dev->fe);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300553 break;
554 default:
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300555 dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
556 ctrl->id, ctrl->name);
Antti Palosaariadaa6162014-01-26 21:02:53 -0300557 ret = -EINVAL;
558 }
559
560 return ret;
561}
562
563static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300564 .g_volatile_ctrl = e4000_g_volatile_ctrl,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300565 .s_ctrl = e4000_s_ctrl,
566};
Antti Palosaari320c6382014-03-16 18:13:05 -0300567#endif
Antti Palosaariadaa6162014-01-26 21:02:53 -0300568
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300569/*
570 * DVB API
571 */
572static int e4000_dvb_set_params(struct dvb_frontend *fe)
573{
574 struct e4000_dev *dev = fe->tuner_priv;
575 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
576
577 dev->f_frequency = c->frequency;
578 dev->f_bandwidth = c->bandwidth_hz;
579 return e4000_set_params(dev);
580}
581
582static int e4000_dvb_init(struct dvb_frontend *fe)
583{
584 return e4000_init(fe->tuner_priv);
585}
586
587static int e4000_dvb_sleep(struct dvb_frontend *fe)
588{
589 return e4000_sleep(fe->tuner_priv);
590}
591
592static int e4000_dvb_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
593{
594 *frequency = 0; /* Zero-IF */
595 return 0;
596}
597
598static const struct dvb_tuner_ops e4000_dvb_tuner_ops = {
Antti Palosaaried85ada2012-09-01 21:09:21 -0300599 .info = {
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -0400600 .name = "Elonics E4000",
601 .frequency_min_hz = 174 * MHz,
602 .frequency_max_hz = 862 * MHz,
Antti Palosaaried85ada2012-09-01 21:09:21 -0300603 },
604
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300605 .init = e4000_dvb_init,
606 .sleep = e4000_dvb_sleep,
607 .set_params = e4000_dvb_set_params,
Antti Palosaaried85ada2012-09-01 21:09:21 -0300608
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300609 .get_if_frequency = e4000_dvb_get_if_frequency,
Antti Palosaaried85ada2012-09-01 21:09:21 -0300610};
611
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300612static int e4000_probe(struct i2c_client *client,
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300613 const struct i2c_device_id *id)
Antti Palosaaried85ada2012-09-01 21:09:21 -0300614{
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300615 struct e4000_dev *dev;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300616 struct e4000_config *cfg = client->dev.platform_data;
617 struct dvb_frontend *fe = cfg->fe;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300618 int ret;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300619 unsigned int uitmp;
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300620 static const struct regmap_config regmap_config = {
621 .reg_bits = 8,
622 .val_bits = 8,
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300623 };
Antti Palosaaried85ada2012-09-01 21:09:21 -0300624
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300625 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
626 if (!dev) {
Antti Palosaaried85ada2012-09-01 21:09:21 -0300627 ret = -ENOMEM;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300628 goto err;
629 }
630
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300631 dev->clk = cfg->clock;
632 dev->client = client;
633 dev->fe = cfg->fe;
634 dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
635 if (IS_ERR(dev->regmap)) {
636 ret = PTR_ERR(dev->regmap);
637 goto err_kfree;
Antti Palosaaribd428bb2014-02-08 06:20:35 -0300638 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300639
640 /* check if the tuner is there */
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300641 ret = regmap_read(dev->regmap, 0x02, &uitmp);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300642 if (ret)
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300643 goto err_kfree;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300644
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300645 dev_dbg(&client->dev, "chip id=%02x\n", uitmp);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300646
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300647 if (uitmp != 0x40) {
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300648 ret = -ENODEV;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300649 goto err_kfree;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300650 }
Antti Palosaaried85ada2012-09-01 21:09:21 -0300651
652 /* put sleep as chip seems to be in normal mode by default */
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300653 ret = regmap_write(dev->regmap, 0x00, 0x00);
Antti Palosaaric5f51b12014-02-10 22:52:51 -0300654 if (ret)
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300655 goto err_kfree;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300656
Antti Palosaari320c6382014-03-16 18:13:05 -0300657#if IS_ENABLED(CONFIG_VIDEO_V4L2)
Antti Palosaariadaa6162014-01-26 21:02:53 -0300658 /* Register controls */
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300659 v4l2_ctrl_handler_init(&dev->hdl, 9);
660 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300661 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300662 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300663 V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300664 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
665 dev->lna_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300666 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300667 dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300668 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300669 v4l2_ctrl_auto_cluster(2, &dev->lna_gain_auto, 0, false);
670 dev->mixer_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300671 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300672 dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300673 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300674 v4l2_ctrl_auto_cluster(2, &dev->mixer_gain_auto, 0, false);
675 dev->if_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300676 V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300677 dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariadaa6162014-01-26 21:02:53 -0300678 V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300679 v4l2_ctrl_auto_cluster(2, &dev->if_gain_auto, 0, false);
680 dev->pll_lock = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
Antti Palosaariecfb7ca2014-02-07 02:55:57 -0300681 V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300682 if (dev->hdl.error) {
683 ret = dev->hdl.error;
684 dev_err(&client->dev, "Could not initialize controls\n");
685 v4l2_ctrl_handler_free(&dev->hdl);
686 goto err_kfree;
Antti Palosaariadaa6162014-01-26 21:02:53 -0300687 }
688
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300689 dev->sd.ctrl_handler = &dev->hdl;
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300690 dev->f_frequency = bands[0].rangelow;
691 dev->f_bandwidth = dev->bandwidth->val;
692 v4l2_i2c_subdev_init(&dev->sd, client, &e4000_subdev_ops);
Antti Palosaari320c6382014-03-16 18:13:05 -0300693#endif
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300694 fe->tuner_priv = dev;
Antti Palosaaric7861bb2015-05-12 14:26:07 -0300695 memcpy(&fe->ops.tuner_ops, &e4000_dvb_tuner_ops,
696 sizeof(fe->ops.tuner_ops));
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300697 v4l2_set_subdevdata(&dev->sd, client);
698 i2c_set_clientdata(client, &dev->sd);
Antti Palosaari36f647b2012-09-22 12:32:27 -0300699
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300700 dev_info(&client->dev, "Elonics E4000 successfully identified\n");
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300701 return 0;
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300702err_kfree:
703 kfree(dev);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300704err:
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300705 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300706 return ret;
Antti Palosaaried85ada2012-09-01 21:09:21 -0300707}
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300708
709static int e4000_remove(struct i2c_client *client)
710{
Antti Palosaariadaa6162014-01-26 21:02:53 -0300711 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300712 struct e4000_dev *dev = container_of(sd, struct e4000_dev, sd);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300713
Antti Palosaari13bd82d2014-08-24 23:35:48 -0300714 dev_dbg(&client->dev, "\n");
Antti Palosaari1c73fc62014-02-08 04:21:10 -0300715
Antti Palosaari320c6382014-03-16 18:13:05 -0300716#if IS_ENABLED(CONFIG_VIDEO_V4L2)
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300717 v4l2_ctrl_handler_free(&dev->hdl);
Antti Palosaari320c6382014-03-16 18:13:05 -0300718#endif
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300719 kfree(dev);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300720
721 return 0;
722}
723
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300724static const struct i2c_device_id e4000_id_table[] = {
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300725 {"e4000", 0},
726 {}
727};
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300728MODULE_DEVICE_TABLE(i2c, e4000_id_table);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300729
730static struct i2c_driver e4000_driver = {
731 .driver = {
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300732 .name = "e4000",
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300733 .suppress_bind_attrs = true,
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300734 },
735 .probe = e4000_probe,
736 .remove = e4000_remove,
Antti Palosaarif8b9b872015-04-21 17:13:39 -0300737 .id_table = e4000_id_table,
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300738};
739
740module_i2c_driver(e4000_driver);
Antti Palosaaried85ada2012-09-01 21:09:21 -0300741
742MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
743MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
744MODULE_LICENSE("GPL");