staging: comedi: introduce addi_watchdog driver
authorH Hartley Sweeten <hsweeten@visionengravers.com>
Fri, 18 Jan 2013 00:41:01 +0000 (17:41 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 18 Jan 2013 20:52:54 +0000 (12:52 -0800)
Many of the ADDI-DATA drivers have a "watchdog" subdevice that can
be used to monitor digital output activity. All the digital outputs
are released (set to 0) if the digital outputs are not accessed, or
the watchdog it not pinged, before the timeout of the watchdog
occurs. The only difference in the drivers for the watchdog subdevice
is the base address used to talk to the watchdog registers.

Instead of duplicating the code needed to support this watchdog,
introduce a helper module, similar to the 8255 module. This module
will be select'ed by the drivers that can use it.

Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Cc: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/comedi/Kconfig
drivers/staging/comedi/drivers/Makefile
drivers/staging/comedi/drivers/addi_watchdog.c [new file with mode: 0644]
drivers/staging/comedi/drivers/addi_watchdog.h [new file with mode: 0644]

index 36eec320569c6bd14d077be6328b34ae4fede36f..8f2bef3434bd11e7d4d04a255bd6b8751428cc5d 100644 (file)
@@ -567,6 +567,13 @@ config COMEDI_8255_PCI
          To compile this driver as a module, choose M here: the module will
          be called 8255_pci.
 
+config COMEDI_ADDI_WATCHDOG
+       tristate
+       ---help---
+         Provides support for the watchdog subdevice found on many ADDI-DATA
+         boards. This module will be automatically selected when needed. The
+         module will be called addi_watchdog.
+
 config COMEDI_ADDI_APCI_035
        tristate "ADDI-DATA APCI_035 support"
        ---help---
index 3a04e3035c2119e1829501c32aecac4fd88bb11c..fd74e7ecb4008d3758355b7acf67ae019058bffb 100644 (file)
@@ -55,6 +55,7 @@ obj-$(CONFIG_COMEDI_POC)              += poc.o
 
 # Comedi PCI drivers
 obj-$(CONFIG_COMEDI_8255_PCI)          += 8255_pci.o
+obj-$(CONFIG_COMEDI_ADDI_WATCHDOG)     += addi_watchdog.o
 obj-$(CONFIG_COMEDI_ADDI_APCI_035)     += addi_apci_035.o
 obj-$(CONFIG_COMEDI_ADDI_APCI_1032)    += addi_apci_1032.o
 obj-$(CONFIG_COMEDI_ADDI_APCI_1500)    += addi_apci_1500.o
diff --git a/drivers/staging/comedi/drivers/addi_watchdog.c b/drivers/staging/comedi/drivers/addi_watchdog.c
new file mode 100644 (file)
index 0000000..b944503
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * COMEDI driver for the watchdog subdevice found on some addi-data boards
+ * Copyright (c) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
+ *
+ * Based on implementations in various addi-data COMEDI drivers.
+ *
+ * COMEDI - Linux Control and Measurement Device Interface
+ * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include "../comedidev.h"
+#include "addi_watchdog.h"
+
+/*
+ * Register offsets/defines for the addi-data watchdog
+ */
+#define ADDI_WDOG_REG                  0x00
+#define ADDI_WDOG_RELOAD_REG           0x04
+#define ADDI_WDOG_TIMEBASE             0x08
+#define ADDI_WDOG_CTRL_REG             0x0c
+#define ADDI_WDOG_CTRL_ENABLE          (1 << 0)
+#define ADDI_WDOG_CTRL_SW_TRIG         (1 << 9)
+#define ADDI_WDOG_STATUS_REG           0x10
+#define ADDI_WDOG_STATUS_ENABLED       (1 << 0)
+#define ADDI_WDOG_STATUS_SW_TRIG       (1 << 1)
+
+struct addi_watchdog_private {
+       unsigned long iobase;
+       unsigned int wdog_ctrl;
+};
+
+/*
+ * The watchdog subdevice is configured with two INSN_CONFIG instructions:
+ *
+ * Enable the watchdog and set the reload timeout:
+ *     data[0] = INSN_CONFIG_ARM
+ *     data[1] = timeout reload value
+ *
+ * Disable the watchdog:
+ *     data[0] = INSN_CONFIG_DISARM
+ */
+static int addi_watchdog_insn_config(struct comedi_device *dev,
+                                    struct comedi_subdevice *s,
+                                    struct comedi_insn *insn,
+                                    unsigned int *data)
+{
+       struct addi_watchdog_private *spriv = s->private;
+       unsigned int reload;
+
+       switch (data[0]) {
+       case INSN_CONFIG_ARM:
+               spriv->wdog_ctrl = ADDI_WDOG_CTRL_ENABLE;
+               reload = data[1] & s->maxdata;
+               outw(reload, spriv->iobase + ADDI_WDOG_RELOAD_REG);
+
+               /* Time base is 20ms, let the user know the timeout */
+               dev_info(dev->class_dev, "watchdog enabled, timeout:%dms\n",
+                       20 * reload + 20);
+               break;
+       case INSN_CONFIG_DISARM:
+               spriv->wdog_ctrl = 0;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       outw(spriv->wdog_ctrl, spriv->iobase + ADDI_WDOG_CTRL_REG);
+
+       return insn->n;
+}
+
+static int addi_watchdog_insn_read(struct comedi_device *dev,
+                                  struct comedi_subdevice *s,
+                                  struct comedi_insn *insn,
+                                  unsigned int *data)
+{
+       struct addi_watchdog_private *spriv = s->private;
+       int i;
+
+       for (i = 0; i < insn->n; i++)
+               data[i] = inl(spriv->iobase + ADDI_WDOG_STATUS_REG);
+
+       return insn->n;
+}
+
+static int addi_watchdog_insn_write(struct comedi_device *dev,
+                                   struct comedi_subdevice *s,
+                                   struct comedi_insn *insn,
+                                   unsigned int *data)
+{
+       struct addi_watchdog_private *spriv = s->private;
+       int i;
+
+       if (spriv->wdog_ctrl == 0) {
+               dev_warn(dev->class_dev, "watchdog is disabled\n");
+               return -EINVAL;
+       }
+
+       /* "ping" the watchdog */
+       for (i = 0; i < insn->n; i++) {
+               outw(spriv->wdog_ctrl | ADDI_WDOG_CTRL_SW_TRIG,
+                    spriv->iobase + ADDI_WDOG_CTRL_REG);
+       }
+
+       return insn->n;
+}
+
+void addi_watchdog_reset(unsigned long iobase)
+{
+       outl(0x0, iobase + ADDI_WDOG_CTRL_REG);
+       outl(0x0, iobase + ADDI_WDOG_RELOAD_REG);
+}
+EXPORT_SYMBOL_GPL(addi_watchdog_reset);
+
+int addi_watchdog_init(struct comedi_subdevice *s, unsigned long iobase)
+{
+       struct addi_watchdog_private *spriv;
+
+       spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
+       if (!spriv)
+               return -ENOMEM;
+
+       spriv->iobase = iobase;
+
+       s->private      = spriv;
+
+       s->type         = COMEDI_SUBD_TIMER;
+       s->subdev_flags = SDF_WRITEABLE;
+       s->n_chan       = 1;
+       s->maxdata      = 0xff;
+       s->insn_config  = addi_watchdog_insn_config;
+       s->insn_read    = addi_watchdog_insn_read;
+       s->insn_write   = addi_watchdog_insn_write;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(addi_watchdog_init);
+
+void addi_watchdog_cleanup(struct comedi_subdevice *s)
+{
+       kfree(s->private);
+}
+EXPORT_SYMBOL_GPL(addi_watchdog_cleanup);
+
+static int __init addi_watchdog_module_init(void)
+{
+       return 0;
+}
+module_init(addi_watchdog_module_init);
+
+static void __exit addi_watchdog_module_exit(void)
+{
+}
+module_exit(addi_watchdog_module_exit);
+
+MODULE_DESCRIPTION("ADDI-DATA Watchdog subdevice");
+MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
+MODULE_LICENSE("GPL");
diff --git a/drivers/staging/comedi/drivers/addi_watchdog.h b/drivers/staging/comedi/drivers/addi_watchdog.h
new file mode 100644 (file)
index 0000000..f374a7b
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef _ADDI_WATCHDOG_H
+#define _ADDI_WATCHDOG_H
+
+#include "../comedidev.h"
+
+void addi_watchdog_reset(unsigned long iobase);
+int addi_watchdog_init(struct comedi_subdevice *, unsigned long iobase);
+void addi_watchdog_cleanup(struct comedi_subdevice *s);
+
+#endif
This page took 0.028143 seconds and 5 git commands to generate.