1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5 */
6
7 #include <linux/clk.h>
8 #include <linux/device.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 #include <soc/mediatek/smi.h>
13
14 #include "mtk_mdp_comp.h"
15
16
mtk_mdp_comp_clock_on(struct device * dev,struct mtk_mdp_comp * comp)17 void mtk_mdp_comp_clock_on(struct device *dev, struct mtk_mdp_comp *comp)
18 {
19 int i, err;
20
21 if (comp->larb_dev) {
22 err = mtk_smi_larb_get(comp->larb_dev);
23 if (err)
24 dev_err(dev,
25 "failed to get larb, err %d. type:%d\n",
26 err, comp->type);
27 }
28
29 for (i = 0; i < ARRAY_SIZE(comp->clk); i++) {
30 if (IS_ERR(comp->clk[i]))
31 continue;
32 err = clk_prepare_enable(comp->clk[i]);
33 if (err)
34 dev_err(dev,
35 "failed to enable clock, err %d. type:%d i:%d\n",
36 err, comp->type, i);
37 }
38 }
39
mtk_mdp_comp_clock_off(struct device * dev,struct mtk_mdp_comp * comp)40 void mtk_mdp_comp_clock_off(struct device *dev, struct mtk_mdp_comp *comp)
41 {
42 int i;
43
44 for (i = 0; i < ARRAY_SIZE(comp->clk); i++) {
45 if (IS_ERR(comp->clk[i]))
46 continue;
47 clk_disable_unprepare(comp->clk[i]);
48 }
49
50 if (comp->larb_dev)
51 mtk_smi_larb_put(comp->larb_dev);
52 }
53
mtk_mdp_comp_init(struct device * dev,struct device_node * node,struct mtk_mdp_comp * comp,enum mtk_mdp_comp_type comp_type)54 int mtk_mdp_comp_init(struct device *dev, struct device_node *node,
55 struct mtk_mdp_comp *comp,
56 enum mtk_mdp_comp_type comp_type)
57 {
58 struct device_node *larb_node;
59 struct platform_device *larb_pdev;
60 int ret;
61 int i;
62
63 comp->dev_node = of_node_get(node);
64 comp->type = comp_type;
65
66 for (i = 0; i < ARRAY_SIZE(comp->clk); i++) {
67 comp->clk[i] = of_clk_get(node, i);
68 if (IS_ERR(comp->clk[i])) {
69 if (PTR_ERR(comp->clk[i]) != -EPROBE_DEFER)
70 dev_err(dev, "Failed to get clock\n");
71 ret = PTR_ERR(comp->clk[i]);
72 goto put_dev;
73 }
74
75 /* Only RDMA needs two clocks */
76 if (comp->type != MTK_MDP_RDMA)
77 break;
78 }
79
80 /* Only DMA capable components need the LARB property */
81 comp->larb_dev = NULL;
82 if (comp->type != MTK_MDP_RDMA &&
83 comp->type != MTK_MDP_WDMA &&
84 comp->type != MTK_MDP_WROT)
85 return 0;
86
87 larb_node = of_parse_phandle(node, "mediatek,larb", 0);
88 if (!larb_node) {
89 dev_err(dev,
90 "Missing mediadek,larb phandle in %pOF node\n", node);
91 ret = -EINVAL;
92 goto put_dev;
93 }
94
95 larb_pdev = of_find_device_by_node(larb_node);
96 if (!larb_pdev) {
97 dev_warn(dev, "Waiting for larb device %pOF\n", larb_node);
98 of_node_put(larb_node);
99 ret = -EPROBE_DEFER;
100 goto put_dev;
101 }
102 of_node_put(larb_node);
103
104 comp->larb_dev = &larb_pdev->dev;
105
106 return 0;
107
108 put_dev:
109 of_node_put(comp->dev_node);
110
111 return ret;
112 }
113
mtk_mdp_comp_deinit(struct device * dev,struct mtk_mdp_comp * comp)114 void mtk_mdp_comp_deinit(struct device *dev, struct mtk_mdp_comp *comp)
115 {
116 of_node_put(comp->dev_node);
117 }
118