clk-periph-fixed.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/io.h>
  7. #include "clk.h"
  8. static inline struct tegra_clk_periph_fixed *
  9. to_tegra_clk_periph_fixed(struct clk_hw *hw)
  10. {
  11. return container_of(hw, struct tegra_clk_periph_fixed, hw);
  12. }
  13. static int tegra_clk_periph_fixed_is_enabled(struct clk_hw *hw)
  14. {
  15. struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
  16. u32 mask = 1 << (fixed->num % 32), value;
  17. value = readl(fixed->base + fixed->regs->enb_reg);
  18. if (value & mask) {
  19. value = readl(fixed->base + fixed->regs->rst_reg);
  20. if ((value & mask) == 0)
  21. return 1;
  22. }
  23. return 0;
  24. }
  25. static int tegra_clk_periph_fixed_enable(struct clk_hw *hw)
  26. {
  27. struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
  28. u32 mask = 1 << (fixed->num % 32);
  29. writel(mask, fixed->base + fixed->regs->enb_set_reg);
  30. return 0;
  31. }
  32. static void tegra_clk_periph_fixed_disable(struct clk_hw *hw)
  33. {
  34. struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
  35. u32 mask = 1 << (fixed->num % 32);
  36. writel(mask, fixed->base + fixed->regs->enb_clr_reg);
  37. }
  38. static unsigned long
  39. tegra_clk_periph_fixed_recalc_rate(struct clk_hw *hw,
  40. unsigned long parent_rate)
  41. {
  42. struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
  43. unsigned long long rate;
  44. rate = (unsigned long long)parent_rate * fixed->mul;
  45. do_div(rate, fixed->div);
  46. return (unsigned long)rate;
  47. }
  48. static const struct clk_ops tegra_clk_periph_fixed_ops = {
  49. .is_enabled = tegra_clk_periph_fixed_is_enabled,
  50. .enable = tegra_clk_periph_fixed_enable,
  51. .disable = tegra_clk_periph_fixed_disable,
  52. .recalc_rate = tegra_clk_periph_fixed_recalc_rate,
  53. };
  54. struct clk *tegra_clk_register_periph_fixed(const char *name,
  55. const char *parent,
  56. unsigned long flags,
  57. void __iomem *base,
  58. unsigned int mul,
  59. unsigned int div,
  60. unsigned int num)
  61. {
  62. const struct tegra_clk_periph_regs *regs;
  63. struct tegra_clk_periph_fixed *fixed;
  64. struct clk_init_data init;
  65. struct clk *clk;
  66. regs = get_reg_bank(num);
  67. if (!regs)
  68. return ERR_PTR(-EINVAL);
  69. fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
  70. if (!fixed)
  71. return ERR_PTR(-ENOMEM);
  72. init.name = name;
  73. init.flags = flags;
  74. init.parent_names = parent ? &parent : NULL;
  75. init.num_parents = parent ? 1 : 0;
  76. init.ops = &tegra_clk_periph_fixed_ops;
  77. fixed->base = base;
  78. fixed->regs = regs;
  79. fixed->mul = mul;
  80. fixed->div = div;
  81. fixed->num = num;
  82. fixed->hw.init = &init;
  83. clk = clk_register(NULL, &fixed->hw);
  84. if (IS_ERR(clk))
  85. kfree(fixed);
  86. return clk;
  87. }