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 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");
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
50 unsigned chan_out_states
[STX104_NUM_CHAN
];
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
62 struct gpio_chip chip
;
65 unsigned int out_state
;
69 * struct stx104_dev - STX104 device private data structure
70 * @indio_dev: IIO device
71 * @chip: instance of the gpio_chip
74 struct iio_dev
*indio_dev
;
75 struct gpio_chip
*chip
;
78 static int stx104_read_raw(struct iio_dev
*indio_dev
,
79 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
81 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
83 if (mask
!= IIO_CHAN_INFO_RAW
)
86 *val
= priv
->chan_out_states
[chan
->channel
];
91 static int stx104_write_raw(struct iio_dev
*indio_dev
,
92 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
94 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
95 const unsigned chan_addr_offset
= 2 * chan
->channel
;
97 if (mask
!= IIO_CHAN_INFO_RAW
)
100 priv
->chan_out_states
[chan
->channel
] = val
;
101 outw(val
, priv
->base
+ 4 + chan_addr_offset
);
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
112 static const struct iio_chan_spec stx104_channels
[STX104_NUM_CHAN
] = {
117 static int stx104_gpio_get_direction(struct gpio_chip
*chip
,
120 /* GPIO 0-3 are input only, while the rest are output only */
127 static int stx104_gpio_direction_input(struct gpio_chip
*chip
,
136 static int stx104_gpio_direction_output(struct gpio_chip
*chip
,
137 unsigned int offset
, int value
)
142 chip
->set(chip
, offset
, value
);
146 static int stx104_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
148 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
153 return !!(inb(stx104gpio
->base
) & BIT(offset
));
156 static void stx104_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
159 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
160 const unsigned int mask
= BIT(offset
) >> 4;
166 spin_lock_irqsave(&stx104gpio
->lock
, flags
);
169 stx104gpio
->out_state
|= mask
;
171 stx104gpio
->out_state
&= ~mask
;
173 outb(stx104gpio
->out_state
, stx104gpio
->base
);
175 spin_unlock_irqrestore(&stx104gpio
->lock
, flags
);
178 static int stx104_probe(struct device
*dev
, unsigned int id
)
180 struct iio_dev
*indio_dev
;
181 struct stx104_iio
*priv
;
182 struct stx104_gpio
*stx104gpio
;
183 struct stx104_dev
*stx104dev
;
186 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
190 stx104gpio
= devm_kzalloc(dev
, sizeof(*stx104gpio
), GFP_KERNEL
);
194 stx104dev
= devm_kzalloc(dev
, sizeof(*stx104dev
), GFP_KERNEL
);
198 if (!devm_request_region(dev
, base
[id
], STX104_EXTENT
,
200 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
201 base
[id
], base
[id
] + STX104_EXTENT
);
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
);
211 priv
= iio_priv(indio_dev
);
212 priv
->base
= base
[id
];
214 /* initialize DAC output to 0V */
215 outw(0, base
[id
] + 4);
216 outw(0, base
[id
] + 6);
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;
231 spin_lock_init(&stx104gpio
->lock
);
233 stx104dev
->indio_dev
= indio_dev
;
234 stx104dev
->chip
= &stx104gpio
->chip
;
235 dev_set_drvdata(dev
, stx104dev
);
237 err
= gpiochip_add_data(&stx104gpio
->chip
, stx104gpio
);
239 dev_err(dev
, "GPIO registering failed (%d)\n", err
);
243 err
= iio_device_register(indio_dev
);
245 dev_err(dev
, "IIO device registering failed (%d)\n", err
);
246 gpiochip_remove(&stx104gpio
->chip
);
253 static int stx104_remove(struct device
*dev
, unsigned int id
)
255 struct stx104_dev
*const stx104dev
= dev_get_drvdata(dev
);
257 iio_device_unregister(stx104dev
->indio_dev
);
258 gpiochip_remove(stx104dev
->chip
);
263 static struct isa_driver stx104_driver
= {
264 .probe
= stx104_probe
,
268 .remove
= stx104_remove
271 module_isa_driver(stx104_driver
, num_stx104
);
273 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
274 MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
275 MODULE_LICENSE("GPL v2");