sun8i_tcon_top.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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, struct_size(clk_data, hws, CLK_NUM),
  104. GFP_KERNEL);
  105. if (!clk_data)
  106. return -ENOMEM;
  107. tcon_top->clk_data = clk_data;
  108. spin_lock_init(&tcon_top->reg_lock);
  109. tcon_top->rst = devm_reset_control_get(dev, NULL);
  110. if (IS_ERR(tcon_top->rst)) {
  111. dev_err(dev, "Couldn't get our reset line\n");
  112. return PTR_ERR(tcon_top->rst);
  113. }
  114. tcon_top->bus = devm_clk_get(dev, "bus");
  115. if (IS_ERR(tcon_top->bus)) {
  116. dev_err(dev, "Couldn't get the bus clock\n");
  117. return PTR_ERR(tcon_top->bus);
  118. }
  119. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  120. regs = devm_ioremap_resource(dev, res);
  121. tcon_top->regs = regs;
  122. if (IS_ERR(regs))
  123. return PTR_ERR(regs);
  124. ret = reset_control_deassert(tcon_top->rst);
  125. if (ret) {
  126. dev_err(dev, "Could not deassert ctrl reset control\n");
  127. return ret;
  128. }
  129. ret = clk_prepare_enable(tcon_top->bus);
  130. if (ret) {
  131. dev_err(dev, "Could not enable bus clock\n");
  132. goto err_assert_reset;
  133. }
  134. /*
  135. * TCON TOP has two muxes, which select parent clock for each TCON TV
  136. * channel clock. Parent could be either TCON TV or TVE clock. For now
  137. * we leave this fixed to TCON TV, since TVE driver for R40 is not yet
  138. * implemented. Once it is, graph needs to be traversed to determine
  139. * if TVE is active on each TCON TV. If it is, mux should be switched
  140. * to TVE clock parent.
  141. */
  142. clk_data->hws[CLK_TCON_TOP_TV0] =
  143. sun8i_tcon_top_register_gate(dev, "tcon-tv0", regs,
  144. &tcon_top->reg_lock,
  145. TCON_TOP_TCON_TV0_GATE, 0);
  146. clk_data->hws[CLK_TCON_TOP_TV1] =
  147. sun8i_tcon_top_register_gate(dev, "tcon-tv1", regs,
  148. &tcon_top->reg_lock,
  149. TCON_TOP_TCON_TV1_GATE, 1);
  150. clk_data->hws[CLK_TCON_TOP_DSI] =
  151. sun8i_tcon_top_register_gate(dev, "dsi", regs,
  152. &tcon_top->reg_lock,
  153. TCON_TOP_TCON_DSI_GATE, 2);
  154. for (i = 0; i < CLK_NUM; i++)
  155. if (IS_ERR(clk_data->hws[i])) {
  156. ret = PTR_ERR(clk_data->hws[i]);
  157. goto err_unregister_gates;
  158. }
  159. clk_data->num = CLK_NUM;
  160. ret = of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
  161. clk_data);
  162. if (ret)
  163. goto err_unregister_gates;
  164. dev_set_drvdata(dev, tcon_top);
  165. return 0;
  166. err_unregister_gates:
  167. for (i = 0; i < CLK_NUM; i++)
  168. if (clk_data->hws[i])
  169. clk_hw_unregister_gate(clk_data->hws[i]);
  170. clk_disable_unprepare(tcon_top->bus);
  171. err_assert_reset:
  172. reset_control_assert(tcon_top->rst);
  173. return ret;
  174. }
  175. static void sun8i_tcon_top_unbind(struct device *dev, struct device *master,
  176. void *data)
  177. {
  178. struct sun8i_tcon_top *tcon_top = dev_get_drvdata(dev);
  179. struct clk_hw_onecell_data *clk_data = tcon_top->clk_data;
  180. int i;
  181. of_clk_del_provider(dev->of_node);
  182. for (i = 0; i < CLK_NUM; i++)
  183. clk_hw_unregister_gate(clk_data->hws[i]);
  184. clk_disable_unprepare(tcon_top->bus);
  185. reset_control_assert(tcon_top->rst);
  186. }
  187. static const struct component_ops sun8i_tcon_top_ops = {
  188. .bind = sun8i_tcon_top_bind,
  189. .unbind = sun8i_tcon_top_unbind,
  190. };
  191. static int sun8i_tcon_top_probe(struct platform_device *pdev)
  192. {
  193. return component_add(&pdev->dev, &sun8i_tcon_top_ops);
  194. }
  195. static int sun8i_tcon_top_remove(struct platform_device *pdev)
  196. {
  197. component_del(&pdev->dev, &sun8i_tcon_top_ops);
  198. return 0;
  199. }
  200. /* sun4i_drv uses this list to check if a device node is a TCON TOP */
  201. const struct of_device_id sun8i_tcon_top_of_table[] = {
  202. { .compatible = "allwinner,sun8i-r40-tcon-top" },
  203. { /* sentinel */ }
  204. };
  205. MODULE_DEVICE_TABLE(of, sun8i_tcon_top_of_table);
  206. EXPORT_SYMBOL(sun8i_tcon_top_of_table);
  207. static struct platform_driver sun8i_tcon_top_platform_driver = {
  208. .probe = sun8i_tcon_top_probe,
  209. .remove = sun8i_tcon_top_remove,
  210. .driver = {
  211. .name = "sun8i-tcon-top",
  212. .of_match_table = sun8i_tcon_top_of_table,
  213. },
  214. };
  215. module_platform_driver(sun8i_tcon_top_platform_driver);
  216. MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
  217. MODULE_DESCRIPTION("Allwinner R40 TCON TOP driver");
  218. MODULE_LICENSE("GPL");