1/* 2 * Panasonic MN88472 DVB-T/T2/C demodulator driver 3 * 4 * Copyright (C) 2013 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 "mn88472_priv.h" 18 19static int mn88472_get_tune_settings(struct dvb_frontend *fe, 20 struct dvb_frontend_tune_settings *s) 21{ 22 s->min_delay_ms = 800; 23 return 0; 24} 25 26static int mn88472_set_frontend(struct dvb_frontend *fe) 27{ 28 struct i2c_client *client = fe->demodulator_priv; 29 struct mn88472_dev *dev = i2c_get_clientdata(client); 30 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 31 int ret, i; 32 u32 if_frequency = 0; 33 u64 tmp; 34 u8 delivery_system_val, if_val[3], bw_val[7], bw_val2; 35 36 dev_dbg(&client->dev, 37 "delivery_system=%d modulation=%d frequency=%d symbol_rate=%d inversion=%d\n", 38 c->delivery_system, c->modulation, 39 c->frequency, c->symbol_rate, c->inversion); 40 41 if (!dev->warm) { 42 ret = -EAGAIN; 43 goto err; 44 } 45 46 switch (c->delivery_system) { 47 case SYS_DVBT: 48 delivery_system_val = 0x02; 49 break; 50 case SYS_DVBT2: 51 delivery_system_val = 0x03; 52 break; 53 case SYS_DVBC_ANNEX_A: 54 delivery_system_val = 0x04; 55 break; 56 default: 57 ret = -EINVAL; 58 goto err; 59 } 60 61 if (c->bandwidth_hz <= 5000000) { 62 memcpy(bw_val, "\xe5\x99\x9a\x1b\xa9\x1b\xa9", 7); 63 bw_val2 = 0x03; 64 } else if (c->bandwidth_hz <= 6000000) { 65 /* IF 3570000 Hz, BW 6000000 Hz */ 66 memcpy(bw_val, "\xbf\x55\x55\x15\x6b\x15\x6b", 7); 67 bw_val2 = 0x02; 68 } else if (c->bandwidth_hz <= 7000000) { 69 /* IF 4570000 Hz, BW 7000000 Hz */ 70 memcpy(bw_val, "\xa4\x00\x00\x0f\x2c\x0f\x2c", 7); 71 bw_val2 = 0x01; 72 } else if (c->bandwidth_hz <= 8000000) { 73 /* IF 4570000 Hz, BW 8000000 Hz */ 74 memcpy(bw_val, "\x8f\x80\x00\x08\xee\x08\xee", 7); 75 bw_val2 = 0x00; 76 } else { 77 ret = -EINVAL; 78 goto err; 79 } 80 81 /* program tuner */ 82 if (fe->ops.tuner_ops.set_params) { 83 ret = fe->ops.tuner_ops.set_params(fe); 84 if (ret) 85 goto err; 86 } 87 88 if (fe->ops.tuner_ops.get_if_frequency) { 89 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); 90 if (ret) 91 goto err; 92 93 dev_dbg(&client->dev, "get_if_frequency=%d\n", if_frequency); 94 } 95 96 /* Calculate IF registers ( (1<<24)*IF / Xtal ) */ 97 tmp = div_u64(if_frequency * (u64)(1<<24) + (dev->xtal / 2), 98 dev->xtal); 99 if_val[0] = ((tmp >> 16) & 0xff); 100 if_val[1] = ((tmp >> 8) & 0xff); 101 if_val[2] = ((tmp >> 0) & 0xff); 102 103 ret = regmap_write(dev->regmap[2], 0xfb, 0x13); 104 ret = regmap_write(dev->regmap[2], 0xef, 0x13); 105 ret = regmap_write(dev->regmap[2], 0xf9, 0x13); 106 if (ret) 107 goto err; 108 109 ret = regmap_write(dev->regmap[2], 0x00, 0x66); 110 if (ret) 111 goto err; 112 ret = regmap_write(dev->regmap[2], 0x01, 0x00); 113 if (ret) 114 goto err; 115 ret = regmap_write(dev->regmap[2], 0x02, 0x01); 116 if (ret) 117 goto err; 118 ret = regmap_write(dev->regmap[2], 0x03, delivery_system_val); 119 if (ret) 120 goto err; 121 ret = regmap_write(dev->regmap[2], 0x04, bw_val2); 122 if (ret) 123 goto err; 124 125 for (i = 0; i < sizeof(if_val); i++) { 126 ret = regmap_write(dev->regmap[2], 0x10 + i, if_val[i]); 127 if (ret) 128 goto err; 129 } 130 131 for (i = 0; i < sizeof(bw_val); i++) { 132 ret = regmap_write(dev->regmap[2], 0x13 + i, bw_val[i]); 133 if (ret) 134 goto err; 135 } 136 137 switch (c->delivery_system) { 138 case SYS_DVBT: 139 ret = regmap_write(dev->regmap[0], 0x07, 0x26); 140 ret = regmap_write(dev->regmap[0], 0xb0, 0x0a); 141 ret = regmap_write(dev->regmap[0], 0xb4, 0x00); 142 ret = regmap_write(dev->regmap[0], 0xcd, 0x1f); 143 ret = regmap_write(dev->regmap[0], 0xd4, 0x0a); 144 ret = regmap_write(dev->regmap[0], 0xd6, 0x48); 145 ret = regmap_write(dev->regmap[0], 0x00, 0xba); 146 ret = regmap_write(dev->regmap[0], 0x01, 0x13); 147 if (ret) 148 goto err; 149 break; 150 case SYS_DVBT2: 151 ret = regmap_write(dev->regmap[2], 0x2b, 0x13); 152 ret = regmap_write(dev->regmap[2], 0x4f, 0x05); 153 ret = regmap_write(dev->regmap[1], 0xf6, 0x05); 154 ret = regmap_write(dev->regmap[0], 0xb0, 0x0a); 155 ret = regmap_write(dev->regmap[0], 0xb4, 0xf6); 156 ret = regmap_write(dev->regmap[0], 0xcd, 0x01); 157 ret = regmap_write(dev->regmap[0], 0xd4, 0x09); 158 ret = regmap_write(dev->regmap[0], 0xd6, 0x46); 159 ret = regmap_write(dev->regmap[2], 0x30, 0x80); 160 ret = regmap_write(dev->regmap[2], 0x32, 0x00); 161 if (ret) 162 goto err; 163 break; 164 case SYS_DVBC_ANNEX_A: 165 ret = regmap_write(dev->regmap[0], 0xb0, 0x0b); 166 ret = regmap_write(dev->regmap[0], 0xb4, 0x00); 167 ret = regmap_write(dev->regmap[0], 0xcd, 0x17); 168 ret = regmap_write(dev->regmap[0], 0xd4, 0x09); 169 ret = regmap_write(dev->regmap[0], 0xd6, 0x48); 170 ret = regmap_write(dev->regmap[1], 0x00, 0xb0); 171 if (ret) 172 goto err; 173 break; 174 default: 175 ret = -EINVAL; 176 goto err; 177 } 178 179 ret = regmap_write(dev->regmap[0], 0x46, 0x00); 180 ret = regmap_write(dev->regmap[0], 0xae, 0x00); 181 182 switch (dev->ts_mode) { 183 case SERIAL_TS_MODE: 184 ret = regmap_write(dev->regmap[2], 0x08, 0x1d); 185 break; 186 case PARALLEL_TS_MODE: 187 ret = regmap_write(dev->regmap[2], 0x08, 0x00); 188 break; 189 default: 190 dev_dbg(&client->dev, "ts_mode error: %d\n", dev->ts_mode); 191 ret = -EINVAL; 192 goto err; 193 } 194 195 switch (dev->ts_clock) { 196 case VARIABLE_TS_CLOCK: 197 ret = regmap_write(dev->regmap[0], 0xd9, 0xe3); 198 break; 199 case FIXED_TS_CLOCK: 200 ret = regmap_write(dev->regmap[0], 0xd9, 0xe1); 201 break; 202 default: 203 dev_dbg(&client->dev, "ts_clock error: %d\n", dev->ts_clock); 204 ret = -EINVAL; 205 goto err; 206 } 207 208 /* Reset demod */ 209 ret = regmap_write(dev->regmap[2], 0xf8, 0x9f); 210 if (ret) 211 goto err; 212 213 dev->delivery_system = c->delivery_system; 214 215 return 0; 216err: 217 dev_dbg(&client->dev, "failed=%d\n", ret); 218 return ret; 219} 220 221static int mn88472_read_status(struct dvb_frontend *fe, fe_status_t *status) 222{ 223 struct i2c_client *client = fe->demodulator_priv; 224 struct mn88472_dev *dev = i2c_get_clientdata(client); 225 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 226 int ret; 227 unsigned int utmp; 228 int lock = 0; 229 230 *status = 0; 231 232 if (!dev->warm) { 233 ret = -EAGAIN; 234 goto err; 235 } 236 237 switch (c->delivery_system) { 238 case SYS_DVBT: 239 ret = regmap_read(dev->regmap[0], 0x7F, &utmp); 240 if (ret) 241 goto err; 242 if ((utmp & 0xF) >= 0x09) 243 lock = 1; 244 break; 245 case SYS_DVBT2: 246 ret = regmap_read(dev->regmap[2], 0x92, &utmp); 247 if (ret) 248 goto err; 249 if ((utmp & 0xF) >= 0x07) 250 *status |= FE_HAS_SIGNAL; 251 if ((utmp & 0xF) >= 0x0a) 252 *status |= FE_HAS_CARRIER; 253 if ((utmp & 0xF) >= 0x0d) 254 *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; 255 break; 256 case SYS_DVBC_ANNEX_A: 257 ret = regmap_read(dev->regmap[1], 0x84, &utmp); 258 if (ret) 259 goto err; 260 if ((utmp & 0xF) >= 0x08) 261 lock = 1; 262 break; 263 default: 264 ret = -EINVAL; 265 goto err; 266 } 267 268 if (lock) 269 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | 270 FE_HAS_SYNC | FE_HAS_LOCK; 271 272 return 0; 273err: 274 dev_dbg(&client->dev, "failed=%d\n", ret); 275 return ret; 276} 277 278static int mn88472_init(struct dvb_frontend *fe) 279{ 280 struct i2c_client *client = fe->demodulator_priv; 281 struct mn88472_dev *dev = i2c_get_clientdata(client); 282 int ret, len, remaining; 283 const struct firmware *fw = NULL; 284 u8 *fw_file = MN88472_FIRMWARE; 285 unsigned int tmp; 286 287 dev_dbg(&client->dev, "\n"); 288 289 /* set cold state by default */ 290 dev->warm = false; 291 292 /* power on */ 293 ret = regmap_write(dev->regmap[2], 0x05, 0x00); 294 if (ret) 295 goto err; 296 297 ret = regmap_bulk_write(dev->regmap[2], 0x0b, "\x00\x00", 2); 298 if (ret) 299 goto err; 300 301 /* check if firmware is already running */ 302 ret = regmap_read(dev->regmap[0], 0xf5, &tmp); 303 if (ret) 304 goto err; 305 306 if (!(tmp & 0x1)) { 307 dev_info(&client->dev, "firmware already running\n"); 308 dev->warm = true; 309 return 0; 310 } 311 312 /* request the firmware, this will block and timeout */ 313 ret = request_firmware(&fw, fw_file, &client->dev); 314 if (ret) { 315 dev_err(&client->dev, "firmare file '%s' not found\n", 316 fw_file); 317 goto err; 318 } 319 320 dev_info(&client->dev, "downloading firmware from file '%s'\n", 321 fw_file); 322 323 ret = regmap_write(dev->regmap[0], 0xf5, 0x03); 324 if (ret) 325 goto firmware_release; 326 327 for (remaining = fw->size; remaining > 0; 328 remaining -= (dev->i2c_wr_max - 1)) { 329 len = remaining; 330 if (len > (dev->i2c_wr_max - 1)) 331 len = dev->i2c_wr_max - 1; 332 333 ret = regmap_bulk_write(dev->regmap[0], 0xf6, 334 &fw->data[fw->size - remaining], len); 335 if (ret) { 336 dev_err(&client->dev, 337 "firmware download failed=%d\n", ret); 338 goto firmware_release; 339 } 340 } 341 342 /* parity check of firmware */ 343 ret = regmap_read(dev->regmap[0], 0xf8, &tmp); 344 if (ret) { 345 dev_err(&client->dev, 346 "parity reg read failed=%d\n", ret); 347 goto err; 348 } 349 if (tmp & 0x10) { 350 dev_err(&client->dev, 351 "firmware parity check failed=0x%x\n", tmp); 352 goto err; 353 } 354 dev_err(&client->dev, "firmware parity check succeeded=0x%x\n", tmp); 355 356 ret = regmap_write(dev->regmap[0], 0xf5, 0x00); 357 if (ret) 358 goto firmware_release; 359 360 release_firmware(fw); 361 fw = NULL; 362 363 /* warm state */ 364 dev->warm = true; 365 366 return 0; 367firmware_release: 368 release_firmware(fw); 369err: 370 dev_dbg(&client->dev, "failed=%d\n", ret); 371 return ret; 372} 373 374static int mn88472_sleep(struct dvb_frontend *fe) 375{ 376 struct i2c_client *client = fe->demodulator_priv; 377 struct mn88472_dev *dev = i2c_get_clientdata(client); 378 int ret; 379 380 dev_dbg(&client->dev, "\n"); 381 382 /* power off */ 383 ret = regmap_write(dev->regmap[2], 0x0b, 0x30); 384 385 if (ret) 386 goto err; 387 388 ret = regmap_write(dev->regmap[2], 0x05, 0x3e); 389 if (ret) 390 goto err; 391 392 dev->delivery_system = SYS_UNDEFINED; 393 394 return 0; 395err: 396 dev_dbg(&client->dev, "failed=%d\n", ret); 397 return ret; 398} 399 400static struct dvb_frontend_ops mn88472_ops = { 401 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A}, 402 .info = { 403 .name = "Panasonic MN88472", 404 .symbol_rate_min = 1000000, 405 .symbol_rate_max = 7200000, 406 .caps = FE_CAN_FEC_1_2 | 407 FE_CAN_FEC_2_3 | 408 FE_CAN_FEC_3_4 | 409 FE_CAN_FEC_5_6 | 410 FE_CAN_FEC_7_8 | 411 FE_CAN_FEC_AUTO | 412 FE_CAN_QPSK | 413 FE_CAN_QAM_16 | 414 FE_CAN_QAM_32 | 415 FE_CAN_QAM_64 | 416 FE_CAN_QAM_128 | 417 FE_CAN_QAM_256 | 418 FE_CAN_QAM_AUTO | 419 FE_CAN_TRANSMISSION_MODE_AUTO | 420 FE_CAN_GUARD_INTERVAL_AUTO | 421 FE_CAN_HIERARCHY_AUTO | 422 FE_CAN_MUTE_TS | 423 FE_CAN_2G_MODULATION | 424 FE_CAN_MULTISTREAM 425 }, 426 427 .get_tune_settings = mn88472_get_tune_settings, 428 429 .init = mn88472_init, 430 .sleep = mn88472_sleep, 431 432 .set_frontend = mn88472_set_frontend, 433 434 .read_status = mn88472_read_status, 435}; 436 437static int mn88472_probe(struct i2c_client *client, 438 const struct i2c_device_id *id) 439{ 440 struct mn88472_config *config = client->dev.platform_data; 441 struct mn88472_dev *dev; 442 int ret; 443 unsigned int utmp; 444 static const struct regmap_config regmap_config = { 445 .reg_bits = 8, 446 .val_bits = 8, 447 }; 448 449 dev_dbg(&client->dev, "\n"); 450 451 /* Caller really need to provide pointer for frontend we create. */ 452 if (config->fe == NULL) { 453 dev_err(&client->dev, "frontend pointer not defined\n"); 454 ret = -EINVAL; 455 goto err; 456 } 457 458 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 459 if (dev == NULL) { 460 ret = -ENOMEM; 461 goto err; 462 } 463 464 dev->i2c_wr_max = config->i2c_wr_max; 465 dev->xtal = config->xtal; 466 dev->ts_mode = config->ts_mode; 467 dev->ts_clock = config->ts_clock; 468 dev->client[0] = client; 469 dev->regmap[0] = regmap_init_i2c(dev->client[0], ®map_config); 470 if (IS_ERR(dev->regmap[0])) { 471 ret = PTR_ERR(dev->regmap[0]); 472 goto err_kfree; 473 } 474 475 /* check demod answers to I2C */ 476 ret = regmap_read(dev->regmap[0], 0x00, &utmp); 477 if (ret) 478 goto err_regmap_0_regmap_exit; 479 480 /* 481 * Chip has three I2C addresses for different register pages. Used 482 * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients, 483 * 0x1a and 0x1c, in order to get own I2C client for each register page. 484 */ 485 dev->client[1] = i2c_new_dummy(client->adapter, 0x1a); 486 if (dev->client[1] == NULL) { 487 ret = -ENODEV; 488 dev_err(&client->dev, "I2C registration failed\n"); 489 if (ret) 490 goto err_regmap_0_regmap_exit; 491 } 492 dev->regmap[1] = regmap_init_i2c(dev->client[1], ®map_config); 493 if (IS_ERR(dev->regmap[1])) { 494 ret = PTR_ERR(dev->regmap[1]); 495 goto err_client_1_i2c_unregister_device; 496 } 497 i2c_set_clientdata(dev->client[1], dev); 498 499 dev->client[2] = i2c_new_dummy(client->adapter, 0x1c); 500 if (dev->client[2] == NULL) { 501 ret = -ENODEV; 502 dev_err(&client->dev, "2nd I2C registration failed\n"); 503 if (ret) 504 goto err_regmap_1_regmap_exit; 505 } 506 dev->regmap[2] = regmap_init_i2c(dev->client[2], ®map_config); 507 if (IS_ERR(dev->regmap[2])) { 508 ret = PTR_ERR(dev->regmap[2]); 509 goto err_client_2_i2c_unregister_device; 510 } 511 i2c_set_clientdata(dev->client[2], dev); 512 513 /* create dvb_frontend */ 514 memcpy(&dev->fe.ops, &mn88472_ops, sizeof(struct dvb_frontend_ops)); 515 dev->fe.demodulator_priv = client; 516 *config->fe = &dev->fe; 517 i2c_set_clientdata(client, dev); 518 519 dev_info(&client->dev, "Panasonic MN88472 successfully attached\n"); 520 return 0; 521 522err_client_2_i2c_unregister_device: 523 i2c_unregister_device(dev->client[2]); 524err_regmap_1_regmap_exit: 525 regmap_exit(dev->regmap[1]); 526err_client_1_i2c_unregister_device: 527 i2c_unregister_device(dev->client[1]); 528err_regmap_0_regmap_exit: 529 regmap_exit(dev->regmap[0]); 530err_kfree: 531 kfree(dev); 532err: 533 dev_dbg(&client->dev, "failed=%d\n", ret); 534 return ret; 535} 536 537static int mn88472_remove(struct i2c_client *client) 538{ 539 struct mn88472_dev *dev = i2c_get_clientdata(client); 540 541 dev_dbg(&client->dev, "\n"); 542 543 regmap_exit(dev->regmap[2]); 544 i2c_unregister_device(dev->client[2]); 545 546 regmap_exit(dev->regmap[1]); 547 i2c_unregister_device(dev->client[1]); 548 549 regmap_exit(dev->regmap[0]); 550 551 kfree(dev); 552 553 return 0; 554} 555 556static const struct i2c_device_id mn88472_id_table[] = { 557 {"mn88472", 0}, 558 {} 559}; 560MODULE_DEVICE_TABLE(i2c, mn88472_id_table); 561 562static struct i2c_driver mn88472_driver = { 563 .driver = { 564 .owner = THIS_MODULE, 565 .name = "mn88472", 566 }, 567 .probe = mn88472_probe, 568 .remove = mn88472_remove, 569 .id_table = mn88472_id_table, 570}; 571 572module_i2c_driver(mn88472_driver); 573 574MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 575MODULE_DESCRIPTION("Panasonic MN88472 DVB-T/T2/C demodulator driver"); 576MODULE_LICENSE("GPL"); 577MODULE_FIRMWARE(MN88472_FIRMWARE); 578