33 #include <linux/slab.h>
34 #include <linux/module.h>
45 struct clock_event_device
ced;
58 static inline unsigned long sh_tmu_read(
struct sh_tmu_priv *
p,
int reg_nr)
75 static inline void sh_tmu_write(
struct sh_tmu_priv *
p,
int reg_nr,
102 value = sh_tmu_read(p,
TSTR);
109 sh_tmu_write(p,
TSTR, value);
125 sh_tmu_start_stop_ch(p, 0);
128 sh_tmu_write(p,
TCOR, 0xffffffff);
129 sh_tmu_write(p,
TCNT, 0xffffffff);
133 sh_tmu_write(p,
TCR, 0x0000);
136 sh_tmu_start_stop_ch(p, 1);
146 pm_runtime_get_sync(&p->
pdev->dev);
147 dev_pm_syscore_device(&p->
pdev->dev,
true);
149 return __sh_tmu_enable(p);
152 static void __sh_tmu_disable(
struct sh_tmu_priv *p)
155 sh_tmu_start_stop_ch(p, 0);
158 sh_tmu_write(p,
TCR, 0x0000);
174 dev_pm_syscore_device(&p->
pdev->dev,
false);
175 pm_runtime_put(&p->
pdev->dev);
182 sh_tmu_start_stop_ch(p, 0);
188 sh_tmu_write(p,
TCR, 0x0020);
192 sh_tmu_write(p,
TCOR, delta);
194 sh_tmu_write(p,
TCOR, 0xffffffff);
196 sh_tmu_write(p,
TCNT, delta);
199 sh_tmu_start_stop_ch(p, 1);
207 if (p->
ced.mode == CLOCK_EVT_MODE_ONESHOT)
208 sh_tmu_write(p,
TCR, 0x0000);
210 sh_tmu_write(p,
TCR, 0x0020);
213 p->
ced.event_handler(&p->
ced);
226 return sh_tmu_read(p,
TCNT) ^ 0xffffffff;
237 ret = sh_tmu_enable(p);
239 __clocksource_updatefreq_hz(cs, p->
rate);
246 static void sh_tmu_clocksource_disable(
struct clocksource *cs)
257 static void sh_tmu_clocksource_suspend(
struct clocksource *cs)
266 pm_genpd_syscore_poweroff(&p->
pdev->dev);
270 static void sh_tmu_clocksource_resume(
struct clocksource *cs)
278 pm_genpd_syscore_poweron(&p->
pdev->dev);
283 static int sh_tmu_register_clocksource(
struct sh_tmu_priv *p,
284 char *
name,
unsigned long rating)
290 cs->
read = sh_tmu_clocksource_read;
291 cs->
enable = sh_tmu_clocksource_enable;
292 cs->
disable = sh_tmu_clocksource_disable;
293 cs->
suspend = sh_tmu_clocksource_suspend;
294 cs->
resume = sh_tmu_clocksource_resume;
301 clocksource_register_hz(cs, 1);
305 static struct sh_tmu_priv *ced_to_sh_tmu(
struct clock_event_device *
ced)
310 static void sh_tmu_clock_event_start(
struct sh_tmu_priv *p,
int periodic)
312 struct clock_event_device *ced = &p->
ced;
324 static void sh_tmu_clock_event_mode(
enum clock_event_mode
mode,
325 struct clock_event_device *ced)
332 case CLOCK_EVT_MODE_PERIODIC:
333 case CLOCK_EVT_MODE_ONESHOT:
342 case CLOCK_EVT_MODE_PERIODIC:
343 dev_info(&p->
pdev->dev,
"used for periodic clock events\n");
344 sh_tmu_clock_event_start(p, 1);
346 case CLOCK_EVT_MODE_ONESHOT:
347 dev_info(&p->
pdev->dev,
"used for oneshot clock events\n");
348 sh_tmu_clock_event_start(p, 0);
350 case CLOCK_EVT_MODE_UNUSED:
354 case CLOCK_EVT_MODE_SHUTDOWN:
360 static int sh_tmu_clock_event_next(
unsigned long delta,
361 struct clock_event_device *ced)
365 BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
368 sh_tmu_set_next(p, delta, 0);
372 static void sh_tmu_clock_event_suspend(
struct clock_event_device *ced)
374 pm_genpd_syscore_poweroff(&ced_to_sh_tmu(ced)->
pdev->dev);
377 static void sh_tmu_clock_event_resume(
struct clock_event_device *ced)
379 pm_genpd_syscore_poweron(&ced_to_sh_tmu(ced)->
pdev->dev);
382 static void sh_tmu_register_clockevent(
struct sh_tmu_priv *p,
383 char *
name,
unsigned long rating)
385 struct clock_event_device *ced = &p->
ced;
388 memset(ced, 0,
sizeof(*ced));
391 ced->features = CLOCK_EVT_FEAT_PERIODIC;
392 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
393 ced->rating = rating;
395 ced->set_next_event = sh_tmu_clock_event_next;
396 ced->set_mode = sh_tmu_clock_event_mode;
397 ced->suspend = sh_tmu_clock_event_suspend;
398 ced->resume = sh_tmu_clock_event_resume;
406 dev_err(&p->
pdev->dev,
"failed to request irq %d\n",
412 static int sh_tmu_register(
struct sh_tmu_priv *p,
char *name,
413 unsigned long clockevent_rating,
414 unsigned long clocksource_rating)
416 if (clockevent_rating)
417 sh_tmu_register_clockevent(p, name, clockevent_rating);
418 else if (clocksource_rating)
419 sh_tmu_register_clocksource(p, name, clocksource_rating);
439 platform_set_drvdata(pdev, p);
443 dev_err(&p->
pdev->dev,
"failed to get I/O memory\n");
456 dev_err(&p->
pdev->dev,
"failed to remap I/O memory\n");
470 if (IS_ERR(p->
clk)) {
472 ret = PTR_ERR(p->
clk);
478 return sh_tmu_register(p, (
char *)dev_name(&p->
pdev->dev),
489 struct sh_tmu_priv *p = platform_get_drvdata(pdev);
493 if (!is_early_platform_device(pdev)) {
494 pm_runtime_set_active(&pdev->
dev);
505 dev_err(&pdev->
dev,
"failed to allocate driver data\n");
509 ret = sh_tmu_setup(p, pdev);
512 platform_set_drvdata(pdev,
NULL);
513 pm_runtime_idle(&pdev->
dev);
516 if (is_early_platform_device(pdev))
523 pm_runtime_idle(&pdev->
dev);
534 .probe = sh_tmu_probe,
541 static int __init sh_tmu_init(
void)
546 static void __exit sh_tmu_exit(
void)