1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO driver for the Measurement Computing CIO-DAC
4 * Copyright (C) 2016 William Breathitt Gray
6 * This driver supports the following Measurement Computing devices: CIO-DAC16,
7 * CIO-DAC06, and PC104-DAC06.
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/types.h>
15 #include <linux/ioport.h>
16 #include <linux/isa.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
20 #define CIO_DAC_NUM_CHAN 16
22 #define CIO_DAC_CHAN(chan) { \
23 .type = IIO_VOLTAGE, \
25 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
30 #define CIO_DAC_EXTENT 32
32 static unsigned int base
[max_num_isa_dev(CIO_DAC_EXTENT
)];
33 static unsigned int num_cio_dac
;
34 module_param_hw_array(base
, uint
, ioport
, &num_cio_dac
, 0);
35 MODULE_PARM_DESC(base
, "Measurement Computing CIO-DAC base addresses");
38 * struct cio_dac_iio - IIO device private data structure
39 * @chan_out_states: channels' output states
40 * @base: base port address of the IIO device
43 int chan_out_states
[CIO_DAC_NUM_CHAN
];
47 static int cio_dac_read_raw(struct iio_dev
*indio_dev
,
48 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
50 struct cio_dac_iio
*const priv
= iio_priv(indio_dev
);
52 if (mask
!= IIO_CHAN_INFO_RAW
)
55 *val
= priv
->chan_out_states
[chan
->channel
];
60 static int cio_dac_write_raw(struct iio_dev
*indio_dev
,
61 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
63 struct cio_dac_iio
*const priv
= iio_priv(indio_dev
);
64 const unsigned int chan_addr_offset
= 2 * chan
->channel
;
66 if (mask
!= IIO_CHAN_INFO_RAW
)
69 /* DAC can only accept up to a 16-bit value */
70 if ((unsigned int)val
> 65535)
73 priv
->chan_out_states
[chan
->channel
] = val
;
74 outw(val
, priv
->base
+ chan_addr_offset
);
79 static const struct iio_info cio_dac_info
= {
80 .read_raw
= cio_dac_read_raw
,
81 .write_raw
= cio_dac_write_raw
84 static const struct iio_chan_spec cio_dac_channels
[CIO_DAC_NUM_CHAN
] = {
85 CIO_DAC_CHAN(0), CIO_DAC_CHAN(1), CIO_DAC_CHAN(2), CIO_DAC_CHAN(3),
86 CIO_DAC_CHAN(4), CIO_DAC_CHAN(5), CIO_DAC_CHAN(6), CIO_DAC_CHAN(7),
87 CIO_DAC_CHAN(8), CIO_DAC_CHAN(9), CIO_DAC_CHAN(10), CIO_DAC_CHAN(11),
88 CIO_DAC_CHAN(12), CIO_DAC_CHAN(13), CIO_DAC_CHAN(14), CIO_DAC_CHAN(15)
91 static int cio_dac_probe(struct device
*dev
, unsigned int id
)
93 struct iio_dev
*indio_dev
;
94 struct cio_dac_iio
*priv
;
97 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
101 if (!devm_request_region(dev
, base
[id
], CIO_DAC_EXTENT
,
103 dev_err(dev
, "Unable to request port addresses (0x%X-0x%X)\n",
104 base
[id
], base
[id
] + CIO_DAC_EXTENT
);
108 indio_dev
->info
= &cio_dac_info
;
109 indio_dev
->modes
= INDIO_DIRECT_MODE
;
110 indio_dev
->channels
= cio_dac_channels
;
111 indio_dev
->num_channels
= CIO_DAC_NUM_CHAN
;
112 indio_dev
->name
= dev_name(dev
);
114 priv
= iio_priv(indio_dev
);
115 priv
->base
= base
[id
];
117 /* initialize DAC outputs to 0V */
118 for (i
= 0; i
< 32; i
+= 2)
119 outw(0, base
[id
] + i
);
121 return devm_iio_device_register(dev
, indio_dev
);
124 static struct isa_driver cio_dac_driver
= {
125 .probe
= cio_dac_probe
,
131 module_isa_driver(cio_dac_driver
, num_cio_dac
);
133 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
134 MODULE_DESCRIPTION("Measurement Computing CIO-DAC IIO driver");
135 MODULE_LICENSE("GPL v2");