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 * The highest base address possible for an ISA device is 0x3FF; this results in
38 * 1024 possible base addresses. Dividing the number of possible base addresses
39 * by the address extent taken by each device results in the maximum number of
40 * devices on a system.
42 #define MAX_NUM_STX104 (1024 / STX104_EXTENT)
44 static unsigned base
[MAX_NUM_STX104
];
45 static unsigned num_stx104
;
46 module_param_array(base
, uint
, &num_stx104
, 0);
47 MODULE_PARM_DESC(base
, "Apex Embedded Systems STX104 base addresses");
50 * struct stx104_iio - IIO device private data structure
51 * @chan_out_states: channels' output states
52 * @base: base port address of the IIO device
55 unsigned chan_out_states
[STX104_NUM_CHAN
];
59 static int stx104_read_raw(struct iio_dev
*indio_dev
,
60 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
62 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
64 if (mask
!= IIO_CHAN_INFO_RAW
)
67 *val
= priv
->chan_out_states
[chan
->channel
];
72 static int stx104_write_raw(struct iio_dev
*indio_dev
,
73 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
75 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
76 const unsigned chan_addr_offset
= 2 * chan
->channel
;
78 if (mask
!= IIO_CHAN_INFO_RAW
)
81 priv
->chan_out_states
[chan
->channel
] = val
;
82 outw(val
, priv
->base
+ 4 + chan_addr_offset
);
87 static const struct iio_info stx104_info
= {
88 .driver_module
= THIS_MODULE
,
89 .read_raw
= stx104_read_raw
,
90 .write_raw
= stx104_write_raw
93 static const struct iio_chan_spec stx104_channels
[STX104_NUM_CHAN
] = {
98 static int stx104_probe(struct device
*dev
, unsigned int id
)
100 struct iio_dev
*indio_dev
;
101 struct stx104_iio
*priv
;
103 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
107 if (!devm_request_region(dev
, base
[id
], STX104_EXTENT
,
109 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
110 base
[id
], base
[id
] + STX104_EXTENT
);
114 indio_dev
->info
= &stx104_info
;
115 indio_dev
->modes
= INDIO_DIRECT_MODE
;
116 indio_dev
->channels
= stx104_channels
;
117 indio_dev
->num_channels
= STX104_NUM_CHAN
;
118 indio_dev
->name
= dev_name(dev
);
120 priv
= iio_priv(indio_dev
);
121 priv
->base
= base
[id
];
123 /* initialize DAC output to 0V */
124 outw(0, base
[id
] + 4);
125 outw(0, base
[id
] + 6);
127 return devm_iio_device_register(dev
, indio_dev
);
130 static struct isa_driver stx104_driver
= {
131 .probe
= stx104_probe
,
137 static void __exit
stx104_exit(void)
139 isa_unregister_driver(&stx104_driver
);
142 static int __init
stx104_init(void)
144 return isa_register_driver(&stx104_driver
, num_stx104
);
147 module_init(stx104_init
);
148 module_exit(stx104_exit
);
150 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
151 MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver");
152 MODULE_LICENSE("GPL v2");