summaryrefslogtreecommitdiff
path: root/drivers/clk/mediatek/reset.c
blob: 290ceda84ce47dc54b5ad8b347b6189df347a371 (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2014 MediaTek Inc.
 */

#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/slab.h>

#include "reset.h"

static inline struct mtk_clk_rst_data *to_mtk_clk_rst_data(struct reset_controller_dev *rcdev)
{
	return container_of(rcdev, struct mtk_clk_rst_data, rcdev);
}

static int mtk_reset_update(struct reset_controller_dev *rcdev,
			    unsigned long id, bool deassert)
{
	struct mtk_clk_rst_data *data = to_mtk_clk_rst_data(rcdev);
	unsigned int val = deassert ? 0 : ~0;

	return regmap_update_bits(data->regmap,
				  data->desc->rst_bank_ofs[id / RST_NR_PER_BANK],
				  BIT(id % RST_NR_PER_BANK), val);
}

static int mtk_reset_assert(struct reset_controller_dev *rcdev,
			    unsigned long id)
{
	return mtk_reset_update(rcdev, id, false);
}

static int mtk_reset_deassert(struct reset_controller_dev *rcdev,
			      unsigned long id)
{
	return mtk_reset_update(rcdev, id, true);
}

static int mtk_reset(struct reset_controller_dev *rcdev, unsigned long id)
{
	int ret;

	ret = mtk_reset_assert(rcdev, id);
	if (ret)
		return ret;

	return mtk_reset_deassert(rcdev, id);
}

static int mtk_reset_update_set_clr(struct reset_controller_dev *rcdev,
				    unsigned long id, bool deassert)
{
	struct mtk_clk_rst_data *data = to_mtk_clk_rst_data(rcdev);
	unsigned int deassert_ofs = deassert ? 0x4 : 0;

	return regmap_write(data->regmap,
			    data->desc->rst_bank_ofs[id / RST_NR_PER_BANK] +
			    deassert_ofs,
			    BIT(id % RST_NR_PER_BANK));
}

static int mtk_reset_assert_set_clr(struct reset_controller_dev *rcdev,
				    unsigned long id)
{
	return mtk_reset_update_set_clr(rcdev, id, false);
}

static int mtk_reset_deassert_set_clr(struct reset_controller_dev *rcdev,
				      unsigned long id)
{
	return mtk_reset_update_set_clr(rcdev, id, true);
}

static int mtk_reset_set_clr(struct reset_controller_dev *rcdev,
			     unsigned long id)
{
	int ret;

	ret = mtk_reset_assert_set_clr(rcdev, id);
	if (ret)
		return ret;
	return mtk_reset_deassert_set_clr(rcdev, id);
}

static const struct reset_control_ops mtk_reset_ops = {
	.assert = mtk_reset_assert,
	.deassert = mtk_reset_deassert,
	.reset = mtk_reset,
};

static const struct reset_control_ops mtk_reset_ops_set_clr = {
	.assert = mtk_reset_assert_set_clr,
	.deassert = mtk_reset_deassert_set_clr,
	.reset = mtk_reset_set_clr,
};

static int reset_xlate(struct reset_controller_dev *rcdev,
		       const struct of_phandle_args *reset_spec)
{
	struct mtk_clk_rst_data *data = to_mtk_clk_rst_data(rcdev);

	if (reset_spec->args[0] >= rcdev->nr_resets ||
	    reset_spec->args[0] >= data->desc->rst_idx_map_nr)
		return -EINVAL;

	return data->desc->rst_idx_map[reset_spec->args[0]];
}

int mtk_register_reset_controller(struct device_node *np,
				  const struct mtk_clk_rst_desc *desc)
{
	struct regmap *regmap;
	const struct reset_control_ops *rcops = NULL;
	struct mtk_clk_rst_data *data;
	int ret;

	if (!desc) {
		pr_err("mtk clock reset desc is NULL\n");
		return -EINVAL;
	}

	switch (desc->version) {
	case MTK_RST_SIMPLE:
		rcops = &mtk_reset_ops;
		break;
	case MTK_RST_SET_CLR:
		rcops = &mtk_reset_ops_set_clr;
		break;
	default:
		pr_err("Unknown reset version %d\n", desc->version);
		return -EINVAL;
	}

	regmap = device_node_to_regmap(np);
	if (IS_ERR(regmap)) {
		pr_err("Cannot find regmap for %pOF: %pe\n", np, regmap);
		return -EINVAL;
	}

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

	data->desc = desc;
	data->regmap = regmap;
	data->rcdev.owner = THIS_MODULE;
	data->rcdev.ops = rcops;
	data->rcdev.of_node = np;

	if (data->desc->rst_idx_map_nr > 0) {
		data->rcdev.of_reset_n_cells = 1;
		data->rcdev.nr_resets = desc->rst_idx_map_nr;
		data->rcdev.of_xlate = reset_xlate;
	} else {
		data->rcdev.nr_resets = desc->rst_bank_nr * RST_NR_PER_BANK;
	}

	ret = reset_controller_register(&data->rcdev);
	if (ret) {
		pr_err("could not register reset controller: %d\n", ret);
		kfree(data);
		return ret;
	}

	return 0;
}

int mtk_register_reset_controller_with_dev(struct device *dev,
					   const struct mtk_clk_rst_desc *desc)
{
	struct device_node *np = dev->of_node;
	struct regmap *regmap;
	const struct reset_control_ops *rcops = NULL;
	struct mtk_clk_rst_data *data;
	int ret;

	if (!desc) {
		dev_err(dev, "mtk clock reset desc is NULL\n");
		return -EINVAL;
	}

	switch (desc->version) {
	case MTK_RST_SIMPLE:
		rcops = &mtk_reset_ops;
		break;
	case MTK_RST_SET_CLR:
		rcops = &mtk_reset_ops_set_clr;
		break;
	default:
		dev_err(dev, "Unknown reset version %d\n", desc->version);
		return -EINVAL;
	}

	regmap = device_node_to_regmap(np);
	if (IS_ERR(regmap)) {
		dev_err(dev, "Cannot find regmap %pe\n", regmap);
		return -EINVAL;
	}

	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	data->desc = desc;
	data->regmap = regmap;
	data->rcdev.owner = THIS_MODULE;
	data->rcdev.ops = rcops;
	data->rcdev.of_node = np;
	data->rcdev.dev = dev;

	if (data->desc->rst_idx_map_nr > 0) {
		data->rcdev.of_reset_n_cells = 1;
		data->rcdev.nr_resets = desc->rst_idx_map_nr;
		data->rcdev.of_xlate = reset_xlate;
	} else {
		data->rcdev.nr_resets = desc->rst_bank_nr * RST_NR_PER_BANK;
	}

	ret = devm_reset_controller_register(dev, &data->rcdev);
	if (ret) {
		dev_err(dev, "could not register reset controller: %d\n", ret);
		return ret;
	}

	return 0;
}
EXPORT_SYMBOL_GPL(mtk_register_reset_controller_with_dev);

MODULE_LICENSE("GPL");