1 /* 2 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 */ 15 16 #include <linux/iio/iio.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/mfd/core.h> 20 #include <linux/module.h> 21 #include <linux/of.h> 22 #include <linux/of_gpio.h> 23 #include <linux/of_platform.h> 24 #include "ssp.h" 25 26 #define SSP_WDT_TIME 10000 27 #define SSP_LIMIT_RESET_CNT 20 28 #define SSP_LIMIT_TIMEOUT_CNT 3 29 30 /* It is possible that it is max clk rate for version 1.0 of bootcode */ 31 #define SSP_BOOT_SPI_HZ 400000 32 33 /* 34 * These fields can look enigmatic but this structure is used mainly to flat 35 * some values and depends on command type. 36 */ 37 struct ssp_instruction { 38 __le32 a; 39 __le32 b; 40 u8 c; 41 } __attribute__((__packed__)); 42 43 static const u8 ssp_magnitude_table[] = {110, 85, 171, 71, 203, 195, 0, 67, 44 208, 56, 175, 244, 206, 213, 0, 92, 250, 0, 55, 48, 189, 252, 171, 45 243, 13, 45, 250}; 46 47 static const struct ssp_sensorhub_info ssp_rinato_info = { 48 .fw_name = "ssp_B2.fw", 49 .fw_crashed_name = "ssp_crashed.fw", 50 .fw_rev = 14052300, 51 .mag_table = ssp_magnitude_table, 52 .mag_length = ARRAY_SIZE(ssp_magnitude_table), 53 }; 54 55 static const struct ssp_sensorhub_info ssp_thermostat_info = { 56 .fw_name = "thermostat_B2.fw", 57 .fw_crashed_name = "ssp_crashed.fw", 58 .fw_rev = 14080600, 59 .mag_table = ssp_magnitude_table, 60 .mag_length = ARRAY_SIZE(ssp_magnitude_table), 61 }; 62 63 static const struct mfd_cell sensorhub_sensor_devs[] = { 64 { 65 .name = "ssp-accelerometer", 66 }, 67 { 68 .name = "ssp-gyroscope", 69 }, 70 }; 71 72 static void ssp_toggle_mcu_reset_gpio(struct ssp_data *data) 73 { 74 gpio_set_value(data->mcu_reset_gpio, 0); 75 usleep_range(1000, 1200); 76 gpio_set_value(data->mcu_reset_gpio, 1); 77 msleep(50); 78 } 79 80 static void ssp_sync_available_sensors(struct ssp_data *data) 81 { 82 int i, ret; 83 84 for (i = 0; i < SSP_SENSOR_MAX; ++i) { 85 if (data->available_sensors & BIT(i)) { 86 ret = ssp_enable_sensor(data, i, data->delay_buf[i]); 87 if (ret < 0) { 88 dev_err(&data->spi->dev, 89 "Sync sensor nr: %d fail\n", i); 90 continue; 91 } 92 } 93 } 94 95 ret = ssp_command(data, SSP_MSG2SSP_AP_MCU_SET_DUMPMODE, 96 data->mcu_dump_mode); 97 if (ret < 0) 98 dev_err(&data->spi->dev, 99 "SSP_MSG2SSP_AP_MCU_SET_DUMPMODE failed\n"); 100 } 101 102 static void ssp_enable_mcu(struct ssp_data *data, bool enable) 103 { 104 dev_info(&data->spi->dev, "current shutdown = %d, old = %d\n", enable, 105 data->shut_down); 106 107 if (enable && data->shut_down) { 108 data->shut_down = false; 109 enable_irq(data->spi->irq); 110 enable_irq_wake(data->spi->irq); 111 } else if (!enable && !data->shut_down) { 112 data->shut_down = true; 113 disable_irq(data->spi->irq); 114 disable_irq_wake(data->spi->irq); 115 } else { 116 dev_warn(&data->spi->dev, "current shutdown = %d, old = %d\n", 117 enable, data->shut_down); 118 } 119 } 120 121 /* 122 * This function is the first one which communicates with the mcu so it is 123 * possible that the first attempt will fail 124 */ 125 static int ssp_check_fwbl(struct ssp_data *data) 126 { 127 int retries = 0; 128 129 while (retries++ < 5) { 130 data->cur_firm_rev = ssp_get_firmware_rev(data); 131 if (data->cur_firm_rev == SSP_INVALID_REVISION || 132 data->cur_firm_rev == SSP_INVALID_REVISION2) { 133 dev_warn(&data->spi->dev, 134 "Invalid revision, trying %d time\n", retries); 135 } else { 136 break; 137 } 138 } 139 140 if (data->cur_firm_rev == SSP_INVALID_REVISION || 141 data->cur_firm_rev == SSP_INVALID_REVISION2) { 142 dev_err(&data->spi->dev, "SSP_INVALID_REVISION\n"); 143 return SSP_FW_DL_STATE_NEED_TO_SCHEDULE; 144 } 145 146 dev_info(&data->spi->dev, 147 "MCU Firm Rev : Old = %8u, New = %8u\n", 148 data->cur_firm_rev, 149 data->sensorhub_info->fw_rev); 150 151 if (data->cur_firm_rev != data->sensorhub_info->fw_rev) 152 return SSP_FW_DL_STATE_NEED_TO_SCHEDULE; 153 154 return SSP_FW_DL_STATE_NONE; 155 } 156 157 static void ssp_reset_mcu(struct ssp_data *data) 158 { 159 ssp_enable_mcu(data, false); 160 ssp_clean_pending_list(data); 161 ssp_toggle_mcu_reset_gpio(data); 162 ssp_enable_mcu(data, true); 163 } 164 165 static void ssp_wdt_work_func(struct work_struct *work) 166 { 167 struct ssp_data *data = container_of(work, struct ssp_data, work_wdt); 168 169 dev_err(&data->spi->dev, "%s - Sensor state: 0x%x, RC: %u, CC: %u\n", 170 __func__, data->available_sensors, data->reset_cnt, 171 data->com_fail_cnt); 172 173 ssp_reset_mcu(data); 174 data->com_fail_cnt = 0; 175 data->timeout_cnt = 0; 176 } 177 178 static void ssp_wdt_timer_func(struct timer_list *t) 179 { 180 struct ssp_data *data = from_timer(data, t, wdt_timer); 181 182 switch (data->fw_dl_state) { 183 case SSP_FW_DL_STATE_FAIL: 184 case SSP_FW_DL_STATE_DOWNLOADING: 185 case SSP_FW_DL_STATE_SYNC: 186 goto _mod; 187 } 188 189 if (data->timeout_cnt > SSP_LIMIT_TIMEOUT_CNT || 190 data->com_fail_cnt > SSP_LIMIT_RESET_CNT) 191 queue_work(system_power_efficient_wq, &data->work_wdt); 192 _mod: 193 mod_timer(&data->wdt_timer, jiffies + msecs_to_jiffies(SSP_WDT_TIME)); 194 } 195 196 static void ssp_enable_wdt_timer(struct ssp_data *data) 197 { 198 mod_timer(&data->wdt_timer, jiffies + msecs_to_jiffies(SSP_WDT_TIME)); 199 } 200 201 static void ssp_disable_wdt_timer(struct ssp_data *data) 202 { 203 del_timer_sync(&data->wdt_timer); 204 cancel_work_sync(&data->work_wdt); 205 } 206 207 /** 208 * ssp_get_sensor_delay() - gets sensor data acquisition period 209 * @data: sensorhub structure 210 * @type: SSP sensor type 211 * 212 * Returns acquisition period in ms 213 */ 214 u32 ssp_get_sensor_delay(struct ssp_data *data, enum ssp_sensor_type type) 215 { 216 return data->delay_buf[type]; 217 } 218 EXPORT_SYMBOL(ssp_get_sensor_delay); 219 220 /** 221 * ssp_enable_sensor() - enables data acquisition for sensor 222 * @data: sensorhub structure 223 * @type: SSP sensor type 224 * @delay: delay in ms 225 * 226 * Returns 0 or negative value in case of error 227 */ 228 int ssp_enable_sensor(struct ssp_data *data, enum ssp_sensor_type type, 229 u32 delay) 230 { 231 int ret; 232 struct ssp_instruction to_send; 233 234 to_send.a = cpu_to_le32(delay); 235 to_send.b = cpu_to_le32(data->batch_latency_buf[type]); 236 to_send.c = data->batch_opt_buf[type]; 237 238 switch (data->check_status[type]) { 239 case SSP_INITIALIZATION_STATE: 240 /* do calibration step, now just enable */ 241 case SSP_ADD_SENSOR_STATE: 242 ret = ssp_send_instruction(data, 243 SSP_MSG2SSP_INST_BYPASS_SENSOR_ADD, 244 type, 245 (u8 *)&to_send, sizeof(to_send)); 246 if (ret < 0) { 247 dev_err(&data->spi->dev, "Enabling sensor failed\n"); 248 data->check_status[type] = SSP_NO_SENSOR_STATE; 249 goto derror; 250 } 251 252 data->sensor_enable |= BIT(type); 253 data->check_status[type] = SSP_RUNNING_SENSOR_STATE; 254 break; 255 case SSP_RUNNING_SENSOR_STATE: 256 ret = ssp_send_instruction(data, 257 SSP_MSG2SSP_INST_CHANGE_DELAY, type, 258 (u8 *)&to_send, sizeof(to_send)); 259 if (ret < 0) { 260 dev_err(&data->spi->dev, 261 "Changing sensor delay failed\n"); 262 goto derror; 263 } 264 break; 265 default: 266 data->check_status[type] = SSP_ADD_SENSOR_STATE; 267 break; 268 } 269 270 data->delay_buf[type] = delay; 271 272 if (atomic_inc_return(&data->enable_refcount) == 1) 273 ssp_enable_wdt_timer(data); 274 275 return 0; 276 277 derror: 278 return ret; 279 } 280 EXPORT_SYMBOL(ssp_enable_sensor); 281 282 /** 283 * ssp_change_delay() - changes data acquisition for sensor 284 * @data: sensorhub structure 285 * @type: SSP sensor type 286 * @delay: delay in ms 287 * 288 * Returns 0 or negative value in case of error 289 */ 290 int ssp_change_delay(struct ssp_data *data, enum ssp_sensor_type type, 291 u32 delay) 292 { 293 int ret; 294 struct ssp_instruction to_send; 295 296 to_send.a = cpu_to_le32(delay); 297 to_send.b = cpu_to_le32(data->batch_latency_buf[type]); 298 to_send.c = data->batch_opt_buf[type]; 299 300 ret = ssp_send_instruction(data, SSP_MSG2SSP_INST_CHANGE_DELAY, type, 301 (u8 *)&to_send, sizeof(to_send)); 302 if (ret < 0) { 303 dev_err(&data->spi->dev, "Changing sensor delay failed\n"); 304 return ret; 305 } 306 307 data->delay_buf[type] = delay; 308 309 return 0; 310 } 311 EXPORT_SYMBOL(ssp_change_delay); 312 313 /** 314 * ssp_disable_sensor() - disables sensor 315 * 316 * @data: sensorhub structure 317 * @type: SSP sensor type 318 * 319 * Returns 0 or negative value in case of error 320 */ 321 int ssp_disable_sensor(struct ssp_data *data, enum ssp_sensor_type type) 322 { 323 int ret; 324 __le32 command; 325 326 if (data->sensor_enable & BIT(type)) { 327 command = cpu_to_le32(data->delay_buf[type]); 328 329 ret = ssp_send_instruction(data, 330 SSP_MSG2SSP_INST_BYPASS_SENSOR_RM, 331 type, (u8 *)&command, 332 sizeof(command)); 333 if (ret < 0) { 334 dev_err(&data->spi->dev, "Remove sensor fail\n"); 335 return ret; 336 } 337 338 data->sensor_enable &= ~BIT(type); 339 } 340 341 data->check_status[type] = SSP_ADD_SENSOR_STATE; 342 343 if (atomic_dec_and_test(&data->enable_refcount)) 344 ssp_disable_wdt_timer(data); 345 346 return 0; 347 } 348 EXPORT_SYMBOL(ssp_disable_sensor); 349 350 static irqreturn_t ssp_irq_thread_fn(int irq, void *dev_id) 351 { 352 struct ssp_data *data = dev_id; 353 354 /* 355 * This wrapper is done to preserve error path for ssp_irq_msg, also 356 * it is defined in different file. 357 */ 358 ssp_irq_msg(data); 359 360 return IRQ_HANDLED; 361 } 362 363 static int ssp_initialize_mcu(struct ssp_data *data) 364 { 365 int ret; 366 367 ssp_clean_pending_list(data); 368 369 ret = ssp_get_chipid(data); 370 if (ret != SSP_DEVICE_ID) { 371 dev_err(&data->spi->dev, "%s - MCU %s ret = %d\n", __func__, 372 ret < 0 ? "is not working" : "identification failed", 373 ret); 374 return ret < 0 ? ret : -ENODEV; 375 } 376 377 dev_info(&data->spi->dev, "MCU device ID = %d\n", ret); 378 379 /* 380 * needs clarification, for now do not want to export all transfer 381 * methods to sensors' drivers 382 */ 383 ret = ssp_set_magnetic_matrix(data); 384 if (ret < 0) { 385 dev_err(&data->spi->dev, 386 "%s - ssp_set_magnetic_matrix failed\n", __func__); 387 return ret; 388 } 389 390 data->available_sensors = ssp_get_sensor_scanning_info(data); 391 if (data->available_sensors == 0) { 392 dev_err(&data->spi->dev, 393 "%s - ssp_get_sensor_scanning_info failed\n", __func__); 394 return -EIO; 395 } 396 397 data->cur_firm_rev = ssp_get_firmware_rev(data); 398 dev_info(&data->spi->dev, "MCU Firm Rev : New = %8u\n", 399 data->cur_firm_rev); 400 401 return ssp_command(data, SSP_MSG2SSP_AP_MCU_DUMP_CHECK, 0); 402 } 403 404 /* 405 * sensorhub can request its reinitialization as some brutal and rare error 406 * handling. It can be requested from the MCU. 407 */ 408 static void ssp_refresh_task(struct work_struct *work) 409 { 410 struct ssp_data *data = container_of((struct delayed_work *)work, 411 struct ssp_data, work_refresh); 412 413 dev_info(&data->spi->dev, "refreshing\n"); 414 415 data->reset_cnt++; 416 417 if (ssp_initialize_mcu(data) >= 0) { 418 ssp_sync_available_sensors(data); 419 if (data->last_ap_state != 0) 420 ssp_command(data, data->last_ap_state, 0); 421 422 if (data->last_resume_state != 0) 423 ssp_command(data, data->last_resume_state, 0); 424 425 data->timeout_cnt = 0; 426 data->com_fail_cnt = 0; 427 } 428 } 429 430 int ssp_queue_ssp_refresh_task(struct ssp_data *data, unsigned int delay) 431 { 432 cancel_delayed_work_sync(&data->work_refresh); 433 434 return queue_delayed_work(system_power_efficient_wq, 435 &data->work_refresh, 436 msecs_to_jiffies(delay)); 437 } 438 439 #ifdef CONFIG_OF 440 static const struct of_device_id ssp_of_match[] = { 441 { 442 .compatible = "samsung,sensorhub-rinato", 443 .data = &ssp_rinato_info, 444 }, { 445 .compatible = "samsung,sensorhub-thermostat", 446 .data = &ssp_thermostat_info, 447 }, 448 {}, 449 }; 450 MODULE_DEVICE_TABLE(of, ssp_of_match); 451 452 static struct ssp_data *ssp_parse_dt(struct device *dev) 453 { 454 int ret; 455 struct ssp_data *data; 456 struct device_node *node = dev->of_node; 457 const struct of_device_id *match; 458 459 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 460 if (!data) 461 return NULL; 462 463 data->mcu_ap_gpio = of_get_named_gpio(node, "mcu-ap-gpios", 0); 464 if (data->mcu_ap_gpio < 0) 465 return NULL; 466 467 data->ap_mcu_gpio = of_get_named_gpio(node, "ap-mcu-gpios", 0); 468 if (data->ap_mcu_gpio < 0) 469 return NULL; 470 471 data->mcu_reset_gpio = of_get_named_gpio(node, "mcu-reset-gpios", 0); 472 if (data->mcu_reset_gpio < 0) 473 return NULL; 474 475 ret = devm_gpio_request_one(dev, data->ap_mcu_gpio, GPIOF_OUT_INIT_HIGH, 476 "ap-mcu-gpios"); 477 if (ret) 478 return NULL; 479 480 ret = devm_gpio_request_one(dev, data->mcu_reset_gpio, 481 GPIOF_OUT_INIT_HIGH, "mcu-reset-gpios"); 482 if (ret) 483 return NULL; 484 485 match = of_match_node(ssp_of_match, node); 486 if (!match) 487 return NULL; 488 489 data->sensorhub_info = match->data; 490 491 dev_set_drvdata(dev, data); 492 493 return data; 494 } 495 #else 496 static struct ssp_data *ssp_parse_dt(struct device *pdev) 497 { 498 return NULL; 499 } 500 #endif 501 502 /** 503 * ssp_register_consumer() - registers iio consumer in ssp framework 504 * 505 * @indio_dev: consumer iio device 506 * @type: ssp sensor type 507 */ 508 void ssp_register_consumer(struct iio_dev *indio_dev, enum ssp_sensor_type type) 509 { 510 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent); 511 512 data->sensor_devs[type] = indio_dev; 513 } 514 EXPORT_SYMBOL(ssp_register_consumer); 515 516 static int ssp_probe(struct spi_device *spi) 517 { 518 int ret, i; 519 struct ssp_data *data; 520 521 data = ssp_parse_dt(&spi->dev); 522 if (!data) { 523 dev_err(&spi->dev, "Failed to find platform data\n"); 524 return -ENODEV; 525 } 526 527 ret = mfd_add_devices(&spi->dev, -1, sensorhub_sensor_devs, 528 ARRAY_SIZE(sensorhub_sensor_devs), NULL, 0, NULL); 529 if (ret < 0) { 530 dev_err(&spi->dev, "mfd add devices fail\n"); 531 return ret; 532 } 533 534 spi->mode = SPI_MODE_1; 535 ret = spi_setup(spi); 536 if (ret < 0) { 537 dev_err(&spi->dev, "Failed to setup spi\n"); 538 return ret; 539 } 540 541 data->fw_dl_state = SSP_FW_DL_STATE_NONE; 542 data->spi = spi; 543 spi_set_drvdata(spi, data); 544 545 mutex_init(&data->comm_lock); 546 547 for (i = 0; i < SSP_SENSOR_MAX; ++i) { 548 data->delay_buf[i] = SSP_DEFAULT_POLLING_DELAY; 549 data->batch_latency_buf[i] = 0; 550 data->batch_opt_buf[i] = 0; 551 data->check_status[i] = SSP_INITIALIZATION_STATE; 552 } 553 554 data->delay_buf[SSP_BIO_HRM_LIB] = 100; 555 556 data->time_syncing = true; 557 558 mutex_init(&data->pending_lock); 559 INIT_LIST_HEAD(&data->pending_list); 560 561 atomic_set(&data->enable_refcount, 0); 562 563 INIT_WORK(&data->work_wdt, ssp_wdt_work_func); 564 INIT_DELAYED_WORK(&data->work_refresh, ssp_refresh_task); 565 566 timer_setup(&data->wdt_timer, ssp_wdt_timer_func, 0); 567 568 ret = request_threaded_irq(data->spi->irq, NULL, 569 ssp_irq_thread_fn, 570 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 571 "SSP_Int", data); 572 if (ret < 0) { 573 dev_err(&spi->dev, "Irq request fail\n"); 574 goto err_setup_irq; 575 } 576 577 /* Let's start with enabled one so irq balance could be ok */ 578 data->shut_down = false; 579 580 /* just to avoid unbalanced irq set wake up */ 581 enable_irq_wake(data->spi->irq); 582 583 data->fw_dl_state = ssp_check_fwbl(data); 584 if (data->fw_dl_state == SSP_FW_DL_STATE_NONE) { 585 ret = ssp_initialize_mcu(data); 586 if (ret < 0) { 587 dev_err(&spi->dev, "Initialize_mcu failed\n"); 588 goto err_read_reg; 589 } 590 } else { 591 dev_err(&spi->dev, "Firmware version not supported\n"); 592 ret = -EPERM; 593 goto err_read_reg; 594 } 595 596 return 0; 597 598 err_read_reg: 599 free_irq(data->spi->irq, data); 600 err_setup_irq: 601 mutex_destroy(&data->pending_lock); 602 mutex_destroy(&data->comm_lock); 603 604 dev_err(&spi->dev, "Probe failed!\n"); 605 606 return ret; 607 } 608 609 static int ssp_remove(struct spi_device *spi) 610 { 611 struct ssp_data *data = spi_get_drvdata(spi); 612 613 if (ssp_command(data, SSP_MSG2SSP_AP_STATUS_SHUTDOWN, 0) < 0) 614 dev_err(&data->spi->dev, 615 "SSP_MSG2SSP_AP_STATUS_SHUTDOWN failed\n"); 616 617 ssp_enable_mcu(data, false); 618 ssp_disable_wdt_timer(data); 619 620 ssp_clean_pending_list(data); 621 622 free_irq(data->spi->irq, data); 623 624 del_timer_sync(&data->wdt_timer); 625 cancel_work_sync(&data->work_wdt); 626 627 mutex_destroy(&data->comm_lock); 628 mutex_destroy(&data->pending_lock); 629 630 mfd_remove_devices(&spi->dev); 631 632 return 0; 633 } 634 635 #ifdef CONFIG_PM_SLEEP 636 static int ssp_suspend(struct device *dev) 637 { 638 int ret; 639 struct ssp_data *data = spi_get_drvdata(to_spi_device(dev)); 640 641 data->last_resume_state = SSP_MSG2SSP_AP_STATUS_SUSPEND; 642 643 if (atomic_read(&data->enable_refcount) > 0) 644 ssp_disable_wdt_timer(data); 645 646 ret = ssp_command(data, SSP_MSG2SSP_AP_STATUS_SUSPEND, 0); 647 if (ret < 0) { 648 dev_err(&data->spi->dev, 649 "%s SSP_MSG2SSP_AP_STATUS_SUSPEND failed\n", __func__); 650 651 ssp_enable_wdt_timer(data); 652 return ret; 653 } 654 655 data->time_syncing = false; 656 disable_irq(data->spi->irq); 657 658 return 0; 659 } 660 661 static int ssp_resume(struct device *dev) 662 { 663 int ret; 664 struct ssp_data *data = spi_get_drvdata(to_spi_device(dev)); 665 666 enable_irq(data->spi->irq); 667 668 if (atomic_read(&data->enable_refcount) > 0) 669 ssp_enable_wdt_timer(data); 670 671 ret = ssp_command(data, SSP_MSG2SSP_AP_STATUS_RESUME, 0); 672 if (ret < 0) { 673 dev_err(&data->spi->dev, 674 "%s SSP_MSG2SSP_AP_STATUS_RESUME failed\n", __func__); 675 ssp_disable_wdt_timer(data); 676 return ret; 677 } 678 679 /* timesyncing is set by MCU */ 680 data->last_resume_state = SSP_MSG2SSP_AP_STATUS_RESUME; 681 682 return 0; 683 } 684 #endif /* CONFIG_PM_SLEEP */ 685 686 static const struct dev_pm_ops ssp_pm_ops = { 687 SET_SYSTEM_SLEEP_PM_OPS(ssp_suspend, ssp_resume) 688 }; 689 690 static struct spi_driver ssp_driver = { 691 .probe = ssp_probe, 692 .remove = ssp_remove, 693 .driver = { 694 .pm = &ssp_pm_ops, 695 .of_match_table = of_match_ptr(ssp_of_match), 696 .name = "sensorhub" 697 }, 698 }; 699 700 module_spi_driver(ssp_driver); 701 702 MODULE_DESCRIPTION("ssp sensorhub driver"); 703 MODULE_AUTHOR("Samsung Electronics"); 704 MODULE_LICENSE("GPL"); 705