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> |
| 17 | #include <linux/iio/iio.h> |
| 18 | #include <linux/iio/types.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/ioport.h> |
| 21 | #include <linux/isa.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
| 24 | |
| 25 | #define STX104_NUM_CHAN 2 |
| 26 | |
| 27 | #define STX104_CHAN(chan) { \ |
| 28 | .type = IIO_VOLTAGE, \ |
| 29 | .channel = chan, \ |
| 30 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 31 | .indexed = 1, \ |
| 32 | .output = 1 \ |
| 33 | } |
| 34 | |
| 35 | #define STX104_EXTENT 16 |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 36 | |
William Breathitt Gray | 9c2cfc1 | 2016-05-01 18:44:11 -0400 | [diff] [blame^] | 37 | static unsigned int base[max_num_isa_dev(STX104_EXTENT)]; |
| 38 | static unsigned int num_stx104; |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 39 | module_param_array(base, uint, &num_stx104, 0); |
| 40 | MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses"); |
| 41 | |
| 42 | /** |
| 43 | * struct stx104_iio - IIO device private data structure |
| 44 | * @chan_out_states: channels' output states |
| 45 | * @base: base port address of the IIO device |
| 46 | */ |
| 47 | struct stx104_iio { |
| 48 | unsigned chan_out_states[STX104_NUM_CHAN]; |
| 49 | unsigned base; |
| 50 | }; |
| 51 | |
| 52 | static int stx104_read_raw(struct iio_dev *indio_dev, |
| 53 | struct iio_chan_spec const *chan, int *val, int *val2, long mask) |
| 54 | { |
| 55 | struct stx104_iio *const priv = iio_priv(indio_dev); |
| 56 | |
| 57 | if (mask != IIO_CHAN_INFO_RAW) |
| 58 | return -EINVAL; |
| 59 | |
| 60 | *val = priv->chan_out_states[chan->channel]; |
| 61 | |
| 62 | return IIO_VAL_INT; |
| 63 | } |
| 64 | |
| 65 | static int stx104_write_raw(struct iio_dev *indio_dev, |
| 66 | struct iio_chan_spec const *chan, int val, int val2, long mask) |
| 67 | { |
| 68 | struct stx104_iio *const priv = iio_priv(indio_dev); |
| 69 | const unsigned chan_addr_offset = 2 * chan->channel; |
| 70 | |
| 71 | if (mask != IIO_CHAN_INFO_RAW) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | priv->chan_out_states[chan->channel] = val; |
| 75 | outw(val, priv->base + 4 + chan_addr_offset); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static const struct iio_info stx104_info = { |
| 81 | .driver_module = THIS_MODULE, |
| 82 | .read_raw = stx104_read_raw, |
| 83 | .write_raw = stx104_write_raw |
| 84 | }; |
| 85 | |
| 86 | static const struct iio_chan_spec stx104_channels[STX104_NUM_CHAN] = { |
| 87 | STX104_CHAN(0), |
| 88 | STX104_CHAN(1) |
| 89 | }; |
| 90 | |
| 91 | static int stx104_probe(struct device *dev, unsigned int id) |
| 92 | { |
| 93 | struct iio_dev *indio_dev; |
| 94 | struct stx104_iio *priv; |
| 95 | |
| 96 | indio_dev = devm_iio_device_alloc(dev, sizeof(*priv)); |
| 97 | if (!indio_dev) |
| 98 | return -ENOMEM; |
| 99 | |
| 100 | if (!devm_request_region(dev, base[id], STX104_EXTENT, |
| 101 | dev_name(dev))) { |
| 102 | dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n", |
| 103 | base[id], base[id] + STX104_EXTENT); |
| 104 | return -EBUSY; |
| 105 | } |
| 106 | |
| 107 | indio_dev->info = &stx104_info; |
| 108 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 109 | indio_dev->channels = stx104_channels; |
| 110 | indio_dev->num_channels = STX104_NUM_CHAN; |
| 111 | indio_dev->name = dev_name(dev); |
| 112 | |
| 113 | priv = iio_priv(indio_dev); |
| 114 | priv->base = base[id]; |
| 115 | |
| 116 | /* initialize DAC output to 0V */ |
| 117 | outw(0, base[id] + 4); |
| 118 | outw(0, base[id] + 6); |
| 119 | |
| 120 | return devm_iio_device_register(dev, indio_dev); |
| 121 | } |
| 122 | |
| 123 | static struct isa_driver stx104_driver = { |
| 124 | .probe = stx104_probe, |
| 125 | .driver = { |
| 126 | .name = "stx104" |
| 127 | } |
| 128 | }; |
| 129 | |
William Breathitt Gray | 9c2cfc1 | 2016-05-01 18:44:11 -0400 | [diff] [blame^] | 130 | module_isa_driver(stx104_driver, num_stx104); |
William Breathitt Gray | 97a445d | 2016-02-11 11:49:36 -0500 | [diff] [blame] | 131 | |
| 132 | MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); |
| 133 | MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver"); |
| 134 | MODULE_LICENSE("GPL v2"); |