Lines Matching +full:reg +full:- +full:spacing
1 // SPDX-License-Identifier: GPL-2.0-only
32 #include <linux/pinctrl/pinconf-generic.h>
135 { "irq-gpios", &pca953x_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
161 * relative. Since first controller (gpio-sch.c) and
162 * second (gpio-dwapb.c) are at the fixed bases, we may
184 #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
228 u8 (*recalc_addr)(struct pca953x_chip *chip, int reg, int off);
229 bool (*check_reg)(struct pca953x_chip *chip, unsigned int reg,
235 return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); in pca953x_bank_shift()
257 * - Standard set, below 0x40, each port can be replicated up to 8 times
258 * - PCA953x standard
263 * - PCA957x with mixed up registers
270 * - Extended set, above 0x40, often chip specific.
271 * - PCAL6524/PCAL9555A with custom PCAL IRQ handling:
273 * Pull-up/pull-down enable reg 0x40 + 3 * bank_size RW
274 * Pull-up/pull-down select reg 0x40 + 4 * bank_size RW
278 * - Registers with bit 0x80 set, the AI bit
283 static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg, in pca953x_check_register() argument
287 int bank = (reg & REG_ADDR_MASK) >> bank_shift; in pca953x_check_register()
288 int offset = reg & (BIT(bank_shift) - 1); in pca953x_check_register()
291 if (reg & REG_ADDR_EXT) { in pca953x_check_register()
292 if (!(chip->driver_data & PCA_PCAL)) in pca953x_check_register()
310 * same register layout as the PCAL6524, the spacing of the registers has been
315 static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg, in pcal6534_check_register() argument
322 if (reg >= 0x54) { in pcal6534_check_register()
327 reg -= 0x54; in pcal6534_check_register()
329 } else if (reg >= 0x30) { in pcal6534_check_register()
334 reg -= 0x30; in pcal6534_check_register()
340 bank = bank_shift + reg / NBANK(chip); in pcal6534_check_register()
341 offset = reg % NBANK(chip); in pcal6534_check_register()
354 static bool pca953x_readable_register(struct device *dev, unsigned int reg) in pca953x_readable_register() argument
359 if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) { in pca953x_readable_register()
368 if (chip->driver_data & PCA_PCAL) { in pca953x_readable_register()
374 return chip->check_reg(chip, reg, bank); in pca953x_readable_register()
377 static bool pca953x_writeable_register(struct device *dev, unsigned int reg) in pca953x_writeable_register() argument
382 if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) { in pca953x_writeable_register()
390 if (chip->driver_data & PCA_PCAL) in pca953x_writeable_register()
394 return chip->check_reg(chip, reg, bank); in pca953x_writeable_register()
397 static bool pca953x_volatile_register(struct device *dev, unsigned int reg) in pca953x_volatile_register() argument
402 if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) in pca953x_volatile_register()
407 if (chip->driver_data & PCA_PCAL) in pca953x_volatile_register()
410 return chip->check_reg(chip, reg, bank); in pca953x_volatile_register()
445 static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off) in pca953x_recalc_addr() argument
448 int addr = (reg & PCAL_GPIO_MASK) << bank_shift; in pca953x_recalc_addr()
449 int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1; in pca953x_recalc_addr()
459 static u8 pcal6534_recalc_addr(struct pca953x_chip *chip, int reg, int off) in pcal6534_recalc_addr() argument
464 addr = (reg & PCAL_GPIO_MASK) * NBANK(chip); in pcal6534_recalc_addr()
466 switch (reg) { in pcal6534_recalc_addr()
473 pinctrl = ((reg & PCAL_PINCTRL_MASK) >> 1) + 0x20; in pcal6534_recalc_addr()
480 pinctrl = ((reg & PCAL_PINCTRL_MASK) >> 1) + 0x1c; in pcal6534_recalc_addr()
490 static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long *val) in pca953x_write_regs() argument
492 u8 regaddr = chip->recalc_addr(chip, reg, 0); in pca953x_write_regs()
499 ret = regmap_bulk_write(chip->regmap, regaddr, value, NBANK(chip)); in pca953x_write_regs()
501 dev_err(&chip->client->dev, "failed writing register: %d\n", ret); in pca953x_write_regs()
508 static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *val) in pca953x_read_regs() argument
510 u8 regaddr = chip->recalc_addr(chip, reg, 0); in pca953x_read_regs()
514 ret = regmap_bulk_read(chip->regmap, regaddr, value, NBANK(chip)); in pca953x_read_regs()
516 dev_err(&chip->client->dev, "failed reading register: %d\n", ret); in pca953x_read_regs()
529 u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off); in pca953x_gpio_direction_input()
532 guard(mutex)(&chip->i2c_lock); in pca953x_gpio_direction_input()
534 return regmap_write_bits(chip->regmap, dirreg, bit, bit); in pca953x_gpio_direction_input()
541 u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off); in pca953x_gpio_direction_output()
542 u8 outreg = chip->recalc_addr(chip, chip->regs->output, off); in pca953x_gpio_direction_output()
546 guard(mutex)(&chip->i2c_lock); in pca953x_gpio_direction_output()
549 ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0); in pca953x_gpio_direction_output()
554 return regmap_write_bits(chip->regmap, dirreg, bit, 0); in pca953x_gpio_direction_output()
560 u8 inreg = chip->recalc_addr(chip, chip->regs->input, off); in pca953x_gpio_get_value()
565 scoped_guard(mutex, &chip->i2c_lock) in pca953x_gpio_get_value()
566 ret = regmap_read(chip->regmap, inreg, ®_val); in pca953x_gpio_get_value()
576 u8 outreg = chip->recalc_addr(chip, chip->regs->output, off); in pca953x_gpio_set_value()
579 guard(mutex)(&chip->i2c_lock); in pca953x_gpio_set_value()
581 regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0); in pca953x_gpio_set_value()
587 u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off); in pca953x_gpio_get_direction()
592 scoped_guard(mutex, &chip->i2c_lock) in pca953x_gpio_get_direction()
593 ret = regmap_read(chip->regmap, dirreg, ®_val); in pca953x_gpio_get_direction()
610 scoped_guard(mutex, &chip->i2c_lock) in pca953x_gpio_get_multiple()
611 ret = pca953x_read_regs(chip, chip->regs->input, reg_val); in pca953x_gpio_get_multiple()
615 bitmap_replace(bits, bits, reg_val, mask, gc->ngpio); in pca953x_gpio_get_multiple()
626 guard(mutex)(&chip->i2c_lock); in pca953x_gpio_set_multiple()
628 ret = pca953x_read_regs(chip, chip->regs->output, reg_val); in pca953x_gpio_set_multiple()
632 bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio); in pca953x_gpio_set_multiple()
634 pca953x_write_regs(chip, chip->regs->output, reg_val); in pca953x_gpio_set_multiple()
642 u8 pull_en_reg = chip->recalc_addr(chip, PCAL953X_PULL_EN, offset); in pca953x_gpio_set_pull_up_down()
643 u8 pull_sel_reg = chip->recalc_addr(chip, PCAL953X_PULL_SEL, offset); in pca953x_gpio_set_pull_up_down()
648 * pull-up/pull-down configuration requires PCAL extended in pca953x_gpio_set_pull_up_down()
651 if (!(chip->driver_data & PCA_PCAL)) in pca953x_gpio_set_pull_up_down()
652 return -ENOTSUPP; in pca953x_gpio_set_pull_up_down()
654 guard(mutex)(&chip->i2c_lock); in pca953x_gpio_set_pull_up_down()
656 /* Configure pull-up/pull-down */ in pca953x_gpio_set_pull_up_down()
658 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, bit); in pca953x_gpio_set_pull_up_down()
660 ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, 0); in pca953x_gpio_set_pull_up_down()
666 /* Disable/Enable pull-up/pull-down */ in pca953x_gpio_set_pull_up_down()
668 return regmap_write_bits(chip->regmap, pull_en_reg, bit, 0); in pca953x_gpio_set_pull_up_down()
670 return regmap_write_bits(chip->regmap, pull_en_reg, bit, bit); in pca953x_gpio_set_pull_up_down()
685 return -ENOTSUPP; in pca953x_gpio_set_config()
691 struct gpio_chip *gc = &chip->gpio_chip; in pca953x_setup_gpio()
693 gc->direction_input = pca953x_gpio_direction_input; in pca953x_setup_gpio()
694 gc->direction_output = pca953x_gpio_direction_output; in pca953x_setup_gpio()
695 gc->get = pca953x_gpio_get_value; in pca953x_setup_gpio()
696 gc->set = pca953x_gpio_set_value; in pca953x_setup_gpio()
697 gc->get_direction = pca953x_gpio_get_direction; in pca953x_setup_gpio()
698 gc->get_multiple = pca953x_gpio_get_multiple; in pca953x_setup_gpio()
699 gc->set_multiple = pca953x_gpio_set_multiple; in pca953x_setup_gpio()
700 gc->set_config = pca953x_gpio_set_config; in pca953x_setup_gpio()
701 gc->can_sleep = true; in pca953x_setup_gpio()
703 gc->base = chip->gpio_start; in pca953x_setup_gpio()
704 gc->ngpio = gpios; in pca953x_setup_gpio()
705 gc->label = dev_name(&chip->client->dev); in pca953x_setup_gpio()
706 gc->parent = &chip->client->dev; in pca953x_setup_gpio()
707 gc->owner = THIS_MODULE; in pca953x_setup_gpio()
717 clear_bit(hwirq, chip->irq_mask); in pca953x_irq_mask()
728 set_bit(hwirq, chip->irq_mask); in pca953x_irq_unmask()
737 atomic_inc(&chip->wakeup_path); in pca953x_irq_set_wake()
739 atomic_dec(&chip->wakeup_path); in pca953x_irq_set_wake()
741 return irq_set_irq_wake(chip->client->irq, on); in pca953x_irq_set_wake()
749 mutex_lock(&chip->irq_lock); in pca953x_irq_bus_lock()
760 if (chip->driver_data & PCA_PCAL) { in pca953x_irq_bus_sync_unlock()
761 guard(mutex)(&chip->i2c_lock); in pca953x_irq_bus_sync_unlock()
763 /* Enable latch on interrupt-enabled inputs */ in pca953x_irq_bus_sync_unlock()
764 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask); in pca953x_irq_bus_sync_unlock()
766 bitmap_complement(irq_mask, chip->irq_mask, gc->ngpio); in pca953x_irq_bus_sync_unlock()
773 pca953x_read_regs(chip, chip->regs->direction, reg_direction); in pca953x_irq_bus_sync_unlock()
775 bitmap_or(irq_mask, chip->irq_trig_fall, chip->irq_trig_raise, gc->ngpio); in pca953x_irq_bus_sync_unlock()
776 bitmap_complement(reg_direction, reg_direction, gc->ngpio); in pca953x_irq_bus_sync_unlock()
777 bitmap_and(irq_mask, irq_mask, reg_direction, gc->ngpio); in pca953x_irq_bus_sync_unlock()
780 for_each_set_bit(level, irq_mask, gc->ngpio) in pca953x_irq_bus_sync_unlock()
781 pca953x_gpio_direction_input(&chip->gpio_chip, level); in pca953x_irq_bus_sync_unlock()
783 mutex_unlock(&chip->irq_lock); in pca953x_irq_bus_sync_unlock()
790 struct device *dev = &chip->client->dev; in pca953x_irq_set_type()
794 dev_err(dev, "irq %d: unsupported type %d\n", d->irq, type); in pca953x_irq_set_type()
795 return -EINVAL; in pca953x_irq_set_type()
798 assign_bit(hwirq, chip->irq_trig_fall, type & IRQ_TYPE_EDGE_FALLING); in pca953x_irq_set_type()
799 assign_bit(hwirq, chip->irq_trig_raise, type & IRQ_TYPE_EDGE_RISING); in pca953x_irq_set_type()
810 clear_bit(hwirq, chip->irq_trig_raise); in pca953x_irq_shutdown()
811 clear_bit(hwirq, chip->irq_trig_fall); in pca953x_irq_shutdown()
818 seq_printf(p, dev_name(gc->parent)); in pca953x_irq_print_chip()
836 struct gpio_chip *gc = &chip->gpio_chip; in pca953x_irq_pending()
844 if (chip->driver_data & PCA_PCAL) { in pca953x_irq_pending()
851 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat); in pca953x_irq_pending()
856 bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise, cur_stat, gc->ngpio); in pca953x_irq_pending()
858 bitmap_and(pending, new_stat, trigger, gc->ngpio); in pca953x_irq_pending()
860 return !bitmap_empty(pending, gc->ngpio); in pca953x_irq_pending()
863 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat); in pca953x_irq_pending()
868 pca953x_read_regs(chip, chip->regs->direction, reg_direction); in pca953x_irq_pending()
870 bitmap_copy(old_stat, chip->irq_stat, gc->ngpio); in pca953x_irq_pending()
872 bitmap_and(new_stat, cur_stat, reg_direction, gc->ngpio); in pca953x_irq_pending()
873 bitmap_xor(cur_stat, new_stat, old_stat, gc->ngpio); in pca953x_irq_pending()
874 bitmap_and(trigger, cur_stat, chip->irq_mask, gc->ngpio); in pca953x_irq_pending()
876 bitmap_copy(chip->irq_stat, new_stat, gc->ngpio); in pca953x_irq_pending()
878 if (bitmap_empty(trigger, gc->ngpio)) in pca953x_irq_pending()
881 bitmap_and(cur_stat, chip->irq_trig_fall, old_stat, gc->ngpio); in pca953x_irq_pending()
882 bitmap_and(old_stat, chip->irq_trig_raise, new_stat, gc->ngpio); in pca953x_irq_pending()
883 bitmap_or(new_stat, old_stat, cur_stat, gc->ngpio); in pca953x_irq_pending()
884 bitmap_and(pending, new_stat, trigger, gc->ngpio); in pca953x_irq_pending()
886 return !bitmap_empty(pending, gc->ngpio); in pca953x_irq_pending()
892 struct gpio_chip *gc = &chip->gpio_chip; in pca953x_irq_handler()
899 scoped_guard(mutex, &chip->i2c_lock) in pca953x_irq_handler()
904 for_each_set_bit(level, pending, gc->ngpio) { in pca953x_irq_handler()
905 int nested_irq = irq_find_mapping(gc->irq.domain, level); in pca953x_irq_handler()
908 dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level); in pca953x_irq_handler()
922 struct i2c_client *client = chip->client; in pca953x_irq_setup()
923 struct device *dev = &client->dev; in pca953x_irq_setup()
926 struct gpio_chip *gc = &chip->gpio_chip; in pca953x_irq_setup()
933 client->irq = ret; in pca953x_irq_setup()
936 if (!client->irq) in pca953x_irq_setup()
939 if (irq_base == -1) in pca953x_irq_setup()
942 if (!(chip->driver_data & PCA_INT)) in pca953x_irq_setup()
945 ret = pca953x_read_regs(chip, chip->regs->input, irq_stat); in pca953x_irq_setup()
954 pca953x_read_regs(chip, chip->regs->direction, reg_direction); in pca953x_irq_setup()
955 bitmap_and(chip->irq_stat, irq_stat, reg_direction, gc->ngpio); in pca953x_irq_setup()
956 mutex_init(&chip->irq_lock); in pca953x_irq_setup()
958 girq = &chip->gpio_chip.irq; in pca953x_irq_setup()
961 girq->parent_handler = NULL; in pca953x_irq_setup()
962 girq->num_parents = 0; in pca953x_irq_setup()
963 girq->parents = NULL; in pca953x_irq_setup()
964 girq->default_type = IRQ_TYPE_NONE; in pca953x_irq_setup()
965 girq->handler = handle_simple_irq; in pca953x_irq_setup()
966 girq->threaded = true; in pca953x_irq_setup()
967 girq->first = irq_base; /* FIXME: get rid of this */ in pca953x_irq_setup()
969 ret = devm_request_threaded_irq(dev, client->irq, NULL, pca953x_irq_handler, in pca953x_irq_setup()
973 return dev_err_probe(dev, client->irq, "failed to request irq\n"); in pca953x_irq_setup()
981 struct i2c_client *client = chip->client; in pca953x_irq_setup()
982 struct device *dev = &client->dev; in pca953x_irq_setup()
984 if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT)) in pca953x_irq_setup()
997 regaddr = chip->recalc_addr(chip, chip->regs->output, 0); in device_pca95xx_init()
998 ret = regcache_sync_region(chip->regmap, regaddr, in device_pca95xx_init()
999 regaddr + NBANK(chip) - 1); in device_pca95xx_init()
1003 regaddr = chip->recalc_addr(chip, chip->regs->direction, 0); in device_pca95xx_init()
1004 ret = regcache_sync_region(chip->regmap, regaddr, in device_pca95xx_init()
1005 regaddr + NBANK(chip) - 1); in device_pca95xx_init()
1012 return pca953x_write_regs(chip, chip->regs->invert, val); in device_pca95xx_init()
1032 static void pca953x_disable_regulator(void *reg) in pca953x_disable_regulator() argument
1034 regulator_disable(reg); in pca953x_disable_regulator()
1039 struct device *dev = &chip->client->dev; in pca953x_get_and_enable_regulator()
1040 struct regulator *reg = chip->regulator; in pca953x_get_and_enable_regulator() local
1043 reg = devm_regulator_get(dev, "vcc"); in pca953x_get_and_enable_regulator()
1044 if (IS_ERR(reg)) in pca953x_get_and_enable_regulator()
1045 return dev_err_probe(dev, PTR_ERR(reg), "reg get err\n"); in pca953x_get_and_enable_regulator()
1047 ret = regulator_enable(reg); in pca953x_get_and_enable_regulator()
1049 return dev_err_probe(dev, ret, "reg en err\n"); in pca953x_get_and_enable_regulator()
1051 ret = devm_add_action_or_reset(dev, pca953x_disable_regulator, reg); in pca953x_get_and_enable_regulator()
1055 chip->regulator = reg; in pca953x_get_and_enable_regulator()
1061 struct device *dev = &client->dev; in pca953x_probe()
1070 return -ENOMEM; in pca953x_probe()
1074 irq_base = pdata->irq_base; in pca953x_probe()
1075 chip->gpio_start = pdata->gpio_base; in pca953x_probe()
1079 chip->gpio_start = -1; in pca953x_probe()
1083 * See if we need to de-assert a reset pin. in pca953x_probe()
1085 * There is no known ACPI-enabled platforms that are in pca953x_probe()
1094 chip->client = client; in pca953x_probe()
1095 chip->driver_data = (uintptr_t)i2c_get_match_data(client); in pca953x_probe()
1096 if (!chip->driver_data) in pca953x_probe()
1097 return -ENODEV; in pca953x_probe()
1105 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK); in pca953x_probe()
1107 if (NBANK(chip) > 2 || PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) { in pca953x_probe()
1115 if (PCA_CHIP_TYPE(chip->driver_data) == PCAL653X_TYPE) { in pca953x_probe()
1116 chip->recalc_addr = pcal6534_recalc_addr; in pca953x_probe()
1117 chip->check_reg = pcal6534_check_register; in pca953x_probe()
1119 chip->recalc_addr = pca953x_recalc_addr; in pca953x_probe()
1120 chip->check_reg = pca953x_check_register; in pca953x_probe()
1123 chip->regmap = devm_regmap_init_i2c(client, regmap_config); in pca953x_probe()
1124 if (IS_ERR(chip->regmap)) in pca953x_probe()
1125 return PTR_ERR(chip->regmap); in pca953x_probe()
1127 regcache_mark_dirty(chip->regmap); in pca953x_probe()
1129 mutex_init(&chip->i2c_lock); in pca953x_probe()
1131 * In case we have an i2c-mux controlled by a GPIO provided by an in pca953x_probe()
1134 * subclass for chip->i2c_lock. in pca953x_probe()
1137 * false positives when the expander controlling the i2c-mux is on in pca953x_probe()
1143 * applied to all other i2c-controlled GPIO expanders (and potentially in pca953x_probe()
1144 * regmap-i2c). in pca953x_probe()
1146 lockdep_set_subclass(&chip->i2c_lock, in pca953x_probe()
1147 i2c_adapter_depth(client->adapter)); in pca953x_probe()
1152 if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) { in pca953x_probe()
1153 chip->regs = &pca957x_regs; in pca953x_probe()
1156 chip->regs = &pca953x_regs; in pca953x_probe()
1166 return devm_gpiochip_add_data(dev, &chip->gpio_chip, chip); in pca953x_probe()
1171 struct device *dev = &chip->client->dev; in pca953x_regcache_sync()
1179 regaddr = chip->recalc_addr(chip, chip->regs->direction, 0); in pca953x_regcache_sync()
1180 ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1); in pca953x_regcache_sync()
1186 regaddr = chip->recalc_addr(chip, chip->regs->output, 0); in pca953x_regcache_sync()
1187 ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1); in pca953x_regcache_sync()
1194 if (chip->driver_data & PCA_PCAL) { in pca953x_regcache_sync()
1195 regaddr = chip->recalc_addr(chip, PCAL953X_IN_LATCH, 0); in pca953x_regcache_sync()
1196 ret = regcache_sync_region(chip->regmap, regaddr, in pca953x_regcache_sync()
1197 regaddr + NBANK(chip) - 1); in pca953x_regcache_sync()
1204 regaddr = chip->recalc_addr(chip, PCAL953X_INT_MASK, 0); in pca953x_regcache_sync()
1205 ret = regcache_sync_region(chip->regmap, regaddr, in pca953x_regcache_sync()
1206 regaddr + NBANK(chip) - 1); in pca953x_regcache_sync()
1222 guard(mutex)(&chip->i2c_lock); in pca953x_restore_context()
1224 regcache_cache_only(chip->regmap, false); in pca953x_restore_context()
1225 regcache_mark_dirty(chip->regmap); in pca953x_restore_context()
1230 return regcache_sync(chip->regmap); in pca953x_restore_context()
1235 guard(mutex)(&chip->i2c_lock); in pca953x_save_context()
1236 regcache_cache_only(chip->regmap, true); in pca953x_save_context()
1245 if (atomic_read(&chip->wakeup_path)) in pca953x_suspend()
1248 regulator_disable(chip->regulator); in pca953x_suspend()
1258 if (!atomic_read(&chip->wakeup_path)) { in pca953x_resume()
1259 ret = regulator_enable(chip->regulator); in pca953x_resume()
1275 /* convenience to stop overlong match-table lines */