autoidle.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * TI clock autoidle support
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * Tero Kristo <t-kristo@ti.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 version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/clk/ti.h>
  23. struct clk_ti_autoidle {
  24. void __iomem *reg;
  25. u8 shift;
  26. u8 flags;
  27. const char *name;
  28. struct list_head node;
  29. };
  30. #define AUTOIDLE_LOW 0x1
  31. static LIST_HEAD(autoidle_clks);
  32. static void ti_allow_autoidle(struct clk_ti_autoidle *clk)
  33. {
  34. u32 val;
  35. val = ti_clk_ll_ops->clk_readl(clk->reg);
  36. if (clk->flags & AUTOIDLE_LOW)
  37. val &= ~(1 << clk->shift);
  38. else
  39. val |= (1 << clk->shift);
  40. ti_clk_ll_ops->clk_writel(val, clk->reg);
  41. }
  42. static void ti_deny_autoidle(struct clk_ti_autoidle *clk)
  43. {
  44. u32 val;
  45. val = ti_clk_ll_ops->clk_readl(clk->reg);
  46. if (clk->flags & AUTOIDLE_LOW)
  47. val |= (1 << clk->shift);
  48. else
  49. val &= ~(1 << clk->shift);
  50. ti_clk_ll_ops->clk_writel(val, clk->reg);
  51. }
  52. /**
  53. * of_ti_clk_allow_autoidle_all - enable autoidle for all clocks
  54. *
  55. * Enables hardware autoidle for all registered DT clocks, which have
  56. * the feature.
  57. */
  58. void of_ti_clk_allow_autoidle_all(void)
  59. {
  60. struct clk_ti_autoidle *c;
  61. list_for_each_entry(c, &autoidle_clks, node)
  62. ti_allow_autoidle(c);
  63. }
  64. /**
  65. * of_ti_clk_deny_autoidle_all - disable autoidle for all clocks
  66. *
  67. * Disables hardware autoidle for all registered DT clocks, which have
  68. * the feature.
  69. */
  70. void of_ti_clk_deny_autoidle_all(void)
  71. {
  72. struct clk_ti_autoidle *c;
  73. list_for_each_entry(c, &autoidle_clks, node)
  74. ti_deny_autoidle(c);
  75. }
  76. /**
  77. * of_ti_clk_autoidle_setup - sets up hardware autoidle for a clock
  78. * @node: pointer to the clock device node
  79. *
  80. * Checks if a clock has hardware autoidle support or not (check
  81. * for presence of 'ti,autoidle-shift' property in the device tree
  82. * node) and sets up the hardware autoidle feature for the clock
  83. * if available. If autoidle is available, the clock is also added
  84. * to the autoidle list for later processing. Returns 0 on success,
  85. * negative error value on failure.
  86. */
  87. int __init of_ti_clk_autoidle_setup(struct device_node *node)
  88. {
  89. u32 shift;
  90. struct clk_ti_autoidle *clk;
  91. /* Check if this clock has autoidle support or not */
  92. if (of_property_read_u32(node, "ti,autoidle-shift", &shift))
  93. return 0;
  94. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  95. if (!clk)
  96. return -ENOMEM;
  97. clk->shift = shift;
  98. clk->name = node->name;
  99. clk->reg = ti_clk_get_reg_addr(node, 0);
  100. if (!clk->reg) {
  101. kfree(clk);
  102. return -EINVAL;
  103. }
  104. if (of_property_read_bool(node, "ti,invert-autoidle-bit"))
  105. clk->flags |= AUTOIDLE_LOW;
  106. list_add(&clk->node, &autoidle_clks);
  107. return 0;
  108. }