blob: c9fb864fcfa16471cae164968157d1e671db204d [file] [log] [blame]
Dan Murphy5443c222019-05-09 11:11:08 -05001// 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 Murphy2de49732019-12-04 11:51:12 -0600104#define TCAN4X5X_DISABLE_WAKE_MSK (BIT(31) | BIT(30))
105
Dan Murphy5443c222019-05-09 11:11:08 -0500106#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
115struct tcan4x5x_priv {
116 struct regmap *regmap;
117 struct spi_device *spi;
Dan Murphy5443c222019-05-09 11:11:08 -0500118
119 struct m_can_classdev *mcan_dev;
120
121 struct gpio_desc *reset_gpio;
Dan Murphy5443c222019-05-09 11:11:08 -0500122 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
131static 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
143static 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
155static 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
169static int regmap_spi_gather_write(void *context, const void *reg,
170 size_t reg_len, const void *val,
171 size_t val_len)
172{
173 struct device *dev = context;
174 struct spi_device *spi = to_spi_device(dev);
175 struct spi_message m;
176 u32 addr;
177 struct spi_transfer t[2] = {
178 { .tx_buf = &addr, .len = reg_len, .cs_change = 0,},
179 { .tx_buf = val, .len = val_len, },
180 };
181
Marc Kleine-Budde6093f742019-08-16 10:44:49 +0200182 addr = TCAN4X5X_WRITE_CMD | (*((u16 *)reg) << 8) | val_len >> 2;
Dan Murphy5443c222019-05-09 11:11:08 -0500183
184 spi_message_init(&m);
185 spi_message_add_tail(&t[0], &m);
186 spi_message_add_tail(&t[1], &m);
187
188 return spi_sync(spi, &m);
189}
190
191static int tcan4x5x_regmap_write(void *context, const void *data, size_t count)
192{
193 u16 *reg = (u16 *)(data);
194 const u32 *val = data + 4;
195
Marc Kleine-Budde6093f742019-08-16 10:44:49 +0200196 return regmap_spi_gather_write(context, reg, 4, val, count - 4);
Dan Murphy5443c222019-05-09 11:11:08 -0500197}
198
199static int regmap_spi_async_write(void *context,
200 const void *reg, size_t reg_len,
201 const void *val, size_t val_len,
202 struct regmap_async *a)
203{
204 return -ENOTSUPP;
205}
206
207static struct regmap_async *regmap_spi_async_alloc(void)
208{
209 return NULL;
210}
211
212static int tcan4x5x_regmap_read(void *context,
213 const void *reg, size_t reg_size,
214 void *val, size_t val_size)
215{
216 struct device *dev = context;
217 struct spi_device *spi = to_spi_device(dev);
218
219 u32 addr = TCAN4X5X_READ_CMD | (*((u16 *)reg) << 8) | val_size >> 2;
220
221 return spi_write_then_read(spi, &addr, reg_size, (u32 *)val, val_size);
222}
223
224static struct regmap_bus tcan4x5x_bus = {
225 .write = tcan4x5x_regmap_write,
226 .gather_write = regmap_spi_gather_write,
227 .async_write = regmap_spi_async_write,
228 .async_alloc = regmap_spi_async_alloc,
229 .read = tcan4x5x_regmap_read,
230 .read_flag_mask = 0x00,
231 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
232 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
233};
234
235static u32 tcan4x5x_read_reg(struct m_can_classdev *cdev, int reg)
236{
Marc Kleine-Buddead078192019-08-19 19:17:13 +0200237 struct tcan4x5x_priv *priv = cdev->device_data;
Dan Murphy5443c222019-05-09 11:11:08 -0500238 u32 val;
239
Dan Murphy5443c222019-05-09 11:11:08 -0500240 regmap_read(priv->regmap, priv->reg_offset + reg, &val);
241
242 return val;
243}
244
245static u32 tcan4x5x_read_fifo(struct m_can_classdev *cdev, int addr_offset)
246{
Marc Kleine-Buddead078192019-08-19 19:17:13 +0200247 struct tcan4x5x_priv *priv = cdev->device_data;
Dan Murphy5443c222019-05-09 11:11:08 -0500248 u32 val;
249
Dan Murphy5443c222019-05-09 11:11:08 -0500250 regmap_read(priv->regmap, priv->mram_start + addr_offset, &val);
251
252 return val;
253}
254
255static int tcan4x5x_write_reg(struct m_can_classdev *cdev, int reg, int val)
256{
Marc Kleine-Buddead078192019-08-19 19:17:13 +0200257 struct tcan4x5x_priv *priv = cdev->device_data;
Dan Murphy5443c222019-05-09 11:11:08 -0500258
Dan Murphy5443c222019-05-09 11:11:08 -0500259 return regmap_write(priv->regmap, priv->reg_offset + reg, val);
260}
261
262static int tcan4x5x_write_fifo(struct m_can_classdev *cdev,
263 int addr_offset, int val)
264{
Marc Kleine-Buddead078192019-08-19 19:17:13 +0200265 struct tcan4x5x_priv *priv = cdev->device_data;
Dan Murphy5443c222019-05-09 11:11:08 -0500266
Dan Murphy5443c222019-05-09 11:11:08 -0500267 return regmap_write(priv->regmap, priv->mram_start + addr_offset, val);
268}
269
270static int tcan4x5x_power_enable(struct regulator *reg, int enable)
271{
272 if (IS_ERR_OR_NULL(reg))
273 return 0;
274
275 if (enable)
276 return regulator_enable(reg);
277 else
278 return regulator_disable(reg);
279}
280
281static int tcan4x5x_write_tcan_reg(struct m_can_classdev *cdev,
282 int reg, int val)
283{
Marc Kleine-Buddead078192019-08-19 19:17:13 +0200284 struct tcan4x5x_priv *priv = cdev->device_data;
Dan Murphy5443c222019-05-09 11:11:08 -0500285
Dan Murphy5443c222019-05-09 11:11:08 -0500286 return regmap_write(priv->regmap, reg, val);
287}
288
289static int tcan4x5x_clear_interrupts(struct m_can_classdev *cdev)
290{
Dan Murphy5443c222019-05-09 11:11:08 -0500291 int ret;
292
Dan Murphy5443c222019-05-09 11:11:08 -0500293 ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_STATUS,
294 TCAN4X5X_CLEAR_ALL_INT);
295 if (ret)
296 return ret;
297
298 ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_MCAN_INT_REG,
299 TCAN4X5X_ENABLE_MCAN_INT);
300 if (ret)
301 return ret;
302
303 ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_INT_FLAGS,
304 TCAN4X5X_CLEAR_ALL_INT);
305 if (ret)
306 return ret;
307
308 ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_ERROR_STATUS,
309 TCAN4X5X_CLEAR_ALL_INT);
310 if (ret)
311 return ret;
312
313 return ret;
314}
315
316static int tcan4x5x_init(struct m_can_classdev *cdev)
317{
Marc Kleine-Buddead078192019-08-19 19:17:13 +0200318 struct tcan4x5x_priv *tcan4x5x = cdev->device_data;
Dan Murphy5443c222019-05-09 11:11:08 -0500319 int ret;
320
321 tcan4x5x_check_wake(tcan4x5x);
322
323 ret = tcan4x5x_clear_interrupts(cdev);
324 if (ret)
325 return ret;
326
327 ret = tcan4x5x_write_tcan_reg(cdev, TCAN4X5X_INT_EN,
328 TCAN4X5X_ENABLE_TCAN_INT);
329 if (ret)
330 return ret;
331
332 ret = regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG,
333 TCAN4X5X_MODE_SEL_MASK, TCAN4X5X_MODE_NORMAL);
334 if (ret)
335 return ret;
336
337 /* Zero out the MCAN buffers */
338 m_can_init_ram(cdev);
339
340 return ret;
341}
342
Dan Murphy2de49732019-12-04 11:51:12 -0600343static int tcan4x5x_disable_wake(struct m_can_classdev *cdev)
344{
345 struct tcan4x5x_priv *tcan4x5x = cdev->device_data;
346
347 return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG,
348 TCAN4X5X_DISABLE_WAKE_MSK, 0x00);
349}
350
Dan Murphy5443c222019-05-09 11:11:08 -0500351static int tcan4x5x_parse_config(struct m_can_classdev *cdev)
352{
Marc Kleine-Buddead078192019-08-19 19:17:13 +0200353 struct tcan4x5x_priv *tcan4x5x = cdev->device_data;
Dan Murphy5443c222019-05-09 11:11:08 -0500354
Dan Murphy5443c222019-05-09 11:11:08 -0500355 tcan4x5x->device_wake_gpio = devm_gpiod_get(cdev->dev, "device-wake",
356 GPIOD_OUT_HIGH);
357 if (IS_ERR(tcan4x5x->device_wake_gpio)) {
Dan Murphy2de49732019-12-04 11:51:12 -0600358 if (PTR_ERR(tcan4x5x->power) == -EPROBE_DEFER)
359 return -EPROBE_DEFER;
360
361 tcan4x5x_disable_wake(cdev);
Dan Murphy5443c222019-05-09 11:11:08 -0500362 }
363
364 tcan4x5x->reset_gpio = devm_gpiod_get_optional(cdev->dev, "reset",
365 GPIOD_OUT_LOW);
366 if (IS_ERR(tcan4x5x->reset_gpio))
367 tcan4x5x->reset_gpio = NULL;
368
Sean Nyekjaer60552252019-12-06 16:29:22 +0100369 usleep_range(700, 1000);
370
Dan Murphy5443c222019-05-09 11:11:08 -0500371 tcan4x5x->device_state_gpio = devm_gpiod_get_optional(cdev->dev,
372 "device-state",
373 GPIOD_IN);
374 if (IS_ERR(tcan4x5x->device_state_gpio))
375 tcan4x5x->device_state_gpio = NULL;
376
Dan Murphy5443c222019-05-09 11:11:08 -0500377 tcan4x5x->power = devm_regulator_get_optional(cdev->dev,
378 "vsup");
379 if (PTR_ERR(tcan4x5x->power) == -EPROBE_DEFER)
380 return -EPROBE_DEFER;
381
382 return 0;
383}
384
385static const struct regmap_config tcan4x5x_regmap = {
386 .reg_bits = 32,
387 .val_bits = 32,
388 .cache_type = REGCACHE_NONE,
389 .max_register = TCAN4X5X_MAX_REGISTER,
390};
391
392static struct m_can_ops tcan4x5x_ops = {
393 .init = tcan4x5x_init,
394 .read_reg = tcan4x5x_read_reg,
395 .write_reg = tcan4x5x_write_reg,
396 .write_fifo = tcan4x5x_write_fifo,
397 .read_fifo = tcan4x5x_read_fifo,
398 .clear_interrupts = tcan4x5x_clear_interrupts,
399};
400
401static int tcan4x5x_can_probe(struct spi_device *spi)
402{
403 struct tcan4x5x_priv *priv;
404 struct m_can_classdev *mcan_class;
405 int freq, ret;
406
407 mcan_class = m_can_class_allocate_dev(&spi->dev);
Marc Kleine-Budde7fbda132019-08-19 19:34:28 +0200408 if (!mcan_class)
409 return -ENOMEM;
410
Dan Murphy5443c222019-05-09 11:11:08 -0500411 priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
412 if (!priv)
413 return -ENOMEM;
414
415 mcan_class->device_data = priv;
416
417 m_can_class_get_clocks(mcan_class);
418 if (IS_ERR(mcan_class->cclk)) {
419 dev_err(&spi->dev, "no CAN clock source defined\n");
420 freq = TCAN4X5X_EXT_CLK_DEF;
421 } else {
422 freq = clk_get_rate(mcan_class->cclk);
423 }
424
425 /* Sanity check */
426 if (freq < 20000000 || freq > TCAN4X5X_EXT_CLK_DEF)
427 return -ERANGE;
428
429 priv->reg_offset = TCAN4X5X_MCAN_OFFSET;
430 priv->mram_start = TCAN4X5X_MRAM_START;
431 priv->spi = spi;
432 priv->mcan_dev = mcan_class;
433
434 mcan_class->pm_clock_support = 0;
435 mcan_class->can.clock.freq = freq;
436 mcan_class->dev = &spi->dev;
437 mcan_class->ops = &tcan4x5x_ops;
438 mcan_class->is_peripheral = true;
439 mcan_class->bit_timing = &tcan4x5x_bittiming_const;
440 mcan_class->data_timing = &tcan4x5x_data_bittiming_const;
Dan Murphybe1d2842019-08-23 12:50:57 -0500441 mcan_class->net->irq = spi->irq;
Dan Murphy5443c222019-05-09 11:11:08 -0500442
443 spi_set_drvdata(spi, priv);
444
Dan Murphy5443c222019-05-09 11:11:08 -0500445 /* Configure the SPI bus */
446 spi->bits_per_word = 32;
447 ret = spi_setup(spi);
448 if (ret)
449 goto out_clk;
450
451 priv->regmap = devm_regmap_init(&spi->dev, &tcan4x5x_bus,
452 &spi->dev, &tcan4x5x_regmap);
453
Dan Murphy2de49732019-12-04 11:51:12 -0600454 ret = tcan4x5x_parse_config(mcan_class);
455 if (ret)
456 goto out_clk;
457
Dan Murphy5443c222019-05-09 11:11:08 -0500458 tcan4x5x_power_enable(priv->power, 1);
459
Sean Nyekjaer3069ce62019-12-11 14:58:52 +0100460 ret = tcan4x5x_init(mcan_class);
461 if (ret)
462 goto out_power;
463
Dan Murphy5443c222019-05-09 11:11:08 -0500464 ret = m_can_class_register(mcan_class);
465 if (ret)
466 goto out_power;
467
468 netdev_info(mcan_class->net, "TCAN4X5X successfully initialized.\n");
469 return 0;
470
471out_power:
472 tcan4x5x_power_enable(priv->power, 0);
473out_clk:
474 if (!IS_ERR(mcan_class->cclk)) {
475 clk_disable_unprepare(mcan_class->cclk);
476 clk_disable_unprepare(mcan_class->hclk);
477 }
478
479 dev_err(&spi->dev, "Probe failed, err=%d\n", ret);
480 return ret;
481}
482
483static int tcan4x5x_can_remove(struct spi_device *spi)
484{
485 struct tcan4x5x_priv *priv = spi_get_drvdata(spi);
486
487 tcan4x5x_power_enable(priv->power, 0);
488
489 m_can_class_unregister(priv->mcan_dev);
490
491 return 0;
492}
493
494static const struct of_device_id tcan4x5x_of_match[] = {
495 { .compatible = "ti,tcan4x5x", },
496 { }
497};
498MODULE_DEVICE_TABLE(of, tcan4x5x_of_match);
499
500static const struct spi_device_id tcan4x5x_id_table[] = {
501 {
502 .name = "tcan4x5x",
503 .driver_data = 0,
504 },
505 { }
506};
507MODULE_DEVICE_TABLE(spi, tcan4x5x_id_table);
508
509static struct spi_driver tcan4x5x_can_driver = {
510 .driver = {
511 .name = DEVICE_NAME,
512 .of_match_table = tcan4x5x_of_match,
513 .pm = NULL,
514 },
515 .id_table = tcan4x5x_id_table,
516 .probe = tcan4x5x_can_probe,
517 .remove = tcan4x5x_can_remove,
518};
519module_spi_driver(tcan4x5x_can_driver);
520
521MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
522MODULE_DESCRIPTION("Texas Instruments TCAN4x5x CAN driver");
523MODULE_LICENSE("GPL v2");