1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011-2012 Avionic Design GmbH 4 */ 5 6 #include <linux/gpio/driver.h> 7 #include <linux/i2c.h> 8 #include <linux/interrupt.h> 9 #include <linux/mod_devicetable.h> 10 #include <linux/module.h> 11 #include <linux/property.h> 12 #include <linux/seq_file.h> 13 #include <linux/slab.h> 14 15 #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift) 16 #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift) 17 #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift) 18 #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift) 19 #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift) 20 21 struct adnp { 22 struct i2c_client *client; 23 struct gpio_chip gpio; 24 unsigned int reg_shift; 25 26 struct mutex i2c_lock; 27 struct mutex irq_lock; 28 29 u8 *irq_enable; 30 u8 *irq_level; 31 u8 *irq_rise; 32 u8 *irq_fall; 33 u8 *irq_high; 34 u8 *irq_low; 35 }; 36 37 static int adnp_read(struct adnp *adnp, unsigned offset, uint8_t *value) 38 { 39 int err; 40 41 err = i2c_smbus_read_byte_data(adnp->client, offset); 42 if (err < 0) { 43 dev_err(adnp->gpio.parent, "%s failed: %d\n", 44 "i2c_smbus_read_byte_data()", err); 45 return err; 46 } 47 48 *value = err; 49 return 0; 50 } 51 52 static int adnp_write(struct adnp *adnp, unsigned offset, uint8_t value) 53 { 54 int err; 55 56 err = i2c_smbus_write_byte_data(adnp->client, offset, value); 57 if (err < 0) { 58 dev_err(adnp->gpio.parent, "%s failed: %d\n", 59 "i2c_smbus_write_byte_data()", err); 60 return err; 61 } 62 63 return 0; 64 } 65 66 static int adnp_gpio_get(struct gpio_chip *chip, unsigned offset) 67 { 68 struct adnp *adnp = gpiochip_get_data(chip); 69 unsigned int reg = offset >> adnp->reg_shift; 70 unsigned int pos = offset & 7; 71 u8 value; 72 int err; 73 74 err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &value); 75 if (err < 0) 76 return err; 77 78 return (value & BIT(pos)) ? 1 : 0; 79 } 80 81 static void __adnp_gpio_set(struct adnp *adnp, unsigned offset, int value) 82 { 83 unsigned int reg = offset >> adnp->reg_shift; 84 unsigned int pos = offset & 7; 85 int err; 86 u8 val; 87 88 err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &val); 89 if (err < 0) 90 return; 91 92 if (value) 93 val |= BIT(pos); 94 else 95 val &= ~BIT(pos); 96 97 adnp_write(adnp, GPIO_PLR(adnp) + reg, val); 98 } 99 100 static void adnp_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 101 { 102 struct adnp *adnp = gpiochip_get_data(chip); 103 104 mutex_lock(&adnp->i2c_lock); 105 __adnp_gpio_set(adnp, offset, value); 106 mutex_unlock(&adnp->i2c_lock); 107 } 108 109 static int adnp_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 110 { 111 struct adnp *adnp = gpiochip_get_data(chip); 112 unsigned int reg = offset >> adnp->reg_shift; 113 unsigned int pos = offset & 7; 114 u8 value; 115 int err; 116 117 mutex_lock(&adnp->i2c_lock); 118 119 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value); 120 if (err < 0) 121 goto out; 122 123 value &= ~BIT(pos); 124 125 err = adnp_write(adnp, GPIO_DDR(adnp) + reg, value); 126 if (err < 0) 127 goto out; 128 129 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value); 130 if (err < 0) 131 goto out; 132 133 if (value & BIT(pos)) { 134 err = -EPERM; 135 goto out; 136 } 137 138 err = 0; 139 140 out: 141 mutex_unlock(&adnp->i2c_lock); 142 return err; 143 } 144 145 static int adnp_gpio_direction_output(struct gpio_chip *chip, unsigned offset, 146 int value) 147 { 148 struct adnp *adnp = gpiochip_get_data(chip); 149 unsigned int reg = offset >> adnp->reg_shift; 150 unsigned int pos = offset & 7; 151 int err; 152 u8 val; 153 154 mutex_lock(&adnp->i2c_lock); 155 156 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val); 157 if (err < 0) 158 goto out; 159 160 val |= BIT(pos); 161 162 err = adnp_write(adnp, GPIO_DDR(adnp) + reg, val); 163 if (err < 0) 164 goto out; 165 166 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val); 167 if (err < 0) 168 goto out; 169 170 if (!(val & BIT(pos))) { 171 err = -EPERM; 172 goto out; 173 } 174 175 __adnp_gpio_set(adnp, offset, value); 176 err = 0; 177 178 out: 179 mutex_unlock(&adnp->i2c_lock); 180 return err; 181 } 182 183 static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) 184 { 185 struct adnp *adnp = gpiochip_get_data(chip); 186 unsigned int num_regs = 1 << adnp->reg_shift, i, j; 187 int err; 188 189 for (i = 0; i < num_regs; i++) { 190 u8 ddr, plr, ier, isr; 191 192 mutex_lock(&adnp->i2c_lock); 193 194 err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr); 195 if (err < 0) 196 goto unlock; 197 198 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr); 199 if (err < 0) 200 goto unlock; 201 202 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier); 203 if (err < 0) 204 goto unlock; 205 206 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr); 207 if (err < 0) 208 goto unlock; 209 210 mutex_unlock(&adnp->i2c_lock); 211 212 for (j = 0; j < 8; j++) { 213 unsigned int bit = (i << adnp->reg_shift) + j; 214 const char *direction = "input "; 215 const char *level = "low "; 216 const char *interrupt = "disabled"; 217 const char *pending = ""; 218 219 if (ddr & BIT(j)) 220 direction = "output"; 221 222 if (plr & BIT(j)) 223 level = "high"; 224 225 if (ier & BIT(j)) 226 interrupt = "enabled "; 227 228 if (isr & BIT(j)) 229 pending = "pending"; 230 231 seq_printf(s, "%2u: %s %s IRQ %s %s\n", bit, 232 direction, level, interrupt, pending); 233 } 234 } 235 236 return; 237 238 unlock: 239 mutex_unlock(&adnp->i2c_lock); 240 } 241 242 static irqreturn_t adnp_irq(int irq, void *data) 243 { 244 struct adnp *adnp = data; 245 unsigned int num_regs, i; 246 247 num_regs = 1 << adnp->reg_shift; 248 249 for (i = 0; i < num_regs; i++) { 250 unsigned int base = i << adnp->reg_shift, bit; 251 u8 changed, level, isr, ier; 252 unsigned long pending; 253 int err; 254 255 mutex_lock(&adnp->i2c_lock); 256 257 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &level); 258 if (err < 0) { 259 mutex_unlock(&adnp->i2c_lock); 260 continue; 261 } 262 263 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr); 264 if (err < 0) { 265 mutex_unlock(&adnp->i2c_lock); 266 continue; 267 } 268 269 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier); 270 if (err < 0) { 271 mutex_unlock(&adnp->i2c_lock); 272 continue; 273 } 274 275 mutex_unlock(&adnp->i2c_lock); 276 277 /* determine pins that changed levels */ 278 changed = level ^ adnp->irq_level[i]; 279 280 /* compute edge-triggered interrupts */ 281 pending = changed & ((adnp->irq_fall[i] & ~level) | 282 (adnp->irq_rise[i] & level)); 283 284 /* add in level-triggered interrupts */ 285 pending |= (adnp->irq_high[i] & level) | 286 (adnp->irq_low[i] & ~level); 287 288 /* mask out non-pending and disabled interrupts */ 289 pending &= isr & ier; 290 291 for_each_set_bit(bit, &pending, 8) { 292 unsigned int child_irq; 293 child_irq = irq_find_mapping(adnp->gpio.irq.domain, 294 base + bit); 295 handle_nested_irq(child_irq); 296 } 297 } 298 299 return IRQ_HANDLED; 300 } 301 302 static void adnp_irq_mask(struct irq_data *d) 303 { 304 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 305 struct adnp *adnp = gpiochip_get_data(gc); 306 unsigned int reg = d->hwirq >> adnp->reg_shift; 307 unsigned int pos = d->hwirq & 7; 308 309 adnp->irq_enable[reg] &= ~BIT(pos); 310 } 311 312 static void adnp_irq_unmask(struct irq_data *d) 313 { 314 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 315 struct adnp *adnp = gpiochip_get_data(gc); 316 unsigned int reg = d->hwirq >> adnp->reg_shift; 317 unsigned int pos = d->hwirq & 7; 318 319 adnp->irq_enable[reg] |= BIT(pos); 320 } 321 322 static int adnp_irq_set_type(struct irq_data *d, unsigned int type) 323 { 324 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 325 struct adnp *adnp = gpiochip_get_data(gc); 326 unsigned int reg = d->hwirq >> adnp->reg_shift; 327 unsigned int pos = d->hwirq & 7; 328 329 if (type & IRQ_TYPE_EDGE_RISING) 330 adnp->irq_rise[reg] |= BIT(pos); 331 else 332 adnp->irq_rise[reg] &= ~BIT(pos); 333 334 if (type & IRQ_TYPE_EDGE_FALLING) 335 adnp->irq_fall[reg] |= BIT(pos); 336 else 337 adnp->irq_fall[reg] &= ~BIT(pos); 338 339 if (type & IRQ_TYPE_LEVEL_HIGH) 340 adnp->irq_high[reg] |= BIT(pos); 341 else 342 adnp->irq_high[reg] &= ~BIT(pos); 343 344 if (type & IRQ_TYPE_LEVEL_LOW) 345 adnp->irq_low[reg] |= BIT(pos); 346 else 347 adnp->irq_low[reg] &= ~BIT(pos); 348 349 return 0; 350 } 351 352 static void adnp_irq_bus_lock(struct irq_data *d) 353 { 354 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 355 struct adnp *adnp = gpiochip_get_data(gc); 356 357 mutex_lock(&adnp->irq_lock); 358 } 359 360 static void adnp_irq_bus_unlock(struct irq_data *d) 361 { 362 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 363 struct adnp *adnp = gpiochip_get_data(gc); 364 unsigned int num_regs = 1 << adnp->reg_shift, i; 365 366 mutex_lock(&adnp->i2c_lock); 367 368 for (i = 0; i < num_regs; i++) 369 adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]); 370 371 mutex_unlock(&adnp->i2c_lock); 372 mutex_unlock(&adnp->irq_lock); 373 } 374 375 static struct irq_chip adnp_irq_chip = { 376 .name = "gpio-adnp", 377 .irq_mask = adnp_irq_mask, 378 .irq_unmask = adnp_irq_unmask, 379 .irq_set_type = adnp_irq_set_type, 380 .irq_bus_lock = adnp_irq_bus_lock, 381 .irq_bus_sync_unlock = adnp_irq_bus_unlock, 382 }; 383 384 static int adnp_irq_setup(struct adnp *adnp) 385 { 386 unsigned int num_regs = 1 << adnp->reg_shift, i; 387 struct gpio_chip *chip = &adnp->gpio; 388 int err; 389 390 mutex_init(&adnp->irq_lock); 391 392 /* 393 * Allocate memory to keep track of the current level and trigger 394 * modes of the interrupts. To avoid multiple allocations, a single 395 * large buffer is allocated and pointers are setup to point at the 396 * corresponding offsets. For consistency, the layout of the buffer 397 * is chosen to match the register layout of the hardware in that 398 * each segment contains the corresponding bits for all interrupts. 399 */ 400 adnp->irq_enable = devm_kcalloc(chip->parent, num_regs, 6, 401 GFP_KERNEL); 402 if (!adnp->irq_enable) 403 return -ENOMEM; 404 405 adnp->irq_level = adnp->irq_enable + (num_regs * 1); 406 adnp->irq_rise = adnp->irq_enable + (num_regs * 2); 407 adnp->irq_fall = adnp->irq_enable + (num_regs * 3); 408 adnp->irq_high = adnp->irq_enable + (num_regs * 4); 409 adnp->irq_low = adnp->irq_enable + (num_regs * 5); 410 411 for (i = 0; i < num_regs; i++) { 412 /* 413 * Read the initial level of all pins to allow the emulation 414 * of edge triggered interrupts. 415 */ 416 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &adnp->irq_level[i]); 417 if (err < 0) 418 return err; 419 420 /* disable all interrupts */ 421 err = adnp_write(adnp, GPIO_IER(adnp) + i, 0); 422 if (err < 0) 423 return err; 424 425 adnp->irq_enable[i] = 0x00; 426 } 427 428 err = devm_request_threaded_irq(chip->parent, adnp->client->irq, 429 NULL, adnp_irq, 430 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 431 dev_name(chip->parent), adnp); 432 if (err != 0) { 433 dev_err(chip->parent, "can't request IRQ#%d: %d\n", 434 adnp->client->irq, err); 435 return err; 436 } 437 438 return 0; 439 } 440 441 static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios, 442 bool is_irq_controller) 443 { 444 struct gpio_chip *chip = &adnp->gpio; 445 int err; 446 447 adnp->reg_shift = get_count_order(num_gpios) - 3; 448 449 chip->direction_input = adnp_gpio_direction_input; 450 chip->direction_output = adnp_gpio_direction_output; 451 chip->get = adnp_gpio_get; 452 chip->set = adnp_gpio_set; 453 chip->can_sleep = true; 454 455 if (IS_ENABLED(CONFIG_DEBUG_FS)) 456 chip->dbg_show = adnp_gpio_dbg_show; 457 458 chip->base = -1; 459 chip->ngpio = num_gpios; 460 chip->label = adnp->client->name; 461 chip->parent = &adnp->client->dev; 462 chip->owner = THIS_MODULE; 463 464 if (is_irq_controller) { 465 struct gpio_irq_chip *girq; 466 467 err = adnp_irq_setup(adnp); 468 if (err) 469 return err; 470 471 girq = &chip->irq; 472 girq->chip = &adnp_irq_chip; 473 /* This will let us handle the parent IRQ in the driver */ 474 girq->parent_handler = NULL; 475 girq->num_parents = 0; 476 girq->parents = NULL; 477 girq->default_type = IRQ_TYPE_NONE; 478 girq->handler = handle_simple_irq; 479 girq->threaded = true; 480 } 481 482 err = devm_gpiochip_add_data(&adnp->client->dev, chip, adnp); 483 if (err) 484 return err; 485 486 return 0; 487 } 488 489 static int adnp_i2c_probe(struct i2c_client *client) 490 { 491 struct device *dev = &client->dev; 492 struct adnp *adnp; 493 u32 num_gpios; 494 int err; 495 496 err = device_property_read_u32(dev, "nr-gpios", &num_gpios); 497 if (err < 0) 498 return err; 499 500 adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL); 501 if (!adnp) 502 return -ENOMEM; 503 504 mutex_init(&adnp->i2c_lock); 505 adnp->client = client; 506 507 err = adnp_gpio_setup(adnp, num_gpios, device_property_read_bool(dev, "interrupt-controller")); 508 if (err) 509 return err; 510 511 i2c_set_clientdata(client, adnp); 512 513 return 0; 514 } 515 516 static const struct i2c_device_id adnp_i2c_id[] = { 517 { "gpio-adnp" }, 518 { }, 519 }; 520 MODULE_DEVICE_TABLE(i2c, adnp_i2c_id); 521 522 static const struct of_device_id adnp_of_match[] = { 523 { .compatible = "ad,gpio-adnp", }, 524 { }, 525 }; 526 MODULE_DEVICE_TABLE(of, adnp_of_match); 527 528 static struct i2c_driver adnp_i2c_driver = { 529 .driver = { 530 .name = "gpio-adnp", 531 .of_match_table = adnp_of_match, 532 }, 533 .probe_new = adnp_i2c_probe, 534 .id_table = adnp_i2c_id, 535 }; 536 module_i2c_driver(adnp_i2c_driver); 537 538 MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander"); 539 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); 540 MODULE_LICENSE("GPL"); 541