blob: edc3b29eed62178f826fc657a1027a9cbbafac3c [file] [log] [blame]
William Breathitt Gray97a445d2016-02-11 11:49:36 -05001/*
William Breathitt Gray4075a282016-08-29 16:22:56 -04002 * IIO driver for the Apex Embedded Systems STX104
William Breathitt Gray97a445d2016-02-11 11:49:36 -05003 * 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>
William Breathitt Gray765550e2016-06-13 09:06:48 -040017#include <linux/gpio/driver.h>
William Breathitt Gray97a445d2016-02-11 11:49:36 -050018#include <linux/iio/iio.h>
19#include <linux/iio/types.h>
20#include <linux/io.h>
21#include <linux/ioport.h>
22#include <linux/isa.h>
William Breathitt Gray4075a282016-08-29 16:22:56 -040023#include <linux/kernel.h>
William Breathitt Gray97a445d2016-02-11 11:49:36 -050024#include <linux/module.h>
25#include <linux/moduleparam.h>
William Breathitt Graye10468b2024-04-17 12:23:38 -040026#include <linux/mutex.h>
William Breathitt Gray765550e2016-06-13 09:06:48 -040027#include <linux/spinlock.h>
William Breathitt Gray97a445d2016-02-11 11:49:36 -050028
William Breathitt Gray4075a282016-08-29 16:22:56 -040029#define STX104_OUT_CHAN(chan) { \
William Breathitt Gray97a445d2016-02-11 11:49:36 -050030 .type = IIO_VOLTAGE, \
31 .channel = chan, \
32 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
33 .indexed = 1, \
34 .output = 1 \
35}
William Breathitt Gray4075a282016-08-29 16:22:56 -040036#define STX104_IN_CHAN(chan, diff) { \
37 .type = IIO_VOLTAGE, \
38 .channel = chan, \
39 .channel2 = chan, \
40 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
41 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE), \
42 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
43 .indexed = 1, \
44 .differential = diff \
45}
46
47#define STX104_NUM_OUT_CHAN 2
William Breathitt Gray97a445d2016-02-11 11:49:36 -050048
49#define STX104_EXTENT 16
William Breathitt Gray97a445d2016-02-11 11:49:36 -050050
William Breathitt Gray9c2cfc12016-05-01 18:44:11 -040051static unsigned int base[max_num_isa_dev(STX104_EXTENT)];
52static unsigned int num_stx104;
David Howells8863b3e2017-04-04 16:54:23 +010053module_param_hw_array(base, uint, ioport, &num_stx104, 0);
William Breathitt Gray97a445d2016-02-11 11:49:36 -050054MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses");
55
56/**
57 * struct stx104_iio - IIO device private data structure
William Breathitt Graye10468b2024-04-17 12:23:38 -040058 * @lock: synchronization lock to prevent I/O race conditions
William Breathitt Gray97a445d2016-02-11 11:49:36 -050059 * @chan_out_states: channels' output states
60 * @base: base port address of the IIO device
61 */
62struct stx104_iio {
William Breathitt Graye10468b2024-04-17 12:23:38 -040063 struct mutex lock;
William Breathitt Gray4075a282016-08-29 16:22:56 -040064 unsigned int chan_out_states[STX104_NUM_OUT_CHAN];
65 unsigned int base;
William Breathitt Gray97a445d2016-02-11 11:49:36 -050066};
67
William Breathitt Gray765550e2016-06-13 09:06:48 -040068/**
69 * struct stx104_gpio - GPIO device private data structure
70 * @chip: instance of the gpio_chip
71 * @lock: synchronization lock to prevent I/O race conditions
72 * @base: base port address of the GPIO device
73 * @out_state: output bits state
74 */
75struct stx104_gpio {
76 struct gpio_chip chip;
77 spinlock_t lock;
78 unsigned int base;
79 unsigned int out_state;
80};
81
William Breathitt Gray97a445d2016-02-11 11:49:36 -050082static int stx104_read_raw(struct iio_dev *indio_dev,
83 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
84{
85 struct stx104_iio *const priv = iio_priv(indio_dev);
William Breathitt Gray4075a282016-08-29 16:22:56 -040086 unsigned int adc_config;
87 int adbu;
88 int gain;
William Breathitt Gray97a445d2016-02-11 11:49:36 -050089
William Breathitt Gray4075a282016-08-29 16:22:56 -040090 switch (mask) {
91 case IIO_CHAN_INFO_HARDWAREGAIN:
92 /* get gain configuration */
93 adc_config = inb(priv->base + 11);
94 gain = adc_config & 0x3;
William Breathitt Gray97a445d2016-02-11 11:49:36 -050095
William Breathitt Gray4075a282016-08-29 16:22:56 -040096 *val = 1 << gain;
97 return IIO_VAL_INT;
98 case IIO_CHAN_INFO_RAW:
99 if (chan->output) {
100 *val = priv->chan_out_states[chan->channel];
101 return IIO_VAL_INT;
102 }
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500103
William Breathitt Gray4075a282016-08-29 16:22:56 -0400104 /* select ADC channel */
105 outb(chan->channel | (chan->channel << 4), priv->base + 2);
106
107 /* trigger ADC sample capture and wait for completion */
108 outb(0, priv->base);
109 while (inb(priv->base + 8) & BIT(7));
110
111 *val = inw(priv->base);
112 return IIO_VAL_INT;
113 case IIO_CHAN_INFO_OFFSET:
114 /* get ADC bipolar/unipolar configuration */
115 adc_config = inb(priv->base + 11);
116 adbu = !(adc_config & BIT(2));
117
118 *val = -32768 * adbu;
119 return IIO_VAL_INT;
120 case IIO_CHAN_INFO_SCALE:
121 /* get ADC bipolar/unipolar and gain configuration */
122 adc_config = inb(priv->base + 11);
123 adbu = !(adc_config & BIT(2));
124 gain = adc_config & 0x3;
125
126 *val = 5;
127 *val2 = 15 - adbu + gain;
128 return IIO_VAL_FRACTIONAL_LOG2;
129 }
130
131 return -EINVAL;
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500132}
133
134static int stx104_write_raw(struct iio_dev *indio_dev,
135 struct iio_chan_spec const *chan, int val, int val2, long mask)
136{
137 struct stx104_iio *const priv = iio_priv(indio_dev);
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500138
William Breathitt Gray4075a282016-08-29 16:22:56 -0400139 switch (mask) {
140 case IIO_CHAN_INFO_HARDWAREGAIN:
141 /* Only four gain states (x1, x2, x4, x8) */
142 switch (val) {
143 case 1:
144 outb(0, priv->base + 11);
145 break;
146 case 2:
147 outb(1, priv->base + 11);
148 break;
149 case 4:
150 outb(2, priv->base + 11);
151 break;
152 case 8:
153 outb(3, priv->base + 11);
154 break;
155 default:
156 return -EINVAL;
157 }
158
159 return 0;
160 case IIO_CHAN_INFO_RAW:
161 if (chan->output) {
162 /* DAC can only accept up to a 16-bit value */
163 if ((unsigned int)val > 65535)
164 return -EINVAL;
165
William Breathitt Graye10468b2024-04-17 12:23:38 -0400166 mutex_lock(&priv->lock);
167
William Breathitt Gray4075a282016-08-29 16:22:56 -0400168 priv->chan_out_states[chan->channel] = val;
169 outw(val, priv->base + 4 + 2 * chan->channel);
170
William Breathitt Graye10468b2024-04-17 12:23:38 -0400171 mutex_unlock(&priv->lock);
William Breathitt Gray4075a282016-08-29 16:22:56 -0400172 return 0;
173 }
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500174 return -EINVAL;
William Breathitt Gray4075a282016-08-29 16:22:56 -0400175 }
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500176
William Breathitt Gray4075a282016-08-29 16:22:56 -0400177 return -EINVAL;
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500178}
179
180static const struct iio_info stx104_info = {
181 .driver_module = THIS_MODULE,
182 .read_raw = stx104_read_raw,
183 .write_raw = stx104_write_raw
184};
185
William Breathitt Gray4075a282016-08-29 16:22:56 -0400186/* single-ended input channels configuration */
187static const struct iio_chan_spec stx104_channels_sing[] = {
188 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
189 STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0),
190 STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0),
191 STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0),
192 STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0),
193 STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0),
194 STX104_IN_CHAN(15, 0)
195};
196/* differential input channels configuration */
197static const struct iio_chan_spec stx104_channels_diff[] = {
198 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
199 STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1),
200 STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1),
201 STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1)
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500202};
203
William Breathitt Gray765550e2016-06-13 09:06:48 -0400204static int stx104_gpio_get_direction(struct gpio_chip *chip,
205 unsigned int offset)
206{
William Breathitt Gray45e98152016-07-19 12:25:00 -0400207 /* GPIO 0-3 are input only, while the rest are output only */
William Breathitt Gray765550e2016-06-13 09:06:48 -0400208 if (offset < 4)
209 return 1;
210
211 return 0;
212}
213
214static int stx104_gpio_direction_input(struct gpio_chip *chip,
215 unsigned int offset)
216{
217 if (offset >= 4)
218 return -EINVAL;
219
220 return 0;
221}
222
223static int stx104_gpio_direction_output(struct gpio_chip *chip,
224 unsigned int offset, int value)
225{
226 if (offset < 4)
227 return -EINVAL;
228
229 chip->set(chip, offset, value);
230 return 0;
231}
232
233static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset)
234{
235 struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
236
237 if (offset >= 4)
238 return -EINVAL;
239
240 return !!(inb(stx104gpio->base) & BIT(offset));
241}
242
243static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset,
244 int value)
245{
246 struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
247 const unsigned int mask = BIT(offset) >> 4;
248 unsigned long flags;
249
250 if (offset < 4)
251 return;
252
253 spin_lock_irqsave(&stx104gpio->lock, flags);
254
255 if (value)
256 stx104gpio->out_state |= mask;
257 else
258 stx104gpio->out_state &= ~mask;
259
260 outb(stx104gpio->out_state, stx104gpio->base);
261
262 spin_unlock_irqrestore(&stx104gpio->lock, flags);
263}
264
William Breathitt Gray7d816e52017-01-30 12:16:04 -0500265#define STX104_NGPIO 8
266static const char *stx104_names[STX104_NGPIO] = {
267 "DIN0", "DIN1", "DIN2", "DIN3", "DOUT0", "DOUT1", "DOUT2", "DOUT3"
268};
269
William Breathitt Graybfe72882017-01-19 10:06:16 -0500270static void stx104_gpio_set_multiple(struct gpio_chip *chip,
271 unsigned long *mask, unsigned long *bits)
272{
273 struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
274 unsigned long flags;
275
276 /* verify masked GPIO are output */
277 if (!(*mask & 0xF0))
278 return;
279
280 *mask >>= 4;
281 *bits >>= 4;
282
283 spin_lock_irqsave(&stx104gpio->lock, flags);
284
285 stx104gpio->out_state &= ~*mask;
286 stx104gpio->out_state |= *mask & *bits;
287 outb(stx104gpio->out_state, stx104gpio->base);
288
289 spin_unlock_irqrestore(&stx104gpio->lock, flags);
290}
291
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500292static int stx104_probe(struct device *dev, unsigned int id)
293{
294 struct iio_dev *indio_dev;
295 struct stx104_iio *priv;
William Breathitt Gray765550e2016-06-13 09:06:48 -0400296 struct stx104_gpio *stx104gpio;
297 int err;
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500298
299 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
300 if (!indio_dev)
301 return -ENOMEM;
302
William Breathitt Gray765550e2016-06-13 09:06:48 -0400303 stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL);
304 if (!stx104gpio)
305 return -ENOMEM;
306
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500307 if (!devm_request_region(dev, base[id], STX104_EXTENT,
308 dev_name(dev))) {
309 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
310 base[id], base[id] + STX104_EXTENT);
311 return -EBUSY;
312 }
313
314 indio_dev->info = &stx104_info;
315 indio_dev->modes = INDIO_DIRECT_MODE;
William Breathitt Gray4075a282016-08-29 16:22:56 -0400316
317 /* determine if differential inputs */
318 if (inb(base[id] + 8) & BIT(5)) {
319 indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff);
320 indio_dev->channels = stx104_channels_diff;
321 } else {
322 indio_dev->num_channels = ARRAY_SIZE(stx104_channels_sing);
323 indio_dev->channels = stx104_channels_sing;
324 }
325
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500326 indio_dev->name = dev_name(dev);
Lars-Peter Clausenc5c7d1f2017-02-11 13:34:14 +0100327 indio_dev->dev.parent = dev;
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500328
329 priv = iio_priv(indio_dev);
330 priv->base = base[id];
331
William Breathitt Graye10468b2024-04-17 12:23:38 -0400332 mutex_init(&priv->lock);
333
William Breathitt Gray4075a282016-08-29 16:22:56 -0400334 /* configure device for software trigger operation */
335 outb(0, base[id] + 9);
336
337 /* initialize gain setting to x1 */
338 outb(0, base[id] + 11);
339
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500340 /* initialize DAC output to 0V */
341 outw(0, base[id] + 4);
342 outw(0, base[id] + 6);
343
William Breathitt Gray765550e2016-06-13 09:06:48 -0400344 stx104gpio->chip.label = dev_name(dev);
345 stx104gpio->chip.parent = dev;
346 stx104gpio->chip.owner = THIS_MODULE;
347 stx104gpio->chip.base = -1;
William Breathitt Gray7d816e52017-01-30 12:16:04 -0500348 stx104gpio->chip.ngpio = STX104_NGPIO;
349 stx104gpio->chip.names = stx104_names;
William Breathitt Gray765550e2016-06-13 09:06:48 -0400350 stx104gpio->chip.get_direction = stx104_gpio_get_direction;
351 stx104gpio->chip.direction_input = stx104_gpio_direction_input;
352 stx104gpio->chip.direction_output = stx104_gpio_direction_output;
353 stx104gpio->chip.get = stx104_gpio_get;
354 stx104gpio->chip.set = stx104_gpio_set;
William Breathitt Graybfe72882017-01-19 10:06:16 -0500355 stx104gpio->chip.set_multiple = stx104_gpio_set_multiple;
William Breathitt Gray765550e2016-06-13 09:06:48 -0400356 stx104gpio->base = base[id] + 3;
357 stx104gpio->out_state = 0x0;
358
359 spin_lock_init(&stx104gpio->lock);
360
William Breathitt Grayb2d226c2017-01-24 15:26:17 -0500361 err = devm_gpiochip_add_data(dev, &stx104gpio->chip, stx104gpio);
William Breathitt Gray765550e2016-06-13 09:06:48 -0400362 if (err) {
363 dev_err(dev, "GPIO registering failed (%d)\n", err);
364 return err;
365 }
366
William Breathitt Grayb2d226c2017-01-24 15:26:17 -0500367 return devm_iio_device_register(dev, indio_dev);
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500368}
369
370static struct isa_driver stx104_driver = {
371 .probe = stx104_probe,
372 .driver = {
373 .name = "stx104"
William Breathitt Gray765550e2016-06-13 09:06:48 -0400374 },
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500375};
376
William Breathitt Gray9c2cfc12016-05-01 18:44:11 -0400377module_isa_driver(stx104_driver, num_stx104);
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500378
379MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
William Breathitt Gray4075a282016-08-29 16:22:56 -0400380MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver");
William Breathitt Gray97a445d2016-02-11 11:49:36 -0500381MODULE_LICENSE("GPL v2");