From ad7a370c8be47247f68f7187cc82f4f25a347116 Mon Sep 17 00:00:00 2001 From: H Hartley Sweeten Date: Wed, 7 Jan 2015 17:19:32 -0700 Subject: [PATCH] staging: comedi: aio_iiro_16: add command support for change of state detection This board supports interrupts on change of state of the digital inputs. Add the necessary subdevice support and interrupt handler to allow async commands to detect the change of state. Signed-off-by: H Hartley Sweeten Reviewed-by: Ian Abbott Signed-off-by: Greg Kroah-Hartman --- drivers/staging/comedi/drivers/aio_iiro_16.c | 116 ++++++++++++++++++- 1 file changed, 111 insertions(+), 5 deletions(-) diff --git a/drivers/staging/comedi/drivers/aio_iiro_16.c b/drivers/staging/comedi/drivers/aio_iiro_16.c index f84fb7446994..ae6700637a3b 100644 --- a/drivers/staging/comedi/drivers/aio_iiro_16.c +++ b/drivers/staging/comedi/drivers/aio_iiro_16.c @@ -23,16 +23,49 @@ * * Configuration Options: * [0] - I/O port base address + * [1] - IRQ (optional) + * + * The board supports interrupts on change of state of the digital inputs. + * The sample data returned by the async command indicates which inputs + * changed state: + * + * Bit 7 - IRQ Enable (1) / Disable (0) + * Bit 1 - Input 8-15 Changed State (1 = Changed, 0 = No Change) + * Bit 0 - Input 0-7 Changed State (1 = Changed, 0 = No Change) */ #include +#include + #include "../comedidev.h" -#define AIO_IIRO_16_RELAY_0_7 0x00 -#define AIO_IIRO_16_INPUT_0_7 0x01 -#define AIO_IIRO_16_IRQ 0x02 -#define AIO_IIRO_16_RELAY_8_15 0x04 -#define AIO_IIRO_16_INPUT_8_15 0x05 +#include "comedi_fc.h" + +#define AIO_IIRO_16_RELAY_0_7 0x00 +#define AIO_IIRO_16_INPUT_0_7 0x01 +#define AIO_IIRO_16_IRQ 0x02 +#define AIO_IIRO_16_RELAY_8_15 0x04 +#define AIO_IIRO_16_INPUT_8_15 0x05 +#define AIO_IIRO_16_STATUS 0x07 +#define AIO_IIRO_16_STATUS_IRQE BIT(7) +#define AIO_IIRO_16_STATUS_INPUT_8_15 BIT(1) +#define AIO_IIRO_16_STATUS_INPUT_0_7 BIT(0) + +static irqreturn_t aio_iiro_16_cos(int irq, void *d) +{ + struct comedi_device *dev = d; + struct comedi_subdevice *s = dev->read_subdev; + unsigned int status; + + status = inb(dev->iobase + AIO_IIRO_16_STATUS); + if (!(status & AIO_IIRO_16_STATUS_IRQE)) + return IRQ_NONE; + + comedi_buf_write_samples(s, &status, 1); + comedi_handle_events(dev, s); + + return IRQ_HANDLED; +} static void aio_iiro_enable_irq(struct comedi_device *dev, bool enable) { @@ -42,6 +75,60 @@ static void aio_iiro_enable_irq(struct comedi_device *dev, bool enable) outb(0, dev->iobase + AIO_IIRO_16_IRQ); } +static int aio_iiro_16_cos_cancel(struct comedi_device *dev, + struct comedi_subdevice *s) +{ + aio_iiro_enable_irq(dev, false); + + return 0; +} + +static int aio_iiro_16_cos_cmd(struct comedi_device *dev, + struct comedi_subdevice *s) +{ + aio_iiro_enable_irq(dev, true); + + return 0; +} + +static int aio_iiro_16_cos_cmdtest(struct comedi_device *dev, + struct comedi_subdevice *s, + struct comedi_cmd *cmd) +{ + int err = 0; + + /* Step 1 : check if triggers are trivially valid */ + + err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW); + err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT); + err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW); + err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT); + err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_NONE); + + if (err) + return 1; + + /* Step 2a : make sure trigger sources are unique */ + /* Step 2b : and mutually compatible */ + + /* Step 3: check if arguments are trivially valid */ + + err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0); + err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0); + err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0); + err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len); + err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0); + + if (err) + return 3; + + /* Step 4: fix up any arguments */ + + /* Step 5: check channel list if it exists */ + + return 0; +} + static int aio_iiro_16_do_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s, struct comedi_insn *insn, @@ -82,6 +169,17 @@ static int aio_iiro_16_attach(struct comedi_device *dev, aio_iiro_enable_irq(dev, false); + /* + * Digital input change of state interrupts are optionally supported + * using IRQ 2-7, 10-12, 14, or 15. + */ + if ((1 << it->options[1]) & 0xdcfc) { + ret = request_irq(it->options[1], aio_iiro_16_cos, 0, + dev->board_name, dev); + if (ret == 0) + dev->irq = it->options[1]; + } + ret = comedi_alloc_subdevices(dev, 2); if (ret) return ret; @@ -107,6 +205,14 @@ static int aio_iiro_16_attach(struct comedi_device *dev, s->maxdata = 1; s->range_table = &range_digital; s->insn_bits = aio_iiro_16_di_insn_bits; + if (dev->irq) { + dev->read_subdev = s; + s->subdev_flags |= SDF_CMD_READ; + s->len_chanlist = 1; + s->do_cmdtest = aio_iiro_16_cos_cmdtest; + s->do_cmd = aio_iiro_16_cos_cmd; + s->cancel = aio_iiro_16_cos_cancel; + } return 0; } -- 2.20.1