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
;
68 static int stx104_read_raw(struct iio_dev
*indio_dev
,
69 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
71 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
73 if (mask
!= IIO_CHAN_INFO_RAW
)
76 *val
= priv
->chan_out_states
[chan
->channel
];
81 static int stx104_write_raw(struct iio_dev
*indio_dev
,
82 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
84 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
85 const unsigned chan_addr_offset
= 2 * chan
->channel
;
87 if (mask
!= IIO_CHAN_INFO_RAW
)
90 priv
->chan_out_states
[chan
->channel
] = val
;
91 outw(val
, priv
->base
+ 4 + chan_addr_offset
);
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
102 static const struct iio_chan_spec stx104_channels
[STX104_NUM_CHAN
] = {
107 static int stx104_gpio_get_direction(struct gpio_chip
*chip
,
116 static int stx104_gpio_direction_input(struct gpio_chip
*chip
,
125 static int stx104_gpio_direction_output(struct gpio_chip
*chip
,
126 unsigned int offset
, int value
)
131 chip
->set(chip
, offset
, value
);
135 static int stx104_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
137 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
142 return !!(inb(stx104gpio
->base
) & BIT(offset
));
145 static void stx104_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
148 struct stx104_gpio
*const stx104gpio
= gpiochip_get_data(chip
);
149 const unsigned int mask
= BIT(offset
) >> 4;
155 spin_lock_irqsave(&stx104gpio
->lock
, flags
);
158 stx104gpio
->out_state
|= mask
;
160 stx104gpio
->out_state
&= ~mask
;
162 outb(stx104gpio
->out_state
, stx104gpio
->base
);
164 spin_unlock_irqrestore(&stx104gpio
->lock
, flags
);
167 static int stx104_probe(struct device
*dev
, unsigned int id
)
169 struct iio_dev
*indio_dev
;
170 struct stx104_iio
*priv
;
171 struct stx104_gpio
*stx104gpio
;
174 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
178 stx104gpio
= devm_kzalloc(dev
, sizeof(*stx104gpio
), GFP_KERNEL
);
182 if (!devm_request_region(dev
, base
[id
], STX104_EXTENT
,
184 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
185 base
[id
], base
[id
] + STX104_EXTENT
);
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
);
195 priv
= iio_priv(indio_dev
);
196 priv
->base
= base
[id
];
198 /* initialize DAC output to 0V */
199 outw(0, base
[id
] + 4);
200 outw(0, base
[id
] + 6);
202 err
= devm_iio_device_register(dev
, indio_dev
);
204 dev_err(dev
, "IIO device registering failed (%d)\n", err
);
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;
221 spin_lock_init(&stx104gpio
->lock
);
223 dev_set_drvdata(dev
, stx104gpio
);
225 err
= gpiochip_add_data(&stx104gpio
->chip
, stx104gpio
);
227 dev_err(dev
, "GPIO registering failed (%d)\n", err
);
234 static int stx104_remove(struct device
*dev
, unsigned int id
)
236 struct stx104_gpio
*const stx104gpio
= dev_get_drvdata(dev
);
238 gpiochip_remove(&stx104gpio
->chip
);
243 static struct isa_driver stx104_driver
= {
244 .probe
= stx104_probe
,
248 .remove
= stx104_remove
251 module_isa_driver(stx104_driver
, num_stx104
);
253 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
254 MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
255 MODULE_LICENSE("GPL v2");