Lines Matching +full:turris +full:- +full:omnia +full:- +full:mcu
1 // SPDX-License-Identifier: GPL-2.0
3 * CZ.NIC's Turris Omnia MCU GPIO and IRQ driver
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 */
186 /* GPIOs with value read from the 16-bit wide extended control status */
233 static int omnia_ctl_cmd_locked(struct omnia_mcu *mcu, u8 cmd, u16 val, u16 mask) in omnia_ctl_cmd_locked() argument
257 return omnia_cmd_write(mcu->client, buf, len); in omnia_ctl_cmd_locked()
260 static int omnia_ctl_cmd(struct omnia_mcu *mcu, u8 cmd, u16 val, u16 mask) in omnia_ctl_cmd() argument
262 guard(mutex)(&mcu->lock); in omnia_ctl_cmd()
264 return omnia_ctl_cmd_locked(mcu, cmd, val, mask); in omnia_ctl_cmd()
270 return -EINVAL; in omnia_gpio_request()
277 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_get_direction() local
282 scoped_guard(mutex, &mcu->lock) { in omnia_gpio_get_direction()
283 val = omnia_cmd_read_bit(mcu->client, in omnia_gpio_get_direction()
305 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_direction_input() local
308 return omnia_ctl_cmd(mcu, OMNIA_CMD_EXT_CONTROL, in omnia_gpio_direction_input()
312 if (gpio->ctl_cmd) in omnia_gpio_direction_input()
313 return -ENOTSUPP; in omnia_gpio_direction_input()
322 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_direction_output() local
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()
340 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_get() local
344 * of every change of the status word by an interrupt from MCU and save in omnia_gpio_get()
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()
385 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_get_multiple() local
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()
444 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_set() local
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()
460 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_set_multiple() local
488 guard(mutex)(&mcu->lock); in omnia_gpio_set_multiple()
491 omnia_ctl_cmd_locked(mcu, OMNIA_CMD_GENERAL_CONTROL, in omnia_gpio_set_multiple()
495 omnia_ctl_cmd_locked(mcu, OMNIA_CMD_EXT_CONTROL, in omnia_gpio_set_multiple()
499 static bool omnia_gpio_available(struct omnia_mcu *mcu, in omnia_gpio_available() argument
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()
515 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_gpio_init_valid_mask() local
520 if (gpio->cmd || is_int_bit_valid(gpio)) in omnia_gpio_init_valid_mask()
522 omnia_gpio_available(mcu, 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()
560 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_shutdown() local
564 __clear_bit(bit, &mcu->rising); in omnia_irq_shutdown()
565 __clear_bit(bit, &mcu->falling); in omnia_irq_shutdown()
571 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_mask() local
576 __clear_bit(bit, &mcu->rising); in omnia_irq_mask()
577 __clear_bit(bit, &mcu->mask); in omnia_irq_mask()
584 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_unmask() local
589 __set_bit(bit, &mcu->mask); in omnia_irq_unmask()
591 __set_bit(bit, &mcu->rising); in omnia_irq_unmask()
597 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_set_type() local
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()
616 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_bus_lock() local
618 /* nothing to do if MCU firmware does not support new interrupt API */ in omnia_irq_bus_lock()
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.
636 * The MCU receives an interrupt mask and reports a pending interrupt bitmap in
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
676 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_bus_sync_unlock() local
677 struct device *dev = &mcu->client->dev; in omnia_irq_bus_sync_unlock()
682 /* nothing to do if MCU firmware does not support new interrupt API */ 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()
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()
716 .name = "Turris Omnia MCU interrupts",
731 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_init_valid_mask() local
738 omnia_gpio_available(mcu, gpio)); in omnia_irq_init_valid_mask()
746 struct omnia_mcu *mcu = gpiochip_get_data(gc); in omnia_irq_init_hw() local
751 return omnia_cmd_write(mcu->client, cmd, sizeof(cmd)); in omnia_irq_init_hw()
766 static bool omnia_irq_read_pending_new(struct omnia_mcu *mcu, in omnia_irq_read_pending_new() argument
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()
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()
804 static int omnia_read_status_word_old_fw(struct omnia_mcu *mcu, in omnia_read_status_word_old_fw() argument
810 err = omnia_cmd_read_u16(mcu->client, OMNIA_CMD_GET_STATUS_WORD, in omnia_read_status_word_old_fw()
826 struct omnia_mcu *mcu = container_of(to_delayed_work(work), in button_release_emul_fn() local
830 mcu->button_pressed_emul = false; in button_release_emul_fn()
831 generic_handle_irq_safe(mcu->client->irq); in button_release_emul_fn()
845 static bool omnia_irq_read_pending_old(struct omnia_mcu *mcu, in omnia_irq_read_pending_old() argument
849 struct device *dev = &mcu->client->dev; in omnia_irq_read_pending_old()
852 guard(mutex)(&mcu->lock); in omnia_irq_read_pending_old()
854 err = omnia_read_status_word_old_fw(mcu, &status); 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()
918 static bool omnia_irq_read_pending(struct omnia_mcu *mcu, in omnia_irq_read_pending() argument
921 if (mcu->features & OMNIA_FEAT_NEW_INT_API) in omnia_irq_read_pending()
922 return omnia_irq_read_pending_new(mcu, pending); in omnia_irq_read_pending()
924 return omnia_irq_read_pending_old(mcu, pending); in omnia_irq_read_pending()
929 struct omnia_mcu *mcu = dev_id; in omnia_irq_thread_handler() local
934 if (!omnia_irq_read_pending(mcu, &pending)) in omnia_irq_thread_handler()
937 domain = mcu->gc.irq.domain; in omnia_irq_thread_handler()
950 static const char * const front_button_modes[] = { "mcu", "cpu" };
955 struct omnia_mcu *mcu = dev_get_drvdata(dev); in front_button_mode_show() local
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()
974 struct omnia_mcu *mcu = dev_get_drvdata(dev); in front_button_mode_store() local
981 err = omnia_ctl_cmd_locked(mcu, OMNIA_CMD_GENERAL_CONTROL, in front_button_mode_store()
1000 int omnia_mcu_register_gpiochip(struct omnia_mcu *mcu) in omnia_mcu_register_gpiochip() argument
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()