• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * IIO 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/gpio/driver.h>
18 #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>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/spinlock.h>
27 
28 #define STX104_OUT_CHAN(chan) {				\
29 	.type = IIO_VOLTAGE,				\
30 	.channel = chan,				\
31 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
32 	.indexed = 1,					\
33 	.output = 1					\
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
47 
48 #define STX104_EXTENT 16
49 
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");
54 
55 /**
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
59  */
60 struct stx104_iio {
61 	unsigned int chan_out_states[STX104_NUM_OUT_CHAN];
62 	unsigned int base;
63 };
64 
65 /**
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
71  */
72 struct stx104_gpio {
73 	struct gpio_chip chip;
74 	spinlock_t lock;
75 	unsigned int base;
76 	unsigned int out_state;
77 };
78 
79 /**
80  * struct stx104_dev - STX104 device private data structure
81  * @indio_dev:	IIO device
82  * @chip:	instance of the gpio_chip
83  */
84 struct stx104_dev {
85 	struct iio_dev *indio_dev;
86 	struct gpio_chip *chip;
87 };
88 
stx104_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)89 static int stx104_read_raw(struct iio_dev *indio_dev,
90 	struct iio_chan_spec const *chan, int *val, int *val2, long mask)
91 {
92 	struct stx104_iio *const priv = iio_priv(indio_dev);
93 	unsigned int adc_config;
94 	int adbu;
95 	int gain;
96 
97 	switch (mask) {
98 	case IIO_CHAN_INFO_HARDWAREGAIN:
99 		/* get gain configuration */
100 		adc_config = inb(priv->base + 11);
101 		gain = adc_config & 0x3;
102 
103 		*val = 1 << gain;
104 		return IIO_VAL_INT;
105 	case IIO_CHAN_INFO_RAW:
106 		if (chan->output) {
107 			*val = priv->chan_out_states[chan->channel];
108 			return IIO_VAL_INT;
109 		}
110 
111 		/* select ADC channel */
112 		outb(chan->channel | (chan->channel << 4), priv->base + 2);
113 
114 		/* trigger ADC sample capture and wait for completion */
115 		outb(0, priv->base);
116 		while (inb(priv->base + 8) & BIT(7));
117 
118 		*val = inw(priv->base);
119 		return IIO_VAL_INT;
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));
124 
125 		*val = -32768 * adbu;
126 		return IIO_VAL_INT;
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;
132 
133 		*val = 5;
134 		*val2 = 15 - adbu + gain;
135 		return IIO_VAL_FRACTIONAL_LOG2;
136 	}
137 
138 	return -EINVAL;
139 }
140 
stx104_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)141 static int stx104_write_raw(struct iio_dev *indio_dev,
142 	struct iio_chan_spec const *chan, int val, int val2, long mask)
143 {
144 	struct stx104_iio *const priv = iio_priv(indio_dev);
145 
146 	switch (mask) {
147 	case IIO_CHAN_INFO_HARDWAREGAIN:
148 		/* Only four gain states (x1, x2, x4, x8) */
149 		switch (val) {
150 		case 1:
151 			outb(0, priv->base + 11);
152 			break;
153 		case 2:
154 			outb(1, priv->base + 11);
155 			break;
156 		case 4:
157 			outb(2, priv->base + 11);
158 			break;
159 		case 8:
160 			outb(3, priv->base + 11);
161 			break;
162 		default:
163 			return -EINVAL;
164 		}
165 
166 		return 0;
167 	case IIO_CHAN_INFO_RAW:
168 		if (chan->output) {
169 			/* DAC can only accept up to a 16-bit value */
170 			if ((unsigned int)val > 65535)
171 				return -EINVAL;
172 
173 			priv->chan_out_states[chan->channel] = val;
174 			outw(val, priv->base + 4 + 2 * chan->channel);
175 
176 			return 0;
177 		}
178 		return -EINVAL;
179 	}
180 
181 	return -EINVAL;
182 }
183 
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
188 };
189 
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)
199 };
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)
206 };
207 
stx104_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)208 static int stx104_gpio_get_direction(struct gpio_chip *chip,
209 	unsigned int offset)
210 {
211 	/* GPIO 0-3 are input only, while the rest are output only */
212 	if (offset < 4)
213 		return 1;
214 
215 	return 0;
216 }
217 
stx104_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)218 static int stx104_gpio_direction_input(struct gpio_chip *chip,
219 	unsigned int offset)
220 {
221 	if (offset >= 4)
222 		return -EINVAL;
223 
224 	return 0;
225 }
226 
stx104_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int value)227 static int stx104_gpio_direction_output(struct gpio_chip *chip,
228 	unsigned int offset, int value)
229 {
230 	if (offset < 4)
231 		return -EINVAL;
232 
233 	chip->set(chip, offset, value);
234 	return 0;
235 }
236 
stx104_gpio_get(struct gpio_chip * chip,unsigned int offset)237 static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset)
238 {
239 	struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
240 
241 	if (offset >= 4)
242 		return -EINVAL;
243 
244 	return !!(inb(stx104gpio->base) & BIT(offset));
245 }
246 
stx104_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)247 static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset,
248 	int value)
249 {
250 	struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
251 	const unsigned int mask = BIT(offset) >> 4;
252 	unsigned long flags;
253 
254 	if (offset < 4)
255 		return;
256 
257 	spin_lock_irqsave(&stx104gpio->lock, flags);
258 
259 	if (value)
260 		stx104gpio->out_state |= mask;
261 	else
262 		stx104gpio->out_state &= ~mask;
263 
264 	outb(stx104gpio->out_state, stx104gpio->base);
265 
266 	spin_unlock_irqrestore(&stx104gpio->lock, flags);
267 }
268 
stx104_probe(struct device * dev,unsigned int id)269 static int stx104_probe(struct device *dev, unsigned int id)
270 {
271 	struct iio_dev *indio_dev;
272 	struct stx104_iio *priv;
273 	struct stx104_gpio *stx104gpio;
274 	struct stx104_dev *stx104dev;
275 	int err;
276 
277 	indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
278 	if (!indio_dev)
279 		return -ENOMEM;
280 
281 	stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL);
282 	if (!stx104gpio)
283 		return -ENOMEM;
284 
285 	stx104dev = devm_kzalloc(dev, sizeof(*stx104dev), GFP_KERNEL);
286 	if (!stx104dev)
287 		return -ENOMEM;
288 
289 	if (!devm_request_region(dev, base[id], STX104_EXTENT,
290 		dev_name(dev))) {
291 		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
292 			base[id], base[id] + STX104_EXTENT);
293 		return -EBUSY;
294 	}
295 
296 	indio_dev->info = &stx104_info;
297 	indio_dev->modes = INDIO_DIRECT_MODE;
298 
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;
303 	} else {
304 		indio_dev->num_channels = ARRAY_SIZE(stx104_channels_sing);
305 		indio_dev->channels = stx104_channels_sing;
306 	}
307 
308 	indio_dev->name = dev_name(dev);
309 
310 	priv = iio_priv(indio_dev);
311 	priv->base = base[id];
312 
313 	/* configure device for software trigger operation */
314 	outb(0, base[id] + 9);
315 
316 	/* initialize gain setting to x1 */
317 	outb(0, base[id] + 11);
318 
319 	/* initialize DAC output to 0V */
320 	outw(0, base[id] + 4);
321 	outw(0, base[id] + 6);
322 
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;
335 
336 	spin_lock_init(&stx104gpio->lock);
337 
338 	stx104dev->indio_dev = indio_dev;
339 	stx104dev->chip = &stx104gpio->chip;
340 	dev_set_drvdata(dev, stx104dev);
341 
342 	err = gpiochip_add_data(&stx104gpio->chip, stx104gpio);
343 	if (err) {
344 		dev_err(dev, "GPIO registering failed (%d)\n", err);
345 		return err;
346 	}
347 
348 	err = iio_device_register(indio_dev);
349 	if (err) {
350 		dev_err(dev, "IIO device registering failed (%d)\n", err);
351 		gpiochip_remove(&stx104gpio->chip);
352 		return err;
353 	}
354 
355 	return 0;
356 }
357 
stx104_remove(struct device * dev,unsigned int id)358 static int stx104_remove(struct device *dev, unsigned int id)
359 {
360 	struct stx104_dev *const stx104dev = dev_get_drvdata(dev);
361 
362 	iio_device_unregister(stx104dev->indio_dev);
363 	gpiochip_remove(stx104dev->chip);
364 
365 	return 0;
366 }
367 
368 static struct isa_driver stx104_driver = {
369 	.probe = stx104_probe,
370 	.driver = {
371 		.name = "stx104"
372 	},
373 	.remove = stx104_remove
374 };
375 
376 module_isa_driver(stx104_driver, num_stx104);
377 
378 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
379 MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver");
380 MODULE_LICENSE("GPL v2");
381