2 * DAC 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/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/spinlock.h>
27 #define STX104_NUM_CHAN 2
29 #define STX104_CHAN(chan) { \
30 .type = IIO_VOLTAGE, \
32 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
37 #define STX104_EXTENT 16
39 * The highest base address possible for an ISA device is 0x3FF; this results in
40 * 1024 possible base addresses. Dividing the number of possible base addresses
41 * by the address extent taken by each device results in the maximum number of
42 * devices on a system.
44 #define MAX_NUM_STX104 (1024 / STX104_EXTENT)
46 static unsigned base
[MAX_NUM_STX104
];
47 static unsigned num_stx104
;
48 module_param_array(base
, uint
, &num_stx104
, 0);
49 MODULE_PARM_DESC(base
, "Apex Embedded Systems STX104 base addresses");
52 * struct stx104_iio - IIO device private data structure
53 * @chan_out_states: channels' output states
54 * @base: base port address of the IIO device
57 unsigned chan_out_states
[STX104_NUM_CHAN
];
62 * struct stx104_gpio - GPIO device private data structure
63 * @chip: instance of the gpio_chip
64 * @lock: synchronization lock to prevent I/O race conditions
65 * @base: base port address of the GPIO device
66 * @out_state: output bits state
69 struct gpio_chip chip
;
72 unsigned int out_state
;
75 static int stx104_read_raw(struct iio_dev
*indio_dev
,
76 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
78 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
80 if (mask
!= IIO_CHAN_INFO_RAW
)
83 *val
= priv
->chan_out_states
[chan
->channel
];
88 static int stx104_write_raw(struct iio_dev
*indio_dev
,
89 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
91 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
92 const unsigned chan_addr_offset
= 2 * chan
->channel
;
94 if (mask
!= IIO_CHAN_INFO_RAW
)
97 priv
->chan_out_states
[chan
->channel
] = val
;
98 outw(val
, priv
->base
+ 4 + chan_addr_offset
);
103 static const struct iio_info stx104_info
= {
104 .driver_module
= THIS_MODULE
,
105 .read_raw
= stx104_read_raw
,
106 .write_raw
= stx104_write_raw
109 static const struct iio_chan_spec stx104_channels
[STX104_NUM_CHAN
] = {
114 static int stx104_gpio_get_direction(struct gpio_chip
*chip
,
123 static int stx104_gpio_direction_input(struct gpio_chip
*chip
,
132 static int stx104_gpio_direction_output(struct gpio_chip
*chip
,
133 unsigned int offset
, int value
)
138 chip
->set(chip
, offset
, value
);
142 static int stx104_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
144 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
149 return !!(inb(stx104gpio
->base
) & BIT(offset
));
152 static void stx104_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
155 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
156 const unsigned int mask
= BIT(offset
) >> 4;
162 spin_lock_irqsave(&stx104gpio
->lock
, flags
);
165 stx104gpio
->out_state
|= mask
;
167 stx104gpio
->out_state
&= ~mask
;
169 outb(stx104gpio
->out_state
, stx104gpio
->base
);
171 spin_unlock_irqrestore(&stx104gpio
->lock
, flags
);
174 static int stx104_probe(struct device
*dev
, unsigned int id
)
176 struct iio_dev
*indio_dev
;
177 struct stx104_iio
*priv
;
178 struct stx104_gpio
*stx104gpio
;
181 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
185 stx104gpio
= devm_kzalloc(dev
, sizeof(*stx104gpio
), GFP_KERNEL
);
189 if (!devm_request_region(dev
, base
[id
], STX104_EXTENT
,
191 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
192 base
[id
], base
[id
] + STX104_EXTENT
);
196 indio_dev
->info
= &stx104_info
;
197 indio_dev
->modes
= INDIO_DIRECT_MODE
;
198 indio_dev
->channels
= stx104_channels
;
199 indio_dev
->num_channels
= STX104_NUM_CHAN
;
200 indio_dev
->name
= dev_name(dev
);
202 priv
= iio_priv(indio_dev
);
203 priv
->base
= base
[id
];
205 /* initialize DAC output to 0V */
206 outw(0, base
[id
] + 4);
207 outw(0, base
[id
] + 6);
209 err
= devm_iio_device_register(dev
, indio_dev
);
211 dev_err(dev
, "IIO device registering failed (%d)\n", err
);
215 stx104gpio
->chip
.label
= dev_name(dev
);
216 stx104gpio
->chip
.parent
= dev
;
217 stx104gpio
->chip
.owner
= THIS_MODULE
;
218 stx104gpio
->chip
.base
= -1;
219 stx104gpio
->chip
.ngpio
= 8;
220 stx104gpio
->chip
.get_direction
= stx104_gpio_get_direction
;
221 stx104gpio
->chip
.direction_input
= stx104_gpio_direction_input
;
222 stx104gpio
->chip
.direction_output
= stx104_gpio_direction_output
;
223 stx104gpio
->chip
.get
= stx104_gpio_get
;
224 stx104gpio
->chip
.set
= stx104_gpio_set
;
225 stx104gpio
->base
= base
[id
] + 3;
226 stx104gpio
->out_state
= 0x0;
228 spin_lock_init(&stx104gpio
->lock
);
230 dev_set_drvdata(dev
, stx104gpio
);
232 err
= gpiochip_add_data(&stx104gpio
->chip
, stx104gpio
);
234 dev_err(dev
, "GPIO registering failed (%d)\n", err
);
241 static int stx104_remove(struct device
*dev
, unsigned int id
)
243 struct stx104_gpio
*const stx104gpio
= dev_get_drvdata(dev
);
245 gpiochip_remove(&stx104gpio
->chip
);
250 static struct isa_driver stx104_driver
= {
251 .probe
= stx104_probe
,
255 .remove
= stx104_remove
258 static void __exit
stx104_exit(void)
260 isa_unregister_driver(&stx104_driver
);
263 static int __init
stx104_init(void)
265 return isa_register_driver(&stx104_driver
, num_stx104
);
268 module_init(stx104_init
);
269 module_exit(stx104_exit
);
271 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
272 MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
273 MODULE_LICENSE("GPL v2");