Thomas Gleixner | 1621633 | 2019-05-19 15:51:31 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 2 | /* |
| 3 | * Elonics E4000 silicon tuner driver |
| 4 | * |
| 5 | * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "e4000_priv.h" |
| 9 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 10 | static int e4000_init(struct e4000_dev *dev) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 11 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 12 | struct i2c_client *client = dev->client; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 13 | int ret; |
| 14 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 15 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 16 | |
| 17 | /* reset */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 18 | ret = regmap_write(dev->regmap, 0x00, 0x01); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 19 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 20 | goto err; |
| 21 | |
| 22 | /* disable output clock */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 23 | ret = regmap_write(dev->regmap, 0x06, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 24 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 25 | goto err; |
| 26 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 27 | ret = regmap_write(dev->regmap, 0x7a, 0x96); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 28 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 29 | goto err; |
| 30 | |
| 31 | /* configure gains */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 32 | ret = regmap_bulk_write(dev->regmap, 0x7e, "\x01\xfe", 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 33 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 34 | goto err; |
| 35 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 36 | ret = regmap_write(dev->regmap, 0x82, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 37 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 38 | goto err; |
| 39 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 40 | ret = regmap_write(dev->regmap, 0x24, 0x05); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 41 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 42 | goto err; |
| 43 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 44 | ret = regmap_bulk_write(dev->regmap, 0x87, "\x20\x01", 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 45 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 46 | goto err; |
| 47 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 48 | ret = regmap_bulk_write(dev->regmap, 0x9f, "\x7f\x07", 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 49 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 50 | goto err; |
| 51 | |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 52 | /* DC offset control */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 53 | ret = regmap_write(dev->regmap, 0x2d, 0x1f); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 54 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 55 | goto err; |
| 56 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 57 | ret = regmap_bulk_write(dev->regmap, 0x70, "\x01\x01", 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 58 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 59 | goto err; |
| 60 | |
| 61 | /* gain control */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 62 | ret = regmap_write(dev->regmap, 0x1a, 0x17); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 63 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 64 | goto err; |
| 65 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 66 | ret = regmap_write(dev->regmap, 0x1f, 0x1a); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 67 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 68 | goto err; |
| 69 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 70 | dev->active = true; |
| 71 | |
| 72 | return 0; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 73 | err: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 74 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 78 | static int e4000_sleep(struct e4000_dev *dev) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 79 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 80 | struct i2c_client *client = dev->client; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 81 | int ret; |
| 82 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 83 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 84 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 85 | dev->active = false; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 86 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 87 | ret = regmap_write(dev->regmap, 0x00, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 88 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 89 | goto err; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 90 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 91 | return 0; |
| 92 | err: |
| 93 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 94 | return ret; |
| 95 | } |
| 96 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 97 | static int e4000_set_params(struct e4000_dev *dev) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 98 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 99 | struct i2c_client *client = dev->client; |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 100 | int ret, i; |
| 101 | unsigned int div_n, k, k_cw, div_out; |
Antti Palosaari | 0ed0b22 | 2014-01-27 03:13:19 -0300 | [diff] [blame] | 102 | u64 f_vco; |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 103 | u8 buf[5], i_data[4], q_data[4]; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 104 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 105 | if (!dev->active) { |
| 106 | dev_dbg(&client->dev, "tuner is sleeping\n"); |
| 107 | return 0; |
| 108 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 109 | |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 110 | /* gain control manual */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 111 | ret = regmap_write(dev->regmap, 0x1a, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 112 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 113 | goto err; |
| 114 | |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 115 | /* |
| 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 Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 130 | for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 131 | if (dev->f_frequency <= e4000_pll_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 132 | break; |
| 133 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 134 | if (i == ARRAY_SIZE(e4000_pll_lut)) { |
| 135 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 136 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 137 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 138 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 139 | #define F_REF dev->clk |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 140 | div_out = e4000_pll_lut[i].div_out; |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 141 | f_vco = (u64) dev->f_frequency * div_out; |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 142 | /* 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 Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 146 | dev_dbg(&client->dev, |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 147 | "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 Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 150 | |
| 151 | buf[0] = div_n; |
| 152 | buf[1] = (k_cw >> 0) & 0xff; |
| 153 | buf[2] = (k_cw >> 8) & 0xff; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 154 | buf[3] = 0x00; |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 155 | buf[4] = e4000_pll_lut[i].div_out_reg; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 156 | ret = regmap_bulk_write(dev->regmap, 0x09, buf, 5); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 157 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 158 | goto err; |
| 159 | |
| 160 | /* LNA filter (RF filter) */ |
| 161 | for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 162 | if (dev->f_frequency <= e400_lna_filter_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 163 | break; |
| 164 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 165 | if (i == ARRAY_SIZE(e400_lna_filter_lut)) { |
| 166 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 167 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 168 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 169 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 170 | ret = regmap_write(dev->regmap, 0x10, e400_lna_filter_lut[i].val); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 171 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 172 | goto err; |
| 173 | |
| 174 | /* IF filters */ |
| 175 | for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 176 | if (dev->f_bandwidth <= e4000_if_filter_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 177 | break; |
| 178 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 179 | if (i == ARRAY_SIZE(e4000_if_filter_lut)) { |
| 180 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 181 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 182 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 183 | |
| 184 | buf[0] = e4000_if_filter_lut[i].reg11_val; |
| 185 | buf[1] = e4000_if_filter_lut[i].reg12_val; |
| 186 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 187 | ret = regmap_bulk_write(dev->regmap, 0x11, buf, 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 188 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 189 | goto err; |
| 190 | |
| 191 | /* frequency band */ |
| 192 | for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 193 | if (dev->f_frequency <= e4000_band_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 194 | break; |
| 195 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 196 | if (i == ARRAY_SIZE(e4000_band_lut)) { |
| 197 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 198 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 199 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 200 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 201 | ret = regmap_write(dev->regmap, 0x07, e4000_band_lut[i].reg07_val); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 202 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 203 | goto err; |
| 204 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 205 | ret = regmap_write(dev->regmap, 0x78, e4000_band_lut[i].reg78_val); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 206 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 207 | goto err; |
| 208 | |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 209 | /* DC offset */ |
| 210 | for (i = 0; i < 4; i++) { |
| 211 | if (i == 0) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 212 | ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7e\x24", 3); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 213 | else if (i == 1) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 214 | ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7f", 2); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 215 | else if (i == 2) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 216 | ret = regmap_bulk_write(dev->regmap, 0x15, "\x01", 1); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 217 | else |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 218 | ret = regmap_bulk_write(dev->regmap, 0x16, "\x7e", 1); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 219 | |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 220 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 221 | goto err; |
| 222 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 223 | ret = regmap_write(dev->regmap, 0x29, 0x01); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 224 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 225 | goto err; |
| 226 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 227 | ret = regmap_bulk_read(dev->regmap, 0x2a, buf, 3); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 228 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 229 | 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 Palosaari | d4992da | 2013-07-24 18:33:51 -0300 | [diff] [blame] | 235 | swap(q_data[2], q_data[3]); |
| 236 | swap(i_data[2], i_data[3]); |
| 237 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 238 | ret = regmap_bulk_write(dev->regmap, 0x50, q_data, 4); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 239 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 240 | goto err; |
| 241 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 242 | ret = regmap_bulk_write(dev->regmap, 0x60, i_data, 4); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 243 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 244 | goto err; |
| 245 | |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 246 | /* gain control auto */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 247 | ret = regmap_write(dev->regmap, 0x1a, 0x17); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 248 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 249 | goto err; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 250 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 251 | return 0; |
| 252 | err: |
| 253 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 254 | return ret; |
| 255 | } |
| 256 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 257 | /* |
| 258 | * V4L2 API |
| 259 | */ |
| 260 | #if IS_ENABLED(CONFIG_VIDEO_V4L2) |
| 261 | static 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 Verkuil | 1ba90492 | 2015-05-22 04:39:54 -0300 | [diff] [blame] | 274 | .rangehigh = 2208000000UL, |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 275 | }, |
| 276 | }; |
| 277 | |
| 278 | static inline struct e4000_dev *e4000_subdev_to_dev(struct v4l2_subdev *sd) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 279 | { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 280 | return container_of(sd, struct e4000_dev, sd); |
| 281 | } |
| 282 | |
Hans Verkuil | 3aab15a | 2018-02-21 02:49:25 -0500 | [diff] [blame] | 283 | static int e4000_standby(struct v4l2_subdev *sd) |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 284 | { |
| 285 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 286 | int ret; |
| 287 | |
Hans Verkuil | 3aab15a | 2018-02-21 02:49:25 -0500 | [diff] [blame] | 288 | ret = e4000_sleep(dev); |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 289 | if (ret) |
| 290 | return ret; |
| 291 | |
| 292 | return e4000_set_params(dev); |
| 293 | } |
| 294 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 295 | static int e4000_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v) |
| 296 | { |
| 297 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 298 | struct i2c_client *client = dev->client; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 299 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 300 | dev_dbg(&client->dev, "index=%d\n", v->index); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 301 | |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 302 | strscpy(v->name, "Elonics E4000", sizeof(v->name)); |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 303 | 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 Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 310 | static 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 | |
| 319 | static 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 | |
| 329 | static 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 | |
| 343 | static 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 | |
| 361 | static const struct v4l2_subdev_tuner_ops e4000_subdev_tuner_ops = { |
Hans Verkuil | 3aab15a | 2018-02-21 02:49:25 -0500 | [diff] [blame] | 362 | .standby = e4000_standby, |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 363 | .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 | |
| 370 | static const struct v4l2_subdev_ops e4000_subdev_ops = { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 371 | .tuner = &e4000_subdev_tuner_ops, |
| 372 | }; |
| 373 | |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 374 | static int e4000_set_lna_gain(struct dvb_frontend *fe) |
| 375 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 376 | struct e4000_dev *dev = fe->tuner_priv; |
| 377 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 378 | int ret; |
| 379 | u8 u8tmp; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 380 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 381 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 384 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 385 | if (dev->lna_gain_auto->val && dev->if_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 386 | u8tmp = 0x17; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 387 | else if (dev->lna_gain_auto->val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 388 | u8tmp = 0x19; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 389 | else if (dev->if_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 390 | u8tmp = 0x16; |
| 391 | else |
| 392 | u8tmp = 0x10; |
| 393 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 394 | ret = regmap_write(dev->regmap, 0x1a, u8tmp); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 395 | if (ret) |
| 396 | goto err; |
| 397 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 398 | if (dev->lna_gain_auto->val == false) { |
| 399 | ret = regmap_write(dev->regmap, 0x14, dev->lna_gain->val); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 400 | if (ret) |
| 401 | goto err; |
| 402 | } |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 403 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 404 | return 0; |
| 405 | err: |
| 406 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | static int e4000_set_mixer_gain(struct dvb_frontend *fe) |
| 411 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 412 | struct e4000_dev *dev = fe->tuner_priv; |
| 413 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 414 | int ret; |
| 415 | u8 u8tmp; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 416 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 417 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 420 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 421 | if (dev->mixer_gain_auto->val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 422 | u8tmp = 0x15; |
| 423 | else |
| 424 | u8tmp = 0x14; |
| 425 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 426 | ret = regmap_write(dev->regmap, 0x20, u8tmp); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 427 | if (ret) |
| 428 | goto err; |
| 429 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 430 | if (dev->mixer_gain_auto->val == false) { |
| 431 | ret = regmap_write(dev->regmap, 0x15, dev->mixer_gain->val); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 432 | if (ret) |
| 433 | goto err; |
| 434 | } |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 435 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 436 | return 0; |
| 437 | err: |
| 438 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static int e4000_set_if_gain(struct dvb_frontend *fe) |
| 443 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 444 | struct e4000_dev *dev = fe->tuner_priv; |
| 445 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 446 | int ret; |
| 447 | u8 buf[2]; |
| 448 | u8 u8tmp; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 449 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 450 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 453 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 454 | if (dev->if_gain_auto->val && dev->lna_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 455 | u8tmp = 0x17; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 456 | else if (dev->lna_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 457 | u8tmp = 0x19; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 458 | else if (dev->if_gain_auto->val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 459 | u8tmp = 0x16; |
| 460 | else |
| 461 | u8tmp = 0x10; |
| 462 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 463 | ret = regmap_write(dev->regmap, 0x1a, u8tmp); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 464 | if (ret) |
| 465 | goto err; |
| 466 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 467 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 471 | if (ret) |
| 472 | goto err; |
| 473 | } |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 474 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 475 | return 0; |
| 476 | err: |
| 477 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 478 | return ret; |
| 479 | } |
| 480 | |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 481 | static int e4000_pll_lock(struct dvb_frontend *fe) |
| 482 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 483 | struct e4000_dev *dev = fe->tuner_priv; |
| 484 | struct i2c_client *client = dev->client; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 485 | int ret; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 486 | unsigned int uitmp; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 487 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 488 | ret = regmap_read(dev->regmap, 0x07, &uitmp); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 489 | if (ret) |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 490 | goto err; |
| 491 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 492 | dev->pll_lock->val = (uitmp & 0x01); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 493 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 494 | return 0; |
| 495 | err: |
| 496 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl) |
| 501 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 502 | struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl); |
| 503 | struct i2c_client *client = dev->client; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 504 | int ret; |
| 505 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 506 | if (!dev->active) |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 507 | return 0; |
| 508 | |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 509 | switch (ctrl->id) { |
| 510 | case V4L2_CID_RF_TUNER_PLL_LOCK: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 511 | ret = e4000_pll_lock(dev->fe); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 512 | break; |
| 513 | default: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 514 | dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n", |
| 515 | ctrl->id, ctrl->name); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 516 | ret = -EINVAL; |
| 517 | } |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 522 | static int e4000_s_ctrl(struct v4l2_ctrl *ctrl) |
| 523 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 524 | struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl); |
| 525 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 526 | int ret; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 527 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 528 | if (!dev->active) |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 529 | return 0; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 530 | |
| 531 | switch (ctrl->id) { |
| 532 | case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: |
| 533 | case V4L2_CID_RF_TUNER_BANDWIDTH: |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 534 | /* |
| 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 541 | break; |
| 542 | case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO: |
| 543 | case V4L2_CID_RF_TUNER_LNA_GAIN: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 544 | ret = e4000_set_lna_gain(dev->fe); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 545 | break; |
| 546 | case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO: |
| 547 | case V4L2_CID_RF_TUNER_MIXER_GAIN: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 548 | ret = e4000_set_mixer_gain(dev->fe); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 549 | break; |
| 550 | case V4L2_CID_RF_TUNER_IF_GAIN_AUTO: |
| 551 | case V4L2_CID_RF_TUNER_IF_GAIN: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 552 | ret = e4000_set_if_gain(dev->fe); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 553 | break; |
| 554 | default: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 555 | dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n", |
| 556 | ctrl->id, ctrl->name); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 557 | ret = -EINVAL; |
| 558 | } |
| 559 | |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | static const struct v4l2_ctrl_ops e4000_ctrl_ops = { |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 564 | .g_volatile_ctrl = e4000_g_volatile_ctrl, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 565 | .s_ctrl = e4000_s_ctrl, |
| 566 | }; |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 567 | #endif |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 568 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 569 | /* |
| 570 | * DVB API |
| 571 | */ |
| 572 | static 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 | |
| 582 | static int e4000_dvb_init(struct dvb_frontend *fe) |
| 583 | { |
| 584 | return e4000_init(fe->tuner_priv); |
| 585 | } |
| 586 | |
| 587 | static int e4000_dvb_sleep(struct dvb_frontend *fe) |
| 588 | { |
| 589 | return e4000_sleep(fe->tuner_priv); |
| 590 | } |
| 591 | |
| 592 | static int e4000_dvb_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 593 | { |
| 594 | *frequency = 0; /* Zero-IF */ |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static const struct dvb_tuner_ops e4000_dvb_tuner_ops = { |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 599 | .info = { |
Mauro Carvalho Chehab | a3f90c7 | 2018-07-05 18:59:35 -0400 | [diff] [blame] | 600 | .name = "Elonics E4000", |
| 601 | .frequency_min_hz = 174 * MHz, |
| 602 | .frequency_max_hz = 862 * MHz, |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 603 | }, |
| 604 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 605 | .init = e4000_dvb_init, |
| 606 | .sleep = e4000_dvb_sleep, |
| 607 | .set_params = e4000_dvb_set_params, |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 608 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 609 | .get_if_frequency = e4000_dvb_get_if_frequency, |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 610 | }; |
| 611 | |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 612 | static int e4000_probe(struct i2c_client *client, |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 613 | const struct i2c_device_id *id) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 614 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 615 | struct e4000_dev *dev; |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 616 | struct e4000_config *cfg = client->dev.platform_data; |
| 617 | struct dvb_frontend *fe = cfg->fe; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 618 | int ret; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 619 | unsigned int uitmp; |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 620 | static const struct regmap_config regmap_config = { |
| 621 | .reg_bits = 8, |
| 622 | .val_bits = 8, |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 623 | }; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 624 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 625 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 626 | if (!dev) { |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 627 | ret = -ENOMEM; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 628 | goto err; |
| 629 | } |
| 630 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 631 | dev->clk = cfg->clock; |
| 632 | dev->client = client; |
| 633 | dev->fe = cfg->fe; |
| 634 | dev->regmap = devm_regmap_init_i2c(client, ®map_config); |
| 635 | if (IS_ERR(dev->regmap)) { |
| 636 | ret = PTR_ERR(dev->regmap); |
| 637 | goto err_kfree; |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 638 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 639 | |
| 640 | /* check if the tuner is there */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 641 | ret = regmap_read(dev->regmap, 0x02, &uitmp); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 642 | if (ret) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 643 | goto err_kfree; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 644 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 645 | dev_dbg(&client->dev, "chip id=%02x\n", uitmp); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 646 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 647 | if (uitmp != 0x40) { |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 648 | ret = -ENODEV; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 649 | goto err_kfree; |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 650 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 651 | |
| 652 | /* put sleep as chip seems to be in normal mode by default */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 653 | ret = regmap_write(dev->regmap, 0x00, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 654 | if (ret) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 655 | goto err_kfree; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 656 | |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 657 | #if IS_ENABLED(CONFIG_VIDEO_V4L2) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 658 | /* Register controls */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 659 | v4l2_ctrl_handler_init(&dev->hdl, 9); |
| 660 | dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 661 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 662 | dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 663 | V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 664 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 666 | V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 667 | dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 668 | V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 669 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 671 | V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 672 | dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 673 | V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 674 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 676 | V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 677 | dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 678 | V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 679 | 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 Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 681 | V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 682 | 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 Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 687 | } |
| 688 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 689 | dev->sd.ctrl_handler = &dev->hdl; |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 690 | 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 Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 693 | #endif |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 694 | fe->tuner_priv = dev; |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 695 | memcpy(&fe->ops.tuner_ops, &e4000_dvb_tuner_ops, |
| 696 | sizeof(fe->ops.tuner_ops)); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 697 | v4l2_set_subdevdata(&dev->sd, client); |
| 698 | i2c_set_clientdata(client, &dev->sd); |
Antti Palosaari | 36f647b | 2012-09-22 12:32:27 -0300 | [diff] [blame] | 699 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 700 | dev_info(&client->dev, "Elonics E4000 successfully identified\n"); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 701 | return 0; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 702 | err_kfree: |
| 703 | kfree(dev); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 704 | err: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 705 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 706 | return ret; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 707 | } |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 708 | |
| 709 | static int e4000_remove(struct i2c_client *client) |
| 710 | { |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 711 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 712 | struct e4000_dev *dev = container_of(sd, struct e4000_dev, sd); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 713 | |
Antti Palosaari | 13bd82d | 2014-08-24 23:35:48 -0300 | [diff] [blame] | 714 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 715 | |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 716 | #if IS_ENABLED(CONFIG_VIDEO_V4L2) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 717 | v4l2_ctrl_handler_free(&dev->hdl); |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 718 | #endif |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 719 | kfree(dev); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 724 | static const struct i2c_device_id e4000_id_table[] = { |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 725 | {"e4000", 0}, |
| 726 | {} |
| 727 | }; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 728 | MODULE_DEVICE_TABLE(i2c, e4000_id_table); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 729 | |
| 730 | static struct i2c_driver e4000_driver = { |
| 731 | .driver = { |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 732 | .name = "e4000", |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 733 | .suppress_bind_attrs = true, |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 734 | }, |
| 735 | .probe = e4000_probe, |
| 736 | .remove = e4000_remove, |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 737 | .id_table = e4000_id_table, |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 738 | }; |
| 739 | |
| 740 | module_i2c_driver(e4000_driver); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 741 | |
| 742 | MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver"); |
| 743 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 744 | MODULE_LICENSE("GPL"); |