clk-plldiv.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/io.h>
  16. #include "pmc.h"
  17. #define to_clk_plldiv(hw) container_of(hw, struct clk_plldiv, hw)
  18. struct clk_plldiv {
  19. struct clk_hw hw;
  20. struct at91_pmc *pmc;
  21. };
  22. static unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw,
  23. unsigned long parent_rate)
  24. {
  25. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  26. struct at91_pmc *pmc = plldiv->pmc;
  27. if (pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_PLLADIV2)
  28. return parent_rate / 2;
  29. return parent_rate;
  30. }
  31. static long clk_plldiv_round_rate(struct clk_hw *hw, unsigned long rate,
  32. unsigned long *parent_rate)
  33. {
  34. unsigned long div;
  35. if (rate > *parent_rate)
  36. return *parent_rate;
  37. div = *parent_rate / 2;
  38. if (rate < div)
  39. return div;
  40. if (rate - div < *parent_rate - rate)
  41. return div;
  42. return *parent_rate;
  43. }
  44. static int clk_plldiv_set_rate(struct clk_hw *hw, unsigned long rate,
  45. unsigned long parent_rate)
  46. {
  47. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  48. struct at91_pmc *pmc = plldiv->pmc;
  49. u32 tmp;
  50. if (parent_rate != rate && (parent_rate / 2) != rate)
  51. return -EINVAL;
  52. pmc_lock(pmc);
  53. tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_PLLADIV2;
  54. if ((parent_rate / 2) == rate)
  55. tmp |= AT91_PMC_PLLADIV2;
  56. pmc_write(pmc, AT91_PMC_MCKR, tmp);
  57. pmc_unlock(pmc);
  58. return 0;
  59. }
  60. static const struct clk_ops plldiv_ops = {
  61. .recalc_rate = clk_plldiv_recalc_rate,
  62. .round_rate = clk_plldiv_round_rate,
  63. .set_rate = clk_plldiv_set_rate,
  64. };
  65. static struct clk * __init
  66. at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  67. const char *parent_name)
  68. {
  69. struct clk_plldiv *plldiv;
  70. struct clk *clk = NULL;
  71. struct clk_init_data init;
  72. plldiv = kzalloc(sizeof(*plldiv), GFP_KERNEL);
  73. if (!plldiv)
  74. return ERR_PTR(-ENOMEM);
  75. init.name = name;
  76. init.ops = &plldiv_ops;
  77. init.parent_names = parent_name ? &parent_name : NULL;
  78. init.num_parents = parent_name ? 1 : 0;
  79. init.flags = CLK_SET_RATE_GATE;
  80. plldiv->hw.init = &init;
  81. plldiv->pmc = pmc;
  82. clk = clk_register(NULL, &plldiv->hw);
  83. if (IS_ERR(clk))
  84. kfree(plldiv);
  85. return clk;
  86. }
  87. static void __init
  88. of_at91_clk_plldiv_setup(struct device_node *np, struct at91_pmc *pmc)
  89. {
  90. struct clk *clk;
  91. const char *parent_name;
  92. const char *name = np->name;
  93. parent_name = of_clk_get_parent_name(np, 0);
  94. of_property_read_string(np, "clock-output-names", &name);
  95. clk = at91_clk_register_plldiv(pmc, name, parent_name);
  96. if (IS_ERR(clk))
  97. return;
  98. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  99. return;
  100. }
  101. void __init of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  102. struct at91_pmc *pmc)
  103. {
  104. of_at91_clk_plldiv_setup(np, pmc);
  105. }