1 // SPDX-License-Identifier: GPL-2.0-only
3 * Awinic AW9523B i2c pin controller driver
4 * Copyright (c) 2020, AngeloGioacchino Del Regno
5 * <angelogioacchino.delregno@somainline.org>
8 #include <linux/bitfield.h>
9 #include <linux/regmap.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <linux/of_device.h>
19 #include <linux/version.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
25 #include <linux/pinctrl/pinconf-generic.h>
26 #include <linux/regulator/consumer.h>
30 #include "pinctrl-utils.h"
32 #define AW9523_MAX_FUNCS 2
33 #define AW9523_NUM_PORTS 2
34 #define AW9523_PINS_PER_PORT 8
37 * HW needs at least 20uS for reset and at least 1-2uS to recover from
38 * reset, but we have to account for eventual board quirks, if any:
39 * for this reason, keep reset asserted for 50uS and wait for 20uS
40 * to recover from the reset.
42 #define AW9523_HW_RESET_US 50
43 #define AW9523_HW_RESET_RECOVERY_US 20
45 /* Port 0: P0_0...P0_7 - Port 1: P1_0...P1_7 */
46 #define AW9523_PIN_TO_PORT(pin) (pin >> 3)
47 #define AW9523_REG_IN_STATE(pin) (0x00 + AW9523_PIN_TO_PORT(pin))
48 #define AW9523_REG_OUT_STATE(pin) (0x02 + AW9523_PIN_TO_PORT(pin))
49 #define AW9523_REG_CONF_STATE(pin) (0x04 + AW9523_PIN_TO_PORT(pin))
50 #define AW9523_REG_INTR_DIS(pin) (0x06 + AW9523_PIN_TO_PORT(pin))
51 #define AW9523_REG_CHIPID 0x10
52 #define AW9523_VAL_EXPECTED_CHIPID 0x23
54 #define AW9523_REG_GCR 0x11
55 #define AW9523_GCR_ISEL_MASK GENMASK(0, 1)
56 #define AW9523_GCR_GPOMD_MASK BIT(4)
58 #define AW9523_REG_PORT_MODE(pin) (0x12 + AW9523_PIN_TO_PORT(pin))
59 #define AW9523_REG_SOFT_RESET 0x7f
60 #define AW9523_VAL_RESET 0x00
63 * struct aw9523_irq - Interrupt controller structure
64 * @lock: mutex locking for the irq bus
65 * @irqchip: structure holding irqchip params
66 * @cached_gpio: stores the previous gpio status for bit comparison
70 struct irq_chip
*irqchip
;
75 * struct aw9523_pinmux - Pin mux params
76 * @name: Name of the mux
77 * @grps: Groups of the mux
78 * @num_grps: Number of groups (sizeof array grps)
80 struct aw9523_pinmux
{
82 const char * const *grps
;
87 * struct aw9523 - Main driver structure
89 * @regmap: regmap handle for current device
90 * @i2c_lock: Mutex lock for i2c operations
91 * @reset_gpio: Hardware reset (RSTN) signal GPIO
92 * @vio_vreg: VCC regulator (Optional)
93 * @pctl: pinctrl handle for current device
94 * @gpio: structure holding gpiochip params
95 * @irq: Interrupt controller structure
99 struct regmap
*regmap
;
100 struct mutex i2c_lock
;
101 struct gpio_desc
*reset_gpio
;
102 struct regulator
*vio_vreg
;
103 struct pinctrl_dev
*pctl
;
104 struct gpio_chip gpio
;
105 struct aw9523_irq
*irq
;
108 static const struct pinctrl_pin_desc aw9523_pins
[] = {
110 PINCTRL_PIN(0, "gpio0"),
111 PINCTRL_PIN(1, "gpio1"),
112 PINCTRL_PIN(2, "gpio2"),
113 PINCTRL_PIN(3, "gpio3"),
114 PINCTRL_PIN(4, "gpio4"),
115 PINCTRL_PIN(5, "gpio5"),
116 PINCTRL_PIN(6, "gpio6"),
117 PINCTRL_PIN(7, "gpio7"),
120 PINCTRL_PIN(8, "gpio8"),
121 PINCTRL_PIN(9, "gpio9"),
122 PINCTRL_PIN(10, "gpio10"),
123 PINCTRL_PIN(11, "gpio11"),
124 PINCTRL_PIN(12, "gpio12"),
125 PINCTRL_PIN(13, "gpio13"),
126 PINCTRL_PIN(14, "gpio14"),
127 PINCTRL_PIN(15, "gpio15"),
130 static int aw9523_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
132 return ARRAY_SIZE(aw9523_pins
);
135 static const char *aw9523_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
136 unsigned int selector
)
138 return aw9523_pins
[selector
].name
;
141 static int aw9523_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
142 unsigned int selector
,
143 const unsigned int **pins
,
144 unsigned int *num_pins
)
146 *pins
= &aw9523_pins
[selector
].number
;
151 static const struct pinctrl_ops aw9523_pinctrl_ops
= {
152 .get_groups_count
= aw9523_pinctrl_get_groups_count
,
153 .get_group_pins
= aw9523_pinctrl_get_group_pins
,
154 .get_group_name
= aw9523_pinctrl_get_group_name
,
155 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
156 .dt_free_map
= pinconf_generic_dt_free_map
,
159 static const char * const gpio_pwm_groups
[] = {
160 "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", "gpio5",
161 "gpio6", "gpio7", "gpio8", "gpio9", "gpio10", "gpio11",
162 "gpio12", "gpio13", "gpio14", "gpio15"
165 /* Warning: Do NOT reorder this array */
166 static const struct aw9523_pinmux aw9523_pmx
[] = {
169 .grps
= gpio_pwm_groups
,
170 .num_grps
= ARRAY_SIZE(gpio_pwm_groups
),
174 .grps
= gpio_pwm_groups
,
175 .num_grps
= ARRAY_SIZE(gpio_pwm_groups
),
179 static int aw9523_pmx_get_funcs_count(struct pinctrl_dev
*pctl
)
181 return ARRAY_SIZE(aw9523_pmx
);
184 static const char *aw9523_pmx_get_fname(struct pinctrl_dev
*pctl
,
187 return aw9523_pmx
[sel
].name
;
190 static int aw9523_pmx_get_groups(struct pinctrl_dev
*pctl
, unsigned int sel
,
191 const char * const **groups
,
192 unsigned int * const num_groups
)
194 *groups
= aw9523_pmx
[sel
].grps
;
195 *num_groups
= aw9523_pmx
[sel
].num_grps
;
199 static int aw9523_pmx_set_mux(struct pinctrl_dev
*pctl
, unsigned int fsel
,
202 struct aw9523
*awi
= pinctrl_dev_get_drvdata(pctl
);
203 int ret
, pin
= aw9523_pins
[grp
].number
% AW9523_PINS_PER_PORT
;
205 if (fsel
>= ARRAY_SIZE(aw9523_pmx
))
209 * This maps directly to the aw9523_pmx array: programming a
210 * high bit means "gpio" and a low bit means "pwm".
212 mutex_lock(&awi
->i2c_lock
);
213 ret
= regmap_update_bits(awi
->regmap
, AW9523_REG_PORT_MODE(pin
),
214 BIT(pin
), (fsel
? BIT(pin
) : 0));
215 mutex_unlock(&awi
->i2c_lock
);
219 static const struct pinmux_ops aw9523_pinmux_ops
= {
220 .get_functions_count
= aw9523_pmx_get_funcs_count
,
221 .get_function_name
= aw9523_pmx_get_fname
,
222 .get_function_groups
= aw9523_pmx_get_groups
,
223 .set_mux
= aw9523_pmx_set_mux
,
226 static int aw9523_pcfg_param_to_reg(enum pin_config_param pcp
, int pin
, u8
*r
)
231 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
232 case PIN_CONFIG_BIAS_PULL_DOWN
:
233 case PIN_CONFIG_BIAS_PULL_UP
:
234 reg
= AW9523_REG_IN_STATE(pin
);
236 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
237 case PIN_CONFIG_DRIVE_PUSH_PULL
:
238 reg
= AW9523_REG_GCR
;
240 case PIN_CONFIG_INPUT_ENABLE
:
241 case PIN_CONFIG_OUTPUT_ENABLE
:
242 reg
= AW9523_REG_CONF_STATE(pin
);
244 case PIN_CONFIG_OUTPUT
:
245 reg
= AW9523_REG_OUT_STATE(pin
);
255 static int aw9523_pconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
256 unsigned long *config
)
258 struct aw9523
*awi
= pinctrl_dev_get_drvdata(pctldev
);
259 enum pin_config_param param
= pinconf_to_config_param(*config
);
260 int regbit
= pin
% AW9523_PINS_PER_PORT
;
265 rc
= aw9523_pcfg_param_to_reg(param
, pin
, ®
);
269 mutex_lock(&awi
->i2c_lock
);
270 rc
= regmap_read(awi
->regmap
, reg
, &val
);
271 mutex_unlock(&awi
->i2c_lock
);
276 case PIN_CONFIG_BIAS_PULL_UP
:
277 case PIN_CONFIG_INPUT_ENABLE
:
278 case PIN_CONFIG_OUTPUT
:
281 case PIN_CONFIG_BIAS_PULL_DOWN
:
282 case PIN_CONFIG_OUTPUT_ENABLE
:
286 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
287 if (pin
>= AW9523_PINS_PER_PORT
)
290 val
= !FIELD_GET(AW9523_GCR_GPOMD_MASK
, val
);
292 case PIN_CONFIG_DRIVE_PUSH_PULL
:
293 if (pin
>= AW9523_PINS_PER_PORT
)
296 val
= FIELD_GET(AW9523_GCR_GPOMD_MASK
, val
);
304 *config
= pinconf_to_config_packed(param
, !!val
);
309 static int aw9523_pconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
310 unsigned long *configs
, unsigned int num_configs
)
312 struct aw9523
*awi
= pinctrl_dev_get_drvdata(pctldev
);
313 enum pin_config_param param
;
314 int regbit
= pin
% AW9523_PINS_PER_PORT
;
317 unsigned int mask
, val
;
320 mutex_lock(&awi
->i2c_lock
);
321 for (i
= 0; i
< num_configs
; i
++) {
322 param
= pinconf_to_config_param(configs
[i
]);
323 arg
= pinconf_to_config_argument(configs
[i
]);
325 rc
= aw9523_pcfg_param_to_reg(param
, pin
, ®
);
330 case PIN_CONFIG_OUTPUT
:
331 /* First, enable pin output */
332 rc
= regmap_update_bits(awi
->regmap
,
333 AW9523_REG_CONF_STATE(pin
),
338 /* Then, fall through to config output level */
340 case PIN_CONFIG_OUTPUT_ENABLE
:
343 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
344 case PIN_CONFIG_BIAS_PULL_DOWN
:
345 case PIN_CONFIG_BIAS_PULL_UP
:
346 case PIN_CONFIG_INPUT_ENABLE
:
348 val
= arg
? BIT(regbit
) : 0;
350 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
351 /* Open-Drain is supported only on port 0 */
352 if (pin
>= AW9523_PINS_PER_PORT
) {
356 mask
= AW9523_GCR_GPOMD_MASK
;
359 case PIN_CONFIG_DRIVE_PUSH_PULL
:
360 /* Port 1 is always Push-Pull */
361 if (pin
>= AW9523_PINS_PER_PORT
) {
366 mask
= AW9523_GCR_GPOMD_MASK
;
367 val
= AW9523_GCR_GPOMD_MASK
;
374 rc
= regmap_update_bits(awi
->regmap
, reg
, mask
, val
);
379 mutex_unlock(&awi
->i2c_lock
);
383 static const struct pinconf_ops aw9523_pinconf_ops
= {
384 .pin_config_get
= aw9523_pconf_get
,
385 .pin_config_set
= aw9523_pconf_set
,
390 * aw9523_get_pin_direction - Get pin direction
391 * @regmap: Regmap structure
392 * @pin: gpiolib pin number
393 * @n: pin index in port register
395 * Return: Pin direction for success or negative number for error
397 static int aw9523_get_pin_direction(struct regmap
*regmap
, u8 pin
, u8 n
)
401 ret
= regmap_read(regmap
, AW9523_REG_CONF_STATE(pin
), &val
);
405 return (val
& BIT(n
)) == BIT(n
);
409 * aw9523_get_port_state - Get input or output state for entire port
410 * @regmap: Regmap structure
411 * @pin: gpiolib pin number
412 * @regbit: hw pin index, used to retrieve port number
413 * @state: returned port state
415 * Return: Zero for success or negative number for error
417 static int aw9523_get_port_state(struct regmap
*regmap
, u8 pin
,
418 u8 regbit
, unsigned int *state
)
423 dir
= aw9523_get_pin_direction(regmap
, pin
, regbit
);
427 if (dir
== GPIO_LINE_DIRECTION_IN
)
428 reg
= AW9523_REG_IN_STATE(pin
);
430 reg
= AW9523_REG_OUT_STATE(pin
);
432 return regmap_read(regmap
, reg
, state
);
435 static int aw9523_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
439 case IRQ_TYPE_EDGE_BOTH
:
447 * aw9523_irq_mask - Mask interrupt
450 * Sets which interrupt to mask in the bitmap;
451 * The interrupt will be masked when unlocking the irq bus.
453 static void aw9523_irq_mask(struct irq_data
*d
)
455 struct aw9523
*awi
= gpiochip_get_data(irq_data_get_irq_chip_data(d
));
456 unsigned int n
= d
->hwirq
% AW9523_PINS_PER_PORT
;
458 regmap_update_bits(awi
->regmap
,
459 AW9523_REG_INTR_DIS(d
->hwirq
),
464 * aw9523_irq_unmask - Unmask interrupt
467 * Sets which interrupt to unmask in the bitmap;
468 * The interrupt will be masked when unlocking the irq bus.
470 static void aw9523_irq_unmask(struct irq_data
*d
)
472 struct aw9523
*awi
= gpiochip_get_data(irq_data_get_irq_chip_data(d
));
473 unsigned int n
= d
->hwirq
% AW9523_PINS_PER_PORT
;
475 regmap_update_bits(awi
->regmap
,
476 AW9523_REG_INTR_DIS(d
->hwirq
),
480 static irqreturn_t
aw9523_irq_thread_func(int irq
, void *dev_id
)
482 struct aw9523
*awi
= (struct aw9523
*)dev_id
;
483 unsigned long n
, val
= 0;
484 unsigned long changed_gpio
;
485 unsigned int tmp
, port_pin
, i
, ret
;
487 for (i
= 0; i
< AW9523_NUM_PORTS
; i
++) {
488 port_pin
= i
* AW9523_PINS_PER_PORT
;
489 ret
= regmap_read(awi
->regmap
,
490 AW9523_REG_IN_STATE(port_pin
),
494 val
|= (u8
)tmp
<< (i
* 8);
497 /* Handle GPIO input release interrupt as well */
498 changed_gpio
= awi
->irq
->cached_gpio
^ val
;
499 awi
->irq
->cached_gpio
= val
;
502 * To avoid up to four *slow* i2c reads from any driver hooked
503 * up to our interrupts, just check for the irq_find_mapping
504 * result: if the interrupt is not mapped, then we don't want
507 for_each_set_bit(n
, &changed_gpio
, awi
->gpio
.ngpio
) {
508 tmp
= irq_find_mapping(awi
->gpio
.irq
.domain
, n
);
511 handle_nested_irq(tmp
);
518 * aw9523_irq_bus_lock - Grab lock for interrupt operation
521 static void aw9523_irq_bus_lock(struct irq_data
*d
)
523 struct aw9523
*awi
= gpiochip_get_data(irq_data_get_irq_chip_data(d
));
525 mutex_lock(&awi
->irq
->lock
);
526 regcache_cache_only(awi
->regmap
, true);
530 * aw9523_irq_bus_sync_unlock - Synchronize state and unlock
533 * Writes the interrupt mask bits (found in the bit map) to the
534 * hardware, then unlocks the bus.
536 static void aw9523_irq_bus_sync_unlock(struct irq_data
*d
)
538 struct aw9523
*awi
= gpiochip_get_data(irq_data_get_irq_chip_data(d
));
540 regcache_cache_only(awi
->regmap
, false);
541 regcache_sync(awi
->regmap
);
542 mutex_unlock(&awi
->irq
->lock
);
545 static int aw9523_gpio_get_direction(struct gpio_chip
*chip
,
548 struct aw9523
*awi
= gpiochip_get_data(chip
);
549 u8 regbit
= offset
% AW9523_PINS_PER_PORT
;
552 mutex_lock(&awi
->i2c_lock
);
553 ret
= aw9523_get_pin_direction(awi
->regmap
, offset
, regbit
);
554 mutex_unlock(&awi
->i2c_lock
);
559 static int aw9523_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
561 struct aw9523
*awi
= gpiochip_get_data(chip
);
562 u8 regbit
= offset
% AW9523_PINS_PER_PORT
;
566 mutex_lock(&awi
->i2c_lock
);
567 ret
= aw9523_get_port_state(awi
->regmap
, offset
, regbit
, &val
);
568 mutex_unlock(&awi
->i2c_lock
);
572 return !!(val
& BIT(regbit
));
576 * _aw9523_gpio_get_multiple - Get I/O state for an entire port
577 * @regmap: Regmap structure
578 * @pin: gpiolib pin number
579 * @regbit: hw pin index, used to retrieve port number
580 * @state: returned port I/O state
582 * Return: Zero for success or negative number for error
584 static int _aw9523_gpio_get_multiple(struct aw9523
*awi
, u8 regbit
,
591 /* Registers are 8-bits wide */
592 ret
= regmap_read(awi
->regmap
, AW9523_REG_CONF_STATE(regbit
), &dir_in
);
599 ret
= regmap_read(awi
->regmap
, AW9523_REG_IN_STATE(regbit
),
603 *state
|= (u8
)val
& m
;
608 ret
= regmap_read(awi
->regmap
, AW9523_REG_OUT_STATE(regbit
),
612 *state
|= (u8
)val
& m
;
618 static int aw9523_gpio_get_multiple(struct gpio_chip
*chip
,
622 struct aw9523
*awi
= gpiochip_get_data(chip
);
626 mutex_lock(&awi
->i2c_lock
);
628 /* Port 0 (gpio 0-7) */
631 ret
= _aw9523_gpio_get_multiple(awi
, 0, &state
, m
);
637 /* Port 1 (gpio 8-15) */
638 m
= (*mask
>> 8) & U8_MAX
;
640 ret
= _aw9523_gpio_get_multiple(awi
, AW9523_PINS_PER_PORT
,
645 *bits
|= (state
<< 8);
648 mutex_unlock(&awi
->i2c_lock
);
652 static void aw9523_gpio_set_multiple(struct gpio_chip
*chip
,
656 struct aw9523
*awi
= gpiochip_get_data(chip
);
657 u8 mask_lo
, mask_hi
, bits_lo
, bits_hi
;
661 mask_lo
= *mask
& U8_MAX
;
662 mask_hi
= (*mask
>> 8) & U8_MAX
;
663 mutex_lock(&awi
->i2c_lock
);
665 reg
= AW9523_REG_OUT_STATE(AW9523_PINS_PER_PORT
);
666 bits_hi
= (*bits
>> 8) & U8_MAX
;
668 ret
= regmap_write_bits(awi
->regmap
, reg
, mask_hi
, bits_hi
);
670 dev_warn(awi
->dev
, "Cannot write port1 out level\n");
675 reg
= AW9523_REG_OUT_STATE(0);
676 bits_lo
= *bits
& U8_MAX
;
677 ret
= regmap_write_bits(awi
->regmap
, reg
, mask_lo
, bits_lo
);
679 dev_warn(awi
->dev
, "Cannot write port0 out level\n");
682 mutex_unlock(&awi
->i2c_lock
);
685 static void aw9523_gpio_set(struct gpio_chip
*chip
,
686 unsigned int offset
, int value
)
688 struct aw9523
*awi
= gpiochip_get_data(chip
);
689 u8 regbit
= offset
% AW9523_PINS_PER_PORT
;
691 mutex_lock(&awi
->i2c_lock
);
692 regmap_update_bits(awi
->regmap
, AW9523_REG_OUT_STATE(offset
),
693 BIT(regbit
), value
? BIT(regbit
) : 0);
694 mutex_unlock(&awi
->i2c_lock
);
698 static int aw9523_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
700 struct aw9523
*awi
= gpiochip_get_data(chip
);
701 u8 regbit
= offset
% AW9523_PINS_PER_PORT
;
704 mutex_lock(&awi
->i2c_lock
);
705 ret
= regmap_update_bits(awi
->regmap
, AW9523_REG_CONF_STATE(offset
),
706 BIT(regbit
), BIT(regbit
));
707 mutex_unlock(&awi
->i2c_lock
);
712 static int aw9523_direction_output(struct gpio_chip
*chip
,
713 unsigned int offset
, int value
)
715 struct aw9523
*awi
= gpiochip_get_data(chip
);
716 u8 regbit
= offset
% AW9523_PINS_PER_PORT
;
719 mutex_lock(&awi
->i2c_lock
);
720 ret
= regmap_update_bits(awi
->regmap
, AW9523_REG_OUT_STATE(offset
),
721 BIT(regbit
), value
? BIT(regbit
) : 0);
725 ret
= regmap_update_bits(awi
->regmap
, AW9523_REG_CONF_STATE(offset
),
728 mutex_unlock(&awi
->i2c_lock
);
732 static int aw9523_drive_reset_gpio(struct aw9523
*awi
)
734 unsigned int chip_id
;
738 * If the chip is already configured for any reason, then we
739 * will probably succeed in sending the soft reset signal to
740 * the hardware through I2C: this operation takes less time
741 * compared to a full HW reset and it gives the same results.
743 ret
= regmap_write(awi
->regmap
, AW9523_REG_SOFT_RESET
, 0);
747 dev_dbg(awi
->dev
, "Cannot execute soft reset: trying hard reset\n");
748 ret
= gpiod_direction_output(awi
->reset_gpio
, 0);
752 /* The reset pulse has to be longer than 20uS due to deglitch */
753 usleep_range(AW9523_HW_RESET_US
, AW9523_HW_RESET_US
+ 1);
755 ret
= gpiod_direction_output(awi
->reset_gpio
, 1);
759 /* The HW needs at least 1uS to reliably recover after reset */
760 usleep_range(AW9523_HW_RESET_RECOVERY_US
,
761 AW9523_HW_RESET_RECOVERY_US
+ 1);
763 /* Check the ChipID */
764 ret
= regmap_read(awi
->regmap
, AW9523_REG_CHIPID
, &chip_id
);
766 dev_err(awi
->dev
, "Cannot read Chip ID: %d\n", ret
);
769 if (chip_id
!= AW9523_VAL_EXPECTED_CHIPID
) {
770 dev_err(awi
->dev
, "Bad ChipID; read 0x%x, expected 0x%x\n",
771 chip_id
, AW9523_VAL_EXPECTED_CHIPID
);
778 static int aw9523_hw_reset(struct aw9523
*awi
)
780 int ret
, max_retries
= 2;
782 /* Sometimes the chip needs more than one reset cycle */
784 ret
= aw9523_drive_reset_gpio(awi
);
788 } while (max_retries
);
793 static int aw9523_init_gpiochip(struct aw9523
*awi
, unsigned int npins
)
795 struct device
*dev
= awi
->dev
;
796 struct gpio_chip
*gpiochip
= &awi
->gpio
;
798 gpiochip
->label
= devm_kstrdup(dev
, dev_name(dev
), GFP_KERNEL
);
799 if (!gpiochip
->label
)
803 gpiochip
->ngpio
= npins
;
804 gpiochip
->get_direction
= aw9523_gpio_get_direction
;
805 gpiochip
->direction_input
= aw9523_direction_input
;
806 gpiochip
->direction_output
= aw9523_direction_output
;
807 gpiochip
->get
= aw9523_gpio_get
;
808 gpiochip
->get_multiple
= aw9523_gpio_get_multiple
;
809 gpiochip
->set
= aw9523_gpio_set
;
810 gpiochip
->set_multiple
= aw9523_gpio_set_multiple
;
811 gpiochip
->set_config
= gpiochip_generic_config
;
812 gpiochip
->parent
= dev
;
813 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
814 gpiochip
->fwnode
= dev
->fwnode
;
816 gpiochip
->of_node
= dev
->of_node
;
818 gpiochip
->owner
= THIS_MODULE
;
819 gpiochip
->can_sleep
= true;
824 static int aw9523_init_irq(struct aw9523
*awi
, int irq
)
826 struct device
*dev
= awi
->dev
;
827 struct gpio_irq_chip
*gpioirq
;
828 struct irq_chip
*irqchip
;
831 if (!device_property_read_bool(dev
, "interrupt-controller"))
834 irqchip
= devm_kzalloc(dev
, sizeof(*irqchip
), GFP_KERNEL
);
838 awi
->irq
= devm_kzalloc(dev
, sizeof(*awi
->irq
), GFP_KERNEL
);
842 irqchip
->name
= devm_kstrdup(dev
, dev_name(dev
), GFP_KERNEL
);
846 irqchip
->irq_mask
= aw9523_irq_mask
;
847 irqchip
->irq_unmask
= aw9523_irq_unmask
;
848 irqchip
->irq_bus_lock
= aw9523_irq_bus_lock
;
849 irqchip
->irq_bus_sync_unlock
= aw9523_irq_bus_sync_unlock
;
850 irqchip
->irq_set_type
= aw9523_gpio_irq_type
;
851 awi
->irq
->irqchip
= irqchip
;
852 mutex_init(&awi
->irq
->lock
);
854 ret
= devm_request_threaded_irq(dev
, irq
, NULL
, aw9523_irq_thread_func
,
855 IRQF_ONESHOT
, dev_name(dev
), awi
);
857 dev_err(dev
, "Failed to request irq %d\n", irq
);
861 gpioirq
= &awi
->gpio
.irq
;
862 gpioirq
->chip
= irqchip
;
863 gpioirq
->parent_handler
= NULL
;
864 gpioirq
->num_parents
= 0;
865 gpioirq
->parents
= NULL
;
866 gpioirq
->default_type
= IRQ_TYPE_LEVEL_MASK
;
867 gpioirq
->handler
= handle_simple_irq
;
868 gpioirq
->threaded
= true;
874 static bool aw9523_is_reg_hole(unsigned int reg
)
876 return (reg
> AW9523_REG_PORT_MODE(AW9523_PINS_PER_PORT
) &&
877 reg
< AW9523_REG_SOFT_RESET
) ||
878 (reg
> AW9523_REG_INTR_DIS(AW9523_PINS_PER_PORT
) &&
879 reg
< AW9523_REG_CHIPID
);
882 static bool aw9523_readable_reg(struct device
*dev
, unsigned int reg
)
884 /* All available registers (minus holes) can be read */
885 return !aw9523_is_reg_hole(reg
);
888 static bool aw9523_volatile_reg(struct device
*dev
, unsigned int reg
)
890 return aw9523_is_reg_hole(reg
) ||
891 reg
== AW9523_REG_IN_STATE(0) ||
892 reg
== AW9523_REG_IN_STATE(AW9523_PINS_PER_PORT
) ||
893 reg
== AW9523_REG_CHIPID
||
894 reg
== AW9523_REG_SOFT_RESET
;
897 static bool aw9523_writeable_reg(struct device
*dev
, unsigned int reg
)
899 return !aw9523_is_reg_hole(reg
) && reg
!= AW9523_REG_CHIPID
;
902 static bool aw9523_precious_reg(struct device
*dev
, unsigned int reg
)
904 /* Reading AW9523_REG_IN_STATE clears interrupt status */
905 return aw9523_is_reg_hole(reg
) ||
906 reg
== AW9523_REG_IN_STATE(0) ||
907 reg
== AW9523_REG_IN_STATE(AW9523_PINS_PER_PORT
);
910 static const struct regmap_config aw9523_regmap
= {
915 .precious_reg
= aw9523_precious_reg
,
916 .readable_reg
= aw9523_readable_reg
,
917 .volatile_reg
= aw9523_volatile_reg
,
918 .writeable_reg
= aw9523_writeable_reg
,
920 .cache_type
= REGCACHE_FLAT
,
921 .disable_locking
= true,
923 .num_reg_defaults_raw
= AW9523_REG_SOFT_RESET
,
926 static int aw9523_hw_init(struct aw9523
*awi
)
928 u8 p1_pin
= AW9523_PINS_PER_PORT
;
932 /* No register caching during initialization */
933 regcache_cache_bypass(awi
->regmap
, true);
935 /* Bring up the chip */
936 ret
= aw9523_hw_reset(awi
);
938 dev_err(awi
->dev
, "HW Reset failed: %d\n", ret
);
943 * This is the expected chip and it is running: it's time to
944 * set a safe default configuration in case the user doesn't
945 * configure (all of the available) pins in this chip.
946 * P.S.: The writes order doesn't matter.
949 /* Set all pins as GPIO */
950 ret
= regmap_write(awi
->regmap
, AW9523_REG_PORT_MODE(0), U8_MAX
);
953 ret
= regmap_write(awi
->regmap
, AW9523_REG_PORT_MODE(p1_pin
), U8_MAX
);
957 /* Set Open-Drain mode on Port 0 (Port 1 is always P-P) */
958 ret
= regmap_write(awi
->regmap
, AW9523_REG_GCR
, 0);
962 /* Set all pins as inputs */
963 ret
= regmap_write(awi
->regmap
, AW9523_REG_CONF_STATE(0), U8_MAX
);
966 ret
= regmap_write(awi
->regmap
, AW9523_REG_CONF_STATE(p1_pin
), U8_MAX
);
970 /* Disable all interrupts to avoid unreasoned wakeups */
971 ret
= regmap_write(awi
->regmap
, AW9523_REG_INTR_DIS(0), U8_MAX
);
974 ret
= regmap_write(awi
->regmap
, AW9523_REG_INTR_DIS(p1_pin
), U8_MAX
);
978 /* Clear setup-generated interrupts by performing a port state read */
979 ret
= aw9523_get_port_state(awi
->regmap
, 0, 0, &val
);
982 ret
= aw9523_get_port_state(awi
->regmap
, p1_pin
, 0, &val
);
986 /* Everything went fine: activate and reinitialize register cache */
987 regcache_cache_bypass(awi
->regmap
, false);
988 return regmap_reinit_cache(awi
->regmap
, &aw9523_regmap
);
991 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
992 static int aw9523_probe(struct i2c_client
*client
)
994 static int aw9523_probe(struct i2c_client
*client
,
995 const struct i2c_device_id
*id
)
998 struct device
*dev
= &client
->dev
;
999 struct pinctrl_desc
*pdesc
;
1003 awi
= devm_kzalloc(dev
, sizeof(*awi
), GFP_KERNEL
);
1007 i2c_set_clientdata(client
, awi
);
1010 awi
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
1011 if (IS_ERR(awi
->reset_gpio
))
1012 return PTR_ERR(awi
->reset_gpio
);
1013 gpiod_set_consumer_name(awi
->reset_gpio
, "aw9523 reset");
1015 awi
->regmap
= devm_regmap_init_i2c(client
, &aw9523_regmap
);
1016 if (IS_ERR(awi
->regmap
))
1017 return PTR_ERR(awi
->regmap
);
1019 awi
->vio_vreg
= devm_regulator_get_optional(dev
, "vio");
1020 if (IS_ERR(awi
->vio_vreg
)) {
1021 if (PTR_ERR(awi
->vio_vreg
) == -EPROBE_DEFER
)
1022 return -EPROBE_DEFER
;
1023 awi
->vio_vreg
= NULL
;
1025 ret
= regulator_enable(awi
->vio_vreg
);
1030 mutex_init(&awi
->i2c_lock
);
1031 lockdep_set_subclass(&awi
->i2c_lock
,
1032 i2c_adapter_depth(client
->adapter
));
1034 pdesc
= devm_kzalloc(dev
, sizeof(*pdesc
), GFP_KERNEL
);
1038 ret
= aw9523_hw_init(awi
);
1040 goto err_disable_vregs
;
1042 pdesc
->name
= dev_name(dev
);
1043 pdesc
->owner
= THIS_MODULE
;
1044 pdesc
->pctlops
= &aw9523_pinctrl_ops
;
1045 pdesc
->pmxops
= &aw9523_pinmux_ops
;
1046 pdesc
->confops
= &aw9523_pinconf_ops
;
1047 pdesc
->pins
= aw9523_pins
;
1048 pdesc
->npins
= ARRAY_SIZE(aw9523_pins
);
1050 ret
= aw9523_init_gpiochip(awi
, pdesc
->npins
);
1052 goto err_disable_vregs
;
1055 ret
= aw9523_init_irq(awi
, client
->irq
);
1057 goto err_disable_vregs
;
1060 awi
->pctl
= devm_pinctrl_register(dev
, pdesc
, awi
);
1061 if (IS_ERR(awi
->pctl
)) {
1062 ret
= PTR_ERR(awi
->pctl
);
1063 dev_err(dev
, "Cannot register pinctrl: %d", ret
);
1064 goto err_disable_vregs
;
1067 ret
= devm_gpiochip_add_data(dev
, &awi
->gpio
, awi
);
1069 goto err_disable_vregs
;
1075 regulator_disable(awi
->vio_vreg
);
1076 mutex_destroy(&awi
->i2c_lock
);
1080 static int aw9523_remove(struct i2c_client
*client
)
1082 struct aw9523
*awi
= i2c_get_clientdata(client
);
1089 * If the chip VIO is connected to a regulator that we can turn
1090 * off, life is easy... otherwise, reinitialize the chip and
1091 * set the pins to hardware defaults before removing the driver
1092 * to leave it in a clean, safe and predictable state.
1094 if (awi
->vio_vreg
) {
1095 regulator_disable(awi
->vio_vreg
);
1097 mutex_lock(&awi
->i2c_lock
);
1098 ret
= aw9523_hw_init(awi
);
1099 mutex_unlock(&awi
->i2c_lock
);
1104 mutex_destroy(&awi
->i2c_lock
);
1108 static void aw9523_remove_void(struct i2c_client
*client
)
1110 aw9523_remove(client
);
1113 static const struct i2c_device_id aw9523_i2c_id_table
[] = {
1114 { "aw9523_i2c", 0 },
1117 MODULE_DEVICE_TABLE(i2c
, aw9523_i2c_id_table
);
1119 static const struct of_device_id of_aw9523_i2c_match
[] = {
1120 { .compatible
= "awinic,aw9523-pinctrl", },
1122 MODULE_DEVICE_TABLE(of
, of_aw9523_i2c_match
);
1124 static struct i2c_driver aw9523_driver
= {
1126 .name
= "aw9523-pinctrl",
1127 .of_match_table
= of_aw9523_i2c_match
,
1129 .probe
= aw9523_probe
,
1130 .remove
= aw9523_remove_void
,
1131 .id_table
= aw9523_i2c_id_table
,
1133 module_i2c_driver(aw9523_driver
);
1135 MODULE_DESCRIPTION("Awinic AW9523 I2C GPIO Expander driver");
1136 MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>");
1137 MODULE_LICENSE("GPL v2");
1138 MODULE_ALIAS("platform:aw9523");