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/iio/iio.h>
18 #include <linux/iio/types.h>
20 #include <linux/ioport.h>
21 #include <linux/isa.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
25 #define STX104_NUM_CHAN 2
27 #define STX104_CHAN(chan) { \
28 .type = IIO_VOLTAGE, \
30 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
35 #define STX104_EXTENT 16
37 static unsigned int base
[max_num_isa_dev(STX104_EXTENT
)];
38 static unsigned int num_stx104
;
39 module_param_array(base
, uint
, &num_stx104
, 0);
40 MODULE_PARM_DESC(base
, "Apex Embedded Systems STX104 base addresses");
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
48 unsigned chan_out_states
[STX104_NUM_CHAN
];
52 static int stx104_read_raw(struct iio_dev
*indio_dev
,
53 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
55 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
57 if (mask
!= IIO_CHAN_INFO_RAW
)
60 *val
= priv
->chan_out_states
[chan
->channel
];
65 static int stx104_write_raw(struct iio_dev
*indio_dev
,
66 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
68 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
69 const unsigned chan_addr_offset
= 2 * chan
->channel
;
71 if (mask
!= IIO_CHAN_INFO_RAW
)
74 priv
->chan_out_states
[chan
->channel
] = val
;
75 outw(val
, priv
->base
+ 4 + chan_addr_offset
);
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
86 static const struct iio_chan_spec stx104_channels
[STX104_NUM_CHAN
] = {
91 static int stx104_probe(struct device
*dev
, unsigned int id
)
93 struct iio_dev
*indio_dev
;
94 struct stx104_iio
*priv
;
96 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
100 if (!devm_request_region(dev
, base
[id
], STX104_EXTENT
,
102 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
103 base
[id
], base
[id
] + STX104_EXTENT
);
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
);
113 priv
= iio_priv(indio_dev
);
114 priv
->base
= base
[id
];
116 /* initialize DAC output to 0V */
117 outw(0, base
[id
] + 4);
118 outw(0, base
[id
] + 6);
120 return devm_iio_device_register(dev
, indio_dev
);
123 static struct isa_driver stx104_driver
= {
124 .probe
= stx104_probe
,
130 module_isa_driver(stx104_driver
, num_stx104
);
132 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
133 MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
134 MODULE_LICENSE("GPL v2");