clk-pll-out.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/io.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/clk-provider.h>
  22. #include "clk.h"
  23. #define pll_out_enb(p) (BIT(p->enb_bit_idx))
  24. #define pll_out_rst(p) (BIT(p->rst_bit_idx))
  25. static int clk_pll_out_is_enabled(struct clk_hw *hw)
  26. {
  27. struct tegra_clk_pll_out *pll_out = to_clk_pll_out(hw);
  28. u32 val = readl_relaxed(pll_out->reg);
  29. int state;
  30. state = (val & pll_out_enb(pll_out)) ? 1 : 0;
  31. if (!(val & (pll_out_rst(pll_out))))
  32. state = 0;
  33. return state;
  34. }
  35. static int clk_pll_out_enable(struct clk_hw *hw)
  36. {
  37. struct tegra_clk_pll_out *pll_out = to_clk_pll_out(hw);
  38. unsigned long flags = 0;
  39. u32 val;
  40. if (pll_out->lock)
  41. spin_lock_irqsave(pll_out->lock, flags);
  42. val = readl_relaxed(pll_out->reg);
  43. val |= (pll_out_enb(pll_out) | pll_out_rst(pll_out));
  44. writel_relaxed(val, pll_out->reg);
  45. udelay(2);
  46. if (pll_out->lock)
  47. spin_unlock_irqrestore(pll_out->lock, flags);
  48. return 0;
  49. }
  50. static void clk_pll_out_disable(struct clk_hw *hw)
  51. {
  52. struct tegra_clk_pll_out *pll_out = to_clk_pll_out(hw);
  53. unsigned long flags = 0;
  54. u32 val;
  55. if (pll_out->lock)
  56. spin_lock_irqsave(pll_out->lock, flags);
  57. val = readl_relaxed(pll_out->reg);
  58. val &= ~(pll_out_enb(pll_out) | pll_out_rst(pll_out));
  59. writel_relaxed(val, pll_out->reg);
  60. udelay(2);
  61. if (pll_out->lock)
  62. spin_unlock_irqrestore(pll_out->lock, flags);
  63. }
  64. const struct clk_ops tegra_clk_pll_out_ops = {
  65. .is_enabled = clk_pll_out_is_enabled,
  66. .enable = clk_pll_out_enable,
  67. .disable = clk_pll_out_disable,
  68. };
  69. struct clk *tegra_clk_register_pll_out(const char *name,
  70. const char *parent_name, void __iomem *reg, u8 enb_bit_idx,
  71. u8 rst_bit_idx, unsigned long flags, u8 pll_out_flags,
  72. spinlock_t *lock)
  73. {
  74. struct tegra_clk_pll_out *pll_out;
  75. struct clk *clk;
  76. struct clk_init_data init;
  77. pll_out = kzalloc(sizeof(*pll_out), GFP_KERNEL);
  78. if (!pll_out)
  79. return ERR_PTR(-ENOMEM);
  80. init.name = name;
  81. init.ops = &tegra_clk_pll_out_ops;
  82. init.parent_names = (parent_name ? &parent_name : NULL);
  83. init.num_parents = (parent_name ? 1 : 0);
  84. init.flags = flags;
  85. pll_out->reg = reg;
  86. pll_out->enb_bit_idx = enb_bit_idx;
  87. pll_out->rst_bit_idx = rst_bit_idx;
  88. pll_out->flags = pll_out_flags;
  89. pll_out->lock = lock;
  90. /* Data in .init is copied by clk_register(), so stack variable OK */
  91. pll_out->hw.init = &init;
  92. clk = clk_register(NULL, &pll_out->hw);
  93. if (IS_ERR(clk))
  94. kfree(pll_out);
  95. return clk;
  96. }