162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// tcan4x5x - Texas Instruments TCAN4x5x Family CAN controller driver
462306a36Sopenharmony_ci//
562306a36Sopenharmony_ci// Copyright (c) 2020 Pengutronix,
662306a36Sopenharmony_ci//                    Marc Kleine-Budde <kernel@pengutronix.de>
762306a36Sopenharmony_ci// Copyright (c) 2018-2019 Texas Instruments Incorporated
862306a36Sopenharmony_ci//                    http://www.ti.com/
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include "tcan4x5x.h"
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#define TCAN4X5X_SPI_INSTRUCTION_WRITE (0x61 << 24)
1362306a36Sopenharmony_ci#define TCAN4X5X_SPI_INSTRUCTION_READ (0x41 << 24)
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define TCAN4X5X_MAX_REGISTER 0x87fc
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic int tcan4x5x_regmap_gather_write(void *context,
1862306a36Sopenharmony_ci					const void *reg, size_t reg_len,
1962306a36Sopenharmony_ci					const void *val, size_t val_len)
2062306a36Sopenharmony_ci{
2162306a36Sopenharmony_ci	struct spi_device *spi = context;
2262306a36Sopenharmony_ci	struct tcan4x5x_priv *priv = spi_get_drvdata(spi);
2362306a36Sopenharmony_ci	struct tcan4x5x_map_buf *buf_tx = &priv->map_buf_tx;
2462306a36Sopenharmony_ci	struct spi_transfer xfer[] = {
2562306a36Sopenharmony_ci		{
2662306a36Sopenharmony_ci			.tx_buf = buf_tx,
2762306a36Sopenharmony_ci			.len = sizeof(buf_tx->cmd) + val_len,
2862306a36Sopenharmony_ci		},
2962306a36Sopenharmony_ci	};
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	memcpy(&buf_tx->cmd, reg, sizeof(buf_tx->cmd.cmd) +
3262306a36Sopenharmony_ci	       sizeof(buf_tx->cmd.addr));
3362306a36Sopenharmony_ci	tcan4x5x_spi_cmd_set_len(&buf_tx->cmd, val_len);
3462306a36Sopenharmony_ci	memcpy(buf_tx->data, val, val_len);
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	return spi_sync_transfer(spi, xfer, ARRAY_SIZE(xfer));
3762306a36Sopenharmony_ci}
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistatic int tcan4x5x_regmap_write(void *context, const void *data, size_t count)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	return tcan4x5x_regmap_gather_write(context, data, sizeof(__be32),
4262306a36Sopenharmony_ci					    data + sizeof(__be32),
4362306a36Sopenharmony_ci					    count - sizeof(__be32));
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic int tcan4x5x_regmap_read(void *context,
4762306a36Sopenharmony_ci				const void *reg_buf, size_t reg_len,
4862306a36Sopenharmony_ci				void *val_buf, size_t val_len)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	struct spi_device *spi = context;
5162306a36Sopenharmony_ci	struct tcan4x5x_priv *priv = spi_get_drvdata(spi);
5262306a36Sopenharmony_ci	struct tcan4x5x_map_buf *buf_rx = &priv->map_buf_rx;
5362306a36Sopenharmony_ci	struct tcan4x5x_map_buf *buf_tx = &priv->map_buf_tx;
5462306a36Sopenharmony_ci	struct spi_transfer xfer[2] = {
5562306a36Sopenharmony_ci		{
5662306a36Sopenharmony_ci			.tx_buf = buf_tx,
5762306a36Sopenharmony_ci		}
5862306a36Sopenharmony_ci	};
5962306a36Sopenharmony_ci	struct spi_message msg;
6062306a36Sopenharmony_ci	int err;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	spi_message_init(&msg);
6362306a36Sopenharmony_ci	spi_message_add_tail(&xfer[0], &msg);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	memcpy(&buf_tx->cmd, reg_buf, sizeof(buf_tx->cmd.cmd) +
6662306a36Sopenharmony_ci	       sizeof(buf_tx->cmd.addr));
6762306a36Sopenharmony_ci	tcan4x5x_spi_cmd_set_len(&buf_tx->cmd, val_len);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
7062306a36Sopenharmony_ci		xfer[0].len = sizeof(buf_tx->cmd);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci		xfer[1].rx_buf = val_buf;
7362306a36Sopenharmony_ci		xfer[1].len = val_len;
7462306a36Sopenharmony_ci		spi_message_add_tail(&xfer[1], &msg);
7562306a36Sopenharmony_ci	} else {
7662306a36Sopenharmony_ci		xfer[0].rx_buf = buf_rx;
7762306a36Sopenharmony_ci		xfer[0].len = sizeof(buf_tx->cmd) + val_len;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci		if (TCAN4X5X_SANITIZE_SPI)
8062306a36Sopenharmony_ci			memset(buf_tx->data, 0x0, val_len);
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	err = spi_sync(spi, &msg);
8462306a36Sopenharmony_ci	if (err)
8562306a36Sopenharmony_ci		return err;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	if (!(spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX))
8862306a36Sopenharmony_ci		memcpy(val_buf, buf_rx->data, val_len);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	return 0;
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic const struct regmap_range tcan4x5x_reg_table_wr_range[] = {
9462306a36Sopenharmony_ci	/* Device ID and SPI Registers */
9562306a36Sopenharmony_ci	regmap_reg_range(0x000c, 0x0010),
9662306a36Sopenharmony_ci	/* Device configuration registers and Interrupt Flags*/
9762306a36Sopenharmony_ci	regmap_reg_range(0x0800, 0x080c),
9862306a36Sopenharmony_ci	regmap_reg_range(0x0820, 0x0820),
9962306a36Sopenharmony_ci	regmap_reg_range(0x0830, 0x0830),
10062306a36Sopenharmony_ci	/* M_CAN */
10162306a36Sopenharmony_ci	regmap_reg_range(0x100c, 0x102c),
10262306a36Sopenharmony_ci	regmap_reg_range(0x1048, 0x1048),
10362306a36Sopenharmony_ci	regmap_reg_range(0x1050, 0x105c),
10462306a36Sopenharmony_ci	regmap_reg_range(0x1080, 0x1088),
10562306a36Sopenharmony_ci	regmap_reg_range(0x1090, 0x1090),
10662306a36Sopenharmony_ci	regmap_reg_range(0x1098, 0x10a0),
10762306a36Sopenharmony_ci	regmap_reg_range(0x10a8, 0x10b0),
10862306a36Sopenharmony_ci	regmap_reg_range(0x10b8, 0x10c0),
10962306a36Sopenharmony_ci	regmap_reg_range(0x10c8, 0x10c8),
11062306a36Sopenharmony_ci	regmap_reg_range(0x10d0, 0x10d4),
11162306a36Sopenharmony_ci	regmap_reg_range(0x10e0, 0x10e4),
11262306a36Sopenharmony_ci	regmap_reg_range(0x10f0, 0x10f0),
11362306a36Sopenharmony_ci	regmap_reg_range(0x10f8, 0x10f8),
11462306a36Sopenharmony_ci	/* MRAM */
11562306a36Sopenharmony_ci	regmap_reg_range(0x8000, 0x87fc),
11662306a36Sopenharmony_ci};
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic const struct regmap_range tcan4x5x_reg_table_rd_range[] = {
11962306a36Sopenharmony_ci	regmap_reg_range(0x0000, 0x0010),	/* Device ID and SPI Registers */
12062306a36Sopenharmony_ci	regmap_reg_range(0x0800, 0x0830),	/* Device configuration registers and Interrupt Flags*/
12162306a36Sopenharmony_ci	regmap_reg_range(0x1000, 0x10fc),	/* M_CAN */
12262306a36Sopenharmony_ci	regmap_reg_range(0x8000, 0x87fc),	/* MRAM */
12362306a36Sopenharmony_ci};
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic const struct regmap_access_table tcan4x5x_reg_table_wr = {
12662306a36Sopenharmony_ci	.yes_ranges = tcan4x5x_reg_table_wr_range,
12762306a36Sopenharmony_ci	.n_yes_ranges = ARRAY_SIZE(tcan4x5x_reg_table_wr_range),
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic const struct regmap_access_table tcan4x5x_reg_table_rd = {
13162306a36Sopenharmony_ci	.yes_ranges = tcan4x5x_reg_table_rd_range,
13262306a36Sopenharmony_ci	.n_yes_ranges = ARRAY_SIZE(tcan4x5x_reg_table_rd_range),
13362306a36Sopenharmony_ci};
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic const struct regmap_config tcan4x5x_regmap = {
13662306a36Sopenharmony_ci	.reg_bits = 24,
13762306a36Sopenharmony_ci	.reg_stride = 4,
13862306a36Sopenharmony_ci	.pad_bits = 8,
13962306a36Sopenharmony_ci	.val_bits = 32,
14062306a36Sopenharmony_ci	.wr_table = &tcan4x5x_reg_table_wr,
14162306a36Sopenharmony_ci	.rd_table = &tcan4x5x_reg_table_rd,
14262306a36Sopenharmony_ci	.max_register = TCAN4X5X_MAX_REGISTER,
14362306a36Sopenharmony_ci	.cache_type = REGCACHE_NONE,
14462306a36Sopenharmony_ci	.read_flag_mask = (__force unsigned long)
14562306a36Sopenharmony_ci		cpu_to_be32(TCAN4X5X_SPI_INSTRUCTION_READ),
14662306a36Sopenharmony_ci	.write_flag_mask = (__force unsigned long)
14762306a36Sopenharmony_ci		cpu_to_be32(TCAN4X5X_SPI_INSTRUCTION_WRITE),
14862306a36Sopenharmony_ci};
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_cistatic const struct regmap_bus tcan4x5x_bus = {
15162306a36Sopenharmony_ci	.write = tcan4x5x_regmap_write,
15262306a36Sopenharmony_ci	.gather_write = tcan4x5x_regmap_gather_write,
15362306a36Sopenharmony_ci	.read = tcan4x5x_regmap_read,
15462306a36Sopenharmony_ci	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
15562306a36Sopenharmony_ci	.val_format_endian_default = REGMAP_ENDIAN_BIG,
15662306a36Sopenharmony_ci	.max_raw_read = 256,
15762306a36Sopenharmony_ci	.max_raw_write = 256,
15862306a36Sopenharmony_ci};
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciint tcan4x5x_regmap_init(struct tcan4x5x_priv *priv)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	priv->regmap = devm_regmap_init(&priv->spi->dev, &tcan4x5x_bus,
16362306a36Sopenharmony_ci					priv->spi, &tcan4x5x_regmap);
16462306a36Sopenharmony_ci	return PTR_ERR_OR_ZERO(priv->regmap);
16562306a36Sopenharmony_ci}
166