blob: ca80dbaf7a3f5415050fd143125a00ee4aa35397 [file] [log] [blame]
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +01001// SPDX-License-Identifier: GPL-2.0
2//
3// tcan4x5x - Texas Instruments TCAN4x5x Family CAN controller driver
4//
5// Copyright (c) 2020 Pengutronix,
6// Marc Kleine-Budde <kernel@pengutronix.de>
7// Copyright (c) 2018-2019 Texas Instruments Incorporated
8// http://www.ti.com/
9
10#include "tcan4x5x.h"
11
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010012#define TCAN4X5X_SPI_INSTRUCTION_WRITE (0x61 << 24)
13#define TCAN4X5X_SPI_INSTRUCTION_READ (0x41 << 24)
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010014
Marc Kleine-Budde6e1caaf2020-12-16 00:17:41 +010015#define TCAN4X5X_MAX_REGISTER 0x8ffc
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010016
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010017static int tcan4x5x_regmap_gather_write(void *context,
18 const void *reg, size_t reg_len,
19 const void *val, size_t val_len)
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010020{
Marc Kleine-Buddebf722fd2020-12-16 00:17:40 +010021 struct spi_device *spi = context;
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010022 struct tcan4x5x_priv *priv = spi_get_drvdata(spi);
23 struct tcan4x5x_map_buf *buf_tx = &priv->map_buf_tx;
24 struct spi_transfer xfer[] = {
25 {
26 .tx_buf = buf_tx,
27 .len = sizeof(buf_tx->cmd) + val_len,
28 },
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010029 };
30
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010031 memcpy(&buf_tx->cmd, reg, sizeof(buf_tx->cmd.cmd) +
32 sizeof(buf_tx->cmd.addr));
33 tcan4x5x_spi_cmd_set_len(&buf_tx->cmd, val_len);
34 memcpy(buf_tx->data, val, val_len);
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010035
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010036 return spi_sync_transfer(spi, xfer, ARRAY_SIZE(xfer));
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010037}
38
39static int tcan4x5x_regmap_write(void *context, const void *data, size_t count)
40{
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010041 return tcan4x5x_regmap_gather_write(context, data, sizeof(__be32),
42 data + sizeof(__be32),
43 count - sizeof(__be32));
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010044}
45
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010046static int tcan4x5x_regmap_read(void *context,
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010047 const void *reg_buf, size_t reg_len,
48 void *val_buf, size_t val_len)
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010049{
Marc Kleine-Buddebf722fd2020-12-16 00:17:40 +010050 struct spi_device *spi = context;
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010051 struct tcan4x5x_priv *priv = spi_get_drvdata(spi);
52 struct tcan4x5x_map_buf *buf_rx = &priv->map_buf_rx;
53 struct tcan4x5x_map_buf *buf_tx = &priv->map_buf_tx;
Marc Kleine-Budde0460eca2020-12-16 00:17:46 +010054 struct spi_transfer xfer[2] = {
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010055 {
56 .tx_buf = buf_tx,
57 }
58 };
59 struct spi_message msg;
60 int err;
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010061
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010062 spi_message_init(&msg);
63 spi_message_add_tail(&xfer[0], &msg);
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010064
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010065 memcpy(&buf_tx->cmd, reg_buf, sizeof(buf_tx->cmd.cmd) +
66 sizeof(buf_tx->cmd.addr));
67 tcan4x5x_spi_cmd_set_len(&buf_tx->cmd, val_len);
68
Marc Kleine-Budde0460eca2020-12-16 00:17:46 +010069 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
70 xfer[0].len = sizeof(buf_tx->cmd);
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010071
Marc Kleine-Budde0460eca2020-12-16 00:17:46 +010072 xfer[1].rx_buf = val_buf;
73 xfer[1].len = val_len;
74 spi_message_add_tail(&xfer[1], &msg);
75 } else {
76 xfer[0].rx_buf = buf_rx;
77 xfer[0].len = sizeof(buf_tx->cmd) + val_len;
78
79 if (TCAN4X5X_SANITIZE_SPI)
80 memset(buf_tx->data, 0x0, val_len);
81 }
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010082
83 err = spi_sync(spi, &msg);
84 if (err)
85 return err;
86
Marc Kleine-Budde0460eca2020-12-16 00:17:46 +010087 if (!(spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX))
88 memcpy(val_buf, buf_rx->data, val_len);
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +010089
90 return 0;
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +010091}
92
Marc Kleine-Budde1e81d522020-12-16 00:17:44 +010093static const struct regmap_range tcan4x5x_reg_table_yes_range[] = {
94 regmap_reg_range(0x0000, 0x002c), /* Device ID and SPI Registers */
95 regmap_reg_range(0x0800, 0x083c), /* Device configuration registers and Interrupt Flags*/
96 regmap_reg_range(0x1000, 0x10fc), /* M_CAN */
97 regmap_reg_range(0x8000, 0x87fc), /* MRAM */
98};
99
100static const struct regmap_access_table tcan4x5x_reg_table = {
101 .yes_ranges = tcan4x5x_reg_table_yes_range,
102 .n_yes_ranges = ARRAY_SIZE(tcan4x5x_reg_table_yes_range),
103};
104
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100105static const struct regmap_config tcan4x5x_regmap = {
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +0100106 .reg_bits = 24,
Marc Kleine-Buddeaaf120c2020-12-16 00:17:42 +0100107 .reg_stride = 4,
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +0100108 .pad_bits = 8,
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100109 .val_bits = 32,
Marc Kleine-Budde1e81d522020-12-16 00:17:44 +0100110 .wr_table = &tcan4x5x_reg_table,
111 .rd_table = &tcan4x5x_reg_table,
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100112 .max_register = TCAN4X5X_MAX_REGISTER,
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +0100113 .cache_type = REGCACHE_NONE,
114 .read_flag_mask = (__force unsigned long)
115 cpu_to_be32(TCAN4X5X_SPI_INSTRUCTION_READ),
116 .write_flag_mask = (__force unsigned long)
117 cpu_to_be32(TCAN4X5X_SPI_INSTRUCTION_WRITE),
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100118};
119
Marc Kleine-Budde1784aa12020-12-16 00:17:35 +0100120static const struct regmap_bus tcan4x5x_bus = {
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100121 .write = tcan4x5x_regmap_write,
Marc Kleine-Budde52be9772020-12-16 00:17:38 +0100122 .gather_write = tcan4x5x_regmap_gather_write,
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100123 .read = tcan4x5x_regmap_read,
Marc Kleine-Budde1c5d0fc2020-12-16 00:17:45 +0100124 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
125 .val_format_endian_default = REGMAP_ENDIAN_BIG,
Marc Kleine-Budde55841142020-12-16 00:17:43 +0100126 .max_raw_read = 256,
127 .max_raw_write = 256,
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100128};
129
130int tcan4x5x_regmap_init(struct tcan4x5x_priv *priv)
131{
132 priv->regmap = devm_regmap_init(&priv->spi->dev, &tcan4x5x_bus,
Marc Kleine-Buddebf722fd2020-12-16 00:17:40 +0100133 priv->spi, &tcan4x5x_regmap);
Marc Kleine-Budde67def4ef2020-12-16 00:17:34 +0100134 return PTR_ERR_OR_ZERO(priv->regmap);
135}