clk-pll-out.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <linux/clk.h>
  23. #include "clk.h"
  24. #define pll_out_enb(p) (BIT(p->enb_bit_idx))
  25. #define pll_out_rst(p) (BIT(p->rst_bit_idx))
  26. static int clk_pll_out_is_enabled(struct clk_hw *hw)
  27. {
  28. struct tegra_clk_pll_out *pll_out = to_clk_pll_out(hw);
  29. u32 val = readl_relaxed(pll_out->reg);
  30. int state;
  31. state = (val & pll_out_enb(pll_out)) ? 1 : 0;
  32. if (!(val & (pll_out_rst(pll_out))))
  33. state = 0;
  34. return state;
  35. }
  36. static int clk_pll_out_enable(struct clk_hw *hw)
  37. {
  38. struct tegra_clk_pll_out *pll_out = to_clk_pll_out(hw);
  39. unsigned long flags = 0;
  40. u32 val;
  41. if (pll_out->lock)
  42. spin_lock_irqsave(pll_out->lock, flags);
  43. val = readl_relaxed(pll_out->reg);
  44. val |= (pll_out_enb(pll_out) | pll_out_rst(pll_out));
  45. writel_relaxed(val, pll_out->reg);
  46. udelay(2);
  47. if (pll_out->lock)
  48. spin_unlock_irqrestore(pll_out->lock, flags);
  49. return 0;
  50. }
  51. static void clk_pll_out_disable(struct clk_hw *hw)
  52. {
  53. struct tegra_clk_pll_out *pll_out = to_clk_pll_out(hw);
  54. unsigned long flags = 0;
  55. u32 val;
  56. if (pll_out->lock)
  57. spin_lock_irqsave(pll_out->lock, flags);
  58. val = readl_relaxed(pll_out->reg);
  59. val &= ~(pll_out_enb(pll_out) | pll_out_rst(pll_out));
  60. writel_relaxed(val, pll_out->reg);
  61. udelay(2);
  62. if (pll_out->lock)
  63. spin_unlock_irqrestore(pll_out->lock, flags);
  64. }
  65. const struct clk_ops tegra_clk_pll_out_ops = {
  66. .is_enabled = clk_pll_out_is_enabled,
  67. .enable = clk_pll_out_enable,
  68. .disable = clk_pll_out_disable,
  69. };
  70. struct clk *tegra_clk_register_pll_out(const char *name,
  71. const char *parent_name, void __iomem *reg, u8 enb_bit_idx,
  72. u8 rst_bit_idx, unsigned long flags, u8 pll_out_flags,
  73. spinlock_t *lock)
  74. {
  75. struct tegra_clk_pll_out *pll_out;
  76. struct clk *clk;
  77. struct clk_init_data init;
  78. pll_out = kzalloc(sizeof(*pll_out), GFP_KERNEL);
  79. if (!pll_out)
  80. return ERR_PTR(-ENOMEM);
  81. init.name = name;
  82. init.ops = &tegra_clk_pll_out_ops;
  83. init.parent_names = (parent_name ? &parent_name : NULL);
  84. init.num_parents = (parent_name ? 1 : 0);
  85. init.flags = flags;
  86. pll_out->reg = reg;
  87. pll_out->enb_bit_idx = enb_bit_idx;
  88. pll_out->rst_bit_idx = rst_bit_idx;
  89. pll_out->flags = pll_out_flags;
  90. pll_out->lock = lock;
  91. /* Data in .init is copied by clk_register(), so stack variable OK */
  92. pll_out->hw.init = &init;
  93. clk = clk_register(NULL, &pll_out->hw);
  94. if (IS_ERR(clk))
  95. kfree(pll_out);
  96. return clk;
  97. }