clk-sun4i-display.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2015 Maxime Ripard
  3. *
  4. * Maxime Ripard <maxime.ripard@free-electrons.com>
  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/kernel.h>
  18. #include <linux/of_address.h>
  19. #include <linux/reset-controller.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. struct sun4i_a10_display_clk_data {
  23. bool has_div;
  24. u8 num_rst;
  25. u8 parents;
  26. u8 offset_en;
  27. u8 offset_div;
  28. u8 offset_mux;
  29. u8 offset_rst;
  30. u8 width_div;
  31. u8 width_mux;
  32. u32 flags;
  33. };
  34. struct reset_data {
  35. void __iomem *reg;
  36. spinlock_t *lock;
  37. struct reset_controller_dev rcdev;
  38. u8 offset;
  39. };
  40. static DEFINE_SPINLOCK(sun4i_a10_display_lock);
  41. static inline struct reset_data *rcdev_to_reset_data(struct reset_controller_dev *rcdev)
  42. {
  43. return container_of(rcdev, struct reset_data, rcdev);
  44. };
  45. static int sun4i_a10_display_assert(struct reset_controller_dev *rcdev,
  46. unsigned long id)
  47. {
  48. struct reset_data *data = rcdev_to_reset_data(rcdev);
  49. unsigned long flags;
  50. u32 reg;
  51. spin_lock_irqsave(data->lock, flags);
  52. reg = readl(data->reg);
  53. writel(reg & ~BIT(data->offset + id), data->reg);
  54. spin_unlock_irqrestore(data->lock, flags);
  55. return 0;
  56. }
  57. static int sun4i_a10_display_deassert(struct reset_controller_dev *rcdev,
  58. unsigned long id)
  59. {
  60. struct reset_data *data = rcdev_to_reset_data(rcdev);
  61. unsigned long flags;
  62. u32 reg;
  63. spin_lock_irqsave(data->lock, flags);
  64. reg = readl(data->reg);
  65. writel(reg | BIT(data->offset + id), data->reg);
  66. spin_unlock_irqrestore(data->lock, flags);
  67. return 0;
  68. }
  69. static int sun4i_a10_display_status(struct reset_controller_dev *rcdev,
  70. unsigned long id)
  71. {
  72. struct reset_data *data = rcdev_to_reset_data(rcdev);
  73. return !(readl(data->reg) & BIT(data->offset + id));
  74. }
  75. static const struct reset_control_ops sun4i_a10_display_reset_ops = {
  76. .assert = sun4i_a10_display_assert,
  77. .deassert = sun4i_a10_display_deassert,
  78. .status = sun4i_a10_display_status,
  79. };
  80. static int sun4i_a10_display_reset_xlate(struct reset_controller_dev *rcdev,
  81. const struct of_phandle_args *spec)
  82. {
  83. /* We only have a single reset signal */
  84. return 0;
  85. }
  86. static void __init sun4i_a10_display_init(struct device_node *node,
  87. const struct sun4i_a10_display_clk_data *data)
  88. {
  89. const char *parents[4];
  90. const char *clk_name = node->name;
  91. struct reset_data *reset_data;
  92. struct clk_divider *div = NULL;
  93. struct clk_gate *gate;
  94. struct resource res;
  95. struct clk_mux *mux;
  96. void __iomem *reg;
  97. struct clk *clk;
  98. int ret;
  99. of_property_read_string(node, "clock-output-names", &clk_name);
  100. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  101. if (IS_ERR(reg)) {
  102. pr_err("%s: Could not map the clock registers\n", clk_name);
  103. return;
  104. }
  105. ret = of_clk_parent_fill(node, parents, data->parents);
  106. if (ret != data->parents) {
  107. pr_err("%s: Could not retrieve the parents\n", clk_name);
  108. goto unmap;
  109. }
  110. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  111. if (!mux)
  112. goto unmap;
  113. mux->reg = reg;
  114. mux->shift = data->offset_mux;
  115. mux->mask = (1 << data->width_mux) - 1;
  116. mux->lock = &sun4i_a10_display_lock;
  117. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  118. if (!gate)
  119. goto free_mux;
  120. gate->reg = reg;
  121. gate->bit_idx = data->offset_en;
  122. gate->lock = &sun4i_a10_display_lock;
  123. if (data->has_div) {
  124. div = kzalloc(sizeof(*div), GFP_KERNEL);
  125. if (!div)
  126. goto free_gate;
  127. div->reg = reg;
  128. div->shift = data->offset_div;
  129. div->width = data->width_div;
  130. div->lock = &sun4i_a10_display_lock;
  131. }
  132. clk = clk_register_composite(NULL, clk_name,
  133. parents, data->parents,
  134. &mux->hw, &clk_mux_ops,
  135. data->has_div ? &div->hw : NULL,
  136. data->has_div ? &clk_divider_ops : NULL,
  137. &gate->hw, &clk_gate_ops,
  138. data->flags);
  139. if (IS_ERR(clk)) {
  140. pr_err("%s: Couldn't register the clock\n", clk_name);
  141. goto free_div;
  142. }
  143. ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  144. if (ret) {
  145. pr_err("%s: Couldn't register DT provider\n", clk_name);
  146. goto free_clk;
  147. }
  148. if (!data->num_rst)
  149. return;
  150. reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
  151. if (!reset_data)
  152. goto free_of_clk;
  153. reset_data->reg = reg;
  154. reset_data->offset = data->offset_rst;
  155. reset_data->lock = &sun4i_a10_display_lock;
  156. reset_data->rcdev.nr_resets = data->num_rst;
  157. reset_data->rcdev.ops = &sun4i_a10_display_reset_ops;
  158. reset_data->rcdev.of_node = node;
  159. if (data->num_rst == 1) {
  160. reset_data->rcdev.of_reset_n_cells = 0;
  161. reset_data->rcdev.of_xlate = &sun4i_a10_display_reset_xlate;
  162. } else {
  163. reset_data->rcdev.of_reset_n_cells = 1;
  164. }
  165. if (reset_controller_register(&reset_data->rcdev)) {
  166. pr_err("%s: Couldn't register the reset controller\n",
  167. clk_name);
  168. goto free_reset;
  169. }
  170. return;
  171. free_reset:
  172. kfree(reset_data);
  173. free_of_clk:
  174. of_clk_del_provider(node);
  175. free_clk:
  176. clk_unregister_composite(clk);
  177. free_div:
  178. kfree(div);
  179. free_gate:
  180. kfree(gate);
  181. free_mux:
  182. kfree(mux);
  183. unmap:
  184. iounmap(reg);
  185. of_address_to_resource(node, 0, &res);
  186. release_mem_region(res.start, resource_size(&res));
  187. }
  188. static const struct sun4i_a10_display_clk_data sun4i_a10_tcon_ch0_data __initconst = {
  189. .num_rst = 2,
  190. .parents = 4,
  191. .offset_en = 31,
  192. .offset_rst = 29,
  193. .offset_mux = 24,
  194. .width_mux = 2,
  195. .flags = CLK_SET_RATE_PARENT,
  196. };
  197. static void __init sun4i_a10_tcon_ch0_setup(struct device_node *node)
  198. {
  199. sun4i_a10_display_init(node, &sun4i_a10_tcon_ch0_data);
  200. }
  201. CLK_OF_DECLARE(sun4i_a10_tcon_ch0, "allwinner,sun4i-a10-tcon-ch0-clk",
  202. sun4i_a10_tcon_ch0_setup);
  203. static const struct sun4i_a10_display_clk_data sun4i_a10_display_data __initconst = {
  204. .has_div = true,
  205. .num_rst = 1,
  206. .parents = 3,
  207. .offset_en = 31,
  208. .offset_rst = 30,
  209. .offset_mux = 24,
  210. .offset_div = 0,
  211. .width_mux = 2,
  212. .width_div = 4,
  213. };
  214. static void __init sun4i_a10_display_setup(struct device_node *node)
  215. {
  216. sun4i_a10_display_init(node, &sun4i_a10_display_data);
  217. }
  218. CLK_OF_DECLARE(sun4i_a10_display, "allwinner,sun4i-a10-display-clk",
  219. sun4i_a10_display_setup);