clk-apmu.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include "clk.h"
  18. #define to_clk_apmu(clk) (container_of(clk, struct clk_apmu, clk))
  19. struct clk_apmu {
  20. struct clk_hw hw;
  21. void __iomem *base;
  22. u32 rst_mask;
  23. u32 enable_mask;
  24. spinlock_t *lock;
  25. };
  26. static int clk_apmu_enable(struct clk_hw *hw)
  27. {
  28. struct clk_apmu *apmu = to_clk_apmu(hw);
  29. unsigned long data;
  30. unsigned long flags = 0;
  31. if (apmu->lock)
  32. spin_lock_irqsave(apmu->lock, flags);
  33. data = readl_relaxed(apmu->base) | apmu->enable_mask;
  34. writel_relaxed(data, apmu->base);
  35. if (apmu->lock)
  36. spin_unlock_irqrestore(apmu->lock, flags);
  37. return 0;
  38. }
  39. static void clk_apmu_disable(struct clk_hw *hw)
  40. {
  41. struct clk_apmu *apmu = to_clk_apmu(hw);
  42. unsigned long data;
  43. unsigned long flags = 0;
  44. if (apmu->lock)
  45. spin_lock_irqsave(apmu->lock, flags);
  46. data = readl_relaxed(apmu->base) & ~apmu->enable_mask;
  47. writel_relaxed(data, apmu->base);
  48. if (apmu->lock)
  49. spin_unlock_irqrestore(apmu->lock, flags);
  50. }
  51. static struct clk_ops clk_apmu_ops = {
  52. .enable = clk_apmu_enable,
  53. .disable = clk_apmu_disable,
  54. };
  55. struct clk *mmp_clk_register_apmu(const char *name, const char *parent_name,
  56. void __iomem *base, u32 enable_mask, spinlock_t *lock)
  57. {
  58. struct clk_apmu *apmu;
  59. struct clk *clk;
  60. struct clk_init_data init;
  61. apmu = kzalloc(sizeof(*apmu), GFP_KERNEL);
  62. if (!apmu)
  63. return NULL;
  64. init.name = name;
  65. init.ops = &clk_apmu_ops;
  66. init.flags = CLK_SET_RATE_PARENT;
  67. init.parent_names = (parent_name ? &parent_name : NULL);
  68. init.num_parents = (parent_name ? 1 : 0);
  69. apmu->base = base;
  70. apmu->enable_mask = enable_mask;
  71. apmu->lock = lock;
  72. apmu->hw.init = &init;
  73. clk = clk_register(NULL, &apmu->hw);
  74. if (IS_ERR(clk))
  75. kfree(apmu);
  76. return clk;
  77. }