syscon-reboot-mode.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
  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. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reboot.h>
  15. #include <linux/regmap.h>
  16. #include <linux/mfd/syscon.h>
  17. #include "reboot-mode.h"
  18. struct syscon_reboot_mode {
  19. struct regmap *map;
  20. struct reboot_mode_driver reboot;
  21. u32 offset;
  22. u32 mask;
  23. };
  24. static int syscon_reboot_mode_write(struct reboot_mode_driver *reboot,
  25. unsigned int magic)
  26. {
  27. struct syscon_reboot_mode *syscon_rbm;
  28. int ret;
  29. syscon_rbm = container_of(reboot, struct syscon_reboot_mode, reboot);
  30. ret = regmap_update_bits(syscon_rbm->map, syscon_rbm->offset,
  31. syscon_rbm->mask, magic);
  32. if (ret < 0)
  33. dev_err(reboot->dev, "update reboot mode bits failed\n");
  34. return ret;
  35. }
  36. static int syscon_reboot_mode_probe(struct platform_device *pdev)
  37. {
  38. int ret;
  39. struct syscon_reboot_mode *syscon_rbm;
  40. syscon_rbm = devm_kzalloc(&pdev->dev, sizeof(*syscon_rbm), GFP_KERNEL);
  41. if (!syscon_rbm)
  42. return -ENOMEM;
  43. syscon_rbm->reboot.dev = &pdev->dev;
  44. syscon_rbm->reboot.write = syscon_reboot_mode_write;
  45. syscon_rbm->mask = 0xffffffff;
  46. dev_set_drvdata(&pdev->dev, syscon_rbm);
  47. syscon_rbm->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
  48. if (IS_ERR(syscon_rbm->map))
  49. return PTR_ERR(syscon_rbm->map);
  50. if (of_property_read_u32(pdev->dev.of_node, "offset",
  51. &syscon_rbm->offset))
  52. return -EINVAL;
  53. of_property_read_u32(pdev->dev.of_node, "mask", &syscon_rbm->mask);
  54. ret = reboot_mode_register(&syscon_rbm->reboot);
  55. if (ret)
  56. dev_err(&pdev->dev, "can't register reboot mode\n");
  57. return ret;
  58. }
  59. static int syscon_reboot_mode_remove(struct platform_device *pdev)
  60. {
  61. struct syscon_reboot_mode *syscon_rbm = dev_get_drvdata(&pdev->dev);
  62. return reboot_mode_unregister(&syscon_rbm->reboot);
  63. }
  64. static const struct of_device_id syscon_reboot_mode_of_match[] = {
  65. { .compatible = "syscon-reboot-mode" },
  66. {}
  67. };
  68. static struct platform_driver syscon_reboot_mode_driver = {
  69. .probe = syscon_reboot_mode_probe,
  70. .remove = syscon_reboot_mode_remove,
  71. .driver = {
  72. .name = "syscon-reboot-mode",
  73. .of_match_table = syscon_reboot_mode_of_match,
  74. },
  75. };
  76. module_platform_driver(syscon_reboot_mode_driver);
  77. MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com");
  78. MODULE_DESCRIPTION("SYSCON reboot mode driver");
  79. MODULE_LICENSE("GPL v2");