Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: stx104: Add IIO support for the ADC channels

The Apex Embedded Systems STX104 features 16 channels of single-ended (8
channels of true differential) 16-bit analog input. Differential input
configuration may be selected via a physical jumper on the device.
Similarly, input polarity (unipolar/bipolar) is configured via a
physical jumper on the device.

Input gain selection is available to the user via software, thus
allowing eight possible input ranges: +-10V, +-5V, +-2.5V, +-1.25V,
0 to 10V, 0 to 5V, 0 to 2.5V, and 0 to 1.25V. Four input gain
configurations are supported: x1, x2, x4, and x8.

This ADC resolution is 16-bits (1/65536 of full scale). Analog input
samples are taken on software trigger; neither FIFO sampling nor
interrupt triggering is supported by this driver.

The Apex Embedded Systems STX104 is primarily an analog-to-digital
converter device. The STX104 IIO driver was initially placed in the DAC
directory because only the DAC portion of the STX104 was supported at
the time. Now that ADC support has been added to the STX104 IIO driver,
the driver should be moved to the more appropriate ADC directory.

Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

William Breathitt Gray and committed by
Jonathan Cameron
4075a283 c0609919

+145 -35
+2 -2
MAINTAINERS
··· 809 809 S: Maintained 810 810 F: sound/aoa/ 811 811 812 - APEX EMBEDDED SYSTEMS STX104 DAC DRIVER 812 + APEX EMBEDDED SYSTEMS STX104 IIO DRIVER 813 813 M: William Breathitt Gray <vilhelm.gray@gmail.com> 814 814 L: linux-iio@vger.kernel.org 815 815 S: Maintained 816 - F: drivers/iio/dac/stx104.c 816 + F: drivers/iio/adc/stx104.c 817 817 818 818 APM DRIVER 819 819 M: Jiri Kosina <jikos@kernel.org>
+15
drivers/iio/adc/Kconfig
··· 418 418 To compile this driver as a module, choose M here: the 419 419 module will be called rockchip_saradc. 420 420 421 + config STX104 422 + tristate "Apex Embedded Systems STX104 driver" 423 + depends on X86 && ISA_BUS_API 424 + select GPIOLIB 425 + help 426 + Say yes here to build support for the Apex Embedded Systems STX104 427 + integrated analog PC/104 card. 428 + 429 + This driver supports the 16 channels of single-ended (8 channels of 430 + differential) analog inputs, 2 channels of analog output, 4 digital 431 + inputs, and 4 digital outputs provided by the STX104. 432 + 433 + The base port addresses for the devices may be configured via the base 434 + array module parameter. 435 + 421 436 config TI_ADC081C 422 437 tristate "Texas Instruments ADC081C/ADC101C/ADC121C family" 423 438 depends on I2C
+1
drivers/iio/adc/Makefile
··· 40 40 obj-$(CONFIG_QCOM_SPMI_IADC) += qcom-spmi-iadc.o 41 41 obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o 42 42 obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o 43 + obj-$(CONFIG_STX104) += stx104.o 43 44 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o 44 45 obj-$(CONFIG_TI_ADC0832) += ti-adc0832.o 45 46 obj-$(CONFIG_TI_ADC12138) += ti-adc12138.o
-10
drivers/iio/dac/Kconfig
··· 264 264 To compile this driver as a module, choose M here: the module 265 265 will be called mcp4922. 266 266 267 - config STX104 268 - tristate "Apex Embedded Systems STX104 DAC driver" 269 - depends on X86 && ISA_BUS_API 270 - select GPIOLIB 271 - help 272 - Say yes here to build support for the 2-channel DAC and GPIO on the 273 - Apex Embedded Systems STX104 integrated analog PC/104 card. The base 274 - port addresses for the devices may be configured via the base array 275 - module parameter. 276 - 277 267 config VF610_DAC 278 268 tristate "Vybrid vf610 DAC driver" 279 269 depends on OF
-1
drivers/iio/dac/Makefile
··· 28 28 obj-$(CONFIG_MAX5821) += max5821.o 29 29 obj-$(CONFIG_MCP4725) += mcp4725.o 30 30 obj-$(CONFIG_MCP4922) += mcp4922.o 31 - obj-$(CONFIG_STX104) += stx104.o 32 31 obj-$(CONFIG_VF610_DAC) += vf610_dac.o
+127 -22
drivers/iio/dac/stx104.c drivers/iio/adc/stx104.c
··· 1 1 /* 2 - * DAC driver for the Apex Embedded Systems STX104 2 + * IIO driver for the Apex Embedded Systems STX104 3 3 * Copyright (C) 2016 William Breathitt Gray 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify ··· 20 20 #include <linux/io.h> 21 21 #include <linux/ioport.h> 22 22 #include <linux/isa.h> 23 + #include <linux/kernel.h> 23 24 #include <linux/module.h> 24 25 #include <linux/moduleparam.h> 25 26 #include <linux/spinlock.h> 26 27 27 - #define STX104_NUM_CHAN 2 28 - 29 - #define STX104_CHAN(chan) { \ 28 + #define STX104_OUT_CHAN(chan) { \ 30 29 .type = IIO_VOLTAGE, \ 31 30 .channel = chan, \ 32 31 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 33 32 .indexed = 1, \ 34 33 .output = 1 \ 35 34 } 35 + #define STX104_IN_CHAN(chan, diff) { \ 36 + .type = IIO_VOLTAGE, \ 37 + .channel = chan, \ 38 + .channel2 = chan, \ 39 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \ 40 + BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE), \ 41 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 42 + .indexed = 1, \ 43 + .differential = diff \ 44 + } 45 + 46 + #define STX104_NUM_OUT_CHAN 2 36 47 37 48 #define STX104_EXTENT 16 38 49 ··· 58 47 * @base: base port address of the IIO device 59 48 */ 60 49 struct stx104_iio { 61 - unsigned chan_out_states[STX104_NUM_CHAN]; 62 - unsigned base; 50 + unsigned int chan_out_states[STX104_NUM_OUT_CHAN]; 51 + unsigned int base; 63 52 }; 64 53 65 54 /** ··· 80 69 struct iio_chan_spec const *chan, int *val, int *val2, long mask) 81 70 { 82 71 struct stx104_iio *const priv = iio_priv(indio_dev); 72 + unsigned int adc_config; 73 + int adbu; 74 + int gain; 83 75 84 - if (mask != IIO_CHAN_INFO_RAW) 85 - return -EINVAL; 76 + switch (mask) { 77 + case IIO_CHAN_INFO_HARDWAREGAIN: 78 + /* get gain configuration */ 79 + adc_config = inb(priv->base + 11); 80 + gain = adc_config & 0x3; 86 81 87 - *val = priv->chan_out_states[chan->channel]; 82 + *val = 1 << gain; 83 + return IIO_VAL_INT; 84 + case IIO_CHAN_INFO_RAW: 85 + if (chan->output) { 86 + *val = priv->chan_out_states[chan->channel]; 87 + return IIO_VAL_INT; 88 + } 88 89 89 - return IIO_VAL_INT; 90 + /* select ADC channel */ 91 + outb(chan->channel | (chan->channel << 4), priv->base + 2); 92 + 93 + /* trigger ADC sample capture and wait for completion */ 94 + outb(0, priv->base); 95 + while (inb(priv->base + 8) & BIT(7)); 96 + 97 + *val = inw(priv->base); 98 + return IIO_VAL_INT; 99 + case IIO_CHAN_INFO_OFFSET: 100 + /* get ADC bipolar/unipolar configuration */ 101 + adc_config = inb(priv->base + 11); 102 + adbu = !(adc_config & BIT(2)); 103 + 104 + *val = -32768 * adbu; 105 + return IIO_VAL_INT; 106 + case IIO_CHAN_INFO_SCALE: 107 + /* get ADC bipolar/unipolar and gain configuration */ 108 + adc_config = inb(priv->base + 11); 109 + adbu = !(adc_config & BIT(2)); 110 + gain = adc_config & 0x3; 111 + 112 + *val = 5; 113 + *val2 = 15 - adbu + gain; 114 + return IIO_VAL_FRACTIONAL_LOG2; 115 + } 116 + 117 + return -EINVAL; 90 118 } 91 119 92 120 static int stx104_write_raw(struct iio_dev *indio_dev, 93 121 struct iio_chan_spec const *chan, int val, int val2, long mask) 94 122 { 95 123 struct stx104_iio *const priv = iio_priv(indio_dev); 96 - const unsigned chan_addr_offset = 2 * chan->channel; 97 124 98 - if (mask != IIO_CHAN_INFO_RAW) 125 + switch (mask) { 126 + case IIO_CHAN_INFO_HARDWAREGAIN: 127 + /* Only four gain states (x1, x2, x4, x8) */ 128 + switch (val) { 129 + case 1: 130 + outb(0, priv->base + 11); 131 + break; 132 + case 2: 133 + outb(1, priv->base + 11); 134 + break; 135 + case 4: 136 + outb(2, priv->base + 11); 137 + break; 138 + case 8: 139 + outb(3, priv->base + 11); 140 + break; 141 + default: 142 + return -EINVAL; 143 + } 144 + 145 + return 0; 146 + case IIO_CHAN_INFO_RAW: 147 + if (chan->output) { 148 + /* DAC can only accept up to a 16-bit value */ 149 + if ((unsigned int)val > 65535) 150 + return -EINVAL; 151 + 152 + priv->chan_out_states[chan->channel] = val; 153 + outw(val, priv->base + 4 + 2 * chan->channel); 154 + 155 + return 0; 156 + } 99 157 return -EINVAL; 158 + } 100 159 101 - priv->chan_out_states[chan->channel] = val; 102 - outw(val, priv->base + 4 + chan_addr_offset); 103 - 104 - return 0; 160 + return -EINVAL; 105 161 } 106 162 107 163 static const struct iio_info stx104_info = { ··· 177 99 .write_raw = stx104_write_raw 178 100 }; 179 101 180 - static const struct iio_chan_spec stx104_channels[STX104_NUM_CHAN] = { 181 - STX104_CHAN(0), 182 - STX104_CHAN(1) 102 + /* single-ended input channels configuration */ 103 + static const struct iio_chan_spec stx104_channels_sing[] = { 104 + STX104_OUT_CHAN(0), STX104_OUT_CHAN(1), 105 + STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0), 106 + STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0), 107 + STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0), 108 + STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0), 109 + STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0), 110 + STX104_IN_CHAN(15, 0) 111 + }; 112 + /* differential input channels configuration */ 113 + static const struct iio_chan_spec stx104_channels_diff[] = { 114 + STX104_OUT_CHAN(0), STX104_OUT_CHAN(1), 115 + STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1), 116 + STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1), 117 + STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1) 183 118 }; 184 119 185 120 static int stx104_gpio_get_direction(struct gpio_chip *chip, ··· 279 188 280 189 indio_dev->info = &stx104_info; 281 190 indio_dev->modes = INDIO_DIRECT_MODE; 282 - indio_dev->channels = stx104_channels; 283 - indio_dev->num_channels = STX104_NUM_CHAN; 191 + 192 + /* determine if differential inputs */ 193 + if (inb(base[id] + 8) & BIT(5)) { 194 + indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff); 195 + indio_dev->channels = stx104_channels_diff; 196 + } else { 197 + indio_dev->num_channels = ARRAY_SIZE(stx104_channels_sing); 198 + indio_dev->channels = stx104_channels_sing; 199 + } 200 + 284 201 indio_dev->name = dev_name(dev); 285 202 286 203 priv = iio_priv(indio_dev); 287 204 priv->base = base[id]; 205 + 206 + /* configure device for software trigger operation */ 207 + outb(0, base[id] + 9); 208 + 209 + /* initialize gain setting to x1 */ 210 + outb(0, base[id] + 11); 288 211 289 212 /* initialize DAC output to 0V */ 290 213 outw(0, base[id] + 4); ··· 356 251 module_isa_driver(stx104_driver, num_stx104); 357 252 358 253 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); 359 - MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver"); 254 + MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver"); 360 255 MODULE_LICENSE("GPL v2");