clk-a10-ve.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2015 Chen-Yu Tsai
  3. *
  4. * Chen-Yu Tsai <wens@csie.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/reset-controller.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. static DEFINE_SPINLOCK(ve_lock);
  23. #define SUN4I_VE_ENABLE 31
  24. #define SUN4I_VE_DIVIDER_SHIFT 16
  25. #define SUN4I_VE_DIVIDER_WIDTH 3
  26. #define SUN4I_VE_RESET 0
  27. /**
  28. * sunxi_ve_reset... - reset bit in ve clk registers handling
  29. */
  30. struct ve_reset_data {
  31. void __iomem *reg;
  32. spinlock_t *lock;
  33. struct reset_controller_dev rcdev;
  34. };
  35. static int sunxi_ve_reset_assert(struct reset_controller_dev *rcdev,
  36. unsigned long id)
  37. {
  38. struct ve_reset_data *data = container_of(rcdev,
  39. struct ve_reset_data,
  40. rcdev);
  41. unsigned long flags;
  42. u32 reg;
  43. spin_lock_irqsave(data->lock, flags);
  44. reg = readl(data->reg);
  45. writel(reg & ~BIT(SUN4I_VE_RESET), data->reg);
  46. spin_unlock_irqrestore(data->lock, flags);
  47. return 0;
  48. }
  49. static int sunxi_ve_reset_deassert(struct reset_controller_dev *rcdev,
  50. unsigned long id)
  51. {
  52. struct ve_reset_data *data = container_of(rcdev,
  53. struct ve_reset_data,
  54. rcdev);
  55. unsigned long flags;
  56. u32 reg;
  57. spin_lock_irqsave(data->lock, flags);
  58. reg = readl(data->reg);
  59. writel(reg | BIT(SUN4I_VE_RESET), data->reg);
  60. spin_unlock_irqrestore(data->lock, flags);
  61. return 0;
  62. }
  63. static int sunxi_ve_of_xlate(struct reset_controller_dev *rcdev,
  64. const struct of_phandle_args *reset_spec)
  65. {
  66. if (WARN_ON(reset_spec->args_count != 0))
  67. return -EINVAL;
  68. return 0;
  69. }
  70. static const struct reset_control_ops sunxi_ve_reset_ops = {
  71. .assert = sunxi_ve_reset_assert,
  72. .deassert = sunxi_ve_reset_deassert,
  73. };
  74. static void __init sun4i_ve_clk_setup(struct device_node *node)
  75. {
  76. struct clk *clk;
  77. struct clk_divider *div;
  78. struct clk_gate *gate;
  79. struct ve_reset_data *reset_data;
  80. const char *parent;
  81. const char *clk_name = node->name;
  82. void __iomem *reg;
  83. int err;
  84. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  85. if (IS_ERR(reg))
  86. return;
  87. div = kzalloc(sizeof(*div), GFP_KERNEL);
  88. if (!div)
  89. goto err_unmap;
  90. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  91. if (!gate)
  92. goto err_free_div;
  93. of_property_read_string(node, "clock-output-names", &clk_name);
  94. parent = of_clk_get_parent_name(node, 0);
  95. gate->reg = reg;
  96. gate->bit_idx = SUN4I_VE_ENABLE;
  97. gate->lock = &ve_lock;
  98. div->reg = reg;
  99. div->shift = SUN4I_VE_DIVIDER_SHIFT;
  100. div->width = SUN4I_VE_DIVIDER_WIDTH;
  101. div->lock = &ve_lock;
  102. clk = clk_register_composite(NULL, clk_name, &parent, 1,
  103. NULL, NULL,
  104. &div->hw, &clk_divider_ops,
  105. &gate->hw, &clk_gate_ops,
  106. CLK_SET_RATE_PARENT);
  107. if (IS_ERR(clk))
  108. goto err_free_gate;
  109. err = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  110. if (err)
  111. goto err_unregister_clk;
  112. reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
  113. if (!reset_data)
  114. goto err_del_provider;
  115. reset_data->reg = reg;
  116. reset_data->lock = &ve_lock;
  117. reset_data->rcdev.nr_resets = 1;
  118. reset_data->rcdev.ops = &sunxi_ve_reset_ops;
  119. reset_data->rcdev.of_node = node;
  120. reset_data->rcdev.of_xlate = sunxi_ve_of_xlate;
  121. reset_data->rcdev.of_reset_n_cells = 0;
  122. err = reset_controller_register(&reset_data->rcdev);
  123. if (err)
  124. goto err_free_reset;
  125. return;
  126. err_free_reset:
  127. kfree(reset_data);
  128. err_del_provider:
  129. of_clk_del_provider(node);
  130. err_unregister_clk:
  131. clk_unregister(clk);
  132. err_free_gate:
  133. kfree(gate);
  134. err_free_div:
  135. kfree(div);
  136. err_unmap:
  137. iounmap(reg);
  138. }
  139. CLK_OF_DECLARE(sun4i_ve, "allwinner,sun4i-a10-ve-clk",
  140. sun4i_ve_clk_setup);