Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / comedi / drivers / addi_watchdog.c
1 /*
2  * COMEDI driver for the watchdog subdevice found on some addi-data boards
3  * Copyright (c) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
4  *
5  * Based on implementations in various addi-data COMEDI drivers.
6  *
7  * COMEDI - Linux Control and Measurement Device Interface
8  * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
9  *
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.
14  *
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.
19  */
20
21 #include <linux/module.h>
22 #include "../comedidev.h"
23 #include "addi_tcw.h"
24 #include "addi_watchdog.h"
25
26 struct addi_watchdog_private {
27         unsigned long iobase;
28         unsigned int wdog_ctrl;
29 };
30
31 /*
32  * The watchdog subdevice is configured with two INSN_CONFIG instructions:
33  *
34  * Enable the watchdog and set the reload timeout:
35  *      data[0] = INSN_CONFIG_ARM
36  *      data[1] = timeout reload value
37  *
38  * Disable the watchdog:
39  *      data[0] = INSN_CONFIG_DISARM
40  */
41 static int addi_watchdog_insn_config(struct comedi_device *dev,
42                                      struct comedi_subdevice *s,
43                                      struct comedi_insn *insn,
44                                      unsigned int *data)
45 {
46         struct addi_watchdog_private *spriv = s->private;
47         unsigned int reload;
48
49         switch (data[0]) {
50         case INSN_CONFIG_ARM:
51                 spriv->wdog_ctrl = ADDI_TCW_CTRL_ENA;
52                 reload = data[1] & s->maxdata;
53                 outl(reload, spriv->iobase + ADDI_TCW_RELOAD_REG);
54
55                 /* Time base is 20ms, let the user know the timeout */
56                 dev_info(dev->class_dev, "watchdog enabled, timeout:%dms\n",
57                          20 * reload + 20);
58                 break;
59         case INSN_CONFIG_DISARM:
60                 spriv->wdog_ctrl = 0;
61                 break;
62         default:
63                 return -EINVAL;
64         }
65
66         outl(spriv->wdog_ctrl, spriv->iobase + ADDI_TCW_CTRL_REG);
67
68         return insn->n;
69 }
70
71 static int addi_watchdog_insn_read(struct comedi_device *dev,
72                                    struct comedi_subdevice *s,
73                                    struct comedi_insn *insn,
74                                    unsigned int *data)
75 {
76         struct addi_watchdog_private *spriv = s->private;
77         int i;
78
79         for (i = 0; i < insn->n; i++)
80                 data[i] = inl(spriv->iobase + ADDI_TCW_STATUS_REG);
81
82         return insn->n;
83 }
84
85 static int addi_watchdog_insn_write(struct comedi_device *dev,
86                                     struct comedi_subdevice *s,
87                                     struct comedi_insn *insn,
88                                     unsigned int *data)
89 {
90         struct addi_watchdog_private *spriv = s->private;
91         int i;
92
93         if (spriv->wdog_ctrl == 0) {
94                 dev_warn(dev->class_dev, "watchdog is disabled\n");
95                 return -EINVAL;
96         }
97
98         /* "ping" the watchdog */
99         for (i = 0; i < insn->n; i++) {
100                 outl(spriv->wdog_ctrl | ADDI_TCW_CTRL_TRIG,
101                      spriv->iobase + ADDI_TCW_CTRL_REG);
102         }
103
104         return insn->n;
105 }
106
107 void addi_watchdog_reset(unsigned long iobase)
108 {
109         outl(0x0, iobase + ADDI_TCW_CTRL_REG);
110         outl(0x0, iobase + ADDI_TCW_RELOAD_REG);
111 }
112 EXPORT_SYMBOL_GPL(addi_watchdog_reset);
113
114 int addi_watchdog_init(struct comedi_subdevice *s, unsigned long iobase)
115 {
116         struct addi_watchdog_private *spriv;
117
118         spriv = comedi_alloc_spriv(s, sizeof(*spriv));
119         if (!spriv)
120                 return -ENOMEM;
121
122         spriv->iobase = iobase;
123
124         s->type         = COMEDI_SUBD_TIMER;
125         s->subdev_flags = SDF_WRITABLE;
126         s->n_chan       = 1;
127         s->maxdata      = 0xff;
128         s->insn_config  = addi_watchdog_insn_config;
129         s->insn_read    = addi_watchdog_insn_read;
130         s->insn_write   = addi_watchdog_insn_write;
131
132         return 0;
133 }
134 EXPORT_SYMBOL_GPL(addi_watchdog_init);
135
136 static int __init addi_watchdog_module_init(void)
137 {
138         return 0;
139 }
140 module_init(addi_watchdog_module_init);
141
142 static void __exit addi_watchdog_module_exit(void)
143 {
144 }
145 module_exit(addi_watchdog_module_exit);
146
147 MODULE_DESCRIPTION("ADDI-DATA Watchdog subdevice");
148 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
149 MODULE_LICENSE("GPL");