aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/adi/clk-adi-pll.c
blob: 372baa9c11b5d3802ea2ca4670da554ffd19d85b (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * (C) Copyright 2022 - Analog Devices, Inc.
 *
 * Written and/or maintained by Timesys Corporation
 *
 * Author: Greg Malysa <greg.malysa@timesys.com>
 *
 * Ported from Linux: Nathan Barrett-Morrison <nathan.morrison@timesys.com>
 */

#include <clk.h>
#include <clk-uclass.h>
#include <asm/io.h>
#include <dm/device.h>
#include <linux/compiler_types.h>
#include <linux/err.h>
#include <linux/kernel.h>

#include "clk.h"

#define ADI_CLK_PLL_GENERIC "adi_clk_pll_generic"

struct clk_sc5xx_cgu_pll {
	struct clk clk;
	void __iomem *base;
	u32 mask;
	u32 max;
	u32 m_offset;
	u8 shift;
	bool half_m;
};

#define to_clk_sc5xx_cgu_pll(_clk) container_of(_clk, struct clk_sc5xx_cgu_pll, clk)

static unsigned long sc5xx_cgu_pll_get_rate(struct clk *clk)
{
	struct clk_sc5xx_cgu_pll *pll = to_clk_sc5xx_cgu_pll(dev_get_clk_ptr(clk->dev));
	unsigned long parent_rate = clk_get_parent_rate(clk);

	u32 reg = readl(pll->base);
	u32 m = ((reg & pll->mask) >> pll->shift) + pll->m_offset;

	if (m == 0)
		m = pll->max;

	if (pll->half_m)
		return parent_rate * m * 2;
	return parent_rate * m;
}

static const struct clk_ops clk_sc5xx_cgu_pll_ops = {
	.get_rate = sc5xx_cgu_pll_get_rate,
};

struct clk *sc5xx_cgu_pll(const char *name, const char *parent_name,
			  void __iomem *base, u8 shift, u8 width, u32 m_offset,
			  bool half_m)
{
	struct clk_sc5xx_cgu_pll *pll;
	struct clk *clk;
	int ret;
	char *drv_name = ADI_CLK_PLL_GENERIC;

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

	pll->base = base;
	pll->shift = shift;
	pll->mask = GENMASK(width - 1, 0) << shift;
	pll->max = pll->mask + 1;
	pll->m_offset = m_offset;
	pll->half_m = half_m;

	clk = &pll->clk;

	ret = clk_register(clk, drv_name, name, parent_name);
	if (ret) {
		pr_err("Failed to register %s in %s: %d\n", name, __func__, ret);
		kfree(pll);
		return ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(clk_adi_pll_generic) = {
	.name	= ADI_CLK_PLL_GENERIC,
	.id	= UCLASS_CLK,
	.ops	= &clk_sc5xx_cgu_pll_ops,
	.flags = DM_FLAG_PRE_RELOC,
};