reset-hi3660.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016-2017 Linaro Ltd.
  4. * Copyright (c) 2016-2017 HiSilicon Technologies Co., Ltd.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/mfd/syscon.h>
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/reset-controller.h>
  13. struct hi3660_reset_controller {
  14. struct reset_controller_dev rst;
  15. struct regmap *map;
  16. };
  17. #define to_hi3660_reset_controller(_rst) \
  18. container_of(_rst, struct hi3660_reset_controller, rst)
  19. static int hi3660_reset_program_hw(struct reset_controller_dev *rcdev,
  20. unsigned long idx, bool assert)
  21. {
  22. struct hi3660_reset_controller *rc = to_hi3660_reset_controller(rcdev);
  23. unsigned int offset = idx >> 8;
  24. unsigned int mask = BIT(idx & 0x1f);
  25. if (assert)
  26. return regmap_write(rc->map, offset, mask);
  27. else
  28. return regmap_write(rc->map, offset + 4, mask);
  29. }
  30. static int hi3660_reset_assert(struct reset_controller_dev *rcdev,
  31. unsigned long idx)
  32. {
  33. return hi3660_reset_program_hw(rcdev, idx, true);
  34. }
  35. static int hi3660_reset_deassert(struct reset_controller_dev *rcdev,
  36. unsigned long idx)
  37. {
  38. return hi3660_reset_program_hw(rcdev, idx, false);
  39. }
  40. static int hi3660_reset_dev(struct reset_controller_dev *rcdev,
  41. unsigned long idx)
  42. {
  43. int err;
  44. err = hi3660_reset_assert(rcdev, idx);
  45. if (err)
  46. return err;
  47. return hi3660_reset_deassert(rcdev, idx);
  48. }
  49. static const struct reset_control_ops hi3660_reset_ops = {
  50. .reset = hi3660_reset_dev,
  51. .assert = hi3660_reset_assert,
  52. .deassert = hi3660_reset_deassert,
  53. };
  54. static int hi3660_reset_xlate(struct reset_controller_dev *rcdev,
  55. const struct of_phandle_args *reset_spec)
  56. {
  57. unsigned int offset, bit;
  58. offset = reset_spec->args[0];
  59. bit = reset_spec->args[1];
  60. return (offset << 8) | bit;
  61. }
  62. static int hi3660_reset_probe(struct platform_device *pdev)
  63. {
  64. struct hi3660_reset_controller *rc;
  65. struct device_node *np = pdev->dev.of_node;
  66. struct device *dev = &pdev->dev;
  67. rc = devm_kzalloc(dev, sizeof(*rc), GFP_KERNEL);
  68. if (!rc)
  69. return -ENOMEM;
  70. rc->map = syscon_regmap_lookup_by_phandle(np, "hisi,rst-syscon");
  71. if (IS_ERR(rc->map)) {
  72. dev_err(dev, "failed to get hi3660,rst-syscon\n");
  73. return PTR_ERR(rc->map);
  74. }
  75. rc->rst.ops = &hi3660_reset_ops,
  76. rc->rst.of_node = np;
  77. rc->rst.of_reset_n_cells = 2;
  78. rc->rst.of_xlate = hi3660_reset_xlate;
  79. return reset_controller_register(&rc->rst);
  80. }
  81. static const struct of_device_id hi3660_reset_match[] = {
  82. { .compatible = "hisilicon,hi3660-reset", },
  83. {},
  84. };
  85. MODULE_DEVICE_TABLE(of, hi3660_reset_match);
  86. static struct platform_driver hi3660_reset_driver = {
  87. .probe = hi3660_reset_probe,
  88. .driver = {
  89. .name = "hi3660-reset",
  90. .of_match_table = hi3660_reset_match,
  91. },
  92. };
  93. static int __init hi3660_reset_init(void)
  94. {
  95. return platform_driver_register(&hi3660_reset_driver);
  96. }
  97. arch_initcall(hi3660_reset_init);
  98. MODULE_LICENSE("GPL");
  99. MODULE_ALIAS("platform:hi3660-reset");
  100. MODULE_DESCRIPTION("HiSilicon Hi3660 Reset Driver");