xref: /linux/drivers/clk/versatile/clk-sp810.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26e973d2cSPawel Moll /*
36e973d2cSPawel Moll  *
46e973d2cSPawel Moll  * Copyright (C) 2013 ARM Limited
56e973d2cSPawel Moll  */
66e973d2cSPawel Moll 
76e973d2cSPawel Moll #include <linux/amba/sp810.h>
86d31e3b2SStephen Boyd #include <linux/slab.h>
96d31e3b2SStephen Boyd #include <linux/clk.h>
106e973d2cSPawel Moll #include <linux/clk-provider.h>
116e973d2cSPawel Moll #include <linux/err.h>
1262e59c4eSStephen Boyd #include <linux/io.h>
136e973d2cSPawel Moll #include <linux/of.h>
146e973d2cSPawel Moll #include <linux/of_address.h>
156e973d2cSPawel Moll 
166e973d2cSPawel Moll #define to_clk_sp810_timerclken(_hw) \
176e973d2cSPawel Moll 		container_of(_hw, struct clk_sp810_timerclken, hw)
186e973d2cSPawel Moll 
196e973d2cSPawel Moll struct clk_sp810;
206e973d2cSPawel Moll 
216e973d2cSPawel Moll struct clk_sp810_timerclken {
226e973d2cSPawel Moll 	struct clk_hw hw;
236e973d2cSPawel Moll 	struct clk *clk;
246e973d2cSPawel Moll 	struct clk_sp810 *sp810;
256e973d2cSPawel Moll 	int channel;
266e973d2cSPawel Moll };
276e973d2cSPawel Moll 
286e973d2cSPawel Moll struct clk_sp810 {
296e973d2cSPawel Moll 	struct device_node *node;
306e973d2cSPawel Moll 	void __iomem *base;
316e973d2cSPawel Moll 	spinlock_t lock;
326e973d2cSPawel Moll 	struct clk_sp810_timerclken timerclken[4];
336e973d2cSPawel Moll };
346e973d2cSPawel Moll 
clk_sp810_timerclken_get_parent(struct clk_hw * hw)356e973d2cSPawel Moll static u8 clk_sp810_timerclken_get_parent(struct clk_hw *hw)
366e973d2cSPawel Moll {
376e973d2cSPawel Moll 	struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
386e973d2cSPawel Moll 	u32 val = readl(timerclken->sp810->base + SCCTRL);
396e973d2cSPawel Moll 
406e973d2cSPawel Moll 	return !!(val & (1 << SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel)));
416e973d2cSPawel Moll }
426e973d2cSPawel Moll 
clk_sp810_timerclken_set_parent(struct clk_hw * hw,u8 index)436e973d2cSPawel Moll static int clk_sp810_timerclken_set_parent(struct clk_hw *hw, u8 index)
446e973d2cSPawel Moll {
456e973d2cSPawel Moll 	struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
466e973d2cSPawel Moll 	struct clk_sp810 *sp810 = timerclken->sp810;
476e973d2cSPawel Moll 	u32 val, shift = SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel);
486e973d2cSPawel Moll 	unsigned long flags = 0;
496e973d2cSPawel Moll 
506e973d2cSPawel Moll 	if (WARN_ON(index > 1))
516e973d2cSPawel Moll 		return -EINVAL;
526e973d2cSPawel Moll 
536e973d2cSPawel Moll 	spin_lock_irqsave(&sp810->lock, flags);
546e973d2cSPawel Moll 
556e973d2cSPawel Moll 	val = readl(sp810->base + SCCTRL);
566e973d2cSPawel Moll 	val &= ~(1 << shift);
576e973d2cSPawel Moll 	val |= index << shift;
586e973d2cSPawel Moll 	writel(val, sp810->base + SCCTRL);
596e973d2cSPawel Moll 
606e973d2cSPawel Moll 	spin_unlock_irqrestore(&sp810->lock, flags);
616e973d2cSPawel Moll 
626e973d2cSPawel Moll 	return 0;
636e973d2cSPawel Moll }
646e973d2cSPawel Moll 
656e973d2cSPawel Moll static const struct clk_ops clk_sp810_timerclken_ops = {
66d5a5a6e4SMaxime Ripard 	.determine_rate = clk_hw_determine_rate_no_reparent,
676e973d2cSPawel Moll 	.get_parent = clk_sp810_timerclken_get_parent,
686e973d2cSPawel Moll 	.set_parent = clk_sp810_timerclken_set_parent,
696e973d2cSPawel Moll };
706e973d2cSPawel Moll 
clk_sp810_timerclken_of_get(struct of_phandle_args * clkspec,void * data)7114b260ceSSachin Kamat static struct clk *clk_sp810_timerclken_of_get(struct of_phandle_args *clkspec,
726e973d2cSPawel Moll 		void *data)
736e973d2cSPawel Moll {
746e973d2cSPawel Moll 	struct clk_sp810 *sp810 = data;
756e973d2cSPawel Moll 
763294bee8SDan Carpenter 	if (WARN_ON(clkspec->args_count != 1 ||
773294bee8SDan Carpenter 		    clkspec->args[0] >=	ARRAY_SIZE(sp810->timerclken)))
786e973d2cSPawel Moll 		return NULL;
796e973d2cSPawel Moll 
806e973d2cSPawel Moll 	return sp810->timerclken[clkspec->args[0]].clk;
816e973d2cSPawel Moll }
826e973d2cSPawel Moll 
clk_sp810_of_setup(struct device_node * node)8311bee5e1SStephen Boyd static void __init clk_sp810_of_setup(struct device_node *node)
846e973d2cSPawel Moll {
856e973d2cSPawel Moll 	struct clk_sp810 *sp810 = kzalloc(sizeof(*sp810), GFP_KERNEL);
866e973d2cSPawel Moll 	const char *parent_names[2];
8762f47711SStephen Boyd 	int num = ARRAY_SIZE(parent_names);
886e973d2cSPawel Moll 	char name[12];
896e973d2cSPawel Moll 	struct clk_init_data init;
90ec7957a6SLinus Walleij 	static int instance;
916e973d2cSPawel Moll 	int i;
9262f47711SStephen Boyd 	bool deprecated;
936e973d2cSPawel Moll 
9459fe6631SSudip Mukherjee 	if (!sp810)
956e973d2cSPawel Moll 		return;
966e973d2cSPawel Moll 
9762f47711SStephen Boyd 	if (of_clk_parent_fill(node, parent_names, num) != num) {
986e973d2cSPawel Moll 		pr_warn("Failed to obtain parent clocks for SP810!\n");
9947c5ee34SSudip Mukherjee 		kfree(sp810);
1006e973d2cSPawel Moll 		return;
1016e973d2cSPawel Moll 	}
1026e973d2cSPawel Moll 
1036e973d2cSPawel Moll 	sp810->node = node;
1046e973d2cSPawel Moll 	sp810->base = of_iomap(node, 0);
1056e973d2cSPawel Moll 	spin_lock_init(&sp810->lock);
1066e973d2cSPawel Moll 
1076e973d2cSPawel Moll 	init.name = name;
1086e973d2cSPawel Moll 	init.ops = &clk_sp810_timerclken_ops;
109354e1210SStephen Boyd 	init.flags = 0;
1106e973d2cSPawel Moll 	init.parent_names = parent_names;
11162f47711SStephen Boyd 	init.num_parents = num;
11262f47711SStephen Boyd 
113*66b06523SRob Herring (Arm) 	deprecated = !of_property_present(node, "assigned-clock-parents");
1146e973d2cSPawel Moll 
1156e973d2cSPawel Moll 	for (i = 0; i < ARRAY_SIZE(sp810->timerclken); i++) {
116ec7957a6SLinus Walleij 		snprintf(name, sizeof(name), "sp810_%d_%d", instance, i);
1176e973d2cSPawel Moll 
1186e973d2cSPawel Moll 		sp810->timerclken[i].sp810 = sp810;
1196e973d2cSPawel Moll 		sp810->timerclken[i].channel = i;
1206e973d2cSPawel Moll 		sp810->timerclken[i].hw.init = &init;
1216e973d2cSPawel Moll 
12262f47711SStephen Boyd 		/*
12362f47711SStephen Boyd 		 * If DT isn't setting the parent, force it to be
12462f47711SStephen Boyd 		 * the 1 MHz clock without going through the framework.
12562f47711SStephen Boyd 		 * We do this before clk_register() so that it can determine
12662f47711SStephen Boyd 		 * the parent and setup the tree properly.
12762f47711SStephen Boyd 		 */
12862f47711SStephen Boyd 		if (deprecated)
12962f47711SStephen Boyd 			init.ops->set_parent(&sp810->timerclken[i].hw, 1);
13062f47711SStephen Boyd 
1316e973d2cSPawel Moll 		sp810->timerclken[i].clk = clk_register(NULL,
1326e973d2cSPawel Moll 				&sp810->timerclken[i].hw);
1336e973d2cSPawel Moll 		WARN_ON(IS_ERR(sp810->timerclken[i].clk));
1346e973d2cSPawel Moll 	}
1356e973d2cSPawel Moll 
1366e973d2cSPawel Moll 	of_clk_add_provider(node, clk_sp810_timerclken_of_get, sp810);
137ec7957a6SLinus Walleij 	instance++;
1386e973d2cSPawel Moll }
1396e973d2cSPawel Moll CLK_OF_DECLARE(sp810, "arm,sp810", clk_sp810_of_setup);
140