William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * DAC 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> |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 17 | #include <linux/gpio/driver.h> |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 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/module.h> |
| 24 | #include <linux/moduleparam.h> |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 26 | |
| 27 | #define STX104_NUM_CHAN 2 |
| 28 | |
| 29 | #define STX104_CHAN(chan) { \ |
| 30 | .type = IIO_VOLTAGE, \ |
| 31 | .channel = chan, \ |
| 32 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 33 | .indexed = 1, \ |
| 34 | .output = 1 \ |
| 35 | } |
| 36 | |
| 37 | #define STX104_EXTENT 16 |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 38 | |
William Breathitt Gray | 9c2cfc1 | 2016-05-01 18:44:11 -0400 | [diff] [blame] | 39 | static unsigned int base[max_num_isa_dev(STX104_EXTENT)]; |
| 40 | static unsigned int num_stx104; |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 41 | module_param_array(base, uint, &num_stx104, 0); |
| 42 | MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses"); |
| 43 | |
| 44 | /** |
| 45 | * struct stx104_iio - IIO device private data structure |
| 46 | * @chan_out_states: channels' output states |
| 47 | * @base: base port address of the IIO device |
| 48 | */ |
| 49 | struct stx104_iio { |
| 50 | unsigned chan_out_states[STX104_NUM_CHAN]; |
| 51 | unsigned base; |
| 52 | }; |
| 53 | |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 54 | /** |
| 55 | * struct stx104_gpio - GPIO device private data structure |
| 56 | * @chip: instance of the gpio_chip |
| 57 | * @lock: synchronization lock to prevent I/O race conditions |
| 58 | * @base: base port address of the GPIO device |
| 59 | * @out_state: output bits state |
| 60 | */ |
| 61 | struct stx104_gpio { |
| 62 | struct gpio_chip chip; |
| 63 | spinlock_t lock; |
| 64 | unsigned int base; |
| 65 | unsigned int out_state; |
| 66 | }; |
| 67 | |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 68 | /** |
| 69 | * struct stx104_dev - STX104 device private data structure |
| 70 | * @indio_dev: IIO device |
| 71 | * @chip: instance of the gpio_chip |
| 72 | */ |
| 73 | struct stx104_dev { |
| 74 | struct iio_dev *indio_dev; |
| 75 | struct gpio_chip *chip; |
| 76 | }; |
| 77 | |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 78 | static int stx104_read_raw(struct iio_dev *indio_dev, |
| 79 | struct iio_chan_spec const *chan, int *val, int *val2, long mask) |
| 80 | { |
| 81 | struct stx104_iio *const priv = iio_priv(indio_dev); |
| 82 | |
| 83 | if (mask != IIO_CHAN_INFO_RAW) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | *val = priv->chan_out_states[chan->channel]; |
| 87 | |
| 88 | return IIO_VAL_INT; |
| 89 | } |
| 90 | |
| 91 | static int stx104_write_raw(struct iio_dev *indio_dev, |
| 92 | struct iio_chan_spec const *chan, int val, int val2, long mask) |
| 93 | { |
| 94 | struct stx104_iio *const priv = iio_priv(indio_dev); |
| 95 | const unsigned chan_addr_offset = 2 * chan->channel; |
| 96 | |
| 97 | if (mask != IIO_CHAN_INFO_RAW) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | priv->chan_out_states[chan->channel] = val; |
| 101 | outw(val, priv->base + 4 + chan_addr_offset); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static const struct iio_info stx104_info = { |
| 107 | .driver_module = THIS_MODULE, |
| 108 | .read_raw = stx104_read_raw, |
| 109 | .write_raw = stx104_write_raw |
| 110 | }; |
| 111 | |
| 112 | static const struct iio_chan_spec stx104_channels[STX104_NUM_CHAN] = { |
| 113 | STX104_CHAN(0), |
| 114 | STX104_CHAN(1) |
| 115 | }; |
| 116 | |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 117 | static int stx104_gpio_get_direction(struct gpio_chip *chip, |
| 118 | unsigned int offset) |
| 119 | { |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 120 | /* GPIO 0-3 are input only, while the rest are output only */ |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 121 | if (offset < 4) |
| 122 | return 1; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int stx104_gpio_direction_input(struct gpio_chip *chip, |
| 128 | unsigned int offset) |
| 129 | { |
| 130 | if (offset >= 4) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int stx104_gpio_direction_output(struct gpio_chip *chip, |
| 137 | unsigned int offset, int value) |
| 138 | { |
| 139 | if (offset < 4) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | chip->set(chip, offset, value); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset) |
| 147 | { |
| 148 | struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip); |
| 149 | |
| 150 | if (offset >= 4) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | return !!(inb(stx104gpio->base) & BIT(offset)); |
| 154 | } |
| 155 | |
| 156 | static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 157 | int value) |
| 158 | { |
| 159 | struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip); |
| 160 | const unsigned int mask = BIT(offset) >> 4; |
| 161 | unsigned long flags; |
| 162 | |
| 163 | if (offset < 4) |
| 164 | return; |
| 165 | |
| 166 | spin_lock_irqsave(&stx104gpio->lock, flags); |
| 167 | |
| 168 | if (value) |
| 169 | stx104gpio->out_state |= mask; |
| 170 | else |
| 171 | stx104gpio->out_state &= ~mask; |
| 172 | |
| 173 | outb(stx104gpio->out_state, stx104gpio->base); |
| 174 | |
| 175 | spin_unlock_irqrestore(&stx104gpio->lock, flags); |
| 176 | } |
| 177 | |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 178 | static int stx104_probe(struct device *dev, unsigned int id) |
| 179 | { |
| 180 | struct iio_dev *indio_dev; |
| 181 | struct stx104_iio *priv; |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 182 | struct stx104_gpio *stx104gpio; |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 183 | struct stx104_dev *stx104dev; |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 184 | int err; |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 185 | |
| 186 | indio_dev = devm_iio_device_alloc(dev, sizeof(*priv)); |
| 187 | if (!indio_dev) |
| 188 | return -ENOMEM; |
| 189 | |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 190 | stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL); |
| 191 | if (!stx104gpio) |
| 192 | return -ENOMEM; |
| 193 | |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 194 | stx104dev = devm_kzalloc(dev, sizeof(*stx104dev), GFP_KERNEL); |
| 195 | if (!stx104dev) |
| 196 | return -ENOMEM; |
| 197 | |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 198 | if (!devm_request_region(dev, base[id], STX104_EXTENT, |
| 199 | dev_name(dev))) { |
| 200 | dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n", |
| 201 | base[id], base[id] + STX104_EXTENT); |
| 202 | return -EBUSY; |
| 203 | } |
| 204 | |
| 205 | indio_dev->info = &stx104_info; |
| 206 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 207 | indio_dev->channels = stx104_channels; |
| 208 | indio_dev->num_channels = STX104_NUM_CHAN; |
| 209 | indio_dev->name = dev_name(dev); |
| 210 | |
| 211 | priv = iio_priv(indio_dev); |
| 212 | priv->base = base[id]; |
| 213 | |
| 214 | /* initialize DAC output to 0V */ |
| 215 | outw(0, base[id] + 4); |
| 216 | outw(0, base[id] + 6); |
| 217 | |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 218 | stx104gpio->chip.label = dev_name(dev); |
| 219 | stx104gpio->chip.parent = dev; |
| 220 | stx104gpio->chip.owner = THIS_MODULE; |
| 221 | stx104gpio->chip.base = -1; |
| 222 | stx104gpio->chip.ngpio = 8; |
| 223 | stx104gpio->chip.get_direction = stx104_gpio_get_direction; |
| 224 | stx104gpio->chip.direction_input = stx104_gpio_direction_input; |
| 225 | stx104gpio->chip.direction_output = stx104_gpio_direction_output; |
| 226 | stx104gpio->chip.get = stx104_gpio_get; |
| 227 | stx104gpio->chip.set = stx104_gpio_set; |
| 228 | stx104gpio->base = base[id] + 3; |
| 229 | stx104gpio->out_state = 0x0; |
| 230 | |
| 231 | spin_lock_init(&stx104gpio->lock); |
| 232 | |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 233 | stx104dev->indio_dev = indio_dev; |
| 234 | stx104dev->chip = &stx104gpio->chip; |
| 235 | dev_set_drvdata(dev, stx104dev); |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 236 | |
| 237 | err = gpiochip_add_data(&stx104gpio->chip, stx104gpio); |
| 238 | if (err) { |
| 239 | dev_err(dev, "GPIO registering failed (%d)\n", err); |
| 240 | return err; |
| 241 | } |
| 242 | |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 243 | err = iio_device_register(indio_dev); |
| 244 | if (err) { |
| 245 | dev_err(dev, "IIO device registering failed (%d)\n", err); |
| 246 | gpiochip_remove(&stx104gpio->chip); |
| 247 | return err; |
| 248 | } |
| 249 | |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int stx104_remove(struct device *dev, unsigned int id) |
| 254 | { |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 255 | struct stx104_dev *const stx104dev = dev_get_drvdata(dev); |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 256 | |
William Breathitt Gray | 45e9815 | 2016-07-19 12:25:00 -0400 | [diff] [blame] | 257 | iio_device_unregister(stx104dev->indio_dev); |
| 258 | gpiochip_remove(stx104dev->chip); |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 259 | |
| 260 | return 0; |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static struct isa_driver stx104_driver = { |
| 264 | .probe = stx104_probe, |
| 265 | .driver = { |
| 266 | .name = "stx104" |
William Breathitt Gray | 765550e | 2016-06-13 09:06:48 -0400 | [diff] [blame] | 267 | }, |
| 268 | .remove = stx104_remove |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 269 | }; |
| 270 | |
William Breathitt Gray | 9c2cfc1 | 2016-05-01 18:44:11 -0400 | [diff] [blame] | 271 | module_isa_driver(stx104_driver, num_stx104); |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 272 | |
| 273 | MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); |
| 274 | MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver"); |
| 275 | MODULE_LICENSE("GPL v2"); |