From patchwork Mon May 8 14:24:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudiu Beznea X-Patchwork-Id: 9716271 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F15726035D for ; Mon, 8 May 2017 14:25:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E1F802522B for ; Mon, 8 May 2017 14:25:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D617E2621B; Mon, 8 May 2017 14:25:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5E3312522B for ; Mon, 8 May 2017 14:25:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fqbjhXJSdhaLIrv5qg1Lj7NernpBtRtoMbfT62PaALk=; b=aRhrWGzOXMTqLE gegPHBWmA+yhnjjGsc8VotnLaz6WmmR6tv5JZpWinMW0Wfm9OZJPuTmYWZVQr06xEdmB62+8alTBj BsZPF1cwZ1w+lgKGhbUaOIR6RmZnEpq81cQ7f75hbLJfWXDf6vVr8t3h3lN0niIsMomBRXkq2TYNg UIdGuze2QwSLrxlVM2J4MgiI+0++3fchtkRXGhIfBsak0h3CUryL19iXM/a05OFfaFw8F9bAJthqZ QKAsD6GHFVuKkzjJFkztzkxTXi9u9fQKsuKA7GHQb57K0ZCzqsyLFdcN2sdUcAQlL97/enhq3OL1x 89EppW3P8u19rMdKBnfw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1d7jbY-0001XS-Ns; Mon, 08 May 2017 14:25:40 +0000 Received: from esa3.microchip.iphmx.com ([68.232.153.233]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1d7jb6-0008FB-CQ for linux-arm-kernel@lists.infradead.org; Mon, 08 May 2017 14:25:14 +0000 X-IronPort-AV: E=Sophos;i="5.38,309,1491289200"; d="scan'208";a="2601562" Received: from exsmtp03.microchip.com (HELO email.microchip.com) ([198.175.253.49]) by esa3.microchip.iphmx.com with ESMTP/TLS/AES128-SHA; 08 May 2017 07:24:53 -0700 Received: from m18063-ThinkPad-T460p.mchp-main.com (10.10.76.4) by chn-sv-exch03.mchp-main.com (10.10.76.49) with Microsoft SMTP Server id 14.3.181.6; Mon, 8 May 2017 07:24:52 -0700 From: Claudiu Beznea To: , , , , , , , Subject: [PATCH 2/2] drivers: pwm: pwm-atmel: implement pwm dead-times Date: Mon, 8 May 2017 17:24:23 +0300 Message-ID: <1494253463-26993-3-git-send-email-claudiu.beznea@microchip.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1494253463-26993-1-git-send-email-claudiu.beznea@microchip.com> References: <1494253463-26993-1-git-send-email-claudiu.beznea@microchip.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170508_072512_520593_F81B2E7B X-CRM114-Status: GOOD ( 14.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: claudiu.beznea@microchip.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Implement PWM dead-times for atmel PWM controllers. Since this driver is used by PWM controllers which supports dead-times and PWM controllers which doesn't, add specific input for dead-time register in atmel register private data structure. Signed-off-by: Claudiu Beznea --- drivers/pwm/pwm-atmel.c | 62 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/drivers/pwm/pwm-atmel.c b/drivers/pwm/pwm-atmel.c index 530d7dc..4fba167 100644 --- a/drivers/pwm/pwm-atmel.c +++ b/drivers/pwm/pwm-atmel.c @@ -33,8 +33,9 @@ #define PWM_CMR 0x0 /* Bit field in CMR */ -#define PWM_CMR_CPOL (1 << 9) -#define PWM_CMR_UPD_CDTY (1 << 10) +#define PWM_CMR_CPOL BIT(9) +#define PWM_CMR_UPD_CDTY BIT(10) +#define PWM_CMR_DTE BIT(16) #define PWM_CMR_CPRE_MSK 0xF /* The following registers for PWM v1 */ @@ -47,6 +48,7 @@ #define PWMV2_CDTYUPD 0x08 #define PWMV2_CPRD 0x0C #define PWMV2_CPRDUPD 0x10 +#define PWMV2_DT 0x18 /* * Max value for duty and period @@ -63,6 +65,7 @@ struct atmel_pwm_registers { u8 period_upd; u8 duty; u8 duty_upd; + u8 dt; }; struct atmel_pwm_chip { @@ -161,6 +164,32 @@ static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm, atmel_pwm->regs->duty_upd, cdty); } +static int atmel_pwm_calculate_deadtime(struct pwm_chip *chip, + struct pwm_state *state, + unsigned long cprd, unsigned long *dt) +{ + unsigned long long cycles; + + if (state->deadtime_fe > state->duty_cycle || + state->deadtime_re > state->period - state->duty_cycle) + return -EINVAL; + + *dt = 0; + if (state->deadtime_fe) { + cycles = (unsigned long long)state->deadtime_fe * cprd; + do_div(cycles, state->period); + *dt = (cycles << 16); + } + + if (state->deadtime_re) { + cycles = (unsigned long long)state->deadtime_re * cprd; + do_div(cycles, state->period); + *dt |= cycles; + } + + return 0; +} + static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip, struct pwm_device *pwm, unsigned long cprd, unsigned long cdty) @@ -214,7 +243,7 @@ static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, { struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); struct pwm_state cstate; - unsigned long cprd, cdty; + unsigned long cprd, cdty, dt; u32 pres, val; int ret; @@ -223,7 +252,9 @@ static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, if (state->enabled) { if (cstate.enabled && cstate.polarity == state->polarity && - cstate.period == state->period) { + cstate.period == state->period && + cstate.deadtime_re == state->deadtime_re && + cstate.deadtime_fe == state->deadtime_fe) { cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, atmel_pwm->regs->period); atmel_pwm_calculate_cdty(state, cprd, &cdty); @@ -239,6 +270,16 @@ static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, return ret; } + if (atmel_pwm->regs->dt) { + ret = atmel_pwm_calculate_deadtime(chip, state, cprd, + &dt); + if (ret) { + dev_err(chip->dev, + "failed to calculate dead-time\n"); + return ret; + } + } + atmel_pwm_calculate_cdty(state, cprd, &cdty); if (cstate.enabled) { @@ -258,8 +299,19 @@ static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, val &= ~PWM_CMR_CPOL; else val |= PWM_CMR_CPOL; + + if (atmel_pwm->regs->dt) { + if (dt) + val |= PWM_CMR_DTE; + else + val &= ~PWM_CMR_DTE; + } + atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val); atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty); + if (atmel_pwm->regs->dt) + atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, + atmel_pwm->regs->dt, dt); mutex_lock(&atmel_pwm->isr_lock); atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR); atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm); @@ -282,6 +334,7 @@ static const struct atmel_pwm_registers atmel_pwm_regs_v1 = { .period_upd = PWMV1_CUPD, .duty = PWMV1_CDTY, .duty_upd = PWMV1_CUPD, + .dt = 0, }; static const struct atmel_pwm_registers atmel_pwm_regs_v2 = { @@ -289,6 +342,7 @@ static const struct atmel_pwm_registers atmel_pwm_regs_v2 = { .period_upd = PWMV2_CPRDUPD, .duty = PWMV2_CDTY, .duty_upd = PWMV2_CDTYUPD, + .dt = PWMV2_DT, }; static const struct platform_device_id atmel_pwm_devtypes[] = {