Lines Matching +full:pin +full:- +full:val
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Awinic AW9523B i2c pin controller driver
22 #include <linux/pinctrl/pinconf-generic.h>
32 * HW needs at least 20uS for reset and at least 1-2uS to recover from
40 /* Port 0: P0_0...P0_7 - Port 1: P1_0...P1_7 */
41 #define AW9523_PIN_TO_PORT(pin) (pin >> 3) argument
42 #define AW9523_REG_IN_STATE(pin) (0x00 + AW9523_PIN_TO_PORT(pin)) argument
43 #define AW9523_REG_OUT_STATE(pin) (0x02 + AW9523_PIN_TO_PORT(pin)) argument
44 #define AW9523_REG_CONF_STATE(pin) (0x04 + AW9523_PIN_TO_PORT(pin)) argument
45 #define AW9523_REG_INTR_DIS(pin) (0x06 + AW9523_PIN_TO_PORT(pin)) argument
53 #define AW9523_REG_PORT_MODE(pin) (0x12 + AW9523_PIN_TO_PORT(pin)) argument
58 * struct aw9523_irq - Interrupt controller structure
68 * struct aw9523 - Main driver structure
141 "gpio0", "gpio1", "gpio2", "gpio3", /* 0-3 */
142 "gpio4", "gpio5", "gpio6", "gpio7", /* 4-7 */
143 "gpio8", "gpio9", "gpio10", "gpio11", /* 8-11 */
144 "gpio12", "gpio13", "gpio14", "gpio15", /* 11-15 */
177 int ret, pin = aw9523_pins[grp].number % AW9523_PINS_PER_PORT; in aw9523_pmx_set_mux() local
180 return -EINVAL; in aw9523_pmx_set_mux()
186 mutex_lock(&awi->i2c_lock); in aw9523_pmx_set_mux()
187 ret = regmap_update_bits(awi->regmap, AW9523_REG_PORT_MODE(pin), in aw9523_pmx_set_mux()
188 BIT(pin), (fsel ? BIT(pin) : 0)); in aw9523_pmx_set_mux()
189 mutex_unlock(&awi->i2c_lock); in aw9523_pmx_set_mux()
200 static int aw9523_pcfg_param_to_reg(enum pin_config_param pcp, int pin, u8 *r) in aw9523_pcfg_param_to_reg() argument
208 reg = AW9523_REG_IN_STATE(pin); in aw9523_pcfg_param_to_reg()
216 reg = AW9523_REG_CONF_STATE(pin); in aw9523_pcfg_param_to_reg()
219 reg = AW9523_REG_OUT_STATE(pin); in aw9523_pcfg_param_to_reg()
222 return -ENOTSUPP; in aw9523_pcfg_param_to_reg()
229 static int aw9523_pconf_get(struct pinctrl_dev *pctldev, unsigned int pin, in aw9523_pconf_get() argument
234 int regbit = pin % AW9523_PINS_PER_PORT; in aw9523_pconf_get()
235 unsigned int val; in aw9523_pconf_get() local
239 rc = aw9523_pcfg_param_to_reg(param, pin, ®); in aw9523_pconf_get()
243 mutex_lock(&awi->i2c_lock); in aw9523_pconf_get()
244 rc = regmap_read(awi->regmap, reg, &val); in aw9523_pconf_get()
245 mutex_unlock(&awi->i2c_lock); in aw9523_pconf_get()
253 val &= BIT(regbit); in aw9523_pconf_get()
257 val &= BIT(regbit); in aw9523_pconf_get()
258 val = !val; in aw9523_pconf_get()
261 if (pin >= AW9523_PINS_PER_PORT) in aw9523_pconf_get()
262 val = 0; in aw9523_pconf_get()
264 val = !FIELD_GET(AW9523_GCR_GPOMD_MASK, val); in aw9523_pconf_get()
267 if (pin >= AW9523_PINS_PER_PORT) in aw9523_pconf_get()
268 val = 1; in aw9523_pconf_get()
270 val = FIELD_GET(AW9523_GCR_GPOMD_MASK, val); in aw9523_pconf_get()
273 return -ENOTSUPP; in aw9523_pconf_get()
275 if (val < 1) in aw9523_pconf_get()
276 return -EINVAL; in aw9523_pconf_get()
278 *config = pinconf_to_config_packed(param, !!val); in aw9523_pconf_get()
283 static int aw9523_pconf_set(struct pinctrl_dev *pctldev, unsigned int pin, in aw9523_pconf_set() argument
288 int regbit = pin % AW9523_PINS_PER_PORT; in aw9523_pconf_set()
291 unsigned int mask, val; in aw9523_pconf_set() local
294 mutex_lock(&awi->i2c_lock); in aw9523_pconf_set()
299 rc = aw9523_pcfg_param_to_reg(param, pin, ®); in aw9523_pconf_set()
305 /* First, enable pin output */ in aw9523_pconf_set()
306 rc = regmap_update_bits(awi->regmap, in aw9523_pconf_set()
307 AW9523_REG_CONF_STATE(pin), in aw9523_pconf_set()
322 val = arg ? BIT(regbit) : 0; in aw9523_pconf_set()
325 /* Open-Drain is supported only on port 0 */ in aw9523_pconf_set()
326 if (pin >= AW9523_PINS_PER_PORT) { in aw9523_pconf_set()
327 rc = -ENOTSUPP; in aw9523_pconf_set()
331 val = 0; in aw9523_pconf_set()
334 /* Port 1 is always Push-Pull */ in aw9523_pconf_set()
335 if (pin >= AW9523_PINS_PER_PORT) { in aw9523_pconf_set()
337 val = 0; in aw9523_pconf_set()
341 val = AW9523_GCR_GPOMD_MASK; in aw9523_pconf_set()
344 rc = -ENOTSUPP; in aw9523_pconf_set()
348 rc = regmap_update_bits(awi->regmap, reg, mask, val); in aw9523_pconf_set()
353 mutex_unlock(&awi->i2c_lock); in aw9523_pconf_set()
364 * aw9523_get_pin_direction - Get pin direction
366 * @pin: gpiolib pin number
367 * @n: pin index in port register
369 * Return: Pin direction for success or negative number for error
371 static int aw9523_get_pin_direction(struct regmap *regmap, u8 pin, u8 n) in aw9523_get_pin_direction() argument
375 ret = regmap_test_bits(regmap, AW9523_REG_CONF_STATE(pin), BIT(n)); in aw9523_get_pin_direction()
383 * aw9523_get_port_state - Get input or output state for entire port
385 * @pin: gpiolib pin number
386 * @regbit: hw pin index, used to retrieve port number
391 static int aw9523_get_port_state(struct regmap *regmap, u8 pin, u8 regbit, in aw9523_get_port_state() argument
397 dir = aw9523_get_pin_direction(regmap, pin, regbit); in aw9523_get_port_state()
402 reg = AW9523_REG_IN_STATE(pin); in aw9523_get_port_state()
404 reg = AW9523_REG_OUT_STATE(pin); in aw9523_get_port_state()
416 return -EINVAL; in aw9523_gpio_irq_type()
421 * aw9523_irq_mask - Mask interrupt
433 regmap_update_bits(awi->regmap, AW9523_REG_INTR_DIS(hwirq), in aw9523_irq_mask()
435 gpiochip_disable_irq(&awi->gpio, hwirq); in aw9523_irq_mask()
439 * aw9523_irq_unmask - Unmask interrupt
451 gpiochip_enable_irq(&awi->gpio, hwirq); in aw9523_irq_unmask()
452 regmap_update_bits(awi->regmap, AW9523_REG_INTR_DIS(hwirq), in aw9523_irq_unmask()
459 unsigned long n, val = 0; in aw9523_irq_thread_func() local
465 ret = regmap_read(awi->regmap, in aw9523_irq_thread_func()
470 val |= (u8)tmp << (i * 8); in aw9523_irq_thread_func()
474 changed_gpio = awi->irq->cached_gpio ^ val; in aw9523_irq_thread_func()
475 awi->irq->cached_gpio = val; in aw9523_irq_thread_func()
483 for_each_set_bit(n, &changed_gpio, awi->gpio.ngpio) { in aw9523_irq_thread_func()
484 tmp = irq_find_mapping(awi->gpio.irq.domain, n); in aw9523_irq_thread_func()
494 * aw9523_irq_bus_lock - Grab lock for interrupt operation
501 mutex_lock(&awi->irq->lock); in aw9523_irq_bus_lock()
502 regcache_cache_only(awi->regmap, true); in aw9523_irq_bus_lock()
506 * aw9523_irq_bus_sync_unlock - Synchronize state and unlock
516 regcache_cache_only(awi->regmap, false); in aw9523_irq_bus_sync_unlock()
517 regcache_sync(awi->regmap); in aw9523_irq_bus_sync_unlock()
518 mutex_unlock(&awi->irq->lock); in aw9523_irq_bus_sync_unlock()
528 mutex_lock(&awi->i2c_lock); in aw9523_gpio_get_direction()
529 ret = aw9523_get_pin_direction(awi->regmap, offset, regbit); in aw9523_gpio_get_direction()
530 mutex_unlock(&awi->i2c_lock); in aw9523_gpio_get_direction()
539 unsigned int val; in aw9523_gpio_get() local
542 mutex_lock(&awi->i2c_lock); in aw9523_gpio_get()
543 ret = aw9523_get_port_state(awi->regmap, offset, regbit, &val); in aw9523_gpio_get()
544 mutex_unlock(&awi->i2c_lock); in aw9523_gpio_get()
548 return !!(val & BIT(regbit)); in aw9523_gpio_get()
552 * _aw9523_gpio_get_multiple - Get I/O state for an entire port
554 * @regbit: hw pin index, used to retrieve port number
563 u32 dir_in, val; in _aw9523_gpio_get_multiple() local
567 /* Registers are 8-bits wide */ in _aw9523_gpio_get_multiple()
568 ret = regmap_read(awi->regmap, AW9523_REG_CONF_STATE(regbit), &dir_in); in _aw9523_gpio_get_multiple()
575 ret = regmap_read(awi->regmap, AW9523_REG_IN_STATE(regbit), in _aw9523_gpio_get_multiple()
576 &val); in _aw9523_gpio_get_multiple()
579 *state |= (u8)val & m; in _aw9523_gpio_get_multiple()
584 ret = regmap_read(awi->regmap, AW9523_REG_OUT_STATE(regbit), in _aw9523_gpio_get_multiple()
585 &val); in _aw9523_gpio_get_multiple()
588 *state |= (u8)val & m; in _aw9523_gpio_get_multiple()
602 mutex_lock(&awi->i2c_lock); in aw9523_gpio_get_multiple()
604 /* Port 0 (gpio 0-7) */ in aw9523_gpio_get_multiple()
613 /* Port 1 (gpio 8-15) */ in aw9523_gpio_get_multiple()
624 mutex_unlock(&awi->i2c_lock); in aw9523_gpio_get_multiple()
642 mutex_lock(&awi->i2c_lock); in aw9523_gpio_set_multiple()
645 ret = regmap_write_bits(awi->regmap, reg, mask_hi, bits_hi); in aw9523_gpio_set_multiple()
647 dev_warn(awi->dev, "Cannot write port1 out level\n"); in aw9523_gpio_set_multiple()
651 ret = regmap_write_bits(awi->regmap, reg, mask_lo, bits_lo); in aw9523_gpio_set_multiple()
653 dev_warn(awi->dev, "Cannot write port0 out level\n"); in aw9523_gpio_set_multiple()
655 mutex_unlock(&awi->i2c_lock); in aw9523_gpio_set_multiple()
664 mutex_lock(&awi->i2c_lock); in aw9523_gpio_set()
665 regmap_update_bits(awi->regmap, AW9523_REG_OUT_STATE(offset), in aw9523_gpio_set()
667 mutex_unlock(&awi->i2c_lock); in aw9523_gpio_set()
677 mutex_lock(&awi->i2c_lock); in aw9523_direction_input()
678 ret = regmap_update_bits(awi->regmap, AW9523_REG_CONF_STATE(offset), in aw9523_direction_input()
680 mutex_unlock(&awi->i2c_lock); in aw9523_direction_input()
692 mutex_lock(&awi->i2c_lock); in aw9523_direction_output()
693 ret = regmap_update_bits(awi->regmap, AW9523_REG_OUT_STATE(offset), in aw9523_direction_output()
698 ret = regmap_update_bits(awi->regmap, AW9523_REG_CONF_STATE(offset), in aw9523_direction_output()
701 mutex_unlock(&awi->i2c_lock); in aw9523_direction_output()
716 ret = regmap_write(awi->regmap, AW9523_REG_SOFT_RESET, 0); in aw9523_drive_reset_gpio()
720 dev_dbg(awi->dev, "Cannot execute soft reset: trying hard reset\n"); in aw9523_drive_reset_gpio()
721 ret = gpiod_direction_output(awi->reset_gpio, 0); in aw9523_drive_reset_gpio()
728 ret = gpiod_direction_output(awi->reset_gpio, 1); in aw9523_drive_reset_gpio()
737 ret = regmap_read(awi->regmap, AW9523_REG_CHIPID, &chip_id); in aw9523_drive_reset_gpio()
739 dev_err(awi->dev, "Cannot read Chip ID: %d\n", ret); in aw9523_drive_reset_gpio()
743 dev_err(awi->dev, "Bad ChipID; read 0x%x, expected 0x%x\n", in aw9523_drive_reset_gpio()
745 return -EINVAL; in aw9523_drive_reset_gpio()
760 max_retries--; in aw9523_hw_reset()
768 struct device *dev = awi->dev; in aw9523_init_gpiochip()
769 struct gpio_chip *gc = &awi->gpio; in aw9523_init_gpiochip()
771 gc->label = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL); in aw9523_init_gpiochip()
772 if (!gc->label) in aw9523_init_gpiochip()
773 return -ENOMEM; in aw9523_init_gpiochip()
775 gc->base = -1; in aw9523_init_gpiochip()
776 gc->ngpio = npins; in aw9523_init_gpiochip()
777 gc->get_direction = aw9523_gpio_get_direction; in aw9523_init_gpiochip()
778 gc->direction_input = aw9523_direction_input; in aw9523_init_gpiochip()
779 gc->direction_output = aw9523_direction_output; in aw9523_init_gpiochip()
780 gc->get = aw9523_gpio_get; in aw9523_init_gpiochip()
781 gc->get_multiple = aw9523_gpio_get_multiple; in aw9523_init_gpiochip()
782 gc->set = aw9523_gpio_set; in aw9523_init_gpiochip()
783 gc->set_multiple = aw9523_gpio_set_multiple; in aw9523_init_gpiochip()
784 gc->set_config = gpiochip_generic_config; in aw9523_init_gpiochip()
785 gc->parent = dev; in aw9523_init_gpiochip()
786 gc->owner = THIS_MODULE; in aw9523_init_gpiochip()
787 gc->can_sleep = false; in aw9523_init_gpiochip()
805 struct device *dev = awi->dev; in aw9523_init_irq()
809 if (!device_property_read_bool(dev, "interrupt-controller")) in aw9523_init_irq()
812 awi->irq = devm_kzalloc(dev, sizeof(*awi->irq), GFP_KERNEL); in aw9523_init_irq()
813 if (!awi->irq) in aw9523_init_irq()
814 return -ENOMEM; in aw9523_init_irq()
816 mutex_init(&awi->irq->lock); in aw9523_init_irq()
823 girq = &awi->gpio.irq; in aw9523_init_irq()
825 girq->parent_handler = NULL; in aw9523_init_irq()
826 girq->num_parents = 0; in aw9523_init_irq()
827 girq->parents = NULL; in aw9523_init_irq()
828 girq->default_type = IRQ_TYPE_EDGE_BOTH; in aw9523_init_irq()
829 girq->handler = handle_simple_irq; in aw9523_init_irq()
830 girq->threaded = true; in aw9523_init_irq()
890 unsigned int val; in aw9523_hw_init() local
894 regcache_cache_bypass(awi->regmap, true); in aw9523_hw_init()
899 dev_err(awi->dev, "HW Reset failed: %d\n", ret); in aw9523_hw_init()
911 ret = regmap_write(awi->regmap, AW9523_REG_PORT_MODE(0), U8_MAX); in aw9523_hw_init()
914 ret = regmap_write(awi->regmap, AW9523_REG_PORT_MODE(p1_pin), U8_MAX); in aw9523_hw_init()
918 /* Set Open-Drain mode on Port 0 (Port 1 is always P-P) */ in aw9523_hw_init()
919 ret = regmap_write(awi->regmap, AW9523_REG_GCR, 0); in aw9523_hw_init()
924 ret = regmap_write(awi->regmap, AW9523_REG_CONF_STATE(0), U8_MAX); in aw9523_hw_init()
927 ret = regmap_write(awi->regmap, AW9523_REG_CONF_STATE(p1_pin), U8_MAX); in aw9523_hw_init()
932 ret = regmap_write(awi->regmap, AW9523_REG_INTR_DIS(0), U8_MAX); in aw9523_hw_init()
935 ret = regmap_write(awi->regmap, AW9523_REG_INTR_DIS(p1_pin), U8_MAX); in aw9523_hw_init()
939 /* Clear setup-generated interrupts by performing a port state read */ in aw9523_hw_init()
940 ret = aw9523_get_port_state(awi->regmap, 0, 0, &val); in aw9523_hw_init()
943 ret = aw9523_get_port_state(awi->regmap, p1_pin, 0, &val); in aw9523_hw_init()
948 regcache_cache_bypass(awi->regmap, false); in aw9523_hw_init()
949 return regmap_reinit_cache(awi->regmap, &aw9523_regmap); in aw9523_hw_init()
954 struct device *dev = &client->dev; in aw9523_probe()
961 return -ENOMEM; in aw9523_probe()
965 awi->dev = dev; in aw9523_probe()
966 awi->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); in aw9523_probe()
967 if (IS_ERR(awi->reset_gpio)) in aw9523_probe()
968 return PTR_ERR(awi->reset_gpio); in aw9523_probe()
969 gpiod_set_consumer_name(awi->reset_gpio, "aw9523 reset"); in aw9523_probe()
971 awi->regmap = devm_regmap_init_i2c(client, &aw9523_regmap); in aw9523_probe()
972 if (IS_ERR(awi->regmap)) in aw9523_probe()
973 return PTR_ERR(awi->regmap); in aw9523_probe()
975 awi->vio_vreg = devm_regulator_get_enable_optional(dev, "vio"); in aw9523_probe()
976 if (awi->vio_vreg && awi->vio_vreg != -ENODEV) in aw9523_probe()
977 return awi->vio_vreg; in aw9523_probe()
979 ret = devm_mutex_init(dev, &awi->i2c_lock); in aw9523_probe()
983 lockdep_set_subclass(&awi->i2c_lock, i2c_adapter_depth(client->adapter)); in aw9523_probe()
987 return -ENOMEM; in aw9523_probe()
993 pdesc->name = dev_name(dev); in aw9523_probe()
994 pdesc->owner = THIS_MODULE; in aw9523_probe()
995 pdesc->pctlops = &aw9523_pinctrl_ops; in aw9523_probe()
996 pdesc->pmxops = &aw9523_pinmux_ops; in aw9523_probe()
997 pdesc->confops = &aw9523_pinconf_ops; in aw9523_probe()
998 pdesc->pins = aw9523_pins; in aw9523_probe()
999 pdesc->npins = ARRAY_SIZE(aw9523_pins); in aw9523_probe()
1001 ret = aw9523_init_gpiochip(awi, pdesc->npins); in aw9523_probe()
1005 if (client->irq) { in aw9523_probe()
1006 ret = aw9523_init_irq(awi, client->irq); in aw9523_probe()
1011 awi->pctl = devm_pinctrl_register(dev, pdesc, awi); in aw9523_probe()
1012 if (IS_ERR(awi->pctl)) in aw9523_probe()
1013 return dev_err_probe(dev, PTR_ERR(awi->pctl), in aw9523_probe()
1016 return devm_gpiochip_add_data(dev, &awi->gpio, awi); in aw9523_probe()
1029 if (awi->vio_vreg == -ENODEV) { in aw9523_remove()
1030 mutex_lock(&awi->i2c_lock); in aw9523_remove()
1032 mutex_unlock(&awi->i2c_lock); in aw9523_remove()
1043 { .compatible = "awinic,aw9523-pinctrl", },
1050 .name = "aw9523-pinctrl",