Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3043345imm; Sun, 5 Aug 2018 19:33:21 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeHuWs2EX+aYn7ZIMXSA6hsMqYfUK+AgDV4Q9nli1Vo6RrRzgnPMtk2VPLaJiTK0L5DHwaz X-Received: by 2002:a17:902:e85:: with SMTP id 5-v6mr12120109plx.73.1533522801936; Sun, 05 Aug 2018 19:33:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533522801; cv=none; d=google.com; s=arc-20160816; b=yrRyksrJoqZM/4smS/zG42ZCM8bPuHElA37XhuUSu/b6BvlsJrnEyTP1vQn1QeKtTD Vx5y6RE6O/qMT6RSUpZNIr6CJg6QIzR7ds7xkB3vQGjNwMN1lwrQR8U/MnivJFmzZzri r9VMCCm9gr2TuessoKqj7T7lqPCmdWG316ez8wIcpdoCmdFjguqWdVn5Zt+SzOwmGrPH IQkJP1rQc4kSzYN9djbbnd7DR3na5DxQiIknbwaWZEpT/zqzEOcvnaTWhD/PXMxWDrei NadfVGQMhoqYRxGjLVB7O50LSzRlapAfcitbfi8CxqYURFcvTjHIidMPfcXxYqfvLcK5 WfmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=hST8uAE3i0ps1v9iT3TtNfaCpRHMKGjHw1lUOrkJvKE=; b=LSed53ZgcC2zP34Gr11ZYHwgKnLbrAJsV2omuDVGAlZs6JdSyIImoDebneiZoPs/pM ez7Vi0paI1hIrKzsbj3OWHZvXNmPh0oBY1Vgv2UO+9Rsv7WAEOfwzSkQ8kW6KGdThERt 0I1FK4WResvLzCBj/No9cF2KMkCeRf6JwY82T4e0RZGiQ3P/lHL4LIC1whvzsJMPsct4 5bp91b/w0ExYIxJzAd+OmRlqRzCHrYmjfRoUrUEDPcLkpYbWwPCHHvf2iPIyKtEnIMJ+ 61N62gugizlaU/QkquMfXmsPgLhNO4t6mv1zwrcPqPNEdMt3KtUzl69Zzcny2UcR4FmR 3Lmw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h18-v6si10606866pgd.238.2018.08.05.19.33.01; Sun, 05 Aug 2018 19:33:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727099AbeHFEid (ORCPT + 99 others); Mon, 6 Aug 2018 00:38:33 -0400 Received: from relay6-d.mail.gandi.net ([217.70.183.198]:47245 "EHLO relay6-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726143AbeHFEic (ORCPT ); Mon, 6 Aug 2018 00:38:32 -0400 X-Originating-IP: 112.215.208.161 Received: from ubuntuVM.localdomain (unknown [112.215.208.161]) (Authenticated sender: aditya@kobol.io) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 453CAC0007; Mon, 6 Aug 2018 02:31:33 +0000 (UTC) From: Aditya Prayoga To: linux-gpio@vger.kernel.org Cc: Richard Genoud , Gregory CLEMENT , Gauthier Provost , Alban Browaeys , Thierry Reding , Linus Walleij , linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org, Dennis Gilmore , Ralph Sennhauser , Andrew Lunn , Aditya Prayoga Subject: [PATCH RESEND 2/2] gpio: mvebu: Allow to use non-default PWM counter Date: Mon, 6 Aug 2018 10:29:16 +0800 Message-Id: <1533522556-55055-3-git-send-email-aditya@kobol.io> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1533522556-55055-1-git-send-email-aditya@kobol.io> References: <1533522556-55055-1-git-send-email-aditya@kobol.io> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On multiple PWM lines, if the other PWM counter is unused, allocate it to next PWM request. The priority would be: 1. Default counter assigned to the bank 2. Unused counter that is assigned to other bank 3. Fallback to default counter For example on second bank there are three PWM request, first one would use default counter (counter B), second one would try to use counter A, and the third one would use counter B. Signed-off-by: Aditya Prayoga --- drivers/gpio/gpio-mvebu.c | 58 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpio-mvebu.c b/drivers/gpio/gpio-mvebu.c index 0617e66..5a39478 100644 --- a/drivers/gpio/gpio-mvebu.c +++ b/drivers/gpio/gpio-mvebu.c @@ -92,6 +92,11 @@ #define MVEBU_MAX_GPIO_PER_BANK 32 +enum mvebu_pwm_counter { + MVEBU_PWM_COUNTER_A = 0, + MVEBU_PWM_COUNTER_B, +}; + struct mvebu_pwm_item { struct gpio_desc *gpiod; struct pwm_device *device; @@ -101,7 +106,8 @@ struct mvebu_pwm_item { struct mvebu_pwm { void __iomem *membase; unsigned long clk_rate; - int id; + enum mvebu_pwm_counter id; + struct list_head node; struct list_head pwms; struct pwm_chip chip; spinlock_t lock; @@ -113,6 +119,8 @@ struct mvebu_pwm { u32 blink_off_duration; }; +static LIST_HEAD(mvebu_pwm_list); + struct mvebu_gpio_chip { struct gpio_chip chip; struct regmap *regs; @@ -601,12 +609,24 @@ static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip) return container_of(chip, struct mvebu_pwm, chip); } +static struct mvebu_pwm *mvebu_pwm_get_avail_counter(void) +{ + struct mvebu_pwm *counter; + + list_for_each_entry(counter, &mvebu_pwm_list, node) { + if (list_empty(&counter->pwms)) + return counter; + } + return NULL; +} + static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) { struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip); struct mvebu_gpio_chip *mvchip = mvpwm->mvchip; struct gpio_desc *desc; struct mvebu_pwm_item *item; + struct mvebu_pwm *counter; unsigned long flags; int ret = 0; @@ -615,6 +635,14 @@ static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) return -ENODEV; spin_lock_irqsave(&mvpwm->lock, flags); + regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, + &mvchip->blink_en_reg); + + if (mvchip->blink_en_reg & BIT(pwm->hwpwm)) { + ret = -EBUSY; + goto out; + } + desc = gpiochip_request_own_desc(&mvchip->chip, pwm->hwpwm, "mvebu-pwm"); if (IS_ERR(desc)) { @@ -627,10 +655,25 @@ static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) gpiochip_free_own_desc(desc); goto out; } + + counter = mvpwm; + if (!list_empty(&mvpwm->pwms)) { + counter = mvebu_pwm_get_avail_counter(); + if (counter) + pwm->chip_data = counter; + else + counter = mvpwm; + } + + regmap_update_bits(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + + mvchip->offset, BIT(pwm->hwpwm), + counter->id ? BIT(pwm->hwpwm) : 0); + regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + + mvchip->offset, &mvpwm->blink_select); item->gpiod = desc; item->device = pwm; INIT_LIST_HEAD(&item->node); - list_add_tail(&item->node, &mvpwm->pwms); + list_add_tail(&item->node, &counter->pwms); out: spin_unlock_irqrestore(&mvpwm->lock, flags); return ret; @@ -642,6 +685,9 @@ static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) struct mvebu_pwm_item *item, *tmp; unsigned long flags; + if (pwm->chip_data) + mvpwm = (struct mvebu_pwm *) pwm->chip_data; + list_for_each_entry_safe(item, tmp, &mvpwm->pwms, node) { if (item->device == pwm) { spin_lock_irqsave(&mvpwm->lock, flags); @@ -665,6 +711,9 @@ static void mvebu_pwm_get_state(struct pwm_chip *chip, unsigned long flags; u32 u; + if (pwm->chip_data) + mvpwm = (struct mvebu_pwm *) pwm->chip_data; + spin_lock_irqsave(&mvpwm->lock, flags); val = (unsigned long long) @@ -712,6 +761,9 @@ static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, unsigned long flags; unsigned int on, off; + if (pwm->chip_data) + mvpwm = (struct mvebu_pwm *) pwm->chip_data; + val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle; do_div(val, NSEC_PER_SEC); if (val > UINT_MAX) @@ -823,6 +875,7 @@ static int mvebu_pwm_probe(struct platform_device *pdev, mvpwm->mvchip = mvchip; mvpwm->id = id; INIT_LIST_HEAD(&mvpwm->pwms); + INIT_LIST_HEAD(&mvpwm->node); mvpwm->membase = devm_ioremap_resource(dev, res); if (IS_ERR(mvpwm->membase)) @@ -846,6 +899,7 @@ static int mvebu_pwm_probe(struct platform_device *pdev, mvpwm->chip.base = -1; spin_lock_init(&mvpwm->lock); + list_add_tail(&mvpwm->node, &mvebu_pwm_list); return pwmchip_add(&mvpwm->chip); } -- 2.7.4