// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2013, Sony Mobile Communications AB. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
*/
/** * struct msm_pinctrl - state for a pinctrl-msm device * @dev: device handle. * @pctrl: pinctrl handle. * @chip: gpiochip handle. * @desc: pin controller descriptor * @irq: parent irq for the TLMM irq_chip. * @intr_target_use_scm: route irq to application cpu using scm calls * @lock: Spinlock to protect register resources as well * as msm_pinctrl data structures. * @enabled_irqs: Bitmap of currently enabled irqs. * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge * detection. * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller * @disabled_for_mux: These IRQs were disabled because we muxed away. * @ever_gpio: This bit is set the first time we mux a pin to gpio_func. * @soc: Reference to soc_data of platform specific data. * @regs: Base addresses for the TLMM tiles. * @phys_base: Physical base address
*/ struct msm_pinctrl { struct device *dev; struct pinctrl_dev *pctrl; struct gpio_chip chip; struct pinctrl_desc desc;
for (i = 0; i < g->nfuncs; i++) { if (g->funcs[i] == function) break;
}
if (WARN_ON(i == g->nfuncs)) return -EINVAL;
/* * If an GPIO interrupt is setup on this pin then we need special * handling. Specifically interrupt detection logic will still see * the pin twiddle even when we're muxed away. * * When we see a pin with an interrupt setup on it then we'll disable * (mask) interrupts on it when we mux away until we mux back. Note * that disable_irq() refcounts and interrupts are disabled as long as * at least one disable_irq() has been called.
*/ if (d && i != gpio_func &&
!test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
disable_irq(irq);
raw_spin_lock_irqsave(&pctrl->lock, flags);
val = msm_readl_ctl(pctrl, g);
/* * If this is the first time muxing to GPIO and the direction is * output, make sure that we're not going to be glitching the pin * by reading the current state of the pin and setting it as the * output.
*/ if (i == gpio_func && (val & BIT(g->oe_bit)) &&
!test_and_set_bit(group, pctrl->ever_gpio)) {
u32 io_val = msm_readl_io(pctrl, g);
if (io_val & BIT(g->in_bit)) { if (!(io_val & BIT(g->out_bit)))
msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
} else { if (io_val & BIT(g->out_bit))
msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
}
}
if (egpio_func && i == egpio_func) { if (val & BIT(g->egpio_present))
val &= ~BIT(g->egpio_enable);
} else {
val &= ~mask;
val |= i << g->mux_bit; /* Claim ownership of pin if egpio capable */ if (egpio_func && val & BIT(g->egpio_present))
val |= BIT(g->egpio_enable);
}
msm_writel_ctl(val, pctrl, g);
raw_spin_unlock_irqrestore(&pctrl->lock, flags);
if (d && i == gpio_func &&
test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) { /* * Clear interrupts detected while not GPIO since we only * masked things.
*/ if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false); else
msm_ack_intr_status(pctrl, g);
/* Convert register value to pinconf value */ switch (param) { case PIN_CONFIG_BIAS_DISABLE: if (arg != MSM_NO_PULL) return -EINVAL;
arg = 1; break; case PIN_CONFIG_BIAS_PULL_DOWN: if (arg != MSM_PULL_DOWN) return -EINVAL;
arg = 1; break; case PIN_CONFIG_BIAS_BUS_HOLD: if (pctrl->soc->pull_no_keeper) return -ENOTSUPP;
if (arg != MSM_KEEPER) return -EINVAL;
arg = 1; break; case PIN_CONFIG_BIAS_PULL_UP: if (pctrl->soc->pull_no_keeper)
arg = arg == MSM_PULL_UP_NO_KEEPER; elseif (arg & BIT(g->i2c_pull_bit))
arg = MSM_I2C_STRONG_PULL_UP; else
arg = arg == MSM_PULL_UP; if (!arg) return -EINVAL; break; case PIN_CONFIG_DRIVE_OPEN_DRAIN: /* Pin is not open-drain */ if (!arg) return -EINVAL;
arg = 1; break; case PIN_CONFIG_DRIVE_STRENGTH:
arg = msm_regval_to_drive(arg); break; case PIN_CONFIG_OUTPUT: /* Pin is not output */ if (!arg) return -EINVAL;
val = msm_readl_io(pctrl, g);
arg = !!(val & BIT(g->in_bit)); break; case PIN_CONFIG_OUTPUT_ENABLE: if (!arg) return -EINVAL; break; default: return -ENOTSUPP;
}
for (i = 0; i < num_configs; i++) {
param = pinconf_to_config_param(configs[i]);
arg = pinconf_to_config_argument(configs[i]);
ret = msm_config_reg(pctrl, g, param, &mask, &bit); if (ret < 0) return ret;
/* Convert pinconf values to register values */ switch (param) { case PIN_CONFIG_BIAS_DISABLE:
arg = MSM_NO_PULL; break; case PIN_CONFIG_BIAS_PULL_DOWN:
arg = MSM_PULL_DOWN; break; case PIN_CONFIG_BIAS_BUS_HOLD: if (pctrl->soc->pull_no_keeper) return -ENOTSUPP;
arg = MSM_KEEPER; break; case PIN_CONFIG_BIAS_PULL_UP: if (pctrl->soc->pull_no_keeper)
arg = MSM_PULL_UP_NO_KEEPER; elseif (g->i2c_pull_bit && arg == MSM_I2C_STRONG_PULL_UP)
arg = BIT(g->i2c_pull_bit) | MSM_PULL_UP; else
arg = MSM_PULL_UP; break; case PIN_CONFIG_DRIVE_OPEN_DRAIN:
arg = 1; break; case PIN_CONFIG_DRIVE_STRENGTH: /* Check for invalid values */ if (arg > 16 || arg < 2 || (arg % 2) != 0)
arg = -1; else
arg = (arg / 2) - 1; break; case PIN_CONFIG_OUTPUT: /* set output value */
raw_spin_lock_irqsave(&pctrl->lock, flags);
val = msm_readl_io(pctrl, g); if (arg)
val |= BIT(g->out_bit); else
val &= ~BIT(g->out_bit);
msm_writel_io(val, pctrl, g);
raw_spin_unlock_irqrestore(&pctrl->lock, flags);
/* enable output */
arg = 1; break; case PIN_CONFIG_INPUT_ENABLE: /* * According to pinctrl documentation this should * actually be a no-op. * * The docs are explicit that "this does not affect * the pin's ability to drive output" but what we do * here is to modify the output enable bit. Thus, to * follow the docs we should remove that. * * The docs say that we should enable any relevant * input buffer, but TLMM there is no input buffer that * can be enabled/disabled. It's always on. * * The points above, explain why this _should_ be a * no-op. However, for historical reasons and to * support old device trees, we'll violate the docs * and still affect the output. * * It should further be noted that this old historical * behavior actually overrides arg to 0. That means * that "input-enable" and "input-disable" in a device * tree would _both_ disable the output. We'll * continue to preserve this behavior as well since * we have no other use for this attribute.
*/
arg = 0; break; case PIN_CONFIG_OUTPUT_ENABLE:
arg = !!arg; break; default:
dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
param); return -EINVAL;
}
/* Range-check user-supplied value */ if (arg & ~mask) {
dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg); return -EINVAL;
}
raw_spin_lock_irqsave(&pctrl->lock, flags);
val = msm_readl_ctl(pctrl, g);
val &= ~(mask << bit);
val |= arg << bit;
msm_writel_ctl(val, pctrl, g);
raw_spin_unlock_irqrestore(&pctrl->lock, flags);
}
/* For dual-edge interrupts in software, since some hardware has no * such support: * * At appropriate moments, this function may be called to flip the polarity * settings of both-edge irq lines to try and catch the next edge. * * The attempt is considered successful if: * - the status bit goes high, indicating that an edge was caught, or * - the input value of the gpio doesn't change during the attempt. * If the value changes twice during the process, that would cause the first * test to fail but would force the second, as two opposite * transitions would cause a detection no matter the polarity setting. * * The do-loop tries to sledge-hammer closed the timing hole between * the initial value-read and the polarity-write - if the line value changes * during that window, an interrupt is lost, the new polarity setting is * incorrect, and the first success test will fail, causing a retry. * * Algorithm comes from Google's msmgpio driver.
*/ staticvoid msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl, conststruct msm_pingroup *g, struct irq_data *d)
{ int loop_limit = 100; unsigned val, val2, intstat; unsigned pol;
do {
val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
pol = msm_readl_intr_cfg(pctrl, g);
pol ^= BIT(g->intr_polarity_bit);
msm_writel_intr_cfg(pol, pctrl, g);
if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) return;
g = &pctrl->soc->groups[d->hwirq];
raw_spin_lock_irqsave(&pctrl->lock, flags);
val = msm_readl_intr_cfg(pctrl, g); /* * There are two bits that control interrupt forwarding to the CPU. The * RAW_STATUS_EN bit causes the level or edge sensed on the line to be * latched into the interrupt status register when the hardware detects * an irq that it's configured for (either edge for edge type or level * for level type irq). The 'non-raw' status enable bit causes the * hardware to assert the summary interrupt to the CPU if the latched * status bit is set. There's a bug though, the edge detection logic * seems to have a problem where toggling the RAW_STATUS_EN bit may * cause the status bit to latch spuriously when there isn't any edge * so we can't touch that bit for edge type irqs and we have to keep * the bit set anyway so that edges are latched while the line is masked. * * To make matters more complicated, leaving the RAW_STATUS_EN bit * enabled all the time causes level interrupts to re-latch into the * status register because the level is still present on the line after * we ack it. We clear the raw status enable bit during mask here and * set the bit on unmask so the interrupt can't latch into the hardware * while it's masked.
*/ if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
val &= ~BIT(g->intr_raw_status_bit);
val &= ~BIT(g->intr_enable_bit);
msm_writel_intr_cfg(val, pctrl, g);
if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
msm_gpio_irq_mask(d);
gpiochip_disable_irq(gc, d->hwirq);
}
/** * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent. * @d: The irq dta. * * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are * normally handled by the parent irqchip. The logic here is slightly * different due to what's easy to do with our parent, but in principle it's * the same.
*/ staticvoid msm_gpio_update_dual_edge_parent(struct irq_data *d)
{ struct gpio_chip *gc = irq_data_get_irq_chip_data(d); struct msm_pinctrl *pctrl = gpiochip_get_data(gc); conststruct msm_pingroup *g = &pctrl->soc->groups[d->hwirq]; int loop_limit = 100; unsignedint val; unsignedint type;
/* Read the value and make a guess about what edge we need to catch */
val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
do { /* Set the parent to catch the next edge */
irq_chip_set_type_parent(d, type);
/* * Possibly the line changed between when we last read "val" * (and decided what edge we needed) and when set the edge. * If the value didn't change (or changed and then changed * back) then we're done.
*/
val = msm_readl_io(pctrl, g) & BIT(g->in_bit); if (type == IRQ_TYPE_EDGE_RISING) { if (!val) return;
type = IRQ_TYPE_EDGE_FALLING;
} elseif (type == IRQ_TYPE_EDGE_FALLING) { if (val) return;
type = IRQ_TYPE_EDGE_RISING;
}
} while (loop_limit-- > 0);
dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
}
/* * For hw without possibility of detecting both edges
*/ if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
set_bit(d->hwirq, pctrl->dual_edge_irqs); else
clear_bit(d->hwirq, pctrl->dual_edge_irqs);
/* Route interrupts to application cpu. * With intr_target_use_scm interrupts are routed to * application cpu using scm calls.
*/ if (g->intr_target_width)
intr_target_mask = GENMASK(g->intr_target_width - 1, 0);
if (pctrl->intr_target_use_scm) {
u32 addr = pctrl->phys_base[0] + g->intr_target_reg; int ret;
qcom_scm_io_readl(addr, &val);
val &= ~(intr_target_mask << g->intr_target_bit);
val |= g->intr_target_kpss_val << g->intr_target_bit;
ret = qcom_scm_io_writel(addr, val); if (ret)
dev_err(pctrl->dev, "Failed routing %lu interrupt to Apps proc",
d->hwirq);
} else {
val = msm_readl_intr_target(pctrl, g);
val &= ~(intr_target_mask << g->intr_target_bit);
val |= g->intr_target_kpss_val << g->intr_target_bit;
msm_writel_intr_target(val, pctrl, g);
}
/* Update configuration for gpio. * RAW_STATUS_EN is left on for all gpio irqs. Due to the * internal circuitry of TLMM, toggling the RAW_STATUS * could cause the INTR_STATUS to be set for EDGE interrupts.
*/
val = oldval = msm_readl_intr_cfg(pctrl, g);
val |= BIT(g->intr_raw_status_bit); if (g->intr_detection_width == 2) {
val &= ~(3 << g->intr_detection_bit);
val &= ~(1 << g->intr_polarity_bit); switch (type) { case IRQ_TYPE_EDGE_RISING:
val |= 1 << g->intr_detection_bit;
val |= BIT(g->intr_polarity_bit); break; case IRQ_TYPE_EDGE_FALLING:
val |= 2 << g->intr_detection_bit;
val |= BIT(g->intr_polarity_bit); break; case IRQ_TYPE_EDGE_BOTH:
val |= 3 << g->intr_detection_bit;
val |= BIT(g->intr_polarity_bit); break; case IRQ_TYPE_LEVEL_LOW: break; case IRQ_TYPE_LEVEL_HIGH:
val |= BIT(g->intr_polarity_bit); break;
}
} elseif (g->intr_detection_width == 1) {
val &= ~(1 << g->intr_detection_bit);
val &= ~(1 << g->intr_polarity_bit); switch (type) { case IRQ_TYPE_EDGE_RISING:
val |= BIT(g->intr_detection_bit);
val |= BIT(g->intr_polarity_bit); break; case IRQ_TYPE_EDGE_FALLING:
val |= BIT(g->intr_detection_bit); break; case IRQ_TYPE_EDGE_BOTH:
val |= BIT(g->intr_detection_bit);
val |= BIT(g->intr_polarity_bit); break; case IRQ_TYPE_LEVEL_LOW: break; case IRQ_TYPE_LEVEL_HIGH:
val |= BIT(g->intr_polarity_bit); break;
}
} else {
BUG();
}
msm_writel_intr_cfg(val, pctrl, g);
/* * The first time we set RAW_STATUS_EN it could trigger an interrupt. * Clear the interrupt. This is safe because we have * IRQCHIP_SET_TYPE_MASKED. When changing the interrupt type, we could * also still have a non-matching interrupt latched, so clear whenever * making changes to the interrupt configuration.
*/ if (val != oldval)
msm_ack_intr_status(pctrl, g);
if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
msm_gpio_update_dual_edge_pos(pctrl, g, d);
/* * While they may not wake up when the TLMM is powered off, * some GPIOs would like to wakeup the system from suspend * when TLMM is powered on. To allow that, enable the GPIO * summary line to be wakeup capable at GIC.
*/ if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs)) return irq_chip_set_wake_parent(d, on);
ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq); if (ret) goto out;
msm_gpio_direction_input(gc, d->hwirq);
if (gpiochip_lock_as_irq(gc, d->hwirq)) {
dev_err(gc->parent, "unable to lock HW IRQ %lu for IRQ\n",
d->hwirq);
ret = -EINVAL; goto out;
}
/* * The disable / clear-enable workaround we do in msm_pinmux_set_mux() * only works if disable is not lazy since we only clear any bogus * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
*/
irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
/* * If the wakeup_enable bit is present and marked as available for the * requested GPIO, it should be enabled when the GPIO is marked as * wake irq in order to allow the interrupt event to be transfered to * the PDC HW. * While the name implies only the wakeup event, it's also required for * the interrupt event.
*/ if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) {
u32 intr_cfg;
/* Disable the wakeup_enable bit if it has been set in msm_gpio_irq_reqres() */ if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) {
u32 intr_cfg;
/* * Each pin has it's own IRQ status register, so use * enabled_irq bitmap to limit the number of reads.
*/
for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
g = &pctrl->soc->groups[i];
val = msm_readl_intr_status(pctrl, g); if (val & BIT(g->intr_status_bit)) {
generic_handle_domain_irq(gc->irq.domain, i);
handled++;
}
}
/* No interrupts were flagged */ if (handled == 0)
handle_bad_irq(desc);
np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0); if (np) {
chip->irq.parent_domain = irq_find_matching_host(np,
DOMAIN_BUS_WAKEUP);
of_node_put(np); if (!chip->irq.parent_domain) return -EPROBE_DEFER;
chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq; /* * Let's skip handling the GPIOs, if the parent irqchip * is handling the direct connect IRQ of the GPIO.
*/
skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain); for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
gpio = pctrl->soc->wakeirq_map[i].gpio;
set_bit(gpio, pctrl->skip_wake_irqs);
}
}
ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl); if (ret) {
dev_err(pctrl->dev, "Failed register gpiochip\n"); return ret;
}
/* * For DeviceTree-supported systems, the gpio core checks the * pinctrl's device node for the "gpio-ranges" property. * If it is present, it takes care of adding the pin ranges * for the driver. In this case the driver can skip ahead. * * In order to remain compatible with older, existing DeviceTree * files which don't set the "gpio-ranges" property or systems that * utilize ACPI the driver has to call gpiochip_add_pin_range().
*/ if (!of_property_present(pctrl->dev->of_node, "gpio-ranges")) {
ret = gpiochip_add_pin_range(&pctrl->chip,
dev_name(pctrl->dev), 0, 0, chip->ngpio); if (ret) {
dev_err(pctrl->dev, "Failed to add pin range\n"); return ret;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.