Skip to contentSkip to conversation

gpio: Implement and utilize register structures for ISA drivers

PATCH Superseded William Breathitt Gray 4 participants 11 messages Jul 18, 2022 – Jul 19, 2022

Subsystems (7)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover gpio: Implement and utilize register structures for ISA drivers

Changes in v3:
- Updated contact information in MAINTAINERS
- Added help text for GPIO_I8255 Kconfig option
- Move include/linux/gpio/i8255.h to drivers/gpio/gpio-i8255.h
- Include "gpio-i8255.h" instead of <linux/gpio/i8255.h>
- Include linux/types.h instead of linux/compiler_types.h
- Add underscores for *PORTC_LOWER* and *PORTC_UPPER* defines
- Move (offset % 8) expression to a port_offset const above the io_port const in i8255_direction_mask(); this should help optimize assembly instructions on some architectures
- Implement an opaque i8255_state struct to organize and access i8255 device states; this replaces the control_state array passed to various i8255 library functions in previous patchsets
- Implement and provide a i8255_state_init() function to initialize the i8255_state struct for a consumer
- Use a spinlock within i8255 library functions to protect access to i8255 states and synchronize I/O operations; a spinlock is used so that these functions may be used within an interrupt context
- Export the i8255 library symbols within a new I8255 namespace
- Update the 104-dio-48e, 104-idi-48, gpio-mm drivers to use the new i8255_state struct and I8255 namespace

The PC104/ISA drivers were updated to use I/O memory accessor calls such as ioread8()/iowrite8() in a previous patch series [0]. This patchset is a continuation of the effort to improve the code readability and reduce magic numbers by implementing and utilizing named register data structures.

One of the benefits is that we can now observe more easily similarities in devices that share similar interfaces; such as the i8255 interfaces used by the 104-DIO-48E, 104-IDI-48, and GPIO-MM drivers -- as well as the similar interface used by the 104-IDIO-16 and PCI-IDIO-16 drivers.

A new module supporting the Intel 8255 interface is introduced to consolidate the common code found among the 104-DIO-48E, 104-IDI-48, and GPIO-MM drivers.

[0] https://lore.kernel.org/all/[email protected]/

William Breathitt Gray (6):
gpio: ws16c48: Implement and utilize register structures
gpio: 104-idio-16: Implement and utilize register structures
gpio: i8255: Introduce the Intel 8255 interface library module
gpio: 104-dio-48e: Implement and utilize register structures
gpio: 104-idi-48: Implement and utilize register structures
gpio: gpio-mm: Implement and utilize register structures

 MAINTAINERS                     |   6 +
 drivers/gpio/Kconfig            |  13 ++
 drivers/gpio/Makefile           |   1 +
 drivers/gpio/gpio-104-dio-48e.c | 248 ++++++++-------------------
 drivers/gpio/gpio-104-idi-48.c  | 141 +++++++--------
 drivers/gpio/gpio-104-idio-16.c |  58 +++++--
 drivers/gpio/gpio-gpio-mm.c     | 202 +++++-----------------
 drivers/gpio/gpio-i8255.c       | 292 ++++++++++++++++++++++++++++++++
 drivers/gpio/gpio-i8255.h       |  47 +++++
 drivers/gpio/gpio-ws16c48.c     | 119 +++++++++----
 10 files changed, 657 insertions(+), 470 deletions(-)
 create mode 100644 drivers/gpio/gpio-i8255.c
 create mode 100644 drivers/gpio/gpio-i8255.h

base-commit: f2906aa863381afb0015a9eb7fefad885d4e5a56

Signature · 1 line
2.36.1
#1 lore raw

PATCH v3 1/6 gpio: ws16c48: Implement and utilize register structures

Reduce magic numbers and improve code readability by implementing and utilizing named register data structures.

---
Changes in v3: None

 drivers/gpio/gpio-ws16c48.c | 119 +++++++++++++++++++++++++-----------
 1 file changed, 84 insertions(+), 35 deletions(-)
drivers/gpio/gpio-ws16c48.c +84 -35
index 5078631d8014..663d4491b90f 100644
@@ -17,8 +17,9 @@
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/spinlock.h>
+#include <linux/types.h>
 
-#define WS16C48_EXTENT 16
+#define WS16C48_EXTENT 10
 #define MAX_NUM_WS16C48 max_num_isa_dev(WS16C48_EXTENT)
 
 static unsigned int base[MAX_NUM_WS16C48];
@@ -30,6 +31,20 @@ static unsigned int irq[MAX_NUM_WS16C48];
 module_param_hw_array(irq, uint, irq, NULL, 0);
 MODULE_PARM_DESC(irq, "WinSystems WS16C48 interrupt line numbers");
 
+/**
+ * struct ws16c48_reg - device register structure
+ * @port:		Port 0 through 5 I/O
+ * @int_pending:	Interrupt Pending
+ * @page_lock:		Register page (Bits 7-6) and I/O port lock (Bits 5-0)
+ * @pol_enab_int_id:	Interrupt polarity, enable, and ID
+ */
+struct ws16c48_reg {
+	u8 port[6];
+	u8 int_pending;
+	u8 page_lock;
+	u8 pol_enab_int_id[3];
+};
+
 /**
  * struct ws16c48_gpio - GPIO device private data structure
  * @chip:	instance of the gpio_chip
@@ -38,7 +53,7 @@ MODULE_PARM_DESC(irq, "WinSystems WS16C48 interrupt line numbers");
  * @lock:	synchronization lock to prevent I/O race conditions
  * @irq_mask:	I/O bits affected by interrupts
  * @flow_mask:	IRQ flow type mask for the respective I/O bits
- * @base:	base port address of the GPIO device
+ * @reg:	I/O address offset for the device registers
  */
 struct ws16c48_gpio {
 	struct gpio_chip chip;
@@ -47,7 +62,7 @@ struct ws16c48_gpio {
 	raw_spinlock_t lock;
 	unsigned long irq_mask;
 	unsigned long flow_mask;
-	void __iomem *base;
+	struct ws16c48_reg __iomem *reg;
 };
 
 static int ws16c48_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
@@ -73,7 +88,7 @@ static int ws16c48_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
 
 	ws16c48gpio->io_state[port] |= mask;
 	ws16c48gpio->out_state[port] &= ~mask;
-	iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
+	iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->reg->port + port);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 
@@ -95,7 +110,7 @@ static int ws16c48_gpio_direction_output(struct gpio_chip *chip,
 		ws16c48gpio->out_state[port] |= mask;
 	else
 		ws16c48gpio->out_state[port] &= ~mask;
-	iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
+	iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->reg->port + port);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 
@@ -118,7 +133,7 @@ static int ws16c48_gpio_get(struct gpio_chip *chip, unsigned offset)
 		return -EINVAL;
 	}
 
-	port_state = ioread8(ws16c48gpio->base + port);
+	port_state = ioread8(ws16c48gpio->reg->port + port);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 
@@ -131,14 +146,16 @@ static int ws16c48_gpio_get_multiple(struct gpio_chip *chip,
 	struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip);
 	unsigned long offset;
 	unsigned long gpio_mask;
-	void __iomem *port_addr;
+	size_t index;
+	u8 __iomem *port_addr;
 	unsigned long port_state;
 
 	/* clear bits array to a clean slate */
 	bitmap_zero(bits, chip->ngpio);
 
 	for_each_set_clump8(offset, gpio_mask, mask, chip->ngpio) {
-		port_addr = ws16c48gpio->base + offset / 8;
+		index = offset / 8;
+		port_addr = ws16c48gpio->reg->port + index;
 		port_state = ioread8(port_addr) & gpio_mask;
 
 		bitmap_set_value8(bits, port_state, offset);
@@ -166,7 +183,7 @@ static void ws16c48_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
 		ws16c48gpio->out_state[port] |= mask;
 	else
 		ws16c48gpio->out_state[port] &= ~mask;
-	iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->base + port);
+	iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->reg->port + port);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 }
@@ -178,13 +195,13 @@ static void ws16c48_gpio_set_multiple(struct gpio_chip *chip,
 	unsigned long offset;
 	unsigned long gpio_mask;
 	size_t index;
-	void __iomem *port_addr;
+	u8 __iomem *port_addr;
 	unsigned long bitmask;
 	unsigned long flags;
 
 	for_each_set_clump8(offset, gpio_mask, mask, chip->ngpio) {
 		index = offset / 8;
-		port_addr = ws16c48gpio->base + index;
+		port_addr = ws16c48gpio->reg->port + index;
 
 		/* mask out GPIO configured for input */
 		gpio_mask &= ~ws16c48gpio->io_state[index];
@@ -219,10 +236,15 @@ static void ws16c48_irq_ack(struct irq_data *data)
 
 	port_state = ws16c48gpio->irq_mask >> (8*port);
 
-	iowrite8(0x80, ws16c48gpio->base + 7);
-	iowrite8(port_state & ~mask, ws16c48gpio->base + 8 + port);
-	iowrite8(port_state | mask, ws16c48gpio->base + 8 + port);
-	iowrite8(0xC0, ws16c48gpio->base + 7);
+	/* Select Register Page 2; Unlock all I/O ports */
+	iowrite8(0x80, &ws16c48gpio->reg->page_lock);
+
+	/* Clear pending interrupt */
+	iowrite8(port_state & ~mask, ws16c48gpio->reg->pol_enab_int_id + port);
+	iowrite8(port_state | mask, ws16c48gpio->reg->pol_enab_int_id + port);
+
+	/* Select Register Page 3; Unlock all I/O ports */
+	iowrite8(0xC0, &ws16c48gpio->reg->page_lock);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 }
@@ -235,6 +257,7 @@ static void ws16c48_irq_mask(struct irq_data *data)
 	const unsigned long mask = BIT(offset);
 	const unsigned port = offset / 8;
 	unsigned long flags;
+	unsigned long port_state;
 
 	/* only the first 3 ports support interrupts */
 	if (port > 2)
@@ -243,10 +266,16 @@ static void ws16c48_irq_mask(struct irq_data *data)
 	raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
 
 	ws16c48gpio->irq_mask &= ~mask;
+	port_state = ws16c48gpio->irq_mask >> (8 * port);
+
+	/* Select Register Page 2; Unlock all I/O ports */
+	iowrite8(0x80, &ws16c48gpio->reg->page_lock);
 
-	iowrite8(0x80, ws16c48gpio->base + 7);
-	iowrite8(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
-	iowrite8(0xC0, ws16c48gpio->base + 7);
+	/* Disable interrupt */
+	iowrite8(port_state, ws16c48gpio->reg->pol_enab_int_id + port);
+
+	/* Select Register Page 3; Unlock all I/O ports */
+	iowrite8(0xC0, &ws16c48gpio->reg->page_lock);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 }
@@ -259,6 +288,7 @@ static void ws16c48_irq_unmask(struct irq_data *data)
 	const unsigned long mask = BIT(offset);
 	const unsigned port = offset / 8;
 	unsigned long flags;
+	unsigned long port_state;
 
 	/* only the first 3 ports support interrupts */
 	if (port > 2)
@@ -267,10 +297,16 @@ static void ws16c48_irq_unmask(struct irq_data *data)
 	raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
 
 	ws16c48gpio->irq_mask |= mask;
+	port_state = ws16c48gpio->irq_mask >> (8 * port);
+
+	/* Select Register Page 2; Unlock all I/O ports */
+	iowrite8(0x80, &ws16c48gpio->reg->page_lock);
 
-	iowrite8(0x80, ws16c48gpio->base + 7);
-	iowrite8(ws16c48gpio->irq_mask >> (8*port), ws16c48gpio->base + 8 + port);
-	iowrite8(0xC0, ws16c48gpio->base + 7);
+	/* Enable interrupt */
+	iowrite8(port_state, ws16c48gpio->reg->pol_enab_int_id + port);
+
+	/* Select Register Page 3; Unlock all I/O ports */
+	iowrite8(0xC0, &ws16c48gpio->reg->page_lock);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 }
@@ -283,6 +319,7 @@ static int ws16c48_irq_set_type(struct irq_data *data, unsigned flow_type)
 	const unsigned long mask = BIT(offset);
 	const unsigned port = offset / 8;
 	unsigned long flags;
+	unsigned long port_state;
 
 	/* only the first 3 ports support interrupts */
 	if (port > 2)
@@ -304,9 +341,16 @@ static int ws16c48_irq_set_type(struct irq_data *data, unsigned flow_type)
 		return -EINVAL;
 	}
 
-	iowrite8(0x40, ws16c48gpio->base + 7);
-	iowrite8(ws16c48gpio->flow_mask >> (8*port), ws16c48gpio->base + 8 + port);
-	iowrite8(0xC0, ws16c48gpio->base + 7);
+	port_state = ws16c48gpio->flow_mask >> (8 * port);
+
+	/* Select Register Page 1; Unlock all I/O ports */
+	iowrite8(0x40, &ws16c48gpio->reg->page_lock);
+
+	/* Set interrupt polarity */
+	iowrite8(port_state, ws16c48gpio->reg->pol_enab_int_id + port);
+
+	/* Select Register Page 3; Unlock all I/O ports */
+	iowrite8(0xC0, &ws16c48gpio->reg->page_lock);
 
 	raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
 
@@ -325,25 +369,26 @@ static irqreturn_t ws16c48_irq_handler(int irq, void *dev_id)
 {
 	struct ws16c48_gpio *const ws16c48gpio = dev_id;
 	struct gpio_chip *const chip = &ws16c48gpio->chip;
+	struct ws16c48_reg __iomem *const reg = ws16c48gpio->reg;
 	unsigned long int_pending;
 	unsigned long port;
 	unsigned long int_id;
 	unsigned long gpio;
 
-	int_pending = ioread8(ws16c48gpio->base + 6) & 0x7;
+	int_pending = ioread8(&reg->int_pending) & 0x7;
 	if (!int_pending)
 		return IRQ_NONE;
 
 	/* loop until all pending interrupts are handled */
 	do {
 		for_each_set_bit(port, &int_pending, 3) {
-			int_id = ioread8(ws16c48gpio->base + 8 + port);
+			int_id = ioread8(reg->pol_enab_int_id + port);
 			for_each_set_bit(gpio, &int_id, 8)
 				generic_handle_domain_irq(chip->irq.domain,
 							  gpio + 8*port);
 		}
 
-		int_pending = ioread8(ws16c48gpio->base + 6) & 0x7;
+		int_pending = ioread8(&reg->int_pending) & 0x7;
 	} while (int_pending);
 
 	return IRQ_HANDLED;
@@ -369,12 +414,16 @@ static int ws16c48_irq_init_hw(struct gpio_chip *gc)
 {
 	struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(gc);
 
-	/* Disable IRQ by default */
-	iowrite8(0x80, ws16c48gpio->base + 7);
-	iowrite8(0, ws16c48gpio->base + 8);
-	iowrite8(0, ws16c48gpio->base + 9);
-	iowrite8(0, ws16c48gpio->base + 10);
-	iowrite8(0xC0, ws16c48gpio->base + 7);
+	/* Select Register Page 2; Unlock all I/O ports */
+	iowrite8(0x80, &ws16c48gpio->reg->page_lock);
+
+	/* Disable interrupts for all lines */
+	iowrite8(0, &ws16c48gpio->reg->pol_enab_int_id[0]);
+	iowrite8(0, &ws16c48gpio->reg->pol_enab_int_id[1]);
+	iowrite8(0, &ws16c48gpio->reg->pol_enab_int_id[2]);
+
+	/* Select Register Page 3; Unlock all I/O ports */
+	iowrite8(0xC0, &ws16c48gpio->reg->page_lock);
 
 	return 0;
 }
@@ -396,8 +445,8 @@ static int ws16c48_probe(struct device *dev, unsigned int id)
 		return -EBUSY;
 	}
 
-	ws16c48gpio->base = devm_ioport_map(dev, base[id], WS16C48_EXTENT);
-	if (!ws16c48gpio->base)
+	ws16c48gpio->reg = devm_ioport_map(dev, base[id], WS16C48_EXTENT);
+	if (!ws16c48gpio->reg)
 		return -ENOMEM;
 
 	ws16c48gpio->chip.label = name;
Signature · 1 line
2.36.1
#2 lore raw

PATCH v3 2/6 gpio: 104-idio-16: Implement and utilize register structures

In reply to William Breathitt Gray

Reduce magic numbers and improve code readability by implementing and utilizing named register data structures.

---
Changes in v3: None

 drivers/gpio/gpio-104-idio-16.c | 58 +++++++++++++++++++++++----------
 1 file changed, 41 insertions(+), 17 deletions(-)
drivers/gpio/gpio-104-idio-16.c +41 -17
index 45f7ad8573e1..2f8e295f5541 100644
@@ -19,6 +19,7 @@
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/spinlock.h>
+#include <linux/types.h>
 
 #define IDIO_16_EXTENT 8
 #define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
@@ -32,19 +33,42 @@ static unsigned int irq[MAX_NUM_IDIO_16];
 module_param_hw_array(irq, uint, irq, NULL, 0);
 MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
 
+/**
+ * struct idio_16_reg - device registers structure
+ * @out0_7:	Read: N/A
+ *		Write: FET Drive Outputs 0-7
+ * @in0_7:	Read: Isolated Inputs 0-7
+ *		Write: Clear Interrupt
+ * @irq_ctl:	Read: Enable IRQ
+ *		Write: Disable IRQ
+ * @unused:	N/A
+ * @out8_15:	Read: N/A
+ *		Write: FET Drive Outputs 8-15
+ * @in8_15:	Read: Isolated Inputs 8-15
+ *		Write: N/A
+ */
+struct idio_16_reg {
+	u8 out0_7;
+	u8 in0_7;
+	u8 irq_ctl;
+	u8 unused;
+	u8 out8_15;
+	u8 in8_15;
+};
+
 /**
  * struct idio_16_gpio - GPIO device private data structure
  * @chip:	instance of the gpio_chip
  * @lock:	synchronization lock to prevent I/O race conditions
  * @irq_mask:	I/O bits affected by interrupts
- * @base:	base port address of the GPIO device
+ * @reg:	I/O address offset for the device registers
  * @out_state:	output bits state
  */
 struct idio_16_gpio {
 	struct gpio_chip chip;
 	raw_spinlock_t lock;
 	unsigned long irq_mask;
-	void __iomem *base;
+	struct idio_16_reg __iomem *reg;
 	unsigned int out_state;
 };
 
@@ -79,9 +103,9 @@ static int idio_16_gpio_get(struct gpio_chip *chip, unsigned int offset)
 		return -EINVAL;
 
 	if (offset < 24)
-		return !!(ioread8(idio16gpio->base + 1) & mask);
+		return !!(ioread8(&idio16gpio->reg->in0_7) & mask);
 
-	return !!(ioread8(idio16gpio->base + 5) & (mask>>8));
+	return !!(ioread8(&idio16gpio->reg->in8_15) & (mask>>8));
 }
 
 static int idio_16_gpio_get_multiple(struct gpio_chip *chip,
@@ -91,9 +115,9 @@ static int idio_16_gpio_get_multiple(struct gpio_chip *chip,
 
 	*bits = 0;
 	if (*mask & GENMASK(23, 16))
-		*bits |= (unsigned long)ioread8(idio16gpio->base + 1) << 16;
+		*bits |= (unsigned long)ioread8(&idio16gpio->reg->in0_7) << 16;
 	if (*mask & GENMASK(31, 24))
-		*bits |= (unsigned long)ioread8(idio16gpio->base + 5) << 24;
+		*bits |= (unsigned long)ioread8(&idio16gpio->reg->in8_15) << 24;
 
 	return 0;
 }
@@ -116,9 +140,9 @@ static void idio_16_gpio_set(struct gpio_chip *chip, unsigned int offset,
 		idio16gpio->out_state &= ~mask;
 
 	if (offset > 7)
-		iowrite8(idio16gpio->out_state >> 8, idio16gpio->base + 4);
+		iowrite8(idio16gpio->out_state >> 8, &idio16gpio->reg->out8_15);
 	else
-		iowrite8(idio16gpio->out_state, idio16gpio->base);
+		iowrite8(idio16gpio->out_state, &idio16gpio->reg->out0_7);
 
 	raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
 }
@@ -135,9 +159,9 @@ static void idio_16_gpio_set_multiple(struct gpio_chip *chip,
 	idio16gpio->out_state |= *mask & *bits;
 
 	if (*mask & 0xFF)
-		iowrite8(idio16gpio->out_state, idio16gpio->base);
+		iowrite8(idio16gpio->out_state, &idio16gpio->reg->out0_7);
 	if ((*mask >> 8) & 0xFF)
-		iowrite8(idio16gpio->out_state >> 8, idio16gpio->base + 4);
+		iowrite8(idio16gpio->out_state >> 8, &idio16gpio->reg->out8_15);
 
 	raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
 }
@@ -158,7 +182,7 @@ static void idio_16_irq_mask(struct irq_data *data)
 	if (!idio16gpio->irq_mask) {
 		raw_spin_lock_irqsave(&idio16gpio->lock, flags);
 
-		iowrite8(0, idio16gpio->base + 2);
+		iowrite8(0, &idio16gpio->reg->irq_ctl);
 
 		raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
 	}
@@ -177,7 +201,7 @@ static void idio_16_irq_unmask(struct irq_data *data)
 	if (!prev_irq_mask) {
 		raw_spin_lock_irqsave(&idio16gpio->lock, flags);
 
-		ioread8(idio16gpio->base + 2);
+		ioread8(&idio16gpio->reg->irq_ctl);
 
 		raw_spin_unlock_irqrestore(&idio16gpio->lock, flags);
 	}
@@ -212,7 +236,7 @@ static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
 
 	raw_spin_lock(&idio16gpio->lock);
 
-	iowrite8(0, idio16gpio->base + 1);
+	iowrite8(0, &idio16gpio->reg->in0_7);
 
 	raw_spin_unlock(&idio16gpio->lock);
 
@@ -232,8 +256,8 @@ static int idio_16_irq_init_hw(struct gpio_chip *gc)
 	struct idio_16_gpio *const idio16gpio = gpiochip_get_data(gc);
 
 	/* Disable IRQ by default */
-	iowrite8(0, idio16gpio->base + 2);
-	iowrite8(0, idio16gpio->base + 1);
+	iowrite8(0, &idio16gpio->reg->irq_ctl);
+	iowrite8(0, &idio16gpio->reg->in0_7);
 
 	return 0;
 }
@@ -255,8 +279,8 @@ static int idio_16_probe(struct device *dev, unsigned int id)
 		return -EBUSY;
 	}
 
-	idio16gpio->base = devm_ioport_map(dev, base[id], IDIO_16_EXTENT);
-	if (!idio16gpio->base)
+	idio16gpio->reg = devm_ioport_map(dev, base[id], IDIO_16_EXTENT);
+	if (!idio16gpio->reg)
 		return -ENOMEM;
 
 	idio16gpio->chip.label = name;
Signature · 1 line
2.36.1
#3 lore raw

PATCH v3 3/6 gpio: i8255: Introduce the Intel 8255 interface library module

In reply to William Breathitt Gray

Exposes consumer library functions providing support for interfaces compatible with the venerable Intel 8255 Programmable Peripheral Interface (PPI).

The Intel 8255 PPI first appeared in the early 1970s, initially for the Intel 8080 and later appearing in the original IBM-PC. The popularity of the original Intel 8255 chip led to many subsequent variants and clones of the interface in various chips and integrated circuits. Although still popular, interfaces compatible with the Intel 8255 PPI are nowdays typically found embedded in larger VLSI processing chips and FPGA components rather than as discrete ICs.

A CONFIG_GPIO_I8255 Kconfig option is introduced by this patch. Modules wanting access to these i8255 library functions should select this Kconfig option, and import the I8255 symbol namespace.

---
Changes in v3:
- Updated contact information in MAINTAINERS
- Added help text for GPIO_I8255 Kconfig option
- Move include/linux/gpio/i8255.h to drivers/gpio/gpio-i8255.h
- Include "gpio-i8255.h" instead of <linux/gpio/i8255.h>
- Include linux/types.h instead of linux/compiler_types.h
- Add underscores for *PORTC_LOWER* and *PORTC_UPPER* defines
- Move (offset % 8) expression to the local definition block of i8255_direction_mask() as port_offset const near the io_port definition; this should help optimize assembly instructions on some architectures
- Implement an opaque i8255_state struct to organize and access i8255 device states; this replaces the control_state array passed to various i8255 library functions in previous patchsets
- Implement and provide a i8255_state_init() function to initialize the i8255_state struct for a consumer
- Use a spinlock within i8255 library functions to protect access to i8255 states and synchronize I/O operations; a spinlock is used so that these functions may be used within an interrupt context
- Export the i8255 library symbols within a new I8255 namespace

 MAINTAINERS               |   6 +
 drivers/gpio/Kconfig      |  10 ++
 drivers/gpio/Makefile     |   1 +
 drivers/gpio/gpio-i8255.c | 292 ++++++++++++++++++++++++++++++++++++++
 drivers/gpio/gpio-i8255.h |  47 ++++++
 5 files changed, 356 insertions(+)
 create mode 100644 drivers/gpio/gpio-i8255.c
 create mode 100644 drivers/gpio/gpio-i8255.h
MAINTAINERS +6 -0
index a6d3bd9d2a8d..cca3059639e6 100644
@@ -9799,6 +9799,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/video/fbdev/i810/
 
+INTEL 8255 GPIO DRIVER
+M:	William Breathitt Gray <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/gpio/gpio-i8255.c
+
 INTEL ASoC DRIVERS
 M:	Cezary Rojewski <[email protected]>
 M:	Pierre-Louis Bossart <[email protected]>
drivers/gpio/Kconfig +10 -0
index b01961999ced..4074a41008a8 100644
@@ -829,6 +829,16 @@ endmenu
 menu "Port-mapped I/O GPIO drivers"
 	depends on X86 # Unconditional I/O space access
 
+config GPIO_I8255
+	tristate
+	help
+	  Enables support for the i8255 interface library functions. The i8255
+	  interface library provides functions to facilitate communication with
+	  interfaces compatible with the venerable Intel 8255 Programmable
+	  Peripheral Interface (PPI). The Intel 8255 PPI chip was first released
+	  in the early 1970s but compatible interfaces are nowadays typically
+	  found embedded in larger VLSI processing chips and FPGA components.
+
 config GPIO_104_DIO_48E
 	tristate "ACCES 104-DIO-48E GPIO support"
 	depends on PC104
drivers/gpio/Makefile +1 -0
index 14352f6dfe8e..06057e127949 100644
@@ -67,6 +67,7 @@ obj-$(CONFIG_GPIO_GW_PLD)		+= gpio-gw-pld.o
 obj-$(CONFIG_GPIO_HISI)                 += gpio-hisi.o
 obj-$(CONFIG_GPIO_HLWD)			+= gpio-hlwd.o
 obj-$(CONFIG_HTC_EGPIO)			+= gpio-htc-egpio.o
+obj-$(CONFIG_GPIO_I8255)		+= gpio-i8255.o
 obj-$(CONFIG_GPIO_ICH)			+= gpio-ich.o
 obj-$(CONFIG_GPIO_IDT3243X)		+= gpio-idt3243x.o
 obj-$(CONFIG_GPIO_IOP)			+= gpio-iop.o
drivers/gpio/gpio-i8255.c new file +292 -0

292 lines not on this page · raw message

drivers/gpio/gpio-i8255.h new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.36.1
#5 lore raw

PATCH v3 4/6 gpio: 104-dio-48e: Implement and utilize register structures

In reply to William Breathitt Gray

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.

---
Changes in v3:
- Include "gpio-i8255.h" instead of <linux/gpio/i8255.h>
- Import I8255 symbol namespace
- Define DIO48E_NUM_PPI as the number of PPI (2)
- Utilize i8255_state struct; replaces 'control_state' with 'ppi_state'
- Call i8255_state_init() to initialize 'ppi_state'

 drivers/gpio/Kconfig            |   1 +
 drivers/gpio/gpio-104-dio-48e.c | 248 ++++++++++----------------------
 2 files changed, 75 insertions(+), 174 deletions(-)
drivers/gpio/Kconfig +1 -0
index 4074a41008a8..1d4529c39dc6 100644
@@ -844,6 +844,7 @@ config GPIO_104_DIO_48E
 	depends on PC104
 	select ISA_BUS_API
 	select GPIOLIB_IRQCHIP
+	select GPIO_I8255
 	help
 	  Enables GPIO support for the ACCES 104-DIO-48E series (104-DIO-48E,
 	  104-DIO-24E). The base port addresses for the devices may be
drivers/gpio/gpio-104-dio-48e.c +74 -174

361 lines not on this page · raw message

Signature · 1 line
2.36.1
#6 lore raw

PATCH v3 5/6 gpio: 104-idi-48: Implement and utilize register structures

In reply to William Breathitt Gray

Reduce magic numbers and improve code readability by implementing and utilizing named register data structures. The 104-IDI-48 device features an Intel 8255 compatible GPIO interface, so the i8255 GPIO module is selected and utilized as well.

---
Changes in v3:
- Include "gpio-i8255.h" instead of <linux/gpio/i8255.h>
- Import I8255 symbol namespace
- Consolidate 'lock' and 'ack_lock' into a single spinlock 'lock' synchronizing I/O as well as protecting irq_mask states

 drivers/gpio/Kconfig           |   1 +
 drivers/gpio/gpio-104-idi-48.c | 141 ++++++++++++++-------------------
 2 files changed, 60 insertions(+), 82 deletions(-)
drivers/gpio/Kconfig +1 -0
index 1d4529c39dc6..253a03ee3fa4 100644
@@ -868,6 +868,7 @@ config GPIO_104_IDI_48
 	depends on PC104
 	select ISA_BUS_API
 	select GPIOLIB_IRQCHIP
+	select GPIO_I8255
 	help
 	  Enables GPIO support for the ACCES 104-IDI-48 family (104-IDI-48A,
 	  104-IDI-48AC, 104-IDI-48B, 104-IDI-48BC). The base port addresses for
drivers/gpio/gpio-104-idi-48.c +59 -82

245 lines not on this page · raw message

Signature · 1 line
2.36.1
#7 lore raw

PATCH v3 6/6 gpio: gpio-mm: Implement and utilize register structures

In reply to William Breathitt Gray

Reduce magic numbers and improve code readability by implementing and utilizing named register data structures. The GPIO-MM device features an Intel 8255 compatible GPIO interface, so the i8255 GPIO module is selected and utilized as well.

---
Changes in v3:
- Include "gpio-i8255.h" instead of <linux/gpio/i8255.h>
- Import I8255 symbol namespace
- Define GPIOMM_NUM_PPI as the number of PPI (2)
- Utilize i8255_state struct; replaces 'control_state' and 'lock' with 'ppi_state'
- Call i8255_state_init() to initialize 'ppi_state'

 drivers/gpio/Kconfig        |   1 +
 drivers/gpio/gpio-gpio-mm.c | 202 +++++++-----------------------------
 2 files changed, 41 insertions(+), 162 deletions(-)
drivers/gpio/Kconfig +1 -0
index 253a03ee3fa4..5202e3b6f2c6 100644
@@ -889,6 +889,7 @@ config GPIO_GPIO_MM
 	tristate "Diamond Systems GPIO-MM GPIO support"
 	depends on PC104
 	select ISA_BUS_API
+	select GPIO_I8255
 	help
 	  Enables GPIO support for the Diamond Systems GPIO-MM and GPIO-MM-12.
 
drivers/gpio/gpio-gpio-mm.c +40 -162

298 lines not on this page · raw message

Signature · 1 line
2.36.1
#4 lore raw

Re: [PATCH v3 3/6] gpio: i8255: Introduce the Intel 8255 interface library module

In reply to William Breathitt Gray
Quoted 13 lines · William Breathitt Gray

Exposes consumer library functions providing support for interfaces compatible with the venerable Intel 8255 Programmable Peripheral Interface (PPI).

The Intel 8255 PPI first appeared in the early 1970s, initially for the Intel 8080 and later appearing in the original IBM-PC. The popularity of the original Intel 8255 chip led to many subsequent variants and clones of the interface in various chips and integrated circuits. Although still popular, interfaces compatible with the Intel 8255 PPI are nowdays typically found embedded in larger VLSI processing chips and FPGA components rather than as discrete ICs.

Quoted 3 lines

A CONFIG_GPIO_I8255 Kconfig option is introduced by this patch. Modules wanting access to these i8255 library functions should select this Kconfig option, and import the I8255 symbol namespace.

Thanks for an update, my comments below.

...

drivers/gpio/Kconfig endmenu · line 840

Quoted 9 lines
drivers/gpio/Kconfig +9 -0
+config GPIO_I8255
+       tristate
+       help
+         Enables support for the i8255 interface library functions. The i8255
+         interface library provides functions to facilitate communication with
+         interfaces compatible with the venerable Intel 8255 Programmable
+         Peripheral Interface (PPI). The Intel 8255 PPI chip was first released
+         in the early 1970s but compatible interfaces are nowadays typically
+         found embedded in larger VLSI processing chips and FPGA components.

+ "If built as a module its name will be ..." or similar sentence would be good to add.

...

drivers/gpio/gpio-i8255.c line 47

Quoted 3 lines
drivers/gpio/gpio-i8255.c new file +3 -0
+       case I8255_PORTC:
+               /* Port C can be configured by nibble */
+               if (port_offset > 3)

More naturally looks >= 4 to show the beginning offset number for the UPPER.

Quoted 2 lines
drivers/gpio/gpio-i8255.c new file +2 -0
+                       return I8255_CONTROL_PORTC_UPPER_DIRECTION;
+               return I8255_CONTROL_PORTC_LOWER_DIRECTION;

...

drivers/gpio/gpio-i8255.c line 71

Quoted 3 lines
drivers/gpio/gpio-i8255.c new file +3 -0
+       out_state = ioread8(&ppi[group].port[ppi_port]);
+       out_state &= ~io_mask;
+       out_state |= bit_mask;

Usual pattern is

  out_state = (out_state & ~mask) | (bits & mask);

(and we call them mask and value or bits)

Quoted 1 line

+

No need for this blank line.

Quoted 1 line

+ iowrite8(out_state, &ppi[group].port[ppi_port]);

...

drivers/gpio/gpio-i8255.c line 266

Quoted 2 lines
drivers/gpio/gpio-i8255.c new file +2 -0
+               bit_mask = bitmap_get_value8(bits, offset) & gpio_mask;
+               io_port = offset / 8;

Exactly why I recommended reconsidering the above pattern, you won't need to do ' & mask' in the caller(s).

Quoted 1 line

+ i8255_set_port(ppi, state, io_port, gpio_mask, bit_mask);

...

Quoted 2 lines

+ * Initializes the @state of each Intel 8255 Programmable Peripheral Interface
+ * group for use in i8255 library functions.

I'm not sure about terminology. What's 'group'? We have a very well established term 'bank' isn't it what you meant here by 'group'?

...

Quoted 1 line

+int i8255_get(const struct i8255 __iomem *ppi, unsigned long offset);

I'm not sure what const with __iomem gives us? The purpose of that is?. And if it's about the content of the register, then const is a lie.

Ditto for the rest of the similar cases.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
In reply to William Breathitt Gray
Quoted 40 lines · William Breathitt Gray

Changes in v3:
- Updated contact information in MAINTAINERS
- Added help text for GPIO_I8255 Kconfig option
- Move include/linux/gpio/i8255.h to drivers/gpio/gpio-i8255.h
- Include "gpio-i8255.h" instead of <linux/gpio/i8255.h>
- Include linux/types.h instead of linux/compiler_types.h
- Add underscores for *PORTC_LOWER* and *PORTC_UPPER* defines
- Move (offset % 8) expression to a port_offset const above the io_port const in i8255_direction_mask(); this should help optimize assembly instructions on some architectures
- Implement an opaque i8255_state struct to organize and access i8255 device states; this replaces the control_state array passed to various i8255 library functions in previous patchsets
- Implement and provide a i8255_state_init() function to initialize the i8255_state struct for a consumer
- Use a spinlock within i8255 library functions to protect access to i8255 states and synchronize I/O operations; a spinlock is used so that these functions may be used within an interrupt context
- Export the i8255 library symbols within a new I8255 namespace
- Update the 104-dio-48e, 104-idi-48, gpio-mm drivers to use the new i8255_state struct and I8255 namespace

The PC104/ISA drivers were updated to use I/O memory accessor calls such as ioread8()/iowrite8() in a previous patch series [0]. This patchset is a continuation of the effort to improve the code readability and reduce magic numbers by implementing and utilizing named register data structures.

One of the benefits is that we can now observe more easily similarities in devices that share similar interfaces; such as the i8255 interfaces used by the 104-DIO-48E, 104-IDI-48, and GPIO-MM drivers -- as well as the similar interface used by the 104-IDIO-16 and PCI-IDIO-16 drivers.

A new module supporting the Intel 8255 interface is introduced to consolidate the common code found among the 104-DIO-48E, 104-IDI-48, and GPIO-MM drivers.

[0] https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

William Breathitt Gray (6):
gpio: ws16c48: Implement and utilize register structures
gpio: 104-idio-16: Implement and utilize register structures
gpio: i8255: Introduce the Intel 8255 interface library module
gpio: 104-dio-48e: Implement and utilize register structures
gpio: 104-idi-48: Implement and utilize register structures
gpio: gpio-mm: Implement and utilize register structures

Hey William!

Are you planning to submit a fourth version anytime soon? I am willing to take it for the next merge window if it arrives soon - like tomorrow at the latest.

Bart

#10 lore raw
Quoted 51 lines · Bartosz Golaszewski
Quoted 40 lines · William Breathitt Gray

Changes in v3:
- Updated contact information in MAINTAINERS
- Added help text for GPIO_I8255 Kconfig option
- Move include/linux/gpio/i8255.h to drivers/gpio/gpio-i8255.h
- Include "gpio-i8255.h" instead of <linux/gpio/i8255.h>
- Include linux/types.h instead of linux/compiler_types.h
- Add underscores for *PORTC_LOWER* and *PORTC_UPPER* defines
- Move (offset % 8) expression to a port_offset const above the io_port const in i8255_direction_mask(); this should help optimize assembly instructions on some architectures
- Implement an opaque i8255_state struct to organize and access i8255 device states; this replaces the control_state array passed to various i8255 library functions in previous patchsets
- Implement and provide a i8255_state_init() function to initialize the i8255_state struct for a consumer
- Use a spinlock within i8255 library functions to protect access to i8255 states and synchronize I/O operations; a spinlock is used so that these functions may be used within an interrupt context
- Export the i8255 library symbols within a new I8255 namespace
- Update the 104-dio-48e, 104-idi-48, gpio-mm drivers to use the new i8255_state struct and I8255 namespace

The PC104/ISA drivers were updated to use I/O memory accessor calls such as ioread8()/iowrite8() in a previous patch series [0]. This patchset is a continuation of the effort to improve the code readability and reduce magic numbers by implementing and utilizing named register data structures.

One of the benefits is that we can now observe more easily similarities in devices that share similar interfaces; such as the i8255 interfaces used by the 104-DIO-48E, 104-IDI-48, and GPIO-MM drivers -- as well as the similar interface used by the 104-IDIO-16 and PCI-IDIO-16 drivers.

A new module supporting the Intel 8255 interface is introduced to consolidate the common code found among the 104-DIO-48E, 104-IDI-48, and GPIO-MM drivers.

[0] https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

William Breathitt Gray (6):
gpio: ws16c48: Implement and utilize register structures
gpio: 104-idio-16: Implement and utilize register structures
gpio: i8255: Introduce the Intel 8255 interface library module
gpio: 104-dio-48e: Implement and utilize register structures
gpio: 104-idi-48: Implement and utilize register structures
gpio: gpio-mm: Implement and utilize register structures

Quoted 7 lines

Hey William!

Are you planning to submit a fourth version anytime soon? I am willing to take it for the next merge window if it arrives soon - like tomorrow at the latest.

Bart

Hi Bart,

I'm incorporating Andy's comments for v4, and I expect it should be ready later today for you to pick up.

Thanks,

William Breathitt Gray