Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 1 | // 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-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 12 | #define TCAN4X5X_SPI_INSTRUCTION_WRITE (0x61 << 24) |
| 13 | #define TCAN4X5X_SPI_INSTRUCTION_READ (0x41 << 24) |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 14 | |
Marc Kleine-Budde | 6e1caaf | 2020-12-16 00:17:41 +0100 | [diff] [blame] | 15 | #define TCAN4X5X_MAX_REGISTER 0x8ffc |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 16 | |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 17 | static 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-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 20 | { |
Marc Kleine-Budde | bf722fd | 2020-12-16 00:17:40 +0100 | [diff] [blame] | 21 | struct spi_device *spi = context; |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 22 | 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-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 31 | 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-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 35 | |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 36 | return spi_sync_transfer(spi, xfer, ARRAY_SIZE(xfer)); |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int tcan4x5x_regmap_write(void *context, const void *data, size_t count) |
| 40 | { |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 41 | return tcan4x5x_regmap_gather_write(context, data, sizeof(__be32), |
| 42 | data + sizeof(__be32), |
| 43 | count - sizeof(__be32)); |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 46 | static int tcan4x5x_regmap_read(void *context, |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 47 | const void *reg_buf, size_t reg_len, |
| 48 | void *val_buf, size_t val_len) |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 49 | { |
Marc Kleine-Budde | bf722fd | 2020-12-16 00:17:40 +0100 | [diff] [blame] | 50 | struct spi_device *spi = context; |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 51 | 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-Budde | 0460eca | 2020-12-16 00:17:46 +0100 | [diff] [blame] | 54 | struct spi_transfer xfer[2] = { |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 55 | { |
| 56 | .tx_buf = buf_tx, |
| 57 | } |
| 58 | }; |
| 59 | struct spi_message msg; |
| 60 | int err; |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 61 | |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 62 | spi_message_init(&msg); |
| 63 | spi_message_add_tail(&xfer[0], &msg); |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 64 | |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 65 | 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-Budde | 0460eca | 2020-12-16 00:17:46 +0100 | [diff] [blame] | 69 | if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { |
| 70 | xfer[0].len = sizeof(buf_tx->cmd); |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 71 | |
Marc Kleine-Budde | 0460eca | 2020-12-16 00:17:46 +0100 | [diff] [blame] | 72 | 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-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 82 | |
| 83 | err = spi_sync(spi, &msg); |
| 84 | if (err) |
| 85 | return err; |
| 86 | |
Marc Kleine-Budde | 0460eca | 2020-12-16 00:17:46 +0100 | [diff] [blame] | 87 | if (!(spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX)) |
| 88 | memcpy(val_buf, buf_rx->data, val_len); |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 89 | |
| 90 | return 0; |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Marc Kleine-Budde | 1e81d52 | 2020-12-16 00:17:44 +0100 | [diff] [blame] | 93 | static 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 | |
| 100 | static 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-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 105 | static const struct regmap_config tcan4x5x_regmap = { |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 106 | .reg_bits = 24, |
Marc Kleine-Budde | aaf120c | 2020-12-16 00:17:42 +0100 | [diff] [blame] | 107 | .reg_stride = 4, |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 108 | .pad_bits = 8, |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 109 | .val_bits = 32, |
Marc Kleine-Budde | 1e81d52 | 2020-12-16 00:17:44 +0100 | [diff] [blame] | 110 | .wr_table = &tcan4x5x_reg_table, |
| 111 | .rd_table = &tcan4x5x_reg_table, |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 112 | .max_register = TCAN4X5X_MAX_REGISTER, |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 113 | .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-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
Marc Kleine-Budde | 1784aa1 | 2020-12-16 00:17:35 +0100 | [diff] [blame] | 120 | static const struct regmap_bus tcan4x5x_bus = { |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 121 | .write = tcan4x5x_regmap_write, |
Marc Kleine-Budde | 52be977 | 2020-12-16 00:17:38 +0100 | [diff] [blame] | 122 | .gather_write = tcan4x5x_regmap_gather_write, |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 123 | .read = tcan4x5x_regmap_read, |
Marc Kleine-Budde | 1c5d0fc | 2020-12-16 00:17:45 +0100 | [diff] [blame] | 124 | .reg_format_endian_default = REGMAP_ENDIAN_BIG, |
| 125 | .val_format_endian_default = REGMAP_ENDIAN_BIG, |
Marc Kleine-Budde | 5584114 | 2020-12-16 00:17:43 +0100 | [diff] [blame] | 126 | .max_raw_read = 256, |
| 127 | .max_raw_write = 256, |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | int tcan4x5x_regmap_init(struct tcan4x5x_priv *priv) |
| 131 | { |
| 132 | priv->regmap = devm_regmap_init(&priv->spi->dev, &tcan4x5x_bus, |
Marc Kleine-Budde | bf722fd | 2020-12-16 00:17:40 +0100 | [diff] [blame] | 133 | priv->spi, &tcan4x5x_regmap); |
Marc Kleine-Budde | 67def4ef | 2020-12-16 00:17:34 +0100 | [diff] [blame] | 134 | return PTR_ERR_OR_ZERO(priv->regmap); |
| 135 | } |