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