xref: /linux/drivers/clk/spear/clk-gpt-synth.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*3bb16560SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a45896bdSViresh Kumar /*
3a45896bdSViresh Kumar  * Copyright (C) 2012 ST Microelectronics
4da89947bSViresh Kumar  * Viresh Kumar <vireshk@kernel.org>
5a45896bdSViresh Kumar  *
6a45896bdSViresh Kumar  * General Purpose Timer Synthesizer clock implementation
7a45896bdSViresh Kumar  */
8a45896bdSViresh Kumar 
9a45896bdSViresh Kumar #define pr_fmt(fmt) "clk-gpt-synth: " fmt
10a45896bdSViresh Kumar 
11a45896bdSViresh Kumar #include <linux/clk-provider.h>
12a45896bdSViresh Kumar #include <linux/slab.h>
13a45896bdSViresh Kumar #include <linux/io.h>
14a45896bdSViresh Kumar #include <linux/err.h>
15a45896bdSViresh Kumar #include "clk.h"
16a45896bdSViresh Kumar 
17a45896bdSViresh Kumar #define GPT_MSCALE_MASK		0xFFF
18a45896bdSViresh Kumar #define GPT_NSCALE_SHIFT	12
19a45896bdSViresh Kumar #define GPT_NSCALE_MASK		0xF
20a45896bdSViresh Kumar 
21a45896bdSViresh Kumar /*
22a45896bdSViresh Kumar  * DOC: General Purpose Timer Synthesizer clock
23a45896bdSViresh Kumar  *
24a45896bdSViresh Kumar  * Calculates gpt synth clk rate for different values of mscale and nscale
25a45896bdSViresh Kumar  *
26a45896bdSViresh Kumar  * Fout= Fin/((2 ^ (N+1)) * (M+1))
27a45896bdSViresh Kumar  */
28a45896bdSViresh Kumar 
29a45896bdSViresh Kumar #define to_clk_gpt(_hw) container_of(_hw, struct clk_gpt, hw)
30a45896bdSViresh Kumar 
gpt_calc_rate(struct clk_hw * hw,unsigned long prate,int index)31a45896bdSViresh Kumar static unsigned long gpt_calc_rate(struct clk_hw *hw, unsigned long prate,
32a45896bdSViresh Kumar 		int index)
33a45896bdSViresh Kumar {
34a45896bdSViresh Kumar 	struct clk_gpt *gpt = to_clk_gpt(hw);
35a45896bdSViresh Kumar 	struct gpt_rate_tbl *rtbl = gpt->rtbl;
36a45896bdSViresh Kumar 
37a45896bdSViresh Kumar 	prate /= ((1 << (rtbl[index].nscale + 1)) * (rtbl[index].mscale + 1));
38a45896bdSViresh Kumar 
39a45896bdSViresh Kumar 	return prate;
40a45896bdSViresh Kumar }
41a45896bdSViresh Kumar 
clk_gpt_round_rate(struct clk_hw * hw,unsigned long drate,unsigned long * prate)42a45896bdSViresh Kumar static long clk_gpt_round_rate(struct clk_hw *hw, unsigned long drate,
43a45896bdSViresh Kumar 		unsigned long *prate)
44a45896bdSViresh Kumar {
45a45896bdSViresh Kumar 	struct clk_gpt *gpt = to_clk_gpt(hw);
46a45896bdSViresh Kumar 	int unused;
47a45896bdSViresh Kumar 
48a45896bdSViresh Kumar 	return clk_round_rate_index(hw, drate, *prate, gpt_calc_rate,
49a45896bdSViresh Kumar 			gpt->rtbl_cnt, &unused);
50a45896bdSViresh Kumar }
51a45896bdSViresh Kumar 
clk_gpt_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)52a45896bdSViresh Kumar static unsigned long clk_gpt_recalc_rate(struct clk_hw *hw,
53a45896bdSViresh Kumar 		unsigned long parent_rate)
54a45896bdSViresh Kumar {
55a45896bdSViresh Kumar 	struct clk_gpt *gpt = to_clk_gpt(hw);
56a45896bdSViresh Kumar 	unsigned long flags = 0;
57a45896bdSViresh Kumar 	unsigned int div = 1, val;
58a45896bdSViresh Kumar 
59a45896bdSViresh Kumar 	if (gpt->lock)
60a45896bdSViresh Kumar 		spin_lock_irqsave(gpt->lock, flags);
61a45896bdSViresh Kumar 
62a45896bdSViresh Kumar 	val = readl_relaxed(gpt->reg);
63a45896bdSViresh Kumar 
64a45896bdSViresh Kumar 	if (gpt->lock)
65a45896bdSViresh Kumar 		spin_unlock_irqrestore(gpt->lock, flags);
66a45896bdSViresh Kumar 
67a45896bdSViresh Kumar 	div += val & GPT_MSCALE_MASK;
68a45896bdSViresh Kumar 	div *= 1 << (((val >> GPT_NSCALE_SHIFT) & GPT_NSCALE_MASK) + 1);
69a45896bdSViresh Kumar 
70a45896bdSViresh Kumar 	if (!div)
71a45896bdSViresh Kumar 		return 0;
72a45896bdSViresh Kumar 
73a45896bdSViresh Kumar 	return parent_rate / div;
74a45896bdSViresh Kumar }
75a45896bdSViresh Kumar 
76a45896bdSViresh Kumar /* Configures new clock rate of gpt */
clk_gpt_set_rate(struct clk_hw * hw,unsigned long drate,unsigned long prate)77a45896bdSViresh Kumar static int clk_gpt_set_rate(struct clk_hw *hw, unsigned long drate,
78a45896bdSViresh Kumar 				unsigned long prate)
79a45896bdSViresh Kumar {
80a45896bdSViresh Kumar 	struct clk_gpt *gpt = to_clk_gpt(hw);
81a45896bdSViresh Kumar 	struct gpt_rate_tbl *rtbl = gpt->rtbl;
82a45896bdSViresh Kumar 	unsigned long flags = 0, val;
83a45896bdSViresh Kumar 	int i;
84a45896bdSViresh Kumar 
85a45896bdSViresh Kumar 	clk_round_rate_index(hw, drate, prate, gpt_calc_rate, gpt->rtbl_cnt,
86a45896bdSViresh Kumar 			&i);
87a45896bdSViresh Kumar 
88a45896bdSViresh Kumar 	if (gpt->lock)
89a45896bdSViresh Kumar 		spin_lock_irqsave(gpt->lock, flags);
90a45896bdSViresh Kumar 
91a45896bdSViresh Kumar 	val = readl(gpt->reg) & ~GPT_MSCALE_MASK;
92a45896bdSViresh Kumar 	val &= ~(GPT_NSCALE_MASK << GPT_NSCALE_SHIFT);
93a45896bdSViresh Kumar 
94a45896bdSViresh Kumar 	val |= rtbl[i].mscale & GPT_MSCALE_MASK;
95a45896bdSViresh Kumar 	val |= (rtbl[i].nscale & GPT_NSCALE_MASK) << GPT_NSCALE_SHIFT;
96a45896bdSViresh Kumar 
97a45896bdSViresh Kumar 	writel_relaxed(val, gpt->reg);
98a45896bdSViresh Kumar 
99a45896bdSViresh Kumar 	if (gpt->lock)
100a45896bdSViresh Kumar 		spin_unlock_irqrestore(gpt->lock, flags);
101a45896bdSViresh Kumar 
102a45896bdSViresh Kumar 	return 0;
103a45896bdSViresh Kumar }
104a45896bdSViresh Kumar 
105ba3892dfSBhumika Goyal static const struct clk_ops clk_gpt_ops = {
106a45896bdSViresh Kumar 	.recalc_rate = clk_gpt_recalc_rate,
107a45896bdSViresh Kumar 	.round_rate = clk_gpt_round_rate,
108a45896bdSViresh Kumar 	.set_rate = clk_gpt_set_rate,
109a45896bdSViresh Kumar };
110a45896bdSViresh Kumar 
clk_register_gpt(const char * name,const char * parent_name,unsigned long flags,void __iomem * reg,struct gpt_rate_tbl * rtbl,u8 rtbl_cnt,spinlock_t * lock)111a45896bdSViresh Kumar struct clk *clk_register_gpt(const char *name, const char *parent_name, unsigned
112a45896bdSViresh Kumar 		long flags, void __iomem *reg, struct gpt_rate_tbl *rtbl, u8
113a45896bdSViresh Kumar 		rtbl_cnt, spinlock_t *lock)
114a45896bdSViresh Kumar {
115a45896bdSViresh Kumar 	struct clk_init_data init;
116a45896bdSViresh Kumar 	struct clk_gpt *gpt;
117a45896bdSViresh Kumar 	struct clk *clk;
118a45896bdSViresh Kumar 
119a45896bdSViresh Kumar 	if (!name || !parent_name || !reg || !rtbl || !rtbl_cnt) {
120fd1ced19SArvind Yadav 		pr_err("Invalid arguments passed\n");
121a45896bdSViresh Kumar 		return ERR_PTR(-EINVAL);
122a45896bdSViresh Kumar 	}
123a45896bdSViresh Kumar 
124a45896bdSViresh Kumar 	gpt = kzalloc(sizeof(*gpt), GFP_KERNEL);
12563b1a5d7SMarkus Elfring 	if (!gpt)
126a45896bdSViresh Kumar 		return ERR_PTR(-ENOMEM);
127a45896bdSViresh Kumar 
128a45896bdSViresh Kumar 	/* struct clk_gpt assignments */
129a45896bdSViresh Kumar 	gpt->reg = reg;
130a45896bdSViresh Kumar 	gpt->rtbl = rtbl;
131a45896bdSViresh Kumar 	gpt->rtbl_cnt = rtbl_cnt;
132a45896bdSViresh Kumar 	gpt->lock = lock;
133a45896bdSViresh Kumar 	gpt->hw.init = &init;
134a45896bdSViresh Kumar 
135a45896bdSViresh Kumar 	init.name = name;
136a45896bdSViresh Kumar 	init.ops = &clk_gpt_ops;
137a45896bdSViresh Kumar 	init.flags = flags;
138a45896bdSViresh Kumar 	init.parent_names = &parent_name;
139a45896bdSViresh Kumar 	init.num_parents = 1;
140a45896bdSViresh Kumar 
141a45896bdSViresh Kumar 	clk = clk_register(NULL, &gpt->hw);
142a45896bdSViresh Kumar 	if (!IS_ERR_OR_NULL(clk))
143a45896bdSViresh Kumar 		return clk;
144a45896bdSViresh Kumar 
145a45896bdSViresh Kumar 	pr_err("clk register failed\n");
146a45896bdSViresh Kumar 	kfree(gpt);
147a45896bdSViresh Kumar 
148a45896bdSViresh Kumar 	return NULL;
149a45896bdSViresh Kumar }
150