2 * IIO driver for the Apex Embedded Systems STX104
3 * Copyright (C) 2016 William Breathitt Gray
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.
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.
14 #include <linux/bitops.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/types.h>
21 #include <linux/ioport.h>
22 #include <linux/isa.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/spinlock.h>
28 #define STX104_OUT_CHAN(chan) { \
29 .type = IIO_VOLTAGE, \
31 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
35 #define STX104_IN_CHAN(chan, diff) { \
36 .type = IIO_VOLTAGE, \
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), \
43 .differential = diff \
46 #define STX104_NUM_OUT_CHAN 2
48 #define STX104_EXTENT 16
50 static unsigned int base
[max_num_isa_dev(STX104_EXTENT
)];
51 static unsigned int num_stx104
;
52 module_param_array(base
, uint
, &num_stx104
, 0);
53 MODULE_PARM_DESC(base
, "Apex Embedded Systems STX104 base addresses");
56 * struct stx104_iio - IIO device private data structure
57 * @chan_out_states: channels' output states
58 * @base: base port address of the IIO device
61 unsigned int chan_out_states
[STX104_NUM_OUT_CHAN
];
66 * struct stx104_gpio - GPIO device private data structure
67 * @chip: instance of the gpio_chip
68 * @lock: synchronization lock to prevent I/O race conditions
69 * @base: base port address of the GPIO device
70 * @out_state: output bits state
73 struct gpio_chip chip
;
76 unsigned int out_state
;
80 * struct stx104_dev - STX104 device private data structure
81 * @indio_dev: IIO device
82 * @chip: instance of the gpio_chip
85 struct iio_dev
*indio_dev
;
86 struct gpio_chip
*chip
;
89 static int stx104_read_raw(struct iio_dev
*indio_dev
,
90 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
92 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
93 unsigned int adc_config
;
98 case IIO_CHAN_INFO_HARDWAREGAIN
:
99 /* get gain configuration */
100 adc_config
= inb(priv
->base
+ 11);
101 gain
= adc_config
& 0x3;
105 case IIO_CHAN_INFO_RAW
:
107 *val
= priv
->chan_out_states
[chan
->channel
];
111 /* select ADC channel */
112 outb(chan
->channel
| (chan
->channel
<< 4), priv
->base
+ 2);
114 /* trigger ADC sample capture and wait for completion */
116 while (inb(priv
->base
+ 8) & BIT(7));
118 *val
= inw(priv
->base
);
120 case IIO_CHAN_INFO_OFFSET
:
121 /* get ADC bipolar/unipolar configuration */
122 adc_config
= inb(priv
->base
+ 11);
123 adbu
= !(adc_config
& BIT(2));
125 *val
= -32768 * adbu
;
127 case IIO_CHAN_INFO_SCALE
:
128 /* get ADC bipolar/unipolar and gain configuration */
129 adc_config
= inb(priv
->base
+ 11);
130 adbu
= !(adc_config
& BIT(2));
131 gain
= adc_config
& 0x3;
134 *val2
= 15 - adbu
+ gain
;
135 return IIO_VAL_FRACTIONAL_LOG2
;
141 static int stx104_write_raw(struct iio_dev
*indio_dev
,
142 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
144 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
147 case IIO_CHAN_INFO_HARDWAREGAIN
:
148 /* Only four gain states (x1, x2, x4, x8) */
151 outb(0, priv
->base
+ 11);
154 outb(1, priv
->base
+ 11);
157 outb(2, priv
->base
+ 11);
160 outb(3, priv
->base
+ 11);
167 case IIO_CHAN_INFO_RAW
:
169 /* DAC can only accept up to a 16-bit value */
170 if ((unsigned int)val
> 65535)
173 priv
->chan_out_states
[chan
->channel
] = val
;
174 outw(val
, priv
->base
+ 4 + 2 * chan
->channel
);
184 static const struct iio_info stx104_info
= {
185 .driver_module
= THIS_MODULE
,
186 .read_raw
= stx104_read_raw
,
187 .write_raw
= stx104_write_raw
190 /* single-ended input channels configuration */
191 static const struct iio_chan_spec stx104_channels_sing
[] = {
192 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
193 STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0),
194 STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0),
195 STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0),
196 STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0),
197 STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0),
198 STX104_IN_CHAN(15, 0)
200 /* differential input channels configuration */
201 static const struct iio_chan_spec stx104_channels_diff
[] = {
202 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
203 STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1),
204 STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1),
205 STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1)
208 static int stx104_gpio_get_direction(struct gpio_chip
*chip
,
211 /* GPIO 0-3 are input only, while the rest are output only */
218 static int stx104_gpio_direction_input(struct gpio_chip
*chip
,
227 static int stx104_gpio_direction_output(struct gpio_chip
*chip
,
228 unsigned int offset
, int value
)
233 chip
->set(chip
, offset
, value
);
237 static int stx104_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
239 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
244 return !!(inb(stx104gpio
->base
) & BIT(offset
));
247 static void stx104_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
250 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
251 const unsigned int mask
= BIT(offset
) >> 4;
257 spin_lock_irqsave(&stx104gpio
->lock
, flags
);
260 stx104gpio
->out_state
|= mask
;
262 stx104gpio
->out_state
&= ~mask
;
264 outb(stx104gpio
->out_state
, stx104gpio
->base
);
266 spin_unlock_irqrestore(&stx104gpio
->lock
, flags
);
269 static int stx104_probe(struct device
*dev
, unsigned int id
)
271 struct iio_dev
*indio_dev
;
272 struct stx104_iio
*priv
;
273 struct stx104_gpio
*stx104gpio
;
274 struct stx104_dev
*stx104dev
;
277 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
281 stx104gpio
= devm_kzalloc(dev
, sizeof(*stx104gpio
), GFP_KERNEL
);
285 stx104dev
= devm_kzalloc(dev
, sizeof(*stx104dev
), GFP_KERNEL
);
289 if (!devm_request_region(dev
, base
[id
], STX104_EXTENT
,
291 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
292 base
[id
], base
[id
] + STX104_EXTENT
);
296 indio_dev
->info
= &stx104_info
;
297 indio_dev
->modes
= INDIO_DIRECT_MODE
;
299 /* determine if differential inputs */
300 if (inb(base
[id
] + 8) & BIT(5)) {
301 indio_dev
->num_channels
= ARRAY_SIZE(stx104_channels_diff
);
302 indio_dev
->channels
= stx104_channels_diff
;
304 indio_dev
->num_channels
= ARRAY_SIZE(stx104_channels_sing
);
305 indio_dev
->channels
= stx104_channels_sing
;
308 indio_dev
->name
= dev_name(dev
);
310 priv
= iio_priv(indio_dev
);
311 priv
->base
= base
[id
];
313 /* configure device for software trigger operation */
314 outb(0, base
[id
] + 9);
316 /* initialize gain setting to x1 */
317 outb(0, base
[id
] + 11);
319 /* initialize DAC output to 0V */
320 outw(0, base
[id
] + 4);
321 outw(0, base
[id
] + 6);
323 stx104gpio
->chip
.label
= dev_name(dev
);
324 stx104gpio
->chip
.parent
= dev
;
325 stx104gpio
->chip
.owner
= THIS_MODULE
;
326 stx104gpio
->chip
.base
= -1;
327 stx104gpio
->chip
.ngpio
= 8;
328 stx104gpio
->chip
.get_direction
= stx104_gpio_get_direction
;
329 stx104gpio
->chip
.direction_input
= stx104_gpio_direction_input
;
330 stx104gpio
->chip
.direction_output
= stx104_gpio_direction_output
;
331 stx104gpio
->chip
.get
= stx104_gpio_get
;
332 stx104gpio
->chip
.set
= stx104_gpio_set
;
333 stx104gpio
->base
= base
[id
] + 3;
334 stx104gpio
->out_state
= 0x0;
336 spin_lock_init(&stx104gpio
->lock
);
338 stx104dev
->indio_dev
= indio_dev
;
339 stx104dev
->chip
= &stx104gpio
->chip
;
340 dev_set_drvdata(dev
, stx104dev
);
342 err
= gpiochip_add_data(&stx104gpio
->chip
, stx104gpio
);
344 dev_err(dev
, "GPIO registering failed (%d)\n", err
);
348 err
= iio_device_register(indio_dev
);
350 dev_err(dev
, "IIO device registering failed (%d)\n", err
);
351 gpiochip_remove(&stx104gpio
->chip
);
358 static int stx104_remove(struct device
*dev
, unsigned int id
)
360 struct stx104_dev
*const stx104dev
= dev_get_drvdata(dev
);
362 iio_device_unregister(stx104dev
->indio_dev
);
363 gpiochip_remove(stx104dev
->chip
);
368 static struct isa_driver stx104_driver
= {
369 .probe
= stx104_probe
,
373 .remove
= stx104_remove
376 module_isa_driver(stx104_driver
, num_stx104
);
378 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
379 MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver");
380 MODULE_LICENSE("GPL v2");