reset-hi3660.c 3.2 KB

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