Skip to content

Commit 71b7b39

Browse files
William Breathitt Graybrgl
authored andcommitted
gpio: 104-dio-48e: Implement and utilize register structures
Reduce magic numbers and improve code readability by implementing and utilizing named register data structures. The 104-DIO-48E device features an Intel 8255 compatible GPIO interface, so the i8255 GPIO module is selected and utilized as well. Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Cc: John Hentges <jhentges@accesio.com> Cc: Jay Dolan <jay.dolan@accesio.com> Signed-off-by: William Breathitt Gray <william.gray@linaro.org> Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
1 parent fb38af4 commit 71b7b39

2 files changed

Lines changed: 75 additions & 175 deletions

File tree

drivers/gpio/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ config GPIO_104_DIO_48E
841841
depends on PC104
842842
select ISA_BUS_API
843843
select GPIOLIB_IRQCHIP
844+
select GPIO_I8255
844845
help
845846
Enables GPIO support for the ACCES 104-DIO-48E series (104-DIO-48E,
846847
104-DIO-24E). The base port addresses for the devices may be

drivers/gpio/gpio-104-dio-48e.c

Lines changed: 74 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* This driver supports the following ACCES devices: 104-DIO-48E and
77
* 104-DIO-24E.
88
*/
9-
#include <linux/bitmap.h>
10-
#include <linux/bitops.h>
9+
#include <linux/bits.h>
1110
#include <linux/device.h>
1211
#include <linux/errno.h>
1312
#include <linux/gpio/driver.h>
@@ -20,6 +19,11 @@
2019
#include <linux/module.h>
2120
#include <linux/moduleparam.h>
2221
#include <linux/spinlock.h>
22+
#include <linux/types.h>
23+
24+
#include "gpio-i8255.h"
25+
26+
MODULE_IMPORT_NS(I8255);
2327

2428
#define DIO48E_EXTENT 16
2529
#define MAX_NUM_DIO48E max_num_isa_dev(DIO48E_EXTENT)
@@ -33,73 +37,64 @@ static unsigned int irq[MAX_NUM_DIO48E];
3337
module_param_hw_array(irq, uint, irq, NULL, 0);
3438
MODULE_PARM_DESC(irq, "ACCES 104-DIO-48E interrupt line numbers");
3539

40+
#define DIO48E_NUM_PPI 2
41+
42+
/**
43+
* struct dio48e_reg - device register structure
44+
* @ppi: Programmable Peripheral Interface groups
45+
* @enable_buffer: Enable/Disable Buffer groups
46+
* @unused1: Unused
47+
* @enable_interrupt: Write: Enable Interrupt
48+
* Read: Disable Interrupt
49+
* @unused2: Unused
50+
* @enable_counter: Write: Enable Counter/Timer Addressing
51+
* Read: Disable Counter/Timer Addressing
52+
* @unused3: Unused
53+
* @clear_interrupt: Clear Interrupt
54+
*/
55+
struct dio48e_reg {
56+
struct i8255 ppi[DIO48E_NUM_PPI];
57+
u8 enable_buffer[DIO48E_NUM_PPI];
58+
u8 unused1;
59+
u8 enable_interrupt;
60+
u8 unused2;
61+
u8 enable_counter;
62+
u8 unused3;
63+
u8 clear_interrupt;
64+
};
65+
3666
/**
3767
* struct dio48e_gpio - GPIO device private data structure
38-
* @chip: instance of the gpio_chip
39-
* @io_state: bit I/O state (whether bit is set to input or output)
40-
* @out_state: output bits state
41-
* @control: Control registers state
42-
* @lock: synchronization lock to prevent I/O race conditions
43-
* @base: base port address of the GPIO device
44-
* @irq_mask: I/O bits affected by interrupts
68+
* @chip: instance of the gpio_chip
69+
* @ppi_state: PPI device states
70+
* @lock: synchronization lock to prevent I/O race conditions
71+
* @reg: I/O address offset for the device registers
72+
* @irq_mask: I/O bits affected by interrupts
4573
*/
4674
struct dio48e_gpio {
4775
struct gpio_chip chip;
48-
unsigned char io_state[6];
49-
unsigned char out_state[6];
50-
unsigned char control[2];
76+
struct i8255_state ppi_state[DIO48E_NUM_PPI];
5177
raw_spinlock_t lock;
52-
void __iomem *base;
78+
struct dio48e_reg __iomem *reg;
5379
unsigned char irq_mask;
5480
};
5581

5682
static int dio48e_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
5783
{
5884
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
59-
const unsigned int port = offset / 8;
60-
const unsigned int mask = BIT(offset % 8);
6185

62-
if (dio48egpio->io_state[port] & mask)
63-
return GPIO_LINE_DIRECTION_IN;
86+
if (i8255_get_direction(dio48egpio->ppi_state, offset))
87+
return GPIO_LINE_DIRECTION_IN;
6488

6589
return GPIO_LINE_DIRECTION_OUT;
6690
}
6791

6892
static int dio48e_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
6993
{
7094
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
71-
const unsigned int io_port = offset / 8;
72-
const unsigned int control_port = io_port / 3;
73-
void __iomem *const control_addr = dio48egpio->base + 3 + control_port * 4;
74-
unsigned long flags;
75-
unsigned int control;
76-
77-
raw_spin_lock_irqsave(&dio48egpio->lock, flags);
7895

79-
/* Check if configuring Port C */
80-
if (io_port == 2 || io_port == 5) {
81-
/* Port C can be configured by nibble */
82-
if (offset % 8 > 3) {
83-
dio48egpio->io_state[io_port] |= 0xF0;
84-
dio48egpio->control[control_port] |= BIT(3);
85-
} else {
86-
dio48egpio->io_state[io_port] |= 0x0F;
87-
dio48egpio->control[control_port] |= BIT(0);
88-
}
89-
} else {
90-
dio48egpio->io_state[io_port] |= 0xFF;
91-
if (io_port == 0 || io_port == 3)
92-
dio48egpio->control[control_port] |= BIT(4);
93-
else
94-
dio48egpio->control[control_port] |= BIT(1);
95-
}
96-
97-
control = BIT(7) | dio48egpio->control[control_port];
98-
iowrite8(control, control_addr);
99-
control &= ~BIT(7);
100-
iowrite8(control, control_addr);
101-
102-
raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
96+
i8255_direction_input(dio48egpio->reg->ppi, dio48egpio->ppi_state,
97+
offset);
10398

10499
return 0;
105100
}
@@ -108,146 +103,44 @@ static int dio48e_gpio_direction_output(struct gpio_chip *chip, unsigned int off
108103
int value)
109104
{
110105
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
111-
const unsigned int io_port = offset / 8;
112-
const unsigned int control_port = io_port / 3;
113-
const unsigned int mask = BIT(offset % 8);
114-
void __iomem *const control_addr = dio48egpio->base + 3 + control_port * 4;
115-
const unsigned int out_port = (io_port > 2) ? io_port + 1 : io_port;
116-
unsigned long flags;
117-
unsigned int control;
118-
119-
raw_spin_lock_irqsave(&dio48egpio->lock, flags);
120-
121-
/* Check if configuring Port C */
122-
if (io_port == 2 || io_port == 5) {
123-
/* Port C can be configured by nibble */
124-
if (offset % 8 > 3) {
125-
dio48egpio->io_state[io_port] &= 0x0F;
126-
dio48egpio->control[control_port] &= ~BIT(3);
127-
} else {
128-
dio48egpio->io_state[io_port] &= 0xF0;
129-
dio48egpio->control[control_port] &= ~BIT(0);
130-
}
131-
} else {
132-
dio48egpio->io_state[io_port] &= 0x00;
133-
if (io_port == 0 || io_port == 3)
134-
dio48egpio->control[control_port] &= ~BIT(4);
135-
else
136-
dio48egpio->control[control_port] &= ~BIT(1);
137-
}
138-
139-
if (value)
140-
dio48egpio->out_state[io_port] |= mask;
141-
else
142-
dio48egpio->out_state[io_port] &= ~mask;
143106

144-
control = BIT(7) | dio48egpio->control[control_port];
145-
iowrite8(control, control_addr);
146-
147-
iowrite8(dio48egpio->out_state[io_port], dio48egpio->base + out_port);
148-
149-
control &= ~BIT(7);
150-
iowrite8(control, control_addr);
151-
152-
raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
107+
i8255_direction_output(dio48egpio->reg->ppi, dio48egpio->ppi_state,
108+
offset, value);
153109

154110
return 0;
155111
}
156112

157113
static int dio48e_gpio_get(struct gpio_chip *chip, unsigned int offset)
158114
{
159115
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
160-
const unsigned int port = offset / 8;
161-
const unsigned int mask = BIT(offset % 8);
162-
const unsigned int in_port = (port > 2) ? port + 1 : port;
163-
unsigned long flags;
164-
unsigned int port_state;
165-
166-
raw_spin_lock_irqsave(&dio48egpio->lock, flags);
167116

168-
/* ensure that GPIO is set for input */
169-
if (!(dio48egpio->io_state[port] & mask)) {
170-
raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
171-
return -EINVAL;
172-
}
173-
174-
port_state = ioread8(dio48egpio->base + in_port);
175-
176-
raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
177-
178-
return !!(port_state & mask);
117+
return i8255_get(dio48egpio->reg->ppi, offset);
179118
}
180119

181-
static const size_t ports[] = { 0, 1, 2, 4, 5, 6 };
182-
183120
static int dio48e_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
184121
unsigned long *bits)
185122
{
186123
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
187-
unsigned long offset;
188-
unsigned long gpio_mask;
189-
void __iomem *port_addr;
190-
unsigned long port_state;
191124

192-
/* clear bits array to a clean slate */
193-
bitmap_zero(bits, chip->ngpio);
194-
195-
for_each_set_clump8(offset, gpio_mask, mask, ARRAY_SIZE(ports) * 8) {
196-
port_addr = dio48egpio->base + ports[offset / 8];
197-
port_state = ioread8(port_addr) & gpio_mask;
198-
199-
bitmap_set_value8(bits, port_state, offset);
200-
}
125+
i8255_get_multiple(dio48egpio->reg->ppi, mask, bits, chip->ngpio);
201126

202127
return 0;
203128
}
204129

205130
static void dio48e_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
206131
{
207132
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
208-
const unsigned int port = offset / 8;
209-
const unsigned int mask = BIT(offset % 8);
210-
const unsigned int out_port = (port > 2) ? port + 1 : port;
211-
unsigned long flags;
212133

213-
raw_spin_lock_irqsave(&dio48egpio->lock, flags);
214-
215-
if (value)
216-
dio48egpio->out_state[port] |= mask;
217-
else
218-
dio48egpio->out_state[port] &= ~mask;
219-
220-
iowrite8(dio48egpio->out_state[port], dio48egpio->base + out_port);
221-
222-
raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
134+
i8255_set(dio48egpio->reg->ppi, dio48egpio->ppi_state, offset, value);
223135
}
224136

225137
static void dio48e_gpio_set_multiple(struct gpio_chip *chip,
226138
unsigned long *mask, unsigned long *bits)
227139
{
228140
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(chip);
229-
unsigned long offset;
230-
unsigned long gpio_mask;
231-
size_t index;
232-
void __iomem *port_addr;
233-
unsigned long bitmask;
234-
unsigned long flags;
235-
236-
for_each_set_clump8(offset, gpio_mask, mask, ARRAY_SIZE(ports) * 8) {
237-
index = offset / 8;
238-
port_addr = dio48egpio->base + ports[index];
239-
240-
bitmask = bitmap_get_value8(bits, offset) & gpio_mask;
241-
242-
raw_spin_lock_irqsave(&dio48egpio->lock, flags);
243141

244-
/* update output state data and set device gpio register */
245-
dio48egpio->out_state[index] &= ~gpio_mask;
246-
dio48egpio->out_state[index] |= bitmask;
247-
iowrite8(dio48egpio->out_state[index], port_addr);
248-
249-
raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
250-
}
142+
i8255_set_multiple(dio48egpio->reg->ppi, dio48egpio->ppi_state, mask,
143+
bits, chip->ngpio);
251144
}
252145

253146
static void dio48e_irq_ack(struct irq_data *data)
@@ -274,7 +167,7 @@ static void dio48e_irq_mask(struct irq_data *data)
274167

275168
if (!dio48egpio->irq_mask)
276169
/* disable interrupts */
277-
ioread8(dio48egpio->base + 0xB);
170+
ioread8(&dio48egpio->reg->enable_interrupt);
278171

279172
raw_spin_unlock_irqrestore(&dio48egpio->lock, flags);
280173
}
@@ -294,8 +187,8 @@ static void dio48e_irq_unmask(struct irq_data *data)
294187

295188
if (!dio48egpio->irq_mask) {
296189
/* enable interrupts */
297-
iowrite8(0x00, dio48egpio->base + 0xF);
298-
iowrite8(0x00, dio48egpio->base + 0xB);
190+
iowrite8(0x00, &dio48egpio->reg->clear_interrupt);
191+
iowrite8(0x00, &dio48egpio->reg->enable_interrupt);
299192
}
300193

301194
if (offset == 19)
@@ -341,7 +234,7 @@ static irqreturn_t dio48e_irq_handler(int irq, void *dev_id)
341234

342235
raw_spin_lock(&dio48egpio->lock);
343236

344-
iowrite8(0x00, dio48egpio->base + 0xF);
237+
iowrite8(0x00, &dio48egpio->reg->clear_interrupt);
345238

346239
raw_spin_unlock(&dio48egpio->lock);
347240

@@ -373,11 +266,26 @@ static int dio48e_irq_init_hw(struct gpio_chip *gc)
373266
struct dio48e_gpio *const dio48egpio = gpiochip_get_data(gc);
374267

375268
/* Disable IRQ by default */
376-
ioread8(dio48egpio->base + 0xB);
269+
ioread8(&dio48egpio->reg->enable_interrupt);
377270

378271
return 0;
379272
}
380273

274+
static void dio48e_init_ppi(struct i8255 __iomem *const ppi,
275+
struct i8255_state *const ppi_state)
276+
{
277+
const unsigned long ngpio = 24;
278+
const unsigned long mask = GENMASK(ngpio - 1, 0);
279+
const unsigned long bits = 0;
280+
unsigned long i;
281+
282+
/* Initialize all GPIO to output 0 */
283+
for (i = 0; i < DIO48E_NUM_PPI; i++) {
284+
i8255_mode0_output(&ppi[i]);
285+
i8255_set_multiple(&ppi[i], &ppi_state[i], &mask, &bits, ngpio);
286+
}
287+
}
288+
381289
static int dio48e_probe(struct device *dev, unsigned int id)
382290
{
383291
struct dio48e_gpio *dio48egpio;
@@ -395,8 +303,8 @@ static int dio48e_probe(struct device *dev, unsigned int id)
395303
return -EBUSY;
396304
}
397305

398-
dio48egpio->base = devm_ioport_map(dev, base[id], DIO48E_EXTENT);
399-
if (!dio48egpio->base)
306+
dio48egpio->reg = devm_ioport_map(dev, base[id], DIO48E_EXTENT);
307+
if (!dio48egpio->reg)
400308
return -ENOMEM;
401309

402310
dio48egpio->chip.label = name;
@@ -425,17 +333,8 @@ static int dio48e_probe(struct device *dev, unsigned int id)
425333

426334
raw_spin_lock_init(&dio48egpio->lock);
427335

428-
/* initialize all GPIO as output */
429-
iowrite8(0x80, dio48egpio->base + 3);
430-
iowrite8(0x00, dio48egpio->base);
431-
iowrite8(0x00, dio48egpio->base + 1);
432-
iowrite8(0x00, dio48egpio->base + 2);
433-
iowrite8(0x00, dio48egpio->base + 3);
434-
iowrite8(0x80, dio48egpio->base + 7);
435-
iowrite8(0x00, dio48egpio->base + 4);
436-
iowrite8(0x00, dio48egpio->base + 5);
437-
iowrite8(0x00, dio48egpio->base + 6);
438-
iowrite8(0x00, dio48egpio->base + 7);
336+
i8255_state_init(dio48egpio->ppi_state, DIO48E_NUM_PPI);
337+
dio48e_init_ppi(dio48egpio->reg->ppi, dio48egpio->ppi_state);
439338

440339
err = devm_gpiochip_add_data(dev, &dio48egpio->chip, dio48egpio);
441340
if (err) {

0 commit comments

Comments
 (0)