Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Afatech AF9033 demodulator driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> |
| 5 | * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "af9033_priv.h" |
| 23 | |
Mauro Carvalho Chehab | 37ebaf6 | 2013-11-02 05:11:47 -0300 | [diff] [blame] | 24 | /* Max transfer size done by I2C transfer functions */ |
| 25 | #define MAX_XFER_SIZE 64 |
| 26 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 27 | struct af9033_dev { |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 28 | struct i2c_client *client; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 29 | struct dvb_frontend fe; |
| 30 | struct af9033_config cfg; |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 31 | bool is_af9035; |
| 32 | bool is_it9135; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 33 | |
| 34 | u32 bandwidth_hz; |
| 35 | bool ts_mode_parallel; |
| 36 | bool ts_mode_serial; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 37 | |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 38 | fe_status_t fe_status; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 39 | u32 ber; |
| 40 | u32 ucb; |
Antti Palosaari | 204f431 | 2014-09-02 08:01:10 -0300 | [diff] [blame^] | 41 | u64 error_block_count; |
| 42 | u64 total_block_count; |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 43 | struct delayed_work stat_work; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 44 | unsigned long last_stat_check; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* write multiple registers */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 48 | static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 49 | int len) |
| 50 | { |
| 51 | int ret; |
Mauro Carvalho Chehab | 37ebaf6 | 2013-11-02 05:11:47 -0300 | [diff] [blame] | 52 | u8 buf[MAX_XFER_SIZE]; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 53 | struct i2c_msg msg[1] = { |
| 54 | { |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 55 | .addr = dev->client->addr, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 56 | .flags = 0, |
Mauro Carvalho Chehab | 37ebaf6 | 2013-11-02 05:11:47 -0300 | [diff] [blame] | 57 | .len = 3 + len, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 58 | .buf = buf, |
| 59 | } |
| 60 | }; |
| 61 | |
Mauro Carvalho Chehab | 37ebaf6 | 2013-11-02 05:11:47 -0300 | [diff] [blame] | 62 | if (3 + len > sizeof(buf)) { |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 63 | dev_warn(&dev->client->dev, |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 64 | "i2c wr reg=%04x: len=%d is too big!\n", |
| 65 | reg, len); |
Mauro Carvalho Chehab | 37ebaf6 | 2013-11-02 05:11:47 -0300 | [diff] [blame] | 66 | return -EINVAL; |
| 67 | } |
| 68 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 69 | buf[0] = (reg >> 16) & 0xff; |
| 70 | buf[1] = (reg >> 8) & 0xff; |
| 71 | buf[2] = (reg >> 0) & 0xff; |
| 72 | memcpy(&buf[3], val, len); |
| 73 | |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 74 | ret = i2c_transfer(dev->client->adapter, msg, 1); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 75 | if (ret == 1) { |
| 76 | ret = 0; |
| 77 | } else { |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 78 | dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n", |
| 79 | ret, reg, len); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 80 | ret = -EREMOTEIO; |
| 81 | } |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | /* read multiple registers */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 87 | static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 88 | { |
| 89 | int ret; |
| 90 | u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff, |
| 91 | (reg >> 0) & 0xff }; |
| 92 | struct i2c_msg msg[2] = { |
| 93 | { |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 94 | .addr = dev->client->addr, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 95 | .flags = 0, |
| 96 | .len = sizeof(buf), |
| 97 | .buf = buf |
| 98 | }, { |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 99 | .addr = dev->client->addr, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 100 | .flags = I2C_M_RD, |
| 101 | .len = len, |
| 102 | .buf = val |
| 103 | } |
| 104 | }; |
| 105 | |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 106 | ret = i2c_transfer(dev->client->adapter, msg, 2); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 107 | if (ret == 2) { |
| 108 | ret = 0; |
| 109 | } else { |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 110 | dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n", |
| 111 | ret, reg, len); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 112 | ret = -EREMOTEIO; |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /* write single register */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 120 | static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 121 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 122 | return af9033_wr_regs(dev, reg, &val, 1); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* read single register */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 126 | static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 127 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 128 | return af9033_rd_regs(dev, reg, val, 1); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* write single register with mask */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 132 | static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 133 | u8 mask) |
| 134 | { |
| 135 | int ret; |
| 136 | u8 tmp; |
| 137 | |
| 138 | /* no need for read if whole reg is written */ |
| 139 | if (mask != 0xff) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 140 | ret = af9033_rd_regs(dev, reg, &tmp, 1); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | val &= mask; |
| 145 | tmp &= ~mask; |
| 146 | val |= tmp; |
| 147 | } |
| 148 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 149 | return af9033_wr_regs(dev, reg, &val, 1); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* read single register with mask */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 153 | static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 154 | u8 mask) |
| 155 | { |
| 156 | int ret, i; |
| 157 | u8 tmp; |
| 158 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 159 | ret = af9033_rd_regs(dev, reg, &tmp, 1); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 160 | if (ret) |
| 161 | return ret; |
| 162 | |
| 163 | tmp &= mask; |
| 164 | |
| 165 | /* find position of the first bit */ |
| 166 | for (i = 0; i < 8; i++) { |
| 167 | if ((mask >> i) & 0x01) |
| 168 | break; |
| 169 | } |
| 170 | *val = tmp >> i; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 175 | /* write reg val table using reg addr auto increment */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 176 | static int af9033_wr_reg_val_tab(struct af9033_dev *dev, |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 177 | const struct reg_val *tab, int tab_len) |
| 178 | { |
Antti Palosaari | d18a88b | 2013-11-27 17:17:43 -0300 | [diff] [blame] | 179 | #define MAX_TAB_LEN 212 |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 180 | int ret, i, j; |
Antti Palosaari | d18a88b | 2013-11-27 17:17:43 -0300 | [diff] [blame] | 181 | u8 buf[1 + MAX_TAB_LEN]; |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 182 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 183 | dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len); |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 184 | |
Antti Palosaari | d18a88b | 2013-11-27 17:17:43 -0300 | [diff] [blame] | 185 | if (tab_len > sizeof(buf)) { |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 186 | dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len); |
Antti Palosaari | d18a88b | 2013-11-27 17:17:43 -0300 | [diff] [blame] | 187 | return -EINVAL; |
| 188 | } |
| 189 | |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 190 | for (i = 0, j = 0; i < tab_len; i++) { |
| 191 | buf[j] = tab[i].val; |
| 192 | |
| 193 | if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 194 | ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1); |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 195 | if (ret < 0) |
| 196 | goto err; |
| 197 | |
| 198 | j = 0; |
| 199 | } else { |
| 200 | j++; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | |
| 206 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 207 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 212 | static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 213 | { |
| 214 | u32 r = 0, c = 0, i; |
| 215 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 216 | dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 217 | |
| 218 | if (a > b) { |
| 219 | c = a / b; |
| 220 | a = a - c * b; |
| 221 | } |
| 222 | |
| 223 | for (i = 0; i < x; i++) { |
| 224 | if (a >= b) { |
| 225 | r += 1; |
| 226 | a -= b; |
| 227 | } |
| 228 | a <<= 1; |
| 229 | r <<= 1; |
| 230 | } |
| 231 | r = (c << (u32)x) + r; |
| 232 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 233 | dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 234 | |
| 235 | return r; |
| 236 | } |
| 237 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 238 | static int af9033_init(struct dvb_frontend *fe) |
| 239 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 240 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 241 | int ret, i, len; |
| 242 | const struct reg_val *init; |
| 243 | u8 buf[4]; |
| 244 | u32 adc_cw, clock_cw; |
| 245 | struct reg_val_mask tab[] = { |
| 246 | { 0x80fb24, 0x00, 0x08 }, |
| 247 | { 0x80004c, 0x00, 0xff }, |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 248 | { 0x00f641, dev->cfg.tuner, 0xff }, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 249 | { 0x80f5ca, 0x01, 0x01 }, |
| 250 | { 0x80f715, 0x01, 0x01 }, |
| 251 | { 0x00f41f, 0x04, 0x04 }, |
| 252 | { 0x00f41a, 0x01, 0x01 }, |
| 253 | { 0x80f731, 0x00, 0x01 }, |
| 254 | { 0x00d91e, 0x00, 0x01 }, |
| 255 | { 0x00d919, 0x00, 0x01 }, |
| 256 | { 0x80f732, 0x00, 0x01 }, |
| 257 | { 0x00d91f, 0x00, 0x01 }, |
| 258 | { 0x00d91a, 0x00, 0x01 }, |
| 259 | { 0x80f730, 0x00, 0x01 }, |
| 260 | { 0x80f778, 0x00, 0xff }, |
| 261 | { 0x80f73c, 0x01, 0x01 }, |
| 262 | { 0x80f776, 0x00, 0x01 }, |
| 263 | { 0x00d8fd, 0x01, 0xff }, |
| 264 | { 0x00d830, 0x01, 0xff }, |
| 265 | { 0x00d831, 0x00, 0xff }, |
| 266 | { 0x00d832, 0x00, 0xff }, |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 267 | { 0x80f985, dev->ts_mode_serial, 0x01 }, |
| 268 | { 0x80f986, dev->ts_mode_parallel, 0x01 }, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 269 | { 0x00d827, 0x00, 0xff }, |
| 270 | { 0x00d829, 0x00, 0xff }, |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 271 | { 0x800045, dev->cfg.adc_multiplier, 0xff }, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | /* program clock control */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 275 | clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 276 | buf[0] = (clock_cw >> 0) & 0xff; |
| 277 | buf[1] = (clock_cw >> 8) & 0xff; |
| 278 | buf[2] = (clock_cw >> 16) & 0xff; |
| 279 | buf[3] = (clock_cw >> 24) & 0xff; |
| 280 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 281 | dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n", |
| 282 | dev->cfg.clock, clock_cw); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 283 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 284 | ret = af9033_wr_regs(dev, 0x800025, buf, 4); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 285 | if (ret < 0) |
| 286 | goto err; |
| 287 | |
| 288 | /* program ADC control */ |
| 289 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 290 | if (clock_adc_lut[i].clock == dev->cfg.clock) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 294 | adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 295 | buf[0] = (adc_cw >> 0) & 0xff; |
| 296 | buf[1] = (adc_cw >> 8) & 0xff; |
| 297 | buf[2] = (adc_cw >> 16) & 0xff; |
| 298 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 299 | dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n", |
| 300 | clock_adc_lut[i].adc, adc_cw); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 301 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 302 | ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 303 | if (ret < 0) |
| 304 | goto err; |
| 305 | |
| 306 | /* program register table */ |
| 307 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 308 | ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 309 | tab[i].mask); |
| 310 | if (ret < 0) |
| 311 | goto err; |
| 312 | } |
| 313 | |
Antti Palosaari | ca681fe | 2014-08-08 23:56:49 -0300 | [diff] [blame] | 314 | /* clock output */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 315 | if (dev->cfg.dyn0_clk) { |
| 316 | ret = af9033_wr_reg(dev, 0x80fba8, 0x00); |
Antti Palosaari | 9dc0f3f | 2014-08-09 01:03:14 -0300 | [diff] [blame] | 317 | if (ret < 0) |
| 318 | goto err; |
| 319 | } |
| 320 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 321 | /* settings for TS interface */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 322 | if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) { |
| 323 | ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 324 | if (ret < 0) |
| 325 | goto err; |
| 326 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 327 | ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 328 | if (ret < 0) |
| 329 | goto err; |
| 330 | } else { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 331 | ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 332 | if (ret < 0) |
| 333 | goto err; |
| 334 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 335 | ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 336 | if (ret < 0) |
| 337 | goto err; |
| 338 | } |
| 339 | |
| 340 | /* load OFSM settings */ |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 341 | dev_dbg(&dev->client->dev, "load ofsm settings\n"); |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 342 | switch (dev->cfg.tuner) { |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 343 | case AF9033_TUNER_IT9135_38: |
| 344 | case AF9033_TUNER_IT9135_51: |
| 345 | case AF9033_TUNER_IT9135_52: |
Antti Palosaari | 463c399 | 2013-03-07 00:03:06 -0300 | [diff] [blame] | 346 | len = ARRAY_SIZE(ofsm_init_it9135_v1); |
| 347 | init = ofsm_init_it9135_v1; |
| 348 | break; |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 349 | case AF9033_TUNER_IT9135_60: |
| 350 | case AF9033_TUNER_IT9135_61: |
| 351 | case AF9033_TUNER_IT9135_62: |
Antti Palosaari | 463c399 | 2013-03-07 00:03:06 -0300 | [diff] [blame] | 352 | len = ARRAY_SIZE(ofsm_init_it9135_v2); |
| 353 | init = ofsm_init_it9135_v2; |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 354 | break; |
| 355 | default: |
| 356 | len = ARRAY_SIZE(ofsm_init); |
| 357 | init = ofsm_init; |
| 358 | break; |
| 359 | } |
| 360 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 361 | ret = af9033_wr_reg_val_tab(dev, init, len); |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 362 | if (ret < 0) |
| 363 | goto err; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 364 | |
| 365 | /* load tuner specific settings */ |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 366 | dev_dbg(&dev->client->dev, "load tuner specific settings\n"); |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 367 | switch (dev->cfg.tuner) { |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 368 | case AF9033_TUNER_TUA9001: |
| 369 | len = ARRAY_SIZE(tuner_init_tua9001); |
| 370 | init = tuner_init_tua9001; |
| 371 | break; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 372 | case AF9033_TUNER_FC0011: |
| 373 | len = ARRAY_SIZE(tuner_init_fc0011); |
| 374 | init = tuner_init_fc0011; |
| 375 | break; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 376 | case AF9033_TUNER_MXL5007T: |
| 377 | len = ARRAY_SIZE(tuner_init_mxl5007t); |
| 378 | init = tuner_init_mxl5007t; |
| 379 | break; |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 380 | case AF9033_TUNER_TDA18218: |
| 381 | len = ARRAY_SIZE(tuner_init_tda18218); |
| 382 | init = tuner_init_tda18218; |
| 383 | break; |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame] | 384 | case AF9033_TUNER_FC2580: |
| 385 | len = ARRAY_SIZE(tuner_init_fc2580); |
| 386 | init = tuner_init_fc2580; |
| 387 | break; |
Antti Palosaari | e713ad1 | 2012-12-02 18:47:00 -0300 | [diff] [blame] | 388 | case AF9033_TUNER_FC0012: |
| 389 | len = ARRAY_SIZE(tuner_init_fc0012); |
| 390 | init = tuner_init_fc0012; |
| 391 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 392 | case AF9033_TUNER_IT9135_38: |
Antti Palosaari | a72cbb7 | 2013-03-07 21:17:55 -0300 | [diff] [blame] | 393 | len = ARRAY_SIZE(tuner_init_it9135_38); |
| 394 | init = tuner_init_it9135_38; |
| 395 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 396 | case AF9033_TUNER_IT9135_51: |
Antti Palosaari | bb2e12a | 2013-03-07 21:25:16 -0300 | [diff] [blame] | 397 | len = ARRAY_SIZE(tuner_init_it9135_51); |
| 398 | init = tuner_init_it9135_51; |
| 399 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 400 | case AF9033_TUNER_IT9135_52: |
Antti Palosaari | 22d729f | 2013-03-07 21:29:05 -0300 | [diff] [blame] | 401 | len = ARRAY_SIZE(tuner_init_it9135_52); |
| 402 | init = tuner_init_it9135_52; |
| 403 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 404 | case AF9033_TUNER_IT9135_60: |
Antti Palosaari | a49f53a | 2013-03-07 21:38:00 -0300 | [diff] [blame] | 405 | len = ARRAY_SIZE(tuner_init_it9135_60); |
| 406 | init = tuner_init_it9135_60; |
| 407 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 408 | case AF9033_TUNER_IT9135_61: |
Antti Palosaari | 8521132 | 2013-03-07 21:44:30 -0300 | [diff] [blame] | 409 | len = ARRAY_SIZE(tuner_init_it9135_61); |
| 410 | init = tuner_init_it9135_61; |
| 411 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 412 | case AF9033_TUNER_IT9135_62: |
Antti Palosaari | dc4a2c4 | 2013-03-07 21:47:20 -0300 | [diff] [blame] | 413 | len = ARRAY_SIZE(tuner_init_it9135_62); |
| 414 | init = tuner_init_it9135_62; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 415 | break; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 416 | default: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 417 | dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n", |
| 418 | dev->cfg.tuner); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 419 | ret = -ENODEV; |
| 420 | goto err; |
| 421 | } |
| 422 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 423 | ret = af9033_wr_reg_val_tab(dev, init, len); |
Antti Palosaari | 3bf5e55 | 2013-03-08 16:54:09 -0300 | [diff] [blame] | 424 | if (ret < 0) |
| 425 | goto err; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 426 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 427 | if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
| 428 | ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01); |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 429 | if (ret < 0) |
| 430 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 431 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 432 | ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 433 | if (ret < 0) |
| 434 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 435 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 436 | ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01); |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 437 | if (ret < 0) |
| 438 | goto err; |
| 439 | } |
| 440 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 441 | switch (dev->cfg.tuner) { |
Antti Palosaari | 086991d | 2013-03-07 22:18:56 -0300 | [diff] [blame] | 442 | case AF9033_TUNER_IT9135_60: |
| 443 | case AF9033_TUNER_IT9135_61: |
| 444 | case AF9033_TUNER_IT9135_62: |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 445 | ret = af9033_wr_reg(dev, 0x800000, 0x01); |
Antti Palosaari | 086991d | 2013-03-07 22:18:56 -0300 | [diff] [blame] | 446 | if (ret < 0) |
| 447 | goto err; |
| 448 | } |
| 449 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 450 | dev->bandwidth_hz = 0; /* force to program all parameters */ |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 451 | /* start statistics polling */ |
| 452 | schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000)); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 453 | |
| 454 | return 0; |
| 455 | |
| 456 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 457 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 458 | |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | static int af9033_sleep(struct dvb_frontend *fe) |
| 463 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 464 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 465 | int ret, i; |
| 466 | u8 tmp; |
| 467 | |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 468 | /* stop statistics polling */ |
| 469 | cancel_delayed_work_sync(&dev->stat_work); |
| 470 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 471 | ret = af9033_wr_reg(dev, 0x80004c, 1); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 472 | if (ret < 0) |
| 473 | goto err; |
| 474 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 475 | ret = af9033_wr_reg(dev, 0x800000, 0); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 476 | if (ret < 0) |
| 477 | goto err; |
| 478 | |
| 479 | for (i = 100, tmp = 1; i && tmp; i--) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 480 | ret = af9033_rd_reg(dev, 0x80004c, &tmp); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 481 | if (ret < 0) |
| 482 | goto err; |
| 483 | |
| 484 | usleep_range(200, 10000); |
| 485 | } |
| 486 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 487 | dev_dbg(&dev->client->dev, "loop=%d\n", i); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 488 | |
| 489 | if (i == 0) { |
| 490 | ret = -ETIMEDOUT; |
| 491 | goto err; |
| 492 | } |
| 493 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 494 | ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 495 | if (ret < 0) |
| 496 | goto err; |
| 497 | |
| 498 | /* prevent current leak (?) */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 499 | if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 500 | /* enable parallel TS */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 501 | ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 502 | if (ret < 0) |
| 503 | goto err; |
| 504 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 505 | ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 506 | if (ret < 0) |
| 507 | goto err; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | |
| 512 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 513 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | static int af9033_get_tune_settings(struct dvb_frontend *fe, |
| 519 | struct dvb_frontend_tune_settings *fesettings) |
| 520 | { |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 521 | /* 800 => 2000 because IT9135 v2 is slow to gain lock */ |
| 522 | fesettings->min_delay_ms = 2000; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 523 | fesettings->step_size = 0; |
| 524 | fesettings->max_drift = 0; |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int af9033_set_frontend(struct dvb_frontend *fe) |
| 530 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 531 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 532 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 533 | int ret, i, spec_inv, sampling_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 534 | u8 tmp, buf[3], bandwidth_reg_val; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 535 | u32 if_frequency, freq_cw, adc_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 536 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 537 | dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n", |
| 538 | c->frequency, c->bandwidth_hz); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 539 | |
| 540 | /* check bandwidth */ |
| 541 | switch (c->bandwidth_hz) { |
| 542 | case 6000000: |
| 543 | bandwidth_reg_val = 0x00; |
| 544 | break; |
| 545 | case 7000000: |
| 546 | bandwidth_reg_val = 0x01; |
| 547 | break; |
| 548 | case 8000000: |
| 549 | bandwidth_reg_val = 0x02; |
| 550 | break; |
| 551 | default: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 552 | dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n"); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 553 | ret = -EINVAL; |
| 554 | goto err; |
| 555 | } |
| 556 | |
| 557 | /* program tuner */ |
| 558 | if (fe->ops.tuner_ops.set_params) |
| 559 | fe->ops.tuner_ops.set_params(fe); |
| 560 | |
| 561 | /* program CFOE coefficients */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 562 | if (c->bandwidth_hz != dev->bandwidth_hz) { |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 563 | for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 564 | if (coeff_lut[i].clock == dev->cfg.clock && |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 565 | coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { |
| 566 | break; |
| 567 | } |
| 568 | } |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 569 | ret = af9033_wr_regs(dev, 0x800001, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 570 | coeff_lut[i].val, sizeof(coeff_lut[i].val)); |
| 571 | } |
| 572 | |
| 573 | /* program frequency control */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 574 | if (c->bandwidth_hz != dev->bandwidth_hz) { |
| 575 | spec_inv = dev->cfg.spec_inv ? -1 : 1; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 576 | |
| 577 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 578 | if (clock_adc_lut[i].clock == dev->cfg.clock) |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | adc_freq = clock_adc_lut[i].adc; |
| 582 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 583 | /* get used IF frequency */ |
| 584 | if (fe->ops.tuner_ops.get_if_frequency) |
| 585 | fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); |
| 586 | else |
| 587 | if_frequency = 0; |
| 588 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 589 | sampling_freq = if_frequency; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 590 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 591 | while (sampling_freq > (adc_freq / 2)) |
| 592 | sampling_freq -= adc_freq; |
| 593 | |
| 594 | if (sampling_freq >= 0) |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 595 | spec_inv *= -1; |
| 596 | else |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 597 | sampling_freq *= -1; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 598 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 599 | freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 600 | |
| 601 | if (spec_inv == -1) |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 602 | freq_cw = 0x800000 - freq_cw; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 603 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 604 | if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 605 | freq_cw /= 2; |
| 606 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 607 | buf[0] = (freq_cw >> 0) & 0xff; |
| 608 | buf[1] = (freq_cw >> 8) & 0xff; |
| 609 | buf[2] = (freq_cw >> 16) & 0x7f; |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 610 | |
| 611 | /* FIXME: there seems to be calculation error here... */ |
| 612 | if (if_frequency == 0) |
| 613 | buf[2] = 0; |
| 614 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 615 | ret = af9033_wr_regs(dev, 0x800029, buf, 3); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 616 | if (ret < 0) |
| 617 | goto err; |
| 618 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 619 | dev->bandwidth_hz = c->bandwidth_hz; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 620 | } |
| 621 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 622 | ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 623 | if (ret < 0) |
| 624 | goto err; |
| 625 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 626 | ret = af9033_wr_reg(dev, 0x800040, 0x00); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 627 | if (ret < 0) |
| 628 | goto err; |
| 629 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 630 | ret = af9033_wr_reg(dev, 0x800047, 0x00); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 631 | if (ret < 0) |
| 632 | goto err; |
| 633 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 634 | ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 635 | if (ret < 0) |
| 636 | goto err; |
| 637 | |
| 638 | if (c->frequency <= 230000000) |
| 639 | tmp = 0x00; /* VHF */ |
| 640 | else |
| 641 | tmp = 0x01; /* UHF */ |
| 642 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 643 | ret = af9033_wr_reg(dev, 0x80004b, tmp); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 644 | if (ret < 0) |
| 645 | goto err; |
| 646 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 647 | ret = af9033_wr_reg(dev, 0x800000, 0x00); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 648 | if (ret < 0) |
| 649 | goto err; |
| 650 | |
| 651 | return 0; |
| 652 | |
| 653 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 654 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 659 | static int af9033_get_frontend(struct dvb_frontend *fe) |
| 660 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 661 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 662 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 663 | int ret; |
| 664 | u8 buf[8]; |
| 665 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 666 | dev_dbg(&dev->client->dev, "\n"); |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 667 | |
| 668 | /* read all needed registers */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 669 | ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf)); |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 670 | if (ret < 0) |
| 671 | goto err; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 672 | |
| 673 | switch ((buf[0] >> 0) & 3) { |
| 674 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 675 | c->transmission_mode = TRANSMISSION_MODE_2K; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 676 | break; |
| 677 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 678 | c->transmission_mode = TRANSMISSION_MODE_8K; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | |
| 682 | switch ((buf[1] >> 0) & 3) { |
| 683 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 684 | c->guard_interval = GUARD_INTERVAL_1_32; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 685 | break; |
| 686 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 687 | c->guard_interval = GUARD_INTERVAL_1_16; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 688 | break; |
| 689 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 690 | c->guard_interval = GUARD_INTERVAL_1_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 691 | break; |
| 692 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 693 | c->guard_interval = GUARD_INTERVAL_1_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 694 | break; |
| 695 | } |
| 696 | |
| 697 | switch ((buf[2] >> 0) & 7) { |
| 698 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 699 | c->hierarchy = HIERARCHY_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 700 | break; |
| 701 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 702 | c->hierarchy = HIERARCHY_1; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 703 | break; |
| 704 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 705 | c->hierarchy = HIERARCHY_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 706 | break; |
| 707 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 708 | c->hierarchy = HIERARCHY_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 709 | break; |
| 710 | } |
| 711 | |
| 712 | switch ((buf[3] >> 0) & 3) { |
| 713 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 714 | c->modulation = QPSK; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 715 | break; |
| 716 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 717 | c->modulation = QAM_16; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 718 | break; |
| 719 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 720 | c->modulation = QAM_64; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | |
| 724 | switch ((buf[4] >> 0) & 3) { |
| 725 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 726 | c->bandwidth_hz = 6000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 727 | break; |
| 728 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 729 | c->bandwidth_hz = 7000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 730 | break; |
| 731 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 732 | c->bandwidth_hz = 8000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 733 | break; |
| 734 | } |
| 735 | |
| 736 | switch ((buf[6] >> 0) & 7) { |
| 737 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 738 | c->code_rate_HP = FEC_1_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 739 | break; |
| 740 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 741 | c->code_rate_HP = FEC_2_3; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 742 | break; |
| 743 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 744 | c->code_rate_HP = FEC_3_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 745 | break; |
| 746 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 747 | c->code_rate_HP = FEC_5_6; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 748 | break; |
| 749 | case 4: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 750 | c->code_rate_HP = FEC_7_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 751 | break; |
| 752 | case 5: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 753 | c->code_rate_HP = FEC_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 754 | break; |
| 755 | } |
| 756 | |
| 757 | switch ((buf[7] >> 0) & 7) { |
| 758 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 759 | c->code_rate_LP = FEC_1_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 760 | break; |
| 761 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 762 | c->code_rate_LP = FEC_2_3; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 763 | break; |
| 764 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 765 | c->code_rate_LP = FEC_3_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 766 | break; |
| 767 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 768 | c->code_rate_LP = FEC_5_6; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 769 | break; |
| 770 | case 4: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 771 | c->code_rate_LP = FEC_7_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 772 | break; |
| 773 | case 5: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 774 | c->code_rate_LP = FEC_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 775 | break; |
| 776 | } |
| 777 | |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 778 | return 0; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 779 | |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 780 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 781 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 782 | |
| 783 | return ret; |
| 784 | } |
| 785 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 786 | static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 787 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 788 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 789 | int ret; |
| 790 | u8 tmp; |
| 791 | |
| 792 | *status = 0; |
| 793 | |
| 794 | /* radio channel status, 0=no result, 1=has signal, 2=no signal */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 795 | ret = af9033_rd_reg(dev, 0x800047, &tmp); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 796 | if (ret < 0) |
| 797 | goto err; |
| 798 | |
| 799 | /* has signal */ |
| 800 | if (tmp == 0x01) |
| 801 | *status |= FE_HAS_SIGNAL; |
| 802 | |
| 803 | if (tmp != 0x02) { |
| 804 | /* TPS lock */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 805 | ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 806 | if (ret < 0) |
| 807 | goto err; |
| 808 | |
| 809 | if (tmp) |
| 810 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 811 | FE_HAS_VITERBI; |
| 812 | |
| 813 | /* full lock */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 814 | ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 815 | if (ret < 0) |
| 816 | goto err; |
| 817 | |
| 818 | if (tmp) |
| 819 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 820 | FE_HAS_VITERBI | FE_HAS_SYNC | |
| 821 | FE_HAS_LOCK; |
| 822 | } |
| 823 | |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 824 | dev->fe_status = *status; |
| 825 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 826 | return 0; |
| 827 | |
| 828 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 829 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 830 | |
| 831 | return ret; |
| 832 | } |
| 833 | |
| 834 | static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 835 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 836 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 6b45778 | 2014-09-02 05:03:21 -0300 | [diff] [blame] | 837 | struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 838 | |
Antti Palosaari | 6b45778 | 2014-09-02 05:03:21 -0300 | [diff] [blame] | 839 | /* use DVBv5 CNR */ |
| 840 | if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) |
| 841 | *snr = div_s64(c->cnr.stat[0].svalue, 100); /* 1000x => 10x */ |
| 842 | else |
| 843 | *snr = 0; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 844 | |
| 845 | return 0; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 849 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 850 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 851 | int ret; |
| 852 | u8 strength2; |
| 853 | |
| 854 | /* read signal strength of 0-100 scale */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 855 | ret = af9033_rd_reg(dev, 0x800048, &strength2); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 856 | if (ret < 0) |
| 857 | goto err; |
| 858 | |
| 859 | /* scale value to 0x0000-0xffff */ |
| 860 | *strength = strength2 * 0xffff / 100; |
| 861 | |
| 862 | return 0; |
| 863 | |
| 864 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 865 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 870 | static int af9033_update_ch_stat(struct af9033_dev *dev) |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 871 | { |
| 872 | int ret = 0; |
| 873 | u32 err_cnt, bit_cnt; |
| 874 | u16 abort_cnt; |
| 875 | u8 buf[7]; |
| 876 | |
| 877 | /* only update data every half second */ |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 878 | if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) { |
| 879 | ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf)); |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 880 | if (ret < 0) |
| 881 | goto err; |
| 882 | /* in 8 byte packets? */ |
| 883 | abort_cnt = (buf[1] << 8) + buf[0]; |
| 884 | /* in bits */ |
| 885 | err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2]; |
| 886 | /* in 8 byte packets? always(?) 0x2710 = 10000 */ |
| 887 | bit_cnt = (buf[6] << 8) + buf[5]; |
| 888 | |
| 889 | if (bit_cnt < abort_cnt) { |
| 890 | abort_cnt = 1000; |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 891 | dev->ber = 0xffffffff; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 892 | } else { |
Antti Palosaari | 24e419a | 2014-08-09 14:59:53 -0300 | [diff] [blame] | 893 | /* |
| 894 | * 8 byte packets, that have not been rejected already |
| 895 | */ |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 896 | bit_cnt -= (u32)abort_cnt; |
| 897 | if (bit_cnt == 0) { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 898 | dev->ber = 0xffffffff; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 899 | } else { |
| 900 | err_cnt -= (u32)abort_cnt * 8 * 8; |
| 901 | bit_cnt *= 8 * 8; |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 902 | dev->ber = err_cnt * (0xffffffff / bit_cnt); |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 903 | } |
| 904 | } |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 905 | dev->ucb += abort_cnt; |
| 906 | dev->last_stat_check = jiffies; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | return 0; |
| 910 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 911 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 912 | |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 913 | return ret; |
| 914 | } |
| 915 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 916 | static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 917 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 918 | struct af9033_dev *dev = fe->demodulator_priv; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 919 | int ret; |
| 920 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 921 | ret = af9033_update_ch_stat(dev); |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 922 | if (ret < 0) |
| 923 | return ret; |
| 924 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 925 | *ber = dev->ber; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 931 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 932 | struct af9033_dev *dev = fe->demodulator_priv; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 933 | int ret; |
| 934 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 935 | ret = af9033_update_ch_stat(dev); |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 936 | if (ret < 0) |
| 937 | return ret; |
| 938 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 939 | *ucblocks = dev->ucb; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
| 945 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 946 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 947 | int ret; |
| 948 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 949 | dev_dbg(&dev->client->dev, "enable=%d\n", enable); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 950 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 951 | ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 952 | if (ret < 0) |
| 953 | goto err; |
| 954 | |
| 955 | return 0; |
| 956 | |
| 957 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 958 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 959 | |
| 960 | return ret; |
| 961 | } |
| 962 | |
Mauro Carvalho Chehab | ed97a6f | 2014-03-14 14:29:06 -0300 | [diff] [blame] | 963 | static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff) |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 964 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 965 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 966 | int ret; |
| 967 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 968 | dev_dbg(&dev->client->dev, "onoff=%d\n", onoff); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 969 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 970 | ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 971 | if (ret < 0) |
| 972 | goto err; |
| 973 | |
| 974 | return 0; |
| 975 | |
| 976 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 977 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 978 | |
| 979 | return ret; |
| 980 | } |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 981 | |
Antti Palosaari | 24e419a | 2014-08-09 14:59:53 -0300 | [diff] [blame] | 982 | static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid, |
| 983 | int onoff) |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 984 | { |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 985 | struct af9033_dev *dev = fe->demodulator_priv; |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 986 | int ret; |
| 987 | u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff}; |
| 988 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 989 | dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n", |
| 990 | index, pid, onoff); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 991 | |
| 992 | if (pid > 0x1fff) |
| 993 | return 0; |
| 994 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 995 | ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 996 | if (ret < 0) |
| 997 | goto err; |
| 998 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 999 | ret = af9033_wr_reg(dev, 0x80f994, onoff); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 1000 | if (ret < 0) |
| 1001 | goto err; |
| 1002 | |
Antti Palosaari | 09611ca | 2014-08-31 02:18:34 -0300 | [diff] [blame] | 1003 | ret = af9033_wr_reg(dev, 0x80f995, index); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 1004 | if (ret < 0) |
| 1005 | goto err; |
| 1006 | |
| 1007 | return 0; |
| 1008 | |
| 1009 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 1010 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 1011 | |
| 1012 | return ret; |
| 1013 | } |
Antti Palosaari | 040cf86 | 2014-02-13 15:40:59 -0300 | [diff] [blame] | 1014 | |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 1015 | static void af9033_stat_work(struct work_struct *work) |
| 1016 | { |
| 1017 | struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work); |
| 1018 | struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; |
Antti Palosaari | 3e41313 | 2014-09-02 04:24:41 -0300 | [diff] [blame] | 1019 | int ret, tmp, i, len; |
Antti Palosaari | 204f431 | 2014-09-02 08:01:10 -0300 | [diff] [blame^] | 1020 | u8 u8tmp, buf[7]; |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 1021 | |
| 1022 | dev_dbg(&dev->client->dev, "\n"); |
| 1023 | |
Antti Palosaari | 3e41313 | 2014-09-02 04:24:41 -0300 | [diff] [blame] | 1024 | /* signal strength */ |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 1025 | if (dev->fe_status & FE_HAS_SIGNAL) { |
| 1026 | if (dev->is_af9035) { |
| 1027 | ret = af9033_rd_reg(dev, 0x80004a, &u8tmp); |
| 1028 | tmp = -u8tmp * 1000; |
| 1029 | } else { |
| 1030 | ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp); |
| 1031 | tmp = (u8tmp - 100) * 1000; |
| 1032 | } |
| 1033 | if (ret) |
| 1034 | goto err; |
| 1035 | |
| 1036 | c->strength.len = 1; |
| 1037 | c->strength.stat[0].scale = FE_SCALE_DECIBEL; |
| 1038 | c->strength.stat[0].svalue = tmp; |
| 1039 | } else { |
| 1040 | c->strength.len = 1; |
| 1041 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 1042 | } |
| 1043 | |
Antti Palosaari | 3e41313 | 2014-09-02 04:24:41 -0300 | [diff] [blame] | 1044 | /* CNR */ |
| 1045 | if (dev->fe_status & FE_HAS_VITERBI) { |
| 1046 | u32 snr_val; |
| 1047 | const struct val_snr *snr_lut; |
| 1048 | |
| 1049 | /* read value */ |
| 1050 | ret = af9033_rd_regs(dev, 0x80002c, buf, 3); |
| 1051 | if (ret) |
| 1052 | goto err; |
| 1053 | |
| 1054 | snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0); |
| 1055 | |
| 1056 | /* read current modulation */ |
| 1057 | ret = af9033_rd_reg(dev, 0x80f903, &u8tmp); |
| 1058 | if (ret) |
| 1059 | goto err; |
| 1060 | |
| 1061 | switch ((u8tmp >> 0) & 3) { |
| 1062 | case 0: |
| 1063 | len = ARRAY_SIZE(qpsk_snr_lut); |
| 1064 | snr_lut = qpsk_snr_lut; |
| 1065 | break; |
| 1066 | case 1: |
| 1067 | len = ARRAY_SIZE(qam16_snr_lut); |
| 1068 | snr_lut = qam16_snr_lut; |
| 1069 | break; |
| 1070 | case 2: |
| 1071 | len = ARRAY_SIZE(qam64_snr_lut); |
| 1072 | snr_lut = qam64_snr_lut; |
| 1073 | break; |
| 1074 | default: |
| 1075 | goto err_schedule_delayed_work; |
| 1076 | } |
| 1077 | |
| 1078 | for (i = 0; i < len; i++) { |
| 1079 | tmp = snr_lut[i].snr * 1000; |
| 1080 | if (snr_val < snr_lut[i].val) |
| 1081 | break; |
| 1082 | } |
| 1083 | |
| 1084 | c->cnr.len = 1; |
| 1085 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 1086 | c->cnr.stat[0].svalue = tmp; |
| 1087 | } else { |
| 1088 | c->cnr.len = 1; |
| 1089 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 1090 | } |
| 1091 | |
Antti Palosaari | 204f431 | 2014-09-02 08:01:10 -0300 | [diff] [blame^] | 1092 | /* UCB/PER/BER */ |
| 1093 | if (dev->fe_status & FE_HAS_LOCK) { |
| 1094 | /* outer FEC, 204 byte packets */ |
| 1095 | u16 abort_packet_count, rsd_packet_count; |
| 1096 | |
| 1097 | /* |
| 1098 | * Packet count used for measurement is 10000 |
| 1099 | * (rsd_packet_count). Maybe it should be increased? |
| 1100 | */ |
| 1101 | |
| 1102 | ret = af9033_rd_regs(dev, 0x800032, buf, 7); |
| 1103 | if (ret) |
| 1104 | goto err; |
| 1105 | |
| 1106 | abort_packet_count = (buf[1] << 8) | (buf[0] << 0); |
| 1107 | rsd_packet_count = (buf[6] << 8) | (buf[5] << 0); |
| 1108 | |
| 1109 | dev->error_block_count += abort_packet_count; |
| 1110 | dev->total_block_count += rsd_packet_count; |
| 1111 | |
| 1112 | c->block_count.len = 1; |
| 1113 | c->block_count.stat[0].scale = FE_SCALE_COUNTER; |
| 1114 | c->block_count.stat[0].uvalue = dev->total_block_count; |
| 1115 | |
| 1116 | c->block_error.len = 1; |
| 1117 | c->block_error.stat[0].scale = FE_SCALE_COUNTER; |
| 1118 | c->block_error.stat[0].uvalue = dev->error_block_count; |
| 1119 | } |
| 1120 | |
Antti Palosaari | 3e41313 | 2014-09-02 04:24:41 -0300 | [diff] [blame] | 1121 | err_schedule_delayed_work: |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 1122 | schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000)); |
| 1123 | return; |
| 1124 | err: |
| 1125 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
| 1126 | } |
| 1127 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1128 | static struct dvb_frontend_ops af9033_ops = { |
| 1129 | .delsys = { SYS_DVBT }, |
| 1130 | .info = { |
| 1131 | .name = "Afatech AF9033 (DVB-T)", |
| 1132 | .frequency_min = 174000000, |
| 1133 | .frequency_max = 862000000, |
| 1134 | .frequency_stepsize = 250000, |
| 1135 | .frequency_tolerance = 0, |
| 1136 | .caps = FE_CAN_FEC_1_2 | |
| 1137 | FE_CAN_FEC_2_3 | |
| 1138 | FE_CAN_FEC_3_4 | |
| 1139 | FE_CAN_FEC_5_6 | |
| 1140 | FE_CAN_FEC_7_8 | |
| 1141 | FE_CAN_FEC_AUTO | |
| 1142 | FE_CAN_QPSK | |
| 1143 | FE_CAN_QAM_16 | |
| 1144 | FE_CAN_QAM_64 | |
| 1145 | FE_CAN_QAM_AUTO | |
| 1146 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 1147 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 1148 | FE_CAN_HIERARCHY_AUTO | |
| 1149 | FE_CAN_RECOVER | |
| 1150 | FE_CAN_MUTE_TS |
| 1151 | }, |
| 1152 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1153 | .init = af9033_init, |
| 1154 | .sleep = af9033_sleep, |
| 1155 | |
| 1156 | .get_tune_settings = af9033_get_tune_settings, |
| 1157 | .set_frontend = af9033_set_frontend, |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 1158 | .get_frontend = af9033_get_frontend, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1159 | |
| 1160 | .read_status = af9033_read_status, |
| 1161 | .read_snr = af9033_read_snr, |
| 1162 | .read_signal_strength = af9033_read_signal_strength, |
| 1163 | .read_ber = af9033_read_ber, |
| 1164 | .read_ucblocks = af9033_read_ucblocks, |
| 1165 | |
| 1166 | .i2c_gate_ctrl = af9033_i2c_gate_ctrl, |
| 1167 | }; |
| 1168 | |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1169 | static int af9033_probe(struct i2c_client *client, |
| 1170 | const struct i2c_device_id *id) |
| 1171 | { |
| 1172 | struct af9033_config *cfg = client->dev.platform_data; |
| 1173 | struct af9033_dev *dev; |
| 1174 | int ret; |
| 1175 | u8 buf[8]; |
| 1176 | u32 reg; |
| 1177 | |
| 1178 | /* allocate memory for the internal state */ |
| 1179 | dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL); |
| 1180 | if (dev == NULL) { |
| 1181 | ret = -ENOMEM; |
| 1182 | dev_err(&client->dev, "Could not allocate memory for state\n"); |
| 1183 | goto err; |
| 1184 | } |
| 1185 | |
| 1186 | /* setup the state */ |
| 1187 | dev->client = client; |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 1188 | INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work); |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1189 | memcpy(&dev->cfg, cfg, sizeof(struct af9033_config)); |
| 1190 | |
| 1191 | if (dev->cfg.clock != 12000000) { |
| 1192 | ret = -ENODEV; |
| 1193 | dev_err(&dev->client->dev, |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 1194 | "unsupported clock %d Hz, only 12000000 Hz is supported currently\n", |
| 1195 | dev->cfg.clock); |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1196 | goto err_kfree; |
| 1197 | } |
| 1198 | |
| 1199 | /* firmware version */ |
| 1200 | switch (dev->cfg.tuner) { |
| 1201 | case AF9033_TUNER_IT9135_38: |
| 1202 | case AF9033_TUNER_IT9135_51: |
| 1203 | case AF9033_TUNER_IT9135_52: |
| 1204 | case AF9033_TUNER_IT9135_60: |
| 1205 | case AF9033_TUNER_IT9135_61: |
| 1206 | case AF9033_TUNER_IT9135_62: |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 1207 | dev->is_it9135 = true; |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1208 | reg = 0x004bfc; |
| 1209 | break; |
| 1210 | default: |
Antti Palosaari | 83f1161 | 2014-09-02 03:55:21 -0300 | [diff] [blame] | 1211 | dev->is_af9035 = true; |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1212 | reg = 0x0083e9; |
| 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | ret = af9033_rd_regs(dev, reg, &buf[0], 4); |
| 1217 | if (ret < 0) |
| 1218 | goto err_kfree; |
| 1219 | |
| 1220 | ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4); |
| 1221 | if (ret < 0) |
| 1222 | goto err_kfree; |
| 1223 | |
| 1224 | dev_info(&dev->client->dev, |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 1225 | "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n", |
| 1226 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], |
| 1227 | buf[7]); |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1228 | |
| 1229 | /* sleep */ |
| 1230 | switch (dev->cfg.tuner) { |
| 1231 | case AF9033_TUNER_IT9135_38: |
| 1232 | case AF9033_TUNER_IT9135_51: |
| 1233 | case AF9033_TUNER_IT9135_52: |
| 1234 | case AF9033_TUNER_IT9135_60: |
| 1235 | case AF9033_TUNER_IT9135_61: |
| 1236 | case AF9033_TUNER_IT9135_62: |
| 1237 | /* IT9135 did not like to sleep at that early */ |
| 1238 | break; |
| 1239 | default: |
| 1240 | ret = af9033_wr_reg(dev, 0x80004c, 1); |
| 1241 | if (ret < 0) |
| 1242 | goto err_kfree; |
| 1243 | |
| 1244 | ret = af9033_wr_reg(dev, 0x800000, 0); |
| 1245 | if (ret < 0) |
| 1246 | goto err_kfree; |
| 1247 | } |
| 1248 | |
| 1249 | /* configure internal TS mode */ |
| 1250 | switch (dev->cfg.ts_mode) { |
| 1251 | case AF9033_TS_MODE_PARALLEL: |
| 1252 | dev->ts_mode_parallel = true; |
| 1253 | break; |
| 1254 | case AF9033_TS_MODE_SERIAL: |
| 1255 | dev->ts_mode_serial = true; |
| 1256 | break; |
| 1257 | case AF9033_TS_MODE_USB: |
| 1258 | /* usb mode for AF9035 */ |
| 1259 | default: |
| 1260 | break; |
| 1261 | } |
| 1262 | |
| 1263 | /* create dvb_frontend */ |
| 1264 | memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); |
| 1265 | dev->fe.demodulator_priv = dev; |
| 1266 | *cfg->fe = &dev->fe; |
| 1267 | if (cfg->ops) { |
| 1268 | cfg->ops->pid_filter = af9033_pid_filter; |
| 1269 | cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl; |
| 1270 | } |
| 1271 | i2c_set_clientdata(client, dev); |
| 1272 | |
| 1273 | dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n"); |
| 1274 | return 0; |
| 1275 | err_kfree: |
| 1276 | kfree(dev); |
| 1277 | err: |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 1278 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1279 | return ret; |
| 1280 | } |
| 1281 | |
| 1282 | static int af9033_remove(struct i2c_client *client) |
| 1283 | { |
| 1284 | struct af9033_dev *dev = i2c_get_clientdata(client); |
| 1285 | |
Antti Palosaari | 6a087f1 | 2014-08-31 21:08:09 -0300 | [diff] [blame] | 1286 | dev_dbg(&dev->client->dev, "\n"); |
Antti Palosaari | f5b00a7 | 2014-08-31 20:57:05 -0300 | [diff] [blame] | 1287 | |
| 1288 | dev->fe.ops.release = NULL; |
| 1289 | dev->fe.demodulator_priv = NULL; |
| 1290 | kfree(dev); |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | static const struct i2c_device_id af9033_id_table[] = { |
| 1296 | {"af9033", 0}, |
| 1297 | {} |
| 1298 | }; |
| 1299 | MODULE_DEVICE_TABLE(i2c, af9033_id_table); |
| 1300 | |
| 1301 | static struct i2c_driver af9033_driver = { |
| 1302 | .driver = { |
| 1303 | .owner = THIS_MODULE, |
| 1304 | .name = "af9033", |
| 1305 | }, |
| 1306 | .probe = af9033_probe, |
| 1307 | .remove = af9033_remove, |
| 1308 | .id_table = af9033_id_table, |
| 1309 | }; |
| 1310 | |
| 1311 | module_i2c_driver(af9033_driver); |
| 1312 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1313 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1314 | MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); |
| 1315 | MODULE_LICENSE("GPL"); |