1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * GPIO driver for the WinSystems WS16C48 4 * Copyright (C) 2016 William Breathitt Gray 5 */ 6 #include <linux/bitmap.h> 7 #include <linux/device.h> 8 #include <linux/errno.h> 9 #include <linux/gpio/driver.h> 10 #include <linux/io.h> 11 #include <linux/ioport.h> 12 #include <linux/interrupt.h> 13 #include <linux/irqdesc.h> 14 #include <linux/isa.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/moduleparam.h> 18 #include <linux/spinlock.h> 19 #include <linux/types.h> 20 21 #define WS16C48_EXTENT 10 22 #define MAX_NUM_WS16C48 max_num_isa_dev(WS16C48_EXTENT) 23 24 static unsigned int base[MAX_NUM_WS16C48]; 25 static unsigned int num_ws16c48; 26 module_param_hw_array(base, uint, ioport, &num_ws16c48, 0); 27 MODULE_PARM_DESC(base, "WinSystems WS16C48 base addresses"); 28 29 static unsigned int irq[MAX_NUM_WS16C48]; 30 module_param_hw_array(irq, uint, irq, NULL, 0); 31 MODULE_PARM_DESC(irq, "WinSystems WS16C48 interrupt line numbers"); 32 33 /** 34 * struct ws16c48_reg - device register structure 35 * @port: Port 0 through 5 I/O 36 * @int_pending: Interrupt Pending 37 * @page_lock: Register page (Bits 7-6) and I/O port lock (Bits 5-0) 38 * @pol_enab_int_id: Interrupt polarity, enable, and ID 39 */ 40 struct ws16c48_reg { 41 u8 port[6]; 42 u8 int_pending; 43 u8 page_lock; 44 u8 pol_enab_int_id[3]; 45 }; 46 47 /** 48 * struct ws16c48_gpio - GPIO device private data structure 49 * @chip: instance of the gpio_chip 50 * @io_state: bit I/O state (whether bit is set to input or output) 51 * @out_state: output bits state 52 * @lock: synchronization lock to prevent I/O race conditions 53 * @irq_mask: I/O bits affected by interrupts 54 * @flow_mask: IRQ flow type mask for the respective I/O bits 55 * @reg: I/O address offset for the device registers 56 */ 57 struct ws16c48_gpio { 58 struct gpio_chip chip; 59 unsigned char io_state[6]; 60 unsigned char out_state[6]; 61 raw_spinlock_t lock; 62 unsigned long irq_mask; 63 unsigned long flow_mask; 64 struct ws16c48_reg __iomem *reg; 65 }; 66 67 static int ws16c48_gpio_get_direction(struct gpio_chip *chip, unsigned offset) 68 { 69 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 70 const unsigned port = offset / 8; 71 const unsigned mask = BIT(offset % 8); 72 73 if (ws16c48gpio->io_state[port] & mask) 74 return GPIO_LINE_DIRECTION_IN; 75 76 return GPIO_LINE_DIRECTION_OUT; 77 } 78 79 static int ws16c48_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 80 { 81 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 82 const unsigned port = offset / 8; 83 const unsigned mask = BIT(offset % 8); 84 unsigned long flags; 85 86 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 87 88 ws16c48gpio->io_state[port] |= mask; 89 ws16c48gpio->out_state[port] &= ~mask; 90 iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->reg->port + port); 91 92 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 93 94 return 0; 95 } 96 97 static int ws16c48_gpio_direction_output(struct gpio_chip *chip, 98 unsigned offset, int value) 99 { 100 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 101 const unsigned port = offset / 8; 102 const unsigned mask = BIT(offset % 8); 103 unsigned long flags; 104 105 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 106 107 ws16c48gpio->io_state[port] &= ~mask; 108 if (value) 109 ws16c48gpio->out_state[port] |= mask; 110 else 111 ws16c48gpio->out_state[port] &= ~mask; 112 iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->reg->port + port); 113 114 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 115 116 return 0; 117 } 118 119 static int ws16c48_gpio_get(struct gpio_chip *chip, unsigned offset) 120 { 121 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 122 const unsigned port = offset / 8; 123 const unsigned mask = BIT(offset % 8); 124 unsigned long flags; 125 unsigned port_state; 126 127 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 128 129 /* ensure that GPIO is set for input */ 130 if (!(ws16c48gpio->io_state[port] & mask)) { 131 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 132 return -EINVAL; 133 } 134 135 port_state = ioread8(ws16c48gpio->reg->port + port); 136 137 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 138 139 return !!(port_state & mask); 140 } 141 142 static int ws16c48_gpio_get_multiple(struct gpio_chip *chip, 143 unsigned long *mask, unsigned long *bits) 144 { 145 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 146 unsigned long offset; 147 unsigned long gpio_mask; 148 size_t index; 149 u8 __iomem *port_addr; 150 unsigned long port_state; 151 152 /* clear bits array to a clean slate */ 153 bitmap_zero(bits, chip->ngpio); 154 155 for_each_set_clump8(offset, gpio_mask, mask, chip->ngpio) { 156 index = offset / 8; 157 port_addr = ws16c48gpio->reg->port + index; 158 port_state = ioread8(port_addr) & gpio_mask; 159 160 bitmap_set_value8(bits, port_state, offset); 161 } 162 163 return 0; 164 } 165 166 static void ws16c48_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 167 { 168 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 169 const unsigned port = offset / 8; 170 const unsigned mask = BIT(offset % 8); 171 unsigned long flags; 172 173 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 174 175 /* ensure that GPIO is set for output */ 176 if (ws16c48gpio->io_state[port] & mask) { 177 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 178 return; 179 } 180 181 if (value) 182 ws16c48gpio->out_state[port] |= mask; 183 else 184 ws16c48gpio->out_state[port] &= ~mask; 185 iowrite8(ws16c48gpio->out_state[port], ws16c48gpio->reg->port + port); 186 187 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 188 } 189 190 static void ws16c48_gpio_set_multiple(struct gpio_chip *chip, 191 unsigned long *mask, unsigned long *bits) 192 { 193 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 194 unsigned long offset; 195 unsigned long gpio_mask; 196 size_t index; 197 u8 __iomem *port_addr; 198 unsigned long bitmask; 199 unsigned long flags; 200 201 for_each_set_clump8(offset, gpio_mask, mask, chip->ngpio) { 202 index = offset / 8; 203 port_addr = ws16c48gpio->reg->port + index; 204 205 /* mask out GPIO configured for input */ 206 gpio_mask &= ~ws16c48gpio->io_state[index]; 207 bitmask = bitmap_get_value8(bits, offset) & gpio_mask; 208 209 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 210 211 /* update output state data and set device gpio register */ 212 ws16c48gpio->out_state[index] &= ~gpio_mask; 213 ws16c48gpio->out_state[index] |= bitmask; 214 iowrite8(ws16c48gpio->out_state[index], port_addr); 215 216 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 217 } 218 } 219 220 static void ws16c48_irq_ack(struct irq_data *data) 221 { 222 struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 223 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 224 const unsigned long offset = irqd_to_hwirq(data); 225 const unsigned port = offset / 8; 226 const unsigned mask = BIT(offset % 8); 227 unsigned long flags; 228 unsigned port_state; 229 230 /* only the first 3 ports support interrupts */ 231 if (port > 2) 232 return; 233 234 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 235 236 port_state = ws16c48gpio->irq_mask >> (8*port); 237 238 /* Select Register Page 2; Unlock all I/O ports */ 239 iowrite8(0x80, &ws16c48gpio->reg->page_lock); 240 241 /* Clear pending interrupt */ 242 iowrite8(port_state & ~mask, ws16c48gpio->reg->pol_enab_int_id + port); 243 iowrite8(port_state | mask, ws16c48gpio->reg->pol_enab_int_id + port); 244 245 /* Select Register Page 3; Unlock all I/O ports */ 246 iowrite8(0xC0, &ws16c48gpio->reg->page_lock); 247 248 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 249 } 250 251 static void ws16c48_irq_mask(struct irq_data *data) 252 { 253 struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 254 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 255 const unsigned long offset = irqd_to_hwirq(data); 256 const unsigned long mask = BIT(offset); 257 const unsigned port = offset / 8; 258 unsigned long flags; 259 unsigned long port_state; 260 261 /* only the first 3 ports support interrupts */ 262 if (port > 2) 263 return; 264 265 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 266 267 ws16c48gpio->irq_mask &= ~mask; 268 gpiochip_disable_irq(chip, offset); 269 port_state = ws16c48gpio->irq_mask >> (8 * port); 270 271 /* Select Register Page 2; Unlock all I/O ports */ 272 iowrite8(0x80, &ws16c48gpio->reg->page_lock); 273 274 /* Disable interrupt */ 275 iowrite8(port_state, ws16c48gpio->reg->pol_enab_int_id + port); 276 277 /* Select Register Page 3; Unlock all I/O ports */ 278 iowrite8(0xC0, &ws16c48gpio->reg->page_lock); 279 280 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 281 } 282 283 static void ws16c48_irq_unmask(struct irq_data *data) 284 { 285 struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 286 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 287 const unsigned long offset = irqd_to_hwirq(data); 288 const unsigned long mask = BIT(offset); 289 const unsigned port = offset / 8; 290 unsigned long flags; 291 unsigned long port_state; 292 293 /* only the first 3 ports support interrupts */ 294 if (port > 2) 295 return; 296 297 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 298 299 gpiochip_enable_irq(chip, offset); 300 ws16c48gpio->irq_mask |= mask; 301 port_state = ws16c48gpio->irq_mask >> (8 * port); 302 303 /* Select Register Page 2; Unlock all I/O ports */ 304 iowrite8(0x80, &ws16c48gpio->reg->page_lock); 305 306 /* Enable interrupt */ 307 iowrite8(port_state, ws16c48gpio->reg->pol_enab_int_id + port); 308 309 /* Select Register Page 3; Unlock all I/O ports */ 310 iowrite8(0xC0, &ws16c48gpio->reg->page_lock); 311 312 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 313 } 314 315 static int ws16c48_irq_set_type(struct irq_data *data, unsigned flow_type) 316 { 317 struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 318 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(chip); 319 const unsigned long offset = irqd_to_hwirq(data); 320 const unsigned long mask = BIT(offset); 321 const unsigned port = offset / 8; 322 unsigned long flags; 323 unsigned long port_state; 324 325 /* only the first 3 ports support interrupts */ 326 if (port > 2) 327 return -EINVAL; 328 329 raw_spin_lock_irqsave(&ws16c48gpio->lock, flags); 330 331 switch (flow_type) { 332 case IRQ_TYPE_NONE: 333 break; 334 case IRQ_TYPE_EDGE_RISING: 335 ws16c48gpio->flow_mask |= mask; 336 break; 337 case IRQ_TYPE_EDGE_FALLING: 338 ws16c48gpio->flow_mask &= ~mask; 339 break; 340 default: 341 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 342 return -EINVAL; 343 } 344 345 port_state = ws16c48gpio->flow_mask >> (8 * port); 346 347 /* Select Register Page 1; Unlock all I/O ports */ 348 iowrite8(0x40, &ws16c48gpio->reg->page_lock); 349 350 /* Set interrupt polarity */ 351 iowrite8(port_state, ws16c48gpio->reg->pol_enab_int_id + port); 352 353 /* Select Register Page 3; Unlock all I/O ports */ 354 iowrite8(0xC0, &ws16c48gpio->reg->page_lock); 355 356 raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags); 357 358 return 0; 359 } 360 361 static const struct irq_chip ws16c48_irqchip = { 362 .name = "ws16c48", 363 .irq_ack = ws16c48_irq_ack, 364 .irq_mask = ws16c48_irq_mask, 365 .irq_unmask = ws16c48_irq_unmask, 366 .irq_set_type = ws16c48_irq_set_type, 367 .flags = IRQCHIP_IMMUTABLE, 368 GPIOCHIP_IRQ_RESOURCE_HELPERS, 369 }; 370 371 static irqreturn_t ws16c48_irq_handler(int irq, void *dev_id) 372 { 373 struct ws16c48_gpio *const ws16c48gpio = dev_id; 374 struct gpio_chip *const chip = &ws16c48gpio->chip; 375 struct ws16c48_reg __iomem *const reg = ws16c48gpio->reg; 376 unsigned long int_pending; 377 unsigned long port; 378 unsigned long int_id; 379 unsigned long gpio; 380 381 int_pending = ioread8(®->int_pending) & 0x7; 382 if (!int_pending) 383 return IRQ_NONE; 384 385 /* loop until all pending interrupts are handled */ 386 do { 387 for_each_set_bit(port, &int_pending, 3) { 388 int_id = ioread8(reg->pol_enab_int_id + port); 389 for_each_set_bit(gpio, &int_id, 8) 390 generic_handle_domain_irq(chip->irq.domain, 391 gpio + 8*port); 392 } 393 394 int_pending = ioread8(®->int_pending) & 0x7; 395 } while (int_pending); 396 397 return IRQ_HANDLED; 398 } 399 400 #define WS16C48_NGPIO 48 401 static const char *ws16c48_names[WS16C48_NGPIO] = { 402 "Port 0 Bit 0", "Port 0 Bit 1", "Port 0 Bit 2", "Port 0 Bit 3", 403 "Port 0 Bit 4", "Port 0 Bit 5", "Port 0 Bit 6", "Port 0 Bit 7", 404 "Port 1 Bit 0", "Port 1 Bit 1", "Port 1 Bit 2", "Port 1 Bit 3", 405 "Port 1 Bit 4", "Port 1 Bit 5", "Port 1 Bit 6", "Port 1 Bit 7", 406 "Port 2 Bit 0", "Port 2 Bit 1", "Port 2 Bit 2", "Port 2 Bit 3", 407 "Port 2 Bit 4", "Port 2 Bit 5", "Port 2 Bit 6", "Port 2 Bit 7", 408 "Port 3 Bit 0", "Port 3 Bit 1", "Port 3 Bit 2", "Port 3 Bit 3", 409 "Port 3 Bit 4", "Port 3 Bit 5", "Port 3 Bit 6", "Port 3 Bit 7", 410 "Port 4 Bit 0", "Port 4 Bit 1", "Port 4 Bit 2", "Port 4 Bit 3", 411 "Port 4 Bit 4", "Port 4 Bit 5", "Port 4 Bit 6", "Port 4 Bit 7", 412 "Port 5 Bit 0", "Port 5 Bit 1", "Port 5 Bit 2", "Port 5 Bit 3", 413 "Port 5 Bit 4", "Port 5 Bit 5", "Port 5 Bit 6", "Port 5 Bit 7" 414 }; 415 416 static int ws16c48_irq_init_hw(struct gpio_chip *gc) 417 { 418 struct ws16c48_gpio *const ws16c48gpio = gpiochip_get_data(gc); 419 420 /* Select Register Page 2; Unlock all I/O ports */ 421 iowrite8(0x80, &ws16c48gpio->reg->page_lock); 422 423 /* Disable interrupts for all lines */ 424 iowrite8(0, &ws16c48gpio->reg->pol_enab_int_id[0]); 425 iowrite8(0, &ws16c48gpio->reg->pol_enab_int_id[1]); 426 iowrite8(0, &ws16c48gpio->reg->pol_enab_int_id[2]); 427 428 /* Select Register Page 3; Unlock all I/O ports */ 429 iowrite8(0xC0, &ws16c48gpio->reg->page_lock); 430 431 return 0; 432 } 433 434 static int ws16c48_probe(struct device *dev, unsigned int id) 435 { 436 struct ws16c48_gpio *ws16c48gpio; 437 const char *const name = dev_name(dev); 438 struct gpio_irq_chip *girq; 439 int err; 440 441 ws16c48gpio = devm_kzalloc(dev, sizeof(*ws16c48gpio), GFP_KERNEL); 442 if (!ws16c48gpio) 443 return -ENOMEM; 444 445 if (!devm_request_region(dev, base[id], WS16C48_EXTENT, name)) { 446 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n", 447 base[id], base[id] + WS16C48_EXTENT); 448 return -EBUSY; 449 } 450 451 ws16c48gpio->reg = devm_ioport_map(dev, base[id], WS16C48_EXTENT); 452 if (!ws16c48gpio->reg) 453 return -ENOMEM; 454 455 ws16c48gpio->chip.label = name; 456 ws16c48gpio->chip.parent = dev; 457 ws16c48gpio->chip.owner = THIS_MODULE; 458 ws16c48gpio->chip.base = -1; 459 ws16c48gpio->chip.ngpio = WS16C48_NGPIO; 460 ws16c48gpio->chip.names = ws16c48_names; 461 ws16c48gpio->chip.get_direction = ws16c48_gpio_get_direction; 462 ws16c48gpio->chip.direction_input = ws16c48_gpio_direction_input; 463 ws16c48gpio->chip.direction_output = ws16c48_gpio_direction_output; 464 ws16c48gpio->chip.get = ws16c48_gpio_get; 465 ws16c48gpio->chip.get_multiple = ws16c48_gpio_get_multiple; 466 ws16c48gpio->chip.set = ws16c48_gpio_set; 467 ws16c48gpio->chip.set_multiple = ws16c48_gpio_set_multiple; 468 469 girq = &ws16c48gpio->chip.irq; 470 gpio_irq_chip_set_chip(girq, &ws16c48_irqchip); 471 /* This will let us handle the parent IRQ in the driver */ 472 girq->parent_handler = NULL; 473 girq->num_parents = 0; 474 girq->parents = NULL; 475 girq->default_type = IRQ_TYPE_NONE; 476 girq->handler = handle_edge_irq; 477 girq->init_hw = ws16c48_irq_init_hw; 478 479 raw_spin_lock_init(&ws16c48gpio->lock); 480 481 err = devm_gpiochip_add_data(dev, &ws16c48gpio->chip, ws16c48gpio); 482 if (err) { 483 dev_err(dev, "GPIO registering failed (%d)\n", err); 484 return err; 485 } 486 487 err = devm_request_irq(dev, irq[id], ws16c48_irq_handler, IRQF_SHARED, 488 name, ws16c48gpio); 489 if (err) { 490 dev_err(dev, "IRQ handler registering failed (%d)\n", err); 491 return err; 492 } 493 494 return 0; 495 } 496 497 static struct isa_driver ws16c48_driver = { 498 .probe = ws16c48_probe, 499 .driver = { 500 .name = "ws16c48" 501 }, 502 }; 503 504 module_isa_driver(ws16c48_driver, num_ws16c48); 505 506 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); 507 MODULE_DESCRIPTION("WinSystems WS16C48 GPIO driver"); 508 MODULE_LICENSE("GPL v2"); 509