clk-apmu.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * mmp AXI peripharal clock operation source file
  3. *
  4. * Copyright (C) 2012 Marvell
  5. * Chao Xie <xiechao.mail@gmail.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/io.h>
  13. #include <linux/err.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include "clk.h"
  17. #define to_clk_apmu(clk) (container_of(clk, struct clk_apmu, clk))
  18. struct clk_apmu {
  19. struct clk_hw hw;
  20. void __iomem *base;
  21. u32 rst_mask;
  22. u32 enable_mask;
  23. spinlock_t *lock;
  24. };
  25. static int clk_apmu_enable(struct clk_hw *hw)
  26. {
  27. struct clk_apmu *apmu = to_clk_apmu(hw);
  28. unsigned long data;
  29. unsigned long flags = 0;
  30. if (apmu->lock)
  31. spin_lock_irqsave(apmu->lock, flags);
  32. data = readl_relaxed(apmu->base) | apmu->enable_mask;
  33. writel_relaxed(data, apmu->base);
  34. if (apmu->lock)
  35. spin_unlock_irqrestore(apmu->lock, flags);
  36. return 0;
  37. }
  38. static void clk_apmu_disable(struct clk_hw *hw)
  39. {
  40. struct clk_apmu *apmu = to_clk_apmu(hw);
  41. unsigned long data;
  42. unsigned long flags = 0;
  43. if (apmu->lock)
  44. spin_lock_irqsave(apmu->lock, flags);
  45. data = readl_relaxed(apmu->base) & ~apmu->enable_mask;
  46. writel_relaxed(data, apmu->base);
  47. if (apmu->lock)
  48. spin_unlock_irqrestore(apmu->lock, flags);
  49. }
  50. static const struct clk_ops clk_apmu_ops = {
  51. .enable = clk_apmu_enable,
  52. .disable = clk_apmu_disable,
  53. };
  54. struct clk *mmp_clk_register_apmu(const char *name, const char *parent_name,
  55. void __iomem *base, u32 enable_mask, spinlock_t *lock)
  56. {
  57. struct clk_apmu *apmu;
  58. struct clk *clk;
  59. struct clk_init_data init;
  60. apmu = kzalloc(sizeof(*apmu), GFP_KERNEL);
  61. if (!apmu)
  62. return NULL;
  63. init.name = name;
  64. init.ops = &clk_apmu_ops;
  65. init.flags = CLK_SET_RATE_PARENT;
  66. init.parent_names = (parent_name ? &parent_name : NULL);
  67. init.num_parents = (parent_name ? 1 : 0);
  68. apmu->base = base;
  69. apmu->enable_mask = enable_mask;
  70. apmu->lock = lock;
  71. apmu->hw.init = &init;
  72. clk = clk_register(NULL, &apmu->hw);
  73. if (IS_ERR(clk))
  74. kfree(apmu);
  75. return clk;
  76. }