rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Silicon Labs Si2168 DVB-T/T2/C demodulator driver |
| 3 | * |
| 4 | * Copyright (C) 2014 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 | |
| 17 | #include <linux/delay.h> |
| 18 | |
| 19 | #include "si2168_priv.h" |
| 20 | |
| 21 | static const struct dvb_frontend_ops si2168_ops; |
| 22 | |
| 23 | /* execute firmware command */ |
| 24 | static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd) |
| 25 | { |
| 26 | struct si2168_dev *dev = i2c_get_clientdata(client); |
| 27 | int ret; |
| 28 | unsigned long timeout; |
| 29 | |
| 30 | mutex_lock(&dev->i2c_mutex); |
| 31 | |
| 32 | if (cmd->wlen) { |
| 33 | /* write cmd and args for firmware */ |
| 34 | ret = i2c_master_send(client, cmd->args, cmd->wlen); |
| 35 | if (ret < 0) { |
| 36 | goto err_mutex_unlock; |
| 37 | } else if (ret != cmd->wlen) { |
| 38 | ret = -EREMOTEIO; |
| 39 | goto err_mutex_unlock; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if (cmd->rlen) { |
| 44 | /* wait cmd execution terminate */ |
| 45 | #define TIMEOUT 70 |
| 46 | timeout = jiffies + msecs_to_jiffies(TIMEOUT); |
| 47 | while (!time_after(jiffies, timeout)) { |
| 48 | ret = i2c_master_recv(client, cmd->args, cmd->rlen); |
| 49 | if (ret < 0) { |
| 50 | goto err_mutex_unlock; |
| 51 | } else if (ret != cmd->rlen) { |
| 52 | ret = -EREMOTEIO; |
| 53 | goto err_mutex_unlock; |
| 54 | } |
| 55 | |
| 56 | /* firmware ready? */ |
| 57 | if ((cmd->args[0] >> 7) & 0x01) |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | dev_dbg(&client->dev, "cmd execution took %d ms\n", |
| 62 | jiffies_to_msecs(jiffies) - |
| 63 | (jiffies_to_msecs(timeout) - TIMEOUT)); |
| 64 | |
| 65 | /* error bit set? */ |
| 66 | if ((cmd->args[0] >> 6) & 0x01) { |
| 67 | ret = -EREMOTEIO; |
| 68 | goto err_mutex_unlock; |
| 69 | } |
| 70 | |
| 71 | if (!((cmd->args[0] >> 7) & 0x01)) { |
| 72 | ret = -ETIMEDOUT; |
| 73 | goto err_mutex_unlock; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | mutex_unlock(&dev->i2c_mutex); |
| 78 | return 0; |
| 79 | err_mutex_unlock: |
| 80 | mutex_unlock(&dev->i2c_mutex); |
| 81 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status) |
| 86 | { |
| 87 | struct i2c_client *client = fe->demodulator_priv; |
| 88 | struct si2168_dev *dev = i2c_get_clientdata(client); |
| 89 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 90 | int ret, i; |
| 91 | unsigned int utmp, utmp1, utmp2; |
| 92 | struct si2168_cmd cmd; |
| 93 | |
| 94 | *status = 0; |
| 95 | |
| 96 | if (!dev->active) { |
| 97 | ret = -EAGAIN; |
| 98 | goto err; |
| 99 | } |
| 100 | |
| 101 | switch (c->delivery_system) { |
| 102 | case SYS_DVBT: |
| 103 | memcpy(cmd.args, "\xa0\x01", 2); |
| 104 | cmd.wlen = 2; |
| 105 | cmd.rlen = 13; |
| 106 | break; |
| 107 | case SYS_DVBC_ANNEX_A: |
| 108 | memcpy(cmd.args, "\x90\x01", 2); |
| 109 | cmd.wlen = 2; |
| 110 | cmd.rlen = 9; |
| 111 | break; |
| 112 | case SYS_DVBT2: |
| 113 | memcpy(cmd.args, "\x50\x01", 2); |
| 114 | cmd.wlen = 2; |
| 115 | cmd.rlen = 14; |
| 116 | break; |
| 117 | default: |
| 118 | ret = -EINVAL; |
| 119 | goto err; |
| 120 | } |
| 121 | |
| 122 | ret = si2168_cmd_execute(client, &cmd); |
| 123 | if (ret) |
| 124 | goto err; |
| 125 | |
| 126 | switch ((cmd.args[2] >> 1) & 0x03) { |
| 127 | case 0x01: |
| 128 | *status = FE_HAS_SIGNAL | FE_HAS_CARRIER; |
| 129 | break; |
| 130 | case 0x03: |
| 131 | *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | |
| 132 | FE_HAS_SYNC | FE_HAS_LOCK; |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | dev->fe_status = *status; |
| 137 | |
| 138 | if (*status & FE_HAS_LOCK) { |
| 139 | c->cnr.len = 1; |
| 140 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 141 | c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4; |
| 142 | } else { |
| 143 | c->cnr.len = 1; |
| 144 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 145 | } |
| 146 | |
| 147 | dev_dbg(&client->dev, "status=%02x args=%*ph\n", |
| 148 | *status, cmd.rlen, cmd.args); |
| 149 | |
| 150 | /* BER */ |
| 151 | if (*status & FE_HAS_VITERBI) { |
| 152 | memcpy(cmd.args, "\x82\x00", 2); |
| 153 | cmd.wlen = 2; |
| 154 | cmd.rlen = 3; |
| 155 | ret = si2168_cmd_execute(client, &cmd); |
| 156 | if (ret) |
| 157 | goto err; |
| 158 | |
| 159 | /* |
| 160 | * Firmware returns [0, 255] mantissa and [0, 8] exponent. |
| 161 | * Convert to DVB API: mantissa * 10^(8 - exponent) / 10^8 |
| 162 | */ |
| 163 | utmp = clamp(8 - cmd.args[1], 0, 8); |
| 164 | for (i = 0, utmp1 = 1; i < utmp; i++) |
| 165 | utmp1 = utmp1 * 10; |
| 166 | |
| 167 | utmp1 = cmd.args[2] * utmp1; |
| 168 | utmp2 = 100000000; /* 10^8 */ |
| 169 | |
| 170 | dev_dbg(&client->dev, |
| 171 | "post_bit_error=%u post_bit_count=%u ber=%u*10^-%u\n", |
| 172 | utmp1, utmp2, cmd.args[2], cmd.args[1]); |
| 173 | |
| 174 | c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; |
| 175 | c->post_bit_error.stat[0].uvalue += utmp1; |
| 176 | c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; |
| 177 | c->post_bit_count.stat[0].uvalue += utmp2; |
| 178 | } else { |
| 179 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 180 | c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 181 | } |
| 182 | |
| 183 | /* UCB */ |
| 184 | if (*status & FE_HAS_SYNC) { |
| 185 | memcpy(cmd.args, "\x84\x01", 2); |
| 186 | cmd.wlen = 2; |
| 187 | cmd.rlen = 3; |
| 188 | ret = si2168_cmd_execute(client, &cmd); |
| 189 | if (ret) |
| 190 | goto err; |
| 191 | |
| 192 | utmp1 = cmd.args[2] << 8 | cmd.args[1] << 0; |
| 193 | dev_dbg(&client->dev, "block_error=%u\n", utmp1); |
| 194 | |
| 195 | /* Sometimes firmware returns bogus value */ |
| 196 | if (utmp1 == 0xffff) |
| 197 | utmp1 = 0; |
| 198 | |
| 199 | c->block_error.stat[0].scale = FE_SCALE_COUNTER; |
| 200 | c->block_error.stat[0].uvalue += utmp1; |
| 201 | } else { |
| 202 | c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | err: |
| 207 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static int si2168_set_frontend(struct dvb_frontend *fe) |
| 212 | { |
| 213 | struct i2c_client *client = fe->demodulator_priv; |
| 214 | struct si2168_dev *dev = i2c_get_clientdata(client); |
| 215 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 216 | int ret; |
| 217 | struct si2168_cmd cmd; |
| 218 | u8 bandwidth, delivery_system; |
| 219 | |
| 220 | dev_dbg(&client->dev, |
| 221 | "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n", |
| 222 | c->delivery_system, c->modulation, c->frequency, |
| 223 | c->bandwidth_hz, c->symbol_rate, c->inversion, |
| 224 | c->stream_id); |
| 225 | |
| 226 | if (!dev->active) { |
| 227 | ret = -EAGAIN; |
| 228 | goto err; |
| 229 | } |
| 230 | |
| 231 | switch (c->delivery_system) { |
| 232 | case SYS_DVBT: |
| 233 | delivery_system = 0x20; |
| 234 | break; |
| 235 | case SYS_DVBC_ANNEX_A: |
| 236 | delivery_system = 0x30; |
| 237 | break; |
| 238 | case SYS_DVBT2: |
| 239 | delivery_system = 0x70; |
| 240 | break; |
| 241 | default: |
| 242 | ret = -EINVAL; |
| 243 | goto err; |
| 244 | } |
| 245 | |
| 246 | if (c->bandwidth_hz == 0) { |
| 247 | ret = -EINVAL; |
| 248 | goto err; |
| 249 | } else if (c->bandwidth_hz <= 2000000) |
| 250 | bandwidth = 0x02; |
| 251 | else if (c->bandwidth_hz <= 5000000) |
| 252 | bandwidth = 0x05; |
| 253 | else if (c->bandwidth_hz <= 6000000) |
| 254 | bandwidth = 0x06; |
| 255 | else if (c->bandwidth_hz <= 7000000) |
| 256 | bandwidth = 0x07; |
| 257 | else if (c->bandwidth_hz <= 8000000) |
| 258 | bandwidth = 0x08; |
| 259 | else if (c->bandwidth_hz <= 9000000) |
| 260 | bandwidth = 0x09; |
| 261 | else if (c->bandwidth_hz <= 10000000) |
| 262 | bandwidth = 0x0a; |
| 263 | else |
| 264 | bandwidth = 0x0f; |
| 265 | |
| 266 | /* program tuner */ |
| 267 | if (fe->ops.tuner_ops.set_params) { |
| 268 | ret = fe->ops.tuner_ops.set_params(fe); |
| 269 | if (ret) |
| 270 | goto err; |
| 271 | } |
| 272 | |
| 273 | memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5); |
| 274 | cmd.wlen = 5; |
| 275 | cmd.rlen = 5; |
| 276 | ret = si2168_cmd_execute(client, &cmd); |
| 277 | if (ret) |
| 278 | goto err; |
| 279 | |
| 280 | /* that has no big effect */ |
| 281 | if (c->delivery_system == SYS_DVBT) |
| 282 | memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6); |
| 283 | else if (c->delivery_system == SYS_DVBC_ANNEX_A) |
| 284 | memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6); |
| 285 | else if (c->delivery_system == SYS_DVBT2) |
| 286 | memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6); |
| 287 | cmd.wlen = 6; |
| 288 | cmd.rlen = 3; |
| 289 | ret = si2168_cmd_execute(client, &cmd); |
| 290 | if (ret) |
| 291 | goto err; |
| 292 | |
| 293 | if (c->delivery_system == SYS_DVBT2) { |
| 294 | /* select PLP */ |
| 295 | cmd.args[0] = 0x52; |
| 296 | cmd.args[1] = c->stream_id & 0xff; |
| 297 | cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1; |
| 298 | cmd.wlen = 3; |
| 299 | cmd.rlen = 1; |
| 300 | ret = si2168_cmd_execute(client, &cmd); |
| 301 | if (ret) |
| 302 | goto err; |
| 303 | } |
| 304 | |
| 305 | memcpy(cmd.args, "\x51\x03", 2); |
| 306 | cmd.wlen = 2; |
| 307 | cmd.rlen = 12; |
| 308 | ret = si2168_cmd_execute(client, &cmd); |
| 309 | if (ret) |
| 310 | goto err; |
| 311 | |
| 312 | memcpy(cmd.args, "\x12\x08\x04", 3); |
| 313 | cmd.wlen = 3; |
| 314 | cmd.rlen = 3; |
| 315 | ret = si2168_cmd_execute(client, &cmd); |
| 316 | if (ret) |
| 317 | goto err; |
| 318 | |
| 319 | memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6); |
| 320 | cmd.wlen = 6; |
| 321 | cmd.rlen = 4; |
| 322 | ret = si2168_cmd_execute(client, &cmd); |
| 323 | if (ret) |
| 324 | goto err; |
| 325 | |
| 326 | memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6); |
| 327 | cmd.wlen = 6; |
| 328 | cmd.rlen = 4; |
| 329 | ret = si2168_cmd_execute(client, &cmd); |
| 330 | if (ret) |
| 331 | goto err; |
| 332 | |
| 333 | memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6); |
| 334 | cmd.wlen = 6; |
| 335 | cmd.rlen = 4; |
| 336 | ret = si2168_cmd_execute(client, &cmd); |
| 337 | if (ret) |
| 338 | goto err; |
| 339 | |
| 340 | memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6); |
| 341 | cmd.args[4] = delivery_system | bandwidth; |
| 342 | cmd.wlen = 6; |
| 343 | cmd.rlen = 4; |
| 344 | ret = si2168_cmd_execute(client, &cmd); |
| 345 | if (ret) |
| 346 | goto err; |
| 347 | |
| 348 | /* set DVB-C symbol rate */ |
| 349 | if (c->delivery_system == SYS_DVBC_ANNEX_A) { |
| 350 | memcpy(cmd.args, "\x14\x00\x02\x11", 4); |
| 351 | cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff; |
| 352 | cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff; |
| 353 | cmd.wlen = 6; |
| 354 | cmd.rlen = 4; |
| 355 | ret = si2168_cmd_execute(client, &cmd); |
| 356 | if (ret) |
| 357 | goto err; |
| 358 | } |
| 359 | |
| 360 | memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6); |
| 361 | cmd.wlen = 6; |
| 362 | cmd.rlen = 4; |
| 363 | ret = si2168_cmd_execute(client, &cmd); |
| 364 | if (ret) |
| 365 | goto err; |
| 366 | |
| 367 | memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6); |
| 368 | cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10; |
| 369 | cmd.wlen = 6; |
| 370 | cmd.rlen = 4; |
| 371 | ret = si2168_cmd_execute(client, &cmd); |
| 372 | if (ret) |
| 373 | goto err; |
| 374 | |
| 375 | memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6); |
| 376 | cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10; |
| 377 | cmd.wlen = 6; |
| 378 | cmd.rlen = 4; |
| 379 | ret = si2168_cmd_execute(client, &cmd); |
| 380 | if (ret) |
| 381 | goto err; |
| 382 | |
| 383 | memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6); |
| 384 | cmd.wlen = 6; |
| 385 | cmd.rlen = 4; |
| 386 | ret = si2168_cmd_execute(client, &cmd); |
| 387 | if (ret) |
| 388 | goto err; |
| 389 | |
| 390 | memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6); |
| 391 | cmd.wlen = 6; |
| 392 | cmd.rlen = 4; |
| 393 | ret = si2168_cmd_execute(client, &cmd); |
| 394 | if (ret) |
| 395 | goto err; |
| 396 | |
| 397 | memcpy(cmd.args, "\x85", 1); |
| 398 | cmd.wlen = 1; |
| 399 | cmd.rlen = 1; |
| 400 | ret = si2168_cmd_execute(client, &cmd); |
| 401 | if (ret) |
| 402 | goto err; |
| 403 | |
| 404 | dev->delivery_system = c->delivery_system; |
| 405 | |
| 406 | return 0; |
| 407 | err: |
| 408 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static int si2168_init(struct dvb_frontend *fe) |
| 413 | { |
| 414 | struct i2c_client *client = fe->demodulator_priv; |
| 415 | struct si2168_dev *dev = i2c_get_clientdata(client); |
| 416 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 417 | int ret, len, remaining; |
| 418 | const struct firmware *fw; |
| 419 | struct si2168_cmd cmd; |
| 420 | |
| 421 | dev_dbg(&client->dev, "\n"); |
| 422 | |
| 423 | /* initialize */ |
| 424 | memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13); |
| 425 | cmd.wlen = 13; |
| 426 | cmd.rlen = 0; |
| 427 | ret = si2168_cmd_execute(client, &cmd); |
| 428 | if (ret) |
| 429 | goto err; |
| 430 | |
| 431 | if (dev->warm) { |
| 432 | /* resume */ |
| 433 | memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8); |
| 434 | cmd.wlen = 8; |
| 435 | cmd.rlen = 1; |
| 436 | ret = si2168_cmd_execute(client, &cmd); |
| 437 | if (ret) |
| 438 | goto err; |
| 439 | |
| 440 | udelay(100); |
| 441 | memcpy(cmd.args, "\x85", 1); |
| 442 | cmd.wlen = 1; |
| 443 | cmd.rlen = 1; |
| 444 | ret = si2168_cmd_execute(client, &cmd); |
| 445 | if (ret) |
| 446 | goto err; |
| 447 | |
| 448 | goto warm; |
| 449 | } |
| 450 | |
| 451 | /* power up */ |
| 452 | memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8); |
| 453 | cmd.wlen = 8; |
| 454 | cmd.rlen = 1; |
| 455 | ret = si2168_cmd_execute(client, &cmd); |
| 456 | if (ret) |
| 457 | goto err; |
| 458 | |
| 459 | /* request the firmware, this will block and timeout */ |
| 460 | ret = request_firmware(&fw, dev->firmware_name, &client->dev); |
| 461 | if (ret) { |
| 462 | /* fallback mechanism to handle old name for Si2168 B40 fw */ |
| 463 | if (dev->chip_id == SI2168_CHIP_ID_B40) { |
| 464 | dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK; |
| 465 | ret = request_firmware(&fw, dev->firmware_name, |
| 466 | &client->dev); |
| 467 | } |
| 468 | |
| 469 | if (ret == 0) { |
| 470 | dev_notice(&client->dev, |
| 471 | "please install firmware file '%s'\n", |
| 472 | SI2168_B40_FIRMWARE); |
| 473 | } else { |
| 474 | dev_err(&client->dev, |
| 475 | "firmware file '%s' not found\n", |
| 476 | dev->firmware_name); |
| 477 | goto err_release_firmware; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | dev_info(&client->dev, "downloading firmware from file '%s'\n", |
| 482 | dev->firmware_name); |
| 483 | |
| 484 | if ((fw->size % 17 == 0) && (fw->data[0] > 5)) { |
| 485 | /* firmware is in the new format */ |
| 486 | for (remaining = fw->size; remaining > 0; remaining -= 17) { |
| 487 | len = fw->data[fw->size - remaining]; |
| 488 | if (len > SI2168_ARGLEN) { |
| 489 | ret = -EINVAL; |
| 490 | break; |
| 491 | } |
| 492 | memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len); |
| 493 | cmd.wlen = len; |
| 494 | cmd.rlen = 1; |
| 495 | ret = si2168_cmd_execute(client, &cmd); |
| 496 | if (ret) |
| 497 | break; |
| 498 | } |
| 499 | } else if (fw->size % 8 == 0) { |
| 500 | /* firmware is in the old format */ |
| 501 | for (remaining = fw->size; remaining > 0; remaining -= 8) { |
| 502 | len = 8; |
| 503 | memcpy(cmd.args, &fw->data[fw->size - remaining], len); |
| 504 | cmd.wlen = len; |
| 505 | cmd.rlen = 1; |
| 506 | ret = si2168_cmd_execute(client, &cmd); |
| 507 | if (ret) |
| 508 | break; |
| 509 | } |
| 510 | } else { |
| 511 | /* bad or unknown firmware format */ |
| 512 | ret = -EINVAL; |
| 513 | } |
| 514 | |
| 515 | if (ret) { |
| 516 | dev_err(&client->dev, "firmware download failed %d\n", ret); |
| 517 | goto err_release_firmware; |
| 518 | } |
| 519 | |
| 520 | release_firmware(fw); |
| 521 | |
| 522 | memcpy(cmd.args, "\x01\x01", 2); |
| 523 | cmd.wlen = 2; |
| 524 | cmd.rlen = 1; |
| 525 | ret = si2168_cmd_execute(client, &cmd); |
| 526 | if (ret) |
| 527 | goto err; |
| 528 | |
| 529 | /* query firmware version */ |
| 530 | memcpy(cmd.args, "\x11", 1); |
| 531 | cmd.wlen = 1; |
| 532 | cmd.rlen = 10; |
| 533 | ret = si2168_cmd_execute(client, &cmd); |
| 534 | if (ret) |
| 535 | goto err; |
| 536 | |
| 537 | dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 | |
| 538 | (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0; |
| 539 | dev_info(&client->dev, "firmware version: %c %d.%d.%d\n", |
| 540 | dev->version >> 24 & 0xff, dev->version >> 16 & 0xff, |
| 541 | dev->version >> 8 & 0xff, dev->version >> 0 & 0xff); |
| 542 | |
| 543 | /* set ts mode */ |
| 544 | memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6); |
| 545 | cmd.args[4] |= dev->ts_mode; |
| 546 | if (dev->ts_clock_gapped) |
| 547 | cmd.args[4] |= 0x40; |
| 548 | cmd.wlen = 6; |
| 549 | cmd.rlen = 4; |
| 550 | ret = si2168_cmd_execute(client, &cmd); |
| 551 | if (ret) |
| 552 | goto err; |
| 553 | |
| 554 | dev->warm = true; |
| 555 | warm: |
| 556 | /* Init stats here to indicate which stats are supported */ |
| 557 | c->cnr.len = 1; |
| 558 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 559 | c->post_bit_error.len = 1; |
| 560 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 561 | c->post_bit_count.len = 1; |
| 562 | c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 563 | c->block_error.len = 1; |
| 564 | c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 565 | |
| 566 | dev->active = true; |
| 567 | |
| 568 | return 0; |
| 569 | err_release_firmware: |
| 570 | release_firmware(fw); |
| 571 | err: |
| 572 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | static int si2168_sleep(struct dvb_frontend *fe) |
| 577 | { |
| 578 | struct i2c_client *client = fe->demodulator_priv; |
| 579 | struct si2168_dev *dev = i2c_get_clientdata(client); |
| 580 | int ret; |
| 581 | struct si2168_cmd cmd; |
| 582 | |
| 583 | dev_dbg(&client->dev, "\n"); |
| 584 | |
| 585 | dev->active = false; |
| 586 | |
| 587 | /* Firmware B 4.0-11 or later loses warm state during sleep */ |
| 588 | if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0)) |
| 589 | dev->warm = false; |
| 590 | |
| 591 | memcpy(cmd.args, "\x13", 1); |
| 592 | cmd.wlen = 1; |
| 593 | cmd.rlen = 0; |
| 594 | ret = si2168_cmd_execute(client, &cmd); |
| 595 | if (ret) |
| 596 | goto err; |
| 597 | |
| 598 | return 0; |
| 599 | err: |
| 600 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static int si2168_get_tune_settings(struct dvb_frontend *fe, |
| 605 | struct dvb_frontend_tune_settings *s) |
| 606 | { |
| 607 | s->min_delay_ms = 900; |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int si2168_select(struct i2c_mux_core *muxc, u32 chan) |
| 613 | { |
| 614 | struct i2c_client *client = i2c_mux_priv(muxc); |
| 615 | int ret; |
| 616 | struct si2168_cmd cmd; |
| 617 | |
| 618 | /* open I2C gate */ |
| 619 | memcpy(cmd.args, "\xc0\x0d\x01", 3); |
| 620 | cmd.wlen = 3; |
| 621 | cmd.rlen = 0; |
| 622 | ret = si2168_cmd_execute(client, &cmd); |
| 623 | if (ret) |
| 624 | goto err; |
| 625 | |
| 626 | return 0; |
| 627 | err: |
| 628 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan) |
| 633 | { |
| 634 | struct i2c_client *client = i2c_mux_priv(muxc); |
| 635 | int ret; |
| 636 | struct si2168_cmd cmd; |
| 637 | |
| 638 | /* close I2C gate */ |
| 639 | memcpy(cmd.args, "\xc0\x0d\x00", 3); |
| 640 | cmd.wlen = 3; |
| 641 | cmd.rlen = 0; |
| 642 | ret = si2168_cmd_execute(client, &cmd); |
| 643 | if (ret) |
| 644 | goto err; |
| 645 | |
| 646 | return 0; |
| 647 | err: |
| 648 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | static const struct dvb_frontend_ops si2168_ops = { |
| 653 | .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A}, |
| 654 | .info = { |
| 655 | .name = "Silicon Labs Si2168", |
| 656 | .symbol_rate_min = 1000000, |
| 657 | .symbol_rate_max = 7200000, |
| 658 | .caps = FE_CAN_FEC_1_2 | |
| 659 | FE_CAN_FEC_2_3 | |
| 660 | FE_CAN_FEC_3_4 | |
| 661 | FE_CAN_FEC_5_6 | |
| 662 | FE_CAN_FEC_7_8 | |
| 663 | FE_CAN_FEC_AUTO | |
| 664 | FE_CAN_QPSK | |
| 665 | FE_CAN_QAM_16 | |
| 666 | FE_CAN_QAM_32 | |
| 667 | FE_CAN_QAM_64 | |
| 668 | FE_CAN_QAM_128 | |
| 669 | FE_CAN_QAM_256 | |
| 670 | FE_CAN_QAM_AUTO | |
| 671 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 672 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 673 | FE_CAN_HIERARCHY_AUTO | |
| 674 | FE_CAN_MUTE_TS | |
| 675 | FE_CAN_2G_MODULATION | |
| 676 | FE_CAN_MULTISTREAM |
| 677 | }, |
| 678 | |
| 679 | .get_tune_settings = si2168_get_tune_settings, |
| 680 | |
| 681 | .init = si2168_init, |
| 682 | .sleep = si2168_sleep, |
| 683 | |
| 684 | .set_frontend = si2168_set_frontend, |
| 685 | |
| 686 | .read_status = si2168_read_status, |
| 687 | }; |
| 688 | |
| 689 | static int si2168_probe(struct i2c_client *client, |
| 690 | const struct i2c_device_id *id) |
| 691 | { |
| 692 | struct si2168_config *config = client->dev.platform_data; |
| 693 | struct si2168_dev *dev; |
| 694 | int ret; |
| 695 | struct si2168_cmd cmd; |
| 696 | |
| 697 | dev_dbg(&client->dev, "\n"); |
| 698 | |
| 699 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 700 | if (!dev) { |
| 701 | ret = -ENOMEM; |
| 702 | dev_err(&client->dev, "kzalloc() failed\n"); |
| 703 | goto err; |
| 704 | } |
| 705 | |
| 706 | i2c_set_clientdata(client, dev); |
| 707 | mutex_init(&dev->i2c_mutex); |
| 708 | |
| 709 | /* Initialize */ |
| 710 | memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13); |
| 711 | cmd.wlen = 13; |
| 712 | cmd.rlen = 0; |
| 713 | ret = si2168_cmd_execute(client, &cmd); |
| 714 | if (ret) |
| 715 | goto err_kfree; |
| 716 | |
| 717 | /* Power up */ |
| 718 | memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8); |
| 719 | cmd.wlen = 8; |
| 720 | cmd.rlen = 1; |
| 721 | ret = si2168_cmd_execute(client, &cmd); |
| 722 | if (ret) |
| 723 | goto err_kfree; |
| 724 | |
| 725 | /* Query chip revision */ |
| 726 | memcpy(cmd.args, "\x02", 1); |
| 727 | cmd.wlen = 1; |
| 728 | cmd.rlen = 13; |
| 729 | ret = si2168_cmd_execute(client, &cmd); |
| 730 | if (ret) |
| 731 | goto err_kfree; |
| 732 | |
| 733 | dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | |
| 734 | cmd.args[3] << 8 | cmd.args[4] << 0; |
| 735 | |
| 736 | switch (dev->chip_id) { |
| 737 | case SI2168_CHIP_ID_A20: |
| 738 | dev->firmware_name = SI2168_A20_FIRMWARE; |
| 739 | break; |
| 740 | case SI2168_CHIP_ID_A30: |
| 741 | dev->firmware_name = SI2168_A30_FIRMWARE; |
| 742 | break; |
| 743 | case SI2168_CHIP_ID_B40: |
| 744 | dev->firmware_name = SI2168_B40_FIRMWARE; |
| 745 | break; |
| 746 | case SI2168_CHIP_ID_D60: |
| 747 | dev->firmware_name = SI2168_D60_FIRMWARE; |
| 748 | break; |
| 749 | default: |
| 750 | dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n", |
| 751 | cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]); |
| 752 | ret = -ENODEV; |
| 753 | goto err_kfree; |
| 754 | } |
| 755 | |
| 756 | dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 | |
| 757 | (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0; |
| 758 | |
| 759 | /* create mux i2c adapter for tuner */ |
| 760 | dev->muxc = i2c_mux_alloc(client->adapter, &client->dev, |
| 761 | 1, 0, I2C_MUX_LOCKED, |
| 762 | si2168_select, si2168_deselect); |
| 763 | if (!dev->muxc) { |
| 764 | ret = -ENOMEM; |
| 765 | goto err_kfree; |
| 766 | } |
| 767 | dev->muxc->priv = client; |
| 768 | ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0); |
| 769 | if (ret) |
| 770 | goto err_kfree; |
| 771 | |
| 772 | /* create dvb_frontend */ |
| 773 | memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops)); |
| 774 | dev->fe.demodulator_priv = client; |
| 775 | *config->i2c_adapter = dev->muxc->adapter[0]; |
| 776 | *config->fe = &dev->fe; |
| 777 | dev->ts_mode = config->ts_mode; |
| 778 | dev->ts_clock_inv = config->ts_clock_inv; |
| 779 | dev->ts_clock_gapped = config->ts_clock_gapped; |
| 780 | |
| 781 | dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n", |
| 782 | dev->version >> 24 & 0xff, dev->version >> 16 & 0xff, |
| 783 | dev->version >> 8 & 0xff); |
| 784 | dev_info(&client->dev, "firmware version: %c %d.%d.%d\n", |
| 785 | dev->version >> 24 & 0xff, dev->version >> 16 & 0xff, |
| 786 | dev->version >> 8 & 0xff, dev->version >> 0 & 0xff); |
| 787 | |
| 788 | return 0; |
| 789 | err_kfree: |
| 790 | kfree(dev); |
| 791 | err: |
| 792 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | static int si2168_remove(struct i2c_client *client) |
| 797 | { |
| 798 | struct si2168_dev *dev = i2c_get_clientdata(client); |
| 799 | |
| 800 | dev_dbg(&client->dev, "\n"); |
| 801 | |
| 802 | i2c_mux_del_adapters(dev->muxc); |
| 803 | |
| 804 | dev->fe.ops.release = NULL; |
| 805 | dev->fe.demodulator_priv = NULL; |
| 806 | |
| 807 | kfree(dev); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | static const struct i2c_device_id si2168_id_table[] = { |
| 813 | {"si2168", 0}, |
| 814 | {} |
| 815 | }; |
| 816 | MODULE_DEVICE_TABLE(i2c, si2168_id_table); |
| 817 | |
| 818 | static struct i2c_driver si2168_driver = { |
| 819 | .driver = { |
| 820 | .name = "si2168", |
| 821 | .suppress_bind_attrs = true, |
| 822 | }, |
| 823 | .probe = si2168_probe, |
| 824 | .remove = si2168_remove, |
| 825 | .id_table = si2168_id_table, |
| 826 | }; |
| 827 | |
| 828 | module_i2c_driver(si2168_driver); |
| 829 | |
| 830 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 831 | MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver"); |
| 832 | MODULE_LICENSE("GPL"); |
| 833 | MODULE_FIRMWARE(SI2168_A20_FIRMWARE); |
| 834 | MODULE_FIRMWARE(SI2168_A30_FIRMWARE); |
| 835 | MODULE_FIRMWARE(SI2168_B40_FIRMWARE); |
| 836 | MODULE_FIRMWARE(SI2168_D60_FIRMWARE); |