summaryrefslogtreecommitdiffstats
path: root/drivers/clk/clk-gate.c
blob: b298b193dc3bd66773acdb0cb2b70bff4f15c246 (plain) (blame)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * clk-gate.c - generic barebox clock support. Based on Linux clk support
 *
 * Copyright (c) 2012 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <common.h>
#include <io.h>
#include <malloc.h>
#include <linux/clk.h>
#include <linux/err.h>

struct clk_gate {
	struct clk clk;
	void __iomem *reg;
	int shift;
	const char *parent;
#define CLK_GATE_INVERTED	(1 << 0)
	unsigned flags;
};

#define to_clk_gate(_clk) container_of(_clk, struct clk_gate, clk)

static int clk_gate_enable(struct clk *clk)
{
	struct clk_gate *g = container_of(clk, struct clk_gate, clk);
	u32 val;

	val = readl(g->reg);

	if (g->flags & CLK_GATE_INVERTED)
		val &= ~(1 << g->shift);
	else
		val |= 1 << g->shift;

	writel(val, g->reg);

	return 0;
}

static void clk_gate_disable(struct clk *clk)
{
	struct clk_gate *g = container_of(clk, struct clk_gate, clk);
	u32 val;

	val = readl(g->reg);

	if (g->flags & CLK_GATE_INVERTED)
		val |= 1 << g->shift;
	else
		val &= ~(1 << g->shift);

	writel(val, g->reg);
}

static int clk_gate_is_enabled(struct clk *clk)
{
	struct clk_gate *g = container_of(clk, struct clk_gate, clk);
	u32 val;

	val = readl(g->reg);

	if (val & (1 << g->shift))
		return g->flags & CLK_GATE_INVERTED ? 0 : 1;
	else
		return g->flags & CLK_GATE_INVERTED ? 1 : 0;
}

static struct clk_ops clk_gate_ops = {
	.set_rate = clk_parent_set_rate,
	.round_rate = clk_parent_round_rate,
	.enable = clk_gate_enable,
	.disable = clk_gate_disable,
	.is_enabled = clk_gate_is_enabled,
};

struct clk *clk_gate_alloc(const char *name, const char *parent,
		void __iomem *reg, u8 shift, unsigned flags)
{
	struct clk_gate *g = xzalloc(sizeof(*g));

	g->parent = parent;
	g->reg = reg;
	g->shift = shift;
	g->clk.ops = &clk_gate_ops;
	g->clk.name = name;
	g->clk.flags = flags;
	g->clk.parent_names = &g->parent;
	g->clk.num_parents = 1;

	return &g->clk;
}

void clk_gate_free(struct clk *clk_gate)
{
	struct clk_gate *g = to_clk_gate(clk_gate);

	free(g);
}

struct clk *clk_gate(const char *name, const char *parent, void __iomem *reg,
		u8 shift, unsigned flags)
{
	struct clk *g;
	int ret;

	g = clk_gate_alloc(name , parent, reg, shift, flags);

	ret = clk_register(g);
	if (ret) {
		free(to_clk_gate(g));
		return ERR_PTR(ret);
	}

	return g;
}

struct clk *clk_gate_inverted(const char *name, const char *parent,
		void __iomem *reg, u8 shift, unsigned flags)
{
	struct clk *clk;
	struct clk_gate *g;

	clk = clk_gate(name, parent, reg, shift, flags);
	if (IS_ERR(clk))
		return clk;

	g = container_of(clk, struct clk_gate, clk);

	g->flags = CLK_GATE_INVERTED;

	return clk;
}