summaryrefslogtreecommitdiffstats
path: root/drivers/clk/zynqmp/clk-gate-zynqmp.c
blob: 6f0335776878b27fff51919ed3b6862f7a9e834d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SPDX-License-Identifier: GPL-2.0
/*
 * Zynq UltraScale+ MPSoC Clock Gate
 *
 * Copyright (C) 2019 Pengutronix, Michael Tretter <m.tretter@pengutronix.de>
 *
 * Based on the Linux driver in drivers/clk/zynqmp/
 *
 * Copyright (C) 2016-2018 Xilinx
 */

#include <common.h>
#include <linux/clk.h>
#include <mach/firmware-zynqmp.h>

#include "clk-zynqmp.h"

struct zynqmp_clk_gate {
	struct clk clk;
	unsigned int clk_id;
	const char *parent;
	const struct zynqmp_eemi_ops *ops;
};

#define to_zynqmp_clk_gate(_hw) container_of(_hw, struct zynqmp_clk_gate, clk)

static int zynqmp_clk_gate_enable(struct clk *clk)
{
	struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(clk);

	return gate->ops->clock_enable(gate->clk_id);
}

static void zynqmp_clk_gate_disable(struct clk *clk)
{
	struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(clk);

	gate->ops->clock_disable(gate->clk_id);
}

static int zynqmp_clk_gate_is_enabled(struct clk *clk)
{
	struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(clk);
	u32 state;
	int ret;
	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();

	ret = eemi_ops->clock_getstate(gate->clk_id, &state);
	if (ret)
		return -EIO;

	return !!state;
}

static const struct clk_ops zynqmp_clk_gate_ops = {
	.set_rate = clk_parent_set_rate,
	.round_rate = clk_parent_round_rate,
	.enable = zynqmp_clk_gate_enable,
	.disable = zynqmp_clk_gate_disable,
	.is_enabled = zynqmp_clk_gate_is_enabled,
};

struct clk *zynqmp_clk_register_gate(const char *name,
				     unsigned int clk_id,
				     const char **parents,
				     unsigned int num_parents,
				     const struct clock_topology *nodes)
{
	struct zynqmp_clk_gate *gate;
	int ret;

	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
	if (!gate)
		return ERR_PTR(-ENOMEM);

	gate->clk_id = clk_id;
	gate->ops = zynqmp_pm_get_eemi_ops();
	gate->parent = strdup(parents[0]);

	gate->clk.name = strdup(name);
	gate->clk.ops = &zynqmp_clk_gate_ops;
	gate->clk.flags = nodes->flag | CLK_SET_RATE_PARENT;
	gate->clk.parent_names = &gate->parent;
	gate->clk.num_parents = 1;

	ret = clk_register(&gate->clk);
	if (ret) {
		kfree(gate);
		return ERR_PTR(ret);
	}

	return &gate->clk;
}