1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO driver for the Apex Embedded Systems STX104
4 * Copyright (C) 2016 William Breathitt Gray
6 #include <linux/bitops.h>
7 #include <linux/device.h>
8 #include <linux/errno.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/iio/iio.h>
11 #include <linux/iio/types.h>
13 #include <linux/ioport.h>
14 #include <linux/isa.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/spinlock.h>
19 #include <linux/types.h>
21 #define STX104_OUT_CHAN(chan) { \
22 .type = IIO_VOLTAGE, \
24 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
28 #define STX104_IN_CHAN(chan, diff) { \
29 .type = IIO_VOLTAGE, \
32 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
33 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE), \
34 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
36 .differential = diff \
39 #define STX104_NUM_OUT_CHAN 2
41 #define STX104_EXTENT 16
43 static unsigned int base[max_num_isa_dev(STX104_EXTENT)];
44 static unsigned int num_stx104;
45 module_param_hw_array(base, uint, ioport, &num_stx104, 0);
46 MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses");
49 * struct stx104_reg - device register structure
50 * @ssr_ad: Software Strobe Register and ADC Data
54 * @cir_asr: Clear Interrupts and ADC Status
56 * @pccr_fsh: Pacer Clock Control and FIFO Status MSB
57 * @acfg: ADC Configuration
71 * struct stx104_iio - IIO device private data structure
72 * @chan_out_states: channels' output states
73 * @reg: I/O address offset for the device registers
76 unsigned int chan_out_states[STX104_NUM_OUT_CHAN];
77 struct stx104_reg __iomem *reg;
81 * struct stx104_gpio - GPIO device private data structure
82 * @chip: instance of the gpio_chip
83 * @lock: synchronization lock to prevent I/O race conditions
84 * @base: base port address of the GPIO device
85 * @out_state: output bits state
88 struct gpio_chip chip;
91 unsigned int out_state;
94 static int stx104_read_raw(struct iio_dev *indio_dev,
95 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
97 struct stx104_iio *const priv = iio_priv(indio_dev);
98 struct stx104_reg __iomem *const reg = priv->reg;
99 unsigned int adc_config;
104 case IIO_CHAN_INFO_HARDWAREGAIN:
105 /* get gain configuration */
106 adc_config = ioread8(®->acfg);
107 gain = adc_config & 0x3;
111 case IIO_CHAN_INFO_RAW:
113 *val = priv->chan_out_states[chan->channel];
117 /* select ADC channel */
118 iowrite8(chan->channel | (chan->channel << 4), ®->achan);
120 /* trigger ADC sample capture by writing to the 8-bit
121 * Software Strobe Register and wait for completion
123 iowrite8(0, ®->ssr_ad);
124 while (ioread8(®->cir_asr) & BIT(7));
126 *val = ioread16(®->ssr_ad);
128 case IIO_CHAN_INFO_OFFSET:
129 /* get ADC bipolar/unipolar configuration */
130 adc_config = ioread8(®->acfg);
131 adbu = !(adc_config & BIT(2));
133 *val = -32768 * adbu;
135 case IIO_CHAN_INFO_SCALE:
136 /* get ADC bipolar/unipolar and gain configuration */
137 adc_config = ioread8(®->acfg);
138 adbu = !(adc_config & BIT(2));
139 gain = adc_config & 0x3;
142 *val2 = 15 - adbu + gain;
143 return IIO_VAL_FRACTIONAL_LOG2;
149 static int stx104_write_raw(struct iio_dev *indio_dev,
150 struct iio_chan_spec const *chan, int val, int val2, long mask)
152 struct stx104_iio *const priv = iio_priv(indio_dev);
155 case IIO_CHAN_INFO_HARDWAREGAIN:
156 /* Only four gain states (x1, x2, x4, x8) */
159 iowrite8(0, &priv->reg->acfg);
162 iowrite8(1, &priv->reg->acfg);
165 iowrite8(2, &priv->reg->acfg);
168 iowrite8(3, &priv->reg->acfg);
175 case IIO_CHAN_INFO_RAW:
177 /* DAC can only accept up to a 16-bit value */
178 if ((unsigned int)val > 65535)
181 priv->chan_out_states[chan->channel] = val;
182 iowrite16(val, &priv->reg->dac[chan->channel]);
192 static const struct iio_info stx104_info = {
193 .read_raw = stx104_read_raw,
194 .write_raw = stx104_write_raw
197 /* single-ended input channels configuration */
198 static const struct iio_chan_spec stx104_channels_sing[] = {
199 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
200 STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0),
201 STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0),
202 STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0),
203 STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0),
204 STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0),
205 STX104_IN_CHAN(15, 0)
207 /* differential input channels configuration */
208 static const struct iio_chan_spec stx104_channels_diff[] = {
209 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
210 STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1),
211 STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1),
212 STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1)
215 static int stx104_gpio_get_direction(struct gpio_chip *chip,
218 /* GPIO 0-3 are input only, while the rest are output only */
225 static int stx104_gpio_direction_input(struct gpio_chip *chip,
234 static int stx104_gpio_direction_output(struct gpio_chip *chip,
235 unsigned int offset, int value)
240 chip->set(chip, offset, value);
244 static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset)
246 struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
251 return !!(ioread8(stx104gpio->base) & BIT(offset));
254 static int stx104_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
257 struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
259 *bits = ioread8(stx104gpio->base);
264 static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset,
267 struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
268 const unsigned int mask = BIT(offset) >> 4;
274 spin_lock_irqsave(&stx104gpio->lock, flags);
277 stx104gpio->out_state |= mask;
279 stx104gpio->out_state &= ~mask;
281 iowrite8(stx104gpio->out_state, stx104gpio->base);
283 spin_unlock_irqrestore(&stx104gpio->lock, flags);
286 #define STX104_NGPIO 8
287 static const char *stx104_names[STX104_NGPIO] = {
288 "DIN0", "DIN1", "DIN2", "DIN3", "DOUT0", "DOUT1", "DOUT2", "DOUT3"
291 static void stx104_gpio_set_multiple(struct gpio_chip *chip,
292 unsigned long *mask, unsigned long *bits)
294 struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
297 /* verify masked GPIO are output */
304 spin_lock_irqsave(&stx104gpio->lock, flags);
306 stx104gpio->out_state &= ~*mask;
307 stx104gpio->out_state |= *mask & *bits;
308 iowrite8(stx104gpio->out_state, stx104gpio->base);
310 spin_unlock_irqrestore(&stx104gpio->lock, flags);
313 static int stx104_probe(struct device *dev, unsigned int id)
315 struct iio_dev *indio_dev;
316 struct stx104_iio *priv;
317 struct stx104_gpio *stx104gpio;
320 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
324 stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL);
328 if (!devm_request_region(dev, base[id], STX104_EXTENT,
330 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
331 base[id], base[id] + STX104_EXTENT);
335 priv = iio_priv(indio_dev);
336 priv->reg = devm_ioport_map(dev, base[id], STX104_EXTENT);
340 indio_dev->info = &stx104_info;
341 indio_dev->modes = INDIO_DIRECT_MODE;
343 /* determine if differential inputs */
344 if (ioread8(&priv->reg->cir_asr) & BIT(5)) {
345 indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff);
346 indio_dev->channels = stx104_channels_diff;
348 indio_dev->num_channels = ARRAY_SIZE(stx104_channels_sing);
349 indio_dev->channels = stx104_channels_sing;
352 indio_dev->name = dev_name(dev);
354 /* configure device for software trigger operation */
355 iowrite8(0, &priv->reg->acr);
357 /* initialize gain setting to x1 */
358 iowrite8(0, &priv->reg->acfg);
360 /* initialize DAC output to 0V */
361 iowrite16(0, &priv->reg->dac[0]);
362 iowrite16(0, &priv->reg->dac[1]);
364 stx104gpio->chip.label = dev_name(dev);
365 stx104gpio->chip.parent = dev;
366 stx104gpio->chip.owner = THIS_MODULE;
367 stx104gpio->chip.base = -1;
368 stx104gpio->chip.ngpio = STX104_NGPIO;
369 stx104gpio->chip.names = stx104_names;
370 stx104gpio->chip.get_direction = stx104_gpio_get_direction;
371 stx104gpio->chip.direction_input = stx104_gpio_direction_input;
372 stx104gpio->chip.direction_output = stx104_gpio_direction_output;
373 stx104gpio->chip.get = stx104_gpio_get;
374 stx104gpio->chip.get_multiple = stx104_gpio_get_multiple;
375 stx104gpio->chip.set = stx104_gpio_set;
376 stx104gpio->chip.set_multiple = stx104_gpio_set_multiple;
377 stx104gpio->base = &priv->reg->dio;
378 stx104gpio->out_state = 0x0;
380 spin_lock_init(&stx104gpio->lock);
382 err = devm_gpiochip_add_data(dev, &stx104gpio->chip, stx104gpio);
384 dev_err(dev, "GPIO registering failed (%d)\n", err);
388 return devm_iio_device_register(dev, indio_dev);
391 static struct isa_driver stx104_driver = {
392 .probe = stx104_probe,
398 module_isa_driver(stx104_driver, num_stx104);
400 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
401 MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver");
402 MODULE_LICENSE("GPL v2");