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 }, |
| 45 | { 0x1001f, priv->cfg.if_agc_polarity << 7, 0x80 }, |
| 46 | { 0x10070, priv->cfg.ts_mode, 0xff }, |
CrazyCat | 4d1ab18 | 2014-06-03 14:19:07 -0300 | [diff] [blame] | 47 | { 0x10071, !priv->cfg.ts_clock_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 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 152 | int cxd2820r_read_ber_c(struct dvb_frontend *fe, u32 *ber) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 153 | { |
| 154 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 155 | int ret; |
| 156 | u8 buf[3], start_ber = 0; |
| 157 | *ber = 0; |
| 158 | |
| 159 | if (priv->ber_running) { |
| 160 | ret = cxd2820r_rd_regs(priv, 0x10076, buf, sizeof(buf)); |
| 161 | if (ret) |
| 162 | goto error; |
| 163 | |
| 164 | if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) { |
| 165 | *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0]; |
| 166 | start_ber = 1; |
| 167 | } |
| 168 | } else { |
Mauro Carvalho Chehab | 285c0b0 | 2014-09-03 15:22:02 -0300 | [diff] [blame] | 169 | priv->ber_running = true; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 170 | start_ber = 1; |
| 171 | } |
| 172 | |
| 173 | if (start_ber) { |
| 174 | /* (re)start BER */ |
| 175 | ret = cxd2820r_wr_reg(priv, 0x10079, 0x01); |
| 176 | if (ret) |
| 177 | goto error; |
| 178 | } |
| 179 | |
| 180 | return ret; |
| 181 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 182 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 186 | int cxd2820r_read_signal_strength_c(struct dvb_frontend *fe, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 187 | u16 *strength) |
| 188 | { |
| 189 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 190 | int ret; |
| 191 | u8 buf[2]; |
| 192 | u16 tmp; |
| 193 | |
| 194 | ret = cxd2820r_rd_regs(priv, 0x10049, buf, sizeof(buf)); |
| 195 | if (ret) |
| 196 | goto error; |
| 197 | |
| 198 | tmp = (buf[0] & 0x03) << 8 | buf[1]; |
| 199 | tmp = (~tmp & 0x03ff); |
| 200 | |
| 201 | if (tmp == 512) |
| 202 | /* ~no signal */ |
| 203 | tmp = 0; |
| 204 | else if (tmp > 350) |
| 205 | tmp = 350; |
| 206 | |
| 207 | /* scale value to 0x0000-0xffff */ |
| 208 | *strength = tmp * 0xffff / (350-0); |
| 209 | |
| 210 | return ret; |
| 211 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 212 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 216 | int cxd2820r_read_snr_c(struct dvb_frontend *fe, u16 *snr) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 217 | { |
| 218 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 219 | int ret; |
| 220 | u8 tmp; |
| 221 | unsigned int A, B; |
| 222 | /* report SNR in dB * 10 */ |
| 223 | |
| 224 | ret = cxd2820r_rd_reg(priv, 0x10019, &tmp); |
| 225 | if (ret) |
| 226 | goto error; |
| 227 | |
| 228 | if (((tmp >> 0) & 0x03) % 2) { |
| 229 | A = 875; |
| 230 | B = 650; |
| 231 | } else { |
| 232 | A = 950; |
| 233 | B = 760; |
| 234 | } |
| 235 | |
| 236 | ret = cxd2820r_rd_reg(priv, 0x1004d, &tmp); |
| 237 | if (ret) |
| 238 | goto error; |
| 239 | |
| 240 | #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */ |
| 241 | if (tmp) |
| 242 | *snr = A * (intlog2(B / tmp) >> 5) / (CXD2820R_LOG2_E_24 >> 5) |
| 243 | / 10; |
| 244 | else |
| 245 | *snr = 0; |
| 246 | |
| 247 | return ret; |
| 248 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 249 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 250 | return ret; |
| 251 | } |
| 252 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 253 | int cxd2820r_read_ucblocks_c(struct dvb_frontend *fe, u32 *ucblocks) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 254 | { |
| 255 | *ucblocks = 0; |
| 256 | /* no way to read ? */ |
| 257 | return 0; |
| 258 | } |
| 259 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 260 | 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] | 261 | { |
| 262 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 263 | int ret; |
| 264 | u8 buf[2]; |
| 265 | *status = 0; |
| 266 | |
| 267 | ret = cxd2820r_rd_regs(priv, 0x10088, buf, sizeof(buf)); |
| 268 | if (ret) |
| 269 | goto error; |
| 270 | |
| 271 | if (((buf[0] >> 0) & 0x01) == 1) { |
| 272 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 273 | FE_HAS_VITERBI | FE_HAS_SYNC; |
| 274 | |
| 275 | if (((buf[1] >> 3) & 0x01) == 1) { |
| 276 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 277 | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; |
| 278 | } |
| 279 | } |
| 280 | |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 281 | dev_dbg(&priv->i2c->dev, "%s: lock=%02x %02x\n", __func__, buf[0], |
| 282 | buf[1]); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 283 | |
| 284 | return ret; |
| 285 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 286 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 287 | return ret; |
| 288 | } |
| 289 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 290 | int cxd2820r_init_c(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 291 | { |
| 292 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 293 | int ret; |
| 294 | |
| 295 | ret = cxd2820r_wr_reg(priv, 0x00085, 0x07); |
| 296 | if (ret) |
| 297 | goto error; |
| 298 | |
| 299 | return ret; |
| 300 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 301 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 302 | return ret; |
| 303 | } |
| 304 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 305 | int cxd2820r_sleep_c(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 306 | { |
| 307 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
| 308 | int ret, i; |
| 309 | struct reg_val_mask tab[] = { |
| 310 | { 0x000ff, 0x1f, 0xff }, |
| 311 | { 0x00085, 0x00, 0xff }, |
| 312 | { 0x00088, 0x01, 0xff }, |
| 313 | { 0x00081, 0x00, 0xff }, |
| 314 | { 0x00080, 0x00, 0xff }, |
| 315 | }; |
| 316 | |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 317 | dev_dbg(&priv->i2c->dev, "%s\n", __func__); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 318 | |
| 319 | priv->delivery_system = SYS_UNDEFINED; |
| 320 | |
| 321 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 322 | ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val, |
| 323 | tab[i].mask); |
| 324 | if (ret) |
| 325 | goto error; |
| 326 | } |
| 327 | |
| 328 | return ret; |
| 329 | error: |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 330 | dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 331 | return ret; |
| 332 | } |
| 333 | |
Steve Kerrison | 9ac51c5 | 2011-05-02 18:19:13 -0300 | [diff] [blame] | 334 | int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe, |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 335 | struct dvb_frontend_tune_settings *s) |
| 336 | { |
| 337 | s->min_delay_ms = 500; |
| 338 | s->step_size = 0; /* no zigzag */ |
| 339 | s->max_drift = 0; |
| 340 | |
| 341 | return 0; |
| 342 | } |