Skip to content

Commit 3071efa

Browse files
mripardingomolnar
authored andcommitted
clocksource/drivers/sun5i: Add clock notifiers
The parent clock of the sun5i timer is the AHB clock, which rate might change because of other devices requirements. This is for example the case on the Allwinner A31, where the DMA controller needs a minimum rate higher than the default, that is enforced after the timer driver has probed. Add clock notifiers to make sure we reflect the clock rate changes in the timer rates. Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> Cc: linux-arm-kernel@lists.infradead.org Link: http://lkml.kernel.org/r/1427796746-373-5-git-send-email-daniel.lezcano@linaro.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4a59058 commit 3071efa

1 file changed

Lines changed: 64 additions & 2 deletions

File tree

‎drivers/clocksource/timer-sun5i.c‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040
struct sun5i_timer {
4141
void __iomem *base;
4242
struct clk *clk;
43+
struct notifier_block clk_rate_cb;
4344
u32 ticks_per_jiffy;
4445
};
4546

47+
#define to_sun5i_timer(x) \
48+
container_of(x, struct sun5i_timer, clk_rate_cb)
49+
4650
struct sun5i_timer_clksrc {
4751
struct sun5i_timer timer;
4852
struct clocksource clksrc;
@@ -151,6 +155,29 @@ static cycle_t sun5i_clksrc_read(struct clocksource *clksrc)
151155
return ~readl(cs->timer.base + TIMER_CNTVAL_LO_REG(1));
152156
}
153157

158+
static int sun5i_rate_cb_clksrc(struct notifier_block *nb,
159+
unsigned long event, void *data)
160+
{
161+
struct clk_notifier_data *ndata = data;
162+
struct sun5i_timer *timer = to_sun5i_timer(nb);
163+
struct sun5i_timer_clksrc *cs = container_of(timer, struct sun5i_timer_clksrc, timer);
164+
165+
switch (event) {
166+
case PRE_RATE_CHANGE:
167+
clocksource_unregister(&cs->clksrc);
168+
break;
169+
170+
case POST_RATE_CHANGE:
171+
clocksource_register_hz(&cs->clksrc, ndata->new_rate);
172+
break;
173+
174+
default:
175+
break;
176+
}
177+
178+
return NOTIFY_DONE;
179+
}
180+
154181
static int __init sun5i_setup_clocksource(struct device_node *node,
155182
void __iomem *base,
156183
struct clk *clk, int irq)
@@ -173,6 +200,14 @@ static int __init sun5i_setup_clocksource(struct device_node *node,
173200

174201
cs->timer.base = base;
175202
cs->timer.clk = clk;
203+
cs->timer.clk_rate_cb.notifier_call = sun5i_rate_cb_clksrc;
204+
cs->timer.clk_rate_cb.next = NULL;
205+
206+
ret = clk_notifier_register(clk, &cs->timer.clk_rate_cb);
207+
if (ret) {
208+
pr_err("Unable to register clock notifier.\n");
209+
goto err_disable_clk;
210+
}
176211

177212
writel(~0, base + TIMER_INTVAL_LO_REG(1));
178213
writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
@@ -187,18 +222,35 @@ static int __init sun5i_setup_clocksource(struct device_node *node,
187222
ret = clocksource_register_hz(&cs->clksrc, rate);
188223
if (ret) {
189224
pr_err("Couldn't register clock source.\n");
190-
goto err_disable_clk;
225+
goto err_remove_notifier;
191226
}
192227

193228
return 0;
194229

230+
err_remove_notifier:
231+
clk_notifier_unregister(clk, &cs->timer.clk_rate_cb);
195232
err_disable_clk:
196233
clk_disable_unprepare(clk);
197234
err_free:
198235
kfree(cs);
199236
return ret;
200237
}
201238

239+
static int sun5i_rate_cb_clkevt(struct notifier_block *nb,
240+
unsigned long event, void *data)
241+
{
242+
struct clk_notifier_data *ndata = data;
243+
struct sun5i_timer *timer = to_sun5i_timer(nb);
244+
struct sun5i_timer_clkevt *ce = container_of(timer, struct sun5i_timer_clkevt, timer);
245+
246+
if (event == POST_RATE_CHANGE) {
247+
clockevents_update_freq(&ce->clkevt, ndata->new_rate);
248+
ce->timer.ticks_per_jiffy = DIV_ROUND_UP(ndata->new_rate, HZ);
249+
}
250+
251+
return NOTIFY_DONE;
252+
}
253+
202254
static int __init sun5i_setup_clockevent(struct device_node *node, void __iomem *base,
203255
struct clk *clk, int irq)
204256
{
@@ -222,6 +274,14 @@ static int __init sun5i_setup_clockevent(struct device_node *node, void __iomem
222274
ce->timer.base = base;
223275
ce->timer.ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
224276
ce->timer.clk = clk;
277+
ce->timer.clk_rate_cb.notifier_call = sun5i_rate_cb_clkevt;
278+
ce->timer.clk_rate_cb.next = NULL;
279+
280+
ret = clk_notifier_register(clk, &ce->timer.clk_rate_cb);
281+
if (ret) {
282+
pr_err("Unable to register clock notifier.\n");
283+
goto err_disable_clk;
284+
}
225285

226286
ce->clkevt.name = node->name;
227287
ce->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
@@ -242,11 +302,13 @@ static int __init sun5i_setup_clockevent(struct device_node *node, void __iomem
242302
"sun5i_timer0", ce);
243303
if (ret) {
244304
pr_err("Unable to register interrupt\n");
245-
goto err_disable_clk;
305+
goto err_remove_notifier;
246306
}
247307

248308
return 0;
249309

310+
err_remove_notifier:
311+
clk_notifier_unregister(clk, &ce->timer.clk_rate_cb);
250312
err_disable_clk:
251313
clk_disable_unprepare(clk);
252314
err_free:

0 commit comments

Comments
 (0)