summaryrefslogtreecommitdiffstats
path: root/drivers/staging/media/atomisp/platform/clock/vlv2_plat_clock.c
blob: f96789a31819cb9b171acb490603faa3f0b944a6 (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
/*
 * vlv2_plat_clock.c - VLV2 platform clock driver
 * Copyright (C) 2013 Intel Corporation
 *
 * Author: Asutosh Pathak <asutosh.pathak@intel.com>
 * Author: Chandra Sekhar Anagani <chandra.sekhar.anagani@intel.com>
 *
 * 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; version 2 of the License.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <linux/err.h>
#include <linux/io.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include "../../include/linux/vlv2_plat_clock.h"

/* NOTE: Most of below constants could come from platform data.
 * To be fixed when appropriate ACPI support comes.
 */
#define VLV2_PMC_CLK_BASE_ADDRESS	0xfed03060
#define PLT_CLK_CTL_OFFSET(x)		(0x04 * (x))

#define CLK_CONFG_BIT_POS		0
#define CLK_CONFG_BIT_LEN		2
#define CLK_CONFG_D3_GATED		0
#define CLK_CONFG_FORCE_ON		1
#define CLK_CONFG_FORCE_OFF		2

#define CLK_FREQ_TYPE_BIT_POS		2
#define CLK_FREQ_TYPE_BIT_LEN		1
#define CLK_FREQ_TYPE_XTAL		0	/* 25 MHz */
#define CLK_FREQ_TYPE_PLL		1	/* 19.2 MHz */

#define MAX_CLK_COUNT			5

/* Helper macros to manipulate bitfields */
#define REG_MASK(n)		(((1 << (n##_BIT_LEN)) - 1) << (n##_BIT_POS))
#define REG_SET_FIELD(r, n, v)	(((r) & ~REG_MASK(n)) | \
				 (((v) << (n##_BIT_POS)) & REG_MASK(n)))
#define REG_GET_FIELD(r, n)	(((r) & REG_MASK(n)) >> n##_BIT_POS)
/*
 * vlv2 platform has 6 platform clocks, controlled by 4 byte registers
 * Total size required for mapping is 6*4 = 24 bytes
 */
#define PMC_MAP_SIZE			24

static DEFINE_MUTEX(clk_mutex);
static void __iomem *pmc_base;

/*
 * vlv2_plat_set_clock_freq - Set clock frequency to a specified platform clock
 * @clk_num: Platform clock number (i.e. 0, 1, 2, ...,5)
 * @freq_type: Clock frequency (0-25 MHz(XTAL), 1-19.2 MHz(PLL) )
 */
int vlv2_plat_set_clock_freq(int clk_num, int freq_type)
{
	void __iomem *addr;

	if (clk_num < 0 || clk_num >= MAX_CLK_COUNT) {
		pr_err("Clock number out of range (%d)\n", clk_num);
		return -EINVAL;
	}

	if (freq_type != CLK_FREQ_TYPE_XTAL &&
	    freq_type != CLK_FREQ_TYPE_PLL) {
		pr_err("wrong clock type\n");
		return -EINVAL;
	}

	if (!pmc_base) {
		pr_err("memio map is not set\n");
		return -EINVAL;
	}

	addr = pmc_base + PLT_CLK_CTL_OFFSET(clk_num);

	mutex_lock(&clk_mutex);
	writel(REG_SET_FIELD(readl(addr), CLK_FREQ_TYPE, freq_type), addr);
	mutex_unlock(&clk_mutex);

	return 0;
}
EXPORT_SYMBOL_GPL(vlv2_plat_set_clock_freq);

/*
 * vlv2_plat_get_clock_freq - Get the status of specified platform clock
 * @clk_num: Platform clock number (i.e. 0, 1, 2, ...,5)
 *
 * Returns 0 for 25 MHz(XTAL) and 1 for 19.2 MHz(PLL)
 */
int vlv2_plat_get_clock_freq(int clk_num)
{
	u32 ret;

	if (clk_num < 0 || clk_num >= MAX_CLK_COUNT) {
		pr_err("Clock number out of range (%d)\n", clk_num);
		return -EINVAL;
	}

	if (!pmc_base) {
		pr_err("memio map is not set\n");
		return -EINVAL;
	}

	mutex_lock(&clk_mutex);
	ret = REG_GET_FIELD(readl(pmc_base + PLT_CLK_CTL_OFFSET(clk_num)),
			    CLK_FREQ_TYPE);
	mutex_unlock(&clk_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(vlv2_plat_get_clock_freq);

/*
 * vlv2_plat_configure_clock - Configure the specified platform clock
 * @clk_num: Platform clock number (i.e. 0, 1, 2, ...,5)
 * @conf:      Clock gating:
 *		0   - Clock gated on D3 state
 *		1   - Force on
 *		2,3 - Force off
 */
int vlv2_plat_configure_clock(int clk_num, u32 conf)
{
	void __iomem *addr;

	if (clk_num < 0 || clk_num >= MAX_CLK_COUNT) {
		pr_err("Clock number out of range (%d)\n", clk_num);
		return -EINVAL;
	}

	if (conf != CLK_CONFG_D3_GATED &&
	    conf != CLK_CONFG_FORCE_ON &&
	    conf != CLK_CONFG_FORCE_OFF) {
		pr_err("Invalid clock configuration requested\n");
		return -EINVAL;
	}

	if (!pmc_base) {
		pr_err("memio map is not set\n");
		return -EINVAL;
	}

	addr = pmc_base + PLT_CLK_CTL_OFFSET(clk_num);

	mutex_lock(&clk_mutex);
	writel(REG_SET_FIELD(readl(addr), CLK_CONFG, conf), addr);
	mutex_unlock(&clk_mutex);
	return 0;
}
EXPORT_SYMBOL_GPL(vlv2_plat_configure_clock);

/*
 * vlv2_plat_get_clock_status - Get the status of specified platform clock
 * @clk_num: Platform clock number (i.e. 0, 1, 2, ...,5)
 *
 * Returns 1 - On, 0 - Off
 */
int vlv2_plat_get_clock_status(int clk_num)
{
	int ret;

	if (clk_num < 0 || clk_num >= MAX_CLK_COUNT) {
		pr_err("Clock number out of range (%d)\n", clk_num);
		return -EINVAL;
	}

	if (!pmc_base) {
		pr_err("memio map is not set\n");
		return -EINVAL;
	}

	mutex_lock(&clk_mutex);
	ret = (int)REG_GET_FIELD(readl(pmc_base + PLT_CLK_CTL_OFFSET(clk_num)),
				 CLK_CONFG);
	mutex_unlock(&clk_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(vlv2_plat_get_clock_status);

static int vlv2_plat_clk_probe(struct platform_device *pdev)
{
	int i = 0;

	pmc_base = ioremap_nocache(VLV2_PMC_CLK_BASE_ADDRESS, PMC_MAP_SIZE);
	if (!pmc_base) {
		dev_err(&pdev->dev, "I/O memory remapping failed\n");
		return -ENOMEM;
	}

	/* Initialize all clocks as disabled */
	for (i = 0; i < MAX_CLK_COUNT; i++)
		vlv2_plat_configure_clock(i, CLK_CONFG_FORCE_OFF);

	dev_info(&pdev->dev, "vlv2_plat_clk initialized\n");
	return 0;
}

static const struct platform_device_id vlv2_plat_clk_id[] = {
	{"vlv2_plat_clk", 0},
	{}
};

static int vlv2_resume(struct device *device)
{
	int i;

	/* Initialize all clocks as disabled */
	for (i = 0; i < MAX_CLK_COUNT; i++)
		vlv2_plat_configure_clock(i, CLK_CONFG_FORCE_OFF);

	return 0;
}

static int vlv2_suspend(struct device *device)
{
	return 0;
}

static const struct dev_pm_ops vlv2_pm_ops = {
	.suspend = vlv2_suspend,
	.resume = vlv2_resume,
};

static struct platform_driver vlv2_plat_clk_driver = {
	.probe = vlv2_plat_clk_probe,
	.id_table = vlv2_plat_clk_id,
	.driver = {
		.name = "vlv2_plat_clk",
		.pm = &vlv2_pm_ops,
	},
};

static int __init vlv2_plat_clk_init(void)
{
	return platform_driver_register(&vlv2_plat_clk_driver);
}
arch_initcall(vlv2_plat_clk_init);
OpenPOWER on IntegriCloud