aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/qcom/rpmcc-apq8064.c
blob: 392cca7d69c1bdb44acc1cfe28e993befcd5a308 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <linux/kernel.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/clk-provider.h>
#include <linux/mfd/qcom_rpm.h>
#include <dt-bindings/mfd/qcom-rpm.h>

struct rpm_cc {
	struct clk_onecell_data data;
	struct clk *clks[];
};
struct rpm_clk {
	const int rpm_clk_id;
	struct qcom_rpm *rpm;
	unsigned last_set_khz;
	bool enabled;
	bool branch; /* true: RPM only accepts 1 for ON and 0 for OFF */
	unsigned factor;
	struct clk_hw hw;
};

#define to_rpm_clk(_hw) container_of(_hw, struct rpm_clk, hw)

static int rpm_clk_prepare(struct clk_hw *hw)
{
	struct rpm_clk *r = to_rpm_clk(hw);
	uint32_t value;
	int rc = 0;
	unsigned long this_khz;

	this_khz = r->last_set_khz;
	/* Don't send requests to the RPM if the rate has not been set. */
	if (r->last_set_khz == 0)
		goto out;

	value = this_khz;
	if (r->branch)
		value = !!value;

	rc = qcom_rpm_write(r->rpm, QCOM_RPM_ACTIVE_STATE,
			    r->rpm_clk_id, &value, 1);
	if (rc)
		goto out;

out:
	if (!rc)
		r->enabled = true;
	return rc;
}

static void rpm_clk_unprepare(struct clk_hw *hw)
{
	struct rpm_clk *r = to_rpm_clk(hw);

	if (r->last_set_khz) {
		uint32_t value = 0;
		int rc;

		rc = qcom_rpm_write(r->rpm, QCOM_RPM_ACTIVE_STATE,
			    r->rpm_clk_id, &value, 1);
		if (rc)
			return;

	}
	r->enabled = false;
}

int rpm_clk_set_rate(struct clk_hw *hw,
		     unsigned long rate, unsigned long prate)
{
	struct rpm_clk *r = to_rpm_clk(hw);
	unsigned long this_khz;
	int rc = 0;

	this_khz = DIV_ROUND_UP(rate, r->factor);

	if (r->enabled) {
		uint32_t value = this_khz;

		rc = qcom_rpm_write(r->rpm, QCOM_RPM_ACTIVE_STATE,
				    r->rpm_clk_id, &value, 1);
		if (rc)
			goto out;
	}

	if (!rc)
		r->last_set_khz = this_khz;

out:
	return rc;
}

static long rpm_clk_round_rate(struct clk_hw *hw, unsigned long rate,
		unsigned long *parent_rate)
{
	return rate;
}

static unsigned long rpm_clk_recalc_rate(struct clk_hw *hw,
				unsigned long parent_rate)
{
	struct rpm_clk *r = to_rpm_clk(hw);
	u32 val;
	int rc;

	rc = qcom_rpm_read(r->rpm, r->rpm_clk_id, &val, 1);
	if (rc < 0)
		return 0;

	return val * r->factor;
}

static unsigned long rpm_branch_clk_recalc_rate(struct clk_hw *hw,
				unsigned long parent_rate)
{
	struct rpm_clk *r = to_rpm_clk(hw);

	return r->last_set_khz * r->factor;
}

static const struct clk_ops branch_clk_ops_rpm = {
	.prepare	= rpm_clk_prepare,
	.unprepare	= rpm_clk_unprepare,
	.recalc_rate	= rpm_branch_clk_recalc_rate,
	.round_rate	= rpm_clk_round_rate,
};

static const struct clk_ops clk_ops_rpm = {
	.prepare	= rpm_clk_prepare,
	.unprepare	= rpm_clk_unprepare,
	.set_rate	= rpm_clk_set_rate,
	.recalc_rate	= rpm_clk_recalc_rate,
	.round_rate	= rpm_clk_round_rate,
};

static struct rpm_clk pxo_clk = {
	.rpm_clk_id = QCOM_RPM_PXO_CLK,
	.branch	= true,
	.factor = 1000,
	.last_set_khz = 27000,
	.hw.init = &(struct clk_init_data){
		.name = "pxo",
		.ops = &branch_clk_ops_rpm,
	},
};

static struct rpm_clk cxo_clk = {
	.rpm_clk_id = QCOM_RPM_CXO_CLK,
	.branch	= true,
	.factor = 1000,
	.last_set_khz = 19200,
	.hw.init = &(struct clk_init_data){
		.name = "cxo",
		.ops = &branch_clk_ops_rpm,
	},
};

static struct rpm_clk afab_clk = {
	.rpm_clk_id = QCOM_RPM_APPS_FABRIC_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "afab_clk",
		.ops = &clk_ops_rpm,
	},
};

static struct rpm_clk cfpb_clk = {
	.rpm_clk_id = QCOM_RPM_CFPB_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "cfpb_clk",
		.ops = &clk_ops_rpm,
	},
};

static struct rpm_clk daytona_clk = {
	.rpm_clk_id = QCOM_RPM_DAYTONA_FABRIC_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "daytona_clk",
		.ops = &clk_ops_rpm,
	},
};

static struct rpm_clk ebi1_clk = {
	.rpm_clk_id = QCOM_RPM_EBI1_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "ebi1_clk",
		.ops = &clk_ops_rpm,
	},
};

static struct rpm_clk mmfab_clk = {
	.rpm_clk_id = QCOM_RPM_MM_FABRIC_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "mmfab_clk",
		.ops = &clk_ops_rpm,
	},
};

static struct rpm_clk mmfpb_clk = {
	.rpm_clk_id = QCOM_RPM_MMFPB_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "mmfpb_clk",
		.ops = &clk_ops_rpm,
	},
};

static struct rpm_clk sfab_clk = {
	.rpm_clk_id = QCOM_RPM_SYS_FABRIC_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "sfab_clk",
		.ops = &clk_ops_rpm,
	},
};

static struct rpm_clk sfpb_clk = {
	.rpm_clk_id = QCOM_RPM_SFPB_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "sfpb_clk",
		.ops = &clk_ops_rpm,
	},
};

/*
static struct rpm_clk qdss_clk = {
	.rpm_clk_id = QCOM_RPM_QDSS_CLK,
	.factor = 1000,
	.hw.init = &(struct clk_init_data){
		.name = "qdss_clk",
		.ops = &clk_ops_rpm,
	},
};
*/

struct rpm_clk *rpm_clks[] = {
	[QCOM_RPM_PXO_CLK] = &pxo_clk,
	[QCOM_RPM_CXO_CLK] = &cxo_clk,
	[QCOM_RPM_APPS_FABRIC_CLK] = &afab_clk,
	[QCOM_RPM_CFPB_CLK] = &cfpb_clk,
	[QCOM_RPM_DAYTONA_FABRIC_CLK] = &daytona_clk,
	[QCOM_RPM_EBI1_CLK] = &ebi1_clk,
	[QCOM_RPM_MM_FABRIC_CLK] = &mmfab_clk,
	[QCOM_RPM_MMFPB_CLK] = &mmfpb_clk,
	[QCOM_RPM_SYS_FABRIC_CLK] = &sfab_clk,
	[QCOM_RPM_SFPB_CLK] = &sfpb_clk,
/**	[QCOM_RPM_QDSS_CLK] = &qdss_clk, Needs more checking here **/
};

static int rpm_clk_probe(struct platform_device *pdev)
{
	struct clk **clks;
	struct clk *clk;
	struct rpm_cc *cc;
	struct qcom_rpm *rpm;
	int num_clks = ARRAY_SIZE(rpm_clks);
	struct clk_onecell_data *data;
	int i, ret;

	if (!pdev->dev.of_node)
		return -ENODEV;

	cc = devm_kzalloc(&pdev->dev, sizeof(*cc) + sizeof(*clks) * num_clks,
			  GFP_KERNEL);
	if (!cc)
		return -ENOMEM;

	clks = cc->clks;
	data = &cc->data;
	data->clks = clks;
	data->clk_num = num_clks;

	rpm = dev_get_drvdata(pdev->dev.parent);
	if (!rpm) {
		dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
		return -ENODEV;
	}

	for (i = 0; i < num_clks; i++) {
		if (!rpm_clks[i]) {
			clks[i] = ERR_PTR(-ENOENT);
			continue;
		}
		rpm_clks[i]->rpm = rpm;
		clk = devm_clk_register(&pdev->dev, &rpm_clks[i]->hw);
		if (IS_ERR(clk))
			return PTR_ERR(clk);

		clks[i] = clk;
	}

	ret = of_clk_add_provider(pdev->dev.of_node, of_clk_src_onecell_get,
				    data);
	if (ret)
		return ret;

	/* Hold and active set vote */
	clk_set_rate(afab_clk.hw.clk, INT_MAX);
	clk_prepare_enable(afab_clk.hw.clk);

	return 0;
}

static int rpm_clk_remove(struct platform_device *pdev)
{
	of_clk_del_provider(pdev->dev.of_node);
	return 0;
}

static const struct of_device_id rpm_clk_of_match[] = {
	{ .compatible = "qcom,apq8064-rpm-clk" },
	{ },
};

static struct platform_driver rpm_clk_driver = {
	.driver = {
		.name = "qcom-rpm-clk",
		.of_match_table = rpm_clk_of_match,
	},
	.probe = rpm_clk_probe,
	.remove = rpm_clk_remove,
};

static int __init rpm_clk_init(void)
{
	return platform_driver_register(&rpm_clk_driver);
}
subsys_initcall(rpm_clk_init);

static void __exit rpm_clk_exit(void)
{
	platform_driver_unregister(&rpm_clk_driver);
}
module_exit(rpm_clk_exit)

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
MODULE_DESCRIPTION("Driver for the RPM clocks");