reset-hi3660.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, "hisilicon,rst-syscon");
  71. if (rc->map == ERR_PTR(-ENODEV)) {
  72. /* fall back to the deprecated compatible */
  73. rc->map = syscon_regmap_lookup_by_phandle(np,
  74. "hisi,rst-syscon");
  75. }
  76. if (IS_ERR(rc->map)) {
  77. dev_err(dev, "failed to get hisilicon,rst-syscon\n");
  78. return PTR_ERR(rc->map);
  79. }
  80. rc->rst.ops = &hi3660_reset_ops,
  81. rc->rst.of_node = np;
  82. rc->rst.of_reset_n_cells = 2;
  83. rc->rst.of_xlate = hi3660_reset_xlate;
  84. return reset_controller_register(&rc->rst);
  85. }
  86. static const struct of_device_id hi3660_reset_match[] = {
  87. { .compatible = "hisilicon,hi3660-reset", },
  88. {},
  89. };
  90. MODULE_DEVICE_TABLE(of, hi3660_reset_match);
  91. static struct platform_driver hi3660_reset_driver = {
  92. .probe = hi3660_reset_probe,
  93. .driver = {
  94. .name = "hi3660-reset",
  95. .of_match_table = hi3660_reset_match,
  96. },
  97. };
  98. static int __init hi3660_reset_init(void)
  99. {
  100. return platform_driver_register(&hi3660_reset_driver);
  101. }
  102. arch_initcall(hi3660_reset_init);
  103. MODULE_LICENSE("GPL");
  104. MODULE_ALIAS("platform:hi3660-reset");
  105. MODULE_DESCRIPTION("HiSilicon Hi3660 Reset Driver");