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 int cy8c95x0_pinmux_direction(struct cy8c95x0_pinctrl *chip,
311 unsigned int pin, bool input);
313 static inline u8 cypress_get_port(struct cy8c95x0_pinctrl *chip, unsigned int pin)
315 /* Account for GPORT2 which only has 4 bits */
316 return CY8C95X0_PIN_TO_OFFSET(pin) / BANK_SZ;
319 static int cypress_get_pin_mask(struct cy8c95x0_pinctrl *chip, unsigned int pin)
321 /* Account for GPORT2 which only has 4 bits */
322 return BIT(CY8C95X0_PIN_TO_OFFSET(pin) % BANK_SZ);
325 static bool cy8c95x0_readable_register(struct device *dev, unsigned int reg)
335 static bool cy8c95x0_writeable_register(struct device *dev, unsigned int reg)
338 case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
349 static bool cy8c95x0_volatile_register(struct device *dev, unsigned int reg)
352 case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
353 case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
354 case CY8C95X0_INTMASK:
355 case CY8C95X0_INVERT:
356 case CY8C95X0_PWMSEL:
357 case CY8C95X0_DIRECTION:
358 case CY8C95X0_DRV_PU:
359 case CY8C95X0_DRV_PD:
360 case CY8C95X0_DRV_ODH:
361 case CY8C95X0_DRV_ODL:
362 case CY8C95X0_DRV_PP_FAST:
363 case CY8C95X0_DRV_PP_SLOW:
364 case CY8C95X0_DRV_HIZ:
371 static bool cy8c95x0_precious_register(struct device *dev, unsigned int reg)
374 case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
381 static const struct reg_default cy8c95x0_reg_defaults[] = {
382 { CY8C95X0_OUTPUT_(0), GENMASK(7, 0) },
383 { CY8C95X0_OUTPUT_(1), GENMASK(7, 0) },
384 { CY8C95X0_OUTPUT_(2), GENMASK(7, 0) },
385 { CY8C95X0_OUTPUT_(3), GENMASK(7, 0) },
386 { CY8C95X0_OUTPUT_(4), GENMASK(7, 0) },
387 { CY8C95X0_OUTPUT_(5), GENMASK(7, 0) },
388 { CY8C95X0_OUTPUT_(6), GENMASK(7, 0) },
389 { CY8C95X0_OUTPUT_(7), GENMASK(7, 0) },
390 { CY8C95X0_PORTSEL, 0 },
391 { CY8C95X0_PWMSEL, 0 },
394 static const struct regmap_config cy8c95x0_i2c_regmap = {
398 .reg_defaults = cy8c95x0_reg_defaults,
399 .num_reg_defaults = ARRAY_SIZE(cy8c95x0_reg_defaults),
401 .readable_reg = cy8c95x0_readable_register,
402 .writeable_reg = cy8c95x0_writeable_register,
403 .volatile_reg = cy8c95x0_volatile_register,
404 .precious_reg = cy8c95x0_precious_register,
406 .cache_type = REGCACHE_FLAT,
407 .max_register = CY8C95X0_COMMAND,
410 static int cy8c95x0_write_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
411 unsigned long *val, unsigned long *mask)
413 DECLARE_BITMAP(tmask, MAX_LINE);
414 DECLARE_BITMAP(tval, MAX_LINE);
420 /* Add the 4 bit gap of Gport2 */
421 bitmap_andnot(tmask, mask, chip->shiftmask, MAX_LINE);
422 bitmap_shift_left(tmask, tmask, 4, MAX_LINE);
423 bitmap_replace(tmask, tmask, mask, chip->shiftmask, BANK_SZ * 3);
425 bitmap_andnot(tval, val, chip->shiftmask, MAX_LINE);
426 bitmap_shift_left(tval, tval, 4, MAX_LINE);
427 bitmap_replace(tval, tval, val, chip->shiftmask, BANK_SZ * 3);
429 mutex_lock(&chip->i2c_lock);
430 for (i = 0; i < chip->nport; i++) {
431 /* Skip over unused banks */
432 bits = bitmap_get_value8(tmask, i * BANK_SZ);
437 /* Muxed registers */
438 case CY8C95X0_INTMASK:
439 case CY8C95X0_PWMSEL:
440 case CY8C95X0_INVERT:
441 case CY8C95X0_DIRECTION:
442 case CY8C95X0_DRV_PU:
443 case CY8C95X0_DRV_PD:
444 case CY8C95X0_DRV_ODH:
445 case CY8C95X0_DRV_ODL:
446 case CY8C95X0_DRV_PP_FAST:
447 case CY8C95X0_DRV_PP_SLOW:
448 case CY8C95X0_DRV_HIZ:
449 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, i);
454 /* Direct access registers */
456 case CY8C95X0_OUTPUT:
457 case CY8C95X0_INTSTATUS:
465 write_val = bitmap_get_value8(tval, i * BANK_SZ);
467 ret = regmap_update_bits(chip->regmap, off, bits, write_val);
472 mutex_unlock(&chip->i2c_lock);
475 dev_err(chip->dev, "failed writing register %d: err %d\n", off, ret);
480 static int cy8c95x0_read_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
481 unsigned long *val, unsigned long *mask)
483 DECLARE_BITMAP(tmask, MAX_LINE);
484 DECLARE_BITMAP(tval, MAX_LINE);
485 DECLARE_BITMAP(tmp, MAX_LINE);
491 /* Add the 4 bit gap of Gport2 */
492 bitmap_andnot(tmask, mask, chip->shiftmask, MAX_LINE);
493 bitmap_shift_left(tmask, tmask, 4, MAX_LINE);
494 bitmap_replace(tmask, tmask, mask, chip->shiftmask, BANK_SZ * 3);
496 bitmap_andnot(tval, val, chip->shiftmask, MAX_LINE);
497 bitmap_shift_left(tval, tval, 4, MAX_LINE);
498 bitmap_replace(tval, tval, val, chip->shiftmask, BANK_SZ * 3);
500 mutex_lock(&chip->i2c_lock);
501 for (i = 0; i < chip->nport; i++) {
502 /* Skip over unused banks */
503 bits = bitmap_get_value8(tmask, i * BANK_SZ);
508 /* Muxed registers */
509 case CY8C95X0_INTMASK:
510 case CY8C95X0_PWMSEL:
511 case CY8C95X0_INVERT:
512 case CY8C95X0_DIRECTION:
513 case CY8C95X0_DRV_PU:
514 case CY8C95X0_DRV_PD:
515 case CY8C95X0_DRV_ODH:
516 case CY8C95X0_DRV_ODL:
517 case CY8C95X0_DRV_PP_FAST:
518 case CY8C95X0_DRV_PP_SLOW:
519 case CY8C95X0_DRV_HIZ:
520 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, i);
525 /* Direct access registers */
527 case CY8C95X0_OUTPUT:
528 case CY8C95X0_INTSTATUS:
536 ret = regmap_read(chip->regmap, off, &read_val);
541 read_val |= bitmap_get_value8(tval, i * BANK_SZ) & ~bits;
542 bitmap_set_value8(tval, read_val, i * BANK_SZ);
545 /* Fill the 4 bit gap of Gport2 */
546 bitmap_shift_right(tmp, tval, 4, MAX_LINE);
547 bitmap_replace(val, tmp, tval, chip->shiftmask, MAX_LINE);
550 mutex_unlock(&chip->i2c_lock);
553 dev_err(chip->dev, "failed reading register %d: err %d\n", off, ret);
558 static int cy8c95x0_gpio_direction_input(struct gpio_chip *gc, unsigned int off)
560 return pinctrl_gpio_direction_input(gc->base + off);
563 static int cy8c95x0_gpio_direction_output(struct gpio_chip *gc,
564 unsigned int off, int val)
566 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
567 u8 port = cypress_get_port(chip, off);
568 u8 outreg = CY8C95X0_OUTPUT_(port);
569 u8 bit = cypress_get_pin_mask(chip, off);
572 /* Set output level */
573 ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
577 return pinctrl_gpio_direction_output(gc->base + off);
580 static int cy8c95x0_gpio_get_value(struct gpio_chip *gc, unsigned int off)
582 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
583 u8 inreg = CY8C95X0_INPUT_(cypress_get_port(chip, off));
584 u8 bit = cypress_get_pin_mask(chip, off);
588 ret = regmap_read(chip->regmap, inreg, ®_val);
592 * Diagnostic already emitted; that's all we should
593 * do unless gpio_*_value_cansleep() calls become different
594 * from their nonsleeping siblings (and report faults).
599 return !!(reg_val & bit);
602 static void cy8c95x0_gpio_set_value(struct gpio_chip *gc, unsigned int off,
605 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
606 u8 outreg = CY8C95X0_OUTPUT_(cypress_get_port(chip, off));
607 u8 bit = cypress_get_pin_mask(chip, off);
609 regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
612 static int cy8c95x0_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
614 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
615 u8 port = cypress_get_port(chip, off);
616 u8 bit = cypress_get_pin_mask(chip, off);
620 mutex_lock(&chip->i2c_lock);
622 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
626 ret = regmap_read(chip->regmap, CY8C95X0_DIRECTION, ®_val);
630 mutex_unlock(&chip->i2c_lock);
633 return GPIO_LINE_DIRECTION_IN;
635 return GPIO_LINE_DIRECTION_OUT;
637 mutex_unlock(&chip->i2c_lock);
641 static int cy8c95x0_gpio_get_pincfg(struct cy8c95x0_pinctrl *chip,
643 unsigned long *config)
645 enum pin_config_param param = pinconf_to_config_param(*config);
646 u8 port = cypress_get_port(chip, off);
647 u8 bit = cypress_get_pin_mask(chip, off);
653 mutex_lock(&chip->i2c_lock);
656 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
661 case PIN_CONFIG_BIAS_PULL_UP:
662 reg = CY8C95X0_DRV_PU;
664 case PIN_CONFIG_BIAS_PULL_DOWN:
665 reg = CY8C95X0_DRV_PD;
667 case PIN_CONFIG_BIAS_DISABLE:
668 reg = CY8C95X0_DRV_HIZ;
670 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
671 reg = CY8C95X0_DRV_ODL;
673 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
674 reg = CY8C95X0_DRV_ODH;
676 case PIN_CONFIG_DRIVE_PUSH_PULL:
677 reg = CY8C95X0_DRV_PP_FAST;
679 case PIN_CONFIG_INPUT_ENABLE:
680 reg = CY8C95X0_DIRECTION;
682 case PIN_CONFIG_MODE_PWM:
683 reg = CY8C95X0_PWMSEL;
685 case PIN_CONFIG_OUTPUT:
686 reg = CY8C95X0_OUTPUT_(port);
688 case PIN_CONFIG_OUTPUT_ENABLE:
689 reg = CY8C95X0_DIRECTION;
692 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
693 case PIN_CONFIG_BIAS_BUS_HOLD:
694 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
695 case PIN_CONFIG_DRIVE_STRENGTH:
696 case PIN_CONFIG_DRIVE_STRENGTH_UA:
697 case PIN_CONFIG_INPUT_DEBOUNCE:
698 case PIN_CONFIG_INPUT_SCHMITT:
699 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
700 case PIN_CONFIG_MODE_LOW_POWER:
701 case PIN_CONFIG_PERSIST_STATE:
702 case PIN_CONFIG_POWER_SOURCE:
703 case PIN_CONFIG_SKEW_DELAY:
704 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
705 case PIN_CONFIG_SLEW_RATE:
711 * Writing 1 to one of the drive mode registers will automatically
712 * clear conflicting set bits in the other drive mode registers.
714 ret = regmap_read(chip->regmap, reg, ®_val);
717 if (param == PIN_CONFIG_OUTPUT_ENABLE)
720 *config = pinconf_to_config_packed(param, (u16)arg);
722 mutex_unlock(&chip->i2c_lock);
727 static int cy8c95x0_gpio_set_pincfg(struct cy8c95x0_pinctrl *chip,
729 unsigned long config)
731 u8 port = cypress_get_port(chip, off);
732 u8 bit = cypress_get_pin_mask(chip, off);
733 unsigned long param = pinconf_to_config_param(config);
734 unsigned long arg = pinconf_to_config_argument(config);
738 mutex_lock(&chip->i2c_lock);
741 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
746 case PIN_CONFIG_BIAS_PULL_UP:
747 __clear_bit(off, chip->push_pull);
748 reg = CY8C95X0_DRV_PU;
750 case PIN_CONFIG_BIAS_PULL_DOWN:
751 __clear_bit(off, chip->push_pull);
752 reg = CY8C95X0_DRV_PD;
754 case PIN_CONFIG_BIAS_DISABLE:
755 __clear_bit(off, chip->push_pull);
756 reg = CY8C95X0_DRV_HIZ;
758 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
759 __clear_bit(off, chip->push_pull);
760 reg = CY8C95X0_DRV_ODL;
762 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
763 __clear_bit(off, chip->push_pull);
764 reg = CY8C95X0_DRV_ODH;
766 case PIN_CONFIG_DRIVE_PUSH_PULL:
767 __set_bit(off, chip->push_pull);
768 reg = CY8C95X0_DRV_PP_FAST;
770 case PIN_CONFIG_MODE_PWM:
771 reg = CY8C95X0_PWMSEL;
773 case PIN_CONFIG_OUTPUT_ENABLE:
774 ret = cy8c95x0_pinmux_direction(chip, off, !arg);
776 case PIN_CONFIG_INPUT_ENABLE:
777 ret = cy8c95x0_pinmux_direction(chip, off, arg);
784 * Writing 1 to one of the drive mode registers will automatically
785 * clear conflicting set bits in the other drive mode registers.
787 ret = regmap_write_bits(chip->regmap, reg, bit, bit);
790 mutex_unlock(&chip->i2c_lock);
794 static int cy8c95x0_gpio_get_multiple(struct gpio_chip *gc,
795 unsigned long *mask, unsigned long *bits)
797 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
799 return cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, bits, mask);
802 static void cy8c95x0_gpio_set_multiple(struct gpio_chip *gc,
803 unsigned long *mask, unsigned long *bits)
805 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
807 cy8c95x0_write_regs_mask(chip, CY8C95X0_OUTPUT, bits, mask);
810 static int cy8c95x0_add_pin_ranges(struct gpio_chip *gc)
812 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
813 struct device *dev = chip->dev;
816 ret = gpiochip_add_pin_range(gc, dev_name(dev), 0, 0, chip->tpin);
818 dev_err(dev, "failed to add GPIO pin range\n");
823 static int cy8c95x0_setup_gpiochip(struct cy8c95x0_pinctrl *chip)
825 struct gpio_chip *gc = &chip->gpio_chip;
827 gc->request = gpiochip_generic_request;
828 gc->free = gpiochip_generic_free;
829 gc->direction_input = cy8c95x0_gpio_direction_input;
830 gc->direction_output = cy8c95x0_gpio_direction_output;
831 gc->get = cy8c95x0_gpio_get_value;
832 gc->set = cy8c95x0_gpio_set_value;
833 gc->get_direction = cy8c95x0_gpio_get_direction;
834 gc->get_multiple = cy8c95x0_gpio_get_multiple;
835 gc->set_multiple = cy8c95x0_gpio_set_multiple;
836 gc->set_config = gpiochip_generic_config;
837 gc->can_sleep = true;
838 gc->add_pin_ranges = cy8c95x0_add_pin_ranges;
841 gc->ngpio = chip->tpin;
843 gc->parent = chip->dev;
844 gc->owner = THIS_MODULE;
847 gc->label = dev_name(chip->dev);
849 return devm_gpiochip_add_data(chip->dev, gc, chip);
852 static void cy8c95x0_irq_mask(struct irq_data *d)
854 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
855 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
856 irq_hw_number_t hwirq = irqd_to_hwirq(d);
858 set_bit(hwirq, chip->irq_mask);
859 gpiochip_disable_irq(gc, hwirq);
862 static void cy8c95x0_irq_unmask(struct irq_data *d)
864 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
865 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
866 irq_hw_number_t hwirq = irqd_to_hwirq(d);
868 gpiochip_enable_irq(gc, hwirq);
869 clear_bit(hwirq, chip->irq_mask);
872 static void cy8c95x0_irq_bus_lock(struct irq_data *d)
874 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
875 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
877 mutex_lock(&chip->irq_lock);
880 static void cy8c95x0_irq_bus_sync_unlock(struct irq_data *d)
882 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
883 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
884 DECLARE_BITMAP(ones, MAX_LINE);
885 DECLARE_BITMAP(irq_mask, MAX_LINE);
886 DECLARE_BITMAP(reg_direction, MAX_LINE);
888 bitmap_fill(ones, MAX_LINE);
890 cy8c95x0_write_regs_mask(chip, CY8C95X0_INTMASK, chip->irq_mask, ones);
892 /* Switch direction to input if needed */
893 cy8c95x0_read_regs_mask(chip, CY8C95X0_DIRECTION, reg_direction, chip->irq_mask);
894 bitmap_or(irq_mask, chip->irq_mask, reg_direction, MAX_LINE);
895 bitmap_complement(irq_mask, irq_mask, MAX_LINE);
897 /* Look for any newly setup interrupt */
898 cy8c95x0_write_regs_mask(chip, CY8C95X0_DIRECTION, ones, irq_mask);
900 mutex_unlock(&chip->irq_lock);
903 static int cy8c95x0_irq_set_type(struct irq_data *d, unsigned int type)
905 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
906 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
907 irq_hw_number_t hwirq = irqd_to_hwirq(d);
908 unsigned int trig_type;
911 case IRQ_TYPE_EDGE_RISING:
912 case IRQ_TYPE_EDGE_FALLING:
913 case IRQ_TYPE_EDGE_BOTH:
916 case IRQ_TYPE_LEVEL_HIGH:
917 trig_type = IRQ_TYPE_EDGE_RISING;
919 case IRQ_TYPE_LEVEL_LOW:
920 trig_type = IRQ_TYPE_EDGE_FALLING;
923 dev_err(chip->dev, "irq %d: unsupported type %d\n", d->irq, type);
927 assign_bit(hwirq, chip->irq_trig_fall, trig_type & IRQ_TYPE_EDGE_FALLING);
928 assign_bit(hwirq, chip->irq_trig_raise, trig_type & IRQ_TYPE_EDGE_RISING);
929 assign_bit(hwirq, chip->irq_trig_low, type == IRQ_TYPE_LEVEL_LOW);
930 assign_bit(hwirq, chip->irq_trig_high, type == IRQ_TYPE_LEVEL_HIGH);
935 static void cy8c95x0_irq_shutdown(struct irq_data *d)
937 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
938 struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
939 irq_hw_number_t hwirq = irqd_to_hwirq(d);
941 clear_bit(hwirq, chip->irq_trig_raise);
942 clear_bit(hwirq, chip->irq_trig_fall);
943 clear_bit(hwirq, chip->irq_trig_low);
944 clear_bit(hwirq, chip->irq_trig_high);
947 static const struct irq_chip cy8c95x0_irqchip = {
948 .name = "cy8c95x0-irq",
949 .irq_mask = cy8c95x0_irq_mask,
950 .irq_unmask = cy8c95x0_irq_unmask,
951 .irq_bus_lock = cy8c95x0_irq_bus_lock,
952 .irq_bus_sync_unlock = cy8c95x0_irq_bus_sync_unlock,
953 .irq_set_type = cy8c95x0_irq_set_type,
954 .irq_shutdown = cy8c95x0_irq_shutdown,
955 .flags = IRQCHIP_IMMUTABLE,
956 GPIOCHIP_IRQ_RESOURCE_HELPERS,
959 static bool cy8c95x0_irq_pending(struct cy8c95x0_pinctrl *chip, unsigned long *pending)
961 DECLARE_BITMAP(ones, MAX_LINE);
962 DECLARE_BITMAP(cur_stat, MAX_LINE);
963 DECLARE_BITMAP(new_stat, MAX_LINE);
964 DECLARE_BITMAP(trigger, MAX_LINE);
966 bitmap_fill(ones, MAX_LINE);
968 /* Read the current interrupt status from the device */
969 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INTSTATUS, trigger, ones))
972 /* Check latched inputs */
973 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, cur_stat, trigger))
976 /* Apply filter for rising/falling edge selection */
977 bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise,
980 bitmap_and(pending, new_stat, trigger, MAX_LINE);
982 return !bitmap_empty(pending, MAX_LINE);
985 static irqreturn_t cy8c95x0_irq_handler(int irq, void *devid)
987 struct cy8c95x0_pinctrl *chip = devid;
988 struct gpio_chip *gc = &chip->gpio_chip;
989 DECLARE_BITMAP(pending, MAX_LINE);
990 int nested_irq, level;
993 ret = cy8c95x0_irq_pending(chip, pending);
995 return IRQ_RETVAL(0);
998 for_each_set_bit(level, pending, MAX_LINE) {
999 /* Already accounted for 4bit gap in GPort2 */
1000 nested_irq = irq_find_mapping(gc->irq.domain, level);
1002 if (unlikely(nested_irq <= 0)) {
1003 dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
1007 if (test_bit(level, chip->irq_trig_low))
1008 while (!cy8c95x0_gpio_get_value(gc, level))
1009 handle_nested_irq(nested_irq);
1010 else if (test_bit(level, chip->irq_trig_high))
1011 while (cy8c95x0_gpio_get_value(gc, level))
1012 handle_nested_irq(nested_irq);
1014 handle_nested_irq(nested_irq);
1019 return IRQ_RETVAL(ret);
1022 static int cy8c95x0_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
1024 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1029 static const char *cy8c95x0_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
1032 return cy8c95x0_groups[group];
1035 static int cy8c95x0_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
1037 const unsigned int **pins,
1038 unsigned int *num_pins)
1040 *pins = &cy8c9560_pins[group].number;
1045 static const char *cy8c95x0_get_fname(unsigned int selector)
1053 static void cy8c95x0_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1056 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1057 DECLARE_BITMAP(mask, MAX_LINE);
1058 DECLARE_BITMAP(pwm, MAX_LINE);
1060 bitmap_zero(mask, MAX_LINE);
1061 __set_bit(pin, mask);
1063 if (cy8c95x0_read_regs_mask(chip, CY8C95X0_PWMSEL, pwm, mask)) {
1064 seq_puts(s, "not available");
1068 seq_printf(s, "MODE:%s", cy8c95x0_get_fname(test_bit(pin, pwm)));
1071 static const struct pinctrl_ops cy8c95x0_pinctrl_ops = {
1072 .get_groups_count = cy8c95x0_pinctrl_get_groups_count,
1073 .get_group_name = cy8c95x0_pinctrl_get_group_name,
1074 .get_group_pins = cy8c95x0_pinctrl_get_group_pins,
1076 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1077 .dt_free_map = pinconf_generic_dt_free_map,
1079 .pin_dbg_show = cy8c95x0_pin_dbg_show,
1082 static const char *cy8c95x0_get_function_name(struct pinctrl_dev *pctldev, unsigned int selector)
1084 return cy8c95x0_get_fname(selector);
1087 static int cy8c95x0_get_functions_count(struct pinctrl_dev *pctldev)
1092 static int cy8c95x0_get_function_groups(struct pinctrl_dev *pctldev, unsigned int selector,
1093 const char * const **groups,
1094 unsigned int * const num_groups)
1096 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1098 *groups = cy8c95x0_groups;
1099 *num_groups = chip->tpin;
1103 static int cy8c95x0_set_mode(struct cy8c95x0_pinctrl *chip, unsigned int off, bool mode)
1105 u8 port = cypress_get_port(chip, off);
1106 u8 bit = cypress_get_pin_mask(chip, off);
1110 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
1114 return regmap_write_bits(chip->regmap, CY8C95X0_PWMSEL, bit, mode ? bit : 0);
1117 static int cy8c95x0_pinmux_mode(struct cy8c95x0_pinctrl *chip,
1118 unsigned int selector, unsigned int group)
1120 u8 port = cypress_get_port(chip, group);
1121 u8 bit = cypress_get_pin_mask(chip, group);
1124 ret = cy8c95x0_set_mode(chip, group, selector);
1131 /* Set direction to output & set output to 1 so that PWM can work */
1132 ret = regmap_write_bits(chip->regmap, CY8C95X0_DIRECTION, bit, bit);
1136 return regmap_write_bits(chip->regmap, CY8C95X0_OUTPUT_(port), bit, bit);
1139 static int cy8c95x0_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
1142 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1145 mutex_lock(&chip->i2c_lock);
1146 ret = cy8c95x0_pinmux_mode(chip, selector, group);
1147 mutex_unlock(&chip->i2c_lock);
1152 static int cy8c95x0_gpio_request_enable(struct pinctrl_dev *pctldev,
1153 struct pinctrl_gpio_range *range,
1156 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1159 mutex_lock(&chip->i2c_lock);
1160 ret = cy8c95x0_set_mode(chip, pin, false);
1161 mutex_unlock(&chip->i2c_lock);
1166 static int cy8c95x0_pinmux_direction(struct cy8c95x0_pinctrl *chip,
1167 unsigned int pin, bool input)
1169 u8 port = cypress_get_port(chip, pin);
1170 u8 bit = cypress_get_pin_mask(chip, pin);
1173 /* Select port... */
1174 ret = regmap_write(chip->regmap, CY8C95X0_PORTSEL, port);
1178 /* ...then direction */
1179 ret = regmap_write_bits(chip->regmap, CY8C95X0_DIRECTION, bit, input ? bit : 0);
1184 * Disable driving the pin by forcing it to HighZ. Only setting
1185 * the direction register isn't sufficient in Push-Pull mode.
1187 if (input && test_bit(pin, chip->push_pull)) {
1188 ret = regmap_write_bits(chip->regmap, CY8C95X0_DRV_HIZ, bit, bit);
1192 __clear_bit(pin, chip->push_pull);
1198 static int cy8c95x0_gpio_set_direction(struct pinctrl_dev *pctldev,
1199 struct pinctrl_gpio_range *range,
1200 unsigned int pin, bool input)
1202 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1205 mutex_lock(&chip->i2c_lock);
1206 ret = cy8c95x0_pinmux_direction(chip, pin, input);
1207 mutex_unlock(&chip->i2c_lock);
1212 static const struct pinmux_ops cy8c95x0_pmxops = {
1213 .get_functions_count = cy8c95x0_get_functions_count,
1214 .get_function_name = cy8c95x0_get_function_name,
1215 .get_function_groups = cy8c95x0_get_function_groups,
1216 .set_mux = cy8c95x0_set_mux,
1217 .gpio_request_enable = cy8c95x0_gpio_request_enable,
1218 .gpio_set_direction = cy8c95x0_gpio_set_direction,
1222 static int cy8c95x0_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1223 unsigned long *config)
1225 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1227 return cy8c95x0_gpio_get_pincfg(chip, pin, config);
1230 static int cy8c95x0_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1231 unsigned long *configs, unsigned int num_configs)
1233 struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
1237 for (i = 0; i < num_configs; i++) {
1238 ret = cy8c95x0_gpio_set_pincfg(chip, pin, configs[i]);
1246 static const struct pinconf_ops cy8c95x0_pinconf_ops = {
1247 .pin_config_get = cy8c95x0_pinconf_get,
1248 .pin_config_set = cy8c95x0_pinconf_set,
1252 static int cy8c95x0_irq_setup(struct cy8c95x0_pinctrl *chip, int irq)
1254 struct gpio_irq_chip *girq = &chip->gpio_chip.irq;
1255 DECLARE_BITMAP(pending_irqs, MAX_LINE);
1258 mutex_init(&chip->irq_lock);
1260 bitmap_zero(pending_irqs, MAX_LINE);
1262 /* Read IRQ status register to clear all pending interrupts */
1263 ret = cy8c95x0_irq_pending(chip, pending_irqs);
1265 dev_err(chip->dev, "failed to clear irq status register\n");
1269 /* Mask all interrupts */
1270 bitmap_fill(chip->irq_mask, MAX_LINE);
1272 gpio_irq_chip_set_chip(girq, &cy8c95x0_irqchip);
1274 /* This will let us handle the parent IRQ in the driver */
1275 girq->parent_handler = NULL;
1276 girq->num_parents = 0;
1277 girq->parents = NULL;
1278 girq->default_type = IRQ_TYPE_NONE;
1279 girq->handler = handle_simple_irq;
1280 girq->threaded = true;
1282 ret = devm_request_threaded_irq(chip->dev, irq,
1283 NULL, cy8c95x0_irq_handler,
1284 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_HIGH,
1285 dev_name(chip->dev), chip);
1287 dev_err(chip->dev, "failed to request irq %d\n", irq);
1290 dev_info(chip->dev, "Registered threaded IRQ\n");
1295 static int cy8c95x0_setup_pinctrl(struct cy8c95x0_pinctrl *chip)
1297 struct pinctrl_desc *pd = &chip->pinctrl_desc;
1299 pd->pctlops = &cy8c95x0_pinctrl_ops;
1300 pd->confops = &cy8c95x0_pinconf_ops;
1301 pd->pmxops = &cy8c95x0_pmxops;
1302 pd->name = dev_name(chip->dev);
1303 pd->pins = cy8c9560_pins;
1304 pd->npins = chip->tpin;
1305 pd->owner = THIS_MODULE;
1307 chip->pctldev = devm_pinctrl_register(chip->dev, pd, chip);
1308 if (IS_ERR(chip->pctldev))
1309 return dev_err_probe(chip->dev, PTR_ERR(chip->pctldev),
1310 "can't register controller\n");
1315 static int cy8c95x0_detect(struct i2c_client *client,
1316 struct i2c_board_info *info)
1318 struct i2c_adapter *adapter = client->adapter;
1322 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1325 ret = i2c_smbus_read_byte_data(client, CY8C95X0_DEVID);
1328 switch (ret & GENMASK(7, 4)) {
1330 name = cy8c95x0_id[0].name;
1333 name = cy8c95x0_id[1].name;
1336 name = cy8c95x0_id[2].name;
1342 dev_info(&client->dev, "Found a %s chip at 0x%02x.\n", name, client->addr);
1343 strscpy(info->type, name, I2C_NAME_SIZE);
1348 static int cy8c95x0_probe(struct i2c_client *client)
1350 struct cy8c95x0_pinctrl *chip;
1351 struct regulator *reg;
1354 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1358 chip->dev = &client->dev;
1360 /* Set the device type */
1361 chip->driver_data = (unsigned long)device_get_match_data(&client->dev);
1362 if (!chip->driver_data)
1363 chip->driver_data = i2c_match_id(cy8c95x0_id, client)->driver_data;
1364 if (!chip->driver_data)
1367 i2c_set_clientdata(client, chip);
1369 chip->tpin = chip->driver_data & CY8C95X0_GPIO_MASK;
1370 chip->nport = DIV_ROUND_UP(CY8C95X0_PIN_TO_OFFSET(chip->tpin), BANK_SZ);
1372 switch (chip->tpin) {
1374 strscpy(chip->name, cy8c95x0_id[0].name, I2C_NAME_SIZE);
1377 strscpy(chip->name, cy8c95x0_id[1].name, I2C_NAME_SIZE);
1380 strscpy(chip->name, cy8c95x0_id[2].name, I2C_NAME_SIZE);
1386 reg = devm_regulator_get(&client->dev, "vdd");
1388 if (PTR_ERR(reg) == -EPROBE_DEFER)
1389 return -EPROBE_DEFER;
1391 ret = regulator_enable(reg);
1393 dev_err(&client->dev, "failed to enable regulator vdd: %d\n", ret);
1396 chip->regulator = reg;
1399 /* bring the chip out of reset if reset pin is provided */
1400 chip->gpio_reset = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
1401 if (IS_ERR(chip->gpio_reset)) {
1402 ret = dev_err_probe(chip->dev, PTR_ERR(chip->gpio_reset),
1403 "Failed to get GPIO 'reset'\n");
1405 } else if (chip->gpio_reset) {
1406 usleep_range(1000, 2000);
1407 gpiod_set_value_cansleep(chip->gpio_reset, 0);
1408 usleep_range(250000, 300000);
1410 gpiod_set_consumer_name(chip->gpio_reset, "CY8C95X0 RESET");
1413 chip->regmap = devm_regmap_init_i2c(client, &cy8c95x0_i2c_regmap);
1414 if (IS_ERR(chip->regmap)) {
1415 ret = PTR_ERR(chip->regmap);
1419 bitmap_zero(chip->push_pull, MAX_LINE);
1420 bitmap_zero(chip->shiftmask, MAX_LINE);
1421 bitmap_set(chip->shiftmask, 0, 20);
1422 mutex_init(&chip->i2c_lock);
1424 if (dmi_first_match(cy8c95x0_dmi_acpi_irq_info)) {
1425 ret = cy8c95x0_acpi_get_irq(&client->dev);
1431 ret = cy8c95x0_irq_setup(chip, client->irq);
1436 ret = cy8c95x0_setup_pinctrl(chip);
1440 ret = cy8c95x0_setup_gpiochip(chip);
1447 if (!IS_ERR_OR_NULL(chip->regulator))
1448 regulator_disable(chip->regulator);
1452 static void cy8c95x0_remove(struct i2c_client *client)
1454 struct cy8c95x0_pinctrl *chip = i2c_get_clientdata(client);
1456 if (!IS_ERR_OR_NULL(chip->regulator))
1457 regulator_disable(chip->regulator);
1460 static const struct acpi_device_id cy8c95x0_acpi_ids[] = {
1464 MODULE_DEVICE_TABLE(acpi, cy8c95x0_acpi_ids);
1466 static struct i2c_driver cy8c95x0_driver = {
1468 .name = "cy8c95x0-pinctrl",
1469 .of_match_table = cy8c95x0_dt_ids,
1470 .acpi_match_table = cy8c95x0_acpi_ids,
1472 .probe = cy8c95x0_probe,
1473 .remove = cy8c95x0_remove,
1474 .id_table = cy8c95x0_id,
1475 .detect = cy8c95x0_detect,
1477 module_i2c_driver(cy8c95x0_driver);
1479 MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>");
1480 MODULE_AUTHOR("Naresh Solanki <naresh.solanki@9elements.com>");
1481 MODULE_DESCRIPTION("Pinctrl driver for CY8C95X0");
1482 MODULE_LICENSE("GPL");