2 * COMEDI driver for the watchdog subdevice found on some addi-data boards
3 * Copyright (c) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
5 * Based on implementations in various addi-data COMEDI drivers.
7 * COMEDI - Linux Control and Measurement Device Interface
8 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/module.h>
22 #include "../comedidev.h"
23 #include "addi_watchdog.h"
26 * Register offsets/defines for the addi-data watchdog
28 #define ADDI_WDOG_REG 0x00
29 #define ADDI_WDOG_RELOAD_REG 0x04
30 #define ADDI_WDOG_TIMEBASE 0x08
31 #define ADDI_WDOG_CTRL_REG 0x0c
32 #define ADDI_WDOG_CTRL_ENABLE (1 << 0)
33 #define ADDI_WDOG_CTRL_SW_TRIG (1 << 9)
34 #define ADDI_WDOG_STATUS_REG 0x10
35 #define ADDI_WDOG_STATUS_ENABLED (1 << 0)
36 #define ADDI_WDOG_STATUS_SW_TRIG (1 << 1)
38 struct addi_watchdog_private {
40 unsigned int wdog_ctrl;
44 * The watchdog subdevice is configured with two INSN_CONFIG instructions:
46 * Enable the watchdog and set the reload timeout:
47 * data[0] = INSN_CONFIG_ARM
48 * data[1] = timeout reload value
50 * Disable the watchdog:
51 * data[0] = INSN_CONFIG_DISARM
53 static int addi_watchdog_insn_config(struct comedi_device *dev,
54 struct comedi_subdevice *s,
55 struct comedi_insn *insn,
58 struct addi_watchdog_private *spriv = s->private;
63 spriv->wdog_ctrl = ADDI_WDOG_CTRL_ENABLE;
64 reload = data[1] & s->maxdata;
65 outl(reload, spriv->iobase + ADDI_WDOG_RELOAD_REG);
67 /* Time base is 20ms, let the user know the timeout */
68 dev_info(dev->class_dev, "watchdog enabled, timeout:%dms\n",
71 case INSN_CONFIG_DISARM:
78 outl(spriv->wdog_ctrl, spriv->iobase + ADDI_WDOG_CTRL_REG);
83 static int addi_watchdog_insn_read(struct comedi_device *dev,
84 struct comedi_subdevice *s,
85 struct comedi_insn *insn,
88 struct addi_watchdog_private *spriv = s->private;
91 for (i = 0; i < insn->n; i++)
92 data[i] = inl(spriv->iobase + ADDI_WDOG_STATUS_REG);
97 static int addi_watchdog_insn_write(struct comedi_device *dev,
98 struct comedi_subdevice *s,
99 struct comedi_insn *insn,
102 struct addi_watchdog_private *spriv = s->private;
105 if (spriv->wdog_ctrl == 0) {
106 dev_warn(dev->class_dev, "watchdog is disabled\n");
110 /* "ping" the watchdog */
111 for (i = 0; i < insn->n; i++) {
112 outl(spriv->wdog_ctrl | ADDI_WDOG_CTRL_SW_TRIG,
113 spriv->iobase + ADDI_WDOG_CTRL_REG);
119 void addi_watchdog_reset(unsigned long iobase)
121 outl(0x0, iobase + ADDI_WDOG_CTRL_REG);
122 outl(0x0, iobase + ADDI_WDOG_RELOAD_REG);
124 EXPORT_SYMBOL_GPL(addi_watchdog_reset);
126 int addi_watchdog_init(struct comedi_subdevice *s, unsigned long iobase)
128 struct addi_watchdog_private *spriv;
130 spriv = comedi_alloc_spriv(s, sizeof(*spriv));
134 spriv->iobase = iobase;
136 s->type = COMEDI_SUBD_TIMER;
137 s->subdev_flags = SDF_WRITEABLE;
140 s->insn_config = addi_watchdog_insn_config;
141 s->insn_read = addi_watchdog_insn_read;
142 s->insn_write = addi_watchdog_insn_write;
146 EXPORT_SYMBOL_GPL(addi_watchdog_init);
148 static int __init addi_watchdog_module_init(void)
152 module_init(addi_watchdog_module_init);
154 static void __exit addi_watchdog_module_exit(void)
157 module_exit(addi_watchdog_module_exit);
159 MODULE_DESCRIPTION("ADDI-DATA Watchdog subdevice");
160 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
161 MODULE_LICENSE("GPL");