1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MPIC timer wakeup driver
5 * Copyright 2013 Freescale Semiconductor, Inc.
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/device.h>
15 #include <asm/mpic_timer.h>
18 struct fsl_mpic_timer_wakeup
{
19 struct mpic_timer
*timer
;
20 struct work_struct free_work
;
23 static struct fsl_mpic_timer_wakeup
*fsl_wakeup
;
24 static DEFINE_MUTEX(sysfs_lock
);
26 static void fsl_free_resource(struct work_struct
*ws
)
28 struct fsl_mpic_timer_wakeup
*wakeup
=
29 container_of(ws
, struct fsl_mpic_timer_wakeup
, free_work
);
31 mutex_lock(&sysfs_lock
);
34 disable_irq_wake(wakeup
->timer
->irq
);
35 mpic_free_timer(wakeup
->timer
);
39 mutex_unlock(&sysfs_lock
);
42 static irqreturn_t
fsl_mpic_timer_irq(int irq
, void *dev_id
)
44 struct fsl_mpic_timer_wakeup
*wakeup
= dev_id
;
46 schedule_work(&wakeup
->free_work
);
48 return wakeup
->timer
? IRQ_HANDLED
: IRQ_NONE
;
51 static ssize_t
fsl_timer_wakeup_show(struct device
*dev
,
52 struct device_attribute
*attr
,
55 time64_t interval
= 0;
57 mutex_lock(&sysfs_lock
);
58 if (fsl_wakeup
->timer
) {
59 mpic_get_remain_time(fsl_wakeup
->timer
, &interval
);
62 mutex_unlock(&sysfs_lock
);
64 return sprintf(buf
, "%lld\n", interval
);
67 static ssize_t
fsl_timer_wakeup_store(struct device
*dev
,
68 struct device_attribute
*attr
,
75 if (kstrtoll(buf
, 0, &interval
))
78 mutex_lock(&sysfs_lock
);
80 if (fsl_wakeup
->timer
) {
81 disable_irq_wake(fsl_wakeup
->timer
->irq
);
82 mpic_free_timer(fsl_wakeup
->timer
);
83 fsl_wakeup
->timer
= NULL
;
87 mutex_unlock(&sysfs_lock
);
91 fsl_wakeup
->timer
= mpic_request_timer(fsl_mpic_timer_irq
,
92 fsl_wakeup
, interval
);
93 if (!fsl_wakeup
->timer
) {
94 mutex_unlock(&sysfs_lock
);
98 ret
= enable_irq_wake(fsl_wakeup
->timer
->irq
);
100 mpic_free_timer(fsl_wakeup
->timer
);
101 fsl_wakeup
->timer
= NULL
;
102 mutex_unlock(&sysfs_lock
);
107 mpic_start_timer(fsl_wakeup
->timer
);
109 mutex_unlock(&sysfs_lock
);
114 static struct device_attribute mpic_attributes
= __ATTR(timer_wakeup
, 0644,
115 fsl_timer_wakeup_show
, fsl_timer_wakeup_store
);
117 static int __init
fsl_wakeup_sys_init(void)
121 fsl_wakeup
= kzalloc(sizeof(struct fsl_mpic_timer_wakeup
), GFP_KERNEL
);
125 INIT_WORK(&fsl_wakeup
->free_work
, fsl_free_resource
);
127 ret
= device_create_file(mpic_subsys
.dev_root
, &mpic_attributes
);
134 static void __exit
fsl_wakeup_sys_exit(void)
136 device_remove_file(mpic_subsys
.dev_root
, &mpic_attributes
);
138 mutex_lock(&sysfs_lock
);
140 if (fsl_wakeup
->timer
) {
141 disable_irq_wake(fsl_wakeup
->timer
->irq
);
142 mpic_free_timer(fsl_wakeup
->timer
);
147 mutex_unlock(&sysfs_lock
);
150 module_init(fsl_wakeup_sys_init
);
151 module_exit(fsl_wakeup_sys_exit
);
153 MODULE_DESCRIPTION("Freescale MPIC global timer wakeup driver");
154 MODULE_LICENSE("GPL v2");
155 MODULE_AUTHOR("Wang Dongsheng <dongsheng.wang@freescale.com>");