1 // SPDX-License-Identifier: GPL-2.0
3 * Ingenic SoCs TCU IRQ driver
4 * Copyright (C) 2019 Paul Cercueil <paul@crapouillou.net>
5 * Copyright (C) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
8 #include <linux/bitops.h>
10 #include <linux/clockchips.h>
11 #include <linux/clocksource.h>
12 #include <linux/cpuhotplug.h>
13 #include <linux/interrupt.h>
14 #include <linux/mfd/ingenic-tcu.h>
15 #include <linux/mfd/syscon.h>
17 #include <linux/of_irq.h>
18 #include <linux/overflow.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/sched_clock.h>
23 #include <dt-bindings/clock/ingenic,tcu.h>
25 static DEFINE_PER_CPU(call_single_data_t, ingenic_cevt_csd);
27 struct ingenic_soc_info {
28 unsigned int num_channels;
31 struct ingenic_tcu_timer {
34 struct clock_event_device cevt;
41 struct device_node *np;
43 unsigned int cs_channel;
44 struct clocksource cs;
45 unsigned long pwm_channels_mask;
46 struct ingenic_tcu_timer timers[];
49 static struct ingenic_tcu *ingenic_tcu;
51 static u64 notrace ingenic_tcu_timer_read(void)
53 struct ingenic_tcu *tcu = ingenic_tcu;
56 regmap_read(tcu->map, TCU_REG_TCNTc(tcu->cs_channel), &count);
61 static u64 notrace ingenic_tcu_timer_cs_read(struct clocksource *cs)
63 return ingenic_tcu_timer_read();
66 static inline struct ingenic_tcu *
67 to_ingenic_tcu(struct ingenic_tcu_timer *timer)
69 return container_of(timer, struct ingenic_tcu, timers[timer->cpu]);
72 static inline struct ingenic_tcu_timer *
73 to_ingenic_tcu_timer(struct clock_event_device *evt)
75 return container_of(evt, struct ingenic_tcu_timer, cevt);
78 static int ingenic_tcu_cevt_set_state_shutdown(struct clock_event_device *evt)
80 struct ingenic_tcu_timer *timer = to_ingenic_tcu_timer(evt);
81 struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
83 regmap_write(tcu->map, TCU_REG_TECR, BIT(timer->channel));
88 static int ingenic_tcu_cevt_set_next(unsigned long next,
89 struct clock_event_device *evt)
91 struct ingenic_tcu_timer *timer = to_ingenic_tcu_timer(evt);
92 struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
97 regmap_write(tcu->map, TCU_REG_TDFRc(timer->channel), next);
98 regmap_write(tcu->map, TCU_REG_TCNTc(timer->channel), 0);
99 regmap_write(tcu->map, TCU_REG_TESR, BIT(timer->channel));
104 static void ingenic_per_cpu_event_handler(void *info)
106 struct clock_event_device *cevt = (struct clock_event_device *) info;
108 cevt->event_handler(cevt);
111 static irqreturn_t ingenic_tcu_cevt_cb(int irq, void *dev_id)
113 struct ingenic_tcu_timer *timer = dev_id;
114 struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
115 call_single_data_t *csd;
117 regmap_write(tcu->map, TCU_REG_TECR, BIT(timer->channel));
119 if (timer->cevt.event_handler) {
120 csd = &per_cpu(ingenic_cevt_csd, timer->cpu);
121 csd->info = (void *) &timer->cevt;
122 csd->func = ingenic_per_cpu_event_handler;
123 smp_call_function_single_async(timer->cpu, csd);
129 static struct clk *ingenic_tcu_get_clock(struct device_node *np, int id)
131 struct of_phandle_args args;
137 return of_clk_get_from_provider(&args);
140 static int ingenic_tcu_setup_cevt(unsigned int cpu)
142 struct ingenic_tcu *tcu = ingenic_tcu;
143 struct ingenic_tcu_timer *timer = &tcu->timers[cpu];
144 unsigned int timer_virq;
145 struct irq_domain *domain;
149 timer->clk = ingenic_tcu_get_clock(tcu->np, timer->channel);
150 if (IS_ERR(timer->clk))
151 return PTR_ERR(timer->clk);
153 err = clk_prepare_enable(timer->clk);
157 rate = clk_get_rate(timer->clk);
160 goto err_clk_disable;
163 domain = irq_find_host(tcu->np);
166 goto err_clk_disable;
169 timer_virq = irq_create_mapping(domain, timer->channel);
172 goto err_clk_disable;
175 snprintf(timer->name, sizeof(timer->name), "TCU%u", timer->channel);
177 err = request_irq(timer_virq, ingenic_tcu_cevt_cb, IRQF_TIMER,
180 goto err_irq_dispose_mapping;
182 timer->cpu = smp_processor_id();
183 timer->cevt.cpumask = cpumask_of(smp_processor_id());
184 timer->cevt.features = CLOCK_EVT_FEAT_ONESHOT;
185 timer->cevt.name = timer->name;
186 timer->cevt.rating = 200;
187 timer->cevt.set_state_shutdown = ingenic_tcu_cevt_set_state_shutdown;
188 timer->cevt.set_next_event = ingenic_tcu_cevt_set_next;
190 clockevents_config_and_register(&timer->cevt, rate, 10, 0xffff);
194 err_irq_dispose_mapping:
195 irq_dispose_mapping(timer_virq);
197 clk_disable_unprepare(timer->clk);
203 static int __init ingenic_tcu_clocksource_init(struct device_node *np,
204 struct ingenic_tcu *tcu)
206 unsigned int channel = tcu->cs_channel;
207 struct clocksource *cs = &tcu->cs;
211 tcu->cs_clk = ingenic_tcu_get_clock(np, channel);
212 if (IS_ERR(tcu->cs_clk))
213 return PTR_ERR(tcu->cs_clk);
215 err = clk_prepare_enable(tcu->cs_clk);
219 rate = clk_get_rate(tcu->cs_clk);
222 goto err_clk_disable;
226 regmap_update_bits(tcu->map, TCU_REG_TCSRc(channel),
227 0xffff & ~TCU_TCSR_RESERVED_BITS, 0);
230 regmap_write(tcu->map, TCU_REG_TDFRc(channel), 0xffff);
231 regmap_write(tcu->map, TCU_REG_TCNTc(channel), 0);
234 regmap_write(tcu->map, TCU_REG_TESR, BIT(channel));
236 cs->name = "ingenic-timer";
238 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
239 cs->mask = CLOCKSOURCE_MASK(16);
240 cs->read = ingenic_tcu_timer_cs_read;
242 err = clocksource_register_hz(cs, rate);
244 goto err_clk_disable;
249 clk_disable_unprepare(tcu->cs_clk);
251 clk_put(tcu->cs_clk);
255 static const struct ingenic_soc_info jz4740_soc_info = {
259 static const struct ingenic_soc_info jz4725b_soc_info = {
263 static const struct of_device_id ingenic_tcu_of_match[] = {
264 { .compatible = "ingenic,jz4740-tcu", .data = &jz4740_soc_info, },
265 { .compatible = "ingenic,jz4725b-tcu", .data = &jz4725b_soc_info, },
266 { .compatible = "ingenic,jz4760-tcu", .data = &jz4740_soc_info, },
267 { .compatible = "ingenic,jz4770-tcu", .data = &jz4740_soc_info, },
268 { .compatible = "ingenic,x1000-tcu", .data = &jz4740_soc_info, },
272 static int __init ingenic_tcu_init(struct device_node *np)
274 const struct of_device_id *id = of_match_node(ingenic_tcu_of_match, np);
275 const struct ingenic_soc_info *soc_info = id->data;
276 struct ingenic_tcu_timer *timer;
277 struct ingenic_tcu *tcu;
280 int ret, last_bit = -1;
283 of_node_clear_flag(np, OF_POPULATED);
285 map = device_node_to_regmap(np);
289 tcu = kzalloc(struct_size(tcu, timers, num_possible_cpus()),
295 * Enable all TCU channels for PWM use by default except channels 0/1,
296 * and channel 2 if target CPU is JZ4780/X2000 and SMP is selected.
298 tcu->pwm_channels_mask = GENMASK(soc_info->num_channels - 1,
299 num_possible_cpus() + 1);
300 of_property_read_u32(np, "ingenic,pwm-channels-mask",
301 (u32 *)&tcu->pwm_channels_mask);
303 /* Verify that we have at least num_possible_cpus() + 1 free channels */
304 if (hweight8(tcu->pwm_channels_mask) >
305 soc_info->num_channels - num_possible_cpus() + 1) {
306 pr_crit("%s: Invalid PWM channel mask: 0x%02lx\n", __func__,
307 tcu->pwm_channels_mask);
309 goto err_free_ingenic_tcu;
316 for (cpu = 0; cpu < num_possible_cpus(); cpu++) {
317 timer = &tcu->timers[cpu];
320 timer->channel = find_next_zero_bit(&tcu->pwm_channels_mask,
321 soc_info->num_channels,
323 last_bit = timer->channel;
326 tcu->cs_channel = find_next_zero_bit(&tcu->pwm_channels_mask,
327 soc_info->num_channels,
330 ret = ingenic_tcu_clocksource_init(np, tcu);
332 pr_crit("%s: Unable to init clocksource: %d\n", __func__, ret);
333 goto err_free_ingenic_tcu;
336 /* Setup clock events on each CPU core */
337 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "Ingenic XBurst: online",
338 ingenic_tcu_setup_cevt, NULL);
340 pr_crit("%s: Unable to start CPU timers: %d\n", __func__, ret);
341 goto err_tcu_clocksource_cleanup;
344 /* Register the sched_clock at the end as there's no way to undo it */
345 rate = clk_get_rate(tcu->cs_clk);
346 sched_clock_register(ingenic_tcu_timer_read, 16, rate);
350 err_tcu_clocksource_cleanup:
351 clocksource_unregister(&tcu->cs);
352 clk_disable_unprepare(tcu->cs_clk);
353 clk_put(tcu->cs_clk);
354 err_free_ingenic_tcu:
359 TIMER_OF_DECLARE(jz4740_tcu_intc, "ingenic,jz4740-tcu", ingenic_tcu_init);
360 TIMER_OF_DECLARE(jz4725b_tcu_intc, "ingenic,jz4725b-tcu", ingenic_tcu_init);
361 TIMER_OF_DECLARE(jz4760_tcu_intc, "ingenic,jz4760-tcu", ingenic_tcu_init);
362 TIMER_OF_DECLARE(jz4770_tcu_intc, "ingenic,jz4770-tcu", ingenic_tcu_init);
363 TIMER_OF_DECLARE(x1000_tcu_intc, "ingenic,x1000-tcu", ingenic_tcu_init);
365 static int __init ingenic_tcu_probe(struct platform_device *pdev)
367 platform_set_drvdata(pdev, ingenic_tcu);
372 static int ingenic_tcu_suspend(struct device *dev)
374 struct ingenic_tcu *tcu = dev_get_drvdata(dev);
377 clk_disable(tcu->cs_clk);
379 for (cpu = 0; cpu < num_online_cpus(); cpu++)
380 clk_disable(tcu->timers[cpu].clk);
385 static int ingenic_tcu_resume(struct device *dev)
387 struct ingenic_tcu *tcu = dev_get_drvdata(dev);
391 for (cpu = 0; cpu < num_online_cpus(); cpu++) {
392 ret = clk_enable(tcu->timers[cpu].clk);
394 goto err_timer_clk_disable;
397 ret = clk_enable(tcu->cs_clk);
399 goto err_timer_clk_disable;
403 err_timer_clk_disable:
404 for (; cpu > 0; cpu--)
405 clk_disable(tcu->timers[cpu - 1].clk);
409 static const struct dev_pm_ops ingenic_tcu_pm_ops = {
410 /* _noirq: We want the TCU clocks to be gated last / ungated first */
411 .suspend_noirq = ingenic_tcu_suspend,
412 .resume_noirq = ingenic_tcu_resume,
415 static struct platform_driver ingenic_tcu_driver = {
417 .name = "ingenic-tcu-timer",
418 .pm = pm_sleep_ptr(&ingenic_tcu_pm_ops),
419 .of_match_table = ingenic_tcu_of_match,
422 builtin_platform_driver_probe(ingenic_tcu_driver, ingenic_tcu_probe);