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