xref: /linux/drivers/clk/x86/clk-cgu-pll.c (revision eaabee88)
1d058fd9eSRahul Tanwar // SPDX-License-Identifier: GPL-2.0
2d058fd9eSRahul Tanwar /*
3*03617731SRahul Tanwar  * Copyright (C) 2020-2022 MaxLinear, Inc.
4d058fd9eSRahul Tanwar  * Copyright (C) 2020 Intel Corporation.
5*03617731SRahul Tanwar  * Zhu Yixin <yzhu@maxlinear.com>
6*03617731SRahul Tanwar  * Rahul Tanwar <rtanwar@maxlinear.com>
7d058fd9eSRahul Tanwar  */
8d058fd9eSRahul Tanwar 
9d058fd9eSRahul Tanwar #include <linux/clk-provider.h>
10d058fd9eSRahul Tanwar #include <linux/delay.h>
11d058fd9eSRahul Tanwar #include <linux/device.h>
12d058fd9eSRahul Tanwar #include <linux/iopoll.h>
13d058fd9eSRahul Tanwar #include <linux/of.h>
14d058fd9eSRahul Tanwar 
15d058fd9eSRahul Tanwar #include "clk-cgu.h"
16d058fd9eSRahul Tanwar 
17d058fd9eSRahul Tanwar #define to_lgm_clk_pll(_hw)	container_of(_hw, struct lgm_clk_pll, hw)
18d058fd9eSRahul Tanwar #define PLL_REF_DIV(x)		((x) + 0x08)
19d058fd9eSRahul Tanwar 
20d058fd9eSRahul Tanwar /*
21d058fd9eSRahul Tanwar  * Calculate formula:
22d058fd9eSRahul Tanwar  * rate = (prate * mult + (prate * frac) / frac_div) / div
23d058fd9eSRahul Tanwar  */
24d058fd9eSRahul Tanwar static unsigned long
lgm_pll_calc_rate(unsigned long prate,unsigned int mult,unsigned int div,unsigned int frac,unsigned int frac_div)25d058fd9eSRahul Tanwar lgm_pll_calc_rate(unsigned long prate, unsigned int mult,
26d058fd9eSRahul Tanwar 		  unsigned int div, unsigned int frac, unsigned int frac_div)
27d058fd9eSRahul Tanwar {
28d058fd9eSRahul Tanwar 	u64 crate, frate, rate64;
29d058fd9eSRahul Tanwar 
30d058fd9eSRahul Tanwar 	rate64 = prate;
31d058fd9eSRahul Tanwar 	crate = rate64 * mult;
32d058fd9eSRahul Tanwar 	frate = rate64 * frac;
33d058fd9eSRahul Tanwar 	do_div(frate, frac_div);
34d058fd9eSRahul Tanwar 	crate += frate;
35d058fd9eSRahul Tanwar 	do_div(crate, div);
36d058fd9eSRahul Tanwar 
37d058fd9eSRahul Tanwar 	return crate;
38d058fd9eSRahul Tanwar }
39d058fd9eSRahul Tanwar 
lgm_pll_recalc_rate(struct clk_hw * hw,unsigned long prate)40d058fd9eSRahul Tanwar static unsigned long lgm_pll_recalc_rate(struct clk_hw *hw, unsigned long prate)
41d058fd9eSRahul Tanwar {
42d058fd9eSRahul Tanwar 	struct lgm_clk_pll *pll = to_lgm_clk_pll(hw);
43d058fd9eSRahul Tanwar 	unsigned int div, mult, frac;
44d058fd9eSRahul Tanwar 
45d058fd9eSRahul Tanwar 	mult = lgm_get_clk_val(pll->membase, PLL_REF_DIV(pll->reg), 0, 12);
46d058fd9eSRahul Tanwar 	div = lgm_get_clk_val(pll->membase, PLL_REF_DIV(pll->reg), 18, 6);
47d058fd9eSRahul Tanwar 	frac = lgm_get_clk_val(pll->membase, pll->reg, 2, 24);
48d058fd9eSRahul Tanwar 
49d058fd9eSRahul Tanwar 	if (pll->type == TYPE_LJPLL)
50d058fd9eSRahul Tanwar 		div *= 4;
51d058fd9eSRahul Tanwar 
52d058fd9eSRahul Tanwar 	return lgm_pll_calc_rate(prate, mult, div, frac, BIT(24));
53d058fd9eSRahul Tanwar }
54d058fd9eSRahul Tanwar 
lgm_pll_is_enabled(struct clk_hw * hw)55d058fd9eSRahul Tanwar static int lgm_pll_is_enabled(struct clk_hw *hw)
56d058fd9eSRahul Tanwar {
57d058fd9eSRahul Tanwar 	struct lgm_clk_pll *pll = to_lgm_clk_pll(hw);
58d058fd9eSRahul Tanwar 	unsigned int ret;
59d058fd9eSRahul Tanwar 
60d058fd9eSRahul Tanwar 	ret = lgm_get_clk_val(pll->membase, pll->reg, 0, 1);
61d058fd9eSRahul Tanwar 
62d058fd9eSRahul Tanwar 	return ret;
63d058fd9eSRahul Tanwar }
64d058fd9eSRahul Tanwar 
lgm_pll_enable(struct clk_hw * hw)65d058fd9eSRahul Tanwar static int lgm_pll_enable(struct clk_hw *hw)
66d058fd9eSRahul Tanwar {
67d058fd9eSRahul Tanwar 	struct lgm_clk_pll *pll = to_lgm_clk_pll(hw);
68d058fd9eSRahul Tanwar 	u32 val;
69d058fd9eSRahul Tanwar 	int ret;
70d058fd9eSRahul Tanwar 
71d058fd9eSRahul Tanwar 	lgm_set_clk_val(pll->membase, pll->reg, 0, 1, 1);
72*03617731SRahul Tanwar 	ret = regmap_read_poll_timeout_atomic(pll->membase, pll->reg,
73d058fd9eSRahul Tanwar 					      val, (val & 0x1), 1, 100);
74*03617731SRahul Tanwar 
75d058fd9eSRahul Tanwar 
76d058fd9eSRahul Tanwar 	return ret;
77d058fd9eSRahul Tanwar }
78d058fd9eSRahul Tanwar 
lgm_pll_disable(struct clk_hw * hw)79d058fd9eSRahul Tanwar static void lgm_pll_disable(struct clk_hw *hw)
80d058fd9eSRahul Tanwar {
81d058fd9eSRahul Tanwar 	struct lgm_clk_pll *pll = to_lgm_clk_pll(hw);
82d058fd9eSRahul Tanwar 
83d058fd9eSRahul Tanwar 	lgm_set_clk_val(pll->membase, pll->reg, 0, 1, 0);
84d058fd9eSRahul Tanwar }
85d058fd9eSRahul Tanwar 
86d058fd9eSRahul Tanwar static const struct clk_ops lgm_pll_ops = {
87d058fd9eSRahul Tanwar 	.recalc_rate = lgm_pll_recalc_rate,
88d058fd9eSRahul Tanwar 	.is_enabled = lgm_pll_is_enabled,
89d058fd9eSRahul Tanwar 	.enable = lgm_pll_enable,
90d058fd9eSRahul Tanwar 	.disable = lgm_pll_disable,
91d058fd9eSRahul Tanwar };
92d058fd9eSRahul Tanwar 
93d058fd9eSRahul Tanwar static struct clk_hw *
lgm_clk_register_pll(struct lgm_clk_provider * ctx,const struct lgm_pll_clk_data * list)94d058fd9eSRahul Tanwar lgm_clk_register_pll(struct lgm_clk_provider *ctx,
95d058fd9eSRahul Tanwar 		     const struct lgm_pll_clk_data *list)
96d058fd9eSRahul Tanwar {
97d058fd9eSRahul Tanwar 	struct clk_init_data init = {};
98d058fd9eSRahul Tanwar 	struct lgm_clk_pll *pll;
99d058fd9eSRahul Tanwar 	struct device *dev = ctx->dev;
100d058fd9eSRahul Tanwar 	struct clk_hw *hw;
101d058fd9eSRahul Tanwar 	int ret;
102d058fd9eSRahul Tanwar 
103d058fd9eSRahul Tanwar 	init.ops = &lgm_pll_ops;
104d058fd9eSRahul Tanwar 	init.name = list->name;
105d058fd9eSRahul Tanwar 	init.flags = list->flags;
106d058fd9eSRahul Tanwar 	init.parent_data = list->parent_data;
107d058fd9eSRahul Tanwar 	init.num_parents = list->num_parents;
108d058fd9eSRahul Tanwar 
109d058fd9eSRahul Tanwar 	pll = devm_kzalloc(dev, sizeof(*pll), GFP_KERNEL);
110d058fd9eSRahul Tanwar 	if (!pll)
111d058fd9eSRahul Tanwar 		return ERR_PTR(-ENOMEM);
112d058fd9eSRahul Tanwar 
113d058fd9eSRahul Tanwar 	pll->membase = ctx->membase;
114d058fd9eSRahul Tanwar 	pll->reg = list->reg;
115d058fd9eSRahul Tanwar 	pll->flags = list->flags;
116d058fd9eSRahul Tanwar 	pll->type = list->type;
117d058fd9eSRahul Tanwar 	pll->hw.init = &init;
118d058fd9eSRahul Tanwar 
119d058fd9eSRahul Tanwar 	hw = &pll->hw;
1208529fc0aSRahul Tanwar 	ret = devm_clk_hw_register(dev, hw);
121d058fd9eSRahul Tanwar 	if (ret)
122d058fd9eSRahul Tanwar 		return ERR_PTR(ret);
123d058fd9eSRahul Tanwar 
124d058fd9eSRahul Tanwar 	return hw;
125d058fd9eSRahul Tanwar }
126d058fd9eSRahul Tanwar 
lgm_clk_register_plls(struct lgm_clk_provider * ctx,const struct lgm_pll_clk_data * list,unsigned int nr_clk)127d058fd9eSRahul Tanwar int lgm_clk_register_plls(struct lgm_clk_provider *ctx,
128d058fd9eSRahul Tanwar 			  const struct lgm_pll_clk_data *list,
129d058fd9eSRahul Tanwar 			  unsigned int nr_clk)
130d058fd9eSRahul Tanwar {
131d058fd9eSRahul Tanwar 	struct clk_hw *hw;
132d058fd9eSRahul Tanwar 	int i;
133d058fd9eSRahul Tanwar 
134d058fd9eSRahul Tanwar 	for (i = 0; i < nr_clk; i++, list++) {
135d058fd9eSRahul Tanwar 		hw = lgm_clk_register_pll(ctx, list);
136d058fd9eSRahul Tanwar 		if (IS_ERR(hw)) {
137d058fd9eSRahul Tanwar 			dev_err(ctx->dev, "failed to register pll: %s\n",
138d058fd9eSRahul Tanwar 				list->name);
139d058fd9eSRahul Tanwar 			return PTR_ERR(hw);
140d058fd9eSRahul Tanwar 		}
141d058fd9eSRahul Tanwar 		ctx->clk_data.hws[list->id] = hw;
142d058fd9eSRahul Tanwar 	}
143d058fd9eSRahul Tanwar 
144d058fd9eSRahul Tanwar 	return 0;
145d058fd9eSRahul Tanwar }
146