Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Elonics E4000 silicon tuner driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include "e4000_priv.h" |
| 22 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 23 | static int e4000_init(struct e4000_dev *dev) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 24 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 25 | struct i2c_client *client = dev->client; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 26 | int ret; |
| 27 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 28 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 29 | |
| 30 | /* reset */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 31 | ret = regmap_write(dev->regmap, 0x00, 0x01); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 32 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 33 | goto err; |
| 34 | |
| 35 | /* disable output clock */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 36 | ret = regmap_write(dev->regmap, 0x06, 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, 0x7a, 0x96); |
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 | |
| 44 | /* configure gains */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 45 | ret = regmap_bulk_write(dev->regmap, 0x7e, "\x01\xfe", 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 46 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 47 | goto err; |
| 48 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 49 | ret = regmap_write(dev->regmap, 0x82, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 50 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 51 | goto err; |
| 52 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 53 | ret = regmap_write(dev->regmap, 0x24, 0x05); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 54 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -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, 0x87, "\x20\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 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 61 | ret = regmap_bulk_write(dev->regmap, 0x9f, "\x7f\x07", 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 62 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 63 | goto err; |
| 64 | |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 65 | /* DC offset control */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 66 | ret = regmap_write(dev->regmap, 0x2d, 0x1f); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 67 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 68 | goto err; |
| 69 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 70 | ret = regmap_bulk_write(dev->regmap, 0x70, "\x01\x01", 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 71 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 72 | goto err; |
| 73 | |
| 74 | /* gain control */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 75 | ret = regmap_write(dev->regmap, 0x1a, 0x17); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 76 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 77 | goto err; |
| 78 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 79 | ret = regmap_write(dev->regmap, 0x1f, 0x1a); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 80 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 81 | goto err; |
| 82 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 83 | dev->active = true; |
| 84 | |
| 85 | return 0; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 86 | err: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 87 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 91 | static int e4000_sleep(struct e4000_dev *dev) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 92 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 93 | struct i2c_client *client = dev->client; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 94 | int ret; |
| 95 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 96 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 97 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 98 | dev->active = false; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 99 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 100 | ret = regmap_write(dev->regmap, 0x00, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 101 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 102 | goto err; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 103 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 104 | return 0; |
| 105 | err: |
| 106 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 107 | return ret; |
| 108 | } |
| 109 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 110 | static int e4000_set_params(struct e4000_dev *dev) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 111 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 112 | struct i2c_client *client = dev->client; |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 113 | int ret, i; |
| 114 | unsigned int div_n, k, k_cw, div_out; |
Antti Palosaari | 0ed0b22 | 2014-01-27 03:13:19 -0300 | [diff] [blame] | 115 | u64 f_vco; |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 116 | u8 buf[5], i_data[4], q_data[4]; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 117 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 118 | if (!dev->active) { |
| 119 | dev_dbg(&client->dev, "tuner is sleeping\n"); |
| 120 | return 0; |
| 121 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 122 | |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 123 | /* gain control manual */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 124 | ret = regmap_write(dev->regmap, 0x1a, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 125 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 126 | goto err; |
| 127 | |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 128 | /* |
| 129 | * Fractional-N synthesizer |
| 130 | * |
| 131 | * +----------------------------+ |
| 132 | * v | |
| 133 | * Fref +----+ +-------+ +------+ +---+ |
| 134 | * ------> | PD | --> | VCO | ------> | /N.F | <-- | K | |
| 135 | * +----+ +-------+ +------+ +---+ |
| 136 | * | |
| 137 | * | |
| 138 | * v |
| 139 | * +-------+ Fout |
| 140 | * | /Rout | ------> |
| 141 | * +-------+ |
| 142 | */ |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 143 | for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 144 | if (dev->f_frequency <= e4000_pll_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 145 | break; |
| 146 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 147 | if (i == ARRAY_SIZE(e4000_pll_lut)) { |
| 148 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 149 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 150 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 151 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 152 | #define F_REF dev->clk |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 153 | div_out = e4000_pll_lut[i].div_out; |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 154 | f_vco = (u64) dev->f_frequency * div_out; |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 155 | /* calculate PLL integer and fractional control word */ |
| 156 | div_n = div_u64_rem(f_vco, F_REF, &k); |
| 157 | k_cw = div_u64((u64) k * 0x10000, F_REF); |
| 158 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 159 | dev_dbg(&client->dev, |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 160 | "frequency=%u bandwidth=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n", |
| 161 | dev->f_frequency, dev->f_bandwidth, f_vco, F_REF, div_n, k, |
| 162 | k_cw, div_out); |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 163 | |
| 164 | buf[0] = div_n; |
| 165 | buf[1] = (k_cw >> 0) & 0xff; |
| 166 | buf[2] = (k_cw >> 8) & 0xff; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 167 | buf[3] = 0x00; |
Antti Palosaari | 0e3a71c | 2015-04-21 21:18:45 -0300 | [diff] [blame] | 168 | buf[4] = e4000_pll_lut[i].div_out_reg; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 169 | ret = regmap_bulk_write(dev->regmap, 0x09, buf, 5); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 170 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 171 | goto err; |
| 172 | |
| 173 | /* LNA filter (RF filter) */ |
| 174 | for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 175 | if (dev->f_frequency <= e400_lna_filter_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 176 | break; |
| 177 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 178 | if (i == ARRAY_SIZE(e400_lna_filter_lut)) { |
| 179 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 180 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 181 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 182 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 183 | ret = regmap_write(dev->regmap, 0x10, e400_lna_filter_lut[i].val); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 184 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 185 | goto err; |
| 186 | |
| 187 | /* IF filters */ |
| 188 | for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 189 | if (dev->f_bandwidth <= e4000_if_filter_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 190 | break; |
| 191 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 192 | if (i == ARRAY_SIZE(e4000_if_filter_lut)) { |
| 193 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 194 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 195 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 196 | |
| 197 | buf[0] = e4000_if_filter_lut[i].reg11_val; |
| 198 | buf[1] = e4000_if_filter_lut[i].reg12_val; |
| 199 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 200 | ret = regmap_bulk_write(dev->regmap, 0x11, buf, 2); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 201 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 202 | goto err; |
| 203 | |
| 204 | /* frequency band */ |
| 205 | for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 206 | if (dev->f_frequency <= e4000_band_lut[i].freq) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 207 | break; |
| 208 | } |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 209 | if (i == ARRAY_SIZE(e4000_band_lut)) { |
| 210 | ret = -EINVAL; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 211 | goto err; |
Julia Lawall | 58f087c | 2013-12-29 19:47:35 -0300 | [diff] [blame] | 212 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 213 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 214 | ret = regmap_write(dev->regmap, 0x07, e4000_band_lut[i].reg07_val); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 215 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 216 | goto err; |
| 217 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 218 | ret = regmap_write(dev->regmap, 0x78, e4000_band_lut[i].reg78_val); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 219 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 220 | goto err; |
| 221 | |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 222 | /* DC offset */ |
| 223 | for (i = 0; i < 4; i++) { |
| 224 | if (i == 0) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 225 | ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7e\x24", 3); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 226 | else if (i == 1) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 227 | ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7f", 2); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 228 | else if (i == 2) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 229 | ret = regmap_bulk_write(dev->regmap, 0x15, "\x01", 1); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 230 | else |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 231 | ret = regmap_bulk_write(dev->regmap, 0x16, "\x7e", 1); |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 232 | |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 233 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 234 | goto err; |
| 235 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 236 | ret = regmap_write(dev->regmap, 0x29, 0x01); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 237 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 238 | goto err; |
| 239 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 240 | ret = regmap_bulk_read(dev->regmap, 0x2a, buf, 3); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 241 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 242 | goto err; |
| 243 | |
| 244 | i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f); |
| 245 | q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f); |
| 246 | } |
| 247 | |
Antti Palosaari | d4992da | 2013-07-24 18:33:51 -0300 | [diff] [blame] | 248 | swap(q_data[2], q_data[3]); |
| 249 | swap(i_data[2], i_data[3]); |
| 250 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 251 | ret = regmap_bulk_write(dev->regmap, 0x50, q_data, 4); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 252 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 253 | goto err; |
| 254 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 255 | ret = regmap_bulk_write(dev->regmap, 0x60, i_data, 4); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 256 | if (ret) |
Antti Palosaari | 8514611 | 2013-07-24 02:04:12 -0300 | [diff] [blame] | 257 | goto err; |
| 258 | |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 259 | /* gain control auto */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 260 | ret = regmap_write(dev->regmap, 0x1a, 0x17); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 261 | if (ret) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 262 | goto err; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 263 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 264 | return 0; |
| 265 | err: |
| 266 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 267 | return ret; |
| 268 | } |
| 269 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 270 | /* |
| 271 | * V4L2 API |
| 272 | */ |
| 273 | #if IS_ENABLED(CONFIG_VIDEO_V4L2) |
| 274 | static const struct v4l2_frequency_band bands[] = { |
| 275 | { |
| 276 | .type = V4L2_TUNER_RF, |
| 277 | .index = 0, |
| 278 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 279 | .rangelow = 59000000, |
| 280 | .rangehigh = 1105000000, |
| 281 | }, |
| 282 | { |
| 283 | .type = V4L2_TUNER_RF, |
| 284 | .index = 1, |
| 285 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 286 | .rangelow = 1249000000, |
Hans Verkuil | 1ba90492 | 2015-05-22 04:39:54 -0300 | [diff] [blame] | 287 | .rangehigh = 2208000000UL, |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 288 | }, |
| 289 | }; |
| 290 | |
| 291 | 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] | 292 | { |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 293 | return container_of(sd, struct e4000_dev, sd); |
| 294 | } |
| 295 | |
| 296 | static int e4000_s_power(struct v4l2_subdev *sd, int on) |
| 297 | { |
| 298 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
| 299 | struct i2c_client *client = dev->client; |
| 300 | int ret; |
| 301 | |
| 302 | dev_dbg(&client->dev, "on=%d\n", on); |
| 303 | |
| 304 | if (on) |
| 305 | ret = e4000_init(dev); |
| 306 | else |
| 307 | ret = e4000_sleep(dev); |
| 308 | if (ret) |
| 309 | return ret; |
| 310 | |
| 311 | return e4000_set_params(dev); |
| 312 | } |
| 313 | |
| 314 | static const struct v4l2_subdev_core_ops e4000_subdev_core_ops = { |
| 315 | .s_power = e4000_s_power, |
| 316 | }; |
| 317 | |
| 318 | static int e4000_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v) |
| 319 | { |
| 320 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 321 | struct i2c_client *client = dev->client; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 322 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 323 | dev_dbg(&client->dev, "index=%d\n", v->index); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 324 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 325 | strlcpy(v->name, "Elonics E4000", sizeof(v->name)); |
| 326 | v->type = V4L2_TUNER_RF; |
| 327 | v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
| 328 | v->rangelow = bands[0].rangelow; |
| 329 | v->rangehigh = bands[1].rangehigh; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 333 | static int e4000_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v) |
| 334 | { |
| 335 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
| 336 | struct i2c_client *client = dev->client; |
| 337 | |
| 338 | dev_dbg(&client->dev, "index=%d\n", v->index); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int e4000_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) |
| 343 | { |
| 344 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
| 345 | struct i2c_client *client = dev->client; |
| 346 | |
| 347 | dev_dbg(&client->dev, "tuner=%d\n", f->tuner); |
| 348 | f->frequency = dev->f_frequency; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static int e4000_s_frequency(struct v4l2_subdev *sd, |
| 353 | const struct v4l2_frequency *f) |
| 354 | { |
| 355 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
| 356 | struct i2c_client *client = dev->client; |
| 357 | |
| 358 | dev_dbg(&client->dev, "tuner=%d type=%d frequency=%u\n", |
| 359 | f->tuner, f->type, f->frequency); |
| 360 | |
| 361 | dev->f_frequency = clamp_t(unsigned int, f->frequency, |
| 362 | bands[0].rangelow, bands[1].rangehigh); |
| 363 | return e4000_set_params(dev); |
| 364 | } |
| 365 | |
| 366 | static int e4000_enum_freq_bands(struct v4l2_subdev *sd, |
| 367 | struct v4l2_frequency_band *band) |
| 368 | { |
| 369 | struct e4000_dev *dev = e4000_subdev_to_dev(sd); |
| 370 | struct i2c_client *client = dev->client; |
| 371 | |
| 372 | dev_dbg(&client->dev, "tuner=%d type=%d index=%d\n", |
| 373 | band->tuner, band->type, band->index); |
| 374 | |
| 375 | if (band->index >= ARRAY_SIZE(bands)) |
| 376 | return -EINVAL; |
| 377 | |
| 378 | band->capability = bands[band->index].capability; |
| 379 | band->rangelow = bands[band->index].rangelow; |
| 380 | band->rangehigh = bands[band->index].rangehigh; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static const struct v4l2_subdev_tuner_ops e4000_subdev_tuner_ops = { |
| 385 | .g_tuner = e4000_g_tuner, |
| 386 | .s_tuner = e4000_s_tuner, |
| 387 | .g_frequency = e4000_g_frequency, |
| 388 | .s_frequency = e4000_s_frequency, |
| 389 | .enum_freq_bands = e4000_enum_freq_bands, |
| 390 | }; |
| 391 | |
| 392 | static const struct v4l2_subdev_ops e4000_subdev_ops = { |
| 393 | .core = &e4000_subdev_core_ops, |
| 394 | .tuner = &e4000_subdev_tuner_ops, |
| 395 | }; |
| 396 | |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 397 | static int e4000_set_lna_gain(struct dvb_frontend *fe) |
| 398 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 399 | struct e4000_dev *dev = fe->tuner_priv; |
| 400 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 401 | int ret; |
| 402 | u8 u8tmp; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 403 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 404 | dev_dbg(&client->dev, "lna auto=%d->%d val=%d->%d\n", |
| 405 | dev->lna_gain_auto->cur.val, dev->lna_gain_auto->val, |
| 406 | dev->lna_gain->cur.val, dev->lna_gain->val); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 407 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 408 | if (dev->lna_gain_auto->val && dev->if_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 409 | u8tmp = 0x17; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 410 | else if (dev->lna_gain_auto->val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 411 | u8tmp = 0x19; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 412 | else if (dev->if_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 413 | u8tmp = 0x16; |
| 414 | else |
| 415 | u8tmp = 0x10; |
| 416 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 417 | ret = regmap_write(dev->regmap, 0x1a, u8tmp); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 418 | if (ret) |
| 419 | goto err; |
| 420 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 421 | if (dev->lna_gain_auto->val == false) { |
| 422 | ret = regmap_write(dev->regmap, 0x14, dev->lna_gain->val); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 423 | if (ret) |
| 424 | goto err; |
| 425 | } |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 426 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 427 | return 0; |
| 428 | err: |
| 429 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | static int e4000_set_mixer_gain(struct dvb_frontend *fe) |
| 434 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 435 | struct e4000_dev *dev = fe->tuner_priv; |
| 436 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 437 | int ret; |
| 438 | u8 u8tmp; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 439 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 440 | dev_dbg(&client->dev, "mixer auto=%d->%d val=%d->%d\n", |
| 441 | dev->mixer_gain_auto->cur.val, dev->mixer_gain_auto->val, |
| 442 | dev->mixer_gain->cur.val, dev->mixer_gain->val); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 443 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 444 | if (dev->mixer_gain_auto->val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 445 | u8tmp = 0x15; |
| 446 | else |
| 447 | u8tmp = 0x14; |
| 448 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 449 | ret = regmap_write(dev->regmap, 0x20, u8tmp); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 450 | if (ret) |
| 451 | goto err; |
| 452 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 453 | if (dev->mixer_gain_auto->val == false) { |
| 454 | ret = regmap_write(dev->regmap, 0x15, dev->mixer_gain->val); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 455 | if (ret) |
| 456 | goto err; |
| 457 | } |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 458 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 459 | return 0; |
| 460 | err: |
| 461 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | static int e4000_set_if_gain(struct dvb_frontend *fe) |
| 466 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 467 | struct e4000_dev *dev = fe->tuner_priv; |
| 468 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 469 | int ret; |
| 470 | u8 buf[2]; |
| 471 | u8 u8tmp; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 472 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 473 | dev_dbg(&client->dev, "if auto=%d->%d val=%d->%d\n", |
| 474 | dev->if_gain_auto->cur.val, dev->if_gain_auto->val, |
| 475 | dev->if_gain->cur.val, dev->if_gain->val); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 476 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 477 | if (dev->if_gain_auto->val && dev->lna_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 478 | u8tmp = 0x17; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 479 | else if (dev->lna_gain_auto->cur.val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 480 | u8tmp = 0x19; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 481 | else if (dev->if_gain_auto->val) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 482 | u8tmp = 0x16; |
| 483 | else |
| 484 | u8tmp = 0x10; |
| 485 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 486 | ret = regmap_write(dev->regmap, 0x1a, u8tmp); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 487 | if (ret) |
| 488 | goto err; |
| 489 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 490 | if (dev->if_gain_auto->val == false) { |
| 491 | buf[0] = e4000_if_gain_lut[dev->if_gain->val].reg16_val; |
| 492 | buf[1] = e4000_if_gain_lut[dev->if_gain->val].reg17_val; |
| 493 | ret = regmap_bulk_write(dev->regmap, 0x16, buf, 2); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 494 | if (ret) |
| 495 | goto err; |
| 496 | } |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 497 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 498 | return 0; |
| 499 | err: |
| 500 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 501 | return ret; |
| 502 | } |
| 503 | |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 504 | static int e4000_pll_lock(struct dvb_frontend *fe) |
| 505 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 506 | struct e4000_dev *dev = fe->tuner_priv; |
| 507 | struct i2c_client *client = dev->client; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 508 | int ret; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 509 | unsigned int uitmp; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 510 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 511 | ret = regmap_read(dev->regmap, 0x07, &uitmp); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 512 | if (ret) |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 513 | goto err; |
| 514 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 515 | dev->pll_lock->val = (uitmp & 0x01); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 516 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 517 | return 0; |
| 518 | err: |
| 519 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl) |
| 524 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 525 | struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl); |
| 526 | struct i2c_client *client = dev->client; |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 527 | int ret; |
| 528 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 529 | if (!dev->active) |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 530 | return 0; |
| 531 | |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 532 | switch (ctrl->id) { |
| 533 | case V4L2_CID_RF_TUNER_PLL_LOCK: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 534 | ret = e4000_pll_lock(dev->fe); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 535 | break; |
| 536 | default: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 537 | dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n", |
| 538 | ctrl->id, ctrl->name); |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 539 | ret = -EINVAL; |
| 540 | } |
| 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 545 | static int e4000_s_ctrl(struct v4l2_ctrl *ctrl) |
| 546 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 547 | struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl); |
| 548 | struct i2c_client *client = dev->client; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 549 | int ret; |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 550 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 551 | if (!dev->active) |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 552 | return 0; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 553 | |
| 554 | switch (ctrl->id) { |
| 555 | case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: |
| 556 | case V4L2_CID_RF_TUNER_BANDWIDTH: |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 557 | /* |
| 558 | * TODO: Auto logic does not work 100% correctly as tuner driver |
| 559 | * do not have information to calculate maximum suitable |
| 560 | * bandwidth. Calculating it is responsible of master driver. |
| 561 | */ |
| 562 | dev->f_bandwidth = dev->bandwidth->val; |
| 563 | ret = e4000_set_params(dev); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 564 | break; |
| 565 | case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO: |
| 566 | case V4L2_CID_RF_TUNER_LNA_GAIN: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 567 | ret = e4000_set_lna_gain(dev->fe); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 568 | break; |
| 569 | case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO: |
| 570 | case V4L2_CID_RF_TUNER_MIXER_GAIN: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 571 | ret = e4000_set_mixer_gain(dev->fe); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 572 | break; |
| 573 | case V4L2_CID_RF_TUNER_IF_GAIN_AUTO: |
| 574 | case V4L2_CID_RF_TUNER_IF_GAIN: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 575 | ret = e4000_set_if_gain(dev->fe); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 576 | break; |
| 577 | default: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 578 | dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n", |
| 579 | ctrl->id, ctrl->name); |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 580 | ret = -EINVAL; |
| 581 | } |
| 582 | |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static const struct v4l2_ctrl_ops e4000_ctrl_ops = { |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 587 | .g_volatile_ctrl = e4000_g_volatile_ctrl, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 588 | .s_ctrl = e4000_s_ctrl, |
| 589 | }; |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 590 | #endif |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 591 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 592 | /* |
| 593 | * DVB API |
| 594 | */ |
| 595 | static int e4000_dvb_set_params(struct dvb_frontend *fe) |
| 596 | { |
| 597 | struct e4000_dev *dev = fe->tuner_priv; |
| 598 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 599 | |
| 600 | dev->f_frequency = c->frequency; |
| 601 | dev->f_bandwidth = c->bandwidth_hz; |
| 602 | return e4000_set_params(dev); |
| 603 | } |
| 604 | |
| 605 | static int e4000_dvb_init(struct dvb_frontend *fe) |
| 606 | { |
| 607 | return e4000_init(fe->tuner_priv); |
| 608 | } |
| 609 | |
| 610 | static int e4000_dvb_sleep(struct dvb_frontend *fe) |
| 611 | { |
| 612 | return e4000_sleep(fe->tuner_priv); |
| 613 | } |
| 614 | |
| 615 | static int e4000_dvb_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 616 | { |
| 617 | *frequency = 0; /* Zero-IF */ |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static const struct dvb_tuner_ops e4000_dvb_tuner_ops = { |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 622 | .info = { |
| 623 | .name = "Elonics E4000", |
| 624 | .frequency_min = 174000000, |
| 625 | .frequency_max = 862000000, |
| 626 | }, |
| 627 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 628 | .init = e4000_dvb_init, |
| 629 | .sleep = e4000_dvb_sleep, |
| 630 | .set_params = e4000_dvb_set_params, |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 631 | |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 632 | .get_if_frequency = e4000_dvb_get_if_frequency, |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 633 | }; |
| 634 | |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 635 | static int e4000_probe(struct i2c_client *client, |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 636 | const struct i2c_device_id *id) |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 637 | { |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 638 | struct e4000_dev *dev; |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 639 | struct e4000_config *cfg = client->dev.platform_data; |
| 640 | struct dvb_frontend *fe = cfg->fe; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 641 | int ret; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 642 | unsigned int uitmp; |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 643 | static const struct regmap_config regmap_config = { |
| 644 | .reg_bits = 8, |
| 645 | .val_bits = 8, |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 646 | }; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 647 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 648 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 649 | if (!dev) { |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 650 | ret = -ENOMEM; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 651 | goto err; |
| 652 | } |
| 653 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 654 | dev->clk = cfg->clock; |
| 655 | dev->client = client; |
| 656 | dev->fe = cfg->fe; |
| 657 | dev->regmap = devm_regmap_init_i2c(client, ®map_config); |
| 658 | if (IS_ERR(dev->regmap)) { |
| 659 | ret = PTR_ERR(dev->regmap); |
| 660 | goto err_kfree; |
Antti Palosaari | bd428bb | 2014-02-08 06:20:35 -0300 | [diff] [blame] | 661 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 662 | |
| 663 | /* check if the tuner is there */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 664 | ret = regmap_read(dev->regmap, 0x02, &uitmp); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 665 | if (ret) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 666 | goto err_kfree; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 667 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 668 | dev_dbg(&client->dev, "chip id=%02x\n", uitmp); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 669 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 670 | if (uitmp != 0x40) { |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 671 | ret = -ENODEV; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 672 | goto err_kfree; |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 673 | } |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 674 | |
| 675 | /* put sleep as chip seems to be in normal mode by default */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 676 | ret = regmap_write(dev->regmap, 0x00, 0x00); |
Antti Palosaari | c5f51b1 | 2014-02-10 22:52:51 -0300 | [diff] [blame] | 677 | if (ret) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 678 | goto err_kfree; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 679 | |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 680 | #if IS_ENABLED(CONFIG_VIDEO_V4L2) |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 681 | /* Register controls */ |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 682 | v4l2_ctrl_handler_init(&dev->hdl, 9); |
| 683 | dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 684 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 685 | dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 686 | V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 687 | v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); |
| 688 | 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] | 689 | V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 690 | dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 691 | V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 692 | v4l2_ctrl_auto_cluster(2, &dev->lna_gain_auto, 0, false); |
| 693 | 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] | 694 | V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 695 | dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 696 | V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 697 | v4l2_ctrl_auto_cluster(2, &dev->mixer_gain_auto, 0, false); |
| 698 | 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] | 699 | V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 700 | dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 701 | V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 702 | v4l2_ctrl_auto_cluster(2, &dev->if_gain_auto, 0, false); |
| 703 | dev->pll_lock = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops, |
Antti Palosaari | ecfb7ca | 2014-02-07 02:55:57 -0300 | [diff] [blame] | 704 | V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 705 | if (dev->hdl.error) { |
| 706 | ret = dev->hdl.error; |
| 707 | dev_err(&client->dev, "Could not initialize controls\n"); |
| 708 | v4l2_ctrl_handler_free(&dev->hdl); |
| 709 | goto err_kfree; |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 710 | } |
| 711 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 712 | dev->sd.ctrl_handler = &dev->hdl; |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 713 | dev->f_frequency = bands[0].rangelow; |
| 714 | dev->f_bandwidth = dev->bandwidth->val; |
| 715 | v4l2_i2c_subdev_init(&dev->sd, client, &e4000_subdev_ops); |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 716 | #endif |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 717 | fe->tuner_priv = dev; |
Antti Palosaari | c7861bb | 2015-05-12 14:26:07 -0300 | [diff] [blame] | 718 | memcpy(&fe->ops.tuner_ops, &e4000_dvb_tuner_ops, |
| 719 | sizeof(fe->ops.tuner_ops)); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 720 | v4l2_set_subdevdata(&dev->sd, client); |
| 721 | i2c_set_clientdata(client, &dev->sd); |
Antti Palosaari | 36f647b | 2012-09-22 12:32:27 -0300 | [diff] [blame] | 722 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 723 | dev_info(&client->dev, "Elonics E4000 successfully identified\n"); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 724 | return 0; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 725 | err_kfree: |
| 726 | kfree(dev); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 727 | err: |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 728 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 729 | return ret; |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 730 | } |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 731 | |
| 732 | static int e4000_remove(struct i2c_client *client) |
| 733 | { |
Antti Palosaari | adaa616 | 2014-01-26 21:02:53 -0300 | [diff] [blame] | 734 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 735 | struct e4000_dev *dev = container_of(sd, struct e4000_dev, sd); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 736 | |
Antti Palosaari | 13bd82d | 2014-08-24 23:35:48 -0300 | [diff] [blame] | 737 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 1c73fc6 | 2014-02-08 04:21:10 -0300 | [diff] [blame] | 738 | |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 739 | #if IS_ENABLED(CONFIG_VIDEO_V4L2) |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 740 | v4l2_ctrl_handler_free(&dev->hdl); |
Antti Palosaari | 320c638 | 2014-03-16 18:13:05 -0300 | [diff] [blame] | 741 | #endif |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 742 | kfree(dev); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 747 | static const struct i2c_device_id e4000_id_table[] = { |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 748 | {"e4000", 0}, |
| 749 | {} |
| 750 | }; |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 751 | MODULE_DEVICE_TABLE(i2c, e4000_id_table); |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 752 | |
| 753 | static struct i2c_driver e4000_driver = { |
| 754 | .driver = { |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 755 | .name = "e4000", |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 756 | .suppress_bind_attrs = true, |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 757 | }, |
| 758 | .probe = e4000_probe, |
| 759 | .remove = e4000_remove, |
Antti Palosaari | f8b9b87 | 2015-04-21 17:13:39 -0300 | [diff] [blame] | 760 | .id_table = e4000_id_table, |
Antti Palosaari | 28fd31f | 2013-10-15 19:22:45 -0300 | [diff] [blame] | 761 | }; |
| 762 | |
| 763 | module_i2c_driver(e4000_driver); |
Antti Palosaari | ed85ada | 2012-09-01 21:09:21 -0300 | [diff] [blame] | 764 | |
| 765 | MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver"); |
| 766 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 767 | MODULE_LICENSE("GPL"); |