]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/iio/dac/stx104.c
Merge branch 'for-4.8/core' of git://git.kernel.dk/linux-block
[karo-tx-linux.git] / drivers / iio / dac / stx104.c
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/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/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/spinlock.h>
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
38
39 static unsigned int base[max_num_isa_dev(STX104_EXTENT)];
40 static unsigned int num_stx104;
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
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
68 static int stx104_read_raw(struct iio_dev *indio_dev,
69         struct iio_chan_spec const *chan, int *val, int *val2, long mask)
70 {
71         struct stx104_iio *const priv = iio_priv(indio_dev);
72
73         if (mask != IIO_CHAN_INFO_RAW)
74                 return -EINVAL;
75
76         *val = priv->chan_out_states[chan->channel];
77
78         return IIO_VAL_INT;
79 }
80
81 static int stx104_write_raw(struct iio_dev *indio_dev,
82         struct iio_chan_spec const *chan, int val, int val2, long mask)
83 {
84         struct stx104_iio *const priv = iio_priv(indio_dev);
85         const unsigned chan_addr_offset = 2 * chan->channel;
86
87         if (mask != IIO_CHAN_INFO_RAW)
88                 return -EINVAL;
89
90         priv->chan_out_states[chan->channel] = val;
91         outw(val, priv->base + 4 + chan_addr_offset);
92
93         return 0;
94 }
95
96 static const struct iio_info stx104_info = {
97         .driver_module = THIS_MODULE,
98         .read_raw = stx104_read_raw,
99         .write_raw = stx104_write_raw
100 };
101
102 static const struct iio_chan_spec stx104_channels[STX104_NUM_CHAN] = {
103         STX104_CHAN(0),
104         STX104_CHAN(1)
105 };
106
107 static int stx104_gpio_get_direction(struct gpio_chip *chip,
108         unsigned int offset)
109 {
110         if (offset < 4)
111                 return 1;
112
113         return 0;
114 }
115
116 static int stx104_gpio_direction_input(struct gpio_chip *chip,
117         unsigned int offset)
118 {
119         if (offset >= 4)
120                 return -EINVAL;
121
122         return 0;
123 }
124
125 static int stx104_gpio_direction_output(struct gpio_chip *chip,
126         unsigned int offset, int value)
127 {
128         if (offset < 4)
129                 return -EINVAL;
130
131         chip->set(chip, offset, value);
132         return 0;
133 }
134
135 static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset)
136 {
137         struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
138
139         if (offset >= 4)
140                 return -EINVAL;
141
142         return !!(inb(stx104gpio->base) & BIT(offset));
143 }
144
145 static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset,
146         int value)
147 {
148         struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
149         const unsigned int mask = BIT(offset) >> 4;
150         unsigned long flags;
151
152         if (offset < 4)
153                 return;
154
155         spin_lock_irqsave(&stx104gpio->lock, flags);
156
157         if (value)
158                 stx104gpio->out_state |= mask;
159         else
160                 stx104gpio->out_state &= ~mask;
161
162         outb(stx104gpio->out_state, stx104gpio->base);
163
164         spin_unlock_irqrestore(&stx104gpio->lock, flags);
165 }
166
167 static int stx104_probe(struct device *dev, unsigned int id)
168 {
169         struct iio_dev *indio_dev;
170         struct stx104_iio *priv;
171         struct stx104_gpio *stx104gpio;
172         int err;
173
174         indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
175         if (!indio_dev)
176                 return -ENOMEM;
177
178         stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL);
179         if (!stx104gpio)
180                 return -ENOMEM;
181
182         if (!devm_request_region(dev, base[id], STX104_EXTENT,
183                 dev_name(dev))) {
184                 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
185                         base[id], base[id] + STX104_EXTENT);
186                 return -EBUSY;
187         }
188
189         indio_dev->info = &stx104_info;
190         indio_dev->modes = INDIO_DIRECT_MODE;
191         indio_dev->channels = stx104_channels;
192         indio_dev->num_channels = STX104_NUM_CHAN;
193         indio_dev->name = dev_name(dev);
194
195         priv = iio_priv(indio_dev);
196         priv->base = base[id];
197
198         /* initialize DAC output to 0V */
199         outw(0, base[id] + 4);
200         outw(0, base[id] + 6);
201
202         err = devm_iio_device_register(dev, indio_dev);
203         if (err) {
204                 dev_err(dev, "IIO device registering failed (%d)\n", err);
205                 return err;
206         }
207
208         stx104gpio->chip.label = dev_name(dev);
209         stx104gpio->chip.parent = dev;
210         stx104gpio->chip.owner = THIS_MODULE;
211         stx104gpio->chip.base = -1;
212         stx104gpio->chip.ngpio = 8;
213         stx104gpio->chip.get_direction = stx104_gpio_get_direction;
214         stx104gpio->chip.direction_input = stx104_gpio_direction_input;
215         stx104gpio->chip.direction_output = stx104_gpio_direction_output;
216         stx104gpio->chip.get = stx104_gpio_get;
217         stx104gpio->chip.set = stx104_gpio_set;
218         stx104gpio->base = base[id] + 3;
219         stx104gpio->out_state = 0x0;
220
221         spin_lock_init(&stx104gpio->lock);
222
223         dev_set_drvdata(dev, stx104gpio);
224
225         err = gpiochip_add_data(&stx104gpio->chip, stx104gpio);
226         if (err) {
227                 dev_err(dev, "GPIO registering failed (%d)\n", err);
228                 return err;
229         }
230
231         return 0;
232 }
233
234 static int stx104_remove(struct device *dev, unsigned int id)
235 {
236         struct stx104_gpio *const stx104gpio = dev_get_drvdata(dev);
237
238         gpiochip_remove(&stx104gpio->chip);
239
240         return 0;
241 }
242
243 static struct isa_driver stx104_driver = {
244         .probe = stx104_probe,
245         .driver = {
246                 .name = "stx104"
247         },
248         .remove = stx104_remove
249 };
250
251 module_isa_driver(stx104_driver, num_stx104);
252
253 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
254 MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
255 MODULE_LICENSE("GPL v2");