clk-h32mx.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * clk-h32mx.c
  3. *
  4. * Copyright (C) 2014 Atmel
  5. *
  6. * Alexandre Belloni <alexandre.belloni@free-electrons.com>
  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; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <linux/clk-provider.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/clk/at91_pmc.h>
  17. #include <linux/delay.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/io.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/sched.h>
  25. #include <linux/wait.h>
  26. #include "pmc.h"
  27. #define H32MX_MAX_FREQ 90000000
  28. struct clk_sama5d4_h32mx {
  29. struct clk_hw hw;
  30. struct at91_pmc *pmc;
  31. };
  32. #define to_clk_sama5d4_h32mx(hw) container_of(hw, struct clk_sama5d4_h32mx, hw)
  33. static unsigned long clk_sama5d4_h32mx_recalc_rate(struct clk_hw *hw,
  34. unsigned long parent_rate)
  35. {
  36. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  37. if (pmc_read(h32mxclk->pmc, AT91_PMC_MCKR) & AT91_PMC_H32MXDIV)
  38. return parent_rate / 2;
  39. if (parent_rate > H32MX_MAX_FREQ)
  40. pr_warn("H32MX clock is too fast\n");
  41. return parent_rate;
  42. }
  43. static long clk_sama5d4_h32mx_round_rate(struct clk_hw *hw, unsigned long rate,
  44. unsigned long *parent_rate)
  45. {
  46. unsigned long div;
  47. if (rate > *parent_rate)
  48. return *parent_rate;
  49. div = *parent_rate / 2;
  50. if (rate < div)
  51. return div;
  52. if (rate - div < *parent_rate - rate)
  53. return div;
  54. return *parent_rate;
  55. }
  56. static int clk_sama5d4_h32mx_set_rate(struct clk_hw *hw, unsigned long rate,
  57. unsigned long parent_rate)
  58. {
  59. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  60. struct at91_pmc *pmc = h32mxclk->pmc;
  61. u32 tmp;
  62. if (parent_rate != rate && (parent_rate / 2) != rate)
  63. return -EINVAL;
  64. pmc_lock(pmc);
  65. tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_H32MXDIV;
  66. if ((parent_rate / 2) == rate)
  67. tmp |= AT91_PMC_H32MXDIV;
  68. pmc_write(pmc, AT91_PMC_MCKR, tmp);
  69. pmc_unlock(pmc);
  70. return 0;
  71. }
  72. static const struct clk_ops h32mx_ops = {
  73. .recalc_rate = clk_sama5d4_h32mx_recalc_rate,
  74. .round_rate = clk_sama5d4_h32mx_round_rate,
  75. .set_rate = clk_sama5d4_h32mx_set_rate,
  76. };
  77. void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  78. struct at91_pmc *pmc)
  79. {
  80. struct clk_sama5d4_h32mx *h32mxclk;
  81. struct clk_init_data init;
  82. const char *parent_name;
  83. struct clk *clk;
  84. h32mxclk = kzalloc(sizeof(*h32mxclk), GFP_KERNEL);
  85. if (!h32mxclk)
  86. return;
  87. parent_name = of_clk_get_parent_name(np, 0);
  88. init.name = np->name;
  89. init.ops = &h32mx_ops;
  90. init.parent_names = parent_name ? &parent_name : NULL;
  91. init.num_parents = parent_name ? 1 : 0;
  92. init.flags = CLK_SET_RATE_GATE;
  93. h32mxclk->hw.init = &init;
  94. h32mxclk->pmc = pmc;
  95. clk = clk_register(NULL, &h32mxclk->hw);
  96. if (IS_ERR(clk)) {
  97. kfree(h32mxclk);
  98. return;
  99. }
  100. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  101. }