clk-pxa.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Marvell PXA family clocks
  3. *
  4. * Copyright (C) 2014 Robert Jarzmik
  5. *
  6. * Common clock code for PXA clocks ("CKEN" type clocks + DT)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/of.h>
  17. #include <dt-bindings/clock/pxa-clock.h>
  18. #include "clk-pxa.h"
  19. DEFINE_SPINLOCK(lock);
  20. static struct clk *pxa_clocks[CLK_MAX];
  21. static struct clk_onecell_data onecell_data = {
  22. .clks = pxa_clocks,
  23. .clk_num = CLK_MAX,
  24. };
  25. struct pxa_clk {
  26. struct clk_hw hw;
  27. struct clk_fixed_factor lp;
  28. struct clk_fixed_factor hp;
  29. struct clk_gate gate;
  30. bool (*is_in_low_power)(void);
  31. };
  32. #define to_pxa_clk(_hw) container_of(_hw, struct pxa_clk, hw)
  33. static unsigned long cken_recalc_rate(struct clk_hw *hw,
  34. unsigned long parent_rate)
  35. {
  36. struct pxa_clk *pclk = to_pxa_clk(hw);
  37. struct clk_fixed_factor *fix;
  38. if (!pclk->is_in_low_power || pclk->is_in_low_power())
  39. fix = &pclk->lp;
  40. else
  41. fix = &pclk->hp;
  42. __clk_hw_set_clk(&fix->hw, hw);
  43. return clk_fixed_factor_ops.recalc_rate(&fix->hw, parent_rate);
  44. }
  45. static struct clk_ops cken_rate_ops = {
  46. .recalc_rate = cken_recalc_rate,
  47. };
  48. static u8 cken_get_parent(struct clk_hw *hw)
  49. {
  50. struct pxa_clk *pclk = to_pxa_clk(hw);
  51. if (!pclk->is_in_low_power)
  52. return 0;
  53. return pclk->is_in_low_power() ? 0 : 1;
  54. }
  55. static struct clk_ops cken_mux_ops = {
  56. .get_parent = cken_get_parent,
  57. .set_parent = dummy_clk_set_parent,
  58. };
  59. void __init clkdev_pxa_register(int ckid, const char *con_id,
  60. const char *dev_id, struct clk *clk)
  61. {
  62. if (!IS_ERR(clk) && (ckid != CLK_NONE))
  63. pxa_clocks[ckid] = clk;
  64. if (!IS_ERR(clk))
  65. clk_register_clkdev(clk, con_id, dev_id);
  66. }
  67. int __init clk_pxa_cken_init(const struct desc_clk_cken *clks, int nb_clks)
  68. {
  69. int i;
  70. struct pxa_clk *pxa_clk;
  71. struct clk *clk;
  72. for (i = 0; i < nb_clks; i++) {
  73. pxa_clk = kzalloc(sizeof(*pxa_clk), GFP_KERNEL);
  74. pxa_clk->is_in_low_power = clks[i].is_in_low_power;
  75. pxa_clk->lp = clks[i].lp;
  76. pxa_clk->hp = clks[i].hp;
  77. pxa_clk->gate = clks[i].gate;
  78. pxa_clk->gate.lock = &lock;
  79. clk = clk_register_composite(NULL, clks[i].name,
  80. clks[i].parent_names, 2,
  81. &pxa_clk->hw, &cken_mux_ops,
  82. &pxa_clk->hw, &cken_rate_ops,
  83. &pxa_clk->gate.hw, &clk_gate_ops,
  84. clks[i].flags);
  85. clkdev_pxa_register(clks[i].ckid, clks[i].con_id,
  86. clks[i].dev_id, clk);
  87. }
  88. return 0;
  89. }
  90. void __init clk_pxa_dt_common_init(struct device_node *np)
  91. {
  92. of_clk_add_provider(np, of_clk_src_onecell_get, &onecell_data);
  93. }