clk-gate.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: James Liao <jamesjj.liao@mediatek.com>
  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 version 2 as
  7. * published by the Free Software Foundation.
  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/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/clkdev.h>
  20. #include "clk-mtk.h"
  21. #include "clk-gate.h"
  22. static int mtk_cg_bit_is_cleared(struct clk_hw *hw)
  23. {
  24. struct mtk_clk_gate *cg = to_clk_gate(hw);
  25. u32 val;
  26. regmap_read(cg->regmap, cg->sta_ofs, &val);
  27. val &= BIT(cg->bit);
  28. return val == 0;
  29. }
  30. static int mtk_cg_bit_is_set(struct clk_hw *hw)
  31. {
  32. struct mtk_clk_gate *cg = to_clk_gate(hw);
  33. u32 val;
  34. regmap_read(cg->regmap, cg->sta_ofs, &val);
  35. val &= BIT(cg->bit);
  36. return val != 0;
  37. }
  38. static void mtk_cg_set_bit(struct clk_hw *hw)
  39. {
  40. struct mtk_clk_gate *cg = to_clk_gate(hw);
  41. regmap_write(cg->regmap, cg->set_ofs, BIT(cg->bit));
  42. }
  43. static void mtk_cg_clr_bit(struct clk_hw *hw)
  44. {
  45. struct mtk_clk_gate *cg = to_clk_gate(hw);
  46. regmap_write(cg->regmap, cg->clr_ofs, BIT(cg->bit));
  47. }
  48. static int mtk_cg_enable(struct clk_hw *hw)
  49. {
  50. mtk_cg_clr_bit(hw);
  51. return 0;
  52. }
  53. static void mtk_cg_disable(struct clk_hw *hw)
  54. {
  55. mtk_cg_set_bit(hw);
  56. }
  57. static int mtk_cg_enable_inv(struct clk_hw *hw)
  58. {
  59. mtk_cg_set_bit(hw);
  60. return 0;
  61. }
  62. static void mtk_cg_disable_inv(struct clk_hw *hw)
  63. {
  64. mtk_cg_clr_bit(hw);
  65. }
  66. const struct clk_ops mtk_clk_gate_ops_setclr = {
  67. .is_enabled = mtk_cg_bit_is_cleared,
  68. .enable = mtk_cg_enable,
  69. .disable = mtk_cg_disable,
  70. };
  71. const struct clk_ops mtk_clk_gate_ops_setclr_inv = {
  72. .is_enabled = mtk_cg_bit_is_set,
  73. .enable = mtk_cg_enable_inv,
  74. .disable = mtk_cg_disable_inv,
  75. };
  76. struct clk * __init mtk_clk_register_gate(
  77. const char *name,
  78. const char *parent_name,
  79. struct regmap *regmap,
  80. int set_ofs,
  81. int clr_ofs,
  82. int sta_ofs,
  83. u8 bit,
  84. const struct clk_ops *ops)
  85. {
  86. struct mtk_clk_gate *cg;
  87. struct clk *clk;
  88. struct clk_init_data init = {};
  89. cg = kzalloc(sizeof(*cg), GFP_KERNEL);
  90. if (!cg)
  91. return ERR_PTR(-ENOMEM);
  92. init.name = name;
  93. init.flags = CLK_SET_RATE_PARENT;
  94. init.parent_names = parent_name ? &parent_name : NULL;
  95. init.num_parents = parent_name ? 1 : 0;
  96. init.ops = ops;
  97. cg->regmap = regmap;
  98. cg->set_ofs = set_ofs;
  99. cg->clr_ofs = clr_ofs;
  100. cg->sta_ofs = sta_ofs;
  101. cg->bit = bit;
  102. cg->hw.init = &init;
  103. clk = clk_register(NULL, &cg->hw);
  104. if (IS_ERR(clk))
  105. kfree(cg);
  106. return clk;
  107. }