Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver |
| 3 | * |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 4 | * Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com> |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 5 | * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com> |
| 6 | * |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation version 2. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <asm/div64.h> |
| 19 | |
| 20 | #include "dvb_frontend.h" |
| 21 | #include "mb86a20s.h" |
| 22 | |
| 23 | static int debug = 1; |
| 24 | module_param(debug, int, 0644); |
| 25 | MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)"); |
| 26 | |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 27 | struct mb86a20s_state { |
| 28 | struct i2c_adapter *i2c; |
| 29 | const struct mb86a20s_config *config; |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 30 | u32 last_frequency; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 31 | |
| 32 | struct dvb_frontend frontend; |
Mauro Carvalho Chehab | c736a5f | 2011-01-14 11:10:05 -0300 | [diff] [blame] | 33 | |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 34 | u32 if_freq; |
| 35 | |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 36 | u32 estimated_rate[3]; |
| 37 | |
Mauro Carvalho Chehab | c736a5f | 2011-01-14 11:10:05 -0300 | [diff] [blame] | 38 | bool need_init; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct regdata { |
| 42 | u8 reg; |
| 43 | u8 data; |
| 44 | }; |
| 45 | |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 46 | #define BER_SAMPLING_RATE 1 /* Seconds */ |
| 47 | |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 48 | /* |
| 49 | * Initialization sequence: Use whatevere default values that PV SBTVD |
| 50 | * does on its initialisation, obtained via USB snoop |
| 51 | */ |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 52 | static struct regdata mb86a20s_init1[] = { |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 53 | { 0x70, 0x0f }, |
| 54 | { 0x70, 0xff }, |
| 55 | { 0x08, 0x01 }, |
| 56 | { 0x09, 0x3e }, |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 57 | { 0x50, 0xd1 }, { 0x51, 0x22 }, |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 58 | { 0x39, 0x01 }, |
| 59 | { 0x71, 0x00 }, |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 60 | { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 }, |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static struct regdata mb86a20s_init2[] = { |
Mauro Carvalho Chehab | ebe9674 | 2012-01-11 11:00:28 -0200 | [diff] [blame] | 64 | { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 }, |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 65 | { 0x3b, 0x21 }, |
| 66 | { 0x3c, 0x3a }, |
| 67 | { 0x01, 0x0d }, |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 68 | { 0x04, 0x08 }, { 0x05, 0x05 }, |
| 69 | { 0x04, 0x0e }, { 0x05, 0x00 }, |
| 70 | { 0x04, 0x0f }, { 0x05, 0x14 }, |
| 71 | { 0x04, 0x0b }, { 0x05, 0x8c }, |
| 72 | { 0x04, 0x00 }, { 0x05, 0x00 }, |
| 73 | { 0x04, 0x01 }, { 0x05, 0x07 }, |
| 74 | { 0x04, 0x02 }, { 0x05, 0x0f }, |
| 75 | { 0x04, 0x03 }, { 0x05, 0xa0 }, |
| 76 | { 0x04, 0x09 }, { 0x05, 0x00 }, |
| 77 | { 0x04, 0x0a }, { 0x05, 0xff }, |
| 78 | { 0x04, 0x27 }, { 0x05, 0x64 }, |
| 79 | { 0x04, 0x28 }, { 0x05, 0x00 }, |
| 80 | { 0x04, 0x1e }, { 0x05, 0xff }, |
| 81 | { 0x04, 0x29 }, { 0x05, 0x0a }, |
| 82 | { 0x04, 0x32 }, { 0x05, 0x0a }, |
| 83 | { 0x04, 0x14 }, { 0x05, 0x02 }, |
| 84 | { 0x04, 0x04 }, { 0x05, 0x00 }, |
| 85 | { 0x04, 0x05 }, { 0x05, 0x22 }, |
| 86 | { 0x04, 0x06 }, { 0x05, 0x0e }, |
| 87 | { 0x04, 0x07 }, { 0x05, 0xd8 }, |
| 88 | { 0x04, 0x12 }, { 0x05, 0x00 }, |
| 89 | { 0x04, 0x13 }, { 0x05, 0xff }, |
Mauro Carvalho Chehab | ebe9674 | 2012-01-11 11:00:28 -0200 | [diff] [blame] | 90 | { 0x04, 0x15 }, { 0x05, 0x4e }, |
| 91 | { 0x04, 0x16 }, { 0x05, 0x20 }, |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * On this demod, when the bit count reaches the count below, |
| 95 | * it collects the bit error count. The bit counters are initialized |
| 96 | * to 65535 here. This warrants that all of them will be quickly |
| 97 | * calculated when device gets locked. As TMCC is parsed, the values |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 98 | * will be adjusted later in the driver's code. |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 99 | */ |
| 100 | { 0x52, 0x01 }, /* Turn on BER before Viterbi */ |
| 101 | { 0x50, 0xa7 }, { 0x51, 0x00 }, |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 102 | { 0x50, 0xa8 }, { 0x51, 0xff }, |
| 103 | { 0x50, 0xa9 }, { 0x51, 0xff }, |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 104 | { 0x50, 0xaa }, { 0x51, 0x00 }, |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 105 | { 0x50, 0xab }, { 0x51, 0xff }, |
| 106 | { 0x50, 0xac }, { 0x51, 0xff }, |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 107 | { 0x50, 0xad }, { 0x51, 0x00 }, |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 108 | { 0x50, 0xae }, { 0x51, 0xff }, |
| 109 | { 0x50, 0xaf }, { 0x51, 0xff }, |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 111 | /* |
| 112 | * On this demod, post BER counts blocks. When the count reaches the |
| 113 | * value below, it collects the block error count. The block counters |
| 114 | * are initialized to 127 here. This warrants that all of them will be |
| 115 | * quickly calculated when device gets locked. As TMCC is parsed, the |
| 116 | * values will be adjusted later in the driver's code. |
| 117 | */ |
| 118 | { 0x5e, 0x07 }, /* Turn on BER after Viterbi */ |
| 119 | { 0x50, 0xdc }, { 0x51, 0x00 }, |
| 120 | { 0x50, 0xdd }, { 0x51, 0x7f }, |
| 121 | { 0x50, 0xde }, { 0x51, 0x00 }, |
| 122 | { 0x50, 0xdf }, { 0x51, 0x7f }, |
| 123 | { 0x50, 0xe0 }, { 0x51, 0x00 }, |
| 124 | { 0x50, 0xe1 }, { 0x51, 0x7f }, |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * On this demod, when the block count reaches the count below, |
| 128 | * it collects the block error count. The block counters are initialized |
| 129 | * to 127 here. This warrants that all of them will be quickly |
| 130 | * calculated when device gets locked. As TMCC is parsed, the values |
| 131 | * will be adjusted later in the driver's code. |
| 132 | */ |
| 133 | { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */ |
| 134 | { 0x50, 0xb2 }, { 0x51, 0x00 }, |
| 135 | { 0x50, 0xb3 }, { 0x51, 0x7f }, |
| 136 | { 0x50, 0xb4 }, { 0x51, 0x00 }, |
| 137 | { 0x50, 0xb5 }, { 0x51, 0x7f }, |
| 138 | { 0x50, 0xb6 }, { 0x51, 0x00 }, |
| 139 | { 0x50, 0xb7 }, { 0x51, 0x7f }, |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 140 | |
| 141 | { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */ |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 142 | { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */ |
| 143 | { 0x45, 0x04 }, /* CN symbol 4 */ |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 144 | { 0x48, 0x04 }, /* CN manual mode */ |
| 145 | |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 146 | { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */ |
| 147 | { 0x50, 0xd6 }, { 0x51, 0x1f }, |
| 148 | { 0x50, 0xd2 }, { 0x51, 0x03 }, |
| 149 | { 0x50, 0xd7 }, { 0x51, 0x3f }, |
Mauro Carvalho Chehab | ebe9674 | 2012-01-11 11:00:28 -0200 | [diff] [blame] | 150 | { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 }, |
| 151 | { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c }, |
Mauro Carvalho Chehab | ce77d12 | 2013-01-14 14:12:10 -0300 | [diff] [blame] | 152 | |
| 153 | { 0x04, 0x40 }, { 0x05, 0x00 }, |
Mauro Carvalho Chehab | ebe9674 | 2012-01-11 11:00:28 -0200 | [diff] [blame] | 154 | { 0x28, 0x00 }, { 0x29, 0x10 }, |
| 155 | { 0x28, 0x05 }, { 0x29, 0x02 }, |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 156 | { 0x1c, 0x01 }, |
Mauro Carvalho Chehab | a7025ed | 2012-01-11 10:56:30 -0200 | [diff] [blame] | 157 | { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 }, |
| 158 | { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d }, |
| 159 | { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 }, |
| 160 | { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 }, |
| 161 | { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 }, |
| 162 | { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 }, |
| 163 | { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 }, |
| 164 | { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 }, |
| 165 | { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e }, |
| 166 | { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e }, |
| 167 | { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 }, |
| 168 | { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f }, |
| 169 | { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 }, |
| 170 | { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 }, |
| 171 | { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe }, |
| 172 | { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 }, |
| 173 | { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee }, |
| 174 | { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 }, |
| 175 | { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f }, |
| 176 | { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 }, |
| 177 | { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 }, |
| 178 | { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a }, |
| 179 | { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc }, |
| 180 | { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba }, |
| 181 | { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 }, |
| 182 | { 0x50, 0x1e }, { 0x51, 0x5d }, |
| 183 | { 0x50, 0x22 }, { 0x51, 0x00 }, |
| 184 | { 0x50, 0x23 }, { 0x51, 0xc8 }, |
| 185 | { 0x50, 0x24 }, { 0x51, 0x00 }, |
| 186 | { 0x50, 0x25 }, { 0x51, 0xf0 }, |
| 187 | { 0x50, 0x26 }, { 0x51, 0x00 }, |
| 188 | { 0x50, 0x27 }, { 0x51, 0xc3 }, |
| 189 | { 0x50, 0x39 }, { 0x51, 0x02 }, |
Mauro Carvalho Chehab | ebe9674 | 2012-01-11 11:00:28 -0200 | [diff] [blame] | 190 | { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 }, |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 191 | { 0xd0, 0x00 }, |
| 192 | }; |
| 193 | |
| 194 | static struct regdata mb86a20s_reset_reception[] = { |
| 195 | { 0x70, 0xf0 }, |
| 196 | { 0x70, 0xff }, |
| 197 | { 0x08, 0x01 }, |
| 198 | { 0x08, 0x00 }, |
| 199 | }; |
| 200 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 201 | static struct regdata mb86a20s_per_ber_reset[] = { |
| 202 | { 0x53, 0x00 }, /* pre BER Counter reset */ |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 203 | { 0x53, 0x07 }, |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 204 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 205 | { 0x5f, 0x00 }, /* post BER Counter reset */ |
| 206 | { 0x5f, 0x07 }, |
| 207 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 208 | { 0x50, 0xb1 }, /* PER Counter reset */ |
| 209 | { 0x51, 0x07 }, |
| 210 | { 0x51, 0x00 }, |
| 211 | }; |
| 212 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 213 | /* |
| 214 | * I2C read/write functions and macros |
| 215 | */ |
| 216 | |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 217 | static int mb86a20s_i2c_writereg(struct mb86a20s_state *state, |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 218 | u8 i2c_addr, u8 reg, u8 data) |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 219 | { |
| 220 | u8 buf[] = { reg, data }; |
| 221 | struct i2c_msg msg = { |
| 222 | .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2 |
| 223 | }; |
| 224 | int rc; |
| 225 | |
| 226 | rc = i2c_transfer(state->i2c, &msg, 1); |
| 227 | if (rc != 1) { |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 228 | dev_err(&state->i2c->dev, |
| 229 | "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n", |
| 230 | __func__, rc, reg, data); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 231 | return rc; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state, |
| 238 | u8 i2c_addr, struct regdata *rd, int size) |
| 239 | { |
| 240 | int i, rc; |
| 241 | |
| 242 | for (i = 0; i < size; i++) { |
| 243 | rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg, |
| 244 | rd[i].data); |
| 245 | if (rc < 0) |
| 246 | return rc; |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int mb86a20s_i2c_readreg(struct mb86a20s_state *state, |
| 252 | u8 i2c_addr, u8 reg) |
| 253 | { |
| 254 | u8 val; |
| 255 | int rc; |
| 256 | struct i2c_msg msg[] = { |
| 257 | { .addr = i2c_addr, .flags = 0, .buf = ®, .len = 1 }, |
| 258 | { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 } |
| 259 | }; |
| 260 | |
| 261 | rc = i2c_transfer(state->i2c, msg, 2); |
| 262 | |
| 263 | if (rc != 2) { |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 264 | dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n", |
| 265 | __func__, reg, rc); |
| 266 | return (rc < 0) ? rc : -EIO; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return val; |
| 270 | } |
| 271 | |
| 272 | #define mb86a20s_readreg(state, reg) \ |
| 273 | mb86a20s_i2c_readreg(state, state->config->demod_address, reg) |
| 274 | #define mb86a20s_writereg(state, reg, val) \ |
| 275 | mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val) |
| 276 | #define mb86a20s_writeregdata(state, regdata) \ |
| 277 | mb86a20s_i2c_writeregdata(state, state->config->demod_address, \ |
| 278 | regdata, ARRAY_SIZE(regdata)) |
| 279 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 280 | /* |
| 281 | * Ancillary internal routines (likely compiled inlined) |
| 282 | * |
| 283 | * The functions below assume that gateway lock has already obtained |
| 284 | */ |
| 285 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 286 | static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status) |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 287 | { |
| 288 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 289 | int val; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 290 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 291 | *status = 0; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 292 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 293 | val = mb86a20s_readreg(state, 0x0a) & 0xf; |
| 294 | if (val < 0) |
| 295 | return val; |
Mauro Carvalho Chehab | 68541cd | 2010-10-03 05:27:59 -0300 | [diff] [blame] | 296 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 297 | if (val >= 2) |
| 298 | *status |= FE_HAS_SIGNAL; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 299 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 300 | if (val >= 4) |
| 301 | *status |= FE_HAS_CARRIER; |
Mauro Carvalho Chehab | 7572f9c | 2010-10-03 16:48:49 -0300 | [diff] [blame] | 302 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 303 | if (val >= 5) |
| 304 | *status |= FE_HAS_VITERBI; |
Mauro Carvalho Chehab | 7572f9c | 2010-10-03 16:48:49 -0300 | [diff] [blame] | 305 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 306 | if (val >= 7) |
| 307 | *status |= FE_HAS_SYNC; |
Mauro Carvalho Chehab | 68541cd | 2010-10-03 05:27:59 -0300 | [diff] [blame] | 308 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 309 | if (val >= 8) /* Maybe 9? */ |
| 310 | *status |= FE_HAS_LOCK; |
| 311 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 312 | dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n", |
| 313 | __func__, *status, val); |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 314 | |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 315 | return val; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 316 | } |
| 317 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 318 | static int mb86a20s_read_signal_strength(struct dvb_frontend *fe) |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 319 | { |
| 320 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 321 | int rc; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 322 | unsigned rf_max, rf_min, rf; |
Mauro Carvalho Chehab | 68541cd | 2010-10-03 05:27:59 -0300 | [diff] [blame] | 323 | |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 324 | /* Does a binary search to get RF strength */ |
| 325 | rf_max = 0xfff; |
| 326 | rf_min = 0; |
| 327 | do { |
| 328 | rf = (rf_max + rf_min) / 2; |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 329 | rc = mb86a20s_writereg(state, 0x04, 0x1f); |
| 330 | if (rc < 0) |
| 331 | return rc; |
| 332 | rc = mb86a20s_writereg(state, 0x05, rf >> 8); |
| 333 | if (rc < 0) |
| 334 | return rc; |
| 335 | rc = mb86a20s_writereg(state, 0x04, 0x20); |
| 336 | if (rc < 0) |
| 337 | return rc; |
Mauro Carvalho Chehab | dad78c5 | 2013-03-01 16:15:16 -0300 | [diff] [blame^] | 338 | rc = mb86a20s_writereg(state, 0x05, rf); |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 339 | if (rc < 0) |
| 340 | return rc; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 341 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 342 | rc = mb86a20s_readreg(state, 0x02); |
| 343 | if (rc < 0) |
| 344 | return rc; |
| 345 | if (rc & 0x08) |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 346 | rf_min = (rf_max + rf_min) / 2; |
| 347 | else |
| 348 | rf_max = (rf_max + rf_min) / 2; |
| 349 | if (rf_max - rf_min < 4) { |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 350 | rf = (rf_max + rf_min) / 2; |
| 351 | |
| 352 | /* Rescale it from 2^12 (4096) to 2^16 */ |
| 353 | rf <<= (16 - 12); |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 354 | dev_dbg(&state->i2c->dev, |
| 355 | "%s: signal strength = %d (%d < RF=%d < %d)\n", |
| 356 | __func__, rf, rf_min, rf >> 4, rf_max); |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 357 | return rf; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 358 | } |
| 359 | } while (1); |
| 360 | |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 364 | static int mb86a20s_get_modulation(struct mb86a20s_state *state, |
| 365 | unsigned layer) |
| 366 | { |
| 367 | int rc; |
| 368 | static unsigned char reg[] = { |
| 369 | [0] = 0x86, /* Layer A */ |
| 370 | [1] = 0x8a, /* Layer B */ |
| 371 | [2] = 0x8e, /* Layer C */ |
| 372 | }; |
| 373 | |
Dan Carpenter | 82033bc | 2012-01-13 02:28:34 -0300 | [diff] [blame] | 374 | if (layer >= ARRAY_SIZE(reg)) |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 375 | return -EINVAL; |
| 376 | rc = mb86a20s_writereg(state, 0x6d, reg[layer]); |
| 377 | if (rc < 0) |
| 378 | return rc; |
| 379 | rc = mb86a20s_readreg(state, 0x6e); |
| 380 | if (rc < 0) |
| 381 | return rc; |
Mauro Carvalho Chehab | 0458592 | 2013-01-14 12:31:13 -0300 | [diff] [blame] | 382 | switch ((rc >> 4) & 0x07) { |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 383 | case 0: |
| 384 | return DQPSK; |
| 385 | case 1: |
| 386 | return QPSK; |
| 387 | case 2: |
| 388 | return QAM_16; |
| 389 | case 3: |
| 390 | return QAM_64; |
| 391 | default: |
| 392 | return QAM_AUTO; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static int mb86a20s_get_fec(struct mb86a20s_state *state, |
| 397 | unsigned layer) |
| 398 | { |
| 399 | int rc; |
| 400 | |
| 401 | static unsigned char reg[] = { |
| 402 | [0] = 0x87, /* Layer A */ |
| 403 | [1] = 0x8b, /* Layer B */ |
| 404 | [2] = 0x8f, /* Layer C */ |
| 405 | }; |
| 406 | |
Dan Carpenter | 82033bc | 2012-01-13 02:28:34 -0300 | [diff] [blame] | 407 | if (layer >= ARRAY_SIZE(reg)) |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 408 | return -EINVAL; |
| 409 | rc = mb86a20s_writereg(state, 0x6d, reg[layer]); |
| 410 | if (rc < 0) |
| 411 | return rc; |
| 412 | rc = mb86a20s_readreg(state, 0x6e); |
| 413 | if (rc < 0) |
| 414 | return rc; |
Mauro Carvalho Chehab | 0458592 | 2013-01-14 12:31:13 -0300 | [diff] [blame] | 415 | switch ((rc >> 4) & 0x07) { |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 416 | case 0: |
| 417 | return FEC_1_2; |
| 418 | case 1: |
| 419 | return FEC_2_3; |
| 420 | case 2: |
| 421 | return FEC_3_4; |
| 422 | case 3: |
| 423 | return FEC_5_6; |
| 424 | case 4: |
| 425 | return FEC_7_8; |
| 426 | default: |
| 427 | return FEC_AUTO; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static int mb86a20s_get_interleaving(struct mb86a20s_state *state, |
| 432 | unsigned layer) |
| 433 | { |
| 434 | int rc; |
| 435 | |
| 436 | static unsigned char reg[] = { |
| 437 | [0] = 0x88, /* Layer A */ |
| 438 | [1] = 0x8c, /* Layer B */ |
| 439 | [2] = 0x90, /* Layer C */ |
| 440 | }; |
| 441 | |
Dan Carpenter | 82033bc | 2012-01-13 02:28:34 -0300 | [diff] [blame] | 442 | if (layer >= ARRAY_SIZE(reg)) |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 443 | return -EINVAL; |
| 444 | rc = mb86a20s_writereg(state, 0x6d, reg[layer]); |
| 445 | if (rc < 0) |
| 446 | return rc; |
| 447 | rc = mb86a20s_readreg(state, 0x6e); |
| 448 | if (rc < 0) |
| 449 | return rc; |
Mauro Carvalho Chehab | 0458592 | 2013-01-14 12:31:13 -0300 | [diff] [blame] | 450 | |
| 451 | switch ((rc >> 4) & 0x07) { |
| 452 | case 1: |
| 453 | return GUARD_INTERVAL_1_4; |
| 454 | case 2: |
| 455 | return GUARD_INTERVAL_1_8; |
| 456 | case 3: |
| 457 | return GUARD_INTERVAL_1_16; |
| 458 | case 4: |
| 459 | return GUARD_INTERVAL_1_32; |
| 460 | |
| 461 | default: |
| 462 | case 0: |
| 463 | return GUARD_INTERVAL_AUTO; |
| 464 | } |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static int mb86a20s_get_segment_count(struct mb86a20s_state *state, |
| 468 | unsigned layer) |
| 469 | { |
| 470 | int rc, count; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 471 | static unsigned char reg[] = { |
| 472 | [0] = 0x89, /* Layer A */ |
| 473 | [1] = 0x8d, /* Layer B */ |
| 474 | [2] = 0x91, /* Layer C */ |
| 475 | }; |
| 476 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 477 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 478 | |
Dan Carpenter | 82033bc | 2012-01-13 02:28:34 -0300 | [diff] [blame] | 479 | if (layer >= ARRAY_SIZE(reg)) |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 480 | return -EINVAL; |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 481 | |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 482 | rc = mb86a20s_writereg(state, 0x6d, reg[layer]); |
| 483 | if (rc < 0) |
| 484 | return rc; |
| 485 | rc = mb86a20s_readreg(state, 0x6e); |
| 486 | if (rc < 0) |
| 487 | return rc; |
| 488 | count = (rc >> 4) & 0x0f; |
| 489 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 490 | dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count); |
| 491 | |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 492 | return count; |
| 493 | } |
| 494 | |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 495 | static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe) |
| 496 | { |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 497 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 498 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 499 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 500 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 501 | |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 502 | /* Fixed parameters */ |
| 503 | c->delivery_system = SYS_ISDBT; |
| 504 | c->bandwidth_hz = 6000000; |
| 505 | |
| 506 | /* Initialize values that will be later autodetected */ |
| 507 | c->isdbt_layer_enabled = 0; |
| 508 | c->transmission_mode = TRANSMISSION_MODE_AUTO; |
| 509 | c->guard_interval = GUARD_INTERVAL_AUTO; |
| 510 | c->isdbt_sb_mode = 0; |
| 511 | c->isdbt_sb_segment_count = 0; |
| 512 | } |
| 513 | |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 514 | /* |
| 515 | * Estimates the bit rate using the per-segment bit rate given by |
| 516 | * ABNT/NBR 15601 spec (table 4). |
| 517 | */ |
| 518 | static u32 isdbt_rate[3][5][4] = { |
| 519 | { /* DQPSK/QPSK */ |
| 520 | { 280850, 312060, 330420, 340430 }, /* 1/2 */ |
| 521 | { 374470, 416080, 440560, 453910 }, /* 2/3 */ |
| 522 | { 421280, 468090, 495630, 510650 }, /* 3/4 */ |
| 523 | { 468090, 520100, 550700, 567390 }, /* 5/6 */ |
| 524 | { 491500, 546110, 578230, 595760 }, /* 7/8 */ |
| 525 | }, { /* QAM16 */ |
| 526 | { 561710, 624130, 660840, 680870 }, /* 1/2 */ |
| 527 | { 748950, 832170, 881120, 907820 }, /* 2/3 */ |
| 528 | { 842570, 936190, 991260, 1021300 }, /* 3/4 */ |
| 529 | { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */ |
| 530 | { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */ |
| 531 | }, { /* QAM64 */ |
| 532 | { 842570, 936190, 991260, 1021300 }, /* 1/2 */ |
| 533 | { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */ |
| 534 | { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */ |
| 535 | { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */ |
| 536 | { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */ |
| 537 | } |
| 538 | }; |
| 539 | |
| 540 | static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer, |
| 541 | u32 modulation, u32 fec, u32 interleaving, |
| 542 | u32 segment) |
| 543 | { |
| 544 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 545 | u32 rate; |
| 546 | int m, f, i; |
| 547 | |
| 548 | /* |
| 549 | * If modulation/fec/interleaving is not detected, the default is |
| 550 | * to consider the lowest bit rate, to avoid taking too long time |
| 551 | * to get BER. |
| 552 | */ |
| 553 | switch (modulation) { |
| 554 | case DQPSK: |
| 555 | case QPSK: |
| 556 | default: |
| 557 | m = 0; |
| 558 | break; |
| 559 | case QAM_16: |
| 560 | m = 1; |
| 561 | break; |
| 562 | case QAM_64: |
| 563 | m = 2; |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | switch (fec) { |
| 568 | default: |
| 569 | case FEC_1_2: |
| 570 | case FEC_AUTO: |
| 571 | f = 0; |
| 572 | break; |
| 573 | case FEC_2_3: |
| 574 | f = 1; |
| 575 | break; |
| 576 | case FEC_3_4: |
| 577 | f = 2; |
| 578 | break; |
| 579 | case FEC_5_6: |
| 580 | f = 3; |
| 581 | break; |
| 582 | case FEC_7_8: |
| 583 | f = 4; |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | switch (interleaving) { |
| 588 | default: |
| 589 | case GUARD_INTERVAL_1_4: |
| 590 | i = 0; |
| 591 | break; |
| 592 | case GUARD_INTERVAL_1_8: |
| 593 | i = 1; |
| 594 | break; |
| 595 | case GUARD_INTERVAL_1_16: |
| 596 | i = 2; |
| 597 | break; |
| 598 | case GUARD_INTERVAL_1_32: |
| 599 | i = 3; |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | /* Samples BER at BER_SAMPLING_RATE seconds */ |
| 604 | rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE; |
| 605 | |
| 606 | /* Avoids sampling too quickly or to overflow the register */ |
| 607 | if (rate < 256) |
| 608 | rate = 256; |
| 609 | else if (rate > (1 << 24) - 1) |
| 610 | rate = (1 << 24) - 1; |
| 611 | |
| 612 | dev_dbg(&state->i2c->dev, |
| 613 | "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n", |
| 614 | __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000, |
| 615 | rate, rate); |
| 616 | |
| 617 | state->estimated_rate[i] = rate; |
| 618 | } |
| 619 | |
| 620 | |
Mauro Carvalho Chehab | 7c61d80 | 2011-12-30 11:30:21 -0300 | [diff] [blame] | 621 | static int mb86a20s_get_frontend(struct dvb_frontend *fe) |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 622 | { |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 623 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 624 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 625 | int i, rc; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 626 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 627 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 628 | |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 629 | /* Reset frontend cache to default values */ |
| 630 | mb86a20s_reset_frontend_cache(fe); |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 631 | |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 632 | /* Check for partial reception */ |
| 633 | rc = mb86a20s_writereg(state, 0x6d, 0x85); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 634 | if (rc < 0) |
| 635 | return rc; |
| 636 | rc = mb86a20s_readreg(state, 0x6e); |
| 637 | if (rc < 0) |
| 638 | return rc; |
| 639 | c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 640 | |
| 641 | /* Get per-layer data */ |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 642 | |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 643 | for (i = 0; i < 3; i++) { |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 644 | dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n", |
| 645 | __func__, 'A' + i); |
| 646 | |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 647 | rc = mb86a20s_get_segment_count(state, i); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 648 | if (rc < 0) |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 649 | goto noperlayer_error; |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 650 | if (rc >= 0 && rc < 14) { |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 651 | c->layer[i].segment_count = rc; |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 652 | } else { |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 653 | c->layer[i].segment_count = 0; |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 654 | state->estimated_rate[i] = 0; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 655 | continue; |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 656 | } |
| 657 | c->isdbt_layer_enabled |= 1 << i; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 658 | rc = mb86a20s_get_modulation(state, i); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 659 | if (rc < 0) |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 660 | goto noperlayer_error; |
| 661 | dev_dbg(&state->i2c->dev, "%s: modulation %d.\n", |
| 662 | __func__, rc); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 663 | c->layer[i].modulation = rc; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 664 | rc = mb86a20s_get_fec(state, i); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 665 | if (rc < 0) |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 666 | goto noperlayer_error; |
| 667 | dev_dbg(&state->i2c->dev, "%s: FEC %d.\n", |
| 668 | __func__, rc); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 669 | c->layer[i].fec = rc; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 670 | rc = mb86a20s_get_interleaving(state, i); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 671 | if (rc < 0) |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 672 | goto noperlayer_error; |
| 673 | dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n", |
| 674 | __func__, rc); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 675 | c->layer[i].interleaving = rc; |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 676 | mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation, |
| 677 | c->layer[i].fec, |
| 678 | c->layer[i].interleaving, |
| 679 | c->layer[i].segment_count); |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 680 | } |
| 681 | |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 682 | rc = mb86a20s_writereg(state, 0x6d, 0x84); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 683 | if (rc < 0) |
| 684 | return rc; |
| 685 | if ((rc & 0x60) == 0x20) { |
| 686 | c->isdbt_sb_mode = 1; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 687 | /* At least, one segment should exist */ |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 688 | if (!c->isdbt_sb_segment_count) |
| 689 | c->isdbt_sb_segment_count = 1; |
| 690 | } |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 691 | |
| 692 | /* Get transmission mode and guard interval */ |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 693 | rc = mb86a20s_readreg(state, 0x07); |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 694 | if (rc < 0) |
| 695 | return rc; |
| 696 | if ((rc & 0x60) == 0x20) { |
| 697 | switch (rc & 0x0c >> 2) { |
| 698 | case 0: |
| 699 | c->transmission_mode = TRANSMISSION_MODE_2K; |
| 700 | break; |
| 701 | case 1: |
| 702 | c->transmission_mode = TRANSMISSION_MODE_4K; |
| 703 | break; |
| 704 | case 2: |
| 705 | c->transmission_mode = TRANSMISSION_MODE_8K; |
| 706 | break; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 707 | } |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 708 | } |
| 709 | if (!(rc & 0x10)) { |
| 710 | switch (rc & 0x3) { |
| 711 | case 0: |
| 712 | c->guard_interval = GUARD_INTERVAL_1_4; |
| 713 | break; |
| 714 | case 1: |
| 715 | c->guard_interval = GUARD_INTERVAL_1_8; |
| 716 | break; |
| 717 | case 2: |
| 718 | c->guard_interval = GUARD_INTERVAL_1_16; |
| 719 | break; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 720 | } |
| 721 | } |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 722 | return 0; |
Mauro Carvalho Chehab | 959a119 | 2012-01-09 18:16:36 -0200 | [diff] [blame] | 723 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 724 | noperlayer_error: |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 725 | |
| 726 | /* per-layer info is incomplete; discard all per-layer */ |
| 727 | c->isdbt_layer_enabled = 0; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 728 | |
Mauro Carvalho Chehab | a77cfca | 2013-01-14 09:26:09 -0300 | [diff] [blame] | 729 | return rc; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 730 | } |
| 731 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 732 | static int mb86a20s_reset_counters(struct dvb_frontend *fe) |
| 733 | { |
| 734 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 735 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 736 | int rc, val; |
| 737 | |
| 738 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 739 | |
| 740 | /* Reset the counters, if the channel changed */ |
| 741 | if (state->last_frequency != c->frequency) { |
| 742 | memset(&c->strength, 0, sizeof(c->strength)); |
| 743 | memset(&c->cnr, 0, sizeof(c->cnr)); |
| 744 | memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error)); |
| 745 | memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count)); |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 746 | memset(&c->post_bit_error, 0, sizeof(c->post_bit_error)); |
| 747 | memset(&c->post_bit_count, 0, sizeof(c->post_bit_count)); |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 748 | memset(&c->block_error, 0, sizeof(c->block_error)); |
| 749 | memset(&c->block_count, 0, sizeof(c->block_count)); |
| 750 | |
| 751 | state->last_frequency = c->frequency; |
| 752 | } |
| 753 | |
| 754 | /* Clear status for most stats */ |
| 755 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 756 | /* BER/PER counter reset */ |
| 757 | rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset); |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 758 | if (rc < 0) |
| 759 | goto err; |
| 760 | |
| 761 | /* CNR counter reset */ |
| 762 | rc = mb86a20s_readreg(state, 0x45); |
| 763 | if (rc < 0) |
| 764 | goto err; |
| 765 | val = rc; |
| 766 | rc = mb86a20s_writereg(state, 0x45, val | 0x10); |
| 767 | if (rc < 0) |
| 768 | goto err; |
| 769 | rc = mb86a20s_writereg(state, 0x45, val & 0x6f); |
| 770 | if (rc < 0) |
| 771 | goto err; |
| 772 | |
| 773 | /* MER counter reset */ |
| 774 | rc = mb86a20s_writereg(state, 0x50, 0x50); |
| 775 | if (rc < 0) |
| 776 | goto err; |
| 777 | rc = mb86a20s_readreg(state, 0x51); |
| 778 | if (rc < 0) |
| 779 | goto err; |
| 780 | val = rc; |
| 781 | rc = mb86a20s_writereg(state, 0x51, val | 0x01); |
| 782 | if (rc < 0) |
| 783 | goto err; |
| 784 | rc = mb86a20s_writereg(state, 0x51, val & 0x06); |
| 785 | if (rc < 0) |
| 786 | goto err; |
| 787 | |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 788 | goto ok; |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 789 | err: |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 790 | dev_err(&state->i2c->dev, |
| 791 | "%s: Can't reset FE statistics (error %d).\n", |
| 792 | __func__, rc); |
| 793 | ok: |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 794 | return rc; |
| 795 | } |
| 796 | |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 797 | static int mb86a20s_get_pre_ber(struct dvb_frontend *fe, |
| 798 | unsigned layer, |
| 799 | u32 *error, u32 *count) |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 800 | { |
| 801 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 802 | int rc, val; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 803 | |
| 804 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 805 | |
| 806 | if (layer >= 3) |
| 807 | return -EINVAL; |
| 808 | |
| 809 | /* Check if the BER measures are already available */ |
| 810 | rc = mb86a20s_readreg(state, 0x54); |
| 811 | if (rc < 0) |
| 812 | return rc; |
| 813 | |
| 814 | /* Check if data is available for that layer */ |
| 815 | if (!(rc & (1 << layer))) { |
| 816 | dev_dbg(&state->i2c->dev, |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 817 | "%s: preBER for layer %c is not available yet.\n", |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 818 | __func__, 'A' + layer); |
| 819 | return -EBUSY; |
| 820 | } |
| 821 | |
| 822 | /* Read Bit Error Count */ |
| 823 | rc = mb86a20s_readreg(state, 0x55 + layer * 3); |
| 824 | if (rc < 0) |
| 825 | return rc; |
| 826 | *error = rc << 16; |
| 827 | rc = mb86a20s_readreg(state, 0x56 + layer * 3); |
| 828 | if (rc < 0) |
| 829 | return rc; |
| 830 | *error |= rc << 8; |
| 831 | rc = mb86a20s_readreg(state, 0x57 + layer * 3); |
| 832 | if (rc < 0) |
| 833 | return rc; |
| 834 | *error |= rc; |
| 835 | |
| 836 | dev_dbg(&state->i2c->dev, |
| 837 | "%s: bit error before Viterbi for layer %c: %d.\n", |
| 838 | __func__, 'A' + layer, *error); |
| 839 | |
| 840 | /* Read Bit Count */ |
| 841 | rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3); |
| 842 | if (rc < 0) |
| 843 | return rc; |
| 844 | rc = mb86a20s_readreg(state, 0x51); |
| 845 | if (rc < 0) |
| 846 | return rc; |
| 847 | *count = rc << 16; |
| 848 | rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3); |
| 849 | if (rc < 0) |
| 850 | return rc; |
| 851 | rc = mb86a20s_readreg(state, 0x51); |
| 852 | if (rc < 0) |
| 853 | return rc; |
| 854 | *count |= rc << 8; |
| 855 | rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3); |
| 856 | if (rc < 0) |
| 857 | return rc; |
| 858 | rc = mb86a20s_readreg(state, 0x51); |
| 859 | if (rc < 0) |
| 860 | return rc; |
| 861 | *count |= rc; |
| 862 | |
| 863 | dev_dbg(&state->i2c->dev, |
| 864 | "%s: bit count before Viterbi for layer %c: %d.\n", |
| 865 | __func__, 'A' + layer, *count); |
| 866 | |
| 867 | |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 868 | /* |
| 869 | * As we get TMCC data from the frontend, we can better estimate the |
| 870 | * BER bit counters, in order to do the BER measure during a longer |
| 871 | * time. Use those data, if available, to update the bit count |
| 872 | * measure. |
| 873 | */ |
| 874 | |
| 875 | if (state->estimated_rate[layer] |
| 876 | && state->estimated_rate[layer] != *count) { |
| 877 | dev_dbg(&state->i2c->dev, |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 878 | "%s: updating layer %c preBER counter to %d.\n", |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 879 | __func__, 'A' + layer, state->estimated_rate[layer]); |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 880 | |
| 881 | /* Turn off BER before Viterbi */ |
| 882 | rc = mb86a20s_writereg(state, 0x52, 0x00); |
| 883 | |
| 884 | /* Update counter for this layer */ |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 885 | rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3); |
| 886 | if (rc < 0) |
| 887 | return rc; |
| 888 | rc = mb86a20s_writereg(state, 0x51, |
| 889 | state->estimated_rate[layer] >> 16); |
| 890 | if (rc < 0) |
| 891 | return rc; |
| 892 | rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3); |
| 893 | if (rc < 0) |
| 894 | return rc; |
| 895 | rc = mb86a20s_writereg(state, 0x51, |
| 896 | state->estimated_rate[layer] >> 8); |
| 897 | if (rc < 0) |
| 898 | return rc; |
| 899 | rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3); |
| 900 | if (rc < 0) |
| 901 | return rc; |
| 902 | rc = mb86a20s_writereg(state, 0x51, |
| 903 | state->estimated_rate[layer]); |
| 904 | if (rc < 0) |
| 905 | return rc; |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 906 | |
| 907 | /* Turn on BER before Viterbi */ |
| 908 | rc = mb86a20s_writereg(state, 0x52, 0x01); |
| 909 | |
| 910 | /* Reset all preBER counters */ |
| 911 | rc = mb86a20s_writereg(state, 0x53, 0x00); |
| 912 | if (rc < 0) |
| 913 | return rc; |
| 914 | rc = mb86a20s_writereg(state, 0x53, 0x07); |
| 915 | } else { |
| 916 | /* Reset counter to collect new data */ |
| 917 | rc = mb86a20s_readreg(state, 0x53); |
| 918 | if (rc < 0) |
| 919 | return rc; |
| 920 | val = rc; |
| 921 | rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer)); |
| 922 | if (rc < 0) |
| 923 | return rc; |
| 924 | rc = mb86a20s_writereg(state, 0x53, val | (1 << layer)); |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 925 | } |
| 926 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 927 | return rc; |
| 928 | } |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 929 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 930 | static int mb86a20s_get_post_ber(struct dvb_frontend *fe, |
| 931 | unsigned layer, |
| 932 | u32 *error, u32 *count) |
| 933 | { |
| 934 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 935 | u32 counter, collect_rate; |
| 936 | int rc, val; |
| 937 | |
| 938 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 939 | |
| 940 | if (layer >= 3) |
| 941 | return -EINVAL; |
| 942 | |
| 943 | /* Check if the BER measures are already available */ |
| 944 | rc = mb86a20s_readreg(state, 0x60); |
| 945 | if (rc < 0) |
| 946 | return rc; |
| 947 | |
| 948 | /* Check if data is available for that layer */ |
| 949 | if (!(rc & (1 << layer))) { |
| 950 | dev_dbg(&state->i2c->dev, |
| 951 | "%s: post BER for layer %c is not available yet.\n", |
| 952 | __func__, 'A' + layer); |
| 953 | return -EBUSY; |
| 954 | } |
| 955 | |
| 956 | /* Read Bit Error Count */ |
| 957 | rc = mb86a20s_readreg(state, 0x64 + layer * 3); |
| 958 | if (rc < 0) |
| 959 | return rc; |
| 960 | *error = rc << 16; |
| 961 | rc = mb86a20s_readreg(state, 0x65 + layer * 3); |
| 962 | if (rc < 0) |
| 963 | return rc; |
| 964 | *error |= rc << 8; |
| 965 | rc = mb86a20s_readreg(state, 0x66 + layer * 3); |
| 966 | if (rc < 0) |
| 967 | return rc; |
| 968 | *error |= rc; |
| 969 | |
| 970 | dev_dbg(&state->i2c->dev, |
| 971 | "%s: post bit error for layer %c: %d.\n", |
| 972 | __func__, 'A' + layer, *error); |
| 973 | |
| 974 | /* Read Bit Count */ |
| 975 | rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2); |
| 976 | if (rc < 0) |
| 977 | return rc; |
| 978 | rc = mb86a20s_readreg(state, 0x51); |
| 979 | if (rc < 0) |
| 980 | return rc; |
| 981 | counter = rc << 8; |
| 982 | rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2); |
| 983 | if (rc < 0) |
| 984 | return rc; |
| 985 | rc = mb86a20s_readreg(state, 0x51); |
| 986 | if (rc < 0) |
| 987 | return rc; |
| 988 | counter |= rc; |
| 989 | *count = counter * 204 * 8; |
| 990 | |
| 991 | dev_dbg(&state->i2c->dev, |
| 992 | "%s: post bit count for layer %c: %d.\n", |
| 993 | __func__, 'A' + layer, *count); |
| 994 | |
| 995 | /* |
| 996 | * As we get TMCC data from the frontend, we can better estimate the |
| 997 | * BER bit counters, in order to do the BER measure during a longer |
| 998 | * time. Use those data, if available, to update the bit count |
| 999 | * measure. |
| 1000 | */ |
| 1001 | |
| 1002 | if (!state->estimated_rate[layer]) |
| 1003 | goto reset_measurement; |
| 1004 | |
| 1005 | collect_rate = state->estimated_rate[layer] / 204 / 8; |
| 1006 | if (collect_rate < 32) |
| 1007 | collect_rate = 32; |
| 1008 | if (collect_rate > 65535) |
| 1009 | collect_rate = 65535; |
| 1010 | if (collect_rate != counter) { |
| 1011 | dev_dbg(&state->i2c->dev, |
| 1012 | "%s: updating postBER counter on layer %c to %d.\n", |
| 1013 | __func__, 'A' + layer, collect_rate); |
| 1014 | |
| 1015 | /* Turn off BER after Viterbi */ |
| 1016 | rc = mb86a20s_writereg(state, 0x5e, 0x00); |
| 1017 | |
| 1018 | /* Update counter for this layer */ |
| 1019 | rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2); |
| 1020 | if (rc < 0) |
| 1021 | return rc; |
| 1022 | rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8); |
| 1023 | if (rc < 0) |
| 1024 | return rc; |
| 1025 | rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2); |
| 1026 | if (rc < 0) |
| 1027 | return rc; |
| 1028 | rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff); |
| 1029 | if (rc < 0) |
| 1030 | return rc; |
| 1031 | |
| 1032 | /* Turn on BER after Viterbi */ |
| 1033 | rc = mb86a20s_writereg(state, 0x5e, 0x07); |
| 1034 | |
| 1035 | /* Reset all preBER counters */ |
| 1036 | rc = mb86a20s_writereg(state, 0x5f, 0x00); |
| 1037 | if (rc < 0) |
| 1038 | return rc; |
| 1039 | rc = mb86a20s_writereg(state, 0x5f, 0x07); |
| 1040 | |
| 1041 | return rc; |
| 1042 | } |
| 1043 | |
| 1044 | reset_measurement: |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1045 | /* Reset counter to collect new data */ |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 1046 | rc = mb86a20s_readreg(state, 0x5f); |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1047 | if (rc < 0) |
| 1048 | return rc; |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 1049 | val = rc; |
| 1050 | rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer)); |
| 1051 | if (rc < 0) |
| 1052 | return rc; |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1053 | rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer)); |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1054 | |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 1055 | return rc; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1056 | } |
| 1057 | |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1058 | static int mb86a20s_get_blk_error(struct dvb_frontend *fe, |
| 1059 | unsigned layer, |
| 1060 | u32 *error, u32 *count) |
| 1061 | { |
| 1062 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 313cf4e | 2013-01-24 11:51:23 -0300 | [diff] [blame] | 1063 | int rc, val; |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1064 | u32 collect_rate; |
| 1065 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 1066 | |
| 1067 | if (layer >= 3) |
| 1068 | return -EINVAL; |
| 1069 | |
| 1070 | /* Check if the PER measures are already available */ |
| 1071 | rc = mb86a20s_writereg(state, 0x50, 0xb8); |
| 1072 | if (rc < 0) |
| 1073 | return rc; |
| 1074 | rc = mb86a20s_readreg(state, 0x51); |
| 1075 | if (rc < 0) |
| 1076 | return rc; |
| 1077 | |
| 1078 | /* Check if data is available for that layer */ |
| 1079 | |
| 1080 | if (!(rc & (1 << layer))) { |
| 1081 | dev_dbg(&state->i2c->dev, |
| 1082 | "%s: block counts for layer %c aren't available yet.\n", |
| 1083 | __func__, 'A' + layer); |
| 1084 | return -EBUSY; |
| 1085 | } |
| 1086 | |
| 1087 | /* Read Packet error Count */ |
| 1088 | rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2); |
| 1089 | if (rc < 0) |
| 1090 | return rc; |
| 1091 | rc = mb86a20s_readreg(state, 0x51); |
| 1092 | if (rc < 0) |
| 1093 | return rc; |
| 1094 | *error = rc << 8; |
| 1095 | rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2); |
| 1096 | if (rc < 0) |
| 1097 | return rc; |
| 1098 | rc = mb86a20s_readreg(state, 0x51); |
| 1099 | if (rc < 0) |
| 1100 | return rc; |
| 1101 | *error |= rc; |
Mauro Carvalho Chehab | d56e326 | 2013-03-02 09:05:42 -0300 | [diff] [blame] | 1102 | dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n", |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1103 | __func__, 'A' + layer, *error); |
| 1104 | |
| 1105 | /* Read Bit Count */ |
| 1106 | rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2); |
| 1107 | if (rc < 0) |
| 1108 | return rc; |
| 1109 | rc = mb86a20s_readreg(state, 0x51); |
| 1110 | if (rc < 0) |
| 1111 | return rc; |
| 1112 | *count = rc << 8; |
| 1113 | rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2); |
| 1114 | if (rc < 0) |
| 1115 | return rc; |
| 1116 | rc = mb86a20s_readreg(state, 0x51); |
| 1117 | if (rc < 0) |
| 1118 | return rc; |
| 1119 | *count |= rc; |
| 1120 | |
| 1121 | dev_dbg(&state->i2c->dev, |
| 1122 | "%s: block count for layer %c: %d.\n", |
| 1123 | __func__, 'A' + layer, *count); |
| 1124 | |
| 1125 | /* |
| 1126 | * As we get TMCC data from the frontend, we can better estimate the |
| 1127 | * BER bit counters, in order to do the BER measure during a longer |
| 1128 | * time. Use those data, if available, to update the bit count |
| 1129 | * measure. |
| 1130 | */ |
| 1131 | |
| 1132 | if (!state->estimated_rate[layer]) |
| 1133 | goto reset_measurement; |
| 1134 | |
| 1135 | collect_rate = state->estimated_rate[layer] / 204 / 8; |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1136 | if (collect_rate < 32) |
| 1137 | collect_rate = 32; |
| 1138 | if (collect_rate > 65535) |
| 1139 | collect_rate = 65535; |
| 1140 | |
| 1141 | if (collect_rate != *count) { |
| 1142 | dev_dbg(&state->i2c->dev, |
| 1143 | "%s: updating PER counter on layer %c to %d.\n", |
| 1144 | __func__, 'A' + layer, collect_rate); |
Mauro Carvalho Chehab | 313cf4e | 2013-01-24 11:51:23 -0300 | [diff] [blame] | 1145 | |
| 1146 | /* Stop PER measurement */ |
| 1147 | rc = mb86a20s_writereg(state, 0x50, 0xb0); |
| 1148 | if (rc < 0) |
| 1149 | return rc; |
| 1150 | rc = mb86a20s_writereg(state, 0x51, 0x00); |
| 1151 | if (rc < 0) |
| 1152 | return rc; |
| 1153 | |
| 1154 | /* Update this layer's counter */ |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1155 | rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2); |
| 1156 | if (rc < 0) |
| 1157 | return rc; |
| 1158 | rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8); |
| 1159 | if (rc < 0) |
| 1160 | return rc; |
| 1161 | rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2); |
| 1162 | if (rc < 0) |
| 1163 | return rc; |
| 1164 | rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff); |
| 1165 | if (rc < 0) |
| 1166 | return rc; |
Mauro Carvalho Chehab | 313cf4e | 2013-01-24 11:51:23 -0300 | [diff] [blame] | 1167 | |
| 1168 | /* start PER measurement */ |
| 1169 | rc = mb86a20s_writereg(state, 0x50, 0xb0); |
| 1170 | if (rc < 0) |
| 1171 | return rc; |
| 1172 | rc = mb86a20s_writereg(state, 0x51, 0x07); |
| 1173 | if (rc < 0) |
| 1174 | return rc; |
| 1175 | |
| 1176 | /* Reset all counters to collect new data */ |
| 1177 | rc = mb86a20s_writereg(state, 0x50, 0xb1); |
| 1178 | if (rc < 0) |
| 1179 | return rc; |
| 1180 | rc = mb86a20s_writereg(state, 0x51, 0x07); |
| 1181 | if (rc < 0) |
| 1182 | return rc; |
| 1183 | rc = mb86a20s_writereg(state, 0x51, 0x00); |
| 1184 | |
| 1185 | return rc; |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | reset_measurement: |
| 1189 | /* Reset counter to collect new data */ |
| 1190 | rc = mb86a20s_writereg(state, 0x50, 0xb1); |
| 1191 | if (rc < 0) |
| 1192 | return rc; |
Mauro Carvalho Chehab | 313cf4e | 2013-01-24 11:51:23 -0300 | [diff] [blame] | 1193 | rc = mb86a20s_readreg(state, 0x51); |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1194 | if (rc < 0) |
| 1195 | return rc; |
Mauro Carvalho Chehab | 313cf4e | 2013-01-24 11:51:23 -0300 | [diff] [blame] | 1196 | val = rc; |
| 1197 | rc = mb86a20s_writereg(state, 0x51, val | (1 << layer)); |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1198 | if (rc < 0) |
| 1199 | return rc; |
Mauro Carvalho Chehab | 313cf4e | 2013-01-24 11:51:23 -0300 | [diff] [blame] | 1200 | rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer)); |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1201 | |
Mauro Carvalho Chehab | 313cf4e | 2013-01-24 11:51:23 -0300 | [diff] [blame] | 1202 | return rc; |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1203 | } |
| 1204 | |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 1205 | struct linear_segments { |
| 1206 | unsigned x, y; |
| 1207 | }; |
| 1208 | |
| 1209 | /* |
| 1210 | * All tables below return a dB/1000 measurement |
| 1211 | */ |
| 1212 | |
| 1213 | static struct linear_segments cnr_to_db_table[] = { |
| 1214 | { 19648, 0}, |
| 1215 | { 18187, 1000}, |
| 1216 | { 16534, 2000}, |
| 1217 | { 14823, 3000}, |
| 1218 | { 13161, 4000}, |
| 1219 | { 11622, 5000}, |
| 1220 | { 10279, 6000}, |
| 1221 | { 9089, 7000}, |
| 1222 | { 8042, 8000}, |
| 1223 | { 7137, 9000}, |
| 1224 | { 6342, 10000}, |
| 1225 | { 5641, 11000}, |
| 1226 | { 5030, 12000}, |
| 1227 | { 4474, 13000}, |
| 1228 | { 3988, 14000}, |
| 1229 | { 3556, 15000}, |
| 1230 | { 3180, 16000}, |
| 1231 | { 2841, 17000}, |
| 1232 | { 2541, 18000}, |
| 1233 | { 2276, 19000}, |
| 1234 | { 2038, 20000}, |
| 1235 | { 1800, 21000}, |
| 1236 | { 1625, 22000}, |
| 1237 | { 1462, 23000}, |
| 1238 | { 1324, 24000}, |
| 1239 | { 1175, 25000}, |
| 1240 | { 1063, 26000}, |
| 1241 | { 980, 27000}, |
| 1242 | { 907, 28000}, |
| 1243 | { 840, 29000}, |
| 1244 | { 788, 30000}, |
| 1245 | }; |
| 1246 | |
| 1247 | static struct linear_segments cnr_64qam_table[] = { |
| 1248 | { 3922688, 0}, |
| 1249 | { 3920384, 1000}, |
| 1250 | { 3902720, 2000}, |
| 1251 | { 3894784, 3000}, |
| 1252 | { 3882496, 4000}, |
| 1253 | { 3872768, 5000}, |
| 1254 | { 3858944, 6000}, |
| 1255 | { 3851520, 7000}, |
| 1256 | { 3838976, 8000}, |
| 1257 | { 3829248, 9000}, |
| 1258 | { 3818240, 10000}, |
| 1259 | { 3806976, 11000}, |
| 1260 | { 3791872, 12000}, |
| 1261 | { 3767040, 13000}, |
| 1262 | { 3720960, 14000}, |
| 1263 | { 3637504, 15000}, |
| 1264 | { 3498496, 16000}, |
| 1265 | { 3296000, 17000}, |
| 1266 | { 3031040, 18000}, |
| 1267 | { 2715392, 19000}, |
| 1268 | { 2362624, 20000}, |
| 1269 | { 1963264, 21000}, |
| 1270 | { 1649664, 22000}, |
| 1271 | { 1366784, 23000}, |
| 1272 | { 1120768, 24000}, |
| 1273 | { 890880, 25000}, |
| 1274 | { 723456, 26000}, |
| 1275 | { 612096, 27000}, |
| 1276 | { 518912, 28000}, |
| 1277 | { 448256, 29000}, |
| 1278 | { 388864, 30000}, |
| 1279 | }; |
| 1280 | |
| 1281 | static struct linear_segments cnr_16qam_table[] = { |
| 1282 | { 5314816, 0}, |
| 1283 | { 5219072, 1000}, |
| 1284 | { 5118720, 2000}, |
| 1285 | { 4998912, 3000}, |
| 1286 | { 4875520, 4000}, |
| 1287 | { 4736000, 5000}, |
| 1288 | { 4604160, 6000}, |
| 1289 | { 4458752, 7000}, |
| 1290 | { 4300288, 8000}, |
| 1291 | { 4092928, 9000}, |
| 1292 | { 3836160, 10000}, |
| 1293 | { 3521024, 11000}, |
| 1294 | { 3155968, 12000}, |
| 1295 | { 2756864, 13000}, |
| 1296 | { 2347008, 14000}, |
| 1297 | { 1955072, 15000}, |
| 1298 | { 1593600, 16000}, |
| 1299 | { 1297920, 17000}, |
| 1300 | { 1043968, 18000}, |
| 1301 | { 839680, 19000}, |
| 1302 | { 672256, 20000}, |
| 1303 | { 523008, 21000}, |
| 1304 | { 424704, 22000}, |
| 1305 | { 345088, 23000}, |
| 1306 | { 280064, 24000}, |
| 1307 | { 221440, 25000}, |
| 1308 | { 179712, 26000}, |
| 1309 | { 151040, 27000}, |
| 1310 | { 128512, 28000}, |
| 1311 | { 110080, 29000}, |
| 1312 | { 95744, 30000}, |
| 1313 | }; |
| 1314 | |
| 1315 | struct linear_segments cnr_qpsk_table[] = { |
| 1316 | { 2834176, 0}, |
| 1317 | { 2683648, 1000}, |
| 1318 | { 2536960, 2000}, |
| 1319 | { 2391808, 3000}, |
| 1320 | { 2133248, 4000}, |
| 1321 | { 1906176, 5000}, |
| 1322 | { 1666560, 6000}, |
| 1323 | { 1422080, 7000}, |
| 1324 | { 1189632, 8000}, |
| 1325 | { 976384, 9000}, |
| 1326 | { 790272, 10000}, |
| 1327 | { 633344, 11000}, |
| 1328 | { 505600, 12000}, |
| 1329 | { 402944, 13000}, |
| 1330 | { 320768, 14000}, |
| 1331 | { 255488, 15000}, |
| 1332 | { 204032, 16000}, |
| 1333 | { 163072, 17000}, |
| 1334 | { 130304, 18000}, |
| 1335 | { 105216, 19000}, |
| 1336 | { 83456, 20000}, |
| 1337 | { 65024, 21000}, |
| 1338 | { 52480, 22000}, |
| 1339 | { 42752, 23000}, |
| 1340 | { 34560, 24000}, |
| 1341 | { 27136, 25000}, |
| 1342 | { 22016, 26000}, |
| 1343 | { 18432, 27000}, |
| 1344 | { 15616, 28000}, |
| 1345 | { 13312, 29000}, |
| 1346 | { 11520, 30000}, |
| 1347 | }; |
| 1348 | |
| 1349 | static u32 interpolate_value(u32 value, struct linear_segments *segments, |
| 1350 | unsigned len) |
| 1351 | { |
| 1352 | u64 tmp64; |
| 1353 | u32 dx, dy; |
| 1354 | int i, ret; |
| 1355 | |
| 1356 | if (value >= segments[0].x) |
| 1357 | return segments[0].y; |
| 1358 | if (value < segments[len-1].x) |
| 1359 | return segments[len-1].y; |
| 1360 | |
| 1361 | for (i = 1; i < len - 1; i++) { |
| 1362 | /* If value is identical, no need to interpolate */ |
| 1363 | if (value == segments[i].x) |
| 1364 | return segments[i].y; |
| 1365 | if (value > segments[i].x) |
| 1366 | break; |
| 1367 | } |
| 1368 | |
| 1369 | /* Linear interpolation between the two (x,y) points */ |
| 1370 | dy = segments[i].y - segments[i - 1].y; |
| 1371 | dx = segments[i - 1].x - segments[i].x; |
| 1372 | tmp64 = value - segments[i].x; |
| 1373 | tmp64 *= dy; |
| 1374 | do_div(tmp64, dx); |
| 1375 | ret = segments[i].y - tmp64; |
| 1376 | |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | static int mb86a20s_get_main_CNR(struct dvb_frontend *fe) |
| 1381 | { |
| 1382 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 1383 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1384 | u32 cnr_linear, cnr; |
| 1385 | int rc, val; |
| 1386 | |
| 1387 | /* Check if CNR is available */ |
| 1388 | rc = mb86a20s_readreg(state, 0x45); |
| 1389 | if (rc < 0) |
| 1390 | return rc; |
| 1391 | |
| 1392 | if (!(rc & 0x40)) { |
Mauro Carvalho Chehab | d56e326 | 2013-03-02 09:05:42 -0300 | [diff] [blame] | 1393 | dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n", |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 1394 | __func__); |
| 1395 | return -EBUSY; |
| 1396 | } |
| 1397 | val = rc; |
| 1398 | |
| 1399 | rc = mb86a20s_readreg(state, 0x46); |
| 1400 | if (rc < 0) |
| 1401 | return rc; |
| 1402 | cnr_linear = rc << 8; |
| 1403 | |
| 1404 | rc = mb86a20s_readreg(state, 0x46); |
| 1405 | if (rc < 0) |
| 1406 | return rc; |
| 1407 | cnr_linear |= rc; |
| 1408 | |
| 1409 | cnr = interpolate_value(cnr_linear, |
| 1410 | cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table)); |
| 1411 | |
| 1412 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 1413 | c->cnr.stat[0].svalue = cnr; |
| 1414 | |
| 1415 | dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n", |
| 1416 | __func__, cnr / 1000, cnr % 1000, cnr_linear); |
| 1417 | |
| 1418 | /* CNR counter reset */ |
| 1419 | rc = mb86a20s_writereg(state, 0x45, val | 0x10); |
| 1420 | if (rc < 0) |
| 1421 | return rc; |
| 1422 | rc = mb86a20s_writereg(state, 0x45, val & 0x6f); |
| 1423 | |
| 1424 | return rc; |
| 1425 | } |
| 1426 | |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1427 | static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe) |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 1428 | { |
| 1429 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 1430 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1431 | u32 mer, cnr; |
| 1432 | int rc, val, i; |
| 1433 | struct linear_segments *segs; |
| 1434 | unsigned segs_len; |
| 1435 | |
| 1436 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 1437 | |
| 1438 | /* Check if the measures are already available */ |
| 1439 | rc = mb86a20s_writereg(state, 0x50, 0x5b); |
| 1440 | if (rc < 0) |
| 1441 | return rc; |
| 1442 | rc = mb86a20s_readreg(state, 0x51); |
| 1443 | if (rc < 0) |
| 1444 | return rc; |
| 1445 | |
| 1446 | /* Check if data is available */ |
| 1447 | if (!(rc & 0x01)) { |
Mauro Carvalho Chehab | d56e326 | 2013-03-02 09:05:42 -0300 | [diff] [blame] | 1448 | dev_dbg(&state->i2c->dev, |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 1449 | "%s: MER measures aren't available yet.\n", __func__); |
| 1450 | return -EBUSY; |
| 1451 | } |
| 1452 | |
| 1453 | /* Read all layers */ |
| 1454 | for (i = 0; i < 3; i++) { |
| 1455 | if (!(c->isdbt_layer_enabled & (1 << i))) { |
| 1456 | c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1457 | continue; |
| 1458 | } |
| 1459 | |
| 1460 | rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3); |
| 1461 | if (rc < 0) |
| 1462 | return rc; |
| 1463 | rc = mb86a20s_readreg(state, 0x51); |
| 1464 | if (rc < 0) |
| 1465 | return rc; |
| 1466 | mer = rc << 16; |
| 1467 | rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3); |
| 1468 | if (rc < 0) |
| 1469 | return rc; |
| 1470 | rc = mb86a20s_readreg(state, 0x51); |
| 1471 | if (rc < 0) |
| 1472 | return rc; |
| 1473 | mer |= rc << 8; |
| 1474 | rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3); |
| 1475 | if (rc < 0) |
| 1476 | return rc; |
| 1477 | rc = mb86a20s_readreg(state, 0x51); |
| 1478 | if (rc < 0) |
| 1479 | return rc; |
| 1480 | mer |= rc; |
| 1481 | |
| 1482 | switch (c->layer[i].modulation) { |
| 1483 | case DQPSK: |
| 1484 | case QPSK: |
| 1485 | segs = cnr_qpsk_table; |
| 1486 | segs_len = ARRAY_SIZE(cnr_qpsk_table); |
| 1487 | break; |
| 1488 | case QAM_16: |
| 1489 | segs = cnr_16qam_table; |
| 1490 | segs_len = ARRAY_SIZE(cnr_16qam_table); |
| 1491 | break; |
| 1492 | default: |
| 1493 | case QAM_64: |
| 1494 | segs = cnr_64qam_table; |
| 1495 | segs_len = ARRAY_SIZE(cnr_64qam_table); |
| 1496 | break; |
| 1497 | } |
| 1498 | cnr = interpolate_value(mer, segs, segs_len); |
| 1499 | |
| 1500 | c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL; |
| 1501 | c->cnr.stat[1 + i].svalue = cnr; |
| 1502 | |
| 1503 | dev_dbg(&state->i2c->dev, |
| 1504 | "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n", |
| 1505 | __func__, 'A' + i, cnr / 1000, cnr % 1000, mer); |
| 1506 | |
| 1507 | } |
| 1508 | |
| 1509 | /* Start a new MER measurement */ |
| 1510 | /* MER counter reset */ |
| 1511 | rc = mb86a20s_writereg(state, 0x50, 0x50); |
| 1512 | if (rc < 0) |
| 1513 | return rc; |
| 1514 | rc = mb86a20s_readreg(state, 0x51); |
| 1515 | if (rc < 0) |
| 1516 | return rc; |
| 1517 | val = rc; |
| 1518 | |
| 1519 | rc = mb86a20s_writereg(state, 0x51, val | 0x01); |
| 1520 | if (rc < 0) |
| 1521 | return rc; |
| 1522 | rc = mb86a20s_writereg(state, 0x51, val & 0x06); |
| 1523 | if (rc < 0) |
| 1524 | return rc; |
| 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1529 | static void mb86a20s_stats_not_ready(struct dvb_frontend *fe) |
| 1530 | { |
| 1531 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 1532 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1533 | int i; |
| 1534 | |
| 1535 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 1536 | |
| 1537 | /* Fill the length of each status counter */ |
| 1538 | |
| 1539 | /* Only global stats */ |
| 1540 | c->strength.len = 1; |
| 1541 | |
| 1542 | /* Per-layer stats - 3 layers + global */ |
| 1543 | c->cnr.len = 4; |
| 1544 | c->pre_bit_error.len = 4; |
| 1545 | c->pre_bit_count.len = 4; |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1546 | c->post_bit_error.len = 4; |
| 1547 | c->post_bit_count.len = 4; |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1548 | c->block_error.len = 4; |
| 1549 | c->block_count.len = 4; |
| 1550 | |
| 1551 | /* Signal is always available */ |
| 1552 | c->strength.stat[0].scale = FE_SCALE_RELATIVE; |
| 1553 | c->strength.stat[0].uvalue = 0; |
| 1554 | |
| 1555 | /* Put all of them at FE_SCALE_NOT_AVAILABLE */ |
| 1556 | for (i = 0; i < 4; i++) { |
| 1557 | c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1558 | c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1559 | c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE; |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1560 | c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1561 | c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE; |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1562 | c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1563 | c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1564 | } |
| 1565 | } |
| 1566 | |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 1567 | static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr) |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1568 | { |
| 1569 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 1570 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1571 | int rc = 0, i; |
| 1572 | u32 bit_error = 0, bit_count = 0; |
| 1573 | u32 t_pre_bit_error = 0, t_pre_bit_count = 0; |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1574 | u32 t_post_bit_error = 0, t_post_bit_count = 0; |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1575 | u32 block_error = 0, block_count = 0; |
| 1576 | u32 t_block_error = 0, t_block_count = 0; |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1577 | int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0; |
| 1578 | int per_layers = 0; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1579 | |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 1580 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
| 1581 | |
| 1582 | mb86a20s_get_main_CNR(fe); |
| 1583 | |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1584 | /* Get per-layer stats */ |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1585 | mb86a20s_get_blk_error_layer_CNR(fe); |
Mauro Carvalho Chehab | 25188bd | 2013-01-16 15:12:05 -0300 | [diff] [blame] | 1586 | |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 1587 | /* |
| 1588 | * At state 7, only CNR is available |
| 1589 | * For BER measures, state=9 is required |
| 1590 | * FIXME: we may get MER measures with state=8 |
| 1591 | */ |
| 1592 | if (status_nr < 9) |
| 1593 | return 0; |
| 1594 | |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1595 | for (i = 0; i < 3; i++) { |
| 1596 | if (c->isdbt_layer_enabled & (1 << i)) { |
| 1597 | /* Layer is active and has rc segments */ |
| 1598 | active_layers++; |
| 1599 | |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1600 | /* Handle BER before vterbi */ |
Mauro Carvalho Chehab | ad0abbf | 2013-01-24 11:48:44 -0300 | [diff] [blame] | 1601 | rc = mb86a20s_get_pre_ber(fe, i, |
| 1602 | &bit_error, &bit_count); |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1603 | if (rc >= 0) { |
| 1604 | c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER; |
| 1605 | c->pre_bit_error.stat[1 + i].uvalue += bit_error; |
| 1606 | c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER; |
| 1607 | c->pre_bit_count.stat[1 + i].uvalue += bit_count; |
| 1608 | } else if (rc != -EBUSY) { |
| 1609 | /* |
| 1610 | * If an I/O error happened, |
| 1611 | * measures are now unavailable |
| 1612 | */ |
| 1613 | c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1614 | c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1615 | dev_err(&state->i2c->dev, |
| 1616 | "%s: Can't get BER for layer %c (error %d).\n", |
| 1617 | __func__, 'A' + i, rc); |
| 1618 | } |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1619 | if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE) |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1620 | pre_ber_layers++; |
| 1621 | |
| 1622 | /* Handle BER post vterbi */ |
| 1623 | rc = mb86a20s_get_post_ber(fe, i, |
| 1624 | &bit_error, &bit_count); |
| 1625 | if (rc >= 0) { |
| 1626 | c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER; |
| 1627 | c->post_bit_error.stat[1 + i].uvalue += bit_error; |
| 1628 | c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER; |
| 1629 | c->post_bit_count.stat[1 + i].uvalue += bit_count; |
| 1630 | } else if (rc != -EBUSY) { |
| 1631 | /* |
| 1632 | * If an I/O error happened, |
| 1633 | * measures are now unavailable |
| 1634 | */ |
| 1635 | c->post_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1636 | c->post_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1637 | dev_err(&state->i2c->dev, |
| 1638 | "%s: Can't get BER for layer %c (error %d).\n", |
| 1639 | __func__, 'A' + i, rc); |
| 1640 | } |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1641 | if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE) |
| 1642 | post_ber_layers++; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1643 | |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1644 | /* Handle Block errors for PER/UCB reports */ |
| 1645 | rc = mb86a20s_get_blk_error(fe, i, |
| 1646 | &block_error, |
| 1647 | &block_count); |
| 1648 | if (rc >= 0) { |
| 1649 | c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER; |
| 1650 | c->block_error.stat[1 + i].uvalue += block_error; |
| 1651 | c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER; |
| 1652 | c->block_count.stat[1 + i].uvalue += block_count; |
| 1653 | } else if (rc != -EBUSY) { |
| 1654 | /* |
| 1655 | * If an I/O error happened, |
| 1656 | * measures are now unavailable |
| 1657 | */ |
| 1658 | c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1659 | c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE; |
| 1660 | dev_err(&state->i2c->dev, |
| 1661 | "%s: Can't get PER for layer %c (error %d).\n", |
| 1662 | __func__, 'A' + i, rc); |
| 1663 | |
| 1664 | } |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1665 | if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE) |
| 1666 | per_layers++; |
| 1667 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1668 | /* Update total preBER */ |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1669 | t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue; |
| 1670 | t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue; |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1671 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1672 | /* Update total postBER */ |
| 1673 | t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue; |
| 1674 | t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue; |
| 1675 | |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1676 | /* Update total PER */ |
| 1677 | t_block_error += c->block_error.stat[1 + i].uvalue; |
| 1678 | t_block_count += c->block_count.stat[1 + i].uvalue; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | /* |
| 1683 | * Start showing global count if at least one error count is |
| 1684 | * available. |
| 1685 | */ |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1686 | if (pre_ber_layers) { |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1687 | /* |
| 1688 | * At least one per-layer BER measure was read. We can now |
| 1689 | * calculate the total BER |
| 1690 | * |
| 1691 | * Total Bit Error/Count is calculated as the sum of the |
| 1692 | * bit errors on all active layers. |
| 1693 | */ |
| 1694 | c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER; |
| 1695 | c->pre_bit_error.stat[0].uvalue = t_pre_bit_error; |
| 1696 | c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER; |
| 1697 | c->pre_bit_count.stat[0].uvalue = t_pre_bit_count; |
Mauro Carvalho Chehab | f67102c | 2013-01-24 13:16:17 -0300 | [diff] [blame] | 1698 | } else { |
| 1699 | c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 1700 | c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1701 | } |
| 1702 | |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1703 | /* |
| 1704 | * Start showing global count if at least one error count is |
| 1705 | * available. |
| 1706 | */ |
| 1707 | if (post_ber_layers) { |
| 1708 | /* |
| 1709 | * At least one per-layer BER measure was read. We can now |
| 1710 | * calculate the total BER |
| 1711 | * |
| 1712 | * Total Bit Error/Count is calculated as the sum of the |
| 1713 | * bit errors on all active layers. |
| 1714 | */ |
| 1715 | c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; |
| 1716 | c->post_bit_error.stat[0].uvalue = t_post_bit_error; |
| 1717 | c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; |
| 1718 | c->post_bit_count.stat[0].uvalue = t_post_bit_count; |
Mauro Carvalho Chehab | f67102c | 2013-01-24 13:16:17 -0300 | [diff] [blame] | 1719 | } else { |
| 1720 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 1721 | c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; |
Mauro Carvalho Chehab | d9b6f08 | 2013-01-24 10:25:16 -0300 | [diff] [blame] | 1722 | } |
| 1723 | |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1724 | if (per_layers) { |
| 1725 | /* |
| 1726 | * At least one per-layer UCB measure was read. We can now |
| 1727 | * calculate the total UCB |
| 1728 | * |
| 1729 | * Total block Error/Count is calculated as the sum of the |
| 1730 | * block errors on all active layers. |
| 1731 | */ |
| 1732 | c->block_error.stat[0].scale = FE_SCALE_COUNTER; |
| 1733 | c->block_error.stat[0].uvalue = t_block_error; |
| 1734 | c->block_count.stat[0].scale = FE_SCALE_COUNTER; |
| 1735 | c->block_count.stat[0].uvalue = t_block_count; |
Mauro Carvalho Chehab | f67102c | 2013-01-24 13:16:17 -0300 | [diff] [blame] | 1736 | } else { |
| 1737 | c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 1738 | c->block_count.stat[0].scale = FE_SCALE_COUNTER; |
Mauro Carvalho Chehab | 593ae89 | 2013-01-17 20:10:47 -0300 | [diff] [blame] | 1739 | } |
| 1740 | |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1741 | return rc; |
| 1742 | } |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1743 | |
| 1744 | /* |
| 1745 | * The functions below are called via DVB callbacks, so they need to |
| 1746 | * properly use the I2C gate control |
| 1747 | */ |
| 1748 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1749 | static int mb86a20s_initfe(struct dvb_frontend *fe) |
| 1750 | { |
| 1751 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 1752 | u64 pll; |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1753 | int rc; |
| 1754 | u8 regD5 = 1; |
| 1755 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 1756 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1757 | |
| 1758 | if (fe->ops.i2c_gate_ctrl) |
| 1759 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 1760 | |
| 1761 | /* Initialize the frontend */ |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 1762 | rc = mb86a20s_writeregdata(state, mb86a20s_init1); |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1763 | if (rc < 0) |
| 1764 | goto err; |
| 1765 | |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 1766 | /* Adjust IF frequency to match tuner */ |
| 1767 | if (fe->ops.tuner_ops.get_if_frequency) |
| 1768 | fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq); |
| 1769 | |
| 1770 | if (!state->if_freq) |
| 1771 | state->if_freq = 3300000; |
| 1772 | |
| 1773 | /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */ |
| 1774 | pll = state->if_freq * 1677721600L; |
| 1775 | do_div(pll, 1628571429L); |
| 1776 | rc = mb86a20s_writereg(state, 0x28, 0x20); |
| 1777 | if (rc < 0) |
| 1778 | goto err; |
| 1779 | rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff); |
| 1780 | if (rc < 0) |
| 1781 | goto err; |
| 1782 | rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff); |
| 1783 | if (rc < 0) |
| 1784 | goto err; |
| 1785 | rc = mb86a20s_writereg(state, 0x2b, pll & 0xff); |
| 1786 | if (rc < 0) |
| 1787 | goto err; |
| 1788 | dev_dbg(&state->i2c->dev, "%s: IF=%d, PLL=0x%06llx\n", |
| 1789 | __func__, state->if_freq, (long long)pll); |
| 1790 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1791 | if (!state->config->is_serial) { |
| 1792 | regD5 &= ~1; |
| 1793 | |
| 1794 | rc = mb86a20s_writereg(state, 0x50, 0xd5); |
| 1795 | if (rc < 0) |
| 1796 | goto err; |
| 1797 | rc = mb86a20s_writereg(state, 0x51, regD5); |
| 1798 | if (rc < 0) |
| 1799 | goto err; |
| 1800 | } |
| 1801 | |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 1802 | rc = mb86a20s_writeregdata(state, mb86a20s_init2); |
| 1803 | if (rc < 0) |
| 1804 | goto err; |
| 1805 | |
| 1806 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1807 | err: |
| 1808 | if (fe->ops.i2c_gate_ctrl) |
| 1809 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 1810 | |
| 1811 | if (rc < 0) { |
| 1812 | state->need_init = true; |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 1813 | dev_info(&state->i2c->dev, |
| 1814 | "mb86a20s: Init failed. Will try again later\n"); |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1815 | } else { |
| 1816 | state->need_init = false; |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 1817 | dev_dbg(&state->i2c->dev, "Initialization succeeded.\n"); |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1818 | } |
| 1819 | return rc; |
| 1820 | } |
| 1821 | |
| 1822 | static int mb86a20s_set_frontend(struct dvb_frontend *fe) |
| 1823 | { |
| 1824 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 1825 | int rc, if_freq; |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1826 | #if 0 |
| 1827 | /* |
| 1828 | * FIXME: Properly implement the set frontend properties |
| 1829 | */ |
| 1830 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1831 | #endif |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 1832 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1833 | |
| 1834 | /* |
| 1835 | * Gate should already be opened, but it doesn't hurt to |
| 1836 | * double-check |
| 1837 | */ |
| 1838 | if (fe->ops.i2c_gate_ctrl) |
| 1839 | fe->ops.i2c_gate_ctrl(fe, 1); |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1840 | fe->ops.tuner_ops.set_params(fe); |
| 1841 | |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 1842 | if (fe->ops.tuner_ops.get_if_frequency) { |
| 1843 | fe->ops.tuner_ops.get_if_frequency(fe, &if_freq); |
| 1844 | |
| 1845 | /* |
| 1846 | * If the IF frequency changed, re-initialize the |
| 1847 | * frontend. This is needed by some drivers like tda18271, |
| 1848 | * that only sets the IF after receiving a set_params() call |
| 1849 | */ |
| 1850 | if (if_freq != state->if_freq) |
| 1851 | state->need_init = true; |
| 1852 | } |
| 1853 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1854 | /* |
| 1855 | * Make it more reliable: if, for some reason, the initial |
| 1856 | * device initialization doesn't happen, initialize it when |
| 1857 | * a SBTVD parameters are adjusted. |
| 1858 | * |
| 1859 | * Unfortunately, due to a hard to track bug at tda829x/tda18271, |
| 1860 | * the agc callback logic is not called during DVB attach time, |
| 1861 | * causing mb86a20s to not be initialized with Kworld SBTVD. |
| 1862 | * So, this hack is needed, in order to make Kworld SBTVD to work. |
Mauro Carvalho Chehab | 768e6dad | 2013-02-28 16:45:39 -0300 | [diff] [blame] | 1863 | * |
| 1864 | * It is also needed to change the IF after the initial init. |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1865 | */ |
| 1866 | if (state->need_init) |
| 1867 | mb86a20s_initfe(fe); |
| 1868 | |
| 1869 | if (fe->ops.i2c_gate_ctrl) |
| 1870 | fe->ops.i2c_gate_ctrl(fe, 0); |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 1871 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1872 | rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception); |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1873 | mb86a20s_reset_counters(fe); |
Mauro Carvalho Chehab | d01a8ee | 2013-01-14 20:34:55 -0300 | [diff] [blame] | 1874 | |
Mauro Carvalho Chehab | dd4493e | 2013-01-22 08:53:11 -0200 | [diff] [blame] | 1875 | if (fe->ops.i2c_gate_ctrl) |
| 1876 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 1877 | |
| 1878 | return rc; |
| 1879 | } |
| 1880 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1881 | static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe, |
| 1882 | fe_status_t *status) |
Mauro Carvalho Chehab | d36e418 | 2013-01-22 08:49:39 -0200 | [diff] [blame] | 1883 | { |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1884 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 1885 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 1886 | int rc, status_nr; |
Mauro Carvalho Chehab | d36e418 | 2013-01-22 08:49:39 -0200 | [diff] [blame] | 1887 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1888 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
Mauro Carvalho Chehab | d36e418 | 2013-01-22 08:49:39 -0200 | [diff] [blame] | 1889 | |
| 1890 | if (fe->ops.i2c_gate_ctrl) |
| 1891 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 1892 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1893 | /* Get lock */ |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 1894 | status_nr = mb86a20s_read_status(fe, status); |
| 1895 | if (status_nr < 7) { |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1896 | mb86a20s_stats_not_ready(fe); |
| 1897 | mb86a20s_reset_frontend_cache(fe); |
| 1898 | } |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 1899 | if (status_nr < 0) { |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1900 | dev_err(&state->i2c->dev, |
| 1901 | "%s: Can't read frontend lock status\n", __func__); |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1902 | goto error; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1903 | } |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1904 | |
| 1905 | /* Get signal strength */ |
| 1906 | rc = mb86a20s_read_signal_strength(fe); |
| 1907 | if (rc < 0) { |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1908 | dev_err(&state->i2c->dev, |
| 1909 | "%s: Can't reset VBER registers.\n", __func__); |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1910 | mb86a20s_stats_not_ready(fe); |
| 1911 | mb86a20s_reset_frontend_cache(fe); |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1912 | |
| 1913 | rc = 0; /* Status is OK */ |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1914 | goto error; |
| 1915 | } |
| 1916 | /* Fill signal strength */ |
| 1917 | c->strength.stat[0].uvalue = rc; |
| 1918 | |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 1919 | if (status_nr >= 7) { |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1920 | /* Get TMCC info*/ |
| 1921 | rc = mb86a20s_get_frontend(fe); |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1922 | if (rc < 0) { |
| 1923 | dev_err(&state->i2c->dev, |
| 1924 | "%s: Can't get FE TMCC data.\n", __func__); |
| 1925 | rc = 0; /* Status is OK */ |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1926 | goto error; |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1927 | } |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1928 | |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1929 | /* Get statistics */ |
Mauro Carvalho Chehab | 15b1c5a | 2013-03-02 09:06:17 -0300 | [diff] [blame] | 1930 | rc = mb86a20s_get_stats(fe, status_nr); |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1931 | if (rc < 0 && rc != -EBUSY) { |
| 1932 | dev_err(&state->i2c->dev, |
| 1933 | "%s: Can't get FE statistics.\n", __func__); |
| 1934 | rc = 0; |
| 1935 | goto error; |
| 1936 | } |
| 1937 | rc = 0; /* Don't return EBUSY to userspace */ |
| 1938 | } |
| 1939 | goto ok; |
| 1940 | |
| 1941 | error: |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1942 | mb86a20s_stats_not_ready(fe); |
Mauro Carvalho Chehab | d36e418 | 2013-01-22 08:49:39 -0200 | [diff] [blame] | 1943 | |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1944 | ok: |
Mauro Carvalho Chehab | d36e418 | 2013-01-22 08:49:39 -0200 | [diff] [blame] | 1945 | if (fe->ops.i2c_gate_ctrl) |
| 1946 | fe->ops.i2c_gate_ctrl(fe, 1); |
Mauro Carvalho Chehab | 149d518 | 2013-01-22 12:30:07 -0300 | [diff] [blame] | 1947 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1948 | return rc; |
Mauro Carvalho Chehab | d36e418 | 2013-01-22 08:49:39 -0200 | [diff] [blame] | 1949 | } |
| 1950 | |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1951 | static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe, |
| 1952 | u16 *strength) |
| 1953 | { |
| 1954 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1955 | |
| 1956 | |
| 1957 | *strength = c->strength.stat[0].uvalue; |
| 1958 | |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe) |
| 1963 | { |
| 1964 | /* |
| 1965 | * get_frontend is now handled together with other stats |
| 1966 | * retrival, when read_status() is called, as some statistics |
| 1967 | * will depend on the layers detection. |
| 1968 | */ |
| 1969 | return 0; |
| 1970 | }; |
| 1971 | |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1972 | static int mb86a20s_tune(struct dvb_frontend *fe, |
Mauro Carvalho Chehab | 7e07222 | 2011-12-26 17:48:33 -0300 | [diff] [blame] | 1973 | bool re_tune, |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1974 | unsigned int mode_flags, |
| 1975 | unsigned int *delay, |
| 1976 | fe_status_t *status) |
| 1977 | { |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 1978 | struct mb86a20s_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1979 | int rc = 0; |
| 1980 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 1981 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1982 | |
Mauro Carvalho Chehab | 7e07222 | 2011-12-26 17:48:33 -0300 | [diff] [blame] | 1983 | if (re_tune) |
Mauro Carvalho Chehab | 2d76e22b | 2011-12-26 12:11:51 -0300 | [diff] [blame] | 1984 | rc = mb86a20s_set_frontend(fe); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1985 | |
| 1986 | if (!(mode_flags & FE_TUNE_MODE_ONESHOT)) |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 1987 | mb86a20s_read_status_and_stats(fe, status); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1988 | |
| 1989 | return rc; |
| 1990 | } |
| 1991 | |
| 1992 | static void mb86a20s_release(struct dvb_frontend *fe) |
| 1993 | { |
| 1994 | struct mb86a20s_state *state = fe->demodulator_priv; |
| 1995 | |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 1996 | dev_dbg(&state->i2c->dev, "%s called.\n", __func__); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 1997 | |
| 1998 | kfree(state); |
| 1999 | } |
| 2000 | |
| 2001 | static struct dvb_frontend_ops mb86a20s_ops; |
| 2002 | |
| 2003 | struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config, |
| 2004 | struct i2c_adapter *i2c) |
| 2005 | { |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 2006 | struct mb86a20s_state *state; |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2007 | u8 rev; |
| 2008 | |
Mauro Carvalho Chehab | f167e302 | 2013-01-23 13:22:22 -0200 | [diff] [blame] | 2009 | dev_dbg(&i2c->dev, "%s called.\n", __func__); |
| 2010 | |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2011 | /* allocate memory for the internal state */ |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 2012 | state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2013 | if (state == NULL) { |
Mauro Carvalho Chehab | f167e302 | 2013-01-23 13:22:22 -0200 | [diff] [blame] | 2014 | dev_err(&i2c->dev, |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 2015 | "%s: unable to allocate memory for state\n", __func__); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2016 | goto error; |
| 2017 | } |
| 2018 | |
| 2019 | /* setup the state */ |
| 2020 | state->config = config; |
| 2021 | state->i2c = i2c; |
| 2022 | |
| 2023 | /* create dvb_frontend */ |
| 2024 | memcpy(&state->frontend.ops, &mb86a20s_ops, |
| 2025 | sizeof(struct dvb_frontend_ops)); |
| 2026 | state->frontend.demodulator_priv = state; |
| 2027 | |
| 2028 | /* Check if it is a mb86a20s frontend */ |
| 2029 | rev = mb86a20s_readreg(state, 0); |
| 2030 | |
| 2031 | if (rev == 0x13) { |
Mauro Carvalho Chehab | f167e302 | 2013-01-23 13:22:22 -0200 | [diff] [blame] | 2032 | dev_info(&i2c->dev, |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 2033 | "Detected a Fujitsu mb86a20s frontend\n"); |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2034 | } else { |
Mauro Carvalho Chehab | f167e302 | 2013-01-23 13:22:22 -0200 | [diff] [blame] | 2035 | dev_dbg(&i2c->dev, |
Mauro Carvalho Chehab | f66d81b | 2013-01-22 09:13:08 -0200 | [diff] [blame] | 2036 | "Frontend revision %d is unknown - aborting.\n", |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2037 | rev); |
| 2038 | goto error; |
| 2039 | } |
| 2040 | |
| 2041 | return &state->frontend; |
| 2042 | |
| 2043 | error: |
| 2044 | kfree(state); |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | EXPORT_SYMBOL(mb86a20s_attach); |
| 2048 | |
| 2049 | static struct dvb_frontend_ops mb86a20s_ops = { |
Mauro Carvalho Chehab | 2d76e22b | 2011-12-26 12:11:51 -0300 | [diff] [blame] | 2050 | .delsys = { SYS_ISDBT }, |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2051 | /* Use dib8000 values per default */ |
| 2052 | .info = { |
| 2053 | .name = "Fujitsu mb86A20s", |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2054 | .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER | |
| 2055 | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 2056 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | |
| 2057 | FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | |
| 2058 | FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO | |
| 2059 | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO, |
| 2060 | /* Actually, those values depend on the used tuner */ |
| 2061 | .frequency_min = 45000000, |
| 2062 | .frequency_max = 864000000, |
| 2063 | .frequency_stepsize = 62500, |
| 2064 | }, |
| 2065 | |
| 2066 | .release = mb86a20s_release, |
| 2067 | |
| 2068 | .init = mb86a20s_initfe, |
Mauro Carvalho Chehab | 2d76e22b | 2011-12-26 12:11:51 -0300 | [diff] [blame] | 2069 | .set_frontend = mb86a20s_set_frontend, |
Mauro Carvalho Chehab | 09b6d21 | 2013-01-22 12:28:31 -0300 | [diff] [blame] | 2070 | .get_frontend = mb86a20s_get_frontend_dummy, |
| 2071 | .read_status = mb86a20s_read_status_and_stats, |
| 2072 | .read_signal_strength = mb86a20s_read_signal_strength_from_cache, |
Mauro Carvalho Chehab | b9ede79a | 2010-09-27 20:52:43 -0300 | [diff] [blame] | 2073 | .tune = mb86a20s_tune, |
| 2074 | }; |
| 2075 | |
| 2076 | MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware"); |
| 2077 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 2078 | MODULE_LICENSE("GPL"); |