Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 2 | /* |
| 3 | STB0899 Multistandard Frontend driver |
| 4 | Copyright (C) Manu Abraham (abraham.manu@gmail.com) |
| 5 | |
| 6 | Copyright (C) ST Microelectronics |
| 7 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/init.h> |
Asaf Vertz | 3edd59a | 2014-12-14 03:49:47 -0300 | [diff] [blame] | 11 | #include <linux/jiffies.h> |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 15 | #include <linux/string.h> |
| 16 | |
| 17 | #include <linux/dvb/frontend.h> |
Mauro Carvalho Chehab | fada193 | 2017-12-28 13:03:51 -0500 | [diff] [blame] | 18 | #include <media/dvb_frontend.h> |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 19 | |
| 20 | #include "stb0899_drv.h" |
| 21 | #include "stb0899_priv.h" |
| 22 | #include "stb0899_reg.h" |
| 23 | |
Mauro Carvalho Chehab | ba47464 | 2013-11-02 05:14:58 -0300 | [diff] [blame] | 24 | /* Max transfer size done by I2C transfer functions */ |
| 25 | #define MAX_XFER_SIZE 64 |
| 26 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 27 | static unsigned int verbose = 0;//1; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 28 | module_param(verbose, int, 0644); |
| 29 | |
| 30 | /* C/N in dB/10, NIRM/NIRL */ |
| 31 | static const struct stb0899_tab stb0899_cn_tab[] = { |
| 32 | { 200, 2600 }, |
| 33 | { 190, 2700 }, |
| 34 | { 180, 2860 }, |
| 35 | { 170, 3020 }, |
| 36 | { 160, 3210 }, |
| 37 | { 150, 3440 }, |
| 38 | { 140, 3710 }, |
| 39 | { 130, 4010 }, |
| 40 | { 120, 4360 }, |
| 41 | { 110, 4740 }, |
| 42 | { 100, 5190 }, |
| 43 | { 90, 5670 }, |
| 44 | { 80, 6200 }, |
| 45 | { 70, 6770 }, |
| 46 | { 60, 7360 }, |
| 47 | { 50, 7970 }, |
| 48 | { 40, 8250 }, |
| 49 | { 30, 9000 }, |
| 50 | { 20, 9450 }, |
| 51 | { 15, 9600 }, |
| 52 | }; |
| 53 | |
| 54 | /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10. |
| 55 | * As measured, connected to a modulator. |
| 56 | * -8.0 to -50.0 dBm directly connected, |
| 57 | * -52.0 to -74.8 with extra attenuation. |
| 58 | * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm. |
| 59 | * Crude linear extrapolation below -84.8dBm and above -8.0dBm. |
| 60 | */ |
| 61 | static const struct stb0899_tab stb0899_dvbsrf_tab[] = { |
Klaus Schmidinger | 0e37781 | 2012-01-24 05:33:53 -0300 | [diff] [blame] | 62 | { -750, -128 }, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 63 | { -748, -94 }, |
| 64 | { -745, -92 }, |
| 65 | { -735, -90 }, |
| 66 | { -720, -87 }, |
| 67 | { -670, -77 }, |
| 68 | { -640, -70 }, |
| 69 | { -610, -62 }, |
| 70 | { -600, -60 }, |
| 71 | { -590, -56 }, |
| 72 | { -560, -41 }, |
| 73 | { -540, -25 }, |
| 74 | { -530, -17 }, |
| 75 | { -520, -11 }, |
| 76 | { -500, 1 }, |
| 77 | { -490, 6 }, |
| 78 | { -480, 10 }, |
| 79 | { -440, 22 }, |
| 80 | { -420, 27 }, |
| 81 | { -400, 31 }, |
| 82 | { -380, 34 }, |
| 83 | { -340, 40 }, |
| 84 | { -320, 43 }, |
| 85 | { -280, 48 }, |
| 86 | { -250, 52 }, |
| 87 | { -230, 55 }, |
| 88 | { -180, 61 }, |
| 89 | { -140, 66 }, |
| 90 | { -90, 73 }, |
| 91 | { -80, 74 }, |
| 92 | { 500, 127 } |
| 93 | }; |
| 94 | |
| 95 | /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10. |
| 96 | * As measured, connected to a modulator. |
| 97 | * -8.0 to -50.1 dBm directly connected, |
| 98 | * -53.0 to -76.6 with extra attenuation. |
| 99 | * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm. |
| 100 | * Crude linear extrapolation below -76.6dBm and above -8.0dBm. |
| 101 | */ |
| 102 | static const struct stb0899_tab stb0899_dvbs2rf_tab[] = { |
| 103 | { 700, 0 }, |
| 104 | { -80, 3217 }, |
| 105 | { -150, 3893 }, |
| 106 | { -190, 4217 }, |
| 107 | { -240, 4621 }, |
| 108 | { -280, 4945 }, |
| 109 | { -320, 5273 }, |
| 110 | { -350, 5545 }, |
| 111 | { -370, 5741 }, |
| 112 | { -410, 6147 }, |
| 113 | { -450, 6671 }, |
| 114 | { -490, 7413 }, |
| 115 | { -501, 7665 }, |
| 116 | { -530, 8767 }, |
| 117 | { -560, 10219 }, |
| 118 | { -580, 10939 }, |
| 119 | { -590, 11518 }, |
| 120 | { -600, 11723 }, |
| 121 | { -650, 12659 }, |
| 122 | { -690, 13219 }, |
| 123 | { -730, 13645 }, |
| 124 | { -750, 13909 }, |
| 125 | { -766, 14153 }, |
Klaus Schmidinger | 0e37781 | 2012-01-24 05:33:53 -0300 | [diff] [blame] | 126 | { -950, 16383 } |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/ |
Mauro Carvalho Chehab | ffbc5f8 | 2009-01-05 01:34:20 -0300 | [diff] [blame] | 130 | static struct stb0899_tab stb0899_quant_tab[] = { |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 131 | { 0, 0 }, |
| 132 | { 0, 100 }, |
| 133 | { 600, 200 }, |
| 134 | { 950, 299 }, |
| 135 | { 1200, 398 }, |
| 136 | { 1400, 501 }, |
| 137 | { 1560, 603 }, |
| 138 | { 1690, 700 }, |
| 139 | { 1810, 804 }, |
| 140 | { 1910, 902 }, |
| 141 | { 2000, 1000 }, |
| 142 | { 2080, 1096 }, |
| 143 | { 2160, 1202 }, |
| 144 | { 2230, 1303 }, |
| 145 | { 2350, 1496 }, |
| 146 | { 2410, 1603 }, |
| 147 | { 2460, 1698 }, |
| 148 | { 2510, 1799 }, |
| 149 | { 2600, 1995 }, |
| 150 | { 2650, 2113 }, |
| 151 | { 2690, 2213 }, |
| 152 | { 2720, 2291 }, |
| 153 | { 2760, 2399 }, |
| 154 | { 2800, 2512 }, |
| 155 | { 2860, 2692 }, |
| 156 | { 2930, 2917 }, |
| 157 | { 2960, 3020 }, |
| 158 | { 3010, 3199 }, |
| 159 | { 3040, 3311 }, |
| 160 | { 3060, 3388 }, |
| 161 | { 3120, 3631 }, |
| 162 | { 3190, 3936 }, |
| 163 | { 3400, 5012 }, |
| 164 | { 3610, 6383 }, |
| 165 | { 3800, 7943 }, |
| 166 | { 4210, 12735 }, |
| 167 | { 4500, 17783 }, |
| 168 | { 4690, 22131 }, |
| 169 | { 4810, 25410 } |
| 170 | }; |
| 171 | |
| 172 | /* DVB-S2 Es/N0 estimate in dB/100 vs read value */ |
Mauro Carvalho Chehab | ffbc5f8 | 2009-01-05 01:34:20 -0300 | [diff] [blame] | 173 | static struct stb0899_tab stb0899_est_tab[] = { |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 174 | { 0, 0 }, |
| 175 | { 0, 1 }, |
| 176 | { 301, 2 }, |
| 177 | { 1204, 16 }, |
| 178 | { 1806, 64 }, |
| 179 | { 2408, 256 }, |
| 180 | { 2709, 512 }, |
| 181 | { 3010, 1023 }, |
| 182 | { 3311, 2046 }, |
| 183 | { 3612, 4093 }, |
| 184 | { 3823, 6653 }, |
| 185 | { 3913, 8185 }, |
| 186 | { 4010, 10233 }, |
| 187 | { 4107, 12794 }, |
| 188 | { 4214, 16368 }, |
| 189 | { 4266, 18450 }, |
| 190 | { 4311, 20464 }, |
| 191 | { 4353, 22542 }, |
| 192 | { 4391, 24604 }, |
| 193 | { 4425, 26607 }, |
| 194 | { 4457, 28642 }, |
| 195 | { 4487, 30690 }, |
| 196 | { 4515, 32734 }, |
| 197 | { 4612, 40926 }, |
| 198 | { 4692, 49204 }, |
| 199 | { 4816, 65464 }, |
| 200 | { 4913, 81846 }, |
| 201 | { 4993, 98401 }, |
| 202 | { 5060, 114815 }, |
| 203 | { 5118, 131220 }, |
| 204 | { 5200, 158489 }, |
| 205 | { 5300, 199526 }, |
| 206 | { 5400, 251189 }, |
| 207 | { 5500, 316228 }, |
| 208 | { 5600, 398107 }, |
| 209 | { 5720, 524807 }, |
| 210 | { 5721, 526017 }, |
| 211 | }; |
| 212 | |
Mauro Carvalho Chehab | ffbc5f8 | 2009-01-05 01:34:20 -0300 | [diff] [blame] | 213 | static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 214 | { |
| 215 | int ret; |
| 216 | |
| 217 | u8 b0[] = { reg >> 8, reg & 0xff }; |
| 218 | u8 buf; |
| 219 | |
| 220 | struct i2c_msg msg[] = { |
| 221 | { |
| 222 | .addr = state->config->demod_address, |
| 223 | .flags = 0, |
| 224 | .buf = b0, |
| 225 | .len = 2 |
| 226 | },{ |
| 227 | .addr = state->config->demod_address, |
| 228 | .flags = I2C_M_RD, |
| 229 | .buf = &buf, |
| 230 | .len = 1 |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | ret = i2c_transfer(state->i2c, msg, 2); |
| 235 | if (ret != 2) { |
| 236 | if (ret != -ERESTARTSYS) |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 237 | dprintk(state->verbose, FE_ERROR, 1, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 238 | "Read error, Reg=[0x%02x], Status=%d", |
| 239 | reg, ret); |
| 240 | |
| 241 | return ret < 0 ? ret : -EREMOTEIO; |
| 242 | } |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 243 | if (unlikely(*state->verbose >= FE_DEBUGREG)) |
| 244 | dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x", |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 245 | reg, buf); |
| 246 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 247 | return (unsigned int)buf; |
| 248 | } |
| 249 | |
| 250 | int stb0899_read_reg(struct stb0899_state *state, unsigned int reg) |
| 251 | { |
| 252 | int result; |
| 253 | |
| 254 | result = _stb0899_read_reg(state, reg); |
| 255 | /* |
| 256 | * Bug ID 9: |
| 257 | * access to 0xf2xx/0xf6xx |
| 258 | * must be followed by read from 0xf2ff/0xf6ff. |
| 259 | */ |
| 260 | if ((reg != 0xf2ff) && (reg != 0xf6ff) && |
| 261 | (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600))) |
| 262 | _stb0899_read_reg(state, (reg | 0x00ff)); |
| 263 | |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | u32 _stb0899_read_s2reg(struct stb0899_state *state, |
Igor M. Liplianin | 886134e | 2008-10-30 22:04:56 -0300 | [diff] [blame] | 268 | u32 stb0899_i2cdev, |
| 269 | u32 stb0899_base_addr, |
| 270 | u16 stb0899_reg_offset) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 271 | { |
| 272 | int status; |
| 273 | u32 data; |
| 274 | u8 buf[7] = { 0 }; |
| 275 | u16 tmpaddr; |
| 276 | |
| 277 | u8 buf_0[] = { |
| 278 | GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */ |
| 279 | GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */ |
| 280 | GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */ |
| 281 | GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */ |
| 282 | GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */ |
| 283 | GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */ |
| 284 | }; |
| 285 | u8 buf_1[] = { |
| 286 | 0x00, /* 0xf3 Reg Offset */ |
| 287 | 0x00, /* 0x44 Reg Offset */ |
| 288 | }; |
| 289 | |
| 290 | struct i2c_msg msg_0 = { |
| 291 | .addr = state->config->demod_address, |
| 292 | .flags = 0, |
| 293 | .buf = buf_0, |
| 294 | .len = 6 |
| 295 | }; |
| 296 | |
| 297 | struct i2c_msg msg_1 = { |
| 298 | .addr = state->config->demod_address, |
| 299 | .flags = 0, |
| 300 | .buf = buf_1, |
| 301 | .len = 2 |
| 302 | }; |
| 303 | |
| 304 | struct i2c_msg msg_r = { |
| 305 | .addr = state->config->demod_address, |
| 306 | .flags = I2C_M_RD, |
| 307 | .buf = buf, |
| 308 | .len = 4 |
| 309 | }; |
| 310 | |
| 311 | tmpaddr = stb0899_reg_offset & 0xff00; |
| 312 | if (!(stb0899_reg_offset & 0x8)) |
| 313 | tmpaddr = stb0899_reg_offset | 0x20; |
| 314 | |
| 315 | buf_1[0] = GETBYTE(tmpaddr, BYTE1); |
| 316 | buf_1[1] = GETBYTE(tmpaddr, BYTE0); |
| 317 | |
| 318 | status = i2c_transfer(state->i2c, &msg_0, 1); |
| 319 | if (status < 1) { |
| 320 | if (status != -ERESTARTSYS) |
| 321 | printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n", |
| 322 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status); |
| 323 | |
| 324 | goto err; |
| 325 | } |
| 326 | |
| 327 | /* Dummy */ |
| 328 | status = i2c_transfer(state->i2c, &msg_1, 1); |
| 329 | if (status < 1) |
| 330 | goto err; |
| 331 | |
| 332 | status = i2c_transfer(state->i2c, &msg_r, 1); |
| 333 | if (status < 1) |
| 334 | goto err; |
| 335 | |
| 336 | buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1); |
| 337 | buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0); |
| 338 | |
| 339 | /* Actual */ |
| 340 | status = i2c_transfer(state->i2c, &msg_1, 1); |
| 341 | if (status < 1) { |
| 342 | if (status != -ERESTARTSYS) |
| 343 | printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n", |
| 344 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status); |
| 345 | goto err; |
| 346 | } |
| 347 | |
| 348 | status = i2c_transfer(state->i2c, &msg_r, 1); |
| 349 | if (status < 1) { |
| 350 | if (status != -ERESTARTSYS) |
| 351 | printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n", |
| 352 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status); |
| 353 | return status < 0 ? status : -EREMOTEIO; |
| 354 | } |
| 355 | |
| 356 | data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]); |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 357 | if (unlikely(*state->verbose >= FE_DEBUGREG)) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 358 | printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n", |
| 359 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data); |
| 360 | |
| 361 | return data; |
| 362 | |
| 363 | err: |
| 364 | return status < 0 ? status : -EREMOTEIO; |
| 365 | } |
| 366 | |
| 367 | int stb0899_write_s2reg(struct stb0899_state *state, |
| 368 | u32 stb0899_i2cdev, |
| 369 | u32 stb0899_base_addr, |
| 370 | u16 stb0899_reg_offset, |
| 371 | u32 stb0899_data) |
| 372 | { |
| 373 | int status; |
| 374 | |
| 375 | /* Base Address Setup */ |
| 376 | u8 buf_0[] = { |
| 377 | GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */ |
| 378 | GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */ |
| 379 | GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */ |
| 380 | GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */ |
| 381 | GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */ |
| 382 | GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */ |
| 383 | }; |
| 384 | u8 buf_1[] = { |
| 385 | 0x00, /* 0xf3 Reg Offset */ |
| 386 | 0x00, /* 0x44 Reg Offset */ |
| 387 | 0x00, /* data */ |
| 388 | 0x00, /* data */ |
| 389 | 0x00, /* data */ |
| 390 | 0x00, /* data */ |
| 391 | }; |
| 392 | |
| 393 | struct i2c_msg msg_0 = { |
| 394 | .addr = state->config->demod_address, |
| 395 | .flags = 0, |
| 396 | .buf = buf_0, |
| 397 | .len = 6 |
| 398 | }; |
| 399 | |
| 400 | struct i2c_msg msg_1 = { |
| 401 | .addr = state->config->demod_address, |
| 402 | .flags = 0, |
| 403 | .buf = buf_1, |
| 404 | .len = 6 |
| 405 | }; |
| 406 | |
| 407 | buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1); |
| 408 | buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0); |
| 409 | buf_1[2] = GETBYTE(stb0899_data, BYTE0); |
| 410 | buf_1[3] = GETBYTE(stb0899_data, BYTE1); |
| 411 | buf_1[4] = GETBYTE(stb0899_data, BYTE2); |
| 412 | buf_1[5] = GETBYTE(stb0899_data, BYTE3); |
| 413 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 414 | if (unlikely(*state->verbose >= FE_DEBUGREG)) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 415 | printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n", |
| 416 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data); |
| 417 | |
| 418 | status = i2c_transfer(state->i2c, &msg_0, 1); |
| 419 | if (unlikely(status < 1)) { |
| 420 | if (status != -ERESTARTSYS) |
| 421 | printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n", |
| 422 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status); |
| 423 | goto err; |
| 424 | } |
| 425 | status = i2c_transfer(state->i2c, &msg_1, 1); |
| 426 | if (unlikely(status < 1)) { |
| 427 | if (status != -ERESTARTSYS) |
| 428 | printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n", |
| 429 | __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status); |
| 430 | |
| 431 | return status < 0 ? status : -EREMOTEIO; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | |
| 436 | err: |
| 437 | return status < 0 ? status : -EREMOTEIO; |
| 438 | } |
| 439 | |
Manu Abraham | 3d6a3beb | 2008-01-25 20:39:16 -0300 | [diff] [blame] | 440 | int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 441 | { |
| 442 | int status; |
| 443 | |
| 444 | u8 b0[] = { reg >> 8, reg & 0xff }; |
| 445 | |
| 446 | struct i2c_msg msg[] = { |
| 447 | { |
| 448 | .addr = state->config->demod_address, |
| 449 | .flags = 0, |
| 450 | .buf = b0, |
| 451 | .len = 2 |
| 452 | },{ |
| 453 | .addr = state->config->demod_address, |
| 454 | .flags = I2C_M_RD, |
| 455 | .buf = buf, |
| 456 | .len = count |
| 457 | } |
| 458 | }; |
| 459 | |
| 460 | status = i2c_transfer(state->i2c, msg, 2); |
| 461 | if (status != 2) { |
| 462 | if (status != -ERESTARTSYS) |
| 463 | printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n", |
| 464 | __func__, reg, count, status); |
| 465 | goto err; |
| 466 | } |
| 467 | /* |
| 468 | * Bug ID 9: |
| 469 | * access to 0xf2xx/0xf6xx |
| 470 | * must be followed by read from 0xf2ff/0xf6ff. |
| 471 | */ |
| 472 | if ((reg != 0xf2ff) && (reg != 0xf6ff) && |
| 473 | (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600))) |
| 474 | _stb0899_read_reg(state, (reg | 0x00ff)); |
| 475 | |
Mauro Carvalho Chehab | a8cd47d | 2016-10-13 15:39:45 -0300 | [diff] [blame] | 476 | dprintk(state->verbose, FE_DEBUGREG, 1, |
| 477 | "%s [0x%04x]: %*ph", __func__, reg, count, buf); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | err: |
| 481 | return status < 0 ? status : -EREMOTEIO; |
| 482 | } |
| 483 | |
Manu Abraham | 85eabac | 2008-01-25 20:28:46 -0300 | [diff] [blame] | 484 | int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 485 | { |
| 486 | int ret; |
Mauro Carvalho Chehab | ba47464 | 2013-11-02 05:14:58 -0300 | [diff] [blame] | 487 | u8 buf[MAX_XFER_SIZE]; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 488 | struct i2c_msg i2c_msg = { |
| 489 | .addr = state->config->demod_address, |
| 490 | .flags = 0, |
| 491 | .buf = buf, |
| 492 | .len = 2 + count |
| 493 | }; |
| 494 | |
Mauro Carvalho Chehab | ba47464 | 2013-11-02 05:14:58 -0300 | [diff] [blame] | 495 | if (2 + count > sizeof(buf)) { |
| 496 | printk(KERN_WARNING |
| 497 | "%s: i2c wr reg=%04x: len=%d is too big!\n", |
| 498 | KBUILD_MODNAME, reg, count); |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 502 | buf[0] = reg >> 8; |
| 503 | buf[1] = reg & 0xff; |
| 504 | memcpy(&buf[2], data, count); |
| 505 | |
Mauro Carvalho Chehab | a8cd47d | 2016-10-13 15:39:45 -0300 | [diff] [blame] | 506 | dprintk(state->verbose, FE_DEBUGREG, 1, |
| 507 | "%s [0x%04x]: %*ph", __func__, reg, count, data); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 508 | ret = i2c_transfer(state->i2c, &i2c_msg, 1); |
| 509 | |
| 510 | /* |
| 511 | * Bug ID 9: |
| 512 | * access to 0xf2xx/0xf6xx |
| 513 | * must be followed by read from 0xf2ff/0xf6ff. |
| 514 | */ |
| 515 | if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600))) |
| 516 | stb0899_read_reg(state, (reg | 0x00ff)); |
| 517 | |
| 518 | if (ret != 1) { |
| 519 | if (ret != -ERESTARTSYS) |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 520 | dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d", |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 521 | reg, data[0], count, ret); |
| 522 | return ret < 0 ? ret : -EREMOTEIO; |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data) |
| 529 | { |
Arnd Bergmann | 3cd890d | 2017-11-30 11:55:46 -0500 | [diff] [blame] | 530 | u8 tmp = data; |
| 531 | return stb0899_write_regs(state, reg, &tmp, 1); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
| 535 | * stb0899_get_mclk |
| 536 | * Get STB0899 master clock frequency |
| 537 | * ExtClk: external clock frequency (Hz) |
| 538 | */ |
| 539 | static u32 stb0899_get_mclk(struct stb0899_state *state) |
| 540 | { |
Manu Abraham | 72f7841 | 2007-11-10 16:59:16 -0300 | [diff] [blame] | 541 | u32 mclk = 0, div = 0; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 542 | |
| 543 | div = stb0899_read_reg(state, STB0899_NCOARSE); |
| 544 | mclk = (div + 1) * state->config->xtal_freq / 6; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 545 | dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 546 | |
| 547 | return mclk; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * stb0899_set_mclk |
| 552 | * Set STB0899 master Clock frequency |
| 553 | * Mclk: demodulator master clock |
| 554 | * ExtClk: external clock frequency (Hz) |
| 555 | */ |
| 556 | static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk) |
| 557 | { |
| 558 | struct stb0899_internal *internal = &state->internal; |
| 559 | u8 mdiv = 0; |
| 560 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 561 | dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 562 | mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 563 | dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 564 | |
| 565 | stb0899_write_reg(state, STB0899_NCOARSE, mdiv); |
| 566 | internal->master_clk = stb0899_get_mclk(state); |
| 567 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 568 | dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 569 | } |
| 570 | |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 571 | static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable) |
| 572 | { |
| 573 | struct stb0899_config *config = state->config; |
Manu Abraham | 043a68b | 2008-01-18 14:15:17 -0300 | [diff] [blame] | 574 | const struct stb0899_postproc *postproc = config->postproc; |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 575 | |
| 576 | /* post process event */ |
| 577 | if (postproc) { |
| 578 | if (enable) { |
Manu Abraham | 9efdd29 | 2007-10-30 20:05:59 -0300 | [diff] [blame] | 579 | if (postproc[ctl].level == STB0899_GPIOPULLUP) |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 580 | stb0899_write_reg(state, postproc[ctl].gpio, 0x02); |
| 581 | else |
| 582 | stb0899_write_reg(state, postproc[ctl].gpio, 0x82); |
| 583 | } else { |
Manu Abraham | 9efdd29 | 2007-10-30 20:05:59 -0300 | [diff] [blame] | 584 | if (postproc[ctl].level == STB0899_GPIOPULLUP) |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 585 | stb0899_write_reg(state, postproc[ctl].gpio, 0x82); |
| 586 | else |
| 587 | stb0899_write_reg(state, postproc[ctl].gpio, 0x02); |
| 588 | } |
| 589 | } |
| 590 | return 0; |
| 591 | } |
| 592 | |
Max Kellermann | f686c14 | 2016-08-09 18:32:46 -0300 | [diff] [blame] | 593 | static void stb0899_detach(struct dvb_frontend *fe) |
| 594 | { |
| 595 | struct stb0899_state *state = fe->demodulator_priv; |
| 596 | |
| 597 | /* post process event */ |
| 598 | stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0); |
| 599 | } |
| 600 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 601 | static void stb0899_release(struct dvb_frontend *fe) |
| 602 | { |
| 603 | struct stb0899_state *state = fe->demodulator_priv; |
| 604 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 605 | dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 606 | kfree(state); |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * stb0899_get_alpha |
| 611 | * return: rolloff |
| 612 | */ |
| 613 | static int stb0899_get_alpha(struct stb0899_state *state) |
| 614 | { |
| 615 | u8 mode_coeff; |
| 616 | |
| 617 | mode_coeff = stb0899_read_reg(state, STB0899_DEMOD); |
| 618 | |
| 619 | if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1) |
| 620 | return 20; |
| 621 | else |
| 622 | return 35; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * stb0899_init_calc |
| 627 | */ |
| 628 | static void stb0899_init_calc(struct stb0899_state *state) |
| 629 | { |
| 630 | struct stb0899_internal *internal = &state->internal; |
| 631 | int master_clk; |
Marko Schluessler | 7d8f1e5 | 2007-10-23 19:56:18 -0300 | [diff] [blame] | 632 | u8 agc[2]; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 633 | u32 reg; |
| 634 | |
| 635 | /* Read registers (in burst mode) */ |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 636 | stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */ |
| 637 | |
| 638 | /* Initial calculations */ |
| 639 | master_clk = stb0899_get_mclk(state); |
| 640 | internal->t_agc1 = 0; |
| 641 | internal->t_agc2 = 0; |
| 642 | internal->master_clk = master_clk; |
| 643 | internal->mclk = master_clk / 65536L; |
| 644 | internal->rolloff = stb0899_get_alpha(state); |
| 645 | |
| 646 | /* DVBS2 Initial calculations */ |
| 647 | /* Set AGC value to the middle */ |
| 648 | internal->agc_gain = 8154; |
| 649 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL); |
| 650 | STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain); |
| 651 | stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg); |
| 652 | |
| 653 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA); |
| 654 | internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg); |
| 655 | |
| 656 | internal->center_freq = 0; |
| 657 | internal->av_frame_coarse = 10; |
| 658 | internal->av_frame_fine = 20; |
| 659 | internal->step_size = 2; |
| 660 | /* |
| 661 | if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO)) |
| 662 | pParams->IQLocked = 0; |
| 663 | else |
| 664 | pParams->IQLocked = 1; |
| 665 | */ |
| 666 | } |
| 667 | |
| 668 | static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout) |
| 669 | { |
| 670 | u8 reg = 0; |
| 671 | unsigned long start = jiffies; |
| 672 | |
| 673 | while (1) { |
| 674 | reg = stb0899_read_reg(state, STB0899_DISSTATUS); |
| 675 | if (!STB0899_GETFIELD(FIFOFULL, reg)) |
| 676 | break; |
Asaf Vertz | 3edd59a | 2014-12-14 03:49:47 -0300 | [diff] [blame] | 677 | if (time_after(jiffies, start + timeout)) { |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 678 | dprintk(state->verbose, FE_ERROR, 1, "timed out !!"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 679 | return -ETIMEDOUT; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd) |
| 687 | { |
| 688 | struct stb0899_state *state = fe->demodulator_priv; |
| 689 | u8 reg, i; |
| 690 | |
Mauro Carvalho Chehab | b9f62ff | 2014-11-05 09:34:12 -0200 | [diff] [blame] | 691 | if (cmd->msg_len > sizeof(cmd->msg)) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 692 | return -EINVAL; |
| 693 | |
| 694 | /* enable FIFO precharge */ |
| 695 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); |
| 696 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1); |
| 697 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); |
| 698 | for (i = 0; i < cmd->msg_len; i++) { |
| 699 | /* wait for FIFO empty */ |
Hans Petter Selasky | aad04c7 | 2011-05-23 11:21:47 -0300 | [diff] [blame] | 700 | if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 701 | return -ETIMEDOUT; |
| 702 | |
| 703 | stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]); |
| 704 | } |
| 705 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); |
| 706 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0); |
| 707 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); |
Manu Abraham | 1697c8d | 2010-11-14 16:01:47 -0300 | [diff] [blame] | 708 | msleep(100); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout) |
| 713 | { |
| 714 | u8 reg = 0; |
| 715 | unsigned long start = jiffies; |
| 716 | |
| 717 | while (!STB0899_GETFIELD(RXEND, reg)) { |
| 718 | reg = stb0899_read_reg(state, STB0899_DISRX_ST0); |
Asaf Vertz | 3edd59a | 2014-12-14 03:49:47 -0300 | [diff] [blame] | 719 | if (time_after(jiffies, start + timeout)) { |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 720 | dprintk(state->verbose, FE_ERROR, 1, "timed out!!"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 721 | return -ETIMEDOUT; |
| 722 | } |
| 723 | msleep(10); |
| 724 | } |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply) |
| 730 | { |
| 731 | struct stb0899_state *state = fe->demodulator_priv; |
| 732 | u8 reg, length = 0, i; |
| 733 | int result; |
| 734 | |
| 735 | if (stb0899_wait_diseqc_rxidle(state, 100) < 0) |
| 736 | return -ETIMEDOUT; |
| 737 | |
| 738 | reg = stb0899_read_reg(state, STB0899_DISRX_ST0); |
| 739 | if (STB0899_GETFIELD(RXEND, reg)) { |
| 740 | |
| 741 | reg = stb0899_read_reg(state, STB0899_DISRX_ST1); |
| 742 | length = STB0899_GETFIELD(FIFOBYTENBR, reg); |
| 743 | |
| 744 | if (length > sizeof (reply->msg)) { |
| 745 | result = -EOVERFLOW; |
| 746 | goto exit; |
| 747 | } |
| 748 | reply->msg_len = length; |
| 749 | |
| 750 | /* extract data */ |
| 751 | for (i = 0; i < length; i++) |
| 752 | reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO); |
| 753 | } |
| 754 | |
| 755 | return 0; |
| 756 | exit: |
| 757 | |
| 758 | return result; |
| 759 | } |
| 760 | |
| 761 | static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout) |
| 762 | { |
| 763 | u8 reg = 0; |
| 764 | unsigned long start = jiffies; |
| 765 | |
| 766 | while (!STB0899_GETFIELD(TXIDLE, reg)) { |
| 767 | reg = stb0899_read_reg(state, STB0899_DISSTATUS); |
Asaf Vertz | 3edd59a | 2014-12-14 03:49:47 -0300 | [diff] [blame] | 768 | if (time_after(jiffies, start + timeout)) { |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 769 | dprintk(state->verbose, FE_ERROR, 1, "timed out!!"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 770 | return -ETIMEDOUT; |
| 771 | } |
| 772 | msleep(10); |
| 773 | } |
| 774 | return 0; |
| 775 | } |
| 776 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 777 | static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, |
| 778 | enum fe_sec_mini_cmd burst) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 779 | { |
| 780 | struct stb0899_state *state = fe->demodulator_priv; |
| 781 | u8 reg, old_state; |
| 782 | |
| 783 | /* wait for diseqc idle */ |
| 784 | if (stb0899_wait_diseqc_txidle(state, 100) < 0) |
| 785 | return -ETIMEDOUT; |
| 786 | |
| 787 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); |
| 788 | old_state = reg; |
| 789 | /* set to burst mode */ |
Sigmund Augdal | d284e4f | 2009-01-26 19:08:48 -0300 | [diff] [blame] | 790 | STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 791 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01); |
| 792 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); |
| 793 | switch (burst) { |
| 794 | case SEC_MINI_A: |
| 795 | /* unmodulated */ |
| 796 | stb0899_write_reg(state, STB0899_DISFIFO, 0x00); |
| 797 | break; |
| 798 | case SEC_MINI_B: |
| 799 | /* modulated */ |
| 800 | stb0899_write_reg(state, STB0899_DISFIFO, 0xff); |
| 801 | break; |
| 802 | } |
| 803 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); |
| 804 | STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00); |
| 805 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); |
| 806 | /* wait for diseqc idle */ |
| 807 | if (stb0899_wait_diseqc_txidle(state, 100) < 0) |
| 808 | return -ETIMEDOUT; |
| 809 | |
| 810 | /* restore state */ |
| 811 | stb0899_write_reg(state, STB0899_DISCNTRL1, old_state); |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
Manu Abraham | 6c1022c | 2007-10-05 11:41:19 -0300 | [diff] [blame] | 816 | static int stb0899_diseqc_init(struct stb0899_state *state) |
| 817 | { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 818 | /* |
Manu Abraham | 6c1022c | 2007-10-05 11:41:19 -0300 | [diff] [blame] | 819 | struct dvb_diseqc_slave_reply rx_data; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 820 | */ |
Hans Verkuil | 5becbc5 | 2012-05-14 10:22:58 -0300 | [diff] [blame] | 821 | u8 f22_tx, reg; |
Manu Abraham | 6c1022c | 2007-10-05 11:41:19 -0300 | [diff] [blame] | 822 | |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 823 | u32 mclk, tx_freq = 22000;/* count = 0, i; */ |
Manu Abraham | 6c1022c | 2007-10-05 11:41:19 -0300 | [diff] [blame] | 824 | reg = stb0899_read_reg(state, STB0899_DISCNTRL2); |
| 825 | STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0); |
| 826 | stb0899_write_reg(state, STB0899_DISCNTRL2, reg); |
| 827 | |
| 828 | /* disable Tx spy */ |
| 829 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); |
| 830 | STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1); |
| 831 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); |
| 832 | |
| 833 | reg = stb0899_read_reg(state, STB0899_DISCNTRL1); |
| 834 | STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0); |
| 835 | stb0899_write_reg(state, STB0899_DISCNTRL1, reg); |
| 836 | |
| 837 | mclk = stb0899_get_mclk(state); |
| 838 | f22_tx = mclk / (tx_freq * 32); |
| 839 | stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */ |
| 840 | state->rx_freq = 20000; |
Manu Abraham | 6c1022c | 2007-10-05 11:41:19 -0300 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 844 | |
| 845 | static int stb0899_sleep(struct dvb_frontend *fe) |
| 846 | { |
| 847 | struct stb0899_state *state = fe->demodulator_priv; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 848 | /* |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 849 | u8 reg; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 850 | */ |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 851 | dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))"); |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 852 | /* post process event */ |
| 853 | stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0); |
| 854 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static int stb0899_wakeup(struct dvb_frontend *fe) |
| 859 | { |
| 860 | int rc; |
| 861 | struct stb0899_state *state = fe->demodulator_priv; |
| 862 | |
Manu Abraham | de29eb8 | 2007-09-22 21:39:17 -0300 | [diff] [blame] | 863 | if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI))) |
| 864 | return rc; |
| 865 | /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */ |
| 866 | if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00))) |
| 867 | return rc; |
| 868 | if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00))) |
| 869 | return rc; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 870 | |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 871 | /* post process event */ |
| 872 | stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1); |
| 873 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | static int stb0899_init(struct dvb_frontend *fe) |
| 878 | { |
| 879 | int i; |
| 880 | struct stb0899_state *state = fe->demodulator_priv; |
| 881 | struct stb0899_config *config = state->config; |
| 882 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 883 | dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... "); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 884 | |
| 885 | /* init device */ |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 886 | dprintk(state->verbose, FE_DEBUG, 1, "init device"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 887 | for (i = 0; config->init_dev[i].address != 0xffff; i++) |
| 888 | stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data); |
| 889 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 890 | dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 891 | /* init S2 demod */ |
| 892 | for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++) |
| 893 | stb0899_write_s2reg(state, STB0899_S2DEMOD, |
| 894 | config->init_s2_demod[i].base_address, |
| 895 | config->init_s2_demod[i].offset, |
| 896 | config->init_s2_demod[i].data); |
| 897 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 898 | dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 899 | /* init S1 demod */ |
| 900 | for (i = 0; config->init_s1_demod[i].address != 0xffff; i++) |
| 901 | stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data); |
| 902 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 903 | dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 904 | /* init S2 fec */ |
| 905 | for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++) |
| 906 | stb0899_write_s2reg(state, STB0899_S2FEC, |
| 907 | config->init_s2_fec[i].base_address, |
| 908 | config->init_s2_fec[i].offset, |
| 909 | config->init_s2_fec[i].data); |
| 910 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 911 | dprintk(state->verbose, FE_DEBUG, 1, "init TST"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 912 | /* init test */ |
| 913 | for (i = 0; config->init_tst[i].address != 0xffff; i++) |
| 914 | stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data); |
| 915 | |
| 916 | stb0899_init_calc(state); |
Manu Abraham | 6c1022c | 2007-10-05 11:41:19 -0300 | [diff] [blame] | 917 | stb0899_diseqc_init(state); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val) |
| 923 | { |
| 924 | int res = 0; |
| 925 | int min = 0, med; |
| 926 | |
| 927 | if (val < tab[min].read) |
| 928 | res = tab[min].real; |
| 929 | else if (val >= tab[max].read) |
| 930 | res = tab[max].real; |
| 931 | else { |
| 932 | while ((max - min) > 1) { |
| 933 | med = (max + min) / 2; |
| 934 | if (val >= tab[min].read && val < tab[med].read) |
| 935 | max = med; |
| 936 | else |
| 937 | min = med; |
| 938 | } |
| 939 | res = ((val - tab[min].read) * |
| 940 | (tab[max].real - tab[min].real) / |
| 941 | (tab[max].read - tab[min].read)) + |
| 942 | tab[min].real; |
| 943 | } |
| 944 | |
| 945 | return res; |
| 946 | } |
| 947 | |
| 948 | static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 949 | { |
| 950 | struct stb0899_state *state = fe->demodulator_priv; |
| 951 | struct stb0899_internal *internal = &state->internal; |
| 952 | |
| 953 | int val; |
| 954 | u32 reg; |
Klaus Schmidinger | 0e37781 | 2012-01-24 05:33:53 -0300 | [diff] [blame] | 955 | *strength = 0; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 956 | switch (state->delsys) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 957 | case SYS_DVBS: |
| 958 | case SYS_DSS: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 959 | if (internal->lock) { |
| 960 | reg = stb0899_read_reg(state, STB0899_VSTATUS); |
| 961 | if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) { |
| 962 | |
| 963 | reg = stb0899_read_reg(state, STB0899_AGCIQIN); |
| 964 | val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg); |
| 965 | |
| 966 | *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val); |
| 967 | *strength += 750; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 968 | dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm", |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 969 | val & 0xff, *strength); |
| 970 | } |
| 971 | } |
| 972 | break; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 973 | case SYS_DVBS2: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 974 | if (internal->lock) { |
Andreas Regel | ef3d23de | 2012-02-28 14:40:44 -0300 | [diff] [blame] | 975 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 976 | val = STB0899_GETFIELD(IF_AGC_GAIN, reg); |
| 977 | |
| 978 | *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val); |
Klaus Schmidinger | 0e37781 | 2012-01-24 05:33:53 -0300 | [diff] [blame] | 979 | *strength += 950; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 980 | dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm", |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 981 | val & 0x3fff, *strength); |
| 982 | } |
| 983 | break; |
| 984 | default: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 985 | dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system"); |
Manu Abraham | 417dd69 | 2007-10-10 06:12:13 -0300 | [diff] [blame] | 986 | return -EINVAL; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 993 | { |
| 994 | struct stb0899_state *state = fe->demodulator_priv; |
| 995 | struct stb0899_internal *internal = &state->internal; |
| 996 | |
| 997 | unsigned int val, quant, quantn = -1, est, estn = -1; |
| 998 | u8 buf[2]; |
| 999 | u32 reg; |
| 1000 | |
Klaus Schmidinger | 0e37781 | 2012-01-24 05:33:53 -0300 | [diff] [blame] | 1001 | *snr = 0; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1002 | reg = stb0899_read_reg(state, STB0899_VSTATUS); |
| 1003 | switch (state->delsys) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1004 | case SYS_DVBS: |
| 1005 | case SYS_DSS: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1006 | if (internal->lock) { |
| 1007 | if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) { |
| 1008 | |
| 1009 | stb0899_read_regs(state, STB0899_NIRM, buf, 2); |
| 1010 | val = MAKEWORD16(buf[0], buf[1]); |
| 1011 | |
| 1012 | *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val); |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1013 | dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n", |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1014 | buf[0], buf[1], val, *snr); |
| 1015 | } |
| 1016 | } |
| 1017 | break; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1018 | case SYS_DVBS2: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1019 | if (internal->lock) { |
| 1020 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1); |
| 1021 | quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg); |
| 1022 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2); |
| 1023 | est = STB0899_GETFIELD(ESN0_EST, reg); |
| 1024 | if (est == 1) |
| 1025 | val = 301; /* C/N = 30.1 dB */ |
| 1026 | else if (est == 2) |
| 1027 | val = 270; /* C/N = 27.0 dB */ |
| 1028 | else { |
| 1029 | /* quantn = 100 * log(quant^2) */ |
| 1030 | quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100); |
| 1031 | /* estn = 100 * log(est) */ |
| 1032 | estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est); |
| 1033 | /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */ |
| 1034 | val = (quantn - estn) / 10; |
| 1035 | } |
| 1036 | *snr = val; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1037 | dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm", |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1038 | quant, quantn, est, estn, val); |
| 1039 | } |
| 1040 | break; |
| 1041 | default: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1042 | dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system"); |
Manu Abraham | 417dd69 | 2007-10-10 06:12:13 -0300 | [diff] [blame] | 1043 | return -EINVAL; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status) |
| 1050 | { |
| 1051 | struct stb0899_state *state = fe->demodulator_priv; |
| 1052 | struct stb0899_internal *internal = &state->internal; |
| 1053 | u8 reg; |
| 1054 | *status = 0; |
| 1055 | |
| 1056 | switch (state->delsys) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1057 | case SYS_DVBS: |
| 1058 | case SYS_DSS: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1059 | dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS"); |
| 1060 | if (internal->lock) { |
| 1061 | reg = stb0899_read_reg(state, STB0899_VSTATUS); |
| 1062 | if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) { |
| 1063 | dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK"); |
Andreas Regel | 91caad3 | 2012-02-28 14:40:40 -0300 | [diff] [blame] | 1064 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1065 | |
| 1066 | reg = stb0899_read_reg(state, STB0899_PLPARM); |
| 1067 | if (STB0899_GETFIELD(VITCURPUN, reg)) { |
| 1068 | dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC"); |
| 1069 | *status |= FE_HAS_VITERBI | FE_HAS_SYNC; |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 1070 | /* post process event */ |
| 1071 | stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | break; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1076 | case SYS_DVBS2: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1077 | dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2"); |
| 1078 | if (internal->lock) { |
| 1079 | reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2); |
| 1080 | if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) { |
| 1081 | *status |= FE_HAS_CARRIER; |
| 1082 | dprintk(state->verbose, FE_DEBUG, 1, |
| 1083 | "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER"); |
| 1084 | |
| 1085 | reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1); |
| 1086 | if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) { |
| 1087 | *status |= FE_HAS_LOCK; |
| 1088 | dprintk(state->verbose, FE_DEBUG, 1, |
| 1089 | "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK"); |
| 1090 | |
| 1091 | } |
| 1092 | if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) { |
| 1093 | *status |= FE_HAS_VITERBI; |
| 1094 | dprintk(state->verbose, FE_DEBUG, 1, |
| 1095 | "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI"); |
| 1096 | } |
| 1097 | if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) { |
| 1098 | *status |= FE_HAS_SYNC; |
| 1099 | dprintk(state->verbose, FE_DEBUG, 1, |
| 1100 | "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC"); |
Manu Abraham | b168e35 | 2007-10-30 19:46:49 -0300 | [diff] [blame] | 1101 | /* post process event */ |
| 1102 | stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | break; |
| 1107 | default: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1108 | dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system"); |
Manu Abraham | 417dd69 | 2007-10-10 06:12:13 -0300 | [diff] [blame] | 1109 | return -EINVAL; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1110 | } |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
| 1115 | * stb0899_get_error |
| 1116 | * viterbi error for DVB-S/DSS |
| 1117 | * packet error for DVB-S2 |
| 1118 | * Bit Error Rate or Packet Error Rate * 10 ^ 7 |
| 1119 | */ |
| 1120 | static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 1121 | { |
| 1122 | struct stb0899_state *state = fe->demodulator_priv; |
| 1123 | struct stb0899_internal *internal = &state->internal; |
| 1124 | |
| 1125 | u8 lsb, msb; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1126 | |
| 1127 | *ber = 0; |
| 1128 | |
| 1129 | switch (state->delsys) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1130 | case SYS_DVBS: |
| 1131 | case SYS_DSS: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1132 | if (internal->lock) { |
Klaus Schmidinger | fdaaee6 | 2012-06-02 11:04:17 -0300 | [diff] [blame] | 1133 | lsb = stb0899_read_reg(state, STB0899_ECNT1L); |
| 1134 | msb = stb0899_read_reg(state, STB0899_ECNT1M); |
| 1135 | *ber = MAKEWORD16(msb, lsb); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1136 | /* Viterbi Check */ |
| 1137 | if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) { |
| 1138 | /* Error Rate */ |
| 1139 | *ber *= 9766; |
| 1140 | /* ber = ber * 10 ^ 7 */ |
| 1141 | *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl)))); |
| 1142 | *ber /= 8; |
| 1143 | } |
| 1144 | } |
| 1145 | break; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1146 | case SYS_DVBS2: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1147 | if (internal->lock) { |
Klaus Schmidinger | fdaaee6 | 2012-06-02 11:04:17 -0300 | [diff] [blame] | 1148 | lsb = stb0899_read_reg(state, STB0899_ECNT1L); |
| 1149 | msb = stb0899_read_reg(state, STB0899_ECNT1M); |
| 1150 | *ber = MAKEWORD16(msb, lsb); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1151 | /* ber = ber * 10 ^ 7 */ |
| 1152 | *ber *= 10000000; |
| 1153 | *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl)))); |
| 1154 | } |
| 1155 | break; |
| 1156 | default: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1157 | dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system"); |
Manu Abraham | 417dd69 | 2007-10-10 06:12:13 -0300 | [diff] [blame] | 1158 | return -EINVAL; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 1164 | static int stb0899_set_voltage(struct dvb_frontend *fe, |
| 1165 | enum fe_sec_voltage voltage) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1166 | { |
| 1167 | struct stb0899_state *state = fe->demodulator_priv; |
| 1168 | |
| 1169 | switch (voltage) { |
| 1170 | case SEC_VOLTAGE_13: |
| 1171 | stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82); |
| 1172 | stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02); |
| 1173 | stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00); |
| 1174 | break; |
| 1175 | case SEC_VOLTAGE_18: |
| 1176 | stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02); |
| 1177 | stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02); |
| 1178 | stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82); |
| 1179 | break; |
| 1180 | case SEC_VOLTAGE_OFF: |
| 1181 | stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82); |
| 1182 | stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82); |
| 1183 | stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82); |
| 1184 | break; |
| 1185 | default: |
| 1186 | return -EINVAL; |
| 1187 | } |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 1192 | static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1193 | { |
| 1194 | struct stb0899_state *state = fe->demodulator_priv; |
| 1195 | struct stb0899_internal *internal = &state->internal; |
| 1196 | |
Manu Abraham | baa40e4 | 2007-10-05 09:04:32 -0300 | [diff] [blame] | 1197 | u8 div, reg; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1198 | |
| 1199 | /* wait for diseqc idle */ |
| 1200 | if (stb0899_wait_diseqc_txidle(state, 100) < 0) |
| 1201 | return -ETIMEDOUT; |
| 1202 | |
| 1203 | switch (tone) { |
| 1204 | case SEC_TONE_ON: |
| 1205 | div = (internal->master_clk / 100) / 5632; |
| 1206 | div = (div + 5) / 10; |
| 1207 | stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66); |
Manu Abraham | baa40e4 | 2007-10-05 09:04:32 -0300 | [diff] [blame] | 1208 | reg = stb0899_read_reg(state, STB0899_ACRPRESC); |
| 1209 | STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03); |
| 1210 | stb0899_write_reg(state, STB0899_ACRPRESC, reg); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1211 | stb0899_write_reg(state, STB0899_ACRDIV1, div); |
| 1212 | break; |
| 1213 | case SEC_TONE_OFF: |
| 1214 | stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20); |
| 1215 | break; |
| 1216 | default: |
Manu Abraham | 417dd69 | 2007-10-10 06:12:13 -0300 | [diff] [blame] | 1217 | return -EINVAL; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1218 | } |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
Manu Abraham | 40e8ce3 | 2008-02-03 19:37:02 -0300 | [diff] [blame] | 1222 | int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1223 | { |
| 1224 | int i2c_stat; |
| 1225 | struct stb0899_state *state = fe->demodulator_priv; |
| 1226 | |
| 1227 | i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT); |
| 1228 | if (i2c_stat < 0) |
| 1229 | goto err; |
| 1230 | |
| 1231 | if (enable) { |
| 1232 | dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ..."); |
| 1233 | i2c_stat |= STB0899_I2CTON; |
| 1234 | if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0) |
| 1235 | goto err; |
Manu Abraham | 40e8ce3 | 2008-02-03 19:37:02 -0300 | [diff] [blame] | 1236 | } else { |
| 1237 | dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ..."); |
| 1238 | i2c_stat &= ~STB0899_I2CTON; |
| 1239 | if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0) |
| 1240 | goto err; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1241 | } |
| 1242 | return 0; |
| 1243 | err: |
Manu Abraham | 40e8ce3 | 2008-02-03 19:37:02 -0300 | [diff] [blame] | 1244 | dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1245 | return -EREMOTEIO; |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | static inline void CONVERT32(u32 x, char *str) |
| 1250 | { |
| 1251 | *str++ = (x >> 24) & 0xff; |
| 1252 | *str++ = (x >> 16) & 0xff; |
| 1253 | *str++ = (x >> 8) & 0xff; |
| 1254 | *str++ = (x >> 0) & 0xff; |
| 1255 | *str = '\0'; |
| 1256 | } |
| 1257 | |
Mauro Carvalho Chehab | 5506bcb | 2012-10-27 11:25:28 -0300 | [diff] [blame] | 1258 | static int stb0899_get_dev_id(struct stb0899_state *state) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1259 | { |
| 1260 | u8 chip_id, release; |
| 1261 | u16 id; |
| 1262 | u32 demod_ver = 0, fec_ver = 0; |
Reinhard Nissl | 5613741 | 2008-01-21 16:17:48 -0300 | [diff] [blame] | 1263 | char demod_str[5] = { 0 }; |
| 1264 | char fec_str[5] = { 0 }; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1265 | |
| 1266 | id = stb0899_read_reg(state, STB0899_DEV_ID); |
| 1267 | dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id); |
| 1268 | chip_id = STB0899_GETFIELD(CHIP_ID, id); |
| 1269 | release = STB0899_GETFIELD(CHIP_REL, id); |
| 1270 | |
| 1271 | dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]", |
| 1272 | chip_id, release); |
| 1273 | |
| 1274 | CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str); |
| 1275 | |
| 1276 | demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID); |
| 1277 | dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver); |
| 1278 | CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str); |
| 1279 | fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG); |
| 1280 | if (! (chip_id > 0)) { |
| 1281 | dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899"); |
| 1282 | |
| 1283 | return -ENODEV; |
| 1284 | } |
| 1285 | dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver); |
| 1286 | |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
Manu Abraham | e399a78 | 2008-03-08 20:28:09 -0300 | [diff] [blame] | 1290 | static void stb0899_set_delivery(struct stb0899_state *state) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1291 | { |
| 1292 | u8 reg; |
| 1293 | u8 stop_clk[2]; |
| 1294 | |
| 1295 | stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1); |
| 1296 | stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2); |
| 1297 | |
| 1298 | switch (state->delsys) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1299 | case SYS_DVBS: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1300 | dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1301 | /* FECM/Viterbi ON */ |
| 1302 | reg = stb0899_read_reg(state, STB0899_FECM); |
| 1303 | STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0); |
| 1304 | STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1); |
| 1305 | stb0899_write_reg(state, STB0899_FECM, reg); |
| 1306 | |
| 1307 | stb0899_write_reg(state, STB0899_RSULC, 0xb1); |
| 1308 | stb0899_write_reg(state, STB0899_TSULC, 0x40); |
| 1309 | stb0899_write_reg(state, STB0899_RSLLC, 0x42); |
| 1310 | stb0899_write_reg(state, STB0899_TSLPL, 0x12); |
| 1311 | |
| 1312 | reg = stb0899_read_reg(state, STB0899_TSTRES); |
| 1313 | STB0899_SETFIELD_VAL(FRESLDPC, reg, 1); |
| 1314 | stb0899_write_reg(state, STB0899_TSTRES, reg); |
| 1315 | |
| 1316 | STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1); |
| 1317 | STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1); |
| 1318 | STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1); |
| 1319 | |
| 1320 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1); |
| 1321 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1); |
| 1322 | |
| 1323 | STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1); |
Manu Abraham | 18527be | 2008-01-25 18:25:13 -0300 | [diff] [blame] | 1324 | STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1325 | |
| 1326 | STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1); |
| 1327 | break; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1328 | case SYS_DVBS2: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1329 | /* FECM/Viterbi OFF */ |
| 1330 | reg = stb0899_read_reg(state, STB0899_FECM); |
| 1331 | STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0); |
| 1332 | STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0); |
| 1333 | stb0899_write_reg(state, STB0899_FECM, reg); |
| 1334 | |
| 1335 | stb0899_write_reg(state, STB0899_RSULC, 0xb1); |
| 1336 | stb0899_write_reg(state, STB0899_TSULC, 0x42); |
| 1337 | stb0899_write_reg(state, STB0899_RSLLC, 0x40); |
| 1338 | stb0899_write_reg(state, STB0899_TSLPL, 0x02); |
| 1339 | |
| 1340 | reg = stb0899_read_reg(state, STB0899_TSTRES); |
| 1341 | STB0899_SETFIELD_VAL(FRESLDPC, reg, 0); |
| 1342 | stb0899_write_reg(state, STB0899_TSTRES, reg); |
| 1343 | |
| 1344 | STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1); |
| 1345 | STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0); |
| 1346 | STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0); |
| 1347 | |
| 1348 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0); |
| 1349 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0); |
| 1350 | |
| 1351 | STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0); |
| 1352 | STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0); |
| 1353 | |
| 1354 | STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0); |
| 1355 | break; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1356 | case SYS_DSS: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1357 | /* FECM/Viterbi ON */ |
| 1358 | reg = stb0899_read_reg(state, STB0899_FECM); |
| 1359 | STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1); |
| 1360 | STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1); |
| 1361 | stb0899_write_reg(state, STB0899_FECM, reg); |
| 1362 | |
| 1363 | stb0899_write_reg(state, STB0899_RSULC, 0xa1); |
| 1364 | stb0899_write_reg(state, STB0899_TSULC, 0x61); |
| 1365 | stb0899_write_reg(state, STB0899_RSLLC, 0x42); |
| 1366 | |
| 1367 | reg = stb0899_read_reg(state, STB0899_TSTRES); |
| 1368 | STB0899_SETFIELD_VAL(FRESLDPC, reg, 1); |
| 1369 | stb0899_write_reg(state, STB0899_TSTRES, reg); |
| 1370 | |
| 1371 | STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1); |
| 1372 | STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1); |
| 1373 | STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1); |
| 1374 | |
| 1375 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1); |
| 1376 | STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1); |
| 1377 | |
| 1378 | STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0); |
| 1379 | |
| 1380 | STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1); |
| 1381 | break; |
| 1382 | default: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1383 | dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1384 | break; |
| 1385 | } |
| 1386 | STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0); |
| 1387 | stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2); |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * stb0899_set_iterations |
| 1392 | * set the LDPC iteration scale function |
| 1393 | */ |
| 1394 | static void stb0899_set_iterations(struct stb0899_state *state) |
| 1395 | { |
| 1396 | struct stb0899_internal *internal = &state->internal; |
| 1397 | struct stb0899_config *config = state->config; |
| 1398 | |
| 1399 | s32 iter_scale; |
| 1400 | u32 reg; |
| 1401 | |
| 1402 | iter_scale = 17 * (internal->master_clk / 1000); |
| 1403 | iter_scale += 410000; |
| 1404 | iter_scale /= (internal->srate / 1000000); |
| 1405 | iter_scale /= 1000; |
| 1406 | |
| 1407 | if (iter_scale > config->ldpc_max_iter) |
| 1408 | iter_scale = config->ldpc_max_iter; |
| 1409 | |
Lutz Sammer | 699cc19 | 2011-05-04 07:27:31 -0300 | [diff] [blame] | 1410 | reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1411 | STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale); |
Lutz Sammer | 699cc19 | 2011-05-04 07:27:31 -0300 | [diff] [blame] | 1412 | stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1413 | } |
| 1414 | |
Mauro Carvalho Chehab | 41da532 | 2011-12-26 18:03:12 -0300 | [diff] [blame] | 1415 | static enum dvbfe_search stb0899_search(struct dvb_frontend *fe) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1416 | { |
| 1417 | struct stb0899_state *state = fe->demodulator_priv; |
| 1418 | struct stb0899_params *i_params = &state->params; |
| 1419 | struct stb0899_internal *internal = &state->internal; |
Manu Abraham | b91a7cb | 2008-03-04 19:19:58 -0300 | [diff] [blame] | 1420 | struct stb0899_config *config = state->config; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1421 | struct dtv_frontend_properties *props = &fe->dtv_property_cache; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1422 | |
| 1423 | u32 SearchRange, gain; |
| 1424 | |
Mauro Carvalho Chehab | 41da532 | 2011-12-26 18:03:12 -0300 | [diff] [blame] | 1425 | i_params->freq = props->frequency; |
| 1426 | i_params->srate = props->symbol_rate; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1427 | state->delsys = props->delivery_system; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1428 | dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1429 | |
Manu Abraham | 763fbaf | 2008-01-18 11:28:48 -0300 | [diff] [blame] | 1430 | SearchRange = 10000000; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1431 | dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1432 | /* checking Search Range is meaningless for a fixed 3 Mhz */ |
| 1433 | if (INRANGE(i_params->srate, 1000000, 45000000)) { |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1434 | dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE"); |
Manu Abraham | e399a78 | 2008-03-08 20:28:09 -0300 | [diff] [blame] | 1435 | stb0899_set_delivery(state); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1436 | |
| 1437 | if (state->config->tuner_set_rfsiggain) { |
| 1438 | if (internal->srate > 15000000) |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1439 | gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */ |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1440 | else if (internal->srate > 5000000) |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1441 | gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */ |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1442 | else |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1443 | gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */ |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1444 | state->config->tuner_set_rfsiggain(fe, gain); |
| 1445 | } |
| 1446 | |
| 1447 | if (i_params->srate <= 5000000) |
Manu Abraham | b91a7cb | 2008-03-04 19:19:58 -0300 | [diff] [blame] | 1448 | stb0899_set_mclk(state, config->lo_clk); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1449 | else |
Manu Abraham | b91a7cb | 2008-03-04 19:19:58 -0300 | [diff] [blame] | 1450 | stb0899_set_mclk(state, config->hi_clk); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1451 | |
| 1452 | switch (state->delsys) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1453 | case SYS_DVBS: |
| 1454 | case SYS_DSS: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1455 | dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1456 | internal->freq = i_params->freq; |
| 1457 | internal->srate = i_params->srate; |
| 1458 | /* |
| 1459 | * search = user search range + |
| 1460 | * 500Khz + |
| 1461 | * 2 * Tuner_step_size + |
| 1462 | * 10% of the symbol rate |
| 1463 | */ |
| 1464 | internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5); |
| 1465 | internal->derot_percent = 30; |
| 1466 | |
| 1467 | /* What to do for tuners having no bandwidth setup ? */ |
Manu Abraham | 40e8ce3 | 2008-02-03 19:37:02 -0300 | [diff] [blame] | 1468 | /* enable tuner I/O */ |
| 1469 | stb0899_i2c_gate_ctrl(&state->frontend, 1); |
| 1470 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1471 | if (state->config->tuner_set_bandwidth) |
Reinhard Nissl | 02ec9d8 | 2008-01-18 20:09:52 -0300 | [diff] [blame] | 1472 | state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1473 | if (state->config->tuner_get_bandwidth) |
| 1474 | state->config->tuner_get_bandwidth(fe, &internal->tuner_bw); |
Manu Abraham | 40e8ce3 | 2008-02-03 19:37:02 -0300 | [diff] [blame] | 1475 | |
| 1476 | /* disable tuner I/O */ |
| 1477 | stb0899_i2c_gate_ctrl(&state->frontend, 0); |
| 1478 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1479 | /* Set DVB-S1 AGC */ |
| 1480 | stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11); |
| 1481 | |
| 1482 | /* Run the search algorithm */ |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1483 | dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo .."); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1484 | if (stb0899_dvbs_algo(state) == RANGEOK) { |
| 1485 | internal->lock = 1; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1486 | dprintk(state->verbose, FE_DEBUG, 1, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1487 | "-------------------------------------> DVB-S LOCK !"); |
| 1488 | |
| 1489 | // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */ |
| 1490 | // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS); |
| 1491 | // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1); |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1492 | // dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status); |
| 1493 | // dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1494 | |
| 1495 | return DVBFE_ALGO_SEARCH_SUCCESS; |
| 1496 | } else { |
| 1497 | internal->lock = 0; |
| 1498 | |
| 1499 | return DVBFE_ALGO_SEARCH_FAILED; |
| 1500 | } |
| 1501 | break; |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1502 | case SYS_DVBS2: |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1503 | internal->freq = i_params->freq; |
| 1504 | internal->srate = i_params->srate; |
| 1505 | internal->srch_range = SearchRange; |
| 1506 | |
Manu Abraham | 40e8ce3 | 2008-02-03 19:37:02 -0300 | [diff] [blame] | 1507 | /* enable tuner I/O */ |
| 1508 | stb0899_i2c_gate_ctrl(&state->frontend, 1); |
| 1509 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1510 | if (state->config->tuner_set_bandwidth) |
Manu Abraham | 763fbaf | 2008-01-18 11:28:48 -0300 | [diff] [blame] | 1511 | state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange)); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1512 | if (state->config->tuner_get_bandwidth) |
| 1513 | state->config->tuner_get_bandwidth(fe, &internal->tuner_bw); |
| 1514 | |
Manu Abraham | 40e8ce3 | 2008-02-03 19:37:02 -0300 | [diff] [blame] | 1515 | /* disable tuner I/O */ |
| 1516 | stb0899_i2c_gate_ctrl(&state->frontend, 0); |
| 1517 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1518 | // pParams->SpectralInv = pSearch->IQ_Inversion; |
| 1519 | |
| 1520 | /* Set DVB-S2 AGC */ |
| 1521 | stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c); |
| 1522 | |
| 1523 | /* Set IterScale =f(MCLK,SYMB) */ |
| 1524 | stb0899_set_iterations(state); |
| 1525 | |
| 1526 | /* Run the search algorithm */ |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1527 | dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo .."); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1528 | if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) { |
| 1529 | internal->lock = 1; |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1530 | dprintk(state->verbose, FE_DEBUG, 1, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1531 | "-------------------------------------> DVB-S2 LOCK !"); |
| 1532 | |
| 1533 | // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */ |
| 1534 | // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS); |
| 1535 | // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1); |
| 1536 | |
| 1537 | return DVBFE_ALGO_SEARCH_SUCCESS; |
| 1538 | } else { |
| 1539 | internal->lock = 0; |
| 1540 | |
| 1541 | return DVBFE_ALGO_SEARCH_FAILED; |
| 1542 | } |
| 1543 | break; |
| 1544 | default: |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1545 | dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system"); |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1546 | return DVBFE_ALGO_SEARCH_INVALID; |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | return DVBFE_ALGO_SEARCH_ERROR; |
| 1551 | } |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1552 | |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 1553 | static int stb0899_get_frontend(struct dvb_frontend *fe, |
| 1554 | struct dtv_frontend_properties *p) |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1555 | { |
| 1556 | struct stb0899_state *state = fe->demodulator_priv; |
| 1557 | struct stb0899_internal *internal = &state->internal; |
| 1558 | |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1559 | dprintk(state->verbose, FE_DEBUG, 1, "Get params"); |
Mauro Carvalho Chehab | 5715836 | 2011-12-26 14:16:52 -0300 | [diff] [blame] | 1560 | p->symbol_rate = internal->srate; |
Reinhard Nissl | 226143f | 2012-08-18 08:59:57 -0300 | [diff] [blame] | 1561 | p->frequency = internal->freq; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1562 | |
| 1563 | return 0; |
| 1564 | } |
| 1565 | |
| 1566 | static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe) |
| 1567 | { |
| 1568 | return DVBFE_ALGO_CUSTOM; |
| 1569 | } |
| 1570 | |
Max Kellermann | bd336e6 | 2016-08-09 18:32:21 -0300 | [diff] [blame] | 1571 | static const struct dvb_frontend_ops stb0899_ops = { |
Mauro Carvalho Chehab | 7581e61 | 2012-01-01 16:11:18 -0300 | [diff] [blame] | 1572 | .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS }, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1573 | .info = { |
Mauro Carvalho Chehab | 6e6a8b5 | 2018-01-04 13:08:56 -0500 | [diff] [blame] | 1574 | .name = "STB0899 Multistandard", |
Mauro Carvalho Chehab | f1b1eab | 2018-07-05 18:59:36 -0400 | [diff] [blame] | 1575 | .frequency_min_hz = 950 * MHz, |
| 1576 | .frequency_max_hz = 2150 * MHz, |
Mauro Carvalho Chehab | 6e6a8b5 | 2018-01-04 13:08:56 -0500 | [diff] [blame] | 1577 | .symbol_rate_min = 5000000, |
| 1578 | .symbol_rate_max = 45000000, |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1579 | |
Mauro Carvalho Chehab | 6e6a8b5 | 2018-01-04 13:08:56 -0500 | [diff] [blame] | 1580 | .caps = FE_CAN_INVERSION_AUTO | |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1581 | FE_CAN_FEC_AUTO | |
Klaus Schmidinger | faed4aa | 2008-12-31 14:13:56 -0300 | [diff] [blame] | 1582 | FE_CAN_2G_MODULATION | |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1583 | FE_CAN_QPSK |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1584 | }, |
| 1585 | |
Max Kellermann | f686c14 | 2016-08-09 18:32:46 -0300 | [diff] [blame] | 1586 | .detach = stb0899_detach, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1587 | .release = stb0899_release, |
| 1588 | .init = stb0899_init, |
| 1589 | .sleep = stb0899_sleep, |
| 1590 | // .wakeup = stb0899_wakeup, |
| 1591 | |
| 1592 | .i2c_gate_ctrl = stb0899_i2c_gate_ctrl, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1593 | |
| 1594 | .get_frontend_algo = stb0899_frontend_algo, |
| 1595 | .search = stb0899_search, |
Mauro Carvalho Chehab | 5715836 | 2011-12-26 14:16:52 -0300 | [diff] [blame] | 1596 | .get_frontend = stb0899_get_frontend, |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 1597 | |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1598 | |
| 1599 | .read_status = stb0899_read_status, |
| 1600 | .read_snr = stb0899_read_snr, |
| 1601 | .read_signal_strength = stb0899_read_signal_strength, |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1602 | .read_ber = stb0899_read_ber, |
| 1603 | |
| 1604 | .set_voltage = stb0899_set_voltage, |
| 1605 | .set_tone = stb0899_set_tone, |
| 1606 | |
| 1607 | .diseqc_send_master_cmd = stb0899_send_diseqc_msg, |
| 1608 | .diseqc_recv_slave_reply = stb0899_recv_slave_reply, |
| 1609 | .diseqc_send_burst = stb0899_send_diseqc_burst, |
| 1610 | }; |
| 1611 | |
| 1612 | struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c) |
| 1613 | { |
| 1614 | struct stb0899_state *state = NULL; |
| 1615 | |
| 1616 | state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL); |
| 1617 | if (state == NULL) |
| 1618 | goto error; |
| 1619 | |
Reinhard Nissl | c615a27 | 2008-07-09 15:33:38 -0300 | [diff] [blame] | 1620 | state->verbose = &verbose; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1621 | state->config = config; |
| 1622 | state->i2c = i2c; |
| 1623 | state->frontend.ops = stb0899_ops; |
| 1624 | state->frontend.demodulator_priv = state; |
Reinhard Nißl | 0c1d2b1 | 2013-06-02 14:59:00 -0300 | [diff] [blame] | 1625 | /* use configured inversion as default -- we'll later autodetect inversion */ |
| 1626 | state->internal.inversion = config->inversion; |
Manu Abraham | 8bd135b | 2007-07-03 09:53:42 -0300 | [diff] [blame] | 1627 | |
| 1628 | stb0899_wakeup(&state->frontend); |
| 1629 | if (stb0899_get_dev_id(state) == -ENODEV) { |
| 1630 | printk("%s: Exiting .. !\n", __func__); |
| 1631 | goto error; |
| 1632 | } |
| 1633 | |
| 1634 | printk("%s: Attaching STB0899 \n", __func__); |
| 1635 | return &state->frontend; |
| 1636 | |
| 1637 | error: |
| 1638 | kfree(state); |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | EXPORT_SYMBOL(stb0899_attach); |
| 1642 | MODULE_PARM_DESC(verbose, "Set Verbosity level"); |
| 1643 | MODULE_AUTHOR("Manu Abraham"); |
| 1644 | MODULE_DESCRIPTION("STB0899 Multi-Std frontend"); |
| 1645 | MODULE_LICENSE("GPL"); |