clk-utmi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/mfd/syscon.h>
  15. #include <linux/regmap.h>
  16. #include "pmc.h"
  17. #define UTMI_FIXED_MUL 40
  18. struct clk_utmi {
  19. struct clk_hw hw;
  20. struct regmap *regmap;
  21. };
  22. #define to_clk_utmi(hw) container_of(hw, struct clk_utmi, hw)
  23. static inline bool clk_utmi_ready(struct regmap *regmap)
  24. {
  25. unsigned int status;
  26. regmap_read(regmap, AT91_PMC_SR, &status);
  27. return status & AT91_PMC_LOCKU;
  28. }
  29. static int clk_utmi_prepare(struct clk_hw *hw)
  30. {
  31. struct clk_utmi *utmi = to_clk_utmi(hw);
  32. unsigned int uckr = AT91_PMC_UPLLEN | AT91_PMC_UPLLCOUNT |
  33. AT91_PMC_BIASEN;
  34. regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, uckr, uckr);
  35. while (!clk_utmi_ready(utmi->regmap))
  36. cpu_relax();
  37. return 0;
  38. }
  39. static int clk_utmi_is_prepared(struct clk_hw *hw)
  40. {
  41. struct clk_utmi *utmi = to_clk_utmi(hw);
  42. return clk_utmi_ready(utmi->regmap);
  43. }
  44. static void clk_utmi_unprepare(struct clk_hw *hw)
  45. {
  46. struct clk_utmi *utmi = to_clk_utmi(hw);
  47. regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, AT91_PMC_UPLLEN, 0);
  48. }
  49. static unsigned long clk_utmi_recalc_rate(struct clk_hw *hw,
  50. unsigned long parent_rate)
  51. {
  52. /* UTMI clk is a fixed clk multiplier */
  53. return parent_rate * UTMI_FIXED_MUL;
  54. }
  55. static const struct clk_ops utmi_ops = {
  56. .prepare = clk_utmi_prepare,
  57. .unprepare = clk_utmi_unprepare,
  58. .is_prepared = clk_utmi_is_prepared,
  59. .recalc_rate = clk_utmi_recalc_rate,
  60. };
  61. static struct clk * __init
  62. at91_clk_register_utmi(struct regmap *regmap,
  63. const char *name, const char *parent_name)
  64. {
  65. struct clk_utmi *utmi;
  66. struct clk *clk = NULL;
  67. struct clk_init_data init;
  68. utmi = kzalloc(sizeof(*utmi), GFP_KERNEL);
  69. if (!utmi)
  70. return ERR_PTR(-ENOMEM);
  71. init.name = name;
  72. init.ops = &utmi_ops;
  73. init.parent_names = parent_name ? &parent_name : NULL;
  74. init.num_parents = parent_name ? 1 : 0;
  75. init.flags = CLK_SET_RATE_GATE;
  76. utmi->hw.init = &init;
  77. utmi->regmap = regmap;
  78. clk = clk_register(NULL, &utmi->hw);
  79. if (IS_ERR(clk))
  80. kfree(utmi);
  81. return clk;
  82. }
  83. static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
  84. {
  85. struct clk *clk;
  86. const char *parent_name;
  87. const char *name = np->name;
  88. struct regmap *regmap;
  89. parent_name = of_clk_get_parent_name(np, 0);
  90. of_property_read_string(np, "clock-output-names", &name);
  91. regmap = syscon_node_to_regmap(of_get_parent(np));
  92. if (IS_ERR(regmap))
  93. return;
  94. clk = at91_clk_register_utmi(regmap, name, parent_name);
  95. if (IS_ERR(clk))
  96. return;
  97. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  98. return;
  99. }
  100. CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
  101. of_at91sam9x5_clk_utmi_setup);