blob: 78e6fd600d8ef77ec60fd1e55aa96feaaf90827f [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Antti Palosaari93203dd2014-02-02 23:34:23 -03002/*
3 * Mirics MSi001 silicon tuner driver
4 *
5 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
6 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
Antti Palosaari93203dd2014-02-02 23:34:23 -03007 */
8
9#include <linux/module.h>
10#include <linux/gcd.h>
11#include <media/v4l2-device.h>
12#include <media/v4l2-ctrls.h>
13
14static const struct v4l2_frequency_band bands[] = {
15 {
16 .type = V4L2_TUNER_RF,
17 .index = 0,
18 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
19 .rangelow = 49000000,
20 .rangehigh = 263000000,
21 }, {
22 .type = V4L2_TUNER_RF,
23 .index = 1,
24 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
25 .rangelow = 390000000,
26 .rangehigh = 960000000,
27 },
28};
29
Antti Palosaari83e64b42015-04-29 10:57:12 -030030struct msi001_dev {
Antti Palosaari93203dd2014-02-02 23:34:23 -030031 struct spi_device *spi;
32 struct v4l2_subdev sd;
33
34 /* Controls */
35 struct v4l2_ctrl_handler hdl;
36 struct v4l2_ctrl *bandwidth_auto;
37 struct v4l2_ctrl *bandwidth;
38 struct v4l2_ctrl *lna_gain;
39 struct v4l2_ctrl *mixer_gain;
40 struct v4l2_ctrl *if_gain;
41
42 unsigned int f_tuner;
43};
44
Antti Palosaari83e64b42015-04-29 10:57:12 -030045static inline struct msi001_dev *sd_to_msi001_dev(struct v4l2_subdev *sd)
Antti Palosaari93203dd2014-02-02 23:34:23 -030046{
Antti Palosaari83e64b42015-04-29 10:57:12 -030047 return container_of(sd, struct msi001_dev, sd);
Antti Palosaari93203dd2014-02-02 23:34:23 -030048}
49
Antti Palosaari83e64b42015-04-29 10:57:12 -030050static int msi001_wreg(struct msi001_dev *dev, u32 data)
Antti Palosaari93203dd2014-02-02 23:34:23 -030051{
52 /* Register format: 4 bits addr + 20 bits value */
Antti Palosaari83e64b42015-04-29 10:57:12 -030053 return spi_write(dev->spi, &data, 3);
Antti Palosaari93203dd2014-02-02 23:34:23 -030054};
55
Antti Palosaari83e64b42015-04-29 10:57:12 -030056static int msi001_set_gain(struct msi001_dev *dev, int lna_gain, int mixer_gain,
57 int if_gain)
Antti Palosaari93203dd2014-02-02 23:34:23 -030058{
Antti Palosaari83e64b42015-04-29 10:57:12 -030059 struct spi_device *spi = dev->spi;
Antti Palosaari93203dd2014-02-02 23:34:23 -030060 int ret;
61 u32 reg;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -030062
Antti Palosaari83e64b42015-04-29 10:57:12 -030063 dev_dbg(&spi->dev, "lna=%d mixer=%d if=%d\n",
64 lna_gain, mixer_gain, if_gain);
Antti Palosaari93203dd2014-02-02 23:34:23 -030065
66 reg = 1 << 0;
67 reg |= (59 - if_gain) << 4;
68 reg |= 0 << 10;
69 reg |= (1 - mixer_gain) << 12;
70 reg |= (1 - lna_gain) << 13;
71 reg |= 4 << 14;
72 reg |= 0 << 17;
Antti Palosaari83e64b42015-04-29 10:57:12 -030073 ret = msi001_wreg(dev, reg);
Antti Palosaari93203dd2014-02-02 23:34:23 -030074 if (ret)
75 goto err;
76
77 return 0;
78err:
Antti Palosaari83e64b42015-04-29 10:57:12 -030079 dev_dbg(&spi->dev, "failed %d\n", ret);
Antti Palosaari93203dd2014-02-02 23:34:23 -030080 return ret;
81};
82
Antti Palosaari83e64b42015-04-29 10:57:12 -030083static int msi001_set_tuner(struct msi001_dev *dev)
Antti Palosaari93203dd2014-02-02 23:34:23 -030084{
Antti Palosaari83e64b42015-04-29 10:57:12 -030085 struct spi_device *spi = dev->spi;
Antti Palosaari93203dd2014-02-02 23:34:23 -030086 int ret, i;
Antti Palosaari9195e5b2015-04-29 10:10:01 -030087 unsigned int uitmp, div_n, k, k_thresh, k_frac, div_lo, f_if1;
Antti Palosaari93203dd2014-02-02 23:34:23 -030088 u32 reg;
Antti Palosaari9195e5b2015-04-29 10:10:01 -030089 u64 f_vco;
90 u8 mode, filter_mode;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -030091
Antti Palosaari93203dd2014-02-02 23:34:23 -030092 static const struct {
93 u32 rf;
94 u8 mode;
Antti Palosaari9195e5b2015-04-29 10:10:01 -030095 u8 div_lo;
Antti Palosaari93203dd2014-02-02 23:34:23 -030096 } band_lut[] = {
97 { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
98 {108000000, 0x42, 32}, /* VHF_MODE */
99 {330000000, 0x44, 16}, /* B3_MODE */
100 {960000000, 0x48, 4}, /* B45_MODE */
101 { ~0U, 0x50, 2}, /* BL_MODE */
102 };
103 static const struct {
104 u32 freq;
105 u8 filter_mode;
106 } if_freq_lut[] = {
107 { 0, 0x03}, /* Zero IF */
108 { 450000, 0x02}, /* 450 kHz IF */
109 {1620000, 0x01}, /* 1.62 MHz IF */
110 {2048000, 0x00}, /* 2.048 MHz IF */
111 };
112 static const struct {
113 u32 freq;
114 u8 val;
115 } bandwidth_lut[] = {
116 { 200000, 0x00}, /* 200 kHz */
117 { 300000, 0x01}, /* 300 kHz */
118 { 600000, 0x02}, /* 600 kHz */
119 {1536000, 0x03}, /* 1.536 MHz */
120 {5000000, 0x04}, /* 5 MHz */
121 {6000000, 0x05}, /* 6 MHz */
122 {7000000, 0x06}, /* 7 MHz */
123 {8000000, 0x07}, /* 8 MHz */
124 };
125
Antti Palosaari83e64b42015-04-29 10:57:12 -0300126 unsigned int f_rf = dev->f_tuner;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300127
128 /*
129 * bandwidth (Hz)
130 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
131 */
132 unsigned int bandwidth;
133
134 /*
135 * intermediate frequency (Hz)
136 * 0, 450000, 1620000, 2048000
137 */
138 unsigned int f_if = 0;
139 #define F_REF 24000000
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300140 #define DIV_PRE_N 4
141 #define F_VCO_STEP div_lo
Antti Palosaari93203dd2014-02-02 23:34:23 -0300142
Antti Palosaari83e64b42015-04-29 10:57:12 -0300143 dev_dbg(&spi->dev, "f_rf=%d f_if=%d\n", f_rf, f_if);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300144
145 for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
146 if (f_rf <= band_lut[i].rf) {
147 mode = band_lut[i].mode;
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300148 div_lo = band_lut[i].div_lo;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300149 break;
150 }
151 }
Antti Palosaari93203dd2014-02-02 23:34:23 -0300152 if (i == ARRAY_SIZE(band_lut)) {
153 ret = -EINVAL;
154 goto err;
155 }
156
157 /* AM_MODE is upconverted */
158 if ((mode >> 0) & 0x1)
159 f_if1 = 5 * F_REF;
160 else
161 f_if1 = 0;
162
163 for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
164 if (f_if == if_freq_lut[i].freq) {
165 filter_mode = if_freq_lut[i].filter_mode;
166 break;
167 }
168 }
Antti Palosaari93203dd2014-02-02 23:34:23 -0300169 if (i == ARRAY_SIZE(if_freq_lut)) {
170 ret = -EINVAL;
171 goto err;
172 }
173
174 /* filters */
Antti Palosaari83e64b42015-04-29 10:57:12 -0300175 bandwidth = dev->bandwidth->val;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300176 bandwidth = clamp(bandwidth, 200000U, 8000000U);
177
178 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
179 if (bandwidth <= bandwidth_lut[i].freq) {
180 bandwidth = bandwidth_lut[i].val;
181 break;
182 }
183 }
Antti Palosaari93203dd2014-02-02 23:34:23 -0300184 if (i == ARRAY_SIZE(bandwidth_lut)) {
185 ret = -EINVAL;
186 goto err;
187 }
188
Antti Palosaari83e64b42015-04-29 10:57:12 -0300189 dev->bandwidth->val = bandwidth_lut[i].freq;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300190
Antti Palosaari83e64b42015-04-29 10:57:12 -0300191 dev_dbg(&spi->dev, "bandwidth selected=%d\n", bandwidth_lut[i].freq);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300192
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300193 /*
194 * Fractional-N synthesizer
195 *
196 * +---------------------------------------+
197 * v |
198 * Fref +----+ +-------+ +----+ +------+ +---+
199 * ------> | PD | --> | VCO | ------> | /4 | --> | /N.F | <-- | K |
200 * +----+ +-------+ +----+ +------+ +---+
201 * |
202 * |
203 * v
204 * +-------+ Fout
205 * | /Rout | ------>
206 * +-------+
207 */
Antti Palosaari93203dd2014-02-02 23:34:23 -0300208
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300209 /* Calculate PLL integer and fractional control word. */
210 f_vco = (u64) (f_rf + f_if + f_if1) * div_lo;
211 div_n = div_u64_rem(f_vco, DIV_PRE_N * F_REF, &k);
212 k_thresh = (DIV_PRE_N * F_REF) / F_VCO_STEP;
213 k_frac = div_u64((u64) k * k_thresh, (DIV_PRE_N * F_REF));
Antti Palosaari93203dd2014-02-02 23:34:23 -0300214
215 /* Find out greatest common divisor and divide to smaller. */
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300216 uitmp = gcd(k_thresh, k_frac);
217 k_thresh /= uitmp;
218 k_frac /= uitmp;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300219
220 /* Force divide to reg max. Resolution will be reduced. */
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300221 uitmp = DIV_ROUND_UP(k_thresh, 4095);
222 k_thresh = DIV_ROUND_CLOSEST(k_thresh, uitmp);
223 k_frac = DIV_ROUND_CLOSEST(k_frac, uitmp);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300224
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300225 /* Calculate real RF set. */
226 uitmp = (unsigned int) F_REF * DIV_PRE_N * div_n;
227 uitmp += (unsigned int) F_REF * DIV_PRE_N * k_frac / k_thresh;
228 uitmp /= div_lo;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300229
Antti Palosaari83e64b42015-04-29 10:57:12 -0300230 dev_dbg(&spi->dev,
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300231 "f_rf=%u:%u f_vco=%llu div_n=%u k_thresh=%u k_frac=%u div_lo=%u\n",
232 f_rf, uitmp, f_vco, div_n, k_thresh, k_frac, div_lo);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300233
Antti Palosaari83e64b42015-04-29 10:57:12 -0300234 ret = msi001_wreg(dev, 0x00000e);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300235 if (ret)
236 goto err;
237
Antti Palosaari83e64b42015-04-29 10:57:12 -0300238 ret = msi001_wreg(dev, 0x000003);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300239 if (ret)
240 goto err;
241
242 reg = 0 << 0;
243 reg |= mode << 4;
244 reg |= filter_mode << 12;
245 reg |= bandwidth << 14;
246 reg |= 0x02 << 17;
247 reg |= 0x00 << 20;
Antti Palosaari83e64b42015-04-29 10:57:12 -0300248 ret = msi001_wreg(dev, reg);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300249 if (ret)
250 goto err;
251
252 reg = 5 << 0;
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300253 reg |= k_thresh << 4;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300254 reg |= 1 << 19;
255 reg |= 1 << 21;
Antti Palosaari83e64b42015-04-29 10:57:12 -0300256 ret = msi001_wreg(dev, reg);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300257 if (ret)
258 goto err;
259
260 reg = 2 << 0;
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300261 reg |= k_frac << 4;
262 reg |= div_n << 16;
Antti Palosaari83e64b42015-04-29 10:57:12 -0300263 ret = msi001_wreg(dev, reg);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300264 if (ret)
265 goto err;
266
Antti Palosaari83e64b42015-04-29 10:57:12 -0300267 ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
268 dev->mixer_gain->cur.val, dev->if_gain->cur.val);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300269 if (ret)
270 goto err;
271
272 reg = 6 << 0;
273 reg |= 63 << 4;
274 reg |= 4095 << 10;
Antti Palosaari83e64b42015-04-29 10:57:12 -0300275 ret = msi001_wreg(dev, reg);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300276 if (ret)
277 goto err;
278
279 return 0;
280err:
Antti Palosaari83e64b42015-04-29 10:57:12 -0300281 dev_dbg(&spi->dev, "failed %d\n", ret);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300282 return ret;
Antti Palosaari9195e5b2015-04-29 10:10:01 -0300283}
Antti Palosaari93203dd2014-02-02 23:34:23 -0300284
Hans Verkuil3aab15a2018-02-21 02:49:25 -0500285static int msi001_standby(struct v4l2_subdev *sd)
Antti Palosaari93203dd2014-02-02 23:34:23 -0300286{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300287 struct msi001_dev *dev = sd_to_msi001_dev(sd);
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300288
Hans Verkuil3aab15a2018-02-21 02:49:25 -0500289 return msi001_wreg(dev, 0x000000);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300290}
291
Antti Palosaari93203dd2014-02-02 23:34:23 -0300292static int msi001_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
293{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300294 struct msi001_dev *dev = sd_to_msi001_dev(sd);
295 struct spi_device *spi = dev->spi;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300296
Antti Palosaari83e64b42015-04-29 10:57:12 -0300297 dev_dbg(&spi->dev, "index=%d\n", v->index);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300298
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400299 strscpy(v->name, "Mirics MSi001", sizeof(v->name));
Antti Palosaari93203dd2014-02-02 23:34:23 -0300300 v->type = V4L2_TUNER_RF;
301 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
302 v->rangelow = 49000000;
303 v->rangehigh = 960000000;
304
305 return 0;
306}
307
308static int msi001_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
309{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300310 struct msi001_dev *dev = sd_to_msi001_dev(sd);
311 struct spi_device *spi = dev->spi;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300312
Antti Palosaari83e64b42015-04-29 10:57:12 -0300313 dev_dbg(&spi->dev, "index=%d\n", v->index);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300314 return 0;
315}
316
317static int msi001_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
318{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300319 struct msi001_dev *dev = sd_to_msi001_dev(sd);
320 struct spi_device *spi = dev->spi;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300321
Antti Palosaari83e64b42015-04-29 10:57:12 -0300322 dev_dbg(&spi->dev, "tuner=%d\n", f->tuner);
323 f->frequency = dev->f_tuner;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300324 return 0;
325}
326
327static int msi001_s_frequency(struct v4l2_subdev *sd,
Antti Palosaari83e64b42015-04-29 10:57:12 -0300328 const struct v4l2_frequency *f)
Antti Palosaari93203dd2014-02-02 23:34:23 -0300329{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300330 struct msi001_dev *dev = sd_to_msi001_dev(sd);
331 struct spi_device *spi = dev->spi;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300332 unsigned int band;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300333
Antti Palosaari83e64b42015-04-29 10:57:12 -0300334 dev_dbg(&spi->dev, "tuner=%d type=%d frequency=%u\n",
335 f->tuner, f->type, f->frequency);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300336
337 if (f->frequency < ((bands[0].rangehigh + bands[1].rangelow) / 2))
338 band = 0;
339 else
340 band = 1;
Antti Palosaari83e64b42015-04-29 10:57:12 -0300341 dev->f_tuner = clamp_t(unsigned int, f->frequency,
342 bands[band].rangelow, bands[band].rangehigh);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300343
Antti Palosaari83e64b42015-04-29 10:57:12 -0300344 return msi001_set_tuner(dev);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300345}
346
347static int msi001_enum_freq_bands(struct v4l2_subdev *sd,
Antti Palosaari83e64b42015-04-29 10:57:12 -0300348 struct v4l2_frequency_band *band)
Antti Palosaari93203dd2014-02-02 23:34:23 -0300349{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300350 struct msi001_dev *dev = sd_to_msi001_dev(sd);
351 struct spi_device *spi = dev->spi;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300352
Antti Palosaari83e64b42015-04-29 10:57:12 -0300353 dev_dbg(&spi->dev, "tuner=%d type=%d index=%d\n",
354 band->tuner, band->type, band->index);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300355
356 if (band->index >= ARRAY_SIZE(bands))
357 return -EINVAL;
358
359 band->capability = bands[band->index].capability;
360 band->rangelow = bands[band->index].rangelow;
361 band->rangehigh = bands[band->index].rangehigh;
362
363 return 0;
364}
365
366static const struct v4l2_subdev_tuner_ops msi001_tuner_ops = {
Hans Verkuil3aab15a2018-02-21 02:49:25 -0500367 .standby = msi001_standby,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300368 .g_tuner = msi001_g_tuner,
369 .s_tuner = msi001_s_tuner,
370 .g_frequency = msi001_g_frequency,
371 .s_frequency = msi001_s_frequency,
372 .enum_freq_bands = msi001_enum_freq_bands,
373};
374
375static const struct v4l2_subdev_ops msi001_ops = {
Antti Palosaari93203dd2014-02-02 23:34:23 -0300376 .tuner = &msi001_tuner_ops,
377};
378
379static int msi001_s_ctrl(struct v4l2_ctrl *ctrl)
380{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300381 struct msi001_dev *dev = container_of(ctrl->handler, struct msi001_dev, hdl);
382 struct spi_device *spi = dev->spi;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300383
384 int ret;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300385
Antti Palosaari83e64b42015-04-29 10:57:12 -0300386 dev_dbg(&spi->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
387 ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
388 ctrl->step);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300389
390 switch (ctrl->id) {
391 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
392 case V4L2_CID_RF_TUNER_BANDWIDTH:
Antti Palosaari83e64b42015-04-29 10:57:12 -0300393 ret = msi001_set_tuner(dev);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300394 break;
395 case V4L2_CID_RF_TUNER_LNA_GAIN:
Antti Palosaari83e64b42015-04-29 10:57:12 -0300396 ret = msi001_set_gain(dev, dev->lna_gain->val,
397 dev->mixer_gain->cur.val,
398 dev->if_gain->cur.val);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300399 break;
400 case V4L2_CID_RF_TUNER_MIXER_GAIN:
Antti Palosaari83e64b42015-04-29 10:57:12 -0300401 ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
402 dev->mixer_gain->val,
403 dev->if_gain->cur.val);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300404 break;
405 case V4L2_CID_RF_TUNER_IF_GAIN:
Antti Palosaari83e64b42015-04-29 10:57:12 -0300406 ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
407 dev->mixer_gain->cur.val,
408 dev->if_gain->val);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300409 break;
410 default:
Antti Palosaari83e64b42015-04-29 10:57:12 -0300411 dev_dbg(&spi->dev, "unknown control %d\n", ctrl->id);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300412 ret = -EINVAL;
413 }
414
415 return ret;
416}
417
418static const struct v4l2_ctrl_ops msi001_ctrl_ops = {
419 .s_ctrl = msi001_s_ctrl,
420};
421
422static int msi001_probe(struct spi_device *spi)
423{
Antti Palosaari83e64b42015-04-29 10:57:12 -0300424 struct msi001_dev *dev;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300425 int ret;
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300426
427 dev_dbg(&spi->dev, "\n");
Antti Palosaari93203dd2014-02-02 23:34:23 -0300428
Antti Palosaari83e64b42015-04-29 10:57:12 -0300429 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
430 if (!dev) {
Antti Palosaari93203dd2014-02-02 23:34:23 -0300431 ret = -ENOMEM;
Antti Palosaari83e64b42015-04-29 10:57:12 -0300432 goto err;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300433 }
434
Antti Palosaari83e64b42015-04-29 10:57:12 -0300435 dev->spi = spi;
436 dev->f_tuner = bands[0].rangelow;
437 v4l2_spi_subdev_init(&dev->sd, spi, &msi001_ops);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300438
439 /* Register controls */
Antti Palosaari83e64b42015-04-29 10:57:12 -0300440 v4l2_ctrl_handler_init(&dev->hdl, 5);
441 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300442 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
Antti Palosaari83e64b42015-04-29 10:57:12 -0300443 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300444 V4L2_CID_RF_TUNER_BANDWIDTH, 200000, 8000000, 1, 200000);
Antti Palosaari83e64b42015-04-29 10:57:12 -0300445 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
446 dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300447 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
Antti Palosaari83e64b42015-04-29 10:57:12 -0300448 dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300449 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
Antti Palosaari83e64b42015-04-29 10:57:12 -0300450 dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300451 V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
Antti Palosaari83e64b42015-04-29 10:57:12 -0300452 if (dev->hdl.error) {
453 ret = dev->hdl.error;
454 dev_err(&spi->dev, "Could not initialize controls\n");
Antti Palosaari93203dd2014-02-02 23:34:23 -0300455 /* control init failed, free handler */
456 goto err_ctrl_handler_free;
457 }
458
Antti Palosaari83e64b42015-04-29 10:57:12 -0300459 dev->sd.ctrl_handler = &dev->hdl;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300460 return 0;
Antti Palosaari93203dd2014-02-02 23:34:23 -0300461err_ctrl_handler_free:
Antti Palosaari83e64b42015-04-29 10:57:12 -0300462 v4l2_ctrl_handler_free(&dev->hdl);
463 kfree(dev);
464err:
Antti Palosaari93203dd2014-02-02 23:34:23 -0300465 return ret;
466}
467
468static int msi001_remove(struct spi_device *spi)
469{
470 struct v4l2_subdev *sd = spi_get_drvdata(spi);
Antti Palosaari83e64b42015-04-29 10:57:12 -0300471 struct msi001_dev *dev = sd_to_msi001_dev(sd);
Antti Palosaari5fa51cc2014-08-24 23:12:13 -0300472
473 dev_dbg(&spi->dev, "\n");
Antti Palosaari93203dd2014-02-02 23:34:23 -0300474
475 /*
476 * Registered by v4l2_spi_new_subdev() from master driver, but we must
477 * unregister it from here. Weird.
478 */
Antti Palosaari83e64b42015-04-29 10:57:12 -0300479 v4l2_device_unregister_subdev(&dev->sd);
480 v4l2_ctrl_handler_free(&dev->hdl);
481 kfree(dev);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300482 return 0;
483}
484
Antti Palosaari83e64b42015-04-29 10:57:12 -0300485static const struct spi_device_id msi001_id_table[] = {
Antti Palosaari93203dd2014-02-02 23:34:23 -0300486 {"msi001", 0},
487 {}
488};
Antti Palosaari83e64b42015-04-29 10:57:12 -0300489MODULE_DEVICE_TABLE(spi, msi001_id_table);
Antti Palosaari93203dd2014-02-02 23:34:23 -0300490
491static struct spi_driver msi001_driver = {
492 .driver = {
493 .name = "msi001",
Antti Palosaari83e64b42015-04-29 10:57:12 -0300494 .suppress_bind_attrs = true,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300495 },
496 .probe = msi001_probe,
497 .remove = msi001_remove,
Antti Palosaari83e64b42015-04-29 10:57:12 -0300498 .id_table = msi001_id_table,
Antti Palosaari93203dd2014-02-02 23:34:23 -0300499};
500module_spi_driver(msi001_driver);
501
502MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
503MODULE_DESCRIPTION("Mirics MSi001");
504MODULE_LICENSE("GPL");