summaryrefslogtreecommitdiffstats
path: root/drivers/net/can/spi/mcp251xfd/mcp251xfd-timestamp.c
blob: 202ca0d24d03b9f5bebba23a9f8d0e6f6422c728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: GPL-2.0
//
// mcp251xfd - Microchip MCP251xFD Family CAN controller driver
//
// Copyright (c) 2021, 2023 Pengutronix,
//               Marc Kleine-Budde <kernel@pengutronix.de>
//

#include <linux/clocksource.h>
#include <linux/workqueue.h>

#include "mcp251xfd.h"

static u64 mcp251xfd_timestamp_raw_read(const struct cyclecounter *cc)
{
	const struct mcp251xfd_priv *priv;
	u32 ts_raw = 0;
	int err;

	priv = container_of(cc, struct mcp251xfd_priv, cc);
	err = mcp251xfd_get_timestamp_raw(priv, &ts_raw);
	if (err)
		netdev_err(priv->ndev,
			   "Error %d while reading timestamp. HW timestamps may be inaccurate.",
			   err);

	return ts_raw;
}

static void mcp251xfd_timestamp_work(struct work_struct *work)
{
	struct delayed_work *delayed_work = to_delayed_work(work);
	struct mcp251xfd_priv *priv;

	priv = container_of(delayed_work, struct mcp251xfd_priv, timestamp);
	timecounter_read(&priv->tc);

	schedule_delayed_work(&priv->timestamp,
			      MCP251XFD_TIMESTAMP_WORK_DELAY_SEC * HZ);
}

void mcp251xfd_timestamp_init(struct mcp251xfd_priv *priv)
{
	struct cyclecounter *cc = &priv->cc;

	cc->read = mcp251xfd_timestamp_raw_read;
	cc->mask = CYCLECOUNTER_MASK(32);
	cc->shift = 1;
	cc->mult = clocksource_hz2mult(priv->can.clock.freq, cc->shift);

	INIT_DELAYED_WORK(&priv->timestamp, mcp251xfd_timestamp_work);
}

void mcp251xfd_timestamp_start(struct mcp251xfd_priv *priv)
{
	timecounter_init(&priv->tc, &priv->cc, ktime_get_real_ns());
	schedule_delayed_work(&priv->timestamp,
			      MCP251XFD_TIMESTAMP_WORK_DELAY_SEC * HZ);
}

void mcp251xfd_timestamp_stop(struct mcp251xfd_priv *priv)
{
	cancel_delayed_work_sync(&priv->timestamp);
}