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 | |
| 24 | int cxd2820r_set_frontend_t(struct dvb_frontend *fe, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 25 | struct dvb_frontend_parameters *p) |
| 26 | { |
| 27 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 28 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 29 | int ret, i; |
Antti Palosaari | fda23fa | 2011-11-13 14:41:25 -0300 | [diff] [blame^] | 30 | u32 if_freq, if_ctl; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 31 | u64 num; |
| 32 | u8 buf[3], bw_param; |
| 33 | u8 bw_params1[][5] = { |
| 34 | { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */ |
| 35 | { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */ |
| 36 | { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */ |
| 37 | }; |
| 38 | u8 bw_params2[][2] = { |
| 39 | { 0x1f, 0xdc }, /* 6 MHz */ |
| 40 | { 0x12, 0xf8 }, /* 7 MHz */ |
| 41 | { 0x01, 0xe0 }, /* 8 MHz */ |
| 42 | }; |
| 43 | struct reg_val_mask tab[] = { |
| 44 | { 0x00080, 0x00, 0xff }, |
| 45 | { 0x00081, 0x03, 0xff }, |
| 46 | { 0x00085, 0x07, 0xff }, |
| 47 | { 0x00088, 0x01, 0xff }, |
| 48 | |
| 49 | { 0x00070, priv->cfg.ts_mode, 0xff }, |
| 50 | { 0x000cb, priv->cfg.if_agc_polarity << 6, 0x40 }, |
| 51 | { 0x000a5, 0x00, 0x01 }, |
| 52 | { 0x00082, 0x20, 0x60 }, |
| 53 | { 0x000c2, 0xc3, 0xff }, |
| 54 | { 0x0016a, 0x50, 0xff }, |
| 55 | { 0x00427, 0x41, 0xff }, |
| 56 | }; |
| 57 | |
| 58 | dbg("%s: RF=%d BW=%d", __func__, c->frequency, c->bandwidth_hz); |
| 59 | |
| 60 | /* update GPIOs */ |
| 61 | ret = cxd2820r_gpio(fe); |
| 62 | if (ret) |
| 63 | goto error; |
| 64 | |
| 65 | /* program tuner */ |
| 66 | if (fe->ops.tuner_ops.set_params) |
| 67 | fe->ops.tuner_ops.set_params(fe, p); |
| 68 | |
| 69 | if (priv->delivery_system != SYS_DVBT) { |
| 70 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 71 | ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, |
| 72 | tab[i].val, tab[i].mask); |
| 73 | if (ret) |
| 74 | goto error; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | priv->delivery_system = SYS_DVBT; |
| 79 | priv->ber_running = 0; /* tune stops BER counter */ |
| 80 | |
| 81 | switch (c->bandwidth_hz) { |
| 82 | case 6000000: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 83 | i = 0; |
| 84 | bw_param = 2; |
| 85 | break; |
| 86 | case 7000000: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 87 | i = 1; |
| 88 | bw_param = 1; |
| 89 | break; |
| 90 | case 8000000: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 91 | i = 2; |
| 92 | bw_param = 0; |
| 93 | break; |
| 94 | default: |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
Antti Palosaari | fda23fa | 2011-11-13 14:41:25 -0300 | [diff] [blame^] | 98 | /* program IF frequency */ |
| 99 | if (fe->ops.tuner_ops.get_if_frequency) { |
| 100 | ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq); |
| 101 | if (ret) |
| 102 | goto error; |
| 103 | } else |
| 104 | if_freq = 0; |
| 105 | |
| 106 | dbg("%s: if_freq=%d", __func__, if_freq); |
| 107 | |
| 108 | num = if_freq / 1000; /* Hz => kHz */ |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 109 | num *= 0x1000000; |
| 110 | if_ctl = cxd2820r_div_u64_round_closest(num, 41000); |
| 111 | buf[0] = ((if_ctl >> 16) & 0xff); |
| 112 | buf[1] = ((if_ctl >> 8) & 0xff); |
| 113 | buf[2] = ((if_ctl >> 0) & 0xff); |
| 114 | |
| 115 | ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3); |
| 116 | if (ret) |
| 117 | goto error; |
| 118 | |
| 119 | ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[i], 5); |
| 120 | if (ret) |
| 121 | goto error; |
| 122 | |
| 123 | ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0); |
| 124 | if (ret) |
| 125 | goto error; |
| 126 | |
| 127 | ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[i], 2); |
| 128 | if (ret) |
| 129 | goto error; |
| 130 | |
| 131 | ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08); |
| 132 | if (ret) |
| 133 | goto error; |
| 134 | |
| 135 | ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01); |
| 136 | if (ret) |
| 137 | goto error; |
| 138 | |
| 139 | return ret; |
| 140 | error: |
| 141 | dbg("%s: failed:%d", __func__, ret); |
| 142 | return ret; |
| 143 | } |
| 144 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 145 | int cxd2820r_get_frontend_t(struct dvb_frontend *fe, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 146 | struct dvb_frontend_parameters *p) |
| 147 | { |
| 148 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 149 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 150 | int ret; |
| 151 | u8 buf[2]; |
| 152 | |
| 153 | ret = cxd2820r_rd_regs(priv, 0x0002f, buf, sizeof(buf)); |
| 154 | if (ret) |
| 155 | goto error; |
| 156 | |
| 157 | switch ((buf[0] >> 6) & 0x03) { |
| 158 | case 0: |
| 159 | c->modulation = QPSK; |
| 160 | break; |
| 161 | case 1: |
| 162 | c->modulation = QAM_16; |
| 163 | break; |
| 164 | case 2: |
| 165 | c->modulation = QAM_64; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | switch ((buf[1] >> 1) & 0x03) { |
| 170 | case 0: |
| 171 | c->transmission_mode = TRANSMISSION_MODE_2K; |
| 172 | break; |
| 173 | case 1: |
| 174 | c->transmission_mode = TRANSMISSION_MODE_8K; |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | switch ((buf[1] >> 3) & 0x03) { |
| 179 | case 0: |
| 180 | c->guard_interval = GUARD_INTERVAL_1_32; |
| 181 | break; |
| 182 | case 1: |
| 183 | c->guard_interval = GUARD_INTERVAL_1_16; |
| 184 | break; |
| 185 | case 2: |
| 186 | c->guard_interval = GUARD_INTERVAL_1_8; |
| 187 | break; |
| 188 | case 3: |
| 189 | c->guard_interval = GUARD_INTERVAL_1_4; |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | switch ((buf[0] >> 3) & 0x07) { |
| 194 | case 0: |
| 195 | c->hierarchy = HIERARCHY_NONE; |
| 196 | break; |
| 197 | case 1: |
| 198 | c->hierarchy = HIERARCHY_1; |
| 199 | break; |
| 200 | case 2: |
| 201 | c->hierarchy = HIERARCHY_2; |
| 202 | break; |
| 203 | case 3: |
| 204 | c->hierarchy = HIERARCHY_4; |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | switch ((buf[0] >> 0) & 0x07) { |
| 209 | case 0: |
| 210 | c->code_rate_HP = FEC_1_2; |
| 211 | break; |
| 212 | case 1: |
| 213 | c->code_rate_HP = FEC_2_3; |
| 214 | break; |
| 215 | case 2: |
| 216 | c->code_rate_HP = FEC_3_4; |
| 217 | break; |
| 218 | case 3: |
| 219 | c->code_rate_HP = FEC_5_6; |
| 220 | break; |
| 221 | case 4: |
| 222 | c->code_rate_HP = FEC_7_8; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | switch ((buf[1] >> 5) & 0x07) { |
| 227 | case 0: |
| 228 | c->code_rate_LP = FEC_1_2; |
| 229 | break; |
| 230 | case 1: |
| 231 | c->code_rate_LP = FEC_2_3; |
| 232 | break; |
| 233 | case 2: |
| 234 | c->code_rate_LP = FEC_3_4; |
| 235 | break; |
| 236 | case 3: |
| 237 | c->code_rate_LP = FEC_5_6; |
| 238 | break; |
| 239 | case 4: |
| 240 | c->code_rate_LP = FEC_7_8; |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | ret = cxd2820r_rd_reg(priv, 0x007c6, &buf[0]); |
| 245 | if (ret) |
| 246 | goto error; |
| 247 | |
| 248 | switch ((buf[0] >> 0) & 0x01) { |
| 249 | case 0: |
| 250 | c->inversion = INVERSION_OFF; |
| 251 | break; |
| 252 | case 1: |
| 253 | c->inversion = INVERSION_ON; |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | return ret; |
| 258 | error: |
| 259 | dbg("%s: failed:%d", __func__, ret); |
| 260 | return ret; |
| 261 | } |
| 262 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 263 | int cxd2820r_read_ber_t(struct dvb_frontend *fe, u32 *ber) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 264 | { |
| 265 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 266 | int ret; |
| 267 | u8 buf[3], start_ber = 0; |
| 268 | *ber = 0; |
| 269 | |
| 270 | if (priv->ber_running) { |
| 271 | ret = cxd2820r_rd_regs(priv, 0x00076, buf, sizeof(buf)); |
| 272 | if (ret) |
| 273 | goto error; |
| 274 | |
| 275 | if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) { |
| 276 | *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0]; |
| 277 | start_ber = 1; |
| 278 | } |
| 279 | } else { |
| 280 | priv->ber_running = 1; |
| 281 | start_ber = 1; |
| 282 | } |
| 283 | |
| 284 | if (start_ber) { |
| 285 | /* (re)start BER */ |
| 286 | ret = cxd2820r_wr_reg(priv, 0x00079, 0x01); |
| 287 | if (ret) |
| 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | return ret; |
| 292 | error: |
| 293 | dbg("%s: failed:%d", __func__, ret); |
| 294 | return ret; |
| 295 | } |
| 296 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 297 | int cxd2820r_read_signal_strength_t(struct dvb_frontend *fe, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 298 | u16 *strength) |
| 299 | { |
| 300 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 301 | int ret; |
| 302 | u8 buf[2]; |
| 303 | u16 tmp; |
| 304 | |
| 305 | ret = cxd2820r_rd_regs(priv, 0x00026, buf, sizeof(buf)); |
| 306 | if (ret) |
| 307 | goto error; |
| 308 | |
| 309 | tmp = (buf[0] & 0x0f) << 8 | buf[1]; |
| 310 | tmp = ~tmp & 0x0fff; |
| 311 | |
| 312 | /* scale value to 0x0000-0xffff from 0x0000-0x0fff */ |
| 313 | *strength = tmp * 0xffff / 0x0fff; |
| 314 | |
| 315 | return ret; |
| 316 | error: |
| 317 | dbg("%s: failed:%d", __func__, ret); |
| 318 | return ret; |
| 319 | } |
| 320 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 321 | int cxd2820r_read_snr_t(struct dvb_frontend *fe, u16 *snr) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 322 | { |
| 323 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 324 | int ret; |
| 325 | u8 buf[2]; |
| 326 | u16 tmp; |
| 327 | /* report SNR in dB * 10 */ |
| 328 | |
| 329 | ret = cxd2820r_rd_regs(priv, 0x00028, buf, sizeof(buf)); |
| 330 | if (ret) |
| 331 | goto error; |
| 332 | |
| 333 | tmp = (buf[0] & 0x1f) << 8 | buf[1]; |
| 334 | #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */ |
| 335 | if (tmp) |
| 336 | *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24) |
| 337 | / 100); |
| 338 | else |
| 339 | *snr = 0; |
| 340 | |
| 341 | dbg("%s: dBx10=%d val=%04x", __func__, *snr, tmp); |
| 342 | |
| 343 | return ret; |
| 344 | error: |
| 345 | dbg("%s: failed:%d", __func__, ret); |
| 346 | return ret; |
| 347 | } |
| 348 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 349 | int cxd2820r_read_ucblocks_t(struct dvb_frontend *fe, u32 *ucblocks) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 350 | { |
| 351 | *ucblocks = 0; |
| 352 | /* no way to read ? */ |
| 353 | return 0; |
| 354 | } |
| 355 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 356 | int cxd2820r_read_status_t(struct dvb_frontend *fe, fe_status_t *status) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 357 | { |
| 358 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 359 | int ret; |
| 360 | u8 buf[4]; |
| 361 | *status = 0; |
| 362 | |
| 363 | ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]); |
| 364 | if (ret) |
| 365 | goto error; |
| 366 | |
| 367 | if ((buf[0] & 0x07) == 6) { |
| 368 | ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]); |
| 369 | if (ret) |
| 370 | goto error; |
| 371 | |
| 372 | if (((buf[1] >> 3) & 0x01) == 1) { |
| 373 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 374 | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; |
| 375 | } else { |
| 376 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 377 | FE_HAS_VITERBI | FE_HAS_SYNC; |
| 378 | } |
| 379 | } else { |
| 380 | ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]); |
| 381 | if (ret) |
| 382 | goto error; |
| 383 | |
| 384 | if ((buf[2] & 0x0f) >= 4) { |
| 385 | ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]); |
| 386 | if (ret) |
| 387 | goto error; |
| 388 | |
| 389 | if (((buf[3] >> 4) & 0x01) == 1) |
| 390 | *status |= FE_HAS_SIGNAL; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | dbg("%s: lock=%02x %02x %02x %02x", __func__, |
| 395 | buf[0], buf[1], buf[2], buf[3]); |
| 396 | |
| 397 | return ret; |
| 398 | error: |
| 399 | dbg("%s: failed:%d", __func__, ret); |
| 400 | return ret; |
| 401 | } |
| 402 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 403 | int cxd2820r_init_t(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 404 | { |
| 405 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 406 | int ret; |
| 407 | |
| 408 | ret = cxd2820r_wr_reg(priv, 0x00085, 0x07); |
| 409 | if (ret) |
| 410 | goto error; |
| 411 | |
| 412 | return ret; |
| 413 | error: |
| 414 | dbg("%s: failed:%d", __func__, ret); |
| 415 | return ret; |
| 416 | } |
| 417 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 418 | int cxd2820r_sleep_t(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 419 | { |
| 420 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 421 | int ret, i; |
| 422 | struct reg_val_mask tab[] = { |
| 423 | { 0x000ff, 0x1f, 0xff }, |
| 424 | { 0x00085, 0x00, 0xff }, |
| 425 | { 0x00088, 0x01, 0xff }, |
| 426 | { 0x00081, 0x00, 0xff }, |
| 427 | { 0x00080, 0x00, 0xff }, |
| 428 | }; |
| 429 | |
| 430 | dbg("%s", __func__); |
| 431 | |
| 432 | priv->delivery_system = SYS_UNDEFINED; |
| 433 | |
| 434 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 435 | ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val, |
| 436 | tab[i].mask); |
| 437 | if (ret) |
| 438 | goto error; |
| 439 | } |
| 440 | |
| 441 | return ret; |
| 442 | error: |
| 443 | dbg("%s: failed:%d", __func__, ret); |
| 444 | return ret; |
| 445 | } |
| 446 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 447 | int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 448 | struct dvb_frontend_tune_settings *s) |
| 449 | { |
| 450 | s->min_delay_ms = 500; |
| 451 | s->step_size = fe->ops.info.frequency_stepsize * 2; |
| 452 | s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1; |
| 453 | |
| 454 | return 0; |
| 455 | } |