reset-ath79.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
  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. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reset-controller.h>
  17. struct ath79_reset {
  18. struct reset_controller_dev rcdev;
  19. void __iomem *base;
  20. spinlock_t lock;
  21. };
  22. static int ath79_reset_update(struct reset_controller_dev *rcdev,
  23. unsigned long id, bool assert)
  24. {
  25. struct ath79_reset *ath79_reset =
  26. container_of(rcdev, struct ath79_reset, rcdev);
  27. unsigned long flags;
  28. u32 val;
  29. spin_lock_irqsave(&ath79_reset->lock, flags);
  30. val = readl(ath79_reset->base);
  31. if (assert)
  32. val |= BIT(id);
  33. else
  34. val &= ~BIT(id);
  35. writel(val, ath79_reset->base);
  36. spin_unlock_irqrestore(&ath79_reset->lock, flags);
  37. return 0;
  38. }
  39. static int ath79_reset_assert(struct reset_controller_dev *rcdev,
  40. unsigned long id)
  41. {
  42. return ath79_reset_update(rcdev, id, true);
  43. }
  44. static int ath79_reset_deassert(struct reset_controller_dev *rcdev,
  45. unsigned long id)
  46. {
  47. return ath79_reset_update(rcdev, id, false);
  48. }
  49. static int ath79_reset_status(struct reset_controller_dev *rcdev,
  50. unsigned long id)
  51. {
  52. struct ath79_reset *ath79_reset =
  53. container_of(rcdev, struct ath79_reset, rcdev);
  54. u32 val;
  55. val = readl(ath79_reset->base);
  56. return !!(val & BIT(id));
  57. }
  58. static struct reset_control_ops ath79_reset_ops = {
  59. .assert = ath79_reset_assert,
  60. .deassert = ath79_reset_deassert,
  61. .status = ath79_reset_status,
  62. };
  63. static int ath79_reset_probe(struct platform_device *pdev)
  64. {
  65. struct ath79_reset *ath79_reset;
  66. struct resource *res;
  67. ath79_reset = devm_kzalloc(&pdev->dev,
  68. sizeof(*ath79_reset), GFP_KERNEL);
  69. if (!ath79_reset)
  70. return -ENOMEM;
  71. platform_set_drvdata(pdev, ath79_reset);
  72. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  73. ath79_reset->base = devm_ioremap_resource(&pdev->dev, res);
  74. if (IS_ERR(ath79_reset->base))
  75. return PTR_ERR(ath79_reset->base);
  76. spin_lock_init(&ath79_reset->lock);
  77. ath79_reset->rcdev.ops = &ath79_reset_ops;
  78. ath79_reset->rcdev.owner = THIS_MODULE;
  79. ath79_reset->rcdev.of_node = pdev->dev.of_node;
  80. ath79_reset->rcdev.of_reset_n_cells = 1;
  81. ath79_reset->rcdev.nr_resets = 32;
  82. return reset_controller_register(&ath79_reset->rcdev);
  83. }
  84. static int ath79_reset_remove(struct platform_device *pdev)
  85. {
  86. struct ath79_reset *ath79_reset = platform_get_drvdata(pdev);
  87. reset_controller_unregister(&ath79_reset->rcdev);
  88. return 0;
  89. }
  90. static const struct of_device_id ath79_reset_dt_ids[] = {
  91. { .compatible = "qca,ar7100-reset", },
  92. { },
  93. };
  94. MODULE_DEVICE_TABLE(of, ath79_reset_dt_ids);
  95. static struct platform_driver ath79_reset_driver = {
  96. .probe = ath79_reset_probe,
  97. .remove = ath79_reset_remove,
  98. .driver = {
  99. .name = "ath79-reset",
  100. .of_match_table = ath79_reset_dt_ids,
  101. },
  102. };
  103. module_platform_driver(ath79_reset_driver);
  104. MODULE_AUTHOR("Alban Bedel <albeu@free.fr>");
  105. MODULE_DESCRIPTION("AR71xx Reset Controller Driver");
  106. MODULE_LICENSE("GPL");