[PATCH v3 06/15] can: grcan: Simplify timing configuration

Arun Muthusamy
2026-01-22T12:10:56Z

Remove redundant error checks and use FIELD_PREP for bit timing
assignments to simplify the timing configuration

Signed-off-by: Arun Muthusamy <arun.muthusamy@gaisler.com>
Signed-off-by: Daniel Hellstrom <daniel@gaisler.com>
---
drivers/net/can/grcan.c | 41 +++++++++++++----------------------------
1 file changed, 13 insertions(+), 28 deletions(-)

diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
index f34ca558fa5d..5a63d0a0365f 100644
--- a/drivers/net/can/grcan.c
+++ b/drivers/net/can/grcan.c
@@ -396,41 +396,26 @@ static const struct can_bittiming_const grcan_bittiming_const = {
static int grcan_set_bittiming(struct net_device *dev)
{
struct grcan_priv *priv = netdev_priv(dev);
- struct grcan_registers __iomem *regs = priv->regs;
- struct can_bittiming *bt = &priv->can.bittiming;
- u32 timing = 0;
+ struct grcan_registers __iomem *regs;
int bpr, rsj, ps1, ps2, scaler;
+ struct can_bittiming *bt;
+ u32 timing = 0;

- /* Should never happen - function will not be called when
- * device is up
- */
- if (grcan_read_bits(&regs->ctrl, GRCAN_CTRL_ENABLE))
- return -EBUSY;
+ regs = priv->regs;
+ bt = &priv->can.bittiming;

bpr = 0; /* Note bpr and brp are different concepts */
rsj = bt->sjw;
ps1 = (bt->prop_seg + bt->phase_seg1) - 1; /* tseg1 - 1 */
ps2 = bt->phase_seg2;
- scaler = (bt->brp - 1);
- netdev_dbg(dev, "Request for BPR=%d, RSJ=%d, PS1=%d, PS2=%d, SCALER=%d",
- bpr, rsj, ps1, ps2, scaler);
- if (!(ps1 > ps2)) {
- netdev_err(dev, "PS1 > PS2 must hold: PS1=%d, PS2=%d\n",
- ps1, ps2);
- return -EINVAL;
- }
- if (!(ps2 >= rsj)) {
- netdev_err(dev, "PS2 >= RSJ must hold: PS2=%d, RSJ=%d\n",
- ps2, rsj);
- return -EINVAL;
- }
-
- timing |= (bpr << GRCAN_CONF_BPR_BIT) & GRCAN_CONF_BPR;
- timing |= (rsj << GRCAN_CONF_RSJ_BIT) & GRCAN_CONF_RSJ;
- timing |= (ps1 << GRCAN_CONF_PS1_BIT) & GRCAN_CONF_PS1;
- timing |= (ps2 << GRCAN_CONF_PS2_BIT) & GRCAN_CONF_PS2;
- timing |= (scaler << GRCAN_CONF_SCALER_BIT) & GRCAN_CONF_SCALER;
- netdev_info(dev, "setting timing=0x%x\n", timing);
+ scaler = bt->brp - 1;
+
+ timing |= FIELD_PREP(GRCAN_CONF_BPR, bpr);
+ timing |= FIELD_PREP(GRCAN_CONF_RSJ, rsj);
+ timing |= FIELD_PREP(GRCAN_CONF_PS1, ps1);
+ timing |= FIELD_PREP(GRCAN_CONF_PS2, ps2);
+ timing |= FIELD_PREP(GRCAN_CONF_SCALER, scaler);
+ netdev_dbg(dev, "setting timing=0x%x\n", timing);
grcan_write_bits(&regs->conf, timing, GRCAN_CONF_TIMING);

return 0;
--
2.51.0