Lines Matching +full:ext +full:- +full:control +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0
14 #include <linux/devm-helpers.h>
25 #include <linux/turris-omnia-mcu-interface.h>
26 #include "turris-omnia-mcu.h"
32 /* GPIOs with value read from the 16-bit wide status */
35 [6] = "Front USB3 port over-current",
36 [7] = "Rear USB3 port over-current",
41 /* GPIOs with value read from the 32-bit wide extended status */
64 /* GPIOs with value read from the 16-bit wide extended control status */
106 -1, 0, 0)
131 __bf_shf(OMNIA_EXT_CTL_ ## _name), -1, \
140 return gpio->int_bit != OMNIA_GPIO_INVALID_INT_BIT; in is_int_bit_valid()
144 /* GPIOs with value read from the 16-bit wide status */
163 /* GPIOs with value read from the 32-bit wide extended status */
168 [31] = _DEF_GPIO_EXT_STS_LED(WPAN0_LED, EXT),
169 [32] = _DEF_GPIO_EXT_STS_LED(WPAN1_LED, EXT),
170 [33] = _DEF_GPIO_EXT_STS_LED(WPAN2_LED, EXT),
186 /* GPIOs with value read from the 16-bit wide extended control status */
197 /* mapping from interrupts to indexes of GPIOs in the omnia_gpios array */
257 return omnia_cmd_write(mcu->client, buf, len); in omnia_ctl_cmd_locked()
262 guard(mutex)(&mcu->lock); in omnia_ctl_cmd()
270 return -EINVAL; in omnia_gpio_request()
282 scoped_guard(mutex, &mcu->lock) { in omnia_gpio_get_direction()
283 val = omnia_cmd_read_bit(mcu->client, in omnia_gpio_get_direction()
312 if (gpio->ctl_cmd) in omnia_gpio_direction_input()
313 return -ENOTSUPP; in omnia_gpio_direction_input()
325 if (!gpio->ctl_cmd) in omnia_gpio_direction_output()
326 return -ENOTSUPP; in omnia_gpio_direction_output()
328 mask = BIT(gpio->ctl_bit); in omnia_gpio_direction_output()
334 return omnia_ctl_cmd(mcu, gpio->ctl_cmd, val, mask); in omnia_gpio_direction_output()
348 if (gpio->cmd == OMNIA_CMD_GET_STATUS_WORD && in omnia_gpio_get()
349 !(mcu->features & OMNIA_FEAT_NEW_INT_API)) in omnia_gpio_get()
350 return test_bit(gpio->bit, &mcu->last_status); in omnia_gpio_get()
352 guard(mutex)(&mcu->lock); in omnia_gpio_get()
359 if (is_int_bit_valid(gpio) && test_bit(gpio->int_bit, &mcu->is_cached)) in omnia_gpio_get()
360 return test_bit(gpio->int_bit, &mcu->cached); in omnia_gpio_get()
362 return omnia_cmd_read_bit(mcu->client, gpio->cmd, BIT(gpio->bit)); in omnia_gpio_get()
386 struct i2c_client *client = mcu->client; in omnia_gpio_get_multiple()
400 guard(mutex)(&mcu->lock); in omnia_gpio_get_multiple()
402 if (mcu->features & OMNIA_FEAT_NEW_INT_API) { in omnia_gpio_get_multiple()
413 sts = mcu->last_status; in omnia_gpio_get_multiple()
422 /* read relevant bits from extended control */ in omnia_gpio_get_multiple()
447 if (!gpio->ctl_cmd) in omnia_gpio_set()
450 mask = BIT(gpio->ctl_bit); in omnia_gpio_set()
453 omnia_ctl_cmd(mcu, gpio->ctl_cmd, val, mask); in omnia_gpio_set()
488 guard(mutex)(&mcu->lock); in omnia_gpio_set_multiple()
502 if (gpio->feat_mask) in omnia_gpio_available()
503 return (mcu->features & gpio->feat_mask) == gpio->feat; in omnia_gpio_available()
505 if (gpio->feat) in omnia_gpio_available()
506 return mcu->features & gpio->feat; in omnia_gpio_available()
520 if (gpio->cmd || is_int_bit_valid(gpio)) in omnia_gpio_init_valid_mask()
536 if (WARN_ON(gpiospec->args_count != 3)) in omnia_gpio_of_xlate()
537 return -EINVAL; in omnia_gpio_of_xlate()
540 *flags = gpiospec->args[2]; in omnia_gpio_of_xlate()
542 bank = gpiospec->args[0]; in omnia_gpio_of_xlate()
543 gpio = gpiospec->args[1]; in omnia_gpio_of_xlate()
547 return gpio < 16 ? gpio : -EINVAL; in omnia_gpio_of_xlate()
549 return gpio < 32 ? 16 + gpio : -EINVAL; in omnia_gpio_of_xlate()
551 return gpio < 16 ? 48 + gpio : -EINVAL; in omnia_gpio_of_xlate()
553 return -EINVAL; in omnia_gpio_of_xlate()
564 __clear_bit(bit, &mcu->rising); in omnia_irq_shutdown()
565 __clear_bit(bit, &mcu->falling); in omnia_irq_shutdown()
576 __clear_bit(bit, &mcu->rising); in omnia_irq_mask()
577 __clear_bit(bit, &mcu->mask); in omnia_irq_mask()
589 __set_bit(bit, &mcu->mask); in omnia_irq_unmask()
591 __set_bit(bit, &mcu->rising); in omnia_irq_unmask()
599 struct device *dev = &mcu->client->dev; in omnia_irq_set_type()
603 dev_err(dev, "irq %u: unsupported type %u\n", d->irq, type); in omnia_irq_set_type()
604 return -EINVAL; in omnia_irq_set_type()
607 __assign_bit(bit, &mcu->rising, type & IRQ_TYPE_EDGE_RISING); in omnia_irq_set_type()
608 __assign_bit(bit, &mcu->falling, type & IRQ_TYPE_EDGE_FALLING); in omnia_irq_set_type()
619 if (!(mcu->features & OMNIA_FEAT_NEW_INT_API)) in omnia_irq_bus_lock()
622 mutex_lock(&mcu->lock); in omnia_irq_bus_lock()
626 * omnia_mask_interleave - Interleaves the bytes from @rising and @falling
631 * Interleaves the little-endian bytes from @rising and @falling words.
637 * this interleaved format. The rationale behind this is that the low-indexed
638 * bits are more important - in many cases, the user will be interested only in
655 * omnia_mask_deinterleave - Deinterleaves the bytes into @rising and @falling
677 struct device *dev = &mcu->client->dev; in omnia_irq_bus_sync_unlock()
683 if (!(mcu->features & OMNIA_FEAT_NEW_INT_API)) in omnia_irq_bus_sync_unlock()
688 rising = mcu->rising & mcu->mask; in omnia_irq_bus_sync_unlock()
689 falling = mcu->falling & mcu->mask; in omnia_irq_bus_sync_unlock()
696 err = omnia_cmd_write(mcu->client, cmd, sizeof(cmd)); in omnia_irq_bus_sync_unlock()
703 * Remember which GPIOs have both rising and falling interrupts enabled. in omnia_irq_bus_sync_unlock()
705 * We also need to forget cached values of GPIOs that aren't cached in omnia_irq_bus_sync_unlock()
708 mcu->both = rising & falling; in omnia_irq_bus_sync_unlock()
709 mcu->is_cached &= mcu->both; in omnia_irq_bus_sync_unlock()
712 mutex_unlock(&mcu->lock); in omnia_irq_bus_sync_unlock()
751 return omnia_cmd_write(mcu->client, cmd, sizeof(cmd)); in omnia_irq_init_hw()
769 struct device *dev = &mcu->client->dev; in omnia_irq_read_pending_new()
775 len = omnia_irq_compute_pending_length(mcu->rising & mcu->mask, in omnia_irq_read_pending_new()
776 mcu->falling & mcu->mask); in omnia_irq_read_pending_new()
780 guard(mutex)(&mcu->lock); in omnia_irq_read_pending_new()
782 err = omnia_cmd_read(mcu->client, OMNIA_CMD_GET_INT_AND_CLEAR, reply, in omnia_irq_read_pending_new()
792 rising &= mcu->mask; in omnia_irq_read_pending_new()
793 falling &= mcu->mask; in omnia_irq_read_pending_new()
796 /* cache values for GPIOs that have both edges enabled */ in omnia_irq_read_pending_new()
797 mcu->is_cached &= ~(rising & falling); in omnia_irq_read_pending_new()
798 mcu->is_cached |= mcu->both & (rising ^ falling); in omnia_irq_read_pending_new()
799 mcu->cached = (mcu->cached | rising) & ~falling; in omnia_irq_read_pending_new()
810 err = omnia_cmd_read_u16(mcu->client, OMNIA_CMD_GET_STATUS_WORD, in omnia_read_status_word_old_fw()
830 mcu->button_pressed_emul = false; in button_release_emul_fn()
831 generic_handle_irq_safe(mcu->client->irq); in button_release_emul_fn()
849 struct device *dev = &mcu->client->dev; in omnia_irq_read_pending_old()
852 guard(mutex)(&mcu->lock); in omnia_irq_read_pending_old()
869 * - first we get an interrupt, we read the status word where in omnia_irq_read_pending_old()
871 * - MCU clears the OMNIA_STS_BUTTON_PRESSED bit because we read the in omnia_irq_read_pending_old()
873 * - we get another interrupt because the status word changed again in omnia_irq_read_pending_old()
876 * The gpiolib-cdev, gpiolib-sysfs and gpio-keys input driver all call in omnia_irq_read_pending_old()
885 mcu->button_pressed_emul = true; in omnia_irq_read_pending_old()
886 mod_delayed_work(system_wq, &mcu->button_release_emul_work, in omnia_irq_read_pending_old()
888 } else if (mcu->button_pressed_emul) { in omnia_irq_read_pending_old()
892 rising_sts = ~mcu->last_status & status; in omnia_irq_read_pending_old()
893 falling_sts = mcu->last_status & ~status; in omnia_irq_read_pending_old()
895 mcu->last_status = status; in omnia_irq_read_pending_old()
911 rising &= mcu->rising & mcu->mask; in omnia_irq_read_pending_old()
912 falling &= mcu->falling & mcu->mask; in omnia_irq_read_pending_old()
921 if (mcu->features & OMNIA_FEAT_NEW_INT_API) in omnia_irq_read_pending()
937 domain = mcu->gc.irq.domain; in omnia_irq_thread_handler()
958 if (mcu->features & OMNIA_FEAT_NEW_INT_API) { in front_button_mode_show()
959 val = omnia_cmd_read_bit(mcu->client, OMNIA_CMD_GET_STATUS_WORD, in front_button_mode_show()
964 val = !!(mcu->last_status & OMNIA_STS_BUTTON_MODE); in front_button_mode_show()
1002 bool new_api = mcu->features & OMNIA_FEAT_NEW_INT_API; in omnia_mcu_register_gpiochip()
1003 struct device *dev = &mcu->client->dev; in omnia_mcu_register_gpiochip()
1007 err = devm_mutex_init(dev, &mcu->lock); in omnia_mcu_register_gpiochip()
1011 mcu->gc.request = omnia_gpio_request; in omnia_mcu_register_gpiochip()
1012 mcu->gc.get_direction = omnia_gpio_get_direction; in omnia_mcu_register_gpiochip()
1013 mcu->gc.direction_input = omnia_gpio_direction_input; in omnia_mcu_register_gpiochip()
1014 mcu->gc.direction_output = omnia_gpio_direction_output; in omnia_mcu_register_gpiochip()
1015 mcu->gc.get = omnia_gpio_get; in omnia_mcu_register_gpiochip()
1016 mcu->gc.get_multiple = omnia_gpio_get_multiple; in omnia_mcu_register_gpiochip()
1017 mcu->gc.set = omnia_gpio_set; in omnia_mcu_register_gpiochip()
1018 mcu->gc.set_multiple = omnia_gpio_set_multiple; in omnia_mcu_register_gpiochip()
1019 mcu->gc.init_valid_mask = omnia_gpio_init_valid_mask; in omnia_mcu_register_gpiochip()
1020 mcu->gc.can_sleep = true; in omnia_mcu_register_gpiochip()
1021 mcu->gc.names = omnia_mcu_gpio_templates; in omnia_mcu_register_gpiochip()
1022 mcu->gc.base = -1; in omnia_mcu_register_gpiochip()
1023 mcu->gc.ngpio = ARRAY_SIZE(omnia_gpios); in omnia_mcu_register_gpiochip()
1024 mcu->gc.label = "Turris Omnia MCU GPIOs"; in omnia_mcu_register_gpiochip()
1025 mcu->gc.parent = dev; in omnia_mcu_register_gpiochip()
1026 mcu->gc.owner = THIS_MODULE; in omnia_mcu_register_gpiochip()
1027 mcu->gc.of_gpio_n_cells = 3; in omnia_mcu_register_gpiochip()
1028 mcu->gc.of_xlate = omnia_gpio_of_xlate; in omnia_mcu_register_gpiochip()
1030 gpio_irq_chip_set_chip(&mcu->gc.irq, &omnia_mcu_irq_chip); in omnia_mcu_register_gpiochip()
1032 mcu->gc.irq.parent_handler = NULL; in omnia_mcu_register_gpiochip()
1033 mcu->gc.irq.num_parents = 0; in omnia_mcu_register_gpiochip()
1034 mcu->gc.irq.parents = NULL; in omnia_mcu_register_gpiochip()
1035 mcu->gc.irq.default_type = IRQ_TYPE_NONE; in omnia_mcu_register_gpiochip()
1036 mcu->gc.irq.handler = handle_bad_irq; in omnia_mcu_register_gpiochip()
1037 mcu->gc.irq.threaded = true; in omnia_mcu_register_gpiochip()
1039 mcu->gc.irq.init_hw = omnia_irq_init_hw; in omnia_mcu_register_gpiochip()
1040 mcu->gc.irq.init_valid_mask = omnia_irq_init_valid_mask; in omnia_mcu_register_gpiochip()
1042 err = devm_gpiochip_add_data(dev, &mcu->gc, mcu); in omnia_mcu_register_gpiochip()
1053 err = omnia_read_status_word_old_fw(mcu, &mcu->last_status); in omnia_mcu_register_gpiochip()
1058 INIT_DELAYED_WORK(&mcu->button_release_emul_work, in omnia_mcu_register_gpiochip()
1068 err = devm_request_threaded_irq(dev, mcu->client->irq, NULL, in omnia_mcu_register_gpiochip()
1070 "turris-omnia-mcu", mcu); in omnia_mcu_register_gpiochip()
1089 &mcu->button_release_emul_work); in omnia_mcu_register_gpiochip()