1 // SPDX-License-Identifier: GPL-2.0-only
3 * CY8C95X0 20/40/60 pin I2C GPIO port expander with interrupt support
5 * Copyright (C) 2022 9elements GmbH
6 * Authors: Patrick Rudolph <patrick.rudolph@9elements.com>
7 * Naresh Solanki <Naresh.Solanki@9elements.com>
10 #include <linux/acpi.h>
11 #include <linux/bitmap.h>
12 #include <linux/dmi.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/module.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/seq_file.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/pinctrl/pinconf.h>
27 #include <linux/pinctrl/pinconf-generic.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
31 /* Fast access registers */
32 #define CY8C95X0_INPUT 0x00
33 #define CY8C95X0_OUTPUT 0x08
34 #define CY8C95X0_INTSTATUS 0x10
36 #define CY8C95X0_INPUT_(x) (CY8C95X0_INPUT + (x))
37 #define CY8C95X0_OUTPUT_(x) (CY8C95X0_OUTPUT + (x))
38 #define CY8C95X0_INTSTATUS_(x) (CY8C95X0_INTSTATUS + (x))
40 /* Port Select configures the port */
41 #define CY8C95X0_PORTSEL 0x18
42 /* Port settings, write PORTSEL first */
43 #define CY8C95X0_INTMASK 0x19
44 #define CY8C95X0_PWMSEL 0x1A
45 #define CY8C95X0_INVERT 0x1B
46 #define CY8C95X0_DIRECTION 0x1C
47 /* Drive mode register change state on writing '1' */
48 #define CY8C95X0_DRV_PU 0x1D
49 #define CY8C95X0_DRV_PD 0x1E
50 #define CY8C95X0_DRV_ODH 0x1F
51 #define CY8C95X0_DRV_ODL 0x20
52 #define CY8C95X0_DRV_PP_FAST 0x21
53 #define CY8C95X0_DRV_PP_SLOW 0x22
54 #define CY8C95X0_DRV_HIZ 0x23
55 #define CY8C95X0_DEVID 0x2E
56 #define CY8C95X0_WATCHDOG 0x2F
57 #define CY8C95X0_COMMAND 0x30
59 #define CY8C95X0_PIN_TO_OFFSET(x) (((x) >= 20) ? ((x) + 4) : (x))
61 static const struct i2c_device_id cy8c95x0_id[] = {
67 MODULE_DEVICE_TABLE(i2c, cy8c95x0_id);
69 #define OF_CY8C95X(__nrgpio) ((void *)(__nrgpio))
71 static const struct of_device_id cy8c95x0_dt_ids[] = {
72 { .compatible = "cypress,cy8c9520", .data = OF_CY8C95X(20), },
73 { .compatible = "cypress,cy8c9540", .data = OF_CY8C95X(40), },
74 { .compatible = "cypress,cy8c9560", .data = OF_CY8C95X(60), },
77 MODULE_DEVICE_TABLE(of, cy8c95x0_dt_ids);
79 static const struct acpi_gpio_params cy8c95x0_irq_gpios = { 0, 0, true };
81 static const struct acpi_gpio_mapping cy8c95x0_acpi_irq_gpios[] = {
82 { "irq-gpios", &cy8c95x0_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
86 static int cy8c95x0_acpi_get_irq(struct device *dev)
90 ret = devm_acpi_dev_add_driver_gpios(dev, cy8c95x0_acpi_irq_gpios);
92 dev_warn(dev, "can't add GPIO ACPI mapping\n");
94 ret = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(dev), "irq-gpios", 0);
98 dev_info(dev, "ACPI interrupt quirk (IRQ %d)\n", ret);
102 static const struct dmi_system_id cy8c95x0_dmi_acpi_irq_info[] = {
105 * On Intel Galileo Gen 1 board the IRQ pin is provided
106 * as an absolute number instead of being relative.
107 * Since first controller (gpio-sch.c) and second
108 * (gpio-dwapb.c) are at the fixed bases, we may safely
109 * refer to the number in the global space to get an IRQ
113 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Galileo"),
121 #define MAX_LINE (MAX_BANK * BANK_SZ)
123 #define CY8C95X0_GPIO_MASK GENMASK(7, 0)
126 * struct cy8c95x0_pinctrl - driver data
127 * @regmap: Device's regmap
128 * @irq_lock: IRQ bus lock
129 * @i2c_lock: Mutex for the device internal mux register
130 * @irq_mask: I/O bits affected by interrupts
131 * @irq_trig_raise: I/O bits affected by raising voltage level
132 * @irq_trig_fall: I/O bits affected by falling voltage level
133 * @irq_trig_low: I/O bits affected by a low voltage level
134 * @irq_trig_high: I/O bits affected by a high voltage level
135 * @push_pull: I/O bits configured as push pull driver
136 * @shiftmask: Mask used to compensate for Gport2 width
137 * @nport: Number of Gports in this chip
138 * @gpio_chip: gpiolib chip
139 * @driver_data: private driver data
140 * @regulator: Pointer to the regulator for the IC
141 * @dev: struct device
142 * @pctldev: pin controller device
143 * @pinctrl_desc: pin controller description
144 * @name: Chip controller name
145 * @tpin: Total number of pins
147 struct cy8c95x0_pinctrl {
148 struct regmap *regmap;
149 struct mutex irq_lock;
150 struct mutex i2c_lock;
151 DECLARE_BITMAP(irq_mask, MAX_LINE);
152 DECLARE_BITMAP(irq_trig_raise, MAX_LINE);
153 DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
154 DECLARE_BITMAP(irq_trig_low, MAX_LINE);
155 DECLARE_BITMAP(irq_trig_high, MAX_LINE);
156 DECLARE_BITMAP(push_pull, MAX_LINE);
157 DECLARE_BITMAP(shiftmask, MAX_LINE);
159 struct gpio_chip gpio_chip;
160 unsigned long driver_data;
161 struct regulator *regulator;
163 struct pinctrl_dev *pctldev;
164 struct pinctrl_desc pinctrl_desc;
167 struct gpio_desc *gpio_reset;
170 static const struct pinctrl_pin_desc cy8c9560_pins[] = {
171 PINCTRL_PIN(0, "gp00"),
172 PINCTRL_PIN(1, "gp01"),
173 PINCTRL_PIN(2, "gp02"),
174 PINCTRL_PIN(3, "gp03"),
175 PINCTRL_PIN(4, "gp04"),
176 PINCTRL_PIN(5, "gp05"),
177 PINCTRL_PIN(6, "gp06"),
178 PINCTRL_PIN(7, "gp07"),
180 PINCTRL_PIN(8, "gp10"),
181 PINCTRL_PIN(9, "gp11"),
182 PINCTRL_PIN(10, "gp12"),
183 PINCTRL_PIN(11, "gp13"),
184 PINCTRL_PIN(12, "gp14"),
185 PINCTRL_PIN(13, "gp15"),
186 PINCTRL_PIN(14, "gp16"),
187 PINCTRL_PIN(15, "gp17"),
189 PINCTRL_PIN(16, "gp20"),
190 PINCTRL_PIN(17, "gp21"),
191 PINCTRL_PIN(18, "gp22"),
192 PINCTRL_PIN(19, "gp23"),
194 PINCTRL_PIN(20, "gp30"),
195 PINCTRL_PIN(21, "gp31"),
196 PINCTRL_PIN(22, "gp32"),
197 PINCTRL_PIN(23, "gp33"),
198 PINCTRL_PIN(24, "gp34"),
199 PINCTRL_PIN(25, "gp35"),
200 PINCTRL_PIN(26, "gp36"),
201 PINCTRL_PIN(27, "gp37"),
203 PINCTRL_PIN(28, "gp40"),
204 PINCTRL_PIN(29, "gp41"),
205 PINCTRL_PIN(30, "gp42"),
206 PINCTRL_PIN(31, "gp43"),
207 PINCTRL_PIN(32, "gp44"),
208 PINCTRL_PIN(33, "gp45"),
209 PINCTRL_PIN(34, "gp46"),
210 PINCTRL_PIN(35, "gp47"),
212 PINCTRL_PIN(36, "gp50"),
213 PINCTRL_PIN(37, "gp51"),
214 PINCTRL_PIN(38, "gp52"),
215 PINCTRL_PIN(39, "gp53"),
216 PINCTRL_PIN(40, "gp54"),
217 PINCTRL_PIN(41, "gp55"),
218 PINCTRL_PIN(42, "gp56"),
219 PINCTRL_PIN(43, "gp57"),
221 PINCTRL_PIN(44, "gp60"),
222 PINCTRL_PIN(45, "gp61"),
223 PINCTRL_PIN(46, "gp62"),
224 PINCTRL_PIN(47, "gp63"),
225 PINCTRL_PIN(48, "gp64"),
226 PINCTRL_PIN(49, "gp65"),
227 PINCTRL_PIN(50, "gp66"),
228 PINCTRL_PIN(51, "gp67"),
230 PINCTRL_PIN(52, "gp70"),
231 PINCTRL_PIN(53, "gp71"),
232 PINCTRL_PIN(54, "gp72"),
233 PINCTRL_PIN(55, "gp73"),
234 PINCTRL_PIN(56, "gp74"),
235 PINCTRL_PIN(57, "gp75"),
236 PINCTRL_PIN(58, "gp76"),
237 PINCTRL_PIN(59, "gp77"),
240 static const char * const cy8c95x0_groups[] = {
310 static inline u8 cypress_get_port(struct cy8c95x0_pinctrl *chip, unsigned int pin)
312 /* Account for GPORT2 which only has 4 bits */
313 return CY8C95X0_PIN_TO_OFFSET(pin) / BANK_SZ;
316 static int cypress_get_pin_mask(struct cy8c95x0_pinctrl *chip, unsigned int pin)
318 /* Account for GPORT2 which only has 4 bits */
319 return BIT(CY8C95X0_PIN_TO_OFFSET(pin) % BANK_SZ);
322 static bool cy8c95x0_readable_register(struct device *dev, unsigned int reg)
332 static bool cy8c95x0_writeable_register(struct device *dev, unsigned int reg)
335 case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
346 static bool cy8c95x0_volatile_register(struct device *dev, unsigned int reg)
349 case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
350 case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
351 case CY8C95X0_INTMASK:
352 case CY8C95X0_INVERT:
353 case CY8C95X0_PWMSEL:
354 case CY8C95X0_DIRECTION:
355 case CY8C95X0_DRV_PU:
356 case CY8C95X0_DRV_PD:
357 case CY8C95X0_DRV_ODH:
358 case CY8C95X0_DRV_ODL:
359 case CY8C95X0_DRV_PP_FAST:
360 case CY8C95X0_DRV_PP_SLOW:
361 case CY8C95X0_DRV_HIZ:
368 static bool cy8c95x0_precious_register(struct device *dev, unsigned int reg)
371 case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
378 static const struct reg_default cy8c95x0_reg_defaults[] = {
379 { CY8C95X0_OUTPUT_(0), GENMASK(7, 0) },
380 { CY8C95X0_OUTPUT_(1), GENMASK(7, 0) },
381 { CY8C95X0_OUTPUT_(2), GENMASK(7, 0) },
382 { CY8C95X0_OUTPUT_(3), GENMASK(7, 0) },
383 { CY8C95X0_OUTPUT_(4), GENMASK(7, 0) },
384 { CY8C95X0_OUTPUT_(5), GENMASK(7, 0) },
385 { CY8C95X0_OUTPUT_(6), GENMASK(7, 0) },
386 { CY8C95X0_OUTPUT_(7), GENMASK(7, 0) },
387 { CY8C95X0_PORTSEL, 0 },
388 { CY8C95X0_PWMSEL, 0 },
391 static const struct regmap_config cy8c95x0_i2c_regmap = {
395 .reg_defaults = cy8c95x0_reg_defaults,
396 .num_reg_defaults = ARRAY_SIZE(cy8c95x0_reg_defaults),
398 .readable_reg = cy8c95x0_readable_register,
399 .writeable_reg = cy8c95x0_writeable_register,
400 .volatile_reg = cy8c95x0_volatile_register,
401 .precious_reg = cy8c95x0_precious_register,
403 .cache_type = REGCACHE_FLAT,
404 .max_register = CY8C95X0_COMMAND,
407 static int cy8c95x0_write_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
408 unsigned long *val, unsigned long *mask)
410 DECLARE_BITMAP(tmask, MAX_LINE);
411 DECLARE_BITMAP(tval, MAX_LINE);
417 /* Add the 4 bit gap of Gport2 */
418 bitmap_andnot(tmask, mask, chip->shiftmask, MAX_LINE);
419 bitmap_shift_left(tmask, tmask, 4, MAX_LINE);
420 bitmap_replace(tmask, tmask, mask, chip->shiftmask, BANK_SZ * 3);
422 bitmap_andnot(tval, val, chip->shiftmask, MAX_LINE);
423 bitmap_shift_left(tval, tval, 4, MAX_LINE);
424 bitmap_replace(tval, tval, val, chip->shiftmask, BANK_SZ * 3);
426 mutex_lock(&chip->i2c_lock);
427 for (i = 0; i < chip->nport; i++) {
428 /* Skip over unused banks */
429 bits = bitmap_get_value8(tmask, i * BANK_SZ);
434 /* Muxed registers */
435 case CY8C95X0_INTMASK:
436 case CY8C95X0_PWMSEL:
437 case CY8C95X0_INVERT:
438 case CY8C95X0_DIRECTION:
439 case CY8C95X0_DRV_PU:
440 case CY8C95X0_DRV_PD:
441 case CY8C95X0_DRV_ODH:
442 case CY8C95X0_DRV_ODL:
443 case CY8C95X0_DRV_PP_FAST:
444 case CY8C95X0_DRV_PP_SLOW:
445 case CY8C95X0_DRV_HIZ:
446 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, i);
451 /* Direct access registers */
453 case CY8C95X0_OUTPUT:
454 case CY8C95X0_INTSTATUS:
462 write_val = bitmap_get_value8(tval, i * BANK_SZ);
464 ret = regmap_update_bits(chip->regmap, off, bits, write_val);
469 mutex_unlock(&chip->i2c_lock);
472 dev_err(chip->dev, "failed writing register %d: err %d\n", off, ret);
477 static int cy8c95x0_read_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
478 unsigned long *val, unsigned long *mask)
480 DECLARE_BITMAP(tmask, MAX_LINE);
481 DECLARE_BITMAP(tval, MAX_LINE);
482 DECLARE_BITMAP(tmp, MAX_LINE);
488 /* Add the 4 bit gap of Gport2 */
489 bitmap_andnot(tmask, mask, chip->shiftmask, MAX_LINE);
490 bitmap_shift_left(tmask, tmask, 4, MAX_LINE);
491 bitmap_replace(tmask, tmask, mask, chip->shiftmask, BANK_SZ * 3);
493 bitmap_andnot(tval, val, chip->shiftmask, MAX_LINE);
494 bitmap_shift_left(tval, tval, 4, MAX_LINE);
495 bitmap_replace(tval, tval, val, chip->shiftmask, BANK_SZ * 3);
497 mutex_lock(&chip->i2c_lock);
498 for (i = 0; i < chip->nport; i++) {
499 /* Skip over unused banks */
500 bits = bitmap_get_value8(tmask, i * BANK_SZ);
505 /* Muxed registers */
506 case CY8C95X0_INTMASK:
507 case CY8C95X0_PWMSEL:
508 case CY8C95X0_INVERT:
509 case CY8C95X0_DIRECTION:
510 case CY8C95X0_DRV_PU:
511 case CY8C95X0_DRV_PD:
512 case CY8C95X0_DRV_ODH:
513 case CY8C95X0_DRV_ODL:
514 case CY8C95X0_DRV_PP_FAST:
515 case CY8C95X0_DRV_PP_SLOW:
516 case CY8C95X0_DRV_HIZ:
517 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, i);
522 /* Direct access registers */
524 case CY8C95X0_OUTPUT:
525 case CY8C95X0_INTSTATUS:
533 ret = regmap_read(chip->regmap, off, &read_val);
538 read_val |= bitmap_get_value8(tval, i * BANK_SZ) & ~bits;
539 bitmap_set_value8(tval, read_val, i * BANK_SZ);
542 /* Fill the 4 bit gap of Gport2 */
543 bitmap_shift_right(tmp, tval, 4, MAX_LINE);
544 bitmap_replace(val, tmp, tval, chip->shiftmask, MAX_LINE);
547 mutex_unlock(&chip->i2c_lock);
550 dev_err(chip->dev, "failed reading register %d: err %d\n", off, ret);
555 static int cy8c95x0_gpio_direction_input(struct gpio_chip *gc, unsigned int off)
557 return pinctrl_gpio_direction_input(gc->base + off);
560 static int cy8c95x0_gpio_direction_output(struct gpio_chip *gc,
561 unsigned int off, int val)
563 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
564 u8 port = cypress_get_port(chip, off);
565 u8 outreg = CY8C95X0_OUTPUT_(port);
566 u8 bit = cypress_get_pin_mask(chip, off);
569 /* Set output level */
570 ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
574 return pinctrl_gpio_direction_output(gc->base + off);
577 static int cy8c95x0_gpio_get_value(struct gpio_chip *gc, unsigned int off)
579 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
580 u8 inreg = CY8C95X0_INPUT_(cypress_get_port(chip, off));
581 u8 bit = cypress_get_pin_mask(chip, off);
585 ret = regmap_read(chip->regmap, inreg, ®_val);
589 * Diagnostic already emitted; that's all we should
590 * do unless gpio_*_value_cansleep() calls become different
591 * from their nonsleeping siblings (and report faults).
596 return !!(reg_val & bit);
599 static void cy8c95x0_gpio_set_value(struct gpio_chip *gc, unsigned int off,
602 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
603 u8 outreg = CY8C95X0_OUTPUT_(cypress_get_port(chip, off));
604 u8 bit = cypress_get_pin_mask(chip, off);
606 regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
609 static int cy8c95x0_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
611 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
612 u8 port = cypress_get_port(chip, off);
613 u8 bit = cypress_get_pin_mask(chip, off);
617 mutex_lock(&chip->i2c_lock);
619 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
623 ret = regmap_read(chip->regmap, CY8C95X0_DIRECTION, ®_val);
627 mutex_unlock(&chip->i2c_lock);
630 return GPIO_LINE_DIRECTION_IN;
632 return GPIO_LINE_DIRECTION_OUT;
634 mutex_unlock(&chip->i2c_lock);
638 static int cy8c95x0_gpio_get_pincfg(struct cy8c95x0_pinctrl *chip,
640 unsigned long *config)
642 enum pin_config_param param = pinconf_to_config_param(*config);
643 u8 port = cypress_get_port(chip, off);
644 u8 bit = cypress_get_pin_mask(chip, off);
650 mutex_lock(&chip->i2c_lock);
653 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
658 case PIN_CONFIG_BIAS_PULL_UP:
659 reg = CY8C95X0_DRV_PU;
661 case PIN_CONFIG_BIAS_PULL_DOWN:
662 reg = CY8C95X0_DRV_PD;
664 case PIN_CONFIG_BIAS_DISABLE:
665 reg = CY8C95X0_DRV_HIZ;
667 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
668 reg = CY8C95X0_DRV_ODL;
670 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
671 reg = CY8C95X0_DRV_ODH;
673 case PIN_CONFIG_DRIVE_PUSH_PULL:
674 reg = CY8C95X0_DRV_PP_FAST;
676 case PIN_CONFIG_INPUT_ENABLE:
677 reg = CY8C95X0_DIRECTION;
679 case PIN_CONFIG_MODE_PWM:
680 reg = CY8C95X0_PWMSEL;
682 case PIN_CONFIG_OUTPUT:
683 reg = CY8C95X0_OUTPUT_(port);
685 case PIN_CONFIG_OUTPUT_ENABLE:
686 reg = CY8C95X0_DIRECTION;
689 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
690 case PIN_CONFIG_BIAS_BUS_HOLD:
691 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
692 case PIN_CONFIG_DRIVE_STRENGTH:
693 case PIN_CONFIG_DRIVE_STRENGTH_UA:
694 case PIN_CONFIG_INPUT_DEBOUNCE:
695 case PIN_CONFIG_INPUT_SCHMITT:
696 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
697 case PIN_CONFIG_MODE_LOW_POWER:
698 case PIN_CONFIG_PERSIST_STATE:
699 case PIN_CONFIG_POWER_SOURCE:
700 case PIN_CONFIG_SKEW_DELAY:
701 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
702 case PIN_CONFIG_SLEW_RATE:
708 * Writing 1 to one of the drive mode registers will automatically
709 * clear conflicting set bits in the other drive mode registers.
711 ret = regmap_read(chip->regmap, reg, ®_val);
715 *config = pinconf_to_config_packed(param, (u16)arg);
717 mutex_unlock(&chip->i2c_lock);
722 static int cy8c95x0_gpio_set_pincfg(struct cy8c95x0_pinctrl *chip,
724 unsigned long config)
726 u8 port = cypress_get_port(chip, off);
727 u8 bit = cypress_get_pin_mask(chip, off);
728 unsigned long param = pinconf_to_config_param(config);
732 mutex_lock(&chip->i2c_lock);
735 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
740 case PIN_CONFIG_BIAS_PULL_UP:
741 __clear_bit(off, chip->push_pull);
742 reg = CY8C95X0_DRV_PU;
744 case PIN_CONFIG_BIAS_PULL_DOWN:
745 __clear_bit(off, chip->push_pull);
746 reg = CY8C95X0_DRV_PD;
748 case PIN_CONFIG_BIAS_DISABLE:
749 __clear_bit(off, chip->push_pull);
750 reg = CY8C95X0_DRV_HIZ;
752 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
753 __clear_bit(off, chip->push_pull);
754 reg = CY8C95X0_DRV_ODL;
756 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
757 __clear_bit(off, chip->push_pull);
758 reg = CY8C95X0_DRV_ODH;
760 case PIN_CONFIG_DRIVE_PUSH_PULL:
761 __set_bit(off, chip->push_pull);
762 reg = CY8C95X0_DRV_PP_FAST;
764 case PIN_CONFIG_MODE_PWM:
765 reg = CY8C95X0_PWMSEL;
772 * Writing 1 to one of the drive mode registers will automatically
773 * clear conflicting set bits in the other drive mode registers.
775 ret = regmap_write_bits(chip->regmap, reg, bit, bit);
778 mutex_unlock(&chip->i2c_lock);
782 static int cy8c95x0_gpio_get_multiple(struct gpio_chip *gc,
783 unsigned long *mask, unsigned long *bits)
785 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
787 return cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, bits, mask);
790 static void cy8c95x0_gpio_set_multiple(struct gpio_chip *gc,
791 unsigned long *mask, unsigned long *bits)
793 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
795 cy8c95x0_write_regs_mask(chip, CY8C95X0_OUTPUT, bits, mask);
798 static int cy8c95x0_add_pin_ranges(struct gpio_chip *gc)
800 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
801 struct device *dev = chip->dev;
804 ret = gpiochip_add_pin_range(gc, dev_name(dev), 0, 0, chip->tpin);
806 dev_err(dev, "failed to add GPIO pin range\n");
811 static int cy8c95x0_setup_gpiochip(struct cy8c95x0_pinctrl *chip)
813 struct gpio_chip *gc = &chip->gpio_chip;
815 gc->request = gpiochip_generic_request;
816 gc->free = gpiochip_generic_free;
817 gc->direction_input = cy8c95x0_gpio_direction_input;
818 gc->direction_output = cy8c95x0_gpio_direction_output;
819 gc->get = cy8c95x0_gpio_get_value;
820 gc->set = cy8c95x0_gpio_set_value;
821 gc->get_direction = cy8c95x0_gpio_get_direction;
822 gc->get_multiple = cy8c95x0_gpio_get_multiple;
823 gc->set_multiple = cy8c95x0_gpio_set_multiple;
824 gc->set_config = gpiochip_generic_config,
825 gc->can_sleep = true;
826 gc->add_pin_ranges = cy8c95x0_add_pin_ranges;
829 gc->ngpio = chip->tpin;
831 gc->parent = chip->dev;
832 gc->owner = THIS_MODULE;
835 gc->label = dev_name(chip->dev);
837 return devm_gpiochip_add_data(chip->dev, gc, chip);
840 static void cy8c95x0_irq_mask(struct irq_data *d)
842 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
843 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
844 irq_hw_number_t hwirq = irqd_to_hwirq(d);
846 set_bit(hwirq, chip->irq_mask);
847 gpiochip_disable_irq(gc, hwirq);
850 static void cy8c95x0_irq_unmask(struct irq_data *d)
852 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
853 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
854 irq_hw_number_t hwirq = irqd_to_hwirq(d);
856 gpiochip_enable_irq(gc, hwirq);
857 clear_bit(hwirq, chip->irq_mask);
860 static void cy8c95x0_irq_bus_lock(struct irq_data *d)
862 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
863 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
865 mutex_lock(&chip->irq_lock);
868 static void cy8c95x0_irq_bus_sync_unlock(struct irq_data *d)
870 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
871 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
872 DECLARE_BITMAP(ones, MAX_LINE);
873 DECLARE_BITMAP(irq_mask, MAX_LINE);
874 DECLARE_BITMAP(reg_direction, MAX_LINE);
876 bitmap_fill(ones, MAX_LINE);
878 cy8c95x0_write_regs_mask(chip, CY8C95X0_INTMASK, chip->irq_mask, ones);
880 /* Switch direction to input if needed */
881 cy8c95x0_read_regs_mask(chip, CY8C95X0_DIRECTION, reg_direction, chip->irq_mask);
882 bitmap_or(irq_mask, chip->irq_mask, reg_direction, MAX_LINE);
883 bitmap_complement(irq_mask, irq_mask, MAX_LINE);
885 /* Look for any newly setup interrupt */
886 cy8c95x0_write_regs_mask(chip, CY8C95X0_DIRECTION, ones, irq_mask);
888 mutex_unlock(&chip->irq_lock);
891 static int cy8c95x0_irq_set_type(struct irq_data *d, unsigned int type)
893 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
894 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
895 irq_hw_number_t hwirq = irqd_to_hwirq(d);
896 unsigned int trig_type;
899 case IRQ_TYPE_EDGE_RISING:
900 case IRQ_TYPE_EDGE_FALLING:
901 case IRQ_TYPE_EDGE_BOTH:
904 case IRQ_TYPE_LEVEL_HIGH:
905 trig_type = IRQ_TYPE_EDGE_RISING;
907 case IRQ_TYPE_LEVEL_LOW:
908 trig_type = IRQ_TYPE_EDGE_FALLING;
911 dev_err(chip->dev, "irq %d: unsupported type %d\n", d->irq, type);
915 assign_bit(hwirq, chip->irq_trig_fall, trig_type & IRQ_TYPE_EDGE_FALLING);
916 assign_bit(hwirq, chip->irq_trig_raise, trig_type & IRQ_TYPE_EDGE_RISING);
917 assign_bit(hwirq, chip->irq_trig_low, type == IRQ_TYPE_LEVEL_LOW);
918 assign_bit(hwirq, chip->irq_trig_high, type == IRQ_TYPE_LEVEL_HIGH);
923 static void cy8c95x0_irq_shutdown(struct irq_data *d)
925 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
926 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
927 irq_hw_number_t hwirq = irqd_to_hwirq(d);
929 clear_bit(hwirq, chip->irq_trig_raise);
930 clear_bit(hwirq, chip->irq_trig_fall);
931 clear_bit(hwirq, chip->irq_trig_low);
932 clear_bit(hwirq, chip->irq_trig_high);
935 static const struct irq_chip cy8c95x0_irqchip = {
936 .name = "cy8c95x0-irq",
937 .irq_mask = cy8c95x0_irq_mask,
938 .irq_unmask = cy8c95x0_irq_unmask,
939 .irq_bus_lock = cy8c95x0_irq_bus_lock,
940 .irq_bus_sync_unlock = cy8c95x0_irq_bus_sync_unlock,
941 .irq_set_type = cy8c95x0_irq_set_type,
942 .irq_shutdown = cy8c95x0_irq_shutdown,
943 .flags = IRQCHIP_IMMUTABLE,
944 GPIOCHIP_IRQ_RESOURCE_HELPERS,
947 static bool cy8c95x0_irq_pending(struct cy8c95x0_pinctrl *chip, unsigned long *pending)
949 DECLARE_BITMAP(ones, MAX_LINE);
950 DECLARE_BITMAP(cur_stat, MAX_LINE);
951 DECLARE_BITMAP(new_stat, MAX_LINE);
952 DECLARE_BITMAP(trigger, MAX_LINE);
954 bitmap_fill(ones, MAX_LINE);
956 /* Read the current interrupt status from the device */
957 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INTSTATUS, trigger, ones))
960 /* Check latched inputs */
961 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, cur_stat, trigger))
964 /* Apply filter for rising/falling edge selection */
965 bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise,
968 bitmap_and(pending, new_stat, trigger, MAX_LINE);
970 return !bitmap_empty(pending, MAX_LINE);
973 static irqreturn_t cy8c95x0_irq_handler(int irq, void *devid)
975 struct cy8c95x0_pinctrl *chip = devid;
976 struct gpio_chip *gc = &chip->gpio_chip;
977 DECLARE_BITMAP(pending, MAX_LINE);
978 int nested_irq, level;
981 ret = cy8c95x0_irq_pending(chip, pending);
983 return IRQ_RETVAL(0);
986 for_each_set_bit(level, pending, MAX_LINE) {
987 /* Already accounted for 4bit gap in GPort2 */
988 nested_irq = irq_find_mapping(gc->irq.domain, level);
990 if (unlikely(nested_irq <= 0)) {
991 dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
995 if (test_bit(level, chip->irq_trig_low))
996 while (!cy8c95x0_gpio_get_value(gc, level))
997 handle_nested_irq(nested_irq);
998 else if (test_bit(level, chip->irq_trig_high))
999 while (cy8c95x0_gpio_get_value(gc, level))
1000 handle_nested_irq(nested_irq);
1002 handle_nested_irq(nested_irq);
1007 return IRQ_RETVAL(ret);
1010 static int cy8c95x0_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
1012 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1017 static const char *cy8c95x0_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
1020 return cy8c95x0_groups[group];
1023 static int cy8c95x0_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
1025 const unsigned int **pins,
1026 unsigned int *num_pins)
1028 *pins = &cy8c9560_pins[group].number;
1033 static const char *cy8c95x0_get_fname(unsigned int selector)
1041 static void cy8c95x0_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1044 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1045 DECLARE_BITMAP(mask, MAX_LINE);
1046 DECLARE_BITMAP(pwm, MAX_LINE);
1048 bitmap_zero(mask, MAX_LINE);
1049 __set_bit(pin, mask);
1051 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_PWMSEL, pwm, mask)) {
1052 seq_puts(s, "not available");
1056 seq_printf(s, "MODE:%s", cy8c95x0_get_fname(test_bit(pin, pwm)));
1059 static const struct pinctrl_ops cy8c95x0_pinctrl_ops = {
1060 .get_groups_count = cy8c95x0_pinctrl_get_groups_count,
1061 .get_group_name = cy8c95x0_pinctrl_get_group_name,
1062 .get_group_pins = cy8c95x0_pinctrl_get_group_pins,
1064 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1065 .dt_free_map = pinconf_generic_dt_free_map,
1067 .pin_dbg_show = cy8c95x0_pin_dbg_show,
1070 static const char *cy8c95x0_get_function_name(struct pinctrl_dev *pctldev, unsigned int selector)
1072 return cy8c95x0_get_fname(selector);
1075 static int cy8c95x0_get_functions_count(struct pinctrl_dev *pctldev)
1080 static int cy8c95x0_get_function_groups(struct pinctrl_dev *pctldev, unsigned int selector,
1081 const char * const **groups,
1082 unsigned int * const num_groups)
1084 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1086 *groups = cy8c95x0_groups;
1087 *num_groups = chip->tpin;
1091 static int cy8c95x0_set_mode(struct cy8c95x0_pinctrl *chip, unsigned int off, bool mode)
1093 u8 port = cypress_get_port(chip, off);
1094 u8 bit = cypress_get_pin_mask(chip, off);
1098 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
1102 return regmap_write_bits(chip->regmap, CY8C95X0_PWMSEL, bit, mode ? bit : 0);
1105 static int cy8c95x0_pinmux_mode(struct cy8c95x0_pinctrl *chip,
1106 unsigned int selector, unsigned int group)
1108 u8 port = cypress_get_port(chip, group);
1109 u8 bit = cypress_get_pin_mask(chip, group);
1112 ret = cy8c95x0_set_mode(chip, group, selector);
1119 /* Set direction to output & set output to 1 so that PWM can work */
1120 ret = regmap_write_bits(chip->regmap, CY8C95X0_DIRECTION, bit, bit);
1124 return regmap_write_bits(chip->regmap, CY8C95X0_OUTPUT_(port), bit, bit);
1127 static int cy8c95x0_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
1130 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1133 mutex_lock(&chip->i2c_lock);
1134 ret = cy8c95x0_pinmux_mode(chip, selector, group);
1135 mutex_unlock(&chip->i2c_lock);
1140 static int cy8c95x0_gpio_request_enable(struct pinctrl_dev *pctldev,
1141 struct pinctrl_gpio_range *range,
1144 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1147 mutex_lock(&chip->i2c_lock);
1148 ret = cy8c95x0_set_mode(chip, pin, false);
1149 mutex_unlock(&chip->i2c_lock);
1154 static int cy8c95x0_pinmux_direction(struct cy8c95x0_pinctrl *chip,
1155 unsigned int pin, bool input)
1157 u8 port = cypress_get_port(chip, pin);
1158 u8 bit = cypress_get_pin_mask(chip, pin);
1161 /* Select port... */
1162 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
1166 /* ...then direction */
1167 ret = regmap_write_bits(chip->regmap, CY8C95X0_DIRECTION, bit, input ? bit : 0);
1172 * Disable driving the pin by forcing it to HighZ. Only setting
1173 * the direction register isn't sufficient in Push-Pull mode.
1175 if (input && test_bit(pin, chip->push_pull)) {
1176 ret = regmap_write_bits(chip->regmap, CY8C95X0_DRV_HIZ, bit, bit);
1180 __clear_bit(pin, chip->push_pull);
1186 static int cy8c95x0_gpio_set_direction(struct pinctrl_dev *pctldev,
1187 struct pinctrl_gpio_range *range,
1188 unsigned int pin, bool input)
1190 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1193 mutex_lock(&chip->i2c_lock);
1194 ret = cy8c95x0_pinmux_direction(chip, pin, input);
1195 mutex_unlock(&chip->i2c_lock);
1200 static const struct pinmux_ops cy8c95x0_pmxops = {
1201 .get_functions_count = cy8c95x0_get_functions_count,
1202 .get_function_name = cy8c95x0_get_function_name,
1203 .get_function_groups = cy8c95x0_get_function_groups,
1204 .set_mux = cy8c95x0_set_mux,
1205 .gpio_request_enable = cy8c95x0_gpio_request_enable,
1206 .gpio_set_direction = cy8c95x0_gpio_set_direction,
1210 static int cy8c95x0_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1211 unsigned long *config)
1213 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1215 return cy8c95x0_gpio_get_pincfg(chip, pin, config);
1218 static int cy8c95x0_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1219 unsigned long *configs, unsigned int num_configs)
1221 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1225 for (i = 0; i < num_configs; i++) {
1226 ret = cy8c95x0_gpio_set_pincfg(chip, pin, configs[i]);
1234 static const struct pinconf_ops cy8c95x0_pinconf_ops = {
1235 .pin_config_get = cy8c95x0_pinconf_get,
1236 .pin_config_set = cy8c95x0_pinconf_set,
1240 static int cy8c95x0_irq_setup(struct cy8c95x0_pinctrl *chip, int irq)
1242 struct gpio_irq_chip *girq = &chip->gpio_chip.irq;
1243 DECLARE_BITMAP(pending_irqs, MAX_LINE);
1246 mutex_init(&chip->irq_lock);
1248 bitmap_zero(pending_irqs, MAX_LINE);
1250 /* Read IRQ status register to clear all pending interrupts */
1251 ret = cy8c95x0_irq_pending(chip, pending_irqs);
1253 dev_err(chip->dev, "failed to clear irq status register\n");
1257 /* Mask all interrupts */
1258 bitmap_fill(chip->irq_mask, MAX_LINE);
1260 gpio_irq_chip_set_chip(girq, &cy8c95x0_irqchip);
1262 /* This will let us handle the parent IRQ in the driver */
1263 girq->parent_handler = NULL;
1264 girq->num_parents = 0;
1265 girq->parents = NULL;
1266 girq->default_type = IRQ_TYPE_NONE;
1267 girq->handler = handle_simple_irq;
1268 girq->threaded = true;
1270 ret = devm_request_threaded_irq(chip->dev, irq,
1271 NULL, cy8c95x0_irq_handler,
1272 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_HIGH,
1273 dev_name(chip->dev), chip);
1275 dev_err(chip->dev, "failed to request irq %d\n", irq);
1278 dev_info(chip->dev, "Registered threaded IRQ\n");
1283 static int cy8c95x0_setup_pinctrl(struct cy8c95x0_pinctrl *chip)
1285 struct pinctrl_desc *pd = &chip->pinctrl_desc;
1287 pd->pctlops = &cy8c95x0_pinctrl_ops;
1288 pd->confops = &cy8c95x0_pinconf_ops;
1289 pd->pmxops = &cy8c95x0_pmxops;
1290 pd->name = dev_name(chip->dev);
1291 pd->pins = cy8c9560_pins;
1292 pd->npins = chip->tpin;
1293 pd->owner = THIS_MODULE;
1295 chip->pctldev = devm_pinctrl_register(chip->dev, pd, chip);
1296 if (IS_ERR(chip->pctldev))
1297 return dev_err_probe(chip->dev, PTR_ERR(chip->pctldev),
1298 "can't register controller\n");
1303 static int cy8c95x0_detect(struct i2c_client *client,
1304 struct i2c_board_info *info)
1306 struct i2c_adapter *adapter = client->adapter;
1310 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1313 ret = i2c_smbus_read_byte_data(client, CY8C95X0_DEVID);
1316 switch (ret & GENMASK(7, 4)) {
1318 name = cy8c95x0_id[0].name;
1321 name = cy8c95x0_id[1].name;
1324 name = cy8c95x0_id[2].name;
1330 dev_info(&client->dev, "Found a %s chip at 0x%02x.\n", name, client->addr);
1331 strscpy(info->type, name, I2C_NAME_SIZE);
1336 static int cy8c95x0_probe(struct i2c_client *client)
1338 struct cy8c95x0_pinctrl *chip;
1339 struct regulator *reg;
1342 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1346 chip->dev = &client->dev;
1348 /* Set the device type */
1349 chip->driver_data = (unsigned long)device_get_match_data(&client->dev);
1350 if (!chip->driver_data)
1351 chip->driver_data = i2c_match_id(cy8c95x0_id, client)->driver_data;
1352 if (!chip->driver_data)
1355 i2c_set_clientdata(client, chip);
1357 chip->tpin = chip->driver_data & CY8C95X0_GPIO_MASK;
1358 chip->nport = DIV_ROUND_UP(CY8C95X0_PIN_TO_OFFSET(chip->tpin), BANK_SZ);
1360 switch (chip->tpin) {
1362 strscpy(chip->name, cy8c95x0_id[0].name, I2C_NAME_SIZE);
1365 strscpy(chip->name, cy8c95x0_id[1].name, I2C_NAME_SIZE);
1368 strscpy(chip->name, cy8c95x0_id[2].name, I2C_NAME_SIZE);
1374 reg = devm_regulator_get(&client->dev, "vdd");
1376 if (PTR_ERR(reg) == -EPROBE_DEFER)
1377 return -EPROBE_DEFER;
1379 ret = regulator_enable(reg);
1381 dev_err(&client->dev, "failed to enable regulator vdd: %d\n", ret);
1384 chip->regulator = reg;
1387 /* bring the chip out of reset if reset pin is provided */
1388 chip->gpio_reset = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
1389 if (IS_ERR(chip->gpio_reset)) {
1390 ret = dev_err_probe(chip->dev, PTR_ERR(chip->gpio_reset),
1391 "Failed to get GPIO 'reset'\n");
1393 } else if (chip->gpio_reset) {
1394 usleep_range(1000, 2000);
1395 gpiod_set_value_cansleep(chip->gpio_reset, 0);
1396 usleep_range(250000, 300000);
1398 gpiod_set_consumer_name(chip->gpio_reset, "CY8C95X0 RESET");
1401 chip->regmap = devm_regmap_init_i2c(client, &cy8c95x0_i2c_regmap);
1402 if (IS_ERR(chip->regmap)) {
1403 ret = PTR_ERR(chip->regmap);
1407 bitmap_zero(chip->push_pull, MAX_LINE);
1408 bitmap_zero(chip->shiftmask, MAX_LINE);
1409 bitmap_set(chip->shiftmask, 0, 20);
1410 mutex_init(&chip->i2c_lock);
1412 if (dmi_first_match(cy8c95x0_dmi_acpi_irq_info)) {
1413 ret = cy8c95x0_acpi_get_irq(&client->dev);
1419 ret = cy8c95x0_irq_setup(chip, client->irq);
1424 ret = cy8c95x0_setup_pinctrl(chip);
1428 ret = cy8c95x0_setup_gpiochip(chip);
1435 if (!IS_ERR_OR_NULL(chip->regulator))
1436 regulator_disable(chip->regulator);
1440 static void cy8c95x0_remove(struct i2c_client *client)
1442 struct cy8c95x0_pinctrl *chip = i2c_get_clientdata(client);
1444 if (!IS_ERR_OR_NULL(chip->regulator))
1445 regulator_disable(chip->regulator);
1448 static const struct acpi_device_id cy8c95x0_acpi_ids[] = {
1452 MODULE_DEVICE_TABLE(acpi, cy8c95x0_acpi_ids);
1454 static struct i2c_driver cy8c95x0_driver = {
1456 .name = "cy8c95x0-pinctrl",
1457 .of_match_table = cy8c95x0_dt_ids,
1458 .acpi_match_table = cy8c95x0_acpi_ids,
1460 .probe = cy8c95x0_probe,
1461 .remove = cy8c95x0_remove,
1462 .id_table = cy8c95x0_id,
1463 .detect = cy8c95x0_detect,
1465 module_i2c_driver(cy8c95x0_driver);
1467 MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>");
1468 MODULE_AUTHOR("Naresh Solanki <naresh.solanki@9elements.com>");
1469 MODULE_DESCRIPTION("Pinctrl driver for CY8C95X0");
1470 MODULE_LICENSE("GPL");