clk-system.c 3.1 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 SYSTEM_MAX_ID 31
  18. #define SYSTEM_MAX_NAME_SZ 32
  19. #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
  20. struct clk_system {
  21. struct clk_hw hw;
  22. struct at91_pmc *pmc;
  23. u8 id;
  24. };
  25. static int clk_system_enable(struct clk_hw *hw)
  26. {
  27. struct clk_system *sys = to_clk_system(hw);
  28. struct at91_pmc *pmc = sys->pmc;
  29. pmc_write(pmc, AT91_PMC_SCER, 1 << sys->id);
  30. return 0;
  31. }
  32. static void clk_system_disable(struct clk_hw *hw)
  33. {
  34. struct clk_system *sys = to_clk_system(hw);
  35. struct at91_pmc *pmc = sys->pmc;
  36. pmc_write(pmc, AT91_PMC_SCDR, 1 << sys->id);
  37. }
  38. static int clk_system_is_enabled(struct clk_hw *hw)
  39. {
  40. struct clk_system *sys = to_clk_system(hw);
  41. struct at91_pmc *pmc = sys->pmc;
  42. return !!(pmc_read(pmc, AT91_PMC_SCSR) & (1 << sys->id));
  43. }
  44. static const struct clk_ops system_ops = {
  45. .enable = clk_system_enable,
  46. .disable = clk_system_disable,
  47. .is_enabled = clk_system_is_enabled,
  48. };
  49. static struct clk * __init
  50. at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  51. const char *parent_name, u8 id)
  52. {
  53. struct clk_system *sys;
  54. struct clk *clk = NULL;
  55. struct clk_init_data init;
  56. if (!parent_name || id > SYSTEM_MAX_ID)
  57. return ERR_PTR(-EINVAL);
  58. sys = kzalloc(sizeof(*sys), GFP_KERNEL);
  59. if (!sys)
  60. return ERR_PTR(-ENOMEM);
  61. init.name = name;
  62. init.ops = &system_ops;
  63. init.parent_names = &parent_name;
  64. init.num_parents = 1;
  65. /*
  66. * CLK_IGNORE_UNUSED is used to avoid ddrck switch off.
  67. * TODO : we should implement a driver supporting at91 ddr controller
  68. * (see drivers/memory) which would request and enable the ddrck clock.
  69. * When this is done we will be able to remove CLK_IGNORE_UNUSED flag.
  70. */
  71. init.flags = CLK_IGNORE_UNUSED;
  72. sys->id = id;
  73. sys->hw.init = &init;
  74. sys->pmc = pmc;
  75. clk = clk_register(NULL, &sys->hw);
  76. if (IS_ERR(clk))
  77. kfree(sys);
  78. return clk;
  79. }
  80. static void __init
  81. of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  82. {
  83. int num;
  84. u32 id;
  85. struct clk *clk;
  86. const char *name;
  87. struct device_node *sysclknp;
  88. const char *parent_name;
  89. num = of_get_child_count(np);
  90. if (num > (SYSTEM_MAX_ID + 1))
  91. return;
  92. for_each_child_of_node(np, sysclknp) {
  93. if (of_property_read_u32(sysclknp, "reg", &id))
  94. continue;
  95. if (of_property_read_string(np, "clock-output-names", &name))
  96. name = sysclknp->name;
  97. parent_name = of_clk_get_parent_name(sysclknp, 0);
  98. clk = at91_clk_register_system(pmc, name, parent_name, id);
  99. if (IS_ERR(clk))
  100. continue;
  101. of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
  102. }
  103. }
  104. void __init of_at91rm9200_clk_sys_setup(struct device_node *np,
  105. struct at91_pmc *pmc)
  106. {
  107. of_at91_clk_sys_setup(np, pmc);
  108. }