clk-peripheral.c 12.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5
/*
 *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
 */

6
#include <linux/bitops.h>
7 8 9 10
#include <linux/clk-provider.h>
#include <linux/clkdev.h>
#include <linux/clk/at91_pmc.h>
#include <linux/of.h>
11 12
#include <linux/mfd/syscon.h>
#include <linux/regmap.h>
13 14 15

#include "pmc.h"

16 17
DEFINE_SPINLOCK(pmc_pcr_lock);

18 19 20 21
#define PERIPHERAL_ID_MIN	2
#define PERIPHERAL_ID_MAX	31
#define PERIPHERAL_MASK(id)	(1 << ((id) & PERIPHERAL_ID_MAX))

22
#define PERIPHERAL_MAX_SHIFT	3
23 24 25

struct clk_peripheral {
	struct clk_hw hw;
26
	struct regmap *regmap;
27 28 29 30 31 32 33
	u32 id;
};

#define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)

struct clk_sam9x5_peripheral {
	struct clk_hw hw;
34
	struct regmap *regmap;
35
	struct clk_range range;
36
	spinlock_t *lock;
37 38
	u32 id;
	u32 div;
39
	const struct clk_pcr_layout *layout;
40
	struct at91_clk_pms pms;
41
	bool auto_div;
42
	int chg_pid;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
};

#define to_clk_sam9x5_peripheral(hw) \
	container_of(hw, struct clk_sam9x5_peripheral, hw)

static int clk_peripheral_enable(struct clk_hw *hw)
{
	struct clk_peripheral *periph = to_clk_peripheral(hw);
	int offset = AT91_PMC_PCER;
	u32 id = periph->id;

	if (id < PERIPHERAL_ID_MIN)
		return 0;
	if (id > PERIPHERAL_ID_MAX)
		offset = AT91_PMC_PCER1;
58 59
	regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));

60 61 62 63 64 65 66 67 68 69 70 71 72
	return 0;
}

static void clk_peripheral_disable(struct clk_hw *hw)
{
	struct clk_peripheral *periph = to_clk_peripheral(hw);
	int offset = AT91_PMC_PCDR;
	u32 id = periph->id;

	if (id < PERIPHERAL_ID_MIN)
		return;
	if (id > PERIPHERAL_ID_MAX)
		offset = AT91_PMC_PCDR1;
73
	regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
74 75 76 77 78 79
}

static int clk_peripheral_is_enabled(struct clk_hw *hw)
{
	struct clk_peripheral *periph = to_clk_peripheral(hw);
	int offset = AT91_PMC_PCSR;
80
	unsigned int status;
81 82 83 84 85 86
	u32 id = periph->id;

	if (id < PERIPHERAL_ID_MIN)
		return 1;
	if (id > PERIPHERAL_ID_MAX)
		offset = AT91_PMC_PCSR1;
87 88 89
	regmap_read(periph->regmap, offset, &status);

	return status & PERIPHERAL_MASK(id) ? 1 : 0;
90 91 92 93 94 95 96 97
}

static const struct clk_ops peripheral_ops = {
	.enable = clk_peripheral_enable,
	.disable = clk_peripheral_disable,
	.is_enabled = clk_peripheral_is_enabled,
};

98
struct clk_hw * __init
99
at91_clk_register_peripheral(struct regmap *regmap, const char *name,
100 101
			     const char *parent_name, struct clk_hw *parent_hw,
			     u32 id)
102 103
{
	struct clk_peripheral *periph;
104
	struct clk_init_data init = {};
105 106
	struct clk_hw *hw;
	int ret;
107

108
	if (!name || !(parent_name || parent_hw) || id > PERIPHERAL_ID_MAX)
109 110 111 112 113 114 115 116
		return ERR_PTR(-EINVAL);

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

	init.name = name;
	init.ops = &peripheral_ops;
117 118 119 120
	if (parent_hw)
		init.parent_hws = (const struct clk_hw **)&parent_hw;
	else
		init.parent_names = &parent_name;
121
	init.num_parents = 1;
122 123 124 125
	init.flags = 0;

	periph->id = id;
	periph->hw.init = &init;
126
	periph->regmap = regmap;
127

128 129 130
	hw = &periph->hw;
	ret = clk_hw_register(NULL, &periph->hw);
	if (ret) {
131
		kfree(periph);
132 133
		hw = ERR_PTR(ret);
	}
134

135
	return hw;
136 137 138 139
}

static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
{
140
	struct clk_hw *parent;
141 142 143 144 145 146 147
	unsigned long parent_rate;
	int shift = 0;

	if (!periph->auto_div)
		return;

	if (periph->range.max) {
148 149
		parent = clk_hw_get_parent_by_index(&periph->hw, 0);
		parent_rate = clk_hw_get_rate(parent);
150 151 152 153 154 155 156 157 158 159 160 161 162
		if (!parent_rate)
			return;

		for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
			if (parent_rate >> shift <= periph->range.max)
				break;
		}
	}

	periph->auto_div = false;
	periph->div = shift;
}

163 164
static int clk_sam9x5_peripheral_set(struct clk_sam9x5_peripheral *periph,
				     unsigned int status)
165
{
166
	unsigned long flags;
167
	unsigned int enable = status ? AT91_PMC_PCR_EN : 0;
168 169 170 171

	if (periph->id < PERIPHERAL_ID_MIN)
		return 0;

172
	spin_lock_irqsave(periph->lock, flags);
173 174 175 176
	regmap_write(periph->regmap, periph->layout->offset,
		     (periph->id & periph->layout->pid_mask));
	regmap_update_bits(periph->regmap, periph->layout->offset,
			   periph->layout->div_mask | periph->layout->cmd |
177
			   enable,
178
			   field_prep(periph->layout->div_mask, periph->div) |
179
			   periph->layout->cmd | enable);
180 181
	spin_unlock_irqrestore(periph->lock, flags);

182 183 184
	return 0;
}

185 186 187 188 189 190 191
static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);

	return clk_sam9x5_peripheral_set(periph, 1);
}

192 193 194
static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
195
	unsigned long flags;
196 197 198 199

	if (periph->id < PERIPHERAL_ID_MIN)
		return;

200
	spin_lock_irqsave(periph->lock, flags);
201 202 203 204 205
	regmap_write(periph->regmap, periph->layout->offset,
		     (periph->id & periph->layout->pid_mask));
	regmap_update_bits(periph->regmap, periph->layout->offset,
			   AT91_PMC_PCR_EN | periph->layout->cmd,
			   periph->layout->cmd);
206
	spin_unlock_irqrestore(periph->lock, flags);
207 208 209 210 211
}

static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
212 213
	unsigned long flags;
	unsigned int status;
214 215 216 217

	if (periph->id < PERIPHERAL_ID_MIN)
		return 1;

218
	spin_lock_irqsave(periph->lock, flags);
219 220 221
	regmap_write(periph->regmap, periph->layout->offset,
		     (periph->id & periph->layout->pid_mask));
	regmap_read(periph->regmap, periph->layout->offset, &status);
222
	spin_unlock_irqrestore(periph->lock, flags);
223

224
	return !!(status & AT91_PMC_PCR_EN);
225 226 227 228 229 230 231
}

static unsigned long
clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
				  unsigned long parent_rate)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
232 233
	unsigned long flags;
	unsigned int status;
234 235 236 237

	if (periph->id < PERIPHERAL_ID_MIN)
		return parent_rate;

238
	spin_lock_irqsave(periph->lock, flags);
239 240 241
	regmap_write(periph->regmap, periph->layout->offset,
		     (periph->id & periph->layout->pid_mask));
	regmap_read(periph->regmap, periph->layout->offset, &status);
242
	spin_unlock_irqrestore(periph->lock, flags);
243

244
	if (status & AT91_PMC_PCR_EN) {
245
		periph->div = field_get(periph->layout->div_mask, status);
246 247 248 249 250 251 252 253
		periph->auto_div = false;
	} else {
		clk_sam9x5_peripheral_autodiv(periph);
	}

	return parent_rate >> periph->div;
}

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
static void clk_sam9x5_peripheral_best_diff(struct clk_rate_request *req,
					    struct clk_hw *parent,
					    unsigned long parent_rate,
					    u32 shift, long *best_diff,
					    long *best_rate)
{
	unsigned long tmp_rate = parent_rate >> shift;
	unsigned long tmp_diff = abs(req->rate - tmp_rate);

	if (*best_diff < 0 || *best_diff >= tmp_diff) {
		*best_rate = tmp_rate;
		*best_diff = tmp_diff;
		req->best_parent_rate = parent_rate;
		req->best_parent_hw = parent;
	}
}

static int clk_sam9x5_peripheral_determine_rate(struct clk_hw *hw,
						struct clk_rate_request *req)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
	struct clk_hw *parent = clk_hw_get_parent(hw);
	unsigned long parent_rate = clk_hw_get_rate(parent);
	unsigned long tmp_rate;
	long best_rate = LONG_MIN;
	long best_diff = LONG_MIN;
	u32 shift;

	if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
		return parent_rate;

	/* Fist step: check the available dividers. */
	for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
		tmp_rate = parent_rate >> shift;

		if (periph->range.max && tmp_rate > periph->range.max)
			continue;

		clk_sam9x5_peripheral_best_diff(req, parent, parent_rate,
						shift, &best_diff, &best_rate);

		if (!best_diff || best_rate <= req->rate)
			break;
	}

	if (periph->chg_pid < 0)
		goto end;

	/* Step two: try to request rate from parent. */
	parent = clk_hw_get_parent_by_index(hw, periph->chg_pid);
	if (!parent)
		goto end;

	for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
308
		struct clk_rate_request req_parent;
309

310
		clk_hw_forward_rate_request(hw, req, parent, &req_parent, req->rate << shift);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
		if (__clk_determine_rate(parent, &req_parent))
			continue;

		clk_sam9x5_peripheral_best_diff(req, parent, req_parent.rate,
						shift, &best_diff, &best_rate);

		if (!best_diff)
			break;
	}
end:
	if (best_rate < 0 ||
	    (periph->range.max && best_rate > periph->range.max))
		return -EINVAL;

	pr_debug("PCK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
		 __func__, best_rate,
		 __clk_get_name((req->best_parent_hw)->clk),
		 req->best_parent_rate);

	req->rate = best_rate;

	return 0;
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
					     unsigned long rate,
					     unsigned long *parent_rate)
{
	int shift = 0;
	unsigned long best_rate;
	unsigned long best_diff;
	unsigned long cur_rate = *parent_rate;
	unsigned long cur_diff;
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);

	if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
		return *parent_rate;

	if (periph->range.max) {
350
		for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
351 352 353 354 355 356 357 358 359 360 361
			cur_rate = *parent_rate >> shift;
			if (cur_rate <= periph->range.max)
				break;
		}
	}

	if (rate >= cur_rate)
		return cur_rate;

	best_diff = cur_rate - rate;
	best_rate = cur_rate;
362
	for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		cur_rate = *parent_rate >> shift;
		if (cur_rate < rate)
			cur_diff = rate - cur_rate;
		else
			cur_diff = cur_rate - rate;

		if (cur_diff < best_diff) {
			best_diff = cur_diff;
			best_rate = cur_rate;
		}

		if (!best_diff || cur_rate < rate)
			break;
	}

	return best_rate;
}

static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
					  unsigned long rate,
					  unsigned long parent_rate)
{
	int shift;
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
	if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
		if (parent_rate == rate)
			return 0;
		else
			return -EINVAL;
	}

	if (periph->range.max && rate > periph->range.max)
		return -EINVAL;

397
	for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
398 399 400 401 402 403 404 405 406 407
		if (parent_rate >> shift == rate) {
			periph->auto_div = false;
			periph->div = shift;
			return 0;
		}
	}

	return -EINVAL;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static int clk_sam9x5_peripheral_save_context(struct clk_hw *hw)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);

	periph->pms.status = clk_sam9x5_peripheral_is_enabled(hw);

	return 0;
}

static void clk_sam9x5_peripheral_restore_context(struct clk_hw *hw)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);

	if (periph->pms.status)
		clk_sam9x5_peripheral_set(periph, periph->pms.status);
}

425 426 427 428 429 430 431
static const struct clk_ops sam9x5_peripheral_ops = {
	.enable = clk_sam9x5_peripheral_enable,
	.disable = clk_sam9x5_peripheral_disable,
	.is_enabled = clk_sam9x5_peripheral_is_enabled,
	.recalc_rate = clk_sam9x5_peripheral_recalc_rate,
	.round_rate = clk_sam9x5_peripheral_round_rate,
	.set_rate = clk_sam9x5_peripheral_set_rate,
432 433
	.save_context = clk_sam9x5_peripheral_save_context,
	.restore_context = clk_sam9x5_peripheral_restore_context,
434 435
};

436 437 438 439 440 441 442
static const struct clk_ops sam9x5_peripheral_chg_ops = {
	.enable = clk_sam9x5_peripheral_enable,
	.disable = clk_sam9x5_peripheral_disable,
	.is_enabled = clk_sam9x5_peripheral_is_enabled,
	.recalc_rate = clk_sam9x5_peripheral_recalc_rate,
	.determine_rate = clk_sam9x5_peripheral_determine_rate,
	.set_rate = clk_sam9x5_peripheral_set_rate,
443 444
	.save_context = clk_sam9x5_peripheral_save_context,
	.restore_context = clk_sam9x5_peripheral_restore_context,
445 446
};

447
struct clk_hw * __init
448
at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
449
				    const struct clk_pcr_layout *layout,
450
				    const char *name, const char *parent_name,
451
				    struct clk_hw *parent_hw,
452
				    u32 id, const struct clk_range *range,
453
				    int chg_pid, unsigned long flags)
454 455
{
	struct clk_sam9x5_peripheral *periph;
456
	struct clk_init_data init = {};
457 458
	struct clk_hw *hw;
	int ret;
459

460
	if (!name || !(parent_name || parent_hw))
461 462 463 464 465 466 467
		return ERR_PTR(-EINVAL);

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

	init.name = name;
468 469 470 471
	if (parent_hw)
		init.parent_hws = (const struct clk_hw **)&parent_hw;
	else
		init.parent_names = &parent_name;
472
	init.num_parents = 1;
473
	init.flags = flags;
474 475 476
	if (chg_pid < 0) {
		init.ops = &sam9x5_peripheral_ops;
	} else {
477 478
		init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
			      CLK_SET_RATE_PARENT;
479 480
		init.ops = &sam9x5_peripheral_chg_ops;
	}
481 482 483 484

	periph->id = id;
	periph->hw.init = &init;
	periph->div = 0;
485 486
	periph->regmap = regmap;
	periph->lock = lock;
487 488 489
	if (layout->div_mask)
		periph->auto_div = true;
	periph->layout = layout;
490
	periph->range = *range;
491
	periph->chg_pid = chg_pid;
492

493 494 495
	hw = &periph->hw;
	ret = clk_hw_register(NULL, &periph->hw);
	if (ret) {
496
		kfree(periph);
497
		hw = ERR_PTR(ret);
498
	} else {
499
		clk_sam9x5_peripheral_autodiv(periph);
500
	}
501

502
	return hw;
503
}