32 #include <linux/slab.h>
33 #include <linux/module.h>
44 struct clock_event_device
ced;
58 static unsigned long mtu2_reg_offs[] = {
68 static inline unsigned long sh_mtu2_read(
struct sh_mtu2_priv *
p,
int reg_nr)
77 offs = mtu2_reg_offs[reg_nr];
79 if ((reg_nr ==
TCNT) || (reg_nr ==
TGR))
85 static inline void sh_mtu2_write(
struct sh_mtu2_priv *p,
int reg_nr,
97 offs = mtu2_reg_offs[reg_nr];
99 if ((reg_nr ==
TCNT) || (reg_nr ==
TGR))
112 value = sh_mtu2_read(p,
TSTR);
119 sh_mtu2_write(p,
TSTR, value);
127 pm_runtime_get_sync(&p->
pdev->dev);
128 dev_pm_syscore_device(&p->
pdev->dev,
true);
138 sh_mtu2_start_stop_ch(p, 0);
144 sh_mtu2_write(p,
TCR, 0x23);
145 sh_mtu2_write(p,
TIOR, 0);
147 sh_mtu2_write(p,
TCNT, 0);
148 sh_mtu2_write(p,
TMDR, 0);
149 sh_mtu2_write(p,
TIER, 0x01);
152 sh_mtu2_start_stop_ch(p, 1);
160 sh_mtu2_start_stop_ch(p, 0);
165 dev_pm_syscore_device(&p->
pdev->dev,
false);
166 pm_runtime_put(&p->
pdev->dev);
174 sh_mtu2_read(p,
TSR);
175 sh_mtu2_write(p,
TSR, 0xfe);
178 p->
ced.event_handler(&p->
ced);
182 static struct sh_mtu2_priv *ced_to_sh_mtu2(
struct clock_event_device *
ced)
187 static void sh_mtu2_clock_event_mode(
enum clock_event_mode
mode,
188 struct clock_event_device *
ced)
195 case CLOCK_EVT_MODE_PERIODIC:
204 case CLOCK_EVT_MODE_PERIODIC:
205 dev_info(&p->
pdev->dev,
"used for periodic clock events\n");
208 case CLOCK_EVT_MODE_UNUSED:
212 case CLOCK_EVT_MODE_SHUTDOWN:
218 static void sh_mtu2_clock_event_suspend(
struct clock_event_device *ced)
220 pm_genpd_syscore_poweroff(&ced_to_sh_mtu2(ced)->
pdev->dev);
223 static void sh_mtu2_clock_event_resume(
struct clock_event_device *ced)
225 pm_genpd_syscore_poweron(&ced_to_sh_mtu2(ced)->
pdev->dev);
228 static void sh_mtu2_register_clockevent(
struct sh_mtu2_priv *p,
229 char *
name,
unsigned long rating)
231 struct clock_event_device *ced = &p->
ced;
234 memset(ced, 0,
sizeof(*ced));
237 ced->features = CLOCK_EVT_FEAT_PERIODIC;
238 ced->rating = rating;
240 ced->set_mode = sh_mtu2_clock_event_mode;
241 ced->suspend = sh_mtu2_clock_event_suspend;
242 ced->resume = sh_mtu2_clock_event_resume;
249 dev_err(&p->
pdev->dev,
"failed to request irq %d\n",
255 static int sh_mtu2_register(
struct sh_mtu2_priv *p,
char *name,
256 unsigned long clockevent_rating)
258 if (clockevent_rating)
259 sh_mtu2_register_clockevent(p, name, clockevent_rating);
279 platform_set_drvdata(pdev, p);
283 dev_err(&p->
pdev->dev,
"failed to get I/O memory\n");
296 dev_err(&p->
pdev->dev,
"failed to remap I/O memory\n");
302 p->
irqaction.handler = sh_mtu2_interrupt;
310 if (IS_ERR(p->
clk)) {
312 ret = PTR_ERR(p->
clk);
316 return sh_mtu2_register(p, (
char *)dev_name(&p->
pdev->dev),
330 if (!is_early_platform_device(pdev)) {
331 pm_runtime_set_active(&pdev->
dev);
342 dev_err(&pdev->
dev,
"failed to allocate driver data\n");
346 ret = sh_mtu2_setup(p, pdev);
349 platform_set_drvdata(pdev,
NULL);
350 pm_runtime_idle(&pdev->
dev);
353 if (is_early_platform_device(pdev))
360 pm_runtime_idle(&pdev->
dev);
371 .probe = sh_mtu2_probe,
378 static int __init sh_mtu2_init(
void)
383 static void __exit sh_mtu2_exit(
void)