owl-sps-helper.c 870 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Actions Semi Owl Smart Power System (SPS) shared helpers
  4. *
  5. * Copyright 2012 Actions Semi Inc.
  6. * Author: Actions Semi, Inc.
  7. *
  8. * Copyright (c) 2017 Andreas Färber
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #define OWL_SPS_PG_CTL 0x0
  13. int owl_sps_set_pg(void __iomem *base, u32 pwr_mask, u32 ack_mask, bool enable)
  14. {
  15. u32 val;
  16. bool ack;
  17. int timeout;
  18. val = readl(base + OWL_SPS_PG_CTL);
  19. ack = val & ack_mask;
  20. if (ack == enable)
  21. return 0;
  22. if (enable)
  23. val |= pwr_mask;
  24. else
  25. val &= ~pwr_mask;
  26. writel(val, base + OWL_SPS_PG_CTL);
  27. for (timeout = 5000; timeout > 0; timeout -= 50) {
  28. val = readl(base + OWL_SPS_PG_CTL);
  29. if ((val & ack_mask) == (enable ? ack_mask : 0))
  30. break;
  31. udelay(50);
  32. }
  33. if (timeout <= 0)
  34. return -ETIMEDOUT;
  35. udelay(10);
  36. return 0;
  37. }
  38. EXPORT_SYMBOL_GPL(owl_sps_set_pg);