1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Amplicon PCI263 relay board.
4  *
5  * Copyright (C) 2002 MEV Ltd. <https://www.mev.co.uk/>
6  *
7  * COMEDI - Linux Control and Measurement Device Interface
8  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
9  */
10 
11 /*
12  * Driver: amplc_pci263
13  * Description: Amplicon PCI263
14  * Author: Ian Abbott <abbotti@mev.co.uk>
15  * Devices: [Amplicon] PCI263 (amplc_pci263)
16  * Updated: Fri, 12 Apr 2013 15:19:36 +0100
17  * Status: works
18  *
19  * Configuration options: not applicable, uses PCI auto config
20  *
21  * The board appears as one subdevice, with 16 digital outputs, each
22  * connected to a reed-relay. Relay contacts are closed when output is 1.
23  * The state of the outputs can be read.
24  */
25 
26 #include <linux/module.h>
27 
28 #include "../comedi_pci.h"
29 
30 /* PCI263 registers */
31 #define PCI263_DO_0_7_REG	0x00
32 #define PCI263_DO_8_15_REG	0x01
33 
pci263_do_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)34 static int pci263_do_insn_bits(struct comedi_device *dev,
35 			       struct comedi_subdevice *s,
36 			       struct comedi_insn *insn,
37 			       unsigned int *data)
38 {
39 	if (comedi_dio_update_state(s, data)) {
40 		outb(s->state & 0xff, dev->iobase + PCI263_DO_0_7_REG);
41 		outb((s->state >> 8) & 0xff, dev->iobase + PCI263_DO_8_15_REG);
42 	}
43 
44 	data[1] = s->state;
45 
46 	return insn->n;
47 }
48 
pci263_auto_attach(struct comedi_device * dev,unsigned long context_unused)49 static int pci263_auto_attach(struct comedi_device *dev,
50 			      unsigned long context_unused)
51 {
52 	struct pci_dev *pci_dev = comedi_to_pci_dev(dev);
53 	struct comedi_subdevice *s;
54 	int ret;
55 
56 	ret = comedi_pci_enable(dev);
57 	if (ret)
58 		return ret;
59 
60 	dev->iobase = pci_resource_start(pci_dev, 2);
61 	ret = comedi_alloc_subdevices(dev, 1);
62 	if (ret)
63 		return ret;
64 
65 	/* Digital Output subdevice */
66 	s = &dev->subdevices[0];
67 	s->type		= COMEDI_SUBD_DO;
68 	s->subdev_flags	= SDF_WRITABLE;
69 	s->n_chan	= 16;
70 	s->maxdata	= 1;
71 	s->range_table	= &range_digital;
72 	s->insn_bits	= pci263_do_insn_bits;
73 
74 	/* read initial relay state */
75 	s->state = inb(dev->iobase + PCI263_DO_0_7_REG) |
76 		   (inb(dev->iobase + PCI263_DO_8_15_REG) << 8);
77 
78 	return 0;
79 }
80 
81 static struct comedi_driver amplc_pci263_driver = {
82 	.driver_name	= "amplc_pci263",
83 	.module		= THIS_MODULE,
84 	.auto_attach	= pci263_auto_attach,
85 	.detach		= comedi_pci_detach,
86 };
87 
88 static const struct pci_device_id pci263_pci_table[] = {
89 	{ PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, 0x000c) },
90 	{0}
91 };
92 MODULE_DEVICE_TABLE(pci, pci263_pci_table);
93 
amplc_pci263_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)94 static int amplc_pci263_pci_probe(struct pci_dev *dev,
95 				  const struct pci_device_id *id)
96 {
97 	return comedi_pci_auto_config(dev, &amplc_pci263_driver,
98 				      id->driver_data);
99 }
100 
101 static struct pci_driver amplc_pci263_pci_driver = {
102 	.name		= "amplc_pci263",
103 	.id_table	= pci263_pci_table,
104 	.probe		= &amplc_pci263_pci_probe,
105 	.remove		= comedi_pci_auto_unconfig,
106 };
107 module_comedi_pci_driver(amplc_pci263_driver, amplc_pci263_pci_driver);
108 
109 MODULE_AUTHOR("Comedi https://www.comedi.org");
110 MODULE_DESCRIPTION("Comedi driver for Amplicon PCI263 relay board");
111 MODULE_LICENSE("GPL");
112