xref: /openbmc/linux/drivers/iio/dac/cio-dac.c (revision ebd09753)
1 /*
2  * IIO driver for the Measurement Computing CIO-DAC
3  * Copyright (C) 2016 William Breathitt Gray
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License, version 2, as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * This driver supports the following Measurement Computing devices: CIO-DAC16,
15  * CIO-DAC06, and PC104-DAC06.
16  */
17 #include <linux/bitops.h>
18 #include <linux/device.h>
19 #include <linux/errno.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/types.h>
22 #include <linux/io.h>
23 #include <linux/ioport.h>
24 #include <linux/isa.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 
28 #define CIO_DAC_NUM_CHAN 16
29 
30 #define CIO_DAC_CHAN(chan) {				\
31 	.type = IIO_VOLTAGE,				\
32 	.channel = chan,				\
33 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
34 	.indexed = 1,					\
35 	.output = 1					\
36 }
37 
38 #define CIO_DAC_EXTENT 32
39 
40 static unsigned int base[max_num_isa_dev(CIO_DAC_EXTENT)];
41 static unsigned int num_cio_dac;
42 module_param_hw_array(base, uint, ioport, &num_cio_dac, 0);
43 MODULE_PARM_DESC(base, "Measurement Computing CIO-DAC base addresses");
44 
45 /**
46  * struct cio_dac_iio - IIO device private data structure
47  * @chan_out_states:	channels' output states
48  * @base:		base port address of the IIO device
49  */
50 struct cio_dac_iio {
51 	int chan_out_states[CIO_DAC_NUM_CHAN];
52 	unsigned int base;
53 };
54 
55 static int cio_dac_read_raw(struct iio_dev *indio_dev,
56 	struct iio_chan_spec const *chan, int *val, int *val2, long mask)
57 {
58 	struct cio_dac_iio *const priv = iio_priv(indio_dev);
59 
60 	if (mask != IIO_CHAN_INFO_RAW)
61 		return -EINVAL;
62 
63 	*val = priv->chan_out_states[chan->channel];
64 
65 	return IIO_VAL_INT;
66 }
67 
68 static int cio_dac_write_raw(struct iio_dev *indio_dev,
69 	struct iio_chan_spec const *chan, int val, int val2, long mask)
70 {
71 	struct cio_dac_iio *const priv = iio_priv(indio_dev);
72 	const unsigned int chan_addr_offset = 2 * chan->channel;
73 
74 	if (mask != IIO_CHAN_INFO_RAW)
75 		return -EINVAL;
76 
77 	/* DAC can only accept up to a 16-bit value */
78 	if ((unsigned int)val > 65535)
79 		return -EINVAL;
80 
81 	priv->chan_out_states[chan->channel] = val;
82 	outw(val, priv->base + chan_addr_offset);
83 
84 	return 0;
85 }
86 
87 static const struct iio_info cio_dac_info = {
88 	.read_raw = cio_dac_read_raw,
89 	.write_raw = cio_dac_write_raw
90 };
91 
92 static const struct iio_chan_spec cio_dac_channels[CIO_DAC_NUM_CHAN] = {
93 	CIO_DAC_CHAN(0), CIO_DAC_CHAN(1), CIO_DAC_CHAN(2), CIO_DAC_CHAN(3),
94 	CIO_DAC_CHAN(4), CIO_DAC_CHAN(5), CIO_DAC_CHAN(6), CIO_DAC_CHAN(7),
95 	CIO_DAC_CHAN(8), CIO_DAC_CHAN(9), CIO_DAC_CHAN(10), CIO_DAC_CHAN(11),
96 	CIO_DAC_CHAN(12), CIO_DAC_CHAN(13), CIO_DAC_CHAN(14), CIO_DAC_CHAN(15)
97 };
98 
99 static int cio_dac_probe(struct device *dev, unsigned int id)
100 {
101 	struct iio_dev *indio_dev;
102 	struct cio_dac_iio *priv;
103 	unsigned int i;
104 
105 	indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
106 	if (!indio_dev)
107 		return -ENOMEM;
108 
109 	if (!devm_request_region(dev, base[id], CIO_DAC_EXTENT,
110 		dev_name(dev))) {
111 		dev_err(dev, "Unable to request port addresses (0x%X-0x%X)\n",
112 			base[id], base[id] + CIO_DAC_EXTENT);
113 		return -EBUSY;
114 	}
115 
116 	indio_dev->info = &cio_dac_info;
117 	indio_dev->modes = INDIO_DIRECT_MODE;
118 	indio_dev->channels = cio_dac_channels;
119 	indio_dev->num_channels = CIO_DAC_NUM_CHAN;
120 	indio_dev->name = dev_name(dev);
121 	indio_dev->dev.parent = dev;
122 
123 	priv = iio_priv(indio_dev);
124 	priv->base = base[id];
125 
126 	/* initialize DAC outputs to 0V */
127 	for (i = 0; i < 32; i += 2)
128 		outw(0, base[id] + i);
129 
130 	return devm_iio_device_register(dev, indio_dev);
131 }
132 
133 static struct isa_driver cio_dac_driver = {
134 	.probe = cio_dac_probe,
135 	.driver = {
136 		.name = "cio-dac"
137 	}
138 };
139 
140 module_isa_driver(cio_dac_driver, num_cio_dac);
141 
142 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
143 MODULE_DESCRIPTION("Measurement Computing CIO-DAC IIO driver");
144 MODULE_LICENSE("GPL v2");
145