sun8i_tcon_top.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (c) 2018 Jernej Skrabec <jernej.skrabec@siol.net> */
  3. #include <drm/drmP.h>
  4. #include <dt-bindings/clock/sun8i-tcon-top.h>
  5. #include <linux/bitfield.h>
  6. #include <linux/component.h>
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include <linux/of_graph.h>
  10. #include <linux/platform_device.h>
  11. #include "sun8i_tcon_top.h"
  12. static bool sun8i_tcon_top_node_is_tcon_top(struct device_node *node)
  13. {
  14. return !!of_match_node(sun8i_tcon_top_of_table, node);
  15. }
  16. int sun8i_tcon_top_set_hdmi_src(struct device *dev, int tcon)
  17. {
  18. struct sun8i_tcon_top *tcon_top = dev_get_drvdata(dev);
  19. unsigned long flags;
  20. u32 val;
  21. if (!sun8i_tcon_top_node_is_tcon_top(dev->of_node)) {
  22. dev_err(dev, "Device is not TCON TOP!\n");
  23. return -EINVAL;
  24. }
  25. if (tcon < 2 || tcon > 3) {
  26. dev_err(dev, "TCON index must be 2 or 3!\n");
  27. return -EINVAL;
  28. }
  29. spin_lock_irqsave(&tcon_top->reg_lock, flags);
  30. val = readl(tcon_top->regs + TCON_TOP_GATE_SRC_REG);
  31. val &= ~TCON_TOP_HDMI_SRC_MSK;
  32. val |= FIELD_PREP(TCON_TOP_HDMI_SRC_MSK, tcon - 1);
  33. writel(val, tcon_top->regs + TCON_TOP_GATE_SRC_REG);
  34. spin_unlock_irqrestore(&tcon_top->reg_lock, flags);
  35. return 0;
  36. }
  37. EXPORT_SYMBOL(sun8i_tcon_top_set_hdmi_src);
  38. int sun8i_tcon_top_de_config(struct device *dev, int mixer, int tcon)
  39. {
  40. struct sun8i_tcon_top *tcon_top = dev_get_drvdata(dev);
  41. unsigned long flags;
  42. u32 reg;
  43. if (!sun8i_tcon_top_node_is_tcon_top(dev->of_node)) {
  44. dev_err(dev, "Device is not TCON TOP!\n");
  45. return -EINVAL;
  46. }
  47. if (mixer > 1) {
  48. dev_err(dev, "Mixer index is too high!\n");
  49. return -EINVAL;
  50. }
  51. if (tcon > 3) {
  52. dev_err(dev, "TCON index is too high!\n");
  53. return -EINVAL;
  54. }
  55. spin_lock_irqsave(&tcon_top->reg_lock, flags);
  56. reg = readl(tcon_top->regs + TCON_TOP_PORT_SEL_REG);
  57. if (mixer == 0) {
  58. reg &= ~TCON_TOP_PORT_DE0_MSK;
  59. reg |= FIELD_PREP(TCON_TOP_PORT_DE0_MSK, tcon);
  60. } else {
  61. reg &= ~TCON_TOP_PORT_DE1_MSK;
  62. reg |= FIELD_PREP(TCON_TOP_PORT_DE1_MSK, tcon);
  63. }
  64. writel(reg, tcon_top->regs + TCON_TOP_PORT_SEL_REG);
  65. spin_unlock_irqrestore(&tcon_top->reg_lock, flags);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(sun8i_tcon_top_de_config);
  69. static struct clk_hw *sun8i_tcon_top_register_gate(struct device *dev,
  70. const char *parent,
  71. void __iomem *regs,
  72. spinlock_t *lock,
  73. u8 bit, int name_index)
  74. {
  75. const char *clk_name, *parent_name;
  76. int ret, index;
  77. index = of_property_match_string(dev->of_node, "clock-names", parent);
  78. if (index < 0)
  79. return ERR_PTR(index);
  80. parent_name = of_clk_get_parent_name(dev->of_node, index);
  81. ret = of_property_read_string_index(dev->of_node,
  82. "clock-output-names", name_index,
  83. &clk_name);
  84. if (ret)
  85. return ERR_PTR(ret);
  86. return clk_hw_register_gate(dev, clk_name, parent_name,
  87. CLK_SET_RATE_PARENT,
  88. regs + TCON_TOP_GATE_SRC_REG,
  89. bit, 0, lock);
  90. };
  91. static int sun8i_tcon_top_bind(struct device *dev, struct device *master,
  92. void *data)
  93. {
  94. struct platform_device *pdev = to_platform_device(dev);
  95. struct clk_hw_onecell_data *clk_data;
  96. struct sun8i_tcon_top *tcon_top;
  97. struct resource *res;
  98. void __iomem *regs;
  99. int ret, i;
  100. tcon_top = devm_kzalloc(dev, sizeof(*tcon_top), GFP_KERNEL);
  101. if (!tcon_top)
  102. return -ENOMEM;
  103. clk_data = devm_kzalloc(dev, sizeof(*clk_data) +
  104. sizeof(*clk_data->hws) * CLK_NUM,
  105. GFP_KERNEL);
  106. if (!clk_data)
  107. return -ENOMEM;
  108. tcon_top->clk_data = clk_data;
  109. spin_lock_init(&tcon_top->reg_lock);
  110. tcon_top->rst = devm_reset_control_get(dev, NULL);
  111. if (IS_ERR(tcon_top->rst)) {
  112. dev_err(dev, "Couldn't get our reset line\n");
  113. return PTR_ERR(tcon_top->rst);
  114. }
  115. tcon_top->bus = devm_clk_get(dev, "bus");
  116. if (IS_ERR(tcon_top->bus)) {
  117. dev_err(dev, "Couldn't get the bus clock\n");
  118. return PTR_ERR(tcon_top->bus);
  119. }
  120. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  121. regs = devm_ioremap_resource(dev, res);
  122. tcon_top->regs = regs;
  123. if (IS_ERR(regs))
  124. return PTR_ERR(regs);
  125. ret = reset_control_deassert(tcon_top->rst);
  126. if (ret) {
  127. dev_err(dev, "Could not deassert ctrl reset control\n");
  128. return ret;
  129. }
  130. ret = clk_prepare_enable(tcon_top->bus);
  131. if (ret) {
  132. dev_err(dev, "Could not enable bus clock\n");
  133. goto err_assert_reset;
  134. }
  135. /*
  136. * At least on H6, some registers have some bits set by default
  137. * which may cause issues. Clear them here.
  138. */
  139. writel(0, regs + TCON_TOP_PORT_SEL_REG);
  140. writel(0, regs + TCON_TOP_GATE_SRC_REG);
  141. /*
  142. * TCON TOP has two muxes, which select parent clock for each TCON TV
  143. * channel clock. Parent could be either TCON TV or TVE clock. For now
  144. * we leave this fixed to TCON TV, since TVE driver for R40 is not yet
  145. * implemented. Once it is, graph needs to be traversed to determine
  146. * if TVE is active on each TCON TV. If it is, mux should be switched
  147. * to TVE clock parent.
  148. */
  149. clk_data->hws[CLK_TCON_TOP_TV0] =
  150. sun8i_tcon_top_register_gate(dev, "tcon-tv0", regs,
  151. &tcon_top->reg_lock,
  152. TCON_TOP_TCON_TV0_GATE, 0);
  153. clk_data->hws[CLK_TCON_TOP_TV1] =
  154. sun8i_tcon_top_register_gate(dev, "tcon-tv1", regs,
  155. &tcon_top->reg_lock,
  156. TCON_TOP_TCON_TV1_GATE, 1);
  157. clk_data->hws[CLK_TCON_TOP_DSI] =
  158. sun8i_tcon_top_register_gate(dev, "dsi", regs,
  159. &tcon_top->reg_lock,
  160. TCON_TOP_TCON_DSI_GATE, 2);
  161. for (i = 0; i < CLK_NUM; i++)
  162. if (IS_ERR(clk_data->hws[i])) {
  163. ret = PTR_ERR(clk_data->hws[i]);
  164. goto err_unregister_gates;
  165. }
  166. clk_data->num = CLK_NUM;
  167. ret = of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
  168. clk_data);
  169. if (ret)
  170. goto err_unregister_gates;
  171. dev_set_drvdata(dev, tcon_top);
  172. return 0;
  173. err_unregister_gates:
  174. for (i = 0; i < CLK_NUM; i++)
  175. if (!IS_ERR_OR_NULL(clk_data->hws[i]))
  176. clk_hw_unregister_gate(clk_data->hws[i]);
  177. clk_disable_unprepare(tcon_top->bus);
  178. err_assert_reset:
  179. reset_control_assert(tcon_top->rst);
  180. return ret;
  181. }
  182. static void sun8i_tcon_top_unbind(struct device *dev, struct device *master,
  183. void *data)
  184. {
  185. struct sun8i_tcon_top *tcon_top = dev_get_drvdata(dev);
  186. struct clk_hw_onecell_data *clk_data = tcon_top->clk_data;
  187. int i;
  188. of_clk_del_provider(dev->of_node);
  189. for (i = 0; i < CLK_NUM; i++)
  190. if (clk_data->hws[i])
  191. clk_hw_unregister_gate(clk_data->hws[i]);
  192. clk_disable_unprepare(tcon_top->bus);
  193. reset_control_assert(tcon_top->rst);
  194. }
  195. static const struct component_ops sun8i_tcon_top_ops = {
  196. .bind = sun8i_tcon_top_bind,
  197. .unbind = sun8i_tcon_top_unbind,
  198. };
  199. static int sun8i_tcon_top_probe(struct platform_device *pdev)
  200. {
  201. return component_add(&pdev->dev, &sun8i_tcon_top_ops);
  202. }
  203. static int sun8i_tcon_top_remove(struct platform_device *pdev)
  204. {
  205. component_del(&pdev->dev, &sun8i_tcon_top_ops);
  206. return 0;
  207. }
  208. /* sun4i_drv uses this list to check if a device node is a TCON TOP */
  209. const struct of_device_id sun8i_tcon_top_of_table[] = {
  210. { /* sentinel */ }
  211. };
  212. MODULE_DEVICE_TABLE(of, sun8i_tcon_top_of_table);
  213. EXPORT_SYMBOL(sun8i_tcon_top_of_table);
  214. static struct platform_driver sun8i_tcon_top_platform_driver = {
  215. .probe = sun8i_tcon_top_probe,
  216. .remove = sun8i_tcon_top_remove,
  217. .driver = {
  218. .name = "sun8i-tcon-top",
  219. .of_match_table = sun8i_tcon_top_of_table,
  220. },
  221. };
  222. module_platform_driver(sun8i_tcon_top_platform_driver);
  223. MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
  224. MODULE_DESCRIPTION("Allwinner R40 TCON TOP driver");
  225. MODULE_LICENSE("GPL");