qcom-reboot-reason.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2019, 2021 The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/err.h>
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/io.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/module.h>
  11. #include <linux/reboot.h>
  12. #include <linux/pm.h>
  13. #include <linux/of_address.h>
  14. #include <linux/nvmem-consumer.h>
  15. struct qcom_reboot_reason {
  16. struct device *dev;
  17. struct notifier_block reboot_nb;
  18. struct nvmem_cell *nvmem_cell;
  19. };
  20. struct poweroff_reason {
  21. const char *cmd;
  22. unsigned char pon_reason;
  23. };
  24. static struct poweroff_reason reasons[] = {
  25. { "recovery", 0x01 },
  26. { "bootloader", 0x02 },
  27. { "rtc", 0x03 },
  28. { "dm-verity device corrupted", 0x04 },
  29. { "dm-verity enforcing", 0x05 },
  30. { "keys clear", 0x06 },
  31. {}
  32. };
  33. static int qcom_reboot_reason_reboot(struct notifier_block *this,
  34. unsigned long event, void *ptr)
  35. {
  36. char *cmd = ptr;
  37. struct qcom_reboot_reason *reboot = container_of(this,
  38. struct qcom_reboot_reason, reboot_nb);
  39. struct poweroff_reason *reason;
  40. if (!cmd)
  41. return NOTIFY_OK;
  42. for (reason = reasons; reason->cmd; reason++) {
  43. if (!strcmp(cmd, reason->cmd)) {
  44. nvmem_cell_write(reboot->nvmem_cell,
  45. &reason->pon_reason,
  46. sizeof(reason->pon_reason));
  47. break;
  48. }
  49. }
  50. return NOTIFY_OK;
  51. }
  52. static int qcom_reboot_reason_probe(struct platform_device *pdev)
  53. {
  54. struct qcom_reboot_reason *reboot;
  55. reboot = devm_kzalloc(&pdev->dev, sizeof(*reboot), GFP_KERNEL);
  56. if (!reboot)
  57. return -ENOMEM;
  58. reboot->dev = &pdev->dev;
  59. reboot->nvmem_cell = nvmem_cell_get(reboot->dev, "restart_reason");
  60. if (IS_ERR(reboot->nvmem_cell))
  61. return PTR_ERR(reboot->nvmem_cell);
  62. reboot->reboot_nb.notifier_call = qcom_reboot_reason_reboot;
  63. reboot->reboot_nb.priority = 255;
  64. register_reboot_notifier(&reboot->reboot_nb);
  65. platform_set_drvdata(pdev, reboot);
  66. return 0;
  67. }
  68. static int qcom_reboot_reason_remove(struct platform_device *pdev)
  69. {
  70. struct qcom_reboot_reason *reboot = platform_get_drvdata(pdev);
  71. unregister_reboot_notifier(&reboot->reboot_nb);
  72. return 0;
  73. }
  74. static const struct of_device_id of_qcom_reboot_reason_match[] = {
  75. { .compatible = "qcom,reboot-reason", },
  76. {},
  77. };
  78. MODULE_DEVICE_TABLE(of, of_qcom_reboot_reason_match);
  79. static struct platform_driver qcom_reboot_reason_driver = {
  80. .probe = qcom_reboot_reason_probe,
  81. .remove = qcom_reboot_reason_remove,
  82. .driver = {
  83. .name = "qcom-reboot-reason",
  84. .of_match_table = of_match_ptr(of_qcom_reboot_reason_match),
  85. },
  86. };
  87. module_platform_driver(qcom_reboot_reason_driver);
  88. MODULE_DESCRIPTION("MSM Reboot Reason Driver");
  89. MODULE_LICENSE("GPL v2");