0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #include <linux/module.h>
0029 #include <linux/moduleparam.h>
0030 #include <linux/types.h>
0031 #include <linux/errno.h>
0032 #include <linux/miscdevice.h>
0033 #include <linux/fs.h>
0034 #include <linux/ioport.h>
0035 #include <linux/timer.h>
0036 #include <linux/completion.h>
0037 #include <linux/jiffies.h>
0038 #include <linux/watchdog.h>
0039 #include <linux/platform_device.h>
0040 #include <linux/io.h>
0041 #include <linux/uaccess.h>
0042 #include <linux/gpio/consumer.h>
0043
0044 #define MTX1_WDT_INTERVAL (5 * HZ)
0045
0046 static int ticks = 100 * HZ;
0047
0048 static struct {
0049 struct completion stop;
0050 spinlock_t lock;
0051 int running;
0052 struct timer_list timer;
0053 int queue;
0054 int default_ticks;
0055 unsigned long inuse;
0056 struct gpio_desc *gpiod;
0057 unsigned int gstate;
0058 } mtx1_wdt_device;
0059
0060 static void mtx1_wdt_trigger(struct timer_list *unused)
0061 {
0062 spin_lock(&mtx1_wdt_device.lock);
0063 if (mtx1_wdt_device.running)
0064 ticks--;
0065
0066
0067 mtx1_wdt_device.gstate = !mtx1_wdt_device.gstate;
0068 gpiod_set_value(mtx1_wdt_device.gpiod, mtx1_wdt_device.gstate);
0069
0070 if (mtx1_wdt_device.queue && ticks)
0071 mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
0072 else
0073 complete(&mtx1_wdt_device.stop);
0074 spin_unlock(&mtx1_wdt_device.lock);
0075 }
0076
0077 static void mtx1_wdt_reset(void)
0078 {
0079 ticks = mtx1_wdt_device.default_ticks;
0080 }
0081
0082
0083 static void mtx1_wdt_start(void)
0084 {
0085 unsigned long flags;
0086
0087 spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
0088 if (!mtx1_wdt_device.queue) {
0089 mtx1_wdt_device.queue = 1;
0090 mtx1_wdt_device.gstate = 1;
0091 gpiod_set_value(mtx1_wdt_device.gpiod, 1);
0092 mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
0093 }
0094 mtx1_wdt_device.running++;
0095 spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
0096 }
0097
0098 static int mtx1_wdt_stop(void)
0099 {
0100 unsigned long flags;
0101
0102 spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
0103 if (mtx1_wdt_device.queue) {
0104 mtx1_wdt_device.queue = 0;
0105 mtx1_wdt_device.gstate = 0;
0106 gpiod_set_value(mtx1_wdt_device.gpiod, 0);
0107 }
0108 ticks = mtx1_wdt_device.default_ticks;
0109 spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
0110 return 0;
0111 }
0112
0113
0114
0115 static int mtx1_wdt_open(struct inode *inode, struct file *file)
0116 {
0117 if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
0118 return -EBUSY;
0119 return stream_open(inode, file);
0120 }
0121
0122
0123 static int mtx1_wdt_release(struct inode *inode, struct file *file)
0124 {
0125 clear_bit(0, &mtx1_wdt_device.inuse);
0126 return 0;
0127 }
0128
0129 static long mtx1_wdt_ioctl(struct file *file, unsigned int cmd,
0130 unsigned long arg)
0131 {
0132 void __user *argp = (void __user *)arg;
0133 int __user *p = (int __user *)argp;
0134 unsigned int value;
0135 static const struct watchdog_info ident = {
0136 .options = WDIOF_CARDRESET,
0137 .identity = "MTX-1 WDT",
0138 };
0139
0140 switch (cmd) {
0141 case WDIOC_GETSUPPORT:
0142 if (copy_to_user(argp, &ident, sizeof(ident)))
0143 return -EFAULT;
0144 break;
0145 case WDIOC_GETSTATUS:
0146 case WDIOC_GETBOOTSTATUS:
0147 put_user(0, p);
0148 break;
0149 case WDIOC_SETOPTIONS:
0150 if (get_user(value, p))
0151 return -EFAULT;
0152 if (value & WDIOS_ENABLECARD)
0153 mtx1_wdt_start();
0154 else if (value & WDIOS_DISABLECARD)
0155 mtx1_wdt_stop();
0156 else
0157 return -EINVAL;
0158 return 0;
0159 case WDIOC_KEEPALIVE:
0160 mtx1_wdt_reset();
0161 break;
0162 default:
0163 return -ENOTTY;
0164 }
0165 return 0;
0166 }
0167
0168
0169 static ssize_t mtx1_wdt_write(struct file *file, const char *buf,
0170 size_t count, loff_t *ppos)
0171 {
0172 if (!count)
0173 return -EIO;
0174 mtx1_wdt_reset();
0175 return count;
0176 }
0177
0178 static const struct file_operations mtx1_wdt_fops = {
0179 .owner = THIS_MODULE,
0180 .llseek = no_llseek,
0181 .unlocked_ioctl = mtx1_wdt_ioctl,
0182 .compat_ioctl = compat_ptr_ioctl,
0183 .open = mtx1_wdt_open,
0184 .write = mtx1_wdt_write,
0185 .release = mtx1_wdt_release,
0186 };
0187
0188
0189 static struct miscdevice mtx1_wdt_misc = {
0190 .minor = WATCHDOG_MINOR,
0191 .name = "watchdog",
0192 .fops = &mtx1_wdt_fops,
0193 };
0194
0195
0196 static int mtx1_wdt_probe(struct platform_device *pdev)
0197 {
0198 int ret;
0199
0200 mtx1_wdt_device.gpiod = devm_gpiod_get(&pdev->dev,
0201 NULL, GPIOD_OUT_HIGH);
0202 if (IS_ERR(mtx1_wdt_device.gpiod)) {
0203 dev_err(&pdev->dev, "failed to request gpio");
0204 return PTR_ERR(mtx1_wdt_device.gpiod);
0205 }
0206
0207 spin_lock_init(&mtx1_wdt_device.lock);
0208 init_completion(&mtx1_wdt_device.stop);
0209 mtx1_wdt_device.queue = 0;
0210 clear_bit(0, &mtx1_wdt_device.inuse);
0211 timer_setup(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0);
0212 mtx1_wdt_device.default_ticks = ticks;
0213
0214 ret = misc_register(&mtx1_wdt_misc);
0215 if (ret < 0) {
0216 dev_err(&pdev->dev, "failed to register\n");
0217 return ret;
0218 }
0219 mtx1_wdt_start();
0220 dev_info(&pdev->dev, "MTX-1 Watchdog driver\n");
0221 return 0;
0222 }
0223
0224 static int mtx1_wdt_remove(struct platform_device *pdev)
0225 {
0226
0227 if (mtx1_wdt_device.queue) {
0228 mtx1_wdt_device.queue = 0;
0229 wait_for_completion(&mtx1_wdt_device.stop);
0230 }
0231
0232 misc_deregister(&mtx1_wdt_misc);
0233 return 0;
0234 }
0235
0236 static struct platform_driver mtx1_wdt_driver = {
0237 .probe = mtx1_wdt_probe,
0238 .remove = mtx1_wdt_remove,
0239 .driver.name = "mtx1-wdt",
0240 .driver.owner = THIS_MODULE,
0241 };
0242
0243 module_platform_driver(mtx1_wdt_driver);
0244
0245 MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
0246 MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
0247 MODULE_LICENSE("GPL");
0248 MODULE_ALIAS("platform:mtx1-wdt");