Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // SPI to CAN driver for the Texas Instruments TCAN4x5x |
| 3 | // Copyright (C) 2018-19 Texas Instruments Incorporated - http://www.ti.com/ |
| 4 | |
| 5 | #include <linux/regmap.h> |
| 6 | #include <linux/spi/spi.h> |
| 7 | |
| 8 | #include <linux/regulator/consumer.h> |
| 9 | #include <linux/gpio/consumer.h> |
| 10 | |
| 11 | #include "m_can.h" |
| 12 | |
| 13 | #define DEVICE_NAME "tcan4x5x" |
| 14 | #define TCAN4X5X_EXT_CLK_DEF 40000000 |
| 15 | |
| 16 | #define TCAN4X5X_DEV_ID0 0x00 |
| 17 | #define TCAN4X5X_DEV_ID1 0x04 |
| 18 | #define TCAN4X5X_REV 0x08 |
| 19 | #define TCAN4X5X_STATUS 0x0C |
| 20 | #define TCAN4X5X_ERROR_STATUS 0x10 |
| 21 | #define TCAN4X5X_CONTROL 0x14 |
| 22 | |
| 23 | #define TCAN4X5X_CONFIG 0x800 |
| 24 | #define TCAN4X5X_TS_PRESCALE 0x804 |
| 25 | #define TCAN4X5X_TEST_REG 0x808 |
| 26 | #define TCAN4X5X_INT_FLAGS 0x820 |
| 27 | #define TCAN4X5X_MCAN_INT_REG 0x824 |
| 28 | #define TCAN4X5X_INT_EN 0x830 |
| 29 | |
| 30 | /* Interrupt bits */ |
| 31 | #define TCAN4X5X_CANBUSTERMOPEN_INT_EN BIT(30) |
| 32 | #define TCAN4X5X_CANHCANL_INT_EN BIT(29) |
| 33 | #define TCAN4X5X_CANHBAT_INT_EN BIT(28) |
| 34 | #define TCAN4X5X_CANLGND_INT_EN BIT(27) |
| 35 | #define TCAN4X5X_CANBUSOPEN_INT_EN BIT(26) |
| 36 | #define TCAN4X5X_CANBUSGND_INT_EN BIT(25) |
| 37 | #define TCAN4X5X_CANBUSBAT_INT_EN BIT(24) |
| 38 | #define TCAN4X5X_UVSUP_INT_EN BIT(22) |
| 39 | #define TCAN4X5X_UVIO_INT_EN BIT(21) |
| 40 | #define TCAN4X5X_TSD_INT_EN BIT(19) |
| 41 | #define TCAN4X5X_ECCERR_INT_EN BIT(16) |
| 42 | #define TCAN4X5X_CANINT_INT_EN BIT(15) |
| 43 | #define TCAN4X5X_LWU_INT_EN BIT(14) |
| 44 | #define TCAN4X5X_CANSLNT_INT_EN BIT(10) |
| 45 | #define TCAN4X5X_CANDOM_INT_EN BIT(8) |
| 46 | #define TCAN4X5X_CANBUS_ERR_INT_EN BIT(5) |
| 47 | #define TCAN4X5X_BUS_FAULT BIT(4) |
| 48 | #define TCAN4X5X_MCAN_INT BIT(1) |
| 49 | #define TCAN4X5X_ENABLE_TCAN_INT \ |
| 50 | (TCAN4X5X_MCAN_INT | TCAN4X5X_BUS_FAULT | \ |
| 51 | TCAN4X5X_CANBUS_ERR_INT_EN | TCAN4X5X_CANINT_INT_EN) |
| 52 | |
| 53 | /* MCAN Interrupt bits */ |
| 54 | #define TCAN4X5X_MCAN_IR_ARA BIT(29) |
| 55 | #define TCAN4X5X_MCAN_IR_PED BIT(28) |
| 56 | #define TCAN4X5X_MCAN_IR_PEA BIT(27) |
| 57 | #define TCAN4X5X_MCAN_IR_WD BIT(26) |
| 58 | #define TCAN4X5X_MCAN_IR_BO BIT(25) |
| 59 | #define TCAN4X5X_MCAN_IR_EW BIT(24) |
| 60 | #define TCAN4X5X_MCAN_IR_EP BIT(23) |
| 61 | #define TCAN4X5X_MCAN_IR_ELO BIT(22) |
| 62 | #define TCAN4X5X_MCAN_IR_BEU BIT(21) |
| 63 | #define TCAN4X5X_MCAN_IR_BEC BIT(20) |
| 64 | #define TCAN4X5X_MCAN_IR_DRX BIT(19) |
| 65 | #define TCAN4X5X_MCAN_IR_TOO BIT(18) |
| 66 | #define TCAN4X5X_MCAN_IR_MRAF BIT(17) |
| 67 | #define TCAN4X5X_MCAN_IR_TSW BIT(16) |
| 68 | #define TCAN4X5X_MCAN_IR_TEFL BIT(15) |
| 69 | #define TCAN4X5X_MCAN_IR_TEFF BIT(14) |
| 70 | #define TCAN4X5X_MCAN_IR_TEFW BIT(13) |
| 71 | #define TCAN4X5X_MCAN_IR_TEFN BIT(12) |
| 72 | #define TCAN4X5X_MCAN_IR_TFE BIT(11) |
| 73 | #define TCAN4X5X_MCAN_IR_TCF BIT(10) |
| 74 | #define TCAN4X5X_MCAN_IR_TC BIT(9) |
| 75 | #define TCAN4X5X_MCAN_IR_HPM BIT(8) |
| 76 | #define TCAN4X5X_MCAN_IR_RF1L BIT(7) |
| 77 | #define TCAN4X5X_MCAN_IR_RF1F BIT(6) |
| 78 | #define TCAN4X5X_MCAN_IR_RF1W BIT(5) |
| 79 | #define TCAN4X5X_MCAN_IR_RF1N BIT(4) |
| 80 | #define TCAN4X5X_MCAN_IR_RF0L BIT(3) |
| 81 | #define TCAN4X5X_MCAN_IR_RF0F BIT(2) |
| 82 | #define TCAN4X5X_MCAN_IR_RF0W BIT(1) |
| 83 | #define TCAN4X5X_MCAN_IR_RF0N BIT(0) |
| 84 | #define TCAN4X5X_ENABLE_MCAN_INT \ |
| 85 | (TCAN4X5X_MCAN_IR_TC | TCAN4X5X_MCAN_IR_RF0N | \ |
| 86 | TCAN4X5X_MCAN_IR_RF1N | TCAN4X5X_MCAN_IR_RF0F | \ |
| 87 | TCAN4X5X_MCAN_IR_RF1F) |
| 88 | |
| 89 | #define TCAN4X5X_MRAM_START 0x8000 |
| 90 | #define TCAN4X5X_MCAN_OFFSET 0x1000 |
| 91 | #define TCAN4X5X_MAX_REGISTER 0x8fff |
| 92 | |
| 93 | #define TCAN4X5X_CLEAR_ALL_INT 0xffffffff |
| 94 | #define TCAN4X5X_SET_ALL_INT 0xffffffff |
| 95 | |
| 96 | #define TCAN4X5X_WRITE_CMD (0x61 << 24) |
| 97 | #define TCAN4X5X_READ_CMD (0x41 << 24) |
| 98 | |
| 99 | #define TCAN4X5X_MODE_SEL_MASK (BIT(7) | BIT(6)) |
| 100 | #define TCAN4X5X_MODE_SLEEP 0x00 |
| 101 | #define TCAN4X5X_MODE_STANDBY BIT(6) |
| 102 | #define TCAN4X5X_MODE_NORMAL BIT(7) |
| 103 | |
Dan Murphy | 2de4973 | 2019-12-04 11:51:12 -0600 | [diff] [blame] | 104 | #define TCAN4X5X_DISABLE_WAKE_MSK (BIT(31) | BIT(30)) |
| 105 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 106 | #define TCAN4X5X_SW_RESET BIT(2) |
| 107 | |
| 108 | #define TCAN4X5X_MCAN_CONFIGURED BIT(5) |
| 109 | #define TCAN4X5X_WATCHDOG_EN BIT(3) |
| 110 | #define TCAN4X5X_WD_60_MS_TIMER 0 |
| 111 | #define TCAN4X5X_WD_600_MS_TIMER BIT(28) |
| 112 | #define TCAN4X5X_WD_3_S_TIMER BIT(29) |
| 113 | #define TCAN4X5X_WD_6_S_TIMER (BIT(28) | BIT(29)) |
| 114 | |
| 115 | struct tcan4x5x_priv { |
| 116 | struct regmap *regmap; |
| 117 | struct spi_device *spi; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 118 | |
| 119 | struct m_can_classdev *mcan_dev; |
| 120 | |
| 121 | struct gpio_desc *reset_gpio; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 122 | struct gpio_desc *device_wake_gpio; |
| 123 | struct gpio_desc *device_state_gpio; |
| 124 | struct regulator *power; |
| 125 | |
| 126 | /* Register based ip */ |
| 127 | int mram_start; |
| 128 | int reg_offset; |
| 129 | }; |
| 130 | |
| 131 | static struct can_bittiming_const tcan4x5x_bittiming_const = { |
| 132 | .name = DEVICE_NAME, |
| 133 | .tseg1_min = 2, |
| 134 | .tseg1_max = 31, |
| 135 | .tseg2_min = 2, |
| 136 | .tseg2_max = 16, |
| 137 | .sjw_max = 16, |
| 138 | .brp_min = 1, |
| 139 | .brp_max = 32, |
| 140 | .brp_inc = 1, |
| 141 | }; |
| 142 | |
| 143 | static struct can_bittiming_const tcan4x5x_data_bittiming_const = { |
| 144 | .name = DEVICE_NAME, |
| 145 | .tseg1_min = 1, |
| 146 | .tseg1_max = 32, |
| 147 | .tseg2_min = 1, |
| 148 | .tseg2_max = 16, |
| 149 | .sjw_max = 16, |
| 150 | .brp_min = 1, |
| 151 | .brp_max = 32, |
| 152 | .brp_inc = 1, |
| 153 | }; |
| 154 | |
| 155 | static void tcan4x5x_check_wake(struct tcan4x5x_priv *priv) |
| 156 | { |
| 157 | int wake_state = 0; |
| 158 | |
| 159 | if (priv->device_state_gpio) |
| 160 | wake_state = gpiod_get_value(priv->device_state_gpio); |
| 161 | |
| 162 | if (priv->device_wake_gpio && wake_state) { |
| 163 | gpiod_set_value(priv->device_wake_gpio, 0); |
| 164 | usleep_range(5, 50); |
| 165 | gpiod_set_value(priv->device_wake_gpio, 1); |
| 166 | } |
| 167 | } |
| 168 | |
Sean Nyekjaer | c308312 | 2019-12-11 14:58:51 +0100 | [diff] [blame^] | 169 | static int tcan4x5x_reset(struct tcan4x5x_priv *priv) |
| 170 | { |
| 171 | int ret = 0; |
| 172 | |
| 173 | if (priv->reset_gpio) { |
| 174 | gpiod_set_value(priv->reset_gpio, 1); |
| 175 | |
| 176 | /* tpulse_width minimum 30us */ |
| 177 | usleep_range(30, 100); |
| 178 | gpiod_set_value(priv->reset_gpio, 0); |
| 179 | } else { |
| 180 | ret = regmap_write(priv->regmap, TCAN4X5X_CONFIG, |
| 181 | TCAN4X5X_SW_RESET); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | usleep_range(700, 1000); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 191 | static int regmap_spi_gather_write(void *context, const void *reg, |
| 192 | size_t reg_len, const void *val, |
| 193 | size_t val_len) |
| 194 | { |
| 195 | struct device *dev = context; |
| 196 | struct spi_device *spi = to_spi_device(dev); |
| 197 | struct spi_message m; |
| 198 | u32 addr; |
| 199 | struct spi_transfer t[2] = { |
| 200 | { .tx_buf = &addr, .len = reg_len, .cs_change = 0,}, |
| 201 | { .tx_buf = val, .len = val_len, }, |
| 202 | }; |
| 203 | |
Marc Kleine-Budde | 6093f74 | 2019-08-16 10:44:49 +0200 | [diff] [blame] | 204 | addr = TCAN4X5X_WRITE_CMD | (*((u16 *)reg) << 8) | val_len >> 2; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 205 | |
| 206 | spi_message_init(&m); |
| 207 | spi_message_add_tail(&t[0], &m); |
| 208 | spi_message_add_tail(&t[1], &m); |
| 209 | |
| 210 | return spi_sync(spi, &m); |
| 211 | } |
| 212 | |
| 213 | static int tcan4x5x_regmap_write(void *context, const void *data, size_t count) |
| 214 | { |
| 215 | u16 *reg = (u16 *)(data); |
| 216 | const u32 *val = data + 4; |
| 217 | |
Marc Kleine-Budde | 6093f74 | 2019-08-16 10:44:49 +0200 | [diff] [blame] | 218 | return regmap_spi_gather_write(context, reg, 4, val, count - 4); |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int regmap_spi_async_write(void *context, |
| 222 | const void *reg, size_t reg_len, |
| 223 | const void *val, size_t val_len, |
| 224 | struct regmap_async *a) |
| 225 | { |
| 226 | return -ENOTSUPP; |
| 227 | } |
| 228 | |
| 229 | static struct regmap_async *regmap_spi_async_alloc(void) |
| 230 | { |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | static int tcan4x5x_regmap_read(void *context, |
| 235 | const void *reg, size_t reg_size, |
| 236 | void *val, size_t val_size) |
| 237 | { |
| 238 | struct device *dev = context; |
| 239 | struct spi_device *spi = to_spi_device(dev); |
| 240 | |
| 241 | u32 addr = TCAN4X5X_READ_CMD | (*((u16 *)reg) << 8) | val_size >> 2; |
| 242 | |
| 243 | return spi_write_then_read(spi, &addr, reg_size, (u32 *)val, val_size); |
| 244 | } |
| 245 | |
| 246 | static struct regmap_bus tcan4x5x_bus = { |
| 247 | .write = tcan4x5x_regmap_write, |
| 248 | .gather_write = regmap_spi_gather_write, |
| 249 | .async_write = regmap_spi_async_write, |
| 250 | .async_alloc = regmap_spi_async_alloc, |
| 251 | .read = tcan4x5x_regmap_read, |
| 252 | .read_flag_mask = 0x00, |
| 253 | .reg_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 254 | .val_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 255 | }; |
| 256 | |
| 257 | static u32 tcan4x5x_read_reg(struct m_can_classdev *cdev, int reg) |
| 258 | { |
Marc Kleine-Budde | ad07819 | 2019-08-19 19:17:13 +0200 | [diff] [blame] | 259 | struct tcan4x5x_priv *priv = cdev->device_data; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 260 | u32 val; |
| 261 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 262 | regmap_read(priv->regmap, priv->reg_offset + reg, &val); |
| 263 | |
| 264 | return val; |
| 265 | } |
| 266 | |
| 267 | static u32 tcan4x5x_read_fifo(struct m_can_classdev *cdev, int addr_offset) |
| 268 | { |
Marc Kleine-Budde | ad07819 | 2019-08-19 19:17:13 +0200 | [diff] [blame] | 269 | struct tcan4x5x_priv *priv = cdev->device_data; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 270 | u32 val; |
| 271 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 272 | regmap_read(priv->regmap, priv->mram_start + addr_offset, &val); |
| 273 | |
| 274 | return val; |
| 275 | } |
| 276 | |
| 277 | static int tcan4x5x_write_reg(struct m_can_classdev *cdev, int reg, int val) |
| 278 | { |
Marc Kleine-Budde | ad07819 | 2019-08-19 19:17:13 +0200 | [diff] [blame] | 279 | struct tcan4x5x_priv *priv = cdev->device_data; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 280 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 281 | return regmap_write(priv->regmap, priv->reg_offset + reg, val); |
| 282 | } |
| 283 | |
| 284 | static int tcan4x5x_write_fifo(struct m_can_classdev *cdev, |
| 285 | int addr_offset, int val) |
| 286 | { |
Marc Kleine-Budde | ad07819 | 2019-08-19 19:17:13 +0200 | [diff] [blame] | 287 | struct tcan4x5x_priv *priv = cdev->device_data; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 288 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 289 | return regmap_write(priv->regmap, priv->mram_start + addr_offset, val); |
| 290 | } |
| 291 | |
| 292 | static int tcan4x5x_power_enable(struct regulator *reg, int enable) |
| 293 | { |
| 294 | if (IS_ERR_OR_NULL(reg)) |
| 295 | return 0; |
| 296 | |
| 297 | if (enable) |
| 298 | return regulator_enable(reg); |
| 299 | else |
| 300 | return regulator_disable(reg); |
| 301 | } |
| 302 | |
| 303 | static int tcan4x5x_write_tcan_reg(struct m_can_classdev *cdev, |
| 304 | int reg, int val) |
| 305 | { |
Marc Kleine-Budde | ad07819 | 2019-08-19 19:17:13 +0200 | [diff] [blame] | 306 | struct tcan4x5x_priv *priv = cdev->device_data; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 307 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 308 | return regmap_write(priv->regmap, reg, val); |
| 309 | } |
| 310 | |
| 311 | static int tcan4x5x_clear_interrupts(struct m_can_classdev *cdev) |
| 312 | { |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 313 | int ret; |
| 314 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 315 | ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_STATUS, |
| 316 | TCAN4X5X_CLEAR_ALL_INT); |
| 317 | if (ret) |
| 318 | return ret; |
| 319 | |
| 320 | ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_MCAN_INT_REG, |
| 321 | TCAN4X5X_ENABLE_MCAN_INT); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
| 325 | ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_INT_FLAGS, |
| 326 | TCAN4X5X_CLEAR_ALL_INT); |
| 327 | if (ret) |
| 328 | return ret; |
| 329 | |
| 330 | ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_ERROR_STATUS, |
| 331 | TCAN4X5X_CLEAR_ALL_INT); |
| 332 | if (ret) |
| 333 | return ret; |
| 334 | |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | static int tcan4x5x_init(struct m_can_classdev *cdev) |
| 339 | { |
Marc Kleine-Budde | ad07819 | 2019-08-19 19:17:13 +0200 | [diff] [blame] | 340 | struct tcan4x5x_priv *tcan4x5x = cdev->device_data; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 341 | int ret; |
| 342 | |
| 343 | tcan4x5x_check_wake(tcan4x5x); |
| 344 | |
| 345 | ret = tcan4x5x_clear_interrupts(cdev); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | |
| 349 | ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_INT_EN, |
| 350 | TCAN4X5X_ENABLE_TCAN_INT); |
| 351 | if (ret) |
| 352 | return ret; |
| 353 | |
| 354 | ret = regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG, |
| 355 | TCAN4X5X_MODE_SEL_MASK, TCAN4X5X_MODE_NORMAL); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | |
| 359 | /* Zero out the MCAN buffers */ |
| 360 | m_can_init_ram(cdev); |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
Dan Murphy | 2de4973 | 2019-12-04 11:51:12 -0600 | [diff] [blame] | 365 | static int tcan4x5x_disable_wake(struct m_can_classdev *cdev) |
| 366 | { |
| 367 | struct tcan4x5x_priv *tcan4x5x = cdev->device_data; |
| 368 | |
| 369 | return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG, |
| 370 | TCAN4X5X_DISABLE_WAKE_MSK, 0x00); |
| 371 | } |
| 372 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 373 | static int tcan4x5x_parse_config(struct m_can_classdev *cdev) |
| 374 | { |
Marc Kleine-Budde | ad07819 | 2019-08-19 19:17:13 +0200 | [diff] [blame] | 375 | struct tcan4x5x_priv *tcan4x5x = cdev->device_data; |
Sean Nyekjaer | c308312 | 2019-12-11 14:58:51 +0100 | [diff] [blame^] | 376 | int ret; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 377 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 378 | tcan4x5x->device_wake_gpio = devm_gpiod_get(cdev->dev, "device-wake", |
| 379 | GPIOD_OUT_HIGH); |
| 380 | if (IS_ERR(tcan4x5x->device_wake_gpio)) { |
Dan Murphy | 2de4973 | 2019-12-04 11:51:12 -0600 | [diff] [blame] | 381 | if (PTR_ERR(tcan4x5x->power) == -EPROBE_DEFER) |
| 382 | return -EPROBE_DEFER; |
| 383 | |
| 384 | tcan4x5x_disable_wake(cdev); |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | tcan4x5x->reset_gpio = devm_gpiod_get_optional(cdev->dev, "reset", |
| 388 | GPIOD_OUT_LOW); |
| 389 | if (IS_ERR(tcan4x5x->reset_gpio)) |
| 390 | tcan4x5x->reset_gpio = NULL; |
| 391 | |
Sean Nyekjaer | c308312 | 2019-12-11 14:58:51 +0100 | [diff] [blame^] | 392 | ret = tcan4x5x_reset(tcan4x5x); |
| 393 | if (ret) |
| 394 | return ret; |
Sean Nyekjaer | 6055225 | 2019-12-06 16:29:22 +0100 | [diff] [blame] | 395 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 396 | tcan4x5x->device_state_gpio = devm_gpiod_get_optional(cdev->dev, |
| 397 | "device-state", |
| 398 | GPIOD_IN); |
| 399 | if (IS_ERR(tcan4x5x->device_state_gpio)) |
| 400 | tcan4x5x->device_state_gpio = NULL; |
| 401 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static const struct regmap_config tcan4x5x_regmap = { |
| 406 | .reg_bits = 32, |
| 407 | .val_bits = 32, |
| 408 | .cache_type = REGCACHE_NONE, |
| 409 | .max_register = TCAN4X5X_MAX_REGISTER, |
| 410 | }; |
| 411 | |
| 412 | static struct m_can_ops tcan4x5x_ops = { |
| 413 | .init = tcan4x5x_init, |
| 414 | .read_reg = tcan4x5x_read_reg, |
| 415 | .write_reg = tcan4x5x_write_reg, |
| 416 | .write_fifo = tcan4x5x_write_fifo, |
| 417 | .read_fifo = tcan4x5x_read_fifo, |
| 418 | .clear_interrupts = tcan4x5x_clear_interrupts, |
| 419 | }; |
| 420 | |
| 421 | static int tcan4x5x_can_probe(struct spi_device *spi) |
| 422 | { |
| 423 | struct tcan4x5x_priv *priv; |
| 424 | struct m_can_classdev *mcan_class; |
| 425 | int freq, ret; |
| 426 | |
| 427 | mcan_class = m_can_class_allocate_dev(&spi->dev); |
Marc Kleine-Budde | 7fbda13 | 2019-08-19 19:34:28 +0200 | [diff] [blame] | 428 | if (!mcan_class) |
| 429 | return -ENOMEM; |
| 430 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 431 | priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL); |
| 432 | if (!priv) |
| 433 | return -ENOMEM; |
| 434 | |
Dan Murphy | 3814ca3 | 2019-12-10 10:32:04 -0600 | [diff] [blame] | 435 | priv->power = devm_regulator_get_optional(&spi->dev, "vsup"); |
| 436 | if (PTR_ERR(priv->power) == -EPROBE_DEFER) |
| 437 | return -EPROBE_DEFER; |
| 438 | else |
| 439 | priv->power = NULL; |
| 440 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 441 | mcan_class->device_data = priv; |
| 442 | |
| 443 | m_can_class_get_clocks(mcan_class); |
| 444 | if (IS_ERR(mcan_class->cclk)) { |
| 445 | dev_err(&spi->dev, "no CAN clock source defined\n"); |
| 446 | freq = TCAN4X5X_EXT_CLK_DEF; |
| 447 | } else { |
| 448 | freq = clk_get_rate(mcan_class->cclk); |
| 449 | } |
| 450 | |
| 451 | /* Sanity check */ |
| 452 | if (freq < 20000000 || freq > TCAN4X5X_EXT_CLK_DEF) |
| 453 | return -ERANGE; |
| 454 | |
| 455 | priv->reg_offset = TCAN4X5X_MCAN_OFFSET; |
| 456 | priv->mram_start = TCAN4X5X_MRAM_START; |
| 457 | priv->spi = spi; |
| 458 | priv->mcan_dev = mcan_class; |
| 459 | |
| 460 | mcan_class->pm_clock_support = 0; |
| 461 | mcan_class->can.clock.freq = freq; |
| 462 | mcan_class->dev = &spi->dev; |
| 463 | mcan_class->ops = &tcan4x5x_ops; |
| 464 | mcan_class->is_peripheral = true; |
| 465 | mcan_class->bit_timing = &tcan4x5x_bittiming_const; |
| 466 | mcan_class->data_timing = &tcan4x5x_data_bittiming_const; |
Dan Murphy | be1d284 | 2019-08-23 12:50:57 -0500 | [diff] [blame] | 467 | mcan_class->net->irq = spi->irq; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 468 | |
| 469 | spi_set_drvdata(spi, priv); |
| 470 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 471 | /* Configure the SPI bus */ |
| 472 | spi->bits_per_word = 32; |
| 473 | ret = spi_setup(spi); |
| 474 | if (ret) |
| 475 | goto out_clk; |
| 476 | |
| 477 | priv->regmap = devm_regmap_init(&spi->dev, &tcan4x5x_bus, |
| 478 | &spi->dev, &tcan4x5x_regmap); |
| 479 | |
Dan Murphy | 3814ca3 | 2019-12-10 10:32:04 -0600 | [diff] [blame] | 480 | ret = tcan4x5x_power_enable(priv->power, 1); |
Dan Murphy | 2de4973 | 2019-12-04 11:51:12 -0600 | [diff] [blame] | 481 | if (ret) |
| 482 | goto out_clk; |
| 483 | |
Dan Murphy | 3814ca3 | 2019-12-10 10:32:04 -0600 | [diff] [blame] | 484 | ret = tcan4x5x_parse_config(mcan_class); |
| 485 | if (ret) |
| 486 | goto out_power; |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 487 | |
Sean Nyekjaer | 3069ce6 | 2019-12-11 14:58:52 +0100 | [diff] [blame] | 488 | ret = tcan4x5x_init(mcan_class); |
| 489 | if (ret) |
| 490 | goto out_power; |
| 491 | |
Dan Murphy | 5443c22 | 2019-05-09 11:11:08 -0500 | [diff] [blame] | 492 | ret = m_can_class_register(mcan_class); |
| 493 | if (ret) |
| 494 | goto out_power; |
| 495 | |
| 496 | netdev_info(mcan_class->net, "TCAN4X5X successfully initialized.\n"); |
| 497 | return 0; |
| 498 | |
| 499 | out_power: |
| 500 | tcan4x5x_power_enable(priv->power, 0); |
| 501 | out_clk: |
| 502 | if (!IS_ERR(mcan_class->cclk)) { |
| 503 | clk_disable_unprepare(mcan_class->cclk); |
| 504 | clk_disable_unprepare(mcan_class->hclk); |
| 505 | } |
| 506 | |
| 507 | dev_err(&spi->dev, "Probe failed, err=%d\n", ret); |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | static int tcan4x5x_can_remove(struct spi_device *spi) |
| 512 | { |
| 513 | struct tcan4x5x_priv *priv = spi_get_drvdata(spi); |
| 514 | |
| 515 | tcan4x5x_power_enable(priv->power, 0); |
| 516 | |
| 517 | m_can_class_unregister(priv->mcan_dev); |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static const struct of_device_id tcan4x5x_of_match[] = { |
| 523 | { .compatible = "ti,tcan4x5x", }, |
| 524 | { } |
| 525 | }; |
| 526 | MODULE_DEVICE_TABLE(of, tcan4x5x_of_match); |
| 527 | |
| 528 | static const struct spi_device_id tcan4x5x_id_table[] = { |
| 529 | { |
| 530 | .name = "tcan4x5x", |
| 531 | .driver_data = 0, |
| 532 | }, |
| 533 | { } |
| 534 | }; |
| 535 | MODULE_DEVICE_TABLE(spi, tcan4x5x_id_table); |
| 536 | |
| 537 | static struct spi_driver tcan4x5x_can_driver = { |
| 538 | .driver = { |
| 539 | .name = DEVICE_NAME, |
| 540 | .of_match_table = tcan4x5x_of_match, |
| 541 | .pm = NULL, |
| 542 | }, |
| 543 | .id_table = tcan4x5x_id_table, |
| 544 | .probe = tcan4x5x_can_probe, |
| 545 | .remove = tcan4x5x_can_remove, |
| 546 | }; |
| 547 | module_spi_driver(tcan4x5x_can_driver); |
| 548 | |
| 549 | MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); |
| 550 | MODULE_DESCRIPTION("Texas Instruments TCAN4x5x CAN driver"); |
| 551 | MODULE_LICENSE("GPL v2"); |