Lines Matching +full:pctl +full:- +full:regmap
1 // SPDX-License-Identifier: GPL-2.0-only
18 #include <linux/regmap.h>
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 */
58 * struct aw9523_irq - Interrupt controller structure
68 * struct aw9523 - Main driver structure
70 * @regmap: regmap handle for current device
74 * @pctl: pinctrl handle for current device
80 struct regmap *regmap; member
84 struct pinctrl_dev *pctl; member
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 */
153 static int aw9523_pmx_get_funcs_count(struct pinctrl_dev *pctl) in aw9523_pmx_get_funcs_count() argument
158 static const char *aw9523_pmx_get_fname(struct pinctrl_dev *pctl, in aw9523_pmx_get_fname() argument
164 static int aw9523_pmx_get_groups(struct pinctrl_dev *pctl, unsigned int sel, in aw9523_pmx_get_groups() argument
173 static int aw9523_pmx_set_mux(struct pinctrl_dev *pctl, unsigned int fsel, in aw9523_pmx_set_mux() argument
176 struct aw9523 *awi = pinctrl_dev_get_drvdata(pctl); in aw9523_pmx_set_mux()
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()
189 mutex_unlock(&awi->i2c_lock); in aw9523_pmx_set_mux()
222 return -ENOTSUPP; in aw9523_pcfg_param_to_reg()
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()
273 return -ENOTSUPP; in aw9523_pconf_get()
276 return -EINVAL; in aw9523_pconf_get()
294 mutex_lock(&awi->i2c_lock); in aw9523_pconf_set()
306 rc = regmap_update_bits(awi->regmap, in aw9523_pconf_set()
325 /* Open-Drain is supported only on port 0 */ in aw9523_pconf_set()
327 rc = -ENOTSUPP; in aw9523_pconf_set()
334 /* Port 1 is always Push-Pull */ 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
365 * @regmap: Regmap structure
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
384 * @regmap: Regmap structure
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()
406 return regmap_read(regmap, reg, state); 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()
465 ret = regmap_read(awi->regmap, 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()
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()
552 * _aw9523_gpio_get_multiple - Get I/O state for an entire port
553 * @regmap: Regmap structure
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()
584 ret = regmap_read(awi->regmap, AW9523_REG_OUT_STATE(regbit), 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()
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_optional(dev, "vio"); in aw9523_probe()
976 if (IS_ERR(awi->vio_vreg)) { in aw9523_probe()
977 if (PTR_ERR(awi->vio_vreg) == -EPROBE_DEFER) in aw9523_probe()
978 return -EPROBE_DEFER; in aw9523_probe()
979 awi->vio_vreg = NULL; in aw9523_probe()
981 ret = regulator_enable(awi->vio_vreg); in aw9523_probe()
986 mutex_init(&awi->i2c_lock); in aw9523_probe()
987 lockdep_set_subclass(&awi->i2c_lock, i2c_adapter_depth(client->adapter)); in aw9523_probe()
991 ret = -ENOMEM; in aw9523_probe()
999 pdesc->name = dev_name(dev); in aw9523_probe()
1000 pdesc->owner = THIS_MODULE; in aw9523_probe()
1001 pdesc->pctlops = &aw9523_pinctrl_ops; in aw9523_probe()
1002 pdesc->pmxops = &aw9523_pinmux_ops; in aw9523_probe()
1003 pdesc->confops = &aw9523_pinconf_ops; in aw9523_probe()
1004 pdesc->pins = aw9523_pins; in aw9523_probe()
1005 pdesc->npins = ARRAY_SIZE(aw9523_pins); in aw9523_probe()
1007 ret = aw9523_init_gpiochip(awi, pdesc->npins); in aw9523_probe()
1011 if (client->irq) { in aw9523_probe()
1012 ret = aw9523_init_irq(awi, client->irq); in aw9523_probe()
1017 awi->pctl = devm_pinctrl_register(dev, pdesc, awi); in aw9523_probe()
1018 if (IS_ERR(awi->pctl)) { in aw9523_probe()
1019 ret = dev_err_probe(dev, PTR_ERR(awi->pctl), "Cannot register pinctrl"); in aw9523_probe()
1023 ret = devm_gpiochip_add_data(dev, &awi->gpio, awi); in aw9523_probe()
1030 if (awi->vio_vreg) in aw9523_probe()
1031 regulator_disable(awi->vio_vreg); in aw9523_probe()
1032 mutex_destroy(&awi->i2c_lock); in aw9523_probe()
1046 if (awi->vio_vreg) { in aw9523_remove()
1047 regulator_disable(awi->vio_vreg); in aw9523_remove()
1049 mutex_lock(&awi->i2c_lock); in aw9523_remove()
1051 mutex_unlock(&awi->i2c_lock); in aw9523_remove()
1054 mutex_destroy(&awi->i2c_lock); in aw9523_remove()
1064 { .compatible = "awinic,aw9523-pinctrl", },
1071 .name = "aw9523-pinctrl",