Skip to content

Commit 97a445d

Browse files
William Breathitt Grayjic23
authored andcommitted
iio: Add IIO support for the DAC on the Apex Embedded Systems STX104
The Apex Embedded Systems STX104 is a 16-channel 16-bit analog input and 2-channel 16-bit analog output PC/104 card. The STX104 incorporates a large one mega-sample FIFO. This driver provides IIO support for the 2-channel DAC on the STX104. The base port addresses for the devices may be configured via the "base" module parameter array. Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent fd64df1 commit 97a445d

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

‎MAINTAINERS‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,12 @@ L: alsa-devel@alsa-project.org (moderated for non-subscribers)
778778
S: Maintained
779779
F: sound/aoa/
780780

781+
APEX EMBEDDED SYSTEMS STX104 DAC DRIVER
782+
M: William Breathitt Gray <vilhelm.gray@gmail.com>
783+
L: linux-iio@vger.kernel.org
784+
S: Maintained
785+
F: drivers/iio/dac/stx104.c
786+
781787
APM DRIVER
782788
M: Jiri Kosina <jikos@kernel.org>
783789
S: Odd fixes

‎drivers/iio/dac/Kconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,13 @@ config MCP4922
208208
To compile this driver as a module, choose M here: the module
209209
will be called mcp4922.
210210

211+
config STX104
212+
tristate "Apex Embedded Systems STX104 DAC driver"
213+
depends on ISA
214+
help
215+
Say yes here to build support for the 2-channel DAC on the Apex
216+
Embedded Systems STX104 integrated analog PC/104 card. The base port
217+
addresses for the devices may be configured via the "base" module
218+
parameter array.
219+
211220
endmenu

‎drivers/iio/dac/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ obj-$(CONFIG_MAX517) += max517.o
2222
obj-$(CONFIG_MAX5821) += max5821.o
2323
obj-$(CONFIG_MCP4725) += mcp4725.o
2424
obj-$(CONFIG_MCP4922) += mcp4922.o
25+
obj-$(CONFIG_STX104) += stx104.o

‎drivers/iio/dac/stx104.c‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* DAC driver for the Apex Embedded Systems STX104
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+
#include <linux/bitops.h>
15+
#include <linux/device.h>
16+
#include <linux/errno.h>
17+
#include <linux/iio/iio.h>
18+
#include <linux/iio/types.h>
19+
#include <linux/io.h>
20+
#include <linux/ioport.h>
21+
#include <linux/isa.h>
22+
#include <linux/module.h>
23+
#include <linux/moduleparam.h>
24+
25+
#define STX104_NUM_CHAN 2
26+
27+
#define STX104_CHAN(chan) { \
28+
.type = IIO_VOLTAGE, \
29+
.channel = chan, \
30+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
31+
.indexed = 1, \
32+
.output = 1 \
33+
}
34+
35+
#define STX104_EXTENT 16
36+
/**
37+
* The highest base address possible for an ISA device is 0x3FF; this results in
38+
* 1024 possible base addresses. Dividing the number of possible base addresses
39+
* by the address extent taken by each device results in the maximum number of
40+
* devices on a system.
41+
*/
42+
#define MAX_NUM_STX104 (1024 / STX104_EXTENT)
43+
44+
static unsigned base[MAX_NUM_STX104];
45+
static unsigned num_stx104;
46+
module_param_array(base, uint, &num_stx104, 0);
47+
MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses");
48+
49+
/**
50+
* struct stx104_iio - IIO device private data structure
51+
* @chan_out_states: channels' output states
52+
* @base: base port address of the IIO device
53+
*/
54+
struct stx104_iio {
55+
unsigned chan_out_states[STX104_NUM_CHAN];
56+
unsigned base;
57+
};
58+
59+
static int stx104_read_raw(struct iio_dev *indio_dev,
60+
struct iio_chan_spec const *chan, int *val, int *val2, long mask)
61+
{
62+
struct stx104_iio *const priv = iio_priv(indio_dev);
63+
64+
if (mask != IIO_CHAN_INFO_RAW)
65+
return -EINVAL;
66+
67+
*val = priv->chan_out_states[chan->channel];
68+
69+
return IIO_VAL_INT;
70+
}
71+
72+
static int stx104_write_raw(struct iio_dev *indio_dev,
73+
struct iio_chan_spec const *chan, int val, int val2, long mask)
74+
{
75+
struct stx104_iio *const priv = iio_priv(indio_dev);
76+
const unsigned chan_addr_offset = 2 * chan->channel;
77+
78+
if (mask != IIO_CHAN_INFO_RAW)
79+
return -EINVAL;
80+
81+
priv->chan_out_states[chan->channel] = val;
82+
outw(val, priv->base + 4 + chan_addr_offset);
83+
84+
return 0;
85+
}
86+
87+
static const struct iio_info stx104_info = {
88+
.driver_module = THIS_MODULE,
89+
.read_raw = stx104_read_raw,
90+
.write_raw = stx104_write_raw
91+
};
92+
93+
static const struct iio_chan_spec stx104_channels[STX104_NUM_CHAN] = {
94+
STX104_CHAN(0),
95+
STX104_CHAN(1)
96+
};
97+
98+
static int stx104_probe(struct device *dev, unsigned int id)
99+
{
100+
struct iio_dev *indio_dev;
101+
struct stx104_iio *priv;
102+
103+
indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
104+
if (!indio_dev)
105+
return -ENOMEM;
106+
107+
if (!devm_request_region(dev, base[id], STX104_EXTENT,
108+
dev_name(dev))) {
109+
dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
110+
base[id], base[id] + STX104_EXTENT);
111+
return -EBUSY;
112+
}
113+
114+
indio_dev->info = &stx104_info;
115+
indio_dev->modes = INDIO_DIRECT_MODE;
116+
indio_dev->channels = stx104_channels;
117+
indio_dev->num_channels = STX104_NUM_CHAN;
118+
indio_dev->name = dev_name(dev);
119+
120+
priv = iio_priv(indio_dev);
121+
priv->base = base[id];
122+
123+
/* initialize DAC output to 0V */
124+
outw(0, base[id] + 4);
125+
outw(0, base[id] + 6);
126+
127+
return devm_iio_device_register(dev, indio_dev);
128+
}
129+
130+
static struct isa_driver stx104_driver = {
131+
.probe = stx104_probe,
132+
.driver = {
133+
.name = "stx104"
134+
}
135+
};
136+
137+
static void __exit stx104_exit(void)
138+
{
139+
isa_unregister_driver(&stx104_driver);
140+
}
141+
142+
static int __init stx104_init(void)
143+
{
144+
return isa_register_driver(&stx104_driver, num_stx104);
145+
}
146+
147+
module_init(stx104_init);
148+
module_exit(stx104_exit);
149+
150+
MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
151+
MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
152+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)