iio: ad5755: Add DT binding documentation
[cascardo/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  * 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.
43  */
44 #define MAX_NUM_STX104 (1024 / STX104_EXTENT)
45
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");
50
51 /**
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
55  */
56 struct stx104_iio {
57         unsigned chan_out_states[STX104_NUM_CHAN];
58         unsigned base;
59 };
60
61 /**
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
67  */
68 struct stx104_gpio {
69         struct gpio_chip chip;
70         spinlock_t lock;
71         unsigned int base;
72         unsigned int out_state;
73 };
74
75 static int stx104_read_raw(struct iio_dev *indio_dev,
76         struct iio_chan_spec const *chan, int *val, int *val2, long mask)
77 {
78         struct stx104_iio *const priv = iio_priv(indio_dev);
79
80         if (mask != IIO_CHAN_INFO_RAW)
81                 return -EINVAL;
82
83         *val = priv->chan_out_states[chan->channel];
84
85         return IIO_VAL_INT;
86 }
87
88 static int stx104_write_raw(struct iio_dev *indio_dev,
89         struct iio_chan_spec const *chan, int val, int val2, long mask)
90 {
91         struct stx104_iio *const priv = iio_priv(indio_dev);
92         const unsigned chan_addr_offset = 2 * chan->channel;
93
94         if (mask != IIO_CHAN_INFO_RAW)
95                 return -EINVAL;
96
97         priv->chan_out_states[chan->channel] = val;
98         outw(val, priv->base + 4 + chan_addr_offset);
99
100         return 0;
101 }
102
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
107 };
108
109 static const struct iio_chan_spec stx104_channels[STX104_NUM_CHAN] = {
110         STX104_CHAN(0),
111         STX104_CHAN(1)
112 };
113
114 static int stx104_gpio_get_direction(struct gpio_chip *chip,
115         unsigned int offset)
116 {
117         if (offset < 4)
118                 return 1;
119
120         return 0;
121 }
122
123 static int stx104_gpio_direction_input(struct gpio_chip *chip,
124         unsigned int offset)
125 {
126         if (offset >= 4)
127                 return -EINVAL;
128
129         return 0;
130 }
131
132 static int stx104_gpio_direction_output(struct gpio_chip *chip,
133         unsigned int offset, int value)
134 {
135         if (offset < 4)
136                 return -EINVAL;
137
138         chip->set(chip, offset, value);
139         return 0;
140 }
141
142 static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset)
143 {
144         struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
145
146         if (offset >= 4)
147                 return -EINVAL;
148
149         return !!(inb(stx104gpio->base) & BIT(offset));
150 }
151
152 static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset,
153         int value)
154 {
155         struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
156         const unsigned int mask = BIT(offset) >> 4;
157         unsigned long flags;
158
159         if (offset < 4)
160                 return;
161
162         spin_lock_irqsave(&stx104gpio->lock, flags);
163
164         if (value)
165                 stx104gpio->out_state |= mask;
166         else
167                 stx104gpio->out_state &= ~mask;
168
169         outb(stx104gpio->out_state, stx104gpio->base);
170
171         spin_unlock_irqrestore(&stx104gpio->lock, flags);
172 }
173
174 static int stx104_probe(struct device *dev, unsigned int id)
175 {
176         struct iio_dev *indio_dev;
177         struct stx104_iio *priv;
178         struct stx104_gpio *stx104gpio;
179         int err;
180
181         indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
182         if (!indio_dev)
183                 return -ENOMEM;
184
185         stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL);
186         if (!stx104gpio)
187                 return -ENOMEM;
188
189         if (!devm_request_region(dev, base[id], STX104_EXTENT,
190                 dev_name(dev))) {
191                 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
192                         base[id], base[id] + STX104_EXTENT);
193                 return -EBUSY;
194         }
195
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);
201
202         priv = iio_priv(indio_dev);
203         priv->base = base[id];
204
205         /* initialize DAC output to 0V */
206         outw(0, base[id] + 4);
207         outw(0, base[id] + 6);
208
209         err = devm_iio_device_register(dev, indio_dev);
210         if (err) {
211                 dev_err(dev, "IIO device registering failed (%d)\n", err);
212                 return err;
213         }
214
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;
227
228         spin_lock_init(&stx104gpio->lock);
229
230         dev_set_drvdata(dev, stx104gpio);
231
232         err = gpiochip_add_data(&stx104gpio->chip, stx104gpio);
233         if (err) {
234                 dev_err(dev, "GPIO registering failed (%d)\n", err);
235                 return err;
236         }
237
238         return 0;
239 }
240
241 static int stx104_remove(struct device *dev, unsigned int id)
242 {
243         struct stx104_gpio *const stx104gpio = dev_get_drvdata(dev);
244
245         gpiochip_remove(&stx104gpio->chip);
246
247         return 0;
248 }
249
250 static struct isa_driver stx104_driver = {
251         .probe = stx104_probe,
252         .driver = {
253                 .name = "stx104"
254         },
255         .remove = stx104_remove
256 };
257
258 static void __exit stx104_exit(void)
259 {
260         isa_unregister_driver(&stx104_driver);
261 }
262
263 static int __init stx104_init(void)
264 {
265         return isa_register_driver(&stx104_driver, num_stx104);
266 }
267
268 module_init(stx104_init);
269 module_exit(stx104_exit);
270
271 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
272 MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
273 MODULE_LICENSE("GPL v2");