clk.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
 * Copyright (c) 2013 Linaro Ltd.
 * Author: Thomas Abraham <thomas.ab@samsung.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 file includes utility functions to register clocks to common
 * clock framework for Samsung platforms.
*/

14
#include <linux/of_address.h>
15
#include <linux/syscore_ops.h>
16

17 18
#include "clk.h"

19 20
static LIST_HEAD(clock_reg_cache_list);

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
void samsung_clk_save(void __iomem *base,
				    struct samsung_clk_reg_dump *rd,
				    unsigned int num_regs)
{
	for (; num_regs > 0; --num_regs, ++rd)
		rd->value = readl(base + rd->offset);
}

void samsung_clk_restore(void __iomem *base,
				      const struct samsung_clk_reg_dump *rd,
				      unsigned int num_regs)
{
	for (; num_regs > 0; --num_regs, ++rd)
		writel(rd->value, base + rd->offset);
}

37 38 39
struct samsung_clk_reg_dump *samsung_clk_alloc_reg_dump(
						const unsigned long *rdump,
						unsigned long nr_rdump)
40 41 42 43 44 45 46 47 48 49 50 51 52 53
{
	struct samsung_clk_reg_dump *rd;
	unsigned int i;

	rd = kcalloc(nr_rdump, sizeof(*rd), GFP_KERNEL);
	if (!rd)
		return NULL;

	for (i = 0; i < nr_rdump; ++i)
		rd[i].offset = rdump[i];

	return rd;
}

54
/* setup the essentials required to support clock lookup using ccf */
55 56
struct samsung_clk_provider *__init samsung_clk_init(struct device_node *np,
			void __iomem *base, unsigned long nr_clks)
57
{
58 59
	struct samsung_clk_provider *ctx;
	struct clk **clk_table;
60 61
	int i;

62 63 64
	ctx = kzalloc(sizeof(struct samsung_clk_provider), GFP_KERNEL);
	if (!ctx)
		panic("could not allocate clock provider context.\n");
65

66
	clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
67 68 69
	if (!clk_table)
		panic("could not allocate clock lookup table\n");

70 71 72
	for (i = 0; i < nr_clks; ++i)
		clk_table[i] = ERR_PTR(-ENOENT);

73 74 75 76 77 78
	ctx->reg_base = base;
	ctx->clk_data.clks = clk_table;
	ctx->clk_data.clk_num = nr_clks;
	spin_lock_init(&ctx->lock);

	return ctx;
79 80 81 82 83 84 85 86 87 88
}

void __init samsung_clk_of_add_provider(struct device_node *np,
				struct samsung_clk_provider *ctx)
{
	if (np) {
		if (of_clk_add_provider(np, of_clk_src_onecell_get,
					&ctx->clk_data))
			panic("could not register clk provider\n");
	}
89 90 91
}

/* add a clock instance to the clock lookup table used for dt based lookup */
92 93
void samsung_clk_add_lookup(struct samsung_clk_provider *ctx, struct clk *clk,
				unsigned int id)
94
{
95 96
	if (ctx->clk_data.clks && id)
		ctx->clk_data.clks[id] = clk;
97 98
}

99
/* register a list of aliases */
100 101 102
void __init samsung_clk_register_alias(struct samsung_clk_provider *ctx,
				struct samsung_clock_alias *list,
				unsigned int nr_clk)
103 104 105 106
{
	struct clk *clk;
	unsigned int idx, ret;

107
	if (!ctx->clk_data.clks) {
108 109 110 111 112 113 114 115 116 117 118
		pr_err("%s: clock table missing\n", __func__);
		return;
	}

	for (idx = 0; idx < nr_clk; idx++, list++) {
		if (!list->id) {
			pr_err("%s: clock id missing for index %d\n", __func__,
				idx);
			continue;
		}

119
		clk = ctx->clk_data.clks[list->id];
120 121 122 123 124 125 126 127 128 129 130 131 132
		if (!clk) {
			pr_err("%s: failed to find clock %d\n", __func__,
				list->id);
			continue;
		}

		ret = clk_register_clkdev(clk, list->alias, list->dev_name);
		if (ret)
			pr_err("%s: failed to register lookup %s\n",
					__func__, list->alias);
	}
}

133
/* register a list of fixed clocks */
134
void __init samsung_clk_register_fixed_rate(struct samsung_clk_provider *ctx,
135 136 137 138 139 140 141 142 143 144 145 146 147 148
		struct samsung_fixed_rate_clock *list, unsigned int nr_clk)
{
	struct clk *clk;
	unsigned int idx, ret;

	for (idx = 0; idx < nr_clk; idx++, list++) {
		clk = clk_register_fixed_rate(NULL, list->name,
			list->parent_name, list->flags, list->fixed_rate);
		if (IS_ERR(clk)) {
			pr_err("%s: failed to register clock %s\n", __func__,
				list->name);
			continue;
		}

149
		samsung_clk_add_lookup(ctx, clk, list->id);
150 151 152 153 154 155 156 157 158 159 160 161 162

		/*
		 * Unconditionally add a clock lookup for the fixed rate clocks.
		 * There are not many of these on any of Samsung platforms.
		 */
		ret = clk_register_clkdev(clk, list->name, NULL);
		if (ret)
			pr_err("%s: failed to register clock lookup for %s",
				__func__, list->name);
	}
}

/* register a list of fixed factor clocks */
163
void __init samsung_clk_register_fixed_factor(struct samsung_clk_provider *ctx,
164 165 166 167 168 169 170 171 172 173 174 175 176 177
		struct samsung_fixed_factor_clock *list, unsigned int nr_clk)
{
	struct clk *clk;
	unsigned int idx;

	for (idx = 0; idx < nr_clk; idx++, list++) {
		clk = clk_register_fixed_factor(NULL, list->name,
			list->parent_name, list->flags, list->mult, list->div);
		if (IS_ERR(clk)) {
			pr_err("%s: failed to register clock %s\n", __func__,
				list->name);
			continue;
		}

178
		samsung_clk_add_lookup(ctx, clk, list->id);
179 180 181 182
	}
}

/* register a list of mux clocks */
183 184 185
void __init samsung_clk_register_mux(struct samsung_clk_provider *ctx,
				struct samsung_mux_clock *list,
				unsigned int nr_clk)
186 187 188 189 190 191
{
	struct clk *clk;
	unsigned int idx, ret;

	for (idx = 0; idx < nr_clk; idx++, list++) {
		clk = clk_register_mux(NULL, list->name, list->parent_names,
192 193 194
			list->num_parents, list->flags,
			ctx->reg_base + list->offset,
			list->shift, list->width, list->mux_flags, &ctx->lock);
195 196 197 198 199 200
		if (IS_ERR(clk)) {
			pr_err("%s: failed to register clock %s\n", __func__,
				list->name);
			continue;
		}

201
		samsung_clk_add_lookup(ctx, clk, list->id);
202 203 204 205 206 207 208 209 210 211 212 213 214

		/* register a clock lookup only if a clock alias is specified */
		if (list->alias) {
			ret = clk_register_clkdev(clk, list->alias,
						list->dev_name);
			if (ret)
				pr_err("%s: failed to register lookup %s\n",
						__func__, list->alias);
		}
	}
}

/* register a list of div clocks */
215 216 217
void __init samsung_clk_register_div(struct samsung_clk_provider *ctx,
				struct samsung_div_clock *list,
				unsigned int nr_clk)
218 219 220 221 222
{
	struct clk *clk;
	unsigned int idx, ret;

	for (idx = 0; idx < nr_clk; idx++, list++) {
223 224
		if (list->table)
			clk = clk_register_divider_table(NULL, list->name,
225 226 227 228
				list->parent_name, list->flags,
				ctx->reg_base + list->offset,
				list->shift, list->width, list->div_flags,
				list->table, &ctx->lock);
229 230
		else
			clk = clk_register_divider(NULL, list->name,
231 232 233
				list->parent_name, list->flags,
				ctx->reg_base + list->offset, list->shift,
				list->width, list->div_flags, &ctx->lock);
234 235 236 237 238 239
		if (IS_ERR(clk)) {
			pr_err("%s: failed to register clock %s\n", __func__,
				list->name);
			continue;
		}

240
		samsung_clk_add_lookup(ctx, clk, list->id);
241 242 243 244 245 246 247 248 249 250 251 252 253

		/* register a clock lookup only if a clock alias is specified */
		if (list->alias) {
			ret = clk_register_clkdev(clk, list->alias,
						list->dev_name);
			if (ret)
				pr_err("%s: failed to register lookup %s\n",
						__func__, list->alias);
		}
	}
}

/* register a list of gate clocks */
254 255 256
void __init samsung_clk_register_gate(struct samsung_clk_provider *ctx,
				struct samsung_gate_clock *list,
				unsigned int nr_clk)
257 258 259 260 261 262
{
	struct clk *clk;
	unsigned int idx, ret;

	for (idx = 0; idx < nr_clk; idx++, list++) {
		clk = clk_register_gate(NULL, list->name, list->parent_name,
263 264
				list->flags, ctx->reg_base + list->offset,
				list->bit_idx, list->gate_flags, &ctx->lock);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		if (IS_ERR(clk)) {
			pr_err("%s: failed to register clock %s\n", __func__,
				list->name);
			continue;
		}

		/* register a clock lookup only if a clock alias is specified */
		if (list->alias) {
			ret = clk_register_clkdev(clk, list->alias,
							list->dev_name);
			if (ret)
				pr_err("%s: failed to register lookup %s\n",
					__func__, list->alias);
		}

280
		samsung_clk_add_lookup(ctx, clk, list->id);
281 282 283 284 285 286 287
	}
}

/*
 * obtain the clock speed of all external fixed clock sources from device
 * tree and register it
 */
288
void __init samsung_clk_of_register_fixed_ext(struct samsung_clk_provider *ctx,
289 290
			struct samsung_fixed_rate_clock *fixed_rate_clk,
			unsigned int nr_fixed_rate_clk,
291
			const struct of_device_id *clk_matches)
292 293
{
	const struct of_device_id *match;
294
	struct device_node *clk_np;
295 296
	u32 freq;

297 298
	for_each_matching_node_and_match(clk_np, clk_matches, &match) {
		if (of_property_read_u32(clk_np, "clock-frequency", &freq))
299
			continue;
300
		fixed_rate_clk[(unsigned long)match->data].fixed_rate = freq;
301
	}
302
	samsung_clk_register_fixed_rate(ctx, fixed_rate_clk, nr_fixed_rate_clk);
303 304 305 306 307 308 309
}

/* utility function to get the rate of a specified clock */
unsigned long _get_rate(const char *clk_name)
{
	struct clk *clk;

310 311
	clk = __clk_lookup(clk_name);
	if (!clk) {
312 313 314
		pr_err("%s: could not find clock %s\n", __func__, clk_name);
		return 0;
	}
315 316

	return clk_get_rate(clk);
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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

#ifdef CONFIG_PM_SLEEP
static int samsung_clk_suspend(void)
{
	struct samsung_clock_reg_cache *reg_cache;

	list_for_each_entry(reg_cache, &clock_reg_cache_list, node)
		samsung_clk_save(reg_cache->reg_base, reg_cache->rdump,
				reg_cache->rd_num);
	return 0;
}

static void samsung_clk_resume(void)
{
	struct samsung_clock_reg_cache *reg_cache;

	list_for_each_entry(reg_cache, &clock_reg_cache_list, node)
		samsung_clk_restore(reg_cache->reg_base, reg_cache->rdump,
				reg_cache->rd_num);
}

static struct syscore_ops samsung_clk_syscore_ops = {
	.suspend = samsung_clk_suspend,
	.resume = samsung_clk_resume,
};

static void samsung_clk_sleep_init(void __iomem *reg_base,
		const unsigned long *rdump,
		unsigned long nr_rdump)
{
	struct samsung_clock_reg_cache *reg_cache;

	reg_cache = kzalloc(sizeof(struct samsung_clock_reg_cache),
			GFP_KERNEL);
	if (!reg_cache)
		panic("could not allocate register reg_cache.\n");
	reg_cache->rdump = samsung_clk_alloc_reg_dump(rdump, nr_rdump);

	if (!reg_cache->rdump)
		panic("could not allocate register dump storage.\n");

	if (list_empty(&clock_reg_cache_list))
		register_syscore_ops(&samsung_clk_syscore_ops);

	reg_cache->reg_base = reg_base;
	reg_cache->rd_num = nr_rdump;
	list_add_tail(&reg_cache->node, &clock_reg_cache_list);
}

#else
static void samsung_clk_sleep_init(void __iomem *reg_base,
		const unsigned long *rdump,
		unsigned long nr_rdump) {}
#endif

/*
 * Common function which registers plls, muxes, dividers and gates
 * for each CMU. It also add CMU register list to register cache.
 */
377 378
struct samsung_clk_provider * __init samsung_cmu_register_one(
			struct device_node *np,
379 380 381 382 383 384
			struct samsung_cmu_info *cmu)
{
	void __iomem *reg_base;
	struct samsung_clk_provider *ctx;

	reg_base = of_iomap(np, 0);
385
	if (!reg_base) {
386
		panic("%s: failed to map registers\n", __func__);
387 388
		return NULL;
	}
389 390

	ctx = samsung_clk_init(np, reg_base, cmu->nr_clk_ids);
391
	if (!ctx) {
392
		panic("%s: unable to allocate ctx\n", __func__);
393 394
		return ctx;
	}
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

	if (cmu->pll_clks)
		samsung_clk_register_pll(ctx, cmu->pll_clks, cmu->nr_pll_clks,
			reg_base);
	if (cmu->mux_clks)
		samsung_clk_register_mux(ctx, cmu->mux_clks,
			cmu->nr_mux_clks);
	if (cmu->div_clks)
		samsung_clk_register_div(ctx, cmu->div_clks, cmu->nr_div_clks);
	if (cmu->gate_clks)
		samsung_clk_register_gate(ctx, cmu->gate_clks,
			cmu->nr_gate_clks);
	if (cmu->fixed_clks)
		samsung_clk_register_fixed_rate(ctx, cmu->fixed_clks,
			cmu->nr_fixed_clks);
410 411 412
	if (cmu->fixed_factor_clks)
		samsung_clk_register_fixed_factor(ctx, cmu->fixed_factor_clks,
			cmu->nr_fixed_factor_clks);
413 414 415 416 417
	if (cmu->clk_regs)
		samsung_clk_sleep_init(reg_base, cmu->clk_regs,
			cmu->nr_clk_regs);

	samsung_clk_of_add_provider(np, ctx);
418 419

	return ctx;
420
}