Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp716812lqs; Fri, 14 Jun 2024 03:48:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUdrzu0ZjOcYqnCbT1DNVXkpjHXzqYt1CpqO2YJBPWsRlUc3KH3XvXnN17mxMW7chWmdvcXTfoNcL++tnMba1utlcs9IzRL9LuhQjpzyg== X-Google-Smtp-Source: AGHT+IFcxsqTkGq7Gd5eLhZAw6PwJ1LTO04M0QVqptb0DqPBJV68DOCSKA5BvWC7CAi8scHZiW6i X-Received: by 2002:a05:6358:478e:b0:19f:54a7:4f0b with SMTP id e5c5f4694b2df-19fa9e28f6cmr269712955d.19.1718362133418; Fri, 14 Jun 2024 03:48:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718362133; cv=pass; d=google.com; s=arc-20160816; b=G5NlPYKnYojJ0tc6u3qr2Le8mw+OdP6j43XgsMgXgTauVr0HHJEPQnr/sr/8qv1tzN pc4WWnn2efS3rqN+NnwgNQbg4EI9R6adBveb9dFjZdFNfjqB0fqDmDX0u6V5WF/xcTBr f4ZsxWPGWTxODUhXy3Dt047l5oI266zEKDRvmeRff2Dn57XJpWAAskfa4ee4+0N+0sPO e2BQSyO1JDEX7FxKnlasXpS4dwWQ/ga7ZkjwSaUec6HNPIpgTLWqvTXFLOZF2ixcSvDs /JuRT/ySzTkUB3Ad+XOCH93ztGrJx+zVQdHsa8ijaq4/Px6ffe/zq7uND7580Rxfav3w qdXg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=0OriRI7xNCMkxlWs2IiRsExQGF8yht7ObcIi6rihikc=; fh=wsr73u/8RgXWcxLw8pGaiss+pU/7PFpuGi9xmiB3upA=; b=nOPsBqCMQOG3snntpjru4vd4TGgjUHSmvESRvqe1h/UIlLuKJhsfUij28fXV1He2sM qttKNOZWoQLmFCBdP2v2uNlLm2VZcKt+DDJtHUFR/6d30wEwQS1pyPkISC71geUwleNA 75wdgGYJqNypxFai5urxurXko05IczQeLc62KmXdSEHzVFw3omf1l6Ohjv4bSvZHamzM 8CsvVyG+0CMpA39LOiRf/Ie6tYIbtKms0+oLtEC6wVrimlLiwLZ2Upmes4rKrcaos821 VQwl6Vt28lIkROXhKOt22etXrPo40zWk+BM15Nv5VuOOzpEPxfnhE8SvIUA8gFaWHqn5 SJ0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=BLT4kW81; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-214800-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214800-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-7029032d4efsi1227316a12.482.2024.06.14.03.48.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 03:48:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-214800-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=BLT4kW81; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-214800-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214800-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C9B3C283A21 for ; Fri, 14 Jun 2024 10:37:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 965C719414A; Fri, 14 Jun 2024 10:37:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="BLT4kW81" Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C682A194085 for ; Fri, 14 Jun 2024 10:37:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718361451; cv=none; b=DsTfbe67Our79CxDNy6IKxee+eOphP1xFD8R72n5cZmNRDoS510COFW8mNmy6Wm2sJapFOtHgkoGzrUi0qh6NCXdx7IYsOA23adoC51TNkC8+uBQRy8jO6PqlGllWkuLKjwlDd3w+sZlFOpP/1+TZPLQ7xdc4zzfpaAzKeH7YE8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718361451; c=relaxed/simple; bh=zmtM9P8EOqx1FZjRdKTb5n/z6vFq5gDORgSkGrxnYuk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=pi3WF9wjYW5MdYa7t58vroLWi18B9+TVM83n4KE4eeE6HxBIfsb2NSySuJKkWJ4P+rXpWXt2/PrY19IAg6woun8gzWQxmS953Lq0SZyZ/u2NGHKneffwTUu8kOfwx/oyl29vRjdR31wQOOJJT5+4EfaLjiiAsJbEo5s2Q1O+ycE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=BLT4kW81; arc=none smtp.client-ip=209.85.208.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-57c75464e77so2366847a12.0 for ; Fri, 14 Jun 2024 03:37:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1718361447; x=1718966247; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=0OriRI7xNCMkxlWs2IiRsExQGF8yht7ObcIi6rihikc=; b=BLT4kW811cw5ReF4z09DjJF9eWr+0mHyEfOhoX3ondfHVjhsyQQcNOX+sTNwwQP3Be MisNvEdYQgcXrghIIdRy0Ksez+JWulkIHfyRGjzLjSamFHaVXOzeKQFFjgz5XCj/Z9oM RdmZ9LBVzRN7IDaSPYMHQHJ/8clbfuFWVboXRBqzjjPUU3ww31I2CGc/DaT2PRcQ9N2L NJrCX2o4dpcgXMTpo9imzt/Tuz7eoRtwCvSxkjKhtmFj0ebWlkigpHGRxW7k04zkkvlc Iv5+GQkw7rXpIyUlpF7JslX5Hi6+bjcj5Uk6hKmGJUIjIuNxNL0x8pcVsbuvPvAOjwoK anDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718361447; x=1718966247; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0OriRI7xNCMkxlWs2IiRsExQGF8yht7ObcIi6rihikc=; b=KivPujR/MEpKbqBOZBCW+lpiSblE4S545BvLMJl/G/UtQyosCpijB1ZxB/hzIw5OM9 ntTyDUQrocwdmbIVjIgUKu97BWKw6sqQwOGG8M/wB8iPfWctGiz2D62NWn2xYZ9Bx0wJ CxdlUCEIQjNNY4WQ/83NgpCiybz/BAuNUjT8voQgmR1FiafJG/GFcom7zWbqzmIxJxiB Bs5IKkceAjIxRhZEoaNjBDK0ADRSOFiiTIE9qMlCdyguDacEp3Z2MwDWHTfnFhyk1/8q 9b/eZySjHgKuTPzCNSv62QpRJE/YdebkZzuOvi+6E3204DOpRnlBBcP+LmD6AWwAETcH Hhag== X-Gm-Message-State: AOJu0Yyo8seEWFr/TTBcVqMF/AZhI1FR83JLLalEQO2sNXraufntOHHo pdEjdRRxWQv+5kZvu90e3DJIZ8U0UOPImwZgkWOb/f7jkWT3YazwtaRgnNpIZwg= X-Received: by 2002:a50:9ea9:0:b0:57a:2327:d2d2 with SMTP id 4fb4d7f45d1cf-57cbd6c6e54mr1653455a12.29.1718361446835; Fri, 14 Jun 2024 03:37:26 -0700 (PDT) Received: from localhost (p509153eb.dip0.t-ipconnect.de. [80.145.83.235]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57cbb30ba30sm1564062a12.39.2024.06.14.03.37.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 03:37:26 -0700 (PDT) Date: Fri, 14 Jun 2024 12:37:25 +0200 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: William Qiu Cc: linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org, Hal Feng , Philipp Zabel Subject: Re: [PATCH v12] pwm: opencores: Add PWM driver support Message-ID: <6hbj4uua442il6koeaypkqapctlwrhrmsbguyx74hwqzkycepf@7zpqo6mojqvn> References: <20240429075140.56867-1-william.qiu@starfivetech.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="pz7cmbt7u7ur6jmg" Content-Disposition: inline In-Reply-To: <20240429075140.56867-1-william.qiu@starfivetech.com> --pz7cmbt7u7ur6jmg Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hello William, thanks for your patience and sorry for taking so long until I came around to review this. On Mon, Apr 29, 2024 at 03:51:40PM +0800, William Qiu wrote: > diff --git a/drivers/pwm/pwm-ocores.c b/drivers/pwm/pwm-ocores.c > new file mode 100644 > index 000000000000..039fb3c526a7 > --- /dev/null > +++ b/drivers/pwm/pwm-ocores.c > @@ -0,0 +1,240 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * OpenCores PWM Driver > + * > + * https://opencores.org/projects/ptc > + * > + * Copyright (C) 2018-2023 StarFive Technology Co., Ltd. > + * > + * Limitations: > + * - The hardware only do inverted polarity. s/do/does/ > + * - The hardware minimum period / duty_cycle is (1 / pwm_apb clock frequency) ns. > + * - The hardware maximum period / duty_cycle is (U32_MAX / pwm_apb clock frequency) ns. How does the hardware behave on disable? Does it complete the currently running period when reconfiguring or disabling? Are glitches expected in .apply()? Please answer these questions in the Limitations paragraph. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +/* OpenCores Register offsets */ > +#define REG_OCPWM_CNTR 0x0 > +#define REG_OCPWM_HRC 0x4 > +#define REG_OCPWM_LRC 0x8 > +#define REG_OCPWM_CTRL 0xC > + > +/* OCPWM_CTRL register bits*/ > +#define REG_OCPWM_EN BIT(0) I would prefer this one to be called REG_OCPWM_CNTR_EN. Ditto for the following definitions. > +#define REG_OCPWM_ECLK BIT(1) > +#define REG_OCPWM_NEC BIT(2) > +#define REG_OCPWM_OE BIT(3) > +#define REG_OCPWM_SIGNLE BIT(4) > +#define REG_OCPWM_INTE BIT(5) > +#define REG_OCPWM_INT BIT(6) > +#define REG_OCPWM_CNTRRST BIT(7) > +#define REG_OCPWM_CAPTE BIT(8) > + > [...] > +static int ocores_pwm_apply(struct pwm_chip *chip, > + struct pwm_device *pwm, > + const struct pwm_state *state) > +{ > + struct ocores_pwm_device *ddata = chip_to_ocores(chip); > + u32 ctrl_data = 0; > + u64 period_data, duty_data; > + > + if (state->polarity != PWM_POLARITY_INVERSED) > + return -EINVAL; > + > + ctrl_data = ocores_pwm_readl(ddata, pwm->hwpwm, REG_OCPWM_CTRL); > + ocores_pwm_writel(ddata, pwm->hwpwm, REG_OCPWM_CTRL, 0); > + > + period_data = mul_u64_u32_div(state->period, ddata->clk_rate, NSEC_PER_SEC); > + if (period_data > U32_MAX) > + period_data = U32_MAX; This assignment is useless, the value of period_data isn't used later, I think you want: period_data = ... if (!period_data) return -EINVAL if (period_data > U32_MAX) period_data = U32_MAX; ocores_pwm_writel(ddata, pwm->hwpwm, 0x8, (u32)period_data); > + else if (period_data > 0) > + ocores_pwm_writel(ddata, pwm->hwpwm, 0x8, (u32)period_data); > + else > + return -EINVAL; > + > + duty_data = mul_u64_u32_div(state->duty_cycle, ddata->clk_rate, NSEC_PER_SEC); > + if (duty_data <= U32_MAX) > + ocores_pwm_writel(ddata, pwm->hwpwm, REG_OCPWM_HRC, (u32)duty_data); > + else > + return -EINVAL; duty_data > U32_MAX should be handled in the same way as period_data > U32_MAX. > + ocores_pwm_writel(ddata, pwm->hwpwm, REG_OCPWM_CNTR, 0); > + > + if (state->enabled) { > + ctrl_data = ocores_pwm_readl(ddata, pwm->hwpwm, REG_OCPWM_CTRL); > + ocores_pwm_writel(ddata, pwm->hwpwm, REG_OCPWM_CTRL, > + ctrl_data | REG_OCPWM_EN | REG_OCPWM_OE); > + } Wouldn't it make sense to unset REG_OCPWM_EN | REG_OCPWM_OE if (!state->enabled)? > + return 0; > +} > + > +static const struct pwm_ops ocores_pwm_ops = { > + .get_state = ocores_pwm_get_state, > + .apply = ocores_pwm_apply, In other structs you're using a single space before =. I'd prefer that here, too. > +}; > + > +static const struct ocores_pwm_data jh7100_pwm_data = { > + .get_ch_base = starfive_jh71x0_get_ch_base, > +}; > + > +static const struct ocores_pwm_data jh7110_pwm_data = { > + .get_ch_base = starfive_jh71x0_get_ch_base, > +}; These two are identical. Does it make sense to use only one instance of these? > [...] > +static int ocores_pwm_probe(struct platform_device *pdev) > +{ > + const struct of_device_id *id; > + struct device *dev = &pdev->dev; > + struct ocores_pwm_device *ddata; > + struct pwm_chip *chip; > + struct clk *clk; > + struct reset_control *rst; > + int ret; > + > + id = of_match_device(ocores_pwm_of_match, dev); > + if (!id) > + return -EINVAL; > + > + chip = devm_pwmchip_alloc(&pdev->dev, 8, sizeof(*ddata)); > + if (IS_ERR(chip)) > + return -ENOMEM; > + > + ddata = chip_to_ocores(chip); > + ddata->data = id->data; > + chip->ops = &ocores_pwm_ops; > + > + ddata->regs = devm_platform_ioremap_resource(pdev, 0); > + if (IS_ERR(ddata->regs)) > + return dev_err_probe(dev, PTR_ERR(ddata->regs), > + "Unable to map IO resources\n"); > + > + clk = devm_clk_get_enabled(dev, NULL); > + if (IS_ERR(clk)) > + return dev_err_probe(dev, PTR_ERR(clk), > + "Unable to get pwm's clock\n"); > + > + ret = devm_clk_rate_exclusive_get(dev, clk); > + if (ret) > + return ret; > + > + rst = devm_reset_control_get_optional_exclusive(dev, NULL); > + if (IS_ERR(rst)) > + return dev_err_probe(dev, PTR_ERR(rst), > + "Unable to get pwm's reset\n"); > + > + reset_control_deassert(rst); > + > + ret = devm_add_action_or_reset(dev, ocores_pwm_reset_control_assert, rst); > + if (ret) > + return ret; > + > + ddata->clk_rate = clk_get_rate(clk); > + if (ddata->clk_rate <= 0 || ddata->clk_rate > NSEC_PER_SEC) clk_rate is an u32. So ddata->clk_rate <= 0 will never be true. Also on 64bit archs clk_get_rate() might return 4294967297 which results in ddata->clk_rate being assigned 1 and then passing this test. > + return dev_err_probe(dev, ddata->clk_rate, > + "Unable to get clock's rate\n"); > + > + ret = devm_pwmchip_add(dev, chip); > + if (ret < 0) > + return dev_err_probe(dev, ret, "Could not register PWM chip\n"); > + > + return 0; > +} Best regards Uwe --pz7cmbt7u7ur6jmg Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEP4GsaTp6HlmJrf7Tj4D7WH0S/k4FAmZsHWAACgkQj4D7WH0S /k6lKQf+NrcJlhAWZK2lB9x1qKMuEf//I36x7OJfXngdmSPtftOITcdOcDS7lTsA IHIwvdbt6lLAVKAIfAH1jj5BMEXYj7JnqggU4HCaGVPFA8/60Tiviwf++x1KMmqS wb/Z2gUunjqLkZH74ur/RlNb2wfkr/vE9UAjsMqXSimQvvcwiCpnthOld7/VPwG/ Ud+mvxizzs/rlXNDHR/wDB8tuTVLjzxGjpdhgOI0huSoA5EUT8HhrdMqcmzmwUHc cvcDNIT9BaXRzlEIHvRrBVh8G1OigEOJChdA6Z+ncPf+L7U6sNMATjcReJiCta01 3pfQy2GISFd9JefXZGMjNkVHn/ti/A== =TK0N -----END PGP SIGNATURE----- --pz7cmbt7u7ur6jmg--