1 // SPDX-License-Identifier: GPL-2.0
3 * GPIO library for the ACCES IDIO-16 family
4 * Copyright (C) 2022 William Breathitt Gray
6 #include <linux/bitmap.h>
7 #include <linux/export.h>
9 #include <linux/module.h>
10 #include <linux/spinlock.h>
11 #include <linux/types.h>
13 #include "gpio-idio-16.h"
15 #define DEFAULT_SYMBOL_NAMESPACE GPIO_IDIO_16
18 * idio_16_get - get signal value at signal offset
19 * @reg: ACCES IDIO-16 device registers
20 * @state: ACCES IDIO-16 device state
21 * @offset: offset of signal to get
23 * Returns the signal value (0=low, 1=high) for the signal at @offset.
25 int idio_16_get(struct idio_16 __iomem *const reg,
26 struct idio_16_state *const state, const unsigned long offset)
28 const unsigned long mask = BIT(offset);
30 if (offset < IDIO_16_NOUT)
31 return test_bit(offset, state->out_state);
34 return !!(ioread8(®->in0_7) & (mask >> IDIO_16_NOUT));
37 return !!(ioread8(®->in8_15) & (mask >> 24));
41 EXPORT_SYMBOL_GPL(idio_16_get);
44 * idio_16_get_multiple - get multiple signal values at multiple signal offsets
45 * @reg: ACCES IDIO-16 device registers
46 * @state: ACCES IDIO-16 device state
47 * @mask: mask of signals to get
48 * @bits: bitmap to store signal values
50 * Stores in @bits the values (0=low, 1=high) for the signals defined by @mask.
52 void idio_16_get_multiple(struct idio_16 __iomem *const reg,
53 struct idio_16_state *const state,
54 const unsigned long *const mask,
55 unsigned long *const bits)
58 const unsigned long out_mask = GENMASK(IDIO_16_NOUT - 1, 0);
60 spin_lock_irqsave(&state->lock, flags);
62 bitmap_replace(bits, bits, state->out_state, &out_mask, IDIO_16_NOUT);
63 if (*mask & GENMASK(23, 16))
64 bitmap_set_value8(bits, ioread8(®->in0_7), 16);
65 if (*mask & GENMASK(31, 24))
66 bitmap_set_value8(bits, ioread8(®->in8_15), 24);
68 spin_unlock_irqrestore(&state->lock, flags);
70 EXPORT_SYMBOL_GPL(idio_16_get_multiple);
73 * idio_16_set - set signal value at signal offset
74 * @reg: ACCES IDIO-16 device registers
75 * @state: ACCES IDIO-16 device state
76 * @offset: offset of signal to set
77 * @value: value of signal to set
79 * Assigns output @value for the signal at @offset.
81 void idio_16_set(struct idio_16 __iomem *const reg,
82 struct idio_16_state *const state, const unsigned long offset,
83 const unsigned long value)
87 if (offset >= IDIO_16_NOUT)
90 spin_lock_irqsave(&state->lock, flags);
92 __assign_bit(offset, state->out_state, value);
94 iowrite8(bitmap_get_value8(state->out_state, 0), ®->out0_7);
96 iowrite8(bitmap_get_value8(state->out_state, 8), ®->out8_15);
98 spin_unlock_irqrestore(&state->lock, flags);
100 EXPORT_SYMBOL_GPL(idio_16_set);
103 * idio_16_set_multiple - set signal values at multiple signal offsets
104 * @reg: ACCES IDIO-16 device registers
105 * @state: ACCES IDIO-16 device state
106 * @mask: mask of signals to set
107 * @bits: bitmap of signal output values
109 * Assigns output values defined by @bits for the signals defined by @mask.
111 void idio_16_set_multiple(struct idio_16 __iomem *const reg,
112 struct idio_16_state *const state,
113 const unsigned long *const mask,
114 const unsigned long *const bits)
118 spin_lock_irqsave(&state->lock, flags);
120 bitmap_replace(state->out_state, state->out_state, bits, mask,
122 if (*mask & GENMASK(7, 0))
123 iowrite8(bitmap_get_value8(state->out_state, 0), ®->out0_7);
124 if (*mask & GENMASK(15, 8))
125 iowrite8(bitmap_get_value8(state->out_state, 8), ®->out8_15);
127 spin_unlock_irqrestore(&state->lock, flags);
129 EXPORT_SYMBOL_GPL(idio_16_set_multiple);
132 * idio_16_state_init - initialize idio_16_state structure
133 * @state: ACCES IDIO-16 device state
135 * Initializes the ACCES IDIO-16 device @state for use in idio-16 library
138 void idio_16_state_init(struct idio_16_state *const state)
140 spin_lock_init(&state->lock);
142 EXPORT_SYMBOL_GPL(idio_16_state_init);
144 MODULE_AUTHOR("William Breathitt Gray");
145 MODULE_DESCRIPTION("ACCES IDIO-16 GPIO Library");
146 MODULE_LICENSE("GPL");