From nobody Fri Oct 3 01:10:08 2025 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 BFC1927F4D5; Tue, 9 Sep 2025 09:12:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757409153; cv=none; b=E3b1yYSqBhOAbftQsKxIkhIWqBPZGpC2IfMo8GCMISSDvusKJV5B4vMmlojmPMqKVhsaYrHaOGdHd0um233tnWjjpsVbX3LrXUBA0GyoRrmV71ek2NZHGVSjVrCJy+yrYhNRFoXA1ZEdftt1kcw1ez+NxwINMpY2Ulrd5igSXhI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757409153; c=relaxed/simple; bh=LYvSNoj8sG+ycYRrJpqTXvz629EslR4dZKJMoeKQQWI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WhpQ5Ezhqsp30jtZFJpT2Xh36fbpPEGhWJNapTzWZcHgo/Qi7UELp6Z/EbOyjPXXXv/iToAzzieNBepgJUhJvIC832Bi3xq6GkLOXnshHqlL/iO7LPPB5yFbrGTAP4BIeDt8TZmk5r+EgsxhiDrg/k10Dh2g6uDzap0EToEUCXc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KkS8vNUZ; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KkS8vNUZ" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-45dd505a1dfso31340425e9.2; Tue, 09 Sep 2025 02:12:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757409149; x=1758013949; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=j617YhmA2SMXaLYPpqLS280wIJosRqcK9ePzWm8QYGM=; b=KkS8vNUZDwG5tWmGxQLMFqlR4tsMkcyvz0hVq9gs1teWuACj/1XsBvQMK3FpcjzBOm HjdSAumiF1joqrOW+5x9UkSD6wtqE7nb1h+Err0Rq2WXT6CGsMaBKLTji+V/j/uH0BnE WNrJ8RcSdKDwIt7hNvks9wy2op54CMsW3F3E6muDQgaYTVQ91GRk2FHkEyOVbU7NNHar qRcidsY3MbAhgkKwHDeD1T7Yrk64kFnKyqe8ZZNYRfodY91sTlTKAJn968RUbsM7Gyqv YBIFA5b/DcL3Y7jP5PvyCMYYY4uUDc8ufljelL20cTkYdg42FHVAxMQbBt4FiuDbmx10 b8mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757409149; x=1758013949; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j617YhmA2SMXaLYPpqLS280wIJosRqcK9ePzWm8QYGM=; b=FokVqS+uRgd3fNm6gXoFMjxqD9jeSiKInbtrYsbEKWNJo/NUXwX0A0iFfGD0ezScJK k6pCHRs37nBXT/T7B8hDdfU4vS2LTXwKq+6lar7TqzfQUentGQizTR5WRZXqsEB6avg8 ZVVimrbyrGXhQVqbaV8GfEz57WTdm7y0QFzNGyLSyCOpds/MbaZOzNU8PdiVI/B1TTSD B0L7Psj9zQ4a3Sk7Dwvq1EWLEHBu559WB/mX14HGZ15/SXNccEBGJzUGtox5Hb2YVl7X Kr/XtmjbtGVGpMuOboc5R0lQ+uJPw+osNyB1okHRK5H2unhUjOFWBGMCp1AG28P7r0Qo GbgA== X-Forwarded-Encrypted: i=1; AJvYcCUdMuitj0otnQ/KBoEuLQFe4ExXZXMa/SWYW7LL+8QHwUOjeEkGVTuiGsCL1SafLjjEmgUwytsOInGliAjz9mo+UF4=@vger.kernel.org, AJvYcCW5XxY7ZIyIv1LGpIJaRlJ7D19HdzIrdElUqnmz8CArkM1KvEARh8Y90SJqRB7wyycaikbBWm3CKd1FUE4=@vger.kernel.org, AJvYcCXpXwnWF20AwY9x32O7EnbAeIOvrGm748wsRfqSlfXMsbJlfENDjefwZz08Wuhr3Wa2VK8K4UcciKcT@vger.kernel.org X-Gm-Message-State: AOJu0Yw1ppXJQEbD6sUgDr9aR5vu2nZ3eU1mwzTGgRY6zz8n9pS81f63 UfvBrSraoDZwo7+RCcRzy2H3d2nSvLa9pto1G+53ledbpJzto5YOly+EZ/9C3atQ X-Gm-Gg: ASbGncudlFiUaDIUNBGn/TpAwp1ss3rp+3p4gqCuM5h/0eACKs4aBs2PMgqwcQQjD1G caEpEHj1Uk99MRrHbfTCj61Hzy48Ob+DDBqQCa1CviZhB1JaQYgFOxPT1tp+Q8HnjjKC3TeuKpK SQzGlWo576bFqi7LBdp4DwHrS2MD/7W2oNL5no/v1Sb0I5MEP6zs01zDzvelz6IX4QARKnbO9xp FtdVcvtXrxtUcdtHqpHPVuuLidKADtHUJGyY2uSjZzzN23LiXIC2aPxG9xk/LrVRf737KSGokpr yI6A804Ke6JNK2034k/TEfTLmYZcLSI+12hX5RhthghN+2TalE7wK7gFYzzOzmUSTEwu/et49BI 4HVnXjya30ePJnj1sTXBNcRIPiwVCJtXC6PWMvRW/hQiVbntOKwp4CYWpZaLASU+qwZ73UT7eGF 763YnNU9E6AYVb X-Google-Smtp-Source: AGHT+IFWbh3msT+OwFbIgdgIgDlgAaQ1hadjrPQt5/ABhCzbvWId/uys0sDjsGW1f2qZKBKrSzaJPg== X-Received: by 2002:a05:600c:3b1d:b0:45d:dc85:c009 with SMTP id 5b1f17b1804b1-45ddde92816mr103466065e9.10.1757409148615; Tue, 09 Sep 2025 02:12:28 -0700 (PDT) Received: from biju.lan (host86-139-30-37.range86-139.btcentralplus.com. [86.139.30.37]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45deff68b43sm6975685e9.2.2025.09.09.02.12.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Sep 2025 02:12:28 -0700 (PDT) From: Biju X-Google-Original-From: Biju To: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= Cc: Biju Das , linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven , Prabhakar Mahadev Lad , Biju Das , linux-renesas-soc@vger.kernel.org Subject: [PATCH v2 2/8] pwm: rzg2l-gpt: Implementation of the waveform callbacks Date: Tue, 9 Sep 2025 10:12:12 +0100 Message-ID: <20250909091225.128658-3-biju.das.jz@bp.renesas.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250909091225.128658-1-biju.das.jz@bp.renesas.com> References: <20250909091225.128658-1-biju.das.jz@bp.renesas.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Biju Das Convert the rzg2l-gpt driver to use the new callbacks for hardware programming. Signed-off-by: Biju Das --- v1->v2: * Dropped modifing hardware from .round_waveform_tohw() callback. --- drivers/pwm/pwm-rzg2l-gpt.c | 175 +++++++++++++++++++++--------------- 1 file changed, 102 insertions(+), 73 deletions(-) diff --git a/drivers/pwm/pwm-rzg2l-gpt.c b/drivers/pwm/pwm-rzg2l-gpt.c index 360c8bf3b190..f0a8531457ca 100644 --- a/drivers/pwm/pwm-rzg2l-gpt.c +++ b/drivers/pwm/pwm-rzg2l-gpt.c @@ -86,6 +86,13 @@ struct rzg2l_gpt_chip { u32 channel_enable_count[RZG2L_MAX_HW_CHANNELS]; }; =20 +/* This represents a hardware configuration for one channel */ +struct rzg2l_gpt_waveform { + u32 gtpr; + u32 gtccr; + u8 prescale; +}; + static inline struct rzg2l_gpt_chip *to_rzg2l_gpt_chip(struct pwm_chip *ch= ip) { return pwmchip_get_drvdata(chip); @@ -190,8 +197,10 @@ static void rzg2l_gpt_disable(struct rzg2l_gpt_chip *r= zg2l_gpt, /* Stop count, Output low on GTIOCx pin when counting stops */ rzg2l_gpt->channel_enable_count[ch]--; =20 - if (!rzg2l_gpt->channel_enable_count[ch]) + if (!rzg2l_gpt->channel_enable_count[ch]) { rzg2l_gpt_modify(rzg2l_gpt, RZG2L_GTCR(ch), RZG2L_GTCR_CST, 0); + rzg2l_gpt->period_ticks[ch] =3D 0; + } =20 /* Disable pin output */ rzg2l_gpt_modify(rzg2l_gpt, RZG2L_GTIOR(ch), RZG2L_GTIOR_OxE(sub_ch), 0); @@ -215,54 +224,37 @@ static u64 rzg2l_gpt_calculate_period_or_duty(struct = rzg2l_gpt_chip *rzg2l_gpt, return DIV64_U64_ROUND_UP(tmp, rzg2l_gpt->rate_khz); } =20 -static int rzg2l_gpt_get_state(struct pwm_chip *chip, struct pwm_device *p= wm, - struct pwm_state *state) -{ - struct rzg2l_gpt_chip *rzg2l_gpt =3D to_rzg2l_gpt_chip(chip); - - state->enabled =3D rzg2l_gpt_is_ch_enabled(rzg2l_gpt, pwm->hwpwm); - if (state->enabled) { - u32 sub_ch =3D rzg2l_gpt_subchannel(pwm->hwpwm); - u32 ch =3D RZG2L_GET_CH(pwm->hwpwm); - u8 prescale; - u32 val; - - val =3D rzg2l_gpt_read(rzg2l_gpt, RZG2L_GTCR(ch)); - prescale =3D FIELD_GET(RZG2L_GTCR_TPCS, val); - - val =3D rzg2l_gpt_read(rzg2l_gpt, RZG2L_GTPR(ch)); - state->period =3D rzg2l_gpt_calculate_period_or_duty(rzg2l_gpt, val, pre= scale); - - val =3D rzg2l_gpt_read(rzg2l_gpt, RZG2L_GTCCR(ch, sub_ch)); - state->duty_cycle =3D rzg2l_gpt_calculate_period_or_duty(rzg2l_gpt, val,= prescale); - if (state->duty_cycle > state->period) - state->duty_cycle =3D state->period; - } - - state->polarity =3D PWM_POLARITY_NORMAL; - - return 0; -} - static u32 rzg2l_gpt_calculate_pv_or_dc(u64 period_or_duty_cycle, u8 presc= ale) { return min_t(u64, DIV_ROUND_DOWN_ULL(period_or_duty_cycle, 1 << (2 * pres= cale)), U32_MAX); } =20 -/* Caller holds the lock while calling rzg2l_gpt_config() */ -static int rzg2l_gpt_config(struct pwm_chip *chip, struct pwm_device *pwm, - const struct pwm_state *state) +static int rzg2l_gpt_round_waveform_tohw(struct pwm_chip *chip, + struct pwm_device *pwm, + const struct pwm_waveform *wf, + void *_wfhw) + { struct rzg2l_gpt_chip *rzg2l_gpt =3D to_rzg2l_gpt_chip(chip); - u8 sub_ch =3D rzg2l_gpt_subchannel(pwm->hwpwm); + struct rzg2l_gpt_waveform *wfhw =3D _wfhw; u8 ch =3D RZG2L_GET_CH(pwm->hwpwm); u64 period_ticks, duty_ticks; unsigned long pv, dc; - u8 prescale; + + guard(mutex)(&rzg2l_gpt->lock); + if (wf->period_length_ns =3D=3D 0) { + *wfhw =3D (struct rzg2l_gpt_waveform){ + .gtpr =3D 0, + .gtccr =3D 0, + .prescale =3D 0, + }; + + return 0; + } =20 /* Limit period/duty cycle to max value supported by the HW */ - period_ticks =3D mul_u64_u64_div_u64(state->period, rzg2l_gpt->rate_khz, = USEC_PER_SEC); + period_ticks =3D mul_u64_u64_div_u64(wf->period_length_ns, rzg2l_gpt->rat= e_khz, USEC_PER_SEC); if (period_ticks > RZG2L_MAX_TICKS) period_ticks =3D RZG2L_MAX_TICKS; /* @@ -277,13 +269,14 @@ static int rzg2l_gpt_config(struct pwm_chip *chip, st= ruct pwm_device *pwm, period_ticks =3D rzg2l_gpt->period_ticks[ch]; } =20 - prescale =3D rzg2l_gpt_calculate_prescale(rzg2l_gpt, period_ticks); - pv =3D rzg2l_gpt_calculate_pv_or_dc(period_ticks, prescale); - - duty_ticks =3D mul_u64_u64_div_u64(state->duty_cycle, rzg2l_gpt->rate_khz= , USEC_PER_SEC); + wfhw->prescale =3D rzg2l_gpt_calculate_prescale(rzg2l_gpt, period_ticks); + pv =3D rzg2l_gpt_calculate_pv_or_dc(period_ticks, wfhw->prescale); + wfhw->gtpr =3D pv; + duty_ticks =3D mul_u64_u64_div_u64(wf->duty_length_ns, rzg2l_gpt->rate_kh= z, USEC_PER_SEC); if (duty_ticks > period_ticks) duty_ticks =3D period_ticks; - dc =3D rzg2l_gpt_calculate_pv_or_dc(duty_ticks, prescale); + dc =3D rzg2l_gpt_calculate_pv_or_dc(duty_ticks, wfhw->prescale); + wfhw->gtccr =3D dc; =20 /* * GPT counter is shared by multiple channels, we cache the period ticks @@ -292,6 +285,58 @@ static int rzg2l_gpt_config(struct pwm_chip *chip, str= uct pwm_device *pwm, */ rzg2l_gpt->period_ticks[ch] =3D period_ticks; =20 + return 0; +} + +static int rzg2l_gpt_round_waveform_fromhw(struct pwm_chip *chip, + struct pwm_device *pwm, + const void *_wfhw, + struct pwm_waveform *wf) +{ + struct rzg2l_gpt_chip *rzg2l_gpt =3D to_rzg2l_gpt_chip(chip); + const struct rzg2l_gpt_waveform *wfhw =3D _wfhw; + + wf->period_length_ns =3D rzg2l_gpt_calculate_period_or_duty(rzg2l_gpt, wf= hw->gtpr, + wfhw->prescale); + wf->duty_length_ns =3D rzg2l_gpt_calculate_period_or_duty(rzg2l_gpt, wfhw= ->gtccr, + wfhw->prescale); + wf->duty_offset_ns =3D 0; + + return 0; +} + +static int rzg2l_gpt_read_waveform(struct pwm_chip *chip, + struct pwm_device *pwm, + void *_wfhw) +{ + struct rzg2l_gpt_chip *rzg2l_gpt =3D to_rzg2l_gpt_chip(chip); + struct rzg2l_gpt_waveform *wfhw =3D _wfhw; + u32 sub_ch =3D rzg2l_gpt_subchannel(pwm->hwpwm); + u32 ch =3D RZG2L_GET_CH(pwm->hwpwm); + u32 gtcr; + + if (rzg2l_gpt_is_ch_enabled(rzg2l_gpt, pwm->hwpwm)) { + gtcr =3D rzg2l_gpt_read(rzg2l_gpt, RZG2L_GTCR(ch)); + wfhw->prescale =3D FIELD_GET(RZG2L_GTCR_TPCS, gtcr); + wfhw->gtpr =3D rzg2l_gpt_read(rzg2l_gpt, RZG2L_GTPR(ch)); + wfhw->gtccr =3D rzg2l_gpt_read(rzg2l_gpt, RZG2L_GTCCR(ch, sub_ch)); + if (wfhw->gtccr > wfhw->gtpr) + wfhw->gtccr =3D wfhw->gtpr; + } + + return 0; +} + +static int rzg2l_gpt_write_waveform(struct pwm_chip *chip, + struct pwm_device *pwm, + const void *_wfhw) +{ + struct rzg2l_gpt_chip *rzg2l_gpt =3D to_rzg2l_gpt_chip(chip); + const struct rzg2l_gpt_waveform *wfhw =3D _wfhw; + u8 sub_ch =3D rzg2l_gpt_subchannel(pwm->hwpwm); + u8 ch =3D RZG2L_GET_CH(pwm->hwpwm); + + guard(mutex)(&rzg2l_gpt->lock); /* * Counter must be stopped before modifying mode, prescaler, timer * counter and buffer enable registers. These registers are shared @@ -310,14 +355,14 @@ static int rzg2l_gpt_config(struct pwm_chip *chip, st= ruct pwm_device *pwm, =20 /* Select count clock */ rzg2l_gpt_modify(rzg2l_gpt, RZG2L_GTCR(ch), RZG2L_GTCR_TPCS, - FIELD_PREP(RZG2L_GTCR_TPCS, prescale)); + FIELD_PREP(RZG2L_GTCR_TPCS, wfhw->prescale)); =20 /* Set period */ - rzg2l_gpt_write(rzg2l_gpt, RZG2L_GTPR(ch), pv); + rzg2l_gpt_write(rzg2l_gpt, RZG2L_GTPR(ch), wfhw->gtpr); } =20 /* Set duty cycle */ - rzg2l_gpt_write(rzg2l_gpt, RZG2L_GTCCR(ch, sub_ch), dc); + rzg2l_gpt_write(rzg2l_gpt, RZG2L_GTCCR(ch, sub_ch), wfhw->gtccr); =20 if (rzg2l_gpt->channel_enable_count[ch] <=3D 1) { /* Set initial value for counter */ @@ -326,44 +371,28 @@ static int rzg2l_gpt_config(struct pwm_chip *chip, st= ruct pwm_device *pwm, /* Set no buffer operation */ rzg2l_gpt_write(rzg2l_gpt, RZG2L_GTBER(ch), 0); =20 - /* Restart the counter after updating the registers */ - rzg2l_gpt_modify(rzg2l_gpt, RZG2L_GTCR(ch), - RZG2L_GTCR_CST, RZG2L_GTCR_CST); - } - - return 0; -} - -static int rzg2l_gpt_apply(struct pwm_chip *chip, struct pwm_device *pwm, - const struct pwm_state *state) -{ - struct rzg2l_gpt_chip *rzg2l_gpt =3D to_rzg2l_gpt_chip(chip); - bool enabled =3D pwm->state.enabled; - int ret; - - if (state->polarity !=3D PWM_POLARITY_NORMAL) - return -EINVAL; - - guard(mutex)(&rzg2l_gpt->lock); - if (!state->enabled) { - if (enabled) - rzg2l_gpt_disable(rzg2l_gpt, pwm); - - return 0; + if (wfhw->gtpr) + /* Restart the counter after updating the registers */ + rzg2l_gpt_modify(rzg2l_gpt, RZG2L_GTCR(ch), + RZG2L_GTCR_CST, RZG2L_GTCR_CST); } =20 - ret =3D rzg2l_gpt_config(chip, pwm, state); - if (!ret && !enabled) + if (wfhw->gtpr && !rzg2l_gpt_is_ch_enabled(rzg2l_gpt, pwm->hwpwm)) rzg2l_gpt_enable(rzg2l_gpt, pwm); + else if (!wfhw->gtpr && rzg2l_gpt_is_ch_enabled(rzg2l_gpt, pwm->hwpwm)) + rzg2l_gpt_disable(rzg2l_gpt, pwm); =20 - return ret; + return 0; } =20 static const struct pwm_ops rzg2l_gpt_ops =3D { .request =3D rzg2l_gpt_request, .free =3D rzg2l_gpt_free, - .get_state =3D rzg2l_gpt_get_state, - .apply =3D rzg2l_gpt_apply, + .sizeof_wfhw =3D sizeof(struct rzg2l_gpt_waveform), + .round_waveform_tohw =3D rzg2l_gpt_round_waveform_tohw, + .round_waveform_fromhw =3D rzg2l_gpt_round_waveform_fromhw, + .read_waveform =3D rzg2l_gpt_read_waveform, + .write_waveform =3D rzg2l_gpt_write_waveform, }; =20 static int rzg2l_gpt_probe(struct platform_device *pdev) --=20 2.43.0