1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO driver for the Apex Embedded Systems STX104
4 * Copyright (C) 2016 William Breathitt Gray
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
8 #include <linux/device.h>
10 #include <linux/gpio/regmap.h>
11 #include <linux/i8254.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/types.h>
14 #include <linux/isa.h>
15 #include <linux/kernel.h>
16 #include <linux/limits.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/mutex.h>
20 #include <linux/regmap.h>
21 #include <linux/types.h>
23 #define STX104_OUT_CHAN(chan) { \
24 .type = IIO_VOLTAGE, \
26 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
30 #define STX104_IN_CHAN(chan, diff) { \
31 .type = IIO_VOLTAGE, \
34 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
35 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE), \
36 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
38 .differential = diff \
41 #define STX104_NUM_OUT_CHAN 2
43 #define STX104_EXTENT 16
45 static unsigned int base
[max_num_isa_dev(STX104_EXTENT
)];
46 static unsigned int num_stx104
;
47 module_param_hw_array(base
, uint
, ioport
, &num_stx104
, 0);
48 MODULE_PARM_DESC(base
, "Apex Embedded Systems STX104 base addresses");
50 #define STX104_AIO_BASE 0x0
51 #define STX104_SOFTWARE_STROBE STX104_AIO_BASE
52 #define STX104_ADC_DATA STX104_AIO_BASE
53 #define STX104_ADC_CHANNEL (STX104_AIO_BASE + 0x2)
54 #define STX104_DIO_REG (STX104_AIO_BASE + 0x3)
55 #define STX104_DAC_BASE (STX104_AIO_BASE + 0x4)
56 #define STX104_ADC_STATUS (STX104_AIO_BASE + 0x8)
57 #define STX104_ADC_CONTROL (STX104_AIO_BASE + 0x9)
58 #define STX104_ADC_CONFIGURATION (STX104_AIO_BASE + 0x11)
59 #define STX104_I8254_BASE (STX104_AIO_BASE + 0x12)
61 #define STX104_AIO_DATA_STRIDE 2
62 #define STX104_DAC_OFFSET(_channel) (STX104_DAC_BASE + STX104_AIO_DATA_STRIDE * (_channel))
65 #define STX104_FC GENMASK(3, 0)
66 #define STX104_LC GENMASK(7, 4)
67 #define STX104_SINGLE_CHANNEL(_channel) \
68 (u8_encode_bits(_channel, STX104_FC) | u8_encode_bits(_channel, STX104_LC))
71 #define STX104_SD BIT(5)
72 #define STX104_CNV BIT(7)
73 #define STX104_DIFFERENTIAL 1
76 #define STX104_ALSS GENMASK(1, 0)
77 #define STX104_SOFTWARE_TRIGGER u8_encode_bits(0x0, STX104_ALSS)
79 /* ADC Configuration */
80 #define STX104_GAIN GENMASK(1, 0)
81 #define STX104_ADBU BIT(2)
82 #define STX104_RBK GENMASK(7, 4)
83 #define STX104_BIPOLAR 0
84 #define STX104_GAIN_X1 0
85 #define STX104_GAIN_X2 1
86 #define STX104_GAIN_X4 2
87 #define STX104_GAIN_X8 3
90 * struct stx104_iio - IIO device private data structure
91 * @lock: synchronization lock to prevent I/O race conditions
92 * @aio_data_map: Regmap for analog I/O data
93 * @aio_ctl_map: Regmap for analog I/O control
97 struct regmap
*aio_data_map
;
98 struct regmap
*aio_ctl_map
;
101 static const struct regmap_range aio_ctl_wr_ranges
[] = {
102 regmap_reg_range(0x0, 0x0), regmap_reg_range(0x2, 0x2), regmap_reg_range(0x9, 0x9),
103 regmap_reg_range(0x11, 0x11),
105 static const struct regmap_range aio_ctl_rd_ranges
[] = {
106 regmap_reg_range(0x2, 0x2), regmap_reg_range(0x8, 0x9), regmap_reg_range(0x11, 0x11),
108 static const struct regmap_range aio_ctl_volatile_ranges
[] = {
109 regmap_reg_range(0x8, 0x8),
111 static const struct regmap_access_table aio_ctl_wr_table
= {
112 .yes_ranges
= aio_ctl_wr_ranges
,
113 .n_yes_ranges
= ARRAY_SIZE(aio_ctl_wr_ranges
),
115 static const struct regmap_access_table aio_ctl_rd_table
= {
116 .yes_ranges
= aio_ctl_rd_ranges
,
117 .n_yes_ranges
= ARRAY_SIZE(aio_ctl_rd_ranges
),
119 static const struct regmap_access_table aio_ctl_volatile_table
= {
120 .yes_ranges
= aio_ctl_volatile_ranges
,
121 .n_yes_ranges
= ARRAY_SIZE(aio_ctl_volatile_ranges
),
124 static const struct regmap_config aio_ctl_regmap_config
= {
128 .reg_base
= STX104_AIO_BASE
,
131 .wr_table
= &aio_ctl_wr_table
,
132 .rd_table
= &aio_ctl_rd_table
,
133 .volatile_table
= &aio_ctl_volatile_table
,
134 .cache_type
= REGCACHE_FLAT
,
137 static const struct regmap_range aio_data_wr_ranges
[] = {
138 regmap_reg_range(0x4, 0x6),
140 static const struct regmap_range aio_data_rd_ranges
[] = {
141 regmap_reg_range(0x0, 0x0),
143 static const struct regmap_access_table aio_data_wr_table
= {
144 .yes_ranges
= aio_data_wr_ranges
,
145 .n_yes_ranges
= ARRAY_SIZE(aio_data_wr_ranges
),
147 static const struct regmap_access_table aio_data_rd_table
= {
148 .yes_ranges
= aio_data_rd_ranges
,
149 .n_yes_ranges
= ARRAY_SIZE(aio_data_rd_ranges
),
152 static const struct regmap_config aio_data_regmap_config
= {
155 .reg_stride
= STX104_AIO_DATA_STRIDE
,
156 .reg_base
= STX104_AIO_BASE
,
159 .wr_table
= &aio_data_wr_table
,
160 .rd_table
= &aio_data_rd_table
,
161 .volatile_table
= &aio_data_rd_table
,
162 .cache_type
= REGCACHE_FLAT
,
165 static const struct regmap_config dio_regmap_config
= {
169 .reg_base
= STX104_DIO_REG
,
174 static const struct regmap_range pit_wr_ranges
[] = {
175 regmap_reg_range(0x0, 0x3),
177 static const struct regmap_range pit_rd_ranges
[] = {
178 regmap_reg_range(0x0, 0x2),
180 static const struct regmap_access_table pit_wr_table
= {
181 .yes_ranges
= pit_wr_ranges
,
182 .n_yes_ranges
= ARRAY_SIZE(pit_wr_ranges
),
184 static const struct regmap_access_table pit_rd_table
= {
185 .yes_ranges
= pit_rd_ranges
,
186 .n_yes_ranges
= ARRAY_SIZE(pit_rd_ranges
),
189 static const struct regmap_config pit_regmap_config
= {
193 .reg_base
= STX104_I8254_BASE
,
196 .wr_table
= &pit_wr_table
,
197 .rd_table
= &pit_rd_table
,
200 static int stx104_read_raw(struct iio_dev
*indio_dev
,
201 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
203 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
205 unsigned int adc_config
;
207 unsigned int adc_status
;
210 case IIO_CHAN_INFO_HARDWAREGAIN
:
211 err
= regmap_read(priv
->aio_ctl_map
, STX104_ADC_CONFIGURATION
, &adc_config
);
215 *val
= BIT(u8_get_bits(adc_config
, STX104_GAIN
));
217 case IIO_CHAN_INFO_RAW
:
219 err
= regmap_read(priv
->aio_data_map
, STX104_DAC_OFFSET(chan
->channel
),
227 mutex_lock(&priv
->lock
);
229 /* select ADC channel */
230 err
= regmap_write(priv
->aio_ctl_map
, STX104_ADC_CHANNEL
,
231 STX104_SINGLE_CHANNEL(chan
->channel
));
233 mutex_unlock(&priv
->lock
);
238 * Trigger ADC sample capture by writing to the 8-bit Software Strobe Register and
239 * wait for completion; the conversion time range is 5 microseconds to 53.68 seconds
240 * in steps of 25 nanoseconds. The actual Analog Input Frame Timer time interval is
242 * ai_time_frame_ns = ( AIFT + 1 ) * ( 25 nanoseconds ).
243 * Where 0 <= AIFT <= 2147483648.
245 err
= regmap_write(priv
->aio_ctl_map
, STX104_SOFTWARE_STROBE
, 0);
247 mutex_unlock(&priv
->lock
);
250 err
= regmap_read_poll_timeout(priv
->aio_ctl_map
, STX104_ADC_STATUS
, adc_status
,
251 !u8_get_bits(adc_status
, STX104_CNV
), 0, 53687092);
253 mutex_unlock(&priv
->lock
);
257 err
= regmap_read(priv
->aio_data_map
, STX104_ADC_DATA
, &value
);
259 mutex_unlock(&priv
->lock
);
264 mutex_unlock(&priv
->lock
);
266 case IIO_CHAN_INFO_OFFSET
:
267 /* get ADC bipolar/unipolar configuration */
268 err
= regmap_read(priv
->aio_ctl_map
, STX104_ADC_CONFIGURATION
, &adc_config
);
272 *val
= (u8_get_bits(adc_config
, STX104_ADBU
) == STX104_BIPOLAR
) ? -32768 : 0;
274 case IIO_CHAN_INFO_SCALE
:
275 /* get ADC bipolar/unipolar and gain configuration */
276 err
= regmap_read(priv
->aio_ctl_map
, STX104_ADC_CONFIGURATION
, &adc_config
);
281 *val2
= (u8_get_bits(adc_config
, STX104_ADBU
) == STX104_BIPOLAR
) ? 14 : 15;
282 *val2
+= u8_get_bits(adc_config
, STX104_GAIN
);
283 return IIO_VAL_FRACTIONAL_LOG2
;
289 static int stx104_write_raw(struct iio_dev
*indio_dev
,
290 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
292 struct stx104_iio
*const priv
= iio_priv(indio_dev
);
296 case IIO_CHAN_INFO_HARDWAREGAIN
:
297 /* Only four gain states (x1, x2, x4, x8) */
300 gain
= STX104_GAIN_X1
;
303 gain
= STX104_GAIN_X2
;
306 gain
= STX104_GAIN_X4
;
309 gain
= STX104_GAIN_X8
;
315 return regmap_write(priv
->aio_ctl_map
, STX104_ADC_CONFIGURATION
, gain
);
316 case IIO_CHAN_INFO_RAW
:
320 if (val
< 0 || val
> U16_MAX
)
323 return regmap_write(priv
->aio_data_map
, STX104_DAC_OFFSET(chan
->channel
), val
);
329 static const struct iio_info stx104_info
= {
330 .read_raw
= stx104_read_raw
,
331 .write_raw
= stx104_write_raw
334 /* single-ended input channels configuration */
335 static const struct iio_chan_spec stx104_channels_sing
[] = {
336 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
337 STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0),
338 STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0),
339 STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0),
340 STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0),
341 STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0),
342 STX104_IN_CHAN(15, 0)
344 /* differential input channels configuration */
345 static const struct iio_chan_spec stx104_channels_diff
[] = {
346 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
347 STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1),
348 STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1),
349 STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1)
352 static int stx104_reg_mask_xlate(struct gpio_regmap
*const gpio
, const unsigned int base
,
353 unsigned int offset
, unsigned int *const reg
,
354 unsigned int *const mask
)
356 /* Output lines are located at same register bit offsets as input lines */
366 #define STX104_NGPIO 8
367 static const char *stx104_names
[STX104_NGPIO
] = {
368 "DIN0", "DIN1", "DIN2", "DIN3", "DOUT0", "DOUT1", "DOUT2", "DOUT3"
371 static int bank_select_i8254(struct regmap
*map
)
373 const u8 select_i8254
[] = { 0x3, 0xB, 0xA };
377 for (i
= 0; i
< ARRAY_SIZE(select_i8254
); i
++) {
378 err
= regmap_write_bits(map
, STX104_ADC_CONFIGURATION
, STX104_RBK
, select_i8254
[i
]);
386 static int stx104_init_hw(struct stx104_iio
*const priv
)
390 /* configure device for software trigger operation */
391 err
= regmap_write(priv
->aio_ctl_map
, STX104_ADC_CONTROL
, STX104_SOFTWARE_TRIGGER
);
395 /* initialize gain setting to x1 */
396 err
= regmap_write(priv
->aio_ctl_map
, STX104_ADC_CONFIGURATION
, STX104_GAIN_X1
);
400 /* initialize DAC outputs to 0V */
401 err
= regmap_write(priv
->aio_data_map
, STX104_DAC_BASE
, 0);
404 err
= regmap_write(priv
->aio_data_map
, STX104_DAC_BASE
+ STX104_AIO_DATA_STRIDE
, 0);
408 return bank_select_i8254(priv
->aio_ctl_map
);
411 static int stx104_probe(struct device
*dev
, unsigned int id
)
413 struct iio_dev
*indio_dev
;
414 struct stx104_iio
*priv
;
415 struct gpio_regmap_config gpio_config
;
416 struct i8254_regmap_config pit_config
;
417 void __iomem
*stx104_base
;
418 struct regmap
*aio_ctl_map
;
419 struct regmap
*aio_data_map
;
420 struct regmap
*dio_map
;
422 unsigned int adc_status
;
424 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
428 if (!devm_request_region(dev
, base
[id
], STX104_EXTENT
,
430 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
431 base
[id
], base
[id
] + STX104_EXTENT
);
435 stx104_base
= devm_ioport_map(dev
, base
[id
], STX104_EXTENT
);
439 aio_ctl_map
= devm_regmap_init_mmio(dev
, stx104_base
, &aio_ctl_regmap_config
);
440 if (IS_ERR(aio_ctl_map
))
441 return dev_err_probe(dev
, PTR_ERR(aio_ctl_map
),
442 "Unable to initialize aio_ctl register map\n");
444 aio_data_map
= devm_regmap_init_mmio(dev
, stx104_base
, &aio_data_regmap_config
);
445 if (IS_ERR(aio_data_map
))
446 return dev_err_probe(dev
, PTR_ERR(aio_data_map
),
447 "Unable to initialize aio_data register map\n");
449 dio_map
= devm_regmap_init_mmio(dev
, stx104_base
, &dio_regmap_config
);
451 return dev_err_probe(dev
, PTR_ERR(dio_map
),
452 "Unable to initialize dio register map\n");
454 pit_config
.map
= devm_regmap_init_mmio(dev
, stx104_base
, &pit_regmap_config
);
455 if (IS_ERR(pit_config
.map
))
456 return dev_err_probe(dev
, PTR_ERR(pit_config
.map
),
457 "Unable to initialize i8254 register map\n");
459 priv
= iio_priv(indio_dev
);
460 priv
->aio_ctl_map
= aio_ctl_map
;
461 priv
->aio_data_map
= aio_data_map
;
463 indio_dev
->info
= &stx104_info
;
464 indio_dev
->modes
= INDIO_DIRECT_MODE
;
466 err
= regmap_read(aio_ctl_map
, STX104_ADC_STATUS
, &adc_status
);
470 if (u8_get_bits(adc_status
, STX104_SD
) == STX104_DIFFERENTIAL
) {
471 indio_dev
->num_channels
= ARRAY_SIZE(stx104_channels_diff
);
472 indio_dev
->channels
= stx104_channels_diff
;
474 indio_dev
->num_channels
= ARRAY_SIZE(stx104_channels_sing
);
475 indio_dev
->channels
= stx104_channels_sing
;
478 indio_dev
->name
= dev_name(dev
);
480 mutex_init(&priv
->lock
);
482 err
= stx104_init_hw(priv
);
486 err
= devm_iio_device_register(dev
, indio_dev
);
490 gpio_config
= (struct gpio_regmap_config
) {
493 .ngpio
= STX104_NGPIO
,
494 .names
= stx104_names
,
495 .reg_dat_base
= GPIO_REGMAP_ADDR(STX104_DIO_REG
),
496 .reg_set_base
= GPIO_REGMAP_ADDR(STX104_DIO_REG
),
497 .ngpio_per_reg
= STX104_NGPIO
,
498 .reg_mask_xlate
= stx104_reg_mask_xlate
,
502 err
= PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev
, &gpio_config
));
506 pit_config
.parent
= dev
;
508 return devm_i8254_regmap_register(dev
, &pit_config
);
511 static struct isa_driver stx104_driver
= {
512 .probe
= stx104_probe
,
518 module_isa_driver(stx104_driver
, num_stx104
);
520 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
521 MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver");
522 MODULE_LICENSE("GPL v2");
523 MODULE_IMPORT_NS("I8254");