clk-uniphier-gate.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2016 Socionext Inc.
  3. * Author: Masahiro Yamada <[email protected]>
  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 as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/clk-provider.h>
  16. #include <linux/device.h>
  17. #include <linux/regmap.h>
  18. #include "clk-uniphier.h"
  19. struct uniphier_clk_gate {
  20. struct clk_hw hw;
  21. struct regmap *regmap;
  22. unsigned int reg;
  23. unsigned int bit;
  24. };
  25. #define to_uniphier_clk_gate(_hw) \
  26. container_of(_hw, struct uniphier_clk_gate, hw)
  27. static int uniphier_clk_gate_endisable(struct clk_hw *hw, int enable)
  28. {
  29. struct uniphier_clk_gate *gate = to_uniphier_clk_gate(hw);
  30. return regmap_write_bits(gate->regmap, gate->reg, BIT(gate->bit),
  31. enable ? BIT(gate->bit) : 0);
  32. }
  33. static int uniphier_clk_gate_enable(struct clk_hw *hw)
  34. {
  35. return uniphier_clk_gate_endisable(hw, 1);
  36. }
  37. static void uniphier_clk_gate_disable(struct clk_hw *hw)
  38. {
  39. if (uniphier_clk_gate_endisable(hw, 0) < 0)
  40. pr_warn("failed to disable clk\n");
  41. }
  42. static int uniphier_clk_gate_is_enabled(struct clk_hw *hw)
  43. {
  44. struct uniphier_clk_gate *gate = to_uniphier_clk_gate(hw);
  45. unsigned int val;
  46. if (regmap_read(gate->regmap, gate->reg, &val) < 0)
  47. pr_warn("is_enabled() may return wrong result\n");
  48. return !!(val & BIT(gate->bit));
  49. }
  50. static const struct clk_ops uniphier_clk_gate_ops = {
  51. .enable = uniphier_clk_gate_enable,
  52. .disable = uniphier_clk_gate_disable,
  53. .is_enabled = uniphier_clk_gate_is_enabled,
  54. };
  55. struct clk_hw *uniphier_clk_register_gate(struct device *dev,
  56. struct regmap *regmap,
  57. const char *name,
  58. const struct uniphier_clk_gate_data *data)
  59. {
  60. struct uniphier_clk_gate *gate;
  61. struct clk_init_data init;
  62. int ret;
  63. gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
  64. if (!gate)
  65. return ERR_PTR(-ENOMEM);
  66. init.name = name;
  67. init.ops = &uniphier_clk_gate_ops;
  68. init.flags = data->parent_name ? CLK_SET_RATE_PARENT : 0;
  69. init.parent_names = data->parent_name ? &data->parent_name : NULL;
  70. init.num_parents = data->parent_name ? 1 : 0;
  71. gate->regmap = regmap;
  72. gate->reg = data->reg;
  73. gate->bit = data->bit;
  74. gate->hw.init = &init;
  75. ret = devm_clk_hw_register(dev, &gate->hw);
  76. if (ret)
  77. return ERR_PTR(ret);
  78. return &gate->hw;
  79. }