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 
11 #include "mtk_mdp_comp.h"
12 
13 
14 void mtk_mdp_comp_clock_on(struct device *dev, struct mtk_mdp_comp *comp)
15 {
16 	int i, err;
17 
18 	for (i = 0; i < ARRAY_SIZE(comp->clk); i++) {
19 		if (IS_ERR(comp->clk[i]))
20 			continue;
21 		err = clk_prepare_enable(comp->clk[i]);
22 		if (err)
23 			dev_err(dev,
24 			"failed to enable clock, err %d. type:%d i:%d\n",
25 				err, comp->type, i);
26 	}
27 }
28 
29 void mtk_mdp_comp_clock_off(struct device *dev, struct mtk_mdp_comp *comp)
30 {
31 	int i;
32 
33 	for (i = 0; i < ARRAY_SIZE(comp->clk); i++) {
34 		if (IS_ERR(comp->clk[i]))
35 			continue;
36 		clk_disable_unprepare(comp->clk[i]);
37 	}
38 }
39 
40 int mtk_mdp_comp_init(struct device *dev, struct device_node *node,
41 		      struct mtk_mdp_comp *comp,
42 		      enum mtk_mdp_comp_type comp_type)
43 {
44 	int ret;
45 	int i;
46 
47 	comp->dev_node = of_node_get(node);
48 	comp->type = comp_type;
49 
50 	for (i = 0; i < ARRAY_SIZE(comp->clk); i++) {
51 		comp->clk[i] = of_clk_get(node, i);
52 		if (IS_ERR(comp->clk[i])) {
53 			ret = dev_err_probe(dev, PTR_ERR(comp->clk[i]),
54 					    "Failed to get clock\n");
55 			goto put_dev;
56 		}
57 
58 		/* Only RDMA needs two clocks */
59 		if (comp->type != MTK_MDP_RDMA)
60 			break;
61 	}
62 
63 	return 0;
64 
65 put_dev:
66 	of_node_put(comp->dev_node);
67 
68 	return ret;
69 }
70 
71 void mtk_mdp_comp_deinit(struct device *dev, struct mtk_mdp_comp *comp)
72 {
73 	of_node_put(comp->dev_node);
74 }
75