aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/mediatek/clk-gate.c
blob: 61631be257a9de981ac5ef383a1f66c208adaef5 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2014 MediaTek Inc.
 * Author: James Liao <jamesjj.liao@mediatek.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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 <linux/of.h>
#include <linux/of_address.h>

#include <linux/io.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/clkdev.h>

#include "clk-mtk.h"
#include "clk-gate.h"

/*
 * clk_gate
 */

static void cg_set_mask(struct mtk_clk_gate *cg, u32 mask)
{
	u32 r;

	if (cg->flags & CLK_GATE_NO_SETCLR_REG) {
		r = readl_relaxed(cg->sta_addr) | mask;
		writel_relaxed(r, cg->sta_addr);
	} else
		writel_relaxed(mask, cg->set_addr);
}

static void cg_clr_mask(struct mtk_clk_gate *cg, u32 mask)
{
	u32 r;

	if (cg->flags & CLK_GATE_NO_SETCLR_REG) {
		r = readl_relaxed(cg->sta_addr) & ~mask;
		writel_relaxed(r, cg->sta_addr);
	} else
		writel_relaxed(mask, cg->clr_addr);
}

static int cg_enable(struct clk_hw *hw)
{
	unsigned long flags = 0;
	struct mtk_clk_gate *cg = to_clk_gate(hw);
	u32 mask = BIT(cg->bit);
#ifdef Bring_Up
	if (printk_ratelimit())
		pr_notice("[CCF] %s: %s, bit: %u\n", __func__, __clk_get_name(hw->clk),
		       cg->bit);
        return 0;
#endif

	pr_debug("%s, bit: %u\n", __clk_get_name(hw->clk), cg->bit);

	mtk_clk_lock(flags);

	if (cg->flags & CLK_GATE_INVERSE)
		cg_set_mask(cg, mask);
	else
		cg_clr_mask(cg, mask);

	mtk_clk_unlock(flags);

	return 0;
}

static void cg_disable(struct clk_hw *hw)
{
	unsigned long flags = 0;
	struct mtk_clk_gate *cg = to_clk_gate(hw);
	u32 mask = BIT(cg->bit);
#ifdef Bring_Up
	if (printk_ratelimit())
		pr_notice("[CCF] %s: %s, bit: %u\n", __func__, __clk_get_name(hw->clk),
		       cg->bit);
        return;
#endif

	pr_debug("%s, bit: %u\n", __clk_get_name(hw->clk), cg->bit);

	mtk_clk_lock(flags);

	if (cg->flags & CLK_GATE_INVERSE)
		cg_clr_mask(cg, mask);
	else
		cg_set_mask(cg, mask);

	mtk_clk_unlock(flags);
}

static int cg_is_enabled(struct clk_hw *hw)
{
	struct mtk_clk_gate *cg = to_clk_gate(hw);
	u32 mask;
	u32 val;
	int r;
#ifdef Bring_Up
	if (printk_ratelimit())
		pr_notice("[CCF] %s: %s\n", __func__, __clk_get_name(hw->clk));
	return 1;
#endif

	mask = BIT(cg->bit);
	val = mask & readl(cg->sta_addr);

	r = (cg->flags & CLK_GATE_INVERSE) ? (val != 0) : (val == 0);

	pr_debug("%d, %s, bit[%d]\n", r, __clk_get_name(hw->clk), (int)cg->bit);

	return r;
}

static const struct clk_ops mtk_clk_gate_ops = {
	.is_enabled	= cg_is_enabled,
	.enable		= cg_enable,
	.disable	= cg_disable,
};

struct clk *mtk_clk_register_gate(
		const char *name,
		const char *parent_name,
		void __iomem *set_addr,
		void __iomem *clr_addr,
		void __iomem *sta_addr,
		u8 bit,
		u32 flags)
{
	struct mtk_clk_gate *cg;
	struct clk *clk;
	struct clk_init_data init;

#ifdef Bring_Up
	pr_notice("[CCF] name: %s, bit: %d\n", name, (int)bit);
#else
	pr_debug("name: %s, bit: %d\n", name, (int)bit);
#endif

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

	init.name = name;
	init.flags = CLK_IGNORE_UNUSED;
	init.parent_names = parent_name ? &parent_name : NULL;
	init.num_parents = parent_name ? 1 : 0;
	init.ops = &mtk_clk_gate_ops;

	cg->set_addr = set_addr;
	cg->clr_addr = clr_addr;
	cg->sta_addr = sta_addr;
	cg->bit = bit;
	cg->flags = flags;

	cg->hw.init = &init;

	clk = clk_register(NULL, &cg->hw);
	if (IS_ERR(clk))
		kfree(cg);

	return clk;
}