Merge branch 'parisc-4.6-4' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[deliverable/linux.git] / drivers / iio / dac / stx104.c
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");
This page took 0.055061 seconds and 5 git commands to generate.