Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 2 | /* |
| 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/gcd.h> |
| 11 | #include <media/v4l2-device.h> |
| 12 | #include <media/v4l2-ctrls.h> |
| 13 | |
| 14 | static 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 Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 30 | struct msi001_dev { |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 31 | 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 Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 45 | static inline struct msi001_dev *sd_to_msi001_dev(struct v4l2_subdev *sd) |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 46 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 47 | return container_of(sd, struct msi001_dev, sd); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 48 | } |
| 49 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 50 | static int msi001_wreg(struct msi001_dev *dev, u32 data) |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 51 | { |
| 52 | /* Register format: 4 bits addr + 20 bits value */ |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 53 | return spi_write(dev->spi, &data, 3); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 54 | }; |
| 55 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 56 | static int msi001_set_gain(struct msi001_dev *dev, int lna_gain, int mixer_gain, |
| 57 | int if_gain) |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 58 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 59 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 60 | int ret; |
| 61 | u32 reg; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 62 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 63 | dev_dbg(&spi->dev, "lna=%d mixer=%d if=%d\n", |
| 64 | lna_gain, mixer_gain, if_gain); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 65 | |
| 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 Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 73 | ret = msi001_wreg(dev, reg); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 74 | if (ret) |
| 75 | goto err; |
| 76 | |
| 77 | return 0; |
| 78 | err: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 79 | dev_dbg(&spi->dev, "failed %d\n", ret); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 80 | return ret; |
| 81 | }; |
| 82 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 83 | static int msi001_set_tuner(struct msi001_dev *dev) |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 84 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 85 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 86 | int ret, i; |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 87 | unsigned int uitmp, div_n, k, k_thresh, k_frac, div_lo, f_if1; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 88 | u32 reg; |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 89 | u64 f_vco; |
| 90 | u8 mode, filter_mode; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 91 | |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 92 | static const struct { |
| 93 | u32 rf; |
| 94 | u8 mode; |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 95 | u8 div_lo; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 96 | } 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 Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 126 | unsigned int f_rf = dev->f_tuner; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 127 | |
| 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 Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 140 | #define DIV_PRE_N 4 |
| 141 | #define F_VCO_STEP div_lo |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 142 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 143 | dev_dbg(&spi->dev, "f_rf=%d f_if=%d\n", f_rf, f_if); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 144 | |
| 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 Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 148 | div_lo = band_lut[i].div_lo; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | } |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 152 | 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 169 | if (i == ARRAY_SIZE(if_freq_lut)) { |
| 170 | ret = -EINVAL; |
| 171 | goto err; |
| 172 | } |
| 173 | |
| 174 | /* filters */ |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 175 | bandwidth = dev->bandwidth->val; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 176 | 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 184 | if (i == ARRAY_SIZE(bandwidth_lut)) { |
| 185 | ret = -EINVAL; |
| 186 | goto err; |
| 187 | } |
| 188 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 189 | dev->bandwidth->val = bandwidth_lut[i].freq; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 190 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 191 | dev_dbg(&spi->dev, "bandwidth selected=%d\n", bandwidth_lut[i].freq); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 192 | |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 193 | /* |
| 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 208 | |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 209 | /* 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 214 | |
| 215 | /* Find out greatest common divisor and divide to smaller. */ |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 216 | uitmp = gcd(k_thresh, k_frac); |
| 217 | k_thresh /= uitmp; |
| 218 | k_frac /= uitmp; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 219 | |
| 220 | /* Force divide to reg max. Resolution will be reduced. */ |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 221 | 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 224 | |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 225 | /* 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 229 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 230 | dev_dbg(&spi->dev, |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 231 | "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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 233 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 234 | ret = msi001_wreg(dev, 0x00000e); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 235 | if (ret) |
| 236 | goto err; |
| 237 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 238 | ret = msi001_wreg(dev, 0x000003); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 239 | 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 Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 248 | ret = msi001_wreg(dev, reg); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 249 | if (ret) |
| 250 | goto err; |
| 251 | |
| 252 | reg = 5 << 0; |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 253 | reg |= k_thresh << 4; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 254 | reg |= 1 << 19; |
| 255 | reg |= 1 << 21; |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 256 | ret = msi001_wreg(dev, reg); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 257 | if (ret) |
| 258 | goto err; |
| 259 | |
| 260 | reg = 2 << 0; |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 261 | reg |= k_frac << 4; |
| 262 | reg |= div_n << 16; |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 263 | ret = msi001_wreg(dev, reg); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 264 | if (ret) |
| 265 | goto err; |
| 266 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 267 | ret = msi001_set_gain(dev, dev->lna_gain->cur.val, |
| 268 | dev->mixer_gain->cur.val, dev->if_gain->cur.val); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 269 | if (ret) |
| 270 | goto err; |
| 271 | |
| 272 | reg = 6 << 0; |
| 273 | reg |= 63 << 4; |
| 274 | reg |= 4095 << 10; |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 275 | ret = msi001_wreg(dev, reg); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 276 | if (ret) |
| 277 | goto err; |
| 278 | |
| 279 | return 0; |
| 280 | err: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 281 | dev_dbg(&spi->dev, "failed %d\n", ret); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 282 | return ret; |
Antti Palosaari | 9195e5b | 2015-04-29 10:10:01 -0300 | [diff] [blame] | 283 | } |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 284 | |
Hans Verkuil | 3aab15a | 2018-02-21 02:49:25 -0500 | [diff] [blame] | 285 | static int msi001_standby(struct v4l2_subdev *sd) |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 286 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 287 | struct msi001_dev *dev = sd_to_msi001_dev(sd); |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 288 | |
Hans Verkuil | 3aab15a | 2018-02-21 02:49:25 -0500 | [diff] [blame] | 289 | return msi001_wreg(dev, 0x000000); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 290 | } |
| 291 | |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 292 | static int msi001_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v) |
| 293 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 294 | struct msi001_dev *dev = sd_to_msi001_dev(sd); |
| 295 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 296 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 297 | dev_dbg(&spi->dev, "index=%d\n", v->index); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 298 | |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 299 | strscpy(v->name, "Mirics MSi001", sizeof(v->name)); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 300 | 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 | |
| 308 | static int msi001_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v) |
| 309 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 310 | struct msi001_dev *dev = sd_to_msi001_dev(sd); |
| 311 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 312 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 313 | dev_dbg(&spi->dev, "index=%d\n", v->index); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int msi001_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) |
| 318 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 319 | struct msi001_dev *dev = sd_to_msi001_dev(sd); |
| 320 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 321 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 322 | dev_dbg(&spi->dev, "tuner=%d\n", f->tuner); |
| 323 | f->frequency = dev->f_tuner; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int msi001_s_frequency(struct v4l2_subdev *sd, |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 328 | const struct v4l2_frequency *f) |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 329 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 330 | struct msi001_dev *dev = sd_to_msi001_dev(sd); |
| 331 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 332 | unsigned int band; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 333 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 334 | dev_dbg(&spi->dev, "tuner=%d type=%d frequency=%u\n", |
| 335 | f->tuner, f->type, f->frequency); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 336 | |
| 337 | if (f->frequency < ((bands[0].rangehigh + bands[1].rangelow) / 2)) |
| 338 | band = 0; |
| 339 | else |
| 340 | band = 1; |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 341 | dev->f_tuner = clamp_t(unsigned int, f->frequency, |
| 342 | bands[band].rangelow, bands[band].rangehigh); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 343 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 344 | return msi001_set_tuner(dev); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static int msi001_enum_freq_bands(struct v4l2_subdev *sd, |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 348 | struct v4l2_frequency_band *band) |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 349 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 350 | struct msi001_dev *dev = sd_to_msi001_dev(sd); |
| 351 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 352 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 353 | dev_dbg(&spi->dev, "tuner=%d type=%d index=%d\n", |
| 354 | band->tuner, band->type, band->index); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 355 | |
| 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 | |
| 366 | static const struct v4l2_subdev_tuner_ops msi001_tuner_ops = { |
Hans Verkuil | 3aab15a | 2018-02-21 02:49:25 -0500 | [diff] [blame] | 367 | .standby = msi001_standby, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 368 | .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 | |
| 375 | static const struct v4l2_subdev_ops msi001_ops = { |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 376 | .tuner = &msi001_tuner_ops, |
| 377 | }; |
| 378 | |
| 379 | static int msi001_s_ctrl(struct v4l2_ctrl *ctrl) |
| 380 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 381 | struct msi001_dev *dev = container_of(ctrl->handler, struct msi001_dev, hdl); |
| 382 | struct spi_device *spi = dev->spi; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 383 | |
| 384 | int ret; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 385 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 386 | 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 Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 389 | |
| 390 | switch (ctrl->id) { |
| 391 | case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: |
| 392 | case V4L2_CID_RF_TUNER_BANDWIDTH: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 393 | ret = msi001_set_tuner(dev); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 394 | break; |
| 395 | case V4L2_CID_RF_TUNER_LNA_GAIN: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 396 | ret = msi001_set_gain(dev, dev->lna_gain->val, |
| 397 | dev->mixer_gain->cur.val, |
| 398 | dev->if_gain->cur.val); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 399 | break; |
| 400 | case V4L2_CID_RF_TUNER_MIXER_GAIN: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 401 | ret = msi001_set_gain(dev, dev->lna_gain->cur.val, |
| 402 | dev->mixer_gain->val, |
| 403 | dev->if_gain->cur.val); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 404 | break; |
| 405 | case V4L2_CID_RF_TUNER_IF_GAIN: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 406 | ret = msi001_set_gain(dev, dev->lna_gain->cur.val, |
| 407 | dev->mixer_gain->cur.val, |
| 408 | dev->if_gain->val); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 409 | break; |
| 410 | default: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 411 | dev_dbg(&spi->dev, "unknown control %d\n", ctrl->id); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 412 | ret = -EINVAL; |
| 413 | } |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static const struct v4l2_ctrl_ops msi001_ctrl_ops = { |
| 419 | .s_ctrl = msi001_s_ctrl, |
| 420 | }; |
| 421 | |
| 422 | static int msi001_probe(struct spi_device *spi) |
| 423 | { |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 424 | struct msi001_dev *dev; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 425 | int ret; |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 426 | |
| 427 | dev_dbg(&spi->dev, "\n"); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 428 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 429 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 430 | if (!dev) { |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 431 | ret = -ENOMEM; |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 432 | goto err; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 433 | } |
| 434 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 435 | dev->spi = spi; |
| 436 | dev->f_tuner = bands[0].rangelow; |
| 437 | v4l2_spi_subdev_init(&dev->sd, spi, &msi001_ops); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 438 | |
| 439 | /* Register controls */ |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 440 | v4l2_ctrl_handler_init(&dev->hdl, 5); |
| 441 | dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 442 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1); |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 443 | dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 444 | V4L2_CID_RF_TUNER_BANDWIDTH, 200000, 8000000, 1, 200000); |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 445 | v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); |
| 446 | dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 447 | V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1); |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 448 | dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 449 | V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1); |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 450 | dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 451 | V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0); |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 452 | if (dev->hdl.error) { |
| 453 | ret = dev->hdl.error; |
| 454 | dev_err(&spi->dev, "Could not initialize controls\n"); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 455 | /* control init failed, free handler */ |
| 456 | goto err_ctrl_handler_free; |
| 457 | } |
| 458 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 459 | dev->sd.ctrl_handler = &dev->hdl; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 460 | return 0; |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 461 | err_ctrl_handler_free: |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 462 | v4l2_ctrl_handler_free(&dev->hdl); |
| 463 | kfree(dev); |
| 464 | err: |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | static int msi001_remove(struct spi_device *spi) |
| 469 | { |
| 470 | struct v4l2_subdev *sd = spi_get_drvdata(spi); |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 471 | struct msi001_dev *dev = sd_to_msi001_dev(sd); |
Antti Palosaari | 5fa51cc | 2014-08-24 23:12:13 -0300 | [diff] [blame] | 472 | |
| 473 | dev_dbg(&spi->dev, "\n"); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * Registered by v4l2_spi_new_subdev() from master driver, but we must |
| 477 | * unregister it from here. Weird. |
| 478 | */ |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 479 | v4l2_device_unregister_subdev(&dev->sd); |
| 480 | v4l2_ctrl_handler_free(&dev->hdl); |
| 481 | kfree(dev); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 485 | static const struct spi_device_id msi001_id_table[] = { |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 486 | {"msi001", 0}, |
| 487 | {} |
| 488 | }; |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 489 | MODULE_DEVICE_TABLE(spi, msi001_id_table); |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 490 | |
| 491 | static struct spi_driver msi001_driver = { |
| 492 | .driver = { |
| 493 | .name = "msi001", |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 494 | .suppress_bind_attrs = true, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 495 | }, |
| 496 | .probe = msi001_probe, |
| 497 | .remove = msi001_remove, |
Antti Palosaari | 83e64b4 | 2015-04-29 10:57:12 -0300 | [diff] [blame] | 498 | .id_table = msi001_id_table, |
Antti Palosaari | 93203dd | 2014-02-02 23:34:23 -0300 | [diff] [blame] | 499 | }; |
| 500 | module_spi_driver(msi001_driver); |
| 501 | |
| 502 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 503 | MODULE_DESCRIPTION("Mirics MSi001"); |
| 504 | MODULE_LICENSE("GPL"); |