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