18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// SPI to CAN driver for the Texas Instruments TCAN4x5x 38c2ecf20Sopenharmony_ci// Copyright (C) 2018-19 Texas Instruments Incorporated - http://www.ti.com/ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/regmap.h> 68c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 98c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include "m_can.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define DEVICE_NAME "tcan4x5x" 148c2ecf20Sopenharmony_ci#define TCAN4X5X_EXT_CLK_DEF 40000000 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define TCAN4X5X_DEV_ID0 0x00 178c2ecf20Sopenharmony_ci#define TCAN4X5X_DEV_ID1 0x04 188c2ecf20Sopenharmony_ci#define TCAN4X5X_REV 0x08 198c2ecf20Sopenharmony_ci#define TCAN4X5X_STATUS 0x0C 208c2ecf20Sopenharmony_ci#define TCAN4X5X_ERROR_STATUS 0x10 218c2ecf20Sopenharmony_ci#define TCAN4X5X_CONTROL 0x14 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define TCAN4X5X_CONFIG 0x800 248c2ecf20Sopenharmony_ci#define TCAN4X5X_TS_PRESCALE 0x804 258c2ecf20Sopenharmony_ci#define TCAN4X5X_TEST_REG 0x808 268c2ecf20Sopenharmony_ci#define TCAN4X5X_INT_FLAGS 0x820 278c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_INT_REG 0x824 288c2ecf20Sopenharmony_ci#define TCAN4X5X_INT_EN 0x830 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* Interrupt bits */ 318c2ecf20Sopenharmony_ci#define TCAN4X5X_CANBUSTERMOPEN_INT_EN BIT(30) 328c2ecf20Sopenharmony_ci#define TCAN4X5X_CANHCANL_INT_EN BIT(29) 338c2ecf20Sopenharmony_ci#define TCAN4X5X_CANHBAT_INT_EN BIT(28) 348c2ecf20Sopenharmony_ci#define TCAN4X5X_CANLGND_INT_EN BIT(27) 358c2ecf20Sopenharmony_ci#define TCAN4X5X_CANBUSOPEN_INT_EN BIT(26) 368c2ecf20Sopenharmony_ci#define TCAN4X5X_CANBUSGND_INT_EN BIT(25) 378c2ecf20Sopenharmony_ci#define TCAN4X5X_CANBUSBAT_INT_EN BIT(24) 388c2ecf20Sopenharmony_ci#define TCAN4X5X_UVSUP_INT_EN BIT(22) 398c2ecf20Sopenharmony_ci#define TCAN4X5X_UVIO_INT_EN BIT(21) 408c2ecf20Sopenharmony_ci#define TCAN4X5X_TSD_INT_EN BIT(19) 418c2ecf20Sopenharmony_ci#define TCAN4X5X_ECCERR_INT_EN BIT(16) 428c2ecf20Sopenharmony_ci#define TCAN4X5X_CANINT_INT_EN BIT(15) 438c2ecf20Sopenharmony_ci#define TCAN4X5X_LWU_INT_EN BIT(14) 448c2ecf20Sopenharmony_ci#define TCAN4X5X_CANSLNT_INT_EN BIT(10) 458c2ecf20Sopenharmony_ci#define TCAN4X5X_CANDOM_INT_EN BIT(8) 468c2ecf20Sopenharmony_ci#define TCAN4X5X_CANBUS_ERR_INT_EN BIT(5) 478c2ecf20Sopenharmony_ci#define TCAN4X5X_BUS_FAULT BIT(4) 488c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_INT BIT(1) 498c2ecf20Sopenharmony_ci#define TCAN4X5X_ENABLE_TCAN_INT \ 508c2ecf20Sopenharmony_ci (TCAN4X5X_MCAN_INT | TCAN4X5X_BUS_FAULT | \ 518c2ecf20Sopenharmony_ci TCAN4X5X_CANBUS_ERR_INT_EN | TCAN4X5X_CANINT_INT_EN) 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* MCAN Interrupt bits */ 548c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_ARA BIT(29) 558c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_PED BIT(28) 568c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_PEA BIT(27) 578c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_WD BIT(26) 588c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_BO BIT(25) 598c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_EW BIT(24) 608c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_EP BIT(23) 618c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_ELO BIT(22) 628c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_BEU BIT(21) 638c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_BEC BIT(20) 648c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_DRX BIT(19) 658c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TOO BIT(18) 668c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_MRAF BIT(17) 678c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TSW BIT(16) 688c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TEFL BIT(15) 698c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TEFF BIT(14) 708c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TEFW BIT(13) 718c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TEFN BIT(12) 728c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TFE BIT(11) 738c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TCF BIT(10) 748c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_TC BIT(9) 758c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_HPM BIT(8) 768c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF1L BIT(7) 778c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF1F BIT(6) 788c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF1W BIT(5) 798c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF1N BIT(4) 808c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF0L BIT(3) 818c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF0F BIT(2) 828c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF0W BIT(1) 838c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_IR_RF0N BIT(0) 848c2ecf20Sopenharmony_ci#define TCAN4X5X_ENABLE_MCAN_INT \ 858c2ecf20Sopenharmony_ci (TCAN4X5X_MCAN_IR_TC | TCAN4X5X_MCAN_IR_RF0N | \ 868c2ecf20Sopenharmony_ci TCAN4X5X_MCAN_IR_RF1N | TCAN4X5X_MCAN_IR_RF0F | \ 878c2ecf20Sopenharmony_ci TCAN4X5X_MCAN_IR_RF1F) 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci#define TCAN4X5X_MRAM_START 0x8000 908c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_OFFSET 0x1000 918c2ecf20Sopenharmony_ci#define TCAN4X5X_MAX_REGISTER 0x8ffc 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#define TCAN4X5X_CLEAR_ALL_INT 0xffffffff 948c2ecf20Sopenharmony_ci#define TCAN4X5X_SET_ALL_INT 0xffffffff 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci#define TCAN4X5X_WRITE_CMD (0x61 << 24) 978c2ecf20Sopenharmony_ci#define TCAN4X5X_READ_CMD (0x41 << 24) 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci#define TCAN4X5X_MODE_SEL_MASK (BIT(7) | BIT(6)) 1008c2ecf20Sopenharmony_ci#define TCAN4X5X_MODE_SLEEP 0x00 1018c2ecf20Sopenharmony_ci#define TCAN4X5X_MODE_STANDBY BIT(6) 1028c2ecf20Sopenharmony_ci#define TCAN4X5X_MODE_NORMAL BIT(7) 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci#define TCAN4X5X_DISABLE_WAKE_MSK (BIT(31) | BIT(30)) 1058c2ecf20Sopenharmony_ci#define TCAN4X5X_DISABLE_INH_MSK BIT(9) 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci#define TCAN4X5X_SW_RESET BIT(2) 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci#define TCAN4X5X_MCAN_CONFIGURED BIT(5) 1108c2ecf20Sopenharmony_ci#define TCAN4X5X_WATCHDOG_EN BIT(3) 1118c2ecf20Sopenharmony_ci#define TCAN4X5X_WD_60_MS_TIMER 0 1128c2ecf20Sopenharmony_ci#define TCAN4X5X_WD_600_MS_TIMER BIT(28) 1138c2ecf20Sopenharmony_ci#define TCAN4X5X_WD_3_S_TIMER BIT(29) 1148c2ecf20Sopenharmony_ci#define TCAN4X5X_WD_6_S_TIMER (BIT(28) | BIT(29)) 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistruct tcan4x5x_priv { 1178c2ecf20Sopenharmony_ci struct regmap *regmap; 1188c2ecf20Sopenharmony_ci struct spi_device *spi; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci struct m_can_classdev *mcan_dev; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci struct gpio_desc *reset_gpio; 1238c2ecf20Sopenharmony_ci struct gpio_desc *device_wake_gpio; 1248c2ecf20Sopenharmony_ci struct gpio_desc *device_state_gpio; 1258c2ecf20Sopenharmony_ci struct regulator *power; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* Register based ip */ 1288c2ecf20Sopenharmony_ci int mram_start; 1298c2ecf20Sopenharmony_ci int reg_offset; 1308c2ecf20Sopenharmony_ci}; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic void tcan4x5x_check_wake(struct tcan4x5x_priv *priv) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci int wake_state = 0; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (priv->device_state_gpio) 1378c2ecf20Sopenharmony_ci wake_state = gpiod_get_value(priv->device_state_gpio); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (priv->device_wake_gpio && wake_state) { 1408c2ecf20Sopenharmony_ci gpiod_set_value(priv->device_wake_gpio, 0); 1418c2ecf20Sopenharmony_ci usleep_range(5, 50); 1428c2ecf20Sopenharmony_ci gpiod_set_value(priv->device_wake_gpio, 1); 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci} 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic int tcan4x5x_reset(struct tcan4x5x_priv *priv) 1478c2ecf20Sopenharmony_ci{ 1488c2ecf20Sopenharmony_ci int ret = 0; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci if (priv->reset_gpio) { 1518c2ecf20Sopenharmony_ci gpiod_set_value(priv->reset_gpio, 1); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* tpulse_width minimum 30us */ 1548c2ecf20Sopenharmony_ci usleep_range(30, 100); 1558c2ecf20Sopenharmony_ci gpiod_set_value(priv->reset_gpio, 0); 1568c2ecf20Sopenharmony_ci } else { 1578c2ecf20Sopenharmony_ci ret = regmap_write(priv->regmap, TCAN4X5X_CONFIG, 1588c2ecf20Sopenharmony_ci TCAN4X5X_SW_RESET); 1598c2ecf20Sopenharmony_ci if (ret) 1608c2ecf20Sopenharmony_ci return ret; 1618c2ecf20Sopenharmony_ci } 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci usleep_range(700, 1000); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci return ret; 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic int regmap_spi_gather_write(void *context, const void *reg, 1698c2ecf20Sopenharmony_ci size_t reg_len, const void *val, 1708c2ecf20Sopenharmony_ci size_t val_len) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci struct device *dev = context; 1738c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 1748c2ecf20Sopenharmony_ci struct spi_message m; 1758c2ecf20Sopenharmony_ci u32 addr; 1768c2ecf20Sopenharmony_ci struct spi_transfer t[2] = { 1778c2ecf20Sopenharmony_ci { .tx_buf = &addr, .len = reg_len, .cs_change = 0,}, 1788c2ecf20Sopenharmony_ci { .tx_buf = val, .len = val_len, }, 1798c2ecf20Sopenharmony_ci }; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci addr = TCAN4X5X_WRITE_CMD | (*((u16 *)reg) << 8) | val_len >> 2; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci spi_message_init(&m); 1848c2ecf20Sopenharmony_ci spi_message_add_tail(&t[0], &m); 1858c2ecf20Sopenharmony_ci spi_message_add_tail(&t[1], &m); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci return spi_sync(spi, &m); 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic int tcan4x5x_regmap_write(void *context, const void *data, size_t count) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci u16 *reg = (u16 *)(data); 1938c2ecf20Sopenharmony_ci const u32 *val = data + 4; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci return regmap_spi_gather_write(context, reg, 4, val, count - 4); 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic int regmap_spi_async_write(void *context, 1998c2ecf20Sopenharmony_ci const void *reg, size_t reg_len, 2008c2ecf20Sopenharmony_ci const void *val, size_t val_len, 2018c2ecf20Sopenharmony_ci struct regmap_async *a) 2028c2ecf20Sopenharmony_ci{ 2038c2ecf20Sopenharmony_ci return -ENOTSUPP; 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic struct regmap_async *regmap_spi_async_alloc(void) 2078c2ecf20Sopenharmony_ci{ 2088c2ecf20Sopenharmony_ci return NULL; 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cistatic int tcan4x5x_regmap_read(void *context, 2128c2ecf20Sopenharmony_ci const void *reg, size_t reg_size, 2138c2ecf20Sopenharmony_ci void *val, size_t val_size) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci struct device *dev = context; 2168c2ecf20Sopenharmony_ci struct spi_device *spi = to_spi_device(dev); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci u32 addr = TCAN4X5X_READ_CMD | (*((u16 *)reg) << 8) | val_size >> 2; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci return spi_write_then_read(spi, &addr, reg_size, (u32 *)val, val_size); 2218c2ecf20Sopenharmony_ci} 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_cistatic struct regmap_bus tcan4x5x_bus = { 2248c2ecf20Sopenharmony_ci .write = tcan4x5x_regmap_write, 2258c2ecf20Sopenharmony_ci .gather_write = regmap_spi_gather_write, 2268c2ecf20Sopenharmony_ci .async_write = regmap_spi_async_write, 2278c2ecf20Sopenharmony_ci .async_alloc = regmap_spi_async_alloc, 2288c2ecf20Sopenharmony_ci .read = tcan4x5x_regmap_read, 2298c2ecf20Sopenharmony_ci .read_flag_mask = 0x00, 2308c2ecf20Sopenharmony_ci .reg_format_endian_default = REGMAP_ENDIAN_NATIVE, 2318c2ecf20Sopenharmony_ci .val_format_endian_default = REGMAP_ENDIAN_NATIVE, 2328c2ecf20Sopenharmony_ci}; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic u32 tcan4x5x_read_reg(struct m_can_classdev *cdev, int reg) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci struct tcan4x5x_priv *priv = cdev->device_data; 2378c2ecf20Sopenharmony_ci u32 val; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci regmap_read(priv->regmap, priv->reg_offset + reg, &val); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci return val; 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic u32 tcan4x5x_read_fifo(struct m_can_classdev *cdev, int addr_offset) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci struct tcan4x5x_priv *priv = cdev->device_data; 2478c2ecf20Sopenharmony_ci u32 val; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci regmap_read(priv->regmap, priv->mram_start + addr_offset, &val); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci return val; 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic int tcan4x5x_write_reg(struct m_can_classdev *cdev, int reg, int val) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci struct tcan4x5x_priv *priv = cdev->device_data; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci return regmap_write(priv->regmap, priv->reg_offset + reg, val); 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic int tcan4x5x_write_fifo(struct m_can_classdev *cdev, 2628c2ecf20Sopenharmony_ci int addr_offset, int val) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci struct tcan4x5x_priv *priv = cdev->device_data; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci return regmap_write(priv->regmap, priv->mram_start + addr_offset, val); 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic int tcan4x5x_power_enable(struct regulator *reg, int enable) 2708c2ecf20Sopenharmony_ci{ 2718c2ecf20Sopenharmony_ci if (IS_ERR_OR_NULL(reg)) 2728c2ecf20Sopenharmony_ci return 0; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci if (enable) 2758c2ecf20Sopenharmony_ci return regulator_enable(reg); 2768c2ecf20Sopenharmony_ci else 2778c2ecf20Sopenharmony_ci return regulator_disable(reg); 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cistatic int tcan4x5x_write_tcan_reg(struct m_can_classdev *cdev, 2818c2ecf20Sopenharmony_ci int reg, int val) 2828c2ecf20Sopenharmony_ci{ 2838c2ecf20Sopenharmony_ci struct tcan4x5x_priv *priv = cdev->device_data; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci return regmap_write(priv->regmap, reg, val); 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic int tcan4x5x_clear_interrupts(struct m_can_classdev *cdev) 2898c2ecf20Sopenharmony_ci{ 2908c2ecf20Sopenharmony_ci int ret; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_STATUS, 2938c2ecf20Sopenharmony_ci TCAN4X5X_CLEAR_ALL_INT); 2948c2ecf20Sopenharmony_ci if (ret) 2958c2ecf20Sopenharmony_ci return ret; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_INT_FLAGS, 2988c2ecf20Sopenharmony_ci TCAN4X5X_CLEAR_ALL_INT); 2998c2ecf20Sopenharmony_ci if (ret) 3008c2ecf20Sopenharmony_ci return ret; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_ERROR_STATUS, 3038c2ecf20Sopenharmony_ci TCAN4X5X_CLEAR_ALL_INT); 3048c2ecf20Sopenharmony_ci if (ret) 3058c2ecf20Sopenharmony_ci return ret; 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci return ret; 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic int tcan4x5x_init(struct m_can_classdev *cdev) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci struct tcan4x5x_priv *tcan4x5x = cdev->device_data; 3138c2ecf20Sopenharmony_ci int ret; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci tcan4x5x_check_wake(tcan4x5x); 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci ret = tcan4x5x_clear_interrupts(cdev); 3188c2ecf20Sopenharmony_ci if (ret) 3198c2ecf20Sopenharmony_ci return ret; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_INT_EN, 3228c2ecf20Sopenharmony_ci TCAN4X5X_ENABLE_TCAN_INT); 3238c2ecf20Sopenharmony_ci if (ret) 3248c2ecf20Sopenharmony_ci return ret; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci /* Zero out the MCAN buffers */ 3278c2ecf20Sopenharmony_ci m_can_init_ram(cdev); 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci ret = regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG, 3308c2ecf20Sopenharmony_ci TCAN4X5X_MODE_SEL_MASK, TCAN4X5X_MODE_NORMAL); 3318c2ecf20Sopenharmony_ci if (ret) 3328c2ecf20Sopenharmony_ci return ret; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci return ret; 3358c2ecf20Sopenharmony_ci} 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_cistatic int tcan4x5x_disable_wake(struct m_can_classdev *cdev) 3388c2ecf20Sopenharmony_ci{ 3398c2ecf20Sopenharmony_ci struct tcan4x5x_priv *tcan4x5x = cdev->device_data; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG, 3428c2ecf20Sopenharmony_ci TCAN4X5X_DISABLE_WAKE_MSK, 0x00); 3438c2ecf20Sopenharmony_ci} 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_cistatic int tcan4x5x_disable_state(struct m_can_classdev *cdev) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci struct tcan4x5x_priv *tcan4x5x = cdev->device_data; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG, 3508c2ecf20Sopenharmony_ci TCAN4X5X_DISABLE_INH_MSK, 0x01); 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_cistatic int tcan4x5x_parse_config(struct m_can_classdev *cdev) 3548c2ecf20Sopenharmony_ci{ 3558c2ecf20Sopenharmony_ci struct tcan4x5x_priv *tcan4x5x = cdev->device_data; 3568c2ecf20Sopenharmony_ci int ret; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci tcan4x5x->device_wake_gpio = devm_gpiod_get(cdev->dev, "device-wake", 3598c2ecf20Sopenharmony_ci GPIOD_OUT_HIGH); 3608c2ecf20Sopenharmony_ci if (IS_ERR(tcan4x5x->device_wake_gpio)) { 3618c2ecf20Sopenharmony_ci if (PTR_ERR(tcan4x5x->device_wake_gpio) == -EPROBE_DEFER) 3628c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci tcan4x5x_disable_wake(cdev); 3658c2ecf20Sopenharmony_ci } 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci tcan4x5x->reset_gpio = devm_gpiod_get_optional(cdev->dev, "reset", 3688c2ecf20Sopenharmony_ci GPIOD_OUT_LOW); 3698c2ecf20Sopenharmony_ci if (IS_ERR(tcan4x5x->reset_gpio)) 3708c2ecf20Sopenharmony_ci tcan4x5x->reset_gpio = NULL; 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci ret = tcan4x5x_reset(tcan4x5x); 3738c2ecf20Sopenharmony_ci if (ret) 3748c2ecf20Sopenharmony_ci return ret; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci tcan4x5x->device_state_gpio = devm_gpiod_get_optional(cdev->dev, 3778c2ecf20Sopenharmony_ci "device-state", 3788c2ecf20Sopenharmony_ci GPIOD_IN); 3798c2ecf20Sopenharmony_ci if (IS_ERR(tcan4x5x->device_state_gpio)) { 3808c2ecf20Sopenharmony_ci tcan4x5x->device_state_gpio = NULL; 3818c2ecf20Sopenharmony_ci tcan4x5x_disable_state(cdev); 3828c2ecf20Sopenharmony_ci } 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci return 0; 3858c2ecf20Sopenharmony_ci} 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_cistatic const struct regmap_config tcan4x5x_regmap = { 3888c2ecf20Sopenharmony_ci .reg_bits = 32, 3898c2ecf20Sopenharmony_ci .val_bits = 32, 3908c2ecf20Sopenharmony_ci .cache_type = REGCACHE_NONE, 3918c2ecf20Sopenharmony_ci .max_register = TCAN4X5X_MAX_REGISTER, 3928c2ecf20Sopenharmony_ci}; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_cistatic struct m_can_ops tcan4x5x_ops = { 3958c2ecf20Sopenharmony_ci .init = tcan4x5x_init, 3968c2ecf20Sopenharmony_ci .read_reg = tcan4x5x_read_reg, 3978c2ecf20Sopenharmony_ci .write_reg = tcan4x5x_write_reg, 3988c2ecf20Sopenharmony_ci .write_fifo = tcan4x5x_write_fifo, 3998c2ecf20Sopenharmony_ci .read_fifo = tcan4x5x_read_fifo, 4008c2ecf20Sopenharmony_ci .clear_interrupts = tcan4x5x_clear_interrupts, 4018c2ecf20Sopenharmony_ci}; 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_cistatic int tcan4x5x_can_probe(struct spi_device *spi) 4048c2ecf20Sopenharmony_ci{ 4058c2ecf20Sopenharmony_ci struct tcan4x5x_priv *priv; 4068c2ecf20Sopenharmony_ci struct m_can_classdev *mcan_class; 4078c2ecf20Sopenharmony_ci int freq, ret; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci mcan_class = m_can_class_allocate_dev(&spi->dev); 4108c2ecf20Sopenharmony_ci if (!mcan_class) 4118c2ecf20Sopenharmony_ci return -ENOMEM; 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL); 4148c2ecf20Sopenharmony_ci if (!priv) { 4158c2ecf20Sopenharmony_ci ret = -ENOMEM; 4168c2ecf20Sopenharmony_ci goto out_m_can_class_free_dev; 4178c2ecf20Sopenharmony_ci } 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci priv->power = devm_regulator_get_optional(&spi->dev, "vsup"); 4208c2ecf20Sopenharmony_ci if (PTR_ERR(priv->power) == -EPROBE_DEFER) { 4218c2ecf20Sopenharmony_ci ret = -EPROBE_DEFER; 4228c2ecf20Sopenharmony_ci goto out_m_can_class_free_dev; 4238c2ecf20Sopenharmony_ci } else { 4248c2ecf20Sopenharmony_ci priv->power = NULL; 4258c2ecf20Sopenharmony_ci } 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci mcan_class->device_data = priv; 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci m_can_class_get_clocks(mcan_class); 4308c2ecf20Sopenharmony_ci if (IS_ERR(mcan_class->cclk)) { 4318c2ecf20Sopenharmony_ci dev_err(&spi->dev, "no CAN clock source defined\n"); 4328c2ecf20Sopenharmony_ci freq = TCAN4X5X_EXT_CLK_DEF; 4338c2ecf20Sopenharmony_ci } else { 4348c2ecf20Sopenharmony_ci freq = clk_get_rate(mcan_class->cclk); 4358c2ecf20Sopenharmony_ci } 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci /* Sanity check */ 4388c2ecf20Sopenharmony_ci if (freq < 20000000 || freq > TCAN4X5X_EXT_CLK_DEF) { 4398c2ecf20Sopenharmony_ci ret = -ERANGE; 4408c2ecf20Sopenharmony_ci goto out_m_can_class_free_dev; 4418c2ecf20Sopenharmony_ci } 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci priv->reg_offset = TCAN4X5X_MCAN_OFFSET; 4448c2ecf20Sopenharmony_ci priv->mram_start = TCAN4X5X_MRAM_START; 4458c2ecf20Sopenharmony_ci priv->spi = spi; 4468c2ecf20Sopenharmony_ci priv->mcan_dev = mcan_class; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci mcan_class->pm_clock_support = 0; 4498c2ecf20Sopenharmony_ci mcan_class->can.clock.freq = freq; 4508c2ecf20Sopenharmony_ci mcan_class->dev = &spi->dev; 4518c2ecf20Sopenharmony_ci mcan_class->ops = &tcan4x5x_ops; 4528c2ecf20Sopenharmony_ci mcan_class->is_peripheral = true; 4538c2ecf20Sopenharmony_ci mcan_class->net->irq = spi->irq; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci spi_set_drvdata(spi, priv); 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci /* Configure the SPI bus */ 4588c2ecf20Sopenharmony_ci spi->bits_per_word = 32; 4598c2ecf20Sopenharmony_ci ret = spi_setup(spi); 4608c2ecf20Sopenharmony_ci if (ret) 4618c2ecf20Sopenharmony_ci goto out_m_can_class_free_dev; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci priv->regmap = devm_regmap_init(&spi->dev, &tcan4x5x_bus, 4648c2ecf20Sopenharmony_ci &spi->dev, &tcan4x5x_regmap); 4658c2ecf20Sopenharmony_ci if (IS_ERR(priv->regmap)) { 4668c2ecf20Sopenharmony_ci ret = PTR_ERR(priv->regmap); 4678c2ecf20Sopenharmony_ci goto out_m_can_class_free_dev; 4688c2ecf20Sopenharmony_ci } 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci ret = tcan4x5x_power_enable(priv->power, 1); 4718c2ecf20Sopenharmony_ci if (ret) 4728c2ecf20Sopenharmony_ci goto out_m_can_class_free_dev; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci ret = tcan4x5x_parse_config(mcan_class); 4758c2ecf20Sopenharmony_ci if (ret) 4768c2ecf20Sopenharmony_ci goto out_power; 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci ret = tcan4x5x_init(mcan_class); 4798c2ecf20Sopenharmony_ci if (ret) 4808c2ecf20Sopenharmony_ci goto out_power; 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci ret = m_can_class_register(mcan_class); 4838c2ecf20Sopenharmony_ci if (ret) 4848c2ecf20Sopenharmony_ci goto out_power; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci netdev_info(mcan_class->net, "TCAN4X5X successfully initialized.\n"); 4878c2ecf20Sopenharmony_ci return 0; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ciout_power: 4908c2ecf20Sopenharmony_ci tcan4x5x_power_enable(priv->power, 0); 4918c2ecf20Sopenharmony_ci out_m_can_class_free_dev: 4928c2ecf20Sopenharmony_ci m_can_class_free_dev(mcan_class->net); 4938c2ecf20Sopenharmony_ci dev_err(&spi->dev, "Probe failed, err=%d\n", ret); 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci return ret; 4968c2ecf20Sopenharmony_ci} 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_cistatic int tcan4x5x_can_remove(struct spi_device *spi) 4998c2ecf20Sopenharmony_ci{ 5008c2ecf20Sopenharmony_ci struct tcan4x5x_priv *priv = spi_get_drvdata(spi); 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci m_can_class_unregister(priv->mcan_dev); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci tcan4x5x_power_enable(priv->power, 0); 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci m_can_class_free_dev(priv->mcan_dev->net); 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci return 0; 5098c2ecf20Sopenharmony_ci} 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_cistatic const struct of_device_id tcan4x5x_of_match[] = { 5128c2ecf20Sopenharmony_ci { .compatible = "ti,tcan4x5x", }, 5138c2ecf20Sopenharmony_ci { } 5148c2ecf20Sopenharmony_ci}; 5158c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tcan4x5x_of_match); 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_cistatic const struct spi_device_id tcan4x5x_id_table[] = { 5188c2ecf20Sopenharmony_ci { 5198c2ecf20Sopenharmony_ci .name = "tcan4x5x", 5208c2ecf20Sopenharmony_ci .driver_data = 0, 5218c2ecf20Sopenharmony_ci }, 5228c2ecf20Sopenharmony_ci { } 5238c2ecf20Sopenharmony_ci}; 5248c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, tcan4x5x_id_table); 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_cistatic struct spi_driver tcan4x5x_can_driver = { 5278c2ecf20Sopenharmony_ci .driver = { 5288c2ecf20Sopenharmony_ci .name = DEVICE_NAME, 5298c2ecf20Sopenharmony_ci .of_match_table = tcan4x5x_of_match, 5308c2ecf20Sopenharmony_ci .pm = NULL, 5318c2ecf20Sopenharmony_ci }, 5328c2ecf20Sopenharmony_ci .id_table = tcan4x5x_id_table, 5338c2ecf20Sopenharmony_ci .probe = tcan4x5x_can_probe, 5348c2ecf20Sopenharmony_ci .remove = tcan4x5x_can_remove, 5358c2ecf20Sopenharmony_ci}; 5368c2ecf20Sopenharmony_cimodule_spi_driver(tcan4x5x_can_driver); 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ciMODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); 5398c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Texas Instruments TCAN4x5x CAN driver"); 5408c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 541