Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Sony CXD2820R demodulator driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Antti Palosaari <crope@iki.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 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 |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 22 | #include "cxd2820r_priv.h" |
| 23 | |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 24 | int cxd2820r_set_frontend_c(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 25 | { |
| 26 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 27 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 28 | int ret, i; |
Antti Palosaari | fcd09f6 | 2016-08-08 15:54:10 -0300 | [diff] [blame] | 29 | unsigned int utmp; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 30 | u8 buf[2]; |
Antti Palosaari | fcd09f6 | 2016-08-08 15:54:10 -0300 | [diff] [blame] | 31 | u32 if_frequency; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 32 | struct reg_val_mask tab[] = { |
| 33 | { 0x00080, 0x01, 0xff }, |
| 34 | { 0x00081, 0x05, 0xff }, |
| 35 | { 0x00085, 0x07, 0xff }, |
| 36 | { 0x00088, 0x01, 0xff }, |
| 37 | |
| 38 | { 0x00082, 0x20, 0x60 }, |
| 39 | { 0x1016a, 0x48, 0xff }, |
| 40 | { 0x100a5, 0x00, 0x01 }, |
| 41 | { 0x10020, 0x06, 0x07 }, |
| 42 | { 0x10059, 0x50, 0xff }, |
| 43 | { 0x10087, 0x0c, 0x3c }, |
| 44 | { 0x1008b, 0x07, 0xff }, |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame^] | 45 | { 0x1001f, priv->if_agc_polarity << 7, 0x80 }, |
| 46 | { 0x10070, priv->ts_mode, 0xff }, |
| 47 | { 0x10071, !priv->ts_clk_inv << 4, 0x10 }, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 48 | }; |
| 49 | |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 50 | dev_dbg(&priv->i2c->dev, "%s: frequency=%d symbol_rate=%d\n", __func__, |
| 51 | c->frequency, c->symbol_rate); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 52 | |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 53 | /* program tuner */ |
| 54 | if (fe->ops.tuner_ops.set_params) |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 55 | fe->ops.tuner_ops.set_params(fe); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 56 | |
Mauro Carvalho Chehab | 0349471 | 2011-12-22 18:11:39 -0300 | [diff] [blame] | 57 | if (priv->delivery_system != SYS_DVBC_ANNEX_A) { |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 58 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 59 | ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, |
| 60 | tab[i].val, tab[i].mask); |
| 61 | if (ret) |
| 62 | goto error; |
| 63 | } |
| 64 | } |
| 65 | |
Mauro Carvalho Chehab | 0349471 | 2011-12-22 18:11:39 -0300 | [diff] [blame] | 66 | priv->delivery_system = SYS_DVBC_ANNEX_A; |
Mauro Carvalho Chehab | 285c0b0 | 2014-09-03 15:22:02 -0300 | [diff] [blame] | 67 | priv->ber_running = false; /* tune stops BER counter */ |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 68 | |
Antti Palosaari | fda23fa | 2011-11-13 14:41:25 -0300 | [diff] [blame] | 69 | /* program IF frequency */ |
| 70 | if (fe->ops.tuner_ops.get_if_frequency) { |
Antti Palosaari | fcd09f6 | 2016-08-08 15:54:10 -0300 | [diff] [blame] | 71 | ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); |
Antti Palosaari | fda23fa | 2011-11-13 14:41:25 -0300 | [diff] [blame] | 72 | if (ret) |
| 73 | goto error; |
Antti Palosaari | fcd09f6 | 2016-08-08 15:54:10 -0300 | [diff] [blame] | 74 | dev_dbg(&priv->i2c->dev, "%s: if_frequency=%u\n", __func__, |
| 75 | if_frequency); |
| 76 | } else { |
| 77 | ret = -EINVAL; |
| 78 | goto error; |
| 79 | } |
Antti Palosaari | fda23fa | 2011-11-13 14:41:25 -0300 | [diff] [blame] | 80 | |
Antti Palosaari | fcd09f6 | 2016-08-08 15:54:10 -0300 | [diff] [blame] | 81 | utmp = 0x4000 - DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x4000, CXD2820R_CLK); |
| 82 | buf[0] = (utmp >> 8) & 0xff; |
| 83 | buf[1] = (utmp >> 0) & 0xff; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 84 | ret = cxd2820r_wr_regs(priv, 0x10042, buf, 2); |
| 85 | if (ret) |
| 86 | goto error; |
| 87 | |
| 88 | ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08); |
| 89 | if (ret) |
| 90 | goto error; |
| 91 | |
| 92 | ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01); |
| 93 | if (ret) |
| 94 | goto error; |
| 95 | |
| 96 | return ret; |
| 97 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 98 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 102 | int cxd2820r_get_frontend_c(struct dvb_frontend *fe, |
| 103 | struct dtv_frontend_properties *c) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 104 | { |
| 105 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 106 | int ret; |
| 107 | u8 buf[2]; |
| 108 | |
| 109 | ret = cxd2820r_rd_regs(priv, 0x1001a, buf, 2); |
| 110 | if (ret) |
| 111 | goto error; |
| 112 | |
| 113 | c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]); |
| 114 | |
| 115 | ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]); |
| 116 | if (ret) |
| 117 | goto error; |
| 118 | |
Janne Huttunen | cba5d0b | 2012-05-30 05:28:46 -0300 | [diff] [blame] | 119 | switch ((buf[0] >> 0) & 0x07) { |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 120 | case 0: |
| 121 | c->modulation = QAM_16; |
| 122 | break; |
| 123 | case 1: |
| 124 | c->modulation = QAM_32; |
| 125 | break; |
| 126 | case 2: |
| 127 | c->modulation = QAM_64; |
| 128 | break; |
| 129 | case 3: |
| 130 | c->modulation = QAM_128; |
| 131 | break; |
| 132 | case 4: |
| 133 | c->modulation = QAM_256; |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | switch ((buf[0] >> 7) & 0x01) { |
| 138 | case 0: |
| 139 | c->inversion = INVERSION_OFF; |
| 140 | break; |
| 141 | case 1: |
| 142 | c->inversion = INVERSION_ON; |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | return ret; |
| 147 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 148 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 149 | return ret; |
| 150 | } |
| 151 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 152 | int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 153 | { |
| 154 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | 90d5d2e | 2016-08-08 23:03:32 -0300 | [diff] [blame] | 155 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 156 | int ret; |
Antti Palosaari | 90d5d2e | 2016-08-08 23:03:32 -0300 | [diff] [blame] | 157 | unsigned int utmp; |
| 158 | u8 buf[3]; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 159 | *status = 0; |
| 160 | |
Antti Palosaari | 90d5d2e | 2016-08-08 23:03:32 -0300 | [diff] [blame] | 161 | ret = cxd2820r_rd_regs(priv, 0x10088, buf, 2); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 162 | if (ret) |
| 163 | goto error; |
| 164 | |
| 165 | if (((buf[0] >> 0) & 0x01) == 1) { |
| 166 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 167 | FE_HAS_VITERBI | FE_HAS_SYNC; |
| 168 | |
| 169 | if (((buf[1] >> 3) & 0x01) == 1) { |
| 170 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 171 | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; |
| 172 | } |
| 173 | } |
| 174 | |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 175 | dev_dbg(&priv->i2c->dev, "%s: lock=%02x %02x\n", __func__, buf[0], |
| 176 | buf[1]); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 177 | |
Antti Palosaari | 90d5d2e | 2016-08-08 23:03:32 -0300 | [diff] [blame] | 178 | /* Signal strength */ |
| 179 | if (*status & FE_HAS_SIGNAL) { |
| 180 | unsigned int strength; |
| 181 | |
| 182 | ret = cxd2820r_rd_regs(priv, 0x10049, buf, 2); |
| 183 | if (ret) |
| 184 | goto error; |
| 185 | |
| 186 | utmp = buf[0] << 8 | buf[1] << 0; |
| 187 | utmp = 511 - sign_extend32(utmp, 9); |
| 188 | /* Scale value to 0x0000-0xffff */ |
| 189 | strength = utmp << 6 | utmp >> 4; |
| 190 | |
| 191 | c->strength.len = 1; |
| 192 | c->strength.stat[0].scale = FE_SCALE_RELATIVE; |
| 193 | c->strength.stat[0].uvalue = strength; |
| 194 | } else { |
| 195 | c->strength.len = 1; |
| 196 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 197 | } |
| 198 | |
| 199 | /* CNR */ |
| 200 | if (*status & FE_HAS_VITERBI) { |
| 201 | unsigned int cnr, const_a, const_b; |
| 202 | |
| 203 | ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]); |
| 204 | if (ret) |
| 205 | goto error; |
| 206 | |
| 207 | if (((buf[0] >> 0) & 0x03) % 2) { |
| 208 | const_a = 8750; |
| 209 | const_b = 650; |
| 210 | } else { |
| 211 | const_a = 9500; |
| 212 | const_b = 760; |
| 213 | } |
| 214 | |
| 215 | ret = cxd2820r_rd_reg(priv, 0x1004d, &buf[0]); |
| 216 | if (ret) |
| 217 | goto error; |
| 218 | |
| 219 | utmp = buf[0] << 0; |
| 220 | #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */ |
| 221 | if (utmp) |
| 222 | cnr = div_u64((u64)(intlog2(const_b) - intlog2(utmp)) |
| 223 | * const_a, CXD2820R_LOG2_E_24); |
| 224 | else |
| 225 | cnr = 0; |
| 226 | |
| 227 | c->cnr.len = 1; |
| 228 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 229 | c->cnr.stat[0].svalue = cnr; |
| 230 | } else { |
| 231 | c->cnr.len = 1; |
| 232 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 233 | } |
| 234 | |
| 235 | /* BER */ |
| 236 | if (*status & FE_HAS_SYNC) { |
| 237 | unsigned int post_bit_error; |
| 238 | bool start_ber; |
| 239 | |
| 240 | if (priv->ber_running) { |
| 241 | ret = cxd2820r_rd_regs(priv, 0x10076, buf, 3); |
| 242 | if (ret) |
| 243 | goto error; |
| 244 | |
| 245 | if ((buf[2] >> 7) & 0x01) { |
| 246 | post_bit_error = buf[2] << 16 | buf[1] << 8 | |
| 247 | buf[0] << 0; |
| 248 | post_bit_error &= 0x0fffff; |
| 249 | start_ber = true; |
| 250 | } else { |
| 251 | post_bit_error = 0; |
| 252 | start_ber = false; |
| 253 | } |
| 254 | } else { |
| 255 | post_bit_error = 0; |
| 256 | start_ber = true; |
| 257 | } |
| 258 | |
| 259 | if (start_ber) { |
| 260 | ret = cxd2820r_wr_reg(priv, 0x10079, 0x01); |
| 261 | if (ret) |
| 262 | goto error; |
| 263 | priv->ber_running = true; |
| 264 | } |
| 265 | |
| 266 | priv->post_bit_error += post_bit_error; |
| 267 | |
| 268 | c->post_bit_error.len = 1; |
| 269 | c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; |
| 270 | c->post_bit_error.stat[0].uvalue = priv->post_bit_error; |
| 271 | } else { |
| 272 | c->post_bit_error.len = 1; |
| 273 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 274 | } |
| 275 | |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 276 | return ret; |
| 277 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 278 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 279 | return ret; |
| 280 | } |
| 281 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 282 | int cxd2820r_init_c(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 283 | { |
| 284 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 285 | int ret; |
| 286 | |
| 287 | ret = cxd2820r_wr_reg(priv, 0x00085, 0x07); |
| 288 | if (ret) |
| 289 | goto error; |
| 290 | |
| 291 | return ret; |
| 292 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 293 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 294 | return ret; |
| 295 | } |
| 296 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 297 | int cxd2820r_sleep_c(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 298 | { |
| 299 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 300 | int ret, i; |
| 301 | struct reg_val_mask tab[] = { |
| 302 | { 0x000ff, 0x1f, 0xff }, |
| 303 | { 0x00085, 0x00, 0xff }, |
| 304 | { 0x00088, 0x01, 0xff }, |
| 305 | { 0x00081, 0x00, 0xff }, |
| 306 | { 0x00080, 0x00, 0xff }, |
| 307 | }; |
| 308 | |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 309 | dev_dbg(&priv->i2c->dev, "%s\n", __func__); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 310 | |
| 311 | priv->delivery_system = SYS_UNDEFINED; |
| 312 | |
| 313 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 314 | ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val, |
| 315 | tab[i].mask); |
| 316 | if (ret) |
| 317 | goto error; |
| 318 | } |
| 319 | |
| 320 | return ret; |
| 321 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 322 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 326 | int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 327 | struct dvb_frontend_tune_settings *s) |
| 328 | { |
| 329 | s->min_delay_ms = 500; |
| 330 | s->step_size = 0; /* no zigzag */ |
| 331 | s->max_drift = 0; |
| 332 | |
| 333 | return 0; |
| 334 | } |